]>
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> | |
771b53d0 JA |
12 | #include <linux/percpu.h> |
13 | #include <linux/slab.h> | |
771b53d0 | 14 | #include <linux/rculist_nulls.h> |
43c01fbe | 15 | #include <linux/cpu.h> |
3bfe6106 | 16 | #include <linux/tracehook.h> |
771b53d0 JA |
17 | |
18 | #include "io-wq.h" | |
19 | ||
20 | #define WORKER_IDLE_TIMEOUT (5 * HZ) | |
21 | ||
22 | enum { | |
23 | IO_WORKER_F_UP = 1, /* up and active */ | |
24 | IO_WORKER_F_RUNNING = 2, /* account as running */ | |
25 | IO_WORKER_F_FREE = 4, /* worker on free list */ | |
145cc8c6 JA |
26 | IO_WORKER_F_FIXED = 8, /* static idle worker */ |
27 | IO_WORKER_F_BOUND = 16, /* is doing bounded work */ | |
771b53d0 JA |
28 | }; |
29 | ||
30 | enum { | |
31 | IO_WQ_BIT_EXIT = 0, /* wq exiting */ | |
771b53d0 JA |
32 | }; |
33 | ||
34 | enum { | |
35 | IO_WQE_FLAG_STALLED = 1, /* stalled on hash */ | |
36 | }; | |
37 | ||
38 | /* | |
39 | * One for each thread in a wqe pool | |
40 | */ | |
41 | struct io_worker { | |
42 | refcount_t ref; | |
43 | unsigned flags; | |
44 | struct hlist_nulls_node nulls_node; | |
e61df66c | 45 | struct list_head all_list; |
771b53d0 | 46 | struct task_struct *task; |
771b53d0 | 47 | struct io_wqe *wqe; |
36c2f922 | 48 | |
771b53d0 | 49 | struct io_wq_work *cur_work; |
36c2f922 | 50 | spinlock_t lock; |
771b53d0 | 51 | |
eb2de941 JA |
52 | struct completion ref_done; |
53 | ||
d3e9f732 JA |
54 | unsigned long create_state; |
55 | struct callback_head create_work; | |
56 | int create_index; | |
57 | ||
771b53d0 | 58 | struct rcu_head rcu; |
771b53d0 JA |
59 | }; |
60 | ||
771b53d0 JA |
61 | #if BITS_PER_LONG == 64 |
62 | #define IO_WQ_HASH_ORDER 6 | |
63 | #else | |
64 | #define IO_WQ_HASH_ORDER 5 | |
65 | #endif | |
66 | ||
86f3cd1b PB |
67 | #define IO_WQ_NR_HASH_BUCKETS (1u << IO_WQ_HASH_ORDER) |
68 | ||
c5def4ab JA |
69 | struct io_wqe_acct { |
70 | unsigned nr_workers; | |
71 | unsigned max_workers; | |
685fe7fe | 72 | int index; |
c5def4ab JA |
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]; |
0e03496d JA |
101 | |
102 | cpumask_var_t cpu_mask; | |
771b53d0 JA |
103 | }; |
104 | ||
105 | /* | |
106 | * Per io_wq state | |
107 | */ | |
108 | struct io_wq { | |
771b53d0 | 109 | unsigned long state; |
771b53d0 | 110 | |
e9fd9396 | 111 | free_work_fn *free_work; |
f5fa38c5 | 112 | io_wq_work_fn *do_work; |
7d723065 | 113 | |
e941894e JA |
114 | struct io_wq_hash *hash; |
115 | ||
fb3a1f6c JA |
116 | atomic_t worker_refs; |
117 | struct completion worker_done; | |
118 | ||
43c01fbe | 119 | struct hlist_node cpuhp_node; |
3bfe6106 | 120 | |
685fe7fe | 121 | struct task_struct *task; |
c7f405d6 PB |
122 | |
123 | struct io_wqe *wqes[]; | |
771b53d0 JA |
124 | }; |
125 | ||
43c01fbe JA |
126 | static enum cpuhp_state io_wq_online; |
127 | ||
f0127254 JA |
128 | struct io_cb_cancel_data { |
129 | work_cancel_fn *fn; | |
130 | void *data; | |
131 | int nr_running; | |
132 | int nr_pending; | |
133 | bool cancel_all; | |
134 | }; | |
135 | ||
47cae0c7 | 136 | static void create_io_worker(struct io_wq *wq, struct io_wqe *wqe, int index, bool first); |
83d6c393 | 137 | static void io_wqe_dec_running(struct io_worker *worker); |
f0127254 | 138 | |
771b53d0 JA |
139 | static bool io_worker_get(struct io_worker *worker) |
140 | { | |
141 | return refcount_inc_not_zero(&worker->ref); | |
142 | } | |
143 | ||
144 | static void io_worker_release(struct io_worker *worker) | |
145 | { | |
146 | if (refcount_dec_and_test(&worker->ref)) | |
eb2de941 | 147 | complete(&worker->ref_done); |
771b53d0 JA |
148 | } |
149 | ||
8418f22a PB |
150 | static inline struct io_wqe_acct *io_get_acct(struct io_wqe *wqe, bool bound) |
151 | { | |
152 | return &wqe->acct[bound ? IO_WQ_ACCT_BOUND : IO_WQ_ACCT_UNBOUND]; | |
153 | } | |
154 | ||
c5def4ab JA |
155 | static inline struct io_wqe_acct *io_work_get_acct(struct io_wqe *wqe, |
156 | struct io_wq_work *work) | |
157 | { | |
8418f22a | 158 | return io_get_acct(wqe, !(work->flags & IO_WQ_WORK_UNBOUND)); |
c5def4ab JA |
159 | } |
160 | ||
958234d5 | 161 | static inline struct io_wqe_acct *io_wqe_get_acct(struct io_worker *worker) |
c5def4ab | 162 | { |
8418f22a | 163 | return io_get_acct(worker->wqe, worker->flags & IO_WORKER_F_BOUND); |
c5def4ab JA |
164 | } |
165 | ||
685fe7fe JA |
166 | static void io_worker_ref_put(struct io_wq *wq) |
167 | { | |
168 | if (atomic_dec_and_test(&wq->worker_refs)) | |
169 | complete(&wq->worker_done); | |
170 | } | |
171 | ||
771b53d0 JA |
172 | static void io_worker_exit(struct io_worker *worker) |
173 | { | |
174 | struct io_wqe *wqe = worker->wqe; | |
958234d5 | 175 | struct io_wqe_acct *acct = io_wqe_get_acct(worker); |
771b53d0 | 176 | |
eb2de941 JA |
177 | if (refcount_dec_and_test(&worker->ref)) |
178 | complete(&worker->ref_done); | |
179 | wait_for_completion(&worker->ref_done); | |
771b53d0 | 180 | |
95da8465 | 181 | raw_spin_lock_irq(&wqe->lock); |
83d6c393 | 182 | if (worker->flags & IO_WORKER_F_FREE) |
bf1daa4b | 183 | hlist_nulls_del_rcu(&worker->nulls_node); |
e61df66c | 184 | list_del_rcu(&worker->all_list); |
c5def4ab | 185 | acct->nr_workers--; |
83d6c393 JA |
186 | preempt_disable(); |
187 | io_wqe_dec_running(worker); | |
188 | worker->flags = 0; | |
189 | current->flags &= ~PF_IO_WORKER; | |
190 | preempt_enable(); | |
95da8465 | 191 | raw_spin_unlock_irq(&wqe->lock); |
771b53d0 | 192 | |
364b05fd | 193 | kfree_rcu(worker, rcu); |
685fe7fe | 194 | io_worker_ref_put(wqe->wq); |
46fe18b1 | 195 | do_exit(0); |
771b53d0 JA |
196 | } |
197 | ||
c5def4ab JA |
198 | static inline bool io_wqe_run_queue(struct io_wqe *wqe) |
199 | __must_hold(wqe->lock) | |
200 | { | |
6206f0e1 JA |
201 | if (!wq_list_empty(&wqe->work_list) && |
202 | !(wqe->flags & IO_WQE_FLAG_STALLED)) | |
c5def4ab JA |
203 | return true; |
204 | return false; | |
205 | } | |
206 | ||
207 | /* | |
208 | * Check head of free list for an available worker. If one isn't available, | |
685fe7fe | 209 | * caller must create one. |
c5def4ab JA |
210 | */ |
211 | static bool io_wqe_activate_free_worker(struct io_wqe *wqe) | |
212 | __must_hold(RCU) | |
213 | { | |
214 | struct hlist_nulls_node *n; | |
215 | struct io_worker *worker; | |
216 | ||
83d6c393 JA |
217 | /* |
218 | * Iterate free_list and see if we can find an idle worker to | |
219 | * activate. If a given worker is on the free_list but in the process | |
220 | * of exiting, keep trying. | |
221 | */ | |
222 | hlist_nulls_for_each_entry_rcu(worker, n, &wqe->free_list, nulls_node) { | |
223 | if (!io_worker_get(worker)) | |
224 | continue; | |
225 | if (wake_up_process(worker->task)) { | |
226 | io_worker_release(worker); | |
227 | return true; | |
228 | } | |
c5def4ab | 229 | io_worker_release(worker); |
c5def4ab JA |
230 | } |
231 | ||
232 | return false; | |
233 | } | |
234 | ||
235 | /* | |
236 | * We need a worker. If we find a free one, we're good. If not, and we're | |
685fe7fe | 237 | * below the max number of workers, create one. |
c5def4ab JA |
238 | */ |
239 | static void io_wqe_wake_worker(struct io_wqe *wqe, struct io_wqe_acct *acct) | |
240 | { | |
241 | bool ret; | |
242 | ||
243 | /* | |
244 | * Most likely an attempt to queue unbounded work on an io_wq that | |
245 | * wasn't setup with any unbounded workers. | |
246 | */ | |
e6ab8991 PB |
247 | if (unlikely(!acct->max_workers)) |
248 | pr_warn_once("io-wq is not configured for unbound workers"); | |
c5def4ab JA |
249 | |
250 | rcu_read_lock(); | |
251 | ret = io_wqe_activate_free_worker(wqe); | |
252 | rcu_read_unlock(); | |
253 | ||
3d4e4fac | 254 | if (!ret) { |
47cae0c7 | 255 | bool do_create = false, first = false; |
3d4e4fac HX |
256 | |
257 | raw_spin_lock_irq(&wqe->lock); | |
258 | if (acct->nr_workers < acct->max_workers) { | |
47cae0c7 HX |
259 | if (!acct->nr_workers) |
260 | first = true; | |
3d4e4fac HX |
261 | acct->nr_workers++; |
262 | do_create = true; | |
263 | } | |
264 | raw_spin_unlock_irq(&wqe->lock); | |
79dca184 HX |
265 | if (do_create) { |
266 | atomic_inc(&acct->nr_running); | |
267 | atomic_inc(&wqe->wq->worker_refs); | |
47cae0c7 | 268 | create_io_worker(wqe->wq, wqe, acct->index, first); |
79dca184 | 269 | } |
685fe7fe | 270 | } |
c5def4ab JA |
271 | } |
272 | ||
958234d5 | 273 | static void io_wqe_inc_running(struct io_worker *worker) |
c5def4ab | 274 | { |
958234d5 | 275 | struct io_wqe_acct *acct = io_wqe_get_acct(worker); |
c5def4ab JA |
276 | |
277 | atomic_inc(&acct->nr_running); | |
278 | } | |
279 | ||
685fe7fe JA |
280 | static void create_worker_cb(struct callback_head *cb) |
281 | { | |
d3e9f732 | 282 | struct io_worker *worker; |
685fe7fe | 283 | struct io_wq *wq; |
21698274 HX |
284 | struct io_wqe *wqe; |
285 | struct io_wqe_acct *acct; | |
47cae0c7 | 286 | bool do_create = false, first = false; |
685fe7fe | 287 | |
d3e9f732 JA |
288 | worker = container_of(cb, struct io_worker, create_work); |
289 | wqe = worker->wqe; | |
21698274 | 290 | wq = wqe->wq; |
d3e9f732 | 291 | acct = &wqe->acct[worker->create_index]; |
21698274 | 292 | raw_spin_lock_irq(&wqe->lock); |
49e7f0c7 | 293 | if (acct->nr_workers < acct->max_workers) { |
47cae0c7 HX |
294 | if (!acct->nr_workers) |
295 | first = true; | |
21698274 | 296 | acct->nr_workers++; |
49e7f0c7 HX |
297 | do_create = true; |
298 | } | |
21698274 | 299 | raw_spin_unlock_irq(&wqe->lock); |
49e7f0c7 | 300 | if (do_create) { |
d3e9f732 | 301 | create_io_worker(wq, wqe, worker->create_index, first); |
49e7f0c7 HX |
302 | } else { |
303 | atomic_dec(&acct->nr_running); | |
304 | io_worker_ref_put(wq); | |
305 | } | |
d3e9f732 JA |
306 | clear_bit_unlock(0, &worker->create_state); |
307 | io_worker_release(worker); | |
685fe7fe JA |
308 | } |
309 | ||
d3e9f732 JA |
310 | static void io_queue_worker_create(struct io_wqe *wqe, struct io_worker *worker, |
311 | struct io_wqe_acct *acct) | |
685fe7fe | 312 | { |
685fe7fe JA |
313 | struct io_wq *wq = wqe->wq; |
314 | ||
315 | /* raced with exit, just ignore create call */ | |
316 | if (test_bit(IO_WQ_BIT_EXIT, &wq->state)) | |
317 | goto fail; | |
d3e9f732 JA |
318 | if (!io_worker_get(worker)) |
319 | goto fail; | |
320 | /* | |
321 | * create_state manages ownership of create_work/index. We should | |
322 | * only need one entry per worker, as the worker going to sleep | |
323 | * will trigger the condition, and waking will clear it once it | |
324 | * runs the task_work. | |
325 | */ | |
326 | if (test_bit(0, &worker->create_state) || | |
327 | test_and_set_bit_lock(0, &worker->create_state)) | |
328 | goto fail_release; | |
685fe7fe | 329 | |
d3e9f732 JA |
330 | init_task_work(&worker->create_work, create_worker_cb); |
331 | worker->create_index = acct->index; | |
332 | if (!task_work_add(wq->task, &worker->create_work, TWA_SIGNAL)) | |
333 | return; | |
334 | clear_bit_unlock(0, &worker->create_state); | |
335 | fail_release: | |
336 | io_worker_release(worker); | |
685fe7fe JA |
337 | fail: |
338 | atomic_dec(&acct->nr_running); | |
339 | io_worker_ref_put(wq); | |
340 | } | |
341 | ||
958234d5 | 342 | static void io_wqe_dec_running(struct io_worker *worker) |
c5def4ab JA |
343 | __must_hold(wqe->lock) |
344 | { | |
958234d5 JA |
345 | struct io_wqe_acct *acct = io_wqe_get_acct(worker); |
346 | struct io_wqe *wqe = worker->wqe; | |
c5def4ab | 347 | |
685fe7fe JA |
348 | if (!(worker->flags & IO_WORKER_F_UP)) |
349 | return; | |
350 | ||
351 | if (atomic_dec_and_test(&acct->nr_running) && io_wqe_run_queue(wqe)) { | |
352 | atomic_inc(&acct->nr_running); | |
353 | atomic_inc(&wqe->wq->worker_refs); | |
d3e9f732 | 354 | io_queue_worker_create(wqe, worker, acct); |
685fe7fe | 355 | } |
c5def4ab JA |
356 | } |
357 | ||
771b53d0 JA |
358 | /* |
359 | * Worker will start processing some work. Move it to the busy list, if | |
360 | * it's currently on the freelist | |
361 | */ | |
362 | static void __io_worker_busy(struct io_wqe *wqe, struct io_worker *worker, | |
363 | struct io_wq_work *work) | |
364 | __must_hold(wqe->lock) | |
365 | { | |
c5def4ab JA |
366 | bool worker_bound, work_bound; |
367 | ||
417b5052 HX |
368 | BUILD_BUG_ON((IO_WQ_ACCT_UNBOUND ^ IO_WQ_ACCT_BOUND) != 1); |
369 | ||
771b53d0 JA |
370 | if (worker->flags & IO_WORKER_F_FREE) { |
371 | worker->flags &= ~IO_WORKER_F_FREE; | |
372 | hlist_nulls_del_init_rcu(&worker->nulls_node); | |
771b53d0 | 373 | } |
c5def4ab JA |
374 | |
375 | /* | |
376 | * If worker is moving from bound to unbound (or vice versa), then | |
377 | * ensure we update the running accounting. | |
378 | */ | |
b2e9c7d6 DC |
379 | worker_bound = (worker->flags & IO_WORKER_F_BOUND) != 0; |
380 | work_bound = (work->flags & IO_WQ_WORK_UNBOUND) == 0; | |
381 | if (worker_bound != work_bound) { | |
417b5052 | 382 | int index = work_bound ? IO_WQ_ACCT_UNBOUND : IO_WQ_ACCT_BOUND; |
958234d5 | 383 | io_wqe_dec_running(worker); |
417b5052 HX |
384 | worker->flags ^= IO_WORKER_F_BOUND; |
385 | wqe->acct[index].nr_workers--; | |
386 | wqe->acct[index ^ 1].nr_workers++; | |
958234d5 | 387 | io_wqe_inc_running(worker); |
c5def4ab | 388 | } |
771b53d0 JA |
389 | } |
390 | ||
391 | /* | |
392 | * No work, worker going to sleep. Move to freelist, and unuse mm if we | |
393 | * have one attached. Dropping the mm may potentially sleep, so we drop | |
394 | * the lock in that case and return success. Since the caller has to | |
395 | * retry the loop in that case (we changed task state), we don't regrab | |
396 | * the lock if we return success. | |
397 | */ | |
c6d77d92 | 398 | static void __io_worker_idle(struct io_wqe *wqe, struct io_worker *worker) |
771b53d0 JA |
399 | __must_hold(wqe->lock) |
400 | { | |
401 | if (!(worker->flags & IO_WORKER_F_FREE)) { | |
402 | worker->flags |= IO_WORKER_F_FREE; | |
021d1cdd | 403 | hlist_nulls_add_head_rcu(&worker->nulls_node, &wqe->free_list); |
771b53d0 | 404 | } |
771b53d0 JA |
405 | } |
406 | ||
60cf46ae PB |
407 | static inline unsigned int io_get_work_hash(struct io_wq_work *work) |
408 | { | |
409 | return work->flags >> IO_WQ_HASH_SHIFT; | |
410 | } | |
411 | ||
e941894e JA |
412 | static void io_wait_on_hash(struct io_wqe *wqe, unsigned int hash) |
413 | { | |
414 | struct io_wq *wq = wqe->wq; | |
415 | ||
416 | spin_lock(&wq->hash->wait.lock); | |
417 | if (list_empty(&wqe->wait.entry)) { | |
418 | __add_wait_queue(&wq->hash->wait, &wqe->wait); | |
419 | if (!test_bit(hash, &wq->hash->map)) { | |
420 | __set_current_state(TASK_RUNNING); | |
421 | list_del_init(&wqe->wait.entry); | |
422 | } | |
423 | } | |
424 | spin_unlock(&wq->hash->wait.lock); | |
425 | } | |
426 | ||
60cf46ae | 427 | static struct io_wq_work *io_get_next_work(struct io_wqe *wqe) |
771b53d0 JA |
428 | __must_hold(wqe->lock) |
429 | { | |
6206f0e1 | 430 | struct io_wq_work_node *node, *prev; |
86f3cd1b | 431 | struct io_wq_work *work, *tail; |
e941894e | 432 | unsigned int stall_hash = -1U; |
771b53d0 | 433 | |
6206f0e1 | 434 | wq_list_for_each(node, prev, &wqe->work_list) { |
e941894e JA |
435 | unsigned int hash; |
436 | ||
6206f0e1 JA |
437 | work = container_of(node, struct io_wq_work, list); |
438 | ||
771b53d0 | 439 | /* not hashed, can run anytime */ |
8766dd51 | 440 | if (!io_wq_is_hashed(work)) { |
86f3cd1b | 441 | wq_list_del(&wqe->work_list, node, prev); |
771b53d0 JA |
442 | return work; |
443 | } | |
444 | ||
60cf46ae | 445 | hash = io_get_work_hash(work); |
e941894e JA |
446 | /* all items with this hash lie in [work, tail] */ |
447 | tail = wqe->hash_tail[hash]; | |
448 | ||
449 | /* hashed, can run if not already running */ | |
450 | if (!test_and_set_bit(hash, &wqe->wq->hash->map)) { | |
86f3cd1b PB |
451 | wqe->hash_tail[hash] = NULL; |
452 | wq_list_cut(&wqe->work_list, &tail->list, prev); | |
771b53d0 JA |
453 | return work; |
454 | } | |
e941894e JA |
455 | if (stall_hash == -1U) |
456 | stall_hash = hash; | |
457 | /* fast forward to a next hash, for-each will fix up @prev */ | |
458 | node = &tail->list; | |
459 | } | |
460 | ||
461 | if (stall_hash != -1U) { | |
462 | raw_spin_unlock(&wqe->lock); | |
463 | io_wait_on_hash(wqe, stall_hash); | |
464 | raw_spin_lock(&wqe->lock); | |
771b53d0 JA |
465 | } |
466 | ||
467 | return NULL; | |
468 | } | |
469 | ||
00ddff43 | 470 | static bool io_flush_signals(void) |
cccf0ee8 | 471 | { |
0b8cfa97 | 472 | if (unlikely(test_thread_flag(TIF_NOTIFY_SIGNAL))) { |
00ddff43 | 473 | __set_current_state(TASK_RUNNING); |
0b8cfa97 | 474 | tracehook_notify_signal(); |
00ddff43 | 475 | return true; |
cccf0ee8 | 476 | } |
00ddff43 | 477 | return false; |
dc026a73 PB |
478 | } |
479 | ||
480 | static void io_assign_current_work(struct io_worker *worker, | |
481 | struct io_wq_work *work) | |
482 | { | |
d78298e7 | 483 | if (work) { |
3bfe6106 | 484 | io_flush_signals(); |
d78298e7 PB |
485 | cond_resched(); |
486 | } | |
dc026a73 PB |
487 | |
488 | spin_lock_irq(&worker->lock); | |
489 | worker->cur_work = work; | |
490 | spin_unlock_irq(&worker->lock); | |
491 | } | |
492 | ||
60cf46ae PB |
493 | static void io_wqe_enqueue(struct io_wqe *wqe, struct io_wq_work *work); |
494 | ||
771b53d0 JA |
495 | static void io_worker_handle_work(struct io_worker *worker) |
496 | __releases(wqe->lock) | |
497 | { | |
771b53d0 JA |
498 | struct io_wqe *wqe = worker->wqe; |
499 | struct io_wq *wq = wqe->wq; | |
c60eb049 | 500 | bool do_kill = test_bit(IO_WQ_BIT_EXIT, &wq->state); |
771b53d0 JA |
501 | |
502 | do { | |
86f3cd1b | 503 | struct io_wq_work *work; |
f462fd36 | 504 | get_next: |
771b53d0 JA |
505 | /* |
506 | * If we got some work, mark us as busy. If we didn't, but | |
507 | * the list isn't empty, it means we stalled on hashed work. | |
508 | * Mark us stalled so we don't keep looking for work when we | |
509 | * can't make progress, any work completion or insertion will | |
510 | * clear the stalled flag. | |
511 | */ | |
60cf46ae | 512 | work = io_get_next_work(wqe); |
771b53d0 JA |
513 | if (work) |
514 | __io_worker_busy(wqe, worker, work); | |
6206f0e1 | 515 | else if (!wq_list_empty(&wqe->work_list)) |
771b53d0 JA |
516 | wqe->flags |= IO_WQE_FLAG_STALLED; |
517 | ||
95da8465 | 518 | raw_spin_unlock_irq(&wqe->lock); |
771b53d0 JA |
519 | if (!work) |
520 | break; | |
58e39319 | 521 | io_assign_current_work(worker, work); |
e941894e | 522 | __set_current_state(TASK_RUNNING); |
36c2f922 | 523 | |
dc026a73 PB |
524 | /* handle a whole dependent link */ |
525 | do { | |
5280f7e5 | 526 | struct io_wq_work *next_hashed, *linked; |
b089ed39 | 527 | unsigned int hash = io_get_work_hash(work); |
dc026a73 | 528 | |
86f3cd1b | 529 | next_hashed = wq_next_work(work); |
c60eb049 PB |
530 | |
531 | if (unlikely(do_kill) && (work->flags & IO_WQ_WORK_UNBOUND)) | |
532 | work->flags |= IO_WQ_WORK_CANCEL; | |
5280f7e5 PB |
533 | wq->do_work(work); |
534 | io_assign_current_work(worker, NULL); | |
dc026a73 | 535 | |
5280f7e5 | 536 | linked = wq->free_work(work); |
86f3cd1b PB |
537 | work = next_hashed; |
538 | if (!work && linked && !io_wq_is_hashed(linked)) { | |
539 | work = linked; | |
540 | linked = NULL; | |
541 | } | |
542 | io_assign_current_work(worker, work); | |
86f3cd1b PB |
543 | if (linked) |
544 | io_wqe_enqueue(wqe, linked); | |
545 | ||
546 | if (hash != -1U && !next_hashed) { | |
e941894e JA |
547 | clear_bit(hash, &wq->hash->map); |
548 | if (wq_has_sleeper(&wq->hash->wait)) | |
549 | wake_up(&wq->hash->wait); | |
95da8465 | 550 | raw_spin_lock_irq(&wqe->lock); |
dc026a73 | 551 | wqe->flags &= ~IO_WQE_FLAG_STALLED; |
f462fd36 PB |
552 | /* skip unnecessary unlock-lock wqe->lock */ |
553 | if (!work) | |
554 | goto get_next; | |
95da8465 | 555 | raw_spin_unlock_irq(&wqe->lock); |
7d723065 | 556 | } |
58e39319 | 557 | } while (work); |
7d723065 | 558 | |
95da8465 | 559 | raw_spin_lock_irq(&wqe->lock); |
771b53d0 JA |
560 | } while (1); |
561 | } | |
562 | ||
771b53d0 JA |
563 | static int io_wqe_worker(void *data) |
564 | { | |
565 | struct io_worker *worker = data; | |
566 | struct io_wqe *wqe = worker->wqe; | |
567 | struct io_wq *wq = wqe->wq; | |
46fe18b1 | 568 | char buf[TASK_COMM_LEN]; |
771b53d0 | 569 | |
46fe18b1 | 570 | worker->flags |= (IO_WORKER_F_UP | IO_WORKER_F_RUNNING); |
46fe18b1 | 571 | |
685fe7fe | 572 | snprintf(buf, sizeof(buf), "iou-wrk-%d", wq->task->pid); |
46fe18b1 | 573 | set_task_comm(current, buf); |
771b53d0 JA |
574 | |
575 | while (!test_bit(IO_WQ_BIT_EXIT, &wq->state)) { | |
16efa4fc JA |
576 | long ret; |
577 | ||
506d95ff | 578 | set_current_state(TASK_INTERRUPTIBLE); |
e995d512 | 579 | loop: |
95da8465 | 580 | raw_spin_lock_irq(&wqe->lock); |
771b53d0 | 581 | if (io_wqe_run_queue(wqe)) { |
771b53d0 | 582 | io_worker_handle_work(worker); |
e995d512 | 583 | goto loop; |
771b53d0 | 584 | } |
c6d77d92 | 585 | __io_worker_idle(wqe, worker); |
95da8465 | 586 | raw_spin_unlock_irq(&wqe->lock); |
00ddff43 JA |
587 | if (io_flush_signals()) |
588 | continue; | |
16efa4fc | 589 | ret = schedule_timeout(WORKER_IDLE_TIMEOUT); |
dbe1bdbb JA |
590 | if (signal_pending(current)) { |
591 | struct ksignal ksig; | |
592 | ||
593 | if (!get_signal(&ksig)) | |
594 | continue; | |
3bfe6106 | 595 | break; |
dbe1bdbb JA |
596 | } |
597 | if (ret) | |
598 | continue; | |
771b53d0 | 599 | /* timed out, exit unless we're the fixed worker */ |
769e6837 | 600 | if (!(worker->flags & IO_WORKER_F_FIXED)) |
771b53d0 JA |
601 | break; |
602 | } | |
603 | ||
771b53d0 | 604 | if (test_bit(IO_WQ_BIT_EXIT, &wq->state)) { |
95da8465 | 605 | raw_spin_lock_irq(&wqe->lock); |
e587227b | 606 | io_worker_handle_work(worker); |
771b53d0 JA |
607 | } |
608 | ||
609 | io_worker_exit(worker); | |
610 | return 0; | |
611 | } | |
612 | ||
771b53d0 JA |
613 | /* |
614 | * Called when a worker is scheduled in. Mark us as currently running. | |
615 | */ | |
616 | void io_wq_worker_running(struct task_struct *tsk) | |
617 | { | |
3bfe6106 | 618 | struct io_worker *worker = tsk->pf_io_worker; |
771b53d0 | 619 | |
3bfe6106 JA |
620 | if (!worker) |
621 | return; | |
771b53d0 JA |
622 | if (!(worker->flags & IO_WORKER_F_UP)) |
623 | return; | |
624 | if (worker->flags & IO_WORKER_F_RUNNING) | |
625 | return; | |
626 | worker->flags |= IO_WORKER_F_RUNNING; | |
958234d5 | 627 | io_wqe_inc_running(worker); |
771b53d0 JA |
628 | } |
629 | ||
630 | /* | |
631 | * Called when worker is going to sleep. If there are no workers currently | |
685fe7fe | 632 | * running and we have work pending, wake up a free one or create a new one. |
771b53d0 JA |
633 | */ |
634 | void io_wq_worker_sleeping(struct task_struct *tsk) | |
635 | { | |
3bfe6106 | 636 | struct io_worker *worker = tsk->pf_io_worker; |
771b53d0 | 637 | |
3bfe6106 JA |
638 | if (!worker) |
639 | return; | |
771b53d0 JA |
640 | if (!(worker->flags & IO_WORKER_F_UP)) |
641 | return; | |
642 | if (!(worker->flags & IO_WORKER_F_RUNNING)) | |
643 | return; | |
644 | ||
645 | worker->flags &= ~IO_WORKER_F_RUNNING; | |
646 | ||
3bfe6106 | 647 | raw_spin_lock_irq(&worker->wqe->lock); |
958234d5 | 648 | io_wqe_dec_running(worker); |
3bfe6106 | 649 | raw_spin_unlock_irq(&worker->wqe->lock); |
771b53d0 JA |
650 | } |
651 | ||
47cae0c7 | 652 | static void create_io_worker(struct io_wq *wq, struct io_wqe *wqe, int index, bool first) |
3bfe6106 | 653 | { |
46fe18b1 | 654 | struct io_wqe_acct *acct = &wqe->acct[index]; |
3bfe6106 | 655 | struct io_worker *worker; |
46fe18b1 | 656 | struct task_struct *tsk; |
3bfe6106 | 657 | |
8b3e78b5 JA |
658 | __set_current_state(TASK_RUNNING); |
659 | ||
3bfe6106 JA |
660 | worker = kzalloc_node(sizeof(*worker), GFP_KERNEL, wqe->node); |
661 | if (!worker) | |
685fe7fe | 662 | goto fail; |
3bfe6106 JA |
663 | |
664 | refcount_set(&worker->ref, 1); | |
665 | worker->nulls_node.pprev = NULL; | |
666 | worker->wqe = wqe; | |
667 | spin_lock_init(&worker->lock); | |
eb2de941 | 668 | init_completion(&worker->ref_done); |
3bfe6106 | 669 | |
46fe18b1 JA |
670 | tsk = create_io_thread(io_wqe_worker, worker, wqe->node); |
671 | if (IS_ERR(tsk)) { | |
3bfe6106 | 672 | kfree(worker); |
685fe7fe JA |
673 | fail: |
674 | atomic_dec(&acct->nr_running); | |
3d4e4fac HX |
675 | raw_spin_lock_irq(&wqe->lock); |
676 | acct->nr_workers--; | |
677 | raw_spin_unlock_irq(&wqe->lock); | |
685fe7fe JA |
678 | io_worker_ref_put(wq); |
679 | return; | |
3bfe6106 | 680 | } |
46fe18b1 JA |
681 | |
682 | tsk->pf_io_worker = worker; | |
683 | worker->task = tsk; | |
0e03496d | 684 | set_cpus_allowed_ptr(tsk, wqe->cpu_mask); |
e22bc9b4 | 685 | tsk->flags |= PF_NO_SETAFFINITY; |
46fe18b1 JA |
686 | |
687 | raw_spin_lock_irq(&wqe->lock); | |
688 | hlist_nulls_add_head_rcu(&worker->nulls_node, &wqe->free_list); | |
689 | list_add_tail_rcu(&worker->all_list, &wqe->all_list); | |
690 | worker->flags |= IO_WORKER_F_FREE; | |
691 | if (index == IO_WQ_ACCT_BOUND) | |
692 | worker->flags |= IO_WORKER_F_BOUND; | |
47cae0c7 | 693 | if (first && (worker->flags & IO_WORKER_F_BOUND)) |
46fe18b1 | 694 | worker->flags |= IO_WORKER_F_FIXED; |
46fe18b1 JA |
695 | raw_spin_unlock_irq(&wqe->lock); |
696 | wake_up_new_task(tsk); | |
771b53d0 JA |
697 | } |
698 | ||
c4068bf8 HD |
699 | /* |
700 | * Iterate the passed in list and call the specific function for each | |
701 | * worker that isn't exiting | |
702 | */ | |
703 | static bool io_wq_for_each_worker(struct io_wqe *wqe, | |
704 | bool (*func)(struct io_worker *, void *), | |
705 | void *data) | |
706 | { | |
707 | struct io_worker *worker; | |
708 | bool ret = false; | |
709 | ||
710 | list_for_each_entry_rcu(worker, &wqe->all_list, all_list) { | |
711 | if (io_worker_get(worker)) { | |
712 | /* no task if node is/was offline */ | |
713 | if (worker->task) | |
714 | ret = func(worker, data); | |
715 | io_worker_release(worker); | |
716 | if (ret) | |
717 | break; | |
718 | } | |
719 | } | |
720 | ||
721 | return ret; | |
722 | } | |
723 | ||
724 | static bool io_wq_worker_wake(struct io_worker *worker, void *data) | |
725 | { | |
46fe18b1 | 726 | set_notify_signal(worker->task); |
c4068bf8 HD |
727 | wake_up_process(worker->task); |
728 | return false; | |
729 | } | |
730 | ||
f0127254 JA |
731 | static bool io_wq_work_match_all(struct io_wq_work *work, void *data) |
732 | { | |
733 | return true; | |
734 | } | |
735 | ||
e9fd9396 | 736 | static void io_run_cancel(struct io_wq_work *work, struct io_wqe *wqe) |
fc04c39b | 737 | { |
e9fd9396 PB |
738 | struct io_wq *wq = wqe->wq; |
739 | ||
fc04c39b | 740 | do { |
fc04c39b | 741 | work->flags |= IO_WQ_WORK_CANCEL; |
5280f7e5 PB |
742 | wq->do_work(work); |
743 | work = wq->free_work(work); | |
fc04c39b PB |
744 | } while (work); |
745 | } | |
746 | ||
86f3cd1b PB |
747 | static void io_wqe_insert_work(struct io_wqe *wqe, struct io_wq_work *work) |
748 | { | |
749 | unsigned int hash; | |
750 | struct io_wq_work *tail; | |
751 | ||
752 | if (!io_wq_is_hashed(work)) { | |
753 | append: | |
754 | wq_list_add_tail(&work->list, &wqe->work_list); | |
755 | return; | |
756 | } | |
757 | ||
758 | hash = io_get_work_hash(work); | |
759 | tail = wqe->hash_tail[hash]; | |
760 | wqe->hash_tail[hash] = work; | |
761 | if (!tail) | |
762 | goto append; | |
763 | ||
764 | wq_list_add_after(&work->list, &tail->list, &wqe->work_list); | |
765 | } | |
766 | ||
771b53d0 JA |
767 | static void io_wqe_enqueue(struct io_wqe *wqe, struct io_wq_work *work) |
768 | { | |
c5def4ab | 769 | struct io_wqe_acct *acct = io_work_get_acct(wqe, work); |
895e2ca0 | 770 | int work_flags; |
771b53d0 JA |
771 | unsigned long flags; |
772 | ||
991468dc JA |
773 | /* |
774 | * If io-wq is exiting for this task, or if the request has explicitly | |
775 | * been marked as one that should not get executed, cancel it here. | |
776 | */ | |
777 | if (test_bit(IO_WQ_BIT_EXIT, &wqe->wq->state) || | |
778 | (work->flags & IO_WQ_WORK_CANCEL)) { | |
70e35125 | 779 | io_run_cancel(work, wqe); |
4fb6ac32 JA |
780 | return; |
781 | } | |
782 | ||
895e2ca0 | 783 | work_flags = work->flags; |
95da8465 | 784 | raw_spin_lock_irqsave(&wqe->lock, flags); |
86f3cd1b | 785 | io_wqe_insert_work(wqe, work); |
771b53d0 | 786 | wqe->flags &= ~IO_WQE_FLAG_STALLED; |
95da8465 | 787 | raw_spin_unlock_irqrestore(&wqe->lock, flags); |
771b53d0 | 788 | |
895e2ca0 JA |
789 | if ((work_flags & IO_WQ_WORK_CONCURRENT) || |
790 | !atomic_read(&acct->nr_running)) | |
c5def4ab | 791 | io_wqe_wake_worker(wqe, acct); |
771b53d0 JA |
792 | } |
793 | ||
794 | void io_wq_enqueue(struct io_wq *wq, struct io_wq_work *work) | |
795 | { | |
796 | struct io_wqe *wqe = wq->wqes[numa_node_id()]; | |
797 | ||
798 | io_wqe_enqueue(wqe, work); | |
799 | } | |
800 | ||
801 | /* | |
8766dd51 PB |
802 | * Work items that hash to the same value will not be done in parallel. |
803 | * Used to limit concurrent writes, generally hashed by inode. | |
771b53d0 | 804 | */ |
8766dd51 | 805 | void io_wq_hash_work(struct io_wq_work *work, void *val) |
771b53d0 | 806 | { |
8766dd51 | 807 | unsigned int bit; |
771b53d0 JA |
808 | |
809 | bit = hash_ptr(val, IO_WQ_HASH_ORDER); | |
810 | work->flags |= (IO_WQ_WORK_HASHED | (bit << IO_WQ_HASH_SHIFT)); | |
771b53d0 JA |
811 | } |
812 | ||
2293b419 | 813 | static bool io_wq_worker_cancel(struct io_worker *worker, void *data) |
62755e35 | 814 | { |
2293b419 | 815 | struct io_cb_cancel_data *match = data; |
6f72653e | 816 | unsigned long flags; |
62755e35 JA |
817 | |
818 | /* | |
819 | * Hold the lock to avoid ->cur_work going out of scope, caller | |
36c2f922 | 820 | * may dereference the passed in work. |
62755e35 | 821 | */ |
36c2f922 | 822 | spin_lock_irqsave(&worker->lock, flags); |
62755e35 | 823 | if (worker->cur_work && |
2293b419 | 824 | match->fn(worker->cur_work, match->data)) { |
3bfe6106 | 825 | set_notify_signal(worker->task); |
4f26bda1 | 826 | match->nr_running++; |
771b53d0 | 827 | } |
36c2f922 | 828 | spin_unlock_irqrestore(&worker->lock, flags); |
771b53d0 | 829 | |
4f26bda1 | 830 | return match->nr_running && !match->cancel_all; |
771b53d0 JA |
831 | } |
832 | ||
204361a7 PB |
833 | static inline void io_wqe_remove_pending(struct io_wqe *wqe, |
834 | struct io_wq_work *work, | |
835 | struct io_wq_work_node *prev) | |
836 | { | |
837 | unsigned int hash = io_get_work_hash(work); | |
838 | struct io_wq_work *prev_work = NULL; | |
839 | ||
840 | if (io_wq_is_hashed(work) && work == wqe->hash_tail[hash]) { | |
841 | if (prev) | |
842 | prev_work = container_of(prev, struct io_wq_work, list); | |
843 | if (prev_work && io_get_work_hash(prev_work) == hash) | |
844 | wqe->hash_tail[hash] = prev_work; | |
845 | else | |
846 | wqe->hash_tail[hash] = NULL; | |
847 | } | |
848 | wq_list_del(&wqe->work_list, &work->list, prev); | |
849 | } | |
850 | ||
4f26bda1 | 851 | static void io_wqe_cancel_pending_work(struct io_wqe *wqe, |
f4c2665e | 852 | struct io_cb_cancel_data *match) |
771b53d0 | 853 | { |
6206f0e1 | 854 | struct io_wq_work_node *node, *prev; |
771b53d0 | 855 | struct io_wq_work *work; |
6f72653e | 856 | unsigned long flags; |
771b53d0 | 857 | |
4f26bda1 | 858 | retry: |
95da8465 | 859 | raw_spin_lock_irqsave(&wqe->lock, flags); |
6206f0e1 JA |
860 | wq_list_for_each(node, prev, &wqe->work_list) { |
861 | work = container_of(node, struct io_wq_work, list); | |
4f26bda1 PB |
862 | if (!match->fn(work, match->data)) |
863 | continue; | |
204361a7 | 864 | io_wqe_remove_pending(wqe, work, prev); |
95da8465 | 865 | raw_spin_unlock_irqrestore(&wqe->lock, flags); |
4f26bda1 PB |
866 | io_run_cancel(work, wqe); |
867 | match->nr_pending++; | |
868 | if (!match->cancel_all) | |
869 | return; | |
870 | ||
871 | /* not safe to continue after unlock */ | |
872 | goto retry; | |
771b53d0 | 873 | } |
95da8465 | 874 | raw_spin_unlock_irqrestore(&wqe->lock, flags); |
f4c2665e PB |
875 | } |
876 | ||
4f26bda1 | 877 | static void io_wqe_cancel_running_work(struct io_wqe *wqe, |
f4c2665e PB |
878 | struct io_cb_cancel_data *match) |
879 | { | |
771b53d0 | 880 | rcu_read_lock(); |
4f26bda1 | 881 | io_wq_for_each_worker(wqe, io_wq_worker_cancel, match); |
771b53d0 | 882 | rcu_read_unlock(); |
771b53d0 JA |
883 | } |
884 | ||
2293b419 | 885 | enum io_wq_cancel io_wq_cancel_cb(struct io_wq *wq, work_cancel_fn *cancel, |
4f26bda1 | 886 | void *data, bool cancel_all) |
771b53d0 | 887 | { |
2293b419 | 888 | struct io_cb_cancel_data match = { |
4f26bda1 PB |
889 | .fn = cancel, |
890 | .data = data, | |
891 | .cancel_all = cancel_all, | |
00bcda13 | 892 | }; |
3fc50ab5 | 893 | int node; |
771b53d0 | 894 | |
f4c2665e PB |
895 | /* |
896 | * First check pending list, if we're lucky we can just remove it | |
897 | * from there. CANCEL_OK means that the work is returned as-new, | |
898 | * no completion will be posted for it. | |
899 | */ | |
3fc50ab5 JH |
900 | for_each_node(node) { |
901 | struct io_wqe *wqe = wq->wqes[node]; | |
771b53d0 | 902 | |
4f26bda1 PB |
903 | io_wqe_cancel_pending_work(wqe, &match); |
904 | if (match.nr_pending && !match.cancel_all) | |
f4c2665e | 905 | return IO_WQ_CANCEL_OK; |
771b53d0 JA |
906 | } |
907 | ||
f4c2665e PB |
908 | /* |
909 | * Now check if a free (going busy) or busy worker has the work | |
910 | * currently running. If we find it there, we'll return CANCEL_RUNNING | |
911 | * as an indication that we attempt to signal cancellation. The | |
912 | * completion will run normally in this case. | |
913 | */ | |
914 | for_each_node(node) { | |
915 | struct io_wqe *wqe = wq->wqes[node]; | |
916 | ||
4f26bda1 PB |
917 | io_wqe_cancel_running_work(wqe, &match); |
918 | if (match.nr_running && !match.cancel_all) | |
f4c2665e PB |
919 | return IO_WQ_CANCEL_RUNNING; |
920 | } | |
921 | ||
4f26bda1 PB |
922 | if (match.nr_running) |
923 | return IO_WQ_CANCEL_RUNNING; | |
924 | if (match.nr_pending) | |
925 | return IO_WQ_CANCEL_OK; | |
f4c2665e | 926 | return IO_WQ_CANCEL_NOTFOUND; |
771b53d0 JA |
927 | } |
928 | ||
e941894e JA |
929 | static int io_wqe_hash_wake(struct wait_queue_entry *wait, unsigned mode, |
930 | int sync, void *key) | |
931 | { | |
932 | struct io_wqe *wqe = container_of(wait, struct io_wqe, wait); | |
e941894e JA |
933 | |
934 | list_del_init(&wait->entry); | |
935 | ||
936 | rcu_read_lock(); | |
685fe7fe | 937 | io_wqe_activate_free_worker(wqe); |
e941894e | 938 | rcu_read_unlock(); |
e941894e JA |
939 | return 1; |
940 | } | |
941 | ||
576a347b | 942 | struct io_wq *io_wq_create(unsigned bounded, struct io_wq_data *data) |
771b53d0 | 943 | { |
b1b2fc35 | 944 | int ret, node; |
771b53d0 JA |
945 | struct io_wq *wq; |
946 | ||
f5fa38c5 | 947 | if (WARN_ON_ONCE(!data->free_work || !data->do_work)) |
e9fd9396 | 948 | return ERR_PTR(-EINVAL); |
e6ab8991 PB |
949 | if (WARN_ON_ONCE(!bounded)) |
950 | return ERR_PTR(-EINVAL); | |
e9fd9396 | 951 | |
c7f405d6 | 952 | wq = kzalloc(struct_size(wq, wqes, nr_node_ids), GFP_KERNEL); |
771b53d0 JA |
953 | if (!wq) |
954 | return ERR_PTR(-ENOMEM); | |
43c01fbe JA |
955 | ret = cpuhp_state_add_instance_nocalls(io_wq_online, &wq->cpuhp_node); |
956 | if (ret) | |
c7f405d6 | 957 | goto err_wq; |
771b53d0 | 958 | |
e941894e JA |
959 | refcount_inc(&data->hash->refs); |
960 | wq->hash = data->hash; | |
e9fd9396 | 961 | wq->free_work = data->free_work; |
f5fa38c5 | 962 | wq->do_work = data->do_work; |
7d723065 | 963 | |
43c01fbe | 964 | ret = -ENOMEM; |
3fc50ab5 | 965 | for_each_node(node) { |
771b53d0 | 966 | struct io_wqe *wqe; |
7563439a | 967 | int alloc_node = node; |
771b53d0 | 968 | |
7563439a JA |
969 | if (!node_online(alloc_node)) |
970 | alloc_node = NUMA_NO_NODE; | |
971 | wqe = kzalloc_node(sizeof(struct io_wqe), GFP_KERNEL, alloc_node); | |
771b53d0 | 972 | if (!wqe) |
3fc50ab5 | 973 | goto err; |
0e03496d JA |
974 | if (!alloc_cpumask_var(&wqe->cpu_mask, GFP_KERNEL)) |
975 | goto err; | |
976 | cpumask_copy(wqe->cpu_mask, cpumask_of_node(node)); | |
3fc50ab5 | 977 | wq->wqes[node] = wqe; |
7563439a | 978 | wqe->node = alloc_node; |
685fe7fe JA |
979 | wqe->acct[IO_WQ_ACCT_BOUND].index = IO_WQ_ACCT_BOUND; |
980 | wqe->acct[IO_WQ_ACCT_UNBOUND].index = IO_WQ_ACCT_UNBOUND; | |
c5def4ab JA |
981 | wqe->acct[IO_WQ_ACCT_BOUND].max_workers = bounded; |
982 | atomic_set(&wqe->acct[IO_WQ_ACCT_BOUND].nr_running, 0); | |
728f13e7 | 983 | wqe->acct[IO_WQ_ACCT_UNBOUND].max_workers = |
c5def4ab | 984 | task_rlimit(current, RLIMIT_NPROC); |
c5def4ab | 985 | atomic_set(&wqe->acct[IO_WQ_ACCT_UNBOUND].nr_running, 0); |
e941894e JA |
986 | wqe->wait.func = io_wqe_hash_wake; |
987 | INIT_LIST_HEAD(&wqe->wait.entry); | |
771b53d0 | 988 | wqe->wq = wq; |
95da8465 | 989 | raw_spin_lock_init(&wqe->lock); |
6206f0e1 | 990 | INIT_WQ_LIST(&wqe->work_list); |
021d1cdd | 991 | INIT_HLIST_NULLS_HEAD(&wqe->free_list, 0); |
e61df66c | 992 | INIT_LIST_HEAD(&wqe->all_list); |
771b53d0 JA |
993 | } |
994 | ||
685fe7fe | 995 | wq->task = get_task_struct(data->task); |
685fe7fe JA |
996 | atomic_set(&wq->worker_refs, 1); |
997 | init_completion(&wq->worker_done); | |
998 | return wq; | |
b60fda60 | 999 | err: |
dc7bbc9e | 1000 | io_wq_put_hash(data->hash); |
43c01fbe | 1001 | cpuhp_state_remove_instance_nocalls(io_wq_online, &wq->cpuhp_node); |
0e03496d JA |
1002 | for_each_node(node) { |
1003 | if (!wq->wqes[node]) | |
1004 | continue; | |
1005 | free_cpumask_var(wq->wqes[node]->cpu_mask); | |
3fc50ab5 | 1006 | kfree(wq->wqes[node]); |
0e03496d | 1007 | } |
43c01fbe | 1008 | err_wq: |
b60fda60 | 1009 | kfree(wq); |
771b53d0 JA |
1010 | return ERR_PTR(ret); |
1011 | } | |
1012 | ||
c80ca470 JA |
1013 | static bool io_task_work_match(struct callback_head *cb, void *data) |
1014 | { | |
d3e9f732 | 1015 | struct io_worker *worker; |
c80ca470 JA |
1016 | |
1017 | if (cb->func != create_worker_cb) | |
1018 | return false; | |
d3e9f732 JA |
1019 | worker = container_of(cb, struct io_worker, create_work); |
1020 | return worker->wqe->wq == data; | |
c80ca470 JA |
1021 | } |
1022 | ||
17a91051 PB |
1023 | void io_wq_exit_start(struct io_wq *wq) |
1024 | { | |
1025 | set_bit(IO_WQ_BIT_EXIT, &wq->state); | |
1026 | } | |
1027 | ||
685fe7fe | 1028 | static void io_wq_exit_workers(struct io_wq *wq) |
afcc4015 | 1029 | { |
685fe7fe JA |
1030 | struct callback_head *cb; |
1031 | int node; | |
1032 | ||
685fe7fe JA |
1033 | if (!wq->task) |
1034 | return; | |
1035 | ||
c80ca470 | 1036 | while ((cb = task_work_cancel_match(wq->task, io_task_work_match, wq)) != NULL) { |
d3e9f732 | 1037 | struct io_worker *worker; |
685fe7fe | 1038 | |
d3e9f732 JA |
1039 | worker = container_of(cb, struct io_worker, create_work); |
1040 | atomic_dec(&worker->wqe->acct[worker->create_index].nr_running); | |
685fe7fe | 1041 | io_worker_ref_put(wq); |
d3e9f732 JA |
1042 | clear_bit_unlock(0, &worker->create_state); |
1043 | io_worker_release(worker); | |
685fe7fe JA |
1044 | } |
1045 | ||
1046 | rcu_read_lock(); | |
1047 | for_each_node(node) { | |
1048 | struct io_wqe *wqe = wq->wqes[node]; | |
1049 | ||
1050 | io_wq_for_each_worker(wqe, io_wq_worker_wake, NULL); | |
afcc4015 | 1051 | } |
685fe7fe JA |
1052 | rcu_read_unlock(); |
1053 | io_worker_ref_put(wq); | |
1054 | wait_for_completion(&wq->worker_done); | |
3743c172 Z |
1055 | |
1056 | for_each_node(node) { | |
1057 | spin_lock_irq(&wq->hash->wait.lock); | |
1058 | list_del_init(&wq->wqes[node]->wait.entry); | |
1059 | spin_unlock_irq(&wq->hash->wait.lock); | |
1060 | } | |
685fe7fe JA |
1061 | put_task_struct(wq->task); |
1062 | wq->task = NULL; | |
afcc4015 JA |
1063 | } |
1064 | ||
4fb6ac32 | 1065 | static void io_wq_destroy(struct io_wq *wq) |
771b53d0 | 1066 | { |
3fc50ab5 | 1067 | int node; |
771b53d0 | 1068 | |
43c01fbe JA |
1069 | cpuhp_state_remove_instance_nocalls(io_wq_online, &wq->cpuhp_node); |
1070 | ||
e941894e JA |
1071 | for_each_node(node) { |
1072 | struct io_wqe *wqe = wq->wqes[node]; | |
f5d2d23b JA |
1073 | struct io_cb_cancel_data match = { |
1074 | .fn = io_wq_work_match_all, | |
1075 | .cancel_all = true, | |
1076 | }; | |
1077 | io_wqe_cancel_pending_work(wqe, &match); | |
0e03496d | 1078 | free_cpumask_var(wqe->cpu_mask); |
e941894e JA |
1079 | kfree(wqe); |
1080 | } | |
e941894e | 1081 | io_wq_put_hash(wq->hash); |
771b53d0 | 1082 | kfree(wq); |
4fb6ac32 JA |
1083 | } |
1084 | ||
afcc4015 JA |
1085 | void io_wq_put_and_exit(struct io_wq *wq) |
1086 | { | |
17a91051 PB |
1087 | WARN_ON_ONCE(!test_bit(IO_WQ_BIT_EXIT, &wq->state)); |
1088 | ||
685fe7fe | 1089 | io_wq_exit_workers(wq); |
382cb030 | 1090 | io_wq_destroy(wq); |
afcc4015 JA |
1091 | } |
1092 | ||
0e03496d JA |
1093 | struct online_data { |
1094 | unsigned int cpu; | |
1095 | bool online; | |
1096 | }; | |
1097 | ||
43c01fbe JA |
1098 | static bool io_wq_worker_affinity(struct io_worker *worker, void *data) |
1099 | { | |
0e03496d | 1100 | struct online_data *od = data; |
e0051d7d | 1101 | |
0e03496d JA |
1102 | if (od->online) |
1103 | cpumask_set_cpu(od->cpu, worker->wqe->cpu_mask); | |
1104 | else | |
1105 | cpumask_clear_cpu(od->cpu, worker->wqe->cpu_mask); | |
43c01fbe JA |
1106 | return false; |
1107 | } | |
1108 | ||
0e03496d | 1109 | static int __io_wq_cpu_online(struct io_wq *wq, unsigned int cpu, bool online) |
43c01fbe | 1110 | { |
0e03496d JA |
1111 | struct online_data od = { |
1112 | .cpu = cpu, | |
1113 | .online = online | |
1114 | }; | |
43c01fbe JA |
1115 | int i; |
1116 | ||
1117 | rcu_read_lock(); | |
1118 | for_each_node(i) | |
0e03496d | 1119 | io_wq_for_each_worker(wq->wqes[i], io_wq_worker_affinity, &od); |
43c01fbe JA |
1120 | rcu_read_unlock(); |
1121 | return 0; | |
1122 | } | |
1123 | ||
0e03496d JA |
1124 | static int io_wq_cpu_online(unsigned int cpu, struct hlist_node *node) |
1125 | { | |
1126 | struct io_wq *wq = hlist_entry_safe(node, struct io_wq, cpuhp_node); | |
1127 | ||
1128 | return __io_wq_cpu_online(wq, cpu, true); | |
1129 | } | |
1130 | ||
1131 | static int io_wq_cpu_offline(unsigned int cpu, struct hlist_node *node) | |
1132 | { | |
1133 | struct io_wq *wq = hlist_entry_safe(node, struct io_wq, cpuhp_node); | |
1134 | ||
1135 | return __io_wq_cpu_online(wq, cpu, false); | |
1136 | } | |
1137 | ||
fe76421d JA |
1138 | int io_wq_cpu_affinity(struct io_wq *wq, cpumask_var_t mask) |
1139 | { | |
1140 | int i; | |
1141 | ||
1142 | rcu_read_lock(); | |
1143 | for_each_node(i) { | |
1144 | struct io_wqe *wqe = wq->wqes[i]; | |
1145 | ||
1146 | if (mask) | |
1147 | cpumask_copy(wqe->cpu_mask, mask); | |
1148 | else | |
1149 | cpumask_copy(wqe->cpu_mask, cpumask_of_node(i)); | |
1150 | } | |
1151 | rcu_read_unlock(); | |
1152 | return 0; | |
1153 | } | |
1154 | ||
2e480058 JA |
1155 | /* |
1156 | * Set max number of unbounded workers, returns old value. If new_count is 0, | |
1157 | * then just return the old value. | |
1158 | */ | |
1159 | int io_wq_max_workers(struct io_wq *wq, int *new_count) | |
1160 | { | |
1161 | int i, node, prev = 0; | |
1162 | ||
1163 | for (i = 0; i < 2; i++) { | |
1164 | if (new_count[i] > task_rlimit(current, RLIMIT_NPROC)) | |
1165 | new_count[i] = task_rlimit(current, RLIMIT_NPROC); | |
1166 | } | |
1167 | ||
1168 | rcu_read_lock(); | |
1169 | for_each_node(node) { | |
1170 | struct io_wqe_acct *acct; | |
1171 | ||
1172 | for (i = 0; i < 2; i++) { | |
1173 | acct = &wq->wqes[node]->acct[i]; | |
1174 | prev = max_t(int, acct->max_workers, prev); | |
1175 | if (new_count[i]) | |
1176 | acct->max_workers = new_count[i]; | |
1177 | new_count[i] = prev; | |
1178 | } | |
1179 | } | |
1180 | rcu_read_unlock(); | |
1181 | return 0; | |
1182 | } | |
1183 | ||
43c01fbe JA |
1184 | static __init int io_wq_init(void) |
1185 | { | |
1186 | int ret; | |
1187 | ||
1188 | ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "io-wq/online", | |
0e03496d | 1189 | io_wq_cpu_online, io_wq_cpu_offline); |
43c01fbe JA |
1190 | if (ret < 0) |
1191 | return ret; | |
1192 | io_wq_online = ret; | |
1193 | return 0; | |
1194 | } | |
1195 | subsys_initcall(io_wq_init); |