blob: fcb6c74209dafa6230ba34f1ac03109945a1698f [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
62struct io_wq_nulls_list {
63 struct hlist_nulls_head head;
64 unsigned long nulls;
65};
66
67#if BITS_PER_LONG == 64
68#define IO_WQ_HASH_ORDER 6
69#else
70#define IO_WQ_HASH_ORDER 5
71#endif
72
Jens Axboec5def4a2019-11-07 11:41:16 -070073struct io_wqe_acct {
74 unsigned nr_workers;
75 unsigned max_workers;
76 atomic_t nr_running;
77};
78
79enum {
80 IO_WQ_ACCT_BOUND,
81 IO_WQ_ACCT_UNBOUND,
82};
83
Jens Axboe771b53d02019-10-22 10:25:58 -060084/*
85 * Per-node worker thread pool
86 */
87struct io_wqe {
88 struct {
89 spinlock_t lock;
90 struct list_head work_list;
91 unsigned long hash_map;
92 unsigned flags;
93 } ____cacheline_aligned_in_smp;
94
95 int node;
Jens Axboec5def4a2019-11-07 11:41:16 -070096 struct io_wqe_acct acct[2];
Jens Axboe771b53d02019-10-22 10:25:58 -060097
98 struct io_wq_nulls_list free_list;
99 struct io_wq_nulls_list busy_list;
Jens Axboee61df662019-11-13 13:54:49 -0700100 struct list_head all_list;
Jens Axboe771b53d02019-10-22 10:25:58 -0600101
102 struct io_wq *wq;
103};
104
105/*
106 * Per io_wq state
107 */
108struct io_wq {
109 struct io_wqe **wqes;
110 unsigned long state;
111 unsigned nr_wqes;
112
Jens Axboe7d723062019-11-12 22:31:31 -0700113 get_work_fn *get_work;
114 put_work_fn *put_work;
115
Jens Axboe771b53d02019-10-22 10:25:58 -0600116 struct task_struct *manager;
Jens Axboec5def4a2019-11-07 11:41:16 -0700117 struct user_struct *user;
Jens Axboe771b53d02019-10-22 10:25:58 -0600118 struct mm_struct *mm;
119 refcount_t refs;
120 struct completion done;
121};
122
Jens Axboe771b53d02019-10-22 10:25:58 -0600123static bool io_worker_get(struct io_worker *worker)
124{
125 return refcount_inc_not_zero(&worker->ref);
126}
127
128static void io_worker_release(struct io_worker *worker)
129{
130 if (refcount_dec_and_test(&worker->ref))
131 wake_up_process(worker->task);
132}
133
134/*
135 * Note: drops the wqe->lock if returning true! The caller must re-acquire
136 * the lock in that case. Some callers need to restart handling if this
137 * happens, so we can't just re-acquire the lock on behalf of the caller.
138 */
139static bool __io_worker_unuse(struct io_wqe *wqe, struct io_worker *worker)
140{
Jens Axboefcb323c2019-10-24 12:39:47 -0600141 bool dropped_lock = false;
142
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
Jens Axboe771b53d02019-10-22 10:25:58 -0600153 /*
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) {
Jens Axboefcb323c2019-10-24 12:39:47 -0600158 if (!dropped_lock) {
159 __acquire(&wqe->lock);
160 spin_unlock_irq(&wqe->lock);
161 dropped_lock = true;
162 }
Jens Axboe771b53d02019-10-22 10:25:58 -0600163 __set_current_state(TASK_RUNNING);
164 set_fs(KERNEL_DS);
165 unuse_mm(worker->mm);
166 mmput(worker->mm);
167 worker->mm = NULL;
Jens Axboe771b53d02019-10-22 10:25:58 -0600168 }
169
Jens Axboefcb323c2019-10-24 12:39:47 -0600170 return dropped_lock;
Jens Axboe771b53d02019-10-22 10:25:58 -0600171}
172
Jens Axboec5def4a2019-11-07 11:41:16 -0700173static 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
Jens Axboe771b53d02019-10-22 10:25:58 -0600191static void io_worker_exit(struct io_worker *worker)
192{
193 struct io_wqe *wqe = worker->wqe;
Jens Axboec5def4a2019-11-07 11:41:16 -0700194 struct io_wqe_acct *acct = io_wqe_get_acct(wqe, worker);
195 unsigned nr_workers;
Jens Axboe771b53d02019-10-22 10:25:58 -0600196
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)
Jens Axboec5def4a2019-11-07 11:41:16 -0700209 atomic_dec(&acct->nr_running);
210 if (!(worker->flags & IO_WORKER_F_BOUND))
211 atomic_dec(&wqe->wq->user->processes);
Jens Axboe771b53d02019-10-22 10:25:58 -0600212 worker->flags = 0;
213 preempt_enable();
214
215 spin_lock_irq(&wqe->lock);
216 hlist_nulls_del_rcu(&worker->nulls_node);
Jens Axboee61df662019-11-13 13:54:49 -0700217 list_del_rcu(&worker->all_list);
Jens Axboe771b53d02019-10-22 10:25:58 -0600218 if (__io_worker_unuse(wqe, worker)) {
219 __release(&wqe->lock);
220 spin_lock_irq(&wqe->lock);
221 }
Jens Axboec5def4a2019-11-07 11:41:16 -0700222 acct->nr_workers--;
223 nr_workers = wqe->acct[IO_WQ_ACCT_BOUND].nr_workers +
224 wqe->acct[IO_WQ_ACCT_UNBOUND].nr_workers;
Jens Axboe771b53d02019-10-22 10:25:58 -0600225 spin_unlock_irq(&wqe->lock);
226
227 /* all workers gone, wq exit can proceed */
Jens Axboec5def4a2019-11-07 11:41:16 -0700228 if (!nr_workers && refcount_dec_and_test(&wqe->wq->refs))
Jens Axboe771b53d02019-10-22 10:25:58 -0600229 complete(&wqe->wq->done);
230
YueHaibing364b05f2019-11-02 15:55:01 +0800231 kfree_rcu(worker, rcu);
Jens Axboe771b53d02019-10-22 10:25:58 -0600232}
233
Jens Axboec5def4a2019-11-07 11:41:16 -0700234static inline bool io_wqe_run_queue(struct io_wqe *wqe)
235 __must_hold(wqe->lock)
236{
237 if (!list_empty(&wqe->work_list) && !(wqe->flags & IO_WQE_FLAG_STALLED))
238 return true;
239 return false;
240}
241
242/*
243 * Check head of free list for an available worker. If one isn't available,
244 * caller must wake up the wq manager to create one.
245 */
246static bool io_wqe_activate_free_worker(struct io_wqe *wqe)
247 __must_hold(RCU)
248{
249 struct hlist_nulls_node *n;
250 struct io_worker *worker;
251
252 n = rcu_dereference(hlist_nulls_first_rcu(&wqe->free_list.head));
253 if (is_a_nulls(n))
254 return false;
255
256 worker = hlist_nulls_entry(n, struct io_worker, nulls_node);
257 if (io_worker_get(worker)) {
258 wake_up(&worker->wait);
259 io_worker_release(worker);
260 return true;
261 }
262
263 return false;
264}
265
266/*
267 * We need a worker. If we find a free one, we're good. If not, and we're
268 * below the max number of workers, wake up the manager to create one.
269 */
270static void io_wqe_wake_worker(struct io_wqe *wqe, struct io_wqe_acct *acct)
271{
272 bool ret;
273
274 /*
275 * Most likely an attempt to queue unbounded work on an io_wq that
276 * wasn't setup with any unbounded workers.
277 */
278 WARN_ON_ONCE(!acct->max_workers);
279
280 rcu_read_lock();
281 ret = io_wqe_activate_free_worker(wqe);
282 rcu_read_unlock();
283
284 if (!ret && acct->nr_workers < acct->max_workers)
285 wake_up_process(wqe->wq->manager);
286}
287
288static void io_wqe_inc_running(struct io_wqe *wqe, struct io_worker *worker)
289{
290 struct io_wqe_acct *acct = io_wqe_get_acct(wqe, worker);
291
292 atomic_inc(&acct->nr_running);
293}
294
295static void io_wqe_dec_running(struct io_wqe *wqe, struct io_worker *worker)
296 __must_hold(wqe->lock)
297{
298 struct io_wqe_acct *acct = io_wqe_get_acct(wqe, worker);
299
300 if (atomic_dec_and_test(&acct->nr_running) && io_wqe_run_queue(wqe))
301 io_wqe_wake_worker(wqe, acct);
302}
303
Jens Axboe771b53d02019-10-22 10:25:58 -0600304static void io_worker_start(struct io_wqe *wqe, struct io_worker *worker)
305{
306 allow_kernel_signal(SIGINT);
307
308 current->flags |= PF_IO_WORKER;
309
310 worker->flags |= (IO_WORKER_F_UP | IO_WORKER_F_RUNNING);
Jens Axboefcb323c2019-10-24 12:39:47 -0600311 worker->restore_files = current->files;
Jens Axboec5def4a2019-11-07 11:41:16 -0700312 io_wqe_inc_running(wqe, worker);
Jens Axboe771b53d02019-10-22 10:25:58 -0600313}
314
315/*
316 * Worker will start processing some work. Move it to the busy list, if
317 * it's currently on the freelist
318 */
319static void __io_worker_busy(struct io_wqe *wqe, struct io_worker *worker,
320 struct io_wq_work *work)
321 __must_hold(wqe->lock)
322{
Jens Axboec5def4a2019-11-07 11:41:16 -0700323 bool worker_bound, work_bound;
324
Jens Axboe771b53d02019-10-22 10:25:58 -0600325 if (worker->flags & IO_WORKER_F_FREE) {
326 worker->flags &= ~IO_WORKER_F_FREE;
327 hlist_nulls_del_init_rcu(&worker->nulls_node);
328 hlist_nulls_add_head_rcu(&worker->nulls_node,
329 &wqe->busy_list.head);
330 }
Jens Axboec5def4a2019-11-07 11:41:16 -0700331
332 /*
333 * If worker is moving from bound to unbound (or vice versa), then
334 * ensure we update the running accounting.
335 */
336 worker_bound = (worker->flags & IO_WORKER_F_BOUND) != 0;
337 work_bound = (work->flags & IO_WQ_WORK_UNBOUND) == 0;
338 if (worker_bound != work_bound) {
339 io_wqe_dec_running(wqe, worker);
340 if (work_bound) {
341 worker->flags |= IO_WORKER_F_BOUND;
342 wqe->acct[IO_WQ_ACCT_UNBOUND].nr_workers--;
343 wqe->acct[IO_WQ_ACCT_BOUND].nr_workers++;
344 atomic_dec(&wqe->wq->user->processes);
345 } else {
346 worker->flags &= ~IO_WORKER_F_BOUND;
347 wqe->acct[IO_WQ_ACCT_UNBOUND].nr_workers++;
348 wqe->acct[IO_WQ_ACCT_BOUND].nr_workers--;
349 atomic_inc(&wqe->wq->user->processes);
350 }
351 io_wqe_inc_running(wqe, worker);
352 }
Jens Axboe771b53d02019-10-22 10:25:58 -0600353}
354
355/*
356 * No work, worker going to sleep. Move to freelist, and unuse mm if we
357 * have one attached. Dropping the mm may potentially sleep, so we drop
358 * the lock in that case and return success. Since the caller has to
359 * retry the loop in that case (we changed task state), we don't regrab
360 * the lock if we return success.
361 */
362static bool __io_worker_idle(struct io_wqe *wqe, struct io_worker *worker)
363 __must_hold(wqe->lock)
364{
365 if (!(worker->flags & IO_WORKER_F_FREE)) {
366 worker->flags |= IO_WORKER_F_FREE;
367 hlist_nulls_del_init_rcu(&worker->nulls_node);
368 hlist_nulls_add_head_rcu(&worker->nulls_node,
369 &wqe->free_list.head);
370 }
371
372 return __io_worker_unuse(wqe, worker);
373}
374
375static struct io_wq_work *io_get_next_work(struct io_wqe *wqe, unsigned *hash)
376 __must_hold(wqe->lock)
377{
378 struct io_wq_work *work;
379
380 list_for_each_entry(work, &wqe->work_list, list) {
381 /* not hashed, can run anytime */
382 if (!(work->flags & IO_WQ_WORK_HASHED)) {
383 list_del(&work->list);
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);
391 list_del(&work->list);
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{
Jens Axboe7d723062019-11-12 22:31:31 -0700402 struct io_wq_work *work, *old_work = NULL, *put_work = NULL;
Jens Axboe771b53d02019-10-22 10:25:58 -0600403 struct io_wqe *wqe = worker->wqe;
404 struct io_wq *wq = wqe->wq;
405
406 do {
407 unsigned hash = -1U;
408
409 /*
Jens Axboe771b53d02019-10-22 10:25:58 -0600410 * 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);
419 else if (!list_empty(&wqe->work_list))
420 wqe->flags |= IO_WQE_FLAG_STALLED;
421
422 spin_unlock_irq(&wqe->lock);
Jens Axboe7d723062019-11-12 22:31:31 -0700423 if (put_work && wq->put_work)
424 wq->put_work(old_work);
Jens Axboe771b53d02019-10-22 10:25:58 -0600425 if (!work)
426 break;
427next:
Jens Axboe36c2f922019-11-13 09:43:34 -0700428 /* flush any pending signals before assigning new work */
429 if (signal_pending(current))
430 flush_signals(current);
431
432 spin_lock_irq(&worker->lock);
433 worker->cur_work = work;
434 spin_unlock_irq(&worker->lock);
435
Jens Axboefcb323c2019-10-24 12:39:47 -0600436 if ((work->flags & IO_WQ_WORK_NEEDS_FILES) &&
437 current->files != work->files) {
438 task_lock(current);
439 current->files = work->files;
440 task_unlock(current);
441 }
Jens Axboe771b53d02019-10-22 10:25:58 -0600442 if ((work->flags & IO_WQ_WORK_NEEDS_USER) && !worker->mm &&
443 wq->mm && mmget_not_zero(wq->mm)) {
444 use_mm(wq->mm);
445 set_fs(USER_DS);
446 worker->mm = wq->mm;
447 }
448 if (test_bit(IO_WQ_BIT_CANCEL, &wq->state))
449 work->flags |= IO_WQ_WORK_CANCEL;
450 if (worker->mm)
451 work->flags |= IO_WQ_WORK_HAS_MM;
452
Jens Axboe7d723062019-11-12 22:31:31 -0700453 if (wq->get_work && !(work->flags & IO_WQ_WORK_INTERNAL)) {
454 put_work = work;
455 wq->get_work(work);
456 }
457
Jens Axboe771b53d02019-10-22 10:25:58 -0600458 old_work = work;
459 work->func(&work);
460
Jens Axboe36c2f922019-11-13 09:43:34 -0700461 spin_lock_irq(&worker->lock);
Jens Axboe771b53d02019-10-22 10:25:58 -0600462 worker->cur_work = NULL;
Jens Axboe36c2f922019-11-13 09:43:34 -0700463 spin_unlock_irq(&worker->lock);
464
465 spin_lock_irq(&wqe->lock);
466
Jens Axboe771b53d02019-10-22 10:25:58 -0600467 if (hash != -1U) {
468 wqe->hash_map &= ~BIT_ULL(hash);
469 wqe->flags &= ~IO_WQE_FLAG_STALLED;
470 }
471 if (work && work != old_work) {
472 spin_unlock_irq(&wqe->lock);
Jens Axboe7d723062019-11-12 22:31:31 -0700473
474 if (put_work && wq->put_work) {
475 wq->put_work(put_work);
476 put_work = NULL;
477 }
478
Jens Axboe771b53d02019-10-22 10:25:58 -0600479 /* dependent work not hashed */
480 hash = -1U;
481 goto next;
482 }
483 } while (1);
484}
485
Jens Axboe771b53d02019-10-22 10:25:58 -0600486static int io_wqe_worker(void *data)
487{
488 struct io_worker *worker = data;
489 struct io_wqe *wqe = worker->wqe;
490 struct io_wq *wq = wqe->wq;
491 DEFINE_WAIT(wait);
492
493 io_worker_start(wqe, worker);
494
495 while (!test_bit(IO_WQ_BIT_EXIT, &wq->state)) {
496 prepare_to_wait(&worker->wait, &wait, TASK_INTERRUPTIBLE);
497
498 spin_lock_irq(&wqe->lock);
499 if (io_wqe_run_queue(wqe)) {
500 __set_current_state(TASK_RUNNING);
501 io_worker_handle_work(worker);
502 continue;
503 }
504 /* drops the lock on success, retry */
505 if (__io_worker_idle(wqe, worker)) {
506 __release(&wqe->lock);
507 continue;
508 }
509 spin_unlock_irq(&wqe->lock);
510 if (signal_pending(current))
511 flush_signals(current);
512 if (schedule_timeout(WORKER_IDLE_TIMEOUT))
513 continue;
514 /* timed out, exit unless we're the fixed worker */
515 if (test_bit(IO_WQ_BIT_EXIT, &wq->state) ||
516 !(worker->flags & IO_WORKER_F_FIXED))
517 break;
518 }
519
520 finish_wait(&worker->wait, &wait);
521
522 if (test_bit(IO_WQ_BIT_EXIT, &wq->state)) {
523 spin_lock_irq(&wqe->lock);
524 if (!list_empty(&wqe->work_list))
525 io_worker_handle_work(worker);
526 else
527 spin_unlock_irq(&wqe->lock);
528 }
529
530 io_worker_exit(worker);
531 return 0;
532}
533
534/*
Jens Axboe771b53d02019-10-22 10:25:58 -0600535 * Called when a worker is scheduled in. Mark us as currently running.
536 */
537void io_wq_worker_running(struct task_struct *tsk)
538{
539 struct io_worker *worker = kthread_data(tsk);
540 struct io_wqe *wqe = worker->wqe;
541
542 if (!(worker->flags & IO_WORKER_F_UP))
543 return;
544 if (worker->flags & IO_WORKER_F_RUNNING)
545 return;
546 worker->flags |= IO_WORKER_F_RUNNING;
Jens Axboec5def4a2019-11-07 11:41:16 -0700547 io_wqe_inc_running(wqe, worker);
Jens Axboe771b53d02019-10-22 10:25:58 -0600548}
549
550/*
551 * Called when worker is going to sleep. If there are no workers currently
552 * running and we have work pending, wake up a free one or have the manager
553 * set one up.
554 */
555void io_wq_worker_sleeping(struct task_struct *tsk)
556{
557 struct io_worker *worker = kthread_data(tsk);
558 struct io_wqe *wqe = worker->wqe;
559
560 if (!(worker->flags & IO_WORKER_F_UP))
561 return;
562 if (!(worker->flags & IO_WORKER_F_RUNNING))
563 return;
564
565 worker->flags &= ~IO_WORKER_F_RUNNING;
566
567 spin_lock_irq(&wqe->lock);
Jens Axboec5def4a2019-11-07 11:41:16 -0700568 io_wqe_dec_running(wqe, worker);
Jens Axboe771b53d02019-10-22 10:25:58 -0600569 spin_unlock_irq(&wqe->lock);
570}
571
Jens Axboec5def4a2019-11-07 11:41:16 -0700572static void create_io_worker(struct io_wq *wq, struct io_wqe *wqe, int index)
Jens Axboe771b53d02019-10-22 10:25:58 -0600573{
Jens Axboec5def4a2019-11-07 11:41:16 -0700574 struct io_wqe_acct *acct =&wqe->acct[index];
Jens Axboe771b53d02019-10-22 10:25:58 -0600575 struct io_worker *worker;
576
577 worker = kcalloc_node(1, sizeof(*worker), GFP_KERNEL, wqe->node);
578 if (!worker)
579 return;
580
581 refcount_set(&worker->ref, 1);
582 worker->nulls_node.pprev = NULL;
583 init_waitqueue_head(&worker->wait);
584 worker->wqe = wqe;
Jens Axboe36c2f922019-11-13 09:43:34 -0700585 spin_lock_init(&worker->lock);
Jens Axboe771b53d02019-10-22 10:25:58 -0600586
587 worker->task = kthread_create_on_node(io_wqe_worker, worker, wqe->node,
Jens Axboec5def4a2019-11-07 11:41:16 -0700588 "io_wqe_worker-%d/%d", index, wqe->node);
Jens Axboe771b53d02019-10-22 10:25:58 -0600589 if (IS_ERR(worker->task)) {
590 kfree(worker);
591 return;
592 }
593
594 spin_lock_irq(&wqe->lock);
595 hlist_nulls_add_head_rcu(&worker->nulls_node, &wqe->free_list.head);
Jens Axboee61df662019-11-13 13:54:49 -0700596 list_add_tail_rcu(&worker->all_list, &wqe->all_list);
Jens Axboe771b53d02019-10-22 10:25:58 -0600597 worker->flags |= IO_WORKER_F_FREE;
Jens Axboec5def4a2019-11-07 11:41:16 -0700598 if (index == IO_WQ_ACCT_BOUND)
599 worker->flags |= IO_WORKER_F_BOUND;
600 if (!acct->nr_workers && (worker->flags & IO_WORKER_F_BOUND))
Jens Axboe771b53d02019-10-22 10:25:58 -0600601 worker->flags |= IO_WORKER_F_FIXED;
Jens Axboec5def4a2019-11-07 11:41:16 -0700602 acct->nr_workers++;
Jens Axboe771b53d02019-10-22 10:25:58 -0600603 spin_unlock_irq(&wqe->lock);
604
Jens Axboec5def4a2019-11-07 11:41:16 -0700605 if (index == IO_WQ_ACCT_UNBOUND)
606 atomic_inc(&wq->user->processes);
607
Jens Axboe771b53d02019-10-22 10:25:58 -0600608 wake_up_process(worker->task);
609}
610
Jens Axboec5def4a2019-11-07 11:41:16 -0700611static inline bool io_wqe_need_worker(struct io_wqe *wqe, int index)
Jens Axboe771b53d02019-10-22 10:25:58 -0600612 __must_hold(wqe->lock)
613{
Jens Axboec5def4a2019-11-07 11:41:16 -0700614 struct io_wqe_acct *acct = &wqe->acct[index];
Jens Axboe771b53d02019-10-22 10:25:58 -0600615
Jens Axboec5def4a2019-11-07 11:41:16 -0700616 /* always ensure we have one bounded worker */
617 if (index == IO_WQ_ACCT_BOUND && !acct->nr_workers)
618 return true;
619 /* if we have available workers or no work, no need */
620 if (!hlist_nulls_empty(&wqe->free_list.head) || !io_wqe_run_queue(wqe))
621 return false;
622 return acct->nr_workers < acct->max_workers;
Jens Axboe771b53d02019-10-22 10:25:58 -0600623}
624
625/*
626 * Manager thread. Tasked with creating new workers, if we need them.
627 */
628static int io_wq_manager(void *data)
629{
630 struct io_wq *wq = data;
631
632 while (!kthread_should_stop()) {
633 int i;
634
635 for (i = 0; i < wq->nr_wqes; i++) {
636 struct io_wqe *wqe = wq->wqes[i];
Jens Axboec5def4a2019-11-07 11:41:16 -0700637 bool fork_worker[2] = { false, false };
Jens Axboe771b53d02019-10-22 10:25:58 -0600638
639 spin_lock_irq(&wqe->lock);
Jens Axboec5def4a2019-11-07 11:41:16 -0700640 if (io_wqe_need_worker(wqe, IO_WQ_ACCT_BOUND))
641 fork_worker[IO_WQ_ACCT_BOUND] = true;
642 if (io_wqe_need_worker(wqe, IO_WQ_ACCT_UNBOUND))
643 fork_worker[IO_WQ_ACCT_UNBOUND] = true;
Jens Axboe771b53d02019-10-22 10:25:58 -0600644 spin_unlock_irq(&wqe->lock);
Jens Axboec5def4a2019-11-07 11:41:16 -0700645 if (fork_worker[IO_WQ_ACCT_BOUND])
646 create_io_worker(wq, wqe, IO_WQ_ACCT_BOUND);
647 if (fork_worker[IO_WQ_ACCT_UNBOUND])
648 create_io_worker(wq, wqe, IO_WQ_ACCT_UNBOUND);
Jens Axboe771b53d02019-10-22 10:25:58 -0600649 }
650 set_current_state(TASK_INTERRUPTIBLE);
651 schedule_timeout(HZ);
652 }
653
654 return 0;
655}
656
Jens Axboec5def4a2019-11-07 11:41:16 -0700657static bool io_wq_can_queue(struct io_wqe *wqe, struct io_wqe_acct *acct,
658 struct io_wq_work *work)
659{
660 bool free_worker;
661
662 if (!(work->flags & IO_WQ_WORK_UNBOUND))
663 return true;
664 if (atomic_read(&acct->nr_running))
665 return true;
666
667 rcu_read_lock();
668 free_worker = !hlist_nulls_empty(&wqe->free_list.head);
669 rcu_read_unlock();
670 if (free_worker)
671 return true;
672
673 if (atomic_read(&wqe->wq->user->processes) >= acct->max_workers &&
674 !(capable(CAP_SYS_RESOURCE) || capable(CAP_SYS_ADMIN)))
675 return false;
676
677 return true;
678}
679
Jens Axboe771b53d02019-10-22 10:25:58 -0600680static void io_wqe_enqueue(struct io_wqe *wqe, struct io_wq_work *work)
681{
Jens Axboec5def4a2019-11-07 11:41:16 -0700682 struct io_wqe_acct *acct = io_work_get_acct(wqe, work);
Jens Axboe771b53d02019-10-22 10:25:58 -0600683 unsigned long flags;
684
Jens Axboec5def4a2019-11-07 11:41:16 -0700685 /*
686 * Do early check to see if we need a new unbound worker, and if we do,
687 * if we're allowed to do so. This isn't 100% accurate as there's a
688 * gap between this check and incrementing the value, but that's OK.
689 * It's close enough to not be an issue, fork() has the same delay.
690 */
691 if (unlikely(!io_wq_can_queue(wqe, acct, work))) {
692 work->flags |= IO_WQ_WORK_CANCEL;
693 work->func(&work);
694 return;
695 }
696
Jens Axboe771b53d02019-10-22 10:25:58 -0600697 spin_lock_irqsave(&wqe->lock, flags);
698 list_add_tail(&work->list, &wqe->work_list);
699 wqe->flags &= ~IO_WQE_FLAG_STALLED;
700 spin_unlock_irqrestore(&wqe->lock, flags);
701
Jens Axboec5def4a2019-11-07 11:41:16 -0700702 if (!atomic_read(&acct->nr_running))
703 io_wqe_wake_worker(wqe, acct);
Jens Axboe771b53d02019-10-22 10:25:58 -0600704}
705
706void io_wq_enqueue(struct io_wq *wq, struct io_wq_work *work)
707{
708 struct io_wqe *wqe = wq->wqes[numa_node_id()];
709
710 io_wqe_enqueue(wqe, work);
711}
712
713/*
714 * Enqueue work, hashed by some key. Work items that hash to the same value
715 * will not be done in parallel. Used to limit concurrent writes, generally
716 * hashed by inode.
717 */
718void io_wq_enqueue_hashed(struct io_wq *wq, struct io_wq_work *work, void *val)
719{
720 struct io_wqe *wqe = wq->wqes[numa_node_id()];
721 unsigned bit;
722
723
724 bit = hash_ptr(val, IO_WQ_HASH_ORDER);
725 work->flags |= (IO_WQ_WORK_HASHED | (bit << IO_WQ_HASH_SHIFT));
726 io_wqe_enqueue(wqe, work);
727}
728
729static bool io_wqe_worker_send_sig(struct io_worker *worker, void *data)
730{
731 send_sig(SIGINT, worker->task, 1);
732 return false;
733}
734
735/*
736 * Iterate the passed in list and call the specific function for each
737 * worker that isn't exiting
738 */
739static bool io_wq_for_each_worker(struct io_wqe *wqe,
Jens Axboe771b53d02019-10-22 10:25:58 -0600740 bool (*func)(struct io_worker *, void *),
741 void *data)
742{
Jens Axboe771b53d02019-10-22 10:25:58 -0600743 struct io_worker *worker;
744 bool ret = false;
745
Jens Axboee61df662019-11-13 13:54:49 -0700746 list_for_each_entry_rcu(worker, &wqe->all_list, all_list) {
Jens Axboe771b53d02019-10-22 10:25:58 -0600747 if (io_worker_get(worker)) {
748 ret = func(worker, data);
749 io_worker_release(worker);
750 if (ret)
751 break;
752 }
753 }
Jens Axboee61df662019-11-13 13:54:49 -0700754
Jens Axboe771b53d02019-10-22 10:25:58 -0600755 return ret;
756}
757
758void io_wq_cancel_all(struct io_wq *wq)
759{
760 int i;
761
762 set_bit(IO_WQ_BIT_CANCEL, &wq->state);
763
764 /*
765 * Browse both lists, as there's a gap between handing work off
766 * to a worker and the worker putting itself on the busy_list
767 */
768 rcu_read_lock();
769 for (i = 0; i < wq->nr_wqes; i++) {
770 struct io_wqe *wqe = wq->wqes[i];
771
Jens Axboee61df662019-11-13 13:54:49 -0700772 io_wq_for_each_worker(wqe, io_wqe_worker_send_sig, NULL);
Jens Axboe771b53d02019-10-22 10:25:58 -0600773 }
774 rcu_read_unlock();
775}
776
Jens Axboe62755e32019-10-28 21:49:21 -0600777struct io_cb_cancel_data {
778 struct io_wqe *wqe;
779 work_cancel_fn *cancel;
780 void *caller_data;
781};
782
783static bool io_work_cancel(struct io_worker *worker, void *cancel_data)
784{
785 struct io_cb_cancel_data *data = cancel_data;
Jens Axboe6f726532019-11-05 13:51:51 -0700786 unsigned long flags;
Jens Axboe62755e32019-10-28 21:49:21 -0600787 bool ret = false;
788
789 /*
790 * Hold the lock to avoid ->cur_work going out of scope, caller
Jens Axboe36c2f922019-11-13 09:43:34 -0700791 * may dereference the passed in work.
Jens Axboe62755e32019-10-28 21:49:21 -0600792 */
Jens Axboe36c2f922019-11-13 09:43:34 -0700793 spin_lock_irqsave(&worker->lock, flags);
Jens Axboe62755e32019-10-28 21:49:21 -0600794 if (worker->cur_work &&
795 data->cancel(worker->cur_work, data->caller_data)) {
796 send_sig(SIGINT, worker->task, 1);
797 ret = true;
798 }
Jens Axboe36c2f922019-11-13 09:43:34 -0700799 spin_unlock_irqrestore(&worker->lock, flags);
Jens Axboe62755e32019-10-28 21:49:21 -0600800
801 return ret;
802}
803
804static enum io_wq_cancel io_wqe_cancel_cb_work(struct io_wqe *wqe,
805 work_cancel_fn *cancel,
806 void *cancel_data)
807{
808 struct io_cb_cancel_data data = {
809 .wqe = wqe,
810 .cancel = cancel,
811 .caller_data = cancel_data,
812 };
813 struct io_wq_work *work;
Jens Axboe6f726532019-11-05 13:51:51 -0700814 unsigned long flags;
Jens Axboe62755e32019-10-28 21:49:21 -0600815 bool found = false;
816
Jens Axboe6f726532019-11-05 13:51:51 -0700817 spin_lock_irqsave(&wqe->lock, flags);
Jens Axboe62755e32019-10-28 21:49:21 -0600818 list_for_each_entry(work, &wqe->work_list, list) {
819 if (cancel(work, cancel_data)) {
820 list_del(&work->list);
821 found = true;
822 break;
823 }
824 }
Jens Axboe6f726532019-11-05 13:51:51 -0700825 spin_unlock_irqrestore(&wqe->lock, flags);
Jens Axboe62755e32019-10-28 21:49:21 -0600826
827 if (found) {
828 work->flags |= IO_WQ_WORK_CANCEL;
829 work->func(&work);
830 return IO_WQ_CANCEL_OK;
831 }
832
833 rcu_read_lock();
Jens Axboee61df662019-11-13 13:54:49 -0700834 found = io_wq_for_each_worker(wqe, io_work_cancel, &data);
Jens Axboe62755e32019-10-28 21:49:21 -0600835 rcu_read_unlock();
836 return found ? IO_WQ_CANCEL_RUNNING : IO_WQ_CANCEL_NOTFOUND;
837}
838
839enum io_wq_cancel io_wq_cancel_cb(struct io_wq *wq, work_cancel_fn *cancel,
840 void *data)
841{
842 enum io_wq_cancel ret = IO_WQ_CANCEL_NOTFOUND;
843 int i;
844
845 for (i = 0; i < wq->nr_wqes; i++) {
846 struct io_wqe *wqe = wq->wqes[i];
847
848 ret = io_wqe_cancel_cb_work(wqe, cancel, data);
849 if (ret != IO_WQ_CANCEL_NOTFOUND)
850 break;
851 }
852
853 return ret;
854}
855
Jens Axboe771b53d02019-10-22 10:25:58 -0600856static bool io_wq_worker_cancel(struct io_worker *worker, void *data)
857{
858 struct io_wq_work *work = data;
Jens Axboe36c2f922019-11-13 09:43:34 -0700859 unsigned long flags;
860 bool ret = false;
Jens Axboe771b53d02019-10-22 10:25:58 -0600861
Jens Axboe36c2f922019-11-13 09:43:34 -0700862 if (worker->cur_work != work)
863 return false;
864
865 spin_lock_irqsave(&worker->lock, flags);
Jens Axboe771b53d02019-10-22 10:25:58 -0600866 if (worker->cur_work == work) {
867 send_sig(SIGINT, worker->task, 1);
Jens Axboe36c2f922019-11-13 09:43:34 -0700868 ret = true;
Jens Axboe771b53d02019-10-22 10:25:58 -0600869 }
Jens Axboe36c2f922019-11-13 09:43:34 -0700870 spin_unlock_irqrestore(&worker->lock, flags);
Jens Axboe771b53d02019-10-22 10:25:58 -0600871
Jens Axboe36c2f922019-11-13 09:43:34 -0700872 return ret;
Jens Axboe771b53d02019-10-22 10:25:58 -0600873}
874
875static enum io_wq_cancel io_wqe_cancel_work(struct io_wqe *wqe,
876 struct io_wq_work *cwork)
877{
878 struct io_wq_work *work;
Jens Axboe6f726532019-11-05 13:51:51 -0700879 unsigned long flags;
Jens Axboe771b53d02019-10-22 10:25:58 -0600880 bool found = false;
881
882 cwork->flags |= IO_WQ_WORK_CANCEL;
883
884 /*
885 * First check pending list, if we're lucky we can just remove it
886 * from there. CANCEL_OK means that the work is returned as-new,
887 * no completion will be posted for it.
888 */
Jens Axboe6f726532019-11-05 13:51:51 -0700889 spin_lock_irqsave(&wqe->lock, flags);
Jens Axboe771b53d02019-10-22 10:25:58 -0600890 list_for_each_entry(work, &wqe->work_list, list) {
891 if (work == cwork) {
892 list_del(&work->list);
893 found = true;
894 break;
895 }
896 }
Jens Axboe6f726532019-11-05 13:51:51 -0700897 spin_unlock_irqrestore(&wqe->lock, flags);
Jens Axboe771b53d02019-10-22 10:25:58 -0600898
899 if (found) {
900 work->flags |= IO_WQ_WORK_CANCEL;
901 work->func(&work);
902 return IO_WQ_CANCEL_OK;
903 }
904
905 /*
906 * Now check if a free (going busy) or busy worker has the work
907 * currently running. If we find it there, we'll return CANCEL_RUNNING
908 * as an indication that we attempte to signal cancellation. The
909 * completion will run normally in this case.
910 */
911 rcu_read_lock();
Jens Axboee61df662019-11-13 13:54:49 -0700912 found = io_wq_for_each_worker(wqe, io_wq_worker_cancel, cwork);
Jens Axboe771b53d02019-10-22 10:25:58 -0600913 rcu_read_unlock();
914 return found ? IO_WQ_CANCEL_RUNNING : IO_WQ_CANCEL_NOTFOUND;
915}
916
917enum io_wq_cancel io_wq_cancel_work(struct io_wq *wq, struct io_wq_work *cwork)
918{
919 enum io_wq_cancel ret = IO_WQ_CANCEL_NOTFOUND;
920 int i;
921
922 for (i = 0; i < wq->nr_wqes; i++) {
923 struct io_wqe *wqe = wq->wqes[i];
924
925 ret = io_wqe_cancel_work(wqe, cwork);
926 if (ret != IO_WQ_CANCEL_NOTFOUND)
927 break;
928 }
929
930 return ret;
931}
932
933struct io_wq_flush_data {
934 struct io_wq_work work;
935 struct completion done;
936};
937
938static void io_wq_flush_func(struct io_wq_work **workptr)
939{
940 struct io_wq_work *work = *workptr;
941 struct io_wq_flush_data *data;
942
943 data = container_of(work, struct io_wq_flush_data, work);
944 complete(&data->done);
945}
946
947/*
948 * Doesn't wait for previously queued work to finish. When this completes,
949 * it just means that previously queued work was started.
950 */
951void io_wq_flush(struct io_wq *wq)
952{
953 struct io_wq_flush_data data;
954 int i;
955
956 for (i = 0; i < wq->nr_wqes; i++) {
957 struct io_wqe *wqe = wq->wqes[i];
958
959 init_completion(&data.done);
960 INIT_IO_WORK(&data.work, io_wq_flush_func);
Jens Axboe7d723062019-11-12 22:31:31 -0700961 data.work.flags |= IO_WQ_WORK_INTERNAL;
Jens Axboe771b53d02019-10-22 10:25:58 -0600962 io_wqe_enqueue(wqe, &data.work);
963 wait_for_completion(&data.done);
964 }
965}
966
Jens Axboec5def4a2019-11-07 11:41:16 -0700967struct io_wq *io_wq_create(unsigned bounded, struct mm_struct *mm,
Jens Axboe7d723062019-11-12 22:31:31 -0700968 struct user_struct *user, get_work_fn *get_work,
969 put_work_fn *put_work)
Jens Axboe771b53d02019-10-22 10:25:58 -0600970{
971 int ret = -ENOMEM, i, node;
972 struct io_wq *wq;
973
974 wq = kcalloc(1, sizeof(*wq), GFP_KERNEL);
975 if (!wq)
976 return ERR_PTR(-ENOMEM);
977
978 wq->nr_wqes = num_online_nodes();
979 wq->wqes = kcalloc(wq->nr_wqes, sizeof(struct io_wqe *), GFP_KERNEL);
980 if (!wq->wqes) {
981 kfree(wq);
982 return ERR_PTR(-ENOMEM);
983 }
984
Jens Axboe7d723062019-11-12 22:31:31 -0700985 wq->get_work = get_work;
986 wq->put_work = put_work;
987
Jens Axboec5def4a2019-11-07 11:41:16 -0700988 /* caller must already hold a reference to this */
989 wq->user = user;
990
Jens Axboe771b53d02019-10-22 10:25:58 -0600991 i = 0;
992 refcount_set(&wq->refs, wq->nr_wqes);
993 for_each_online_node(node) {
994 struct io_wqe *wqe;
995
996 wqe = kcalloc_node(1, sizeof(struct io_wqe), GFP_KERNEL, node);
997 if (!wqe)
998 break;
999 wq->wqes[i] = wqe;
1000 wqe->node = node;
Jens Axboec5def4a2019-11-07 11:41:16 -07001001 wqe->acct[IO_WQ_ACCT_BOUND].max_workers = bounded;
1002 atomic_set(&wqe->acct[IO_WQ_ACCT_BOUND].nr_running, 0);
1003 if (user) {
1004 wqe->acct[IO_WQ_ACCT_UNBOUND].max_workers =
1005 task_rlimit(current, RLIMIT_NPROC);
1006 }
1007 atomic_set(&wqe->acct[IO_WQ_ACCT_UNBOUND].nr_running, 0);
Jens Axboe771b53d02019-10-22 10:25:58 -06001008 wqe->node = node;
1009 wqe->wq = wq;
1010 spin_lock_init(&wqe->lock);
1011 INIT_LIST_HEAD(&wqe->work_list);
1012 INIT_HLIST_NULLS_HEAD(&wqe->free_list.head, 0);
1013 wqe->free_list.nulls = 0;
1014 INIT_HLIST_NULLS_HEAD(&wqe->busy_list.head, 1);
1015 wqe->busy_list.nulls = 1;
Jens Axboee61df662019-11-13 13:54:49 -07001016 INIT_LIST_HEAD(&wqe->all_list);
Jens Axboe771b53d02019-10-22 10:25:58 -06001017
1018 i++;
1019 }
1020
1021 init_completion(&wq->done);
1022
1023 if (i != wq->nr_wqes)
1024 goto err;
1025
1026 /* caller must have already done mmgrab() on this mm */
1027 wq->mm = mm;
1028
1029 wq->manager = kthread_create(io_wq_manager, wq, "io_wq_manager");
1030 if (!IS_ERR(wq->manager)) {
1031 wake_up_process(wq->manager);
1032 return wq;
1033 }
1034
1035 ret = PTR_ERR(wq->manager);
1036 wq->manager = NULL;
1037err:
1038 complete(&wq->done);
1039 io_wq_destroy(wq);
1040 return ERR_PTR(ret);
1041}
1042
1043static bool io_wq_worker_wake(struct io_worker *worker, void *data)
1044{
1045 wake_up_process(worker->task);
1046 return false;
1047}
1048
1049void io_wq_destroy(struct io_wq *wq)
1050{
1051 int i;
1052
1053 if (wq->manager) {
1054 set_bit(IO_WQ_BIT_EXIT, &wq->state);
1055 kthread_stop(wq->manager);
1056 }
1057
1058 rcu_read_lock();
1059 for (i = 0; i < wq->nr_wqes; i++) {
1060 struct io_wqe *wqe = wq->wqes[i];
1061
1062 if (!wqe)
1063 continue;
Jens Axboee61df662019-11-13 13:54:49 -07001064 io_wq_for_each_worker(wqe, io_wq_worker_wake, NULL);
Jens Axboe771b53d02019-10-22 10:25:58 -06001065 }
1066 rcu_read_unlock();
1067
1068 wait_for_completion(&wq->done);
1069
1070 for (i = 0; i < wq->nr_wqes; i++)
1071 kfree(wq->wqes[i]);
1072 kfree(wq->wqes);
1073 kfree(wq);
1074}