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