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 <uapi/linux/io_uring.h>
24 #define WORKER_IDLE_TIMEOUT (5 * HZ)
27 IO_WORKER_F_UP = 1, /* up and active */
28 IO_WORKER_F_RUNNING = 2, /* account as running */
29 IO_WORKER_F_FREE = 4, /* worker on free list */
30 IO_WORKER_F_BOUND = 8, /* is doing bounded work */
34 IO_WQ_BIT_EXIT = 0, /* wq exiting */
38 IO_ACCT_STALLED_BIT = 0, /* stalled on hash */
42 * One for each thread in a wqe pool
47 struct hlist_nulls_node nulls_node;
48 struct list_head all_list;
49 struct task_struct *task;
52 struct io_wq_work *cur_work;
53 struct io_wq_work *next_work;
56 struct completion ref_done;
58 unsigned long create_state;
59 struct callback_head create_work;
64 struct work_struct work;
68 #if BITS_PER_LONG == 64
69 #define IO_WQ_HASH_ORDER 6
71 #define IO_WQ_HASH_ORDER 5
74 #define IO_WQ_NR_HASH_BUCKETS (1u << IO_WQ_HASH_ORDER)
82 struct io_wq_work_list work_list;
93 * Per-node worker thread pool
97 struct io_wqe_acct acct[IO_WQ_ACCT_NR];
101 struct hlist_nulls_head free_list;
102 struct list_head all_list;
104 struct wait_queue_entry wait;
107 struct io_wq_work *hash_tail[IO_WQ_NR_HASH_BUCKETS];
109 cpumask_var_t cpu_mask;
118 free_work_fn *free_work;
119 io_wq_work_fn *do_work;
121 struct io_wq_hash *hash;
123 atomic_t worker_refs;
124 struct completion worker_done;
126 struct hlist_node cpuhp_node;
128 struct task_struct *task;
130 struct io_wqe *wqes[];
133 static enum cpuhp_state io_wq_online;
135 struct io_cb_cancel_data {
143 static bool create_io_worker(struct io_wq *wq, struct io_wqe *wqe, int index);
144 static void io_wqe_dec_running(struct io_worker *worker);
145 static bool io_acct_cancel_pending_work(struct io_wqe *wqe,
146 struct io_wqe_acct *acct,
147 struct io_cb_cancel_data *match);
148 static void create_worker_cb(struct callback_head *cb);
149 static void io_wq_cancel_tw_create(struct io_wq *wq);
151 static bool io_worker_get(struct io_worker *worker)
153 return refcount_inc_not_zero(&worker->ref);
156 static void io_worker_release(struct io_worker *worker)
158 if (refcount_dec_and_test(&worker->ref))
159 complete(&worker->ref_done);
162 static inline struct io_wqe_acct *io_get_acct(struct io_wqe *wqe, bool bound)
164 return &wqe->acct[bound ? IO_WQ_ACCT_BOUND : IO_WQ_ACCT_UNBOUND];
167 static inline struct io_wqe_acct *io_work_get_acct(struct io_wqe *wqe,
168 struct io_wq_work *work)
170 return io_get_acct(wqe, !(work->flags & IO_WQ_WORK_UNBOUND));
173 static inline struct io_wqe_acct *io_wqe_get_acct(struct io_worker *worker)
175 return io_get_acct(worker->wqe, worker->flags & IO_WORKER_F_BOUND);
178 static void io_worker_ref_put(struct io_wq *wq)
180 if (atomic_dec_and_test(&wq->worker_refs))
181 complete(&wq->worker_done);
184 bool io_wq_worker_stopped(void)
186 struct io_worker *worker = current->worker_private;
188 if (WARN_ON_ONCE(!io_wq_current_is_worker()))
191 return test_bit(IO_WQ_BIT_EXIT, &worker->wqe->wq->state);
194 static void io_worker_cancel_cb(struct io_worker *worker)
196 struct io_wqe_acct *acct = io_wqe_get_acct(worker);
197 struct io_wqe *wqe = worker->wqe;
198 struct io_wq *wq = wqe->wq;
200 atomic_dec(&acct->nr_running);
201 raw_spin_lock(&worker->wqe->lock);
203 raw_spin_unlock(&worker->wqe->lock);
204 io_worker_ref_put(wq);
205 clear_bit_unlock(0, &worker->create_state);
206 io_worker_release(worker);
209 static bool io_task_worker_match(struct callback_head *cb, void *data)
211 struct io_worker *worker;
213 if (cb->func != create_worker_cb)
215 worker = container_of(cb, struct io_worker, create_work);
216 return worker == data;
219 static void io_worker_exit(struct io_worker *worker)
221 struct io_wqe *wqe = worker->wqe;
222 struct io_wq *wq = wqe->wq;
225 struct callback_head *cb = task_work_cancel_match(wq->task,
226 io_task_worker_match, worker);
230 io_worker_cancel_cb(worker);
233 io_worker_release(worker);
234 wait_for_completion(&worker->ref_done);
236 raw_spin_lock(&wqe->lock);
237 if (worker->flags & IO_WORKER_F_FREE)
238 hlist_nulls_del_rcu(&worker->nulls_node);
239 list_del_rcu(&worker->all_list);
240 raw_spin_unlock(&wqe->lock);
241 io_wqe_dec_running(worker);
244 current->flags &= ~PF_IO_WORKER;
247 kfree_rcu(worker, rcu);
248 io_worker_ref_put(wqe->wq);
252 static inline bool io_acct_run_queue(struct io_wqe_acct *acct)
256 raw_spin_lock(&acct->lock);
257 if (!wq_list_empty(&acct->work_list) &&
258 !test_bit(IO_ACCT_STALLED_BIT, &acct->flags))
260 raw_spin_unlock(&acct->lock);
266 * Check head of free list for an available worker. If one isn't available,
267 * caller must create one.
269 static bool io_wqe_activate_free_worker(struct io_wqe *wqe,
270 struct io_wqe_acct *acct)
273 struct hlist_nulls_node *n;
274 struct io_worker *worker;
277 * Iterate free_list and see if we can find an idle worker to
278 * activate. If a given worker is on the free_list but in the process
279 * of exiting, keep trying.
281 hlist_nulls_for_each_entry_rcu(worker, n, &wqe->free_list, nulls_node) {
282 if (!io_worker_get(worker))
284 if (io_wqe_get_acct(worker) != acct) {
285 io_worker_release(worker);
288 if (wake_up_process(worker->task)) {
289 io_worker_release(worker);
292 io_worker_release(worker);
299 * We need a worker. If we find a free one, we're good. If not, and we're
300 * below the max number of workers, create one.
302 static bool io_wqe_create_worker(struct io_wqe *wqe, struct io_wqe_acct *acct)
305 * Most likely an attempt to queue unbounded work on an io_wq that
306 * wasn't setup with any unbounded workers.
308 if (unlikely(!acct->max_workers))
309 pr_warn_once("io-wq is not configured for unbound workers");
311 raw_spin_lock(&wqe->lock);
312 if (acct->nr_workers >= acct->max_workers) {
313 raw_spin_unlock(&wqe->lock);
317 raw_spin_unlock(&wqe->lock);
318 atomic_inc(&acct->nr_running);
319 atomic_inc(&wqe->wq->worker_refs);
320 return create_io_worker(wqe->wq, wqe, acct->index);
323 static void io_wqe_inc_running(struct io_worker *worker)
325 struct io_wqe_acct *acct = io_wqe_get_acct(worker);
327 atomic_inc(&acct->nr_running);
330 static void create_worker_cb(struct callback_head *cb)
332 struct io_worker *worker;
335 struct io_wqe_acct *acct;
336 bool do_create = false;
338 worker = container_of(cb, struct io_worker, create_work);
341 acct = &wqe->acct[worker->create_index];
342 raw_spin_lock(&wqe->lock);
343 if (acct->nr_workers < acct->max_workers) {
347 raw_spin_unlock(&wqe->lock);
349 create_io_worker(wq, wqe, worker->create_index);
351 atomic_dec(&acct->nr_running);
352 io_worker_ref_put(wq);
354 clear_bit_unlock(0, &worker->create_state);
355 io_worker_release(worker);
358 static bool io_queue_worker_create(struct io_worker *worker,
359 struct io_wqe_acct *acct,
360 task_work_func_t func)
362 struct io_wqe *wqe = worker->wqe;
363 struct io_wq *wq = wqe->wq;
365 /* raced with exit, just ignore create call */
366 if (test_bit(IO_WQ_BIT_EXIT, &wq->state))
368 if (!io_worker_get(worker))
371 * create_state manages ownership of create_work/index. We should
372 * only need one entry per worker, as the worker going to sleep
373 * will trigger the condition, and waking will clear it once it
374 * runs the task_work.
376 if (test_bit(0, &worker->create_state) ||
377 test_and_set_bit_lock(0, &worker->create_state))
380 atomic_inc(&wq->worker_refs);
381 init_task_work(&worker->create_work, func);
382 worker->create_index = acct->index;
383 if (!task_work_add(wq->task, &worker->create_work, TWA_SIGNAL)) {
385 * EXIT may have been set after checking it above, check after
386 * adding the task_work and remove any creation item if it is
387 * now set. wq exit does that too, but we can have added this
388 * work item after we canceled in io_wq_exit_workers().
390 if (test_bit(IO_WQ_BIT_EXIT, &wq->state))
391 io_wq_cancel_tw_create(wq);
392 io_worker_ref_put(wq);
395 io_worker_ref_put(wq);
396 clear_bit_unlock(0, &worker->create_state);
398 io_worker_release(worker);
400 atomic_dec(&acct->nr_running);
401 io_worker_ref_put(wq);
405 static void io_wqe_dec_running(struct io_worker *worker)
407 struct io_wqe_acct *acct = io_wqe_get_acct(worker);
408 struct io_wqe *wqe = worker->wqe;
410 if (!(worker->flags & IO_WORKER_F_UP))
413 if (!atomic_dec_and_test(&acct->nr_running))
415 if (!io_acct_run_queue(acct))
418 atomic_inc(&acct->nr_running);
419 atomic_inc(&wqe->wq->worker_refs);
420 io_queue_worker_create(worker, acct, create_worker_cb);
424 * Worker will start processing some work. Move it to the busy list, if
425 * it's currently on the freelist
427 static void __io_worker_busy(struct io_wqe *wqe, struct io_worker *worker)
429 if (worker->flags & IO_WORKER_F_FREE) {
430 worker->flags &= ~IO_WORKER_F_FREE;
431 raw_spin_lock(&wqe->lock);
432 hlist_nulls_del_init_rcu(&worker->nulls_node);
433 raw_spin_unlock(&wqe->lock);
438 * No work, worker going to sleep. Move to freelist, and unuse mm if we
439 * have one attached. Dropping the mm may potentially sleep, so we drop
440 * the lock in that case and return success. Since the caller has to
441 * retry the loop in that case (we changed task state), we don't regrab
442 * the lock if we return success.
444 static void __io_worker_idle(struct io_wqe *wqe, struct io_worker *worker)
445 __must_hold(wqe->lock)
447 if (!(worker->flags & IO_WORKER_F_FREE)) {
448 worker->flags |= IO_WORKER_F_FREE;
449 hlist_nulls_add_head_rcu(&worker->nulls_node, &wqe->free_list);
453 static inline unsigned int io_get_work_hash(struct io_wq_work *work)
455 return work->flags >> IO_WQ_HASH_SHIFT;
458 static bool io_wait_on_hash(struct io_wqe *wqe, unsigned int hash)
460 struct io_wq *wq = wqe->wq;
463 spin_lock_irq(&wq->hash->wait.lock);
464 if (list_empty(&wqe->wait.entry)) {
465 __add_wait_queue(&wq->hash->wait, &wqe->wait);
466 if (!test_bit(hash, &wq->hash->map)) {
467 __set_current_state(TASK_RUNNING);
468 list_del_init(&wqe->wait.entry);
472 spin_unlock_irq(&wq->hash->wait.lock);
476 static struct io_wq_work *io_get_next_work(struct io_wqe_acct *acct,
477 struct io_worker *worker)
478 __must_hold(acct->lock)
480 struct io_wq_work_node *node, *prev;
481 struct io_wq_work *work, *tail;
482 unsigned int stall_hash = -1U;
483 struct io_wqe *wqe = worker->wqe;
485 wq_list_for_each(node, prev, &acct->work_list) {
488 work = container_of(node, struct io_wq_work, list);
490 /* not hashed, can run anytime */
491 if (!io_wq_is_hashed(work)) {
492 wq_list_del(&acct->work_list, node, prev);
496 hash = io_get_work_hash(work);
497 /* all items with this hash lie in [work, tail] */
498 tail = wqe->hash_tail[hash];
500 /* hashed, can run if not already running */
501 if (!test_and_set_bit(hash, &wqe->wq->hash->map)) {
502 wqe->hash_tail[hash] = NULL;
503 wq_list_cut(&acct->work_list, &tail->list, prev);
506 if (stall_hash == -1U)
508 /* fast forward to a next hash, for-each will fix up @prev */
512 if (stall_hash != -1U) {
516 * Set this before dropping the lock to avoid racing with new
517 * work being added and clearing the stalled bit.
519 set_bit(IO_ACCT_STALLED_BIT, &acct->flags);
520 raw_spin_unlock(&acct->lock);
521 unstalled = io_wait_on_hash(wqe, stall_hash);
522 raw_spin_lock(&acct->lock);
524 clear_bit(IO_ACCT_STALLED_BIT, &acct->flags);
525 if (wq_has_sleeper(&wqe->wq->hash->wait))
526 wake_up(&wqe->wq->hash->wait);
533 static void io_assign_current_work(struct io_worker *worker,
534 struct io_wq_work *work)
541 raw_spin_lock(&worker->lock);
542 worker->cur_work = work;
543 worker->next_work = NULL;
544 raw_spin_unlock(&worker->lock);
547 static void io_wqe_enqueue(struct io_wqe *wqe, struct io_wq_work *work);
549 static void io_worker_handle_work(struct io_worker *worker)
551 struct io_wqe_acct *acct = io_wqe_get_acct(worker);
552 struct io_wqe *wqe = worker->wqe;
553 struct io_wq *wq = wqe->wq;
554 bool do_kill = test_bit(IO_WQ_BIT_EXIT, &wq->state);
557 struct io_wq_work *work;
560 * If we got some work, mark us as busy. If we didn't, but
561 * the list isn't empty, it means we stalled on hashed work.
562 * Mark us stalled so we don't keep looking for work when we
563 * can't make progress, any work completion or insertion will
564 * clear the stalled flag.
566 raw_spin_lock(&acct->lock);
567 work = io_get_next_work(acct, worker);
568 raw_spin_unlock(&acct->lock);
570 __io_worker_busy(wqe, worker);
573 * Make sure cancelation can find this, even before
574 * it becomes the active work. That avoids a window
575 * where the work has been removed from our general
576 * work list, but isn't yet discoverable as the
577 * current work item for this worker.
579 raw_spin_lock(&worker->lock);
580 worker->next_work = work;
581 raw_spin_unlock(&worker->lock);
585 io_assign_current_work(worker, work);
586 __set_current_state(TASK_RUNNING);
588 /* handle a whole dependent link */
590 struct io_wq_work *next_hashed, *linked;
591 unsigned int hash = io_get_work_hash(work);
593 next_hashed = wq_next_work(work);
595 if (unlikely(do_kill) && (work->flags & IO_WQ_WORK_UNBOUND))
596 work->flags |= IO_WQ_WORK_CANCEL;
598 io_assign_current_work(worker, NULL);
600 linked = wq->free_work(work);
602 if (!work && linked && !io_wq_is_hashed(linked)) {
606 io_assign_current_work(worker, work);
608 io_wqe_enqueue(wqe, linked);
610 if (hash != -1U && !next_hashed) {
611 /* serialize hash clear with wake_up() */
612 spin_lock_irq(&wq->hash->wait.lock);
613 clear_bit(hash, &wq->hash->map);
614 clear_bit(IO_ACCT_STALLED_BIT, &acct->flags);
615 spin_unlock_irq(&wq->hash->wait.lock);
616 if (wq_has_sleeper(&wq->hash->wait))
617 wake_up(&wq->hash->wait);
623 static int io_wqe_worker(void *data)
625 struct io_worker *worker = data;
626 struct io_wqe_acct *acct = io_wqe_get_acct(worker);
627 struct io_wqe *wqe = worker->wqe;
628 struct io_wq *wq = wqe->wq;
629 bool last_timeout = false;
630 char buf[TASK_COMM_LEN];
632 worker->flags |= (IO_WORKER_F_UP | IO_WORKER_F_RUNNING);
634 snprintf(buf, sizeof(buf), "iou-wrk-%d", wq->task->pid);
635 set_task_comm(current, buf);
637 while (!test_bit(IO_WQ_BIT_EXIT, &wq->state)) {
640 set_current_state(TASK_INTERRUPTIBLE);
641 while (io_acct_run_queue(acct))
642 io_worker_handle_work(worker);
644 raw_spin_lock(&wqe->lock);
645 /* timed out, exit unless we're the last worker */
646 if (last_timeout && acct->nr_workers > 1) {
648 raw_spin_unlock(&wqe->lock);
649 __set_current_state(TASK_RUNNING);
652 last_timeout = false;
653 __io_worker_idle(wqe, worker);
654 raw_spin_unlock(&wqe->lock);
655 if (io_run_task_work())
657 ret = schedule_timeout(WORKER_IDLE_TIMEOUT);
658 if (signal_pending(current)) {
661 if (!get_signal(&ksig))
668 if (test_bit(IO_WQ_BIT_EXIT, &wq->state))
669 io_worker_handle_work(worker);
671 io_worker_exit(worker);
676 * Called when a worker is scheduled in. Mark us as currently running.
678 void io_wq_worker_running(struct task_struct *tsk)
680 struct io_worker *worker = tsk->worker_private;
684 if (!(worker->flags & IO_WORKER_F_UP))
686 if (worker->flags & IO_WORKER_F_RUNNING)
688 worker->flags |= IO_WORKER_F_RUNNING;
689 io_wqe_inc_running(worker);
693 * Called when worker is going to sleep. If there are no workers currently
694 * running and we have work pending, wake up a free one or create a new one.
696 void io_wq_worker_sleeping(struct task_struct *tsk)
698 struct io_worker *worker = tsk->worker_private;
702 if (!(worker->flags & IO_WORKER_F_UP))
704 if (!(worker->flags & IO_WORKER_F_RUNNING))
707 worker->flags &= ~IO_WORKER_F_RUNNING;
708 io_wqe_dec_running(worker);
711 static void io_init_new_worker(struct io_wqe *wqe, struct io_worker *worker,
712 struct task_struct *tsk)
714 tsk->worker_private = worker;
716 set_cpus_allowed_ptr(tsk, wqe->cpu_mask);
717 tsk->flags |= PF_NO_SETAFFINITY;
719 raw_spin_lock(&wqe->lock);
720 hlist_nulls_add_head_rcu(&worker->nulls_node, &wqe->free_list);
721 list_add_tail_rcu(&worker->all_list, &wqe->all_list);
722 worker->flags |= IO_WORKER_F_FREE;
723 raw_spin_unlock(&wqe->lock);
724 wake_up_new_task(tsk);
727 static bool io_wq_work_match_all(struct io_wq_work *work, void *data)
732 static inline bool io_should_retry_thread(long err)
735 * Prevent perpetual task_work retry, if the task (or its group) is
738 if (fatal_signal_pending(current))
744 case -ERESTARTNOINTR:
745 case -ERESTARTNOHAND:
752 static void create_worker_cont(struct callback_head *cb)
754 struct io_worker *worker;
755 struct task_struct *tsk;
758 worker = container_of(cb, struct io_worker, create_work);
759 clear_bit_unlock(0, &worker->create_state);
761 tsk = create_io_thread(io_wqe_worker, worker, wqe->node);
763 io_init_new_worker(wqe, worker, tsk);
764 io_worker_release(worker);
766 } else if (!io_should_retry_thread(PTR_ERR(tsk))) {
767 struct io_wqe_acct *acct = io_wqe_get_acct(worker);
769 atomic_dec(&acct->nr_running);
770 raw_spin_lock(&wqe->lock);
772 if (!acct->nr_workers) {
773 struct io_cb_cancel_data match = {
774 .fn = io_wq_work_match_all,
778 raw_spin_unlock(&wqe->lock);
779 while (io_acct_cancel_pending_work(wqe, acct, &match))
782 raw_spin_unlock(&wqe->lock);
784 io_worker_ref_put(wqe->wq);
789 /* re-create attempts grab a new worker ref, drop the existing one */
790 io_worker_release(worker);
791 schedule_work(&worker->work);
794 static void io_workqueue_create(struct work_struct *work)
796 struct io_worker *worker = container_of(work, struct io_worker, work);
797 struct io_wqe_acct *acct = io_wqe_get_acct(worker);
799 if (!io_queue_worker_create(worker, acct, create_worker_cont))
803 static bool create_io_worker(struct io_wq *wq, struct io_wqe *wqe, int index)
805 struct io_wqe_acct *acct = &wqe->acct[index];
806 struct io_worker *worker;
807 struct task_struct *tsk;
809 __set_current_state(TASK_RUNNING);
811 worker = kzalloc_node(sizeof(*worker), GFP_KERNEL, wqe->node);
814 atomic_dec(&acct->nr_running);
815 raw_spin_lock(&wqe->lock);
817 raw_spin_unlock(&wqe->lock);
818 io_worker_ref_put(wq);
822 refcount_set(&worker->ref, 1);
824 raw_spin_lock_init(&worker->lock);
825 init_completion(&worker->ref_done);
827 if (index == IO_WQ_ACCT_BOUND)
828 worker->flags |= IO_WORKER_F_BOUND;
830 tsk = create_io_thread(io_wqe_worker, worker, wqe->node);
832 io_init_new_worker(wqe, worker, tsk);
833 } else if (!io_should_retry_thread(PTR_ERR(tsk))) {
837 INIT_WORK(&worker->work, io_workqueue_create);
838 schedule_work(&worker->work);
845 * Iterate the passed in list and call the specific function for each
846 * worker that isn't exiting
848 static bool io_wq_for_each_worker(struct io_wqe *wqe,
849 bool (*func)(struct io_worker *, void *),
852 struct io_worker *worker;
855 list_for_each_entry_rcu(worker, &wqe->all_list, all_list) {
856 if (io_worker_get(worker)) {
857 /* no task if node is/was offline */
859 ret = func(worker, data);
860 io_worker_release(worker);
869 static bool io_wq_worker_wake(struct io_worker *worker, void *data)
871 __set_notify_signal(worker->task);
872 wake_up_process(worker->task);
876 static void io_run_cancel(struct io_wq_work *work, struct io_wqe *wqe)
878 struct io_wq *wq = wqe->wq;
881 work->flags |= IO_WQ_WORK_CANCEL;
883 work = wq->free_work(work);
887 static void io_wqe_insert_work(struct io_wqe *wqe, struct io_wq_work *work)
889 struct io_wqe_acct *acct = io_work_get_acct(wqe, work);
891 struct io_wq_work *tail;
893 if (!io_wq_is_hashed(work)) {
895 wq_list_add_tail(&work->list, &acct->work_list);
899 hash = io_get_work_hash(work);
900 tail = wqe->hash_tail[hash];
901 wqe->hash_tail[hash] = work;
905 wq_list_add_after(&work->list, &tail->list, &acct->work_list);
908 static bool io_wq_work_match_item(struct io_wq_work *work, void *data)
913 static void io_wqe_enqueue(struct io_wqe *wqe, struct io_wq_work *work)
915 struct io_wqe_acct *acct = io_work_get_acct(wqe, work);
916 struct io_cb_cancel_data match;
917 unsigned work_flags = work->flags;
921 * If io-wq is exiting for this task, or if the request has explicitly
922 * been marked as one that should not get executed, cancel it here.
924 if (test_bit(IO_WQ_BIT_EXIT, &wqe->wq->state) ||
925 (work->flags & IO_WQ_WORK_CANCEL)) {
926 io_run_cancel(work, wqe);
930 raw_spin_lock(&acct->lock);
931 io_wqe_insert_work(wqe, work);
932 clear_bit(IO_ACCT_STALLED_BIT, &acct->flags);
933 raw_spin_unlock(&acct->lock);
935 raw_spin_lock(&wqe->lock);
937 do_create = !io_wqe_activate_free_worker(wqe, acct);
940 raw_spin_unlock(&wqe->lock);
942 if (do_create && ((work_flags & IO_WQ_WORK_CONCURRENT) ||
943 !atomic_read(&acct->nr_running))) {
946 did_create = io_wqe_create_worker(wqe, acct);
947 if (likely(did_create))
950 raw_spin_lock(&wqe->lock);
951 if (acct->nr_workers) {
952 raw_spin_unlock(&wqe->lock);
955 raw_spin_unlock(&wqe->lock);
957 /* fatal condition, failed to create the first worker */
958 match.fn = io_wq_work_match_item,
960 match.cancel_all = false,
962 io_acct_cancel_pending_work(wqe, acct, &match);
966 void io_wq_enqueue(struct io_wq *wq, struct io_wq_work *work)
968 struct io_wqe *wqe = wq->wqes[numa_node_id()];
970 io_wqe_enqueue(wqe, work);
974 * Work items that hash to the same value will not be done in parallel.
975 * Used to limit concurrent writes, generally hashed by inode.
977 void io_wq_hash_work(struct io_wq_work *work, void *val)
981 bit = hash_ptr(val, IO_WQ_HASH_ORDER);
982 work->flags |= (IO_WQ_WORK_HASHED | (bit << IO_WQ_HASH_SHIFT));
985 static bool __io_wq_worker_cancel(struct io_worker *worker,
986 struct io_cb_cancel_data *match,
987 struct io_wq_work *work)
989 if (work && match->fn(work, match->data)) {
990 work->flags |= IO_WQ_WORK_CANCEL;
991 __set_notify_signal(worker->task);
998 static bool io_wq_worker_cancel(struct io_worker *worker, void *data)
1000 struct io_cb_cancel_data *match = data;
1003 * Hold the lock to avoid ->cur_work going out of scope, caller
1004 * may dereference the passed in work.
1006 raw_spin_lock(&worker->lock);
1007 if (__io_wq_worker_cancel(worker, match, worker->cur_work) ||
1008 __io_wq_worker_cancel(worker, match, worker->next_work))
1009 match->nr_running++;
1010 raw_spin_unlock(&worker->lock);
1012 return match->nr_running && !match->cancel_all;
1015 static inline void io_wqe_remove_pending(struct io_wqe *wqe,
1016 struct io_wq_work *work,
1017 struct io_wq_work_node *prev)
1019 struct io_wqe_acct *acct = io_work_get_acct(wqe, work);
1020 unsigned int hash = io_get_work_hash(work);
1021 struct io_wq_work *prev_work = NULL;
1023 if (io_wq_is_hashed(work) && work == wqe->hash_tail[hash]) {
1025 prev_work = container_of(prev, struct io_wq_work, list);
1026 if (prev_work && io_get_work_hash(prev_work) == hash)
1027 wqe->hash_tail[hash] = prev_work;
1029 wqe->hash_tail[hash] = NULL;
1031 wq_list_del(&acct->work_list, &work->list, prev);
1034 static bool io_acct_cancel_pending_work(struct io_wqe *wqe,
1035 struct io_wqe_acct *acct,
1036 struct io_cb_cancel_data *match)
1038 struct io_wq_work_node *node, *prev;
1039 struct io_wq_work *work;
1041 raw_spin_lock(&acct->lock);
1042 wq_list_for_each(node, prev, &acct->work_list) {
1043 work = container_of(node, struct io_wq_work, list);
1044 if (!match->fn(work, match->data))
1046 io_wqe_remove_pending(wqe, work, prev);
1047 raw_spin_unlock(&acct->lock);
1048 io_run_cancel(work, wqe);
1049 match->nr_pending++;
1050 /* not safe to continue after unlock */
1053 raw_spin_unlock(&acct->lock);
1058 static void io_wqe_cancel_pending_work(struct io_wqe *wqe,
1059 struct io_cb_cancel_data *match)
1063 for (i = 0; i < IO_WQ_ACCT_NR; i++) {
1064 struct io_wqe_acct *acct = io_get_acct(wqe, i == 0);
1066 if (io_acct_cancel_pending_work(wqe, acct, match)) {
1067 if (match->cancel_all)
1074 static void io_wqe_cancel_running_work(struct io_wqe *wqe,
1075 struct io_cb_cancel_data *match)
1078 io_wq_for_each_worker(wqe, io_wq_worker_cancel, match);
1082 enum io_wq_cancel io_wq_cancel_cb(struct io_wq *wq, work_cancel_fn *cancel,
1083 void *data, bool cancel_all)
1085 struct io_cb_cancel_data match = {
1088 .cancel_all = cancel_all,
1093 * First check pending list, if we're lucky we can just remove it
1094 * from there. CANCEL_OK means that the work is returned as-new,
1095 * no completion will be posted for it.
1097 * Then check if a free (going busy) or busy worker has the work
1098 * currently running. If we find it there, we'll return CANCEL_RUNNING
1099 * as an indication that we attempt to signal cancellation. The
1100 * completion will run normally in this case.
1102 * Do both of these while holding the wqe->lock, to ensure that
1103 * we'll find a work item regardless of state.
1105 for_each_node(node) {
1106 struct io_wqe *wqe = wq->wqes[node];
1108 io_wqe_cancel_pending_work(wqe, &match);
1109 if (match.nr_pending && !match.cancel_all)
1110 return IO_WQ_CANCEL_OK;
1112 raw_spin_lock(&wqe->lock);
1113 io_wqe_cancel_running_work(wqe, &match);
1114 raw_spin_unlock(&wqe->lock);
1115 if (match.nr_running && !match.cancel_all)
1116 return IO_WQ_CANCEL_RUNNING;
1119 if (match.nr_running)
1120 return IO_WQ_CANCEL_RUNNING;
1121 if (match.nr_pending)
1122 return IO_WQ_CANCEL_OK;
1123 return IO_WQ_CANCEL_NOTFOUND;
1126 static int io_wqe_hash_wake(struct wait_queue_entry *wait, unsigned mode,
1127 int sync, void *key)
1129 struct io_wqe *wqe = container_of(wait, struct io_wqe, wait);
1132 list_del_init(&wait->entry);
1135 for (i = 0; i < IO_WQ_ACCT_NR; i++) {
1136 struct io_wqe_acct *acct = &wqe->acct[i];
1138 if (test_and_clear_bit(IO_ACCT_STALLED_BIT, &acct->flags))
1139 io_wqe_activate_free_worker(wqe, acct);
1145 struct io_wq *io_wq_create(unsigned bounded, struct io_wq_data *data)
1150 if (WARN_ON_ONCE(!data->free_work || !data->do_work))
1151 return ERR_PTR(-EINVAL);
1152 if (WARN_ON_ONCE(!bounded))
1153 return ERR_PTR(-EINVAL);
1155 wq = kzalloc(struct_size(wq, wqes, nr_node_ids), GFP_KERNEL);
1157 return ERR_PTR(-ENOMEM);
1158 ret = cpuhp_state_add_instance_nocalls(io_wq_online, &wq->cpuhp_node);
1162 refcount_inc(&data->hash->refs);
1163 wq->hash = data->hash;
1164 wq->free_work = data->free_work;
1165 wq->do_work = data->do_work;
1168 for_each_node(node) {
1170 int alloc_node = node;
1172 if (!node_online(alloc_node))
1173 alloc_node = NUMA_NO_NODE;
1174 wqe = kzalloc_node(sizeof(struct io_wqe), GFP_KERNEL, alloc_node);
1177 wq->wqes[node] = wqe;
1178 if (!alloc_cpumask_var(&wqe->cpu_mask, GFP_KERNEL))
1180 cpumask_copy(wqe->cpu_mask, cpumask_of_node(node));
1181 wqe->node = alloc_node;
1182 wqe->acct[IO_WQ_ACCT_BOUND].max_workers = bounded;
1183 wqe->acct[IO_WQ_ACCT_UNBOUND].max_workers =
1184 task_rlimit(current, RLIMIT_NPROC);
1185 INIT_LIST_HEAD(&wqe->wait.entry);
1186 wqe->wait.func = io_wqe_hash_wake;
1187 for (i = 0; i < IO_WQ_ACCT_NR; i++) {
1188 struct io_wqe_acct *acct = &wqe->acct[i];
1191 atomic_set(&acct->nr_running, 0);
1192 INIT_WQ_LIST(&acct->work_list);
1193 raw_spin_lock_init(&acct->lock);
1196 raw_spin_lock_init(&wqe->lock);
1197 INIT_HLIST_NULLS_HEAD(&wqe->free_list, 0);
1198 INIT_LIST_HEAD(&wqe->all_list);
1201 wq->task = get_task_struct(data->task);
1202 atomic_set(&wq->worker_refs, 1);
1203 init_completion(&wq->worker_done);
1206 io_wq_put_hash(data->hash);
1207 cpuhp_state_remove_instance_nocalls(io_wq_online, &wq->cpuhp_node);
1208 for_each_node(node) {
1209 if (!wq->wqes[node])
1211 free_cpumask_var(wq->wqes[node]->cpu_mask);
1212 kfree(wq->wqes[node]);
1216 return ERR_PTR(ret);
1219 static bool io_task_work_match(struct callback_head *cb, void *data)
1221 struct io_worker *worker;
1223 if (cb->func != create_worker_cb && cb->func != create_worker_cont)
1225 worker = container_of(cb, struct io_worker, create_work);
1226 return worker->wqe->wq == data;
1229 void io_wq_exit_start(struct io_wq *wq)
1231 set_bit(IO_WQ_BIT_EXIT, &wq->state);
1234 static void io_wq_cancel_tw_create(struct io_wq *wq)
1236 struct callback_head *cb;
1238 while ((cb = task_work_cancel_match(wq->task, io_task_work_match, wq)) != NULL) {
1239 struct io_worker *worker;
1241 worker = container_of(cb, struct io_worker, create_work);
1242 io_worker_cancel_cb(worker);
1244 * Only the worker continuation helper has worker allocated and
1245 * hence needs freeing.
1247 if (cb->func == create_worker_cont)
1252 static void io_wq_exit_workers(struct io_wq *wq)
1259 io_wq_cancel_tw_create(wq);
1262 for_each_node(node) {
1263 struct io_wqe *wqe = wq->wqes[node];
1265 io_wq_for_each_worker(wqe, io_wq_worker_wake, NULL);
1268 io_worker_ref_put(wq);
1269 wait_for_completion(&wq->worker_done);
1271 for_each_node(node) {
1272 spin_lock_irq(&wq->hash->wait.lock);
1273 list_del_init(&wq->wqes[node]->wait.entry);
1274 spin_unlock_irq(&wq->hash->wait.lock);
1276 put_task_struct(wq->task);
1280 static void io_wq_destroy(struct io_wq *wq)
1284 cpuhp_state_remove_instance_nocalls(io_wq_online, &wq->cpuhp_node);
1286 for_each_node(node) {
1287 struct io_wqe *wqe = wq->wqes[node];
1288 struct io_cb_cancel_data match = {
1289 .fn = io_wq_work_match_all,
1292 io_wqe_cancel_pending_work(wqe, &match);
1293 free_cpumask_var(wqe->cpu_mask);
1296 io_wq_put_hash(wq->hash);
1300 void io_wq_put_and_exit(struct io_wq *wq)
1302 WARN_ON_ONCE(!test_bit(IO_WQ_BIT_EXIT, &wq->state));
1304 io_wq_exit_workers(wq);
1308 struct online_data {
1313 static bool io_wq_worker_affinity(struct io_worker *worker, void *data)
1315 struct online_data *od = data;
1318 cpumask_set_cpu(od->cpu, worker->wqe->cpu_mask);
1320 cpumask_clear_cpu(od->cpu, worker->wqe->cpu_mask);
1324 static int __io_wq_cpu_online(struct io_wq *wq, unsigned int cpu, bool online)
1326 struct online_data od = {
1334 io_wq_for_each_worker(wq->wqes[i], io_wq_worker_affinity, &od);
1339 static int io_wq_cpu_online(unsigned int cpu, struct hlist_node *node)
1341 struct io_wq *wq = hlist_entry_safe(node, struct io_wq, cpuhp_node);
1343 return __io_wq_cpu_online(wq, cpu, true);
1346 static int io_wq_cpu_offline(unsigned int cpu, struct hlist_node *node)
1348 struct io_wq *wq = hlist_entry_safe(node, struct io_wq, cpuhp_node);
1350 return __io_wq_cpu_online(wq, cpu, false);
1353 int io_wq_cpu_affinity(struct io_uring_task *tctx, cpumask_var_t mask)
1357 if (!tctx || !tctx->io_wq)
1362 struct io_wqe *wqe = tctx->io_wq->wqes[i];
1365 cpumask_copy(wqe->cpu_mask, mask);
1367 cpumask_copy(wqe->cpu_mask, cpumask_of_node(i));
1374 * Set max number of unbounded workers, returns old value. If new_count is 0,
1375 * then just return the old value.
1377 int io_wq_max_workers(struct io_wq *wq, int *new_count)
1379 int prev[IO_WQ_ACCT_NR];
1380 bool first_node = true;
1383 BUILD_BUG_ON((int) IO_WQ_ACCT_BOUND != (int) IO_WQ_BOUND);
1384 BUILD_BUG_ON((int) IO_WQ_ACCT_UNBOUND != (int) IO_WQ_UNBOUND);
1385 BUILD_BUG_ON((int) IO_WQ_ACCT_NR != 2);
1387 for (i = 0; i < IO_WQ_ACCT_NR; i++) {
1388 if (new_count[i] > task_rlimit(current, RLIMIT_NPROC))
1389 new_count[i] = task_rlimit(current, RLIMIT_NPROC);
1392 for (i = 0; i < IO_WQ_ACCT_NR; i++)
1396 for_each_node(node) {
1397 struct io_wqe *wqe = wq->wqes[node];
1398 struct io_wqe_acct *acct;
1400 raw_spin_lock(&wqe->lock);
1401 for (i = 0; i < IO_WQ_ACCT_NR; i++) {
1402 acct = &wqe->acct[i];
1404 prev[i] = max_t(int, acct->max_workers, prev[i]);
1406 acct->max_workers = new_count[i];
1408 raw_spin_unlock(&wqe->lock);
1413 for (i = 0; i < IO_WQ_ACCT_NR; i++)
1414 new_count[i] = prev[i];
1419 static __init int io_wq_init(void)
1423 ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "io-wq/online",
1424 io_wq_cpu_online, io_wq_cpu_offline);
1430 subsys_initcall(io_wq_init);