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>
19 #include <linux/freezer.h>
21 #include "../kernel/sched/sched.h"
24 #define WORKER_IDLE_TIMEOUT (5 * HZ)
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_FIXED = 8, /* static idle worker */
31 IO_WORKER_F_BOUND = 16, /* is doing bounded work */
35 IO_WQ_BIT_EXIT = 0, /* wq exiting */
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 struct completion ref_done;
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 exited;
120 atomic_t worker_refs;
121 struct completion worker_done;
123 struct hlist_node cpuhp_node;
128 static enum cpuhp_state io_wq_online;
130 struct io_cb_cancel_data {
138 static void io_wqe_cancel_pending_work(struct io_wqe *wqe,
139 struct io_cb_cancel_data *match);
141 static bool io_worker_get(struct io_worker *worker)
143 return refcount_inc_not_zero(&worker->ref);
146 static void io_worker_release(struct io_worker *worker)
148 if (refcount_dec_and_test(&worker->ref))
149 complete(&worker->ref_done);
152 static inline struct io_wqe_acct *io_work_get_acct(struct io_wqe *wqe,
153 struct io_wq_work *work)
155 if (work->flags & IO_WQ_WORK_UNBOUND)
156 return &wqe->acct[IO_WQ_ACCT_UNBOUND];
158 return &wqe->acct[IO_WQ_ACCT_BOUND];
161 static inline struct io_wqe_acct *io_wqe_get_acct(struct io_worker *worker)
163 struct io_wqe *wqe = worker->wqe;
165 if (worker->flags & IO_WORKER_F_BOUND)
166 return &wqe->acct[IO_WQ_ACCT_BOUND];
168 return &wqe->acct[IO_WQ_ACCT_UNBOUND];
171 static void io_worker_exit(struct io_worker *worker)
173 struct io_wqe *wqe = worker->wqe;
174 struct io_wqe_acct *acct = io_wqe_get_acct(worker);
177 if (refcount_dec_and_test(&worker->ref))
178 complete(&worker->ref_done);
179 wait_for_completion(&worker->ref_done);
182 current->flags &= ~PF_IO_WORKER;
183 flags = worker->flags;
185 if (flags & IO_WORKER_F_RUNNING)
186 atomic_dec(&acct->nr_running);
190 raw_spin_lock_irq(&wqe->lock);
191 if (flags & IO_WORKER_F_FREE)
192 hlist_nulls_del_rcu(&worker->nulls_node);
193 list_del_rcu(&worker->all_list);
195 raw_spin_unlock_irq(&wqe->lock);
197 kfree_rcu(worker, rcu);
198 if (atomic_dec_and_test(&wqe->wq->worker_refs))
199 complete(&wqe->wq->worker_done);
203 static inline bool io_wqe_run_queue(struct io_wqe *wqe)
204 __must_hold(wqe->lock)
206 if (!wq_list_empty(&wqe->work_list) &&
207 !(wqe->flags & IO_WQE_FLAG_STALLED))
213 * Check head of free list for an available worker. If one isn't available,
214 * caller must wake up the wq manager to create one.
216 static bool io_wqe_activate_free_worker(struct io_wqe *wqe)
219 struct hlist_nulls_node *n;
220 struct io_worker *worker;
222 n = rcu_dereference(hlist_nulls_first_rcu(&wqe->free_list));
226 worker = hlist_nulls_entry(n, struct io_worker, nulls_node);
227 if (io_worker_get(worker)) {
228 wake_up_process(worker->task);
229 io_worker_release(worker);
237 * We need a worker. If we find a free one, we're good. If not, and we're
238 * below the max number of workers, wake up the manager to create one.
240 static void io_wqe_wake_worker(struct io_wqe *wqe, struct io_wqe_acct *acct)
245 * Most likely an attempt to queue unbounded work on an io_wq that
246 * wasn't setup with any unbounded workers.
248 WARN_ON_ONCE(!acct->max_workers);
251 ret = io_wqe_activate_free_worker(wqe);
254 if (!ret && acct->nr_workers < acct->max_workers)
255 wake_up_process(wqe->wq->manager);
258 static void io_wqe_inc_running(struct io_worker *worker)
260 struct io_wqe_acct *acct = io_wqe_get_acct(worker);
262 atomic_inc(&acct->nr_running);
265 static void io_wqe_dec_running(struct io_worker *worker)
266 __must_hold(wqe->lock)
268 struct io_wqe_acct *acct = io_wqe_get_acct(worker);
269 struct io_wqe *wqe = worker->wqe;
271 if (atomic_dec_and_test(&acct->nr_running) && io_wqe_run_queue(wqe))
272 io_wqe_wake_worker(wqe, acct);
276 * Worker will start processing some work. Move it to the busy list, if
277 * it's currently on the freelist
279 static void __io_worker_busy(struct io_wqe *wqe, struct io_worker *worker,
280 struct io_wq_work *work)
281 __must_hold(wqe->lock)
283 bool worker_bound, work_bound;
285 if (worker->flags & IO_WORKER_F_FREE) {
286 worker->flags &= ~IO_WORKER_F_FREE;
287 hlist_nulls_del_init_rcu(&worker->nulls_node);
291 * If worker is moving from bound to unbound (or vice versa), then
292 * ensure we update the running accounting.
294 worker_bound = (worker->flags & IO_WORKER_F_BOUND) != 0;
295 work_bound = (work->flags & IO_WQ_WORK_UNBOUND) == 0;
296 if (worker_bound != work_bound) {
297 io_wqe_dec_running(worker);
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 worker->flags &= ~IO_WORKER_F_BOUND;
304 wqe->acct[IO_WQ_ACCT_UNBOUND].nr_workers++;
305 wqe->acct[IO_WQ_ACCT_BOUND].nr_workers--;
307 io_wqe_inc_running(worker);
312 * No work, worker going to sleep. Move to freelist, and unuse mm if we
313 * have one attached. Dropping the mm may potentially sleep, so we drop
314 * the lock in that case and return success. Since the caller has to
315 * retry the loop in that case (we changed task state), we don't regrab
316 * the lock if we return success.
318 static void __io_worker_idle(struct io_wqe *wqe, struct io_worker *worker)
319 __must_hold(wqe->lock)
321 if (!(worker->flags & IO_WORKER_F_FREE)) {
322 worker->flags |= IO_WORKER_F_FREE;
323 hlist_nulls_add_head_rcu(&worker->nulls_node, &wqe->free_list);
327 static inline unsigned int io_get_work_hash(struct io_wq_work *work)
329 return work->flags >> IO_WQ_HASH_SHIFT;
332 static void io_wait_on_hash(struct io_wqe *wqe, unsigned int hash)
334 struct io_wq *wq = wqe->wq;
336 spin_lock(&wq->hash->wait.lock);
337 if (list_empty(&wqe->wait.entry)) {
338 __add_wait_queue(&wq->hash->wait, &wqe->wait);
339 if (!test_bit(hash, &wq->hash->map)) {
340 __set_current_state(TASK_RUNNING);
341 list_del_init(&wqe->wait.entry);
344 spin_unlock(&wq->hash->wait.lock);
347 static struct io_wq_work *io_get_next_work(struct io_wqe *wqe)
348 __must_hold(wqe->lock)
350 struct io_wq_work_node *node, *prev;
351 struct io_wq_work *work, *tail;
352 unsigned int stall_hash = -1U;
354 wq_list_for_each(node, prev, &wqe->work_list) {
357 work = container_of(node, struct io_wq_work, list);
359 /* not hashed, can run anytime */
360 if (!io_wq_is_hashed(work)) {
361 wq_list_del(&wqe->work_list, node, prev);
365 hash = io_get_work_hash(work);
366 /* all items with this hash lie in [work, tail] */
367 tail = wqe->hash_tail[hash];
369 /* hashed, can run if not already running */
370 if (!test_and_set_bit(hash, &wqe->wq->hash->map)) {
371 wqe->hash_tail[hash] = NULL;
372 wq_list_cut(&wqe->work_list, &tail->list, prev);
375 if (stall_hash == -1U)
377 /* fast forward to a next hash, for-each will fix up @prev */
381 if (stall_hash != -1U) {
382 raw_spin_unlock(&wqe->lock);
383 io_wait_on_hash(wqe, stall_hash);
384 raw_spin_lock(&wqe->lock);
390 static void io_flush_signals(void)
392 if (unlikely(test_tsk_thread_flag(current, TIF_NOTIFY_SIGNAL))) {
393 if (current->task_works)
395 clear_tsk_thread_flag(current, TIF_NOTIFY_SIGNAL);
399 static void io_assign_current_work(struct io_worker *worker,
400 struct io_wq_work *work)
407 spin_lock_irq(&worker->lock);
408 worker->cur_work = work;
409 spin_unlock_irq(&worker->lock);
412 static void io_wqe_enqueue(struct io_wqe *wqe, struct io_wq_work *work);
414 static void io_worker_handle_work(struct io_worker *worker)
415 __releases(wqe->lock)
417 struct io_wqe *wqe = worker->wqe;
418 struct io_wq *wq = wqe->wq;
421 struct io_wq_work *work;
424 * If we got some work, mark us as busy. If we didn't, but
425 * the list isn't empty, it means we stalled on hashed work.
426 * Mark us stalled so we don't keep looking for work when we
427 * can't make progress, any work completion or insertion will
428 * clear the stalled flag.
430 work = io_get_next_work(wqe);
432 __io_worker_busy(wqe, worker, work);
433 else if (!wq_list_empty(&wqe->work_list))
434 wqe->flags |= IO_WQE_FLAG_STALLED;
436 raw_spin_unlock_irq(&wqe->lock);
439 io_assign_current_work(worker, work);
440 __set_current_state(TASK_RUNNING);
442 /* handle a whole dependent link */
444 struct io_wq_work *next_hashed, *linked;
445 unsigned int hash = io_get_work_hash(work);
447 next_hashed = wq_next_work(work);
449 io_assign_current_work(worker, NULL);
451 linked = wq->free_work(work);
453 if (!work && linked && !io_wq_is_hashed(linked)) {
457 io_assign_current_work(worker, work);
459 io_wqe_enqueue(wqe, linked);
461 if (hash != -1U && !next_hashed) {
462 clear_bit(hash, &wq->hash->map);
463 if (wq_has_sleeper(&wq->hash->wait))
464 wake_up(&wq->hash->wait);
465 raw_spin_lock_irq(&wqe->lock);
466 wqe->flags &= ~IO_WQE_FLAG_STALLED;
467 /* skip unnecessary unlock-lock wqe->lock */
470 raw_spin_unlock_irq(&wqe->lock);
474 raw_spin_lock_irq(&wqe->lock);
478 static int io_wqe_worker(void *data)
480 struct io_worker *worker = data;
481 struct io_wqe *wqe = worker->wqe;
482 struct io_wq *wq = wqe->wq;
483 char buf[TASK_COMM_LEN];
485 worker->flags |= (IO_WORKER_F_UP | IO_WORKER_F_RUNNING);
486 io_wqe_inc_running(worker);
488 sprintf(buf, "iou-wrk-%d", wq->task_pid);
489 set_task_comm(current, buf);
491 while (!test_bit(IO_WQ_BIT_EXIT, &wq->state)) {
492 set_current_state(TASK_INTERRUPTIBLE);
494 raw_spin_lock_irq(&wqe->lock);
495 if (io_wqe_run_queue(wqe)) {
496 io_worker_handle_work(worker);
499 __io_worker_idle(wqe, worker);
500 raw_spin_unlock_irq(&wqe->lock);
502 if (schedule_timeout(WORKER_IDLE_TIMEOUT))
504 if (fatal_signal_pending(current))
506 /* timed out, exit unless we're the fixed worker */
507 if (test_bit(IO_WQ_BIT_EXIT, &wq->state) ||
508 !(worker->flags & IO_WORKER_F_FIXED))
512 if (test_bit(IO_WQ_BIT_EXIT, &wq->state)) {
513 raw_spin_lock_irq(&wqe->lock);
514 if (!wq_list_empty(&wqe->work_list))
515 io_worker_handle_work(worker);
517 raw_spin_unlock_irq(&wqe->lock);
520 io_worker_exit(worker);
525 * Called when a worker is scheduled in. Mark us as currently running.
527 void io_wq_worker_running(struct task_struct *tsk)
529 struct io_worker *worker = tsk->pf_io_worker;
533 if (!(worker->flags & IO_WORKER_F_UP))
535 if (worker->flags & IO_WORKER_F_RUNNING)
537 worker->flags |= IO_WORKER_F_RUNNING;
538 io_wqe_inc_running(worker);
542 * Called when worker is going to sleep. If there are no workers currently
543 * running and we have work pending, wake up a free one or have the manager
546 void io_wq_worker_sleeping(struct task_struct *tsk)
548 struct io_worker *worker = tsk->pf_io_worker;
552 if (!(worker->flags & IO_WORKER_F_UP))
554 if (!(worker->flags & IO_WORKER_F_RUNNING))
557 worker->flags &= ~IO_WORKER_F_RUNNING;
559 raw_spin_lock_irq(&worker->wqe->lock);
560 io_wqe_dec_running(worker);
561 raw_spin_unlock_irq(&worker->wqe->lock);
564 static bool create_io_worker(struct io_wq *wq, struct io_wqe *wqe, int index)
566 struct io_wqe_acct *acct = &wqe->acct[index];
567 struct io_worker *worker;
568 struct task_struct *tsk;
570 __set_current_state(TASK_RUNNING);
572 worker = kzalloc_node(sizeof(*worker), GFP_KERNEL, wqe->node);
576 refcount_set(&worker->ref, 1);
577 worker->nulls_node.pprev = NULL;
579 spin_lock_init(&worker->lock);
580 init_completion(&worker->ref_done);
582 atomic_inc(&wq->worker_refs);
584 tsk = create_io_thread(io_wqe_worker, worker, wqe->node);
586 if (atomic_dec_and_test(&wq->worker_refs))
587 complete(&wq->worker_done);
592 tsk->pf_io_worker = worker;
594 set_cpus_allowed_ptr(tsk, cpumask_of_node(wqe->node));
595 tsk->flags |= PF_NOFREEZE | PF_NO_SETAFFINITY;
597 raw_spin_lock_irq(&wqe->lock);
598 hlist_nulls_add_head_rcu(&worker->nulls_node, &wqe->free_list);
599 list_add_tail_rcu(&worker->all_list, &wqe->all_list);
600 worker->flags |= IO_WORKER_F_FREE;
601 if (index == IO_WQ_ACCT_BOUND)
602 worker->flags |= IO_WORKER_F_BOUND;
603 if (!acct->nr_workers && (worker->flags & IO_WORKER_F_BOUND))
604 worker->flags |= IO_WORKER_F_FIXED;
606 raw_spin_unlock_irq(&wqe->lock);
607 wake_up_new_task(tsk);
611 static inline bool io_wqe_need_worker(struct io_wqe *wqe, int index)
612 __must_hold(wqe->lock)
614 struct io_wqe_acct *acct = &wqe->acct[index];
616 if (acct->nr_workers && test_bit(IO_WQ_BIT_EXIT, &wqe->wq->state))
618 /* if we have available workers or no work, no need */
619 if (!hlist_nulls_empty(&wqe->free_list) || !io_wqe_run_queue(wqe))
621 return acct->nr_workers < acct->max_workers;
625 * Iterate the passed in list and call the specific function for each
626 * worker that isn't exiting
628 static bool io_wq_for_each_worker(struct io_wqe *wqe,
629 bool (*func)(struct io_worker *, void *),
632 struct io_worker *worker;
635 list_for_each_entry_rcu(worker, &wqe->all_list, all_list) {
636 if (io_worker_get(worker)) {
637 /* no task if node is/was offline */
639 ret = func(worker, data);
640 io_worker_release(worker);
649 static bool io_wq_worker_wake(struct io_worker *worker, void *data)
651 set_notify_signal(worker->task);
652 wake_up_process(worker->task);
656 static void io_wq_check_workers(struct io_wq *wq)
660 for_each_node(node) {
661 struct io_wqe *wqe = wq->wqes[node];
662 bool fork_worker[2] = { false, false };
664 if (!node_online(node))
667 raw_spin_lock_irq(&wqe->lock);
668 if (io_wqe_need_worker(wqe, IO_WQ_ACCT_BOUND))
669 fork_worker[IO_WQ_ACCT_BOUND] = true;
670 if (io_wqe_need_worker(wqe, IO_WQ_ACCT_UNBOUND))
671 fork_worker[IO_WQ_ACCT_UNBOUND] = true;
672 raw_spin_unlock_irq(&wqe->lock);
673 if (fork_worker[IO_WQ_ACCT_BOUND])
674 create_io_worker(wq, wqe, IO_WQ_ACCT_BOUND);
675 if (fork_worker[IO_WQ_ACCT_UNBOUND])
676 create_io_worker(wq, wqe, IO_WQ_ACCT_UNBOUND);
680 static bool io_wq_work_match_all(struct io_wq_work *work, void *data)
685 static void io_wq_cancel_pending(struct io_wq *wq)
687 struct io_cb_cancel_data match = {
688 .fn = io_wq_work_match_all,
694 io_wqe_cancel_pending_work(wq->wqes[node], &match);
698 * Manager thread. Tasked with creating new workers, if we need them.
700 static int io_wq_manager(void *data)
702 struct io_wq *wq = data;
703 char buf[TASK_COMM_LEN];
706 sprintf(buf, "iou-mgr-%d", wq->task_pid);
707 set_task_comm(current, buf);
710 set_current_state(TASK_INTERRUPTIBLE);
711 io_wq_check_workers(wq);
712 schedule_timeout(HZ);
714 if (fatal_signal_pending(current))
715 set_bit(IO_WQ_BIT_EXIT, &wq->state);
716 } while (!test_bit(IO_WQ_BIT_EXIT, &wq->state));
718 io_wq_check_workers(wq);
722 io_wq_for_each_worker(wq->wqes[node], io_wq_worker_wake, NULL);
725 /* we might not ever have created any workers */
726 if (atomic_read(&wq->worker_refs))
727 wait_for_completion(&wq->worker_done);
729 spin_lock_irq(&wq->hash->wait.lock);
731 list_del_init(&wq->wqes[node]->wait.entry);
732 spin_unlock_irq(&wq->hash->wait.lock);
734 io_wq_cancel_pending(wq);
735 complete(&wq->exited);
739 static void io_run_cancel(struct io_wq_work *work, struct io_wqe *wqe)
741 struct io_wq *wq = wqe->wq;
744 work->flags |= IO_WQ_WORK_CANCEL;
746 work = wq->free_work(work);
750 static void io_wqe_insert_work(struct io_wqe *wqe, struct io_wq_work *work)
753 struct io_wq_work *tail;
755 if (!io_wq_is_hashed(work)) {
757 wq_list_add_tail(&work->list, &wqe->work_list);
761 hash = io_get_work_hash(work);
762 tail = wqe->hash_tail[hash];
763 wqe->hash_tail[hash] = work;
767 wq_list_add_after(&work->list, &tail->list, &wqe->work_list);
770 static int io_wq_fork_manager(struct io_wq *wq)
772 struct task_struct *tsk;
777 reinit_completion(&wq->worker_done);
778 tsk = create_io_thread(io_wq_manager, wq, NUMA_NO_NODE);
780 wq->manager = get_task_struct(tsk);
781 wake_up_new_task(tsk);
788 static void io_wqe_enqueue(struct io_wqe *wqe, struct io_wq_work *work)
790 struct io_wqe_acct *acct = io_work_get_acct(wqe, work);
794 /* Can only happen if manager creation fails after exec */
795 if (io_wq_fork_manager(wqe->wq) ||
796 test_bit(IO_WQ_BIT_EXIT, &wqe->wq->state)) {
797 work->flags |= IO_WQ_WORK_CANCEL;
798 wqe->wq->do_work(work);
802 work_flags = work->flags;
803 raw_spin_lock_irqsave(&wqe->lock, flags);
804 io_wqe_insert_work(wqe, work);
805 wqe->flags &= ~IO_WQE_FLAG_STALLED;
806 raw_spin_unlock_irqrestore(&wqe->lock, flags);
808 if ((work_flags & IO_WQ_WORK_CONCURRENT) ||
809 !atomic_read(&acct->nr_running))
810 io_wqe_wake_worker(wqe, acct);
813 void io_wq_enqueue(struct io_wq *wq, struct io_wq_work *work)
815 struct io_wqe *wqe = wq->wqes[numa_node_id()];
817 io_wqe_enqueue(wqe, work);
821 * Work items that hash to the same value will not be done in parallel.
822 * Used to limit concurrent writes, generally hashed by inode.
824 void io_wq_hash_work(struct io_wq_work *work, void *val)
828 bit = hash_ptr(val, IO_WQ_HASH_ORDER);
829 work->flags |= (IO_WQ_WORK_HASHED | (bit << IO_WQ_HASH_SHIFT));
832 static bool io_wq_worker_cancel(struct io_worker *worker, void *data)
834 struct io_cb_cancel_data *match = data;
838 * Hold the lock to avoid ->cur_work going out of scope, caller
839 * may dereference the passed in work.
841 spin_lock_irqsave(&worker->lock, flags);
842 if (worker->cur_work &&
843 match->fn(worker->cur_work, match->data)) {
844 set_notify_signal(worker->task);
847 spin_unlock_irqrestore(&worker->lock, flags);
849 return match->nr_running && !match->cancel_all;
852 static inline void io_wqe_remove_pending(struct io_wqe *wqe,
853 struct io_wq_work *work,
854 struct io_wq_work_node *prev)
856 unsigned int hash = io_get_work_hash(work);
857 struct io_wq_work *prev_work = NULL;
859 if (io_wq_is_hashed(work) && work == wqe->hash_tail[hash]) {
861 prev_work = container_of(prev, struct io_wq_work, list);
862 if (prev_work && io_get_work_hash(prev_work) == hash)
863 wqe->hash_tail[hash] = prev_work;
865 wqe->hash_tail[hash] = NULL;
867 wq_list_del(&wqe->work_list, &work->list, prev);
870 static void io_wqe_cancel_pending_work(struct io_wqe *wqe,
871 struct io_cb_cancel_data *match)
873 struct io_wq_work_node *node, *prev;
874 struct io_wq_work *work;
878 raw_spin_lock_irqsave(&wqe->lock, flags);
879 wq_list_for_each(node, prev, &wqe->work_list) {
880 work = container_of(node, struct io_wq_work, list);
881 if (!match->fn(work, match->data))
883 io_wqe_remove_pending(wqe, work, prev);
884 raw_spin_unlock_irqrestore(&wqe->lock, flags);
885 io_run_cancel(work, wqe);
887 if (!match->cancel_all)
890 /* not safe to continue after unlock */
893 raw_spin_unlock_irqrestore(&wqe->lock, flags);
896 static void io_wqe_cancel_running_work(struct io_wqe *wqe,
897 struct io_cb_cancel_data *match)
900 io_wq_for_each_worker(wqe, io_wq_worker_cancel, match);
904 enum io_wq_cancel io_wq_cancel_cb(struct io_wq *wq, work_cancel_fn *cancel,
905 void *data, bool cancel_all)
907 struct io_cb_cancel_data match = {
910 .cancel_all = cancel_all,
915 * First check pending list, if we're lucky we can just remove it
916 * from there. CANCEL_OK means that the work is returned as-new,
917 * no completion will be posted for it.
919 for_each_node(node) {
920 struct io_wqe *wqe = wq->wqes[node];
922 io_wqe_cancel_pending_work(wqe, &match);
923 if (match.nr_pending && !match.cancel_all)
924 return IO_WQ_CANCEL_OK;
928 * Now check if a free (going busy) or busy worker has the work
929 * currently running. If we find it there, we'll return CANCEL_RUNNING
930 * as an indication that we attempt to signal cancellation. The
931 * completion will run normally in this case.
933 for_each_node(node) {
934 struct io_wqe *wqe = wq->wqes[node];
936 io_wqe_cancel_running_work(wqe, &match);
937 if (match.nr_running && !match.cancel_all)
938 return IO_WQ_CANCEL_RUNNING;
941 if (match.nr_running)
942 return IO_WQ_CANCEL_RUNNING;
943 if (match.nr_pending)
944 return IO_WQ_CANCEL_OK;
945 return IO_WQ_CANCEL_NOTFOUND;
948 static int io_wqe_hash_wake(struct wait_queue_entry *wait, unsigned mode,
951 struct io_wqe *wqe = container_of(wait, struct io_wqe, wait);
954 list_del_init(&wait->entry);
957 ret = io_wqe_activate_free_worker(wqe);
961 wake_up_process(wqe->wq->manager);
966 struct io_wq *io_wq_create(unsigned bounded, struct io_wq_data *data)
968 int ret = -ENOMEM, node;
971 if (WARN_ON_ONCE(!data->free_work || !data->do_work))
972 return ERR_PTR(-EINVAL);
974 wq = kzalloc(sizeof(*wq), GFP_KERNEL);
976 return ERR_PTR(-ENOMEM);
978 wq->wqes = kcalloc(nr_node_ids, sizeof(struct io_wqe *), GFP_KERNEL);
982 ret = cpuhp_state_add_instance_nocalls(io_wq_online, &wq->cpuhp_node);
986 refcount_inc(&data->hash->refs);
987 wq->hash = data->hash;
988 wq->free_work = data->free_work;
989 wq->do_work = data->do_work;
992 for_each_node(node) {
994 int alloc_node = node;
996 if (!node_online(alloc_node))
997 alloc_node = NUMA_NO_NODE;
998 wqe = kzalloc_node(sizeof(struct io_wqe), GFP_KERNEL, alloc_node);
1001 wq->wqes[node] = wqe;
1002 wqe->node = alloc_node;
1003 wqe->acct[IO_WQ_ACCT_BOUND].max_workers = bounded;
1004 atomic_set(&wqe->acct[IO_WQ_ACCT_BOUND].nr_running, 0);
1005 wqe->acct[IO_WQ_ACCT_UNBOUND].max_workers =
1006 task_rlimit(current, RLIMIT_NPROC);
1007 atomic_set(&wqe->acct[IO_WQ_ACCT_UNBOUND].nr_running, 0);
1008 wqe->wait.func = io_wqe_hash_wake;
1009 INIT_LIST_HEAD(&wqe->wait.entry);
1011 raw_spin_lock_init(&wqe->lock);
1012 INIT_WQ_LIST(&wqe->work_list);
1013 INIT_HLIST_NULLS_HEAD(&wqe->free_list, 0);
1014 INIT_LIST_HEAD(&wqe->all_list);
1017 wq->task_pid = current->pid;
1018 init_completion(&wq->exited);
1019 refcount_set(&wq->refs, 1);
1021 init_completion(&wq->worker_done);
1022 atomic_set(&wq->worker_refs, 0);
1024 ret = io_wq_fork_manager(wq);
1029 io_wq_put_hash(data->hash);
1030 cpuhp_state_remove_instance_nocalls(io_wq_online, &wq->cpuhp_node);
1032 kfree(wq->wqes[node]);
1037 return ERR_PTR(ret);
1040 static void io_wq_destroy_manager(struct io_wq *wq)
1043 wake_up_process(wq->manager);
1044 wait_for_completion(&wq->exited);
1045 put_task_struct(wq->manager);
1050 static void io_wq_destroy(struct io_wq *wq)
1054 cpuhp_state_remove_instance_nocalls(io_wq_online, &wq->cpuhp_node);
1056 set_bit(IO_WQ_BIT_EXIT, &wq->state);
1057 io_wq_destroy_manager(wq);
1059 for_each_node(node) {
1060 struct io_wqe *wqe = wq->wqes[node];
1061 WARN_ON_ONCE(!wq_list_empty(&wqe->work_list));
1064 io_wq_put_hash(wq->hash);
1069 void io_wq_put(struct io_wq *wq)
1071 if (refcount_dec_and_test(&wq->refs))
1075 void io_wq_put_and_exit(struct io_wq *wq)
1077 set_bit(IO_WQ_BIT_EXIT, &wq->state);
1078 io_wq_destroy_manager(wq);
1082 static bool io_wq_worker_affinity(struct io_worker *worker, void *data)
1084 struct task_struct *task = worker->task;
1088 rq = task_rq_lock(task, &rf);
1089 do_set_cpus_allowed(task, cpumask_of_node(worker->wqe->node));
1090 task->flags |= PF_NO_SETAFFINITY;
1091 task_rq_unlock(rq, task, &rf);
1095 static int io_wq_cpu_online(unsigned int cpu, struct hlist_node *node)
1097 struct io_wq *wq = hlist_entry_safe(node, struct io_wq, cpuhp_node);
1102 io_wq_for_each_worker(wq->wqes[i], io_wq_worker_affinity, NULL);
1107 static __init int io_wq_init(void)
1111 ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "io-wq/online",
1112 io_wq_cpu_online, NULL);
1118 subsys_initcall(io_wq_init);