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;
54 struct io_wq_work *cur_work;
59 const struct cred *creds;
60 struct files_struct *restore_files;
63 #if BITS_PER_LONG == 64
64 #define IO_WQ_HASH_ORDER 6
66 #define IO_WQ_HASH_ORDER 5
81 * Per-node worker thread pool
86 struct io_wq_work_list work_list;
87 unsigned long hash_map;
89 } ____cacheline_aligned_in_smp;
92 struct io_wqe_acct acct[2];
94 struct hlist_nulls_head free_list;
95 struct hlist_nulls_head busy_list;
96 struct list_head all_list;
105 struct io_wqe **wqes;
108 get_work_fn *get_work;
109 put_work_fn *put_work;
111 struct task_struct *manager;
112 struct user_struct *user;
113 const struct cred *creds;
114 struct mm_struct *mm;
116 struct completion done;
119 static bool io_worker_get(struct io_worker *worker)
121 return refcount_inc_not_zero(&worker->ref);
124 static void io_worker_release(struct io_worker *worker)
126 if (refcount_dec_and_test(&worker->ref))
127 wake_up_process(worker->task);
131 * Note: drops the wqe->lock if returning true! The caller must re-acquire
132 * the lock in that case. Some callers need to restart handling if this
133 * happens, so we can't just re-acquire the lock on behalf of the caller.
135 static bool __io_worker_unuse(struct io_wqe *wqe, struct io_worker *worker)
137 bool dropped_lock = false;
140 revert_creds(worker->creds);
141 worker->creds = NULL;
144 if (current->files != worker->restore_files) {
145 __acquire(&wqe->lock);
146 spin_unlock_irq(&wqe->lock);
150 current->files = worker->restore_files;
151 task_unlock(current);
155 * If we have an active mm, we need to drop the wq lock before unusing
156 * it. If we do, return true and let the caller retry the idle loop.
160 __acquire(&wqe->lock);
161 spin_unlock_irq(&wqe->lock);
164 __set_current_state(TASK_RUNNING);
166 unuse_mm(worker->mm);
174 static inline struct io_wqe_acct *io_work_get_acct(struct io_wqe *wqe,
175 struct io_wq_work *work)
177 if (work->flags & IO_WQ_WORK_UNBOUND)
178 return &wqe->acct[IO_WQ_ACCT_UNBOUND];
180 return &wqe->acct[IO_WQ_ACCT_BOUND];
183 static inline struct io_wqe_acct *io_wqe_get_acct(struct io_wqe *wqe,
184 struct io_worker *worker)
186 if (worker->flags & IO_WORKER_F_BOUND)
187 return &wqe->acct[IO_WQ_ACCT_BOUND];
189 return &wqe->acct[IO_WQ_ACCT_UNBOUND];
192 static void io_worker_exit(struct io_worker *worker)
194 struct io_wqe *wqe = worker->wqe;
195 struct io_wqe_acct *acct = io_wqe_get_acct(wqe, worker);
199 * If we're not at zero, someone else is holding a brief reference
200 * to the worker. Wait for that to go away.
202 set_current_state(TASK_INTERRUPTIBLE);
203 if (!refcount_dec_and_test(&worker->ref))
205 __set_current_state(TASK_RUNNING);
208 current->flags &= ~PF_IO_WORKER;
209 if (worker->flags & IO_WORKER_F_RUNNING)
210 atomic_dec(&acct->nr_running);
211 if (!(worker->flags & IO_WORKER_F_BOUND))
212 atomic_dec(&wqe->wq->user->processes);
216 spin_lock_irq(&wqe->lock);
217 hlist_nulls_del_rcu(&worker->nulls_node);
218 list_del_rcu(&worker->all_list);
219 if (__io_worker_unuse(wqe, worker)) {
220 __release(&wqe->lock);
221 spin_lock_irq(&wqe->lock);
224 nr_workers = wqe->acct[IO_WQ_ACCT_BOUND].nr_workers +
225 wqe->acct[IO_WQ_ACCT_UNBOUND].nr_workers;
226 spin_unlock_irq(&wqe->lock);
228 /* all workers gone, wq exit can proceed */
229 if (!nr_workers && refcount_dec_and_test(&wqe->wq->refs))
230 complete(&wqe->wq->done);
232 kfree_rcu(worker, rcu);
235 static inline bool io_wqe_run_queue(struct io_wqe *wqe)
236 __must_hold(wqe->lock)
238 if (!wq_list_empty(&wqe->work_list) &&
239 !(wqe->flags & IO_WQE_FLAG_STALLED))
245 * Check head of free list for an available worker. If one isn't available,
246 * caller must wake up the wq manager to create one.
248 static bool io_wqe_activate_free_worker(struct io_wqe *wqe)
251 struct hlist_nulls_node *n;
252 struct io_worker *worker;
254 n = rcu_dereference(hlist_nulls_first_rcu(&wqe->free_list));
258 worker = hlist_nulls_entry(n, struct io_worker, nulls_node);
259 if (io_worker_get(worker)) {
260 wake_up_process(worker->task);
261 io_worker_release(worker);
269 * We need a worker. If we find a free one, we're good. If not, and we're
270 * below the max number of workers, wake up the manager to create one.
272 static void io_wqe_wake_worker(struct io_wqe *wqe, struct io_wqe_acct *acct)
277 * Most likely an attempt to queue unbounded work on an io_wq that
278 * wasn't setup with any unbounded workers.
280 WARN_ON_ONCE(!acct->max_workers);
283 ret = io_wqe_activate_free_worker(wqe);
286 if (!ret && acct->nr_workers < acct->max_workers)
287 wake_up_process(wqe->wq->manager);
290 static void io_wqe_inc_running(struct io_wqe *wqe, struct io_worker *worker)
292 struct io_wqe_acct *acct = io_wqe_get_acct(wqe, worker);
294 atomic_inc(&acct->nr_running);
297 static void io_wqe_dec_running(struct io_wqe *wqe, struct io_worker *worker)
298 __must_hold(wqe->lock)
300 struct io_wqe_acct *acct = io_wqe_get_acct(wqe, worker);
302 if (atomic_dec_and_test(&acct->nr_running) && io_wqe_run_queue(wqe))
303 io_wqe_wake_worker(wqe, acct);
306 static void io_worker_start(struct io_wqe *wqe, struct io_worker *worker)
308 allow_kernel_signal(SIGINT);
310 current->flags |= PF_IO_WORKER;
312 worker->flags |= (IO_WORKER_F_UP | IO_WORKER_F_RUNNING);
313 worker->restore_files = current->files;
314 io_wqe_inc_running(wqe, worker);
318 * Worker will start processing some work. Move it to the busy list, if
319 * it's currently on the freelist
321 static void __io_worker_busy(struct io_wqe *wqe, struct io_worker *worker,
322 struct io_wq_work *work)
323 __must_hold(wqe->lock)
325 bool worker_bound, work_bound;
327 if (worker->flags & IO_WORKER_F_FREE) {
328 worker->flags &= ~IO_WORKER_F_FREE;
329 hlist_nulls_del_init_rcu(&worker->nulls_node);
330 hlist_nulls_add_head_rcu(&worker->nulls_node, &wqe->busy_list);
334 * If worker is moving from bound to unbound (or vice versa), then
335 * ensure we update the running accounting.
337 worker_bound = (worker->flags & IO_WORKER_F_BOUND) != 0;
338 work_bound = (work->flags & IO_WQ_WORK_UNBOUND) == 0;
339 if (worker_bound != work_bound) {
340 io_wqe_dec_running(wqe, worker);
342 worker->flags |= IO_WORKER_F_BOUND;
343 wqe->acct[IO_WQ_ACCT_UNBOUND].nr_workers--;
344 wqe->acct[IO_WQ_ACCT_BOUND].nr_workers++;
345 atomic_dec(&wqe->wq->user->processes);
347 worker->flags &= ~IO_WORKER_F_BOUND;
348 wqe->acct[IO_WQ_ACCT_UNBOUND].nr_workers++;
349 wqe->acct[IO_WQ_ACCT_BOUND].nr_workers--;
350 atomic_inc(&wqe->wq->user->processes);
352 io_wqe_inc_running(wqe, worker);
357 * No work, worker going to sleep. Move to freelist, and unuse mm if we
358 * have one attached. Dropping the mm may potentially sleep, so we drop
359 * the lock in that case and return success. Since the caller has to
360 * retry the loop in that case (we changed task state), we don't regrab
361 * the lock if we return success.
363 static bool __io_worker_idle(struct io_wqe *wqe, struct io_worker *worker)
364 __must_hold(wqe->lock)
366 if (!(worker->flags & IO_WORKER_F_FREE)) {
367 worker->flags |= IO_WORKER_F_FREE;
368 hlist_nulls_del_init_rcu(&worker->nulls_node);
369 hlist_nulls_add_head_rcu(&worker->nulls_node, &wqe->free_list);
372 return __io_worker_unuse(wqe, worker);
375 static struct io_wq_work *io_get_next_work(struct io_wqe *wqe, unsigned *hash)
376 __must_hold(wqe->lock)
378 struct io_wq_work_node *node, *prev;
379 struct io_wq_work *work;
381 wq_list_for_each(node, prev, &wqe->work_list) {
382 work = container_of(node, struct io_wq_work, list);
384 /* not hashed, can run anytime */
385 if (!(work->flags & IO_WQ_WORK_HASHED)) {
386 wq_node_del(&wqe->work_list, node, prev);
390 /* hashed, can run if not already running */
391 *hash = work->flags >> IO_WQ_HASH_SHIFT;
392 if (!(wqe->hash_map & BIT_ULL(*hash))) {
393 wqe->hash_map |= BIT_ULL(*hash);
394 wq_node_del(&wqe->work_list, node, prev);
402 static void io_worker_handle_work(struct io_worker *worker)
403 __releases(wqe->lock)
405 struct io_wq_work *work, *old_work = NULL, *put_work = NULL;
406 struct io_wqe *wqe = worker->wqe;
407 struct io_wq *wq = wqe->wq;
413 * If we got some work, mark us as busy. If we didn't, but
414 * the list isn't empty, it means we stalled on hashed work.
415 * Mark us stalled so we don't keep looking for work when we
416 * can't make progress, any work completion or insertion will
417 * clear the stalled flag.
419 work = io_get_next_work(wqe, &hash);
421 __io_worker_busy(wqe, worker, work);
422 else if (!wq_list_empty(&wqe->work_list))
423 wqe->flags |= IO_WQE_FLAG_STALLED;
425 spin_unlock_irq(&wqe->lock);
426 if (put_work && wq->put_work)
427 wq->put_work(old_work);
431 /* flush any pending signals before assigning new work */
432 if (signal_pending(current))
433 flush_signals(current);
435 spin_lock_irq(&worker->lock);
436 worker->cur_work = work;
437 spin_unlock_irq(&worker->lock);
439 if (work->flags & IO_WQ_WORK_CB)
442 if ((work->flags & IO_WQ_WORK_NEEDS_FILES) &&
443 current->files != work->files) {
445 current->files = work->files;
446 task_unlock(current);
448 if ((work->flags & IO_WQ_WORK_NEEDS_USER) && !worker->mm &&
449 wq->mm && mmget_not_zero(wq->mm)) {
455 worker->creds = override_creds(wq->creds);
456 if (test_bit(IO_WQ_BIT_CANCEL, &wq->state))
457 work->flags |= IO_WQ_WORK_CANCEL;
459 work->flags |= IO_WQ_WORK_HAS_MM;
461 if (wq->get_work && !(work->flags & IO_WQ_WORK_INTERNAL)) {
469 spin_lock_irq(&worker->lock);
470 worker->cur_work = NULL;
471 spin_unlock_irq(&worker->lock);
473 spin_lock_irq(&wqe->lock);
476 wqe->hash_map &= ~BIT_ULL(hash);
477 wqe->flags &= ~IO_WQE_FLAG_STALLED;
479 if (work && work != old_work) {
480 spin_unlock_irq(&wqe->lock);
482 if (put_work && wq->put_work) {
483 wq->put_work(put_work);
487 /* dependent work not hashed */
494 static inline void io_worker_spin_for_work(struct io_wqe *wqe)
499 if (io_wqe_run_queue(wqe))
507 static int io_wqe_worker(void *data)
509 struct io_worker *worker = data;
510 struct io_wqe *wqe = worker->wqe;
511 struct io_wq *wq = wqe->wq;
514 io_worker_start(wqe, worker);
517 while (!test_bit(IO_WQ_BIT_EXIT, &wq->state)) {
518 set_current_state(TASK_INTERRUPTIBLE);
521 io_worker_spin_for_work(wqe);
522 spin_lock_irq(&wqe->lock);
523 if (io_wqe_run_queue(wqe)) {
524 __set_current_state(TASK_RUNNING);
525 io_worker_handle_work(worker);
530 /* drops the lock on success, retry */
531 if (__io_worker_idle(wqe, worker)) {
532 __release(&wqe->lock);
535 spin_unlock_irq(&wqe->lock);
536 if (signal_pending(current))
537 flush_signals(current);
538 if (schedule_timeout(WORKER_IDLE_TIMEOUT))
540 /* timed out, exit unless we're the fixed worker */
541 if (test_bit(IO_WQ_BIT_EXIT, &wq->state) ||
542 !(worker->flags & IO_WORKER_F_FIXED))
546 if (test_bit(IO_WQ_BIT_EXIT, &wq->state)) {
547 spin_lock_irq(&wqe->lock);
548 if (!wq_list_empty(&wqe->work_list))
549 io_worker_handle_work(worker);
551 spin_unlock_irq(&wqe->lock);
554 io_worker_exit(worker);
559 * Called when a worker is scheduled in. Mark us as currently running.
561 void io_wq_worker_running(struct task_struct *tsk)
563 struct io_worker *worker = kthread_data(tsk);
564 struct io_wqe *wqe = worker->wqe;
566 if (!(worker->flags & IO_WORKER_F_UP))
568 if (worker->flags & IO_WORKER_F_RUNNING)
570 worker->flags |= IO_WORKER_F_RUNNING;
571 io_wqe_inc_running(wqe, worker);
575 * Called when worker is going to sleep. If there are no workers currently
576 * running and we have work pending, wake up a free one or have the manager
579 void io_wq_worker_sleeping(struct task_struct *tsk)
581 struct io_worker *worker = kthread_data(tsk);
582 struct io_wqe *wqe = worker->wqe;
584 if (!(worker->flags & IO_WORKER_F_UP))
586 if (!(worker->flags & IO_WORKER_F_RUNNING))
589 worker->flags &= ~IO_WORKER_F_RUNNING;
591 spin_lock_irq(&wqe->lock);
592 io_wqe_dec_running(wqe, worker);
593 spin_unlock_irq(&wqe->lock);
596 static bool create_io_worker(struct io_wq *wq, struct io_wqe *wqe, int index)
598 struct io_wqe_acct *acct =&wqe->acct[index];
599 struct io_worker *worker;
601 worker = kzalloc_node(sizeof(*worker), GFP_KERNEL, wqe->node);
605 refcount_set(&worker->ref, 1);
606 worker->nulls_node.pprev = NULL;
608 spin_lock_init(&worker->lock);
610 worker->task = kthread_create_on_node(io_wqe_worker, worker, wqe->node,
611 "io_wqe_worker-%d/%d", index, wqe->node);
612 if (IS_ERR(worker->task)) {
617 spin_lock_irq(&wqe->lock);
618 hlist_nulls_add_head_rcu(&worker->nulls_node, &wqe->free_list);
619 list_add_tail_rcu(&worker->all_list, &wqe->all_list);
620 worker->flags |= IO_WORKER_F_FREE;
621 if (index == IO_WQ_ACCT_BOUND)
622 worker->flags |= IO_WORKER_F_BOUND;
623 if (!acct->nr_workers && (worker->flags & IO_WORKER_F_BOUND))
624 worker->flags |= IO_WORKER_F_FIXED;
626 spin_unlock_irq(&wqe->lock);
628 if (index == IO_WQ_ACCT_UNBOUND)
629 atomic_inc(&wq->user->processes);
631 wake_up_process(worker->task);
635 static inline bool io_wqe_need_worker(struct io_wqe *wqe, int index)
636 __must_hold(wqe->lock)
638 struct io_wqe_acct *acct = &wqe->acct[index];
640 /* if we have available workers or no work, no need */
641 if (!hlist_nulls_empty(&wqe->free_list) || !io_wqe_run_queue(wqe))
643 return acct->nr_workers < acct->max_workers;
647 * Manager thread. Tasked with creating new workers, if we need them.
649 static int io_wq_manager(void *data)
651 struct io_wq *wq = data;
652 int workers_to_create = num_possible_nodes();
655 /* create fixed workers */
656 refcount_set(&wq->refs, workers_to_create);
657 for_each_node(node) {
658 if (!create_io_worker(wq, wq->wqes[node], IO_WQ_ACCT_BOUND))
665 while (!kthread_should_stop()) {
666 for_each_node(node) {
667 struct io_wqe *wqe = wq->wqes[node];
668 bool fork_worker[2] = { false, false };
670 spin_lock_irq(&wqe->lock);
671 if (io_wqe_need_worker(wqe, IO_WQ_ACCT_BOUND))
672 fork_worker[IO_WQ_ACCT_BOUND] = true;
673 if (io_wqe_need_worker(wqe, IO_WQ_ACCT_UNBOUND))
674 fork_worker[IO_WQ_ACCT_UNBOUND] = true;
675 spin_unlock_irq(&wqe->lock);
676 if (fork_worker[IO_WQ_ACCT_BOUND])
677 create_io_worker(wq, wqe, IO_WQ_ACCT_BOUND);
678 if (fork_worker[IO_WQ_ACCT_UNBOUND])
679 create_io_worker(wq, wqe, IO_WQ_ACCT_UNBOUND);
681 set_current_state(TASK_INTERRUPTIBLE);
682 schedule_timeout(HZ);
687 set_bit(IO_WQ_BIT_ERROR, &wq->state);
688 set_bit(IO_WQ_BIT_EXIT, &wq->state);
689 if (refcount_sub_and_test(workers_to_create, &wq->refs))
694 static bool io_wq_can_queue(struct io_wqe *wqe, struct io_wqe_acct *acct,
695 struct io_wq_work *work)
699 if (!(work->flags & IO_WQ_WORK_UNBOUND))
701 if (atomic_read(&acct->nr_running))
705 free_worker = !hlist_nulls_empty(&wqe->free_list);
710 if (atomic_read(&wqe->wq->user->processes) >= acct->max_workers &&
711 !(capable(CAP_SYS_RESOURCE) || capable(CAP_SYS_ADMIN)))
717 static void io_wqe_enqueue(struct io_wqe *wqe, struct io_wq_work *work)
719 struct io_wqe_acct *acct = io_work_get_acct(wqe, work);
723 * Do early check to see if we need a new unbound worker, and if we do,
724 * if we're allowed to do so. This isn't 100% accurate as there's a
725 * gap between this check and incrementing the value, but that's OK.
726 * It's close enough to not be an issue, fork() has the same delay.
728 if (unlikely(!io_wq_can_queue(wqe, acct, work))) {
729 work->flags |= IO_WQ_WORK_CANCEL;
734 spin_lock_irqsave(&wqe->lock, flags);
735 wq_list_add_tail(&work->list, &wqe->work_list);
736 wqe->flags &= ~IO_WQE_FLAG_STALLED;
737 spin_unlock_irqrestore(&wqe->lock, flags);
739 if (!atomic_read(&acct->nr_running))
740 io_wqe_wake_worker(wqe, acct);
743 void io_wq_enqueue(struct io_wq *wq, struct io_wq_work *work)
745 struct io_wqe *wqe = wq->wqes[numa_node_id()];
747 io_wqe_enqueue(wqe, work);
751 * Enqueue work, hashed by some key. Work items that hash to the same value
752 * will not be done in parallel. Used to limit concurrent writes, generally
755 void io_wq_enqueue_hashed(struct io_wq *wq, struct io_wq_work *work, void *val)
757 struct io_wqe *wqe = wq->wqes[numa_node_id()];
761 bit = hash_ptr(val, IO_WQ_HASH_ORDER);
762 work->flags |= (IO_WQ_WORK_HASHED | (bit << IO_WQ_HASH_SHIFT));
763 io_wqe_enqueue(wqe, work);
766 static bool io_wqe_worker_send_sig(struct io_worker *worker, void *data)
768 send_sig(SIGINT, worker->task, 1);
773 * Iterate the passed in list and call the specific function for each
774 * worker that isn't exiting
776 static bool io_wq_for_each_worker(struct io_wqe *wqe,
777 bool (*func)(struct io_worker *, void *),
780 struct io_worker *worker;
783 list_for_each_entry_rcu(worker, &wqe->all_list, all_list) {
784 if (io_worker_get(worker)) {
785 ret = func(worker, data);
786 io_worker_release(worker);
795 void io_wq_cancel_all(struct io_wq *wq)
799 set_bit(IO_WQ_BIT_CANCEL, &wq->state);
802 * Browse both lists, as there's a gap between handing work off
803 * to a worker and the worker putting itself on the busy_list
806 for_each_node(node) {
807 struct io_wqe *wqe = wq->wqes[node];
809 io_wq_for_each_worker(wqe, io_wqe_worker_send_sig, NULL);
814 struct io_cb_cancel_data {
816 work_cancel_fn *cancel;
820 static bool io_work_cancel(struct io_worker *worker, void *cancel_data)
822 struct io_cb_cancel_data *data = cancel_data;
827 * Hold the lock to avoid ->cur_work going out of scope, caller
828 * may dereference the passed in work.
830 spin_lock_irqsave(&worker->lock, flags);
831 if (worker->cur_work &&
832 data->cancel(worker->cur_work, data->caller_data)) {
833 send_sig(SIGINT, worker->task, 1);
836 spin_unlock_irqrestore(&worker->lock, flags);
841 static enum io_wq_cancel io_wqe_cancel_cb_work(struct io_wqe *wqe,
842 work_cancel_fn *cancel,
845 struct io_cb_cancel_data data = {
848 .caller_data = cancel_data,
850 struct io_wq_work_node *node, *prev;
851 struct io_wq_work *work;
855 spin_lock_irqsave(&wqe->lock, flags);
856 wq_list_for_each(node, prev, &wqe->work_list) {
857 work = container_of(node, struct io_wq_work, list);
859 if (cancel(work, cancel_data)) {
860 wq_node_del(&wqe->work_list, node, prev);
865 spin_unlock_irqrestore(&wqe->lock, flags);
868 work->flags |= IO_WQ_WORK_CANCEL;
870 return IO_WQ_CANCEL_OK;
874 found = io_wq_for_each_worker(wqe, io_work_cancel, &data);
876 return found ? IO_WQ_CANCEL_RUNNING : IO_WQ_CANCEL_NOTFOUND;
879 enum io_wq_cancel io_wq_cancel_cb(struct io_wq *wq, work_cancel_fn *cancel,
882 enum io_wq_cancel ret = IO_WQ_CANCEL_NOTFOUND;
885 for_each_node(node) {
886 struct io_wqe *wqe = wq->wqes[node];
888 ret = io_wqe_cancel_cb_work(wqe, cancel, data);
889 if (ret != IO_WQ_CANCEL_NOTFOUND)
896 static bool io_wq_worker_cancel(struct io_worker *worker, void *data)
898 struct io_wq_work *work = data;
902 if (worker->cur_work != work)
905 spin_lock_irqsave(&worker->lock, flags);
906 if (worker->cur_work == work) {
907 send_sig(SIGINT, worker->task, 1);
910 spin_unlock_irqrestore(&worker->lock, flags);
915 static enum io_wq_cancel io_wqe_cancel_work(struct io_wqe *wqe,
916 struct io_wq_work *cwork)
918 struct io_wq_work_node *node, *prev;
919 struct io_wq_work *work;
923 cwork->flags |= IO_WQ_WORK_CANCEL;
926 * First check pending list, if we're lucky we can just remove it
927 * from there. CANCEL_OK means that the work is returned as-new,
928 * no completion will be posted for it.
930 spin_lock_irqsave(&wqe->lock, flags);
931 wq_list_for_each(node, prev, &wqe->work_list) {
932 work = container_of(node, struct io_wq_work, list);
935 wq_node_del(&wqe->work_list, node, prev);
940 spin_unlock_irqrestore(&wqe->lock, flags);
943 work->flags |= IO_WQ_WORK_CANCEL;
945 return IO_WQ_CANCEL_OK;
949 * Now check if a free (going busy) or busy worker has the work
950 * currently running. If we find it there, we'll return CANCEL_RUNNING
951 * as an indication that we attempte to signal cancellation. The
952 * completion will run normally in this case.
955 found = io_wq_for_each_worker(wqe, io_wq_worker_cancel, cwork);
957 return found ? IO_WQ_CANCEL_RUNNING : IO_WQ_CANCEL_NOTFOUND;
960 enum io_wq_cancel io_wq_cancel_work(struct io_wq *wq, struct io_wq_work *cwork)
962 enum io_wq_cancel ret = IO_WQ_CANCEL_NOTFOUND;
965 for_each_node(node) {
966 struct io_wqe *wqe = wq->wqes[node];
968 ret = io_wqe_cancel_work(wqe, cwork);
969 if (ret != IO_WQ_CANCEL_NOTFOUND)
976 struct io_wq_flush_data {
977 struct io_wq_work work;
978 struct completion done;
981 static void io_wq_flush_func(struct io_wq_work **workptr)
983 struct io_wq_work *work = *workptr;
984 struct io_wq_flush_data *data;
986 data = container_of(work, struct io_wq_flush_data, work);
987 complete(&data->done);
991 * Doesn't wait for previously queued work to finish. When this completes,
992 * it just means that previously queued work was started.
994 void io_wq_flush(struct io_wq *wq)
996 struct io_wq_flush_data data;
999 for_each_node(node) {
1000 struct io_wqe *wqe = wq->wqes[node];
1002 init_completion(&data.done);
1003 INIT_IO_WORK(&data.work, io_wq_flush_func);
1004 data.work.flags |= IO_WQ_WORK_INTERNAL;
1005 io_wqe_enqueue(wqe, &data.work);
1006 wait_for_completion(&data.done);
1010 struct io_wq *io_wq_create(unsigned bounded, struct io_wq_data *data)
1012 int ret = -ENOMEM, node;
1015 wq = kzalloc(sizeof(*wq), GFP_KERNEL);
1017 return ERR_PTR(-ENOMEM);
1019 wq->wqes = kcalloc(nr_node_ids, sizeof(struct io_wqe *), GFP_KERNEL);
1022 return ERR_PTR(-ENOMEM);
1025 wq->get_work = data->get_work;
1026 wq->put_work = data->put_work;
1028 /* caller must already hold a reference to this */
1029 wq->user = data->user;
1030 wq->creds = data->creds;
1032 for_each_node(node) {
1035 wqe = kzalloc_node(sizeof(struct io_wqe), GFP_KERNEL, node);
1038 wq->wqes[node] = wqe;
1040 wqe->acct[IO_WQ_ACCT_BOUND].max_workers = bounded;
1041 atomic_set(&wqe->acct[IO_WQ_ACCT_BOUND].nr_running, 0);
1043 wqe->acct[IO_WQ_ACCT_UNBOUND].max_workers =
1044 task_rlimit(current, RLIMIT_NPROC);
1046 atomic_set(&wqe->acct[IO_WQ_ACCT_UNBOUND].nr_running, 0);
1049 spin_lock_init(&wqe->lock);
1050 INIT_WQ_LIST(&wqe->work_list);
1051 INIT_HLIST_NULLS_HEAD(&wqe->free_list, 0);
1052 INIT_HLIST_NULLS_HEAD(&wqe->busy_list, 1);
1053 INIT_LIST_HEAD(&wqe->all_list);
1056 init_completion(&wq->done);
1058 /* caller must have already done mmgrab() on this mm */
1061 wq->manager = kthread_create(io_wq_manager, wq, "io_wq_manager");
1062 if (!IS_ERR(wq->manager)) {
1063 wake_up_process(wq->manager);
1064 wait_for_completion(&wq->done);
1065 if (test_bit(IO_WQ_BIT_ERROR, &wq->state)) {
1069 reinit_completion(&wq->done);
1073 ret = PTR_ERR(wq->manager);
1074 complete(&wq->done);
1077 kfree(wq->wqes[node]);
1080 return ERR_PTR(ret);
1083 static bool io_wq_worker_wake(struct io_worker *worker, void *data)
1085 wake_up_process(worker->task);
1089 void io_wq_destroy(struct io_wq *wq)
1093 set_bit(IO_WQ_BIT_EXIT, &wq->state);
1095 kthread_stop(wq->manager);
1099 io_wq_for_each_worker(wq->wqes[node], io_wq_worker_wake, NULL);
1102 wait_for_completion(&wq->done);
1105 kfree(wq->wqes[node]);