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