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;
57 #if BITS_PER_LONG == 64
58 #define IO_WQ_HASH_ORDER 6
60 #define IO_WQ_HASH_ORDER 5
63 #define IO_WQ_NR_HASH_BUCKETS (1u << IO_WQ_HASH_ORDER)
78 * Per-node worker thread pool
83 struct io_wq_work_list work_list;
85 } ____cacheline_aligned_in_smp;
88 struct io_wqe_acct acct[2];
90 struct hlist_nulls_head free_list;
91 struct list_head all_list;
93 struct wait_queue_entry wait;
96 struct io_wq_work *hash_tail[IO_WQ_NR_HASH_BUCKETS];
98 cpumask_var_t cpu_mask;
107 free_work_fn *free_work;
108 io_wq_work_fn *do_work;
110 struct io_wq_hash *hash;
112 atomic_t worker_refs;
113 struct completion worker_done;
115 struct hlist_node cpuhp_node;
117 struct task_struct *task;
119 struct io_wqe *wqes[];
122 static enum cpuhp_state io_wq_online;
124 struct io_cb_cancel_data {
132 static void create_io_worker(struct io_wq *wq, struct io_wqe *wqe, int index);
133 static void io_wqe_dec_running(struct io_worker *worker);
135 static bool io_worker_get(struct io_worker *worker)
137 return refcount_inc_not_zero(&worker->ref);
140 static void io_worker_release(struct io_worker *worker)
142 if (refcount_dec_and_test(&worker->ref))
143 complete(&worker->ref_done);
146 static inline struct io_wqe_acct *io_get_acct(struct io_wqe *wqe, bool bound)
148 return &wqe->acct[bound ? IO_WQ_ACCT_BOUND : IO_WQ_ACCT_UNBOUND];
151 static inline struct io_wqe_acct *io_work_get_acct(struct io_wqe *wqe,
152 struct io_wq_work *work)
154 return io_get_acct(wqe, !(work->flags & IO_WQ_WORK_UNBOUND));
157 static inline struct io_wqe_acct *io_wqe_get_acct(struct io_worker *worker)
159 return io_get_acct(worker->wqe, worker->flags & IO_WORKER_F_BOUND);
162 static void io_worker_ref_put(struct io_wq *wq)
164 if (atomic_dec_and_test(&wq->worker_refs))
165 complete(&wq->worker_done);
168 static void io_worker_exit(struct io_worker *worker)
170 struct io_wqe *wqe = worker->wqe;
171 struct io_wqe_acct *acct = io_wqe_get_acct(worker);
173 if (refcount_dec_and_test(&worker->ref))
174 complete(&worker->ref_done);
175 wait_for_completion(&worker->ref_done);
177 raw_spin_lock_irq(&wqe->lock);
178 if (worker->flags & IO_WORKER_F_FREE)
179 hlist_nulls_del_rcu(&worker->nulls_node);
180 list_del_rcu(&worker->all_list);
183 io_wqe_dec_running(worker);
185 current->flags &= ~PF_IO_WORKER;
187 raw_spin_unlock_irq(&wqe->lock);
189 kfree_rcu(worker, rcu);
190 io_worker_ref_put(wqe->wq);
194 static inline bool io_wqe_run_queue(struct io_wqe *wqe)
195 __must_hold(wqe->lock)
197 if (!wq_list_empty(&wqe->work_list) &&
198 !(wqe->flags & IO_WQE_FLAG_STALLED))
204 * Check head of free list for an available worker. If one isn't available,
205 * caller must create one.
207 static bool io_wqe_activate_free_worker(struct io_wqe *wqe)
210 struct hlist_nulls_node *n;
211 struct io_worker *worker;
214 * Iterate free_list and see if we can find an idle worker to
215 * activate. If a given worker is on the free_list but in the process
216 * of exiting, keep trying.
218 hlist_nulls_for_each_entry_rcu(worker, n, &wqe->free_list, nulls_node) {
219 if (!io_worker_get(worker))
221 if (wake_up_process(worker->task)) {
222 io_worker_release(worker);
225 io_worker_release(worker);
232 * We need a worker. If we find a free one, we're good. If not, and we're
233 * below the max number of workers, create one.
235 static void io_wqe_wake_worker(struct io_wqe *wqe, struct io_wqe_acct *acct)
240 * Most likely an attempt to queue unbounded work on an io_wq that
241 * wasn't setup with any unbounded workers.
243 if (unlikely(!acct->max_workers))
244 pr_warn_once("io-wq is not configured for unbound workers");
247 ret = io_wqe_activate_free_worker(wqe);
251 bool do_create = false;
253 raw_spin_lock_irq(&wqe->lock);
254 if (acct->nr_workers < acct->max_workers) {
255 atomic_inc(&acct->nr_running);
256 atomic_inc(&wqe->wq->worker_refs);
260 raw_spin_unlock_irq(&wqe->lock);
262 create_io_worker(wqe->wq, wqe, acct->index);
266 static void io_wqe_inc_running(struct io_worker *worker)
268 struct io_wqe_acct *acct = io_wqe_get_acct(worker);
270 atomic_inc(&acct->nr_running);
273 struct create_worker_data {
274 struct callback_head work;
279 static void create_worker_cb(struct callback_head *cb)
281 struct create_worker_data *cwd;
284 struct io_wqe_acct *acct;
286 cwd = container_of(cb, struct create_worker_data, work);
289 acct = &wqe->acct[cwd->index];
290 raw_spin_lock_irq(&wqe->lock);
291 if (acct->nr_workers < acct->max_workers)
293 raw_spin_unlock_irq(&wqe->lock);
294 create_io_worker(wq, cwd->wqe, cwd->index);
298 static void io_queue_worker_create(struct io_wqe *wqe, struct io_wqe_acct *acct)
300 struct create_worker_data *cwd;
301 struct io_wq *wq = wqe->wq;
303 /* raced with exit, just ignore create call */
304 if (test_bit(IO_WQ_BIT_EXIT, &wq->state))
307 cwd = kmalloc(sizeof(*cwd), GFP_ATOMIC);
309 init_task_work(&cwd->work, create_worker_cb);
311 cwd->index = acct->index;
312 if (!task_work_add(wq->task, &cwd->work, TWA_SIGNAL))
318 atomic_dec(&acct->nr_running);
319 io_worker_ref_put(wq);
322 static void io_wqe_dec_running(struct io_worker *worker)
323 __must_hold(wqe->lock)
325 struct io_wqe_acct *acct = io_wqe_get_acct(worker);
326 struct io_wqe *wqe = worker->wqe;
328 if (!(worker->flags & IO_WORKER_F_UP))
331 if (atomic_dec_and_test(&acct->nr_running) && io_wqe_run_queue(wqe)) {
332 atomic_inc(&acct->nr_running);
333 atomic_inc(&wqe->wq->worker_refs);
334 io_queue_worker_create(wqe, acct);
339 * Worker will start processing some work. Move it to the busy list, if
340 * it's currently on the freelist
342 static void __io_worker_busy(struct io_wqe *wqe, struct io_worker *worker,
343 struct io_wq_work *work)
344 __must_hold(wqe->lock)
346 bool worker_bound, work_bound;
348 BUILD_BUG_ON((IO_WQ_ACCT_UNBOUND ^ IO_WQ_ACCT_BOUND) != 1);
350 if (worker->flags & IO_WORKER_F_FREE) {
351 worker->flags &= ~IO_WORKER_F_FREE;
352 hlist_nulls_del_init_rcu(&worker->nulls_node);
356 * If worker is moving from bound to unbound (or vice versa), then
357 * ensure we update the running accounting.
359 worker_bound = (worker->flags & IO_WORKER_F_BOUND) != 0;
360 work_bound = (work->flags & IO_WQ_WORK_UNBOUND) == 0;
361 if (worker_bound != work_bound) {
362 int index = work_bound ? IO_WQ_ACCT_UNBOUND : IO_WQ_ACCT_BOUND;
363 io_wqe_dec_running(worker);
364 worker->flags ^= IO_WORKER_F_BOUND;
365 wqe->acct[index].nr_workers--;
366 wqe->acct[index ^ 1].nr_workers++;
367 io_wqe_inc_running(worker);
372 * No work, worker going to sleep. Move to freelist, and unuse mm if we
373 * have one attached. Dropping the mm may potentially sleep, so we drop
374 * the lock in that case and return success. Since the caller has to
375 * retry the loop in that case (we changed task state), we don't regrab
376 * the lock if we return success.
378 static void __io_worker_idle(struct io_wqe *wqe, struct io_worker *worker)
379 __must_hold(wqe->lock)
381 if (!(worker->flags & IO_WORKER_F_FREE)) {
382 worker->flags |= IO_WORKER_F_FREE;
383 hlist_nulls_add_head_rcu(&worker->nulls_node, &wqe->free_list);
387 static inline unsigned int io_get_work_hash(struct io_wq_work *work)
389 return work->flags >> IO_WQ_HASH_SHIFT;
392 static void io_wait_on_hash(struct io_wqe *wqe, unsigned int hash)
394 struct io_wq *wq = wqe->wq;
396 spin_lock(&wq->hash->wait.lock);
397 if (list_empty(&wqe->wait.entry)) {
398 __add_wait_queue(&wq->hash->wait, &wqe->wait);
399 if (!test_bit(hash, &wq->hash->map)) {
400 __set_current_state(TASK_RUNNING);
401 list_del_init(&wqe->wait.entry);
404 spin_unlock(&wq->hash->wait.lock);
407 static struct io_wq_work *io_get_next_work(struct io_wqe *wqe)
408 __must_hold(wqe->lock)
410 struct io_wq_work_node *node, *prev;
411 struct io_wq_work *work, *tail;
412 unsigned int stall_hash = -1U;
414 wq_list_for_each(node, prev, &wqe->work_list) {
417 work = container_of(node, struct io_wq_work, list);
419 /* not hashed, can run anytime */
420 if (!io_wq_is_hashed(work)) {
421 wq_list_del(&wqe->work_list, node, prev);
425 hash = io_get_work_hash(work);
426 /* all items with this hash lie in [work, tail] */
427 tail = wqe->hash_tail[hash];
429 /* hashed, can run if not already running */
430 if (!test_and_set_bit(hash, &wqe->wq->hash->map)) {
431 wqe->hash_tail[hash] = NULL;
432 wq_list_cut(&wqe->work_list, &tail->list, prev);
435 if (stall_hash == -1U)
437 /* fast forward to a next hash, for-each will fix up @prev */
441 if (stall_hash != -1U) {
442 raw_spin_unlock(&wqe->lock);
443 io_wait_on_hash(wqe, stall_hash);
444 raw_spin_lock(&wqe->lock);
450 static bool io_flush_signals(void)
452 if (unlikely(test_thread_flag(TIF_NOTIFY_SIGNAL))) {
453 __set_current_state(TASK_RUNNING);
454 tracehook_notify_signal();
460 static void io_assign_current_work(struct io_worker *worker,
461 struct io_wq_work *work)
468 spin_lock_irq(&worker->lock);
469 worker->cur_work = work;
470 spin_unlock_irq(&worker->lock);
473 static void io_wqe_enqueue(struct io_wqe *wqe, struct io_wq_work *work);
475 static void io_worker_handle_work(struct io_worker *worker)
476 __releases(wqe->lock)
478 struct io_wqe *wqe = worker->wqe;
479 struct io_wq *wq = wqe->wq;
480 bool do_kill = test_bit(IO_WQ_BIT_EXIT, &wq->state);
483 struct io_wq_work *work;
486 * If we got some work, mark us as busy. If we didn't, but
487 * the list isn't empty, it means we stalled on hashed work.
488 * Mark us stalled so we don't keep looking for work when we
489 * can't make progress, any work completion or insertion will
490 * clear the stalled flag.
492 work = io_get_next_work(wqe);
494 __io_worker_busy(wqe, worker, work);
495 else if (!wq_list_empty(&wqe->work_list))
496 wqe->flags |= IO_WQE_FLAG_STALLED;
498 raw_spin_unlock_irq(&wqe->lock);
501 io_assign_current_work(worker, work);
502 __set_current_state(TASK_RUNNING);
504 /* handle a whole dependent link */
506 struct io_wq_work *next_hashed, *linked;
507 unsigned int hash = io_get_work_hash(work);
509 next_hashed = wq_next_work(work);
511 if (unlikely(do_kill) && (work->flags & IO_WQ_WORK_UNBOUND))
512 work->flags |= IO_WQ_WORK_CANCEL;
514 io_assign_current_work(worker, NULL);
516 linked = wq->free_work(work);
518 if (!work && linked && !io_wq_is_hashed(linked)) {
522 io_assign_current_work(worker, work);
524 io_wqe_enqueue(wqe, linked);
526 if (hash != -1U && !next_hashed) {
527 clear_bit(hash, &wq->hash->map);
528 if (wq_has_sleeper(&wq->hash->wait))
529 wake_up(&wq->hash->wait);
530 raw_spin_lock_irq(&wqe->lock);
531 wqe->flags &= ~IO_WQE_FLAG_STALLED;
532 /* skip unnecessary unlock-lock wqe->lock */
535 raw_spin_unlock_irq(&wqe->lock);
539 raw_spin_lock_irq(&wqe->lock);
543 static int io_wqe_worker(void *data)
545 struct io_worker *worker = data;
546 struct io_wqe *wqe = worker->wqe;
547 struct io_wq *wq = wqe->wq;
548 char buf[TASK_COMM_LEN];
550 worker->flags |= (IO_WORKER_F_UP | IO_WORKER_F_RUNNING);
552 snprintf(buf, sizeof(buf), "iou-wrk-%d", wq->task->pid);
553 set_task_comm(current, buf);
555 while (!test_bit(IO_WQ_BIT_EXIT, &wq->state)) {
558 set_current_state(TASK_INTERRUPTIBLE);
560 raw_spin_lock_irq(&wqe->lock);
561 if (io_wqe_run_queue(wqe)) {
562 io_worker_handle_work(worker);
565 __io_worker_idle(wqe, worker);
566 raw_spin_unlock_irq(&wqe->lock);
567 if (io_flush_signals())
569 ret = schedule_timeout(WORKER_IDLE_TIMEOUT);
570 if (signal_pending(current)) {
573 if (!get_signal(&ksig))
579 /* timed out, exit unless we're the fixed worker */
580 if (!(worker->flags & IO_WORKER_F_FIXED))
584 if (test_bit(IO_WQ_BIT_EXIT, &wq->state)) {
585 raw_spin_lock_irq(&wqe->lock);
586 io_worker_handle_work(worker);
589 io_worker_exit(worker);
594 * Called when a worker is scheduled in. Mark us as currently running.
596 void io_wq_worker_running(struct task_struct *tsk)
598 struct io_worker *worker = tsk->pf_io_worker;
602 if (!(worker->flags & IO_WORKER_F_UP))
604 if (worker->flags & IO_WORKER_F_RUNNING)
606 worker->flags |= IO_WORKER_F_RUNNING;
607 io_wqe_inc_running(worker);
611 * Called when worker is going to sleep. If there are no workers currently
612 * running and we have work pending, wake up a free one or create a new one.
614 void io_wq_worker_sleeping(struct task_struct *tsk)
616 struct io_worker *worker = tsk->pf_io_worker;
620 if (!(worker->flags & IO_WORKER_F_UP))
622 if (!(worker->flags & IO_WORKER_F_RUNNING))
625 worker->flags &= ~IO_WORKER_F_RUNNING;
627 raw_spin_lock_irq(&worker->wqe->lock);
628 io_wqe_dec_running(worker);
629 raw_spin_unlock_irq(&worker->wqe->lock);
632 static void create_io_worker(struct io_wq *wq, struct io_wqe *wqe, int index)
634 struct io_wqe_acct *acct = &wqe->acct[index];
635 struct io_worker *worker;
636 struct task_struct *tsk;
638 __set_current_state(TASK_RUNNING);
640 worker = kzalloc_node(sizeof(*worker), GFP_KERNEL, wqe->node);
644 refcount_set(&worker->ref, 1);
645 worker->nulls_node.pprev = NULL;
647 spin_lock_init(&worker->lock);
648 init_completion(&worker->ref_done);
650 tsk = create_io_thread(io_wqe_worker, worker, wqe->node);
654 atomic_dec(&acct->nr_running);
655 raw_spin_lock_irq(&wqe->lock);
657 raw_spin_unlock_irq(&wqe->lock);
658 io_worker_ref_put(wq);
662 tsk->pf_io_worker = worker;
664 set_cpus_allowed_ptr(tsk, wqe->cpu_mask);
665 tsk->flags |= PF_NO_SETAFFINITY;
667 raw_spin_lock_irq(&wqe->lock);
668 hlist_nulls_add_head_rcu(&worker->nulls_node, &wqe->free_list);
669 list_add_tail_rcu(&worker->all_list, &wqe->all_list);
670 worker->flags |= IO_WORKER_F_FREE;
671 if (index == IO_WQ_ACCT_BOUND)
672 worker->flags |= IO_WORKER_F_BOUND;
673 if ((acct->nr_workers == 1) && (worker->flags & IO_WORKER_F_BOUND))
674 worker->flags |= IO_WORKER_F_FIXED;
675 raw_spin_unlock_irq(&wqe->lock);
676 wake_up_new_task(tsk);
680 * Iterate the passed in list and call the specific function for each
681 * worker that isn't exiting
683 static bool io_wq_for_each_worker(struct io_wqe *wqe,
684 bool (*func)(struct io_worker *, void *),
687 struct io_worker *worker;
690 list_for_each_entry_rcu(worker, &wqe->all_list, all_list) {
691 if (io_worker_get(worker)) {
692 /* no task if node is/was offline */
694 ret = func(worker, data);
695 io_worker_release(worker);
704 static bool io_wq_worker_wake(struct io_worker *worker, void *data)
706 set_notify_signal(worker->task);
707 wake_up_process(worker->task);
711 static bool io_wq_work_match_all(struct io_wq_work *work, void *data)
716 static void io_run_cancel(struct io_wq_work *work, struct io_wqe *wqe)
718 struct io_wq *wq = wqe->wq;
721 work->flags |= IO_WQ_WORK_CANCEL;
723 work = wq->free_work(work);
727 static void io_wqe_insert_work(struct io_wqe *wqe, struct io_wq_work *work)
730 struct io_wq_work *tail;
732 if (!io_wq_is_hashed(work)) {
734 wq_list_add_tail(&work->list, &wqe->work_list);
738 hash = io_get_work_hash(work);
739 tail = wqe->hash_tail[hash];
740 wqe->hash_tail[hash] = work;
744 wq_list_add_after(&work->list, &tail->list, &wqe->work_list);
747 static void io_wqe_enqueue(struct io_wqe *wqe, struct io_wq_work *work)
749 struct io_wqe_acct *acct = io_work_get_acct(wqe, work);
754 * If io-wq is exiting for this task, or if the request has explicitly
755 * been marked as one that should not get executed, cancel it here.
757 if (test_bit(IO_WQ_BIT_EXIT, &wqe->wq->state) ||
758 (work->flags & IO_WQ_WORK_CANCEL)) {
759 io_run_cancel(work, wqe);
763 work_flags = work->flags;
764 raw_spin_lock_irqsave(&wqe->lock, flags);
765 io_wqe_insert_work(wqe, work);
766 wqe->flags &= ~IO_WQE_FLAG_STALLED;
767 raw_spin_unlock_irqrestore(&wqe->lock, flags);
769 if ((work_flags & IO_WQ_WORK_CONCURRENT) ||
770 !atomic_read(&acct->nr_running))
771 io_wqe_wake_worker(wqe, acct);
774 void io_wq_enqueue(struct io_wq *wq, struct io_wq_work *work)
776 struct io_wqe *wqe = wq->wqes[numa_node_id()];
778 io_wqe_enqueue(wqe, work);
782 * Work items that hash to the same value will not be done in parallel.
783 * Used to limit concurrent writes, generally hashed by inode.
785 void io_wq_hash_work(struct io_wq_work *work, void *val)
789 bit = hash_ptr(val, IO_WQ_HASH_ORDER);
790 work->flags |= (IO_WQ_WORK_HASHED | (bit << IO_WQ_HASH_SHIFT));
793 static bool io_wq_worker_cancel(struct io_worker *worker, void *data)
795 struct io_cb_cancel_data *match = data;
799 * Hold the lock to avoid ->cur_work going out of scope, caller
800 * may dereference the passed in work.
802 spin_lock_irqsave(&worker->lock, flags);
803 if (worker->cur_work &&
804 match->fn(worker->cur_work, match->data)) {
805 set_notify_signal(worker->task);
808 spin_unlock_irqrestore(&worker->lock, flags);
810 return match->nr_running && !match->cancel_all;
813 static inline void io_wqe_remove_pending(struct io_wqe *wqe,
814 struct io_wq_work *work,
815 struct io_wq_work_node *prev)
817 unsigned int hash = io_get_work_hash(work);
818 struct io_wq_work *prev_work = NULL;
820 if (io_wq_is_hashed(work) && work == wqe->hash_tail[hash]) {
822 prev_work = container_of(prev, struct io_wq_work, list);
823 if (prev_work && io_get_work_hash(prev_work) == hash)
824 wqe->hash_tail[hash] = prev_work;
826 wqe->hash_tail[hash] = NULL;
828 wq_list_del(&wqe->work_list, &work->list, prev);
831 static void io_wqe_cancel_pending_work(struct io_wqe *wqe,
832 struct io_cb_cancel_data *match)
834 struct io_wq_work_node *node, *prev;
835 struct io_wq_work *work;
839 raw_spin_lock_irqsave(&wqe->lock, flags);
840 wq_list_for_each(node, prev, &wqe->work_list) {
841 work = container_of(node, struct io_wq_work, list);
842 if (!match->fn(work, match->data))
844 io_wqe_remove_pending(wqe, work, prev);
845 raw_spin_unlock_irqrestore(&wqe->lock, flags);
846 io_run_cancel(work, wqe);
848 if (!match->cancel_all)
851 /* not safe to continue after unlock */
854 raw_spin_unlock_irqrestore(&wqe->lock, flags);
857 static void io_wqe_cancel_running_work(struct io_wqe *wqe,
858 struct io_cb_cancel_data *match)
861 io_wq_for_each_worker(wqe, io_wq_worker_cancel, match);
865 enum io_wq_cancel io_wq_cancel_cb(struct io_wq *wq, work_cancel_fn *cancel,
866 void *data, bool cancel_all)
868 struct io_cb_cancel_data match = {
871 .cancel_all = cancel_all,
876 * First check pending list, if we're lucky we can just remove it
877 * from there. CANCEL_OK means that the work is returned as-new,
878 * no completion will be posted for it.
880 for_each_node(node) {
881 struct io_wqe *wqe = wq->wqes[node];
883 io_wqe_cancel_pending_work(wqe, &match);
884 if (match.nr_pending && !match.cancel_all)
885 return IO_WQ_CANCEL_OK;
889 * Now check if a free (going busy) or busy worker has the work
890 * currently running. If we find it there, we'll return CANCEL_RUNNING
891 * as an indication that we attempt to signal cancellation. The
892 * completion will run normally in this case.
894 for_each_node(node) {
895 struct io_wqe *wqe = wq->wqes[node];
897 io_wqe_cancel_running_work(wqe, &match);
898 if (match.nr_running && !match.cancel_all)
899 return IO_WQ_CANCEL_RUNNING;
902 if (match.nr_running)
903 return IO_WQ_CANCEL_RUNNING;
904 if (match.nr_pending)
905 return IO_WQ_CANCEL_OK;
906 return IO_WQ_CANCEL_NOTFOUND;
909 static int io_wqe_hash_wake(struct wait_queue_entry *wait, unsigned mode,
912 struct io_wqe *wqe = container_of(wait, struct io_wqe, wait);
914 list_del_init(&wait->entry);
917 io_wqe_activate_free_worker(wqe);
922 struct io_wq *io_wq_create(unsigned bounded, struct io_wq_data *data)
927 if (WARN_ON_ONCE(!data->free_work || !data->do_work))
928 return ERR_PTR(-EINVAL);
929 if (WARN_ON_ONCE(!bounded))
930 return ERR_PTR(-EINVAL);
932 wq = kzalloc(struct_size(wq, wqes, nr_node_ids), GFP_KERNEL);
934 return ERR_PTR(-ENOMEM);
935 ret = cpuhp_state_add_instance_nocalls(io_wq_online, &wq->cpuhp_node);
939 refcount_inc(&data->hash->refs);
940 wq->hash = data->hash;
941 wq->free_work = data->free_work;
942 wq->do_work = data->do_work;
945 for_each_node(node) {
947 int alloc_node = node;
949 if (!node_online(alloc_node))
950 alloc_node = NUMA_NO_NODE;
951 wqe = kzalloc_node(sizeof(struct io_wqe), GFP_KERNEL, alloc_node);
954 if (!alloc_cpumask_var(&wqe->cpu_mask, GFP_KERNEL))
956 cpumask_copy(wqe->cpu_mask, cpumask_of_node(node));
957 wq->wqes[node] = wqe;
958 wqe->node = alloc_node;
959 wqe->acct[IO_WQ_ACCT_BOUND].index = IO_WQ_ACCT_BOUND;
960 wqe->acct[IO_WQ_ACCT_UNBOUND].index = IO_WQ_ACCT_UNBOUND;
961 wqe->acct[IO_WQ_ACCT_BOUND].max_workers = bounded;
962 atomic_set(&wqe->acct[IO_WQ_ACCT_BOUND].nr_running, 0);
963 wqe->acct[IO_WQ_ACCT_UNBOUND].max_workers =
964 task_rlimit(current, RLIMIT_NPROC);
965 atomic_set(&wqe->acct[IO_WQ_ACCT_UNBOUND].nr_running, 0);
966 wqe->wait.func = io_wqe_hash_wake;
967 INIT_LIST_HEAD(&wqe->wait.entry);
969 raw_spin_lock_init(&wqe->lock);
970 INIT_WQ_LIST(&wqe->work_list);
971 INIT_HLIST_NULLS_HEAD(&wqe->free_list, 0);
972 INIT_LIST_HEAD(&wqe->all_list);
975 wq->task = get_task_struct(data->task);
976 atomic_set(&wq->worker_refs, 1);
977 init_completion(&wq->worker_done);
980 io_wq_put_hash(data->hash);
981 cpuhp_state_remove_instance_nocalls(io_wq_online, &wq->cpuhp_node);
982 for_each_node(node) {
985 free_cpumask_var(wq->wqes[node]->cpu_mask);
986 kfree(wq->wqes[node]);
993 static bool io_task_work_match(struct callback_head *cb, void *data)
995 struct create_worker_data *cwd;
997 if (cb->func != create_worker_cb)
999 cwd = container_of(cb, struct create_worker_data, work);
1000 return cwd->wqe->wq == data;
1003 void io_wq_exit_start(struct io_wq *wq)
1005 set_bit(IO_WQ_BIT_EXIT, &wq->state);
1008 static void io_wq_exit_workers(struct io_wq *wq)
1010 struct callback_head *cb;
1016 while ((cb = task_work_cancel_match(wq->task, io_task_work_match, wq)) != NULL) {
1017 struct create_worker_data *cwd;
1019 cwd = container_of(cb, struct create_worker_data, work);
1020 atomic_dec(&cwd->wqe->acct[cwd->index].nr_running);
1021 io_worker_ref_put(wq);
1026 for_each_node(node) {
1027 struct io_wqe *wqe = wq->wqes[node];
1029 io_wq_for_each_worker(wqe, io_wq_worker_wake, NULL);
1032 io_worker_ref_put(wq);
1033 wait_for_completion(&wq->worker_done);
1035 for_each_node(node) {
1036 spin_lock_irq(&wq->hash->wait.lock);
1037 list_del_init(&wq->wqes[node]->wait.entry);
1038 spin_unlock_irq(&wq->hash->wait.lock);
1040 put_task_struct(wq->task);
1044 static void io_wq_destroy(struct io_wq *wq)
1048 cpuhp_state_remove_instance_nocalls(io_wq_online, &wq->cpuhp_node);
1050 for_each_node(node) {
1051 struct io_wqe *wqe = wq->wqes[node];
1052 struct io_cb_cancel_data match = {
1053 .fn = io_wq_work_match_all,
1056 io_wqe_cancel_pending_work(wqe, &match);
1057 free_cpumask_var(wqe->cpu_mask);
1060 io_wq_put_hash(wq->hash);
1064 void io_wq_put_and_exit(struct io_wq *wq)
1066 WARN_ON_ONCE(!test_bit(IO_WQ_BIT_EXIT, &wq->state));
1068 io_wq_exit_workers(wq);
1072 struct online_data {
1077 static bool io_wq_worker_affinity(struct io_worker *worker, void *data)
1079 struct online_data *od = data;
1082 cpumask_set_cpu(od->cpu, worker->wqe->cpu_mask);
1084 cpumask_clear_cpu(od->cpu, worker->wqe->cpu_mask);
1088 static int __io_wq_cpu_online(struct io_wq *wq, unsigned int cpu, bool online)
1090 struct online_data od = {
1098 io_wq_for_each_worker(wq->wqes[i], io_wq_worker_affinity, &od);
1103 static int io_wq_cpu_online(unsigned int cpu, struct hlist_node *node)
1105 struct io_wq *wq = hlist_entry_safe(node, struct io_wq, cpuhp_node);
1107 return __io_wq_cpu_online(wq, cpu, true);
1110 static int io_wq_cpu_offline(unsigned int cpu, struct hlist_node *node)
1112 struct io_wq *wq = hlist_entry_safe(node, struct io_wq, cpuhp_node);
1114 return __io_wq_cpu_online(wq, cpu, false);
1117 int io_wq_cpu_affinity(struct io_wq *wq, cpumask_var_t mask)
1123 struct io_wqe *wqe = wq->wqes[i];
1126 cpumask_copy(wqe->cpu_mask, mask);
1128 cpumask_copy(wqe->cpu_mask, cpumask_of_node(i));
1134 static __init int io_wq_init(void)
1138 ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "io-wq/online",
1139 io_wq_cpu_online, io_wq_cpu_offline);
1145 subsys_initcall(io_wq_init);