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/kthread.h>
17 #include <linux/rculist_nulls.h>
18 #include <linux/fs_struct.h>
19 #include <linux/task_work.h>
20 #include <linux/blk-cgroup.h>
21 #include <linux/audit.h>
22 #include <linux/cpu.h>
24 #include "../kernel/sched/sched.h"
27 #define WORKER_IDLE_TIMEOUT (5 * HZ)
30 IO_WORKER_F_UP = 1, /* up and active */
31 IO_WORKER_F_RUNNING = 2, /* account as running */
32 IO_WORKER_F_FREE = 4, /* worker on free list */
33 IO_WORKER_F_FIXED = 8, /* static idle worker */
34 IO_WORKER_F_BOUND = 16, /* is doing bounded work */
38 IO_WQ_BIT_EXIT = 0, /* wq exiting */
39 IO_WQ_BIT_ERROR = 1, /* error on setup */
43 IO_WQE_FLAG_STALLED = 1, /* stalled on hash */
47 * One for each thread in a wqe pool
52 struct hlist_nulls_node nulls_node;
53 struct list_head all_list;
54 struct task_struct *task;
57 struct io_wq_work *cur_work;
62 #ifdef CONFIG_BLK_CGROUP
63 struct cgroup_subsys_state *blkcg_css;
65 const struct cred *cur_creds;
66 const struct cred *saved_creds;
67 struct nsproxy *restore_nsproxy;
70 #if BITS_PER_LONG == 64
71 #define IO_WQ_HASH_ORDER 6
73 #define IO_WQ_HASH_ORDER 5
76 #define IO_WQ_NR_HASH_BUCKETS (1u << IO_WQ_HASH_ORDER)
90 * Per-node worker thread pool
95 struct io_wq_work_list work_list;
96 unsigned long hash_map;
98 } ____cacheline_aligned_in_smp;
101 struct io_wqe_acct acct[2];
103 struct hlist_nulls_head free_list;
104 struct list_head all_list;
107 struct io_wq_work *hash_tail[IO_WQ_NR_HASH_BUCKETS];
114 struct io_wqe **wqes;
117 free_work_fn *free_work;
118 io_wq_work_fn *do_work;
120 struct task_struct *manager;
121 struct user_struct *user;
123 struct completion done;
125 struct hlist_node cpuhp_node;
130 static enum cpuhp_state io_wq_online;
132 static bool io_worker_get(struct io_worker *worker)
134 return refcount_inc_not_zero(&worker->ref);
137 static void io_worker_release(struct io_worker *worker)
139 if (refcount_dec_and_test(&worker->ref))
140 wake_up_process(worker->task);
144 * Note: drops the wqe->lock if returning true! The caller must re-acquire
145 * the lock in that case. Some callers need to restart handling if this
146 * happens, so we can't just re-acquire the lock on behalf of the caller.
148 static bool __io_worker_unuse(struct io_wqe *wqe, struct io_worker *worker)
150 bool dropped_lock = false;
152 if (worker->saved_creds) {
153 revert_creds(worker->saved_creds);
154 worker->cur_creds = worker->saved_creds = NULL;
157 if (current->files) {
158 __acquire(&wqe->lock);
159 raw_spin_unlock_irq(&wqe->lock);
163 current->files = NULL;
164 current->nsproxy = worker->restore_nsproxy;
165 task_unlock(current);
172 * If we have an active mm, we need to drop the wq lock before unusing
173 * it. If we do, return true and let the caller retry the idle loop.
177 __acquire(&wqe->lock);
178 raw_spin_unlock_irq(&wqe->lock);
181 __set_current_state(TASK_RUNNING);
182 kthread_unuse_mm(worker->mm);
187 #ifdef CONFIG_BLK_CGROUP
188 if (worker->blkcg_css) {
189 kthread_associate_blkcg(NULL);
190 worker->blkcg_css = NULL;
193 if (current->signal->rlim[RLIMIT_FSIZE].rlim_cur != RLIM_INFINITY)
194 current->signal->rlim[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
198 static inline struct io_wqe_acct *io_work_get_acct(struct io_wqe *wqe,
199 struct io_wq_work *work)
201 if (work->flags & IO_WQ_WORK_UNBOUND)
202 return &wqe->acct[IO_WQ_ACCT_UNBOUND];
204 return &wqe->acct[IO_WQ_ACCT_BOUND];
207 static inline struct io_wqe_acct *io_wqe_get_acct(struct io_wqe *wqe,
208 struct io_worker *worker)
210 if (worker->flags & IO_WORKER_F_BOUND)
211 return &wqe->acct[IO_WQ_ACCT_BOUND];
213 return &wqe->acct[IO_WQ_ACCT_UNBOUND];
216 static void io_worker_exit(struct io_worker *worker)
218 struct io_wqe *wqe = worker->wqe;
219 struct io_wqe_acct *acct = io_wqe_get_acct(wqe, worker);
222 * If we're not at zero, someone else is holding a brief reference
223 * to the worker. Wait for that to go away.
225 set_current_state(TASK_INTERRUPTIBLE);
226 if (!refcount_dec_and_test(&worker->ref))
228 __set_current_state(TASK_RUNNING);
231 current->flags &= ~PF_IO_WORKER;
232 if (worker->flags & IO_WORKER_F_RUNNING)
233 atomic_dec(&acct->nr_running);
234 if (!(worker->flags & IO_WORKER_F_BOUND))
235 atomic_dec(&wqe->wq->user->processes);
239 raw_spin_lock_irq(&wqe->lock);
240 hlist_nulls_del_rcu(&worker->nulls_node);
241 list_del_rcu(&worker->all_list);
242 if (__io_worker_unuse(wqe, worker)) {
243 __release(&wqe->lock);
244 raw_spin_lock_irq(&wqe->lock);
247 raw_spin_unlock_irq(&wqe->lock);
249 kfree_rcu(worker, rcu);
250 if (refcount_dec_and_test(&wqe->wq->refs))
251 complete(&wqe->wq->done);
254 static inline bool io_wqe_run_queue(struct io_wqe *wqe)
255 __must_hold(wqe->lock)
257 if (!wq_list_empty(&wqe->work_list) &&
258 !(wqe->flags & IO_WQE_FLAG_STALLED))
264 * Check head of free list for an available worker. If one isn't available,
265 * caller must wake up the wq manager to create one.
267 static bool io_wqe_activate_free_worker(struct io_wqe *wqe)
270 struct hlist_nulls_node *n;
271 struct io_worker *worker;
273 n = rcu_dereference(hlist_nulls_first_rcu(&wqe->free_list));
277 worker = hlist_nulls_entry(n, struct io_worker, nulls_node);
278 if (io_worker_get(worker)) {
279 wake_up_process(worker->task);
280 io_worker_release(worker);
288 * We need a worker. If we find a free one, we're good. If not, and we're
289 * below the max number of workers, wake up the manager to create one.
291 static void io_wqe_wake_worker(struct io_wqe *wqe, struct io_wqe_acct *acct)
296 * Most likely an attempt to queue unbounded work on an io_wq that
297 * wasn't setup with any unbounded workers.
299 WARN_ON_ONCE(!acct->max_workers);
302 ret = io_wqe_activate_free_worker(wqe);
305 if (!ret && acct->nr_workers < acct->max_workers)
306 wake_up_process(wqe->wq->manager);
309 static void io_wqe_inc_running(struct io_wqe *wqe, struct io_worker *worker)
311 struct io_wqe_acct *acct = io_wqe_get_acct(wqe, worker);
313 atomic_inc(&acct->nr_running);
316 static void io_wqe_dec_running(struct io_wqe *wqe, struct io_worker *worker)
317 __must_hold(wqe->lock)
319 struct io_wqe_acct *acct = io_wqe_get_acct(wqe, worker);
321 if (atomic_dec_and_test(&acct->nr_running) && io_wqe_run_queue(wqe))
322 io_wqe_wake_worker(wqe, acct);
325 static void io_worker_start(struct io_wqe *wqe, struct io_worker *worker)
327 allow_kernel_signal(SIGINT);
329 current->flags |= PF_IO_WORKER;
331 current->files = NULL;
333 worker->flags |= (IO_WORKER_F_UP | IO_WORKER_F_RUNNING);
334 worker->restore_nsproxy = current->nsproxy;
335 io_wqe_inc_running(wqe, worker);
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 if (worker->flags & IO_WORKER_F_FREE) {
349 worker->flags &= ~IO_WORKER_F_FREE;
350 hlist_nulls_del_init_rcu(&worker->nulls_node);
354 * If worker is moving from bound to unbound (or vice versa), then
355 * ensure we update the running accounting.
357 worker_bound = (worker->flags & IO_WORKER_F_BOUND) != 0;
358 work_bound = (work->flags & IO_WQ_WORK_UNBOUND) == 0;
359 if (worker_bound != work_bound) {
360 io_wqe_dec_running(wqe, worker);
362 worker->flags |= IO_WORKER_F_BOUND;
363 wqe->acct[IO_WQ_ACCT_UNBOUND].nr_workers--;
364 wqe->acct[IO_WQ_ACCT_BOUND].nr_workers++;
365 atomic_dec(&wqe->wq->user->processes);
367 worker->flags &= ~IO_WORKER_F_BOUND;
368 wqe->acct[IO_WQ_ACCT_UNBOUND].nr_workers++;
369 wqe->acct[IO_WQ_ACCT_BOUND].nr_workers--;
370 atomic_inc(&wqe->wq->user->processes);
372 io_wqe_inc_running(wqe, worker);
377 * No work, worker going to sleep. Move to freelist, and unuse mm if we
378 * have one attached. Dropping the mm may potentially sleep, so we drop
379 * the lock in that case and return success. Since the caller has to
380 * retry the loop in that case (we changed task state), we don't regrab
381 * the lock if we return success.
383 static bool __io_worker_idle(struct io_wqe *wqe, struct io_worker *worker)
384 __must_hold(wqe->lock)
386 if (!(worker->flags & IO_WORKER_F_FREE)) {
387 worker->flags |= IO_WORKER_F_FREE;
388 hlist_nulls_add_head_rcu(&worker->nulls_node, &wqe->free_list);
391 return __io_worker_unuse(wqe, worker);
394 static inline unsigned int io_get_work_hash(struct io_wq_work *work)
396 return work->flags >> IO_WQ_HASH_SHIFT;
399 static struct io_wq_work *io_get_next_work(struct io_wqe *wqe)
400 __must_hold(wqe->lock)
402 struct io_wq_work_node *node, *prev;
403 struct io_wq_work *work, *tail;
406 wq_list_for_each(node, prev, &wqe->work_list) {
407 work = container_of(node, struct io_wq_work, list);
409 /* not hashed, can run anytime */
410 if (!io_wq_is_hashed(work)) {
411 wq_list_del(&wqe->work_list, node, prev);
415 /* hashed, can run if not already running */
416 hash = io_get_work_hash(work);
417 if (!(wqe->hash_map & BIT(hash))) {
418 wqe->hash_map |= BIT(hash);
419 /* all items with this hash lie in [work, tail] */
420 tail = wqe->hash_tail[hash];
421 wqe->hash_tail[hash] = NULL;
422 wq_list_cut(&wqe->work_list, &tail->list, prev);
430 static void io_wq_switch_mm(struct io_worker *worker, struct io_wq_work *work)
433 kthread_unuse_mm(worker->mm);
438 if (mmget_not_zero(work->identity->mm)) {
439 kthread_use_mm(work->identity->mm);
440 worker->mm = work->identity->mm;
444 /* failed grabbing mm, ensure work gets cancelled */
445 work->flags |= IO_WQ_WORK_CANCEL;
448 static inline void io_wq_switch_blkcg(struct io_worker *worker,
449 struct io_wq_work *work)
451 #ifdef CONFIG_BLK_CGROUP
452 if (!(work->flags & IO_WQ_WORK_BLKCG))
454 if (work->identity->blkcg_css != worker->blkcg_css) {
455 kthread_associate_blkcg(work->identity->blkcg_css);
456 worker->blkcg_css = work->identity->blkcg_css;
461 static void io_wq_switch_creds(struct io_worker *worker,
462 struct io_wq_work *work)
464 const struct cred *old_creds = override_creds(work->identity->creds);
466 worker->cur_creds = work->identity->creds;
467 if (worker->saved_creds)
468 put_cred(old_creds); /* creds set by previous switch */
470 worker->saved_creds = old_creds;
473 static void io_impersonate_work(struct io_worker *worker,
474 struct io_wq_work *work)
476 if ((work->flags & IO_WQ_WORK_FILES) &&
477 current->files != work->identity->files) {
479 current->files = work->identity->files;
480 current->nsproxy = work->identity->nsproxy;
481 task_unlock(current);
482 if (!work->identity->files) {
483 /* failed grabbing files, ensure work gets cancelled */
484 work->flags |= IO_WQ_WORK_CANCEL;
487 if ((work->flags & IO_WQ_WORK_FS) && current->fs != work->identity->fs)
488 current->fs = work->identity->fs;
489 if ((work->flags & IO_WQ_WORK_MM) && work->identity->mm != worker->mm)
490 io_wq_switch_mm(worker, work);
491 if ((work->flags & IO_WQ_WORK_CREDS) &&
492 worker->cur_creds != work->identity->creds)
493 io_wq_switch_creds(worker, work);
494 if (work->flags & IO_WQ_WORK_FSIZE)
495 current->signal->rlim[RLIMIT_FSIZE].rlim_cur = work->identity->fsize;
496 else if (current->signal->rlim[RLIMIT_FSIZE].rlim_cur != RLIM_INFINITY)
497 current->signal->rlim[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
498 io_wq_switch_blkcg(worker, work);
500 current->loginuid = work->identity->loginuid;
501 current->sessionid = work->identity->sessionid;
505 static void io_assign_current_work(struct io_worker *worker,
506 struct io_wq_work *work)
509 /* flush pending signals before assigning new work */
510 if (signal_pending(current))
511 flush_signals(current);
516 current->loginuid = KUIDT_INIT(AUDIT_UID_UNSET);
517 current->sessionid = AUDIT_SID_UNSET;
520 spin_lock_irq(&worker->lock);
521 worker->cur_work = work;
522 spin_unlock_irq(&worker->lock);
525 static void io_wqe_enqueue(struct io_wqe *wqe, struct io_wq_work *work);
527 static void io_worker_handle_work(struct io_worker *worker)
528 __releases(wqe->lock)
530 struct io_wqe *wqe = worker->wqe;
531 struct io_wq *wq = wqe->wq;
534 struct io_wq_work *work;
537 * If we got some work, mark us as busy. If we didn't, but
538 * the list isn't empty, it means we stalled on hashed work.
539 * Mark us stalled so we don't keep looking for work when we
540 * can't make progress, any work completion or insertion will
541 * clear the stalled flag.
543 work = io_get_next_work(wqe);
545 __io_worker_busy(wqe, worker, work);
546 else if (!wq_list_empty(&wqe->work_list))
547 wqe->flags |= IO_WQE_FLAG_STALLED;
549 raw_spin_unlock_irq(&wqe->lock);
552 io_assign_current_work(worker, work);
554 /* handle a whole dependent link */
556 struct io_wq_work *next_hashed, *linked;
557 unsigned int hash = io_get_work_hash(work);
559 next_hashed = wq_next_work(work);
560 io_impersonate_work(worker, work);
562 io_assign_current_work(worker, NULL);
564 linked = wq->free_work(work);
566 if (!work && linked && !io_wq_is_hashed(linked)) {
570 io_assign_current_work(worker, work);
572 io_wqe_enqueue(wqe, linked);
574 if (hash != -1U && !next_hashed) {
575 raw_spin_lock_irq(&wqe->lock);
576 wqe->hash_map &= ~BIT_ULL(hash);
577 wqe->flags &= ~IO_WQE_FLAG_STALLED;
578 /* skip unnecessary unlock-lock wqe->lock */
581 raw_spin_unlock_irq(&wqe->lock);
585 raw_spin_lock_irq(&wqe->lock);
589 static int io_wqe_worker(void *data)
591 struct io_worker *worker = data;
592 struct io_wqe *wqe = worker->wqe;
593 struct io_wq *wq = wqe->wq;
595 io_worker_start(wqe, worker);
597 while (!test_bit(IO_WQ_BIT_EXIT, &wq->state)) {
598 set_current_state(TASK_INTERRUPTIBLE);
600 raw_spin_lock_irq(&wqe->lock);
601 if (io_wqe_run_queue(wqe)) {
602 __set_current_state(TASK_RUNNING);
603 io_worker_handle_work(worker);
606 /* drops the lock on success, retry */
607 if (__io_worker_idle(wqe, worker)) {
608 __release(&wqe->lock);
611 raw_spin_unlock_irq(&wqe->lock);
612 if (signal_pending(current))
613 flush_signals(current);
614 if (schedule_timeout(WORKER_IDLE_TIMEOUT))
616 /* timed out, exit unless we're the fixed worker */
617 if (test_bit(IO_WQ_BIT_EXIT, &wq->state) ||
618 !(worker->flags & IO_WORKER_F_FIXED))
622 if (test_bit(IO_WQ_BIT_EXIT, &wq->state)) {
623 raw_spin_lock_irq(&wqe->lock);
624 if (!wq_list_empty(&wqe->work_list))
625 io_worker_handle_work(worker);
627 raw_spin_unlock_irq(&wqe->lock);
630 io_worker_exit(worker);
635 * Called when a worker is scheduled in. Mark us as currently running.
637 void io_wq_worker_running(struct task_struct *tsk)
639 struct io_worker *worker = kthread_data(tsk);
640 struct io_wqe *wqe = worker->wqe;
642 if (!(worker->flags & IO_WORKER_F_UP))
644 if (worker->flags & IO_WORKER_F_RUNNING)
646 worker->flags |= IO_WORKER_F_RUNNING;
647 io_wqe_inc_running(wqe, worker);
651 * Called when worker is going to sleep. If there are no workers currently
652 * running and we have work pending, wake up a free one or have the manager
655 void io_wq_worker_sleeping(struct task_struct *tsk)
657 struct io_worker *worker = kthread_data(tsk);
658 struct io_wqe *wqe = worker->wqe;
660 if (!(worker->flags & IO_WORKER_F_UP))
662 if (!(worker->flags & IO_WORKER_F_RUNNING))
665 worker->flags &= ~IO_WORKER_F_RUNNING;
667 raw_spin_lock_irq(&wqe->lock);
668 io_wqe_dec_running(wqe, worker);
669 raw_spin_unlock_irq(&wqe->lock);
672 static bool create_io_worker(struct io_wq *wq, struct io_wqe *wqe, int index)
674 struct io_wqe_acct *acct = &wqe->acct[index];
675 struct io_worker *worker;
677 worker = kzalloc_node(sizeof(*worker), GFP_KERNEL, wqe->node);
681 refcount_set(&worker->ref, 1);
682 worker->nulls_node.pprev = NULL;
684 spin_lock_init(&worker->lock);
686 worker->task = kthread_create_on_node(io_wqe_worker, worker, wqe->node,
687 "io_wqe_worker-%d/%d", index, wqe->node);
688 if (IS_ERR(worker->task)) {
692 kthread_bind_mask(worker->task, cpumask_of_node(wqe->node));
694 raw_spin_lock_irq(&wqe->lock);
695 hlist_nulls_add_head_rcu(&worker->nulls_node, &wqe->free_list);
696 list_add_tail_rcu(&worker->all_list, &wqe->all_list);
697 worker->flags |= IO_WORKER_F_FREE;
698 if (index == IO_WQ_ACCT_BOUND)
699 worker->flags |= IO_WORKER_F_BOUND;
700 if (!acct->nr_workers && (worker->flags & IO_WORKER_F_BOUND))
701 worker->flags |= IO_WORKER_F_FIXED;
703 raw_spin_unlock_irq(&wqe->lock);
705 if (index == IO_WQ_ACCT_UNBOUND)
706 atomic_inc(&wq->user->processes);
708 refcount_inc(&wq->refs);
709 wake_up_process(worker->task);
713 static inline bool io_wqe_need_worker(struct io_wqe *wqe, int index)
714 __must_hold(wqe->lock)
716 struct io_wqe_acct *acct = &wqe->acct[index];
718 /* if we have available workers or no work, no need */
719 if (!hlist_nulls_empty(&wqe->free_list) || !io_wqe_run_queue(wqe))
721 return acct->nr_workers < acct->max_workers;
725 * Iterate the passed in list and call the specific function for each
726 * worker that isn't exiting
728 static bool io_wq_for_each_worker(struct io_wqe *wqe,
729 bool (*func)(struct io_worker *, void *),
732 struct io_worker *worker;
735 list_for_each_entry_rcu(worker, &wqe->all_list, all_list) {
736 if (io_worker_get(worker)) {
737 /* no task if node is/was offline */
739 ret = func(worker, data);
740 io_worker_release(worker);
749 static bool io_wq_worker_wake(struct io_worker *worker, void *data)
751 wake_up_process(worker->task);
756 * Manager thread. Tasked with creating new workers, if we need them.
758 static int io_wq_manager(void *data)
760 struct io_wq *wq = data;
763 /* create fixed workers */
764 refcount_set(&wq->refs, 1);
765 for_each_node(node) {
766 if (!node_online(node))
768 if (create_io_worker(wq, wq->wqes[node], IO_WQ_ACCT_BOUND))
770 set_bit(IO_WQ_BIT_ERROR, &wq->state);
771 set_bit(IO_WQ_BIT_EXIT, &wq->state);
777 while (!kthread_should_stop()) {
778 if (current->task_works)
781 for_each_node(node) {
782 struct io_wqe *wqe = wq->wqes[node];
783 bool fork_worker[2] = { false, false };
785 if (!node_online(node))
788 raw_spin_lock_irq(&wqe->lock);
789 if (io_wqe_need_worker(wqe, IO_WQ_ACCT_BOUND))
790 fork_worker[IO_WQ_ACCT_BOUND] = true;
791 if (io_wqe_need_worker(wqe, IO_WQ_ACCT_UNBOUND))
792 fork_worker[IO_WQ_ACCT_UNBOUND] = true;
793 raw_spin_unlock_irq(&wqe->lock);
794 if (fork_worker[IO_WQ_ACCT_BOUND])
795 create_io_worker(wq, wqe, IO_WQ_ACCT_BOUND);
796 if (fork_worker[IO_WQ_ACCT_UNBOUND])
797 create_io_worker(wq, wqe, IO_WQ_ACCT_UNBOUND);
799 set_current_state(TASK_INTERRUPTIBLE);
800 schedule_timeout(HZ);
803 if (current->task_works)
807 if (refcount_dec_and_test(&wq->refs)) {
811 /* if ERROR is set and we get here, we have workers to wake */
812 if (test_bit(IO_WQ_BIT_ERROR, &wq->state)) {
815 io_wq_for_each_worker(wq->wqes[node], io_wq_worker_wake, NULL);
821 static bool io_wq_can_queue(struct io_wqe *wqe, struct io_wqe_acct *acct,
822 struct io_wq_work *work)
826 if (!(work->flags & IO_WQ_WORK_UNBOUND))
828 if (atomic_read(&acct->nr_running))
832 free_worker = !hlist_nulls_empty(&wqe->free_list);
837 if (atomic_read(&wqe->wq->user->processes) >= acct->max_workers &&
838 !(capable(CAP_SYS_RESOURCE) || capable(CAP_SYS_ADMIN)))
844 static void io_run_cancel(struct io_wq_work *work, struct io_wqe *wqe)
846 struct io_wq *wq = wqe->wq;
849 work->flags |= IO_WQ_WORK_CANCEL;
851 work = wq->free_work(work);
855 static void io_wqe_insert_work(struct io_wqe *wqe, struct io_wq_work *work)
858 struct io_wq_work *tail;
860 if (!io_wq_is_hashed(work)) {
862 wq_list_add_tail(&work->list, &wqe->work_list);
866 hash = io_get_work_hash(work);
867 tail = wqe->hash_tail[hash];
868 wqe->hash_tail[hash] = work;
872 wq_list_add_after(&work->list, &tail->list, &wqe->work_list);
875 static void io_wqe_enqueue(struct io_wqe *wqe, struct io_wq_work *work)
877 struct io_wqe_acct *acct = io_work_get_acct(wqe, work);
882 * Do early check to see if we need a new unbound worker, and if we do,
883 * if we're allowed to do so. This isn't 100% accurate as there's a
884 * gap between this check and incrementing the value, but that's OK.
885 * It's close enough to not be an issue, fork() has the same delay.
887 if (unlikely(!io_wq_can_queue(wqe, acct, work))) {
888 io_run_cancel(work, wqe);
892 work_flags = work->flags;
893 raw_spin_lock_irqsave(&wqe->lock, flags);
894 io_wqe_insert_work(wqe, work);
895 wqe->flags &= ~IO_WQE_FLAG_STALLED;
896 raw_spin_unlock_irqrestore(&wqe->lock, flags);
898 if ((work_flags & IO_WQ_WORK_CONCURRENT) ||
899 !atomic_read(&acct->nr_running))
900 io_wqe_wake_worker(wqe, acct);
903 void io_wq_enqueue(struct io_wq *wq, struct io_wq_work *work)
905 struct io_wqe *wqe = wq->wqes[numa_node_id()];
907 io_wqe_enqueue(wqe, work);
911 * Work items that hash to the same value will not be done in parallel.
912 * Used to limit concurrent writes, generally hashed by inode.
914 void io_wq_hash_work(struct io_wq_work *work, void *val)
918 bit = hash_ptr(val, IO_WQ_HASH_ORDER);
919 work->flags |= (IO_WQ_WORK_HASHED | (bit << IO_WQ_HASH_SHIFT));
922 struct io_cb_cancel_data {
930 static bool io_wq_worker_cancel(struct io_worker *worker, void *data)
932 struct io_cb_cancel_data *match = data;
936 * Hold the lock to avoid ->cur_work going out of scope, caller
937 * may dereference the passed in work.
939 spin_lock_irqsave(&worker->lock, flags);
940 if (worker->cur_work &&
941 match->fn(worker->cur_work, match->data)) {
942 send_sig(SIGINT, worker->task, 1);
945 spin_unlock_irqrestore(&worker->lock, flags);
947 return match->nr_running && !match->cancel_all;
950 static inline void io_wqe_remove_pending(struct io_wqe *wqe,
951 struct io_wq_work *work,
952 struct io_wq_work_node *prev)
954 unsigned int hash = io_get_work_hash(work);
955 struct io_wq_work *prev_work = NULL;
957 if (io_wq_is_hashed(work) && work == wqe->hash_tail[hash]) {
959 prev_work = container_of(prev, struct io_wq_work, list);
960 if (prev_work && io_get_work_hash(prev_work) == hash)
961 wqe->hash_tail[hash] = prev_work;
963 wqe->hash_tail[hash] = NULL;
965 wq_list_del(&wqe->work_list, &work->list, prev);
968 static void io_wqe_cancel_pending_work(struct io_wqe *wqe,
969 struct io_cb_cancel_data *match)
971 struct io_wq_work_node *node, *prev;
972 struct io_wq_work *work;
976 raw_spin_lock_irqsave(&wqe->lock, flags);
977 wq_list_for_each(node, prev, &wqe->work_list) {
978 work = container_of(node, struct io_wq_work, list);
979 if (!match->fn(work, match->data))
981 io_wqe_remove_pending(wqe, work, prev);
982 raw_spin_unlock_irqrestore(&wqe->lock, flags);
983 io_run_cancel(work, wqe);
985 if (!match->cancel_all)
988 /* not safe to continue after unlock */
991 raw_spin_unlock_irqrestore(&wqe->lock, flags);
994 static void io_wqe_cancel_running_work(struct io_wqe *wqe,
995 struct io_cb_cancel_data *match)
998 io_wq_for_each_worker(wqe, io_wq_worker_cancel, match);
1002 enum io_wq_cancel io_wq_cancel_cb(struct io_wq *wq, work_cancel_fn *cancel,
1003 void *data, bool cancel_all)
1005 struct io_cb_cancel_data match = {
1008 .cancel_all = cancel_all,
1013 * First check pending list, if we're lucky we can just remove it
1014 * from there. CANCEL_OK means that the work is returned as-new,
1015 * no completion will be posted for it.
1017 for_each_node(node) {
1018 struct io_wqe *wqe = wq->wqes[node];
1020 io_wqe_cancel_pending_work(wqe, &match);
1021 if (match.nr_pending && !match.cancel_all)
1022 return IO_WQ_CANCEL_OK;
1026 * Now check if a free (going busy) or busy worker has the work
1027 * currently running. If we find it there, we'll return CANCEL_RUNNING
1028 * as an indication that we attempt to signal cancellation. The
1029 * completion will run normally in this case.
1031 for_each_node(node) {
1032 struct io_wqe *wqe = wq->wqes[node];
1034 io_wqe_cancel_running_work(wqe, &match);
1035 if (match.nr_running && !match.cancel_all)
1036 return IO_WQ_CANCEL_RUNNING;
1039 if (match.nr_running)
1040 return IO_WQ_CANCEL_RUNNING;
1041 if (match.nr_pending)
1042 return IO_WQ_CANCEL_OK;
1043 return IO_WQ_CANCEL_NOTFOUND;
1046 struct io_wq *io_wq_create(unsigned bounded, struct io_wq_data *data)
1048 int ret = -ENOMEM, node;
1051 if (WARN_ON_ONCE(!data->free_work || !data->do_work))
1052 return ERR_PTR(-EINVAL);
1054 wq = kzalloc(sizeof(*wq), GFP_KERNEL);
1056 return ERR_PTR(-ENOMEM);
1058 wq->wqes = kcalloc(nr_node_ids, sizeof(struct io_wqe *), GFP_KERNEL);
1062 ret = cpuhp_state_add_instance_nocalls(io_wq_online, &wq->cpuhp_node);
1066 wq->free_work = data->free_work;
1067 wq->do_work = data->do_work;
1069 /* caller must already hold a reference to this */
1070 wq->user = data->user;
1073 for_each_node(node) {
1075 int alloc_node = node;
1077 if (!node_online(alloc_node))
1078 alloc_node = NUMA_NO_NODE;
1079 wqe = kzalloc_node(sizeof(struct io_wqe), GFP_KERNEL, alloc_node);
1082 wq->wqes[node] = wqe;
1083 wqe->node = alloc_node;
1084 wqe->acct[IO_WQ_ACCT_BOUND].max_workers = bounded;
1085 atomic_set(&wqe->acct[IO_WQ_ACCT_BOUND].nr_running, 0);
1087 wqe->acct[IO_WQ_ACCT_UNBOUND].max_workers =
1088 task_rlimit(current, RLIMIT_NPROC);
1090 atomic_set(&wqe->acct[IO_WQ_ACCT_UNBOUND].nr_running, 0);
1092 raw_spin_lock_init(&wqe->lock);
1093 INIT_WQ_LIST(&wqe->work_list);
1094 INIT_HLIST_NULLS_HEAD(&wqe->free_list, 0);
1095 INIT_LIST_HEAD(&wqe->all_list);
1098 init_completion(&wq->done);
1100 wq->manager = kthread_create(io_wq_manager, wq, "io_wq_manager");
1101 if (!IS_ERR(wq->manager)) {
1102 wake_up_process(wq->manager);
1103 wait_for_completion(&wq->done);
1104 if (test_bit(IO_WQ_BIT_ERROR, &wq->state)) {
1108 refcount_set(&wq->use_refs, 1);
1109 reinit_completion(&wq->done);
1113 ret = PTR_ERR(wq->manager);
1114 complete(&wq->done);
1116 cpuhp_state_remove_instance_nocalls(io_wq_online, &wq->cpuhp_node);
1118 kfree(wq->wqes[node]);
1123 return ERR_PTR(ret);
1126 bool io_wq_get(struct io_wq *wq, struct io_wq_data *data)
1128 if (data->free_work != wq->free_work || data->do_work != wq->do_work)
1131 return refcount_inc_not_zero(&wq->use_refs);
1134 static void __io_wq_destroy(struct io_wq *wq)
1138 cpuhp_state_remove_instance_nocalls(io_wq_online, &wq->cpuhp_node);
1140 set_bit(IO_WQ_BIT_EXIT, &wq->state);
1142 kthread_stop(wq->manager);
1146 io_wq_for_each_worker(wq->wqes[node], io_wq_worker_wake, NULL);
1149 wait_for_completion(&wq->done);
1152 kfree(wq->wqes[node]);
1157 void io_wq_destroy(struct io_wq *wq)
1159 if (refcount_dec_and_test(&wq->use_refs))
1160 __io_wq_destroy(wq);
1163 struct task_struct *io_wq_get_task(struct io_wq *wq)
1168 static bool io_wq_worker_affinity(struct io_worker *worker, void *data)
1170 struct task_struct *task = worker->task;
1174 rq = task_rq_lock(task, &rf);
1175 do_set_cpus_allowed(task, cpumask_of_node(worker->wqe->node));
1176 task->flags |= PF_NO_SETAFFINITY;
1177 task_rq_unlock(rq, task, &rf);
1181 static int io_wq_cpu_online(unsigned int cpu, struct hlist_node *node)
1183 struct io_wq *wq = hlist_entry_safe(node, struct io_wq, cpuhp_node);
1188 io_wq_for_each_worker(wq->wqes[i], io_wq_worker_affinity, NULL);
1193 static __init int io_wq_init(void)
1197 ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "io-wq/online",
1198 io_wq_cpu_online, NULL);
1204 subsys_initcall(io_wq_init);