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>
25 #define WORKER_IDLE_TIMEOUT (5 * HZ)
28 IO_WORKER_F_UP = 1, /* up and active */
29 IO_WORKER_F_RUNNING = 2, /* account as running */
30 IO_WORKER_F_FREE = 4, /* worker on free list */
31 IO_WORKER_F_FIXED = 8, /* static idle worker */
32 IO_WORKER_F_BOUND = 16, /* is doing bounded work */
36 IO_WQ_BIT_EXIT = 0, /* wq exiting */
37 IO_WQ_BIT_CANCEL = 1, /* cancel work on list */
38 IO_WQ_BIT_ERROR = 2, /* error on setup */
42 IO_WQE_FLAG_STALLED = 1, /* stalled on hash */
46 * One for each thread in a wqe pool
51 struct hlist_nulls_node nulls_node;
52 struct list_head all_list;
53 struct task_struct *task;
56 struct io_wq_work *cur_work;
61 #ifdef CONFIG_BLK_CGROUP
62 struct cgroup_subsys_state *blkcg_css;
64 const struct cred *cur_creds;
65 const struct cred *saved_creds;
66 struct files_struct *restore_files;
67 struct nsproxy *restore_nsproxy;
68 struct fs_struct *restore_fs;
71 #if BITS_PER_LONG == 64
72 #define IO_WQ_HASH_ORDER 6
74 #define IO_WQ_HASH_ORDER 5
77 #define IO_WQ_NR_HASH_BUCKETS (1u << IO_WQ_HASH_ORDER)
91 * Per-node worker thread pool
96 struct io_wq_work_list work_list;
97 unsigned long hash_map;
99 } ____cacheline_aligned_in_smp;
102 struct io_wqe_acct acct[2];
104 struct hlist_nulls_head free_list;
105 struct list_head all_list;
108 struct io_wq_work *hash_tail[IO_WQ_NR_HASH_BUCKETS];
115 struct io_wqe **wqes;
118 free_work_fn *free_work;
119 io_wq_work_fn *do_work;
121 struct task_struct *manager;
122 struct user_struct *user;
124 struct completion done;
129 static bool io_worker_get(struct io_worker *worker)
131 return refcount_inc_not_zero(&worker->ref);
134 static void io_worker_release(struct io_worker *worker)
136 if (refcount_dec_and_test(&worker->ref))
137 wake_up_process(worker->task);
141 * Note: drops the wqe->lock if returning true! The caller must re-acquire
142 * the lock in that case. Some callers need to restart handling if this
143 * happens, so we can't just re-acquire the lock on behalf of the caller.
145 static bool __io_worker_unuse(struct io_wqe *wqe, struct io_worker *worker)
147 bool dropped_lock = false;
149 if (worker->saved_creds) {
150 revert_creds(worker->saved_creds);
151 worker->cur_creds = worker->saved_creds = NULL;
154 if (current->files != worker->restore_files) {
155 __acquire(&wqe->lock);
156 raw_spin_unlock_irq(&wqe->lock);
160 current->files = worker->restore_files;
161 current->nsproxy = worker->restore_nsproxy;
162 task_unlock(current);
165 if (current->fs != worker->restore_fs)
166 current->fs = worker->restore_fs;
169 * If we have an active mm, we need to drop the wq lock before unusing
170 * it. If we do, return true and let the caller retry the idle loop.
174 __acquire(&wqe->lock);
175 raw_spin_unlock_irq(&wqe->lock);
178 __set_current_state(TASK_RUNNING);
179 kthread_unuse_mm(worker->mm);
184 #ifdef CONFIG_BLK_CGROUP
185 if (worker->blkcg_css) {
186 kthread_associate_blkcg(NULL);
187 worker->blkcg_css = NULL;
194 static inline struct io_wqe_acct *io_work_get_acct(struct io_wqe *wqe,
195 struct io_wq_work *work)
197 if (work->flags & IO_WQ_WORK_UNBOUND)
198 return &wqe->acct[IO_WQ_ACCT_UNBOUND];
200 return &wqe->acct[IO_WQ_ACCT_BOUND];
203 static inline struct io_wqe_acct *io_wqe_get_acct(struct io_wqe *wqe,
204 struct io_worker *worker)
206 if (worker->flags & IO_WORKER_F_BOUND)
207 return &wqe->acct[IO_WQ_ACCT_BOUND];
209 return &wqe->acct[IO_WQ_ACCT_UNBOUND];
212 static void io_worker_exit(struct io_worker *worker)
214 struct io_wqe *wqe = worker->wqe;
215 struct io_wqe_acct *acct = io_wqe_get_acct(wqe, worker);
218 * If we're not at zero, someone else is holding a brief reference
219 * to the worker. Wait for that to go away.
221 set_current_state(TASK_INTERRUPTIBLE);
222 if (!refcount_dec_and_test(&worker->ref))
224 __set_current_state(TASK_RUNNING);
227 current->flags &= ~PF_IO_WORKER;
228 if (worker->flags & IO_WORKER_F_RUNNING)
229 atomic_dec(&acct->nr_running);
230 if (!(worker->flags & IO_WORKER_F_BOUND))
231 atomic_dec(&wqe->wq->user->processes);
235 raw_spin_lock_irq(&wqe->lock);
236 hlist_nulls_del_rcu(&worker->nulls_node);
237 list_del_rcu(&worker->all_list);
238 if (__io_worker_unuse(wqe, worker)) {
239 __release(&wqe->lock);
240 raw_spin_lock_irq(&wqe->lock);
243 raw_spin_unlock_irq(&wqe->lock);
245 kfree_rcu(worker, rcu);
246 if (refcount_dec_and_test(&wqe->wq->refs))
247 complete(&wqe->wq->done);
250 static inline bool io_wqe_run_queue(struct io_wqe *wqe)
251 __must_hold(wqe->lock)
253 if (!wq_list_empty(&wqe->work_list) &&
254 !(wqe->flags & IO_WQE_FLAG_STALLED))
260 * Check head of free list for an available worker. If one isn't available,
261 * caller must wake up the wq manager to create one.
263 static bool io_wqe_activate_free_worker(struct io_wqe *wqe)
266 struct hlist_nulls_node *n;
267 struct io_worker *worker;
269 n = rcu_dereference(hlist_nulls_first_rcu(&wqe->free_list));
273 worker = hlist_nulls_entry(n, struct io_worker, nulls_node);
274 if (io_worker_get(worker)) {
275 wake_up_process(worker->task);
276 io_worker_release(worker);
284 * We need a worker. If we find a free one, we're good. If not, and we're
285 * below the max number of workers, wake up the manager to create one.
287 static void io_wqe_wake_worker(struct io_wqe *wqe, struct io_wqe_acct *acct)
292 * Most likely an attempt to queue unbounded work on an io_wq that
293 * wasn't setup with any unbounded workers.
295 WARN_ON_ONCE(!acct->max_workers);
298 ret = io_wqe_activate_free_worker(wqe);
301 if (!ret && acct->nr_workers < acct->max_workers)
302 wake_up_process(wqe->wq->manager);
305 static void io_wqe_inc_running(struct io_wqe *wqe, struct io_worker *worker)
307 struct io_wqe_acct *acct = io_wqe_get_acct(wqe, worker);
309 atomic_inc(&acct->nr_running);
312 static void io_wqe_dec_running(struct io_wqe *wqe, struct io_worker *worker)
313 __must_hold(wqe->lock)
315 struct io_wqe_acct *acct = io_wqe_get_acct(wqe, worker);
317 if (atomic_dec_and_test(&acct->nr_running) && io_wqe_run_queue(wqe))
318 io_wqe_wake_worker(wqe, acct);
321 static void io_worker_start(struct io_wqe *wqe, struct io_worker *worker)
323 allow_kernel_signal(SIGINT);
325 current->flags |= PF_IO_WORKER;
327 worker->flags |= (IO_WORKER_F_UP | IO_WORKER_F_RUNNING);
328 worker->restore_files = current->files;
329 worker->restore_nsproxy = current->nsproxy;
330 worker->restore_fs = current->fs;
331 io_wqe_inc_running(wqe, worker);
335 * Worker will start processing some work. Move it to the busy list, if
336 * it's currently on the freelist
338 static void __io_worker_busy(struct io_wqe *wqe, struct io_worker *worker,
339 struct io_wq_work *work)
340 __must_hold(wqe->lock)
342 bool worker_bound, work_bound;
344 if (worker->flags & IO_WORKER_F_FREE) {
345 worker->flags &= ~IO_WORKER_F_FREE;
346 hlist_nulls_del_init_rcu(&worker->nulls_node);
350 * If worker is moving from bound to unbound (or vice versa), then
351 * ensure we update the running accounting.
353 worker_bound = (worker->flags & IO_WORKER_F_BOUND) != 0;
354 work_bound = (work->flags & IO_WQ_WORK_UNBOUND) == 0;
355 if (worker_bound != work_bound) {
356 io_wqe_dec_running(wqe, worker);
358 worker->flags |= IO_WORKER_F_BOUND;
359 wqe->acct[IO_WQ_ACCT_UNBOUND].nr_workers--;
360 wqe->acct[IO_WQ_ACCT_BOUND].nr_workers++;
361 atomic_dec(&wqe->wq->user->processes);
363 worker->flags &= ~IO_WORKER_F_BOUND;
364 wqe->acct[IO_WQ_ACCT_UNBOUND].nr_workers++;
365 wqe->acct[IO_WQ_ACCT_BOUND].nr_workers--;
366 atomic_inc(&wqe->wq->user->processes);
368 io_wqe_inc_running(wqe, worker);
373 * No work, worker going to sleep. Move to freelist, and unuse mm if we
374 * have one attached. Dropping the mm may potentially sleep, so we drop
375 * the lock in that case and return success. Since the caller has to
376 * retry the loop in that case (we changed task state), we don't regrab
377 * the lock if we return success.
379 static bool __io_worker_idle(struct io_wqe *wqe, struct io_worker *worker)
380 __must_hold(wqe->lock)
382 if (!(worker->flags & IO_WORKER_F_FREE)) {
383 worker->flags |= IO_WORKER_F_FREE;
384 hlist_nulls_add_head_rcu(&worker->nulls_node, &wqe->free_list);
387 return __io_worker_unuse(wqe, worker);
390 static inline unsigned int io_get_work_hash(struct io_wq_work *work)
392 return work->flags >> IO_WQ_HASH_SHIFT;
395 static struct io_wq_work *io_get_next_work(struct io_wqe *wqe)
396 __must_hold(wqe->lock)
398 struct io_wq_work_node *node, *prev;
399 struct io_wq_work *work, *tail;
402 wq_list_for_each(node, prev, &wqe->work_list) {
403 work = container_of(node, struct io_wq_work, list);
405 /* not hashed, can run anytime */
406 if (!io_wq_is_hashed(work)) {
407 wq_list_del(&wqe->work_list, node, prev);
411 /* hashed, can run if not already running */
412 hash = io_get_work_hash(work);
413 if (!(wqe->hash_map & BIT(hash))) {
414 wqe->hash_map |= BIT(hash);
415 /* all items with this hash lie in [work, tail] */
416 tail = wqe->hash_tail[hash];
417 wqe->hash_tail[hash] = NULL;
418 wq_list_cut(&wqe->work_list, &tail->list, prev);
426 static void io_wq_switch_mm(struct io_worker *worker, struct io_wq_work *work)
429 kthread_unuse_mm(worker->mm);
434 if (mmget_not_zero(work->identity->mm)) {
435 kthread_use_mm(work->identity->mm);
436 worker->mm = work->identity->mm;
440 /* failed grabbing mm, ensure work gets cancelled */
441 work->flags |= IO_WQ_WORK_CANCEL;
444 static inline void io_wq_switch_blkcg(struct io_worker *worker,
445 struct io_wq_work *work)
447 #ifdef CONFIG_BLK_CGROUP
448 if (!(work->flags & IO_WQ_WORK_BLKCG))
450 if (work->identity->blkcg_css != worker->blkcg_css) {
451 kthread_associate_blkcg(work->identity->blkcg_css);
452 worker->blkcg_css = work->identity->blkcg_css;
457 static void io_wq_switch_creds(struct io_worker *worker,
458 struct io_wq_work *work)
460 const struct cred *old_creds = override_creds(work->identity->creds);
462 worker->cur_creds = work->identity->creds;
463 if (worker->saved_creds)
464 put_cred(old_creds); /* creds set by previous switch */
466 worker->saved_creds = old_creds;
469 static void io_impersonate_work(struct io_worker *worker,
470 struct io_wq_work *work)
472 if ((work->flags & IO_WQ_WORK_FILES) &&
473 current->files != work->identity->files) {
475 current->files = work->identity->files;
476 current->nsproxy = work->identity->nsproxy;
477 task_unlock(current);
479 if ((work->flags & IO_WQ_WORK_FS) && current->fs != work->identity->fs)
480 current->fs = work->identity->fs;
481 if ((work->flags & IO_WQ_WORK_MM) && work->identity->mm != worker->mm)
482 io_wq_switch_mm(worker, work);
483 if ((work->flags & IO_WQ_WORK_CREDS) &&
484 worker->cur_creds != work->identity->creds)
485 io_wq_switch_creds(worker, work);
486 current->signal->rlim[RLIMIT_FSIZE].rlim_cur = work->identity->fsize;
487 io_wq_switch_blkcg(worker, work);
489 current->loginuid = work->identity->loginuid;
490 current->sessionid = work->identity->sessionid;
494 static void io_assign_current_work(struct io_worker *worker,
495 struct io_wq_work *work)
498 /* flush pending signals before assigning new work */
499 if (signal_pending(current))
500 flush_signals(current);
505 current->loginuid = KUIDT_INIT(AUDIT_UID_UNSET);
506 current->sessionid = AUDIT_SID_UNSET;
509 spin_lock_irq(&worker->lock);
510 worker->cur_work = work;
511 spin_unlock_irq(&worker->lock);
514 static void io_wqe_enqueue(struct io_wqe *wqe, struct io_wq_work *work);
516 static void io_worker_handle_work(struct io_worker *worker)
517 __releases(wqe->lock)
519 struct io_wqe *wqe = worker->wqe;
520 struct io_wq *wq = wqe->wq;
523 struct io_wq_work *work;
526 * If we got some work, mark us as busy. If we didn't, but
527 * the list isn't empty, it means we stalled on hashed work.
528 * Mark us stalled so we don't keep looking for work when we
529 * can't make progress, any work completion or insertion will
530 * clear the stalled flag.
532 work = io_get_next_work(wqe);
534 __io_worker_busy(wqe, worker, work);
535 else if (!wq_list_empty(&wqe->work_list))
536 wqe->flags |= IO_WQE_FLAG_STALLED;
538 raw_spin_unlock_irq(&wqe->lock);
541 io_assign_current_work(worker, work);
543 /* handle a whole dependent link */
545 struct io_wq_work *old_work, *next_hashed, *linked;
546 unsigned int hash = io_get_work_hash(work);
548 next_hashed = wq_next_work(work);
549 io_impersonate_work(worker, work);
551 * OK to set IO_WQ_WORK_CANCEL even for uncancellable
552 * work, the worker function will do the right thing.
554 if (test_bit(IO_WQ_BIT_CANCEL, &wq->state))
555 work->flags |= IO_WQ_WORK_CANCEL;
558 linked = wq->do_work(work);
561 if (!work && linked && !io_wq_is_hashed(linked)) {
565 io_assign_current_work(worker, work);
566 wq->free_work(old_work);
569 io_wqe_enqueue(wqe, linked);
571 if (hash != -1U && !next_hashed) {
572 raw_spin_lock_irq(&wqe->lock);
573 wqe->hash_map &= ~BIT_ULL(hash);
574 wqe->flags &= ~IO_WQE_FLAG_STALLED;
575 /* skip unnecessary unlock-lock wqe->lock */
578 raw_spin_unlock_irq(&wqe->lock);
582 raw_spin_lock_irq(&wqe->lock);
586 static int io_wqe_worker(void *data)
588 struct io_worker *worker = data;
589 struct io_wqe *wqe = worker->wqe;
590 struct io_wq *wq = wqe->wq;
592 io_worker_start(wqe, worker);
594 while (!test_bit(IO_WQ_BIT_EXIT, &wq->state)) {
595 set_current_state(TASK_INTERRUPTIBLE);
597 raw_spin_lock_irq(&wqe->lock);
598 if (io_wqe_run_queue(wqe)) {
599 __set_current_state(TASK_RUNNING);
600 io_worker_handle_work(worker);
603 /* drops the lock on success, retry */
604 if (__io_worker_idle(wqe, worker)) {
605 __release(&wqe->lock);
608 raw_spin_unlock_irq(&wqe->lock);
609 if (signal_pending(current))
610 flush_signals(current);
611 if (schedule_timeout(WORKER_IDLE_TIMEOUT))
613 /* timed out, exit unless we're the fixed worker */
614 if (test_bit(IO_WQ_BIT_EXIT, &wq->state) ||
615 !(worker->flags & IO_WORKER_F_FIXED))
619 if (test_bit(IO_WQ_BIT_EXIT, &wq->state)) {
620 raw_spin_lock_irq(&wqe->lock);
621 if (!wq_list_empty(&wqe->work_list))
622 io_worker_handle_work(worker);
624 raw_spin_unlock_irq(&wqe->lock);
627 io_worker_exit(worker);
632 * Called when a worker is scheduled in. Mark us as currently running.
634 void io_wq_worker_running(struct task_struct *tsk)
636 struct io_worker *worker = kthread_data(tsk);
637 struct io_wqe *wqe = worker->wqe;
639 if (!(worker->flags & IO_WORKER_F_UP))
641 if (worker->flags & IO_WORKER_F_RUNNING)
643 worker->flags |= IO_WORKER_F_RUNNING;
644 io_wqe_inc_running(wqe, worker);
648 * Called when worker is going to sleep. If there are no workers currently
649 * running and we have work pending, wake up a free one or have the manager
652 void io_wq_worker_sleeping(struct task_struct *tsk)
654 struct io_worker *worker = kthread_data(tsk);
655 struct io_wqe *wqe = worker->wqe;
657 if (!(worker->flags & IO_WORKER_F_UP))
659 if (!(worker->flags & IO_WORKER_F_RUNNING))
662 worker->flags &= ~IO_WORKER_F_RUNNING;
664 raw_spin_lock_irq(&wqe->lock);
665 io_wqe_dec_running(wqe, worker);
666 raw_spin_unlock_irq(&wqe->lock);
669 static bool create_io_worker(struct io_wq *wq, struct io_wqe *wqe, int index)
671 struct io_wqe_acct *acct = &wqe->acct[index];
672 struct io_worker *worker;
674 worker = kzalloc_node(sizeof(*worker), GFP_KERNEL, wqe->node);
678 refcount_set(&worker->ref, 1);
679 worker->nulls_node.pprev = NULL;
681 spin_lock_init(&worker->lock);
683 worker->task = kthread_create_on_node(io_wqe_worker, worker, wqe->node,
684 "io_wqe_worker-%d/%d", index, wqe->node);
685 if (IS_ERR(worker->task)) {
689 kthread_bind_mask(worker->task, cpumask_of_node(wqe->node));
691 raw_spin_lock_irq(&wqe->lock);
692 hlist_nulls_add_head_rcu(&worker->nulls_node, &wqe->free_list);
693 list_add_tail_rcu(&worker->all_list, &wqe->all_list);
694 worker->flags |= IO_WORKER_F_FREE;
695 if (index == IO_WQ_ACCT_BOUND)
696 worker->flags |= IO_WORKER_F_BOUND;
697 if (!acct->nr_workers && (worker->flags & IO_WORKER_F_BOUND))
698 worker->flags |= IO_WORKER_F_FIXED;
700 raw_spin_unlock_irq(&wqe->lock);
702 if (index == IO_WQ_ACCT_UNBOUND)
703 atomic_inc(&wq->user->processes);
705 refcount_inc(&wq->refs);
706 wake_up_process(worker->task);
710 static inline bool io_wqe_need_worker(struct io_wqe *wqe, int index)
711 __must_hold(wqe->lock)
713 struct io_wqe_acct *acct = &wqe->acct[index];
715 /* if we have available workers or no work, no need */
716 if (!hlist_nulls_empty(&wqe->free_list) || !io_wqe_run_queue(wqe))
718 return acct->nr_workers < acct->max_workers;
721 static bool io_wqe_worker_send_sig(struct io_worker *worker, void *data)
723 send_sig(SIGINT, worker->task, 1);
728 * Iterate the passed in list and call the specific function for each
729 * worker that isn't exiting
731 static bool io_wq_for_each_worker(struct io_wqe *wqe,
732 bool (*func)(struct io_worker *, void *),
735 struct io_worker *worker;
738 list_for_each_entry_rcu(worker, &wqe->all_list, all_list) {
739 if (io_worker_get(worker)) {
740 /* no task if node is/was offline */
742 ret = func(worker, data);
743 io_worker_release(worker);
752 static bool io_wq_worker_wake(struct io_worker *worker, void *data)
754 wake_up_process(worker->task);
759 * Manager thread. Tasked with creating new workers, if we need them.
761 static int io_wq_manager(void *data)
763 struct io_wq *wq = data;
766 /* create fixed workers */
767 refcount_set(&wq->refs, 1);
768 for_each_node(node) {
769 if (!node_online(node))
771 if (create_io_worker(wq, wq->wqes[node], IO_WQ_ACCT_BOUND))
773 set_bit(IO_WQ_BIT_ERROR, &wq->state);
774 set_bit(IO_WQ_BIT_EXIT, &wq->state);
780 while (!kthread_should_stop()) {
781 if (current->task_works)
784 for_each_node(node) {
785 struct io_wqe *wqe = wq->wqes[node];
786 bool fork_worker[2] = { false, false };
788 if (!node_online(node))
791 raw_spin_lock_irq(&wqe->lock);
792 if (io_wqe_need_worker(wqe, IO_WQ_ACCT_BOUND))
793 fork_worker[IO_WQ_ACCT_BOUND] = true;
794 if (io_wqe_need_worker(wqe, IO_WQ_ACCT_UNBOUND))
795 fork_worker[IO_WQ_ACCT_UNBOUND] = true;
796 raw_spin_unlock_irq(&wqe->lock);
797 if (fork_worker[IO_WQ_ACCT_BOUND])
798 create_io_worker(wq, wqe, IO_WQ_ACCT_BOUND);
799 if (fork_worker[IO_WQ_ACCT_UNBOUND])
800 create_io_worker(wq, wqe, IO_WQ_ACCT_UNBOUND);
802 set_current_state(TASK_INTERRUPTIBLE);
803 schedule_timeout(HZ);
806 if (current->task_works)
810 if (refcount_dec_and_test(&wq->refs)) {
814 /* if ERROR is set and we get here, we have workers to wake */
815 if (test_bit(IO_WQ_BIT_ERROR, &wq->state)) {
818 io_wq_for_each_worker(wq->wqes[node], io_wq_worker_wake, NULL);
824 static bool io_wq_can_queue(struct io_wqe *wqe, struct io_wqe_acct *acct,
825 struct io_wq_work *work)
829 if (!(work->flags & IO_WQ_WORK_UNBOUND))
831 if (atomic_read(&acct->nr_running))
835 free_worker = !hlist_nulls_empty(&wqe->free_list);
840 if (atomic_read(&wqe->wq->user->processes) >= acct->max_workers &&
841 !(capable(CAP_SYS_RESOURCE) || capable(CAP_SYS_ADMIN)))
847 static void io_run_cancel(struct io_wq_work *work, struct io_wqe *wqe)
849 struct io_wq *wq = wqe->wq;
852 struct io_wq_work *old_work = work;
854 work->flags |= IO_WQ_WORK_CANCEL;
855 work = wq->do_work(work);
856 wq->free_work(old_work);
860 static void io_wqe_insert_work(struct io_wqe *wqe, struct io_wq_work *work)
863 struct io_wq_work *tail;
865 if (!io_wq_is_hashed(work)) {
867 wq_list_add_tail(&work->list, &wqe->work_list);
871 hash = io_get_work_hash(work);
872 tail = wqe->hash_tail[hash];
873 wqe->hash_tail[hash] = work;
877 wq_list_add_after(&work->list, &tail->list, &wqe->work_list);
880 static void io_wqe_enqueue(struct io_wqe *wqe, struct io_wq_work *work)
882 struct io_wqe_acct *acct = io_work_get_acct(wqe, work);
887 * Do early check to see if we need a new unbound worker, and if we do,
888 * if we're allowed to do so. This isn't 100% accurate as there's a
889 * gap between this check and incrementing the value, but that's OK.
890 * It's close enough to not be an issue, fork() has the same delay.
892 if (unlikely(!io_wq_can_queue(wqe, acct, work))) {
893 io_run_cancel(work, wqe);
897 work_flags = work->flags;
898 raw_spin_lock_irqsave(&wqe->lock, flags);
899 io_wqe_insert_work(wqe, work);
900 wqe->flags &= ~IO_WQE_FLAG_STALLED;
901 raw_spin_unlock_irqrestore(&wqe->lock, flags);
903 if ((work_flags & IO_WQ_WORK_CONCURRENT) ||
904 !atomic_read(&acct->nr_running))
905 io_wqe_wake_worker(wqe, acct);
908 void io_wq_enqueue(struct io_wq *wq, struct io_wq_work *work)
910 struct io_wqe *wqe = wq->wqes[numa_node_id()];
912 io_wqe_enqueue(wqe, work);
916 * Work items that hash to the same value will not be done in parallel.
917 * Used to limit concurrent writes, generally hashed by inode.
919 void io_wq_hash_work(struct io_wq_work *work, void *val)
923 bit = hash_ptr(val, IO_WQ_HASH_ORDER);
924 work->flags |= (IO_WQ_WORK_HASHED | (bit << IO_WQ_HASH_SHIFT));
927 void io_wq_cancel_all(struct io_wq *wq)
931 set_bit(IO_WQ_BIT_CANCEL, &wq->state);
934 for_each_node(node) {
935 struct io_wqe *wqe = wq->wqes[node];
937 io_wq_for_each_worker(wqe, io_wqe_worker_send_sig, NULL);
942 struct io_cb_cancel_data {
950 static bool io_wq_worker_cancel(struct io_worker *worker, void *data)
952 struct io_cb_cancel_data *match = data;
956 * Hold the lock to avoid ->cur_work going out of scope, caller
957 * may dereference the passed in work.
959 spin_lock_irqsave(&worker->lock, flags);
960 if (worker->cur_work &&
961 !(worker->cur_work->flags & IO_WQ_WORK_NO_CANCEL) &&
962 match->fn(worker->cur_work, match->data)) {
963 send_sig(SIGINT, worker->task, 1);
966 spin_unlock_irqrestore(&worker->lock, flags);
968 return match->nr_running && !match->cancel_all;
971 static inline void io_wqe_remove_pending(struct io_wqe *wqe,
972 struct io_wq_work *work,
973 struct io_wq_work_node *prev)
975 unsigned int hash = io_get_work_hash(work);
976 struct io_wq_work *prev_work = NULL;
978 if (io_wq_is_hashed(work) && work == wqe->hash_tail[hash]) {
980 prev_work = container_of(prev, struct io_wq_work, list);
981 if (prev_work && io_get_work_hash(prev_work) == hash)
982 wqe->hash_tail[hash] = prev_work;
984 wqe->hash_tail[hash] = NULL;
986 wq_list_del(&wqe->work_list, &work->list, prev);
989 static void io_wqe_cancel_pending_work(struct io_wqe *wqe,
990 struct io_cb_cancel_data *match)
992 struct io_wq_work_node *node, *prev;
993 struct io_wq_work *work;
997 raw_spin_lock_irqsave(&wqe->lock, flags);
998 wq_list_for_each(node, prev, &wqe->work_list) {
999 work = container_of(node, struct io_wq_work, list);
1000 if (!match->fn(work, match->data))
1002 io_wqe_remove_pending(wqe, work, prev);
1003 raw_spin_unlock_irqrestore(&wqe->lock, flags);
1004 io_run_cancel(work, wqe);
1005 match->nr_pending++;
1006 if (!match->cancel_all)
1009 /* not safe to continue after unlock */
1012 raw_spin_unlock_irqrestore(&wqe->lock, flags);
1015 static void io_wqe_cancel_running_work(struct io_wqe *wqe,
1016 struct io_cb_cancel_data *match)
1019 io_wq_for_each_worker(wqe, io_wq_worker_cancel, match);
1023 enum io_wq_cancel io_wq_cancel_cb(struct io_wq *wq, work_cancel_fn *cancel,
1024 void *data, bool cancel_all)
1026 struct io_cb_cancel_data match = {
1029 .cancel_all = cancel_all,
1034 * First check pending list, if we're lucky we can just remove it
1035 * from there. CANCEL_OK means that the work is returned as-new,
1036 * no completion will be posted for it.
1038 for_each_node(node) {
1039 struct io_wqe *wqe = wq->wqes[node];
1041 io_wqe_cancel_pending_work(wqe, &match);
1042 if (match.nr_pending && !match.cancel_all)
1043 return IO_WQ_CANCEL_OK;
1047 * Now check if a free (going busy) or busy worker has the work
1048 * currently running. If we find it there, we'll return CANCEL_RUNNING
1049 * as an indication that we attempt to signal cancellation. The
1050 * completion will run normally in this case.
1052 for_each_node(node) {
1053 struct io_wqe *wqe = wq->wqes[node];
1055 io_wqe_cancel_running_work(wqe, &match);
1056 if (match.nr_running && !match.cancel_all)
1057 return IO_WQ_CANCEL_RUNNING;
1060 if (match.nr_running)
1061 return IO_WQ_CANCEL_RUNNING;
1062 if (match.nr_pending)
1063 return IO_WQ_CANCEL_OK;
1064 return IO_WQ_CANCEL_NOTFOUND;
1067 static bool io_wq_io_cb_cancel_data(struct io_wq_work *work, void *data)
1069 return work == data;
1072 enum io_wq_cancel io_wq_cancel_work(struct io_wq *wq, struct io_wq_work *cwork)
1074 return io_wq_cancel_cb(wq, io_wq_io_cb_cancel_data, (void *)cwork, false);
1077 struct io_wq *io_wq_create(unsigned bounded, struct io_wq_data *data)
1079 int ret = -ENOMEM, node;
1082 if (WARN_ON_ONCE(!data->free_work || !data->do_work))
1083 return ERR_PTR(-EINVAL);
1085 wq = kzalloc(sizeof(*wq), GFP_KERNEL);
1087 return ERR_PTR(-ENOMEM);
1089 wq->wqes = kcalloc(nr_node_ids, sizeof(struct io_wqe *), GFP_KERNEL);
1092 return ERR_PTR(-ENOMEM);
1095 wq->free_work = data->free_work;
1096 wq->do_work = data->do_work;
1098 /* caller must already hold a reference to this */
1099 wq->user = data->user;
1101 for_each_node(node) {
1103 int alloc_node = node;
1105 if (!node_online(alloc_node))
1106 alloc_node = NUMA_NO_NODE;
1107 wqe = kzalloc_node(sizeof(struct io_wqe), GFP_KERNEL, alloc_node);
1110 wq->wqes[node] = wqe;
1111 wqe->node = alloc_node;
1112 wqe->acct[IO_WQ_ACCT_BOUND].max_workers = bounded;
1113 atomic_set(&wqe->acct[IO_WQ_ACCT_BOUND].nr_running, 0);
1115 wqe->acct[IO_WQ_ACCT_UNBOUND].max_workers =
1116 task_rlimit(current, RLIMIT_NPROC);
1118 atomic_set(&wqe->acct[IO_WQ_ACCT_UNBOUND].nr_running, 0);
1120 raw_spin_lock_init(&wqe->lock);
1121 INIT_WQ_LIST(&wqe->work_list);
1122 INIT_HLIST_NULLS_HEAD(&wqe->free_list, 0);
1123 INIT_LIST_HEAD(&wqe->all_list);
1126 init_completion(&wq->done);
1128 wq->manager = kthread_create(io_wq_manager, wq, "io_wq_manager");
1129 if (!IS_ERR(wq->manager)) {
1130 wake_up_process(wq->manager);
1131 wait_for_completion(&wq->done);
1132 if (test_bit(IO_WQ_BIT_ERROR, &wq->state)) {
1136 refcount_set(&wq->use_refs, 1);
1137 reinit_completion(&wq->done);
1141 ret = PTR_ERR(wq->manager);
1142 complete(&wq->done);
1145 kfree(wq->wqes[node]);
1148 return ERR_PTR(ret);
1151 bool io_wq_get(struct io_wq *wq, struct io_wq_data *data)
1153 if (data->free_work != wq->free_work || data->do_work != wq->do_work)
1156 return refcount_inc_not_zero(&wq->use_refs);
1159 static void __io_wq_destroy(struct io_wq *wq)
1163 set_bit(IO_WQ_BIT_EXIT, &wq->state);
1165 kthread_stop(wq->manager);
1169 io_wq_for_each_worker(wq->wqes[node], io_wq_worker_wake, NULL);
1172 wait_for_completion(&wq->done);
1175 kfree(wq->wqes[node]);
1180 void io_wq_destroy(struct io_wq *wq)
1182 if (refcount_dec_and_test(&wq->use_refs))
1183 __io_wq_destroy(wq);
1186 struct task_struct *io_wq_get_task(struct io_wq *wq)