]>
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> | |
12 | #include <linux/mm.h> | |
771b53d0 JA |
13 | #include <linux/sched/mm.h> |
14 | #include <linux/percpu.h> | |
15 | #include <linux/slab.h> | |
771b53d0 | 16 | #include <linux/rculist_nulls.h> |
43c01fbe | 17 | #include <linux/cpu.h> |
3bfe6106 | 18 | #include <linux/tracehook.h> |
771b53d0 JA |
19 | |
20 | #include "io-wq.h" | |
21 | ||
22 | #define WORKER_IDLE_TIMEOUT (5 * HZ) | |
23 | ||
24 | enum { | |
25 | IO_WORKER_F_UP = 1, /* up and active */ | |
26 | IO_WORKER_F_RUNNING = 2, /* account as running */ | |
27 | IO_WORKER_F_FREE = 4, /* worker on free list */ | |
145cc8c6 JA |
28 | IO_WORKER_F_FIXED = 8, /* static idle worker */ |
29 | IO_WORKER_F_BOUND = 16, /* is doing bounded work */ | |
771b53d0 JA |
30 | }; |
31 | ||
32 | enum { | |
33 | IO_WQ_BIT_EXIT = 0, /* wq exiting */ | |
771b53d0 JA |
34 | }; |
35 | ||
36 | enum { | |
37 | IO_WQE_FLAG_STALLED = 1, /* stalled on hash */ | |
38 | }; | |
39 | ||
40 | /* | |
41 | * One for each thread in a wqe pool | |
42 | */ | |
43 | struct io_worker { | |
44 | refcount_t ref; | |
45 | unsigned flags; | |
46 | struct hlist_nulls_node nulls_node; | |
e61df66c | 47 | struct list_head all_list; |
771b53d0 | 48 | struct task_struct *task; |
771b53d0 | 49 | struct io_wqe *wqe; |
36c2f922 | 50 | |
771b53d0 | 51 | struct io_wq_work *cur_work; |
36c2f922 | 52 | spinlock_t lock; |
771b53d0 | 53 | |
eb2de941 JA |
54 | struct completion ref_done; |
55 | ||
771b53d0 | 56 | struct rcu_head rcu; |
771b53d0 JA |
57 | }; |
58 | ||
771b53d0 JA |
59 | #if BITS_PER_LONG == 64 |
60 | #define IO_WQ_HASH_ORDER 6 | |
61 | #else | |
62 | #define IO_WQ_HASH_ORDER 5 | |
63 | #endif | |
64 | ||
86f3cd1b PB |
65 | #define IO_WQ_NR_HASH_BUCKETS (1u << IO_WQ_HASH_ORDER) |
66 | ||
c5def4ab JA |
67 | struct io_wqe_acct { |
68 | unsigned nr_workers; | |
69 | unsigned max_workers; | |
685fe7fe | 70 | int index; |
c5def4ab JA |
71 | atomic_t nr_running; |
72 | }; | |
73 | ||
74 | enum { | |
75 | IO_WQ_ACCT_BOUND, | |
76 | IO_WQ_ACCT_UNBOUND, | |
77 | }; | |
78 | ||
771b53d0 JA |
79 | /* |
80 | * Per-node worker thread pool | |
81 | */ | |
82 | struct io_wqe { | |
83 | struct { | |
95da8465 | 84 | raw_spinlock_t lock; |
6206f0e1 | 85 | struct io_wq_work_list work_list; |
771b53d0 JA |
86 | unsigned flags; |
87 | } ____cacheline_aligned_in_smp; | |
88 | ||
89 | int node; | |
c5def4ab | 90 | struct io_wqe_acct acct[2]; |
771b53d0 | 91 | |
021d1cdd | 92 | struct hlist_nulls_head free_list; |
e61df66c | 93 | struct list_head all_list; |
771b53d0 | 94 | |
e941894e JA |
95 | struct wait_queue_entry wait; |
96 | ||
771b53d0 | 97 | struct io_wq *wq; |
86f3cd1b | 98 | struct io_wq_work *hash_tail[IO_WQ_NR_HASH_BUCKETS]; |
771b53d0 JA |
99 | }; |
100 | ||
101 | /* | |
102 | * Per io_wq state | |
103 | */ | |
104 | struct io_wq { | |
105 | struct io_wqe **wqes; | |
106 | unsigned long state; | |
771b53d0 | 107 | |
e9fd9396 | 108 | free_work_fn *free_work; |
f5fa38c5 | 109 | io_wq_work_fn *do_work; |
7d723065 | 110 | |
e941894e JA |
111 | struct io_wq_hash *hash; |
112 | ||
771b53d0 | 113 | refcount_t refs; |
848f7e18 | 114 | |
fb3a1f6c JA |
115 | atomic_t worker_refs; |
116 | struct completion worker_done; | |
117 | ||
43c01fbe | 118 | struct hlist_node cpuhp_node; |
3bfe6106 | 119 | |
685fe7fe | 120 | struct task_struct *task; |
771b53d0 JA |
121 | }; |
122 | ||
43c01fbe JA |
123 | static enum cpuhp_state io_wq_online; |
124 | ||
f0127254 JA |
125 | struct io_cb_cancel_data { |
126 | work_cancel_fn *fn; | |
127 | void *data; | |
128 | int nr_running; | |
129 | int nr_pending; | |
130 | bool cancel_all; | |
131 | }; | |
132 | ||
685fe7fe | 133 | static void create_io_worker(struct io_wq *wq, struct io_wqe *wqe, int index); |
f0127254 | 134 | |
771b53d0 JA |
135 | static bool io_worker_get(struct io_worker *worker) |
136 | { | |
137 | return refcount_inc_not_zero(&worker->ref); | |
138 | } | |
139 | ||
140 | static void io_worker_release(struct io_worker *worker) | |
141 | { | |
142 | if (refcount_dec_and_test(&worker->ref)) | |
eb2de941 | 143 | complete(&worker->ref_done); |
771b53d0 JA |
144 | } |
145 | ||
8418f22a PB |
146 | static inline struct io_wqe_acct *io_get_acct(struct io_wqe *wqe, bool bound) |
147 | { | |
148 | return &wqe->acct[bound ? IO_WQ_ACCT_BOUND : IO_WQ_ACCT_UNBOUND]; | |
149 | } | |
150 | ||
c5def4ab JA |
151 | static inline struct io_wqe_acct *io_work_get_acct(struct io_wqe *wqe, |
152 | struct io_wq_work *work) | |
153 | { | |
8418f22a | 154 | return io_get_acct(wqe, !(work->flags & IO_WQ_WORK_UNBOUND)); |
c5def4ab JA |
155 | } |
156 | ||
958234d5 | 157 | static inline struct io_wqe_acct *io_wqe_get_acct(struct io_worker *worker) |
c5def4ab | 158 | { |
8418f22a | 159 | return io_get_acct(worker->wqe, worker->flags & IO_WORKER_F_BOUND); |
c5def4ab JA |
160 | } |
161 | ||
685fe7fe JA |
162 | static void io_worker_ref_put(struct io_wq *wq) |
163 | { | |
164 | if (atomic_dec_and_test(&wq->worker_refs)) | |
165 | complete(&wq->worker_done); | |
166 | } | |
167 | ||
771b53d0 JA |
168 | static void io_worker_exit(struct io_worker *worker) |
169 | { | |
170 | struct io_wqe *wqe = worker->wqe; | |
958234d5 | 171 | struct io_wqe_acct *acct = io_wqe_get_acct(worker); |
bf1daa4b | 172 | unsigned flags; |
771b53d0 | 173 | |
eb2de941 JA |
174 | if (refcount_dec_and_test(&worker->ref)) |
175 | complete(&worker->ref_done); | |
176 | wait_for_completion(&worker->ref_done); | |
771b53d0 JA |
177 | |
178 | preempt_disable(); | |
179 | current->flags &= ~PF_IO_WORKER; | |
bf1daa4b JA |
180 | flags = worker->flags; |
181 | worker->flags = 0; | |
182 | if (flags & IO_WORKER_F_RUNNING) | |
c5def4ab | 183 | atomic_dec(&acct->nr_running); |
771b53d0 JA |
184 | worker->flags = 0; |
185 | preempt_enable(); | |
186 | ||
95da8465 | 187 | raw_spin_lock_irq(&wqe->lock); |
bf1daa4b JA |
188 | if (flags & IO_WORKER_F_FREE) |
189 | hlist_nulls_del_rcu(&worker->nulls_node); | |
e61df66c | 190 | list_del_rcu(&worker->all_list); |
c5def4ab | 191 | acct->nr_workers--; |
95da8465 | 192 | raw_spin_unlock_irq(&wqe->lock); |
771b53d0 | 193 | |
364b05fd | 194 | kfree_rcu(worker, rcu); |
685fe7fe | 195 | io_worker_ref_put(wqe->wq); |
46fe18b1 | 196 | do_exit(0); |
771b53d0 JA |
197 | } |
198 | ||
c5def4ab JA |
199 | static inline bool io_wqe_run_queue(struct io_wqe *wqe) |
200 | __must_hold(wqe->lock) | |
201 | { | |
6206f0e1 JA |
202 | if (!wq_list_empty(&wqe->work_list) && |
203 | !(wqe->flags & IO_WQE_FLAG_STALLED)) | |
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 JA |
211 | */ |
212 | static bool io_wqe_activate_free_worker(struct io_wqe *wqe) | |
213 | __must_hold(RCU) | |
214 | { | |
215 | struct hlist_nulls_node *n; | |
216 | struct io_worker *worker; | |
217 | ||
021d1cdd | 218 | n = rcu_dereference(hlist_nulls_first_rcu(&wqe->free_list)); |
c5def4ab JA |
219 | if (is_a_nulls(n)) |
220 | return false; | |
221 | ||
222 | worker = hlist_nulls_entry(n, struct io_worker, nulls_node); | |
223 | if (io_worker_get(worker)) { | |
506d95ff | 224 | wake_up_process(worker->task); |
c5def4ab JA |
225 | io_worker_release(worker); |
226 | return true; | |
227 | } | |
228 | ||
229 | return false; | |
230 | } | |
231 | ||
232 | /* | |
233 | * We need a worker. If we find a free one, we're good. If not, and we're | |
685fe7fe | 234 | * below the max number of workers, create one. |
c5def4ab JA |
235 | */ |
236 | static void io_wqe_wake_worker(struct io_wqe *wqe, struct io_wqe_acct *acct) | |
237 | { | |
238 | bool ret; | |
239 | ||
240 | /* | |
241 | * Most likely an attempt to queue unbounded work on an io_wq that | |
242 | * wasn't setup with any unbounded workers. | |
243 | */ | |
244 | WARN_ON_ONCE(!acct->max_workers); | |
245 | ||
246 | rcu_read_lock(); | |
247 | ret = io_wqe_activate_free_worker(wqe); | |
248 | rcu_read_unlock(); | |
249 | ||
685fe7fe JA |
250 | if (!ret && acct->nr_workers < acct->max_workers) { |
251 | atomic_inc(&acct->nr_running); | |
252 | atomic_inc(&wqe->wq->worker_refs); | |
253 | create_io_worker(wqe->wq, wqe, acct->index); | |
254 | } | |
c5def4ab JA |
255 | } |
256 | ||
958234d5 | 257 | static void io_wqe_inc_running(struct io_worker *worker) |
c5def4ab | 258 | { |
958234d5 | 259 | struct io_wqe_acct *acct = io_wqe_get_acct(worker); |
c5def4ab JA |
260 | |
261 | atomic_inc(&acct->nr_running); | |
262 | } | |
263 | ||
685fe7fe JA |
264 | struct create_worker_data { |
265 | struct callback_head work; | |
266 | struct io_wqe *wqe; | |
267 | int index; | |
268 | }; | |
269 | ||
270 | static void create_worker_cb(struct callback_head *cb) | |
271 | { | |
272 | struct create_worker_data *cwd; | |
273 | struct io_wq *wq; | |
274 | ||
275 | cwd = container_of(cb, struct create_worker_data, work); | |
276 | wq = cwd->wqe->wq; | |
277 | create_io_worker(wq, cwd->wqe, cwd->index); | |
278 | kfree(cwd); | |
279 | } | |
280 | ||
281 | static void io_queue_worker_create(struct io_wqe *wqe, struct io_wqe_acct *acct) | |
282 | { | |
283 | struct create_worker_data *cwd; | |
284 | struct io_wq *wq = wqe->wq; | |
285 | ||
286 | /* raced with exit, just ignore create call */ | |
287 | if (test_bit(IO_WQ_BIT_EXIT, &wq->state)) | |
288 | goto fail; | |
289 | ||
290 | cwd = kmalloc(sizeof(*cwd), GFP_ATOMIC); | |
291 | if (cwd) { | |
292 | init_task_work(&cwd->work, create_worker_cb); | |
293 | cwd->wqe = wqe; | |
294 | cwd->index = acct->index; | |
295 | if (!task_work_add(wq->task, &cwd->work, TWA_SIGNAL)) | |
296 | return; | |
297 | ||
298 | kfree(cwd); | |
299 | } | |
300 | fail: | |
301 | atomic_dec(&acct->nr_running); | |
302 | io_worker_ref_put(wq); | |
303 | } | |
304 | ||
958234d5 | 305 | static void io_wqe_dec_running(struct io_worker *worker) |
c5def4ab JA |
306 | __must_hold(wqe->lock) |
307 | { | |
958234d5 JA |
308 | struct io_wqe_acct *acct = io_wqe_get_acct(worker); |
309 | struct io_wqe *wqe = worker->wqe; | |
c5def4ab | 310 | |
685fe7fe JA |
311 | if (!(worker->flags & IO_WORKER_F_UP)) |
312 | return; | |
313 | ||
314 | if (atomic_dec_and_test(&acct->nr_running) && io_wqe_run_queue(wqe)) { | |
315 | atomic_inc(&acct->nr_running); | |
316 | atomic_inc(&wqe->wq->worker_refs); | |
317 | io_queue_worker_create(wqe, acct); | |
318 | } | |
c5def4ab JA |
319 | } |
320 | ||
771b53d0 JA |
321 | /* |
322 | * Worker will start processing some work. Move it to the busy list, if | |
323 | * it's currently on the freelist | |
324 | */ | |
325 | static void __io_worker_busy(struct io_wqe *wqe, struct io_worker *worker, | |
326 | struct io_wq_work *work) | |
327 | __must_hold(wqe->lock) | |
328 | { | |
c5def4ab JA |
329 | bool worker_bound, work_bound; |
330 | ||
417b5052 HX |
331 | BUILD_BUG_ON((IO_WQ_ACCT_UNBOUND ^ IO_WQ_ACCT_BOUND) != 1); |
332 | ||
771b53d0 JA |
333 | if (worker->flags & IO_WORKER_F_FREE) { |
334 | worker->flags &= ~IO_WORKER_F_FREE; | |
335 | hlist_nulls_del_init_rcu(&worker->nulls_node); | |
771b53d0 | 336 | } |
c5def4ab JA |
337 | |
338 | /* | |
339 | * If worker is moving from bound to unbound (or vice versa), then | |
340 | * ensure we update the running accounting. | |
341 | */ | |
b2e9c7d6 DC |
342 | worker_bound = (worker->flags & IO_WORKER_F_BOUND) != 0; |
343 | work_bound = (work->flags & IO_WQ_WORK_UNBOUND) == 0; | |
344 | if (worker_bound != work_bound) { | |
417b5052 | 345 | int index = work_bound ? IO_WQ_ACCT_UNBOUND : IO_WQ_ACCT_BOUND; |
958234d5 | 346 | io_wqe_dec_running(worker); |
417b5052 HX |
347 | worker->flags ^= IO_WORKER_F_BOUND; |
348 | wqe->acct[index].nr_workers--; | |
349 | wqe->acct[index ^ 1].nr_workers++; | |
958234d5 | 350 | io_wqe_inc_running(worker); |
c5def4ab | 351 | } |
771b53d0 JA |
352 | } |
353 | ||
354 | /* | |
355 | * No work, worker going to sleep. Move to freelist, and unuse mm if we | |
356 | * have one attached. Dropping the mm may potentially sleep, so we drop | |
357 | * the lock in that case and return success. Since the caller has to | |
358 | * retry the loop in that case (we changed task state), we don't regrab | |
359 | * the lock if we return success. | |
360 | */ | |
c6d77d92 | 361 | static void __io_worker_idle(struct io_wqe *wqe, struct io_worker *worker) |
771b53d0 JA |
362 | __must_hold(wqe->lock) |
363 | { | |
364 | if (!(worker->flags & IO_WORKER_F_FREE)) { | |
365 | worker->flags |= IO_WORKER_F_FREE; | |
021d1cdd | 366 | hlist_nulls_add_head_rcu(&worker->nulls_node, &wqe->free_list); |
771b53d0 | 367 | } |
771b53d0 JA |
368 | } |
369 | ||
60cf46ae PB |
370 | static inline unsigned int io_get_work_hash(struct io_wq_work *work) |
371 | { | |
372 | return work->flags >> IO_WQ_HASH_SHIFT; | |
373 | } | |
374 | ||
e941894e JA |
375 | static void io_wait_on_hash(struct io_wqe *wqe, unsigned int hash) |
376 | { | |
377 | struct io_wq *wq = wqe->wq; | |
378 | ||
379 | spin_lock(&wq->hash->wait.lock); | |
380 | if (list_empty(&wqe->wait.entry)) { | |
381 | __add_wait_queue(&wq->hash->wait, &wqe->wait); | |
382 | if (!test_bit(hash, &wq->hash->map)) { | |
383 | __set_current_state(TASK_RUNNING); | |
384 | list_del_init(&wqe->wait.entry); | |
385 | } | |
386 | } | |
387 | spin_unlock(&wq->hash->wait.lock); | |
388 | } | |
389 | ||
60cf46ae | 390 | static struct io_wq_work *io_get_next_work(struct io_wqe *wqe) |
771b53d0 JA |
391 | __must_hold(wqe->lock) |
392 | { | |
6206f0e1 | 393 | struct io_wq_work_node *node, *prev; |
86f3cd1b | 394 | struct io_wq_work *work, *tail; |
e941894e | 395 | unsigned int stall_hash = -1U; |
771b53d0 | 396 | |
6206f0e1 | 397 | wq_list_for_each(node, prev, &wqe->work_list) { |
e941894e JA |
398 | unsigned int hash; |
399 | ||
6206f0e1 JA |
400 | work = container_of(node, struct io_wq_work, list); |
401 | ||
771b53d0 | 402 | /* not hashed, can run anytime */ |
8766dd51 | 403 | if (!io_wq_is_hashed(work)) { |
86f3cd1b | 404 | wq_list_del(&wqe->work_list, node, prev); |
771b53d0 JA |
405 | return work; |
406 | } | |
407 | ||
60cf46ae | 408 | hash = io_get_work_hash(work); |
e941894e JA |
409 | /* all items with this hash lie in [work, tail] */ |
410 | tail = wqe->hash_tail[hash]; | |
411 | ||
412 | /* hashed, can run if not already running */ | |
413 | if (!test_and_set_bit(hash, &wqe->wq->hash->map)) { | |
86f3cd1b PB |
414 | wqe->hash_tail[hash] = NULL; |
415 | wq_list_cut(&wqe->work_list, &tail->list, prev); | |
771b53d0 JA |
416 | return work; |
417 | } | |
e941894e JA |
418 | if (stall_hash == -1U) |
419 | stall_hash = hash; | |
420 | /* fast forward to a next hash, for-each will fix up @prev */ | |
421 | node = &tail->list; | |
422 | } | |
423 | ||
424 | if (stall_hash != -1U) { | |
425 | raw_spin_unlock(&wqe->lock); | |
426 | io_wait_on_hash(wqe, stall_hash); | |
427 | raw_spin_lock(&wqe->lock); | |
771b53d0 JA |
428 | } |
429 | ||
430 | return NULL; | |
431 | } | |
432 | ||
00ddff43 | 433 | static bool io_flush_signals(void) |
cccf0ee8 | 434 | { |
0b8cfa97 | 435 | if (unlikely(test_thread_flag(TIF_NOTIFY_SIGNAL))) { |
00ddff43 | 436 | __set_current_state(TASK_RUNNING); |
0b8cfa97 | 437 | tracehook_notify_signal(); |
00ddff43 | 438 | return true; |
cccf0ee8 | 439 | } |
00ddff43 | 440 | return false; |
dc026a73 PB |
441 | } |
442 | ||
443 | static void io_assign_current_work(struct io_worker *worker, | |
444 | struct io_wq_work *work) | |
445 | { | |
d78298e7 | 446 | if (work) { |
3bfe6106 | 447 | io_flush_signals(); |
d78298e7 PB |
448 | cond_resched(); |
449 | } | |
dc026a73 PB |
450 | |
451 | spin_lock_irq(&worker->lock); | |
452 | worker->cur_work = work; | |
453 | spin_unlock_irq(&worker->lock); | |
454 | } | |
455 | ||
60cf46ae PB |
456 | static void io_wqe_enqueue(struct io_wqe *wqe, struct io_wq_work *work); |
457 | ||
771b53d0 JA |
458 | static void io_worker_handle_work(struct io_worker *worker) |
459 | __releases(wqe->lock) | |
460 | { | |
771b53d0 JA |
461 | struct io_wqe *wqe = worker->wqe; |
462 | struct io_wq *wq = wqe->wq; | |
c60eb049 | 463 | bool do_kill = test_bit(IO_WQ_BIT_EXIT, &wq->state); |
771b53d0 JA |
464 | |
465 | do { | |
86f3cd1b | 466 | struct io_wq_work *work; |
f462fd36 | 467 | get_next: |
771b53d0 JA |
468 | /* |
469 | * If we got some work, mark us as busy. If we didn't, but | |
470 | * the list isn't empty, it means we stalled on hashed work. | |
471 | * Mark us stalled so we don't keep looking for work when we | |
472 | * can't make progress, any work completion or insertion will | |
473 | * clear the stalled flag. | |
474 | */ | |
60cf46ae | 475 | work = io_get_next_work(wqe); |
771b53d0 JA |
476 | if (work) |
477 | __io_worker_busy(wqe, worker, work); | |
6206f0e1 | 478 | else if (!wq_list_empty(&wqe->work_list)) |
771b53d0 JA |
479 | wqe->flags |= IO_WQE_FLAG_STALLED; |
480 | ||
95da8465 | 481 | raw_spin_unlock_irq(&wqe->lock); |
771b53d0 JA |
482 | if (!work) |
483 | break; | |
58e39319 | 484 | io_assign_current_work(worker, work); |
e941894e | 485 | __set_current_state(TASK_RUNNING); |
36c2f922 | 486 | |
dc026a73 PB |
487 | /* handle a whole dependent link */ |
488 | do { | |
5280f7e5 | 489 | struct io_wq_work *next_hashed, *linked; |
b089ed39 | 490 | unsigned int hash = io_get_work_hash(work); |
dc026a73 | 491 | |
86f3cd1b | 492 | next_hashed = wq_next_work(work); |
c60eb049 PB |
493 | |
494 | if (unlikely(do_kill) && (work->flags & IO_WQ_WORK_UNBOUND)) | |
495 | work->flags |= IO_WQ_WORK_CANCEL; | |
5280f7e5 PB |
496 | wq->do_work(work); |
497 | io_assign_current_work(worker, NULL); | |
dc026a73 | 498 | |
5280f7e5 | 499 | linked = wq->free_work(work); |
86f3cd1b PB |
500 | work = next_hashed; |
501 | if (!work && linked && !io_wq_is_hashed(linked)) { | |
502 | work = linked; | |
503 | linked = NULL; | |
504 | } | |
505 | io_assign_current_work(worker, work); | |
86f3cd1b PB |
506 | if (linked) |
507 | io_wqe_enqueue(wqe, linked); | |
508 | ||
509 | if (hash != -1U && !next_hashed) { | |
e941894e JA |
510 | clear_bit(hash, &wq->hash->map); |
511 | if (wq_has_sleeper(&wq->hash->wait)) | |
512 | wake_up(&wq->hash->wait); | |
95da8465 | 513 | raw_spin_lock_irq(&wqe->lock); |
dc026a73 | 514 | wqe->flags &= ~IO_WQE_FLAG_STALLED; |
f462fd36 PB |
515 | /* skip unnecessary unlock-lock wqe->lock */ |
516 | if (!work) | |
517 | goto get_next; | |
95da8465 | 518 | raw_spin_unlock_irq(&wqe->lock); |
7d723065 | 519 | } |
58e39319 | 520 | } while (work); |
7d723065 | 521 | |
95da8465 | 522 | raw_spin_lock_irq(&wqe->lock); |
771b53d0 JA |
523 | } while (1); |
524 | } | |
525 | ||
771b53d0 JA |
526 | static int io_wqe_worker(void *data) |
527 | { | |
528 | struct io_worker *worker = data; | |
529 | struct io_wqe *wqe = worker->wqe; | |
530 | struct io_wq *wq = wqe->wq; | |
46fe18b1 | 531 | char buf[TASK_COMM_LEN]; |
771b53d0 | 532 | |
46fe18b1 | 533 | worker->flags |= (IO_WORKER_F_UP | IO_WORKER_F_RUNNING); |
46fe18b1 | 534 | |
685fe7fe | 535 | snprintf(buf, sizeof(buf), "iou-wrk-%d", wq->task->pid); |
46fe18b1 | 536 | set_task_comm(current, buf); |
771b53d0 JA |
537 | |
538 | while (!test_bit(IO_WQ_BIT_EXIT, &wq->state)) { | |
16efa4fc JA |
539 | long ret; |
540 | ||
506d95ff | 541 | set_current_state(TASK_INTERRUPTIBLE); |
e995d512 | 542 | loop: |
95da8465 | 543 | raw_spin_lock_irq(&wqe->lock); |
771b53d0 | 544 | if (io_wqe_run_queue(wqe)) { |
771b53d0 | 545 | io_worker_handle_work(worker); |
e995d512 | 546 | goto loop; |
771b53d0 | 547 | } |
c6d77d92 | 548 | __io_worker_idle(wqe, worker); |
95da8465 | 549 | raw_spin_unlock_irq(&wqe->lock); |
00ddff43 JA |
550 | if (io_flush_signals()) |
551 | continue; | |
16efa4fc | 552 | ret = schedule_timeout(WORKER_IDLE_TIMEOUT); |
dbe1bdbb JA |
553 | if (signal_pending(current)) { |
554 | struct ksignal ksig; | |
555 | ||
556 | if (!get_signal(&ksig)) | |
557 | continue; | |
3bfe6106 | 558 | break; |
dbe1bdbb JA |
559 | } |
560 | if (ret) | |
561 | continue; | |
771b53d0 JA |
562 | /* timed out, exit unless we're the fixed worker */ |
563 | if (test_bit(IO_WQ_BIT_EXIT, &wq->state) || | |
564 | !(worker->flags & IO_WORKER_F_FIXED)) | |
565 | break; | |
566 | } | |
567 | ||
771b53d0 | 568 | if (test_bit(IO_WQ_BIT_EXIT, &wq->state)) { |
95da8465 | 569 | raw_spin_lock_irq(&wqe->lock); |
6206f0e1 | 570 | if (!wq_list_empty(&wqe->work_list)) |
771b53d0 JA |
571 | io_worker_handle_work(worker); |
572 | else | |
95da8465 | 573 | raw_spin_unlock_irq(&wqe->lock); |
771b53d0 JA |
574 | } |
575 | ||
576 | io_worker_exit(worker); | |
577 | return 0; | |
578 | } | |
579 | ||
771b53d0 JA |
580 | /* |
581 | * Called when a worker is scheduled in. Mark us as currently running. | |
582 | */ | |
583 | void io_wq_worker_running(struct task_struct *tsk) | |
584 | { | |
3bfe6106 | 585 | struct io_worker *worker = tsk->pf_io_worker; |
771b53d0 | 586 | |
3bfe6106 JA |
587 | if (!worker) |
588 | return; | |
771b53d0 JA |
589 | if (!(worker->flags & IO_WORKER_F_UP)) |
590 | return; | |
591 | if (worker->flags & IO_WORKER_F_RUNNING) | |
592 | return; | |
593 | worker->flags |= IO_WORKER_F_RUNNING; | |
958234d5 | 594 | io_wqe_inc_running(worker); |
771b53d0 JA |
595 | } |
596 | ||
597 | /* | |
598 | * Called when worker is going to sleep. If there are no workers currently | |
685fe7fe | 599 | * running and we have work pending, wake up a free one or create a new one. |
771b53d0 JA |
600 | */ |
601 | void io_wq_worker_sleeping(struct task_struct *tsk) | |
602 | { | |
3bfe6106 | 603 | struct io_worker *worker = tsk->pf_io_worker; |
771b53d0 | 604 | |
3bfe6106 JA |
605 | if (!worker) |
606 | return; | |
771b53d0 JA |
607 | if (!(worker->flags & IO_WORKER_F_UP)) |
608 | return; | |
609 | if (!(worker->flags & IO_WORKER_F_RUNNING)) | |
610 | return; | |
611 | ||
612 | worker->flags &= ~IO_WORKER_F_RUNNING; | |
613 | ||
3bfe6106 | 614 | raw_spin_lock_irq(&worker->wqe->lock); |
958234d5 | 615 | io_wqe_dec_running(worker); |
3bfe6106 | 616 | raw_spin_unlock_irq(&worker->wqe->lock); |
771b53d0 JA |
617 | } |
618 | ||
685fe7fe | 619 | static void create_io_worker(struct io_wq *wq, struct io_wqe *wqe, int index) |
3bfe6106 | 620 | { |
46fe18b1 | 621 | struct io_wqe_acct *acct = &wqe->acct[index]; |
3bfe6106 | 622 | struct io_worker *worker; |
46fe18b1 | 623 | struct task_struct *tsk; |
3bfe6106 | 624 | |
8b3e78b5 JA |
625 | __set_current_state(TASK_RUNNING); |
626 | ||
3bfe6106 JA |
627 | worker = kzalloc_node(sizeof(*worker), GFP_KERNEL, wqe->node); |
628 | if (!worker) | |
685fe7fe | 629 | goto fail; |
3bfe6106 JA |
630 | |
631 | refcount_set(&worker->ref, 1); | |
632 | worker->nulls_node.pprev = NULL; | |
633 | worker->wqe = wqe; | |
634 | spin_lock_init(&worker->lock); | |
eb2de941 | 635 | init_completion(&worker->ref_done); |
3bfe6106 | 636 | |
46fe18b1 JA |
637 | tsk = create_io_thread(io_wqe_worker, worker, wqe->node); |
638 | if (IS_ERR(tsk)) { | |
3bfe6106 | 639 | kfree(worker); |
685fe7fe JA |
640 | fail: |
641 | atomic_dec(&acct->nr_running); | |
642 | io_worker_ref_put(wq); | |
643 | return; | |
3bfe6106 | 644 | } |
46fe18b1 JA |
645 | |
646 | tsk->pf_io_worker = worker; | |
647 | worker->task = tsk; | |
648 | set_cpus_allowed_ptr(tsk, cpumask_of_node(wqe->node)); | |
e22bc9b4 | 649 | tsk->flags |= PF_NO_SETAFFINITY; |
46fe18b1 JA |
650 | |
651 | raw_spin_lock_irq(&wqe->lock); | |
652 | hlist_nulls_add_head_rcu(&worker->nulls_node, &wqe->free_list); | |
653 | list_add_tail_rcu(&worker->all_list, &wqe->all_list); | |
654 | worker->flags |= IO_WORKER_F_FREE; | |
655 | if (index == IO_WQ_ACCT_BOUND) | |
656 | worker->flags |= IO_WORKER_F_BOUND; | |
657 | if (!acct->nr_workers && (worker->flags & IO_WORKER_F_BOUND)) | |
658 | worker->flags |= IO_WORKER_F_FIXED; | |
659 | acct->nr_workers++; | |
660 | raw_spin_unlock_irq(&wqe->lock); | |
661 | wake_up_new_task(tsk); | |
771b53d0 JA |
662 | } |
663 | ||
c4068bf8 HD |
664 | /* |
665 | * Iterate the passed in list and call the specific function for each | |
666 | * worker that isn't exiting | |
667 | */ | |
668 | static bool io_wq_for_each_worker(struct io_wqe *wqe, | |
669 | bool (*func)(struct io_worker *, void *), | |
670 | void *data) | |
671 | { | |
672 | struct io_worker *worker; | |
673 | bool ret = false; | |
674 | ||
675 | list_for_each_entry_rcu(worker, &wqe->all_list, all_list) { | |
676 | if (io_worker_get(worker)) { | |
677 | /* no task if node is/was offline */ | |
678 | if (worker->task) | |
679 | ret = func(worker, data); | |
680 | io_worker_release(worker); | |
681 | if (ret) | |
682 | break; | |
683 | } | |
684 | } | |
685 | ||
686 | return ret; | |
687 | } | |
688 | ||
689 | static bool io_wq_worker_wake(struct io_worker *worker, void *data) | |
690 | { | |
46fe18b1 | 691 | set_notify_signal(worker->task); |
c4068bf8 HD |
692 | wake_up_process(worker->task); |
693 | return false; | |
694 | } | |
695 | ||
f0127254 JA |
696 | static bool io_wq_work_match_all(struct io_wq_work *work, void *data) |
697 | { | |
698 | return true; | |
699 | } | |
700 | ||
e9fd9396 | 701 | static void io_run_cancel(struct io_wq_work *work, struct io_wqe *wqe) |
fc04c39b | 702 | { |
e9fd9396 PB |
703 | struct io_wq *wq = wqe->wq; |
704 | ||
fc04c39b | 705 | do { |
fc04c39b | 706 | work->flags |= IO_WQ_WORK_CANCEL; |
5280f7e5 PB |
707 | wq->do_work(work); |
708 | work = wq->free_work(work); | |
fc04c39b PB |
709 | } while (work); |
710 | } | |
711 | ||
86f3cd1b PB |
712 | static void io_wqe_insert_work(struct io_wqe *wqe, struct io_wq_work *work) |
713 | { | |
714 | unsigned int hash; | |
715 | struct io_wq_work *tail; | |
716 | ||
717 | if (!io_wq_is_hashed(work)) { | |
718 | append: | |
719 | wq_list_add_tail(&work->list, &wqe->work_list); | |
720 | return; | |
721 | } | |
722 | ||
723 | hash = io_get_work_hash(work); | |
724 | tail = wqe->hash_tail[hash]; | |
725 | wqe->hash_tail[hash] = work; | |
726 | if (!tail) | |
727 | goto append; | |
728 | ||
729 | wq_list_add_after(&work->list, &tail->list, &wqe->work_list); | |
730 | } | |
731 | ||
771b53d0 JA |
732 | static void io_wqe_enqueue(struct io_wqe *wqe, struct io_wq_work *work) |
733 | { | |
c5def4ab | 734 | struct io_wqe_acct *acct = io_work_get_acct(wqe, work); |
895e2ca0 | 735 | int work_flags; |
771b53d0 JA |
736 | unsigned long flags; |
737 | ||
685fe7fe | 738 | if (test_bit(IO_WQ_BIT_EXIT, &wqe->wq->state)) { |
70e35125 | 739 | io_run_cancel(work, wqe); |
4fb6ac32 JA |
740 | return; |
741 | } | |
742 | ||
895e2ca0 | 743 | work_flags = work->flags; |
95da8465 | 744 | raw_spin_lock_irqsave(&wqe->lock, flags); |
86f3cd1b | 745 | io_wqe_insert_work(wqe, work); |
771b53d0 | 746 | wqe->flags &= ~IO_WQE_FLAG_STALLED; |
95da8465 | 747 | raw_spin_unlock_irqrestore(&wqe->lock, flags); |
771b53d0 | 748 | |
895e2ca0 JA |
749 | if ((work_flags & IO_WQ_WORK_CONCURRENT) || |
750 | !atomic_read(&acct->nr_running)) | |
c5def4ab | 751 | io_wqe_wake_worker(wqe, acct); |
771b53d0 JA |
752 | } |
753 | ||
754 | void io_wq_enqueue(struct io_wq *wq, struct io_wq_work *work) | |
755 | { | |
756 | struct io_wqe *wqe = wq->wqes[numa_node_id()]; | |
757 | ||
758 | io_wqe_enqueue(wqe, work); | |
759 | } | |
760 | ||
761 | /* | |
8766dd51 PB |
762 | * Work items that hash to the same value will not be done in parallel. |
763 | * Used to limit concurrent writes, generally hashed by inode. | |
771b53d0 | 764 | */ |
8766dd51 | 765 | void io_wq_hash_work(struct io_wq_work *work, void *val) |
771b53d0 | 766 | { |
8766dd51 | 767 | unsigned int bit; |
771b53d0 JA |
768 | |
769 | bit = hash_ptr(val, IO_WQ_HASH_ORDER); | |
770 | work->flags |= (IO_WQ_WORK_HASHED | (bit << IO_WQ_HASH_SHIFT)); | |
771b53d0 JA |
771 | } |
772 | ||
2293b419 | 773 | static bool io_wq_worker_cancel(struct io_worker *worker, void *data) |
62755e35 | 774 | { |
2293b419 | 775 | struct io_cb_cancel_data *match = data; |
6f72653e | 776 | unsigned long flags; |
62755e35 JA |
777 | |
778 | /* | |
779 | * Hold the lock to avoid ->cur_work going out of scope, caller | |
36c2f922 | 780 | * may dereference the passed in work. |
62755e35 | 781 | */ |
36c2f922 | 782 | spin_lock_irqsave(&worker->lock, flags); |
62755e35 | 783 | if (worker->cur_work && |
2293b419 | 784 | match->fn(worker->cur_work, match->data)) { |
3bfe6106 | 785 | set_notify_signal(worker->task); |
4f26bda1 | 786 | match->nr_running++; |
771b53d0 | 787 | } |
36c2f922 | 788 | spin_unlock_irqrestore(&worker->lock, flags); |
771b53d0 | 789 | |
4f26bda1 | 790 | return match->nr_running && !match->cancel_all; |
771b53d0 JA |
791 | } |
792 | ||
204361a7 PB |
793 | static inline void io_wqe_remove_pending(struct io_wqe *wqe, |
794 | struct io_wq_work *work, | |
795 | struct io_wq_work_node *prev) | |
796 | { | |
797 | unsigned int hash = io_get_work_hash(work); | |
798 | struct io_wq_work *prev_work = NULL; | |
799 | ||
800 | if (io_wq_is_hashed(work) && work == wqe->hash_tail[hash]) { | |
801 | if (prev) | |
802 | prev_work = container_of(prev, struct io_wq_work, list); | |
803 | if (prev_work && io_get_work_hash(prev_work) == hash) | |
804 | wqe->hash_tail[hash] = prev_work; | |
805 | else | |
806 | wqe->hash_tail[hash] = NULL; | |
807 | } | |
808 | wq_list_del(&wqe->work_list, &work->list, prev); | |
809 | } | |
810 | ||
4f26bda1 | 811 | static void io_wqe_cancel_pending_work(struct io_wqe *wqe, |
f4c2665e | 812 | struct io_cb_cancel_data *match) |
771b53d0 | 813 | { |
6206f0e1 | 814 | struct io_wq_work_node *node, *prev; |
771b53d0 | 815 | struct io_wq_work *work; |
6f72653e | 816 | unsigned long flags; |
771b53d0 | 817 | |
4f26bda1 | 818 | retry: |
95da8465 | 819 | raw_spin_lock_irqsave(&wqe->lock, flags); |
6206f0e1 JA |
820 | wq_list_for_each(node, prev, &wqe->work_list) { |
821 | work = container_of(node, struct io_wq_work, list); | |
4f26bda1 PB |
822 | if (!match->fn(work, match->data)) |
823 | continue; | |
204361a7 | 824 | io_wqe_remove_pending(wqe, work, prev); |
95da8465 | 825 | raw_spin_unlock_irqrestore(&wqe->lock, flags); |
4f26bda1 PB |
826 | io_run_cancel(work, wqe); |
827 | match->nr_pending++; | |
828 | if (!match->cancel_all) | |
829 | return; | |
830 | ||
831 | /* not safe to continue after unlock */ | |
832 | goto retry; | |
771b53d0 | 833 | } |
95da8465 | 834 | raw_spin_unlock_irqrestore(&wqe->lock, flags); |
f4c2665e PB |
835 | } |
836 | ||
4f26bda1 | 837 | static void io_wqe_cancel_running_work(struct io_wqe *wqe, |
f4c2665e PB |
838 | struct io_cb_cancel_data *match) |
839 | { | |
771b53d0 | 840 | rcu_read_lock(); |
4f26bda1 | 841 | io_wq_for_each_worker(wqe, io_wq_worker_cancel, match); |
771b53d0 | 842 | rcu_read_unlock(); |
771b53d0 JA |
843 | } |
844 | ||
2293b419 | 845 | enum io_wq_cancel io_wq_cancel_cb(struct io_wq *wq, work_cancel_fn *cancel, |
4f26bda1 | 846 | void *data, bool cancel_all) |
771b53d0 | 847 | { |
2293b419 | 848 | struct io_cb_cancel_data match = { |
4f26bda1 PB |
849 | .fn = cancel, |
850 | .data = data, | |
851 | .cancel_all = cancel_all, | |
00bcda13 | 852 | }; |
3fc50ab5 | 853 | int node; |
771b53d0 | 854 | |
f4c2665e PB |
855 | /* |
856 | * First check pending list, if we're lucky we can just remove it | |
857 | * from there. CANCEL_OK means that the work is returned as-new, | |
858 | * no completion will be posted for it. | |
859 | */ | |
3fc50ab5 JH |
860 | for_each_node(node) { |
861 | struct io_wqe *wqe = wq->wqes[node]; | |
771b53d0 | 862 | |
4f26bda1 PB |
863 | io_wqe_cancel_pending_work(wqe, &match); |
864 | if (match.nr_pending && !match.cancel_all) | |
f4c2665e | 865 | return IO_WQ_CANCEL_OK; |
771b53d0 JA |
866 | } |
867 | ||
f4c2665e PB |
868 | /* |
869 | * Now check if a free (going busy) or busy worker has the work | |
870 | * currently running. If we find it there, we'll return CANCEL_RUNNING | |
871 | * as an indication that we attempt to signal cancellation. The | |
872 | * completion will run normally in this case. | |
873 | */ | |
874 | for_each_node(node) { | |
875 | struct io_wqe *wqe = wq->wqes[node]; | |
876 | ||
4f26bda1 PB |
877 | io_wqe_cancel_running_work(wqe, &match); |
878 | if (match.nr_running && !match.cancel_all) | |
f4c2665e PB |
879 | return IO_WQ_CANCEL_RUNNING; |
880 | } | |
881 | ||
4f26bda1 PB |
882 | if (match.nr_running) |
883 | return IO_WQ_CANCEL_RUNNING; | |
884 | if (match.nr_pending) | |
885 | return IO_WQ_CANCEL_OK; | |
f4c2665e | 886 | return IO_WQ_CANCEL_NOTFOUND; |
771b53d0 JA |
887 | } |
888 | ||
e941894e JA |
889 | static int io_wqe_hash_wake(struct wait_queue_entry *wait, unsigned mode, |
890 | int sync, void *key) | |
891 | { | |
892 | struct io_wqe *wqe = container_of(wait, struct io_wqe, wait); | |
e941894e JA |
893 | |
894 | list_del_init(&wait->entry); | |
895 | ||
896 | rcu_read_lock(); | |
685fe7fe | 897 | io_wqe_activate_free_worker(wqe); |
e941894e | 898 | rcu_read_unlock(); |
e941894e JA |
899 | return 1; |
900 | } | |
901 | ||
576a347b | 902 | struct io_wq *io_wq_create(unsigned bounded, struct io_wq_data *data) |
771b53d0 | 903 | { |
3fc50ab5 | 904 | int ret = -ENOMEM, node; |
771b53d0 JA |
905 | struct io_wq *wq; |
906 | ||
f5fa38c5 | 907 | if (WARN_ON_ONCE(!data->free_work || !data->do_work)) |
e9fd9396 PB |
908 | return ERR_PTR(-EINVAL); |
909 | ||
ad6e005c | 910 | wq = kzalloc(sizeof(*wq), GFP_KERNEL); |
771b53d0 JA |
911 | if (!wq) |
912 | return ERR_PTR(-ENOMEM); | |
913 | ||
3fc50ab5 | 914 | wq->wqes = kcalloc(nr_node_ids, sizeof(struct io_wqe *), GFP_KERNEL); |
43c01fbe JA |
915 | if (!wq->wqes) |
916 | goto err_wq; | |
917 | ||
918 | ret = cpuhp_state_add_instance_nocalls(io_wq_online, &wq->cpuhp_node); | |
919 | if (ret) | |
920 | goto err_wqes; | |
771b53d0 | 921 | |
e941894e JA |
922 | refcount_inc(&data->hash->refs); |
923 | wq->hash = data->hash; | |
e9fd9396 | 924 | wq->free_work = data->free_work; |
f5fa38c5 | 925 | wq->do_work = data->do_work; |
7d723065 | 926 | |
43c01fbe | 927 | ret = -ENOMEM; |
3fc50ab5 | 928 | for_each_node(node) { |
771b53d0 | 929 | struct io_wqe *wqe; |
7563439a | 930 | int alloc_node = node; |
771b53d0 | 931 | |
7563439a JA |
932 | if (!node_online(alloc_node)) |
933 | alloc_node = NUMA_NO_NODE; | |
934 | wqe = kzalloc_node(sizeof(struct io_wqe), GFP_KERNEL, alloc_node); | |
771b53d0 | 935 | if (!wqe) |
3fc50ab5 JH |
936 | goto err; |
937 | wq->wqes[node] = wqe; | |
7563439a | 938 | wqe->node = alloc_node; |
685fe7fe JA |
939 | wqe->acct[IO_WQ_ACCT_BOUND].index = IO_WQ_ACCT_BOUND; |
940 | wqe->acct[IO_WQ_ACCT_UNBOUND].index = IO_WQ_ACCT_UNBOUND; | |
c5def4ab JA |
941 | wqe->acct[IO_WQ_ACCT_BOUND].max_workers = bounded; |
942 | atomic_set(&wqe->acct[IO_WQ_ACCT_BOUND].nr_running, 0); | |
728f13e7 | 943 | wqe->acct[IO_WQ_ACCT_UNBOUND].max_workers = |
c5def4ab | 944 | task_rlimit(current, RLIMIT_NPROC); |
c5def4ab | 945 | atomic_set(&wqe->acct[IO_WQ_ACCT_UNBOUND].nr_running, 0); |
e941894e JA |
946 | wqe->wait.func = io_wqe_hash_wake; |
947 | INIT_LIST_HEAD(&wqe->wait.entry); | |
771b53d0 | 948 | wqe->wq = wq; |
95da8465 | 949 | raw_spin_lock_init(&wqe->lock); |
6206f0e1 | 950 | INIT_WQ_LIST(&wqe->work_list); |
021d1cdd | 951 | INIT_HLIST_NULLS_HEAD(&wqe->free_list, 0); |
e61df66c | 952 | INIT_LIST_HEAD(&wqe->all_list); |
771b53d0 JA |
953 | } |
954 | ||
685fe7fe | 955 | wq->task = get_task_struct(data->task); |
3bfe6106 | 956 | refcount_set(&wq->refs, 1); |
685fe7fe JA |
957 | atomic_set(&wq->worker_refs, 1); |
958 | init_completion(&wq->worker_done); | |
959 | return wq; | |
b60fda60 | 960 | err: |
dc7bbc9e | 961 | io_wq_put_hash(data->hash); |
43c01fbe | 962 | cpuhp_state_remove_instance_nocalls(io_wq_online, &wq->cpuhp_node); |
3fc50ab5 JH |
963 | for_each_node(node) |
964 | kfree(wq->wqes[node]); | |
43c01fbe | 965 | err_wqes: |
b60fda60 | 966 | kfree(wq->wqes); |
43c01fbe | 967 | err_wq: |
b60fda60 | 968 | kfree(wq); |
771b53d0 JA |
969 | return ERR_PTR(ret); |
970 | } | |
971 | ||
c80ca470 JA |
972 | static bool io_task_work_match(struct callback_head *cb, void *data) |
973 | { | |
974 | struct create_worker_data *cwd; | |
975 | ||
976 | if (cb->func != create_worker_cb) | |
977 | return false; | |
978 | cwd = container_of(cb, struct create_worker_data, work); | |
979 | return cwd->wqe->wq == data; | |
980 | } | |
981 | ||
685fe7fe | 982 | static void io_wq_exit_workers(struct io_wq *wq) |
afcc4015 | 983 | { |
685fe7fe JA |
984 | struct callback_head *cb; |
985 | int node; | |
986 | ||
987 | set_bit(IO_WQ_BIT_EXIT, &wq->state); | |
988 | ||
989 | if (!wq->task) | |
990 | return; | |
991 | ||
c80ca470 | 992 | while ((cb = task_work_cancel_match(wq->task, io_task_work_match, wq)) != NULL) { |
685fe7fe JA |
993 | struct create_worker_data *cwd; |
994 | ||
995 | cwd = container_of(cb, struct create_worker_data, work); | |
996 | atomic_dec(&cwd->wqe->acct[cwd->index].nr_running); | |
997 | io_worker_ref_put(wq); | |
998 | kfree(cwd); | |
999 | } | |
1000 | ||
1001 | rcu_read_lock(); | |
1002 | for_each_node(node) { | |
1003 | struct io_wqe *wqe = wq->wqes[node]; | |
1004 | ||
1005 | io_wq_for_each_worker(wqe, io_wq_worker_wake, NULL); | |
1006 | spin_lock_irq(&wq->hash->wait.lock); | |
1007 | list_del_init(&wq->wqes[node]->wait.entry); | |
1008 | spin_unlock_irq(&wq->hash->wait.lock); | |
afcc4015 | 1009 | } |
685fe7fe JA |
1010 | rcu_read_unlock(); |
1011 | io_worker_ref_put(wq); | |
1012 | wait_for_completion(&wq->worker_done); | |
1013 | put_task_struct(wq->task); | |
1014 | wq->task = NULL; | |
afcc4015 JA |
1015 | } |
1016 | ||
4fb6ac32 | 1017 | static void io_wq_destroy(struct io_wq *wq) |
771b53d0 | 1018 | { |
3fc50ab5 | 1019 | int node; |
771b53d0 | 1020 | |
43c01fbe JA |
1021 | cpuhp_state_remove_instance_nocalls(io_wq_online, &wq->cpuhp_node); |
1022 | ||
685fe7fe | 1023 | io_wq_exit_workers(wq); |
771b53d0 | 1024 | |
e941894e JA |
1025 | for_each_node(node) { |
1026 | struct io_wqe *wqe = wq->wqes[node]; | |
f5d2d23b JA |
1027 | struct io_cb_cancel_data match = { |
1028 | .fn = io_wq_work_match_all, | |
1029 | .cancel_all = true, | |
1030 | }; | |
1031 | io_wqe_cancel_pending_work(wqe, &match); | |
e941894e JA |
1032 | kfree(wqe); |
1033 | } | |
e941894e | 1034 | io_wq_put_hash(wq->hash); |
771b53d0 JA |
1035 | kfree(wq->wqes); |
1036 | kfree(wq); | |
4fb6ac32 JA |
1037 | } |
1038 | ||
1039 | void io_wq_put(struct io_wq *wq) | |
1040 | { | |
1041 | if (refcount_dec_and_test(&wq->refs)) | |
1042 | io_wq_destroy(wq); | |
771b53d0 | 1043 | } |
848f7e18 | 1044 | |
afcc4015 JA |
1045 | void io_wq_put_and_exit(struct io_wq *wq) |
1046 | { | |
685fe7fe | 1047 | io_wq_exit_workers(wq); |
afcc4015 JA |
1048 | io_wq_put(wq); |
1049 | } | |
1050 | ||
43c01fbe JA |
1051 | static bool io_wq_worker_affinity(struct io_worker *worker, void *data) |
1052 | { | |
e0051d7d PZ |
1053 | set_cpus_allowed_ptr(worker->task, cpumask_of_node(worker->wqe->node)); |
1054 | ||
43c01fbe JA |
1055 | return false; |
1056 | } | |
1057 | ||
1058 | static int io_wq_cpu_online(unsigned int cpu, struct hlist_node *node) | |
1059 | { | |
1060 | struct io_wq *wq = hlist_entry_safe(node, struct io_wq, cpuhp_node); | |
1061 | int i; | |
1062 | ||
1063 | rcu_read_lock(); | |
1064 | for_each_node(i) | |
1065 | io_wq_for_each_worker(wq->wqes[i], io_wq_worker_affinity, NULL); | |
1066 | rcu_read_unlock(); | |
1067 | return 0; | |
1068 | } | |
1069 | ||
1070 | static __init int io_wq_init(void) | |
1071 | { | |
1072 | int ret; | |
1073 | ||
1074 | ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "io-wq/online", | |
1075 | io_wq_cpu_online, NULL); | |
1076 | if (ret < 0) | |
1077 | return ret; | |
1078 | io_wq_online = ret; | |
1079 | return 0; | |
1080 | } | |
1081 | subsys_initcall(io_wq_init); |