blob: 5147d2213b019f9b80841a0e14a1709ba719bf69 [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 */
Jens Axboeb60fda62019-11-19 08:37:07 -070036 IO_WQ_BIT_ERROR = 2, /* error on setup */
Jens Axboe771b53d02019-10-22 10:25:58 -060037};
38
39enum {
40 IO_WQE_FLAG_STALLED = 1, /* stalled on hash */
41};
42
43/*
44 * One for each thread in a wqe pool
45 */
46struct io_worker {
47 refcount_t ref;
48 unsigned flags;
49 struct hlist_nulls_node nulls_node;
Jens Axboee61df662019-11-13 13:54:49 -070050 struct list_head all_list;
Jens Axboe771b53d02019-10-22 10:25:58 -060051 struct task_struct *task;
Jens Axboe771b53d02019-10-22 10:25:58 -060052 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 Axboe181e4482019-11-25 08:52:30 -070059 const struct cred *creds;
Jens Axboefcb323c2019-10-24 12:39:47 -060060 struct files_struct *restore_files;
Jens Axboe771b53d02019-10-22 10:25:58 -060061};
62
Jens Axboe771b53d02019-10-22 10:25:58 -060063#if BITS_PER_LONG == 64
64#define IO_WQ_HASH_ORDER 6
65#else
66#define IO_WQ_HASH_ORDER 5
67#endif
68
Jens Axboec5def4a2019-11-07 11:41:16 -070069struct io_wqe_acct {
70 unsigned nr_workers;
71 unsigned max_workers;
72 atomic_t nr_running;
73};
74
75enum {
76 IO_WQ_ACCT_BOUND,
77 IO_WQ_ACCT_UNBOUND,
78};
79
Jens Axboe771b53d02019-10-22 10:25:58 -060080/*
81 * Per-node worker thread pool
82 */
83struct io_wqe {
84 struct {
85 spinlock_t lock;
Jens Axboe6206f0e2019-11-26 11:59:32 -070086 struct io_wq_work_list work_list;
Jens Axboe771b53d02019-10-22 10:25:58 -060087 unsigned long hash_map;
88 unsigned flags;
89 } ____cacheline_aligned_in_smp;
90
91 int node;
Jens Axboec5def4a2019-11-07 11:41:16 -070092 struct io_wqe_acct acct[2];
Jens Axboe771b53d02019-10-22 10:25:58 -060093
Jens Axboe021d1cd2019-11-14 08:00:41 -070094 struct hlist_nulls_head free_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;
Jens Axboe771b53d02019-10-22 10:25:58 -0600106
Jens Axboe7d723062019-11-12 22:31:31 -0700107 get_work_fn *get_work;
108 put_work_fn *put_work;
109
Jens Axboe771b53d02019-10-22 10:25:58 -0600110 struct task_struct *manager;
Jens Axboec5def4a2019-11-07 11:41:16 -0700111 struct user_struct *user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -0700112 const struct cred *creds;
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
Jens Axboe181e4482019-11-25 08:52:30 -0700138 if (worker->creds) {
139 revert_creds(worker->creds);
140 worker->creds = NULL;
141 }
142
Jens Axboefcb323c2019-10-24 12:39:47 -0600143 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{
Jens Axboe6206f0e2019-11-26 11:59:32 -0700237 if (!wq_list_empty(&wqe->work_list) &&
238 !(wqe->flags & IO_WQE_FLAG_STALLED))
Jens Axboec5def4a2019-11-07 11:41:16 -0700239 return true;
240 return false;
241}
242
243/*
244 * Check head of free list for an available worker. If one isn't available,
245 * caller must wake up the wq manager to create one.
246 */
247static bool io_wqe_activate_free_worker(struct io_wqe *wqe)
248 __must_hold(RCU)
249{
250 struct hlist_nulls_node *n;
251 struct io_worker *worker;
252
Jens Axboe021d1cd2019-11-14 08:00:41 -0700253 n = rcu_dereference(hlist_nulls_first_rcu(&wqe->free_list));
Jens Axboec5def4a2019-11-07 11:41:16 -0700254 if (is_a_nulls(n))
255 return false;
256
257 worker = hlist_nulls_entry(n, struct io_worker, nulls_node);
258 if (io_worker_get(worker)) {
Jens Axboe506d95f2019-12-07 21:03:59 -0700259 wake_up_process(worker->task);
Jens Axboec5def4a2019-11-07 11:41:16 -0700260 io_worker_release(worker);
261 return true;
262 }
263
264 return false;
265}
266
267/*
268 * We need a worker. If we find a free one, we're good. If not, and we're
269 * below the max number of workers, wake up the manager to create one.
270 */
271static void io_wqe_wake_worker(struct io_wqe *wqe, struct io_wqe_acct *acct)
272{
273 bool ret;
274
275 /*
276 * Most likely an attempt to queue unbounded work on an io_wq that
277 * wasn't setup with any unbounded workers.
278 */
279 WARN_ON_ONCE(!acct->max_workers);
280
281 rcu_read_lock();
282 ret = io_wqe_activate_free_worker(wqe);
283 rcu_read_unlock();
284
285 if (!ret && acct->nr_workers < acct->max_workers)
286 wake_up_process(wqe->wq->manager);
287}
288
289static void io_wqe_inc_running(struct io_wqe *wqe, struct io_worker *worker)
290{
291 struct io_wqe_acct *acct = io_wqe_get_acct(wqe, worker);
292
293 atomic_inc(&acct->nr_running);
294}
295
296static void io_wqe_dec_running(struct io_wqe *wqe, struct io_worker *worker)
297 __must_hold(wqe->lock)
298{
299 struct io_wqe_acct *acct = io_wqe_get_acct(wqe, worker);
300
301 if (atomic_dec_and_test(&acct->nr_running) && io_wqe_run_queue(wqe))
302 io_wqe_wake_worker(wqe, acct);
303}
304
Jens Axboe771b53d02019-10-22 10:25:58 -0600305static void io_worker_start(struct io_wqe *wqe, struct io_worker *worker)
306{
307 allow_kernel_signal(SIGINT);
308
309 current->flags |= PF_IO_WORKER;
310
311 worker->flags |= (IO_WORKER_F_UP | IO_WORKER_F_RUNNING);
Jens Axboefcb323c2019-10-24 12:39:47 -0600312 worker->restore_files = current->files;
Jens Axboec5def4a2019-11-07 11:41:16 -0700313 io_wqe_inc_running(wqe, worker);
Jens Axboe771b53d02019-10-22 10:25:58 -0600314}
315
316/*
317 * Worker will start processing some work. Move it to the busy list, if
318 * it's currently on the freelist
319 */
320static void __io_worker_busy(struct io_wqe *wqe, struct io_worker *worker,
321 struct io_wq_work *work)
322 __must_hold(wqe->lock)
323{
Jens Axboec5def4a2019-11-07 11:41:16 -0700324 bool worker_bound, work_bound;
325
Jens Axboe771b53d02019-10-22 10:25:58 -0600326 if (worker->flags & IO_WORKER_F_FREE) {
327 worker->flags &= ~IO_WORKER_F_FREE;
328 hlist_nulls_del_init_rcu(&worker->nulls_node);
Jens Axboe771b53d02019-10-22 10:25:58 -0600329 }
Jens Axboec5def4a2019-11-07 11:41:16 -0700330
331 /*
332 * If worker is moving from bound to unbound (or vice versa), then
333 * ensure we update the running accounting.
334 */
Dan Carpenterb2e9c7d62019-11-19 09:22:16 +0300335 worker_bound = (worker->flags & IO_WORKER_F_BOUND) != 0;
336 work_bound = (work->flags & IO_WQ_WORK_UNBOUND) == 0;
337 if (worker_bound != work_bound) {
Jens Axboec5def4a2019-11-07 11:41:16 -0700338 io_wqe_dec_running(wqe, worker);
339 if (work_bound) {
340 worker->flags |= IO_WORKER_F_BOUND;
341 wqe->acct[IO_WQ_ACCT_UNBOUND].nr_workers--;
342 wqe->acct[IO_WQ_ACCT_BOUND].nr_workers++;
343 atomic_dec(&wqe->wq->user->processes);
344 } else {
345 worker->flags &= ~IO_WORKER_F_BOUND;
346 wqe->acct[IO_WQ_ACCT_UNBOUND].nr_workers++;
347 wqe->acct[IO_WQ_ACCT_BOUND].nr_workers--;
348 atomic_inc(&wqe->wq->user->processes);
349 }
350 io_wqe_inc_running(wqe, worker);
351 }
Jens Axboe771b53d02019-10-22 10:25:58 -0600352}
353
354/*
355 * No work, worker going to sleep. Move to freelist, and unuse mm if we
356 * have one attached. Dropping the mm may potentially sleep, so we drop
357 * the lock in that case and return success. Since the caller has to
358 * retry the loop in that case (we changed task state), we don't regrab
359 * the lock if we return success.
360 */
361static bool __io_worker_idle(struct io_wqe *wqe, struct io_worker *worker)
362 __must_hold(wqe->lock)
363{
364 if (!(worker->flags & IO_WORKER_F_FREE)) {
365 worker->flags |= IO_WORKER_F_FREE;
Jens Axboe021d1cd2019-11-14 08:00:41 -0700366 hlist_nulls_add_head_rcu(&worker->nulls_node, &wqe->free_list);
Jens Axboe771b53d02019-10-22 10:25:58 -0600367 }
368
369 return __io_worker_unuse(wqe, worker);
370}
371
372static struct io_wq_work *io_get_next_work(struct io_wqe *wqe, unsigned *hash)
373 __must_hold(wqe->lock)
374{
Jens Axboe6206f0e2019-11-26 11:59:32 -0700375 struct io_wq_work_node *node, *prev;
Jens Axboe771b53d02019-10-22 10:25:58 -0600376 struct io_wq_work *work;
377
Jens Axboe6206f0e2019-11-26 11:59:32 -0700378 wq_list_for_each(node, prev, &wqe->work_list) {
379 work = container_of(node, struct io_wq_work, list);
380
Jens Axboe771b53d02019-10-22 10:25:58 -0600381 /* not hashed, can run anytime */
382 if (!(work->flags & IO_WQ_WORK_HASHED)) {
Jens Axboe6206f0e2019-11-26 11:59:32 -0700383 wq_node_del(&wqe->work_list, node, prev);
Jens Axboe771b53d02019-10-22 10:25:58 -0600384 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);
Jens Axboe6206f0e2019-11-26 11:59:32 -0700391 wq_node_del(&wqe->work_list, node, prev);
Jens Axboe771b53d02019-10-22 10:25:58 -0600392 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);
Jens Axboe6206f0e2019-11-26 11:59:32 -0700419 else if (!wq_list_empty(&wqe->work_list))
Jens Axboe771b53d02019-10-22 10:25:58 -0600420 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
Hillf Dantonfd1c4bc2019-12-24 09:14:29 -0700432 cond_resched();
433
Jens Axboe36c2f922019-11-13 09:43:34 -0700434 spin_lock_irq(&worker->lock);
435 worker->cur_work = work;
436 spin_unlock_irq(&worker->lock);
437
Jens Axboeb76da702019-11-20 13:05:32 -0700438 if (work->flags & IO_WQ_WORK_CB)
439 work->func(&work);
440
Jens Axboefcb323c2019-10-24 12:39:47 -0600441 if ((work->flags & IO_WQ_WORK_NEEDS_FILES) &&
442 current->files != work->files) {
443 task_lock(current);
444 current->files = work->files;
445 task_unlock(current);
446 }
Jens Axboe771b53d02019-10-22 10:25:58 -0600447 if ((work->flags & IO_WQ_WORK_NEEDS_USER) && !worker->mm &&
Jens Axboee0bbb342020-01-14 22:06:11 -0700448 wq->mm) {
449 if (mmget_not_zero(wq->mm)) {
450 use_mm(wq->mm);
451 set_fs(USER_DS);
452 worker->mm = wq->mm;
453 } else {
454 work->flags |= IO_WQ_WORK_CANCEL;
455 }
Jens Axboe771b53d02019-10-22 10:25:58 -0600456 }
Jens Axboe181e4482019-11-25 08:52:30 -0700457 if (!worker->creds)
458 worker->creds = override_creds(wq->creds);
Jens Axboe771b53d02019-10-22 10:25:58 -0600459 if (test_bit(IO_WQ_BIT_CANCEL, &wq->state))
460 work->flags |= IO_WQ_WORK_CANCEL;
461 if (worker->mm)
462 work->flags |= IO_WQ_WORK_HAS_MM;
463
Jens Axboe7d723062019-11-12 22:31:31 -0700464 if (wq->get_work && !(work->flags & IO_WQ_WORK_INTERNAL)) {
465 put_work = work;
466 wq->get_work(work);
467 }
468
Jens Axboe771b53d02019-10-22 10:25:58 -0600469 old_work = work;
470 work->func(&work);
471
Jens Axboe36c2f922019-11-13 09:43:34 -0700472 spin_lock_irq(&worker->lock);
Jens Axboe771b53d02019-10-22 10:25:58 -0600473 worker->cur_work = NULL;
Jens Axboe36c2f922019-11-13 09:43:34 -0700474 spin_unlock_irq(&worker->lock);
475
476 spin_lock_irq(&wqe->lock);
477
Jens Axboe771b53d02019-10-22 10:25:58 -0600478 if (hash != -1U) {
479 wqe->hash_map &= ~BIT_ULL(hash);
480 wqe->flags &= ~IO_WQE_FLAG_STALLED;
481 }
482 if (work && work != old_work) {
483 spin_unlock_irq(&wqe->lock);
Jens Axboe7d723062019-11-12 22:31:31 -0700484
485 if (put_work && wq->put_work) {
486 wq->put_work(put_work);
487 put_work = NULL;
488 }
489
Jens Axboe771b53d02019-10-22 10:25:58 -0600490 /* dependent work not hashed */
491 hash = -1U;
492 goto next;
493 }
494 } while (1);
495}
496
Jens Axboee995d512019-12-07 21:06:46 -0700497static inline void io_worker_spin_for_work(struct io_wqe *wqe)
498{
499 int i = 0;
500
501 while (++i < 1000) {
502 if (io_wqe_run_queue(wqe))
503 break;
504 if (need_resched())
505 break;
506 cpu_relax();
507 }
508}
509
Jens Axboe771b53d02019-10-22 10:25:58 -0600510static int io_wqe_worker(void *data)
511{
512 struct io_worker *worker = data;
513 struct io_wqe *wqe = worker->wqe;
514 struct io_wq *wq = wqe->wq;
Jens Axboee995d512019-12-07 21:06:46 -0700515 bool did_work;
Jens Axboe771b53d02019-10-22 10:25:58 -0600516
517 io_worker_start(wqe, worker);
518
Jens Axboee995d512019-12-07 21:06:46 -0700519 did_work = false;
Jens Axboe771b53d02019-10-22 10:25:58 -0600520 while (!test_bit(IO_WQ_BIT_EXIT, &wq->state)) {
Jens Axboe506d95f2019-12-07 21:03:59 -0700521 set_current_state(TASK_INTERRUPTIBLE);
Jens Axboee995d512019-12-07 21:06:46 -0700522loop:
523 if (did_work)
524 io_worker_spin_for_work(wqe);
Jens Axboe771b53d02019-10-22 10:25:58 -0600525 spin_lock_irq(&wqe->lock);
526 if (io_wqe_run_queue(wqe)) {
527 __set_current_state(TASK_RUNNING);
528 io_worker_handle_work(worker);
Jens Axboee995d512019-12-07 21:06:46 -0700529 did_work = true;
530 goto loop;
Jens Axboe771b53d02019-10-22 10:25:58 -0600531 }
Jens Axboee995d512019-12-07 21:06:46 -0700532 did_work = false;
Jens Axboe771b53d02019-10-22 10:25:58 -0600533 /* drops the lock on success, retry */
534 if (__io_worker_idle(wqe, worker)) {
535 __release(&wqe->lock);
Jens Axboee995d512019-12-07 21:06:46 -0700536 goto loop;
Jens Axboe771b53d02019-10-22 10:25:58 -0600537 }
538 spin_unlock_irq(&wqe->lock);
539 if (signal_pending(current))
540 flush_signals(current);
541 if (schedule_timeout(WORKER_IDLE_TIMEOUT))
542 continue;
543 /* timed out, exit unless we're the fixed worker */
544 if (test_bit(IO_WQ_BIT_EXIT, &wq->state) ||
545 !(worker->flags & IO_WORKER_F_FIXED))
546 break;
547 }
548
Jens Axboe771b53d02019-10-22 10:25:58 -0600549 if (test_bit(IO_WQ_BIT_EXIT, &wq->state)) {
550 spin_lock_irq(&wqe->lock);
Jens Axboe6206f0e2019-11-26 11:59:32 -0700551 if (!wq_list_empty(&wqe->work_list))
Jens Axboe771b53d02019-10-22 10:25:58 -0600552 io_worker_handle_work(worker);
553 else
554 spin_unlock_irq(&wqe->lock);
555 }
556
557 io_worker_exit(worker);
558 return 0;
559}
560
561/*
Jens Axboe771b53d02019-10-22 10:25:58 -0600562 * Called when a worker is scheduled in. Mark us as currently running.
563 */
564void io_wq_worker_running(struct task_struct *tsk)
565{
566 struct io_worker *worker = kthread_data(tsk);
567 struct io_wqe *wqe = worker->wqe;
568
569 if (!(worker->flags & IO_WORKER_F_UP))
570 return;
571 if (worker->flags & IO_WORKER_F_RUNNING)
572 return;
573 worker->flags |= IO_WORKER_F_RUNNING;
Jens Axboec5def4a2019-11-07 11:41:16 -0700574 io_wqe_inc_running(wqe, worker);
Jens Axboe771b53d02019-10-22 10:25:58 -0600575}
576
577/*
578 * Called when worker is going to sleep. If there are no workers currently
579 * running and we have work pending, wake up a free one or have the manager
580 * set one up.
581 */
582void io_wq_worker_sleeping(struct task_struct *tsk)
583{
584 struct io_worker *worker = kthread_data(tsk);
585 struct io_wqe *wqe = worker->wqe;
586
587 if (!(worker->flags & IO_WORKER_F_UP))
588 return;
589 if (!(worker->flags & IO_WORKER_F_RUNNING))
590 return;
591
592 worker->flags &= ~IO_WORKER_F_RUNNING;
593
594 spin_lock_irq(&wqe->lock);
Jens Axboec5def4a2019-11-07 11:41:16 -0700595 io_wqe_dec_running(wqe, worker);
Jens Axboe771b53d02019-10-22 10:25:58 -0600596 spin_unlock_irq(&wqe->lock);
597}
598
Jens Axboeb60fda62019-11-19 08:37:07 -0700599static bool create_io_worker(struct io_wq *wq, struct io_wqe *wqe, int index)
Jens Axboe771b53d02019-10-22 10:25:58 -0600600{
Jens Axboec5def4a2019-11-07 11:41:16 -0700601 struct io_wqe_acct *acct =&wqe->acct[index];
Jens Axboe771b53d02019-10-22 10:25:58 -0600602 struct io_worker *worker;
603
Jann Hornad6e0052019-11-26 17:39:45 +0100604 worker = kzalloc_node(sizeof(*worker), GFP_KERNEL, wqe->node);
Jens Axboe771b53d02019-10-22 10:25:58 -0600605 if (!worker)
Jens Axboeb60fda62019-11-19 08:37:07 -0700606 return false;
Jens Axboe771b53d02019-10-22 10:25:58 -0600607
608 refcount_set(&worker->ref, 1);
609 worker->nulls_node.pprev = NULL;
Jens Axboe771b53d02019-10-22 10:25:58 -0600610 worker->wqe = wqe;
Jens Axboe36c2f922019-11-13 09:43:34 -0700611 spin_lock_init(&worker->lock);
Jens Axboe771b53d02019-10-22 10:25:58 -0600612
613 worker->task = kthread_create_on_node(io_wqe_worker, worker, wqe->node,
Jens Axboec5def4a2019-11-07 11:41:16 -0700614 "io_wqe_worker-%d/%d", index, wqe->node);
Jens Axboe771b53d02019-10-22 10:25:58 -0600615 if (IS_ERR(worker->task)) {
616 kfree(worker);
Jens Axboeb60fda62019-11-19 08:37:07 -0700617 return false;
Jens Axboe771b53d02019-10-22 10:25:58 -0600618 }
619
620 spin_lock_irq(&wqe->lock);
Jens Axboe021d1cd2019-11-14 08:00:41 -0700621 hlist_nulls_add_head_rcu(&worker->nulls_node, &wqe->free_list);
Jens Axboee61df662019-11-13 13:54:49 -0700622 list_add_tail_rcu(&worker->all_list, &wqe->all_list);
Jens Axboe771b53d02019-10-22 10:25:58 -0600623 worker->flags |= IO_WORKER_F_FREE;
Jens Axboec5def4a2019-11-07 11:41:16 -0700624 if (index == IO_WQ_ACCT_BOUND)
625 worker->flags |= IO_WORKER_F_BOUND;
626 if (!acct->nr_workers && (worker->flags & IO_WORKER_F_BOUND))
Jens Axboe771b53d02019-10-22 10:25:58 -0600627 worker->flags |= IO_WORKER_F_FIXED;
Jens Axboec5def4a2019-11-07 11:41:16 -0700628 acct->nr_workers++;
Jens Axboe771b53d02019-10-22 10:25:58 -0600629 spin_unlock_irq(&wqe->lock);
630
Jens Axboec5def4a2019-11-07 11:41:16 -0700631 if (index == IO_WQ_ACCT_UNBOUND)
632 atomic_inc(&wq->user->processes);
633
Jens Axboe771b53d02019-10-22 10:25:58 -0600634 wake_up_process(worker->task);
Jens Axboeb60fda62019-11-19 08:37:07 -0700635 return true;
Jens Axboe771b53d02019-10-22 10:25:58 -0600636}
637
Jens Axboec5def4a2019-11-07 11:41:16 -0700638static inline bool io_wqe_need_worker(struct io_wqe *wqe, int index)
Jens Axboe771b53d02019-10-22 10:25:58 -0600639 __must_hold(wqe->lock)
640{
Jens Axboec5def4a2019-11-07 11:41:16 -0700641 struct io_wqe_acct *acct = &wqe->acct[index];
Jens Axboe771b53d02019-10-22 10:25:58 -0600642
Jens Axboec5def4a2019-11-07 11:41:16 -0700643 /* if we have available workers or no work, no need */
Jens Axboe021d1cd2019-11-14 08:00:41 -0700644 if (!hlist_nulls_empty(&wqe->free_list) || !io_wqe_run_queue(wqe))
Jens Axboec5def4a2019-11-07 11:41:16 -0700645 return false;
646 return acct->nr_workers < acct->max_workers;
Jens Axboe771b53d02019-10-22 10:25:58 -0600647}
648
649/*
650 * Manager thread. Tasked with creating new workers, if we need them.
651 */
652static int io_wq_manager(void *data)
653{
654 struct io_wq *wq = data;
Jann Horn3fc50ab2019-11-26 19:10:20 +0100655 int workers_to_create = num_possible_nodes();
656 int node;
Jens Axboeb60fda62019-11-19 08:37:07 -0700657
658 /* create fixed workers */
Jann Horn3fc50ab2019-11-26 19:10:20 +0100659 refcount_set(&wq->refs, workers_to_create);
660 for_each_node(node) {
661 if (!create_io_worker(wq, wq->wqes[node], IO_WQ_ACCT_BOUND))
662 goto err;
663 workers_to_create--;
Jens Axboeb60fda62019-11-19 08:37:07 -0700664 }
665
666 complete(&wq->done);
Jens Axboe771b53d02019-10-22 10:25:58 -0600667
668 while (!kthread_should_stop()) {
Jann Horn3fc50ab2019-11-26 19:10:20 +0100669 for_each_node(node) {
670 struct io_wqe *wqe = wq->wqes[node];
Jens Axboec5def4a2019-11-07 11:41:16 -0700671 bool fork_worker[2] = { false, false };
Jens Axboe771b53d02019-10-22 10:25:58 -0600672
673 spin_lock_irq(&wqe->lock);
Jens Axboec5def4a2019-11-07 11:41:16 -0700674 if (io_wqe_need_worker(wqe, IO_WQ_ACCT_BOUND))
675 fork_worker[IO_WQ_ACCT_BOUND] = true;
676 if (io_wqe_need_worker(wqe, IO_WQ_ACCT_UNBOUND))
677 fork_worker[IO_WQ_ACCT_UNBOUND] = true;
Jens Axboe771b53d02019-10-22 10:25:58 -0600678 spin_unlock_irq(&wqe->lock);
Jens Axboec5def4a2019-11-07 11:41:16 -0700679 if (fork_worker[IO_WQ_ACCT_BOUND])
680 create_io_worker(wq, wqe, IO_WQ_ACCT_BOUND);
681 if (fork_worker[IO_WQ_ACCT_UNBOUND])
682 create_io_worker(wq, wqe, IO_WQ_ACCT_UNBOUND);
Jens Axboe771b53d02019-10-22 10:25:58 -0600683 }
684 set_current_state(TASK_INTERRUPTIBLE);
685 schedule_timeout(HZ);
686 }
687
688 return 0;
Jens Axboeb60fda62019-11-19 08:37:07 -0700689err:
690 set_bit(IO_WQ_BIT_ERROR, &wq->state);
691 set_bit(IO_WQ_BIT_EXIT, &wq->state);
Jann Horn3fc50ab2019-11-26 19:10:20 +0100692 if (refcount_sub_and_test(workers_to_create, &wq->refs))
Jens Axboeb60fda62019-11-19 08:37:07 -0700693 complete(&wq->done);
694 return 0;
Jens Axboe771b53d02019-10-22 10:25:58 -0600695}
696
Jens Axboec5def4a2019-11-07 11:41:16 -0700697static bool io_wq_can_queue(struct io_wqe *wqe, struct io_wqe_acct *acct,
698 struct io_wq_work *work)
699{
700 bool free_worker;
701
702 if (!(work->flags & IO_WQ_WORK_UNBOUND))
703 return true;
704 if (atomic_read(&acct->nr_running))
705 return true;
706
707 rcu_read_lock();
Jens Axboe021d1cd2019-11-14 08:00:41 -0700708 free_worker = !hlist_nulls_empty(&wqe->free_list);
Jens Axboec5def4a2019-11-07 11:41:16 -0700709 rcu_read_unlock();
710 if (free_worker)
711 return true;
712
713 if (atomic_read(&wqe->wq->user->processes) >= acct->max_workers &&
714 !(capable(CAP_SYS_RESOURCE) || capable(CAP_SYS_ADMIN)))
715 return false;
716
717 return true;
718}
719
Jens Axboe771b53d02019-10-22 10:25:58 -0600720static void io_wqe_enqueue(struct io_wqe *wqe, struct io_wq_work *work)
721{
Jens Axboec5def4a2019-11-07 11:41:16 -0700722 struct io_wqe_acct *acct = io_work_get_acct(wqe, work);
Jens Axboe771b53d02019-10-22 10:25:58 -0600723 unsigned long flags;
724
Jens Axboec5def4a2019-11-07 11:41:16 -0700725 /*
726 * Do early check to see if we need a new unbound worker, and if we do,
727 * if we're allowed to do so. This isn't 100% accurate as there's a
728 * gap between this check and incrementing the value, but that's OK.
729 * It's close enough to not be an issue, fork() has the same delay.
730 */
731 if (unlikely(!io_wq_can_queue(wqe, acct, work))) {
732 work->flags |= IO_WQ_WORK_CANCEL;
733 work->func(&work);
734 return;
735 }
736
Jens Axboe771b53d02019-10-22 10:25:58 -0600737 spin_lock_irqsave(&wqe->lock, flags);
Jens Axboe6206f0e2019-11-26 11:59:32 -0700738 wq_list_add_tail(&work->list, &wqe->work_list);
Jens Axboe771b53d02019-10-22 10:25:58 -0600739 wqe->flags &= ~IO_WQE_FLAG_STALLED;
740 spin_unlock_irqrestore(&wqe->lock, flags);
741
Jens Axboec5def4a2019-11-07 11:41:16 -0700742 if (!atomic_read(&acct->nr_running))
743 io_wqe_wake_worker(wqe, acct);
Jens Axboe771b53d02019-10-22 10:25:58 -0600744}
745
746void io_wq_enqueue(struct io_wq *wq, struct io_wq_work *work)
747{
748 struct io_wqe *wqe = wq->wqes[numa_node_id()];
749
750 io_wqe_enqueue(wqe, work);
751}
752
753/*
754 * Enqueue work, hashed by some key. Work items that hash to the same value
755 * will not be done in parallel. Used to limit concurrent writes, generally
756 * hashed by inode.
757 */
758void io_wq_enqueue_hashed(struct io_wq *wq, struct io_wq_work *work, void *val)
759{
760 struct io_wqe *wqe = wq->wqes[numa_node_id()];
761 unsigned bit;
762
763
764 bit = hash_ptr(val, IO_WQ_HASH_ORDER);
765 work->flags |= (IO_WQ_WORK_HASHED | (bit << IO_WQ_HASH_SHIFT));
766 io_wqe_enqueue(wqe, work);
767}
768
769static bool io_wqe_worker_send_sig(struct io_worker *worker, void *data)
770{
771 send_sig(SIGINT, worker->task, 1);
772 return false;
773}
774
775/*
776 * Iterate the passed in list and call the specific function for each
777 * worker that isn't exiting
778 */
779static bool io_wq_for_each_worker(struct io_wqe *wqe,
Jens Axboe771b53d02019-10-22 10:25:58 -0600780 bool (*func)(struct io_worker *, void *),
781 void *data)
782{
Jens Axboe771b53d02019-10-22 10:25:58 -0600783 struct io_worker *worker;
784 bool ret = false;
785
Jens Axboee61df662019-11-13 13:54:49 -0700786 list_for_each_entry_rcu(worker, &wqe->all_list, all_list) {
Jens Axboe771b53d02019-10-22 10:25:58 -0600787 if (io_worker_get(worker)) {
788 ret = func(worker, data);
789 io_worker_release(worker);
790 if (ret)
791 break;
792 }
793 }
Jens Axboee61df662019-11-13 13:54:49 -0700794
Jens Axboe771b53d02019-10-22 10:25:58 -0600795 return ret;
796}
797
798void io_wq_cancel_all(struct io_wq *wq)
799{
Jann Horn3fc50ab2019-11-26 19:10:20 +0100800 int node;
Jens Axboe771b53d02019-10-22 10:25:58 -0600801
802 set_bit(IO_WQ_BIT_CANCEL, &wq->state);
803
Jens Axboe771b53d02019-10-22 10:25:58 -0600804 rcu_read_lock();
Jann Horn3fc50ab2019-11-26 19:10:20 +0100805 for_each_node(node) {
806 struct io_wqe *wqe = wq->wqes[node];
Jens Axboe771b53d02019-10-22 10:25:58 -0600807
Jens Axboee61df662019-11-13 13:54:49 -0700808 io_wq_for_each_worker(wqe, io_wqe_worker_send_sig, NULL);
Jens Axboe771b53d02019-10-22 10:25:58 -0600809 }
810 rcu_read_unlock();
811}
812
Jens Axboe62755e32019-10-28 21:49:21 -0600813struct io_cb_cancel_data {
814 struct io_wqe *wqe;
815 work_cancel_fn *cancel;
816 void *caller_data;
817};
818
819static bool io_work_cancel(struct io_worker *worker, void *cancel_data)
820{
821 struct io_cb_cancel_data *data = cancel_data;
Jens Axboe6f726532019-11-05 13:51:51 -0700822 unsigned long flags;
Jens Axboe62755e32019-10-28 21:49:21 -0600823 bool ret = false;
824
825 /*
826 * Hold the lock to avoid ->cur_work going out of scope, caller
Jens Axboe36c2f922019-11-13 09:43:34 -0700827 * may dereference the passed in work.
Jens Axboe62755e32019-10-28 21:49:21 -0600828 */
Jens Axboe36c2f922019-11-13 09:43:34 -0700829 spin_lock_irqsave(&worker->lock, flags);
Jens Axboe62755e32019-10-28 21:49:21 -0600830 if (worker->cur_work &&
831 data->cancel(worker->cur_work, data->caller_data)) {
832 send_sig(SIGINT, worker->task, 1);
833 ret = true;
834 }
Jens Axboe36c2f922019-11-13 09:43:34 -0700835 spin_unlock_irqrestore(&worker->lock, flags);
Jens Axboe62755e32019-10-28 21:49:21 -0600836
837 return ret;
838}
839
840static enum io_wq_cancel io_wqe_cancel_cb_work(struct io_wqe *wqe,
841 work_cancel_fn *cancel,
842 void *cancel_data)
843{
844 struct io_cb_cancel_data data = {
845 .wqe = wqe,
846 .cancel = cancel,
847 .caller_data = cancel_data,
848 };
Jens Axboe6206f0e2019-11-26 11:59:32 -0700849 struct io_wq_work_node *node, *prev;
Jens Axboe62755e32019-10-28 21:49:21 -0600850 struct io_wq_work *work;
Jens Axboe6f726532019-11-05 13:51:51 -0700851 unsigned long flags;
Jens Axboe62755e32019-10-28 21:49:21 -0600852 bool found = false;
853
Jens Axboe6f726532019-11-05 13:51:51 -0700854 spin_lock_irqsave(&wqe->lock, flags);
Jens Axboe6206f0e2019-11-26 11:59:32 -0700855 wq_list_for_each(node, prev, &wqe->work_list) {
856 work = container_of(node, struct io_wq_work, list);
857
Jens Axboe62755e32019-10-28 21:49:21 -0600858 if (cancel(work, cancel_data)) {
Jens Axboe6206f0e2019-11-26 11:59:32 -0700859 wq_node_del(&wqe->work_list, node, prev);
Jens Axboe62755e32019-10-28 21:49:21 -0600860 found = true;
861 break;
862 }
863 }
Jens Axboe6f726532019-11-05 13:51:51 -0700864 spin_unlock_irqrestore(&wqe->lock, flags);
Jens Axboe62755e32019-10-28 21:49:21 -0600865
866 if (found) {
867 work->flags |= IO_WQ_WORK_CANCEL;
868 work->func(&work);
869 return IO_WQ_CANCEL_OK;
870 }
871
872 rcu_read_lock();
Jens Axboee61df662019-11-13 13:54:49 -0700873 found = io_wq_for_each_worker(wqe, io_work_cancel, &data);
Jens Axboe62755e32019-10-28 21:49:21 -0600874 rcu_read_unlock();
875 return found ? IO_WQ_CANCEL_RUNNING : IO_WQ_CANCEL_NOTFOUND;
876}
877
878enum io_wq_cancel io_wq_cancel_cb(struct io_wq *wq, work_cancel_fn *cancel,
879 void *data)
880{
881 enum io_wq_cancel ret = IO_WQ_CANCEL_NOTFOUND;
Jann Horn3fc50ab2019-11-26 19:10:20 +0100882 int node;
Jens Axboe62755e32019-10-28 21:49:21 -0600883
Jann Horn3fc50ab2019-11-26 19:10:20 +0100884 for_each_node(node) {
885 struct io_wqe *wqe = wq->wqes[node];
Jens Axboe62755e32019-10-28 21:49:21 -0600886
887 ret = io_wqe_cancel_cb_work(wqe, cancel, data);
888 if (ret != IO_WQ_CANCEL_NOTFOUND)
889 break;
890 }
891
892 return ret;
893}
894
Jens Axboe771b53d02019-10-22 10:25:58 -0600895static bool io_wq_worker_cancel(struct io_worker *worker, void *data)
896{
897 struct io_wq_work *work = data;
Jens Axboe36c2f922019-11-13 09:43:34 -0700898 unsigned long flags;
899 bool ret = false;
Jens Axboe771b53d02019-10-22 10:25:58 -0600900
Jens Axboe36c2f922019-11-13 09:43:34 -0700901 if (worker->cur_work != work)
902 return false;
903
904 spin_lock_irqsave(&worker->lock, flags);
Jens Axboe771b53d02019-10-22 10:25:58 -0600905 if (worker->cur_work == work) {
906 send_sig(SIGINT, worker->task, 1);
Jens Axboe36c2f922019-11-13 09:43:34 -0700907 ret = true;
Jens Axboe771b53d02019-10-22 10:25:58 -0600908 }
Jens Axboe36c2f922019-11-13 09:43:34 -0700909 spin_unlock_irqrestore(&worker->lock, flags);
Jens Axboe771b53d02019-10-22 10:25:58 -0600910
Jens Axboe36c2f922019-11-13 09:43:34 -0700911 return ret;
Jens Axboe771b53d02019-10-22 10:25:58 -0600912}
913
914static enum io_wq_cancel io_wqe_cancel_work(struct io_wqe *wqe,
915 struct io_wq_work *cwork)
916{
Jens Axboe6206f0e2019-11-26 11:59:32 -0700917 struct io_wq_work_node *node, *prev;
Jens Axboe771b53d02019-10-22 10:25:58 -0600918 struct io_wq_work *work;
Jens Axboe6f726532019-11-05 13:51:51 -0700919 unsigned long flags;
Jens Axboe771b53d02019-10-22 10:25:58 -0600920 bool found = false;
921
922 cwork->flags |= IO_WQ_WORK_CANCEL;
923
924 /*
925 * First check pending list, if we're lucky we can just remove it
926 * from there. CANCEL_OK means that the work is returned as-new,
927 * no completion will be posted for it.
928 */
Jens Axboe6f726532019-11-05 13:51:51 -0700929 spin_lock_irqsave(&wqe->lock, flags);
Jens Axboe6206f0e2019-11-26 11:59:32 -0700930 wq_list_for_each(node, prev, &wqe->work_list) {
931 work = container_of(node, struct io_wq_work, list);
932
Jens Axboe771b53d02019-10-22 10:25:58 -0600933 if (work == cwork) {
Jens Axboe6206f0e2019-11-26 11:59:32 -0700934 wq_node_del(&wqe->work_list, node, prev);
Jens Axboe771b53d02019-10-22 10:25:58 -0600935 found = true;
936 break;
937 }
938 }
Jens Axboe6f726532019-11-05 13:51:51 -0700939 spin_unlock_irqrestore(&wqe->lock, flags);
Jens Axboe771b53d02019-10-22 10:25:58 -0600940
941 if (found) {
942 work->flags |= IO_WQ_WORK_CANCEL;
943 work->func(&work);
944 return IO_WQ_CANCEL_OK;
945 }
946
947 /*
948 * Now check if a free (going busy) or busy worker has the work
949 * currently running. If we find it there, we'll return CANCEL_RUNNING
Brian Gianforcarod195a662019-12-13 03:09:50 -0800950 * as an indication that we attempt to signal cancellation. The
Jens Axboe771b53d02019-10-22 10:25:58 -0600951 * completion will run normally in this case.
952 */
953 rcu_read_lock();
Jens Axboee61df662019-11-13 13:54:49 -0700954 found = io_wq_for_each_worker(wqe, io_wq_worker_cancel, cwork);
Jens Axboe771b53d02019-10-22 10:25:58 -0600955 rcu_read_unlock();
956 return found ? IO_WQ_CANCEL_RUNNING : IO_WQ_CANCEL_NOTFOUND;
957}
958
959enum io_wq_cancel io_wq_cancel_work(struct io_wq *wq, struct io_wq_work *cwork)
960{
961 enum io_wq_cancel ret = IO_WQ_CANCEL_NOTFOUND;
Jann Horn3fc50ab2019-11-26 19:10:20 +0100962 int node;
Jens Axboe771b53d02019-10-22 10:25:58 -0600963
Jann Horn3fc50ab2019-11-26 19:10:20 +0100964 for_each_node(node) {
965 struct io_wqe *wqe = wq->wqes[node];
Jens Axboe771b53d02019-10-22 10:25:58 -0600966
967 ret = io_wqe_cancel_work(wqe, cwork);
968 if (ret != IO_WQ_CANCEL_NOTFOUND)
969 break;
970 }
971
972 return ret;
973}
974
975struct io_wq_flush_data {
976 struct io_wq_work work;
977 struct completion done;
978};
979
980static void io_wq_flush_func(struct io_wq_work **workptr)
981{
982 struct io_wq_work *work = *workptr;
983 struct io_wq_flush_data *data;
984
985 data = container_of(work, struct io_wq_flush_data, work);
986 complete(&data->done);
987}
988
989/*
990 * Doesn't wait for previously queued work to finish. When this completes,
991 * it just means that previously queued work was started.
992 */
993void io_wq_flush(struct io_wq *wq)
994{
995 struct io_wq_flush_data data;
Jann Horn3fc50ab2019-11-26 19:10:20 +0100996 int node;
Jens Axboe771b53d02019-10-22 10:25:58 -0600997
Jann Horn3fc50ab2019-11-26 19:10:20 +0100998 for_each_node(node) {
999 struct io_wqe *wqe = wq->wqes[node];
Jens Axboe771b53d02019-10-22 10:25:58 -06001000
1001 init_completion(&data.done);
1002 INIT_IO_WORK(&data.work, io_wq_flush_func);
Jens Axboe7d723062019-11-12 22:31:31 -07001003 data.work.flags |= IO_WQ_WORK_INTERNAL;
Jens Axboe771b53d02019-10-22 10:25:58 -06001004 io_wqe_enqueue(wqe, &data.work);
1005 wait_for_completion(&data.done);
1006 }
1007}
1008
Jens Axboe576a3472019-11-25 08:49:20 -07001009struct io_wq *io_wq_create(unsigned bounded, struct io_wq_data *data)
Jens Axboe771b53d02019-10-22 10:25:58 -06001010{
Jann Horn3fc50ab2019-11-26 19:10:20 +01001011 int ret = -ENOMEM, node;
Jens Axboe771b53d02019-10-22 10:25:58 -06001012 struct io_wq *wq;
1013
Jann Hornad6e0052019-11-26 17:39:45 +01001014 wq = kzalloc(sizeof(*wq), GFP_KERNEL);
Jens Axboe771b53d02019-10-22 10:25:58 -06001015 if (!wq)
1016 return ERR_PTR(-ENOMEM);
1017
Jann Horn3fc50ab2019-11-26 19:10:20 +01001018 wq->wqes = kcalloc(nr_node_ids, sizeof(struct io_wqe *), GFP_KERNEL);
Jens Axboe771b53d02019-10-22 10:25:58 -06001019 if (!wq->wqes) {
1020 kfree(wq);
1021 return ERR_PTR(-ENOMEM);
1022 }
1023
Jens Axboe576a3472019-11-25 08:49:20 -07001024 wq->get_work = data->get_work;
1025 wq->put_work = data->put_work;
Jens Axboe7d723062019-11-12 22:31:31 -07001026
Jens Axboec5def4a2019-11-07 11:41:16 -07001027 /* caller must already hold a reference to this */
Jens Axboe576a3472019-11-25 08:49:20 -07001028 wq->user = data->user;
Jens Axboe181e4482019-11-25 08:52:30 -07001029 wq->creds = data->creds;
Jens Axboec5def4a2019-11-07 11:41:16 -07001030
Jann Horn3fc50ab2019-11-26 19:10:20 +01001031 for_each_node(node) {
Jens Axboe771b53d02019-10-22 10:25:58 -06001032 struct io_wqe *wqe;
1033
Jann Hornad6e0052019-11-26 17:39:45 +01001034 wqe = kzalloc_node(sizeof(struct io_wqe), GFP_KERNEL, node);
Jens Axboe771b53d02019-10-22 10:25:58 -06001035 if (!wqe)
Jann Horn3fc50ab2019-11-26 19:10:20 +01001036 goto err;
1037 wq->wqes[node] = wqe;
Jens Axboe771b53d02019-10-22 10:25:58 -06001038 wqe->node = node;
Jens Axboec5def4a2019-11-07 11:41:16 -07001039 wqe->acct[IO_WQ_ACCT_BOUND].max_workers = bounded;
1040 atomic_set(&wqe->acct[IO_WQ_ACCT_BOUND].nr_running, 0);
Jens Axboe576a3472019-11-25 08:49:20 -07001041 if (wq->user) {
Jens Axboec5def4a2019-11-07 11:41:16 -07001042 wqe->acct[IO_WQ_ACCT_UNBOUND].max_workers =
1043 task_rlimit(current, RLIMIT_NPROC);
1044 }
1045 atomic_set(&wqe->acct[IO_WQ_ACCT_UNBOUND].nr_running, 0);
Jens Axboe771b53d02019-10-22 10:25:58 -06001046 wqe->node = node;
1047 wqe->wq = wq;
1048 spin_lock_init(&wqe->lock);
Jens Axboe6206f0e2019-11-26 11:59:32 -07001049 INIT_WQ_LIST(&wqe->work_list);
Jens Axboe021d1cd2019-11-14 08:00:41 -07001050 INIT_HLIST_NULLS_HEAD(&wqe->free_list, 0);
Jens Axboee61df662019-11-13 13:54:49 -07001051 INIT_LIST_HEAD(&wqe->all_list);
Jens Axboe771b53d02019-10-22 10:25:58 -06001052 }
1053
1054 init_completion(&wq->done);
1055
Jens Axboe771b53d02019-10-22 10:25:58 -06001056 /* caller must have already done mmgrab() on this mm */
Jens Axboe576a3472019-11-25 08:49:20 -07001057 wq->mm = data->mm;
Jens Axboe771b53d02019-10-22 10:25:58 -06001058
1059 wq->manager = kthread_create(io_wq_manager, wq, "io_wq_manager");
1060 if (!IS_ERR(wq->manager)) {
1061 wake_up_process(wq->manager);
Jens Axboeb60fda62019-11-19 08:37:07 -07001062 wait_for_completion(&wq->done);
1063 if (test_bit(IO_WQ_BIT_ERROR, &wq->state)) {
1064 ret = -ENOMEM;
1065 goto err;
1066 }
1067 reinit_completion(&wq->done);
Jens Axboe771b53d02019-10-22 10:25:58 -06001068 return wq;
1069 }
1070
1071 ret = PTR_ERR(wq->manager);
Jens Axboe771b53d02019-10-22 10:25:58 -06001072 complete(&wq->done);
Jens Axboeb60fda62019-11-19 08:37:07 -07001073err:
Jann Horn3fc50ab2019-11-26 19:10:20 +01001074 for_each_node(node)
1075 kfree(wq->wqes[node]);
Jens Axboeb60fda62019-11-19 08:37:07 -07001076 kfree(wq->wqes);
1077 kfree(wq);
Jens Axboe771b53d02019-10-22 10:25:58 -06001078 return ERR_PTR(ret);
1079}
1080
1081static bool io_wq_worker_wake(struct io_worker *worker, void *data)
1082{
1083 wake_up_process(worker->task);
1084 return false;
1085}
1086
1087void io_wq_destroy(struct io_wq *wq)
1088{
Jann Horn3fc50ab2019-11-26 19:10:20 +01001089 int node;
Jens Axboe771b53d02019-10-22 10:25:58 -06001090
Jens Axboeb60fda62019-11-19 08:37:07 -07001091 set_bit(IO_WQ_BIT_EXIT, &wq->state);
1092 if (wq->manager)
Jens Axboe771b53d02019-10-22 10:25:58 -06001093 kthread_stop(wq->manager);
Jens Axboe771b53d02019-10-22 10:25:58 -06001094
1095 rcu_read_lock();
Jann Horn3fc50ab2019-11-26 19:10:20 +01001096 for_each_node(node)
1097 io_wq_for_each_worker(wq->wqes[node], io_wq_worker_wake, NULL);
Jens Axboe771b53d02019-10-22 10:25:58 -06001098 rcu_read_unlock();
1099
1100 wait_for_completion(&wq->done);
1101
Jann Horn3fc50ab2019-11-26 19:10:20 +01001102 for_each_node(node)
1103 kfree(wq->wqes[node]);
Jens Axboe771b53d02019-10-22 10:25:58 -06001104 kfree(wq->wqes);
1105 kfree(wq);
1106}