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/cpuset.h>
17 #include <linux/task_work.h>
18 #include <linux/audit.h>
19 #include <linux/mmu_context.h>
20 #include <uapi/linux/io_uring.h>
26 #define WORKER_IDLE_TIMEOUT (5 * HZ)
27 #define WORKER_INIT_LIMIT 3
30 IO_WORKER_F_UP
= 0, /* up and active */
31 IO_WORKER_F_RUNNING
= 1, /* account as running */
32 IO_WORKER_F_FREE
= 2, /* worker on free list */
33 IO_WORKER_F_BOUND
= 3, /* is doing bounded work */
37 IO_WQ_BIT_EXIT
= 0, /* wq exiting */
41 IO_ACCT_STALLED_BIT
= 0, /* stalled on hash */
45 * One for each thread in a wq pool
51 struct hlist_nulls_node nulls_node
;
52 struct list_head all_list
;
53 struct task_struct
*task
;
56 struct io_wq_work
*cur_work
;
59 struct completion ref_done
;
61 unsigned long create_state
;
62 struct callback_head create_work
;
67 struct work_struct work
;
71 #if BITS_PER_LONG == 64
72 #define IO_WQ_HASH_ORDER 6
74 #define IO_WQ_HASH_ORDER 5
77 #define IO_WQ_NR_HASH_BUCKETS (1u << IO_WQ_HASH_ORDER)
85 struct io_wq_work_list work_list
;
101 free_work_fn
*free_work
;
102 io_wq_work_fn
*do_work
;
104 struct io_wq_hash
*hash
;
106 atomic_t worker_refs
;
107 struct completion worker_done
;
109 struct hlist_node cpuhp_node
;
111 struct task_struct
*task
;
113 struct io_wq_acct acct
[IO_WQ_ACCT_NR
];
115 /* lock protects access to elements below */
118 struct hlist_nulls_head free_list
;
119 struct list_head all_list
;
121 struct wait_queue_entry wait
;
123 struct io_wq_work
*hash_tail
[IO_WQ_NR_HASH_BUCKETS
];
125 cpumask_var_t cpu_mask
;
128 static enum cpuhp_state io_wq_online
;
130 struct io_cb_cancel_data
{
138 static bool create_io_worker(struct io_wq
*wq
, int index
);
139 static void io_wq_dec_running(struct io_worker
*worker
);
140 static bool io_acct_cancel_pending_work(struct io_wq
*wq
,
141 struct io_wq_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_wq_acct
*io_get_acct(struct io_wq
*wq
, bool bound
)
159 return &wq
->acct
[bound
? IO_WQ_ACCT_BOUND
: IO_WQ_ACCT_UNBOUND
];
162 static inline struct io_wq_acct
*io_work_get_acct(struct io_wq
*wq
,
163 struct io_wq_work
*work
)
165 return io_get_acct(wq
, !(atomic_read(&work
->flags
) & IO_WQ_WORK_UNBOUND
));
168 static inline struct io_wq_acct
*io_wq_get_acct(struct io_worker
*worker
)
170 return io_get_acct(worker
->wq
, test_bit(IO_WORKER_F_BOUND
, &worker
->flags
));
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
->worker_private
;
183 if (WARN_ON_ONCE(!io_wq_current_is_worker()))
186 return test_bit(IO_WQ_BIT_EXIT
, &worker
->wq
->state
);
189 static void io_worker_cancel_cb(struct io_worker
*worker
)
191 struct io_wq_acct
*acct
= io_wq_get_acct(worker
);
192 struct io_wq
*wq
= worker
->wq
;
194 atomic_dec(&acct
->nr_running
);
195 raw_spin_lock(&wq
->lock
);
197 raw_spin_unlock(&wq
->lock
);
198 io_worker_ref_put(wq
);
199 clear_bit_unlock(0, &worker
->create_state
);
200 io_worker_release(worker
);
203 static bool io_task_worker_match(struct callback_head
*cb
, void *data
)
205 struct io_worker
*worker
;
207 if (cb
->func
!= create_worker_cb
)
209 worker
= container_of(cb
, struct io_worker
, create_work
);
210 return worker
== data
;
213 static void io_worker_exit(struct io_worker
*worker
)
215 struct io_wq
*wq
= worker
->wq
;
218 struct callback_head
*cb
= task_work_cancel_match(wq
->task
,
219 io_task_worker_match
, worker
);
223 io_worker_cancel_cb(worker
);
226 io_worker_release(worker
);
227 wait_for_completion(&worker
->ref_done
);
229 raw_spin_lock(&wq
->lock
);
230 if (test_bit(IO_WORKER_F_FREE
, &worker
->flags
))
231 hlist_nulls_del_rcu(&worker
->nulls_node
);
232 list_del_rcu(&worker
->all_list
);
233 raw_spin_unlock(&wq
->lock
);
234 io_wq_dec_running(worker
);
236 * this worker is a goner, clear ->worker_private to avoid any
237 * inc/dec running calls that could happen as part of exit from
240 current
->worker_private
= NULL
;
242 kfree_rcu(worker
, rcu
);
243 io_worker_ref_put(wq
);
247 static inline bool __io_acct_run_queue(struct io_wq_acct
*acct
)
249 return !test_bit(IO_ACCT_STALLED_BIT
, &acct
->flags
) &&
250 !wq_list_empty(&acct
->work_list
);
254 * If there's work to do, returns true with acct->lock acquired. If not,
255 * returns false with no lock held.
257 static inline bool io_acct_run_queue(struct io_wq_acct
*acct
)
258 __acquires(&acct
->lock
)
260 raw_spin_lock(&acct
->lock
);
261 if (__io_acct_run_queue(acct
))
264 raw_spin_unlock(&acct
->lock
);
269 * Check head of free list for an available worker. If one isn't available,
270 * caller must create one.
272 static bool io_wq_activate_free_worker(struct io_wq
*wq
,
273 struct io_wq_acct
*acct
)
276 struct hlist_nulls_node
*n
;
277 struct io_worker
*worker
;
280 * Iterate free_list and see if we can find an idle worker to
281 * activate. If a given worker is on the free_list but in the process
282 * of exiting, keep trying.
284 hlist_nulls_for_each_entry_rcu(worker
, n
, &wq
->free_list
, nulls_node
) {
285 if (!io_worker_get(worker
))
287 if (io_wq_get_acct(worker
) != acct
) {
288 io_worker_release(worker
);
292 * If the worker is already running, it's either already
293 * starting work or finishing work. In either case, if it does
294 * to go sleep, we'll kick off a new task for this work anyway.
296 wake_up_process(worker
->task
);
297 io_worker_release(worker
);
305 * We need a worker. If we find a free one, we're good. If not, and we're
306 * below the max number of workers, create one.
308 static bool io_wq_create_worker(struct io_wq
*wq
, struct io_wq_acct
*acct
)
311 * Most likely an attempt to queue unbounded work on an io_wq that
312 * wasn't setup with any unbounded workers.
314 if (unlikely(!acct
->max_workers
))
315 pr_warn_once("io-wq is not configured for unbound workers");
317 raw_spin_lock(&wq
->lock
);
318 if (acct
->nr_workers
>= acct
->max_workers
) {
319 raw_spin_unlock(&wq
->lock
);
323 raw_spin_unlock(&wq
->lock
);
324 atomic_inc(&acct
->nr_running
);
325 atomic_inc(&wq
->worker_refs
);
326 return create_io_worker(wq
, acct
->index
);
329 static void io_wq_inc_running(struct io_worker
*worker
)
331 struct io_wq_acct
*acct
= io_wq_get_acct(worker
);
333 atomic_inc(&acct
->nr_running
);
336 static void create_worker_cb(struct callback_head
*cb
)
338 struct io_worker
*worker
;
341 struct io_wq_acct
*acct
;
342 bool do_create
= false;
344 worker
= container_of(cb
, struct io_worker
, create_work
);
346 acct
= &wq
->acct
[worker
->create_index
];
347 raw_spin_lock(&wq
->lock
);
349 if (acct
->nr_workers
< acct
->max_workers
) {
353 raw_spin_unlock(&wq
->lock
);
355 create_io_worker(wq
, worker
->create_index
);
357 atomic_dec(&acct
->nr_running
);
358 io_worker_ref_put(wq
);
360 clear_bit_unlock(0, &worker
->create_state
);
361 io_worker_release(worker
);
364 static bool io_queue_worker_create(struct io_worker
*worker
,
365 struct io_wq_acct
*acct
,
366 task_work_func_t func
)
368 struct io_wq
*wq
= worker
->wq
;
370 /* raced with exit, just ignore create call */
371 if (test_bit(IO_WQ_BIT_EXIT
, &wq
->state
))
373 if (!io_worker_get(worker
))
376 * create_state manages ownership of create_work/index. We should
377 * only need one entry per worker, as the worker going to sleep
378 * will trigger the condition, and waking will clear it once it
379 * runs the task_work.
381 if (test_bit(0, &worker
->create_state
) ||
382 test_and_set_bit_lock(0, &worker
->create_state
))
385 atomic_inc(&wq
->worker_refs
);
386 init_task_work(&worker
->create_work
, func
);
387 worker
->create_index
= acct
->index
;
388 if (!task_work_add(wq
->task
, &worker
->create_work
, TWA_SIGNAL
)) {
390 * EXIT may have been set after checking it above, check after
391 * adding the task_work and remove any creation item if it is
392 * now set. wq exit does that too, but we can have added this
393 * work item after we canceled in io_wq_exit_workers().
395 if (test_bit(IO_WQ_BIT_EXIT
, &wq
->state
))
396 io_wq_cancel_tw_create(wq
);
397 io_worker_ref_put(wq
);
400 io_worker_ref_put(wq
);
401 clear_bit_unlock(0, &worker
->create_state
);
403 io_worker_release(worker
);
405 atomic_dec(&acct
->nr_running
);
406 io_worker_ref_put(wq
);
410 static void io_wq_dec_running(struct io_worker
*worker
)
412 struct io_wq_acct
*acct
= io_wq_get_acct(worker
);
413 struct io_wq
*wq
= worker
->wq
;
415 if (!test_bit(IO_WORKER_F_UP
, &worker
->flags
))
418 if (!atomic_dec_and_test(&acct
->nr_running
))
420 if (!io_acct_run_queue(acct
))
423 raw_spin_unlock(&acct
->lock
);
424 atomic_inc(&acct
->nr_running
);
425 atomic_inc(&wq
->worker_refs
);
426 io_queue_worker_create(worker
, acct
, create_worker_cb
);
430 * Worker will start processing some work. Move it to the busy list, if
431 * it's currently on the freelist
433 static void __io_worker_busy(struct io_wq
*wq
, struct io_worker
*worker
)
435 if (test_bit(IO_WORKER_F_FREE
, &worker
->flags
)) {
436 clear_bit(IO_WORKER_F_FREE
, &worker
->flags
);
437 raw_spin_lock(&wq
->lock
);
438 hlist_nulls_del_init_rcu(&worker
->nulls_node
);
439 raw_spin_unlock(&wq
->lock
);
444 * No work, worker going to sleep. Move to freelist.
446 static void __io_worker_idle(struct io_wq
*wq
, struct io_worker
*worker
)
447 __must_hold(wq
->lock
)
449 if (!test_bit(IO_WORKER_F_FREE
, &worker
->flags
)) {
450 set_bit(IO_WORKER_F_FREE
, &worker
->flags
);
451 hlist_nulls_add_head_rcu(&worker
->nulls_node
, &wq
->free_list
);
455 static inline unsigned int io_get_work_hash(struct io_wq_work
*work
)
457 return atomic_read(&work
->flags
) >> IO_WQ_HASH_SHIFT
;
460 static bool io_wait_on_hash(struct io_wq
*wq
, unsigned int hash
)
464 spin_lock_irq(&wq
->hash
->wait
.lock
);
465 if (list_empty(&wq
->wait
.entry
)) {
466 __add_wait_queue(&wq
->hash
->wait
, &wq
->wait
);
467 if (!test_bit(hash
, &wq
->hash
->map
)) {
468 __set_current_state(TASK_RUNNING
);
469 list_del_init(&wq
->wait
.entry
);
473 spin_unlock_irq(&wq
->hash
->wait
.lock
);
477 static struct io_wq_work
*io_get_next_work(struct io_wq_acct
*acct
,
478 struct io_worker
*worker
)
479 __must_hold(acct
->lock
)
481 struct io_wq_work_node
*node
, *prev
;
482 struct io_wq_work
*work
, *tail
;
483 unsigned int stall_hash
= -1U;
484 struct io_wq
*wq
= worker
->wq
;
486 wq_list_for_each(node
, prev
, &acct
->work_list
) {
489 work
= container_of(node
, struct io_wq_work
, list
);
491 /* not hashed, can run anytime */
492 if (!io_wq_is_hashed(work
)) {
493 wq_list_del(&acct
->work_list
, node
, prev
);
497 hash
= io_get_work_hash(work
);
498 /* all items with this hash lie in [work, tail] */
499 tail
= wq
->hash_tail
[hash
];
501 /* hashed, can run if not already running */
502 if (!test_and_set_bit(hash
, &wq
->hash
->map
)) {
503 wq
->hash_tail
[hash
] = NULL
;
504 wq_list_cut(&acct
->work_list
, &tail
->list
, prev
);
507 if (stall_hash
== -1U)
509 /* fast forward to a next hash, for-each will fix up @prev */
513 if (stall_hash
!= -1U) {
517 * Set this before dropping the lock to avoid racing with new
518 * work being added and clearing the stalled bit.
520 set_bit(IO_ACCT_STALLED_BIT
, &acct
->flags
);
521 raw_spin_unlock(&acct
->lock
);
522 unstalled
= io_wait_on_hash(wq
, stall_hash
);
523 raw_spin_lock(&acct
->lock
);
525 clear_bit(IO_ACCT_STALLED_BIT
, &acct
->flags
);
526 if (wq_has_sleeper(&wq
->hash
->wait
))
527 wake_up(&wq
->hash
->wait
);
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 raw_spin_unlock(&worker
->lock
);
548 * Called with acct->lock held, drops it before returning
550 static void io_worker_handle_work(struct io_wq_acct
*acct
,
551 struct io_worker
*worker
)
552 __releases(&acct
->lock
)
554 struct io_wq
*wq
= worker
->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
);
570 * Make sure cancelation can find this, even before
571 * it becomes the active work. That avoids a window
572 * where the work has been removed from our general
573 * work list, but isn't yet discoverable as the
574 * current work item for this worker.
576 raw_spin_lock(&worker
->lock
);
577 worker
->cur_work
= work
;
578 raw_spin_unlock(&worker
->lock
);
581 raw_spin_unlock(&acct
->lock
);
586 __io_worker_busy(wq
, worker
);
588 io_assign_current_work(worker
, work
);
589 __set_current_state(TASK_RUNNING
);
591 /* handle a whole dependent link */
593 struct io_wq_work
*next_hashed
, *linked
;
594 unsigned int hash
= io_get_work_hash(work
);
596 next_hashed
= wq_next_work(work
);
599 (atomic_read(&work
->flags
) & IO_WQ_WORK_UNBOUND
))
600 atomic_or(IO_WQ_WORK_CANCEL
, &work
->flags
);
602 io_assign_current_work(worker
, NULL
);
604 linked
= wq
->free_work(work
);
606 if (!work
&& linked
&& !io_wq_is_hashed(linked
)) {
610 io_assign_current_work(worker
, work
);
612 io_wq_enqueue(wq
, linked
);
614 if (hash
!= -1U && !next_hashed
) {
615 /* serialize hash clear with wake_up() */
616 spin_lock_irq(&wq
->hash
->wait
.lock
);
617 clear_bit(hash
, &wq
->hash
->map
);
618 clear_bit(IO_ACCT_STALLED_BIT
, &acct
->flags
);
619 spin_unlock_irq(&wq
->hash
->wait
.lock
);
620 if (wq_has_sleeper(&wq
->hash
->wait
))
621 wake_up(&wq
->hash
->wait
);
625 if (!__io_acct_run_queue(acct
))
627 raw_spin_lock(&acct
->lock
);
631 static int io_wq_worker(void *data
)
633 struct io_worker
*worker
= data
;
634 struct io_wq_acct
*acct
= io_wq_get_acct(worker
);
635 struct io_wq
*wq
= worker
->wq
;
636 bool exit_mask
= false, last_timeout
= false;
637 char buf
[TASK_COMM_LEN
];
639 set_mask_bits(&worker
->flags
, 0,
640 BIT(IO_WORKER_F_UP
) | BIT(IO_WORKER_F_RUNNING
));
642 snprintf(buf
, sizeof(buf
), "iou-wrk-%d", wq
->task
->pid
);
643 set_task_comm(current
, buf
);
645 while (!test_bit(IO_WQ_BIT_EXIT
, &wq
->state
)) {
648 set_current_state(TASK_INTERRUPTIBLE
);
651 * If we have work to do, io_acct_run_queue() returns with
652 * the acct->lock held. If not, it will drop it.
654 while (io_acct_run_queue(acct
))
655 io_worker_handle_work(acct
, worker
);
657 raw_spin_lock(&wq
->lock
);
659 * Last sleep timed out. Exit if we're not the last worker,
660 * or if someone modified our affinity.
662 if (last_timeout
&& (exit_mask
|| acct
->nr_workers
> 1)) {
664 raw_spin_unlock(&wq
->lock
);
665 __set_current_state(TASK_RUNNING
);
668 last_timeout
= false;
669 __io_worker_idle(wq
, worker
);
670 raw_spin_unlock(&wq
->lock
);
671 if (io_run_task_work())
673 ret
= schedule_timeout(WORKER_IDLE_TIMEOUT
);
674 if (signal_pending(current
)) {
677 if (!get_signal(&ksig
))
683 exit_mask
= !cpumask_test_cpu(raw_smp_processor_id(),
688 if (test_bit(IO_WQ_BIT_EXIT
, &wq
->state
) && io_acct_run_queue(acct
))
689 io_worker_handle_work(acct
, worker
);
691 io_worker_exit(worker
);
696 * Called when a worker is scheduled in. Mark us as currently running.
698 void io_wq_worker_running(struct task_struct
*tsk
)
700 struct io_worker
*worker
= tsk
->worker_private
;
704 if (!test_bit(IO_WORKER_F_UP
, &worker
->flags
))
706 if (test_bit(IO_WORKER_F_RUNNING
, &worker
->flags
))
708 set_bit(IO_WORKER_F_RUNNING
, &worker
->flags
);
709 io_wq_inc_running(worker
);
713 * Called when worker is going to sleep. If there are no workers currently
714 * running and we have work pending, wake up a free one or create a new one.
716 void io_wq_worker_sleeping(struct task_struct
*tsk
)
718 struct io_worker
*worker
= tsk
->worker_private
;
722 if (!test_bit(IO_WORKER_F_UP
, &worker
->flags
))
724 if (!test_bit(IO_WORKER_F_RUNNING
, &worker
->flags
))
727 clear_bit(IO_WORKER_F_RUNNING
, &worker
->flags
);
728 io_wq_dec_running(worker
);
731 static void io_init_new_worker(struct io_wq
*wq
, struct io_worker
*worker
,
732 struct task_struct
*tsk
)
734 tsk
->worker_private
= worker
;
736 set_cpus_allowed_ptr(tsk
, wq
->cpu_mask
);
738 raw_spin_lock(&wq
->lock
);
739 hlist_nulls_add_head_rcu(&worker
->nulls_node
, &wq
->free_list
);
740 list_add_tail_rcu(&worker
->all_list
, &wq
->all_list
);
741 set_bit(IO_WORKER_F_FREE
, &worker
->flags
);
742 raw_spin_unlock(&wq
->lock
);
743 wake_up_new_task(tsk
);
746 static bool io_wq_work_match_all(struct io_wq_work
*work
, void *data
)
751 static inline bool io_should_retry_thread(struct io_worker
*worker
, long err
)
754 * Prevent perpetual task_work retry, if the task (or its group) is
757 if (fatal_signal_pending(current
))
759 if (worker
->init_retries
++ >= WORKER_INIT_LIMIT
)
765 case -ERESTARTNOINTR
:
766 case -ERESTARTNOHAND
:
773 static void create_worker_cont(struct callback_head
*cb
)
775 struct io_worker
*worker
;
776 struct task_struct
*tsk
;
779 worker
= container_of(cb
, struct io_worker
, create_work
);
780 clear_bit_unlock(0, &worker
->create_state
);
782 tsk
= create_io_thread(io_wq_worker
, worker
, NUMA_NO_NODE
);
784 io_init_new_worker(wq
, worker
, tsk
);
785 io_worker_release(worker
);
787 } else if (!io_should_retry_thread(worker
, PTR_ERR(tsk
))) {
788 struct io_wq_acct
*acct
= io_wq_get_acct(worker
);
790 atomic_dec(&acct
->nr_running
);
791 raw_spin_lock(&wq
->lock
);
793 if (!acct
->nr_workers
) {
794 struct io_cb_cancel_data match
= {
795 .fn
= io_wq_work_match_all
,
799 raw_spin_unlock(&wq
->lock
);
800 while (io_acct_cancel_pending_work(wq
, acct
, &match
))
803 raw_spin_unlock(&wq
->lock
);
805 io_worker_ref_put(wq
);
810 /* re-create attempts grab a new worker ref, drop the existing one */
811 io_worker_release(worker
);
812 schedule_work(&worker
->work
);
815 static void io_workqueue_create(struct work_struct
*work
)
817 struct io_worker
*worker
= container_of(work
, struct io_worker
, work
);
818 struct io_wq_acct
*acct
= io_wq_get_acct(worker
);
820 if (!io_queue_worker_create(worker
, acct
, create_worker_cont
))
824 static bool create_io_worker(struct io_wq
*wq
, int index
)
826 struct io_wq_acct
*acct
= &wq
->acct
[index
];
827 struct io_worker
*worker
;
828 struct task_struct
*tsk
;
830 __set_current_state(TASK_RUNNING
);
832 worker
= kzalloc(sizeof(*worker
), GFP_KERNEL
);
835 atomic_dec(&acct
->nr_running
);
836 raw_spin_lock(&wq
->lock
);
838 raw_spin_unlock(&wq
->lock
);
839 io_worker_ref_put(wq
);
843 refcount_set(&worker
->ref
, 1);
845 raw_spin_lock_init(&worker
->lock
);
846 init_completion(&worker
->ref_done
);
848 if (index
== IO_WQ_ACCT_BOUND
)
849 set_bit(IO_WORKER_F_BOUND
, &worker
->flags
);
851 tsk
= create_io_thread(io_wq_worker
, worker
, NUMA_NO_NODE
);
853 io_init_new_worker(wq
, worker
, tsk
);
854 } else if (!io_should_retry_thread(worker
, PTR_ERR(tsk
))) {
858 INIT_WORK(&worker
->work
, io_workqueue_create
);
859 schedule_work(&worker
->work
);
866 * Iterate the passed in list and call the specific function for each
867 * worker that isn't exiting
869 static bool io_wq_for_each_worker(struct io_wq
*wq
,
870 bool (*func
)(struct io_worker
*, void *),
873 struct io_worker
*worker
;
876 list_for_each_entry_rcu(worker
, &wq
->all_list
, all_list
) {
877 if (io_worker_get(worker
)) {
878 /* no task if node is/was offline */
880 ret
= func(worker
, data
);
881 io_worker_release(worker
);
890 static bool io_wq_worker_wake(struct io_worker
*worker
, void *data
)
892 __set_notify_signal(worker
->task
);
893 wake_up_process(worker
->task
);
897 static void io_run_cancel(struct io_wq_work
*work
, struct io_wq
*wq
)
900 atomic_or(IO_WQ_WORK_CANCEL
, &work
->flags
);
902 work
= wq
->free_work(work
);
906 static void io_wq_insert_work(struct io_wq
*wq
, struct io_wq_work
*work
)
908 struct io_wq_acct
*acct
= io_work_get_acct(wq
, work
);
910 struct io_wq_work
*tail
;
912 if (!io_wq_is_hashed(work
)) {
914 wq_list_add_tail(&work
->list
, &acct
->work_list
);
918 hash
= io_get_work_hash(work
);
919 tail
= wq
->hash_tail
[hash
];
920 wq
->hash_tail
[hash
] = work
;
924 wq_list_add_after(&work
->list
, &tail
->list
, &acct
->work_list
);
927 static bool io_wq_work_match_item(struct io_wq_work
*work
, void *data
)
932 void io_wq_enqueue(struct io_wq
*wq
, struct io_wq_work
*work
)
934 struct io_wq_acct
*acct
= io_work_get_acct(wq
, work
);
935 unsigned int work_flags
= atomic_read(&work
->flags
);
936 struct io_cb_cancel_data match
= {
937 .fn
= io_wq_work_match_item
,
944 * If io-wq is exiting for this task, or if the request has explicitly
945 * been marked as one that should not get executed, cancel it here.
947 if (test_bit(IO_WQ_BIT_EXIT
, &wq
->state
) ||
948 (work_flags
& IO_WQ_WORK_CANCEL
)) {
949 io_run_cancel(work
, wq
);
953 raw_spin_lock(&acct
->lock
);
954 io_wq_insert_work(wq
, work
);
955 clear_bit(IO_ACCT_STALLED_BIT
, &acct
->flags
);
956 raw_spin_unlock(&acct
->lock
);
959 do_create
= !io_wq_activate_free_worker(wq
, acct
);
962 if (do_create
&& ((work_flags
& IO_WQ_WORK_CONCURRENT
) ||
963 !atomic_read(&acct
->nr_running
))) {
966 did_create
= io_wq_create_worker(wq
, acct
);
967 if (likely(did_create
))
970 raw_spin_lock(&wq
->lock
);
971 if (acct
->nr_workers
) {
972 raw_spin_unlock(&wq
->lock
);
975 raw_spin_unlock(&wq
->lock
);
977 /* fatal condition, failed to create the first worker */
978 io_acct_cancel_pending_work(wq
, acct
, &match
);
983 * Work items that hash to the same value will not be done in parallel.
984 * Used to limit concurrent writes, generally hashed by inode.
986 void io_wq_hash_work(struct io_wq_work
*work
, void *val
)
990 bit
= hash_ptr(val
, IO_WQ_HASH_ORDER
);
991 atomic_or(IO_WQ_WORK_HASHED
| (bit
<< IO_WQ_HASH_SHIFT
), &work
->flags
);
994 static bool __io_wq_worker_cancel(struct io_worker
*worker
,
995 struct io_cb_cancel_data
*match
,
996 struct io_wq_work
*work
)
998 if (work
&& match
->fn(work
, match
->data
)) {
999 atomic_or(IO_WQ_WORK_CANCEL
, &work
->flags
);
1000 __set_notify_signal(worker
->task
);
1007 static bool io_wq_worker_cancel(struct io_worker
*worker
, void *data
)
1009 struct io_cb_cancel_data
*match
= data
;
1012 * Hold the lock to avoid ->cur_work going out of scope, caller
1013 * may dereference the passed in work.
1015 raw_spin_lock(&worker
->lock
);
1016 if (__io_wq_worker_cancel(worker
, match
, worker
->cur_work
))
1017 match
->nr_running
++;
1018 raw_spin_unlock(&worker
->lock
);
1020 return match
->nr_running
&& !match
->cancel_all
;
1023 static inline void io_wq_remove_pending(struct io_wq
*wq
,
1024 struct io_wq_work
*work
,
1025 struct io_wq_work_node
*prev
)
1027 struct io_wq_acct
*acct
= io_work_get_acct(wq
, work
);
1028 unsigned int hash
= io_get_work_hash(work
);
1029 struct io_wq_work
*prev_work
= NULL
;
1031 if (io_wq_is_hashed(work
) && work
== wq
->hash_tail
[hash
]) {
1033 prev_work
= container_of(prev
, struct io_wq_work
, list
);
1034 if (prev_work
&& io_get_work_hash(prev_work
) == hash
)
1035 wq
->hash_tail
[hash
] = prev_work
;
1037 wq
->hash_tail
[hash
] = NULL
;
1039 wq_list_del(&acct
->work_list
, &work
->list
, prev
);
1042 static bool io_acct_cancel_pending_work(struct io_wq
*wq
,
1043 struct io_wq_acct
*acct
,
1044 struct io_cb_cancel_data
*match
)
1046 struct io_wq_work_node
*node
, *prev
;
1047 struct io_wq_work
*work
;
1049 raw_spin_lock(&acct
->lock
);
1050 wq_list_for_each(node
, prev
, &acct
->work_list
) {
1051 work
= container_of(node
, struct io_wq_work
, list
);
1052 if (!match
->fn(work
, match
->data
))
1054 io_wq_remove_pending(wq
, work
, prev
);
1055 raw_spin_unlock(&acct
->lock
);
1056 io_run_cancel(work
, wq
);
1057 match
->nr_pending
++;
1058 /* not safe to continue after unlock */
1061 raw_spin_unlock(&acct
->lock
);
1066 static void io_wq_cancel_pending_work(struct io_wq
*wq
,
1067 struct io_cb_cancel_data
*match
)
1071 for (i
= 0; i
< IO_WQ_ACCT_NR
; i
++) {
1072 struct io_wq_acct
*acct
= io_get_acct(wq
, i
== 0);
1074 if (io_acct_cancel_pending_work(wq
, acct
, match
)) {
1075 if (match
->cancel_all
)
1082 static void io_wq_cancel_running_work(struct io_wq
*wq
,
1083 struct io_cb_cancel_data
*match
)
1086 io_wq_for_each_worker(wq
, io_wq_worker_cancel
, match
);
1090 enum io_wq_cancel
io_wq_cancel_cb(struct io_wq
*wq
, work_cancel_fn
*cancel
,
1091 void *data
, bool cancel_all
)
1093 struct io_cb_cancel_data match
= {
1096 .cancel_all
= cancel_all
,
1100 * First check pending list, if we're lucky we can just remove it
1101 * from there. CANCEL_OK means that the work is returned as-new,
1102 * no completion will be posted for it.
1104 * Then check if a free (going busy) or busy worker has the work
1105 * currently running. If we find it there, we'll return CANCEL_RUNNING
1106 * as an indication that we attempt to signal cancellation. The
1107 * completion will run normally in this case.
1109 * Do both of these while holding the wq->lock, to ensure that
1110 * we'll find a work item regardless of state.
1112 io_wq_cancel_pending_work(wq
, &match
);
1113 if (match
.nr_pending
&& !match
.cancel_all
)
1114 return IO_WQ_CANCEL_OK
;
1116 raw_spin_lock(&wq
->lock
);
1117 io_wq_cancel_running_work(wq
, &match
);
1118 raw_spin_unlock(&wq
->lock
);
1119 if (match
.nr_running
&& !match
.cancel_all
)
1120 return IO_WQ_CANCEL_RUNNING
;
1122 if (match
.nr_running
)
1123 return IO_WQ_CANCEL_RUNNING
;
1124 if (match
.nr_pending
)
1125 return IO_WQ_CANCEL_OK
;
1126 return IO_WQ_CANCEL_NOTFOUND
;
1129 static int io_wq_hash_wake(struct wait_queue_entry
*wait
, unsigned mode
,
1130 int sync
, void *key
)
1132 struct io_wq
*wq
= container_of(wait
, struct io_wq
, wait
);
1135 list_del_init(&wait
->entry
);
1138 for (i
= 0; i
< IO_WQ_ACCT_NR
; i
++) {
1139 struct io_wq_acct
*acct
= &wq
->acct
[i
];
1141 if (test_and_clear_bit(IO_ACCT_STALLED_BIT
, &acct
->flags
))
1142 io_wq_activate_free_worker(wq
, acct
);
1148 struct io_wq
*io_wq_create(unsigned bounded
, struct io_wq_data
*data
)
1153 if (WARN_ON_ONCE(!data
->free_work
|| !data
->do_work
))
1154 return ERR_PTR(-EINVAL
);
1155 if (WARN_ON_ONCE(!bounded
))
1156 return ERR_PTR(-EINVAL
);
1158 wq
= kzalloc(sizeof(struct io_wq
), GFP_KERNEL
);
1160 return ERR_PTR(-ENOMEM
);
1162 refcount_inc(&data
->hash
->refs
);
1163 wq
->hash
= data
->hash
;
1164 wq
->free_work
= data
->free_work
;
1165 wq
->do_work
= data
->do_work
;
1169 if (!alloc_cpumask_var(&wq
->cpu_mask
, GFP_KERNEL
))
1171 cpuset_cpus_allowed(data
->task
, wq
->cpu_mask
);
1172 wq
->acct
[IO_WQ_ACCT_BOUND
].max_workers
= bounded
;
1173 wq
->acct
[IO_WQ_ACCT_UNBOUND
].max_workers
=
1174 task_rlimit(current
, RLIMIT_NPROC
);
1175 INIT_LIST_HEAD(&wq
->wait
.entry
);
1176 wq
->wait
.func
= io_wq_hash_wake
;
1177 for (i
= 0; i
< IO_WQ_ACCT_NR
; i
++) {
1178 struct io_wq_acct
*acct
= &wq
->acct
[i
];
1181 atomic_set(&acct
->nr_running
, 0);
1182 INIT_WQ_LIST(&acct
->work_list
);
1183 raw_spin_lock_init(&acct
->lock
);
1186 raw_spin_lock_init(&wq
->lock
);
1187 INIT_HLIST_NULLS_HEAD(&wq
->free_list
, 0);
1188 INIT_LIST_HEAD(&wq
->all_list
);
1190 wq
->task
= get_task_struct(data
->task
);
1191 atomic_set(&wq
->worker_refs
, 1);
1192 init_completion(&wq
->worker_done
);
1193 ret
= cpuhp_state_add_instance_nocalls(io_wq_online
, &wq
->cpuhp_node
);
1199 io_wq_put_hash(data
->hash
);
1200 free_cpumask_var(wq
->cpu_mask
);
1202 return ERR_PTR(ret
);
1205 static bool io_task_work_match(struct callback_head
*cb
, void *data
)
1207 struct io_worker
*worker
;
1209 if (cb
->func
!= create_worker_cb
&& cb
->func
!= create_worker_cont
)
1211 worker
= container_of(cb
, struct io_worker
, create_work
);
1212 return worker
->wq
== data
;
1215 void io_wq_exit_start(struct io_wq
*wq
)
1217 set_bit(IO_WQ_BIT_EXIT
, &wq
->state
);
1220 static void io_wq_cancel_tw_create(struct io_wq
*wq
)
1222 struct callback_head
*cb
;
1224 while ((cb
= task_work_cancel_match(wq
->task
, io_task_work_match
, wq
)) != NULL
) {
1225 struct io_worker
*worker
;
1227 worker
= container_of(cb
, struct io_worker
, create_work
);
1228 io_worker_cancel_cb(worker
);
1230 * Only the worker continuation helper has worker allocated and
1231 * hence needs freeing.
1233 if (cb
->func
== create_worker_cont
)
1238 static void io_wq_exit_workers(struct io_wq
*wq
)
1243 io_wq_cancel_tw_create(wq
);
1246 io_wq_for_each_worker(wq
, io_wq_worker_wake
, NULL
);
1248 io_worker_ref_put(wq
);
1249 wait_for_completion(&wq
->worker_done
);
1251 spin_lock_irq(&wq
->hash
->wait
.lock
);
1252 list_del_init(&wq
->wait
.entry
);
1253 spin_unlock_irq(&wq
->hash
->wait
.lock
);
1255 put_task_struct(wq
->task
);
1259 static void io_wq_destroy(struct io_wq
*wq
)
1261 struct io_cb_cancel_data match
= {
1262 .fn
= io_wq_work_match_all
,
1266 cpuhp_state_remove_instance_nocalls(io_wq_online
, &wq
->cpuhp_node
);
1267 io_wq_cancel_pending_work(wq
, &match
);
1268 free_cpumask_var(wq
->cpu_mask
);
1269 io_wq_put_hash(wq
->hash
);
1273 void io_wq_put_and_exit(struct io_wq
*wq
)
1275 WARN_ON_ONCE(!test_bit(IO_WQ_BIT_EXIT
, &wq
->state
));
1277 io_wq_exit_workers(wq
);
1281 struct online_data
{
1286 static bool io_wq_worker_affinity(struct io_worker
*worker
, void *data
)
1288 struct online_data
*od
= data
;
1291 cpumask_set_cpu(od
->cpu
, worker
->wq
->cpu_mask
);
1293 cpumask_clear_cpu(od
->cpu
, worker
->wq
->cpu_mask
);
1297 static int __io_wq_cpu_online(struct io_wq
*wq
, unsigned int cpu
, bool online
)
1299 struct online_data od
= {
1305 io_wq_for_each_worker(wq
, io_wq_worker_affinity
, &od
);
1310 static int io_wq_cpu_online(unsigned int cpu
, struct hlist_node
*node
)
1312 struct io_wq
*wq
= hlist_entry_safe(node
, struct io_wq
, cpuhp_node
);
1314 return __io_wq_cpu_online(wq
, cpu
, true);
1317 static int io_wq_cpu_offline(unsigned int cpu
, struct hlist_node
*node
)
1319 struct io_wq
*wq
= hlist_entry_safe(node
, struct io_wq
, cpuhp_node
);
1321 return __io_wq_cpu_online(wq
, cpu
, false);
1324 int io_wq_cpu_affinity(struct io_uring_task
*tctx
, cpumask_var_t mask
)
1326 cpumask_var_t allowed_mask
;
1329 if (!tctx
|| !tctx
->io_wq
)
1332 if (!alloc_cpumask_var(&allowed_mask
, GFP_KERNEL
))
1336 cpuset_cpus_allowed(tctx
->io_wq
->task
, allowed_mask
);
1338 if (cpumask_subset(mask
, allowed_mask
))
1339 cpumask_copy(tctx
->io_wq
->cpu_mask
, mask
);
1343 cpumask_copy(tctx
->io_wq
->cpu_mask
, allowed_mask
);
1347 free_cpumask_var(allowed_mask
);
1352 * Set max number of unbounded workers, returns old value. If new_count is 0,
1353 * then just return the old value.
1355 int io_wq_max_workers(struct io_wq
*wq
, int *new_count
)
1357 struct io_wq_acct
*acct
;
1358 int prev
[IO_WQ_ACCT_NR
];
1361 BUILD_BUG_ON((int) IO_WQ_ACCT_BOUND
!= (int) IO_WQ_BOUND
);
1362 BUILD_BUG_ON((int) IO_WQ_ACCT_UNBOUND
!= (int) IO_WQ_UNBOUND
);
1363 BUILD_BUG_ON((int) IO_WQ_ACCT_NR
!= 2);
1365 for (i
= 0; i
< IO_WQ_ACCT_NR
; i
++) {
1366 if (new_count
[i
] > task_rlimit(current
, RLIMIT_NPROC
))
1367 new_count
[i
] = task_rlimit(current
, RLIMIT_NPROC
);
1370 for (i
= 0; i
< IO_WQ_ACCT_NR
; i
++)
1375 raw_spin_lock(&wq
->lock
);
1376 for (i
= 0; i
< IO_WQ_ACCT_NR
; i
++) {
1377 acct
= &wq
->acct
[i
];
1378 prev
[i
] = max_t(int, acct
->max_workers
, prev
[i
]);
1380 acct
->max_workers
= new_count
[i
];
1382 raw_spin_unlock(&wq
->lock
);
1385 for (i
= 0; i
< IO_WQ_ACCT_NR
; i
++)
1386 new_count
[i
] = prev
[i
];
1391 static __init
int io_wq_init(void)
1395 ret
= cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN
, "io-wq/online",
1396 io_wq_cpu_online
, io_wq_cpu_offline
);
1402 subsys_initcall(io_wq_init
);