blob: 13aeb48a0964ed310798504333d6b5092a0039c8 [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>
Jens Axboe771b53d02019-10-22 10:25:58 -060012#include <linux/percpu.h>
13#include <linux/slab.h>
Jens Axboe771b53d02019-10-22 10:25:58 -060014#include <linux/rculist_nulls.h>
Jens Axboe43c01fb2020-10-22 09:02:50 -060015#include <linux/cpu.h>
Jens Axboe3bfe6102021-02-16 14:15:30 -070016#include <linux/tracehook.h>
Jens Axboe771b53d02019-10-22 10:25:58 -060017
18#include "io-wq.h"
19
20#define WORKER_IDLE_TIMEOUT (5 * HZ)
21
22enum {
23 IO_WORKER_F_UP = 1, /* up and active */
24 IO_WORKER_F_RUNNING = 2, /* account as running */
25 IO_WORKER_F_FREE = 4, /* worker on free list */
Jens Axboe145cc8c2020-09-26 12:37:46 -060026 IO_WORKER_F_FIXED = 8, /* static idle worker */
27 IO_WORKER_F_BOUND = 16, /* is doing bounded work */
Jens Axboe771b53d02019-10-22 10:25:58 -060028};
29
30enum {
31 IO_WQ_BIT_EXIT = 0, /* wq exiting */
Jens Axboe771b53d02019-10-22 10:25:58 -060032};
33
34enum {
35 IO_WQE_FLAG_STALLED = 1, /* stalled on hash */
36};
37
38/*
39 * One for each thread in a wqe pool
40 */
41struct io_worker {
42 refcount_t ref;
43 unsigned flags;
44 struct hlist_nulls_node nulls_node;
Jens Axboee61df662019-11-13 13:54:49 -070045 struct list_head all_list;
Jens Axboe771b53d02019-10-22 10:25:58 -060046 struct task_struct *task;
Jens Axboe771b53d02019-10-22 10:25:58 -060047 struct io_wqe *wqe;
Jens Axboe36c2f922019-11-13 09:43:34 -070048
Jens Axboe771b53d02019-10-22 10:25:58 -060049 struct io_wq_work *cur_work;
Jens Axboe36c2f922019-11-13 09:43:34 -070050 spinlock_t lock;
Jens Axboe771b53d02019-10-22 10:25:58 -060051
Jens Axboeeb2de942021-02-23 19:59:06 -070052 struct completion ref_done;
53
Jens Axboed3e9f732021-08-04 08:37:25 -060054 unsigned long create_state;
55 struct callback_head create_work;
56 int create_index;
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;
Jens Axboe685fe7f2021-03-08 09:37:51 -070072 int index;
Jens Axboec5def4a2019-11-07 11:41:16 -070073 atomic_t nr_running;
74};
75
76enum {
77 IO_WQ_ACCT_BOUND,
78 IO_WQ_ACCT_UNBOUND,
79};
80
Jens Axboe771b53d02019-10-22 10:25:58 -060081/*
82 * Per-node worker thread pool
83 */
84struct io_wqe {
85 struct {
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +020086 raw_spinlock_t lock;
Jens Axboe6206f0e2019-11-26 11:59:32 -070087 struct io_wq_work_list work_list;
Jens Axboe771b53d02019-10-22 10:25:58 -060088 unsigned flags;
89 } ____cacheline_aligned_in_smp;
90
91 int node;
Jens Axboec5def4a2019-11-07 11:41:16 -070092 struct io_wqe_acct acct[2];
Jens Axboe771b53d02019-10-22 10:25:58 -060093
Jens Axboe021d1cd2019-11-14 08:00:41 -070094 struct hlist_nulls_head free_list;
Jens Axboee61df662019-11-13 13:54:49 -070095 struct list_head all_list;
Jens Axboe771b53d02019-10-22 10:25:58 -060096
Jens Axboee9418942021-02-19 12:33:30 -070097 struct wait_queue_entry wait;
98
Jens Axboe771b53d02019-10-22 10:25:58 -060099 struct io_wq *wq;
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300100 struct io_wq_work *hash_tail[IO_WQ_NR_HASH_BUCKETS];
Jens Axboe0e034962021-06-17 10:08:11 -0600101
102 cpumask_var_t cpu_mask;
Jens Axboe771b53d02019-10-22 10:25:58 -0600103};
104
105/*
106 * Per io_wq state
107 */
108struct io_wq {
Jens Axboe771b53d02019-10-22 10:25:58 -0600109 unsigned long state;
Jens Axboe771b53d02019-10-22 10:25:58 -0600110
Pavel Begunkove9fd9392020-03-04 16:14:12 +0300111 free_work_fn *free_work;
Pavel Begunkovf5fa38c2020-06-08 21:08:20 +0300112 io_wq_work_fn *do_work;
Jens Axboe7d723062019-11-12 22:31:31 -0700113
Jens Axboee9418942021-02-19 12:33:30 -0700114 struct io_wq_hash *hash;
115
Jens Axboefb3a1f62021-02-26 09:47:20 -0700116 atomic_t worker_refs;
117 struct completion worker_done;
118
Jens Axboe43c01fb2020-10-22 09:02:50 -0600119 struct hlist_node cpuhp_node;
Jens Axboe3bfe6102021-02-16 14:15:30 -0700120
Jens Axboe685fe7f2021-03-08 09:37:51 -0700121 struct task_struct *task;
Pavel Begunkovc7f405d2021-06-14 02:36:12 +0100122
123 struct io_wqe *wqes[];
Jens Axboe771b53d02019-10-22 10:25:58 -0600124};
125
Jens Axboe43c01fb2020-10-22 09:02:50 -0600126static enum cpuhp_state io_wq_online;
127
Jens Axboef0127252021-03-03 15:47:04 -0700128struct io_cb_cancel_data {
129 work_cancel_fn *fn;
130 void *data;
131 int nr_running;
132 int nr_pending;
133 bool cancel_all;
134};
135
Hao Xu47cae0c2021-08-08 21:54:34 +0800136static void create_io_worker(struct io_wq *wq, struct io_wqe *wqe, int index, bool first);
Jens Axboe83d6c392021-08-03 09:14:35 -0600137static void io_wqe_dec_running(struct io_worker *worker);
Jens Axboef0127252021-03-03 15:47:04 -0700138
Jens Axboe771b53d02019-10-22 10:25:58 -0600139static bool io_worker_get(struct io_worker *worker)
140{
141 return refcount_inc_not_zero(&worker->ref);
142}
143
144static void io_worker_release(struct io_worker *worker)
145{
146 if (refcount_dec_and_test(&worker->ref))
Jens Axboeeb2de942021-02-23 19:59:06 -0700147 complete(&worker->ref_done);
Jens Axboe771b53d02019-10-22 10:25:58 -0600148}
149
Pavel Begunkov8418f222021-03-22 01:58:28 +0000150static inline struct io_wqe_acct *io_get_acct(struct io_wqe *wqe, bool bound)
151{
152 return &wqe->acct[bound ? IO_WQ_ACCT_BOUND : IO_WQ_ACCT_UNBOUND];
153}
154
Jens Axboec5def4a2019-11-07 11:41:16 -0700155static inline struct io_wqe_acct *io_work_get_acct(struct io_wqe *wqe,
156 struct io_wq_work *work)
157{
Pavel Begunkov8418f222021-03-22 01:58:28 +0000158 return io_get_acct(wqe, !(work->flags & IO_WQ_WORK_UNBOUND));
Jens Axboec5def4a2019-11-07 11:41:16 -0700159}
160
Jens Axboe958234d2021-02-17 09:00:57 -0700161static inline struct io_wqe_acct *io_wqe_get_acct(struct io_worker *worker)
Jens Axboec5def4a2019-11-07 11:41:16 -0700162{
Pavel Begunkov8418f222021-03-22 01:58:28 +0000163 return io_get_acct(worker->wqe, worker->flags & IO_WORKER_F_BOUND);
Jens Axboec5def4a2019-11-07 11:41:16 -0700164}
165
Jens Axboe685fe7f2021-03-08 09:37:51 -0700166static void io_worker_ref_put(struct io_wq *wq)
167{
168 if (atomic_dec_and_test(&wq->worker_refs))
169 complete(&wq->worker_done);
170}
171
Jens Axboe771b53d02019-10-22 10:25:58 -0600172static void io_worker_exit(struct io_worker *worker)
173{
174 struct io_wqe *wqe = worker->wqe;
Jens Axboe958234d2021-02-17 09:00:57 -0700175 struct io_wqe_acct *acct = io_wqe_get_acct(worker);
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
Jens Axboea9a4aa92021-08-30 06:33:08 -0600181 raw_spin_lock(&wqe->lock);
Jens Axboe83d6c392021-08-03 09:14:35 -0600182 if (worker->flags & IO_WORKER_F_FREE)
Jens Axboebf1daa42021-02-16 18:00:55 -0700183 hlist_nulls_del_rcu(&worker->nulls_node);
Jens Axboee61df662019-11-13 13:54:49 -0700184 list_del_rcu(&worker->all_list);
Jens Axboec5def4a2019-11-07 11:41:16 -0700185 acct->nr_workers--;
Jens Axboe83d6c392021-08-03 09:14:35 -0600186 preempt_disable();
187 io_wqe_dec_running(worker);
188 worker->flags = 0;
189 current->flags &= ~PF_IO_WORKER;
190 preempt_enable();
Jens Axboea9a4aa92021-08-30 06:33:08 -0600191 raw_spin_unlock(&wqe->lock);
Jens Axboe771b53d02019-10-22 10:25:58 -0600192
YueHaibing364b05f2019-11-02 15:55:01 +0800193 kfree_rcu(worker, rcu);
Jens Axboe685fe7f2021-03-08 09:37:51 -0700194 io_worker_ref_put(wqe->wq);
Jens Axboe46fe18b2021-03-04 12:39:36 -0700195 do_exit(0);
Jens Axboe771b53d02019-10-22 10:25:58 -0600196}
197
Jens Axboec5def4a2019-11-07 11:41:16 -0700198static inline bool io_wqe_run_queue(struct io_wqe *wqe)
199 __must_hold(wqe->lock)
200{
Jens Axboe6206f0e2019-11-26 11:59:32 -0700201 if (!wq_list_empty(&wqe->work_list) &&
202 !(wqe->flags & IO_WQE_FLAG_STALLED))
Jens Axboec5def4a2019-11-07 11:41:16 -0700203 return true;
204 return false;
205}
206
207/*
208 * Check head of free list for an available worker. If one isn't available,
Jens Axboe685fe7f2021-03-08 09:37:51 -0700209 * caller must create one.
Jens Axboec5def4a2019-11-07 11:41:16 -0700210 */
211static bool io_wqe_activate_free_worker(struct io_wqe *wqe)
212 __must_hold(RCU)
213{
214 struct hlist_nulls_node *n;
215 struct io_worker *worker;
216
Jens Axboe83d6c392021-08-03 09:14:35 -0600217 /*
218 * Iterate free_list and see if we can find an idle worker to
219 * activate. If a given worker is on the free_list but in the process
220 * of exiting, keep trying.
221 */
222 hlist_nulls_for_each_entry_rcu(worker, n, &wqe->free_list, nulls_node) {
223 if (!io_worker_get(worker))
224 continue;
225 if (wake_up_process(worker->task)) {
226 io_worker_release(worker);
227 return true;
228 }
Jens Axboec5def4a2019-11-07 11:41:16 -0700229 io_worker_release(worker);
Jens Axboec5def4a2019-11-07 11:41:16 -0700230 }
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
Jens Axboe685fe7f2021-03-08 09:37:51 -0700237 * below the max number of workers, create one.
Jens Axboec5def4a2019-11-07 11:41:16 -0700238 */
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 */
Pavel Begunkove6ab8992021-06-17 18:13:59 +0100247 if (unlikely(!acct->max_workers))
248 pr_warn_once("io-wq is not configured for unbound workers");
Jens Axboec5def4a2019-11-07 11:41:16 -0700249
250 rcu_read_lock();
251 ret = io_wqe_activate_free_worker(wqe);
252 rcu_read_unlock();
253
Hao Xu3d4e4fa2021-08-05 18:05:37 +0800254 if (!ret) {
Hao Xu47cae0c2021-08-08 21:54:34 +0800255 bool do_create = false, first = false;
Hao Xu3d4e4fa2021-08-05 18:05:37 +0800256
Jens Axboea9a4aa92021-08-30 06:33:08 -0600257 raw_spin_lock(&wqe->lock);
Hao Xu3d4e4fa2021-08-05 18:05:37 +0800258 if (acct->nr_workers < acct->max_workers) {
Hao Xu47cae0c2021-08-08 21:54:34 +0800259 if (!acct->nr_workers)
260 first = true;
Hao Xu3d4e4fa2021-08-05 18:05:37 +0800261 acct->nr_workers++;
262 do_create = true;
263 }
Jens Axboea9a4aa92021-08-30 06:33:08 -0600264 raw_spin_unlock(&wqe->lock);
Hao Xu79dca182021-08-10 20:55:54 +0800265 if (do_create) {
266 atomic_inc(&acct->nr_running);
267 atomic_inc(&wqe->wq->worker_refs);
Hao Xu47cae0c2021-08-08 21:54:34 +0800268 create_io_worker(wqe->wq, wqe, acct->index, first);
Hao Xu79dca182021-08-10 20:55:54 +0800269 }
Jens Axboe685fe7f2021-03-08 09:37:51 -0700270 }
Jens Axboec5def4a2019-11-07 11:41:16 -0700271}
272
Jens Axboe958234d2021-02-17 09:00:57 -0700273static void io_wqe_inc_running(struct io_worker *worker)
Jens Axboec5def4a2019-11-07 11:41:16 -0700274{
Jens Axboe958234d2021-02-17 09:00:57 -0700275 struct io_wqe_acct *acct = io_wqe_get_acct(worker);
Jens Axboec5def4a2019-11-07 11:41:16 -0700276
277 atomic_inc(&acct->nr_running);
278}
279
Jens Axboe685fe7f2021-03-08 09:37:51 -0700280static void create_worker_cb(struct callback_head *cb)
281{
Jens Axboed3e9f732021-08-04 08:37:25 -0600282 struct io_worker *worker;
Jens Axboe685fe7f2021-03-08 09:37:51 -0700283 struct io_wq *wq;
Hao Xu21698272021-08-05 18:05:38 +0800284 struct io_wqe *wqe;
285 struct io_wqe_acct *acct;
Hao Xu47cae0c2021-08-08 21:54:34 +0800286 bool do_create = false, first = false;
Jens Axboe685fe7f2021-03-08 09:37:51 -0700287
Jens Axboed3e9f732021-08-04 08:37:25 -0600288 worker = container_of(cb, struct io_worker, create_work);
289 wqe = worker->wqe;
Hao Xu21698272021-08-05 18:05:38 +0800290 wq = wqe->wq;
Jens Axboed3e9f732021-08-04 08:37:25 -0600291 acct = &wqe->acct[worker->create_index];
Jens Axboea9a4aa92021-08-30 06:33:08 -0600292 raw_spin_lock(&wqe->lock);
Hao Xu49e7f0c2021-08-08 21:54:33 +0800293 if (acct->nr_workers < acct->max_workers) {
Hao Xu47cae0c2021-08-08 21:54:34 +0800294 if (!acct->nr_workers)
295 first = true;
Hao Xu21698272021-08-05 18:05:38 +0800296 acct->nr_workers++;
Hao Xu49e7f0c2021-08-08 21:54:33 +0800297 do_create = true;
298 }
Jens Axboea9a4aa92021-08-30 06:33:08 -0600299 raw_spin_unlock(&wqe->lock);
Hao Xu49e7f0c2021-08-08 21:54:33 +0800300 if (do_create) {
Jens Axboed3e9f732021-08-04 08:37:25 -0600301 create_io_worker(wq, wqe, worker->create_index, first);
Hao Xu49e7f0c2021-08-08 21:54:33 +0800302 } else {
303 atomic_dec(&acct->nr_running);
304 io_worker_ref_put(wq);
305 }
Jens Axboed3e9f732021-08-04 08:37:25 -0600306 clear_bit_unlock(0, &worker->create_state);
307 io_worker_release(worker);
Jens Axboe685fe7f2021-03-08 09:37:51 -0700308}
309
Jens Axboed3e9f732021-08-04 08:37:25 -0600310static void io_queue_worker_create(struct io_wqe *wqe, struct io_worker *worker,
311 struct io_wqe_acct *acct)
Jens Axboe685fe7f2021-03-08 09:37:51 -0700312{
Jens Axboe685fe7f2021-03-08 09:37:51 -0700313 struct io_wq *wq = wqe->wq;
314
315 /* raced with exit, just ignore create call */
316 if (test_bit(IO_WQ_BIT_EXIT, &wq->state))
317 goto fail;
Jens Axboed3e9f732021-08-04 08:37:25 -0600318 if (!io_worker_get(worker))
319 goto fail;
320 /*
321 * create_state manages ownership of create_work/index. We should
322 * only need one entry per worker, as the worker going to sleep
323 * will trigger the condition, and waking will clear it once it
324 * runs the task_work.
325 */
326 if (test_bit(0, &worker->create_state) ||
327 test_and_set_bit_lock(0, &worker->create_state))
328 goto fail_release;
Jens Axboe685fe7f2021-03-08 09:37:51 -0700329
Jens Axboed3e9f732021-08-04 08:37:25 -0600330 init_task_work(&worker->create_work, create_worker_cb);
331 worker->create_index = acct->index;
332 if (!task_work_add(wq->task, &worker->create_work, TWA_SIGNAL))
333 return;
334 clear_bit_unlock(0, &worker->create_state);
335fail_release:
336 io_worker_release(worker);
Jens Axboe685fe7f2021-03-08 09:37:51 -0700337fail:
338 atomic_dec(&acct->nr_running);
339 io_worker_ref_put(wq);
340}
341
Jens Axboe958234d2021-02-17 09:00:57 -0700342static void io_wqe_dec_running(struct io_worker *worker)
Jens Axboec5def4a2019-11-07 11:41:16 -0700343 __must_hold(wqe->lock)
344{
Jens Axboe958234d2021-02-17 09:00:57 -0700345 struct io_wqe_acct *acct = io_wqe_get_acct(worker);
346 struct io_wqe *wqe = worker->wqe;
Jens Axboec5def4a2019-11-07 11:41:16 -0700347
Jens Axboe685fe7f2021-03-08 09:37:51 -0700348 if (!(worker->flags & IO_WORKER_F_UP))
349 return;
350
351 if (atomic_dec_and_test(&acct->nr_running) && io_wqe_run_queue(wqe)) {
352 atomic_inc(&acct->nr_running);
353 atomic_inc(&wqe->wq->worker_refs);
Jens Axboed3e9f732021-08-04 08:37:25 -0600354 io_queue_worker_create(wqe, worker, acct);
Jens Axboe685fe7f2021-03-08 09:37:51 -0700355 }
Jens Axboec5def4a2019-11-07 11:41:16 -0700356}
357
Jens Axboe771b53d02019-10-22 10:25:58 -0600358/*
359 * Worker will start processing some work. Move it to the busy list, if
360 * it's currently on the freelist
361 */
362static void __io_worker_busy(struct io_wqe *wqe, struct io_worker *worker,
363 struct io_wq_work *work)
364 __must_hold(wqe->lock)
365{
Jens Axboec5def4a2019-11-07 11:41:16 -0700366 bool worker_bound, work_bound;
367
Hao Xu417b5052021-04-06 11:08:45 +0800368 BUILD_BUG_ON((IO_WQ_ACCT_UNBOUND ^ IO_WQ_ACCT_BOUND) != 1);
369
Jens Axboe771b53d02019-10-22 10:25:58 -0600370 if (worker->flags & IO_WORKER_F_FREE) {
371 worker->flags &= ~IO_WORKER_F_FREE;
372 hlist_nulls_del_init_rcu(&worker->nulls_node);
Jens Axboe771b53d02019-10-22 10:25:58 -0600373 }
Jens Axboec5def4a2019-11-07 11:41:16 -0700374
375 /*
376 * If worker is moving from bound to unbound (or vice versa), then
377 * ensure we update the running accounting.
378 */
Dan Carpenterb2e9c7d62019-11-19 09:22:16 +0300379 worker_bound = (worker->flags & IO_WORKER_F_BOUND) != 0;
380 work_bound = (work->flags & IO_WQ_WORK_UNBOUND) == 0;
381 if (worker_bound != work_bound) {
Hao Xu417b5052021-04-06 11:08:45 +0800382 int index = work_bound ? IO_WQ_ACCT_UNBOUND : IO_WQ_ACCT_BOUND;
Jens Axboe958234d2021-02-17 09:00:57 -0700383 io_wqe_dec_running(worker);
Hao Xu417b5052021-04-06 11:08:45 +0800384 worker->flags ^= IO_WORKER_F_BOUND;
385 wqe->acct[index].nr_workers--;
386 wqe->acct[index ^ 1].nr_workers++;
Jens Axboe958234d2021-02-17 09:00:57 -0700387 io_wqe_inc_running(worker);
Jens Axboec5def4a2019-11-07 11:41:16 -0700388 }
Jens Axboe771b53d02019-10-22 10:25:58 -0600389}
390
391/*
392 * No work, worker going to sleep. Move to freelist, and unuse mm if we
393 * have one attached. Dropping the mm may potentially sleep, so we drop
394 * the lock in that case and return success. Since the caller has to
395 * retry the loop in that case (we changed task state), we don't regrab
396 * the lock if we return success.
397 */
Jens Axboec6d77d92021-02-15 13:26:34 -0700398static void __io_worker_idle(struct io_wqe *wqe, struct io_worker *worker)
Jens Axboe771b53d02019-10-22 10:25:58 -0600399 __must_hold(wqe->lock)
400{
401 if (!(worker->flags & IO_WORKER_F_FREE)) {
402 worker->flags |= IO_WORKER_F_FREE;
Jens Axboe021d1cd2019-11-14 08:00:41 -0700403 hlist_nulls_add_head_rcu(&worker->nulls_node, &wqe->free_list);
Jens Axboe771b53d02019-10-22 10:25:58 -0600404 }
Jens Axboe771b53d02019-10-22 10:25:58 -0600405}
406
Pavel Begunkov60cf46a2020-03-14 00:31:05 +0300407static inline unsigned int io_get_work_hash(struct io_wq_work *work)
408{
409 return work->flags >> IO_WQ_HASH_SHIFT;
410}
411
Jens Axboee9418942021-02-19 12:33:30 -0700412static void io_wait_on_hash(struct io_wqe *wqe, unsigned int hash)
413{
414 struct io_wq *wq = wqe->wq;
415
416 spin_lock(&wq->hash->wait.lock);
417 if (list_empty(&wqe->wait.entry)) {
418 __add_wait_queue(&wq->hash->wait, &wqe->wait);
419 if (!test_bit(hash, &wq->hash->map)) {
420 __set_current_state(TASK_RUNNING);
421 list_del_init(&wqe->wait.entry);
422 }
423 }
424 spin_unlock(&wq->hash->wait.lock);
425}
426
Jens Axboeecc53c42021-08-29 16:13:03 -0600427/*
428 * We can always run the work if the worker is currently the same type as
429 * the work (eg both are bound, or both are unbound). If they are not the
430 * same, only allow it if incrementing the worker count would be allowed.
431 */
432static bool io_worker_can_run_work(struct io_worker *worker,
433 struct io_wq_work *work)
434{
435 struct io_wqe_acct *acct;
436
437 if (!(worker->flags & IO_WORKER_F_BOUND) !=
438 !(work->flags & IO_WQ_WORK_UNBOUND))
439 return true;
440
441 /* not the same type, check if we'd go over the limit */
442 acct = io_work_get_acct(worker->wqe, work);
443 return acct->nr_workers < acct->max_workers;
444}
445
446static struct io_wq_work *io_get_next_work(struct io_wqe *wqe,
447 struct io_worker *worker,
448 bool *stalled)
Jens Axboe771b53d02019-10-22 10:25:58 -0600449 __must_hold(wqe->lock)
450{
Jens Axboe6206f0e2019-11-26 11:59:32 -0700451 struct io_wq_work_node *node, *prev;
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300452 struct io_wq_work *work, *tail;
Jens Axboee9418942021-02-19 12:33:30 -0700453 unsigned int stall_hash = -1U;
Jens Axboe771b53d02019-10-22 10:25:58 -0600454
Jens Axboe6206f0e2019-11-26 11:59:32 -0700455 wq_list_for_each(node, prev, &wqe->work_list) {
Jens Axboee9418942021-02-19 12:33:30 -0700456 unsigned int hash;
457
Jens Axboe6206f0e2019-11-26 11:59:32 -0700458 work = container_of(node, struct io_wq_work, list);
459
Jens Axboeecc53c42021-08-29 16:13:03 -0600460 if (!io_worker_can_run_work(worker, work))
461 break;
462
Jens Axboe771b53d02019-10-22 10:25:58 -0600463 /* not hashed, can run anytime */
Pavel Begunkov8766dd52020-03-14 00:31:04 +0300464 if (!io_wq_is_hashed(work)) {
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300465 wq_list_del(&wqe->work_list, node, prev);
Jens Axboe771b53d02019-10-22 10:25:58 -0600466 return work;
467 }
468
Pavel Begunkov60cf46a2020-03-14 00:31:05 +0300469 hash = io_get_work_hash(work);
Jens Axboee9418942021-02-19 12:33:30 -0700470 /* all items with this hash lie in [work, tail] */
471 tail = wqe->hash_tail[hash];
472
473 /* hashed, can run if not already running */
474 if (!test_and_set_bit(hash, &wqe->wq->hash->map)) {
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300475 wqe->hash_tail[hash] = NULL;
476 wq_list_cut(&wqe->work_list, &tail->list, prev);
Jens Axboe771b53d02019-10-22 10:25:58 -0600477 return work;
478 }
Jens Axboee9418942021-02-19 12:33:30 -0700479 if (stall_hash == -1U)
480 stall_hash = hash;
481 /* fast forward to a next hash, for-each will fix up @prev */
482 node = &tail->list;
483 }
484
485 if (stall_hash != -1U) {
486 raw_spin_unlock(&wqe->lock);
487 io_wait_on_hash(wqe, stall_hash);
488 raw_spin_lock(&wqe->lock);
Jens Axboeecc53c42021-08-29 16:13:03 -0600489 *stalled = true;
Jens Axboe771b53d02019-10-22 10:25:58 -0600490 }
491
492 return NULL;
493}
494
Jens Axboe00ddff42021-03-21 07:06:56 -0600495static bool io_flush_signals(void)
Jens Axboecccf0ee2020-01-27 16:34:48 -0700496{
Jens Axboe0b8cfa92021-03-21 14:16:08 -0600497 if (unlikely(test_thread_flag(TIF_NOTIFY_SIGNAL))) {
Jens Axboe00ddff42021-03-21 07:06:56 -0600498 __set_current_state(TASK_RUNNING);
Jens Axboe0b8cfa92021-03-21 14:16:08 -0600499 tracehook_notify_signal();
Jens Axboe00ddff42021-03-21 07:06:56 -0600500 return true;
Jens Axboecccf0ee2020-01-27 16:34:48 -0700501 }
Jens Axboe00ddff42021-03-21 07:06:56 -0600502 return false;
Pavel Begunkovdc026a72020-03-04 16:14:09 +0300503}
504
505static void io_assign_current_work(struct io_worker *worker,
506 struct io_wq_work *work)
507{
Pavel Begunkovd78298e2020-03-14 00:31:03 +0300508 if (work) {
Jens Axboe3bfe6102021-02-16 14:15:30 -0700509 io_flush_signals();
Pavel Begunkovd78298e2020-03-14 00:31:03 +0300510 cond_resched();
511 }
Pavel Begunkovdc026a72020-03-04 16:14:09 +0300512
Jens Axboea9a4aa92021-08-30 06:33:08 -0600513 spin_lock(&worker->lock);
Pavel Begunkovdc026a72020-03-04 16:14:09 +0300514 worker->cur_work = work;
Jens Axboea9a4aa92021-08-30 06:33:08 -0600515 spin_unlock(&worker->lock);
Pavel Begunkovdc026a72020-03-04 16:14:09 +0300516}
517
Pavel Begunkov60cf46a2020-03-14 00:31:05 +0300518static void io_wqe_enqueue(struct io_wqe *wqe, struct io_wq_work *work);
519
Jens Axboe771b53d02019-10-22 10:25:58 -0600520static void io_worker_handle_work(struct io_worker *worker)
521 __releases(wqe->lock)
522{
Jens Axboe771b53d02019-10-22 10:25:58 -0600523 struct io_wqe *wqe = worker->wqe;
524 struct io_wq *wq = wqe->wq;
Pavel Begunkovc60eb042021-04-08 01:54:42 +0100525 bool do_kill = test_bit(IO_WQ_BIT_EXIT, &wq->state);
Jens Axboe771b53d02019-10-22 10:25:58 -0600526
527 do {
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300528 struct io_wq_work *work;
Jens Axboeecc53c42021-08-29 16:13:03 -0600529 bool stalled;
Pavel Begunkovf462fd32020-03-04 16:14:11 +0300530get_next:
Jens Axboe771b53d02019-10-22 10:25:58 -0600531 /*
Jens Axboe771b53d02019-10-22 10:25:58 -0600532 * If we got some work, mark us as busy. If we didn't, but
533 * the list isn't empty, it means we stalled on hashed work.
534 * Mark us stalled so we don't keep looking for work when we
535 * can't make progress, any work completion or insertion will
536 * clear the stalled flag.
537 */
Jens Axboeecc53c42021-08-29 16:13:03 -0600538 stalled = false;
539 work = io_get_next_work(wqe, worker, &stalled);
Jens Axboe771b53d02019-10-22 10:25:58 -0600540 if (work)
541 __io_worker_busy(wqe, worker, work);
Jens Axboeecc53c42021-08-29 16:13:03 -0600542 else if (stalled)
Jens Axboe771b53d02019-10-22 10:25:58 -0600543 wqe->flags |= IO_WQE_FLAG_STALLED;
544
Jens Axboea9a4aa92021-08-30 06:33:08 -0600545 raw_spin_unlock(&wqe->lock);
Jens Axboe771b53d02019-10-22 10:25:58 -0600546 if (!work)
547 break;
Pavel Begunkov58e39312020-03-04 16:14:10 +0300548 io_assign_current_work(worker, work);
Jens Axboee9418942021-02-19 12:33:30 -0700549 __set_current_state(TASK_RUNNING);
Jens Axboe36c2f922019-11-13 09:43:34 -0700550
Pavel Begunkovdc026a72020-03-04 16:14:09 +0300551 /* handle a whole dependent link */
552 do {
Pavel Begunkov5280f7e2021-02-04 13:52:08 +0000553 struct io_wq_work *next_hashed, *linked;
Pavel Begunkovb089ed392020-07-25 14:42:00 +0300554 unsigned int hash = io_get_work_hash(work);
Hillf Dantonfd1c4bc2019-12-24 09:14:29 -0700555
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300556 next_hashed = wq_next_work(work);
Pavel Begunkovc60eb042021-04-08 01:54:42 +0100557
558 if (unlikely(do_kill) && (work->flags & IO_WQ_WORK_UNBOUND))
559 work->flags |= IO_WQ_WORK_CANCEL;
Pavel Begunkov5280f7e2021-02-04 13:52:08 +0000560 wq->do_work(work);
561 io_assign_current_work(worker, NULL);
Jens Axboe36c2f922019-11-13 09:43:34 -0700562
Pavel Begunkov5280f7e2021-02-04 13:52:08 +0000563 linked = wq->free_work(work);
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300564 work = next_hashed;
565 if (!work && linked && !io_wq_is_hashed(linked)) {
566 work = linked;
567 linked = NULL;
568 }
569 io_assign_current_work(worker, work);
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300570 if (linked)
571 io_wqe_enqueue(wqe, linked);
572
573 if (hash != -1U && !next_hashed) {
Jens Axboee9418942021-02-19 12:33:30 -0700574 clear_bit(hash, &wq->hash->map);
575 if (wq_has_sleeper(&wq->hash->wait))
576 wake_up(&wq->hash->wait);
Jens Axboea9a4aa92021-08-30 06:33:08 -0600577 raw_spin_lock(&wqe->lock);
Pavel Begunkovdc026a72020-03-04 16:14:09 +0300578 wqe->flags &= ~IO_WQE_FLAG_STALLED;
Pavel Begunkovf462fd32020-03-04 16:14:11 +0300579 /* skip unnecessary unlock-lock wqe->lock */
580 if (!work)
581 goto get_next;
Jens Axboea9a4aa92021-08-30 06:33:08 -0600582 raw_spin_unlock(&wqe->lock);
Pavel Begunkovdc026a72020-03-04 16:14:09 +0300583 }
Pavel Begunkov58e39312020-03-04 16:14:10 +0300584 } while (work);
Jens Axboe36c2f922019-11-13 09:43:34 -0700585
Jens Axboea9a4aa92021-08-30 06:33:08 -0600586 raw_spin_lock(&wqe->lock);
Jens Axboe771b53d02019-10-22 10:25:58 -0600587 } while (1);
588}
589
Jens Axboe771b53d02019-10-22 10:25:58 -0600590static int io_wqe_worker(void *data)
591{
592 struct io_worker *worker = data;
593 struct io_wqe *wqe = worker->wqe;
594 struct io_wq *wq = wqe->wq;
Jens Axboe46fe18b2021-03-04 12:39:36 -0700595 char buf[TASK_COMM_LEN];
Jens Axboe771b53d02019-10-22 10:25:58 -0600596
Jens Axboe46fe18b2021-03-04 12:39:36 -0700597 worker->flags |= (IO_WORKER_F_UP | IO_WORKER_F_RUNNING);
Jens Axboe46fe18b2021-03-04 12:39:36 -0700598
Jens Axboe685fe7f2021-03-08 09:37:51 -0700599 snprintf(buf, sizeof(buf), "iou-wrk-%d", wq->task->pid);
Jens Axboe46fe18b2021-03-04 12:39:36 -0700600 set_task_comm(current, buf);
Jens Axboe771b53d02019-10-22 10:25:58 -0600601
602 while (!test_bit(IO_WQ_BIT_EXIT, &wq->state)) {
Jens Axboe16efa4f2021-03-12 20:26:13 -0700603 long ret;
604
Jens Axboe506d95f2019-12-07 21:03:59 -0700605 set_current_state(TASK_INTERRUPTIBLE);
Jens Axboee995d512019-12-07 21:06:46 -0700606loop:
Jens Axboea9a4aa92021-08-30 06:33:08 -0600607 raw_spin_lock(&wqe->lock);
Jens Axboe771b53d02019-10-22 10:25:58 -0600608 if (io_wqe_run_queue(wqe)) {
Jens Axboe771b53d02019-10-22 10:25:58 -0600609 io_worker_handle_work(worker);
Jens Axboee995d512019-12-07 21:06:46 -0700610 goto loop;
Jens Axboe771b53d02019-10-22 10:25:58 -0600611 }
Jens Axboec6d77d92021-02-15 13:26:34 -0700612 __io_worker_idle(wqe, worker);
Jens Axboea9a4aa92021-08-30 06:33:08 -0600613 raw_spin_unlock(&wqe->lock);
Jens Axboe00ddff42021-03-21 07:06:56 -0600614 if (io_flush_signals())
615 continue;
Jens Axboe16efa4f2021-03-12 20:26:13 -0700616 ret = schedule_timeout(WORKER_IDLE_TIMEOUT);
Jens Axboedbe1bdb2021-03-25 18:16:06 -0600617 if (signal_pending(current)) {
618 struct ksignal ksig;
619
620 if (!get_signal(&ksig))
621 continue;
Jens Axboe3bfe6102021-02-16 14:15:30 -0700622 break;
Jens Axboedbe1bdb2021-03-25 18:16:06 -0600623 }
624 if (ret)
625 continue;
Jens Axboe771b53d02019-10-22 10:25:58 -0600626 /* timed out, exit unless we're the fixed worker */
Pavel Begunkov769e6832021-06-14 02:36:16 +0100627 if (!(worker->flags & IO_WORKER_F_FIXED))
Jens Axboe771b53d02019-10-22 10:25:58 -0600628 break;
629 }
630
Jens Axboe771b53d02019-10-22 10:25:58 -0600631 if (test_bit(IO_WQ_BIT_EXIT, &wq->state)) {
Jens Axboea9a4aa92021-08-30 06:33:08 -0600632 raw_spin_lock(&wqe->lock);
Pavel Begunkove5872272021-06-14 02:36:17 +0100633 io_worker_handle_work(worker);
Jens Axboe771b53d02019-10-22 10:25:58 -0600634 }
635
636 io_worker_exit(worker);
637 return 0;
638}
639
640/*
Jens Axboe771b53d02019-10-22 10:25:58 -0600641 * Called when a worker is scheduled in. Mark us as currently running.
642 */
643void io_wq_worker_running(struct task_struct *tsk)
644{
Jens Axboe3bfe6102021-02-16 14:15:30 -0700645 struct io_worker *worker = tsk->pf_io_worker;
Jens Axboe771b53d02019-10-22 10:25:58 -0600646
Jens Axboe3bfe6102021-02-16 14:15:30 -0700647 if (!worker)
648 return;
Jens Axboe771b53d02019-10-22 10:25:58 -0600649 if (!(worker->flags & IO_WORKER_F_UP))
650 return;
651 if (worker->flags & IO_WORKER_F_RUNNING)
652 return;
653 worker->flags |= IO_WORKER_F_RUNNING;
Jens Axboe958234d2021-02-17 09:00:57 -0700654 io_wqe_inc_running(worker);
Jens Axboe771b53d02019-10-22 10:25:58 -0600655}
656
657/*
658 * Called when worker is going to sleep. If there are no workers currently
Jens Axboe685fe7f2021-03-08 09:37:51 -0700659 * running and we have work pending, wake up a free one or create a new one.
Jens Axboe771b53d02019-10-22 10:25:58 -0600660 */
661void io_wq_worker_sleeping(struct task_struct *tsk)
662{
Jens Axboe3bfe6102021-02-16 14:15:30 -0700663 struct io_worker *worker = tsk->pf_io_worker;
Jens Axboe771b53d02019-10-22 10:25:58 -0600664
Jens Axboe3bfe6102021-02-16 14:15:30 -0700665 if (!worker)
666 return;
Jens Axboe771b53d02019-10-22 10:25:58 -0600667 if (!(worker->flags & IO_WORKER_F_UP))
668 return;
669 if (!(worker->flags & IO_WORKER_F_RUNNING))
670 return;
671
672 worker->flags &= ~IO_WORKER_F_RUNNING;
673
Jens Axboea9a4aa92021-08-30 06:33:08 -0600674 raw_spin_lock(&worker->wqe->lock);
Jens Axboe958234d2021-02-17 09:00:57 -0700675 io_wqe_dec_running(worker);
Jens Axboea9a4aa92021-08-30 06:33:08 -0600676 raw_spin_unlock(&worker->wqe->lock);
Jens Axboe771b53d02019-10-22 10:25:58 -0600677}
678
Hao Xu47cae0c2021-08-08 21:54:34 +0800679static void create_io_worker(struct io_wq *wq, struct io_wqe *wqe, int index, bool first)
Jens Axboe3bfe6102021-02-16 14:15:30 -0700680{
Jens Axboe46fe18b2021-03-04 12:39:36 -0700681 struct io_wqe_acct *acct = &wqe->acct[index];
Jens Axboe3bfe6102021-02-16 14:15:30 -0700682 struct io_worker *worker;
Jens Axboe46fe18b2021-03-04 12:39:36 -0700683 struct task_struct *tsk;
Jens Axboe3bfe6102021-02-16 14:15:30 -0700684
Jens Axboe8b3e78b2021-02-23 15:34:06 -0700685 __set_current_state(TASK_RUNNING);
686
Jens Axboe3bfe6102021-02-16 14:15:30 -0700687 worker = kzalloc_node(sizeof(*worker), GFP_KERNEL, wqe->node);
688 if (!worker)
Jens Axboe685fe7f2021-03-08 09:37:51 -0700689 goto fail;
Jens Axboe3bfe6102021-02-16 14:15:30 -0700690
691 refcount_set(&worker->ref, 1);
692 worker->nulls_node.pprev = NULL;
693 worker->wqe = wqe;
694 spin_lock_init(&worker->lock);
Jens Axboeeb2de942021-02-23 19:59:06 -0700695 init_completion(&worker->ref_done);
Jens Axboe3bfe6102021-02-16 14:15:30 -0700696
Jens Axboe46fe18b2021-03-04 12:39:36 -0700697 tsk = create_io_thread(io_wqe_worker, worker, wqe->node);
698 if (IS_ERR(tsk)) {
Jens Axboe3bfe6102021-02-16 14:15:30 -0700699 kfree(worker);
Jens Axboe685fe7f2021-03-08 09:37:51 -0700700fail:
701 atomic_dec(&acct->nr_running);
Jens Axboea9a4aa92021-08-30 06:33:08 -0600702 raw_spin_lock(&wqe->lock);
Hao Xu3d4e4fa2021-08-05 18:05:37 +0800703 acct->nr_workers--;
Jens Axboea9a4aa92021-08-30 06:33:08 -0600704 raw_spin_unlock(&wqe->lock);
Jens Axboe685fe7f2021-03-08 09:37:51 -0700705 io_worker_ref_put(wq);
706 return;
Jens Axboe3bfe6102021-02-16 14:15:30 -0700707 }
Jens Axboe46fe18b2021-03-04 12:39:36 -0700708
709 tsk->pf_io_worker = worker;
710 worker->task = tsk;
Jens Axboe0e034962021-06-17 10:08:11 -0600711 set_cpus_allowed_ptr(tsk, wqe->cpu_mask);
Jens Axboee22bc9b2021-03-09 19:49:02 -0700712 tsk->flags |= PF_NO_SETAFFINITY;
Jens Axboe46fe18b2021-03-04 12:39:36 -0700713
Jens Axboea9a4aa92021-08-30 06:33:08 -0600714 raw_spin_lock(&wqe->lock);
Jens Axboe46fe18b2021-03-04 12:39:36 -0700715 hlist_nulls_add_head_rcu(&worker->nulls_node, &wqe->free_list);
716 list_add_tail_rcu(&worker->all_list, &wqe->all_list);
717 worker->flags |= IO_WORKER_F_FREE;
718 if (index == IO_WQ_ACCT_BOUND)
719 worker->flags |= IO_WORKER_F_BOUND;
Hao Xu47cae0c2021-08-08 21:54:34 +0800720 if (first && (worker->flags & IO_WORKER_F_BOUND))
Jens Axboe46fe18b2021-03-04 12:39:36 -0700721 worker->flags |= IO_WORKER_F_FIXED;
Jens Axboea9a4aa92021-08-30 06:33:08 -0600722 raw_spin_unlock(&wqe->lock);
Jens Axboe46fe18b2021-03-04 12:39:36 -0700723 wake_up_new_task(tsk);
Jens Axboe771b53d02019-10-22 10:25:58 -0600724}
725
Hillf Dantonc4068bf2020-09-26 21:26:55 +0800726/*
727 * Iterate the passed in list and call the specific function for each
728 * worker that isn't exiting
729 */
730static bool io_wq_for_each_worker(struct io_wqe *wqe,
731 bool (*func)(struct io_worker *, void *),
732 void *data)
733{
734 struct io_worker *worker;
735 bool ret = false;
736
737 list_for_each_entry_rcu(worker, &wqe->all_list, all_list) {
738 if (io_worker_get(worker)) {
739 /* no task if node is/was offline */
740 if (worker->task)
741 ret = func(worker, data);
742 io_worker_release(worker);
743 if (ret)
744 break;
745 }
746 }
747
748 return ret;
749}
750
751static bool io_wq_worker_wake(struct io_worker *worker, void *data)
752{
Jens Axboe46fe18b2021-03-04 12:39:36 -0700753 set_notify_signal(worker->task);
Hillf Dantonc4068bf2020-09-26 21:26:55 +0800754 wake_up_process(worker->task);
755 return false;
756}
757
Jens Axboef0127252021-03-03 15:47:04 -0700758static bool io_wq_work_match_all(struct io_wq_work *work, void *data)
759{
760 return true;
761}
762
Pavel Begunkove9fd9392020-03-04 16:14:12 +0300763static void io_run_cancel(struct io_wq_work *work, struct io_wqe *wqe)
Pavel Begunkovfc04c392020-03-01 19:18:19 +0300764{
Pavel Begunkove9fd9392020-03-04 16:14:12 +0300765 struct io_wq *wq = wqe->wq;
766
Pavel Begunkovfc04c392020-03-01 19:18:19 +0300767 do {
Pavel Begunkovfc04c392020-03-01 19:18:19 +0300768 work->flags |= IO_WQ_WORK_CANCEL;
Pavel Begunkov5280f7e2021-02-04 13:52:08 +0000769 wq->do_work(work);
770 work = wq->free_work(work);
Pavel Begunkovfc04c392020-03-01 19:18:19 +0300771 } while (work);
772}
773
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300774static void io_wqe_insert_work(struct io_wqe *wqe, struct io_wq_work *work)
775{
776 unsigned int hash;
777 struct io_wq_work *tail;
778
779 if (!io_wq_is_hashed(work)) {
780append:
781 wq_list_add_tail(&work->list, &wqe->work_list);
782 return;
783 }
784
785 hash = io_get_work_hash(work);
786 tail = wqe->hash_tail[hash];
787 wqe->hash_tail[hash] = work;
788 if (!tail)
789 goto append;
790
791 wq_list_add_after(&work->list, &tail->list, &wqe->work_list);
792}
793
Jens Axboe771b53d02019-10-22 10:25:58 -0600794static void io_wqe_enqueue(struct io_wqe *wqe, struct io_wq_work *work)
795{
Jens Axboec5def4a2019-11-07 11:41:16 -0700796 struct io_wqe_acct *acct = io_work_get_acct(wqe, work);
Jens Axboe895e2ca2019-12-17 08:46:33 -0700797 int work_flags;
Jens Axboe771b53d02019-10-22 10:25:58 -0600798
Jens Axboe991468d2021-07-23 11:53:54 -0600799 /*
800 * If io-wq is exiting for this task, or if the request has explicitly
801 * been marked as one that should not get executed, cancel it here.
802 */
803 if (test_bit(IO_WQ_BIT_EXIT, &wqe->wq->state) ||
804 (work->flags & IO_WQ_WORK_CANCEL)) {
yangerkun70e35122021-03-09 11:04:10 +0800805 io_run_cancel(work, wqe);
Jens Axboe4fb6ac32021-02-25 10:17:09 -0700806 return;
807 }
808
Jens Axboe895e2ca2019-12-17 08:46:33 -0700809 work_flags = work->flags;
Jens Axboea9a4aa92021-08-30 06:33:08 -0600810 raw_spin_lock(&wqe->lock);
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300811 io_wqe_insert_work(wqe, work);
Jens Axboe771b53d02019-10-22 10:25:58 -0600812 wqe->flags &= ~IO_WQE_FLAG_STALLED;
Jens Axboea9a4aa92021-08-30 06:33:08 -0600813 raw_spin_unlock(&wqe->lock);
Jens Axboe771b53d02019-10-22 10:25:58 -0600814
Jens Axboe895e2ca2019-12-17 08:46:33 -0700815 if ((work_flags & IO_WQ_WORK_CONCURRENT) ||
816 !atomic_read(&acct->nr_running))
Jens Axboec5def4a2019-11-07 11:41:16 -0700817 io_wqe_wake_worker(wqe, acct);
Jens Axboe771b53d02019-10-22 10:25:58 -0600818}
819
820void io_wq_enqueue(struct io_wq *wq, struct io_wq_work *work)
821{
822 struct io_wqe *wqe = wq->wqes[numa_node_id()];
823
824 io_wqe_enqueue(wqe, work);
825}
826
827/*
Pavel Begunkov8766dd52020-03-14 00:31:04 +0300828 * Work items that hash to the same value will not be done in parallel.
829 * Used to limit concurrent writes, generally hashed by inode.
Jens Axboe771b53d02019-10-22 10:25:58 -0600830 */
Pavel Begunkov8766dd52020-03-14 00:31:04 +0300831void io_wq_hash_work(struct io_wq_work *work, void *val)
Jens Axboe771b53d02019-10-22 10:25:58 -0600832{
Pavel Begunkov8766dd52020-03-14 00:31:04 +0300833 unsigned int bit;
Jens Axboe771b53d02019-10-22 10:25:58 -0600834
835 bit = hash_ptr(val, IO_WQ_HASH_ORDER);
836 work->flags |= (IO_WQ_WORK_HASHED | (bit << IO_WQ_HASH_SHIFT));
Jens Axboe771b53d02019-10-22 10:25:58 -0600837}
838
Pavel Begunkov2293b412020-03-07 01:15:39 +0300839static bool io_wq_worker_cancel(struct io_worker *worker, void *data)
Jens Axboe62755e32019-10-28 21:49:21 -0600840{
Pavel Begunkov2293b412020-03-07 01:15:39 +0300841 struct io_cb_cancel_data *match = data;
Jens Axboe62755e32019-10-28 21:49:21 -0600842
843 /*
844 * Hold the lock to avoid ->cur_work going out of scope, caller
Jens Axboe36c2f922019-11-13 09:43:34 -0700845 * may dereference the passed in work.
Jens Axboe62755e32019-10-28 21:49:21 -0600846 */
Jens Axboea9a4aa92021-08-30 06:33:08 -0600847 spin_lock(&worker->lock);
Jens Axboe62755e32019-10-28 21:49:21 -0600848 if (worker->cur_work &&
Pavel Begunkov2293b412020-03-07 01:15:39 +0300849 match->fn(worker->cur_work, match->data)) {
Jens Axboe3bfe6102021-02-16 14:15:30 -0700850 set_notify_signal(worker->task);
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300851 match->nr_running++;
Jens Axboe771b53d02019-10-22 10:25:58 -0600852 }
Jens Axboea9a4aa92021-08-30 06:33:08 -0600853 spin_unlock(&worker->lock);
Jens Axboe771b53d02019-10-22 10:25:58 -0600854
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300855 return match->nr_running && !match->cancel_all;
Jens Axboe771b53d02019-10-22 10:25:58 -0600856}
857
Pavel Begunkov204361a2020-08-23 20:33:10 +0300858static inline void io_wqe_remove_pending(struct io_wqe *wqe,
859 struct io_wq_work *work,
860 struct io_wq_work_node *prev)
861{
862 unsigned int hash = io_get_work_hash(work);
863 struct io_wq_work *prev_work = NULL;
864
865 if (io_wq_is_hashed(work) && work == wqe->hash_tail[hash]) {
866 if (prev)
867 prev_work = container_of(prev, struct io_wq_work, list);
868 if (prev_work && io_get_work_hash(prev_work) == hash)
869 wqe->hash_tail[hash] = prev_work;
870 else
871 wqe->hash_tail[hash] = NULL;
872 }
873 wq_list_del(&wqe->work_list, &work->list, prev);
874}
875
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300876static void io_wqe_cancel_pending_work(struct io_wqe *wqe,
Pavel Begunkovf4c26652020-06-15 10:24:02 +0300877 struct io_cb_cancel_data *match)
Jens Axboe771b53d02019-10-22 10:25:58 -0600878{
Jens Axboe6206f0e2019-11-26 11:59:32 -0700879 struct io_wq_work_node *node, *prev;
Jens Axboe771b53d02019-10-22 10:25:58 -0600880 struct io_wq_work *work;
Jens Axboe771b53d02019-10-22 10:25:58 -0600881
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300882retry:
Jens Axboea9a4aa92021-08-30 06:33:08 -0600883 raw_spin_lock(&wqe->lock);
Jens Axboe6206f0e2019-11-26 11:59:32 -0700884 wq_list_for_each(node, prev, &wqe->work_list) {
885 work = container_of(node, struct io_wq_work, list);
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300886 if (!match->fn(work, match->data))
887 continue;
Pavel Begunkov204361a2020-08-23 20:33:10 +0300888 io_wqe_remove_pending(wqe, work, prev);
Jens Axboea9a4aa92021-08-30 06:33:08 -0600889 raw_spin_unlock(&wqe->lock);
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300890 io_run_cancel(work, wqe);
891 match->nr_pending++;
892 if (!match->cancel_all)
893 return;
894
895 /* not safe to continue after unlock */
896 goto retry;
Jens Axboe771b53d02019-10-22 10:25:58 -0600897 }
Jens Axboea9a4aa92021-08-30 06:33:08 -0600898 raw_spin_unlock(&wqe->lock);
Pavel Begunkovf4c26652020-06-15 10:24:02 +0300899}
Jens Axboe771b53d02019-10-22 10:25:58 -0600900
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300901static void io_wqe_cancel_running_work(struct io_wqe *wqe,
Pavel Begunkovf4c26652020-06-15 10:24:02 +0300902 struct io_cb_cancel_data *match)
903{
Jens Axboe771b53d02019-10-22 10:25:58 -0600904 rcu_read_lock();
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300905 io_wq_for_each_worker(wqe, io_wq_worker_cancel, match);
Jens Axboe771b53d02019-10-22 10:25:58 -0600906 rcu_read_unlock();
Jens Axboe771b53d02019-10-22 10:25:58 -0600907}
908
Pavel Begunkov2293b412020-03-07 01:15:39 +0300909enum io_wq_cancel io_wq_cancel_cb(struct io_wq *wq, work_cancel_fn *cancel,
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300910 void *data, bool cancel_all)
Pavel Begunkov2293b412020-03-07 01:15:39 +0300911{
912 struct io_cb_cancel_data match = {
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300913 .fn = cancel,
914 .data = data,
915 .cancel_all = cancel_all,
Pavel Begunkov2293b412020-03-07 01:15:39 +0300916 };
Pavel Begunkov2293b412020-03-07 01:15:39 +0300917 int node;
918
Pavel Begunkovf4c26652020-06-15 10:24:02 +0300919 /*
920 * First check pending list, if we're lucky we can just remove it
921 * from there. CANCEL_OK means that the work is returned as-new,
922 * no completion will be posted for it.
923 */
Pavel Begunkov2293b412020-03-07 01:15:39 +0300924 for_each_node(node) {
925 struct io_wqe *wqe = wq->wqes[node];
926
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300927 io_wqe_cancel_pending_work(wqe, &match);
928 if (match.nr_pending && !match.cancel_all)
Pavel Begunkovf4c26652020-06-15 10:24:02 +0300929 return IO_WQ_CANCEL_OK;
Pavel Begunkov2293b412020-03-07 01:15:39 +0300930 }
931
Pavel Begunkovf4c26652020-06-15 10:24:02 +0300932 /*
933 * Now check if a free (going busy) or busy worker has the work
934 * currently running. If we find it there, we'll return CANCEL_RUNNING
935 * as an indication that we attempt to signal cancellation. The
936 * completion will run normally in this case.
937 */
938 for_each_node(node) {
939 struct io_wqe *wqe = wq->wqes[node];
940
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300941 io_wqe_cancel_running_work(wqe, &match);
942 if (match.nr_running && !match.cancel_all)
Pavel Begunkovf4c26652020-06-15 10:24:02 +0300943 return IO_WQ_CANCEL_RUNNING;
944 }
945
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300946 if (match.nr_running)
947 return IO_WQ_CANCEL_RUNNING;
948 if (match.nr_pending)
949 return IO_WQ_CANCEL_OK;
Pavel Begunkovf4c26652020-06-15 10:24:02 +0300950 return IO_WQ_CANCEL_NOTFOUND;
Pavel Begunkov2293b412020-03-07 01:15:39 +0300951}
952
Jens Axboee9418942021-02-19 12:33:30 -0700953static int io_wqe_hash_wake(struct wait_queue_entry *wait, unsigned mode,
954 int sync, void *key)
955{
956 struct io_wqe *wqe = container_of(wait, struct io_wqe, wait);
Jens Axboee9418942021-02-19 12:33:30 -0700957
958 list_del_init(&wait->entry);
959
960 rcu_read_lock();
Jens Axboe685fe7f2021-03-08 09:37:51 -0700961 io_wqe_activate_free_worker(wqe);
Jens Axboee9418942021-02-19 12:33:30 -0700962 rcu_read_unlock();
Jens Axboee9418942021-02-19 12:33:30 -0700963 return 1;
964}
965
Jens Axboe576a3472019-11-25 08:49:20 -0700966struct io_wq *io_wq_create(unsigned bounded, struct io_wq_data *data)
Jens Axboe771b53d02019-10-22 10:25:58 -0600967{
Colin Ian Kingb1b2fc32021-06-15 15:34:24 +0100968 int ret, node;
Jens Axboe771b53d02019-10-22 10:25:58 -0600969 struct io_wq *wq;
970
Pavel Begunkovf5fa38c2020-06-08 21:08:20 +0300971 if (WARN_ON_ONCE(!data->free_work || !data->do_work))
Pavel Begunkove9fd9392020-03-04 16:14:12 +0300972 return ERR_PTR(-EINVAL);
Pavel Begunkove6ab8992021-06-17 18:13:59 +0100973 if (WARN_ON_ONCE(!bounded))
974 return ERR_PTR(-EINVAL);
Pavel Begunkove9fd9392020-03-04 16:14:12 +0300975
Pavel Begunkovc7f405d2021-06-14 02:36:12 +0100976 wq = kzalloc(struct_size(wq, wqes, nr_node_ids), GFP_KERNEL);
Jens Axboe771b53d02019-10-22 10:25:58 -0600977 if (!wq)
978 return ERR_PTR(-ENOMEM);
Jens Axboe43c01fb2020-10-22 09:02:50 -0600979 ret = cpuhp_state_add_instance_nocalls(io_wq_online, &wq->cpuhp_node);
980 if (ret)
Pavel Begunkovc7f405d2021-06-14 02:36:12 +0100981 goto err_wq;
Jens Axboe771b53d02019-10-22 10:25:58 -0600982
Jens Axboee9418942021-02-19 12:33:30 -0700983 refcount_inc(&data->hash->refs);
984 wq->hash = data->hash;
Pavel Begunkove9fd9392020-03-04 16:14:12 +0300985 wq->free_work = data->free_work;
Pavel Begunkovf5fa38c2020-06-08 21:08:20 +0300986 wq->do_work = data->do_work;
Jens Axboe7d723062019-11-12 22:31:31 -0700987
Jens Axboe43c01fb2020-10-22 09:02:50 -0600988 ret = -ENOMEM;
Jann Horn3fc50ab2019-11-26 19:10:20 +0100989 for_each_node(node) {
Jens Axboe771b53d02019-10-22 10:25:58 -0600990 struct io_wqe *wqe;
Jens Axboe75634392020-02-11 06:30:06 -0700991 int alloc_node = node;
Jens Axboe771b53d02019-10-22 10:25:58 -0600992
Jens Axboe75634392020-02-11 06:30:06 -0700993 if (!node_online(alloc_node))
994 alloc_node = NUMA_NO_NODE;
995 wqe = kzalloc_node(sizeof(struct io_wqe), GFP_KERNEL, alloc_node);
Jens Axboe771b53d02019-10-22 10:25:58 -0600996 if (!wqe)
Jann Horn3fc50ab2019-11-26 19:10:20 +0100997 goto err;
Jens Axboe0e034962021-06-17 10:08:11 -0600998 if (!alloc_cpumask_var(&wqe->cpu_mask, GFP_KERNEL))
999 goto err;
1000 cpumask_copy(wqe->cpu_mask, cpumask_of_node(node));
Jann Horn3fc50ab2019-11-26 19:10:20 +01001001 wq->wqes[node] = wqe;
Jens Axboe75634392020-02-11 06:30:06 -07001002 wqe->node = alloc_node;
Jens Axboe685fe7f2021-03-08 09:37:51 -07001003 wqe->acct[IO_WQ_ACCT_BOUND].index = IO_WQ_ACCT_BOUND;
1004 wqe->acct[IO_WQ_ACCT_UNBOUND].index = IO_WQ_ACCT_UNBOUND;
Jens Axboec5def4a2019-11-07 11:41:16 -07001005 wqe->acct[IO_WQ_ACCT_BOUND].max_workers = bounded;
1006 atomic_set(&wqe->acct[IO_WQ_ACCT_BOUND].nr_running, 0);
Jens Axboe728f13e2021-02-21 16:02:53 -07001007 wqe->acct[IO_WQ_ACCT_UNBOUND].max_workers =
Jens Axboec5def4a2019-11-07 11:41:16 -07001008 task_rlimit(current, RLIMIT_NPROC);
Jens Axboec5def4a2019-11-07 11:41:16 -07001009 atomic_set(&wqe->acct[IO_WQ_ACCT_UNBOUND].nr_running, 0);
Jens Axboee9418942021-02-19 12:33:30 -07001010 wqe->wait.func = io_wqe_hash_wake;
1011 INIT_LIST_HEAD(&wqe->wait.entry);
Jens Axboe771b53d02019-10-22 10:25:58 -06001012 wqe->wq = wq;
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +02001013 raw_spin_lock_init(&wqe->lock);
Jens Axboe6206f0e2019-11-26 11:59:32 -07001014 INIT_WQ_LIST(&wqe->work_list);
Jens Axboe021d1cd2019-11-14 08:00:41 -07001015 INIT_HLIST_NULLS_HEAD(&wqe->free_list, 0);
Jens Axboee61df662019-11-13 13:54:49 -07001016 INIT_LIST_HEAD(&wqe->all_list);
Jens Axboe771b53d02019-10-22 10:25:58 -06001017 }
1018
Jens Axboe685fe7f2021-03-08 09:37:51 -07001019 wq->task = get_task_struct(data->task);
Jens Axboe685fe7f2021-03-08 09:37:51 -07001020 atomic_set(&wq->worker_refs, 1);
1021 init_completion(&wq->worker_done);
1022 return wq;
Jens Axboeb60fda62019-11-19 08:37:07 -07001023err:
Jens Axboedc7bbc92021-03-01 09:09:56 -07001024 io_wq_put_hash(data->hash);
Jens Axboe43c01fb2020-10-22 09:02:50 -06001025 cpuhp_state_remove_instance_nocalls(io_wq_online, &wq->cpuhp_node);
Jens Axboe0e034962021-06-17 10:08:11 -06001026 for_each_node(node) {
1027 if (!wq->wqes[node])
1028 continue;
1029 free_cpumask_var(wq->wqes[node]->cpu_mask);
Jann Horn3fc50ab2019-11-26 19:10:20 +01001030 kfree(wq->wqes[node]);
Jens Axboe0e034962021-06-17 10:08:11 -06001031 }
Jens Axboe43c01fb2020-10-22 09:02:50 -06001032err_wq:
Jens Axboeb60fda62019-11-19 08:37:07 -07001033 kfree(wq);
Jens Axboe771b53d02019-10-22 10:25:58 -06001034 return ERR_PTR(ret);
1035}
1036
Jens Axboec80ca472021-04-01 19:57:07 -06001037static bool io_task_work_match(struct callback_head *cb, void *data)
1038{
Jens Axboed3e9f732021-08-04 08:37:25 -06001039 struct io_worker *worker;
Jens Axboec80ca472021-04-01 19:57:07 -06001040
1041 if (cb->func != create_worker_cb)
1042 return false;
Jens Axboed3e9f732021-08-04 08:37:25 -06001043 worker = container_of(cb, struct io_worker, create_work);
1044 return worker->wqe->wq == data;
Jens Axboec80ca472021-04-01 19:57:07 -06001045}
1046
Pavel Begunkov17a91052021-05-23 15:48:39 +01001047void io_wq_exit_start(struct io_wq *wq)
1048{
1049 set_bit(IO_WQ_BIT_EXIT, &wq->state);
1050}
1051
Jens Axboe685fe7f2021-03-08 09:37:51 -07001052static void io_wq_exit_workers(struct io_wq *wq)
Jens Axboeafcc4012021-02-26 13:48:19 -07001053{
Jens Axboe685fe7f2021-03-08 09:37:51 -07001054 struct callback_head *cb;
1055 int node;
1056
Jens Axboe685fe7f2021-03-08 09:37:51 -07001057 if (!wq->task)
1058 return;
1059
Jens Axboec80ca472021-04-01 19:57:07 -06001060 while ((cb = task_work_cancel_match(wq->task, io_task_work_match, wq)) != NULL) {
Jens Axboed3e9f732021-08-04 08:37:25 -06001061 struct io_worker *worker;
Jens Axboe685fe7f2021-03-08 09:37:51 -07001062
Jens Axboed3e9f732021-08-04 08:37:25 -06001063 worker = container_of(cb, struct io_worker, create_work);
1064 atomic_dec(&worker->wqe->acct[worker->create_index].nr_running);
Jens Axboe685fe7f2021-03-08 09:37:51 -07001065 io_worker_ref_put(wq);
Jens Axboed3e9f732021-08-04 08:37:25 -06001066 clear_bit_unlock(0, &worker->create_state);
1067 io_worker_release(worker);
Jens Axboeafcc4012021-02-26 13:48:19 -07001068 }
Jens Axboe685fe7f2021-03-08 09:37:51 -07001069
1070 rcu_read_lock();
1071 for_each_node(node) {
1072 struct io_wqe *wqe = wq->wqes[node];
1073
1074 io_wq_for_each_worker(wqe, io_wq_worker_wake, NULL);
Jens Axboe685fe7f2021-03-08 09:37:51 -07001075 }
1076 rcu_read_unlock();
1077 io_worker_ref_put(wq);
1078 wait_for_completion(&wq->worker_done);
Zqiang3743c172021-05-26 13:08:26 +08001079
1080 for_each_node(node) {
1081 spin_lock_irq(&wq->hash->wait.lock);
1082 list_del_init(&wq->wqes[node]->wait.entry);
1083 spin_unlock_irq(&wq->hash->wait.lock);
1084 }
Jens Axboe685fe7f2021-03-08 09:37:51 -07001085 put_task_struct(wq->task);
1086 wq->task = NULL;
Jens Axboeafcc4012021-02-26 13:48:19 -07001087}
1088
Jens Axboe4fb6ac32021-02-25 10:17:09 -07001089static void io_wq_destroy(struct io_wq *wq)
Jens Axboe771b53d02019-10-22 10:25:58 -06001090{
Jann Horn3fc50ab2019-11-26 19:10:20 +01001091 int node;
Jens Axboe771b53d02019-10-22 10:25:58 -06001092
Jens Axboe43c01fb2020-10-22 09:02:50 -06001093 cpuhp_state_remove_instance_nocalls(io_wq_online, &wq->cpuhp_node);
1094
Jens Axboee9418942021-02-19 12:33:30 -07001095 for_each_node(node) {
1096 struct io_wqe *wqe = wq->wqes[node];
Jens Axboef5d2d232021-03-25 10:16:12 -06001097 struct io_cb_cancel_data match = {
1098 .fn = io_wq_work_match_all,
1099 .cancel_all = true,
1100 };
1101 io_wqe_cancel_pending_work(wqe, &match);
Jens Axboe0e034962021-06-17 10:08:11 -06001102 free_cpumask_var(wqe->cpu_mask);
Jens Axboee9418942021-02-19 12:33:30 -07001103 kfree(wqe);
1104 }
Jens Axboee9418942021-02-19 12:33:30 -07001105 io_wq_put_hash(wq->hash);
Jens Axboe771b53d02019-10-22 10:25:58 -06001106 kfree(wq);
Jens Axboe4fb6ac32021-02-25 10:17:09 -07001107}
1108
Jens Axboeafcc4012021-02-26 13:48:19 -07001109void io_wq_put_and_exit(struct io_wq *wq)
1110{
Pavel Begunkov17a91052021-05-23 15:48:39 +01001111 WARN_ON_ONCE(!test_bit(IO_WQ_BIT_EXIT, &wq->state));
1112
Jens Axboe685fe7f2021-03-08 09:37:51 -07001113 io_wq_exit_workers(wq);
Pavel Begunkov382cb032021-06-14 02:36:13 +01001114 io_wq_destroy(wq);
Jens Axboeafcc4012021-02-26 13:48:19 -07001115}
1116
Jens Axboe0e034962021-06-17 10:08:11 -06001117struct online_data {
1118 unsigned int cpu;
1119 bool online;
1120};
1121
Jens Axboe43c01fb2020-10-22 09:02:50 -06001122static bool io_wq_worker_affinity(struct io_worker *worker, void *data)
1123{
Jens Axboe0e034962021-06-17 10:08:11 -06001124 struct online_data *od = data;
Jens Axboe43c01fb2020-10-22 09:02:50 -06001125
Jens Axboe0e034962021-06-17 10:08:11 -06001126 if (od->online)
1127 cpumask_set_cpu(od->cpu, worker->wqe->cpu_mask);
1128 else
1129 cpumask_clear_cpu(od->cpu, worker->wqe->cpu_mask);
Jens Axboe43c01fb2020-10-22 09:02:50 -06001130 return false;
1131}
1132
Jens Axboe0e034962021-06-17 10:08:11 -06001133static int __io_wq_cpu_online(struct io_wq *wq, unsigned int cpu, bool online)
1134{
1135 struct online_data od = {
1136 .cpu = cpu,
1137 .online = online
1138 };
1139 int i;
1140
1141 rcu_read_lock();
1142 for_each_node(i)
1143 io_wq_for_each_worker(wq->wqes[i], io_wq_worker_affinity, &od);
1144 rcu_read_unlock();
1145 return 0;
1146}
1147
Jens Axboe43c01fb2020-10-22 09:02:50 -06001148static int io_wq_cpu_online(unsigned int cpu, struct hlist_node *node)
1149{
1150 struct io_wq *wq = hlist_entry_safe(node, struct io_wq, cpuhp_node);
Jens Axboe43c01fb2020-10-22 09:02:50 -06001151
Jens Axboe0e034962021-06-17 10:08:11 -06001152 return __io_wq_cpu_online(wq, cpu, true);
1153}
1154
1155static int io_wq_cpu_offline(unsigned int cpu, struct hlist_node *node)
1156{
1157 struct io_wq *wq = hlist_entry_safe(node, struct io_wq, cpuhp_node);
1158
1159 return __io_wq_cpu_online(wq, cpu, false);
Jens Axboe43c01fb2020-10-22 09:02:50 -06001160}
1161
Jens Axboefe764212021-06-17 10:19:54 -06001162int io_wq_cpu_affinity(struct io_wq *wq, cpumask_var_t mask)
1163{
1164 int i;
1165
1166 rcu_read_lock();
1167 for_each_node(i) {
1168 struct io_wqe *wqe = wq->wqes[i];
1169
1170 if (mask)
1171 cpumask_copy(wqe->cpu_mask, mask);
1172 else
1173 cpumask_copy(wqe->cpu_mask, cpumask_of_node(i));
1174 }
1175 rcu_read_unlock();
1176 return 0;
1177}
1178
Jens Axboe2e480052021-08-27 11:33:19 -06001179/*
1180 * Set max number of unbounded workers, returns old value. If new_count is 0,
1181 * then just return the old value.
1182 */
1183int io_wq_max_workers(struct io_wq *wq, int *new_count)
1184{
1185 int i, node, prev = 0;
1186
1187 for (i = 0; i < 2; i++) {
1188 if (new_count[i] > task_rlimit(current, RLIMIT_NPROC))
1189 new_count[i] = task_rlimit(current, RLIMIT_NPROC);
1190 }
1191
1192 rcu_read_lock();
1193 for_each_node(node) {
1194 struct io_wqe_acct *acct;
1195
1196 for (i = 0; i < 2; i++) {
1197 acct = &wq->wqes[node]->acct[i];
1198 prev = max_t(int, acct->max_workers, prev);
1199 if (new_count[i])
1200 acct->max_workers = new_count[i];
1201 new_count[i] = prev;
1202 }
1203 }
1204 rcu_read_unlock();
1205 return 0;
1206}
1207
Jens Axboe43c01fb2020-10-22 09:02:50 -06001208static __init int io_wq_init(void)
1209{
1210 int ret;
1211
1212 ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "io-wq/online",
Jens Axboe0e034962021-06-17 10:08:11 -06001213 io_wq_cpu_online, io_wq_cpu_offline);
Jens Axboe43c01fb2020-10-22 09:02:50 -06001214 if (ret < 0)
1215 return ret;
1216 io_wq_online = ret;
1217 return 0;
1218}
1219subsys_initcall(io_wq_init);