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