blob: 3dc10bfd8c3baa4a7130ab9caf6cdbf90cf5bd84 [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 Axboee4b4a132021-03-01 18:36:25 -070019#include <linux/freezer.h>
Jens Axboe771b53d02019-10-22 10:25:58 -060020
Jens Axboe43c01fb2020-10-22 09:02:50 -060021#include "../kernel/sched/sched.h"
Jens Axboe771b53d02019-10-22 10:25:58 -060022#include "io-wq.h"
23
24#define WORKER_IDLE_TIMEOUT (5 * HZ)
25
26enum {
27 IO_WORKER_F_UP = 1, /* up and active */
28 IO_WORKER_F_RUNNING = 2, /* account as running */
29 IO_WORKER_F_FREE = 4, /* worker on free list */
Jens Axboe145cc8c2020-09-26 12:37:46 -060030 IO_WORKER_F_FIXED = 8, /* static idle worker */
31 IO_WORKER_F_BOUND = 16, /* is doing bounded work */
Jens Axboe771b53d02019-10-22 10:25:58 -060032};
33
34enum {
35 IO_WQ_BIT_EXIT = 0, /* wq exiting */
Jens Axboe771b53d02019-10-22 10:25:58 -060036};
37
38enum {
39 IO_WQE_FLAG_STALLED = 1, /* stalled on hash */
40};
41
42/*
43 * One for each thread in a wqe pool
44 */
45struct io_worker {
46 refcount_t ref;
47 unsigned flags;
48 struct hlist_nulls_node nulls_node;
Jens Axboee61df662019-11-13 13:54:49 -070049 struct list_head all_list;
Jens Axboe771b53d02019-10-22 10:25:58 -060050 struct task_struct *task;
Jens Axboe771b53d02019-10-22 10:25:58 -060051 struct io_wqe *wqe;
Jens Axboe36c2f922019-11-13 09:43:34 -070052
Jens Axboe771b53d02019-10-22 10:25:58 -060053 struct io_wq_work *cur_work;
Jens Axboe36c2f922019-11-13 09:43:34 -070054 spinlock_t lock;
Jens Axboe771b53d02019-10-22 10:25:58 -060055
Jens Axboeeb2de942021-02-23 19:59:06 -070056 struct completion ref_done;
57
Jens Axboe771b53d02019-10-22 10:25:58 -060058 struct rcu_head rcu;
Jens Axboe771b53d02019-10-22 10:25:58 -060059};
60
Jens Axboe771b53d02019-10-22 10:25:58 -060061#if BITS_PER_LONG == 64
62#define IO_WQ_HASH_ORDER 6
63#else
64#define IO_WQ_HASH_ORDER 5
65#endif
66
Pavel Begunkov86f3cd12020-03-23 22:57:22 +030067#define IO_WQ_NR_HASH_BUCKETS (1u << IO_WQ_HASH_ORDER)
68
Jens Axboec5def4a2019-11-07 11:41:16 -070069struct io_wqe_acct {
70 unsigned nr_workers;
71 unsigned max_workers;
72 atomic_t nr_running;
73};
74
75enum {
76 IO_WQ_ACCT_BOUND,
77 IO_WQ_ACCT_UNBOUND,
78};
79
Jens Axboe771b53d02019-10-22 10:25:58 -060080/*
81 * Per-node worker thread pool
82 */
83struct io_wqe {
84 struct {
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +020085 raw_spinlock_t lock;
Jens Axboe6206f0e2019-11-26 11:59:32 -070086 struct io_wq_work_list work_list;
Jens Axboe771b53d02019-10-22 10:25:58 -060087 unsigned flags;
88 } ____cacheline_aligned_in_smp;
89
90 int node;
Jens Axboec5def4a2019-11-07 11:41:16 -070091 struct io_wqe_acct acct[2];
Jens Axboe771b53d02019-10-22 10:25:58 -060092
Jens Axboe021d1cd2019-11-14 08:00:41 -070093 struct hlist_nulls_head free_list;
Jens Axboee61df662019-11-13 13:54:49 -070094 struct list_head all_list;
Jens Axboe771b53d02019-10-22 10:25:58 -060095
Jens Axboee9418942021-02-19 12:33:30 -070096 struct wait_queue_entry wait;
97
Jens Axboe771b53d02019-10-22 10:25:58 -060098 struct io_wq *wq;
Pavel Begunkov86f3cd12020-03-23 22:57:22 +030099 struct io_wq_work *hash_tail[IO_WQ_NR_HASH_BUCKETS];
Jens Axboe771b53d02019-10-22 10:25:58 -0600100};
101
102/*
103 * Per io_wq state
104 */
105struct io_wq {
106 struct io_wqe **wqes;
107 unsigned long state;
Jens Axboe771b53d02019-10-22 10:25:58 -0600108
Pavel Begunkove9fd9392020-03-04 16:14:12 +0300109 free_work_fn *free_work;
Pavel Begunkovf5fa38c2020-06-08 21:08:20 +0300110 io_wq_work_fn *do_work;
Jens Axboe7d723062019-11-12 22:31:31 -0700111
Jens Axboe771b53d02019-10-22 10:25:58 -0600112 struct task_struct *manager;
Jens Axboee9418942021-02-19 12:33:30 -0700113
114 struct io_wq_hash *hash;
115
Jens Axboe771b53d02019-10-22 10:25:58 -0600116 refcount_t refs;
Jens Axboed364d9e2021-02-26 09:56:32 -0700117 struct completion exited;
Jens Axboe848f7e12020-01-23 15:33:32 -0700118
Jens Axboefb3a1f62021-02-26 09:47:20 -0700119 atomic_t worker_refs;
120 struct completion worker_done;
121
Jens Axboe43c01fb2020-10-22 09:02:50 -0600122 struct hlist_node cpuhp_node;
Jens Axboe3bfe6102021-02-16 14:15:30 -0700123
124 pid_t task_pid;
Jens Axboe771b53d02019-10-22 10:25:58 -0600125};
126
Jens Axboe43c01fb2020-10-22 09:02:50 -0600127static enum cpuhp_state io_wq_online;
128
Jens Axboef0127252021-03-03 15:47:04 -0700129struct io_cb_cancel_data {
130 work_cancel_fn *fn;
131 void *data;
132 int nr_running;
133 int nr_pending;
134 bool cancel_all;
135};
136
137static void io_wqe_cancel_pending_work(struct io_wqe *wqe,
138 struct io_cb_cancel_data *match);
139
Jens Axboe771b53d02019-10-22 10:25:58 -0600140static bool io_worker_get(struct io_worker *worker)
141{
142 return refcount_inc_not_zero(&worker->ref);
143}
144
145static void io_worker_release(struct io_worker *worker)
146{
147 if (refcount_dec_and_test(&worker->ref))
Jens Axboeeb2de942021-02-23 19:59:06 -0700148 complete(&worker->ref_done);
Jens Axboe771b53d02019-10-22 10:25:58 -0600149}
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{
154 if (work->flags & IO_WQ_WORK_UNBOUND)
155 return &wqe->acct[IO_WQ_ACCT_UNBOUND];
156
157 return &wqe->acct[IO_WQ_ACCT_BOUND];
158}
159
Jens Axboe958234d2021-02-17 09:00:57 -0700160static inline struct io_wqe_acct *io_wqe_get_acct(struct io_worker *worker)
Jens Axboec5def4a2019-11-07 11:41:16 -0700161{
Jens Axboe958234d2021-02-17 09:00:57 -0700162 struct io_wqe *wqe = worker->wqe;
163
Jens Axboec5def4a2019-11-07 11:41:16 -0700164 if (worker->flags & IO_WORKER_F_BOUND)
165 return &wqe->acct[IO_WQ_ACCT_BOUND];
166
167 return &wqe->acct[IO_WQ_ACCT_UNBOUND];
168}
169
Jens Axboe771b53d02019-10-22 10:25:58 -0600170static void io_worker_exit(struct io_worker *worker)
171{
172 struct io_wqe *wqe = worker->wqe;
Jens Axboe958234d2021-02-17 09:00:57 -0700173 struct io_wqe_acct *acct = io_wqe_get_acct(worker);
Jens Axboebf1daa42021-02-16 18:00:55 -0700174 unsigned flags;
Jens Axboe771b53d02019-10-22 10:25:58 -0600175
Jens Axboeeb2de942021-02-23 19:59:06 -0700176 if (refcount_dec_and_test(&worker->ref))
177 complete(&worker->ref_done);
178 wait_for_completion(&worker->ref_done);
Jens Axboe771b53d02019-10-22 10:25:58 -0600179
180 preempt_disable();
181 current->flags &= ~PF_IO_WORKER;
Jens Axboebf1daa42021-02-16 18:00:55 -0700182 flags = worker->flags;
183 worker->flags = 0;
184 if (flags & IO_WORKER_F_RUNNING)
Jens Axboec5def4a2019-11-07 11:41:16 -0700185 atomic_dec(&acct->nr_running);
Jens Axboe771b53d02019-10-22 10:25:58 -0600186 worker->flags = 0;
187 preempt_enable();
188
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200189 raw_spin_lock_irq(&wqe->lock);
Jens Axboebf1daa42021-02-16 18:00:55 -0700190 if (flags & IO_WORKER_F_FREE)
191 hlist_nulls_del_rcu(&worker->nulls_node);
Jens Axboee61df662019-11-13 13:54:49 -0700192 list_del_rcu(&worker->all_list);
Jens Axboec5def4a2019-11-07 11:41:16 -0700193 acct->nr_workers--;
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200194 raw_spin_unlock_irq(&wqe->lock);
Jens Axboe771b53d02019-10-22 10:25:58 -0600195
YueHaibing364b05f2019-11-02 15:55:01 +0800196 kfree_rcu(worker, rcu);
Jens Axboefb3a1f62021-02-26 09:47:20 -0700197 if (atomic_dec_and_test(&wqe->wq->worker_refs))
198 complete(&wqe->wq->worker_done);
Jens Axboe46fe18b2021-03-04 12:39:36 -0700199 do_exit(0);
Jens Axboe771b53d02019-10-22 10:25:58 -0600200}
201
Jens Axboec5def4a2019-11-07 11:41:16 -0700202static inline bool io_wqe_run_queue(struct io_wqe *wqe)
203 __must_hold(wqe->lock)
204{
Jens Axboe6206f0e2019-11-26 11:59:32 -0700205 if (!wq_list_empty(&wqe->work_list) &&
206 !(wqe->flags & IO_WQE_FLAG_STALLED))
Jens Axboec5def4a2019-11-07 11:41:16 -0700207 return true;
208 return false;
209}
210
211/*
212 * Check head of free list for an available worker. If one isn't available,
213 * caller must wake up the wq manager to create one.
214 */
215static bool io_wqe_activate_free_worker(struct io_wqe *wqe)
216 __must_hold(RCU)
217{
218 struct hlist_nulls_node *n;
219 struct io_worker *worker;
220
Jens Axboe021d1cd2019-11-14 08:00:41 -0700221 n = rcu_dereference(hlist_nulls_first_rcu(&wqe->free_list));
Jens Axboec5def4a2019-11-07 11:41:16 -0700222 if (is_a_nulls(n))
223 return false;
224
225 worker = hlist_nulls_entry(n, struct io_worker, nulls_node);
226 if (io_worker_get(worker)) {
Jens Axboe506d95f2019-12-07 21:03:59 -0700227 wake_up_process(worker->task);
Jens Axboec5def4a2019-11-07 11:41:16 -0700228 io_worker_release(worker);
229 return true;
230 }
231
232 return false;
233}
234
235/*
236 * We need a worker. If we find a free one, we're good. If not, and we're
237 * below the max number of workers, wake up the manager to create one.
238 */
239static void io_wqe_wake_worker(struct io_wqe *wqe, struct io_wqe_acct *acct)
240{
241 bool ret;
242
243 /*
244 * Most likely an attempt to queue unbounded work on an io_wq that
245 * wasn't setup with any unbounded workers.
246 */
247 WARN_ON_ONCE(!acct->max_workers);
248
249 rcu_read_lock();
250 ret = io_wqe_activate_free_worker(wqe);
251 rcu_read_unlock();
252
253 if (!ret && acct->nr_workers < acct->max_workers)
254 wake_up_process(wqe->wq->manager);
255}
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 Axboe958234d2021-02-17 09:00:57 -0700264static void io_wqe_dec_running(struct io_worker *worker)
Jens Axboec5def4a2019-11-07 11:41:16 -0700265 __must_hold(wqe->lock)
266{
Jens Axboe958234d2021-02-17 09:00:57 -0700267 struct io_wqe_acct *acct = io_wqe_get_acct(worker);
268 struct io_wqe *wqe = worker->wqe;
Jens Axboec5def4a2019-11-07 11:41:16 -0700269
270 if (atomic_dec_and_test(&acct->nr_running) && io_wqe_run_queue(wqe))
271 io_wqe_wake_worker(wqe, acct);
272}
273
Jens Axboe771b53d02019-10-22 10:25:58 -0600274/*
275 * Worker will start processing some work. Move it to the busy list, if
276 * it's currently on the freelist
277 */
278static void __io_worker_busy(struct io_wqe *wqe, struct io_worker *worker,
279 struct io_wq_work *work)
280 __must_hold(wqe->lock)
281{
Jens Axboec5def4a2019-11-07 11:41:16 -0700282 bool worker_bound, work_bound;
283
Jens Axboe771b53d02019-10-22 10:25:58 -0600284 if (worker->flags & IO_WORKER_F_FREE) {
285 worker->flags &= ~IO_WORKER_F_FREE;
286 hlist_nulls_del_init_rcu(&worker->nulls_node);
Jens Axboe771b53d02019-10-22 10:25:58 -0600287 }
Jens Axboec5def4a2019-11-07 11:41:16 -0700288
289 /*
290 * If worker is moving from bound to unbound (or vice versa), then
291 * ensure we update the running accounting.
292 */
Dan Carpenterb2e9c7d62019-11-19 09:22:16 +0300293 worker_bound = (worker->flags & IO_WORKER_F_BOUND) != 0;
294 work_bound = (work->flags & IO_WQ_WORK_UNBOUND) == 0;
295 if (worker_bound != work_bound) {
Jens Axboe958234d2021-02-17 09:00:57 -0700296 io_wqe_dec_running(worker);
Jens Axboec5def4a2019-11-07 11:41:16 -0700297 if (work_bound) {
298 worker->flags |= IO_WORKER_F_BOUND;
299 wqe->acct[IO_WQ_ACCT_UNBOUND].nr_workers--;
300 wqe->acct[IO_WQ_ACCT_BOUND].nr_workers++;
Jens Axboec5def4a2019-11-07 11:41:16 -0700301 } else {
302 worker->flags &= ~IO_WORKER_F_BOUND;
303 wqe->acct[IO_WQ_ACCT_UNBOUND].nr_workers++;
304 wqe->acct[IO_WQ_ACCT_BOUND].nr_workers--;
Jens Axboec5def4a2019-11-07 11:41:16 -0700305 }
Jens Axboe958234d2021-02-17 09:00:57 -0700306 io_wqe_inc_running(worker);
Jens Axboec5def4a2019-11-07 11:41:16 -0700307 }
Jens Axboe771b53d02019-10-22 10:25:58 -0600308}
309
310/*
311 * No work, worker going to sleep. Move to freelist, and unuse mm if we
312 * have one attached. Dropping the mm may potentially sleep, so we drop
313 * the lock in that case and return success. Since the caller has to
314 * retry the loop in that case (we changed task state), we don't regrab
315 * the lock if we return success.
316 */
Jens Axboec6d77d92021-02-15 13:26:34 -0700317static void __io_worker_idle(struct io_wqe *wqe, struct io_worker *worker)
Jens Axboe771b53d02019-10-22 10:25:58 -0600318 __must_hold(wqe->lock)
319{
320 if (!(worker->flags & IO_WORKER_F_FREE)) {
321 worker->flags |= IO_WORKER_F_FREE;
Jens Axboe021d1cd2019-11-14 08:00:41 -0700322 hlist_nulls_add_head_rcu(&worker->nulls_node, &wqe->free_list);
Jens Axboe771b53d02019-10-22 10:25:58 -0600323 }
Jens Axboe771b53d02019-10-22 10:25:58 -0600324}
325
Pavel Begunkov60cf46a2020-03-14 00:31:05 +0300326static inline unsigned int io_get_work_hash(struct io_wq_work *work)
327{
328 return work->flags >> IO_WQ_HASH_SHIFT;
329}
330
Jens Axboee9418942021-02-19 12:33:30 -0700331static void io_wait_on_hash(struct io_wqe *wqe, unsigned int hash)
332{
333 struct io_wq *wq = wqe->wq;
334
335 spin_lock(&wq->hash->wait.lock);
336 if (list_empty(&wqe->wait.entry)) {
337 __add_wait_queue(&wq->hash->wait, &wqe->wait);
338 if (!test_bit(hash, &wq->hash->map)) {
339 __set_current_state(TASK_RUNNING);
340 list_del_init(&wqe->wait.entry);
341 }
342 }
343 spin_unlock(&wq->hash->wait.lock);
344}
345
Pavel Begunkov60cf46a2020-03-14 00:31:05 +0300346static struct io_wq_work *io_get_next_work(struct io_wqe *wqe)
Jens Axboe771b53d02019-10-22 10:25:58 -0600347 __must_hold(wqe->lock)
348{
Jens Axboe6206f0e2019-11-26 11:59:32 -0700349 struct io_wq_work_node *node, *prev;
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300350 struct io_wq_work *work, *tail;
Jens Axboee9418942021-02-19 12:33:30 -0700351 unsigned int stall_hash = -1U;
Jens Axboe771b53d02019-10-22 10:25:58 -0600352
Jens Axboe6206f0e2019-11-26 11:59:32 -0700353 wq_list_for_each(node, prev, &wqe->work_list) {
Jens Axboee9418942021-02-19 12:33:30 -0700354 unsigned int hash;
355
Jens Axboe6206f0e2019-11-26 11:59:32 -0700356 work = container_of(node, struct io_wq_work, list);
357
Jens Axboe771b53d02019-10-22 10:25:58 -0600358 /* not hashed, can run anytime */
Pavel Begunkov8766dd52020-03-14 00:31:04 +0300359 if (!io_wq_is_hashed(work)) {
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300360 wq_list_del(&wqe->work_list, node, prev);
Jens Axboe771b53d02019-10-22 10:25:58 -0600361 return work;
362 }
363
Pavel Begunkov60cf46a2020-03-14 00:31:05 +0300364 hash = io_get_work_hash(work);
Jens Axboee9418942021-02-19 12:33:30 -0700365 /* all items with this hash lie in [work, tail] */
366 tail = wqe->hash_tail[hash];
367
368 /* hashed, can run if not already running */
369 if (!test_and_set_bit(hash, &wqe->wq->hash->map)) {
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300370 wqe->hash_tail[hash] = NULL;
371 wq_list_cut(&wqe->work_list, &tail->list, prev);
Jens Axboe771b53d02019-10-22 10:25:58 -0600372 return work;
373 }
Jens Axboee9418942021-02-19 12:33:30 -0700374 if (stall_hash == -1U)
375 stall_hash = hash;
376 /* fast forward to a next hash, for-each will fix up @prev */
377 node = &tail->list;
378 }
379
380 if (stall_hash != -1U) {
381 raw_spin_unlock(&wqe->lock);
382 io_wait_on_hash(wqe, stall_hash);
383 raw_spin_lock(&wqe->lock);
Jens Axboe771b53d02019-10-22 10:25:58 -0600384 }
385
386 return NULL;
387}
388
Jens Axboe00ddff42021-03-21 07:06:56 -0600389static bool io_flush_signals(void)
Jens Axboecccf0ee2020-01-27 16:34:48 -0700390{
Jens Axboe3bfe6102021-02-16 14:15:30 -0700391 if (unlikely(test_tsk_thread_flag(current, TIF_NOTIFY_SIGNAL))) {
Jens Axboe00ddff42021-03-21 07:06:56 -0600392 __set_current_state(TASK_RUNNING);
Jens Axboe3bfe6102021-02-16 14:15:30 -0700393 if (current->task_works)
394 task_work_run();
395 clear_tsk_thread_flag(current, TIF_NOTIFY_SIGNAL);
Jens Axboe00ddff42021-03-21 07:06:56 -0600396 return true;
Jens Axboecccf0ee2020-01-27 16:34:48 -0700397 }
Jens Axboe00ddff42021-03-21 07:06:56 -0600398 return false;
Pavel Begunkovdc026a72020-03-04 16:14:09 +0300399}
400
401static void io_assign_current_work(struct io_worker *worker,
402 struct io_wq_work *work)
403{
Pavel Begunkovd78298e2020-03-14 00:31:03 +0300404 if (work) {
Jens Axboe3bfe6102021-02-16 14:15:30 -0700405 io_flush_signals();
Pavel Begunkovd78298e2020-03-14 00:31:03 +0300406 cond_resched();
407 }
Pavel Begunkovdc026a72020-03-04 16:14:09 +0300408
409 spin_lock_irq(&worker->lock);
410 worker->cur_work = work;
411 spin_unlock_irq(&worker->lock);
412}
413
Pavel Begunkov60cf46a2020-03-14 00:31:05 +0300414static void io_wqe_enqueue(struct io_wqe *wqe, struct io_wq_work *work);
415
Jens Axboe771b53d02019-10-22 10:25:58 -0600416static void io_worker_handle_work(struct io_worker *worker)
417 __releases(wqe->lock)
418{
Jens Axboe771b53d02019-10-22 10:25:58 -0600419 struct io_wqe *wqe = worker->wqe;
420 struct io_wq *wq = wqe->wq;
421
422 do {
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300423 struct io_wq_work *work;
Pavel Begunkovf462fd32020-03-04 16:14:11 +0300424get_next:
Jens Axboe771b53d02019-10-22 10:25:58 -0600425 /*
Jens Axboe771b53d02019-10-22 10:25:58 -0600426 * If we got some work, mark us as busy. If we didn't, but
427 * the list isn't empty, it means we stalled on hashed work.
428 * Mark us stalled so we don't keep looking for work when we
429 * can't make progress, any work completion or insertion will
430 * clear the stalled flag.
431 */
Pavel Begunkov60cf46a2020-03-14 00:31:05 +0300432 work = io_get_next_work(wqe);
Jens Axboe771b53d02019-10-22 10:25:58 -0600433 if (work)
434 __io_worker_busy(wqe, worker, work);
Jens Axboe6206f0e2019-11-26 11:59:32 -0700435 else if (!wq_list_empty(&wqe->work_list))
Jens Axboe771b53d02019-10-22 10:25:58 -0600436 wqe->flags |= IO_WQE_FLAG_STALLED;
437
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200438 raw_spin_unlock_irq(&wqe->lock);
Jens Axboe771b53d02019-10-22 10:25:58 -0600439 if (!work)
440 break;
Pavel Begunkov58e39312020-03-04 16:14:10 +0300441 io_assign_current_work(worker, work);
Jens Axboee9418942021-02-19 12:33:30 -0700442 __set_current_state(TASK_RUNNING);
Jens Axboe36c2f922019-11-13 09:43:34 -0700443
Pavel Begunkovdc026a72020-03-04 16:14:09 +0300444 /* handle a whole dependent link */
445 do {
Pavel Begunkov5280f7e2021-02-04 13:52:08 +0000446 struct io_wq_work *next_hashed, *linked;
Pavel Begunkovb089ed392020-07-25 14:42:00 +0300447 unsigned int hash = io_get_work_hash(work);
Hillf Dantonfd1c4bc2019-12-24 09:14:29 -0700448
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300449 next_hashed = wq_next_work(work);
Pavel Begunkov5280f7e2021-02-04 13:52:08 +0000450 wq->do_work(work);
451 io_assign_current_work(worker, NULL);
Jens Axboe36c2f922019-11-13 09:43:34 -0700452
Pavel Begunkov5280f7e2021-02-04 13:52:08 +0000453 linked = wq->free_work(work);
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300454 work = next_hashed;
455 if (!work && linked && !io_wq_is_hashed(linked)) {
456 work = linked;
457 linked = NULL;
458 }
459 io_assign_current_work(worker, work);
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300460 if (linked)
461 io_wqe_enqueue(wqe, linked);
462
463 if (hash != -1U && !next_hashed) {
Jens Axboee9418942021-02-19 12:33:30 -0700464 clear_bit(hash, &wq->hash->map);
465 if (wq_has_sleeper(&wq->hash->wait))
466 wake_up(&wq->hash->wait);
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200467 raw_spin_lock_irq(&wqe->lock);
Pavel Begunkovdc026a72020-03-04 16:14:09 +0300468 wqe->flags &= ~IO_WQE_FLAG_STALLED;
Pavel Begunkovf462fd32020-03-04 16:14:11 +0300469 /* skip unnecessary unlock-lock wqe->lock */
470 if (!work)
471 goto get_next;
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200472 raw_spin_unlock_irq(&wqe->lock);
Pavel Begunkovdc026a72020-03-04 16:14:09 +0300473 }
Pavel Begunkov58e39312020-03-04 16:14:10 +0300474 } while (work);
Jens Axboe36c2f922019-11-13 09:43:34 -0700475
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200476 raw_spin_lock_irq(&wqe->lock);
Jens Axboe771b53d02019-10-22 10:25:58 -0600477 } while (1);
478}
479
Jens Axboe771b53d02019-10-22 10:25:58 -0600480static int io_wqe_worker(void *data)
481{
482 struct io_worker *worker = data;
483 struct io_wqe *wqe = worker->wqe;
484 struct io_wq *wq = wqe->wq;
Jens Axboe46fe18b2021-03-04 12:39:36 -0700485 char buf[TASK_COMM_LEN];
Jens Axboe771b53d02019-10-22 10:25:58 -0600486
Jens Axboe46fe18b2021-03-04 12:39:36 -0700487 worker->flags |= (IO_WORKER_F_UP | IO_WORKER_F_RUNNING);
488 io_wqe_inc_running(worker);
489
490 sprintf(buf, "iou-wrk-%d", wq->task_pid);
491 set_task_comm(current, buf);
Jens Axboe771b53d02019-10-22 10:25:58 -0600492
493 while (!test_bit(IO_WQ_BIT_EXIT, &wq->state)) {
Jens Axboe16efa4f2021-03-12 20:26:13 -0700494 long ret;
495
Jens Axboe506d95f2019-12-07 21:03:59 -0700496 set_current_state(TASK_INTERRUPTIBLE);
Jens Axboee995d512019-12-07 21:06:46 -0700497loop:
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200498 raw_spin_lock_irq(&wqe->lock);
Jens Axboe771b53d02019-10-22 10:25:58 -0600499 if (io_wqe_run_queue(wqe)) {
Jens Axboe771b53d02019-10-22 10:25:58 -0600500 io_worker_handle_work(worker);
Jens Axboee995d512019-12-07 21:06:46 -0700501 goto loop;
Jens Axboe771b53d02019-10-22 10:25:58 -0600502 }
Jens Axboec6d77d92021-02-15 13:26:34 -0700503 __io_worker_idle(wqe, worker);
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200504 raw_spin_unlock_irq(&wqe->lock);
Jens Axboe00ddff42021-03-21 07:06:56 -0600505 if (io_flush_signals())
506 continue;
Jens Axboe16efa4f2021-03-12 20:26:13 -0700507 ret = schedule_timeout(WORKER_IDLE_TIMEOUT);
508 if (try_to_freeze() || ret)
Jens Axboe771b53d02019-10-22 10:25:58 -0600509 continue;
Jens Axboe3bfe6102021-02-16 14:15:30 -0700510 if (fatal_signal_pending(current))
511 break;
Jens Axboe771b53d02019-10-22 10:25:58 -0600512 /* timed out, exit unless we're the fixed worker */
513 if (test_bit(IO_WQ_BIT_EXIT, &wq->state) ||
514 !(worker->flags & IO_WORKER_F_FIXED))
515 break;
516 }
517
Jens Axboe771b53d02019-10-22 10:25:58 -0600518 if (test_bit(IO_WQ_BIT_EXIT, &wq->state)) {
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200519 raw_spin_lock_irq(&wqe->lock);
Jens Axboe6206f0e2019-11-26 11:59:32 -0700520 if (!wq_list_empty(&wqe->work_list))
Jens Axboe771b53d02019-10-22 10:25:58 -0600521 io_worker_handle_work(worker);
522 else
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200523 raw_spin_unlock_irq(&wqe->lock);
Jens Axboe771b53d02019-10-22 10:25:58 -0600524 }
525
526 io_worker_exit(worker);
527 return 0;
528}
529
530/*
Jens Axboe771b53d02019-10-22 10:25:58 -0600531 * Called when a worker is scheduled in. Mark us as currently running.
532 */
533void io_wq_worker_running(struct task_struct *tsk)
534{
Jens Axboe3bfe6102021-02-16 14:15:30 -0700535 struct io_worker *worker = tsk->pf_io_worker;
Jens Axboe771b53d02019-10-22 10:25:58 -0600536
Jens Axboe3bfe6102021-02-16 14:15:30 -0700537 if (!worker)
538 return;
Jens Axboe771b53d02019-10-22 10:25:58 -0600539 if (!(worker->flags & IO_WORKER_F_UP))
540 return;
541 if (worker->flags & IO_WORKER_F_RUNNING)
542 return;
543 worker->flags |= IO_WORKER_F_RUNNING;
Jens Axboe958234d2021-02-17 09:00:57 -0700544 io_wqe_inc_running(worker);
Jens Axboe771b53d02019-10-22 10:25:58 -0600545}
546
547/*
548 * Called when worker is going to sleep. If there are no workers currently
549 * running and we have work pending, wake up a free one or have the manager
550 * set one up.
551 */
552void io_wq_worker_sleeping(struct task_struct *tsk)
553{
Jens Axboe3bfe6102021-02-16 14:15:30 -0700554 struct io_worker *worker = tsk->pf_io_worker;
Jens Axboe771b53d02019-10-22 10:25:58 -0600555
Jens Axboe3bfe6102021-02-16 14:15:30 -0700556 if (!worker)
557 return;
Jens Axboe771b53d02019-10-22 10:25:58 -0600558 if (!(worker->flags & IO_WORKER_F_UP))
559 return;
560 if (!(worker->flags & IO_WORKER_F_RUNNING))
561 return;
562
563 worker->flags &= ~IO_WORKER_F_RUNNING;
564
Jens Axboe3bfe6102021-02-16 14:15:30 -0700565 raw_spin_lock_irq(&worker->wqe->lock);
Jens Axboe958234d2021-02-17 09:00:57 -0700566 io_wqe_dec_running(worker);
Jens Axboe3bfe6102021-02-16 14:15:30 -0700567 raw_spin_unlock_irq(&worker->wqe->lock);
Jens Axboe771b53d02019-10-22 10:25:58 -0600568}
569
Jens Axboe3bfe6102021-02-16 14:15:30 -0700570static bool create_io_worker(struct io_wq *wq, struct io_wqe *wqe, int index)
571{
Jens Axboe46fe18b2021-03-04 12:39:36 -0700572 struct io_wqe_acct *acct = &wqe->acct[index];
Jens Axboe3bfe6102021-02-16 14:15:30 -0700573 struct io_worker *worker;
Jens Axboe46fe18b2021-03-04 12:39:36 -0700574 struct task_struct *tsk;
Jens Axboe3bfe6102021-02-16 14:15:30 -0700575
Jens Axboe8b3e78b2021-02-23 15:34:06 -0700576 __set_current_state(TASK_RUNNING);
577
Jens Axboe3bfe6102021-02-16 14:15:30 -0700578 worker = kzalloc_node(sizeof(*worker), GFP_KERNEL, wqe->node);
579 if (!worker)
580 return false;
581
582 refcount_set(&worker->ref, 1);
583 worker->nulls_node.pprev = NULL;
584 worker->wqe = wqe;
585 spin_lock_init(&worker->lock);
Jens Axboeeb2de942021-02-23 19:59:06 -0700586 init_completion(&worker->ref_done);
Jens Axboe3bfe6102021-02-16 14:15:30 -0700587
Jens Axboefb3a1f62021-02-26 09:47:20 -0700588 atomic_inc(&wq->worker_refs);
Jens Axboe8b3e78b2021-02-23 15:34:06 -0700589
Jens Axboe46fe18b2021-03-04 12:39:36 -0700590 tsk = create_io_thread(io_wqe_worker, worker, wqe->node);
591 if (IS_ERR(tsk)) {
Jens Axboefb3a1f62021-02-26 09:47:20 -0700592 if (atomic_dec_and_test(&wq->worker_refs))
593 complete(&wq->worker_done);
Jens Axboe3bfe6102021-02-16 14:15:30 -0700594 kfree(worker);
595 return false;
596 }
Jens Axboe46fe18b2021-03-04 12:39:36 -0700597
598 tsk->pf_io_worker = worker;
599 worker->task = tsk;
600 set_cpus_allowed_ptr(tsk, cpumask_of_node(wqe->node));
Jens Axboee22bc9b2021-03-09 19:49:02 -0700601 tsk->flags |= PF_NO_SETAFFINITY;
Jens Axboe46fe18b2021-03-04 12:39:36 -0700602
603 raw_spin_lock_irq(&wqe->lock);
604 hlist_nulls_add_head_rcu(&worker->nulls_node, &wqe->free_list);
605 list_add_tail_rcu(&worker->all_list, &wqe->all_list);
606 worker->flags |= IO_WORKER_F_FREE;
607 if (index == IO_WQ_ACCT_BOUND)
608 worker->flags |= IO_WORKER_F_BOUND;
609 if (!acct->nr_workers && (worker->flags & IO_WORKER_F_BOUND))
610 worker->flags |= IO_WORKER_F_FIXED;
611 acct->nr_workers++;
612 raw_spin_unlock_irq(&wqe->lock);
613 wake_up_new_task(tsk);
Jens Axboeb60fda62019-11-19 08:37:07 -0700614 return true;
Jens Axboe771b53d02019-10-22 10:25:58 -0600615}
616
Jens Axboec5def4a2019-11-07 11:41:16 -0700617static inline bool io_wqe_need_worker(struct io_wqe *wqe, int index)
Jens Axboe771b53d02019-10-22 10:25:58 -0600618 __must_hold(wqe->lock)
619{
Jens Axboec5def4a2019-11-07 11:41:16 -0700620 struct io_wqe_acct *acct = &wqe->acct[index];
Jens Axboe771b53d02019-10-22 10:25:58 -0600621
Jens Axboe613eeb62021-02-26 09:52:02 -0700622 if (acct->nr_workers && test_bit(IO_WQ_BIT_EXIT, &wqe->wq->state))
623 return false;
Jens Axboec5def4a2019-11-07 11:41:16 -0700624 /* if we have available workers or no work, no need */
Jens Axboe021d1cd2019-11-14 08:00:41 -0700625 if (!hlist_nulls_empty(&wqe->free_list) || !io_wqe_run_queue(wqe))
Jens Axboec5def4a2019-11-07 11:41:16 -0700626 return false;
627 return acct->nr_workers < acct->max_workers;
Jens Axboe771b53d02019-10-22 10:25:58 -0600628}
629
Hillf Dantonc4068bf2020-09-26 21:26:55 +0800630/*
631 * Iterate the passed in list and call the specific function for each
632 * worker that isn't exiting
633 */
634static bool io_wq_for_each_worker(struct io_wqe *wqe,
635 bool (*func)(struct io_worker *, void *),
636 void *data)
637{
638 struct io_worker *worker;
639 bool ret = false;
640
641 list_for_each_entry_rcu(worker, &wqe->all_list, all_list) {
642 if (io_worker_get(worker)) {
643 /* no task if node is/was offline */
644 if (worker->task)
645 ret = func(worker, data);
646 io_worker_release(worker);
647 if (ret)
648 break;
649 }
650 }
651
652 return ret;
653}
654
655static bool io_wq_worker_wake(struct io_worker *worker, void *data)
656{
Jens Axboe46fe18b2021-03-04 12:39:36 -0700657 set_notify_signal(worker->task);
Hillf Dantonc4068bf2020-09-26 21:26:55 +0800658 wake_up_process(worker->task);
659 return false;
660}
661
Jens Axboe8b3e78b2021-02-23 15:34:06 -0700662static void io_wq_check_workers(struct io_wq *wq)
663{
664 int node;
665
666 for_each_node(node) {
667 struct io_wqe *wqe = wq->wqes[node];
668 bool fork_worker[2] = { false, false };
669
670 if (!node_online(node))
671 continue;
672
673 raw_spin_lock_irq(&wqe->lock);
674 if (io_wqe_need_worker(wqe, IO_WQ_ACCT_BOUND))
675 fork_worker[IO_WQ_ACCT_BOUND] = true;
676 if (io_wqe_need_worker(wqe, IO_WQ_ACCT_UNBOUND))
677 fork_worker[IO_WQ_ACCT_UNBOUND] = true;
678 raw_spin_unlock_irq(&wqe->lock);
679 if (fork_worker[IO_WQ_ACCT_BOUND])
680 create_io_worker(wq, wqe, IO_WQ_ACCT_BOUND);
681 if (fork_worker[IO_WQ_ACCT_UNBOUND])
682 create_io_worker(wq, wqe, IO_WQ_ACCT_UNBOUND);
683 }
684}
685
Jens Axboef0127252021-03-03 15:47:04 -0700686static bool io_wq_work_match_all(struct io_wq_work *work, void *data)
687{
688 return true;
689}
690
691static void io_wq_cancel_pending(struct io_wq *wq)
692{
693 struct io_cb_cancel_data match = {
694 .fn = io_wq_work_match_all,
695 .cancel_all = true,
696 };
697 int node;
698
699 for_each_node(node)
700 io_wqe_cancel_pending_work(wq->wqes[node], &match);
701}
702
Jens Axboe771b53d02019-10-22 10:25:58 -0600703/*
704 * Manager thread. Tasked with creating new workers, if we need them.
705 */
706static int io_wq_manager(void *data)
707{
708 struct io_wq *wq = data;
Jens Axboe3bfe6102021-02-16 14:15:30 -0700709 char buf[TASK_COMM_LEN];
Jens Axboefb3a1f62021-02-26 09:47:20 -0700710 int node;
Jens Axboeb60fda62019-11-19 08:37:07 -0700711
Jens Axboe3bfe6102021-02-16 14:15:30 -0700712 sprintf(buf, "iou-mgr-%d", wq->task_pid);
713 set_task_comm(current, buf);
Jens Axboe771b53d02019-10-22 10:25:58 -0600714
Jens Axboe8b3e78b2021-02-23 15:34:06 -0700715 do {
Jens Axboe771b53d02019-10-22 10:25:58 -0600716 set_current_state(TASK_INTERRUPTIBLE);
Jens Axboe8b3e78b2021-02-23 15:34:06 -0700717 io_wq_check_workers(wq);
Jens Axboe771b53d02019-10-22 10:25:58 -0600718 schedule_timeout(HZ);
Jens Axboe16efa4f2021-03-12 20:26:13 -0700719 try_to_freeze();
Jens Axboe3bfe6102021-02-16 14:15:30 -0700720 if (fatal_signal_pending(current))
721 set_bit(IO_WQ_BIT_EXIT, &wq->state);
Jens Axboe8b3e78b2021-02-23 15:34:06 -0700722 } while (!test_bit(IO_WQ_BIT_EXIT, &wq->state));
723
724 io_wq_check_workers(wq);
Jens Axboefb3a1f62021-02-26 09:47:20 -0700725
726 rcu_read_lock();
727 for_each_node(node)
728 io_wq_for_each_worker(wq->wqes[node], io_wq_worker_wake, NULL);
729 rcu_read_unlock();
730
Jens Axboe886d0132021-03-05 12:59:30 -0700731 if (atomic_dec_and_test(&wq->worker_refs))
732 complete(&wq->worker_done);
733 wait_for_completion(&wq->worker_done);
Jens Axboef0127252021-03-03 15:47:04 -0700734
Jens Axboe09ca6c42021-03-05 08:14:08 -0700735 spin_lock_irq(&wq->hash->wait.lock);
736 for_each_node(node)
737 list_del_init(&wq->wqes[node]->wait.entry);
738 spin_unlock_irq(&wq->hash->wait.lock);
739
Jens Axboef0127252021-03-03 15:47:04 -0700740 io_wq_cancel_pending(wq);
Jens Axboed364d9e2021-02-26 09:56:32 -0700741 complete(&wq->exited);
Jens Axboe3bfe6102021-02-16 14:15:30 -0700742 do_exit(0);
Jens Axboe771b53d02019-10-22 10:25:58 -0600743}
744
Pavel Begunkove9fd9392020-03-04 16:14:12 +0300745static void io_run_cancel(struct io_wq_work *work, struct io_wqe *wqe)
Pavel Begunkovfc04c392020-03-01 19:18:19 +0300746{
Pavel Begunkove9fd9392020-03-04 16:14:12 +0300747 struct io_wq *wq = wqe->wq;
748
Pavel Begunkovfc04c392020-03-01 19:18:19 +0300749 do {
Pavel Begunkovfc04c392020-03-01 19:18:19 +0300750 work->flags |= IO_WQ_WORK_CANCEL;
Pavel Begunkov5280f7e2021-02-04 13:52:08 +0000751 wq->do_work(work);
752 work = wq->free_work(work);
Pavel Begunkovfc04c392020-03-01 19:18:19 +0300753 } while (work);
754}
755
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300756static void io_wqe_insert_work(struct io_wqe *wqe, struct io_wq_work *work)
757{
758 unsigned int hash;
759 struct io_wq_work *tail;
760
761 if (!io_wq_is_hashed(work)) {
762append:
763 wq_list_add_tail(&work->list, &wqe->work_list);
764 return;
765 }
766
767 hash = io_get_work_hash(work);
768 tail = wqe->hash_tail[hash];
769 wqe->hash_tail[hash] = work;
770 if (!tail)
771 goto append;
772
773 wq_list_add_after(&work->list, &tail->list, &wqe->work_list);
774}
775
Jens Axboe4fb6ac32021-02-25 10:17:09 -0700776static int io_wq_fork_manager(struct io_wq *wq)
777{
Jens Axboe46fe18b2021-03-04 12:39:36 -0700778 struct task_struct *tsk;
Jens Axboe4fb6ac32021-02-25 10:17:09 -0700779
780 if (wq->manager)
781 return 0;
782
Pavel Begunkov678eeba2021-03-06 11:02:18 +0000783 WARN_ON_ONCE(test_bit(IO_WQ_BIT_EXIT, &wq->state));
784
Jens Axboe886d0132021-03-05 12:59:30 -0700785 init_completion(&wq->worker_done);
786 atomic_set(&wq->worker_refs, 1);
Jens Axboe46fe18b2021-03-04 12:39:36 -0700787 tsk = create_io_thread(io_wq_manager, wq, NUMA_NO_NODE);
788 if (!IS_ERR(tsk)) {
789 wq->manager = get_task_struct(tsk);
790 wake_up_new_task(tsk);
Jens Axboe4fb6ac32021-02-25 10:17:09 -0700791 return 0;
792 }
793
Jens Axboe886d0132021-03-05 12:59:30 -0700794 if (atomic_dec_and_test(&wq->worker_refs))
795 complete(&wq->worker_done);
796
Jens Axboe46fe18b2021-03-04 12:39:36 -0700797 return PTR_ERR(tsk);
Jens Axboe4fb6ac32021-02-25 10:17:09 -0700798}
799
Jens Axboe771b53d02019-10-22 10:25:58 -0600800static void io_wqe_enqueue(struct io_wqe *wqe, struct io_wq_work *work)
801{
Jens Axboec5def4a2019-11-07 11:41:16 -0700802 struct io_wqe_acct *acct = io_work_get_acct(wqe, work);
Jens Axboe895e2ca2019-12-17 08:46:33 -0700803 int work_flags;
Jens Axboe771b53d02019-10-22 10:25:58 -0600804 unsigned long flags;
805
Jens Axboe4fb6ac32021-02-25 10:17:09 -0700806 /* Can only happen if manager creation fails after exec */
Jens Axboef0127252021-03-03 15:47:04 -0700807 if (io_wq_fork_manager(wqe->wq) ||
808 test_bit(IO_WQ_BIT_EXIT, &wqe->wq->state)) {
yangerkun70e35122021-03-09 11:04:10 +0800809 io_run_cancel(work, wqe);
Jens Axboe4fb6ac32021-02-25 10:17:09 -0700810 return;
811 }
812
Jens Axboe895e2ca2019-12-17 08:46:33 -0700813 work_flags = work->flags;
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200814 raw_spin_lock_irqsave(&wqe->lock, flags);
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300815 io_wqe_insert_work(wqe, work);
Jens Axboe771b53d02019-10-22 10:25:58 -0600816 wqe->flags &= ~IO_WQE_FLAG_STALLED;
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200817 raw_spin_unlock_irqrestore(&wqe->lock, flags);
Jens Axboe771b53d02019-10-22 10:25:58 -0600818
Jens Axboe895e2ca2019-12-17 08:46:33 -0700819 if ((work_flags & IO_WQ_WORK_CONCURRENT) ||
820 !atomic_read(&acct->nr_running))
Jens Axboec5def4a2019-11-07 11:41:16 -0700821 io_wqe_wake_worker(wqe, acct);
Jens Axboe771b53d02019-10-22 10:25:58 -0600822}
823
824void io_wq_enqueue(struct io_wq *wq, struct io_wq_work *work)
825{
826 struct io_wqe *wqe = wq->wqes[numa_node_id()];
827
828 io_wqe_enqueue(wqe, work);
829}
830
831/*
Pavel Begunkov8766dd52020-03-14 00:31:04 +0300832 * Work items that hash to the same value will not be done in parallel.
833 * Used to limit concurrent writes, generally hashed by inode.
Jens Axboe771b53d02019-10-22 10:25:58 -0600834 */
Pavel Begunkov8766dd52020-03-14 00:31:04 +0300835void io_wq_hash_work(struct io_wq_work *work, void *val)
Jens Axboe771b53d02019-10-22 10:25:58 -0600836{
Pavel Begunkov8766dd52020-03-14 00:31:04 +0300837 unsigned int bit;
Jens Axboe771b53d02019-10-22 10:25:58 -0600838
839 bit = hash_ptr(val, IO_WQ_HASH_ORDER);
840 work->flags |= (IO_WQ_WORK_HASHED | (bit << IO_WQ_HASH_SHIFT));
Jens Axboe771b53d02019-10-22 10:25:58 -0600841}
842
Pavel Begunkov2293b412020-03-07 01:15:39 +0300843static bool io_wq_worker_cancel(struct io_worker *worker, void *data)
Jens Axboe62755e32019-10-28 21:49:21 -0600844{
Pavel Begunkov2293b412020-03-07 01:15:39 +0300845 struct io_cb_cancel_data *match = data;
Jens Axboe6f726532019-11-05 13:51:51 -0700846 unsigned long flags;
Jens Axboe62755e32019-10-28 21:49:21 -0600847
848 /*
849 * Hold the lock to avoid ->cur_work going out of scope, caller
Jens Axboe36c2f922019-11-13 09:43:34 -0700850 * may dereference the passed in work.
Jens Axboe62755e32019-10-28 21:49:21 -0600851 */
Jens Axboe36c2f922019-11-13 09:43:34 -0700852 spin_lock_irqsave(&worker->lock, flags);
Jens Axboe62755e32019-10-28 21:49:21 -0600853 if (worker->cur_work &&
Pavel Begunkov2293b412020-03-07 01:15:39 +0300854 match->fn(worker->cur_work, match->data)) {
Jens Axboe3bfe6102021-02-16 14:15:30 -0700855 set_notify_signal(worker->task);
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300856 match->nr_running++;
Jens Axboe771b53d02019-10-22 10:25:58 -0600857 }
Jens Axboe36c2f922019-11-13 09:43:34 -0700858 spin_unlock_irqrestore(&worker->lock, flags);
Jens Axboe771b53d02019-10-22 10:25:58 -0600859
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300860 return match->nr_running && !match->cancel_all;
Jens Axboe771b53d02019-10-22 10:25:58 -0600861}
862
Pavel Begunkov204361a2020-08-23 20:33:10 +0300863static inline void io_wqe_remove_pending(struct io_wqe *wqe,
864 struct io_wq_work *work,
865 struct io_wq_work_node *prev)
866{
867 unsigned int hash = io_get_work_hash(work);
868 struct io_wq_work *prev_work = NULL;
869
870 if (io_wq_is_hashed(work) && work == wqe->hash_tail[hash]) {
871 if (prev)
872 prev_work = container_of(prev, struct io_wq_work, list);
873 if (prev_work && io_get_work_hash(prev_work) == hash)
874 wqe->hash_tail[hash] = prev_work;
875 else
876 wqe->hash_tail[hash] = NULL;
877 }
878 wq_list_del(&wqe->work_list, &work->list, prev);
879}
880
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300881static void io_wqe_cancel_pending_work(struct io_wqe *wqe,
Pavel Begunkovf4c26652020-06-15 10:24:02 +0300882 struct io_cb_cancel_data *match)
Jens Axboe771b53d02019-10-22 10:25:58 -0600883{
Jens Axboe6206f0e2019-11-26 11:59:32 -0700884 struct io_wq_work_node *node, *prev;
Jens Axboe771b53d02019-10-22 10:25:58 -0600885 struct io_wq_work *work;
Jens Axboe6f726532019-11-05 13:51:51 -0700886 unsigned long flags;
Jens Axboe771b53d02019-10-22 10:25:58 -0600887
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300888retry:
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200889 raw_spin_lock_irqsave(&wqe->lock, flags);
Jens Axboe6206f0e2019-11-26 11:59:32 -0700890 wq_list_for_each(node, prev, &wqe->work_list) {
891 work = container_of(node, struct io_wq_work, list);
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300892 if (!match->fn(work, match->data))
893 continue;
Pavel Begunkov204361a2020-08-23 20:33:10 +0300894 io_wqe_remove_pending(wqe, work, prev);
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200895 raw_spin_unlock_irqrestore(&wqe->lock, flags);
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300896 io_run_cancel(work, wqe);
897 match->nr_pending++;
898 if (!match->cancel_all)
899 return;
900
901 /* not safe to continue after unlock */
902 goto retry;
Jens Axboe771b53d02019-10-22 10:25:58 -0600903 }
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200904 raw_spin_unlock_irqrestore(&wqe->lock, flags);
Pavel Begunkovf4c26652020-06-15 10:24:02 +0300905}
Jens Axboe771b53d02019-10-22 10:25:58 -0600906
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300907static void io_wqe_cancel_running_work(struct io_wqe *wqe,
Pavel Begunkovf4c26652020-06-15 10:24:02 +0300908 struct io_cb_cancel_data *match)
909{
Jens Axboe771b53d02019-10-22 10:25:58 -0600910 rcu_read_lock();
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300911 io_wq_for_each_worker(wqe, io_wq_worker_cancel, match);
Jens Axboe771b53d02019-10-22 10:25:58 -0600912 rcu_read_unlock();
Jens Axboe771b53d02019-10-22 10:25:58 -0600913}
914
Pavel Begunkov2293b412020-03-07 01:15:39 +0300915enum io_wq_cancel io_wq_cancel_cb(struct io_wq *wq, work_cancel_fn *cancel,
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300916 void *data, bool cancel_all)
Pavel Begunkov2293b412020-03-07 01:15:39 +0300917{
918 struct io_cb_cancel_data match = {
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300919 .fn = cancel,
920 .data = data,
921 .cancel_all = cancel_all,
Pavel Begunkov2293b412020-03-07 01:15:39 +0300922 };
Pavel Begunkov2293b412020-03-07 01:15:39 +0300923 int node;
924
Pavel Begunkovf4c26652020-06-15 10:24:02 +0300925 /*
926 * First check pending list, if we're lucky we can just remove it
927 * from there. CANCEL_OK means that the work is returned as-new,
928 * no completion will be posted for it.
929 */
Pavel Begunkov2293b412020-03-07 01:15:39 +0300930 for_each_node(node) {
931 struct io_wqe *wqe = wq->wqes[node];
932
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300933 io_wqe_cancel_pending_work(wqe, &match);
934 if (match.nr_pending && !match.cancel_all)
Pavel Begunkovf4c26652020-06-15 10:24:02 +0300935 return IO_WQ_CANCEL_OK;
Pavel Begunkov2293b412020-03-07 01:15:39 +0300936 }
937
Pavel Begunkovf4c26652020-06-15 10:24:02 +0300938 /*
939 * Now check if a free (going busy) or busy worker has the work
940 * currently running. If we find it there, we'll return CANCEL_RUNNING
941 * as an indication that we attempt to signal cancellation. The
942 * completion will run normally in this case.
943 */
944 for_each_node(node) {
945 struct io_wqe *wqe = wq->wqes[node];
946
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300947 io_wqe_cancel_running_work(wqe, &match);
948 if (match.nr_running && !match.cancel_all)
Pavel Begunkovf4c26652020-06-15 10:24:02 +0300949 return IO_WQ_CANCEL_RUNNING;
950 }
951
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300952 if (match.nr_running)
953 return IO_WQ_CANCEL_RUNNING;
954 if (match.nr_pending)
955 return IO_WQ_CANCEL_OK;
Pavel Begunkovf4c26652020-06-15 10:24:02 +0300956 return IO_WQ_CANCEL_NOTFOUND;
Pavel Begunkov2293b412020-03-07 01:15:39 +0300957}
958
Jens Axboee9418942021-02-19 12:33:30 -0700959static int io_wqe_hash_wake(struct wait_queue_entry *wait, unsigned mode,
960 int sync, void *key)
961{
962 struct io_wqe *wqe = container_of(wait, struct io_wqe, wait);
963 int ret;
964
965 list_del_init(&wait->entry);
966
967 rcu_read_lock();
968 ret = io_wqe_activate_free_worker(wqe);
969 rcu_read_unlock();
970
971 if (!ret)
972 wake_up_process(wqe->wq->manager);
973
974 return 1;
975}
976
Jens Axboe576a3472019-11-25 08:49:20 -0700977struct io_wq *io_wq_create(unsigned bounded, struct io_wq_data *data)
Jens Axboe771b53d02019-10-22 10:25:58 -0600978{
Jann Horn3fc50ab2019-11-26 19:10:20 +0100979 int ret = -ENOMEM, node;
Jens Axboe771b53d02019-10-22 10:25:58 -0600980 struct io_wq *wq;
981
Pavel Begunkovf5fa38c2020-06-08 21:08:20 +0300982 if (WARN_ON_ONCE(!data->free_work || !data->do_work))
Pavel Begunkove9fd9392020-03-04 16:14:12 +0300983 return ERR_PTR(-EINVAL);
984
Jann Hornad6e0052019-11-26 17:39:45 +0100985 wq = kzalloc(sizeof(*wq), GFP_KERNEL);
Jens Axboe771b53d02019-10-22 10:25:58 -0600986 if (!wq)
987 return ERR_PTR(-ENOMEM);
988
Jann Horn3fc50ab2019-11-26 19:10:20 +0100989 wq->wqes = kcalloc(nr_node_ids, sizeof(struct io_wqe *), GFP_KERNEL);
Jens Axboe43c01fb2020-10-22 09:02:50 -0600990 if (!wq->wqes)
991 goto err_wq;
992
993 ret = cpuhp_state_add_instance_nocalls(io_wq_online, &wq->cpuhp_node);
994 if (ret)
995 goto err_wqes;
Jens Axboe771b53d02019-10-22 10:25:58 -0600996
Jens Axboee9418942021-02-19 12:33:30 -0700997 refcount_inc(&data->hash->refs);
998 wq->hash = data->hash;
Pavel Begunkove9fd9392020-03-04 16:14:12 +0300999 wq->free_work = data->free_work;
Pavel Begunkovf5fa38c2020-06-08 21:08:20 +03001000 wq->do_work = data->do_work;
Jens Axboe7d723062019-11-12 22:31:31 -07001001
Jens Axboe43c01fb2020-10-22 09:02:50 -06001002 ret = -ENOMEM;
Jann Horn3fc50ab2019-11-26 19:10:20 +01001003 for_each_node(node) {
Jens Axboe771b53d02019-10-22 10:25:58 -06001004 struct io_wqe *wqe;
Jens Axboe75634392020-02-11 06:30:06 -07001005 int alloc_node = node;
Jens Axboe771b53d02019-10-22 10:25:58 -06001006
Jens Axboe75634392020-02-11 06:30:06 -07001007 if (!node_online(alloc_node))
1008 alloc_node = NUMA_NO_NODE;
1009 wqe = kzalloc_node(sizeof(struct io_wqe), GFP_KERNEL, alloc_node);
Jens Axboe771b53d02019-10-22 10:25:58 -06001010 if (!wqe)
Jann Horn3fc50ab2019-11-26 19:10:20 +01001011 goto err;
1012 wq->wqes[node] = wqe;
Jens Axboe75634392020-02-11 06:30:06 -07001013 wqe->node = alloc_node;
Jens Axboec5def4a2019-11-07 11:41:16 -07001014 wqe->acct[IO_WQ_ACCT_BOUND].max_workers = bounded;
1015 atomic_set(&wqe->acct[IO_WQ_ACCT_BOUND].nr_running, 0);
Jens Axboe728f13e2021-02-21 16:02:53 -07001016 wqe->acct[IO_WQ_ACCT_UNBOUND].max_workers =
Jens Axboec5def4a2019-11-07 11:41:16 -07001017 task_rlimit(current, RLIMIT_NPROC);
Jens Axboec5def4a2019-11-07 11:41:16 -07001018 atomic_set(&wqe->acct[IO_WQ_ACCT_UNBOUND].nr_running, 0);
Jens Axboee9418942021-02-19 12:33:30 -07001019 wqe->wait.func = io_wqe_hash_wake;
1020 INIT_LIST_HEAD(&wqe->wait.entry);
Jens Axboe771b53d02019-10-22 10:25:58 -06001021 wqe->wq = wq;
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +02001022 raw_spin_lock_init(&wqe->lock);
Jens Axboe6206f0e2019-11-26 11:59:32 -07001023 INIT_WQ_LIST(&wqe->work_list);
Jens Axboe021d1cd2019-11-14 08:00:41 -07001024 INIT_HLIST_NULLS_HEAD(&wqe->free_list, 0);
Jens Axboee61df662019-11-13 13:54:49 -07001025 INIT_LIST_HEAD(&wqe->all_list);
Jens Axboe771b53d02019-10-22 10:25:58 -06001026 }
1027
Jens Axboe3bfe6102021-02-16 14:15:30 -07001028 wq->task_pid = current->pid;
Jens Axboed364d9e2021-02-26 09:56:32 -07001029 init_completion(&wq->exited);
Jens Axboe3bfe6102021-02-16 14:15:30 -07001030 refcount_set(&wq->refs, 1);
Jens Axboe771b53d02019-10-22 10:25:58 -06001031
Jens Axboe4fb6ac32021-02-25 10:17:09 -07001032 ret = io_wq_fork_manager(wq);
1033 if (!ret)
Jens Axboe771b53d02019-10-22 10:25:58 -06001034 return wq;
Jens Axboeb60fda62019-11-19 08:37:07 -07001035err:
Jens Axboedc7bbc92021-03-01 09:09:56 -07001036 io_wq_put_hash(data->hash);
Jens Axboe43c01fb2020-10-22 09:02:50 -06001037 cpuhp_state_remove_instance_nocalls(io_wq_online, &wq->cpuhp_node);
Jann Horn3fc50ab2019-11-26 19:10:20 +01001038 for_each_node(node)
1039 kfree(wq->wqes[node]);
Jens Axboe43c01fb2020-10-22 09:02:50 -06001040err_wqes:
Jens Axboeb60fda62019-11-19 08:37:07 -07001041 kfree(wq->wqes);
Jens Axboe43c01fb2020-10-22 09:02:50 -06001042err_wq:
Jens Axboeb60fda62019-11-19 08:37:07 -07001043 kfree(wq);
Jens Axboe771b53d02019-10-22 10:25:58 -06001044 return ERR_PTR(ret);
1045}
1046
Jens Axboeafcc4012021-02-26 13:48:19 -07001047static void io_wq_destroy_manager(struct io_wq *wq)
1048{
1049 if (wq->manager) {
1050 wake_up_process(wq->manager);
1051 wait_for_completion(&wq->exited);
1052 put_task_struct(wq->manager);
1053 wq->manager = NULL;
1054 }
1055}
1056
Jens Axboe4fb6ac32021-02-25 10:17:09 -07001057static void io_wq_destroy(struct io_wq *wq)
Jens Axboe771b53d02019-10-22 10:25:58 -06001058{
Jann Horn3fc50ab2019-11-26 19:10:20 +01001059 int node;
Jens Axboe771b53d02019-10-22 10:25:58 -06001060
Jens Axboe43c01fb2020-10-22 09:02:50 -06001061 cpuhp_state_remove_instance_nocalls(io_wq_online, &wq->cpuhp_node);
1062
Jens Axboeb60fda62019-11-19 08:37:07 -07001063 set_bit(IO_WQ_BIT_EXIT, &wq->state);
Jens Axboeafcc4012021-02-26 13:48:19 -07001064 io_wq_destroy_manager(wq);
Jens Axboe771b53d02019-10-22 10:25:58 -06001065
Jens Axboee9418942021-02-19 12:33:30 -07001066 for_each_node(node) {
1067 struct io_wqe *wqe = wq->wqes[node];
Jens Axboef0127252021-03-03 15:47:04 -07001068 WARN_ON_ONCE(!wq_list_empty(&wqe->work_list));
Jens Axboee9418942021-02-19 12:33:30 -07001069 kfree(wqe);
1070 }
Jens Axboee9418942021-02-19 12:33:30 -07001071 io_wq_put_hash(wq->hash);
Jens Axboe771b53d02019-10-22 10:25:58 -06001072 kfree(wq->wqes);
1073 kfree(wq);
Jens Axboe4fb6ac32021-02-25 10:17:09 -07001074}
1075
1076void io_wq_put(struct io_wq *wq)
1077{
1078 if (refcount_dec_and_test(&wq->refs))
1079 io_wq_destroy(wq);
Jens Axboe771b53d02019-10-22 10:25:58 -06001080}
Jens Axboe848f7e12020-01-23 15:33:32 -07001081
Jens Axboeafcc4012021-02-26 13:48:19 -07001082void io_wq_put_and_exit(struct io_wq *wq)
1083{
1084 set_bit(IO_WQ_BIT_EXIT, &wq->state);
1085 io_wq_destroy_manager(wq);
1086 io_wq_put(wq);
1087}
1088
Jens Axboe43c01fb2020-10-22 09:02:50 -06001089static bool io_wq_worker_affinity(struct io_worker *worker, void *data)
1090{
1091 struct task_struct *task = worker->task;
1092 struct rq_flags rf;
1093 struct rq *rq;
1094
1095 rq = task_rq_lock(task, &rf);
1096 do_set_cpus_allowed(task, cpumask_of_node(worker->wqe->node));
1097 task->flags |= PF_NO_SETAFFINITY;
1098 task_rq_unlock(rq, task, &rf);
1099 return false;
1100}
1101
1102static int io_wq_cpu_online(unsigned int cpu, struct hlist_node *node)
1103{
1104 struct io_wq *wq = hlist_entry_safe(node, struct io_wq, cpuhp_node);
1105 int i;
1106
1107 rcu_read_lock();
1108 for_each_node(i)
1109 io_wq_for_each_worker(wq->wqes[i], io_wq_worker_affinity, NULL);
1110 rcu_read_unlock();
1111 return 0;
1112}
1113
1114static __init int io_wq_init(void)
1115{
1116 int ret;
1117
1118 ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "io-wq/online",
1119 io_wq_cpu_online, NULL);
1120 if (ret < 0)
1121 return ret;
1122 io_wq_online = ret;
1123 return 0;
1124}
1125subsys_initcall(io_wq_init);