1 // SPDX-License-Identifier: GPL-2.0
3 * Basic worker thread pool for io_uring
5 * Copyright (C) 2019 Jens Axboe
8 #include <linux/kernel.h>
9 #include <linux/init.h>
10 #include <linux/errno.h>
11 #include <linux/sched/signal.h>
13 #include <linux/sched/mm.h>
14 #include <linux/percpu.h>
15 #include <linux/slab.h>
16 #include <linux/rculist_nulls.h>
17 #include <linux/cpu.h>
18 #include <linux/tracehook.h>
20 #include "../kernel/sched/sched.h"
23 #define WORKER_IDLE_TIMEOUT (5 * HZ)
26 IO_WORKER_F_UP = 1, /* up and active */
27 IO_WORKER_F_RUNNING = 2, /* account as running */
28 IO_WORKER_F_FREE = 4, /* worker on free list */
29 IO_WORKER_F_FIXED = 8, /* static idle worker */
30 IO_WORKER_F_BOUND = 16, /* is doing bounded work */
34 IO_WQ_BIT_EXIT = 0, /* wq exiting */
35 IO_WQ_BIT_ERROR = 1, /* error on setup */
39 IO_WQE_FLAG_STALLED = 1, /* stalled on hash */
43 * One for each thread in a wqe pool
48 struct hlist_nulls_node nulls_node;
49 struct list_head all_list;
50 struct task_struct *task;
53 struct io_wq_work *cur_work;
56 const struct cred *cur_creds;
57 const struct cred *saved_creds;
59 struct completion ref_done;
64 #if BITS_PER_LONG == 64
65 #define IO_WQ_HASH_ORDER 6
67 #define IO_WQ_HASH_ORDER 5
70 #define IO_WQ_NR_HASH_BUCKETS (1u << IO_WQ_HASH_ORDER)
84 * Per-node worker thread pool
89 struct io_wq_work_list work_list;
90 unsigned long hash_map;
92 } ____cacheline_aligned_in_smp;
95 struct io_wqe_acct acct[2];
97 struct hlist_nulls_head free_list;
98 struct list_head all_list;
101 struct io_wq_work *hash_tail[IO_WQ_NR_HASH_BUCKETS];
108 struct io_wqe **wqes;
111 free_work_fn *free_work;
112 io_wq_work_fn *do_work;
114 struct task_struct *manager;
115 struct user_struct *user;
117 struct completion done;
119 struct hlist_node cpuhp_node;
124 static enum cpuhp_state io_wq_online;
126 static bool io_worker_get(struct io_worker *worker)
128 return refcount_inc_not_zero(&worker->ref);
131 static void io_worker_release(struct io_worker *worker)
133 if (refcount_dec_and_test(&worker->ref))
134 complete(&worker->ref_done);
137 static inline struct io_wqe_acct *io_work_get_acct(struct io_wqe *wqe,
138 struct io_wq_work *work)
140 if (work->flags & IO_WQ_WORK_UNBOUND)
141 return &wqe->acct[IO_WQ_ACCT_UNBOUND];
143 return &wqe->acct[IO_WQ_ACCT_BOUND];
146 static inline struct io_wqe_acct *io_wqe_get_acct(struct io_worker *worker)
148 struct io_wqe *wqe = worker->wqe;
150 if (worker->flags & IO_WORKER_F_BOUND)
151 return &wqe->acct[IO_WQ_ACCT_BOUND];
153 return &wqe->acct[IO_WQ_ACCT_UNBOUND];
156 static void io_worker_exit(struct io_worker *worker)
158 struct io_wqe *wqe = worker->wqe;
159 struct io_wqe_acct *acct = io_wqe_get_acct(worker);
162 if (refcount_dec_and_test(&worker->ref))
163 complete(&worker->ref_done);
164 wait_for_completion(&worker->ref_done);
167 current->flags &= ~PF_IO_WORKER;
168 flags = worker->flags;
170 if (flags & IO_WORKER_F_RUNNING)
171 atomic_dec(&acct->nr_running);
175 if (worker->saved_creds) {
176 revert_creds(worker->saved_creds);
177 worker->cur_creds = worker->saved_creds = NULL;
180 raw_spin_lock_irq(&wqe->lock);
181 if (flags & IO_WORKER_F_FREE)
182 hlist_nulls_del_rcu(&worker->nulls_node);
183 list_del_rcu(&worker->all_list);
185 raw_spin_unlock_irq(&wqe->lock);
187 kfree_rcu(worker, rcu);
188 if (refcount_dec_and_test(&wqe->wq->refs))
189 complete(&wqe->wq->done);
192 static inline bool io_wqe_run_queue(struct io_wqe *wqe)
193 __must_hold(wqe->lock)
195 if (!wq_list_empty(&wqe->work_list) &&
196 !(wqe->flags & IO_WQE_FLAG_STALLED))
202 * Check head of free list for an available worker. If one isn't available,
203 * caller must wake up the wq manager to create one.
205 static bool io_wqe_activate_free_worker(struct io_wqe *wqe)
208 struct hlist_nulls_node *n;
209 struct io_worker *worker;
211 n = rcu_dereference(hlist_nulls_first_rcu(&wqe->free_list));
215 worker = hlist_nulls_entry(n, struct io_worker, nulls_node);
216 if (io_worker_get(worker)) {
217 wake_up_process(worker->task);
218 io_worker_release(worker);
226 * We need a worker. If we find a free one, we're good. If not, and we're
227 * below the max number of workers, wake up the manager to create one.
229 static void io_wqe_wake_worker(struct io_wqe *wqe, struct io_wqe_acct *acct)
234 * Most likely an attempt to queue unbounded work on an io_wq that
235 * wasn't setup with any unbounded workers.
237 WARN_ON_ONCE(!acct->max_workers);
240 ret = io_wqe_activate_free_worker(wqe);
243 if (!ret && acct->nr_workers < acct->max_workers)
244 wake_up_process(wqe->wq->manager);
247 static void io_wqe_inc_running(struct io_worker *worker)
249 struct io_wqe_acct *acct = io_wqe_get_acct(worker);
251 atomic_inc(&acct->nr_running);
254 static void io_wqe_dec_running(struct io_worker *worker)
255 __must_hold(wqe->lock)
257 struct io_wqe_acct *acct = io_wqe_get_acct(worker);
258 struct io_wqe *wqe = worker->wqe;
260 if (atomic_dec_and_test(&acct->nr_running) && io_wqe_run_queue(wqe))
261 io_wqe_wake_worker(wqe, acct);
264 static void io_worker_start(struct io_worker *worker)
266 worker->flags |= (IO_WORKER_F_UP | IO_WORKER_F_RUNNING);
267 io_wqe_inc_running(worker);
271 * Worker will start processing some work. Move it to the busy list, if
272 * it's currently on the freelist
274 static void __io_worker_busy(struct io_wqe *wqe, struct io_worker *worker,
275 struct io_wq_work *work)
276 __must_hold(wqe->lock)
278 bool worker_bound, work_bound;
280 if (worker->flags & IO_WORKER_F_FREE) {
281 worker->flags &= ~IO_WORKER_F_FREE;
282 hlist_nulls_del_init_rcu(&worker->nulls_node);
286 * If worker is moving from bound to unbound (or vice versa), then
287 * ensure we update the running accounting.
289 worker_bound = (worker->flags & IO_WORKER_F_BOUND) != 0;
290 work_bound = (work->flags & IO_WQ_WORK_UNBOUND) == 0;
291 if (worker_bound != work_bound) {
292 io_wqe_dec_running(worker);
294 worker->flags |= IO_WORKER_F_BOUND;
295 wqe->acct[IO_WQ_ACCT_UNBOUND].nr_workers--;
296 wqe->acct[IO_WQ_ACCT_BOUND].nr_workers++;
298 worker->flags &= ~IO_WORKER_F_BOUND;
299 wqe->acct[IO_WQ_ACCT_UNBOUND].nr_workers++;
300 wqe->acct[IO_WQ_ACCT_BOUND].nr_workers--;
302 io_wqe_inc_running(worker);
307 * No work, worker going to sleep. Move to freelist, and unuse mm if we
308 * have one attached. Dropping the mm may potentially sleep, so we drop
309 * the lock in that case and return success. Since the caller has to
310 * retry the loop in that case (we changed task state), we don't regrab
311 * the lock if we return success.
313 static void __io_worker_idle(struct io_wqe *wqe, struct io_worker *worker)
314 __must_hold(wqe->lock)
316 if (!(worker->flags & IO_WORKER_F_FREE)) {
317 worker->flags |= IO_WORKER_F_FREE;
318 hlist_nulls_add_head_rcu(&worker->nulls_node, &wqe->free_list);
320 if (worker->saved_creds) {
321 revert_creds(worker->saved_creds);
322 worker->cur_creds = worker->saved_creds = NULL;
326 static inline unsigned int io_get_work_hash(struct io_wq_work *work)
328 return work->flags >> IO_WQ_HASH_SHIFT;
331 static struct io_wq_work *io_get_next_work(struct io_wqe *wqe)
332 __must_hold(wqe->lock)
334 struct io_wq_work_node *node, *prev;
335 struct io_wq_work *work, *tail;
338 wq_list_for_each(node, prev, &wqe->work_list) {
339 work = container_of(node, struct io_wq_work, list);
341 /* not hashed, can run anytime */
342 if (!io_wq_is_hashed(work)) {
343 wq_list_del(&wqe->work_list, node, prev);
347 /* hashed, can run if not already running */
348 hash = io_get_work_hash(work);
349 if (!(wqe->hash_map & BIT(hash))) {
350 wqe->hash_map |= BIT(hash);
351 /* all items with this hash lie in [work, tail] */
352 tail = wqe->hash_tail[hash];
353 wqe->hash_tail[hash] = NULL;
354 wq_list_cut(&wqe->work_list, &tail->list, prev);
362 static void io_flush_signals(void)
364 if (unlikely(test_tsk_thread_flag(current, TIF_NOTIFY_SIGNAL))) {
365 if (current->task_works)
367 clear_tsk_thread_flag(current, TIF_NOTIFY_SIGNAL);
371 static void io_wq_switch_creds(struct io_worker *worker,
372 struct io_wq_work *work)
374 const struct cred *old_creds = override_creds(work->creds);
376 worker->cur_creds = work->creds;
377 if (worker->saved_creds)
378 put_cred(old_creds); /* creds set by previous switch */
380 worker->saved_creds = old_creds;
383 static void io_assign_current_work(struct io_worker *worker,
384 struct io_wq_work *work)
391 spin_lock_irq(&worker->lock);
392 worker->cur_work = work;
393 spin_unlock_irq(&worker->lock);
396 static void io_wqe_enqueue(struct io_wqe *wqe, struct io_wq_work *work);
398 static void io_worker_handle_work(struct io_worker *worker)
399 __releases(wqe->lock)
401 struct io_wqe *wqe = worker->wqe;
402 struct io_wq *wq = wqe->wq;
405 struct io_wq_work *work;
408 * If we got some work, mark us as busy. If we didn't, but
409 * the list isn't empty, it means we stalled on hashed work.
410 * Mark us stalled so we don't keep looking for work when we
411 * can't make progress, any work completion or insertion will
412 * clear the stalled flag.
414 work = io_get_next_work(wqe);
416 __io_worker_busy(wqe, worker, work);
417 else if (!wq_list_empty(&wqe->work_list))
418 wqe->flags |= IO_WQE_FLAG_STALLED;
420 raw_spin_unlock_irq(&wqe->lock);
423 io_assign_current_work(worker, work);
425 /* handle a whole dependent link */
427 struct io_wq_work *next_hashed, *linked;
428 unsigned int hash = io_get_work_hash(work);
430 next_hashed = wq_next_work(work);
431 if (work->creds && worker->cur_creds != work->creds)
432 io_wq_switch_creds(worker, work);
434 io_assign_current_work(worker, NULL);
436 linked = wq->free_work(work);
438 if (!work && linked && !io_wq_is_hashed(linked)) {
442 io_assign_current_work(worker, work);
444 io_wqe_enqueue(wqe, linked);
446 if (hash != -1U && !next_hashed) {
447 raw_spin_lock_irq(&wqe->lock);
448 wqe->hash_map &= ~BIT_ULL(hash);
449 wqe->flags &= ~IO_WQE_FLAG_STALLED;
450 /* skip unnecessary unlock-lock wqe->lock */
453 raw_spin_unlock_irq(&wqe->lock);
457 raw_spin_lock_irq(&wqe->lock);
461 static int io_wqe_worker(void *data)
463 struct io_worker *worker = data;
464 struct io_wqe *wqe = worker->wqe;
465 struct io_wq *wq = wqe->wq;
467 io_worker_start(worker);
469 while (!test_bit(IO_WQ_BIT_EXIT, &wq->state)) {
470 set_current_state(TASK_INTERRUPTIBLE);
472 raw_spin_lock_irq(&wqe->lock);
473 if (io_wqe_run_queue(wqe)) {
474 __set_current_state(TASK_RUNNING);
475 io_worker_handle_work(worker);
478 __io_worker_idle(wqe, worker);
479 raw_spin_unlock_irq(&wqe->lock);
481 if (schedule_timeout(WORKER_IDLE_TIMEOUT))
483 if (fatal_signal_pending(current))
485 /* timed out, exit unless we're the fixed worker */
486 if (test_bit(IO_WQ_BIT_EXIT, &wq->state) ||
487 !(worker->flags & IO_WORKER_F_FIXED))
491 if (test_bit(IO_WQ_BIT_EXIT, &wq->state)) {
492 raw_spin_lock_irq(&wqe->lock);
493 if (!wq_list_empty(&wqe->work_list))
494 io_worker_handle_work(worker);
496 raw_spin_unlock_irq(&wqe->lock);
499 io_worker_exit(worker);
504 * Called when a worker is scheduled in. Mark us as currently running.
506 void io_wq_worker_running(struct task_struct *tsk)
508 struct io_worker *worker = tsk->pf_io_worker;
512 if (!(worker->flags & IO_WORKER_F_UP))
514 if (worker->flags & IO_WORKER_F_RUNNING)
516 worker->flags |= IO_WORKER_F_RUNNING;
517 io_wqe_inc_running(worker);
521 * Called when worker is going to sleep. If there are no workers currently
522 * running and we have work pending, wake up a free one or have the manager
525 void io_wq_worker_sleeping(struct task_struct *tsk)
527 struct io_worker *worker = tsk->pf_io_worker;
531 if (!(worker->flags & IO_WORKER_F_UP))
533 if (!(worker->flags & IO_WORKER_F_RUNNING))
536 worker->flags &= ~IO_WORKER_F_RUNNING;
538 raw_spin_lock_irq(&worker->wqe->lock);
539 io_wqe_dec_running(worker);
540 raw_spin_unlock_irq(&worker->wqe->lock);
543 static int task_thread(void *data, int index)
545 struct io_worker *worker = data;
546 struct io_wqe *wqe = worker->wqe;
547 struct io_wqe_acct *acct = &wqe->acct[index];
548 struct io_wq *wq = wqe->wq;
549 char buf[TASK_COMM_LEN];
551 sprintf(buf, "iou-wrk-%d", wq->task_pid);
552 set_task_comm(current, buf);
554 current->pf_io_worker = worker;
555 worker->task = current;
557 set_cpus_allowed_ptr(current, cpumask_of_node(wqe->node));
558 current->flags |= PF_NO_SETAFFINITY;
560 raw_spin_lock_irq(&wqe->lock);
561 hlist_nulls_add_head_rcu(&worker->nulls_node, &wqe->free_list);
562 list_add_tail_rcu(&worker->all_list, &wqe->all_list);
563 worker->flags |= IO_WORKER_F_FREE;
564 if (index == IO_WQ_ACCT_BOUND)
565 worker->flags |= IO_WORKER_F_BOUND;
566 if (!acct->nr_workers && (worker->flags & IO_WORKER_F_BOUND))
567 worker->flags |= IO_WORKER_F_FIXED;
569 raw_spin_unlock_irq(&wqe->lock);
575 static int task_thread_bound(void *data)
577 return task_thread(data, IO_WQ_ACCT_BOUND);
580 static int task_thread_unbound(void *data)
582 return task_thread(data, IO_WQ_ACCT_UNBOUND);
585 pid_t io_wq_fork_thread(int (*fn)(void *), void *arg)
587 unsigned long flags = CLONE_FS|CLONE_FILES|CLONE_SIGHAND|CLONE_THREAD|
589 struct kernel_clone_args args = {
590 .flags = ((lower_32_bits(flags) | CLONE_VM |
591 CLONE_UNTRACED) & ~CSIGNAL),
592 .exit_signal = (lower_32_bits(flags) & CSIGNAL),
593 .stack = (unsigned long)fn,
594 .stack_size = (unsigned long)arg,
597 return kernel_clone(&args);
600 static bool create_io_worker(struct io_wq *wq, struct io_wqe *wqe, int index)
602 struct io_worker *worker;
605 __set_current_state(TASK_RUNNING);
607 worker = kzalloc_node(sizeof(*worker), GFP_KERNEL, wqe->node);
611 refcount_set(&worker->ref, 1);
612 worker->nulls_node.pprev = NULL;
614 spin_lock_init(&worker->lock);
615 init_completion(&worker->ref_done);
617 refcount_inc(&wq->refs);
619 if (index == IO_WQ_ACCT_BOUND)
620 pid = io_wq_fork_thread(task_thread_bound, worker);
622 pid = io_wq_fork_thread(task_thread_unbound, worker);
624 if (refcount_dec_and_test(&wq->refs))
632 static inline bool io_wqe_need_worker(struct io_wqe *wqe, int index)
633 __must_hold(wqe->lock)
635 struct io_wqe_acct *acct = &wqe->acct[index];
637 /* if we have available workers or no work, no need */
638 if (!hlist_nulls_empty(&wqe->free_list) || !io_wqe_run_queue(wqe))
640 return acct->nr_workers < acct->max_workers;
644 * Iterate the passed in list and call the specific function for each
645 * worker that isn't exiting
647 static bool io_wq_for_each_worker(struct io_wqe *wqe,
648 bool (*func)(struct io_worker *, void *),
651 struct io_worker *worker;
654 list_for_each_entry_rcu(worker, &wqe->all_list, all_list) {
655 if (io_worker_get(worker)) {
656 /* no task if node is/was offline */
658 ret = func(worker, data);
659 io_worker_release(worker);
668 static bool io_wq_worker_wake(struct io_worker *worker, void *data)
670 wake_up_process(worker->task);
674 static void io_wq_check_workers(struct io_wq *wq)
678 for_each_node(node) {
679 struct io_wqe *wqe = wq->wqes[node];
680 bool fork_worker[2] = { false, false };
682 if (!node_online(node))
685 raw_spin_lock_irq(&wqe->lock);
686 if (io_wqe_need_worker(wqe, IO_WQ_ACCT_BOUND))
687 fork_worker[IO_WQ_ACCT_BOUND] = true;
688 if (io_wqe_need_worker(wqe, IO_WQ_ACCT_UNBOUND))
689 fork_worker[IO_WQ_ACCT_UNBOUND] = true;
690 raw_spin_unlock_irq(&wqe->lock);
691 if (fork_worker[IO_WQ_ACCT_BOUND])
692 create_io_worker(wq, wqe, IO_WQ_ACCT_BOUND);
693 if (fork_worker[IO_WQ_ACCT_UNBOUND])
694 create_io_worker(wq, wqe, IO_WQ_ACCT_UNBOUND);
699 * Manager thread. Tasked with creating new workers, if we need them.
701 static int io_wq_manager(void *data)
703 struct io_wq *wq = data;
704 char buf[TASK_COMM_LEN];
707 sprintf(buf, "iou-mgr-%d", wq->task_pid);
708 set_task_comm(current, buf);
709 current->flags |= PF_IO_WORKER;
710 wq->manager = current;
715 set_current_state(TASK_INTERRUPTIBLE);
716 io_wq_check_workers(wq);
717 schedule_timeout(HZ);
718 if (fatal_signal_pending(current))
719 set_bit(IO_WQ_BIT_EXIT, &wq->state);
720 } while (!test_bit(IO_WQ_BIT_EXIT, &wq->state));
722 io_wq_check_workers(wq);
724 if (refcount_dec_and_test(&wq->refs)) {
729 /* if ERROR is set and we get here, we have workers to wake */
730 if (test_bit(IO_WQ_BIT_ERROR, &wq->state)) {
733 io_wq_for_each_worker(wq->wqes[node], io_wq_worker_wake, NULL);
740 static void io_run_cancel(struct io_wq_work *work, struct io_wqe *wqe)
742 struct io_wq *wq = wqe->wq;
745 work->flags |= IO_WQ_WORK_CANCEL;
747 work = wq->free_work(work);
751 static void io_wqe_insert_work(struct io_wqe *wqe, struct io_wq_work *work)
754 struct io_wq_work *tail;
756 if (!io_wq_is_hashed(work)) {
758 wq_list_add_tail(&work->list, &wqe->work_list);
762 hash = io_get_work_hash(work);
763 tail = wqe->hash_tail[hash];
764 wqe->hash_tail[hash] = work;
768 wq_list_add_after(&work->list, &tail->list, &wqe->work_list);
771 static void io_wqe_enqueue(struct io_wqe *wqe, struct io_wq_work *work)
773 struct io_wqe_acct *acct = io_work_get_acct(wqe, work);
777 work_flags = work->flags;
778 raw_spin_lock_irqsave(&wqe->lock, flags);
779 io_wqe_insert_work(wqe, work);
780 wqe->flags &= ~IO_WQE_FLAG_STALLED;
781 raw_spin_unlock_irqrestore(&wqe->lock, flags);
783 if ((work_flags & IO_WQ_WORK_CONCURRENT) ||
784 !atomic_read(&acct->nr_running))
785 io_wqe_wake_worker(wqe, acct);
788 void io_wq_enqueue(struct io_wq *wq, struct io_wq_work *work)
790 struct io_wqe *wqe = wq->wqes[numa_node_id()];
792 io_wqe_enqueue(wqe, work);
796 * Work items that hash to the same value will not be done in parallel.
797 * Used to limit concurrent writes, generally hashed by inode.
799 void io_wq_hash_work(struct io_wq_work *work, void *val)
803 bit = hash_ptr(val, IO_WQ_HASH_ORDER);
804 work->flags |= (IO_WQ_WORK_HASHED | (bit << IO_WQ_HASH_SHIFT));
807 struct io_cb_cancel_data {
815 static bool io_wq_worker_cancel(struct io_worker *worker, void *data)
817 struct io_cb_cancel_data *match = data;
821 * Hold the lock to avoid ->cur_work going out of scope, caller
822 * may dereference the passed in work.
824 spin_lock_irqsave(&worker->lock, flags);
825 if (worker->cur_work &&
826 match->fn(worker->cur_work, match->data)) {
827 set_notify_signal(worker->task);
830 spin_unlock_irqrestore(&worker->lock, flags);
832 return match->nr_running && !match->cancel_all;
835 static inline void io_wqe_remove_pending(struct io_wqe *wqe,
836 struct io_wq_work *work,
837 struct io_wq_work_node *prev)
839 unsigned int hash = io_get_work_hash(work);
840 struct io_wq_work *prev_work = NULL;
842 if (io_wq_is_hashed(work) && work == wqe->hash_tail[hash]) {
844 prev_work = container_of(prev, struct io_wq_work, list);
845 if (prev_work && io_get_work_hash(prev_work) == hash)
846 wqe->hash_tail[hash] = prev_work;
848 wqe->hash_tail[hash] = NULL;
850 wq_list_del(&wqe->work_list, &work->list, prev);
853 static void io_wqe_cancel_pending_work(struct io_wqe *wqe,
854 struct io_cb_cancel_data *match)
856 struct io_wq_work_node *node, *prev;
857 struct io_wq_work *work;
861 raw_spin_lock_irqsave(&wqe->lock, flags);
862 wq_list_for_each(node, prev, &wqe->work_list) {
863 work = container_of(node, struct io_wq_work, list);
864 if (!match->fn(work, match->data))
866 io_wqe_remove_pending(wqe, work, prev);
867 raw_spin_unlock_irqrestore(&wqe->lock, flags);
868 io_run_cancel(work, wqe);
870 if (!match->cancel_all)
873 /* not safe to continue after unlock */
876 raw_spin_unlock_irqrestore(&wqe->lock, flags);
879 static void io_wqe_cancel_running_work(struct io_wqe *wqe,
880 struct io_cb_cancel_data *match)
883 io_wq_for_each_worker(wqe, io_wq_worker_cancel, match);
887 enum io_wq_cancel io_wq_cancel_cb(struct io_wq *wq, work_cancel_fn *cancel,
888 void *data, bool cancel_all)
890 struct io_cb_cancel_data match = {
893 .cancel_all = cancel_all,
898 * First check pending list, if we're lucky we can just remove it
899 * from there. CANCEL_OK means that the work is returned as-new,
900 * no completion will be posted for it.
902 for_each_node(node) {
903 struct io_wqe *wqe = wq->wqes[node];
905 io_wqe_cancel_pending_work(wqe, &match);
906 if (match.nr_pending && !match.cancel_all)
907 return IO_WQ_CANCEL_OK;
911 * Now check if a free (going busy) or busy worker has the work
912 * currently running. If we find it there, we'll return CANCEL_RUNNING
913 * as an indication that we attempt to signal cancellation. The
914 * completion will run normally in this case.
916 for_each_node(node) {
917 struct io_wqe *wqe = wq->wqes[node];
919 io_wqe_cancel_running_work(wqe, &match);
920 if (match.nr_running && !match.cancel_all)
921 return IO_WQ_CANCEL_RUNNING;
924 if (match.nr_running)
925 return IO_WQ_CANCEL_RUNNING;
926 if (match.nr_pending)
927 return IO_WQ_CANCEL_OK;
928 return IO_WQ_CANCEL_NOTFOUND;
931 struct io_wq *io_wq_create(unsigned bounded, struct io_wq_data *data)
933 int ret = -ENOMEM, node;
936 if (WARN_ON_ONCE(!data->free_work || !data->do_work))
937 return ERR_PTR(-EINVAL);
939 wq = kzalloc(sizeof(*wq), GFP_KERNEL);
941 return ERR_PTR(-ENOMEM);
943 wq->wqes = kcalloc(nr_node_ids, sizeof(struct io_wqe *), GFP_KERNEL);
947 ret = cpuhp_state_add_instance_nocalls(io_wq_online, &wq->cpuhp_node);
951 wq->free_work = data->free_work;
952 wq->do_work = data->do_work;
955 for_each_node(node) {
957 int alloc_node = node;
959 if (!node_online(alloc_node))
960 alloc_node = NUMA_NO_NODE;
961 wqe = kzalloc_node(sizeof(struct io_wqe), GFP_KERNEL, alloc_node);
964 wq->wqes[node] = wqe;
965 wqe->node = alloc_node;
966 wqe->acct[IO_WQ_ACCT_BOUND].max_workers = bounded;
967 atomic_set(&wqe->acct[IO_WQ_ACCT_BOUND].nr_running, 0);
968 wqe->acct[IO_WQ_ACCT_UNBOUND].max_workers =
969 task_rlimit(current, RLIMIT_NPROC);
970 atomic_set(&wqe->acct[IO_WQ_ACCT_UNBOUND].nr_running, 0);
972 raw_spin_lock_init(&wqe->lock);
973 INIT_WQ_LIST(&wqe->work_list);
974 INIT_HLIST_NULLS_HEAD(&wqe->free_list, 0);
975 INIT_LIST_HEAD(&wqe->all_list);
978 wq->task_pid = current->pid;
979 init_completion(&wq->done);
980 refcount_set(&wq->refs, 1);
982 current->flags |= PF_IO_WORKER;
983 ret = io_wq_fork_thread(io_wq_manager, wq);
984 current->flags &= ~PF_IO_WORKER;
986 wait_for_completion(&wq->done);
990 if (refcount_dec_and_test(&wq->refs))
993 cpuhp_state_remove_instance_nocalls(io_wq_online, &wq->cpuhp_node);
995 kfree(wq->wqes[node]);
1000 return ERR_PTR(ret);
1003 void io_wq_destroy(struct io_wq *wq)
1007 cpuhp_state_remove_instance_nocalls(io_wq_online, &wq->cpuhp_node);
1009 set_bit(IO_WQ_BIT_EXIT, &wq->state);
1011 wake_up_process(wq->manager);
1015 io_wq_for_each_worker(wq->wqes[node], io_wq_worker_wake, NULL);
1018 wait_for_completion(&wq->done);
1021 kfree(wq->wqes[node]);
1026 static bool io_wq_worker_affinity(struct io_worker *worker, void *data)
1028 struct task_struct *task = worker->task;
1032 rq = task_rq_lock(task, &rf);
1033 do_set_cpus_allowed(task, cpumask_of_node(worker->wqe->node));
1034 task->flags |= PF_NO_SETAFFINITY;
1035 task_rq_unlock(rq, task, &rf);
1039 static int io_wq_cpu_online(unsigned int cpu, struct hlist_node *node)
1041 struct io_wq *wq = hlist_entry_safe(node, struct io_wq, cpuhp_node);
1046 io_wq_for_each_worker(wq->wqes[i], io_wq_worker_affinity, NULL);
1051 static __init int io_wq_init(void)
1055 ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "io-wq/online",
1056 io_wq_cpu_online, NULL);
1062 subsys_initcall(io_wq_init);