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