blob: f058ea0bcae8a56c6a7d3703159dfd39ad0d8e99 [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 {
Jens Axboe771b53d02019-10-22 10:25:58 -0600105 unsigned long state;
Jens Axboe771b53d02019-10-22 10:25:58 -0600106
Pavel Begunkove9fd9392020-03-04 16:14:12 +0300107 free_work_fn *free_work;
Pavel Begunkovf5fa38c2020-06-08 21:08:20 +0300108 io_wq_work_fn *do_work;
Jens Axboe7d723062019-11-12 22:31:31 -0700109
Jens Axboee9418942021-02-19 12:33:30 -0700110 struct io_wq_hash *hash;
111
Jens Axboefb3a1f62021-02-26 09:47:20 -0700112 atomic_t worker_refs;
113 struct completion worker_done;
114
Jens Axboe43c01fb2020-10-22 09:02:50 -0600115 struct hlist_node cpuhp_node;
Jens Axboe3bfe6102021-02-16 14:15:30 -0700116
Jens Axboe685fe7f2021-03-08 09:37:51 -0700117 struct task_struct *task;
Pavel Begunkovc7f405d2021-06-14 02:36:12 +0100118
119 struct io_wqe *wqes[];
Jens Axboe771b53d02019-10-22 10:25:58 -0600120};
121
Jens Axboe43c01fb2020-10-22 09:02:50 -0600122static enum cpuhp_state io_wq_online;
123
Jens Axboef0127252021-03-03 15:47:04 -0700124struct io_cb_cancel_data {
125 work_cancel_fn *fn;
126 void *data;
127 int nr_running;
128 int nr_pending;
129 bool cancel_all;
130};
131
Jens Axboe685fe7f2021-03-08 09:37:51 -0700132static void create_io_worker(struct io_wq *wq, struct io_wqe *wqe, int index);
Jens Axboef0127252021-03-03 15:47:04 -0700133
Jens Axboe771b53d02019-10-22 10:25:58 -0600134static bool io_worker_get(struct io_worker *worker)
135{
136 return refcount_inc_not_zero(&worker->ref);
137}
138
139static void io_worker_release(struct io_worker *worker)
140{
141 if (refcount_dec_and_test(&worker->ref))
Jens Axboeeb2de942021-02-23 19:59:06 -0700142 complete(&worker->ref_done);
Jens Axboe771b53d02019-10-22 10:25:58 -0600143}
144
Pavel Begunkov8418f222021-03-22 01:58:28 +0000145static inline struct io_wqe_acct *io_get_acct(struct io_wqe *wqe, bool bound)
146{
147 return &wqe->acct[bound ? IO_WQ_ACCT_BOUND : IO_WQ_ACCT_UNBOUND];
148}
149
Jens Axboec5def4a2019-11-07 11:41:16 -0700150static inline struct io_wqe_acct *io_work_get_acct(struct io_wqe *wqe,
151 struct io_wq_work *work)
152{
Pavel Begunkov8418f222021-03-22 01:58:28 +0000153 return io_get_acct(wqe, !(work->flags & IO_WQ_WORK_UNBOUND));
Jens Axboec5def4a2019-11-07 11:41:16 -0700154}
155
Jens Axboe958234d2021-02-17 09:00:57 -0700156static inline struct io_wqe_acct *io_wqe_get_acct(struct io_worker *worker)
Jens Axboec5def4a2019-11-07 11:41:16 -0700157{
Pavel Begunkov8418f222021-03-22 01:58:28 +0000158 return io_get_acct(worker->wqe, worker->flags & IO_WORKER_F_BOUND);
Jens Axboec5def4a2019-11-07 11:41:16 -0700159}
160
Jens Axboe685fe7f2021-03-08 09:37:51 -0700161static void io_worker_ref_put(struct io_wq *wq)
162{
163 if (atomic_dec_and_test(&wq->worker_refs))
164 complete(&wq->worker_done);
165}
166
Jens Axboe771b53d02019-10-22 10:25:58 -0600167static void io_worker_exit(struct io_worker *worker)
168{
169 struct io_wqe *wqe = worker->wqe;
Jens Axboe958234d2021-02-17 09:00:57 -0700170 struct io_wqe_acct *acct = io_wqe_get_acct(worker);
Jens Axboebf1daa42021-02-16 18:00:55 -0700171 unsigned flags;
Jens Axboe771b53d02019-10-22 10:25:58 -0600172
Jens Axboeeb2de942021-02-23 19:59:06 -0700173 if (refcount_dec_and_test(&worker->ref))
174 complete(&worker->ref_done);
175 wait_for_completion(&worker->ref_done);
Jens Axboe771b53d02019-10-22 10:25:58 -0600176
177 preempt_disable();
178 current->flags &= ~PF_IO_WORKER;
Jens Axboebf1daa42021-02-16 18:00:55 -0700179 flags = worker->flags;
180 worker->flags = 0;
181 if (flags & IO_WORKER_F_RUNNING)
Jens Axboec5def4a2019-11-07 11:41:16 -0700182 atomic_dec(&acct->nr_running);
Jens Axboe771b53d02019-10-22 10:25:58 -0600183 worker->flags = 0;
184 preempt_enable();
185
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200186 raw_spin_lock_irq(&wqe->lock);
Jens Axboebf1daa42021-02-16 18:00:55 -0700187 if (flags & IO_WORKER_F_FREE)
188 hlist_nulls_del_rcu(&worker->nulls_node);
Jens Axboee61df662019-11-13 13:54:49 -0700189 list_del_rcu(&worker->all_list);
Jens Axboec5def4a2019-11-07 11:41:16 -0700190 acct->nr_workers--;
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200191 raw_spin_unlock_irq(&wqe->lock);
Jens Axboe771b53d02019-10-22 10:25:58 -0600192
YueHaibing364b05f2019-11-02 15:55:01 +0800193 kfree_rcu(worker, rcu);
Jens Axboe685fe7f2021-03-08 09:37:51 -0700194 io_worker_ref_put(wqe->wq);
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,
Jens Axboe685fe7f2021-03-08 09:37:51 -0700209 * caller must create one.
Jens Axboec5def4a2019-11-07 11:41:16 -0700210 */
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
Jens Axboe685fe7f2021-03-08 09:37:51 -0700233 * below the max number of workers, create one.
Jens Axboec5def4a2019-11-07 11:41:16 -0700234 */
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
Jens Axboe685fe7f2021-03-08 09:37:51 -0700249 if (!ret && acct->nr_workers < acct->max_workers) {
250 atomic_inc(&acct->nr_running);
251 atomic_inc(&wqe->wq->worker_refs);
252 create_io_worker(wqe->wq, wqe, acct->index);
253 }
Jens Axboec5def4a2019-11-07 11:41:16 -0700254}
255
Jens Axboe958234d2021-02-17 09:00:57 -0700256static void io_wqe_inc_running(struct io_worker *worker)
Jens Axboec5def4a2019-11-07 11:41:16 -0700257{
Jens Axboe958234d2021-02-17 09:00:57 -0700258 struct io_wqe_acct *acct = io_wqe_get_acct(worker);
Jens Axboec5def4a2019-11-07 11:41:16 -0700259
260 atomic_inc(&acct->nr_running);
261}
262
Jens Axboe685fe7f2021-03-08 09:37:51 -0700263struct create_worker_data {
264 struct callback_head work;
265 struct io_wqe *wqe;
266 int index;
267};
268
269static void create_worker_cb(struct callback_head *cb)
270{
271 struct create_worker_data *cwd;
272 struct io_wq *wq;
273
274 cwd = container_of(cb, struct create_worker_data, work);
275 wq = cwd->wqe->wq;
276 create_io_worker(wq, cwd->wqe, cwd->index);
277 kfree(cwd);
278}
279
280static void io_queue_worker_create(struct io_wqe *wqe, struct io_wqe_acct *acct)
281{
282 struct create_worker_data *cwd;
283 struct io_wq *wq = wqe->wq;
284
285 /* raced with exit, just ignore create call */
286 if (test_bit(IO_WQ_BIT_EXIT, &wq->state))
287 goto fail;
288
289 cwd = kmalloc(sizeof(*cwd), GFP_ATOMIC);
290 if (cwd) {
291 init_task_work(&cwd->work, create_worker_cb);
292 cwd->wqe = wqe;
293 cwd->index = acct->index;
294 if (!task_work_add(wq->task, &cwd->work, TWA_SIGNAL))
295 return;
296
297 kfree(cwd);
298 }
299fail:
300 atomic_dec(&acct->nr_running);
301 io_worker_ref_put(wq);
302}
303
Jens Axboe958234d2021-02-17 09:00:57 -0700304static void io_wqe_dec_running(struct io_worker *worker)
Jens Axboec5def4a2019-11-07 11:41:16 -0700305 __must_hold(wqe->lock)
306{
Jens Axboe958234d2021-02-17 09:00:57 -0700307 struct io_wqe_acct *acct = io_wqe_get_acct(worker);
308 struct io_wqe *wqe = worker->wqe;
Jens Axboec5def4a2019-11-07 11:41:16 -0700309
Jens Axboe685fe7f2021-03-08 09:37:51 -0700310 if (!(worker->flags & IO_WORKER_F_UP))
311 return;
312
313 if (atomic_dec_and_test(&acct->nr_running) && io_wqe_run_queue(wqe)) {
314 atomic_inc(&acct->nr_running);
315 atomic_inc(&wqe->wq->worker_refs);
316 io_queue_worker_create(wqe, acct);
317 }
Jens Axboec5def4a2019-11-07 11:41:16 -0700318}
319
Jens Axboe771b53d02019-10-22 10:25:58 -0600320/*
321 * Worker will start processing some work. Move it to the busy list, if
322 * it's currently on the freelist
323 */
324static void __io_worker_busy(struct io_wqe *wqe, struct io_worker *worker,
325 struct io_wq_work *work)
326 __must_hold(wqe->lock)
327{
Jens Axboec5def4a2019-11-07 11:41:16 -0700328 bool worker_bound, work_bound;
329
Hao Xu417b5052021-04-06 11:08:45 +0800330 BUILD_BUG_ON((IO_WQ_ACCT_UNBOUND ^ IO_WQ_ACCT_BOUND) != 1);
331
Jens Axboe771b53d02019-10-22 10:25:58 -0600332 if (worker->flags & IO_WORKER_F_FREE) {
333 worker->flags &= ~IO_WORKER_F_FREE;
334 hlist_nulls_del_init_rcu(&worker->nulls_node);
Jens Axboe771b53d02019-10-22 10:25:58 -0600335 }
Jens Axboec5def4a2019-11-07 11:41:16 -0700336
337 /*
338 * If worker is moving from bound to unbound (or vice versa), then
339 * ensure we update the running accounting.
340 */
Dan Carpenterb2e9c7d62019-11-19 09:22:16 +0300341 worker_bound = (worker->flags & IO_WORKER_F_BOUND) != 0;
342 work_bound = (work->flags & IO_WQ_WORK_UNBOUND) == 0;
343 if (worker_bound != work_bound) {
Hao Xu417b5052021-04-06 11:08:45 +0800344 int index = work_bound ? IO_WQ_ACCT_UNBOUND : IO_WQ_ACCT_BOUND;
Jens Axboe958234d2021-02-17 09:00:57 -0700345 io_wqe_dec_running(worker);
Hao Xu417b5052021-04-06 11:08:45 +0800346 worker->flags ^= IO_WORKER_F_BOUND;
347 wqe->acct[index].nr_workers--;
348 wqe->acct[index ^ 1].nr_workers++;
Jens Axboe958234d2021-02-17 09:00:57 -0700349 io_wqe_inc_running(worker);
Jens Axboec5def4a2019-11-07 11:41:16 -0700350 }
Jens Axboe771b53d02019-10-22 10:25:58 -0600351}
352
353/*
354 * No work, worker going to sleep. Move to freelist, and unuse mm if we
355 * have one attached. Dropping the mm may potentially sleep, so we drop
356 * the lock in that case and return success. Since the caller has to
357 * retry the loop in that case (we changed task state), we don't regrab
358 * the lock if we return success.
359 */
Jens Axboec6d77d92021-02-15 13:26:34 -0700360static void __io_worker_idle(struct io_wqe *wqe, struct io_worker *worker)
Jens Axboe771b53d02019-10-22 10:25:58 -0600361 __must_hold(wqe->lock)
362{
363 if (!(worker->flags & IO_WORKER_F_FREE)) {
364 worker->flags |= IO_WORKER_F_FREE;
Jens Axboe021d1cd2019-11-14 08:00:41 -0700365 hlist_nulls_add_head_rcu(&worker->nulls_node, &wqe->free_list);
Jens Axboe771b53d02019-10-22 10:25:58 -0600366 }
Jens Axboe771b53d02019-10-22 10:25:58 -0600367}
368
Pavel Begunkov60cf46a2020-03-14 00:31:05 +0300369static inline unsigned int io_get_work_hash(struct io_wq_work *work)
370{
371 return work->flags >> IO_WQ_HASH_SHIFT;
372}
373
Jens Axboee9418942021-02-19 12:33:30 -0700374static void io_wait_on_hash(struct io_wqe *wqe, unsigned int hash)
375{
376 struct io_wq *wq = wqe->wq;
377
378 spin_lock(&wq->hash->wait.lock);
379 if (list_empty(&wqe->wait.entry)) {
380 __add_wait_queue(&wq->hash->wait, &wqe->wait);
381 if (!test_bit(hash, &wq->hash->map)) {
382 __set_current_state(TASK_RUNNING);
383 list_del_init(&wqe->wait.entry);
384 }
385 }
386 spin_unlock(&wq->hash->wait.lock);
387}
388
Pavel Begunkov60cf46a2020-03-14 00:31:05 +0300389static struct io_wq_work *io_get_next_work(struct io_wqe *wqe)
Jens Axboe771b53d02019-10-22 10:25:58 -0600390 __must_hold(wqe->lock)
391{
Jens Axboe6206f0e2019-11-26 11:59:32 -0700392 struct io_wq_work_node *node, *prev;
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300393 struct io_wq_work *work, *tail;
Jens Axboee9418942021-02-19 12:33:30 -0700394 unsigned int stall_hash = -1U;
Jens Axboe771b53d02019-10-22 10:25:58 -0600395
Jens Axboe6206f0e2019-11-26 11:59:32 -0700396 wq_list_for_each(node, prev, &wqe->work_list) {
Jens Axboee9418942021-02-19 12:33:30 -0700397 unsigned int hash;
398
Jens Axboe6206f0e2019-11-26 11:59:32 -0700399 work = container_of(node, struct io_wq_work, list);
400
Jens Axboe771b53d02019-10-22 10:25:58 -0600401 /* not hashed, can run anytime */
Pavel Begunkov8766dd52020-03-14 00:31:04 +0300402 if (!io_wq_is_hashed(work)) {
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300403 wq_list_del(&wqe->work_list, node, prev);
Jens Axboe771b53d02019-10-22 10:25:58 -0600404 return work;
405 }
406
Pavel Begunkov60cf46a2020-03-14 00:31:05 +0300407 hash = io_get_work_hash(work);
Jens Axboee9418942021-02-19 12:33:30 -0700408 /* all items with this hash lie in [work, tail] */
409 tail = wqe->hash_tail[hash];
410
411 /* hashed, can run if not already running */
412 if (!test_and_set_bit(hash, &wqe->wq->hash->map)) {
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300413 wqe->hash_tail[hash] = NULL;
414 wq_list_cut(&wqe->work_list, &tail->list, prev);
Jens Axboe771b53d02019-10-22 10:25:58 -0600415 return work;
416 }
Jens Axboee9418942021-02-19 12:33:30 -0700417 if (stall_hash == -1U)
418 stall_hash = hash;
419 /* fast forward to a next hash, for-each will fix up @prev */
420 node = &tail->list;
421 }
422
423 if (stall_hash != -1U) {
424 raw_spin_unlock(&wqe->lock);
425 io_wait_on_hash(wqe, stall_hash);
426 raw_spin_lock(&wqe->lock);
Jens Axboe771b53d02019-10-22 10:25:58 -0600427 }
428
429 return NULL;
430}
431
Jens Axboe00ddff42021-03-21 07:06:56 -0600432static bool io_flush_signals(void)
Jens Axboecccf0ee2020-01-27 16:34:48 -0700433{
Jens Axboe0b8cfa92021-03-21 14:16:08 -0600434 if (unlikely(test_thread_flag(TIF_NOTIFY_SIGNAL))) {
Jens Axboe00ddff42021-03-21 07:06:56 -0600435 __set_current_state(TASK_RUNNING);
Jens Axboe0b8cfa92021-03-21 14:16:08 -0600436 tracehook_notify_signal();
Jens Axboe00ddff42021-03-21 07:06:56 -0600437 return true;
Jens Axboecccf0ee2020-01-27 16:34:48 -0700438 }
Jens Axboe00ddff42021-03-21 07:06:56 -0600439 return false;
Pavel Begunkovdc026a72020-03-04 16:14:09 +0300440}
441
442static void io_assign_current_work(struct io_worker *worker,
443 struct io_wq_work *work)
444{
Pavel Begunkovd78298e2020-03-14 00:31:03 +0300445 if (work) {
Jens Axboe3bfe6102021-02-16 14:15:30 -0700446 io_flush_signals();
Pavel Begunkovd78298e2020-03-14 00:31:03 +0300447 cond_resched();
448 }
Pavel Begunkovdc026a72020-03-04 16:14:09 +0300449
450 spin_lock_irq(&worker->lock);
451 worker->cur_work = work;
452 spin_unlock_irq(&worker->lock);
453}
454
Pavel Begunkov60cf46a2020-03-14 00:31:05 +0300455static void io_wqe_enqueue(struct io_wqe *wqe, struct io_wq_work *work);
456
Jens Axboe771b53d02019-10-22 10:25:58 -0600457static void io_worker_handle_work(struct io_worker *worker)
458 __releases(wqe->lock)
459{
Jens Axboe771b53d02019-10-22 10:25:58 -0600460 struct io_wqe *wqe = worker->wqe;
461 struct io_wq *wq = wqe->wq;
Pavel Begunkovc60eb042021-04-08 01:54:42 +0100462 bool do_kill = test_bit(IO_WQ_BIT_EXIT, &wq->state);
Jens Axboe771b53d02019-10-22 10:25:58 -0600463
464 do {
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300465 struct io_wq_work *work;
Pavel Begunkovf462fd32020-03-04 16:14:11 +0300466get_next:
Jens Axboe771b53d02019-10-22 10:25:58 -0600467 /*
Jens Axboe771b53d02019-10-22 10:25:58 -0600468 * If we got some work, mark us as busy. If we didn't, but
469 * the list isn't empty, it means we stalled on hashed work.
470 * Mark us stalled so we don't keep looking for work when we
471 * can't make progress, any work completion or insertion will
472 * clear the stalled flag.
473 */
Pavel Begunkov60cf46a2020-03-14 00:31:05 +0300474 work = io_get_next_work(wqe);
Jens Axboe771b53d02019-10-22 10:25:58 -0600475 if (work)
476 __io_worker_busy(wqe, worker, work);
Jens Axboe6206f0e2019-11-26 11:59:32 -0700477 else if (!wq_list_empty(&wqe->work_list))
Jens Axboe771b53d02019-10-22 10:25:58 -0600478 wqe->flags |= IO_WQE_FLAG_STALLED;
479
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200480 raw_spin_unlock_irq(&wqe->lock);
Jens Axboe771b53d02019-10-22 10:25:58 -0600481 if (!work)
482 break;
Pavel Begunkov58e39312020-03-04 16:14:10 +0300483 io_assign_current_work(worker, work);
Jens Axboee9418942021-02-19 12:33:30 -0700484 __set_current_state(TASK_RUNNING);
Jens Axboe36c2f922019-11-13 09:43:34 -0700485
Pavel Begunkovdc026a72020-03-04 16:14:09 +0300486 /* handle a whole dependent link */
487 do {
Pavel Begunkov5280f7e2021-02-04 13:52:08 +0000488 struct io_wq_work *next_hashed, *linked;
Pavel Begunkovb089ed392020-07-25 14:42:00 +0300489 unsigned int hash = io_get_work_hash(work);
Hillf Dantonfd1c4bc2019-12-24 09:14:29 -0700490
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300491 next_hashed = wq_next_work(work);
Pavel Begunkovc60eb042021-04-08 01:54:42 +0100492
493 if (unlikely(do_kill) && (work->flags & IO_WQ_WORK_UNBOUND))
494 work->flags |= IO_WQ_WORK_CANCEL;
Pavel Begunkov5280f7e2021-02-04 13:52:08 +0000495 wq->do_work(work);
496 io_assign_current_work(worker, NULL);
Jens Axboe36c2f922019-11-13 09:43:34 -0700497
Pavel Begunkov5280f7e2021-02-04 13:52:08 +0000498 linked = wq->free_work(work);
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300499 work = next_hashed;
500 if (!work && linked && !io_wq_is_hashed(linked)) {
501 work = linked;
502 linked = NULL;
503 }
504 io_assign_current_work(worker, work);
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300505 if (linked)
506 io_wqe_enqueue(wqe, linked);
507
508 if (hash != -1U && !next_hashed) {
Jens Axboee9418942021-02-19 12:33:30 -0700509 clear_bit(hash, &wq->hash->map);
510 if (wq_has_sleeper(&wq->hash->wait))
511 wake_up(&wq->hash->wait);
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200512 raw_spin_lock_irq(&wqe->lock);
Pavel Begunkovdc026a72020-03-04 16:14:09 +0300513 wqe->flags &= ~IO_WQE_FLAG_STALLED;
Pavel Begunkovf462fd32020-03-04 16:14:11 +0300514 /* skip unnecessary unlock-lock wqe->lock */
515 if (!work)
516 goto get_next;
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200517 raw_spin_unlock_irq(&wqe->lock);
Pavel Begunkovdc026a72020-03-04 16:14:09 +0300518 }
Pavel Begunkov58e39312020-03-04 16:14:10 +0300519 } while (work);
Jens Axboe36c2f922019-11-13 09:43:34 -0700520
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200521 raw_spin_lock_irq(&wqe->lock);
Jens Axboe771b53d02019-10-22 10:25:58 -0600522 } while (1);
523}
524
Jens Axboe771b53d02019-10-22 10:25:58 -0600525static int io_wqe_worker(void *data)
526{
527 struct io_worker *worker = data;
528 struct io_wqe *wqe = worker->wqe;
529 struct io_wq *wq = wqe->wq;
Jens Axboe46fe18b2021-03-04 12:39:36 -0700530 char buf[TASK_COMM_LEN];
Jens Axboe771b53d02019-10-22 10:25:58 -0600531
Jens Axboe46fe18b2021-03-04 12:39:36 -0700532 worker->flags |= (IO_WORKER_F_UP | IO_WORKER_F_RUNNING);
Jens Axboe46fe18b2021-03-04 12:39:36 -0700533
Jens Axboe685fe7f2021-03-08 09:37:51 -0700534 snprintf(buf, sizeof(buf), "iou-wrk-%d", wq->task->pid);
Jens Axboe46fe18b2021-03-04 12:39:36 -0700535 set_task_comm(current, buf);
Jens Axboe771b53d02019-10-22 10:25:58 -0600536
537 while (!test_bit(IO_WQ_BIT_EXIT, &wq->state)) {
Jens Axboe16efa4f2021-03-12 20:26:13 -0700538 long ret;
539
Jens Axboe506d95f2019-12-07 21:03:59 -0700540 set_current_state(TASK_INTERRUPTIBLE);
Jens Axboee995d512019-12-07 21:06:46 -0700541loop:
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200542 raw_spin_lock_irq(&wqe->lock);
Jens Axboe771b53d02019-10-22 10:25:58 -0600543 if (io_wqe_run_queue(wqe)) {
Jens Axboe771b53d02019-10-22 10:25:58 -0600544 io_worker_handle_work(worker);
Jens Axboee995d512019-12-07 21:06:46 -0700545 goto loop;
Jens Axboe771b53d02019-10-22 10:25:58 -0600546 }
Jens Axboec6d77d92021-02-15 13:26:34 -0700547 __io_worker_idle(wqe, worker);
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200548 raw_spin_unlock_irq(&wqe->lock);
Jens Axboe00ddff42021-03-21 07:06:56 -0600549 if (io_flush_signals())
550 continue;
Jens Axboe16efa4f2021-03-12 20:26:13 -0700551 ret = schedule_timeout(WORKER_IDLE_TIMEOUT);
Jens Axboedbe1bdb2021-03-25 18:16:06 -0600552 if (signal_pending(current)) {
553 struct ksignal ksig;
554
555 if (!get_signal(&ksig))
556 continue;
Jens Axboe3bfe6102021-02-16 14:15:30 -0700557 break;
Jens Axboedbe1bdb2021-03-25 18:16:06 -0600558 }
559 if (ret)
560 continue;
Jens Axboe771b53d02019-10-22 10:25:58 -0600561 /* timed out, exit unless we're the fixed worker */
562 if (test_bit(IO_WQ_BIT_EXIT, &wq->state) ||
563 !(worker->flags & IO_WORKER_F_FIXED))
564 break;
565 }
566
Jens Axboe771b53d02019-10-22 10:25:58 -0600567 if (test_bit(IO_WQ_BIT_EXIT, &wq->state)) {
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200568 raw_spin_lock_irq(&wqe->lock);
Jens Axboe6206f0e2019-11-26 11:59:32 -0700569 if (!wq_list_empty(&wqe->work_list))
Jens Axboe771b53d02019-10-22 10:25:58 -0600570 io_worker_handle_work(worker);
571 else
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200572 raw_spin_unlock_irq(&wqe->lock);
Jens Axboe771b53d02019-10-22 10:25:58 -0600573 }
574
575 io_worker_exit(worker);
576 return 0;
577}
578
579/*
Jens Axboe771b53d02019-10-22 10:25:58 -0600580 * Called when a worker is scheduled in. Mark us as currently running.
581 */
582void io_wq_worker_running(struct task_struct *tsk)
583{
Jens Axboe3bfe6102021-02-16 14:15:30 -0700584 struct io_worker *worker = tsk->pf_io_worker;
Jens Axboe771b53d02019-10-22 10:25:58 -0600585
Jens Axboe3bfe6102021-02-16 14:15:30 -0700586 if (!worker)
587 return;
Jens Axboe771b53d02019-10-22 10:25:58 -0600588 if (!(worker->flags & IO_WORKER_F_UP))
589 return;
590 if (worker->flags & IO_WORKER_F_RUNNING)
591 return;
592 worker->flags |= IO_WORKER_F_RUNNING;
Jens Axboe958234d2021-02-17 09:00:57 -0700593 io_wqe_inc_running(worker);
Jens Axboe771b53d02019-10-22 10:25:58 -0600594}
595
596/*
597 * Called when worker is going to sleep. If there are no workers currently
Jens Axboe685fe7f2021-03-08 09:37:51 -0700598 * running and we have work pending, wake up a free one or create a new one.
Jens Axboe771b53d02019-10-22 10:25:58 -0600599 */
600void io_wq_worker_sleeping(struct task_struct *tsk)
601{
Jens Axboe3bfe6102021-02-16 14:15:30 -0700602 struct io_worker *worker = tsk->pf_io_worker;
Jens Axboe771b53d02019-10-22 10:25:58 -0600603
Jens Axboe3bfe6102021-02-16 14:15:30 -0700604 if (!worker)
605 return;
Jens Axboe771b53d02019-10-22 10:25:58 -0600606 if (!(worker->flags & IO_WORKER_F_UP))
607 return;
608 if (!(worker->flags & IO_WORKER_F_RUNNING))
609 return;
610
611 worker->flags &= ~IO_WORKER_F_RUNNING;
612
Jens Axboe3bfe6102021-02-16 14:15:30 -0700613 raw_spin_lock_irq(&worker->wqe->lock);
Jens Axboe958234d2021-02-17 09:00:57 -0700614 io_wqe_dec_running(worker);
Jens Axboe3bfe6102021-02-16 14:15:30 -0700615 raw_spin_unlock_irq(&worker->wqe->lock);
Jens Axboe771b53d02019-10-22 10:25:58 -0600616}
617
Jens Axboe685fe7f2021-03-08 09:37:51 -0700618static void create_io_worker(struct io_wq *wq, struct io_wqe *wqe, int index)
Jens Axboe3bfe6102021-02-16 14:15:30 -0700619{
Jens Axboe46fe18b2021-03-04 12:39:36 -0700620 struct io_wqe_acct *acct = &wqe->acct[index];
Jens Axboe3bfe6102021-02-16 14:15:30 -0700621 struct io_worker *worker;
Jens Axboe46fe18b2021-03-04 12:39:36 -0700622 struct task_struct *tsk;
Jens Axboe3bfe6102021-02-16 14:15:30 -0700623
Jens Axboe8b3e78b2021-02-23 15:34:06 -0700624 __set_current_state(TASK_RUNNING);
625
Jens Axboe3bfe6102021-02-16 14:15:30 -0700626 worker = kzalloc_node(sizeof(*worker), GFP_KERNEL, wqe->node);
627 if (!worker)
Jens Axboe685fe7f2021-03-08 09:37:51 -0700628 goto fail;
Jens Axboe3bfe6102021-02-16 14:15:30 -0700629
630 refcount_set(&worker->ref, 1);
631 worker->nulls_node.pprev = NULL;
632 worker->wqe = wqe;
633 spin_lock_init(&worker->lock);
Jens Axboeeb2de942021-02-23 19:59:06 -0700634 init_completion(&worker->ref_done);
Jens Axboe3bfe6102021-02-16 14:15:30 -0700635
Jens Axboe46fe18b2021-03-04 12:39:36 -0700636 tsk = create_io_thread(io_wqe_worker, worker, wqe->node);
637 if (IS_ERR(tsk)) {
Jens Axboe3bfe6102021-02-16 14:15:30 -0700638 kfree(worker);
Jens Axboe685fe7f2021-03-08 09:37:51 -0700639fail:
640 atomic_dec(&acct->nr_running);
641 io_worker_ref_put(wq);
642 return;
Jens Axboe3bfe6102021-02-16 14:15:30 -0700643 }
Jens Axboe46fe18b2021-03-04 12:39:36 -0700644
645 tsk->pf_io_worker = worker;
646 worker->task = tsk;
647 set_cpus_allowed_ptr(tsk, cpumask_of_node(wqe->node));
Jens Axboee22bc9b2021-03-09 19:49:02 -0700648 tsk->flags |= PF_NO_SETAFFINITY;
Jens Axboe46fe18b2021-03-04 12:39:36 -0700649
650 raw_spin_lock_irq(&wqe->lock);
651 hlist_nulls_add_head_rcu(&worker->nulls_node, &wqe->free_list);
652 list_add_tail_rcu(&worker->all_list, &wqe->all_list);
653 worker->flags |= IO_WORKER_F_FREE;
654 if (index == IO_WQ_ACCT_BOUND)
655 worker->flags |= IO_WORKER_F_BOUND;
656 if (!acct->nr_workers && (worker->flags & IO_WORKER_F_BOUND))
657 worker->flags |= IO_WORKER_F_FIXED;
658 acct->nr_workers++;
659 raw_spin_unlock_irq(&wqe->lock);
660 wake_up_new_task(tsk);
Jens Axboe771b53d02019-10-22 10:25:58 -0600661}
662
Hillf Dantonc4068bf2020-09-26 21:26:55 +0800663/*
664 * Iterate the passed in list and call the specific function for each
665 * worker that isn't exiting
666 */
667static bool io_wq_for_each_worker(struct io_wqe *wqe,
668 bool (*func)(struct io_worker *, void *),
669 void *data)
670{
671 struct io_worker *worker;
672 bool ret = false;
673
674 list_for_each_entry_rcu(worker, &wqe->all_list, all_list) {
675 if (io_worker_get(worker)) {
676 /* no task if node is/was offline */
677 if (worker->task)
678 ret = func(worker, data);
679 io_worker_release(worker);
680 if (ret)
681 break;
682 }
683 }
684
685 return ret;
686}
687
688static bool io_wq_worker_wake(struct io_worker *worker, void *data)
689{
Jens Axboe46fe18b2021-03-04 12:39:36 -0700690 set_notify_signal(worker->task);
Hillf Dantonc4068bf2020-09-26 21:26:55 +0800691 wake_up_process(worker->task);
692 return false;
693}
694
Jens Axboef0127252021-03-03 15:47:04 -0700695static bool io_wq_work_match_all(struct io_wq_work *work, void *data)
696{
697 return true;
698}
699
Pavel Begunkove9fd9392020-03-04 16:14:12 +0300700static void io_run_cancel(struct io_wq_work *work, struct io_wqe *wqe)
Pavel Begunkovfc04c392020-03-01 19:18:19 +0300701{
Pavel Begunkove9fd9392020-03-04 16:14:12 +0300702 struct io_wq *wq = wqe->wq;
703
Pavel Begunkovfc04c392020-03-01 19:18:19 +0300704 do {
Pavel Begunkovfc04c392020-03-01 19:18:19 +0300705 work->flags |= IO_WQ_WORK_CANCEL;
Pavel Begunkov5280f7e2021-02-04 13:52:08 +0000706 wq->do_work(work);
707 work = wq->free_work(work);
Pavel Begunkovfc04c392020-03-01 19:18:19 +0300708 } while (work);
709}
710
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300711static void io_wqe_insert_work(struct io_wqe *wqe, struct io_wq_work *work)
712{
713 unsigned int hash;
714 struct io_wq_work *tail;
715
716 if (!io_wq_is_hashed(work)) {
717append:
718 wq_list_add_tail(&work->list, &wqe->work_list);
719 return;
720 }
721
722 hash = io_get_work_hash(work);
723 tail = wqe->hash_tail[hash];
724 wqe->hash_tail[hash] = work;
725 if (!tail)
726 goto append;
727
728 wq_list_add_after(&work->list, &tail->list, &wqe->work_list);
729}
730
Jens Axboe771b53d02019-10-22 10:25:58 -0600731static void io_wqe_enqueue(struct io_wqe *wqe, struct io_wq_work *work)
732{
Jens Axboec5def4a2019-11-07 11:41:16 -0700733 struct io_wqe_acct *acct = io_work_get_acct(wqe, work);
Jens Axboe895e2ca2019-12-17 08:46:33 -0700734 int work_flags;
Jens Axboe771b53d02019-10-22 10:25:58 -0600735 unsigned long flags;
736
Jens Axboe685fe7f2021-03-08 09:37:51 -0700737 if (test_bit(IO_WQ_BIT_EXIT, &wqe->wq->state)) {
yangerkun70e35122021-03-09 11:04:10 +0800738 io_run_cancel(work, wqe);
Jens Axboe4fb6ac32021-02-25 10:17:09 -0700739 return;
740 }
741
Jens Axboe895e2ca2019-12-17 08:46:33 -0700742 work_flags = work->flags;
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200743 raw_spin_lock_irqsave(&wqe->lock, flags);
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300744 io_wqe_insert_work(wqe, work);
Jens Axboe771b53d02019-10-22 10:25:58 -0600745 wqe->flags &= ~IO_WQE_FLAG_STALLED;
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200746 raw_spin_unlock_irqrestore(&wqe->lock, flags);
Jens Axboe771b53d02019-10-22 10:25:58 -0600747
Jens Axboe895e2ca2019-12-17 08:46:33 -0700748 if ((work_flags & IO_WQ_WORK_CONCURRENT) ||
749 !atomic_read(&acct->nr_running))
Jens Axboec5def4a2019-11-07 11:41:16 -0700750 io_wqe_wake_worker(wqe, acct);
Jens Axboe771b53d02019-10-22 10:25:58 -0600751}
752
753void io_wq_enqueue(struct io_wq *wq, struct io_wq_work *work)
754{
755 struct io_wqe *wqe = wq->wqes[numa_node_id()];
756
757 io_wqe_enqueue(wqe, work);
758}
759
760/*
Pavel Begunkov8766dd52020-03-14 00:31:04 +0300761 * Work items that hash to the same value will not be done in parallel.
762 * Used to limit concurrent writes, generally hashed by inode.
Jens Axboe771b53d02019-10-22 10:25:58 -0600763 */
Pavel Begunkov8766dd52020-03-14 00:31:04 +0300764void io_wq_hash_work(struct io_wq_work *work, void *val)
Jens Axboe771b53d02019-10-22 10:25:58 -0600765{
Pavel Begunkov8766dd52020-03-14 00:31:04 +0300766 unsigned int bit;
Jens Axboe771b53d02019-10-22 10:25:58 -0600767
768 bit = hash_ptr(val, IO_WQ_HASH_ORDER);
769 work->flags |= (IO_WQ_WORK_HASHED | (bit << IO_WQ_HASH_SHIFT));
Jens Axboe771b53d02019-10-22 10:25:58 -0600770}
771
Pavel Begunkov2293b412020-03-07 01:15:39 +0300772static bool io_wq_worker_cancel(struct io_worker *worker, void *data)
Jens Axboe62755e32019-10-28 21:49:21 -0600773{
Pavel Begunkov2293b412020-03-07 01:15:39 +0300774 struct io_cb_cancel_data *match = data;
Jens Axboe6f726532019-11-05 13:51:51 -0700775 unsigned long flags;
Jens Axboe62755e32019-10-28 21:49:21 -0600776
777 /*
778 * Hold the lock to avoid ->cur_work going out of scope, caller
Jens Axboe36c2f922019-11-13 09:43:34 -0700779 * may dereference the passed in work.
Jens Axboe62755e32019-10-28 21:49:21 -0600780 */
Jens Axboe36c2f922019-11-13 09:43:34 -0700781 spin_lock_irqsave(&worker->lock, flags);
Jens Axboe62755e32019-10-28 21:49:21 -0600782 if (worker->cur_work &&
Pavel Begunkov2293b412020-03-07 01:15:39 +0300783 match->fn(worker->cur_work, match->data)) {
Jens Axboe3bfe6102021-02-16 14:15:30 -0700784 set_notify_signal(worker->task);
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300785 match->nr_running++;
Jens Axboe771b53d02019-10-22 10:25:58 -0600786 }
Jens Axboe36c2f922019-11-13 09:43:34 -0700787 spin_unlock_irqrestore(&worker->lock, flags);
Jens Axboe771b53d02019-10-22 10:25:58 -0600788
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300789 return match->nr_running && !match->cancel_all;
Jens Axboe771b53d02019-10-22 10:25:58 -0600790}
791
Pavel Begunkov204361a2020-08-23 20:33:10 +0300792static inline void io_wqe_remove_pending(struct io_wqe *wqe,
793 struct io_wq_work *work,
794 struct io_wq_work_node *prev)
795{
796 unsigned int hash = io_get_work_hash(work);
797 struct io_wq_work *prev_work = NULL;
798
799 if (io_wq_is_hashed(work) && work == wqe->hash_tail[hash]) {
800 if (prev)
801 prev_work = container_of(prev, struct io_wq_work, list);
802 if (prev_work && io_get_work_hash(prev_work) == hash)
803 wqe->hash_tail[hash] = prev_work;
804 else
805 wqe->hash_tail[hash] = NULL;
806 }
807 wq_list_del(&wqe->work_list, &work->list, prev);
808}
809
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300810static void io_wqe_cancel_pending_work(struct io_wqe *wqe,
Pavel Begunkovf4c26652020-06-15 10:24:02 +0300811 struct io_cb_cancel_data *match)
Jens Axboe771b53d02019-10-22 10:25:58 -0600812{
Jens Axboe6206f0e2019-11-26 11:59:32 -0700813 struct io_wq_work_node *node, *prev;
Jens Axboe771b53d02019-10-22 10:25:58 -0600814 struct io_wq_work *work;
Jens Axboe6f726532019-11-05 13:51:51 -0700815 unsigned long flags;
Jens Axboe771b53d02019-10-22 10:25:58 -0600816
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300817retry:
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200818 raw_spin_lock_irqsave(&wqe->lock, flags);
Jens Axboe6206f0e2019-11-26 11:59:32 -0700819 wq_list_for_each(node, prev, &wqe->work_list) {
820 work = container_of(node, struct io_wq_work, list);
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300821 if (!match->fn(work, match->data))
822 continue;
Pavel Begunkov204361a2020-08-23 20:33:10 +0300823 io_wqe_remove_pending(wqe, work, prev);
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200824 raw_spin_unlock_irqrestore(&wqe->lock, flags);
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300825 io_run_cancel(work, wqe);
826 match->nr_pending++;
827 if (!match->cancel_all)
828 return;
829
830 /* not safe to continue after unlock */
831 goto retry;
Jens Axboe771b53d02019-10-22 10:25:58 -0600832 }
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200833 raw_spin_unlock_irqrestore(&wqe->lock, flags);
Pavel Begunkovf4c26652020-06-15 10:24:02 +0300834}
Jens Axboe771b53d02019-10-22 10:25:58 -0600835
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300836static void io_wqe_cancel_running_work(struct io_wqe *wqe,
Pavel Begunkovf4c26652020-06-15 10:24:02 +0300837 struct io_cb_cancel_data *match)
838{
Jens Axboe771b53d02019-10-22 10:25:58 -0600839 rcu_read_lock();
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300840 io_wq_for_each_worker(wqe, io_wq_worker_cancel, match);
Jens Axboe771b53d02019-10-22 10:25:58 -0600841 rcu_read_unlock();
Jens Axboe771b53d02019-10-22 10:25:58 -0600842}
843
Pavel Begunkov2293b412020-03-07 01:15:39 +0300844enum io_wq_cancel io_wq_cancel_cb(struct io_wq *wq, work_cancel_fn *cancel,
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300845 void *data, bool cancel_all)
Pavel Begunkov2293b412020-03-07 01:15:39 +0300846{
847 struct io_cb_cancel_data match = {
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300848 .fn = cancel,
849 .data = data,
850 .cancel_all = cancel_all,
Pavel Begunkov2293b412020-03-07 01:15:39 +0300851 };
Pavel Begunkov2293b412020-03-07 01:15:39 +0300852 int node;
853
Pavel Begunkovf4c26652020-06-15 10:24:02 +0300854 /*
855 * First check pending list, if we're lucky we can just remove it
856 * from there. CANCEL_OK means that the work is returned as-new,
857 * no completion will be posted for it.
858 */
Pavel Begunkov2293b412020-03-07 01:15:39 +0300859 for_each_node(node) {
860 struct io_wqe *wqe = wq->wqes[node];
861
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300862 io_wqe_cancel_pending_work(wqe, &match);
863 if (match.nr_pending && !match.cancel_all)
Pavel Begunkovf4c26652020-06-15 10:24:02 +0300864 return IO_WQ_CANCEL_OK;
Pavel Begunkov2293b412020-03-07 01:15:39 +0300865 }
866
Pavel Begunkovf4c26652020-06-15 10:24:02 +0300867 /*
868 * Now check if a free (going busy) or busy worker has the work
869 * currently running. If we find it there, we'll return CANCEL_RUNNING
870 * as an indication that we attempt to signal cancellation. The
871 * completion will run normally in this case.
872 */
873 for_each_node(node) {
874 struct io_wqe *wqe = wq->wqes[node];
875
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300876 io_wqe_cancel_running_work(wqe, &match);
877 if (match.nr_running && !match.cancel_all)
Pavel Begunkovf4c26652020-06-15 10:24:02 +0300878 return IO_WQ_CANCEL_RUNNING;
879 }
880
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300881 if (match.nr_running)
882 return IO_WQ_CANCEL_RUNNING;
883 if (match.nr_pending)
884 return IO_WQ_CANCEL_OK;
Pavel Begunkovf4c26652020-06-15 10:24:02 +0300885 return IO_WQ_CANCEL_NOTFOUND;
Pavel Begunkov2293b412020-03-07 01:15:39 +0300886}
887
Jens Axboee9418942021-02-19 12:33:30 -0700888static int io_wqe_hash_wake(struct wait_queue_entry *wait, unsigned mode,
889 int sync, void *key)
890{
891 struct io_wqe *wqe = container_of(wait, struct io_wqe, wait);
Jens Axboee9418942021-02-19 12:33:30 -0700892
893 list_del_init(&wait->entry);
894
895 rcu_read_lock();
Jens Axboe685fe7f2021-03-08 09:37:51 -0700896 io_wqe_activate_free_worker(wqe);
Jens Axboee9418942021-02-19 12:33:30 -0700897 rcu_read_unlock();
Jens Axboee9418942021-02-19 12:33:30 -0700898 return 1;
899}
900
Jens Axboe576a3472019-11-25 08:49:20 -0700901struct io_wq *io_wq_create(unsigned bounded, struct io_wq_data *data)
Jens Axboe771b53d02019-10-22 10:25:58 -0600902{
Jann Horn3fc50ab2019-11-26 19:10:20 +0100903 int ret = -ENOMEM, node;
Jens Axboe771b53d02019-10-22 10:25:58 -0600904 struct io_wq *wq;
905
Pavel Begunkovf5fa38c2020-06-08 21:08:20 +0300906 if (WARN_ON_ONCE(!data->free_work || !data->do_work))
Pavel Begunkove9fd9392020-03-04 16:14:12 +0300907 return ERR_PTR(-EINVAL);
908
Pavel Begunkovc7f405d2021-06-14 02:36:12 +0100909 wq = kzalloc(struct_size(wq, wqes, nr_node_ids), GFP_KERNEL);
Jens Axboe771b53d02019-10-22 10:25:58 -0600910 if (!wq)
911 return ERR_PTR(-ENOMEM);
Jens Axboe43c01fb2020-10-22 09:02:50 -0600912 ret = cpuhp_state_add_instance_nocalls(io_wq_online, &wq->cpuhp_node);
913 if (ret)
Pavel Begunkovc7f405d2021-06-14 02:36:12 +0100914 goto err_wq;
Jens Axboe771b53d02019-10-22 10:25:58 -0600915
Jens Axboee9418942021-02-19 12:33:30 -0700916 refcount_inc(&data->hash->refs);
917 wq->hash = data->hash;
Pavel Begunkove9fd9392020-03-04 16:14:12 +0300918 wq->free_work = data->free_work;
Pavel Begunkovf5fa38c2020-06-08 21:08:20 +0300919 wq->do_work = data->do_work;
Jens Axboe7d723062019-11-12 22:31:31 -0700920
Jens Axboe43c01fb2020-10-22 09:02:50 -0600921 ret = -ENOMEM;
Jann Horn3fc50ab2019-11-26 19:10:20 +0100922 for_each_node(node) {
Jens Axboe771b53d02019-10-22 10:25:58 -0600923 struct io_wqe *wqe;
Jens Axboe75634392020-02-11 06:30:06 -0700924 int alloc_node = node;
Jens Axboe771b53d02019-10-22 10:25:58 -0600925
Jens Axboe75634392020-02-11 06:30:06 -0700926 if (!node_online(alloc_node))
927 alloc_node = NUMA_NO_NODE;
928 wqe = kzalloc_node(sizeof(struct io_wqe), GFP_KERNEL, alloc_node);
Jens Axboe771b53d02019-10-22 10:25:58 -0600929 if (!wqe)
Jann Horn3fc50ab2019-11-26 19:10:20 +0100930 goto err;
931 wq->wqes[node] = wqe;
Jens Axboe75634392020-02-11 06:30:06 -0700932 wqe->node = alloc_node;
Jens Axboe685fe7f2021-03-08 09:37:51 -0700933 wqe->acct[IO_WQ_ACCT_BOUND].index = IO_WQ_ACCT_BOUND;
934 wqe->acct[IO_WQ_ACCT_UNBOUND].index = IO_WQ_ACCT_UNBOUND;
Jens Axboec5def4a2019-11-07 11:41:16 -0700935 wqe->acct[IO_WQ_ACCT_BOUND].max_workers = bounded;
936 atomic_set(&wqe->acct[IO_WQ_ACCT_BOUND].nr_running, 0);
Jens Axboe728f13e2021-02-21 16:02:53 -0700937 wqe->acct[IO_WQ_ACCT_UNBOUND].max_workers =
Jens Axboec5def4a2019-11-07 11:41:16 -0700938 task_rlimit(current, RLIMIT_NPROC);
Jens Axboec5def4a2019-11-07 11:41:16 -0700939 atomic_set(&wqe->acct[IO_WQ_ACCT_UNBOUND].nr_running, 0);
Jens Axboee9418942021-02-19 12:33:30 -0700940 wqe->wait.func = io_wqe_hash_wake;
941 INIT_LIST_HEAD(&wqe->wait.entry);
Jens Axboe771b53d02019-10-22 10:25:58 -0600942 wqe->wq = wq;
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200943 raw_spin_lock_init(&wqe->lock);
Jens Axboe6206f0e2019-11-26 11:59:32 -0700944 INIT_WQ_LIST(&wqe->work_list);
Jens Axboe021d1cd2019-11-14 08:00:41 -0700945 INIT_HLIST_NULLS_HEAD(&wqe->free_list, 0);
Jens Axboee61df662019-11-13 13:54:49 -0700946 INIT_LIST_HEAD(&wqe->all_list);
Jens Axboe771b53d02019-10-22 10:25:58 -0600947 }
948
Jens Axboe685fe7f2021-03-08 09:37:51 -0700949 wq->task = get_task_struct(data->task);
Jens Axboe685fe7f2021-03-08 09:37:51 -0700950 atomic_set(&wq->worker_refs, 1);
951 init_completion(&wq->worker_done);
952 return wq;
Jens Axboeb60fda62019-11-19 08:37:07 -0700953err:
Jens Axboedc7bbc92021-03-01 09:09:56 -0700954 io_wq_put_hash(data->hash);
Jens Axboe43c01fb2020-10-22 09:02:50 -0600955 cpuhp_state_remove_instance_nocalls(io_wq_online, &wq->cpuhp_node);
Jann Horn3fc50ab2019-11-26 19:10:20 +0100956 for_each_node(node)
957 kfree(wq->wqes[node]);
Jens Axboe43c01fb2020-10-22 09:02:50 -0600958err_wq:
Jens Axboeb60fda62019-11-19 08:37:07 -0700959 kfree(wq);
Jens Axboe771b53d02019-10-22 10:25:58 -0600960 return ERR_PTR(ret);
961}
962
Jens Axboec80ca472021-04-01 19:57:07 -0600963static bool io_task_work_match(struct callback_head *cb, void *data)
964{
965 struct create_worker_data *cwd;
966
967 if (cb->func != create_worker_cb)
968 return false;
969 cwd = container_of(cb, struct create_worker_data, work);
970 return cwd->wqe->wq == data;
971}
972
Pavel Begunkov17a91052021-05-23 15:48:39 +0100973void io_wq_exit_start(struct io_wq *wq)
974{
975 set_bit(IO_WQ_BIT_EXIT, &wq->state);
976}
977
Jens Axboe685fe7f2021-03-08 09:37:51 -0700978static void io_wq_exit_workers(struct io_wq *wq)
Jens Axboeafcc4012021-02-26 13:48:19 -0700979{
Jens Axboe685fe7f2021-03-08 09:37:51 -0700980 struct callback_head *cb;
981 int node;
982
Jens Axboe685fe7f2021-03-08 09:37:51 -0700983 if (!wq->task)
984 return;
985
Jens Axboec80ca472021-04-01 19:57:07 -0600986 while ((cb = task_work_cancel_match(wq->task, io_task_work_match, wq)) != NULL) {
Jens Axboe685fe7f2021-03-08 09:37:51 -0700987 struct create_worker_data *cwd;
988
989 cwd = container_of(cb, struct create_worker_data, work);
990 atomic_dec(&cwd->wqe->acct[cwd->index].nr_running);
991 io_worker_ref_put(wq);
992 kfree(cwd);
Jens Axboeafcc4012021-02-26 13:48:19 -0700993 }
Jens Axboe685fe7f2021-03-08 09:37:51 -0700994
995 rcu_read_lock();
996 for_each_node(node) {
997 struct io_wqe *wqe = wq->wqes[node];
998
999 io_wq_for_each_worker(wqe, io_wq_worker_wake, NULL);
Jens Axboe685fe7f2021-03-08 09:37:51 -07001000 }
1001 rcu_read_unlock();
1002 io_worker_ref_put(wq);
1003 wait_for_completion(&wq->worker_done);
Zqiang3743c172021-05-26 13:08:26 +08001004
1005 for_each_node(node) {
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 }
Jens Axboe685fe7f2021-03-08 09:37:51 -07001010 put_task_struct(wq->task);
1011 wq->task = NULL;
Jens Axboeafcc4012021-02-26 13:48:19 -07001012}
1013
Jens Axboe4fb6ac32021-02-25 10:17:09 -07001014static void io_wq_destroy(struct io_wq *wq)
Jens Axboe771b53d02019-10-22 10:25:58 -06001015{
Jann Horn3fc50ab2019-11-26 19:10:20 +01001016 int node;
Jens Axboe771b53d02019-10-22 10:25:58 -06001017
Jens Axboe43c01fb2020-10-22 09:02:50 -06001018 cpuhp_state_remove_instance_nocalls(io_wq_online, &wq->cpuhp_node);
1019
Jens Axboee9418942021-02-19 12:33:30 -07001020 for_each_node(node) {
1021 struct io_wqe *wqe = wq->wqes[node];
Jens Axboef5d2d232021-03-25 10:16:12 -06001022 struct io_cb_cancel_data match = {
1023 .fn = io_wq_work_match_all,
1024 .cancel_all = true,
1025 };
1026 io_wqe_cancel_pending_work(wqe, &match);
Jens Axboee9418942021-02-19 12:33:30 -07001027 kfree(wqe);
1028 }
Jens Axboee9418942021-02-19 12:33:30 -07001029 io_wq_put_hash(wq->hash);
Jens Axboe771b53d02019-10-22 10:25:58 -06001030 kfree(wq);
Jens Axboe4fb6ac32021-02-25 10:17:09 -07001031}
1032
Jens Axboeafcc4012021-02-26 13:48:19 -07001033void io_wq_put_and_exit(struct io_wq *wq)
1034{
Pavel Begunkov17a91052021-05-23 15:48:39 +01001035 WARN_ON_ONCE(!test_bit(IO_WQ_BIT_EXIT, &wq->state));
1036
Jens Axboe685fe7f2021-03-08 09:37:51 -07001037 io_wq_exit_workers(wq);
Pavel Begunkov382cb032021-06-14 02:36:13 +01001038 io_wq_destroy(wq);
Jens Axboeafcc4012021-02-26 13:48:19 -07001039}
1040
Jens Axboe43c01fb2020-10-22 09:02:50 -06001041static bool io_wq_worker_affinity(struct io_worker *worker, void *data)
1042{
Peter Zijlstrae0051d72021-04-08 11:44:50 +02001043 set_cpus_allowed_ptr(worker->task, cpumask_of_node(worker->wqe->node));
Jens Axboe43c01fb2020-10-22 09:02:50 -06001044
Jens Axboe43c01fb2020-10-22 09:02:50 -06001045 return false;
1046}
1047
1048static int io_wq_cpu_online(unsigned int cpu, struct hlist_node *node)
1049{
1050 struct io_wq *wq = hlist_entry_safe(node, struct io_wq, cpuhp_node);
1051 int i;
1052
1053 rcu_read_lock();
1054 for_each_node(i)
1055 io_wq_for_each_worker(wq->wqes[i], io_wq_worker_affinity, NULL);
1056 rcu_read_unlock();
1057 return 0;
1058}
1059
1060static __init int io_wq_init(void)
1061{
1062 int ret;
1063
1064 ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "io-wq/online",
1065 io_wq_cpu_online, NULL);
1066 if (ret < 0)
1067 return ret;
1068 io_wq_online = ret;
1069 return 0;
1070}
1071subsys_initcall(io_wq_init);