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>
12 #include <linux/percpu.h>
13 #include <linux/slab.h>
14 #include <linux/rculist_nulls.h>
15 #include <linux/cpu.h>
16 #include <linux/task_work.h>
17 #include <linux/audit.h>
18 #include <uapi/linux/io_uring.h>
23 #define WORKER_IDLE_TIMEOUT (5 * HZ)
26 IO_WORKER_F_UP = 1, /* up and active */
27 IO_WORKER_F_RUNNING = 2, /* account as running */
28 IO_WORKER_F_FREE = 4, /* worker on free list */
29 IO_WORKER_F_BOUND = 8, /* is doing bounded work */
33 IO_WQ_BIT_EXIT = 0, /* wq exiting */
37 IO_ACCT_STALLED_BIT = 0, /* stalled on hash */
41 * One for each thread in a wqe pool
46 struct hlist_nulls_node nulls_node;
47 struct list_head all_list;
48 struct task_struct *task;
51 struct io_wq_work *cur_work;
52 struct io_wq_work *next_work;
55 struct completion ref_done;
57 unsigned long create_state;
58 struct callback_head create_work;
63 struct work_struct work;
67 #if BITS_PER_LONG == 64
68 #define IO_WQ_HASH_ORDER 6
70 #define IO_WQ_HASH_ORDER 5
73 #define IO_WQ_NR_HASH_BUCKETS (1u << IO_WQ_HASH_ORDER)
81 struct io_wq_work_list work_list;
92 * Per-node worker thread pool
96 struct io_wqe_acct acct[IO_WQ_ACCT_NR];
100 struct hlist_nulls_head free_list;
101 struct list_head all_list;
103 struct wait_queue_entry wait;
106 struct io_wq_work *hash_tail[IO_WQ_NR_HASH_BUCKETS];
108 cpumask_var_t cpu_mask;
117 free_work_fn *free_work;
118 io_wq_work_fn *do_work;
120 struct io_wq_hash *hash;
122 atomic_t worker_refs;
123 struct completion worker_done;
125 struct hlist_node cpuhp_node;
127 struct task_struct *task;
129 struct io_wqe *wqes[];
132 static enum cpuhp_state io_wq_online;
134 struct io_cb_cancel_data {
142 static bool create_io_worker(struct io_wq *wq, struct io_wqe *wqe, int index);
143 static void io_wqe_dec_running(struct io_worker *worker);
144 static bool io_acct_cancel_pending_work(struct io_wqe *wqe,
145 struct io_wqe_acct *acct,
146 struct io_cb_cancel_data *match);
147 static void create_worker_cb(struct callback_head *cb);
148 static void io_wq_cancel_tw_create(struct io_wq *wq);
150 static bool io_worker_get(struct io_worker *worker)
152 return refcount_inc_not_zero(&worker->ref);
155 static void io_worker_release(struct io_worker *worker)
157 if (refcount_dec_and_test(&worker->ref))
158 complete(&worker->ref_done);
161 static inline struct io_wqe_acct *io_get_acct(struct io_wqe *wqe, bool bound)
163 return &wqe->acct[bound ? IO_WQ_ACCT_BOUND : IO_WQ_ACCT_UNBOUND];
166 static inline struct io_wqe_acct *io_work_get_acct(struct io_wqe *wqe,
167 struct io_wq_work *work)
169 return io_get_acct(wqe, !(work->flags & IO_WQ_WORK_UNBOUND));
172 static inline struct io_wqe_acct *io_wqe_get_acct(struct io_worker *worker)
174 return io_get_acct(worker->wqe, worker->flags & IO_WORKER_F_BOUND);
177 static void io_worker_ref_put(struct io_wq *wq)
179 if (atomic_dec_and_test(&wq->worker_refs))
180 complete(&wq->worker_done);
183 static void io_worker_cancel_cb(struct io_worker *worker)
185 struct io_wqe_acct *acct = io_wqe_get_acct(worker);
186 struct io_wqe *wqe = worker->wqe;
187 struct io_wq *wq = wqe->wq;
189 atomic_dec(&acct->nr_running);
190 raw_spin_lock(&worker->wqe->lock);
192 raw_spin_unlock(&worker->wqe->lock);
193 io_worker_ref_put(wq);
194 clear_bit_unlock(0, &worker->create_state);
195 io_worker_release(worker);
198 static bool io_task_worker_match(struct callback_head *cb, void *data)
200 struct io_worker *worker;
202 if (cb->func != create_worker_cb)
204 worker = container_of(cb, struct io_worker, create_work);
205 return worker == data;
208 static void io_worker_exit(struct io_worker *worker)
210 struct io_wqe *wqe = worker->wqe;
211 struct io_wq *wq = wqe->wq;
214 struct callback_head *cb = task_work_cancel_match(wq->task,
215 io_task_worker_match, worker);
219 io_worker_cancel_cb(worker);
222 io_worker_release(worker);
223 wait_for_completion(&worker->ref_done);
225 raw_spin_lock(&wqe->lock);
226 if (worker->flags & IO_WORKER_F_FREE)
227 hlist_nulls_del_rcu(&worker->nulls_node);
228 list_del_rcu(&worker->all_list);
229 raw_spin_unlock(&wqe->lock);
230 io_wqe_dec_running(worker);
233 current->flags &= ~PF_IO_WORKER;
236 kfree_rcu(worker, rcu);
237 io_worker_ref_put(wqe->wq);
241 static inline bool io_acct_run_queue(struct io_wqe_acct *acct)
245 raw_spin_lock(&acct->lock);
246 if (!wq_list_empty(&acct->work_list) &&
247 !test_bit(IO_ACCT_STALLED_BIT, &acct->flags))
249 raw_spin_unlock(&acct->lock);
255 * Check head of free list for an available worker. If one isn't available,
256 * caller must create one.
258 static bool io_wqe_activate_free_worker(struct io_wqe *wqe,
259 struct io_wqe_acct *acct)
262 struct hlist_nulls_node *n;
263 struct io_worker *worker;
266 * Iterate free_list and see if we can find an idle worker to
267 * activate. If a given worker is on the free_list but in the process
268 * of exiting, keep trying.
270 hlist_nulls_for_each_entry_rcu(worker, n, &wqe->free_list, nulls_node) {
271 if (!io_worker_get(worker))
273 if (io_wqe_get_acct(worker) != acct) {
274 io_worker_release(worker);
277 if (wake_up_process(worker->task)) {
278 io_worker_release(worker);
281 io_worker_release(worker);
288 * We need a worker. If we find a free one, we're good. If not, and we're
289 * below the max number of workers, create one.
291 static bool io_wqe_create_worker(struct io_wqe *wqe, struct io_wqe_acct *acct)
294 * Most likely an attempt to queue unbounded work on an io_wq that
295 * wasn't setup with any unbounded workers.
297 if (unlikely(!acct->max_workers))
298 pr_warn_once("io-wq is not configured for unbound workers");
300 raw_spin_lock(&wqe->lock);
301 if (acct->nr_workers >= acct->max_workers) {
302 raw_spin_unlock(&wqe->lock);
306 raw_spin_unlock(&wqe->lock);
307 atomic_inc(&acct->nr_running);
308 atomic_inc(&wqe->wq->worker_refs);
309 return create_io_worker(wqe->wq, wqe, acct->index);
312 static void io_wqe_inc_running(struct io_worker *worker)
314 struct io_wqe_acct *acct = io_wqe_get_acct(worker);
316 atomic_inc(&acct->nr_running);
319 static void create_worker_cb(struct callback_head *cb)
321 struct io_worker *worker;
324 struct io_wqe_acct *acct;
325 bool do_create = false;
327 worker = container_of(cb, struct io_worker, create_work);
330 acct = &wqe->acct[worker->create_index];
331 raw_spin_lock(&wqe->lock);
332 if (acct->nr_workers < acct->max_workers) {
336 raw_spin_unlock(&wqe->lock);
338 create_io_worker(wq, wqe, worker->create_index);
340 atomic_dec(&acct->nr_running);
341 io_worker_ref_put(wq);
343 clear_bit_unlock(0, &worker->create_state);
344 io_worker_release(worker);
347 static bool io_queue_worker_create(struct io_worker *worker,
348 struct io_wqe_acct *acct,
349 task_work_func_t func)
351 struct io_wqe *wqe = worker->wqe;
352 struct io_wq *wq = wqe->wq;
354 /* raced with exit, just ignore create call */
355 if (test_bit(IO_WQ_BIT_EXIT, &wq->state))
357 if (!io_worker_get(worker))
360 * create_state manages ownership of create_work/index. We should
361 * only need one entry per worker, as the worker going to sleep
362 * will trigger the condition, and waking will clear it once it
363 * runs the task_work.
365 if (test_bit(0, &worker->create_state) ||
366 test_and_set_bit_lock(0, &worker->create_state))
369 atomic_inc(&wq->worker_refs);
370 init_task_work(&worker->create_work, func);
371 worker->create_index = acct->index;
372 if (!task_work_add(wq->task, &worker->create_work, TWA_SIGNAL)) {
374 * EXIT may have been set after checking it above, check after
375 * adding the task_work and remove any creation item if it is
376 * now set. wq exit does that too, but we can have added this
377 * work item after we canceled in io_wq_exit_workers().
379 if (test_bit(IO_WQ_BIT_EXIT, &wq->state))
380 io_wq_cancel_tw_create(wq);
381 io_worker_ref_put(wq);
384 io_worker_ref_put(wq);
385 clear_bit_unlock(0, &worker->create_state);
387 io_worker_release(worker);
389 atomic_dec(&acct->nr_running);
390 io_worker_ref_put(wq);
394 static void io_wqe_dec_running(struct io_worker *worker)
396 struct io_wqe_acct *acct = io_wqe_get_acct(worker);
397 struct io_wqe *wqe = worker->wqe;
399 if (!(worker->flags & IO_WORKER_F_UP))
402 if (!atomic_dec_and_test(&acct->nr_running))
404 if (!io_acct_run_queue(acct))
407 atomic_inc(&acct->nr_running);
408 atomic_inc(&wqe->wq->worker_refs);
409 io_queue_worker_create(worker, acct, create_worker_cb);
413 * Worker will start processing some work. Move it to the busy list, if
414 * it's currently on the freelist
416 static void __io_worker_busy(struct io_wqe *wqe, struct io_worker *worker)
418 if (worker->flags & IO_WORKER_F_FREE) {
419 worker->flags &= ~IO_WORKER_F_FREE;
420 raw_spin_lock(&wqe->lock);
421 hlist_nulls_del_init_rcu(&worker->nulls_node);
422 raw_spin_unlock(&wqe->lock);
427 * No work, worker going to sleep. Move to freelist, and unuse mm if we
428 * have one attached. Dropping the mm may potentially sleep, so we drop
429 * the lock in that case and return success. Since the caller has to
430 * retry the loop in that case (we changed task state), we don't regrab
431 * the lock if we return success.
433 static void __io_worker_idle(struct io_wqe *wqe, struct io_worker *worker)
434 __must_hold(wqe->lock)
436 if (!(worker->flags & IO_WORKER_F_FREE)) {
437 worker->flags |= IO_WORKER_F_FREE;
438 hlist_nulls_add_head_rcu(&worker->nulls_node, &wqe->free_list);
442 static inline unsigned int io_get_work_hash(struct io_wq_work *work)
444 return work->flags >> IO_WQ_HASH_SHIFT;
447 static bool io_wait_on_hash(struct io_wqe *wqe, unsigned int hash)
449 struct io_wq *wq = wqe->wq;
452 spin_lock_irq(&wq->hash->wait.lock);
453 if (list_empty(&wqe->wait.entry)) {
454 __add_wait_queue(&wq->hash->wait, &wqe->wait);
455 if (!test_bit(hash, &wq->hash->map)) {
456 __set_current_state(TASK_RUNNING);
457 list_del_init(&wqe->wait.entry);
461 spin_unlock_irq(&wq->hash->wait.lock);
465 static struct io_wq_work *io_get_next_work(struct io_wqe_acct *acct,
466 struct io_worker *worker)
467 __must_hold(acct->lock)
469 struct io_wq_work_node *node, *prev;
470 struct io_wq_work *work, *tail;
471 unsigned int stall_hash = -1U;
472 struct io_wqe *wqe = worker->wqe;
474 wq_list_for_each(node, prev, &acct->work_list) {
477 work = container_of(node, struct io_wq_work, list);
479 /* not hashed, can run anytime */
480 if (!io_wq_is_hashed(work)) {
481 wq_list_del(&acct->work_list, node, prev);
485 hash = io_get_work_hash(work);
486 /* all items with this hash lie in [work, tail] */
487 tail = wqe->hash_tail[hash];
489 /* hashed, can run if not already running */
490 if (!test_and_set_bit(hash, &wqe->wq->hash->map)) {
491 wqe->hash_tail[hash] = NULL;
492 wq_list_cut(&acct->work_list, &tail->list, prev);
495 if (stall_hash == -1U)
497 /* fast forward to a next hash, for-each will fix up @prev */
501 if (stall_hash != -1U) {
505 * Set this before dropping the lock to avoid racing with new
506 * work being added and clearing the stalled bit.
508 set_bit(IO_ACCT_STALLED_BIT, &acct->flags);
509 raw_spin_unlock(&acct->lock);
510 unstalled = io_wait_on_hash(wqe, stall_hash);
511 raw_spin_lock(&acct->lock);
513 clear_bit(IO_ACCT_STALLED_BIT, &acct->flags);
514 if (wq_has_sleeper(&wqe->wq->hash->wait))
515 wake_up(&wqe->wq->hash->wait);
522 static bool io_flush_signals(void)
524 if (unlikely(test_thread_flag(TIF_NOTIFY_SIGNAL))) {
525 __set_current_state(TASK_RUNNING);
526 clear_notify_signal();
527 if (task_work_pending(current))
534 static void io_assign_current_work(struct io_worker *worker,
535 struct io_wq_work *work)
542 raw_spin_lock(&worker->lock);
543 worker->cur_work = work;
544 worker->next_work = NULL;
545 raw_spin_unlock(&worker->lock);
548 static void io_wqe_enqueue(struct io_wqe *wqe, struct io_wq_work *work);
550 static void io_worker_handle_work(struct io_worker *worker)
552 struct io_wqe_acct *acct = io_wqe_get_acct(worker);
553 struct io_wqe *wqe = worker->wqe;
554 struct io_wq *wq = wqe->wq;
555 bool do_kill = test_bit(IO_WQ_BIT_EXIT, &wq->state);
558 struct io_wq_work *work;
561 * If we got some work, mark us as busy. If we didn't, but
562 * the list isn't empty, it means we stalled on hashed work.
563 * Mark us stalled so we don't keep looking for work when we
564 * can't make progress, any work completion or insertion will
565 * clear the stalled flag.
567 raw_spin_lock(&acct->lock);
568 work = io_get_next_work(acct, worker);
569 raw_spin_unlock(&acct->lock);
571 __io_worker_busy(wqe, worker);
574 * Make sure cancelation can find this, even before
575 * it becomes the active work. That avoids a window
576 * where the work has been removed from our general
577 * work list, but isn't yet discoverable as the
578 * current work item for this worker.
580 raw_spin_lock(&worker->lock);
581 worker->next_work = work;
582 raw_spin_unlock(&worker->lock);
586 io_assign_current_work(worker, work);
587 __set_current_state(TASK_RUNNING);
589 /* handle a whole dependent link */
591 struct io_wq_work *next_hashed, *linked;
592 unsigned int hash = io_get_work_hash(work);
594 next_hashed = wq_next_work(work);
596 if (unlikely(do_kill) && (work->flags & IO_WQ_WORK_UNBOUND))
597 work->flags |= IO_WQ_WORK_CANCEL;
599 io_assign_current_work(worker, NULL);
601 linked = wq->free_work(work);
603 if (!work && linked && !io_wq_is_hashed(linked)) {
607 io_assign_current_work(worker, work);
609 io_wqe_enqueue(wqe, linked);
611 if (hash != -1U && !next_hashed) {
612 /* serialize hash clear with wake_up() */
613 spin_lock_irq(&wq->hash->wait.lock);
614 clear_bit(hash, &wq->hash->map);
615 clear_bit(IO_ACCT_STALLED_BIT, &acct->flags);
616 spin_unlock_irq(&wq->hash->wait.lock);
617 if (wq_has_sleeper(&wq->hash->wait))
618 wake_up(&wq->hash->wait);
624 static int io_wqe_worker(void *data)
626 struct io_worker *worker = data;
627 struct io_wqe_acct *acct = io_wqe_get_acct(worker);
628 struct io_wqe *wqe = worker->wqe;
629 struct io_wq *wq = wqe->wq;
630 bool last_timeout = false;
631 char buf[TASK_COMM_LEN];
633 worker->flags |= (IO_WORKER_F_UP | IO_WORKER_F_RUNNING);
635 snprintf(buf, sizeof(buf), "iou-wrk-%d", wq->task->pid);
636 set_task_comm(current, buf);
638 audit_alloc_kernel(current);
640 while (!test_bit(IO_WQ_BIT_EXIT, &wq->state)) {
643 set_current_state(TASK_INTERRUPTIBLE);
644 while (io_acct_run_queue(acct))
645 io_worker_handle_work(worker);
647 raw_spin_lock(&wqe->lock);
648 /* timed out, exit unless we're the last worker */
649 if (last_timeout && acct->nr_workers > 1) {
651 raw_spin_unlock(&wqe->lock);
652 __set_current_state(TASK_RUNNING);
655 last_timeout = false;
656 __io_worker_idle(wqe, worker);
657 raw_spin_unlock(&wqe->lock);
658 if (io_flush_signals())
660 ret = schedule_timeout(WORKER_IDLE_TIMEOUT);
661 if (signal_pending(current)) {
664 if (!get_signal(&ksig))
671 if (test_bit(IO_WQ_BIT_EXIT, &wq->state))
672 io_worker_handle_work(worker);
675 io_worker_exit(worker);
680 * Called when a worker is scheduled in. Mark us as currently running.
682 void io_wq_worker_running(struct task_struct *tsk)
684 struct io_worker *worker = tsk->worker_private;
688 if (!(worker->flags & IO_WORKER_F_UP))
690 if (worker->flags & IO_WORKER_F_RUNNING)
692 worker->flags |= IO_WORKER_F_RUNNING;
693 io_wqe_inc_running(worker);
697 * Called when worker is going to sleep. If there are no workers currently
698 * running and we have work pending, wake up a free one or create a new one.
700 void io_wq_worker_sleeping(struct task_struct *tsk)
702 struct io_worker *worker = tsk->worker_private;
706 if (!(worker->flags & IO_WORKER_F_UP))
708 if (!(worker->flags & IO_WORKER_F_RUNNING))
711 worker->flags &= ~IO_WORKER_F_RUNNING;
712 io_wqe_dec_running(worker);
715 static void io_init_new_worker(struct io_wqe *wqe, struct io_worker *worker,
716 struct task_struct *tsk)
718 tsk->worker_private = worker;
720 set_cpus_allowed_ptr(tsk, wqe->cpu_mask);
721 tsk->flags |= PF_NO_SETAFFINITY;
723 raw_spin_lock(&wqe->lock);
724 hlist_nulls_add_head_rcu(&worker->nulls_node, &wqe->free_list);
725 list_add_tail_rcu(&worker->all_list, &wqe->all_list);
726 worker->flags |= IO_WORKER_F_FREE;
727 raw_spin_unlock(&wqe->lock);
728 wake_up_new_task(tsk);
731 static bool io_wq_work_match_all(struct io_wq_work *work, void *data)
736 static inline bool io_should_retry_thread(long err)
739 * Prevent perpetual task_work retry, if the task (or its group) is
742 if (fatal_signal_pending(current))
748 case -ERESTARTNOINTR:
749 case -ERESTARTNOHAND:
756 static void create_worker_cont(struct callback_head *cb)
758 struct io_worker *worker;
759 struct task_struct *tsk;
762 worker = container_of(cb, struct io_worker, create_work);
763 clear_bit_unlock(0, &worker->create_state);
765 tsk = create_io_thread(io_wqe_worker, worker, wqe->node);
767 io_init_new_worker(wqe, worker, tsk);
768 io_worker_release(worker);
770 } else if (!io_should_retry_thread(PTR_ERR(tsk))) {
771 struct io_wqe_acct *acct = io_wqe_get_acct(worker);
773 atomic_dec(&acct->nr_running);
774 raw_spin_lock(&wqe->lock);
776 if (!acct->nr_workers) {
777 struct io_cb_cancel_data match = {
778 .fn = io_wq_work_match_all,
782 raw_spin_unlock(&wqe->lock);
783 while (io_acct_cancel_pending_work(wqe, acct, &match))
786 raw_spin_unlock(&wqe->lock);
788 io_worker_ref_put(wqe->wq);
793 /* re-create attempts grab a new worker ref, drop the existing one */
794 io_worker_release(worker);
795 schedule_work(&worker->work);
798 static void io_workqueue_create(struct work_struct *work)
800 struct io_worker *worker = container_of(work, struct io_worker, work);
801 struct io_wqe_acct *acct = io_wqe_get_acct(worker);
803 if (!io_queue_worker_create(worker, acct, create_worker_cont))
807 static bool create_io_worker(struct io_wq *wq, struct io_wqe *wqe, int index)
809 struct io_wqe_acct *acct = &wqe->acct[index];
810 struct io_worker *worker;
811 struct task_struct *tsk;
813 __set_current_state(TASK_RUNNING);
815 worker = kzalloc_node(sizeof(*worker), GFP_KERNEL, wqe->node);
818 atomic_dec(&acct->nr_running);
819 raw_spin_lock(&wqe->lock);
821 raw_spin_unlock(&wqe->lock);
822 io_worker_ref_put(wq);
826 refcount_set(&worker->ref, 1);
828 raw_spin_lock_init(&worker->lock);
829 init_completion(&worker->ref_done);
831 if (index == IO_WQ_ACCT_BOUND)
832 worker->flags |= IO_WORKER_F_BOUND;
834 tsk = create_io_thread(io_wqe_worker, worker, wqe->node);
836 io_init_new_worker(wqe, worker, tsk);
837 } else if (!io_should_retry_thread(PTR_ERR(tsk))) {
841 INIT_WORK(&worker->work, io_workqueue_create);
842 schedule_work(&worker->work);
849 * Iterate the passed in list and call the specific function for each
850 * worker that isn't exiting
852 static bool io_wq_for_each_worker(struct io_wqe *wqe,
853 bool (*func)(struct io_worker *, void *),
856 struct io_worker *worker;
859 list_for_each_entry_rcu(worker, &wqe->all_list, all_list) {
860 if (io_worker_get(worker)) {
861 /* no task if node is/was offline */
863 ret = func(worker, data);
864 io_worker_release(worker);
873 static bool io_wq_worker_wake(struct io_worker *worker, void *data)
875 __set_notify_signal(worker->task);
876 wake_up_process(worker->task);
880 static void io_run_cancel(struct io_wq_work *work, struct io_wqe *wqe)
882 struct io_wq *wq = wqe->wq;
885 work->flags |= IO_WQ_WORK_CANCEL;
887 work = wq->free_work(work);
891 static void io_wqe_insert_work(struct io_wqe *wqe, struct io_wq_work *work)
893 struct io_wqe_acct *acct = io_work_get_acct(wqe, work);
895 struct io_wq_work *tail;
897 if (!io_wq_is_hashed(work)) {
899 wq_list_add_tail(&work->list, &acct->work_list);
903 hash = io_get_work_hash(work);
904 tail = wqe->hash_tail[hash];
905 wqe->hash_tail[hash] = work;
909 wq_list_add_after(&work->list, &tail->list, &acct->work_list);
912 static bool io_wq_work_match_item(struct io_wq_work *work, void *data)
917 static void io_wqe_enqueue(struct io_wqe *wqe, struct io_wq_work *work)
919 struct io_wqe_acct *acct = io_work_get_acct(wqe, work);
920 struct io_cb_cancel_data match;
921 unsigned work_flags = work->flags;
925 * If io-wq is exiting for this task, or if the request has explicitly
926 * been marked as one that should not get executed, cancel it here.
928 if (test_bit(IO_WQ_BIT_EXIT, &wqe->wq->state) ||
929 (work->flags & IO_WQ_WORK_CANCEL)) {
930 io_run_cancel(work, wqe);
934 raw_spin_lock(&acct->lock);
935 io_wqe_insert_work(wqe, work);
936 clear_bit(IO_ACCT_STALLED_BIT, &acct->flags);
937 raw_spin_unlock(&acct->lock);
939 raw_spin_lock(&wqe->lock);
941 do_create = !io_wqe_activate_free_worker(wqe, acct);
944 raw_spin_unlock(&wqe->lock);
946 if (do_create && ((work_flags & IO_WQ_WORK_CONCURRENT) ||
947 !atomic_read(&acct->nr_running))) {
950 did_create = io_wqe_create_worker(wqe, acct);
951 if (likely(did_create))
954 raw_spin_lock(&wqe->lock);
955 if (acct->nr_workers) {
956 raw_spin_unlock(&wqe->lock);
959 raw_spin_unlock(&wqe->lock);
961 /* fatal condition, failed to create the first worker */
962 match.fn = io_wq_work_match_item,
964 match.cancel_all = false,
966 io_acct_cancel_pending_work(wqe, acct, &match);
970 void io_wq_enqueue(struct io_wq *wq, struct io_wq_work *work)
972 struct io_wqe *wqe = wq->wqes[numa_node_id()];
974 io_wqe_enqueue(wqe, work);
978 * Work items that hash to the same value will not be done in parallel.
979 * Used to limit concurrent writes, generally hashed by inode.
981 void io_wq_hash_work(struct io_wq_work *work, void *val)
985 bit = hash_ptr(val, IO_WQ_HASH_ORDER);
986 work->flags |= (IO_WQ_WORK_HASHED | (bit << IO_WQ_HASH_SHIFT));
989 static bool __io_wq_worker_cancel(struct io_worker *worker,
990 struct io_cb_cancel_data *match,
991 struct io_wq_work *work)
993 if (work && match->fn(work, match->data)) {
994 work->flags |= IO_WQ_WORK_CANCEL;
995 __set_notify_signal(worker->task);
1002 static bool io_wq_worker_cancel(struct io_worker *worker, void *data)
1004 struct io_cb_cancel_data *match = data;
1007 * Hold the lock to avoid ->cur_work going out of scope, caller
1008 * may dereference the passed in work.
1010 raw_spin_lock(&worker->lock);
1011 if (__io_wq_worker_cancel(worker, match, worker->cur_work) ||
1012 __io_wq_worker_cancel(worker, match, worker->next_work))
1013 match->nr_running++;
1014 raw_spin_unlock(&worker->lock);
1016 return match->nr_running && !match->cancel_all;
1019 static inline void io_wqe_remove_pending(struct io_wqe *wqe,
1020 struct io_wq_work *work,
1021 struct io_wq_work_node *prev)
1023 struct io_wqe_acct *acct = io_work_get_acct(wqe, work);
1024 unsigned int hash = io_get_work_hash(work);
1025 struct io_wq_work *prev_work = NULL;
1027 if (io_wq_is_hashed(work) && work == wqe->hash_tail[hash]) {
1029 prev_work = container_of(prev, struct io_wq_work, list);
1030 if (prev_work && io_get_work_hash(prev_work) == hash)
1031 wqe->hash_tail[hash] = prev_work;
1033 wqe->hash_tail[hash] = NULL;
1035 wq_list_del(&acct->work_list, &work->list, prev);
1038 static bool io_acct_cancel_pending_work(struct io_wqe *wqe,
1039 struct io_wqe_acct *acct,
1040 struct io_cb_cancel_data *match)
1042 struct io_wq_work_node *node, *prev;
1043 struct io_wq_work *work;
1045 raw_spin_lock(&acct->lock);
1046 wq_list_for_each(node, prev, &acct->work_list) {
1047 work = container_of(node, struct io_wq_work, list);
1048 if (!match->fn(work, match->data))
1050 io_wqe_remove_pending(wqe, work, prev);
1051 raw_spin_unlock(&acct->lock);
1052 io_run_cancel(work, wqe);
1053 match->nr_pending++;
1054 /* not safe to continue after unlock */
1057 raw_spin_unlock(&acct->lock);
1062 static void io_wqe_cancel_pending_work(struct io_wqe *wqe,
1063 struct io_cb_cancel_data *match)
1067 for (i = 0; i < IO_WQ_ACCT_NR; i++) {
1068 struct io_wqe_acct *acct = io_get_acct(wqe, i == 0);
1070 if (io_acct_cancel_pending_work(wqe, acct, match)) {
1071 if (match->cancel_all)
1078 static void io_wqe_cancel_running_work(struct io_wqe *wqe,
1079 struct io_cb_cancel_data *match)
1082 io_wq_for_each_worker(wqe, io_wq_worker_cancel, match);
1086 enum io_wq_cancel io_wq_cancel_cb(struct io_wq *wq, work_cancel_fn *cancel,
1087 void *data, bool cancel_all)
1089 struct io_cb_cancel_data match = {
1092 .cancel_all = cancel_all,
1097 * First check pending list, if we're lucky we can just remove it
1098 * from there. CANCEL_OK means that the work is returned as-new,
1099 * no completion will be posted for it.
1101 * Then check if a free (going busy) or busy worker has the work
1102 * currently running. If we find it there, we'll return CANCEL_RUNNING
1103 * as an indication that we attempt to signal cancellation. The
1104 * completion will run normally in this case.
1106 * Do both of these while holding the wqe->lock, to ensure that
1107 * we'll find a work item regardless of state.
1109 for_each_node(node) {
1110 struct io_wqe *wqe = wq->wqes[node];
1112 io_wqe_cancel_pending_work(wqe, &match);
1113 if (match.nr_pending && !match.cancel_all)
1114 return IO_WQ_CANCEL_OK;
1116 raw_spin_lock(&wqe->lock);
1117 io_wqe_cancel_running_work(wqe, &match);
1118 raw_spin_unlock(&wqe->lock);
1119 if (match.nr_running && !match.cancel_all)
1120 return IO_WQ_CANCEL_RUNNING;
1123 if (match.nr_running)
1124 return IO_WQ_CANCEL_RUNNING;
1125 if (match.nr_pending)
1126 return IO_WQ_CANCEL_OK;
1127 return IO_WQ_CANCEL_NOTFOUND;
1130 static int io_wqe_hash_wake(struct wait_queue_entry *wait, unsigned mode,
1131 int sync, void *key)
1133 struct io_wqe *wqe = container_of(wait, struct io_wqe, wait);
1136 list_del_init(&wait->entry);
1139 for (i = 0; i < IO_WQ_ACCT_NR; i++) {
1140 struct io_wqe_acct *acct = &wqe->acct[i];
1142 if (test_and_clear_bit(IO_ACCT_STALLED_BIT, &acct->flags))
1143 io_wqe_activate_free_worker(wqe, acct);
1149 struct io_wq *io_wq_create(unsigned bounded, struct io_wq_data *data)
1154 if (WARN_ON_ONCE(!data->free_work || !data->do_work))
1155 return ERR_PTR(-EINVAL);
1156 if (WARN_ON_ONCE(!bounded))
1157 return ERR_PTR(-EINVAL);
1159 wq = kzalloc(struct_size(wq, wqes, nr_node_ids), GFP_KERNEL);
1161 return ERR_PTR(-ENOMEM);
1162 ret = cpuhp_state_add_instance_nocalls(io_wq_online, &wq->cpuhp_node);
1166 refcount_inc(&data->hash->refs);
1167 wq->hash = data->hash;
1168 wq->free_work = data->free_work;
1169 wq->do_work = data->do_work;
1172 for_each_node(node) {
1174 int alloc_node = node;
1176 if (!node_online(alloc_node))
1177 alloc_node = NUMA_NO_NODE;
1178 wqe = kzalloc_node(sizeof(struct io_wqe), GFP_KERNEL, alloc_node);
1181 if (!alloc_cpumask_var(&wqe->cpu_mask, GFP_KERNEL))
1183 cpumask_copy(wqe->cpu_mask, cpumask_of_node(node));
1184 wq->wqes[node] = wqe;
1185 wqe->node = alloc_node;
1186 wqe->acct[IO_WQ_ACCT_BOUND].max_workers = bounded;
1187 wqe->acct[IO_WQ_ACCT_UNBOUND].max_workers =
1188 task_rlimit(current, RLIMIT_NPROC);
1189 INIT_LIST_HEAD(&wqe->wait.entry);
1190 wqe->wait.func = io_wqe_hash_wake;
1191 for (i = 0; i < IO_WQ_ACCT_NR; i++) {
1192 struct io_wqe_acct *acct = &wqe->acct[i];
1195 atomic_set(&acct->nr_running, 0);
1196 INIT_WQ_LIST(&acct->work_list);
1197 raw_spin_lock_init(&acct->lock);
1200 raw_spin_lock_init(&wqe->lock);
1201 INIT_HLIST_NULLS_HEAD(&wqe->free_list, 0);
1202 INIT_LIST_HEAD(&wqe->all_list);
1205 wq->task = get_task_struct(data->task);
1206 atomic_set(&wq->worker_refs, 1);
1207 init_completion(&wq->worker_done);
1210 io_wq_put_hash(data->hash);
1211 cpuhp_state_remove_instance_nocalls(io_wq_online, &wq->cpuhp_node);
1212 for_each_node(node) {
1213 if (!wq->wqes[node])
1215 free_cpumask_var(wq->wqes[node]->cpu_mask);
1216 kfree(wq->wqes[node]);
1220 return ERR_PTR(ret);
1223 static bool io_task_work_match(struct callback_head *cb, void *data)
1225 struct io_worker *worker;
1227 if (cb->func != create_worker_cb && cb->func != create_worker_cont)
1229 worker = container_of(cb, struct io_worker, create_work);
1230 return worker->wqe->wq == data;
1233 void io_wq_exit_start(struct io_wq *wq)
1235 set_bit(IO_WQ_BIT_EXIT, &wq->state);
1238 static void io_wq_cancel_tw_create(struct io_wq *wq)
1240 struct callback_head *cb;
1242 while ((cb = task_work_cancel_match(wq->task, io_task_work_match, wq)) != NULL) {
1243 struct io_worker *worker;
1245 worker = container_of(cb, struct io_worker, create_work);
1246 io_worker_cancel_cb(worker);
1250 static void io_wq_exit_workers(struct io_wq *wq)
1257 io_wq_cancel_tw_create(wq);
1260 for_each_node(node) {
1261 struct io_wqe *wqe = wq->wqes[node];
1263 io_wq_for_each_worker(wqe, io_wq_worker_wake, NULL);
1266 io_worker_ref_put(wq);
1267 wait_for_completion(&wq->worker_done);
1269 for_each_node(node) {
1270 spin_lock_irq(&wq->hash->wait.lock);
1271 list_del_init(&wq->wqes[node]->wait.entry);
1272 spin_unlock_irq(&wq->hash->wait.lock);
1274 put_task_struct(wq->task);
1278 static void io_wq_destroy(struct io_wq *wq)
1282 cpuhp_state_remove_instance_nocalls(io_wq_online, &wq->cpuhp_node);
1284 for_each_node(node) {
1285 struct io_wqe *wqe = wq->wqes[node];
1286 struct io_cb_cancel_data match = {
1287 .fn = io_wq_work_match_all,
1290 io_wqe_cancel_pending_work(wqe, &match);
1291 free_cpumask_var(wqe->cpu_mask);
1294 io_wq_put_hash(wq->hash);
1298 void io_wq_put_and_exit(struct io_wq *wq)
1300 WARN_ON_ONCE(!test_bit(IO_WQ_BIT_EXIT, &wq->state));
1302 io_wq_exit_workers(wq);
1306 struct online_data {
1311 static bool io_wq_worker_affinity(struct io_worker *worker, void *data)
1313 struct online_data *od = data;
1316 cpumask_set_cpu(od->cpu, worker->wqe->cpu_mask);
1318 cpumask_clear_cpu(od->cpu, worker->wqe->cpu_mask);
1322 static int __io_wq_cpu_online(struct io_wq *wq, unsigned int cpu, bool online)
1324 struct online_data od = {
1332 io_wq_for_each_worker(wq->wqes[i], io_wq_worker_affinity, &od);
1337 static int io_wq_cpu_online(unsigned int cpu, struct hlist_node *node)
1339 struct io_wq *wq = hlist_entry_safe(node, struct io_wq, cpuhp_node);
1341 return __io_wq_cpu_online(wq, cpu, true);
1344 static int io_wq_cpu_offline(unsigned int cpu, struct hlist_node *node)
1346 struct io_wq *wq = hlist_entry_safe(node, struct io_wq, cpuhp_node);
1348 return __io_wq_cpu_online(wq, cpu, false);
1351 int io_wq_cpu_affinity(struct io_wq *wq, cpumask_var_t mask)
1357 struct io_wqe *wqe = wq->wqes[i];
1360 cpumask_copy(wqe->cpu_mask, mask);
1362 cpumask_copy(wqe->cpu_mask, cpumask_of_node(i));
1369 * Set max number of unbounded workers, returns old value. If new_count is 0,
1370 * then just return the old value.
1372 int io_wq_max_workers(struct io_wq *wq, int *new_count)
1374 int prev[IO_WQ_ACCT_NR];
1375 bool first_node = true;
1378 BUILD_BUG_ON((int) IO_WQ_ACCT_BOUND != (int) IO_WQ_BOUND);
1379 BUILD_BUG_ON((int) IO_WQ_ACCT_UNBOUND != (int) IO_WQ_UNBOUND);
1380 BUILD_BUG_ON((int) IO_WQ_ACCT_NR != 2);
1382 for (i = 0; i < IO_WQ_ACCT_NR; i++) {
1383 if (new_count[i] > task_rlimit(current, RLIMIT_NPROC))
1384 new_count[i] = task_rlimit(current, RLIMIT_NPROC);
1387 for (i = 0; i < IO_WQ_ACCT_NR; i++)
1391 for_each_node(node) {
1392 struct io_wqe *wqe = wq->wqes[node];
1393 struct io_wqe_acct *acct;
1395 raw_spin_lock(&wqe->lock);
1396 for (i = 0; i < IO_WQ_ACCT_NR; i++) {
1397 acct = &wqe->acct[i];
1399 prev[i] = max_t(int, acct->max_workers, prev[i]);
1401 acct->max_workers = new_count[i];
1403 raw_spin_unlock(&wqe->lock);
1408 for (i = 0; i < IO_WQ_ACCT_NR; i++)
1409 new_count[i] = prev[i];
1414 static __init int io_wq_init(void)
1418 ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "io-wq/online",
1419 io_wq_cpu_online, io_wq_cpu_offline);
1425 subsys_initcall(io_wq_init);