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>
13 #include <linux/sched/mm.h>
14 #include <linux/percpu.h>
15 #include <linux/slab.h>
16 #include <linux/rculist_nulls.h>
17 #include <linux/cpu.h>
18 #include <linux/tracehook.h>
19 #include <linux/freezer.h>
21 #include "../kernel/sched/sched.h"
24 #define WORKER_IDLE_TIMEOUT (5 * HZ)
27 IO_WORKER_F_UP = 1, /* up and active */
28 IO_WORKER_F_RUNNING = 2, /* account as running */
29 IO_WORKER_F_FREE = 4, /* worker on free list */
30 IO_WORKER_F_FIXED = 8, /* static idle worker */
31 IO_WORKER_F_BOUND = 16, /* is doing bounded work */
35 IO_WQ_BIT_EXIT = 0, /* wq exiting */
39 IO_WQE_FLAG_STALLED = 1, /* stalled on hash */
43 * One for each thread in a wqe pool
48 struct hlist_nulls_node nulls_node;
49 struct list_head all_list;
50 struct task_struct *task;
53 struct io_wq_work *cur_work;
56 struct completion ref_done;
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)
81 * Per-node worker thread pool
86 struct io_wq_work_list work_list;
88 } ____cacheline_aligned_in_smp;
91 struct io_wqe_acct acct[2];
93 struct hlist_nulls_head free_list;
94 struct list_head all_list;
96 struct wait_queue_entry wait;
99 struct io_wq_work *hash_tail[IO_WQ_NR_HASH_BUCKETS];
106 struct io_wqe **wqes;
109 free_work_fn *free_work;
110 io_wq_work_fn *do_work;
112 struct task_struct *manager;
114 struct io_wq_hash *hash;
117 struct completion exited;
119 atomic_t worker_refs;
120 struct completion worker_done;
122 struct hlist_node cpuhp_node;
127 static enum cpuhp_state io_wq_online;
129 struct io_cb_cancel_data {
137 static void io_wqe_cancel_pending_work(struct io_wqe *wqe,
138 struct io_cb_cancel_data *match);
140 static bool io_worker_get(struct io_worker *worker)
142 return refcount_inc_not_zero(&worker->ref);
145 static void io_worker_release(struct io_worker *worker)
147 if (refcount_dec_and_test(&worker->ref))
148 complete(&worker->ref_done);
151 static inline struct io_wqe_acct *io_work_get_acct(struct io_wqe *wqe,
152 struct io_wq_work *work)
154 if (work->flags & IO_WQ_WORK_UNBOUND)
155 return &wqe->acct[IO_WQ_ACCT_UNBOUND];
157 return &wqe->acct[IO_WQ_ACCT_BOUND];
160 static inline struct io_wqe_acct *io_wqe_get_acct(struct io_worker *worker)
162 struct io_wqe *wqe = worker->wqe;
164 if (worker->flags & IO_WORKER_F_BOUND)
165 return &wqe->acct[IO_WQ_ACCT_BOUND];
167 return &wqe->acct[IO_WQ_ACCT_UNBOUND];
170 static void io_worker_exit(struct io_worker *worker)
172 struct io_wqe *wqe = worker->wqe;
173 struct io_wqe_acct *acct = io_wqe_get_acct(worker);
176 if (refcount_dec_and_test(&worker->ref))
177 complete(&worker->ref_done);
178 wait_for_completion(&worker->ref_done);
181 current->flags &= ~PF_IO_WORKER;
182 flags = worker->flags;
184 if (flags & IO_WORKER_F_RUNNING)
185 atomic_dec(&acct->nr_running);
189 raw_spin_lock_irq(&wqe->lock);
190 if (flags & IO_WORKER_F_FREE)
191 hlist_nulls_del_rcu(&worker->nulls_node);
192 list_del_rcu(&worker->all_list);
194 raw_spin_unlock_irq(&wqe->lock);
196 kfree_rcu(worker, rcu);
197 if (atomic_dec_and_test(&wqe->wq->worker_refs))
198 complete(&wqe->wq->worker_done);
202 static inline bool io_wqe_run_queue(struct io_wqe *wqe)
203 __must_hold(wqe->lock)
205 if (!wq_list_empty(&wqe->work_list) &&
206 !(wqe->flags & IO_WQE_FLAG_STALLED))
212 * Check head of free list for an available worker. If one isn't available,
213 * caller must wake up the wq manager to create one.
215 static bool io_wqe_activate_free_worker(struct io_wqe *wqe)
218 struct hlist_nulls_node *n;
219 struct io_worker *worker;
221 n = rcu_dereference(hlist_nulls_first_rcu(&wqe->free_list));
225 worker = hlist_nulls_entry(n, struct io_worker, nulls_node);
226 if (io_worker_get(worker)) {
227 wake_up_process(worker->task);
228 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, wake up the manager to 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 WARN_ON_ONCE(!acct->max_workers);
250 ret = io_wqe_activate_free_worker(wqe);
253 if (!ret && acct->nr_workers < acct->max_workers)
254 wake_up_process(wqe->wq->manager);
257 static void io_wqe_inc_running(struct io_worker *worker)
259 struct io_wqe_acct *acct = io_wqe_get_acct(worker);
261 atomic_inc(&acct->nr_running);
264 static void io_wqe_dec_running(struct io_worker *worker)
265 __must_hold(wqe->lock)
267 struct io_wqe_acct *acct = io_wqe_get_acct(worker);
268 struct io_wqe *wqe = worker->wqe;
270 if (atomic_dec_and_test(&acct->nr_running) && io_wqe_run_queue(wqe))
271 io_wqe_wake_worker(wqe, acct);
275 * Worker will start processing some work. Move it to the busy list, if
276 * it's currently on the freelist
278 static void __io_worker_busy(struct io_wqe *wqe, struct io_worker *worker,
279 struct io_wq_work *work)
280 __must_hold(wqe->lock)
282 bool worker_bound, work_bound;
284 if (worker->flags & IO_WORKER_F_FREE) {
285 worker->flags &= ~IO_WORKER_F_FREE;
286 hlist_nulls_del_init_rcu(&worker->nulls_node);
290 * If worker is moving from bound to unbound (or vice versa), then
291 * ensure we update the running accounting.
293 worker_bound = (worker->flags & IO_WORKER_F_BOUND) != 0;
294 work_bound = (work->flags & IO_WQ_WORK_UNBOUND) == 0;
295 if (worker_bound != work_bound) {
296 io_wqe_dec_running(worker);
298 worker->flags |= IO_WORKER_F_BOUND;
299 wqe->acct[IO_WQ_ACCT_UNBOUND].nr_workers--;
300 wqe->acct[IO_WQ_ACCT_BOUND].nr_workers++;
302 worker->flags &= ~IO_WORKER_F_BOUND;
303 wqe->acct[IO_WQ_ACCT_UNBOUND].nr_workers++;
304 wqe->acct[IO_WQ_ACCT_BOUND].nr_workers--;
306 io_wqe_inc_running(worker);
311 * No work, worker going to sleep. Move to freelist, and unuse mm if we
312 * have one attached. Dropping the mm may potentially sleep, so we drop
313 * the lock in that case and return success. Since the caller has to
314 * retry the loop in that case (we changed task state), we don't regrab
315 * the lock if we return success.
317 static void __io_worker_idle(struct io_wqe *wqe, struct io_worker *worker)
318 __must_hold(wqe->lock)
320 if (!(worker->flags & IO_WORKER_F_FREE)) {
321 worker->flags |= IO_WORKER_F_FREE;
322 hlist_nulls_add_head_rcu(&worker->nulls_node, &wqe->free_list);
326 static inline unsigned int io_get_work_hash(struct io_wq_work *work)
328 return work->flags >> IO_WQ_HASH_SHIFT;
331 static void io_wait_on_hash(struct io_wqe *wqe, unsigned int hash)
333 struct io_wq *wq = wqe->wq;
335 spin_lock(&wq->hash->wait.lock);
336 if (list_empty(&wqe->wait.entry)) {
337 __add_wait_queue(&wq->hash->wait, &wqe->wait);
338 if (!test_bit(hash, &wq->hash->map)) {
339 __set_current_state(TASK_RUNNING);
340 list_del_init(&wqe->wait.entry);
343 spin_unlock(&wq->hash->wait.lock);
346 static struct io_wq_work *io_get_next_work(struct io_wqe *wqe)
347 __must_hold(wqe->lock)
349 struct io_wq_work_node *node, *prev;
350 struct io_wq_work *work, *tail;
351 unsigned int stall_hash = -1U;
353 wq_list_for_each(node, prev, &wqe->work_list) {
356 work = container_of(node, struct io_wq_work, list);
358 /* not hashed, can run anytime */
359 if (!io_wq_is_hashed(work)) {
360 wq_list_del(&wqe->work_list, node, prev);
364 hash = io_get_work_hash(work);
365 /* all items with this hash lie in [work, tail] */
366 tail = wqe->hash_tail[hash];
368 /* hashed, can run if not already running */
369 if (!test_and_set_bit(hash, &wqe->wq->hash->map)) {
370 wqe->hash_tail[hash] = NULL;
371 wq_list_cut(&wqe->work_list, &tail->list, prev);
374 if (stall_hash == -1U)
376 /* fast forward to a next hash, for-each will fix up @prev */
380 if (stall_hash != -1U) {
381 raw_spin_unlock(&wqe->lock);
382 io_wait_on_hash(wqe, stall_hash);
383 raw_spin_lock(&wqe->lock);
389 static bool io_flush_signals(void)
391 if (unlikely(test_tsk_thread_flag(current, TIF_NOTIFY_SIGNAL))) {
392 __set_current_state(TASK_RUNNING);
393 if (current->task_works)
395 clear_tsk_thread_flag(current, TIF_NOTIFY_SIGNAL);
401 static void io_assign_current_work(struct io_worker *worker,
402 struct io_wq_work *work)
409 spin_lock_irq(&worker->lock);
410 worker->cur_work = work;
411 spin_unlock_irq(&worker->lock);
414 static void io_wqe_enqueue(struct io_wqe *wqe, struct io_wq_work *work);
416 static void io_worker_handle_work(struct io_worker *worker)
417 __releases(wqe->lock)
419 struct io_wqe *wqe = worker->wqe;
420 struct io_wq *wq = wqe->wq;
423 struct io_wq_work *work;
426 * If we got some work, mark us as busy. If we didn't, but
427 * the list isn't empty, it means we stalled on hashed work.
428 * Mark us stalled so we don't keep looking for work when we
429 * can't make progress, any work completion or insertion will
430 * clear the stalled flag.
432 work = io_get_next_work(wqe);
434 __io_worker_busy(wqe, worker, work);
435 else if (!wq_list_empty(&wqe->work_list))
436 wqe->flags |= IO_WQE_FLAG_STALLED;
438 raw_spin_unlock_irq(&wqe->lock);
441 io_assign_current_work(worker, work);
442 __set_current_state(TASK_RUNNING);
444 /* handle a whole dependent link */
446 struct io_wq_work *next_hashed, *linked;
447 unsigned int hash = io_get_work_hash(work);
449 next_hashed = wq_next_work(work);
451 io_assign_current_work(worker, NULL);
453 linked = wq->free_work(work);
455 if (!work && linked && !io_wq_is_hashed(linked)) {
459 io_assign_current_work(worker, work);
461 io_wqe_enqueue(wqe, linked);
463 if (hash != -1U && !next_hashed) {
464 clear_bit(hash, &wq->hash->map);
465 if (wq_has_sleeper(&wq->hash->wait))
466 wake_up(&wq->hash->wait);
467 raw_spin_lock_irq(&wqe->lock);
468 wqe->flags &= ~IO_WQE_FLAG_STALLED;
469 /* skip unnecessary unlock-lock wqe->lock */
472 raw_spin_unlock_irq(&wqe->lock);
476 raw_spin_lock_irq(&wqe->lock);
480 static int io_wqe_worker(void *data)
482 struct io_worker *worker = data;
483 struct io_wqe *wqe = worker->wqe;
484 struct io_wq *wq = wqe->wq;
485 char buf[TASK_COMM_LEN];
487 worker->flags |= (IO_WORKER_F_UP | IO_WORKER_F_RUNNING);
488 io_wqe_inc_running(worker);
490 sprintf(buf, "iou-wrk-%d", wq->task_pid);
491 set_task_comm(current, buf);
493 while (!test_bit(IO_WQ_BIT_EXIT, &wq->state)) {
496 set_current_state(TASK_INTERRUPTIBLE);
498 raw_spin_lock_irq(&wqe->lock);
499 if (io_wqe_run_queue(wqe)) {
500 io_worker_handle_work(worker);
503 __io_worker_idle(wqe, worker);
504 raw_spin_unlock_irq(&wqe->lock);
505 if (io_flush_signals())
507 ret = schedule_timeout(WORKER_IDLE_TIMEOUT);
508 if (try_to_freeze() || ret)
510 if (fatal_signal_pending(current))
512 /* timed out, exit unless we're the fixed worker */
513 if (test_bit(IO_WQ_BIT_EXIT, &wq->state) ||
514 !(worker->flags & IO_WORKER_F_FIXED))
518 if (test_bit(IO_WQ_BIT_EXIT, &wq->state)) {
519 raw_spin_lock_irq(&wqe->lock);
520 if (!wq_list_empty(&wqe->work_list))
521 io_worker_handle_work(worker);
523 raw_spin_unlock_irq(&wqe->lock);
526 io_worker_exit(worker);
531 * Called when a worker is scheduled in. Mark us as currently running.
533 void io_wq_worker_running(struct task_struct *tsk)
535 struct io_worker *worker = tsk->pf_io_worker;
539 if (!(worker->flags & IO_WORKER_F_UP))
541 if (worker->flags & IO_WORKER_F_RUNNING)
543 worker->flags |= IO_WORKER_F_RUNNING;
544 io_wqe_inc_running(worker);
548 * Called when worker is going to sleep. If there are no workers currently
549 * running and we have work pending, wake up a free one or have the manager
552 void io_wq_worker_sleeping(struct task_struct *tsk)
554 struct io_worker *worker = tsk->pf_io_worker;
558 if (!(worker->flags & IO_WORKER_F_UP))
560 if (!(worker->flags & IO_WORKER_F_RUNNING))
563 worker->flags &= ~IO_WORKER_F_RUNNING;
565 raw_spin_lock_irq(&worker->wqe->lock);
566 io_wqe_dec_running(worker);
567 raw_spin_unlock_irq(&worker->wqe->lock);
570 static bool create_io_worker(struct io_wq *wq, struct io_wqe *wqe, int index)
572 struct io_wqe_acct *acct = &wqe->acct[index];
573 struct io_worker *worker;
574 struct task_struct *tsk;
576 __set_current_state(TASK_RUNNING);
578 worker = kzalloc_node(sizeof(*worker), GFP_KERNEL, wqe->node);
582 refcount_set(&worker->ref, 1);
583 worker->nulls_node.pprev = NULL;
585 spin_lock_init(&worker->lock);
586 init_completion(&worker->ref_done);
588 atomic_inc(&wq->worker_refs);
590 tsk = create_io_thread(io_wqe_worker, worker, wqe->node);
592 if (atomic_dec_and_test(&wq->worker_refs))
593 complete(&wq->worker_done);
598 tsk->pf_io_worker = worker;
600 set_cpus_allowed_ptr(tsk, cpumask_of_node(wqe->node));
601 tsk->flags |= PF_NO_SETAFFINITY;
603 raw_spin_lock_irq(&wqe->lock);
604 hlist_nulls_add_head_rcu(&worker->nulls_node, &wqe->free_list);
605 list_add_tail_rcu(&worker->all_list, &wqe->all_list);
606 worker->flags |= IO_WORKER_F_FREE;
607 if (index == IO_WQ_ACCT_BOUND)
608 worker->flags |= IO_WORKER_F_BOUND;
609 if (!acct->nr_workers && (worker->flags & IO_WORKER_F_BOUND))
610 worker->flags |= IO_WORKER_F_FIXED;
612 raw_spin_unlock_irq(&wqe->lock);
613 wake_up_new_task(tsk);
617 static inline bool io_wqe_need_worker(struct io_wqe *wqe, int index)
618 __must_hold(wqe->lock)
620 struct io_wqe_acct *acct = &wqe->acct[index];
622 if (acct->nr_workers && test_bit(IO_WQ_BIT_EXIT, &wqe->wq->state))
624 /* if we have available workers or no work, no need */
625 if (!hlist_nulls_empty(&wqe->free_list) || !io_wqe_run_queue(wqe))
627 return acct->nr_workers < acct->max_workers;
631 * Iterate the passed in list and call the specific function for each
632 * worker that isn't exiting
634 static bool io_wq_for_each_worker(struct io_wqe *wqe,
635 bool (*func)(struct io_worker *, void *),
638 struct io_worker *worker;
641 list_for_each_entry_rcu(worker, &wqe->all_list, all_list) {
642 if (io_worker_get(worker)) {
643 /* no task if node is/was offline */
645 ret = func(worker, data);
646 io_worker_release(worker);
655 static bool io_wq_worker_wake(struct io_worker *worker, void *data)
657 set_notify_signal(worker->task);
658 wake_up_process(worker->task);
662 static void io_wq_check_workers(struct io_wq *wq)
666 for_each_node(node) {
667 struct io_wqe *wqe = wq->wqes[node];
668 bool fork_worker[2] = { false, false };
670 if (!node_online(node))
673 raw_spin_lock_irq(&wqe->lock);
674 if (io_wqe_need_worker(wqe, IO_WQ_ACCT_BOUND))
675 fork_worker[IO_WQ_ACCT_BOUND] = true;
676 if (io_wqe_need_worker(wqe, IO_WQ_ACCT_UNBOUND))
677 fork_worker[IO_WQ_ACCT_UNBOUND] = true;
678 raw_spin_unlock_irq(&wqe->lock);
679 if (fork_worker[IO_WQ_ACCT_BOUND])
680 create_io_worker(wq, wqe, IO_WQ_ACCT_BOUND);
681 if (fork_worker[IO_WQ_ACCT_UNBOUND])
682 create_io_worker(wq, wqe, IO_WQ_ACCT_UNBOUND);
686 static bool io_wq_work_match_all(struct io_wq_work *work, void *data)
691 static void io_wq_cancel_pending(struct io_wq *wq)
693 struct io_cb_cancel_data match = {
694 .fn = io_wq_work_match_all,
700 io_wqe_cancel_pending_work(wq->wqes[node], &match);
704 * Manager thread. Tasked with creating new workers, if we need them.
706 static int io_wq_manager(void *data)
708 struct io_wq *wq = data;
709 char buf[TASK_COMM_LEN];
712 sprintf(buf, "iou-mgr-%d", wq->task_pid);
713 set_task_comm(current, buf);
716 set_current_state(TASK_INTERRUPTIBLE);
717 io_wq_check_workers(wq);
718 schedule_timeout(HZ);
720 if (fatal_signal_pending(current))
721 set_bit(IO_WQ_BIT_EXIT, &wq->state);
722 } while (!test_bit(IO_WQ_BIT_EXIT, &wq->state));
724 io_wq_check_workers(wq);
728 io_wq_for_each_worker(wq->wqes[node], io_wq_worker_wake, NULL);
731 if (atomic_dec_and_test(&wq->worker_refs))
732 complete(&wq->worker_done);
733 wait_for_completion(&wq->worker_done);
735 spin_lock_irq(&wq->hash->wait.lock);
737 list_del_init(&wq->wqes[node]->wait.entry);
738 spin_unlock_irq(&wq->hash->wait.lock);
740 io_wq_cancel_pending(wq);
741 complete(&wq->exited);
745 static void io_run_cancel(struct io_wq_work *work, struct io_wqe *wqe)
747 struct io_wq *wq = wqe->wq;
750 work->flags |= IO_WQ_WORK_CANCEL;
752 work = wq->free_work(work);
756 static void io_wqe_insert_work(struct io_wqe *wqe, struct io_wq_work *work)
759 struct io_wq_work *tail;
761 if (!io_wq_is_hashed(work)) {
763 wq_list_add_tail(&work->list, &wqe->work_list);
767 hash = io_get_work_hash(work);
768 tail = wqe->hash_tail[hash];
769 wqe->hash_tail[hash] = work;
773 wq_list_add_after(&work->list, &tail->list, &wqe->work_list);
776 static int io_wq_fork_manager(struct io_wq *wq)
778 struct task_struct *tsk;
783 WARN_ON_ONCE(test_bit(IO_WQ_BIT_EXIT, &wq->state));
785 init_completion(&wq->worker_done);
786 atomic_set(&wq->worker_refs, 1);
787 tsk = create_io_thread(io_wq_manager, wq, NUMA_NO_NODE);
789 wq->manager = get_task_struct(tsk);
790 wake_up_new_task(tsk);
794 if (atomic_dec_and_test(&wq->worker_refs))
795 complete(&wq->worker_done);
800 static void io_wqe_enqueue(struct io_wqe *wqe, struct io_wq_work *work)
802 struct io_wqe_acct *acct = io_work_get_acct(wqe, work);
806 /* Can only happen if manager creation fails after exec */
807 if (io_wq_fork_manager(wqe->wq) ||
808 test_bit(IO_WQ_BIT_EXIT, &wqe->wq->state)) {
809 io_run_cancel(work, wqe);
813 work_flags = work->flags;
814 raw_spin_lock_irqsave(&wqe->lock, flags);
815 io_wqe_insert_work(wqe, work);
816 wqe->flags &= ~IO_WQE_FLAG_STALLED;
817 raw_spin_unlock_irqrestore(&wqe->lock, flags);
819 if ((work_flags & IO_WQ_WORK_CONCURRENT) ||
820 !atomic_read(&acct->nr_running))
821 io_wqe_wake_worker(wqe, acct);
824 void io_wq_enqueue(struct io_wq *wq, struct io_wq_work *work)
826 struct io_wqe *wqe = wq->wqes[numa_node_id()];
828 io_wqe_enqueue(wqe, work);
832 * Work items that hash to the same value will not be done in parallel.
833 * Used to limit concurrent writes, generally hashed by inode.
835 void io_wq_hash_work(struct io_wq_work *work, void *val)
839 bit = hash_ptr(val, IO_WQ_HASH_ORDER);
840 work->flags |= (IO_WQ_WORK_HASHED | (bit << IO_WQ_HASH_SHIFT));
843 static bool io_wq_worker_cancel(struct io_worker *worker, void *data)
845 struct io_cb_cancel_data *match = data;
849 * Hold the lock to avoid ->cur_work going out of scope, caller
850 * may dereference the passed in work.
852 spin_lock_irqsave(&worker->lock, flags);
853 if (worker->cur_work &&
854 match->fn(worker->cur_work, match->data)) {
855 set_notify_signal(worker->task);
858 spin_unlock_irqrestore(&worker->lock, flags);
860 return match->nr_running && !match->cancel_all;
863 static inline void io_wqe_remove_pending(struct io_wqe *wqe,
864 struct io_wq_work *work,
865 struct io_wq_work_node *prev)
867 unsigned int hash = io_get_work_hash(work);
868 struct io_wq_work *prev_work = NULL;
870 if (io_wq_is_hashed(work) && work == wqe->hash_tail[hash]) {
872 prev_work = container_of(prev, struct io_wq_work, list);
873 if (prev_work && io_get_work_hash(prev_work) == hash)
874 wqe->hash_tail[hash] = prev_work;
876 wqe->hash_tail[hash] = NULL;
878 wq_list_del(&wqe->work_list, &work->list, prev);
881 static void io_wqe_cancel_pending_work(struct io_wqe *wqe,
882 struct io_cb_cancel_data *match)
884 struct io_wq_work_node *node, *prev;
885 struct io_wq_work *work;
889 raw_spin_lock_irqsave(&wqe->lock, flags);
890 wq_list_for_each(node, prev, &wqe->work_list) {
891 work = container_of(node, struct io_wq_work, list);
892 if (!match->fn(work, match->data))
894 io_wqe_remove_pending(wqe, work, prev);
895 raw_spin_unlock_irqrestore(&wqe->lock, flags);
896 io_run_cancel(work, wqe);
898 if (!match->cancel_all)
901 /* not safe to continue after unlock */
904 raw_spin_unlock_irqrestore(&wqe->lock, flags);
907 static void io_wqe_cancel_running_work(struct io_wqe *wqe,
908 struct io_cb_cancel_data *match)
911 io_wq_for_each_worker(wqe, io_wq_worker_cancel, match);
915 enum io_wq_cancel io_wq_cancel_cb(struct io_wq *wq, work_cancel_fn *cancel,
916 void *data, bool cancel_all)
918 struct io_cb_cancel_data match = {
921 .cancel_all = cancel_all,
926 * First check pending list, if we're lucky we can just remove it
927 * from there. CANCEL_OK means that the work is returned as-new,
928 * no completion will be posted for it.
930 for_each_node(node) {
931 struct io_wqe *wqe = wq->wqes[node];
933 io_wqe_cancel_pending_work(wqe, &match);
934 if (match.nr_pending && !match.cancel_all)
935 return IO_WQ_CANCEL_OK;
939 * Now check if a free (going busy) or busy worker has the work
940 * currently running. If we find it there, we'll return CANCEL_RUNNING
941 * as an indication that we attempt to signal cancellation. The
942 * completion will run normally in this case.
944 for_each_node(node) {
945 struct io_wqe *wqe = wq->wqes[node];
947 io_wqe_cancel_running_work(wqe, &match);
948 if (match.nr_running && !match.cancel_all)
949 return IO_WQ_CANCEL_RUNNING;
952 if (match.nr_running)
953 return IO_WQ_CANCEL_RUNNING;
954 if (match.nr_pending)
955 return IO_WQ_CANCEL_OK;
956 return IO_WQ_CANCEL_NOTFOUND;
959 static int io_wqe_hash_wake(struct wait_queue_entry *wait, unsigned mode,
962 struct io_wqe *wqe = container_of(wait, struct io_wqe, wait);
965 list_del_init(&wait->entry);
968 ret = io_wqe_activate_free_worker(wqe);
972 wake_up_process(wqe->wq->manager);
977 struct io_wq *io_wq_create(unsigned bounded, struct io_wq_data *data)
979 int ret = -ENOMEM, node;
982 if (WARN_ON_ONCE(!data->free_work || !data->do_work))
983 return ERR_PTR(-EINVAL);
985 wq = kzalloc(sizeof(*wq), GFP_KERNEL);
987 return ERR_PTR(-ENOMEM);
989 wq->wqes = kcalloc(nr_node_ids, sizeof(struct io_wqe *), GFP_KERNEL);
993 ret = cpuhp_state_add_instance_nocalls(io_wq_online, &wq->cpuhp_node);
997 refcount_inc(&data->hash->refs);
998 wq->hash = data->hash;
999 wq->free_work = data->free_work;
1000 wq->do_work = data->do_work;
1003 for_each_node(node) {
1005 int alloc_node = node;
1007 if (!node_online(alloc_node))
1008 alloc_node = NUMA_NO_NODE;
1009 wqe = kzalloc_node(sizeof(struct io_wqe), GFP_KERNEL, alloc_node);
1012 wq->wqes[node] = wqe;
1013 wqe->node = alloc_node;
1014 wqe->acct[IO_WQ_ACCT_BOUND].max_workers = bounded;
1015 atomic_set(&wqe->acct[IO_WQ_ACCT_BOUND].nr_running, 0);
1016 wqe->acct[IO_WQ_ACCT_UNBOUND].max_workers =
1017 task_rlimit(current, RLIMIT_NPROC);
1018 atomic_set(&wqe->acct[IO_WQ_ACCT_UNBOUND].nr_running, 0);
1019 wqe->wait.func = io_wqe_hash_wake;
1020 INIT_LIST_HEAD(&wqe->wait.entry);
1022 raw_spin_lock_init(&wqe->lock);
1023 INIT_WQ_LIST(&wqe->work_list);
1024 INIT_HLIST_NULLS_HEAD(&wqe->free_list, 0);
1025 INIT_LIST_HEAD(&wqe->all_list);
1028 wq->task_pid = current->pid;
1029 init_completion(&wq->exited);
1030 refcount_set(&wq->refs, 1);
1032 ret = io_wq_fork_manager(wq);
1036 io_wq_put_hash(data->hash);
1037 cpuhp_state_remove_instance_nocalls(io_wq_online, &wq->cpuhp_node);
1039 kfree(wq->wqes[node]);
1044 return ERR_PTR(ret);
1047 static void io_wq_destroy_manager(struct io_wq *wq)
1050 wake_up_process(wq->manager);
1051 wait_for_completion(&wq->exited);
1052 put_task_struct(wq->manager);
1057 static void io_wq_destroy(struct io_wq *wq)
1061 cpuhp_state_remove_instance_nocalls(io_wq_online, &wq->cpuhp_node);
1063 set_bit(IO_WQ_BIT_EXIT, &wq->state);
1064 io_wq_destroy_manager(wq);
1066 for_each_node(node) {
1067 struct io_wqe *wqe = wq->wqes[node];
1068 WARN_ON_ONCE(!wq_list_empty(&wqe->work_list));
1071 io_wq_put_hash(wq->hash);
1076 void io_wq_put(struct io_wq *wq)
1078 if (refcount_dec_and_test(&wq->refs))
1082 void io_wq_put_and_exit(struct io_wq *wq)
1084 set_bit(IO_WQ_BIT_EXIT, &wq->state);
1085 io_wq_destroy_manager(wq);
1089 static bool io_wq_worker_affinity(struct io_worker *worker, void *data)
1091 struct task_struct *task = worker->task;
1095 rq = task_rq_lock(task, &rf);
1096 do_set_cpus_allowed(task, cpumask_of_node(worker->wqe->node));
1097 task->flags |= PF_NO_SETAFFINITY;
1098 task_rq_unlock(rq, task, &rf);
1102 static int io_wq_cpu_online(unsigned int cpu, struct hlist_node *node)
1104 struct io_wq *wq = hlist_entry_safe(node, struct io_wq, cpuhp_node);
1109 io_wq_for_each_worker(wq->wqes[i], io_wq_worker_affinity, NULL);
1114 static __init int io_wq_init(void)
1118 ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "io-wq/online",
1119 io_wq_cpu_online, NULL);
1125 subsys_initcall(io_wq_init);