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 */
39 IO_WQE_FLAG_STALLED = 1, /* stalled on hash */
43 * One for each thread in a wqe pool
48 struct hlist_nulls_node nulls_node;
49 struct list_head all_list;
50 struct task_struct *task;
51 wait_queue_head_t wait;
54 struct io_wq_work *cur_work;
59 struct files_struct *restore_files;
62 #if BITS_PER_LONG == 64
63 #define IO_WQ_HASH_ORDER 6
65 #define IO_WQ_HASH_ORDER 5
80 * Per-node worker thread pool
85 struct list_head work_list;
86 unsigned long hash_map;
88 } ____cacheline_aligned_in_smp;
91 struct io_wqe_acct acct[2];
93 struct hlist_nulls_head free_list;
94 struct hlist_nulls_head busy_list;
95 struct list_head all_list;
104 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 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;
138 if (current->files != worker->restore_files) {
139 __acquire(&wqe->lock);
140 spin_unlock_irq(&wqe->lock);
144 current->files = worker->restore_files;
145 task_unlock(current);
149 * If we have an active mm, we need to drop the wq lock before unusing
150 * it. If we do, return true and let the caller retry the idle loop.
154 __acquire(&wqe->lock);
155 spin_unlock_irq(&wqe->lock);
158 __set_current_state(TASK_RUNNING);
160 unuse_mm(worker->mm);
168 static inline struct io_wqe_acct *io_work_get_acct(struct io_wqe *wqe,
169 struct io_wq_work *work)
171 if (work->flags & IO_WQ_WORK_UNBOUND)
172 return &wqe->acct[IO_WQ_ACCT_UNBOUND];
174 return &wqe->acct[IO_WQ_ACCT_BOUND];
177 static inline struct io_wqe_acct *io_wqe_get_acct(struct io_wqe *wqe,
178 struct io_worker *worker)
180 if (worker->flags & IO_WORKER_F_BOUND)
181 return &wqe->acct[IO_WQ_ACCT_BOUND];
183 return &wqe->acct[IO_WQ_ACCT_UNBOUND];
186 static void io_worker_exit(struct io_worker *worker)
188 struct io_wqe *wqe = worker->wqe;
189 struct io_wqe_acct *acct = io_wqe_get_acct(wqe, worker);
193 * If we're not at zero, someone else is holding a brief reference
194 * to the worker. Wait for that to go away.
196 set_current_state(TASK_INTERRUPTIBLE);
197 if (!refcount_dec_and_test(&worker->ref))
199 __set_current_state(TASK_RUNNING);
202 current->flags &= ~PF_IO_WORKER;
203 if (worker->flags & IO_WORKER_F_RUNNING)
204 atomic_dec(&acct->nr_running);
205 if (!(worker->flags & IO_WORKER_F_BOUND))
206 atomic_dec(&wqe->wq->user->processes);
210 spin_lock_irq(&wqe->lock);
211 hlist_nulls_del_rcu(&worker->nulls_node);
212 list_del_rcu(&worker->all_list);
213 if (__io_worker_unuse(wqe, worker)) {
214 __release(&wqe->lock);
215 spin_lock_irq(&wqe->lock);
218 nr_workers = wqe->acct[IO_WQ_ACCT_BOUND].nr_workers +
219 wqe->acct[IO_WQ_ACCT_UNBOUND].nr_workers;
220 spin_unlock_irq(&wqe->lock);
222 /* all workers gone, wq exit can proceed */
223 if (!nr_workers && refcount_dec_and_test(&wqe->wq->refs))
224 complete(&wqe->wq->done);
226 kfree_rcu(worker, rcu);
229 static inline bool io_wqe_run_queue(struct io_wqe *wqe)
230 __must_hold(wqe->lock)
232 if (!list_empty(&wqe->work_list) && !(wqe->flags & IO_WQE_FLAG_STALLED))
238 * Check head of free list for an available worker. If one isn't available,
239 * caller must wake up the wq manager to create one.
241 static bool io_wqe_activate_free_worker(struct io_wqe *wqe)
244 struct hlist_nulls_node *n;
245 struct io_worker *worker;
247 n = rcu_dereference(hlist_nulls_first_rcu(&wqe->free_list));
251 worker = hlist_nulls_entry(n, struct io_worker, nulls_node);
252 if (io_worker_get(worker)) {
253 wake_up(&worker->wait);
254 io_worker_release(worker);
262 * We need a worker. If we find a free one, we're good. If not, and we're
263 * below the max number of workers, wake up the manager to create one.
265 static void io_wqe_wake_worker(struct io_wqe *wqe, struct io_wqe_acct *acct)
270 * Most likely an attempt to queue unbounded work on an io_wq that
271 * wasn't setup with any unbounded workers.
273 WARN_ON_ONCE(!acct->max_workers);
276 ret = io_wqe_activate_free_worker(wqe);
279 if (!ret && acct->nr_workers < acct->max_workers)
280 wake_up_process(wqe->wq->manager);
283 static void io_wqe_inc_running(struct io_wqe *wqe, struct io_worker *worker)
285 struct io_wqe_acct *acct = io_wqe_get_acct(wqe, worker);
287 atomic_inc(&acct->nr_running);
290 static void io_wqe_dec_running(struct io_wqe *wqe, struct io_worker *worker)
291 __must_hold(wqe->lock)
293 struct io_wqe_acct *acct = io_wqe_get_acct(wqe, worker);
295 if (atomic_dec_and_test(&acct->nr_running) && io_wqe_run_queue(wqe))
296 io_wqe_wake_worker(wqe, acct);
299 static void io_worker_start(struct io_wqe *wqe, struct io_worker *worker)
301 allow_kernel_signal(SIGINT);
303 current->flags |= PF_IO_WORKER;
305 worker->flags |= (IO_WORKER_F_UP | IO_WORKER_F_RUNNING);
306 worker->restore_files = current->files;
307 io_wqe_inc_running(wqe, worker);
311 * Worker will start processing some work. Move it to the busy list, if
312 * it's currently on the freelist
314 static void __io_worker_busy(struct io_wqe *wqe, struct io_worker *worker,
315 struct io_wq_work *work)
316 __must_hold(wqe->lock)
318 bool worker_bound, work_bound;
320 if (worker->flags & IO_WORKER_F_FREE) {
321 worker->flags &= ~IO_WORKER_F_FREE;
322 hlist_nulls_del_init_rcu(&worker->nulls_node);
323 hlist_nulls_add_head_rcu(&worker->nulls_node, &wqe->busy_list);
327 * If worker is moving from bound to unbound (or vice versa), then
328 * ensure we update the running accounting.
330 worker_bound = (worker->flags & IO_WORKER_F_BOUND) != 0;
331 work_bound = (work->flags & IO_WQ_WORK_UNBOUND) == 0;
332 if (worker_bound != work_bound) {
333 io_wqe_dec_running(wqe, worker);
335 worker->flags |= IO_WORKER_F_BOUND;
336 wqe->acct[IO_WQ_ACCT_UNBOUND].nr_workers--;
337 wqe->acct[IO_WQ_ACCT_BOUND].nr_workers++;
338 atomic_dec(&wqe->wq->user->processes);
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_inc(&wqe->wq->user->processes);
345 io_wqe_inc_running(wqe, worker);
350 * No work, worker going to sleep. Move to freelist, and unuse mm if we
351 * have one attached. Dropping the mm may potentially sleep, so we drop
352 * the lock in that case and return success. Since the caller has to
353 * retry the loop in that case (we changed task state), we don't regrab
354 * the lock if we return success.
356 static bool __io_worker_idle(struct io_wqe *wqe, struct io_worker *worker)
357 __must_hold(wqe->lock)
359 if (!(worker->flags & IO_WORKER_F_FREE)) {
360 worker->flags |= IO_WORKER_F_FREE;
361 hlist_nulls_del_init_rcu(&worker->nulls_node);
362 hlist_nulls_add_head_rcu(&worker->nulls_node, &wqe->free_list);
365 return __io_worker_unuse(wqe, worker);
368 static struct io_wq_work *io_get_next_work(struct io_wqe *wqe, unsigned *hash)
369 __must_hold(wqe->lock)
371 struct io_wq_work *work;
373 list_for_each_entry(work, &wqe->work_list, list) {
374 /* not hashed, can run anytime */
375 if (!(work->flags & IO_WQ_WORK_HASHED)) {
376 list_del(&work->list);
380 /* hashed, can run if not already running */
381 *hash = work->flags >> IO_WQ_HASH_SHIFT;
382 if (!(wqe->hash_map & BIT_ULL(*hash))) {
383 wqe->hash_map |= BIT_ULL(*hash);
384 list_del(&work->list);
392 static void io_worker_handle_work(struct io_worker *worker)
393 __releases(wqe->lock)
395 struct io_wq_work *work, *old_work = NULL, *put_work = NULL;
396 struct io_wqe *wqe = worker->wqe;
397 struct io_wq *wq = wqe->wq;
403 * If we got some work, mark us as busy. If we didn't, but
404 * the list isn't empty, it means we stalled on hashed work.
405 * Mark us stalled so we don't keep looking for work when we
406 * can't make progress, any work completion or insertion will
407 * clear the stalled flag.
409 work = io_get_next_work(wqe, &hash);
411 __io_worker_busy(wqe, worker, work);
412 else if (!list_empty(&wqe->work_list))
413 wqe->flags |= IO_WQE_FLAG_STALLED;
415 spin_unlock_irq(&wqe->lock);
416 if (put_work && wq->put_work)
417 wq->put_work(old_work);
421 /* flush any pending signals before assigning new work */
422 if (signal_pending(current))
423 flush_signals(current);
425 spin_lock_irq(&worker->lock);
426 worker->cur_work = work;
427 spin_unlock_irq(&worker->lock);
429 if ((work->flags & IO_WQ_WORK_NEEDS_FILES) &&
430 current->files != work->files) {
432 current->files = work->files;
433 task_unlock(current);
435 if ((work->flags & IO_WQ_WORK_NEEDS_USER) && !worker->mm &&
436 wq->mm && mmget_not_zero(wq->mm)) {
441 if (test_bit(IO_WQ_BIT_CANCEL, &wq->state))
442 work->flags |= IO_WQ_WORK_CANCEL;
444 work->flags |= IO_WQ_WORK_HAS_MM;
446 if (wq->get_work && !(work->flags & IO_WQ_WORK_INTERNAL)) {
454 spin_lock_irq(&worker->lock);
455 worker->cur_work = NULL;
456 spin_unlock_irq(&worker->lock);
458 spin_lock_irq(&wqe->lock);
461 wqe->hash_map &= ~BIT_ULL(hash);
462 wqe->flags &= ~IO_WQE_FLAG_STALLED;
464 if (work && work != old_work) {
465 spin_unlock_irq(&wqe->lock);
467 if (put_work && wq->put_work) {
468 wq->put_work(put_work);
472 /* dependent work not hashed */
479 static int io_wqe_worker(void *data)
481 struct io_worker *worker = data;
482 struct io_wqe *wqe = worker->wqe;
483 struct io_wq *wq = wqe->wq;
486 io_worker_start(wqe, worker);
488 while (!test_bit(IO_WQ_BIT_EXIT, &wq->state)) {
489 prepare_to_wait(&worker->wait, &wait, TASK_INTERRUPTIBLE);
491 spin_lock_irq(&wqe->lock);
492 if (io_wqe_run_queue(wqe)) {
493 __set_current_state(TASK_RUNNING);
494 io_worker_handle_work(worker);
497 /* drops the lock on success, retry */
498 if (__io_worker_idle(wqe, worker)) {
499 __release(&wqe->lock);
502 spin_unlock_irq(&wqe->lock);
503 if (signal_pending(current))
504 flush_signals(current);
505 if (schedule_timeout(WORKER_IDLE_TIMEOUT))
507 /* timed out, exit unless we're the fixed worker */
508 if (test_bit(IO_WQ_BIT_EXIT, &wq->state) ||
509 !(worker->flags & IO_WORKER_F_FIXED))
513 finish_wait(&worker->wait, &wait);
515 if (test_bit(IO_WQ_BIT_EXIT, &wq->state)) {
516 spin_lock_irq(&wqe->lock);
517 if (!list_empty(&wqe->work_list))
518 io_worker_handle_work(worker);
520 spin_unlock_irq(&wqe->lock);
523 io_worker_exit(worker);
528 * Called when a worker is scheduled in. Mark us as currently running.
530 void io_wq_worker_running(struct task_struct *tsk)
532 struct io_worker *worker = kthread_data(tsk);
533 struct io_wqe *wqe = worker->wqe;
535 if (!(worker->flags & IO_WORKER_F_UP))
537 if (worker->flags & IO_WORKER_F_RUNNING)
539 worker->flags |= IO_WORKER_F_RUNNING;
540 io_wqe_inc_running(wqe, worker);
544 * Called when worker is going to sleep. If there are no workers currently
545 * running and we have work pending, wake up a free one or have the manager
548 void io_wq_worker_sleeping(struct task_struct *tsk)
550 struct io_worker *worker = kthread_data(tsk);
551 struct io_wqe *wqe = worker->wqe;
553 if (!(worker->flags & IO_WORKER_F_UP))
555 if (!(worker->flags & IO_WORKER_F_RUNNING))
558 worker->flags &= ~IO_WORKER_F_RUNNING;
560 spin_lock_irq(&wqe->lock);
561 io_wqe_dec_running(wqe, worker);
562 spin_unlock_irq(&wqe->lock);
565 static void create_io_worker(struct io_wq *wq, struct io_wqe *wqe, int index)
567 struct io_wqe_acct *acct =&wqe->acct[index];
568 struct io_worker *worker;
570 worker = kcalloc_node(1, sizeof(*worker), GFP_KERNEL, wqe->node);
574 refcount_set(&worker->ref, 1);
575 worker->nulls_node.pprev = NULL;
576 init_waitqueue_head(&worker->wait);
578 spin_lock_init(&worker->lock);
580 worker->task = kthread_create_on_node(io_wqe_worker, worker, wqe->node,
581 "io_wqe_worker-%d/%d", index, wqe->node);
582 if (IS_ERR(worker->task)) {
587 spin_lock_irq(&wqe->lock);
588 hlist_nulls_add_head_rcu(&worker->nulls_node, &wqe->free_list);
589 list_add_tail_rcu(&worker->all_list, &wqe->all_list);
590 worker->flags |= IO_WORKER_F_FREE;
591 if (index == IO_WQ_ACCT_BOUND)
592 worker->flags |= IO_WORKER_F_BOUND;
593 if (!acct->nr_workers && (worker->flags & IO_WORKER_F_BOUND))
594 worker->flags |= IO_WORKER_F_FIXED;
596 spin_unlock_irq(&wqe->lock);
598 if (index == IO_WQ_ACCT_UNBOUND)
599 atomic_inc(&wq->user->processes);
601 wake_up_process(worker->task);
604 static inline bool io_wqe_need_worker(struct io_wqe *wqe, int index)
605 __must_hold(wqe->lock)
607 struct io_wqe_acct *acct = &wqe->acct[index];
609 /* always ensure we have one bounded worker */
610 if (index == IO_WQ_ACCT_BOUND && !acct->nr_workers)
612 /* if we have available workers or no work, no need */
613 if (!hlist_nulls_empty(&wqe->free_list) || !io_wqe_run_queue(wqe))
615 return acct->nr_workers < acct->max_workers;
619 * Manager thread. Tasked with creating new workers, if we need them.
621 static int io_wq_manager(void *data)
623 struct io_wq *wq = data;
625 while (!kthread_should_stop()) {
628 for (i = 0; i < wq->nr_wqes; i++) {
629 struct io_wqe *wqe = wq->wqes[i];
630 bool fork_worker[2] = { false, false };
632 spin_lock_irq(&wqe->lock);
633 if (io_wqe_need_worker(wqe, IO_WQ_ACCT_BOUND))
634 fork_worker[IO_WQ_ACCT_BOUND] = true;
635 if (io_wqe_need_worker(wqe, IO_WQ_ACCT_UNBOUND))
636 fork_worker[IO_WQ_ACCT_UNBOUND] = true;
637 spin_unlock_irq(&wqe->lock);
638 if (fork_worker[IO_WQ_ACCT_BOUND])
639 create_io_worker(wq, wqe, IO_WQ_ACCT_BOUND);
640 if (fork_worker[IO_WQ_ACCT_UNBOUND])
641 create_io_worker(wq, wqe, IO_WQ_ACCT_UNBOUND);
643 set_current_state(TASK_INTERRUPTIBLE);
644 schedule_timeout(HZ);
650 static bool io_wq_can_queue(struct io_wqe *wqe, struct io_wqe_acct *acct,
651 struct io_wq_work *work)
655 if (!(work->flags & IO_WQ_WORK_UNBOUND))
657 if (atomic_read(&acct->nr_running))
661 free_worker = !hlist_nulls_empty(&wqe->free_list);
666 if (atomic_read(&wqe->wq->user->processes) >= acct->max_workers &&
667 !(capable(CAP_SYS_RESOURCE) || capable(CAP_SYS_ADMIN)))
673 static void io_wqe_enqueue(struct io_wqe *wqe, struct io_wq_work *work)
675 struct io_wqe_acct *acct = io_work_get_acct(wqe, work);
679 * Do early check to see if we need a new unbound worker, and if we do,
680 * if we're allowed to do so. This isn't 100% accurate as there's a
681 * gap between this check and incrementing the value, but that's OK.
682 * It's close enough to not be an issue, fork() has the same delay.
684 if (unlikely(!io_wq_can_queue(wqe, acct, work))) {
685 work->flags |= IO_WQ_WORK_CANCEL;
690 spin_lock_irqsave(&wqe->lock, flags);
691 list_add_tail(&work->list, &wqe->work_list);
692 wqe->flags &= ~IO_WQE_FLAG_STALLED;
693 spin_unlock_irqrestore(&wqe->lock, flags);
695 if (!atomic_read(&acct->nr_running))
696 io_wqe_wake_worker(wqe, acct);
699 void io_wq_enqueue(struct io_wq *wq, struct io_wq_work *work)
701 struct io_wqe *wqe = wq->wqes[numa_node_id()];
703 io_wqe_enqueue(wqe, work);
707 * Enqueue work, hashed by some key. Work items that hash to the same value
708 * will not be done in parallel. Used to limit concurrent writes, generally
711 void io_wq_enqueue_hashed(struct io_wq *wq, struct io_wq_work *work, void *val)
713 struct io_wqe *wqe = wq->wqes[numa_node_id()];
717 bit = hash_ptr(val, IO_WQ_HASH_ORDER);
718 work->flags |= (IO_WQ_WORK_HASHED | (bit << IO_WQ_HASH_SHIFT));
719 io_wqe_enqueue(wqe, work);
722 static bool io_wqe_worker_send_sig(struct io_worker *worker, void *data)
724 send_sig(SIGINT, worker->task, 1);
729 * Iterate the passed in list and call the specific function for each
730 * worker that isn't exiting
732 static bool io_wq_for_each_worker(struct io_wqe *wqe,
733 bool (*func)(struct io_worker *, void *),
736 struct io_worker *worker;
739 list_for_each_entry_rcu(worker, &wqe->all_list, all_list) {
740 if (io_worker_get(worker)) {
741 ret = func(worker, data);
742 io_worker_release(worker);
751 void io_wq_cancel_all(struct io_wq *wq)
755 set_bit(IO_WQ_BIT_CANCEL, &wq->state);
758 * Browse both lists, as there's a gap between handing work off
759 * to a worker and the worker putting itself on the busy_list
762 for (i = 0; i < wq->nr_wqes; i++) {
763 struct io_wqe *wqe = wq->wqes[i];
765 io_wq_for_each_worker(wqe, io_wqe_worker_send_sig, NULL);
770 struct io_cb_cancel_data {
772 work_cancel_fn *cancel;
776 static bool io_work_cancel(struct io_worker *worker, void *cancel_data)
778 struct io_cb_cancel_data *data = cancel_data;
783 * Hold the lock to avoid ->cur_work going out of scope, caller
784 * may dereference the passed in work.
786 spin_lock_irqsave(&worker->lock, flags);
787 if (worker->cur_work &&
788 data->cancel(worker->cur_work, data->caller_data)) {
789 send_sig(SIGINT, worker->task, 1);
792 spin_unlock_irqrestore(&worker->lock, flags);
797 static enum io_wq_cancel io_wqe_cancel_cb_work(struct io_wqe *wqe,
798 work_cancel_fn *cancel,
801 struct io_cb_cancel_data data = {
804 .caller_data = cancel_data,
806 struct io_wq_work *work;
810 spin_lock_irqsave(&wqe->lock, flags);
811 list_for_each_entry(work, &wqe->work_list, list) {
812 if (cancel(work, cancel_data)) {
813 list_del(&work->list);
818 spin_unlock_irqrestore(&wqe->lock, flags);
821 work->flags |= IO_WQ_WORK_CANCEL;
823 return IO_WQ_CANCEL_OK;
827 found = io_wq_for_each_worker(wqe, io_work_cancel, &data);
829 return found ? IO_WQ_CANCEL_RUNNING : IO_WQ_CANCEL_NOTFOUND;
832 enum io_wq_cancel io_wq_cancel_cb(struct io_wq *wq, work_cancel_fn *cancel,
835 enum io_wq_cancel ret = IO_WQ_CANCEL_NOTFOUND;
838 for (i = 0; i < wq->nr_wqes; i++) {
839 struct io_wqe *wqe = wq->wqes[i];
841 ret = io_wqe_cancel_cb_work(wqe, cancel, data);
842 if (ret != IO_WQ_CANCEL_NOTFOUND)
849 static bool io_wq_worker_cancel(struct io_worker *worker, void *data)
851 struct io_wq_work *work = data;
855 if (worker->cur_work != work)
858 spin_lock_irqsave(&worker->lock, flags);
859 if (worker->cur_work == work) {
860 send_sig(SIGINT, worker->task, 1);
863 spin_unlock_irqrestore(&worker->lock, flags);
868 static enum io_wq_cancel io_wqe_cancel_work(struct io_wqe *wqe,
869 struct io_wq_work *cwork)
871 struct io_wq_work *work;
875 cwork->flags |= IO_WQ_WORK_CANCEL;
878 * First check pending list, if we're lucky we can just remove it
879 * from there. CANCEL_OK means that the work is returned as-new,
880 * no completion will be posted for it.
882 spin_lock_irqsave(&wqe->lock, flags);
883 list_for_each_entry(work, &wqe->work_list, list) {
885 list_del(&work->list);
890 spin_unlock_irqrestore(&wqe->lock, flags);
893 work->flags |= IO_WQ_WORK_CANCEL;
895 return IO_WQ_CANCEL_OK;
899 * Now check if a free (going busy) or busy worker has the work
900 * currently running. If we find it there, we'll return CANCEL_RUNNING
901 * as an indication that we attempte to signal cancellation. The
902 * completion will run normally in this case.
905 found = io_wq_for_each_worker(wqe, io_wq_worker_cancel, cwork);
907 return found ? IO_WQ_CANCEL_RUNNING : IO_WQ_CANCEL_NOTFOUND;
910 enum io_wq_cancel io_wq_cancel_work(struct io_wq *wq, struct io_wq_work *cwork)
912 enum io_wq_cancel ret = IO_WQ_CANCEL_NOTFOUND;
915 for (i = 0; i < wq->nr_wqes; i++) {
916 struct io_wqe *wqe = wq->wqes[i];
918 ret = io_wqe_cancel_work(wqe, cwork);
919 if (ret != IO_WQ_CANCEL_NOTFOUND)
926 struct io_wq_flush_data {
927 struct io_wq_work work;
928 struct completion done;
931 static void io_wq_flush_func(struct io_wq_work **workptr)
933 struct io_wq_work *work = *workptr;
934 struct io_wq_flush_data *data;
936 data = container_of(work, struct io_wq_flush_data, work);
937 complete(&data->done);
941 * Doesn't wait for previously queued work to finish. When this completes,
942 * it just means that previously queued work was started.
944 void io_wq_flush(struct io_wq *wq)
946 struct io_wq_flush_data data;
949 for (i = 0; i < wq->nr_wqes; i++) {
950 struct io_wqe *wqe = wq->wqes[i];
952 init_completion(&data.done);
953 INIT_IO_WORK(&data.work, io_wq_flush_func);
954 data.work.flags |= IO_WQ_WORK_INTERNAL;
955 io_wqe_enqueue(wqe, &data.work);
956 wait_for_completion(&data.done);
960 struct io_wq *io_wq_create(unsigned bounded, struct mm_struct *mm,
961 struct user_struct *user, get_work_fn *get_work,
962 put_work_fn *put_work)
964 int ret = -ENOMEM, i, node;
967 wq = kcalloc(1, sizeof(*wq), GFP_KERNEL);
969 return ERR_PTR(-ENOMEM);
971 wq->nr_wqes = num_online_nodes();
972 wq->wqes = kcalloc(wq->nr_wqes, sizeof(struct io_wqe *), GFP_KERNEL);
975 return ERR_PTR(-ENOMEM);
978 wq->get_work = get_work;
979 wq->put_work = put_work;
981 /* caller must already hold a reference to this */
985 refcount_set(&wq->refs, wq->nr_wqes);
986 for_each_online_node(node) {
989 wqe = kcalloc_node(1, sizeof(struct io_wqe), GFP_KERNEL, node);
994 wqe->acct[IO_WQ_ACCT_BOUND].max_workers = bounded;
995 atomic_set(&wqe->acct[IO_WQ_ACCT_BOUND].nr_running, 0);
997 wqe->acct[IO_WQ_ACCT_UNBOUND].max_workers =
998 task_rlimit(current, RLIMIT_NPROC);
1000 atomic_set(&wqe->acct[IO_WQ_ACCT_UNBOUND].nr_running, 0);
1003 spin_lock_init(&wqe->lock);
1004 INIT_LIST_HEAD(&wqe->work_list);
1005 INIT_HLIST_NULLS_HEAD(&wqe->free_list, 0);
1006 INIT_HLIST_NULLS_HEAD(&wqe->busy_list, 1);
1007 INIT_LIST_HEAD(&wqe->all_list);
1012 init_completion(&wq->done);
1014 if (i != wq->nr_wqes)
1017 /* caller must have already done mmgrab() on this mm */
1020 wq->manager = kthread_create(io_wq_manager, wq, "io_wq_manager");
1021 if (!IS_ERR(wq->manager)) {
1022 wake_up_process(wq->manager);
1026 ret = PTR_ERR(wq->manager);
1029 complete(&wq->done);
1031 return ERR_PTR(ret);
1034 static bool io_wq_worker_wake(struct io_worker *worker, void *data)
1036 wake_up_process(worker->task);
1040 void io_wq_destroy(struct io_wq *wq)
1045 set_bit(IO_WQ_BIT_EXIT, &wq->state);
1046 kthread_stop(wq->manager);
1050 for (i = 0; i < wq->nr_wqes; i++) {
1051 struct io_wqe *wqe = wq->wqes[i];
1055 io_wq_for_each_worker(wqe, io_wq_worker_wake, NULL);
1059 wait_for_completion(&wq->done);
1061 for (i = 0; i < wq->nr_wqes; i++)