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