blob: c36bbcd823ce3148d2b5263f8440ed5c571ad894 [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>
Jens Axboe771b53d02019-10-22 10:25:58 -060013#include <linux/sched/mm.h>
14#include <linux/percpu.h>
15#include <linux/slab.h>
16#include <linux/kthread.h>
17#include <linux/rculist_nulls.h>
Jens Axboe9392a272020-02-06 21:42:51 -070018#include <linux/fs_struct.h>
Jens Axboeaa96bf82020-04-03 11:26:26 -060019#include <linux/task_work.h>
Dennis Zhou91d8f512020-09-16 13:41:05 -070020#include <linux/blk-cgroup.h>
Jens Axboe4ea33a92020-10-15 13:46:44 -060021#include <linux/audit.h>
Jens Axboe43c01fb2020-10-22 09:02:50 -060022#include <linux/cpu.h>
Jens Axboe771b53d02019-10-22 10:25:58 -060023
Jens Axboe43c01fb2020-10-22 09:02:50 -060024#include "../kernel/sched/sched.h"
Jens Axboe771b53d02019-10-22 10:25:58 -060025#include "io-wq.h"
26
27#define WORKER_IDLE_TIMEOUT (5 * HZ)
28
29enum {
30 IO_WORKER_F_UP = 1, /* up and active */
31 IO_WORKER_F_RUNNING = 2, /* account as running */
32 IO_WORKER_F_FREE = 4, /* worker on free list */
Jens Axboe145cc8c2020-09-26 12:37:46 -060033 IO_WORKER_F_FIXED = 8, /* static idle worker */
34 IO_WORKER_F_BOUND = 16, /* is doing bounded work */
Jens Axboe771b53d02019-10-22 10:25:58 -060035};
36
37enum {
38 IO_WQ_BIT_EXIT = 0, /* wq exiting */
Jens Axboe446bc1c2020-12-20 10:47:42 -070039 IO_WQ_BIT_ERROR = 1, /* error on setup */
Jens Axboe771b53d02019-10-22 10:25:58 -060040};
41
42enum {
43 IO_WQE_FLAG_STALLED = 1, /* stalled on hash */
44};
45
46/*
47 * One for each thread in a wqe pool
48 */
49struct io_worker {
50 refcount_t ref;
51 unsigned flags;
52 struct hlist_nulls_node nulls_node;
Jens Axboee61df662019-11-13 13:54:49 -070053 struct list_head all_list;
Jens Axboe771b53d02019-10-22 10:25:58 -060054 struct task_struct *task;
Jens Axboe771b53d02019-10-22 10:25:58 -060055 struct io_wqe *wqe;
Jens Axboe36c2f922019-11-13 09:43:34 -070056
Jens Axboe771b53d02019-10-22 10:25:58 -060057 struct io_wq_work *cur_work;
Jens Axboe36c2f922019-11-13 09:43:34 -070058 spinlock_t lock;
Jens Axboe771b53d02019-10-22 10:25:58 -060059
60 struct rcu_head rcu;
61 struct mm_struct *mm;
Dennis Zhou91d8f512020-09-16 13:41:05 -070062#ifdef CONFIG_BLK_CGROUP
63 struct cgroup_subsys_state *blkcg_css;
64#endif
Jens Axboecccf0ee2020-01-27 16:34:48 -070065 const struct cred *cur_creds;
66 const struct cred *saved_creds;
Jens Axboe9b828492020-09-18 20:13:06 -060067 struct nsproxy *restore_nsproxy;
Jens Axboe771b53d02019-10-22 10:25:58 -060068};
69
Jens Axboe771b53d02019-10-22 10:25:58 -060070#if BITS_PER_LONG == 64
71#define IO_WQ_HASH_ORDER 6
72#else
73#define IO_WQ_HASH_ORDER 5
74#endif
75
Pavel Begunkov86f3cd12020-03-23 22:57:22 +030076#define IO_WQ_NR_HASH_BUCKETS (1u << IO_WQ_HASH_ORDER)
77
Jens Axboec5def4a2019-11-07 11:41:16 -070078struct io_wqe_acct {
79 unsigned nr_workers;
80 unsigned max_workers;
81 atomic_t nr_running;
82};
83
84enum {
85 IO_WQ_ACCT_BOUND,
86 IO_WQ_ACCT_UNBOUND,
87};
88
Jens Axboe771b53d02019-10-22 10:25:58 -060089/*
90 * Per-node worker thread pool
91 */
92struct io_wqe {
93 struct {
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +020094 raw_spinlock_t lock;
Jens Axboe6206f0e2019-11-26 11:59:32 -070095 struct io_wq_work_list work_list;
Jens Axboe771b53d02019-10-22 10:25:58 -060096 unsigned long hash_map;
97 unsigned flags;
98 } ____cacheline_aligned_in_smp;
99
100 int node;
Jens Axboec5def4a2019-11-07 11:41:16 -0700101 struct io_wqe_acct acct[2];
Jens Axboe771b53d02019-10-22 10:25:58 -0600102
Jens Axboe021d1cd2019-11-14 08:00:41 -0700103 struct hlist_nulls_head free_list;
Jens Axboee61df662019-11-13 13:54:49 -0700104 struct list_head all_list;
Jens Axboe771b53d02019-10-22 10:25:58 -0600105
106 struct io_wq *wq;
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300107 struct io_wq_work *hash_tail[IO_WQ_NR_HASH_BUCKETS];
Jens Axboe771b53d02019-10-22 10:25:58 -0600108};
109
110/*
111 * Per io_wq state
112 */
113struct io_wq {
114 struct io_wqe **wqes;
115 unsigned long state;
Jens Axboe771b53d02019-10-22 10:25:58 -0600116
Pavel Begunkove9fd9392020-03-04 16:14:12 +0300117 free_work_fn *free_work;
Pavel Begunkovf5fa38c2020-06-08 21:08:20 +0300118 io_wq_work_fn *do_work;
Jens Axboe7d723062019-11-12 22:31:31 -0700119
Jens Axboe771b53d02019-10-22 10:25:58 -0600120 struct task_struct *manager;
Jens Axboec5def4a2019-11-07 11:41:16 -0700121 struct user_struct *user;
Jens Axboe771b53d02019-10-22 10:25:58 -0600122 refcount_t refs;
123 struct completion done;
Jens Axboe848f7e12020-01-23 15:33:32 -0700124
Jens Axboe43c01fb2020-10-22 09:02:50 -0600125 struct hlist_node cpuhp_node;
126
Jens Axboe848f7e12020-01-23 15:33:32 -0700127 refcount_t use_refs;
Jens Axboe771b53d02019-10-22 10:25:58 -0600128};
129
Jens Axboe43c01fb2020-10-22 09:02:50 -0600130static enum cpuhp_state io_wq_online;
131
Jens Axboe771b53d02019-10-22 10:25:58 -0600132static bool io_worker_get(struct io_worker *worker)
133{
134 return refcount_inc_not_zero(&worker->ref);
135}
136
137static void io_worker_release(struct io_worker *worker)
138{
139 if (refcount_dec_and_test(&worker->ref))
140 wake_up_process(worker->task);
141}
142
143/*
144 * Note: drops the wqe->lock if returning true! The caller must re-acquire
145 * the lock in that case. Some callers need to restart handling if this
146 * happens, so we can't just re-acquire the lock on behalf of the caller.
147 */
148static bool __io_worker_unuse(struct io_wqe *wqe, struct io_worker *worker)
149{
Jens Axboefcb323c2019-10-24 12:39:47 -0600150 bool dropped_lock = false;
151
Jens Axboecccf0ee2020-01-27 16:34:48 -0700152 if (worker->saved_creds) {
153 revert_creds(worker->saved_creds);
154 worker->cur_creds = worker->saved_creds = NULL;
Jens Axboe181e4482019-11-25 08:52:30 -0700155 }
156
Jens Axboee06aa2e2021-02-12 14:02:54 -0700157 if (current->files) {
Jens Axboefcb323c2019-10-24 12:39:47 -0600158 __acquire(&wqe->lock);
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200159 raw_spin_unlock_irq(&wqe->lock);
Jens Axboefcb323c2019-10-24 12:39:47 -0600160 dropped_lock = true;
161
162 task_lock(current);
Jens Axboee06aa2e2021-02-12 14:02:54 -0700163 current->files = NULL;
Jens Axboe9b828492020-09-18 20:13:06 -0600164 current->nsproxy = worker->restore_nsproxy;
Jens Axboefcb323c2019-10-24 12:39:47 -0600165 task_unlock(current);
166 }
167
Jens Axboee06aa2e2021-02-12 14:02:54 -0700168 if (current->fs)
169 current->fs = NULL;
Jens Axboe9392a272020-02-06 21:42:51 -0700170
Jens Axboe771b53d02019-10-22 10:25:58 -0600171 /*
172 * If we have an active mm, we need to drop the wq lock before unusing
173 * it. If we do, return true and let the caller retry the idle loop.
174 */
175 if (worker->mm) {
Jens Axboefcb323c2019-10-24 12:39:47 -0600176 if (!dropped_lock) {
177 __acquire(&wqe->lock);
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200178 raw_spin_unlock_irq(&wqe->lock);
Jens Axboefcb323c2019-10-24 12:39:47 -0600179 dropped_lock = true;
180 }
Jens Axboe771b53d02019-10-22 10:25:58 -0600181 __set_current_state(TASK_RUNNING);
Christoph Hellwigf5678e72020-06-10 18:42:06 -0700182 kthread_unuse_mm(worker->mm);
Jens Axboe771b53d02019-10-22 10:25:58 -0600183 mmput(worker->mm);
184 worker->mm = NULL;
Jens Axboe771b53d02019-10-22 10:25:58 -0600185 }
186
Dennis Zhou91d8f512020-09-16 13:41:05 -0700187#ifdef CONFIG_BLK_CGROUP
188 if (worker->blkcg_css) {
189 kthread_associate_blkcg(NULL);
190 worker->blkcg_css = NULL;
191 }
192#endif
Jens Axboe69228332020-10-20 14:28:41 -0600193 if (current->signal->rlim[RLIMIT_FSIZE].rlim_cur != RLIM_INFINITY)
194 current->signal->rlim[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
Jens Axboefcb323c2019-10-24 12:39:47 -0600195 return dropped_lock;
Jens Axboe771b53d02019-10-22 10:25:58 -0600196}
197
Jens Axboec5def4a2019-11-07 11:41:16 -0700198static inline struct io_wqe_acct *io_work_get_acct(struct io_wqe *wqe,
199 struct io_wq_work *work)
200{
201 if (work->flags & IO_WQ_WORK_UNBOUND)
202 return &wqe->acct[IO_WQ_ACCT_UNBOUND];
203
204 return &wqe->acct[IO_WQ_ACCT_BOUND];
205}
206
207static inline struct io_wqe_acct *io_wqe_get_acct(struct io_wqe *wqe,
208 struct io_worker *worker)
209{
210 if (worker->flags & IO_WORKER_F_BOUND)
211 return &wqe->acct[IO_WQ_ACCT_BOUND];
212
213 return &wqe->acct[IO_WQ_ACCT_UNBOUND];
214}
215
Jens Axboe771b53d02019-10-22 10:25:58 -0600216static void io_worker_exit(struct io_worker *worker)
217{
218 struct io_wqe *wqe = worker->wqe;
Jens Axboec5def4a2019-11-07 11:41:16 -0700219 struct io_wqe_acct *acct = io_wqe_get_acct(wqe, worker);
Jens Axboe771b53d02019-10-22 10:25:58 -0600220
221 /*
222 * If we're not at zero, someone else is holding a brief reference
223 * to the worker. Wait for that to go away.
224 */
225 set_current_state(TASK_INTERRUPTIBLE);
226 if (!refcount_dec_and_test(&worker->ref))
227 schedule();
228 __set_current_state(TASK_RUNNING);
229
230 preempt_disable();
231 current->flags &= ~PF_IO_WORKER;
232 if (worker->flags & IO_WORKER_F_RUNNING)
Jens Axboec5def4a2019-11-07 11:41:16 -0700233 atomic_dec(&acct->nr_running);
234 if (!(worker->flags & IO_WORKER_F_BOUND))
235 atomic_dec(&wqe->wq->user->processes);
Jens Axboe771b53d02019-10-22 10:25:58 -0600236 worker->flags = 0;
237 preempt_enable();
238
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200239 raw_spin_lock_irq(&wqe->lock);
Jens Axboe771b53d02019-10-22 10:25:58 -0600240 hlist_nulls_del_rcu(&worker->nulls_node);
Jens Axboee61df662019-11-13 13:54:49 -0700241 list_del_rcu(&worker->all_list);
Jens Axboe771b53d02019-10-22 10:25:58 -0600242 if (__io_worker_unuse(wqe, worker)) {
243 __release(&wqe->lock);
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200244 raw_spin_lock_irq(&wqe->lock);
Jens Axboe771b53d02019-10-22 10:25:58 -0600245 }
Jens Axboec5def4a2019-11-07 11:41:16 -0700246 acct->nr_workers--;
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200247 raw_spin_unlock_irq(&wqe->lock);
Jens Axboe771b53d02019-10-22 10:25:58 -0600248
YueHaibing364b05f2019-11-02 15:55:01 +0800249 kfree_rcu(worker, rcu);
Hillf Dantonc4068bf2020-09-26 21:26:55 +0800250 if (refcount_dec_and_test(&wqe->wq->refs))
251 complete(&wqe->wq->done);
Jens Axboe771b53d02019-10-22 10:25:58 -0600252}
253
Jens Axboec5def4a2019-11-07 11:41:16 -0700254static inline bool io_wqe_run_queue(struct io_wqe *wqe)
255 __must_hold(wqe->lock)
256{
Jens Axboe6206f0e2019-11-26 11:59:32 -0700257 if (!wq_list_empty(&wqe->work_list) &&
258 !(wqe->flags & IO_WQE_FLAG_STALLED))
Jens Axboec5def4a2019-11-07 11:41:16 -0700259 return true;
260 return false;
261}
262
263/*
264 * Check head of free list for an available worker. If one isn't available,
265 * caller must wake up the wq manager to create one.
266 */
267static bool io_wqe_activate_free_worker(struct io_wqe *wqe)
268 __must_hold(RCU)
269{
270 struct hlist_nulls_node *n;
271 struct io_worker *worker;
272
Jens Axboe021d1cd2019-11-14 08:00:41 -0700273 n = rcu_dereference(hlist_nulls_first_rcu(&wqe->free_list));
Jens Axboec5def4a2019-11-07 11:41:16 -0700274 if (is_a_nulls(n))
275 return false;
276
277 worker = hlist_nulls_entry(n, struct io_worker, nulls_node);
278 if (io_worker_get(worker)) {
Jens Axboe506d95f2019-12-07 21:03:59 -0700279 wake_up_process(worker->task);
Jens Axboec5def4a2019-11-07 11:41:16 -0700280 io_worker_release(worker);
281 return true;
282 }
283
284 return false;
285}
286
287/*
288 * We need a worker. If we find a free one, we're good. If not, and we're
289 * below the max number of workers, wake up the manager to create one.
290 */
291static void io_wqe_wake_worker(struct io_wqe *wqe, struct io_wqe_acct *acct)
292{
293 bool ret;
294
295 /*
296 * Most likely an attempt to queue unbounded work on an io_wq that
297 * wasn't setup with any unbounded workers.
298 */
299 WARN_ON_ONCE(!acct->max_workers);
300
301 rcu_read_lock();
302 ret = io_wqe_activate_free_worker(wqe);
303 rcu_read_unlock();
304
305 if (!ret && acct->nr_workers < acct->max_workers)
306 wake_up_process(wqe->wq->manager);
307}
308
309static void io_wqe_inc_running(struct io_wqe *wqe, struct io_worker *worker)
310{
311 struct io_wqe_acct *acct = io_wqe_get_acct(wqe, worker);
312
313 atomic_inc(&acct->nr_running);
314}
315
316static void io_wqe_dec_running(struct io_wqe *wqe, struct io_worker *worker)
317 __must_hold(wqe->lock)
318{
319 struct io_wqe_acct *acct = io_wqe_get_acct(wqe, worker);
320
321 if (atomic_dec_and_test(&acct->nr_running) && io_wqe_run_queue(wqe))
322 io_wqe_wake_worker(wqe, acct);
323}
324
Jens Axboe771b53d02019-10-22 10:25:58 -0600325static void io_worker_start(struct io_wqe *wqe, struct io_worker *worker)
326{
327 allow_kernel_signal(SIGINT);
328
329 current->flags |= PF_IO_WORKER;
Jens Axboee06aa2e2021-02-12 14:02:54 -0700330 current->fs = NULL;
331 current->files = NULL;
Jens Axboe771b53d02019-10-22 10:25:58 -0600332
333 worker->flags |= (IO_WORKER_F_UP | IO_WORKER_F_RUNNING);
Jens Axboe9b828492020-09-18 20:13:06 -0600334 worker->restore_nsproxy = current->nsproxy;
Jens Axboec5def4a2019-11-07 11:41:16 -0700335 io_wqe_inc_running(wqe, worker);
Jens Axboe771b53d02019-10-22 10:25:58 -0600336}
337
338/*
339 * Worker will start processing some work. Move it to the busy list, if
340 * it's currently on the freelist
341 */
342static void __io_worker_busy(struct io_wqe *wqe, struct io_worker *worker,
343 struct io_wq_work *work)
344 __must_hold(wqe->lock)
345{
Jens Axboec5def4a2019-11-07 11:41:16 -0700346 bool worker_bound, work_bound;
347
Jens Axboe771b53d02019-10-22 10:25:58 -0600348 if (worker->flags & IO_WORKER_F_FREE) {
349 worker->flags &= ~IO_WORKER_F_FREE;
350 hlist_nulls_del_init_rcu(&worker->nulls_node);
Jens Axboe771b53d02019-10-22 10:25:58 -0600351 }
Jens Axboec5def4a2019-11-07 11:41:16 -0700352
353 /*
354 * If worker is moving from bound to unbound (or vice versa), then
355 * ensure we update the running accounting.
356 */
Dan Carpenterb2e9c7d62019-11-19 09:22:16 +0300357 worker_bound = (worker->flags & IO_WORKER_F_BOUND) != 0;
358 work_bound = (work->flags & IO_WQ_WORK_UNBOUND) == 0;
359 if (worker_bound != work_bound) {
Jens Axboec5def4a2019-11-07 11:41:16 -0700360 io_wqe_dec_running(wqe, worker);
361 if (work_bound) {
362 worker->flags |= IO_WORKER_F_BOUND;
363 wqe->acct[IO_WQ_ACCT_UNBOUND].nr_workers--;
364 wqe->acct[IO_WQ_ACCT_BOUND].nr_workers++;
365 atomic_dec(&wqe->wq->user->processes);
366 } else {
367 worker->flags &= ~IO_WORKER_F_BOUND;
368 wqe->acct[IO_WQ_ACCT_UNBOUND].nr_workers++;
369 wqe->acct[IO_WQ_ACCT_BOUND].nr_workers--;
370 atomic_inc(&wqe->wq->user->processes);
371 }
372 io_wqe_inc_running(wqe, worker);
373 }
Jens Axboe771b53d02019-10-22 10:25:58 -0600374}
375
376/*
377 * No work, worker going to sleep. Move to freelist, and unuse mm if we
378 * have one attached. Dropping the mm may potentially sleep, so we drop
379 * the lock in that case and return success. Since the caller has to
380 * retry the loop in that case (we changed task state), we don't regrab
381 * the lock if we return success.
382 */
383static bool __io_worker_idle(struct io_wqe *wqe, struct io_worker *worker)
384 __must_hold(wqe->lock)
385{
386 if (!(worker->flags & IO_WORKER_F_FREE)) {
387 worker->flags |= IO_WORKER_F_FREE;
Jens Axboe021d1cd2019-11-14 08:00:41 -0700388 hlist_nulls_add_head_rcu(&worker->nulls_node, &wqe->free_list);
Jens Axboe771b53d02019-10-22 10:25:58 -0600389 }
390
391 return __io_worker_unuse(wqe, worker);
392}
393
Pavel Begunkov60cf46a2020-03-14 00:31:05 +0300394static inline unsigned int io_get_work_hash(struct io_wq_work *work)
395{
396 return work->flags >> IO_WQ_HASH_SHIFT;
397}
398
399static struct io_wq_work *io_get_next_work(struct io_wqe *wqe)
Jens Axboe771b53d02019-10-22 10:25:58 -0600400 __must_hold(wqe->lock)
401{
Jens Axboe6206f0e2019-11-26 11:59:32 -0700402 struct io_wq_work_node *node, *prev;
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300403 struct io_wq_work *work, *tail;
Pavel Begunkov60cf46a2020-03-14 00:31:05 +0300404 unsigned int hash;
Jens Axboe771b53d02019-10-22 10:25:58 -0600405
Jens Axboe6206f0e2019-11-26 11:59:32 -0700406 wq_list_for_each(node, prev, &wqe->work_list) {
407 work = container_of(node, struct io_wq_work, list);
408
Jens Axboe771b53d02019-10-22 10:25:58 -0600409 /* not hashed, can run anytime */
Pavel Begunkov8766dd52020-03-14 00:31:04 +0300410 if (!io_wq_is_hashed(work)) {
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300411 wq_list_del(&wqe->work_list, node, prev);
Jens Axboe771b53d02019-10-22 10:25:58 -0600412 return work;
413 }
414
415 /* hashed, can run if not already running */
Pavel Begunkov60cf46a2020-03-14 00:31:05 +0300416 hash = io_get_work_hash(work);
417 if (!(wqe->hash_map & BIT(hash))) {
418 wqe->hash_map |= BIT(hash);
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300419 /* all items with this hash lie in [work, tail] */
420 tail = wqe->hash_tail[hash];
421 wqe->hash_tail[hash] = NULL;
422 wq_list_cut(&wqe->work_list, &tail->list, prev);
Jens Axboe771b53d02019-10-22 10:25:58 -0600423 return work;
424 }
425 }
426
427 return NULL;
428}
429
Jens Axboecccf0ee2020-01-27 16:34:48 -0700430static void io_wq_switch_mm(struct io_worker *worker, struct io_wq_work *work)
431{
432 if (worker->mm) {
Christoph Hellwigf5678e72020-06-10 18:42:06 -0700433 kthread_unuse_mm(worker->mm);
Jens Axboecccf0ee2020-01-27 16:34:48 -0700434 mmput(worker->mm);
435 worker->mm = NULL;
436 }
Christoph Hellwig37c54f92020-06-10 18:42:10 -0700437
Jens Axboe98447d62020-10-14 10:48:51 -0600438 if (mmget_not_zero(work->identity->mm)) {
439 kthread_use_mm(work->identity->mm);
440 worker->mm = work->identity->mm;
Jens Axboecccf0ee2020-01-27 16:34:48 -0700441 return;
442 }
443
444 /* failed grabbing mm, ensure work gets cancelled */
445 work->flags |= IO_WQ_WORK_CANCEL;
446}
447
Dennis Zhou91d8f512020-09-16 13:41:05 -0700448static inline void io_wq_switch_blkcg(struct io_worker *worker,
449 struct io_wq_work *work)
450{
451#ifdef CONFIG_BLK_CGROUP
Jens Axboe0f203762020-10-14 09:23:55 -0600452 if (!(work->flags & IO_WQ_WORK_BLKCG))
453 return;
Jens Axboe98447d62020-10-14 10:48:51 -0600454 if (work->identity->blkcg_css != worker->blkcg_css) {
455 kthread_associate_blkcg(work->identity->blkcg_css);
456 worker->blkcg_css = work->identity->blkcg_css;
Dennis Zhou91d8f512020-09-16 13:41:05 -0700457 }
458#endif
459}
460
Jens Axboecccf0ee2020-01-27 16:34:48 -0700461static void io_wq_switch_creds(struct io_worker *worker,
462 struct io_wq_work *work)
463{
Jens Axboe98447d62020-10-14 10:48:51 -0600464 const struct cred *old_creds = override_creds(work->identity->creds);
Jens Axboecccf0ee2020-01-27 16:34:48 -0700465
Jens Axboe98447d62020-10-14 10:48:51 -0600466 worker->cur_creds = work->identity->creds;
Jens Axboecccf0ee2020-01-27 16:34:48 -0700467 if (worker->saved_creds)
468 put_cred(old_creds); /* creds set by previous switch */
469 else
470 worker->saved_creds = old_creds;
471}
472
Pavel Begunkovdc026a72020-03-04 16:14:09 +0300473static void io_impersonate_work(struct io_worker *worker,
474 struct io_wq_work *work)
475{
Jens Axboe98447d62020-10-14 10:48:51 -0600476 if ((work->flags & IO_WQ_WORK_FILES) &&
477 current->files != work->identity->files) {
Pavel Begunkovdc026a72020-03-04 16:14:09 +0300478 task_lock(current);
Jens Axboe98447d62020-10-14 10:48:51 -0600479 current->files = work->identity->files;
480 current->nsproxy = work->identity->nsproxy;
Pavel Begunkovdc026a72020-03-04 16:14:09 +0300481 task_unlock(current);
Jens Axboe3dd16802020-10-30 09:36:41 -0600482 if (!work->identity->files) {
483 /* failed grabbing files, ensure work gets cancelled */
484 work->flags |= IO_WQ_WORK_CANCEL;
485 }
Pavel Begunkovdc026a72020-03-04 16:14:09 +0300486 }
Jens Axboe98447d62020-10-14 10:48:51 -0600487 if ((work->flags & IO_WQ_WORK_FS) && current->fs != work->identity->fs)
488 current->fs = work->identity->fs;
489 if ((work->flags & IO_WQ_WORK_MM) && work->identity->mm != worker->mm)
Pavel Begunkovdc026a72020-03-04 16:14:09 +0300490 io_wq_switch_mm(worker, work);
Jens Axboe98447d62020-10-14 10:48:51 -0600491 if ((work->flags & IO_WQ_WORK_CREDS) &&
492 worker->cur_creds != work->identity->creds)
Pavel Begunkovdc026a72020-03-04 16:14:09 +0300493 io_wq_switch_creds(worker, work);
Jens Axboe69228332020-10-20 14:28:41 -0600494 if (work->flags & IO_WQ_WORK_FSIZE)
495 current->signal->rlim[RLIMIT_FSIZE].rlim_cur = work->identity->fsize;
496 else if (current->signal->rlim[RLIMIT_FSIZE].rlim_cur != RLIM_INFINITY)
497 current->signal->rlim[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
Dennis Zhou91d8f512020-09-16 13:41:05 -0700498 io_wq_switch_blkcg(worker, work);
Jens Axboe4ea33a92020-10-15 13:46:44 -0600499#ifdef CONFIG_AUDIT
500 current->loginuid = work->identity->loginuid;
501 current->sessionid = work->identity->sessionid;
502#endif
Pavel Begunkovdc026a72020-03-04 16:14:09 +0300503}
504
505static void io_assign_current_work(struct io_worker *worker,
506 struct io_wq_work *work)
507{
Pavel Begunkovd78298e2020-03-14 00:31:03 +0300508 if (work) {
509 /* flush pending signals before assigning new work */
510 if (signal_pending(current))
511 flush_signals(current);
512 cond_resched();
513 }
Pavel Begunkovdc026a72020-03-04 16:14:09 +0300514
Jens Axboe4ea33a92020-10-15 13:46:44 -0600515#ifdef CONFIG_AUDIT
516 current->loginuid = KUIDT_INIT(AUDIT_UID_UNSET);
517 current->sessionid = AUDIT_SID_UNSET;
518#endif
519
Pavel Begunkovdc026a72020-03-04 16:14:09 +0300520 spin_lock_irq(&worker->lock);
521 worker->cur_work = work;
522 spin_unlock_irq(&worker->lock);
523}
524
Pavel Begunkov60cf46a2020-03-14 00:31:05 +0300525static void io_wqe_enqueue(struct io_wqe *wqe, struct io_wq_work *work);
526
Jens Axboe771b53d02019-10-22 10:25:58 -0600527static void io_worker_handle_work(struct io_worker *worker)
528 __releases(wqe->lock)
529{
Jens Axboe771b53d02019-10-22 10:25:58 -0600530 struct io_wqe *wqe = worker->wqe;
531 struct io_wq *wq = wqe->wq;
532
533 do {
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300534 struct io_wq_work *work;
Pavel Begunkovf462fd32020-03-04 16:14:11 +0300535get_next:
Jens Axboe771b53d02019-10-22 10:25:58 -0600536 /*
Jens Axboe771b53d02019-10-22 10:25:58 -0600537 * If we got some work, mark us as busy. If we didn't, but
538 * the list isn't empty, it means we stalled on hashed work.
539 * Mark us stalled so we don't keep looking for work when we
540 * can't make progress, any work completion or insertion will
541 * clear the stalled flag.
542 */
Pavel Begunkov60cf46a2020-03-14 00:31:05 +0300543 work = io_get_next_work(wqe);
Jens Axboe771b53d02019-10-22 10:25:58 -0600544 if (work)
545 __io_worker_busy(wqe, worker, work);
Jens Axboe6206f0e2019-11-26 11:59:32 -0700546 else if (!wq_list_empty(&wqe->work_list))
Jens Axboe771b53d02019-10-22 10:25:58 -0600547 wqe->flags |= IO_WQE_FLAG_STALLED;
548
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200549 raw_spin_unlock_irq(&wqe->lock);
Jens Axboe771b53d02019-10-22 10:25:58 -0600550 if (!work)
551 break;
Pavel Begunkov58e39312020-03-04 16:14:10 +0300552 io_assign_current_work(worker, work);
Jens Axboe36c2f922019-11-13 09:43:34 -0700553
Pavel Begunkovdc026a72020-03-04 16:14:09 +0300554 /* handle a whole dependent link */
555 do {
Pavel Begunkov5280f7e2021-02-04 13:52:08 +0000556 struct io_wq_work *next_hashed, *linked;
Pavel Begunkovb089ed392020-07-25 14:42:00 +0300557 unsigned int hash = io_get_work_hash(work);
Hillf Dantonfd1c4bc2019-12-24 09:14:29 -0700558
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300559 next_hashed = wq_next_work(work);
Pavel Begunkov58e39312020-03-04 16:14:10 +0300560 io_impersonate_work(worker, work);
Pavel Begunkov5280f7e2021-02-04 13:52:08 +0000561 wq->do_work(work);
562 io_assign_current_work(worker, NULL);
Jens Axboe36c2f922019-11-13 09:43:34 -0700563
Pavel Begunkov5280f7e2021-02-04 13:52:08 +0000564 linked = wq->free_work(work);
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300565 work = next_hashed;
566 if (!work && linked && !io_wq_is_hashed(linked)) {
567 work = linked;
568 linked = NULL;
569 }
570 io_assign_current_work(worker, work);
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300571 if (linked)
572 io_wqe_enqueue(wqe, linked);
573
574 if (hash != -1U && !next_hashed) {
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200575 raw_spin_lock_irq(&wqe->lock);
Pavel Begunkovdc026a72020-03-04 16:14:09 +0300576 wqe->hash_map &= ~BIT_ULL(hash);
577 wqe->flags &= ~IO_WQE_FLAG_STALLED;
Pavel Begunkovf462fd32020-03-04 16:14:11 +0300578 /* skip unnecessary unlock-lock wqe->lock */
579 if (!work)
580 goto get_next;
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200581 raw_spin_unlock_irq(&wqe->lock);
Pavel Begunkovdc026a72020-03-04 16:14:09 +0300582 }
Pavel Begunkov58e39312020-03-04 16:14:10 +0300583 } while (work);
Jens Axboe36c2f922019-11-13 09:43:34 -0700584
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200585 raw_spin_lock_irq(&wqe->lock);
Jens Axboe771b53d02019-10-22 10:25:58 -0600586 } while (1);
587}
588
Jens Axboe771b53d02019-10-22 10:25:58 -0600589static int io_wqe_worker(void *data)
590{
591 struct io_worker *worker = data;
592 struct io_wqe *wqe = worker->wqe;
593 struct io_wq *wq = wqe->wq;
Jens Axboe771b53d02019-10-22 10:25:58 -0600594
595 io_worker_start(wqe, worker);
596
597 while (!test_bit(IO_WQ_BIT_EXIT, &wq->state)) {
Jens Axboe506d95f2019-12-07 21:03:59 -0700598 set_current_state(TASK_INTERRUPTIBLE);
Jens Axboee995d512019-12-07 21:06:46 -0700599loop:
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200600 raw_spin_lock_irq(&wqe->lock);
Jens Axboe771b53d02019-10-22 10:25:58 -0600601 if (io_wqe_run_queue(wqe)) {
602 __set_current_state(TASK_RUNNING);
603 io_worker_handle_work(worker);
Jens Axboee995d512019-12-07 21:06:46 -0700604 goto loop;
Jens Axboe771b53d02019-10-22 10:25:58 -0600605 }
606 /* drops the lock on success, retry */
607 if (__io_worker_idle(wqe, worker)) {
608 __release(&wqe->lock);
Jens Axboee995d512019-12-07 21:06:46 -0700609 goto loop;
Jens Axboe771b53d02019-10-22 10:25:58 -0600610 }
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200611 raw_spin_unlock_irq(&wqe->lock);
Jens Axboe771b53d02019-10-22 10:25:58 -0600612 if (signal_pending(current))
613 flush_signals(current);
614 if (schedule_timeout(WORKER_IDLE_TIMEOUT))
615 continue;
616 /* timed out, exit unless we're the fixed worker */
617 if (test_bit(IO_WQ_BIT_EXIT, &wq->state) ||
618 !(worker->flags & IO_WORKER_F_FIXED))
619 break;
620 }
621
Jens Axboe771b53d02019-10-22 10:25:58 -0600622 if (test_bit(IO_WQ_BIT_EXIT, &wq->state)) {
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200623 raw_spin_lock_irq(&wqe->lock);
Jens Axboe6206f0e2019-11-26 11:59:32 -0700624 if (!wq_list_empty(&wqe->work_list))
Jens Axboe771b53d02019-10-22 10:25:58 -0600625 io_worker_handle_work(worker);
626 else
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200627 raw_spin_unlock_irq(&wqe->lock);
Jens Axboe771b53d02019-10-22 10:25:58 -0600628 }
629
630 io_worker_exit(worker);
631 return 0;
632}
633
634/*
Jens Axboe771b53d02019-10-22 10:25:58 -0600635 * Called when a worker is scheduled in. Mark us as currently running.
636 */
637void io_wq_worker_running(struct task_struct *tsk)
638{
639 struct io_worker *worker = kthread_data(tsk);
640 struct io_wqe *wqe = worker->wqe;
641
642 if (!(worker->flags & IO_WORKER_F_UP))
643 return;
644 if (worker->flags & IO_WORKER_F_RUNNING)
645 return;
646 worker->flags |= IO_WORKER_F_RUNNING;
Jens Axboec5def4a2019-11-07 11:41:16 -0700647 io_wqe_inc_running(wqe, worker);
Jens Axboe771b53d02019-10-22 10:25:58 -0600648}
649
650/*
651 * Called when worker is going to sleep. If there are no workers currently
652 * running and we have work pending, wake up a free one or have the manager
653 * set one up.
654 */
655void io_wq_worker_sleeping(struct task_struct *tsk)
656{
657 struct io_worker *worker = kthread_data(tsk);
658 struct io_wqe *wqe = worker->wqe;
659
660 if (!(worker->flags & IO_WORKER_F_UP))
661 return;
662 if (!(worker->flags & IO_WORKER_F_RUNNING))
663 return;
664
665 worker->flags &= ~IO_WORKER_F_RUNNING;
666
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200667 raw_spin_lock_irq(&wqe->lock);
Jens Axboec5def4a2019-11-07 11:41:16 -0700668 io_wqe_dec_running(wqe, worker);
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200669 raw_spin_unlock_irq(&wqe->lock);
Jens Axboe771b53d02019-10-22 10:25:58 -0600670}
671
Jens Axboeb60fda62019-11-19 08:37:07 -0700672static bool create_io_worker(struct io_wq *wq, struct io_wqe *wqe, int index)
Jens Axboe771b53d02019-10-22 10:25:58 -0600673{
Hillf Dantonc4068bf2020-09-26 21:26:55 +0800674 struct io_wqe_acct *acct = &wqe->acct[index];
Jens Axboe771b53d02019-10-22 10:25:58 -0600675 struct io_worker *worker;
676
Jann Hornad6e0052019-11-26 17:39:45 +0100677 worker = kzalloc_node(sizeof(*worker), GFP_KERNEL, wqe->node);
Jens Axboe771b53d02019-10-22 10:25:58 -0600678 if (!worker)
Jens Axboeb60fda62019-11-19 08:37:07 -0700679 return false;
Jens Axboe771b53d02019-10-22 10:25:58 -0600680
681 refcount_set(&worker->ref, 1);
682 worker->nulls_node.pprev = NULL;
Jens Axboe771b53d02019-10-22 10:25:58 -0600683 worker->wqe = wqe;
Jens Axboe36c2f922019-11-13 09:43:34 -0700684 spin_lock_init(&worker->lock);
Jens Axboe771b53d02019-10-22 10:25:58 -0600685
686 worker->task = kthread_create_on_node(io_wqe_worker, worker, wqe->node,
Jens Axboec5def4a2019-11-07 11:41:16 -0700687 "io_wqe_worker-%d/%d", index, wqe->node);
Jens Axboe771b53d02019-10-22 10:25:58 -0600688 if (IS_ERR(worker->task)) {
689 kfree(worker);
Jens Axboeb60fda62019-11-19 08:37:07 -0700690 return false;
Jens Axboe771b53d02019-10-22 10:25:58 -0600691 }
Jens Axboea8b595b2020-10-15 10:13:07 -0600692 kthread_bind_mask(worker->task, cpumask_of_node(wqe->node));
Jens Axboe771b53d02019-10-22 10:25:58 -0600693
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200694 raw_spin_lock_irq(&wqe->lock);
Jens Axboe021d1cd2019-11-14 08:00:41 -0700695 hlist_nulls_add_head_rcu(&worker->nulls_node, &wqe->free_list);
Jens Axboee61df662019-11-13 13:54:49 -0700696 list_add_tail_rcu(&worker->all_list, &wqe->all_list);
Jens Axboe771b53d02019-10-22 10:25:58 -0600697 worker->flags |= IO_WORKER_F_FREE;
Jens Axboec5def4a2019-11-07 11:41:16 -0700698 if (index == IO_WQ_ACCT_BOUND)
699 worker->flags |= IO_WORKER_F_BOUND;
700 if (!acct->nr_workers && (worker->flags & IO_WORKER_F_BOUND))
Jens Axboe771b53d02019-10-22 10:25:58 -0600701 worker->flags |= IO_WORKER_F_FIXED;
Jens Axboec5def4a2019-11-07 11:41:16 -0700702 acct->nr_workers++;
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200703 raw_spin_unlock_irq(&wqe->lock);
Jens Axboe771b53d02019-10-22 10:25:58 -0600704
Jens Axboec5def4a2019-11-07 11:41:16 -0700705 if (index == IO_WQ_ACCT_UNBOUND)
706 atomic_inc(&wq->user->processes);
707
Hillf Dantonc4068bf2020-09-26 21:26:55 +0800708 refcount_inc(&wq->refs);
Jens Axboe771b53d02019-10-22 10:25:58 -0600709 wake_up_process(worker->task);
Jens Axboeb60fda62019-11-19 08:37:07 -0700710 return true;
Jens Axboe771b53d02019-10-22 10:25:58 -0600711}
712
Jens Axboec5def4a2019-11-07 11:41:16 -0700713static inline bool io_wqe_need_worker(struct io_wqe *wqe, int index)
Jens Axboe771b53d02019-10-22 10:25:58 -0600714 __must_hold(wqe->lock)
715{
Jens Axboec5def4a2019-11-07 11:41:16 -0700716 struct io_wqe_acct *acct = &wqe->acct[index];
Jens Axboe771b53d02019-10-22 10:25:58 -0600717
Jens Axboec5def4a2019-11-07 11:41:16 -0700718 /* if we have available workers or no work, no need */
Jens Axboe021d1cd2019-11-14 08:00:41 -0700719 if (!hlist_nulls_empty(&wqe->free_list) || !io_wqe_run_queue(wqe))
Jens Axboec5def4a2019-11-07 11:41:16 -0700720 return false;
721 return acct->nr_workers < acct->max_workers;
Jens Axboe771b53d02019-10-22 10:25:58 -0600722}
723
Hillf Dantonc4068bf2020-09-26 21:26:55 +0800724/*
725 * Iterate the passed in list and call the specific function for each
726 * worker that isn't exiting
727 */
728static bool io_wq_for_each_worker(struct io_wqe *wqe,
729 bool (*func)(struct io_worker *, void *),
730 void *data)
731{
732 struct io_worker *worker;
733 bool ret = false;
734
735 list_for_each_entry_rcu(worker, &wqe->all_list, all_list) {
736 if (io_worker_get(worker)) {
737 /* no task if node is/was offline */
738 if (worker->task)
739 ret = func(worker, data);
740 io_worker_release(worker);
741 if (ret)
742 break;
743 }
744 }
745
746 return ret;
747}
748
749static bool io_wq_worker_wake(struct io_worker *worker, void *data)
750{
751 wake_up_process(worker->task);
752 return false;
753}
754
Jens Axboe771b53d02019-10-22 10:25:58 -0600755/*
756 * Manager thread. Tasked with creating new workers, if we need them.
757 */
758static int io_wq_manager(void *data)
759{
760 struct io_wq *wq = data;
Jann Horn3fc50ab2019-11-26 19:10:20 +0100761 int node;
Jens Axboeb60fda62019-11-19 08:37:07 -0700762
763 /* create fixed workers */
Hillf Dantonc4068bf2020-09-26 21:26:55 +0800764 refcount_set(&wq->refs, 1);
Jann Horn3fc50ab2019-11-26 19:10:20 +0100765 for_each_node(node) {
Jens Axboe75634392020-02-11 06:30:06 -0700766 if (!node_online(node))
767 continue;
Hillf Dantonc4068bf2020-09-26 21:26:55 +0800768 if (create_io_worker(wq, wq->wqes[node], IO_WQ_ACCT_BOUND))
769 continue;
770 set_bit(IO_WQ_BIT_ERROR, &wq->state);
771 set_bit(IO_WQ_BIT_EXIT, &wq->state);
772 goto out;
Jens Axboeb60fda62019-11-19 08:37:07 -0700773 }
774
775 complete(&wq->done);
Jens Axboe771b53d02019-10-22 10:25:58 -0600776
777 while (!kthread_should_stop()) {
Jens Axboeaa96bf82020-04-03 11:26:26 -0600778 if (current->task_works)
779 task_work_run();
780
Jann Horn3fc50ab2019-11-26 19:10:20 +0100781 for_each_node(node) {
782 struct io_wqe *wqe = wq->wqes[node];
Jens Axboec5def4a2019-11-07 11:41:16 -0700783 bool fork_worker[2] = { false, false };
Jens Axboe771b53d02019-10-22 10:25:58 -0600784
Jens Axboe75634392020-02-11 06:30:06 -0700785 if (!node_online(node))
786 continue;
787
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200788 raw_spin_lock_irq(&wqe->lock);
Jens Axboec5def4a2019-11-07 11:41:16 -0700789 if (io_wqe_need_worker(wqe, IO_WQ_ACCT_BOUND))
790 fork_worker[IO_WQ_ACCT_BOUND] = true;
791 if (io_wqe_need_worker(wqe, IO_WQ_ACCT_UNBOUND))
792 fork_worker[IO_WQ_ACCT_UNBOUND] = true;
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200793 raw_spin_unlock_irq(&wqe->lock);
Jens Axboec5def4a2019-11-07 11:41:16 -0700794 if (fork_worker[IO_WQ_ACCT_BOUND])
795 create_io_worker(wq, wqe, IO_WQ_ACCT_BOUND);
796 if (fork_worker[IO_WQ_ACCT_UNBOUND])
797 create_io_worker(wq, wqe, IO_WQ_ACCT_UNBOUND);
Jens Axboe771b53d02019-10-22 10:25:58 -0600798 }
799 set_current_state(TASK_INTERRUPTIBLE);
800 schedule_timeout(HZ);
801 }
802
Jens Axboeaa96bf82020-04-03 11:26:26 -0600803 if (current->task_works)
804 task_work_run();
805
Hillf Dantonc4068bf2020-09-26 21:26:55 +0800806out:
807 if (refcount_dec_and_test(&wq->refs)) {
Jens Axboeb60fda62019-11-19 08:37:07 -0700808 complete(&wq->done);
Hillf Dantonc4068bf2020-09-26 21:26:55 +0800809 return 0;
810 }
811 /* if ERROR is set and we get here, we have workers to wake */
812 if (test_bit(IO_WQ_BIT_ERROR, &wq->state)) {
813 rcu_read_lock();
814 for_each_node(node)
815 io_wq_for_each_worker(wq->wqes[node], io_wq_worker_wake, NULL);
816 rcu_read_unlock();
817 }
Jens Axboeb60fda62019-11-19 08:37:07 -0700818 return 0;
Jens Axboe771b53d02019-10-22 10:25:58 -0600819}
820
Jens Axboec5def4a2019-11-07 11:41:16 -0700821static bool io_wq_can_queue(struct io_wqe *wqe, struct io_wqe_acct *acct,
822 struct io_wq_work *work)
823{
824 bool free_worker;
825
826 if (!(work->flags & IO_WQ_WORK_UNBOUND))
827 return true;
828 if (atomic_read(&acct->nr_running))
829 return true;
830
831 rcu_read_lock();
Jens Axboe021d1cd2019-11-14 08:00:41 -0700832 free_worker = !hlist_nulls_empty(&wqe->free_list);
Jens Axboec5def4a2019-11-07 11:41:16 -0700833 rcu_read_unlock();
834 if (free_worker)
835 return true;
836
837 if (atomic_read(&wqe->wq->user->processes) >= acct->max_workers &&
838 !(capable(CAP_SYS_RESOURCE) || capable(CAP_SYS_ADMIN)))
839 return false;
840
841 return true;
842}
843
Pavel Begunkove9fd9392020-03-04 16:14:12 +0300844static void io_run_cancel(struct io_wq_work *work, struct io_wqe *wqe)
Pavel Begunkovfc04c392020-03-01 19:18:19 +0300845{
Pavel Begunkove9fd9392020-03-04 16:14:12 +0300846 struct io_wq *wq = wqe->wq;
847
Pavel Begunkovfc04c392020-03-01 19:18:19 +0300848 do {
Pavel Begunkovfc04c392020-03-01 19:18:19 +0300849 work->flags |= IO_WQ_WORK_CANCEL;
Pavel Begunkov5280f7e2021-02-04 13:52:08 +0000850 wq->do_work(work);
851 work = wq->free_work(work);
Pavel Begunkovfc04c392020-03-01 19:18:19 +0300852 } while (work);
853}
854
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300855static void io_wqe_insert_work(struct io_wqe *wqe, struct io_wq_work *work)
856{
857 unsigned int hash;
858 struct io_wq_work *tail;
859
860 if (!io_wq_is_hashed(work)) {
861append:
862 wq_list_add_tail(&work->list, &wqe->work_list);
863 return;
864 }
865
866 hash = io_get_work_hash(work);
867 tail = wqe->hash_tail[hash];
868 wqe->hash_tail[hash] = work;
869 if (!tail)
870 goto append;
871
872 wq_list_add_after(&work->list, &tail->list, &wqe->work_list);
873}
874
Jens Axboe771b53d02019-10-22 10:25:58 -0600875static void io_wqe_enqueue(struct io_wqe *wqe, struct io_wq_work *work)
876{
Jens Axboec5def4a2019-11-07 11:41:16 -0700877 struct io_wqe_acct *acct = io_work_get_acct(wqe, work);
Jens Axboe895e2ca2019-12-17 08:46:33 -0700878 int work_flags;
Jens Axboe771b53d02019-10-22 10:25:58 -0600879 unsigned long flags;
880
Jens Axboec5def4a2019-11-07 11:41:16 -0700881 /*
882 * Do early check to see if we need a new unbound worker, and if we do,
883 * if we're allowed to do so. This isn't 100% accurate as there's a
884 * gap between this check and incrementing the value, but that's OK.
885 * It's close enough to not be an issue, fork() has the same delay.
886 */
887 if (unlikely(!io_wq_can_queue(wqe, acct, work))) {
Pavel Begunkove9fd9392020-03-04 16:14:12 +0300888 io_run_cancel(work, wqe);
Jens Axboec5def4a2019-11-07 11:41:16 -0700889 return;
890 }
891
Jens Axboe895e2ca2019-12-17 08:46:33 -0700892 work_flags = work->flags;
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200893 raw_spin_lock_irqsave(&wqe->lock, flags);
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300894 io_wqe_insert_work(wqe, work);
Jens Axboe771b53d02019-10-22 10:25:58 -0600895 wqe->flags &= ~IO_WQE_FLAG_STALLED;
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200896 raw_spin_unlock_irqrestore(&wqe->lock, flags);
Jens Axboe771b53d02019-10-22 10:25:58 -0600897
Jens Axboe895e2ca2019-12-17 08:46:33 -0700898 if ((work_flags & IO_WQ_WORK_CONCURRENT) ||
899 !atomic_read(&acct->nr_running))
Jens Axboec5def4a2019-11-07 11:41:16 -0700900 io_wqe_wake_worker(wqe, acct);
Jens Axboe771b53d02019-10-22 10:25:58 -0600901}
902
903void io_wq_enqueue(struct io_wq *wq, struct io_wq_work *work)
904{
905 struct io_wqe *wqe = wq->wqes[numa_node_id()];
906
907 io_wqe_enqueue(wqe, work);
908}
909
910/*
Pavel Begunkov8766dd52020-03-14 00:31:04 +0300911 * Work items that hash to the same value will not be done in parallel.
912 * Used to limit concurrent writes, generally hashed by inode.
Jens Axboe771b53d02019-10-22 10:25:58 -0600913 */
Pavel Begunkov8766dd52020-03-14 00:31:04 +0300914void io_wq_hash_work(struct io_wq_work *work, void *val)
Jens Axboe771b53d02019-10-22 10:25:58 -0600915{
Pavel Begunkov8766dd52020-03-14 00:31:04 +0300916 unsigned int bit;
Jens Axboe771b53d02019-10-22 10:25:58 -0600917
918 bit = hash_ptr(val, IO_WQ_HASH_ORDER);
919 work->flags |= (IO_WQ_WORK_HASHED | (bit << IO_WQ_HASH_SHIFT));
Jens Axboe771b53d02019-10-22 10:25:58 -0600920}
921
Jens Axboe62755e32019-10-28 21:49:21 -0600922struct io_cb_cancel_data {
Pavel Begunkov2293b412020-03-07 01:15:39 +0300923 work_cancel_fn *fn;
924 void *data;
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300925 int nr_running;
926 int nr_pending;
927 bool cancel_all;
Jens Axboe62755e32019-10-28 21:49:21 -0600928};
929
Pavel Begunkov2293b412020-03-07 01:15:39 +0300930static bool io_wq_worker_cancel(struct io_worker *worker, void *data)
Jens Axboe62755e32019-10-28 21:49:21 -0600931{
Pavel Begunkov2293b412020-03-07 01:15:39 +0300932 struct io_cb_cancel_data *match = data;
Jens Axboe6f726532019-11-05 13:51:51 -0700933 unsigned long flags;
Jens Axboe62755e32019-10-28 21:49:21 -0600934
935 /*
936 * Hold the lock to avoid ->cur_work going out of scope, caller
Jens Axboe36c2f922019-11-13 09:43:34 -0700937 * may dereference the passed in work.
Jens Axboe62755e32019-10-28 21:49:21 -0600938 */
Jens Axboe36c2f922019-11-13 09:43:34 -0700939 spin_lock_irqsave(&worker->lock, flags);
Jens Axboe62755e32019-10-28 21:49:21 -0600940 if (worker->cur_work &&
Pavel Begunkov2293b412020-03-07 01:15:39 +0300941 match->fn(worker->cur_work, match->data)) {
Jens Axboe771b53d02019-10-22 10:25:58 -0600942 send_sig(SIGINT, worker->task, 1);
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300943 match->nr_running++;
Jens Axboe771b53d02019-10-22 10:25:58 -0600944 }
Jens Axboe36c2f922019-11-13 09:43:34 -0700945 spin_unlock_irqrestore(&worker->lock, flags);
Jens Axboe771b53d02019-10-22 10:25:58 -0600946
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300947 return match->nr_running && !match->cancel_all;
Jens Axboe771b53d02019-10-22 10:25:58 -0600948}
949
Pavel Begunkov204361a2020-08-23 20:33:10 +0300950static inline void io_wqe_remove_pending(struct io_wqe *wqe,
951 struct io_wq_work *work,
952 struct io_wq_work_node *prev)
953{
954 unsigned int hash = io_get_work_hash(work);
955 struct io_wq_work *prev_work = NULL;
956
957 if (io_wq_is_hashed(work) && work == wqe->hash_tail[hash]) {
958 if (prev)
959 prev_work = container_of(prev, struct io_wq_work, list);
960 if (prev_work && io_get_work_hash(prev_work) == hash)
961 wqe->hash_tail[hash] = prev_work;
962 else
963 wqe->hash_tail[hash] = NULL;
964 }
965 wq_list_del(&wqe->work_list, &work->list, prev);
966}
967
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300968static void io_wqe_cancel_pending_work(struct io_wqe *wqe,
Pavel Begunkovf4c26652020-06-15 10:24:02 +0300969 struct io_cb_cancel_data *match)
Jens Axboe771b53d02019-10-22 10:25:58 -0600970{
Jens Axboe6206f0e2019-11-26 11:59:32 -0700971 struct io_wq_work_node *node, *prev;
Jens Axboe771b53d02019-10-22 10:25:58 -0600972 struct io_wq_work *work;
Jens Axboe6f726532019-11-05 13:51:51 -0700973 unsigned long flags;
Jens Axboe771b53d02019-10-22 10:25:58 -0600974
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300975retry:
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200976 raw_spin_lock_irqsave(&wqe->lock, flags);
Jens Axboe6206f0e2019-11-26 11:59:32 -0700977 wq_list_for_each(node, prev, &wqe->work_list) {
978 work = container_of(node, struct io_wq_work, list);
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300979 if (!match->fn(work, match->data))
980 continue;
Pavel Begunkov204361a2020-08-23 20:33:10 +0300981 io_wqe_remove_pending(wqe, work, prev);
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200982 raw_spin_unlock_irqrestore(&wqe->lock, flags);
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300983 io_run_cancel(work, wqe);
984 match->nr_pending++;
985 if (!match->cancel_all)
986 return;
987
988 /* not safe to continue after unlock */
989 goto retry;
Jens Axboe771b53d02019-10-22 10:25:58 -0600990 }
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200991 raw_spin_unlock_irqrestore(&wqe->lock, flags);
Pavel Begunkovf4c26652020-06-15 10:24:02 +0300992}
Jens Axboe771b53d02019-10-22 10:25:58 -0600993
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300994static void io_wqe_cancel_running_work(struct io_wqe *wqe,
Pavel Begunkovf4c26652020-06-15 10:24:02 +0300995 struct io_cb_cancel_data *match)
996{
Jens Axboe771b53d02019-10-22 10:25:58 -0600997 rcu_read_lock();
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300998 io_wq_for_each_worker(wqe, io_wq_worker_cancel, match);
Jens Axboe771b53d02019-10-22 10:25:58 -0600999 rcu_read_unlock();
Jens Axboe771b53d02019-10-22 10:25:58 -06001000}
1001
Pavel Begunkov2293b412020-03-07 01:15:39 +03001002enum io_wq_cancel io_wq_cancel_cb(struct io_wq *wq, work_cancel_fn *cancel,
Pavel Begunkov4f26bda2020-06-15 10:24:03 +03001003 void *data, bool cancel_all)
Pavel Begunkov2293b412020-03-07 01:15:39 +03001004{
1005 struct io_cb_cancel_data match = {
Pavel Begunkov4f26bda2020-06-15 10:24:03 +03001006 .fn = cancel,
1007 .data = data,
1008 .cancel_all = cancel_all,
Pavel Begunkov2293b412020-03-07 01:15:39 +03001009 };
Pavel Begunkov2293b412020-03-07 01:15:39 +03001010 int node;
1011
Pavel Begunkovf4c26652020-06-15 10:24:02 +03001012 /*
1013 * First check pending list, if we're lucky we can just remove it
1014 * from there. CANCEL_OK means that the work is returned as-new,
1015 * no completion will be posted for it.
1016 */
Pavel Begunkov2293b412020-03-07 01:15:39 +03001017 for_each_node(node) {
1018 struct io_wqe *wqe = wq->wqes[node];
1019
Pavel Begunkov4f26bda2020-06-15 10:24:03 +03001020 io_wqe_cancel_pending_work(wqe, &match);
1021 if (match.nr_pending && !match.cancel_all)
Pavel Begunkovf4c26652020-06-15 10:24:02 +03001022 return IO_WQ_CANCEL_OK;
Pavel Begunkov2293b412020-03-07 01:15:39 +03001023 }
1024
Pavel Begunkovf4c26652020-06-15 10:24:02 +03001025 /*
1026 * Now check if a free (going busy) or busy worker has the work
1027 * currently running. If we find it there, we'll return CANCEL_RUNNING
1028 * as an indication that we attempt to signal cancellation. The
1029 * completion will run normally in this case.
1030 */
1031 for_each_node(node) {
1032 struct io_wqe *wqe = wq->wqes[node];
1033
Pavel Begunkov4f26bda2020-06-15 10:24:03 +03001034 io_wqe_cancel_running_work(wqe, &match);
1035 if (match.nr_running && !match.cancel_all)
Pavel Begunkovf4c26652020-06-15 10:24:02 +03001036 return IO_WQ_CANCEL_RUNNING;
1037 }
1038
Pavel Begunkov4f26bda2020-06-15 10:24:03 +03001039 if (match.nr_running)
1040 return IO_WQ_CANCEL_RUNNING;
1041 if (match.nr_pending)
1042 return IO_WQ_CANCEL_OK;
Pavel Begunkovf4c26652020-06-15 10:24:02 +03001043 return IO_WQ_CANCEL_NOTFOUND;
Pavel Begunkov2293b412020-03-07 01:15:39 +03001044}
1045
Jens Axboe576a3472019-11-25 08:49:20 -07001046struct io_wq *io_wq_create(unsigned bounded, struct io_wq_data *data)
Jens Axboe771b53d02019-10-22 10:25:58 -06001047{
Jann Horn3fc50ab2019-11-26 19:10:20 +01001048 int ret = -ENOMEM, node;
Jens Axboe771b53d02019-10-22 10:25:58 -06001049 struct io_wq *wq;
1050
Pavel Begunkovf5fa38c2020-06-08 21:08:20 +03001051 if (WARN_ON_ONCE(!data->free_work || !data->do_work))
Pavel Begunkove9fd9392020-03-04 16:14:12 +03001052 return ERR_PTR(-EINVAL);
1053
Jann Hornad6e0052019-11-26 17:39:45 +01001054 wq = kzalloc(sizeof(*wq), GFP_KERNEL);
Jens Axboe771b53d02019-10-22 10:25:58 -06001055 if (!wq)
1056 return ERR_PTR(-ENOMEM);
1057
Jann Horn3fc50ab2019-11-26 19:10:20 +01001058 wq->wqes = kcalloc(nr_node_ids, sizeof(struct io_wqe *), GFP_KERNEL);
Jens Axboe43c01fb2020-10-22 09:02:50 -06001059 if (!wq->wqes)
1060 goto err_wq;
1061
1062 ret = cpuhp_state_add_instance_nocalls(io_wq_online, &wq->cpuhp_node);
1063 if (ret)
1064 goto err_wqes;
Jens Axboe771b53d02019-10-22 10:25:58 -06001065
Pavel Begunkove9fd9392020-03-04 16:14:12 +03001066 wq->free_work = data->free_work;
Pavel Begunkovf5fa38c2020-06-08 21:08:20 +03001067 wq->do_work = data->do_work;
Jens Axboe7d723062019-11-12 22:31:31 -07001068
Jens Axboec5def4a2019-11-07 11:41:16 -07001069 /* caller must already hold a reference to this */
Jens Axboe576a3472019-11-25 08:49:20 -07001070 wq->user = data->user;
Jens Axboec5def4a2019-11-07 11:41:16 -07001071
Jens Axboe43c01fb2020-10-22 09:02:50 -06001072 ret = -ENOMEM;
Jann Horn3fc50ab2019-11-26 19:10:20 +01001073 for_each_node(node) {
Jens Axboe771b53d02019-10-22 10:25:58 -06001074 struct io_wqe *wqe;
Jens Axboe75634392020-02-11 06:30:06 -07001075 int alloc_node = node;
Jens Axboe771b53d02019-10-22 10:25:58 -06001076
Jens Axboe75634392020-02-11 06:30:06 -07001077 if (!node_online(alloc_node))
1078 alloc_node = NUMA_NO_NODE;
1079 wqe = kzalloc_node(sizeof(struct io_wqe), GFP_KERNEL, alloc_node);
Jens Axboe771b53d02019-10-22 10:25:58 -06001080 if (!wqe)
Jann Horn3fc50ab2019-11-26 19:10:20 +01001081 goto err;
1082 wq->wqes[node] = wqe;
Jens Axboe75634392020-02-11 06:30:06 -07001083 wqe->node = alloc_node;
Jens Axboec5def4a2019-11-07 11:41:16 -07001084 wqe->acct[IO_WQ_ACCT_BOUND].max_workers = bounded;
1085 atomic_set(&wqe->acct[IO_WQ_ACCT_BOUND].nr_running, 0);
Jens Axboe576a3472019-11-25 08:49:20 -07001086 if (wq->user) {
Jens Axboec5def4a2019-11-07 11:41:16 -07001087 wqe->acct[IO_WQ_ACCT_UNBOUND].max_workers =
1088 task_rlimit(current, RLIMIT_NPROC);
1089 }
1090 atomic_set(&wqe->acct[IO_WQ_ACCT_UNBOUND].nr_running, 0);
Jens Axboe771b53d02019-10-22 10:25:58 -06001091 wqe->wq = wq;
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +02001092 raw_spin_lock_init(&wqe->lock);
Jens Axboe6206f0e2019-11-26 11:59:32 -07001093 INIT_WQ_LIST(&wqe->work_list);
Jens Axboe021d1cd2019-11-14 08:00:41 -07001094 INIT_HLIST_NULLS_HEAD(&wqe->free_list, 0);
Jens Axboee61df662019-11-13 13:54:49 -07001095 INIT_LIST_HEAD(&wqe->all_list);
Jens Axboe771b53d02019-10-22 10:25:58 -06001096 }
1097
1098 init_completion(&wq->done);
1099
Jens Axboe771b53d02019-10-22 10:25:58 -06001100 wq->manager = kthread_create(io_wq_manager, wq, "io_wq_manager");
1101 if (!IS_ERR(wq->manager)) {
1102 wake_up_process(wq->manager);
Jens Axboeb60fda62019-11-19 08:37:07 -07001103 wait_for_completion(&wq->done);
1104 if (test_bit(IO_WQ_BIT_ERROR, &wq->state)) {
1105 ret = -ENOMEM;
1106 goto err;
1107 }
Jens Axboe848f7e12020-01-23 15:33:32 -07001108 refcount_set(&wq->use_refs, 1);
Jens Axboeb60fda62019-11-19 08:37:07 -07001109 reinit_completion(&wq->done);
Jens Axboe771b53d02019-10-22 10:25:58 -06001110 return wq;
1111 }
1112
1113 ret = PTR_ERR(wq->manager);
Jens Axboe771b53d02019-10-22 10:25:58 -06001114 complete(&wq->done);
Jens Axboeb60fda62019-11-19 08:37:07 -07001115err:
Jens Axboe43c01fb2020-10-22 09:02:50 -06001116 cpuhp_state_remove_instance_nocalls(io_wq_online, &wq->cpuhp_node);
Jann Horn3fc50ab2019-11-26 19:10:20 +01001117 for_each_node(node)
1118 kfree(wq->wqes[node]);
Jens Axboe43c01fb2020-10-22 09:02:50 -06001119err_wqes:
Jens Axboeb60fda62019-11-19 08:37:07 -07001120 kfree(wq->wqes);
Jens Axboe43c01fb2020-10-22 09:02:50 -06001121err_wq:
Jens Axboeb60fda62019-11-19 08:37:07 -07001122 kfree(wq);
Jens Axboe771b53d02019-10-22 10:25:58 -06001123 return ERR_PTR(ret);
1124}
1125
Pavel Begunkoveba6f5a2020-01-28 03:15:47 +03001126bool io_wq_get(struct io_wq *wq, struct io_wq_data *data)
1127{
Pavel Begunkovf5fa38c2020-06-08 21:08:20 +03001128 if (data->free_work != wq->free_work || data->do_work != wq->do_work)
Pavel Begunkoveba6f5a2020-01-28 03:15:47 +03001129 return false;
1130
1131 return refcount_inc_not_zero(&wq->use_refs);
1132}
1133
Jens Axboe848f7e12020-01-23 15:33:32 -07001134static void __io_wq_destroy(struct io_wq *wq)
Jens Axboe771b53d02019-10-22 10:25:58 -06001135{
Jann Horn3fc50ab2019-11-26 19:10:20 +01001136 int node;
Jens Axboe771b53d02019-10-22 10:25:58 -06001137
Jens Axboe43c01fb2020-10-22 09:02:50 -06001138 cpuhp_state_remove_instance_nocalls(io_wq_online, &wq->cpuhp_node);
1139
Jens Axboeb60fda62019-11-19 08:37:07 -07001140 set_bit(IO_WQ_BIT_EXIT, &wq->state);
1141 if (wq->manager)
Jens Axboe771b53d02019-10-22 10:25:58 -06001142 kthread_stop(wq->manager);
Jens Axboe771b53d02019-10-22 10:25:58 -06001143
1144 rcu_read_lock();
Jann Horn3fc50ab2019-11-26 19:10:20 +01001145 for_each_node(node)
1146 io_wq_for_each_worker(wq->wqes[node], io_wq_worker_wake, NULL);
Jens Axboe771b53d02019-10-22 10:25:58 -06001147 rcu_read_unlock();
1148
1149 wait_for_completion(&wq->done);
1150
Jann Horn3fc50ab2019-11-26 19:10:20 +01001151 for_each_node(node)
1152 kfree(wq->wqes[node]);
Jens Axboe771b53d02019-10-22 10:25:58 -06001153 kfree(wq->wqes);
1154 kfree(wq);
1155}
Jens Axboe848f7e12020-01-23 15:33:32 -07001156
1157void io_wq_destroy(struct io_wq *wq)
1158{
1159 if (refcount_dec_and_test(&wq->use_refs))
1160 __io_wq_destroy(wq);
1161}
Jens Axboeaa96bf82020-04-03 11:26:26 -06001162
1163struct task_struct *io_wq_get_task(struct io_wq *wq)
1164{
1165 return wq->manager;
1166}
Jens Axboe43c01fb2020-10-22 09:02:50 -06001167
1168static bool io_wq_worker_affinity(struct io_worker *worker, void *data)
1169{
1170 struct task_struct *task = worker->task;
1171 struct rq_flags rf;
1172 struct rq *rq;
1173
1174 rq = task_rq_lock(task, &rf);
1175 do_set_cpus_allowed(task, cpumask_of_node(worker->wqe->node));
1176 task->flags |= PF_NO_SETAFFINITY;
1177 task_rq_unlock(rq, task, &rf);
1178 return false;
1179}
1180
1181static int io_wq_cpu_online(unsigned int cpu, struct hlist_node *node)
1182{
1183 struct io_wq *wq = hlist_entry_safe(node, struct io_wq, cpuhp_node);
1184 int i;
1185
1186 rcu_read_lock();
1187 for_each_node(i)
1188 io_wq_for_each_worker(wq->wqes[i], io_wq_worker_affinity, NULL);
1189 rcu_read_unlock();
1190 return 0;
1191}
1192
1193static __init int io_wq_init(void)
1194{
1195 int ret;
1196
1197 ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "io-wq/online",
1198 io_wq_cpu_online, NULL);
1199 if (ret < 0)
1200 return ret;
1201 io_wq_online = ret;
1202 return 0;
1203}
1204subsys_initcall(io_wq_init);