blob: dc430381b6941376c848011c329b32b82fd2f0e5 [file] [log] [blame]
Jens Axboe771b53d02019-10-22 10:25:58 -06001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Basic worker thread pool for io_uring
4 *
5 * Copyright (C) 2019 Jens Axboe
6 *
7 */
8#include <linux/kernel.h>
9#include <linux/init.h>
10#include <linux/errno.h>
11#include <linux/sched/signal.h>
12#include <linux/mm.h>
Jens Axboe771b53d02019-10-22 10:25:58 -060013#include <linux/sched/mm.h>
14#include <linux/percpu.h>
15#include <linux/slab.h>
Jens Axboe771b53d02019-10-22 10:25:58 -060016#include <linux/rculist_nulls.h>
Jens Axboe43c01fb2020-10-22 09:02:50 -060017#include <linux/cpu.h>
Jens Axboe3bfe6102021-02-16 14:15:30 -070018#include <linux/tracehook.h>
Jens Axboe771b53d02019-10-22 10:25:58 -060019
Jens Axboe43c01fb2020-10-22 09:02:50 -060020#include "../kernel/sched/sched.h"
Jens Axboe771b53d02019-10-22 10:25:58 -060021#include "io-wq.h"
22
23#define WORKER_IDLE_TIMEOUT (5 * HZ)
24
25enum {
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 */
Jens Axboe145cc8c2020-09-26 12:37:46 -060029 IO_WORKER_F_FIXED = 8, /* static idle worker */
30 IO_WORKER_F_BOUND = 16, /* is doing bounded work */
Jens Axboe771b53d02019-10-22 10:25:58 -060031};
32
33enum {
34 IO_WQ_BIT_EXIT = 0, /* wq exiting */
Jens Axboe771b53d02019-10-22 10:25:58 -060035};
36
37enum {
38 IO_WQE_FLAG_STALLED = 1, /* stalled on hash */
39};
40
41/*
42 * One for each thread in a wqe pool
43 */
44struct io_worker {
45 refcount_t ref;
46 unsigned flags;
47 struct hlist_nulls_node nulls_node;
Jens Axboee61df662019-11-13 13:54:49 -070048 struct list_head all_list;
Jens Axboe771b53d02019-10-22 10:25:58 -060049 struct task_struct *task;
Jens Axboe771b53d02019-10-22 10:25:58 -060050 struct io_wqe *wqe;
Jens Axboe36c2f922019-11-13 09:43:34 -070051
Jens Axboe771b53d02019-10-22 10:25:58 -060052 struct io_wq_work *cur_work;
Jens Axboe36c2f922019-11-13 09:43:34 -070053 spinlock_t lock;
Jens Axboe771b53d02019-10-22 10:25:58 -060054
Jens Axboeeb2de942021-02-23 19:59:06 -070055 struct completion ref_done;
Jens Axboe65d43022021-02-26 09:50:55 -070056 struct completion started;
Jens Axboeeb2de942021-02-23 19:59:06 -070057
Jens Axboe771b53d02019-10-22 10:25:58 -060058 struct rcu_head rcu;
Jens Axboe771b53d02019-10-22 10:25:58 -060059};
60
Jens Axboe771b53d02019-10-22 10:25:58 -060061#if BITS_PER_LONG == 64
62#define IO_WQ_HASH_ORDER 6
63#else
64#define IO_WQ_HASH_ORDER 5
65#endif
66
Pavel Begunkov86f3cd12020-03-23 22:57:22 +030067#define IO_WQ_NR_HASH_BUCKETS (1u << IO_WQ_HASH_ORDER)
68
Jens Axboec5def4a2019-11-07 11:41:16 -070069struct io_wqe_acct {
70 unsigned nr_workers;
71 unsigned max_workers;
72 atomic_t nr_running;
73};
74
75enum {
76 IO_WQ_ACCT_BOUND,
77 IO_WQ_ACCT_UNBOUND,
78};
79
Jens Axboe771b53d02019-10-22 10:25:58 -060080/*
81 * Per-node worker thread pool
82 */
83struct io_wqe {
84 struct {
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +020085 raw_spinlock_t lock;
Jens Axboe6206f0e2019-11-26 11:59:32 -070086 struct io_wq_work_list work_list;
Jens Axboe771b53d02019-10-22 10:25:58 -060087 unsigned flags;
88 } ____cacheline_aligned_in_smp;
89
90 int node;
Jens Axboec5def4a2019-11-07 11:41:16 -070091 struct io_wqe_acct acct[2];
Jens Axboe771b53d02019-10-22 10:25:58 -060092
Jens Axboe021d1cd2019-11-14 08:00:41 -070093 struct hlist_nulls_head free_list;
Jens Axboee61df662019-11-13 13:54:49 -070094 struct list_head all_list;
Jens Axboe771b53d02019-10-22 10:25:58 -060095
Jens Axboee9418942021-02-19 12:33:30 -070096 struct wait_queue_entry wait;
97
Jens Axboe771b53d02019-10-22 10:25:58 -060098 struct io_wq *wq;
Pavel Begunkov86f3cd12020-03-23 22:57:22 +030099 struct io_wq_work *hash_tail[IO_WQ_NR_HASH_BUCKETS];
Jens Axboe771b53d02019-10-22 10:25:58 -0600100};
101
102/*
103 * Per io_wq state
104 */
105struct io_wq {
106 struct io_wqe **wqes;
107 unsigned long state;
Jens Axboe771b53d02019-10-22 10:25:58 -0600108
Pavel Begunkove9fd9392020-03-04 16:14:12 +0300109 free_work_fn *free_work;
Pavel Begunkovf5fa38c2020-06-08 21:08:20 +0300110 io_wq_work_fn *do_work;
Jens Axboe7d723062019-11-12 22:31:31 -0700111
Jens Axboe771b53d02019-10-22 10:25:58 -0600112 struct task_struct *manager;
Jens Axboec5def4a2019-11-07 11:41:16 -0700113 struct user_struct *user;
Jens Axboee9418942021-02-19 12:33:30 -0700114
115 struct io_wq_hash *hash;
116
Jens Axboe771b53d02019-10-22 10:25:58 -0600117 refcount_t refs;
Jens Axboedbf99622021-02-26 09:53:17 -0700118 struct completion started;
Jens Axboed364d9e2021-02-26 09:56:32 -0700119 struct completion exited;
Jens Axboe848f7e12020-01-23 15:33:32 -0700120
Jens Axboefb3a1f62021-02-26 09:47:20 -0700121 atomic_t worker_refs;
122 struct completion worker_done;
123
Jens Axboe43c01fb2020-10-22 09:02:50 -0600124 struct hlist_node cpuhp_node;
Jens Axboe3bfe6102021-02-16 14:15:30 -0700125
126 pid_t task_pid;
Jens Axboe771b53d02019-10-22 10:25:58 -0600127};
128
Jens Axboe43c01fb2020-10-22 09:02:50 -0600129static enum cpuhp_state io_wq_online;
130
Jens Axboe771b53d02019-10-22 10:25:58 -0600131static bool io_worker_get(struct io_worker *worker)
132{
133 return refcount_inc_not_zero(&worker->ref);
134}
135
136static void io_worker_release(struct io_worker *worker)
137{
138 if (refcount_dec_and_test(&worker->ref))
Jens Axboeeb2de942021-02-23 19:59:06 -0700139 complete(&worker->ref_done);
Jens Axboe771b53d02019-10-22 10:25:58 -0600140}
141
Jens Axboec5def4a2019-11-07 11:41:16 -0700142static inline struct io_wqe_acct *io_work_get_acct(struct io_wqe *wqe,
143 struct io_wq_work *work)
144{
145 if (work->flags & IO_WQ_WORK_UNBOUND)
146 return &wqe->acct[IO_WQ_ACCT_UNBOUND];
147
148 return &wqe->acct[IO_WQ_ACCT_BOUND];
149}
150
Jens Axboe958234d2021-02-17 09:00:57 -0700151static inline struct io_wqe_acct *io_wqe_get_acct(struct io_worker *worker)
Jens Axboec5def4a2019-11-07 11:41:16 -0700152{
Jens Axboe958234d2021-02-17 09:00:57 -0700153 struct io_wqe *wqe = worker->wqe;
154
Jens Axboec5def4a2019-11-07 11:41:16 -0700155 if (worker->flags & IO_WORKER_F_BOUND)
156 return &wqe->acct[IO_WQ_ACCT_BOUND];
157
158 return &wqe->acct[IO_WQ_ACCT_UNBOUND];
159}
160
Jens Axboe771b53d02019-10-22 10:25:58 -0600161static void io_worker_exit(struct io_worker *worker)
162{
163 struct io_wqe *wqe = worker->wqe;
Jens Axboe958234d2021-02-17 09:00:57 -0700164 struct io_wqe_acct *acct = io_wqe_get_acct(worker);
Jens Axboebf1daa42021-02-16 18:00:55 -0700165 unsigned flags;
Jens Axboe771b53d02019-10-22 10:25:58 -0600166
Jens Axboeeb2de942021-02-23 19:59:06 -0700167 if (refcount_dec_and_test(&worker->ref))
168 complete(&worker->ref_done);
169 wait_for_completion(&worker->ref_done);
Jens Axboe771b53d02019-10-22 10:25:58 -0600170
171 preempt_disable();
172 current->flags &= ~PF_IO_WORKER;
Jens Axboebf1daa42021-02-16 18:00:55 -0700173 flags = worker->flags;
174 worker->flags = 0;
175 if (flags & IO_WORKER_F_RUNNING)
Jens Axboec5def4a2019-11-07 11:41:16 -0700176 atomic_dec(&acct->nr_running);
Jens Axboe771b53d02019-10-22 10:25:58 -0600177 worker->flags = 0;
178 preempt_enable();
179
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200180 raw_spin_lock_irq(&wqe->lock);
Jens Axboebf1daa42021-02-16 18:00:55 -0700181 if (flags & IO_WORKER_F_FREE)
182 hlist_nulls_del_rcu(&worker->nulls_node);
Jens Axboee61df662019-11-13 13:54:49 -0700183 list_del_rcu(&worker->all_list);
Jens Axboec5def4a2019-11-07 11:41:16 -0700184 acct->nr_workers--;
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200185 raw_spin_unlock_irq(&wqe->lock);
Jens Axboe771b53d02019-10-22 10:25:58 -0600186
YueHaibing364b05f2019-11-02 15:55:01 +0800187 kfree_rcu(worker, rcu);
Jens Axboefb3a1f62021-02-26 09:47:20 -0700188 if (atomic_dec_and_test(&wqe->wq->worker_refs))
189 complete(&wqe->wq->worker_done);
Jens Axboe771b53d02019-10-22 10:25:58 -0600190}
191
Jens Axboec5def4a2019-11-07 11:41:16 -0700192static inline bool io_wqe_run_queue(struct io_wqe *wqe)
193 __must_hold(wqe->lock)
194{
Jens Axboe6206f0e2019-11-26 11:59:32 -0700195 if (!wq_list_empty(&wqe->work_list) &&
196 !(wqe->flags & IO_WQE_FLAG_STALLED))
Jens Axboec5def4a2019-11-07 11:41:16 -0700197 return true;
198 return false;
199}
200
201/*
202 * Check head of free list for an available worker. If one isn't available,
203 * caller must wake up the wq manager to create one.
204 */
205static bool io_wqe_activate_free_worker(struct io_wqe *wqe)
206 __must_hold(RCU)
207{
208 struct hlist_nulls_node *n;
209 struct io_worker *worker;
210
Jens Axboe021d1cd2019-11-14 08:00:41 -0700211 n = rcu_dereference(hlist_nulls_first_rcu(&wqe->free_list));
Jens Axboec5def4a2019-11-07 11:41:16 -0700212 if (is_a_nulls(n))
213 return false;
214
215 worker = hlist_nulls_entry(n, struct io_worker, nulls_node);
216 if (io_worker_get(worker)) {
Jens Axboe506d95f2019-12-07 21:03:59 -0700217 wake_up_process(worker->task);
Jens Axboec5def4a2019-11-07 11:41:16 -0700218 io_worker_release(worker);
219 return true;
220 }
221
222 return false;
223}
224
225/*
226 * We need a worker. If we find a free one, we're good. If not, and we're
227 * below the max number of workers, wake up the manager to create one.
228 */
229static void io_wqe_wake_worker(struct io_wqe *wqe, struct io_wqe_acct *acct)
230{
231 bool ret;
232
233 /*
234 * Most likely an attempt to queue unbounded work on an io_wq that
235 * wasn't setup with any unbounded workers.
236 */
237 WARN_ON_ONCE(!acct->max_workers);
238
239 rcu_read_lock();
240 ret = io_wqe_activate_free_worker(wqe);
241 rcu_read_unlock();
242
243 if (!ret && acct->nr_workers < acct->max_workers)
244 wake_up_process(wqe->wq->manager);
245}
246
Jens Axboe958234d2021-02-17 09:00:57 -0700247static void io_wqe_inc_running(struct io_worker *worker)
Jens Axboec5def4a2019-11-07 11:41:16 -0700248{
Jens Axboe958234d2021-02-17 09:00:57 -0700249 struct io_wqe_acct *acct = io_wqe_get_acct(worker);
Jens Axboec5def4a2019-11-07 11:41:16 -0700250
251 atomic_inc(&acct->nr_running);
252}
253
Jens Axboe958234d2021-02-17 09:00:57 -0700254static void io_wqe_dec_running(struct io_worker *worker)
Jens Axboec5def4a2019-11-07 11:41:16 -0700255 __must_hold(wqe->lock)
256{
Jens Axboe958234d2021-02-17 09:00:57 -0700257 struct io_wqe_acct *acct = io_wqe_get_acct(worker);
258 struct io_wqe *wqe = worker->wqe;
Jens Axboec5def4a2019-11-07 11:41:16 -0700259
260 if (atomic_dec_and_test(&acct->nr_running) && io_wqe_run_queue(wqe))
261 io_wqe_wake_worker(wqe, acct);
262}
263
Jens Axboe958234d2021-02-17 09:00:57 -0700264static void io_worker_start(struct io_worker *worker)
Jens Axboe771b53d02019-10-22 10:25:58 -0600265{
Jens Axboe771b53d02019-10-22 10:25:58 -0600266 worker->flags |= (IO_WORKER_F_UP | IO_WORKER_F_RUNNING);
Jens Axboe958234d2021-02-17 09:00:57 -0700267 io_wqe_inc_running(worker);
Jens Axboe65d43022021-02-26 09:50:55 -0700268 complete(&worker->started);
Jens Axboe771b53d02019-10-22 10:25:58 -0600269}
270
271/*
272 * Worker will start processing some work. Move it to the busy list, if
273 * it's currently on the freelist
274 */
275static void __io_worker_busy(struct io_wqe *wqe, struct io_worker *worker,
276 struct io_wq_work *work)
277 __must_hold(wqe->lock)
278{
Jens Axboec5def4a2019-11-07 11:41:16 -0700279 bool worker_bound, work_bound;
280
Jens Axboe771b53d02019-10-22 10:25:58 -0600281 if (worker->flags & IO_WORKER_F_FREE) {
282 worker->flags &= ~IO_WORKER_F_FREE;
283 hlist_nulls_del_init_rcu(&worker->nulls_node);
Jens Axboe771b53d02019-10-22 10:25:58 -0600284 }
Jens Axboec5def4a2019-11-07 11:41:16 -0700285
286 /*
287 * If worker is moving from bound to unbound (or vice versa), then
288 * ensure we update the running accounting.
289 */
Dan Carpenterb2e9c7d62019-11-19 09:22:16 +0300290 worker_bound = (worker->flags & IO_WORKER_F_BOUND) != 0;
291 work_bound = (work->flags & IO_WQ_WORK_UNBOUND) == 0;
292 if (worker_bound != work_bound) {
Jens Axboe958234d2021-02-17 09:00:57 -0700293 io_wqe_dec_running(worker);
Jens Axboec5def4a2019-11-07 11:41:16 -0700294 if (work_bound) {
295 worker->flags |= IO_WORKER_F_BOUND;
296 wqe->acct[IO_WQ_ACCT_UNBOUND].nr_workers--;
297 wqe->acct[IO_WQ_ACCT_BOUND].nr_workers++;
Jens Axboec5def4a2019-11-07 11:41:16 -0700298 } else {
299 worker->flags &= ~IO_WORKER_F_BOUND;
300 wqe->acct[IO_WQ_ACCT_UNBOUND].nr_workers++;
301 wqe->acct[IO_WQ_ACCT_BOUND].nr_workers--;
Jens Axboec5def4a2019-11-07 11:41:16 -0700302 }
Jens Axboe958234d2021-02-17 09:00:57 -0700303 io_wqe_inc_running(worker);
Jens Axboec5def4a2019-11-07 11:41:16 -0700304 }
Jens Axboe771b53d02019-10-22 10:25:58 -0600305}
306
307/*
308 * No work, worker going to sleep. Move to freelist, and unuse mm if we
309 * have one attached. Dropping the mm may potentially sleep, so we drop
310 * the lock in that case and return success. Since the caller has to
311 * retry the loop in that case (we changed task state), we don't regrab
312 * the lock if we return success.
313 */
Jens Axboec6d77d92021-02-15 13:26:34 -0700314static void __io_worker_idle(struct io_wqe *wqe, struct io_worker *worker)
Jens Axboe771b53d02019-10-22 10:25:58 -0600315 __must_hold(wqe->lock)
316{
317 if (!(worker->flags & IO_WORKER_F_FREE)) {
318 worker->flags |= IO_WORKER_F_FREE;
Jens Axboe021d1cd2019-11-14 08:00:41 -0700319 hlist_nulls_add_head_rcu(&worker->nulls_node, &wqe->free_list);
Jens Axboe771b53d02019-10-22 10:25:58 -0600320 }
Jens Axboe771b53d02019-10-22 10:25:58 -0600321}
322
Pavel Begunkov60cf46a2020-03-14 00:31:05 +0300323static inline unsigned int io_get_work_hash(struct io_wq_work *work)
324{
325 return work->flags >> IO_WQ_HASH_SHIFT;
326}
327
Jens Axboee9418942021-02-19 12:33:30 -0700328static void io_wait_on_hash(struct io_wqe *wqe, unsigned int hash)
329{
330 struct io_wq *wq = wqe->wq;
331
332 spin_lock(&wq->hash->wait.lock);
333 if (list_empty(&wqe->wait.entry)) {
334 __add_wait_queue(&wq->hash->wait, &wqe->wait);
335 if (!test_bit(hash, &wq->hash->map)) {
336 __set_current_state(TASK_RUNNING);
337 list_del_init(&wqe->wait.entry);
338 }
339 }
340 spin_unlock(&wq->hash->wait.lock);
341}
342
Pavel Begunkov60cf46a2020-03-14 00:31:05 +0300343static struct io_wq_work *io_get_next_work(struct io_wqe *wqe)
Jens Axboe771b53d02019-10-22 10:25:58 -0600344 __must_hold(wqe->lock)
345{
Jens Axboe6206f0e2019-11-26 11:59:32 -0700346 struct io_wq_work_node *node, *prev;
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300347 struct io_wq_work *work, *tail;
Jens Axboee9418942021-02-19 12:33:30 -0700348 unsigned int stall_hash = -1U;
Jens Axboe771b53d02019-10-22 10:25:58 -0600349
Jens Axboe6206f0e2019-11-26 11:59:32 -0700350 wq_list_for_each(node, prev, &wqe->work_list) {
Jens Axboee9418942021-02-19 12:33:30 -0700351 unsigned int hash;
352
Jens Axboe6206f0e2019-11-26 11:59:32 -0700353 work = container_of(node, struct io_wq_work, list);
354
Jens Axboe771b53d02019-10-22 10:25:58 -0600355 /* not hashed, can run anytime */
Pavel Begunkov8766dd52020-03-14 00:31:04 +0300356 if (!io_wq_is_hashed(work)) {
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300357 wq_list_del(&wqe->work_list, node, prev);
Jens Axboe771b53d02019-10-22 10:25:58 -0600358 return work;
359 }
360
Pavel Begunkov60cf46a2020-03-14 00:31:05 +0300361 hash = io_get_work_hash(work);
Jens Axboee9418942021-02-19 12:33:30 -0700362 /* all items with this hash lie in [work, tail] */
363 tail = wqe->hash_tail[hash];
364
365 /* hashed, can run if not already running */
366 if (!test_and_set_bit(hash, &wqe->wq->hash->map)) {
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300367 wqe->hash_tail[hash] = NULL;
368 wq_list_cut(&wqe->work_list, &tail->list, prev);
Jens Axboe771b53d02019-10-22 10:25:58 -0600369 return work;
370 }
Jens Axboee9418942021-02-19 12:33:30 -0700371 if (stall_hash == -1U)
372 stall_hash = hash;
373 /* fast forward to a next hash, for-each will fix up @prev */
374 node = &tail->list;
375 }
376
377 if (stall_hash != -1U) {
378 raw_spin_unlock(&wqe->lock);
379 io_wait_on_hash(wqe, stall_hash);
380 raw_spin_lock(&wqe->lock);
Jens Axboe771b53d02019-10-22 10:25:58 -0600381 }
382
383 return NULL;
384}
385
Jens Axboe3bfe6102021-02-16 14:15:30 -0700386static void io_flush_signals(void)
Jens Axboecccf0ee2020-01-27 16:34:48 -0700387{
Jens Axboe3bfe6102021-02-16 14:15:30 -0700388 if (unlikely(test_tsk_thread_flag(current, TIF_NOTIFY_SIGNAL))) {
389 if (current->task_works)
390 task_work_run();
391 clear_tsk_thread_flag(current, TIF_NOTIFY_SIGNAL);
Jens Axboecccf0ee2020-01-27 16:34:48 -0700392 }
Pavel Begunkovdc026a72020-03-04 16:14:09 +0300393}
394
395static void io_assign_current_work(struct io_worker *worker,
396 struct io_wq_work *work)
397{
Pavel Begunkovd78298e2020-03-14 00:31:03 +0300398 if (work) {
Jens Axboe3bfe6102021-02-16 14:15:30 -0700399 io_flush_signals();
Pavel Begunkovd78298e2020-03-14 00:31:03 +0300400 cond_resched();
401 }
Pavel Begunkovdc026a72020-03-04 16:14:09 +0300402
403 spin_lock_irq(&worker->lock);
404 worker->cur_work = work;
405 spin_unlock_irq(&worker->lock);
406}
407
Pavel Begunkov60cf46a2020-03-14 00:31:05 +0300408static void io_wqe_enqueue(struct io_wqe *wqe, struct io_wq_work *work);
409
Jens Axboe771b53d02019-10-22 10:25:58 -0600410static void io_worker_handle_work(struct io_worker *worker)
411 __releases(wqe->lock)
412{
Jens Axboe771b53d02019-10-22 10:25:58 -0600413 struct io_wqe *wqe = worker->wqe;
414 struct io_wq *wq = wqe->wq;
415
416 do {
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300417 struct io_wq_work *work;
Pavel Begunkovf462fd32020-03-04 16:14:11 +0300418get_next:
Jens Axboe771b53d02019-10-22 10:25:58 -0600419 /*
Jens Axboe771b53d02019-10-22 10:25:58 -0600420 * If we got some work, mark us as busy. If we didn't, but
421 * the list isn't empty, it means we stalled on hashed work.
422 * Mark us stalled so we don't keep looking for work when we
423 * can't make progress, any work completion or insertion will
424 * clear the stalled flag.
425 */
Pavel Begunkov60cf46a2020-03-14 00:31:05 +0300426 work = io_get_next_work(wqe);
Jens Axboe771b53d02019-10-22 10:25:58 -0600427 if (work)
428 __io_worker_busy(wqe, worker, work);
Jens Axboe6206f0e2019-11-26 11:59:32 -0700429 else if (!wq_list_empty(&wqe->work_list))
Jens Axboe771b53d02019-10-22 10:25:58 -0600430 wqe->flags |= IO_WQE_FLAG_STALLED;
431
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200432 raw_spin_unlock_irq(&wqe->lock);
Jens Axboe771b53d02019-10-22 10:25:58 -0600433 if (!work)
434 break;
Pavel Begunkov58e39312020-03-04 16:14:10 +0300435 io_assign_current_work(worker, work);
Jens Axboee9418942021-02-19 12:33:30 -0700436 __set_current_state(TASK_RUNNING);
Jens Axboe36c2f922019-11-13 09:43:34 -0700437
Pavel Begunkovdc026a72020-03-04 16:14:09 +0300438 /* handle a whole dependent link */
439 do {
Pavel Begunkov5280f7e2021-02-04 13:52:08 +0000440 struct io_wq_work *next_hashed, *linked;
Pavel Begunkovb089ed392020-07-25 14:42:00 +0300441 unsigned int hash = io_get_work_hash(work);
Hillf Dantonfd1c4bc2019-12-24 09:14:29 -0700442
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300443 next_hashed = wq_next_work(work);
Pavel Begunkov5280f7e2021-02-04 13:52:08 +0000444 wq->do_work(work);
445 io_assign_current_work(worker, NULL);
Jens Axboe36c2f922019-11-13 09:43:34 -0700446
Pavel Begunkov5280f7e2021-02-04 13:52:08 +0000447 linked = wq->free_work(work);
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300448 work = next_hashed;
449 if (!work && linked && !io_wq_is_hashed(linked)) {
450 work = linked;
451 linked = NULL;
452 }
453 io_assign_current_work(worker, work);
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300454 if (linked)
455 io_wqe_enqueue(wqe, linked);
456
457 if (hash != -1U && !next_hashed) {
Jens Axboee9418942021-02-19 12:33:30 -0700458 clear_bit(hash, &wq->hash->map);
459 if (wq_has_sleeper(&wq->hash->wait))
460 wake_up(&wq->hash->wait);
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200461 raw_spin_lock_irq(&wqe->lock);
Pavel Begunkovdc026a72020-03-04 16:14:09 +0300462 wqe->flags &= ~IO_WQE_FLAG_STALLED;
Pavel Begunkovf462fd32020-03-04 16:14:11 +0300463 /* skip unnecessary unlock-lock wqe->lock */
464 if (!work)
465 goto get_next;
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200466 raw_spin_unlock_irq(&wqe->lock);
Pavel Begunkovdc026a72020-03-04 16:14:09 +0300467 }
Pavel Begunkov58e39312020-03-04 16:14:10 +0300468 } while (work);
Jens Axboe36c2f922019-11-13 09:43:34 -0700469
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200470 raw_spin_lock_irq(&wqe->lock);
Jens Axboe771b53d02019-10-22 10:25:58 -0600471 } while (1);
472}
473
Jens Axboe771b53d02019-10-22 10:25:58 -0600474static int io_wqe_worker(void *data)
475{
476 struct io_worker *worker = data;
477 struct io_wqe *wqe = worker->wqe;
478 struct io_wq *wq = wqe->wq;
Jens Axboe771b53d02019-10-22 10:25:58 -0600479
Jens Axboe958234d2021-02-17 09:00:57 -0700480 io_worker_start(worker);
Jens Axboe771b53d02019-10-22 10:25:58 -0600481
482 while (!test_bit(IO_WQ_BIT_EXIT, &wq->state)) {
Jens Axboe506d95f2019-12-07 21:03:59 -0700483 set_current_state(TASK_INTERRUPTIBLE);
Jens Axboee995d512019-12-07 21:06:46 -0700484loop:
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200485 raw_spin_lock_irq(&wqe->lock);
Jens Axboe771b53d02019-10-22 10:25:58 -0600486 if (io_wqe_run_queue(wqe)) {
Jens Axboe771b53d02019-10-22 10:25:58 -0600487 io_worker_handle_work(worker);
Jens Axboee995d512019-12-07 21:06:46 -0700488 goto loop;
Jens Axboe771b53d02019-10-22 10:25:58 -0600489 }
Jens Axboec6d77d92021-02-15 13:26:34 -0700490 __io_worker_idle(wqe, worker);
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200491 raw_spin_unlock_irq(&wqe->lock);
Jens Axboe3bfe6102021-02-16 14:15:30 -0700492 io_flush_signals();
Jens Axboe771b53d02019-10-22 10:25:58 -0600493 if (schedule_timeout(WORKER_IDLE_TIMEOUT))
494 continue;
Jens Axboe3bfe6102021-02-16 14:15:30 -0700495 if (fatal_signal_pending(current))
496 break;
Jens Axboe771b53d02019-10-22 10:25:58 -0600497 /* timed out, exit unless we're the fixed worker */
498 if (test_bit(IO_WQ_BIT_EXIT, &wq->state) ||
499 !(worker->flags & IO_WORKER_F_FIXED))
500 break;
501 }
502
Jens Axboe771b53d02019-10-22 10:25:58 -0600503 if (test_bit(IO_WQ_BIT_EXIT, &wq->state)) {
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200504 raw_spin_lock_irq(&wqe->lock);
Jens Axboe6206f0e2019-11-26 11:59:32 -0700505 if (!wq_list_empty(&wqe->work_list))
Jens Axboe771b53d02019-10-22 10:25:58 -0600506 io_worker_handle_work(worker);
507 else
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200508 raw_spin_unlock_irq(&wqe->lock);
Jens Axboe771b53d02019-10-22 10:25:58 -0600509 }
510
511 io_worker_exit(worker);
512 return 0;
513}
514
515/*
Jens Axboe771b53d02019-10-22 10:25:58 -0600516 * Called when a worker is scheduled in. Mark us as currently running.
517 */
518void io_wq_worker_running(struct task_struct *tsk)
519{
Jens Axboe3bfe6102021-02-16 14:15:30 -0700520 struct io_worker *worker = tsk->pf_io_worker;
Jens Axboe771b53d02019-10-22 10:25:58 -0600521
Jens Axboe3bfe6102021-02-16 14:15:30 -0700522 if (!worker)
523 return;
Jens Axboe771b53d02019-10-22 10:25:58 -0600524 if (!(worker->flags & IO_WORKER_F_UP))
525 return;
526 if (worker->flags & IO_WORKER_F_RUNNING)
527 return;
528 worker->flags |= IO_WORKER_F_RUNNING;
Jens Axboe958234d2021-02-17 09:00:57 -0700529 io_wqe_inc_running(worker);
Jens Axboe771b53d02019-10-22 10:25:58 -0600530}
531
532/*
533 * Called when worker is going to sleep. If there are no workers currently
534 * running and we have work pending, wake up a free one or have the manager
535 * set one up.
536 */
537void io_wq_worker_sleeping(struct task_struct *tsk)
538{
Jens Axboe3bfe6102021-02-16 14:15:30 -0700539 struct io_worker *worker = tsk->pf_io_worker;
Jens Axboe771b53d02019-10-22 10:25:58 -0600540
Jens Axboe3bfe6102021-02-16 14:15:30 -0700541 if (!worker)
542 return;
Jens Axboe771b53d02019-10-22 10:25:58 -0600543 if (!(worker->flags & IO_WORKER_F_UP))
544 return;
545 if (!(worker->flags & IO_WORKER_F_RUNNING))
546 return;
547
548 worker->flags &= ~IO_WORKER_F_RUNNING;
549
Jens Axboe3bfe6102021-02-16 14:15:30 -0700550 raw_spin_lock_irq(&worker->wqe->lock);
Jens Axboe958234d2021-02-17 09:00:57 -0700551 io_wqe_dec_running(worker);
Jens Axboe3bfe6102021-02-16 14:15:30 -0700552 raw_spin_unlock_irq(&worker->wqe->lock);
Jens Axboe771b53d02019-10-22 10:25:58 -0600553}
554
Jens Axboe3bfe6102021-02-16 14:15:30 -0700555static int task_thread(void *data, int index)
Jens Axboe771b53d02019-10-22 10:25:58 -0600556{
Jens Axboe3bfe6102021-02-16 14:15:30 -0700557 struct io_worker *worker = data;
558 struct io_wqe *wqe = worker->wqe;
Hillf Dantonc4068bf2020-09-26 21:26:55 +0800559 struct io_wqe_acct *acct = &wqe->acct[index];
Jens Axboe3bfe6102021-02-16 14:15:30 -0700560 struct io_wq *wq = wqe->wq;
561 char buf[TASK_COMM_LEN];
Jens Axboe771b53d02019-10-22 10:25:58 -0600562
Jens Axboe3bfe6102021-02-16 14:15:30 -0700563 sprintf(buf, "iou-wrk-%d", wq->task_pid);
564 set_task_comm(current, buf);
Jens Axboe771b53d02019-10-22 10:25:58 -0600565
Jens Axboe3bfe6102021-02-16 14:15:30 -0700566 current->pf_io_worker = worker;
567 worker->task = current;
Jens Axboe771b53d02019-10-22 10:25:58 -0600568
Jens Axboe3bfe6102021-02-16 14:15:30 -0700569 set_cpus_allowed_ptr(current, cpumask_of_node(wqe->node));
570 current->flags |= PF_NO_SETAFFINITY;
Jens Axboe771b53d02019-10-22 10:25:58 -0600571
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200572 raw_spin_lock_irq(&wqe->lock);
Jens Axboe021d1cd2019-11-14 08:00:41 -0700573 hlist_nulls_add_head_rcu(&worker->nulls_node, &wqe->free_list);
Jens Axboee61df662019-11-13 13:54:49 -0700574 list_add_tail_rcu(&worker->all_list, &wqe->all_list);
Jens Axboe771b53d02019-10-22 10:25:58 -0600575 worker->flags |= IO_WORKER_F_FREE;
Jens Axboec5def4a2019-11-07 11:41:16 -0700576 if (index == IO_WQ_ACCT_BOUND)
577 worker->flags |= IO_WORKER_F_BOUND;
578 if (!acct->nr_workers && (worker->flags & IO_WORKER_F_BOUND))
Jens Axboe771b53d02019-10-22 10:25:58 -0600579 worker->flags |= IO_WORKER_F_FIXED;
Jens Axboec5def4a2019-11-07 11:41:16 -0700580 acct->nr_workers++;
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200581 raw_spin_unlock_irq(&wqe->lock);
Jens Axboe771b53d02019-10-22 10:25:58 -0600582
Jens Axboe3bfe6102021-02-16 14:15:30 -0700583 io_wqe_worker(data);
584 do_exit(0);
585}
586
587static int task_thread_bound(void *data)
588{
589 return task_thread(data, IO_WQ_ACCT_BOUND);
590}
591
592static int task_thread_unbound(void *data)
593{
594 return task_thread(data, IO_WQ_ACCT_UNBOUND);
595}
596
Jens Axboe843bbfd2021-02-17 21:05:41 -0700597pid_t io_wq_fork_thread(int (*fn)(void *), void *arg)
Jens Axboe3bfe6102021-02-16 14:15:30 -0700598{
599 unsigned long flags = CLONE_FS|CLONE_FILES|CLONE_SIGHAND|CLONE_THREAD|
600 CLONE_IO|SIGCHLD;
601 struct kernel_clone_args args = {
602 .flags = ((lower_32_bits(flags) | CLONE_VM |
603 CLONE_UNTRACED) & ~CSIGNAL),
604 .exit_signal = (lower_32_bits(flags) & CSIGNAL),
605 .stack = (unsigned long)fn,
606 .stack_size = (unsigned long)arg,
607 };
608
609 return kernel_clone(&args);
610}
611
612static bool create_io_worker(struct io_wq *wq, struct io_wqe *wqe, int index)
613{
614 struct io_worker *worker;
615 pid_t pid;
616
Jens Axboe8b3e78b2021-02-23 15:34:06 -0700617 __set_current_state(TASK_RUNNING);
618
Jens Axboe3bfe6102021-02-16 14:15:30 -0700619 worker = kzalloc_node(sizeof(*worker), GFP_KERNEL, wqe->node);
620 if (!worker)
621 return false;
622
623 refcount_set(&worker->ref, 1);
624 worker->nulls_node.pprev = NULL;
625 worker->wqe = wqe;
626 spin_lock_init(&worker->lock);
Jens Axboeeb2de942021-02-23 19:59:06 -0700627 init_completion(&worker->ref_done);
Jens Axboe65d43022021-02-26 09:50:55 -0700628 init_completion(&worker->started);
Jens Axboe3bfe6102021-02-16 14:15:30 -0700629
Jens Axboefb3a1f62021-02-26 09:47:20 -0700630 atomic_inc(&wq->worker_refs);
Jens Axboe8b3e78b2021-02-23 15:34:06 -0700631
Jens Axboe3bfe6102021-02-16 14:15:30 -0700632 if (index == IO_WQ_ACCT_BOUND)
Jens Axboe843bbfd2021-02-17 21:05:41 -0700633 pid = io_wq_fork_thread(task_thread_bound, worker);
Jens Axboe3bfe6102021-02-16 14:15:30 -0700634 else
Jens Axboe843bbfd2021-02-17 21:05:41 -0700635 pid = io_wq_fork_thread(task_thread_unbound, worker);
Jens Axboe3bfe6102021-02-16 14:15:30 -0700636 if (pid < 0) {
Jens Axboefb3a1f62021-02-26 09:47:20 -0700637 if (atomic_dec_and_test(&wq->worker_refs))
638 complete(&wq->worker_done);
Jens Axboe3bfe6102021-02-16 14:15:30 -0700639 kfree(worker);
640 return false;
641 }
Jens Axboe65d43022021-02-26 09:50:55 -0700642 wait_for_completion(&worker->started);
Jens Axboeb60fda62019-11-19 08:37:07 -0700643 return true;
Jens Axboe771b53d02019-10-22 10:25:58 -0600644}
645
Jens Axboec5def4a2019-11-07 11:41:16 -0700646static inline bool io_wqe_need_worker(struct io_wqe *wqe, int index)
Jens Axboe771b53d02019-10-22 10:25:58 -0600647 __must_hold(wqe->lock)
648{
Jens Axboec5def4a2019-11-07 11:41:16 -0700649 struct io_wqe_acct *acct = &wqe->acct[index];
Jens Axboe771b53d02019-10-22 10:25:58 -0600650
Jens Axboe613eeb62021-02-26 09:52:02 -0700651 if (acct->nr_workers && test_bit(IO_WQ_BIT_EXIT, &wqe->wq->state))
652 return false;
Jens Axboec5def4a2019-11-07 11:41:16 -0700653 /* if we have available workers or no work, no need */
Jens Axboe021d1cd2019-11-14 08:00:41 -0700654 if (!hlist_nulls_empty(&wqe->free_list) || !io_wqe_run_queue(wqe))
Jens Axboec5def4a2019-11-07 11:41:16 -0700655 return false;
656 return acct->nr_workers < acct->max_workers;
Jens Axboe771b53d02019-10-22 10:25:58 -0600657}
658
Hillf Dantonc4068bf2020-09-26 21:26:55 +0800659/*
660 * Iterate the passed in list and call the specific function for each
661 * worker that isn't exiting
662 */
663static bool io_wq_for_each_worker(struct io_wqe *wqe,
664 bool (*func)(struct io_worker *, void *),
665 void *data)
666{
667 struct io_worker *worker;
668 bool ret = false;
669
670 list_for_each_entry_rcu(worker, &wqe->all_list, all_list) {
671 if (io_worker_get(worker)) {
672 /* no task if node is/was offline */
673 if (worker->task)
674 ret = func(worker, data);
675 io_worker_release(worker);
676 if (ret)
677 break;
678 }
679 }
680
681 return ret;
682}
683
684static bool io_wq_worker_wake(struct io_worker *worker, void *data)
685{
686 wake_up_process(worker->task);
687 return false;
688}
689
Jens Axboe8b3e78b2021-02-23 15:34:06 -0700690static void io_wq_check_workers(struct io_wq *wq)
691{
692 int node;
693
694 for_each_node(node) {
695 struct io_wqe *wqe = wq->wqes[node];
696 bool fork_worker[2] = { false, false };
697
698 if (!node_online(node))
699 continue;
700
701 raw_spin_lock_irq(&wqe->lock);
702 if (io_wqe_need_worker(wqe, IO_WQ_ACCT_BOUND))
703 fork_worker[IO_WQ_ACCT_BOUND] = true;
704 if (io_wqe_need_worker(wqe, IO_WQ_ACCT_UNBOUND))
705 fork_worker[IO_WQ_ACCT_UNBOUND] = true;
706 raw_spin_unlock_irq(&wqe->lock);
707 if (fork_worker[IO_WQ_ACCT_BOUND])
708 create_io_worker(wq, wqe, IO_WQ_ACCT_BOUND);
709 if (fork_worker[IO_WQ_ACCT_UNBOUND])
710 create_io_worker(wq, wqe, IO_WQ_ACCT_UNBOUND);
711 }
712}
713
Jens Axboe771b53d02019-10-22 10:25:58 -0600714/*
715 * Manager thread. Tasked with creating new workers, if we need them.
716 */
717static int io_wq_manager(void *data)
718{
719 struct io_wq *wq = data;
Jens Axboe3bfe6102021-02-16 14:15:30 -0700720 char buf[TASK_COMM_LEN];
Jens Axboefb3a1f62021-02-26 09:47:20 -0700721 int node;
Jens Axboeb60fda62019-11-19 08:37:07 -0700722
Jens Axboe3bfe6102021-02-16 14:15:30 -0700723 sprintf(buf, "iou-mgr-%d", wq->task_pid);
724 set_task_comm(current, buf);
725 current->flags |= PF_IO_WORKER;
Jens Axboeafcc4012021-02-26 13:48:19 -0700726 wq->manager = get_task_struct(current);
Jens Axboe3bfe6102021-02-16 14:15:30 -0700727
Jens Axboedbf99622021-02-26 09:53:17 -0700728 complete(&wq->started);
Jens Axboe771b53d02019-10-22 10:25:58 -0600729
Jens Axboe8b3e78b2021-02-23 15:34:06 -0700730 do {
Jens Axboe771b53d02019-10-22 10:25:58 -0600731 set_current_state(TASK_INTERRUPTIBLE);
Jens Axboe8b3e78b2021-02-23 15:34:06 -0700732 io_wq_check_workers(wq);
Jens Axboe771b53d02019-10-22 10:25:58 -0600733 schedule_timeout(HZ);
Jens Axboe3bfe6102021-02-16 14:15:30 -0700734 if (fatal_signal_pending(current))
735 set_bit(IO_WQ_BIT_EXIT, &wq->state);
Jens Axboe8b3e78b2021-02-23 15:34:06 -0700736 } while (!test_bit(IO_WQ_BIT_EXIT, &wq->state));
737
738 io_wq_check_workers(wq);
Jens Axboefb3a1f62021-02-26 09:47:20 -0700739
740 rcu_read_lock();
741 for_each_node(node)
742 io_wq_for_each_worker(wq->wqes[node], io_wq_worker_wake, NULL);
743 rcu_read_unlock();
744
745 /* we might not ever have created any workers */
746 if (atomic_read(&wq->worker_refs))
747 wait_for_completion(&wq->worker_done);
Jens Axboed364d9e2021-02-26 09:56:32 -0700748 complete(&wq->exited);
Jens Axboe3bfe6102021-02-16 14:15:30 -0700749 do_exit(0);
Jens Axboe771b53d02019-10-22 10:25:58 -0600750}
751
Pavel Begunkove9fd9392020-03-04 16:14:12 +0300752static void io_run_cancel(struct io_wq_work *work, struct io_wqe *wqe)
Pavel Begunkovfc04c392020-03-01 19:18:19 +0300753{
Pavel Begunkove9fd9392020-03-04 16:14:12 +0300754 struct io_wq *wq = wqe->wq;
755
Pavel Begunkovfc04c392020-03-01 19:18:19 +0300756 do {
Pavel Begunkovfc04c392020-03-01 19:18:19 +0300757 work->flags |= IO_WQ_WORK_CANCEL;
Pavel Begunkov5280f7e2021-02-04 13:52:08 +0000758 wq->do_work(work);
759 work = wq->free_work(work);
Pavel Begunkovfc04c392020-03-01 19:18:19 +0300760 } while (work);
761}
762
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300763static void io_wqe_insert_work(struct io_wqe *wqe, struct io_wq_work *work)
764{
765 unsigned int hash;
766 struct io_wq_work *tail;
767
768 if (!io_wq_is_hashed(work)) {
769append:
770 wq_list_add_tail(&work->list, &wqe->work_list);
771 return;
772 }
773
774 hash = io_get_work_hash(work);
775 tail = wqe->hash_tail[hash];
776 wqe->hash_tail[hash] = work;
777 if (!tail)
778 goto append;
779
780 wq_list_add_after(&work->list, &tail->list, &wqe->work_list);
781}
782
Jens Axboe4fb6ac32021-02-25 10:17:09 -0700783static int io_wq_fork_manager(struct io_wq *wq)
784{
785 int ret;
786
787 if (wq->manager)
788 return 0;
789
Jens Axboefb3a1f62021-02-26 09:47:20 -0700790 reinit_completion(&wq->worker_done);
Jens Axboe4fb6ac32021-02-25 10:17:09 -0700791 current->flags |= PF_IO_WORKER;
792 ret = io_wq_fork_thread(io_wq_manager, wq);
793 current->flags &= ~PF_IO_WORKER;
794 if (ret >= 0) {
Jens Axboedbf99622021-02-26 09:53:17 -0700795 wait_for_completion(&wq->started);
Jens Axboe4fb6ac32021-02-25 10:17:09 -0700796 return 0;
797 }
798
Jens Axboe4fb6ac32021-02-25 10:17:09 -0700799 return ret;
800}
801
Jens Axboe771b53d02019-10-22 10:25:58 -0600802static void io_wqe_enqueue(struct io_wqe *wqe, struct io_wq_work *work)
803{
Jens Axboec5def4a2019-11-07 11:41:16 -0700804 struct io_wqe_acct *acct = io_work_get_acct(wqe, work);
Jens Axboe895e2ca2019-12-17 08:46:33 -0700805 int work_flags;
Jens Axboe771b53d02019-10-22 10:25:58 -0600806 unsigned long flags;
807
Jens Axboe4fb6ac32021-02-25 10:17:09 -0700808 /* Can only happen if manager creation fails after exec */
809 if (unlikely(io_wq_fork_manager(wqe->wq))) {
810 work->flags |= IO_WQ_WORK_CANCEL;
811 wqe->wq->do_work(work);
812 return;
813 }
814
Jens Axboe895e2ca2019-12-17 08:46:33 -0700815 work_flags = work->flags;
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200816 raw_spin_lock_irqsave(&wqe->lock, flags);
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300817 io_wqe_insert_work(wqe, work);
Jens Axboe771b53d02019-10-22 10:25:58 -0600818 wqe->flags &= ~IO_WQE_FLAG_STALLED;
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200819 raw_spin_unlock_irqrestore(&wqe->lock, flags);
Jens Axboe771b53d02019-10-22 10:25:58 -0600820
Jens Axboe895e2ca2019-12-17 08:46:33 -0700821 if ((work_flags & IO_WQ_WORK_CONCURRENT) ||
822 !atomic_read(&acct->nr_running))
Jens Axboec5def4a2019-11-07 11:41:16 -0700823 io_wqe_wake_worker(wqe, acct);
Jens Axboe771b53d02019-10-22 10:25:58 -0600824}
825
826void io_wq_enqueue(struct io_wq *wq, struct io_wq_work *work)
827{
828 struct io_wqe *wqe = wq->wqes[numa_node_id()];
829
830 io_wqe_enqueue(wqe, work);
831}
832
833/*
Pavel Begunkov8766dd52020-03-14 00:31:04 +0300834 * Work items that hash to the same value will not be done in parallel.
835 * Used to limit concurrent writes, generally hashed by inode.
Jens Axboe771b53d02019-10-22 10:25:58 -0600836 */
Pavel Begunkov8766dd52020-03-14 00:31:04 +0300837void io_wq_hash_work(struct io_wq_work *work, void *val)
Jens Axboe771b53d02019-10-22 10:25:58 -0600838{
Pavel Begunkov8766dd52020-03-14 00:31:04 +0300839 unsigned int bit;
Jens Axboe771b53d02019-10-22 10:25:58 -0600840
841 bit = hash_ptr(val, IO_WQ_HASH_ORDER);
842 work->flags |= (IO_WQ_WORK_HASHED | (bit << IO_WQ_HASH_SHIFT));
Jens Axboe771b53d02019-10-22 10:25:58 -0600843}
844
Jens Axboe62755e32019-10-28 21:49:21 -0600845struct io_cb_cancel_data {
Pavel Begunkov2293b412020-03-07 01:15:39 +0300846 work_cancel_fn *fn;
847 void *data;
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300848 int nr_running;
849 int nr_pending;
850 bool cancel_all;
Jens Axboe62755e32019-10-28 21:49:21 -0600851};
852
Pavel Begunkov2293b412020-03-07 01:15:39 +0300853static bool io_wq_worker_cancel(struct io_worker *worker, void *data)
Jens Axboe62755e32019-10-28 21:49:21 -0600854{
Pavel Begunkov2293b412020-03-07 01:15:39 +0300855 struct io_cb_cancel_data *match = data;
Jens Axboe6f726532019-11-05 13:51:51 -0700856 unsigned long flags;
Jens Axboe62755e32019-10-28 21:49:21 -0600857
858 /*
859 * Hold the lock to avoid ->cur_work going out of scope, caller
Jens Axboe36c2f922019-11-13 09:43:34 -0700860 * may dereference the passed in work.
Jens Axboe62755e32019-10-28 21:49:21 -0600861 */
Jens Axboe36c2f922019-11-13 09:43:34 -0700862 spin_lock_irqsave(&worker->lock, flags);
Jens Axboe62755e32019-10-28 21:49:21 -0600863 if (worker->cur_work &&
Pavel Begunkov2293b412020-03-07 01:15:39 +0300864 match->fn(worker->cur_work, match->data)) {
Jens Axboe3bfe6102021-02-16 14:15:30 -0700865 set_notify_signal(worker->task);
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300866 match->nr_running++;
Jens Axboe771b53d02019-10-22 10:25:58 -0600867 }
Jens Axboe36c2f922019-11-13 09:43:34 -0700868 spin_unlock_irqrestore(&worker->lock, flags);
Jens Axboe771b53d02019-10-22 10:25:58 -0600869
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300870 return match->nr_running && !match->cancel_all;
Jens Axboe771b53d02019-10-22 10:25:58 -0600871}
872
Pavel Begunkov204361a2020-08-23 20:33:10 +0300873static inline void io_wqe_remove_pending(struct io_wqe *wqe,
874 struct io_wq_work *work,
875 struct io_wq_work_node *prev)
876{
877 unsigned int hash = io_get_work_hash(work);
878 struct io_wq_work *prev_work = NULL;
879
880 if (io_wq_is_hashed(work) && work == wqe->hash_tail[hash]) {
881 if (prev)
882 prev_work = container_of(prev, struct io_wq_work, list);
883 if (prev_work && io_get_work_hash(prev_work) == hash)
884 wqe->hash_tail[hash] = prev_work;
885 else
886 wqe->hash_tail[hash] = NULL;
887 }
888 wq_list_del(&wqe->work_list, &work->list, prev);
889}
890
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300891static void io_wqe_cancel_pending_work(struct io_wqe *wqe,
Pavel Begunkovf4c26652020-06-15 10:24:02 +0300892 struct io_cb_cancel_data *match)
Jens Axboe771b53d02019-10-22 10:25:58 -0600893{
Jens Axboe6206f0e2019-11-26 11:59:32 -0700894 struct io_wq_work_node *node, *prev;
Jens Axboe771b53d02019-10-22 10:25:58 -0600895 struct io_wq_work *work;
Jens Axboe6f726532019-11-05 13:51:51 -0700896 unsigned long flags;
Jens Axboe771b53d02019-10-22 10:25:58 -0600897
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300898retry:
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200899 raw_spin_lock_irqsave(&wqe->lock, flags);
Jens Axboe6206f0e2019-11-26 11:59:32 -0700900 wq_list_for_each(node, prev, &wqe->work_list) {
901 work = container_of(node, struct io_wq_work, list);
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300902 if (!match->fn(work, match->data))
903 continue;
Pavel Begunkov204361a2020-08-23 20:33:10 +0300904 io_wqe_remove_pending(wqe, work, prev);
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200905 raw_spin_unlock_irqrestore(&wqe->lock, flags);
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300906 io_run_cancel(work, wqe);
907 match->nr_pending++;
908 if (!match->cancel_all)
909 return;
910
911 /* not safe to continue after unlock */
912 goto retry;
Jens Axboe771b53d02019-10-22 10:25:58 -0600913 }
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200914 raw_spin_unlock_irqrestore(&wqe->lock, flags);
Pavel Begunkovf4c26652020-06-15 10:24:02 +0300915}
Jens Axboe771b53d02019-10-22 10:25:58 -0600916
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300917static void io_wqe_cancel_running_work(struct io_wqe *wqe,
Pavel Begunkovf4c26652020-06-15 10:24:02 +0300918 struct io_cb_cancel_data *match)
919{
Jens Axboe771b53d02019-10-22 10:25:58 -0600920 rcu_read_lock();
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300921 io_wq_for_each_worker(wqe, io_wq_worker_cancel, match);
Jens Axboe771b53d02019-10-22 10:25:58 -0600922 rcu_read_unlock();
Jens Axboe771b53d02019-10-22 10:25:58 -0600923}
924
Pavel Begunkov2293b412020-03-07 01:15:39 +0300925enum io_wq_cancel io_wq_cancel_cb(struct io_wq *wq, work_cancel_fn *cancel,
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300926 void *data, bool cancel_all)
Pavel Begunkov2293b412020-03-07 01:15:39 +0300927{
928 struct io_cb_cancel_data match = {
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300929 .fn = cancel,
930 .data = data,
931 .cancel_all = cancel_all,
Pavel Begunkov2293b412020-03-07 01:15:39 +0300932 };
Pavel Begunkov2293b412020-03-07 01:15:39 +0300933 int node;
934
Pavel Begunkovf4c26652020-06-15 10:24:02 +0300935 /*
936 * First check pending list, if we're lucky we can just remove it
937 * from there. CANCEL_OK means that the work is returned as-new,
938 * no completion will be posted for it.
939 */
Pavel Begunkov2293b412020-03-07 01:15:39 +0300940 for_each_node(node) {
941 struct io_wqe *wqe = wq->wqes[node];
942
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300943 io_wqe_cancel_pending_work(wqe, &match);
944 if (match.nr_pending && !match.cancel_all)
Pavel Begunkovf4c26652020-06-15 10:24:02 +0300945 return IO_WQ_CANCEL_OK;
Pavel Begunkov2293b412020-03-07 01:15:39 +0300946 }
947
Pavel Begunkovf4c26652020-06-15 10:24:02 +0300948 /*
949 * Now check if a free (going busy) or busy worker has the work
950 * currently running. If we find it there, we'll return CANCEL_RUNNING
951 * as an indication that we attempt to signal cancellation. The
952 * completion will run normally in this case.
953 */
954 for_each_node(node) {
955 struct io_wqe *wqe = wq->wqes[node];
956
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300957 io_wqe_cancel_running_work(wqe, &match);
958 if (match.nr_running && !match.cancel_all)
Pavel Begunkovf4c26652020-06-15 10:24:02 +0300959 return IO_WQ_CANCEL_RUNNING;
960 }
961
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300962 if (match.nr_running)
963 return IO_WQ_CANCEL_RUNNING;
964 if (match.nr_pending)
965 return IO_WQ_CANCEL_OK;
Pavel Begunkovf4c26652020-06-15 10:24:02 +0300966 return IO_WQ_CANCEL_NOTFOUND;
Pavel Begunkov2293b412020-03-07 01:15:39 +0300967}
968
Jens Axboee9418942021-02-19 12:33:30 -0700969static int io_wqe_hash_wake(struct wait_queue_entry *wait, unsigned mode,
970 int sync, void *key)
971{
972 struct io_wqe *wqe = container_of(wait, struct io_wqe, wait);
973 int ret;
974
975 list_del_init(&wait->entry);
976
977 rcu_read_lock();
978 ret = io_wqe_activate_free_worker(wqe);
979 rcu_read_unlock();
980
981 if (!ret)
982 wake_up_process(wqe->wq->manager);
983
984 return 1;
985}
986
Jens Axboe576a3472019-11-25 08:49:20 -0700987struct io_wq *io_wq_create(unsigned bounded, struct io_wq_data *data)
Jens Axboe771b53d02019-10-22 10:25:58 -0600988{
Jann Horn3fc50ab2019-11-26 19:10:20 +0100989 int ret = -ENOMEM, node;
Jens Axboe771b53d02019-10-22 10:25:58 -0600990 struct io_wq *wq;
991
Pavel Begunkovf5fa38c2020-06-08 21:08:20 +0300992 if (WARN_ON_ONCE(!data->free_work || !data->do_work))
Pavel Begunkove9fd9392020-03-04 16:14:12 +0300993 return ERR_PTR(-EINVAL);
994
Jann Hornad6e0052019-11-26 17:39:45 +0100995 wq = kzalloc(sizeof(*wq), GFP_KERNEL);
Jens Axboe771b53d02019-10-22 10:25:58 -0600996 if (!wq)
997 return ERR_PTR(-ENOMEM);
998
Jann Horn3fc50ab2019-11-26 19:10:20 +0100999 wq->wqes = kcalloc(nr_node_ids, sizeof(struct io_wqe *), GFP_KERNEL);
Jens Axboe43c01fb2020-10-22 09:02:50 -06001000 if (!wq->wqes)
1001 goto err_wq;
1002
1003 ret = cpuhp_state_add_instance_nocalls(io_wq_online, &wq->cpuhp_node);
1004 if (ret)
1005 goto err_wqes;
Jens Axboe771b53d02019-10-22 10:25:58 -06001006
Jens Axboee9418942021-02-19 12:33:30 -07001007 refcount_inc(&data->hash->refs);
1008 wq->hash = data->hash;
Pavel Begunkove9fd9392020-03-04 16:14:12 +03001009 wq->free_work = data->free_work;
Pavel Begunkovf5fa38c2020-06-08 21:08:20 +03001010 wq->do_work = data->do_work;
Jens Axboe7d723062019-11-12 22:31:31 -07001011
Jens Axboe43c01fb2020-10-22 09:02:50 -06001012 ret = -ENOMEM;
Jann Horn3fc50ab2019-11-26 19:10:20 +01001013 for_each_node(node) {
Jens Axboe771b53d02019-10-22 10:25:58 -06001014 struct io_wqe *wqe;
Jens Axboe75634392020-02-11 06:30:06 -07001015 int alloc_node = node;
Jens Axboe771b53d02019-10-22 10:25:58 -06001016
Jens Axboe75634392020-02-11 06:30:06 -07001017 if (!node_online(alloc_node))
1018 alloc_node = NUMA_NO_NODE;
1019 wqe = kzalloc_node(sizeof(struct io_wqe), GFP_KERNEL, alloc_node);
Jens Axboe771b53d02019-10-22 10:25:58 -06001020 if (!wqe)
Jann Horn3fc50ab2019-11-26 19:10:20 +01001021 goto err;
1022 wq->wqes[node] = wqe;
Jens Axboe75634392020-02-11 06:30:06 -07001023 wqe->node = alloc_node;
Jens Axboec5def4a2019-11-07 11:41:16 -07001024 wqe->acct[IO_WQ_ACCT_BOUND].max_workers = bounded;
1025 atomic_set(&wqe->acct[IO_WQ_ACCT_BOUND].nr_running, 0);
Jens Axboe728f13e2021-02-21 16:02:53 -07001026 wqe->acct[IO_WQ_ACCT_UNBOUND].max_workers =
Jens Axboec5def4a2019-11-07 11:41:16 -07001027 task_rlimit(current, RLIMIT_NPROC);
Jens Axboec5def4a2019-11-07 11:41:16 -07001028 atomic_set(&wqe->acct[IO_WQ_ACCT_UNBOUND].nr_running, 0);
Jens Axboee9418942021-02-19 12:33:30 -07001029 wqe->wait.func = io_wqe_hash_wake;
1030 INIT_LIST_HEAD(&wqe->wait.entry);
Jens Axboe771b53d02019-10-22 10:25:58 -06001031 wqe->wq = wq;
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +02001032 raw_spin_lock_init(&wqe->lock);
Jens Axboe6206f0e2019-11-26 11:59:32 -07001033 INIT_WQ_LIST(&wqe->work_list);
Jens Axboe021d1cd2019-11-14 08:00:41 -07001034 INIT_HLIST_NULLS_HEAD(&wqe->free_list, 0);
Jens Axboee61df662019-11-13 13:54:49 -07001035 INIT_LIST_HEAD(&wqe->all_list);
Jens Axboe771b53d02019-10-22 10:25:58 -06001036 }
1037
Jens Axboe3bfe6102021-02-16 14:15:30 -07001038 wq->task_pid = current->pid;
Jens Axboedbf99622021-02-26 09:53:17 -07001039 init_completion(&wq->started);
Jens Axboed364d9e2021-02-26 09:56:32 -07001040 init_completion(&wq->exited);
Jens Axboe3bfe6102021-02-16 14:15:30 -07001041 refcount_set(&wq->refs, 1);
Jens Axboe771b53d02019-10-22 10:25:58 -06001042
Jens Axboefb3a1f62021-02-26 09:47:20 -07001043 init_completion(&wq->worker_done);
1044 atomic_set(&wq->worker_refs, 0);
1045
Jens Axboe4fb6ac32021-02-25 10:17:09 -07001046 ret = io_wq_fork_manager(wq);
1047 if (!ret)
Jens Axboe771b53d02019-10-22 10:25:58 -06001048 return wq;
Jens Axboe771b53d02019-10-22 10:25:58 -06001049
Jens Axboeb60fda62019-11-19 08:37:07 -07001050err:
Jens Axboedc7bbc92021-03-01 09:09:56 -07001051 io_wq_put_hash(data->hash);
Jens Axboe43c01fb2020-10-22 09:02:50 -06001052 cpuhp_state_remove_instance_nocalls(io_wq_online, &wq->cpuhp_node);
Jann Horn3fc50ab2019-11-26 19:10:20 +01001053 for_each_node(node)
1054 kfree(wq->wqes[node]);
Jens Axboe43c01fb2020-10-22 09:02:50 -06001055err_wqes:
Jens Axboeb60fda62019-11-19 08:37:07 -07001056 kfree(wq->wqes);
Jens Axboe43c01fb2020-10-22 09:02:50 -06001057err_wq:
Jens Axboeb60fda62019-11-19 08:37:07 -07001058 kfree(wq);
Jens Axboe771b53d02019-10-22 10:25:58 -06001059 return ERR_PTR(ret);
1060}
1061
Jens Axboeafcc4012021-02-26 13:48:19 -07001062static void io_wq_destroy_manager(struct io_wq *wq)
1063{
1064 if (wq->manager) {
1065 wake_up_process(wq->manager);
1066 wait_for_completion(&wq->exited);
1067 put_task_struct(wq->manager);
1068 wq->manager = NULL;
1069 }
1070}
1071
Jens Axboe4fb6ac32021-02-25 10:17:09 -07001072static void io_wq_destroy(struct io_wq *wq)
Jens Axboe771b53d02019-10-22 10:25:58 -06001073{
Jann Horn3fc50ab2019-11-26 19:10:20 +01001074 int node;
Jens Axboe771b53d02019-10-22 10:25:58 -06001075
Jens Axboe43c01fb2020-10-22 09:02:50 -06001076 cpuhp_state_remove_instance_nocalls(io_wq_online, &wq->cpuhp_node);
1077
Jens Axboeb60fda62019-11-19 08:37:07 -07001078 set_bit(IO_WQ_BIT_EXIT, &wq->state);
Jens Axboeafcc4012021-02-26 13:48:19 -07001079 io_wq_destroy_manager(wq);
Jens Axboe771b53d02019-10-22 10:25:58 -06001080
Jens Axboee9418942021-02-19 12:33:30 -07001081 spin_lock_irq(&wq->hash->wait.lock);
1082 for_each_node(node) {
1083 struct io_wqe *wqe = wq->wqes[node];
1084
1085 list_del_init(&wqe->wait.entry);
1086 kfree(wqe);
1087 }
1088 spin_unlock_irq(&wq->hash->wait.lock);
1089 io_wq_put_hash(wq->hash);
Jens Axboe771b53d02019-10-22 10:25:58 -06001090 kfree(wq->wqes);
1091 kfree(wq);
Jens Axboe4fb6ac32021-02-25 10:17:09 -07001092}
1093
1094void io_wq_put(struct io_wq *wq)
1095{
1096 if (refcount_dec_and_test(&wq->refs))
1097 io_wq_destroy(wq);
Jens Axboe771b53d02019-10-22 10:25:58 -06001098}
Jens Axboe848f7e12020-01-23 15:33:32 -07001099
Jens Axboeafcc4012021-02-26 13:48:19 -07001100void io_wq_put_and_exit(struct io_wq *wq)
1101{
1102 set_bit(IO_WQ_BIT_EXIT, &wq->state);
1103 io_wq_destroy_manager(wq);
1104 io_wq_put(wq);
1105}
1106
Jens Axboe43c01fb2020-10-22 09:02:50 -06001107static bool io_wq_worker_affinity(struct io_worker *worker, void *data)
1108{
1109 struct task_struct *task = worker->task;
1110 struct rq_flags rf;
1111 struct rq *rq;
1112
1113 rq = task_rq_lock(task, &rf);
1114 do_set_cpus_allowed(task, cpumask_of_node(worker->wqe->node));
1115 task->flags |= PF_NO_SETAFFINITY;
1116 task_rq_unlock(rq, task, &rf);
1117 return false;
1118}
1119
1120static int io_wq_cpu_online(unsigned int cpu, struct hlist_node *node)
1121{
1122 struct io_wq *wq = hlist_entry_safe(node, struct io_wq, cpuhp_node);
1123 int i;
1124
1125 rcu_read_lock();
1126 for_each_node(i)
1127 io_wq_for_each_worker(wq->wqes[i], io_wq_worker_affinity, NULL);
1128 rcu_read_unlock();
1129 return 0;
1130}
1131
1132static __init int io_wq_init(void)
1133{
1134 int ret;
1135
1136 ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "io-wq/online",
1137 io_wq_cpu_online, NULL);
1138 if (ret < 0)
1139 return ret;
1140 io_wq_online = ret;
1141 return 0;
1142}
1143subsys_initcall(io_wq_init);