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/tracehook.h>
20 #define WORKER_IDLE_TIMEOUT (5 * HZ)
23 IO_WORKER_F_UP = 1, /* up and active */
24 IO_WORKER_F_RUNNING = 2, /* account as running */
25 IO_WORKER_F_FREE = 4, /* worker on free list */
26 IO_WORKER_F_BOUND = 8, /* is doing bounded work */
30 IO_WQ_BIT_EXIT = 0, /* wq exiting */
34 IO_ACCT_STALLED_BIT = 0, /* stalled on hash */
38 * One for each thread in a wqe pool
43 struct hlist_nulls_node nulls_node;
44 struct list_head all_list;
45 struct task_struct *task;
48 struct io_wq_work *cur_work;
51 struct completion ref_done;
53 unsigned long create_state;
54 struct callback_head create_work;
59 struct work_struct work;
63 #if BITS_PER_LONG == 64
64 #define IO_WQ_HASH_ORDER 6
66 #define IO_WQ_HASH_ORDER 5
69 #define IO_WQ_NR_HASH_BUCKETS (1u << IO_WQ_HASH_ORDER)
76 struct io_wq_work_list work_list;
87 * Per-node worker thread pool
91 struct io_wqe_acct acct[2];
95 struct hlist_nulls_head free_list;
96 struct list_head all_list;
98 struct wait_queue_entry wait;
101 struct io_wq_work *hash_tail[IO_WQ_NR_HASH_BUCKETS];
103 cpumask_var_t cpu_mask;
112 free_work_fn *free_work;
113 io_wq_work_fn *do_work;
115 struct io_wq_hash *hash;
117 atomic_t worker_refs;
118 struct completion worker_done;
120 struct hlist_node cpuhp_node;
122 struct task_struct *task;
124 struct io_wqe *wqes[];
127 static enum cpuhp_state io_wq_online;
129 struct io_cb_cancel_data {
137 static bool create_io_worker(struct io_wq *wq, struct io_wqe *wqe, int index);
138 static void io_wqe_dec_running(struct io_worker *worker);
139 static bool io_acct_cancel_pending_work(struct io_wqe *wqe,
140 struct io_wqe_acct *acct,
141 struct io_cb_cancel_data *match);
143 static bool io_worker_get(struct io_worker *worker)
145 return refcount_inc_not_zero(&worker->ref);
148 static void io_worker_release(struct io_worker *worker)
150 if (refcount_dec_and_test(&worker->ref))
151 complete(&worker->ref_done);
154 static inline struct io_wqe_acct *io_get_acct(struct io_wqe *wqe, bool bound)
156 return &wqe->acct[bound ? IO_WQ_ACCT_BOUND : IO_WQ_ACCT_UNBOUND];
159 static inline struct io_wqe_acct *io_work_get_acct(struct io_wqe *wqe,
160 struct io_wq_work *work)
162 return io_get_acct(wqe, !(work->flags & IO_WQ_WORK_UNBOUND));
165 static inline struct io_wqe_acct *io_wqe_get_acct(struct io_worker *worker)
167 return io_get_acct(worker->wqe, worker->flags & IO_WORKER_F_BOUND);
170 static void io_worker_ref_put(struct io_wq *wq)
172 if (atomic_dec_and_test(&wq->worker_refs))
173 complete(&wq->worker_done);
176 static void io_worker_exit(struct io_worker *worker)
178 struct io_wqe *wqe = worker->wqe;
179 struct io_wqe_acct *acct = io_wqe_get_acct(worker);
181 if (refcount_dec_and_test(&worker->ref))
182 complete(&worker->ref_done);
183 wait_for_completion(&worker->ref_done);
185 raw_spin_lock(&wqe->lock);
186 if (worker->flags & IO_WORKER_F_FREE)
187 hlist_nulls_del_rcu(&worker->nulls_node);
188 list_del_rcu(&worker->all_list);
191 io_wqe_dec_running(worker);
193 current->flags &= ~PF_IO_WORKER;
195 raw_spin_unlock(&wqe->lock);
197 kfree_rcu(worker, rcu);
198 io_worker_ref_put(wqe->wq);
202 static inline bool io_acct_run_queue(struct io_wqe_acct *acct)
204 if (!wq_list_empty(&acct->work_list) &&
205 !test_bit(IO_ACCT_STALLED_BIT, &acct->flags))
211 * Check head of free list for an available worker. If one isn't available,
212 * caller must create one.
214 static bool io_wqe_activate_free_worker(struct io_wqe *wqe,
215 struct io_wqe_acct *acct)
218 struct hlist_nulls_node *n;
219 struct io_worker *worker;
222 * Iterate free_list and see if we can find an idle worker to
223 * activate. If a given worker is on the free_list but in the process
224 * of exiting, keep trying.
226 hlist_nulls_for_each_entry_rcu(worker, n, &wqe->free_list, nulls_node) {
227 if (!io_worker_get(worker))
229 if (io_wqe_get_acct(worker) != acct) {
230 io_worker_release(worker);
233 if (wake_up_process(worker->task)) {
234 io_worker_release(worker);
237 io_worker_release(worker);
244 * We need a worker. If we find a free one, we're good. If not, and we're
245 * below the max number of workers, create one.
247 static bool io_wqe_create_worker(struct io_wqe *wqe, struct io_wqe_acct *acct)
249 bool do_create = false;
252 * Most likely an attempt to queue unbounded work on an io_wq that
253 * wasn't setup with any unbounded workers.
255 if (unlikely(!acct->max_workers))
256 pr_warn_once("io-wq is not configured for unbound workers");
258 raw_spin_lock(&wqe->lock);
259 if (acct->nr_workers < acct->max_workers) {
263 raw_spin_unlock(&wqe->lock);
265 atomic_inc(&acct->nr_running);
266 atomic_inc(&wqe->wq->worker_refs);
267 return create_io_worker(wqe->wq, wqe, acct->index);
273 static void io_wqe_inc_running(struct io_worker *worker)
275 struct io_wqe_acct *acct = io_wqe_get_acct(worker);
277 atomic_inc(&acct->nr_running);
280 static void create_worker_cb(struct callback_head *cb)
282 struct io_worker *worker;
285 struct io_wqe_acct *acct;
286 bool do_create = false;
288 worker = container_of(cb, struct io_worker, create_work);
291 acct = &wqe->acct[worker->create_index];
292 raw_spin_lock(&wqe->lock);
293 if (acct->nr_workers < acct->max_workers) {
297 raw_spin_unlock(&wqe->lock);
299 create_io_worker(wq, wqe, worker->create_index);
301 atomic_dec(&acct->nr_running);
302 io_worker_ref_put(wq);
304 clear_bit_unlock(0, &worker->create_state);
305 io_worker_release(worker);
308 static bool io_queue_worker_create(struct io_worker *worker,
309 struct io_wqe_acct *acct,
310 task_work_func_t func)
312 struct io_wqe *wqe = worker->wqe;
313 struct io_wq *wq = wqe->wq;
315 /* raced with exit, just ignore create call */
316 if (test_bit(IO_WQ_BIT_EXIT, &wq->state))
318 if (!io_worker_get(worker))
321 * create_state manages ownership of create_work/index. We should
322 * only need one entry per worker, as the worker going to sleep
323 * will trigger the condition, and waking will clear it once it
324 * runs the task_work.
326 if (test_bit(0, &worker->create_state) ||
327 test_and_set_bit_lock(0, &worker->create_state))
330 init_task_work(&worker->create_work, func);
331 worker->create_index = acct->index;
332 if (!task_work_add(wq->task, &worker->create_work, TWA_SIGNAL))
334 clear_bit_unlock(0, &worker->create_state);
336 io_worker_release(worker);
338 atomic_dec(&acct->nr_running);
339 io_worker_ref_put(wq);
343 static void io_wqe_dec_running(struct io_worker *worker)
344 __must_hold(wqe->lock)
346 struct io_wqe_acct *acct = io_wqe_get_acct(worker);
347 struct io_wqe *wqe = worker->wqe;
349 if (!(worker->flags & IO_WORKER_F_UP))
352 if (atomic_dec_and_test(&acct->nr_running) && io_acct_run_queue(acct)) {
353 atomic_inc(&acct->nr_running);
354 atomic_inc(&wqe->wq->worker_refs);
355 io_queue_worker_create(worker, acct, create_worker_cb);
360 * Worker will start processing some work. Move it to the busy list, if
361 * it's currently on the freelist
363 static void __io_worker_busy(struct io_wqe *wqe, struct io_worker *worker,
364 struct io_wq_work *work)
365 __must_hold(wqe->lock)
367 if (worker->flags & IO_WORKER_F_FREE) {
368 worker->flags &= ~IO_WORKER_F_FREE;
369 hlist_nulls_del_init_rcu(&worker->nulls_node);
374 * No work, worker going to sleep. Move to freelist, and unuse mm if we
375 * have one attached. Dropping the mm may potentially sleep, so we drop
376 * the lock in that case and return success. Since the caller has to
377 * retry the loop in that case (we changed task state), we don't regrab
378 * the lock if we return success.
380 static void __io_worker_idle(struct io_wqe *wqe, struct io_worker *worker)
381 __must_hold(wqe->lock)
383 if (!(worker->flags & IO_WORKER_F_FREE)) {
384 worker->flags |= IO_WORKER_F_FREE;
385 hlist_nulls_add_head_rcu(&worker->nulls_node, &wqe->free_list);
389 static inline unsigned int io_get_work_hash(struct io_wq_work *work)
391 return work->flags >> IO_WQ_HASH_SHIFT;
394 static void io_wait_on_hash(struct io_wqe *wqe, unsigned int hash)
396 struct io_wq *wq = wqe->wq;
398 spin_lock_irq(&wq->hash->wait.lock);
399 if (list_empty(&wqe->wait.entry)) {
400 __add_wait_queue(&wq->hash->wait, &wqe->wait);
401 if (!test_bit(hash, &wq->hash->map)) {
402 __set_current_state(TASK_RUNNING);
403 list_del_init(&wqe->wait.entry);
406 spin_unlock_irq(&wq->hash->wait.lock);
409 static struct io_wq_work *io_get_next_work(struct io_wqe_acct *acct,
410 struct io_worker *worker)
411 __must_hold(wqe->lock)
413 struct io_wq_work_node *node, *prev;
414 struct io_wq_work *work, *tail;
415 unsigned int stall_hash = -1U;
416 struct io_wqe *wqe = worker->wqe;
418 wq_list_for_each(node, prev, &acct->work_list) {
421 work = container_of(node, struct io_wq_work, list);
423 /* not hashed, can run anytime */
424 if (!io_wq_is_hashed(work)) {
425 wq_list_del(&acct->work_list, node, prev);
429 hash = io_get_work_hash(work);
430 /* all items with this hash lie in [work, tail] */
431 tail = wqe->hash_tail[hash];
433 /* hashed, can run if not already running */
434 if (!test_and_set_bit(hash, &wqe->wq->hash->map)) {
435 wqe->hash_tail[hash] = NULL;
436 wq_list_cut(&acct->work_list, &tail->list, prev);
439 if (stall_hash == -1U)
441 /* fast forward to a next hash, for-each will fix up @prev */
445 if (stall_hash != -1U) {
447 * Set this before dropping the lock to avoid racing with new
448 * work being added and clearing the stalled bit.
450 set_bit(IO_ACCT_STALLED_BIT, &acct->flags);
451 raw_spin_unlock(&wqe->lock);
452 io_wait_on_hash(wqe, stall_hash);
453 raw_spin_lock(&wqe->lock);
459 static bool io_flush_signals(void)
461 if (unlikely(test_thread_flag(TIF_NOTIFY_SIGNAL))) {
462 __set_current_state(TASK_RUNNING);
463 tracehook_notify_signal();
469 static void io_assign_current_work(struct io_worker *worker,
470 struct io_wq_work *work)
477 spin_lock(&worker->lock);
478 worker->cur_work = work;
479 spin_unlock(&worker->lock);
482 static void io_wqe_enqueue(struct io_wqe *wqe, struct io_wq_work *work);
484 static void io_worker_handle_work(struct io_worker *worker)
485 __releases(wqe->lock)
487 struct io_wqe_acct *acct = io_wqe_get_acct(worker);
488 struct io_wqe *wqe = worker->wqe;
489 struct io_wq *wq = wqe->wq;
490 bool do_kill = test_bit(IO_WQ_BIT_EXIT, &wq->state);
493 struct io_wq_work *work;
496 * If we got some work, mark us as busy. If we didn't, but
497 * the list isn't empty, it means we stalled on hashed work.
498 * Mark us stalled so we don't keep looking for work when we
499 * can't make progress, any work completion or insertion will
500 * clear the stalled flag.
502 work = io_get_next_work(acct, worker);
504 __io_worker_busy(wqe, worker, work);
506 raw_spin_unlock(&wqe->lock);
509 io_assign_current_work(worker, work);
510 __set_current_state(TASK_RUNNING);
512 /* handle a whole dependent link */
514 struct io_wq_work *next_hashed, *linked;
515 unsigned int hash = io_get_work_hash(work);
517 next_hashed = wq_next_work(work);
519 if (unlikely(do_kill) && (work->flags & IO_WQ_WORK_UNBOUND))
520 work->flags |= IO_WQ_WORK_CANCEL;
522 io_assign_current_work(worker, NULL);
524 linked = wq->free_work(work);
526 if (!work && linked && !io_wq_is_hashed(linked)) {
530 io_assign_current_work(worker, work);
532 io_wqe_enqueue(wqe, linked);
534 if (hash != -1U && !next_hashed) {
535 clear_bit(hash, &wq->hash->map);
536 clear_bit(IO_ACCT_STALLED_BIT, &acct->flags);
537 if (wq_has_sleeper(&wq->hash->wait))
538 wake_up(&wq->hash->wait);
539 raw_spin_lock(&wqe->lock);
540 /* skip unnecessary unlock-lock wqe->lock */
543 raw_spin_unlock(&wqe->lock);
547 raw_spin_lock(&wqe->lock);
551 static int io_wqe_worker(void *data)
553 struct io_worker *worker = data;
554 struct io_wqe_acct *acct = io_wqe_get_acct(worker);
555 struct io_wqe *wqe = worker->wqe;
556 struct io_wq *wq = wqe->wq;
557 bool last_timeout = false;
558 char buf[TASK_COMM_LEN];
560 worker->flags |= (IO_WORKER_F_UP | IO_WORKER_F_RUNNING);
562 snprintf(buf, sizeof(buf), "iou-wrk-%d", wq->task->pid);
563 set_task_comm(current, buf);
565 while (!test_bit(IO_WQ_BIT_EXIT, &wq->state)) {
568 set_current_state(TASK_INTERRUPTIBLE);
570 raw_spin_lock(&wqe->lock);
571 if (io_acct_run_queue(acct)) {
572 io_worker_handle_work(worker);
575 /* timed out, exit unless we're the last worker */
576 if (last_timeout && acct->nr_workers > 1) {
577 raw_spin_unlock(&wqe->lock);
578 __set_current_state(TASK_RUNNING);
581 last_timeout = false;
582 __io_worker_idle(wqe, worker);
583 raw_spin_unlock(&wqe->lock);
584 if (io_flush_signals())
586 ret = schedule_timeout(WORKER_IDLE_TIMEOUT);
587 if (signal_pending(current)) {
590 if (!get_signal(&ksig))
592 if (fatal_signal_pending(current))
599 if (test_bit(IO_WQ_BIT_EXIT, &wq->state)) {
600 raw_spin_lock(&wqe->lock);
601 io_worker_handle_work(worker);
604 io_worker_exit(worker);
609 * Called when a worker is scheduled in. Mark us as currently running.
611 void io_wq_worker_running(struct task_struct *tsk)
613 struct io_worker *worker = tsk->pf_io_worker;
617 if (!(worker->flags & IO_WORKER_F_UP))
619 if (worker->flags & IO_WORKER_F_RUNNING)
621 worker->flags |= IO_WORKER_F_RUNNING;
622 io_wqe_inc_running(worker);
626 * Called when worker is going to sleep. If there are no workers currently
627 * running and we have work pending, wake up a free one or create a new one.
629 void io_wq_worker_sleeping(struct task_struct *tsk)
631 struct io_worker *worker = tsk->pf_io_worker;
635 if (!(worker->flags & IO_WORKER_F_UP))
637 if (!(worker->flags & IO_WORKER_F_RUNNING))
640 worker->flags &= ~IO_WORKER_F_RUNNING;
642 raw_spin_lock(&worker->wqe->lock);
643 io_wqe_dec_running(worker);
644 raw_spin_unlock(&worker->wqe->lock);
647 static void io_init_new_worker(struct io_wqe *wqe, struct io_worker *worker,
648 struct task_struct *tsk)
650 tsk->pf_io_worker = worker;
652 set_cpus_allowed_ptr(tsk, wqe->cpu_mask);
653 tsk->flags |= PF_NO_SETAFFINITY;
655 raw_spin_lock(&wqe->lock);
656 hlist_nulls_add_head_rcu(&worker->nulls_node, &wqe->free_list);
657 list_add_tail_rcu(&worker->all_list, &wqe->all_list);
658 worker->flags |= IO_WORKER_F_FREE;
659 raw_spin_unlock(&wqe->lock);
660 wake_up_new_task(tsk);
663 static bool io_wq_work_match_all(struct io_wq_work *work, void *data)
668 static inline bool io_should_retry_thread(long err)
673 case -ERESTARTNOINTR:
674 case -ERESTARTNOHAND:
681 static void create_worker_cont(struct callback_head *cb)
683 struct io_worker *worker;
684 struct task_struct *tsk;
687 worker = container_of(cb, struct io_worker, create_work);
688 clear_bit_unlock(0, &worker->create_state);
690 tsk = create_io_thread(io_wqe_worker, worker, wqe->node);
692 io_init_new_worker(wqe, worker, tsk);
693 io_worker_release(worker);
695 } else if (!io_should_retry_thread(PTR_ERR(tsk))) {
696 struct io_wqe_acct *acct = io_wqe_get_acct(worker);
698 atomic_dec(&acct->nr_running);
699 raw_spin_lock(&wqe->lock);
701 if (!acct->nr_workers) {
702 struct io_cb_cancel_data match = {
703 .fn = io_wq_work_match_all,
707 while (io_acct_cancel_pending_work(wqe, acct, &match))
708 raw_spin_lock(&wqe->lock);
710 raw_spin_unlock(&wqe->lock);
711 io_worker_ref_put(wqe->wq);
716 /* re-create attempts grab a new worker ref, drop the existing one */
717 io_worker_release(worker);
718 schedule_work(&worker->work);
721 static void io_workqueue_create(struct work_struct *work)
723 struct io_worker *worker = container_of(work, struct io_worker, work);
724 struct io_wqe_acct *acct = io_wqe_get_acct(worker);
726 if (!io_queue_worker_create(worker, acct, create_worker_cont)) {
727 clear_bit_unlock(0, &worker->create_state);
728 io_worker_release(worker);
733 static bool create_io_worker(struct io_wq *wq, struct io_wqe *wqe, int index)
735 struct io_wqe_acct *acct = &wqe->acct[index];
736 struct io_worker *worker;
737 struct task_struct *tsk;
739 __set_current_state(TASK_RUNNING);
741 worker = kzalloc_node(sizeof(*worker), GFP_KERNEL, wqe->node);
744 atomic_dec(&acct->nr_running);
745 raw_spin_lock(&wqe->lock);
747 raw_spin_unlock(&wqe->lock);
748 io_worker_ref_put(wq);
752 refcount_set(&worker->ref, 1);
754 spin_lock_init(&worker->lock);
755 init_completion(&worker->ref_done);
757 if (index == IO_WQ_ACCT_BOUND)
758 worker->flags |= IO_WORKER_F_BOUND;
760 tsk = create_io_thread(io_wqe_worker, worker, wqe->node);
762 io_init_new_worker(wqe, worker, tsk);
763 } else if (!io_should_retry_thread(PTR_ERR(tsk))) {
767 INIT_WORK(&worker->work, io_workqueue_create);
768 schedule_work(&worker->work);
775 * Iterate the passed in list and call the specific function for each
776 * worker that isn't exiting
778 static bool io_wq_for_each_worker(struct io_wqe *wqe,
779 bool (*func)(struct io_worker *, void *),
782 struct io_worker *worker;
785 list_for_each_entry_rcu(worker, &wqe->all_list, all_list) {
786 if (io_worker_get(worker)) {
787 /* no task if node is/was offline */
789 ret = func(worker, data);
790 io_worker_release(worker);
799 static bool io_wq_worker_wake(struct io_worker *worker, void *data)
801 set_notify_signal(worker->task);
802 wake_up_process(worker->task);
806 static void io_run_cancel(struct io_wq_work *work, struct io_wqe *wqe)
808 struct io_wq *wq = wqe->wq;
811 work->flags |= IO_WQ_WORK_CANCEL;
813 work = wq->free_work(work);
817 static void io_wqe_insert_work(struct io_wqe *wqe, struct io_wq_work *work)
819 struct io_wqe_acct *acct = io_work_get_acct(wqe, work);
821 struct io_wq_work *tail;
823 if (!io_wq_is_hashed(work)) {
825 wq_list_add_tail(&work->list, &acct->work_list);
829 hash = io_get_work_hash(work);
830 tail = wqe->hash_tail[hash];
831 wqe->hash_tail[hash] = work;
835 wq_list_add_after(&work->list, &tail->list, &acct->work_list);
838 static bool io_wq_work_match_item(struct io_wq_work *work, void *data)
843 static void io_wqe_enqueue(struct io_wqe *wqe, struct io_wq_work *work)
845 struct io_wqe_acct *acct = io_work_get_acct(wqe, work);
846 unsigned work_flags = work->flags;
850 * If io-wq is exiting for this task, or if the request has explicitly
851 * been marked as one that should not get executed, cancel it here.
853 if (test_bit(IO_WQ_BIT_EXIT, &wqe->wq->state) ||
854 (work->flags & IO_WQ_WORK_CANCEL)) {
855 io_run_cancel(work, wqe);
859 raw_spin_lock(&wqe->lock);
860 io_wqe_insert_work(wqe, work);
861 clear_bit(IO_ACCT_STALLED_BIT, &acct->flags);
864 do_create = !io_wqe_activate_free_worker(wqe, acct);
867 raw_spin_unlock(&wqe->lock);
869 if (do_create && ((work_flags & IO_WQ_WORK_CONCURRENT) ||
870 !atomic_read(&acct->nr_running))) {
873 did_create = io_wqe_create_worker(wqe, acct);
874 if (likely(did_create))
877 raw_spin_lock(&wqe->lock);
878 /* fatal condition, failed to create the first worker */
879 if (!acct->nr_workers) {
880 struct io_cb_cancel_data match = {
881 .fn = io_wq_work_match_item,
886 if (io_acct_cancel_pending_work(wqe, acct, &match))
887 raw_spin_lock(&wqe->lock);
889 raw_spin_unlock(&wqe->lock);
893 void io_wq_enqueue(struct io_wq *wq, struct io_wq_work *work)
895 struct io_wqe *wqe = wq->wqes[numa_node_id()];
897 io_wqe_enqueue(wqe, work);
901 * Work items that hash to the same value will not be done in parallel.
902 * Used to limit concurrent writes, generally hashed by inode.
904 void io_wq_hash_work(struct io_wq_work *work, void *val)
908 bit = hash_ptr(val, IO_WQ_HASH_ORDER);
909 work->flags |= (IO_WQ_WORK_HASHED | (bit << IO_WQ_HASH_SHIFT));
912 static bool io_wq_worker_cancel(struct io_worker *worker, void *data)
914 struct io_cb_cancel_data *match = data;
917 * Hold the lock to avoid ->cur_work going out of scope, caller
918 * may dereference the passed in work.
920 spin_lock(&worker->lock);
921 if (worker->cur_work &&
922 match->fn(worker->cur_work, match->data)) {
923 set_notify_signal(worker->task);
926 spin_unlock(&worker->lock);
928 return match->nr_running && !match->cancel_all;
931 static inline void io_wqe_remove_pending(struct io_wqe *wqe,
932 struct io_wq_work *work,
933 struct io_wq_work_node *prev)
935 struct io_wqe_acct *acct = io_work_get_acct(wqe, work);
936 unsigned int hash = io_get_work_hash(work);
937 struct io_wq_work *prev_work = NULL;
939 if (io_wq_is_hashed(work) && work == wqe->hash_tail[hash]) {
941 prev_work = container_of(prev, struct io_wq_work, list);
942 if (prev_work && io_get_work_hash(prev_work) == hash)
943 wqe->hash_tail[hash] = prev_work;
945 wqe->hash_tail[hash] = NULL;
947 wq_list_del(&acct->work_list, &work->list, prev);
950 static bool io_acct_cancel_pending_work(struct io_wqe *wqe,
951 struct io_wqe_acct *acct,
952 struct io_cb_cancel_data *match)
953 __releases(wqe->lock)
955 struct io_wq_work_node *node, *prev;
956 struct io_wq_work *work;
958 wq_list_for_each(node, prev, &acct->work_list) {
959 work = container_of(node, struct io_wq_work, list);
960 if (!match->fn(work, match->data))
962 io_wqe_remove_pending(wqe, work, prev);
963 raw_spin_unlock(&wqe->lock);
964 io_run_cancel(work, wqe);
966 /* not safe to continue after unlock */
973 static void io_wqe_cancel_pending_work(struct io_wqe *wqe,
974 struct io_cb_cancel_data *match)
978 raw_spin_lock(&wqe->lock);
979 for (i = 0; i < IO_WQ_ACCT_NR; i++) {
980 struct io_wqe_acct *acct = io_get_acct(wqe, i == 0);
982 if (io_acct_cancel_pending_work(wqe, acct, match)) {
983 if (match->cancel_all)
988 raw_spin_unlock(&wqe->lock);
991 static void io_wqe_cancel_running_work(struct io_wqe *wqe,
992 struct io_cb_cancel_data *match)
995 io_wq_for_each_worker(wqe, io_wq_worker_cancel, match);
999 enum io_wq_cancel io_wq_cancel_cb(struct io_wq *wq, work_cancel_fn *cancel,
1000 void *data, bool cancel_all)
1002 struct io_cb_cancel_data match = {
1005 .cancel_all = cancel_all,
1010 * First check pending list, if we're lucky we can just remove it
1011 * from there. CANCEL_OK means that the work is returned as-new,
1012 * no completion will be posted for it.
1014 for_each_node(node) {
1015 struct io_wqe *wqe = wq->wqes[node];
1017 io_wqe_cancel_pending_work(wqe, &match);
1018 if (match.nr_pending && !match.cancel_all)
1019 return IO_WQ_CANCEL_OK;
1023 * Now check if a free (going busy) or busy worker has the work
1024 * currently running. If we find it there, we'll return CANCEL_RUNNING
1025 * as an indication that we attempt to signal cancellation. The
1026 * completion will run normally in this case.
1028 for_each_node(node) {
1029 struct io_wqe *wqe = wq->wqes[node];
1031 io_wqe_cancel_running_work(wqe, &match);
1032 if (match.nr_running && !match.cancel_all)
1033 return IO_WQ_CANCEL_RUNNING;
1036 if (match.nr_running)
1037 return IO_WQ_CANCEL_RUNNING;
1038 if (match.nr_pending)
1039 return IO_WQ_CANCEL_OK;
1040 return IO_WQ_CANCEL_NOTFOUND;
1043 static int io_wqe_hash_wake(struct wait_queue_entry *wait, unsigned mode,
1044 int sync, void *key)
1046 struct io_wqe *wqe = container_of(wait, struct io_wqe, wait);
1049 list_del_init(&wait->entry);
1052 for (i = 0; i < IO_WQ_ACCT_NR; i++) {
1053 struct io_wqe_acct *acct = &wqe->acct[i];
1055 if (test_and_clear_bit(IO_ACCT_STALLED_BIT, &acct->flags))
1056 io_wqe_activate_free_worker(wqe, acct);
1062 struct io_wq *io_wq_create(unsigned bounded, struct io_wq_data *data)
1067 if (WARN_ON_ONCE(!data->free_work || !data->do_work))
1068 return ERR_PTR(-EINVAL);
1069 if (WARN_ON_ONCE(!bounded))
1070 return ERR_PTR(-EINVAL);
1072 wq = kzalloc(struct_size(wq, wqes, nr_node_ids), GFP_KERNEL);
1074 return ERR_PTR(-ENOMEM);
1075 ret = cpuhp_state_add_instance_nocalls(io_wq_online, &wq->cpuhp_node);
1079 refcount_inc(&data->hash->refs);
1080 wq->hash = data->hash;
1081 wq->free_work = data->free_work;
1082 wq->do_work = data->do_work;
1085 for_each_node(node) {
1087 int alloc_node = node;
1089 if (!node_online(alloc_node))
1090 alloc_node = NUMA_NO_NODE;
1091 wqe = kzalloc_node(sizeof(struct io_wqe), GFP_KERNEL, alloc_node);
1094 if (!alloc_cpumask_var(&wqe->cpu_mask, GFP_KERNEL))
1096 cpumask_copy(wqe->cpu_mask, cpumask_of_node(node));
1097 wq->wqes[node] = wqe;
1098 wqe->node = alloc_node;
1099 wqe->acct[IO_WQ_ACCT_BOUND].max_workers = bounded;
1100 wqe->acct[IO_WQ_ACCT_UNBOUND].max_workers =
1101 task_rlimit(current, RLIMIT_NPROC);
1102 INIT_LIST_HEAD(&wqe->wait.entry);
1103 wqe->wait.func = io_wqe_hash_wake;
1104 for (i = 0; i < IO_WQ_ACCT_NR; i++) {
1105 struct io_wqe_acct *acct = &wqe->acct[i];
1108 atomic_set(&acct->nr_running, 0);
1109 INIT_WQ_LIST(&acct->work_list);
1112 raw_spin_lock_init(&wqe->lock);
1113 INIT_HLIST_NULLS_HEAD(&wqe->free_list, 0);
1114 INIT_LIST_HEAD(&wqe->all_list);
1117 wq->task = get_task_struct(data->task);
1118 atomic_set(&wq->worker_refs, 1);
1119 init_completion(&wq->worker_done);
1122 io_wq_put_hash(data->hash);
1123 cpuhp_state_remove_instance_nocalls(io_wq_online, &wq->cpuhp_node);
1124 for_each_node(node) {
1125 if (!wq->wqes[node])
1127 free_cpumask_var(wq->wqes[node]->cpu_mask);
1128 kfree(wq->wqes[node]);
1132 return ERR_PTR(ret);
1135 static bool io_task_work_match(struct callback_head *cb, void *data)
1137 struct io_worker *worker;
1139 if (cb->func != create_worker_cb && cb->func != create_worker_cont)
1141 worker = container_of(cb, struct io_worker, create_work);
1142 return worker->wqe->wq == data;
1145 void io_wq_exit_start(struct io_wq *wq)
1147 set_bit(IO_WQ_BIT_EXIT, &wq->state);
1150 static void io_wq_exit_workers(struct io_wq *wq)
1152 struct callback_head *cb;
1158 while ((cb = task_work_cancel_match(wq->task, io_task_work_match, wq)) != NULL) {
1159 struct io_worker *worker;
1160 struct io_wqe_acct *acct;
1162 worker = container_of(cb, struct io_worker, create_work);
1163 acct = io_wqe_get_acct(worker);
1164 atomic_dec(&acct->nr_running);
1165 raw_spin_lock(&worker->wqe->lock);
1167 raw_spin_unlock(&worker->wqe->lock);
1168 io_worker_ref_put(wq);
1169 clear_bit_unlock(0, &worker->create_state);
1170 io_worker_release(worker);
1174 for_each_node(node) {
1175 struct io_wqe *wqe = wq->wqes[node];
1177 io_wq_for_each_worker(wqe, io_wq_worker_wake, NULL);
1180 io_worker_ref_put(wq);
1181 wait_for_completion(&wq->worker_done);
1183 for_each_node(node) {
1184 spin_lock_irq(&wq->hash->wait.lock);
1185 list_del_init(&wq->wqes[node]->wait.entry);
1186 spin_unlock_irq(&wq->hash->wait.lock);
1188 put_task_struct(wq->task);
1192 static void io_wq_destroy(struct io_wq *wq)
1196 cpuhp_state_remove_instance_nocalls(io_wq_online, &wq->cpuhp_node);
1198 for_each_node(node) {
1199 struct io_wqe *wqe = wq->wqes[node];
1200 struct io_cb_cancel_data match = {
1201 .fn = io_wq_work_match_all,
1204 io_wqe_cancel_pending_work(wqe, &match);
1205 free_cpumask_var(wqe->cpu_mask);
1208 io_wq_put_hash(wq->hash);
1212 void io_wq_put_and_exit(struct io_wq *wq)
1214 WARN_ON_ONCE(!test_bit(IO_WQ_BIT_EXIT, &wq->state));
1216 io_wq_exit_workers(wq);
1220 struct online_data {
1225 static bool io_wq_worker_affinity(struct io_worker *worker, void *data)
1227 struct online_data *od = data;
1230 cpumask_set_cpu(od->cpu, worker->wqe->cpu_mask);
1232 cpumask_clear_cpu(od->cpu, worker->wqe->cpu_mask);
1236 static int __io_wq_cpu_online(struct io_wq *wq, unsigned int cpu, bool online)
1238 struct online_data od = {
1246 io_wq_for_each_worker(wq->wqes[i], io_wq_worker_affinity, &od);
1251 static int io_wq_cpu_online(unsigned int cpu, struct hlist_node *node)
1253 struct io_wq *wq = hlist_entry_safe(node, struct io_wq, cpuhp_node);
1255 return __io_wq_cpu_online(wq, cpu, true);
1258 static int io_wq_cpu_offline(unsigned int cpu, struct hlist_node *node)
1260 struct io_wq *wq = hlist_entry_safe(node, struct io_wq, cpuhp_node);
1262 return __io_wq_cpu_online(wq, cpu, false);
1265 int io_wq_cpu_affinity(struct io_wq *wq, cpumask_var_t mask)
1271 struct io_wqe *wqe = wq->wqes[i];
1274 cpumask_copy(wqe->cpu_mask, mask);
1276 cpumask_copy(wqe->cpu_mask, cpumask_of_node(i));
1283 * Set max number of unbounded workers, returns old value. If new_count is 0,
1284 * then just return the old value.
1286 int io_wq_max_workers(struct io_wq *wq, int *new_count)
1288 int i, node, prev = 0;
1290 for (i = 0; i < 2; i++) {
1291 if (new_count[i] > task_rlimit(current, RLIMIT_NPROC))
1292 new_count[i] = task_rlimit(current, RLIMIT_NPROC);
1296 for_each_node(node) {
1297 struct io_wqe_acct *acct;
1299 for (i = 0; i < IO_WQ_ACCT_NR; i++) {
1300 acct = &wq->wqes[node]->acct[i];
1301 prev = max_t(int, acct->max_workers, prev);
1303 acct->max_workers = new_count[i];
1304 new_count[i] = prev;
1311 static __init int io_wq_init(void)
1315 ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "io-wq/online",
1316 io_wq_cpu_online, io_wq_cpu_offline);
1322 subsys_initcall(io_wq_init);