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/tracehook.h>
17 #include <uapi/linux/io_uring.h>
21 #define WORKER_IDLE_TIMEOUT (5 * HZ)
24 IO_WORKER_F_UP = 1, /* up and active */
25 IO_WORKER_F_RUNNING = 2, /* account as running */
26 IO_WORKER_F_FREE = 4, /* worker on free list */
27 IO_WORKER_F_BOUND = 8, /* is doing bounded work */
31 IO_WQ_BIT_EXIT = 0, /* wq exiting */
35 IO_ACCT_STALLED_BIT = 0, /* stalled on hash */
39 * One for each thread in a wqe pool
44 struct hlist_nulls_node nulls_node;
45 struct list_head all_list;
46 struct task_struct *task;
49 struct io_wq_work *cur_work;
52 struct completion ref_done;
54 unsigned long create_state;
55 struct callback_head create_work;
60 struct work_struct work;
64 #if BITS_PER_LONG == 64
65 #define IO_WQ_HASH_ORDER 6
67 #define IO_WQ_HASH_ORDER 5
70 #define IO_WQ_NR_HASH_BUCKETS (1u << IO_WQ_HASH_ORDER)
77 struct io_wq_work_list work_list;
88 * Per-node worker thread pool
92 struct io_wqe_acct acct[2];
96 struct hlist_nulls_head free_list;
97 struct list_head all_list;
99 struct wait_queue_entry wait;
102 struct io_wq_work *hash_tail[IO_WQ_NR_HASH_BUCKETS];
104 cpumask_var_t cpu_mask;
113 free_work_fn *free_work;
114 io_wq_work_fn *do_work;
116 struct io_wq_hash *hash;
118 atomic_t worker_refs;
119 struct completion worker_done;
121 struct hlist_node cpuhp_node;
123 struct task_struct *task;
125 struct io_wqe *wqes[];
128 static enum cpuhp_state io_wq_online;
130 struct io_cb_cancel_data {
138 static bool create_io_worker(struct io_wq *wq, struct io_wqe *wqe, int index);
139 static void io_wqe_dec_running(struct io_worker *worker);
140 static bool io_acct_cancel_pending_work(struct io_wqe *wqe,
141 struct io_wqe_acct *acct,
142 struct io_cb_cancel_data *match);
143 static void create_worker_cb(struct callback_head *cb);
144 static void io_wq_cancel_tw_create(struct io_wq *wq);
146 static bool io_worker_get(struct io_worker *worker)
148 return refcount_inc_not_zero(&worker->ref);
151 static void io_worker_release(struct io_worker *worker)
153 if (refcount_dec_and_test(&worker->ref))
154 complete(&worker->ref_done);
157 static inline struct io_wqe_acct *io_get_acct(struct io_wqe *wqe, bool bound)
159 return &wqe->acct[bound ? IO_WQ_ACCT_BOUND : IO_WQ_ACCT_UNBOUND];
162 static inline struct io_wqe_acct *io_work_get_acct(struct io_wqe *wqe,
163 struct io_wq_work *work)
165 return io_get_acct(wqe, !(work->flags & IO_WQ_WORK_UNBOUND));
168 static inline struct io_wqe_acct *io_wqe_get_acct(struct io_worker *worker)
170 return io_get_acct(worker->wqe, worker->flags & IO_WORKER_F_BOUND);
173 static void io_worker_ref_put(struct io_wq *wq)
175 if (atomic_dec_and_test(&wq->worker_refs))
176 complete(&wq->worker_done);
179 bool io_wq_worker_stopped(void)
181 struct io_worker *worker = current->pf_io_worker;
183 if (WARN_ON_ONCE(!io_wq_current_is_worker()))
186 return test_bit(IO_WQ_BIT_EXIT, &worker->wqe->wq->state);
189 static void io_worker_cancel_cb(struct io_worker *worker)
191 struct io_wqe_acct *acct = io_wqe_get_acct(worker);
192 struct io_wqe *wqe = worker->wqe;
193 struct io_wq *wq = wqe->wq;
195 atomic_dec(&acct->nr_running);
196 raw_spin_lock(&worker->wqe->lock);
198 raw_spin_unlock(&worker->wqe->lock);
199 io_worker_ref_put(wq);
200 clear_bit_unlock(0, &worker->create_state);
201 io_worker_release(worker);
204 static bool io_task_worker_match(struct callback_head *cb, void *data)
206 struct io_worker *worker;
208 if (cb->func != create_worker_cb)
210 worker = container_of(cb, struct io_worker, create_work);
211 return worker == data;
214 static void io_worker_exit(struct io_worker *worker)
216 struct io_wqe *wqe = worker->wqe;
217 struct io_wq *wq = wqe->wq;
220 struct callback_head *cb = task_work_cancel_match(wq->task,
221 io_task_worker_match, worker);
225 io_worker_cancel_cb(worker);
228 if (refcount_dec_and_test(&worker->ref))
229 complete(&worker->ref_done);
230 wait_for_completion(&worker->ref_done);
232 raw_spin_lock(&wqe->lock);
233 if (worker->flags & IO_WORKER_F_FREE)
234 hlist_nulls_del_rcu(&worker->nulls_node);
235 list_del_rcu(&worker->all_list);
237 io_wqe_dec_running(worker);
239 current->flags &= ~PF_IO_WORKER;
241 raw_spin_unlock(&wqe->lock);
243 kfree_rcu(worker, rcu);
244 io_worker_ref_put(wqe->wq);
248 static inline bool io_acct_run_queue(struct io_wqe_acct *acct)
250 if (!wq_list_empty(&acct->work_list) &&
251 !test_bit(IO_ACCT_STALLED_BIT, &acct->flags))
257 * Check head of free list for an available worker. If one isn't available,
258 * caller must create one.
260 static bool io_wqe_activate_free_worker(struct io_wqe *wqe,
261 struct io_wqe_acct *acct)
264 struct hlist_nulls_node *n;
265 struct io_worker *worker;
268 * Iterate free_list and see if we can find an idle worker to
269 * activate. If a given worker is on the free_list but in the process
270 * of exiting, keep trying.
272 hlist_nulls_for_each_entry_rcu(worker, n, &wqe->free_list, nulls_node) {
273 if (!io_worker_get(worker))
275 if (io_wqe_get_acct(worker) != acct) {
276 io_worker_release(worker);
279 if (wake_up_process(worker->task)) {
280 io_worker_release(worker);
283 io_worker_release(worker);
290 * We need a worker. If we find a free one, we're good. If not, and we're
291 * below the max number of workers, create one.
293 static bool io_wqe_create_worker(struct io_wqe *wqe, struct io_wqe_acct *acct)
296 * Most likely an attempt to queue unbounded work on an io_wq that
297 * wasn't setup with any unbounded workers.
299 if (unlikely(!acct->max_workers))
300 pr_warn_once("io-wq is not configured for unbound workers");
302 raw_spin_lock(&wqe->lock);
303 if (acct->nr_workers >= acct->max_workers) {
304 raw_spin_unlock(&wqe->lock);
308 raw_spin_unlock(&wqe->lock);
309 atomic_inc(&acct->nr_running);
310 atomic_inc(&wqe->wq->worker_refs);
311 return create_io_worker(wqe->wq, wqe, acct->index);
314 static void io_wqe_inc_running(struct io_worker *worker)
316 struct io_wqe_acct *acct = io_wqe_get_acct(worker);
318 atomic_inc(&acct->nr_running);
321 static void create_worker_cb(struct callback_head *cb)
323 struct io_worker *worker;
326 struct io_wqe_acct *acct;
327 bool do_create = false;
329 worker = container_of(cb, struct io_worker, create_work);
332 acct = &wqe->acct[worker->create_index];
333 raw_spin_lock(&wqe->lock);
334 if (acct->nr_workers < acct->max_workers) {
338 raw_spin_unlock(&wqe->lock);
340 create_io_worker(wq, wqe, worker->create_index);
342 atomic_dec(&acct->nr_running);
343 io_worker_ref_put(wq);
345 clear_bit_unlock(0, &worker->create_state);
346 io_worker_release(worker);
349 static bool io_queue_worker_create(struct io_worker *worker,
350 struct io_wqe_acct *acct,
351 task_work_func_t func)
353 struct io_wqe *wqe = worker->wqe;
354 struct io_wq *wq = wqe->wq;
356 /* raced with exit, just ignore create call */
357 if (test_bit(IO_WQ_BIT_EXIT, &wq->state))
359 if (!io_worker_get(worker))
362 * create_state manages ownership of create_work/index. We should
363 * only need one entry per worker, as the worker going to sleep
364 * will trigger the condition, and waking will clear it once it
365 * runs the task_work.
367 if (test_bit(0, &worker->create_state) ||
368 test_and_set_bit_lock(0, &worker->create_state))
371 atomic_inc(&wq->worker_refs);
372 init_task_work(&worker->create_work, func);
373 worker->create_index = acct->index;
374 if (!task_work_add(wq->task, &worker->create_work, TWA_SIGNAL)) {
376 * EXIT may have been set after checking it above, check after
377 * adding the task_work and remove any creation item if it is
378 * now set. wq exit does that too, but we can have added this
379 * work item after we canceled in io_wq_exit_workers().
381 if (test_bit(IO_WQ_BIT_EXIT, &wq->state))
382 io_wq_cancel_tw_create(wq);
383 io_worker_ref_put(wq);
386 io_worker_ref_put(wq);
387 clear_bit_unlock(0, &worker->create_state);
389 io_worker_release(worker);
391 atomic_dec(&acct->nr_running);
392 io_worker_ref_put(wq);
396 static void io_wqe_dec_running(struct io_worker *worker)
397 __must_hold(wqe->lock)
399 struct io_wqe_acct *acct = io_wqe_get_acct(worker);
400 struct io_wqe *wqe = worker->wqe;
402 if (!(worker->flags & IO_WORKER_F_UP))
405 if (atomic_dec_and_test(&acct->nr_running) && io_acct_run_queue(acct)) {
406 atomic_inc(&acct->nr_running);
407 atomic_inc(&wqe->wq->worker_refs);
408 raw_spin_unlock(&wqe->lock);
409 io_queue_worker_create(worker, acct, create_worker_cb);
410 raw_spin_lock(&wqe->lock);
415 * Worker will start processing some work. Move it to the busy list, if
416 * it's currently on the freelist
418 static void __io_worker_busy(struct io_wqe *wqe, struct io_worker *worker,
419 struct io_wq_work *work)
420 __must_hold(wqe->lock)
422 if (worker->flags & IO_WORKER_F_FREE) {
423 worker->flags &= ~IO_WORKER_F_FREE;
424 hlist_nulls_del_init_rcu(&worker->nulls_node);
429 * No work, worker going to sleep. Move to freelist, and unuse mm if we
430 * have one attached. Dropping the mm may potentially sleep, so we drop
431 * the lock in that case and return success. Since the caller has to
432 * retry the loop in that case (we changed task state), we don't regrab
433 * the lock if we return success.
435 static void __io_worker_idle(struct io_wqe *wqe, struct io_worker *worker)
436 __must_hold(wqe->lock)
438 if (!(worker->flags & IO_WORKER_F_FREE)) {
439 worker->flags |= IO_WORKER_F_FREE;
440 hlist_nulls_add_head_rcu(&worker->nulls_node, &wqe->free_list);
444 static inline unsigned int io_get_work_hash(struct io_wq_work *work)
446 return work->flags >> IO_WQ_HASH_SHIFT;
449 static bool io_wait_on_hash(struct io_wqe *wqe, unsigned int hash)
451 struct io_wq *wq = wqe->wq;
454 spin_lock_irq(&wq->hash->wait.lock);
455 if (list_empty(&wqe->wait.entry)) {
456 __add_wait_queue(&wq->hash->wait, &wqe->wait);
457 if (!test_bit(hash, &wq->hash->map)) {
458 __set_current_state(TASK_RUNNING);
459 list_del_init(&wqe->wait.entry);
463 spin_unlock_irq(&wq->hash->wait.lock);
467 static struct io_wq_work *io_get_next_work(struct io_wqe_acct *acct,
468 struct io_worker *worker)
469 __must_hold(wqe->lock)
471 struct io_wq_work_node *node, *prev;
472 struct io_wq_work *work, *tail;
473 unsigned int stall_hash = -1U;
474 struct io_wqe *wqe = worker->wqe;
476 wq_list_for_each(node, prev, &acct->work_list) {
479 work = container_of(node, struct io_wq_work, list);
481 /* not hashed, can run anytime */
482 if (!io_wq_is_hashed(work)) {
483 wq_list_del(&acct->work_list, node, prev);
487 hash = io_get_work_hash(work);
488 /* all items with this hash lie in [work, tail] */
489 tail = wqe->hash_tail[hash];
491 /* hashed, can run if not already running */
492 if (!test_and_set_bit(hash, &wqe->wq->hash->map)) {
493 wqe->hash_tail[hash] = NULL;
494 wq_list_cut(&acct->work_list, &tail->list, prev);
497 if (stall_hash == -1U)
499 /* fast forward to a next hash, for-each will fix up @prev */
503 if (stall_hash != -1U) {
507 * Set this before dropping the lock to avoid racing with new
508 * work being added and clearing the stalled bit.
510 set_bit(IO_ACCT_STALLED_BIT, &acct->flags);
511 raw_spin_unlock(&wqe->lock);
512 unstalled = io_wait_on_hash(wqe, stall_hash);
513 raw_spin_lock(&wqe->lock);
515 clear_bit(IO_ACCT_STALLED_BIT, &acct->flags);
516 if (wq_has_sleeper(&wqe->wq->hash->wait))
517 wake_up(&wqe->wq->hash->wait);
524 static bool io_flush_signals(void)
526 if (test_thread_flag(TIF_NOTIFY_SIGNAL) || current->task_works) {
527 __set_current_state(TASK_RUNNING);
528 tracehook_notify_signal();
534 static void io_assign_current_work(struct io_worker *worker,
535 struct io_wq_work *work)
542 spin_lock(&worker->lock);
543 worker->cur_work = work;
544 spin_unlock(&worker->lock);
547 static void io_wqe_enqueue(struct io_wqe *wqe, struct io_wq_work *work);
549 static void io_worker_handle_work(struct io_worker *worker)
550 __releases(wqe->lock)
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 work = io_get_next_work(acct, worker);
569 __io_worker_busy(wqe, worker, work);
571 raw_spin_unlock(&wqe->lock);
574 io_assign_current_work(worker, work);
575 __set_current_state(TASK_RUNNING);
577 /* handle a whole dependent link */
579 struct io_wq_work *next_hashed, *linked;
580 unsigned int hash = io_get_work_hash(work);
582 next_hashed = wq_next_work(work);
584 if (unlikely(do_kill) && (work->flags & IO_WQ_WORK_UNBOUND))
585 work->flags |= IO_WQ_WORK_CANCEL;
587 io_assign_current_work(worker, NULL);
589 linked = wq->free_work(work);
591 if (!work && linked && !io_wq_is_hashed(linked)) {
595 io_assign_current_work(worker, work);
597 io_wqe_enqueue(wqe, linked);
599 if (hash != -1U && !next_hashed) {
600 /* serialize hash clear with wake_up() */
601 spin_lock_irq(&wq->hash->wait.lock);
602 clear_bit(hash, &wq->hash->map);
603 clear_bit(IO_ACCT_STALLED_BIT, &acct->flags);
604 spin_unlock_irq(&wq->hash->wait.lock);
605 if (wq_has_sleeper(&wq->hash->wait))
606 wake_up(&wq->hash->wait);
607 raw_spin_lock(&wqe->lock);
608 /* skip unnecessary unlock-lock wqe->lock */
611 raw_spin_unlock(&wqe->lock);
615 raw_spin_lock(&wqe->lock);
619 static int io_wqe_worker(void *data)
621 struct io_worker *worker = data;
622 struct io_wqe_acct *acct = io_wqe_get_acct(worker);
623 struct io_wqe *wqe = worker->wqe;
624 struct io_wq *wq = wqe->wq;
625 bool last_timeout = false;
626 char buf[TASK_COMM_LEN];
628 worker->flags |= (IO_WORKER_F_UP | IO_WORKER_F_RUNNING);
630 snprintf(buf, sizeof(buf), "iou-wrk-%d", wq->task->pid);
631 set_task_comm(current, buf);
633 while (!test_bit(IO_WQ_BIT_EXIT, &wq->state)) {
636 set_current_state(TASK_INTERRUPTIBLE);
638 raw_spin_lock(&wqe->lock);
639 if (io_acct_run_queue(acct)) {
640 io_worker_handle_work(worker);
643 /* timed out, exit unless we're the last worker */
644 if (last_timeout && acct->nr_workers > 1) {
646 raw_spin_unlock(&wqe->lock);
647 __set_current_state(TASK_RUNNING);
650 last_timeout = false;
651 __io_worker_idle(wqe, worker);
652 raw_spin_unlock(&wqe->lock);
653 if (io_flush_signals())
655 ret = schedule_timeout(WORKER_IDLE_TIMEOUT);
656 if (signal_pending(current)) {
659 if (!get_signal(&ksig))
666 if (test_bit(IO_WQ_BIT_EXIT, &wq->state)) {
667 raw_spin_lock(&wqe->lock);
668 io_worker_handle_work(worker);
671 io_worker_exit(worker);
676 * Called when a worker is scheduled in. Mark us as currently running.
678 void io_wq_worker_running(struct task_struct *tsk)
680 struct io_worker *worker = tsk->pf_io_worker;
684 if (!(worker->flags & IO_WORKER_F_UP))
686 if (worker->flags & IO_WORKER_F_RUNNING)
688 worker->flags |= IO_WORKER_F_RUNNING;
689 io_wqe_inc_running(worker);
693 * Called when worker is going to sleep. If there are no workers currently
694 * running and we have work pending, wake up a free one or create a new one.
696 void io_wq_worker_sleeping(struct task_struct *tsk)
698 struct io_worker *worker = tsk->pf_io_worker;
702 if (!(worker->flags & IO_WORKER_F_UP))
704 if (!(worker->flags & IO_WORKER_F_RUNNING))
707 worker->flags &= ~IO_WORKER_F_RUNNING;
709 raw_spin_lock(&worker->wqe->lock);
710 io_wqe_dec_running(worker);
711 raw_spin_unlock(&worker->wqe->lock);
714 static void io_init_new_worker(struct io_wqe *wqe, struct io_worker *worker,
715 struct task_struct *tsk)
717 tsk->pf_io_worker = worker;
719 set_cpus_allowed_ptr(tsk, wqe->cpu_mask);
720 tsk->flags |= PF_NO_SETAFFINITY;
722 raw_spin_lock(&wqe->lock);
723 hlist_nulls_add_head_rcu(&worker->nulls_node, &wqe->free_list);
724 list_add_tail_rcu(&worker->all_list, &wqe->all_list);
725 worker->flags |= IO_WORKER_F_FREE;
726 raw_spin_unlock(&wqe->lock);
727 wake_up_new_task(tsk);
730 static bool io_wq_work_match_all(struct io_wq_work *work, void *data)
735 static inline bool io_should_retry_thread(long err)
738 * Prevent perpetual task_work retry, if the task (or its group) is
741 if (fatal_signal_pending(current))
747 case -ERESTARTNOINTR:
748 case -ERESTARTNOHAND:
755 static void create_worker_cont(struct callback_head *cb)
757 struct io_worker *worker;
758 struct task_struct *tsk;
761 worker = container_of(cb, struct io_worker, create_work);
762 clear_bit_unlock(0, &worker->create_state);
764 tsk = create_io_thread(io_wqe_worker, worker, wqe->node);
766 io_init_new_worker(wqe, worker, tsk);
767 io_worker_release(worker);
769 } else if (!io_should_retry_thread(PTR_ERR(tsk))) {
770 struct io_wqe_acct *acct = io_wqe_get_acct(worker);
772 atomic_dec(&acct->nr_running);
773 raw_spin_lock(&wqe->lock);
775 if (!acct->nr_workers) {
776 struct io_cb_cancel_data match = {
777 .fn = io_wq_work_match_all,
781 while (io_acct_cancel_pending_work(wqe, acct, &match))
782 raw_spin_lock(&wqe->lock);
784 raw_spin_unlock(&wqe->lock);
785 io_worker_ref_put(wqe->wq);
790 /* re-create attempts grab a new worker ref, drop the existing one */
791 io_worker_release(worker);
792 schedule_work(&worker->work);
795 static void io_workqueue_create(struct work_struct *work)
797 struct io_worker *worker = container_of(work, struct io_worker, work);
798 struct io_wqe_acct *acct = io_wqe_get_acct(worker);
800 if (!io_queue_worker_create(worker, acct, create_worker_cont))
804 static bool create_io_worker(struct io_wq *wq, struct io_wqe *wqe, int index)
806 struct io_wqe_acct *acct = &wqe->acct[index];
807 struct io_worker *worker;
808 struct task_struct *tsk;
810 __set_current_state(TASK_RUNNING);
812 worker = kzalloc_node(sizeof(*worker), GFP_KERNEL, wqe->node);
815 atomic_dec(&acct->nr_running);
816 raw_spin_lock(&wqe->lock);
818 raw_spin_unlock(&wqe->lock);
819 io_worker_ref_put(wq);
823 refcount_set(&worker->ref, 1);
825 spin_lock_init(&worker->lock);
826 init_completion(&worker->ref_done);
828 if (index == IO_WQ_ACCT_BOUND)
829 worker->flags |= IO_WORKER_F_BOUND;
831 tsk = create_io_thread(io_wqe_worker, worker, wqe->node);
833 io_init_new_worker(wqe, worker, tsk);
834 } else if (!io_should_retry_thread(PTR_ERR(tsk))) {
838 INIT_WORK(&worker->work, io_workqueue_create);
839 schedule_work(&worker->work);
846 * Iterate the passed in list and call the specific function for each
847 * worker that isn't exiting
849 static bool io_wq_for_each_worker(struct io_wqe *wqe,
850 bool (*func)(struct io_worker *, void *),
853 struct io_worker *worker;
856 list_for_each_entry_rcu(worker, &wqe->all_list, all_list) {
857 if (io_worker_get(worker)) {
858 /* no task if node is/was offline */
860 ret = func(worker, data);
861 io_worker_release(worker);
870 static bool io_wq_worker_wake(struct io_worker *worker, void *data)
872 set_notify_signal(worker->task);
873 wake_up_process(worker->task);
877 static void io_run_cancel(struct io_wq_work *work, struct io_wqe *wqe)
879 struct io_wq *wq = wqe->wq;
882 work->flags |= IO_WQ_WORK_CANCEL;
884 work = wq->free_work(work);
888 static void io_wqe_insert_work(struct io_wqe *wqe, struct io_wq_work *work)
890 struct io_wqe_acct *acct = io_work_get_acct(wqe, work);
892 struct io_wq_work *tail;
894 if (!io_wq_is_hashed(work)) {
896 wq_list_add_tail(&work->list, &acct->work_list);
900 hash = io_get_work_hash(work);
901 tail = wqe->hash_tail[hash];
902 wqe->hash_tail[hash] = work;
906 wq_list_add_after(&work->list, &tail->list, &acct->work_list);
909 static bool io_wq_work_match_item(struct io_wq_work *work, void *data)
914 static void io_wqe_enqueue(struct io_wqe *wqe, struct io_wq_work *work)
916 struct io_wqe_acct *acct = io_work_get_acct(wqe, work);
917 unsigned work_flags = work->flags;
921 * If io-wq is exiting for this task, or if the request has explicitly
922 * been marked as one that should not get executed, cancel it here.
924 if (test_bit(IO_WQ_BIT_EXIT, &wqe->wq->state) ||
925 (work->flags & IO_WQ_WORK_CANCEL)) {
926 io_run_cancel(work, wqe);
930 raw_spin_lock(&wqe->lock);
931 io_wqe_insert_work(wqe, work);
932 clear_bit(IO_ACCT_STALLED_BIT, &acct->flags);
935 do_create = !io_wqe_activate_free_worker(wqe, acct);
938 raw_spin_unlock(&wqe->lock);
940 if (do_create && ((work_flags & IO_WQ_WORK_CONCURRENT) ||
941 !atomic_read(&acct->nr_running))) {
944 did_create = io_wqe_create_worker(wqe, acct);
945 if (likely(did_create))
948 raw_spin_lock(&wqe->lock);
949 /* fatal condition, failed to create the first worker */
950 if (!acct->nr_workers) {
951 struct io_cb_cancel_data match = {
952 .fn = io_wq_work_match_item,
957 if (io_acct_cancel_pending_work(wqe, acct, &match))
958 raw_spin_lock(&wqe->lock);
960 raw_spin_unlock(&wqe->lock);
964 void io_wq_enqueue(struct io_wq *wq, struct io_wq_work *work)
966 struct io_wqe *wqe = wq->wqes[numa_node_id()];
968 io_wqe_enqueue(wqe, work);
972 * Work items that hash to the same value will not be done in parallel.
973 * Used to limit concurrent writes, generally hashed by inode.
975 void io_wq_hash_work(struct io_wq_work *work, void *val)
979 bit = hash_ptr(val, IO_WQ_HASH_ORDER);
980 work->flags |= (IO_WQ_WORK_HASHED | (bit << IO_WQ_HASH_SHIFT));
983 static bool io_wq_worker_cancel(struct io_worker *worker, void *data)
985 struct io_cb_cancel_data *match = data;
988 * Hold the lock to avoid ->cur_work going out of scope, caller
989 * may dereference the passed in work.
991 spin_lock(&worker->lock);
992 if (worker->cur_work &&
993 match->fn(worker->cur_work, match->data)) {
994 set_notify_signal(worker->task);
997 spin_unlock(&worker->lock);
999 return match->nr_running && !match->cancel_all;
1002 static inline void io_wqe_remove_pending(struct io_wqe *wqe,
1003 struct io_wq_work *work,
1004 struct io_wq_work_node *prev)
1006 struct io_wqe_acct *acct = io_work_get_acct(wqe, work);
1007 unsigned int hash = io_get_work_hash(work);
1008 struct io_wq_work *prev_work = NULL;
1010 if (io_wq_is_hashed(work) && work == wqe->hash_tail[hash]) {
1012 prev_work = container_of(prev, struct io_wq_work, list);
1013 if (prev_work && io_get_work_hash(prev_work) == hash)
1014 wqe->hash_tail[hash] = prev_work;
1016 wqe->hash_tail[hash] = NULL;
1018 wq_list_del(&acct->work_list, &work->list, prev);
1021 static bool io_acct_cancel_pending_work(struct io_wqe *wqe,
1022 struct io_wqe_acct *acct,
1023 struct io_cb_cancel_data *match)
1024 __releases(wqe->lock)
1026 struct io_wq_work_node *node, *prev;
1027 struct io_wq_work *work;
1029 wq_list_for_each(node, prev, &acct->work_list) {
1030 work = container_of(node, struct io_wq_work, list);
1031 if (!match->fn(work, match->data))
1033 io_wqe_remove_pending(wqe, work, prev);
1034 raw_spin_unlock(&wqe->lock);
1035 io_run_cancel(work, wqe);
1036 match->nr_pending++;
1037 /* not safe to continue after unlock */
1044 static void io_wqe_cancel_pending_work(struct io_wqe *wqe,
1045 struct io_cb_cancel_data *match)
1049 raw_spin_lock(&wqe->lock);
1050 for (i = 0; i < IO_WQ_ACCT_NR; i++) {
1051 struct io_wqe_acct *acct = io_get_acct(wqe, i == 0);
1053 if (io_acct_cancel_pending_work(wqe, acct, match)) {
1054 if (match->cancel_all)
1059 raw_spin_unlock(&wqe->lock);
1062 static void io_wqe_cancel_running_work(struct io_wqe *wqe,
1063 struct io_cb_cancel_data *match)
1066 io_wq_for_each_worker(wqe, io_wq_worker_cancel, match);
1070 enum io_wq_cancel io_wq_cancel_cb(struct io_wq *wq, work_cancel_fn *cancel,
1071 void *data, bool cancel_all)
1073 struct io_cb_cancel_data match = {
1076 .cancel_all = cancel_all,
1081 * First check pending list, if we're lucky we can just remove it
1082 * from there. CANCEL_OK means that the work is returned as-new,
1083 * no completion will be posted for it.
1085 for_each_node(node) {
1086 struct io_wqe *wqe = wq->wqes[node];
1088 io_wqe_cancel_pending_work(wqe, &match);
1089 if (match.nr_pending && !match.cancel_all)
1090 return IO_WQ_CANCEL_OK;
1094 * Now check if a free (going busy) or busy worker has the work
1095 * currently running. If we find it there, we'll return CANCEL_RUNNING
1096 * as an indication that we attempt to signal cancellation. The
1097 * completion will run normally in this case.
1099 for_each_node(node) {
1100 struct io_wqe *wqe = wq->wqes[node];
1102 io_wqe_cancel_running_work(wqe, &match);
1103 if (match.nr_running && !match.cancel_all)
1104 return IO_WQ_CANCEL_RUNNING;
1107 if (match.nr_running)
1108 return IO_WQ_CANCEL_RUNNING;
1109 if (match.nr_pending)
1110 return IO_WQ_CANCEL_OK;
1111 return IO_WQ_CANCEL_NOTFOUND;
1114 static int io_wqe_hash_wake(struct wait_queue_entry *wait, unsigned mode,
1115 int sync, void *key)
1117 struct io_wqe *wqe = container_of(wait, struct io_wqe, wait);
1120 list_del_init(&wait->entry);
1123 for (i = 0; i < IO_WQ_ACCT_NR; i++) {
1124 struct io_wqe_acct *acct = &wqe->acct[i];
1126 if (test_and_clear_bit(IO_ACCT_STALLED_BIT, &acct->flags))
1127 io_wqe_activate_free_worker(wqe, acct);
1133 struct io_wq *io_wq_create(unsigned bounded, struct io_wq_data *data)
1138 if (WARN_ON_ONCE(!data->free_work || !data->do_work))
1139 return ERR_PTR(-EINVAL);
1140 if (WARN_ON_ONCE(!bounded))
1141 return ERR_PTR(-EINVAL);
1143 wq = kzalloc(struct_size(wq, wqes, nr_node_ids), GFP_KERNEL);
1145 return ERR_PTR(-ENOMEM);
1146 ret = cpuhp_state_add_instance_nocalls(io_wq_online, &wq->cpuhp_node);
1150 refcount_inc(&data->hash->refs);
1151 wq->hash = data->hash;
1152 wq->free_work = data->free_work;
1153 wq->do_work = data->do_work;
1156 for_each_node(node) {
1158 int alloc_node = node;
1160 if (!node_online(alloc_node))
1161 alloc_node = NUMA_NO_NODE;
1162 wqe = kzalloc_node(sizeof(struct io_wqe), GFP_KERNEL, alloc_node);
1165 wq->wqes[node] = wqe;
1166 if (!alloc_cpumask_var(&wqe->cpu_mask, GFP_KERNEL))
1168 cpumask_copy(wqe->cpu_mask, cpumask_of_node(node));
1169 wqe->node = alloc_node;
1170 wqe->acct[IO_WQ_ACCT_BOUND].max_workers = bounded;
1171 wqe->acct[IO_WQ_ACCT_UNBOUND].max_workers =
1172 task_rlimit(current, RLIMIT_NPROC);
1173 INIT_LIST_HEAD(&wqe->wait.entry);
1174 wqe->wait.func = io_wqe_hash_wake;
1175 for (i = 0; i < IO_WQ_ACCT_NR; i++) {
1176 struct io_wqe_acct *acct = &wqe->acct[i];
1179 atomic_set(&acct->nr_running, 0);
1180 INIT_WQ_LIST(&acct->work_list);
1183 raw_spin_lock_init(&wqe->lock);
1184 INIT_HLIST_NULLS_HEAD(&wqe->free_list, 0);
1185 INIT_LIST_HEAD(&wqe->all_list);
1188 wq->task = get_task_struct(data->task);
1189 atomic_set(&wq->worker_refs, 1);
1190 init_completion(&wq->worker_done);
1193 io_wq_put_hash(data->hash);
1194 cpuhp_state_remove_instance_nocalls(io_wq_online, &wq->cpuhp_node);
1195 for_each_node(node) {
1196 if (!wq->wqes[node])
1198 free_cpumask_var(wq->wqes[node]->cpu_mask);
1199 kfree(wq->wqes[node]);
1203 return ERR_PTR(ret);
1206 static bool io_task_work_match(struct callback_head *cb, void *data)
1208 struct io_worker *worker;
1210 if (cb->func != create_worker_cb && cb->func != create_worker_cont)
1212 worker = container_of(cb, struct io_worker, create_work);
1213 return worker->wqe->wq == data;
1216 void io_wq_exit_start(struct io_wq *wq)
1218 set_bit(IO_WQ_BIT_EXIT, &wq->state);
1221 static void io_wq_cancel_tw_create(struct io_wq *wq)
1223 struct callback_head *cb;
1225 while ((cb = task_work_cancel_match(wq->task, io_task_work_match, wq)) != NULL) {
1226 struct io_worker *worker;
1228 worker = container_of(cb, struct io_worker, create_work);
1229 io_worker_cancel_cb(worker);
1231 * Only the worker continuation helper has worker allocated and
1232 * hence needs freeing.
1234 if (cb->func == create_worker_cont)
1239 static void io_wq_exit_workers(struct io_wq *wq)
1246 io_wq_cancel_tw_create(wq);
1249 for_each_node(node) {
1250 struct io_wqe *wqe = wq->wqes[node];
1252 io_wq_for_each_worker(wqe, io_wq_worker_wake, NULL);
1255 io_worker_ref_put(wq);
1256 wait_for_completion(&wq->worker_done);
1258 for_each_node(node) {
1259 spin_lock_irq(&wq->hash->wait.lock);
1260 list_del_init(&wq->wqes[node]->wait.entry);
1261 spin_unlock_irq(&wq->hash->wait.lock);
1263 put_task_struct(wq->task);
1267 static void io_wq_destroy(struct io_wq *wq)
1271 cpuhp_state_remove_instance_nocalls(io_wq_online, &wq->cpuhp_node);
1273 for_each_node(node) {
1274 struct io_wqe *wqe = wq->wqes[node];
1275 struct io_cb_cancel_data match = {
1276 .fn = io_wq_work_match_all,
1279 io_wqe_cancel_pending_work(wqe, &match);
1280 free_cpumask_var(wqe->cpu_mask);
1283 io_wq_put_hash(wq->hash);
1287 void io_wq_put_and_exit(struct io_wq *wq)
1289 WARN_ON_ONCE(!test_bit(IO_WQ_BIT_EXIT, &wq->state));
1291 io_wq_exit_workers(wq);
1295 struct online_data {
1300 static bool io_wq_worker_affinity(struct io_worker *worker, void *data)
1302 struct online_data *od = data;
1305 cpumask_set_cpu(od->cpu, worker->wqe->cpu_mask);
1307 cpumask_clear_cpu(od->cpu, worker->wqe->cpu_mask);
1311 static int __io_wq_cpu_online(struct io_wq *wq, unsigned int cpu, bool online)
1313 struct online_data od = {
1321 io_wq_for_each_worker(wq->wqes[i], io_wq_worker_affinity, &od);
1326 static int io_wq_cpu_online(unsigned int cpu, struct hlist_node *node)
1328 struct io_wq *wq = hlist_entry_safe(node, struct io_wq, cpuhp_node);
1330 return __io_wq_cpu_online(wq, cpu, true);
1333 static int io_wq_cpu_offline(unsigned int cpu, struct hlist_node *node)
1335 struct io_wq *wq = hlist_entry_safe(node, struct io_wq, cpuhp_node);
1337 return __io_wq_cpu_online(wq, cpu, false);
1340 int io_wq_cpu_affinity(struct io_wq *wq, cpumask_var_t mask)
1346 struct io_wqe *wqe = wq->wqes[i];
1349 cpumask_copy(wqe->cpu_mask, mask);
1351 cpumask_copy(wqe->cpu_mask, cpumask_of_node(i));
1358 * Set max number of unbounded workers, returns old value. If new_count is 0,
1359 * then just return the old value.
1361 int io_wq_max_workers(struct io_wq *wq, int *new_count)
1363 int prev[IO_WQ_ACCT_NR];
1364 bool first_node = true;
1367 BUILD_BUG_ON((int) IO_WQ_ACCT_BOUND != (int) IO_WQ_BOUND);
1368 BUILD_BUG_ON((int) IO_WQ_ACCT_UNBOUND != (int) IO_WQ_UNBOUND);
1369 BUILD_BUG_ON((int) IO_WQ_ACCT_NR != 2);
1371 for (i = 0; i < 2; i++) {
1372 if (new_count[i] > task_rlimit(current, RLIMIT_NPROC))
1373 new_count[i] = task_rlimit(current, RLIMIT_NPROC);
1376 for (i = 0; i < IO_WQ_ACCT_NR; i++)
1380 for_each_node(node) {
1381 struct io_wqe *wqe = wq->wqes[node];
1382 struct io_wqe_acct *acct;
1384 raw_spin_lock(&wqe->lock);
1385 for (i = 0; i < IO_WQ_ACCT_NR; i++) {
1386 acct = &wqe->acct[i];
1388 prev[i] = max_t(int, acct->max_workers, prev[i]);
1390 acct->max_workers = new_count[i];
1392 raw_spin_unlock(&wqe->lock);
1397 for (i = 0; i < IO_WQ_ACCT_NR; i++)
1398 new_count[i] = prev[i];
1403 static __init int io_wq_init(void)
1407 ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "io-wq/online",
1408 io_wq_cpu_online, io_wq_cpu_offline);
1414 subsys_initcall(io_wq_init);