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>
20 #include "../kernel/sched/sched.h"
23 #define WORKER_IDLE_TIMEOUT (5 * HZ)
26 IO_WORKER_F_UP = 1, /* up and active */
27 IO_WORKER_F_RUNNING = 2, /* account as running */
28 IO_WORKER_F_FREE = 4, /* worker on free list */
29 IO_WORKER_F_FIXED = 8, /* static idle worker */
30 IO_WORKER_F_BOUND = 16, /* is doing bounded work */
34 IO_WQ_BIT_EXIT = 0, /* wq exiting */
35 IO_WQ_BIT_ERROR = 1, /* error on setup */
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 const struct cred *cur_creds;
57 const struct cred *saved_creds;
62 #if BITS_PER_LONG == 64
63 #define IO_WQ_HASH_ORDER 6
65 #define IO_WQ_HASH_ORDER 5
68 #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;
88 unsigned long hash_map;
90 } ____cacheline_aligned_in_smp;
93 struct io_wqe_acct acct[2];
95 struct hlist_nulls_head free_list;
96 struct list_head all_list;
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;
113 struct user_struct *user;
115 struct completion done;
117 struct hlist_node cpuhp_node;
122 static enum cpuhp_state io_wq_online;
124 static bool io_worker_get(struct io_worker *worker)
126 return refcount_inc_not_zero(&worker->ref);
129 static void io_worker_release(struct io_worker *worker)
131 if (refcount_dec_and_test(&worker->ref))
132 wake_up_process(worker->task);
135 static inline struct io_wqe_acct *io_work_get_acct(struct io_wqe *wqe,
136 struct io_wq_work *work)
138 if (work->flags & IO_WQ_WORK_UNBOUND)
139 return &wqe->acct[IO_WQ_ACCT_UNBOUND];
141 return &wqe->acct[IO_WQ_ACCT_BOUND];
144 static inline struct io_wqe_acct *io_wqe_get_acct(struct io_worker *worker)
146 struct io_wqe *wqe = worker->wqe;
148 if (worker->flags & IO_WORKER_F_BOUND)
149 return &wqe->acct[IO_WQ_ACCT_BOUND];
151 return &wqe->acct[IO_WQ_ACCT_UNBOUND];
154 static void io_worker_exit(struct io_worker *worker)
156 struct io_wqe *wqe = worker->wqe;
157 struct io_wqe_acct *acct = io_wqe_get_acct(worker);
161 * If we're not at zero, someone else is holding a brief reference
162 * to the worker. Wait for that to go away.
164 set_current_state(TASK_INTERRUPTIBLE);
165 if (!refcount_dec_and_test(&worker->ref))
167 __set_current_state(TASK_RUNNING);
170 current->flags &= ~PF_IO_WORKER;
171 flags = worker->flags;
173 if (flags & IO_WORKER_F_RUNNING)
174 atomic_dec(&acct->nr_running);
178 if (worker->saved_creds) {
179 revert_creds(worker->saved_creds);
180 worker->cur_creds = worker->saved_creds = NULL;
183 raw_spin_lock_irq(&wqe->lock);
184 if (flags & IO_WORKER_F_FREE)
185 hlist_nulls_del_rcu(&worker->nulls_node);
186 list_del_rcu(&worker->all_list);
188 raw_spin_unlock_irq(&wqe->lock);
190 kfree_rcu(worker, rcu);
191 if (refcount_dec_and_test(&wqe->wq->refs))
192 complete(&wqe->wq->done);
195 static inline bool io_wqe_run_queue(struct io_wqe *wqe)
196 __must_hold(wqe->lock)
198 if (!wq_list_empty(&wqe->work_list) &&
199 !(wqe->flags & IO_WQE_FLAG_STALLED))
205 * Check head of free list for an available worker. If one isn't available,
206 * caller must wake up the wq manager to create one.
208 static bool io_wqe_activate_free_worker(struct io_wqe *wqe)
211 struct hlist_nulls_node *n;
212 struct io_worker *worker;
214 n = rcu_dereference(hlist_nulls_first_rcu(&wqe->free_list));
218 worker = hlist_nulls_entry(n, struct io_worker, nulls_node);
219 if (io_worker_get(worker)) {
220 wake_up_process(worker->task);
221 io_worker_release(worker);
229 * We need a worker. If we find a free one, we're good. If not, and we're
230 * below the max number of workers, wake up the manager to create one.
232 static void io_wqe_wake_worker(struct io_wqe *wqe, struct io_wqe_acct *acct)
237 * Most likely an attempt to queue unbounded work on an io_wq that
238 * wasn't setup with any unbounded workers.
240 WARN_ON_ONCE(!acct->max_workers);
243 ret = io_wqe_activate_free_worker(wqe);
246 if (!ret && acct->nr_workers < acct->max_workers)
247 wake_up_process(wqe->wq->manager);
250 static void io_wqe_inc_running(struct io_worker *worker)
252 struct io_wqe_acct *acct = io_wqe_get_acct(worker);
254 atomic_inc(&acct->nr_running);
257 static void io_wqe_dec_running(struct io_worker *worker)
258 __must_hold(wqe->lock)
260 struct io_wqe_acct *acct = io_wqe_get_acct(worker);
261 struct io_wqe *wqe = worker->wqe;
263 if (atomic_dec_and_test(&acct->nr_running) && io_wqe_run_queue(wqe))
264 io_wqe_wake_worker(wqe, acct);
267 static void io_worker_start(struct io_worker *worker)
269 worker->flags |= (IO_WORKER_F_UP | IO_WORKER_F_RUNNING);
270 io_wqe_inc_running(worker);
274 * Worker will start processing some work. Move it to the busy list, if
275 * it's currently on the freelist
277 static void __io_worker_busy(struct io_wqe *wqe, struct io_worker *worker,
278 struct io_wq_work *work)
279 __must_hold(wqe->lock)
281 bool worker_bound, work_bound;
283 if (worker->flags & IO_WORKER_F_FREE) {
284 worker->flags &= ~IO_WORKER_F_FREE;
285 hlist_nulls_del_init_rcu(&worker->nulls_node);
289 * If worker is moving from bound to unbound (or vice versa), then
290 * ensure we update the running accounting.
292 worker_bound = (worker->flags & IO_WORKER_F_BOUND) != 0;
293 work_bound = (work->flags & IO_WQ_WORK_UNBOUND) == 0;
294 if (worker_bound != work_bound) {
295 io_wqe_dec_running(worker);
297 worker->flags |= IO_WORKER_F_BOUND;
298 wqe->acct[IO_WQ_ACCT_UNBOUND].nr_workers--;
299 wqe->acct[IO_WQ_ACCT_BOUND].nr_workers++;
301 worker->flags &= ~IO_WORKER_F_BOUND;
302 wqe->acct[IO_WQ_ACCT_UNBOUND].nr_workers++;
303 wqe->acct[IO_WQ_ACCT_BOUND].nr_workers--;
305 io_wqe_inc_running(worker);
310 * No work, worker going to sleep. Move to freelist, and unuse mm if we
311 * have one attached. Dropping the mm may potentially sleep, so we drop
312 * the lock in that case and return success. Since the caller has to
313 * retry the loop in that case (we changed task state), we don't regrab
314 * the lock if we return success.
316 static void __io_worker_idle(struct io_wqe *wqe, struct io_worker *worker)
317 __must_hold(wqe->lock)
319 if (!(worker->flags & IO_WORKER_F_FREE)) {
320 worker->flags |= IO_WORKER_F_FREE;
321 hlist_nulls_add_head_rcu(&worker->nulls_node, &wqe->free_list);
323 if (worker->saved_creds) {
324 revert_creds(worker->saved_creds);
325 worker->cur_creds = worker->saved_creds = NULL;
329 static inline unsigned int io_get_work_hash(struct io_wq_work *work)
331 return work->flags >> IO_WQ_HASH_SHIFT;
334 static struct io_wq_work *io_get_next_work(struct io_wqe *wqe)
335 __must_hold(wqe->lock)
337 struct io_wq_work_node *node, *prev;
338 struct io_wq_work *work, *tail;
341 wq_list_for_each(node, prev, &wqe->work_list) {
342 work = container_of(node, struct io_wq_work, list);
344 /* not hashed, can run anytime */
345 if (!io_wq_is_hashed(work)) {
346 wq_list_del(&wqe->work_list, node, prev);
350 /* hashed, can run if not already running */
351 hash = io_get_work_hash(work);
352 if (!(wqe->hash_map & BIT(hash))) {
353 wqe->hash_map |= BIT(hash);
354 /* all items with this hash lie in [work, tail] */
355 tail = wqe->hash_tail[hash];
356 wqe->hash_tail[hash] = NULL;
357 wq_list_cut(&wqe->work_list, &tail->list, prev);
365 static void io_flush_signals(void)
367 if (unlikely(test_tsk_thread_flag(current, TIF_NOTIFY_SIGNAL))) {
368 if (current->task_works)
370 clear_tsk_thread_flag(current, TIF_NOTIFY_SIGNAL);
374 static void io_wq_switch_creds(struct io_worker *worker,
375 struct io_wq_work *work)
377 const struct cred *old_creds = override_creds(work->creds);
379 worker->cur_creds = work->creds;
380 if (worker->saved_creds)
381 put_cred(old_creds); /* creds set by previous switch */
383 worker->saved_creds = old_creds;
386 static void io_assign_current_work(struct io_worker *worker,
387 struct io_wq_work *work)
394 spin_lock_irq(&worker->lock);
395 worker->cur_work = work;
396 spin_unlock_irq(&worker->lock);
399 static void io_wqe_enqueue(struct io_wqe *wqe, struct io_wq_work *work);
401 static void io_worker_handle_work(struct io_worker *worker)
402 __releases(wqe->lock)
404 struct io_wqe *wqe = worker->wqe;
405 struct io_wq *wq = wqe->wq;
408 struct io_wq_work *work;
411 * If we got some work, mark us as busy. If we didn't, but
412 * the list isn't empty, it means we stalled on hashed work.
413 * Mark us stalled so we don't keep looking for work when we
414 * can't make progress, any work completion or insertion will
415 * clear the stalled flag.
417 work = io_get_next_work(wqe);
419 __io_worker_busy(wqe, worker, work);
420 else if (!wq_list_empty(&wqe->work_list))
421 wqe->flags |= IO_WQE_FLAG_STALLED;
423 raw_spin_unlock_irq(&wqe->lock);
426 io_assign_current_work(worker, work);
428 /* handle a whole dependent link */
430 struct io_wq_work *next_hashed, *linked;
431 unsigned int hash = io_get_work_hash(work);
433 next_hashed = wq_next_work(work);
434 if (work->creds && worker->cur_creds != work->creds)
435 io_wq_switch_creds(worker, work);
437 io_assign_current_work(worker, NULL);
439 linked = wq->free_work(work);
441 if (!work && linked && !io_wq_is_hashed(linked)) {
445 io_assign_current_work(worker, work);
447 io_wqe_enqueue(wqe, linked);
449 if (hash != -1U && !next_hashed) {
450 raw_spin_lock_irq(&wqe->lock);
451 wqe->hash_map &= ~BIT_ULL(hash);
452 wqe->flags &= ~IO_WQE_FLAG_STALLED;
453 /* skip unnecessary unlock-lock wqe->lock */
456 raw_spin_unlock_irq(&wqe->lock);
460 raw_spin_lock_irq(&wqe->lock);
464 static int io_wqe_worker(void *data)
466 struct io_worker *worker = data;
467 struct io_wqe *wqe = worker->wqe;
468 struct io_wq *wq = wqe->wq;
470 io_worker_start(worker);
472 while (!test_bit(IO_WQ_BIT_EXIT, &wq->state)) {
473 set_current_state(TASK_INTERRUPTIBLE);
475 raw_spin_lock_irq(&wqe->lock);
476 if (io_wqe_run_queue(wqe)) {
477 __set_current_state(TASK_RUNNING);
478 io_worker_handle_work(worker);
481 __io_worker_idle(wqe, worker);
482 raw_spin_unlock_irq(&wqe->lock);
484 if (schedule_timeout(WORKER_IDLE_TIMEOUT))
486 if (fatal_signal_pending(current))
488 /* timed out, exit unless we're the fixed worker */
489 if (test_bit(IO_WQ_BIT_EXIT, &wq->state) ||
490 !(worker->flags & IO_WORKER_F_FIXED))
494 if (test_bit(IO_WQ_BIT_EXIT, &wq->state)) {
495 raw_spin_lock_irq(&wqe->lock);
496 if (!wq_list_empty(&wqe->work_list))
497 io_worker_handle_work(worker);
499 raw_spin_unlock_irq(&wqe->lock);
502 io_worker_exit(worker);
507 * Called when a worker is scheduled in. Mark us as currently running.
509 void io_wq_worker_running(struct task_struct *tsk)
511 struct io_worker *worker = tsk->pf_io_worker;
515 if (!(worker->flags & IO_WORKER_F_UP))
517 if (worker->flags & IO_WORKER_F_RUNNING)
519 worker->flags |= IO_WORKER_F_RUNNING;
520 io_wqe_inc_running(worker);
524 * Called when worker is going to sleep. If there are no workers currently
525 * running and we have work pending, wake up a free one or have the manager
528 void io_wq_worker_sleeping(struct task_struct *tsk)
530 struct io_worker *worker = tsk->pf_io_worker;
534 if (!(worker->flags & IO_WORKER_F_UP))
536 if (!(worker->flags & IO_WORKER_F_RUNNING))
539 worker->flags &= ~IO_WORKER_F_RUNNING;
541 raw_spin_lock_irq(&worker->wqe->lock);
542 io_wqe_dec_running(worker);
543 raw_spin_unlock_irq(&worker->wqe->lock);
546 static int task_thread(void *data, int index)
548 struct io_worker *worker = data;
549 struct io_wqe *wqe = worker->wqe;
550 struct io_wqe_acct *acct = &wqe->acct[index];
551 struct io_wq *wq = wqe->wq;
552 char buf[TASK_COMM_LEN];
554 sprintf(buf, "iou-wrk-%d", wq->task_pid);
555 set_task_comm(current, buf);
557 current->pf_io_worker = worker;
558 worker->task = current;
560 set_cpus_allowed_ptr(current, cpumask_of_node(wqe->node));
561 current->flags |= PF_NO_SETAFFINITY;
563 raw_spin_lock_irq(&wqe->lock);
564 hlist_nulls_add_head_rcu(&worker->nulls_node, &wqe->free_list);
565 list_add_tail_rcu(&worker->all_list, &wqe->all_list);
566 worker->flags |= IO_WORKER_F_FREE;
567 if (index == IO_WQ_ACCT_BOUND)
568 worker->flags |= IO_WORKER_F_BOUND;
569 if (!acct->nr_workers && (worker->flags & IO_WORKER_F_BOUND))
570 worker->flags |= IO_WORKER_F_FIXED;
572 raw_spin_unlock_irq(&wqe->lock);
578 static int task_thread_bound(void *data)
580 return task_thread(data, IO_WQ_ACCT_BOUND);
583 static int task_thread_unbound(void *data)
585 return task_thread(data, IO_WQ_ACCT_UNBOUND);
588 pid_t io_wq_fork_thread(int (*fn)(void *), void *arg)
590 unsigned long flags = CLONE_FS|CLONE_FILES|CLONE_SIGHAND|CLONE_THREAD|
592 struct kernel_clone_args args = {
593 .flags = ((lower_32_bits(flags) | CLONE_VM |
594 CLONE_UNTRACED) & ~CSIGNAL),
595 .exit_signal = (lower_32_bits(flags) & CSIGNAL),
596 .stack = (unsigned long)fn,
597 .stack_size = (unsigned long)arg,
600 return kernel_clone(&args);
603 static bool create_io_worker(struct io_wq *wq, struct io_wqe *wqe, int index)
605 struct io_worker *worker;
608 worker = kzalloc_node(sizeof(*worker), GFP_KERNEL, wqe->node);
612 refcount_set(&worker->ref, 1);
613 worker->nulls_node.pprev = NULL;
615 spin_lock_init(&worker->lock);
617 if (index == IO_WQ_ACCT_BOUND)
618 pid = io_wq_fork_thread(task_thread_bound, worker);
620 pid = io_wq_fork_thread(task_thread_unbound, worker);
625 refcount_inc(&wq->refs);
629 static inline bool io_wqe_need_worker(struct io_wqe *wqe, int index)
630 __must_hold(wqe->lock)
632 struct io_wqe_acct *acct = &wqe->acct[index];
634 /* if we have available workers or no work, no need */
635 if (!hlist_nulls_empty(&wqe->free_list) || !io_wqe_run_queue(wqe))
637 return acct->nr_workers < acct->max_workers;
641 * Iterate the passed in list and call the specific function for each
642 * worker that isn't exiting
644 static bool io_wq_for_each_worker(struct io_wqe *wqe,
645 bool (*func)(struct io_worker *, void *),
648 struct io_worker *worker;
651 list_for_each_entry_rcu(worker, &wqe->all_list, all_list) {
652 if (io_worker_get(worker)) {
653 /* no task if node is/was offline */
655 ret = func(worker, data);
656 io_worker_release(worker);
665 static bool io_wq_worker_wake(struct io_worker *worker, void *data)
667 wake_up_process(worker->task);
672 * Manager thread. Tasked with creating new workers, if we need them.
674 static int io_wq_manager(void *data)
676 struct io_wq *wq = data;
677 char buf[TASK_COMM_LEN];
680 sprintf(buf, "iou-mgr-%d", wq->task_pid);
681 set_task_comm(current, buf);
682 current->flags |= PF_IO_WORKER;
683 wq->manager = current;
687 while (!test_bit(IO_WQ_BIT_EXIT, &wq->state)) {
688 for_each_node(node) {
689 struct io_wqe *wqe = wq->wqes[node];
690 bool fork_worker[2] = { false, false };
692 if (!node_online(node))
695 raw_spin_lock_irq(&wqe->lock);
696 if (io_wqe_need_worker(wqe, IO_WQ_ACCT_BOUND))
697 fork_worker[IO_WQ_ACCT_BOUND] = true;
698 if (io_wqe_need_worker(wqe, IO_WQ_ACCT_UNBOUND))
699 fork_worker[IO_WQ_ACCT_UNBOUND] = true;
700 raw_spin_unlock_irq(&wqe->lock);
701 if (fork_worker[IO_WQ_ACCT_BOUND])
702 create_io_worker(wq, wqe, IO_WQ_ACCT_BOUND);
703 if (fork_worker[IO_WQ_ACCT_UNBOUND])
704 create_io_worker(wq, wqe, IO_WQ_ACCT_UNBOUND);
706 set_current_state(TASK_INTERRUPTIBLE);
707 schedule_timeout(HZ);
708 if (fatal_signal_pending(current))
709 set_bit(IO_WQ_BIT_EXIT, &wq->state);
712 if (refcount_dec_and_test(&wq->refs)) {
716 /* if ERROR is set and we get here, we have workers to wake */
717 if (test_bit(IO_WQ_BIT_ERROR, &wq->state)) {
720 io_wq_for_each_worker(wq->wqes[node], io_wq_worker_wake, NULL);
726 static void io_run_cancel(struct io_wq_work *work, struct io_wqe *wqe)
728 struct io_wq *wq = wqe->wq;
731 work->flags |= IO_WQ_WORK_CANCEL;
733 work = wq->free_work(work);
737 static void io_wqe_insert_work(struct io_wqe *wqe, struct io_wq_work *work)
740 struct io_wq_work *tail;
742 if (!io_wq_is_hashed(work)) {
744 wq_list_add_tail(&work->list, &wqe->work_list);
748 hash = io_get_work_hash(work);
749 tail = wqe->hash_tail[hash];
750 wqe->hash_tail[hash] = work;
754 wq_list_add_after(&work->list, &tail->list, &wqe->work_list);
757 static void io_wqe_enqueue(struct io_wqe *wqe, struct io_wq_work *work)
759 struct io_wqe_acct *acct = io_work_get_acct(wqe, work);
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 struct io_cb_cancel_data {
801 static bool io_wq_worker_cancel(struct io_worker *worker, void *data)
803 struct io_cb_cancel_data *match = data;
807 * Hold the lock to avoid ->cur_work going out of scope, caller
808 * may dereference the passed in work.
810 spin_lock_irqsave(&worker->lock, flags);
811 if (worker->cur_work &&
812 match->fn(worker->cur_work, match->data)) {
813 set_notify_signal(worker->task);
816 spin_unlock_irqrestore(&worker->lock, flags);
818 return match->nr_running && !match->cancel_all;
821 static inline void io_wqe_remove_pending(struct io_wqe *wqe,
822 struct io_wq_work *work,
823 struct io_wq_work_node *prev)
825 unsigned int hash = io_get_work_hash(work);
826 struct io_wq_work *prev_work = NULL;
828 if (io_wq_is_hashed(work) && work == wqe->hash_tail[hash]) {
830 prev_work = container_of(prev, struct io_wq_work, list);
831 if (prev_work && io_get_work_hash(prev_work) == hash)
832 wqe->hash_tail[hash] = prev_work;
834 wqe->hash_tail[hash] = NULL;
836 wq_list_del(&wqe->work_list, &work->list, prev);
839 static void io_wqe_cancel_pending_work(struct io_wqe *wqe,
840 struct io_cb_cancel_data *match)
842 struct io_wq_work_node *node, *prev;
843 struct io_wq_work *work;
847 raw_spin_lock_irqsave(&wqe->lock, flags);
848 wq_list_for_each(node, prev, &wqe->work_list) {
849 work = container_of(node, struct io_wq_work, list);
850 if (!match->fn(work, match->data))
852 io_wqe_remove_pending(wqe, work, prev);
853 raw_spin_unlock_irqrestore(&wqe->lock, flags);
854 io_run_cancel(work, wqe);
856 if (!match->cancel_all)
859 /* not safe to continue after unlock */
862 raw_spin_unlock_irqrestore(&wqe->lock, flags);
865 static void io_wqe_cancel_running_work(struct io_wqe *wqe,
866 struct io_cb_cancel_data *match)
869 io_wq_for_each_worker(wqe, io_wq_worker_cancel, match);
873 enum io_wq_cancel io_wq_cancel_cb(struct io_wq *wq, work_cancel_fn *cancel,
874 void *data, bool cancel_all)
876 struct io_cb_cancel_data match = {
879 .cancel_all = cancel_all,
884 * First check pending list, if we're lucky we can just remove it
885 * from there. CANCEL_OK means that the work is returned as-new,
886 * no completion will be posted for it.
888 for_each_node(node) {
889 struct io_wqe *wqe = wq->wqes[node];
891 io_wqe_cancel_pending_work(wqe, &match);
892 if (match.nr_pending && !match.cancel_all)
893 return IO_WQ_CANCEL_OK;
897 * Now check if a free (going busy) or busy worker has the work
898 * currently running. If we find it there, we'll return CANCEL_RUNNING
899 * as an indication that we attempt to signal cancellation. The
900 * completion will run normally in this case.
902 for_each_node(node) {
903 struct io_wqe *wqe = wq->wqes[node];
905 io_wqe_cancel_running_work(wqe, &match);
906 if (match.nr_running && !match.cancel_all)
907 return IO_WQ_CANCEL_RUNNING;
910 if (match.nr_running)
911 return IO_WQ_CANCEL_RUNNING;
912 if (match.nr_pending)
913 return IO_WQ_CANCEL_OK;
914 return IO_WQ_CANCEL_NOTFOUND;
917 struct io_wq *io_wq_create(unsigned bounded, struct io_wq_data *data)
919 int ret = -ENOMEM, node;
922 if (WARN_ON_ONCE(!data->free_work || !data->do_work))
923 return ERR_PTR(-EINVAL);
925 wq = kzalloc(sizeof(*wq), GFP_KERNEL);
927 return ERR_PTR(-ENOMEM);
929 wq->wqes = kcalloc(nr_node_ids, sizeof(struct io_wqe *), GFP_KERNEL);
933 ret = cpuhp_state_add_instance_nocalls(io_wq_online, &wq->cpuhp_node);
937 wq->free_work = data->free_work;
938 wq->do_work = data->do_work;
941 for_each_node(node) {
943 int alloc_node = node;
945 if (!node_online(alloc_node))
946 alloc_node = NUMA_NO_NODE;
947 wqe = kzalloc_node(sizeof(struct io_wqe), GFP_KERNEL, alloc_node);
950 wq->wqes[node] = wqe;
951 wqe->node = alloc_node;
952 wqe->acct[IO_WQ_ACCT_BOUND].max_workers = bounded;
953 atomic_set(&wqe->acct[IO_WQ_ACCT_BOUND].nr_running, 0);
954 wqe->acct[IO_WQ_ACCT_UNBOUND].max_workers =
955 task_rlimit(current, RLIMIT_NPROC);
956 atomic_set(&wqe->acct[IO_WQ_ACCT_UNBOUND].nr_running, 0);
958 raw_spin_lock_init(&wqe->lock);
959 INIT_WQ_LIST(&wqe->work_list);
960 INIT_HLIST_NULLS_HEAD(&wqe->free_list, 0);
961 INIT_LIST_HEAD(&wqe->all_list);
964 wq->task_pid = current->pid;
965 init_completion(&wq->done);
966 refcount_set(&wq->refs, 1);
968 current->flags |= PF_IO_WORKER;
969 ret = io_wq_fork_thread(io_wq_manager, wq);
970 current->flags &= ~PF_IO_WORKER;
972 wait_for_completion(&wq->done);
973 reinit_completion(&wq->done);
977 if (refcount_dec_and_test(&wq->refs))
980 cpuhp_state_remove_instance_nocalls(io_wq_online, &wq->cpuhp_node);
982 kfree(wq->wqes[node]);
990 void io_wq_destroy(struct io_wq *wq)
994 cpuhp_state_remove_instance_nocalls(io_wq_online, &wq->cpuhp_node);
996 set_bit(IO_WQ_BIT_EXIT, &wq->state);
998 wake_up_process(wq->manager);
1002 io_wq_for_each_worker(wq->wqes[node], io_wq_worker_wake, NULL);
1005 wait_for_completion(&wq->done);
1008 kfree(wq->wqes[node]);
1013 static bool io_wq_worker_affinity(struct io_worker *worker, void *data)
1015 struct task_struct *task = worker->task;
1019 rq = task_rq_lock(task, &rf);
1020 do_set_cpus_allowed(task, cpumask_of_node(worker->wqe->node));
1021 task->flags |= PF_NO_SETAFFINITY;
1022 task_rq_unlock(rq, task, &rf);
1026 static int io_wq_cpu_online(unsigned int cpu, struct hlist_node *node)
1028 struct io_wq *wq = hlist_entry_safe(node, struct io_wq, cpuhp_node);
1033 io_wq_for_each_worker(wq->wqes[i], io_wq_worker_affinity, NULL);
1038 static __init int io_wq_init(void)
1042 ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "io-wq/online",
1043 io_wq_cpu_online, NULL);
1049 subsys_initcall(io_wq_init);