blob: 6c55362c1f99a52e010f9c62a65d18670dc24184 [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 Axboe05c5f4e2021-09-01 13:01:17 -060026 IO_WORKER_F_BOUND = 8, /* is doing bounded work */
Jens Axboe771b53d02019-10-22 10:25:58 -060027};
28
29enum {
30 IO_WQ_BIT_EXIT = 0, /* wq exiting */
Jens Axboe771b53d02019-10-22 10:25:58 -060031};
32
33enum {
Jens Axboef95dc202021-08-31 13:57:32 -060034 IO_ACCT_STALLED_BIT = 0, /* stalled on hash */
Jens Axboe771b53d02019-10-22 10:25:58 -060035};
36
37/*
38 * One for each thread in a wqe pool
39 */
40struct io_worker {
41 refcount_t ref;
42 unsigned flags;
43 struct hlist_nulls_node nulls_node;
Jens Axboee61df662019-11-13 13:54:49 -070044 struct list_head all_list;
Jens Axboe771b53d02019-10-22 10:25:58 -060045 struct task_struct *task;
Jens Axboe771b53d02019-10-22 10:25:58 -060046 struct io_wqe *wqe;
Jens Axboe36c2f922019-11-13 09:43:34 -070047
Jens Axboe771b53d02019-10-22 10:25:58 -060048 struct io_wq_work *cur_work;
Jens Axboe36c2f922019-11-13 09:43:34 -070049 spinlock_t lock;
Jens Axboe771b53d02019-10-22 10:25:58 -060050
Jens Axboeeb2de942021-02-23 19:59:06 -070051 struct completion ref_done;
52
Jens Axboed3e9f732021-08-04 08:37:25 -060053 unsigned long create_state;
54 struct callback_head create_work;
55 int create_index;
56
Jens Axboe3146cba2021-09-01 11:20:10 -060057 union {
58 struct rcu_head rcu;
59 struct work_struct work;
60 };
Jens Axboe771b53d02019-10-22 10:25:58 -060061};
62
Jens Axboe771b53d02019-10-22 10:25:58 -060063#if BITS_PER_LONG == 64
64#define IO_WQ_HASH_ORDER 6
65#else
66#define IO_WQ_HASH_ORDER 5
67#endif
68
Pavel Begunkov86f3cd12020-03-23 22:57:22 +030069#define IO_WQ_NR_HASH_BUCKETS (1u << IO_WQ_HASH_ORDER)
70
Jens Axboec5def4a2019-11-07 11:41:16 -070071struct io_wqe_acct {
72 unsigned nr_workers;
73 unsigned max_workers;
Jens Axboe685fe7f2021-03-08 09:37:51 -070074 int index;
Jens Axboec5def4a2019-11-07 11:41:16 -070075 atomic_t nr_running;
Jens Axboef95dc202021-08-31 13:57:32 -060076 struct io_wq_work_list work_list;
77 unsigned long flags;
Jens Axboec5def4a2019-11-07 11:41:16 -070078};
79
80enum {
81 IO_WQ_ACCT_BOUND,
82 IO_WQ_ACCT_UNBOUND,
Jens Axboef95dc202021-08-31 13:57:32 -060083 IO_WQ_ACCT_NR,
Jens Axboec5def4a2019-11-07 11:41:16 -070084};
85
Jens Axboe771b53d02019-10-22 10:25:58 -060086/*
87 * Per-node worker thread pool
88 */
89struct io_wqe {
Jens Axboef95dc202021-08-31 13:57:32 -060090 raw_spinlock_t lock;
91 struct io_wqe_acct acct[2];
Jens Axboe771b53d02019-10-22 10:25:58 -060092
93 int node;
Jens Axboe771b53d02019-10-22 10:25:58 -060094
Jens Axboe021d1cd2019-11-14 08:00:41 -070095 struct hlist_nulls_head free_list;
Jens Axboee61df662019-11-13 13:54:49 -070096 struct list_head all_list;
Jens Axboe771b53d02019-10-22 10:25:58 -060097
Jens Axboee9418942021-02-19 12:33:30 -070098 struct wait_queue_entry wait;
99
Jens Axboe771b53d02019-10-22 10:25:58 -0600100 struct io_wq *wq;
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300101 struct io_wq_work *hash_tail[IO_WQ_NR_HASH_BUCKETS];
Jens Axboe0e034962021-06-17 10:08:11 -0600102
103 cpumask_var_t cpu_mask;
Jens Axboe771b53d02019-10-22 10:25:58 -0600104};
105
106/*
107 * Per io_wq state
108 */
109struct io_wq {
Jens Axboe771b53d02019-10-22 10:25:58 -0600110 unsigned long state;
Jens Axboe771b53d02019-10-22 10:25:58 -0600111
Pavel Begunkove9fd9392020-03-04 16:14:12 +0300112 free_work_fn *free_work;
Pavel Begunkovf5fa38c2020-06-08 21:08:20 +0300113 io_wq_work_fn *do_work;
Jens Axboe7d723062019-11-12 22:31:31 -0700114
Jens Axboee9418942021-02-19 12:33:30 -0700115 struct io_wq_hash *hash;
116
Jens Axboefb3a1f62021-02-26 09:47:20 -0700117 atomic_t worker_refs;
118 struct completion worker_done;
119
Jens Axboe43c01fb2020-10-22 09:02:50 -0600120 struct hlist_node cpuhp_node;
Jens Axboe3bfe6102021-02-16 14:15:30 -0700121
Jens Axboe685fe7f2021-03-08 09:37:51 -0700122 struct task_struct *task;
Pavel Begunkovc7f405d2021-06-14 02:36:12 +0100123
124 struct io_wqe *wqes[];
Jens Axboe771b53d02019-10-22 10:25:58 -0600125};
126
Jens Axboe43c01fb2020-10-22 09:02:50 -0600127static enum cpuhp_state io_wq_online;
128
Jens Axboef0127252021-03-03 15:47:04 -0700129struct io_cb_cancel_data {
130 work_cancel_fn *fn;
131 void *data;
132 int nr_running;
133 int nr_pending;
134 bool cancel_all;
135};
136
Jens Axboe3146cba2021-09-01 11:20:10 -0600137static bool create_io_worker(struct io_wq *wq, struct io_wqe *wqe, int index);
Jens Axboe83d6c392021-08-03 09:14:35 -0600138static void io_wqe_dec_running(struct io_worker *worker);
Jens Axboe3146cba2021-09-01 11:20:10 -0600139static bool io_acct_cancel_pending_work(struct io_wqe *wqe,
140 struct io_wqe_acct *acct,
141 struct io_cb_cancel_data *match);
Jens Axboef0127252021-03-03 15:47:04 -0700142
Jens Axboe771b53d02019-10-22 10:25:58 -0600143static bool io_worker_get(struct io_worker *worker)
144{
145 return refcount_inc_not_zero(&worker->ref);
146}
147
148static void io_worker_release(struct io_worker *worker)
149{
150 if (refcount_dec_and_test(&worker->ref))
Jens Axboeeb2de942021-02-23 19:59:06 -0700151 complete(&worker->ref_done);
Jens Axboe771b53d02019-10-22 10:25:58 -0600152}
153
Pavel Begunkov8418f222021-03-22 01:58:28 +0000154static inline struct io_wqe_acct *io_get_acct(struct io_wqe *wqe, bool bound)
155{
156 return &wqe->acct[bound ? IO_WQ_ACCT_BOUND : IO_WQ_ACCT_UNBOUND];
157}
158
Jens Axboec5def4a2019-11-07 11:41:16 -0700159static inline struct io_wqe_acct *io_work_get_acct(struct io_wqe *wqe,
160 struct io_wq_work *work)
161{
Pavel Begunkov8418f222021-03-22 01:58:28 +0000162 return io_get_acct(wqe, !(work->flags & IO_WQ_WORK_UNBOUND));
Jens Axboec5def4a2019-11-07 11:41:16 -0700163}
164
Jens Axboe958234d2021-02-17 09:00:57 -0700165static inline struct io_wqe_acct *io_wqe_get_acct(struct io_worker *worker)
Jens Axboec5def4a2019-11-07 11:41:16 -0700166{
Pavel Begunkov8418f222021-03-22 01:58:28 +0000167 return io_get_acct(worker->wqe, worker->flags & IO_WORKER_F_BOUND);
Jens Axboec5def4a2019-11-07 11:41:16 -0700168}
169
Jens Axboe685fe7f2021-03-08 09:37:51 -0700170static void io_worker_ref_put(struct io_wq *wq)
171{
172 if (atomic_dec_and_test(&wq->worker_refs))
173 complete(&wq->worker_done);
174}
175
Jens Axboe771b53d02019-10-22 10:25:58 -0600176static void io_worker_exit(struct io_worker *worker)
177{
178 struct io_wqe *wqe = worker->wqe;
Jens Axboe958234d2021-02-17 09:00:57 -0700179 struct io_wqe_acct *acct = io_wqe_get_acct(worker);
Jens Axboe771b53d02019-10-22 10:25:58 -0600180
Jens Axboeeb2de942021-02-23 19:59:06 -0700181 if (refcount_dec_and_test(&worker->ref))
182 complete(&worker->ref_done);
183 wait_for_completion(&worker->ref_done);
Jens Axboe771b53d02019-10-22 10:25:58 -0600184
Jens Axboea9a4aa92021-08-30 06:33:08 -0600185 raw_spin_lock(&wqe->lock);
Jens Axboe83d6c392021-08-03 09:14:35 -0600186 if (worker->flags & IO_WORKER_F_FREE)
Jens Axboebf1daa42021-02-16 18:00:55 -0700187 hlist_nulls_del_rcu(&worker->nulls_node);
Jens Axboee61df662019-11-13 13:54:49 -0700188 list_del_rcu(&worker->all_list);
Jens Axboec5def4a2019-11-07 11:41:16 -0700189 acct->nr_workers--;
Jens Axboe83d6c392021-08-03 09:14:35 -0600190 preempt_disable();
191 io_wqe_dec_running(worker);
192 worker->flags = 0;
193 current->flags &= ~PF_IO_WORKER;
194 preempt_enable();
Jens Axboea9a4aa92021-08-30 06:33:08 -0600195 raw_spin_unlock(&wqe->lock);
Jens Axboe771b53d02019-10-22 10:25:58 -0600196
YueHaibing364b05f2019-11-02 15:55:01 +0800197 kfree_rcu(worker, rcu);
Jens Axboe685fe7f2021-03-08 09:37:51 -0700198 io_worker_ref_put(wqe->wq);
Jens Axboe46fe18b2021-03-04 12:39:36 -0700199 do_exit(0);
Jens Axboe771b53d02019-10-22 10:25:58 -0600200}
201
Jens Axboef95dc202021-08-31 13:57:32 -0600202static inline bool io_acct_run_queue(struct io_wqe_acct *acct)
Jens Axboec5def4a2019-11-07 11:41:16 -0700203{
Jens Axboef95dc202021-08-31 13:57:32 -0600204 if (!wq_list_empty(&acct->work_list) &&
205 !test_bit(IO_ACCT_STALLED_BIT, &acct->flags))
Jens Axboec5def4a2019-11-07 11:41:16 -0700206 return true;
207 return false;
208}
209
210/*
211 * Check head of free list for an available worker. If one isn't available,
Jens Axboe685fe7f2021-03-08 09:37:51 -0700212 * caller must create one.
Jens Axboec5def4a2019-11-07 11:41:16 -0700213 */
Jens Axboef95dc202021-08-31 13:57:32 -0600214static bool io_wqe_activate_free_worker(struct io_wqe *wqe,
215 struct io_wqe_acct *acct)
Jens Axboec5def4a2019-11-07 11:41:16 -0700216 __must_hold(RCU)
217{
218 struct hlist_nulls_node *n;
219 struct io_worker *worker;
220
Jens Axboe83d6c392021-08-03 09:14:35 -0600221 /*
222 * Iterate free_list and see if we can find an idle worker to
223 * activate. If a given worker is on the free_list but in the process
224 * of exiting, keep trying.
225 */
226 hlist_nulls_for_each_entry_rcu(worker, n, &wqe->free_list, nulls_node) {
227 if (!io_worker_get(worker))
228 continue;
Jens Axboef95dc202021-08-31 13:57:32 -0600229 if (io_wqe_get_acct(worker) != acct) {
230 io_worker_release(worker);
231 continue;
232 }
Jens Axboe83d6c392021-08-03 09:14:35 -0600233 if (wake_up_process(worker->task)) {
234 io_worker_release(worker);
235 return true;
236 }
Jens Axboec5def4a2019-11-07 11:41:16 -0700237 io_worker_release(worker);
Jens Axboec5def4a2019-11-07 11:41:16 -0700238 }
239
240 return false;
241}
242
243/*
244 * 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 -0700245 * below the max number of workers, create one.
Jens Axboec5def4a2019-11-07 11:41:16 -0700246 */
Jens Axboe3146cba2021-09-01 11:20:10 -0600247static bool io_wqe_create_worker(struct io_wqe *wqe, struct io_wqe_acct *acct)
Jens Axboec5def4a2019-11-07 11:41:16 -0700248{
Jens Axboe05c5f4e2021-09-01 13:01:17 -0600249 bool do_create = false;
Jens Axboec5def4a2019-11-07 11:41:16 -0700250
251 /*
252 * Most likely an attempt to queue unbounded work on an io_wq that
253 * wasn't setup with any unbounded workers.
254 */
Pavel Begunkove6ab8992021-06-17 18:13:59 +0100255 if (unlikely(!acct->max_workers))
256 pr_warn_once("io-wq is not configured for unbound workers");
Jens Axboec5def4a2019-11-07 11:41:16 -0700257
Jens Axboe94ffb0a2021-08-30 11:55:22 -0600258 raw_spin_lock(&wqe->lock);
259 if (acct->nr_workers < acct->max_workers) {
Jens Axboe94ffb0a2021-08-30 11:55:22 -0600260 acct->nr_workers++;
261 do_create = true;
262 }
263 raw_spin_unlock(&wqe->lock);
264 if (do_create) {
265 atomic_inc(&acct->nr_running);
266 atomic_inc(&wqe->wq->worker_refs);
Jens Axboe3146cba2021-09-01 11:20:10 -0600267 return create_io_worker(wqe->wq, wqe, acct->index);
Jens Axboe685fe7f2021-03-08 09:37:51 -0700268 }
Jens Axboe3146cba2021-09-01 11:20:10 -0600269
270 return true;
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;
Jens Axboe05c5f4e2021-09-01 13:01:17 -0600286 bool do_create = 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 Xu21698272021-08-05 18:05:38 +0800294 acct->nr_workers++;
Hao Xu49e7f0c2021-08-08 21:54:33 +0800295 do_create = true;
296 }
Jens Axboea9a4aa92021-08-30 06:33:08 -0600297 raw_spin_unlock(&wqe->lock);
Hao Xu49e7f0c2021-08-08 21:54:33 +0800298 if (do_create) {
Jens Axboe05c5f4e2021-09-01 13:01:17 -0600299 create_io_worker(wq, wqe, worker->create_index);
Hao Xu49e7f0c2021-08-08 21:54:33 +0800300 } else {
301 atomic_dec(&acct->nr_running);
302 io_worker_ref_put(wq);
303 }
Jens Axboed3e9f732021-08-04 08:37:25 -0600304 clear_bit_unlock(0, &worker->create_state);
305 io_worker_release(worker);
Jens Axboe685fe7f2021-03-08 09:37:51 -0700306}
307
Jens Axboe3146cba2021-09-01 11:20:10 -0600308static bool io_queue_worker_create(struct io_worker *worker,
309 struct io_wqe_acct *acct,
310 task_work_func_t func)
Jens Axboe685fe7f2021-03-08 09:37:51 -0700311{
Jens Axboe3146cba2021-09-01 11:20:10 -0600312 struct io_wqe *wqe = worker->wqe;
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 Axboe3146cba2021-09-01 11:20:10 -0600330 init_task_work(&worker->create_work, func);
Jens Axboed3e9f732021-08-04 08:37:25 -0600331 worker->create_index = acct->index;
332 if (!task_work_add(wq->task, &worker->create_work, TWA_SIGNAL))
Jens Axboe3146cba2021-09-01 11:20:10 -0600333 return true;
Jens Axboed3e9f732021-08-04 08:37:25 -0600334 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);
Jens Axboe3146cba2021-09-01 11:20:10 -0600340 return false;
Jens Axboe685fe7f2021-03-08 09:37:51 -0700341}
342
Jens Axboe958234d2021-02-17 09:00:57 -0700343static void io_wqe_dec_running(struct io_worker *worker)
Jens Axboec5def4a2019-11-07 11:41:16 -0700344 __must_hold(wqe->lock)
345{
Jens Axboe958234d2021-02-17 09:00:57 -0700346 struct io_wqe_acct *acct = io_wqe_get_acct(worker);
347 struct io_wqe *wqe = worker->wqe;
Jens Axboec5def4a2019-11-07 11:41:16 -0700348
Jens Axboe685fe7f2021-03-08 09:37:51 -0700349 if (!(worker->flags & IO_WORKER_F_UP))
350 return;
351
Jens Axboef95dc202021-08-31 13:57:32 -0600352 if (atomic_dec_and_test(&acct->nr_running) && io_acct_run_queue(acct)) {
Jens Axboe685fe7f2021-03-08 09:37:51 -0700353 atomic_inc(&acct->nr_running);
354 atomic_inc(&wqe->wq->worker_refs);
Jens Axboe3146cba2021-09-01 11:20:10 -0600355 io_queue_worker_create(worker, acct, create_worker_cb);
Jens Axboe685fe7f2021-03-08 09:37:51 -0700356 }
Jens Axboec5def4a2019-11-07 11:41:16 -0700357}
358
Jens Axboe771b53d02019-10-22 10:25:58 -0600359/*
360 * Worker will start processing some work. Move it to the busy list, if
361 * it's currently on the freelist
362 */
363static void __io_worker_busy(struct io_wqe *wqe, struct io_worker *worker,
364 struct io_wq_work *work)
365 __must_hold(wqe->lock)
366{
367 if (worker->flags & IO_WORKER_F_FREE) {
368 worker->flags &= ~IO_WORKER_F_FREE;
369 hlist_nulls_del_init_rcu(&worker->nulls_node);
Jens Axboe771b53d02019-10-22 10:25:58 -0600370 }
Jens Axboe771b53d02019-10-22 10:25:58 -0600371}
372
373/*
374 * No work, worker going to sleep. Move to freelist, and unuse mm if we
375 * have one attached. Dropping the mm may potentially sleep, so we drop
376 * the lock in that case and return success. Since the caller has to
377 * retry the loop in that case (we changed task state), we don't regrab
378 * the lock if we return success.
379 */
Jens Axboec6d77d92021-02-15 13:26:34 -0700380static void __io_worker_idle(struct io_wqe *wqe, struct io_worker *worker)
Jens Axboe771b53d02019-10-22 10:25:58 -0600381 __must_hold(wqe->lock)
382{
383 if (!(worker->flags & IO_WORKER_F_FREE)) {
384 worker->flags |= IO_WORKER_F_FREE;
Jens Axboe021d1cd2019-11-14 08:00:41 -0700385 hlist_nulls_add_head_rcu(&worker->nulls_node, &wqe->free_list);
Jens Axboe771b53d02019-10-22 10:25:58 -0600386 }
Jens Axboe771b53d02019-10-22 10:25:58 -0600387}
388
Pavel Begunkov60cf46a2020-03-14 00:31:05 +0300389static inline unsigned int io_get_work_hash(struct io_wq_work *work)
390{
391 return work->flags >> IO_WQ_HASH_SHIFT;
392}
393
Jens Axboee9418942021-02-19 12:33:30 -0700394static void io_wait_on_hash(struct io_wqe *wqe, unsigned int hash)
395{
396 struct io_wq *wq = wqe->wq;
397
Jens Axboe08bdbd32021-08-31 06:57:25 -0600398 spin_lock_irq(&wq->hash->wait.lock);
Jens Axboee9418942021-02-19 12:33:30 -0700399 if (list_empty(&wqe->wait.entry)) {
400 __add_wait_queue(&wq->hash->wait, &wqe->wait);
401 if (!test_bit(hash, &wq->hash->map)) {
402 __set_current_state(TASK_RUNNING);
403 list_del_init(&wqe->wait.entry);
404 }
405 }
Jens Axboe08bdbd32021-08-31 06:57:25 -0600406 spin_unlock_irq(&wq->hash->wait.lock);
Jens Axboee9418942021-02-19 12:33:30 -0700407}
408
Jens Axboef95dc202021-08-31 13:57:32 -0600409static struct io_wq_work *io_get_next_work(struct io_wqe_acct *acct,
Jens Axboe0242f642021-08-31 13:53:00 -0600410 struct io_worker *worker)
Jens Axboe771b53d02019-10-22 10:25:58 -0600411 __must_hold(wqe->lock)
412{
Jens Axboe6206f0e2019-11-26 11:59:32 -0700413 struct io_wq_work_node *node, *prev;
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300414 struct io_wq_work *work, *tail;
Jens Axboee9418942021-02-19 12:33:30 -0700415 unsigned int stall_hash = -1U;
Jens Axboef95dc202021-08-31 13:57:32 -0600416 struct io_wqe *wqe = worker->wqe;
Jens Axboe771b53d02019-10-22 10:25:58 -0600417
Jens Axboef95dc202021-08-31 13:57:32 -0600418 wq_list_for_each(node, prev, &acct->work_list) {
Jens Axboee9418942021-02-19 12:33:30 -0700419 unsigned int hash;
420
Jens Axboe6206f0e2019-11-26 11:59:32 -0700421 work = container_of(node, struct io_wq_work, list);
422
Jens Axboe771b53d02019-10-22 10:25:58 -0600423 /* not hashed, can run anytime */
Pavel Begunkov8766dd52020-03-14 00:31:04 +0300424 if (!io_wq_is_hashed(work)) {
Jens Axboef95dc202021-08-31 13:57:32 -0600425 wq_list_del(&acct->work_list, node, prev);
Jens Axboe771b53d02019-10-22 10:25:58 -0600426 return work;
427 }
428
Pavel Begunkov60cf46a2020-03-14 00:31:05 +0300429 hash = io_get_work_hash(work);
Jens Axboee9418942021-02-19 12:33:30 -0700430 /* all items with this hash lie in [work, tail] */
431 tail = wqe->hash_tail[hash];
432
433 /* hashed, can run if not already running */
434 if (!test_and_set_bit(hash, &wqe->wq->hash->map)) {
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300435 wqe->hash_tail[hash] = NULL;
Jens Axboef95dc202021-08-31 13:57:32 -0600436 wq_list_cut(&acct->work_list, &tail->list, prev);
Jens Axboe771b53d02019-10-22 10:25:58 -0600437 return work;
438 }
Jens Axboee9418942021-02-19 12:33:30 -0700439 if (stall_hash == -1U)
440 stall_hash = hash;
441 /* fast forward to a next hash, for-each will fix up @prev */
442 node = &tail->list;
443 }
444
445 if (stall_hash != -1U) {
Jens Axboe0242f642021-08-31 13:53:00 -0600446 /*
447 * Set this before dropping the lock to avoid racing with new
448 * work being added and clearing the stalled bit.
449 */
Jens Axboef95dc202021-08-31 13:57:32 -0600450 set_bit(IO_ACCT_STALLED_BIT, &acct->flags);
Jens Axboee9418942021-02-19 12:33:30 -0700451 raw_spin_unlock(&wqe->lock);
452 io_wait_on_hash(wqe, stall_hash);
453 raw_spin_lock(&wqe->lock);
Jens Axboe771b53d02019-10-22 10:25:58 -0600454 }
455
456 return NULL;
457}
458
Jens Axboe00ddff42021-03-21 07:06:56 -0600459static bool io_flush_signals(void)
Jens Axboecccf0ee2020-01-27 16:34:48 -0700460{
Jens Axboe0b8cfa92021-03-21 14:16:08 -0600461 if (unlikely(test_thread_flag(TIF_NOTIFY_SIGNAL))) {
Jens Axboe00ddff42021-03-21 07:06:56 -0600462 __set_current_state(TASK_RUNNING);
Jens Axboe0b8cfa92021-03-21 14:16:08 -0600463 tracehook_notify_signal();
Jens Axboe00ddff42021-03-21 07:06:56 -0600464 return true;
Jens Axboecccf0ee2020-01-27 16:34:48 -0700465 }
Jens Axboe00ddff42021-03-21 07:06:56 -0600466 return false;
Pavel Begunkovdc026a72020-03-04 16:14:09 +0300467}
468
469static void io_assign_current_work(struct io_worker *worker,
470 struct io_wq_work *work)
471{
Pavel Begunkovd78298e2020-03-14 00:31:03 +0300472 if (work) {
Jens Axboe3bfe6102021-02-16 14:15:30 -0700473 io_flush_signals();
Pavel Begunkovd78298e2020-03-14 00:31:03 +0300474 cond_resched();
475 }
Pavel Begunkovdc026a72020-03-04 16:14:09 +0300476
Jens Axboea9a4aa92021-08-30 06:33:08 -0600477 spin_lock(&worker->lock);
Pavel Begunkovdc026a72020-03-04 16:14:09 +0300478 worker->cur_work = work;
Jens Axboea9a4aa92021-08-30 06:33:08 -0600479 spin_unlock(&worker->lock);
Pavel Begunkovdc026a72020-03-04 16:14:09 +0300480}
481
Pavel Begunkov60cf46a2020-03-14 00:31:05 +0300482static void io_wqe_enqueue(struct io_wqe *wqe, struct io_wq_work *work);
483
Jens Axboe771b53d02019-10-22 10:25:58 -0600484static void io_worker_handle_work(struct io_worker *worker)
485 __releases(wqe->lock)
486{
Jens Axboef95dc202021-08-31 13:57:32 -0600487 struct io_wqe_acct *acct = io_wqe_get_acct(worker);
Jens Axboe771b53d02019-10-22 10:25:58 -0600488 struct io_wqe *wqe = worker->wqe;
489 struct io_wq *wq = wqe->wq;
Pavel Begunkovc60eb042021-04-08 01:54:42 +0100490 bool do_kill = test_bit(IO_WQ_BIT_EXIT, &wq->state);
Jens Axboe771b53d02019-10-22 10:25:58 -0600491
492 do {
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300493 struct io_wq_work *work;
Pavel Begunkovf462fd32020-03-04 16:14:11 +0300494get_next:
Jens Axboe771b53d02019-10-22 10:25:58 -0600495 /*
Jens Axboe771b53d02019-10-22 10:25:58 -0600496 * If we got some work, mark us as busy. If we didn't, but
497 * the list isn't empty, it means we stalled on hashed work.
498 * Mark us stalled so we don't keep looking for work when we
499 * can't make progress, any work completion or insertion will
500 * clear the stalled flag.
501 */
Jens Axboef95dc202021-08-31 13:57:32 -0600502 work = io_get_next_work(acct, worker);
Jens Axboe771b53d02019-10-22 10:25:58 -0600503 if (work)
504 __io_worker_busy(wqe, worker, work);
Jens Axboe771b53d02019-10-22 10:25:58 -0600505
Jens Axboea9a4aa92021-08-30 06:33:08 -0600506 raw_spin_unlock(&wqe->lock);
Jens Axboe771b53d02019-10-22 10:25:58 -0600507 if (!work)
508 break;
Pavel Begunkov58e39312020-03-04 16:14:10 +0300509 io_assign_current_work(worker, work);
Jens Axboee9418942021-02-19 12:33:30 -0700510 __set_current_state(TASK_RUNNING);
Jens Axboe36c2f922019-11-13 09:43:34 -0700511
Pavel Begunkovdc026a72020-03-04 16:14:09 +0300512 /* handle a whole dependent link */
513 do {
Pavel Begunkov5280f7e2021-02-04 13:52:08 +0000514 struct io_wq_work *next_hashed, *linked;
Pavel Begunkovb089ed392020-07-25 14:42:00 +0300515 unsigned int hash = io_get_work_hash(work);
Hillf Dantonfd1c4bc2019-12-24 09:14:29 -0700516
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300517 next_hashed = wq_next_work(work);
Pavel Begunkovc60eb042021-04-08 01:54:42 +0100518
519 if (unlikely(do_kill) && (work->flags & IO_WQ_WORK_UNBOUND))
520 work->flags |= IO_WQ_WORK_CANCEL;
Pavel Begunkov5280f7e2021-02-04 13:52:08 +0000521 wq->do_work(work);
522 io_assign_current_work(worker, NULL);
Jens Axboe36c2f922019-11-13 09:43:34 -0700523
Pavel Begunkov5280f7e2021-02-04 13:52:08 +0000524 linked = wq->free_work(work);
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300525 work = next_hashed;
526 if (!work && linked && !io_wq_is_hashed(linked)) {
527 work = linked;
528 linked = NULL;
529 }
530 io_assign_current_work(worker, work);
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300531 if (linked)
532 io_wqe_enqueue(wqe, linked);
533
534 if (hash != -1U && !next_hashed) {
Jens Axboee9418942021-02-19 12:33:30 -0700535 clear_bit(hash, &wq->hash->map);
Jens Axboef95dc202021-08-31 13:57:32 -0600536 clear_bit(IO_ACCT_STALLED_BIT, &acct->flags);
Jens Axboee9418942021-02-19 12:33:30 -0700537 if (wq_has_sleeper(&wq->hash->wait))
538 wake_up(&wq->hash->wait);
Jens Axboea9a4aa92021-08-30 06:33:08 -0600539 raw_spin_lock(&wqe->lock);
Pavel Begunkovf462fd32020-03-04 16:14:11 +0300540 /* skip unnecessary unlock-lock wqe->lock */
541 if (!work)
542 goto get_next;
Jens Axboea9a4aa92021-08-30 06:33:08 -0600543 raw_spin_unlock(&wqe->lock);
Pavel Begunkovdc026a72020-03-04 16:14:09 +0300544 }
Pavel Begunkov58e39312020-03-04 16:14:10 +0300545 } while (work);
Jens Axboe36c2f922019-11-13 09:43:34 -0700546
Jens Axboea9a4aa92021-08-30 06:33:08 -0600547 raw_spin_lock(&wqe->lock);
Jens Axboe771b53d02019-10-22 10:25:58 -0600548 } while (1);
549}
550
Jens Axboe771b53d02019-10-22 10:25:58 -0600551static int io_wqe_worker(void *data)
552{
553 struct io_worker *worker = data;
Jens Axboef95dc202021-08-31 13:57:32 -0600554 struct io_wqe_acct *acct = io_wqe_get_acct(worker);
Jens Axboe771b53d02019-10-22 10:25:58 -0600555 struct io_wqe *wqe = worker->wqe;
556 struct io_wq *wq = wqe->wq;
Jens Axboe05c5f4e2021-09-01 13:01:17 -0600557 bool last_timeout = false;
Jens Axboe46fe18b2021-03-04 12:39:36 -0700558 char buf[TASK_COMM_LEN];
Jens Axboe771b53d02019-10-22 10:25:58 -0600559
Jens Axboe46fe18b2021-03-04 12:39:36 -0700560 worker->flags |= (IO_WORKER_F_UP | IO_WORKER_F_RUNNING);
Jens Axboe46fe18b2021-03-04 12:39:36 -0700561
Jens Axboe685fe7f2021-03-08 09:37:51 -0700562 snprintf(buf, sizeof(buf), "iou-wrk-%d", wq->task->pid);
Jens Axboe46fe18b2021-03-04 12:39:36 -0700563 set_task_comm(current, buf);
Jens Axboe771b53d02019-10-22 10:25:58 -0600564
565 while (!test_bit(IO_WQ_BIT_EXIT, &wq->state)) {
Jens Axboe16efa4f2021-03-12 20:26:13 -0700566 long ret;
567
Jens Axboe506d95f2019-12-07 21:03:59 -0700568 set_current_state(TASK_INTERRUPTIBLE);
Jens Axboee995d512019-12-07 21:06:46 -0700569loop:
Jens Axboea9a4aa92021-08-30 06:33:08 -0600570 raw_spin_lock(&wqe->lock);
Jens Axboef95dc202021-08-31 13:57:32 -0600571 if (io_acct_run_queue(acct)) {
Jens Axboe771b53d02019-10-22 10:25:58 -0600572 io_worker_handle_work(worker);
Jens Axboee995d512019-12-07 21:06:46 -0700573 goto loop;
Jens Axboe771b53d02019-10-22 10:25:58 -0600574 }
Jens Axboe05c5f4e2021-09-01 13:01:17 -0600575 /* timed out, exit unless we're the last worker */
576 if (last_timeout && acct->nr_workers > 1) {
577 raw_spin_unlock(&wqe->lock);
578 __set_current_state(TASK_RUNNING);
579 break;
580 }
581 last_timeout = false;
Jens Axboec6d77d92021-02-15 13:26:34 -0700582 __io_worker_idle(wqe, worker);
Jens Axboea9a4aa92021-08-30 06:33:08 -0600583 raw_spin_unlock(&wqe->lock);
Jens Axboe00ddff42021-03-21 07:06:56 -0600584 if (io_flush_signals())
585 continue;
Jens Axboe16efa4f2021-03-12 20:26:13 -0700586 ret = schedule_timeout(WORKER_IDLE_TIMEOUT);
Jens Axboedbe1bdb2021-03-25 18:16:06 -0600587 if (signal_pending(current)) {
588 struct ksignal ksig;
589
590 if (!get_signal(&ksig))
591 continue;
Jens Axboe15e20db2021-09-01 11:18:41 -0600592 if (fatal_signal_pending(current))
593 break;
594 continue;
Jens Axboedbe1bdb2021-03-25 18:16:06 -0600595 }
Jens Axboe05c5f4e2021-09-01 13:01:17 -0600596 last_timeout = !ret;
Jens Axboe771b53d02019-10-22 10:25:58 -0600597 }
598
Jens Axboe771b53d02019-10-22 10:25:58 -0600599 if (test_bit(IO_WQ_BIT_EXIT, &wq->state)) {
Jens Axboea9a4aa92021-08-30 06:33:08 -0600600 raw_spin_lock(&wqe->lock);
Pavel Begunkove5872272021-06-14 02:36:17 +0100601 io_worker_handle_work(worker);
Jens Axboe771b53d02019-10-22 10:25:58 -0600602 }
603
604 io_worker_exit(worker);
605 return 0;
606}
607
608/*
Jens Axboe771b53d02019-10-22 10:25:58 -0600609 * Called when a worker is scheduled in. Mark us as currently running.
610 */
611void io_wq_worker_running(struct task_struct *tsk)
612{
Jens Axboe3bfe6102021-02-16 14:15:30 -0700613 struct io_worker *worker = tsk->pf_io_worker;
Jens Axboe771b53d02019-10-22 10:25:58 -0600614
Jens Axboe3bfe6102021-02-16 14:15:30 -0700615 if (!worker)
616 return;
Jens Axboe771b53d02019-10-22 10:25:58 -0600617 if (!(worker->flags & IO_WORKER_F_UP))
618 return;
619 if (worker->flags & IO_WORKER_F_RUNNING)
620 return;
621 worker->flags |= IO_WORKER_F_RUNNING;
Jens Axboe958234d2021-02-17 09:00:57 -0700622 io_wqe_inc_running(worker);
Jens Axboe771b53d02019-10-22 10:25:58 -0600623}
624
625/*
626 * Called when worker is going to sleep. If there are no workers currently
Jens Axboe685fe7f2021-03-08 09:37:51 -0700627 * running and we have work pending, wake up a free one or create a new one.
Jens Axboe771b53d02019-10-22 10:25:58 -0600628 */
629void io_wq_worker_sleeping(struct task_struct *tsk)
630{
Jens Axboe3bfe6102021-02-16 14:15:30 -0700631 struct io_worker *worker = tsk->pf_io_worker;
Jens Axboe771b53d02019-10-22 10:25:58 -0600632
Jens Axboe3bfe6102021-02-16 14:15:30 -0700633 if (!worker)
634 return;
Jens Axboe771b53d02019-10-22 10:25:58 -0600635 if (!(worker->flags & IO_WORKER_F_UP))
636 return;
637 if (!(worker->flags & IO_WORKER_F_RUNNING))
638 return;
639
640 worker->flags &= ~IO_WORKER_F_RUNNING;
641
Jens Axboea9a4aa92021-08-30 06:33:08 -0600642 raw_spin_lock(&worker->wqe->lock);
Jens Axboe958234d2021-02-17 09:00:57 -0700643 io_wqe_dec_running(worker);
Jens Axboea9a4aa92021-08-30 06:33:08 -0600644 raw_spin_unlock(&worker->wqe->lock);
Jens Axboe771b53d02019-10-22 10:25:58 -0600645}
646
Jens Axboe3146cba2021-09-01 11:20:10 -0600647static void io_init_new_worker(struct io_wqe *wqe, struct io_worker *worker,
648 struct task_struct *tsk)
Jens Axboe3bfe6102021-02-16 14:15:30 -0700649{
Jens Axboe46fe18b2021-03-04 12:39:36 -0700650 tsk->pf_io_worker = worker;
651 worker->task = tsk;
Jens Axboe0e034962021-06-17 10:08:11 -0600652 set_cpus_allowed_ptr(tsk, wqe->cpu_mask);
Jens Axboee22bc9b2021-03-09 19:49:02 -0700653 tsk->flags |= PF_NO_SETAFFINITY;
Jens Axboe46fe18b2021-03-04 12:39:36 -0700654
Jens Axboea9a4aa92021-08-30 06:33:08 -0600655 raw_spin_lock(&wqe->lock);
Jens Axboe46fe18b2021-03-04 12:39:36 -0700656 hlist_nulls_add_head_rcu(&worker->nulls_node, &wqe->free_list);
657 list_add_tail_rcu(&worker->all_list, &wqe->all_list);
658 worker->flags |= IO_WORKER_F_FREE;
Jens Axboea9a4aa92021-08-30 06:33:08 -0600659 raw_spin_unlock(&wqe->lock);
Jens Axboe46fe18b2021-03-04 12:39:36 -0700660 wake_up_new_task(tsk);
Jens Axboe771b53d02019-10-22 10:25:58 -0600661}
662
Jens Axboe3146cba2021-09-01 11:20:10 -0600663static bool io_wq_work_match_all(struct io_wq_work *work, void *data)
664{
665 return true;
666}
667
668static inline bool io_should_retry_thread(long err)
669{
670 switch (err) {
671 case -EAGAIN:
672 case -ERESTARTSYS:
673 case -ERESTARTNOINTR:
674 case -ERESTARTNOHAND:
675 return true;
676 default:
677 return false;
678 }
679}
680
681static void create_worker_cont(struct callback_head *cb)
682{
683 struct io_worker *worker;
684 struct task_struct *tsk;
685 struct io_wqe *wqe;
686
687 worker = container_of(cb, struct io_worker, create_work);
688 clear_bit_unlock(0, &worker->create_state);
689 wqe = worker->wqe;
690 tsk = create_io_thread(io_wqe_worker, worker, wqe->node);
691 if (!IS_ERR(tsk)) {
692 io_init_new_worker(wqe, worker, tsk);
693 io_worker_release(worker);
694 return;
695 } else if (!io_should_retry_thread(PTR_ERR(tsk))) {
696 struct io_wqe_acct *acct = io_wqe_get_acct(worker);
697
698 atomic_dec(&acct->nr_running);
699 raw_spin_lock(&wqe->lock);
700 acct->nr_workers--;
701 if (!acct->nr_workers) {
702 struct io_cb_cancel_data match = {
703 .fn = io_wq_work_match_all,
704 .cancel_all = true,
705 };
706
707 while (io_acct_cancel_pending_work(wqe, acct, &match))
708 raw_spin_lock(&wqe->lock);
709 }
710 raw_spin_unlock(&wqe->lock);
711 io_worker_ref_put(wqe->wq);
Qiang.zhang66e70be2021-09-09 19:58:22 +0800712 kfree(worker);
Jens Axboe3146cba2021-09-01 11:20:10 -0600713 return;
714 }
715
716 /* re-create attempts grab a new worker ref, drop the existing one */
717 io_worker_release(worker);
718 schedule_work(&worker->work);
719}
720
721static void io_workqueue_create(struct work_struct *work)
722{
723 struct io_worker *worker = container_of(work, struct io_worker, work);
724 struct io_wqe_acct *acct = io_wqe_get_acct(worker);
725
726 if (!io_queue_worker_create(worker, acct, create_worker_cont)) {
727 clear_bit_unlock(0, &worker->create_state);
728 io_worker_release(worker);
Qiang.zhang66e70be2021-09-09 19:58:22 +0800729 kfree(worker);
Jens Axboe3146cba2021-09-01 11:20:10 -0600730 }
731}
732
733static bool create_io_worker(struct io_wq *wq, struct io_wqe *wqe, int index)
734{
735 struct io_wqe_acct *acct = &wqe->acct[index];
736 struct io_worker *worker;
737 struct task_struct *tsk;
738
739 __set_current_state(TASK_RUNNING);
740
741 worker = kzalloc_node(sizeof(*worker), GFP_KERNEL, wqe->node);
742 if (!worker) {
743fail:
744 atomic_dec(&acct->nr_running);
745 raw_spin_lock(&wqe->lock);
746 acct->nr_workers--;
747 raw_spin_unlock(&wqe->lock);
748 io_worker_ref_put(wq);
749 return false;
750 }
751
752 refcount_set(&worker->ref, 1);
753 worker->wqe = wqe;
754 spin_lock_init(&worker->lock);
755 init_completion(&worker->ref_done);
756
757 if (index == IO_WQ_ACCT_BOUND)
758 worker->flags |= IO_WORKER_F_BOUND;
759
760 tsk = create_io_thread(io_wqe_worker, worker, wqe->node);
761 if (!IS_ERR(tsk)) {
762 io_init_new_worker(wqe, worker, tsk);
763 } else if (!io_should_retry_thread(PTR_ERR(tsk))) {
Qiang.zhang66e70be2021-09-09 19:58:22 +0800764 kfree(worker);
Jens Axboe3146cba2021-09-01 11:20:10 -0600765 goto fail;
766 } else {
767 INIT_WORK(&worker->work, io_workqueue_create);
768 schedule_work(&worker->work);
769 }
770
771 return true;
772}
773
Hillf Dantonc4068bf2020-09-26 21:26:55 +0800774/*
775 * Iterate the passed in list and call the specific function for each
776 * worker that isn't exiting
777 */
778static bool io_wq_for_each_worker(struct io_wqe *wqe,
779 bool (*func)(struct io_worker *, void *),
780 void *data)
781{
782 struct io_worker *worker;
783 bool ret = false;
784
785 list_for_each_entry_rcu(worker, &wqe->all_list, all_list) {
786 if (io_worker_get(worker)) {
787 /* no task if node is/was offline */
788 if (worker->task)
789 ret = func(worker, data);
790 io_worker_release(worker);
791 if (ret)
792 break;
793 }
794 }
795
796 return ret;
797}
798
799static bool io_wq_worker_wake(struct io_worker *worker, void *data)
800{
Jens Axboe46fe18b2021-03-04 12:39:36 -0700801 set_notify_signal(worker->task);
Hillf Dantonc4068bf2020-09-26 21:26:55 +0800802 wake_up_process(worker->task);
803 return false;
804}
805
Pavel Begunkove9fd9392020-03-04 16:14:12 +0300806static void io_run_cancel(struct io_wq_work *work, struct io_wqe *wqe)
Pavel Begunkovfc04c392020-03-01 19:18:19 +0300807{
Pavel Begunkove9fd9392020-03-04 16:14:12 +0300808 struct io_wq *wq = wqe->wq;
809
Pavel Begunkovfc04c392020-03-01 19:18:19 +0300810 do {
Pavel Begunkovfc04c392020-03-01 19:18:19 +0300811 work->flags |= IO_WQ_WORK_CANCEL;
Pavel Begunkov5280f7e2021-02-04 13:52:08 +0000812 wq->do_work(work);
813 work = wq->free_work(work);
Pavel Begunkovfc04c392020-03-01 19:18:19 +0300814 } while (work);
815}
816
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300817static void io_wqe_insert_work(struct io_wqe *wqe, struct io_wq_work *work)
818{
Jens Axboef95dc202021-08-31 13:57:32 -0600819 struct io_wqe_acct *acct = io_work_get_acct(wqe, work);
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300820 unsigned int hash;
821 struct io_wq_work *tail;
822
823 if (!io_wq_is_hashed(work)) {
824append:
Jens Axboef95dc202021-08-31 13:57:32 -0600825 wq_list_add_tail(&work->list, &acct->work_list);
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300826 return;
827 }
828
829 hash = io_get_work_hash(work);
830 tail = wqe->hash_tail[hash];
831 wqe->hash_tail[hash] = work;
832 if (!tail)
833 goto append;
834
Jens Axboef95dc202021-08-31 13:57:32 -0600835 wq_list_add_after(&work->list, &tail->list, &acct->work_list);
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300836}
837
Pavel Begunkov713b9822021-09-08 10:09:29 +0100838static bool io_wq_work_match_item(struct io_wq_work *work, void *data)
839{
840 return work == data;
841}
842
Jens Axboe771b53d02019-10-22 10:25:58 -0600843static void io_wqe_enqueue(struct io_wqe *wqe, struct io_wq_work *work)
844{
Jens Axboec5def4a2019-11-07 11:41:16 -0700845 struct io_wqe_acct *acct = io_work_get_acct(wqe, work);
Jens Axboe94ffb0a2021-08-30 11:55:22 -0600846 unsigned work_flags = work->flags;
847 bool do_create;
Jens Axboe771b53d02019-10-22 10:25:58 -0600848
Jens Axboe991468d2021-07-23 11:53:54 -0600849 /*
850 * If io-wq is exiting for this task, or if the request has explicitly
851 * been marked as one that should not get executed, cancel it here.
852 */
853 if (test_bit(IO_WQ_BIT_EXIT, &wqe->wq->state) ||
854 (work->flags & IO_WQ_WORK_CANCEL)) {
yangerkun70e35122021-03-09 11:04:10 +0800855 io_run_cancel(work, wqe);
Jens Axboe4fb6ac32021-02-25 10:17:09 -0700856 return;
857 }
858
Jens Axboea9a4aa92021-08-30 06:33:08 -0600859 raw_spin_lock(&wqe->lock);
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300860 io_wqe_insert_work(wqe, work);
Jens Axboef95dc202021-08-31 13:57:32 -0600861 clear_bit(IO_ACCT_STALLED_BIT, &acct->flags);
Jens Axboe94ffb0a2021-08-30 11:55:22 -0600862
863 rcu_read_lock();
Jens Axboef95dc202021-08-31 13:57:32 -0600864 do_create = !io_wqe_activate_free_worker(wqe, acct);
Jens Axboe94ffb0a2021-08-30 11:55:22 -0600865 rcu_read_unlock();
866
Jens Axboea9a4aa92021-08-30 06:33:08 -0600867 raw_spin_unlock(&wqe->lock);
Jens Axboe771b53d02019-10-22 10:25:58 -0600868
Jens Axboe94ffb0a2021-08-30 11:55:22 -0600869 if (do_create && ((work_flags & IO_WQ_WORK_CONCURRENT) ||
Jens Axboe3146cba2021-09-01 11:20:10 -0600870 !atomic_read(&acct->nr_running))) {
871 bool did_create;
872
873 did_create = io_wqe_create_worker(wqe, acct);
Pavel Begunkov713b9822021-09-08 10:09:29 +0100874 if (likely(did_create))
875 return;
876
877 raw_spin_lock(&wqe->lock);
878 /* fatal condition, failed to create the first worker */
879 if (!acct->nr_workers) {
880 struct io_cb_cancel_data match = {
881 .fn = io_wq_work_match_item,
882 .data = work,
883 .cancel_all = false,
884 };
885
886 if (io_acct_cancel_pending_work(wqe, acct, &match))
887 raw_spin_lock(&wqe->lock);
Jens Axboe3146cba2021-09-01 11:20:10 -0600888 }
Pavel Begunkov713b9822021-09-08 10:09:29 +0100889 raw_spin_unlock(&wqe->lock);
Jens Axboe3146cba2021-09-01 11:20:10 -0600890 }
Jens Axboe771b53d02019-10-22 10:25:58 -0600891}
892
893void io_wq_enqueue(struct io_wq *wq, struct io_wq_work *work)
894{
895 struct io_wqe *wqe = wq->wqes[numa_node_id()];
896
897 io_wqe_enqueue(wqe, work);
898}
899
900/*
Pavel Begunkov8766dd52020-03-14 00:31:04 +0300901 * Work items that hash to the same value will not be done in parallel.
902 * Used to limit concurrent writes, generally hashed by inode.
Jens Axboe771b53d02019-10-22 10:25:58 -0600903 */
Pavel Begunkov8766dd52020-03-14 00:31:04 +0300904void io_wq_hash_work(struct io_wq_work *work, void *val)
Jens Axboe771b53d02019-10-22 10:25:58 -0600905{
Pavel Begunkov8766dd52020-03-14 00:31:04 +0300906 unsigned int bit;
Jens Axboe771b53d02019-10-22 10:25:58 -0600907
908 bit = hash_ptr(val, IO_WQ_HASH_ORDER);
909 work->flags |= (IO_WQ_WORK_HASHED | (bit << IO_WQ_HASH_SHIFT));
Jens Axboe771b53d02019-10-22 10:25:58 -0600910}
911
Pavel Begunkov2293b412020-03-07 01:15:39 +0300912static bool io_wq_worker_cancel(struct io_worker *worker, void *data)
Jens Axboe62755e32019-10-28 21:49:21 -0600913{
Pavel Begunkov2293b412020-03-07 01:15:39 +0300914 struct io_cb_cancel_data *match = data;
Jens Axboe62755e32019-10-28 21:49:21 -0600915
916 /*
917 * Hold the lock to avoid ->cur_work going out of scope, caller
Jens Axboe36c2f922019-11-13 09:43:34 -0700918 * may dereference the passed in work.
Jens Axboe62755e32019-10-28 21:49:21 -0600919 */
Jens Axboea9a4aa92021-08-30 06:33:08 -0600920 spin_lock(&worker->lock);
Jens Axboe62755e32019-10-28 21:49:21 -0600921 if (worker->cur_work &&
Pavel Begunkov2293b412020-03-07 01:15:39 +0300922 match->fn(worker->cur_work, match->data)) {
Jens Axboe3bfe6102021-02-16 14:15:30 -0700923 set_notify_signal(worker->task);
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300924 match->nr_running++;
Jens Axboe771b53d02019-10-22 10:25:58 -0600925 }
Jens Axboea9a4aa92021-08-30 06:33:08 -0600926 spin_unlock(&worker->lock);
Jens Axboe771b53d02019-10-22 10:25:58 -0600927
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300928 return match->nr_running && !match->cancel_all;
Jens Axboe771b53d02019-10-22 10:25:58 -0600929}
930
Pavel Begunkov204361a2020-08-23 20:33:10 +0300931static inline void io_wqe_remove_pending(struct io_wqe *wqe,
932 struct io_wq_work *work,
933 struct io_wq_work_node *prev)
934{
Jens Axboef95dc202021-08-31 13:57:32 -0600935 struct io_wqe_acct *acct = io_work_get_acct(wqe, work);
Pavel Begunkov204361a2020-08-23 20:33:10 +0300936 unsigned int hash = io_get_work_hash(work);
937 struct io_wq_work *prev_work = NULL;
938
939 if (io_wq_is_hashed(work) && work == wqe->hash_tail[hash]) {
940 if (prev)
941 prev_work = container_of(prev, struct io_wq_work, list);
942 if (prev_work && io_get_work_hash(prev_work) == hash)
943 wqe->hash_tail[hash] = prev_work;
944 else
945 wqe->hash_tail[hash] = NULL;
946 }
Jens Axboef95dc202021-08-31 13:57:32 -0600947 wq_list_del(&acct->work_list, &work->list, prev);
Pavel Begunkov204361a2020-08-23 20:33:10 +0300948}
949
Jens Axboe3146cba2021-09-01 11:20:10 -0600950static bool io_acct_cancel_pending_work(struct io_wqe *wqe,
951 struct io_wqe_acct *acct,
952 struct io_cb_cancel_data *match)
953 __releases(wqe->lock)
Jens Axboe771b53d02019-10-22 10:25:58 -0600954{
Jens Axboe6206f0e2019-11-26 11:59:32 -0700955 struct io_wq_work_node *node, *prev;
Jens Axboe771b53d02019-10-22 10:25:58 -0600956 struct io_wq_work *work;
Jens Axboe771b53d02019-10-22 10:25:58 -0600957
Jens Axboe3146cba2021-09-01 11:20:10 -0600958 wq_list_for_each(node, prev, &acct->work_list) {
959 work = container_of(node, struct io_wq_work, list);
960 if (!match->fn(work, match->data))
961 continue;
962 io_wqe_remove_pending(wqe, work, prev);
963 raw_spin_unlock(&wqe->lock);
964 io_run_cancel(work, wqe);
965 match->nr_pending++;
966 /* not safe to continue after unlock */
967 return true;
968 }
969
970 return false;
971}
972
973static void io_wqe_cancel_pending_work(struct io_wqe *wqe,
974 struct io_cb_cancel_data *match)
975{
976 int i;
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300977retry:
Jens Axboea9a4aa92021-08-30 06:33:08 -0600978 raw_spin_lock(&wqe->lock);
Jens Axboef95dc202021-08-31 13:57:32 -0600979 for (i = 0; i < IO_WQ_ACCT_NR; i++) {
980 struct io_wqe_acct *acct = io_get_acct(wqe, i == 0);
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300981
Jens Axboe3146cba2021-09-01 11:20:10 -0600982 if (io_acct_cancel_pending_work(wqe, acct, match)) {
983 if (match->cancel_all)
984 goto retry;
985 return;
Jens Axboef95dc202021-08-31 13:57:32 -0600986 }
Jens Axboe771b53d02019-10-22 10:25:58 -0600987 }
Jens Axboea9a4aa92021-08-30 06:33:08 -0600988 raw_spin_unlock(&wqe->lock);
Pavel Begunkovf4c26652020-06-15 10:24:02 +0300989}
Jens Axboe771b53d02019-10-22 10:25:58 -0600990
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300991static void io_wqe_cancel_running_work(struct io_wqe *wqe,
Pavel Begunkovf4c26652020-06-15 10:24:02 +0300992 struct io_cb_cancel_data *match)
993{
Jens Axboe771b53d02019-10-22 10:25:58 -0600994 rcu_read_lock();
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300995 io_wq_for_each_worker(wqe, io_wq_worker_cancel, match);
Jens Axboe771b53d02019-10-22 10:25:58 -0600996 rcu_read_unlock();
Jens Axboe771b53d02019-10-22 10:25:58 -0600997}
998
Pavel Begunkov2293b412020-03-07 01:15:39 +0300999enum io_wq_cancel io_wq_cancel_cb(struct io_wq *wq, work_cancel_fn *cancel,
Pavel Begunkov4f26bda2020-06-15 10:24:03 +03001000 void *data, bool cancel_all)
Pavel Begunkov2293b412020-03-07 01:15:39 +03001001{
1002 struct io_cb_cancel_data match = {
Pavel Begunkov4f26bda2020-06-15 10:24:03 +03001003 .fn = cancel,
1004 .data = data,
1005 .cancel_all = cancel_all,
Pavel Begunkov2293b412020-03-07 01:15:39 +03001006 };
Pavel Begunkov2293b412020-03-07 01:15:39 +03001007 int node;
1008
Pavel Begunkovf4c26652020-06-15 10:24:02 +03001009 /*
1010 * First check pending list, if we're lucky we can just remove it
1011 * from there. CANCEL_OK means that the work is returned as-new,
1012 * no completion will be posted for it.
1013 */
Pavel Begunkov2293b412020-03-07 01:15:39 +03001014 for_each_node(node) {
1015 struct io_wqe *wqe = wq->wqes[node];
1016
Pavel Begunkov4f26bda2020-06-15 10:24:03 +03001017 io_wqe_cancel_pending_work(wqe, &match);
1018 if (match.nr_pending && !match.cancel_all)
Pavel Begunkovf4c26652020-06-15 10:24:02 +03001019 return IO_WQ_CANCEL_OK;
Pavel Begunkov2293b412020-03-07 01:15:39 +03001020 }
1021
Pavel Begunkovf4c26652020-06-15 10:24:02 +03001022 /*
1023 * Now check if a free (going busy) or busy worker has the work
1024 * currently running. If we find it there, we'll return CANCEL_RUNNING
1025 * as an indication that we attempt to signal cancellation. The
1026 * completion will run normally in this case.
1027 */
1028 for_each_node(node) {
1029 struct io_wqe *wqe = wq->wqes[node];
1030
Pavel Begunkov4f26bda2020-06-15 10:24:03 +03001031 io_wqe_cancel_running_work(wqe, &match);
1032 if (match.nr_running && !match.cancel_all)
Pavel Begunkovf4c26652020-06-15 10:24:02 +03001033 return IO_WQ_CANCEL_RUNNING;
1034 }
1035
Pavel Begunkov4f26bda2020-06-15 10:24:03 +03001036 if (match.nr_running)
1037 return IO_WQ_CANCEL_RUNNING;
1038 if (match.nr_pending)
1039 return IO_WQ_CANCEL_OK;
Pavel Begunkovf4c26652020-06-15 10:24:02 +03001040 return IO_WQ_CANCEL_NOTFOUND;
Pavel Begunkov2293b412020-03-07 01:15:39 +03001041}
1042
Jens Axboee9418942021-02-19 12:33:30 -07001043static int io_wqe_hash_wake(struct wait_queue_entry *wait, unsigned mode,
1044 int sync, void *key)
1045{
1046 struct io_wqe *wqe = container_of(wait, struct io_wqe, wait);
Jens Axboef95dc202021-08-31 13:57:32 -06001047 int i;
Jens Axboee9418942021-02-19 12:33:30 -07001048
1049 list_del_init(&wait->entry);
1050
1051 rcu_read_lock();
Jens Axboef95dc202021-08-31 13:57:32 -06001052 for (i = 0; i < IO_WQ_ACCT_NR; i++) {
1053 struct io_wqe_acct *acct = &wqe->acct[i];
1054
1055 if (test_and_clear_bit(IO_ACCT_STALLED_BIT, &acct->flags))
1056 io_wqe_activate_free_worker(wqe, acct);
1057 }
Jens Axboee9418942021-02-19 12:33:30 -07001058 rcu_read_unlock();
Jens Axboee9418942021-02-19 12:33:30 -07001059 return 1;
1060}
1061
Jens Axboe576a3472019-11-25 08:49:20 -07001062struct io_wq *io_wq_create(unsigned bounded, struct io_wq_data *data)
Jens Axboe771b53d02019-10-22 10:25:58 -06001063{
Jens Axboef95dc202021-08-31 13:57:32 -06001064 int ret, node, i;
Jens Axboe771b53d02019-10-22 10:25:58 -06001065 struct io_wq *wq;
1066
Pavel Begunkovf5fa38c2020-06-08 21:08:20 +03001067 if (WARN_ON_ONCE(!data->free_work || !data->do_work))
Pavel Begunkove9fd9392020-03-04 16:14:12 +03001068 return ERR_PTR(-EINVAL);
Pavel Begunkove6ab8992021-06-17 18:13:59 +01001069 if (WARN_ON_ONCE(!bounded))
1070 return ERR_PTR(-EINVAL);
Pavel Begunkove9fd9392020-03-04 16:14:12 +03001071
Pavel Begunkovc7f405d2021-06-14 02:36:12 +01001072 wq = kzalloc(struct_size(wq, wqes, nr_node_ids), GFP_KERNEL);
Jens Axboe771b53d02019-10-22 10:25:58 -06001073 if (!wq)
1074 return ERR_PTR(-ENOMEM);
Jens Axboe43c01fb2020-10-22 09:02:50 -06001075 ret = cpuhp_state_add_instance_nocalls(io_wq_online, &wq->cpuhp_node);
1076 if (ret)
Pavel Begunkovc7f405d2021-06-14 02:36:12 +01001077 goto err_wq;
Jens Axboe771b53d02019-10-22 10:25:58 -06001078
Jens Axboee9418942021-02-19 12:33:30 -07001079 refcount_inc(&data->hash->refs);
1080 wq->hash = data->hash;
Pavel Begunkove9fd9392020-03-04 16:14:12 +03001081 wq->free_work = data->free_work;
Pavel Begunkovf5fa38c2020-06-08 21:08:20 +03001082 wq->do_work = data->do_work;
Jens Axboe7d723062019-11-12 22:31:31 -07001083
Jens Axboe43c01fb2020-10-22 09:02:50 -06001084 ret = -ENOMEM;
Jann Horn3fc50ab2019-11-26 19:10:20 +01001085 for_each_node(node) {
Jens Axboe771b53d02019-10-22 10:25:58 -06001086 struct io_wqe *wqe;
Jens Axboe75634392020-02-11 06:30:06 -07001087 int alloc_node = node;
Jens Axboe771b53d02019-10-22 10:25:58 -06001088
Jens Axboe75634392020-02-11 06:30:06 -07001089 if (!node_online(alloc_node))
1090 alloc_node = NUMA_NO_NODE;
1091 wqe = kzalloc_node(sizeof(struct io_wqe), GFP_KERNEL, alloc_node);
Jens Axboe771b53d02019-10-22 10:25:58 -06001092 if (!wqe)
Jann Horn3fc50ab2019-11-26 19:10:20 +01001093 goto err;
Jens Axboe0e034962021-06-17 10:08:11 -06001094 if (!alloc_cpumask_var(&wqe->cpu_mask, GFP_KERNEL))
1095 goto err;
1096 cpumask_copy(wqe->cpu_mask, cpumask_of_node(node));
Jann Horn3fc50ab2019-11-26 19:10:20 +01001097 wq->wqes[node] = wqe;
Jens Axboe75634392020-02-11 06:30:06 -07001098 wqe->node = alloc_node;
Jens Axboec5def4a2019-11-07 11:41:16 -07001099 wqe->acct[IO_WQ_ACCT_BOUND].max_workers = bounded;
Jens Axboe728f13e2021-02-21 16:02:53 -07001100 wqe->acct[IO_WQ_ACCT_UNBOUND].max_workers =
Jens Axboec5def4a2019-11-07 11:41:16 -07001101 task_rlimit(current, RLIMIT_NPROC);
Jens Axboee9418942021-02-19 12:33:30 -07001102 INIT_LIST_HEAD(&wqe->wait.entry);
Jens Axboef95dc202021-08-31 13:57:32 -06001103 wqe->wait.func = io_wqe_hash_wake;
1104 for (i = 0; i < IO_WQ_ACCT_NR; i++) {
1105 struct io_wqe_acct *acct = &wqe->acct[i];
1106
1107 acct->index = i;
1108 atomic_set(&acct->nr_running, 0);
1109 INIT_WQ_LIST(&acct->work_list);
1110 }
Jens Axboe771b53d02019-10-22 10:25:58 -06001111 wqe->wq = wq;
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +02001112 raw_spin_lock_init(&wqe->lock);
Jens Axboe021d1cd2019-11-14 08:00:41 -07001113 INIT_HLIST_NULLS_HEAD(&wqe->free_list, 0);
Jens Axboee61df662019-11-13 13:54:49 -07001114 INIT_LIST_HEAD(&wqe->all_list);
Jens Axboe771b53d02019-10-22 10:25:58 -06001115 }
1116
Jens Axboe685fe7f2021-03-08 09:37:51 -07001117 wq->task = get_task_struct(data->task);
Jens Axboe685fe7f2021-03-08 09:37:51 -07001118 atomic_set(&wq->worker_refs, 1);
1119 init_completion(&wq->worker_done);
1120 return wq;
Jens Axboeb60fda62019-11-19 08:37:07 -07001121err:
Jens Axboedc7bbc92021-03-01 09:09:56 -07001122 io_wq_put_hash(data->hash);
Jens Axboe43c01fb2020-10-22 09:02:50 -06001123 cpuhp_state_remove_instance_nocalls(io_wq_online, &wq->cpuhp_node);
Jens Axboe0e034962021-06-17 10:08:11 -06001124 for_each_node(node) {
1125 if (!wq->wqes[node])
1126 continue;
1127 free_cpumask_var(wq->wqes[node]->cpu_mask);
Jann Horn3fc50ab2019-11-26 19:10:20 +01001128 kfree(wq->wqes[node]);
Jens Axboe0e034962021-06-17 10:08:11 -06001129 }
Jens Axboe43c01fb2020-10-22 09:02:50 -06001130err_wq:
Jens Axboeb60fda62019-11-19 08:37:07 -07001131 kfree(wq);
Jens Axboe771b53d02019-10-22 10:25:58 -06001132 return ERR_PTR(ret);
1133}
1134
Jens Axboec80ca472021-04-01 19:57:07 -06001135static bool io_task_work_match(struct callback_head *cb, void *data)
1136{
Jens Axboed3e9f732021-08-04 08:37:25 -06001137 struct io_worker *worker;
Jens Axboec80ca472021-04-01 19:57:07 -06001138
Jens Axboe3b33e3f2021-09-08 19:57:26 -06001139 if (cb->func != create_worker_cb && cb->func != create_worker_cont)
Jens Axboec80ca472021-04-01 19:57:07 -06001140 return false;
Jens Axboed3e9f732021-08-04 08:37:25 -06001141 worker = container_of(cb, struct io_worker, create_work);
1142 return worker->wqe->wq == data;
Jens Axboec80ca472021-04-01 19:57:07 -06001143}
1144
Pavel Begunkov17a91052021-05-23 15:48:39 +01001145void io_wq_exit_start(struct io_wq *wq)
1146{
1147 set_bit(IO_WQ_BIT_EXIT, &wq->state);
1148}
1149
Jens Axboe685fe7f2021-03-08 09:37:51 -07001150static void io_wq_exit_workers(struct io_wq *wq)
Jens Axboeafcc4012021-02-26 13:48:19 -07001151{
Jens Axboe685fe7f2021-03-08 09:37:51 -07001152 struct callback_head *cb;
1153 int node;
1154
Jens Axboe685fe7f2021-03-08 09:37:51 -07001155 if (!wq->task)
1156 return;
1157
Jens Axboec80ca472021-04-01 19:57:07 -06001158 while ((cb = task_work_cancel_match(wq->task, io_task_work_match, wq)) != NULL) {
Jens Axboed3e9f732021-08-04 08:37:25 -06001159 struct io_worker *worker;
Jens Axboe3b33e3f2021-09-08 19:57:26 -06001160 struct io_wqe_acct *acct;
Jens Axboe685fe7f2021-03-08 09:37:51 -07001161
Jens Axboed3e9f732021-08-04 08:37:25 -06001162 worker = container_of(cb, struct io_worker, create_work);
Jens Axboe3b33e3f2021-09-08 19:57:26 -06001163 acct = io_wqe_get_acct(worker);
1164 atomic_dec(&acct->nr_running);
1165 raw_spin_lock(&worker->wqe->lock);
1166 acct->nr_workers--;
1167 raw_spin_unlock(&worker->wqe->lock);
Jens Axboe685fe7f2021-03-08 09:37:51 -07001168 io_worker_ref_put(wq);
Jens Axboed3e9f732021-08-04 08:37:25 -06001169 clear_bit_unlock(0, &worker->create_state);
1170 io_worker_release(worker);
Jens Axboeafcc4012021-02-26 13:48:19 -07001171 }
Jens Axboe685fe7f2021-03-08 09:37:51 -07001172
1173 rcu_read_lock();
1174 for_each_node(node) {
1175 struct io_wqe *wqe = wq->wqes[node];
1176
1177 io_wq_for_each_worker(wqe, io_wq_worker_wake, NULL);
Jens Axboe685fe7f2021-03-08 09:37:51 -07001178 }
1179 rcu_read_unlock();
1180 io_worker_ref_put(wq);
1181 wait_for_completion(&wq->worker_done);
Zqiang3743c172021-05-26 13:08:26 +08001182
1183 for_each_node(node) {
1184 spin_lock_irq(&wq->hash->wait.lock);
1185 list_del_init(&wq->wqes[node]->wait.entry);
1186 spin_unlock_irq(&wq->hash->wait.lock);
1187 }
Jens Axboe685fe7f2021-03-08 09:37:51 -07001188 put_task_struct(wq->task);
1189 wq->task = NULL;
Jens Axboeafcc4012021-02-26 13:48:19 -07001190}
1191
Jens Axboe4fb6ac32021-02-25 10:17:09 -07001192static void io_wq_destroy(struct io_wq *wq)
Jens Axboe771b53d02019-10-22 10:25:58 -06001193{
Jann Horn3fc50ab2019-11-26 19:10:20 +01001194 int node;
Jens Axboe771b53d02019-10-22 10:25:58 -06001195
Jens Axboe43c01fb2020-10-22 09:02:50 -06001196 cpuhp_state_remove_instance_nocalls(io_wq_online, &wq->cpuhp_node);
1197
Jens Axboee9418942021-02-19 12:33:30 -07001198 for_each_node(node) {
1199 struct io_wqe *wqe = wq->wqes[node];
Jens Axboef5d2d232021-03-25 10:16:12 -06001200 struct io_cb_cancel_data match = {
1201 .fn = io_wq_work_match_all,
1202 .cancel_all = true,
1203 };
1204 io_wqe_cancel_pending_work(wqe, &match);
Jens Axboe0e034962021-06-17 10:08:11 -06001205 free_cpumask_var(wqe->cpu_mask);
Jens Axboee9418942021-02-19 12:33:30 -07001206 kfree(wqe);
1207 }
Jens Axboee9418942021-02-19 12:33:30 -07001208 io_wq_put_hash(wq->hash);
Jens Axboe771b53d02019-10-22 10:25:58 -06001209 kfree(wq);
Jens Axboe4fb6ac32021-02-25 10:17:09 -07001210}
1211
Jens Axboeafcc4012021-02-26 13:48:19 -07001212void io_wq_put_and_exit(struct io_wq *wq)
1213{
Pavel Begunkov17a91052021-05-23 15:48:39 +01001214 WARN_ON_ONCE(!test_bit(IO_WQ_BIT_EXIT, &wq->state));
1215
Jens Axboe685fe7f2021-03-08 09:37:51 -07001216 io_wq_exit_workers(wq);
Pavel Begunkov382cb032021-06-14 02:36:13 +01001217 io_wq_destroy(wq);
Jens Axboeafcc4012021-02-26 13:48:19 -07001218}
1219
Jens Axboe0e034962021-06-17 10:08:11 -06001220struct online_data {
1221 unsigned int cpu;
1222 bool online;
1223};
1224
Jens Axboe43c01fb2020-10-22 09:02:50 -06001225static bool io_wq_worker_affinity(struct io_worker *worker, void *data)
1226{
Jens Axboe0e034962021-06-17 10:08:11 -06001227 struct online_data *od = data;
Jens Axboe43c01fb2020-10-22 09:02:50 -06001228
Jens Axboe0e034962021-06-17 10:08:11 -06001229 if (od->online)
1230 cpumask_set_cpu(od->cpu, worker->wqe->cpu_mask);
1231 else
1232 cpumask_clear_cpu(od->cpu, worker->wqe->cpu_mask);
Jens Axboe43c01fb2020-10-22 09:02:50 -06001233 return false;
1234}
1235
Jens Axboe0e034962021-06-17 10:08:11 -06001236static int __io_wq_cpu_online(struct io_wq *wq, unsigned int cpu, bool online)
1237{
1238 struct online_data od = {
1239 .cpu = cpu,
1240 .online = online
1241 };
1242 int i;
1243
1244 rcu_read_lock();
1245 for_each_node(i)
1246 io_wq_for_each_worker(wq->wqes[i], io_wq_worker_affinity, &od);
1247 rcu_read_unlock();
1248 return 0;
1249}
1250
Jens Axboe43c01fb2020-10-22 09:02:50 -06001251static int io_wq_cpu_online(unsigned int cpu, struct hlist_node *node)
1252{
1253 struct io_wq *wq = hlist_entry_safe(node, struct io_wq, cpuhp_node);
Jens Axboe43c01fb2020-10-22 09:02:50 -06001254
Jens Axboe0e034962021-06-17 10:08:11 -06001255 return __io_wq_cpu_online(wq, cpu, true);
1256}
1257
1258static int io_wq_cpu_offline(unsigned int cpu, struct hlist_node *node)
1259{
1260 struct io_wq *wq = hlist_entry_safe(node, struct io_wq, cpuhp_node);
1261
1262 return __io_wq_cpu_online(wq, cpu, false);
Jens Axboe43c01fb2020-10-22 09:02:50 -06001263}
1264
Jens Axboefe764212021-06-17 10:19:54 -06001265int io_wq_cpu_affinity(struct io_wq *wq, cpumask_var_t mask)
1266{
1267 int i;
1268
1269 rcu_read_lock();
1270 for_each_node(i) {
1271 struct io_wqe *wqe = wq->wqes[i];
1272
1273 if (mask)
1274 cpumask_copy(wqe->cpu_mask, mask);
1275 else
1276 cpumask_copy(wqe->cpu_mask, cpumask_of_node(i));
1277 }
1278 rcu_read_unlock();
1279 return 0;
1280}
1281
Jens Axboe2e480052021-08-27 11:33:19 -06001282/*
1283 * Set max number of unbounded workers, returns old value. If new_count is 0,
1284 * then just return the old value.
1285 */
1286int io_wq_max_workers(struct io_wq *wq, int *new_count)
1287{
1288 int i, node, prev = 0;
1289
1290 for (i = 0; i < 2; i++) {
1291 if (new_count[i] > task_rlimit(current, RLIMIT_NPROC))
1292 new_count[i] = task_rlimit(current, RLIMIT_NPROC);
1293 }
1294
1295 rcu_read_lock();
1296 for_each_node(node) {
1297 struct io_wqe_acct *acct;
1298
Jens Axboef95dc202021-08-31 13:57:32 -06001299 for (i = 0; i < IO_WQ_ACCT_NR; i++) {
Jens Axboe2e480052021-08-27 11:33:19 -06001300 acct = &wq->wqes[node]->acct[i];
1301 prev = max_t(int, acct->max_workers, prev);
1302 if (new_count[i])
1303 acct->max_workers = new_count[i];
1304 new_count[i] = prev;
1305 }
1306 }
1307 rcu_read_unlock();
1308 return 0;
1309}
1310
Jens Axboe43c01fb2020-10-22 09:02:50 -06001311static __init int io_wq_init(void)
1312{
1313 int ret;
1314
1315 ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "io-wq/online",
Jens Axboe0e034962021-06-17 10:08:11 -06001316 io_wq_cpu_online, io_wq_cpu_offline);
Jens Axboe43c01fb2020-10-22 09:02:50 -06001317 if (ret < 0)
1318 return ret;
1319 io_wq_online = ret;
1320 return 0;
1321}
1322subsys_initcall(io_wq_init);