blob: 9541df2729de077ab3ed3dd07ea934e6007fbf00 [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 Begunkov58e39312020-03-04 16:14:10 +0300488 struct io_wq_work *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;
525 io_assign_current_work(worker, work);
Pavel Begunkove9fd9392020-03-04 16:14:12 +0300526 wq->free_work(old_work);
Pavel Begunkovdc026a72020-03-04 16:14:09 +0300527
Pavel Begunkov60cf46a2020-03-14 00:31:05 +0300528 if (work && io_wq_is_hashed(work)) {
529 io_wqe_enqueue(wqe, work);
530 work = NULL;
531 }
Pavel Begunkovdc026a72020-03-04 16:14:09 +0300532 if (hash != -1U) {
533 spin_lock_irq(&wqe->lock);
534 wqe->hash_map &= ~BIT_ULL(hash);
535 wqe->flags &= ~IO_WQE_FLAG_STALLED;
Pavel Begunkovdc026a72020-03-04 16:14:09 +0300536 /* dependent work is not hashed */
537 hash = -1U;
Pavel Begunkovf462fd32020-03-04 16:14:11 +0300538 /* skip unnecessary unlock-lock wqe->lock */
539 if (!work)
540 goto get_next;
541 spin_unlock_irq(&wqe->lock);
Pavel Begunkovdc026a72020-03-04 16:14:09 +0300542 }
Pavel Begunkov58e39312020-03-04 16:14:10 +0300543 } while (work);
Jens Axboe36c2f922019-11-13 09:43:34 -0700544
545 spin_lock_irq(&wqe->lock);
Jens Axboe771b53d02019-10-22 10:25:58 -0600546 } while (1);
547}
548
Jens Axboe771b53d02019-10-22 10:25:58 -0600549static int io_wqe_worker(void *data)
550{
551 struct io_worker *worker = data;
552 struct io_wqe *wqe = worker->wqe;
553 struct io_wq *wq = wqe->wq;
Jens Axboe771b53d02019-10-22 10:25:58 -0600554
555 io_worker_start(wqe, worker);
556
557 while (!test_bit(IO_WQ_BIT_EXIT, &wq->state)) {
Jens Axboe506d95f2019-12-07 21:03:59 -0700558 set_current_state(TASK_INTERRUPTIBLE);
Jens Axboee995d512019-12-07 21:06:46 -0700559loop:
Jens Axboe771b53d02019-10-22 10:25:58 -0600560 spin_lock_irq(&wqe->lock);
561 if (io_wqe_run_queue(wqe)) {
562 __set_current_state(TASK_RUNNING);
563 io_worker_handle_work(worker);
Jens Axboee995d512019-12-07 21:06:46 -0700564 goto loop;
Jens Axboe771b53d02019-10-22 10:25:58 -0600565 }
566 /* drops the lock on success, retry */
567 if (__io_worker_idle(wqe, worker)) {
568 __release(&wqe->lock);
Jens Axboee995d512019-12-07 21:06:46 -0700569 goto loop;
Jens Axboe771b53d02019-10-22 10:25:58 -0600570 }
571 spin_unlock_irq(&wqe->lock);
572 if (signal_pending(current))
573 flush_signals(current);
574 if (schedule_timeout(WORKER_IDLE_TIMEOUT))
575 continue;
576 /* timed out, exit unless we're the fixed worker */
577 if (test_bit(IO_WQ_BIT_EXIT, &wq->state) ||
578 !(worker->flags & IO_WORKER_F_FIXED))
579 break;
580 }
581
Jens Axboe771b53d02019-10-22 10:25:58 -0600582 if (test_bit(IO_WQ_BIT_EXIT, &wq->state)) {
583 spin_lock_irq(&wqe->lock);
Jens Axboe6206f0e2019-11-26 11:59:32 -0700584 if (!wq_list_empty(&wqe->work_list))
Jens Axboe771b53d02019-10-22 10:25:58 -0600585 io_worker_handle_work(worker);
586 else
587 spin_unlock_irq(&wqe->lock);
588 }
589
590 io_worker_exit(worker);
591 return 0;
592}
593
594/*
Jens Axboe771b53d02019-10-22 10:25:58 -0600595 * Called when a worker is scheduled in. Mark us as currently running.
596 */
597void io_wq_worker_running(struct task_struct *tsk)
598{
599 struct io_worker *worker = kthread_data(tsk);
600 struct io_wqe *wqe = worker->wqe;
601
602 if (!(worker->flags & IO_WORKER_F_UP))
603 return;
604 if (worker->flags & IO_WORKER_F_RUNNING)
605 return;
606 worker->flags |= IO_WORKER_F_RUNNING;
Jens Axboec5def4a2019-11-07 11:41:16 -0700607 io_wqe_inc_running(wqe, worker);
Jens Axboe771b53d02019-10-22 10:25:58 -0600608}
609
610/*
611 * Called when worker is going to sleep. If there are no workers currently
612 * running and we have work pending, wake up a free one or have the manager
613 * set one up.
614 */
615void io_wq_worker_sleeping(struct task_struct *tsk)
616{
617 struct io_worker *worker = kthread_data(tsk);
618 struct io_wqe *wqe = worker->wqe;
619
620 if (!(worker->flags & IO_WORKER_F_UP))
621 return;
622 if (!(worker->flags & IO_WORKER_F_RUNNING))
623 return;
624
625 worker->flags &= ~IO_WORKER_F_RUNNING;
626
627 spin_lock_irq(&wqe->lock);
Jens Axboec5def4a2019-11-07 11:41:16 -0700628 io_wqe_dec_running(wqe, worker);
Jens Axboe771b53d02019-10-22 10:25:58 -0600629 spin_unlock_irq(&wqe->lock);
630}
631
Jens Axboeb60fda62019-11-19 08:37:07 -0700632static bool create_io_worker(struct io_wq *wq, struct io_wqe *wqe, int index)
Jens Axboe771b53d02019-10-22 10:25:58 -0600633{
Jens Axboec5def4a2019-11-07 11:41:16 -0700634 struct io_wqe_acct *acct =&wqe->acct[index];
Jens Axboe771b53d02019-10-22 10:25:58 -0600635 struct io_worker *worker;
636
Jann Hornad6e0052019-11-26 17:39:45 +0100637 worker = kzalloc_node(sizeof(*worker), GFP_KERNEL, wqe->node);
Jens Axboe771b53d02019-10-22 10:25:58 -0600638 if (!worker)
Jens Axboeb60fda62019-11-19 08:37:07 -0700639 return false;
Jens Axboe771b53d02019-10-22 10:25:58 -0600640
641 refcount_set(&worker->ref, 1);
642 worker->nulls_node.pprev = NULL;
Jens Axboe771b53d02019-10-22 10:25:58 -0600643 worker->wqe = wqe;
Jens Axboe36c2f922019-11-13 09:43:34 -0700644 spin_lock_init(&worker->lock);
Jens Axboe771b53d02019-10-22 10:25:58 -0600645
646 worker->task = kthread_create_on_node(io_wqe_worker, worker, wqe->node,
Jens Axboec5def4a2019-11-07 11:41:16 -0700647 "io_wqe_worker-%d/%d", index, wqe->node);
Jens Axboe771b53d02019-10-22 10:25:58 -0600648 if (IS_ERR(worker->task)) {
649 kfree(worker);
Jens Axboeb60fda62019-11-19 08:37:07 -0700650 return false;
Jens Axboe771b53d02019-10-22 10:25:58 -0600651 }
652
653 spin_lock_irq(&wqe->lock);
Jens Axboe021d1cd2019-11-14 08:00:41 -0700654 hlist_nulls_add_head_rcu(&worker->nulls_node, &wqe->free_list);
Jens Axboee61df662019-11-13 13:54:49 -0700655 list_add_tail_rcu(&worker->all_list, &wqe->all_list);
Jens Axboe771b53d02019-10-22 10:25:58 -0600656 worker->flags |= IO_WORKER_F_FREE;
Jens Axboec5def4a2019-11-07 11:41:16 -0700657 if (index == IO_WQ_ACCT_BOUND)
658 worker->flags |= IO_WORKER_F_BOUND;
659 if (!acct->nr_workers && (worker->flags & IO_WORKER_F_BOUND))
Jens Axboe771b53d02019-10-22 10:25:58 -0600660 worker->flags |= IO_WORKER_F_FIXED;
Jens Axboec5def4a2019-11-07 11:41:16 -0700661 acct->nr_workers++;
Jens Axboe771b53d02019-10-22 10:25:58 -0600662 spin_unlock_irq(&wqe->lock);
663
Jens Axboec5def4a2019-11-07 11:41:16 -0700664 if (index == IO_WQ_ACCT_UNBOUND)
665 atomic_inc(&wq->user->processes);
666
Jens Axboe771b53d02019-10-22 10:25:58 -0600667 wake_up_process(worker->task);
Jens Axboeb60fda62019-11-19 08:37:07 -0700668 return true;
Jens Axboe771b53d02019-10-22 10:25:58 -0600669}
670
Jens Axboec5def4a2019-11-07 11:41:16 -0700671static inline bool io_wqe_need_worker(struct io_wqe *wqe, int index)
Jens Axboe771b53d02019-10-22 10:25:58 -0600672 __must_hold(wqe->lock)
673{
Jens Axboec5def4a2019-11-07 11:41:16 -0700674 struct io_wqe_acct *acct = &wqe->acct[index];
Jens Axboe771b53d02019-10-22 10:25:58 -0600675
Jens Axboec5def4a2019-11-07 11:41:16 -0700676 /* if we have available workers or no work, no need */
Jens Axboe021d1cd2019-11-14 08:00:41 -0700677 if (!hlist_nulls_empty(&wqe->free_list) || !io_wqe_run_queue(wqe))
Jens Axboec5def4a2019-11-07 11:41:16 -0700678 return false;
679 return acct->nr_workers < acct->max_workers;
Jens Axboe771b53d02019-10-22 10:25:58 -0600680}
681
682/*
683 * Manager thread. Tasked with creating new workers, if we need them.
684 */
685static int io_wq_manager(void *data)
686{
687 struct io_wq *wq = data;
Jann Horn3fc50ab2019-11-26 19:10:20 +0100688 int workers_to_create = num_possible_nodes();
689 int node;
Jens Axboeb60fda62019-11-19 08:37:07 -0700690
691 /* create fixed workers */
Jann Horn3fc50ab2019-11-26 19:10:20 +0100692 refcount_set(&wq->refs, workers_to_create);
693 for_each_node(node) {
Jens Axboe75634392020-02-11 06:30:06 -0700694 if (!node_online(node))
695 continue;
Jann Horn3fc50ab2019-11-26 19:10:20 +0100696 if (!create_io_worker(wq, wq->wqes[node], IO_WQ_ACCT_BOUND))
697 goto err;
698 workers_to_create--;
Jens Axboeb60fda62019-11-19 08:37:07 -0700699 }
700
Jens Axboe75634392020-02-11 06:30:06 -0700701 while (workers_to_create--)
702 refcount_dec(&wq->refs);
703
Jens Axboeb60fda62019-11-19 08:37:07 -0700704 complete(&wq->done);
Jens Axboe771b53d02019-10-22 10:25:58 -0600705
706 while (!kthread_should_stop()) {
Jann Horn3fc50ab2019-11-26 19:10:20 +0100707 for_each_node(node) {
708 struct io_wqe *wqe = wq->wqes[node];
Jens Axboec5def4a2019-11-07 11:41:16 -0700709 bool fork_worker[2] = { false, false };
Jens Axboe771b53d02019-10-22 10:25:58 -0600710
Jens Axboe75634392020-02-11 06:30:06 -0700711 if (!node_online(node))
712 continue;
713
Jens Axboe771b53d02019-10-22 10:25:58 -0600714 spin_lock_irq(&wqe->lock);
Jens Axboec5def4a2019-11-07 11:41:16 -0700715 if (io_wqe_need_worker(wqe, IO_WQ_ACCT_BOUND))
716 fork_worker[IO_WQ_ACCT_BOUND] = true;
717 if (io_wqe_need_worker(wqe, IO_WQ_ACCT_UNBOUND))
718 fork_worker[IO_WQ_ACCT_UNBOUND] = true;
Jens Axboe771b53d02019-10-22 10:25:58 -0600719 spin_unlock_irq(&wqe->lock);
Jens Axboec5def4a2019-11-07 11:41:16 -0700720 if (fork_worker[IO_WQ_ACCT_BOUND])
721 create_io_worker(wq, wqe, IO_WQ_ACCT_BOUND);
722 if (fork_worker[IO_WQ_ACCT_UNBOUND])
723 create_io_worker(wq, wqe, IO_WQ_ACCT_UNBOUND);
Jens Axboe771b53d02019-10-22 10:25:58 -0600724 }
725 set_current_state(TASK_INTERRUPTIBLE);
726 schedule_timeout(HZ);
727 }
728
729 return 0;
Jens Axboeb60fda62019-11-19 08:37:07 -0700730err:
731 set_bit(IO_WQ_BIT_ERROR, &wq->state);
732 set_bit(IO_WQ_BIT_EXIT, &wq->state);
Jann Horn3fc50ab2019-11-26 19:10:20 +0100733 if (refcount_sub_and_test(workers_to_create, &wq->refs))
Jens Axboeb60fda62019-11-19 08:37:07 -0700734 complete(&wq->done);
735 return 0;
Jens Axboe771b53d02019-10-22 10:25:58 -0600736}
737
Jens Axboec5def4a2019-11-07 11:41:16 -0700738static bool io_wq_can_queue(struct io_wqe *wqe, struct io_wqe_acct *acct,
739 struct io_wq_work *work)
740{
741 bool free_worker;
742
743 if (!(work->flags & IO_WQ_WORK_UNBOUND))
744 return true;
745 if (atomic_read(&acct->nr_running))
746 return true;
747
748 rcu_read_lock();
Jens Axboe021d1cd2019-11-14 08:00:41 -0700749 free_worker = !hlist_nulls_empty(&wqe->free_list);
Jens Axboec5def4a2019-11-07 11:41:16 -0700750 rcu_read_unlock();
751 if (free_worker)
752 return true;
753
754 if (atomic_read(&wqe->wq->user->processes) >= acct->max_workers &&
755 !(capable(CAP_SYS_RESOURCE) || capable(CAP_SYS_ADMIN)))
756 return false;
757
758 return true;
759}
760
Pavel Begunkove9fd9392020-03-04 16:14:12 +0300761static void io_run_cancel(struct io_wq_work *work, struct io_wqe *wqe)
Pavel Begunkovfc04c392020-03-01 19:18:19 +0300762{
Pavel Begunkove9fd9392020-03-04 16:14:12 +0300763 struct io_wq *wq = wqe->wq;
764
Pavel Begunkovfc04c392020-03-01 19:18:19 +0300765 do {
766 struct io_wq_work *old_work = work;
767
768 work->flags |= IO_WQ_WORK_CANCEL;
769 work->func(&work);
770 work = (work == old_work) ? NULL : work;
Pavel Begunkove9fd9392020-03-04 16:14:12 +0300771 wq->free_work(old_work);
Pavel Begunkovfc04c392020-03-01 19:18:19 +0300772 } while (work);
773}
774
Jens Axboe771b53d02019-10-22 10:25:58 -0600775static void io_wqe_enqueue(struct io_wqe *wqe, struct io_wq_work *work)
776{
Jens Axboec5def4a2019-11-07 11:41:16 -0700777 struct io_wqe_acct *acct = io_work_get_acct(wqe, work);
Jens Axboe895e2ca2019-12-17 08:46:33 -0700778 int work_flags;
Jens Axboe771b53d02019-10-22 10:25:58 -0600779 unsigned long flags;
780
Jens Axboec5def4a2019-11-07 11:41:16 -0700781 /*
782 * Do early check to see if we need a new unbound worker, and if we do,
783 * if we're allowed to do so. This isn't 100% accurate as there's a
784 * gap between this check and incrementing the value, but that's OK.
785 * It's close enough to not be an issue, fork() has the same delay.
786 */
787 if (unlikely(!io_wq_can_queue(wqe, acct, work))) {
Pavel Begunkove9fd9392020-03-04 16:14:12 +0300788 io_run_cancel(work, wqe);
Jens Axboec5def4a2019-11-07 11:41:16 -0700789 return;
790 }
791
Jens Axboe895e2ca2019-12-17 08:46:33 -0700792 work_flags = work->flags;
Jens Axboe771b53d02019-10-22 10:25:58 -0600793 spin_lock_irqsave(&wqe->lock, flags);
Jens Axboe6206f0e2019-11-26 11:59:32 -0700794 wq_list_add_tail(&work->list, &wqe->work_list);
Jens Axboe771b53d02019-10-22 10:25:58 -0600795 wqe->flags &= ~IO_WQE_FLAG_STALLED;
796 spin_unlock_irqrestore(&wqe->lock, flags);
797
Jens Axboe895e2ca2019-12-17 08:46:33 -0700798 if ((work_flags & IO_WQ_WORK_CONCURRENT) ||
799 !atomic_read(&acct->nr_running))
Jens Axboec5def4a2019-11-07 11:41:16 -0700800 io_wqe_wake_worker(wqe, acct);
Jens Axboe771b53d02019-10-22 10:25:58 -0600801}
802
803void io_wq_enqueue(struct io_wq *wq, struct io_wq_work *work)
804{
805 struct io_wqe *wqe = wq->wqes[numa_node_id()];
806
807 io_wqe_enqueue(wqe, work);
808}
809
810/*
Pavel Begunkov8766dd52020-03-14 00:31:04 +0300811 * Work items that hash to the same value will not be done in parallel.
812 * Used to limit concurrent writes, generally hashed by inode.
Jens Axboe771b53d02019-10-22 10:25:58 -0600813 */
Pavel Begunkov8766dd52020-03-14 00:31:04 +0300814void io_wq_hash_work(struct io_wq_work *work, void *val)
Jens Axboe771b53d02019-10-22 10:25:58 -0600815{
Pavel Begunkov8766dd52020-03-14 00:31:04 +0300816 unsigned int bit;
Jens Axboe771b53d02019-10-22 10:25:58 -0600817
818 bit = hash_ptr(val, IO_WQ_HASH_ORDER);
819 work->flags |= (IO_WQ_WORK_HASHED | (bit << IO_WQ_HASH_SHIFT));
Jens Axboe771b53d02019-10-22 10:25:58 -0600820}
821
822static bool io_wqe_worker_send_sig(struct io_worker *worker, void *data)
823{
824 send_sig(SIGINT, worker->task, 1);
825 return false;
826}
827
828/*
829 * Iterate the passed in list and call the specific function for each
830 * worker that isn't exiting
831 */
832static bool io_wq_for_each_worker(struct io_wqe *wqe,
Jens Axboe771b53d02019-10-22 10:25:58 -0600833 bool (*func)(struct io_worker *, void *),
834 void *data)
835{
Jens Axboe771b53d02019-10-22 10:25:58 -0600836 struct io_worker *worker;
837 bool ret = false;
838
Jens Axboee61df662019-11-13 13:54:49 -0700839 list_for_each_entry_rcu(worker, &wqe->all_list, all_list) {
Jens Axboe771b53d02019-10-22 10:25:58 -0600840 if (io_worker_get(worker)) {
Jens Axboe75634392020-02-11 06:30:06 -0700841 /* no task if node is/was offline */
842 if (worker->task)
843 ret = func(worker, data);
Jens Axboe771b53d02019-10-22 10:25:58 -0600844 io_worker_release(worker);
845 if (ret)
846 break;
847 }
848 }
Jens Axboee61df662019-11-13 13:54:49 -0700849
Jens Axboe771b53d02019-10-22 10:25:58 -0600850 return ret;
851}
852
853void io_wq_cancel_all(struct io_wq *wq)
854{
Jann Horn3fc50ab2019-11-26 19:10:20 +0100855 int node;
Jens Axboe771b53d02019-10-22 10:25:58 -0600856
857 set_bit(IO_WQ_BIT_CANCEL, &wq->state);
858
Jens Axboe771b53d02019-10-22 10:25:58 -0600859 rcu_read_lock();
Jann Horn3fc50ab2019-11-26 19:10:20 +0100860 for_each_node(node) {
861 struct io_wqe *wqe = wq->wqes[node];
Jens Axboe771b53d02019-10-22 10:25:58 -0600862
Jens Axboee61df662019-11-13 13:54:49 -0700863 io_wq_for_each_worker(wqe, io_wqe_worker_send_sig, NULL);
Jens Axboe771b53d02019-10-22 10:25:58 -0600864 }
865 rcu_read_unlock();
866}
867
Jens Axboe62755e32019-10-28 21:49:21 -0600868struct io_cb_cancel_data {
Pavel Begunkov2293b412020-03-07 01:15:39 +0300869 work_cancel_fn *fn;
870 void *data;
Jens Axboe62755e32019-10-28 21:49:21 -0600871};
872
Pavel Begunkov2293b412020-03-07 01:15:39 +0300873static bool io_wq_worker_cancel(struct io_worker *worker, void *data)
Jens Axboe62755e32019-10-28 21:49:21 -0600874{
Pavel Begunkov2293b412020-03-07 01:15:39 +0300875 struct io_cb_cancel_data *match = data;
Jens Axboe6f726532019-11-05 13:51:51 -0700876 unsigned long flags;
Jens Axboe62755e32019-10-28 21:49:21 -0600877 bool ret = false;
878
879 /*
880 * Hold the lock to avoid ->cur_work going out of scope, caller
Jens Axboe36c2f922019-11-13 09:43:34 -0700881 * may dereference the passed in work.
Jens Axboe62755e32019-10-28 21:49:21 -0600882 */
Jens Axboe36c2f922019-11-13 09:43:34 -0700883 spin_lock_irqsave(&worker->lock, flags);
Jens Axboe62755e32019-10-28 21:49:21 -0600884 if (worker->cur_work &&
Jens Axboe0c9d5cc2019-12-11 19:29:43 -0700885 !(worker->cur_work->flags & IO_WQ_WORK_NO_CANCEL) &&
Pavel Begunkov2293b412020-03-07 01:15:39 +0300886 match->fn(worker->cur_work, match->data)) {
Jens Axboe771b53d02019-10-22 10:25:58 -0600887 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,
Pavel Begunkov2293b412020-03-07 01:15:39 +0300896 struct io_cb_cancel_data *match)
Jens Axboe771b53d02019-10-22 10:25:58 -0600897{
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
Jens Axboe771b53d02019-10-22 10:25:58 -0600903 /*
904 * First check pending list, if we're lucky we can just remove it
905 * from there. CANCEL_OK means that the work is returned as-new,
906 * no completion will be posted for it.
907 */
Jens Axboe6f726532019-11-05 13:51:51 -0700908 spin_lock_irqsave(&wqe->lock, flags);
Jens Axboe6206f0e2019-11-26 11:59:32 -0700909 wq_list_for_each(node, prev, &wqe->work_list) {
910 work = container_of(node, struct io_wq_work, list);
911
Jens Axboe00bcda12020-02-08 19:13:32 -0700912 if (match->fn(work, match->data)) {
Jens Axboe6206f0e2019-11-26 11:59:32 -0700913 wq_node_del(&wqe->work_list, node, prev);
Jens Axboe771b53d02019-10-22 10:25:58 -0600914 found = true;
915 break;
916 }
917 }
Jens Axboe6f726532019-11-05 13:51:51 -0700918 spin_unlock_irqrestore(&wqe->lock, flags);
Jens Axboe771b53d02019-10-22 10:25:58 -0600919
920 if (found) {
Pavel Begunkove9fd9392020-03-04 16:14:12 +0300921 io_run_cancel(work, wqe);
Jens Axboe771b53d02019-10-22 10:25:58 -0600922 return IO_WQ_CANCEL_OK;
923 }
924
925 /*
926 * Now check if a free (going busy) or busy worker has the work
927 * currently running. If we find it there, we'll return CANCEL_RUNNING
Brian Gianforcarod195a662019-12-13 03:09:50 -0800928 * as an indication that we attempt to signal cancellation. The
Jens Axboe771b53d02019-10-22 10:25:58 -0600929 * completion will run normally in this case.
930 */
931 rcu_read_lock();
Jens Axboe00bcda12020-02-08 19:13:32 -0700932 found = io_wq_for_each_worker(wqe, io_wq_worker_cancel, match);
Jens Axboe771b53d02019-10-22 10:25:58 -0600933 rcu_read_unlock();
934 return found ? IO_WQ_CANCEL_RUNNING : IO_WQ_CANCEL_NOTFOUND;
935}
936
Pavel Begunkov2293b412020-03-07 01:15:39 +0300937enum io_wq_cancel io_wq_cancel_cb(struct io_wq *wq, work_cancel_fn *cancel,
938 void *data)
939{
940 struct io_cb_cancel_data match = {
941 .fn = cancel,
942 .data = data,
943 };
944 enum io_wq_cancel ret = IO_WQ_CANCEL_NOTFOUND;
945 int node;
946
947 for_each_node(node) {
948 struct io_wqe *wqe = wq->wqes[node];
949
950 ret = io_wqe_cancel_work(wqe, &match);
951 if (ret != IO_WQ_CANCEL_NOTFOUND)
952 break;
953 }
954
955 return ret;
956}
957
958static bool io_wq_io_cb_cancel_data(struct io_wq_work *work, void *data)
Jens Axboe00bcda12020-02-08 19:13:32 -0700959{
960 return work == data;
961}
962
Jens Axboe771b53d02019-10-22 10:25:58 -0600963enum io_wq_cancel io_wq_cancel_work(struct io_wq *wq, struct io_wq_work *cwork)
964{
Pavel Begunkov2293b412020-03-07 01:15:39 +0300965 return io_wq_cancel_cb(wq, io_wq_io_cb_cancel_data, (void *)cwork);
Jens Axboe771b53d02019-10-22 10:25:58 -0600966}
967
Jens Axboe36282882020-02-08 19:16:39 -0700968static bool io_wq_pid_match(struct io_wq_work *work, void *data)
969{
970 pid_t pid = (pid_t) (unsigned long) data;
971
Pavel Begunkov2293b412020-03-07 01:15:39 +0300972 return work->task_pid == pid;
Jens Axboe36282882020-02-08 19:16:39 -0700973}
974
975enum io_wq_cancel io_wq_cancel_pid(struct io_wq *wq, pid_t pid)
976{
Pavel Begunkov2293b412020-03-07 01:15:39 +0300977 void *data = (void *) (unsigned long) pid;
Jens Axboe36282882020-02-08 19:16:39 -0700978
Pavel Begunkov2293b412020-03-07 01:15:39 +0300979 return io_wq_cancel_cb(wq, io_wq_pid_match, data);
Jens Axboe36282882020-02-08 19:16:39 -0700980}
981
Jens Axboe576a3472019-11-25 08:49:20 -0700982struct io_wq *io_wq_create(unsigned bounded, struct io_wq_data *data)
Jens Axboe771b53d02019-10-22 10:25:58 -0600983{
Jann Horn3fc50ab2019-11-26 19:10:20 +0100984 int ret = -ENOMEM, node;
Jens Axboe771b53d02019-10-22 10:25:58 -0600985 struct io_wq *wq;
986
Pavel Begunkove9fd9392020-03-04 16:14:12 +0300987 if (WARN_ON_ONCE(!data->free_work))
988 return ERR_PTR(-EINVAL);
989
Jann Hornad6e0052019-11-26 17:39:45 +0100990 wq = kzalloc(sizeof(*wq), GFP_KERNEL);
Jens Axboe771b53d02019-10-22 10:25:58 -0600991 if (!wq)
992 return ERR_PTR(-ENOMEM);
993
Jann Horn3fc50ab2019-11-26 19:10:20 +0100994 wq->wqes = kcalloc(nr_node_ids, sizeof(struct io_wqe *), GFP_KERNEL);
Jens Axboe771b53d02019-10-22 10:25:58 -0600995 if (!wq->wqes) {
996 kfree(wq);
997 return ERR_PTR(-ENOMEM);
998 }
999
Pavel Begunkove9fd9392020-03-04 16:14:12 +03001000 wq->free_work = data->free_work;
Jens Axboe7d723062019-11-12 22:31:31 -07001001
Jens Axboec5def4a2019-11-07 11:41:16 -07001002 /* caller must already hold a reference to this */
Jens Axboe576a3472019-11-25 08:49:20 -07001003 wq->user = data->user;
Jens Axboec5def4a2019-11-07 11:41:16 -07001004
Jann Horn3fc50ab2019-11-26 19:10:20 +01001005 for_each_node(node) {
Jens Axboe771b53d02019-10-22 10:25:58 -06001006 struct io_wqe *wqe;
Jens Axboe75634392020-02-11 06:30:06 -07001007 int alloc_node = node;
Jens Axboe771b53d02019-10-22 10:25:58 -06001008
Jens Axboe75634392020-02-11 06:30:06 -07001009 if (!node_online(alloc_node))
1010 alloc_node = NUMA_NO_NODE;
1011 wqe = kzalloc_node(sizeof(struct io_wqe), GFP_KERNEL, alloc_node);
Jens Axboe771b53d02019-10-22 10:25:58 -06001012 if (!wqe)
Jann Horn3fc50ab2019-11-26 19:10:20 +01001013 goto err;
1014 wq->wqes[node] = wqe;
Jens Axboe75634392020-02-11 06:30:06 -07001015 wqe->node = alloc_node;
Jens Axboec5def4a2019-11-07 11:41:16 -07001016 wqe->acct[IO_WQ_ACCT_BOUND].max_workers = bounded;
1017 atomic_set(&wqe->acct[IO_WQ_ACCT_BOUND].nr_running, 0);
Jens Axboe576a3472019-11-25 08:49:20 -07001018 if (wq->user) {
Jens Axboec5def4a2019-11-07 11:41:16 -07001019 wqe->acct[IO_WQ_ACCT_UNBOUND].max_workers =
1020 task_rlimit(current, RLIMIT_NPROC);
1021 }
1022 atomic_set(&wqe->acct[IO_WQ_ACCT_UNBOUND].nr_running, 0);
Jens Axboe771b53d02019-10-22 10:25:58 -06001023 wqe->wq = wq;
1024 spin_lock_init(&wqe->lock);
Jens Axboe6206f0e2019-11-26 11:59:32 -07001025 INIT_WQ_LIST(&wqe->work_list);
Jens Axboe021d1cd2019-11-14 08:00:41 -07001026 INIT_HLIST_NULLS_HEAD(&wqe->free_list, 0);
Jens Axboee61df662019-11-13 13:54:49 -07001027 INIT_LIST_HEAD(&wqe->all_list);
Jens Axboe771b53d02019-10-22 10:25:58 -06001028 }
1029
1030 init_completion(&wq->done);
1031
Jens Axboe771b53d02019-10-22 10:25:58 -06001032 wq->manager = kthread_create(io_wq_manager, wq, "io_wq_manager");
1033 if (!IS_ERR(wq->manager)) {
1034 wake_up_process(wq->manager);
Jens Axboeb60fda62019-11-19 08:37:07 -07001035 wait_for_completion(&wq->done);
1036 if (test_bit(IO_WQ_BIT_ERROR, &wq->state)) {
1037 ret = -ENOMEM;
1038 goto err;
1039 }
Jens Axboe848f7e12020-01-23 15:33:32 -07001040 refcount_set(&wq->use_refs, 1);
Jens Axboeb60fda62019-11-19 08:37:07 -07001041 reinit_completion(&wq->done);
Jens Axboe771b53d02019-10-22 10:25:58 -06001042 return wq;
1043 }
1044
1045 ret = PTR_ERR(wq->manager);
Jens Axboe771b53d02019-10-22 10:25:58 -06001046 complete(&wq->done);
Jens Axboeb60fda62019-11-19 08:37:07 -07001047err:
Jann Horn3fc50ab2019-11-26 19:10:20 +01001048 for_each_node(node)
1049 kfree(wq->wqes[node]);
Jens Axboeb60fda62019-11-19 08:37:07 -07001050 kfree(wq->wqes);
1051 kfree(wq);
Jens Axboe771b53d02019-10-22 10:25:58 -06001052 return ERR_PTR(ret);
1053}
1054
Pavel Begunkoveba6f5a2020-01-28 03:15:47 +03001055bool io_wq_get(struct io_wq *wq, struct io_wq_data *data)
1056{
Pavel Begunkove9fd9392020-03-04 16:14:12 +03001057 if (data->free_work != wq->free_work)
Pavel Begunkoveba6f5a2020-01-28 03:15:47 +03001058 return false;
1059
1060 return refcount_inc_not_zero(&wq->use_refs);
1061}
1062
Jens Axboe771b53d02019-10-22 10:25:58 -06001063static bool io_wq_worker_wake(struct io_worker *worker, void *data)
1064{
1065 wake_up_process(worker->task);
1066 return false;
1067}
1068
Jens Axboe848f7e12020-01-23 15:33:32 -07001069static void __io_wq_destroy(struct io_wq *wq)
Jens Axboe771b53d02019-10-22 10:25:58 -06001070{
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}
Jens Axboe848f7e12020-01-23 15:33:32 -07001089
1090void io_wq_destroy(struct io_wq *wq)
1091{
1092 if (refcount_dec_and_test(&wq->use_refs))
1093 __io_wq_destroy(wq);
1094}