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