blob: 9174007ce107b6ff9034e44001e1b89c14c22c5b [file] [log] [blame]
Jens Axboe771b53d02019-10-22 10:25:58 -06001// 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 */
Jens Axboec5def4a2019-11-07 11:41:16 -070030 IO_WORKER_F_BOUND = 32, /* is doing bounded work */
Jens Axboe771b53d02019-10-22 10:25:58 -060031};
32
33enum {
34 IO_WQ_BIT_EXIT = 0, /* wq exiting */
35 IO_WQ_BIT_CANCEL = 1, /* cancel work on list */
36};
37
38enum {
39 IO_WQE_FLAG_STALLED = 1, /* stalled on hash */
40};
41
42/*
43 * One for each thread in a wqe pool
44 */
45struct io_worker {
46 refcount_t ref;
47 unsigned flags;
48 struct hlist_nulls_node nulls_node;
Jens Axboee61df662019-11-13 13:54:49 -070049 struct list_head all_list;
Jens Axboe771b53d02019-10-22 10:25:58 -060050 struct task_struct *task;
51 wait_queue_head_t wait;
52 struct io_wqe *wqe;
Jens Axboe36c2f922019-11-13 09:43:34 -070053
Jens Axboe771b53d02019-10-22 10:25:58 -060054 struct io_wq_work *cur_work;
Jens Axboe36c2f922019-11-13 09:43:34 -070055 spinlock_t lock;
Jens Axboe771b53d02019-10-22 10:25:58 -060056
57 struct rcu_head rcu;
58 struct mm_struct *mm;
Jens Axboefcb323c2019-10-24 12:39:47 -060059 struct files_struct *restore_files;
Jens Axboe771b53d02019-10-22 10:25:58 -060060};
61
Jens Axboe771b53d02019-10-22 10:25:58 -060062#if BITS_PER_LONG == 64
63#define IO_WQ_HASH_ORDER 6
64#else
65#define IO_WQ_HASH_ORDER 5
66#endif
67
Jens Axboec5def4a2019-11-07 11:41:16 -070068struct io_wqe_acct {
69 unsigned nr_workers;
70 unsigned max_workers;
71 atomic_t nr_running;
72};
73
74enum {
75 IO_WQ_ACCT_BOUND,
76 IO_WQ_ACCT_UNBOUND,
77};
78
Jens Axboe771b53d02019-10-22 10:25:58 -060079/*
80 * Per-node worker thread pool
81 */
82struct io_wqe {
83 struct {
84 spinlock_t lock;
85 struct list_head work_list;
86 unsigned long hash_map;
87 unsigned flags;
88 } ____cacheline_aligned_in_smp;
89
90 int node;
Jens Axboec5def4a2019-11-07 11:41:16 -070091 struct io_wqe_acct acct[2];
Jens Axboe771b53d02019-10-22 10:25:58 -060092
Jens Axboe021d1cd2019-11-14 08:00:41 -070093 struct hlist_nulls_head free_list;
94 struct hlist_nulls_head busy_list;
Jens Axboee61df662019-11-13 13:54:49 -070095 struct list_head all_list;
Jens Axboe771b53d02019-10-22 10:25:58 -060096
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;
106 unsigned nr_wqes;
107
Jens Axboe7d723062019-11-12 22:31:31 -0700108 get_work_fn *get_work;
109 put_work_fn *put_work;
110
Jens Axboe771b53d02019-10-22 10:25:58 -0600111 struct task_struct *manager;
Jens Axboec5def4a2019-11-07 11:41:16 -0700112 struct user_struct *user;
Jens Axboe771b53d02019-10-22 10:25:58 -0600113 struct mm_struct *mm;
114 refcount_t refs;
115 struct completion done;
116};
117
Jens Axboe771b53d02019-10-22 10:25:58 -0600118static 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{
Jens Axboefcb323c2019-10-24 12:39:47 -0600136 bool dropped_lock = false;
137
138 if (current->files != worker->restore_files) {
139 __acquire(&wqe->lock);
140 spin_unlock_irq(&wqe->lock);
141 dropped_lock = true;
142
143 task_lock(current);
144 current->files = worker->restore_files;
145 task_unlock(current);
146 }
147
Jens Axboe771b53d02019-10-22 10:25:58 -0600148 /*
149 * If we have an active mm, we need to drop the wq lock before unusing
150 * it. If we do, return true and let the caller retry the idle loop.
151 */
152 if (worker->mm) {
Jens Axboefcb323c2019-10-24 12:39:47 -0600153 if (!dropped_lock) {
154 __acquire(&wqe->lock);
155 spin_unlock_irq(&wqe->lock);
156 dropped_lock = true;
157 }
Jens Axboe771b53d02019-10-22 10:25:58 -0600158 __set_current_state(TASK_RUNNING);
159 set_fs(KERNEL_DS);
160 unuse_mm(worker->mm);
161 mmput(worker->mm);
162 worker->mm = NULL;
Jens Axboe771b53d02019-10-22 10:25:58 -0600163 }
164
Jens Axboefcb323c2019-10-24 12:39:47 -0600165 return dropped_lock;
Jens Axboe771b53d02019-10-22 10:25:58 -0600166}
167
Jens Axboec5def4a2019-11-07 11:41:16 -0700168static inline struct io_wqe_acct *io_work_get_acct(struct io_wqe *wqe,
169 struct io_wq_work *work)
170{
171 if (work->flags & IO_WQ_WORK_UNBOUND)
172 return &wqe->acct[IO_WQ_ACCT_UNBOUND];
173
174 return &wqe->acct[IO_WQ_ACCT_BOUND];
175}
176
177static inline struct io_wqe_acct *io_wqe_get_acct(struct io_wqe *wqe,
178 struct io_worker *worker)
179{
180 if (worker->flags & IO_WORKER_F_BOUND)
181 return &wqe->acct[IO_WQ_ACCT_BOUND];
182
183 return &wqe->acct[IO_WQ_ACCT_UNBOUND];
184}
185
Jens Axboe771b53d02019-10-22 10:25:58 -0600186static void io_worker_exit(struct io_worker *worker)
187{
188 struct io_wqe *wqe = worker->wqe;
Jens Axboec5def4a2019-11-07 11:41:16 -0700189 struct io_wqe_acct *acct = io_wqe_get_acct(wqe, worker);
190 unsigned nr_workers;
Jens Axboe771b53d02019-10-22 10:25:58 -0600191
192 /*
193 * If we're not at zero, someone else is holding a brief reference
194 * to the worker. Wait for that to go away.
195 */
196 set_current_state(TASK_INTERRUPTIBLE);
197 if (!refcount_dec_and_test(&worker->ref))
198 schedule();
199 __set_current_state(TASK_RUNNING);
200
201 preempt_disable();
202 current->flags &= ~PF_IO_WORKER;
203 if (worker->flags & IO_WORKER_F_RUNNING)
Jens Axboec5def4a2019-11-07 11:41:16 -0700204 atomic_dec(&acct->nr_running);
205 if (!(worker->flags & IO_WORKER_F_BOUND))
206 atomic_dec(&wqe->wq->user->processes);
Jens Axboe771b53d02019-10-22 10:25:58 -0600207 worker->flags = 0;
208 preempt_enable();
209
210 spin_lock_irq(&wqe->lock);
211 hlist_nulls_del_rcu(&worker->nulls_node);
Jens Axboee61df662019-11-13 13:54:49 -0700212 list_del_rcu(&worker->all_list);
Jens Axboe771b53d02019-10-22 10:25:58 -0600213 if (__io_worker_unuse(wqe, worker)) {
214 __release(&wqe->lock);
215 spin_lock_irq(&wqe->lock);
216 }
Jens Axboec5def4a2019-11-07 11:41:16 -0700217 acct->nr_workers--;
218 nr_workers = wqe->acct[IO_WQ_ACCT_BOUND].nr_workers +
219 wqe->acct[IO_WQ_ACCT_UNBOUND].nr_workers;
Jens Axboe771b53d02019-10-22 10:25:58 -0600220 spin_unlock_irq(&wqe->lock);
221
222 /* all workers gone, wq exit can proceed */
Jens Axboec5def4a2019-11-07 11:41:16 -0700223 if (!nr_workers && refcount_dec_and_test(&wqe->wq->refs))
Jens Axboe771b53d02019-10-22 10:25:58 -0600224 complete(&wqe->wq->done);
225
YueHaibing364b05f2019-11-02 15:55:01 +0800226 kfree_rcu(worker, rcu);
Jens Axboe771b53d02019-10-22 10:25:58 -0600227}
228
Jens Axboec5def4a2019-11-07 11:41:16 -0700229static inline bool io_wqe_run_queue(struct io_wqe *wqe)
230 __must_hold(wqe->lock)
231{
232 if (!list_empty(&wqe->work_list) && !(wqe->flags & IO_WQE_FLAG_STALLED))
233 return true;
234 return false;
235}
236
237/*
238 * Check head of free list for an available worker. If one isn't available,
239 * caller must wake up the wq manager to create one.
240 */
241static bool io_wqe_activate_free_worker(struct io_wqe *wqe)
242 __must_hold(RCU)
243{
244 struct hlist_nulls_node *n;
245 struct io_worker *worker;
246
Jens Axboe021d1cd2019-11-14 08:00:41 -0700247 n = rcu_dereference(hlist_nulls_first_rcu(&wqe->free_list));
Jens Axboec5def4a2019-11-07 11:41:16 -0700248 if (is_a_nulls(n))
249 return false;
250
251 worker = hlist_nulls_entry(n, struct io_worker, nulls_node);
252 if (io_worker_get(worker)) {
253 wake_up(&worker->wait);
254 io_worker_release(worker);
255 return true;
256 }
257
258 return false;
259}
260
261/*
262 * We need a worker. If we find a free one, we're good. If not, and we're
263 * below the max number of workers, wake up the manager to create one.
264 */
265static void io_wqe_wake_worker(struct io_wqe *wqe, struct io_wqe_acct *acct)
266{
267 bool ret;
268
269 /*
270 * Most likely an attempt to queue unbounded work on an io_wq that
271 * wasn't setup with any unbounded workers.
272 */
273 WARN_ON_ONCE(!acct->max_workers);
274
275 rcu_read_lock();
276 ret = io_wqe_activate_free_worker(wqe);
277 rcu_read_unlock();
278
279 if (!ret && acct->nr_workers < acct->max_workers)
280 wake_up_process(wqe->wq->manager);
281}
282
283static void io_wqe_inc_running(struct io_wqe *wqe, struct io_worker *worker)
284{
285 struct io_wqe_acct *acct = io_wqe_get_acct(wqe, worker);
286
287 atomic_inc(&acct->nr_running);
288}
289
290static void io_wqe_dec_running(struct io_wqe *wqe, struct io_worker *worker)
291 __must_hold(wqe->lock)
292{
293 struct io_wqe_acct *acct = io_wqe_get_acct(wqe, worker);
294
295 if (atomic_dec_and_test(&acct->nr_running) && io_wqe_run_queue(wqe))
296 io_wqe_wake_worker(wqe, acct);
297}
298
Jens Axboe771b53d02019-10-22 10:25:58 -0600299static void io_worker_start(struct io_wqe *wqe, struct io_worker *worker)
300{
301 allow_kernel_signal(SIGINT);
302
303 current->flags |= PF_IO_WORKER;
304
305 worker->flags |= (IO_WORKER_F_UP | IO_WORKER_F_RUNNING);
Jens Axboefcb323c2019-10-24 12:39:47 -0600306 worker->restore_files = current->files;
Jens Axboec5def4a2019-11-07 11:41:16 -0700307 io_wqe_inc_running(wqe, worker);
Jens Axboe771b53d02019-10-22 10:25:58 -0600308}
309
310/*
311 * Worker will start processing some work. Move it to the busy list, if
312 * it's currently on the freelist
313 */
314static void __io_worker_busy(struct io_wqe *wqe, struct io_worker *worker,
315 struct io_wq_work *work)
316 __must_hold(wqe->lock)
317{
Jens Axboec5def4a2019-11-07 11:41:16 -0700318 bool worker_bound, work_bound;
319
Jens Axboe771b53d02019-10-22 10:25:58 -0600320 if (worker->flags & IO_WORKER_F_FREE) {
321 worker->flags &= ~IO_WORKER_F_FREE;
322 hlist_nulls_del_init_rcu(&worker->nulls_node);
Jens Axboe021d1cd2019-11-14 08:00:41 -0700323 hlist_nulls_add_head_rcu(&worker->nulls_node, &wqe->busy_list);
Jens Axboe771b53d02019-10-22 10:25:58 -0600324 }
Jens Axboec5def4a2019-11-07 11:41:16 -0700325
326 /*
327 * If worker is moving from bound to unbound (or vice versa), then
328 * ensure we update the running accounting.
329 */
330 worker_bound = (worker->flags & IO_WORKER_F_BOUND) != 0;
331 work_bound = (work->flags & IO_WQ_WORK_UNBOUND) == 0;
332 if (worker_bound != work_bound) {
333 io_wqe_dec_running(wqe, worker);
334 if (work_bound) {
335 worker->flags |= IO_WORKER_F_BOUND;
336 wqe->acct[IO_WQ_ACCT_UNBOUND].nr_workers--;
337 wqe->acct[IO_WQ_ACCT_BOUND].nr_workers++;
338 atomic_dec(&wqe->wq->user->processes);
339 } else {
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_inc(&wqe->wq->user->processes);
344 }
345 io_wqe_inc_running(wqe, worker);
346 }
Jens Axboe771b53d02019-10-22 10:25:58 -0600347}
348
349/*
350 * No work, worker going to sleep. Move to freelist, and unuse mm if we
351 * have one attached. Dropping the mm may potentially sleep, so we drop
352 * the lock in that case and return success. Since the caller has to
353 * retry the loop in that case (we changed task state), we don't regrab
354 * the lock if we return success.
355 */
356static bool __io_worker_idle(struct io_wqe *wqe, struct io_worker *worker)
357 __must_hold(wqe->lock)
358{
359 if (!(worker->flags & IO_WORKER_F_FREE)) {
360 worker->flags |= IO_WORKER_F_FREE;
361 hlist_nulls_del_init_rcu(&worker->nulls_node);
Jens Axboe021d1cd2019-11-14 08:00:41 -0700362 hlist_nulls_add_head_rcu(&worker->nulls_node, &wqe->free_list);
Jens Axboe771b53d02019-10-22 10:25:58 -0600363 }
364
365 return __io_worker_unuse(wqe, worker);
366}
367
368static struct io_wq_work *io_get_next_work(struct io_wqe *wqe, unsigned *hash)
369 __must_hold(wqe->lock)
370{
371 struct io_wq_work *work;
372
373 list_for_each_entry(work, &wqe->work_list, list) {
374 /* not hashed, can run anytime */
375 if (!(work->flags & IO_WQ_WORK_HASHED)) {
376 list_del(&work->list);
377 return work;
378 }
379
380 /* hashed, can run if not already running */
381 *hash = work->flags >> IO_WQ_HASH_SHIFT;
382 if (!(wqe->hash_map & BIT_ULL(*hash))) {
383 wqe->hash_map |= BIT_ULL(*hash);
384 list_del(&work->list);
385 return work;
386 }
387 }
388
389 return NULL;
390}
391
392static void io_worker_handle_work(struct io_worker *worker)
393 __releases(wqe->lock)
394{
Jens Axboe7d723062019-11-12 22:31:31 -0700395 struct io_wq_work *work, *old_work = NULL, *put_work = NULL;
Jens Axboe771b53d02019-10-22 10:25:58 -0600396 struct io_wqe *wqe = worker->wqe;
397 struct io_wq *wq = wqe->wq;
398
399 do {
400 unsigned hash = -1U;
401
402 /*
Jens Axboe771b53d02019-10-22 10:25:58 -0600403 * If we got some work, mark us as busy. If we didn't, but
404 * the list isn't empty, it means we stalled on hashed work.
405 * Mark us stalled so we don't keep looking for work when we
406 * can't make progress, any work completion or insertion will
407 * clear the stalled flag.
408 */
409 work = io_get_next_work(wqe, &hash);
410 if (work)
411 __io_worker_busy(wqe, worker, work);
412 else if (!list_empty(&wqe->work_list))
413 wqe->flags |= IO_WQE_FLAG_STALLED;
414
415 spin_unlock_irq(&wqe->lock);
Jens Axboe7d723062019-11-12 22:31:31 -0700416 if (put_work && wq->put_work)
417 wq->put_work(old_work);
Jens Axboe771b53d02019-10-22 10:25:58 -0600418 if (!work)
419 break;
420next:
Jens Axboe36c2f922019-11-13 09:43:34 -0700421 /* flush any pending signals before assigning new work */
422 if (signal_pending(current))
423 flush_signals(current);
424
425 spin_lock_irq(&worker->lock);
426 worker->cur_work = work;
427 spin_unlock_irq(&worker->lock);
428
Jens Axboefcb323c2019-10-24 12:39:47 -0600429 if ((work->flags & IO_WQ_WORK_NEEDS_FILES) &&
430 current->files != work->files) {
431 task_lock(current);
432 current->files = work->files;
433 task_unlock(current);
434 }
Jens Axboe771b53d02019-10-22 10:25:58 -0600435 if ((work->flags & IO_WQ_WORK_NEEDS_USER) && !worker->mm &&
436 wq->mm && mmget_not_zero(wq->mm)) {
437 use_mm(wq->mm);
438 set_fs(USER_DS);
439 worker->mm = wq->mm;
440 }
441 if (test_bit(IO_WQ_BIT_CANCEL, &wq->state))
442 work->flags |= IO_WQ_WORK_CANCEL;
443 if (worker->mm)
444 work->flags |= IO_WQ_WORK_HAS_MM;
445
Jens Axboe7d723062019-11-12 22:31:31 -0700446 if (wq->get_work && !(work->flags & IO_WQ_WORK_INTERNAL)) {
447 put_work = work;
448 wq->get_work(work);
449 }
450
Jens Axboe771b53d02019-10-22 10:25:58 -0600451 old_work = work;
452 work->func(&work);
453
Jens Axboe36c2f922019-11-13 09:43:34 -0700454 spin_lock_irq(&worker->lock);
Jens Axboe771b53d02019-10-22 10:25:58 -0600455 worker->cur_work = NULL;
Jens Axboe36c2f922019-11-13 09:43:34 -0700456 spin_unlock_irq(&worker->lock);
457
458 spin_lock_irq(&wqe->lock);
459
Jens Axboe771b53d02019-10-22 10:25:58 -0600460 if (hash != -1U) {
461 wqe->hash_map &= ~BIT_ULL(hash);
462 wqe->flags &= ~IO_WQE_FLAG_STALLED;
463 }
464 if (work && work != old_work) {
465 spin_unlock_irq(&wqe->lock);
Jens Axboe7d723062019-11-12 22:31:31 -0700466
467 if (put_work && wq->put_work) {
468 wq->put_work(put_work);
469 put_work = NULL;
470 }
471
Jens Axboe771b53d02019-10-22 10:25:58 -0600472 /* dependent work not hashed */
473 hash = -1U;
474 goto next;
475 }
476 } while (1);
477}
478
Jens Axboe771b53d02019-10-22 10:25:58 -0600479static int io_wqe_worker(void *data)
480{
481 struct io_worker *worker = data;
482 struct io_wqe *wqe = worker->wqe;
483 struct io_wq *wq = wqe->wq;
484 DEFINE_WAIT(wait);
485
486 io_worker_start(wqe, worker);
487
488 while (!test_bit(IO_WQ_BIT_EXIT, &wq->state)) {
489 prepare_to_wait(&worker->wait, &wait, TASK_INTERRUPTIBLE);
490
491 spin_lock_irq(&wqe->lock);
492 if (io_wqe_run_queue(wqe)) {
493 __set_current_state(TASK_RUNNING);
494 io_worker_handle_work(worker);
495 continue;
496 }
497 /* drops the lock on success, retry */
498 if (__io_worker_idle(wqe, worker)) {
499 __release(&wqe->lock);
500 continue;
501 }
502 spin_unlock_irq(&wqe->lock);
503 if (signal_pending(current))
504 flush_signals(current);
505 if (schedule_timeout(WORKER_IDLE_TIMEOUT))
506 continue;
507 /* timed out, exit unless we're the fixed worker */
508 if (test_bit(IO_WQ_BIT_EXIT, &wq->state) ||
509 !(worker->flags & IO_WORKER_F_FIXED))
510 break;
511 }
512
513 finish_wait(&worker->wait, &wait);
514
515 if (test_bit(IO_WQ_BIT_EXIT, &wq->state)) {
516 spin_lock_irq(&wqe->lock);
517 if (!list_empty(&wqe->work_list))
518 io_worker_handle_work(worker);
519 else
520 spin_unlock_irq(&wqe->lock);
521 }
522
523 io_worker_exit(worker);
524 return 0;
525}
526
527/*
Jens Axboe771b53d02019-10-22 10:25:58 -0600528 * Called when a worker is scheduled in. Mark us as currently running.
529 */
530void io_wq_worker_running(struct task_struct *tsk)
531{
532 struct io_worker *worker = kthread_data(tsk);
533 struct io_wqe *wqe = worker->wqe;
534
535 if (!(worker->flags & IO_WORKER_F_UP))
536 return;
537 if (worker->flags & IO_WORKER_F_RUNNING)
538 return;
539 worker->flags |= IO_WORKER_F_RUNNING;
Jens Axboec5def4a2019-11-07 11:41:16 -0700540 io_wqe_inc_running(wqe, worker);
Jens Axboe771b53d02019-10-22 10:25:58 -0600541}
542
543/*
544 * Called when worker is going to sleep. If there are no workers currently
545 * running and we have work pending, wake up a free one or have the manager
546 * set one up.
547 */
548void io_wq_worker_sleeping(struct task_struct *tsk)
549{
550 struct io_worker *worker = kthread_data(tsk);
551 struct io_wqe *wqe = worker->wqe;
552
553 if (!(worker->flags & IO_WORKER_F_UP))
554 return;
555 if (!(worker->flags & IO_WORKER_F_RUNNING))
556 return;
557
558 worker->flags &= ~IO_WORKER_F_RUNNING;
559
560 spin_lock_irq(&wqe->lock);
Jens Axboec5def4a2019-11-07 11:41:16 -0700561 io_wqe_dec_running(wqe, worker);
Jens Axboe771b53d02019-10-22 10:25:58 -0600562 spin_unlock_irq(&wqe->lock);
563}
564
Jens Axboec5def4a2019-11-07 11:41:16 -0700565static void create_io_worker(struct io_wq *wq, struct io_wqe *wqe, int index)
Jens Axboe771b53d02019-10-22 10:25:58 -0600566{
Jens Axboec5def4a2019-11-07 11:41:16 -0700567 struct io_wqe_acct *acct =&wqe->acct[index];
Jens Axboe771b53d02019-10-22 10:25:58 -0600568 struct io_worker *worker;
569
570 worker = kcalloc_node(1, sizeof(*worker), GFP_KERNEL, wqe->node);
571 if (!worker)
572 return;
573
574 refcount_set(&worker->ref, 1);
575 worker->nulls_node.pprev = NULL;
576 init_waitqueue_head(&worker->wait);
577 worker->wqe = wqe;
Jens Axboe36c2f922019-11-13 09:43:34 -0700578 spin_lock_init(&worker->lock);
Jens Axboe771b53d02019-10-22 10:25:58 -0600579
580 worker->task = kthread_create_on_node(io_wqe_worker, worker, wqe->node,
Jens Axboec5def4a2019-11-07 11:41:16 -0700581 "io_wqe_worker-%d/%d", index, wqe->node);
Jens Axboe771b53d02019-10-22 10:25:58 -0600582 if (IS_ERR(worker->task)) {
583 kfree(worker);
584 return;
585 }
586
587 spin_lock_irq(&wqe->lock);
Jens Axboe021d1cd2019-11-14 08:00:41 -0700588 hlist_nulls_add_head_rcu(&worker->nulls_node, &wqe->free_list);
Jens Axboee61df662019-11-13 13:54:49 -0700589 list_add_tail_rcu(&worker->all_list, &wqe->all_list);
Jens Axboe771b53d02019-10-22 10:25:58 -0600590 worker->flags |= IO_WORKER_F_FREE;
Jens Axboec5def4a2019-11-07 11:41:16 -0700591 if (index == IO_WQ_ACCT_BOUND)
592 worker->flags |= IO_WORKER_F_BOUND;
593 if (!acct->nr_workers && (worker->flags & IO_WORKER_F_BOUND))
Jens Axboe771b53d02019-10-22 10:25:58 -0600594 worker->flags |= IO_WORKER_F_FIXED;
Jens Axboec5def4a2019-11-07 11:41:16 -0700595 acct->nr_workers++;
Jens Axboe771b53d02019-10-22 10:25:58 -0600596 spin_unlock_irq(&wqe->lock);
597
Jens Axboec5def4a2019-11-07 11:41:16 -0700598 if (index == IO_WQ_ACCT_UNBOUND)
599 atomic_inc(&wq->user->processes);
600
Jens Axboe771b53d02019-10-22 10:25:58 -0600601 wake_up_process(worker->task);
602}
603
Jens Axboec5def4a2019-11-07 11:41:16 -0700604static inline bool io_wqe_need_worker(struct io_wqe *wqe, int index)
Jens Axboe771b53d02019-10-22 10:25:58 -0600605 __must_hold(wqe->lock)
606{
Jens Axboec5def4a2019-11-07 11:41:16 -0700607 struct io_wqe_acct *acct = &wqe->acct[index];
Jens Axboe771b53d02019-10-22 10:25:58 -0600608
Jens Axboec5def4a2019-11-07 11:41:16 -0700609 /* always ensure we have one bounded worker */
610 if (index == IO_WQ_ACCT_BOUND && !acct->nr_workers)
611 return true;
612 /* if we have available workers or no work, no need */
Jens Axboe021d1cd2019-11-14 08:00:41 -0700613 if (!hlist_nulls_empty(&wqe->free_list) || !io_wqe_run_queue(wqe))
Jens Axboec5def4a2019-11-07 11:41:16 -0700614 return false;
615 return acct->nr_workers < acct->max_workers;
Jens Axboe771b53d02019-10-22 10:25:58 -0600616}
617
618/*
619 * Manager thread. Tasked with creating new workers, if we need them.
620 */
621static int io_wq_manager(void *data)
622{
623 struct io_wq *wq = data;
624
625 while (!kthread_should_stop()) {
626 int i;
627
628 for (i = 0; i < wq->nr_wqes; i++) {
629 struct io_wqe *wqe = wq->wqes[i];
Jens Axboec5def4a2019-11-07 11:41:16 -0700630 bool fork_worker[2] = { false, false };
Jens Axboe771b53d02019-10-22 10:25:58 -0600631
632 spin_lock_irq(&wqe->lock);
Jens Axboec5def4a2019-11-07 11:41:16 -0700633 if (io_wqe_need_worker(wqe, IO_WQ_ACCT_BOUND))
634 fork_worker[IO_WQ_ACCT_BOUND] = true;
635 if (io_wqe_need_worker(wqe, IO_WQ_ACCT_UNBOUND))
636 fork_worker[IO_WQ_ACCT_UNBOUND] = true;
Jens Axboe771b53d02019-10-22 10:25:58 -0600637 spin_unlock_irq(&wqe->lock);
Jens Axboec5def4a2019-11-07 11:41:16 -0700638 if (fork_worker[IO_WQ_ACCT_BOUND])
639 create_io_worker(wq, wqe, IO_WQ_ACCT_BOUND);
640 if (fork_worker[IO_WQ_ACCT_UNBOUND])
641 create_io_worker(wq, wqe, IO_WQ_ACCT_UNBOUND);
Jens Axboe771b53d02019-10-22 10:25:58 -0600642 }
643 set_current_state(TASK_INTERRUPTIBLE);
644 schedule_timeout(HZ);
645 }
646
647 return 0;
648}
649
Jens Axboec5def4a2019-11-07 11:41:16 -0700650static bool io_wq_can_queue(struct io_wqe *wqe, struct io_wqe_acct *acct,
651 struct io_wq_work *work)
652{
653 bool free_worker;
654
655 if (!(work->flags & IO_WQ_WORK_UNBOUND))
656 return true;
657 if (atomic_read(&acct->nr_running))
658 return true;
659
660 rcu_read_lock();
Jens Axboe021d1cd2019-11-14 08:00:41 -0700661 free_worker = !hlist_nulls_empty(&wqe->free_list);
Jens Axboec5def4a2019-11-07 11:41:16 -0700662 rcu_read_unlock();
663 if (free_worker)
664 return true;
665
666 if (atomic_read(&wqe->wq->user->processes) >= acct->max_workers &&
667 !(capable(CAP_SYS_RESOURCE) || capable(CAP_SYS_ADMIN)))
668 return false;
669
670 return true;
671}
672
Jens Axboe771b53d02019-10-22 10:25:58 -0600673static void io_wqe_enqueue(struct io_wqe *wqe, struct io_wq_work *work)
674{
Jens Axboec5def4a2019-11-07 11:41:16 -0700675 struct io_wqe_acct *acct = io_work_get_acct(wqe, work);
Jens Axboe771b53d02019-10-22 10:25:58 -0600676 unsigned long flags;
677
Jens Axboec5def4a2019-11-07 11:41:16 -0700678 /*
679 * Do early check to see if we need a new unbound worker, and if we do,
680 * if we're allowed to do so. This isn't 100% accurate as there's a
681 * gap between this check and incrementing the value, but that's OK.
682 * It's close enough to not be an issue, fork() has the same delay.
683 */
684 if (unlikely(!io_wq_can_queue(wqe, acct, work))) {
685 work->flags |= IO_WQ_WORK_CANCEL;
686 work->func(&work);
687 return;
688 }
689
Jens Axboe771b53d02019-10-22 10:25:58 -0600690 spin_lock_irqsave(&wqe->lock, flags);
691 list_add_tail(&work->list, &wqe->work_list);
692 wqe->flags &= ~IO_WQE_FLAG_STALLED;
693 spin_unlock_irqrestore(&wqe->lock, flags);
694
Jens Axboec5def4a2019-11-07 11:41:16 -0700695 if (!atomic_read(&acct->nr_running))
696 io_wqe_wake_worker(wqe, acct);
Jens Axboe771b53d02019-10-22 10:25:58 -0600697}
698
699void io_wq_enqueue(struct io_wq *wq, struct io_wq_work *work)
700{
701 struct io_wqe *wqe = wq->wqes[numa_node_id()];
702
703 io_wqe_enqueue(wqe, work);
704}
705
706/*
707 * Enqueue work, hashed by some key. Work items that hash to the same value
708 * will not be done in parallel. Used to limit concurrent writes, generally
709 * hashed by inode.
710 */
711void io_wq_enqueue_hashed(struct io_wq *wq, struct io_wq_work *work, void *val)
712{
713 struct io_wqe *wqe = wq->wqes[numa_node_id()];
714 unsigned bit;
715
716
717 bit = hash_ptr(val, IO_WQ_HASH_ORDER);
718 work->flags |= (IO_WQ_WORK_HASHED | (bit << IO_WQ_HASH_SHIFT));
719 io_wqe_enqueue(wqe, work);
720}
721
722static bool io_wqe_worker_send_sig(struct io_worker *worker, void *data)
723{
724 send_sig(SIGINT, worker->task, 1);
725 return false;
726}
727
728/*
729 * Iterate the passed in list and call the specific function for each
730 * worker that isn't exiting
731 */
732static bool io_wq_for_each_worker(struct io_wqe *wqe,
Jens Axboe771b53d02019-10-22 10:25:58 -0600733 bool (*func)(struct io_worker *, void *),
734 void *data)
735{
Jens Axboe771b53d02019-10-22 10:25:58 -0600736 struct io_worker *worker;
737 bool ret = false;
738
Jens Axboee61df662019-11-13 13:54:49 -0700739 list_for_each_entry_rcu(worker, &wqe->all_list, all_list) {
Jens Axboe771b53d02019-10-22 10:25:58 -0600740 if (io_worker_get(worker)) {
741 ret = func(worker, data);
742 io_worker_release(worker);
743 if (ret)
744 break;
745 }
746 }
Jens Axboee61df662019-11-13 13:54:49 -0700747
Jens Axboe771b53d02019-10-22 10:25:58 -0600748 return ret;
749}
750
751void io_wq_cancel_all(struct io_wq *wq)
752{
753 int i;
754
755 set_bit(IO_WQ_BIT_CANCEL, &wq->state);
756
757 /*
758 * Browse both lists, as there's a gap between handing work off
759 * to a worker and the worker putting itself on the busy_list
760 */
761 rcu_read_lock();
762 for (i = 0; i < wq->nr_wqes; i++) {
763 struct io_wqe *wqe = wq->wqes[i];
764
Jens Axboee61df662019-11-13 13:54:49 -0700765 io_wq_for_each_worker(wqe, io_wqe_worker_send_sig, NULL);
Jens Axboe771b53d02019-10-22 10:25:58 -0600766 }
767 rcu_read_unlock();
768}
769
Jens Axboe62755e32019-10-28 21:49:21 -0600770struct io_cb_cancel_data {
771 struct io_wqe *wqe;
772 work_cancel_fn *cancel;
773 void *caller_data;
774};
775
776static bool io_work_cancel(struct io_worker *worker, void *cancel_data)
777{
778 struct io_cb_cancel_data *data = cancel_data;
Jens Axboe6f726532019-11-05 13:51:51 -0700779 unsigned long flags;
Jens Axboe62755e32019-10-28 21:49:21 -0600780 bool ret = false;
781
782 /*
783 * Hold the lock to avoid ->cur_work going out of scope, caller
Jens Axboe36c2f922019-11-13 09:43:34 -0700784 * may dereference the passed in work.
Jens Axboe62755e32019-10-28 21:49:21 -0600785 */
Jens Axboe36c2f922019-11-13 09:43:34 -0700786 spin_lock_irqsave(&worker->lock, flags);
Jens Axboe62755e32019-10-28 21:49:21 -0600787 if (worker->cur_work &&
788 data->cancel(worker->cur_work, data->caller_data)) {
789 send_sig(SIGINT, worker->task, 1);
790 ret = true;
791 }
Jens Axboe36c2f922019-11-13 09:43:34 -0700792 spin_unlock_irqrestore(&worker->lock, flags);
Jens Axboe62755e32019-10-28 21:49:21 -0600793
794 return ret;
795}
796
797static enum io_wq_cancel io_wqe_cancel_cb_work(struct io_wqe *wqe,
798 work_cancel_fn *cancel,
799 void *cancel_data)
800{
801 struct io_cb_cancel_data data = {
802 .wqe = wqe,
803 .cancel = cancel,
804 .caller_data = cancel_data,
805 };
806 struct io_wq_work *work;
Jens Axboe6f726532019-11-05 13:51:51 -0700807 unsigned long flags;
Jens Axboe62755e32019-10-28 21:49:21 -0600808 bool found = false;
809
Jens Axboe6f726532019-11-05 13:51:51 -0700810 spin_lock_irqsave(&wqe->lock, flags);
Jens Axboe62755e32019-10-28 21:49:21 -0600811 list_for_each_entry(work, &wqe->work_list, list) {
812 if (cancel(work, cancel_data)) {
813 list_del(&work->list);
814 found = true;
815 break;
816 }
817 }
Jens Axboe6f726532019-11-05 13:51:51 -0700818 spin_unlock_irqrestore(&wqe->lock, flags);
Jens Axboe62755e32019-10-28 21:49:21 -0600819
820 if (found) {
821 work->flags |= IO_WQ_WORK_CANCEL;
822 work->func(&work);
823 return IO_WQ_CANCEL_OK;
824 }
825
826 rcu_read_lock();
Jens Axboee61df662019-11-13 13:54:49 -0700827 found = io_wq_for_each_worker(wqe, io_work_cancel, &data);
Jens Axboe62755e32019-10-28 21:49:21 -0600828 rcu_read_unlock();
829 return found ? IO_WQ_CANCEL_RUNNING : IO_WQ_CANCEL_NOTFOUND;
830}
831
832enum io_wq_cancel io_wq_cancel_cb(struct io_wq *wq, work_cancel_fn *cancel,
833 void *data)
834{
835 enum io_wq_cancel ret = IO_WQ_CANCEL_NOTFOUND;
836 int i;
837
838 for (i = 0; i < wq->nr_wqes; i++) {
839 struct io_wqe *wqe = wq->wqes[i];
840
841 ret = io_wqe_cancel_cb_work(wqe, cancel, data);
842 if (ret != IO_WQ_CANCEL_NOTFOUND)
843 break;
844 }
845
846 return ret;
847}
848
Jens Axboe771b53d02019-10-22 10:25:58 -0600849static bool io_wq_worker_cancel(struct io_worker *worker, void *data)
850{
851 struct io_wq_work *work = data;
Jens Axboe36c2f922019-11-13 09:43:34 -0700852 unsigned long flags;
853 bool ret = false;
Jens Axboe771b53d02019-10-22 10:25:58 -0600854
Jens Axboe36c2f922019-11-13 09:43:34 -0700855 if (worker->cur_work != work)
856 return false;
857
858 spin_lock_irqsave(&worker->lock, flags);
Jens Axboe771b53d02019-10-22 10:25:58 -0600859 if (worker->cur_work == work) {
860 send_sig(SIGINT, worker->task, 1);
Jens Axboe36c2f922019-11-13 09:43:34 -0700861 ret = true;
Jens Axboe771b53d02019-10-22 10:25:58 -0600862 }
Jens Axboe36c2f922019-11-13 09:43:34 -0700863 spin_unlock_irqrestore(&worker->lock, flags);
Jens Axboe771b53d02019-10-22 10:25:58 -0600864
Jens Axboe36c2f922019-11-13 09:43:34 -0700865 return ret;
Jens Axboe771b53d02019-10-22 10:25:58 -0600866}
867
868static enum io_wq_cancel io_wqe_cancel_work(struct io_wqe *wqe,
869 struct io_wq_work *cwork)
870{
871 struct io_wq_work *work;
Jens Axboe6f726532019-11-05 13:51:51 -0700872 unsigned long flags;
Jens Axboe771b53d02019-10-22 10:25:58 -0600873 bool found = false;
874
875 cwork->flags |= IO_WQ_WORK_CANCEL;
876
877 /*
878 * First check pending list, if we're lucky we can just remove it
879 * from there. CANCEL_OK means that the work is returned as-new,
880 * no completion will be posted for it.
881 */
Jens Axboe6f726532019-11-05 13:51:51 -0700882 spin_lock_irqsave(&wqe->lock, flags);
Jens Axboe771b53d02019-10-22 10:25:58 -0600883 list_for_each_entry(work, &wqe->work_list, list) {
884 if (work == cwork) {
885 list_del(&work->list);
886 found = true;
887 break;
888 }
889 }
Jens Axboe6f726532019-11-05 13:51:51 -0700890 spin_unlock_irqrestore(&wqe->lock, flags);
Jens Axboe771b53d02019-10-22 10:25:58 -0600891
892 if (found) {
893 work->flags |= IO_WQ_WORK_CANCEL;
894 work->func(&work);
895 return IO_WQ_CANCEL_OK;
896 }
897
898 /*
899 * Now check if a free (going busy) or busy worker has the work
900 * currently running. If we find it there, we'll return CANCEL_RUNNING
901 * as an indication that we attempte to signal cancellation. The
902 * completion will run normally in this case.
903 */
904 rcu_read_lock();
Jens Axboee61df662019-11-13 13:54:49 -0700905 found = io_wq_for_each_worker(wqe, io_wq_worker_cancel, cwork);
Jens Axboe771b53d02019-10-22 10:25:58 -0600906 rcu_read_unlock();
907 return found ? IO_WQ_CANCEL_RUNNING : IO_WQ_CANCEL_NOTFOUND;
908}
909
910enum io_wq_cancel io_wq_cancel_work(struct io_wq *wq, struct io_wq_work *cwork)
911{
912 enum io_wq_cancel ret = IO_WQ_CANCEL_NOTFOUND;
913 int i;
914
915 for (i = 0; i < wq->nr_wqes; i++) {
916 struct io_wqe *wqe = wq->wqes[i];
917
918 ret = io_wqe_cancel_work(wqe, cwork);
919 if (ret != IO_WQ_CANCEL_NOTFOUND)
920 break;
921 }
922
923 return ret;
924}
925
926struct io_wq_flush_data {
927 struct io_wq_work work;
928 struct completion done;
929};
930
931static void io_wq_flush_func(struct io_wq_work **workptr)
932{
933 struct io_wq_work *work = *workptr;
934 struct io_wq_flush_data *data;
935
936 data = container_of(work, struct io_wq_flush_data, work);
937 complete(&data->done);
938}
939
940/*
941 * Doesn't wait for previously queued work to finish. When this completes,
942 * it just means that previously queued work was started.
943 */
944void io_wq_flush(struct io_wq *wq)
945{
946 struct io_wq_flush_data data;
947 int i;
948
949 for (i = 0; i < wq->nr_wqes; i++) {
950 struct io_wqe *wqe = wq->wqes[i];
951
952 init_completion(&data.done);
953 INIT_IO_WORK(&data.work, io_wq_flush_func);
Jens Axboe7d723062019-11-12 22:31:31 -0700954 data.work.flags |= IO_WQ_WORK_INTERNAL;
Jens Axboe771b53d02019-10-22 10:25:58 -0600955 io_wqe_enqueue(wqe, &data.work);
956 wait_for_completion(&data.done);
957 }
958}
959
Jens Axboec5def4a2019-11-07 11:41:16 -0700960struct io_wq *io_wq_create(unsigned bounded, struct mm_struct *mm,
Jens Axboe7d723062019-11-12 22:31:31 -0700961 struct user_struct *user, get_work_fn *get_work,
962 put_work_fn *put_work)
Jens Axboe771b53d02019-10-22 10:25:58 -0600963{
964 int ret = -ENOMEM, i, node;
965 struct io_wq *wq;
966
967 wq = kcalloc(1, sizeof(*wq), GFP_KERNEL);
968 if (!wq)
969 return ERR_PTR(-ENOMEM);
970
971 wq->nr_wqes = num_online_nodes();
972 wq->wqes = kcalloc(wq->nr_wqes, sizeof(struct io_wqe *), GFP_KERNEL);
973 if (!wq->wqes) {
974 kfree(wq);
975 return ERR_PTR(-ENOMEM);
976 }
977
Jens Axboe7d723062019-11-12 22:31:31 -0700978 wq->get_work = get_work;
979 wq->put_work = put_work;
980
Jens Axboec5def4a2019-11-07 11:41:16 -0700981 /* caller must already hold a reference to this */
982 wq->user = user;
983
Jens Axboe771b53d02019-10-22 10:25:58 -0600984 i = 0;
985 refcount_set(&wq->refs, wq->nr_wqes);
986 for_each_online_node(node) {
987 struct io_wqe *wqe;
988
989 wqe = kcalloc_node(1, sizeof(struct io_wqe), GFP_KERNEL, node);
990 if (!wqe)
991 break;
992 wq->wqes[i] = wqe;
993 wqe->node = node;
Jens Axboec5def4a2019-11-07 11:41:16 -0700994 wqe->acct[IO_WQ_ACCT_BOUND].max_workers = bounded;
995 atomic_set(&wqe->acct[IO_WQ_ACCT_BOUND].nr_running, 0);
996 if (user) {
997 wqe->acct[IO_WQ_ACCT_UNBOUND].max_workers =
998 task_rlimit(current, RLIMIT_NPROC);
999 }
1000 atomic_set(&wqe->acct[IO_WQ_ACCT_UNBOUND].nr_running, 0);
Jens Axboe771b53d02019-10-22 10:25:58 -06001001 wqe->node = node;
1002 wqe->wq = wq;
1003 spin_lock_init(&wqe->lock);
1004 INIT_LIST_HEAD(&wqe->work_list);
Jens Axboe021d1cd2019-11-14 08:00:41 -07001005 INIT_HLIST_NULLS_HEAD(&wqe->free_list, 0);
1006 INIT_HLIST_NULLS_HEAD(&wqe->busy_list, 1);
Jens Axboee61df662019-11-13 13:54:49 -07001007 INIT_LIST_HEAD(&wqe->all_list);
Jens Axboe771b53d02019-10-22 10:25:58 -06001008
1009 i++;
1010 }
1011
1012 init_completion(&wq->done);
1013
1014 if (i != wq->nr_wqes)
1015 goto err;
1016
1017 /* caller must have already done mmgrab() on this mm */
1018 wq->mm = mm;
1019
1020 wq->manager = kthread_create(io_wq_manager, wq, "io_wq_manager");
1021 if (!IS_ERR(wq->manager)) {
1022 wake_up_process(wq->manager);
1023 return wq;
1024 }
1025
1026 ret = PTR_ERR(wq->manager);
1027 wq->manager = NULL;
1028err:
1029 complete(&wq->done);
1030 io_wq_destroy(wq);
1031 return ERR_PTR(ret);
1032}
1033
1034static bool io_wq_worker_wake(struct io_worker *worker, void *data)
1035{
1036 wake_up_process(worker->task);
1037 return false;
1038}
1039
1040void io_wq_destroy(struct io_wq *wq)
1041{
1042 int i;
1043
1044 if (wq->manager) {
1045 set_bit(IO_WQ_BIT_EXIT, &wq->state);
1046 kthread_stop(wq->manager);
1047 }
1048
1049 rcu_read_lock();
1050 for (i = 0; i < wq->nr_wqes; i++) {
1051 struct io_wqe *wqe = wq->wqes[i];
1052
1053 if (!wqe)
1054 continue;
Jens Axboee61df662019-11-13 13:54:49 -07001055 io_wq_for_each_worker(wqe, io_wq_worker_wake, NULL);
Jens Axboe771b53d02019-10-22 10:25:58 -06001056 }
1057 rcu_read_unlock();
1058
1059 wait_for_completion(&wq->done);
1060
1061 for (i = 0; i < wq->nr_wqes; i++)
1062 kfree(wq->wqes[i]);
1063 kfree(wq->wqes);
1064 kfree(wq);
1065}