]>
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> |
e4b4a13f | 19 | #include <linux/freezer.h> |
771b53d0 | 20 | |
43c01fbe | 21 | #include "../kernel/sched/sched.h" |
771b53d0 JA |
22 | #include "io-wq.h" |
23 | ||
24 | #define WORKER_IDLE_TIMEOUT (5 * HZ) | |
25 | ||
26 | enum { | |
27 | IO_WORKER_F_UP = 1, /* up and active */ | |
28 | IO_WORKER_F_RUNNING = 2, /* account as running */ | |
29 | IO_WORKER_F_FREE = 4, /* worker on free list */ | |
145cc8c6 JA |
30 | IO_WORKER_F_FIXED = 8, /* static idle worker */ |
31 | IO_WORKER_F_BOUND = 16, /* is doing bounded work */ | |
771b53d0 JA |
32 | }; |
33 | ||
34 | enum { | |
35 | IO_WQ_BIT_EXIT = 0, /* wq exiting */ | |
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 | 55 | |
eb2de941 | 56 | struct completion ref_done; |
65d43023 | 57 | struct completion started; |
eb2de941 | 58 | |
771b53d0 | 59 | struct rcu_head rcu; |
771b53d0 JA |
60 | }; |
61 | ||
771b53d0 JA |
62 | #if BITS_PER_LONG == 64 |
63 | #define IO_WQ_HASH_ORDER 6 | |
64 | #else | |
65 | #define IO_WQ_HASH_ORDER 5 | |
66 | #endif | |
67 | ||
86f3cd1b PB |
68 | #define IO_WQ_NR_HASH_BUCKETS (1u << IO_WQ_HASH_ORDER) |
69 | ||
c5def4ab JA |
70 | struct io_wqe_acct { |
71 | unsigned nr_workers; | |
72 | unsigned max_workers; | |
73 | atomic_t nr_running; | |
74 | }; | |
75 | ||
76 | enum { | |
77 | IO_WQ_ACCT_BOUND, | |
78 | IO_WQ_ACCT_UNBOUND, | |
79 | }; | |
80 | ||
771b53d0 JA |
81 | /* |
82 | * Per-node worker thread pool | |
83 | */ | |
84 | struct io_wqe { | |
85 | struct { | |
95da8465 | 86 | raw_spinlock_t lock; |
6206f0e1 | 87 | struct io_wq_work_list work_list; |
771b53d0 JA |
88 | unsigned flags; |
89 | } ____cacheline_aligned_in_smp; | |
90 | ||
91 | int node; | |
c5def4ab | 92 | struct io_wqe_acct acct[2]; |
771b53d0 | 93 | |
021d1cdd | 94 | struct hlist_nulls_head free_list; |
e61df66c | 95 | struct list_head all_list; |
771b53d0 | 96 | |
e941894e JA |
97 | struct wait_queue_entry wait; |
98 | ||
771b53d0 | 99 | struct io_wq *wq; |
86f3cd1b | 100 | struct io_wq_work *hash_tail[IO_WQ_NR_HASH_BUCKETS]; |
771b53d0 JA |
101 | }; |
102 | ||
103 | /* | |
104 | * Per io_wq state | |
105 | */ | |
106 | struct io_wq { | |
107 | struct io_wqe **wqes; | |
108 | unsigned long state; | |
771b53d0 | 109 | |
e9fd9396 | 110 | free_work_fn *free_work; |
f5fa38c5 | 111 | io_wq_work_fn *do_work; |
7d723065 | 112 | |
771b53d0 | 113 | struct task_struct *manager; |
c5def4ab | 114 | struct user_struct *user; |
e941894e JA |
115 | |
116 | struct io_wq_hash *hash; | |
117 | ||
771b53d0 | 118 | refcount_t refs; |
dbf99620 | 119 | struct completion started; |
d364d9e5 | 120 | struct completion exited; |
848f7e18 | 121 | |
fb3a1f6c JA |
122 | atomic_t worker_refs; |
123 | struct completion worker_done; | |
124 | ||
43c01fbe | 125 | struct hlist_node cpuhp_node; |
3bfe6106 JA |
126 | |
127 | pid_t task_pid; | |
771b53d0 JA |
128 | }; |
129 | ||
43c01fbe JA |
130 | static enum cpuhp_state io_wq_online; |
131 | ||
771b53d0 JA |
132 | static bool io_worker_get(struct io_worker *worker) |
133 | { | |
134 | return refcount_inc_not_zero(&worker->ref); | |
135 | } | |
136 | ||
137 | static void io_worker_release(struct io_worker *worker) | |
138 | { | |
139 | if (refcount_dec_and_test(&worker->ref)) | |
eb2de941 | 140 | complete(&worker->ref_done); |
771b53d0 JA |
141 | } |
142 | ||
c5def4ab JA |
143 | static inline struct io_wqe_acct *io_work_get_acct(struct io_wqe *wqe, |
144 | struct io_wq_work *work) | |
145 | { | |
146 | if (work->flags & IO_WQ_WORK_UNBOUND) | |
147 | return &wqe->acct[IO_WQ_ACCT_UNBOUND]; | |
148 | ||
149 | return &wqe->acct[IO_WQ_ACCT_BOUND]; | |
150 | } | |
151 | ||
958234d5 | 152 | static inline struct io_wqe_acct *io_wqe_get_acct(struct io_worker *worker) |
c5def4ab | 153 | { |
958234d5 JA |
154 | struct io_wqe *wqe = worker->wqe; |
155 | ||
c5def4ab JA |
156 | if (worker->flags & IO_WORKER_F_BOUND) |
157 | return &wqe->acct[IO_WQ_ACCT_BOUND]; | |
158 | ||
159 | return &wqe->acct[IO_WQ_ACCT_UNBOUND]; | |
160 | } | |
161 | ||
771b53d0 JA |
162 | static void io_worker_exit(struct io_worker *worker) |
163 | { | |
164 | struct io_wqe *wqe = worker->wqe; | |
958234d5 | 165 | struct io_wqe_acct *acct = io_wqe_get_acct(worker); |
bf1daa4b | 166 | unsigned flags; |
771b53d0 | 167 | |
eb2de941 JA |
168 | if (refcount_dec_and_test(&worker->ref)) |
169 | complete(&worker->ref_done); | |
170 | wait_for_completion(&worker->ref_done); | |
771b53d0 JA |
171 | |
172 | preempt_disable(); | |
173 | current->flags &= ~PF_IO_WORKER; | |
bf1daa4b JA |
174 | flags = worker->flags; |
175 | worker->flags = 0; | |
176 | if (flags & IO_WORKER_F_RUNNING) | |
c5def4ab | 177 | atomic_dec(&acct->nr_running); |
771b53d0 JA |
178 | worker->flags = 0; |
179 | preempt_enable(); | |
180 | ||
95da8465 | 181 | raw_spin_lock_irq(&wqe->lock); |
bf1daa4b JA |
182 | if (flags & IO_WORKER_F_FREE) |
183 | hlist_nulls_del_rcu(&worker->nulls_node); | |
e61df66c | 184 | list_del_rcu(&worker->all_list); |
c5def4ab | 185 | acct->nr_workers--; |
95da8465 | 186 | raw_spin_unlock_irq(&wqe->lock); |
771b53d0 | 187 | |
364b05fd | 188 | kfree_rcu(worker, rcu); |
fb3a1f6c JA |
189 | if (atomic_dec_and_test(&wqe->wq->worker_refs)) |
190 | complete(&wqe->wq->worker_done); | |
771b53d0 JA |
191 | } |
192 | ||
c5def4ab JA |
193 | static inline bool io_wqe_run_queue(struct io_wqe *wqe) |
194 | __must_hold(wqe->lock) | |
195 | { | |
6206f0e1 JA |
196 | if (!wq_list_empty(&wqe->work_list) && |
197 | !(wqe->flags & IO_WQE_FLAG_STALLED)) | |
c5def4ab JA |
198 | return true; |
199 | return false; | |
200 | } | |
201 | ||
202 | /* | |
203 | * Check head of free list for an available worker. If one isn't available, | |
204 | * caller must wake up the wq manager to create one. | |
205 | */ | |
206 | static bool io_wqe_activate_free_worker(struct io_wqe *wqe) | |
207 | __must_hold(RCU) | |
208 | { | |
209 | struct hlist_nulls_node *n; | |
210 | struct io_worker *worker; | |
211 | ||
021d1cdd | 212 | n = rcu_dereference(hlist_nulls_first_rcu(&wqe->free_list)); |
c5def4ab JA |
213 | if (is_a_nulls(n)) |
214 | return false; | |
215 | ||
216 | worker = hlist_nulls_entry(n, struct io_worker, nulls_node); | |
217 | if (io_worker_get(worker)) { | |
506d95ff | 218 | wake_up_process(worker->task); |
c5def4ab JA |
219 | io_worker_release(worker); |
220 | return true; | |
221 | } | |
222 | ||
223 | return false; | |
224 | } | |
225 | ||
226 | /* | |
227 | * We need a worker. If we find a free one, we're good. If not, and we're | |
228 | * below the max number of workers, wake up the manager to create one. | |
229 | */ | |
230 | static void io_wqe_wake_worker(struct io_wqe *wqe, struct io_wqe_acct *acct) | |
231 | { | |
232 | bool ret; | |
233 | ||
234 | /* | |
235 | * Most likely an attempt to queue unbounded work on an io_wq that | |
236 | * wasn't setup with any unbounded workers. | |
237 | */ | |
238 | WARN_ON_ONCE(!acct->max_workers); | |
239 | ||
240 | rcu_read_lock(); | |
241 | ret = io_wqe_activate_free_worker(wqe); | |
242 | rcu_read_unlock(); | |
243 | ||
244 | if (!ret && acct->nr_workers < acct->max_workers) | |
245 | wake_up_process(wqe->wq->manager); | |
246 | } | |
247 | ||
958234d5 | 248 | static void io_wqe_inc_running(struct io_worker *worker) |
c5def4ab | 249 | { |
958234d5 | 250 | struct io_wqe_acct *acct = io_wqe_get_acct(worker); |
c5def4ab JA |
251 | |
252 | atomic_inc(&acct->nr_running); | |
253 | } | |
254 | ||
958234d5 | 255 | static void io_wqe_dec_running(struct io_worker *worker) |
c5def4ab JA |
256 | __must_hold(wqe->lock) |
257 | { | |
958234d5 JA |
258 | struct io_wqe_acct *acct = io_wqe_get_acct(worker); |
259 | struct io_wqe *wqe = worker->wqe; | |
c5def4ab JA |
260 | |
261 | if (atomic_dec_and_test(&acct->nr_running) && io_wqe_run_queue(wqe)) | |
262 | io_wqe_wake_worker(wqe, acct); | |
263 | } | |
264 | ||
958234d5 | 265 | static void io_worker_start(struct io_worker *worker) |
771b53d0 | 266 | { |
e4b4a13f | 267 | current->flags |= PF_NOFREEZE; |
771b53d0 | 268 | worker->flags |= (IO_WORKER_F_UP | IO_WORKER_F_RUNNING); |
958234d5 | 269 | io_wqe_inc_running(worker); |
65d43023 | 270 | complete(&worker->started); |
771b53d0 JA |
271 | } |
272 | ||
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 | ||
3bfe6106 | 388 | static void io_flush_signals(void) |
cccf0ee8 | 389 | { |
3bfe6106 JA |
390 | if (unlikely(test_tsk_thread_flag(current, TIF_NOTIFY_SIGNAL))) { |
391 | if (current->task_works) | |
392 | task_work_run(); | |
393 | clear_tsk_thread_flag(current, TIF_NOTIFY_SIGNAL); | |
cccf0ee8 | 394 | } |
dc026a73 PB |
395 | } |
396 | ||
397 | static void io_assign_current_work(struct io_worker *worker, | |
398 | struct io_wq_work *work) | |
399 | { | |
d78298e7 | 400 | if (work) { |
3bfe6106 | 401 | io_flush_signals(); |
d78298e7 PB |
402 | cond_resched(); |
403 | } | |
dc026a73 PB |
404 | |
405 | spin_lock_irq(&worker->lock); | |
406 | worker->cur_work = work; | |
407 | spin_unlock_irq(&worker->lock); | |
408 | } | |
409 | ||
60cf46ae PB |
410 | static void io_wqe_enqueue(struct io_wqe *wqe, struct io_wq_work *work); |
411 | ||
771b53d0 JA |
412 | static void io_worker_handle_work(struct io_worker *worker) |
413 | __releases(wqe->lock) | |
414 | { | |
771b53d0 JA |
415 | struct io_wqe *wqe = worker->wqe; |
416 | struct io_wq *wq = wqe->wq; | |
417 | ||
418 | do { | |
86f3cd1b | 419 | struct io_wq_work *work; |
f462fd36 | 420 | get_next: |
771b53d0 JA |
421 | /* |
422 | * If we got some work, mark us as busy. If we didn't, but | |
423 | * the list isn't empty, it means we stalled on hashed work. | |
424 | * Mark us stalled so we don't keep looking for work when we | |
425 | * can't make progress, any work completion or insertion will | |
426 | * clear the stalled flag. | |
427 | */ | |
60cf46ae | 428 | work = io_get_next_work(wqe); |
771b53d0 JA |
429 | if (work) |
430 | __io_worker_busy(wqe, worker, work); | |
6206f0e1 | 431 | else if (!wq_list_empty(&wqe->work_list)) |
771b53d0 JA |
432 | wqe->flags |= IO_WQE_FLAG_STALLED; |
433 | ||
95da8465 | 434 | raw_spin_unlock_irq(&wqe->lock); |
771b53d0 JA |
435 | if (!work) |
436 | break; | |
58e39319 | 437 | io_assign_current_work(worker, work); |
e941894e | 438 | __set_current_state(TASK_RUNNING); |
36c2f922 | 439 | |
dc026a73 PB |
440 | /* handle a whole dependent link */ |
441 | do { | |
5280f7e5 | 442 | struct io_wq_work *next_hashed, *linked; |
b089ed39 | 443 | unsigned int hash = io_get_work_hash(work); |
dc026a73 | 444 | |
86f3cd1b | 445 | next_hashed = wq_next_work(work); |
5280f7e5 PB |
446 | wq->do_work(work); |
447 | io_assign_current_work(worker, NULL); | |
dc026a73 | 448 | |
5280f7e5 | 449 | linked = wq->free_work(work); |
86f3cd1b PB |
450 | work = next_hashed; |
451 | if (!work && linked && !io_wq_is_hashed(linked)) { | |
452 | work = linked; | |
453 | linked = NULL; | |
454 | } | |
455 | io_assign_current_work(worker, work); | |
86f3cd1b PB |
456 | if (linked) |
457 | io_wqe_enqueue(wqe, linked); | |
458 | ||
459 | if (hash != -1U && !next_hashed) { | |
e941894e JA |
460 | clear_bit(hash, &wq->hash->map); |
461 | if (wq_has_sleeper(&wq->hash->wait)) | |
462 | wake_up(&wq->hash->wait); | |
95da8465 | 463 | raw_spin_lock_irq(&wqe->lock); |
dc026a73 | 464 | wqe->flags &= ~IO_WQE_FLAG_STALLED; |
f462fd36 PB |
465 | /* skip unnecessary unlock-lock wqe->lock */ |
466 | if (!work) | |
467 | goto get_next; | |
95da8465 | 468 | raw_spin_unlock_irq(&wqe->lock); |
7d723065 | 469 | } |
58e39319 | 470 | } while (work); |
7d723065 | 471 | |
95da8465 | 472 | raw_spin_lock_irq(&wqe->lock); |
771b53d0 JA |
473 | } while (1); |
474 | } | |
475 | ||
771b53d0 JA |
476 | static int io_wqe_worker(void *data) |
477 | { | |
478 | struct io_worker *worker = data; | |
479 | struct io_wqe *wqe = worker->wqe; | |
480 | struct io_wq *wq = wqe->wq; | |
771b53d0 | 481 | |
958234d5 | 482 | io_worker_start(worker); |
771b53d0 JA |
483 | |
484 | while (!test_bit(IO_WQ_BIT_EXIT, &wq->state)) { | |
506d95ff | 485 | set_current_state(TASK_INTERRUPTIBLE); |
e995d512 | 486 | loop: |
95da8465 | 487 | raw_spin_lock_irq(&wqe->lock); |
771b53d0 | 488 | if (io_wqe_run_queue(wqe)) { |
771b53d0 | 489 | io_worker_handle_work(worker); |
e995d512 | 490 | goto loop; |
771b53d0 | 491 | } |
c6d77d92 | 492 | __io_worker_idle(wqe, worker); |
95da8465 | 493 | raw_spin_unlock_irq(&wqe->lock); |
3bfe6106 | 494 | io_flush_signals(); |
771b53d0 JA |
495 | if (schedule_timeout(WORKER_IDLE_TIMEOUT)) |
496 | continue; | |
3bfe6106 JA |
497 | if (fatal_signal_pending(current)) |
498 | break; | |
771b53d0 JA |
499 | /* timed out, exit unless we're the fixed worker */ |
500 | if (test_bit(IO_WQ_BIT_EXIT, &wq->state) || | |
501 | !(worker->flags & IO_WORKER_F_FIXED)) | |
502 | break; | |
503 | } | |
504 | ||
771b53d0 | 505 | if (test_bit(IO_WQ_BIT_EXIT, &wq->state)) { |
95da8465 | 506 | raw_spin_lock_irq(&wqe->lock); |
6206f0e1 | 507 | if (!wq_list_empty(&wqe->work_list)) |
771b53d0 JA |
508 | io_worker_handle_work(worker); |
509 | else | |
95da8465 | 510 | raw_spin_unlock_irq(&wqe->lock); |
771b53d0 JA |
511 | } |
512 | ||
513 | io_worker_exit(worker); | |
514 | return 0; | |
515 | } | |
516 | ||
771b53d0 JA |
517 | /* |
518 | * Called when a worker is scheduled in. Mark us as currently running. | |
519 | */ | |
520 | void io_wq_worker_running(struct task_struct *tsk) | |
521 | { | |
3bfe6106 | 522 | struct io_worker *worker = tsk->pf_io_worker; |
771b53d0 | 523 | |
3bfe6106 JA |
524 | if (!worker) |
525 | return; | |
771b53d0 JA |
526 | if (!(worker->flags & IO_WORKER_F_UP)) |
527 | return; | |
528 | if (worker->flags & IO_WORKER_F_RUNNING) | |
529 | return; | |
530 | worker->flags |= IO_WORKER_F_RUNNING; | |
958234d5 | 531 | io_wqe_inc_running(worker); |
771b53d0 JA |
532 | } |
533 | ||
534 | /* | |
535 | * Called when worker is going to sleep. If there are no workers currently | |
536 | * running and we have work pending, wake up a free one or have the manager | |
537 | * set one up. | |
538 | */ | |
539 | void io_wq_worker_sleeping(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 | ||
550 | worker->flags &= ~IO_WORKER_F_RUNNING; | |
551 | ||
3bfe6106 | 552 | raw_spin_lock_irq(&worker->wqe->lock); |
958234d5 | 553 | io_wqe_dec_running(worker); |
3bfe6106 | 554 | raw_spin_unlock_irq(&worker->wqe->lock); |
771b53d0 JA |
555 | } |
556 | ||
3bfe6106 | 557 | static int task_thread(void *data, int index) |
771b53d0 | 558 | { |
3bfe6106 JA |
559 | struct io_worker *worker = data; |
560 | struct io_wqe *wqe = worker->wqe; | |
c4068bf8 | 561 | struct io_wqe_acct *acct = &wqe->acct[index]; |
3bfe6106 JA |
562 | struct io_wq *wq = wqe->wq; |
563 | char buf[TASK_COMM_LEN]; | |
771b53d0 | 564 | |
3bfe6106 JA |
565 | sprintf(buf, "iou-wrk-%d", wq->task_pid); |
566 | set_task_comm(current, buf); | |
771b53d0 | 567 | |
3bfe6106 JA |
568 | current->pf_io_worker = worker; |
569 | worker->task = current; | |
771b53d0 | 570 | |
3bfe6106 JA |
571 | set_cpus_allowed_ptr(current, cpumask_of_node(wqe->node)); |
572 | current->flags |= PF_NO_SETAFFINITY; | |
771b53d0 | 573 | |
95da8465 | 574 | raw_spin_lock_irq(&wqe->lock); |
021d1cdd | 575 | hlist_nulls_add_head_rcu(&worker->nulls_node, &wqe->free_list); |
e61df66c | 576 | list_add_tail_rcu(&worker->all_list, &wqe->all_list); |
771b53d0 | 577 | worker->flags |= IO_WORKER_F_FREE; |
c5def4ab JA |
578 | if (index == IO_WQ_ACCT_BOUND) |
579 | worker->flags |= IO_WORKER_F_BOUND; | |
580 | if (!acct->nr_workers && (worker->flags & IO_WORKER_F_BOUND)) | |
771b53d0 | 581 | worker->flags |= IO_WORKER_F_FIXED; |
c5def4ab | 582 | acct->nr_workers++; |
95da8465 | 583 | raw_spin_unlock_irq(&wqe->lock); |
771b53d0 | 584 | |
3bfe6106 JA |
585 | io_wqe_worker(data); |
586 | do_exit(0); | |
587 | } | |
588 | ||
589 | static int task_thread_bound(void *data) | |
590 | { | |
591 | return task_thread(data, IO_WQ_ACCT_BOUND); | |
592 | } | |
593 | ||
594 | static int task_thread_unbound(void *data) | |
595 | { | |
596 | return task_thread(data, IO_WQ_ACCT_UNBOUND); | |
597 | } | |
598 | ||
843bbfd4 | 599 | pid_t io_wq_fork_thread(int (*fn)(void *), void *arg) |
3bfe6106 JA |
600 | { |
601 | unsigned long flags = CLONE_FS|CLONE_FILES|CLONE_SIGHAND|CLONE_THREAD| | |
602 | CLONE_IO|SIGCHLD; | |
603 | struct kernel_clone_args args = { | |
604 | .flags = ((lower_32_bits(flags) | CLONE_VM | | |
605 | CLONE_UNTRACED) & ~CSIGNAL), | |
606 | .exit_signal = (lower_32_bits(flags) & CSIGNAL), | |
607 | .stack = (unsigned long)fn, | |
608 | .stack_size = (unsigned long)arg, | |
609 | }; | |
610 | ||
611 | return kernel_clone(&args); | |
612 | } | |
613 | ||
614 | static bool create_io_worker(struct io_wq *wq, struct io_wqe *wqe, int index) | |
615 | { | |
616 | struct io_worker *worker; | |
617 | pid_t pid; | |
618 | ||
8b3e78b5 JA |
619 | __set_current_state(TASK_RUNNING); |
620 | ||
3bfe6106 JA |
621 | worker = kzalloc_node(sizeof(*worker), GFP_KERNEL, wqe->node); |
622 | if (!worker) | |
623 | return false; | |
624 | ||
625 | refcount_set(&worker->ref, 1); | |
626 | worker->nulls_node.pprev = NULL; | |
627 | worker->wqe = wqe; | |
628 | spin_lock_init(&worker->lock); | |
eb2de941 | 629 | init_completion(&worker->ref_done); |
65d43023 | 630 | init_completion(&worker->started); |
3bfe6106 | 631 | |
fb3a1f6c | 632 | atomic_inc(&wq->worker_refs); |
8b3e78b5 | 633 | |
3bfe6106 | 634 | if (index == IO_WQ_ACCT_BOUND) |
843bbfd4 | 635 | pid = io_wq_fork_thread(task_thread_bound, worker); |
3bfe6106 | 636 | else |
843bbfd4 | 637 | pid = io_wq_fork_thread(task_thread_unbound, worker); |
3bfe6106 | 638 | if (pid < 0) { |
fb3a1f6c JA |
639 | if (atomic_dec_and_test(&wq->worker_refs)) |
640 | complete(&wq->worker_done); | |
3bfe6106 JA |
641 | kfree(worker); |
642 | return false; | |
643 | } | |
65d43023 | 644 | wait_for_completion(&worker->started); |
b60fda60 | 645 | return true; |
771b53d0 JA |
646 | } |
647 | ||
c5def4ab | 648 | static inline bool io_wqe_need_worker(struct io_wqe *wqe, int index) |
771b53d0 JA |
649 | __must_hold(wqe->lock) |
650 | { | |
c5def4ab | 651 | struct io_wqe_acct *acct = &wqe->acct[index]; |
771b53d0 | 652 | |
613eeb60 JA |
653 | if (acct->nr_workers && test_bit(IO_WQ_BIT_EXIT, &wqe->wq->state)) |
654 | return false; | |
c5def4ab | 655 | /* if we have available workers or no work, no need */ |
021d1cdd | 656 | if (!hlist_nulls_empty(&wqe->free_list) || !io_wqe_run_queue(wqe)) |
c5def4ab JA |
657 | return false; |
658 | return acct->nr_workers < acct->max_workers; | |
771b53d0 JA |
659 | } |
660 | ||
c4068bf8 HD |
661 | /* |
662 | * Iterate the passed in list and call the specific function for each | |
663 | * worker that isn't exiting | |
664 | */ | |
665 | static bool io_wq_for_each_worker(struct io_wqe *wqe, | |
666 | bool (*func)(struct io_worker *, void *), | |
667 | void *data) | |
668 | { | |
669 | struct io_worker *worker; | |
670 | bool ret = false; | |
671 | ||
672 | list_for_each_entry_rcu(worker, &wqe->all_list, all_list) { | |
673 | if (io_worker_get(worker)) { | |
674 | /* no task if node is/was offline */ | |
675 | if (worker->task) | |
676 | ret = func(worker, data); | |
677 | io_worker_release(worker); | |
678 | if (ret) | |
679 | break; | |
680 | } | |
681 | } | |
682 | ||
683 | return ret; | |
684 | } | |
685 | ||
686 | static bool io_wq_worker_wake(struct io_worker *worker, void *data) | |
687 | { | |
688 | wake_up_process(worker->task); | |
689 | return false; | |
690 | } | |
691 | ||
8b3e78b5 JA |
692 | static void io_wq_check_workers(struct io_wq *wq) |
693 | { | |
694 | int node; | |
695 | ||
696 | for_each_node(node) { | |
697 | struct io_wqe *wqe = wq->wqes[node]; | |
698 | bool fork_worker[2] = { false, false }; | |
699 | ||
700 | if (!node_online(node)) | |
701 | continue; | |
702 | ||
703 | raw_spin_lock_irq(&wqe->lock); | |
704 | if (io_wqe_need_worker(wqe, IO_WQ_ACCT_BOUND)) | |
705 | fork_worker[IO_WQ_ACCT_BOUND] = true; | |
706 | if (io_wqe_need_worker(wqe, IO_WQ_ACCT_UNBOUND)) | |
707 | fork_worker[IO_WQ_ACCT_UNBOUND] = true; | |
708 | raw_spin_unlock_irq(&wqe->lock); | |
709 | if (fork_worker[IO_WQ_ACCT_BOUND]) | |
710 | create_io_worker(wq, wqe, IO_WQ_ACCT_BOUND); | |
711 | if (fork_worker[IO_WQ_ACCT_UNBOUND]) | |
712 | create_io_worker(wq, wqe, IO_WQ_ACCT_UNBOUND); | |
713 | } | |
714 | } | |
715 | ||
771b53d0 JA |
716 | /* |
717 | * Manager thread. Tasked with creating new workers, if we need them. | |
718 | */ | |
719 | static int io_wq_manager(void *data) | |
720 | { | |
721 | struct io_wq *wq = data; | |
3bfe6106 | 722 | char buf[TASK_COMM_LEN]; |
fb3a1f6c | 723 | int node; |
771b53d0 | 724 | |
3bfe6106 JA |
725 | sprintf(buf, "iou-mgr-%d", wq->task_pid); |
726 | set_task_comm(current, buf); | |
727 | current->flags |= PF_IO_WORKER; | |
afcc4015 | 728 | wq->manager = get_task_struct(current); |
3bfe6106 | 729 | |
dbf99620 | 730 | complete(&wq->started); |
b60fda60 | 731 | |
8b3e78b5 | 732 | do { |
771b53d0 | 733 | set_current_state(TASK_INTERRUPTIBLE); |
8b3e78b5 | 734 | io_wq_check_workers(wq); |
771b53d0 | 735 | schedule_timeout(HZ); |
e4b4a13f | 736 | try_to_freeze(); |
3bfe6106 JA |
737 | if (fatal_signal_pending(current)) |
738 | set_bit(IO_WQ_BIT_EXIT, &wq->state); | |
8b3e78b5 JA |
739 | } while (!test_bit(IO_WQ_BIT_EXIT, &wq->state)); |
740 | ||
741 | io_wq_check_workers(wq); | |
fb3a1f6c JA |
742 | |
743 | rcu_read_lock(); | |
744 | for_each_node(node) | |
745 | io_wq_for_each_worker(wq->wqes[node], io_wq_worker_wake, NULL); | |
746 | rcu_read_unlock(); | |
747 | ||
748 | /* we might not ever have created any workers */ | |
749 | if (atomic_read(&wq->worker_refs)) | |
750 | wait_for_completion(&wq->worker_done); | |
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 | { | |
788 | int ret; | |
789 | ||
790 | if (wq->manager) | |
791 | return 0; | |
792 | ||
fb3a1f6c | 793 | reinit_completion(&wq->worker_done); |
4fb6ac32 JA |
794 | current->flags |= PF_IO_WORKER; |
795 | ret = io_wq_fork_thread(io_wq_manager, wq); | |
796 | current->flags &= ~PF_IO_WORKER; | |
797 | if (ret >= 0) { | |
dbf99620 | 798 | wait_for_completion(&wq->started); |
4fb6ac32 JA |
799 | return 0; |
800 | } | |
801 | ||
4fb6ac32 JA |
802 | return ret; |
803 | } | |
804 | ||
771b53d0 JA |
805 | static void io_wqe_enqueue(struct io_wqe *wqe, struct io_wq_work *work) |
806 | { | |
c5def4ab | 807 | struct io_wqe_acct *acct = io_work_get_acct(wqe, work); |
895e2ca0 | 808 | int work_flags; |
771b53d0 JA |
809 | unsigned long flags; |
810 | ||
4fb6ac32 JA |
811 | /* Can only happen if manager creation fails after exec */ |
812 | if (unlikely(io_wq_fork_manager(wqe->wq))) { | |
813 | work->flags |= IO_WQ_WORK_CANCEL; | |
814 | wqe->wq->do_work(work); | |
815 | return; | |
816 | } | |
817 | ||
895e2ca0 | 818 | work_flags = work->flags; |
95da8465 | 819 | raw_spin_lock_irqsave(&wqe->lock, flags); |
86f3cd1b | 820 | io_wqe_insert_work(wqe, work); |
771b53d0 | 821 | wqe->flags &= ~IO_WQE_FLAG_STALLED; |
95da8465 | 822 | raw_spin_unlock_irqrestore(&wqe->lock, flags); |
771b53d0 | 823 | |
895e2ca0 JA |
824 | if ((work_flags & IO_WQ_WORK_CONCURRENT) || |
825 | !atomic_read(&acct->nr_running)) | |
c5def4ab | 826 | io_wqe_wake_worker(wqe, acct); |
771b53d0 JA |
827 | } |
828 | ||
829 | void io_wq_enqueue(struct io_wq *wq, struct io_wq_work *work) | |
830 | { | |
831 | struct io_wqe *wqe = wq->wqes[numa_node_id()]; | |
832 | ||
833 | io_wqe_enqueue(wqe, work); | |
834 | } | |
835 | ||
836 | /* | |
8766dd51 PB |
837 | * Work items that hash to the same value will not be done in parallel. |
838 | * Used to limit concurrent writes, generally hashed by inode. | |
771b53d0 | 839 | */ |
8766dd51 | 840 | void io_wq_hash_work(struct io_wq_work *work, void *val) |
771b53d0 | 841 | { |
8766dd51 | 842 | unsigned int bit; |
771b53d0 JA |
843 | |
844 | bit = hash_ptr(val, IO_WQ_HASH_ORDER); | |
845 | work->flags |= (IO_WQ_WORK_HASHED | (bit << IO_WQ_HASH_SHIFT)); | |
771b53d0 JA |
846 | } |
847 | ||
62755e35 | 848 | struct io_cb_cancel_data { |
2293b419 PB |
849 | work_cancel_fn *fn; |
850 | void *data; | |
4f26bda1 PB |
851 | int nr_running; |
852 | int nr_pending; | |
853 | bool cancel_all; | |
62755e35 JA |
854 | }; |
855 | ||
2293b419 | 856 | static bool io_wq_worker_cancel(struct io_worker *worker, void *data) |
62755e35 | 857 | { |
2293b419 | 858 | struct io_cb_cancel_data *match = data; |
6f72653e | 859 | unsigned long flags; |
62755e35 JA |
860 | |
861 | /* | |
862 | * Hold the lock to avoid ->cur_work going out of scope, caller | |
36c2f922 | 863 | * may dereference the passed in work. |
62755e35 | 864 | */ |
36c2f922 | 865 | spin_lock_irqsave(&worker->lock, flags); |
62755e35 | 866 | if (worker->cur_work && |
2293b419 | 867 | match->fn(worker->cur_work, match->data)) { |
3bfe6106 | 868 | set_notify_signal(worker->task); |
4f26bda1 | 869 | match->nr_running++; |
771b53d0 | 870 | } |
36c2f922 | 871 | spin_unlock_irqrestore(&worker->lock, flags); |
771b53d0 | 872 | |
4f26bda1 | 873 | return match->nr_running && !match->cancel_all; |
771b53d0 JA |
874 | } |
875 | ||
204361a7 PB |
876 | static inline void io_wqe_remove_pending(struct io_wqe *wqe, |
877 | struct io_wq_work *work, | |
878 | struct io_wq_work_node *prev) | |
879 | { | |
880 | unsigned int hash = io_get_work_hash(work); | |
881 | struct io_wq_work *prev_work = NULL; | |
882 | ||
883 | if (io_wq_is_hashed(work) && work == wqe->hash_tail[hash]) { | |
884 | if (prev) | |
885 | prev_work = container_of(prev, struct io_wq_work, list); | |
886 | if (prev_work && io_get_work_hash(prev_work) == hash) | |
887 | wqe->hash_tail[hash] = prev_work; | |
888 | else | |
889 | wqe->hash_tail[hash] = NULL; | |
890 | } | |
891 | wq_list_del(&wqe->work_list, &work->list, prev); | |
892 | } | |
893 | ||
4f26bda1 | 894 | static void io_wqe_cancel_pending_work(struct io_wqe *wqe, |
f4c2665e | 895 | struct io_cb_cancel_data *match) |
771b53d0 | 896 | { |
6206f0e1 | 897 | struct io_wq_work_node *node, *prev; |
771b53d0 | 898 | struct io_wq_work *work; |
6f72653e | 899 | unsigned long flags; |
771b53d0 | 900 | |
4f26bda1 | 901 | retry: |
95da8465 | 902 | raw_spin_lock_irqsave(&wqe->lock, flags); |
6206f0e1 JA |
903 | wq_list_for_each(node, prev, &wqe->work_list) { |
904 | work = container_of(node, struct io_wq_work, list); | |
4f26bda1 PB |
905 | if (!match->fn(work, match->data)) |
906 | continue; | |
204361a7 | 907 | io_wqe_remove_pending(wqe, work, prev); |
95da8465 | 908 | raw_spin_unlock_irqrestore(&wqe->lock, flags); |
4f26bda1 PB |
909 | io_run_cancel(work, wqe); |
910 | match->nr_pending++; | |
911 | if (!match->cancel_all) | |
912 | return; | |
913 | ||
914 | /* not safe to continue after unlock */ | |
915 | goto retry; | |
771b53d0 | 916 | } |
95da8465 | 917 | raw_spin_unlock_irqrestore(&wqe->lock, flags); |
f4c2665e PB |
918 | } |
919 | ||
4f26bda1 | 920 | static void io_wqe_cancel_running_work(struct io_wqe *wqe, |
f4c2665e PB |
921 | struct io_cb_cancel_data *match) |
922 | { | |
771b53d0 | 923 | rcu_read_lock(); |
4f26bda1 | 924 | io_wq_for_each_worker(wqe, io_wq_worker_cancel, match); |
771b53d0 | 925 | rcu_read_unlock(); |
771b53d0 JA |
926 | } |
927 | ||
2293b419 | 928 | enum io_wq_cancel io_wq_cancel_cb(struct io_wq *wq, work_cancel_fn *cancel, |
4f26bda1 | 929 | void *data, bool cancel_all) |
771b53d0 | 930 | { |
2293b419 | 931 | struct io_cb_cancel_data match = { |
4f26bda1 PB |
932 | .fn = cancel, |
933 | .data = data, | |
934 | .cancel_all = cancel_all, | |
00bcda13 | 935 | }; |
3fc50ab5 | 936 | int node; |
771b53d0 | 937 | |
f4c2665e PB |
938 | /* |
939 | * First check pending list, if we're lucky we can just remove it | |
940 | * from there. CANCEL_OK means that the work is returned as-new, | |
941 | * no completion will be posted for it. | |
942 | */ | |
3fc50ab5 JH |
943 | for_each_node(node) { |
944 | struct io_wqe *wqe = wq->wqes[node]; | |
771b53d0 | 945 | |
4f26bda1 PB |
946 | io_wqe_cancel_pending_work(wqe, &match); |
947 | if (match.nr_pending && !match.cancel_all) | |
f4c2665e | 948 | return IO_WQ_CANCEL_OK; |
771b53d0 JA |
949 | } |
950 | ||
f4c2665e PB |
951 | /* |
952 | * Now check if a free (going busy) or busy worker has the work | |
953 | * currently running. If we find it there, we'll return CANCEL_RUNNING | |
954 | * as an indication that we attempt to signal cancellation. The | |
955 | * completion will run normally in this case. | |
956 | */ | |
957 | for_each_node(node) { | |
958 | struct io_wqe *wqe = wq->wqes[node]; | |
959 | ||
4f26bda1 PB |
960 | io_wqe_cancel_running_work(wqe, &match); |
961 | if (match.nr_running && !match.cancel_all) | |
f4c2665e PB |
962 | return IO_WQ_CANCEL_RUNNING; |
963 | } | |
964 | ||
4f26bda1 PB |
965 | if (match.nr_running) |
966 | return IO_WQ_CANCEL_RUNNING; | |
967 | if (match.nr_pending) | |
968 | return IO_WQ_CANCEL_OK; | |
f4c2665e | 969 | return IO_WQ_CANCEL_NOTFOUND; |
771b53d0 JA |
970 | } |
971 | ||
e941894e JA |
972 | static int io_wqe_hash_wake(struct wait_queue_entry *wait, unsigned mode, |
973 | int sync, void *key) | |
974 | { | |
975 | struct io_wqe *wqe = container_of(wait, struct io_wqe, wait); | |
976 | int ret; | |
977 | ||
978 | list_del_init(&wait->entry); | |
979 | ||
980 | rcu_read_lock(); | |
981 | ret = io_wqe_activate_free_worker(wqe); | |
982 | rcu_read_unlock(); | |
983 | ||
984 | if (!ret) | |
985 | wake_up_process(wqe->wq->manager); | |
986 | ||
987 | return 1; | |
988 | } | |
989 | ||
576a347b | 990 | struct io_wq *io_wq_create(unsigned bounded, struct io_wq_data *data) |
771b53d0 | 991 | { |
3fc50ab5 | 992 | int ret = -ENOMEM, node; |
771b53d0 JA |
993 | struct io_wq *wq; |
994 | ||
f5fa38c5 | 995 | if (WARN_ON_ONCE(!data->free_work || !data->do_work)) |
e9fd9396 PB |
996 | return ERR_PTR(-EINVAL); |
997 | ||
ad6e005c | 998 | wq = kzalloc(sizeof(*wq), GFP_KERNEL); |
771b53d0 JA |
999 | if (!wq) |
1000 | return ERR_PTR(-ENOMEM); | |
1001 | ||
3fc50ab5 | 1002 | wq->wqes = kcalloc(nr_node_ids, sizeof(struct io_wqe *), GFP_KERNEL); |
43c01fbe JA |
1003 | if (!wq->wqes) |
1004 | goto err_wq; | |
1005 | ||
1006 | ret = cpuhp_state_add_instance_nocalls(io_wq_online, &wq->cpuhp_node); | |
1007 | if (ret) | |
1008 | goto err_wqes; | |
771b53d0 | 1009 | |
e941894e JA |
1010 | refcount_inc(&data->hash->refs); |
1011 | wq->hash = data->hash; | |
e9fd9396 | 1012 | wq->free_work = data->free_work; |
f5fa38c5 | 1013 | wq->do_work = data->do_work; |
7d723065 | 1014 | |
43c01fbe | 1015 | ret = -ENOMEM; |
3fc50ab5 | 1016 | for_each_node(node) { |
771b53d0 | 1017 | struct io_wqe *wqe; |
7563439a | 1018 | int alloc_node = node; |
771b53d0 | 1019 | |
7563439a JA |
1020 | if (!node_online(alloc_node)) |
1021 | alloc_node = NUMA_NO_NODE; | |
1022 | wqe = kzalloc_node(sizeof(struct io_wqe), GFP_KERNEL, alloc_node); | |
771b53d0 | 1023 | if (!wqe) |
3fc50ab5 JH |
1024 | goto err; |
1025 | wq->wqes[node] = wqe; | |
7563439a | 1026 | wqe->node = alloc_node; |
c5def4ab JA |
1027 | wqe->acct[IO_WQ_ACCT_BOUND].max_workers = bounded; |
1028 | atomic_set(&wqe->acct[IO_WQ_ACCT_BOUND].nr_running, 0); | |
728f13e7 | 1029 | wqe->acct[IO_WQ_ACCT_UNBOUND].max_workers = |
c5def4ab | 1030 | task_rlimit(current, RLIMIT_NPROC); |
c5def4ab | 1031 | atomic_set(&wqe->acct[IO_WQ_ACCT_UNBOUND].nr_running, 0); |
e941894e JA |
1032 | wqe->wait.func = io_wqe_hash_wake; |
1033 | INIT_LIST_HEAD(&wqe->wait.entry); | |
771b53d0 | 1034 | wqe->wq = wq; |
95da8465 | 1035 | raw_spin_lock_init(&wqe->lock); |
6206f0e1 | 1036 | INIT_WQ_LIST(&wqe->work_list); |
021d1cdd | 1037 | INIT_HLIST_NULLS_HEAD(&wqe->free_list, 0); |
e61df66c | 1038 | INIT_LIST_HEAD(&wqe->all_list); |
771b53d0 JA |
1039 | } |
1040 | ||
3bfe6106 | 1041 | wq->task_pid = current->pid; |
dbf99620 | 1042 | init_completion(&wq->started); |
d364d9e5 | 1043 | init_completion(&wq->exited); |
3bfe6106 | 1044 | refcount_set(&wq->refs, 1); |
771b53d0 | 1045 | |
fb3a1f6c JA |
1046 | init_completion(&wq->worker_done); |
1047 | atomic_set(&wq->worker_refs, 0); | |
1048 | ||
4fb6ac32 JA |
1049 | ret = io_wq_fork_manager(wq); |
1050 | if (!ret) | |
771b53d0 | 1051 | return wq; |
771b53d0 | 1052 | |
b60fda60 | 1053 | err: |
dc7bbc9e | 1054 | io_wq_put_hash(data->hash); |
43c01fbe | 1055 | cpuhp_state_remove_instance_nocalls(io_wq_online, &wq->cpuhp_node); |
3fc50ab5 JH |
1056 | for_each_node(node) |
1057 | kfree(wq->wqes[node]); | |
43c01fbe | 1058 | err_wqes: |
b60fda60 | 1059 | kfree(wq->wqes); |
43c01fbe | 1060 | err_wq: |
b60fda60 | 1061 | kfree(wq); |
771b53d0 JA |
1062 | return ERR_PTR(ret); |
1063 | } | |
1064 | ||
afcc4015 JA |
1065 | static void io_wq_destroy_manager(struct io_wq *wq) |
1066 | { | |
1067 | if (wq->manager) { | |
1068 | wake_up_process(wq->manager); | |
1069 | wait_for_completion(&wq->exited); | |
1070 | put_task_struct(wq->manager); | |
1071 | wq->manager = NULL; | |
1072 | } | |
1073 | } | |
1074 | ||
4fb6ac32 | 1075 | static void io_wq_destroy(struct io_wq *wq) |
771b53d0 | 1076 | { |
3fc50ab5 | 1077 | int node; |
771b53d0 | 1078 | |
43c01fbe JA |
1079 | cpuhp_state_remove_instance_nocalls(io_wq_online, &wq->cpuhp_node); |
1080 | ||
b60fda60 | 1081 | set_bit(IO_WQ_BIT_EXIT, &wq->state); |
afcc4015 | 1082 | io_wq_destroy_manager(wq); |
771b53d0 | 1083 | |
e941894e JA |
1084 | spin_lock_irq(&wq->hash->wait.lock); |
1085 | for_each_node(node) { | |
1086 | struct io_wqe *wqe = wq->wqes[node]; | |
1087 | ||
1088 | list_del_init(&wqe->wait.entry); | |
1089 | kfree(wqe); | |
1090 | } | |
1091 | spin_unlock_irq(&wq->hash->wait.lock); | |
1092 | io_wq_put_hash(wq->hash); | |
771b53d0 JA |
1093 | kfree(wq->wqes); |
1094 | kfree(wq); | |
4fb6ac32 JA |
1095 | } |
1096 | ||
1097 | void io_wq_put(struct io_wq *wq) | |
1098 | { | |
1099 | if (refcount_dec_and_test(&wq->refs)) | |
1100 | io_wq_destroy(wq); | |
771b53d0 | 1101 | } |
848f7e18 | 1102 | |
afcc4015 JA |
1103 | void io_wq_put_and_exit(struct io_wq *wq) |
1104 | { | |
1105 | set_bit(IO_WQ_BIT_EXIT, &wq->state); | |
1106 | io_wq_destroy_manager(wq); | |
1107 | io_wq_put(wq); | |
1108 | } | |
1109 | ||
43c01fbe JA |
1110 | static bool io_wq_worker_affinity(struct io_worker *worker, void *data) |
1111 | { | |
1112 | struct task_struct *task = worker->task; | |
1113 | struct rq_flags rf; | |
1114 | struct rq *rq; | |
1115 | ||
1116 | rq = task_rq_lock(task, &rf); | |
1117 | do_set_cpus_allowed(task, cpumask_of_node(worker->wqe->node)); | |
1118 | task->flags |= PF_NO_SETAFFINITY; | |
1119 | task_rq_unlock(rq, task, &rf); | |
1120 | return false; | |
1121 | } | |
1122 | ||
1123 | static int io_wq_cpu_online(unsigned int cpu, struct hlist_node *node) | |
1124 | { | |
1125 | struct io_wq *wq = hlist_entry_safe(node, struct io_wq, cpuhp_node); | |
1126 | int i; | |
1127 | ||
1128 | rcu_read_lock(); | |
1129 | for_each_node(i) | |
1130 | io_wq_for_each_worker(wq->wqes[i], io_wq_worker_affinity, NULL); | |
1131 | rcu_read_unlock(); | |
1132 | return 0; | |
1133 | } | |
1134 | ||
1135 | static __init int io_wq_init(void) | |
1136 | { | |
1137 | int ret; | |
1138 | ||
1139 | ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "io-wq/online", | |
1140 | io_wq_cpu_online, NULL); | |
1141 | if (ret < 0) | |
1142 | return ret; | |
1143 | io_wq_online = ret; | |
1144 | return 0; | |
1145 | } | |
1146 | subsys_initcall(io_wq_init); |