blob: 473af080470a342c475453f393f5a1deb50966fc [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
Jens Axboe7d723062019-11-12 22:31:31 -0700110 get_work_fn *get_work;
111 put_work_fn *put_work;
112
Jens Axboe771b53d02019-10-22 10:25:58 -0600113 struct task_struct *manager;
Jens Axboec5def4a2019-11-07 11:41:16 -0700114 struct user_struct *user;
Jens Axboe771b53d02019-10-22 10:25:58 -0600115 refcount_t refs;
116 struct completion done;
Jens Axboe848f7e12020-01-23 15:33:32 -0700117
118 refcount_t use_refs;
Jens Axboe771b53d02019-10-22 10:25:58 -0600119};
120
Jens Axboe771b53d02019-10-22 10:25:58 -0600121static bool io_worker_get(struct io_worker *worker)
122{
123 return refcount_inc_not_zero(&worker->ref);
124}
125
126static void io_worker_release(struct io_worker *worker)
127{
128 if (refcount_dec_and_test(&worker->ref))
129 wake_up_process(worker->task);
130}
131
132/*
133 * Note: drops the wqe->lock if returning true! The caller must re-acquire
134 * the lock in that case. Some callers need to restart handling if this
135 * happens, so we can't just re-acquire the lock on behalf of the caller.
136 */
137static bool __io_worker_unuse(struct io_wqe *wqe, struct io_worker *worker)
138{
Jens Axboefcb323c2019-10-24 12:39:47 -0600139 bool dropped_lock = false;
140
Jens Axboecccf0ee2020-01-27 16:34:48 -0700141 if (worker->saved_creds) {
142 revert_creds(worker->saved_creds);
143 worker->cur_creds = worker->saved_creds = NULL;
Jens Axboe181e4482019-11-25 08:52:30 -0700144 }
145
Jens Axboefcb323c2019-10-24 12:39:47 -0600146 if (current->files != worker->restore_files) {
147 __acquire(&wqe->lock);
148 spin_unlock_irq(&wqe->lock);
149 dropped_lock = true;
150
151 task_lock(current);
152 current->files = worker->restore_files;
153 task_unlock(current);
154 }
155
Jens Axboe9392a272020-02-06 21:42:51 -0700156 if (current->fs != worker->restore_fs)
157 current->fs = worker->restore_fs;
158
Jens Axboe771b53d02019-10-22 10:25:58 -0600159 /*
160 * If we have an active mm, we need to drop the wq lock before unusing
161 * it. If we do, return true and let the caller retry the idle loop.
162 */
163 if (worker->mm) {
Jens Axboefcb323c2019-10-24 12:39:47 -0600164 if (!dropped_lock) {
165 __acquire(&wqe->lock);
166 spin_unlock_irq(&wqe->lock);
167 dropped_lock = true;
168 }
Jens Axboe771b53d02019-10-22 10:25:58 -0600169 __set_current_state(TASK_RUNNING);
170 set_fs(KERNEL_DS);
171 unuse_mm(worker->mm);
172 mmput(worker->mm);
173 worker->mm = NULL;
Jens Axboe771b53d02019-10-22 10:25:58 -0600174 }
175
Jens Axboefcb323c2019-10-24 12:39:47 -0600176 return dropped_lock;
Jens Axboe771b53d02019-10-22 10:25:58 -0600177}
178
Jens Axboec5def4a2019-11-07 11:41:16 -0700179static inline struct io_wqe_acct *io_work_get_acct(struct io_wqe *wqe,
180 struct io_wq_work *work)
181{
182 if (work->flags & IO_WQ_WORK_UNBOUND)
183 return &wqe->acct[IO_WQ_ACCT_UNBOUND];
184
185 return &wqe->acct[IO_WQ_ACCT_BOUND];
186}
187
188static inline struct io_wqe_acct *io_wqe_get_acct(struct io_wqe *wqe,
189 struct io_worker *worker)
190{
191 if (worker->flags & IO_WORKER_F_BOUND)
192 return &wqe->acct[IO_WQ_ACCT_BOUND];
193
194 return &wqe->acct[IO_WQ_ACCT_UNBOUND];
195}
196
Jens Axboe771b53d02019-10-22 10:25:58 -0600197static void io_worker_exit(struct io_worker *worker)
198{
199 struct io_wqe *wqe = worker->wqe;
Jens Axboec5def4a2019-11-07 11:41:16 -0700200 struct io_wqe_acct *acct = io_wqe_get_acct(wqe, worker);
201 unsigned nr_workers;
Jens Axboe771b53d02019-10-22 10:25:58 -0600202
203 /*
204 * If we're not at zero, someone else is holding a brief reference
205 * to the worker. Wait for that to go away.
206 */
207 set_current_state(TASK_INTERRUPTIBLE);
208 if (!refcount_dec_and_test(&worker->ref))
209 schedule();
210 __set_current_state(TASK_RUNNING);
211
212 preempt_disable();
213 current->flags &= ~PF_IO_WORKER;
214 if (worker->flags & IO_WORKER_F_RUNNING)
Jens Axboec5def4a2019-11-07 11:41:16 -0700215 atomic_dec(&acct->nr_running);
216 if (!(worker->flags & IO_WORKER_F_BOUND))
217 atomic_dec(&wqe->wq->user->processes);
Jens Axboe771b53d02019-10-22 10:25:58 -0600218 worker->flags = 0;
219 preempt_enable();
220
221 spin_lock_irq(&wqe->lock);
222 hlist_nulls_del_rcu(&worker->nulls_node);
Jens Axboee61df662019-11-13 13:54:49 -0700223 list_del_rcu(&worker->all_list);
Jens Axboe771b53d02019-10-22 10:25:58 -0600224 if (__io_worker_unuse(wqe, worker)) {
225 __release(&wqe->lock);
226 spin_lock_irq(&wqe->lock);
227 }
Jens Axboec5def4a2019-11-07 11:41:16 -0700228 acct->nr_workers--;
229 nr_workers = wqe->acct[IO_WQ_ACCT_BOUND].nr_workers +
230 wqe->acct[IO_WQ_ACCT_UNBOUND].nr_workers;
Jens Axboe771b53d02019-10-22 10:25:58 -0600231 spin_unlock_irq(&wqe->lock);
232
233 /* all workers gone, wq exit can proceed */
Jens Axboec5def4a2019-11-07 11:41:16 -0700234 if (!nr_workers && refcount_dec_and_test(&wqe->wq->refs))
Jens Axboe771b53d02019-10-22 10:25:58 -0600235 complete(&wqe->wq->done);
236
YueHaibing364b05f2019-11-02 15:55:01 +0800237 kfree_rcu(worker, rcu);
Jens Axboe771b53d02019-10-22 10:25:58 -0600238}
239
Jens Axboec5def4a2019-11-07 11:41:16 -0700240static inline bool io_wqe_run_queue(struct io_wqe *wqe)
241 __must_hold(wqe->lock)
242{
Jens Axboe6206f0e2019-11-26 11:59:32 -0700243 if (!wq_list_empty(&wqe->work_list) &&
244 !(wqe->flags & IO_WQE_FLAG_STALLED))
Jens Axboec5def4a2019-11-07 11:41:16 -0700245 return true;
246 return false;
247}
248
249/*
250 * Check head of free list for an available worker. If one isn't available,
251 * caller must wake up the wq manager to create one.
252 */
253static bool io_wqe_activate_free_worker(struct io_wqe *wqe)
254 __must_hold(RCU)
255{
256 struct hlist_nulls_node *n;
257 struct io_worker *worker;
258
Jens Axboe021d1cd2019-11-14 08:00:41 -0700259 n = rcu_dereference(hlist_nulls_first_rcu(&wqe->free_list));
Jens Axboec5def4a2019-11-07 11:41:16 -0700260 if (is_a_nulls(n))
261 return false;
262
263 worker = hlist_nulls_entry(n, struct io_worker, nulls_node);
264 if (io_worker_get(worker)) {
Jens Axboe506d95f2019-12-07 21:03:59 -0700265 wake_up_process(worker->task);
Jens Axboec5def4a2019-11-07 11:41:16 -0700266 io_worker_release(worker);
267 return true;
268 }
269
270 return false;
271}
272
273/*
274 * We need a worker. If we find a free one, we're good. If not, and we're
275 * below the max number of workers, wake up the manager to create one.
276 */
277static void io_wqe_wake_worker(struct io_wqe *wqe, struct io_wqe_acct *acct)
278{
279 bool ret;
280
281 /*
282 * Most likely an attempt to queue unbounded work on an io_wq that
283 * wasn't setup with any unbounded workers.
284 */
285 WARN_ON_ONCE(!acct->max_workers);
286
287 rcu_read_lock();
288 ret = io_wqe_activate_free_worker(wqe);
289 rcu_read_unlock();
290
291 if (!ret && acct->nr_workers < acct->max_workers)
292 wake_up_process(wqe->wq->manager);
293}
294
295static void io_wqe_inc_running(struct io_wqe *wqe, struct io_worker *worker)
296{
297 struct io_wqe_acct *acct = io_wqe_get_acct(wqe, worker);
298
299 atomic_inc(&acct->nr_running);
300}
301
302static void io_wqe_dec_running(struct io_wqe *wqe, struct io_worker *worker)
303 __must_hold(wqe->lock)
304{
305 struct io_wqe_acct *acct = io_wqe_get_acct(wqe, worker);
306
307 if (atomic_dec_and_test(&acct->nr_running) && io_wqe_run_queue(wqe))
308 io_wqe_wake_worker(wqe, acct);
309}
310
Jens Axboe771b53d02019-10-22 10:25:58 -0600311static void io_worker_start(struct io_wqe *wqe, struct io_worker *worker)
312{
313 allow_kernel_signal(SIGINT);
314
315 current->flags |= PF_IO_WORKER;
316
317 worker->flags |= (IO_WORKER_F_UP | IO_WORKER_F_RUNNING);
Jens Axboefcb323c2019-10-24 12:39:47 -0600318 worker->restore_files = current->files;
Jens Axboe9392a272020-02-06 21:42:51 -0700319 worker->restore_fs = current->fs;
Jens Axboec5def4a2019-11-07 11:41:16 -0700320 io_wqe_inc_running(wqe, worker);
Jens Axboe771b53d02019-10-22 10:25:58 -0600321}
322
323/*
324 * Worker will start processing some work. Move it to the busy list, if
325 * it's currently on the freelist
326 */
327static void __io_worker_busy(struct io_wqe *wqe, struct io_worker *worker,
328 struct io_wq_work *work)
329 __must_hold(wqe->lock)
330{
Jens Axboec5def4a2019-11-07 11:41:16 -0700331 bool worker_bound, work_bound;
332
Jens Axboe771b53d02019-10-22 10:25:58 -0600333 if (worker->flags & IO_WORKER_F_FREE) {
334 worker->flags &= ~IO_WORKER_F_FREE;
335 hlist_nulls_del_init_rcu(&worker->nulls_node);
Jens Axboe771b53d02019-10-22 10:25:58 -0600336 }
Jens Axboec5def4a2019-11-07 11:41:16 -0700337
338 /*
339 * If worker is moving from bound to unbound (or vice versa), then
340 * ensure we update the running accounting.
341 */
Dan Carpenterb2e9c7d62019-11-19 09:22:16 +0300342 worker_bound = (worker->flags & IO_WORKER_F_BOUND) != 0;
343 work_bound = (work->flags & IO_WQ_WORK_UNBOUND) == 0;
344 if (worker_bound != work_bound) {
Jens Axboec5def4a2019-11-07 11:41:16 -0700345 io_wqe_dec_running(wqe, worker);
346 if (work_bound) {
347 worker->flags |= IO_WORKER_F_BOUND;
348 wqe->acct[IO_WQ_ACCT_UNBOUND].nr_workers--;
349 wqe->acct[IO_WQ_ACCT_BOUND].nr_workers++;
350 atomic_dec(&wqe->wq->user->processes);
351 } else {
352 worker->flags &= ~IO_WORKER_F_BOUND;
353 wqe->acct[IO_WQ_ACCT_UNBOUND].nr_workers++;
354 wqe->acct[IO_WQ_ACCT_BOUND].nr_workers--;
355 atomic_inc(&wqe->wq->user->processes);
356 }
357 io_wqe_inc_running(wqe, worker);
358 }
Jens Axboe771b53d02019-10-22 10:25:58 -0600359}
360
361/*
362 * No work, worker going to sleep. Move to freelist, and unuse mm if we
363 * have one attached. Dropping the mm may potentially sleep, so we drop
364 * the lock in that case and return success. Since the caller has to
365 * retry the loop in that case (we changed task state), we don't regrab
366 * the lock if we return success.
367 */
368static bool __io_worker_idle(struct io_wqe *wqe, struct io_worker *worker)
369 __must_hold(wqe->lock)
370{
371 if (!(worker->flags & IO_WORKER_F_FREE)) {
372 worker->flags |= IO_WORKER_F_FREE;
Jens Axboe021d1cd2019-11-14 08:00:41 -0700373 hlist_nulls_add_head_rcu(&worker->nulls_node, &wqe->free_list);
Jens Axboe771b53d02019-10-22 10:25:58 -0600374 }
375
376 return __io_worker_unuse(wqe, worker);
377}
378
379static struct io_wq_work *io_get_next_work(struct io_wqe *wqe, unsigned *hash)
380 __must_hold(wqe->lock)
381{
Jens Axboe6206f0e2019-11-26 11:59:32 -0700382 struct io_wq_work_node *node, *prev;
Jens Axboe771b53d02019-10-22 10:25:58 -0600383 struct io_wq_work *work;
384
Jens Axboe6206f0e2019-11-26 11:59:32 -0700385 wq_list_for_each(node, prev, &wqe->work_list) {
386 work = container_of(node, struct io_wq_work, list);
387
Jens Axboe771b53d02019-10-22 10:25:58 -0600388 /* not hashed, can run anytime */
389 if (!(work->flags & IO_WQ_WORK_HASHED)) {
Jens Axboe6206f0e2019-11-26 11:59:32 -0700390 wq_node_del(&wqe->work_list, node, prev);
Jens Axboe771b53d02019-10-22 10:25:58 -0600391 return work;
392 }
393
394 /* hashed, can run if not already running */
395 *hash = work->flags >> IO_WQ_HASH_SHIFT;
Pavel Begunkov3684f242020-02-28 10:36:39 +0300396 if (!(wqe->hash_map & BIT(*hash))) {
397 wqe->hash_map |= BIT(*hash);
Jens Axboe6206f0e2019-11-26 11:59:32 -0700398 wq_node_del(&wqe->work_list, node, prev);
Jens Axboe771b53d02019-10-22 10:25:58 -0600399 return work;
400 }
401 }
402
403 return NULL;
404}
405
Jens Axboecccf0ee2020-01-27 16:34:48 -0700406static void io_wq_switch_mm(struct io_worker *worker, struct io_wq_work *work)
407{
408 if (worker->mm) {
409 unuse_mm(worker->mm);
410 mmput(worker->mm);
411 worker->mm = NULL;
412 }
413 if (!work->mm) {
414 set_fs(KERNEL_DS);
415 return;
416 }
417 if (mmget_not_zero(work->mm)) {
418 use_mm(work->mm);
419 if (!worker->mm)
420 set_fs(USER_DS);
421 worker->mm = work->mm;
422 /* hang on to this mm */
423 work->mm = NULL;
424 return;
425 }
426
427 /* failed grabbing mm, ensure work gets cancelled */
428 work->flags |= IO_WQ_WORK_CANCEL;
429}
430
431static void io_wq_switch_creds(struct io_worker *worker,
432 struct io_wq_work *work)
433{
434 const struct cred *old_creds = override_creds(work->creds);
435
436 worker->cur_creds = work->creds;
437 if (worker->saved_creds)
438 put_cred(old_creds); /* creds set by previous switch */
439 else
440 worker->saved_creds = old_creds;
441}
442
Pavel Begunkovdc026a72020-03-04 16:14:09 +0300443static void io_impersonate_work(struct io_worker *worker,
444 struct io_wq_work *work)
445{
446 if (work->files && current->files != work->files) {
447 task_lock(current);
448 current->files = work->files;
449 task_unlock(current);
450 }
451 if (work->fs && current->fs != work->fs)
452 current->fs = work->fs;
453 if (work->mm != worker->mm)
454 io_wq_switch_mm(worker, work);
455 if (worker->cur_creds != work->creds)
456 io_wq_switch_creds(worker, work);
457}
458
459static void io_assign_current_work(struct io_worker *worker,
460 struct io_wq_work *work)
461{
462 /* flush pending signals before assigning new work */
463 if (signal_pending(current))
464 flush_signals(current);
465 cond_resched();
466
467 spin_lock_irq(&worker->lock);
468 worker->cur_work = work;
469 spin_unlock_irq(&worker->lock);
470}
471
Jens Axboe771b53d02019-10-22 10:25:58 -0600472static void io_worker_handle_work(struct io_worker *worker)
473 __releases(wqe->lock)
474{
Jens Axboe771b53d02019-10-22 10:25:58 -0600475 struct io_wqe *wqe = worker->wqe;
476 struct io_wq *wq = wqe->wq;
477
478 do {
Pavel Begunkov58e39312020-03-04 16:14:10 +0300479 struct io_wq_work *work;
Jens Axboe771b53d02019-10-22 10:25:58 -0600480 unsigned hash = -1U;
481
482 /*
Jens Axboe771b53d02019-10-22 10:25:58 -0600483 * If we got some work, mark us as busy. If we didn't, but
484 * the list isn't empty, it means we stalled on hashed work.
485 * Mark us stalled so we don't keep looking for work when we
486 * can't make progress, any work completion or insertion will
487 * clear the stalled flag.
488 */
489 work = io_get_next_work(wqe, &hash);
490 if (work)
491 __io_worker_busy(wqe, worker, work);
Jens Axboe6206f0e2019-11-26 11:59:32 -0700492 else if (!wq_list_empty(&wqe->work_list))
Jens Axboe771b53d02019-10-22 10:25:58 -0600493 wqe->flags |= IO_WQE_FLAG_STALLED;
494
495 spin_unlock_irq(&wqe->lock);
496 if (!work)
497 break;
Pavel Begunkov58e39312020-03-04 16:14:10 +0300498 io_assign_current_work(worker, work);
Jens Axboe36c2f922019-11-13 09:43:34 -0700499
Pavel Begunkovdc026a72020-03-04 16:14:09 +0300500 /* handle a whole dependent link */
501 do {
Pavel Begunkov58e39312020-03-04 16:14:10 +0300502 struct io_wq_work *old_work;
Hillf Dantonfd1c4bc2019-12-24 09:14:29 -0700503
Pavel Begunkov58e39312020-03-04 16:14:10 +0300504 io_impersonate_work(worker, work);
Pavel Begunkovdc026a72020-03-04 16:14:09 +0300505 /*
506 * OK to set IO_WQ_WORK_CANCEL even for uncancellable
507 * work, the worker function will do the right thing.
508 */
509 if (test_bit(IO_WQ_BIT_CANCEL, &wq->state))
510 work->flags |= IO_WQ_WORK_CANCEL;
Jens Axboe36c2f922019-11-13 09:43:34 -0700511
Pavel Begunkovdc026a72020-03-04 16:14:09 +0300512 if (wq->get_work)
513 wq->get_work(work);
Jens Axboe771b53d02019-10-22 10:25:58 -0600514
Pavel Begunkovdc026a72020-03-04 16:14:09 +0300515 old_work = work;
516 work->func(&work);
Pavel Begunkov58e39312020-03-04 16:14:10 +0300517 work = (old_work == work) ? NULL : work;
518 io_assign_current_work(worker, work);
Jens Axboe771b53d02019-10-22 10:25:58 -0600519
Pavel Begunkovdc026a72020-03-04 16:14:09 +0300520 if (wq->put_work)
521 wq->put_work(old_work);
522
523 if (hash != -1U) {
524 spin_lock_irq(&wqe->lock);
525 wqe->hash_map &= ~BIT_ULL(hash);
526 wqe->flags &= ~IO_WQE_FLAG_STALLED;
527 spin_unlock_irq(&wqe->lock);
528 /* dependent work is not hashed */
529 hash = -1U;
530 }
Pavel Begunkov58e39312020-03-04 16:14:10 +0300531 } while (work);
Jens Axboe36c2f922019-11-13 09:43:34 -0700532
533 spin_lock_irq(&wqe->lock);
Jens Axboe771b53d02019-10-22 10:25:58 -0600534 } while (1);
535}
536
Jens Axboe771b53d02019-10-22 10:25:58 -0600537static int io_wqe_worker(void *data)
538{
539 struct io_worker *worker = data;
540 struct io_wqe *wqe = worker->wqe;
541 struct io_wq *wq = wqe->wq;
Jens Axboe771b53d02019-10-22 10:25:58 -0600542
543 io_worker_start(wqe, worker);
544
545 while (!test_bit(IO_WQ_BIT_EXIT, &wq->state)) {
Jens Axboe506d95f2019-12-07 21:03:59 -0700546 set_current_state(TASK_INTERRUPTIBLE);
Jens Axboee995d512019-12-07 21:06:46 -0700547loop:
Jens Axboe771b53d02019-10-22 10:25:58 -0600548 spin_lock_irq(&wqe->lock);
549 if (io_wqe_run_queue(wqe)) {
550 __set_current_state(TASK_RUNNING);
551 io_worker_handle_work(worker);
Jens Axboee995d512019-12-07 21:06:46 -0700552 goto loop;
Jens Axboe771b53d02019-10-22 10:25:58 -0600553 }
554 /* drops the lock on success, retry */
555 if (__io_worker_idle(wqe, worker)) {
556 __release(&wqe->lock);
Jens Axboee995d512019-12-07 21:06:46 -0700557 goto loop;
Jens Axboe771b53d02019-10-22 10:25:58 -0600558 }
559 spin_unlock_irq(&wqe->lock);
560 if (signal_pending(current))
561 flush_signals(current);
562 if (schedule_timeout(WORKER_IDLE_TIMEOUT))
563 continue;
564 /* timed out, exit unless we're the fixed worker */
565 if (test_bit(IO_WQ_BIT_EXIT, &wq->state) ||
566 !(worker->flags & IO_WORKER_F_FIXED))
567 break;
568 }
569
Jens Axboe771b53d02019-10-22 10:25:58 -0600570 if (test_bit(IO_WQ_BIT_EXIT, &wq->state)) {
571 spin_lock_irq(&wqe->lock);
Jens Axboe6206f0e2019-11-26 11:59:32 -0700572 if (!wq_list_empty(&wqe->work_list))
Jens Axboe771b53d02019-10-22 10:25:58 -0600573 io_worker_handle_work(worker);
574 else
575 spin_unlock_irq(&wqe->lock);
576 }
577
578 io_worker_exit(worker);
579 return 0;
580}
581
582/*
Jens Axboe771b53d02019-10-22 10:25:58 -0600583 * Called when a worker is scheduled in. Mark us as currently running.
584 */
585void io_wq_worker_running(struct task_struct *tsk)
586{
587 struct io_worker *worker = kthread_data(tsk);
588 struct io_wqe *wqe = worker->wqe;
589
590 if (!(worker->flags & IO_WORKER_F_UP))
591 return;
592 if (worker->flags & IO_WORKER_F_RUNNING)
593 return;
594 worker->flags |= IO_WORKER_F_RUNNING;
Jens Axboec5def4a2019-11-07 11:41:16 -0700595 io_wqe_inc_running(wqe, worker);
Jens Axboe771b53d02019-10-22 10:25:58 -0600596}
597
598/*
599 * Called when worker is going to sleep. If there are no workers currently
600 * running and we have work pending, wake up a free one or have the manager
601 * set one up.
602 */
603void io_wq_worker_sleeping(struct task_struct *tsk)
604{
605 struct io_worker *worker = kthread_data(tsk);
606 struct io_wqe *wqe = worker->wqe;
607
608 if (!(worker->flags & IO_WORKER_F_UP))
609 return;
610 if (!(worker->flags & IO_WORKER_F_RUNNING))
611 return;
612
613 worker->flags &= ~IO_WORKER_F_RUNNING;
614
615 spin_lock_irq(&wqe->lock);
Jens Axboec5def4a2019-11-07 11:41:16 -0700616 io_wqe_dec_running(wqe, worker);
Jens Axboe771b53d02019-10-22 10:25:58 -0600617 spin_unlock_irq(&wqe->lock);
618}
619
Jens Axboeb60fda62019-11-19 08:37:07 -0700620static bool create_io_worker(struct io_wq *wq, struct io_wqe *wqe, int index)
Jens Axboe771b53d02019-10-22 10:25:58 -0600621{
Jens Axboec5def4a2019-11-07 11:41:16 -0700622 struct io_wqe_acct *acct =&wqe->acct[index];
Jens Axboe771b53d02019-10-22 10:25:58 -0600623 struct io_worker *worker;
624
Jann Hornad6e0052019-11-26 17:39:45 +0100625 worker = kzalloc_node(sizeof(*worker), GFP_KERNEL, wqe->node);
Jens Axboe771b53d02019-10-22 10:25:58 -0600626 if (!worker)
Jens Axboeb60fda62019-11-19 08:37:07 -0700627 return false;
Jens Axboe771b53d02019-10-22 10:25:58 -0600628
629 refcount_set(&worker->ref, 1);
630 worker->nulls_node.pprev = NULL;
Jens Axboe771b53d02019-10-22 10:25:58 -0600631 worker->wqe = wqe;
Jens Axboe36c2f922019-11-13 09:43:34 -0700632 spin_lock_init(&worker->lock);
Jens Axboe771b53d02019-10-22 10:25:58 -0600633
634 worker->task = kthread_create_on_node(io_wqe_worker, worker, wqe->node,
Jens Axboec5def4a2019-11-07 11:41:16 -0700635 "io_wqe_worker-%d/%d", index, wqe->node);
Jens Axboe771b53d02019-10-22 10:25:58 -0600636 if (IS_ERR(worker->task)) {
637 kfree(worker);
Jens Axboeb60fda62019-11-19 08:37:07 -0700638 return false;
Jens Axboe771b53d02019-10-22 10:25:58 -0600639 }
640
641 spin_lock_irq(&wqe->lock);
Jens Axboe021d1cd2019-11-14 08:00:41 -0700642 hlist_nulls_add_head_rcu(&worker->nulls_node, &wqe->free_list);
Jens Axboee61df662019-11-13 13:54:49 -0700643 list_add_tail_rcu(&worker->all_list, &wqe->all_list);
Jens Axboe771b53d02019-10-22 10:25:58 -0600644 worker->flags |= IO_WORKER_F_FREE;
Jens Axboec5def4a2019-11-07 11:41:16 -0700645 if (index == IO_WQ_ACCT_BOUND)
646 worker->flags |= IO_WORKER_F_BOUND;
647 if (!acct->nr_workers && (worker->flags & IO_WORKER_F_BOUND))
Jens Axboe771b53d02019-10-22 10:25:58 -0600648 worker->flags |= IO_WORKER_F_FIXED;
Jens Axboec5def4a2019-11-07 11:41:16 -0700649 acct->nr_workers++;
Jens Axboe771b53d02019-10-22 10:25:58 -0600650 spin_unlock_irq(&wqe->lock);
651
Jens Axboec5def4a2019-11-07 11:41:16 -0700652 if (index == IO_WQ_ACCT_UNBOUND)
653 atomic_inc(&wq->user->processes);
654
Jens Axboe771b53d02019-10-22 10:25:58 -0600655 wake_up_process(worker->task);
Jens Axboeb60fda62019-11-19 08:37:07 -0700656 return true;
Jens Axboe771b53d02019-10-22 10:25:58 -0600657}
658
Jens Axboec5def4a2019-11-07 11:41:16 -0700659static inline bool io_wqe_need_worker(struct io_wqe *wqe, int index)
Jens Axboe771b53d02019-10-22 10:25:58 -0600660 __must_hold(wqe->lock)
661{
Jens Axboec5def4a2019-11-07 11:41:16 -0700662 struct io_wqe_acct *acct = &wqe->acct[index];
Jens Axboe771b53d02019-10-22 10:25:58 -0600663
Jens Axboec5def4a2019-11-07 11:41:16 -0700664 /* if we have available workers or no work, no need */
Jens Axboe021d1cd2019-11-14 08:00:41 -0700665 if (!hlist_nulls_empty(&wqe->free_list) || !io_wqe_run_queue(wqe))
Jens Axboec5def4a2019-11-07 11:41:16 -0700666 return false;
667 return acct->nr_workers < acct->max_workers;
Jens Axboe771b53d02019-10-22 10:25:58 -0600668}
669
670/*
671 * Manager thread. Tasked with creating new workers, if we need them.
672 */
673static int io_wq_manager(void *data)
674{
675 struct io_wq *wq = data;
Jann Horn3fc50ab2019-11-26 19:10:20 +0100676 int workers_to_create = num_possible_nodes();
677 int node;
Jens Axboeb60fda62019-11-19 08:37:07 -0700678
679 /* create fixed workers */
Jann Horn3fc50ab2019-11-26 19:10:20 +0100680 refcount_set(&wq->refs, workers_to_create);
681 for_each_node(node) {
Jens Axboe75634392020-02-11 06:30:06 -0700682 if (!node_online(node))
683 continue;
Jann Horn3fc50ab2019-11-26 19:10:20 +0100684 if (!create_io_worker(wq, wq->wqes[node], IO_WQ_ACCT_BOUND))
685 goto err;
686 workers_to_create--;
Jens Axboeb60fda62019-11-19 08:37:07 -0700687 }
688
Jens Axboe75634392020-02-11 06:30:06 -0700689 while (workers_to_create--)
690 refcount_dec(&wq->refs);
691
Jens Axboeb60fda62019-11-19 08:37:07 -0700692 complete(&wq->done);
Jens Axboe771b53d02019-10-22 10:25:58 -0600693
694 while (!kthread_should_stop()) {
Jann Horn3fc50ab2019-11-26 19:10:20 +0100695 for_each_node(node) {
696 struct io_wqe *wqe = wq->wqes[node];
Jens Axboec5def4a2019-11-07 11:41:16 -0700697 bool fork_worker[2] = { false, false };
Jens Axboe771b53d02019-10-22 10:25:58 -0600698
Jens Axboe75634392020-02-11 06:30:06 -0700699 if (!node_online(node))
700 continue;
701
Jens Axboe771b53d02019-10-22 10:25:58 -0600702 spin_lock_irq(&wqe->lock);
Jens Axboec5def4a2019-11-07 11:41:16 -0700703 if (io_wqe_need_worker(wqe, IO_WQ_ACCT_BOUND))
704 fork_worker[IO_WQ_ACCT_BOUND] = true;
705 if (io_wqe_need_worker(wqe, IO_WQ_ACCT_UNBOUND))
706 fork_worker[IO_WQ_ACCT_UNBOUND] = true;
Jens Axboe771b53d02019-10-22 10:25:58 -0600707 spin_unlock_irq(&wqe->lock);
Jens Axboec5def4a2019-11-07 11:41:16 -0700708 if (fork_worker[IO_WQ_ACCT_BOUND])
709 create_io_worker(wq, wqe, IO_WQ_ACCT_BOUND);
710 if (fork_worker[IO_WQ_ACCT_UNBOUND])
711 create_io_worker(wq, wqe, IO_WQ_ACCT_UNBOUND);
Jens Axboe771b53d02019-10-22 10:25:58 -0600712 }
713 set_current_state(TASK_INTERRUPTIBLE);
714 schedule_timeout(HZ);
715 }
716
717 return 0;
Jens Axboeb60fda62019-11-19 08:37:07 -0700718err:
719 set_bit(IO_WQ_BIT_ERROR, &wq->state);
720 set_bit(IO_WQ_BIT_EXIT, &wq->state);
Jann Horn3fc50ab2019-11-26 19:10:20 +0100721 if (refcount_sub_and_test(workers_to_create, &wq->refs))
Jens Axboeb60fda62019-11-19 08:37:07 -0700722 complete(&wq->done);
723 return 0;
Jens Axboe771b53d02019-10-22 10:25:58 -0600724}
725
Jens Axboec5def4a2019-11-07 11:41:16 -0700726static bool io_wq_can_queue(struct io_wqe *wqe, struct io_wqe_acct *acct,
727 struct io_wq_work *work)
728{
729 bool free_worker;
730
731 if (!(work->flags & IO_WQ_WORK_UNBOUND))
732 return true;
733 if (atomic_read(&acct->nr_running))
734 return true;
735
736 rcu_read_lock();
Jens Axboe021d1cd2019-11-14 08:00:41 -0700737 free_worker = !hlist_nulls_empty(&wqe->free_list);
Jens Axboec5def4a2019-11-07 11:41:16 -0700738 rcu_read_unlock();
739 if (free_worker)
740 return true;
741
742 if (atomic_read(&wqe->wq->user->processes) >= acct->max_workers &&
743 !(capable(CAP_SYS_RESOURCE) || capable(CAP_SYS_ADMIN)))
744 return false;
745
746 return true;
747}
748
Pavel Begunkovfc04c392020-03-01 19:18:19 +0300749static void io_run_cancel(struct io_wq_work *work)
750{
751 do {
752 struct io_wq_work *old_work = work;
753
754 work->flags |= IO_WQ_WORK_CANCEL;
755 work->func(&work);
756 work = (work == old_work) ? NULL : work;
757 } while (work);
758}
759
Jens Axboe771b53d02019-10-22 10:25:58 -0600760static void io_wqe_enqueue(struct io_wqe *wqe, struct io_wq_work *work)
761{
Jens Axboec5def4a2019-11-07 11:41:16 -0700762 struct io_wqe_acct *acct = io_work_get_acct(wqe, work);
Jens Axboe895e2ca2019-12-17 08:46:33 -0700763 int work_flags;
Jens Axboe771b53d02019-10-22 10:25:58 -0600764 unsigned long flags;
765
Jens Axboec5def4a2019-11-07 11:41:16 -0700766 /*
767 * Do early check to see if we need a new unbound worker, and if we do,
768 * if we're allowed to do so. This isn't 100% accurate as there's a
769 * gap between this check and incrementing the value, but that's OK.
770 * It's close enough to not be an issue, fork() has the same delay.
771 */
772 if (unlikely(!io_wq_can_queue(wqe, acct, work))) {
Pavel Begunkovfc04c392020-03-01 19:18:19 +0300773 io_run_cancel(work);
Jens Axboec5def4a2019-11-07 11:41:16 -0700774 return;
775 }
776
Jens Axboe895e2ca2019-12-17 08:46:33 -0700777 work_flags = work->flags;
Jens Axboe771b53d02019-10-22 10:25:58 -0600778 spin_lock_irqsave(&wqe->lock, flags);
Jens Axboe6206f0e2019-11-26 11:59:32 -0700779 wq_list_add_tail(&work->list, &wqe->work_list);
Jens Axboe771b53d02019-10-22 10:25:58 -0600780 wqe->flags &= ~IO_WQE_FLAG_STALLED;
781 spin_unlock_irqrestore(&wqe->lock, flags);
782
Jens Axboe895e2ca2019-12-17 08:46:33 -0700783 if ((work_flags & IO_WQ_WORK_CONCURRENT) ||
784 !atomic_read(&acct->nr_running))
Jens Axboec5def4a2019-11-07 11:41:16 -0700785 io_wqe_wake_worker(wqe, acct);
Jens Axboe771b53d02019-10-22 10:25:58 -0600786}
787
788void io_wq_enqueue(struct io_wq *wq, struct io_wq_work *work)
789{
790 struct io_wqe *wqe = wq->wqes[numa_node_id()];
791
792 io_wqe_enqueue(wqe, work);
793}
794
795/*
796 * Enqueue work, hashed by some key. Work items that hash to the same value
797 * will not be done in parallel. Used to limit concurrent writes, generally
798 * hashed by inode.
799 */
800void io_wq_enqueue_hashed(struct io_wq *wq, struct io_wq_work *work, void *val)
801{
802 struct io_wqe *wqe = wq->wqes[numa_node_id()];
803 unsigned bit;
804
805
806 bit = hash_ptr(val, IO_WQ_HASH_ORDER);
807 work->flags |= (IO_WQ_WORK_HASHED | (bit << IO_WQ_HASH_SHIFT));
808 io_wqe_enqueue(wqe, work);
809}
810
811static bool io_wqe_worker_send_sig(struct io_worker *worker, void *data)
812{
813 send_sig(SIGINT, worker->task, 1);
814 return false;
815}
816
817/*
818 * Iterate the passed in list and call the specific function for each
819 * worker that isn't exiting
820 */
821static bool io_wq_for_each_worker(struct io_wqe *wqe,
Jens Axboe771b53d02019-10-22 10:25:58 -0600822 bool (*func)(struct io_worker *, void *),
823 void *data)
824{
Jens Axboe771b53d02019-10-22 10:25:58 -0600825 struct io_worker *worker;
826 bool ret = false;
827
Jens Axboee61df662019-11-13 13:54:49 -0700828 list_for_each_entry_rcu(worker, &wqe->all_list, all_list) {
Jens Axboe771b53d02019-10-22 10:25:58 -0600829 if (io_worker_get(worker)) {
Jens Axboe75634392020-02-11 06:30:06 -0700830 /* no task if node is/was offline */
831 if (worker->task)
832 ret = func(worker, data);
Jens Axboe771b53d02019-10-22 10:25:58 -0600833 io_worker_release(worker);
834 if (ret)
835 break;
836 }
837 }
Jens Axboee61df662019-11-13 13:54:49 -0700838
Jens Axboe771b53d02019-10-22 10:25:58 -0600839 return ret;
840}
841
842void io_wq_cancel_all(struct io_wq *wq)
843{
Jann Horn3fc50ab2019-11-26 19:10:20 +0100844 int node;
Jens Axboe771b53d02019-10-22 10:25:58 -0600845
846 set_bit(IO_WQ_BIT_CANCEL, &wq->state);
847
Jens Axboe771b53d02019-10-22 10:25:58 -0600848 rcu_read_lock();
Jann Horn3fc50ab2019-11-26 19:10:20 +0100849 for_each_node(node) {
850 struct io_wqe *wqe = wq->wqes[node];
Jens Axboe771b53d02019-10-22 10:25:58 -0600851
Jens Axboee61df662019-11-13 13:54:49 -0700852 io_wq_for_each_worker(wqe, io_wqe_worker_send_sig, NULL);
Jens Axboe771b53d02019-10-22 10:25:58 -0600853 }
854 rcu_read_unlock();
855}
856
Jens Axboe62755e32019-10-28 21:49:21 -0600857struct io_cb_cancel_data {
858 struct io_wqe *wqe;
859 work_cancel_fn *cancel;
860 void *caller_data;
861};
862
863static bool io_work_cancel(struct io_worker *worker, void *cancel_data)
864{
865 struct io_cb_cancel_data *data = cancel_data;
Jens Axboe6f726532019-11-05 13:51:51 -0700866 unsigned long flags;
Jens Axboe62755e32019-10-28 21:49:21 -0600867 bool ret = false;
868
869 /*
870 * Hold the lock to avoid ->cur_work going out of scope, caller
Jens Axboe36c2f922019-11-13 09:43:34 -0700871 * may dereference the passed in work.
Jens Axboe62755e32019-10-28 21:49:21 -0600872 */
Jens Axboe36c2f922019-11-13 09:43:34 -0700873 spin_lock_irqsave(&worker->lock, flags);
Jens Axboe62755e32019-10-28 21:49:21 -0600874 if (worker->cur_work &&
Jens Axboe0c9d5cc2019-12-11 19:29:43 -0700875 !(worker->cur_work->flags & IO_WQ_WORK_NO_CANCEL) &&
Jens Axboe62755e32019-10-28 21:49:21 -0600876 data->cancel(worker->cur_work, data->caller_data)) {
877 send_sig(SIGINT, worker->task, 1);
878 ret = true;
879 }
Jens Axboe36c2f922019-11-13 09:43:34 -0700880 spin_unlock_irqrestore(&worker->lock, flags);
Jens Axboe62755e32019-10-28 21:49:21 -0600881
882 return ret;
883}
884
885static enum io_wq_cancel io_wqe_cancel_cb_work(struct io_wqe *wqe,
886 work_cancel_fn *cancel,
887 void *cancel_data)
888{
889 struct io_cb_cancel_data data = {
890 .wqe = wqe,
891 .cancel = cancel,
892 .caller_data = cancel_data,
893 };
Jens Axboe6206f0e2019-11-26 11:59:32 -0700894 struct io_wq_work_node *node, *prev;
Jens Axboe62755e32019-10-28 21:49:21 -0600895 struct io_wq_work *work;
Jens Axboe6f726532019-11-05 13:51:51 -0700896 unsigned long flags;
Jens Axboe62755e32019-10-28 21:49:21 -0600897 bool found = false;
898
Jens Axboe6f726532019-11-05 13:51:51 -0700899 spin_lock_irqsave(&wqe->lock, flags);
Jens Axboe6206f0e2019-11-26 11:59:32 -0700900 wq_list_for_each(node, prev, &wqe->work_list) {
901 work = container_of(node, struct io_wq_work, list);
902
Jens Axboe62755e32019-10-28 21:49:21 -0600903 if (cancel(work, cancel_data)) {
Jens Axboe6206f0e2019-11-26 11:59:32 -0700904 wq_node_del(&wqe->work_list, node, prev);
Jens Axboe62755e32019-10-28 21:49:21 -0600905 found = true;
906 break;
907 }
908 }
Jens Axboe6f726532019-11-05 13:51:51 -0700909 spin_unlock_irqrestore(&wqe->lock, flags);
Jens Axboe62755e32019-10-28 21:49:21 -0600910
911 if (found) {
Pavel Begunkovfc04c392020-03-01 19:18:19 +0300912 io_run_cancel(work);
Jens Axboe62755e32019-10-28 21:49:21 -0600913 return IO_WQ_CANCEL_OK;
914 }
915
916 rcu_read_lock();
Jens Axboee61df662019-11-13 13:54:49 -0700917 found = io_wq_for_each_worker(wqe, io_work_cancel, &data);
Jens Axboe62755e32019-10-28 21:49:21 -0600918 rcu_read_unlock();
919 return found ? IO_WQ_CANCEL_RUNNING : IO_WQ_CANCEL_NOTFOUND;
920}
921
922enum io_wq_cancel io_wq_cancel_cb(struct io_wq *wq, work_cancel_fn *cancel,
923 void *data)
924{
925 enum io_wq_cancel ret = IO_WQ_CANCEL_NOTFOUND;
Jann Horn3fc50ab2019-11-26 19:10:20 +0100926 int node;
Jens Axboe62755e32019-10-28 21:49:21 -0600927
Jann Horn3fc50ab2019-11-26 19:10:20 +0100928 for_each_node(node) {
929 struct io_wqe *wqe = wq->wqes[node];
Jens Axboe62755e32019-10-28 21:49:21 -0600930
931 ret = io_wqe_cancel_cb_work(wqe, cancel, data);
932 if (ret != IO_WQ_CANCEL_NOTFOUND)
933 break;
934 }
935
936 return ret;
937}
938
Jens Axboe00bcda12020-02-08 19:13:32 -0700939struct work_match {
940 bool (*fn)(struct io_wq_work *, void *data);
941 void *data;
942};
943
Jens Axboe771b53d02019-10-22 10:25:58 -0600944static bool io_wq_worker_cancel(struct io_worker *worker, void *data)
945{
Jens Axboe00bcda12020-02-08 19:13:32 -0700946 struct work_match *match = data;
Jens Axboe36c2f922019-11-13 09:43:34 -0700947 unsigned long flags;
948 bool ret = false;
Jens Axboe771b53d02019-10-22 10:25:58 -0600949
Jens Axboe36c2f922019-11-13 09:43:34 -0700950 spin_lock_irqsave(&worker->lock, flags);
Jens Axboe00bcda12020-02-08 19:13:32 -0700951 if (match->fn(worker->cur_work, match->data) &&
Jens Axboe0c9d5cc2019-12-11 19:29:43 -0700952 !(worker->cur_work->flags & IO_WQ_WORK_NO_CANCEL)) {
Jens Axboe771b53d02019-10-22 10:25:58 -0600953 send_sig(SIGINT, worker->task, 1);
Jens Axboe36c2f922019-11-13 09:43:34 -0700954 ret = true;
Jens Axboe771b53d02019-10-22 10:25:58 -0600955 }
Jens Axboe36c2f922019-11-13 09:43:34 -0700956 spin_unlock_irqrestore(&worker->lock, flags);
Jens Axboe771b53d02019-10-22 10:25:58 -0600957
Jens Axboe36c2f922019-11-13 09:43:34 -0700958 return ret;
Jens Axboe771b53d02019-10-22 10:25:58 -0600959}
960
961static enum io_wq_cancel io_wqe_cancel_work(struct io_wqe *wqe,
Jens Axboe00bcda12020-02-08 19:13:32 -0700962 struct work_match *match)
Jens Axboe771b53d02019-10-22 10:25:58 -0600963{
Jens Axboe6206f0e2019-11-26 11:59:32 -0700964 struct io_wq_work_node *node, *prev;
Jens Axboe771b53d02019-10-22 10:25:58 -0600965 struct io_wq_work *work;
Jens Axboe6f726532019-11-05 13:51:51 -0700966 unsigned long flags;
Jens Axboe771b53d02019-10-22 10:25:58 -0600967 bool found = false;
968
Jens Axboe771b53d02019-10-22 10:25:58 -0600969 /*
970 * First check pending list, if we're lucky we can just remove it
971 * from there. CANCEL_OK means that the work is returned as-new,
972 * no completion will be posted for it.
973 */
Jens Axboe6f726532019-11-05 13:51:51 -0700974 spin_lock_irqsave(&wqe->lock, flags);
Jens Axboe6206f0e2019-11-26 11:59:32 -0700975 wq_list_for_each(node, prev, &wqe->work_list) {
976 work = container_of(node, struct io_wq_work, list);
977
Jens Axboe00bcda12020-02-08 19:13:32 -0700978 if (match->fn(work, match->data)) {
Jens Axboe6206f0e2019-11-26 11:59:32 -0700979 wq_node_del(&wqe->work_list, node, prev);
Jens Axboe771b53d02019-10-22 10:25:58 -0600980 found = true;
981 break;
982 }
983 }
Jens Axboe6f726532019-11-05 13:51:51 -0700984 spin_unlock_irqrestore(&wqe->lock, flags);
Jens Axboe771b53d02019-10-22 10:25:58 -0600985
986 if (found) {
Pavel Begunkovfc04c392020-03-01 19:18:19 +0300987 io_run_cancel(work);
Jens Axboe771b53d02019-10-22 10:25:58 -0600988 return IO_WQ_CANCEL_OK;
989 }
990
991 /*
992 * Now check if a free (going busy) or busy worker has the work
993 * currently running. If we find it there, we'll return CANCEL_RUNNING
Brian Gianforcarod195a662019-12-13 03:09:50 -0800994 * as an indication that we attempt to signal cancellation. The
Jens Axboe771b53d02019-10-22 10:25:58 -0600995 * completion will run normally in this case.
996 */
997 rcu_read_lock();
Jens Axboe00bcda12020-02-08 19:13:32 -0700998 found = io_wq_for_each_worker(wqe, io_wq_worker_cancel, match);
Jens Axboe771b53d02019-10-22 10:25:58 -0600999 rcu_read_unlock();
1000 return found ? IO_WQ_CANCEL_RUNNING : IO_WQ_CANCEL_NOTFOUND;
1001}
1002
Jens Axboe00bcda12020-02-08 19:13:32 -07001003static bool io_wq_work_match(struct io_wq_work *work, void *data)
1004{
1005 return work == data;
1006}
1007
Jens Axboe771b53d02019-10-22 10:25:58 -06001008enum io_wq_cancel io_wq_cancel_work(struct io_wq *wq, struct io_wq_work *cwork)
1009{
Jens Axboe00bcda12020-02-08 19:13:32 -07001010 struct work_match match = {
1011 .fn = io_wq_work_match,
1012 .data = cwork
1013 };
Jens Axboe771b53d02019-10-22 10:25:58 -06001014 enum io_wq_cancel ret = IO_WQ_CANCEL_NOTFOUND;
Jann Horn3fc50ab2019-11-26 19:10:20 +01001015 int node;
Jens Axboe771b53d02019-10-22 10:25:58 -06001016
Jens Axboe00bcda12020-02-08 19:13:32 -07001017 cwork->flags |= IO_WQ_WORK_CANCEL;
1018
Jann Horn3fc50ab2019-11-26 19:10:20 +01001019 for_each_node(node) {
1020 struct io_wqe *wqe = wq->wqes[node];
Jens Axboe771b53d02019-10-22 10:25:58 -06001021
Jens Axboe00bcda12020-02-08 19:13:32 -07001022 ret = io_wqe_cancel_work(wqe, &match);
Jens Axboe771b53d02019-10-22 10:25:58 -06001023 if (ret != IO_WQ_CANCEL_NOTFOUND)
1024 break;
1025 }
1026
1027 return ret;
1028}
1029
Jens Axboe36282882020-02-08 19:16:39 -07001030static bool io_wq_pid_match(struct io_wq_work *work, void *data)
1031{
1032 pid_t pid = (pid_t) (unsigned long) data;
1033
1034 if (work)
1035 return work->task_pid == pid;
1036 return false;
1037}
1038
1039enum io_wq_cancel io_wq_cancel_pid(struct io_wq *wq, pid_t pid)
1040{
1041 struct work_match match = {
1042 .fn = io_wq_pid_match,
1043 .data = (void *) (unsigned long) pid
1044 };
1045 enum io_wq_cancel ret = IO_WQ_CANCEL_NOTFOUND;
1046 int node;
1047
1048 for_each_node(node) {
1049 struct io_wqe *wqe = wq->wqes[node];
1050
1051 ret = io_wqe_cancel_work(wqe, &match);
1052 if (ret != IO_WQ_CANCEL_NOTFOUND)
1053 break;
1054 }
1055
1056 return ret;
1057}
1058
Jens Axboe576a3472019-11-25 08:49:20 -07001059struct io_wq *io_wq_create(unsigned bounded, struct io_wq_data *data)
Jens Axboe771b53d02019-10-22 10:25:58 -06001060{
Jann Horn3fc50ab2019-11-26 19:10:20 +01001061 int ret = -ENOMEM, node;
Jens Axboe771b53d02019-10-22 10:25:58 -06001062 struct io_wq *wq;
1063
Jann Hornad6e0052019-11-26 17:39:45 +01001064 wq = kzalloc(sizeof(*wq), GFP_KERNEL);
Jens Axboe771b53d02019-10-22 10:25:58 -06001065 if (!wq)
1066 return ERR_PTR(-ENOMEM);
1067
Jann Horn3fc50ab2019-11-26 19:10:20 +01001068 wq->wqes = kcalloc(nr_node_ids, sizeof(struct io_wqe *), GFP_KERNEL);
Jens Axboe771b53d02019-10-22 10:25:58 -06001069 if (!wq->wqes) {
1070 kfree(wq);
1071 return ERR_PTR(-ENOMEM);
1072 }
1073
Jens Axboe576a3472019-11-25 08:49:20 -07001074 wq->get_work = data->get_work;
1075 wq->put_work = data->put_work;
Jens Axboe7d723062019-11-12 22:31:31 -07001076
Jens Axboec5def4a2019-11-07 11:41:16 -07001077 /* caller must already hold a reference to this */
Jens Axboe576a3472019-11-25 08:49:20 -07001078 wq->user = data->user;
Jens Axboec5def4a2019-11-07 11:41:16 -07001079
Jann Horn3fc50ab2019-11-26 19:10:20 +01001080 for_each_node(node) {
Jens Axboe771b53d02019-10-22 10:25:58 -06001081 struct io_wqe *wqe;
Jens Axboe75634392020-02-11 06:30:06 -07001082 int alloc_node = node;
Jens Axboe771b53d02019-10-22 10:25:58 -06001083
Jens Axboe75634392020-02-11 06:30:06 -07001084 if (!node_online(alloc_node))
1085 alloc_node = NUMA_NO_NODE;
1086 wqe = kzalloc_node(sizeof(struct io_wqe), GFP_KERNEL, alloc_node);
Jens Axboe771b53d02019-10-22 10:25:58 -06001087 if (!wqe)
Jann Horn3fc50ab2019-11-26 19:10:20 +01001088 goto err;
1089 wq->wqes[node] = wqe;
Jens Axboe75634392020-02-11 06:30:06 -07001090 wqe->node = alloc_node;
Jens Axboec5def4a2019-11-07 11:41:16 -07001091 wqe->acct[IO_WQ_ACCT_BOUND].max_workers = bounded;
1092 atomic_set(&wqe->acct[IO_WQ_ACCT_BOUND].nr_running, 0);
Jens Axboe576a3472019-11-25 08:49:20 -07001093 if (wq->user) {
Jens Axboec5def4a2019-11-07 11:41:16 -07001094 wqe->acct[IO_WQ_ACCT_UNBOUND].max_workers =
1095 task_rlimit(current, RLIMIT_NPROC);
1096 }
1097 atomic_set(&wqe->acct[IO_WQ_ACCT_UNBOUND].nr_running, 0);
Jens Axboe771b53d02019-10-22 10:25:58 -06001098 wqe->wq = wq;
1099 spin_lock_init(&wqe->lock);
Jens Axboe6206f0e2019-11-26 11:59:32 -07001100 INIT_WQ_LIST(&wqe->work_list);
Jens Axboe021d1cd2019-11-14 08:00:41 -07001101 INIT_HLIST_NULLS_HEAD(&wqe->free_list, 0);
Jens Axboee61df662019-11-13 13:54:49 -07001102 INIT_LIST_HEAD(&wqe->all_list);
Jens Axboe771b53d02019-10-22 10:25:58 -06001103 }
1104
1105 init_completion(&wq->done);
1106
Jens Axboe771b53d02019-10-22 10:25:58 -06001107 wq->manager = kthread_create(io_wq_manager, wq, "io_wq_manager");
1108 if (!IS_ERR(wq->manager)) {
1109 wake_up_process(wq->manager);
Jens Axboeb60fda62019-11-19 08:37:07 -07001110 wait_for_completion(&wq->done);
1111 if (test_bit(IO_WQ_BIT_ERROR, &wq->state)) {
1112 ret = -ENOMEM;
1113 goto err;
1114 }
Jens Axboe848f7e12020-01-23 15:33:32 -07001115 refcount_set(&wq->use_refs, 1);
Jens Axboeb60fda62019-11-19 08:37:07 -07001116 reinit_completion(&wq->done);
Jens Axboe771b53d02019-10-22 10:25:58 -06001117 return wq;
1118 }
1119
1120 ret = PTR_ERR(wq->manager);
Jens Axboe771b53d02019-10-22 10:25:58 -06001121 complete(&wq->done);
Jens Axboeb60fda62019-11-19 08:37:07 -07001122err:
Jann Horn3fc50ab2019-11-26 19:10:20 +01001123 for_each_node(node)
1124 kfree(wq->wqes[node]);
Jens Axboeb60fda62019-11-19 08:37:07 -07001125 kfree(wq->wqes);
1126 kfree(wq);
Jens Axboe771b53d02019-10-22 10:25:58 -06001127 return ERR_PTR(ret);
1128}
1129
Pavel Begunkoveba6f5a2020-01-28 03:15:47 +03001130bool io_wq_get(struct io_wq *wq, struct io_wq_data *data)
1131{
1132 if (data->get_work != wq->get_work || data->put_work != wq->put_work)
1133 return false;
1134
1135 return refcount_inc_not_zero(&wq->use_refs);
1136}
1137
Jens Axboe771b53d02019-10-22 10:25:58 -06001138static bool io_wq_worker_wake(struct io_worker *worker, void *data)
1139{
1140 wake_up_process(worker->task);
1141 return false;
1142}
1143
Jens Axboe848f7e12020-01-23 15:33:32 -07001144static void __io_wq_destroy(struct io_wq *wq)
Jens Axboe771b53d02019-10-22 10:25:58 -06001145{
Jann Horn3fc50ab2019-11-26 19:10:20 +01001146 int node;
Jens Axboe771b53d02019-10-22 10:25:58 -06001147
Jens Axboeb60fda62019-11-19 08:37:07 -07001148 set_bit(IO_WQ_BIT_EXIT, &wq->state);
1149 if (wq->manager)
Jens Axboe771b53d02019-10-22 10:25:58 -06001150 kthread_stop(wq->manager);
Jens Axboe771b53d02019-10-22 10:25:58 -06001151
1152 rcu_read_lock();
Jann Horn3fc50ab2019-11-26 19:10:20 +01001153 for_each_node(node)
1154 io_wq_for_each_worker(wq->wqes[node], io_wq_worker_wake, NULL);
Jens Axboe771b53d02019-10-22 10:25:58 -06001155 rcu_read_unlock();
1156
1157 wait_for_completion(&wq->done);
1158
Jann Horn3fc50ab2019-11-26 19:10:20 +01001159 for_each_node(node)
1160 kfree(wq->wqes[node]);
Jens Axboe771b53d02019-10-22 10:25:58 -06001161 kfree(wq->wqes);
1162 kfree(wq);
1163}
Jens Axboe848f7e12020-01-23 15:33:32 -07001164
1165void io_wq_destroy(struct io_wq *wq)
1166{
1167 if (refcount_dec_and_test(&wq->use_refs))
1168 __io_wq_destroy(wq);
1169}