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