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