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