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