blob: 315b54eb05483f39935a4ec84e8da6179acac7e4 [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>
Jens Axboe771b53d02019-10-22 10:25:58 -060016#include <linux/rculist_nulls.h>
Jens Axboe43c01fb2020-10-22 09:02:50 -060017#include <linux/cpu.h>
Jens Axboe3bfe6102021-02-16 14:15:30 -070018#include <linux/tracehook.h>
Jens Axboe771b53d02019-10-22 10:25:58 -060019
Jens Axboe43c01fb2020-10-22 09:02:50 -060020#include "../kernel/sched/sched.h"
Jens Axboe771b53d02019-10-22 10:25:58 -060021#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 */
Jens Axboe145cc8c2020-09-26 12:37:46 -060029 IO_WORKER_F_FIXED = 8, /* static idle worker */
30 IO_WORKER_F_BOUND = 16, /* is doing bounded work */
Jens Axboe771b53d02019-10-22 10:25:58 -060031};
32
33enum {
34 IO_WQ_BIT_EXIT = 0, /* wq exiting */
Jens Axboe771b53d02019-10-22 10:25:58 -060035};
36
37enum {
38 IO_WQE_FLAG_STALLED = 1, /* stalled on hash */
39};
40
41/*
42 * One for each thread in a wqe pool
43 */
44struct io_worker {
45 refcount_t ref;
46 unsigned flags;
47 struct hlist_nulls_node nulls_node;
Jens Axboee61df662019-11-13 13:54:49 -070048 struct list_head all_list;
Jens Axboe771b53d02019-10-22 10:25:58 -060049 struct task_struct *task;
Jens Axboe771b53d02019-10-22 10:25:58 -060050 struct io_wqe *wqe;
Jens Axboe36c2f922019-11-13 09:43:34 -070051
Jens Axboe771b53d02019-10-22 10:25:58 -060052 struct io_wq_work *cur_work;
Jens Axboe36c2f922019-11-13 09:43:34 -070053 spinlock_t lock;
Jens Axboe771b53d02019-10-22 10:25:58 -060054
Jens Axboeeb2de942021-02-23 19:59:06 -070055 struct completion ref_done;
56
Jens Axboe771b53d02019-10-22 10:25:58 -060057 struct rcu_head rcu;
Jens Axboe771b53d02019-10-22 10:25:58 -060058};
59
Jens Axboe771b53d02019-10-22 10:25:58 -060060#if BITS_PER_LONG == 64
61#define IO_WQ_HASH_ORDER 6
62#else
63#define IO_WQ_HASH_ORDER 5
64#endif
65
Pavel Begunkov86f3cd12020-03-23 22:57:22 +030066#define IO_WQ_NR_HASH_BUCKETS (1u << IO_WQ_HASH_ORDER)
67
Jens Axboec5def4a2019-11-07 11:41:16 -070068struct io_wqe_acct {
69 unsigned nr_workers;
70 unsigned max_workers;
71 atomic_t nr_running;
72};
73
74enum {
75 IO_WQ_ACCT_BOUND,
76 IO_WQ_ACCT_UNBOUND,
77};
78
Jens Axboe771b53d02019-10-22 10:25:58 -060079/*
80 * Per-node worker thread pool
81 */
82struct io_wqe {
83 struct {
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +020084 raw_spinlock_t lock;
Jens Axboe6206f0e2019-11-26 11:59:32 -070085 struct io_wq_work_list work_list;
Jens Axboe771b53d02019-10-22 10:25:58 -060086 unsigned flags;
87 } ____cacheline_aligned_in_smp;
88
89 int node;
Jens Axboec5def4a2019-11-07 11:41:16 -070090 struct io_wqe_acct acct[2];
Jens Axboe771b53d02019-10-22 10:25:58 -060091
Jens Axboe021d1cd2019-11-14 08:00:41 -070092 struct hlist_nulls_head free_list;
Jens Axboee61df662019-11-13 13:54:49 -070093 struct list_head all_list;
Jens Axboe771b53d02019-10-22 10:25:58 -060094
Jens Axboee9418942021-02-19 12:33:30 -070095 struct wait_queue_entry wait;
96
Jens Axboe771b53d02019-10-22 10:25:58 -060097 struct io_wq *wq;
Pavel Begunkov86f3cd12020-03-23 22:57:22 +030098 struct io_wq_work *hash_tail[IO_WQ_NR_HASH_BUCKETS];
Jens Axboe771b53d02019-10-22 10:25:58 -060099};
100
101/*
102 * Per io_wq state
103 */
104struct io_wq {
105 struct io_wqe **wqes;
106 unsigned long state;
Jens Axboe771b53d02019-10-22 10:25:58 -0600107
Pavel Begunkove9fd9392020-03-04 16:14:12 +0300108 free_work_fn *free_work;
Pavel Begunkovf5fa38c2020-06-08 21:08:20 +0300109 io_wq_work_fn *do_work;
Jens Axboe7d723062019-11-12 22:31:31 -0700110
Jens Axboe771b53d02019-10-22 10:25:58 -0600111 struct task_struct *manager;
Jens Axboee9418942021-02-19 12:33:30 -0700112
113 struct io_wq_hash *hash;
114
Jens Axboe771b53d02019-10-22 10:25:58 -0600115 refcount_t refs;
Jens Axboed364d9e2021-02-26 09:56:32 -0700116 struct completion exited;
Jens Axboe848f7e12020-01-23 15:33:32 -0700117
Jens Axboefb3a1f62021-02-26 09:47:20 -0700118 atomic_t worker_refs;
119 struct completion worker_done;
120
Jens Axboe43c01fb2020-10-22 09:02:50 -0600121 struct hlist_node cpuhp_node;
Jens Axboe3bfe6102021-02-16 14:15:30 -0700122
123 pid_t task_pid;
Jens Axboe771b53d02019-10-22 10:25:58 -0600124};
125
Jens Axboe43c01fb2020-10-22 09:02:50 -0600126static enum cpuhp_state io_wq_online;
127
Jens Axboef0127252021-03-03 15:47:04 -0700128struct io_cb_cancel_data {
129 work_cancel_fn *fn;
130 void *data;
131 int nr_running;
132 int nr_pending;
133 bool cancel_all;
134};
135
136static void io_wqe_cancel_pending_work(struct io_wqe *wqe,
137 struct io_cb_cancel_data *match);
138
Jens Axboe771b53d02019-10-22 10:25:58 -0600139static bool io_worker_get(struct io_worker *worker)
140{
141 return refcount_inc_not_zero(&worker->ref);
142}
143
144static void io_worker_release(struct io_worker *worker)
145{
146 if (refcount_dec_and_test(&worker->ref))
Jens Axboeeb2de942021-02-23 19:59:06 -0700147 complete(&worker->ref_done);
Jens Axboe771b53d02019-10-22 10:25:58 -0600148}
149
Pavel Begunkov8418f222021-03-22 01:58:28 +0000150static inline struct io_wqe_acct *io_get_acct(struct io_wqe *wqe, bool bound)
151{
152 return &wqe->acct[bound ? IO_WQ_ACCT_BOUND : IO_WQ_ACCT_UNBOUND];
153}
154
Jens Axboec5def4a2019-11-07 11:41:16 -0700155static inline struct io_wqe_acct *io_work_get_acct(struct io_wqe *wqe,
156 struct io_wq_work *work)
157{
Pavel Begunkov8418f222021-03-22 01:58:28 +0000158 return io_get_acct(wqe, !(work->flags & IO_WQ_WORK_UNBOUND));
Jens Axboec5def4a2019-11-07 11:41:16 -0700159}
160
Jens Axboe958234d2021-02-17 09:00:57 -0700161static inline struct io_wqe_acct *io_wqe_get_acct(struct io_worker *worker)
Jens Axboec5def4a2019-11-07 11:41:16 -0700162{
Pavel Begunkov8418f222021-03-22 01:58:28 +0000163 return io_get_acct(worker->wqe, worker->flags & IO_WORKER_F_BOUND);
Jens Axboec5def4a2019-11-07 11:41:16 -0700164}
165
Jens Axboe771b53d02019-10-22 10:25:58 -0600166static void io_worker_exit(struct io_worker *worker)
167{
168 struct io_wqe *wqe = worker->wqe;
Jens Axboe958234d2021-02-17 09:00:57 -0700169 struct io_wqe_acct *acct = io_wqe_get_acct(worker);
Jens Axboebf1daa42021-02-16 18:00:55 -0700170 unsigned flags;
Jens Axboe771b53d02019-10-22 10:25:58 -0600171
Jens Axboeeb2de942021-02-23 19:59:06 -0700172 if (refcount_dec_and_test(&worker->ref))
173 complete(&worker->ref_done);
174 wait_for_completion(&worker->ref_done);
Jens Axboe771b53d02019-10-22 10:25:58 -0600175
176 preempt_disable();
177 current->flags &= ~PF_IO_WORKER;
Jens Axboebf1daa42021-02-16 18:00:55 -0700178 flags = worker->flags;
179 worker->flags = 0;
180 if (flags & IO_WORKER_F_RUNNING)
Jens Axboec5def4a2019-11-07 11:41:16 -0700181 atomic_dec(&acct->nr_running);
Jens Axboe771b53d02019-10-22 10:25:58 -0600182 worker->flags = 0;
183 preempt_enable();
184
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200185 raw_spin_lock_irq(&wqe->lock);
Jens Axboebf1daa42021-02-16 18:00:55 -0700186 if (flags & IO_WORKER_F_FREE)
187 hlist_nulls_del_rcu(&worker->nulls_node);
Jens Axboee61df662019-11-13 13:54:49 -0700188 list_del_rcu(&worker->all_list);
Jens Axboec5def4a2019-11-07 11:41:16 -0700189 acct->nr_workers--;
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200190 raw_spin_unlock_irq(&wqe->lock);
Jens Axboe771b53d02019-10-22 10:25:58 -0600191
YueHaibing364b05f2019-11-02 15:55:01 +0800192 kfree_rcu(worker, rcu);
Jens Axboefb3a1f62021-02-26 09:47:20 -0700193 if (atomic_dec_and_test(&wqe->wq->worker_refs))
194 complete(&wqe->wq->worker_done);
Jens Axboe46fe18b2021-03-04 12:39:36 -0700195 do_exit(0);
Jens Axboe771b53d02019-10-22 10:25:58 -0600196}
197
Jens Axboec5def4a2019-11-07 11:41:16 -0700198static inline bool io_wqe_run_queue(struct io_wqe *wqe)
199 __must_hold(wqe->lock)
200{
Jens Axboe6206f0e2019-11-26 11:59:32 -0700201 if (!wq_list_empty(&wqe->work_list) &&
202 !(wqe->flags & IO_WQE_FLAG_STALLED))
Jens Axboec5def4a2019-11-07 11:41:16 -0700203 return true;
204 return false;
205}
206
207/*
208 * Check head of free list for an available worker. If one isn't available,
209 * caller must wake up the wq manager to create one.
210 */
211static bool io_wqe_activate_free_worker(struct io_wqe *wqe)
212 __must_hold(RCU)
213{
214 struct hlist_nulls_node *n;
215 struct io_worker *worker;
216
Jens Axboe021d1cd2019-11-14 08:00:41 -0700217 n = rcu_dereference(hlist_nulls_first_rcu(&wqe->free_list));
Jens Axboec5def4a2019-11-07 11:41:16 -0700218 if (is_a_nulls(n))
219 return false;
220
221 worker = hlist_nulls_entry(n, struct io_worker, nulls_node);
222 if (io_worker_get(worker)) {
Jens Axboe506d95f2019-12-07 21:03:59 -0700223 wake_up_process(worker->task);
Jens Axboec5def4a2019-11-07 11:41:16 -0700224 io_worker_release(worker);
225 return true;
226 }
227
228 return false;
229}
230
231/*
232 * We need a worker. If we find a free one, we're good. If not, and we're
233 * below the max number of workers, wake up the manager to create one.
234 */
235static void io_wqe_wake_worker(struct io_wqe *wqe, struct io_wqe_acct *acct)
236{
237 bool ret;
238
239 /*
240 * Most likely an attempt to queue unbounded work on an io_wq that
241 * wasn't setup with any unbounded workers.
242 */
243 WARN_ON_ONCE(!acct->max_workers);
244
245 rcu_read_lock();
246 ret = io_wqe_activate_free_worker(wqe);
247 rcu_read_unlock();
248
249 if (!ret && acct->nr_workers < acct->max_workers)
250 wake_up_process(wqe->wq->manager);
251}
252
Jens Axboe958234d2021-02-17 09:00:57 -0700253static void io_wqe_inc_running(struct io_worker *worker)
Jens Axboec5def4a2019-11-07 11:41:16 -0700254{
Jens Axboe958234d2021-02-17 09:00:57 -0700255 struct io_wqe_acct *acct = io_wqe_get_acct(worker);
Jens Axboec5def4a2019-11-07 11:41:16 -0700256
257 atomic_inc(&acct->nr_running);
258}
259
Jens Axboe958234d2021-02-17 09:00:57 -0700260static void io_wqe_dec_running(struct io_worker *worker)
Jens Axboec5def4a2019-11-07 11:41:16 -0700261 __must_hold(wqe->lock)
262{
Jens Axboe958234d2021-02-17 09:00:57 -0700263 struct io_wqe_acct *acct = io_wqe_get_acct(worker);
264 struct io_wqe *wqe = worker->wqe;
Jens Axboec5def4a2019-11-07 11:41:16 -0700265
266 if (atomic_dec_and_test(&acct->nr_running) && io_wqe_run_queue(wqe))
267 io_wqe_wake_worker(wqe, acct);
268}
269
Jens Axboe771b53d02019-10-22 10:25:58 -0600270/*
271 * Worker will start processing some work. Move it to the busy list, if
272 * it's currently on the freelist
273 */
274static void __io_worker_busy(struct io_wqe *wqe, struct io_worker *worker,
275 struct io_wq_work *work)
276 __must_hold(wqe->lock)
277{
Jens Axboec5def4a2019-11-07 11:41:16 -0700278 bool worker_bound, work_bound;
279
Jens Axboe771b53d02019-10-22 10:25:58 -0600280 if (worker->flags & IO_WORKER_F_FREE) {
281 worker->flags &= ~IO_WORKER_F_FREE;
282 hlist_nulls_del_init_rcu(&worker->nulls_node);
Jens Axboe771b53d02019-10-22 10:25:58 -0600283 }
Jens Axboec5def4a2019-11-07 11:41:16 -0700284
285 /*
286 * If worker is moving from bound to unbound (or vice versa), then
287 * ensure we update the running accounting.
288 */
Dan Carpenterb2e9c7d62019-11-19 09:22:16 +0300289 worker_bound = (worker->flags & IO_WORKER_F_BOUND) != 0;
290 work_bound = (work->flags & IO_WQ_WORK_UNBOUND) == 0;
291 if (worker_bound != work_bound) {
Jens Axboe958234d2021-02-17 09:00:57 -0700292 io_wqe_dec_running(worker);
Jens Axboec5def4a2019-11-07 11:41:16 -0700293 if (work_bound) {
294 worker->flags |= IO_WORKER_F_BOUND;
295 wqe->acct[IO_WQ_ACCT_UNBOUND].nr_workers--;
296 wqe->acct[IO_WQ_ACCT_BOUND].nr_workers++;
Jens Axboec5def4a2019-11-07 11:41:16 -0700297 } else {
298 worker->flags &= ~IO_WORKER_F_BOUND;
299 wqe->acct[IO_WQ_ACCT_UNBOUND].nr_workers++;
300 wqe->acct[IO_WQ_ACCT_BOUND].nr_workers--;
Jens Axboec5def4a2019-11-07 11:41:16 -0700301 }
Jens Axboe958234d2021-02-17 09:00:57 -0700302 io_wqe_inc_running(worker);
Jens Axboec5def4a2019-11-07 11:41:16 -0700303 }
Jens Axboe771b53d02019-10-22 10:25:58 -0600304}
305
306/*
307 * No work, worker going to sleep. Move to freelist, and unuse mm if we
308 * have one attached. Dropping the mm may potentially sleep, so we drop
309 * the lock in that case and return success. Since the caller has to
310 * retry the loop in that case (we changed task state), we don't regrab
311 * the lock if we return success.
312 */
Jens Axboec6d77d92021-02-15 13:26:34 -0700313static void __io_worker_idle(struct io_wqe *wqe, struct io_worker *worker)
Jens Axboe771b53d02019-10-22 10:25:58 -0600314 __must_hold(wqe->lock)
315{
316 if (!(worker->flags & IO_WORKER_F_FREE)) {
317 worker->flags |= IO_WORKER_F_FREE;
Jens Axboe021d1cd2019-11-14 08:00:41 -0700318 hlist_nulls_add_head_rcu(&worker->nulls_node, &wqe->free_list);
Jens Axboe771b53d02019-10-22 10:25:58 -0600319 }
Jens Axboe771b53d02019-10-22 10:25:58 -0600320}
321
Pavel Begunkov60cf46a2020-03-14 00:31:05 +0300322static inline unsigned int io_get_work_hash(struct io_wq_work *work)
323{
324 return work->flags >> IO_WQ_HASH_SHIFT;
325}
326
Jens Axboee9418942021-02-19 12:33:30 -0700327static void io_wait_on_hash(struct io_wqe *wqe, unsigned int hash)
328{
329 struct io_wq *wq = wqe->wq;
330
331 spin_lock(&wq->hash->wait.lock);
332 if (list_empty(&wqe->wait.entry)) {
333 __add_wait_queue(&wq->hash->wait, &wqe->wait);
334 if (!test_bit(hash, &wq->hash->map)) {
335 __set_current_state(TASK_RUNNING);
336 list_del_init(&wqe->wait.entry);
337 }
338 }
339 spin_unlock(&wq->hash->wait.lock);
340}
341
Pavel Begunkov60cf46a2020-03-14 00:31:05 +0300342static struct io_wq_work *io_get_next_work(struct io_wqe *wqe)
Jens Axboe771b53d02019-10-22 10:25:58 -0600343 __must_hold(wqe->lock)
344{
Jens Axboe6206f0e2019-11-26 11:59:32 -0700345 struct io_wq_work_node *node, *prev;
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300346 struct io_wq_work *work, *tail;
Jens Axboee9418942021-02-19 12:33:30 -0700347 unsigned int stall_hash = -1U;
Jens Axboe771b53d02019-10-22 10:25:58 -0600348
Jens Axboe6206f0e2019-11-26 11:59:32 -0700349 wq_list_for_each(node, prev, &wqe->work_list) {
Jens Axboee9418942021-02-19 12:33:30 -0700350 unsigned int hash;
351
Jens Axboe6206f0e2019-11-26 11:59:32 -0700352 work = container_of(node, struct io_wq_work, list);
353
Jens Axboe771b53d02019-10-22 10:25:58 -0600354 /* not hashed, can run anytime */
Pavel Begunkov8766dd52020-03-14 00:31:04 +0300355 if (!io_wq_is_hashed(work)) {
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300356 wq_list_del(&wqe->work_list, node, prev);
Jens Axboe771b53d02019-10-22 10:25:58 -0600357 return work;
358 }
359
Pavel Begunkov60cf46a2020-03-14 00:31:05 +0300360 hash = io_get_work_hash(work);
Jens Axboee9418942021-02-19 12:33:30 -0700361 /* all items with this hash lie in [work, tail] */
362 tail = wqe->hash_tail[hash];
363
364 /* hashed, can run if not already running */
365 if (!test_and_set_bit(hash, &wqe->wq->hash->map)) {
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300366 wqe->hash_tail[hash] = NULL;
367 wq_list_cut(&wqe->work_list, &tail->list, prev);
Jens Axboe771b53d02019-10-22 10:25:58 -0600368 return work;
369 }
Jens Axboee9418942021-02-19 12:33:30 -0700370 if (stall_hash == -1U)
371 stall_hash = hash;
372 /* fast forward to a next hash, for-each will fix up @prev */
373 node = &tail->list;
374 }
375
376 if (stall_hash != -1U) {
377 raw_spin_unlock(&wqe->lock);
378 io_wait_on_hash(wqe, stall_hash);
379 raw_spin_lock(&wqe->lock);
Jens Axboe771b53d02019-10-22 10:25:58 -0600380 }
381
382 return NULL;
383}
384
Jens Axboe00ddff42021-03-21 07:06:56 -0600385static bool io_flush_signals(void)
Jens Axboecccf0ee2020-01-27 16:34:48 -0700386{
Jens Axboe0b8cfa92021-03-21 14:16:08 -0600387 if (unlikely(test_thread_flag(TIF_NOTIFY_SIGNAL))) {
Jens Axboe00ddff42021-03-21 07:06:56 -0600388 __set_current_state(TASK_RUNNING);
Jens Axboe0b8cfa92021-03-21 14:16:08 -0600389 tracehook_notify_signal();
Jens Axboe00ddff42021-03-21 07:06:56 -0600390 return true;
Jens Axboecccf0ee2020-01-27 16:34:48 -0700391 }
Jens Axboe00ddff42021-03-21 07:06:56 -0600392 return false;
Pavel Begunkovdc026a72020-03-04 16:14:09 +0300393}
394
395static void io_assign_current_work(struct io_worker *worker,
396 struct io_wq_work *work)
397{
Pavel Begunkovd78298e2020-03-14 00:31:03 +0300398 if (work) {
Jens Axboe3bfe6102021-02-16 14:15:30 -0700399 io_flush_signals();
Pavel Begunkovd78298e2020-03-14 00:31:03 +0300400 cond_resched();
401 }
Pavel Begunkovdc026a72020-03-04 16:14:09 +0300402
403 spin_lock_irq(&worker->lock);
404 worker->cur_work = work;
405 spin_unlock_irq(&worker->lock);
406}
407
Pavel Begunkov60cf46a2020-03-14 00:31:05 +0300408static void io_wqe_enqueue(struct io_wqe *wqe, struct io_wq_work *work);
409
Jens Axboe771b53d02019-10-22 10:25:58 -0600410static void io_worker_handle_work(struct io_worker *worker)
411 __releases(wqe->lock)
412{
Jens Axboe771b53d02019-10-22 10:25:58 -0600413 struct io_wqe *wqe = worker->wqe;
414 struct io_wq *wq = wqe->wq;
Pavel Begunkovc60eb042021-04-08 01:54:42 +0100415 bool do_kill = test_bit(IO_WQ_BIT_EXIT, &wq->state);
Jens Axboe771b53d02019-10-22 10:25:58 -0600416
417 do {
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300418 struct io_wq_work *work;
Pavel Begunkovf462fd32020-03-04 16:14:11 +0300419get_next:
Jens Axboe771b53d02019-10-22 10:25:58 -0600420 /*
Jens Axboe771b53d02019-10-22 10:25:58 -0600421 * If we got some work, mark us as busy. If we didn't, but
422 * the list isn't empty, it means we stalled on hashed work.
423 * Mark us stalled so we don't keep looking for work when we
424 * can't make progress, any work completion or insertion will
425 * clear the stalled flag.
426 */
Pavel Begunkov60cf46a2020-03-14 00:31:05 +0300427 work = io_get_next_work(wqe);
Jens Axboe771b53d02019-10-22 10:25:58 -0600428 if (work)
429 __io_worker_busy(wqe, worker, work);
Jens Axboe6206f0e2019-11-26 11:59:32 -0700430 else if (!wq_list_empty(&wqe->work_list))
Jens Axboe771b53d02019-10-22 10:25:58 -0600431 wqe->flags |= IO_WQE_FLAG_STALLED;
432
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200433 raw_spin_unlock_irq(&wqe->lock);
Jens Axboe771b53d02019-10-22 10:25:58 -0600434 if (!work)
435 break;
Pavel Begunkov58e393192020-03-04 16:14:10 +0300436 io_assign_current_work(worker, work);
Jens Axboee9418942021-02-19 12:33:30 -0700437 __set_current_state(TASK_RUNNING);
Jens Axboe36c2f922019-11-13 09:43:34 -0700438
Pavel Begunkovdc026a72020-03-04 16:14:09 +0300439 /* handle a whole dependent link */
440 do {
Pavel Begunkov5280f7e2021-02-04 13:52:08 +0000441 struct io_wq_work *next_hashed, *linked;
Pavel Begunkovb089ed392020-07-25 14:42:00 +0300442 unsigned int hash = io_get_work_hash(work);
Hillf Dantonfd1c4bc2019-12-24 09:14:29 -0700443
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300444 next_hashed = wq_next_work(work);
Pavel Begunkovc60eb042021-04-08 01:54:42 +0100445
446 if (unlikely(do_kill) && (work->flags & IO_WQ_WORK_UNBOUND))
447 work->flags |= IO_WQ_WORK_CANCEL;
Pavel Begunkov5280f7e2021-02-04 13:52:08 +0000448 wq->do_work(work);
449 io_assign_current_work(worker, NULL);
Jens Axboe36c2f922019-11-13 09:43:34 -0700450
Pavel Begunkov5280f7e2021-02-04 13:52:08 +0000451 linked = wq->free_work(work);
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300452 work = next_hashed;
453 if (!work && linked && !io_wq_is_hashed(linked)) {
454 work = linked;
455 linked = NULL;
456 }
457 io_assign_current_work(worker, work);
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300458 if (linked)
459 io_wqe_enqueue(wqe, linked);
460
461 if (hash != -1U && !next_hashed) {
Jens Axboee9418942021-02-19 12:33:30 -0700462 clear_bit(hash, &wq->hash->map);
463 if (wq_has_sleeper(&wq->hash->wait))
464 wake_up(&wq->hash->wait);
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200465 raw_spin_lock_irq(&wqe->lock);
Pavel Begunkovdc026a72020-03-04 16:14:09 +0300466 wqe->flags &= ~IO_WQE_FLAG_STALLED;
Pavel Begunkovf462fd32020-03-04 16:14:11 +0300467 /* skip unnecessary unlock-lock wqe->lock */
468 if (!work)
469 goto get_next;
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200470 raw_spin_unlock_irq(&wqe->lock);
Pavel Begunkovdc026a72020-03-04 16:14:09 +0300471 }
Pavel Begunkov58e393192020-03-04 16:14:10 +0300472 } while (work);
Jens Axboe36c2f922019-11-13 09:43:34 -0700473
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200474 raw_spin_lock_irq(&wqe->lock);
Jens Axboe771b53d02019-10-22 10:25:58 -0600475 } while (1);
476}
477
Jens Axboe771b53d02019-10-22 10:25:58 -0600478static int io_wqe_worker(void *data)
479{
480 struct io_worker *worker = data;
481 struct io_wqe *wqe = worker->wqe;
482 struct io_wq *wq = wqe->wq;
Jens Axboe46fe18b2021-03-04 12:39:36 -0700483 char buf[TASK_COMM_LEN];
Jens Axboe771b53d02019-10-22 10:25:58 -0600484
Jens Axboe46fe18b2021-03-04 12:39:36 -0700485 worker->flags |= (IO_WORKER_F_UP | IO_WORKER_F_RUNNING);
486 io_wqe_inc_running(worker);
487
Pavel Begunkov696ee882021-04-01 09:55:04 +0100488 snprintf(buf, sizeof(buf), "iou-wrk-%d", wq->task_pid);
Jens Axboe46fe18b2021-03-04 12:39:36 -0700489 set_task_comm(current, buf);
Jens Axboe771b53d02019-10-22 10:25:58 -0600490
491 while (!test_bit(IO_WQ_BIT_EXIT, &wq->state)) {
Jens Axboe16efa4f2021-03-12 20:26:13 -0700492 long ret;
493
Jens Axboe506d95f2019-12-07 21:03:59 -0700494 set_current_state(TASK_INTERRUPTIBLE);
Jens Axboee995d512019-12-07 21:06:46 -0700495loop:
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200496 raw_spin_lock_irq(&wqe->lock);
Jens Axboe771b53d02019-10-22 10:25:58 -0600497 if (io_wqe_run_queue(wqe)) {
Jens Axboe771b53d02019-10-22 10:25:58 -0600498 io_worker_handle_work(worker);
Jens Axboee995d512019-12-07 21:06:46 -0700499 goto loop;
Jens Axboe771b53d02019-10-22 10:25:58 -0600500 }
Jens Axboec6d77d92021-02-15 13:26:34 -0700501 __io_worker_idle(wqe, worker);
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200502 raw_spin_unlock_irq(&wqe->lock);
Jens Axboe00ddff42021-03-21 07:06:56 -0600503 if (io_flush_signals())
504 continue;
Jens Axboe16efa4f2021-03-12 20:26:13 -0700505 ret = schedule_timeout(WORKER_IDLE_TIMEOUT);
Jens Axboedbe1bdb2021-03-25 18:16:06 -0600506 if (signal_pending(current)) {
507 struct ksignal ksig;
508
509 if (!get_signal(&ksig))
510 continue;
Jens Axboe3bfe6102021-02-16 14:15:30 -0700511 break;
Jens Axboedbe1bdb2021-03-25 18:16:06 -0600512 }
513 if (ret)
514 continue;
Jens Axboe771b53d02019-10-22 10:25:58 -0600515 /* timed out, exit unless we're the fixed worker */
516 if (test_bit(IO_WQ_BIT_EXIT, &wq->state) ||
517 !(worker->flags & IO_WORKER_F_FIXED))
518 break;
519 }
520
Jens Axboe771b53d02019-10-22 10:25:58 -0600521 if (test_bit(IO_WQ_BIT_EXIT, &wq->state)) {
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200522 raw_spin_lock_irq(&wqe->lock);
Jens Axboe6206f0e2019-11-26 11:59:32 -0700523 if (!wq_list_empty(&wqe->work_list))
Jens Axboe771b53d02019-10-22 10:25:58 -0600524 io_worker_handle_work(worker);
525 else
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200526 raw_spin_unlock_irq(&wqe->lock);
Jens Axboe771b53d02019-10-22 10:25:58 -0600527 }
528
529 io_worker_exit(worker);
530 return 0;
531}
532
533/*
Jens Axboe771b53d02019-10-22 10:25:58 -0600534 * Called when a worker is scheduled in. Mark us as currently running.
535 */
536void io_wq_worker_running(struct task_struct *tsk)
537{
Jens Axboe3bfe6102021-02-16 14:15:30 -0700538 struct io_worker *worker = tsk->pf_io_worker;
Jens Axboe771b53d02019-10-22 10:25:58 -0600539
Jens Axboe3bfe6102021-02-16 14:15:30 -0700540 if (!worker)
541 return;
Jens Axboe771b53d02019-10-22 10:25:58 -0600542 if (!(worker->flags & IO_WORKER_F_UP))
543 return;
544 if (worker->flags & IO_WORKER_F_RUNNING)
545 return;
546 worker->flags |= IO_WORKER_F_RUNNING;
Jens Axboe958234d2021-02-17 09:00:57 -0700547 io_wqe_inc_running(worker);
Jens Axboe771b53d02019-10-22 10:25:58 -0600548}
549
550/*
551 * Called when worker is going to sleep. If there are no workers currently
552 * running and we have work pending, wake up a free one or have the manager
553 * set one up.
554 */
555void io_wq_worker_sleeping(struct task_struct *tsk)
556{
Jens Axboe3bfe6102021-02-16 14:15:30 -0700557 struct io_worker *worker = tsk->pf_io_worker;
Jens Axboe771b53d02019-10-22 10:25:58 -0600558
Jens Axboe3bfe6102021-02-16 14:15:30 -0700559 if (!worker)
560 return;
Jens Axboe771b53d02019-10-22 10:25:58 -0600561 if (!(worker->flags & IO_WORKER_F_UP))
562 return;
563 if (!(worker->flags & IO_WORKER_F_RUNNING))
564 return;
565
566 worker->flags &= ~IO_WORKER_F_RUNNING;
567
Jens Axboe3bfe6102021-02-16 14:15:30 -0700568 raw_spin_lock_irq(&worker->wqe->lock);
Jens Axboe958234d2021-02-17 09:00:57 -0700569 io_wqe_dec_running(worker);
Jens Axboe3bfe6102021-02-16 14:15:30 -0700570 raw_spin_unlock_irq(&worker->wqe->lock);
Jens Axboe771b53d02019-10-22 10:25:58 -0600571}
572
Jens Axboe3bfe6102021-02-16 14:15:30 -0700573static bool create_io_worker(struct io_wq *wq, struct io_wqe *wqe, int index)
574{
Jens Axboe46fe18b2021-03-04 12:39:36 -0700575 struct io_wqe_acct *acct = &wqe->acct[index];
Jens Axboe3bfe6102021-02-16 14:15:30 -0700576 struct io_worker *worker;
Jens Axboe46fe18b2021-03-04 12:39:36 -0700577 struct task_struct *tsk;
Jens Axboe3bfe6102021-02-16 14:15:30 -0700578
Jens Axboe8b3e78b2021-02-23 15:34:06 -0700579 __set_current_state(TASK_RUNNING);
580
Jens Axboe3bfe6102021-02-16 14:15:30 -0700581 worker = kzalloc_node(sizeof(*worker), GFP_KERNEL, wqe->node);
582 if (!worker)
583 return false;
584
585 refcount_set(&worker->ref, 1);
586 worker->nulls_node.pprev = NULL;
587 worker->wqe = wqe;
588 spin_lock_init(&worker->lock);
Jens Axboeeb2de942021-02-23 19:59:06 -0700589 init_completion(&worker->ref_done);
Jens Axboe3bfe6102021-02-16 14:15:30 -0700590
Jens Axboefb3a1f62021-02-26 09:47:20 -0700591 atomic_inc(&wq->worker_refs);
Jens Axboe8b3e78b2021-02-23 15:34:06 -0700592
Jens Axboe46fe18b2021-03-04 12:39:36 -0700593 tsk = create_io_thread(io_wqe_worker, worker, wqe->node);
594 if (IS_ERR(tsk)) {
Jens Axboefb3a1f62021-02-26 09:47:20 -0700595 if (atomic_dec_and_test(&wq->worker_refs))
596 complete(&wq->worker_done);
Jens Axboe3bfe6102021-02-16 14:15:30 -0700597 kfree(worker);
598 return false;
599 }
Jens Axboe46fe18b2021-03-04 12:39:36 -0700600
601 tsk->pf_io_worker = worker;
602 worker->task = tsk;
603 set_cpus_allowed_ptr(tsk, cpumask_of_node(wqe->node));
Jens Axboee22bc9b2021-03-09 19:49:02 -0700604 tsk->flags |= PF_NO_SETAFFINITY;
Jens Axboe46fe18b2021-03-04 12:39:36 -0700605
606 raw_spin_lock_irq(&wqe->lock);
607 hlist_nulls_add_head_rcu(&worker->nulls_node, &wqe->free_list);
608 list_add_tail_rcu(&worker->all_list, &wqe->all_list);
609 worker->flags |= IO_WORKER_F_FREE;
610 if (index == IO_WQ_ACCT_BOUND)
611 worker->flags |= IO_WORKER_F_BOUND;
612 if (!acct->nr_workers && (worker->flags & IO_WORKER_F_BOUND))
613 worker->flags |= IO_WORKER_F_FIXED;
614 acct->nr_workers++;
615 raw_spin_unlock_irq(&wqe->lock);
616 wake_up_new_task(tsk);
Jens Axboeb60fda62019-11-19 08:37:07 -0700617 return true;
Jens Axboe771b53d02019-10-22 10:25:58 -0600618}
619
Jens Axboec5def4a2019-11-07 11:41:16 -0700620static inline bool io_wqe_need_worker(struct io_wqe *wqe, int index)
Jens Axboe771b53d02019-10-22 10:25:58 -0600621 __must_hold(wqe->lock)
622{
Jens Axboec5def4a2019-11-07 11:41:16 -0700623 struct io_wqe_acct *acct = &wqe->acct[index];
Jens Axboe771b53d02019-10-22 10:25:58 -0600624
Jens Axboe613eeb62021-02-26 09:52:02 -0700625 if (acct->nr_workers && test_bit(IO_WQ_BIT_EXIT, &wqe->wq->state))
626 return false;
Jens Axboec5def4a2019-11-07 11:41:16 -0700627 /* if we have available workers or no work, no need */
Jens Axboe021d1cd2019-11-14 08:00:41 -0700628 if (!hlist_nulls_empty(&wqe->free_list) || !io_wqe_run_queue(wqe))
Jens Axboec5def4a2019-11-07 11:41:16 -0700629 return false;
630 return acct->nr_workers < acct->max_workers;
Jens Axboe771b53d02019-10-22 10:25:58 -0600631}
632
Hillf Dantonc4068bf2020-09-26 21:26:55 +0800633/*
634 * Iterate the passed in list and call the specific function for each
635 * worker that isn't exiting
636 */
637static bool io_wq_for_each_worker(struct io_wqe *wqe,
638 bool (*func)(struct io_worker *, void *),
639 void *data)
640{
641 struct io_worker *worker;
642 bool ret = false;
643
644 list_for_each_entry_rcu(worker, &wqe->all_list, all_list) {
645 if (io_worker_get(worker)) {
646 /* no task if node is/was offline */
647 if (worker->task)
648 ret = func(worker, data);
649 io_worker_release(worker);
650 if (ret)
651 break;
652 }
653 }
654
655 return ret;
656}
657
658static bool io_wq_worker_wake(struct io_worker *worker, void *data)
659{
Jens Axboe46fe18b2021-03-04 12:39:36 -0700660 set_notify_signal(worker->task);
Hillf Dantonc4068bf2020-09-26 21:26:55 +0800661 wake_up_process(worker->task);
662 return false;
663}
664
Jens Axboe8b3e78b2021-02-23 15:34:06 -0700665static void io_wq_check_workers(struct io_wq *wq)
666{
667 int node;
668
669 for_each_node(node) {
670 struct io_wqe *wqe = wq->wqes[node];
671 bool fork_worker[2] = { false, false };
672
673 if (!node_online(node))
674 continue;
675
676 raw_spin_lock_irq(&wqe->lock);
677 if (io_wqe_need_worker(wqe, IO_WQ_ACCT_BOUND))
678 fork_worker[IO_WQ_ACCT_BOUND] = true;
679 if (io_wqe_need_worker(wqe, IO_WQ_ACCT_UNBOUND))
680 fork_worker[IO_WQ_ACCT_UNBOUND] = true;
681 raw_spin_unlock_irq(&wqe->lock);
682 if (fork_worker[IO_WQ_ACCT_BOUND])
683 create_io_worker(wq, wqe, IO_WQ_ACCT_BOUND);
684 if (fork_worker[IO_WQ_ACCT_UNBOUND])
685 create_io_worker(wq, wqe, IO_WQ_ACCT_UNBOUND);
686 }
687}
688
Jens Axboef0127252021-03-03 15:47:04 -0700689static bool io_wq_work_match_all(struct io_wq_work *work, void *data)
690{
691 return true;
692}
693
694static void io_wq_cancel_pending(struct io_wq *wq)
695{
696 struct io_cb_cancel_data match = {
697 .fn = io_wq_work_match_all,
698 .cancel_all = true,
699 };
700 int node;
701
702 for_each_node(node)
703 io_wqe_cancel_pending_work(wq->wqes[node], &match);
704}
705
Jens Axboe771b53d02019-10-22 10:25:58 -0600706/*
707 * Manager thread. Tasked with creating new workers, if we need them.
708 */
709static int io_wq_manager(void *data)
710{
711 struct io_wq *wq = data;
Jens Axboe3bfe6102021-02-16 14:15:30 -0700712 char buf[TASK_COMM_LEN];
Jens Axboefb3a1f62021-02-26 09:47:20 -0700713 int node;
Jens Axboeb60fda62019-11-19 08:37:07 -0700714
Pavel Begunkov696ee882021-04-01 09:55:04 +0100715 snprintf(buf, sizeof(buf), "iou-mgr-%d", wq->task_pid);
Jens Axboe3bfe6102021-02-16 14:15:30 -0700716 set_task_comm(current, buf);
Jens Axboe771b53d02019-10-22 10:25:58 -0600717
Jens Axboe8b3e78b2021-02-23 15:34:06 -0700718 do {
Jens Axboe771b53d02019-10-22 10:25:58 -0600719 set_current_state(TASK_INTERRUPTIBLE);
Jens Axboe8b3e78b2021-02-23 15:34:06 -0700720 io_wq_check_workers(wq);
Jens Axboe771b53d02019-10-22 10:25:58 -0600721 schedule_timeout(HZ);
Jens Axboedbe1bdb2021-03-25 18:16:06 -0600722 if (signal_pending(current)) {
723 struct ksignal ksig;
724
725 if (!get_signal(&ksig))
726 continue;
Jens Axboe3bfe6102021-02-16 14:15:30 -0700727 set_bit(IO_WQ_BIT_EXIT, &wq->state);
Jens Axboedbe1bdb2021-03-25 18:16:06 -0600728 }
Jens Axboe8b3e78b2021-02-23 15:34:06 -0700729 } while (!test_bit(IO_WQ_BIT_EXIT, &wq->state));
730
731 io_wq_check_workers(wq);
Jens Axboefb3a1f62021-02-26 09:47:20 -0700732
733 rcu_read_lock();
734 for_each_node(node)
735 io_wq_for_each_worker(wq->wqes[node], io_wq_worker_wake, NULL);
736 rcu_read_unlock();
737
Jens Axboe886d0132021-03-05 12:59:30 -0700738 if (atomic_dec_and_test(&wq->worker_refs))
739 complete(&wq->worker_done);
740 wait_for_completion(&wq->worker_done);
Jens Axboef0127252021-03-03 15:47:04 -0700741
Jens Axboe09ca6c402021-03-05 08:14:08 -0700742 spin_lock_irq(&wq->hash->wait.lock);
743 for_each_node(node)
744 list_del_init(&wq->wqes[node]->wait.entry);
745 spin_unlock_irq(&wq->hash->wait.lock);
746
Jens Axboef0127252021-03-03 15:47:04 -0700747 io_wq_cancel_pending(wq);
Jens Axboed364d9e2021-02-26 09:56:32 -0700748 complete(&wq->exited);
Jens Axboe3bfe6102021-02-16 14:15:30 -0700749 do_exit(0);
Jens Axboe771b53d02019-10-22 10:25:58 -0600750}
751
Pavel Begunkove9fd9392020-03-04 16:14:12 +0300752static void io_run_cancel(struct io_wq_work *work, struct io_wqe *wqe)
Pavel Begunkovfc04c392020-03-01 19:18:19 +0300753{
Pavel Begunkove9fd9392020-03-04 16:14:12 +0300754 struct io_wq *wq = wqe->wq;
755
Pavel Begunkovfc04c392020-03-01 19:18:19 +0300756 do {
Pavel Begunkovfc04c392020-03-01 19:18:19 +0300757 work->flags |= IO_WQ_WORK_CANCEL;
Pavel Begunkov5280f7e2021-02-04 13:52:08 +0000758 wq->do_work(work);
759 work = wq->free_work(work);
Pavel Begunkovfc04c392020-03-01 19:18:19 +0300760 } while (work);
761}
762
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300763static void io_wqe_insert_work(struct io_wqe *wqe, struct io_wq_work *work)
764{
765 unsigned int hash;
766 struct io_wq_work *tail;
767
768 if (!io_wq_is_hashed(work)) {
769append:
770 wq_list_add_tail(&work->list, &wqe->work_list);
771 return;
772 }
773
774 hash = io_get_work_hash(work);
775 tail = wqe->hash_tail[hash];
776 wqe->hash_tail[hash] = work;
777 if (!tail)
778 goto append;
779
780 wq_list_add_after(&work->list, &tail->list, &wqe->work_list);
781}
782
Jens Axboe4fb6ac32021-02-25 10:17:09 -0700783static int io_wq_fork_manager(struct io_wq *wq)
784{
Jens Axboe46fe18b2021-03-04 12:39:36 -0700785 struct task_struct *tsk;
Jens Axboe4fb6ac32021-02-25 10:17:09 -0700786
787 if (wq->manager)
788 return 0;
789
Pavel Begunkov678eeba2021-03-06 11:02:18 +0000790 WARN_ON_ONCE(test_bit(IO_WQ_BIT_EXIT, &wq->state));
791
Jens Axboe886d0132021-03-05 12:59:30 -0700792 init_completion(&wq->worker_done);
793 atomic_set(&wq->worker_refs, 1);
Jens Axboe46fe18b2021-03-04 12:39:36 -0700794 tsk = create_io_thread(io_wq_manager, wq, NUMA_NO_NODE);
795 if (!IS_ERR(tsk)) {
796 wq->manager = get_task_struct(tsk);
797 wake_up_new_task(tsk);
Jens Axboe4fb6ac32021-02-25 10:17:09 -0700798 return 0;
799 }
800
Jens Axboe886d0132021-03-05 12:59:30 -0700801 if (atomic_dec_and_test(&wq->worker_refs))
802 complete(&wq->worker_done);
803
Jens Axboe46fe18b2021-03-04 12:39:36 -0700804 return PTR_ERR(tsk);
Jens Axboe4fb6ac32021-02-25 10:17:09 -0700805}
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 Axboe4fb6ac32021-02-25 10:17:09 -0700813 /* Can only happen if manager creation fails after exec */
Jens Axboef0127252021-03-03 15:47:04 -0700814 if (io_wq_fork_manager(wqe->wq) ||
815 test_bit(IO_WQ_BIT_EXIT, &wqe->wq->state)) {
yangerkun70e35122021-03-09 11:04:10 +0800816 io_run_cancel(work, wqe);
Jens Axboe4fb6ac32021-02-25 10:17:09 -0700817 return;
818 }
819
Jens Axboe895e2ca2019-12-17 08:46:33 -0700820 work_flags = work->flags;
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200821 raw_spin_lock_irqsave(&wqe->lock, flags);
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300822 io_wqe_insert_work(wqe, work);
Jens Axboe771b53d02019-10-22 10:25:58 -0600823 wqe->flags &= ~IO_WQE_FLAG_STALLED;
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200824 raw_spin_unlock_irqrestore(&wqe->lock, flags);
Jens Axboe771b53d02019-10-22 10:25:58 -0600825
Jens Axboe895e2ca2019-12-17 08:46:33 -0700826 if ((work_flags & IO_WQ_WORK_CONCURRENT) ||
827 !atomic_read(&acct->nr_running))
Jens Axboec5def4a2019-11-07 11:41:16 -0700828 io_wqe_wake_worker(wqe, acct);
Jens Axboe771b53d02019-10-22 10:25:58 -0600829}
830
831void io_wq_enqueue(struct io_wq *wq, struct io_wq_work *work)
832{
833 struct io_wqe *wqe = wq->wqes[numa_node_id()];
834
835 io_wqe_enqueue(wqe, work);
836}
837
838/*
Pavel Begunkov8766dd52020-03-14 00:31:04 +0300839 * Work items that hash to the same value will not be done in parallel.
840 * Used to limit concurrent writes, generally hashed by inode.
Jens Axboe771b53d02019-10-22 10:25:58 -0600841 */
Pavel Begunkov8766dd52020-03-14 00:31:04 +0300842void io_wq_hash_work(struct io_wq_work *work, void *val)
Jens Axboe771b53d02019-10-22 10:25:58 -0600843{
Pavel Begunkov8766dd52020-03-14 00:31:04 +0300844 unsigned int bit;
Jens Axboe771b53d02019-10-22 10:25:58 -0600845
846 bit = hash_ptr(val, IO_WQ_HASH_ORDER);
847 work->flags |= (IO_WQ_WORK_HASHED | (bit << IO_WQ_HASH_SHIFT));
Jens Axboe771b53d02019-10-22 10:25:58 -0600848}
849
Pavel Begunkov2293b412020-03-07 01:15:39 +0300850static bool io_wq_worker_cancel(struct io_worker *worker, void *data)
Jens Axboe62755e32019-10-28 21:49:21 -0600851{
Pavel Begunkov2293b412020-03-07 01:15:39 +0300852 struct io_cb_cancel_data *match = data;
Jens Axboe6f726532019-11-05 13:51:51 -0700853 unsigned long flags;
Jens Axboe62755e32019-10-28 21:49:21 -0600854
855 /*
856 * Hold the lock to avoid ->cur_work going out of scope, caller
Jens Axboe36c2f922019-11-13 09:43:34 -0700857 * may dereference the passed in work.
Jens Axboe62755e32019-10-28 21:49:21 -0600858 */
Jens Axboe36c2f922019-11-13 09:43:34 -0700859 spin_lock_irqsave(&worker->lock, flags);
Jens Axboe62755e32019-10-28 21:49:21 -0600860 if (worker->cur_work &&
Pavel Begunkov2293b412020-03-07 01:15:39 +0300861 match->fn(worker->cur_work, match->data)) {
Jens Axboe3bfe6102021-02-16 14:15:30 -0700862 set_notify_signal(worker->task);
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300863 match->nr_running++;
Jens Axboe771b53d02019-10-22 10:25:58 -0600864 }
Jens Axboe36c2f922019-11-13 09:43:34 -0700865 spin_unlock_irqrestore(&worker->lock, flags);
Jens Axboe771b53d02019-10-22 10:25:58 -0600866
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300867 return match->nr_running && !match->cancel_all;
Jens Axboe771b53d02019-10-22 10:25:58 -0600868}
869
Pavel Begunkov204361a2020-08-23 20:33:10 +0300870static inline void io_wqe_remove_pending(struct io_wqe *wqe,
871 struct io_wq_work *work,
872 struct io_wq_work_node *prev)
873{
874 unsigned int hash = io_get_work_hash(work);
875 struct io_wq_work *prev_work = NULL;
876
877 if (io_wq_is_hashed(work) && work == wqe->hash_tail[hash]) {
878 if (prev)
879 prev_work = container_of(prev, struct io_wq_work, list);
880 if (prev_work && io_get_work_hash(prev_work) == hash)
881 wqe->hash_tail[hash] = prev_work;
882 else
883 wqe->hash_tail[hash] = NULL;
884 }
885 wq_list_del(&wqe->work_list, &work->list, prev);
886}
887
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300888static void io_wqe_cancel_pending_work(struct io_wqe *wqe,
Pavel Begunkovf4c26652020-06-15 10:24:02 +0300889 struct io_cb_cancel_data *match)
Jens Axboe771b53d02019-10-22 10:25:58 -0600890{
Jens Axboe6206f0e2019-11-26 11:59:32 -0700891 struct io_wq_work_node *node, *prev;
Jens Axboe771b53d02019-10-22 10:25:58 -0600892 struct io_wq_work *work;
Jens Axboe6f726532019-11-05 13:51:51 -0700893 unsigned long flags;
Jens Axboe771b53d02019-10-22 10:25:58 -0600894
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300895retry:
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200896 raw_spin_lock_irqsave(&wqe->lock, flags);
Jens Axboe6206f0e2019-11-26 11:59:32 -0700897 wq_list_for_each(node, prev, &wqe->work_list) {
898 work = container_of(node, struct io_wq_work, list);
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300899 if (!match->fn(work, match->data))
900 continue;
Pavel Begunkov204361a2020-08-23 20:33:10 +0300901 io_wqe_remove_pending(wqe, work, prev);
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200902 raw_spin_unlock_irqrestore(&wqe->lock, flags);
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300903 io_run_cancel(work, wqe);
904 match->nr_pending++;
905 if (!match->cancel_all)
906 return;
907
908 /* not safe to continue after unlock */
909 goto retry;
Jens Axboe771b53d02019-10-22 10:25:58 -0600910 }
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200911 raw_spin_unlock_irqrestore(&wqe->lock, flags);
Pavel Begunkovf4c26652020-06-15 10:24:02 +0300912}
Jens Axboe771b53d02019-10-22 10:25:58 -0600913
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300914static void io_wqe_cancel_running_work(struct io_wqe *wqe,
Pavel Begunkovf4c26652020-06-15 10:24:02 +0300915 struct io_cb_cancel_data *match)
916{
Jens Axboe771b53d02019-10-22 10:25:58 -0600917 rcu_read_lock();
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300918 io_wq_for_each_worker(wqe, io_wq_worker_cancel, match);
Jens Axboe771b53d02019-10-22 10:25:58 -0600919 rcu_read_unlock();
Jens Axboe771b53d02019-10-22 10:25:58 -0600920}
921
Pavel Begunkov2293b412020-03-07 01:15:39 +0300922enum io_wq_cancel io_wq_cancel_cb(struct io_wq *wq, work_cancel_fn *cancel,
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300923 void *data, bool cancel_all)
Pavel Begunkov2293b412020-03-07 01:15:39 +0300924{
925 struct io_cb_cancel_data match = {
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300926 .fn = cancel,
927 .data = data,
928 .cancel_all = cancel_all,
Pavel Begunkov2293b412020-03-07 01:15:39 +0300929 };
Pavel Begunkov2293b412020-03-07 01:15:39 +0300930 int node;
931
Pavel Begunkovf4c26652020-06-15 10:24:02 +0300932 /*
933 * First check pending list, if we're lucky we can just remove it
934 * from there. CANCEL_OK means that the work is returned as-new,
935 * no completion will be posted for it.
936 */
Pavel Begunkov2293b412020-03-07 01:15:39 +0300937 for_each_node(node) {
938 struct io_wqe *wqe = wq->wqes[node];
939
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300940 io_wqe_cancel_pending_work(wqe, &match);
941 if (match.nr_pending && !match.cancel_all)
Pavel Begunkovf4c26652020-06-15 10:24:02 +0300942 return IO_WQ_CANCEL_OK;
Pavel Begunkov2293b412020-03-07 01:15:39 +0300943 }
944
Pavel Begunkovf4c26652020-06-15 10:24:02 +0300945 /*
946 * Now check if a free (going busy) or busy worker has the work
947 * currently running. If we find it there, we'll return CANCEL_RUNNING
948 * as an indication that we attempt to signal cancellation. The
949 * completion will run normally in this case.
950 */
951 for_each_node(node) {
952 struct io_wqe *wqe = wq->wqes[node];
953
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300954 io_wqe_cancel_running_work(wqe, &match);
955 if (match.nr_running && !match.cancel_all)
Pavel Begunkovf4c26652020-06-15 10:24:02 +0300956 return IO_WQ_CANCEL_RUNNING;
957 }
958
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300959 if (match.nr_running)
960 return IO_WQ_CANCEL_RUNNING;
961 if (match.nr_pending)
962 return IO_WQ_CANCEL_OK;
Pavel Begunkovf4c26652020-06-15 10:24:02 +0300963 return IO_WQ_CANCEL_NOTFOUND;
Pavel Begunkov2293b412020-03-07 01:15:39 +0300964}
965
Jens Axboee9418942021-02-19 12:33:30 -0700966static int io_wqe_hash_wake(struct wait_queue_entry *wait, unsigned mode,
967 int sync, void *key)
968{
969 struct io_wqe *wqe = container_of(wait, struct io_wqe, wait);
970 int ret;
971
972 list_del_init(&wait->entry);
973
974 rcu_read_lock();
975 ret = io_wqe_activate_free_worker(wqe);
976 rcu_read_unlock();
977
978 if (!ret)
979 wake_up_process(wqe->wq->manager);
980
981 return 1;
982}
983
Jens Axboe576a3472019-11-25 08:49:20 -0700984struct io_wq *io_wq_create(unsigned bounded, struct io_wq_data *data)
Jens Axboe771b53d02019-10-22 10:25:58 -0600985{
Jann Horn3fc50ab2019-11-26 19:10:20 +0100986 int ret = -ENOMEM, node;
Jens Axboe771b53d02019-10-22 10:25:58 -0600987 struct io_wq *wq;
988
Pavel Begunkovf5fa38c2020-06-08 21:08:20 +0300989 if (WARN_ON_ONCE(!data->free_work || !data->do_work))
Pavel Begunkove9fd9392020-03-04 16:14:12 +0300990 return ERR_PTR(-EINVAL);
991
Jann Hornad6e0052019-11-26 17:39:45 +0100992 wq = kzalloc(sizeof(*wq), GFP_KERNEL);
Jens Axboe771b53d02019-10-22 10:25:58 -0600993 if (!wq)
994 return ERR_PTR(-ENOMEM);
995
Jann Horn3fc50ab2019-11-26 19:10:20 +0100996 wq->wqes = kcalloc(nr_node_ids, sizeof(struct io_wqe *), GFP_KERNEL);
Jens Axboe43c01fb2020-10-22 09:02:50 -0600997 if (!wq->wqes)
998 goto err_wq;
999
1000 ret = cpuhp_state_add_instance_nocalls(io_wq_online, &wq->cpuhp_node);
1001 if (ret)
1002 goto err_wqes;
Jens Axboe771b53d02019-10-22 10:25:58 -06001003
Jens Axboee9418942021-02-19 12:33:30 -07001004 refcount_inc(&data->hash->refs);
1005 wq->hash = data->hash;
Pavel Begunkove9fd9392020-03-04 16:14:12 +03001006 wq->free_work = data->free_work;
Pavel Begunkovf5fa38c2020-06-08 21:08:20 +03001007 wq->do_work = data->do_work;
Jens Axboe7d723062019-11-12 22:31:31 -07001008
Jens Axboe43c01fb2020-10-22 09:02:50 -06001009 ret = -ENOMEM;
Jann Horn3fc50ab2019-11-26 19:10:20 +01001010 for_each_node(node) {
Jens Axboe771b53d02019-10-22 10:25:58 -06001011 struct io_wqe *wqe;
Jens Axboe75634392020-02-11 06:30:06 -07001012 int alloc_node = node;
Jens Axboe771b53d02019-10-22 10:25:58 -06001013
Jens Axboe75634392020-02-11 06:30:06 -07001014 if (!node_online(alloc_node))
1015 alloc_node = NUMA_NO_NODE;
1016 wqe = kzalloc_node(sizeof(struct io_wqe), GFP_KERNEL, alloc_node);
Jens Axboe771b53d02019-10-22 10:25:58 -06001017 if (!wqe)
Jann Horn3fc50ab2019-11-26 19:10:20 +01001018 goto err;
1019 wq->wqes[node] = wqe;
Jens Axboe75634392020-02-11 06:30:06 -07001020 wqe->node = alloc_node;
Jens Axboec5def4a2019-11-07 11:41:16 -07001021 wqe->acct[IO_WQ_ACCT_BOUND].max_workers = bounded;
1022 atomic_set(&wqe->acct[IO_WQ_ACCT_BOUND].nr_running, 0);
Jens Axboe728f13e2021-02-21 16:02:53 -07001023 wqe->acct[IO_WQ_ACCT_UNBOUND].max_workers =
Jens Axboec5def4a2019-11-07 11:41:16 -07001024 task_rlimit(current, RLIMIT_NPROC);
Jens Axboec5def4a2019-11-07 11:41:16 -07001025 atomic_set(&wqe->acct[IO_WQ_ACCT_UNBOUND].nr_running, 0);
Jens Axboee9418942021-02-19 12:33:30 -07001026 wqe->wait.func = io_wqe_hash_wake;
1027 INIT_LIST_HEAD(&wqe->wait.entry);
Jens Axboe771b53d02019-10-22 10:25:58 -06001028 wqe->wq = wq;
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +02001029 raw_spin_lock_init(&wqe->lock);
Jens Axboe6206f0e2019-11-26 11:59:32 -07001030 INIT_WQ_LIST(&wqe->work_list);
Jens Axboe021d1cd2019-11-14 08:00:41 -07001031 INIT_HLIST_NULLS_HEAD(&wqe->free_list, 0);
Jens Axboee61df662019-11-13 13:54:49 -07001032 INIT_LIST_HEAD(&wqe->all_list);
Jens Axboe771b53d02019-10-22 10:25:58 -06001033 }
1034
Jens Axboe3bfe6102021-02-16 14:15:30 -07001035 wq->task_pid = current->pid;
Jens Axboed364d9e2021-02-26 09:56:32 -07001036 init_completion(&wq->exited);
Jens Axboe3bfe6102021-02-16 14:15:30 -07001037 refcount_set(&wq->refs, 1);
Jens Axboe771b53d02019-10-22 10:25:58 -06001038
Jens Axboe4fb6ac32021-02-25 10:17:09 -07001039 ret = io_wq_fork_manager(wq);
1040 if (!ret)
Jens Axboe771b53d02019-10-22 10:25:58 -06001041 return wq;
Jens Axboeb60fda62019-11-19 08:37:07 -07001042err:
Jens Axboedc7bbc92021-03-01 09:09:56 -07001043 io_wq_put_hash(data->hash);
Jens Axboe43c01fb2020-10-22 09:02:50 -06001044 cpuhp_state_remove_instance_nocalls(io_wq_online, &wq->cpuhp_node);
Jann Horn3fc50ab2019-11-26 19:10:20 +01001045 for_each_node(node)
1046 kfree(wq->wqes[node]);
Jens Axboe43c01fb2020-10-22 09:02:50 -06001047err_wqes:
Jens Axboeb60fda62019-11-19 08:37:07 -07001048 kfree(wq->wqes);
Jens Axboe43c01fb2020-10-22 09:02:50 -06001049err_wq:
Jens Axboeb60fda62019-11-19 08:37:07 -07001050 kfree(wq);
Jens Axboe771b53d02019-10-22 10:25:58 -06001051 return ERR_PTR(ret);
1052}
1053
Jens Axboeafcc4012021-02-26 13:48:19 -07001054static void io_wq_destroy_manager(struct io_wq *wq)
1055{
1056 if (wq->manager) {
1057 wake_up_process(wq->manager);
1058 wait_for_completion(&wq->exited);
1059 put_task_struct(wq->manager);
1060 wq->manager = NULL;
1061 }
1062}
1063
Jens Axboe4fb6ac32021-02-25 10:17:09 -07001064static void io_wq_destroy(struct io_wq *wq)
Jens Axboe771b53d02019-10-22 10:25:58 -06001065{
Jann Horn3fc50ab2019-11-26 19:10:20 +01001066 int node;
Jens Axboe771b53d02019-10-22 10:25:58 -06001067
Jens Axboe43c01fb2020-10-22 09:02:50 -06001068 cpuhp_state_remove_instance_nocalls(io_wq_online, &wq->cpuhp_node);
1069
Jens Axboeb60fda62019-11-19 08:37:07 -07001070 set_bit(IO_WQ_BIT_EXIT, &wq->state);
Jens Axboeafcc4012021-02-26 13:48:19 -07001071 io_wq_destroy_manager(wq);
Jens Axboe771b53d02019-10-22 10:25:58 -06001072
Jens Axboee9418942021-02-19 12:33:30 -07001073 for_each_node(node) {
1074 struct io_wqe *wqe = wq->wqes[node];
Jens Axboef5d2d23b2021-03-25 10:16:12 -06001075 struct io_cb_cancel_data match = {
1076 .fn = io_wq_work_match_all,
1077 .cancel_all = true,
1078 };
1079 io_wqe_cancel_pending_work(wqe, &match);
Jens Axboee9418942021-02-19 12:33:30 -07001080 kfree(wqe);
1081 }
Jens Axboee9418942021-02-19 12:33:30 -07001082 io_wq_put_hash(wq->hash);
Jens Axboe771b53d02019-10-22 10:25:58 -06001083 kfree(wq->wqes);
1084 kfree(wq);
Jens Axboe4fb6ac32021-02-25 10:17:09 -07001085}
1086
1087void io_wq_put(struct io_wq *wq)
1088{
1089 if (refcount_dec_and_test(&wq->refs))
1090 io_wq_destroy(wq);
Jens Axboe771b53d02019-10-22 10:25:58 -06001091}
Jens Axboe848f7e12020-01-23 15:33:32 -07001092
Jens Axboeafcc4012021-02-26 13:48:19 -07001093void io_wq_put_and_exit(struct io_wq *wq)
1094{
1095 set_bit(IO_WQ_BIT_EXIT, &wq->state);
1096 io_wq_destroy_manager(wq);
1097 io_wq_put(wq);
1098}
1099
Jens Axboe43c01fb2020-10-22 09:02:50 -06001100static bool io_wq_worker_affinity(struct io_worker *worker, void *data)
1101{
1102 struct task_struct *task = worker->task;
1103 struct rq_flags rf;
1104 struct rq *rq;
1105
1106 rq = task_rq_lock(task, &rf);
1107 do_set_cpus_allowed(task, cpumask_of_node(worker->wqe->node));
1108 task->flags |= PF_NO_SETAFFINITY;
1109 task_rq_unlock(rq, task, &rf);
1110 return false;
1111}
1112
1113static int io_wq_cpu_online(unsigned int cpu, struct hlist_node *node)
1114{
1115 struct io_wq *wq = hlist_entry_safe(node, struct io_wq, cpuhp_node);
1116 int i;
1117
1118 rcu_read_lock();
1119 for_each_node(i)
1120 io_wq_for_each_worker(wq->wqes[i], io_wq_worker_affinity, NULL);
1121 rcu_read_unlock();
1122 return 0;
1123}
1124
1125static __init int io_wq_init(void)
1126{
1127 int ret;
1128
1129 ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "io-wq/online",
1130 io_wq_cpu_online, NULL);
1131 if (ret < 0)
1132 return ret;
1133 io_wq_online = ret;
1134 return 0;
1135}
1136subsys_initcall(io_wq_init);