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