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 const struct cred *cur_creds;
56 const struct cred *saved_creds;
58 struct completion ref_done;
63 #if BITS_PER_LONG == 64
64 #define IO_WQ_HASH_ORDER 6
66 #define IO_WQ_HASH_ORDER 5
69 #define IO_WQ_NR_HASH_BUCKETS (1u << IO_WQ_HASH_ORDER)
83 * Per-node worker thread pool
88 struct io_wq_work_list work_list;
90 } ____cacheline_aligned_in_smp;
93 struct io_wqe_acct acct[2];
95 struct hlist_nulls_head free_list;
96 struct list_head all_list;
98 struct wait_queue_entry wait;
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 io_wq_hash *hash;
120 struct completion done;
122 struct hlist_node cpuhp_node;
127 static enum cpuhp_state io_wq_online;
129 static bool io_worker_get(struct io_worker *worker)
131 return refcount_inc_not_zero(&worker->ref);
134 static void io_worker_release(struct io_worker *worker)
136 if (refcount_dec_and_test(&worker->ref))
137 complete(&worker->ref_done);
140 static inline struct io_wqe_acct *io_work_get_acct(struct io_wqe *wqe,
141 struct io_wq_work *work)
143 if (work->flags & IO_WQ_WORK_UNBOUND)
144 return &wqe->acct[IO_WQ_ACCT_UNBOUND];
146 return &wqe->acct[IO_WQ_ACCT_BOUND];
149 static inline struct io_wqe_acct *io_wqe_get_acct(struct io_worker *worker)
151 struct io_wqe *wqe = worker->wqe;
153 if (worker->flags & IO_WORKER_F_BOUND)
154 return &wqe->acct[IO_WQ_ACCT_BOUND];
156 return &wqe->acct[IO_WQ_ACCT_UNBOUND];
159 static void io_worker_exit(struct io_worker *worker)
161 struct io_wqe *wqe = worker->wqe;
162 struct io_wqe_acct *acct = io_wqe_get_acct(worker);
165 if (refcount_dec_and_test(&worker->ref))
166 complete(&worker->ref_done);
167 wait_for_completion(&worker->ref_done);
170 current->flags &= ~PF_IO_WORKER;
171 flags = worker->flags;
173 if (flags & IO_WORKER_F_RUNNING)
174 atomic_dec(&acct->nr_running);
178 if (worker->saved_creds) {
179 revert_creds(worker->saved_creds);
180 worker->cur_creds = worker->saved_creds = NULL;
183 raw_spin_lock_irq(&wqe->lock);
184 if (flags & IO_WORKER_F_FREE)
185 hlist_nulls_del_rcu(&worker->nulls_node);
186 list_del_rcu(&worker->all_list);
188 raw_spin_unlock_irq(&wqe->lock);
190 kfree_rcu(worker, rcu);
194 static inline bool io_wqe_run_queue(struct io_wqe *wqe)
195 __must_hold(wqe->lock)
197 if (!wq_list_empty(&wqe->work_list) &&
198 !(wqe->flags & IO_WQE_FLAG_STALLED))
204 * Check head of free list for an available worker. If one isn't available,
205 * caller must wake up the wq manager to create one.
207 static bool io_wqe_activate_free_worker(struct io_wqe *wqe)
210 struct hlist_nulls_node *n;
211 struct io_worker *worker;
213 n = rcu_dereference(hlist_nulls_first_rcu(&wqe->free_list));
217 worker = hlist_nulls_entry(n, struct io_worker, nulls_node);
218 if (io_worker_get(worker)) {
219 wake_up_process(worker->task);
220 io_worker_release(worker);
228 * We need a worker. If we find a free one, we're good. If not, and we're
229 * below the max number of workers, wake up the manager to create one.
231 static void io_wqe_wake_worker(struct io_wqe *wqe, struct io_wqe_acct *acct)
236 * Most likely an attempt to queue unbounded work on an io_wq that
237 * wasn't setup with any unbounded workers.
239 WARN_ON_ONCE(!acct->max_workers);
242 ret = io_wqe_activate_free_worker(wqe);
245 if (!ret && acct->nr_workers < acct->max_workers)
246 wake_up_process(wqe->wq->manager);
249 static void io_wqe_inc_running(struct io_worker *worker)
251 struct io_wqe_acct *acct = io_wqe_get_acct(worker);
253 atomic_inc(&acct->nr_running);
256 static void io_wqe_dec_running(struct io_worker *worker)
257 __must_hold(wqe->lock)
259 struct io_wqe_acct *acct = io_wqe_get_acct(worker);
260 struct io_wqe *wqe = worker->wqe;
262 if (atomic_dec_and_test(&acct->nr_running) && io_wqe_run_queue(wqe))
263 io_wqe_wake_worker(wqe, acct);
266 static void io_worker_start(struct io_worker *worker)
268 worker->flags |= (IO_WORKER_F_UP | IO_WORKER_F_RUNNING);
269 io_wqe_inc_running(worker);
273 * Worker will start processing some work. Move it to the busy list, if
274 * it's currently on the freelist
276 static void __io_worker_busy(struct io_wqe *wqe, struct io_worker *worker,
277 struct io_wq_work *work)
278 __must_hold(wqe->lock)
280 bool worker_bound, work_bound;
282 if (worker->flags & IO_WORKER_F_FREE) {
283 worker->flags &= ~IO_WORKER_F_FREE;
284 hlist_nulls_del_init_rcu(&worker->nulls_node);
288 * If worker is moving from bound to unbound (or vice versa), then
289 * ensure we update the running accounting.
291 worker_bound = (worker->flags & IO_WORKER_F_BOUND) != 0;
292 work_bound = (work->flags & IO_WQ_WORK_UNBOUND) == 0;
293 if (worker_bound != work_bound) {
294 io_wqe_dec_running(worker);
296 worker->flags |= IO_WORKER_F_BOUND;
297 wqe->acct[IO_WQ_ACCT_UNBOUND].nr_workers--;
298 wqe->acct[IO_WQ_ACCT_BOUND].nr_workers++;
300 worker->flags &= ~IO_WORKER_F_BOUND;
301 wqe->acct[IO_WQ_ACCT_UNBOUND].nr_workers++;
302 wqe->acct[IO_WQ_ACCT_BOUND].nr_workers--;
304 io_wqe_inc_running(worker);
309 * No work, worker going to sleep. Move to freelist, and unuse mm if we
310 * have one attached. Dropping the mm may potentially sleep, so we drop
311 * the lock in that case and return success. Since the caller has to
312 * retry the loop in that case (we changed task state), we don't regrab
313 * the lock if we return success.
315 static void __io_worker_idle(struct io_wqe *wqe, struct io_worker *worker)
316 __must_hold(wqe->lock)
318 if (!(worker->flags & IO_WORKER_F_FREE)) {
319 worker->flags |= IO_WORKER_F_FREE;
320 hlist_nulls_add_head_rcu(&worker->nulls_node, &wqe->free_list);
322 if (worker->saved_creds) {
323 revert_creds(worker->saved_creds);
324 worker->cur_creds = worker->saved_creds = NULL;
328 static inline unsigned int io_get_work_hash(struct io_wq_work *work)
330 return work->flags >> IO_WQ_HASH_SHIFT;
333 static void io_wait_on_hash(struct io_wqe *wqe, unsigned int hash)
335 struct io_wq *wq = wqe->wq;
337 spin_lock(&wq->hash->wait.lock);
338 if (list_empty(&wqe->wait.entry)) {
339 __add_wait_queue(&wq->hash->wait, &wqe->wait);
340 if (!test_bit(hash, &wq->hash->map)) {
341 __set_current_state(TASK_RUNNING);
342 list_del_init(&wqe->wait.entry);
345 spin_unlock(&wq->hash->wait.lock);
348 static struct io_wq_work *io_get_next_work(struct io_wqe *wqe)
349 __must_hold(wqe->lock)
351 struct io_wq_work_node *node, *prev;
352 struct io_wq_work *work, *tail;
353 unsigned int stall_hash = -1U;
355 wq_list_for_each(node, prev, &wqe->work_list) {
358 work = container_of(node, struct io_wq_work, list);
360 /* not hashed, can run anytime */
361 if (!io_wq_is_hashed(work)) {
362 wq_list_del(&wqe->work_list, node, prev);
366 hash = io_get_work_hash(work);
367 /* all items with this hash lie in [work, tail] */
368 tail = wqe->hash_tail[hash];
370 /* hashed, can run if not already running */
371 if (!test_and_set_bit(hash, &wqe->wq->hash->map)) {
372 wqe->hash_tail[hash] = NULL;
373 wq_list_cut(&wqe->work_list, &tail->list, prev);
376 if (stall_hash == -1U)
378 /* fast forward to a next hash, for-each will fix up @prev */
382 if (stall_hash != -1U) {
383 raw_spin_unlock(&wqe->lock);
384 io_wait_on_hash(wqe, stall_hash);
385 raw_spin_lock(&wqe->lock);
391 static void io_flush_signals(void)
393 if (unlikely(test_tsk_thread_flag(current, TIF_NOTIFY_SIGNAL))) {
394 if (current->task_works)
396 clear_tsk_thread_flag(current, TIF_NOTIFY_SIGNAL);
400 static void io_wq_switch_creds(struct io_worker *worker,
401 struct io_wq_work *work)
403 const struct cred *old_creds = override_creds(work->creds);
405 worker->cur_creds = work->creds;
406 if (worker->saved_creds)
407 put_cred(old_creds); /* creds set by previous switch */
409 worker->saved_creds = old_creds;
412 static void io_assign_current_work(struct io_worker *worker,
413 struct io_wq_work *work)
420 spin_lock_irq(&worker->lock);
421 worker->cur_work = work;
422 spin_unlock_irq(&worker->lock);
425 static void io_wqe_enqueue(struct io_wqe *wqe, struct io_wq_work *work);
427 static void io_worker_handle_work(struct io_worker *worker)
428 __releases(wqe->lock)
430 struct io_wqe *wqe = worker->wqe;
431 struct io_wq *wq = wqe->wq;
434 struct io_wq_work *work;
437 * If we got some work, mark us as busy. If we didn't, but
438 * the list isn't empty, it means we stalled on hashed work.
439 * Mark us stalled so we don't keep looking for work when we
440 * can't make progress, any work completion or insertion will
441 * clear the stalled flag.
443 work = io_get_next_work(wqe);
445 __io_worker_busy(wqe, worker, work);
446 else if (!wq_list_empty(&wqe->work_list))
447 wqe->flags |= IO_WQE_FLAG_STALLED;
449 raw_spin_unlock_irq(&wqe->lock);
452 io_assign_current_work(worker, work);
453 __set_current_state(TASK_RUNNING);
455 /* handle a whole dependent link */
457 struct io_wq_work *next_hashed, *linked;
458 unsigned int hash = io_get_work_hash(work);
460 next_hashed = wq_next_work(work);
461 if (work->creds && worker->cur_creds != work->creds)
462 io_wq_switch_creds(worker, work);
464 io_assign_current_work(worker, NULL);
466 linked = wq->free_work(work);
468 if (!work && linked && !io_wq_is_hashed(linked)) {
472 io_assign_current_work(worker, work);
474 io_wqe_enqueue(wqe, linked);
476 if (hash != -1U && !next_hashed) {
477 clear_bit(hash, &wq->hash->map);
478 if (wq_has_sleeper(&wq->hash->wait))
479 wake_up(&wq->hash->wait);
480 raw_spin_lock_irq(&wqe->lock);
481 wqe->flags &= ~IO_WQE_FLAG_STALLED;
482 /* skip unnecessary unlock-lock wqe->lock */
485 raw_spin_unlock_irq(&wqe->lock);
489 raw_spin_lock_irq(&wqe->lock);
493 static int io_wqe_worker(void *data)
495 struct io_worker *worker = data;
496 struct io_wqe *wqe = worker->wqe;
497 struct io_wq *wq = wqe->wq;
499 io_worker_start(worker);
501 while (!test_bit(IO_WQ_BIT_EXIT, &wq->state)) {
502 set_current_state(TASK_INTERRUPTIBLE);
504 raw_spin_lock_irq(&wqe->lock);
505 if (io_wqe_run_queue(wqe)) {
506 io_worker_handle_work(worker);
509 __io_worker_idle(wqe, worker);
510 raw_spin_unlock_irq(&wqe->lock);
512 if (schedule_timeout(WORKER_IDLE_TIMEOUT))
514 if (fatal_signal_pending(current))
516 /* timed out, exit unless we're the fixed worker */
517 if (test_bit(IO_WQ_BIT_EXIT, &wq->state) ||
518 !(worker->flags & IO_WORKER_F_FIXED))
522 if (test_bit(IO_WQ_BIT_EXIT, &wq->state)) {
523 raw_spin_lock_irq(&wqe->lock);
524 if (!wq_list_empty(&wqe->work_list))
525 io_worker_handle_work(worker);
527 raw_spin_unlock_irq(&wqe->lock);
530 io_worker_exit(worker);
535 * Called when a worker is scheduled in. Mark us as currently running.
537 void io_wq_worker_running(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)
547 worker->flags |= IO_WORKER_F_RUNNING;
548 io_wqe_inc_running(worker);
552 * Called when worker is going to sleep. If there are no workers currently
553 * running and we have work pending, wake up a free one or have the manager
556 void io_wq_worker_sleeping(struct task_struct *tsk)
558 struct io_worker *worker = tsk->pf_io_worker;
562 if (!(worker->flags & IO_WORKER_F_UP))
564 if (!(worker->flags & IO_WORKER_F_RUNNING))
567 worker->flags &= ~IO_WORKER_F_RUNNING;
569 raw_spin_lock_irq(&worker->wqe->lock);
570 io_wqe_dec_running(worker);
571 raw_spin_unlock_irq(&worker->wqe->lock);
574 static int task_thread(void *data, int index)
576 struct io_worker *worker = data;
577 struct io_wqe *wqe = worker->wqe;
578 struct io_wqe_acct *acct = &wqe->acct[index];
579 struct io_wq *wq = wqe->wq;
580 char buf[TASK_COMM_LEN];
582 sprintf(buf, "iou-wrk-%d", wq->task_pid);
583 set_task_comm(current, buf);
585 current->pf_io_worker = worker;
586 worker->task = current;
588 set_cpus_allowed_ptr(current, cpumask_of_node(wqe->node));
589 current->flags |= PF_NO_SETAFFINITY;
591 raw_spin_lock_irq(&wqe->lock);
592 hlist_nulls_add_head_rcu(&worker->nulls_node, &wqe->free_list);
593 list_add_tail_rcu(&worker->all_list, &wqe->all_list);
594 worker->flags |= IO_WORKER_F_FREE;
595 if (index == IO_WQ_ACCT_BOUND)
596 worker->flags |= IO_WORKER_F_BOUND;
597 if (!acct->nr_workers && (worker->flags & IO_WORKER_F_BOUND))
598 worker->flags |= IO_WORKER_F_FIXED;
600 raw_spin_unlock_irq(&wqe->lock);
606 static int task_thread_bound(void *data)
608 return task_thread(data, IO_WQ_ACCT_BOUND);
611 static int task_thread_unbound(void *data)
613 return task_thread(data, IO_WQ_ACCT_UNBOUND);
616 pid_t io_wq_fork_thread(int (*fn)(void *), void *arg)
618 unsigned long flags = CLONE_FS|CLONE_FILES|CLONE_SIGHAND|CLONE_THREAD|
620 struct kernel_clone_args args = {
621 .flags = ((lower_32_bits(flags) | CLONE_VM |
622 CLONE_UNTRACED) & ~CSIGNAL),
623 .exit_signal = (lower_32_bits(flags) & CSIGNAL),
624 .stack = (unsigned long)fn,
625 .stack_size = (unsigned long)arg,
628 return kernel_clone(&args);
631 static bool create_io_worker(struct io_wq *wq, struct io_wqe *wqe, int index)
633 struct io_worker *worker;
636 __set_current_state(TASK_RUNNING);
638 worker = kzalloc_node(sizeof(*worker), GFP_KERNEL, wqe->node);
642 refcount_set(&worker->ref, 1);
643 worker->nulls_node.pprev = NULL;
645 spin_lock_init(&worker->lock);
646 init_completion(&worker->ref_done);
648 refcount_inc(&wq->refs);
650 if (index == IO_WQ_ACCT_BOUND)
651 pid = io_wq_fork_thread(task_thread_bound, worker);
653 pid = io_wq_fork_thread(task_thread_unbound, worker);
662 static inline bool io_wqe_need_worker(struct io_wqe *wqe, int index)
663 __must_hold(wqe->lock)
665 struct io_wqe_acct *acct = &wqe->acct[index];
667 /* if we have available workers or no work, no need */
668 if (!hlist_nulls_empty(&wqe->free_list) || !io_wqe_run_queue(wqe))
670 return acct->nr_workers < acct->max_workers;
674 * Iterate the passed in list and call the specific function for each
675 * worker that isn't exiting
677 static bool io_wq_for_each_worker(struct io_wqe *wqe,
678 bool (*func)(struct io_worker *, void *),
681 struct io_worker *worker;
684 list_for_each_entry_rcu(worker, &wqe->all_list, all_list) {
685 if (io_worker_get(worker)) {
686 /* no task if node is/was offline */
688 ret = func(worker, data);
689 io_worker_release(worker);
698 static bool io_wq_worker_wake(struct io_worker *worker, void *data)
700 wake_up_process(worker->task);
704 static void io_wq_check_workers(struct io_wq *wq)
708 for_each_node(node) {
709 struct io_wqe *wqe = wq->wqes[node];
710 bool fork_worker[2] = { false, false };
712 if (!node_online(node))
715 raw_spin_lock_irq(&wqe->lock);
716 if (io_wqe_need_worker(wqe, IO_WQ_ACCT_BOUND))
717 fork_worker[IO_WQ_ACCT_BOUND] = true;
718 if (io_wqe_need_worker(wqe, IO_WQ_ACCT_UNBOUND))
719 fork_worker[IO_WQ_ACCT_UNBOUND] = true;
720 raw_spin_unlock_irq(&wqe->lock);
721 if (fork_worker[IO_WQ_ACCT_BOUND])
722 create_io_worker(wq, wqe, IO_WQ_ACCT_BOUND);
723 if (fork_worker[IO_WQ_ACCT_UNBOUND])
724 create_io_worker(wq, wqe, IO_WQ_ACCT_UNBOUND);
729 * Manager thread. Tasked with creating new workers, if we need them.
731 static int io_wq_manager(void *data)
733 struct io_wq *wq = data;
734 char buf[TASK_COMM_LEN];
736 sprintf(buf, "iou-mgr-%d", wq->task_pid);
737 set_task_comm(current, buf);
738 current->flags |= PF_IO_WORKER;
739 wq->manager = current;
744 set_current_state(TASK_INTERRUPTIBLE);
745 io_wq_check_workers(wq);
746 schedule_timeout(HZ);
747 if (fatal_signal_pending(current))
748 set_bit(IO_WQ_BIT_EXIT, &wq->state);
749 } while (!test_bit(IO_WQ_BIT_EXIT, &wq->state));
751 io_wq_check_workers(wq);
757 static void io_run_cancel(struct io_wq_work *work, struct io_wqe *wqe)
759 struct io_wq *wq = wqe->wq;
762 work->flags |= IO_WQ_WORK_CANCEL;
764 work = wq->free_work(work);
768 static void io_wqe_insert_work(struct io_wqe *wqe, struct io_wq_work *work)
771 struct io_wq_work *tail;
773 if (!io_wq_is_hashed(work)) {
775 wq_list_add_tail(&work->list, &wqe->work_list);
779 hash = io_get_work_hash(work);
780 tail = wqe->hash_tail[hash];
781 wqe->hash_tail[hash] = work;
785 wq_list_add_after(&work->list, &tail->list, &wqe->work_list);
788 static int io_wq_fork_manager(struct io_wq *wq)
795 clear_bit(IO_WQ_BIT_EXIT, &wq->state);
796 refcount_inc(&wq->refs);
797 current->flags |= PF_IO_WORKER;
798 ret = io_wq_fork_thread(io_wq_manager, wq);
799 current->flags &= ~PF_IO_WORKER;
801 wait_for_completion(&wq->done);
809 static void io_wqe_enqueue(struct io_wqe *wqe, struct io_wq_work *work)
811 struct io_wqe_acct *acct = io_work_get_acct(wqe, work);
815 /* Can only happen if manager creation fails after exec */
816 if (unlikely(io_wq_fork_manager(wqe->wq))) {
817 work->flags |= IO_WQ_WORK_CANCEL;
818 wqe->wq->do_work(work);
822 work_flags = work->flags;
823 raw_spin_lock_irqsave(&wqe->lock, flags);
824 io_wqe_insert_work(wqe, work);
825 wqe->flags &= ~IO_WQE_FLAG_STALLED;
826 raw_spin_unlock_irqrestore(&wqe->lock, flags);
828 if ((work_flags & IO_WQ_WORK_CONCURRENT) ||
829 !atomic_read(&acct->nr_running))
830 io_wqe_wake_worker(wqe, acct);
833 void io_wq_enqueue(struct io_wq *wq, struct io_wq_work *work)
835 struct io_wqe *wqe = wq->wqes[numa_node_id()];
837 io_wqe_enqueue(wqe, work);
841 * Work items that hash to the same value will not be done in parallel.
842 * Used to limit concurrent writes, generally hashed by inode.
844 void io_wq_hash_work(struct io_wq_work *work, void *val)
848 bit = hash_ptr(val, IO_WQ_HASH_ORDER);
849 work->flags |= (IO_WQ_WORK_HASHED | (bit << IO_WQ_HASH_SHIFT));
852 struct io_cb_cancel_data {
860 static bool io_wq_worker_cancel(struct io_worker *worker, void *data)
862 struct io_cb_cancel_data *match = data;
866 * Hold the lock to avoid ->cur_work going out of scope, caller
867 * may dereference the passed in work.
869 spin_lock_irqsave(&worker->lock, flags);
870 if (worker->cur_work &&
871 match->fn(worker->cur_work, match->data)) {
872 set_notify_signal(worker->task);
875 spin_unlock_irqrestore(&worker->lock, flags);
877 return match->nr_running && !match->cancel_all;
880 static inline void io_wqe_remove_pending(struct io_wqe *wqe,
881 struct io_wq_work *work,
882 struct io_wq_work_node *prev)
884 unsigned int hash = io_get_work_hash(work);
885 struct io_wq_work *prev_work = NULL;
887 if (io_wq_is_hashed(work) && work == wqe->hash_tail[hash]) {
889 prev_work = container_of(prev, struct io_wq_work, list);
890 if (prev_work && io_get_work_hash(prev_work) == hash)
891 wqe->hash_tail[hash] = prev_work;
893 wqe->hash_tail[hash] = NULL;
895 wq_list_del(&wqe->work_list, &work->list, prev);
898 static void io_wqe_cancel_pending_work(struct io_wqe *wqe,
899 struct io_cb_cancel_data *match)
901 struct io_wq_work_node *node, *prev;
902 struct io_wq_work *work;
906 raw_spin_lock_irqsave(&wqe->lock, flags);
907 wq_list_for_each(node, prev, &wqe->work_list) {
908 work = container_of(node, struct io_wq_work, list);
909 if (!match->fn(work, match->data))
911 io_wqe_remove_pending(wqe, work, prev);
912 raw_spin_unlock_irqrestore(&wqe->lock, flags);
913 io_run_cancel(work, wqe);
915 if (!match->cancel_all)
918 /* not safe to continue after unlock */
921 raw_spin_unlock_irqrestore(&wqe->lock, flags);
924 static void io_wqe_cancel_running_work(struct io_wqe *wqe,
925 struct io_cb_cancel_data *match)
928 io_wq_for_each_worker(wqe, io_wq_worker_cancel, match);
932 enum io_wq_cancel io_wq_cancel_cb(struct io_wq *wq, work_cancel_fn *cancel,
933 void *data, bool cancel_all)
935 struct io_cb_cancel_data match = {
938 .cancel_all = cancel_all,
943 * First check pending list, if we're lucky we can just remove it
944 * from there. CANCEL_OK means that the work is returned as-new,
945 * no completion will be posted for it.
947 for_each_node(node) {
948 struct io_wqe *wqe = wq->wqes[node];
950 io_wqe_cancel_pending_work(wqe, &match);
951 if (match.nr_pending && !match.cancel_all)
952 return IO_WQ_CANCEL_OK;
956 * Now check if a free (going busy) or busy worker has the work
957 * currently running. If we find it there, we'll return CANCEL_RUNNING
958 * as an indication that we attempt to signal cancellation. The
959 * completion will run normally in this case.
961 for_each_node(node) {
962 struct io_wqe *wqe = wq->wqes[node];
964 io_wqe_cancel_running_work(wqe, &match);
965 if (match.nr_running && !match.cancel_all)
966 return IO_WQ_CANCEL_RUNNING;
969 if (match.nr_running)
970 return IO_WQ_CANCEL_RUNNING;
971 if (match.nr_pending)
972 return IO_WQ_CANCEL_OK;
973 return IO_WQ_CANCEL_NOTFOUND;
976 static int io_wqe_hash_wake(struct wait_queue_entry *wait, unsigned mode,
979 struct io_wqe *wqe = container_of(wait, struct io_wqe, wait);
982 list_del_init(&wait->entry);
985 ret = io_wqe_activate_free_worker(wqe);
989 wake_up_process(wqe->wq->manager);
994 struct io_wq *io_wq_create(unsigned bounded, struct io_wq_data *data)
996 int ret = -ENOMEM, node;
999 if (WARN_ON_ONCE(!data->free_work || !data->do_work))
1000 return ERR_PTR(-EINVAL);
1002 wq = kzalloc(sizeof(*wq), GFP_KERNEL);
1004 return ERR_PTR(-ENOMEM);
1006 wq->wqes = kcalloc(nr_node_ids, sizeof(struct io_wqe *), GFP_KERNEL);
1010 ret = cpuhp_state_add_instance_nocalls(io_wq_online, &wq->cpuhp_node);
1014 refcount_inc(&data->hash->refs);
1015 wq->hash = data->hash;
1016 wq->free_work = data->free_work;
1017 wq->do_work = data->do_work;
1020 for_each_node(node) {
1022 int alloc_node = node;
1024 if (!node_online(alloc_node))
1025 alloc_node = NUMA_NO_NODE;
1026 wqe = kzalloc_node(sizeof(struct io_wqe), GFP_KERNEL, alloc_node);
1029 wq->wqes[node] = wqe;
1030 wqe->node = alloc_node;
1031 wqe->acct[IO_WQ_ACCT_BOUND].max_workers = bounded;
1032 atomic_set(&wqe->acct[IO_WQ_ACCT_BOUND].nr_running, 0);
1033 wqe->acct[IO_WQ_ACCT_UNBOUND].max_workers =
1034 task_rlimit(current, RLIMIT_NPROC);
1035 atomic_set(&wqe->acct[IO_WQ_ACCT_UNBOUND].nr_running, 0);
1036 wqe->wait.func = io_wqe_hash_wake;
1037 INIT_LIST_HEAD(&wqe->wait.entry);
1039 raw_spin_lock_init(&wqe->lock);
1040 INIT_WQ_LIST(&wqe->work_list);
1041 INIT_HLIST_NULLS_HEAD(&wqe->free_list, 0);
1042 INIT_LIST_HEAD(&wqe->all_list);
1045 wq->task_pid = current->pid;
1046 init_completion(&wq->done);
1047 refcount_set(&wq->refs, 1);
1049 ret = io_wq_fork_manager(wq);
1054 io_wq_put_hash(data->hash);
1056 cpuhp_state_remove_instance_nocalls(io_wq_online, &wq->cpuhp_node);
1058 kfree(wq->wqes[node]);
1063 return ERR_PTR(ret);
1066 static void io_wq_destroy(struct io_wq *wq)
1070 cpuhp_state_remove_instance_nocalls(io_wq_online, &wq->cpuhp_node);
1072 set_bit(IO_WQ_BIT_EXIT, &wq->state);
1074 wake_up_process(wq->manager);
1078 io_wq_for_each_worker(wq->wqes[node], io_wq_worker_wake, NULL);
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);
1095 void io_wq_put(struct io_wq *wq)
1097 if (refcount_dec_and_test(&wq->refs))
1101 static bool io_wq_worker_affinity(struct io_worker *worker, void *data)
1103 struct task_struct *task = worker->task;
1107 rq = task_rq_lock(task, &rf);
1108 do_set_cpus_allowed(task, cpumask_of_node(worker->wqe->node));
1109 task->flags |= PF_NO_SETAFFINITY;
1110 task_rq_unlock(rq, task, &rf);
1114 static int io_wq_cpu_online(unsigned int cpu, struct hlist_node *node)
1116 struct io_wq *wq = hlist_entry_safe(node, struct io_wq, cpuhp_node);
1121 io_wq_for_each_worker(wq->wqes[i], io_wq_worker_affinity, NULL);
1126 static __init int io_wq_init(void)
1130 ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "io-wq/online",
1131 io_wq_cpu_online, NULL);
1137 subsys_initcall(io_wq_init);