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;
57 struct completion started;
62 #if BITS_PER_LONG == 64
63 #define IO_WQ_HASH_ORDER 6
65 #define IO_WQ_HASH_ORDER 5
68 #define IO_WQ_NR_HASH_BUCKETS (1u << IO_WQ_HASH_ORDER)
82 * Per-node worker thread pool
87 struct io_wq_work_list work_list;
89 } ____cacheline_aligned_in_smp;
92 struct io_wqe_acct acct[2];
94 struct hlist_nulls_head free_list;
95 struct list_head all_list;
97 struct wait_queue_entry wait;
100 struct io_wq_work *hash_tail[IO_WQ_NR_HASH_BUCKETS];
107 struct io_wqe **wqes;
110 free_work_fn *free_work;
111 io_wq_work_fn *do_work;
113 struct task_struct *manager;
114 struct user_struct *user;
116 struct io_wq_hash *hash;
119 struct completion started;
120 struct completion exited;
122 atomic_t worker_refs;
123 struct completion worker_done;
125 struct hlist_node cpuhp_node;
130 static enum cpuhp_state io_wq_online;
132 struct io_cb_cancel_data {
140 static void io_wqe_cancel_pending_work(struct io_wqe *wqe,
141 struct io_cb_cancel_data *match);
143 static bool io_worker_get(struct io_worker *worker)
145 return refcount_inc_not_zero(&worker->ref);
148 static void io_worker_release(struct io_worker *worker)
150 if (refcount_dec_and_test(&worker->ref))
151 complete(&worker->ref_done);
154 static inline struct io_wqe_acct *io_work_get_acct(struct io_wqe *wqe,
155 struct io_wq_work *work)
157 if (work->flags & IO_WQ_WORK_UNBOUND)
158 return &wqe->acct[IO_WQ_ACCT_UNBOUND];
160 return &wqe->acct[IO_WQ_ACCT_BOUND];
163 static inline struct io_wqe_acct *io_wqe_get_acct(struct io_worker *worker)
165 struct io_wqe *wqe = worker->wqe;
167 if (worker->flags & IO_WORKER_F_BOUND)
168 return &wqe->acct[IO_WQ_ACCT_BOUND];
170 return &wqe->acct[IO_WQ_ACCT_UNBOUND];
173 static void io_worker_exit(struct io_worker *worker)
175 struct io_wqe *wqe = worker->wqe;
176 struct io_wqe_acct *acct = io_wqe_get_acct(worker);
179 if (refcount_dec_and_test(&worker->ref))
180 complete(&worker->ref_done);
181 wait_for_completion(&worker->ref_done);
184 current->flags &= ~PF_IO_WORKER;
185 flags = worker->flags;
187 if (flags & IO_WORKER_F_RUNNING)
188 atomic_dec(&acct->nr_running);
192 raw_spin_lock_irq(&wqe->lock);
193 if (flags & IO_WORKER_F_FREE)
194 hlist_nulls_del_rcu(&worker->nulls_node);
195 list_del_rcu(&worker->all_list);
197 raw_spin_unlock_irq(&wqe->lock);
199 kfree_rcu(worker, rcu);
200 if (atomic_dec_and_test(&wqe->wq->worker_refs))
201 complete(&wqe->wq->worker_done);
204 static inline bool io_wqe_run_queue(struct io_wqe *wqe)
205 __must_hold(wqe->lock)
207 if (!wq_list_empty(&wqe->work_list) &&
208 !(wqe->flags & IO_WQE_FLAG_STALLED))
214 * Check head of free list for an available worker. If one isn't available,
215 * caller must wake up the wq manager to create one.
217 static bool io_wqe_activate_free_worker(struct io_wqe *wqe)
220 struct hlist_nulls_node *n;
221 struct io_worker *worker;
223 n = rcu_dereference(hlist_nulls_first_rcu(&wqe->free_list));
227 worker = hlist_nulls_entry(n, struct io_worker, nulls_node);
228 if (io_worker_get(worker)) {
229 wake_up_process(worker->task);
230 io_worker_release(worker);
238 * We need a worker. If we find a free one, we're good. If not, and we're
239 * below the max number of workers, wake up the manager to create one.
241 static void io_wqe_wake_worker(struct io_wqe *wqe, struct io_wqe_acct *acct)
246 * Most likely an attempt to queue unbounded work on an io_wq that
247 * wasn't setup with any unbounded workers.
249 WARN_ON_ONCE(!acct->max_workers);
252 ret = io_wqe_activate_free_worker(wqe);
255 if (!ret && acct->nr_workers < acct->max_workers)
256 wake_up_process(wqe->wq->manager);
259 static void io_wqe_inc_running(struct io_worker *worker)
261 struct io_wqe_acct *acct = io_wqe_get_acct(worker);
263 atomic_inc(&acct->nr_running);
266 static void io_wqe_dec_running(struct io_worker *worker)
267 __must_hold(wqe->lock)
269 struct io_wqe_acct *acct = io_wqe_get_acct(worker);
270 struct io_wqe *wqe = worker->wqe;
272 if (atomic_dec_and_test(&acct->nr_running) && io_wqe_run_queue(wqe))
273 io_wqe_wake_worker(wqe, acct);
276 static void io_worker_start(struct io_worker *worker)
278 current->flags |= PF_NOFREEZE;
279 worker->flags |= (IO_WORKER_F_UP | IO_WORKER_F_RUNNING);
280 io_wqe_inc_running(worker);
281 complete(&worker->started);
285 * Worker will start processing some work. Move it to the busy list, if
286 * it's currently on the freelist
288 static void __io_worker_busy(struct io_wqe *wqe, struct io_worker *worker,
289 struct io_wq_work *work)
290 __must_hold(wqe->lock)
292 bool worker_bound, work_bound;
294 if (worker->flags & IO_WORKER_F_FREE) {
295 worker->flags &= ~IO_WORKER_F_FREE;
296 hlist_nulls_del_init_rcu(&worker->nulls_node);
300 * If worker is moving from bound to unbound (or vice versa), then
301 * ensure we update the running accounting.
303 worker_bound = (worker->flags & IO_WORKER_F_BOUND) != 0;
304 work_bound = (work->flags & IO_WQ_WORK_UNBOUND) == 0;
305 if (worker_bound != work_bound) {
306 io_wqe_dec_running(worker);
308 worker->flags |= IO_WORKER_F_BOUND;
309 wqe->acct[IO_WQ_ACCT_UNBOUND].nr_workers--;
310 wqe->acct[IO_WQ_ACCT_BOUND].nr_workers++;
312 worker->flags &= ~IO_WORKER_F_BOUND;
313 wqe->acct[IO_WQ_ACCT_UNBOUND].nr_workers++;
314 wqe->acct[IO_WQ_ACCT_BOUND].nr_workers--;
316 io_wqe_inc_running(worker);
321 * No work, worker going to sleep. Move to freelist, and unuse mm if we
322 * have one attached. Dropping the mm may potentially sleep, so we drop
323 * the lock in that case and return success. Since the caller has to
324 * retry the loop in that case (we changed task state), we don't regrab
325 * the lock if we return success.
327 static void __io_worker_idle(struct io_wqe *wqe, struct io_worker *worker)
328 __must_hold(wqe->lock)
330 if (!(worker->flags & IO_WORKER_F_FREE)) {
331 worker->flags |= IO_WORKER_F_FREE;
332 hlist_nulls_add_head_rcu(&worker->nulls_node, &wqe->free_list);
336 static inline unsigned int io_get_work_hash(struct io_wq_work *work)
338 return work->flags >> IO_WQ_HASH_SHIFT;
341 static void io_wait_on_hash(struct io_wqe *wqe, unsigned int hash)
343 struct io_wq *wq = wqe->wq;
345 spin_lock(&wq->hash->wait.lock);
346 if (list_empty(&wqe->wait.entry)) {
347 __add_wait_queue(&wq->hash->wait, &wqe->wait);
348 if (!test_bit(hash, &wq->hash->map)) {
349 __set_current_state(TASK_RUNNING);
350 list_del_init(&wqe->wait.entry);
353 spin_unlock(&wq->hash->wait.lock);
356 static struct io_wq_work *io_get_next_work(struct io_wqe *wqe)
357 __must_hold(wqe->lock)
359 struct io_wq_work_node *node, *prev;
360 struct io_wq_work *work, *tail;
361 unsigned int stall_hash = -1U;
363 wq_list_for_each(node, prev, &wqe->work_list) {
366 work = container_of(node, struct io_wq_work, list);
368 /* not hashed, can run anytime */
369 if (!io_wq_is_hashed(work)) {
370 wq_list_del(&wqe->work_list, node, prev);
374 hash = io_get_work_hash(work);
375 /* all items with this hash lie in [work, tail] */
376 tail = wqe->hash_tail[hash];
378 /* hashed, can run if not already running */
379 if (!test_and_set_bit(hash, &wqe->wq->hash->map)) {
380 wqe->hash_tail[hash] = NULL;
381 wq_list_cut(&wqe->work_list, &tail->list, prev);
384 if (stall_hash == -1U)
386 /* fast forward to a next hash, for-each will fix up @prev */
390 if (stall_hash != -1U) {
391 raw_spin_unlock(&wqe->lock);
392 io_wait_on_hash(wqe, stall_hash);
393 raw_spin_lock(&wqe->lock);
399 static void io_flush_signals(void)
401 if (unlikely(test_tsk_thread_flag(current, TIF_NOTIFY_SIGNAL))) {
402 if (current->task_works)
404 clear_tsk_thread_flag(current, TIF_NOTIFY_SIGNAL);
408 static void io_assign_current_work(struct io_worker *worker,
409 struct io_wq_work *work)
416 spin_lock_irq(&worker->lock);
417 worker->cur_work = work;
418 spin_unlock_irq(&worker->lock);
421 static void io_wqe_enqueue(struct io_wqe *wqe, struct io_wq_work *work);
423 static void io_worker_handle_work(struct io_worker *worker)
424 __releases(wqe->lock)
426 struct io_wqe *wqe = worker->wqe;
427 struct io_wq *wq = wqe->wq;
430 struct io_wq_work *work;
433 * If we got some work, mark us as busy. If we didn't, but
434 * the list isn't empty, it means we stalled on hashed work.
435 * Mark us stalled so we don't keep looking for work when we
436 * can't make progress, any work completion or insertion will
437 * clear the stalled flag.
439 work = io_get_next_work(wqe);
441 __io_worker_busy(wqe, worker, work);
442 else if (!wq_list_empty(&wqe->work_list))
443 wqe->flags |= IO_WQE_FLAG_STALLED;
445 raw_spin_unlock_irq(&wqe->lock);
448 io_assign_current_work(worker, work);
449 __set_current_state(TASK_RUNNING);
451 /* handle a whole dependent link */
453 struct io_wq_work *next_hashed, *linked;
454 unsigned int hash = io_get_work_hash(work);
456 next_hashed = wq_next_work(work);
458 io_assign_current_work(worker, NULL);
460 linked = wq->free_work(work);
462 if (!work && linked && !io_wq_is_hashed(linked)) {
466 io_assign_current_work(worker, work);
468 io_wqe_enqueue(wqe, linked);
470 if (hash != -1U && !next_hashed) {
471 clear_bit(hash, &wq->hash->map);
472 if (wq_has_sleeper(&wq->hash->wait))
473 wake_up(&wq->hash->wait);
474 raw_spin_lock_irq(&wqe->lock);
475 wqe->flags &= ~IO_WQE_FLAG_STALLED;
476 /* skip unnecessary unlock-lock wqe->lock */
479 raw_spin_unlock_irq(&wqe->lock);
483 raw_spin_lock_irq(&wqe->lock);
487 static int io_wqe_worker(void *data)
489 struct io_worker *worker = data;
490 struct io_wqe *wqe = worker->wqe;
491 struct io_wq *wq = wqe->wq;
493 io_worker_start(worker);
495 while (!test_bit(IO_WQ_BIT_EXIT, &wq->state)) {
496 set_current_state(TASK_INTERRUPTIBLE);
498 raw_spin_lock_irq(&wqe->lock);
499 if (io_wqe_run_queue(wqe)) {
500 io_worker_handle_work(worker);
503 __io_worker_idle(wqe, worker);
504 raw_spin_unlock_irq(&wqe->lock);
506 if (schedule_timeout(WORKER_IDLE_TIMEOUT))
508 if (fatal_signal_pending(current))
510 /* timed out, exit unless we're the fixed worker */
511 if (test_bit(IO_WQ_BIT_EXIT, &wq->state) ||
512 !(worker->flags & IO_WORKER_F_FIXED))
516 if (test_bit(IO_WQ_BIT_EXIT, &wq->state)) {
517 raw_spin_lock_irq(&wqe->lock);
518 if (!wq_list_empty(&wqe->work_list))
519 io_worker_handle_work(worker);
521 raw_spin_unlock_irq(&wqe->lock);
524 io_worker_exit(worker);
529 * Called when a worker is scheduled in. Mark us as currently running.
531 void io_wq_worker_running(struct task_struct *tsk)
533 struct io_worker *worker = tsk->pf_io_worker;
537 if (!(worker->flags & IO_WORKER_F_UP))
539 if (worker->flags & IO_WORKER_F_RUNNING)
541 worker->flags |= IO_WORKER_F_RUNNING;
542 io_wqe_inc_running(worker);
546 * Called when worker is going to sleep. If there are no workers currently
547 * running and we have work pending, wake up a free one or have the manager
550 void io_wq_worker_sleeping(struct task_struct *tsk)
552 struct io_worker *worker = tsk->pf_io_worker;
556 if (!(worker->flags & IO_WORKER_F_UP))
558 if (!(worker->flags & IO_WORKER_F_RUNNING))
561 worker->flags &= ~IO_WORKER_F_RUNNING;
563 raw_spin_lock_irq(&worker->wqe->lock);
564 io_wqe_dec_running(worker);
565 raw_spin_unlock_irq(&worker->wqe->lock);
568 static int task_thread(void *data, int index)
570 struct io_worker *worker = data;
571 struct io_wqe *wqe = worker->wqe;
572 struct io_wqe_acct *acct = &wqe->acct[index];
573 struct io_wq *wq = wqe->wq;
574 char buf[TASK_COMM_LEN];
576 sprintf(buf, "iou-wrk-%d", wq->task_pid);
577 set_task_comm(current, buf);
579 current->pf_io_worker = worker;
580 worker->task = current;
582 set_cpus_allowed_ptr(current, cpumask_of_node(wqe->node));
583 current->flags |= PF_NO_SETAFFINITY;
585 raw_spin_lock_irq(&wqe->lock);
586 hlist_nulls_add_head_rcu(&worker->nulls_node, &wqe->free_list);
587 list_add_tail_rcu(&worker->all_list, &wqe->all_list);
588 worker->flags |= IO_WORKER_F_FREE;
589 if (index == IO_WQ_ACCT_BOUND)
590 worker->flags |= IO_WORKER_F_BOUND;
591 if (!acct->nr_workers && (worker->flags & IO_WORKER_F_BOUND))
592 worker->flags |= IO_WORKER_F_FIXED;
594 raw_spin_unlock_irq(&wqe->lock);
600 static int task_thread_bound(void *data)
602 return task_thread(data, IO_WQ_ACCT_BOUND);
605 static int task_thread_unbound(void *data)
607 return task_thread(data, IO_WQ_ACCT_UNBOUND);
610 pid_t io_wq_fork_thread(int (*fn)(void *), void *arg)
612 unsigned long flags = CLONE_FS|CLONE_FILES|CLONE_SIGHAND|CLONE_THREAD|
614 struct kernel_clone_args args = {
615 .flags = ((lower_32_bits(flags) | CLONE_VM |
616 CLONE_UNTRACED) & ~CSIGNAL),
617 .exit_signal = (lower_32_bits(flags) & CSIGNAL),
618 .stack = (unsigned long)fn,
619 .stack_size = (unsigned long)arg,
622 return kernel_clone(&args);
625 static bool create_io_worker(struct io_wq *wq, struct io_wqe *wqe, int index)
627 struct io_worker *worker;
630 __set_current_state(TASK_RUNNING);
632 worker = kzalloc_node(sizeof(*worker), GFP_KERNEL, wqe->node);
636 refcount_set(&worker->ref, 1);
637 worker->nulls_node.pprev = NULL;
639 spin_lock_init(&worker->lock);
640 init_completion(&worker->ref_done);
641 init_completion(&worker->started);
643 atomic_inc(&wq->worker_refs);
645 if (index == IO_WQ_ACCT_BOUND)
646 pid = io_wq_fork_thread(task_thread_bound, worker);
648 pid = io_wq_fork_thread(task_thread_unbound, worker);
650 if (atomic_dec_and_test(&wq->worker_refs))
651 complete(&wq->worker_done);
655 wait_for_completion(&worker->started);
659 static inline bool io_wqe_need_worker(struct io_wqe *wqe, int index)
660 __must_hold(wqe->lock)
662 struct io_wqe_acct *acct = &wqe->acct[index];
664 if (acct->nr_workers && test_bit(IO_WQ_BIT_EXIT, &wqe->wq->state))
666 /* if we have available workers or no work, no need */
667 if (!hlist_nulls_empty(&wqe->free_list) || !io_wqe_run_queue(wqe))
669 return acct->nr_workers < acct->max_workers;
673 * Iterate the passed in list and call the specific function for each
674 * worker that isn't exiting
676 static bool io_wq_for_each_worker(struct io_wqe *wqe,
677 bool (*func)(struct io_worker *, void *),
680 struct io_worker *worker;
683 list_for_each_entry_rcu(worker, &wqe->all_list, all_list) {
684 if (io_worker_get(worker)) {
685 /* no task if node is/was offline */
687 ret = func(worker, data);
688 io_worker_release(worker);
697 static bool io_wq_worker_wake(struct io_worker *worker, void *data)
699 wake_up_process(worker->task);
703 static void io_wq_check_workers(struct io_wq *wq)
707 for_each_node(node) {
708 struct io_wqe *wqe = wq->wqes[node];
709 bool fork_worker[2] = { false, false };
711 if (!node_online(node))
714 raw_spin_lock_irq(&wqe->lock);
715 if (io_wqe_need_worker(wqe, IO_WQ_ACCT_BOUND))
716 fork_worker[IO_WQ_ACCT_BOUND] = true;
717 if (io_wqe_need_worker(wqe, IO_WQ_ACCT_UNBOUND))
718 fork_worker[IO_WQ_ACCT_UNBOUND] = true;
719 raw_spin_unlock_irq(&wqe->lock);
720 if (fork_worker[IO_WQ_ACCT_BOUND])
721 create_io_worker(wq, wqe, IO_WQ_ACCT_BOUND);
722 if (fork_worker[IO_WQ_ACCT_UNBOUND])
723 create_io_worker(wq, wqe, IO_WQ_ACCT_UNBOUND);
727 static bool io_wq_work_match_all(struct io_wq_work *work, void *data)
732 static void io_wq_cancel_pending(struct io_wq *wq)
734 struct io_cb_cancel_data match = {
735 .fn = io_wq_work_match_all,
741 io_wqe_cancel_pending_work(wq->wqes[node], &match);
745 * Manager thread. Tasked with creating new workers, if we need them.
747 static int io_wq_manager(void *data)
749 struct io_wq *wq = data;
750 char buf[TASK_COMM_LEN];
753 sprintf(buf, "iou-mgr-%d", wq->task_pid);
754 set_task_comm(current, buf);
755 current->flags |= PF_IO_WORKER;
756 wq->manager = get_task_struct(current);
758 complete(&wq->started);
761 set_current_state(TASK_INTERRUPTIBLE);
762 io_wq_check_workers(wq);
763 schedule_timeout(HZ);
765 if (fatal_signal_pending(current))
766 set_bit(IO_WQ_BIT_EXIT, &wq->state);
767 } while (!test_bit(IO_WQ_BIT_EXIT, &wq->state));
769 io_wq_check_workers(wq);
773 io_wq_for_each_worker(wq->wqes[node], io_wq_worker_wake, NULL);
776 /* we might not ever have created any workers */
777 if (atomic_read(&wq->worker_refs))
778 wait_for_completion(&wq->worker_done);
780 io_wq_cancel_pending(wq);
781 complete(&wq->exited);
785 static void io_run_cancel(struct io_wq_work *work, struct io_wqe *wqe)
787 struct io_wq *wq = wqe->wq;
790 work->flags |= IO_WQ_WORK_CANCEL;
792 work = wq->free_work(work);
796 static void io_wqe_insert_work(struct io_wqe *wqe, struct io_wq_work *work)
799 struct io_wq_work *tail;
801 if (!io_wq_is_hashed(work)) {
803 wq_list_add_tail(&work->list, &wqe->work_list);
807 hash = io_get_work_hash(work);
808 tail = wqe->hash_tail[hash];
809 wqe->hash_tail[hash] = work;
813 wq_list_add_after(&work->list, &tail->list, &wqe->work_list);
816 static int io_wq_fork_manager(struct io_wq *wq)
823 reinit_completion(&wq->worker_done);
824 current->flags |= PF_IO_WORKER;
825 ret = io_wq_fork_thread(io_wq_manager, wq);
826 current->flags &= ~PF_IO_WORKER;
828 wait_for_completion(&wq->started);
835 static void io_wqe_enqueue(struct io_wqe *wqe, struct io_wq_work *work)
837 struct io_wqe_acct *acct = io_work_get_acct(wqe, work);
841 /* Can only happen if manager creation fails after exec */
842 if (io_wq_fork_manager(wqe->wq) ||
843 test_bit(IO_WQ_BIT_EXIT, &wqe->wq->state)) {
844 work->flags |= IO_WQ_WORK_CANCEL;
845 wqe->wq->do_work(work);
849 work_flags = work->flags;
850 raw_spin_lock_irqsave(&wqe->lock, flags);
851 io_wqe_insert_work(wqe, work);
852 wqe->flags &= ~IO_WQE_FLAG_STALLED;
853 raw_spin_unlock_irqrestore(&wqe->lock, flags);
855 if ((work_flags & IO_WQ_WORK_CONCURRENT) ||
856 !atomic_read(&acct->nr_running))
857 io_wqe_wake_worker(wqe, acct);
860 void io_wq_enqueue(struct io_wq *wq, struct io_wq_work *work)
862 struct io_wqe *wqe = wq->wqes[numa_node_id()];
864 io_wqe_enqueue(wqe, work);
868 * Work items that hash to the same value will not be done in parallel.
869 * Used to limit concurrent writes, generally hashed by inode.
871 void io_wq_hash_work(struct io_wq_work *work, void *val)
875 bit = hash_ptr(val, IO_WQ_HASH_ORDER);
876 work->flags |= (IO_WQ_WORK_HASHED | (bit << IO_WQ_HASH_SHIFT));
879 static bool io_wq_worker_cancel(struct io_worker *worker, void *data)
881 struct io_cb_cancel_data *match = data;
885 * Hold the lock to avoid ->cur_work going out of scope, caller
886 * may dereference the passed in work.
888 spin_lock_irqsave(&worker->lock, flags);
889 if (worker->cur_work &&
890 match->fn(worker->cur_work, match->data)) {
891 set_notify_signal(worker->task);
894 spin_unlock_irqrestore(&worker->lock, flags);
896 return match->nr_running && !match->cancel_all;
899 static inline void io_wqe_remove_pending(struct io_wqe *wqe,
900 struct io_wq_work *work,
901 struct io_wq_work_node *prev)
903 unsigned int hash = io_get_work_hash(work);
904 struct io_wq_work *prev_work = NULL;
906 if (io_wq_is_hashed(work) && work == wqe->hash_tail[hash]) {
908 prev_work = container_of(prev, struct io_wq_work, list);
909 if (prev_work && io_get_work_hash(prev_work) == hash)
910 wqe->hash_tail[hash] = prev_work;
912 wqe->hash_tail[hash] = NULL;
914 wq_list_del(&wqe->work_list, &work->list, prev);
917 static void io_wqe_cancel_pending_work(struct io_wqe *wqe,
918 struct io_cb_cancel_data *match)
920 struct io_wq_work_node *node, *prev;
921 struct io_wq_work *work;
925 raw_spin_lock_irqsave(&wqe->lock, flags);
926 wq_list_for_each(node, prev, &wqe->work_list) {
927 work = container_of(node, struct io_wq_work, list);
928 if (!match->fn(work, match->data))
930 io_wqe_remove_pending(wqe, work, prev);
931 raw_spin_unlock_irqrestore(&wqe->lock, flags);
932 io_run_cancel(work, wqe);
934 if (!match->cancel_all)
937 /* not safe to continue after unlock */
940 raw_spin_unlock_irqrestore(&wqe->lock, flags);
943 static void io_wqe_cancel_running_work(struct io_wqe *wqe,
944 struct io_cb_cancel_data *match)
947 io_wq_for_each_worker(wqe, io_wq_worker_cancel, match);
951 enum io_wq_cancel io_wq_cancel_cb(struct io_wq *wq, work_cancel_fn *cancel,
952 void *data, bool cancel_all)
954 struct io_cb_cancel_data match = {
957 .cancel_all = cancel_all,
962 * First check pending list, if we're lucky we can just remove it
963 * from there. CANCEL_OK means that the work is returned as-new,
964 * no completion will be posted for it.
966 for_each_node(node) {
967 struct io_wqe *wqe = wq->wqes[node];
969 io_wqe_cancel_pending_work(wqe, &match);
970 if (match.nr_pending && !match.cancel_all)
971 return IO_WQ_CANCEL_OK;
975 * Now check if a free (going busy) or busy worker has the work
976 * currently running. If we find it there, we'll return CANCEL_RUNNING
977 * as an indication that we attempt to signal cancellation. The
978 * completion will run normally in this case.
980 for_each_node(node) {
981 struct io_wqe *wqe = wq->wqes[node];
983 io_wqe_cancel_running_work(wqe, &match);
984 if (match.nr_running && !match.cancel_all)
985 return IO_WQ_CANCEL_RUNNING;
988 if (match.nr_running)
989 return IO_WQ_CANCEL_RUNNING;
990 if (match.nr_pending)
991 return IO_WQ_CANCEL_OK;
992 return IO_WQ_CANCEL_NOTFOUND;
995 static int io_wqe_hash_wake(struct wait_queue_entry *wait, unsigned mode,
998 struct io_wqe *wqe = container_of(wait, struct io_wqe, wait);
1001 list_del_init(&wait->entry);
1004 ret = io_wqe_activate_free_worker(wqe);
1008 wake_up_process(wqe->wq->manager);
1013 struct io_wq *io_wq_create(unsigned bounded, struct io_wq_data *data)
1015 int ret = -ENOMEM, node;
1018 if (WARN_ON_ONCE(!data->free_work || !data->do_work))
1019 return ERR_PTR(-EINVAL);
1021 wq = kzalloc(sizeof(*wq), GFP_KERNEL);
1023 return ERR_PTR(-ENOMEM);
1025 wq->wqes = kcalloc(nr_node_ids, sizeof(struct io_wqe *), GFP_KERNEL);
1029 ret = cpuhp_state_add_instance_nocalls(io_wq_online, &wq->cpuhp_node);
1033 refcount_inc(&data->hash->refs);
1034 wq->hash = data->hash;
1035 wq->free_work = data->free_work;
1036 wq->do_work = data->do_work;
1039 for_each_node(node) {
1041 int alloc_node = node;
1043 if (!node_online(alloc_node))
1044 alloc_node = NUMA_NO_NODE;
1045 wqe = kzalloc_node(sizeof(struct io_wqe), GFP_KERNEL, alloc_node);
1048 wq->wqes[node] = wqe;
1049 wqe->node = alloc_node;
1050 wqe->acct[IO_WQ_ACCT_BOUND].max_workers = bounded;
1051 atomic_set(&wqe->acct[IO_WQ_ACCT_BOUND].nr_running, 0);
1052 wqe->acct[IO_WQ_ACCT_UNBOUND].max_workers =
1053 task_rlimit(current, RLIMIT_NPROC);
1054 atomic_set(&wqe->acct[IO_WQ_ACCT_UNBOUND].nr_running, 0);
1055 wqe->wait.func = io_wqe_hash_wake;
1056 INIT_LIST_HEAD(&wqe->wait.entry);
1058 raw_spin_lock_init(&wqe->lock);
1059 INIT_WQ_LIST(&wqe->work_list);
1060 INIT_HLIST_NULLS_HEAD(&wqe->free_list, 0);
1061 INIT_LIST_HEAD(&wqe->all_list);
1064 wq->task_pid = current->pid;
1065 init_completion(&wq->started);
1066 init_completion(&wq->exited);
1067 refcount_set(&wq->refs, 1);
1069 init_completion(&wq->worker_done);
1070 atomic_set(&wq->worker_refs, 0);
1072 ret = io_wq_fork_manager(wq);
1077 io_wq_put_hash(data->hash);
1078 cpuhp_state_remove_instance_nocalls(io_wq_online, &wq->cpuhp_node);
1080 kfree(wq->wqes[node]);
1085 return ERR_PTR(ret);
1088 static void io_wq_destroy_manager(struct io_wq *wq)
1091 wake_up_process(wq->manager);
1092 wait_for_completion(&wq->exited);
1093 put_task_struct(wq->manager);
1098 static void io_wq_destroy(struct io_wq *wq)
1102 cpuhp_state_remove_instance_nocalls(io_wq_online, &wq->cpuhp_node);
1104 set_bit(IO_WQ_BIT_EXIT, &wq->state);
1105 io_wq_destroy_manager(wq);
1107 spin_lock_irq(&wq->hash->wait.lock);
1108 for_each_node(node) {
1109 struct io_wqe *wqe = wq->wqes[node];
1111 list_del_init(&wqe->wait.entry);
1112 WARN_ON_ONCE(!wq_list_empty(&wqe->work_list));
1115 spin_unlock_irq(&wq->hash->wait.lock);
1116 io_wq_put_hash(wq->hash);
1121 void io_wq_put(struct io_wq *wq)
1123 if (refcount_dec_and_test(&wq->refs))
1127 void io_wq_put_and_exit(struct io_wq *wq)
1129 set_bit(IO_WQ_BIT_EXIT, &wq->state);
1130 io_wq_destroy_manager(wq);
1134 static bool io_wq_worker_affinity(struct io_worker *worker, void *data)
1136 struct task_struct *task = worker->task;
1140 rq = task_rq_lock(task, &rf);
1141 do_set_cpus_allowed(task, cpumask_of_node(worker->wqe->node));
1142 task->flags |= PF_NO_SETAFFINITY;
1143 task_rq_unlock(rq, task, &rf);
1147 static int io_wq_cpu_online(unsigned int cpu, struct hlist_node *node)
1149 struct io_wq *wq = hlist_entry_safe(node, struct io_wq, cpuhp_node);
1154 io_wq_for_each_worker(wq->wqes[i], io_wq_worker_affinity, NULL);
1159 static __init int io_wq_init(void)
1163 ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "io-wq/online",
1164 io_wq_cpu_online, NULL);
1170 subsys_initcall(io_wq_init);