blob: b3fb61ec087084052c5aad330845601900338d6d [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
Pavel Begunkov60cf46a2020-03-14 00:31:05 +0300378static inline unsigned int io_get_work_hash(struct io_wq_work *work)
379{
380 return work->flags >> IO_WQ_HASH_SHIFT;
381}
382
383static struct io_wq_work *io_get_next_work(struct io_wqe *wqe)
Jens Axboe771b53d02019-10-22 10:25:58 -0600384 __must_hold(wqe->lock)
385{
Jens Axboe6206f0e2019-11-26 11:59:32 -0700386 struct io_wq_work_node *node, *prev;
Jens Axboe771b53d02019-10-22 10:25:58 -0600387 struct io_wq_work *work;
Pavel Begunkov60cf46a2020-03-14 00:31:05 +0300388 unsigned int hash;
Jens Axboe771b53d02019-10-22 10:25:58 -0600389
Jens Axboe6206f0e2019-11-26 11:59:32 -0700390 wq_list_for_each(node, prev, &wqe->work_list) {
391 work = container_of(node, struct io_wq_work, list);
392
Jens Axboe771b53d02019-10-22 10:25:58 -0600393 /* not hashed, can run anytime */
Pavel Begunkov8766dd52020-03-14 00:31:04 +0300394 if (!io_wq_is_hashed(work)) {
Jens Axboe6206f0e2019-11-26 11:59:32 -0700395 wq_node_del(&wqe->work_list, node, prev);
Jens Axboe771b53d02019-10-22 10:25:58 -0600396 return work;
397 }
398
399 /* hashed, can run if not already running */
Pavel Begunkov60cf46a2020-03-14 00:31:05 +0300400 hash = io_get_work_hash(work);
401 if (!(wqe->hash_map & BIT(hash))) {
402 wqe->hash_map |= BIT(hash);
Jens Axboe6206f0e2019-11-26 11:59:32 -0700403 wq_node_del(&wqe->work_list, node, prev);
Jens Axboe771b53d02019-10-22 10:25:58 -0600404 return work;
405 }
406 }
407
408 return NULL;
409}
410
Jens Axboecccf0ee2020-01-27 16:34:48 -0700411static void io_wq_switch_mm(struct io_worker *worker, struct io_wq_work *work)
412{
413 if (worker->mm) {
414 unuse_mm(worker->mm);
415 mmput(worker->mm);
416 worker->mm = NULL;
417 }
418 if (!work->mm) {
419 set_fs(KERNEL_DS);
420 return;
421 }
422 if (mmget_not_zero(work->mm)) {
423 use_mm(work->mm);
424 if (!worker->mm)
425 set_fs(USER_DS);
426 worker->mm = work->mm;
427 /* hang on to this mm */
428 work->mm = NULL;
429 return;
430 }
431
432 /* failed grabbing mm, ensure work gets cancelled */
433 work->flags |= IO_WQ_WORK_CANCEL;
434}
435
436static void io_wq_switch_creds(struct io_worker *worker,
437 struct io_wq_work *work)
438{
439 const struct cred *old_creds = override_creds(work->creds);
440
441 worker->cur_creds = work->creds;
442 if (worker->saved_creds)
443 put_cred(old_creds); /* creds set by previous switch */
444 else
445 worker->saved_creds = old_creds;
446}
447
Pavel Begunkovdc026a72020-03-04 16:14:09 +0300448static void io_impersonate_work(struct io_worker *worker,
449 struct io_wq_work *work)
450{
451 if (work->files && current->files != work->files) {
452 task_lock(current);
453 current->files = work->files;
454 task_unlock(current);
455 }
456 if (work->fs && current->fs != work->fs)
457 current->fs = work->fs;
458 if (work->mm != worker->mm)
459 io_wq_switch_mm(worker, work);
460 if (worker->cur_creds != work->creds)
461 io_wq_switch_creds(worker, work);
462}
463
464static void io_assign_current_work(struct io_worker *worker,
465 struct io_wq_work *work)
466{
Pavel Begunkovd78298e2020-03-14 00:31:03 +0300467 if (work) {
468 /* flush pending signals before assigning new work */
469 if (signal_pending(current))
470 flush_signals(current);
471 cond_resched();
472 }
Pavel Begunkovdc026a72020-03-04 16:14:09 +0300473
474 spin_lock_irq(&worker->lock);
475 worker->cur_work = work;
476 spin_unlock_irq(&worker->lock);
477}
478
Pavel Begunkov60cf46a2020-03-14 00:31:05 +0300479static void io_wqe_enqueue(struct io_wqe *wqe, struct io_wq_work *work);
480
Jens Axboe771b53d02019-10-22 10:25:58 -0600481static void io_worker_handle_work(struct io_worker *worker)
482 __releases(wqe->lock)
483{
Jens Axboe771b53d02019-10-22 10:25:58 -0600484 struct io_wqe *wqe = worker->wqe;
485 struct io_wq *wq = wqe->wq;
486
487 do {
Pavel Begunkovf2cf1142020-03-22 19:14:26 +0300488 struct io_wq_work *work, *assign_work;
Pavel Begunkov60cf46a2020-03-14 00:31:05 +0300489 unsigned int hash;
Pavel Begunkovf462fd32020-03-04 16:14:11 +0300490get_next:
Jens Axboe771b53d02019-10-22 10:25:58 -0600491 /*
Jens Axboe771b53d02019-10-22 10:25:58 -0600492 * If we got some work, mark us as busy. If we didn't, but
493 * the list isn't empty, it means we stalled on hashed work.
494 * Mark us stalled so we don't keep looking for work when we
495 * can't make progress, any work completion or insertion will
496 * clear the stalled flag.
497 */
Pavel Begunkov60cf46a2020-03-14 00:31:05 +0300498 work = io_get_next_work(wqe);
Jens Axboe771b53d02019-10-22 10:25:58 -0600499 if (work)
500 __io_worker_busy(wqe, worker, work);
Jens Axboe6206f0e2019-11-26 11:59:32 -0700501 else if (!wq_list_empty(&wqe->work_list))
Jens Axboe771b53d02019-10-22 10:25:58 -0600502 wqe->flags |= IO_WQE_FLAG_STALLED;
503
504 spin_unlock_irq(&wqe->lock);
505 if (!work)
506 break;
Pavel Begunkov58e39312020-03-04 16:14:10 +0300507 io_assign_current_work(worker, work);
Jens Axboe36c2f922019-11-13 09:43:34 -0700508
Pavel Begunkovdc026a72020-03-04 16:14:09 +0300509 /* handle a whole dependent link */
510 do {
Pavel Begunkov58e39312020-03-04 16:14:10 +0300511 struct io_wq_work *old_work;
Hillf Dantonfd1c4bc2019-12-24 09:14:29 -0700512
Pavel Begunkov58e39312020-03-04 16:14:10 +0300513 io_impersonate_work(worker, work);
Pavel Begunkovdc026a72020-03-04 16:14:09 +0300514 /*
515 * OK to set IO_WQ_WORK_CANCEL even for uncancellable
516 * work, the worker function will do the right thing.
517 */
518 if (test_bit(IO_WQ_BIT_CANCEL, &wq->state))
519 work->flags |= IO_WQ_WORK_CANCEL;
Jens Axboe36c2f922019-11-13 09:43:34 -0700520
Pavel Begunkovdc026a72020-03-04 16:14:09 +0300521 old_work = work;
Pavel Begunkov60cf46a2020-03-14 00:31:05 +0300522 hash = io_get_work_hash(work);
Pavel Begunkovdc026a72020-03-04 16:14:09 +0300523 work->func(&work);
Pavel Begunkov58e39312020-03-04 16:14:10 +0300524 work = (old_work == work) ? NULL : work;
Pavel Begunkovf2cf1142020-03-22 19:14:26 +0300525
526 assign_work = work;
527 if (work && io_wq_is_hashed(work))
528 assign_work = NULL;
529 io_assign_current_work(worker, assign_work);
Pavel Begunkove9fd9392020-03-04 16:14:12 +0300530 wq->free_work(old_work);
Pavel Begunkovdc026a72020-03-04 16:14:09 +0300531
Pavel Begunkovf2cf1142020-03-22 19:14:26 +0300532 if (work && !assign_work) {
Pavel Begunkov60cf46a2020-03-14 00:31:05 +0300533 io_wqe_enqueue(wqe, work);
534 work = NULL;
535 }
Pavel Begunkovdc026a72020-03-04 16:14:09 +0300536 if (hash != -1U) {
537 spin_lock_irq(&wqe->lock);
538 wqe->hash_map &= ~BIT_ULL(hash);
539 wqe->flags &= ~IO_WQE_FLAG_STALLED;
Pavel Begunkovdc026a72020-03-04 16:14:09 +0300540 /* dependent work is not hashed */
541 hash = -1U;
Pavel Begunkovf462fd32020-03-04 16:14:11 +0300542 /* skip unnecessary unlock-lock wqe->lock */
543 if (!work)
544 goto get_next;
545 spin_unlock_irq(&wqe->lock);
Pavel Begunkovdc026a72020-03-04 16:14:09 +0300546 }
Pavel Begunkov58e39312020-03-04 16:14:10 +0300547 } while (work);
Jens Axboe36c2f922019-11-13 09:43:34 -0700548
549 spin_lock_irq(&wqe->lock);
Jens Axboe771b53d02019-10-22 10:25:58 -0600550 } while (1);
551}
552
Jens Axboe771b53d02019-10-22 10:25:58 -0600553static int io_wqe_worker(void *data)
554{
555 struct io_worker *worker = data;
556 struct io_wqe *wqe = worker->wqe;
557 struct io_wq *wq = wqe->wq;
Jens Axboe771b53d02019-10-22 10:25:58 -0600558
559 io_worker_start(wqe, worker);
560
561 while (!test_bit(IO_WQ_BIT_EXIT, &wq->state)) {
Jens Axboe506d95f2019-12-07 21:03:59 -0700562 set_current_state(TASK_INTERRUPTIBLE);
Jens Axboee995d512019-12-07 21:06:46 -0700563loop:
Jens Axboe771b53d02019-10-22 10:25:58 -0600564 spin_lock_irq(&wqe->lock);
565 if (io_wqe_run_queue(wqe)) {
566 __set_current_state(TASK_RUNNING);
567 io_worker_handle_work(worker);
Jens Axboee995d512019-12-07 21:06:46 -0700568 goto loop;
Jens Axboe771b53d02019-10-22 10:25:58 -0600569 }
570 /* drops the lock on success, retry */
571 if (__io_worker_idle(wqe, worker)) {
572 __release(&wqe->lock);
Jens Axboee995d512019-12-07 21:06:46 -0700573 goto loop;
Jens Axboe771b53d02019-10-22 10:25:58 -0600574 }
575 spin_unlock_irq(&wqe->lock);
576 if (signal_pending(current))
577 flush_signals(current);
578 if (schedule_timeout(WORKER_IDLE_TIMEOUT))
579 continue;
580 /* timed out, exit unless we're the fixed worker */
581 if (test_bit(IO_WQ_BIT_EXIT, &wq->state) ||
582 !(worker->flags & IO_WORKER_F_FIXED))
583 break;
584 }
585
Jens Axboe771b53d02019-10-22 10:25:58 -0600586 if (test_bit(IO_WQ_BIT_EXIT, &wq->state)) {
587 spin_lock_irq(&wqe->lock);
Jens Axboe6206f0e2019-11-26 11:59:32 -0700588 if (!wq_list_empty(&wqe->work_list))
Jens Axboe771b53d02019-10-22 10:25:58 -0600589 io_worker_handle_work(worker);
590 else
591 spin_unlock_irq(&wqe->lock);
592 }
593
594 io_worker_exit(worker);
595 return 0;
596}
597
598/*
Jens Axboe771b53d02019-10-22 10:25:58 -0600599 * Called when a worker is scheduled in. Mark us as currently running.
600 */
601void io_wq_worker_running(struct task_struct *tsk)
602{
603 struct io_worker *worker = kthread_data(tsk);
604 struct io_wqe *wqe = worker->wqe;
605
606 if (!(worker->flags & IO_WORKER_F_UP))
607 return;
608 if (worker->flags & IO_WORKER_F_RUNNING)
609 return;
610 worker->flags |= IO_WORKER_F_RUNNING;
Jens Axboec5def4a2019-11-07 11:41:16 -0700611 io_wqe_inc_running(wqe, worker);
Jens Axboe771b53d02019-10-22 10:25:58 -0600612}
613
614/*
615 * Called when worker is going to sleep. If there are no workers currently
616 * running and we have work pending, wake up a free one or have the manager
617 * set one up.
618 */
619void io_wq_worker_sleeping(struct task_struct *tsk)
620{
621 struct io_worker *worker = kthread_data(tsk);
622 struct io_wqe *wqe = worker->wqe;
623
624 if (!(worker->flags & IO_WORKER_F_UP))
625 return;
626 if (!(worker->flags & IO_WORKER_F_RUNNING))
627 return;
628
629 worker->flags &= ~IO_WORKER_F_RUNNING;
630
631 spin_lock_irq(&wqe->lock);
Jens Axboec5def4a2019-11-07 11:41:16 -0700632 io_wqe_dec_running(wqe, worker);
Jens Axboe771b53d02019-10-22 10:25:58 -0600633 spin_unlock_irq(&wqe->lock);
634}
635
Jens Axboeb60fda62019-11-19 08:37:07 -0700636static bool create_io_worker(struct io_wq *wq, struct io_wqe *wqe, int index)
Jens Axboe771b53d02019-10-22 10:25:58 -0600637{
Jens Axboec5def4a2019-11-07 11:41:16 -0700638 struct io_wqe_acct *acct =&wqe->acct[index];
Jens Axboe771b53d02019-10-22 10:25:58 -0600639 struct io_worker *worker;
640
Jann Hornad6e0052019-11-26 17:39:45 +0100641 worker = kzalloc_node(sizeof(*worker), GFP_KERNEL, wqe->node);
Jens Axboe771b53d02019-10-22 10:25:58 -0600642 if (!worker)
Jens Axboeb60fda62019-11-19 08:37:07 -0700643 return false;
Jens Axboe771b53d02019-10-22 10:25:58 -0600644
645 refcount_set(&worker->ref, 1);
646 worker->nulls_node.pprev = NULL;
Jens Axboe771b53d02019-10-22 10:25:58 -0600647 worker->wqe = wqe;
Jens Axboe36c2f922019-11-13 09:43:34 -0700648 spin_lock_init(&worker->lock);
Jens Axboe771b53d02019-10-22 10:25:58 -0600649
650 worker->task = kthread_create_on_node(io_wqe_worker, worker, wqe->node,
Jens Axboec5def4a2019-11-07 11:41:16 -0700651 "io_wqe_worker-%d/%d", index, wqe->node);
Jens Axboe771b53d02019-10-22 10:25:58 -0600652 if (IS_ERR(worker->task)) {
653 kfree(worker);
Jens Axboeb60fda62019-11-19 08:37:07 -0700654 return false;
Jens Axboe771b53d02019-10-22 10:25:58 -0600655 }
656
657 spin_lock_irq(&wqe->lock);
Jens Axboe021d1cd2019-11-14 08:00:41 -0700658 hlist_nulls_add_head_rcu(&worker->nulls_node, &wqe->free_list);
Jens Axboee61df662019-11-13 13:54:49 -0700659 list_add_tail_rcu(&worker->all_list, &wqe->all_list);
Jens Axboe771b53d02019-10-22 10:25:58 -0600660 worker->flags |= IO_WORKER_F_FREE;
Jens Axboec5def4a2019-11-07 11:41:16 -0700661 if (index == IO_WQ_ACCT_BOUND)
662 worker->flags |= IO_WORKER_F_BOUND;
663 if (!acct->nr_workers && (worker->flags & IO_WORKER_F_BOUND))
Jens Axboe771b53d02019-10-22 10:25:58 -0600664 worker->flags |= IO_WORKER_F_FIXED;
Jens Axboec5def4a2019-11-07 11:41:16 -0700665 acct->nr_workers++;
Jens Axboe771b53d02019-10-22 10:25:58 -0600666 spin_unlock_irq(&wqe->lock);
667
Jens Axboec5def4a2019-11-07 11:41:16 -0700668 if (index == IO_WQ_ACCT_UNBOUND)
669 atomic_inc(&wq->user->processes);
670
Jens Axboe771b53d02019-10-22 10:25:58 -0600671 wake_up_process(worker->task);
Jens Axboeb60fda62019-11-19 08:37:07 -0700672 return true;
Jens Axboe771b53d02019-10-22 10:25:58 -0600673}
674
Jens Axboec5def4a2019-11-07 11:41:16 -0700675static inline bool io_wqe_need_worker(struct io_wqe *wqe, int index)
Jens Axboe771b53d02019-10-22 10:25:58 -0600676 __must_hold(wqe->lock)
677{
Jens Axboec5def4a2019-11-07 11:41:16 -0700678 struct io_wqe_acct *acct = &wqe->acct[index];
Jens Axboe771b53d02019-10-22 10:25:58 -0600679
Jens Axboec5def4a2019-11-07 11:41:16 -0700680 /* if we have available workers or no work, no need */
Jens Axboe021d1cd2019-11-14 08:00:41 -0700681 if (!hlist_nulls_empty(&wqe->free_list) || !io_wqe_run_queue(wqe))
Jens Axboec5def4a2019-11-07 11:41:16 -0700682 return false;
683 return acct->nr_workers < acct->max_workers;
Jens Axboe771b53d02019-10-22 10:25:58 -0600684}
685
686/*
687 * Manager thread. Tasked with creating new workers, if we need them.
688 */
689static int io_wq_manager(void *data)
690{
691 struct io_wq *wq = data;
Jann Horn3fc50ab2019-11-26 19:10:20 +0100692 int workers_to_create = num_possible_nodes();
693 int node;
Jens Axboeb60fda62019-11-19 08:37:07 -0700694
695 /* create fixed workers */
Jann Horn3fc50ab2019-11-26 19:10:20 +0100696 refcount_set(&wq->refs, workers_to_create);
697 for_each_node(node) {
Jens Axboe75634392020-02-11 06:30:06 -0700698 if (!node_online(node))
699 continue;
Jann Horn3fc50ab2019-11-26 19:10:20 +0100700 if (!create_io_worker(wq, wq->wqes[node], IO_WQ_ACCT_BOUND))
701 goto err;
702 workers_to_create--;
Jens Axboeb60fda62019-11-19 08:37:07 -0700703 }
704
Jens Axboe75634392020-02-11 06:30:06 -0700705 while (workers_to_create--)
706 refcount_dec(&wq->refs);
707
Jens Axboeb60fda62019-11-19 08:37:07 -0700708 complete(&wq->done);
Jens Axboe771b53d02019-10-22 10:25:58 -0600709
710 while (!kthread_should_stop()) {
Jann Horn3fc50ab2019-11-26 19:10:20 +0100711 for_each_node(node) {
712 struct io_wqe *wqe = wq->wqes[node];
Jens Axboec5def4a2019-11-07 11:41:16 -0700713 bool fork_worker[2] = { false, false };
Jens Axboe771b53d02019-10-22 10:25:58 -0600714
Jens Axboe75634392020-02-11 06:30:06 -0700715 if (!node_online(node))
716 continue;
717
Jens Axboe771b53d02019-10-22 10:25:58 -0600718 spin_lock_irq(&wqe->lock);
Jens Axboec5def4a2019-11-07 11:41:16 -0700719 if (io_wqe_need_worker(wqe, IO_WQ_ACCT_BOUND))
720 fork_worker[IO_WQ_ACCT_BOUND] = true;
721 if (io_wqe_need_worker(wqe, IO_WQ_ACCT_UNBOUND))
722 fork_worker[IO_WQ_ACCT_UNBOUND] = true;
Jens Axboe771b53d02019-10-22 10:25:58 -0600723 spin_unlock_irq(&wqe->lock);
Jens Axboec5def4a2019-11-07 11:41:16 -0700724 if (fork_worker[IO_WQ_ACCT_BOUND])
725 create_io_worker(wq, wqe, IO_WQ_ACCT_BOUND);
726 if (fork_worker[IO_WQ_ACCT_UNBOUND])
727 create_io_worker(wq, wqe, IO_WQ_ACCT_UNBOUND);
Jens Axboe771b53d02019-10-22 10:25:58 -0600728 }
729 set_current_state(TASK_INTERRUPTIBLE);
730 schedule_timeout(HZ);
731 }
732
733 return 0;
Jens Axboeb60fda62019-11-19 08:37:07 -0700734err:
735 set_bit(IO_WQ_BIT_ERROR, &wq->state);
736 set_bit(IO_WQ_BIT_EXIT, &wq->state);
Jann Horn3fc50ab2019-11-26 19:10:20 +0100737 if (refcount_sub_and_test(workers_to_create, &wq->refs))
Jens Axboeb60fda62019-11-19 08:37:07 -0700738 complete(&wq->done);
739 return 0;
Jens Axboe771b53d02019-10-22 10:25:58 -0600740}
741
Jens Axboec5def4a2019-11-07 11:41:16 -0700742static bool io_wq_can_queue(struct io_wqe *wqe, struct io_wqe_acct *acct,
743 struct io_wq_work *work)
744{
745 bool free_worker;
746
747 if (!(work->flags & IO_WQ_WORK_UNBOUND))
748 return true;
749 if (atomic_read(&acct->nr_running))
750 return true;
751
752 rcu_read_lock();
Jens Axboe021d1cd2019-11-14 08:00:41 -0700753 free_worker = !hlist_nulls_empty(&wqe->free_list);
Jens Axboec5def4a2019-11-07 11:41:16 -0700754 rcu_read_unlock();
755 if (free_worker)
756 return true;
757
758 if (atomic_read(&wqe->wq->user->processes) >= acct->max_workers &&
759 !(capable(CAP_SYS_RESOURCE) || capable(CAP_SYS_ADMIN)))
760 return false;
761
762 return true;
763}
764
Pavel Begunkove9fd9392020-03-04 16:14:12 +0300765static void io_run_cancel(struct io_wq_work *work, struct io_wqe *wqe)
Pavel Begunkovfc04c392020-03-01 19:18:19 +0300766{
Pavel Begunkove9fd9392020-03-04 16:14:12 +0300767 struct io_wq *wq = wqe->wq;
768
Pavel Begunkovfc04c392020-03-01 19:18:19 +0300769 do {
770 struct io_wq_work *old_work = work;
771
772 work->flags |= IO_WQ_WORK_CANCEL;
773 work->func(&work);
774 work = (work == old_work) ? NULL : work;
Pavel Begunkove9fd9392020-03-04 16:14:12 +0300775 wq->free_work(old_work);
Pavel Begunkovfc04c392020-03-01 19:18:19 +0300776 } while (work);
777}
778
Jens Axboe771b53d02019-10-22 10:25:58 -0600779static void io_wqe_enqueue(struct io_wqe *wqe, struct io_wq_work *work)
780{
Jens Axboec5def4a2019-11-07 11:41:16 -0700781 struct io_wqe_acct *acct = io_work_get_acct(wqe, work);
Jens Axboe895e2ca2019-12-17 08:46:33 -0700782 int work_flags;
Jens Axboe771b53d02019-10-22 10:25:58 -0600783 unsigned long flags;
784
Jens Axboec5def4a2019-11-07 11:41:16 -0700785 /*
786 * Do early check to see if we need a new unbound worker, and if we do,
787 * if we're allowed to do so. This isn't 100% accurate as there's a
788 * gap between this check and incrementing the value, but that's OK.
789 * It's close enough to not be an issue, fork() has the same delay.
790 */
791 if (unlikely(!io_wq_can_queue(wqe, acct, work))) {
Pavel Begunkove9fd9392020-03-04 16:14:12 +0300792 io_run_cancel(work, wqe);
Jens Axboec5def4a2019-11-07 11:41:16 -0700793 return;
794 }
795
Jens Axboe895e2ca2019-12-17 08:46:33 -0700796 work_flags = work->flags;
Jens Axboe771b53d02019-10-22 10:25:58 -0600797 spin_lock_irqsave(&wqe->lock, flags);
Jens Axboe6206f0e2019-11-26 11:59:32 -0700798 wq_list_add_tail(&work->list, &wqe->work_list);
Jens Axboe771b53d02019-10-22 10:25:58 -0600799 wqe->flags &= ~IO_WQE_FLAG_STALLED;
800 spin_unlock_irqrestore(&wqe->lock, flags);
801
Jens Axboe895e2ca2019-12-17 08:46:33 -0700802 if ((work_flags & IO_WQ_WORK_CONCURRENT) ||
803 !atomic_read(&acct->nr_running))
Jens Axboec5def4a2019-11-07 11:41:16 -0700804 io_wqe_wake_worker(wqe, acct);
Jens Axboe771b53d02019-10-22 10:25:58 -0600805}
806
807void io_wq_enqueue(struct io_wq *wq, struct io_wq_work *work)
808{
809 struct io_wqe *wqe = wq->wqes[numa_node_id()];
810
811 io_wqe_enqueue(wqe, work);
812}
813
814/*
Pavel Begunkov8766dd52020-03-14 00:31:04 +0300815 * Work items that hash to the same value will not be done in parallel.
816 * Used to limit concurrent writes, generally hashed by inode.
Jens Axboe771b53d02019-10-22 10:25:58 -0600817 */
Pavel Begunkov8766dd52020-03-14 00:31:04 +0300818void io_wq_hash_work(struct io_wq_work *work, void *val)
Jens Axboe771b53d02019-10-22 10:25:58 -0600819{
Pavel Begunkov8766dd52020-03-14 00:31:04 +0300820 unsigned int bit;
Jens Axboe771b53d02019-10-22 10:25:58 -0600821
822 bit = hash_ptr(val, IO_WQ_HASH_ORDER);
823 work->flags |= (IO_WQ_WORK_HASHED | (bit << IO_WQ_HASH_SHIFT));
Jens Axboe771b53d02019-10-22 10:25:58 -0600824}
825
826static bool io_wqe_worker_send_sig(struct io_worker *worker, void *data)
827{
828 send_sig(SIGINT, worker->task, 1);
829 return false;
830}
831
832/*
833 * Iterate the passed in list and call the specific function for each
834 * worker that isn't exiting
835 */
836static bool io_wq_for_each_worker(struct io_wqe *wqe,
Jens Axboe771b53d02019-10-22 10:25:58 -0600837 bool (*func)(struct io_worker *, void *),
838 void *data)
839{
Jens Axboe771b53d02019-10-22 10:25:58 -0600840 struct io_worker *worker;
841 bool ret = false;
842
Jens Axboee61df662019-11-13 13:54:49 -0700843 list_for_each_entry_rcu(worker, &wqe->all_list, all_list) {
Jens Axboe771b53d02019-10-22 10:25:58 -0600844 if (io_worker_get(worker)) {
Jens Axboe75634392020-02-11 06:30:06 -0700845 /* no task if node is/was offline */
846 if (worker->task)
847 ret = func(worker, data);
Jens Axboe771b53d02019-10-22 10:25:58 -0600848 io_worker_release(worker);
849 if (ret)
850 break;
851 }
852 }
Jens Axboee61df662019-11-13 13:54:49 -0700853
Jens Axboe771b53d02019-10-22 10:25:58 -0600854 return ret;
855}
856
857void io_wq_cancel_all(struct io_wq *wq)
858{
Jann Horn3fc50ab2019-11-26 19:10:20 +0100859 int node;
Jens Axboe771b53d02019-10-22 10:25:58 -0600860
861 set_bit(IO_WQ_BIT_CANCEL, &wq->state);
862
Jens Axboe771b53d02019-10-22 10:25:58 -0600863 rcu_read_lock();
Jann Horn3fc50ab2019-11-26 19:10:20 +0100864 for_each_node(node) {
865 struct io_wqe *wqe = wq->wqes[node];
Jens Axboe771b53d02019-10-22 10:25:58 -0600866
Jens Axboee61df662019-11-13 13:54:49 -0700867 io_wq_for_each_worker(wqe, io_wqe_worker_send_sig, NULL);
Jens Axboe771b53d02019-10-22 10:25:58 -0600868 }
869 rcu_read_unlock();
870}
871
Jens Axboe62755e32019-10-28 21:49:21 -0600872struct io_cb_cancel_data {
Pavel Begunkov2293b412020-03-07 01:15:39 +0300873 work_cancel_fn *fn;
874 void *data;
Jens Axboe62755e32019-10-28 21:49:21 -0600875};
876
Pavel Begunkov2293b412020-03-07 01:15:39 +0300877static bool io_wq_worker_cancel(struct io_worker *worker, void *data)
Jens Axboe62755e32019-10-28 21:49:21 -0600878{
Pavel Begunkov2293b412020-03-07 01:15:39 +0300879 struct io_cb_cancel_data *match = data;
Jens Axboe6f726532019-11-05 13:51:51 -0700880 unsigned long flags;
Jens Axboe62755e32019-10-28 21:49:21 -0600881 bool ret = false;
882
883 /*
884 * Hold the lock to avoid ->cur_work going out of scope, caller
Jens Axboe36c2f922019-11-13 09:43:34 -0700885 * may dereference the passed in work.
Jens Axboe62755e32019-10-28 21:49:21 -0600886 */
Jens Axboe36c2f922019-11-13 09:43:34 -0700887 spin_lock_irqsave(&worker->lock, flags);
Jens Axboe62755e32019-10-28 21:49:21 -0600888 if (worker->cur_work &&
Jens Axboe0c9d5cc2019-12-11 19:29:43 -0700889 !(worker->cur_work->flags & IO_WQ_WORK_NO_CANCEL) &&
Pavel Begunkov2293b412020-03-07 01:15:39 +0300890 match->fn(worker->cur_work, match->data)) {
Jens Axboe771b53d02019-10-22 10:25:58 -0600891 send_sig(SIGINT, worker->task, 1);
Jens Axboe36c2f922019-11-13 09:43:34 -0700892 ret = true;
Jens Axboe771b53d02019-10-22 10:25:58 -0600893 }
Jens Axboe36c2f922019-11-13 09:43:34 -0700894 spin_unlock_irqrestore(&worker->lock, flags);
Jens Axboe771b53d02019-10-22 10:25:58 -0600895
Jens Axboe36c2f922019-11-13 09:43:34 -0700896 return ret;
Jens Axboe771b53d02019-10-22 10:25:58 -0600897}
898
899static enum io_wq_cancel io_wqe_cancel_work(struct io_wqe *wqe,
Pavel Begunkov2293b412020-03-07 01:15:39 +0300900 struct io_cb_cancel_data *match)
Jens Axboe771b53d02019-10-22 10:25:58 -0600901{
Jens Axboe6206f0e2019-11-26 11:59:32 -0700902 struct io_wq_work_node *node, *prev;
Jens Axboe771b53d02019-10-22 10:25:58 -0600903 struct io_wq_work *work;
Jens Axboe6f726532019-11-05 13:51:51 -0700904 unsigned long flags;
Jens Axboe771b53d02019-10-22 10:25:58 -0600905 bool found = false;
906
Jens Axboe771b53d02019-10-22 10:25:58 -0600907 /*
908 * First check pending list, if we're lucky we can just remove it
909 * from there. CANCEL_OK means that the work is returned as-new,
910 * no completion will be posted for it.
911 */
Jens Axboe6f726532019-11-05 13:51:51 -0700912 spin_lock_irqsave(&wqe->lock, flags);
Jens Axboe6206f0e2019-11-26 11:59:32 -0700913 wq_list_for_each(node, prev, &wqe->work_list) {
914 work = container_of(node, struct io_wq_work, list);
915
Jens Axboe00bcda12020-02-08 19:13:32 -0700916 if (match->fn(work, match->data)) {
Jens Axboe6206f0e2019-11-26 11:59:32 -0700917 wq_node_del(&wqe->work_list, node, prev);
Jens Axboe771b53d02019-10-22 10:25:58 -0600918 found = true;
919 break;
920 }
921 }
Jens Axboe6f726532019-11-05 13:51:51 -0700922 spin_unlock_irqrestore(&wqe->lock, flags);
Jens Axboe771b53d02019-10-22 10:25:58 -0600923
924 if (found) {
Pavel Begunkove9fd9392020-03-04 16:14:12 +0300925 io_run_cancel(work, wqe);
Jens Axboe771b53d02019-10-22 10:25:58 -0600926 return IO_WQ_CANCEL_OK;
927 }
928
929 /*
930 * Now check if a free (going busy) or busy worker has the work
931 * currently running. If we find it there, we'll return CANCEL_RUNNING
Brian Gianforcarod195a662019-12-13 03:09:50 -0800932 * as an indication that we attempt to signal cancellation. The
Jens Axboe771b53d02019-10-22 10:25:58 -0600933 * completion will run normally in this case.
934 */
935 rcu_read_lock();
Jens Axboe00bcda12020-02-08 19:13:32 -0700936 found = io_wq_for_each_worker(wqe, io_wq_worker_cancel, match);
Jens Axboe771b53d02019-10-22 10:25:58 -0600937 rcu_read_unlock();
938 return found ? IO_WQ_CANCEL_RUNNING : IO_WQ_CANCEL_NOTFOUND;
939}
940
Pavel Begunkov2293b412020-03-07 01:15:39 +0300941enum io_wq_cancel io_wq_cancel_cb(struct io_wq *wq, work_cancel_fn *cancel,
942 void *data)
943{
944 struct io_cb_cancel_data match = {
945 .fn = cancel,
946 .data = data,
947 };
948 enum io_wq_cancel ret = IO_WQ_CANCEL_NOTFOUND;
949 int node;
950
951 for_each_node(node) {
952 struct io_wqe *wqe = wq->wqes[node];
953
954 ret = io_wqe_cancel_work(wqe, &match);
955 if (ret != IO_WQ_CANCEL_NOTFOUND)
956 break;
957 }
958
959 return ret;
960}
961
962static bool io_wq_io_cb_cancel_data(struct io_wq_work *work, void *data)
Jens Axboe00bcda12020-02-08 19:13:32 -0700963{
964 return work == data;
965}
966
Jens Axboe771b53d02019-10-22 10:25:58 -0600967enum io_wq_cancel io_wq_cancel_work(struct io_wq *wq, struct io_wq_work *cwork)
968{
Pavel Begunkov2293b412020-03-07 01:15:39 +0300969 return io_wq_cancel_cb(wq, io_wq_io_cb_cancel_data, (void *)cwork);
Jens Axboe771b53d02019-10-22 10:25:58 -0600970}
971
Jens Axboe36282882020-02-08 19:16:39 -0700972static bool io_wq_pid_match(struct io_wq_work *work, void *data)
973{
974 pid_t pid = (pid_t) (unsigned long) data;
975
Pavel Begunkov2293b412020-03-07 01:15:39 +0300976 return work->task_pid == pid;
Jens Axboe36282882020-02-08 19:16:39 -0700977}
978
979enum io_wq_cancel io_wq_cancel_pid(struct io_wq *wq, pid_t pid)
980{
Pavel Begunkov2293b412020-03-07 01:15:39 +0300981 void *data = (void *) (unsigned long) pid;
Jens Axboe36282882020-02-08 19:16:39 -0700982
Pavel Begunkov2293b412020-03-07 01:15:39 +0300983 return io_wq_cancel_cb(wq, io_wq_pid_match, data);
Jens Axboe36282882020-02-08 19:16:39 -0700984}
985
Jens Axboe576a3472019-11-25 08:49:20 -0700986struct io_wq *io_wq_create(unsigned bounded, struct io_wq_data *data)
Jens Axboe771b53d02019-10-22 10:25:58 -0600987{
Jann Horn3fc50ab2019-11-26 19:10:20 +0100988 int ret = -ENOMEM, node;
Jens Axboe771b53d02019-10-22 10:25:58 -0600989 struct io_wq *wq;
990
Pavel Begunkove9fd9392020-03-04 16:14:12 +0300991 if (WARN_ON_ONCE(!data->free_work))
992 return ERR_PTR(-EINVAL);
993
Jann Hornad6e0052019-11-26 17:39:45 +0100994 wq = kzalloc(sizeof(*wq), GFP_KERNEL);
Jens Axboe771b53d02019-10-22 10:25:58 -0600995 if (!wq)
996 return ERR_PTR(-ENOMEM);
997
Jann Horn3fc50ab2019-11-26 19:10:20 +0100998 wq->wqes = kcalloc(nr_node_ids, sizeof(struct io_wqe *), GFP_KERNEL);
Jens Axboe771b53d02019-10-22 10:25:58 -0600999 if (!wq->wqes) {
1000 kfree(wq);
1001 return ERR_PTR(-ENOMEM);
1002 }
1003
Pavel Begunkove9fd9392020-03-04 16:14:12 +03001004 wq->free_work = data->free_work;
Jens Axboe7d723062019-11-12 22:31:31 -07001005
Jens Axboec5def4a2019-11-07 11:41:16 -07001006 /* caller must already hold a reference to this */
Jens Axboe576a3472019-11-25 08:49:20 -07001007 wq->user = data->user;
Jens Axboec5def4a2019-11-07 11:41:16 -07001008
Jann Horn3fc50ab2019-11-26 19:10:20 +01001009 for_each_node(node) {
Jens Axboe771b53d02019-10-22 10:25:58 -06001010 struct io_wqe *wqe;
Jens Axboe75634392020-02-11 06:30:06 -07001011 int alloc_node = node;
Jens Axboe771b53d02019-10-22 10:25:58 -06001012
Jens Axboe75634392020-02-11 06:30:06 -07001013 if (!node_online(alloc_node))
1014 alloc_node = NUMA_NO_NODE;
1015 wqe = kzalloc_node(sizeof(struct io_wqe), GFP_KERNEL, alloc_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 Axboe75634392020-02-11 06:30:06 -07001019 wqe->node = alloc_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->wq = wq;
1028 spin_lock_init(&wqe->lock);
Jens Axboe6206f0e2019-11-26 11:59:32 -07001029 INIT_WQ_LIST(&wqe->work_list);
Jens Axboe021d1cd2019-11-14 08:00:41 -07001030 INIT_HLIST_NULLS_HEAD(&wqe->free_list, 0);
Jens Axboee61df662019-11-13 13:54:49 -07001031 INIT_LIST_HEAD(&wqe->all_list);
Jens Axboe771b53d02019-10-22 10:25:58 -06001032 }
1033
1034 init_completion(&wq->done);
1035
Jens Axboe771b53d02019-10-22 10:25:58 -06001036 wq->manager = kthread_create(io_wq_manager, wq, "io_wq_manager");
1037 if (!IS_ERR(wq->manager)) {
1038 wake_up_process(wq->manager);
Jens Axboeb60fda62019-11-19 08:37:07 -07001039 wait_for_completion(&wq->done);
1040 if (test_bit(IO_WQ_BIT_ERROR, &wq->state)) {
1041 ret = -ENOMEM;
1042 goto err;
1043 }
Jens Axboe848f7e12020-01-23 15:33:32 -07001044 refcount_set(&wq->use_refs, 1);
Jens Axboeb60fda62019-11-19 08:37:07 -07001045 reinit_completion(&wq->done);
Jens Axboe771b53d02019-10-22 10:25:58 -06001046 return wq;
1047 }
1048
1049 ret = PTR_ERR(wq->manager);
Jens Axboe771b53d02019-10-22 10:25:58 -06001050 complete(&wq->done);
Jens Axboeb60fda62019-11-19 08:37:07 -07001051err:
Jann Horn3fc50ab2019-11-26 19:10:20 +01001052 for_each_node(node)
1053 kfree(wq->wqes[node]);
Jens Axboeb60fda62019-11-19 08:37:07 -07001054 kfree(wq->wqes);
1055 kfree(wq);
Jens Axboe771b53d02019-10-22 10:25:58 -06001056 return ERR_PTR(ret);
1057}
1058
Pavel Begunkoveba6f5a2020-01-28 03:15:47 +03001059bool io_wq_get(struct io_wq *wq, struct io_wq_data *data)
1060{
Pavel Begunkove9fd9392020-03-04 16:14:12 +03001061 if (data->free_work != wq->free_work)
Pavel Begunkoveba6f5a2020-01-28 03:15:47 +03001062 return false;
1063
1064 return refcount_inc_not_zero(&wq->use_refs);
1065}
1066
Jens Axboe771b53d02019-10-22 10:25:58 -06001067static bool io_wq_worker_wake(struct io_worker *worker, void *data)
1068{
1069 wake_up_process(worker->task);
1070 return false;
1071}
1072
Jens Axboe848f7e12020-01-23 15:33:32 -07001073static void __io_wq_destroy(struct io_wq *wq)
Jens Axboe771b53d02019-10-22 10:25:58 -06001074{
Jann Horn3fc50ab2019-11-26 19:10:20 +01001075 int node;
Jens Axboe771b53d02019-10-22 10:25:58 -06001076
Jens Axboeb60fda62019-11-19 08:37:07 -07001077 set_bit(IO_WQ_BIT_EXIT, &wq->state);
1078 if (wq->manager)
Jens Axboe771b53d02019-10-22 10:25:58 -06001079 kthread_stop(wq->manager);
Jens Axboe771b53d02019-10-22 10:25:58 -06001080
1081 rcu_read_lock();
Jann Horn3fc50ab2019-11-26 19:10:20 +01001082 for_each_node(node)
1083 io_wq_for_each_worker(wq->wqes[node], io_wq_worker_wake, NULL);
Jens Axboe771b53d02019-10-22 10:25:58 -06001084 rcu_read_unlock();
1085
1086 wait_for_completion(&wq->done);
1087
Jann Horn3fc50ab2019-11-26 19:10:20 +01001088 for_each_node(node)
1089 kfree(wq->wqes[node]);
Jens Axboe771b53d02019-10-22 10:25:58 -06001090 kfree(wq->wqes);
1091 kfree(wq);
1092}
Jens Axboe848f7e12020-01-23 15:33:32 -07001093
1094void io_wq_destroy(struct io_wq *wq)
1095{
1096 if (refcount_dec_and_test(&wq->use_refs))
1097 __io_wq_destroy(wq);
1098}