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 */
38 IO_WQE_FLAG_STALLED = 1, /* stalled on hash */
42 * One for each thread in a wqe pool
47 struct hlist_nulls_node nulls_node;
48 struct list_head all_list;
49 struct task_struct *task;
52 struct io_wq_work *cur_work;
55 struct completion ref_done;
56 struct completion started;
61 #if BITS_PER_LONG == 64
62 #define IO_WQ_HASH_ORDER 6
64 #define IO_WQ_HASH_ORDER 5
67 #define IO_WQ_NR_HASH_BUCKETS (1u << IO_WQ_HASH_ORDER)
81 * Per-node worker thread pool
86 struct io_wq_work_list work_list;
88 } ____cacheline_aligned_in_smp;
91 struct io_wqe_acct acct[2];
93 struct hlist_nulls_head free_list;
94 struct list_head all_list;
96 struct wait_queue_entry wait;
99 struct io_wq_work *hash_tail[IO_WQ_NR_HASH_BUCKETS];
106 struct io_wqe **wqes;
109 free_work_fn *free_work;
110 io_wq_work_fn *do_work;
112 struct task_struct *manager;
113 struct user_struct *user;
115 struct io_wq_hash *hash;
118 struct completion started;
119 struct completion exited;
121 atomic_t worker_refs;
122 struct completion worker_done;
124 struct hlist_node cpuhp_node;
129 static enum cpuhp_state io_wq_online;
131 static bool io_worker_get(struct io_worker *worker)
133 return refcount_inc_not_zero(&worker->ref);
136 static void io_worker_release(struct io_worker *worker)
138 if (refcount_dec_and_test(&worker->ref))
139 complete(&worker->ref_done);
142 static inline struct io_wqe_acct *io_work_get_acct(struct io_wqe *wqe,
143 struct io_wq_work *work)
145 if (work->flags & IO_WQ_WORK_UNBOUND)
146 return &wqe->acct[IO_WQ_ACCT_UNBOUND];
148 return &wqe->acct[IO_WQ_ACCT_BOUND];
151 static inline struct io_wqe_acct *io_wqe_get_acct(struct io_worker *worker)
153 struct io_wqe *wqe = worker->wqe;
155 if (worker->flags & IO_WORKER_F_BOUND)
156 return &wqe->acct[IO_WQ_ACCT_BOUND];
158 return &wqe->acct[IO_WQ_ACCT_UNBOUND];
161 static void io_worker_exit(struct io_worker *worker)
163 struct io_wqe *wqe = worker->wqe;
164 struct io_wqe_acct *acct = io_wqe_get_acct(worker);
167 if (refcount_dec_and_test(&worker->ref))
168 complete(&worker->ref_done);
169 wait_for_completion(&worker->ref_done);
172 current->flags &= ~PF_IO_WORKER;
173 flags = worker->flags;
175 if (flags & IO_WORKER_F_RUNNING)
176 atomic_dec(&acct->nr_running);
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 (atomic_dec_and_test(&wqe->wq->worker_refs))
189 complete(&wqe->wq->worker_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);
268 complete(&worker->started);
272 * Worker will start processing some work. Move it to the busy list, if
273 * it's currently on the freelist
275 static void __io_worker_busy(struct io_wqe *wqe, struct io_worker *worker,
276 struct io_wq_work *work)
277 __must_hold(wqe->lock)
279 bool worker_bound, work_bound;
281 if (worker->flags & IO_WORKER_F_FREE) {
282 worker->flags &= ~IO_WORKER_F_FREE;
283 hlist_nulls_del_init_rcu(&worker->nulls_node);
287 * If worker is moving from bound to unbound (or vice versa), then
288 * ensure we update the running accounting.
290 worker_bound = (worker->flags & IO_WORKER_F_BOUND) != 0;
291 work_bound = (work->flags & IO_WQ_WORK_UNBOUND) == 0;
292 if (worker_bound != work_bound) {
293 io_wqe_dec_running(worker);
295 worker->flags |= IO_WORKER_F_BOUND;
296 wqe->acct[IO_WQ_ACCT_UNBOUND].nr_workers--;
297 wqe->acct[IO_WQ_ACCT_BOUND].nr_workers++;
299 worker->flags &= ~IO_WORKER_F_BOUND;
300 wqe->acct[IO_WQ_ACCT_UNBOUND].nr_workers++;
301 wqe->acct[IO_WQ_ACCT_BOUND].nr_workers--;
303 io_wqe_inc_running(worker);
308 * No work, worker going to sleep. Move to freelist, and unuse mm if we
309 * have one attached. Dropping the mm may potentially sleep, so we drop
310 * the lock in that case and return success. Since the caller has to
311 * retry the loop in that case (we changed task state), we don't regrab
312 * the lock if we return success.
314 static void __io_worker_idle(struct io_wqe *wqe, struct io_worker *worker)
315 __must_hold(wqe->lock)
317 if (!(worker->flags & IO_WORKER_F_FREE)) {
318 worker->flags |= IO_WORKER_F_FREE;
319 hlist_nulls_add_head_rcu(&worker->nulls_node, &wqe->free_list);
323 static inline unsigned int io_get_work_hash(struct io_wq_work *work)
325 return work->flags >> IO_WQ_HASH_SHIFT;
328 static void io_wait_on_hash(struct io_wqe *wqe, unsigned int hash)
330 struct io_wq *wq = wqe->wq;
332 spin_lock(&wq->hash->wait.lock);
333 if (list_empty(&wqe->wait.entry)) {
334 __add_wait_queue(&wq->hash->wait, &wqe->wait);
335 if (!test_bit(hash, &wq->hash->map)) {
336 __set_current_state(TASK_RUNNING);
337 list_del_init(&wqe->wait.entry);
340 spin_unlock(&wq->hash->wait.lock);
343 static struct io_wq_work *io_get_next_work(struct io_wqe *wqe)
344 __must_hold(wqe->lock)
346 struct io_wq_work_node *node, *prev;
347 struct io_wq_work *work, *tail;
348 unsigned int stall_hash = -1U;
350 wq_list_for_each(node, prev, &wqe->work_list) {
353 work = container_of(node, struct io_wq_work, list);
355 /* not hashed, can run anytime */
356 if (!io_wq_is_hashed(work)) {
357 wq_list_del(&wqe->work_list, node, prev);
361 hash = io_get_work_hash(work);
362 /* all items with this hash lie in [work, tail] */
363 tail = wqe->hash_tail[hash];
365 /* hashed, can run if not already running */
366 if (!test_and_set_bit(hash, &wqe->wq->hash->map)) {
367 wqe->hash_tail[hash] = NULL;
368 wq_list_cut(&wqe->work_list, &tail->list, prev);
371 if (stall_hash == -1U)
373 /* fast forward to a next hash, for-each will fix up @prev */
377 if (stall_hash != -1U) {
378 raw_spin_unlock(&wqe->lock);
379 io_wait_on_hash(wqe, stall_hash);
380 raw_spin_lock(&wqe->lock);
386 static void io_flush_signals(void)
388 if (unlikely(test_tsk_thread_flag(current, TIF_NOTIFY_SIGNAL))) {
389 if (current->task_works)
391 clear_tsk_thread_flag(current, TIF_NOTIFY_SIGNAL);
395 static void io_assign_current_work(struct io_worker *worker,
396 struct io_wq_work *work)
403 spin_lock_irq(&worker->lock);
404 worker->cur_work = work;
405 spin_unlock_irq(&worker->lock);
408 static void io_wqe_enqueue(struct io_wqe *wqe, struct io_wq_work *work);
410 static void io_worker_handle_work(struct io_worker *worker)
411 __releases(wqe->lock)
413 struct io_wqe *wqe = worker->wqe;
414 struct io_wq *wq = wqe->wq;
417 struct io_wq_work *work;
420 * If we got some work, mark us as busy. If we didn't, but
421 * the list isn't empty, it means we stalled on hashed work.
422 * Mark us stalled so we don't keep looking for work when we
423 * can't make progress, any work completion or insertion will
424 * clear the stalled flag.
426 work = io_get_next_work(wqe);
428 __io_worker_busy(wqe, worker, work);
429 else if (!wq_list_empty(&wqe->work_list))
430 wqe->flags |= IO_WQE_FLAG_STALLED;
432 raw_spin_unlock_irq(&wqe->lock);
435 io_assign_current_work(worker, work);
436 __set_current_state(TASK_RUNNING);
438 /* handle a whole dependent link */
440 struct io_wq_work *next_hashed, *linked;
441 unsigned int hash = io_get_work_hash(work);
443 next_hashed = wq_next_work(work);
445 io_assign_current_work(worker, NULL);
447 linked = wq->free_work(work);
449 if (!work && linked && !io_wq_is_hashed(linked)) {
453 io_assign_current_work(worker, work);
455 io_wqe_enqueue(wqe, linked);
457 if (hash != -1U && !next_hashed) {
458 clear_bit(hash, &wq->hash->map);
459 if (wq_has_sleeper(&wq->hash->wait))
460 wake_up(&wq->hash->wait);
461 raw_spin_lock_irq(&wqe->lock);
462 wqe->flags &= ~IO_WQE_FLAG_STALLED;
463 /* skip unnecessary unlock-lock wqe->lock */
466 raw_spin_unlock_irq(&wqe->lock);
470 raw_spin_lock_irq(&wqe->lock);
474 static int io_wqe_worker(void *data)
476 struct io_worker *worker = data;
477 struct io_wqe *wqe = worker->wqe;
478 struct io_wq *wq = wqe->wq;
480 io_worker_start(worker);
482 while (!test_bit(IO_WQ_BIT_EXIT, &wq->state)) {
483 set_current_state(TASK_INTERRUPTIBLE);
485 raw_spin_lock_irq(&wqe->lock);
486 if (io_wqe_run_queue(wqe)) {
487 io_worker_handle_work(worker);
490 __io_worker_idle(wqe, worker);
491 raw_spin_unlock_irq(&wqe->lock);
493 if (schedule_timeout(WORKER_IDLE_TIMEOUT))
495 if (fatal_signal_pending(current))
497 /* timed out, exit unless we're the fixed worker */
498 if (test_bit(IO_WQ_BIT_EXIT, &wq->state) ||
499 !(worker->flags & IO_WORKER_F_FIXED))
503 if (test_bit(IO_WQ_BIT_EXIT, &wq->state)) {
504 raw_spin_lock_irq(&wqe->lock);
505 if (!wq_list_empty(&wqe->work_list))
506 io_worker_handle_work(worker);
508 raw_spin_unlock_irq(&wqe->lock);
511 io_worker_exit(worker);
516 * Called when a worker is scheduled in. Mark us as currently running.
518 void io_wq_worker_running(struct task_struct *tsk)
520 struct io_worker *worker = tsk->pf_io_worker;
524 if (!(worker->flags & IO_WORKER_F_UP))
526 if (worker->flags & IO_WORKER_F_RUNNING)
528 worker->flags |= IO_WORKER_F_RUNNING;
529 io_wqe_inc_running(worker);
533 * Called when worker is going to sleep. If there are no workers currently
534 * running and we have work pending, wake up a free one or have the manager
537 void io_wq_worker_sleeping(struct task_struct *tsk)
539 struct io_worker *worker = tsk->pf_io_worker;
543 if (!(worker->flags & IO_WORKER_F_UP))
545 if (!(worker->flags & IO_WORKER_F_RUNNING))
548 worker->flags &= ~IO_WORKER_F_RUNNING;
550 raw_spin_lock_irq(&worker->wqe->lock);
551 io_wqe_dec_running(worker);
552 raw_spin_unlock_irq(&worker->wqe->lock);
555 static int task_thread(void *data, int index)
557 struct io_worker *worker = data;
558 struct io_wqe *wqe = worker->wqe;
559 struct io_wqe_acct *acct = &wqe->acct[index];
560 struct io_wq *wq = wqe->wq;
561 char buf[TASK_COMM_LEN];
563 sprintf(buf, "iou-wrk-%d", wq->task_pid);
564 set_task_comm(current, buf);
566 current->pf_io_worker = worker;
567 worker->task = current;
569 set_cpus_allowed_ptr(current, cpumask_of_node(wqe->node));
570 current->flags |= PF_NO_SETAFFINITY;
572 raw_spin_lock_irq(&wqe->lock);
573 hlist_nulls_add_head_rcu(&worker->nulls_node, &wqe->free_list);
574 list_add_tail_rcu(&worker->all_list, &wqe->all_list);
575 worker->flags |= IO_WORKER_F_FREE;
576 if (index == IO_WQ_ACCT_BOUND)
577 worker->flags |= IO_WORKER_F_BOUND;
578 if (!acct->nr_workers && (worker->flags & IO_WORKER_F_BOUND))
579 worker->flags |= IO_WORKER_F_FIXED;
581 raw_spin_unlock_irq(&wqe->lock);
587 static int task_thread_bound(void *data)
589 return task_thread(data, IO_WQ_ACCT_BOUND);
592 static int task_thread_unbound(void *data)
594 return task_thread(data, IO_WQ_ACCT_UNBOUND);
597 pid_t io_wq_fork_thread(int (*fn)(void *), void *arg)
599 unsigned long flags = CLONE_FS|CLONE_FILES|CLONE_SIGHAND|CLONE_THREAD|
601 struct kernel_clone_args args = {
602 .flags = ((lower_32_bits(flags) | CLONE_VM |
603 CLONE_UNTRACED) & ~CSIGNAL),
604 .exit_signal = (lower_32_bits(flags) & CSIGNAL),
605 .stack = (unsigned long)fn,
606 .stack_size = (unsigned long)arg,
609 return kernel_clone(&args);
612 static bool create_io_worker(struct io_wq *wq, struct io_wqe *wqe, int index)
614 struct io_worker *worker;
617 __set_current_state(TASK_RUNNING);
619 worker = kzalloc_node(sizeof(*worker), GFP_KERNEL, wqe->node);
623 refcount_set(&worker->ref, 1);
624 worker->nulls_node.pprev = NULL;
626 spin_lock_init(&worker->lock);
627 init_completion(&worker->ref_done);
628 init_completion(&worker->started);
630 atomic_inc(&wq->worker_refs);
632 if (index == IO_WQ_ACCT_BOUND)
633 pid = io_wq_fork_thread(task_thread_bound, worker);
635 pid = io_wq_fork_thread(task_thread_unbound, worker);
637 if (atomic_dec_and_test(&wq->worker_refs))
638 complete(&wq->worker_done);
642 wait_for_completion(&worker->started);
646 static inline bool io_wqe_need_worker(struct io_wqe *wqe, int index)
647 __must_hold(wqe->lock)
649 struct io_wqe_acct *acct = &wqe->acct[index];
651 if (acct->nr_workers && test_bit(IO_WQ_BIT_EXIT, &wqe->wq->state))
653 /* if we have available workers or no work, no need */
654 if (!hlist_nulls_empty(&wqe->free_list) || !io_wqe_run_queue(wqe))
656 return acct->nr_workers < acct->max_workers;
660 * Iterate the passed in list and call the specific function for each
661 * worker that isn't exiting
663 static bool io_wq_for_each_worker(struct io_wqe *wqe,
664 bool (*func)(struct io_worker *, void *),
667 struct io_worker *worker;
670 list_for_each_entry_rcu(worker, &wqe->all_list, all_list) {
671 if (io_worker_get(worker)) {
672 /* no task if node is/was offline */
674 ret = func(worker, data);
675 io_worker_release(worker);
684 static bool io_wq_worker_wake(struct io_worker *worker, void *data)
686 wake_up_process(worker->task);
690 static void io_wq_check_workers(struct io_wq *wq)
694 for_each_node(node) {
695 struct io_wqe *wqe = wq->wqes[node];
696 bool fork_worker[2] = { false, false };
698 if (!node_online(node))
701 raw_spin_lock_irq(&wqe->lock);
702 if (io_wqe_need_worker(wqe, IO_WQ_ACCT_BOUND))
703 fork_worker[IO_WQ_ACCT_BOUND] = true;
704 if (io_wqe_need_worker(wqe, IO_WQ_ACCT_UNBOUND))
705 fork_worker[IO_WQ_ACCT_UNBOUND] = true;
706 raw_spin_unlock_irq(&wqe->lock);
707 if (fork_worker[IO_WQ_ACCT_BOUND])
708 create_io_worker(wq, wqe, IO_WQ_ACCT_BOUND);
709 if (fork_worker[IO_WQ_ACCT_UNBOUND])
710 create_io_worker(wq, wqe, IO_WQ_ACCT_UNBOUND);
715 * Manager thread. Tasked with creating new workers, if we need them.
717 static int io_wq_manager(void *data)
719 struct io_wq *wq = data;
720 char buf[TASK_COMM_LEN];
723 sprintf(buf, "iou-mgr-%d", wq->task_pid);
724 set_task_comm(current, buf);
725 current->flags |= PF_IO_WORKER;
726 wq->manager = get_task_struct(current);
728 complete(&wq->started);
731 set_current_state(TASK_INTERRUPTIBLE);
732 io_wq_check_workers(wq);
733 schedule_timeout(HZ);
734 if (fatal_signal_pending(current))
735 set_bit(IO_WQ_BIT_EXIT, &wq->state);
736 } while (!test_bit(IO_WQ_BIT_EXIT, &wq->state));
738 io_wq_check_workers(wq);
742 io_wq_for_each_worker(wq->wqes[node], io_wq_worker_wake, NULL);
745 /* we might not ever have created any workers */
746 if (atomic_read(&wq->worker_refs))
747 wait_for_completion(&wq->worker_done);
748 complete(&wq->exited);
752 static void io_run_cancel(struct io_wq_work *work, struct io_wqe *wqe)
754 struct io_wq *wq = wqe->wq;
757 work->flags |= IO_WQ_WORK_CANCEL;
759 work = wq->free_work(work);
763 static void io_wqe_insert_work(struct io_wqe *wqe, struct io_wq_work *work)
766 struct io_wq_work *tail;
768 if (!io_wq_is_hashed(work)) {
770 wq_list_add_tail(&work->list, &wqe->work_list);
774 hash = io_get_work_hash(work);
775 tail = wqe->hash_tail[hash];
776 wqe->hash_tail[hash] = work;
780 wq_list_add_after(&work->list, &tail->list, &wqe->work_list);
783 static int io_wq_fork_manager(struct io_wq *wq)
790 reinit_completion(&wq->worker_done);
791 current->flags |= PF_IO_WORKER;
792 ret = io_wq_fork_thread(io_wq_manager, wq);
793 current->flags &= ~PF_IO_WORKER;
795 wait_for_completion(&wq->started);
802 static void io_wqe_enqueue(struct io_wqe *wqe, struct io_wq_work *work)
804 struct io_wqe_acct *acct = io_work_get_acct(wqe, work);
808 /* Can only happen if manager creation fails after exec */
809 if (unlikely(io_wq_fork_manager(wqe->wq))) {
810 work->flags |= IO_WQ_WORK_CANCEL;
811 wqe->wq->do_work(work);
815 work_flags = work->flags;
816 raw_spin_lock_irqsave(&wqe->lock, flags);
817 io_wqe_insert_work(wqe, work);
818 wqe->flags &= ~IO_WQE_FLAG_STALLED;
819 raw_spin_unlock_irqrestore(&wqe->lock, flags);
821 if ((work_flags & IO_WQ_WORK_CONCURRENT) ||
822 !atomic_read(&acct->nr_running))
823 io_wqe_wake_worker(wqe, acct);
826 void io_wq_enqueue(struct io_wq *wq, struct io_wq_work *work)
828 struct io_wqe *wqe = wq->wqes[numa_node_id()];
830 io_wqe_enqueue(wqe, work);
834 * Work items that hash to the same value will not be done in parallel.
835 * Used to limit concurrent writes, generally hashed by inode.
837 void io_wq_hash_work(struct io_wq_work *work, void *val)
841 bit = hash_ptr(val, IO_WQ_HASH_ORDER);
842 work->flags |= (IO_WQ_WORK_HASHED | (bit << IO_WQ_HASH_SHIFT));
845 struct io_cb_cancel_data {
853 static bool io_wq_worker_cancel(struct io_worker *worker, void *data)
855 struct io_cb_cancel_data *match = data;
859 * Hold the lock to avoid ->cur_work going out of scope, caller
860 * may dereference the passed in work.
862 spin_lock_irqsave(&worker->lock, flags);
863 if (worker->cur_work &&
864 match->fn(worker->cur_work, match->data)) {
865 set_notify_signal(worker->task);
868 spin_unlock_irqrestore(&worker->lock, flags);
870 return match->nr_running && !match->cancel_all;
873 static inline void io_wqe_remove_pending(struct io_wqe *wqe,
874 struct io_wq_work *work,
875 struct io_wq_work_node *prev)
877 unsigned int hash = io_get_work_hash(work);
878 struct io_wq_work *prev_work = NULL;
880 if (io_wq_is_hashed(work) && work == wqe->hash_tail[hash]) {
882 prev_work = container_of(prev, struct io_wq_work, list);
883 if (prev_work && io_get_work_hash(prev_work) == hash)
884 wqe->hash_tail[hash] = prev_work;
886 wqe->hash_tail[hash] = NULL;
888 wq_list_del(&wqe->work_list, &work->list, prev);
891 static void io_wqe_cancel_pending_work(struct io_wqe *wqe,
892 struct io_cb_cancel_data *match)
894 struct io_wq_work_node *node, *prev;
895 struct io_wq_work *work;
899 raw_spin_lock_irqsave(&wqe->lock, flags);
900 wq_list_for_each(node, prev, &wqe->work_list) {
901 work = container_of(node, struct io_wq_work, list);
902 if (!match->fn(work, match->data))
904 io_wqe_remove_pending(wqe, work, prev);
905 raw_spin_unlock_irqrestore(&wqe->lock, flags);
906 io_run_cancel(work, wqe);
908 if (!match->cancel_all)
911 /* not safe to continue after unlock */
914 raw_spin_unlock_irqrestore(&wqe->lock, flags);
917 static void io_wqe_cancel_running_work(struct io_wqe *wqe,
918 struct io_cb_cancel_data *match)
921 io_wq_for_each_worker(wqe, io_wq_worker_cancel, match);
925 enum io_wq_cancel io_wq_cancel_cb(struct io_wq *wq, work_cancel_fn *cancel,
926 void *data, bool cancel_all)
928 struct io_cb_cancel_data match = {
931 .cancel_all = cancel_all,
936 * First check pending list, if we're lucky we can just remove it
937 * from there. CANCEL_OK means that the work is returned as-new,
938 * no completion will be posted for it.
940 for_each_node(node) {
941 struct io_wqe *wqe = wq->wqes[node];
943 io_wqe_cancel_pending_work(wqe, &match);
944 if (match.nr_pending && !match.cancel_all)
945 return IO_WQ_CANCEL_OK;
949 * Now check if a free (going busy) or busy worker has the work
950 * currently running. If we find it there, we'll return CANCEL_RUNNING
951 * as an indication that we attempt to signal cancellation. The
952 * completion will run normally in this case.
954 for_each_node(node) {
955 struct io_wqe *wqe = wq->wqes[node];
957 io_wqe_cancel_running_work(wqe, &match);
958 if (match.nr_running && !match.cancel_all)
959 return IO_WQ_CANCEL_RUNNING;
962 if (match.nr_running)
963 return IO_WQ_CANCEL_RUNNING;
964 if (match.nr_pending)
965 return IO_WQ_CANCEL_OK;
966 return IO_WQ_CANCEL_NOTFOUND;
969 static int io_wqe_hash_wake(struct wait_queue_entry *wait, unsigned mode,
972 struct io_wqe *wqe = container_of(wait, struct io_wqe, wait);
975 list_del_init(&wait->entry);
978 ret = io_wqe_activate_free_worker(wqe);
982 wake_up_process(wqe->wq->manager);
987 struct io_wq *io_wq_create(unsigned bounded, struct io_wq_data *data)
989 int ret = -ENOMEM, node;
992 if (WARN_ON_ONCE(!data->free_work || !data->do_work))
993 return ERR_PTR(-EINVAL);
995 wq = kzalloc(sizeof(*wq), GFP_KERNEL);
997 return ERR_PTR(-ENOMEM);
999 wq->wqes = kcalloc(nr_node_ids, sizeof(struct io_wqe *), GFP_KERNEL);
1003 ret = cpuhp_state_add_instance_nocalls(io_wq_online, &wq->cpuhp_node);
1007 refcount_inc(&data->hash->refs);
1008 wq->hash = data->hash;
1009 wq->free_work = data->free_work;
1010 wq->do_work = data->do_work;
1013 for_each_node(node) {
1015 int alloc_node = node;
1017 if (!node_online(alloc_node))
1018 alloc_node = NUMA_NO_NODE;
1019 wqe = kzalloc_node(sizeof(struct io_wqe), GFP_KERNEL, alloc_node);
1022 wq->wqes[node] = wqe;
1023 wqe->node = alloc_node;
1024 wqe->acct[IO_WQ_ACCT_BOUND].max_workers = bounded;
1025 atomic_set(&wqe->acct[IO_WQ_ACCT_BOUND].nr_running, 0);
1026 wqe->acct[IO_WQ_ACCT_UNBOUND].max_workers =
1027 task_rlimit(current, RLIMIT_NPROC);
1028 atomic_set(&wqe->acct[IO_WQ_ACCT_UNBOUND].nr_running, 0);
1029 wqe->wait.func = io_wqe_hash_wake;
1030 INIT_LIST_HEAD(&wqe->wait.entry);
1032 raw_spin_lock_init(&wqe->lock);
1033 INIT_WQ_LIST(&wqe->work_list);
1034 INIT_HLIST_NULLS_HEAD(&wqe->free_list, 0);
1035 INIT_LIST_HEAD(&wqe->all_list);
1038 wq->task_pid = current->pid;
1039 init_completion(&wq->started);
1040 init_completion(&wq->exited);
1041 refcount_set(&wq->refs, 1);
1043 init_completion(&wq->worker_done);
1044 atomic_set(&wq->worker_refs, 0);
1046 ret = io_wq_fork_manager(wq);
1051 io_wq_put_hash(data->hash);
1052 cpuhp_state_remove_instance_nocalls(io_wq_online, &wq->cpuhp_node);
1054 kfree(wq->wqes[node]);
1059 return ERR_PTR(ret);
1062 static void io_wq_destroy_manager(struct io_wq *wq)
1065 wake_up_process(wq->manager);
1066 wait_for_completion(&wq->exited);
1067 put_task_struct(wq->manager);
1072 static void io_wq_destroy(struct io_wq *wq)
1076 cpuhp_state_remove_instance_nocalls(io_wq_online, &wq->cpuhp_node);
1078 set_bit(IO_WQ_BIT_EXIT, &wq->state);
1079 io_wq_destroy_manager(wq);
1081 spin_lock_irq(&wq->hash->wait.lock);
1082 for_each_node(node) {
1083 struct io_wqe *wqe = wq->wqes[node];
1085 list_del_init(&wqe->wait.entry);
1088 spin_unlock_irq(&wq->hash->wait.lock);
1089 io_wq_put_hash(wq->hash);
1094 void io_wq_put(struct io_wq *wq)
1096 if (refcount_dec_and_test(&wq->refs))
1100 void io_wq_put_and_exit(struct io_wq *wq)
1102 set_bit(IO_WQ_BIT_EXIT, &wq->state);
1103 io_wq_destroy_manager(wq);
1107 static bool io_wq_worker_affinity(struct io_worker *worker, void *data)
1109 struct task_struct *task = worker->task;
1113 rq = task_rq_lock(task, &rf);
1114 do_set_cpus_allowed(task, cpumask_of_node(worker->wqe->node));
1115 task->flags |= PF_NO_SETAFFINITY;
1116 task_rq_unlock(rq, task, &rf);
1120 static int io_wq_cpu_online(unsigned int cpu, struct hlist_node *node)
1122 struct io_wq *wq = hlist_entry_safe(node, struct io_wq, cpuhp_node);
1127 io_wq_for_each_worker(wq->wqes[i], io_wq_worker_affinity, NULL);
1132 static __init int io_wq_init(void)
1136 ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "io-wq/online",
1137 io_wq_cpu_online, NULL);
1143 subsys_initcall(io_wq_init);