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/cpuset.h>
17 #include <linux/task_work.h>
18 #include <linux/audit.h>
19 #include <linux/mmu_context.h>
20 #include <uapi/linux/io_uring.h>
26 #define WORKER_IDLE_TIMEOUT (5 * HZ)
27 #define WORKER_INIT_LIMIT 3
30 IO_WORKER_F_UP = 0, /* up and active */
31 IO_WORKER_F_RUNNING = 1, /* account as running */
32 IO_WORKER_F_FREE = 2, /* worker on free list */
36 IO_WQ_BIT_EXIT = 0, /* wq exiting */
40 IO_ACCT_STALLED_BIT = 0, /* stalled on hash */
44 * One for each thread in a wq pool
49 struct hlist_nulls_node nulls_node;
50 struct list_head all_list;
51 struct task_struct *task;
53 struct io_wq_acct *acct;
55 struct io_wq_work *cur_work;
58 struct completion ref_done;
60 unsigned long create_state;
61 struct callback_head create_work;
66 struct delayed_work work;
70 #if BITS_PER_LONG == 64
71 #define IO_WQ_HASH_ORDER 6
73 #define IO_WQ_HASH_ORDER 5
76 #define IO_WQ_NR_HASH_BUCKETS (1u << IO_WQ_HASH_ORDER)
80 * Protects access to the worker lists.
82 raw_spinlock_t workers_lock;
89 * The list of free workers. Protected by #workers_lock
90 * (write) and RCU (read).
92 struct hlist_nulls_head free_list;
95 * The list of all workers. Protected by #workers_lock
96 * (write) and RCU (read).
98 struct list_head all_list;
101 struct io_wq_work_list work_list;
117 struct io_wq_hash *hash;
119 atomic_t worker_refs;
120 struct completion worker_done;
122 struct hlist_node cpuhp_node;
124 struct task_struct *task;
126 struct io_wq_acct acct[IO_WQ_ACCT_NR];
128 struct wait_queue_entry wait;
130 struct io_wq_work *hash_tail[IO_WQ_NR_HASH_BUCKETS];
132 cpumask_var_t cpu_mask;
135 static enum cpuhp_state io_wq_online;
137 struct io_cb_cancel_data {
145 static bool create_io_worker(struct io_wq *wq, struct io_wq_acct *acct);
146 static void io_wq_dec_running(struct io_worker *worker);
147 static bool io_acct_cancel_pending_work(struct io_wq *wq,
148 struct io_wq_acct *acct,
149 struct io_cb_cancel_data *match);
150 static void create_worker_cb(struct callback_head *cb);
151 static void io_wq_cancel_tw_create(struct io_wq *wq);
153 static inline unsigned int __io_get_work_hash(unsigned int work_flags)
155 return work_flags >> IO_WQ_HASH_SHIFT;
158 static inline unsigned int io_get_work_hash(struct io_wq_work *work)
160 return __io_get_work_hash(atomic_read(&work->flags));
163 static bool io_worker_get(struct io_worker *worker)
165 return refcount_inc_not_zero(&worker->ref);
168 static void io_worker_release(struct io_worker *worker)
170 if (refcount_dec_and_test(&worker->ref))
171 complete(&worker->ref_done);
174 static inline struct io_wq_acct *io_get_acct(struct io_wq *wq, bool bound)
176 return &wq->acct[bound ? IO_WQ_ACCT_BOUND : IO_WQ_ACCT_UNBOUND];
179 static inline struct io_wq_acct *io_work_get_acct(struct io_wq *wq,
180 unsigned int work_flags)
182 return io_get_acct(wq, !(work_flags & IO_WQ_WORK_UNBOUND));
185 static inline struct io_wq_acct *io_wq_get_acct(struct io_worker *worker)
190 static void io_worker_ref_put(struct io_wq *wq)
192 if (atomic_dec_and_test(&wq->worker_refs))
193 complete(&wq->worker_done);
196 bool io_wq_worker_stopped(void)
198 struct io_worker *worker = current->worker_private;
200 if (WARN_ON_ONCE(!io_wq_current_is_worker()))
203 return test_bit(IO_WQ_BIT_EXIT, &worker->wq->state);
206 static void io_worker_cancel_cb(struct io_worker *worker)
208 struct io_wq_acct *acct = io_wq_get_acct(worker);
209 struct io_wq *wq = worker->wq;
211 atomic_dec(&acct->nr_running);
212 raw_spin_lock(&acct->workers_lock);
214 raw_spin_unlock(&acct->workers_lock);
215 io_worker_ref_put(wq);
216 clear_bit_unlock(0, &worker->create_state);
217 io_worker_release(worker);
220 static bool io_task_worker_match(struct callback_head *cb, void *data)
222 struct io_worker *worker;
224 if (cb->func != create_worker_cb)
226 worker = container_of(cb, struct io_worker, create_work);
227 return worker == data;
230 static void io_worker_exit(struct io_worker *worker)
232 struct io_wq *wq = worker->wq;
233 struct io_wq_acct *acct = io_wq_get_acct(worker);
236 struct callback_head *cb = task_work_cancel_match(wq->task,
237 io_task_worker_match, worker);
241 io_worker_cancel_cb(worker);
244 io_worker_release(worker);
245 wait_for_completion(&worker->ref_done);
247 raw_spin_lock(&acct->workers_lock);
248 if (test_bit(IO_WORKER_F_FREE, &worker->flags))
249 hlist_nulls_del_rcu(&worker->nulls_node);
250 list_del_rcu(&worker->all_list);
251 raw_spin_unlock(&acct->workers_lock);
252 io_wq_dec_running(worker);
254 * this worker is a goner, clear ->worker_private to avoid any
255 * inc/dec running calls that could happen as part of exit from
258 current->worker_private = NULL;
260 kfree_rcu(worker, rcu);
261 io_worker_ref_put(wq);
265 static inline bool __io_acct_run_queue(struct io_wq_acct *acct)
267 return !test_bit(IO_ACCT_STALLED_BIT, &acct->flags) &&
268 !wq_list_empty(&acct->work_list);
272 * If there's work to do, returns true with acct->lock acquired. If not,
273 * returns false with no lock held.
275 static inline bool io_acct_run_queue(struct io_wq_acct *acct)
276 __acquires(&acct->lock)
278 raw_spin_lock(&acct->lock);
279 if (__io_acct_run_queue(acct))
282 raw_spin_unlock(&acct->lock);
287 * Check head of free list for an available worker. If one isn't available,
288 * caller must create one.
290 static bool io_acct_activate_free_worker(struct io_wq_acct *acct)
293 struct hlist_nulls_node *n;
294 struct io_worker *worker;
297 * Iterate free_list and see if we can find an idle worker to
298 * activate. If a given worker is on the free_list but in the process
299 * of exiting, keep trying.
301 hlist_nulls_for_each_entry_rcu(worker, n, &acct->free_list, nulls_node) {
302 if (!io_worker_get(worker))
305 * If the worker is already running, it's either already
306 * starting work or finishing work. In either case, if it does
307 * to go sleep, we'll kick off a new task for this work anyway.
309 wake_up_process(worker->task);
310 io_worker_release(worker);
318 * We need a worker. If we find a free one, we're good. If not, and we're
319 * below the max number of workers, create one.
321 static bool io_wq_create_worker(struct io_wq *wq, struct io_wq_acct *acct)
324 * Most likely an attempt to queue unbounded work on an io_wq that
325 * wasn't setup with any unbounded workers.
327 if (unlikely(!acct->max_workers))
328 pr_warn_once("io-wq is not configured for unbound workers");
330 raw_spin_lock(&acct->workers_lock);
331 if (acct->nr_workers >= acct->max_workers) {
332 raw_spin_unlock(&acct->workers_lock);
336 raw_spin_unlock(&acct->workers_lock);
337 atomic_inc(&acct->nr_running);
338 atomic_inc(&wq->worker_refs);
339 return create_io_worker(wq, acct);
342 static void io_wq_inc_running(struct io_worker *worker)
344 struct io_wq_acct *acct = io_wq_get_acct(worker);
346 atomic_inc(&acct->nr_running);
349 static void create_worker_cb(struct callback_head *cb)
351 struct io_worker *worker;
354 struct io_wq_acct *acct;
355 bool do_create = false;
357 worker = container_of(cb, struct io_worker, create_work);
360 raw_spin_lock(&acct->workers_lock);
362 if (acct->nr_workers < acct->max_workers) {
366 raw_spin_unlock(&acct->workers_lock);
368 create_io_worker(wq, acct);
370 atomic_dec(&acct->nr_running);
371 io_worker_ref_put(wq);
373 clear_bit_unlock(0, &worker->create_state);
374 io_worker_release(worker);
377 static bool io_queue_worker_create(struct io_worker *worker,
378 struct io_wq_acct *acct,
379 task_work_func_t func)
381 struct io_wq *wq = worker->wq;
383 /* raced with exit, just ignore create call */
384 if (test_bit(IO_WQ_BIT_EXIT, &wq->state))
386 if (!io_worker_get(worker))
389 * create_state manages ownership of create_work/index. We should
390 * only need one entry per worker, as the worker going to sleep
391 * will trigger the condition, and waking will clear it once it
392 * runs the task_work.
394 if (test_bit(0, &worker->create_state) ||
395 test_and_set_bit_lock(0, &worker->create_state))
398 atomic_inc(&wq->worker_refs);
399 init_task_work(&worker->create_work, func);
400 if (!task_work_add(wq->task, &worker->create_work, TWA_SIGNAL)) {
402 * EXIT may have been set after checking it above, check after
403 * adding the task_work and remove any creation item if it is
404 * now set. wq exit does that too, but we can have added this
405 * work item after we canceled in io_wq_exit_workers().
407 if (test_bit(IO_WQ_BIT_EXIT, &wq->state))
408 io_wq_cancel_tw_create(wq);
409 io_worker_ref_put(wq);
412 io_worker_ref_put(wq);
413 clear_bit_unlock(0, &worker->create_state);
415 io_worker_release(worker);
417 atomic_dec(&acct->nr_running);
418 io_worker_ref_put(wq);
422 /* Defer if current and next work are both hashed to the same chain */
423 static bool io_wq_hash_defer(struct io_wq_work *work, struct io_wq_acct *acct)
425 unsigned int hash, work_flags;
426 struct io_wq_work *next;
428 lockdep_assert_held(&acct->lock);
430 work_flags = atomic_read(&work->flags);
431 if (!__io_wq_is_hashed(work_flags))
434 /* should not happen, io_acct_run_queue() said we had work */
435 if (wq_list_empty(&acct->work_list))
438 hash = __io_get_work_hash(work_flags);
439 next = container_of(acct->work_list.first, struct io_wq_work, list);
440 work_flags = atomic_read(&next->flags);
441 if (!__io_wq_is_hashed(work_flags))
443 return hash == __io_get_work_hash(work_flags);
446 static void io_wq_dec_running(struct io_worker *worker)
448 struct io_wq_acct *acct = io_wq_get_acct(worker);
449 struct io_wq *wq = worker->wq;
451 if (!test_bit(IO_WORKER_F_UP, &worker->flags))
454 if (!atomic_dec_and_test(&acct->nr_running))
456 if (!worker->cur_work)
458 if (!io_acct_run_queue(acct))
460 if (io_wq_hash_defer(worker->cur_work, acct)) {
461 raw_spin_unlock(&acct->lock);
465 raw_spin_unlock(&acct->lock);
466 atomic_inc(&acct->nr_running);
467 atomic_inc(&wq->worker_refs);
468 io_queue_worker_create(worker, acct, create_worker_cb);
472 * Worker will start processing some work. Move it to the busy list, if
473 * it's currently on the freelist
475 static void __io_worker_busy(struct io_wq_acct *acct, struct io_worker *worker)
477 if (test_bit(IO_WORKER_F_FREE, &worker->flags)) {
478 clear_bit(IO_WORKER_F_FREE, &worker->flags);
479 raw_spin_lock(&acct->workers_lock);
480 hlist_nulls_del_init_rcu(&worker->nulls_node);
481 raw_spin_unlock(&acct->workers_lock);
486 * No work, worker going to sleep. Move to freelist.
488 static void __io_worker_idle(struct io_wq_acct *acct, struct io_worker *worker)
489 __must_hold(acct->workers_lock)
491 if (!test_bit(IO_WORKER_F_FREE, &worker->flags)) {
492 set_bit(IO_WORKER_F_FREE, &worker->flags);
493 hlist_nulls_add_head_rcu(&worker->nulls_node, &acct->free_list);
497 static bool io_wait_on_hash(struct io_wq *wq, unsigned int hash)
501 spin_lock_irq(&wq->hash->wait.lock);
502 if (list_empty(&wq->wait.entry)) {
503 __add_wait_queue(&wq->hash->wait, &wq->wait);
504 if (!test_bit(hash, &wq->hash->map)) {
505 __set_current_state(TASK_RUNNING);
506 list_del_init(&wq->wait.entry);
510 spin_unlock_irq(&wq->hash->wait.lock);
514 static struct io_wq_work *io_get_next_work(struct io_wq_acct *acct,
516 __must_hold(acct->lock)
518 struct io_wq_work_node *node, *prev;
519 struct io_wq_work *work, *tail;
520 unsigned int stall_hash = -1U;
522 wq_list_for_each(node, prev, &acct->work_list) {
523 unsigned int work_flags;
526 work = container_of(node, struct io_wq_work, list);
528 /* not hashed, can run anytime */
529 work_flags = atomic_read(&work->flags);
530 if (!__io_wq_is_hashed(work_flags)) {
531 wq_list_del(&acct->work_list, node, prev);
535 hash = __io_get_work_hash(work_flags);
536 /* all items with this hash lie in [work, tail] */
537 tail = wq->hash_tail[hash];
539 /* hashed, can run if not already running */
540 if (!test_and_set_bit(hash, &wq->hash->map)) {
541 wq->hash_tail[hash] = NULL;
542 wq_list_cut(&acct->work_list, &tail->list, prev);
545 if (stall_hash == -1U)
547 /* fast forward to a next hash, for-each will fix up @prev */
551 if (stall_hash != -1U) {
555 * Set this before dropping the lock to avoid racing with new
556 * work being added and clearing the stalled bit.
558 set_bit(IO_ACCT_STALLED_BIT, &acct->flags);
559 raw_spin_unlock(&acct->lock);
560 unstalled = io_wait_on_hash(wq, stall_hash);
561 raw_spin_lock(&acct->lock);
563 clear_bit(IO_ACCT_STALLED_BIT, &acct->flags);
564 if (wq_has_sleeper(&wq->hash->wait))
565 wake_up(&wq->hash->wait);
572 static void io_assign_current_work(struct io_worker *worker,
573 struct io_wq_work *work)
580 raw_spin_lock(&worker->lock);
581 worker->cur_work = work;
582 raw_spin_unlock(&worker->lock);
586 * Called with acct->lock held, drops it before returning
588 static void io_worker_handle_work(struct io_wq_acct *acct,
589 struct io_worker *worker)
590 __releases(&acct->lock)
592 struct io_wq *wq = worker->wq;
593 bool do_kill = test_bit(IO_WQ_BIT_EXIT, &wq->state);
596 struct io_wq_work *work;
599 * If we got some work, mark us as busy. If we didn't, but
600 * the list isn't empty, it means we stalled on hashed work.
601 * Mark us stalled so we don't keep looking for work when we
602 * can't make progress, any work completion or insertion will
603 * clear the stalled flag.
605 work = io_get_next_work(acct, wq);
608 * Make sure cancelation can find this, even before
609 * it becomes the active work. That avoids a window
610 * where the work has been removed from our general
611 * work list, but isn't yet discoverable as the
612 * current work item for this worker.
614 raw_spin_lock(&worker->lock);
615 worker->cur_work = work;
616 raw_spin_unlock(&worker->lock);
619 raw_spin_unlock(&acct->lock);
624 __io_worker_busy(acct, worker);
626 io_assign_current_work(worker, work);
627 __set_current_state(TASK_RUNNING);
629 /* handle a whole dependent link */
631 struct io_wq_work *next_hashed, *linked;
632 unsigned int work_flags = atomic_read(&work->flags);
633 unsigned int hash = __io_wq_is_hashed(work_flags)
634 ? __io_get_work_hash(work_flags)
637 next_hashed = wq_next_work(work);
640 (work_flags & IO_WQ_WORK_UNBOUND))
641 atomic_or(IO_WQ_WORK_CANCEL, &work->flags);
642 io_wq_submit_work(work);
643 io_assign_current_work(worker, NULL);
645 linked = io_wq_free_work(work);
647 if (!work && linked && !io_wq_is_hashed(linked)) {
651 io_assign_current_work(worker, work);
653 io_wq_enqueue(wq, linked);
655 if (hash != -1U && !next_hashed) {
656 /* serialize hash clear with wake_up() */
657 spin_lock_irq(&wq->hash->wait.lock);
658 clear_bit(hash, &wq->hash->map);
659 clear_bit(IO_ACCT_STALLED_BIT, &acct->flags);
660 spin_unlock_irq(&wq->hash->wait.lock);
661 if (wq_has_sleeper(&wq->hash->wait))
662 wake_up(&wq->hash->wait);
666 if (!__io_acct_run_queue(acct))
668 raw_spin_lock(&acct->lock);
672 static int io_wq_worker(void *data)
674 struct io_worker *worker = data;
675 struct io_wq_acct *acct = io_wq_get_acct(worker);
676 struct io_wq *wq = worker->wq;
677 bool exit_mask = false, last_timeout = false;
678 char buf[TASK_COMM_LEN] = {};
680 set_mask_bits(&worker->flags, 0,
681 BIT(IO_WORKER_F_UP) | BIT(IO_WORKER_F_RUNNING));
683 snprintf(buf, sizeof(buf), "iou-wrk-%d", wq->task->pid);
684 set_task_comm(current, buf);
686 while (!test_bit(IO_WQ_BIT_EXIT, &wq->state)) {
689 set_current_state(TASK_INTERRUPTIBLE);
692 * If we have work to do, io_acct_run_queue() returns with
693 * the acct->lock held. If not, it will drop it.
695 while (io_acct_run_queue(acct))
696 io_worker_handle_work(acct, worker);
698 raw_spin_lock(&acct->workers_lock);
700 * Last sleep timed out. Exit if we're not the last worker,
701 * or if someone modified our affinity.
703 if (last_timeout && (exit_mask || acct->nr_workers > 1)) {
705 raw_spin_unlock(&acct->workers_lock);
706 __set_current_state(TASK_RUNNING);
709 last_timeout = false;
710 __io_worker_idle(acct, worker);
711 raw_spin_unlock(&acct->workers_lock);
712 if (io_run_task_work())
714 ret = schedule_timeout(WORKER_IDLE_TIMEOUT);
715 if (signal_pending(current)) {
718 if (!get_signal(&ksig))
724 exit_mask = !cpumask_test_cpu(raw_smp_processor_id(),
729 if (test_bit(IO_WQ_BIT_EXIT, &wq->state) && io_acct_run_queue(acct))
730 io_worker_handle_work(acct, worker);
732 io_worker_exit(worker);
737 * Called when a worker is scheduled in. Mark us as currently running.
739 void io_wq_worker_running(struct task_struct *tsk)
741 struct io_worker *worker = tsk->worker_private;
745 if (!test_bit(IO_WORKER_F_UP, &worker->flags))
747 if (test_bit(IO_WORKER_F_RUNNING, &worker->flags))
749 set_bit(IO_WORKER_F_RUNNING, &worker->flags);
750 io_wq_inc_running(worker);
754 * Called when worker is going to sleep. If there are no workers currently
755 * running and we have work pending, wake up a free one or create a new one.
757 void io_wq_worker_sleeping(struct task_struct *tsk)
759 struct io_worker *worker = tsk->worker_private;
763 if (!test_bit(IO_WORKER_F_UP, &worker->flags))
765 if (!test_bit(IO_WORKER_F_RUNNING, &worker->flags))
768 clear_bit(IO_WORKER_F_RUNNING, &worker->flags);
769 io_wq_dec_running(worker);
772 static void io_init_new_worker(struct io_wq *wq, struct io_wq_acct *acct, struct io_worker *worker,
773 struct task_struct *tsk)
775 tsk->worker_private = worker;
777 set_cpus_allowed_ptr(tsk, wq->cpu_mask);
779 raw_spin_lock(&acct->workers_lock);
780 hlist_nulls_add_head_rcu(&worker->nulls_node, &acct->free_list);
781 list_add_tail_rcu(&worker->all_list, &acct->all_list);
782 set_bit(IO_WORKER_F_FREE, &worker->flags);
783 raw_spin_unlock(&acct->workers_lock);
784 wake_up_new_task(tsk);
787 static bool io_wq_work_match_all(struct io_wq_work *work, void *data)
792 static inline bool io_should_retry_thread(struct io_worker *worker, long err)
795 * Prevent perpetual task_work retry, if the task (or its group) is
798 if (fatal_signal_pending(current))
800 if (worker->init_retries++ >= WORKER_INIT_LIMIT)
806 case -ERESTARTNOINTR:
807 case -ERESTARTNOHAND:
814 static void queue_create_worker_retry(struct io_worker *worker)
817 * We only bother retrying because there's a chance that the
818 * failure to create a worker is due to some temporary condition
819 * in the forking task (e.g. outstanding signal); give the task
820 * some time to clear that condition.
822 schedule_delayed_work(&worker->work,
823 msecs_to_jiffies(worker->init_retries * 5));
826 static void create_worker_cont(struct callback_head *cb)
828 struct io_worker *worker;
829 struct task_struct *tsk;
831 struct io_wq_acct *acct;
833 worker = container_of(cb, struct io_worker, create_work);
834 clear_bit_unlock(0, &worker->create_state);
836 acct = io_wq_get_acct(worker);
837 tsk = create_io_thread(io_wq_worker, worker, NUMA_NO_NODE);
839 io_init_new_worker(wq, acct, worker, tsk);
840 io_worker_release(worker);
842 } else if (!io_should_retry_thread(worker, PTR_ERR(tsk))) {
843 atomic_dec(&acct->nr_running);
844 raw_spin_lock(&acct->workers_lock);
846 if (!acct->nr_workers) {
847 struct io_cb_cancel_data match = {
848 .fn = io_wq_work_match_all,
852 raw_spin_unlock(&acct->workers_lock);
853 while (io_acct_cancel_pending_work(wq, acct, &match))
856 raw_spin_unlock(&acct->workers_lock);
858 io_worker_ref_put(wq);
863 /* re-create attempts grab a new worker ref, drop the existing one */
864 io_worker_release(worker);
865 queue_create_worker_retry(worker);
868 static void io_workqueue_create(struct work_struct *work)
870 struct io_worker *worker = container_of(work, struct io_worker,
872 struct io_wq_acct *acct = io_wq_get_acct(worker);
874 if (!io_queue_worker_create(worker, acct, create_worker_cont))
878 static bool create_io_worker(struct io_wq *wq, struct io_wq_acct *acct)
880 struct io_worker *worker;
881 struct task_struct *tsk;
883 __set_current_state(TASK_RUNNING);
885 worker = kzalloc(sizeof(*worker), GFP_KERNEL);
888 atomic_dec(&acct->nr_running);
889 raw_spin_lock(&acct->workers_lock);
891 raw_spin_unlock(&acct->workers_lock);
892 io_worker_ref_put(wq);
896 refcount_set(&worker->ref, 1);
899 raw_spin_lock_init(&worker->lock);
900 init_completion(&worker->ref_done);
902 tsk = create_io_thread(io_wq_worker, worker, NUMA_NO_NODE);
904 io_init_new_worker(wq, acct, worker, tsk);
905 } else if (!io_should_retry_thread(worker, PTR_ERR(tsk))) {
909 INIT_DELAYED_WORK(&worker->work, io_workqueue_create);
910 queue_create_worker_retry(worker);
917 * Iterate the passed in list and call the specific function for each
918 * worker that isn't exiting
920 static bool io_acct_for_each_worker(struct io_wq_acct *acct,
921 bool (*func)(struct io_worker *, void *),
924 struct io_worker *worker;
927 list_for_each_entry_rcu(worker, &acct->all_list, all_list) {
928 if (io_worker_get(worker)) {
929 /* no task if node is/was offline */
931 ret = func(worker, data);
932 io_worker_release(worker);
941 static bool io_wq_for_each_worker(struct io_wq *wq,
942 bool (*func)(struct io_worker *, void *),
945 for (int i = 0; i < IO_WQ_ACCT_NR; i++) {
946 if (!io_acct_for_each_worker(&wq->acct[i], func, data))
953 static bool io_wq_worker_wake(struct io_worker *worker, void *data)
955 __set_notify_signal(worker->task);
956 wake_up_process(worker->task);
960 static void io_run_cancel(struct io_wq_work *work, struct io_wq *wq)
963 atomic_or(IO_WQ_WORK_CANCEL, &work->flags);
964 io_wq_submit_work(work);
965 work = io_wq_free_work(work);
969 static void io_wq_insert_work(struct io_wq *wq, struct io_wq_acct *acct,
970 struct io_wq_work *work, unsigned int work_flags)
973 struct io_wq_work *tail;
975 if (!__io_wq_is_hashed(work_flags)) {
977 wq_list_add_tail(&work->list, &acct->work_list);
981 hash = __io_get_work_hash(work_flags);
982 tail = wq->hash_tail[hash];
983 wq->hash_tail[hash] = work;
987 wq_list_add_after(&work->list, &tail->list, &acct->work_list);
990 static bool io_wq_work_match_item(struct io_wq_work *work, void *data)
995 void io_wq_enqueue(struct io_wq *wq, struct io_wq_work *work)
997 unsigned int work_flags = atomic_read(&work->flags);
998 struct io_wq_acct *acct = io_work_get_acct(wq, work_flags);
999 struct io_cb_cancel_data match = {
1000 .fn = io_wq_work_match_item,
1002 .cancel_all = false,
1007 * If io-wq is exiting for this task, or if the request has explicitly
1008 * been marked as one that should not get executed, cancel it here.
1010 if (test_bit(IO_WQ_BIT_EXIT, &wq->state) ||
1011 (work_flags & IO_WQ_WORK_CANCEL)) {
1012 io_run_cancel(work, wq);
1016 raw_spin_lock(&acct->lock);
1017 io_wq_insert_work(wq, acct, work, work_flags);
1018 clear_bit(IO_ACCT_STALLED_BIT, &acct->flags);
1019 raw_spin_unlock(&acct->lock);
1022 do_create = !io_acct_activate_free_worker(acct);
1025 if (do_create && ((work_flags & IO_WQ_WORK_CONCURRENT) ||
1026 !atomic_read(&acct->nr_running))) {
1029 did_create = io_wq_create_worker(wq, acct);
1030 if (likely(did_create))
1033 raw_spin_lock(&acct->workers_lock);
1034 if (acct->nr_workers) {
1035 raw_spin_unlock(&acct->workers_lock);
1038 raw_spin_unlock(&acct->workers_lock);
1040 /* fatal condition, failed to create the first worker */
1041 io_acct_cancel_pending_work(wq, acct, &match);
1046 * Work items that hash to the same value will not be done in parallel.
1047 * Used to limit concurrent writes, generally hashed by inode.
1049 void io_wq_hash_work(struct io_wq_work *work, void *val)
1053 bit = hash_ptr(val, IO_WQ_HASH_ORDER);
1054 atomic_or(IO_WQ_WORK_HASHED | (bit << IO_WQ_HASH_SHIFT), &work->flags);
1057 static bool __io_wq_worker_cancel(struct io_worker *worker,
1058 struct io_cb_cancel_data *match,
1059 struct io_wq_work *work)
1061 if (work && match->fn(work, match->data)) {
1062 atomic_or(IO_WQ_WORK_CANCEL, &work->flags);
1063 __set_notify_signal(worker->task);
1070 static bool io_wq_worker_cancel(struct io_worker *worker, void *data)
1072 struct io_cb_cancel_data *match = data;
1075 * Hold the lock to avoid ->cur_work going out of scope, caller
1076 * may dereference the passed in work.
1078 raw_spin_lock(&worker->lock);
1079 if (__io_wq_worker_cancel(worker, match, worker->cur_work))
1080 match->nr_running++;
1081 raw_spin_unlock(&worker->lock);
1083 return match->nr_running && !match->cancel_all;
1086 static inline void io_wq_remove_pending(struct io_wq *wq,
1087 struct io_wq_acct *acct,
1088 struct io_wq_work *work,
1089 struct io_wq_work_node *prev)
1091 unsigned int hash = io_get_work_hash(work);
1092 struct io_wq_work *prev_work = NULL;
1094 if (io_wq_is_hashed(work) && work == wq->hash_tail[hash]) {
1096 prev_work = container_of(prev, struct io_wq_work, list);
1097 if (prev_work && io_get_work_hash(prev_work) == hash)
1098 wq->hash_tail[hash] = prev_work;
1100 wq->hash_tail[hash] = NULL;
1102 wq_list_del(&acct->work_list, &work->list, prev);
1105 static bool io_acct_cancel_pending_work(struct io_wq *wq,
1106 struct io_wq_acct *acct,
1107 struct io_cb_cancel_data *match)
1109 struct io_wq_work_node *node, *prev;
1110 struct io_wq_work *work;
1112 raw_spin_lock(&acct->lock);
1113 wq_list_for_each(node, prev, &acct->work_list) {
1114 work = container_of(node, struct io_wq_work, list);
1115 if (!match->fn(work, match->data))
1117 io_wq_remove_pending(wq, acct, work, prev);
1118 raw_spin_unlock(&acct->lock);
1119 io_run_cancel(work, wq);
1120 match->nr_pending++;
1121 /* not safe to continue after unlock */
1124 raw_spin_unlock(&acct->lock);
1129 static void io_wq_cancel_pending_work(struct io_wq *wq,
1130 struct io_cb_cancel_data *match)
1134 for (i = 0; i < IO_WQ_ACCT_NR; i++) {
1135 struct io_wq_acct *acct = io_get_acct(wq, i == 0);
1137 if (io_acct_cancel_pending_work(wq, acct, match)) {
1138 if (match->cancel_all)
1145 static void io_acct_cancel_running_work(struct io_wq_acct *acct,
1146 struct io_cb_cancel_data *match)
1148 raw_spin_lock(&acct->workers_lock);
1149 io_acct_for_each_worker(acct, io_wq_worker_cancel, match);
1150 raw_spin_unlock(&acct->workers_lock);
1153 static void io_wq_cancel_running_work(struct io_wq *wq,
1154 struct io_cb_cancel_data *match)
1158 for (int i = 0; i < IO_WQ_ACCT_NR; i++)
1159 io_acct_cancel_running_work(&wq->acct[i], match);
1164 enum io_wq_cancel io_wq_cancel_cb(struct io_wq *wq, work_cancel_fn *cancel,
1165 void *data, bool cancel_all)
1167 struct io_cb_cancel_data match = {
1170 .cancel_all = cancel_all,
1174 * First check pending list, if we're lucky we can just remove it
1175 * from there. CANCEL_OK means that the work is returned as-new,
1176 * no completion will be posted for it.
1178 * Then check if a free (going busy) or busy worker has the work
1179 * currently running. If we find it there, we'll return CANCEL_RUNNING
1180 * as an indication that we attempt to signal cancellation. The
1181 * completion will run normally in this case.
1183 * Do both of these while holding the acct->workers_lock, to ensure that
1184 * we'll find a work item regardless of state.
1186 io_wq_cancel_pending_work(wq, &match);
1187 if (match.nr_pending && !match.cancel_all)
1188 return IO_WQ_CANCEL_OK;
1190 io_wq_cancel_running_work(wq, &match);
1191 if (match.nr_running && !match.cancel_all)
1192 return IO_WQ_CANCEL_RUNNING;
1194 if (match.nr_running)
1195 return IO_WQ_CANCEL_RUNNING;
1196 if (match.nr_pending)
1197 return IO_WQ_CANCEL_OK;
1198 return IO_WQ_CANCEL_NOTFOUND;
1201 static int io_wq_hash_wake(struct wait_queue_entry *wait, unsigned mode,
1202 int sync, void *key)
1204 struct io_wq *wq = container_of(wait, struct io_wq, wait);
1207 list_del_init(&wait->entry);
1210 for (i = 0; i < IO_WQ_ACCT_NR; i++) {
1211 struct io_wq_acct *acct = &wq->acct[i];
1213 if (test_and_clear_bit(IO_ACCT_STALLED_BIT, &acct->flags))
1214 io_acct_activate_free_worker(acct);
1220 struct io_wq *io_wq_create(unsigned bounded, struct io_wq_data *data)
1225 if (WARN_ON_ONCE(!bounded))
1226 return ERR_PTR(-EINVAL);
1228 wq = kzalloc(sizeof(struct io_wq), GFP_KERNEL);
1230 return ERR_PTR(-ENOMEM);
1232 refcount_inc(&data->hash->refs);
1233 wq->hash = data->hash;
1237 if (!alloc_cpumask_var(&wq->cpu_mask, GFP_KERNEL))
1239 cpuset_cpus_allowed(data->task, wq->cpu_mask);
1240 wq->acct[IO_WQ_ACCT_BOUND].max_workers = bounded;
1241 wq->acct[IO_WQ_ACCT_UNBOUND].max_workers =
1242 task_rlimit(current, RLIMIT_NPROC);
1243 INIT_LIST_HEAD(&wq->wait.entry);
1244 wq->wait.func = io_wq_hash_wake;
1245 for (i = 0; i < IO_WQ_ACCT_NR; i++) {
1246 struct io_wq_acct *acct = &wq->acct[i];
1248 atomic_set(&acct->nr_running, 0);
1250 raw_spin_lock_init(&acct->workers_lock);
1251 INIT_HLIST_NULLS_HEAD(&acct->free_list, 0);
1252 INIT_LIST_HEAD(&acct->all_list);
1254 INIT_WQ_LIST(&acct->work_list);
1255 raw_spin_lock_init(&acct->lock);
1258 wq->task = get_task_struct(data->task);
1259 atomic_set(&wq->worker_refs, 1);
1260 init_completion(&wq->worker_done);
1261 ret = cpuhp_state_add_instance_nocalls(io_wq_online, &wq->cpuhp_node);
1263 put_task_struct(wq->task);
1269 io_wq_put_hash(data->hash);
1270 free_cpumask_var(wq->cpu_mask);
1272 return ERR_PTR(ret);
1275 static bool io_task_work_match(struct callback_head *cb, void *data)
1277 struct io_worker *worker;
1279 if (cb->func != create_worker_cb && cb->func != create_worker_cont)
1281 worker = container_of(cb, struct io_worker, create_work);
1282 return worker->wq == data;
1285 void io_wq_exit_start(struct io_wq *wq)
1287 set_bit(IO_WQ_BIT_EXIT, &wq->state);
1290 static void io_wq_cancel_tw_create(struct io_wq *wq)
1292 struct callback_head *cb;
1294 while ((cb = task_work_cancel_match(wq->task, io_task_work_match, wq)) != NULL) {
1295 struct io_worker *worker;
1297 worker = container_of(cb, struct io_worker, create_work);
1298 io_worker_cancel_cb(worker);
1300 * Only the worker continuation helper has worker allocated and
1301 * hence needs freeing.
1303 if (cb->func == create_worker_cont)
1308 static void io_wq_exit_workers(struct io_wq *wq)
1313 io_wq_cancel_tw_create(wq);
1316 io_wq_for_each_worker(wq, io_wq_worker_wake, NULL);
1318 io_worker_ref_put(wq);
1319 wait_for_completion(&wq->worker_done);
1321 spin_lock_irq(&wq->hash->wait.lock);
1322 list_del_init(&wq->wait.entry);
1323 spin_unlock_irq(&wq->hash->wait.lock);
1325 put_task_struct(wq->task);
1329 static void io_wq_destroy(struct io_wq *wq)
1331 struct io_cb_cancel_data match = {
1332 .fn = io_wq_work_match_all,
1336 cpuhp_state_remove_instance_nocalls(io_wq_online, &wq->cpuhp_node);
1337 io_wq_cancel_pending_work(wq, &match);
1338 free_cpumask_var(wq->cpu_mask);
1339 io_wq_put_hash(wq->hash);
1343 void io_wq_put_and_exit(struct io_wq *wq)
1345 WARN_ON_ONCE(!test_bit(IO_WQ_BIT_EXIT, &wq->state));
1347 io_wq_exit_workers(wq);
1351 struct online_data {
1356 static bool io_wq_worker_affinity(struct io_worker *worker, void *data)
1358 struct online_data *od = data;
1361 cpumask_set_cpu(od->cpu, worker->wq->cpu_mask);
1363 cpumask_clear_cpu(od->cpu, worker->wq->cpu_mask);
1367 static int __io_wq_cpu_online(struct io_wq *wq, unsigned int cpu, bool online)
1369 struct online_data od = {
1375 io_wq_for_each_worker(wq, io_wq_worker_affinity, &od);
1380 static int io_wq_cpu_online(unsigned int cpu, struct hlist_node *node)
1382 struct io_wq *wq = hlist_entry_safe(node, struct io_wq, cpuhp_node);
1384 return __io_wq_cpu_online(wq, cpu, true);
1387 static int io_wq_cpu_offline(unsigned int cpu, struct hlist_node *node)
1389 struct io_wq *wq = hlist_entry_safe(node, struct io_wq, cpuhp_node);
1391 return __io_wq_cpu_online(wq, cpu, false);
1394 int io_wq_cpu_affinity(struct io_uring_task *tctx, cpumask_var_t mask)
1396 cpumask_var_t allowed_mask;
1399 if (!tctx || !tctx->io_wq)
1402 if (!alloc_cpumask_var(&allowed_mask, GFP_KERNEL))
1406 cpuset_cpus_allowed(tctx->io_wq->task, allowed_mask);
1408 if (cpumask_subset(mask, allowed_mask))
1409 cpumask_copy(tctx->io_wq->cpu_mask, mask);
1413 cpumask_copy(tctx->io_wq->cpu_mask, allowed_mask);
1417 free_cpumask_var(allowed_mask);
1422 * Set max number of unbounded workers, returns old value. If new_count is 0,
1423 * then just return the old value.
1425 int io_wq_max_workers(struct io_wq *wq, int *new_count)
1427 struct io_wq_acct *acct;
1428 int prev[IO_WQ_ACCT_NR];
1431 BUILD_BUG_ON((int) IO_WQ_ACCT_BOUND != (int) IO_WQ_BOUND);
1432 BUILD_BUG_ON((int) IO_WQ_ACCT_UNBOUND != (int) IO_WQ_UNBOUND);
1433 BUILD_BUG_ON((int) IO_WQ_ACCT_NR != 2);
1435 for (i = 0; i < IO_WQ_ACCT_NR; i++) {
1436 if (new_count[i] > task_rlimit(current, RLIMIT_NPROC))
1437 new_count[i] = task_rlimit(current, RLIMIT_NPROC);
1440 for (i = 0; i < IO_WQ_ACCT_NR; i++)
1445 for (i = 0; i < IO_WQ_ACCT_NR; i++) {
1446 acct = &wq->acct[i];
1447 raw_spin_lock(&acct->workers_lock);
1448 prev[i] = max_t(int, acct->max_workers, prev[i]);
1450 acct->max_workers = new_count[i];
1451 raw_spin_unlock(&acct->workers_lock);
1455 for (i = 0; i < IO_WQ_ACCT_NR; i++)
1456 new_count[i] = prev[i];
1461 static __init int io_wq_init(void)
1465 ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "io-wq/online",
1466 io_wq_cpu_online, io_wq_cpu_offline);
1472 subsys_initcall(io_wq_init);