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/mmu_context.h>
14 #include <linux/sched/mm.h>
15 #include <linux/percpu.h>
16 #include <linux/slab.h>
17 #include <linux/kthread.h>
18 #include <linux/rculist_nulls.h>
22 #define WORKER_IDLE_TIMEOUT (5 * HZ)
25 IO_WORKER_F_UP = 1, /* up and active */
26 IO_WORKER_F_RUNNING = 2, /* account as running */
27 IO_WORKER_F_FREE = 4, /* worker on free list */
28 IO_WORKER_F_EXITING = 8, /* worker exiting */
29 IO_WORKER_F_FIXED = 16, /* static idle worker */
30 IO_WORKER_F_BOUND = 32, /* is doing bounded work */
34 IO_WQ_BIT_EXIT = 0, /* wq exiting */
35 IO_WQ_BIT_CANCEL = 1, /* cancel work on list */
36 IO_WQ_BIT_ERROR = 2, /* error on setup */
40 IO_WQE_FLAG_STALLED = 1, /* stalled on hash */
44 * One for each thread in a wqe pool
49 struct hlist_nulls_node nulls_node;
50 struct list_head all_list;
51 struct task_struct *task;
52 wait_queue_head_t wait;
55 struct io_wq_work *cur_work;
60 const struct cred *creds;
61 struct files_struct *restore_files;
64 #if BITS_PER_LONG == 64
65 #define IO_WQ_HASH_ORDER 6
67 #define IO_WQ_HASH_ORDER 5
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 hlist_nulls_head busy_list;
97 struct list_head all_list;
106 struct io_wqe **wqes;
109 get_work_fn *get_work;
110 put_work_fn *put_work;
112 struct task_struct *manager;
113 struct user_struct *user;
114 const struct cred *creds;
115 struct mm_struct *mm;
117 struct completion done;
120 static bool io_worker_get(struct io_worker *worker)
122 return refcount_inc_not_zero(&worker->ref);
125 static void io_worker_release(struct io_worker *worker)
127 if (refcount_dec_and_test(&worker->ref))
128 wake_up_process(worker->task);
132 * Note: drops the wqe->lock if returning true! The caller must re-acquire
133 * the lock in that case. Some callers need to restart handling if this
134 * happens, so we can't just re-acquire the lock on behalf of the caller.
136 static bool __io_worker_unuse(struct io_wqe *wqe, struct io_worker *worker)
138 bool dropped_lock = false;
141 revert_creds(worker->creds);
142 worker->creds = NULL;
145 if (current->files != worker->restore_files) {
146 __acquire(&wqe->lock);
147 spin_unlock_irq(&wqe->lock);
151 current->files = worker->restore_files;
152 task_unlock(current);
156 * If we have an active mm, we need to drop the wq lock before unusing
157 * it. If we do, return true and let the caller retry the idle loop.
161 __acquire(&wqe->lock);
162 spin_unlock_irq(&wqe->lock);
165 __set_current_state(TASK_RUNNING);
167 unuse_mm(worker->mm);
175 static inline struct io_wqe_acct *io_work_get_acct(struct io_wqe *wqe,
176 struct io_wq_work *work)
178 if (work->flags & IO_WQ_WORK_UNBOUND)
179 return &wqe->acct[IO_WQ_ACCT_UNBOUND];
181 return &wqe->acct[IO_WQ_ACCT_BOUND];
184 static inline struct io_wqe_acct *io_wqe_get_acct(struct io_wqe *wqe,
185 struct io_worker *worker)
187 if (worker->flags & IO_WORKER_F_BOUND)
188 return &wqe->acct[IO_WQ_ACCT_BOUND];
190 return &wqe->acct[IO_WQ_ACCT_UNBOUND];
193 static void io_worker_exit(struct io_worker *worker)
195 struct io_wqe *wqe = worker->wqe;
196 struct io_wqe_acct *acct = io_wqe_get_acct(wqe, worker);
200 * If we're not at zero, someone else is holding a brief reference
201 * to the worker. Wait for that to go away.
203 set_current_state(TASK_INTERRUPTIBLE);
204 if (!refcount_dec_and_test(&worker->ref))
206 __set_current_state(TASK_RUNNING);
209 current->flags &= ~PF_IO_WORKER;
210 if (worker->flags & IO_WORKER_F_RUNNING)
211 atomic_dec(&acct->nr_running);
212 if (!(worker->flags & IO_WORKER_F_BOUND))
213 atomic_dec(&wqe->wq->user->processes);
217 spin_lock_irq(&wqe->lock);
218 hlist_nulls_del_rcu(&worker->nulls_node);
219 list_del_rcu(&worker->all_list);
220 if (__io_worker_unuse(wqe, worker)) {
221 __release(&wqe->lock);
222 spin_lock_irq(&wqe->lock);
225 nr_workers = wqe->acct[IO_WQ_ACCT_BOUND].nr_workers +
226 wqe->acct[IO_WQ_ACCT_UNBOUND].nr_workers;
227 spin_unlock_irq(&wqe->lock);
229 /* all workers gone, wq exit can proceed */
230 if (!nr_workers && refcount_dec_and_test(&wqe->wq->refs))
231 complete(&wqe->wq->done);
233 kfree_rcu(worker, rcu);
236 static inline bool io_wqe_run_queue(struct io_wqe *wqe)
237 __must_hold(wqe->lock)
239 if (!wq_list_empty(&wqe->work_list) &&
240 !(wqe->flags & IO_WQE_FLAG_STALLED))
246 * Check head of free list for an available worker. If one isn't available,
247 * caller must wake up the wq manager to create one.
249 static bool io_wqe_activate_free_worker(struct io_wqe *wqe)
252 struct hlist_nulls_node *n;
253 struct io_worker *worker;
255 n = rcu_dereference(hlist_nulls_first_rcu(&wqe->free_list));
259 worker = hlist_nulls_entry(n, struct io_worker, nulls_node);
260 if (io_worker_get(worker)) {
261 wake_up(&worker->wait);
262 io_worker_release(worker);
270 * We need a worker. If we find a free one, we're good. If not, and we're
271 * below the max number of workers, wake up the manager to create one.
273 static void io_wqe_wake_worker(struct io_wqe *wqe, struct io_wqe_acct *acct)
278 * Most likely an attempt to queue unbounded work on an io_wq that
279 * wasn't setup with any unbounded workers.
281 WARN_ON_ONCE(!acct->max_workers);
284 ret = io_wqe_activate_free_worker(wqe);
287 if (!ret && acct->nr_workers < acct->max_workers)
288 wake_up_process(wqe->wq->manager);
291 static void io_wqe_inc_running(struct io_wqe *wqe, struct io_worker *worker)
293 struct io_wqe_acct *acct = io_wqe_get_acct(wqe, worker);
295 atomic_inc(&acct->nr_running);
298 static void io_wqe_dec_running(struct io_wqe *wqe, struct io_worker *worker)
299 __must_hold(wqe->lock)
301 struct io_wqe_acct *acct = io_wqe_get_acct(wqe, worker);
303 if (atomic_dec_and_test(&acct->nr_running) && io_wqe_run_queue(wqe))
304 io_wqe_wake_worker(wqe, acct);
307 static void io_worker_start(struct io_wqe *wqe, struct io_worker *worker)
309 allow_kernel_signal(SIGINT);
311 current->flags |= PF_IO_WORKER;
313 worker->flags |= (IO_WORKER_F_UP | IO_WORKER_F_RUNNING);
314 worker->restore_files = current->files;
315 io_wqe_inc_running(wqe, worker);
319 * Worker will start processing some work. Move it to the busy list, if
320 * it's currently on the freelist
322 static void __io_worker_busy(struct io_wqe *wqe, struct io_worker *worker,
323 struct io_wq_work *work)
324 __must_hold(wqe->lock)
326 bool worker_bound, work_bound;
328 if (worker->flags & IO_WORKER_F_FREE) {
329 worker->flags &= ~IO_WORKER_F_FREE;
330 hlist_nulls_del_init_rcu(&worker->nulls_node);
331 hlist_nulls_add_head_rcu(&worker->nulls_node, &wqe->busy_list);
335 * If worker is moving from bound to unbound (or vice versa), then
336 * ensure we update the running accounting.
338 worker_bound = (worker->flags & IO_WORKER_F_BOUND) != 0;
339 work_bound = (work->flags & IO_WQ_WORK_UNBOUND) == 0;
340 if (worker_bound != work_bound) {
341 io_wqe_dec_running(wqe, worker);
343 worker->flags |= IO_WORKER_F_BOUND;
344 wqe->acct[IO_WQ_ACCT_UNBOUND].nr_workers--;
345 wqe->acct[IO_WQ_ACCT_BOUND].nr_workers++;
346 atomic_dec(&wqe->wq->user->processes);
348 worker->flags &= ~IO_WORKER_F_BOUND;
349 wqe->acct[IO_WQ_ACCT_UNBOUND].nr_workers++;
350 wqe->acct[IO_WQ_ACCT_BOUND].nr_workers--;
351 atomic_inc(&wqe->wq->user->processes);
353 io_wqe_inc_running(wqe, worker);
358 * No work, worker going to sleep. Move to freelist, and unuse mm if we
359 * have one attached. Dropping the mm may potentially sleep, so we drop
360 * the lock in that case and return success. Since the caller has to
361 * retry the loop in that case (we changed task state), we don't regrab
362 * the lock if we return success.
364 static bool __io_worker_idle(struct io_wqe *wqe, struct io_worker *worker)
365 __must_hold(wqe->lock)
367 if (!(worker->flags & IO_WORKER_F_FREE)) {
368 worker->flags |= IO_WORKER_F_FREE;
369 hlist_nulls_del_init_rcu(&worker->nulls_node);
370 hlist_nulls_add_head_rcu(&worker->nulls_node, &wqe->free_list);
373 return __io_worker_unuse(wqe, worker);
376 static struct io_wq_work *io_get_next_work(struct io_wqe *wqe, unsigned *hash)
377 __must_hold(wqe->lock)
379 struct io_wq_work_node *node, *prev;
380 struct io_wq_work *work;
382 wq_list_for_each(node, prev, &wqe->work_list) {
383 work = container_of(node, struct io_wq_work, list);
385 /* not hashed, can run anytime */
386 if (!(work->flags & IO_WQ_WORK_HASHED)) {
387 wq_node_del(&wqe->work_list, node, prev);
391 /* hashed, can run if not already running */
392 *hash = work->flags >> IO_WQ_HASH_SHIFT;
393 if (!(wqe->hash_map & BIT_ULL(*hash))) {
394 wqe->hash_map |= BIT_ULL(*hash);
395 wq_node_del(&wqe->work_list, node, prev);
403 static void io_worker_handle_work(struct io_worker *worker)
404 __releases(wqe->lock)
406 struct io_wq_work *work, *old_work = NULL, *put_work = NULL;
407 struct io_wqe *wqe = worker->wqe;
408 struct io_wq *wq = wqe->wq;
414 * If we got some work, mark us as busy. If we didn't, but
415 * the list isn't empty, it means we stalled on hashed work.
416 * Mark us stalled so we don't keep looking for work when we
417 * can't make progress, any work completion or insertion will
418 * clear the stalled flag.
420 work = io_get_next_work(wqe, &hash);
422 __io_worker_busy(wqe, worker, work);
423 else if (!wq_list_empty(&wqe->work_list))
424 wqe->flags |= IO_WQE_FLAG_STALLED;
426 spin_unlock_irq(&wqe->lock);
427 if (put_work && wq->put_work)
428 wq->put_work(old_work);
432 /* flush any pending signals before assigning new work */
433 if (signal_pending(current))
434 flush_signals(current);
436 spin_lock_irq(&worker->lock);
437 worker->cur_work = work;
438 spin_unlock_irq(&worker->lock);
440 if (work->flags & IO_WQ_WORK_CB)
443 if ((work->flags & IO_WQ_WORK_NEEDS_FILES) &&
444 current->files != work->files) {
446 current->files = work->files;
447 task_unlock(current);
449 if ((work->flags & IO_WQ_WORK_NEEDS_USER) && !worker->mm &&
450 wq->mm && mmget_not_zero(wq->mm)) {
456 worker->creds = override_creds(wq->creds);
457 if (test_bit(IO_WQ_BIT_CANCEL, &wq->state))
458 work->flags |= IO_WQ_WORK_CANCEL;
460 work->flags |= IO_WQ_WORK_HAS_MM;
462 if (wq->get_work && !(work->flags & IO_WQ_WORK_INTERNAL)) {
470 spin_lock_irq(&worker->lock);
471 worker->cur_work = NULL;
472 spin_unlock_irq(&worker->lock);
474 spin_lock_irq(&wqe->lock);
477 wqe->hash_map &= ~BIT_ULL(hash);
478 wqe->flags &= ~IO_WQE_FLAG_STALLED;
480 if (work && work != old_work) {
481 spin_unlock_irq(&wqe->lock);
483 if (put_work && wq->put_work) {
484 wq->put_work(put_work);
488 /* dependent work not hashed */
495 static int io_wqe_worker(void *data)
497 struct io_worker *worker = data;
498 struct io_wqe *wqe = worker->wqe;
499 struct io_wq *wq = wqe->wq;
502 io_worker_start(wqe, worker);
504 while (!test_bit(IO_WQ_BIT_EXIT, &wq->state)) {
505 prepare_to_wait(&worker->wait, &wait, TASK_INTERRUPTIBLE);
507 spin_lock_irq(&wqe->lock);
508 if (io_wqe_run_queue(wqe)) {
509 __set_current_state(TASK_RUNNING);
510 io_worker_handle_work(worker);
513 /* drops the lock on success, retry */
514 if (__io_worker_idle(wqe, worker)) {
515 __release(&wqe->lock);
518 spin_unlock_irq(&wqe->lock);
519 if (signal_pending(current))
520 flush_signals(current);
521 if (schedule_timeout(WORKER_IDLE_TIMEOUT))
523 /* timed out, exit unless we're the fixed worker */
524 if (test_bit(IO_WQ_BIT_EXIT, &wq->state) ||
525 !(worker->flags & IO_WORKER_F_FIXED))
529 finish_wait(&worker->wait, &wait);
531 if (test_bit(IO_WQ_BIT_EXIT, &wq->state)) {
532 spin_lock_irq(&wqe->lock);
533 if (!wq_list_empty(&wqe->work_list))
534 io_worker_handle_work(worker);
536 spin_unlock_irq(&wqe->lock);
539 io_worker_exit(worker);
544 * Called when a worker is scheduled in. Mark us as currently running.
546 void io_wq_worker_running(struct task_struct *tsk)
548 struct io_worker *worker = kthread_data(tsk);
549 struct io_wqe *wqe = worker->wqe;
551 if (!(worker->flags & IO_WORKER_F_UP))
553 if (worker->flags & IO_WORKER_F_RUNNING)
555 worker->flags |= IO_WORKER_F_RUNNING;
556 io_wqe_inc_running(wqe, worker);
560 * Called when worker is going to sleep. If there are no workers currently
561 * running and we have work pending, wake up a free one or have the manager
564 void io_wq_worker_sleeping(struct task_struct *tsk)
566 struct io_worker *worker = kthread_data(tsk);
567 struct io_wqe *wqe = worker->wqe;
569 if (!(worker->flags & IO_WORKER_F_UP))
571 if (!(worker->flags & IO_WORKER_F_RUNNING))
574 worker->flags &= ~IO_WORKER_F_RUNNING;
576 spin_lock_irq(&wqe->lock);
577 io_wqe_dec_running(wqe, worker);
578 spin_unlock_irq(&wqe->lock);
581 static bool create_io_worker(struct io_wq *wq, struct io_wqe *wqe, int index)
583 struct io_wqe_acct *acct =&wqe->acct[index];
584 struct io_worker *worker;
586 worker = kzalloc_node(sizeof(*worker), GFP_KERNEL, wqe->node);
590 refcount_set(&worker->ref, 1);
591 worker->nulls_node.pprev = NULL;
592 init_waitqueue_head(&worker->wait);
594 spin_lock_init(&worker->lock);
596 worker->task = kthread_create_on_node(io_wqe_worker, worker, wqe->node,
597 "io_wqe_worker-%d/%d", index, wqe->node);
598 if (IS_ERR(worker->task)) {
603 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 spin_unlock_irq(&wqe->lock);
614 if (index == IO_WQ_ACCT_UNBOUND)
615 atomic_inc(&wq->user->processes);
617 wake_up_process(worker->task);
621 static inline bool io_wqe_need_worker(struct io_wqe *wqe, int index)
622 __must_hold(wqe->lock)
624 struct io_wqe_acct *acct = &wqe->acct[index];
626 /* if we have available workers or no work, no need */
627 if (!hlist_nulls_empty(&wqe->free_list) || !io_wqe_run_queue(wqe))
629 return acct->nr_workers < acct->max_workers;
633 * Manager thread. Tasked with creating new workers, if we need them.
635 static int io_wq_manager(void *data)
637 struct io_wq *wq = data;
638 int workers_to_create = num_possible_nodes();
641 /* create fixed workers */
642 refcount_set(&wq->refs, workers_to_create);
643 for_each_node(node) {
644 if (!create_io_worker(wq, wq->wqes[node], IO_WQ_ACCT_BOUND))
651 while (!kthread_should_stop()) {
652 for_each_node(node) {
653 struct io_wqe *wqe = wq->wqes[node];
654 bool fork_worker[2] = { false, false };
656 spin_lock_irq(&wqe->lock);
657 if (io_wqe_need_worker(wqe, IO_WQ_ACCT_BOUND))
658 fork_worker[IO_WQ_ACCT_BOUND] = true;
659 if (io_wqe_need_worker(wqe, IO_WQ_ACCT_UNBOUND))
660 fork_worker[IO_WQ_ACCT_UNBOUND] = true;
661 spin_unlock_irq(&wqe->lock);
662 if (fork_worker[IO_WQ_ACCT_BOUND])
663 create_io_worker(wq, wqe, IO_WQ_ACCT_BOUND);
664 if (fork_worker[IO_WQ_ACCT_UNBOUND])
665 create_io_worker(wq, wqe, IO_WQ_ACCT_UNBOUND);
667 set_current_state(TASK_INTERRUPTIBLE);
668 schedule_timeout(HZ);
673 set_bit(IO_WQ_BIT_ERROR, &wq->state);
674 set_bit(IO_WQ_BIT_EXIT, &wq->state);
675 if (refcount_sub_and_test(workers_to_create, &wq->refs))
680 static bool io_wq_can_queue(struct io_wqe *wqe, struct io_wqe_acct *acct,
681 struct io_wq_work *work)
685 if (!(work->flags & IO_WQ_WORK_UNBOUND))
687 if (atomic_read(&acct->nr_running))
691 free_worker = !hlist_nulls_empty(&wqe->free_list);
696 if (atomic_read(&wqe->wq->user->processes) >= acct->max_workers &&
697 !(capable(CAP_SYS_RESOURCE) || capable(CAP_SYS_ADMIN)))
703 static void io_wqe_enqueue(struct io_wqe *wqe, struct io_wq_work *work)
705 struct io_wqe_acct *acct = io_work_get_acct(wqe, work);
709 * Do early check to see if we need a new unbound worker, and if we do,
710 * if we're allowed to do so. This isn't 100% accurate as there's a
711 * gap between this check and incrementing the value, but that's OK.
712 * It's close enough to not be an issue, fork() has the same delay.
714 if (unlikely(!io_wq_can_queue(wqe, acct, work))) {
715 work->flags |= IO_WQ_WORK_CANCEL;
720 spin_lock_irqsave(&wqe->lock, flags);
721 wq_list_add_tail(&work->list, &wqe->work_list);
722 wqe->flags &= ~IO_WQE_FLAG_STALLED;
723 spin_unlock_irqrestore(&wqe->lock, flags);
725 if (!atomic_read(&acct->nr_running))
726 io_wqe_wake_worker(wqe, acct);
729 void io_wq_enqueue(struct io_wq *wq, struct io_wq_work *work)
731 struct io_wqe *wqe = wq->wqes[numa_node_id()];
733 io_wqe_enqueue(wqe, work);
737 * Enqueue work, hashed by some key. Work items that hash to the same value
738 * will not be done in parallel. Used to limit concurrent writes, generally
741 void io_wq_enqueue_hashed(struct io_wq *wq, struct io_wq_work *work, void *val)
743 struct io_wqe *wqe = wq->wqes[numa_node_id()];
747 bit = hash_ptr(val, IO_WQ_HASH_ORDER);
748 work->flags |= (IO_WQ_WORK_HASHED | (bit << IO_WQ_HASH_SHIFT));
749 io_wqe_enqueue(wqe, work);
752 static bool io_wqe_worker_send_sig(struct io_worker *worker, void *data)
754 send_sig(SIGINT, worker->task, 1);
759 * Iterate the passed in list and call the specific function for each
760 * worker that isn't exiting
762 static bool io_wq_for_each_worker(struct io_wqe *wqe,
763 bool (*func)(struct io_worker *, void *),
766 struct io_worker *worker;
769 list_for_each_entry_rcu(worker, &wqe->all_list, all_list) {
770 if (io_worker_get(worker)) {
771 ret = func(worker, data);
772 io_worker_release(worker);
781 void io_wq_cancel_all(struct io_wq *wq)
785 set_bit(IO_WQ_BIT_CANCEL, &wq->state);
788 * Browse both lists, as there's a gap between handing work off
789 * to a worker and the worker putting itself on the busy_list
792 for_each_node(node) {
793 struct io_wqe *wqe = wq->wqes[node];
795 io_wq_for_each_worker(wqe, io_wqe_worker_send_sig, NULL);
800 struct io_cb_cancel_data {
802 work_cancel_fn *cancel;
806 static bool io_work_cancel(struct io_worker *worker, void *cancel_data)
808 struct io_cb_cancel_data *data = cancel_data;
813 * Hold the lock to avoid ->cur_work going out of scope, caller
814 * may dereference the passed in work.
816 spin_lock_irqsave(&worker->lock, flags);
817 if (worker->cur_work &&
818 data->cancel(worker->cur_work, data->caller_data)) {
819 send_sig(SIGINT, worker->task, 1);
822 spin_unlock_irqrestore(&worker->lock, flags);
827 static enum io_wq_cancel io_wqe_cancel_cb_work(struct io_wqe *wqe,
828 work_cancel_fn *cancel,
831 struct io_cb_cancel_data data = {
834 .caller_data = cancel_data,
836 struct io_wq_work_node *node, *prev;
837 struct io_wq_work *work;
841 spin_lock_irqsave(&wqe->lock, flags);
842 wq_list_for_each(node, prev, &wqe->work_list) {
843 work = container_of(node, struct io_wq_work, list);
845 if (cancel(work, cancel_data)) {
846 wq_node_del(&wqe->work_list, node, prev);
851 spin_unlock_irqrestore(&wqe->lock, flags);
854 work->flags |= IO_WQ_WORK_CANCEL;
856 return IO_WQ_CANCEL_OK;
860 found = io_wq_for_each_worker(wqe, io_work_cancel, &data);
862 return found ? IO_WQ_CANCEL_RUNNING : IO_WQ_CANCEL_NOTFOUND;
865 enum io_wq_cancel io_wq_cancel_cb(struct io_wq *wq, work_cancel_fn *cancel,
868 enum io_wq_cancel ret = IO_WQ_CANCEL_NOTFOUND;
871 for_each_node(node) {
872 struct io_wqe *wqe = wq->wqes[node];
874 ret = io_wqe_cancel_cb_work(wqe, cancel, data);
875 if (ret != IO_WQ_CANCEL_NOTFOUND)
882 static bool io_wq_worker_cancel(struct io_worker *worker, void *data)
884 struct io_wq_work *work = data;
888 if (worker->cur_work != work)
891 spin_lock_irqsave(&worker->lock, flags);
892 if (worker->cur_work == work) {
893 send_sig(SIGINT, worker->task, 1);
896 spin_unlock_irqrestore(&worker->lock, flags);
901 static enum io_wq_cancel io_wqe_cancel_work(struct io_wqe *wqe,
902 struct io_wq_work *cwork)
904 struct io_wq_work_node *node, *prev;
905 struct io_wq_work *work;
909 cwork->flags |= IO_WQ_WORK_CANCEL;
912 * First check pending list, if we're lucky we can just remove it
913 * from there. CANCEL_OK means that the work is returned as-new,
914 * no completion will be posted for it.
916 spin_lock_irqsave(&wqe->lock, flags);
917 wq_list_for_each(node, prev, &wqe->work_list) {
918 work = container_of(node, struct io_wq_work, list);
921 wq_node_del(&wqe->work_list, node, prev);
926 spin_unlock_irqrestore(&wqe->lock, flags);
929 work->flags |= IO_WQ_WORK_CANCEL;
931 return IO_WQ_CANCEL_OK;
935 * Now check if a free (going busy) or busy worker has the work
936 * currently running. If we find it there, we'll return CANCEL_RUNNING
937 * as an indication that we attempte to signal cancellation. The
938 * completion will run normally in this case.
941 found = io_wq_for_each_worker(wqe, io_wq_worker_cancel, cwork);
943 return found ? IO_WQ_CANCEL_RUNNING : IO_WQ_CANCEL_NOTFOUND;
946 enum io_wq_cancel io_wq_cancel_work(struct io_wq *wq, struct io_wq_work *cwork)
948 enum io_wq_cancel ret = IO_WQ_CANCEL_NOTFOUND;
951 for_each_node(node) {
952 struct io_wqe *wqe = wq->wqes[node];
954 ret = io_wqe_cancel_work(wqe, cwork);
955 if (ret != IO_WQ_CANCEL_NOTFOUND)
962 struct io_wq_flush_data {
963 struct io_wq_work work;
964 struct completion done;
967 static void io_wq_flush_func(struct io_wq_work **workptr)
969 struct io_wq_work *work = *workptr;
970 struct io_wq_flush_data *data;
972 data = container_of(work, struct io_wq_flush_data, work);
973 complete(&data->done);
977 * Doesn't wait for previously queued work to finish. When this completes,
978 * it just means that previously queued work was started.
980 void io_wq_flush(struct io_wq *wq)
982 struct io_wq_flush_data data;
985 for_each_node(node) {
986 struct io_wqe *wqe = wq->wqes[node];
988 init_completion(&data.done);
989 INIT_IO_WORK(&data.work, io_wq_flush_func);
990 data.work.flags |= IO_WQ_WORK_INTERNAL;
991 io_wqe_enqueue(wqe, &data.work);
992 wait_for_completion(&data.done);
996 struct io_wq *io_wq_create(unsigned bounded, struct io_wq_data *data)
998 int ret = -ENOMEM, node;
1001 wq = kzalloc(sizeof(*wq), GFP_KERNEL);
1003 return ERR_PTR(-ENOMEM);
1005 wq->wqes = kcalloc(nr_node_ids, sizeof(struct io_wqe *), GFP_KERNEL);
1008 return ERR_PTR(-ENOMEM);
1011 wq->get_work = data->get_work;
1012 wq->put_work = data->put_work;
1014 /* caller must already hold a reference to this */
1015 wq->user = data->user;
1016 wq->creds = data->creds;
1018 for_each_node(node) {
1021 wqe = kzalloc_node(sizeof(struct io_wqe), GFP_KERNEL, node);
1024 wq->wqes[node] = wqe;
1026 wqe->acct[IO_WQ_ACCT_BOUND].max_workers = bounded;
1027 atomic_set(&wqe->acct[IO_WQ_ACCT_BOUND].nr_running, 0);
1029 wqe->acct[IO_WQ_ACCT_UNBOUND].max_workers =
1030 task_rlimit(current, RLIMIT_NPROC);
1032 atomic_set(&wqe->acct[IO_WQ_ACCT_UNBOUND].nr_running, 0);
1035 spin_lock_init(&wqe->lock);
1036 INIT_WQ_LIST(&wqe->work_list);
1037 INIT_HLIST_NULLS_HEAD(&wqe->free_list, 0);
1038 INIT_HLIST_NULLS_HEAD(&wqe->busy_list, 1);
1039 INIT_LIST_HEAD(&wqe->all_list);
1042 init_completion(&wq->done);
1044 /* caller must have already done mmgrab() on this mm */
1047 wq->manager = kthread_create(io_wq_manager, wq, "io_wq_manager");
1048 if (!IS_ERR(wq->manager)) {
1049 wake_up_process(wq->manager);
1050 wait_for_completion(&wq->done);
1051 if (test_bit(IO_WQ_BIT_ERROR, &wq->state)) {
1055 reinit_completion(&wq->done);
1059 ret = PTR_ERR(wq->manager);
1060 complete(&wq->done);
1063 kfree(wq->wqes[node]);
1066 return ERR_PTR(ret);
1069 static bool io_wq_worker_wake(struct io_worker *worker, void *data)
1071 wake_up_process(worker->task);
1075 void io_wq_destroy(struct io_wq *wq)
1079 set_bit(IO_WQ_BIT_EXIT, &wq->state);
1081 kthread_stop(wq->manager);
1085 io_wq_for_each_worker(wq->wqes[node], io_wq_worker_wake, NULL);
1088 wait_for_completion(&wq->done);
1091 kfree(wq->wqes[node]);