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);
224 * this worker is a goner, clear ->worker_private to avoid any
225 * inc/dec running calls that could happen as part of exit from
228 current->worker_private = NULL;
230 kfree_rcu(worker, rcu);
231 io_worker_ref_put(wq);
235 static inline bool __io_acct_run_queue(struct io_wq_acct *acct)
237 return !test_bit(IO_ACCT_STALLED_BIT, &acct->flags) &&
238 !wq_list_empty(&acct->work_list);
242 * If there's work to do, returns true with acct->lock acquired. If not,
243 * returns false with no lock held.
245 static inline bool io_acct_run_queue(struct io_wq_acct *acct)
246 __acquires(&acct->lock)
248 raw_spin_lock(&acct->lock);
249 if (__io_acct_run_queue(acct))
252 raw_spin_unlock(&acct->lock);
257 * Check head of free list for an available worker. If one isn't available,
258 * caller must create one.
260 static bool io_wq_activate_free_worker(struct io_wq *wq,
261 struct io_wq_acct *acct)
264 struct hlist_nulls_node *n;
265 struct io_worker *worker;
268 * Iterate free_list and see if we can find an idle worker to
269 * activate. If a given worker is on the free_list but in the process
270 * of exiting, keep trying.
272 hlist_nulls_for_each_entry_rcu(worker, n, &wq->free_list, nulls_node) {
273 if (!io_worker_get(worker))
275 if (io_wq_get_acct(worker) != acct) {
276 io_worker_release(worker);
280 * If the worker is already running, it's either already
281 * starting work or finishing work. In either case, if it does
282 * to go sleep, we'll kick off a new task for this work anyway.
284 wake_up_process(worker->task);
285 io_worker_release(worker);
293 * We need a worker. If we find a free one, we're good. If not, and we're
294 * below the max number of workers, create one.
296 static bool io_wq_create_worker(struct io_wq *wq, struct io_wq_acct *acct)
299 * Most likely an attempt to queue unbounded work on an io_wq that
300 * wasn't setup with any unbounded workers.
302 if (unlikely(!acct->max_workers))
303 pr_warn_once("io-wq is not configured for unbound workers");
305 raw_spin_lock(&wq->lock);
306 if (acct->nr_workers >= acct->max_workers) {
307 raw_spin_unlock(&wq->lock);
311 raw_spin_unlock(&wq->lock);
312 atomic_inc(&acct->nr_running);
313 atomic_inc(&wq->worker_refs);
314 return create_io_worker(wq, acct->index);
317 static void io_wq_inc_running(struct io_worker *worker)
319 struct io_wq_acct *acct = io_wq_get_acct(worker);
321 atomic_inc(&acct->nr_running);
324 static void create_worker_cb(struct callback_head *cb)
326 struct io_worker *worker;
329 struct io_wq_acct *acct;
330 bool do_create = false;
332 worker = container_of(cb, struct io_worker, create_work);
334 acct = &wq->acct[worker->create_index];
335 raw_spin_lock(&wq->lock);
337 if (acct->nr_workers < acct->max_workers) {
341 raw_spin_unlock(&wq->lock);
343 create_io_worker(wq, worker->create_index);
345 atomic_dec(&acct->nr_running);
346 io_worker_ref_put(wq);
348 clear_bit_unlock(0, &worker->create_state);
349 io_worker_release(worker);
352 static bool io_queue_worker_create(struct io_worker *worker,
353 struct io_wq_acct *acct,
354 task_work_func_t func)
356 struct io_wq *wq = worker->wq;
358 /* raced with exit, just ignore create call */
359 if (test_bit(IO_WQ_BIT_EXIT, &wq->state))
361 if (!io_worker_get(worker))
364 * create_state manages ownership of create_work/index. We should
365 * only need one entry per worker, as the worker going to sleep
366 * will trigger the condition, and waking will clear it once it
367 * runs the task_work.
369 if (test_bit(0, &worker->create_state) ||
370 test_and_set_bit_lock(0, &worker->create_state))
373 atomic_inc(&wq->worker_refs);
374 init_task_work(&worker->create_work, func);
375 worker->create_index = acct->index;
376 if (!task_work_add(wq->task, &worker->create_work, TWA_SIGNAL)) {
378 * EXIT may have been set after checking it above, check after
379 * adding the task_work and remove any creation item if it is
380 * now set. wq exit does that too, but we can have added this
381 * work item after we canceled in io_wq_exit_workers().
383 if (test_bit(IO_WQ_BIT_EXIT, &wq->state))
384 io_wq_cancel_tw_create(wq);
385 io_worker_ref_put(wq);
388 io_worker_ref_put(wq);
389 clear_bit_unlock(0, &worker->create_state);
391 io_worker_release(worker);
393 atomic_dec(&acct->nr_running);
394 io_worker_ref_put(wq);
398 static void io_wq_dec_running(struct io_worker *worker)
400 struct io_wq_acct *acct = io_wq_get_acct(worker);
401 struct io_wq *wq = worker->wq;
403 if (!(worker->flags & IO_WORKER_F_UP))
406 if (!atomic_dec_and_test(&acct->nr_running))
408 if (!io_acct_run_queue(acct))
411 raw_spin_unlock(&acct->lock);
412 atomic_inc(&acct->nr_running);
413 atomic_inc(&wq->worker_refs);
414 io_queue_worker_create(worker, acct, create_worker_cb);
418 * Worker will start processing some work. Move it to the busy list, if
419 * it's currently on the freelist
421 static void __io_worker_busy(struct io_wq *wq, struct io_worker *worker)
423 if (worker->flags & IO_WORKER_F_FREE) {
424 worker->flags &= ~IO_WORKER_F_FREE;
425 raw_spin_lock(&wq->lock);
426 hlist_nulls_del_init_rcu(&worker->nulls_node);
427 raw_spin_unlock(&wq->lock);
432 * No work, worker going to sleep. Move to freelist.
434 static void __io_worker_idle(struct io_wq *wq, struct io_worker *worker)
435 __must_hold(wq->lock)
437 if (!(worker->flags & IO_WORKER_F_FREE)) {
438 worker->flags |= IO_WORKER_F_FREE;
439 hlist_nulls_add_head_rcu(&worker->nulls_node, &wq->free_list);
443 static inline unsigned int io_get_work_hash(struct io_wq_work *work)
445 return work->flags >> IO_WQ_HASH_SHIFT;
448 static bool io_wait_on_hash(struct io_wq *wq, unsigned int hash)
452 spin_lock_irq(&wq->hash->wait.lock);
453 if (list_empty(&wq->wait.entry)) {
454 __add_wait_queue(&wq->hash->wait, &wq->wait);
455 if (!test_bit(hash, &wq->hash->map)) {
456 __set_current_state(TASK_RUNNING);
457 list_del_init(&wq->wait.entry);
461 spin_unlock_irq(&wq->hash->wait.lock);
465 static struct io_wq_work *io_get_next_work(struct io_wq_acct *acct,
466 struct io_worker *worker)
467 __must_hold(acct->lock)
469 struct io_wq_work_node *node, *prev;
470 struct io_wq_work *work, *tail;
471 unsigned int stall_hash = -1U;
472 struct io_wq *wq = worker->wq;
474 wq_list_for_each(node, prev, &acct->work_list) {
477 work = container_of(node, struct io_wq_work, list);
479 /* not hashed, can run anytime */
480 if (!io_wq_is_hashed(work)) {
481 wq_list_del(&acct->work_list, node, prev);
485 hash = io_get_work_hash(work);
486 /* all items with this hash lie in [work, tail] */
487 tail = wq->hash_tail[hash];
489 /* hashed, can run if not already running */
490 if (!test_and_set_bit(hash, &wq->hash->map)) {
491 wq->hash_tail[hash] = NULL;
492 wq_list_cut(&acct->work_list, &tail->list, prev);
495 if (stall_hash == -1U)
497 /* fast forward to a next hash, for-each will fix up @prev */
501 if (stall_hash != -1U) {
505 * Set this before dropping the lock to avoid racing with new
506 * work being added and clearing the stalled bit.
508 set_bit(IO_ACCT_STALLED_BIT, &acct->flags);
509 raw_spin_unlock(&acct->lock);
510 unstalled = io_wait_on_hash(wq, stall_hash);
511 raw_spin_lock(&acct->lock);
513 clear_bit(IO_ACCT_STALLED_BIT, &acct->flags);
514 if (wq_has_sleeper(&wq->hash->wait))
515 wake_up(&wq->hash->wait);
522 static void io_assign_current_work(struct io_worker *worker,
523 struct io_wq_work *work)
530 raw_spin_lock(&worker->lock);
531 worker->cur_work = work;
532 worker->next_work = NULL;
533 raw_spin_unlock(&worker->lock);
537 * Called with acct->lock held, drops it before returning
539 static void io_worker_handle_work(struct io_wq_acct *acct,
540 struct io_worker *worker)
541 __releases(&acct->lock)
543 struct io_wq *wq = worker->wq;
544 bool do_kill = test_bit(IO_WQ_BIT_EXIT, &wq->state);
547 struct io_wq_work *work;
550 * If we got some work, mark us as busy. If we didn't, but
551 * the list isn't empty, it means we stalled on hashed work.
552 * Mark us stalled so we don't keep looking for work when we
553 * can't make progress, any work completion or insertion will
554 * clear the stalled flag.
556 work = io_get_next_work(acct, worker);
557 raw_spin_unlock(&acct->lock);
559 __io_worker_busy(wq, worker);
562 * Make sure cancelation can find this, even before
563 * it becomes the active work. That avoids a window
564 * where the work has been removed from our general
565 * work list, but isn't yet discoverable as the
566 * current work item for this worker.
568 raw_spin_lock(&worker->lock);
569 worker->next_work = work;
570 raw_spin_unlock(&worker->lock);
574 io_assign_current_work(worker, work);
575 __set_current_state(TASK_RUNNING);
577 /* handle a whole dependent link */
579 struct io_wq_work *next_hashed, *linked;
580 unsigned int hash = io_get_work_hash(work);
582 next_hashed = wq_next_work(work);
584 if (unlikely(do_kill) && (work->flags & IO_WQ_WORK_UNBOUND))
585 work->flags |= IO_WQ_WORK_CANCEL;
587 io_assign_current_work(worker, NULL);
589 linked = wq->free_work(work);
591 if (!work && linked && !io_wq_is_hashed(linked)) {
595 io_assign_current_work(worker, work);
597 io_wq_enqueue(wq, linked);
599 if (hash != -1U && !next_hashed) {
600 /* serialize hash clear with wake_up() */
601 spin_lock_irq(&wq->hash->wait.lock);
602 clear_bit(hash, &wq->hash->map);
603 clear_bit(IO_ACCT_STALLED_BIT, &acct->flags);
604 spin_unlock_irq(&wq->hash->wait.lock);
605 if (wq_has_sleeper(&wq->hash->wait))
606 wake_up(&wq->hash->wait);
610 if (!__io_acct_run_queue(acct))
612 raw_spin_lock(&acct->lock);
616 static int io_wq_worker(void *data)
618 struct io_worker *worker = data;
619 struct io_wq_acct *acct = io_wq_get_acct(worker);
620 struct io_wq *wq = worker->wq;
621 bool exit_mask = false, last_timeout = false;
622 char buf[TASK_COMM_LEN];
624 worker->flags |= (IO_WORKER_F_UP | IO_WORKER_F_RUNNING);
626 snprintf(buf, sizeof(buf), "iou-wrk-%d", wq->task->pid);
627 set_task_comm(current, buf);
629 while (!test_bit(IO_WQ_BIT_EXIT, &wq->state)) {
632 set_current_state(TASK_INTERRUPTIBLE);
635 * If we have work to do, io_acct_run_queue() returns with
636 * the acct->lock held. If not, it will drop it.
638 while (io_acct_run_queue(acct))
639 io_worker_handle_work(acct, worker);
641 raw_spin_lock(&wq->lock);
643 * Last sleep timed out. Exit if we're not the last worker,
644 * or if someone modified our affinity.
646 if (last_timeout && (exit_mask || acct->nr_workers > 1)) {
648 raw_spin_unlock(&wq->lock);
649 __set_current_state(TASK_RUNNING);
652 last_timeout = false;
653 __io_worker_idle(wq, worker);
654 raw_spin_unlock(&wq->lock);
655 if (io_run_task_work())
657 ret = schedule_timeout(WORKER_IDLE_TIMEOUT);
658 if (signal_pending(current)) {
661 if (!get_signal(&ksig))
667 exit_mask = !cpumask_test_cpu(raw_smp_processor_id(),
672 if (test_bit(IO_WQ_BIT_EXIT, &wq->state) && io_acct_run_queue(acct))
673 io_worker_handle_work(acct, worker);
675 io_worker_exit(worker);
680 * Called when a worker is scheduled in. Mark us as currently running.
682 void io_wq_worker_running(struct task_struct *tsk)
684 struct io_worker *worker = tsk->worker_private;
688 if (!(worker->flags & IO_WORKER_F_UP))
690 if (worker->flags & IO_WORKER_F_RUNNING)
692 worker->flags |= IO_WORKER_F_RUNNING;
693 io_wq_inc_running(worker);
697 * Called when worker is going to sleep. If there are no workers currently
698 * running and we have work pending, wake up a free one or create a new one.
700 void io_wq_worker_sleeping(struct task_struct *tsk)
702 struct io_worker *worker = tsk->worker_private;
706 if (!(worker->flags & IO_WORKER_F_UP))
708 if (!(worker->flags & IO_WORKER_F_RUNNING))
711 worker->flags &= ~IO_WORKER_F_RUNNING;
712 io_wq_dec_running(worker);
715 static void io_init_new_worker(struct io_wq *wq, struct io_worker *worker,
716 struct task_struct *tsk)
718 tsk->worker_private = worker;
720 set_cpus_allowed_ptr(tsk, wq->cpu_mask);
722 raw_spin_lock(&wq->lock);
723 hlist_nulls_add_head_rcu(&worker->nulls_node, &wq->free_list);
724 list_add_tail_rcu(&worker->all_list, &wq->all_list);
725 worker->flags |= IO_WORKER_F_FREE;
726 raw_spin_unlock(&wq->lock);
727 wake_up_new_task(tsk);
730 static bool io_wq_work_match_all(struct io_wq_work *work, void *data)
735 static inline bool io_should_retry_thread(long err)
738 * Prevent perpetual task_work retry, if the task (or its group) is
741 if (fatal_signal_pending(current))
747 case -ERESTARTNOINTR:
748 case -ERESTARTNOHAND:
755 static void create_worker_cont(struct callback_head *cb)
757 struct io_worker *worker;
758 struct task_struct *tsk;
761 worker = container_of(cb, struct io_worker, create_work);
762 clear_bit_unlock(0, &worker->create_state);
764 tsk = create_io_thread(io_wq_worker, worker, NUMA_NO_NODE);
766 io_init_new_worker(wq, worker, tsk);
767 io_worker_release(worker);
769 } else if (!io_should_retry_thread(PTR_ERR(tsk))) {
770 struct io_wq_acct *acct = io_wq_get_acct(worker);
772 atomic_dec(&acct->nr_running);
773 raw_spin_lock(&wq->lock);
775 if (!acct->nr_workers) {
776 struct io_cb_cancel_data match = {
777 .fn = io_wq_work_match_all,
781 raw_spin_unlock(&wq->lock);
782 while (io_acct_cancel_pending_work(wq, acct, &match))
785 raw_spin_unlock(&wq->lock);
787 io_worker_ref_put(wq);
792 /* re-create attempts grab a new worker ref, drop the existing one */
793 io_worker_release(worker);
794 schedule_work(&worker->work);
797 static void io_workqueue_create(struct work_struct *work)
799 struct io_worker *worker = container_of(work, struct io_worker, work);
800 struct io_wq_acct *acct = io_wq_get_acct(worker);
802 if (!io_queue_worker_create(worker, acct, create_worker_cont))
806 static bool create_io_worker(struct io_wq *wq, int index)
808 struct io_wq_acct *acct = &wq->acct[index];
809 struct io_worker *worker;
810 struct task_struct *tsk;
812 __set_current_state(TASK_RUNNING);
814 worker = kzalloc(sizeof(*worker), GFP_KERNEL);
817 atomic_dec(&acct->nr_running);
818 raw_spin_lock(&wq->lock);
820 raw_spin_unlock(&wq->lock);
821 io_worker_ref_put(wq);
825 refcount_set(&worker->ref, 1);
827 raw_spin_lock_init(&worker->lock);
828 init_completion(&worker->ref_done);
830 if (index == IO_WQ_ACCT_BOUND)
831 worker->flags |= IO_WORKER_F_BOUND;
833 tsk = create_io_thread(io_wq_worker, worker, NUMA_NO_NODE);
835 io_init_new_worker(wq, worker, tsk);
836 } else if (!io_should_retry_thread(PTR_ERR(tsk))) {
840 INIT_WORK(&worker->work, io_workqueue_create);
841 schedule_work(&worker->work);
848 * Iterate the passed in list and call the specific function for each
849 * worker that isn't exiting
851 static bool io_wq_for_each_worker(struct io_wq *wq,
852 bool (*func)(struct io_worker *, void *),
855 struct io_worker *worker;
858 list_for_each_entry_rcu(worker, &wq->all_list, all_list) {
859 if (io_worker_get(worker)) {
860 /* no task if node is/was offline */
862 ret = func(worker, data);
863 io_worker_release(worker);
872 static bool io_wq_worker_wake(struct io_worker *worker, void *data)
874 __set_notify_signal(worker->task);
875 wake_up_process(worker->task);
879 static void io_run_cancel(struct io_wq_work *work, struct io_wq *wq)
882 work->flags |= IO_WQ_WORK_CANCEL;
884 work = wq->free_work(work);
888 static void io_wq_insert_work(struct io_wq *wq, struct io_wq_work *work)
890 struct io_wq_acct *acct = io_work_get_acct(wq, work);
892 struct io_wq_work *tail;
894 if (!io_wq_is_hashed(work)) {
896 wq_list_add_tail(&work->list, &acct->work_list);
900 hash = io_get_work_hash(work);
901 tail = wq->hash_tail[hash];
902 wq->hash_tail[hash] = work;
906 wq_list_add_after(&work->list, &tail->list, &acct->work_list);
909 static bool io_wq_work_match_item(struct io_wq_work *work, void *data)
914 void io_wq_enqueue(struct io_wq *wq, struct io_wq_work *work)
916 struct io_wq_acct *acct = io_work_get_acct(wq, work);
917 struct io_cb_cancel_data match;
918 unsigned work_flags = work->flags;
922 * If io-wq is exiting for this task, or if the request has explicitly
923 * been marked as one that should not get executed, cancel it here.
925 if (test_bit(IO_WQ_BIT_EXIT, &wq->state) ||
926 (work->flags & IO_WQ_WORK_CANCEL)) {
927 io_run_cancel(work, wq);
931 raw_spin_lock(&acct->lock);
932 io_wq_insert_work(wq, work);
933 clear_bit(IO_ACCT_STALLED_BIT, &acct->flags);
934 raw_spin_unlock(&acct->lock);
937 do_create = !io_wq_activate_free_worker(wq, acct);
940 if (do_create && ((work_flags & IO_WQ_WORK_CONCURRENT) ||
941 !atomic_read(&acct->nr_running))) {
944 did_create = io_wq_create_worker(wq, acct);
945 if (likely(did_create))
948 raw_spin_lock(&wq->lock);
949 if (acct->nr_workers) {
950 raw_spin_unlock(&wq->lock);
953 raw_spin_unlock(&wq->lock);
955 /* fatal condition, failed to create the first worker */
956 match.fn = io_wq_work_match_item,
958 match.cancel_all = false,
960 io_acct_cancel_pending_work(wq, acct, &match);
965 * Work items that hash to the same value will not be done in parallel.
966 * Used to limit concurrent writes, generally hashed by inode.
968 void io_wq_hash_work(struct io_wq_work *work, void *val)
972 bit = hash_ptr(val, IO_WQ_HASH_ORDER);
973 work->flags |= (IO_WQ_WORK_HASHED | (bit << IO_WQ_HASH_SHIFT));
976 static bool __io_wq_worker_cancel(struct io_worker *worker,
977 struct io_cb_cancel_data *match,
978 struct io_wq_work *work)
980 if (work && match->fn(work, match->data)) {
981 work->flags |= IO_WQ_WORK_CANCEL;
982 __set_notify_signal(worker->task);
989 static bool io_wq_worker_cancel(struct io_worker *worker, void *data)
991 struct io_cb_cancel_data *match = data;
994 * Hold the lock to avoid ->cur_work going out of scope, caller
995 * may dereference the passed in work.
997 raw_spin_lock(&worker->lock);
998 if (__io_wq_worker_cancel(worker, match, worker->cur_work) ||
999 __io_wq_worker_cancel(worker, match, worker->next_work))
1000 match->nr_running++;
1001 raw_spin_unlock(&worker->lock);
1003 return match->nr_running && !match->cancel_all;
1006 static inline void io_wq_remove_pending(struct io_wq *wq,
1007 struct io_wq_work *work,
1008 struct io_wq_work_node *prev)
1010 struct io_wq_acct *acct = io_work_get_acct(wq, work);
1011 unsigned int hash = io_get_work_hash(work);
1012 struct io_wq_work *prev_work = NULL;
1014 if (io_wq_is_hashed(work) && work == wq->hash_tail[hash]) {
1016 prev_work = container_of(prev, struct io_wq_work, list);
1017 if (prev_work && io_get_work_hash(prev_work) == hash)
1018 wq->hash_tail[hash] = prev_work;
1020 wq->hash_tail[hash] = NULL;
1022 wq_list_del(&acct->work_list, &work->list, prev);
1025 static bool io_acct_cancel_pending_work(struct io_wq *wq,
1026 struct io_wq_acct *acct,
1027 struct io_cb_cancel_data *match)
1029 struct io_wq_work_node *node, *prev;
1030 struct io_wq_work *work;
1032 raw_spin_lock(&acct->lock);
1033 wq_list_for_each(node, prev, &acct->work_list) {
1034 work = container_of(node, struct io_wq_work, list);
1035 if (!match->fn(work, match->data))
1037 io_wq_remove_pending(wq, work, prev);
1038 raw_spin_unlock(&acct->lock);
1039 io_run_cancel(work, wq);
1040 match->nr_pending++;
1041 /* not safe to continue after unlock */
1044 raw_spin_unlock(&acct->lock);
1049 static void io_wq_cancel_pending_work(struct io_wq *wq,
1050 struct io_cb_cancel_data *match)
1054 for (i = 0; i < IO_WQ_ACCT_NR; i++) {
1055 struct io_wq_acct *acct = io_get_acct(wq, i == 0);
1057 if (io_acct_cancel_pending_work(wq, acct, match)) {
1058 if (match->cancel_all)
1065 static void io_wq_cancel_running_work(struct io_wq *wq,
1066 struct io_cb_cancel_data *match)
1069 io_wq_for_each_worker(wq, io_wq_worker_cancel, match);
1073 enum io_wq_cancel io_wq_cancel_cb(struct io_wq *wq, work_cancel_fn *cancel,
1074 void *data, bool cancel_all)
1076 struct io_cb_cancel_data match = {
1079 .cancel_all = cancel_all,
1083 * First check pending list, if we're lucky we can just remove it
1084 * from there. CANCEL_OK means that the work is returned as-new,
1085 * no completion will be posted for it.
1087 * Then check if a free (going busy) or busy worker has the work
1088 * currently running. If we find it there, we'll return CANCEL_RUNNING
1089 * as an indication that we attempt to signal cancellation. The
1090 * completion will run normally in this case.
1092 * Do both of these while holding the wq->lock, to ensure that
1093 * we'll find a work item regardless of state.
1095 io_wq_cancel_pending_work(wq, &match);
1096 if (match.nr_pending && !match.cancel_all)
1097 return IO_WQ_CANCEL_OK;
1099 raw_spin_lock(&wq->lock);
1100 io_wq_cancel_running_work(wq, &match);
1101 raw_spin_unlock(&wq->lock);
1102 if (match.nr_running && !match.cancel_all)
1103 return IO_WQ_CANCEL_RUNNING;
1105 if (match.nr_running)
1106 return IO_WQ_CANCEL_RUNNING;
1107 if (match.nr_pending)
1108 return IO_WQ_CANCEL_OK;
1109 return IO_WQ_CANCEL_NOTFOUND;
1112 static int io_wq_hash_wake(struct wait_queue_entry *wait, unsigned mode,
1113 int sync, void *key)
1115 struct io_wq *wq = container_of(wait, struct io_wq, wait);
1118 list_del_init(&wait->entry);
1121 for (i = 0; i < IO_WQ_ACCT_NR; i++) {
1122 struct io_wq_acct *acct = &wq->acct[i];
1124 if (test_and_clear_bit(IO_ACCT_STALLED_BIT, &acct->flags))
1125 io_wq_activate_free_worker(wq, acct);
1131 struct io_wq *io_wq_create(unsigned bounded, struct io_wq_data *data)
1136 if (WARN_ON_ONCE(!data->free_work || !data->do_work))
1137 return ERR_PTR(-EINVAL);
1138 if (WARN_ON_ONCE(!bounded))
1139 return ERR_PTR(-EINVAL);
1141 wq = kzalloc(sizeof(struct io_wq), GFP_KERNEL);
1143 return ERR_PTR(-ENOMEM);
1144 ret = cpuhp_state_add_instance_nocalls(io_wq_online, &wq->cpuhp_node);
1148 refcount_inc(&data->hash->refs);
1149 wq->hash = data->hash;
1150 wq->free_work = data->free_work;
1151 wq->do_work = data->do_work;
1155 if (!alloc_cpumask_var(&wq->cpu_mask, GFP_KERNEL))
1157 cpumask_copy(wq->cpu_mask, cpu_possible_mask);
1158 wq->acct[IO_WQ_ACCT_BOUND].max_workers = bounded;
1159 wq->acct[IO_WQ_ACCT_UNBOUND].max_workers =
1160 task_rlimit(current, RLIMIT_NPROC);
1161 INIT_LIST_HEAD(&wq->wait.entry);
1162 wq->wait.func = io_wq_hash_wake;
1163 for (i = 0; i < IO_WQ_ACCT_NR; i++) {
1164 struct io_wq_acct *acct = &wq->acct[i];
1167 atomic_set(&acct->nr_running, 0);
1168 INIT_WQ_LIST(&acct->work_list);
1169 raw_spin_lock_init(&acct->lock);
1172 raw_spin_lock_init(&wq->lock);
1173 INIT_HLIST_NULLS_HEAD(&wq->free_list, 0);
1174 INIT_LIST_HEAD(&wq->all_list);
1176 wq->task = get_task_struct(data->task);
1177 atomic_set(&wq->worker_refs, 1);
1178 init_completion(&wq->worker_done);
1181 io_wq_put_hash(data->hash);
1182 cpuhp_state_remove_instance_nocalls(io_wq_online, &wq->cpuhp_node);
1184 free_cpumask_var(wq->cpu_mask);
1187 return ERR_PTR(ret);
1190 static bool io_task_work_match(struct callback_head *cb, void *data)
1192 struct io_worker *worker;
1194 if (cb->func != create_worker_cb && cb->func != create_worker_cont)
1196 worker = container_of(cb, struct io_worker, create_work);
1197 return worker->wq == data;
1200 void io_wq_exit_start(struct io_wq *wq)
1202 set_bit(IO_WQ_BIT_EXIT, &wq->state);
1205 static void io_wq_cancel_tw_create(struct io_wq *wq)
1207 struct callback_head *cb;
1209 while ((cb = task_work_cancel_match(wq->task, io_task_work_match, wq)) != NULL) {
1210 struct io_worker *worker;
1212 worker = container_of(cb, struct io_worker, create_work);
1213 io_worker_cancel_cb(worker);
1215 * Only the worker continuation helper has worker allocated and
1216 * hence needs freeing.
1218 if (cb->func == create_worker_cont)
1223 static void io_wq_exit_workers(struct io_wq *wq)
1228 io_wq_cancel_tw_create(wq);
1231 io_wq_for_each_worker(wq, io_wq_worker_wake, NULL);
1233 io_worker_ref_put(wq);
1234 wait_for_completion(&wq->worker_done);
1236 spin_lock_irq(&wq->hash->wait.lock);
1237 list_del_init(&wq->wait.entry);
1238 spin_unlock_irq(&wq->hash->wait.lock);
1240 put_task_struct(wq->task);
1244 static void io_wq_destroy(struct io_wq *wq)
1246 struct io_cb_cancel_data match = {
1247 .fn = io_wq_work_match_all,
1251 cpuhp_state_remove_instance_nocalls(io_wq_online, &wq->cpuhp_node);
1252 io_wq_cancel_pending_work(wq, &match);
1253 free_cpumask_var(wq->cpu_mask);
1254 io_wq_put_hash(wq->hash);
1258 void io_wq_put_and_exit(struct io_wq *wq)
1260 WARN_ON_ONCE(!test_bit(IO_WQ_BIT_EXIT, &wq->state));
1262 io_wq_exit_workers(wq);
1266 struct online_data {
1271 static bool io_wq_worker_affinity(struct io_worker *worker, void *data)
1273 struct online_data *od = data;
1276 cpumask_set_cpu(od->cpu, worker->wq->cpu_mask);
1278 cpumask_clear_cpu(od->cpu, worker->wq->cpu_mask);
1282 static int __io_wq_cpu_online(struct io_wq *wq, unsigned int cpu, bool online)
1284 struct online_data od = {
1290 io_wq_for_each_worker(wq, io_wq_worker_affinity, &od);
1295 static int io_wq_cpu_online(unsigned int cpu, struct hlist_node *node)
1297 struct io_wq *wq = hlist_entry_safe(node, struct io_wq, cpuhp_node);
1299 return __io_wq_cpu_online(wq, cpu, true);
1302 static int io_wq_cpu_offline(unsigned int cpu, struct hlist_node *node)
1304 struct io_wq *wq = hlist_entry_safe(node, struct io_wq, cpuhp_node);
1306 return __io_wq_cpu_online(wq, cpu, false);
1309 int io_wq_cpu_affinity(struct io_uring_task *tctx, cpumask_var_t mask)
1311 if (!tctx || !tctx->io_wq)
1316 cpumask_copy(tctx->io_wq->cpu_mask, mask);
1318 cpumask_copy(tctx->io_wq->cpu_mask, cpu_possible_mask);
1325 * Set max number of unbounded workers, returns old value. If new_count is 0,
1326 * then just return the old value.
1328 int io_wq_max_workers(struct io_wq *wq, int *new_count)
1330 struct io_wq_acct *acct;
1331 int prev[IO_WQ_ACCT_NR];
1334 BUILD_BUG_ON((int) IO_WQ_ACCT_BOUND != (int) IO_WQ_BOUND);
1335 BUILD_BUG_ON((int) IO_WQ_ACCT_UNBOUND != (int) IO_WQ_UNBOUND);
1336 BUILD_BUG_ON((int) IO_WQ_ACCT_NR != 2);
1338 for (i = 0; i < IO_WQ_ACCT_NR; i++) {
1339 if (new_count[i] > task_rlimit(current, RLIMIT_NPROC))
1340 new_count[i] = task_rlimit(current, RLIMIT_NPROC);
1343 for (i = 0; i < IO_WQ_ACCT_NR; i++)
1348 raw_spin_lock(&wq->lock);
1349 for (i = 0; i < IO_WQ_ACCT_NR; i++) {
1350 acct = &wq->acct[i];
1351 prev[i] = max_t(int, acct->max_workers, prev[i]);
1353 acct->max_workers = new_count[i];
1355 raw_spin_unlock(&wq->lock);
1358 for (i = 0; i < IO_WQ_ACCT_NR; i++)
1359 new_count[i] = prev[i];
1364 static __init int io_wq_init(void)
1368 ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "io-wq/online",
1369 io_wq_cpu_online, io_wq_cpu_offline);
1375 subsys_initcall(io_wq_init);