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