blob: cc5cf2209fb070c7be9291da014ab181617e5d56 [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
Pavel Begunkov86f3cd12020-03-23 22:57:22 +030072#define IO_WQ_NR_HASH_BUCKETS (1u << IO_WQ_HASH_ORDER)
73
Jens Axboec5def4a2019-11-07 11:41:16 -070074struct io_wqe_acct {
75 unsigned nr_workers;
76 unsigned max_workers;
77 atomic_t nr_running;
78};
79
80enum {
81 IO_WQ_ACCT_BOUND,
82 IO_WQ_ACCT_UNBOUND,
83};
84
Jens Axboe771b53d02019-10-22 10:25:58 -060085/*
86 * Per-node worker thread pool
87 */
88struct io_wqe {
89 struct {
90 spinlock_t lock;
Jens Axboe6206f0e2019-11-26 11:59:32 -070091 struct io_wq_work_list work_list;
Jens Axboe771b53d02019-10-22 10:25:58 -060092 unsigned long hash_map;
93 unsigned flags;
94 } ____cacheline_aligned_in_smp;
95
96 int node;
Jens Axboec5def4a2019-11-07 11:41:16 -070097 struct io_wqe_acct acct[2];
Jens Axboe771b53d02019-10-22 10:25:58 -060098
Jens Axboe021d1cd2019-11-14 08:00:41 -070099 struct hlist_nulls_head free_list;
Jens Axboee61df662019-11-13 13:54:49 -0700100 struct list_head all_list;
Jens Axboe771b53d02019-10-22 10:25:58 -0600101
102 struct io_wq *wq;
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300103 struct io_wq_work *hash_tail[IO_WQ_NR_HASH_BUCKETS];
Jens Axboe771b53d02019-10-22 10:25:58 -0600104};
105
106/*
107 * Per io_wq state
108 */
109struct io_wq {
110 struct io_wqe **wqes;
111 unsigned long state;
Jens Axboe771b53d02019-10-22 10:25:58 -0600112
Pavel Begunkove9fd9392020-03-04 16:14:12 +0300113 free_work_fn *free_work;
Jens Axboe7d723062019-11-12 22:31:31 -0700114
Jens Axboe771b53d02019-10-22 10:25:58 -0600115 struct task_struct *manager;
Jens Axboec5def4a2019-11-07 11:41:16 -0700116 struct user_struct *user;
Jens Axboe771b53d02019-10-22 10:25:58 -0600117 refcount_t refs;
118 struct completion done;
Jens Axboe848f7e12020-01-23 15:33:32 -0700119
120 refcount_t use_refs;
Jens Axboe771b53d02019-10-22 10:25:58 -0600121};
122
Jens Axboe771b53d02019-10-22 10:25:58 -0600123static bool io_worker_get(struct io_worker *worker)
124{
125 return refcount_inc_not_zero(&worker->ref);
126}
127
128static void io_worker_release(struct io_worker *worker)
129{
130 if (refcount_dec_and_test(&worker->ref))
131 wake_up_process(worker->task);
132}
133
134/*
135 * Note: drops the wqe->lock if returning true! The caller must re-acquire
136 * the lock in that case. Some callers need to restart handling if this
137 * happens, so we can't just re-acquire the lock on behalf of the caller.
138 */
139static bool __io_worker_unuse(struct io_wqe *wqe, struct io_worker *worker)
140{
Jens Axboefcb323c2019-10-24 12:39:47 -0600141 bool dropped_lock = false;
142
Jens Axboecccf0ee2020-01-27 16:34:48 -0700143 if (worker->saved_creds) {
144 revert_creds(worker->saved_creds);
145 worker->cur_creds = worker->saved_creds = NULL;
Jens Axboe181e4482019-11-25 08:52:30 -0700146 }
147
Jens Axboefcb323c2019-10-24 12:39:47 -0600148 if (current->files != worker->restore_files) {
149 __acquire(&wqe->lock);
150 spin_unlock_irq(&wqe->lock);
151 dropped_lock = true;
152
153 task_lock(current);
154 current->files = worker->restore_files;
155 task_unlock(current);
156 }
157
Jens Axboe9392a272020-02-06 21:42:51 -0700158 if (current->fs != worker->restore_fs)
159 current->fs = worker->restore_fs;
160
Jens Axboe771b53d02019-10-22 10:25:58 -0600161 /*
162 * If we have an active mm, we need to drop the wq lock before unusing
163 * it. If we do, return true and let the caller retry the idle loop.
164 */
165 if (worker->mm) {
Jens Axboefcb323c2019-10-24 12:39:47 -0600166 if (!dropped_lock) {
167 __acquire(&wqe->lock);
168 spin_unlock_irq(&wqe->lock);
169 dropped_lock = true;
170 }
Jens Axboe771b53d02019-10-22 10:25:58 -0600171 __set_current_state(TASK_RUNNING);
172 set_fs(KERNEL_DS);
173 unuse_mm(worker->mm);
174 mmput(worker->mm);
175 worker->mm = NULL;
Jens Axboe771b53d02019-10-22 10:25:58 -0600176 }
177
Jens Axboefcb323c2019-10-24 12:39:47 -0600178 return dropped_lock;
Jens Axboe771b53d02019-10-22 10:25:58 -0600179}
180
Jens Axboec5def4a2019-11-07 11:41:16 -0700181static inline struct io_wqe_acct *io_work_get_acct(struct io_wqe *wqe,
182 struct io_wq_work *work)
183{
184 if (work->flags & IO_WQ_WORK_UNBOUND)
185 return &wqe->acct[IO_WQ_ACCT_UNBOUND];
186
187 return &wqe->acct[IO_WQ_ACCT_BOUND];
188}
189
190static inline struct io_wqe_acct *io_wqe_get_acct(struct io_wqe *wqe,
191 struct io_worker *worker)
192{
193 if (worker->flags & IO_WORKER_F_BOUND)
194 return &wqe->acct[IO_WQ_ACCT_BOUND];
195
196 return &wqe->acct[IO_WQ_ACCT_UNBOUND];
197}
198
Jens Axboe771b53d02019-10-22 10:25:58 -0600199static void io_worker_exit(struct io_worker *worker)
200{
201 struct io_wqe *wqe = worker->wqe;
Jens Axboec5def4a2019-11-07 11:41:16 -0700202 struct io_wqe_acct *acct = io_wqe_get_acct(wqe, worker);
203 unsigned nr_workers;
Jens Axboe771b53d02019-10-22 10:25:58 -0600204
205 /*
206 * If we're not at zero, someone else is holding a brief reference
207 * to the worker. Wait for that to go away.
208 */
209 set_current_state(TASK_INTERRUPTIBLE);
210 if (!refcount_dec_and_test(&worker->ref))
211 schedule();
212 __set_current_state(TASK_RUNNING);
213
214 preempt_disable();
215 current->flags &= ~PF_IO_WORKER;
216 if (worker->flags & IO_WORKER_F_RUNNING)
Jens Axboec5def4a2019-11-07 11:41:16 -0700217 atomic_dec(&acct->nr_running);
218 if (!(worker->flags & IO_WORKER_F_BOUND))
219 atomic_dec(&wqe->wq->user->processes);
Jens Axboe771b53d02019-10-22 10:25:58 -0600220 worker->flags = 0;
221 preempt_enable();
222
223 spin_lock_irq(&wqe->lock);
224 hlist_nulls_del_rcu(&worker->nulls_node);
Jens Axboee61df662019-11-13 13:54:49 -0700225 list_del_rcu(&worker->all_list);
Jens Axboe771b53d02019-10-22 10:25:58 -0600226 if (__io_worker_unuse(wqe, worker)) {
227 __release(&wqe->lock);
228 spin_lock_irq(&wqe->lock);
229 }
Jens Axboec5def4a2019-11-07 11:41:16 -0700230 acct->nr_workers--;
231 nr_workers = wqe->acct[IO_WQ_ACCT_BOUND].nr_workers +
232 wqe->acct[IO_WQ_ACCT_UNBOUND].nr_workers;
Jens Axboe771b53d02019-10-22 10:25:58 -0600233 spin_unlock_irq(&wqe->lock);
234
235 /* all workers gone, wq exit can proceed */
Jens Axboec5def4a2019-11-07 11:41:16 -0700236 if (!nr_workers && refcount_dec_and_test(&wqe->wq->refs))
Jens Axboe771b53d02019-10-22 10:25:58 -0600237 complete(&wqe->wq->done);
238
YueHaibing364b05f2019-11-02 15:55:01 +0800239 kfree_rcu(worker, rcu);
Jens Axboe771b53d02019-10-22 10:25:58 -0600240}
241
Jens Axboec5def4a2019-11-07 11:41:16 -0700242static inline bool io_wqe_run_queue(struct io_wqe *wqe)
243 __must_hold(wqe->lock)
244{
Jens Axboe6206f0e2019-11-26 11:59:32 -0700245 if (!wq_list_empty(&wqe->work_list) &&
246 !(wqe->flags & IO_WQE_FLAG_STALLED))
Jens Axboec5def4a2019-11-07 11:41:16 -0700247 return true;
248 return false;
249}
250
251/*
252 * Check head of free list for an available worker. If one isn't available,
253 * caller must wake up the wq manager to create one.
254 */
255static bool io_wqe_activate_free_worker(struct io_wqe *wqe)
256 __must_hold(RCU)
257{
258 struct hlist_nulls_node *n;
259 struct io_worker *worker;
260
Jens Axboe021d1cd2019-11-14 08:00:41 -0700261 n = rcu_dereference(hlist_nulls_first_rcu(&wqe->free_list));
Jens Axboec5def4a2019-11-07 11:41:16 -0700262 if (is_a_nulls(n))
263 return false;
264
265 worker = hlist_nulls_entry(n, struct io_worker, nulls_node);
266 if (io_worker_get(worker)) {
Jens Axboe506d95f2019-12-07 21:03:59 -0700267 wake_up_process(worker->task);
Jens Axboec5def4a2019-11-07 11:41:16 -0700268 io_worker_release(worker);
269 return true;
270 }
271
272 return false;
273}
274
275/*
276 * We need a worker. If we find a free one, we're good. If not, and we're
277 * below the max number of workers, wake up the manager to create one.
278 */
279static void io_wqe_wake_worker(struct io_wqe *wqe, struct io_wqe_acct *acct)
280{
281 bool ret;
282
283 /*
284 * Most likely an attempt to queue unbounded work on an io_wq that
285 * wasn't setup with any unbounded workers.
286 */
287 WARN_ON_ONCE(!acct->max_workers);
288
289 rcu_read_lock();
290 ret = io_wqe_activate_free_worker(wqe);
291 rcu_read_unlock();
292
293 if (!ret && acct->nr_workers < acct->max_workers)
294 wake_up_process(wqe->wq->manager);
295}
296
297static void io_wqe_inc_running(struct io_wqe *wqe, struct io_worker *worker)
298{
299 struct io_wqe_acct *acct = io_wqe_get_acct(wqe, worker);
300
301 atomic_inc(&acct->nr_running);
302}
303
304static void io_wqe_dec_running(struct io_wqe *wqe, struct io_worker *worker)
305 __must_hold(wqe->lock)
306{
307 struct io_wqe_acct *acct = io_wqe_get_acct(wqe, worker);
308
309 if (atomic_dec_and_test(&acct->nr_running) && io_wqe_run_queue(wqe))
310 io_wqe_wake_worker(wqe, acct);
311}
312
Jens Axboe771b53d02019-10-22 10:25:58 -0600313static void io_worker_start(struct io_wqe *wqe, struct io_worker *worker)
314{
315 allow_kernel_signal(SIGINT);
316
317 current->flags |= PF_IO_WORKER;
318
319 worker->flags |= (IO_WORKER_F_UP | IO_WORKER_F_RUNNING);
Jens Axboefcb323c2019-10-24 12:39:47 -0600320 worker->restore_files = current->files;
Jens Axboe9392a272020-02-06 21:42:51 -0700321 worker->restore_fs = current->fs;
Jens Axboec5def4a2019-11-07 11:41:16 -0700322 io_wqe_inc_running(wqe, worker);
Jens Axboe771b53d02019-10-22 10:25:58 -0600323}
324
325/*
326 * Worker will start processing some work. Move it to the busy list, if
327 * it's currently on the freelist
328 */
329static void __io_worker_busy(struct io_wqe *wqe, struct io_worker *worker,
330 struct io_wq_work *work)
331 __must_hold(wqe->lock)
332{
Jens Axboec5def4a2019-11-07 11:41:16 -0700333 bool worker_bound, work_bound;
334
Jens Axboe771b53d02019-10-22 10:25:58 -0600335 if (worker->flags & IO_WORKER_F_FREE) {
336 worker->flags &= ~IO_WORKER_F_FREE;
337 hlist_nulls_del_init_rcu(&worker->nulls_node);
Jens Axboe771b53d02019-10-22 10:25:58 -0600338 }
Jens Axboec5def4a2019-11-07 11:41:16 -0700339
340 /*
341 * If worker is moving from bound to unbound (or vice versa), then
342 * ensure we update the running accounting.
343 */
Dan Carpenterb2e9c7d62019-11-19 09:22:16 +0300344 worker_bound = (worker->flags & IO_WORKER_F_BOUND) != 0;
345 work_bound = (work->flags & IO_WQ_WORK_UNBOUND) == 0;
346 if (worker_bound != work_bound) {
Jens Axboec5def4a2019-11-07 11:41:16 -0700347 io_wqe_dec_running(wqe, worker);
348 if (work_bound) {
349 worker->flags |= IO_WORKER_F_BOUND;
350 wqe->acct[IO_WQ_ACCT_UNBOUND].nr_workers--;
351 wqe->acct[IO_WQ_ACCT_BOUND].nr_workers++;
352 atomic_dec(&wqe->wq->user->processes);
353 } else {
354 worker->flags &= ~IO_WORKER_F_BOUND;
355 wqe->acct[IO_WQ_ACCT_UNBOUND].nr_workers++;
356 wqe->acct[IO_WQ_ACCT_BOUND].nr_workers--;
357 atomic_inc(&wqe->wq->user->processes);
358 }
359 io_wqe_inc_running(wqe, worker);
360 }
Jens Axboe771b53d02019-10-22 10:25:58 -0600361}
362
363/*
364 * No work, worker going to sleep. Move to freelist, and unuse mm if we
365 * have one attached. Dropping the mm may potentially sleep, so we drop
366 * the lock in that case and return success. Since the caller has to
367 * retry the loop in that case (we changed task state), we don't regrab
368 * the lock if we return success.
369 */
370static bool __io_worker_idle(struct io_wqe *wqe, struct io_worker *worker)
371 __must_hold(wqe->lock)
372{
373 if (!(worker->flags & IO_WORKER_F_FREE)) {
374 worker->flags |= IO_WORKER_F_FREE;
Jens Axboe021d1cd2019-11-14 08:00:41 -0700375 hlist_nulls_add_head_rcu(&worker->nulls_node, &wqe->free_list);
Jens Axboe771b53d02019-10-22 10:25:58 -0600376 }
377
378 return __io_worker_unuse(wqe, worker);
379}
380
Pavel Begunkov60cf46a2020-03-14 00:31:05 +0300381static inline unsigned int io_get_work_hash(struct io_wq_work *work)
382{
383 return work->flags >> IO_WQ_HASH_SHIFT;
384}
385
386static struct io_wq_work *io_get_next_work(struct io_wqe *wqe)
Jens Axboe771b53d02019-10-22 10:25:58 -0600387 __must_hold(wqe->lock)
388{
Jens Axboe6206f0e2019-11-26 11:59:32 -0700389 struct io_wq_work_node *node, *prev;
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300390 struct io_wq_work *work, *tail;
Pavel Begunkov60cf46a2020-03-14 00:31:05 +0300391 unsigned int hash;
Jens Axboe771b53d02019-10-22 10:25:58 -0600392
Jens Axboe6206f0e2019-11-26 11:59:32 -0700393 wq_list_for_each(node, prev, &wqe->work_list) {
394 work = container_of(node, struct io_wq_work, list);
395
Jens Axboe771b53d02019-10-22 10:25:58 -0600396 /* not hashed, can run anytime */
Pavel Begunkov8766dd52020-03-14 00:31:04 +0300397 if (!io_wq_is_hashed(work)) {
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300398 wq_list_del(&wqe->work_list, node, prev);
Jens Axboe771b53d02019-10-22 10:25:58 -0600399 return work;
400 }
401
402 /* hashed, can run if not already running */
Pavel Begunkov60cf46a2020-03-14 00:31:05 +0300403 hash = io_get_work_hash(work);
404 if (!(wqe->hash_map & BIT(hash))) {
405 wqe->hash_map |= BIT(hash);
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300406 /* all items with this hash lie in [work, tail] */
407 tail = wqe->hash_tail[hash];
408 wqe->hash_tail[hash] = NULL;
409 wq_list_cut(&wqe->work_list, &tail->list, prev);
Jens Axboe771b53d02019-10-22 10:25:58 -0600410 return work;
411 }
412 }
413
414 return NULL;
415}
416
Jens Axboecccf0ee2020-01-27 16:34:48 -0700417static void io_wq_switch_mm(struct io_worker *worker, struct io_wq_work *work)
418{
419 if (worker->mm) {
420 unuse_mm(worker->mm);
421 mmput(worker->mm);
422 worker->mm = NULL;
423 }
424 if (!work->mm) {
425 set_fs(KERNEL_DS);
426 return;
427 }
428 if (mmget_not_zero(work->mm)) {
429 use_mm(work->mm);
430 if (!worker->mm)
431 set_fs(USER_DS);
432 worker->mm = work->mm;
433 /* hang on to this mm */
434 work->mm = NULL;
435 return;
436 }
437
438 /* failed grabbing mm, ensure work gets cancelled */
439 work->flags |= IO_WQ_WORK_CANCEL;
440}
441
442static void io_wq_switch_creds(struct io_worker *worker,
443 struct io_wq_work *work)
444{
445 const struct cred *old_creds = override_creds(work->creds);
446
447 worker->cur_creds = work->creds;
448 if (worker->saved_creds)
449 put_cred(old_creds); /* creds set by previous switch */
450 else
451 worker->saved_creds = old_creds;
452}
453
Pavel Begunkovdc026a72020-03-04 16:14:09 +0300454static void io_impersonate_work(struct io_worker *worker,
455 struct io_wq_work *work)
456{
457 if (work->files && current->files != work->files) {
458 task_lock(current);
459 current->files = work->files;
460 task_unlock(current);
461 }
462 if (work->fs && current->fs != work->fs)
463 current->fs = work->fs;
464 if (work->mm != worker->mm)
465 io_wq_switch_mm(worker, work);
466 if (worker->cur_creds != work->creds)
467 io_wq_switch_creds(worker, work);
468}
469
470static void io_assign_current_work(struct io_worker *worker,
471 struct io_wq_work *work)
472{
Pavel Begunkovd78298e2020-03-14 00:31:03 +0300473 if (work) {
474 /* flush pending signals before assigning new work */
475 if (signal_pending(current))
476 flush_signals(current);
477 cond_resched();
478 }
Pavel Begunkovdc026a72020-03-04 16:14:09 +0300479
480 spin_lock_irq(&worker->lock);
481 worker->cur_work = work;
482 spin_unlock_irq(&worker->lock);
483}
484
Pavel Begunkov60cf46a2020-03-14 00:31:05 +0300485static void io_wqe_enqueue(struct io_wqe *wqe, struct io_wq_work *work);
486
Jens Axboe771b53d02019-10-22 10:25:58 -0600487static void io_worker_handle_work(struct io_worker *worker)
488 __releases(wqe->lock)
489{
Jens Axboe771b53d02019-10-22 10:25:58 -0600490 struct io_wqe *wqe = worker->wqe;
491 struct io_wq *wq = wqe->wq;
492
493 do {
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300494 struct io_wq_work *work;
Pavel Begunkov60cf46a2020-03-14 00:31:05 +0300495 unsigned int hash;
Pavel Begunkovf462fd32020-03-04 16:14:11 +0300496get_next:
Jens Axboe771b53d02019-10-22 10:25:58 -0600497 /*
Jens Axboe771b53d02019-10-22 10:25:58 -0600498 * If we got some work, mark us as busy. If we didn't, but
499 * the list isn't empty, it means we stalled on hashed work.
500 * Mark us stalled so we don't keep looking for work when we
501 * can't make progress, any work completion or insertion will
502 * clear the stalled flag.
503 */
Pavel Begunkov60cf46a2020-03-14 00:31:05 +0300504 work = io_get_next_work(wqe);
Jens Axboe771b53d02019-10-22 10:25:58 -0600505 if (work)
506 __io_worker_busy(wqe, worker, work);
Jens Axboe6206f0e2019-11-26 11:59:32 -0700507 else if (!wq_list_empty(&wqe->work_list))
Jens Axboe771b53d02019-10-22 10:25:58 -0600508 wqe->flags |= IO_WQE_FLAG_STALLED;
509
510 spin_unlock_irq(&wqe->lock);
511 if (!work)
512 break;
Pavel Begunkov58e39312020-03-04 16:14:10 +0300513 io_assign_current_work(worker, work);
Jens Axboe36c2f922019-11-13 09:43:34 -0700514
Pavel Begunkovdc026a72020-03-04 16:14:09 +0300515 /* handle a whole dependent link */
516 do {
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300517 struct io_wq_work *old_work, *next_hashed, *linked;
Hillf Dantonfd1c4bc2019-12-24 09:14:29 -0700518
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300519 next_hashed = wq_next_work(work);
Pavel Begunkov58e39312020-03-04 16:14:10 +0300520 io_impersonate_work(worker, work);
Pavel Begunkovdc026a72020-03-04 16:14:09 +0300521 /*
522 * OK to set IO_WQ_WORK_CANCEL even for uncancellable
523 * work, the worker function will do the right thing.
524 */
525 if (test_bit(IO_WQ_BIT_CANCEL, &wq->state))
526 work->flags |= IO_WQ_WORK_CANCEL;
Jens Axboe36c2f922019-11-13 09:43:34 -0700527
Pavel Begunkov60cf46a2020-03-14 00:31:05 +0300528 hash = io_get_work_hash(work);
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300529 linked = old_work = work;
530 linked->func(&linked);
531 linked = (old_work == linked) ? NULL : linked;
Pavel Begunkovf2cf1142020-03-22 19:14:26 +0300532
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300533 work = next_hashed;
534 if (!work && linked && !io_wq_is_hashed(linked)) {
535 work = linked;
536 linked = NULL;
537 }
538 io_assign_current_work(worker, work);
Pavel Begunkove9fd9392020-03-04 16:14:12 +0300539 wq->free_work(old_work);
Pavel Begunkovdc026a72020-03-04 16:14:09 +0300540
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300541 if (linked)
542 io_wqe_enqueue(wqe, linked);
543
544 if (hash != -1U && !next_hashed) {
Pavel Begunkovdc026a72020-03-04 16:14:09 +0300545 spin_lock_irq(&wqe->lock);
546 wqe->hash_map &= ~BIT_ULL(hash);
547 wqe->flags &= ~IO_WQE_FLAG_STALLED;
Pavel Begunkovdc026a72020-03-04 16:14:09 +0300548 /* dependent work is not hashed */
549 hash = -1U;
Pavel Begunkovf462fd32020-03-04 16:14:11 +0300550 /* skip unnecessary unlock-lock wqe->lock */
551 if (!work)
552 goto get_next;
553 spin_unlock_irq(&wqe->lock);
Pavel Begunkovdc026a72020-03-04 16:14:09 +0300554 }
Pavel Begunkov58e39312020-03-04 16:14:10 +0300555 } while (work);
Jens Axboe36c2f922019-11-13 09:43:34 -0700556
557 spin_lock_irq(&wqe->lock);
Jens Axboe771b53d02019-10-22 10:25:58 -0600558 } while (1);
559}
560
Jens Axboe771b53d02019-10-22 10:25:58 -0600561static int io_wqe_worker(void *data)
562{
563 struct io_worker *worker = data;
564 struct io_wqe *wqe = worker->wqe;
565 struct io_wq *wq = wqe->wq;
Jens Axboe771b53d02019-10-22 10:25:58 -0600566
567 io_worker_start(wqe, worker);
568
569 while (!test_bit(IO_WQ_BIT_EXIT, &wq->state)) {
Jens Axboe506d95f2019-12-07 21:03:59 -0700570 set_current_state(TASK_INTERRUPTIBLE);
Jens Axboee995d512019-12-07 21:06:46 -0700571loop:
Jens Axboe771b53d02019-10-22 10:25:58 -0600572 spin_lock_irq(&wqe->lock);
573 if (io_wqe_run_queue(wqe)) {
574 __set_current_state(TASK_RUNNING);
575 io_worker_handle_work(worker);
Jens Axboee995d512019-12-07 21:06:46 -0700576 goto loop;
Jens Axboe771b53d02019-10-22 10:25:58 -0600577 }
578 /* drops the lock on success, retry */
579 if (__io_worker_idle(wqe, worker)) {
580 __release(&wqe->lock);
Jens Axboee995d512019-12-07 21:06:46 -0700581 goto loop;
Jens Axboe771b53d02019-10-22 10:25:58 -0600582 }
583 spin_unlock_irq(&wqe->lock);
584 if (signal_pending(current))
585 flush_signals(current);
586 if (schedule_timeout(WORKER_IDLE_TIMEOUT))
587 continue;
588 /* timed out, exit unless we're the fixed worker */
589 if (test_bit(IO_WQ_BIT_EXIT, &wq->state) ||
590 !(worker->flags & IO_WORKER_F_FIXED))
591 break;
592 }
593
Jens Axboe771b53d02019-10-22 10:25:58 -0600594 if (test_bit(IO_WQ_BIT_EXIT, &wq->state)) {
595 spin_lock_irq(&wqe->lock);
Jens Axboe6206f0e2019-11-26 11:59:32 -0700596 if (!wq_list_empty(&wqe->work_list))
Jens Axboe771b53d02019-10-22 10:25:58 -0600597 io_worker_handle_work(worker);
598 else
599 spin_unlock_irq(&wqe->lock);
600 }
601
602 io_worker_exit(worker);
603 return 0;
604}
605
606/*
Jens Axboe771b53d02019-10-22 10:25:58 -0600607 * Called when a worker is scheduled in. Mark us as currently running.
608 */
609void io_wq_worker_running(struct task_struct *tsk)
610{
611 struct io_worker *worker = kthread_data(tsk);
612 struct io_wqe *wqe = worker->wqe;
613
614 if (!(worker->flags & IO_WORKER_F_UP))
615 return;
616 if (worker->flags & IO_WORKER_F_RUNNING)
617 return;
618 worker->flags |= IO_WORKER_F_RUNNING;
Jens Axboec5def4a2019-11-07 11:41:16 -0700619 io_wqe_inc_running(wqe, worker);
Jens Axboe771b53d02019-10-22 10:25:58 -0600620}
621
622/*
623 * Called when worker is going to sleep. If there are no workers currently
624 * running and we have work pending, wake up a free one or have the manager
625 * set one up.
626 */
627void io_wq_worker_sleeping(struct task_struct *tsk)
628{
629 struct io_worker *worker = kthread_data(tsk);
630 struct io_wqe *wqe = worker->wqe;
631
632 if (!(worker->flags & IO_WORKER_F_UP))
633 return;
634 if (!(worker->flags & IO_WORKER_F_RUNNING))
635 return;
636
637 worker->flags &= ~IO_WORKER_F_RUNNING;
638
639 spin_lock_irq(&wqe->lock);
Jens Axboec5def4a2019-11-07 11:41:16 -0700640 io_wqe_dec_running(wqe, worker);
Jens Axboe771b53d02019-10-22 10:25:58 -0600641 spin_unlock_irq(&wqe->lock);
642}
643
Jens Axboeb60fda62019-11-19 08:37:07 -0700644static bool create_io_worker(struct io_wq *wq, struct io_wqe *wqe, int index)
Jens Axboe771b53d02019-10-22 10:25:58 -0600645{
Jens Axboec5def4a2019-11-07 11:41:16 -0700646 struct io_wqe_acct *acct =&wqe->acct[index];
Jens Axboe771b53d02019-10-22 10:25:58 -0600647 struct io_worker *worker;
648
Jann Hornad6e0052019-11-26 17:39:45 +0100649 worker = kzalloc_node(sizeof(*worker), GFP_KERNEL, wqe->node);
Jens Axboe771b53d02019-10-22 10:25:58 -0600650 if (!worker)
Jens Axboeb60fda62019-11-19 08:37:07 -0700651 return false;
Jens Axboe771b53d02019-10-22 10:25:58 -0600652
653 refcount_set(&worker->ref, 1);
654 worker->nulls_node.pprev = NULL;
Jens Axboe771b53d02019-10-22 10:25:58 -0600655 worker->wqe = wqe;
Jens Axboe36c2f922019-11-13 09:43:34 -0700656 spin_lock_init(&worker->lock);
Jens Axboe771b53d02019-10-22 10:25:58 -0600657
658 worker->task = kthread_create_on_node(io_wqe_worker, worker, wqe->node,
Jens Axboec5def4a2019-11-07 11:41:16 -0700659 "io_wqe_worker-%d/%d", index, wqe->node);
Jens Axboe771b53d02019-10-22 10:25:58 -0600660 if (IS_ERR(worker->task)) {
661 kfree(worker);
Jens Axboeb60fda62019-11-19 08:37:07 -0700662 return false;
Jens Axboe771b53d02019-10-22 10:25:58 -0600663 }
664
665 spin_lock_irq(&wqe->lock);
Jens Axboe021d1cd2019-11-14 08:00:41 -0700666 hlist_nulls_add_head_rcu(&worker->nulls_node, &wqe->free_list);
Jens Axboee61df662019-11-13 13:54:49 -0700667 list_add_tail_rcu(&worker->all_list, &wqe->all_list);
Jens Axboe771b53d02019-10-22 10:25:58 -0600668 worker->flags |= IO_WORKER_F_FREE;
Jens Axboec5def4a2019-11-07 11:41:16 -0700669 if (index == IO_WQ_ACCT_BOUND)
670 worker->flags |= IO_WORKER_F_BOUND;
671 if (!acct->nr_workers && (worker->flags & IO_WORKER_F_BOUND))
Jens Axboe771b53d02019-10-22 10:25:58 -0600672 worker->flags |= IO_WORKER_F_FIXED;
Jens Axboec5def4a2019-11-07 11:41:16 -0700673 acct->nr_workers++;
Jens Axboe771b53d02019-10-22 10:25:58 -0600674 spin_unlock_irq(&wqe->lock);
675
Jens Axboec5def4a2019-11-07 11:41:16 -0700676 if (index == IO_WQ_ACCT_UNBOUND)
677 atomic_inc(&wq->user->processes);
678
Jens Axboe771b53d02019-10-22 10:25:58 -0600679 wake_up_process(worker->task);
Jens Axboeb60fda62019-11-19 08:37:07 -0700680 return true;
Jens Axboe771b53d02019-10-22 10:25:58 -0600681}
682
Jens Axboec5def4a2019-11-07 11:41:16 -0700683static inline bool io_wqe_need_worker(struct io_wqe *wqe, int index)
Jens Axboe771b53d02019-10-22 10:25:58 -0600684 __must_hold(wqe->lock)
685{
Jens Axboec5def4a2019-11-07 11:41:16 -0700686 struct io_wqe_acct *acct = &wqe->acct[index];
Jens Axboe771b53d02019-10-22 10:25:58 -0600687
Jens Axboec5def4a2019-11-07 11:41:16 -0700688 /* if we have available workers or no work, no need */
Jens Axboe021d1cd2019-11-14 08:00:41 -0700689 if (!hlist_nulls_empty(&wqe->free_list) || !io_wqe_run_queue(wqe))
Jens Axboec5def4a2019-11-07 11:41:16 -0700690 return false;
691 return acct->nr_workers < acct->max_workers;
Jens Axboe771b53d02019-10-22 10:25:58 -0600692}
693
694/*
695 * Manager thread. Tasked with creating new workers, if we need them.
696 */
697static int io_wq_manager(void *data)
698{
699 struct io_wq *wq = data;
Jann Horn3fc50ab2019-11-26 19:10:20 +0100700 int workers_to_create = num_possible_nodes();
701 int node;
Jens Axboeb60fda62019-11-19 08:37:07 -0700702
703 /* create fixed workers */
Jann Horn3fc50ab2019-11-26 19:10:20 +0100704 refcount_set(&wq->refs, workers_to_create);
705 for_each_node(node) {
Jens Axboe75634392020-02-11 06:30:06 -0700706 if (!node_online(node))
707 continue;
Jann Horn3fc50ab2019-11-26 19:10:20 +0100708 if (!create_io_worker(wq, wq->wqes[node], IO_WQ_ACCT_BOUND))
709 goto err;
710 workers_to_create--;
Jens Axboeb60fda62019-11-19 08:37:07 -0700711 }
712
Jens Axboe75634392020-02-11 06:30:06 -0700713 while (workers_to_create--)
714 refcount_dec(&wq->refs);
715
Jens Axboeb60fda62019-11-19 08:37:07 -0700716 complete(&wq->done);
Jens Axboe771b53d02019-10-22 10:25:58 -0600717
718 while (!kthread_should_stop()) {
Jann Horn3fc50ab2019-11-26 19:10:20 +0100719 for_each_node(node) {
720 struct io_wqe *wqe = wq->wqes[node];
Jens Axboec5def4a2019-11-07 11:41:16 -0700721 bool fork_worker[2] = { false, false };
Jens Axboe771b53d02019-10-22 10:25:58 -0600722
Jens Axboe75634392020-02-11 06:30:06 -0700723 if (!node_online(node))
724 continue;
725
Jens Axboe771b53d02019-10-22 10:25:58 -0600726 spin_lock_irq(&wqe->lock);
Jens Axboec5def4a2019-11-07 11:41:16 -0700727 if (io_wqe_need_worker(wqe, IO_WQ_ACCT_BOUND))
728 fork_worker[IO_WQ_ACCT_BOUND] = true;
729 if (io_wqe_need_worker(wqe, IO_WQ_ACCT_UNBOUND))
730 fork_worker[IO_WQ_ACCT_UNBOUND] = true;
Jens Axboe771b53d02019-10-22 10:25:58 -0600731 spin_unlock_irq(&wqe->lock);
Jens Axboec5def4a2019-11-07 11:41:16 -0700732 if (fork_worker[IO_WQ_ACCT_BOUND])
733 create_io_worker(wq, wqe, IO_WQ_ACCT_BOUND);
734 if (fork_worker[IO_WQ_ACCT_UNBOUND])
735 create_io_worker(wq, wqe, IO_WQ_ACCT_UNBOUND);
Jens Axboe771b53d02019-10-22 10:25:58 -0600736 }
737 set_current_state(TASK_INTERRUPTIBLE);
738 schedule_timeout(HZ);
739 }
740
741 return 0;
Jens Axboeb60fda62019-11-19 08:37:07 -0700742err:
743 set_bit(IO_WQ_BIT_ERROR, &wq->state);
744 set_bit(IO_WQ_BIT_EXIT, &wq->state);
Jann Horn3fc50ab2019-11-26 19:10:20 +0100745 if (refcount_sub_and_test(workers_to_create, &wq->refs))
Jens Axboeb60fda62019-11-19 08:37:07 -0700746 complete(&wq->done);
747 return 0;
Jens Axboe771b53d02019-10-22 10:25:58 -0600748}
749
Jens Axboec5def4a2019-11-07 11:41:16 -0700750static bool io_wq_can_queue(struct io_wqe *wqe, struct io_wqe_acct *acct,
751 struct io_wq_work *work)
752{
753 bool free_worker;
754
755 if (!(work->flags & IO_WQ_WORK_UNBOUND))
756 return true;
757 if (atomic_read(&acct->nr_running))
758 return true;
759
760 rcu_read_lock();
Jens Axboe021d1cd2019-11-14 08:00:41 -0700761 free_worker = !hlist_nulls_empty(&wqe->free_list);
Jens Axboec5def4a2019-11-07 11:41:16 -0700762 rcu_read_unlock();
763 if (free_worker)
764 return true;
765
766 if (atomic_read(&wqe->wq->user->processes) >= acct->max_workers &&
767 !(capable(CAP_SYS_RESOURCE) || capable(CAP_SYS_ADMIN)))
768 return false;
769
770 return true;
771}
772
Pavel Begunkove9fd9392020-03-04 16:14:12 +0300773static void io_run_cancel(struct io_wq_work *work, struct io_wqe *wqe)
Pavel Begunkovfc04c392020-03-01 19:18:19 +0300774{
Pavel Begunkove9fd9392020-03-04 16:14:12 +0300775 struct io_wq *wq = wqe->wq;
776
Pavel Begunkovfc04c392020-03-01 19:18:19 +0300777 do {
778 struct io_wq_work *old_work = work;
779
780 work->flags |= IO_WQ_WORK_CANCEL;
781 work->func(&work);
782 work = (work == old_work) ? NULL : work;
Pavel Begunkove9fd9392020-03-04 16:14:12 +0300783 wq->free_work(old_work);
Pavel Begunkovfc04c392020-03-01 19:18:19 +0300784 } while (work);
785}
786
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300787static void io_wqe_insert_work(struct io_wqe *wqe, struct io_wq_work *work)
788{
789 unsigned int hash;
790 struct io_wq_work *tail;
791
792 if (!io_wq_is_hashed(work)) {
793append:
794 wq_list_add_tail(&work->list, &wqe->work_list);
795 return;
796 }
797
798 hash = io_get_work_hash(work);
799 tail = wqe->hash_tail[hash];
800 wqe->hash_tail[hash] = work;
801 if (!tail)
802 goto append;
803
804 wq_list_add_after(&work->list, &tail->list, &wqe->work_list);
805}
806
Jens Axboe771b53d02019-10-22 10:25:58 -0600807static void io_wqe_enqueue(struct io_wqe *wqe, struct io_wq_work *work)
808{
Jens Axboec5def4a2019-11-07 11:41:16 -0700809 struct io_wqe_acct *acct = io_work_get_acct(wqe, work);
Jens Axboe895e2ca2019-12-17 08:46:33 -0700810 int work_flags;
Jens Axboe771b53d02019-10-22 10:25:58 -0600811 unsigned long flags;
812
Jens Axboec5def4a2019-11-07 11:41:16 -0700813 /*
814 * Do early check to see if we need a new unbound worker, and if we do,
815 * if we're allowed to do so. This isn't 100% accurate as there's a
816 * gap between this check and incrementing the value, but that's OK.
817 * It's close enough to not be an issue, fork() has the same delay.
818 */
819 if (unlikely(!io_wq_can_queue(wqe, acct, work))) {
Pavel Begunkove9fd9392020-03-04 16:14:12 +0300820 io_run_cancel(work, wqe);
Jens Axboec5def4a2019-11-07 11:41:16 -0700821 return;
822 }
823
Jens Axboe895e2ca2019-12-17 08:46:33 -0700824 work_flags = work->flags;
Jens Axboe771b53d02019-10-22 10:25:58 -0600825 spin_lock_irqsave(&wqe->lock, flags);
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300826 io_wqe_insert_work(wqe, work);
Jens Axboe771b53d02019-10-22 10:25:58 -0600827 wqe->flags &= ~IO_WQE_FLAG_STALLED;
828 spin_unlock_irqrestore(&wqe->lock, flags);
829
Jens Axboe895e2ca2019-12-17 08:46:33 -0700830 if ((work_flags & IO_WQ_WORK_CONCURRENT) ||
831 !atomic_read(&acct->nr_running))
Jens Axboec5def4a2019-11-07 11:41:16 -0700832 io_wqe_wake_worker(wqe, acct);
Jens Axboe771b53d02019-10-22 10:25:58 -0600833}
834
835void io_wq_enqueue(struct io_wq *wq, struct io_wq_work *work)
836{
837 struct io_wqe *wqe = wq->wqes[numa_node_id()];
838
839 io_wqe_enqueue(wqe, work);
840}
841
842/*
Pavel Begunkov8766dd52020-03-14 00:31:04 +0300843 * Work items that hash to the same value will not be done in parallel.
844 * Used to limit concurrent writes, generally hashed by inode.
Jens Axboe771b53d02019-10-22 10:25:58 -0600845 */
Pavel Begunkov8766dd52020-03-14 00:31:04 +0300846void io_wq_hash_work(struct io_wq_work *work, void *val)
Jens Axboe771b53d02019-10-22 10:25:58 -0600847{
Pavel Begunkov8766dd52020-03-14 00:31:04 +0300848 unsigned int bit;
Jens Axboe771b53d02019-10-22 10:25:58 -0600849
850 bit = hash_ptr(val, IO_WQ_HASH_ORDER);
851 work->flags |= (IO_WQ_WORK_HASHED | (bit << IO_WQ_HASH_SHIFT));
Jens Axboe771b53d02019-10-22 10:25:58 -0600852}
853
854static bool io_wqe_worker_send_sig(struct io_worker *worker, void *data)
855{
856 send_sig(SIGINT, worker->task, 1);
857 return false;
858}
859
860/*
861 * Iterate the passed in list and call the specific function for each
862 * worker that isn't exiting
863 */
864static bool io_wq_for_each_worker(struct io_wqe *wqe,
Jens Axboe771b53d02019-10-22 10:25:58 -0600865 bool (*func)(struct io_worker *, void *),
866 void *data)
867{
Jens Axboe771b53d02019-10-22 10:25:58 -0600868 struct io_worker *worker;
869 bool ret = false;
870
Jens Axboee61df662019-11-13 13:54:49 -0700871 list_for_each_entry_rcu(worker, &wqe->all_list, all_list) {
Jens Axboe771b53d02019-10-22 10:25:58 -0600872 if (io_worker_get(worker)) {
Jens Axboe75634392020-02-11 06:30:06 -0700873 /* no task if node is/was offline */
874 if (worker->task)
875 ret = func(worker, data);
Jens Axboe771b53d02019-10-22 10:25:58 -0600876 io_worker_release(worker);
877 if (ret)
878 break;
879 }
880 }
Jens Axboee61df662019-11-13 13:54:49 -0700881
Jens Axboe771b53d02019-10-22 10:25:58 -0600882 return ret;
883}
884
885void io_wq_cancel_all(struct io_wq *wq)
886{
Jann Horn3fc50ab2019-11-26 19:10:20 +0100887 int node;
Jens Axboe771b53d02019-10-22 10:25:58 -0600888
889 set_bit(IO_WQ_BIT_CANCEL, &wq->state);
890
Jens Axboe771b53d02019-10-22 10:25:58 -0600891 rcu_read_lock();
Jann Horn3fc50ab2019-11-26 19:10:20 +0100892 for_each_node(node) {
893 struct io_wqe *wqe = wq->wqes[node];
Jens Axboe771b53d02019-10-22 10:25:58 -0600894
Jens Axboee61df662019-11-13 13:54:49 -0700895 io_wq_for_each_worker(wqe, io_wqe_worker_send_sig, NULL);
Jens Axboe771b53d02019-10-22 10:25:58 -0600896 }
897 rcu_read_unlock();
898}
899
Jens Axboe62755e32019-10-28 21:49:21 -0600900struct io_cb_cancel_data {
Pavel Begunkov2293b412020-03-07 01:15:39 +0300901 work_cancel_fn *fn;
902 void *data;
Jens Axboe62755e32019-10-28 21:49:21 -0600903};
904
Pavel Begunkov2293b412020-03-07 01:15:39 +0300905static bool io_wq_worker_cancel(struct io_worker *worker, void *data)
Jens Axboe62755e32019-10-28 21:49:21 -0600906{
Pavel Begunkov2293b412020-03-07 01:15:39 +0300907 struct io_cb_cancel_data *match = data;
Jens Axboe6f726532019-11-05 13:51:51 -0700908 unsigned long flags;
Jens Axboe62755e32019-10-28 21:49:21 -0600909 bool ret = false;
910
911 /*
912 * Hold the lock to avoid ->cur_work going out of scope, caller
Jens Axboe36c2f922019-11-13 09:43:34 -0700913 * may dereference the passed in work.
Jens Axboe62755e32019-10-28 21:49:21 -0600914 */
Jens Axboe36c2f922019-11-13 09:43:34 -0700915 spin_lock_irqsave(&worker->lock, flags);
Jens Axboe62755e32019-10-28 21:49:21 -0600916 if (worker->cur_work &&
Jens Axboe0c9d5cc2019-12-11 19:29:43 -0700917 !(worker->cur_work->flags & IO_WQ_WORK_NO_CANCEL) &&
Pavel Begunkov2293b412020-03-07 01:15:39 +0300918 match->fn(worker->cur_work, match->data)) {
Jens Axboe771b53d02019-10-22 10:25:58 -0600919 send_sig(SIGINT, worker->task, 1);
Jens Axboe36c2f922019-11-13 09:43:34 -0700920 ret = true;
Jens Axboe771b53d02019-10-22 10:25:58 -0600921 }
Jens Axboe36c2f922019-11-13 09:43:34 -0700922 spin_unlock_irqrestore(&worker->lock, flags);
Jens Axboe771b53d02019-10-22 10:25:58 -0600923
Jens Axboe36c2f922019-11-13 09:43:34 -0700924 return ret;
Jens Axboe771b53d02019-10-22 10:25:58 -0600925}
926
927static enum io_wq_cancel io_wqe_cancel_work(struct io_wqe *wqe,
Pavel Begunkov2293b412020-03-07 01:15:39 +0300928 struct io_cb_cancel_data *match)
Jens Axboe771b53d02019-10-22 10:25:58 -0600929{
Jens Axboe6206f0e2019-11-26 11:59:32 -0700930 struct io_wq_work_node *node, *prev;
Jens Axboe771b53d02019-10-22 10:25:58 -0600931 struct io_wq_work *work;
Jens Axboe6f726532019-11-05 13:51:51 -0700932 unsigned long flags;
Jens Axboe771b53d02019-10-22 10:25:58 -0600933 bool found = false;
934
Jens Axboe771b53d02019-10-22 10:25:58 -0600935 /*
936 * First check pending list, if we're lucky we can just remove it
937 * from there. CANCEL_OK means that the work is returned as-new,
938 * no completion will be posted for it.
939 */
Jens Axboe6f726532019-11-05 13:51:51 -0700940 spin_lock_irqsave(&wqe->lock, flags);
Jens Axboe6206f0e2019-11-26 11:59:32 -0700941 wq_list_for_each(node, prev, &wqe->work_list) {
942 work = container_of(node, struct io_wq_work, list);
943
Jens Axboe00bcda12020-02-08 19:13:32 -0700944 if (match->fn(work, match->data)) {
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300945 wq_list_del(&wqe->work_list, node, prev);
Jens Axboe771b53d02019-10-22 10:25:58 -0600946 found = true;
947 break;
948 }
949 }
Jens Axboe6f726532019-11-05 13:51:51 -0700950 spin_unlock_irqrestore(&wqe->lock, flags);
Jens Axboe771b53d02019-10-22 10:25:58 -0600951
952 if (found) {
Pavel Begunkove9fd9392020-03-04 16:14:12 +0300953 io_run_cancel(work, wqe);
Jens Axboe771b53d02019-10-22 10:25:58 -0600954 return IO_WQ_CANCEL_OK;
955 }
956
957 /*
958 * Now check if a free (going busy) or busy worker has the work
959 * currently running. If we find it there, we'll return CANCEL_RUNNING
Brian Gianforcarod195a662019-12-13 03:09:50 -0800960 * as an indication that we attempt to signal cancellation. The
Jens Axboe771b53d02019-10-22 10:25:58 -0600961 * completion will run normally in this case.
962 */
963 rcu_read_lock();
Jens Axboe00bcda12020-02-08 19:13:32 -0700964 found = io_wq_for_each_worker(wqe, io_wq_worker_cancel, match);
Jens Axboe771b53d02019-10-22 10:25:58 -0600965 rcu_read_unlock();
966 return found ? IO_WQ_CANCEL_RUNNING : IO_WQ_CANCEL_NOTFOUND;
967}
968
Pavel Begunkov2293b412020-03-07 01:15:39 +0300969enum io_wq_cancel io_wq_cancel_cb(struct io_wq *wq, work_cancel_fn *cancel,
970 void *data)
971{
972 struct io_cb_cancel_data match = {
973 .fn = cancel,
974 .data = data,
975 };
976 enum io_wq_cancel ret = IO_WQ_CANCEL_NOTFOUND;
977 int node;
978
979 for_each_node(node) {
980 struct io_wqe *wqe = wq->wqes[node];
981
982 ret = io_wqe_cancel_work(wqe, &match);
983 if (ret != IO_WQ_CANCEL_NOTFOUND)
984 break;
985 }
986
987 return ret;
988}
989
990static bool io_wq_io_cb_cancel_data(struct io_wq_work *work, void *data)
Jens Axboe00bcda12020-02-08 19:13:32 -0700991{
992 return work == data;
993}
994
Jens Axboe771b53d02019-10-22 10:25:58 -0600995enum io_wq_cancel io_wq_cancel_work(struct io_wq *wq, struct io_wq_work *cwork)
996{
Pavel Begunkov2293b412020-03-07 01:15:39 +0300997 return io_wq_cancel_cb(wq, io_wq_io_cb_cancel_data, (void *)cwork);
Jens Axboe771b53d02019-10-22 10:25:58 -0600998}
999
Jens Axboe36282882020-02-08 19:16:39 -07001000static bool io_wq_pid_match(struct io_wq_work *work, void *data)
1001{
1002 pid_t pid = (pid_t) (unsigned long) data;
1003
Pavel Begunkov2293b412020-03-07 01:15:39 +03001004 return work->task_pid == pid;
Jens Axboe36282882020-02-08 19:16:39 -07001005}
1006
1007enum io_wq_cancel io_wq_cancel_pid(struct io_wq *wq, pid_t pid)
1008{
Pavel Begunkov2293b412020-03-07 01:15:39 +03001009 void *data = (void *) (unsigned long) pid;
Jens Axboe36282882020-02-08 19:16:39 -07001010
Pavel Begunkov2293b412020-03-07 01:15:39 +03001011 return io_wq_cancel_cb(wq, io_wq_pid_match, data);
Jens Axboe36282882020-02-08 19:16:39 -07001012}
1013
Jens Axboe576a3472019-11-25 08:49:20 -07001014struct io_wq *io_wq_create(unsigned bounded, struct io_wq_data *data)
Jens Axboe771b53d02019-10-22 10:25:58 -06001015{
Jann Horn3fc50ab2019-11-26 19:10:20 +01001016 int ret = -ENOMEM, node;
Jens Axboe771b53d02019-10-22 10:25:58 -06001017 struct io_wq *wq;
1018
Pavel Begunkove9fd9392020-03-04 16:14:12 +03001019 if (WARN_ON_ONCE(!data->free_work))
1020 return ERR_PTR(-EINVAL);
1021
Jann Hornad6e0052019-11-26 17:39:45 +01001022 wq = kzalloc(sizeof(*wq), GFP_KERNEL);
Jens Axboe771b53d02019-10-22 10:25:58 -06001023 if (!wq)
1024 return ERR_PTR(-ENOMEM);
1025
Jann Horn3fc50ab2019-11-26 19:10:20 +01001026 wq->wqes = kcalloc(nr_node_ids, sizeof(struct io_wqe *), GFP_KERNEL);
Jens Axboe771b53d02019-10-22 10:25:58 -06001027 if (!wq->wqes) {
1028 kfree(wq);
1029 return ERR_PTR(-ENOMEM);
1030 }
1031
Pavel Begunkove9fd9392020-03-04 16:14:12 +03001032 wq->free_work = data->free_work;
Jens Axboe7d723062019-11-12 22:31:31 -07001033
Jens Axboec5def4a2019-11-07 11:41:16 -07001034 /* caller must already hold a reference to this */
Jens Axboe576a3472019-11-25 08:49:20 -07001035 wq->user = data->user;
Jens Axboec5def4a2019-11-07 11:41:16 -07001036
Jann Horn3fc50ab2019-11-26 19:10:20 +01001037 for_each_node(node) {
Jens Axboe771b53d02019-10-22 10:25:58 -06001038 struct io_wqe *wqe;
Jens Axboe75634392020-02-11 06:30:06 -07001039 int alloc_node = node;
Jens Axboe771b53d02019-10-22 10:25:58 -06001040
Jens Axboe75634392020-02-11 06:30:06 -07001041 if (!node_online(alloc_node))
1042 alloc_node = NUMA_NO_NODE;
1043 wqe = kzalloc_node(sizeof(struct io_wqe), GFP_KERNEL, alloc_node);
Jens Axboe771b53d02019-10-22 10:25:58 -06001044 if (!wqe)
Jann Horn3fc50ab2019-11-26 19:10:20 +01001045 goto err;
1046 wq->wqes[node] = wqe;
Jens Axboe75634392020-02-11 06:30:06 -07001047 wqe->node = alloc_node;
Jens Axboec5def4a2019-11-07 11:41:16 -07001048 wqe->acct[IO_WQ_ACCT_BOUND].max_workers = bounded;
1049 atomic_set(&wqe->acct[IO_WQ_ACCT_BOUND].nr_running, 0);
Jens Axboe576a3472019-11-25 08:49:20 -07001050 if (wq->user) {
Jens Axboec5def4a2019-11-07 11:41:16 -07001051 wqe->acct[IO_WQ_ACCT_UNBOUND].max_workers =
1052 task_rlimit(current, RLIMIT_NPROC);
1053 }
1054 atomic_set(&wqe->acct[IO_WQ_ACCT_UNBOUND].nr_running, 0);
Jens Axboe771b53d02019-10-22 10:25:58 -06001055 wqe->wq = wq;
1056 spin_lock_init(&wqe->lock);
Jens Axboe6206f0e2019-11-26 11:59:32 -07001057 INIT_WQ_LIST(&wqe->work_list);
Jens Axboe021d1cd2019-11-14 08:00:41 -07001058 INIT_HLIST_NULLS_HEAD(&wqe->free_list, 0);
Jens Axboee61df662019-11-13 13:54:49 -07001059 INIT_LIST_HEAD(&wqe->all_list);
Jens Axboe771b53d02019-10-22 10:25:58 -06001060 }
1061
1062 init_completion(&wq->done);
1063
Jens Axboe771b53d02019-10-22 10:25:58 -06001064 wq->manager = kthread_create(io_wq_manager, wq, "io_wq_manager");
1065 if (!IS_ERR(wq->manager)) {
1066 wake_up_process(wq->manager);
Jens Axboeb60fda62019-11-19 08:37:07 -07001067 wait_for_completion(&wq->done);
1068 if (test_bit(IO_WQ_BIT_ERROR, &wq->state)) {
1069 ret = -ENOMEM;
1070 goto err;
1071 }
Jens Axboe848f7e12020-01-23 15:33:32 -07001072 refcount_set(&wq->use_refs, 1);
Jens Axboeb60fda62019-11-19 08:37:07 -07001073 reinit_completion(&wq->done);
Jens Axboe771b53d02019-10-22 10:25:58 -06001074 return wq;
1075 }
1076
1077 ret = PTR_ERR(wq->manager);
Jens Axboe771b53d02019-10-22 10:25:58 -06001078 complete(&wq->done);
Jens Axboeb60fda62019-11-19 08:37:07 -07001079err:
Jann Horn3fc50ab2019-11-26 19:10:20 +01001080 for_each_node(node)
1081 kfree(wq->wqes[node]);
Jens Axboeb60fda62019-11-19 08:37:07 -07001082 kfree(wq->wqes);
1083 kfree(wq);
Jens Axboe771b53d02019-10-22 10:25:58 -06001084 return ERR_PTR(ret);
1085}
1086
Pavel Begunkoveba6f5a2020-01-28 03:15:47 +03001087bool io_wq_get(struct io_wq *wq, struct io_wq_data *data)
1088{
Pavel Begunkove9fd9392020-03-04 16:14:12 +03001089 if (data->free_work != wq->free_work)
Pavel Begunkoveba6f5a2020-01-28 03:15:47 +03001090 return false;
1091
1092 return refcount_inc_not_zero(&wq->use_refs);
1093}
1094
Jens Axboe771b53d02019-10-22 10:25:58 -06001095static bool io_wq_worker_wake(struct io_worker *worker, void *data)
1096{
1097 wake_up_process(worker->task);
1098 return false;
1099}
1100
Jens Axboe848f7e12020-01-23 15:33:32 -07001101static void __io_wq_destroy(struct io_wq *wq)
Jens Axboe771b53d02019-10-22 10:25:58 -06001102{
Jann Horn3fc50ab2019-11-26 19:10:20 +01001103 int node;
Jens Axboe771b53d02019-10-22 10:25:58 -06001104
Jens Axboeb60fda62019-11-19 08:37:07 -07001105 set_bit(IO_WQ_BIT_EXIT, &wq->state);
1106 if (wq->manager)
Jens Axboe771b53d02019-10-22 10:25:58 -06001107 kthread_stop(wq->manager);
Jens Axboe771b53d02019-10-22 10:25:58 -06001108
1109 rcu_read_lock();
Jann Horn3fc50ab2019-11-26 19:10:20 +01001110 for_each_node(node)
1111 io_wq_for_each_worker(wq->wqes[node], io_wq_worker_wake, NULL);
Jens Axboe771b53d02019-10-22 10:25:58 -06001112 rcu_read_unlock();
1113
1114 wait_for_completion(&wq->done);
1115
Jann Horn3fc50ab2019-11-26 19:10:20 +01001116 for_each_node(node)
1117 kfree(wq->wqes[node]);
Jens Axboe771b53d02019-10-22 10:25:58 -06001118 kfree(wq->wqes);
1119 kfree(wq);
1120}
Jens Axboe848f7e12020-01-23 15:33:32 -07001121
1122void io_wq_destroy(struct io_wq *wq)
1123{
1124 if (refcount_dec_and_test(&wq->use_refs))
1125 __io_wq_destroy(wq);
1126}