]>
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 | 55 | struct completion ref_done; |
65d43023 | 56 | struct completion started; |
eb2de941 | 57 | |
771b53d0 | 58 | struct rcu_head rcu; |
771b53d0 JA |
59 | }; |
60 | ||
771b53d0 JA |
61 | #if BITS_PER_LONG == 64 |
62 | #define IO_WQ_HASH_ORDER 6 | |
63 | #else | |
64 | #define IO_WQ_HASH_ORDER 5 | |
65 | #endif | |
66 | ||
86f3cd1b PB |
67 | #define IO_WQ_NR_HASH_BUCKETS (1u << IO_WQ_HASH_ORDER) |
68 | ||
c5def4ab JA |
69 | struct io_wqe_acct { |
70 | unsigned nr_workers; | |
71 | unsigned max_workers; | |
72 | atomic_t nr_running; | |
73 | }; | |
74 | ||
75 | enum { | |
76 | IO_WQ_ACCT_BOUND, | |
77 | IO_WQ_ACCT_UNBOUND, | |
78 | }; | |
79 | ||
771b53d0 JA |
80 | /* |
81 | * Per-node worker thread pool | |
82 | */ | |
83 | struct io_wqe { | |
84 | struct { | |
95da8465 | 85 | raw_spinlock_t lock; |
6206f0e1 | 86 | struct io_wq_work_list work_list; |
771b53d0 JA |
87 | unsigned flags; |
88 | } ____cacheline_aligned_in_smp; | |
89 | ||
90 | int node; | |
c5def4ab | 91 | struct io_wqe_acct acct[2]; |
771b53d0 | 92 | |
021d1cdd | 93 | struct hlist_nulls_head free_list; |
e61df66c | 94 | struct list_head all_list; |
771b53d0 | 95 | |
e941894e JA |
96 | struct wait_queue_entry wait; |
97 | ||
771b53d0 | 98 | struct io_wq *wq; |
86f3cd1b | 99 | struct io_wq_work *hash_tail[IO_WQ_NR_HASH_BUCKETS]; |
771b53d0 JA |
100 | }; |
101 | ||
102 | /* | |
103 | * Per io_wq state | |
104 | */ | |
105 | struct io_wq { | |
106 | struct io_wqe **wqes; | |
107 | unsigned long state; | |
771b53d0 | 108 | |
e9fd9396 | 109 | free_work_fn *free_work; |
f5fa38c5 | 110 | io_wq_work_fn *do_work; |
7d723065 | 111 | |
771b53d0 | 112 | struct task_struct *manager; |
c5def4ab | 113 | struct user_struct *user; |
e941894e JA |
114 | |
115 | struct io_wq_hash *hash; | |
116 | ||
771b53d0 | 117 | refcount_t refs; |
dbf99620 | 118 | struct completion started; |
d364d9e5 | 119 | struct completion exited; |
848f7e18 | 120 | |
fb3a1f6c JA |
121 | atomic_t worker_refs; |
122 | struct completion worker_done; | |
123 | ||
43c01fbe | 124 | struct hlist_node cpuhp_node; |
3bfe6106 JA |
125 | |
126 | pid_t task_pid; | |
771b53d0 JA |
127 | }; |
128 | ||
43c01fbe JA |
129 | static enum cpuhp_state io_wq_online; |
130 | ||
771b53d0 JA |
131 | static bool io_worker_get(struct io_worker *worker) |
132 | { | |
133 | return refcount_inc_not_zero(&worker->ref); | |
134 | } | |
135 | ||
136 | static void io_worker_release(struct io_worker *worker) | |
137 | { | |
138 | if (refcount_dec_and_test(&worker->ref)) | |
eb2de941 | 139 | complete(&worker->ref_done); |
771b53d0 JA |
140 | } |
141 | ||
c5def4ab JA |
142 | static inline struct io_wqe_acct *io_work_get_acct(struct io_wqe *wqe, |
143 | struct io_wq_work *work) | |
144 | { | |
145 | if (work->flags & IO_WQ_WORK_UNBOUND) | |
146 | return &wqe->acct[IO_WQ_ACCT_UNBOUND]; | |
147 | ||
148 | return &wqe->acct[IO_WQ_ACCT_BOUND]; | |
149 | } | |
150 | ||
958234d5 | 151 | static inline struct io_wqe_acct *io_wqe_get_acct(struct io_worker *worker) |
c5def4ab | 152 | { |
958234d5 JA |
153 | struct io_wqe *wqe = worker->wqe; |
154 | ||
c5def4ab JA |
155 | if (worker->flags & IO_WORKER_F_BOUND) |
156 | return &wqe->acct[IO_WQ_ACCT_BOUND]; | |
157 | ||
158 | return &wqe->acct[IO_WQ_ACCT_UNBOUND]; | |
159 | } | |
160 | ||
771b53d0 JA |
161 | static void io_worker_exit(struct io_worker *worker) |
162 | { | |
163 | struct io_wqe *wqe = worker->wqe; | |
958234d5 | 164 | struct io_wqe_acct *acct = io_wqe_get_acct(worker); |
bf1daa4b | 165 | unsigned flags; |
771b53d0 | 166 | |
eb2de941 JA |
167 | if (refcount_dec_and_test(&worker->ref)) |
168 | complete(&worker->ref_done); | |
169 | wait_for_completion(&worker->ref_done); | |
771b53d0 JA |
170 | |
171 | preempt_disable(); | |
172 | current->flags &= ~PF_IO_WORKER; | |
bf1daa4b JA |
173 | flags = worker->flags; |
174 | worker->flags = 0; | |
175 | if (flags & IO_WORKER_F_RUNNING) | |
c5def4ab | 176 | atomic_dec(&acct->nr_running); |
771b53d0 JA |
177 | worker->flags = 0; |
178 | preempt_enable(); | |
179 | ||
95da8465 | 180 | raw_spin_lock_irq(&wqe->lock); |
bf1daa4b JA |
181 | if (flags & IO_WORKER_F_FREE) |
182 | hlist_nulls_del_rcu(&worker->nulls_node); | |
e61df66c | 183 | list_del_rcu(&worker->all_list); |
c5def4ab | 184 | acct->nr_workers--; |
95da8465 | 185 | raw_spin_unlock_irq(&wqe->lock); |
771b53d0 | 186 | |
364b05fd | 187 | kfree_rcu(worker, rcu); |
fb3a1f6c JA |
188 | if (atomic_dec_and_test(&wqe->wq->worker_refs)) |
189 | complete(&wqe->wq->worker_done); | |
771b53d0 JA |
190 | } |
191 | ||
c5def4ab JA |
192 | static inline bool io_wqe_run_queue(struct io_wqe *wqe) |
193 | __must_hold(wqe->lock) | |
194 | { | |
6206f0e1 JA |
195 | if (!wq_list_empty(&wqe->work_list) && |
196 | !(wqe->flags & IO_WQE_FLAG_STALLED)) | |
c5def4ab JA |
197 | return true; |
198 | return false; | |
199 | } | |
200 | ||
201 | /* | |
202 | * Check head of free list for an available worker. If one isn't available, | |
203 | * caller must wake up the wq manager to create one. | |
204 | */ | |
205 | static bool io_wqe_activate_free_worker(struct io_wqe *wqe) | |
206 | __must_hold(RCU) | |
207 | { | |
208 | struct hlist_nulls_node *n; | |
209 | struct io_worker *worker; | |
210 | ||
021d1cdd | 211 | n = rcu_dereference(hlist_nulls_first_rcu(&wqe->free_list)); |
c5def4ab JA |
212 | if (is_a_nulls(n)) |
213 | return false; | |
214 | ||
215 | worker = hlist_nulls_entry(n, struct io_worker, nulls_node); | |
216 | if (io_worker_get(worker)) { | |
506d95ff | 217 | wake_up_process(worker->task); |
c5def4ab JA |
218 | io_worker_release(worker); |
219 | return true; | |
220 | } | |
221 | ||
222 | return false; | |
223 | } | |
224 | ||
225 | /* | |
226 | * We need a worker. If we find a free one, we're good. If not, and we're | |
227 | * below the max number of workers, wake up the manager to create one. | |
228 | */ | |
229 | static void io_wqe_wake_worker(struct io_wqe *wqe, struct io_wqe_acct *acct) | |
230 | { | |
231 | bool ret; | |
232 | ||
233 | /* | |
234 | * Most likely an attempt to queue unbounded work on an io_wq that | |
235 | * wasn't setup with any unbounded workers. | |
236 | */ | |
237 | WARN_ON_ONCE(!acct->max_workers); | |
238 | ||
239 | rcu_read_lock(); | |
240 | ret = io_wqe_activate_free_worker(wqe); | |
241 | rcu_read_unlock(); | |
242 | ||
243 | if (!ret && acct->nr_workers < acct->max_workers) | |
244 | wake_up_process(wqe->wq->manager); | |
245 | } | |
246 | ||
958234d5 | 247 | static void io_wqe_inc_running(struct io_worker *worker) |
c5def4ab | 248 | { |
958234d5 | 249 | struct io_wqe_acct *acct = io_wqe_get_acct(worker); |
c5def4ab JA |
250 | |
251 | atomic_inc(&acct->nr_running); | |
252 | } | |
253 | ||
958234d5 | 254 | static void io_wqe_dec_running(struct io_worker *worker) |
c5def4ab JA |
255 | __must_hold(wqe->lock) |
256 | { | |
958234d5 JA |
257 | struct io_wqe_acct *acct = io_wqe_get_acct(worker); |
258 | struct io_wqe *wqe = worker->wqe; | |
c5def4ab JA |
259 | |
260 | if (atomic_dec_and_test(&acct->nr_running) && io_wqe_run_queue(wqe)) | |
261 | io_wqe_wake_worker(wqe, acct); | |
262 | } | |
263 | ||
958234d5 | 264 | static void io_worker_start(struct io_worker *worker) |
771b53d0 | 265 | { |
771b53d0 | 266 | worker->flags |= (IO_WORKER_F_UP | IO_WORKER_F_RUNNING); |
958234d5 | 267 | io_wqe_inc_running(worker); |
65d43023 | 268 | complete(&worker->started); |
771b53d0 JA |
269 | } |
270 | ||
271 | /* | |
272 | * Worker will start processing some work. Move it to the busy list, if | |
273 | * it's currently on the freelist | |
274 | */ | |
275 | static void __io_worker_busy(struct io_wqe *wqe, struct io_worker *worker, | |
276 | struct io_wq_work *work) | |
277 | __must_hold(wqe->lock) | |
278 | { | |
c5def4ab JA |
279 | bool worker_bound, work_bound; |
280 | ||
771b53d0 JA |
281 | if (worker->flags & IO_WORKER_F_FREE) { |
282 | worker->flags &= ~IO_WORKER_F_FREE; | |
283 | hlist_nulls_del_init_rcu(&worker->nulls_node); | |
771b53d0 | 284 | } |
c5def4ab JA |
285 | |
286 | /* | |
287 | * If worker is moving from bound to unbound (or vice versa), then | |
288 | * ensure we update the running accounting. | |
289 | */ | |
b2e9c7d6 DC |
290 | worker_bound = (worker->flags & IO_WORKER_F_BOUND) != 0; |
291 | work_bound = (work->flags & IO_WQ_WORK_UNBOUND) == 0; | |
292 | if (worker_bound != work_bound) { | |
958234d5 | 293 | io_wqe_dec_running(worker); |
c5def4ab JA |
294 | if (work_bound) { |
295 | worker->flags |= IO_WORKER_F_BOUND; | |
296 | wqe->acct[IO_WQ_ACCT_UNBOUND].nr_workers--; | |
297 | wqe->acct[IO_WQ_ACCT_BOUND].nr_workers++; | |
c5def4ab JA |
298 | } else { |
299 | worker->flags &= ~IO_WORKER_F_BOUND; | |
300 | wqe->acct[IO_WQ_ACCT_UNBOUND].nr_workers++; | |
301 | wqe->acct[IO_WQ_ACCT_BOUND].nr_workers--; | |
c5def4ab | 302 | } |
958234d5 | 303 | io_wqe_inc_running(worker); |
c5def4ab | 304 | } |
771b53d0 JA |
305 | } |
306 | ||
307 | /* | |
308 | * No work, worker going to sleep. Move to freelist, and unuse mm if we | |
309 | * have one attached. Dropping the mm may potentially sleep, so we drop | |
310 | * the lock in that case and return success. Since the caller has to | |
311 | * retry the loop in that case (we changed task state), we don't regrab | |
312 | * the lock if we return success. | |
313 | */ | |
c6d77d92 | 314 | static void __io_worker_idle(struct io_wqe *wqe, struct io_worker *worker) |
771b53d0 JA |
315 | __must_hold(wqe->lock) |
316 | { | |
317 | if (!(worker->flags & IO_WORKER_F_FREE)) { | |
318 | worker->flags |= IO_WORKER_F_FREE; | |
021d1cdd | 319 | hlist_nulls_add_head_rcu(&worker->nulls_node, &wqe->free_list); |
771b53d0 | 320 | } |
771b53d0 JA |
321 | } |
322 | ||
60cf46ae PB |
323 | static inline unsigned int io_get_work_hash(struct io_wq_work *work) |
324 | { | |
325 | return work->flags >> IO_WQ_HASH_SHIFT; | |
326 | } | |
327 | ||
e941894e JA |
328 | static void io_wait_on_hash(struct io_wqe *wqe, unsigned int hash) |
329 | { | |
330 | struct io_wq *wq = wqe->wq; | |
331 | ||
332 | spin_lock(&wq->hash->wait.lock); | |
333 | if (list_empty(&wqe->wait.entry)) { | |
334 | __add_wait_queue(&wq->hash->wait, &wqe->wait); | |
335 | if (!test_bit(hash, &wq->hash->map)) { | |
336 | __set_current_state(TASK_RUNNING); | |
337 | list_del_init(&wqe->wait.entry); | |
338 | } | |
339 | } | |
340 | spin_unlock(&wq->hash->wait.lock); | |
341 | } | |
342 | ||
60cf46ae | 343 | static struct io_wq_work *io_get_next_work(struct io_wqe *wqe) |
771b53d0 JA |
344 | __must_hold(wqe->lock) |
345 | { | |
6206f0e1 | 346 | struct io_wq_work_node *node, *prev; |
86f3cd1b | 347 | struct io_wq_work *work, *tail; |
e941894e | 348 | unsigned int stall_hash = -1U; |
771b53d0 | 349 | |
6206f0e1 | 350 | wq_list_for_each(node, prev, &wqe->work_list) { |
e941894e JA |
351 | unsigned int hash; |
352 | ||
6206f0e1 JA |
353 | work = container_of(node, struct io_wq_work, list); |
354 | ||
771b53d0 | 355 | /* not hashed, can run anytime */ |
8766dd51 | 356 | if (!io_wq_is_hashed(work)) { |
86f3cd1b | 357 | wq_list_del(&wqe->work_list, node, prev); |
771b53d0 JA |
358 | return work; |
359 | } | |
360 | ||
60cf46ae | 361 | hash = io_get_work_hash(work); |
e941894e JA |
362 | /* all items with this hash lie in [work, tail] */ |
363 | tail = wqe->hash_tail[hash]; | |
364 | ||
365 | /* hashed, can run if not already running */ | |
366 | if (!test_and_set_bit(hash, &wqe->wq->hash->map)) { | |
86f3cd1b PB |
367 | wqe->hash_tail[hash] = NULL; |
368 | wq_list_cut(&wqe->work_list, &tail->list, prev); | |
771b53d0 JA |
369 | return work; |
370 | } | |
e941894e JA |
371 | if (stall_hash == -1U) |
372 | stall_hash = hash; | |
373 | /* fast forward to a next hash, for-each will fix up @prev */ | |
374 | node = &tail->list; | |
375 | } | |
376 | ||
377 | if (stall_hash != -1U) { | |
378 | raw_spin_unlock(&wqe->lock); | |
379 | io_wait_on_hash(wqe, stall_hash); | |
380 | raw_spin_lock(&wqe->lock); | |
771b53d0 JA |
381 | } |
382 | ||
383 | return NULL; | |
384 | } | |
385 | ||
3bfe6106 | 386 | static void io_flush_signals(void) |
cccf0ee8 | 387 | { |
3bfe6106 JA |
388 | if (unlikely(test_tsk_thread_flag(current, TIF_NOTIFY_SIGNAL))) { |
389 | if (current->task_works) | |
390 | task_work_run(); | |
391 | clear_tsk_thread_flag(current, TIF_NOTIFY_SIGNAL); | |
cccf0ee8 | 392 | } |
dc026a73 PB |
393 | } |
394 | ||
395 | static void io_assign_current_work(struct io_worker *worker, | |
396 | struct io_wq_work *work) | |
397 | { | |
d78298e7 | 398 | if (work) { |
3bfe6106 | 399 | io_flush_signals(); |
d78298e7 PB |
400 | cond_resched(); |
401 | } | |
dc026a73 PB |
402 | |
403 | spin_lock_irq(&worker->lock); | |
404 | worker->cur_work = work; | |
405 | spin_unlock_irq(&worker->lock); | |
406 | } | |
407 | ||
60cf46ae PB |
408 | static void io_wqe_enqueue(struct io_wqe *wqe, struct io_wq_work *work); |
409 | ||
771b53d0 JA |
410 | static void io_worker_handle_work(struct io_worker *worker) |
411 | __releases(wqe->lock) | |
412 | { | |
771b53d0 JA |
413 | struct io_wqe *wqe = worker->wqe; |
414 | struct io_wq *wq = wqe->wq; | |
415 | ||
416 | do { | |
86f3cd1b | 417 | struct io_wq_work *work; |
f462fd36 | 418 | get_next: |
771b53d0 JA |
419 | /* |
420 | * If we got some work, mark us as busy. If we didn't, but | |
421 | * the list isn't empty, it means we stalled on hashed work. | |
422 | * Mark us stalled so we don't keep looking for work when we | |
423 | * can't make progress, any work completion or insertion will | |
424 | * clear the stalled flag. | |
425 | */ | |
60cf46ae | 426 | work = io_get_next_work(wqe); |
771b53d0 JA |
427 | if (work) |
428 | __io_worker_busy(wqe, worker, work); | |
6206f0e1 | 429 | else if (!wq_list_empty(&wqe->work_list)) |
771b53d0 JA |
430 | wqe->flags |= IO_WQE_FLAG_STALLED; |
431 | ||
95da8465 | 432 | raw_spin_unlock_irq(&wqe->lock); |
771b53d0 JA |
433 | if (!work) |
434 | break; | |
58e39319 | 435 | io_assign_current_work(worker, work); |
e941894e | 436 | __set_current_state(TASK_RUNNING); |
36c2f922 | 437 | |
dc026a73 PB |
438 | /* handle a whole dependent link */ |
439 | do { | |
5280f7e5 | 440 | struct io_wq_work *next_hashed, *linked; |
b089ed39 | 441 | unsigned int hash = io_get_work_hash(work); |
dc026a73 | 442 | |
86f3cd1b | 443 | next_hashed = wq_next_work(work); |
5280f7e5 PB |
444 | wq->do_work(work); |
445 | io_assign_current_work(worker, NULL); | |
dc026a73 | 446 | |
5280f7e5 | 447 | linked = wq->free_work(work); |
86f3cd1b PB |
448 | work = next_hashed; |
449 | if (!work && linked && !io_wq_is_hashed(linked)) { | |
450 | work = linked; | |
451 | linked = NULL; | |
452 | } | |
453 | io_assign_current_work(worker, work); | |
86f3cd1b PB |
454 | if (linked) |
455 | io_wqe_enqueue(wqe, linked); | |
456 | ||
457 | if (hash != -1U && !next_hashed) { | |
e941894e JA |
458 | clear_bit(hash, &wq->hash->map); |
459 | if (wq_has_sleeper(&wq->hash->wait)) | |
460 | wake_up(&wq->hash->wait); | |
95da8465 | 461 | raw_spin_lock_irq(&wqe->lock); |
dc026a73 | 462 | wqe->flags &= ~IO_WQE_FLAG_STALLED; |
f462fd36 PB |
463 | /* skip unnecessary unlock-lock wqe->lock */ |
464 | if (!work) | |
465 | goto get_next; | |
95da8465 | 466 | raw_spin_unlock_irq(&wqe->lock); |
7d723065 | 467 | } |
58e39319 | 468 | } while (work); |
7d723065 | 469 | |
95da8465 | 470 | raw_spin_lock_irq(&wqe->lock); |
771b53d0 JA |
471 | } while (1); |
472 | } | |
473 | ||
771b53d0 JA |
474 | static int io_wqe_worker(void *data) |
475 | { | |
476 | struct io_worker *worker = data; | |
477 | struct io_wqe *wqe = worker->wqe; | |
478 | struct io_wq *wq = wqe->wq; | |
771b53d0 | 479 | |
958234d5 | 480 | io_worker_start(worker); |
771b53d0 JA |
481 | |
482 | while (!test_bit(IO_WQ_BIT_EXIT, &wq->state)) { | |
506d95ff | 483 | set_current_state(TASK_INTERRUPTIBLE); |
e995d512 | 484 | loop: |
95da8465 | 485 | raw_spin_lock_irq(&wqe->lock); |
771b53d0 | 486 | if (io_wqe_run_queue(wqe)) { |
771b53d0 | 487 | io_worker_handle_work(worker); |
e995d512 | 488 | goto loop; |
771b53d0 | 489 | } |
c6d77d92 | 490 | __io_worker_idle(wqe, worker); |
95da8465 | 491 | raw_spin_unlock_irq(&wqe->lock); |
3bfe6106 | 492 | io_flush_signals(); |
771b53d0 JA |
493 | if (schedule_timeout(WORKER_IDLE_TIMEOUT)) |
494 | continue; | |
3bfe6106 JA |
495 | if (fatal_signal_pending(current)) |
496 | break; | |
771b53d0 JA |
497 | /* timed out, exit unless we're the fixed worker */ |
498 | if (test_bit(IO_WQ_BIT_EXIT, &wq->state) || | |
499 | !(worker->flags & IO_WORKER_F_FIXED)) | |
500 | break; | |
501 | } | |
502 | ||
771b53d0 | 503 | if (test_bit(IO_WQ_BIT_EXIT, &wq->state)) { |
95da8465 | 504 | raw_spin_lock_irq(&wqe->lock); |
6206f0e1 | 505 | if (!wq_list_empty(&wqe->work_list)) |
771b53d0 JA |
506 | io_worker_handle_work(worker); |
507 | else | |
95da8465 | 508 | raw_spin_unlock_irq(&wqe->lock); |
771b53d0 JA |
509 | } |
510 | ||
511 | io_worker_exit(worker); | |
512 | return 0; | |
513 | } | |
514 | ||
771b53d0 JA |
515 | /* |
516 | * Called when a worker is scheduled in. Mark us as currently running. | |
517 | */ | |
518 | void io_wq_worker_running(struct task_struct *tsk) | |
519 | { | |
3bfe6106 | 520 | struct io_worker *worker = tsk->pf_io_worker; |
771b53d0 | 521 | |
3bfe6106 JA |
522 | if (!worker) |
523 | return; | |
771b53d0 JA |
524 | if (!(worker->flags & IO_WORKER_F_UP)) |
525 | return; | |
526 | if (worker->flags & IO_WORKER_F_RUNNING) | |
527 | return; | |
528 | worker->flags |= IO_WORKER_F_RUNNING; | |
958234d5 | 529 | io_wqe_inc_running(worker); |
771b53d0 JA |
530 | } |
531 | ||
532 | /* | |
533 | * Called when worker is going to sleep. If there are no workers currently | |
534 | * running and we have work pending, wake up a free one or have the manager | |
535 | * set one up. | |
536 | */ | |
537 | void io_wq_worker_sleeping(struct task_struct *tsk) | |
538 | { | |
3bfe6106 | 539 | struct io_worker *worker = tsk->pf_io_worker; |
771b53d0 | 540 | |
3bfe6106 JA |
541 | if (!worker) |
542 | return; | |
771b53d0 JA |
543 | if (!(worker->flags & IO_WORKER_F_UP)) |
544 | return; | |
545 | if (!(worker->flags & IO_WORKER_F_RUNNING)) | |
546 | return; | |
547 | ||
548 | worker->flags &= ~IO_WORKER_F_RUNNING; | |
549 | ||
3bfe6106 | 550 | raw_spin_lock_irq(&worker->wqe->lock); |
958234d5 | 551 | io_wqe_dec_running(worker); |
3bfe6106 | 552 | raw_spin_unlock_irq(&worker->wqe->lock); |
771b53d0 JA |
553 | } |
554 | ||
3bfe6106 | 555 | static int task_thread(void *data, int index) |
771b53d0 | 556 | { |
3bfe6106 JA |
557 | struct io_worker *worker = data; |
558 | struct io_wqe *wqe = worker->wqe; | |
c4068bf8 | 559 | struct io_wqe_acct *acct = &wqe->acct[index]; |
3bfe6106 JA |
560 | struct io_wq *wq = wqe->wq; |
561 | char buf[TASK_COMM_LEN]; | |
771b53d0 | 562 | |
3bfe6106 JA |
563 | sprintf(buf, "iou-wrk-%d", wq->task_pid); |
564 | set_task_comm(current, buf); | |
771b53d0 | 565 | |
3bfe6106 JA |
566 | current->pf_io_worker = worker; |
567 | worker->task = current; | |
771b53d0 | 568 | |
3bfe6106 JA |
569 | set_cpus_allowed_ptr(current, cpumask_of_node(wqe->node)); |
570 | current->flags |= PF_NO_SETAFFINITY; | |
771b53d0 | 571 | |
95da8465 | 572 | raw_spin_lock_irq(&wqe->lock); |
021d1cdd | 573 | hlist_nulls_add_head_rcu(&worker->nulls_node, &wqe->free_list); |
e61df66c | 574 | list_add_tail_rcu(&worker->all_list, &wqe->all_list); |
771b53d0 | 575 | worker->flags |= IO_WORKER_F_FREE; |
c5def4ab JA |
576 | if (index == IO_WQ_ACCT_BOUND) |
577 | worker->flags |= IO_WORKER_F_BOUND; | |
578 | if (!acct->nr_workers && (worker->flags & IO_WORKER_F_BOUND)) | |
771b53d0 | 579 | worker->flags |= IO_WORKER_F_FIXED; |
c5def4ab | 580 | acct->nr_workers++; |
95da8465 | 581 | raw_spin_unlock_irq(&wqe->lock); |
771b53d0 | 582 | |
3bfe6106 JA |
583 | io_wqe_worker(data); |
584 | do_exit(0); | |
585 | } | |
586 | ||
587 | static int task_thread_bound(void *data) | |
588 | { | |
589 | return task_thread(data, IO_WQ_ACCT_BOUND); | |
590 | } | |
591 | ||
592 | static int task_thread_unbound(void *data) | |
593 | { | |
594 | return task_thread(data, IO_WQ_ACCT_UNBOUND); | |
595 | } | |
596 | ||
843bbfd4 | 597 | pid_t io_wq_fork_thread(int (*fn)(void *), void *arg) |
3bfe6106 JA |
598 | { |
599 | unsigned long flags = CLONE_FS|CLONE_FILES|CLONE_SIGHAND|CLONE_THREAD| | |
600 | CLONE_IO|SIGCHLD; | |
601 | struct kernel_clone_args args = { | |
602 | .flags = ((lower_32_bits(flags) | CLONE_VM | | |
603 | CLONE_UNTRACED) & ~CSIGNAL), | |
604 | .exit_signal = (lower_32_bits(flags) & CSIGNAL), | |
605 | .stack = (unsigned long)fn, | |
606 | .stack_size = (unsigned long)arg, | |
607 | }; | |
608 | ||
609 | return kernel_clone(&args); | |
610 | } | |
611 | ||
612 | static bool create_io_worker(struct io_wq *wq, struct io_wqe *wqe, int index) | |
613 | { | |
614 | struct io_worker *worker; | |
615 | pid_t pid; | |
616 | ||
8b3e78b5 JA |
617 | __set_current_state(TASK_RUNNING); |
618 | ||
3bfe6106 JA |
619 | worker = kzalloc_node(sizeof(*worker), GFP_KERNEL, wqe->node); |
620 | if (!worker) | |
621 | return false; | |
622 | ||
623 | refcount_set(&worker->ref, 1); | |
624 | worker->nulls_node.pprev = NULL; | |
625 | worker->wqe = wqe; | |
626 | spin_lock_init(&worker->lock); | |
eb2de941 | 627 | init_completion(&worker->ref_done); |
65d43023 | 628 | init_completion(&worker->started); |
3bfe6106 | 629 | |
fb3a1f6c | 630 | atomic_inc(&wq->worker_refs); |
8b3e78b5 | 631 | |
3bfe6106 | 632 | if (index == IO_WQ_ACCT_BOUND) |
843bbfd4 | 633 | pid = io_wq_fork_thread(task_thread_bound, worker); |
3bfe6106 | 634 | else |
843bbfd4 | 635 | pid = io_wq_fork_thread(task_thread_unbound, worker); |
3bfe6106 | 636 | if (pid < 0) { |
fb3a1f6c JA |
637 | if (atomic_dec_and_test(&wq->worker_refs)) |
638 | complete(&wq->worker_done); | |
3bfe6106 JA |
639 | kfree(worker); |
640 | return false; | |
641 | } | |
65d43023 | 642 | wait_for_completion(&worker->started); |
b60fda60 | 643 | return true; |
771b53d0 JA |
644 | } |
645 | ||
c5def4ab | 646 | static inline bool io_wqe_need_worker(struct io_wqe *wqe, int index) |
771b53d0 JA |
647 | __must_hold(wqe->lock) |
648 | { | |
c5def4ab | 649 | struct io_wqe_acct *acct = &wqe->acct[index]; |
771b53d0 | 650 | |
613eeb60 JA |
651 | if (acct->nr_workers && test_bit(IO_WQ_BIT_EXIT, &wqe->wq->state)) |
652 | return false; | |
c5def4ab | 653 | /* if we have available workers or no work, no need */ |
021d1cdd | 654 | if (!hlist_nulls_empty(&wqe->free_list) || !io_wqe_run_queue(wqe)) |
c5def4ab JA |
655 | return false; |
656 | return acct->nr_workers < acct->max_workers; | |
771b53d0 JA |
657 | } |
658 | ||
c4068bf8 HD |
659 | /* |
660 | * Iterate the passed in list and call the specific function for each | |
661 | * worker that isn't exiting | |
662 | */ | |
663 | static bool io_wq_for_each_worker(struct io_wqe *wqe, | |
664 | bool (*func)(struct io_worker *, void *), | |
665 | void *data) | |
666 | { | |
667 | struct io_worker *worker; | |
668 | bool ret = false; | |
669 | ||
670 | list_for_each_entry_rcu(worker, &wqe->all_list, all_list) { | |
671 | if (io_worker_get(worker)) { | |
672 | /* no task if node is/was offline */ | |
673 | if (worker->task) | |
674 | ret = func(worker, data); | |
675 | io_worker_release(worker); | |
676 | if (ret) | |
677 | break; | |
678 | } | |
679 | } | |
680 | ||
681 | return ret; | |
682 | } | |
683 | ||
684 | static bool io_wq_worker_wake(struct io_worker *worker, void *data) | |
685 | { | |
686 | wake_up_process(worker->task); | |
687 | return false; | |
688 | } | |
689 | ||
8b3e78b5 JA |
690 | static void io_wq_check_workers(struct io_wq *wq) |
691 | { | |
692 | int node; | |
693 | ||
694 | for_each_node(node) { | |
695 | struct io_wqe *wqe = wq->wqes[node]; | |
696 | bool fork_worker[2] = { false, false }; | |
697 | ||
698 | if (!node_online(node)) | |
699 | continue; | |
700 | ||
701 | raw_spin_lock_irq(&wqe->lock); | |
702 | if (io_wqe_need_worker(wqe, IO_WQ_ACCT_BOUND)) | |
703 | fork_worker[IO_WQ_ACCT_BOUND] = true; | |
704 | if (io_wqe_need_worker(wqe, IO_WQ_ACCT_UNBOUND)) | |
705 | fork_worker[IO_WQ_ACCT_UNBOUND] = true; | |
706 | raw_spin_unlock_irq(&wqe->lock); | |
707 | if (fork_worker[IO_WQ_ACCT_BOUND]) | |
708 | create_io_worker(wq, wqe, IO_WQ_ACCT_BOUND); | |
709 | if (fork_worker[IO_WQ_ACCT_UNBOUND]) | |
710 | create_io_worker(wq, wqe, IO_WQ_ACCT_UNBOUND); | |
711 | } | |
712 | } | |
713 | ||
771b53d0 JA |
714 | /* |
715 | * Manager thread. Tasked with creating new workers, if we need them. | |
716 | */ | |
717 | static int io_wq_manager(void *data) | |
718 | { | |
719 | struct io_wq *wq = data; | |
3bfe6106 | 720 | char buf[TASK_COMM_LEN]; |
fb3a1f6c | 721 | int node; |
771b53d0 | 722 | |
3bfe6106 JA |
723 | sprintf(buf, "iou-mgr-%d", wq->task_pid); |
724 | set_task_comm(current, buf); | |
725 | current->flags |= PF_IO_WORKER; | |
afcc4015 | 726 | wq->manager = get_task_struct(current); |
3bfe6106 | 727 | |
dbf99620 | 728 | complete(&wq->started); |
b60fda60 | 729 | |
8b3e78b5 | 730 | do { |
771b53d0 | 731 | set_current_state(TASK_INTERRUPTIBLE); |
8b3e78b5 | 732 | io_wq_check_workers(wq); |
771b53d0 | 733 | schedule_timeout(HZ); |
3bfe6106 JA |
734 | if (fatal_signal_pending(current)) |
735 | set_bit(IO_WQ_BIT_EXIT, &wq->state); | |
8b3e78b5 JA |
736 | } while (!test_bit(IO_WQ_BIT_EXIT, &wq->state)); |
737 | ||
738 | io_wq_check_workers(wq); | |
fb3a1f6c JA |
739 | |
740 | rcu_read_lock(); | |
741 | for_each_node(node) | |
742 | io_wq_for_each_worker(wq->wqes[node], io_wq_worker_wake, NULL); | |
743 | rcu_read_unlock(); | |
744 | ||
745 | /* we might not ever have created any workers */ | |
746 | if (atomic_read(&wq->worker_refs)) | |
747 | wait_for_completion(&wq->worker_done); | |
d364d9e5 | 748 | complete(&wq->exited); |
3bfe6106 | 749 | do_exit(0); |
771b53d0 JA |
750 | } |
751 | ||
e9fd9396 | 752 | static void io_run_cancel(struct io_wq_work *work, struct io_wqe *wqe) |
fc04c39b | 753 | { |
e9fd9396 PB |
754 | struct io_wq *wq = wqe->wq; |
755 | ||
fc04c39b | 756 | do { |
fc04c39b | 757 | work->flags |= IO_WQ_WORK_CANCEL; |
5280f7e5 PB |
758 | wq->do_work(work); |
759 | work = wq->free_work(work); | |
fc04c39b PB |
760 | } while (work); |
761 | } | |
762 | ||
86f3cd1b PB |
763 | static void io_wqe_insert_work(struct io_wqe *wqe, struct io_wq_work *work) |
764 | { | |
765 | unsigned int hash; | |
766 | struct io_wq_work *tail; | |
767 | ||
768 | if (!io_wq_is_hashed(work)) { | |
769 | append: | |
770 | wq_list_add_tail(&work->list, &wqe->work_list); | |
771 | return; | |
772 | } | |
773 | ||
774 | hash = io_get_work_hash(work); | |
775 | tail = wqe->hash_tail[hash]; | |
776 | wqe->hash_tail[hash] = work; | |
777 | if (!tail) | |
778 | goto append; | |
779 | ||
780 | wq_list_add_after(&work->list, &tail->list, &wqe->work_list); | |
781 | } | |
782 | ||
4fb6ac32 JA |
783 | static int io_wq_fork_manager(struct io_wq *wq) |
784 | { | |
785 | int ret; | |
786 | ||
787 | if (wq->manager) | |
788 | return 0; | |
789 | ||
fb3a1f6c | 790 | reinit_completion(&wq->worker_done); |
4fb6ac32 JA |
791 | current->flags |= PF_IO_WORKER; |
792 | ret = io_wq_fork_thread(io_wq_manager, wq); | |
793 | current->flags &= ~PF_IO_WORKER; | |
794 | if (ret >= 0) { | |
dbf99620 | 795 | wait_for_completion(&wq->started); |
4fb6ac32 JA |
796 | return 0; |
797 | } | |
798 | ||
4fb6ac32 JA |
799 | return ret; |
800 | } | |
801 | ||
771b53d0 JA |
802 | static void io_wqe_enqueue(struct io_wqe *wqe, struct io_wq_work *work) |
803 | { | |
c5def4ab | 804 | struct io_wqe_acct *acct = io_work_get_acct(wqe, work); |
895e2ca0 | 805 | int work_flags; |
771b53d0 JA |
806 | unsigned long flags; |
807 | ||
4fb6ac32 JA |
808 | /* Can only happen if manager creation fails after exec */ |
809 | if (unlikely(io_wq_fork_manager(wqe->wq))) { | |
810 | work->flags |= IO_WQ_WORK_CANCEL; | |
811 | wqe->wq->do_work(work); | |
812 | return; | |
813 | } | |
814 | ||
895e2ca0 | 815 | work_flags = work->flags; |
95da8465 | 816 | raw_spin_lock_irqsave(&wqe->lock, flags); |
86f3cd1b | 817 | io_wqe_insert_work(wqe, work); |
771b53d0 | 818 | wqe->flags &= ~IO_WQE_FLAG_STALLED; |
95da8465 | 819 | raw_spin_unlock_irqrestore(&wqe->lock, flags); |
771b53d0 | 820 | |
895e2ca0 JA |
821 | if ((work_flags & IO_WQ_WORK_CONCURRENT) || |
822 | !atomic_read(&acct->nr_running)) | |
c5def4ab | 823 | io_wqe_wake_worker(wqe, acct); |
771b53d0 JA |
824 | } |
825 | ||
826 | void io_wq_enqueue(struct io_wq *wq, struct io_wq_work *work) | |
827 | { | |
828 | struct io_wqe *wqe = wq->wqes[numa_node_id()]; | |
829 | ||
830 | io_wqe_enqueue(wqe, work); | |
831 | } | |
832 | ||
833 | /* | |
8766dd51 PB |
834 | * Work items that hash to the same value will not be done in parallel. |
835 | * Used to limit concurrent writes, generally hashed by inode. | |
771b53d0 | 836 | */ |
8766dd51 | 837 | void io_wq_hash_work(struct io_wq_work *work, void *val) |
771b53d0 | 838 | { |
8766dd51 | 839 | unsigned int bit; |
771b53d0 JA |
840 | |
841 | bit = hash_ptr(val, IO_WQ_HASH_ORDER); | |
842 | work->flags |= (IO_WQ_WORK_HASHED | (bit << IO_WQ_HASH_SHIFT)); | |
771b53d0 JA |
843 | } |
844 | ||
62755e35 | 845 | struct io_cb_cancel_data { |
2293b419 PB |
846 | work_cancel_fn *fn; |
847 | void *data; | |
4f26bda1 PB |
848 | int nr_running; |
849 | int nr_pending; | |
850 | bool cancel_all; | |
62755e35 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; |
dbf99620 | 1039 | init_completion(&wq->started); |
d364d9e5 | 1040 | init_completion(&wq->exited); |
3bfe6106 | 1041 | refcount_set(&wq->refs, 1); |
771b53d0 | 1042 | |
fb3a1f6c JA |
1043 | init_completion(&wq->worker_done); |
1044 | atomic_set(&wq->worker_refs, 0); | |
1045 | ||
4fb6ac32 JA |
1046 | ret = io_wq_fork_manager(wq); |
1047 | if (!ret) | |
771b53d0 | 1048 | return wq; |
771b53d0 | 1049 | |
b60fda60 | 1050 | err: |
dc7bbc9e | 1051 | io_wq_put_hash(data->hash); |
43c01fbe | 1052 | cpuhp_state_remove_instance_nocalls(io_wq_online, &wq->cpuhp_node); |
3fc50ab5 JH |
1053 | for_each_node(node) |
1054 | kfree(wq->wqes[node]); | |
43c01fbe | 1055 | err_wqes: |
b60fda60 | 1056 | kfree(wq->wqes); |
43c01fbe | 1057 | err_wq: |
b60fda60 | 1058 | kfree(wq); |
771b53d0 JA |
1059 | return ERR_PTR(ret); |
1060 | } | |
1061 | ||
afcc4015 JA |
1062 | static void io_wq_destroy_manager(struct io_wq *wq) |
1063 | { | |
1064 | if (wq->manager) { | |
1065 | wake_up_process(wq->manager); | |
1066 | wait_for_completion(&wq->exited); | |
1067 | put_task_struct(wq->manager); | |
1068 | wq->manager = NULL; | |
1069 | } | |
1070 | } | |
1071 | ||
4fb6ac32 | 1072 | static void io_wq_destroy(struct io_wq *wq) |
771b53d0 | 1073 | { |
3fc50ab5 | 1074 | int node; |
771b53d0 | 1075 | |
43c01fbe JA |
1076 | cpuhp_state_remove_instance_nocalls(io_wq_online, &wq->cpuhp_node); |
1077 | ||
b60fda60 | 1078 | set_bit(IO_WQ_BIT_EXIT, &wq->state); |
afcc4015 | 1079 | io_wq_destroy_manager(wq); |
771b53d0 | 1080 | |
e941894e JA |
1081 | spin_lock_irq(&wq->hash->wait.lock); |
1082 | for_each_node(node) { | |
1083 | struct io_wqe *wqe = wq->wqes[node]; | |
1084 | ||
1085 | list_del_init(&wqe->wait.entry); | |
1086 | kfree(wqe); | |
1087 | } | |
1088 | spin_unlock_irq(&wq->hash->wait.lock); | |
1089 | io_wq_put_hash(wq->hash); | |
771b53d0 JA |
1090 | kfree(wq->wqes); |
1091 | kfree(wq); | |
4fb6ac32 JA |
1092 | } |
1093 | ||
1094 | void io_wq_put(struct io_wq *wq) | |
1095 | { | |
1096 | if (refcount_dec_and_test(&wq->refs)) | |
1097 | io_wq_destroy(wq); | |
771b53d0 | 1098 | } |
848f7e18 | 1099 | |
afcc4015 JA |
1100 | void io_wq_put_and_exit(struct io_wq *wq) |
1101 | { | |
1102 | set_bit(IO_WQ_BIT_EXIT, &wq->state); | |
1103 | io_wq_destroy_manager(wq); | |
1104 | io_wq_put(wq); | |
1105 | } | |
1106 | ||
43c01fbe JA |
1107 | static bool io_wq_worker_affinity(struct io_worker *worker, void *data) |
1108 | { | |
1109 | struct task_struct *task = worker->task; | |
1110 | struct rq_flags rf; | |
1111 | struct rq *rq; | |
1112 | ||
1113 | rq = task_rq_lock(task, &rf); | |
1114 | do_set_cpus_allowed(task, cpumask_of_node(worker->wqe->node)); | |
1115 | task->flags |= PF_NO_SETAFFINITY; | |
1116 | task_rq_unlock(rq, task, &rf); | |
1117 | return false; | |
1118 | } | |
1119 | ||
1120 | static int io_wq_cpu_online(unsigned int cpu, struct hlist_node *node) | |
1121 | { | |
1122 | struct io_wq *wq = hlist_entry_safe(node, struct io_wq, cpuhp_node); | |
1123 | int i; | |
1124 | ||
1125 | rcu_read_lock(); | |
1126 | for_each_node(i) | |
1127 | io_wq_for_each_worker(wq->wqes[i], io_wq_worker_affinity, NULL); | |
1128 | rcu_read_unlock(); | |
1129 | return 0; | |
1130 | } | |
1131 | ||
1132 | static __init int io_wq_init(void) | |
1133 | { | |
1134 | int ret; | |
1135 | ||
1136 | ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "io-wq/online", | |
1137 | io_wq_cpu_online, NULL); | |
1138 | if (ret < 0) | |
1139 | return ret; | |
1140 | io_wq_online = ret; | |
1141 | return 0; | |
1142 | } | |
1143 | subsys_initcall(io_wq_init); |