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_FIXED = 8, /* static idle worker */
27 IO_WORKER_F_BOUND = 16, /* is doing bounded work */
31 IO_WQ_BIT_EXIT = 0, /* wq exiting */
35 IO_WQE_FLAG_STALLED = 1, /* stalled on hash */
39 * One for each thread in a wqe pool
44 struct hlist_nulls_node nulls_node;
45 struct list_head all_list;
46 struct task_struct *task;
49 struct io_wq_work *cur_work;
52 struct completion ref_done;
54 unsigned long create_state;
55 struct callback_head create_work;
61 #if BITS_PER_LONG == 64
62 #define IO_WQ_HASH_ORDER 6
64 #define IO_WQ_HASH_ORDER 5
67 #define IO_WQ_NR_HASH_BUCKETS (1u << IO_WQ_HASH_ORDER)
82 * Per-node worker thread pool
87 struct io_wq_work_list work_list;
89 } ____cacheline_aligned_in_smp;
92 struct io_wqe_acct acct[2];
94 struct hlist_nulls_head free_list;
95 struct list_head all_list;
97 struct wait_queue_entry wait;
100 struct io_wq_work *hash_tail[IO_WQ_NR_HASH_BUCKETS];
102 cpumask_var_t cpu_mask;
111 free_work_fn *free_work;
112 io_wq_work_fn *do_work;
114 struct io_wq_hash *hash;
116 atomic_t worker_refs;
117 struct completion worker_done;
119 struct hlist_node cpuhp_node;
121 struct task_struct *task;
123 struct io_wqe *wqes[];
126 static enum cpuhp_state io_wq_online;
128 struct io_cb_cancel_data {
136 static void create_io_worker(struct io_wq *wq, struct io_wqe *wqe, int index, bool first);
137 static void io_wqe_dec_running(struct io_worker *worker);
139 static bool io_worker_get(struct io_worker *worker)
141 return refcount_inc_not_zero(&worker->ref);
144 static void io_worker_release(struct io_worker *worker)
146 if (refcount_dec_and_test(&worker->ref))
147 complete(&worker->ref_done);
150 static inline struct io_wqe_acct *io_get_acct(struct io_wqe *wqe, bool bound)
152 return &wqe->acct[bound ? IO_WQ_ACCT_BOUND : IO_WQ_ACCT_UNBOUND];
155 static inline struct io_wqe_acct *io_work_get_acct(struct io_wqe *wqe,
156 struct io_wq_work *work)
158 return io_get_acct(wqe, !(work->flags & IO_WQ_WORK_UNBOUND));
161 static inline struct io_wqe_acct *io_wqe_get_acct(struct io_worker *worker)
163 return io_get_acct(worker->wqe, worker->flags & IO_WORKER_F_BOUND);
166 static void io_worker_ref_put(struct io_wq *wq)
168 if (atomic_dec_and_test(&wq->worker_refs))
169 complete(&wq->worker_done);
172 static void io_worker_exit(struct io_worker *worker)
174 struct io_wqe *wqe = worker->wqe;
175 struct io_wqe_acct *acct = io_wqe_get_acct(worker);
177 if (refcount_dec_and_test(&worker->ref))
178 complete(&worker->ref_done);
179 wait_for_completion(&worker->ref_done);
181 raw_spin_lock(&wqe->lock);
182 if (worker->flags & IO_WORKER_F_FREE)
183 hlist_nulls_del_rcu(&worker->nulls_node);
184 list_del_rcu(&worker->all_list);
187 io_wqe_dec_running(worker);
189 current->flags &= ~PF_IO_WORKER;
191 raw_spin_unlock(&wqe->lock);
193 kfree_rcu(worker, rcu);
194 io_worker_ref_put(wqe->wq);
198 static inline bool io_wqe_run_queue(struct io_wqe *wqe)
199 __must_hold(wqe->lock)
201 if (!wq_list_empty(&wqe->work_list) &&
202 !(wqe->flags & IO_WQE_FLAG_STALLED))
208 * Check head of free list for an available worker. If one isn't available,
209 * caller must create one.
211 static bool io_wqe_activate_free_worker(struct io_wqe *wqe)
214 struct hlist_nulls_node *n;
215 struct io_worker *worker;
218 * Iterate free_list and see if we can find an idle worker to
219 * activate. If a given worker is on the free_list but in the process
220 * of exiting, keep trying.
222 hlist_nulls_for_each_entry_rcu(worker, n, &wqe->free_list, nulls_node) {
223 if (!io_worker_get(worker))
225 if (wake_up_process(worker->task)) {
226 io_worker_release(worker);
229 io_worker_release(worker);
236 * We need a worker. If we find a free one, we're good. If not, and we're
237 * below the max number of workers, create one.
239 static void io_wqe_wake_worker(struct io_wqe *wqe, struct io_wqe_acct *acct)
244 * Most likely an attempt to queue unbounded work on an io_wq that
245 * wasn't setup with any unbounded workers.
247 if (unlikely(!acct->max_workers))
248 pr_warn_once("io-wq is not configured for unbound workers");
251 ret = io_wqe_activate_free_worker(wqe);
255 bool do_create = false, first = false;
257 raw_spin_lock(&wqe->lock);
258 if (acct->nr_workers < acct->max_workers) {
259 if (!acct->nr_workers)
264 raw_spin_unlock(&wqe->lock);
266 atomic_inc(&acct->nr_running);
267 atomic_inc(&wqe->wq->worker_refs);
268 create_io_worker(wqe->wq, wqe, acct->index, first);
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, first = 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) {
294 if (!acct->nr_workers)
299 raw_spin_unlock(&wqe->lock);
301 create_io_worker(wq, wqe, worker->create_index, first);
303 atomic_dec(&acct->nr_running);
304 io_worker_ref_put(wq);
306 clear_bit_unlock(0, &worker->create_state);
307 io_worker_release(worker);
310 static void io_queue_worker_create(struct io_wqe *wqe, struct io_worker *worker,
311 struct io_wqe_acct *acct)
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, create_worker_cb);
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);
342 static void io_wqe_dec_running(struct io_worker *worker)
343 __must_hold(wqe->lock)
345 struct io_wqe_acct *acct = io_wqe_get_acct(worker);
346 struct io_wqe *wqe = worker->wqe;
348 if (!(worker->flags & IO_WORKER_F_UP))
351 if (atomic_dec_and_test(&acct->nr_running) && io_wqe_run_queue(wqe)) {
352 atomic_inc(&acct->nr_running);
353 atomic_inc(&wqe->wq->worker_refs);
354 io_queue_worker_create(wqe, worker, acct);
359 * Worker will start processing some work. Move it to the busy list, if
360 * it's currently on the freelist
362 static void __io_worker_busy(struct io_wqe *wqe, struct io_worker *worker,
363 struct io_wq_work *work)
364 __must_hold(wqe->lock)
366 bool worker_bound, work_bound;
368 BUILD_BUG_ON((IO_WQ_ACCT_UNBOUND ^ IO_WQ_ACCT_BOUND) != 1);
370 if (worker->flags & IO_WORKER_F_FREE) {
371 worker->flags &= ~IO_WORKER_F_FREE;
372 hlist_nulls_del_init_rcu(&worker->nulls_node);
376 * If worker is moving from bound to unbound (or vice versa), then
377 * ensure we update the running accounting.
379 worker_bound = (worker->flags & IO_WORKER_F_BOUND) != 0;
380 work_bound = (work->flags & IO_WQ_WORK_UNBOUND) == 0;
381 if (worker_bound != work_bound) {
382 int index = work_bound ? IO_WQ_ACCT_UNBOUND : IO_WQ_ACCT_BOUND;
383 io_wqe_dec_running(worker);
384 worker->flags ^= IO_WORKER_F_BOUND;
385 wqe->acct[index].nr_workers--;
386 wqe->acct[index ^ 1].nr_workers++;
387 io_wqe_inc_running(worker);
392 * No work, worker going to sleep. Move to freelist, and unuse mm if we
393 * have one attached. Dropping the mm may potentially sleep, so we drop
394 * the lock in that case and return success. Since the caller has to
395 * retry the loop in that case (we changed task state), we don't regrab
396 * the lock if we return success.
398 static void __io_worker_idle(struct io_wqe *wqe, struct io_worker *worker)
399 __must_hold(wqe->lock)
401 if (!(worker->flags & IO_WORKER_F_FREE)) {
402 worker->flags |= IO_WORKER_F_FREE;
403 hlist_nulls_add_head_rcu(&worker->nulls_node, &wqe->free_list);
407 static inline unsigned int io_get_work_hash(struct io_wq_work *work)
409 return work->flags >> IO_WQ_HASH_SHIFT;
412 static void io_wait_on_hash(struct io_wqe *wqe, unsigned int hash)
414 struct io_wq *wq = wqe->wq;
416 spin_lock(&wq->hash->wait.lock);
417 if (list_empty(&wqe->wait.entry)) {
418 __add_wait_queue(&wq->hash->wait, &wqe->wait);
419 if (!test_bit(hash, &wq->hash->map)) {
420 __set_current_state(TASK_RUNNING);
421 list_del_init(&wqe->wait.entry);
424 spin_unlock(&wq->hash->wait.lock);
428 * We can always run the work if the worker is currently the same type as
429 * the work (eg both are bound, or both are unbound). If they are not the
430 * same, only allow it if incrementing the worker count would be allowed.
432 static bool io_worker_can_run_work(struct io_worker *worker,
433 struct io_wq_work *work)
435 struct io_wqe_acct *acct;
437 if (!(worker->flags & IO_WORKER_F_BOUND) !=
438 !(work->flags & IO_WQ_WORK_UNBOUND))
441 /* not the same type, check if we'd go over the limit */
442 acct = io_work_get_acct(worker->wqe, work);
443 return acct->nr_workers < acct->max_workers;
446 static struct io_wq_work *io_get_next_work(struct io_wqe *wqe,
447 struct io_worker *worker,
449 __must_hold(wqe->lock)
451 struct io_wq_work_node *node, *prev;
452 struct io_wq_work *work, *tail;
453 unsigned int stall_hash = -1U;
455 wq_list_for_each(node, prev, &wqe->work_list) {
458 work = container_of(node, struct io_wq_work, list);
460 if (!io_worker_can_run_work(worker, work))
463 /* not hashed, can run anytime */
464 if (!io_wq_is_hashed(work)) {
465 wq_list_del(&wqe->work_list, node, prev);
469 hash = io_get_work_hash(work);
470 /* all items with this hash lie in [work, tail] */
471 tail = wqe->hash_tail[hash];
473 /* hashed, can run if not already running */
474 if (!test_and_set_bit(hash, &wqe->wq->hash->map)) {
475 wqe->hash_tail[hash] = NULL;
476 wq_list_cut(&wqe->work_list, &tail->list, prev);
479 if (stall_hash == -1U)
481 /* fast forward to a next hash, for-each will fix up @prev */
485 if (stall_hash != -1U) {
486 raw_spin_unlock(&wqe->lock);
487 io_wait_on_hash(wqe, stall_hash);
488 raw_spin_lock(&wqe->lock);
495 static bool io_flush_signals(void)
497 if (unlikely(test_thread_flag(TIF_NOTIFY_SIGNAL))) {
498 __set_current_state(TASK_RUNNING);
499 tracehook_notify_signal();
505 static void io_assign_current_work(struct io_worker *worker,
506 struct io_wq_work *work)
513 spin_lock(&worker->lock);
514 worker->cur_work = work;
515 spin_unlock(&worker->lock);
518 static void io_wqe_enqueue(struct io_wqe *wqe, struct io_wq_work *work);
520 static void io_worker_handle_work(struct io_worker *worker)
521 __releases(wqe->lock)
523 struct io_wqe *wqe = worker->wqe;
524 struct io_wq *wq = wqe->wq;
525 bool do_kill = test_bit(IO_WQ_BIT_EXIT, &wq->state);
528 struct io_wq_work *work;
532 * If we got some work, mark us as busy. If we didn't, but
533 * the list isn't empty, it means we stalled on hashed work.
534 * Mark us stalled so we don't keep looking for work when we
535 * can't make progress, any work completion or insertion will
536 * clear the stalled flag.
539 work = io_get_next_work(wqe, worker, &stalled);
541 __io_worker_busy(wqe, worker, work);
543 wqe->flags |= IO_WQE_FLAG_STALLED;
545 raw_spin_unlock(&wqe->lock);
548 io_assign_current_work(worker, work);
549 __set_current_state(TASK_RUNNING);
551 /* handle a whole dependent link */
553 struct io_wq_work *next_hashed, *linked;
554 unsigned int hash = io_get_work_hash(work);
556 next_hashed = wq_next_work(work);
558 if (unlikely(do_kill) && (work->flags & IO_WQ_WORK_UNBOUND))
559 work->flags |= IO_WQ_WORK_CANCEL;
561 io_assign_current_work(worker, NULL);
563 linked = wq->free_work(work);
565 if (!work && linked && !io_wq_is_hashed(linked)) {
569 io_assign_current_work(worker, work);
571 io_wqe_enqueue(wqe, linked);
573 if (hash != -1U && !next_hashed) {
574 clear_bit(hash, &wq->hash->map);
575 if (wq_has_sleeper(&wq->hash->wait))
576 wake_up(&wq->hash->wait);
577 raw_spin_lock(&wqe->lock);
578 wqe->flags &= ~IO_WQE_FLAG_STALLED;
579 /* skip unnecessary unlock-lock wqe->lock */
582 raw_spin_unlock(&wqe->lock);
586 raw_spin_lock(&wqe->lock);
590 static int io_wqe_worker(void *data)
592 struct io_worker *worker = data;
593 struct io_wqe *wqe = worker->wqe;
594 struct io_wq *wq = wqe->wq;
595 char buf[TASK_COMM_LEN];
597 worker->flags |= (IO_WORKER_F_UP | IO_WORKER_F_RUNNING);
599 snprintf(buf, sizeof(buf), "iou-wrk-%d", wq->task->pid);
600 set_task_comm(current, buf);
602 while (!test_bit(IO_WQ_BIT_EXIT, &wq->state)) {
605 set_current_state(TASK_INTERRUPTIBLE);
607 raw_spin_lock(&wqe->lock);
608 if (io_wqe_run_queue(wqe)) {
609 io_worker_handle_work(worker);
612 __io_worker_idle(wqe, worker);
613 raw_spin_unlock(&wqe->lock);
614 if (io_flush_signals())
616 ret = schedule_timeout(WORKER_IDLE_TIMEOUT);
617 if (signal_pending(current)) {
620 if (!get_signal(&ksig))
626 /* timed out, exit unless we're the fixed worker */
627 if (!(worker->flags & IO_WORKER_F_FIXED))
631 if (test_bit(IO_WQ_BIT_EXIT, &wq->state)) {
632 raw_spin_lock(&wqe->lock);
633 io_worker_handle_work(worker);
636 io_worker_exit(worker);
641 * Called when a worker is scheduled in. Mark us as currently running.
643 void io_wq_worker_running(struct task_struct *tsk)
645 struct io_worker *worker = tsk->pf_io_worker;
649 if (!(worker->flags & IO_WORKER_F_UP))
651 if (worker->flags & IO_WORKER_F_RUNNING)
653 worker->flags |= IO_WORKER_F_RUNNING;
654 io_wqe_inc_running(worker);
658 * Called when worker is going to sleep. If there are no workers currently
659 * running and we have work pending, wake up a free one or create a new one.
661 void io_wq_worker_sleeping(struct task_struct *tsk)
663 struct io_worker *worker = tsk->pf_io_worker;
667 if (!(worker->flags & IO_WORKER_F_UP))
669 if (!(worker->flags & IO_WORKER_F_RUNNING))
672 worker->flags &= ~IO_WORKER_F_RUNNING;
674 raw_spin_lock(&worker->wqe->lock);
675 io_wqe_dec_running(worker);
676 raw_spin_unlock(&worker->wqe->lock);
679 static void create_io_worker(struct io_wq *wq, struct io_wqe *wqe, int index, bool first)
681 struct io_wqe_acct *acct = &wqe->acct[index];
682 struct io_worker *worker;
683 struct task_struct *tsk;
685 __set_current_state(TASK_RUNNING);
687 worker = kzalloc_node(sizeof(*worker), GFP_KERNEL, wqe->node);
691 refcount_set(&worker->ref, 1);
692 worker->nulls_node.pprev = NULL;
694 spin_lock_init(&worker->lock);
695 init_completion(&worker->ref_done);
697 tsk = create_io_thread(io_wqe_worker, worker, wqe->node);
701 atomic_dec(&acct->nr_running);
702 raw_spin_lock(&wqe->lock);
704 raw_spin_unlock(&wqe->lock);
705 io_worker_ref_put(wq);
709 tsk->pf_io_worker = worker;
711 set_cpus_allowed_ptr(tsk, wqe->cpu_mask);
712 tsk->flags |= PF_NO_SETAFFINITY;
714 raw_spin_lock(&wqe->lock);
715 hlist_nulls_add_head_rcu(&worker->nulls_node, &wqe->free_list);
716 list_add_tail_rcu(&worker->all_list, &wqe->all_list);
717 worker->flags |= IO_WORKER_F_FREE;
718 if (index == IO_WQ_ACCT_BOUND)
719 worker->flags |= IO_WORKER_F_BOUND;
720 if (first && (worker->flags & IO_WORKER_F_BOUND))
721 worker->flags |= IO_WORKER_F_FIXED;
722 raw_spin_unlock(&wqe->lock);
723 wake_up_new_task(tsk);
727 * Iterate the passed in list and call the specific function for each
728 * worker that isn't exiting
730 static bool io_wq_for_each_worker(struct io_wqe *wqe,
731 bool (*func)(struct io_worker *, void *),
734 struct io_worker *worker;
737 list_for_each_entry_rcu(worker, &wqe->all_list, all_list) {
738 if (io_worker_get(worker)) {
739 /* no task if node is/was offline */
741 ret = func(worker, data);
742 io_worker_release(worker);
751 static bool io_wq_worker_wake(struct io_worker *worker, void *data)
753 set_notify_signal(worker->task);
754 wake_up_process(worker->task);
758 static bool io_wq_work_match_all(struct io_wq_work *work, void *data)
763 static void io_run_cancel(struct io_wq_work *work, struct io_wqe *wqe)
765 struct io_wq *wq = wqe->wq;
768 work->flags |= IO_WQ_WORK_CANCEL;
770 work = wq->free_work(work);
774 static void io_wqe_insert_work(struct io_wqe *wqe, struct io_wq_work *work)
777 struct io_wq_work *tail;
779 if (!io_wq_is_hashed(work)) {
781 wq_list_add_tail(&work->list, &wqe->work_list);
785 hash = io_get_work_hash(work);
786 tail = wqe->hash_tail[hash];
787 wqe->hash_tail[hash] = work;
791 wq_list_add_after(&work->list, &tail->list, &wqe->work_list);
794 static void io_wqe_enqueue(struct io_wqe *wqe, struct io_wq_work *work)
796 struct io_wqe_acct *acct = io_work_get_acct(wqe, work);
800 * If io-wq is exiting for this task, or if the request has explicitly
801 * been marked as one that should not get executed, cancel it here.
803 if (test_bit(IO_WQ_BIT_EXIT, &wqe->wq->state) ||
804 (work->flags & IO_WQ_WORK_CANCEL)) {
805 io_run_cancel(work, wqe);
809 work_flags = work->flags;
810 raw_spin_lock(&wqe->lock);
811 io_wqe_insert_work(wqe, work);
812 wqe->flags &= ~IO_WQE_FLAG_STALLED;
813 raw_spin_unlock(&wqe->lock);
815 if ((work_flags & IO_WQ_WORK_CONCURRENT) ||
816 !atomic_read(&acct->nr_running))
817 io_wqe_wake_worker(wqe, acct);
820 void io_wq_enqueue(struct io_wq *wq, struct io_wq_work *work)
822 struct io_wqe *wqe = wq->wqes[numa_node_id()];
824 io_wqe_enqueue(wqe, work);
828 * Work items that hash to the same value will not be done in parallel.
829 * Used to limit concurrent writes, generally hashed by inode.
831 void io_wq_hash_work(struct io_wq_work *work, void *val)
835 bit = hash_ptr(val, IO_WQ_HASH_ORDER);
836 work->flags |= (IO_WQ_WORK_HASHED | (bit << IO_WQ_HASH_SHIFT));
839 static bool io_wq_worker_cancel(struct io_worker *worker, void *data)
841 struct io_cb_cancel_data *match = data;
844 * Hold the lock to avoid ->cur_work going out of scope, caller
845 * may dereference the passed in work.
847 spin_lock(&worker->lock);
848 if (worker->cur_work &&
849 match->fn(worker->cur_work, match->data)) {
850 set_notify_signal(worker->task);
853 spin_unlock(&worker->lock);
855 return match->nr_running && !match->cancel_all;
858 static inline void io_wqe_remove_pending(struct io_wqe *wqe,
859 struct io_wq_work *work,
860 struct io_wq_work_node *prev)
862 unsigned int hash = io_get_work_hash(work);
863 struct io_wq_work *prev_work = NULL;
865 if (io_wq_is_hashed(work) && work == wqe->hash_tail[hash]) {
867 prev_work = container_of(prev, struct io_wq_work, list);
868 if (prev_work && io_get_work_hash(prev_work) == hash)
869 wqe->hash_tail[hash] = prev_work;
871 wqe->hash_tail[hash] = NULL;
873 wq_list_del(&wqe->work_list, &work->list, prev);
876 static void io_wqe_cancel_pending_work(struct io_wqe *wqe,
877 struct io_cb_cancel_data *match)
879 struct io_wq_work_node *node, *prev;
880 struct io_wq_work *work;
883 raw_spin_lock(&wqe->lock);
884 wq_list_for_each(node, prev, &wqe->work_list) {
885 work = container_of(node, struct io_wq_work, list);
886 if (!match->fn(work, match->data))
888 io_wqe_remove_pending(wqe, work, prev);
889 raw_spin_unlock(&wqe->lock);
890 io_run_cancel(work, wqe);
892 if (!match->cancel_all)
895 /* not safe to continue after unlock */
898 raw_spin_unlock(&wqe->lock);
901 static void io_wqe_cancel_running_work(struct io_wqe *wqe,
902 struct io_cb_cancel_data *match)
905 io_wq_for_each_worker(wqe, io_wq_worker_cancel, match);
909 enum io_wq_cancel io_wq_cancel_cb(struct io_wq *wq, work_cancel_fn *cancel,
910 void *data, bool cancel_all)
912 struct io_cb_cancel_data match = {
915 .cancel_all = cancel_all,
920 * First check pending list, if we're lucky we can just remove it
921 * from there. CANCEL_OK means that the work is returned as-new,
922 * no completion will be posted for it.
924 for_each_node(node) {
925 struct io_wqe *wqe = wq->wqes[node];
927 io_wqe_cancel_pending_work(wqe, &match);
928 if (match.nr_pending && !match.cancel_all)
929 return IO_WQ_CANCEL_OK;
933 * Now check if a free (going busy) or busy worker has the work
934 * currently running. If we find it there, we'll return CANCEL_RUNNING
935 * as an indication that we attempt to signal cancellation. The
936 * completion will run normally in this case.
938 for_each_node(node) {
939 struct io_wqe *wqe = wq->wqes[node];
941 io_wqe_cancel_running_work(wqe, &match);
942 if (match.nr_running && !match.cancel_all)
943 return IO_WQ_CANCEL_RUNNING;
946 if (match.nr_running)
947 return IO_WQ_CANCEL_RUNNING;
948 if (match.nr_pending)
949 return IO_WQ_CANCEL_OK;
950 return IO_WQ_CANCEL_NOTFOUND;
953 static int io_wqe_hash_wake(struct wait_queue_entry *wait, unsigned mode,
956 struct io_wqe *wqe = container_of(wait, struct io_wqe, wait);
958 list_del_init(&wait->entry);
961 io_wqe_activate_free_worker(wqe);
966 struct io_wq *io_wq_create(unsigned bounded, struct io_wq_data *data)
971 if (WARN_ON_ONCE(!data->free_work || !data->do_work))
972 return ERR_PTR(-EINVAL);
973 if (WARN_ON_ONCE(!bounded))
974 return ERR_PTR(-EINVAL);
976 wq = kzalloc(struct_size(wq, wqes, nr_node_ids), GFP_KERNEL);
978 return ERR_PTR(-ENOMEM);
979 ret = cpuhp_state_add_instance_nocalls(io_wq_online, &wq->cpuhp_node);
983 refcount_inc(&data->hash->refs);
984 wq->hash = data->hash;
985 wq->free_work = data->free_work;
986 wq->do_work = data->do_work;
989 for_each_node(node) {
991 int alloc_node = node;
993 if (!node_online(alloc_node))
994 alloc_node = NUMA_NO_NODE;
995 wqe = kzalloc_node(sizeof(struct io_wqe), GFP_KERNEL, alloc_node);
998 if (!alloc_cpumask_var(&wqe->cpu_mask, GFP_KERNEL))
1000 cpumask_copy(wqe->cpu_mask, cpumask_of_node(node));
1001 wq->wqes[node] = wqe;
1002 wqe->node = alloc_node;
1003 wqe->acct[IO_WQ_ACCT_BOUND].index = IO_WQ_ACCT_BOUND;
1004 wqe->acct[IO_WQ_ACCT_UNBOUND].index = IO_WQ_ACCT_UNBOUND;
1005 wqe->acct[IO_WQ_ACCT_BOUND].max_workers = bounded;
1006 atomic_set(&wqe->acct[IO_WQ_ACCT_BOUND].nr_running, 0);
1007 wqe->acct[IO_WQ_ACCT_UNBOUND].max_workers =
1008 task_rlimit(current, RLIMIT_NPROC);
1009 atomic_set(&wqe->acct[IO_WQ_ACCT_UNBOUND].nr_running, 0);
1010 wqe->wait.func = io_wqe_hash_wake;
1011 INIT_LIST_HEAD(&wqe->wait.entry);
1013 raw_spin_lock_init(&wqe->lock);
1014 INIT_WQ_LIST(&wqe->work_list);
1015 INIT_HLIST_NULLS_HEAD(&wqe->free_list, 0);
1016 INIT_LIST_HEAD(&wqe->all_list);
1019 wq->task = get_task_struct(data->task);
1020 atomic_set(&wq->worker_refs, 1);
1021 init_completion(&wq->worker_done);
1024 io_wq_put_hash(data->hash);
1025 cpuhp_state_remove_instance_nocalls(io_wq_online, &wq->cpuhp_node);
1026 for_each_node(node) {
1027 if (!wq->wqes[node])
1029 free_cpumask_var(wq->wqes[node]->cpu_mask);
1030 kfree(wq->wqes[node]);
1034 return ERR_PTR(ret);
1037 static bool io_task_work_match(struct callback_head *cb, void *data)
1039 struct io_worker *worker;
1041 if (cb->func != create_worker_cb)
1043 worker = container_of(cb, struct io_worker, create_work);
1044 return worker->wqe->wq == data;
1047 void io_wq_exit_start(struct io_wq *wq)
1049 set_bit(IO_WQ_BIT_EXIT, &wq->state);
1052 static void io_wq_exit_workers(struct io_wq *wq)
1054 struct callback_head *cb;
1060 while ((cb = task_work_cancel_match(wq->task, io_task_work_match, wq)) != NULL) {
1061 struct io_worker *worker;
1063 worker = container_of(cb, struct io_worker, create_work);
1064 atomic_dec(&worker->wqe->acct[worker->create_index].nr_running);
1065 io_worker_ref_put(wq);
1066 clear_bit_unlock(0, &worker->create_state);
1067 io_worker_release(worker);
1071 for_each_node(node) {
1072 struct io_wqe *wqe = wq->wqes[node];
1074 io_wq_for_each_worker(wqe, io_wq_worker_wake, NULL);
1077 io_worker_ref_put(wq);
1078 wait_for_completion(&wq->worker_done);
1080 for_each_node(node) {
1081 spin_lock_irq(&wq->hash->wait.lock);
1082 list_del_init(&wq->wqes[node]->wait.entry);
1083 spin_unlock_irq(&wq->hash->wait.lock);
1085 put_task_struct(wq->task);
1089 static void io_wq_destroy(struct io_wq *wq)
1093 cpuhp_state_remove_instance_nocalls(io_wq_online, &wq->cpuhp_node);
1095 for_each_node(node) {
1096 struct io_wqe *wqe = wq->wqes[node];
1097 struct io_cb_cancel_data match = {
1098 .fn = io_wq_work_match_all,
1101 io_wqe_cancel_pending_work(wqe, &match);
1102 free_cpumask_var(wqe->cpu_mask);
1105 io_wq_put_hash(wq->hash);
1109 void io_wq_put_and_exit(struct io_wq *wq)
1111 WARN_ON_ONCE(!test_bit(IO_WQ_BIT_EXIT, &wq->state));
1113 io_wq_exit_workers(wq);
1117 struct online_data {
1122 static bool io_wq_worker_affinity(struct io_worker *worker, void *data)
1124 struct online_data *od = data;
1127 cpumask_set_cpu(od->cpu, worker->wqe->cpu_mask);
1129 cpumask_clear_cpu(od->cpu, worker->wqe->cpu_mask);
1133 static int __io_wq_cpu_online(struct io_wq *wq, unsigned int cpu, bool online)
1135 struct online_data od = {
1143 io_wq_for_each_worker(wq->wqes[i], io_wq_worker_affinity, &od);
1148 static int io_wq_cpu_online(unsigned int cpu, struct hlist_node *node)
1150 struct io_wq *wq = hlist_entry_safe(node, struct io_wq, cpuhp_node);
1152 return __io_wq_cpu_online(wq, cpu, true);
1155 static int io_wq_cpu_offline(unsigned int cpu, struct hlist_node *node)
1157 struct io_wq *wq = hlist_entry_safe(node, struct io_wq, cpuhp_node);
1159 return __io_wq_cpu_online(wq, cpu, false);
1162 int io_wq_cpu_affinity(struct io_wq *wq, cpumask_var_t mask)
1168 struct io_wqe *wqe = wq->wqes[i];
1171 cpumask_copy(wqe->cpu_mask, mask);
1173 cpumask_copy(wqe->cpu_mask, cpumask_of_node(i));
1180 * Set max number of unbounded workers, returns old value. If new_count is 0,
1181 * then just return the old value.
1183 int io_wq_max_workers(struct io_wq *wq, int *new_count)
1185 int i, node, prev = 0;
1187 for (i = 0; i < 2; i++) {
1188 if (new_count[i] > task_rlimit(current, RLIMIT_NPROC))
1189 new_count[i] = task_rlimit(current, RLIMIT_NPROC);
1193 for_each_node(node) {
1194 struct io_wqe_acct *acct;
1196 for (i = 0; i < 2; i++) {
1197 acct = &wq->wqes[node]->acct[i];
1198 prev = max_t(int, acct->max_workers, prev);
1200 acct->max_workers = new_count[i];
1201 new_count[i] = prev;
1208 static __init int io_wq_init(void)
1212 ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "io-wq/online",
1213 io_wq_cpu_online, io_wq_cpu_offline);
1219 subsys_initcall(io_wq_init);