blob: 5361a9b4b47b5f25904f0dbaa8112b79ec6611a8 [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
20#include "io-wq.h"
21
22#define WORKER_IDLE_TIMEOUT (5 * HZ)
23
24enum {
25 IO_WORKER_F_UP = 1, /* up and active */
26 IO_WORKER_F_RUNNING = 2, /* account as running */
27 IO_WORKER_F_FREE = 4, /* worker on free list */
Jens Axboe145cc8c2020-09-26 12:37:46 -060028 IO_WORKER_F_FIXED = 8, /* static idle worker */
29 IO_WORKER_F_BOUND = 16, /* is doing bounded work */
Jens Axboe771b53d02019-10-22 10:25:58 -060030};
31
32enum {
33 IO_WQ_BIT_EXIT = 0, /* wq exiting */
Jens Axboe771b53d02019-10-22 10:25:58 -060034};
35
36enum {
37 IO_WQE_FLAG_STALLED = 1, /* stalled on hash */
38};
39
40/*
41 * One for each thread in a wqe pool
42 */
43struct io_worker {
44 refcount_t ref;
45 unsigned flags;
46 struct hlist_nulls_node nulls_node;
Jens Axboee61df662019-11-13 13:54:49 -070047 struct list_head all_list;
Jens Axboe771b53d02019-10-22 10:25:58 -060048 struct task_struct *task;
Jens Axboe771b53d02019-10-22 10:25:58 -060049 struct io_wqe *wqe;
Jens Axboe36c2f922019-11-13 09:43:34 -070050
Jens Axboe771b53d02019-10-22 10:25:58 -060051 struct io_wq_work *cur_work;
Jens Axboe36c2f922019-11-13 09:43:34 -070052 spinlock_t lock;
Jens Axboe771b53d02019-10-22 10:25:58 -060053
Jens Axboeeb2de942021-02-23 19:59:06 -070054 struct completion ref_done;
55
Jens Axboe771b53d02019-10-22 10:25:58 -060056 struct rcu_head rcu;
Jens Axboe771b53d02019-10-22 10:25:58 -060057};
58
Jens Axboe771b53d02019-10-22 10:25:58 -060059#if BITS_PER_LONG == 64
60#define IO_WQ_HASH_ORDER 6
61#else
62#define IO_WQ_HASH_ORDER 5
63#endif
64
Pavel Begunkov86f3cd12020-03-23 22:57:22 +030065#define IO_WQ_NR_HASH_BUCKETS (1u << IO_WQ_HASH_ORDER)
66
Jens Axboec5def4a2019-11-07 11:41:16 -070067struct io_wqe_acct {
68 unsigned nr_workers;
69 unsigned max_workers;
Jens Axboe685fe7f2021-03-08 09:37:51 -070070 int index;
Jens Axboec5def4a2019-11-07 11:41:16 -070071 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 Axboee9418942021-02-19 12:33:30 -0700111 struct io_wq_hash *hash;
112
Jens Axboe771b53d02019-10-22 10:25:58 -0600113 refcount_t refs;
Jens Axboe848f7e12020-01-23 15:33:32 -0700114
Jens Axboefb3a1f62021-02-26 09:47:20 -0700115 atomic_t worker_refs;
116 struct completion worker_done;
117
Jens Axboe43c01fb2020-10-22 09:02:50 -0600118 struct hlist_node cpuhp_node;
Jens Axboe3bfe6102021-02-16 14:15:30 -0700119
Jens Axboe685fe7f2021-03-08 09:37:51 -0700120 struct task_struct *task;
Jens Axboe771b53d02019-10-22 10:25:58 -0600121};
122
Jens Axboe43c01fb2020-10-22 09:02:50 -0600123static enum cpuhp_state io_wq_online;
124
Jens Axboef0127252021-03-03 15:47:04 -0700125struct io_cb_cancel_data {
126 work_cancel_fn *fn;
127 void *data;
128 int nr_running;
129 int nr_pending;
130 bool cancel_all;
131};
132
Jens Axboe685fe7f2021-03-08 09:37:51 -0700133static void create_io_worker(struct io_wq *wq, struct io_wqe *wqe, int index);
Jens Axboef0127252021-03-03 15:47:04 -0700134
Jens Axboe771b53d02019-10-22 10:25:58 -0600135static bool io_worker_get(struct io_worker *worker)
136{
137 return refcount_inc_not_zero(&worker->ref);
138}
139
140static void io_worker_release(struct io_worker *worker)
141{
142 if (refcount_dec_and_test(&worker->ref))
Jens Axboeeb2de942021-02-23 19:59:06 -0700143 complete(&worker->ref_done);
Jens Axboe771b53d02019-10-22 10:25:58 -0600144}
145
Pavel Begunkov8418f222021-03-22 01:58:28 +0000146static inline struct io_wqe_acct *io_get_acct(struct io_wqe *wqe, bool bound)
147{
148 return &wqe->acct[bound ? IO_WQ_ACCT_BOUND : IO_WQ_ACCT_UNBOUND];
149}
150
Jens Axboec5def4a2019-11-07 11:41:16 -0700151static inline struct io_wqe_acct *io_work_get_acct(struct io_wqe *wqe,
152 struct io_wq_work *work)
153{
Pavel Begunkov8418f222021-03-22 01:58:28 +0000154 return io_get_acct(wqe, !(work->flags & IO_WQ_WORK_UNBOUND));
Jens Axboec5def4a2019-11-07 11:41:16 -0700155}
156
Jens Axboe958234d2021-02-17 09:00:57 -0700157static inline struct io_wqe_acct *io_wqe_get_acct(struct io_worker *worker)
Jens Axboec5def4a2019-11-07 11:41:16 -0700158{
Pavel Begunkov8418f222021-03-22 01:58:28 +0000159 return io_get_acct(worker->wqe, worker->flags & IO_WORKER_F_BOUND);
Jens Axboec5def4a2019-11-07 11:41:16 -0700160}
161
Jens Axboe685fe7f2021-03-08 09:37:51 -0700162static void io_worker_ref_put(struct io_wq *wq)
163{
164 if (atomic_dec_and_test(&wq->worker_refs))
165 complete(&wq->worker_done);
166}
167
Jens Axboe771b53d02019-10-22 10:25:58 -0600168static void io_worker_exit(struct io_worker *worker)
169{
170 struct io_wqe *wqe = worker->wqe;
Jens Axboe958234d2021-02-17 09:00:57 -0700171 struct io_wqe_acct *acct = io_wqe_get_acct(worker);
Jens Axboebf1daa42021-02-16 18:00:55 -0700172 unsigned flags;
Jens Axboe771b53d02019-10-22 10:25:58 -0600173
Jens Axboeeb2de942021-02-23 19:59:06 -0700174 if (refcount_dec_and_test(&worker->ref))
175 complete(&worker->ref_done);
176 wait_for_completion(&worker->ref_done);
Jens Axboe771b53d02019-10-22 10:25:58 -0600177
178 preempt_disable();
179 current->flags &= ~PF_IO_WORKER;
Jens Axboebf1daa42021-02-16 18:00:55 -0700180 flags = worker->flags;
181 worker->flags = 0;
182 if (flags & IO_WORKER_F_RUNNING)
Jens Axboec5def4a2019-11-07 11:41:16 -0700183 atomic_dec(&acct->nr_running);
Jens Axboe771b53d02019-10-22 10:25:58 -0600184 worker->flags = 0;
185 preempt_enable();
186
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200187 raw_spin_lock_irq(&wqe->lock);
Jens Axboebf1daa42021-02-16 18:00:55 -0700188 if (flags & IO_WORKER_F_FREE)
189 hlist_nulls_del_rcu(&worker->nulls_node);
Jens Axboee61df662019-11-13 13:54:49 -0700190 list_del_rcu(&worker->all_list);
Jens Axboec5def4a2019-11-07 11:41:16 -0700191 acct->nr_workers--;
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200192 raw_spin_unlock_irq(&wqe->lock);
Jens Axboe771b53d02019-10-22 10:25:58 -0600193
YueHaibing364b05f2019-11-02 15:55:01 +0800194 kfree_rcu(worker, rcu);
Jens Axboe685fe7f2021-03-08 09:37:51 -0700195 io_worker_ref_put(wqe->wq);
Jens Axboe46fe18b2021-03-04 12:39:36 -0700196 do_exit(0);
Jens Axboe771b53d02019-10-22 10:25:58 -0600197}
198
Jens Axboec5def4a2019-11-07 11:41:16 -0700199static inline bool io_wqe_run_queue(struct io_wqe *wqe)
200 __must_hold(wqe->lock)
201{
Jens Axboe6206f0e2019-11-26 11:59:32 -0700202 if (!wq_list_empty(&wqe->work_list) &&
203 !(wqe->flags & IO_WQE_FLAG_STALLED))
Jens Axboec5def4a2019-11-07 11:41:16 -0700204 return true;
205 return false;
206}
207
208/*
209 * Check head of free list for an available worker. If one isn't available,
Jens Axboe685fe7f2021-03-08 09:37:51 -0700210 * caller must create one.
Jens Axboec5def4a2019-11-07 11:41:16 -0700211 */
212static bool io_wqe_activate_free_worker(struct io_wqe *wqe)
213 __must_hold(RCU)
214{
215 struct hlist_nulls_node *n;
216 struct io_worker *worker;
217
Jens Axboe021d1cd2019-11-14 08:00:41 -0700218 n = rcu_dereference(hlist_nulls_first_rcu(&wqe->free_list));
Jens Axboec5def4a2019-11-07 11:41:16 -0700219 if (is_a_nulls(n))
220 return false;
221
222 worker = hlist_nulls_entry(n, struct io_worker, nulls_node);
223 if (io_worker_get(worker)) {
Jens Axboe506d95f2019-12-07 21:03:59 -0700224 wake_up_process(worker->task);
Jens Axboec5def4a2019-11-07 11:41:16 -0700225 io_worker_release(worker);
226 return true;
227 }
228
229 return false;
230}
231
232/*
233 * We need a worker. If we find a free one, we're good. If not, and we're
Jens Axboe685fe7f2021-03-08 09:37:51 -0700234 * below the max number of workers, create one.
Jens Axboec5def4a2019-11-07 11:41:16 -0700235 */
236static void io_wqe_wake_worker(struct io_wqe *wqe, struct io_wqe_acct *acct)
237{
238 bool ret;
239
240 /*
241 * Most likely an attempt to queue unbounded work on an io_wq that
242 * wasn't setup with any unbounded workers.
243 */
244 WARN_ON_ONCE(!acct->max_workers);
245
246 rcu_read_lock();
247 ret = io_wqe_activate_free_worker(wqe);
248 rcu_read_unlock();
249
Jens Axboe685fe7f2021-03-08 09:37:51 -0700250 if (!ret && acct->nr_workers < acct->max_workers) {
251 atomic_inc(&acct->nr_running);
252 atomic_inc(&wqe->wq->worker_refs);
253 create_io_worker(wqe->wq, wqe, acct->index);
254 }
Jens Axboec5def4a2019-11-07 11:41:16 -0700255}
256
Jens Axboe958234d2021-02-17 09:00:57 -0700257static void io_wqe_inc_running(struct io_worker *worker)
Jens Axboec5def4a2019-11-07 11:41:16 -0700258{
Jens Axboe958234d2021-02-17 09:00:57 -0700259 struct io_wqe_acct *acct = io_wqe_get_acct(worker);
Jens Axboec5def4a2019-11-07 11:41:16 -0700260
261 atomic_inc(&acct->nr_running);
262}
263
Jens Axboe685fe7f2021-03-08 09:37:51 -0700264struct create_worker_data {
265 struct callback_head work;
266 struct io_wqe *wqe;
267 int index;
268};
269
270static void create_worker_cb(struct callback_head *cb)
271{
272 struct create_worker_data *cwd;
273 struct io_wq *wq;
274
275 cwd = container_of(cb, struct create_worker_data, work);
276 wq = cwd->wqe->wq;
277 create_io_worker(wq, cwd->wqe, cwd->index);
278 kfree(cwd);
279}
280
281static void io_queue_worker_create(struct io_wqe *wqe, struct io_wqe_acct *acct)
282{
283 struct create_worker_data *cwd;
284 struct io_wq *wq = wqe->wq;
285
286 /* raced with exit, just ignore create call */
287 if (test_bit(IO_WQ_BIT_EXIT, &wq->state))
288 goto fail;
289
290 cwd = kmalloc(sizeof(*cwd), GFP_ATOMIC);
291 if (cwd) {
292 init_task_work(&cwd->work, create_worker_cb);
293 cwd->wqe = wqe;
294 cwd->index = acct->index;
295 if (!task_work_add(wq->task, &cwd->work, TWA_SIGNAL))
296 return;
297
298 kfree(cwd);
299 }
300fail:
301 atomic_dec(&acct->nr_running);
302 io_worker_ref_put(wq);
303}
304
Jens Axboe958234d2021-02-17 09:00:57 -0700305static void io_wqe_dec_running(struct io_worker *worker)
Jens Axboec5def4a2019-11-07 11:41:16 -0700306 __must_hold(wqe->lock)
307{
Jens Axboe958234d2021-02-17 09:00:57 -0700308 struct io_wqe_acct *acct = io_wqe_get_acct(worker);
309 struct io_wqe *wqe = worker->wqe;
Jens Axboec5def4a2019-11-07 11:41:16 -0700310
Jens Axboe685fe7f2021-03-08 09:37:51 -0700311 if (!(worker->flags & IO_WORKER_F_UP))
312 return;
313
314 if (atomic_dec_and_test(&acct->nr_running) && io_wqe_run_queue(wqe)) {
315 atomic_inc(&acct->nr_running);
316 atomic_inc(&wqe->wq->worker_refs);
317 io_queue_worker_create(wqe, acct);
318 }
Jens Axboec5def4a2019-11-07 11:41:16 -0700319}
320
Jens Axboe771b53d02019-10-22 10:25:58 -0600321/*
322 * Worker will start processing some work. Move it to the busy list, if
323 * it's currently on the freelist
324 */
325static void __io_worker_busy(struct io_wqe *wqe, struct io_worker *worker,
326 struct io_wq_work *work)
327 __must_hold(wqe->lock)
328{
Jens Axboec5def4a2019-11-07 11:41:16 -0700329 bool worker_bound, work_bound;
330
Hao Xu417b5052021-04-06 11:08:45 +0800331 BUILD_BUG_ON((IO_WQ_ACCT_UNBOUND ^ IO_WQ_ACCT_BOUND) != 1);
332
Jens Axboe771b53d02019-10-22 10:25:58 -0600333 if (worker->flags & IO_WORKER_F_FREE) {
334 worker->flags &= ~IO_WORKER_F_FREE;
335 hlist_nulls_del_init_rcu(&worker->nulls_node);
Jens Axboe771b53d02019-10-22 10:25:58 -0600336 }
Jens Axboec5def4a2019-11-07 11:41:16 -0700337
338 /*
339 * If worker is moving from bound to unbound (or vice versa), then
340 * ensure we update the running accounting.
341 */
Dan Carpenterb2e9c7d62019-11-19 09:22:16 +0300342 worker_bound = (worker->flags & IO_WORKER_F_BOUND) != 0;
343 work_bound = (work->flags & IO_WQ_WORK_UNBOUND) == 0;
344 if (worker_bound != work_bound) {
Hao Xu417b5052021-04-06 11:08:45 +0800345 int index = work_bound ? IO_WQ_ACCT_UNBOUND : IO_WQ_ACCT_BOUND;
Jens Axboe958234d2021-02-17 09:00:57 -0700346 io_wqe_dec_running(worker);
Hao Xu417b5052021-04-06 11:08:45 +0800347 worker->flags ^= IO_WORKER_F_BOUND;
348 wqe->acct[index].nr_workers--;
349 wqe->acct[index ^ 1].nr_workers++;
Jens Axboe958234d2021-02-17 09:00:57 -0700350 io_wqe_inc_running(worker);
Jens Axboec5def4a2019-11-07 11:41:16 -0700351 }
Jens Axboe771b53d02019-10-22 10:25:58 -0600352}
353
354/*
355 * No work, worker going to sleep. Move to freelist, and unuse mm if we
356 * have one attached. Dropping the mm may potentially sleep, so we drop
357 * the lock in that case and return success. Since the caller has to
358 * retry the loop in that case (we changed task state), we don't regrab
359 * the lock if we return success.
360 */
Jens Axboec6d77d92021-02-15 13:26:34 -0700361static void __io_worker_idle(struct io_wqe *wqe, struct io_worker *worker)
Jens Axboe771b53d02019-10-22 10:25:58 -0600362 __must_hold(wqe->lock)
363{
364 if (!(worker->flags & IO_WORKER_F_FREE)) {
365 worker->flags |= IO_WORKER_F_FREE;
Jens Axboe021d1cd2019-11-14 08:00:41 -0700366 hlist_nulls_add_head_rcu(&worker->nulls_node, &wqe->free_list);
Jens Axboe771b53d02019-10-22 10:25:58 -0600367 }
Jens Axboe771b53d02019-10-22 10:25:58 -0600368}
369
Pavel Begunkov60cf46a2020-03-14 00:31:05 +0300370static inline unsigned int io_get_work_hash(struct io_wq_work *work)
371{
372 return work->flags >> IO_WQ_HASH_SHIFT;
373}
374
Jens Axboee9418942021-02-19 12:33:30 -0700375static void io_wait_on_hash(struct io_wqe *wqe, unsigned int hash)
376{
377 struct io_wq *wq = wqe->wq;
378
379 spin_lock(&wq->hash->wait.lock);
380 if (list_empty(&wqe->wait.entry)) {
381 __add_wait_queue(&wq->hash->wait, &wqe->wait);
382 if (!test_bit(hash, &wq->hash->map)) {
383 __set_current_state(TASK_RUNNING);
384 list_del_init(&wqe->wait.entry);
385 }
386 }
387 spin_unlock(&wq->hash->wait.lock);
388}
389
Pavel Begunkov60cf46a2020-03-14 00:31:05 +0300390static struct io_wq_work *io_get_next_work(struct io_wqe *wqe)
Jens Axboe771b53d02019-10-22 10:25:58 -0600391 __must_hold(wqe->lock)
392{
Jens Axboe6206f0e2019-11-26 11:59:32 -0700393 struct io_wq_work_node *node, *prev;
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300394 struct io_wq_work *work, *tail;
Jens Axboee9418942021-02-19 12:33:30 -0700395 unsigned int stall_hash = -1U;
Jens Axboe771b53d02019-10-22 10:25:58 -0600396
Jens Axboe6206f0e2019-11-26 11:59:32 -0700397 wq_list_for_each(node, prev, &wqe->work_list) {
Jens Axboee9418942021-02-19 12:33:30 -0700398 unsigned int hash;
399
Jens Axboe6206f0e2019-11-26 11:59:32 -0700400 work = container_of(node, struct io_wq_work, list);
401
Jens Axboe771b53d02019-10-22 10:25:58 -0600402 /* not hashed, can run anytime */
Pavel Begunkov8766dd52020-03-14 00:31:04 +0300403 if (!io_wq_is_hashed(work)) {
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300404 wq_list_del(&wqe->work_list, node, prev);
Jens Axboe771b53d02019-10-22 10:25:58 -0600405 return work;
406 }
407
Pavel Begunkov60cf46a2020-03-14 00:31:05 +0300408 hash = io_get_work_hash(work);
Jens Axboee9418942021-02-19 12:33:30 -0700409 /* all items with this hash lie in [work, tail] */
410 tail = wqe->hash_tail[hash];
411
412 /* hashed, can run if not already running */
413 if (!test_and_set_bit(hash, &wqe->wq->hash->map)) {
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300414 wqe->hash_tail[hash] = NULL;
415 wq_list_cut(&wqe->work_list, &tail->list, prev);
Jens Axboe771b53d02019-10-22 10:25:58 -0600416 return work;
417 }
Jens Axboee9418942021-02-19 12:33:30 -0700418 if (stall_hash == -1U)
419 stall_hash = hash;
420 /* fast forward to a next hash, for-each will fix up @prev */
421 node = &tail->list;
422 }
423
424 if (stall_hash != -1U) {
425 raw_spin_unlock(&wqe->lock);
426 io_wait_on_hash(wqe, stall_hash);
427 raw_spin_lock(&wqe->lock);
Jens Axboe771b53d02019-10-22 10:25:58 -0600428 }
429
430 return NULL;
431}
432
Jens Axboe00ddff42021-03-21 07:06:56 -0600433static bool io_flush_signals(void)
Jens Axboecccf0ee2020-01-27 16:34:48 -0700434{
Jens Axboe0b8cfa92021-03-21 14:16:08 -0600435 if (unlikely(test_thread_flag(TIF_NOTIFY_SIGNAL))) {
Jens Axboe00ddff42021-03-21 07:06:56 -0600436 __set_current_state(TASK_RUNNING);
Jens Axboe0b8cfa92021-03-21 14:16:08 -0600437 tracehook_notify_signal();
Jens Axboe00ddff42021-03-21 07:06:56 -0600438 return true;
Jens Axboecccf0ee2020-01-27 16:34:48 -0700439 }
Jens Axboe00ddff42021-03-21 07:06:56 -0600440 return false;
Pavel Begunkovdc026a72020-03-04 16:14:09 +0300441}
442
443static void io_assign_current_work(struct io_worker *worker,
444 struct io_wq_work *work)
445{
Pavel Begunkovd78298e2020-03-14 00:31:03 +0300446 if (work) {
Jens Axboe3bfe6102021-02-16 14:15:30 -0700447 io_flush_signals();
Pavel Begunkovd78298e2020-03-14 00:31:03 +0300448 cond_resched();
449 }
Pavel Begunkovdc026a72020-03-04 16:14:09 +0300450
451 spin_lock_irq(&worker->lock);
452 worker->cur_work = work;
453 spin_unlock_irq(&worker->lock);
454}
455
Pavel Begunkov60cf46a2020-03-14 00:31:05 +0300456static void io_wqe_enqueue(struct io_wqe *wqe, struct io_wq_work *work);
457
Jens Axboe771b53d02019-10-22 10:25:58 -0600458static void io_worker_handle_work(struct io_worker *worker)
459 __releases(wqe->lock)
460{
Jens Axboe771b53d02019-10-22 10:25:58 -0600461 struct io_wqe *wqe = worker->wqe;
462 struct io_wq *wq = wqe->wq;
Pavel Begunkovc60eb042021-04-08 01:54:42 +0100463 bool do_kill = test_bit(IO_WQ_BIT_EXIT, &wq->state);
Jens Axboe771b53d02019-10-22 10:25:58 -0600464
465 do {
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300466 struct io_wq_work *work;
Pavel Begunkovf462fd32020-03-04 16:14:11 +0300467get_next:
Jens Axboe771b53d02019-10-22 10:25:58 -0600468 /*
Jens Axboe771b53d02019-10-22 10:25:58 -0600469 * If we got some work, mark us as busy. If we didn't, but
470 * the list isn't empty, it means we stalled on hashed work.
471 * Mark us stalled so we don't keep looking for work when we
472 * can't make progress, any work completion or insertion will
473 * clear the stalled flag.
474 */
Pavel Begunkov60cf46a2020-03-14 00:31:05 +0300475 work = io_get_next_work(wqe);
Jens Axboe771b53d02019-10-22 10:25:58 -0600476 if (work)
477 __io_worker_busy(wqe, worker, work);
Jens Axboe6206f0e2019-11-26 11:59:32 -0700478 else if (!wq_list_empty(&wqe->work_list))
Jens Axboe771b53d02019-10-22 10:25:58 -0600479 wqe->flags |= IO_WQE_FLAG_STALLED;
480
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200481 raw_spin_unlock_irq(&wqe->lock);
Jens Axboe771b53d02019-10-22 10:25:58 -0600482 if (!work)
483 break;
Pavel Begunkov58e39312020-03-04 16:14:10 +0300484 io_assign_current_work(worker, work);
Jens Axboee9418942021-02-19 12:33:30 -0700485 __set_current_state(TASK_RUNNING);
Jens Axboe36c2f922019-11-13 09:43:34 -0700486
Pavel Begunkovdc026a72020-03-04 16:14:09 +0300487 /* handle a whole dependent link */
488 do {
Pavel Begunkov5280f7e2021-02-04 13:52:08 +0000489 struct io_wq_work *next_hashed, *linked;
Pavel Begunkovb089ed392020-07-25 14:42:00 +0300490 unsigned int hash = io_get_work_hash(work);
Hillf Dantonfd1c4bc2019-12-24 09:14:29 -0700491
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300492 next_hashed = wq_next_work(work);
Pavel Begunkovc60eb042021-04-08 01:54:42 +0100493
494 if (unlikely(do_kill) && (work->flags & IO_WQ_WORK_UNBOUND))
495 work->flags |= IO_WQ_WORK_CANCEL;
Pavel Begunkov5280f7e2021-02-04 13:52:08 +0000496 wq->do_work(work);
497 io_assign_current_work(worker, NULL);
Jens Axboe36c2f922019-11-13 09:43:34 -0700498
Pavel Begunkov5280f7e2021-02-04 13:52:08 +0000499 linked = wq->free_work(work);
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300500 work = next_hashed;
501 if (!work && linked && !io_wq_is_hashed(linked)) {
502 work = linked;
503 linked = NULL;
504 }
505 io_assign_current_work(worker, work);
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300506 if (linked)
507 io_wqe_enqueue(wqe, linked);
508
509 if (hash != -1U && !next_hashed) {
Jens Axboee9418942021-02-19 12:33:30 -0700510 clear_bit(hash, &wq->hash->map);
511 if (wq_has_sleeper(&wq->hash->wait))
512 wake_up(&wq->hash->wait);
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200513 raw_spin_lock_irq(&wqe->lock);
Pavel Begunkovdc026a72020-03-04 16:14:09 +0300514 wqe->flags &= ~IO_WQE_FLAG_STALLED;
Pavel Begunkovf462fd32020-03-04 16:14:11 +0300515 /* skip unnecessary unlock-lock wqe->lock */
516 if (!work)
517 goto get_next;
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200518 raw_spin_unlock_irq(&wqe->lock);
Pavel Begunkovdc026a72020-03-04 16:14:09 +0300519 }
Pavel Begunkov58e39312020-03-04 16:14:10 +0300520 } while (work);
Jens Axboe36c2f922019-11-13 09:43:34 -0700521
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200522 raw_spin_lock_irq(&wqe->lock);
Jens Axboe771b53d02019-10-22 10:25:58 -0600523 } while (1);
524}
525
Jens Axboe771b53d02019-10-22 10:25:58 -0600526static int io_wqe_worker(void *data)
527{
528 struct io_worker *worker = data;
529 struct io_wqe *wqe = worker->wqe;
530 struct io_wq *wq = wqe->wq;
Jens Axboe46fe18b2021-03-04 12:39:36 -0700531 char buf[TASK_COMM_LEN];
Jens Axboe771b53d02019-10-22 10:25:58 -0600532
Jens Axboe46fe18b2021-03-04 12:39:36 -0700533 worker->flags |= (IO_WORKER_F_UP | IO_WORKER_F_RUNNING);
Jens Axboe46fe18b2021-03-04 12:39:36 -0700534
Jens Axboe685fe7f2021-03-08 09:37:51 -0700535 snprintf(buf, sizeof(buf), "iou-wrk-%d", wq->task->pid);
Jens Axboe46fe18b2021-03-04 12:39:36 -0700536 set_task_comm(current, buf);
Jens Axboe771b53d02019-10-22 10:25:58 -0600537
538 while (!test_bit(IO_WQ_BIT_EXIT, &wq->state)) {
Jens Axboe16efa4f2021-03-12 20:26:13 -0700539 long ret;
540
Jens Axboe506d95f2019-12-07 21:03:59 -0700541 set_current_state(TASK_INTERRUPTIBLE);
Jens Axboee995d512019-12-07 21:06:46 -0700542loop:
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200543 raw_spin_lock_irq(&wqe->lock);
Jens Axboe771b53d02019-10-22 10:25:58 -0600544 if (io_wqe_run_queue(wqe)) {
Jens Axboe771b53d02019-10-22 10:25:58 -0600545 io_worker_handle_work(worker);
Jens Axboee995d512019-12-07 21:06:46 -0700546 goto loop;
Jens Axboe771b53d02019-10-22 10:25:58 -0600547 }
Jens Axboec6d77d92021-02-15 13:26:34 -0700548 __io_worker_idle(wqe, worker);
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200549 raw_spin_unlock_irq(&wqe->lock);
Jens Axboe00ddff42021-03-21 07:06:56 -0600550 if (io_flush_signals())
551 continue;
Jens Axboe16efa4f2021-03-12 20:26:13 -0700552 ret = schedule_timeout(WORKER_IDLE_TIMEOUT);
Jens Axboedbe1bdb2021-03-25 18:16:06 -0600553 if (signal_pending(current)) {
554 struct ksignal ksig;
555
556 if (!get_signal(&ksig))
557 continue;
Jens Axboe3bfe6102021-02-16 14:15:30 -0700558 break;
Jens Axboedbe1bdb2021-03-25 18:16:06 -0600559 }
560 if (ret)
561 continue;
Jens Axboe771b53d02019-10-22 10:25:58 -0600562 /* timed out, exit unless we're the fixed worker */
563 if (test_bit(IO_WQ_BIT_EXIT, &wq->state) ||
564 !(worker->flags & IO_WORKER_F_FIXED))
565 break;
566 }
567
Jens Axboe771b53d02019-10-22 10:25:58 -0600568 if (test_bit(IO_WQ_BIT_EXIT, &wq->state)) {
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200569 raw_spin_lock_irq(&wqe->lock);
Jens Axboe6206f0e2019-11-26 11:59:32 -0700570 if (!wq_list_empty(&wqe->work_list))
Jens Axboe771b53d02019-10-22 10:25:58 -0600571 io_worker_handle_work(worker);
572 else
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200573 raw_spin_unlock_irq(&wqe->lock);
Jens Axboe771b53d02019-10-22 10:25:58 -0600574 }
575
576 io_worker_exit(worker);
577 return 0;
578}
579
580/*
Jens Axboe771b53d02019-10-22 10:25:58 -0600581 * Called when a worker is scheduled in. Mark us as currently running.
582 */
583void io_wq_worker_running(struct task_struct *tsk)
584{
Jens Axboe3bfe6102021-02-16 14:15:30 -0700585 struct io_worker *worker = tsk->pf_io_worker;
Jens Axboe771b53d02019-10-22 10:25:58 -0600586
Jens Axboe3bfe6102021-02-16 14:15:30 -0700587 if (!worker)
588 return;
Jens Axboe771b53d02019-10-22 10:25:58 -0600589 if (!(worker->flags & IO_WORKER_F_UP))
590 return;
591 if (worker->flags & IO_WORKER_F_RUNNING)
592 return;
593 worker->flags |= IO_WORKER_F_RUNNING;
Jens Axboe958234d2021-02-17 09:00:57 -0700594 io_wqe_inc_running(worker);
Jens Axboe771b53d02019-10-22 10:25:58 -0600595}
596
597/*
598 * Called when worker is going to sleep. If there are no workers currently
Jens Axboe685fe7f2021-03-08 09:37:51 -0700599 * running and we have work pending, wake up a free one or create a new one.
Jens Axboe771b53d02019-10-22 10:25:58 -0600600 */
601void io_wq_worker_sleeping(struct task_struct *tsk)
602{
Jens Axboe3bfe6102021-02-16 14:15:30 -0700603 struct io_worker *worker = tsk->pf_io_worker;
Jens Axboe771b53d02019-10-22 10:25:58 -0600604
Jens Axboe3bfe6102021-02-16 14:15:30 -0700605 if (!worker)
606 return;
Jens Axboe771b53d02019-10-22 10:25:58 -0600607 if (!(worker->flags & IO_WORKER_F_UP))
608 return;
609 if (!(worker->flags & IO_WORKER_F_RUNNING))
610 return;
611
612 worker->flags &= ~IO_WORKER_F_RUNNING;
613
Jens Axboe3bfe6102021-02-16 14:15:30 -0700614 raw_spin_lock_irq(&worker->wqe->lock);
Jens Axboe958234d2021-02-17 09:00:57 -0700615 io_wqe_dec_running(worker);
Jens Axboe3bfe6102021-02-16 14:15:30 -0700616 raw_spin_unlock_irq(&worker->wqe->lock);
Jens Axboe771b53d02019-10-22 10:25:58 -0600617}
618
Jens Axboe685fe7f2021-03-08 09:37:51 -0700619static void create_io_worker(struct io_wq *wq, struct io_wqe *wqe, int index)
Jens Axboe3bfe6102021-02-16 14:15:30 -0700620{
Jens Axboe46fe18b2021-03-04 12:39:36 -0700621 struct io_wqe_acct *acct = &wqe->acct[index];
Jens Axboe3bfe6102021-02-16 14:15:30 -0700622 struct io_worker *worker;
Jens Axboe46fe18b2021-03-04 12:39:36 -0700623 struct task_struct *tsk;
Jens Axboe3bfe6102021-02-16 14:15:30 -0700624
Jens Axboe8b3e78b2021-02-23 15:34:06 -0700625 __set_current_state(TASK_RUNNING);
626
Jens Axboe3bfe6102021-02-16 14:15:30 -0700627 worker = kzalloc_node(sizeof(*worker), GFP_KERNEL, wqe->node);
628 if (!worker)
Jens Axboe685fe7f2021-03-08 09:37:51 -0700629 goto fail;
Jens Axboe3bfe6102021-02-16 14:15:30 -0700630
631 refcount_set(&worker->ref, 1);
632 worker->nulls_node.pprev = NULL;
633 worker->wqe = wqe;
634 spin_lock_init(&worker->lock);
Jens Axboeeb2de942021-02-23 19:59:06 -0700635 init_completion(&worker->ref_done);
Jens Axboe3bfe6102021-02-16 14:15:30 -0700636
Jens Axboe46fe18b2021-03-04 12:39:36 -0700637 tsk = create_io_thread(io_wqe_worker, worker, wqe->node);
638 if (IS_ERR(tsk)) {
Jens Axboe3bfe6102021-02-16 14:15:30 -0700639 kfree(worker);
Jens Axboe685fe7f2021-03-08 09:37:51 -0700640fail:
641 atomic_dec(&acct->nr_running);
642 io_worker_ref_put(wq);
643 return;
Jens Axboe3bfe6102021-02-16 14:15:30 -0700644 }
Jens Axboe46fe18b2021-03-04 12:39:36 -0700645
646 tsk->pf_io_worker = worker;
647 worker->task = tsk;
648 set_cpus_allowed_ptr(tsk, cpumask_of_node(wqe->node));
Jens Axboee22bc9b2021-03-09 19:49:02 -0700649 tsk->flags |= PF_NO_SETAFFINITY;
Jens Axboe46fe18b2021-03-04 12:39:36 -0700650
651 raw_spin_lock_irq(&wqe->lock);
652 hlist_nulls_add_head_rcu(&worker->nulls_node, &wqe->free_list);
653 list_add_tail_rcu(&worker->all_list, &wqe->all_list);
654 worker->flags |= IO_WORKER_F_FREE;
655 if (index == IO_WQ_ACCT_BOUND)
656 worker->flags |= IO_WORKER_F_BOUND;
657 if (!acct->nr_workers && (worker->flags & IO_WORKER_F_BOUND))
658 worker->flags |= IO_WORKER_F_FIXED;
659 acct->nr_workers++;
660 raw_spin_unlock_irq(&wqe->lock);
661 wake_up_new_task(tsk);
Jens Axboe771b53d02019-10-22 10:25:58 -0600662}
663
Hillf Dantonc4068bf2020-09-26 21:26:55 +0800664/*
665 * Iterate the passed in list and call the specific function for each
666 * worker that isn't exiting
667 */
668static bool io_wq_for_each_worker(struct io_wqe *wqe,
669 bool (*func)(struct io_worker *, void *),
670 void *data)
671{
672 struct io_worker *worker;
673 bool ret = false;
674
675 list_for_each_entry_rcu(worker, &wqe->all_list, all_list) {
676 if (io_worker_get(worker)) {
677 /* no task if node is/was offline */
678 if (worker->task)
679 ret = func(worker, data);
680 io_worker_release(worker);
681 if (ret)
682 break;
683 }
684 }
685
686 return ret;
687}
688
689static bool io_wq_worker_wake(struct io_worker *worker, void *data)
690{
Jens Axboe46fe18b2021-03-04 12:39:36 -0700691 set_notify_signal(worker->task);
Hillf Dantonc4068bf2020-09-26 21:26:55 +0800692 wake_up_process(worker->task);
693 return false;
694}
695
Jens Axboef0127252021-03-03 15:47:04 -0700696static bool io_wq_work_match_all(struct io_wq_work *work, void *data)
697{
698 return true;
699}
700
Pavel Begunkove9fd9392020-03-04 16:14:12 +0300701static void io_run_cancel(struct io_wq_work *work, struct io_wqe *wqe)
Pavel Begunkovfc04c392020-03-01 19:18:19 +0300702{
Pavel Begunkove9fd9392020-03-04 16:14:12 +0300703 struct io_wq *wq = wqe->wq;
704
Pavel Begunkovfc04c392020-03-01 19:18:19 +0300705 do {
Pavel Begunkovfc04c392020-03-01 19:18:19 +0300706 work->flags |= IO_WQ_WORK_CANCEL;
Pavel Begunkov5280f7e2021-02-04 13:52:08 +0000707 wq->do_work(work);
708 work = wq->free_work(work);
Pavel Begunkovfc04c392020-03-01 19:18:19 +0300709 } while (work);
710}
711
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300712static void io_wqe_insert_work(struct io_wqe *wqe, struct io_wq_work *work)
713{
714 unsigned int hash;
715 struct io_wq_work *tail;
716
717 if (!io_wq_is_hashed(work)) {
718append:
719 wq_list_add_tail(&work->list, &wqe->work_list);
720 return;
721 }
722
723 hash = io_get_work_hash(work);
724 tail = wqe->hash_tail[hash];
725 wqe->hash_tail[hash] = work;
726 if (!tail)
727 goto append;
728
729 wq_list_add_after(&work->list, &tail->list, &wqe->work_list);
730}
731
Jens Axboe771b53d02019-10-22 10:25:58 -0600732static void io_wqe_enqueue(struct io_wqe *wqe, struct io_wq_work *work)
733{
Jens Axboec5def4a2019-11-07 11:41:16 -0700734 struct io_wqe_acct *acct = io_work_get_acct(wqe, work);
Jens Axboe895e2ca2019-12-17 08:46:33 -0700735 int work_flags;
Jens Axboe771b53d02019-10-22 10:25:58 -0600736 unsigned long flags;
737
Jens Axboe685fe7f2021-03-08 09:37:51 -0700738 if (test_bit(IO_WQ_BIT_EXIT, &wqe->wq->state)) {
yangerkun70e35122021-03-09 11:04:10 +0800739 io_run_cancel(work, wqe);
Jens Axboe4fb6ac32021-02-25 10:17:09 -0700740 return;
741 }
742
Jens Axboe895e2ca2019-12-17 08:46:33 -0700743 work_flags = work->flags;
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200744 raw_spin_lock_irqsave(&wqe->lock, flags);
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300745 io_wqe_insert_work(wqe, work);
Jens Axboe771b53d02019-10-22 10:25:58 -0600746 wqe->flags &= ~IO_WQE_FLAG_STALLED;
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200747 raw_spin_unlock_irqrestore(&wqe->lock, flags);
Jens Axboe771b53d02019-10-22 10:25:58 -0600748
Jens Axboe895e2ca2019-12-17 08:46:33 -0700749 if ((work_flags & IO_WQ_WORK_CONCURRENT) ||
750 !atomic_read(&acct->nr_running))
Jens Axboec5def4a2019-11-07 11:41:16 -0700751 io_wqe_wake_worker(wqe, acct);
Jens Axboe771b53d02019-10-22 10:25:58 -0600752}
753
754void io_wq_enqueue(struct io_wq *wq, struct io_wq_work *work)
755{
756 struct io_wqe *wqe = wq->wqes[numa_node_id()];
757
758 io_wqe_enqueue(wqe, work);
759}
760
761/*
Pavel Begunkov8766dd52020-03-14 00:31:04 +0300762 * Work items that hash to the same value will not be done in parallel.
763 * Used to limit concurrent writes, generally hashed by inode.
Jens Axboe771b53d02019-10-22 10:25:58 -0600764 */
Pavel Begunkov8766dd52020-03-14 00:31:04 +0300765void io_wq_hash_work(struct io_wq_work *work, void *val)
Jens Axboe771b53d02019-10-22 10:25:58 -0600766{
Pavel Begunkov8766dd52020-03-14 00:31:04 +0300767 unsigned int bit;
Jens Axboe771b53d02019-10-22 10:25:58 -0600768
769 bit = hash_ptr(val, IO_WQ_HASH_ORDER);
770 work->flags |= (IO_WQ_WORK_HASHED | (bit << IO_WQ_HASH_SHIFT));
Jens Axboe771b53d02019-10-22 10:25:58 -0600771}
772
Pavel Begunkov2293b412020-03-07 01:15:39 +0300773static bool io_wq_worker_cancel(struct io_worker *worker, void *data)
Jens Axboe62755e32019-10-28 21:49:21 -0600774{
Pavel Begunkov2293b412020-03-07 01:15:39 +0300775 struct io_cb_cancel_data *match = data;
Jens Axboe6f726532019-11-05 13:51:51 -0700776 unsigned long flags;
Jens Axboe62755e32019-10-28 21:49:21 -0600777
778 /*
779 * Hold the lock to avoid ->cur_work going out of scope, caller
Jens Axboe36c2f922019-11-13 09:43:34 -0700780 * may dereference the passed in work.
Jens Axboe62755e32019-10-28 21:49:21 -0600781 */
Jens Axboe36c2f922019-11-13 09:43:34 -0700782 spin_lock_irqsave(&worker->lock, flags);
Jens Axboe62755e32019-10-28 21:49:21 -0600783 if (worker->cur_work &&
Pavel Begunkov2293b412020-03-07 01:15:39 +0300784 match->fn(worker->cur_work, match->data)) {
Jens Axboe3bfe6102021-02-16 14:15:30 -0700785 set_notify_signal(worker->task);
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300786 match->nr_running++;
Jens Axboe771b53d02019-10-22 10:25:58 -0600787 }
Jens Axboe36c2f922019-11-13 09:43:34 -0700788 spin_unlock_irqrestore(&worker->lock, flags);
Jens Axboe771b53d02019-10-22 10:25:58 -0600789
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300790 return match->nr_running && !match->cancel_all;
Jens Axboe771b53d02019-10-22 10:25:58 -0600791}
792
Pavel Begunkov204361a2020-08-23 20:33:10 +0300793static inline void io_wqe_remove_pending(struct io_wqe *wqe,
794 struct io_wq_work *work,
795 struct io_wq_work_node *prev)
796{
797 unsigned int hash = io_get_work_hash(work);
798 struct io_wq_work *prev_work = NULL;
799
800 if (io_wq_is_hashed(work) && work == wqe->hash_tail[hash]) {
801 if (prev)
802 prev_work = container_of(prev, struct io_wq_work, list);
803 if (prev_work && io_get_work_hash(prev_work) == hash)
804 wqe->hash_tail[hash] = prev_work;
805 else
806 wqe->hash_tail[hash] = NULL;
807 }
808 wq_list_del(&wqe->work_list, &work->list, prev);
809}
810
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300811static void io_wqe_cancel_pending_work(struct io_wqe *wqe,
Pavel Begunkovf4c26652020-06-15 10:24:02 +0300812 struct io_cb_cancel_data *match)
Jens Axboe771b53d02019-10-22 10:25:58 -0600813{
Jens Axboe6206f0e2019-11-26 11:59:32 -0700814 struct io_wq_work_node *node, *prev;
Jens Axboe771b53d02019-10-22 10:25:58 -0600815 struct io_wq_work *work;
Jens Axboe6f726532019-11-05 13:51:51 -0700816 unsigned long flags;
Jens Axboe771b53d02019-10-22 10:25:58 -0600817
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300818retry:
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200819 raw_spin_lock_irqsave(&wqe->lock, flags);
Jens Axboe6206f0e2019-11-26 11:59:32 -0700820 wq_list_for_each(node, prev, &wqe->work_list) {
821 work = container_of(node, struct io_wq_work, list);
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300822 if (!match->fn(work, match->data))
823 continue;
Pavel Begunkov204361a2020-08-23 20:33:10 +0300824 io_wqe_remove_pending(wqe, work, prev);
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200825 raw_spin_unlock_irqrestore(&wqe->lock, flags);
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300826 io_run_cancel(work, wqe);
827 match->nr_pending++;
828 if (!match->cancel_all)
829 return;
830
831 /* not safe to continue after unlock */
832 goto retry;
Jens Axboe771b53d02019-10-22 10:25:58 -0600833 }
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200834 raw_spin_unlock_irqrestore(&wqe->lock, flags);
Pavel Begunkovf4c26652020-06-15 10:24:02 +0300835}
Jens Axboe771b53d02019-10-22 10:25:58 -0600836
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300837static void io_wqe_cancel_running_work(struct io_wqe *wqe,
Pavel Begunkovf4c26652020-06-15 10:24:02 +0300838 struct io_cb_cancel_data *match)
839{
Jens Axboe771b53d02019-10-22 10:25:58 -0600840 rcu_read_lock();
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300841 io_wq_for_each_worker(wqe, io_wq_worker_cancel, match);
Jens Axboe771b53d02019-10-22 10:25:58 -0600842 rcu_read_unlock();
Jens Axboe771b53d02019-10-22 10:25:58 -0600843}
844
Pavel Begunkov2293b412020-03-07 01:15:39 +0300845enum io_wq_cancel io_wq_cancel_cb(struct io_wq *wq, work_cancel_fn *cancel,
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300846 void *data, bool cancel_all)
Pavel Begunkov2293b412020-03-07 01:15:39 +0300847{
848 struct io_cb_cancel_data match = {
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300849 .fn = cancel,
850 .data = data,
851 .cancel_all = cancel_all,
Pavel Begunkov2293b412020-03-07 01:15:39 +0300852 };
Pavel Begunkov2293b412020-03-07 01:15:39 +0300853 int node;
854
Pavel Begunkovf4c26652020-06-15 10:24:02 +0300855 /*
856 * First check pending list, if we're lucky we can just remove it
857 * from there. CANCEL_OK means that the work is returned as-new,
858 * no completion will be posted for it.
859 */
Pavel Begunkov2293b412020-03-07 01:15:39 +0300860 for_each_node(node) {
861 struct io_wqe *wqe = wq->wqes[node];
862
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300863 io_wqe_cancel_pending_work(wqe, &match);
864 if (match.nr_pending && !match.cancel_all)
Pavel Begunkovf4c26652020-06-15 10:24:02 +0300865 return IO_WQ_CANCEL_OK;
Pavel Begunkov2293b412020-03-07 01:15:39 +0300866 }
867
Pavel Begunkovf4c26652020-06-15 10:24:02 +0300868 /*
869 * Now check if a free (going busy) or busy worker has the work
870 * currently running. If we find it there, we'll return CANCEL_RUNNING
871 * as an indication that we attempt to signal cancellation. The
872 * completion will run normally in this case.
873 */
874 for_each_node(node) {
875 struct io_wqe *wqe = wq->wqes[node];
876
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300877 io_wqe_cancel_running_work(wqe, &match);
878 if (match.nr_running && !match.cancel_all)
Pavel Begunkovf4c26652020-06-15 10:24:02 +0300879 return IO_WQ_CANCEL_RUNNING;
880 }
881
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300882 if (match.nr_running)
883 return IO_WQ_CANCEL_RUNNING;
884 if (match.nr_pending)
885 return IO_WQ_CANCEL_OK;
Pavel Begunkovf4c26652020-06-15 10:24:02 +0300886 return IO_WQ_CANCEL_NOTFOUND;
Pavel Begunkov2293b412020-03-07 01:15:39 +0300887}
888
Jens Axboee9418942021-02-19 12:33:30 -0700889static int io_wqe_hash_wake(struct wait_queue_entry *wait, unsigned mode,
890 int sync, void *key)
891{
892 struct io_wqe *wqe = container_of(wait, struct io_wqe, wait);
Jens Axboee9418942021-02-19 12:33:30 -0700893
894 list_del_init(&wait->entry);
895
896 rcu_read_lock();
Jens Axboe685fe7f2021-03-08 09:37:51 -0700897 io_wqe_activate_free_worker(wqe);
Jens Axboee9418942021-02-19 12:33:30 -0700898 rcu_read_unlock();
Jens Axboee9418942021-02-19 12:33:30 -0700899 return 1;
900}
901
Jens Axboe576a3472019-11-25 08:49:20 -0700902struct io_wq *io_wq_create(unsigned bounded, struct io_wq_data *data)
Jens Axboe771b53d02019-10-22 10:25:58 -0600903{
Jann Horn3fc50ab2019-11-26 19:10:20 +0100904 int ret = -ENOMEM, node;
Jens Axboe771b53d02019-10-22 10:25:58 -0600905 struct io_wq *wq;
906
Pavel Begunkovf5fa38c2020-06-08 21:08:20 +0300907 if (WARN_ON_ONCE(!data->free_work || !data->do_work))
Pavel Begunkove9fd9392020-03-04 16:14:12 +0300908 return ERR_PTR(-EINVAL);
909
Jann Hornad6e0052019-11-26 17:39:45 +0100910 wq = kzalloc(sizeof(*wq), GFP_KERNEL);
Jens Axboe771b53d02019-10-22 10:25:58 -0600911 if (!wq)
912 return ERR_PTR(-ENOMEM);
913
Jann Horn3fc50ab2019-11-26 19:10:20 +0100914 wq->wqes = kcalloc(nr_node_ids, sizeof(struct io_wqe *), GFP_KERNEL);
Jens Axboe43c01fb2020-10-22 09:02:50 -0600915 if (!wq->wqes)
916 goto err_wq;
917
918 ret = cpuhp_state_add_instance_nocalls(io_wq_online, &wq->cpuhp_node);
919 if (ret)
920 goto err_wqes;
Jens Axboe771b53d02019-10-22 10:25:58 -0600921
Jens Axboee9418942021-02-19 12:33:30 -0700922 refcount_inc(&data->hash->refs);
923 wq->hash = data->hash;
Pavel Begunkove9fd9392020-03-04 16:14:12 +0300924 wq->free_work = data->free_work;
Pavel Begunkovf5fa38c2020-06-08 21:08:20 +0300925 wq->do_work = data->do_work;
Jens Axboe7d723062019-11-12 22:31:31 -0700926
Jens Axboe43c01fb2020-10-22 09:02:50 -0600927 ret = -ENOMEM;
Jann Horn3fc50ab2019-11-26 19:10:20 +0100928 for_each_node(node) {
Jens Axboe771b53d02019-10-22 10:25:58 -0600929 struct io_wqe *wqe;
Jens Axboe75634392020-02-11 06:30:06 -0700930 int alloc_node = node;
Jens Axboe771b53d02019-10-22 10:25:58 -0600931
Jens Axboe75634392020-02-11 06:30:06 -0700932 if (!node_online(alloc_node))
933 alloc_node = NUMA_NO_NODE;
934 wqe = kzalloc_node(sizeof(struct io_wqe), GFP_KERNEL, alloc_node);
Jens Axboe771b53d02019-10-22 10:25:58 -0600935 if (!wqe)
Jann Horn3fc50ab2019-11-26 19:10:20 +0100936 goto err;
937 wq->wqes[node] = wqe;
Jens Axboe75634392020-02-11 06:30:06 -0700938 wqe->node = alloc_node;
Jens Axboe685fe7f2021-03-08 09:37:51 -0700939 wqe->acct[IO_WQ_ACCT_BOUND].index = IO_WQ_ACCT_BOUND;
940 wqe->acct[IO_WQ_ACCT_UNBOUND].index = IO_WQ_ACCT_UNBOUND;
Jens Axboec5def4a2019-11-07 11:41:16 -0700941 wqe->acct[IO_WQ_ACCT_BOUND].max_workers = bounded;
942 atomic_set(&wqe->acct[IO_WQ_ACCT_BOUND].nr_running, 0);
Jens Axboe728f13e2021-02-21 16:02:53 -0700943 wqe->acct[IO_WQ_ACCT_UNBOUND].max_workers =
Jens Axboec5def4a2019-11-07 11:41:16 -0700944 task_rlimit(current, RLIMIT_NPROC);
Jens Axboec5def4a2019-11-07 11:41:16 -0700945 atomic_set(&wqe->acct[IO_WQ_ACCT_UNBOUND].nr_running, 0);
Jens Axboee9418942021-02-19 12:33:30 -0700946 wqe->wait.func = io_wqe_hash_wake;
947 INIT_LIST_HEAD(&wqe->wait.entry);
Jens Axboe771b53d02019-10-22 10:25:58 -0600948 wqe->wq = wq;
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200949 raw_spin_lock_init(&wqe->lock);
Jens Axboe6206f0e2019-11-26 11:59:32 -0700950 INIT_WQ_LIST(&wqe->work_list);
Jens Axboe021d1cd2019-11-14 08:00:41 -0700951 INIT_HLIST_NULLS_HEAD(&wqe->free_list, 0);
Jens Axboee61df662019-11-13 13:54:49 -0700952 INIT_LIST_HEAD(&wqe->all_list);
Jens Axboe771b53d02019-10-22 10:25:58 -0600953 }
954
Jens Axboe685fe7f2021-03-08 09:37:51 -0700955 wq->task = get_task_struct(data->task);
Jens Axboe3bfe6102021-02-16 14:15:30 -0700956 refcount_set(&wq->refs, 1);
Jens Axboe685fe7f2021-03-08 09:37:51 -0700957 atomic_set(&wq->worker_refs, 1);
958 init_completion(&wq->worker_done);
959 return wq;
Jens Axboeb60fda62019-11-19 08:37:07 -0700960err:
Jens Axboedc7bbc92021-03-01 09:09:56 -0700961 io_wq_put_hash(data->hash);
Jens Axboe43c01fb2020-10-22 09:02:50 -0600962 cpuhp_state_remove_instance_nocalls(io_wq_online, &wq->cpuhp_node);
Jann Horn3fc50ab2019-11-26 19:10:20 +0100963 for_each_node(node)
964 kfree(wq->wqes[node]);
Jens Axboe43c01fb2020-10-22 09:02:50 -0600965err_wqes:
Jens Axboeb60fda62019-11-19 08:37:07 -0700966 kfree(wq->wqes);
Jens Axboe43c01fb2020-10-22 09:02:50 -0600967err_wq:
Jens Axboeb60fda62019-11-19 08:37:07 -0700968 kfree(wq);
Jens Axboe771b53d02019-10-22 10:25:58 -0600969 return ERR_PTR(ret);
970}
971
Jens Axboec80ca472021-04-01 19:57:07 -0600972static bool io_task_work_match(struct callback_head *cb, void *data)
973{
974 struct create_worker_data *cwd;
975
976 if (cb->func != create_worker_cb)
977 return false;
978 cwd = container_of(cb, struct create_worker_data, work);
979 return cwd->wqe->wq == data;
980}
981
Jens Axboe685fe7f2021-03-08 09:37:51 -0700982static void io_wq_exit_workers(struct io_wq *wq)
Jens Axboeafcc4012021-02-26 13:48:19 -0700983{
Jens Axboe685fe7f2021-03-08 09:37:51 -0700984 struct callback_head *cb;
985 int node;
986
987 set_bit(IO_WQ_BIT_EXIT, &wq->state);
988
989 if (!wq->task)
990 return;
991
Jens Axboec80ca472021-04-01 19:57:07 -0600992 while ((cb = task_work_cancel_match(wq->task, io_task_work_match, wq)) != NULL) {
Jens Axboe685fe7f2021-03-08 09:37:51 -0700993 struct create_worker_data *cwd;
994
995 cwd = container_of(cb, struct create_worker_data, work);
996 atomic_dec(&cwd->wqe->acct[cwd->index].nr_running);
997 io_worker_ref_put(wq);
998 kfree(cwd);
Jens Axboeafcc4012021-02-26 13:48:19 -0700999 }
Jens Axboe685fe7f2021-03-08 09:37:51 -07001000
1001 rcu_read_lock();
1002 for_each_node(node) {
1003 struct io_wqe *wqe = wq->wqes[node];
1004
1005 io_wq_for_each_worker(wqe, io_wq_worker_wake, NULL);
1006 spin_lock_irq(&wq->hash->wait.lock);
1007 list_del_init(&wq->wqes[node]->wait.entry);
1008 spin_unlock_irq(&wq->hash->wait.lock);
1009 }
1010 rcu_read_unlock();
1011 io_worker_ref_put(wq);
1012 wait_for_completion(&wq->worker_done);
1013 put_task_struct(wq->task);
1014 wq->task = NULL;
Jens Axboeafcc4012021-02-26 13:48:19 -07001015}
1016
Jens Axboe4fb6ac32021-02-25 10:17:09 -07001017static void io_wq_destroy(struct io_wq *wq)
Jens Axboe771b53d02019-10-22 10:25:58 -06001018{
Jann Horn3fc50ab2019-11-26 19:10:20 +01001019 int node;
Jens Axboe771b53d02019-10-22 10:25:58 -06001020
Jens Axboe43c01fb2020-10-22 09:02:50 -06001021 cpuhp_state_remove_instance_nocalls(io_wq_online, &wq->cpuhp_node);
1022
Jens Axboe685fe7f2021-03-08 09:37:51 -07001023 io_wq_exit_workers(wq);
Jens Axboe771b53d02019-10-22 10:25:58 -06001024
Jens Axboee9418942021-02-19 12:33:30 -07001025 for_each_node(node) {
1026 struct io_wqe *wqe = wq->wqes[node];
Jens Axboef5d2d232021-03-25 10:16:12 -06001027 struct io_cb_cancel_data match = {
1028 .fn = io_wq_work_match_all,
1029 .cancel_all = true,
1030 };
1031 io_wqe_cancel_pending_work(wqe, &match);
Jens Axboee9418942021-02-19 12:33:30 -07001032 kfree(wqe);
1033 }
Jens Axboee9418942021-02-19 12:33:30 -07001034 io_wq_put_hash(wq->hash);
Jens Axboe771b53d02019-10-22 10:25:58 -06001035 kfree(wq->wqes);
1036 kfree(wq);
Jens Axboe4fb6ac32021-02-25 10:17:09 -07001037}
1038
1039void io_wq_put(struct io_wq *wq)
1040{
1041 if (refcount_dec_and_test(&wq->refs))
1042 io_wq_destroy(wq);
Jens Axboe771b53d02019-10-22 10:25:58 -06001043}
Jens Axboe848f7e12020-01-23 15:33:32 -07001044
Jens Axboeafcc4012021-02-26 13:48:19 -07001045void io_wq_put_and_exit(struct io_wq *wq)
1046{
Jens Axboe685fe7f2021-03-08 09:37:51 -07001047 io_wq_exit_workers(wq);
Jens Axboeafcc4012021-02-26 13:48:19 -07001048 io_wq_put(wq);
1049}
1050
Jens Axboe43c01fb2020-10-22 09:02:50 -06001051static bool io_wq_worker_affinity(struct io_worker *worker, void *data)
1052{
Peter Zijlstrae0051d72021-04-08 11:44:50 +02001053 set_cpus_allowed_ptr(worker->task, cpumask_of_node(worker->wqe->node));
Jens Axboe43c01fb2020-10-22 09:02:50 -06001054
Jens Axboe43c01fb2020-10-22 09:02:50 -06001055 return false;
1056}
1057
1058static int io_wq_cpu_online(unsigned int cpu, struct hlist_node *node)
1059{
1060 struct io_wq *wq = hlist_entry_safe(node, struct io_wq, cpuhp_node);
1061 int i;
1062
1063 rcu_read_lock();
1064 for_each_node(i)
1065 io_wq_for_each_worker(wq->wqes[i], io_wq_worker_affinity, NULL);
1066 rcu_read_unlock();
1067 return 0;
1068}
1069
1070static __init int io_wq_init(void)
1071{
1072 int ret;
1073
1074 ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "io-wq/online",
1075 io_wq_cpu_online, NULL);
1076 if (ret < 0)
1077 return ret;
1078 io_wq_online = ret;
1079 return 0;
1080}
1081subsys_initcall(io_wq_init);