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