blob: 1bfdb86336e49b9627e40ffc60913cbad13e4b69 [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 Axboec5def4a2019-11-07 11:41:16 -0700113 struct user_struct *user;
Jens Axboee9418942021-02-19 12:33:30 -0700114
115 struct io_wq_hash *hash;
116
Jens Axboe771b53d02019-10-22 10:25:58 -0600117 refcount_t refs;
Jens Axboed364d9e2021-02-26 09:56:32 -0700118 struct completion exited;
Jens Axboe848f7e12020-01-23 15:33:32 -0700119
Jens Axboefb3a1f62021-02-26 09:47:20 -0700120 atomic_t worker_refs;
121 struct completion worker_done;
122
Jens Axboe43c01fb2020-10-22 09:02:50 -0600123 struct hlist_node cpuhp_node;
Jens Axboe3bfe6102021-02-16 14:15:30 -0700124
125 pid_t task_pid;
Jens Axboe771b53d02019-10-22 10:25:58 -0600126};
127
Jens Axboe43c01fb2020-10-22 09:02:50 -0600128static enum cpuhp_state io_wq_online;
129
Jens Axboef0127252021-03-03 15:47:04 -0700130struct io_cb_cancel_data {
131 work_cancel_fn *fn;
132 void *data;
133 int nr_running;
134 int nr_pending;
135 bool cancel_all;
136};
137
138static void io_wqe_cancel_pending_work(struct io_wqe *wqe,
139 struct io_cb_cancel_data *match);
140
Jens Axboe771b53d02019-10-22 10:25:58 -0600141static bool io_worker_get(struct io_worker *worker)
142{
143 return refcount_inc_not_zero(&worker->ref);
144}
145
146static void io_worker_release(struct io_worker *worker)
147{
148 if (refcount_dec_and_test(&worker->ref))
Jens Axboeeb2de942021-02-23 19:59:06 -0700149 complete(&worker->ref_done);
Jens Axboe771b53d02019-10-22 10:25:58 -0600150}
151
Jens Axboec5def4a2019-11-07 11:41:16 -0700152static inline struct io_wqe_acct *io_work_get_acct(struct io_wqe *wqe,
153 struct io_wq_work *work)
154{
155 if (work->flags & IO_WQ_WORK_UNBOUND)
156 return &wqe->acct[IO_WQ_ACCT_UNBOUND];
157
158 return &wqe->acct[IO_WQ_ACCT_BOUND];
159}
160
Jens Axboe958234d2021-02-17 09:00:57 -0700161static inline struct io_wqe_acct *io_wqe_get_acct(struct io_worker *worker)
Jens Axboec5def4a2019-11-07 11:41:16 -0700162{
Jens Axboe958234d2021-02-17 09:00:57 -0700163 struct io_wqe *wqe = worker->wqe;
164
Jens Axboec5def4a2019-11-07 11:41:16 -0700165 if (worker->flags & IO_WORKER_F_BOUND)
166 return &wqe->acct[IO_WQ_ACCT_BOUND];
167
168 return &wqe->acct[IO_WQ_ACCT_UNBOUND];
169}
170
Jens Axboe771b53d02019-10-22 10:25:58 -0600171static void io_worker_exit(struct io_worker *worker)
172{
173 struct io_wqe *wqe = worker->wqe;
Jens Axboe958234d2021-02-17 09:00:57 -0700174 struct io_wqe_acct *acct = io_wqe_get_acct(worker);
Jens Axboebf1daa42021-02-16 18:00:55 -0700175 unsigned flags;
Jens Axboe771b53d02019-10-22 10:25:58 -0600176
Jens Axboeeb2de942021-02-23 19:59:06 -0700177 if (refcount_dec_and_test(&worker->ref))
178 complete(&worker->ref_done);
179 wait_for_completion(&worker->ref_done);
Jens Axboe771b53d02019-10-22 10:25:58 -0600180
181 preempt_disable();
182 current->flags &= ~PF_IO_WORKER;
Jens Axboebf1daa42021-02-16 18:00:55 -0700183 flags = worker->flags;
184 worker->flags = 0;
185 if (flags & IO_WORKER_F_RUNNING)
Jens Axboec5def4a2019-11-07 11:41:16 -0700186 atomic_dec(&acct->nr_running);
Jens Axboe771b53d02019-10-22 10:25:58 -0600187 worker->flags = 0;
188 preempt_enable();
189
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200190 raw_spin_lock_irq(&wqe->lock);
Jens Axboebf1daa42021-02-16 18:00:55 -0700191 if (flags & IO_WORKER_F_FREE)
192 hlist_nulls_del_rcu(&worker->nulls_node);
Jens Axboee61df662019-11-13 13:54:49 -0700193 list_del_rcu(&worker->all_list);
Jens Axboec5def4a2019-11-07 11:41:16 -0700194 acct->nr_workers--;
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200195 raw_spin_unlock_irq(&wqe->lock);
Jens Axboe771b53d02019-10-22 10:25:58 -0600196
YueHaibing364b05f2019-11-02 15:55:01 +0800197 kfree_rcu(worker, rcu);
Jens Axboefb3a1f62021-02-26 09:47:20 -0700198 if (atomic_dec_and_test(&wqe->wq->worker_refs))
199 complete(&wqe->wq->worker_done);
Jens Axboe46fe18b2021-03-04 12:39:36 -0700200 do_exit(0);
Jens Axboe771b53d02019-10-22 10:25:58 -0600201}
202
Jens Axboec5def4a2019-11-07 11:41:16 -0700203static inline bool io_wqe_run_queue(struct io_wqe *wqe)
204 __must_hold(wqe->lock)
205{
Jens Axboe6206f0e2019-11-26 11:59:32 -0700206 if (!wq_list_empty(&wqe->work_list) &&
207 !(wqe->flags & IO_WQE_FLAG_STALLED))
Jens Axboec5def4a2019-11-07 11:41:16 -0700208 return true;
209 return false;
210}
211
212/*
213 * Check head of free list for an available worker. If one isn't available,
214 * caller must wake up the wq manager to create one.
215 */
216static bool io_wqe_activate_free_worker(struct io_wqe *wqe)
217 __must_hold(RCU)
218{
219 struct hlist_nulls_node *n;
220 struct io_worker *worker;
221
Jens Axboe021d1cd2019-11-14 08:00:41 -0700222 n = rcu_dereference(hlist_nulls_first_rcu(&wqe->free_list));
Jens Axboec5def4a2019-11-07 11:41:16 -0700223 if (is_a_nulls(n))
224 return false;
225
226 worker = hlist_nulls_entry(n, struct io_worker, nulls_node);
227 if (io_worker_get(worker)) {
Jens Axboe506d95f2019-12-07 21:03:59 -0700228 wake_up_process(worker->task);
Jens Axboec5def4a2019-11-07 11:41:16 -0700229 io_worker_release(worker);
230 return true;
231 }
232
233 return false;
234}
235
236/*
237 * We need a worker. If we find a free one, we're good. If not, and we're
238 * below the max number of workers, wake up the manager to create one.
239 */
240static void io_wqe_wake_worker(struct io_wqe *wqe, struct io_wqe_acct *acct)
241{
242 bool ret;
243
244 /*
245 * Most likely an attempt to queue unbounded work on an io_wq that
246 * wasn't setup with any unbounded workers.
247 */
248 WARN_ON_ONCE(!acct->max_workers);
249
250 rcu_read_lock();
251 ret = io_wqe_activate_free_worker(wqe);
252 rcu_read_unlock();
253
254 if (!ret && acct->nr_workers < acct->max_workers)
255 wake_up_process(wqe->wq->manager);
256}
257
Jens Axboe958234d2021-02-17 09:00:57 -0700258static void io_wqe_inc_running(struct io_worker *worker)
Jens Axboec5def4a2019-11-07 11:41:16 -0700259{
Jens Axboe958234d2021-02-17 09:00:57 -0700260 struct io_wqe_acct *acct = io_wqe_get_acct(worker);
Jens Axboec5def4a2019-11-07 11:41:16 -0700261
262 atomic_inc(&acct->nr_running);
263}
264
Jens Axboe958234d2021-02-17 09:00:57 -0700265static void io_wqe_dec_running(struct io_worker *worker)
Jens Axboec5def4a2019-11-07 11:41:16 -0700266 __must_hold(wqe->lock)
267{
Jens Axboe958234d2021-02-17 09:00:57 -0700268 struct io_wqe_acct *acct = io_wqe_get_acct(worker);
269 struct io_wqe *wqe = worker->wqe;
Jens Axboec5def4a2019-11-07 11:41:16 -0700270
271 if (atomic_dec_and_test(&acct->nr_running) && io_wqe_run_queue(wqe))
272 io_wqe_wake_worker(wqe, acct);
273}
274
Jens Axboe771b53d02019-10-22 10:25:58 -0600275/*
276 * Worker will start processing some work. Move it to the busy list, if
277 * it's currently on the freelist
278 */
279static void __io_worker_busy(struct io_wqe *wqe, struct io_worker *worker,
280 struct io_wq_work *work)
281 __must_hold(wqe->lock)
282{
Jens Axboec5def4a2019-11-07 11:41:16 -0700283 bool worker_bound, work_bound;
284
Jens Axboe771b53d02019-10-22 10:25:58 -0600285 if (worker->flags & IO_WORKER_F_FREE) {
286 worker->flags &= ~IO_WORKER_F_FREE;
287 hlist_nulls_del_init_rcu(&worker->nulls_node);
Jens Axboe771b53d02019-10-22 10:25:58 -0600288 }
Jens Axboec5def4a2019-11-07 11:41:16 -0700289
290 /*
291 * If worker is moving from bound to unbound (or vice versa), then
292 * ensure we update the running accounting.
293 */
Dan Carpenterb2e9c7d62019-11-19 09:22:16 +0300294 worker_bound = (worker->flags & IO_WORKER_F_BOUND) != 0;
295 work_bound = (work->flags & IO_WQ_WORK_UNBOUND) == 0;
296 if (worker_bound != work_bound) {
Jens Axboe958234d2021-02-17 09:00:57 -0700297 io_wqe_dec_running(worker);
Jens Axboec5def4a2019-11-07 11:41:16 -0700298 if (work_bound) {
299 worker->flags |= IO_WORKER_F_BOUND;
300 wqe->acct[IO_WQ_ACCT_UNBOUND].nr_workers--;
301 wqe->acct[IO_WQ_ACCT_BOUND].nr_workers++;
Jens Axboec5def4a2019-11-07 11:41:16 -0700302 } else {
303 worker->flags &= ~IO_WORKER_F_BOUND;
304 wqe->acct[IO_WQ_ACCT_UNBOUND].nr_workers++;
305 wqe->acct[IO_WQ_ACCT_BOUND].nr_workers--;
Jens Axboec5def4a2019-11-07 11:41:16 -0700306 }
Jens Axboe958234d2021-02-17 09:00:57 -0700307 io_wqe_inc_running(worker);
Jens Axboec5def4a2019-11-07 11:41:16 -0700308 }
Jens Axboe771b53d02019-10-22 10:25:58 -0600309}
310
311/*
312 * No work, worker going to sleep. Move to freelist, and unuse mm if we
313 * have one attached. Dropping the mm may potentially sleep, so we drop
314 * the lock in that case and return success. Since the caller has to
315 * retry the loop in that case (we changed task state), we don't regrab
316 * the lock if we return success.
317 */
Jens Axboec6d77d92021-02-15 13:26:34 -0700318static void __io_worker_idle(struct io_wqe *wqe, struct io_worker *worker)
Jens Axboe771b53d02019-10-22 10:25:58 -0600319 __must_hold(wqe->lock)
320{
321 if (!(worker->flags & IO_WORKER_F_FREE)) {
322 worker->flags |= IO_WORKER_F_FREE;
Jens Axboe021d1cd2019-11-14 08:00:41 -0700323 hlist_nulls_add_head_rcu(&worker->nulls_node, &wqe->free_list);
Jens Axboe771b53d02019-10-22 10:25:58 -0600324 }
Jens Axboe771b53d02019-10-22 10:25:58 -0600325}
326
Pavel Begunkov60cf46a2020-03-14 00:31:05 +0300327static inline unsigned int io_get_work_hash(struct io_wq_work *work)
328{
329 return work->flags >> IO_WQ_HASH_SHIFT;
330}
331
Jens Axboee9418942021-02-19 12:33:30 -0700332static void io_wait_on_hash(struct io_wqe *wqe, unsigned int hash)
333{
334 struct io_wq *wq = wqe->wq;
335
336 spin_lock(&wq->hash->wait.lock);
337 if (list_empty(&wqe->wait.entry)) {
338 __add_wait_queue(&wq->hash->wait, &wqe->wait);
339 if (!test_bit(hash, &wq->hash->map)) {
340 __set_current_state(TASK_RUNNING);
341 list_del_init(&wqe->wait.entry);
342 }
343 }
344 spin_unlock(&wq->hash->wait.lock);
345}
346
Pavel Begunkov60cf46a2020-03-14 00:31:05 +0300347static struct io_wq_work *io_get_next_work(struct io_wqe *wqe)
Jens Axboe771b53d02019-10-22 10:25:58 -0600348 __must_hold(wqe->lock)
349{
Jens Axboe6206f0e2019-11-26 11:59:32 -0700350 struct io_wq_work_node *node, *prev;
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300351 struct io_wq_work *work, *tail;
Jens Axboee9418942021-02-19 12:33:30 -0700352 unsigned int stall_hash = -1U;
Jens Axboe771b53d02019-10-22 10:25:58 -0600353
Jens Axboe6206f0e2019-11-26 11:59:32 -0700354 wq_list_for_each(node, prev, &wqe->work_list) {
Jens Axboee9418942021-02-19 12:33:30 -0700355 unsigned int hash;
356
Jens Axboe6206f0e2019-11-26 11:59:32 -0700357 work = container_of(node, struct io_wq_work, list);
358
Jens Axboe771b53d02019-10-22 10:25:58 -0600359 /* not hashed, can run anytime */
Pavel Begunkov8766dd52020-03-14 00:31:04 +0300360 if (!io_wq_is_hashed(work)) {
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300361 wq_list_del(&wqe->work_list, node, prev);
Jens Axboe771b53d02019-10-22 10:25:58 -0600362 return work;
363 }
364
Pavel Begunkov60cf46a2020-03-14 00:31:05 +0300365 hash = io_get_work_hash(work);
Jens Axboee9418942021-02-19 12:33:30 -0700366 /* all items with this hash lie in [work, tail] */
367 tail = wqe->hash_tail[hash];
368
369 /* hashed, can run if not already running */
370 if (!test_and_set_bit(hash, &wqe->wq->hash->map)) {
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300371 wqe->hash_tail[hash] = NULL;
372 wq_list_cut(&wqe->work_list, &tail->list, prev);
Jens Axboe771b53d02019-10-22 10:25:58 -0600373 return work;
374 }
Jens Axboee9418942021-02-19 12:33:30 -0700375 if (stall_hash == -1U)
376 stall_hash = hash;
377 /* fast forward to a next hash, for-each will fix up @prev */
378 node = &tail->list;
379 }
380
381 if (stall_hash != -1U) {
382 raw_spin_unlock(&wqe->lock);
383 io_wait_on_hash(wqe, stall_hash);
384 raw_spin_lock(&wqe->lock);
Jens Axboe771b53d02019-10-22 10:25:58 -0600385 }
386
387 return NULL;
388}
389
Jens Axboe3bfe6102021-02-16 14:15:30 -0700390static void io_flush_signals(void)
Jens Axboecccf0ee2020-01-27 16:34:48 -0700391{
Jens Axboe3bfe6102021-02-16 14:15:30 -0700392 if (unlikely(test_tsk_thread_flag(current, TIF_NOTIFY_SIGNAL))) {
393 if (current->task_works)
394 task_work_run();
395 clear_tsk_thread_flag(current, TIF_NOTIFY_SIGNAL);
Jens Axboecccf0ee2020-01-27 16:34:48 -0700396 }
Pavel Begunkovdc026a72020-03-04 16:14:09 +0300397}
398
399static void io_assign_current_work(struct io_worker *worker,
400 struct io_wq_work *work)
401{
Pavel Begunkovd78298e2020-03-14 00:31:03 +0300402 if (work) {
Jens Axboe3bfe6102021-02-16 14:15:30 -0700403 io_flush_signals();
Pavel Begunkovd78298e2020-03-14 00:31:03 +0300404 cond_resched();
405 }
Pavel Begunkovdc026a72020-03-04 16:14:09 +0300406
407 spin_lock_irq(&worker->lock);
408 worker->cur_work = work;
409 spin_unlock_irq(&worker->lock);
410}
411
Pavel Begunkov60cf46a2020-03-14 00:31:05 +0300412static void io_wqe_enqueue(struct io_wqe *wqe, struct io_wq_work *work);
413
Jens Axboe771b53d02019-10-22 10:25:58 -0600414static void io_worker_handle_work(struct io_worker *worker)
415 __releases(wqe->lock)
416{
Jens Axboe771b53d02019-10-22 10:25:58 -0600417 struct io_wqe *wqe = worker->wqe;
418 struct io_wq *wq = wqe->wq;
419
420 do {
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300421 struct io_wq_work *work;
Pavel Begunkovf462fd32020-03-04 16:14:11 +0300422get_next:
Jens Axboe771b53d02019-10-22 10:25:58 -0600423 /*
Jens Axboe771b53d02019-10-22 10:25:58 -0600424 * If we got some work, mark us as busy. If we didn't, but
425 * the list isn't empty, it means we stalled on hashed work.
426 * Mark us stalled so we don't keep looking for work when we
427 * can't make progress, any work completion or insertion will
428 * clear the stalled flag.
429 */
Pavel Begunkov60cf46a2020-03-14 00:31:05 +0300430 work = io_get_next_work(wqe);
Jens Axboe771b53d02019-10-22 10:25:58 -0600431 if (work)
432 __io_worker_busy(wqe, worker, work);
Jens Axboe6206f0e2019-11-26 11:59:32 -0700433 else if (!wq_list_empty(&wqe->work_list))
Jens Axboe771b53d02019-10-22 10:25:58 -0600434 wqe->flags |= IO_WQE_FLAG_STALLED;
435
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200436 raw_spin_unlock_irq(&wqe->lock);
Jens Axboe771b53d02019-10-22 10:25:58 -0600437 if (!work)
438 break;
Pavel Begunkov58e393192020-03-04 16:14:10 +0300439 io_assign_current_work(worker, work);
Jens Axboee9418942021-02-19 12:33:30 -0700440 __set_current_state(TASK_RUNNING);
Jens Axboe36c2f922019-11-13 09:43:34 -0700441
Pavel Begunkovdc026a72020-03-04 16:14:09 +0300442 /* handle a whole dependent link */
443 do {
Pavel Begunkov5280f7e2021-02-04 13:52:08 +0000444 struct io_wq_work *next_hashed, *linked;
Pavel Begunkovb089ed392020-07-25 14:42:00 +0300445 unsigned int hash = io_get_work_hash(work);
Hillf Dantonfd1c4bc2019-12-24 09:14:29 -0700446
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300447 next_hashed = wq_next_work(work);
Pavel Begunkov5280f7e2021-02-04 13:52:08 +0000448 wq->do_work(work);
449 io_assign_current_work(worker, NULL);
Jens Axboe36c2f922019-11-13 09:43:34 -0700450
Pavel Begunkov5280f7e2021-02-04 13:52:08 +0000451 linked = wq->free_work(work);
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300452 work = next_hashed;
453 if (!work && linked && !io_wq_is_hashed(linked)) {
454 work = linked;
455 linked = NULL;
456 }
457 io_assign_current_work(worker, work);
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300458 if (linked)
459 io_wqe_enqueue(wqe, linked);
460
461 if (hash != -1U && !next_hashed) {
Jens Axboee9418942021-02-19 12:33:30 -0700462 clear_bit(hash, &wq->hash->map);
463 if (wq_has_sleeper(&wq->hash->wait))
464 wake_up(&wq->hash->wait);
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200465 raw_spin_lock_irq(&wqe->lock);
Pavel Begunkovdc026a72020-03-04 16:14:09 +0300466 wqe->flags &= ~IO_WQE_FLAG_STALLED;
Pavel Begunkovf462fd32020-03-04 16:14:11 +0300467 /* skip unnecessary unlock-lock wqe->lock */
468 if (!work)
469 goto get_next;
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200470 raw_spin_unlock_irq(&wqe->lock);
Pavel Begunkovdc026a72020-03-04 16:14:09 +0300471 }
Pavel Begunkov58e393192020-03-04 16:14:10 +0300472 } while (work);
Jens Axboe36c2f922019-11-13 09:43:34 -0700473
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200474 raw_spin_lock_irq(&wqe->lock);
Jens Axboe771b53d02019-10-22 10:25:58 -0600475 } while (1);
476}
477
Jens Axboe771b53d02019-10-22 10:25:58 -0600478static int io_wqe_worker(void *data)
479{
480 struct io_worker *worker = data;
481 struct io_wqe *wqe = worker->wqe;
482 struct io_wq *wq = wqe->wq;
Jens Axboe46fe18b2021-03-04 12:39:36 -0700483 char buf[TASK_COMM_LEN];
Jens Axboe771b53d02019-10-22 10:25:58 -0600484
Jens Axboe46fe18b2021-03-04 12:39:36 -0700485 worker->flags |= (IO_WORKER_F_UP | IO_WORKER_F_RUNNING);
486 io_wqe_inc_running(worker);
487
488 sprintf(buf, "iou-wrk-%d", wq->task_pid);
489 set_task_comm(current, buf);
Jens Axboe771b53d02019-10-22 10:25:58 -0600490
491 while (!test_bit(IO_WQ_BIT_EXIT, &wq->state)) {
Jens Axboe506d95f2019-12-07 21:03:59 -0700492 set_current_state(TASK_INTERRUPTIBLE);
Jens Axboee995d512019-12-07 21:06:46 -0700493loop:
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200494 raw_spin_lock_irq(&wqe->lock);
Jens Axboe771b53d02019-10-22 10:25:58 -0600495 if (io_wqe_run_queue(wqe)) {
Jens Axboe771b53d02019-10-22 10:25:58 -0600496 io_worker_handle_work(worker);
Jens Axboee995d512019-12-07 21:06:46 -0700497 goto loop;
Jens Axboe771b53d02019-10-22 10:25:58 -0600498 }
Jens Axboec6d77d92021-02-15 13:26:34 -0700499 __io_worker_idle(wqe, worker);
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200500 raw_spin_unlock_irq(&wqe->lock);
Jens Axboe3bfe6102021-02-16 14:15:30 -0700501 io_flush_signals();
Jens Axboe771b53d02019-10-22 10:25:58 -0600502 if (schedule_timeout(WORKER_IDLE_TIMEOUT))
503 continue;
Jens Axboe3bfe6102021-02-16 14:15:30 -0700504 if (fatal_signal_pending(current))
505 break;
Jens Axboe771b53d02019-10-22 10:25:58 -0600506 /* timed out, exit unless we're the fixed worker */
507 if (test_bit(IO_WQ_BIT_EXIT, &wq->state) ||
508 !(worker->flags & IO_WORKER_F_FIXED))
509 break;
510 }
511
Jens Axboe771b53d02019-10-22 10:25:58 -0600512 if (test_bit(IO_WQ_BIT_EXIT, &wq->state)) {
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200513 raw_spin_lock_irq(&wqe->lock);
Jens Axboe6206f0e2019-11-26 11:59:32 -0700514 if (!wq_list_empty(&wqe->work_list))
Jens Axboe771b53d02019-10-22 10:25:58 -0600515 io_worker_handle_work(worker);
516 else
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200517 raw_spin_unlock_irq(&wqe->lock);
Jens Axboe771b53d02019-10-22 10:25:58 -0600518 }
519
520 io_worker_exit(worker);
521 return 0;
522}
523
524/*
Jens Axboe771b53d02019-10-22 10:25:58 -0600525 * Called when a worker is scheduled in. Mark us as currently running.
526 */
527void io_wq_worker_running(struct task_struct *tsk)
528{
Jens Axboe3bfe6102021-02-16 14:15:30 -0700529 struct io_worker *worker = tsk->pf_io_worker;
Jens Axboe771b53d02019-10-22 10:25:58 -0600530
Jens Axboe3bfe6102021-02-16 14:15:30 -0700531 if (!worker)
532 return;
Jens Axboe771b53d02019-10-22 10:25:58 -0600533 if (!(worker->flags & IO_WORKER_F_UP))
534 return;
535 if (worker->flags & IO_WORKER_F_RUNNING)
536 return;
537 worker->flags |= IO_WORKER_F_RUNNING;
Jens Axboe958234d2021-02-17 09:00:57 -0700538 io_wqe_inc_running(worker);
Jens Axboe771b53d02019-10-22 10:25:58 -0600539}
540
541/*
542 * Called when worker is going to sleep. If there are no workers currently
543 * running and we have work pending, wake up a free one or have the manager
544 * set one up.
545 */
546void io_wq_worker_sleeping(struct task_struct *tsk)
547{
Jens Axboe3bfe6102021-02-16 14:15:30 -0700548 struct io_worker *worker = tsk->pf_io_worker;
Jens Axboe771b53d02019-10-22 10:25:58 -0600549
Jens Axboe3bfe6102021-02-16 14:15:30 -0700550 if (!worker)
551 return;
Jens Axboe771b53d02019-10-22 10:25:58 -0600552 if (!(worker->flags & IO_WORKER_F_UP))
553 return;
554 if (!(worker->flags & IO_WORKER_F_RUNNING))
555 return;
556
557 worker->flags &= ~IO_WORKER_F_RUNNING;
558
Jens Axboe3bfe6102021-02-16 14:15:30 -0700559 raw_spin_lock_irq(&worker->wqe->lock);
Jens Axboe958234d2021-02-17 09:00:57 -0700560 io_wqe_dec_running(worker);
Jens Axboe3bfe6102021-02-16 14:15:30 -0700561 raw_spin_unlock_irq(&worker->wqe->lock);
Jens Axboe771b53d02019-10-22 10:25:58 -0600562}
563
Jens Axboe3bfe6102021-02-16 14:15:30 -0700564static bool create_io_worker(struct io_wq *wq, struct io_wqe *wqe, int index)
565{
Jens Axboe46fe18b2021-03-04 12:39:36 -0700566 struct io_wqe_acct *acct = &wqe->acct[index];
Jens Axboe3bfe6102021-02-16 14:15:30 -0700567 struct io_worker *worker;
Jens Axboe46fe18b2021-03-04 12:39:36 -0700568 struct task_struct *tsk;
Jens Axboe3bfe6102021-02-16 14:15:30 -0700569
Jens Axboe8b3e78b2021-02-23 15:34:06 -0700570 __set_current_state(TASK_RUNNING);
571
Jens Axboe3bfe6102021-02-16 14:15:30 -0700572 worker = kzalloc_node(sizeof(*worker), GFP_KERNEL, wqe->node);
573 if (!worker)
574 return false;
575
576 refcount_set(&worker->ref, 1);
577 worker->nulls_node.pprev = NULL;
578 worker->wqe = wqe;
579 spin_lock_init(&worker->lock);
Jens Axboeeb2de942021-02-23 19:59:06 -0700580 init_completion(&worker->ref_done);
Jens Axboe3bfe6102021-02-16 14:15:30 -0700581
Jens Axboefb3a1f62021-02-26 09:47:20 -0700582 atomic_inc(&wq->worker_refs);
Jens Axboe8b3e78b2021-02-23 15:34:06 -0700583
Jens Axboe46fe18b2021-03-04 12:39:36 -0700584 tsk = create_io_thread(io_wqe_worker, worker, wqe->node);
585 if (IS_ERR(tsk)) {
Jens Axboefb3a1f62021-02-26 09:47:20 -0700586 if (atomic_dec_and_test(&wq->worker_refs))
587 complete(&wq->worker_done);
Jens Axboe3bfe6102021-02-16 14:15:30 -0700588 kfree(worker);
589 return false;
590 }
Jens Axboe46fe18b2021-03-04 12:39:36 -0700591
592 tsk->pf_io_worker = worker;
593 worker->task = tsk;
594 set_cpus_allowed_ptr(tsk, cpumask_of_node(wqe->node));
595 tsk->flags |= PF_NOFREEZE | PF_NO_SETAFFINITY;
596
597 raw_spin_lock_irq(&wqe->lock);
598 hlist_nulls_add_head_rcu(&worker->nulls_node, &wqe->free_list);
599 list_add_tail_rcu(&worker->all_list, &wqe->all_list);
600 worker->flags |= IO_WORKER_F_FREE;
601 if (index == IO_WQ_ACCT_BOUND)
602 worker->flags |= IO_WORKER_F_BOUND;
603 if (!acct->nr_workers && (worker->flags & IO_WORKER_F_BOUND))
604 worker->flags |= IO_WORKER_F_FIXED;
605 acct->nr_workers++;
606 raw_spin_unlock_irq(&wqe->lock);
607 wake_up_new_task(tsk);
Jens Axboeb60fda62019-11-19 08:37:07 -0700608 return true;
Jens Axboe771b53d02019-10-22 10:25:58 -0600609}
610
Jens Axboec5def4a2019-11-07 11:41:16 -0700611static inline bool io_wqe_need_worker(struct io_wqe *wqe, int index)
Jens Axboe771b53d02019-10-22 10:25:58 -0600612 __must_hold(wqe->lock)
613{
Jens Axboec5def4a2019-11-07 11:41:16 -0700614 struct io_wqe_acct *acct = &wqe->acct[index];
Jens Axboe771b53d02019-10-22 10:25:58 -0600615
Jens Axboe613eeb62021-02-26 09:52:02 -0700616 if (acct->nr_workers && test_bit(IO_WQ_BIT_EXIT, &wqe->wq->state))
617 return false;
Jens Axboec5def4a2019-11-07 11:41:16 -0700618 /* if we have available workers or no work, no need */
Jens Axboe021d1cd2019-11-14 08:00:41 -0700619 if (!hlist_nulls_empty(&wqe->free_list) || !io_wqe_run_queue(wqe))
Jens Axboec5def4a2019-11-07 11:41:16 -0700620 return false;
621 return acct->nr_workers < acct->max_workers;
Jens Axboe771b53d02019-10-22 10:25:58 -0600622}
623
Hillf Dantonc4068bf2020-09-26 21:26:55 +0800624/*
625 * Iterate the passed in list and call the specific function for each
626 * worker that isn't exiting
627 */
628static bool io_wq_for_each_worker(struct io_wqe *wqe,
629 bool (*func)(struct io_worker *, void *),
630 void *data)
631{
632 struct io_worker *worker;
633 bool ret = false;
634
635 list_for_each_entry_rcu(worker, &wqe->all_list, all_list) {
636 if (io_worker_get(worker)) {
637 /* no task if node is/was offline */
638 if (worker->task)
639 ret = func(worker, data);
640 io_worker_release(worker);
641 if (ret)
642 break;
643 }
644 }
645
646 return ret;
647}
648
649static bool io_wq_worker_wake(struct io_worker *worker, void *data)
650{
Jens Axboe46fe18b2021-03-04 12:39:36 -0700651 set_notify_signal(worker->task);
Hillf Dantonc4068bf2020-09-26 21:26:55 +0800652 wake_up_process(worker->task);
653 return false;
654}
655
Jens Axboe8b3e78b2021-02-23 15:34:06 -0700656static void io_wq_check_workers(struct io_wq *wq)
657{
658 int node;
659
660 for_each_node(node) {
661 struct io_wqe *wqe = wq->wqes[node];
662 bool fork_worker[2] = { false, false };
663
664 if (!node_online(node))
665 continue;
666
667 raw_spin_lock_irq(&wqe->lock);
668 if (io_wqe_need_worker(wqe, IO_WQ_ACCT_BOUND))
669 fork_worker[IO_WQ_ACCT_BOUND] = true;
670 if (io_wqe_need_worker(wqe, IO_WQ_ACCT_UNBOUND))
671 fork_worker[IO_WQ_ACCT_UNBOUND] = true;
672 raw_spin_unlock_irq(&wqe->lock);
673 if (fork_worker[IO_WQ_ACCT_BOUND])
674 create_io_worker(wq, wqe, IO_WQ_ACCT_BOUND);
675 if (fork_worker[IO_WQ_ACCT_UNBOUND])
676 create_io_worker(wq, wqe, IO_WQ_ACCT_UNBOUND);
677 }
678}
679
Jens Axboef0127252021-03-03 15:47:04 -0700680static bool io_wq_work_match_all(struct io_wq_work *work, void *data)
681{
682 return true;
683}
684
685static void io_wq_cancel_pending(struct io_wq *wq)
686{
687 struct io_cb_cancel_data match = {
688 .fn = io_wq_work_match_all,
689 .cancel_all = true,
690 };
691 int node;
692
693 for_each_node(node)
694 io_wqe_cancel_pending_work(wq->wqes[node], &match);
695}
696
Jens Axboe771b53d02019-10-22 10:25:58 -0600697/*
698 * Manager thread. Tasked with creating new workers, if we need them.
699 */
700static int io_wq_manager(void *data)
701{
702 struct io_wq *wq = data;
Jens Axboe3bfe6102021-02-16 14:15:30 -0700703 char buf[TASK_COMM_LEN];
Jens Axboefb3a1f62021-02-26 09:47:20 -0700704 int node;
Jens Axboeb60fda62019-11-19 08:37:07 -0700705
Jens Axboe3bfe6102021-02-16 14:15:30 -0700706 sprintf(buf, "iou-mgr-%d", wq->task_pid);
707 set_task_comm(current, buf);
Jens Axboe771b53d02019-10-22 10:25:58 -0600708
Jens Axboe8b3e78b2021-02-23 15:34:06 -0700709 do {
Jens Axboe771b53d02019-10-22 10:25:58 -0600710 set_current_state(TASK_INTERRUPTIBLE);
Jens Axboe8b3e78b2021-02-23 15:34:06 -0700711 io_wq_check_workers(wq);
Jens Axboe771b53d02019-10-22 10:25:58 -0600712 schedule_timeout(HZ);
Jens Axboee4b4a132021-03-01 18:36:25 -0700713 try_to_freeze();
Jens Axboe3bfe6102021-02-16 14:15:30 -0700714 if (fatal_signal_pending(current))
715 set_bit(IO_WQ_BIT_EXIT, &wq->state);
Jens Axboe8b3e78b2021-02-23 15:34:06 -0700716 } while (!test_bit(IO_WQ_BIT_EXIT, &wq->state));
717
718 io_wq_check_workers(wq);
Jens Axboefb3a1f62021-02-26 09:47:20 -0700719
720 rcu_read_lock();
721 for_each_node(node)
722 io_wq_for_each_worker(wq->wqes[node], io_wq_worker_wake, NULL);
723 rcu_read_unlock();
724
Jens Axboe886d0132021-03-05 12:59:30 -0700725 if (atomic_dec_and_test(&wq->worker_refs))
726 complete(&wq->worker_done);
727 wait_for_completion(&wq->worker_done);
Jens Axboef0127252021-03-03 15:47:04 -0700728
Jens Axboe09ca6c402021-03-05 08:14:08 -0700729 spin_lock_irq(&wq->hash->wait.lock);
730 for_each_node(node)
731 list_del_init(&wq->wqes[node]->wait.entry);
732 spin_unlock_irq(&wq->hash->wait.lock);
733
Jens Axboef0127252021-03-03 15:47:04 -0700734 io_wq_cancel_pending(wq);
Jens Axboed364d9e2021-02-26 09:56:32 -0700735 complete(&wq->exited);
Jens Axboe3bfe6102021-02-16 14:15:30 -0700736 do_exit(0);
Jens Axboe771b53d02019-10-22 10:25:58 -0600737}
738
Pavel Begunkove9fd9392020-03-04 16:14:12 +0300739static void io_run_cancel(struct io_wq_work *work, struct io_wqe *wqe)
Pavel Begunkovfc04c392020-03-01 19:18:19 +0300740{
Pavel Begunkove9fd9392020-03-04 16:14:12 +0300741 struct io_wq *wq = wqe->wq;
742
Pavel Begunkovfc04c392020-03-01 19:18:19 +0300743 do {
Pavel Begunkovfc04c392020-03-01 19:18:19 +0300744 work->flags |= IO_WQ_WORK_CANCEL;
Pavel Begunkov5280f7e2021-02-04 13:52:08 +0000745 wq->do_work(work);
746 work = wq->free_work(work);
Pavel Begunkovfc04c392020-03-01 19:18:19 +0300747 } while (work);
748}
749
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300750static void io_wqe_insert_work(struct io_wqe *wqe, struct io_wq_work *work)
751{
752 unsigned int hash;
753 struct io_wq_work *tail;
754
755 if (!io_wq_is_hashed(work)) {
756append:
757 wq_list_add_tail(&work->list, &wqe->work_list);
758 return;
759 }
760
761 hash = io_get_work_hash(work);
762 tail = wqe->hash_tail[hash];
763 wqe->hash_tail[hash] = work;
764 if (!tail)
765 goto append;
766
767 wq_list_add_after(&work->list, &tail->list, &wqe->work_list);
768}
769
Jens Axboe4fb6ac32021-02-25 10:17:09 -0700770static int io_wq_fork_manager(struct io_wq *wq)
771{
Jens Axboe46fe18b2021-03-04 12:39:36 -0700772 struct task_struct *tsk;
Jens Axboe4fb6ac32021-02-25 10:17:09 -0700773
774 if (wq->manager)
775 return 0;
776
Jens Axboe886d0132021-03-05 12:59:30 -0700777 init_completion(&wq->worker_done);
778 atomic_set(&wq->worker_refs, 1);
Jens Axboe46fe18b2021-03-04 12:39:36 -0700779 tsk = create_io_thread(io_wq_manager, wq, NUMA_NO_NODE);
780 if (!IS_ERR(tsk)) {
781 wq->manager = get_task_struct(tsk);
782 wake_up_new_task(tsk);
Jens Axboe4fb6ac32021-02-25 10:17:09 -0700783 return 0;
784 }
785
Jens Axboe886d0132021-03-05 12:59:30 -0700786 if (atomic_dec_and_test(&wq->worker_refs))
787 complete(&wq->worker_done);
788
Jens Axboe46fe18b2021-03-04 12:39:36 -0700789 return PTR_ERR(tsk);
Jens Axboe4fb6ac32021-02-25 10:17:09 -0700790}
791
Jens Axboe771b53d02019-10-22 10:25:58 -0600792static void io_wqe_enqueue(struct io_wqe *wqe, struct io_wq_work *work)
793{
Jens Axboec5def4a2019-11-07 11:41:16 -0700794 struct io_wqe_acct *acct = io_work_get_acct(wqe, work);
Jens Axboe895e2ca2019-12-17 08:46:33 -0700795 int work_flags;
Jens Axboe771b53d02019-10-22 10:25:58 -0600796 unsigned long flags;
797
Jens Axboe4fb6ac32021-02-25 10:17:09 -0700798 /* Can only happen if manager creation fails after exec */
Jens Axboef0127252021-03-03 15:47:04 -0700799 if (io_wq_fork_manager(wqe->wq) ||
800 test_bit(IO_WQ_BIT_EXIT, &wqe->wq->state)) {
Jens Axboe4fb6ac32021-02-25 10:17:09 -0700801 work->flags |= IO_WQ_WORK_CANCEL;
802 wqe->wq->do_work(work);
803 return;
804 }
805
Jens Axboe895e2ca2019-12-17 08:46:33 -0700806 work_flags = work->flags;
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200807 raw_spin_lock_irqsave(&wqe->lock, flags);
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300808 io_wqe_insert_work(wqe, work);
Jens Axboe771b53d02019-10-22 10:25:58 -0600809 wqe->flags &= ~IO_WQE_FLAG_STALLED;
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200810 raw_spin_unlock_irqrestore(&wqe->lock, flags);
Jens Axboe771b53d02019-10-22 10:25:58 -0600811
Jens Axboe895e2ca2019-12-17 08:46:33 -0700812 if ((work_flags & IO_WQ_WORK_CONCURRENT) ||
813 !atomic_read(&acct->nr_running))
Jens Axboec5def4a2019-11-07 11:41:16 -0700814 io_wqe_wake_worker(wqe, acct);
Jens Axboe771b53d02019-10-22 10:25:58 -0600815}
816
817void io_wq_enqueue(struct io_wq *wq, struct io_wq_work *work)
818{
819 struct io_wqe *wqe = wq->wqes[numa_node_id()];
820
821 io_wqe_enqueue(wqe, work);
822}
823
824/*
Pavel Begunkov8766dd52020-03-14 00:31:04 +0300825 * Work items that hash to the same value will not be done in parallel.
826 * Used to limit concurrent writes, generally hashed by inode.
Jens Axboe771b53d02019-10-22 10:25:58 -0600827 */
Pavel Begunkov8766dd52020-03-14 00:31:04 +0300828void io_wq_hash_work(struct io_wq_work *work, void *val)
Jens Axboe771b53d02019-10-22 10:25:58 -0600829{
Pavel Begunkov8766dd52020-03-14 00:31:04 +0300830 unsigned int bit;
Jens Axboe771b53d02019-10-22 10:25:58 -0600831
832 bit = hash_ptr(val, IO_WQ_HASH_ORDER);
833 work->flags |= (IO_WQ_WORK_HASHED | (bit << IO_WQ_HASH_SHIFT));
Jens Axboe771b53d02019-10-22 10:25:58 -0600834}
835
Pavel Begunkov2293b412020-03-07 01:15:39 +0300836static bool io_wq_worker_cancel(struct io_worker *worker, void *data)
Jens Axboe62755e32019-10-28 21:49:21 -0600837{
Pavel Begunkov2293b412020-03-07 01:15:39 +0300838 struct io_cb_cancel_data *match = data;
Jens Axboe6f726532019-11-05 13:51:51 -0700839 unsigned long flags;
Jens Axboe62755e32019-10-28 21:49:21 -0600840
841 /*
842 * Hold the lock to avoid ->cur_work going out of scope, caller
Jens Axboe36c2f922019-11-13 09:43:34 -0700843 * may dereference the passed in work.
Jens Axboe62755e32019-10-28 21:49:21 -0600844 */
Jens Axboe36c2f922019-11-13 09:43:34 -0700845 spin_lock_irqsave(&worker->lock, flags);
Jens Axboe62755e32019-10-28 21:49:21 -0600846 if (worker->cur_work &&
Pavel Begunkov2293b412020-03-07 01:15:39 +0300847 match->fn(worker->cur_work, match->data)) {
Jens Axboe3bfe6102021-02-16 14:15:30 -0700848 set_notify_signal(worker->task);
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300849 match->nr_running++;
Jens Axboe771b53d02019-10-22 10:25:58 -0600850 }
Jens Axboe36c2f922019-11-13 09:43:34 -0700851 spin_unlock_irqrestore(&worker->lock, flags);
Jens Axboe771b53d02019-10-22 10:25:58 -0600852
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300853 return match->nr_running && !match->cancel_all;
Jens Axboe771b53d02019-10-22 10:25:58 -0600854}
855
Pavel Begunkov204361a2020-08-23 20:33:10 +0300856static inline void io_wqe_remove_pending(struct io_wqe *wqe,
857 struct io_wq_work *work,
858 struct io_wq_work_node *prev)
859{
860 unsigned int hash = io_get_work_hash(work);
861 struct io_wq_work *prev_work = NULL;
862
863 if (io_wq_is_hashed(work) && work == wqe->hash_tail[hash]) {
864 if (prev)
865 prev_work = container_of(prev, struct io_wq_work, list);
866 if (prev_work && io_get_work_hash(prev_work) == hash)
867 wqe->hash_tail[hash] = prev_work;
868 else
869 wqe->hash_tail[hash] = NULL;
870 }
871 wq_list_del(&wqe->work_list, &work->list, prev);
872}
873
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300874static void io_wqe_cancel_pending_work(struct io_wqe *wqe,
Pavel Begunkovf4c26652020-06-15 10:24:02 +0300875 struct io_cb_cancel_data *match)
Jens Axboe771b53d02019-10-22 10:25:58 -0600876{
Jens Axboe6206f0e2019-11-26 11:59:32 -0700877 struct io_wq_work_node *node, *prev;
Jens Axboe771b53d02019-10-22 10:25:58 -0600878 struct io_wq_work *work;
Jens Axboe6f726532019-11-05 13:51:51 -0700879 unsigned long flags;
Jens Axboe771b53d02019-10-22 10:25:58 -0600880
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300881retry:
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200882 raw_spin_lock_irqsave(&wqe->lock, flags);
Jens Axboe6206f0e2019-11-26 11:59:32 -0700883 wq_list_for_each(node, prev, &wqe->work_list) {
884 work = container_of(node, struct io_wq_work, list);
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300885 if (!match->fn(work, match->data))
886 continue;
Pavel Begunkov204361a2020-08-23 20:33:10 +0300887 io_wqe_remove_pending(wqe, work, prev);
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200888 raw_spin_unlock_irqrestore(&wqe->lock, flags);
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300889 io_run_cancel(work, wqe);
890 match->nr_pending++;
891 if (!match->cancel_all)
892 return;
893
894 /* not safe to continue after unlock */
895 goto retry;
Jens Axboe771b53d02019-10-22 10:25:58 -0600896 }
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200897 raw_spin_unlock_irqrestore(&wqe->lock, flags);
Pavel Begunkovf4c26652020-06-15 10:24:02 +0300898}
Jens Axboe771b53d02019-10-22 10:25:58 -0600899
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300900static void io_wqe_cancel_running_work(struct io_wqe *wqe,
Pavel Begunkovf4c26652020-06-15 10:24:02 +0300901 struct io_cb_cancel_data *match)
902{
Jens Axboe771b53d02019-10-22 10:25:58 -0600903 rcu_read_lock();
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300904 io_wq_for_each_worker(wqe, io_wq_worker_cancel, match);
Jens Axboe771b53d02019-10-22 10:25:58 -0600905 rcu_read_unlock();
Jens Axboe771b53d02019-10-22 10:25:58 -0600906}
907
Pavel Begunkov2293b412020-03-07 01:15:39 +0300908enum io_wq_cancel io_wq_cancel_cb(struct io_wq *wq, work_cancel_fn *cancel,
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300909 void *data, bool cancel_all)
Pavel Begunkov2293b412020-03-07 01:15:39 +0300910{
911 struct io_cb_cancel_data match = {
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300912 .fn = cancel,
913 .data = data,
914 .cancel_all = cancel_all,
Pavel Begunkov2293b412020-03-07 01:15:39 +0300915 };
Pavel Begunkov2293b412020-03-07 01:15:39 +0300916 int node;
917
Pavel Begunkovf4c26652020-06-15 10:24:02 +0300918 /*
919 * First check pending list, if we're lucky we can just remove it
920 * from there. CANCEL_OK means that the work is returned as-new,
921 * no completion will be posted for it.
922 */
Pavel Begunkov2293b412020-03-07 01:15:39 +0300923 for_each_node(node) {
924 struct io_wqe *wqe = wq->wqes[node];
925
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300926 io_wqe_cancel_pending_work(wqe, &match);
927 if (match.nr_pending && !match.cancel_all)
Pavel Begunkovf4c26652020-06-15 10:24:02 +0300928 return IO_WQ_CANCEL_OK;
Pavel Begunkov2293b412020-03-07 01:15:39 +0300929 }
930
Pavel Begunkovf4c26652020-06-15 10:24:02 +0300931 /*
932 * Now check if a free (going busy) or busy worker has the work
933 * currently running. If we find it there, we'll return CANCEL_RUNNING
934 * as an indication that we attempt to signal cancellation. The
935 * completion will run normally in this case.
936 */
937 for_each_node(node) {
938 struct io_wqe *wqe = wq->wqes[node];
939
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300940 io_wqe_cancel_running_work(wqe, &match);
941 if (match.nr_running && !match.cancel_all)
Pavel Begunkovf4c26652020-06-15 10:24:02 +0300942 return IO_WQ_CANCEL_RUNNING;
943 }
944
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300945 if (match.nr_running)
946 return IO_WQ_CANCEL_RUNNING;
947 if (match.nr_pending)
948 return IO_WQ_CANCEL_OK;
Pavel Begunkovf4c26652020-06-15 10:24:02 +0300949 return IO_WQ_CANCEL_NOTFOUND;
Pavel Begunkov2293b412020-03-07 01:15:39 +0300950}
951
Jens Axboee9418942021-02-19 12:33:30 -0700952static int io_wqe_hash_wake(struct wait_queue_entry *wait, unsigned mode,
953 int sync, void *key)
954{
955 struct io_wqe *wqe = container_of(wait, struct io_wqe, wait);
956 int ret;
957
958 list_del_init(&wait->entry);
959
960 rcu_read_lock();
961 ret = io_wqe_activate_free_worker(wqe);
962 rcu_read_unlock();
963
964 if (!ret)
965 wake_up_process(wqe->wq->manager);
966
967 return 1;
968}
969
Jens Axboe576a3472019-11-25 08:49:20 -0700970struct io_wq *io_wq_create(unsigned bounded, struct io_wq_data *data)
Jens Axboe771b53d02019-10-22 10:25:58 -0600971{
Jann Horn3fc50ab2019-11-26 19:10:20 +0100972 int ret = -ENOMEM, node;
Jens Axboe771b53d02019-10-22 10:25:58 -0600973 struct io_wq *wq;
974
Pavel Begunkovf5fa38c2020-06-08 21:08:20 +0300975 if (WARN_ON_ONCE(!data->free_work || !data->do_work))
Pavel Begunkove9fd9392020-03-04 16:14:12 +0300976 return ERR_PTR(-EINVAL);
977
Jann Hornad6e0052019-11-26 17:39:45 +0100978 wq = kzalloc(sizeof(*wq), GFP_KERNEL);
Jens Axboe771b53d02019-10-22 10:25:58 -0600979 if (!wq)
980 return ERR_PTR(-ENOMEM);
981
Jann Horn3fc50ab2019-11-26 19:10:20 +0100982 wq->wqes = kcalloc(nr_node_ids, sizeof(struct io_wqe *), GFP_KERNEL);
Jens Axboe43c01fb2020-10-22 09:02:50 -0600983 if (!wq->wqes)
984 goto err_wq;
985
986 ret = cpuhp_state_add_instance_nocalls(io_wq_online, &wq->cpuhp_node);
987 if (ret)
988 goto err_wqes;
Jens Axboe771b53d02019-10-22 10:25:58 -0600989
Jens Axboee9418942021-02-19 12:33:30 -0700990 refcount_inc(&data->hash->refs);
991 wq->hash = data->hash;
Pavel Begunkove9fd9392020-03-04 16:14:12 +0300992 wq->free_work = data->free_work;
Pavel Begunkovf5fa38c2020-06-08 21:08:20 +0300993 wq->do_work = data->do_work;
Jens Axboe7d723062019-11-12 22:31:31 -0700994
Jens Axboe43c01fb2020-10-22 09:02:50 -0600995 ret = -ENOMEM;
Jann Horn3fc50ab2019-11-26 19:10:20 +0100996 for_each_node(node) {
Jens Axboe771b53d02019-10-22 10:25:58 -0600997 struct io_wqe *wqe;
Jens Axboe75634392020-02-11 06:30:06 -0700998 int alloc_node = node;
Jens Axboe771b53d02019-10-22 10:25:58 -0600999
Jens Axboe75634392020-02-11 06:30:06 -07001000 if (!node_online(alloc_node))
1001 alloc_node = NUMA_NO_NODE;
1002 wqe = kzalloc_node(sizeof(struct io_wqe), GFP_KERNEL, alloc_node);
Jens Axboe771b53d02019-10-22 10:25:58 -06001003 if (!wqe)
Jann Horn3fc50ab2019-11-26 19:10:20 +01001004 goto err;
1005 wq->wqes[node] = wqe;
Jens Axboe75634392020-02-11 06:30:06 -07001006 wqe->node = alloc_node;
Jens Axboec5def4a2019-11-07 11:41:16 -07001007 wqe->acct[IO_WQ_ACCT_BOUND].max_workers = bounded;
1008 atomic_set(&wqe->acct[IO_WQ_ACCT_BOUND].nr_running, 0);
Jens Axboe728f13e2021-02-21 16:02:53 -07001009 wqe->acct[IO_WQ_ACCT_UNBOUND].max_workers =
Jens Axboec5def4a2019-11-07 11:41:16 -07001010 task_rlimit(current, RLIMIT_NPROC);
Jens Axboec5def4a2019-11-07 11:41:16 -07001011 atomic_set(&wqe->acct[IO_WQ_ACCT_UNBOUND].nr_running, 0);
Jens Axboee9418942021-02-19 12:33:30 -07001012 wqe->wait.func = io_wqe_hash_wake;
1013 INIT_LIST_HEAD(&wqe->wait.entry);
Jens Axboe771b53d02019-10-22 10:25:58 -06001014 wqe->wq = wq;
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +02001015 raw_spin_lock_init(&wqe->lock);
Jens Axboe6206f0e2019-11-26 11:59:32 -07001016 INIT_WQ_LIST(&wqe->work_list);
Jens Axboe021d1cd2019-11-14 08:00:41 -07001017 INIT_HLIST_NULLS_HEAD(&wqe->free_list, 0);
Jens Axboee61df662019-11-13 13:54:49 -07001018 INIT_LIST_HEAD(&wqe->all_list);
Jens Axboe771b53d02019-10-22 10:25:58 -06001019 }
1020
Jens Axboe3bfe6102021-02-16 14:15:30 -07001021 wq->task_pid = current->pid;
Jens Axboed364d9e2021-02-26 09:56:32 -07001022 init_completion(&wq->exited);
Jens Axboe3bfe6102021-02-16 14:15:30 -07001023 refcount_set(&wq->refs, 1);
Jens Axboe771b53d02019-10-22 10:25:58 -06001024
Jens Axboe4fb6ac32021-02-25 10:17:09 -07001025 ret = io_wq_fork_manager(wq);
1026 if (!ret)
Jens Axboe771b53d02019-10-22 10:25:58 -06001027 return wq;
Jens Axboeb60fda62019-11-19 08:37:07 -07001028err:
Jens Axboedc7bbc92021-03-01 09:09:56 -07001029 io_wq_put_hash(data->hash);
Jens Axboe43c01fb2020-10-22 09:02:50 -06001030 cpuhp_state_remove_instance_nocalls(io_wq_online, &wq->cpuhp_node);
Jann Horn3fc50ab2019-11-26 19:10:20 +01001031 for_each_node(node)
1032 kfree(wq->wqes[node]);
Jens Axboe43c01fb2020-10-22 09:02:50 -06001033err_wqes:
Jens Axboeb60fda62019-11-19 08:37:07 -07001034 kfree(wq->wqes);
Jens Axboe43c01fb2020-10-22 09:02:50 -06001035err_wq:
Jens Axboeb60fda62019-11-19 08:37:07 -07001036 kfree(wq);
Jens Axboe771b53d02019-10-22 10:25:58 -06001037 return ERR_PTR(ret);
1038}
1039
Jens Axboeafcc4012021-02-26 13:48:19 -07001040static void io_wq_destroy_manager(struct io_wq *wq)
1041{
1042 if (wq->manager) {
1043 wake_up_process(wq->manager);
1044 wait_for_completion(&wq->exited);
1045 put_task_struct(wq->manager);
1046 wq->manager = NULL;
1047 }
1048}
1049
Jens Axboe4fb6ac32021-02-25 10:17:09 -07001050static void io_wq_destroy(struct io_wq *wq)
Jens Axboe771b53d02019-10-22 10:25:58 -06001051{
Jann Horn3fc50ab2019-11-26 19:10:20 +01001052 int node;
Jens Axboe771b53d02019-10-22 10:25:58 -06001053
Jens Axboe43c01fb2020-10-22 09:02:50 -06001054 cpuhp_state_remove_instance_nocalls(io_wq_online, &wq->cpuhp_node);
1055
Jens Axboeb60fda62019-11-19 08:37:07 -07001056 set_bit(IO_WQ_BIT_EXIT, &wq->state);
Jens Axboeafcc4012021-02-26 13:48:19 -07001057 io_wq_destroy_manager(wq);
Jens Axboe771b53d02019-10-22 10:25:58 -06001058
Jens Axboee9418942021-02-19 12:33:30 -07001059 for_each_node(node) {
1060 struct io_wqe *wqe = wq->wqes[node];
Jens Axboef0127252021-03-03 15:47:04 -07001061 WARN_ON_ONCE(!wq_list_empty(&wqe->work_list));
Jens Axboee9418942021-02-19 12:33:30 -07001062 kfree(wqe);
1063 }
Jens Axboee9418942021-02-19 12:33:30 -07001064 io_wq_put_hash(wq->hash);
Jens Axboe771b53d02019-10-22 10:25:58 -06001065 kfree(wq->wqes);
1066 kfree(wq);
Jens Axboe4fb6ac32021-02-25 10:17:09 -07001067}
1068
1069void io_wq_put(struct io_wq *wq)
1070{
1071 if (refcount_dec_and_test(&wq->refs))
1072 io_wq_destroy(wq);
Jens Axboe771b53d02019-10-22 10:25:58 -06001073}
Jens Axboe848f7e12020-01-23 15:33:32 -07001074
Jens Axboeafcc4012021-02-26 13:48:19 -07001075void io_wq_put_and_exit(struct io_wq *wq)
1076{
1077 set_bit(IO_WQ_BIT_EXIT, &wq->state);
1078 io_wq_destroy_manager(wq);
1079 io_wq_put(wq);
1080}
1081
Jens Axboe43c01fb2020-10-22 09:02:50 -06001082static bool io_wq_worker_affinity(struct io_worker *worker, void *data)
1083{
1084 struct task_struct *task = worker->task;
1085 struct rq_flags rf;
1086 struct rq *rq;
1087
1088 rq = task_rq_lock(task, &rf);
1089 do_set_cpus_allowed(task, cpumask_of_node(worker->wqe->node));
1090 task->flags |= PF_NO_SETAFFINITY;
1091 task_rq_unlock(rq, task, &rf);
1092 return false;
1093}
1094
1095static int io_wq_cpu_online(unsigned int cpu, struct hlist_node *node)
1096{
1097 struct io_wq *wq = hlist_entry_safe(node, struct io_wq, cpuhp_node);
1098 int i;
1099
1100 rcu_read_lock();
1101 for_each_node(i)
1102 io_wq_for_each_worker(wq->wqes[i], io_wq_worker_affinity, NULL);
1103 rcu_read_unlock();
1104 return 0;
1105}
1106
1107static __init int io_wq_init(void)
1108{
1109 int ret;
1110
1111 ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "io-wq/online",
1112 io_wq_cpu_online, NULL);
1113 if (ret < 0)
1114 return ret;
1115 io_wq_online = ret;
1116 return 0;
1117}
1118subsys_initcall(io_wq_init);