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