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>
12 #include <linux/percpu.h>
13 #include <linux/slab.h>
14 #include <linux/rculist_nulls.h>
15 #include <linux/cpu.h>
16 #include <linux/task_work.h>
17 #include <linux/audit.h>
18 #include <linux/mmu_context.h>
19 #include <uapi/linux/io_uring.h>
25 #define WORKER_IDLE_TIMEOUT (5 * HZ)
28 IO_WORKER_F_UP = 1, /* up and active */
29 IO_WORKER_F_RUNNING = 2, /* account as running */
30 IO_WORKER_F_FREE = 4, /* worker on free list */
31 IO_WORKER_F_BOUND = 8, /* is doing bounded work */
35 IO_WQ_BIT_EXIT = 0, /* wq exiting */
39 IO_ACCT_STALLED_BIT = 0, /* stalled on hash */
43 * One for each thread in a wq 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;
54 struct io_wq_work *next_work;
57 struct completion ref_done;
59 unsigned long create_state;
60 struct callback_head create_work;
65 struct work_struct work;
69 #if BITS_PER_LONG == 64
70 #define IO_WQ_HASH_ORDER 6
72 #define IO_WQ_HASH_ORDER 5
75 #define IO_WQ_NR_HASH_BUCKETS (1u << IO_WQ_HASH_ORDER)
83 struct io_wq_work_list work_list;
99 free_work_fn *free_work;
100 io_wq_work_fn *do_work;
102 struct io_wq_hash *hash;
104 atomic_t worker_refs;
105 struct completion worker_done;
107 struct hlist_node cpuhp_node;
109 struct task_struct *task;
111 struct io_wq_acct acct[IO_WQ_ACCT_NR];
113 /* lock protects access to elements below */
116 struct hlist_nulls_head free_list;
117 struct list_head all_list;
119 struct wait_queue_entry wait;
121 struct io_wq_work *hash_tail[IO_WQ_NR_HASH_BUCKETS];
123 cpumask_var_t cpu_mask;
126 static enum cpuhp_state io_wq_online;
128 struct io_cb_cancel_data {
136 static bool create_io_worker(struct io_wq *wq, int index);
137 static void io_wq_dec_running(struct io_worker *worker);
138 static bool io_acct_cancel_pending_work(struct io_wq *wq,
139 struct io_wq_acct *acct,
140 struct io_cb_cancel_data *match);
141 static void create_worker_cb(struct callback_head *cb);
142 static void io_wq_cancel_tw_create(struct io_wq *wq);
144 static bool io_worker_get(struct io_worker *worker)
146 return refcount_inc_not_zero(&worker->ref);
149 static void io_worker_release(struct io_worker *worker)
151 if (refcount_dec_and_test(&worker->ref))
152 complete(&worker->ref_done);
155 static inline struct io_wq_acct *io_get_acct(struct io_wq *wq, bool bound)
157 return &wq->acct[bound ? IO_WQ_ACCT_BOUND : IO_WQ_ACCT_UNBOUND];
160 static inline struct io_wq_acct *io_work_get_acct(struct io_wq *wq,
161 struct io_wq_work *work)
163 return io_get_acct(wq, !(work->flags & IO_WQ_WORK_UNBOUND));
166 static inline struct io_wq_acct *io_wq_get_acct(struct io_worker *worker)
168 return io_get_acct(worker->wq, worker->flags & IO_WORKER_F_BOUND);
171 static void io_worker_ref_put(struct io_wq *wq)
173 if (atomic_dec_and_test(&wq->worker_refs))
174 complete(&wq->worker_done);
177 static void io_worker_cancel_cb(struct io_worker *worker)
179 struct io_wq_acct *acct = io_wq_get_acct(worker);
180 struct io_wq *wq = worker->wq;
182 atomic_dec(&acct->nr_running);
183 raw_spin_lock(&wq->lock);
185 raw_spin_unlock(&wq->lock);
186 io_worker_ref_put(wq);
187 clear_bit_unlock(0, &worker->create_state);
188 io_worker_release(worker);
191 static bool io_task_worker_match(struct callback_head *cb, void *data)
193 struct io_worker *worker;
195 if (cb->func != create_worker_cb)
197 worker = container_of(cb, struct io_worker, create_work);
198 return worker == data;
201 static void io_worker_exit(struct io_worker *worker)
203 struct io_wq *wq = worker->wq;
206 struct callback_head *cb = task_work_cancel_match(wq->task,
207 io_task_worker_match, worker);
211 io_worker_cancel_cb(worker);
214 io_worker_release(worker);
215 wait_for_completion(&worker->ref_done);
217 raw_spin_lock(&wq->lock);
218 if (worker->flags & IO_WORKER_F_FREE)
219 hlist_nulls_del_rcu(&worker->nulls_node);
220 list_del_rcu(&worker->all_list);
221 raw_spin_unlock(&wq->lock);
222 io_wq_dec_running(worker);
225 kfree_rcu(worker, rcu);
226 io_worker_ref_put(wq);
230 static inline bool io_acct_run_queue(struct io_wq_acct *acct)
234 raw_spin_lock(&acct->lock);
235 if (!wq_list_empty(&acct->work_list) &&
236 !test_bit(IO_ACCT_STALLED_BIT, &acct->flags))
238 raw_spin_unlock(&acct->lock);
244 * Check head of free list for an available worker. If one isn't available,
245 * caller must create one.
247 static bool io_wq_activate_free_worker(struct io_wq *wq,
248 struct io_wq_acct *acct)
251 struct hlist_nulls_node *n;
252 struct io_worker *worker;
255 * Iterate free_list and see if we can find an idle worker to
256 * activate. If a given worker is on the free_list but in the process
257 * of exiting, keep trying.
259 hlist_nulls_for_each_entry_rcu(worker, n, &wq->free_list, nulls_node) {
260 if (!io_worker_get(worker))
262 if (io_wq_get_acct(worker) != acct) {
263 io_worker_release(worker);
266 if (wake_up_process(worker->task)) {
267 io_worker_release(worker);
270 io_worker_release(worker);
277 * We need a worker. If we find a free one, we're good. If not, and we're
278 * below the max number of workers, create one.
280 static bool io_wq_create_worker(struct io_wq *wq, struct io_wq_acct *acct)
283 * Most likely an attempt to queue unbounded work on an io_wq that
284 * wasn't setup with any unbounded workers.
286 if (unlikely(!acct->max_workers))
287 pr_warn_once("io-wq is not configured for unbound workers");
289 raw_spin_lock(&wq->lock);
290 if (acct->nr_workers >= acct->max_workers) {
291 raw_spin_unlock(&wq->lock);
295 raw_spin_unlock(&wq->lock);
296 atomic_inc(&acct->nr_running);
297 atomic_inc(&wq->worker_refs);
298 return create_io_worker(wq, acct->index);
301 static void io_wq_inc_running(struct io_worker *worker)
303 struct io_wq_acct *acct = io_wq_get_acct(worker);
305 atomic_inc(&acct->nr_running);
308 static void create_worker_cb(struct callback_head *cb)
310 struct io_worker *worker;
313 struct io_wq_acct *acct;
314 bool do_create = false;
316 worker = container_of(cb, struct io_worker, create_work);
318 acct = &wq->acct[worker->create_index];
319 raw_spin_lock(&wq->lock);
321 if (acct->nr_workers < acct->max_workers) {
325 raw_spin_unlock(&wq->lock);
327 create_io_worker(wq, worker->create_index);
329 atomic_dec(&acct->nr_running);
330 io_worker_ref_put(wq);
332 clear_bit_unlock(0, &worker->create_state);
333 io_worker_release(worker);
336 static bool io_queue_worker_create(struct io_worker *worker,
337 struct io_wq_acct *acct,
338 task_work_func_t func)
340 struct io_wq *wq = worker->wq;
342 /* raced with exit, just ignore create call */
343 if (test_bit(IO_WQ_BIT_EXIT, &wq->state))
345 if (!io_worker_get(worker))
348 * create_state manages ownership of create_work/index. We should
349 * only need one entry per worker, as the worker going to sleep
350 * will trigger the condition, and waking will clear it once it
351 * runs the task_work.
353 if (test_bit(0, &worker->create_state) ||
354 test_and_set_bit_lock(0, &worker->create_state))
357 atomic_inc(&wq->worker_refs);
358 init_task_work(&worker->create_work, func);
359 worker->create_index = acct->index;
360 if (!task_work_add(wq->task, &worker->create_work, TWA_SIGNAL)) {
362 * EXIT may have been set after checking it above, check after
363 * adding the task_work and remove any creation item if it is
364 * now set. wq exit does that too, but we can have added this
365 * work item after we canceled in io_wq_exit_workers().
367 if (test_bit(IO_WQ_BIT_EXIT, &wq->state))
368 io_wq_cancel_tw_create(wq);
369 io_worker_ref_put(wq);
372 io_worker_ref_put(wq);
373 clear_bit_unlock(0, &worker->create_state);
375 io_worker_release(worker);
377 atomic_dec(&acct->nr_running);
378 io_worker_ref_put(wq);
382 static void io_wq_dec_running(struct io_worker *worker)
384 struct io_wq_acct *acct = io_wq_get_acct(worker);
385 struct io_wq *wq = worker->wq;
387 if (!(worker->flags & IO_WORKER_F_UP))
390 if (!atomic_dec_and_test(&acct->nr_running))
392 if (!io_acct_run_queue(acct))
395 atomic_inc(&acct->nr_running);
396 atomic_inc(&wq->worker_refs);
397 io_queue_worker_create(worker, acct, create_worker_cb);
401 * Worker will start processing some work. Move it to the busy list, if
402 * it's currently on the freelist
404 static void __io_worker_busy(struct io_wq *wq, struct io_worker *worker)
406 if (worker->flags & IO_WORKER_F_FREE) {
407 worker->flags &= ~IO_WORKER_F_FREE;
408 raw_spin_lock(&wq->lock);
409 hlist_nulls_del_init_rcu(&worker->nulls_node);
410 raw_spin_unlock(&wq->lock);
415 * No work, worker going to sleep. Move to freelist.
417 static void __io_worker_idle(struct io_wq *wq, struct io_worker *worker)
418 __must_hold(wq->lock)
420 if (!(worker->flags & IO_WORKER_F_FREE)) {
421 worker->flags |= IO_WORKER_F_FREE;
422 hlist_nulls_add_head_rcu(&worker->nulls_node, &wq->free_list);
426 static inline unsigned int io_get_work_hash(struct io_wq_work *work)
428 return work->flags >> IO_WQ_HASH_SHIFT;
431 static bool io_wait_on_hash(struct io_wq *wq, unsigned int hash)
435 spin_lock_irq(&wq->hash->wait.lock);
436 if (list_empty(&wq->wait.entry)) {
437 __add_wait_queue(&wq->hash->wait, &wq->wait);
438 if (!test_bit(hash, &wq->hash->map)) {
439 __set_current_state(TASK_RUNNING);
440 list_del_init(&wq->wait.entry);
444 spin_unlock_irq(&wq->hash->wait.lock);
448 static struct io_wq_work *io_get_next_work(struct io_wq_acct *acct,
449 struct io_worker *worker)
450 __must_hold(acct->lock)
452 struct io_wq_work_node *node, *prev;
453 struct io_wq_work *work, *tail;
454 unsigned int stall_hash = -1U;
455 struct io_wq *wq = worker->wq;
457 wq_list_for_each(node, prev, &acct->work_list) {
460 work = container_of(node, struct io_wq_work, list);
462 /* not hashed, can run anytime */
463 if (!io_wq_is_hashed(work)) {
464 wq_list_del(&acct->work_list, node, prev);
468 hash = io_get_work_hash(work);
469 /* all items with this hash lie in [work, tail] */
470 tail = wq->hash_tail[hash];
472 /* hashed, can run if not already running */
473 if (!test_and_set_bit(hash, &wq->hash->map)) {
474 wq->hash_tail[hash] = NULL;
475 wq_list_cut(&acct->work_list, &tail->list, prev);
478 if (stall_hash == -1U)
480 /* fast forward to a next hash, for-each will fix up @prev */
484 if (stall_hash != -1U) {
488 * Set this before dropping the lock to avoid racing with new
489 * work being added and clearing the stalled bit.
491 set_bit(IO_ACCT_STALLED_BIT, &acct->flags);
492 raw_spin_unlock(&acct->lock);
493 unstalled = io_wait_on_hash(wq, stall_hash);
494 raw_spin_lock(&acct->lock);
496 clear_bit(IO_ACCT_STALLED_BIT, &acct->flags);
497 if (wq_has_sleeper(&wq->hash->wait))
498 wake_up(&wq->hash->wait);
505 static void io_assign_current_work(struct io_worker *worker,
506 struct io_wq_work *work)
513 raw_spin_lock(&worker->lock);
514 worker->cur_work = work;
515 worker->next_work = NULL;
516 raw_spin_unlock(&worker->lock);
519 static void io_worker_handle_work(struct io_worker *worker)
521 struct io_wq_acct *acct = io_wq_get_acct(worker);
522 struct io_wq *wq = worker->wq;
523 bool do_kill = test_bit(IO_WQ_BIT_EXIT, &wq->state);
526 struct io_wq_work *work;
529 * If we got some work, mark us as busy. If we didn't, but
530 * the list isn't empty, it means we stalled on hashed work.
531 * Mark us stalled so we don't keep looking for work when we
532 * can't make progress, any work completion or insertion will
533 * clear the stalled flag.
535 raw_spin_lock(&acct->lock);
536 work = io_get_next_work(acct, worker);
537 raw_spin_unlock(&acct->lock);
539 __io_worker_busy(wq, worker);
542 * Make sure cancelation can find this, even before
543 * it becomes the active work. That avoids a window
544 * where the work has been removed from our general
545 * work list, but isn't yet discoverable as the
546 * current work item for this worker.
548 raw_spin_lock(&worker->lock);
549 worker->next_work = work;
550 raw_spin_unlock(&worker->lock);
554 io_assign_current_work(worker, work);
555 __set_current_state(TASK_RUNNING);
557 /* handle a whole dependent link */
559 struct io_wq_work *next_hashed, *linked;
560 unsigned int hash = io_get_work_hash(work);
562 next_hashed = wq_next_work(work);
564 if (unlikely(do_kill) && (work->flags & IO_WQ_WORK_UNBOUND))
565 work->flags |= IO_WQ_WORK_CANCEL;
567 io_assign_current_work(worker, NULL);
569 linked = wq->free_work(work);
571 if (!work && linked && !io_wq_is_hashed(linked)) {
575 io_assign_current_work(worker, work);
577 io_wq_enqueue(wq, linked);
579 if (hash != -1U && !next_hashed) {
580 /* serialize hash clear with wake_up() */
581 spin_lock_irq(&wq->hash->wait.lock);
582 clear_bit(hash, &wq->hash->map);
583 clear_bit(IO_ACCT_STALLED_BIT, &acct->flags);
584 spin_unlock_irq(&wq->hash->wait.lock);
585 if (wq_has_sleeper(&wq->hash->wait))
586 wake_up(&wq->hash->wait);
592 static int io_wq_worker(void *data)
594 struct io_worker *worker = data;
595 struct io_wq_acct *acct = io_wq_get_acct(worker);
596 struct io_wq *wq = worker->wq;
597 bool exit_mask = false, last_timeout = false;
598 char buf[TASK_COMM_LEN];
600 worker->flags |= (IO_WORKER_F_UP | IO_WORKER_F_RUNNING);
602 snprintf(buf, sizeof(buf), "iou-wrk-%d", wq->task->pid);
603 set_task_comm(current, buf);
605 while (!test_bit(IO_WQ_BIT_EXIT, &wq->state)) {
608 set_current_state(TASK_INTERRUPTIBLE);
609 while (io_acct_run_queue(acct))
610 io_worker_handle_work(worker);
612 raw_spin_lock(&wq->lock);
614 * Last sleep timed out. Exit if we're not the last worker,
615 * or if someone modified our affinity.
617 if (last_timeout && (exit_mask || acct->nr_workers > 1)) {
619 raw_spin_unlock(&wq->lock);
620 __set_current_state(TASK_RUNNING);
623 last_timeout = false;
624 __io_worker_idle(wq, worker);
625 raw_spin_unlock(&wq->lock);
626 if (io_run_task_work())
628 ret = schedule_timeout(WORKER_IDLE_TIMEOUT);
629 if (signal_pending(current)) {
632 if (!get_signal(&ksig))
638 exit_mask = !cpumask_test_cpu(raw_smp_processor_id(),
643 if (test_bit(IO_WQ_BIT_EXIT, &wq->state))
644 io_worker_handle_work(worker);
646 io_worker_exit(worker);
651 * Called when a worker is scheduled in. Mark us as currently running.
653 void io_wq_worker_running(struct task_struct *tsk)
655 struct io_worker *worker = tsk->worker_private;
659 if (!(worker->flags & IO_WORKER_F_UP))
661 if (worker->flags & IO_WORKER_F_RUNNING)
663 worker->flags |= IO_WORKER_F_RUNNING;
664 io_wq_inc_running(worker);
668 * Called when worker is going to sleep. If there are no workers currently
669 * running and we have work pending, wake up a free one or create a new one.
671 void io_wq_worker_sleeping(struct task_struct *tsk)
673 struct io_worker *worker = tsk->worker_private;
677 if (!(worker->flags & IO_WORKER_F_UP))
679 if (!(worker->flags & IO_WORKER_F_RUNNING))
682 worker->flags &= ~IO_WORKER_F_RUNNING;
683 io_wq_dec_running(worker);
686 static void io_init_new_worker(struct io_wq *wq, struct io_worker *worker,
687 struct task_struct *tsk)
689 tsk->worker_private = worker;
691 set_cpus_allowed_ptr(tsk, wq->cpu_mask);
693 raw_spin_lock(&wq->lock);
694 hlist_nulls_add_head_rcu(&worker->nulls_node, &wq->free_list);
695 list_add_tail_rcu(&worker->all_list, &wq->all_list);
696 worker->flags |= IO_WORKER_F_FREE;
697 raw_spin_unlock(&wq->lock);
698 wake_up_new_task(tsk);
701 static bool io_wq_work_match_all(struct io_wq_work *work, void *data)
706 static inline bool io_should_retry_thread(long err)
709 * Prevent perpetual task_work retry, if the task (or its group) is
712 if (fatal_signal_pending(current))
718 case -ERESTARTNOINTR:
719 case -ERESTARTNOHAND:
726 static void create_worker_cont(struct callback_head *cb)
728 struct io_worker *worker;
729 struct task_struct *tsk;
732 worker = container_of(cb, struct io_worker, create_work);
733 clear_bit_unlock(0, &worker->create_state);
735 tsk = create_io_thread(io_wq_worker, worker, NUMA_NO_NODE);
737 io_init_new_worker(wq, worker, tsk);
738 io_worker_release(worker);
740 } else if (!io_should_retry_thread(PTR_ERR(tsk))) {
741 struct io_wq_acct *acct = io_wq_get_acct(worker);
743 atomic_dec(&acct->nr_running);
744 raw_spin_lock(&wq->lock);
746 if (!acct->nr_workers) {
747 struct io_cb_cancel_data match = {
748 .fn = io_wq_work_match_all,
752 raw_spin_unlock(&wq->lock);
753 while (io_acct_cancel_pending_work(wq, acct, &match))
756 raw_spin_unlock(&wq->lock);
758 io_worker_ref_put(wq);
763 /* re-create attempts grab a new worker ref, drop the existing one */
764 io_worker_release(worker);
765 schedule_work(&worker->work);
768 static void io_workqueue_create(struct work_struct *work)
770 struct io_worker *worker = container_of(work, struct io_worker, work);
771 struct io_wq_acct *acct = io_wq_get_acct(worker);
773 if (!io_queue_worker_create(worker, acct, create_worker_cont))
777 static bool create_io_worker(struct io_wq *wq, int index)
779 struct io_wq_acct *acct = &wq->acct[index];
780 struct io_worker *worker;
781 struct task_struct *tsk;
783 __set_current_state(TASK_RUNNING);
785 worker = kzalloc(sizeof(*worker), GFP_KERNEL);
788 atomic_dec(&acct->nr_running);
789 raw_spin_lock(&wq->lock);
791 raw_spin_unlock(&wq->lock);
792 io_worker_ref_put(wq);
796 refcount_set(&worker->ref, 1);
798 raw_spin_lock_init(&worker->lock);
799 init_completion(&worker->ref_done);
801 if (index == IO_WQ_ACCT_BOUND)
802 worker->flags |= IO_WORKER_F_BOUND;
804 tsk = create_io_thread(io_wq_worker, worker, NUMA_NO_NODE);
806 io_init_new_worker(wq, worker, tsk);
807 } else if (!io_should_retry_thread(PTR_ERR(tsk))) {
811 INIT_WORK(&worker->work, io_workqueue_create);
812 schedule_work(&worker->work);
819 * Iterate the passed in list and call the specific function for each
820 * worker that isn't exiting
822 static bool io_wq_for_each_worker(struct io_wq *wq,
823 bool (*func)(struct io_worker *, void *),
826 struct io_worker *worker;
829 list_for_each_entry_rcu(worker, &wq->all_list, all_list) {
830 if (io_worker_get(worker)) {
831 /* no task if node is/was offline */
833 ret = func(worker, data);
834 io_worker_release(worker);
843 static bool io_wq_worker_wake(struct io_worker *worker, void *data)
845 __set_notify_signal(worker->task);
846 wake_up_process(worker->task);
850 static void io_run_cancel(struct io_wq_work *work, struct io_wq *wq)
853 work->flags |= IO_WQ_WORK_CANCEL;
855 work = wq->free_work(work);
859 static void io_wq_insert_work(struct io_wq *wq, struct io_wq_work *work)
861 struct io_wq_acct *acct = io_work_get_acct(wq, work);
863 struct io_wq_work *tail;
865 if (!io_wq_is_hashed(work)) {
867 wq_list_add_tail(&work->list, &acct->work_list);
871 hash = io_get_work_hash(work);
872 tail = wq->hash_tail[hash];
873 wq->hash_tail[hash] = work;
877 wq_list_add_after(&work->list, &tail->list, &acct->work_list);
880 static bool io_wq_work_match_item(struct io_wq_work *work, void *data)
885 void io_wq_enqueue(struct io_wq *wq, struct io_wq_work *work)
887 struct io_wq_acct *acct = io_work_get_acct(wq, work);
888 struct io_cb_cancel_data match;
889 unsigned work_flags = work->flags;
893 * If io-wq is exiting for this task, or if the request has explicitly
894 * been marked as one that should not get executed, cancel it here.
896 if (test_bit(IO_WQ_BIT_EXIT, &wq->state) ||
897 (work->flags & IO_WQ_WORK_CANCEL)) {
898 io_run_cancel(work, wq);
902 raw_spin_lock(&acct->lock);
903 io_wq_insert_work(wq, work);
904 clear_bit(IO_ACCT_STALLED_BIT, &acct->flags);
905 raw_spin_unlock(&acct->lock);
907 raw_spin_lock(&wq->lock);
909 do_create = !io_wq_activate_free_worker(wq, acct);
912 raw_spin_unlock(&wq->lock);
914 if (do_create && ((work_flags & IO_WQ_WORK_CONCURRENT) ||
915 !atomic_read(&acct->nr_running))) {
918 did_create = io_wq_create_worker(wq, acct);
919 if (likely(did_create))
922 raw_spin_lock(&wq->lock);
923 if (acct->nr_workers) {
924 raw_spin_unlock(&wq->lock);
927 raw_spin_unlock(&wq->lock);
929 /* fatal condition, failed to create the first worker */
930 match.fn = io_wq_work_match_item,
932 match.cancel_all = false,
934 io_acct_cancel_pending_work(wq, acct, &match);
939 * Work items that hash to the same value will not be done in parallel.
940 * Used to limit concurrent writes, generally hashed by inode.
942 void io_wq_hash_work(struct io_wq_work *work, void *val)
946 bit = hash_ptr(val, IO_WQ_HASH_ORDER);
947 work->flags |= (IO_WQ_WORK_HASHED | (bit << IO_WQ_HASH_SHIFT));
950 static bool __io_wq_worker_cancel(struct io_worker *worker,
951 struct io_cb_cancel_data *match,
952 struct io_wq_work *work)
954 if (work && match->fn(work, match->data)) {
955 work->flags |= IO_WQ_WORK_CANCEL;
956 __set_notify_signal(worker->task);
963 static bool io_wq_worker_cancel(struct io_worker *worker, void *data)
965 struct io_cb_cancel_data *match = data;
968 * Hold the lock to avoid ->cur_work going out of scope, caller
969 * may dereference the passed in work.
971 raw_spin_lock(&worker->lock);
972 if (__io_wq_worker_cancel(worker, match, worker->cur_work) ||
973 __io_wq_worker_cancel(worker, match, worker->next_work))
975 raw_spin_unlock(&worker->lock);
977 return match->nr_running && !match->cancel_all;
980 static inline void io_wq_remove_pending(struct io_wq *wq,
981 struct io_wq_work *work,
982 struct io_wq_work_node *prev)
984 struct io_wq_acct *acct = io_work_get_acct(wq, work);
985 unsigned int hash = io_get_work_hash(work);
986 struct io_wq_work *prev_work = NULL;
988 if (io_wq_is_hashed(work) && work == wq->hash_tail[hash]) {
990 prev_work = container_of(prev, struct io_wq_work, list);
991 if (prev_work && io_get_work_hash(prev_work) == hash)
992 wq->hash_tail[hash] = prev_work;
994 wq->hash_tail[hash] = NULL;
996 wq_list_del(&acct->work_list, &work->list, prev);
999 static bool io_acct_cancel_pending_work(struct io_wq *wq,
1000 struct io_wq_acct *acct,
1001 struct io_cb_cancel_data *match)
1003 struct io_wq_work_node *node, *prev;
1004 struct io_wq_work *work;
1006 raw_spin_lock(&acct->lock);
1007 wq_list_for_each(node, prev, &acct->work_list) {
1008 work = container_of(node, struct io_wq_work, list);
1009 if (!match->fn(work, match->data))
1011 io_wq_remove_pending(wq, work, prev);
1012 raw_spin_unlock(&acct->lock);
1013 io_run_cancel(work, wq);
1014 match->nr_pending++;
1015 /* not safe to continue after unlock */
1018 raw_spin_unlock(&acct->lock);
1023 static void io_wq_cancel_pending_work(struct io_wq *wq,
1024 struct io_cb_cancel_data *match)
1028 for (i = 0; i < IO_WQ_ACCT_NR; i++) {
1029 struct io_wq_acct *acct = io_get_acct(wq, i == 0);
1031 if (io_acct_cancel_pending_work(wq, acct, match)) {
1032 if (match->cancel_all)
1039 static void io_wq_cancel_running_work(struct io_wq *wq,
1040 struct io_cb_cancel_data *match)
1043 io_wq_for_each_worker(wq, io_wq_worker_cancel, match);
1047 enum io_wq_cancel io_wq_cancel_cb(struct io_wq *wq, work_cancel_fn *cancel,
1048 void *data, bool cancel_all)
1050 struct io_cb_cancel_data match = {
1053 .cancel_all = cancel_all,
1057 * First check pending list, if we're lucky we can just remove it
1058 * from there. CANCEL_OK means that the work is returned as-new,
1059 * no completion will be posted for it.
1061 * Then check if a free (going busy) or busy worker has the work
1062 * currently running. If we find it there, we'll return CANCEL_RUNNING
1063 * as an indication that we attempt to signal cancellation. The
1064 * completion will run normally in this case.
1066 * Do both of these while holding the wq->lock, to ensure that
1067 * we'll find a work item regardless of state.
1069 io_wq_cancel_pending_work(wq, &match);
1070 if (match.nr_pending && !match.cancel_all)
1071 return IO_WQ_CANCEL_OK;
1073 raw_spin_lock(&wq->lock);
1074 io_wq_cancel_running_work(wq, &match);
1075 raw_spin_unlock(&wq->lock);
1076 if (match.nr_running && !match.cancel_all)
1077 return IO_WQ_CANCEL_RUNNING;
1079 if (match.nr_running)
1080 return IO_WQ_CANCEL_RUNNING;
1081 if (match.nr_pending)
1082 return IO_WQ_CANCEL_OK;
1083 return IO_WQ_CANCEL_NOTFOUND;
1086 static int io_wq_hash_wake(struct wait_queue_entry *wait, unsigned mode,
1087 int sync, void *key)
1089 struct io_wq *wq = container_of(wait, struct io_wq, wait);
1092 list_del_init(&wait->entry);
1095 for (i = 0; i < IO_WQ_ACCT_NR; i++) {
1096 struct io_wq_acct *acct = &wq->acct[i];
1098 if (test_and_clear_bit(IO_ACCT_STALLED_BIT, &acct->flags))
1099 io_wq_activate_free_worker(wq, acct);
1105 struct io_wq *io_wq_create(unsigned bounded, struct io_wq_data *data)
1110 if (WARN_ON_ONCE(!data->free_work || !data->do_work))
1111 return ERR_PTR(-EINVAL);
1112 if (WARN_ON_ONCE(!bounded))
1113 return ERR_PTR(-EINVAL);
1115 wq = kzalloc(sizeof(struct io_wq), GFP_KERNEL);
1117 return ERR_PTR(-ENOMEM);
1118 ret = cpuhp_state_add_instance_nocalls(io_wq_online, &wq->cpuhp_node);
1122 refcount_inc(&data->hash->refs);
1123 wq->hash = data->hash;
1124 wq->free_work = data->free_work;
1125 wq->do_work = data->do_work;
1129 if (!alloc_cpumask_var(&wq->cpu_mask, GFP_KERNEL))
1131 cpumask_copy(wq->cpu_mask, cpu_possible_mask);
1132 wq->acct[IO_WQ_ACCT_BOUND].max_workers = bounded;
1133 wq->acct[IO_WQ_ACCT_UNBOUND].max_workers =
1134 task_rlimit(current, RLIMIT_NPROC);
1135 INIT_LIST_HEAD(&wq->wait.entry);
1136 wq->wait.func = io_wq_hash_wake;
1137 for (i = 0; i < IO_WQ_ACCT_NR; i++) {
1138 struct io_wq_acct *acct = &wq->acct[i];
1141 atomic_set(&acct->nr_running, 0);
1142 INIT_WQ_LIST(&acct->work_list);
1143 raw_spin_lock_init(&acct->lock);
1146 raw_spin_lock_init(&wq->lock);
1147 INIT_HLIST_NULLS_HEAD(&wq->free_list, 0);
1148 INIT_LIST_HEAD(&wq->all_list);
1150 wq->task = get_task_struct(data->task);
1151 atomic_set(&wq->worker_refs, 1);
1152 init_completion(&wq->worker_done);
1155 io_wq_put_hash(data->hash);
1156 cpuhp_state_remove_instance_nocalls(io_wq_online, &wq->cpuhp_node);
1158 free_cpumask_var(wq->cpu_mask);
1161 return ERR_PTR(ret);
1164 static bool io_task_work_match(struct callback_head *cb, void *data)
1166 struct io_worker *worker;
1168 if (cb->func != create_worker_cb && cb->func != create_worker_cont)
1170 worker = container_of(cb, struct io_worker, create_work);
1171 return worker->wq == data;
1174 void io_wq_exit_start(struct io_wq *wq)
1176 set_bit(IO_WQ_BIT_EXIT, &wq->state);
1179 static void io_wq_cancel_tw_create(struct io_wq *wq)
1181 struct callback_head *cb;
1183 while ((cb = task_work_cancel_match(wq->task, io_task_work_match, wq)) != NULL) {
1184 struct io_worker *worker;
1186 worker = container_of(cb, struct io_worker, create_work);
1187 io_worker_cancel_cb(worker);
1189 * Only the worker continuation helper has worker allocated and
1190 * hence needs freeing.
1192 if (cb->func == create_worker_cont)
1197 static void io_wq_exit_workers(struct io_wq *wq)
1202 io_wq_cancel_tw_create(wq);
1205 io_wq_for_each_worker(wq, io_wq_worker_wake, NULL);
1207 io_worker_ref_put(wq);
1208 wait_for_completion(&wq->worker_done);
1210 spin_lock_irq(&wq->hash->wait.lock);
1211 list_del_init(&wq->wait.entry);
1212 spin_unlock_irq(&wq->hash->wait.lock);
1214 put_task_struct(wq->task);
1218 static void io_wq_destroy(struct io_wq *wq)
1220 struct io_cb_cancel_data match = {
1221 .fn = io_wq_work_match_all,
1225 cpuhp_state_remove_instance_nocalls(io_wq_online, &wq->cpuhp_node);
1226 io_wq_cancel_pending_work(wq, &match);
1227 free_cpumask_var(wq->cpu_mask);
1228 io_wq_put_hash(wq->hash);
1232 void io_wq_put_and_exit(struct io_wq *wq)
1234 WARN_ON_ONCE(!test_bit(IO_WQ_BIT_EXIT, &wq->state));
1236 io_wq_exit_workers(wq);
1240 struct online_data {
1245 static bool io_wq_worker_affinity(struct io_worker *worker, void *data)
1247 struct online_data *od = data;
1250 cpumask_set_cpu(od->cpu, worker->wq->cpu_mask);
1252 cpumask_clear_cpu(od->cpu, worker->wq->cpu_mask);
1256 static int __io_wq_cpu_online(struct io_wq *wq, unsigned int cpu, bool online)
1258 struct online_data od = {
1264 io_wq_for_each_worker(wq, io_wq_worker_affinity, &od);
1269 static int io_wq_cpu_online(unsigned int cpu, struct hlist_node *node)
1271 struct io_wq *wq = hlist_entry_safe(node, struct io_wq, cpuhp_node);
1273 return __io_wq_cpu_online(wq, cpu, true);
1276 static int io_wq_cpu_offline(unsigned int cpu, struct hlist_node *node)
1278 struct io_wq *wq = hlist_entry_safe(node, struct io_wq, cpuhp_node);
1280 return __io_wq_cpu_online(wq, cpu, false);
1283 int io_wq_cpu_affinity(struct io_wq *wq, cpumask_var_t mask)
1287 cpumask_copy(wq->cpu_mask, mask);
1289 cpumask_copy(wq->cpu_mask, cpu_possible_mask);
1296 * Set max number of unbounded workers, returns old value. If new_count is 0,
1297 * then just return the old value.
1299 int io_wq_max_workers(struct io_wq *wq, int *new_count)
1301 struct io_wq_acct *acct;
1302 int prev[IO_WQ_ACCT_NR];
1305 BUILD_BUG_ON((int) IO_WQ_ACCT_BOUND != (int) IO_WQ_BOUND);
1306 BUILD_BUG_ON((int) IO_WQ_ACCT_UNBOUND != (int) IO_WQ_UNBOUND);
1307 BUILD_BUG_ON((int) IO_WQ_ACCT_NR != 2);
1309 for (i = 0; i < IO_WQ_ACCT_NR; i++) {
1310 if (new_count[i] > task_rlimit(current, RLIMIT_NPROC))
1311 new_count[i] = task_rlimit(current, RLIMIT_NPROC);
1314 for (i = 0; i < IO_WQ_ACCT_NR; i++)
1319 raw_spin_lock(&wq->lock);
1320 for (i = 0; i < IO_WQ_ACCT_NR; i++) {
1321 acct = &wq->acct[i];
1322 prev[i] = max_t(int, acct->max_workers, prev[i]);
1324 acct->max_workers = new_count[i];
1326 raw_spin_unlock(&wq->lock);
1329 for (i = 0; i < IO_WQ_ACCT_NR; i++)
1330 new_count[i] = prev[i];
1335 static __init int io_wq_init(void)
1339 ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "io-wq/online",
1340 io_wq_cpu_online, io_wq_cpu_offline);
1346 subsys_initcall(io_wq_init);