1 // SPDX-License-Identifier: GPL-2.0
3 * Basic worker thread pool for io_uring
5 * Copyright (C) 2019 Jens Axboe
8 #include <linux/kernel.h>
9 #include <linux/init.h>
10 #include <linux/errno.h>
11 #include <linux/sched/signal.h>
13 #include <linux/sched/mm.h>
14 #include <linux/percpu.h>
15 #include <linux/slab.h>
16 #include <linux/rculist_nulls.h>
17 #include <linux/cpu.h>
18 #include <linux/tracehook.h>
20 #include "../kernel/sched/sched.h"
23 #define WORKER_IDLE_TIMEOUT (5 * HZ)
26 IO_WORKER_F_UP = 1, /* up and active */
27 IO_WORKER_F_RUNNING = 2, /* account as running */
28 IO_WORKER_F_FREE = 4, /* worker on free list */
29 IO_WORKER_F_FIXED = 8, /* static idle worker */
30 IO_WORKER_F_BOUND = 16, /* is doing bounded work */
34 IO_WQ_BIT_EXIT = 0, /* wq exiting */
35 IO_WQ_BIT_ERROR = 1, /* error on setup */
39 IO_WQE_FLAG_STALLED = 1, /* stalled on hash */
43 * One for each thread in a wqe pool
48 struct hlist_nulls_node nulls_node;
49 struct list_head all_list;
50 struct task_struct *task;
53 struct io_wq_work *cur_work;
56 const struct cred *cur_creds;
57 const struct cred *saved_creds;
59 struct completion ref_done;
64 #if BITS_PER_LONG == 64
65 #define IO_WQ_HASH_ORDER 6
67 #define IO_WQ_HASH_ORDER 5
70 #define IO_WQ_NR_HASH_BUCKETS (1u << IO_WQ_HASH_ORDER)
84 * Per-node worker thread pool
89 struct io_wq_work_list work_list;
91 } ____cacheline_aligned_in_smp;
94 struct io_wqe_acct acct[2];
96 struct hlist_nulls_head free_list;
97 struct list_head all_list;
99 struct wait_queue_entry wait;
102 struct io_wq_work *hash_tail[IO_WQ_NR_HASH_BUCKETS];
109 struct io_wqe **wqes;
112 free_work_fn *free_work;
113 io_wq_work_fn *do_work;
115 struct task_struct *manager;
116 struct user_struct *user;
118 struct io_wq_hash *hash;
121 struct completion done;
123 struct hlist_node cpuhp_node;
128 static enum cpuhp_state io_wq_online;
130 static bool io_worker_get(struct io_worker *worker)
132 return refcount_inc_not_zero(&worker->ref);
135 static void io_worker_release(struct io_worker *worker)
137 if (refcount_dec_and_test(&worker->ref))
138 complete(&worker->ref_done);
141 static inline struct io_wqe_acct *io_work_get_acct(struct io_wqe *wqe,
142 struct io_wq_work *work)
144 if (work->flags & IO_WQ_WORK_UNBOUND)
145 return &wqe->acct[IO_WQ_ACCT_UNBOUND];
147 return &wqe->acct[IO_WQ_ACCT_BOUND];
150 static inline struct io_wqe_acct *io_wqe_get_acct(struct io_worker *worker)
152 struct io_wqe *wqe = worker->wqe;
154 if (worker->flags & IO_WORKER_F_BOUND)
155 return &wqe->acct[IO_WQ_ACCT_BOUND];
157 return &wqe->acct[IO_WQ_ACCT_UNBOUND];
160 static void io_worker_exit(struct io_worker *worker)
162 struct io_wqe *wqe = worker->wqe;
163 struct io_wqe_acct *acct = io_wqe_get_acct(worker);
166 if (refcount_dec_and_test(&worker->ref))
167 complete(&worker->ref_done);
168 wait_for_completion(&worker->ref_done);
171 current->flags &= ~PF_IO_WORKER;
172 flags = worker->flags;
174 if (flags & IO_WORKER_F_RUNNING)
175 atomic_dec(&acct->nr_running);
179 if (worker->saved_creds) {
180 revert_creds(worker->saved_creds);
181 worker->cur_creds = worker->saved_creds = NULL;
184 raw_spin_lock_irq(&wqe->lock);
185 if (flags & IO_WORKER_F_FREE)
186 hlist_nulls_del_rcu(&worker->nulls_node);
187 list_del_rcu(&worker->all_list);
189 raw_spin_unlock_irq(&wqe->lock);
191 kfree_rcu(worker, rcu);
192 if (refcount_dec_and_test(&wqe->wq->refs))
193 complete(&wqe->wq->done);
196 static inline bool io_wqe_run_queue(struct io_wqe *wqe)
197 __must_hold(wqe->lock)
199 if (!wq_list_empty(&wqe->work_list) &&
200 !(wqe->flags & IO_WQE_FLAG_STALLED))
206 * Check head of free list for an available worker. If one isn't available,
207 * caller must wake up the wq manager to create one.
209 static bool io_wqe_activate_free_worker(struct io_wqe *wqe)
212 struct hlist_nulls_node *n;
213 struct io_worker *worker;
215 n = rcu_dereference(hlist_nulls_first_rcu(&wqe->free_list));
219 worker = hlist_nulls_entry(n, struct io_worker, nulls_node);
220 if (io_worker_get(worker)) {
221 wake_up_process(worker->task);
222 io_worker_release(worker);
230 * We need a worker. If we find a free one, we're good. If not, and we're
231 * below the max number of workers, wake up the manager to create one.
233 static void io_wqe_wake_worker(struct io_wqe *wqe, struct io_wqe_acct *acct)
238 * Most likely an attempt to queue unbounded work on an io_wq that
239 * wasn't setup with any unbounded workers.
241 WARN_ON_ONCE(!acct->max_workers);
244 ret = io_wqe_activate_free_worker(wqe);
247 if (!ret && acct->nr_workers < acct->max_workers)
248 wake_up_process(wqe->wq->manager);
251 static void io_wqe_inc_running(struct io_worker *worker)
253 struct io_wqe_acct *acct = io_wqe_get_acct(worker);
255 atomic_inc(&acct->nr_running);
258 static void io_wqe_dec_running(struct io_worker *worker)
259 __must_hold(wqe->lock)
261 struct io_wqe_acct *acct = io_wqe_get_acct(worker);
262 struct io_wqe *wqe = worker->wqe;
264 if (atomic_dec_and_test(&acct->nr_running) && io_wqe_run_queue(wqe))
265 io_wqe_wake_worker(wqe, acct);
268 static void io_worker_start(struct io_worker *worker)
270 worker->flags |= (IO_WORKER_F_UP | IO_WORKER_F_RUNNING);
271 io_wqe_inc_running(worker);
275 * Worker will start processing some work. Move it to the busy list, if
276 * it's currently on the freelist
278 static void __io_worker_busy(struct io_wqe *wqe, struct io_worker *worker,
279 struct io_wq_work *work)
280 __must_hold(wqe->lock)
282 bool worker_bound, work_bound;
284 if (worker->flags & IO_WORKER_F_FREE) {
285 worker->flags &= ~IO_WORKER_F_FREE;
286 hlist_nulls_del_init_rcu(&worker->nulls_node);
290 * If worker is moving from bound to unbound (or vice versa), then
291 * ensure we update the running accounting.
293 worker_bound = (worker->flags & IO_WORKER_F_BOUND) != 0;
294 work_bound = (work->flags & IO_WQ_WORK_UNBOUND) == 0;
295 if (worker_bound != work_bound) {
296 io_wqe_dec_running(worker);
298 worker->flags |= IO_WORKER_F_BOUND;
299 wqe->acct[IO_WQ_ACCT_UNBOUND].nr_workers--;
300 wqe->acct[IO_WQ_ACCT_BOUND].nr_workers++;
302 worker->flags &= ~IO_WORKER_F_BOUND;
303 wqe->acct[IO_WQ_ACCT_UNBOUND].nr_workers++;
304 wqe->acct[IO_WQ_ACCT_BOUND].nr_workers--;
306 io_wqe_inc_running(worker);
311 * No work, worker going to sleep. Move to freelist, and unuse mm if we
312 * have one attached. Dropping the mm may potentially sleep, so we drop
313 * the lock in that case and return success. Since the caller has to
314 * retry the loop in that case (we changed task state), we don't regrab
315 * the lock if we return success.
317 static void __io_worker_idle(struct io_wqe *wqe, struct io_worker *worker)
318 __must_hold(wqe->lock)
320 if (!(worker->flags & IO_WORKER_F_FREE)) {
321 worker->flags |= IO_WORKER_F_FREE;
322 hlist_nulls_add_head_rcu(&worker->nulls_node, &wqe->free_list);
324 if (worker->saved_creds) {
325 revert_creds(worker->saved_creds);
326 worker->cur_creds = worker->saved_creds = NULL;
330 static inline unsigned int io_get_work_hash(struct io_wq_work *work)
332 return work->flags >> IO_WQ_HASH_SHIFT;
335 static void io_wait_on_hash(struct io_wqe *wqe, unsigned int hash)
337 struct io_wq *wq = wqe->wq;
339 spin_lock(&wq->hash->wait.lock);
340 if (list_empty(&wqe->wait.entry)) {
341 __add_wait_queue(&wq->hash->wait, &wqe->wait);
342 if (!test_bit(hash, &wq->hash->map)) {
343 __set_current_state(TASK_RUNNING);
344 list_del_init(&wqe->wait.entry);
347 spin_unlock(&wq->hash->wait.lock);
350 static struct io_wq_work *io_get_next_work(struct io_wqe *wqe)
351 __must_hold(wqe->lock)
353 struct io_wq_work_node *node, *prev;
354 struct io_wq_work *work, *tail;
355 unsigned int stall_hash = -1U;
357 wq_list_for_each(node, prev, &wqe->work_list) {
360 work = container_of(node, struct io_wq_work, list);
362 /* not hashed, can run anytime */
363 if (!io_wq_is_hashed(work)) {
364 wq_list_del(&wqe->work_list, node, prev);
368 hash = io_get_work_hash(work);
369 /* all items with this hash lie in [work, tail] */
370 tail = wqe->hash_tail[hash];
372 /* hashed, can run if not already running */
373 if (!test_and_set_bit(hash, &wqe->wq->hash->map)) {
374 wqe->hash_tail[hash] = NULL;
375 wq_list_cut(&wqe->work_list, &tail->list, prev);
378 if (stall_hash == -1U)
380 /* fast forward to a next hash, for-each will fix up @prev */
384 if (stall_hash != -1U) {
385 raw_spin_unlock(&wqe->lock);
386 io_wait_on_hash(wqe, stall_hash);
387 raw_spin_lock(&wqe->lock);
393 static void io_flush_signals(void)
395 if (unlikely(test_tsk_thread_flag(current, TIF_NOTIFY_SIGNAL))) {
396 if (current->task_works)
398 clear_tsk_thread_flag(current, TIF_NOTIFY_SIGNAL);
402 static void io_wq_switch_creds(struct io_worker *worker,
403 struct io_wq_work *work)
405 const struct cred *old_creds = override_creds(work->creds);
407 worker->cur_creds = work->creds;
408 if (worker->saved_creds)
409 put_cred(old_creds); /* creds set by previous switch */
411 worker->saved_creds = old_creds;
414 static void io_assign_current_work(struct io_worker *worker,
415 struct io_wq_work *work)
422 spin_lock_irq(&worker->lock);
423 worker->cur_work = work;
424 spin_unlock_irq(&worker->lock);
427 static void io_wqe_enqueue(struct io_wqe *wqe, struct io_wq_work *work);
429 static void io_worker_handle_work(struct io_worker *worker)
430 __releases(wqe->lock)
432 struct io_wqe *wqe = worker->wqe;
433 struct io_wq *wq = wqe->wq;
436 struct io_wq_work *work;
439 * If we got some work, mark us as busy. If we didn't, but
440 * the list isn't empty, it means we stalled on hashed work.
441 * Mark us stalled so we don't keep looking for work when we
442 * can't make progress, any work completion or insertion will
443 * clear the stalled flag.
445 work = io_get_next_work(wqe);
447 __io_worker_busy(wqe, worker, work);
448 else if (!wq_list_empty(&wqe->work_list))
449 wqe->flags |= IO_WQE_FLAG_STALLED;
451 raw_spin_unlock_irq(&wqe->lock);
454 io_assign_current_work(worker, work);
455 __set_current_state(TASK_RUNNING);
457 /* handle a whole dependent link */
459 struct io_wq_work *next_hashed, *linked;
460 unsigned int hash = io_get_work_hash(work);
462 next_hashed = wq_next_work(work);
463 if (work->creds && worker->cur_creds != work->creds)
464 io_wq_switch_creds(worker, work);
466 io_assign_current_work(worker, NULL);
468 linked = wq->free_work(work);
470 if (!work && linked && !io_wq_is_hashed(linked)) {
474 io_assign_current_work(worker, work);
476 io_wqe_enqueue(wqe, linked);
478 if (hash != -1U && !next_hashed) {
479 clear_bit(hash, &wq->hash->map);
480 if (wq_has_sleeper(&wq->hash->wait))
481 wake_up(&wq->hash->wait);
482 raw_spin_lock_irq(&wqe->lock);
483 wqe->flags &= ~IO_WQE_FLAG_STALLED;
484 /* skip unnecessary unlock-lock wqe->lock */
487 raw_spin_unlock_irq(&wqe->lock);
491 raw_spin_lock_irq(&wqe->lock);
495 static int io_wqe_worker(void *data)
497 struct io_worker *worker = data;
498 struct io_wqe *wqe = worker->wqe;
499 struct io_wq *wq = wqe->wq;
501 io_worker_start(worker);
503 while (!test_bit(IO_WQ_BIT_EXIT, &wq->state)) {
504 set_current_state(TASK_INTERRUPTIBLE);
506 raw_spin_lock_irq(&wqe->lock);
507 if (io_wqe_run_queue(wqe)) {
508 io_worker_handle_work(worker);
511 __io_worker_idle(wqe, worker);
512 raw_spin_unlock_irq(&wqe->lock);
514 if (schedule_timeout(WORKER_IDLE_TIMEOUT))
516 if (fatal_signal_pending(current))
518 /* timed out, exit unless we're the fixed worker */
519 if (test_bit(IO_WQ_BIT_EXIT, &wq->state) ||
520 !(worker->flags & IO_WORKER_F_FIXED))
524 if (test_bit(IO_WQ_BIT_EXIT, &wq->state)) {
525 raw_spin_lock_irq(&wqe->lock);
526 if (!wq_list_empty(&wqe->work_list))
527 io_worker_handle_work(worker);
529 raw_spin_unlock_irq(&wqe->lock);
532 io_worker_exit(worker);
537 * Called when a worker is scheduled in. Mark us as currently running.
539 void io_wq_worker_running(struct task_struct *tsk)
541 struct io_worker *worker = tsk->pf_io_worker;
545 if (!(worker->flags & IO_WORKER_F_UP))
547 if (worker->flags & IO_WORKER_F_RUNNING)
549 worker->flags |= IO_WORKER_F_RUNNING;
550 io_wqe_inc_running(worker);
554 * Called when worker is going to sleep. If there are no workers currently
555 * running and we have work pending, wake up a free one or have the manager
558 void io_wq_worker_sleeping(struct task_struct *tsk)
560 struct io_worker *worker = tsk->pf_io_worker;
564 if (!(worker->flags & IO_WORKER_F_UP))
566 if (!(worker->flags & IO_WORKER_F_RUNNING))
569 worker->flags &= ~IO_WORKER_F_RUNNING;
571 raw_spin_lock_irq(&worker->wqe->lock);
572 io_wqe_dec_running(worker);
573 raw_spin_unlock_irq(&worker->wqe->lock);
576 static int task_thread(void *data, int index)
578 struct io_worker *worker = data;
579 struct io_wqe *wqe = worker->wqe;
580 struct io_wqe_acct *acct = &wqe->acct[index];
581 struct io_wq *wq = wqe->wq;
582 char buf[TASK_COMM_LEN];
584 sprintf(buf, "iou-wrk-%d", wq->task_pid);
585 set_task_comm(current, buf);
587 current->pf_io_worker = worker;
588 worker->task = current;
590 set_cpus_allowed_ptr(current, cpumask_of_node(wqe->node));
591 current->flags |= PF_NO_SETAFFINITY;
593 raw_spin_lock_irq(&wqe->lock);
594 hlist_nulls_add_head_rcu(&worker->nulls_node, &wqe->free_list);
595 list_add_tail_rcu(&worker->all_list, &wqe->all_list);
596 worker->flags |= IO_WORKER_F_FREE;
597 if (index == IO_WQ_ACCT_BOUND)
598 worker->flags |= IO_WORKER_F_BOUND;
599 if (!acct->nr_workers && (worker->flags & IO_WORKER_F_BOUND))
600 worker->flags |= IO_WORKER_F_FIXED;
602 raw_spin_unlock_irq(&wqe->lock);
608 static int task_thread_bound(void *data)
610 return task_thread(data, IO_WQ_ACCT_BOUND);
613 static int task_thread_unbound(void *data)
615 return task_thread(data, IO_WQ_ACCT_UNBOUND);
618 pid_t io_wq_fork_thread(int (*fn)(void *), void *arg)
620 unsigned long flags = CLONE_FS|CLONE_FILES|CLONE_SIGHAND|CLONE_THREAD|
622 struct kernel_clone_args args = {
623 .flags = ((lower_32_bits(flags) | CLONE_VM |
624 CLONE_UNTRACED) & ~CSIGNAL),
625 .exit_signal = (lower_32_bits(flags) & CSIGNAL),
626 .stack = (unsigned long)fn,
627 .stack_size = (unsigned long)arg,
630 return kernel_clone(&args);
633 static bool create_io_worker(struct io_wq *wq, struct io_wqe *wqe, int index)
635 struct io_worker *worker;
638 __set_current_state(TASK_RUNNING);
640 worker = kzalloc_node(sizeof(*worker), GFP_KERNEL, wqe->node);
644 refcount_set(&worker->ref, 1);
645 worker->nulls_node.pprev = NULL;
647 spin_lock_init(&worker->lock);
648 init_completion(&worker->ref_done);
650 refcount_inc(&wq->refs);
652 if (index == IO_WQ_ACCT_BOUND)
653 pid = io_wq_fork_thread(task_thread_bound, worker);
655 pid = io_wq_fork_thread(task_thread_unbound, worker);
657 if (refcount_dec_and_test(&wq->refs))
665 static inline bool io_wqe_need_worker(struct io_wqe *wqe, int index)
666 __must_hold(wqe->lock)
668 struct io_wqe_acct *acct = &wqe->acct[index];
670 /* if we have available workers or no work, no need */
671 if (!hlist_nulls_empty(&wqe->free_list) || !io_wqe_run_queue(wqe))
673 return acct->nr_workers < acct->max_workers;
677 * Iterate the passed in list and call the specific function for each
678 * worker that isn't exiting
680 static bool io_wq_for_each_worker(struct io_wqe *wqe,
681 bool (*func)(struct io_worker *, void *),
684 struct io_worker *worker;
687 list_for_each_entry_rcu(worker, &wqe->all_list, all_list) {
688 if (io_worker_get(worker)) {
689 /* no task if node is/was offline */
691 ret = func(worker, data);
692 io_worker_release(worker);
701 static bool io_wq_worker_wake(struct io_worker *worker, void *data)
703 wake_up_process(worker->task);
707 static void io_wq_check_workers(struct io_wq *wq)
711 for_each_node(node) {
712 struct io_wqe *wqe = wq->wqes[node];
713 bool fork_worker[2] = { false, false };
715 if (!node_online(node))
718 raw_spin_lock_irq(&wqe->lock);
719 if (io_wqe_need_worker(wqe, IO_WQ_ACCT_BOUND))
720 fork_worker[IO_WQ_ACCT_BOUND] = true;
721 if (io_wqe_need_worker(wqe, IO_WQ_ACCT_UNBOUND))
722 fork_worker[IO_WQ_ACCT_UNBOUND] = true;
723 raw_spin_unlock_irq(&wqe->lock);
724 if (fork_worker[IO_WQ_ACCT_BOUND])
725 create_io_worker(wq, wqe, IO_WQ_ACCT_BOUND);
726 if (fork_worker[IO_WQ_ACCT_UNBOUND])
727 create_io_worker(wq, wqe, IO_WQ_ACCT_UNBOUND);
732 * Manager thread. Tasked with creating new workers, if we need them.
734 static int io_wq_manager(void *data)
736 struct io_wq *wq = data;
737 char buf[TASK_COMM_LEN];
740 sprintf(buf, "iou-mgr-%d", wq->task_pid);
741 set_task_comm(current, buf);
742 current->flags |= PF_IO_WORKER;
743 wq->manager = current;
748 set_current_state(TASK_INTERRUPTIBLE);
749 io_wq_check_workers(wq);
750 schedule_timeout(HZ);
751 if (fatal_signal_pending(current))
752 set_bit(IO_WQ_BIT_EXIT, &wq->state);
753 } while (!test_bit(IO_WQ_BIT_EXIT, &wq->state));
755 io_wq_check_workers(wq);
757 if (refcount_dec_and_test(&wq->refs)) {
762 /* if ERROR is set and we get here, we have workers to wake */
763 if (test_bit(IO_WQ_BIT_ERROR, &wq->state)) {
766 io_wq_for_each_worker(wq->wqes[node], io_wq_worker_wake, NULL);
773 static void io_run_cancel(struct io_wq_work *work, struct io_wqe *wqe)
775 struct io_wq *wq = wqe->wq;
778 work->flags |= IO_WQ_WORK_CANCEL;
780 work = wq->free_work(work);
784 static void io_wqe_insert_work(struct io_wqe *wqe, struct io_wq_work *work)
787 struct io_wq_work *tail;
789 if (!io_wq_is_hashed(work)) {
791 wq_list_add_tail(&work->list, &wqe->work_list);
795 hash = io_get_work_hash(work);
796 tail = wqe->hash_tail[hash];
797 wqe->hash_tail[hash] = work;
801 wq_list_add_after(&work->list, &tail->list, &wqe->work_list);
804 static void io_wqe_enqueue(struct io_wqe *wqe, struct io_wq_work *work)
806 struct io_wqe_acct *acct = io_work_get_acct(wqe, work);
810 work_flags = work->flags;
811 raw_spin_lock_irqsave(&wqe->lock, flags);
812 io_wqe_insert_work(wqe, work);
813 wqe->flags &= ~IO_WQE_FLAG_STALLED;
814 raw_spin_unlock_irqrestore(&wqe->lock, flags);
816 if ((work_flags & IO_WQ_WORK_CONCURRENT) ||
817 !atomic_read(&acct->nr_running))
818 io_wqe_wake_worker(wqe, acct);
821 void io_wq_enqueue(struct io_wq *wq, struct io_wq_work *work)
823 struct io_wqe *wqe = wq->wqes[numa_node_id()];
825 io_wqe_enqueue(wqe, work);
829 * Work items that hash to the same value will not be done in parallel.
830 * Used to limit concurrent writes, generally hashed by inode.
832 void io_wq_hash_work(struct io_wq_work *work, void *val)
836 bit = hash_ptr(val, IO_WQ_HASH_ORDER);
837 work->flags |= (IO_WQ_WORK_HASHED | (bit << IO_WQ_HASH_SHIFT));
840 struct io_cb_cancel_data {
848 static bool io_wq_worker_cancel(struct io_worker *worker, void *data)
850 struct io_cb_cancel_data *match = data;
854 * Hold the lock to avoid ->cur_work going out of scope, caller
855 * may dereference the passed in work.
857 spin_lock_irqsave(&worker->lock, flags);
858 if (worker->cur_work &&
859 match->fn(worker->cur_work, match->data)) {
860 set_notify_signal(worker->task);
863 spin_unlock_irqrestore(&worker->lock, flags);
865 return match->nr_running && !match->cancel_all;
868 static inline void io_wqe_remove_pending(struct io_wqe *wqe,
869 struct io_wq_work *work,
870 struct io_wq_work_node *prev)
872 unsigned int hash = io_get_work_hash(work);
873 struct io_wq_work *prev_work = NULL;
875 if (io_wq_is_hashed(work) && work == wqe->hash_tail[hash]) {
877 prev_work = container_of(prev, struct io_wq_work, list);
878 if (prev_work && io_get_work_hash(prev_work) == hash)
879 wqe->hash_tail[hash] = prev_work;
881 wqe->hash_tail[hash] = NULL;
883 wq_list_del(&wqe->work_list, &work->list, prev);
886 static void io_wqe_cancel_pending_work(struct io_wqe *wqe,
887 struct io_cb_cancel_data *match)
889 struct io_wq_work_node *node, *prev;
890 struct io_wq_work *work;
894 raw_spin_lock_irqsave(&wqe->lock, flags);
895 wq_list_for_each(node, prev, &wqe->work_list) {
896 work = container_of(node, struct io_wq_work, list);
897 if (!match->fn(work, match->data))
899 io_wqe_remove_pending(wqe, work, prev);
900 raw_spin_unlock_irqrestore(&wqe->lock, flags);
901 io_run_cancel(work, wqe);
903 if (!match->cancel_all)
906 /* not safe to continue after unlock */
909 raw_spin_unlock_irqrestore(&wqe->lock, flags);
912 static void io_wqe_cancel_running_work(struct io_wqe *wqe,
913 struct io_cb_cancel_data *match)
916 io_wq_for_each_worker(wqe, io_wq_worker_cancel, match);
920 enum io_wq_cancel io_wq_cancel_cb(struct io_wq *wq, work_cancel_fn *cancel,
921 void *data, bool cancel_all)
923 struct io_cb_cancel_data match = {
926 .cancel_all = cancel_all,
931 * First check pending list, if we're lucky we can just remove it
932 * from there. CANCEL_OK means that the work is returned as-new,
933 * no completion will be posted for it.
935 for_each_node(node) {
936 struct io_wqe *wqe = wq->wqes[node];
938 io_wqe_cancel_pending_work(wqe, &match);
939 if (match.nr_pending && !match.cancel_all)
940 return IO_WQ_CANCEL_OK;
944 * Now check if a free (going busy) or busy worker has the work
945 * currently running. If we find it there, we'll return CANCEL_RUNNING
946 * as an indication that we attempt to signal cancellation. The
947 * completion will run normally in this case.
949 for_each_node(node) {
950 struct io_wqe *wqe = wq->wqes[node];
952 io_wqe_cancel_running_work(wqe, &match);
953 if (match.nr_running && !match.cancel_all)
954 return IO_WQ_CANCEL_RUNNING;
957 if (match.nr_running)
958 return IO_WQ_CANCEL_RUNNING;
959 if (match.nr_pending)
960 return IO_WQ_CANCEL_OK;
961 return IO_WQ_CANCEL_NOTFOUND;
964 static int io_wqe_hash_wake(struct wait_queue_entry *wait, unsigned mode,
967 struct io_wqe *wqe = container_of(wait, struct io_wqe, wait);
970 list_del_init(&wait->entry);
973 ret = io_wqe_activate_free_worker(wqe);
977 wake_up_process(wqe->wq->manager);
982 struct io_wq *io_wq_create(unsigned bounded, struct io_wq_data *data)
984 int ret = -ENOMEM, node;
987 if (WARN_ON_ONCE(!data->free_work || !data->do_work))
988 return ERR_PTR(-EINVAL);
990 wq = kzalloc(sizeof(*wq), GFP_KERNEL);
992 return ERR_PTR(-ENOMEM);
994 wq->wqes = kcalloc(nr_node_ids, sizeof(struct io_wqe *), GFP_KERNEL);
998 ret = cpuhp_state_add_instance_nocalls(io_wq_online, &wq->cpuhp_node);
1002 refcount_inc(&data->hash->refs);
1003 wq->hash = data->hash;
1004 wq->free_work = data->free_work;
1005 wq->do_work = data->do_work;
1008 for_each_node(node) {
1010 int alloc_node = node;
1012 if (!node_online(alloc_node))
1013 alloc_node = NUMA_NO_NODE;
1014 wqe = kzalloc_node(sizeof(struct io_wqe), GFP_KERNEL, alloc_node);
1017 wq->wqes[node] = wqe;
1018 wqe->node = alloc_node;
1019 wqe->acct[IO_WQ_ACCT_BOUND].max_workers = bounded;
1020 atomic_set(&wqe->acct[IO_WQ_ACCT_BOUND].nr_running, 0);
1021 wqe->acct[IO_WQ_ACCT_UNBOUND].max_workers =
1022 task_rlimit(current, RLIMIT_NPROC);
1023 atomic_set(&wqe->acct[IO_WQ_ACCT_UNBOUND].nr_running, 0);
1024 wqe->wait.func = io_wqe_hash_wake;
1025 INIT_LIST_HEAD(&wqe->wait.entry);
1027 raw_spin_lock_init(&wqe->lock);
1028 INIT_WQ_LIST(&wqe->work_list);
1029 INIT_HLIST_NULLS_HEAD(&wqe->free_list, 0);
1030 INIT_LIST_HEAD(&wqe->all_list);
1033 wq->task_pid = current->pid;
1034 init_completion(&wq->done);
1035 refcount_set(&wq->refs, 1);
1037 current->flags |= PF_IO_WORKER;
1038 ret = io_wq_fork_thread(io_wq_manager, wq);
1039 current->flags &= ~PF_IO_WORKER;
1041 wait_for_completion(&wq->done);
1045 if (refcount_dec_and_test(&wq->refs))
1046 complete(&wq->done);
1047 io_wq_put_hash(data->hash);
1049 cpuhp_state_remove_instance_nocalls(io_wq_online, &wq->cpuhp_node);
1051 kfree(wq->wqes[node]);
1056 return ERR_PTR(ret);
1059 void io_wq_destroy(struct io_wq *wq)
1063 cpuhp_state_remove_instance_nocalls(io_wq_online, &wq->cpuhp_node);
1065 set_bit(IO_WQ_BIT_EXIT, &wq->state);
1067 wake_up_process(wq->manager);
1071 io_wq_for_each_worker(wq->wqes[node], io_wq_worker_wake, NULL);
1074 wait_for_completion(&wq->done);
1076 spin_lock_irq(&wq->hash->wait.lock);
1077 for_each_node(node) {
1078 struct io_wqe *wqe = wq->wqes[node];
1080 list_del_init(&wqe->wait.entry);
1083 spin_unlock_irq(&wq->hash->wait.lock);
1084 io_wq_put_hash(wq->hash);
1089 static bool io_wq_worker_affinity(struct io_worker *worker, void *data)
1091 struct task_struct *task = worker->task;
1095 rq = task_rq_lock(task, &rf);
1096 do_set_cpus_allowed(task, cpumask_of_node(worker->wqe->node));
1097 task->flags |= PF_NO_SETAFFINITY;
1098 task_rq_unlock(rq, task, &rf);
1102 static int io_wq_cpu_online(unsigned int cpu, struct hlist_node *node)
1104 struct io_wq *wq = hlist_entry_safe(node, struct io_wq, cpuhp_node);
1109 io_wq_for_each_worker(wq->wqes[i], io_wq_worker_affinity, NULL);
1114 static __init int io_wq_init(void)
1118 ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "io-wq/online",
1119 io_wq_cpu_online, NULL);
1125 subsys_initcall(io_wq_init);