blob: e26ceef53cbdacf1de6587d107930716a301d681 [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>
Jens Axboe9392a272020-02-06 21:42:51 -070019#include <linux/fs_struct.h>
Jens Axboe771b53d02019-10-22 10:25:58 -060020
21#include "io-wq.h"
22
23#define WORKER_IDLE_TIMEOUT (5 * HZ)
24
25enum {
26 IO_WORKER_F_UP = 1, /* up and active */
27 IO_WORKER_F_RUNNING = 2, /* account as running */
28 IO_WORKER_F_FREE = 4, /* worker on free list */
29 IO_WORKER_F_EXITING = 8, /* worker exiting */
30 IO_WORKER_F_FIXED = 16, /* static idle worker */
Jens Axboec5def4a2019-11-07 11:41:16 -070031 IO_WORKER_F_BOUND = 32, /* is doing bounded work */
Jens Axboe771b53d02019-10-22 10:25:58 -060032};
33
34enum {
35 IO_WQ_BIT_EXIT = 0, /* wq exiting */
36 IO_WQ_BIT_CANCEL = 1, /* cancel work on list */
Jens Axboeb60fda62019-11-19 08:37:07 -070037 IO_WQ_BIT_ERROR = 2, /* error on setup */
Jens Axboe771b53d02019-10-22 10:25:58 -060038};
39
40enum {
41 IO_WQE_FLAG_STALLED = 1, /* stalled on hash */
42};
43
44/*
45 * One for each thread in a wqe pool
46 */
47struct io_worker {
48 refcount_t ref;
49 unsigned flags;
50 struct hlist_nulls_node nulls_node;
Jens Axboee61df662019-11-13 13:54:49 -070051 struct list_head all_list;
Jens Axboe771b53d02019-10-22 10:25:58 -060052 struct task_struct *task;
Jens Axboe771b53d02019-10-22 10:25:58 -060053 struct io_wqe *wqe;
Jens Axboe36c2f922019-11-13 09:43:34 -070054
Jens Axboe771b53d02019-10-22 10:25:58 -060055 struct io_wq_work *cur_work;
Jens Axboe36c2f922019-11-13 09:43:34 -070056 spinlock_t lock;
Jens Axboe771b53d02019-10-22 10:25:58 -060057
58 struct rcu_head rcu;
59 struct mm_struct *mm;
Jens Axboecccf0ee2020-01-27 16:34:48 -070060 const struct cred *cur_creds;
61 const struct cred *saved_creds;
Jens Axboefcb323c2019-10-24 12:39:47 -060062 struct files_struct *restore_files;
Jens Axboe9392a272020-02-06 21:42:51 -070063 struct fs_struct *restore_fs;
Jens Axboe771b53d02019-10-22 10:25:58 -060064};
65
Jens Axboe771b53d02019-10-22 10:25:58 -060066#if BITS_PER_LONG == 64
67#define IO_WQ_HASH_ORDER 6
68#else
69#define IO_WQ_HASH_ORDER 5
70#endif
71
Jens Axboec5def4a2019-11-07 11:41:16 -070072struct io_wqe_acct {
73 unsigned nr_workers;
74 unsigned max_workers;
75 atomic_t nr_running;
76};
77
78enum {
79 IO_WQ_ACCT_BOUND,
80 IO_WQ_ACCT_UNBOUND,
81};
82
Jens Axboe771b53d02019-10-22 10:25:58 -060083/*
84 * Per-node worker thread pool
85 */
86struct io_wqe {
87 struct {
88 spinlock_t lock;
Jens Axboe6206f0e2019-11-26 11:59:32 -070089 struct io_wq_work_list work_list;
Jens Axboe771b53d02019-10-22 10:25:58 -060090 unsigned long hash_map;
91 unsigned flags;
92 } ____cacheline_aligned_in_smp;
93
94 int node;
Jens Axboec5def4a2019-11-07 11:41:16 -070095 struct io_wqe_acct acct[2];
Jens Axboe771b53d02019-10-22 10:25:58 -060096
Jens Axboe021d1cd2019-11-14 08:00:41 -070097 struct hlist_nulls_head free_list;
Jens Axboee61df662019-11-13 13:54:49 -070098 struct list_head all_list;
Jens Axboe771b53d02019-10-22 10:25:58 -060099
100 struct io_wq *wq;
101};
102
103/*
104 * Per io_wq state
105 */
106struct io_wq {
107 struct io_wqe **wqes;
108 unsigned long state;
Jens Axboe771b53d02019-10-22 10:25:58 -0600109
Pavel Begunkove9fd9392020-03-04 16:14:12 +0300110 free_work_fn *free_work;
Jens Axboe7d723062019-11-12 22:31:31 -0700111
Jens Axboe771b53d02019-10-22 10:25:58 -0600112 struct task_struct *manager;
Jens Axboec5def4a2019-11-07 11:41:16 -0700113 struct user_struct *user;
Jens Axboe771b53d02019-10-22 10:25:58 -0600114 refcount_t refs;
115 struct completion done;
Jens Axboe848f7e12020-01-23 15:33:32 -0700116
117 refcount_t use_refs;
Jens Axboe771b53d02019-10-22 10:25:58 -0600118};
119
Jens Axboe771b53d02019-10-22 10:25:58 -0600120static bool io_worker_get(struct io_worker *worker)
121{
122 return refcount_inc_not_zero(&worker->ref);
123}
124
125static void io_worker_release(struct io_worker *worker)
126{
127 if (refcount_dec_and_test(&worker->ref))
128 wake_up_process(worker->task);
129}
130
131/*
132 * Note: drops the wqe->lock if returning true! The caller must re-acquire
133 * the lock in that case. Some callers need to restart handling if this
134 * happens, so we can't just re-acquire the lock on behalf of the caller.
135 */
136static bool __io_worker_unuse(struct io_wqe *wqe, struct io_worker *worker)
137{
Jens Axboefcb323c2019-10-24 12:39:47 -0600138 bool dropped_lock = false;
139
Jens Axboecccf0ee2020-01-27 16:34:48 -0700140 if (worker->saved_creds) {
141 revert_creds(worker->saved_creds);
142 worker->cur_creds = worker->saved_creds = NULL;
Jens Axboe181e4482019-11-25 08:52:30 -0700143 }
144
Jens Axboefcb323c2019-10-24 12:39:47 -0600145 if (current->files != worker->restore_files) {
146 __acquire(&wqe->lock);
147 spin_unlock_irq(&wqe->lock);
148 dropped_lock = true;
149
150 task_lock(current);
151 current->files = worker->restore_files;
152 task_unlock(current);
153 }
154
Jens Axboe9392a272020-02-06 21:42:51 -0700155 if (current->fs != worker->restore_fs)
156 current->fs = worker->restore_fs;
157
Jens Axboe771b53d02019-10-22 10:25:58 -0600158 /*
159 * If we have an active mm, we need to drop the wq lock before unusing
160 * it. If we do, return true and let the caller retry the idle loop.
161 */
162 if (worker->mm) {
Jens Axboefcb323c2019-10-24 12:39:47 -0600163 if (!dropped_lock) {
164 __acquire(&wqe->lock);
165 spin_unlock_irq(&wqe->lock);
166 dropped_lock = true;
167 }
Jens Axboe771b53d02019-10-22 10:25:58 -0600168 __set_current_state(TASK_RUNNING);
169 set_fs(KERNEL_DS);
170 unuse_mm(worker->mm);
171 mmput(worker->mm);
172 worker->mm = NULL;
Jens Axboe771b53d02019-10-22 10:25:58 -0600173 }
174
Jens Axboefcb323c2019-10-24 12:39:47 -0600175 return dropped_lock;
Jens Axboe771b53d02019-10-22 10:25:58 -0600176}
177
Jens Axboec5def4a2019-11-07 11:41:16 -0700178static inline struct io_wqe_acct *io_work_get_acct(struct io_wqe *wqe,
179 struct io_wq_work *work)
180{
181 if (work->flags & IO_WQ_WORK_UNBOUND)
182 return &wqe->acct[IO_WQ_ACCT_UNBOUND];
183
184 return &wqe->acct[IO_WQ_ACCT_BOUND];
185}
186
187static inline struct io_wqe_acct *io_wqe_get_acct(struct io_wqe *wqe,
188 struct io_worker *worker)
189{
190 if (worker->flags & IO_WORKER_F_BOUND)
191 return &wqe->acct[IO_WQ_ACCT_BOUND];
192
193 return &wqe->acct[IO_WQ_ACCT_UNBOUND];
194}
195
Jens Axboe771b53d02019-10-22 10:25:58 -0600196static void io_worker_exit(struct io_worker *worker)
197{
198 struct io_wqe *wqe = worker->wqe;
Jens Axboec5def4a2019-11-07 11:41:16 -0700199 struct io_wqe_acct *acct = io_wqe_get_acct(wqe, worker);
200 unsigned nr_workers;
Jens Axboe771b53d02019-10-22 10:25:58 -0600201
202 /*
203 * If we're not at zero, someone else is holding a brief reference
204 * to the worker. Wait for that to go away.
205 */
206 set_current_state(TASK_INTERRUPTIBLE);
207 if (!refcount_dec_and_test(&worker->ref))
208 schedule();
209 __set_current_state(TASK_RUNNING);
210
211 preempt_disable();
212 current->flags &= ~PF_IO_WORKER;
213 if (worker->flags & IO_WORKER_F_RUNNING)
Jens Axboec5def4a2019-11-07 11:41:16 -0700214 atomic_dec(&acct->nr_running);
215 if (!(worker->flags & IO_WORKER_F_BOUND))
216 atomic_dec(&wqe->wq->user->processes);
Jens Axboe771b53d02019-10-22 10:25:58 -0600217 worker->flags = 0;
218 preempt_enable();
219
220 spin_lock_irq(&wqe->lock);
221 hlist_nulls_del_rcu(&worker->nulls_node);
Jens Axboee61df662019-11-13 13:54:49 -0700222 list_del_rcu(&worker->all_list);
Jens Axboe771b53d02019-10-22 10:25:58 -0600223 if (__io_worker_unuse(wqe, worker)) {
224 __release(&wqe->lock);
225 spin_lock_irq(&wqe->lock);
226 }
Jens Axboec5def4a2019-11-07 11:41:16 -0700227 acct->nr_workers--;
228 nr_workers = wqe->acct[IO_WQ_ACCT_BOUND].nr_workers +
229 wqe->acct[IO_WQ_ACCT_UNBOUND].nr_workers;
Jens Axboe771b53d02019-10-22 10:25:58 -0600230 spin_unlock_irq(&wqe->lock);
231
232 /* all workers gone, wq exit can proceed */
Jens Axboec5def4a2019-11-07 11:41:16 -0700233 if (!nr_workers && refcount_dec_and_test(&wqe->wq->refs))
Jens Axboe771b53d02019-10-22 10:25:58 -0600234 complete(&wqe->wq->done);
235
YueHaibing364b05f2019-11-02 15:55:01 +0800236 kfree_rcu(worker, rcu);
Jens Axboe771b53d02019-10-22 10:25:58 -0600237}
238
Jens Axboec5def4a2019-11-07 11:41:16 -0700239static inline bool io_wqe_run_queue(struct io_wqe *wqe)
240 __must_hold(wqe->lock)
241{
Jens Axboe6206f0e2019-11-26 11:59:32 -0700242 if (!wq_list_empty(&wqe->work_list) &&
243 !(wqe->flags & IO_WQE_FLAG_STALLED))
Jens Axboec5def4a2019-11-07 11:41:16 -0700244 return true;
245 return false;
246}
247
248/*
249 * Check head of free list for an available worker. If one isn't available,
250 * caller must wake up the wq manager to create one.
251 */
252static bool io_wqe_activate_free_worker(struct io_wqe *wqe)
253 __must_hold(RCU)
254{
255 struct hlist_nulls_node *n;
256 struct io_worker *worker;
257
Jens Axboe021d1cd2019-11-14 08:00:41 -0700258 n = rcu_dereference(hlist_nulls_first_rcu(&wqe->free_list));
Jens Axboec5def4a2019-11-07 11:41:16 -0700259 if (is_a_nulls(n))
260 return false;
261
262 worker = hlist_nulls_entry(n, struct io_worker, nulls_node);
263 if (io_worker_get(worker)) {
Jens Axboe506d95f2019-12-07 21:03:59 -0700264 wake_up_process(worker->task);
Jens Axboec5def4a2019-11-07 11:41:16 -0700265 io_worker_release(worker);
266 return true;
267 }
268
269 return false;
270}
271
272/*
273 * We need a worker. If we find a free one, we're good. If not, and we're
274 * below the max number of workers, wake up the manager to create one.
275 */
276static void io_wqe_wake_worker(struct io_wqe *wqe, struct io_wqe_acct *acct)
277{
278 bool ret;
279
280 /*
281 * Most likely an attempt to queue unbounded work on an io_wq that
282 * wasn't setup with any unbounded workers.
283 */
284 WARN_ON_ONCE(!acct->max_workers);
285
286 rcu_read_lock();
287 ret = io_wqe_activate_free_worker(wqe);
288 rcu_read_unlock();
289
290 if (!ret && acct->nr_workers < acct->max_workers)
291 wake_up_process(wqe->wq->manager);
292}
293
294static void io_wqe_inc_running(struct io_wqe *wqe, struct io_worker *worker)
295{
296 struct io_wqe_acct *acct = io_wqe_get_acct(wqe, worker);
297
298 atomic_inc(&acct->nr_running);
299}
300
301static void io_wqe_dec_running(struct io_wqe *wqe, struct io_worker *worker)
302 __must_hold(wqe->lock)
303{
304 struct io_wqe_acct *acct = io_wqe_get_acct(wqe, worker);
305
306 if (atomic_dec_and_test(&acct->nr_running) && io_wqe_run_queue(wqe))
307 io_wqe_wake_worker(wqe, acct);
308}
309
Jens Axboe771b53d02019-10-22 10:25:58 -0600310static void io_worker_start(struct io_wqe *wqe, struct io_worker *worker)
311{
312 allow_kernel_signal(SIGINT);
313
314 current->flags |= PF_IO_WORKER;
315
316 worker->flags |= (IO_WORKER_F_UP | IO_WORKER_F_RUNNING);
Jens Axboefcb323c2019-10-24 12:39:47 -0600317 worker->restore_files = current->files;
Jens Axboe9392a272020-02-06 21:42:51 -0700318 worker->restore_fs = current->fs;
Jens Axboec5def4a2019-11-07 11:41:16 -0700319 io_wqe_inc_running(wqe, worker);
Jens Axboe771b53d02019-10-22 10:25:58 -0600320}
321
322/*
323 * Worker will start processing some work. Move it to the busy list, if
324 * it's currently on the freelist
325 */
326static void __io_worker_busy(struct io_wqe *wqe, struct io_worker *worker,
327 struct io_wq_work *work)
328 __must_hold(wqe->lock)
329{
Jens Axboec5def4a2019-11-07 11:41:16 -0700330 bool worker_bound, work_bound;
331
Jens Axboe771b53d02019-10-22 10:25:58 -0600332 if (worker->flags & IO_WORKER_F_FREE) {
333 worker->flags &= ~IO_WORKER_F_FREE;
334 hlist_nulls_del_init_rcu(&worker->nulls_node);
Jens Axboe771b53d02019-10-22 10:25:58 -0600335 }
Jens Axboec5def4a2019-11-07 11:41:16 -0700336
337 /*
338 * If worker is moving from bound to unbound (or vice versa), then
339 * ensure we update the running accounting.
340 */
Dan Carpenterb2e9c7d62019-11-19 09:22:16 +0300341 worker_bound = (worker->flags & IO_WORKER_F_BOUND) != 0;
342 work_bound = (work->flags & IO_WQ_WORK_UNBOUND) == 0;
343 if (worker_bound != work_bound) {
Jens Axboec5def4a2019-11-07 11:41:16 -0700344 io_wqe_dec_running(wqe, worker);
345 if (work_bound) {
346 worker->flags |= IO_WORKER_F_BOUND;
347 wqe->acct[IO_WQ_ACCT_UNBOUND].nr_workers--;
348 wqe->acct[IO_WQ_ACCT_BOUND].nr_workers++;
349 atomic_dec(&wqe->wq->user->processes);
350 } else {
351 worker->flags &= ~IO_WORKER_F_BOUND;
352 wqe->acct[IO_WQ_ACCT_UNBOUND].nr_workers++;
353 wqe->acct[IO_WQ_ACCT_BOUND].nr_workers--;
354 atomic_inc(&wqe->wq->user->processes);
355 }
356 io_wqe_inc_running(wqe, worker);
357 }
Jens Axboe771b53d02019-10-22 10:25:58 -0600358}
359
360/*
361 * No work, worker going to sleep. Move to freelist, and unuse mm if we
362 * have one attached. Dropping the mm may potentially sleep, so we drop
363 * the lock in that case and return success. Since the caller has to
364 * retry the loop in that case (we changed task state), we don't regrab
365 * the lock if we return success.
366 */
367static bool __io_worker_idle(struct io_wqe *wqe, struct io_worker *worker)
368 __must_hold(wqe->lock)
369{
370 if (!(worker->flags & IO_WORKER_F_FREE)) {
371 worker->flags |= IO_WORKER_F_FREE;
Jens Axboe021d1cd2019-11-14 08:00:41 -0700372 hlist_nulls_add_head_rcu(&worker->nulls_node, &wqe->free_list);
Jens Axboe771b53d02019-10-22 10:25:58 -0600373 }
374
375 return __io_worker_unuse(wqe, worker);
376}
377
378static struct io_wq_work *io_get_next_work(struct io_wqe *wqe, unsigned *hash)
379 __must_hold(wqe->lock)
380{
Jens Axboe6206f0e2019-11-26 11:59:32 -0700381 struct io_wq_work_node *node, *prev;
Jens Axboe771b53d02019-10-22 10:25:58 -0600382 struct io_wq_work *work;
383
Jens Axboe6206f0e2019-11-26 11:59:32 -0700384 wq_list_for_each(node, prev, &wqe->work_list) {
385 work = container_of(node, struct io_wq_work, list);
386
Jens Axboe771b53d02019-10-22 10:25:58 -0600387 /* not hashed, can run anytime */
Pavel Begunkov8766dd52020-03-14 00:31:04 +0300388 if (!io_wq_is_hashed(work)) {
Jens Axboe6206f0e2019-11-26 11:59:32 -0700389 wq_node_del(&wqe->work_list, node, prev);
Jens Axboe771b53d02019-10-22 10:25:58 -0600390 return work;
391 }
392
393 /* hashed, can run if not already running */
394 *hash = work->flags >> IO_WQ_HASH_SHIFT;
Pavel Begunkov3684f242020-02-28 10:36:39 +0300395 if (!(wqe->hash_map & BIT(*hash))) {
396 wqe->hash_map |= BIT(*hash);
Jens Axboe6206f0e2019-11-26 11:59:32 -0700397 wq_node_del(&wqe->work_list, node, prev);
Jens Axboe771b53d02019-10-22 10:25:58 -0600398 return work;
399 }
400 }
401
402 return NULL;
403}
404
Jens Axboecccf0ee2020-01-27 16:34:48 -0700405static void io_wq_switch_mm(struct io_worker *worker, struct io_wq_work *work)
406{
407 if (worker->mm) {
408 unuse_mm(worker->mm);
409 mmput(worker->mm);
410 worker->mm = NULL;
411 }
412 if (!work->mm) {
413 set_fs(KERNEL_DS);
414 return;
415 }
416 if (mmget_not_zero(work->mm)) {
417 use_mm(work->mm);
418 if (!worker->mm)
419 set_fs(USER_DS);
420 worker->mm = work->mm;
421 /* hang on to this mm */
422 work->mm = NULL;
423 return;
424 }
425
426 /* failed grabbing mm, ensure work gets cancelled */
427 work->flags |= IO_WQ_WORK_CANCEL;
428}
429
430static void io_wq_switch_creds(struct io_worker *worker,
431 struct io_wq_work *work)
432{
433 const struct cred *old_creds = override_creds(work->creds);
434
435 worker->cur_creds = work->creds;
436 if (worker->saved_creds)
437 put_cred(old_creds); /* creds set by previous switch */
438 else
439 worker->saved_creds = old_creds;
440}
441
Pavel Begunkovdc026a72020-03-04 16:14:09 +0300442static void io_impersonate_work(struct io_worker *worker,
443 struct io_wq_work *work)
444{
445 if (work->files && current->files != work->files) {
446 task_lock(current);
447 current->files = work->files;
448 task_unlock(current);
449 }
450 if (work->fs && current->fs != work->fs)
451 current->fs = work->fs;
452 if (work->mm != worker->mm)
453 io_wq_switch_mm(worker, work);
454 if (worker->cur_creds != work->creds)
455 io_wq_switch_creds(worker, work);
456}
457
458static void io_assign_current_work(struct io_worker *worker,
459 struct io_wq_work *work)
460{
Pavel Begunkovd78298e2020-03-14 00:31:03 +0300461 if (work) {
462 /* flush pending signals before assigning new work */
463 if (signal_pending(current))
464 flush_signals(current);
465 cond_resched();
466 }
Pavel Begunkovdc026a72020-03-04 16:14:09 +0300467
468 spin_lock_irq(&worker->lock);
469 worker->cur_work = work;
470 spin_unlock_irq(&worker->lock);
471}
472
Jens Axboe771b53d02019-10-22 10:25:58 -0600473static void io_worker_handle_work(struct io_worker *worker)
474 __releases(wqe->lock)
475{
Jens Axboe771b53d02019-10-22 10:25:58 -0600476 struct io_wqe *wqe = worker->wqe;
477 struct io_wq *wq = wqe->wq;
Pavel Begunkovf462fd32020-03-04 16:14:11 +0300478 unsigned hash = -1U;
Jens Axboe771b53d02019-10-22 10:25:58 -0600479
480 do {
Pavel Begunkov58e39312020-03-04 16:14:10 +0300481 struct io_wq_work *work;
Pavel Begunkovf462fd32020-03-04 16:14:11 +0300482get_next:
Jens Axboe771b53d02019-10-22 10:25:58 -0600483 /*
Jens Axboe771b53d02019-10-22 10:25:58 -0600484 * If we got some work, mark us as busy. If we didn't, but
485 * the list isn't empty, it means we stalled on hashed work.
486 * Mark us stalled so we don't keep looking for work when we
487 * can't make progress, any work completion or insertion will
488 * clear the stalled flag.
489 */
490 work = io_get_next_work(wqe, &hash);
491 if (work)
492 __io_worker_busy(wqe, worker, work);
Jens Axboe6206f0e2019-11-26 11:59:32 -0700493 else if (!wq_list_empty(&wqe->work_list))
Jens Axboe771b53d02019-10-22 10:25:58 -0600494 wqe->flags |= IO_WQE_FLAG_STALLED;
495
496 spin_unlock_irq(&wqe->lock);
497 if (!work)
498 break;
Pavel Begunkov58e39312020-03-04 16:14:10 +0300499 io_assign_current_work(worker, work);
Jens Axboe36c2f922019-11-13 09:43:34 -0700500
Pavel Begunkovdc026a72020-03-04 16:14:09 +0300501 /* handle a whole dependent link */
502 do {
Pavel Begunkov58e39312020-03-04 16:14:10 +0300503 struct io_wq_work *old_work;
Hillf Dantonfd1c4bc2019-12-24 09:14:29 -0700504
Pavel Begunkov58e39312020-03-04 16:14:10 +0300505 io_impersonate_work(worker, work);
Pavel Begunkovdc026a72020-03-04 16:14:09 +0300506 /*
507 * OK to set IO_WQ_WORK_CANCEL even for uncancellable
508 * work, the worker function will do the right thing.
509 */
510 if (test_bit(IO_WQ_BIT_CANCEL, &wq->state))
511 work->flags |= IO_WQ_WORK_CANCEL;
Jens Axboe36c2f922019-11-13 09:43:34 -0700512
Pavel Begunkovdc026a72020-03-04 16:14:09 +0300513 old_work = work;
514 work->func(&work);
Pavel Begunkov58e39312020-03-04 16:14:10 +0300515 work = (old_work == work) ? NULL : work;
516 io_assign_current_work(worker, work);
Pavel Begunkove9fd9392020-03-04 16:14:12 +0300517 wq->free_work(old_work);
Pavel Begunkovdc026a72020-03-04 16:14:09 +0300518
519 if (hash != -1U) {
520 spin_lock_irq(&wqe->lock);
521 wqe->hash_map &= ~BIT_ULL(hash);
522 wqe->flags &= ~IO_WQE_FLAG_STALLED;
Pavel Begunkovdc026a72020-03-04 16:14:09 +0300523 /* dependent work is not hashed */
524 hash = -1U;
Pavel Begunkovf462fd32020-03-04 16:14:11 +0300525 /* skip unnecessary unlock-lock wqe->lock */
526 if (!work)
527 goto get_next;
528 spin_unlock_irq(&wqe->lock);
Pavel Begunkovdc026a72020-03-04 16:14:09 +0300529 }
Pavel Begunkov58e39312020-03-04 16:14:10 +0300530 } while (work);
Jens Axboe36c2f922019-11-13 09:43:34 -0700531
532 spin_lock_irq(&wqe->lock);
Jens Axboe771b53d02019-10-22 10:25:58 -0600533 } while (1);
534}
535
Jens Axboe771b53d02019-10-22 10:25:58 -0600536static int io_wqe_worker(void *data)
537{
538 struct io_worker *worker = data;
539 struct io_wqe *wqe = worker->wqe;
540 struct io_wq *wq = wqe->wq;
Jens Axboe771b53d02019-10-22 10:25:58 -0600541
542 io_worker_start(wqe, worker);
543
544 while (!test_bit(IO_WQ_BIT_EXIT, &wq->state)) {
Jens Axboe506d95f2019-12-07 21:03:59 -0700545 set_current_state(TASK_INTERRUPTIBLE);
Jens Axboee995d512019-12-07 21:06:46 -0700546loop:
Jens Axboe771b53d02019-10-22 10:25:58 -0600547 spin_lock_irq(&wqe->lock);
548 if (io_wqe_run_queue(wqe)) {
549 __set_current_state(TASK_RUNNING);
550 io_worker_handle_work(worker);
Jens Axboee995d512019-12-07 21:06:46 -0700551 goto loop;
Jens Axboe771b53d02019-10-22 10:25:58 -0600552 }
553 /* drops the lock on success, retry */
554 if (__io_worker_idle(wqe, worker)) {
555 __release(&wqe->lock);
Jens Axboee995d512019-12-07 21:06:46 -0700556 goto loop;
Jens Axboe771b53d02019-10-22 10:25:58 -0600557 }
558 spin_unlock_irq(&wqe->lock);
559 if (signal_pending(current))
560 flush_signals(current);
561 if (schedule_timeout(WORKER_IDLE_TIMEOUT))
562 continue;
563 /* timed out, exit unless we're the fixed worker */
564 if (test_bit(IO_WQ_BIT_EXIT, &wq->state) ||
565 !(worker->flags & IO_WORKER_F_FIXED))
566 break;
567 }
568
Jens Axboe771b53d02019-10-22 10:25:58 -0600569 if (test_bit(IO_WQ_BIT_EXIT, &wq->state)) {
570 spin_lock_irq(&wqe->lock);
Jens Axboe6206f0e2019-11-26 11:59:32 -0700571 if (!wq_list_empty(&wqe->work_list))
Jens Axboe771b53d02019-10-22 10:25:58 -0600572 io_worker_handle_work(worker);
573 else
574 spin_unlock_irq(&wqe->lock);
575 }
576
577 io_worker_exit(worker);
578 return 0;
579}
580
581/*
Jens Axboe771b53d02019-10-22 10:25:58 -0600582 * Called when a worker is scheduled in. Mark us as currently running.
583 */
584void io_wq_worker_running(struct task_struct *tsk)
585{
586 struct io_worker *worker = kthread_data(tsk);
587 struct io_wqe *wqe = worker->wqe;
588
589 if (!(worker->flags & IO_WORKER_F_UP))
590 return;
591 if (worker->flags & IO_WORKER_F_RUNNING)
592 return;
593 worker->flags |= IO_WORKER_F_RUNNING;
Jens Axboec5def4a2019-11-07 11:41:16 -0700594 io_wqe_inc_running(wqe, worker);
Jens Axboe771b53d02019-10-22 10:25:58 -0600595}
596
597/*
598 * Called when worker is going to sleep. If there are no workers currently
599 * running and we have work pending, wake up a free one or have the manager
600 * set one up.
601 */
602void io_wq_worker_sleeping(struct task_struct *tsk)
603{
604 struct io_worker *worker = kthread_data(tsk);
605 struct io_wqe *wqe = worker->wqe;
606
607 if (!(worker->flags & IO_WORKER_F_UP))
608 return;
609 if (!(worker->flags & IO_WORKER_F_RUNNING))
610 return;
611
612 worker->flags &= ~IO_WORKER_F_RUNNING;
613
614 spin_lock_irq(&wqe->lock);
Jens Axboec5def4a2019-11-07 11:41:16 -0700615 io_wqe_dec_running(wqe, worker);
Jens Axboe771b53d02019-10-22 10:25:58 -0600616 spin_unlock_irq(&wqe->lock);
617}
618
Jens Axboeb60fda62019-11-19 08:37:07 -0700619static bool create_io_worker(struct io_wq *wq, struct io_wqe *wqe, int index)
Jens Axboe771b53d02019-10-22 10:25:58 -0600620{
Jens Axboec5def4a2019-11-07 11:41:16 -0700621 struct io_wqe_acct *acct =&wqe->acct[index];
Jens Axboe771b53d02019-10-22 10:25:58 -0600622 struct io_worker *worker;
623
Jann Hornad6e0052019-11-26 17:39:45 +0100624 worker = kzalloc_node(sizeof(*worker), GFP_KERNEL, wqe->node);
Jens Axboe771b53d02019-10-22 10:25:58 -0600625 if (!worker)
Jens Axboeb60fda62019-11-19 08:37:07 -0700626 return false;
Jens Axboe771b53d02019-10-22 10:25:58 -0600627
628 refcount_set(&worker->ref, 1);
629 worker->nulls_node.pprev = NULL;
Jens Axboe771b53d02019-10-22 10:25:58 -0600630 worker->wqe = wqe;
Jens Axboe36c2f922019-11-13 09:43:34 -0700631 spin_lock_init(&worker->lock);
Jens Axboe771b53d02019-10-22 10:25:58 -0600632
633 worker->task = kthread_create_on_node(io_wqe_worker, worker, wqe->node,
Jens Axboec5def4a2019-11-07 11:41:16 -0700634 "io_wqe_worker-%d/%d", index, wqe->node);
Jens Axboe771b53d02019-10-22 10:25:58 -0600635 if (IS_ERR(worker->task)) {
636 kfree(worker);
Jens Axboeb60fda62019-11-19 08:37:07 -0700637 return false;
Jens Axboe771b53d02019-10-22 10:25:58 -0600638 }
639
640 spin_lock_irq(&wqe->lock);
Jens Axboe021d1cd2019-11-14 08:00:41 -0700641 hlist_nulls_add_head_rcu(&worker->nulls_node, &wqe->free_list);
Jens Axboee61df662019-11-13 13:54:49 -0700642 list_add_tail_rcu(&worker->all_list, &wqe->all_list);
Jens Axboe771b53d02019-10-22 10:25:58 -0600643 worker->flags |= IO_WORKER_F_FREE;
Jens Axboec5def4a2019-11-07 11:41:16 -0700644 if (index == IO_WQ_ACCT_BOUND)
645 worker->flags |= IO_WORKER_F_BOUND;
646 if (!acct->nr_workers && (worker->flags & IO_WORKER_F_BOUND))
Jens Axboe771b53d02019-10-22 10:25:58 -0600647 worker->flags |= IO_WORKER_F_FIXED;
Jens Axboec5def4a2019-11-07 11:41:16 -0700648 acct->nr_workers++;
Jens Axboe771b53d02019-10-22 10:25:58 -0600649 spin_unlock_irq(&wqe->lock);
650
Jens Axboec5def4a2019-11-07 11:41:16 -0700651 if (index == IO_WQ_ACCT_UNBOUND)
652 atomic_inc(&wq->user->processes);
653
Jens Axboe771b53d02019-10-22 10:25:58 -0600654 wake_up_process(worker->task);
Jens Axboeb60fda62019-11-19 08:37:07 -0700655 return true;
Jens Axboe771b53d02019-10-22 10:25:58 -0600656}
657
Jens Axboec5def4a2019-11-07 11:41:16 -0700658static inline bool io_wqe_need_worker(struct io_wqe *wqe, int index)
Jens Axboe771b53d02019-10-22 10:25:58 -0600659 __must_hold(wqe->lock)
660{
Jens Axboec5def4a2019-11-07 11:41:16 -0700661 struct io_wqe_acct *acct = &wqe->acct[index];
Jens Axboe771b53d02019-10-22 10:25:58 -0600662
Jens Axboec5def4a2019-11-07 11:41:16 -0700663 /* if we have available workers or no work, no need */
Jens Axboe021d1cd2019-11-14 08:00:41 -0700664 if (!hlist_nulls_empty(&wqe->free_list) || !io_wqe_run_queue(wqe))
Jens Axboec5def4a2019-11-07 11:41:16 -0700665 return false;
666 return acct->nr_workers < acct->max_workers;
Jens Axboe771b53d02019-10-22 10:25:58 -0600667}
668
669/*
670 * Manager thread. Tasked with creating new workers, if we need them.
671 */
672static int io_wq_manager(void *data)
673{
674 struct io_wq *wq = data;
Jann Horn3fc50ab2019-11-26 19:10:20 +0100675 int workers_to_create = num_possible_nodes();
676 int node;
Jens Axboeb60fda62019-11-19 08:37:07 -0700677
678 /* create fixed workers */
Jann Horn3fc50ab2019-11-26 19:10:20 +0100679 refcount_set(&wq->refs, workers_to_create);
680 for_each_node(node) {
Jens Axboe75634392020-02-11 06:30:06 -0700681 if (!node_online(node))
682 continue;
Jann Horn3fc50ab2019-11-26 19:10:20 +0100683 if (!create_io_worker(wq, wq->wqes[node], IO_WQ_ACCT_BOUND))
684 goto err;
685 workers_to_create--;
Jens Axboeb60fda62019-11-19 08:37:07 -0700686 }
687
Jens Axboe75634392020-02-11 06:30:06 -0700688 while (workers_to_create--)
689 refcount_dec(&wq->refs);
690
Jens Axboeb60fda62019-11-19 08:37:07 -0700691 complete(&wq->done);
Jens Axboe771b53d02019-10-22 10:25:58 -0600692
693 while (!kthread_should_stop()) {
Jann Horn3fc50ab2019-11-26 19:10:20 +0100694 for_each_node(node) {
695 struct io_wqe *wqe = wq->wqes[node];
Jens Axboec5def4a2019-11-07 11:41:16 -0700696 bool fork_worker[2] = { false, false };
Jens Axboe771b53d02019-10-22 10:25:58 -0600697
Jens Axboe75634392020-02-11 06:30:06 -0700698 if (!node_online(node))
699 continue;
700
Jens Axboe771b53d02019-10-22 10:25:58 -0600701 spin_lock_irq(&wqe->lock);
Jens Axboec5def4a2019-11-07 11:41:16 -0700702 if (io_wqe_need_worker(wqe, IO_WQ_ACCT_BOUND))
703 fork_worker[IO_WQ_ACCT_BOUND] = true;
704 if (io_wqe_need_worker(wqe, IO_WQ_ACCT_UNBOUND))
705 fork_worker[IO_WQ_ACCT_UNBOUND] = true;
Jens Axboe771b53d02019-10-22 10:25:58 -0600706 spin_unlock_irq(&wqe->lock);
Jens Axboec5def4a2019-11-07 11:41:16 -0700707 if (fork_worker[IO_WQ_ACCT_BOUND])
708 create_io_worker(wq, wqe, IO_WQ_ACCT_BOUND);
709 if (fork_worker[IO_WQ_ACCT_UNBOUND])
710 create_io_worker(wq, wqe, IO_WQ_ACCT_UNBOUND);
Jens Axboe771b53d02019-10-22 10:25:58 -0600711 }
712 set_current_state(TASK_INTERRUPTIBLE);
713 schedule_timeout(HZ);
714 }
715
716 return 0;
Jens Axboeb60fda62019-11-19 08:37:07 -0700717err:
718 set_bit(IO_WQ_BIT_ERROR, &wq->state);
719 set_bit(IO_WQ_BIT_EXIT, &wq->state);
Jann Horn3fc50ab2019-11-26 19:10:20 +0100720 if (refcount_sub_and_test(workers_to_create, &wq->refs))
Jens Axboeb60fda62019-11-19 08:37:07 -0700721 complete(&wq->done);
722 return 0;
Jens Axboe771b53d02019-10-22 10:25:58 -0600723}
724
Jens Axboec5def4a2019-11-07 11:41:16 -0700725static bool io_wq_can_queue(struct io_wqe *wqe, struct io_wqe_acct *acct,
726 struct io_wq_work *work)
727{
728 bool free_worker;
729
730 if (!(work->flags & IO_WQ_WORK_UNBOUND))
731 return true;
732 if (atomic_read(&acct->nr_running))
733 return true;
734
735 rcu_read_lock();
Jens Axboe021d1cd2019-11-14 08:00:41 -0700736 free_worker = !hlist_nulls_empty(&wqe->free_list);
Jens Axboec5def4a2019-11-07 11:41:16 -0700737 rcu_read_unlock();
738 if (free_worker)
739 return true;
740
741 if (atomic_read(&wqe->wq->user->processes) >= acct->max_workers &&
742 !(capable(CAP_SYS_RESOURCE) || capable(CAP_SYS_ADMIN)))
743 return false;
744
745 return true;
746}
747
Pavel Begunkove9fd9392020-03-04 16:14:12 +0300748static void io_run_cancel(struct io_wq_work *work, struct io_wqe *wqe)
Pavel Begunkovfc04c392020-03-01 19:18:19 +0300749{
Pavel Begunkove9fd9392020-03-04 16:14:12 +0300750 struct io_wq *wq = wqe->wq;
751
Pavel Begunkovfc04c392020-03-01 19:18:19 +0300752 do {
753 struct io_wq_work *old_work = work;
754
755 work->flags |= IO_WQ_WORK_CANCEL;
756 work->func(&work);
757 work = (work == old_work) ? NULL : work;
Pavel Begunkove9fd9392020-03-04 16:14:12 +0300758 wq->free_work(old_work);
Pavel Begunkovfc04c392020-03-01 19:18:19 +0300759 } while (work);
760}
761
Jens Axboe771b53d02019-10-22 10:25:58 -0600762static void io_wqe_enqueue(struct io_wqe *wqe, struct io_wq_work *work)
763{
Jens Axboec5def4a2019-11-07 11:41:16 -0700764 struct io_wqe_acct *acct = io_work_get_acct(wqe, work);
Jens Axboe895e2ca2019-12-17 08:46:33 -0700765 int work_flags;
Jens Axboe771b53d02019-10-22 10:25:58 -0600766 unsigned long flags;
767
Jens Axboec5def4a2019-11-07 11:41:16 -0700768 /*
769 * Do early check to see if we need a new unbound worker, and if we do,
770 * if we're allowed to do so. This isn't 100% accurate as there's a
771 * gap between this check and incrementing the value, but that's OK.
772 * It's close enough to not be an issue, fork() has the same delay.
773 */
774 if (unlikely(!io_wq_can_queue(wqe, acct, work))) {
Pavel Begunkove9fd9392020-03-04 16:14:12 +0300775 io_run_cancel(work, wqe);
Jens Axboec5def4a2019-11-07 11:41:16 -0700776 return;
777 }
778
Jens Axboe895e2ca2019-12-17 08:46:33 -0700779 work_flags = work->flags;
Jens Axboe771b53d02019-10-22 10:25:58 -0600780 spin_lock_irqsave(&wqe->lock, flags);
Jens Axboe6206f0e2019-11-26 11:59:32 -0700781 wq_list_add_tail(&work->list, &wqe->work_list);
Jens Axboe771b53d02019-10-22 10:25:58 -0600782 wqe->flags &= ~IO_WQE_FLAG_STALLED;
783 spin_unlock_irqrestore(&wqe->lock, flags);
784
Jens Axboe895e2ca2019-12-17 08:46:33 -0700785 if ((work_flags & IO_WQ_WORK_CONCURRENT) ||
786 !atomic_read(&acct->nr_running))
Jens Axboec5def4a2019-11-07 11:41:16 -0700787 io_wqe_wake_worker(wqe, acct);
Jens Axboe771b53d02019-10-22 10:25:58 -0600788}
789
790void io_wq_enqueue(struct io_wq *wq, struct io_wq_work *work)
791{
792 struct io_wqe *wqe = wq->wqes[numa_node_id()];
793
794 io_wqe_enqueue(wqe, work);
795}
796
797/*
Pavel Begunkov8766dd52020-03-14 00:31:04 +0300798 * Work items that hash to the same value will not be done in parallel.
799 * Used to limit concurrent writes, generally hashed by inode.
Jens Axboe771b53d02019-10-22 10:25:58 -0600800 */
Pavel Begunkov8766dd52020-03-14 00:31:04 +0300801void io_wq_hash_work(struct io_wq_work *work, void *val)
Jens Axboe771b53d02019-10-22 10:25:58 -0600802{
Pavel Begunkov8766dd52020-03-14 00:31:04 +0300803 unsigned int bit;
Jens Axboe771b53d02019-10-22 10:25:58 -0600804
805 bit = hash_ptr(val, IO_WQ_HASH_ORDER);
806 work->flags |= (IO_WQ_WORK_HASHED | (bit << IO_WQ_HASH_SHIFT));
Jens Axboe771b53d02019-10-22 10:25:58 -0600807}
808
809static bool io_wqe_worker_send_sig(struct io_worker *worker, void *data)
810{
811 send_sig(SIGINT, worker->task, 1);
812 return false;
813}
814
815/*
816 * Iterate the passed in list and call the specific function for each
817 * worker that isn't exiting
818 */
819static bool io_wq_for_each_worker(struct io_wqe *wqe,
Jens Axboe771b53d02019-10-22 10:25:58 -0600820 bool (*func)(struct io_worker *, void *),
821 void *data)
822{
Jens Axboe771b53d02019-10-22 10:25:58 -0600823 struct io_worker *worker;
824 bool ret = false;
825
Jens Axboee61df662019-11-13 13:54:49 -0700826 list_for_each_entry_rcu(worker, &wqe->all_list, all_list) {
Jens Axboe771b53d02019-10-22 10:25:58 -0600827 if (io_worker_get(worker)) {
Jens Axboe75634392020-02-11 06:30:06 -0700828 /* no task if node is/was offline */
829 if (worker->task)
830 ret = func(worker, data);
Jens Axboe771b53d02019-10-22 10:25:58 -0600831 io_worker_release(worker);
832 if (ret)
833 break;
834 }
835 }
Jens Axboee61df662019-11-13 13:54:49 -0700836
Jens Axboe771b53d02019-10-22 10:25:58 -0600837 return ret;
838}
839
840void io_wq_cancel_all(struct io_wq *wq)
841{
Jann Horn3fc50ab2019-11-26 19:10:20 +0100842 int node;
Jens Axboe771b53d02019-10-22 10:25:58 -0600843
844 set_bit(IO_WQ_BIT_CANCEL, &wq->state);
845
Jens Axboe771b53d02019-10-22 10:25:58 -0600846 rcu_read_lock();
Jann Horn3fc50ab2019-11-26 19:10:20 +0100847 for_each_node(node) {
848 struct io_wqe *wqe = wq->wqes[node];
Jens Axboe771b53d02019-10-22 10:25:58 -0600849
Jens Axboee61df662019-11-13 13:54:49 -0700850 io_wq_for_each_worker(wqe, io_wqe_worker_send_sig, NULL);
Jens Axboe771b53d02019-10-22 10:25:58 -0600851 }
852 rcu_read_unlock();
853}
854
Jens Axboe62755e32019-10-28 21:49:21 -0600855struct io_cb_cancel_data {
Pavel Begunkov2293b412020-03-07 01:15:39 +0300856 work_cancel_fn *fn;
857 void *data;
Jens Axboe62755e32019-10-28 21:49:21 -0600858};
859
Pavel Begunkov2293b412020-03-07 01:15:39 +0300860static bool io_wq_worker_cancel(struct io_worker *worker, void *data)
Jens Axboe62755e32019-10-28 21:49:21 -0600861{
Pavel Begunkov2293b412020-03-07 01:15:39 +0300862 struct io_cb_cancel_data *match = data;
Jens Axboe6f726532019-11-05 13:51:51 -0700863 unsigned long flags;
Jens Axboe62755e32019-10-28 21:49:21 -0600864 bool ret = false;
865
866 /*
867 * Hold the lock to avoid ->cur_work going out of scope, caller
Jens Axboe36c2f922019-11-13 09:43:34 -0700868 * may dereference the passed in work.
Jens Axboe62755e32019-10-28 21:49:21 -0600869 */
Jens Axboe36c2f922019-11-13 09:43:34 -0700870 spin_lock_irqsave(&worker->lock, flags);
Jens Axboe62755e32019-10-28 21:49:21 -0600871 if (worker->cur_work &&
Jens Axboe0c9d5cc2019-12-11 19:29:43 -0700872 !(worker->cur_work->flags & IO_WQ_WORK_NO_CANCEL) &&
Pavel Begunkov2293b412020-03-07 01:15:39 +0300873 match->fn(worker->cur_work, match->data)) {
Jens Axboe771b53d02019-10-22 10:25:58 -0600874 send_sig(SIGINT, worker->task, 1);
Jens Axboe36c2f922019-11-13 09:43:34 -0700875 ret = true;
Jens Axboe771b53d02019-10-22 10:25:58 -0600876 }
Jens Axboe36c2f922019-11-13 09:43:34 -0700877 spin_unlock_irqrestore(&worker->lock, flags);
Jens Axboe771b53d02019-10-22 10:25:58 -0600878
Jens Axboe36c2f922019-11-13 09:43:34 -0700879 return ret;
Jens Axboe771b53d02019-10-22 10:25:58 -0600880}
881
882static enum io_wq_cancel io_wqe_cancel_work(struct io_wqe *wqe,
Pavel Begunkov2293b412020-03-07 01:15:39 +0300883 struct io_cb_cancel_data *match)
Jens Axboe771b53d02019-10-22 10:25:58 -0600884{
Jens Axboe6206f0e2019-11-26 11:59:32 -0700885 struct io_wq_work_node *node, *prev;
Jens Axboe771b53d02019-10-22 10:25:58 -0600886 struct io_wq_work *work;
Jens Axboe6f726532019-11-05 13:51:51 -0700887 unsigned long flags;
Jens Axboe771b53d02019-10-22 10:25:58 -0600888 bool found = false;
889
Jens Axboe771b53d02019-10-22 10:25:58 -0600890 /*
891 * First check pending list, if we're lucky we can just remove it
892 * from there. CANCEL_OK means that the work is returned as-new,
893 * no completion will be posted for it.
894 */
Jens Axboe6f726532019-11-05 13:51:51 -0700895 spin_lock_irqsave(&wqe->lock, flags);
Jens Axboe6206f0e2019-11-26 11:59:32 -0700896 wq_list_for_each(node, prev, &wqe->work_list) {
897 work = container_of(node, struct io_wq_work, list);
898
Jens Axboe00bcda12020-02-08 19:13:32 -0700899 if (match->fn(work, match->data)) {
Jens Axboe6206f0e2019-11-26 11:59:32 -0700900 wq_node_del(&wqe->work_list, node, prev);
Jens Axboe771b53d02019-10-22 10:25:58 -0600901 found = true;
902 break;
903 }
904 }
Jens Axboe6f726532019-11-05 13:51:51 -0700905 spin_unlock_irqrestore(&wqe->lock, flags);
Jens Axboe771b53d02019-10-22 10:25:58 -0600906
907 if (found) {
Pavel Begunkove9fd9392020-03-04 16:14:12 +0300908 io_run_cancel(work, wqe);
Jens Axboe771b53d02019-10-22 10:25:58 -0600909 return IO_WQ_CANCEL_OK;
910 }
911
912 /*
913 * Now check if a free (going busy) or busy worker has the work
914 * currently running. If we find it there, we'll return CANCEL_RUNNING
Brian Gianforcarod195a662019-12-13 03:09:50 -0800915 * as an indication that we attempt to signal cancellation. The
Jens Axboe771b53d02019-10-22 10:25:58 -0600916 * completion will run normally in this case.
917 */
918 rcu_read_lock();
Jens Axboe00bcda12020-02-08 19:13:32 -0700919 found = io_wq_for_each_worker(wqe, io_wq_worker_cancel, match);
Jens Axboe771b53d02019-10-22 10:25:58 -0600920 rcu_read_unlock();
921 return found ? IO_WQ_CANCEL_RUNNING : IO_WQ_CANCEL_NOTFOUND;
922}
923
Pavel Begunkov2293b412020-03-07 01:15:39 +0300924enum io_wq_cancel io_wq_cancel_cb(struct io_wq *wq, work_cancel_fn *cancel,
925 void *data)
926{
927 struct io_cb_cancel_data match = {
928 .fn = cancel,
929 .data = data,
930 };
931 enum io_wq_cancel ret = IO_WQ_CANCEL_NOTFOUND;
932 int node;
933
934 for_each_node(node) {
935 struct io_wqe *wqe = wq->wqes[node];
936
937 ret = io_wqe_cancel_work(wqe, &match);
938 if (ret != IO_WQ_CANCEL_NOTFOUND)
939 break;
940 }
941
942 return ret;
943}
944
945static bool io_wq_io_cb_cancel_data(struct io_wq_work *work, void *data)
Jens Axboe00bcda12020-02-08 19:13:32 -0700946{
947 return work == data;
948}
949
Jens Axboe771b53d02019-10-22 10:25:58 -0600950enum io_wq_cancel io_wq_cancel_work(struct io_wq *wq, struct io_wq_work *cwork)
951{
Pavel Begunkov2293b412020-03-07 01:15:39 +0300952 return io_wq_cancel_cb(wq, io_wq_io_cb_cancel_data, (void *)cwork);
Jens Axboe771b53d02019-10-22 10:25:58 -0600953}
954
Jens Axboe36282882020-02-08 19:16:39 -0700955static bool io_wq_pid_match(struct io_wq_work *work, void *data)
956{
957 pid_t pid = (pid_t) (unsigned long) data;
958
Pavel Begunkov2293b412020-03-07 01:15:39 +0300959 return work->task_pid == pid;
Jens Axboe36282882020-02-08 19:16:39 -0700960}
961
962enum io_wq_cancel io_wq_cancel_pid(struct io_wq *wq, pid_t pid)
963{
Pavel Begunkov2293b412020-03-07 01:15:39 +0300964 void *data = (void *) (unsigned long) pid;
Jens Axboe36282882020-02-08 19:16:39 -0700965
Pavel Begunkov2293b412020-03-07 01:15:39 +0300966 return io_wq_cancel_cb(wq, io_wq_pid_match, data);
Jens Axboe36282882020-02-08 19:16:39 -0700967}
968
Jens Axboe576a3472019-11-25 08:49:20 -0700969struct io_wq *io_wq_create(unsigned bounded, struct io_wq_data *data)
Jens Axboe771b53d02019-10-22 10:25:58 -0600970{
Jann Horn3fc50ab2019-11-26 19:10:20 +0100971 int ret = -ENOMEM, node;
Jens Axboe771b53d02019-10-22 10:25:58 -0600972 struct io_wq *wq;
973
Pavel Begunkove9fd9392020-03-04 16:14:12 +0300974 if (WARN_ON_ONCE(!data->free_work))
975 return ERR_PTR(-EINVAL);
976
Jann Hornad6e0052019-11-26 17:39:45 +0100977 wq = kzalloc(sizeof(*wq), GFP_KERNEL);
Jens Axboe771b53d02019-10-22 10:25:58 -0600978 if (!wq)
979 return ERR_PTR(-ENOMEM);
980
Jann Horn3fc50ab2019-11-26 19:10:20 +0100981 wq->wqes = kcalloc(nr_node_ids, sizeof(struct io_wqe *), GFP_KERNEL);
Jens Axboe771b53d02019-10-22 10:25:58 -0600982 if (!wq->wqes) {
983 kfree(wq);
984 return ERR_PTR(-ENOMEM);
985 }
986
Pavel Begunkove9fd9392020-03-04 16:14:12 +0300987 wq->free_work = data->free_work;
Jens Axboe7d723062019-11-12 22:31:31 -0700988
Jens Axboec5def4a2019-11-07 11:41:16 -0700989 /* caller must already hold a reference to this */
Jens Axboe576a3472019-11-25 08:49:20 -0700990 wq->user = data->user;
Jens Axboec5def4a2019-11-07 11:41:16 -0700991
Jann Horn3fc50ab2019-11-26 19:10:20 +0100992 for_each_node(node) {
Jens Axboe771b53d02019-10-22 10:25:58 -0600993 struct io_wqe *wqe;
Jens Axboe75634392020-02-11 06:30:06 -0700994 int alloc_node = node;
Jens Axboe771b53d02019-10-22 10:25:58 -0600995
Jens Axboe75634392020-02-11 06:30:06 -0700996 if (!node_online(alloc_node))
997 alloc_node = NUMA_NO_NODE;
998 wqe = kzalloc_node(sizeof(struct io_wqe), GFP_KERNEL, alloc_node);
Jens Axboe771b53d02019-10-22 10:25:58 -0600999 if (!wqe)
Jann Horn3fc50ab2019-11-26 19:10:20 +01001000 goto err;
1001 wq->wqes[node] = wqe;
Jens Axboe75634392020-02-11 06:30:06 -07001002 wqe->node = alloc_node;
Jens Axboec5def4a2019-11-07 11:41:16 -07001003 wqe->acct[IO_WQ_ACCT_BOUND].max_workers = bounded;
1004 atomic_set(&wqe->acct[IO_WQ_ACCT_BOUND].nr_running, 0);
Jens Axboe576a3472019-11-25 08:49:20 -07001005 if (wq->user) {
Jens Axboec5def4a2019-11-07 11:41:16 -07001006 wqe->acct[IO_WQ_ACCT_UNBOUND].max_workers =
1007 task_rlimit(current, RLIMIT_NPROC);
1008 }
1009 atomic_set(&wqe->acct[IO_WQ_ACCT_UNBOUND].nr_running, 0);
Jens Axboe771b53d02019-10-22 10:25:58 -06001010 wqe->wq = wq;
1011 spin_lock_init(&wqe->lock);
Jens Axboe6206f0e2019-11-26 11:59:32 -07001012 INIT_WQ_LIST(&wqe->work_list);
Jens Axboe021d1cd2019-11-14 08:00:41 -07001013 INIT_HLIST_NULLS_HEAD(&wqe->free_list, 0);
Jens Axboee61df662019-11-13 13:54:49 -07001014 INIT_LIST_HEAD(&wqe->all_list);
Jens Axboe771b53d02019-10-22 10:25:58 -06001015 }
1016
1017 init_completion(&wq->done);
1018
Jens Axboe771b53d02019-10-22 10:25:58 -06001019 wq->manager = kthread_create(io_wq_manager, wq, "io_wq_manager");
1020 if (!IS_ERR(wq->manager)) {
1021 wake_up_process(wq->manager);
Jens Axboeb60fda62019-11-19 08:37:07 -07001022 wait_for_completion(&wq->done);
1023 if (test_bit(IO_WQ_BIT_ERROR, &wq->state)) {
1024 ret = -ENOMEM;
1025 goto err;
1026 }
Jens Axboe848f7e12020-01-23 15:33:32 -07001027 refcount_set(&wq->use_refs, 1);
Jens Axboeb60fda62019-11-19 08:37:07 -07001028 reinit_completion(&wq->done);
Jens Axboe771b53d02019-10-22 10:25:58 -06001029 return wq;
1030 }
1031
1032 ret = PTR_ERR(wq->manager);
Jens Axboe771b53d02019-10-22 10:25:58 -06001033 complete(&wq->done);
Jens Axboeb60fda62019-11-19 08:37:07 -07001034err:
Jann Horn3fc50ab2019-11-26 19:10:20 +01001035 for_each_node(node)
1036 kfree(wq->wqes[node]);
Jens Axboeb60fda62019-11-19 08:37:07 -07001037 kfree(wq->wqes);
1038 kfree(wq);
Jens Axboe771b53d02019-10-22 10:25:58 -06001039 return ERR_PTR(ret);
1040}
1041
Pavel Begunkoveba6f5a2020-01-28 03:15:47 +03001042bool io_wq_get(struct io_wq *wq, struct io_wq_data *data)
1043{
Pavel Begunkove9fd9392020-03-04 16:14:12 +03001044 if (data->free_work != wq->free_work)
Pavel Begunkoveba6f5a2020-01-28 03:15:47 +03001045 return false;
1046
1047 return refcount_inc_not_zero(&wq->use_refs);
1048}
1049
Jens Axboe771b53d02019-10-22 10:25:58 -06001050static bool io_wq_worker_wake(struct io_worker *worker, void *data)
1051{
1052 wake_up_process(worker->task);
1053 return false;
1054}
1055
Jens Axboe848f7e12020-01-23 15:33:32 -07001056static void __io_wq_destroy(struct io_wq *wq)
Jens Axboe771b53d02019-10-22 10:25:58 -06001057{
Jann Horn3fc50ab2019-11-26 19:10:20 +01001058 int node;
Jens Axboe771b53d02019-10-22 10:25:58 -06001059
Jens Axboeb60fda62019-11-19 08:37:07 -07001060 set_bit(IO_WQ_BIT_EXIT, &wq->state);
1061 if (wq->manager)
Jens Axboe771b53d02019-10-22 10:25:58 -06001062 kthread_stop(wq->manager);
Jens Axboe771b53d02019-10-22 10:25:58 -06001063
1064 rcu_read_lock();
Jann Horn3fc50ab2019-11-26 19:10:20 +01001065 for_each_node(node)
1066 io_wq_for_each_worker(wq->wqes[node], io_wq_worker_wake, NULL);
Jens Axboe771b53d02019-10-22 10:25:58 -06001067 rcu_read_unlock();
1068
1069 wait_for_completion(&wq->done);
1070
Jann Horn3fc50ab2019-11-26 19:10:20 +01001071 for_each_node(node)
1072 kfree(wq->wqes[node]);
Jens Axboe771b53d02019-10-22 10:25:58 -06001073 kfree(wq->wqes);
1074 kfree(wq);
1075}
Jens Axboe848f7e12020-01-23 15:33:32 -07001076
1077void io_wq_destroy(struct io_wq *wq)
1078{
1079 if (refcount_dec_and_test(&wq->use_refs))
1080 __io_wq_destroy(wq);
1081}