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 list_head all_list;
104 struct io_wqe **wqes;
107 get_work_fn *get_work;
108 put_work_fn *put_work;
110 struct task_struct *manager;
111 struct user_struct *user;
112 const struct cred *creds;
113 struct mm_struct *mm;
115 struct completion done;
118 static bool io_worker_get(struct io_worker *worker)
120 return refcount_inc_not_zero(&worker->ref);
123 static void io_worker_release(struct io_worker *worker)
125 if (refcount_dec_and_test(&worker->ref))
126 wake_up_process(worker->task);
130 * Note: drops the wqe->lock if returning true! The caller must re-acquire
131 * the lock in that case. Some callers need to restart handling if this
132 * happens, so we can't just re-acquire the lock on behalf of the caller.
134 static bool __io_worker_unuse(struct io_wqe *wqe, struct io_worker *worker)
136 bool dropped_lock = false;
139 revert_creds(worker->creds);
140 worker->creds = NULL;
143 if (current->files != worker->restore_files) {
144 __acquire(&wqe->lock);
145 spin_unlock_irq(&wqe->lock);
149 current->files = worker->restore_files;
150 task_unlock(current);
154 * If we have an active mm, we need to drop the wq lock before unusing
155 * it. If we do, return true and let the caller retry the idle loop.
159 __acquire(&wqe->lock);
160 spin_unlock_irq(&wqe->lock);
163 __set_current_state(TASK_RUNNING);
165 unuse_mm(worker->mm);
173 static inline struct io_wqe_acct *io_work_get_acct(struct io_wqe *wqe,
174 struct io_wq_work *work)
176 if (work->flags & IO_WQ_WORK_UNBOUND)
177 return &wqe->acct[IO_WQ_ACCT_UNBOUND];
179 return &wqe->acct[IO_WQ_ACCT_BOUND];
182 static inline struct io_wqe_acct *io_wqe_get_acct(struct io_wqe *wqe,
183 struct io_worker *worker)
185 if (worker->flags & IO_WORKER_F_BOUND)
186 return &wqe->acct[IO_WQ_ACCT_BOUND];
188 return &wqe->acct[IO_WQ_ACCT_UNBOUND];
191 static void io_worker_exit(struct io_worker *worker)
193 struct io_wqe *wqe = worker->wqe;
194 struct io_wqe_acct *acct = io_wqe_get_acct(wqe, worker);
198 * If we're not at zero, someone else is holding a brief reference
199 * to the worker. Wait for that to go away.
201 set_current_state(TASK_INTERRUPTIBLE);
202 if (!refcount_dec_and_test(&worker->ref))
204 __set_current_state(TASK_RUNNING);
207 current->flags &= ~PF_IO_WORKER;
208 if (worker->flags & IO_WORKER_F_RUNNING)
209 atomic_dec(&acct->nr_running);
210 if (!(worker->flags & IO_WORKER_F_BOUND))
211 atomic_dec(&wqe->wq->user->processes);
215 spin_lock_irq(&wqe->lock);
216 hlist_nulls_del_rcu(&worker->nulls_node);
217 list_del_rcu(&worker->all_list);
218 if (__io_worker_unuse(wqe, worker)) {
219 __release(&wqe->lock);
220 spin_lock_irq(&wqe->lock);
223 nr_workers = wqe->acct[IO_WQ_ACCT_BOUND].nr_workers +
224 wqe->acct[IO_WQ_ACCT_UNBOUND].nr_workers;
225 spin_unlock_irq(&wqe->lock);
227 /* all workers gone, wq exit can proceed */
228 if (!nr_workers && refcount_dec_and_test(&wqe->wq->refs))
229 complete(&wqe->wq->done);
231 kfree_rcu(worker, rcu);
234 static inline bool io_wqe_run_queue(struct io_wqe *wqe)
235 __must_hold(wqe->lock)
237 if (!wq_list_empty(&wqe->work_list) &&
238 !(wqe->flags & IO_WQE_FLAG_STALLED))
244 * Check head of free list for an available worker. If one isn't available,
245 * caller must wake up the wq manager to create one.
247 static bool io_wqe_activate_free_worker(struct io_wqe *wqe)
250 struct hlist_nulls_node *n;
251 struct io_worker *worker;
253 n = rcu_dereference(hlist_nulls_first_rcu(&wqe->free_list));
257 worker = hlist_nulls_entry(n, struct io_worker, nulls_node);
258 if (io_worker_get(worker)) {
259 wake_up_process(worker->task);
260 io_worker_release(worker);
268 * We need a worker. If we find a free one, we're good. If not, and we're
269 * below the max number of workers, wake up the manager to create one.
271 static void io_wqe_wake_worker(struct io_wqe *wqe, struct io_wqe_acct *acct)
276 * Most likely an attempt to queue unbounded work on an io_wq that
277 * wasn't setup with any unbounded workers.
279 WARN_ON_ONCE(!acct->max_workers);
282 ret = io_wqe_activate_free_worker(wqe);
285 if (!ret && acct->nr_workers < acct->max_workers)
286 wake_up_process(wqe->wq->manager);
289 static void io_wqe_inc_running(struct io_wqe *wqe, struct io_worker *worker)
291 struct io_wqe_acct *acct = io_wqe_get_acct(wqe, worker);
293 atomic_inc(&acct->nr_running);
296 static void io_wqe_dec_running(struct io_wqe *wqe, struct io_worker *worker)
297 __must_hold(wqe->lock)
299 struct io_wqe_acct *acct = io_wqe_get_acct(wqe, worker);
301 if (atomic_dec_and_test(&acct->nr_running) && io_wqe_run_queue(wqe))
302 io_wqe_wake_worker(wqe, acct);
305 static void io_worker_start(struct io_wqe *wqe, struct io_worker *worker)
307 allow_kernel_signal(SIGINT);
309 current->flags |= PF_IO_WORKER;
311 worker->flags |= (IO_WORKER_F_UP | IO_WORKER_F_RUNNING);
312 worker->restore_files = current->files;
313 io_wqe_inc_running(wqe, worker);
317 * Worker will start processing some work. Move it to the busy list, if
318 * it's currently on the freelist
320 static void __io_worker_busy(struct io_wqe *wqe, struct io_worker *worker,
321 struct io_wq_work *work)
322 __must_hold(wqe->lock)
324 bool worker_bound, work_bound;
326 if (worker->flags & IO_WORKER_F_FREE) {
327 worker->flags &= ~IO_WORKER_F_FREE;
328 hlist_nulls_del_init_rcu(&worker->nulls_node);
332 * If worker is moving from bound to unbound (or vice versa), then
333 * ensure we update the running accounting.
335 worker_bound = (worker->flags & IO_WORKER_F_BOUND) != 0;
336 work_bound = (work->flags & IO_WQ_WORK_UNBOUND) == 0;
337 if (worker_bound != work_bound) {
338 io_wqe_dec_running(wqe, worker);
340 worker->flags |= IO_WORKER_F_BOUND;
341 wqe->acct[IO_WQ_ACCT_UNBOUND].nr_workers--;
342 wqe->acct[IO_WQ_ACCT_BOUND].nr_workers++;
343 atomic_dec(&wqe->wq->user->processes);
345 worker->flags &= ~IO_WORKER_F_BOUND;
346 wqe->acct[IO_WQ_ACCT_UNBOUND].nr_workers++;
347 wqe->acct[IO_WQ_ACCT_BOUND].nr_workers--;
348 atomic_inc(&wqe->wq->user->processes);
350 io_wqe_inc_running(wqe, worker);
355 * No work, worker going to sleep. Move to freelist, and unuse mm if we
356 * have one attached. Dropping the mm may potentially sleep, so we drop
357 * the lock in that case and return success. Since the caller has to
358 * retry the loop in that case (we changed task state), we don't regrab
359 * the lock if we return success.
361 static bool __io_worker_idle(struct io_wqe *wqe, struct io_worker *worker)
362 __must_hold(wqe->lock)
364 if (!(worker->flags & IO_WORKER_F_FREE)) {
365 worker->flags |= IO_WORKER_F_FREE;
366 hlist_nulls_add_head_rcu(&worker->nulls_node, &wqe->free_list);
369 return __io_worker_unuse(wqe, worker);
372 static struct io_wq_work *io_get_next_work(struct io_wqe *wqe, unsigned *hash)
373 __must_hold(wqe->lock)
375 struct io_wq_work_node *node, *prev;
376 struct io_wq_work *work;
378 wq_list_for_each(node, prev, &wqe->work_list) {
379 work = container_of(node, struct io_wq_work, list);
381 /* not hashed, can run anytime */
382 if (!(work->flags & IO_WQ_WORK_HASHED)) {
383 wq_node_del(&wqe->work_list, node, prev);
387 /* hashed, can run if not already running */
388 *hash = work->flags >> IO_WQ_HASH_SHIFT;
389 if (!(wqe->hash_map & BIT_ULL(*hash))) {
390 wqe->hash_map |= BIT_ULL(*hash);
391 wq_node_del(&wqe->work_list, node, prev);
399 static void io_worker_handle_work(struct io_worker *worker)
400 __releases(wqe->lock)
402 struct io_wq_work *work, *old_work = NULL, *put_work = NULL;
403 struct io_wqe *wqe = worker->wqe;
404 struct io_wq *wq = wqe->wq;
410 * If we got some work, mark us as busy. If we didn't, but
411 * the list isn't empty, it means we stalled on hashed work.
412 * Mark us stalled so we don't keep looking for work when we
413 * can't make progress, any work completion or insertion will
414 * clear the stalled flag.
416 work = io_get_next_work(wqe, &hash);
418 __io_worker_busy(wqe, worker, work);
419 else if (!wq_list_empty(&wqe->work_list))
420 wqe->flags |= IO_WQE_FLAG_STALLED;
422 spin_unlock_irq(&wqe->lock);
423 if (put_work && wq->put_work)
424 wq->put_work(old_work);
428 /* flush any pending signals before assigning new work */
429 if (signal_pending(current))
430 flush_signals(current);
434 spin_lock_irq(&worker->lock);
435 worker->cur_work = work;
436 spin_unlock_irq(&worker->lock);
438 if (work->flags & IO_WQ_WORK_CB)
441 if ((work->flags & IO_WQ_WORK_NEEDS_FILES) &&
442 current->files != work->files) {
444 current->files = work->files;
445 task_unlock(current);
447 if ((work->flags & IO_WQ_WORK_NEEDS_USER) && !worker->mm &&
449 if (mmget_not_zero(wq->mm)) {
454 work->flags |= IO_WQ_WORK_CANCEL;
458 worker->creds = override_creds(wq->creds);
459 if (test_bit(IO_WQ_BIT_CANCEL, &wq->state))
460 work->flags |= IO_WQ_WORK_CANCEL;
462 work->flags |= IO_WQ_WORK_HAS_MM;
464 if (wq->get_work && !(work->flags & IO_WQ_WORK_INTERNAL)) {
472 spin_lock_irq(&worker->lock);
473 worker->cur_work = NULL;
474 spin_unlock_irq(&worker->lock);
476 spin_lock_irq(&wqe->lock);
479 wqe->hash_map &= ~BIT_ULL(hash);
480 wqe->flags &= ~IO_WQE_FLAG_STALLED;
482 if (work && work != old_work) {
483 spin_unlock_irq(&wqe->lock);
485 if (put_work && wq->put_work) {
486 wq->put_work(put_work);
490 /* dependent work not hashed */
497 static inline void io_worker_spin_for_work(struct io_wqe *wqe)
502 if (io_wqe_run_queue(wqe))
510 static int io_wqe_worker(void *data)
512 struct io_worker *worker = data;
513 struct io_wqe *wqe = worker->wqe;
514 struct io_wq *wq = wqe->wq;
517 io_worker_start(wqe, worker);
520 while (!test_bit(IO_WQ_BIT_EXIT, &wq->state)) {
521 set_current_state(TASK_INTERRUPTIBLE);
524 io_worker_spin_for_work(wqe);
525 spin_lock_irq(&wqe->lock);
526 if (io_wqe_run_queue(wqe)) {
527 __set_current_state(TASK_RUNNING);
528 io_worker_handle_work(worker);
533 /* drops the lock on success, retry */
534 if (__io_worker_idle(wqe, worker)) {
535 __release(&wqe->lock);
538 spin_unlock_irq(&wqe->lock);
539 if (signal_pending(current))
540 flush_signals(current);
541 if (schedule_timeout(WORKER_IDLE_TIMEOUT))
543 /* timed out, exit unless we're the fixed worker */
544 if (test_bit(IO_WQ_BIT_EXIT, &wq->state) ||
545 !(worker->flags & IO_WORKER_F_FIXED))
549 if (test_bit(IO_WQ_BIT_EXIT, &wq->state)) {
550 spin_lock_irq(&wqe->lock);
551 if (!wq_list_empty(&wqe->work_list))
552 io_worker_handle_work(worker);
554 spin_unlock_irq(&wqe->lock);
557 io_worker_exit(worker);
562 * Called when a worker is scheduled in. Mark us as currently running.
564 void io_wq_worker_running(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)
573 worker->flags |= IO_WORKER_F_RUNNING;
574 io_wqe_inc_running(wqe, worker);
578 * Called when worker is going to sleep. If there are no workers currently
579 * running and we have work pending, wake up a free one or have the manager
582 void io_wq_worker_sleeping(struct task_struct *tsk)
584 struct io_worker *worker = kthread_data(tsk);
585 struct io_wqe *wqe = worker->wqe;
587 if (!(worker->flags & IO_WORKER_F_UP))
589 if (!(worker->flags & IO_WORKER_F_RUNNING))
592 worker->flags &= ~IO_WORKER_F_RUNNING;
594 spin_lock_irq(&wqe->lock);
595 io_wqe_dec_running(wqe, worker);
596 spin_unlock_irq(&wqe->lock);
599 static bool create_io_worker(struct io_wq *wq, struct io_wqe *wqe, int index)
601 struct io_wqe_acct *acct =&wqe->acct[index];
602 struct io_worker *worker;
604 worker = kzalloc_node(sizeof(*worker), GFP_KERNEL, wqe->node);
608 refcount_set(&worker->ref, 1);
609 worker->nulls_node.pprev = NULL;
611 spin_lock_init(&worker->lock);
613 worker->task = kthread_create_on_node(io_wqe_worker, worker, wqe->node,
614 "io_wqe_worker-%d/%d", index, wqe->node);
615 if (IS_ERR(worker->task)) {
620 spin_lock_irq(&wqe->lock);
621 hlist_nulls_add_head_rcu(&worker->nulls_node, &wqe->free_list);
622 list_add_tail_rcu(&worker->all_list, &wqe->all_list);
623 worker->flags |= IO_WORKER_F_FREE;
624 if (index == IO_WQ_ACCT_BOUND)
625 worker->flags |= IO_WORKER_F_BOUND;
626 if (!acct->nr_workers && (worker->flags & IO_WORKER_F_BOUND))
627 worker->flags |= IO_WORKER_F_FIXED;
629 spin_unlock_irq(&wqe->lock);
631 if (index == IO_WQ_ACCT_UNBOUND)
632 atomic_inc(&wq->user->processes);
634 wake_up_process(worker->task);
638 static inline bool io_wqe_need_worker(struct io_wqe *wqe, int index)
639 __must_hold(wqe->lock)
641 struct io_wqe_acct *acct = &wqe->acct[index];
643 /* if we have available workers or no work, no need */
644 if (!hlist_nulls_empty(&wqe->free_list) || !io_wqe_run_queue(wqe))
646 return acct->nr_workers < acct->max_workers;
650 * Manager thread. Tasked with creating new workers, if we need them.
652 static int io_wq_manager(void *data)
654 struct io_wq *wq = data;
655 int workers_to_create = num_possible_nodes();
658 /* create fixed workers */
659 refcount_set(&wq->refs, workers_to_create);
660 for_each_node(node) {
661 if (!create_io_worker(wq, wq->wqes[node], IO_WQ_ACCT_BOUND))
668 while (!kthread_should_stop()) {
669 for_each_node(node) {
670 struct io_wqe *wqe = wq->wqes[node];
671 bool fork_worker[2] = { false, false };
673 spin_lock_irq(&wqe->lock);
674 if (io_wqe_need_worker(wqe, IO_WQ_ACCT_BOUND))
675 fork_worker[IO_WQ_ACCT_BOUND] = true;
676 if (io_wqe_need_worker(wqe, IO_WQ_ACCT_UNBOUND))
677 fork_worker[IO_WQ_ACCT_UNBOUND] = true;
678 spin_unlock_irq(&wqe->lock);
679 if (fork_worker[IO_WQ_ACCT_BOUND])
680 create_io_worker(wq, wqe, IO_WQ_ACCT_BOUND);
681 if (fork_worker[IO_WQ_ACCT_UNBOUND])
682 create_io_worker(wq, wqe, IO_WQ_ACCT_UNBOUND);
684 set_current_state(TASK_INTERRUPTIBLE);
685 schedule_timeout(HZ);
690 set_bit(IO_WQ_BIT_ERROR, &wq->state);
691 set_bit(IO_WQ_BIT_EXIT, &wq->state);
692 if (refcount_sub_and_test(workers_to_create, &wq->refs))
697 static bool io_wq_can_queue(struct io_wqe *wqe, struct io_wqe_acct *acct,
698 struct io_wq_work *work)
702 if (!(work->flags & IO_WQ_WORK_UNBOUND))
704 if (atomic_read(&acct->nr_running))
708 free_worker = !hlist_nulls_empty(&wqe->free_list);
713 if (atomic_read(&wqe->wq->user->processes) >= acct->max_workers &&
714 !(capable(CAP_SYS_RESOURCE) || capable(CAP_SYS_ADMIN)))
720 static void io_wqe_enqueue(struct io_wqe *wqe, struct io_wq_work *work)
722 struct io_wqe_acct *acct = io_work_get_acct(wqe, work);
726 * Do early check to see if we need a new unbound worker, and if we do,
727 * if we're allowed to do so. This isn't 100% accurate as there's a
728 * gap between this check and incrementing the value, but that's OK.
729 * It's close enough to not be an issue, fork() has the same delay.
731 if (unlikely(!io_wq_can_queue(wqe, acct, work))) {
732 work->flags |= IO_WQ_WORK_CANCEL;
737 spin_lock_irqsave(&wqe->lock, flags);
738 wq_list_add_tail(&work->list, &wqe->work_list);
739 wqe->flags &= ~IO_WQE_FLAG_STALLED;
740 spin_unlock_irqrestore(&wqe->lock, flags);
742 if (!atomic_read(&acct->nr_running))
743 io_wqe_wake_worker(wqe, acct);
746 void io_wq_enqueue(struct io_wq *wq, struct io_wq_work *work)
748 struct io_wqe *wqe = wq->wqes[numa_node_id()];
750 io_wqe_enqueue(wqe, work);
754 * Enqueue work, hashed by some key. Work items that hash to the same value
755 * will not be done in parallel. Used to limit concurrent writes, generally
758 void io_wq_enqueue_hashed(struct io_wq *wq, struct io_wq_work *work, void *val)
760 struct io_wqe *wqe = wq->wqes[numa_node_id()];
764 bit = hash_ptr(val, IO_WQ_HASH_ORDER);
765 work->flags |= (IO_WQ_WORK_HASHED | (bit << IO_WQ_HASH_SHIFT));
766 io_wqe_enqueue(wqe, work);
769 static bool io_wqe_worker_send_sig(struct io_worker *worker, void *data)
771 send_sig(SIGINT, worker->task, 1);
776 * Iterate the passed in list and call the specific function for each
777 * worker that isn't exiting
779 static bool io_wq_for_each_worker(struct io_wqe *wqe,
780 bool (*func)(struct io_worker *, void *),
783 struct io_worker *worker;
786 list_for_each_entry_rcu(worker, &wqe->all_list, all_list) {
787 if (io_worker_get(worker)) {
788 ret = func(worker, data);
789 io_worker_release(worker);
798 void io_wq_cancel_all(struct io_wq *wq)
802 set_bit(IO_WQ_BIT_CANCEL, &wq->state);
805 for_each_node(node) {
806 struct io_wqe *wqe = wq->wqes[node];
808 io_wq_for_each_worker(wqe, io_wqe_worker_send_sig, NULL);
813 struct io_cb_cancel_data {
815 work_cancel_fn *cancel;
819 static bool io_work_cancel(struct io_worker *worker, void *cancel_data)
821 struct io_cb_cancel_data *data = cancel_data;
826 * Hold the lock to avoid ->cur_work going out of scope, caller
827 * may dereference the passed in work.
829 spin_lock_irqsave(&worker->lock, flags);
830 if (worker->cur_work &&
831 data->cancel(worker->cur_work, data->caller_data)) {
832 send_sig(SIGINT, worker->task, 1);
835 spin_unlock_irqrestore(&worker->lock, flags);
840 static enum io_wq_cancel io_wqe_cancel_cb_work(struct io_wqe *wqe,
841 work_cancel_fn *cancel,
844 struct io_cb_cancel_data data = {
847 .caller_data = cancel_data,
849 struct io_wq_work_node *node, *prev;
850 struct io_wq_work *work;
854 spin_lock_irqsave(&wqe->lock, flags);
855 wq_list_for_each(node, prev, &wqe->work_list) {
856 work = container_of(node, struct io_wq_work, list);
858 if (cancel(work, cancel_data)) {
859 wq_node_del(&wqe->work_list, node, prev);
864 spin_unlock_irqrestore(&wqe->lock, flags);
867 work->flags |= IO_WQ_WORK_CANCEL;
869 return IO_WQ_CANCEL_OK;
873 found = io_wq_for_each_worker(wqe, io_work_cancel, &data);
875 return found ? IO_WQ_CANCEL_RUNNING : IO_WQ_CANCEL_NOTFOUND;
878 enum io_wq_cancel io_wq_cancel_cb(struct io_wq *wq, work_cancel_fn *cancel,
881 enum io_wq_cancel ret = IO_WQ_CANCEL_NOTFOUND;
884 for_each_node(node) {
885 struct io_wqe *wqe = wq->wqes[node];
887 ret = io_wqe_cancel_cb_work(wqe, cancel, data);
888 if (ret != IO_WQ_CANCEL_NOTFOUND)
895 static bool io_wq_worker_cancel(struct io_worker *worker, void *data)
897 struct io_wq_work *work = data;
901 if (worker->cur_work != work)
904 spin_lock_irqsave(&worker->lock, flags);
905 if (worker->cur_work == work) {
906 send_sig(SIGINT, worker->task, 1);
909 spin_unlock_irqrestore(&worker->lock, flags);
914 static enum io_wq_cancel io_wqe_cancel_work(struct io_wqe *wqe,
915 struct io_wq_work *cwork)
917 struct io_wq_work_node *node, *prev;
918 struct io_wq_work *work;
922 cwork->flags |= IO_WQ_WORK_CANCEL;
925 * First check pending list, if we're lucky we can just remove it
926 * from there. CANCEL_OK means that the work is returned as-new,
927 * no completion will be posted for it.
929 spin_lock_irqsave(&wqe->lock, flags);
930 wq_list_for_each(node, prev, &wqe->work_list) {
931 work = container_of(node, struct io_wq_work, list);
934 wq_node_del(&wqe->work_list, node, prev);
939 spin_unlock_irqrestore(&wqe->lock, flags);
942 work->flags |= IO_WQ_WORK_CANCEL;
944 return IO_WQ_CANCEL_OK;
948 * Now check if a free (going busy) or busy worker has the work
949 * currently running. If we find it there, we'll return CANCEL_RUNNING
950 * as an indication that we attempt to signal cancellation. The
951 * completion will run normally in this case.
954 found = io_wq_for_each_worker(wqe, io_wq_worker_cancel, cwork);
956 return found ? IO_WQ_CANCEL_RUNNING : IO_WQ_CANCEL_NOTFOUND;
959 enum io_wq_cancel io_wq_cancel_work(struct io_wq *wq, struct io_wq_work *cwork)
961 enum io_wq_cancel ret = IO_WQ_CANCEL_NOTFOUND;
964 for_each_node(node) {
965 struct io_wqe *wqe = wq->wqes[node];
967 ret = io_wqe_cancel_work(wqe, cwork);
968 if (ret != IO_WQ_CANCEL_NOTFOUND)
975 struct io_wq_flush_data {
976 struct io_wq_work work;
977 struct completion done;
980 static void io_wq_flush_func(struct io_wq_work **workptr)
982 struct io_wq_work *work = *workptr;
983 struct io_wq_flush_data *data;
985 data = container_of(work, struct io_wq_flush_data, work);
986 complete(&data->done);
990 * Doesn't wait for previously queued work to finish. When this completes,
991 * it just means that previously queued work was started.
993 void io_wq_flush(struct io_wq *wq)
995 struct io_wq_flush_data data;
998 for_each_node(node) {
999 struct io_wqe *wqe = wq->wqes[node];
1001 init_completion(&data.done);
1002 INIT_IO_WORK(&data.work, io_wq_flush_func);
1003 data.work.flags |= IO_WQ_WORK_INTERNAL;
1004 io_wqe_enqueue(wqe, &data.work);
1005 wait_for_completion(&data.done);
1009 struct io_wq *io_wq_create(unsigned bounded, struct io_wq_data *data)
1011 int ret = -ENOMEM, node;
1014 wq = kzalloc(sizeof(*wq), GFP_KERNEL);
1016 return ERR_PTR(-ENOMEM);
1018 wq->wqes = kcalloc(nr_node_ids, sizeof(struct io_wqe *), GFP_KERNEL);
1021 return ERR_PTR(-ENOMEM);
1024 wq->get_work = data->get_work;
1025 wq->put_work = data->put_work;
1027 /* caller must already hold a reference to this */
1028 wq->user = data->user;
1029 wq->creds = data->creds;
1031 for_each_node(node) {
1034 wqe = kzalloc_node(sizeof(struct io_wqe), GFP_KERNEL, node);
1037 wq->wqes[node] = wqe;
1039 wqe->acct[IO_WQ_ACCT_BOUND].max_workers = bounded;
1040 atomic_set(&wqe->acct[IO_WQ_ACCT_BOUND].nr_running, 0);
1042 wqe->acct[IO_WQ_ACCT_UNBOUND].max_workers =
1043 task_rlimit(current, RLIMIT_NPROC);
1045 atomic_set(&wqe->acct[IO_WQ_ACCT_UNBOUND].nr_running, 0);
1048 spin_lock_init(&wqe->lock);
1049 INIT_WQ_LIST(&wqe->work_list);
1050 INIT_HLIST_NULLS_HEAD(&wqe->free_list, 0);
1051 INIT_LIST_HEAD(&wqe->all_list);
1054 init_completion(&wq->done);
1056 /* caller must have already done mmgrab() on this mm */
1059 wq->manager = kthread_create(io_wq_manager, wq, "io_wq_manager");
1060 if (!IS_ERR(wq->manager)) {
1061 wake_up_process(wq->manager);
1062 wait_for_completion(&wq->done);
1063 if (test_bit(IO_WQ_BIT_ERROR, &wq->state)) {
1067 reinit_completion(&wq->done);
1071 ret = PTR_ERR(wq->manager);
1072 complete(&wq->done);
1075 kfree(wq->wqes[node]);
1078 return ERR_PTR(ret);
1081 static bool io_wq_worker_wake(struct io_worker *worker, void *data)
1083 wake_up_process(worker->task);
1087 void io_wq_destroy(struct io_wq *wq)
1091 set_bit(IO_WQ_BIT_EXIT, &wq->state);
1093 kthread_stop(wq->manager);
1097 io_wq_for_each_worker(wq->wqes[node], io_wq_worker_wake, NULL);
1100 wait_for_completion(&wq->done);
1103 kfree(wq->wqes[node]);