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