1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Basic worker thread pool for io_uring
4 *
5 * Copyright (C) 2019 Jens Axboe
6 *
7 */
8 #include <linux/kernel.h>
9 #include <linux/init.h>
10 #include <linux/errno.h>
11 #include <linux/sched/signal.h>
12 #include <linux/percpu.h>
13 #include <linux/slab.h>
14 #include <linux/rculist_nulls.h>
15 #include <linux/cpu.h>
16 #include <linux/task_work.h>
17 #include <linux/audit.h>
18 #include <uapi/linux/io_uring.h>
19
20 #include "io-wq.h"
21 #include "slist.h"
22 #include "io_uring.h"
23
24 #define WORKER_IDLE_TIMEOUT (5 * HZ)
25
26 enum {
27 IO_WORKER_F_UP = 1, /* up and active */
28 IO_WORKER_F_RUNNING = 2, /* account as running */
29 IO_WORKER_F_FREE = 4, /* worker on free list */
30 IO_WORKER_F_BOUND = 8, /* is doing bounded work */
31 };
32
33 enum {
34 IO_WQ_BIT_EXIT = 0, /* wq exiting */
35 };
36
37 enum {
38 IO_ACCT_STALLED_BIT = 0, /* stalled on hash */
39 };
40
41 /*
42 * One for each thread in a wqe pool
43 */
44 struct io_worker {
45 refcount_t ref;
46 unsigned flags;
47 struct hlist_nulls_node nulls_node;
48 struct list_head all_list;
49 struct task_struct *task;
50 struct io_wqe *wqe;
51
52 struct io_wq_work *cur_work;
53 struct io_wq_work *next_work;
54 raw_spinlock_t lock;
55
56 struct completion ref_done;
57
58 unsigned long create_state;
59 struct callback_head create_work;
60 int create_index;
61
62 union {
63 struct rcu_head rcu;
64 struct work_struct work;
65 };
66 };
67
68 #if BITS_PER_LONG == 64
69 #define IO_WQ_HASH_ORDER 6
70 #else
71 #define IO_WQ_HASH_ORDER 5
72 #endif
73
74 #define IO_WQ_NR_HASH_BUCKETS (1u << IO_WQ_HASH_ORDER)
75
76 struct io_wqe_acct {
77 unsigned nr_workers;
78 unsigned max_workers;
79 int index;
80 atomic_t nr_running;
81 raw_spinlock_t lock;
82 struct io_wq_work_list work_list;
83 unsigned long flags;
84 };
85
86 enum {
87 IO_WQ_ACCT_BOUND,
88 IO_WQ_ACCT_UNBOUND,
89 IO_WQ_ACCT_NR,
90 };
91
92 /*
93 * Per-node worker thread pool
94 */
95 struct io_wqe {
96 raw_spinlock_t lock;
97 struct io_wqe_acct acct[IO_WQ_ACCT_NR];
98
99 int node;
100
101 struct hlist_nulls_head free_list;
102 struct list_head all_list;
103
104 struct wait_queue_entry wait;
105
106 struct io_wq *wq;
107 struct io_wq_work *hash_tail[IO_WQ_NR_HASH_BUCKETS];
108
109 cpumask_var_t cpu_mask;
110 };
111
112 /*
113 * Per io_wq state
114 */
115 struct io_wq {
116 unsigned long state;
117
118 free_work_fn *free_work;
119 io_wq_work_fn *do_work;
120
121 struct io_wq_hash *hash;
122
123 atomic_t worker_refs;
124 struct completion worker_done;
125
126 struct hlist_node cpuhp_node;
127
128 struct task_struct *task;
129
130 struct io_wqe *wqes[];
131 };
132
133 static enum cpuhp_state io_wq_online;
134
135 struct io_cb_cancel_data {
136 work_cancel_fn *fn;
137 void *data;
138 int nr_running;
139 int nr_pending;
140 bool cancel_all;
141 };
142
143 static bool create_io_worker(struct io_wq *wq, struct io_wqe *wqe, int index);
144 static void io_wqe_dec_running(struct io_worker *worker);
145 static bool io_acct_cancel_pending_work(struct io_wqe *wqe,
146 struct io_wqe_acct *acct,
147 struct io_cb_cancel_data *match);
148 static void create_worker_cb(struct callback_head *cb);
149 static void io_wq_cancel_tw_create(struct io_wq *wq);
150
io_worker_get(struct io_worker * worker)151 static bool io_worker_get(struct io_worker *worker)
152 {
153 return refcount_inc_not_zero(&worker->ref);
154 }
155
io_worker_release(struct io_worker * worker)156 static void io_worker_release(struct io_worker *worker)
157 {
158 if (refcount_dec_and_test(&worker->ref))
159 complete(&worker->ref_done);
160 }
161
io_get_acct(struct io_wqe * wqe,bool bound)162 static inline struct io_wqe_acct *io_get_acct(struct io_wqe *wqe, bool bound)
163 {
164 return &wqe->acct[bound ? IO_WQ_ACCT_BOUND : IO_WQ_ACCT_UNBOUND];
165 }
166
io_work_get_acct(struct io_wqe * wqe,struct io_wq_work * work)167 static inline struct io_wqe_acct *io_work_get_acct(struct io_wqe *wqe,
168 struct io_wq_work *work)
169 {
170 return io_get_acct(wqe, !(work->flags & IO_WQ_WORK_UNBOUND));
171 }
172
io_wqe_get_acct(struct io_worker * worker)173 static inline struct io_wqe_acct *io_wqe_get_acct(struct io_worker *worker)
174 {
175 return io_get_acct(worker->wqe, worker->flags & IO_WORKER_F_BOUND);
176 }
177
io_worker_ref_put(struct io_wq * wq)178 static void io_worker_ref_put(struct io_wq *wq)
179 {
180 if (atomic_dec_and_test(&wq->worker_refs))
181 complete(&wq->worker_done);
182 }
183
io_wq_worker_stopped(void)184 bool io_wq_worker_stopped(void)
185 {
186 struct io_worker *worker = current->worker_private;
187
188 if (WARN_ON_ONCE(!io_wq_current_is_worker()))
189 return true;
190
191 return test_bit(IO_WQ_BIT_EXIT, &worker->wqe->wq->state);
192 }
193
io_worker_cancel_cb(struct io_worker * worker)194 static void io_worker_cancel_cb(struct io_worker *worker)
195 {
196 struct io_wqe_acct *acct = io_wqe_get_acct(worker);
197 struct io_wqe *wqe = worker->wqe;
198 struct io_wq *wq = wqe->wq;
199
200 atomic_dec(&acct->nr_running);
201 raw_spin_lock(&worker->wqe->lock);
202 acct->nr_workers--;
203 raw_spin_unlock(&worker->wqe->lock);
204 io_worker_ref_put(wq);
205 clear_bit_unlock(0, &worker->create_state);
206 io_worker_release(worker);
207 }
208
io_task_worker_match(struct callback_head * cb,void * data)209 static bool io_task_worker_match(struct callback_head *cb, void *data)
210 {
211 struct io_worker *worker;
212
213 if (cb->func != create_worker_cb)
214 return false;
215 worker = container_of(cb, struct io_worker, create_work);
216 return worker == data;
217 }
218
io_worker_exit(struct io_worker * worker)219 static void io_worker_exit(struct io_worker *worker)
220 {
221 struct io_wqe *wqe = worker->wqe;
222 struct io_wq *wq = wqe->wq;
223
224 while (1) {
225 struct callback_head *cb = task_work_cancel_match(wq->task,
226 io_task_worker_match, worker);
227
228 if (!cb)
229 break;
230 io_worker_cancel_cb(worker);
231 }
232
233 io_worker_release(worker);
234 wait_for_completion(&worker->ref_done);
235
236 raw_spin_lock(&wqe->lock);
237 if (worker->flags & IO_WORKER_F_FREE)
238 hlist_nulls_del_rcu(&worker->nulls_node);
239 list_del_rcu(&worker->all_list);
240 raw_spin_unlock(&wqe->lock);
241 io_wqe_dec_running(worker);
242 worker->flags = 0;
243 preempt_disable();
244 current->flags &= ~PF_IO_WORKER;
245 preempt_enable();
246
247 kfree_rcu(worker, rcu);
248 io_worker_ref_put(wqe->wq);
249 do_exit(0);
250 }
251
io_acct_run_queue(struct io_wqe_acct * acct)252 static inline bool io_acct_run_queue(struct io_wqe_acct *acct)
253 {
254 bool ret = false;
255
256 raw_spin_lock(&acct->lock);
257 if (!wq_list_empty(&acct->work_list) &&
258 !test_bit(IO_ACCT_STALLED_BIT, &acct->flags))
259 ret = true;
260 raw_spin_unlock(&acct->lock);
261
262 return ret;
263 }
264
265 /*
266 * Check head of free list for an available worker. If one isn't available,
267 * caller must create one.
268 */
io_wqe_activate_free_worker(struct io_wqe * wqe,struct io_wqe_acct * acct)269 static bool io_wqe_activate_free_worker(struct io_wqe *wqe,
270 struct io_wqe_acct *acct)
271 __must_hold(RCU)
272 {
273 struct hlist_nulls_node *n;
274 struct io_worker *worker;
275
276 /*
277 * Iterate free_list and see if we can find an idle worker to
278 * activate. If a given worker is on the free_list but in the process
279 * of exiting, keep trying.
280 */
281 hlist_nulls_for_each_entry_rcu(worker, n, &wqe->free_list, nulls_node) {
282 if (!io_worker_get(worker))
283 continue;
284 if (io_wqe_get_acct(worker) != acct) {
285 io_worker_release(worker);
286 continue;
287 }
288 if (wake_up_process(worker->task)) {
289 io_worker_release(worker);
290 return true;
291 }
292 io_worker_release(worker);
293 }
294
295 return false;
296 }
297
298 /*
299 * We need a worker. If we find a free one, we're good. If not, and we're
300 * below the max number of workers, create one.
301 */
io_wqe_create_worker(struct io_wqe * wqe,struct io_wqe_acct * acct)302 static bool io_wqe_create_worker(struct io_wqe *wqe, struct io_wqe_acct *acct)
303 {
304 /*
305 * Most likely an attempt to queue unbounded work on an io_wq that
306 * wasn't setup with any unbounded workers.
307 */
308 if (unlikely(!acct->max_workers))
309 pr_warn_once("io-wq is not configured for unbound workers");
310
311 raw_spin_lock(&wqe->lock);
312 if (acct->nr_workers >= acct->max_workers) {
313 raw_spin_unlock(&wqe->lock);
314 return true;
315 }
316 acct->nr_workers++;
317 raw_spin_unlock(&wqe->lock);
318 atomic_inc(&acct->nr_running);
319 atomic_inc(&wqe->wq->worker_refs);
320 return create_io_worker(wqe->wq, wqe, acct->index);
321 }
322
io_wqe_inc_running(struct io_worker * worker)323 static void io_wqe_inc_running(struct io_worker *worker)
324 {
325 struct io_wqe_acct *acct = io_wqe_get_acct(worker);
326
327 atomic_inc(&acct->nr_running);
328 }
329
create_worker_cb(struct callback_head * cb)330 static void create_worker_cb(struct callback_head *cb)
331 {
332 struct io_worker *worker;
333 struct io_wq *wq;
334 struct io_wqe *wqe;
335 struct io_wqe_acct *acct;
336 bool do_create = false;
337
338 worker = container_of(cb, struct io_worker, create_work);
339 wqe = worker->wqe;
340 wq = wqe->wq;
341 acct = &wqe->acct[worker->create_index];
342 raw_spin_lock(&wqe->lock);
343 if (acct->nr_workers < acct->max_workers) {
344 acct->nr_workers++;
345 do_create = true;
346 }
347 raw_spin_unlock(&wqe->lock);
348 if (do_create) {
349 create_io_worker(wq, wqe, worker->create_index);
350 } else {
351 atomic_dec(&acct->nr_running);
352 io_worker_ref_put(wq);
353 }
354 clear_bit_unlock(0, &worker->create_state);
355 io_worker_release(worker);
356 }
357
io_queue_worker_create(struct io_worker * worker,struct io_wqe_acct * acct,task_work_func_t func)358 static bool io_queue_worker_create(struct io_worker *worker,
359 struct io_wqe_acct *acct,
360 task_work_func_t func)
361 {
362 struct io_wqe *wqe = worker->wqe;
363 struct io_wq *wq = wqe->wq;
364
365 /* raced with exit, just ignore create call */
366 if (test_bit(IO_WQ_BIT_EXIT, &wq->state))
367 goto fail;
368 if (!io_worker_get(worker))
369 goto fail;
370 /*
371 * create_state manages ownership of create_work/index. We should
372 * only need one entry per worker, as the worker going to sleep
373 * will trigger the condition, and waking will clear it once it
374 * runs the task_work.
375 */
376 if (test_bit(0, &worker->create_state) ||
377 test_and_set_bit_lock(0, &worker->create_state))
378 goto fail_release;
379
380 atomic_inc(&wq->worker_refs);
381 init_task_work(&worker->create_work, func);
382 worker->create_index = acct->index;
383 if (!task_work_add(wq->task, &worker->create_work, TWA_SIGNAL)) {
384 /*
385 * EXIT may have been set after checking it above, check after
386 * adding the task_work and remove any creation item if it is
387 * now set. wq exit does that too, but we can have added this
388 * work item after we canceled in io_wq_exit_workers().
389 */
390 if (test_bit(IO_WQ_BIT_EXIT, &wq->state))
391 io_wq_cancel_tw_create(wq);
392 io_worker_ref_put(wq);
393 return true;
394 }
395 io_worker_ref_put(wq);
396 clear_bit_unlock(0, &worker->create_state);
397 fail_release:
398 io_worker_release(worker);
399 fail:
400 atomic_dec(&acct->nr_running);
401 io_worker_ref_put(wq);
402 return false;
403 }
404
io_wqe_dec_running(struct io_worker * worker)405 static void io_wqe_dec_running(struct io_worker *worker)
406 {
407 struct io_wqe_acct *acct = io_wqe_get_acct(worker);
408 struct io_wqe *wqe = worker->wqe;
409
410 if (!(worker->flags & IO_WORKER_F_UP))
411 return;
412
413 if (!atomic_dec_and_test(&acct->nr_running))
414 return;
415 if (!io_acct_run_queue(acct))
416 return;
417
418 atomic_inc(&acct->nr_running);
419 atomic_inc(&wqe->wq->worker_refs);
420 io_queue_worker_create(worker, acct, create_worker_cb);
421 }
422
423 /*
424 * Worker will start processing some work. Move it to the busy list, if
425 * it's currently on the freelist
426 */
__io_worker_busy(struct io_wqe * wqe,struct io_worker * worker)427 static void __io_worker_busy(struct io_wqe *wqe, struct io_worker *worker)
428 {
429 if (worker->flags & IO_WORKER_F_FREE) {
430 worker->flags &= ~IO_WORKER_F_FREE;
431 raw_spin_lock(&wqe->lock);
432 hlist_nulls_del_init_rcu(&worker->nulls_node);
433 raw_spin_unlock(&wqe->lock);
434 }
435 }
436
437 /*
438 * No work, worker going to sleep. Move to freelist, and unuse mm if we
439 * have one attached. Dropping the mm may potentially sleep, so we drop
440 * the lock in that case and return success. Since the caller has to
441 * retry the loop in that case (we changed task state), we don't regrab
442 * the lock if we return success.
443 */
__io_worker_idle(struct io_wqe * wqe,struct io_worker * worker)444 static void __io_worker_idle(struct io_wqe *wqe, struct io_worker *worker)
445 __must_hold(wqe->lock)
446 {
447 if (!(worker->flags & IO_WORKER_F_FREE)) {
448 worker->flags |= IO_WORKER_F_FREE;
449 hlist_nulls_add_head_rcu(&worker->nulls_node, &wqe->free_list);
450 }
451 }
452
io_get_work_hash(struct io_wq_work * work)453 static inline unsigned int io_get_work_hash(struct io_wq_work *work)
454 {
455 return work->flags >> IO_WQ_HASH_SHIFT;
456 }
457
io_wait_on_hash(struct io_wqe * wqe,unsigned int hash)458 static bool io_wait_on_hash(struct io_wqe *wqe, unsigned int hash)
459 {
460 struct io_wq *wq = wqe->wq;
461 bool ret = false;
462
463 spin_lock_irq(&wq->hash->wait.lock);
464 if (list_empty(&wqe->wait.entry)) {
465 __add_wait_queue(&wq->hash->wait, &wqe->wait);
466 if (!test_bit(hash, &wq->hash->map)) {
467 __set_current_state(TASK_RUNNING);
468 list_del_init(&wqe->wait.entry);
469 ret = true;
470 }
471 }
472 spin_unlock_irq(&wq->hash->wait.lock);
473 return ret;
474 }
475
io_get_next_work(struct io_wqe_acct * acct,struct io_worker * worker)476 static struct io_wq_work *io_get_next_work(struct io_wqe_acct *acct,
477 struct io_worker *worker)
478 __must_hold(acct->lock)
479 {
480 struct io_wq_work_node *node, *prev;
481 struct io_wq_work *work, *tail;
482 unsigned int stall_hash = -1U;
483 struct io_wqe *wqe = worker->wqe;
484
485 wq_list_for_each(node, prev, &acct->work_list) {
486 unsigned int hash;
487
488 work = container_of(node, struct io_wq_work, list);
489
490 /* not hashed, can run anytime */
491 if (!io_wq_is_hashed(work)) {
492 wq_list_del(&acct->work_list, node, prev);
493 return work;
494 }
495
496 hash = io_get_work_hash(work);
497 /* all items with this hash lie in [work, tail] */
498 tail = wqe->hash_tail[hash];
499
500 /* hashed, can run if not already running */
501 if (!test_and_set_bit(hash, &wqe->wq->hash->map)) {
502 wqe->hash_tail[hash] = NULL;
503 wq_list_cut(&acct->work_list, &tail->list, prev);
504 return work;
505 }
506 if (stall_hash == -1U)
507 stall_hash = hash;
508 /* fast forward to a next hash, for-each will fix up @prev */
509 node = &tail->list;
510 }
511
512 if (stall_hash != -1U) {
513 bool unstalled;
514
515 /*
516 * Set this before dropping the lock to avoid racing with new
517 * work being added and clearing the stalled bit.
518 */
519 set_bit(IO_ACCT_STALLED_BIT, &acct->flags);
520 raw_spin_unlock(&acct->lock);
521 unstalled = io_wait_on_hash(wqe, stall_hash);
522 raw_spin_lock(&acct->lock);
523 if (unstalled) {
524 clear_bit(IO_ACCT_STALLED_BIT, &acct->flags);
525 if (wq_has_sleeper(&wqe->wq->hash->wait))
526 wake_up(&wqe->wq->hash->wait);
527 }
528 }
529
530 return NULL;
531 }
532
io_assign_current_work(struct io_worker * worker,struct io_wq_work * work)533 static void io_assign_current_work(struct io_worker *worker,
534 struct io_wq_work *work)
535 {
536 if (work) {
537 io_run_task_work();
538 cond_resched();
539 }
540
541 raw_spin_lock(&worker->lock);
542 worker->cur_work = work;
543 worker->next_work = NULL;
544 raw_spin_unlock(&worker->lock);
545 }
546
547 static void io_wqe_enqueue(struct io_wqe *wqe, struct io_wq_work *work);
548
io_worker_handle_work(struct io_worker * worker)549 static void io_worker_handle_work(struct io_worker *worker)
550 {
551 struct io_wqe_acct *acct = io_wqe_get_acct(worker);
552 struct io_wqe *wqe = worker->wqe;
553 struct io_wq *wq = wqe->wq;
554 bool do_kill = test_bit(IO_WQ_BIT_EXIT, &wq->state);
555
556 do {
557 struct io_wq_work *work;
558
559 /*
560 * If we got some work, mark us as busy. If we didn't, but
561 * the list isn't empty, it means we stalled on hashed work.
562 * Mark us stalled so we don't keep looking for work when we
563 * can't make progress, any work completion or insertion will
564 * clear the stalled flag.
565 */
566 raw_spin_lock(&acct->lock);
567 work = io_get_next_work(acct, worker);
568 raw_spin_unlock(&acct->lock);
569 if (work) {
570 __io_worker_busy(wqe, worker);
571
572 /*
573 * Make sure cancelation can find this, even before
574 * it becomes the active work. That avoids a window
575 * where the work has been removed from our general
576 * work list, but isn't yet discoverable as the
577 * current work item for this worker.
578 */
579 raw_spin_lock(&worker->lock);
580 worker->next_work = work;
581 raw_spin_unlock(&worker->lock);
582 } else {
583 break;
584 }
585 io_assign_current_work(worker, work);
586 __set_current_state(TASK_RUNNING);
587
588 /* handle a whole dependent link */
589 do {
590 struct io_wq_work *next_hashed, *linked;
591 unsigned int hash = io_get_work_hash(work);
592
593 next_hashed = wq_next_work(work);
594
595 if (unlikely(do_kill) && (work->flags & IO_WQ_WORK_UNBOUND))
596 work->flags |= IO_WQ_WORK_CANCEL;
597 wq->do_work(work);
598 io_assign_current_work(worker, NULL);
599
600 linked = wq->free_work(work);
601 work = next_hashed;
602 if (!work && linked && !io_wq_is_hashed(linked)) {
603 work = linked;
604 linked = NULL;
605 }
606 io_assign_current_work(worker, work);
607 if (linked)
608 io_wqe_enqueue(wqe, linked);
609
610 if (hash != -1U && !next_hashed) {
611 /* serialize hash clear with wake_up() */
612 spin_lock_irq(&wq->hash->wait.lock);
613 clear_bit(hash, &wq->hash->map);
614 clear_bit(IO_ACCT_STALLED_BIT, &acct->flags);
615 spin_unlock_irq(&wq->hash->wait.lock);
616 if (wq_has_sleeper(&wq->hash->wait))
617 wake_up(&wq->hash->wait);
618 }
619 } while (work);
620 } while (1);
621 }
622
io_wqe_worker(void * data)623 static int io_wqe_worker(void *data)
624 {
625 struct io_worker *worker = data;
626 struct io_wqe_acct *acct = io_wqe_get_acct(worker);
627 struct io_wqe *wqe = worker->wqe;
628 struct io_wq *wq = wqe->wq;
629 bool last_timeout = false;
630 char buf[TASK_COMM_LEN];
631
632 worker->flags |= (IO_WORKER_F_UP | IO_WORKER_F_RUNNING);
633
634 snprintf(buf, sizeof(buf), "iou-wrk-%d", wq->task->pid);
635 set_task_comm(current, buf);
636
637 while (!test_bit(IO_WQ_BIT_EXIT, &wq->state)) {
638 long ret;
639
640 set_current_state(TASK_INTERRUPTIBLE);
641 while (io_acct_run_queue(acct))
642 io_worker_handle_work(worker);
643
644 raw_spin_lock(&wqe->lock);
645 /* timed out, exit unless we're the last worker */
646 if (last_timeout && acct->nr_workers > 1) {
647 acct->nr_workers--;
648 raw_spin_unlock(&wqe->lock);
649 __set_current_state(TASK_RUNNING);
650 break;
651 }
652 last_timeout = false;
653 __io_worker_idle(wqe, worker);
654 raw_spin_unlock(&wqe->lock);
655 if (io_run_task_work())
656 continue;
657 ret = schedule_timeout(WORKER_IDLE_TIMEOUT);
658 if (signal_pending(current)) {
659 struct ksignal ksig;
660
661 if (!get_signal(&ksig))
662 continue;
663 break;
664 }
665 last_timeout = !ret;
666 }
667
668 if (test_bit(IO_WQ_BIT_EXIT, &wq->state))
669 io_worker_handle_work(worker);
670
671 io_worker_exit(worker);
672 return 0;
673 }
674
675 /*
676 * Called when a worker is scheduled in. Mark us as currently running.
677 */
io_wq_worker_running(struct task_struct * tsk)678 void io_wq_worker_running(struct task_struct *tsk)
679 {
680 struct io_worker *worker = tsk->worker_private;
681
682 if (!worker)
683 return;
684 if (!(worker->flags & IO_WORKER_F_UP))
685 return;
686 if (worker->flags & IO_WORKER_F_RUNNING)
687 return;
688 worker->flags |= IO_WORKER_F_RUNNING;
689 io_wqe_inc_running(worker);
690 }
691
692 /*
693 * Called when worker is going to sleep. If there are no workers currently
694 * running and we have work pending, wake up a free one or create a new one.
695 */
io_wq_worker_sleeping(struct task_struct * tsk)696 void io_wq_worker_sleeping(struct task_struct *tsk)
697 {
698 struct io_worker *worker = tsk->worker_private;
699
700 if (!worker)
701 return;
702 if (!(worker->flags & IO_WORKER_F_UP))
703 return;
704 if (!(worker->flags & IO_WORKER_F_RUNNING))
705 return;
706
707 worker->flags &= ~IO_WORKER_F_RUNNING;
708 io_wqe_dec_running(worker);
709 }
710
io_init_new_worker(struct io_wqe * wqe,struct io_worker * worker,struct task_struct * tsk)711 static void io_init_new_worker(struct io_wqe *wqe, struct io_worker *worker,
712 struct task_struct *tsk)
713 {
714 tsk->worker_private = worker;
715 worker->task = tsk;
716 set_cpus_allowed_ptr(tsk, wqe->cpu_mask);
717 tsk->flags |= PF_NO_SETAFFINITY;
718
719 raw_spin_lock(&wqe->lock);
720 hlist_nulls_add_head_rcu(&worker->nulls_node, &wqe->free_list);
721 list_add_tail_rcu(&worker->all_list, &wqe->all_list);
722 worker->flags |= IO_WORKER_F_FREE;
723 raw_spin_unlock(&wqe->lock);
724 wake_up_new_task(tsk);
725 }
726
io_wq_work_match_all(struct io_wq_work * work,void * data)727 static bool io_wq_work_match_all(struct io_wq_work *work, void *data)
728 {
729 return true;
730 }
731
io_should_retry_thread(long err)732 static inline bool io_should_retry_thread(long err)
733 {
734 /*
735 * Prevent perpetual task_work retry, if the task (or its group) is
736 * exiting.
737 */
738 if (fatal_signal_pending(current))
739 return false;
740
741 switch (err) {
742 case -EAGAIN:
743 case -ERESTARTSYS:
744 case -ERESTARTNOINTR:
745 case -ERESTARTNOHAND:
746 return true;
747 default:
748 return false;
749 }
750 }
751
create_worker_cont(struct callback_head * cb)752 static void create_worker_cont(struct callback_head *cb)
753 {
754 struct io_worker *worker;
755 struct task_struct *tsk;
756 struct io_wqe *wqe;
757
758 worker = container_of(cb, struct io_worker, create_work);
759 clear_bit_unlock(0, &worker->create_state);
760 wqe = worker->wqe;
761 tsk = create_io_thread(io_wqe_worker, worker, wqe->node);
762 if (!IS_ERR(tsk)) {
763 io_init_new_worker(wqe, worker, tsk);
764 io_worker_release(worker);
765 return;
766 } else if (!io_should_retry_thread(PTR_ERR(tsk))) {
767 struct io_wqe_acct *acct = io_wqe_get_acct(worker);
768
769 atomic_dec(&acct->nr_running);
770 raw_spin_lock(&wqe->lock);
771 acct->nr_workers--;
772 if (!acct->nr_workers) {
773 struct io_cb_cancel_data match = {
774 .fn = io_wq_work_match_all,
775 .cancel_all = true,
776 };
777
778 raw_spin_unlock(&wqe->lock);
779 while (io_acct_cancel_pending_work(wqe, acct, &match))
780 ;
781 } else {
782 raw_spin_unlock(&wqe->lock);
783 }
784 io_worker_ref_put(wqe->wq);
785 kfree(worker);
786 return;
787 }
788
789 /* re-create attempts grab a new worker ref, drop the existing one */
790 io_worker_release(worker);
791 schedule_work(&worker->work);
792 }
793
io_workqueue_create(struct work_struct * work)794 static void io_workqueue_create(struct work_struct *work)
795 {
796 struct io_worker *worker = container_of(work, struct io_worker, work);
797 struct io_wqe_acct *acct = io_wqe_get_acct(worker);
798
799 if (!io_queue_worker_create(worker, acct, create_worker_cont))
800 kfree(worker);
801 }
802
create_io_worker(struct io_wq * wq,struct io_wqe * wqe,int index)803 static bool create_io_worker(struct io_wq *wq, struct io_wqe *wqe, int index)
804 {
805 struct io_wqe_acct *acct = &wqe->acct[index];
806 struct io_worker *worker;
807 struct task_struct *tsk;
808
809 __set_current_state(TASK_RUNNING);
810
811 worker = kzalloc_node(sizeof(*worker), GFP_KERNEL, wqe->node);
812 if (!worker) {
813 fail:
814 atomic_dec(&acct->nr_running);
815 raw_spin_lock(&wqe->lock);
816 acct->nr_workers--;
817 raw_spin_unlock(&wqe->lock);
818 io_worker_ref_put(wq);
819 return false;
820 }
821
822 refcount_set(&worker->ref, 1);
823 worker->wqe = wqe;
824 raw_spin_lock_init(&worker->lock);
825 init_completion(&worker->ref_done);
826
827 if (index == IO_WQ_ACCT_BOUND)
828 worker->flags |= IO_WORKER_F_BOUND;
829
830 tsk = create_io_thread(io_wqe_worker, worker, wqe->node);
831 if (!IS_ERR(tsk)) {
832 io_init_new_worker(wqe, worker, tsk);
833 } else if (!io_should_retry_thread(PTR_ERR(tsk))) {
834 kfree(worker);
835 goto fail;
836 } else {
837 INIT_WORK(&worker->work, io_workqueue_create);
838 schedule_work(&worker->work);
839 }
840
841 return true;
842 }
843
844 /*
845 * Iterate the passed in list and call the specific function for each
846 * worker that isn't exiting
847 */
io_wq_for_each_worker(struct io_wqe * wqe,bool (* func)(struct io_worker *,void *),void * data)848 static bool io_wq_for_each_worker(struct io_wqe *wqe,
849 bool (*func)(struct io_worker *, void *),
850 void *data)
851 {
852 struct io_worker *worker;
853 bool ret = false;
854
855 list_for_each_entry_rcu(worker, &wqe->all_list, all_list) {
856 if (io_worker_get(worker)) {
857 /* no task if node is/was offline */
858 if (worker->task)
859 ret = func(worker, data);
860 io_worker_release(worker);
861 if (ret)
862 break;
863 }
864 }
865
866 return ret;
867 }
868
io_wq_worker_wake(struct io_worker * worker,void * data)869 static bool io_wq_worker_wake(struct io_worker *worker, void *data)
870 {
871 __set_notify_signal(worker->task);
872 wake_up_process(worker->task);
873 return false;
874 }
875
io_run_cancel(struct io_wq_work * work,struct io_wqe * wqe)876 static void io_run_cancel(struct io_wq_work *work, struct io_wqe *wqe)
877 {
878 struct io_wq *wq = wqe->wq;
879
880 do {
881 work->flags |= IO_WQ_WORK_CANCEL;
882 wq->do_work(work);
883 work = wq->free_work(work);
884 } while (work);
885 }
886
io_wqe_insert_work(struct io_wqe * wqe,struct io_wq_work * work)887 static void io_wqe_insert_work(struct io_wqe *wqe, struct io_wq_work *work)
888 {
889 struct io_wqe_acct *acct = io_work_get_acct(wqe, work);
890 unsigned int hash;
891 struct io_wq_work *tail;
892
893 if (!io_wq_is_hashed(work)) {
894 append:
895 wq_list_add_tail(&work->list, &acct->work_list);
896 return;
897 }
898
899 hash = io_get_work_hash(work);
900 tail = wqe->hash_tail[hash];
901 wqe->hash_tail[hash] = work;
902 if (!tail)
903 goto append;
904
905 wq_list_add_after(&work->list, &tail->list, &acct->work_list);
906 }
907
io_wq_work_match_item(struct io_wq_work * work,void * data)908 static bool io_wq_work_match_item(struct io_wq_work *work, void *data)
909 {
910 return work == data;
911 }
912
io_wqe_enqueue(struct io_wqe * wqe,struct io_wq_work * work)913 static void io_wqe_enqueue(struct io_wqe *wqe, struct io_wq_work *work)
914 {
915 struct io_wqe_acct *acct = io_work_get_acct(wqe, work);
916 struct io_cb_cancel_data match;
917 unsigned work_flags = work->flags;
918 bool do_create;
919
920 /*
921 * If io-wq is exiting for this task, or if the request has explicitly
922 * been marked as one that should not get executed, cancel it here.
923 */
924 if (test_bit(IO_WQ_BIT_EXIT, &wqe->wq->state) ||
925 (work->flags & IO_WQ_WORK_CANCEL)) {
926 io_run_cancel(work, wqe);
927 return;
928 }
929
930 raw_spin_lock(&acct->lock);
931 io_wqe_insert_work(wqe, work);
932 clear_bit(IO_ACCT_STALLED_BIT, &acct->flags);
933 raw_spin_unlock(&acct->lock);
934
935 raw_spin_lock(&wqe->lock);
936 rcu_read_lock();
937 do_create = !io_wqe_activate_free_worker(wqe, acct);
938 rcu_read_unlock();
939
940 raw_spin_unlock(&wqe->lock);
941
942 if (do_create && ((work_flags & IO_WQ_WORK_CONCURRENT) ||
943 !atomic_read(&acct->nr_running))) {
944 bool did_create;
945
946 did_create = io_wqe_create_worker(wqe, acct);
947 if (likely(did_create))
948 return;
949
950 raw_spin_lock(&wqe->lock);
951 if (acct->nr_workers) {
952 raw_spin_unlock(&wqe->lock);
953 return;
954 }
955 raw_spin_unlock(&wqe->lock);
956
957 /* fatal condition, failed to create the first worker */
958 match.fn = io_wq_work_match_item,
959 match.data = work,
960 match.cancel_all = false,
961
962 io_acct_cancel_pending_work(wqe, acct, &match);
963 }
964 }
965
io_wq_enqueue(struct io_wq * wq,struct io_wq_work * work)966 void io_wq_enqueue(struct io_wq *wq, struct io_wq_work *work)
967 {
968 struct io_wqe *wqe = wq->wqes[numa_node_id()];
969
970 io_wqe_enqueue(wqe, work);
971 }
972
973 /*
974 * Work items that hash to the same value will not be done in parallel.
975 * Used to limit concurrent writes, generally hashed by inode.
976 */
io_wq_hash_work(struct io_wq_work * work,void * val)977 void io_wq_hash_work(struct io_wq_work *work, void *val)
978 {
979 unsigned int bit;
980
981 bit = hash_ptr(val, IO_WQ_HASH_ORDER);
982 work->flags |= (IO_WQ_WORK_HASHED | (bit << IO_WQ_HASH_SHIFT));
983 }
984
__io_wq_worker_cancel(struct io_worker * worker,struct io_cb_cancel_data * match,struct io_wq_work * work)985 static bool __io_wq_worker_cancel(struct io_worker *worker,
986 struct io_cb_cancel_data *match,
987 struct io_wq_work *work)
988 {
989 if (work && match->fn(work, match->data)) {
990 work->flags |= IO_WQ_WORK_CANCEL;
991 __set_notify_signal(worker->task);
992 return true;
993 }
994
995 return false;
996 }
997
io_wq_worker_cancel(struct io_worker * worker,void * data)998 static bool io_wq_worker_cancel(struct io_worker *worker, void *data)
999 {
1000 struct io_cb_cancel_data *match = data;
1001
1002 /*
1003 * Hold the lock to avoid ->cur_work going out of scope, caller
1004 * may dereference the passed in work.
1005 */
1006 raw_spin_lock(&worker->lock);
1007 if (__io_wq_worker_cancel(worker, match, worker->cur_work) ||
1008 __io_wq_worker_cancel(worker, match, worker->next_work))
1009 match->nr_running++;
1010 raw_spin_unlock(&worker->lock);
1011
1012 return match->nr_running && !match->cancel_all;
1013 }
1014
io_wqe_remove_pending(struct io_wqe * wqe,struct io_wq_work * work,struct io_wq_work_node * prev)1015 static inline void io_wqe_remove_pending(struct io_wqe *wqe,
1016 struct io_wq_work *work,
1017 struct io_wq_work_node *prev)
1018 {
1019 struct io_wqe_acct *acct = io_work_get_acct(wqe, work);
1020 unsigned int hash = io_get_work_hash(work);
1021 struct io_wq_work *prev_work = NULL;
1022
1023 if (io_wq_is_hashed(work) && work == wqe->hash_tail[hash]) {
1024 if (prev)
1025 prev_work = container_of(prev, struct io_wq_work, list);
1026 if (prev_work && io_get_work_hash(prev_work) == hash)
1027 wqe->hash_tail[hash] = prev_work;
1028 else
1029 wqe->hash_tail[hash] = NULL;
1030 }
1031 wq_list_del(&acct->work_list, &work->list, prev);
1032 }
1033
io_acct_cancel_pending_work(struct io_wqe * wqe,struct io_wqe_acct * acct,struct io_cb_cancel_data * match)1034 static bool io_acct_cancel_pending_work(struct io_wqe *wqe,
1035 struct io_wqe_acct *acct,
1036 struct io_cb_cancel_data *match)
1037 {
1038 struct io_wq_work_node *node, *prev;
1039 struct io_wq_work *work;
1040
1041 raw_spin_lock(&acct->lock);
1042 wq_list_for_each(node, prev, &acct->work_list) {
1043 work = container_of(node, struct io_wq_work, list);
1044 if (!match->fn(work, match->data))
1045 continue;
1046 io_wqe_remove_pending(wqe, work, prev);
1047 raw_spin_unlock(&acct->lock);
1048 io_run_cancel(work, wqe);
1049 match->nr_pending++;
1050 /* not safe to continue after unlock */
1051 return true;
1052 }
1053 raw_spin_unlock(&acct->lock);
1054
1055 return false;
1056 }
1057
io_wqe_cancel_pending_work(struct io_wqe * wqe,struct io_cb_cancel_data * match)1058 static void io_wqe_cancel_pending_work(struct io_wqe *wqe,
1059 struct io_cb_cancel_data *match)
1060 {
1061 int i;
1062 retry:
1063 for (i = 0; i < IO_WQ_ACCT_NR; i++) {
1064 struct io_wqe_acct *acct = io_get_acct(wqe, i == 0);
1065
1066 if (io_acct_cancel_pending_work(wqe, acct, match)) {
1067 if (match->cancel_all)
1068 goto retry;
1069 break;
1070 }
1071 }
1072 }
1073
io_wqe_cancel_running_work(struct io_wqe * wqe,struct io_cb_cancel_data * match)1074 static void io_wqe_cancel_running_work(struct io_wqe *wqe,
1075 struct io_cb_cancel_data *match)
1076 {
1077 rcu_read_lock();
1078 io_wq_for_each_worker(wqe, io_wq_worker_cancel, match);
1079 rcu_read_unlock();
1080 }
1081
io_wq_cancel_cb(struct io_wq * wq,work_cancel_fn * cancel,void * data,bool cancel_all)1082 enum io_wq_cancel io_wq_cancel_cb(struct io_wq *wq, work_cancel_fn *cancel,
1083 void *data, bool cancel_all)
1084 {
1085 struct io_cb_cancel_data match = {
1086 .fn = cancel,
1087 .data = data,
1088 .cancel_all = cancel_all,
1089 };
1090 int node;
1091
1092 /*
1093 * First check pending list, if we're lucky we can just remove it
1094 * from there. CANCEL_OK means that the work is returned as-new,
1095 * no completion will be posted for it.
1096 *
1097 * Then check if a free (going busy) or busy worker has the work
1098 * currently running. If we find it there, we'll return CANCEL_RUNNING
1099 * as an indication that we attempt to signal cancellation. The
1100 * completion will run normally in this case.
1101 *
1102 * Do both of these while holding the wqe->lock, to ensure that
1103 * we'll find a work item regardless of state.
1104 */
1105 for_each_node(node) {
1106 struct io_wqe *wqe = wq->wqes[node];
1107
1108 io_wqe_cancel_pending_work(wqe, &match);
1109 if (match.nr_pending && !match.cancel_all)
1110 return IO_WQ_CANCEL_OK;
1111
1112 raw_spin_lock(&wqe->lock);
1113 io_wqe_cancel_running_work(wqe, &match);
1114 raw_spin_unlock(&wqe->lock);
1115 if (match.nr_running && !match.cancel_all)
1116 return IO_WQ_CANCEL_RUNNING;
1117 }
1118
1119 if (match.nr_running)
1120 return IO_WQ_CANCEL_RUNNING;
1121 if (match.nr_pending)
1122 return IO_WQ_CANCEL_OK;
1123 return IO_WQ_CANCEL_NOTFOUND;
1124 }
1125
io_wqe_hash_wake(struct wait_queue_entry * wait,unsigned mode,int sync,void * key)1126 static int io_wqe_hash_wake(struct wait_queue_entry *wait, unsigned mode,
1127 int sync, void *key)
1128 {
1129 struct io_wqe *wqe = container_of(wait, struct io_wqe, wait);
1130 int i;
1131
1132 list_del_init(&wait->entry);
1133
1134 rcu_read_lock();
1135 for (i = 0; i < IO_WQ_ACCT_NR; i++) {
1136 struct io_wqe_acct *acct = &wqe->acct[i];
1137
1138 if (test_and_clear_bit(IO_ACCT_STALLED_BIT, &acct->flags))
1139 io_wqe_activate_free_worker(wqe, acct);
1140 }
1141 rcu_read_unlock();
1142 return 1;
1143 }
1144
io_wq_create(unsigned bounded,struct io_wq_data * data)1145 struct io_wq *io_wq_create(unsigned bounded, struct io_wq_data *data)
1146 {
1147 int ret, node, i;
1148 struct io_wq *wq;
1149
1150 if (WARN_ON_ONCE(!data->free_work || !data->do_work))
1151 return ERR_PTR(-EINVAL);
1152 if (WARN_ON_ONCE(!bounded))
1153 return ERR_PTR(-EINVAL);
1154
1155 wq = kzalloc(struct_size(wq, wqes, nr_node_ids), GFP_KERNEL);
1156 if (!wq)
1157 return ERR_PTR(-ENOMEM);
1158 ret = cpuhp_state_add_instance_nocalls(io_wq_online, &wq->cpuhp_node);
1159 if (ret)
1160 goto err_wq;
1161
1162 refcount_inc(&data->hash->refs);
1163 wq->hash = data->hash;
1164 wq->free_work = data->free_work;
1165 wq->do_work = data->do_work;
1166
1167 ret = -ENOMEM;
1168 for_each_node(node) {
1169 struct io_wqe *wqe;
1170 int alloc_node = node;
1171
1172 if (!node_online(alloc_node))
1173 alloc_node = NUMA_NO_NODE;
1174 wqe = kzalloc_node(sizeof(struct io_wqe), GFP_KERNEL, alloc_node);
1175 if (!wqe)
1176 goto err;
1177 wq->wqes[node] = wqe;
1178 if (!alloc_cpumask_var(&wqe->cpu_mask, GFP_KERNEL))
1179 goto err;
1180 cpumask_copy(wqe->cpu_mask, cpumask_of_node(node));
1181 wqe->node = alloc_node;
1182 wqe->acct[IO_WQ_ACCT_BOUND].max_workers = bounded;
1183 wqe->acct[IO_WQ_ACCT_UNBOUND].max_workers =
1184 task_rlimit(current, RLIMIT_NPROC);
1185 INIT_LIST_HEAD(&wqe->wait.entry);
1186 wqe->wait.func = io_wqe_hash_wake;
1187 for (i = 0; i < IO_WQ_ACCT_NR; i++) {
1188 struct io_wqe_acct *acct = &wqe->acct[i];
1189
1190 acct->index = i;
1191 atomic_set(&acct->nr_running, 0);
1192 INIT_WQ_LIST(&acct->work_list);
1193 raw_spin_lock_init(&acct->lock);
1194 }
1195 wqe->wq = wq;
1196 raw_spin_lock_init(&wqe->lock);
1197 INIT_HLIST_NULLS_HEAD(&wqe->free_list, 0);
1198 INIT_LIST_HEAD(&wqe->all_list);
1199 }
1200
1201 wq->task = get_task_struct(data->task);
1202 atomic_set(&wq->worker_refs, 1);
1203 init_completion(&wq->worker_done);
1204 return wq;
1205 err:
1206 io_wq_put_hash(data->hash);
1207 cpuhp_state_remove_instance_nocalls(io_wq_online, &wq->cpuhp_node);
1208 for_each_node(node) {
1209 if (!wq->wqes[node])
1210 continue;
1211 free_cpumask_var(wq->wqes[node]->cpu_mask);
1212 kfree(wq->wqes[node]);
1213 }
1214 err_wq:
1215 kfree(wq);
1216 return ERR_PTR(ret);
1217 }
1218
io_task_work_match(struct callback_head * cb,void * data)1219 static bool io_task_work_match(struct callback_head *cb, void *data)
1220 {
1221 struct io_worker *worker;
1222
1223 if (cb->func != create_worker_cb && cb->func != create_worker_cont)
1224 return false;
1225 worker = container_of(cb, struct io_worker, create_work);
1226 return worker->wqe->wq == data;
1227 }
1228
io_wq_exit_start(struct io_wq * wq)1229 void io_wq_exit_start(struct io_wq *wq)
1230 {
1231 set_bit(IO_WQ_BIT_EXIT, &wq->state);
1232 }
1233
io_wq_cancel_tw_create(struct io_wq * wq)1234 static void io_wq_cancel_tw_create(struct io_wq *wq)
1235 {
1236 struct callback_head *cb;
1237
1238 while ((cb = task_work_cancel_match(wq->task, io_task_work_match, wq)) != NULL) {
1239 struct io_worker *worker;
1240
1241 worker = container_of(cb, struct io_worker, create_work);
1242 io_worker_cancel_cb(worker);
1243 /*
1244 * Only the worker continuation helper has worker allocated and
1245 * hence needs freeing.
1246 */
1247 if (cb->func == create_worker_cont)
1248 kfree(worker);
1249 }
1250 }
1251
io_wq_exit_workers(struct io_wq * wq)1252 static void io_wq_exit_workers(struct io_wq *wq)
1253 {
1254 int node;
1255
1256 if (!wq->task)
1257 return;
1258
1259 io_wq_cancel_tw_create(wq);
1260
1261 rcu_read_lock();
1262 for_each_node(node) {
1263 struct io_wqe *wqe = wq->wqes[node];
1264
1265 io_wq_for_each_worker(wqe, io_wq_worker_wake, NULL);
1266 }
1267 rcu_read_unlock();
1268 io_worker_ref_put(wq);
1269 wait_for_completion(&wq->worker_done);
1270
1271 for_each_node(node) {
1272 spin_lock_irq(&wq->hash->wait.lock);
1273 list_del_init(&wq->wqes[node]->wait.entry);
1274 spin_unlock_irq(&wq->hash->wait.lock);
1275 }
1276 put_task_struct(wq->task);
1277 wq->task = NULL;
1278 }
1279
io_wq_destroy(struct io_wq * wq)1280 static void io_wq_destroy(struct io_wq *wq)
1281 {
1282 int node;
1283
1284 cpuhp_state_remove_instance_nocalls(io_wq_online, &wq->cpuhp_node);
1285
1286 for_each_node(node) {
1287 struct io_wqe *wqe = wq->wqes[node];
1288 struct io_cb_cancel_data match = {
1289 .fn = io_wq_work_match_all,
1290 .cancel_all = true,
1291 };
1292 io_wqe_cancel_pending_work(wqe, &match);
1293 free_cpumask_var(wqe->cpu_mask);
1294 kfree(wqe);
1295 }
1296 io_wq_put_hash(wq->hash);
1297 kfree(wq);
1298 }
1299
io_wq_put_and_exit(struct io_wq * wq)1300 void io_wq_put_and_exit(struct io_wq *wq)
1301 {
1302 WARN_ON_ONCE(!test_bit(IO_WQ_BIT_EXIT, &wq->state));
1303
1304 io_wq_exit_workers(wq);
1305 io_wq_destroy(wq);
1306 }
1307
1308 struct online_data {
1309 unsigned int cpu;
1310 bool online;
1311 };
1312
io_wq_worker_affinity(struct io_worker * worker,void * data)1313 static bool io_wq_worker_affinity(struct io_worker *worker, void *data)
1314 {
1315 struct online_data *od = data;
1316
1317 if (od->online)
1318 cpumask_set_cpu(od->cpu, worker->wqe->cpu_mask);
1319 else
1320 cpumask_clear_cpu(od->cpu, worker->wqe->cpu_mask);
1321 return false;
1322 }
1323
__io_wq_cpu_online(struct io_wq * wq,unsigned int cpu,bool online)1324 static int __io_wq_cpu_online(struct io_wq *wq, unsigned int cpu, bool online)
1325 {
1326 struct online_data od = {
1327 .cpu = cpu,
1328 .online = online
1329 };
1330 int i;
1331
1332 rcu_read_lock();
1333 for_each_node(i)
1334 io_wq_for_each_worker(wq->wqes[i], io_wq_worker_affinity, &od);
1335 rcu_read_unlock();
1336 return 0;
1337 }
1338
io_wq_cpu_online(unsigned int cpu,struct hlist_node * node)1339 static int io_wq_cpu_online(unsigned int cpu, struct hlist_node *node)
1340 {
1341 struct io_wq *wq = hlist_entry_safe(node, struct io_wq, cpuhp_node);
1342
1343 return __io_wq_cpu_online(wq, cpu, true);
1344 }
1345
io_wq_cpu_offline(unsigned int cpu,struct hlist_node * node)1346 static int io_wq_cpu_offline(unsigned int cpu, struct hlist_node *node)
1347 {
1348 struct io_wq *wq = hlist_entry_safe(node, struct io_wq, cpuhp_node);
1349
1350 return __io_wq_cpu_online(wq, cpu, false);
1351 }
1352
io_wq_cpu_affinity(struct io_uring_task * tctx,cpumask_var_t mask)1353 int io_wq_cpu_affinity(struct io_uring_task *tctx, cpumask_var_t mask)
1354 {
1355 int i;
1356
1357 if (!tctx || !tctx->io_wq)
1358 return -EINVAL;
1359
1360 rcu_read_lock();
1361 for_each_node(i) {
1362 struct io_wqe *wqe = tctx->io_wq->wqes[i];
1363
1364 if (mask)
1365 cpumask_copy(wqe->cpu_mask, mask);
1366 else
1367 cpumask_copy(wqe->cpu_mask, cpumask_of_node(i));
1368 }
1369 rcu_read_unlock();
1370 return 0;
1371 }
1372
1373 /*
1374 * Set max number of unbounded workers, returns old value. If new_count is 0,
1375 * then just return the old value.
1376 */
io_wq_max_workers(struct io_wq * wq,int * new_count)1377 int io_wq_max_workers(struct io_wq *wq, int *new_count)
1378 {
1379 int prev[IO_WQ_ACCT_NR];
1380 bool first_node = true;
1381 int i, node;
1382
1383 BUILD_BUG_ON((int) IO_WQ_ACCT_BOUND != (int) IO_WQ_BOUND);
1384 BUILD_BUG_ON((int) IO_WQ_ACCT_UNBOUND != (int) IO_WQ_UNBOUND);
1385 BUILD_BUG_ON((int) IO_WQ_ACCT_NR != 2);
1386
1387 for (i = 0; i < IO_WQ_ACCT_NR; i++) {
1388 if (new_count[i] > task_rlimit(current, RLIMIT_NPROC))
1389 new_count[i] = task_rlimit(current, RLIMIT_NPROC);
1390 }
1391
1392 for (i = 0; i < IO_WQ_ACCT_NR; i++)
1393 prev[i] = 0;
1394
1395 rcu_read_lock();
1396 for_each_node(node) {
1397 struct io_wqe *wqe = wq->wqes[node];
1398 struct io_wqe_acct *acct;
1399
1400 raw_spin_lock(&wqe->lock);
1401 for (i = 0; i < IO_WQ_ACCT_NR; i++) {
1402 acct = &wqe->acct[i];
1403 if (first_node)
1404 prev[i] = max_t(int, acct->max_workers, prev[i]);
1405 if (new_count[i])
1406 acct->max_workers = new_count[i];
1407 }
1408 raw_spin_unlock(&wqe->lock);
1409 first_node = false;
1410 }
1411 rcu_read_unlock();
1412
1413 for (i = 0; i < IO_WQ_ACCT_NR; i++)
1414 new_count[i] = prev[i];
1415
1416 return 0;
1417 }
1418
io_wq_init(void)1419 static __init int io_wq_init(void)
1420 {
1421 int ret;
1422
1423 ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "io-wq/online",
1424 io_wq_cpu_online, io_wq_cpu_offline);
1425 if (ret < 0)
1426 return ret;
1427 io_wq_online = ret;
1428 return 0;
1429 }
1430 subsys_initcall(io_wq_init);
1431