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_irq(&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_irq(&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_irq(&wqe->lock);
258 if (acct->nr_workers < acct->max_workers) {
259 atomic_inc(&acct->nr_running);
260 atomic_inc(&wqe->wq->worker_refs);
261 if (!acct->nr_workers)
266 raw_spin_unlock_irq(&wqe->lock);
268 create_io_worker(wqe->wq, wqe, acct->index, first);
272 static void io_wqe_inc_running(struct io_worker *worker)
274 struct io_wqe_acct *acct = io_wqe_get_acct(worker);
276 atomic_inc(&acct->nr_running);
279 static void create_worker_cb(struct callback_head *cb)
281 struct io_worker *worker;
284 struct io_wqe_acct *acct;
285 bool do_create = false, first = false;
287 worker = container_of(cb, struct io_worker, create_work);
290 acct = &wqe->acct[worker->create_index];
291 raw_spin_lock_irq(&wqe->lock);
292 if (acct->nr_workers < acct->max_workers) {
293 if (!acct->nr_workers)
298 raw_spin_unlock_irq(&wqe->lock);
300 create_io_worker(wq, wqe, worker->create_index, first);
302 atomic_dec(&acct->nr_running);
303 io_worker_ref_put(wq);
305 clear_bit_unlock(0, &worker->create_state);
306 io_worker_release(worker);
309 static void io_queue_worker_create(struct io_wqe *wqe, struct io_worker *worker,
310 struct io_wqe_acct *acct)
312 struct io_wq *wq = wqe->wq;
314 /* raced with exit, just ignore create call */
315 if (test_bit(IO_WQ_BIT_EXIT, &wq->state))
317 if (!io_worker_get(worker))
320 * create_state manages ownership of create_work/index. We should
321 * only need one entry per worker, as the worker going to sleep
322 * will trigger the condition, and waking will clear it once it
323 * runs the task_work.
325 if (test_bit(0, &worker->create_state) ||
326 test_and_set_bit_lock(0, &worker->create_state))
329 init_task_work(&worker->create_work, create_worker_cb);
330 worker->create_index = acct->index;
331 if (!task_work_add(wq->task, &worker->create_work, TWA_SIGNAL))
333 clear_bit_unlock(0, &worker->create_state);
335 io_worker_release(worker);
337 atomic_dec(&acct->nr_running);
338 io_worker_ref_put(wq);
341 static void io_wqe_dec_running(struct io_worker *worker)
342 __must_hold(wqe->lock)
344 struct io_wqe_acct *acct = io_wqe_get_acct(worker);
345 struct io_wqe *wqe = worker->wqe;
347 if (!(worker->flags & IO_WORKER_F_UP))
350 if (atomic_dec_and_test(&acct->nr_running) && io_wqe_run_queue(wqe)) {
351 atomic_inc(&acct->nr_running);
352 atomic_inc(&wqe->wq->worker_refs);
353 io_queue_worker_create(wqe, worker, acct);
358 * Worker will start processing some work. Move it to the busy list, if
359 * it's currently on the freelist
361 static void __io_worker_busy(struct io_wqe *wqe, struct io_worker *worker,
362 struct io_wq_work *work)
363 __must_hold(wqe->lock)
365 bool worker_bound, work_bound;
367 BUILD_BUG_ON((IO_WQ_ACCT_UNBOUND ^ IO_WQ_ACCT_BOUND) != 1);
369 if (worker->flags & IO_WORKER_F_FREE) {
370 worker->flags &= ~IO_WORKER_F_FREE;
371 hlist_nulls_del_init_rcu(&worker->nulls_node);
375 * If worker is moving from bound to unbound (or vice versa), then
376 * ensure we update the running accounting.
378 worker_bound = (worker->flags & IO_WORKER_F_BOUND) != 0;
379 work_bound = (work->flags & IO_WQ_WORK_UNBOUND) == 0;
380 if (worker_bound != work_bound) {
381 int index = work_bound ? IO_WQ_ACCT_UNBOUND : IO_WQ_ACCT_BOUND;
382 io_wqe_dec_running(worker);
383 worker->flags ^= IO_WORKER_F_BOUND;
384 wqe->acct[index].nr_workers--;
385 wqe->acct[index ^ 1].nr_workers++;
386 io_wqe_inc_running(worker);
391 * No work, worker going to sleep. Move to freelist, and unuse mm if we
392 * have one attached. Dropping the mm may potentially sleep, so we drop
393 * the lock in that case and return success. Since the caller has to
394 * retry the loop in that case (we changed task state), we don't regrab
395 * the lock if we return success.
397 static void __io_worker_idle(struct io_wqe *wqe, struct io_worker *worker)
398 __must_hold(wqe->lock)
400 if (!(worker->flags & IO_WORKER_F_FREE)) {
401 worker->flags |= IO_WORKER_F_FREE;
402 hlist_nulls_add_head_rcu(&worker->nulls_node, &wqe->free_list);
406 static inline unsigned int io_get_work_hash(struct io_wq_work *work)
408 return work->flags >> IO_WQ_HASH_SHIFT;
411 static void io_wait_on_hash(struct io_wqe *wqe, unsigned int hash)
413 struct io_wq *wq = wqe->wq;
415 spin_lock(&wq->hash->wait.lock);
416 if (list_empty(&wqe->wait.entry)) {
417 __add_wait_queue(&wq->hash->wait, &wqe->wait);
418 if (!test_bit(hash, &wq->hash->map)) {
419 __set_current_state(TASK_RUNNING);
420 list_del_init(&wqe->wait.entry);
423 spin_unlock(&wq->hash->wait.lock);
426 static struct io_wq_work *io_get_next_work(struct io_wqe *wqe)
427 __must_hold(wqe->lock)
429 struct io_wq_work_node *node, *prev;
430 struct io_wq_work *work, *tail;
431 unsigned int stall_hash = -1U;
433 wq_list_for_each(node, prev, &wqe->work_list) {
436 work = container_of(node, struct io_wq_work, list);
438 /* not hashed, can run anytime */
439 if (!io_wq_is_hashed(work)) {
440 wq_list_del(&wqe->work_list, node, prev);
444 hash = io_get_work_hash(work);
445 /* all items with this hash lie in [work, tail] */
446 tail = wqe->hash_tail[hash];
448 /* hashed, can run if not already running */
449 if (!test_and_set_bit(hash, &wqe->wq->hash->map)) {
450 wqe->hash_tail[hash] = NULL;
451 wq_list_cut(&wqe->work_list, &tail->list, prev);
454 if (stall_hash == -1U)
456 /* fast forward to a next hash, for-each will fix up @prev */
460 if (stall_hash != -1U) {
461 raw_spin_unlock(&wqe->lock);
462 io_wait_on_hash(wqe, stall_hash);
463 raw_spin_lock(&wqe->lock);
469 static bool io_flush_signals(void)
471 if (unlikely(test_thread_flag(TIF_NOTIFY_SIGNAL))) {
472 __set_current_state(TASK_RUNNING);
473 tracehook_notify_signal();
479 static void io_assign_current_work(struct io_worker *worker,
480 struct io_wq_work *work)
487 spin_lock_irq(&worker->lock);
488 worker->cur_work = work;
489 spin_unlock_irq(&worker->lock);
492 static void io_wqe_enqueue(struct io_wqe *wqe, struct io_wq_work *work);
494 static void io_worker_handle_work(struct io_worker *worker)
495 __releases(wqe->lock)
497 struct io_wqe *wqe = worker->wqe;
498 struct io_wq *wq = wqe->wq;
499 bool do_kill = test_bit(IO_WQ_BIT_EXIT, &wq->state);
502 struct io_wq_work *work;
505 * If we got some work, mark us as busy. If we didn't, but
506 * the list isn't empty, it means we stalled on hashed work.
507 * Mark us stalled so we don't keep looking for work when we
508 * can't make progress, any work completion or insertion will
509 * clear the stalled flag.
511 work = io_get_next_work(wqe);
513 __io_worker_busy(wqe, worker, work);
514 else if (!wq_list_empty(&wqe->work_list))
515 wqe->flags |= IO_WQE_FLAG_STALLED;
517 raw_spin_unlock_irq(&wqe->lock);
520 io_assign_current_work(worker, work);
521 __set_current_state(TASK_RUNNING);
523 /* handle a whole dependent link */
525 struct io_wq_work *next_hashed, *linked;
526 unsigned int hash = io_get_work_hash(work);
528 next_hashed = wq_next_work(work);
530 if (unlikely(do_kill) && (work->flags & IO_WQ_WORK_UNBOUND))
531 work->flags |= IO_WQ_WORK_CANCEL;
533 io_assign_current_work(worker, NULL);
535 linked = wq->free_work(work);
537 if (!work && linked && !io_wq_is_hashed(linked)) {
541 io_assign_current_work(worker, work);
543 io_wqe_enqueue(wqe, linked);
545 if (hash != -1U && !next_hashed) {
546 clear_bit(hash, &wq->hash->map);
547 if (wq_has_sleeper(&wq->hash->wait))
548 wake_up(&wq->hash->wait);
549 raw_spin_lock_irq(&wqe->lock);
550 wqe->flags &= ~IO_WQE_FLAG_STALLED;
551 /* skip unnecessary unlock-lock wqe->lock */
554 raw_spin_unlock_irq(&wqe->lock);
558 raw_spin_lock_irq(&wqe->lock);
562 static int io_wqe_worker(void *data)
564 struct io_worker *worker = data;
565 struct io_wqe *wqe = worker->wqe;
566 struct io_wq *wq = wqe->wq;
567 char buf[TASK_COMM_LEN];
569 worker->flags |= (IO_WORKER_F_UP | IO_WORKER_F_RUNNING);
571 snprintf(buf, sizeof(buf), "iou-wrk-%d", wq->task->pid);
572 set_task_comm(current, buf);
574 while (!test_bit(IO_WQ_BIT_EXIT, &wq->state)) {
577 set_current_state(TASK_INTERRUPTIBLE);
579 raw_spin_lock_irq(&wqe->lock);
580 if (io_wqe_run_queue(wqe)) {
581 io_worker_handle_work(worker);
584 __io_worker_idle(wqe, worker);
585 raw_spin_unlock_irq(&wqe->lock);
586 if (io_flush_signals())
588 ret = schedule_timeout(WORKER_IDLE_TIMEOUT);
589 if (signal_pending(current)) {
592 if (!get_signal(&ksig))
598 /* timed out, exit unless we're the fixed worker */
599 if (!(worker->flags & IO_WORKER_F_FIXED))
603 if (test_bit(IO_WQ_BIT_EXIT, &wq->state)) {
604 raw_spin_lock_irq(&wqe->lock);
605 io_worker_handle_work(worker);
608 io_worker_exit(worker);
613 * Called when a worker is scheduled in. Mark us as currently running.
615 void io_wq_worker_running(struct task_struct *tsk)
617 struct io_worker *worker = tsk->pf_io_worker;
621 if (!(worker->flags & IO_WORKER_F_UP))
623 if (worker->flags & IO_WORKER_F_RUNNING)
625 worker->flags |= IO_WORKER_F_RUNNING;
626 io_wqe_inc_running(worker);
630 * Called when worker is going to sleep. If there are no workers currently
631 * running and we have work pending, wake up a free one or create a new one.
633 void io_wq_worker_sleeping(struct task_struct *tsk)
635 struct io_worker *worker = tsk->pf_io_worker;
639 if (!(worker->flags & IO_WORKER_F_UP))
641 if (!(worker->flags & IO_WORKER_F_RUNNING))
644 worker->flags &= ~IO_WORKER_F_RUNNING;
646 raw_spin_lock_irq(&worker->wqe->lock);
647 io_wqe_dec_running(worker);
648 raw_spin_unlock_irq(&worker->wqe->lock);
651 static void create_io_worker(struct io_wq *wq, struct io_wqe *wqe, int index, bool first)
653 struct io_wqe_acct *acct = &wqe->acct[index];
654 struct io_worker *worker;
655 struct task_struct *tsk;
657 __set_current_state(TASK_RUNNING);
659 worker = kzalloc_node(sizeof(*worker), GFP_KERNEL, wqe->node);
663 refcount_set(&worker->ref, 1);
664 worker->nulls_node.pprev = NULL;
666 spin_lock_init(&worker->lock);
667 init_completion(&worker->ref_done);
669 tsk = create_io_thread(io_wqe_worker, worker, wqe->node);
673 atomic_dec(&acct->nr_running);
674 raw_spin_lock_irq(&wqe->lock);
676 raw_spin_unlock_irq(&wqe->lock);
677 io_worker_ref_put(wq);
681 tsk->pf_io_worker = worker;
683 set_cpus_allowed_ptr(tsk, wqe->cpu_mask);
684 tsk->flags |= PF_NO_SETAFFINITY;
686 raw_spin_lock_irq(&wqe->lock);
687 hlist_nulls_add_head_rcu(&worker->nulls_node, &wqe->free_list);
688 list_add_tail_rcu(&worker->all_list, &wqe->all_list);
689 worker->flags |= IO_WORKER_F_FREE;
690 if (index == IO_WQ_ACCT_BOUND)
691 worker->flags |= IO_WORKER_F_BOUND;
692 if (first && (worker->flags & IO_WORKER_F_BOUND))
693 worker->flags |= IO_WORKER_F_FIXED;
694 raw_spin_unlock_irq(&wqe->lock);
695 wake_up_new_task(tsk);
699 * Iterate the passed in list and call the specific function for each
700 * worker that isn't exiting
702 static bool io_wq_for_each_worker(struct io_wqe *wqe,
703 bool (*func)(struct io_worker *, void *),
706 struct io_worker *worker;
709 list_for_each_entry_rcu(worker, &wqe->all_list, all_list) {
710 if (io_worker_get(worker)) {
711 /* no task if node is/was offline */
713 ret = func(worker, data);
714 io_worker_release(worker);
723 static bool io_wq_worker_wake(struct io_worker *worker, void *data)
725 set_notify_signal(worker->task);
726 wake_up_process(worker->task);
730 static bool io_wq_work_match_all(struct io_wq_work *work, void *data)
735 static void io_run_cancel(struct io_wq_work *work, struct io_wqe *wqe)
737 struct io_wq *wq = wqe->wq;
740 work->flags |= IO_WQ_WORK_CANCEL;
742 work = wq->free_work(work);
746 static void io_wqe_insert_work(struct io_wqe *wqe, struct io_wq_work *work)
749 struct io_wq_work *tail;
751 if (!io_wq_is_hashed(work)) {
753 wq_list_add_tail(&work->list, &wqe->work_list);
757 hash = io_get_work_hash(work);
758 tail = wqe->hash_tail[hash];
759 wqe->hash_tail[hash] = work;
763 wq_list_add_after(&work->list, &tail->list, &wqe->work_list);
766 static void io_wqe_enqueue(struct io_wqe *wqe, struct io_wq_work *work)
768 struct io_wqe_acct *acct = io_work_get_acct(wqe, work);
773 * If io-wq is exiting for this task, or if the request has explicitly
774 * been marked as one that should not get executed, cancel it here.
776 if (test_bit(IO_WQ_BIT_EXIT, &wqe->wq->state) ||
777 (work->flags & IO_WQ_WORK_CANCEL)) {
778 io_run_cancel(work, wqe);
782 work_flags = work->flags;
783 raw_spin_lock_irqsave(&wqe->lock, flags);
784 io_wqe_insert_work(wqe, work);
785 wqe->flags &= ~IO_WQE_FLAG_STALLED;
786 raw_spin_unlock_irqrestore(&wqe->lock, flags);
788 if ((work_flags & IO_WQ_WORK_CONCURRENT) ||
789 !atomic_read(&acct->nr_running))
790 io_wqe_wake_worker(wqe, acct);
793 void io_wq_enqueue(struct io_wq *wq, struct io_wq_work *work)
795 struct io_wqe *wqe = wq->wqes[numa_node_id()];
797 io_wqe_enqueue(wqe, work);
801 * Work items that hash to the same value will not be done in parallel.
802 * Used to limit concurrent writes, generally hashed by inode.
804 void io_wq_hash_work(struct io_wq_work *work, void *val)
808 bit = hash_ptr(val, IO_WQ_HASH_ORDER);
809 work->flags |= (IO_WQ_WORK_HASHED | (bit << IO_WQ_HASH_SHIFT));
812 static bool io_wq_worker_cancel(struct io_worker *worker, void *data)
814 struct io_cb_cancel_data *match = data;
818 * Hold the lock to avoid ->cur_work going out of scope, caller
819 * may dereference the passed in work.
821 spin_lock_irqsave(&worker->lock, flags);
822 if (worker->cur_work &&
823 match->fn(worker->cur_work, match->data)) {
824 set_notify_signal(worker->task);
827 spin_unlock_irqrestore(&worker->lock, flags);
829 return match->nr_running && !match->cancel_all;
832 static inline void io_wqe_remove_pending(struct io_wqe *wqe,
833 struct io_wq_work *work,
834 struct io_wq_work_node *prev)
836 unsigned int hash = io_get_work_hash(work);
837 struct io_wq_work *prev_work = NULL;
839 if (io_wq_is_hashed(work) && work == wqe->hash_tail[hash]) {
841 prev_work = container_of(prev, struct io_wq_work, list);
842 if (prev_work && io_get_work_hash(prev_work) == hash)
843 wqe->hash_tail[hash] = prev_work;
845 wqe->hash_tail[hash] = NULL;
847 wq_list_del(&wqe->work_list, &work->list, prev);
850 static void io_wqe_cancel_pending_work(struct io_wqe *wqe,
851 struct io_cb_cancel_data *match)
853 struct io_wq_work_node *node, *prev;
854 struct io_wq_work *work;
858 raw_spin_lock_irqsave(&wqe->lock, flags);
859 wq_list_for_each(node, prev, &wqe->work_list) {
860 work = container_of(node, struct io_wq_work, list);
861 if (!match->fn(work, match->data))
863 io_wqe_remove_pending(wqe, work, prev);
864 raw_spin_unlock_irqrestore(&wqe->lock, flags);
865 io_run_cancel(work, wqe);
867 if (!match->cancel_all)
870 /* not safe to continue after unlock */
873 raw_spin_unlock_irqrestore(&wqe->lock, flags);
876 static void io_wqe_cancel_running_work(struct io_wqe *wqe,
877 struct io_cb_cancel_data *match)
880 io_wq_for_each_worker(wqe, io_wq_worker_cancel, match);
884 enum io_wq_cancel io_wq_cancel_cb(struct io_wq *wq, work_cancel_fn *cancel,
885 void *data, bool cancel_all)
887 struct io_cb_cancel_data match = {
890 .cancel_all = cancel_all,
895 * First check pending list, if we're lucky we can just remove it
896 * from there. CANCEL_OK means that the work is returned as-new,
897 * no completion will be posted for it.
899 for_each_node(node) {
900 struct io_wqe *wqe = wq->wqes[node];
902 io_wqe_cancel_pending_work(wqe, &match);
903 if (match.nr_pending && !match.cancel_all)
904 return IO_WQ_CANCEL_OK;
908 * Now check if a free (going busy) or busy worker has the work
909 * currently running. If we find it there, we'll return CANCEL_RUNNING
910 * as an indication that we attempt to signal cancellation. The
911 * completion will run normally in this case.
913 for_each_node(node) {
914 struct io_wqe *wqe = wq->wqes[node];
916 io_wqe_cancel_running_work(wqe, &match);
917 if (match.nr_running && !match.cancel_all)
918 return IO_WQ_CANCEL_RUNNING;
921 if (match.nr_running)
922 return IO_WQ_CANCEL_RUNNING;
923 if (match.nr_pending)
924 return IO_WQ_CANCEL_OK;
925 return IO_WQ_CANCEL_NOTFOUND;
928 static int io_wqe_hash_wake(struct wait_queue_entry *wait, unsigned mode,
931 struct io_wqe *wqe = container_of(wait, struct io_wqe, wait);
933 list_del_init(&wait->entry);
936 io_wqe_activate_free_worker(wqe);
941 struct io_wq *io_wq_create(unsigned bounded, struct io_wq_data *data)
946 if (WARN_ON_ONCE(!data->free_work || !data->do_work))
947 return ERR_PTR(-EINVAL);
948 if (WARN_ON_ONCE(!bounded))
949 return ERR_PTR(-EINVAL);
951 wq = kzalloc(struct_size(wq, wqes, nr_node_ids), GFP_KERNEL);
953 return ERR_PTR(-ENOMEM);
954 ret = cpuhp_state_add_instance_nocalls(io_wq_online, &wq->cpuhp_node);
958 refcount_inc(&data->hash->refs);
959 wq->hash = data->hash;
960 wq->free_work = data->free_work;
961 wq->do_work = data->do_work;
964 for_each_node(node) {
966 int alloc_node = node;
968 if (!node_online(alloc_node))
969 alloc_node = NUMA_NO_NODE;
970 wqe = kzalloc_node(sizeof(struct io_wqe), GFP_KERNEL, alloc_node);
973 if (!alloc_cpumask_var(&wqe->cpu_mask, GFP_KERNEL))
975 cpumask_copy(wqe->cpu_mask, cpumask_of_node(node));
976 wq->wqes[node] = wqe;
977 wqe->node = alloc_node;
978 wqe->acct[IO_WQ_ACCT_BOUND].index = IO_WQ_ACCT_BOUND;
979 wqe->acct[IO_WQ_ACCT_UNBOUND].index = IO_WQ_ACCT_UNBOUND;
980 wqe->acct[IO_WQ_ACCT_BOUND].max_workers = bounded;
981 atomic_set(&wqe->acct[IO_WQ_ACCT_BOUND].nr_running, 0);
982 wqe->acct[IO_WQ_ACCT_UNBOUND].max_workers =
983 task_rlimit(current, RLIMIT_NPROC);
984 atomic_set(&wqe->acct[IO_WQ_ACCT_UNBOUND].nr_running, 0);
985 wqe->wait.func = io_wqe_hash_wake;
986 INIT_LIST_HEAD(&wqe->wait.entry);
988 raw_spin_lock_init(&wqe->lock);
989 INIT_WQ_LIST(&wqe->work_list);
990 INIT_HLIST_NULLS_HEAD(&wqe->free_list, 0);
991 INIT_LIST_HEAD(&wqe->all_list);
994 wq->task = get_task_struct(data->task);
995 atomic_set(&wq->worker_refs, 1);
996 init_completion(&wq->worker_done);
999 io_wq_put_hash(data->hash);
1000 cpuhp_state_remove_instance_nocalls(io_wq_online, &wq->cpuhp_node);
1001 for_each_node(node) {
1002 if (!wq->wqes[node])
1004 free_cpumask_var(wq->wqes[node]->cpu_mask);
1005 kfree(wq->wqes[node]);
1009 return ERR_PTR(ret);
1012 static bool io_task_work_match(struct callback_head *cb, void *data)
1014 struct io_worker *worker;
1016 if (cb->func != create_worker_cb)
1018 worker = container_of(cb, struct io_worker, create_work);
1019 return worker->wqe->wq == data;
1022 void io_wq_exit_start(struct io_wq *wq)
1024 set_bit(IO_WQ_BIT_EXIT, &wq->state);
1027 static void io_wq_exit_workers(struct io_wq *wq)
1029 struct callback_head *cb;
1035 while ((cb = task_work_cancel_match(wq->task, io_task_work_match, wq)) != NULL) {
1036 struct io_worker *worker;
1038 worker = container_of(cb, struct io_worker, create_work);
1039 atomic_dec(&worker->wqe->acct[worker->create_index].nr_running);
1040 io_worker_ref_put(wq);
1041 clear_bit_unlock(0, &worker->create_state);
1042 io_worker_release(worker);
1046 for_each_node(node) {
1047 struct io_wqe *wqe = wq->wqes[node];
1049 io_wq_for_each_worker(wqe, io_wq_worker_wake, NULL);
1052 io_worker_ref_put(wq);
1053 wait_for_completion(&wq->worker_done);
1055 for_each_node(node) {
1056 spin_lock_irq(&wq->hash->wait.lock);
1057 list_del_init(&wq->wqes[node]->wait.entry);
1058 spin_unlock_irq(&wq->hash->wait.lock);
1060 put_task_struct(wq->task);
1064 static void io_wq_destroy(struct io_wq *wq)
1068 cpuhp_state_remove_instance_nocalls(io_wq_online, &wq->cpuhp_node);
1070 for_each_node(node) {
1071 struct io_wqe *wqe = wq->wqes[node];
1072 struct io_cb_cancel_data match = {
1073 .fn = io_wq_work_match_all,
1076 io_wqe_cancel_pending_work(wqe, &match);
1077 free_cpumask_var(wqe->cpu_mask);
1080 io_wq_put_hash(wq->hash);
1084 void io_wq_put_and_exit(struct io_wq *wq)
1086 WARN_ON_ONCE(!test_bit(IO_WQ_BIT_EXIT, &wq->state));
1088 io_wq_exit_workers(wq);
1092 struct online_data {
1097 static bool io_wq_worker_affinity(struct io_worker *worker, void *data)
1099 struct online_data *od = data;
1102 cpumask_set_cpu(od->cpu, worker->wqe->cpu_mask);
1104 cpumask_clear_cpu(od->cpu, worker->wqe->cpu_mask);
1108 static int __io_wq_cpu_online(struct io_wq *wq, unsigned int cpu, bool online)
1110 struct online_data od = {
1118 io_wq_for_each_worker(wq->wqes[i], io_wq_worker_affinity, &od);
1123 static int io_wq_cpu_online(unsigned int cpu, struct hlist_node *node)
1125 struct io_wq *wq = hlist_entry_safe(node, struct io_wq, cpuhp_node);
1127 return __io_wq_cpu_online(wq, cpu, true);
1130 static int io_wq_cpu_offline(unsigned int cpu, struct hlist_node *node)
1132 struct io_wq *wq = hlist_entry_safe(node, struct io_wq, cpuhp_node);
1134 return __io_wq_cpu_online(wq, cpu, false);
1137 int io_wq_cpu_affinity(struct io_wq *wq, cpumask_var_t mask)
1143 struct io_wqe *wqe = wq->wqes[i];
1146 cpumask_copy(wqe->cpu_mask, mask);
1148 cpumask_copy(wqe->cpu_mask, cpumask_of_node(i));
1154 static __init int io_wq_init(void)
1158 ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "io-wq/online",
1159 io_wq_cpu_online, io_wq_cpu_offline);
1165 subsys_initcall(io_wq_init);