]>
Commit | Line | Data |
---|---|---|
771b53d0 JA |
1 | // 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> | |
771b53d0 JA |
12 | #include <linux/percpu.h> |
13 | #include <linux/slab.h> | |
771b53d0 | 14 | #include <linux/rculist_nulls.h> |
43c01fbe | 15 | #include <linux/cpu.h> |
355f841a | 16 | #include <linux/task_work.h> |
5bd2182d | 17 | #include <linux/audit.h> |
da64d6db | 18 | #include <linux/mmu_context.h> |
dd47c104 | 19 | #include <uapi/linux/io_uring.h> |
771b53d0 JA |
20 | |
21 | #include "io-wq.h" | |
a6b21fbb | 22 | #include "slist.h" |
024f15e0 | 23 | #include "io_uring.h" |
771b53d0 JA |
24 | |
25 | #define WORKER_IDLE_TIMEOUT (5 * HZ) | |
26 | ||
27 | enum { | |
28 | IO_WORKER_F_UP = 1, /* up and active */ | |
29 | IO_WORKER_F_RUNNING = 2, /* account as running */ | |
30 | IO_WORKER_F_FREE = 4, /* worker on free list */ | |
05c5f4ee | 31 | IO_WORKER_F_BOUND = 8, /* is doing bounded work */ |
771b53d0 JA |
32 | }; |
33 | ||
34 | enum { | |
35 | IO_WQ_BIT_EXIT = 0, /* wq exiting */ | |
771b53d0 JA |
36 | }; |
37 | ||
38 | enum { | |
f95dc207 | 39 | IO_ACCT_STALLED_BIT = 0, /* stalled on hash */ |
771b53d0 JA |
40 | }; |
41 | ||
42 | /* | |
eb47943f | 43 | * One for each thread in a wq pool |
771b53d0 JA |
44 | */ |
45 | struct io_worker { | |
46 | refcount_t ref; | |
47 | unsigned flags; | |
48 | struct hlist_nulls_node nulls_node; | |
e61df66c | 49 | struct list_head all_list; |
771b53d0 | 50 | struct task_struct *task; |
eb47943f | 51 | struct io_wq *wq; |
36c2f922 | 52 | |
771b53d0 | 53 | struct io_wq_work *cur_work; |
361aee45 | 54 | struct io_wq_work *next_work; |
081b5820 | 55 | raw_spinlock_t lock; |
771b53d0 | 56 | |
eb2de941 JA |
57 | struct completion ref_done; |
58 | ||
d3e9f732 JA |
59 | unsigned long create_state; |
60 | struct callback_head create_work; | |
61 | int create_index; | |
62 | ||
3146cba9 JA |
63 | union { |
64 | struct rcu_head rcu; | |
65 | struct work_struct work; | |
66 | }; | |
771b53d0 JA |
67 | }; |
68 | ||
771b53d0 JA |
69 | #if BITS_PER_LONG == 64 |
70 | #define IO_WQ_HASH_ORDER 6 | |
71 | #else | |
72 | #define IO_WQ_HASH_ORDER 5 | |
73 | #endif | |
74 | ||
86f3cd1b PB |
75 | #define IO_WQ_NR_HASH_BUCKETS (1u << IO_WQ_HASH_ORDER) |
76 | ||
dfd63baf | 77 | struct io_wq_acct { |
c5def4ab JA |
78 | unsigned nr_workers; |
79 | unsigned max_workers; | |
685fe7fe | 80 | int index; |
c5def4ab | 81 | atomic_t nr_running; |
42abc95f | 82 | raw_spinlock_t lock; |
f95dc207 JA |
83 | struct io_wq_work_list work_list; |
84 | unsigned long flags; | |
c5def4ab JA |
85 | }; |
86 | ||
87 | enum { | |
88 | IO_WQ_ACCT_BOUND, | |
89 | IO_WQ_ACCT_UNBOUND, | |
f95dc207 | 90 | IO_WQ_ACCT_NR, |
c5def4ab JA |
91 | }; |
92 | ||
771b53d0 JA |
93 | /* |
94 | * Per io_wq state | |
95 | */ | |
96 | struct io_wq { | |
771b53d0 | 97 | unsigned long state; |
771b53d0 | 98 | |
e9fd9396 | 99 | free_work_fn *free_work; |
f5fa38c5 | 100 | io_wq_work_fn *do_work; |
7d723065 | 101 | |
e941894e JA |
102 | struct io_wq_hash *hash; |
103 | ||
fb3a1f6c JA |
104 | atomic_t worker_refs; |
105 | struct completion worker_done; | |
106 | ||
43c01fbe | 107 | struct hlist_node cpuhp_node; |
3bfe6106 | 108 | |
685fe7fe | 109 | struct task_struct *task; |
c7f405d6 | 110 | |
dfd63baf GKB |
111 | struct io_wq_acct acct[IO_WQ_ACCT_NR]; |
112 | ||
eb47943f GKB |
113 | /* lock protects access to elements below */ |
114 | raw_spinlock_t lock; | |
115 | ||
116 | struct hlist_nulls_head free_list; | |
117 | struct list_head all_list; | |
118 | ||
119 | struct wait_queue_entry wait; | |
120 | ||
121 | struct io_wq_work *hash_tail[IO_WQ_NR_HASH_BUCKETS]; | |
122 | ||
123 | cpumask_var_t cpu_mask; | |
771b53d0 JA |
124 | }; |
125 | ||
43c01fbe JA |
126 | static enum cpuhp_state io_wq_online; |
127 | ||
f0127254 JA |
128 | struct io_cb_cancel_data { |
129 | work_cancel_fn *fn; | |
130 | void *data; | |
131 | int nr_running; | |
132 | int nr_pending; | |
133 | bool cancel_all; | |
134 | }; | |
135 | ||
eb47943f GKB |
136 | static bool create_io_worker(struct io_wq *wq, int index); |
137 | static void io_wq_dec_running(struct io_worker *worker); | |
138 | static bool io_acct_cancel_pending_work(struct io_wq *wq, | |
dfd63baf | 139 | struct io_wq_acct *acct, |
3146cba9 | 140 | struct io_cb_cancel_data *match); |
1d5f5ea7 | 141 | static void create_worker_cb(struct callback_head *cb); |
71a85387 | 142 | static void io_wq_cancel_tw_create(struct io_wq *wq); |
f0127254 | 143 | |
771b53d0 JA |
144 | static bool io_worker_get(struct io_worker *worker) |
145 | { | |
146 | return refcount_inc_not_zero(&worker->ref); | |
147 | } | |
148 | ||
149 | static void io_worker_release(struct io_worker *worker) | |
150 | { | |
151 | if (refcount_dec_and_test(&worker->ref)) | |
eb2de941 | 152 | complete(&worker->ref_done); |
771b53d0 JA |
153 | } |
154 | ||
dfd63baf | 155 | static inline struct io_wq_acct *io_get_acct(struct io_wq *wq, bool bound) |
8418f22a | 156 | { |
dfd63baf | 157 | return &wq->acct[bound ? IO_WQ_ACCT_BOUND : IO_WQ_ACCT_UNBOUND]; |
8418f22a PB |
158 | } |
159 | ||
dfd63baf GKB |
160 | static inline struct io_wq_acct *io_work_get_acct(struct io_wq *wq, |
161 | struct io_wq_work *work) | |
c5def4ab | 162 | { |
dfd63baf | 163 | return io_get_acct(wq, !(work->flags & IO_WQ_WORK_UNBOUND)); |
c5def4ab JA |
164 | } |
165 | ||
dfd63baf | 166 | static inline struct io_wq_acct *io_wq_get_acct(struct io_worker *worker) |
c5def4ab | 167 | { |
eb47943f | 168 | return io_get_acct(worker->wq, worker->flags & IO_WORKER_F_BOUND); |
c5def4ab JA |
169 | } |
170 | ||
685fe7fe JA |
171 | static void io_worker_ref_put(struct io_wq *wq) |
172 | { | |
173 | if (atomic_dec_and_test(&wq->worker_refs)) | |
174 | complete(&wq->worker_done); | |
175 | } | |
176 | ||
1d5f5ea7 PB |
177 | static void io_worker_cancel_cb(struct io_worker *worker) |
178 | { | |
dfd63baf | 179 | struct io_wq_acct *acct = io_wq_get_acct(worker); |
eb47943f | 180 | struct io_wq *wq = worker->wq; |
1d5f5ea7 PB |
181 | |
182 | atomic_dec(&acct->nr_running); | |
eb47943f | 183 | raw_spin_lock(&wq->lock); |
1d5f5ea7 | 184 | acct->nr_workers--; |
eb47943f | 185 | raw_spin_unlock(&wq->lock); |
1d5f5ea7 PB |
186 | io_worker_ref_put(wq); |
187 | clear_bit_unlock(0, &worker->create_state); | |
188 | io_worker_release(worker); | |
189 | } | |
190 | ||
191 | static bool io_task_worker_match(struct callback_head *cb, void *data) | |
192 | { | |
193 | struct io_worker *worker; | |
194 | ||
195 | if (cb->func != create_worker_cb) | |
196 | return false; | |
197 | worker = container_of(cb, struct io_worker, create_work); | |
198 | return worker == data; | |
199 | } | |
200 | ||
771b53d0 JA |
201 | static void io_worker_exit(struct io_worker *worker) |
202 | { | |
eb47943f | 203 | struct io_wq *wq = worker->wq; |
771b53d0 | 204 | |
1d5f5ea7 PB |
205 | while (1) { |
206 | struct callback_head *cb = task_work_cancel_match(wq->task, | |
207 | io_task_worker_match, worker); | |
208 | ||
209 | if (!cb) | |
210 | break; | |
211 | io_worker_cancel_cb(worker); | |
212 | } | |
771b53d0 | 213 | |
c907e52c | 214 | io_worker_release(worker); |
eb2de941 | 215 | wait_for_completion(&worker->ref_done); |
771b53d0 | 216 | |
eb47943f | 217 | raw_spin_lock(&wq->lock); |
83d6c393 | 218 | if (worker->flags & IO_WORKER_F_FREE) |
bf1daa4b | 219 | hlist_nulls_del_rcu(&worker->nulls_node); |
e61df66c | 220 | list_del_rcu(&worker->all_list); |
eb47943f GKB |
221 | raw_spin_unlock(&wq->lock); |
222 | io_wq_dec_running(worker); | |
adeaa3f2 JA |
223 | /* |
224 | * this worker is a goner, clear ->worker_private to avoid any | |
225 | * inc/dec running calls that could happen as part of exit from | |
226 | * touching 'worker'. | |
227 | */ | |
228 | current->worker_private = NULL; | |
771b53d0 | 229 | |
364b05fd | 230 | kfree_rcu(worker, rcu); |
eb47943f | 231 | io_worker_ref_put(wq); |
46fe18b1 | 232 | do_exit(0); |
771b53d0 JA |
233 | } |
234 | ||
dfd63baf | 235 | static inline bool io_acct_run_queue(struct io_wq_acct *acct) |
c5def4ab | 236 | { |
e13fb1fe HX |
237 | bool ret = false; |
238 | ||
239 | raw_spin_lock(&acct->lock); | |
f95dc207 JA |
240 | if (!wq_list_empty(&acct->work_list) && |
241 | !test_bit(IO_ACCT_STALLED_BIT, &acct->flags)) | |
e13fb1fe HX |
242 | ret = true; |
243 | raw_spin_unlock(&acct->lock); | |
244 | ||
245 | return ret; | |
c5def4ab JA |
246 | } |
247 | ||
248 | /* | |
249 | * Check head of free list for an available worker. If one isn't available, | |
685fe7fe | 250 | * caller must create one. |
c5def4ab | 251 | */ |
eb47943f | 252 | static bool io_wq_activate_free_worker(struct io_wq *wq, |
dfd63baf | 253 | struct io_wq_acct *acct) |
c5def4ab JA |
254 | __must_hold(RCU) |
255 | { | |
256 | struct hlist_nulls_node *n; | |
257 | struct io_worker *worker; | |
258 | ||
83d6c393 JA |
259 | /* |
260 | * Iterate free_list and see if we can find an idle worker to | |
261 | * activate. If a given worker is on the free_list but in the process | |
262 | * of exiting, keep trying. | |
263 | */ | |
eb47943f | 264 | hlist_nulls_for_each_entry_rcu(worker, n, &wq->free_list, nulls_node) { |
83d6c393 JA |
265 | if (!io_worker_get(worker)) |
266 | continue; | |
dfd63baf | 267 | if (io_wq_get_acct(worker) != acct) { |
f95dc207 JA |
268 | io_worker_release(worker); |
269 | continue; | |
270 | } | |
83d6c393 JA |
271 | if (wake_up_process(worker->task)) { |
272 | io_worker_release(worker); | |
273 | return true; | |
274 | } | |
c5def4ab | 275 | io_worker_release(worker); |
c5def4ab JA |
276 | } |
277 | ||
278 | return false; | |
279 | } | |
280 | ||
281 | /* | |
282 | * We need a worker. If we find a free one, we're good. If not, and we're | |
685fe7fe | 283 | * below the max number of workers, create one. |
c5def4ab | 284 | */ |
eb47943f | 285 | static bool io_wq_create_worker(struct io_wq *wq, struct io_wq_acct *acct) |
c5def4ab | 286 | { |
c5def4ab JA |
287 | /* |
288 | * Most likely an attempt to queue unbounded work on an io_wq that | |
289 | * wasn't setup with any unbounded workers. | |
290 | */ | |
e6ab8991 PB |
291 | if (unlikely(!acct->max_workers)) |
292 | pr_warn_once("io-wq is not configured for unbound workers"); | |
c5def4ab | 293 | |
eb47943f | 294 | raw_spin_lock(&wq->lock); |
bc369921 | 295 | if (acct->nr_workers >= acct->max_workers) { |
eb47943f | 296 | raw_spin_unlock(&wq->lock); |
7a842fb5 | 297 | return true; |
94ffb0a2 | 298 | } |
7a842fb5 | 299 | acct->nr_workers++; |
eb47943f | 300 | raw_spin_unlock(&wq->lock); |
7a842fb5 | 301 | atomic_inc(&acct->nr_running); |
eb47943f GKB |
302 | atomic_inc(&wq->worker_refs); |
303 | return create_io_worker(wq, acct->index); | |
c5def4ab JA |
304 | } |
305 | ||
eb47943f | 306 | static void io_wq_inc_running(struct io_worker *worker) |
c5def4ab | 307 | { |
dfd63baf | 308 | struct io_wq_acct *acct = io_wq_get_acct(worker); |
c5def4ab JA |
309 | |
310 | atomic_inc(&acct->nr_running); | |
311 | } | |
312 | ||
685fe7fe JA |
313 | static void create_worker_cb(struct callback_head *cb) |
314 | { | |
d3e9f732 | 315 | struct io_worker *worker; |
685fe7fe | 316 | struct io_wq *wq; |
eb47943f | 317 | |
dfd63baf | 318 | struct io_wq_acct *acct; |
05c5f4ee | 319 | bool do_create = false; |
685fe7fe | 320 | |
d3e9f732 | 321 | worker = container_of(cb, struct io_worker, create_work); |
eb47943f | 322 | wq = worker->wq; |
dfd63baf | 323 | acct = &wq->acct[worker->create_index]; |
eb47943f GKB |
324 | raw_spin_lock(&wq->lock); |
325 | ||
49e7f0c7 | 326 | if (acct->nr_workers < acct->max_workers) { |
21698274 | 327 | acct->nr_workers++; |
49e7f0c7 HX |
328 | do_create = true; |
329 | } | |
eb47943f | 330 | raw_spin_unlock(&wq->lock); |
49e7f0c7 | 331 | if (do_create) { |
eb47943f | 332 | create_io_worker(wq, worker->create_index); |
49e7f0c7 HX |
333 | } else { |
334 | atomic_dec(&acct->nr_running); | |
335 | io_worker_ref_put(wq); | |
336 | } | |
d3e9f732 JA |
337 | clear_bit_unlock(0, &worker->create_state); |
338 | io_worker_release(worker); | |
685fe7fe JA |
339 | } |
340 | ||
3146cba9 | 341 | static bool io_queue_worker_create(struct io_worker *worker, |
dfd63baf | 342 | struct io_wq_acct *acct, |
3146cba9 | 343 | task_work_func_t func) |
685fe7fe | 344 | { |
eb47943f | 345 | struct io_wq *wq = worker->wq; |
685fe7fe JA |
346 | |
347 | /* raced with exit, just ignore create call */ | |
348 | if (test_bit(IO_WQ_BIT_EXIT, &wq->state)) | |
349 | goto fail; | |
d3e9f732 JA |
350 | if (!io_worker_get(worker)) |
351 | goto fail; | |
352 | /* | |
353 | * create_state manages ownership of create_work/index. We should | |
354 | * only need one entry per worker, as the worker going to sleep | |
355 | * will trigger the condition, and waking will clear it once it | |
356 | * runs the task_work. | |
357 | */ | |
358 | if (test_bit(0, &worker->create_state) || | |
359 | test_and_set_bit_lock(0, &worker->create_state)) | |
360 | goto fail_release; | |
685fe7fe | 361 | |
71a85387 | 362 | atomic_inc(&wq->worker_refs); |
3146cba9 | 363 | init_task_work(&worker->create_work, func); |
d3e9f732 | 364 | worker->create_index = acct->index; |
71a85387 JA |
365 | if (!task_work_add(wq->task, &worker->create_work, TWA_SIGNAL)) { |
366 | /* | |
367 | * EXIT may have been set after checking it above, check after | |
368 | * adding the task_work and remove any creation item if it is | |
369 | * now set. wq exit does that too, but we can have added this | |
370 | * work item after we canceled in io_wq_exit_workers(). | |
371 | */ | |
372 | if (test_bit(IO_WQ_BIT_EXIT, &wq->state)) | |
373 | io_wq_cancel_tw_create(wq); | |
374 | io_worker_ref_put(wq); | |
3146cba9 | 375 | return true; |
71a85387 JA |
376 | } |
377 | io_worker_ref_put(wq); | |
d3e9f732 JA |
378 | clear_bit_unlock(0, &worker->create_state); |
379 | fail_release: | |
380 | io_worker_release(worker); | |
685fe7fe JA |
381 | fail: |
382 | atomic_dec(&acct->nr_running); | |
383 | io_worker_ref_put(wq); | |
3146cba9 | 384 | return false; |
685fe7fe JA |
385 | } |
386 | ||
eb47943f | 387 | static void io_wq_dec_running(struct io_worker *worker) |
c5def4ab | 388 | { |
dfd63baf | 389 | struct io_wq_acct *acct = io_wq_get_acct(worker); |
eb47943f | 390 | struct io_wq *wq = worker->wq; |
c5def4ab | 391 | |
685fe7fe JA |
392 | if (!(worker->flags & IO_WORKER_F_UP)) |
393 | return; | |
394 | ||
42abc95f HX |
395 | if (!atomic_dec_and_test(&acct->nr_running)) |
396 | return; | |
e13fb1fe | 397 | if (!io_acct_run_queue(acct)) |
42abc95f | 398 | return; |
42abc95f | 399 | |
42abc95f | 400 | atomic_inc(&acct->nr_running); |
eb47943f | 401 | atomic_inc(&wq->worker_refs); |
42abc95f | 402 | io_queue_worker_create(worker, acct, create_worker_cb); |
c5def4ab JA |
403 | } |
404 | ||
771b53d0 JA |
405 | /* |
406 | * Worker will start processing some work. Move it to the busy list, if | |
407 | * it's currently on the freelist | |
408 | */ | |
eb47943f | 409 | static void __io_worker_busy(struct io_wq *wq, struct io_worker *worker) |
771b53d0 JA |
410 | { |
411 | if (worker->flags & IO_WORKER_F_FREE) { | |
412 | worker->flags &= ~IO_WORKER_F_FREE; | |
eb47943f | 413 | raw_spin_lock(&wq->lock); |
771b53d0 | 414 | hlist_nulls_del_init_rcu(&worker->nulls_node); |
eb47943f | 415 | raw_spin_unlock(&wq->lock); |
771b53d0 | 416 | } |
771b53d0 JA |
417 | } |
418 | ||
419 | /* | |
07d99096 | 420 | * No work, worker going to sleep. Move to freelist. |
771b53d0 | 421 | */ |
eb47943f GKB |
422 | static void __io_worker_idle(struct io_wq *wq, struct io_worker *worker) |
423 | __must_hold(wq->lock) | |
771b53d0 JA |
424 | { |
425 | if (!(worker->flags & IO_WORKER_F_FREE)) { | |
426 | worker->flags |= IO_WORKER_F_FREE; | |
eb47943f | 427 | hlist_nulls_add_head_rcu(&worker->nulls_node, &wq->free_list); |
771b53d0 | 428 | } |
771b53d0 JA |
429 | } |
430 | ||
60cf46ae PB |
431 | static inline unsigned int io_get_work_hash(struct io_wq_work *work) |
432 | { | |
433 | return work->flags >> IO_WQ_HASH_SHIFT; | |
434 | } | |
435 | ||
eb47943f | 436 | static bool io_wait_on_hash(struct io_wq *wq, unsigned int hash) |
e941894e | 437 | { |
d3e3c102 | 438 | bool ret = false; |
e941894e | 439 | |
08bdbd39 | 440 | spin_lock_irq(&wq->hash->wait.lock); |
eb47943f GKB |
441 | if (list_empty(&wq->wait.entry)) { |
442 | __add_wait_queue(&wq->hash->wait, &wq->wait); | |
e941894e JA |
443 | if (!test_bit(hash, &wq->hash->map)) { |
444 | __set_current_state(TASK_RUNNING); | |
eb47943f | 445 | list_del_init(&wq->wait.entry); |
d3e3c102 | 446 | ret = true; |
e941894e JA |
447 | } |
448 | } | |
08bdbd39 | 449 | spin_unlock_irq(&wq->hash->wait.lock); |
d3e3c102 | 450 | return ret; |
e941894e JA |
451 | } |
452 | ||
dfd63baf | 453 | static struct io_wq_work *io_get_next_work(struct io_wq_acct *acct, |
0242f642 | 454 | struct io_worker *worker) |
42abc95f | 455 | __must_hold(acct->lock) |
771b53d0 | 456 | { |
6206f0e1 | 457 | struct io_wq_work_node *node, *prev; |
86f3cd1b | 458 | struct io_wq_work *work, *tail; |
e941894e | 459 | unsigned int stall_hash = -1U; |
eb47943f | 460 | struct io_wq *wq = worker->wq; |
771b53d0 | 461 | |
f95dc207 | 462 | wq_list_for_each(node, prev, &acct->work_list) { |
e941894e JA |
463 | unsigned int hash; |
464 | ||
6206f0e1 JA |
465 | work = container_of(node, struct io_wq_work, list); |
466 | ||
771b53d0 | 467 | /* not hashed, can run anytime */ |
8766dd51 | 468 | if (!io_wq_is_hashed(work)) { |
f95dc207 | 469 | wq_list_del(&acct->work_list, node, prev); |
771b53d0 JA |
470 | return work; |
471 | } | |
472 | ||
60cf46ae | 473 | hash = io_get_work_hash(work); |
e941894e | 474 | /* all items with this hash lie in [work, tail] */ |
eb47943f | 475 | tail = wq->hash_tail[hash]; |
e941894e JA |
476 | |
477 | /* hashed, can run if not already running */ | |
eb47943f GKB |
478 | if (!test_and_set_bit(hash, &wq->hash->map)) { |
479 | wq->hash_tail[hash] = NULL; | |
f95dc207 | 480 | wq_list_cut(&acct->work_list, &tail->list, prev); |
771b53d0 JA |
481 | return work; |
482 | } | |
e941894e JA |
483 | if (stall_hash == -1U) |
484 | stall_hash = hash; | |
485 | /* fast forward to a next hash, for-each will fix up @prev */ | |
486 | node = &tail->list; | |
487 | } | |
488 | ||
489 | if (stall_hash != -1U) { | |
d3e3c102 JA |
490 | bool unstalled; |
491 | ||
0242f642 JA |
492 | /* |
493 | * Set this before dropping the lock to avoid racing with new | |
494 | * work being added and clearing the stalled bit. | |
495 | */ | |
f95dc207 | 496 | set_bit(IO_ACCT_STALLED_BIT, &acct->flags); |
42abc95f | 497 | raw_spin_unlock(&acct->lock); |
eb47943f | 498 | unstalled = io_wait_on_hash(wq, stall_hash); |
42abc95f | 499 | raw_spin_lock(&acct->lock); |
d3e3c102 JA |
500 | if (unstalled) { |
501 | clear_bit(IO_ACCT_STALLED_BIT, &acct->flags); | |
eb47943f GKB |
502 | if (wq_has_sleeper(&wq->hash->wait)) |
503 | wake_up(&wq->hash->wait); | |
d3e3c102 | 504 | } |
771b53d0 JA |
505 | } |
506 | ||
507 | return NULL; | |
508 | } | |
509 | ||
dc026a73 PB |
510 | static void io_assign_current_work(struct io_worker *worker, |
511 | struct io_wq_work *work) | |
512 | { | |
d78298e7 | 513 | if (work) { |
024f15e0 | 514 | io_run_task_work(); |
d78298e7 PB |
515 | cond_resched(); |
516 | } | |
dc026a73 | 517 | |
081b5820 | 518 | raw_spin_lock(&worker->lock); |
dc026a73 | 519 | worker->cur_work = work; |
361aee45 | 520 | worker->next_work = NULL; |
081b5820 | 521 | raw_spin_unlock(&worker->lock); |
dc026a73 PB |
522 | } |
523 | ||
771b53d0 | 524 | static void io_worker_handle_work(struct io_worker *worker) |
771b53d0 | 525 | { |
dfd63baf | 526 | struct io_wq_acct *acct = io_wq_get_acct(worker); |
eb47943f | 527 | struct io_wq *wq = worker->wq; |
c60eb049 | 528 | bool do_kill = test_bit(IO_WQ_BIT_EXIT, &wq->state); |
771b53d0 JA |
529 | |
530 | do { | |
86f3cd1b | 531 | struct io_wq_work *work; |
73031f76 | 532 | |
771b53d0 JA |
533 | /* |
534 | * If we got some work, mark us as busy. If we didn't, but | |
535 | * the list isn't empty, it means we stalled on hashed work. | |
536 | * Mark us stalled so we don't keep looking for work when we | |
537 | * can't make progress, any work completion or insertion will | |
538 | * clear the stalled flag. | |
539 | */ | |
e13fb1fe | 540 | raw_spin_lock(&acct->lock); |
f95dc207 | 541 | work = io_get_next_work(acct, worker); |
42abc95f | 542 | raw_spin_unlock(&acct->lock); |
361aee45 | 543 | if (work) { |
eb47943f | 544 | __io_worker_busy(wq, worker); |
771b53d0 | 545 | |
361aee45 JA |
546 | /* |
547 | * Make sure cancelation can find this, even before | |
548 | * it becomes the active work. That avoids a window | |
549 | * where the work has been removed from our general | |
550 | * work list, but isn't yet discoverable as the | |
551 | * current work item for this worker. | |
552 | */ | |
553 | raw_spin_lock(&worker->lock); | |
554 | worker->next_work = work; | |
555 | raw_spin_unlock(&worker->lock); | |
42abc95f | 556 | } else { |
771b53d0 | 557 | break; |
42abc95f | 558 | } |
58e39319 | 559 | io_assign_current_work(worker, work); |
e941894e | 560 | __set_current_state(TASK_RUNNING); |
36c2f922 | 561 | |
dc026a73 PB |
562 | /* handle a whole dependent link */ |
563 | do { | |
5280f7e5 | 564 | struct io_wq_work *next_hashed, *linked; |
b089ed39 | 565 | unsigned int hash = io_get_work_hash(work); |
dc026a73 | 566 | |
86f3cd1b | 567 | next_hashed = wq_next_work(work); |
c60eb049 PB |
568 | |
569 | if (unlikely(do_kill) && (work->flags & IO_WQ_WORK_UNBOUND)) | |
570 | work->flags |= IO_WQ_WORK_CANCEL; | |
5280f7e5 PB |
571 | wq->do_work(work); |
572 | io_assign_current_work(worker, NULL); | |
dc026a73 | 573 | |
5280f7e5 | 574 | linked = wq->free_work(work); |
86f3cd1b PB |
575 | work = next_hashed; |
576 | if (!work && linked && !io_wq_is_hashed(linked)) { | |
577 | work = linked; | |
578 | linked = NULL; | |
579 | } | |
580 | io_assign_current_work(worker, work); | |
86f3cd1b | 581 | if (linked) |
eb47943f | 582 | io_wq_enqueue(wq, linked); |
86f3cd1b PB |
583 | |
584 | if (hash != -1U && !next_hashed) { | |
d3e3c102 JA |
585 | /* serialize hash clear with wake_up() */ |
586 | spin_lock_irq(&wq->hash->wait.lock); | |
e941894e | 587 | clear_bit(hash, &wq->hash->map); |
f95dc207 | 588 | clear_bit(IO_ACCT_STALLED_BIT, &acct->flags); |
d3e3c102 | 589 | spin_unlock_irq(&wq->hash->wait.lock); |
e941894e JA |
590 | if (wq_has_sleeper(&wq->hash->wait)) |
591 | wake_up(&wq->hash->wait); | |
7d723065 | 592 | } |
58e39319 | 593 | } while (work); |
771b53d0 JA |
594 | } while (1); |
595 | } | |
596 | ||
eb47943f | 597 | static int io_wq_worker(void *data) |
771b53d0 JA |
598 | { |
599 | struct io_worker *worker = data; | |
dfd63baf | 600 | struct io_wq_acct *acct = io_wq_get_acct(worker); |
eb47943f | 601 | struct io_wq *wq = worker->wq; |
01e68ce0 | 602 | bool exit_mask = false, last_timeout = false; |
46fe18b1 | 603 | char buf[TASK_COMM_LEN]; |
771b53d0 | 604 | |
46fe18b1 | 605 | worker->flags |= (IO_WORKER_F_UP | IO_WORKER_F_RUNNING); |
46fe18b1 | 606 | |
685fe7fe | 607 | snprintf(buf, sizeof(buf), "iou-wrk-%d", wq->task->pid); |
46fe18b1 | 608 | set_task_comm(current, buf); |
771b53d0 JA |
609 | |
610 | while (!test_bit(IO_WQ_BIT_EXIT, &wq->state)) { | |
16efa4fc JA |
611 | long ret; |
612 | ||
506d95ff | 613 | set_current_state(TASK_INTERRUPTIBLE); |
e13fb1fe | 614 | while (io_acct_run_queue(acct)) |
771b53d0 | 615 | io_worker_handle_work(worker); |
e13fb1fe | 616 | |
eb47943f | 617 | raw_spin_lock(&wq->lock); |
01e68ce0 JA |
618 | /* |
619 | * Last sleep timed out. Exit if we're not the last worker, | |
620 | * or if someone modified our affinity. | |
621 | */ | |
622 | if (last_timeout && (exit_mask || acct->nr_workers > 1)) { | |
767a65e9 | 623 | acct->nr_workers--; |
eb47943f | 624 | raw_spin_unlock(&wq->lock); |
05c5f4ee JA |
625 | __set_current_state(TASK_RUNNING); |
626 | break; | |
627 | } | |
628 | last_timeout = false; | |
eb47943f GKB |
629 | __io_worker_idle(wq, worker); |
630 | raw_spin_unlock(&wq->lock); | |
024f15e0 | 631 | if (io_run_task_work()) |
00ddff43 | 632 | continue; |
16efa4fc | 633 | ret = schedule_timeout(WORKER_IDLE_TIMEOUT); |
dbe1bdbb JA |
634 | if (signal_pending(current)) { |
635 | struct ksignal ksig; | |
636 | ||
637 | if (!get_signal(&ksig)) | |
638 | continue; | |
78f8876c | 639 | break; |
dbe1bdbb | 640 | } |
01e68ce0 JA |
641 | if (!ret) { |
642 | last_timeout = true; | |
643 | exit_mask = !cpumask_test_cpu(raw_smp_processor_id(), | |
eb47943f | 644 | wq->cpu_mask); |
01e68ce0 | 645 | } |
771b53d0 JA |
646 | } |
647 | ||
e13fb1fe | 648 | if (test_bit(IO_WQ_BIT_EXIT, &wq->state)) |
e587227b | 649 | io_worker_handle_work(worker); |
771b53d0 JA |
650 | |
651 | io_worker_exit(worker); | |
652 | return 0; | |
653 | } | |
654 | ||
771b53d0 JA |
655 | /* |
656 | * Called when a worker is scheduled in. Mark us as currently running. | |
657 | */ | |
658 | void io_wq_worker_running(struct task_struct *tsk) | |
659 | { | |
e32cf5df | 660 | struct io_worker *worker = tsk->worker_private; |
771b53d0 | 661 | |
3bfe6106 JA |
662 | if (!worker) |
663 | return; | |
771b53d0 JA |
664 | if (!(worker->flags & IO_WORKER_F_UP)) |
665 | return; | |
666 | if (worker->flags & IO_WORKER_F_RUNNING) | |
667 | return; | |
668 | worker->flags |= IO_WORKER_F_RUNNING; | |
eb47943f | 669 | io_wq_inc_running(worker); |
771b53d0 JA |
670 | } |
671 | ||
672 | /* | |
673 | * Called when worker is going to sleep. If there are no workers currently | |
685fe7fe | 674 | * running and we have work pending, wake up a free one or create a new one. |
771b53d0 JA |
675 | */ |
676 | void io_wq_worker_sleeping(struct task_struct *tsk) | |
677 | { | |
e32cf5df | 678 | struct io_worker *worker = tsk->worker_private; |
771b53d0 | 679 | |
3bfe6106 JA |
680 | if (!worker) |
681 | return; | |
771b53d0 JA |
682 | if (!(worker->flags & IO_WORKER_F_UP)) |
683 | return; | |
684 | if (!(worker->flags & IO_WORKER_F_RUNNING)) | |
685 | return; | |
686 | ||
687 | worker->flags &= ~IO_WORKER_F_RUNNING; | |
eb47943f | 688 | io_wq_dec_running(worker); |
771b53d0 JA |
689 | } |
690 | ||
eb47943f | 691 | static void io_init_new_worker(struct io_wq *wq, struct io_worker *worker, |
3146cba9 JA |
692 | struct task_struct *tsk) |
693 | { | |
e32cf5df | 694 | tsk->worker_private = worker; |
3146cba9 | 695 | worker->task = tsk; |
eb47943f | 696 | set_cpus_allowed_ptr(tsk, wq->cpu_mask); |
3146cba9 | 697 | |
eb47943f GKB |
698 | raw_spin_lock(&wq->lock); |
699 | hlist_nulls_add_head_rcu(&worker->nulls_node, &wq->free_list); | |
700 | list_add_tail_rcu(&worker->all_list, &wq->all_list); | |
3146cba9 | 701 | worker->flags |= IO_WORKER_F_FREE; |
eb47943f | 702 | raw_spin_unlock(&wq->lock); |
3146cba9 JA |
703 | wake_up_new_task(tsk); |
704 | } | |
705 | ||
706 | static bool io_wq_work_match_all(struct io_wq_work *work, void *data) | |
707 | { | |
708 | return true; | |
709 | } | |
710 | ||
711 | static inline bool io_should_retry_thread(long err) | |
712 | { | |
a226abcd JA |
713 | /* |
714 | * Prevent perpetual task_work retry, if the task (or its group) is | |
715 | * exiting. | |
716 | */ | |
717 | if (fatal_signal_pending(current)) | |
718 | return false; | |
719 | ||
3146cba9 JA |
720 | switch (err) { |
721 | case -EAGAIN: | |
722 | case -ERESTARTSYS: | |
723 | case -ERESTARTNOINTR: | |
724 | case -ERESTARTNOHAND: | |
725 | return true; | |
726 | default: | |
727 | return false; | |
728 | } | |
729 | } | |
730 | ||
731 | static void create_worker_cont(struct callback_head *cb) | |
732 | { | |
733 | struct io_worker *worker; | |
734 | struct task_struct *tsk; | |
eb47943f | 735 | struct io_wq *wq; |
3146cba9 JA |
736 | |
737 | worker = container_of(cb, struct io_worker, create_work); | |
738 | clear_bit_unlock(0, &worker->create_state); | |
eb47943f GKB |
739 | wq = worker->wq; |
740 | tsk = create_io_thread(io_wq_worker, worker, NUMA_NO_NODE); | |
3146cba9 | 741 | if (!IS_ERR(tsk)) { |
eb47943f | 742 | io_init_new_worker(wq, worker, tsk); |
3146cba9 JA |
743 | io_worker_release(worker); |
744 | return; | |
745 | } else if (!io_should_retry_thread(PTR_ERR(tsk))) { | |
dfd63baf | 746 | struct io_wq_acct *acct = io_wq_get_acct(worker); |
3146cba9 JA |
747 | |
748 | atomic_dec(&acct->nr_running); | |
eb47943f | 749 | raw_spin_lock(&wq->lock); |
3146cba9 JA |
750 | acct->nr_workers--; |
751 | if (!acct->nr_workers) { | |
752 | struct io_cb_cancel_data match = { | |
753 | .fn = io_wq_work_match_all, | |
754 | .cancel_all = true, | |
755 | }; | |
756 | ||
eb47943f GKB |
757 | raw_spin_unlock(&wq->lock); |
758 | while (io_acct_cancel_pending_work(wq, acct, &match)) | |
42abc95f HX |
759 | ; |
760 | } else { | |
eb47943f | 761 | raw_spin_unlock(&wq->lock); |
3146cba9 | 762 | } |
eb47943f | 763 | io_worker_ref_put(wq); |
66e70be7 | 764 | kfree(worker); |
3146cba9 JA |
765 | return; |
766 | } | |
767 | ||
768 | /* re-create attempts grab a new worker ref, drop the existing one */ | |
769 | io_worker_release(worker); | |
770 | schedule_work(&worker->work); | |
771 | } | |
772 | ||
773 | static void io_workqueue_create(struct work_struct *work) | |
774 | { | |
775 | struct io_worker *worker = container_of(work, struct io_worker, work); | |
dfd63baf | 776 | struct io_wq_acct *acct = io_wq_get_acct(worker); |
3146cba9 | 777 | |
71e1cef2 | 778 | if (!io_queue_worker_create(worker, acct, create_worker_cont)) |
66e70be7 | 779 | kfree(worker); |
3146cba9 JA |
780 | } |
781 | ||
eb47943f | 782 | static bool create_io_worker(struct io_wq *wq, int index) |
3bfe6106 | 783 | { |
dfd63baf | 784 | struct io_wq_acct *acct = &wq->acct[index]; |
3bfe6106 | 785 | struct io_worker *worker; |
46fe18b1 | 786 | struct task_struct *tsk; |
3bfe6106 | 787 | |
8b3e78b5 JA |
788 | __set_current_state(TASK_RUNNING); |
789 | ||
da64d6db | 790 | worker = kzalloc(sizeof(*worker), GFP_KERNEL); |
3146cba9 | 791 | if (!worker) { |
685fe7fe JA |
792 | fail: |
793 | atomic_dec(&acct->nr_running); | |
eb47943f | 794 | raw_spin_lock(&wq->lock); |
3d4e4fac | 795 | acct->nr_workers--; |
eb47943f | 796 | raw_spin_unlock(&wq->lock); |
685fe7fe | 797 | io_worker_ref_put(wq); |
3146cba9 | 798 | return false; |
3bfe6106 | 799 | } |
46fe18b1 | 800 | |
3146cba9 | 801 | refcount_set(&worker->ref, 1); |
eb47943f | 802 | worker->wq = wq; |
081b5820 | 803 | raw_spin_lock_init(&worker->lock); |
3146cba9 | 804 | init_completion(&worker->ref_done); |
46fe18b1 | 805 | |
46fe18b1 JA |
806 | if (index == IO_WQ_ACCT_BOUND) |
807 | worker->flags |= IO_WORKER_F_BOUND; | |
3146cba9 | 808 | |
eb47943f | 809 | tsk = create_io_thread(io_wq_worker, worker, NUMA_NO_NODE); |
3146cba9 | 810 | if (!IS_ERR(tsk)) { |
eb47943f | 811 | io_init_new_worker(wq, worker, tsk); |
3146cba9 | 812 | } else if (!io_should_retry_thread(PTR_ERR(tsk))) { |
66e70be7 | 813 | kfree(worker); |
3146cba9 JA |
814 | goto fail; |
815 | } else { | |
816 | INIT_WORK(&worker->work, io_workqueue_create); | |
817 | schedule_work(&worker->work); | |
818 | } | |
819 | ||
820 | return true; | |
771b53d0 JA |
821 | } |
822 | ||
c4068bf8 HD |
823 | /* |
824 | * Iterate the passed in list and call the specific function for each | |
825 | * worker that isn't exiting | |
826 | */ | |
eb47943f | 827 | static bool io_wq_for_each_worker(struct io_wq *wq, |
c4068bf8 HD |
828 | bool (*func)(struct io_worker *, void *), |
829 | void *data) | |
830 | { | |
831 | struct io_worker *worker; | |
832 | bool ret = false; | |
833 | ||
eb47943f | 834 | list_for_each_entry_rcu(worker, &wq->all_list, all_list) { |
c4068bf8 HD |
835 | if (io_worker_get(worker)) { |
836 | /* no task if node is/was offline */ | |
837 | if (worker->task) | |
838 | ret = func(worker, data); | |
839 | io_worker_release(worker); | |
840 | if (ret) | |
841 | break; | |
842 | } | |
843 | } | |
844 | ||
845 | return ret; | |
846 | } | |
847 | ||
848 | static bool io_wq_worker_wake(struct io_worker *worker, void *data) | |
849 | { | |
6cf5862e | 850 | __set_notify_signal(worker->task); |
c4068bf8 HD |
851 | wake_up_process(worker->task); |
852 | return false; | |
853 | } | |
854 | ||
eb47943f | 855 | static void io_run_cancel(struct io_wq_work *work, struct io_wq *wq) |
fc04c39b PB |
856 | { |
857 | do { | |
fc04c39b | 858 | work->flags |= IO_WQ_WORK_CANCEL; |
5280f7e5 PB |
859 | wq->do_work(work); |
860 | work = wq->free_work(work); | |
fc04c39b PB |
861 | } while (work); |
862 | } | |
863 | ||
eb47943f | 864 | static void io_wq_insert_work(struct io_wq *wq, struct io_wq_work *work) |
86f3cd1b | 865 | { |
eb47943f | 866 | struct io_wq_acct *acct = io_work_get_acct(wq, work); |
86f3cd1b PB |
867 | unsigned int hash; |
868 | struct io_wq_work *tail; | |
869 | ||
870 | if (!io_wq_is_hashed(work)) { | |
871 | append: | |
f95dc207 | 872 | wq_list_add_tail(&work->list, &acct->work_list); |
86f3cd1b PB |
873 | return; |
874 | } | |
875 | ||
876 | hash = io_get_work_hash(work); | |
eb47943f GKB |
877 | tail = wq->hash_tail[hash]; |
878 | wq->hash_tail[hash] = work; | |
86f3cd1b PB |
879 | if (!tail) |
880 | goto append; | |
881 | ||
f95dc207 | 882 | wq_list_add_after(&work->list, &tail->list, &acct->work_list); |
86f3cd1b PB |
883 | } |
884 | ||
713b9825 PB |
885 | static bool io_wq_work_match_item(struct io_wq_work *work, void *data) |
886 | { | |
887 | return work == data; | |
888 | } | |
889 | ||
eb47943f | 890 | void io_wq_enqueue(struct io_wq *wq, struct io_wq_work *work) |
771b53d0 | 891 | { |
eb47943f | 892 | struct io_wq_acct *acct = io_work_get_acct(wq, work); |
42abc95f | 893 | struct io_cb_cancel_data match; |
94ffb0a2 JA |
894 | unsigned work_flags = work->flags; |
895 | bool do_create; | |
771b53d0 | 896 | |
991468dc JA |
897 | /* |
898 | * If io-wq is exiting for this task, or if the request has explicitly | |
899 | * been marked as one that should not get executed, cancel it here. | |
900 | */ | |
eb47943f | 901 | if (test_bit(IO_WQ_BIT_EXIT, &wq->state) || |
991468dc | 902 | (work->flags & IO_WQ_WORK_CANCEL)) { |
eb47943f | 903 | io_run_cancel(work, wq); |
4fb6ac32 JA |
904 | return; |
905 | } | |
906 | ||
42abc95f | 907 | raw_spin_lock(&acct->lock); |
eb47943f | 908 | io_wq_insert_work(wq, work); |
f95dc207 | 909 | clear_bit(IO_ACCT_STALLED_BIT, &acct->flags); |
42abc95f | 910 | raw_spin_unlock(&acct->lock); |
94ffb0a2 | 911 | |
eb47943f | 912 | raw_spin_lock(&wq->lock); |
94ffb0a2 | 913 | rcu_read_lock(); |
eb47943f | 914 | do_create = !io_wq_activate_free_worker(wq, acct); |
94ffb0a2 JA |
915 | rcu_read_unlock(); |
916 | ||
eb47943f | 917 | raw_spin_unlock(&wq->lock); |
771b53d0 | 918 | |
94ffb0a2 | 919 | if (do_create && ((work_flags & IO_WQ_WORK_CONCURRENT) || |
3146cba9 JA |
920 | !atomic_read(&acct->nr_running))) { |
921 | bool did_create; | |
922 | ||
eb47943f | 923 | did_create = io_wq_create_worker(wq, acct); |
713b9825 PB |
924 | if (likely(did_create)) |
925 | return; | |
926 | ||
eb47943f | 927 | raw_spin_lock(&wq->lock); |
42abc95f | 928 | if (acct->nr_workers) { |
eb47943f | 929 | raw_spin_unlock(&wq->lock); |
42abc95f | 930 | return; |
3146cba9 | 931 | } |
eb47943f | 932 | raw_spin_unlock(&wq->lock); |
42abc95f HX |
933 | |
934 | /* fatal condition, failed to create the first worker */ | |
935 | match.fn = io_wq_work_match_item, | |
936 | match.data = work, | |
937 | match.cancel_all = false, | |
938 | ||
eb47943f | 939 | io_acct_cancel_pending_work(wq, acct, &match); |
3146cba9 | 940 | } |
771b53d0 JA |
941 | } |
942 | ||
771b53d0 | 943 | /* |
8766dd51 PB |
944 | * Work items that hash to the same value will not be done in parallel. |
945 | * Used to limit concurrent writes, generally hashed by inode. | |
771b53d0 | 946 | */ |
8766dd51 | 947 | void io_wq_hash_work(struct io_wq_work *work, void *val) |
771b53d0 | 948 | { |
8766dd51 | 949 | unsigned int bit; |
771b53d0 JA |
950 | |
951 | bit = hash_ptr(val, IO_WQ_HASH_ORDER); | |
952 | work->flags |= (IO_WQ_WORK_HASHED | (bit << IO_WQ_HASH_SHIFT)); | |
771b53d0 JA |
953 | } |
954 | ||
361aee45 JA |
955 | static bool __io_wq_worker_cancel(struct io_worker *worker, |
956 | struct io_cb_cancel_data *match, | |
957 | struct io_wq_work *work) | |
958 | { | |
959 | if (work && match->fn(work, match->data)) { | |
960 | work->flags |= IO_WQ_WORK_CANCEL; | |
6cf5862e | 961 | __set_notify_signal(worker->task); |
361aee45 JA |
962 | return true; |
963 | } | |
964 | ||
965 | return false; | |
966 | } | |
967 | ||
2293b419 | 968 | static bool io_wq_worker_cancel(struct io_worker *worker, void *data) |
62755e35 | 969 | { |
2293b419 | 970 | struct io_cb_cancel_data *match = data; |
62755e35 JA |
971 | |
972 | /* | |
973 | * Hold the lock to avoid ->cur_work going out of scope, caller | |
36c2f922 | 974 | * may dereference the passed in work. |
62755e35 | 975 | */ |
081b5820 | 976 | raw_spin_lock(&worker->lock); |
361aee45 JA |
977 | if (__io_wq_worker_cancel(worker, match, worker->cur_work) || |
978 | __io_wq_worker_cancel(worker, match, worker->next_work)) | |
4f26bda1 | 979 | match->nr_running++; |
081b5820 | 980 | raw_spin_unlock(&worker->lock); |
771b53d0 | 981 | |
4f26bda1 | 982 | return match->nr_running && !match->cancel_all; |
771b53d0 JA |
983 | } |
984 | ||
eb47943f | 985 | static inline void io_wq_remove_pending(struct io_wq *wq, |
204361a7 PB |
986 | struct io_wq_work *work, |
987 | struct io_wq_work_node *prev) | |
988 | { | |
eb47943f | 989 | struct io_wq_acct *acct = io_work_get_acct(wq, work); |
204361a7 PB |
990 | unsigned int hash = io_get_work_hash(work); |
991 | struct io_wq_work *prev_work = NULL; | |
992 | ||
eb47943f | 993 | if (io_wq_is_hashed(work) && work == wq->hash_tail[hash]) { |
204361a7 PB |
994 | if (prev) |
995 | prev_work = container_of(prev, struct io_wq_work, list); | |
996 | if (prev_work && io_get_work_hash(prev_work) == hash) | |
eb47943f | 997 | wq->hash_tail[hash] = prev_work; |
204361a7 | 998 | else |
eb47943f | 999 | wq->hash_tail[hash] = NULL; |
204361a7 | 1000 | } |
f95dc207 | 1001 | wq_list_del(&acct->work_list, &work->list, prev); |
204361a7 PB |
1002 | } |
1003 | ||
eb47943f | 1004 | static bool io_acct_cancel_pending_work(struct io_wq *wq, |
dfd63baf | 1005 | struct io_wq_acct *acct, |
3146cba9 | 1006 | struct io_cb_cancel_data *match) |
771b53d0 | 1007 | { |
6206f0e1 | 1008 | struct io_wq_work_node *node, *prev; |
771b53d0 | 1009 | struct io_wq_work *work; |
771b53d0 | 1010 | |
42abc95f | 1011 | raw_spin_lock(&acct->lock); |
3146cba9 JA |
1012 | wq_list_for_each(node, prev, &acct->work_list) { |
1013 | work = container_of(node, struct io_wq_work, list); | |
1014 | if (!match->fn(work, match->data)) | |
1015 | continue; | |
eb47943f | 1016 | io_wq_remove_pending(wq, work, prev); |
42abc95f | 1017 | raw_spin_unlock(&acct->lock); |
eb47943f | 1018 | io_run_cancel(work, wq); |
3146cba9 JA |
1019 | match->nr_pending++; |
1020 | /* not safe to continue after unlock */ | |
1021 | return true; | |
1022 | } | |
42abc95f | 1023 | raw_spin_unlock(&acct->lock); |
3146cba9 JA |
1024 | |
1025 | return false; | |
1026 | } | |
1027 | ||
eb47943f | 1028 | static void io_wq_cancel_pending_work(struct io_wq *wq, |
dfd63baf | 1029 | struct io_cb_cancel_data *match) |
3146cba9 JA |
1030 | { |
1031 | int i; | |
4f26bda1 | 1032 | retry: |
f95dc207 | 1033 | for (i = 0; i < IO_WQ_ACCT_NR; i++) { |
eb47943f | 1034 | struct io_wq_acct *acct = io_get_acct(wq, i == 0); |
4f26bda1 | 1035 | |
eb47943f | 1036 | if (io_acct_cancel_pending_work(wq, acct, match)) { |
3146cba9 JA |
1037 | if (match->cancel_all) |
1038 | goto retry; | |
36e4c58b | 1039 | break; |
f95dc207 | 1040 | } |
771b53d0 | 1041 | } |
f4c2665e PB |
1042 | } |
1043 | ||
eb47943f | 1044 | static void io_wq_cancel_running_work(struct io_wq *wq, |
f4c2665e PB |
1045 | struct io_cb_cancel_data *match) |
1046 | { | |
771b53d0 | 1047 | rcu_read_lock(); |
eb47943f | 1048 | io_wq_for_each_worker(wq, io_wq_worker_cancel, match); |
771b53d0 | 1049 | rcu_read_unlock(); |
771b53d0 JA |
1050 | } |
1051 | ||
2293b419 | 1052 | enum io_wq_cancel io_wq_cancel_cb(struct io_wq *wq, work_cancel_fn *cancel, |
4f26bda1 | 1053 | void *data, bool cancel_all) |
771b53d0 | 1054 | { |
2293b419 | 1055 | struct io_cb_cancel_data match = { |
4f26bda1 PB |
1056 | .fn = cancel, |
1057 | .data = data, | |
1058 | .cancel_all = cancel_all, | |
00bcda13 | 1059 | }; |
771b53d0 | 1060 | |
f4c2665e PB |
1061 | /* |
1062 | * First check pending list, if we're lucky we can just remove it | |
1063 | * from there. CANCEL_OK means that the work is returned as-new, | |
1064 | * no completion will be posted for it. | |
efdf5184 JA |
1065 | * |
1066 | * Then check if a free (going busy) or busy worker has the work | |
f4c2665e PB |
1067 | * currently running. If we find it there, we'll return CANCEL_RUNNING |
1068 | * as an indication that we attempt to signal cancellation. The | |
1069 | * completion will run normally in this case. | |
efdf5184 | 1070 | * |
eb47943f | 1071 | * Do both of these while holding the wq->lock, to ensure that |
efdf5184 | 1072 | * we'll find a work item regardless of state. |
f4c2665e | 1073 | */ |
eb47943f | 1074 | io_wq_cancel_pending_work(wq, &match); |
da64d6db BL |
1075 | if (match.nr_pending && !match.cancel_all) |
1076 | return IO_WQ_CANCEL_OK; | |
efdf5184 | 1077 | |
eb47943f GKB |
1078 | raw_spin_lock(&wq->lock); |
1079 | io_wq_cancel_running_work(wq, &match); | |
1080 | raw_spin_unlock(&wq->lock); | |
da64d6db BL |
1081 | if (match.nr_running && !match.cancel_all) |
1082 | return IO_WQ_CANCEL_RUNNING; | |
f4c2665e | 1083 | |
4f26bda1 PB |
1084 | if (match.nr_running) |
1085 | return IO_WQ_CANCEL_RUNNING; | |
1086 | if (match.nr_pending) | |
1087 | return IO_WQ_CANCEL_OK; | |
f4c2665e | 1088 | return IO_WQ_CANCEL_NOTFOUND; |
771b53d0 JA |
1089 | } |
1090 | ||
eb47943f | 1091 | static int io_wq_hash_wake(struct wait_queue_entry *wait, unsigned mode, |
e941894e JA |
1092 | int sync, void *key) |
1093 | { | |
eb47943f | 1094 | struct io_wq *wq = container_of(wait, struct io_wq, wait); |
f95dc207 | 1095 | int i; |
e941894e JA |
1096 | |
1097 | list_del_init(&wait->entry); | |
1098 | ||
1099 | rcu_read_lock(); | |
f95dc207 | 1100 | for (i = 0; i < IO_WQ_ACCT_NR; i++) { |
dfd63baf | 1101 | struct io_wq_acct *acct = &wq->acct[i]; |
f95dc207 JA |
1102 | |
1103 | if (test_and_clear_bit(IO_ACCT_STALLED_BIT, &acct->flags)) | |
eb47943f | 1104 | io_wq_activate_free_worker(wq, acct); |
f95dc207 | 1105 | } |
e941894e | 1106 | rcu_read_unlock(); |
e941894e JA |
1107 | return 1; |
1108 | } | |
1109 | ||
576a347b | 1110 | struct io_wq *io_wq_create(unsigned bounded, struct io_wq_data *data) |
771b53d0 | 1111 | { |
da64d6db | 1112 | int ret, i; |
771b53d0 JA |
1113 | struct io_wq *wq; |
1114 | ||
f5fa38c5 | 1115 | if (WARN_ON_ONCE(!data->free_work || !data->do_work)) |
e9fd9396 | 1116 | return ERR_PTR(-EINVAL); |
e6ab8991 PB |
1117 | if (WARN_ON_ONCE(!bounded)) |
1118 | return ERR_PTR(-EINVAL); | |
e9fd9396 | 1119 | |
da64d6db | 1120 | wq = kzalloc(sizeof(struct io_wq), GFP_KERNEL); |
771b53d0 JA |
1121 | if (!wq) |
1122 | return ERR_PTR(-ENOMEM); | |
43c01fbe JA |
1123 | ret = cpuhp_state_add_instance_nocalls(io_wq_online, &wq->cpuhp_node); |
1124 | if (ret) | |
c7f405d6 | 1125 | goto err_wq; |
771b53d0 | 1126 | |
e941894e JA |
1127 | refcount_inc(&data->hash->refs); |
1128 | wq->hash = data->hash; | |
e9fd9396 | 1129 | wq->free_work = data->free_work; |
f5fa38c5 | 1130 | wq->do_work = data->do_work; |
7d723065 | 1131 | |
43c01fbe | 1132 | ret = -ENOMEM; |
da64d6db | 1133 | |
eb47943f | 1134 | if (!alloc_cpumask_var(&wq->cpu_mask, GFP_KERNEL)) |
da64d6db | 1135 | goto err; |
eb47943f | 1136 | cpumask_copy(wq->cpu_mask, cpu_possible_mask); |
dfd63baf GKB |
1137 | wq->acct[IO_WQ_ACCT_BOUND].max_workers = bounded; |
1138 | wq->acct[IO_WQ_ACCT_UNBOUND].max_workers = | |
da64d6db | 1139 | task_rlimit(current, RLIMIT_NPROC); |
eb47943f GKB |
1140 | INIT_LIST_HEAD(&wq->wait.entry); |
1141 | wq->wait.func = io_wq_hash_wake; | |
da64d6db | 1142 | for (i = 0; i < IO_WQ_ACCT_NR; i++) { |
dfd63baf | 1143 | struct io_wq_acct *acct = &wq->acct[i]; |
da64d6db BL |
1144 | |
1145 | acct->index = i; | |
1146 | atomic_set(&acct->nr_running, 0); | |
1147 | INIT_WQ_LIST(&acct->work_list); | |
1148 | raw_spin_lock_init(&acct->lock); | |
771b53d0 | 1149 | } |
eb47943f GKB |
1150 | |
1151 | raw_spin_lock_init(&wq->lock); | |
1152 | INIT_HLIST_NULLS_HEAD(&wq->free_list, 0); | |
1153 | INIT_LIST_HEAD(&wq->all_list); | |
771b53d0 | 1154 | |
685fe7fe | 1155 | wq->task = get_task_struct(data->task); |
685fe7fe JA |
1156 | atomic_set(&wq->worker_refs, 1); |
1157 | init_completion(&wq->worker_done); | |
1158 | return wq; | |
b60fda60 | 1159 | err: |
dc7bbc9e | 1160 | io_wq_put_hash(data->hash); |
43c01fbe | 1161 | cpuhp_state_remove_instance_nocalls(io_wq_online, &wq->cpuhp_node); |
da64d6db | 1162 | |
eb47943f | 1163 | free_cpumask_var(wq->cpu_mask); |
43c01fbe | 1164 | err_wq: |
b60fda60 | 1165 | kfree(wq); |
771b53d0 JA |
1166 | return ERR_PTR(ret); |
1167 | } | |
1168 | ||
c80ca470 JA |
1169 | static bool io_task_work_match(struct callback_head *cb, void *data) |
1170 | { | |
d3e9f732 | 1171 | struct io_worker *worker; |
c80ca470 | 1172 | |
3b33e3f4 | 1173 | if (cb->func != create_worker_cb && cb->func != create_worker_cont) |
c80ca470 | 1174 | return false; |
d3e9f732 | 1175 | worker = container_of(cb, struct io_worker, create_work); |
eb47943f | 1176 | return worker->wq == data; |
c80ca470 JA |
1177 | } |
1178 | ||
17a91051 PB |
1179 | void io_wq_exit_start(struct io_wq *wq) |
1180 | { | |
1181 | set_bit(IO_WQ_BIT_EXIT, &wq->state); | |
1182 | } | |
1183 | ||
71a85387 | 1184 | static void io_wq_cancel_tw_create(struct io_wq *wq) |
afcc4015 | 1185 | { |
685fe7fe | 1186 | struct callback_head *cb; |
685fe7fe | 1187 | |
c80ca470 | 1188 | while ((cb = task_work_cancel_match(wq->task, io_task_work_match, wq)) != NULL) { |
d3e9f732 | 1189 | struct io_worker *worker; |
685fe7fe | 1190 | |
d3e9f732 | 1191 | worker = container_of(cb, struct io_worker, create_work); |
1d5f5ea7 | 1192 | io_worker_cancel_cb(worker); |
e6db6f93 JA |
1193 | /* |
1194 | * Only the worker continuation helper has worker allocated and | |
1195 | * hence needs freeing. | |
1196 | */ | |
1197 | if (cb->func == create_worker_cont) | |
1198 | kfree(worker); | |
685fe7fe | 1199 | } |
71a85387 JA |
1200 | } |
1201 | ||
1202 | static void io_wq_exit_workers(struct io_wq *wq) | |
1203 | { | |
71a85387 JA |
1204 | if (!wq->task) |
1205 | return; | |
1206 | ||
1207 | io_wq_cancel_tw_create(wq); | |
685fe7fe JA |
1208 | |
1209 | rcu_read_lock(); | |
eb47943f | 1210 | io_wq_for_each_worker(wq, io_wq_worker_wake, NULL); |
685fe7fe JA |
1211 | rcu_read_unlock(); |
1212 | io_worker_ref_put(wq); | |
1213 | wait_for_completion(&wq->worker_done); | |
3743c172 | 1214 | |
da64d6db | 1215 | spin_lock_irq(&wq->hash->wait.lock); |
eb47943f | 1216 | list_del_init(&wq->wait.entry); |
da64d6db BL |
1217 | spin_unlock_irq(&wq->hash->wait.lock); |
1218 | ||
685fe7fe JA |
1219 | put_task_struct(wq->task); |
1220 | wq->task = NULL; | |
afcc4015 JA |
1221 | } |
1222 | ||
4fb6ac32 | 1223 | static void io_wq_destroy(struct io_wq *wq) |
771b53d0 | 1224 | { |
da64d6db BL |
1225 | struct io_cb_cancel_data match = { |
1226 | .fn = io_wq_work_match_all, | |
1227 | .cancel_all = true, | |
1228 | }; | |
771b53d0 | 1229 | |
43c01fbe | 1230 | cpuhp_state_remove_instance_nocalls(io_wq_online, &wq->cpuhp_node); |
eb47943f GKB |
1231 | io_wq_cancel_pending_work(wq, &match); |
1232 | free_cpumask_var(wq->cpu_mask); | |
e941894e | 1233 | io_wq_put_hash(wq->hash); |
771b53d0 | 1234 | kfree(wq); |
4fb6ac32 JA |
1235 | } |
1236 | ||
afcc4015 JA |
1237 | void io_wq_put_and_exit(struct io_wq *wq) |
1238 | { | |
17a91051 PB |
1239 | WARN_ON_ONCE(!test_bit(IO_WQ_BIT_EXIT, &wq->state)); |
1240 | ||
685fe7fe | 1241 | io_wq_exit_workers(wq); |
382cb030 | 1242 | io_wq_destroy(wq); |
afcc4015 JA |
1243 | } |
1244 | ||
0e03496d JA |
1245 | struct online_data { |
1246 | unsigned int cpu; | |
1247 | bool online; | |
1248 | }; | |
1249 | ||
43c01fbe JA |
1250 | static bool io_wq_worker_affinity(struct io_worker *worker, void *data) |
1251 | { | |
0e03496d | 1252 | struct online_data *od = data; |
e0051d7d | 1253 | |
0e03496d | 1254 | if (od->online) |
eb47943f | 1255 | cpumask_set_cpu(od->cpu, worker->wq->cpu_mask); |
0e03496d | 1256 | else |
eb47943f | 1257 | cpumask_clear_cpu(od->cpu, worker->wq->cpu_mask); |
43c01fbe JA |
1258 | return false; |
1259 | } | |
1260 | ||
0e03496d | 1261 | static int __io_wq_cpu_online(struct io_wq *wq, unsigned int cpu, bool online) |
43c01fbe | 1262 | { |
0e03496d JA |
1263 | struct online_data od = { |
1264 | .cpu = cpu, | |
1265 | .online = online | |
1266 | }; | |
43c01fbe JA |
1267 | |
1268 | rcu_read_lock(); | |
eb47943f | 1269 | io_wq_for_each_worker(wq, io_wq_worker_affinity, &od); |
43c01fbe JA |
1270 | rcu_read_unlock(); |
1271 | return 0; | |
1272 | } | |
1273 | ||
0e03496d JA |
1274 | static int io_wq_cpu_online(unsigned int cpu, struct hlist_node *node) |
1275 | { | |
1276 | struct io_wq *wq = hlist_entry_safe(node, struct io_wq, cpuhp_node); | |
1277 | ||
1278 | return __io_wq_cpu_online(wq, cpu, true); | |
1279 | } | |
1280 | ||
1281 | static int io_wq_cpu_offline(unsigned int cpu, struct hlist_node *node) | |
1282 | { | |
1283 | struct io_wq *wq = hlist_entry_safe(node, struct io_wq, cpuhp_node); | |
1284 | ||
1285 | return __io_wq_cpu_online(wq, cpu, false); | |
1286 | } | |
1287 | ||
fe76421d JA |
1288 | int io_wq_cpu_affinity(struct io_wq *wq, cpumask_var_t mask) |
1289 | { | |
fe76421d | 1290 | rcu_read_lock(); |
da64d6db | 1291 | if (mask) |
eb47943f | 1292 | cpumask_copy(wq->cpu_mask, mask); |
da64d6db | 1293 | else |
eb47943f | 1294 | cpumask_copy(wq->cpu_mask, cpu_possible_mask); |
fe76421d | 1295 | rcu_read_unlock(); |
da64d6db | 1296 | |
fe76421d JA |
1297 | return 0; |
1298 | } | |
1299 | ||
2e480058 JA |
1300 | /* |
1301 | * Set max number of unbounded workers, returns old value. If new_count is 0, | |
1302 | * then just return the old value. | |
1303 | */ | |
1304 | int io_wq_max_workers(struct io_wq *wq, int *new_count) | |
1305 | { | |
dfd63baf | 1306 | struct io_wq_acct *acct; |
71c9ce27 | 1307 | int prev[IO_WQ_ACCT_NR]; |
da64d6db | 1308 | int i; |
2e480058 | 1309 | |
dd47c104 ES |
1310 | BUILD_BUG_ON((int) IO_WQ_ACCT_BOUND != (int) IO_WQ_BOUND); |
1311 | BUILD_BUG_ON((int) IO_WQ_ACCT_UNBOUND != (int) IO_WQ_UNBOUND); | |
1312 | BUILD_BUG_ON((int) IO_WQ_ACCT_NR != 2); | |
1313 | ||
86127bb1 | 1314 | for (i = 0; i < IO_WQ_ACCT_NR; i++) { |
2e480058 JA |
1315 | if (new_count[i] > task_rlimit(current, RLIMIT_NPROC)) |
1316 | new_count[i] = task_rlimit(current, RLIMIT_NPROC); | |
1317 | } | |
1318 | ||
71c9ce27 BZ |
1319 | for (i = 0; i < IO_WQ_ACCT_NR; i++) |
1320 | prev[i] = 0; | |
1321 | ||
2e480058 | 1322 | rcu_read_lock(); |
2e480058 | 1323 | |
eb47943f | 1324 | raw_spin_lock(&wq->lock); |
da64d6db | 1325 | for (i = 0; i < IO_WQ_ACCT_NR; i++) { |
dfd63baf | 1326 | acct = &wq->acct[i]; |
da64d6db BL |
1327 | prev[i] = max_t(int, acct->max_workers, prev[i]); |
1328 | if (new_count[i]) | |
1329 | acct->max_workers = new_count[i]; | |
2e480058 | 1330 | } |
eb47943f | 1331 | raw_spin_unlock(&wq->lock); |
2e480058 | 1332 | rcu_read_unlock(); |
71c9ce27 BZ |
1333 | |
1334 | for (i = 0; i < IO_WQ_ACCT_NR; i++) | |
1335 | new_count[i] = prev[i]; | |
1336 | ||
2e480058 JA |
1337 | return 0; |
1338 | } | |
1339 | ||
43c01fbe JA |
1340 | static __init int io_wq_init(void) |
1341 | { | |
1342 | int ret; | |
1343 | ||
1344 | ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "io-wq/online", | |
0e03496d | 1345 | io_wq_cpu_online, io_wq_cpu_offline); |
43c01fbe JA |
1346 | if (ret < 0) |
1347 | return ret; | |
1348 | io_wq_online = ret; | |
1349 | return 0; | |
1350 | } | |
1351 | subsys_initcall(io_wq_init); |