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