blob: c2e73ce6888a36d51199d08d62930f4721355db9 [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 Begunkov58e393192020-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 Begunkov58e393192020-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);
712 return;
713 }
714
715 /* re-create attempts grab a new worker ref, drop the existing one */
716 io_worker_release(worker);
717 schedule_work(&worker->work);
718}
719
720static void io_workqueue_create(struct work_struct *work)
721{
722 struct io_worker *worker = container_of(work, struct io_worker, work);
723 struct io_wqe_acct *acct = io_wqe_get_acct(worker);
724
725 if (!io_queue_worker_create(worker, acct, create_worker_cont)) {
726 clear_bit_unlock(0, &worker->create_state);
727 io_worker_release(worker);
728 }
729}
730
731static bool create_io_worker(struct io_wq *wq, struct io_wqe *wqe, int index)
732{
733 struct io_wqe_acct *acct = &wqe->acct[index];
734 struct io_worker *worker;
735 struct task_struct *tsk;
736
737 __set_current_state(TASK_RUNNING);
738
739 worker = kzalloc_node(sizeof(*worker), GFP_KERNEL, wqe->node);
740 if (!worker) {
741fail:
742 atomic_dec(&acct->nr_running);
743 raw_spin_lock(&wqe->lock);
744 acct->nr_workers--;
745 raw_spin_unlock(&wqe->lock);
746 io_worker_ref_put(wq);
747 return false;
748 }
749
750 refcount_set(&worker->ref, 1);
751 worker->wqe = wqe;
752 spin_lock_init(&worker->lock);
753 init_completion(&worker->ref_done);
754
755 if (index == IO_WQ_ACCT_BOUND)
756 worker->flags |= IO_WORKER_F_BOUND;
757
758 tsk = create_io_thread(io_wqe_worker, worker, wqe->node);
759 if (!IS_ERR(tsk)) {
760 io_init_new_worker(wqe, worker, tsk);
761 } else if (!io_should_retry_thread(PTR_ERR(tsk))) {
762 goto fail;
763 } else {
764 INIT_WORK(&worker->work, io_workqueue_create);
765 schedule_work(&worker->work);
766 }
767
768 return true;
769}
770
Hillf Dantonc4068bf2020-09-26 21:26:55 +0800771/*
772 * Iterate the passed in list and call the specific function for each
773 * worker that isn't exiting
774 */
775static bool io_wq_for_each_worker(struct io_wqe *wqe,
776 bool (*func)(struct io_worker *, void *),
777 void *data)
778{
779 struct io_worker *worker;
780 bool ret = false;
781
782 list_for_each_entry_rcu(worker, &wqe->all_list, all_list) {
783 if (io_worker_get(worker)) {
784 /* no task if node is/was offline */
785 if (worker->task)
786 ret = func(worker, data);
787 io_worker_release(worker);
788 if (ret)
789 break;
790 }
791 }
792
793 return ret;
794}
795
796static bool io_wq_worker_wake(struct io_worker *worker, void *data)
797{
Jens Axboe46fe18b2021-03-04 12:39:36 -0700798 set_notify_signal(worker->task);
Hillf Dantonc4068bf2020-09-26 21:26:55 +0800799 wake_up_process(worker->task);
800 return false;
801}
802
Pavel Begunkove9fd9392020-03-04 16:14:12 +0300803static void io_run_cancel(struct io_wq_work *work, struct io_wqe *wqe)
Pavel Begunkovfc04c392020-03-01 19:18:19 +0300804{
Pavel Begunkove9fd9392020-03-04 16:14:12 +0300805 struct io_wq *wq = wqe->wq;
806
Pavel Begunkovfc04c392020-03-01 19:18:19 +0300807 do {
Pavel Begunkovfc04c392020-03-01 19:18:19 +0300808 work->flags |= IO_WQ_WORK_CANCEL;
Pavel Begunkov5280f7e2021-02-04 13:52:08 +0000809 wq->do_work(work);
810 work = wq->free_work(work);
Pavel Begunkovfc04c392020-03-01 19:18:19 +0300811 } while (work);
812}
813
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300814static void io_wqe_insert_work(struct io_wqe *wqe, struct io_wq_work *work)
815{
Jens Axboef95dc202021-08-31 13:57:32 -0600816 struct io_wqe_acct *acct = io_work_get_acct(wqe, work);
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300817 unsigned int hash;
818 struct io_wq_work *tail;
819
820 if (!io_wq_is_hashed(work)) {
821append:
Jens Axboef95dc202021-08-31 13:57:32 -0600822 wq_list_add_tail(&work->list, &acct->work_list);
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300823 return;
824 }
825
826 hash = io_get_work_hash(work);
827 tail = wqe->hash_tail[hash];
828 wqe->hash_tail[hash] = work;
829 if (!tail)
830 goto append;
831
Jens Axboef95dc202021-08-31 13:57:32 -0600832 wq_list_add_after(&work->list, &tail->list, &acct->work_list);
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300833}
834
Pavel Begunkov713b9822021-09-08 10:09:29 +0100835static bool io_wq_work_match_item(struct io_wq_work *work, void *data)
836{
837 return work == data;
838}
839
Jens Axboe771b53d02019-10-22 10:25:58 -0600840static void io_wqe_enqueue(struct io_wqe *wqe, struct io_wq_work *work)
841{
Jens Axboec5def4a2019-11-07 11:41:16 -0700842 struct io_wqe_acct *acct = io_work_get_acct(wqe, work);
Jens Axboe94ffb0a2021-08-30 11:55:22 -0600843 unsigned work_flags = work->flags;
844 bool do_create;
Jens Axboe771b53d02019-10-22 10:25:58 -0600845
Jens Axboe991468d2021-07-23 11:53:54 -0600846 /*
847 * If io-wq is exiting for this task, or if the request has explicitly
848 * been marked as one that should not get executed, cancel it here.
849 */
850 if (test_bit(IO_WQ_BIT_EXIT, &wqe->wq->state) ||
851 (work->flags & IO_WQ_WORK_CANCEL)) {
yangerkun70e35122021-03-09 11:04:10 +0800852 io_run_cancel(work, wqe);
Jens Axboe4fb6ac32021-02-25 10:17:09 -0700853 return;
854 }
855
Jens Axboea9a4aa92021-08-30 06:33:08 -0600856 raw_spin_lock(&wqe->lock);
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300857 io_wqe_insert_work(wqe, work);
Jens Axboef95dc202021-08-31 13:57:32 -0600858 clear_bit(IO_ACCT_STALLED_BIT, &acct->flags);
Jens Axboe94ffb0a2021-08-30 11:55:22 -0600859
860 rcu_read_lock();
Jens Axboef95dc202021-08-31 13:57:32 -0600861 do_create = !io_wqe_activate_free_worker(wqe, acct);
Jens Axboe94ffb0a2021-08-30 11:55:22 -0600862 rcu_read_unlock();
863
Jens Axboea9a4aa92021-08-30 06:33:08 -0600864 raw_spin_unlock(&wqe->lock);
Jens Axboe771b53d02019-10-22 10:25:58 -0600865
Jens Axboe94ffb0a2021-08-30 11:55:22 -0600866 if (do_create && ((work_flags & IO_WQ_WORK_CONCURRENT) ||
Jens Axboe3146cba2021-09-01 11:20:10 -0600867 !atomic_read(&acct->nr_running))) {
868 bool did_create;
869
870 did_create = io_wqe_create_worker(wqe, acct);
Pavel Begunkov713b9822021-09-08 10:09:29 +0100871 if (likely(did_create))
872 return;
873
874 raw_spin_lock(&wqe->lock);
875 /* fatal condition, failed to create the first worker */
876 if (!acct->nr_workers) {
877 struct io_cb_cancel_data match = {
878 .fn = io_wq_work_match_item,
879 .data = work,
880 .cancel_all = false,
881 };
882
883 if (io_acct_cancel_pending_work(wqe, acct, &match))
884 raw_spin_lock(&wqe->lock);
Jens Axboe3146cba2021-09-01 11:20:10 -0600885 }
Pavel Begunkov713b9822021-09-08 10:09:29 +0100886 raw_spin_unlock(&wqe->lock);
Jens Axboe3146cba2021-09-01 11:20:10 -0600887 }
Jens Axboe771b53d02019-10-22 10:25:58 -0600888}
889
890void io_wq_enqueue(struct io_wq *wq, struct io_wq_work *work)
891{
892 struct io_wqe *wqe = wq->wqes[numa_node_id()];
893
894 io_wqe_enqueue(wqe, work);
895}
896
897/*
Pavel Begunkov8766dd52020-03-14 00:31:04 +0300898 * Work items that hash to the same value will not be done in parallel.
899 * Used to limit concurrent writes, generally hashed by inode.
Jens Axboe771b53d02019-10-22 10:25:58 -0600900 */
Pavel Begunkov8766dd52020-03-14 00:31:04 +0300901void io_wq_hash_work(struct io_wq_work *work, void *val)
Jens Axboe771b53d02019-10-22 10:25:58 -0600902{
Pavel Begunkov8766dd52020-03-14 00:31:04 +0300903 unsigned int bit;
Jens Axboe771b53d02019-10-22 10:25:58 -0600904
905 bit = hash_ptr(val, IO_WQ_HASH_ORDER);
906 work->flags |= (IO_WQ_WORK_HASHED | (bit << IO_WQ_HASH_SHIFT));
Jens Axboe771b53d02019-10-22 10:25:58 -0600907}
908
Pavel Begunkov2293b412020-03-07 01:15:39 +0300909static bool io_wq_worker_cancel(struct io_worker *worker, void *data)
Jens Axboe62755e32019-10-28 21:49:21 -0600910{
Pavel Begunkov2293b412020-03-07 01:15:39 +0300911 struct io_cb_cancel_data *match = data;
Jens Axboe62755e32019-10-28 21:49:21 -0600912
913 /*
914 * Hold the lock to avoid ->cur_work going out of scope, caller
Jens Axboe36c2f922019-11-13 09:43:34 -0700915 * may dereference the passed in work.
Jens Axboe62755e32019-10-28 21:49:21 -0600916 */
Jens Axboea9a4aa92021-08-30 06:33:08 -0600917 spin_lock(&worker->lock);
Jens Axboe62755e32019-10-28 21:49:21 -0600918 if (worker->cur_work &&
Pavel Begunkov2293b412020-03-07 01:15:39 +0300919 match->fn(worker->cur_work, match->data)) {
Jens Axboe3bfe6102021-02-16 14:15:30 -0700920 set_notify_signal(worker->task);
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300921 match->nr_running++;
Jens Axboe771b53d02019-10-22 10:25:58 -0600922 }
Jens Axboea9a4aa92021-08-30 06:33:08 -0600923 spin_unlock(&worker->lock);
Jens Axboe771b53d02019-10-22 10:25:58 -0600924
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300925 return match->nr_running && !match->cancel_all;
Jens Axboe771b53d02019-10-22 10:25:58 -0600926}
927
Pavel Begunkov204361a2020-08-23 20:33:10 +0300928static inline void io_wqe_remove_pending(struct io_wqe *wqe,
929 struct io_wq_work *work,
930 struct io_wq_work_node *prev)
931{
Jens Axboef95dc202021-08-31 13:57:32 -0600932 struct io_wqe_acct *acct = io_work_get_acct(wqe, work);
Pavel Begunkov204361a2020-08-23 20:33:10 +0300933 unsigned int hash = io_get_work_hash(work);
934 struct io_wq_work *prev_work = NULL;
935
936 if (io_wq_is_hashed(work) && work == wqe->hash_tail[hash]) {
937 if (prev)
938 prev_work = container_of(prev, struct io_wq_work, list);
939 if (prev_work && io_get_work_hash(prev_work) == hash)
940 wqe->hash_tail[hash] = prev_work;
941 else
942 wqe->hash_tail[hash] = NULL;
943 }
Jens Axboef95dc202021-08-31 13:57:32 -0600944 wq_list_del(&acct->work_list, &work->list, prev);
Pavel Begunkov204361a2020-08-23 20:33:10 +0300945}
946
Jens Axboe3146cba2021-09-01 11:20:10 -0600947static bool io_acct_cancel_pending_work(struct io_wqe *wqe,
948 struct io_wqe_acct *acct,
949 struct io_cb_cancel_data *match)
950 __releases(wqe->lock)
Jens Axboe771b53d02019-10-22 10:25:58 -0600951{
Jens Axboe6206f0e2019-11-26 11:59:32 -0700952 struct io_wq_work_node *node, *prev;
Jens Axboe771b53d02019-10-22 10:25:58 -0600953 struct io_wq_work *work;
Jens Axboe771b53d02019-10-22 10:25:58 -0600954
Jens Axboe3146cba2021-09-01 11:20:10 -0600955 wq_list_for_each(node, prev, &acct->work_list) {
956 work = container_of(node, struct io_wq_work, list);
957 if (!match->fn(work, match->data))
958 continue;
959 io_wqe_remove_pending(wqe, work, prev);
960 raw_spin_unlock(&wqe->lock);
961 io_run_cancel(work, wqe);
962 match->nr_pending++;
963 /* not safe to continue after unlock */
964 return true;
965 }
966
967 return false;
968}
969
970static void io_wqe_cancel_pending_work(struct io_wqe *wqe,
971 struct io_cb_cancel_data *match)
972{
973 int i;
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300974retry:
Jens Axboea9a4aa92021-08-30 06:33:08 -0600975 raw_spin_lock(&wqe->lock);
Jens Axboef95dc202021-08-31 13:57:32 -0600976 for (i = 0; i < IO_WQ_ACCT_NR; i++) {
977 struct io_wqe_acct *acct = io_get_acct(wqe, i == 0);
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300978
Jens Axboe3146cba2021-09-01 11:20:10 -0600979 if (io_acct_cancel_pending_work(wqe, acct, match)) {
980 if (match->cancel_all)
981 goto retry;
982 return;
Jens Axboef95dc202021-08-31 13:57:32 -0600983 }
Jens Axboe771b53d02019-10-22 10:25:58 -0600984 }
Jens Axboea9a4aa92021-08-30 06:33:08 -0600985 raw_spin_unlock(&wqe->lock);
Pavel Begunkovf4c26652020-06-15 10:24:02 +0300986}
Jens Axboe771b53d02019-10-22 10:25:58 -0600987
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300988static void io_wqe_cancel_running_work(struct io_wqe *wqe,
Pavel Begunkovf4c26652020-06-15 10:24:02 +0300989 struct io_cb_cancel_data *match)
990{
Jens Axboe771b53d02019-10-22 10:25:58 -0600991 rcu_read_lock();
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300992 io_wq_for_each_worker(wqe, io_wq_worker_cancel, match);
Jens Axboe771b53d02019-10-22 10:25:58 -0600993 rcu_read_unlock();
Jens Axboe771b53d02019-10-22 10:25:58 -0600994}
995
Pavel Begunkov2293b412020-03-07 01:15:39 +0300996enum io_wq_cancel io_wq_cancel_cb(struct io_wq *wq, work_cancel_fn *cancel,
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300997 void *data, bool cancel_all)
Pavel Begunkov2293b412020-03-07 01:15:39 +0300998{
999 struct io_cb_cancel_data match = {
Pavel Begunkov4f26bda2020-06-15 10:24:03 +03001000 .fn = cancel,
1001 .data = data,
1002 .cancel_all = cancel_all,
Pavel Begunkov2293b412020-03-07 01:15:39 +03001003 };
Pavel Begunkov2293b412020-03-07 01:15:39 +03001004 int node;
1005
Pavel Begunkovf4c26652020-06-15 10:24:02 +03001006 /*
1007 * First check pending list, if we're lucky we can just remove it
1008 * from there. CANCEL_OK means that the work is returned as-new,
1009 * no completion will be posted for it.
1010 */
Pavel Begunkov2293b412020-03-07 01:15:39 +03001011 for_each_node(node) {
1012 struct io_wqe *wqe = wq->wqes[node];
1013
Pavel Begunkov4f26bda2020-06-15 10:24:03 +03001014 io_wqe_cancel_pending_work(wqe, &match);
1015 if (match.nr_pending && !match.cancel_all)
Pavel Begunkovf4c26652020-06-15 10:24:02 +03001016 return IO_WQ_CANCEL_OK;
Pavel Begunkov2293b412020-03-07 01:15:39 +03001017 }
1018
Pavel Begunkovf4c26652020-06-15 10:24:02 +03001019 /*
1020 * Now check if a free (going busy) or busy worker has the work
1021 * currently running. If we find it there, we'll return CANCEL_RUNNING
1022 * as an indication that we attempt to signal cancellation. The
1023 * completion will run normally in this case.
1024 */
1025 for_each_node(node) {
1026 struct io_wqe *wqe = wq->wqes[node];
1027
Pavel Begunkov4f26bda2020-06-15 10:24:03 +03001028 io_wqe_cancel_running_work(wqe, &match);
1029 if (match.nr_running && !match.cancel_all)
Pavel Begunkovf4c26652020-06-15 10:24:02 +03001030 return IO_WQ_CANCEL_RUNNING;
1031 }
1032
Pavel Begunkov4f26bda2020-06-15 10:24:03 +03001033 if (match.nr_running)
1034 return IO_WQ_CANCEL_RUNNING;
1035 if (match.nr_pending)
1036 return IO_WQ_CANCEL_OK;
Pavel Begunkovf4c26652020-06-15 10:24:02 +03001037 return IO_WQ_CANCEL_NOTFOUND;
Pavel Begunkov2293b412020-03-07 01:15:39 +03001038}
1039
Jens Axboee9418942021-02-19 12:33:30 -07001040static int io_wqe_hash_wake(struct wait_queue_entry *wait, unsigned mode,
1041 int sync, void *key)
1042{
1043 struct io_wqe *wqe = container_of(wait, struct io_wqe, wait);
Jens Axboef95dc202021-08-31 13:57:32 -06001044 int i;
Jens Axboee9418942021-02-19 12:33:30 -07001045
1046 list_del_init(&wait->entry);
1047
1048 rcu_read_lock();
Jens Axboef95dc202021-08-31 13:57:32 -06001049 for (i = 0; i < IO_WQ_ACCT_NR; i++) {
1050 struct io_wqe_acct *acct = &wqe->acct[i];
1051
1052 if (test_and_clear_bit(IO_ACCT_STALLED_BIT, &acct->flags))
1053 io_wqe_activate_free_worker(wqe, acct);
1054 }
Jens Axboee9418942021-02-19 12:33:30 -07001055 rcu_read_unlock();
Jens Axboee9418942021-02-19 12:33:30 -07001056 return 1;
1057}
1058
Jens Axboe576a3472019-11-25 08:49:20 -07001059struct io_wq *io_wq_create(unsigned bounded, struct io_wq_data *data)
Jens Axboe771b53d02019-10-22 10:25:58 -06001060{
Jens Axboef95dc202021-08-31 13:57:32 -06001061 int ret, node, i;
Jens Axboe771b53d02019-10-22 10:25:58 -06001062 struct io_wq *wq;
1063
Pavel Begunkovf5fa38c2020-06-08 21:08:20 +03001064 if (WARN_ON_ONCE(!data->free_work || !data->do_work))
Pavel Begunkove9fd9392020-03-04 16:14:12 +03001065 return ERR_PTR(-EINVAL);
Pavel Begunkove6ab8992021-06-17 18:13:59 +01001066 if (WARN_ON_ONCE(!bounded))
1067 return ERR_PTR(-EINVAL);
Pavel Begunkove9fd9392020-03-04 16:14:12 +03001068
Pavel Begunkovc7f405d2021-06-14 02:36:12 +01001069 wq = kzalloc(struct_size(wq, wqes, nr_node_ids), GFP_KERNEL);
Jens Axboe771b53d02019-10-22 10:25:58 -06001070 if (!wq)
1071 return ERR_PTR(-ENOMEM);
Jens Axboe43c01fb2020-10-22 09:02:50 -06001072 ret = cpuhp_state_add_instance_nocalls(io_wq_online, &wq->cpuhp_node);
1073 if (ret)
Pavel Begunkovc7f405d2021-06-14 02:36:12 +01001074 goto err_wq;
Jens Axboe771b53d02019-10-22 10:25:58 -06001075
Jens Axboee9418942021-02-19 12:33:30 -07001076 refcount_inc(&data->hash->refs);
1077 wq->hash = data->hash;
Pavel Begunkove9fd9392020-03-04 16:14:12 +03001078 wq->free_work = data->free_work;
Pavel Begunkovf5fa38c2020-06-08 21:08:20 +03001079 wq->do_work = data->do_work;
Jens Axboe7d723062019-11-12 22:31:31 -07001080
Jens Axboe43c01fb2020-10-22 09:02:50 -06001081 ret = -ENOMEM;
Jann Horn3fc50ab2019-11-26 19:10:20 +01001082 for_each_node(node) {
Jens Axboe771b53d02019-10-22 10:25:58 -06001083 struct io_wqe *wqe;
Jens Axboe75634392020-02-11 06:30:06 -07001084 int alloc_node = node;
Jens Axboe771b53d02019-10-22 10:25:58 -06001085
Jens Axboe75634392020-02-11 06:30:06 -07001086 if (!node_online(alloc_node))
1087 alloc_node = NUMA_NO_NODE;
1088 wqe = kzalloc_node(sizeof(struct io_wqe), GFP_KERNEL, alloc_node);
Jens Axboe771b53d02019-10-22 10:25:58 -06001089 if (!wqe)
Jann Horn3fc50ab2019-11-26 19:10:20 +01001090 goto err;
Jens Axboe0e034962021-06-17 10:08:11 -06001091 if (!alloc_cpumask_var(&wqe->cpu_mask, GFP_KERNEL))
1092 goto err;
1093 cpumask_copy(wqe->cpu_mask, cpumask_of_node(node));
Jann Horn3fc50ab2019-11-26 19:10:20 +01001094 wq->wqes[node] = wqe;
Jens Axboe75634392020-02-11 06:30:06 -07001095 wqe->node = alloc_node;
Jens Axboec5def4a2019-11-07 11:41:16 -07001096 wqe->acct[IO_WQ_ACCT_BOUND].max_workers = bounded;
Jens Axboe728f13e2021-02-21 16:02:53 -07001097 wqe->acct[IO_WQ_ACCT_UNBOUND].max_workers =
Jens Axboec5def4a2019-11-07 11:41:16 -07001098 task_rlimit(current, RLIMIT_NPROC);
Jens Axboee9418942021-02-19 12:33:30 -07001099 INIT_LIST_HEAD(&wqe->wait.entry);
Jens Axboef95dc202021-08-31 13:57:32 -06001100 wqe->wait.func = io_wqe_hash_wake;
1101 for (i = 0; i < IO_WQ_ACCT_NR; i++) {
1102 struct io_wqe_acct *acct = &wqe->acct[i];
1103
1104 acct->index = i;
1105 atomic_set(&acct->nr_running, 0);
1106 INIT_WQ_LIST(&acct->work_list);
1107 }
Jens Axboe771b53d02019-10-22 10:25:58 -06001108 wqe->wq = wq;
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +02001109 raw_spin_lock_init(&wqe->lock);
Jens Axboe021d1cd2019-11-14 08:00:41 -07001110 INIT_HLIST_NULLS_HEAD(&wqe->free_list, 0);
Jens Axboee61df662019-11-13 13:54:49 -07001111 INIT_LIST_HEAD(&wqe->all_list);
Jens Axboe771b53d02019-10-22 10:25:58 -06001112 }
1113
Jens Axboe685fe7f2021-03-08 09:37:51 -07001114 wq->task = get_task_struct(data->task);
Jens Axboe685fe7f2021-03-08 09:37:51 -07001115 atomic_set(&wq->worker_refs, 1);
1116 init_completion(&wq->worker_done);
1117 return wq;
Jens Axboeb60fda62019-11-19 08:37:07 -07001118err:
Jens Axboedc7bbc92021-03-01 09:09:56 -07001119 io_wq_put_hash(data->hash);
Jens Axboe43c01fb2020-10-22 09:02:50 -06001120 cpuhp_state_remove_instance_nocalls(io_wq_online, &wq->cpuhp_node);
Jens Axboe0e034962021-06-17 10:08:11 -06001121 for_each_node(node) {
1122 if (!wq->wqes[node])
1123 continue;
1124 free_cpumask_var(wq->wqes[node]->cpu_mask);
Jann Horn3fc50ab2019-11-26 19:10:20 +01001125 kfree(wq->wqes[node]);
Jens Axboe0e034962021-06-17 10:08:11 -06001126 }
Jens Axboe43c01fb2020-10-22 09:02:50 -06001127err_wq:
Jens Axboeb60fda62019-11-19 08:37:07 -07001128 kfree(wq);
Jens Axboe771b53d02019-10-22 10:25:58 -06001129 return ERR_PTR(ret);
1130}
1131
Jens Axboec80ca472021-04-01 19:57:07 -06001132static bool io_task_work_match(struct callback_head *cb, void *data)
1133{
Jens Axboed3e9f732021-08-04 08:37:25 -06001134 struct io_worker *worker;
Jens Axboec80ca472021-04-01 19:57:07 -06001135
Jens Axboe3b33e3f2021-09-08 19:57:26 -06001136 if (cb->func != create_worker_cb && cb->func != create_worker_cont)
Jens Axboec80ca472021-04-01 19:57:07 -06001137 return false;
Jens Axboed3e9f732021-08-04 08:37:25 -06001138 worker = container_of(cb, struct io_worker, create_work);
1139 return worker->wqe->wq == data;
Jens Axboec80ca472021-04-01 19:57:07 -06001140}
1141
Pavel Begunkov17a91052021-05-23 15:48:39 +01001142void io_wq_exit_start(struct io_wq *wq)
1143{
1144 set_bit(IO_WQ_BIT_EXIT, &wq->state);
1145}
1146
Jens Axboe685fe7f2021-03-08 09:37:51 -07001147static void io_wq_exit_workers(struct io_wq *wq)
Jens Axboeafcc4012021-02-26 13:48:19 -07001148{
Jens Axboe685fe7f2021-03-08 09:37:51 -07001149 struct callback_head *cb;
1150 int node;
1151
Jens Axboe685fe7f2021-03-08 09:37:51 -07001152 if (!wq->task)
1153 return;
1154
Jens Axboec80ca472021-04-01 19:57:07 -06001155 while ((cb = task_work_cancel_match(wq->task, io_task_work_match, wq)) != NULL) {
Jens Axboed3e9f732021-08-04 08:37:25 -06001156 struct io_worker *worker;
Jens Axboe3b33e3f2021-09-08 19:57:26 -06001157 struct io_wqe_acct *acct;
Jens Axboe685fe7f2021-03-08 09:37:51 -07001158
Jens Axboed3e9f732021-08-04 08:37:25 -06001159 worker = container_of(cb, struct io_worker, create_work);
Jens Axboe3b33e3f2021-09-08 19:57:26 -06001160 acct = io_wqe_get_acct(worker);
1161 atomic_dec(&acct->nr_running);
1162 raw_spin_lock(&worker->wqe->lock);
1163 acct->nr_workers--;
1164 raw_spin_unlock(&worker->wqe->lock);
Jens Axboe685fe7f2021-03-08 09:37:51 -07001165 io_worker_ref_put(wq);
Jens Axboed3e9f732021-08-04 08:37:25 -06001166 clear_bit_unlock(0, &worker->create_state);
1167 io_worker_release(worker);
Jens Axboeafcc4012021-02-26 13:48:19 -07001168 }
Jens Axboe685fe7f2021-03-08 09:37:51 -07001169
1170 rcu_read_lock();
1171 for_each_node(node) {
1172 struct io_wqe *wqe = wq->wqes[node];
1173
1174 io_wq_for_each_worker(wqe, io_wq_worker_wake, NULL);
Jens Axboe685fe7f2021-03-08 09:37:51 -07001175 }
1176 rcu_read_unlock();
1177 io_worker_ref_put(wq);
1178 wait_for_completion(&wq->worker_done);
Zqiang3743c172021-05-26 13:08:26 +08001179
1180 for_each_node(node) {
1181 spin_lock_irq(&wq->hash->wait.lock);
1182 list_del_init(&wq->wqes[node]->wait.entry);
1183 spin_unlock_irq(&wq->hash->wait.lock);
1184 }
Jens Axboe685fe7f2021-03-08 09:37:51 -07001185 put_task_struct(wq->task);
1186 wq->task = NULL;
Jens Axboeafcc4012021-02-26 13:48:19 -07001187}
1188
Jens Axboe4fb6ac32021-02-25 10:17:09 -07001189static void io_wq_destroy(struct io_wq *wq)
Jens Axboe771b53d02019-10-22 10:25:58 -06001190{
Jann Horn3fc50ab2019-11-26 19:10:20 +01001191 int node;
Jens Axboe771b53d02019-10-22 10:25:58 -06001192
Jens Axboe43c01fb2020-10-22 09:02:50 -06001193 cpuhp_state_remove_instance_nocalls(io_wq_online, &wq->cpuhp_node);
1194
Jens Axboee9418942021-02-19 12:33:30 -07001195 for_each_node(node) {
1196 struct io_wqe *wqe = wq->wqes[node];
Jens Axboef5d2d23b2021-03-25 10:16:12 -06001197 struct io_cb_cancel_data match = {
1198 .fn = io_wq_work_match_all,
1199 .cancel_all = true,
1200 };
1201 io_wqe_cancel_pending_work(wqe, &match);
Jens Axboe0e034962021-06-17 10:08:11 -06001202 free_cpumask_var(wqe->cpu_mask);
Jens Axboee9418942021-02-19 12:33:30 -07001203 kfree(wqe);
1204 }
Jens Axboee9418942021-02-19 12:33:30 -07001205 io_wq_put_hash(wq->hash);
Jens Axboe771b53d02019-10-22 10:25:58 -06001206 kfree(wq);
Jens Axboe4fb6ac32021-02-25 10:17:09 -07001207}
1208
Jens Axboeafcc4012021-02-26 13:48:19 -07001209void io_wq_put_and_exit(struct io_wq *wq)
1210{
Pavel Begunkov17a91052021-05-23 15:48:39 +01001211 WARN_ON_ONCE(!test_bit(IO_WQ_BIT_EXIT, &wq->state));
1212
Jens Axboe685fe7f2021-03-08 09:37:51 -07001213 io_wq_exit_workers(wq);
Pavel Begunkov382cb032021-06-14 02:36:13 +01001214 io_wq_destroy(wq);
Jens Axboeafcc4012021-02-26 13:48:19 -07001215}
1216
Jens Axboe0e034962021-06-17 10:08:11 -06001217struct online_data {
1218 unsigned int cpu;
1219 bool online;
1220};
1221
Jens Axboe43c01fb2020-10-22 09:02:50 -06001222static bool io_wq_worker_affinity(struct io_worker *worker, void *data)
1223{
Jens Axboe0e034962021-06-17 10:08:11 -06001224 struct online_data *od = data;
Jens Axboe43c01fb2020-10-22 09:02:50 -06001225
Jens Axboe0e034962021-06-17 10:08:11 -06001226 if (od->online)
1227 cpumask_set_cpu(od->cpu, worker->wqe->cpu_mask);
1228 else
1229 cpumask_clear_cpu(od->cpu, worker->wqe->cpu_mask);
Jens Axboe43c01fb2020-10-22 09:02:50 -06001230 return false;
1231}
1232
Jens Axboe0e034962021-06-17 10:08:11 -06001233static int __io_wq_cpu_online(struct io_wq *wq, unsigned int cpu, bool online)
1234{
1235 struct online_data od = {
1236 .cpu = cpu,
1237 .online = online
1238 };
1239 int i;
1240
1241 rcu_read_lock();
1242 for_each_node(i)
1243 io_wq_for_each_worker(wq->wqes[i], io_wq_worker_affinity, &od);
1244 rcu_read_unlock();
1245 return 0;
1246}
1247
Jens Axboe43c01fb2020-10-22 09:02:50 -06001248static int io_wq_cpu_online(unsigned int cpu, struct hlist_node *node)
1249{
1250 struct io_wq *wq = hlist_entry_safe(node, struct io_wq, cpuhp_node);
Jens Axboe43c01fb2020-10-22 09:02:50 -06001251
Jens Axboe0e034962021-06-17 10:08:11 -06001252 return __io_wq_cpu_online(wq, cpu, true);
1253}
1254
1255static int io_wq_cpu_offline(unsigned int cpu, struct hlist_node *node)
1256{
1257 struct io_wq *wq = hlist_entry_safe(node, struct io_wq, cpuhp_node);
1258
1259 return __io_wq_cpu_online(wq, cpu, false);
Jens Axboe43c01fb2020-10-22 09:02:50 -06001260}
1261
Jens Axboefe764212021-06-17 10:19:54 -06001262int io_wq_cpu_affinity(struct io_wq *wq, cpumask_var_t mask)
1263{
1264 int i;
1265
1266 rcu_read_lock();
1267 for_each_node(i) {
1268 struct io_wqe *wqe = wq->wqes[i];
1269
1270 if (mask)
1271 cpumask_copy(wqe->cpu_mask, mask);
1272 else
1273 cpumask_copy(wqe->cpu_mask, cpumask_of_node(i));
1274 }
1275 rcu_read_unlock();
1276 return 0;
1277}
1278
Jens Axboe2e480052021-08-27 11:33:19 -06001279/*
1280 * Set max number of unbounded workers, returns old value. If new_count is 0,
1281 * then just return the old value.
1282 */
1283int io_wq_max_workers(struct io_wq *wq, int *new_count)
1284{
1285 int i, node, prev = 0;
1286
1287 for (i = 0; i < 2; i++) {
1288 if (new_count[i] > task_rlimit(current, RLIMIT_NPROC))
1289 new_count[i] = task_rlimit(current, RLIMIT_NPROC);
1290 }
1291
1292 rcu_read_lock();
1293 for_each_node(node) {
1294 struct io_wqe_acct *acct;
1295
Jens Axboef95dc202021-08-31 13:57:32 -06001296 for (i = 0; i < IO_WQ_ACCT_NR; i++) {
Jens Axboe2e480052021-08-27 11:33:19 -06001297 acct = &wq->wqes[node]->acct[i];
1298 prev = max_t(int, acct->max_workers, prev);
1299 if (new_count[i])
1300 acct->max_workers = new_count[i];
1301 new_count[i] = prev;
1302 }
1303 }
1304 rcu_read_unlock();
1305 return 0;
1306}
1307
Jens Axboe43c01fb2020-10-22 09:02:50 -06001308static __init int io_wq_init(void)
1309{
1310 int ret;
1311
1312 ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "io-wq/online",
Jens Axboe0e034962021-06-17 10:08:11 -06001313 io_wq_cpu_online, io_wq_cpu_offline);
Jens Axboe43c01fb2020-10-22 09:02:50 -06001314 if (ret < 0)
1315 return ret;
1316 io_wq_online = ret;
1317 return 0;
1318}
1319subsys_initcall(io_wq_init);