blob: 6b663ddceb4277ceb6f5b023a5ae7684958c54b8 [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;
95 struct hlist_nulls_head busy_list;
Jens Axboee61df662019-11-13 13:54:49 -070096 struct list_head all_list;
Jens Axboe771b53d02019-10-22 10:25:58 -060097
98 struct io_wq *wq;
99};
100
101/*
102 * Per io_wq state
103 */
104struct io_wq {
105 struct io_wqe **wqes;
106 unsigned long state;
Jens Axboe771b53d02019-10-22 10:25:58 -0600107
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 Axboe0b8c0ec2019-12-02 08:50:00 -0700113 const struct cred *creds;
Jens Axboe771b53d02019-10-22 10:25:58 -0600114 struct mm_struct *mm;
115 refcount_t refs;
116 struct completion done;
117};
118
Jens Axboe771b53d02019-10-22 10:25:58 -0600119static bool io_worker_get(struct io_worker *worker)
120{
121 return refcount_inc_not_zero(&worker->ref);
122}
123
124static void io_worker_release(struct io_worker *worker)
125{
126 if (refcount_dec_and_test(&worker->ref))
127 wake_up_process(worker->task);
128}
129
130/*
131 * Note: drops the wqe->lock if returning true! The caller must re-acquire
132 * the lock in that case. Some callers need to restart handling if this
133 * happens, so we can't just re-acquire the lock on behalf of the caller.
134 */
135static bool __io_worker_unuse(struct io_wqe *wqe, struct io_worker *worker)
136{
Jens Axboefcb323c2019-10-24 12:39:47 -0600137 bool dropped_lock = false;
138
Jens Axboe181e4482019-11-25 08:52:30 -0700139 if (worker->creds) {
140 revert_creds(worker->creds);
141 worker->creds = NULL;
142 }
143
Jens Axboefcb323c2019-10-24 12:39:47 -0600144 if (current->files != worker->restore_files) {
145 __acquire(&wqe->lock);
146 spin_unlock_irq(&wqe->lock);
147 dropped_lock = true;
148
149 task_lock(current);
150 current->files = worker->restore_files;
151 task_unlock(current);
152 }
153
Jens Axboe771b53d02019-10-22 10:25:58 -0600154 /*
155 * If we have an active mm, we need to drop the wq lock before unusing
156 * it. If we do, return true and let the caller retry the idle loop.
157 */
158 if (worker->mm) {
Jens Axboefcb323c2019-10-24 12:39:47 -0600159 if (!dropped_lock) {
160 __acquire(&wqe->lock);
161 spin_unlock_irq(&wqe->lock);
162 dropped_lock = true;
163 }
Jens Axboe771b53d02019-10-22 10:25:58 -0600164 __set_current_state(TASK_RUNNING);
165 set_fs(KERNEL_DS);
166 unuse_mm(worker->mm);
167 mmput(worker->mm);
168 worker->mm = NULL;
Jens Axboe771b53d02019-10-22 10:25:58 -0600169 }
170
Jens Axboefcb323c2019-10-24 12:39:47 -0600171 return dropped_lock;
Jens Axboe771b53d02019-10-22 10:25:58 -0600172}
173
Jens Axboec5def4a2019-11-07 11:41:16 -0700174static inline struct io_wqe_acct *io_work_get_acct(struct io_wqe *wqe,
175 struct io_wq_work *work)
176{
177 if (work->flags & IO_WQ_WORK_UNBOUND)
178 return &wqe->acct[IO_WQ_ACCT_UNBOUND];
179
180 return &wqe->acct[IO_WQ_ACCT_BOUND];
181}
182
183static inline struct io_wqe_acct *io_wqe_get_acct(struct io_wqe *wqe,
184 struct io_worker *worker)
185{
186 if (worker->flags & IO_WORKER_F_BOUND)
187 return &wqe->acct[IO_WQ_ACCT_BOUND];
188
189 return &wqe->acct[IO_WQ_ACCT_UNBOUND];
190}
191
Jens Axboe771b53d02019-10-22 10:25:58 -0600192static void io_worker_exit(struct io_worker *worker)
193{
194 struct io_wqe *wqe = worker->wqe;
Jens Axboec5def4a2019-11-07 11:41:16 -0700195 struct io_wqe_acct *acct = io_wqe_get_acct(wqe, worker);
196 unsigned nr_workers;
Jens Axboe771b53d02019-10-22 10:25:58 -0600197
198 /*
199 * If we're not at zero, someone else is holding a brief reference
200 * to the worker. Wait for that to go away.
201 */
202 set_current_state(TASK_INTERRUPTIBLE);
203 if (!refcount_dec_and_test(&worker->ref))
204 schedule();
205 __set_current_state(TASK_RUNNING);
206
207 preempt_disable();
208 current->flags &= ~PF_IO_WORKER;
209 if (worker->flags & IO_WORKER_F_RUNNING)
Jens Axboec5def4a2019-11-07 11:41:16 -0700210 atomic_dec(&acct->nr_running);
211 if (!(worker->flags & IO_WORKER_F_BOUND))
212 atomic_dec(&wqe->wq->user->processes);
Jens Axboe771b53d02019-10-22 10:25:58 -0600213 worker->flags = 0;
214 preempt_enable();
215
216 spin_lock_irq(&wqe->lock);
217 hlist_nulls_del_rcu(&worker->nulls_node);
Jens Axboee61df662019-11-13 13:54:49 -0700218 list_del_rcu(&worker->all_list);
Jens Axboe771b53d02019-10-22 10:25:58 -0600219 if (__io_worker_unuse(wqe, worker)) {
220 __release(&wqe->lock);
221 spin_lock_irq(&wqe->lock);
222 }
Jens Axboec5def4a2019-11-07 11:41:16 -0700223 acct->nr_workers--;
224 nr_workers = wqe->acct[IO_WQ_ACCT_BOUND].nr_workers +
225 wqe->acct[IO_WQ_ACCT_UNBOUND].nr_workers;
Jens Axboe771b53d02019-10-22 10:25:58 -0600226 spin_unlock_irq(&wqe->lock);
227
228 /* all workers gone, wq exit can proceed */
Jens Axboec5def4a2019-11-07 11:41:16 -0700229 if (!nr_workers && refcount_dec_and_test(&wqe->wq->refs))
Jens Axboe771b53d02019-10-22 10:25:58 -0600230 complete(&wqe->wq->done);
231
YueHaibing364b05f2019-11-02 15:55:01 +0800232 kfree_rcu(worker, rcu);
Jens Axboe771b53d02019-10-22 10:25:58 -0600233}
234
Jens Axboec5def4a2019-11-07 11:41:16 -0700235static inline bool io_wqe_run_queue(struct io_wqe *wqe)
236 __must_hold(wqe->lock)
237{
Jens Axboe6206f0e2019-11-26 11:59:32 -0700238 if (!wq_list_empty(&wqe->work_list) &&
239 !(wqe->flags & IO_WQE_FLAG_STALLED))
Jens Axboec5def4a2019-11-07 11:41:16 -0700240 return true;
241 return false;
242}
243
244/*
245 * Check head of free list for an available worker. If one isn't available,
246 * caller must wake up the wq manager to create one.
247 */
248static bool io_wqe_activate_free_worker(struct io_wqe *wqe)
249 __must_hold(RCU)
250{
251 struct hlist_nulls_node *n;
252 struct io_worker *worker;
253
Jens Axboe021d1cd2019-11-14 08:00:41 -0700254 n = rcu_dereference(hlist_nulls_first_rcu(&wqe->free_list));
Jens Axboec5def4a2019-11-07 11:41:16 -0700255 if (is_a_nulls(n))
256 return false;
257
258 worker = hlist_nulls_entry(n, struct io_worker, nulls_node);
259 if (io_worker_get(worker)) {
Jens Axboe506d95f2019-12-07 21:03:59 -0700260 wake_up_process(worker->task);
Jens Axboec5def4a2019-11-07 11:41:16 -0700261 io_worker_release(worker);
262 return true;
263 }
264
265 return false;
266}
267
268/*
269 * We need a worker. If we find a free one, we're good. If not, and we're
270 * below the max number of workers, wake up the manager to create one.
271 */
272static void io_wqe_wake_worker(struct io_wqe *wqe, struct io_wqe_acct *acct)
273{
274 bool ret;
275
276 /*
277 * Most likely an attempt to queue unbounded work on an io_wq that
278 * wasn't setup with any unbounded workers.
279 */
280 WARN_ON_ONCE(!acct->max_workers);
281
282 rcu_read_lock();
283 ret = io_wqe_activate_free_worker(wqe);
284 rcu_read_unlock();
285
286 if (!ret && acct->nr_workers < acct->max_workers)
287 wake_up_process(wqe->wq->manager);
288}
289
290static void io_wqe_inc_running(struct io_wqe *wqe, struct io_worker *worker)
291{
292 struct io_wqe_acct *acct = io_wqe_get_acct(wqe, worker);
293
294 atomic_inc(&acct->nr_running);
295}
296
297static void io_wqe_dec_running(struct io_wqe *wqe, struct io_worker *worker)
298 __must_hold(wqe->lock)
299{
300 struct io_wqe_acct *acct = io_wqe_get_acct(wqe, worker);
301
302 if (atomic_dec_and_test(&acct->nr_running) && io_wqe_run_queue(wqe))
303 io_wqe_wake_worker(wqe, acct);
304}
305
Jens Axboe771b53d02019-10-22 10:25:58 -0600306static void io_worker_start(struct io_wqe *wqe, struct io_worker *worker)
307{
308 allow_kernel_signal(SIGINT);
309
310 current->flags |= PF_IO_WORKER;
311
312 worker->flags |= (IO_WORKER_F_UP | IO_WORKER_F_RUNNING);
Jens Axboefcb323c2019-10-24 12:39:47 -0600313 worker->restore_files = current->files;
Jens Axboec5def4a2019-11-07 11:41:16 -0700314 io_wqe_inc_running(wqe, worker);
Jens Axboe771b53d02019-10-22 10:25:58 -0600315}
316
317/*
318 * Worker will start processing some work. Move it to the busy list, if
319 * it's currently on the freelist
320 */
321static void __io_worker_busy(struct io_wqe *wqe, struct io_worker *worker,
322 struct io_wq_work *work)
323 __must_hold(wqe->lock)
324{
Jens Axboec5def4a2019-11-07 11:41:16 -0700325 bool worker_bound, work_bound;
326
Jens Axboe771b53d02019-10-22 10:25:58 -0600327 if (worker->flags & IO_WORKER_F_FREE) {
328 worker->flags &= ~IO_WORKER_F_FREE;
329 hlist_nulls_del_init_rcu(&worker->nulls_node);
Jens Axboe021d1cd2019-11-14 08:00:41 -0700330 hlist_nulls_add_head_rcu(&worker->nulls_node, &wqe->busy_list);
Jens Axboe771b53d02019-10-22 10:25:58 -0600331 }
Jens Axboec5def4a2019-11-07 11:41:16 -0700332
333 /*
334 * If worker is moving from bound to unbound (or vice versa), then
335 * ensure we update the running accounting.
336 */
Dan Carpenterb2e9c7d62019-11-19 09:22:16 +0300337 worker_bound = (worker->flags & IO_WORKER_F_BOUND) != 0;
338 work_bound = (work->flags & IO_WQ_WORK_UNBOUND) == 0;
339 if (worker_bound != work_bound) {
Jens Axboec5def4a2019-11-07 11:41:16 -0700340 io_wqe_dec_running(wqe, worker);
341 if (work_bound) {
342 worker->flags |= IO_WORKER_F_BOUND;
343 wqe->acct[IO_WQ_ACCT_UNBOUND].nr_workers--;
344 wqe->acct[IO_WQ_ACCT_BOUND].nr_workers++;
345 atomic_dec(&wqe->wq->user->processes);
346 } else {
347 worker->flags &= ~IO_WORKER_F_BOUND;
348 wqe->acct[IO_WQ_ACCT_UNBOUND].nr_workers++;
349 wqe->acct[IO_WQ_ACCT_BOUND].nr_workers--;
350 atomic_inc(&wqe->wq->user->processes);
351 }
352 io_wqe_inc_running(wqe, worker);
353 }
Jens Axboe771b53d02019-10-22 10:25:58 -0600354}
355
356/*
357 * No work, worker going to sleep. Move to freelist, and unuse mm if we
358 * have one attached. Dropping the mm may potentially sleep, so we drop
359 * the lock in that case and return success. Since the caller has to
360 * retry the loop in that case (we changed task state), we don't regrab
361 * the lock if we return success.
362 */
363static bool __io_worker_idle(struct io_wqe *wqe, struct io_worker *worker)
364 __must_hold(wqe->lock)
365{
366 if (!(worker->flags & IO_WORKER_F_FREE)) {
367 worker->flags |= IO_WORKER_F_FREE;
368 hlist_nulls_del_init_rcu(&worker->nulls_node);
Jens Axboe021d1cd2019-11-14 08:00:41 -0700369 hlist_nulls_add_head_rcu(&worker->nulls_node, &wqe->free_list);
Jens Axboe771b53d02019-10-22 10:25:58 -0600370 }
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{
Jens Axboe6206f0e2019-11-26 11:59:32 -0700378 struct io_wq_work_node *node, *prev;
Jens Axboe771b53d02019-10-22 10:25:58 -0600379 struct io_wq_work *work;
380
Jens Axboe6206f0e2019-11-26 11:59:32 -0700381 wq_list_for_each(node, prev, &wqe->work_list) {
382 work = container_of(node, struct io_wq_work, list);
383
Jens Axboe771b53d02019-10-22 10:25:58 -0600384 /* not hashed, can run anytime */
385 if (!(work->flags & IO_WQ_WORK_HASHED)) {
Jens Axboe6206f0e2019-11-26 11:59:32 -0700386 wq_node_del(&wqe->work_list, node, prev);
Jens Axboe771b53d02019-10-22 10:25:58 -0600387 return work;
388 }
389
390 /* hashed, can run if not already running */
391 *hash = work->flags >> IO_WQ_HASH_SHIFT;
392 if (!(wqe->hash_map & BIT_ULL(*hash))) {
393 wqe->hash_map |= BIT_ULL(*hash);
Jens Axboe6206f0e2019-11-26 11:59:32 -0700394 wq_node_del(&wqe->work_list, node, prev);
Jens Axboe771b53d02019-10-22 10:25:58 -0600395 return work;
396 }
397 }
398
399 return NULL;
400}
401
402static void io_worker_handle_work(struct io_worker *worker)
403 __releases(wqe->lock)
404{
Jens Axboe7d723062019-11-12 22:31:31 -0700405 struct io_wq_work *work, *old_work = NULL, *put_work = NULL;
Jens Axboe771b53d02019-10-22 10:25:58 -0600406 struct io_wqe *wqe = worker->wqe;
407 struct io_wq *wq = wqe->wq;
408
409 do {
410 unsigned hash = -1U;
411
412 /*
Jens Axboe771b53d02019-10-22 10:25:58 -0600413 * If we got some work, mark us as busy. If we didn't, but
414 * the list isn't empty, it means we stalled on hashed work.
415 * Mark us stalled so we don't keep looking for work when we
416 * can't make progress, any work completion or insertion will
417 * clear the stalled flag.
418 */
419 work = io_get_next_work(wqe, &hash);
420 if (work)
421 __io_worker_busy(wqe, worker, work);
Jens Axboe6206f0e2019-11-26 11:59:32 -0700422 else if (!wq_list_empty(&wqe->work_list))
Jens Axboe771b53d02019-10-22 10:25:58 -0600423 wqe->flags |= IO_WQE_FLAG_STALLED;
424
425 spin_unlock_irq(&wqe->lock);
Jens Axboe7d723062019-11-12 22:31:31 -0700426 if (put_work && wq->put_work)
427 wq->put_work(old_work);
Jens Axboe771b53d02019-10-22 10:25:58 -0600428 if (!work)
429 break;
430next:
Jens Axboe36c2f922019-11-13 09:43:34 -0700431 /* flush any pending signals before assigning new work */
432 if (signal_pending(current))
433 flush_signals(current);
434
435 spin_lock_irq(&worker->lock);
436 worker->cur_work = work;
437 spin_unlock_irq(&worker->lock);
438
Jens Axboeb76da702019-11-20 13:05:32 -0700439 if (work->flags & IO_WQ_WORK_CB)
440 work->func(&work);
441
Jens Axboefcb323c2019-10-24 12:39:47 -0600442 if ((work->flags & IO_WQ_WORK_NEEDS_FILES) &&
443 current->files != work->files) {
444 task_lock(current);
445 current->files = work->files;
446 task_unlock(current);
447 }
Jens Axboe771b53d02019-10-22 10:25:58 -0600448 if ((work->flags & IO_WQ_WORK_NEEDS_USER) && !worker->mm &&
449 wq->mm && mmget_not_zero(wq->mm)) {
450 use_mm(wq->mm);
451 set_fs(USER_DS);
452 worker->mm = wq->mm;
453 }
Jens Axboe181e4482019-11-25 08:52:30 -0700454 if (!worker->creds)
455 worker->creds = override_creds(wq->creds);
Jens Axboe771b53d02019-10-22 10:25:58 -0600456 if (test_bit(IO_WQ_BIT_CANCEL, &wq->state))
457 work->flags |= IO_WQ_WORK_CANCEL;
458 if (worker->mm)
459 work->flags |= IO_WQ_WORK_HAS_MM;
460
Jens Axboe7d723062019-11-12 22:31:31 -0700461 if (wq->get_work && !(work->flags & IO_WQ_WORK_INTERNAL)) {
462 put_work = work;
463 wq->get_work(work);
464 }
465
Jens Axboe771b53d02019-10-22 10:25:58 -0600466 old_work = work;
467 work->func(&work);
468
Jens Axboe36c2f922019-11-13 09:43:34 -0700469 spin_lock_irq(&worker->lock);
Jens Axboe771b53d02019-10-22 10:25:58 -0600470 worker->cur_work = NULL;
Jens Axboe36c2f922019-11-13 09:43:34 -0700471 spin_unlock_irq(&worker->lock);
472
473 spin_lock_irq(&wqe->lock);
474
Jens Axboe771b53d02019-10-22 10:25:58 -0600475 if (hash != -1U) {
476 wqe->hash_map &= ~BIT_ULL(hash);
477 wqe->flags &= ~IO_WQE_FLAG_STALLED;
478 }
479 if (work && work != old_work) {
480 spin_unlock_irq(&wqe->lock);
Jens Axboe7d723062019-11-12 22:31:31 -0700481
482 if (put_work && wq->put_work) {
483 wq->put_work(put_work);
484 put_work = NULL;
485 }
486
Jens Axboe771b53d02019-10-22 10:25:58 -0600487 /* dependent work not hashed */
488 hash = -1U;
489 goto next;
490 }
491 } while (1);
492}
493
Jens Axboe771b53d02019-10-22 10:25:58 -0600494static int io_wqe_worker(void *data)
495{
496 struct io_worker *worker = data;
497 struct io_wqe *wqe = worker->wqe;
498 struct io_wq *wq = wqe->wq;
Jens Axboe771b53d02019-10-22 10:25:58 -0600499
500 io_worker_start(wqe, worker);
501
502 while (!test_bit(IO_WQ_BIT_EXIT, &wq->state)) {
Jens Axboe506d95f2019-12-07 21:03:59 -0700503 set_current_state(TASK_INTERRUPTIBLE);
Jens Axboe771b53d02019-10-22 10:25:58 -0600504 spin_lock_irq(&wqe->lock);
505 if (io_wqe_run_queue(wqe)) {
506 __set_current_state(TASK_RUNNING);
507 io_worker_handle_work(worker);
508 continue;
509 }
510 /* drops the lock on success, retry */
511 if (__io_worker_idle(wqe, worker)) {
512 __release(&wqe->lock);
513 continue;
514 }
515 spin_unlock_irq(&wqe->lock);
516 if (signal_pending(current))
517 flush_signals(current);
518 if (schedule_timeout(WORKER_IDLE_TIMEOUT))
519 continue;
520 /* timed out, exit unless we're the fixed worker */
521 if (test_bit(IO_WQ_BIT_EXIT, &wq->state) ||
522 !(worker->flags & IO_WORKER_F_FIXED))
523 break;
524 }
525
Jens Axboe771b53d02019-10-22 10:25:58 -0600526 if (test_bit(IO_WQ_BIT_EXIT, &wq->state)) {
527 spin_lock_irq(&wqe->lock);
Jens Axboe6206f0e2019-11-26 11:59:32 -0700528 if (!wq_list_empty(&wqe->work_list))
Jens Axboe771b53d02019-10-22 10:25:58 -0600529 io_worker_handle_work(worker);
530 else
531 spin_unlock_irq(&wqe->lock);
532 }
533
534 io_worker_exit(worker);
535 return 0;
536}
537
538/*
Jens Axboe771b53d02019-10-22 10:25:58 -0600539 * Called when a worker is scheduled in. Mark us as currently running.
540 */
541void io_wq_worker_running(struct task_struct *tsk)
542{
543 struct io_worker *worker = kthread_data(tsk);
544 struct io_wqe *wqe = worker->wqe;
545
546 if (!(worker->flags & IO_WORKER_F_UP))
547 return;
548 if (worker->flags & IO_WORKER_F_RUNNING)
549 return;
550 worker->flags |= IO_WORKER_F_RUNNING;
Jens Axboec5def4a2019-11-07 11:41:16 -0700551 io_wqe_inc_running(wqe, worker);
Jens Axboe771b53d02019-10-22 10:25:58 -0600552}
553
554/*
555 * Called when worker is going to sleep. If there are no workers currently
556 * running and we have work pending, wake up a free one or have the manager
557 * set one up.
558 */
559void io_wq_worker_sleeping(struct task_struct *tsk)
560{
561 struct io_worker *worker = kthread_data(tsk);
562 struct io_wqe *wqe = worker->wqe;
563
564 if (!(worker->flags & IO_WORKER_F_UP))
565 return;
566 if (!(worker->flags & IO_WORKER_F_RUNNING))
567 return;
568
569 worker->flags &= ~IO_WORKER_F_RUNNING;
570
571 spin_lock_irq(&wqe->lock);
Jens Axboec5def4a2019-11-07 11:41:16 -0700572 io_wqe_dec_running(wqe, worker);
Jens Axboe771b53d02019-10-22 10:25:58 -0600573 spin_unlock_irq(&wqe->lock);
574}
575
Jens Axboeb60fda62019-11-19 08:37:07 -0700576static bool create_io_worker(struct io_wq *wq, struct io_wqe *wqe, int index)
Jens Axboe771b53d02019-10-22 10:25:58 -0600577{
Jens Axboec5def4a2019-11-07 11:41:16 -0700578 struct io_wqe_acct *acct =&wqe->acct[index];
Jens Axboe771b53d02019-10-22 10:25:58 -0600579 struct io_worker *worker;
580
Jann Hornad6e0052019-11-26 17:39:45 +0100581 worker = kzalloc_node(sizeof(*worker), GFP_KERNEL, wqe->node);
Jens Axboe771b53d02019-10-22 10:25:58 -0600582 if (!worker)
Jens Axboeb60fda62019-11-19 08:37:07 -0700583 return false;
Jens Axboe771b53d02019-10-22 10:25:58 -0600584
585 refcount_set(&worker->ref, 1);
586 worker->nulls_node.pprev = NULL;
Jens Axboe771b53d02019-10-22 10:25:58 -0600587 worker->wqe = wqe;
Jens Axboe36c2f922019-11-13 09:43:34 -0700588 spin_lock_init(&worker->lock);
Jens Axboe771b53d02019-10-22 10:25:58 -0600589
590 worker->task = kthread_create_on_node(io_wqe_worker, worker, wqe->node,
Jens Axboec5def4a2019-11-07 11:41:16 -0700591 "io_wqe_worker-%d/%d", index, wqe->node);
Jens Axboe771b53d02019-10-22 10:25:58 -0600592 if (IS_ERR(worker->task)) {
593 kfree(worker);
Jens Axboeb60fda62019-11-19 08:37:07 -0700594 return false;
Jens Axboe771b53d02019-10-22 10:25:58 -0600595 }
596
597 spin_lock_irq(&wqe->lock);
Jens Axboe021d1cd2019-11-14 08:00:41 -0700598 hlist_nulls_add_head_rcu(&worker->nulls_node, &wqe->free_list);
Jens Axboee61df662019-11-13 13:54:49 -0700599 list_add_tail_rcu(&worker->all_list, &wqe->all_list);
Jens Axboe771b53d02019-10-22 10:25:58 -0600600 worker->flags |= IO_WORKER_F_FREE;
Jens Axboec5def4a2019-11-07 11:41:16 -0700601 if (index == IO_WQ_ACCT_BOUND)
602 worker->flags |= IO_WORKER_F_BOUND;
603 if (!acct->nr_workers && (worker->flags & IO_WORKER_F_BOUND))
Jens Axboe771b53d02019-10-22 10:25:58 -0600604 worker->flags |= IO_WORKER_F_FIXED;
Jens Axboec5def4a2019-11-07 11:41:16 -0700605 acct->nr_workers++;
Jens Axboe771b53d02019-10-22 10:25:58 -0600606 spin_unlock_irq(&wqe->lock);
607
Jens Axboec5def4a2019-11-07 11:41:16 -0700608 if (index == IO_WQ_ACCT_UNBOUND)
609 atomic_inc(&wq->user->processes);
610
Jens Axboe771b53d02019-10-22 10:25:58 -0600611 wake_up_process(worker->task);
Jens Axboeb60fda62019-11-19 08:37:07 -0700612 return true;
Jens Axboe771b53d02019-10-22 10:25:58 -0600613}
614
Jens Axboec5def4a2019-11-07 11:41:16 -0700615static inline bool io_wqe_need_worker(struct io_wqe *wqe, int index)
Jens Axboe771b53d02019-10-22 10:25:58 -0600616 __must_hold(wqe->lock)
617{
Jens Axboec5def4a2019-11-07 11:41:16 -0700618 struct io_wqe_acct *acct = &wqe->acct[index];
Jens Axboe771b53d02019-10-22 10:25:58 -0600619
Jens Axboec5def4a2019-11-07 11:41:16 -0700620 /* if we have available workers or no work, no need */
Jens Axboe021d1cd2019-11-14 08:00:41 -0700621 if (!hlist_nulls_empty(&wqe->free_list) || !io_wqe_run_queue(wqe))
Jens Axboec5def4a2019-11-07 11:41:16 -0700622 return false;
623 return acct->nr_workers < acct->max_workers;
Jens Axboe771b53d02019-10-22 10:25:58 -0600624}
625
626/*
627 * Manager thread. Tasked with creating new workers, if we need them.
628 */
629static int io_wq_manager(void *data)
630{
631 struct io_wq *wq = data;
Jann Horn3fc50ab2019-11-26 19:10:20 +0100632 int workers_to_create = num_possible_nodes();
633 int node;
Jens Axboeb60fda62019-11-19 08:37:07 -0700634
635 /* create fixed workers */
Jann Horn3fc50ab2019-11-26 19:10:20 +0100636 refcount_set(&wq->refs, workers_to_create);
637 for_each_node(node) {
638 if (!create_io_worker(wq, wq->wqes[node], IO_WQ_ACCT_BOUND))
639 goto err;
640 workers_to_create--;
Jens Axboeb60fda62019-11-19 08:37:07 -0700641 }
642
643 complete(&wq->done);
Jens Axboe771b53d02019-10-22 10:25:58 -0600644
645 while (!kthread_should_stop()) {
Jann Horn3fc50ab2019-11-26 19:10:20 +0100646 for_each_node(node) {
647 struct io_wqe *wqe = wq->wqes[node];
Jens Axboec5def4a2019-11-07 11:41:16 -0700648 bool fork_worker[2] = { false, false };
Jens Axboe771b53d02019-10-22 10:25:58 -0600649
650 spin_lock_irq(&wqe->lock);
Jens Axboec5def4a2019-11-07 11:41:16 -0700651 if (io_wqe_need_worker(wqe, IO_WQ_ACCT_BOUND))
652 fork_worker[IO_WQ_ACCT_BOUND] = true;
653 if (io_wqe_need_worker(wqe, IO_WQ_ACCT_UNBOUND))
654 fork_worker[IO_WQ_ACCT_UNBOUND] = true;
Jens Axboe771b53d02019-10-22 10:25:58 -0600655 spin_unlock_irq(&wqe->lock);
Jens Axboec5def4a2019-11-07 11:41:16 -0700656 if (fork_worker[IO_WQ_ACCT_BOUND])
657 create_io_worker(wq, wqe, IO_WQ_ACCT_BOUND);
658 if (fork_worker[IO_WQ_ACCT_UNBOUND])
659 create_io_worker(wq, wqe, IO_WQ_ACCT_UNBOUND);
Jens Axboe771b53d02019-10-22 10:25:58 -0600660 }
661 set_current_state(TASK_INTERRUPTIBLE);
662 schedule_timeout(HZ);
663 }
664
665 return 0;
Jens Axboeb60fda62019-11-19 08:37:07 -0700666err:
667 set_bit(IO_WQ_BIT_ERROR, &wq->state);
668 set_bit(IO_WQ_BIT_EXIT, &wq->state);
Jann Horn3fc50ab2019-11-26 19:10:20 +0100669 if (refcount_sub_and_test(workers_to_create, &wq->refs))
Jens Axboeb60fda62019-11-19 08:37:07 -0700670 complete(&wq->done);
671 return 0;
Jens Axboe771b53d02019-10-22 10:25:58 -0600672}
673
Jens Axboec5def4a2019-11-07 11:41:16 -0700674static bool io_wq_can_queue(struct io_wqe *wqe, struct io_wqe_acct *acct,
675 struct io_wq_work *work)
676{
677 bool free_worker;
678
679 if (!(work->flags & IO_WQ_WORK_UNBOUND))
680 return true;
681 if (atomic_read(&acct->nr_running))
682 return true;
683
684 rcu_read_lock();
Jens Axboe021d1cd2019-11-14 08:00:41 -0700685 free_worker = !hlist_nulls_empty(&wqe->free_list);
Jens Axboec5def4a2019-11-07 11:41:16 -0700686 rcu_read_unlock();
687 if (free_worker)
688 return true;
689
690 if (atomic_read(&wqe->wq->user->processes) >= acct->max_workers &&
691 !(capable(CAP_SYS_RESOURCE) || capable(CAP_SYS_ADMIN)))
692 return false;
693
694 return true;
695}
696
Jens Axboe771b53d02019-10-22 10:25:58 -0600697static void io_wqe_enqueue(struct io_wqe *wqe, struct io_wq_work *work)
698{
Jens Axboec5def4a2019-11-07 11:41:16 -0700699 struct io_wqe_acct *acct = io_work_get_acct(wqe, work);
Jens Axboe771b53d02019-10-22 10:25:58 -0600700 unsigned long flags;
701
Jens Axboec5def4a2019-11-07 11:41:16 -0700702 /*
703 * Do early check to see if we need a new unbound worker, and if we do,
704 * if we're allowed to do so. This isn't 100% accurate as there's a
705 * gap between this check and incrementing the value, but that's OK.
706 * It's close enough to not be an issue, fork() has the same delay.
707 */
708 if (unlikely(!io_wq_can_queue(wqe, acct, work))) {
709 work->flags |= IO_WQ_WORK_CANCEL;
710 work->func(&work);
711 return;
712 }
713
Jens Axboe771b53d02019-10-22 10:25:58 -0600714 spin_lock_irqsave(&wqe->lock, flags);
Jens Axboe6206f0e2019-11-26 11:59:32 -0700715 wq_list_add_tail(&work->list, &wqe->work_list);
Jens Axboe771b53d02019-10-22 10:25:58 -0600716 wqe->flags &= ~IO_WQE_FLAG_STALLED;
717 spin_unlock_irqrestore(&wqe->lock, flags);
718
Jens Axboec5def4a2019-11-07 11:41:16 -0700719 if (!atomic_read(&acct->nr_running))
720 io_wqe_wake_worker(wqe, acct);
Jens Axboe771b53d02019-10-22 10:25:58 -0600721}
722
723void io_wq_enqueue(struct io_wq *wq, struct io_wq_work *work)
724{
725 struct io_wqe *wqe = wq->wqes[numa_node_id()];
726
727 io_wqe_enqueue(wqe, work);
728}
729
730/*
731 * Enqueue work, hashed by some key. Work items that hash to the same value
732 * will not be done in parallel. Used to limit concurrent writes, generally
733 * hashed by inode.
734 */
735void io_wq_enqueue_hashed(struct io_wq *wq, struct io_wq_work *work, void *val)
736{
737 struct io_wqe *wqe = wq->wqes[numa_node_id()];
738 unsigned bit;
739
740
741 bit = hash_ptr(val, IO_WQ_HASH_ORDER);
742 work->flags |= (IO_WQ_WORK_HASHED | (bit << IO_WQ_HASH_SHIFT));
743 io_wqe_enqueue(wqe, work);
744}
745
746static bool io_wqe_worker_send_sig(struct io_worker *worker, void *data)
747{
748 send_sig(SIGINT, worker->task, 1);
749 return false;
750}
751
752/*
753 * Iterate the passed in list and call the specific function for each
754 * worker that isn't exiting
755 */
756static bool io_wq_for_each_worker(struct io_wqe *wqe,
Jens Axboe771b53d02019-10-22 10:25:58 -0600757 bool (*func)(struct io_worker *, void *),
758 void *data)
759{
Jens Axboe771b53d02019-10-22 10:25:58 -0600760 struct io_worker *worker;
761 bool ret = false;
762
Jens Axboee61df662019-11-13 13:54:49 -0700763 list_for_each_entry_rcu(worker, &wqe->all_list, all_list) {
Jens Axboe771b53d02019-10-22 10:25:58 -0600764 if (io_worker_get(worker)) {
765 ret = func(worker, data);
766 io_worker_release(worker);
767 if (ret)
768 break;
769 }
770 }
Jens Axboee61df662019-11-13 13:54:49 -0700771
Jens Axboe771b53d02019-10-22 10:25:58 -0600772 return ret;
773}
774
775void io_wq_cancel_all(struct io_wq *wq)
776{
Jann Horn3fc50ab2019-11-26 19:10:20 +0100777 int node;
Jens Axboe771b53d02019-10-22 10:25:58 -0600778
779 set_bit(IO_WQ_BIT_CANCEL, &wq->state);
780
781 /*
782 * Browse both lists, as there's a gap between handing work off
783 * to a worker and the worker putting itself on the busy_list
784 */
785 rcu_read_lock();
Jann Horn3fc50ab2019-11-26 19:10:20 +0100786 for_each_node(node) {
787 struct io_wqe *wqe = wq->wqes[node];
Jens Axboe771b53d02019-10-22 10:25:58 -0600788
Jens Axboee61df662019-11-13 13:54:49 -0700789 io_wq_for_each_worker(wqe, io_wqe_worker_send_sig, NULL);
Jens Axboe771b53d02019-10-22 10:25:58 -0600790 }
791 rcu_read_unlock();
792}
793
Jens Axboe62755e32019-10-28 21:49:21 -0600794struct io_cb_cancel_data {
795 struct io_wqe *wqe;
796 work_cancel_fn *cancel;
797 void *caller_data;
798};
799
800static bool io_work_cancel(struct io_worker *worker, void *cancel_data)
801{
802 struct io_cb_cancel_data *data = cancel_data;
Jens Axboe6f726532019-11-05 13:51:51 -0700803 unsigned long flags;
Jens Axboe62755e32019-10-28 21:49:21 -0600804 bool ret = false;
805
806 /*
807 * Hold the lock to avoid ->cur_work going out of scope, caller
Jens Axboe36c2f922019-11-13 09:43:34 -0700808 * may dereference the passed in work.
Jens Axboe62755e32019-10-28 21:49:21 -0600809 */
Jens Axboe36c2f922019-11-13 09:43:34 -0700810 spin_lock_irqsave(&worker->lock, flags);
Jens Axboe62755e32019-10-28 21:49:21 -0600811 if (worker->cur_work &&
812 data->cancel(worker->cur_work, data->caller_data)) {
813 send_sig(SIGINT, worker->task, 1);
814 ret = true;
815 }
Jens Axboe36c2f922019-11-13 09:43:34 -0700816 spin_unlock_irqrestore(&worker->lock, flags);
Jens Axboe62755e32019-10-28 21:49:21 -0600817
818 return ret;
819}
820
821static enum io_wq_cancel io_wqe_cancel_cb_work(struct io_wqe *wqe,
822 work_cancel_fn *cancel,
823 void *cancel_data)
824{
825 struct io_cb_cancel_data data = {
826 .wqe = wqe,
827 .cancel = cancel,
828 .caller_data = cancel_data,
829 };
Jens Axboe6206f0e2019-11-26 11:59:32 -0700830 struct io_wq_work_node *node, *prev;
Jens Axboe62755e32019-10-28 21:49:21 -0600831 struct io_wq_work *work;
Jens Axboe6f726532019-11-05 13:51:51 -0700832 unsigned long flags;
Jens Axboe62755e32019-10-28 21:49:21 -0600833 bool found = false;
834
Jens Axboe6f726532019-11-05 13:51:51 -0700835 spin_lock_irqsave(&wqe->lock, flags);
Jens Axboe6206f0e2019-11-26 11:59:32 -0700836 wq_list_for_each(node, prev, &wqe->work_list) {
837 work = container_of(node, struct io_wq_work, list);
838
Jens Axboe62755e32019-10-28 21:49:21 -0600839 if (cancel(work, cancel_data)) {
Jens Axboe6206f0e2019-11-26 11:59:32 -0700840 wq_node_del(&wqe->work_list, node, prev);
Jens Axboe62755e32019-10-28 21:49:21 -0600841 found = true;
842 break;
843 }
844 }
Jens Axboe6f726532019-11-05 13:51:51 -0700845 spin_unlock_irqrestore(&wqe->lock, flags);
Jens Axboe62755e32019-10-28 21:49:21 -0600846
847 if (found) {
848 work->flags |= IO_WQ_WORK_CANCEL;
849 work->func(&work);
850 return IO_WQ_CANCEL_OK;
851 }
852
853 rcu_read_lock();
Jens Axboee61df662019-11-13 13:54:49 -0700854 found = io_wq_for_each_worker(wqe, io_work_cancel, &data);
Jens Axboe62755e32019-10-28 21:49:21 -0600855 rcu_read_unlock();
856 return found ? IO_WQ_CANCEL_RUNNING : IO_WQ_CANCEL_NOTFOUND;
857}
858
859enum io_wq_cancel io_wq_cancel_cb(struct io_wq *wq, work_cancel_fn *cancel,
860 void *data)
861{
862 enum io_wq_cancel ret = IO_WQ_CANCEL_NOTFOUND;
Jann Horn3fc50ab2019-11-26 19:10:20 +0100863 int node;
Jens Axboe62755e32019-10-28 21:49:21 -0600864
Jann Horn3fc50ab2019-11-26 19:10:20 +0100865 for_each_node(node) {
866 struct io_wqe *wqe = wq->wqes[node];
Jens Axboe62755e32019-10-28 21:49:21 -0600867
868 ret = io_wqe_cancel_cb_work(wqe, cancel, data);
869 if (ret != IO_WQ_CANCEL_NOTFOUND)
870 break;
871 }
872
873 return ret;
874}
875
Jens Axboe771b53d02019-10-22 10:25:58 -0600876static bool io_wq_worker_cancel(struct io_worker *worker, void *data)
877{
878 struct io_wq_work *work = data;
Jens Axboe36c2f922019-11-13 09:43:34 -0700879 unsigned long flags;
880 bool ret = false;
Jens Axboe771b53d02019-10-22 10:25:58 -0600881
Jens Axboe36c2f922019-11-13 09:43:34 -0700882 if (worker->cur_work != work)
883 return false;
884
885 spin_lock_irqsave(&worker->lock, flags);
Jens Axboe771b53d02019-10-22 10:25:58 -0600886 if (worker->cur_work == work) {
887 send_sig(SIGINT, worker->task, 1);
Jens Axboe36c2f922019-11-13 09:43:34 -0700888 ret = true;
Jens Axboe771b53d02019-10-22 10:25:58 -0600889 }
Jens Axboe36c2f922019-11-13 09:43:34 -0700890 spin_unlock_irqrestore(&worker->lock, flags);
Jens Axboe771b53d02019-10-22 10:25:58 -0600891
Jens Axboe36c2f922019-11-13 09:43:34 -0700892 return ret;
Jens Axboe771b53d02019-10-22 10:25:58 -0600893}
894
895static enum io_wq_cancel io_wqe_cancel_work(struct io_wqe *wqe,
896 struct io_wq_work *cwork)
897{
Jens Axboe6206f0e2019-11-26 11:59:32 -0700898 struct io_wq_work_node *node, *prev;
Jens Axboe771b53d02019-10-22 10:25:58 -0600899 struct io_wq_work *work;
Jens Axboe6f726532019-11-05 13:51:51 -0700900 unsigned long flags;
Jens Axboe771b53d02019-10-22 10:25:58 -0600901 bool found = false;
902
903 cwork->flags |= IO_WQ_WORK_CANCEL;
904
905 /*
906 * First check pending list, if we're lucky we can just remove it
907 * from there. CANCEL_OK means that the work is returned as-new,
908 * no completion will be posted for it.
909 */
Jens Axboe6f726532019-11-05 13:51:51 -0700910 spin_lock_irqsave(&wqe->lock, flags);
Jens Axboe6206f0e2019-11-26 11:59:32 -0700911 wq_list_for_each(node, prev, &wqe->work_list) {
912 work = container_of(node, struct io_wq_work, list);
913
Jens Axboe771b53d02019-10-22 10:25:58 -0600914 if (work == cwork) {
Jens Axboe6206f0e2019-11-26 11:59:32 -0700915 wq_node_del(&wqe->work_list, node, prev);
Jens Axboe771b53d02019-10-22 10:25:58 -0600916 found = true;
917 break;
918 }
919 }
Jens Axboe6f726532019-11-05 13:51:51 -0700920 spin_unlock_irqrestore(&wqe->lock, flags);
Jens Axboe771b53d02019-10-22 10:25:58 -0600921
922 if (found) {
923 work->flags |= IO_WQ_WORK_CANCEL;
924 work->func(&work);
925 return IO_WQ_CANCEL_OK;
926 }
927
928 /*
929 * Now check if a free (going busy) or busy worker has the work
930 * currently running. If we find it there, we'll return CANCEL_RUNNING
931 * as an indication that we attempte to signal cancellation. The
932 * completion will run normally in this case.
933 */
934 rcu_read_lock();
Jens Axboee61df662019-11-13 13:54:49 -0700935 found = io_wq_for_each_worker(wqe, io_wq_worker_cancel, cwork);
Jens Axboe771b53d02019-10-22 10:25:58 -0600936 rcu_read_unlock();
937 return found ? IO_WQ_CANCEL_RUNNING : IO_WQ_CANCEL_NOTFOUND;
938}
939
940enum io_wq_cancel io_wq_cancel_work(struct io_wq *wq, struct io_wq_work *cwork)
941{
942 enum io_wq_cancel ret = IO_WQ_CANCEL_NOTFOUND;
Jann Horn3fc50ab2019-11-26 19:10:20 +0100943 int node;
Jens Axboe771b53d02019-10-22 10:25:58 -0600944
Jann Horn3fc50ab2019-11-26 19:10:20 +0100945 for_each_node(node) {
946 struct io_wqe *wqe = wq->wqes[node];
Jens Axboe771b53d02019-10-22 10:25:58 -0600947
948 ret = io_wqe_cancel_work(wqe, cwork);
949 if (ret != IO_WQ_CANCEL_NOTFOUND)
950 break;
951 }
952
953 return ret;
954}
955
956struct io_wq_flush_data {
957 struct io_wq_work work;
958 struct completion done;
959};
960
961static void io_wq_flush_func(struct io_wq_work **workptr)
962{
963 struct io_wq_work *work = *workptr;
964 struct io_wq_flush_data *data;
965
966 data = container_of(work, struct io_wq_flush_data, work);
967 complete(&data->done);
968}
969
970/*
971 * Doesn't wait for previously queued work to finish. When this completes,
972 * it just means that previously queued work was started.
973 */
974void io_wq_flush(struct io_wq *wq)
975{
976 struct io_wq_flush_data data;
Jann Horn3fc50ab2019-11-26 19:10:20 +0100977 int node;
Jens Axboe771b53d02019-10-22 10:25:58 -0600978
Jann Horn3fc50ab2019-11-26 19:10:20 +0100979 for_each_node(node) {
980 struct io_wqe *wqe = wq->wqes[node];
Jens Axboe771b53d02019-10-22 10:25:58 -0600981
982 init_completion(&data.done);
983 INIT_IO_WORK(&data.work, io_wq_flush_func);
Jens Axboe7d723062019-11-12 22:31:31 -0700984 data.work.flags |= IO_WQ_WORK_INTERNAL;
Jens Axboe771b53d02019-10-22 10:25:58 -0600985 io_wqe_enqueue(wqe, &data.work);
986 wait_for_completion(&data.done);
987 }
988}
989
Jens Axboe576a3472019-11-25 08:49:20 -0700990struct io_wq *io_wq_create(unsigned bounded, struct io_wq_data *data)
Jens Axboe771b53d02019-10-22 10:25:58 -0600991{
Jann Horn3fc50ab2019-11-26 19:10:20 +0100992 int ret = -ENOMEM, node;
Jens Axboe771b53d02019-10-22 10:25:58 -0600993 struct io_wq *wq;
994
Jann Hornad6e0052019-11-26 17:39:45 +0100995 wq = kzalloc(sizeof(*wq), GFP_KERNEL);
Jens Axboe771b53d02019-10-22 10:25:58 -0600996 if (!wq)
997 return ERR_PTR(-ENOMEM);
998
Jann Horn3fc50ab2019-11-26 19:10:20 +0100999 wq->wqes = kcalloc(nr_node_ids, sizeof(struct io_wqe *), GFP_KERNEL);
Jens Axboe771b53d02019-10-22 10:25:58 -06001000 if (!wq->wqes) {
1001 kfree(wq);
1002 return ERR_PTR(-ENOMEM);
1003 }
1004
Jens Axboe576a3472019-11-25 08:49:20 -07001005 wq->get_work = data->get_work;
1006 wq->put_work = data->put_work;
Jens Axboe7d723062019-11-12 22:31:31 -07001007
Jens Axboec5def4a2019-11-07 11:41:16 -07001008 /* caller must already hold a reference to this */
Jens Axboe576a3472019-11-25 08:49:20 -07001009 wq->user = data->user;
Jens Axboe181e4482019-11-25 08:52:30 -07001010 wq->creds = data->creds;
Jens Axboec5def4a2019-11-07 11:41:16 -07001011
Jann Horn3fc50ab2019-11-26 19:10:20 +01001012 for_each_node(node) {
Jens Axboe771b53d02019-10-22 10:25:58 -06001013 struct io_wqe *wqe;
1014
Jann Hornad6e0052019-11-26 17:39:45 +01001015 wqe = kzalloc_node(sizeof(struct io_wqe), GFP_KERNEL, node);
Jens Axboe771b53d02019-10-22 10:25:58 -06001016 if (!wqe)
Jann Horn3fc50ab2019-11-26 19:10:20 +01001017 goto err;
1018 wq->wqes[node] = wqe;
Jens Axboe771b53d02019-10-22 10:25:58 -06001019 wqe->node = node;
Jens Axboec5def4a2019-11-07 11:41:16 -07001020 wqe->acct[IO_WQ_ACCT_BOUND].max_workers = bounded;
1021 atomic_set(&wqe->acct[IO_WQ_ACCT_BOUND].nr_running, 0);
Jens Axboe576a3472019-11-25 08:49:20 -07001022 if (wq->user) {
Jens Axboec5def4a2019-11-07 11:41:16 -07001023 wqe->acct[IO_WQ_ACCT_UNBOUND].max_workers =
1024 task_rlimit(current, RLIMIT_NPROC);
1025 }
1026 atomic_set(&wqe->acct[IO_WQ_ACCT_UNBOUND].nr_running, 0);
Jens Axboe771b53d02019-10-22 10:25:58 -06001027 wqe->node = node;
1028 wqe->wq = wq;
1029 spin_lock_init(&wqe->lock);
Jens Axboe6206f0e2019-11-26 11:59:32 -07001030 INIT_WQ_LIST(&wqe->work_list);
Jens Axboe021d1cd2019-11-14 08:00:41 -07001031 INIT_HLIST_NULLS_HEAD(&wqe->free_list, 0);
1032 INIT_HLIST_NULLS_HEAD(&wqe->busy_list, 1);
Jens Axboee61df662019-11-13 13:54:49 -07001033 INIT_LIST_HEAD(&wqe->all_list);
Jens Axboe771b53d02019-10-22 10:25:58 -06001034 }
1035
1036 init_completion(&wq->done);
1037
Jens Axboe771b53d02019-10-22 10:25:58 -06001038 /* caller must have already done mmgrab() on this mm */
Jens Axboe576a3472019-11-25 08:49:20 -07001039 wq->mm = data->mm;
Jens Axboe771b53d02019-10-22 10:25:58 -06001040
1041 wq->manager = kthread_create(io_wq_manager, wq, "io_wq_manager");
1042 if (!IS_ERR(wq->manager)) {
1043 wake_up_process(wq->manager);
Jens Axboeb60fda62019-11-19 08:37:07 -07001044 wait_for_completion(&wq->done);
1045 if (test_bit(IO_WQ_BIT_ERROR, &wq->state)) {
1046 ret = -ENOMEM;
1047 goto err;
1048 }
1049 reinit_completion(&wq->done);
Jens Axboe771b53d02019-10-22 10:25:58 -06001050 return wq;
1051 }
1052
1053 ret = PTR_ERR(wq->manager);
Jens Axboe771b53d02019-10-22 10:25:58 -06001054 complete(&wq->done);
Jens Axboeb60fda62019-11-19 08:37:07 -07001055err:
Jann Horn3fc50ab2019-11-26 19:10:20 +01001056 for_each_node(node)
1057 kfree(wq->wqes[node]);
Jens Axboeb60fda62019-11-19 08:37:07 -07001058 kfree(wq->wqes);
1059 kfree(wq);
Jens Axboe771b53d02019-10-22 10:25:58 -06001060 return ERR_PTR(ret);
1061}
1062
1063static bool io_wq_worker_wake(struct io_worker *worker, void *data)
1064{
1065 wake_up_process(worker->task);
1066 return false;
1067}
1068
1069void io_wq_destroy(struct io_wq *wq)
1070{
Jann Horn3fc50ab2019-11-26 19:10:20 +01001071 int node;
Jens Axboe771b53d02019-10-22 10:25:58 -06001072
Jens Axboeb60fda62019-11-19 08:37:07 -07001073 set_bit(IO_WQ_BIT_EXIT, &wq->state);
1074 if (wq->manager)
Jens Axboe771b53d02019-10-22 10:25:58 -06001075 kthread_stop(wq->manager);
Jens Axboe771b53d02019-10-22 10:25:58 -06001076
1077 rcu_read_lock();
Jann Horn3fc50ab2019-11-26 19:10:20 +01001078 for_each_node(node)
1079 io_wq_for_each_worker(wq->wqes[node], io_wq_worker_wake, NULL);
Jens Axboe771b53d02019-10-22 10:25:58 -06001080 rcu_read_unlock();
1081
1082 wait_for_completion(&wq->done);
1083
Jann Horn3fc50ab2019-11-26 19:10:20 +01001084 for_each_node(node)
1085 kfree(wq->wqes[node]);
Jens Axboe771b53d02019-10-22 10:25:58 -06001086 kfree(wq->wqes);
1087 kfree(wq);
1088}