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