xref: /openbmc/linux/io_uring/io-wq.c (revision 34d6f206a88c2651d216bd3487ac956a40b2ba8e)
1ed29b0b4SJens Axboe // SPDX-License-Identifier: GPL-2.0
2ed29b0b4SJens Axboe /*
3ed29b0b4SJens Axboe  * Basic worker thread pool for io_uring
4ed29b0b4SJens Axboe  *
5ed29b0b4SJens Axboe  * Copyright (C) 2019 Jens Axboe
6ed29b0b4SJens Axboe  *
7ed29b0b4SJens Axboe  */
8ed29b0b4SJens Axboe #include <linux/kernel.h>
9ed29b0b4SJens Axboe #include <linux/init.h>
10ed29b0b4SJens Axboe #include <linux/errno.h>
11ed29b0b4SJens Axboe #include <linux/sched/signal.h>
12ed29b0b4SJens Axboe #include <linux/percpu.h>
13ed29b0b4SJens Axboe #include <linux/slab.h>
14ed29b0b4SJens Axboe #include <linux/rculist_nulls.h>
15ed29b0b4SJens Axboe #include <linux/cpu.h>
167b3a3558SFelix Moessbauer #include <linux/cpuset.h>
17ed29b0b4SJens Axboe #include <linux/task_work.h>
18ed29b0b4SJens Axboe #include <linux/audit.h>
19da64d6dbSBreno Leitao #include <linux/mmu_context.h>
20ed29b0b4SJens Axboe #include <uapi/linux/io_uring.h>
21ed29b0b4SJens Axboe 
22ed29b0b4SJens Axboe #include "io-wq.h"
23a6b21fbbSPavel Begunkov #include "slist.h"
24024f15e0SPavel Begunkov #include "io_uring.h"
25ed29b0b4SJens Axboe 
26ed29b0b4SJens Axboe #define WORKER_IDLE_TIMEOUT	(5 * HZ)
278d01e63fSPavel Begunkov #define WORKER_INIT_LIMIT	3
28ed29b0b4SJens Axboe 
29ed29b0b4SJens Axboe enum {
30ab702c34SBreno Leitao 	IO_WORKER_F_UP		= 0,	/* up and active */
31ab702c34SBreno Leitao 	IO_WORKER_F_RUNNING	= 1,	/* account as running */
32ab702c34SBreno Leitao 	IO_WORKER_F_FREE	= 2,	/* worker on free list */
33ab702c34SBreno Leitao 	IO_WORKER_F_BOUND	= 3,	/* is doing bounded work */
34ed29b0b4SJens Axboe };
35ed29b0b4SJens Axboe 
36ed29b0b4SJens Axboe enum {
37ed29b0b4SJens Axboe 	IO_WQ_BIT_EXIT		= 0,	/* wq exiting */
38ed29b0b4SJens Axboe };
39ed29b0b4SJens Axboe 
40ed29b0b4SJens Axboe enum {
41ed29b0b4SJens Axboe 	IO_ACCT_STALLED_BIT	= 0,	/* stalled on hash */
42ed29b0b4SJens Axboe };
43ed29b0b4SJens Axboe 
44ed29b0b4SJens Axboe /*
45eb47943fSGabriel Krisman Bertazi  * One for each thread in a wq pool
46ed29b0b4SJens Axboe  */
47ed29b0b4SJens Axboe struct io_worker {
48ed29b0b4SJens Axboe 	refcount_t ref;
49ab702c34SBreno Leitao 	int create_index;
50ab702c34SBreno Leitao 	unsigned long flags;
51ed29b0b4SJens Axboe 	struct hlist_nulls_node nulls_node;
52ed29b0b4SJens Axboe 	struct list_head all_list;
53ed29b0b4SJens Axboe 	struct task_struct *task;
54eb47943fSGabriel Krisman Bertazi 	struct io_wq *wq;
55ed29b0b4SJens Axboe 
56ed29b0b4SJens Axboe 	struct io_wq_work *cur_work;
57ed29b0b4SJens Axboe 	struct io_wq_work *next_work;
58ed29b0b4SJens Axboe 	raw_spinlock_t lock;
59ed29b0b4SJens Axboe 
60ed29b0b4SJens Axboe 	struct completion ref_done;
61ed29b0b4SJens Axboe 
62ed29b0b4SJens Axboe 	unsigned long create_state;
63ed29b0b4SJens Axboe 	struct callback_head create_work;
648d01e63fSPavel Begunkov 	int init_retries;
65ed29b0b4SJens Axboe 
66ed29b0b4SJens Axboe 	union {
67ed29b0b4SJens Axboe 		struct rcu_head rcu;
68ed29b0b4SJens Axboe 		struct work_struct work;
69ed29b0b4SJens Axboe 	};
70ed29b0b4SJens Axboe };
71ed29b0b4SJens Axboe 
72ed29b0b4SJens Axboe #if BITS_PER_LONG == 64
73ed29b0b4SJens Axboe #define IO_WQ_HASH_ORDER	6
74ed29b0b4SJens Axboe #else
75ed29b0b4SJens Axboe #define IO_WQ_HASH_ORDER	5
76ed29b0b4SJens Axboe #endif
77ed29b0b4SJens Axboe 
78ed29b0b4SJens Axboe #define IO_WQ_NR_HASH_BUCKETS	(1u << IO_WQ_HASH_ORDER)
79ed29b0b4SJens Axboe 
80dfd63bafSGabriel Krisman Bertazi struct io_wq_acct {
81ed29b0b4SJens Axboe 	unsigned nr_workers;
82ed29b0b4SJens Axboe 	unsigned max_workers;
83ed29b0b4SJens Axboe 	int index;
84ed29b0b4SJens Axboe 	atomic_t nr_running;
85ed29b0b4SJens Axboe 	raw_spinlock_t lock;
86ed29b0b4SJens Axboe 	struct io_wq_work_list work_list;
87ed29b0b4SJens Axboe 	unsigned long flags;
88ed29b0b4SJens Axboe };
89ed29b0b4SJens Axboe 
90ed29b0b4SJens Axboe enum {
91ed29b0b4SJens Axboe 	IO_WQ_ACCT_BOUND,
92ed29b0b4SJens Axboe 	IO_WQ_ACCT_UNBOUND,
93ed29b0b4SJens Axboe 	IO_WQ_ACCT_NR,
94ed29b0b4SJens Axboe };
95ed29b0b4SJens Axboe 
96ed29b0b4SJens Axboe /*
97ed29b0b4SJens Axboe  * Per io_wq state
98ed29b0b4SJens Axboe   */
99ed29b0b4SJens Axboe struct io_wq {
100ed29b0b4SJens Axboe 	unsigned long state;
101ed29b0b4SJens Axboe 
102ed29b0b4SJens Axboe 	free_work_fn *free_work;
103ed29b0b4SJens Axboe 	io_wq_work_fn *do_work;
104ed29b0b4SJens Axboe 
105ed29b0b4SJens Axboe 	struct io_wq_hash *hash;
106ed29b0b4SJens Axboe 
107ed29b0b4SJens Axboe 	atomic_t worker_refs;
108ed29b0b4SJens Axboe 	struct completion worker_done;
109ed29b0b4SJens Axboe 
110ed29b0b4SJens Axboe 	struct hlist_node cpuhp_node;
111ed29b0b4SJens Axboe 
112ed29b0b4SJens Axboe 	struct task_struct *task;
113ed29b0b4SJens Axboe 
114dfd63bafSGabriel Krisman Bertazi 	struct io_wq_acct acct[IO_WQ_ACCT_NR];
115dfd63bafSGabriel Krisman Bertazi 
116eb47943fSGabriel Krisman Bertazi 	/* lock protects access to elements below */
117eb47943fSGabriel Krisman Bertazi 	raw_spinlock_t lock;
118eb47943fSGabriel Krisman Bertazi 
119eb47943fSGabriel Krisman Bertazi 	struct hlist_nulls_head free_list;
120eb47943fSGabriel Krisman Bertazi 	struct list_head all_list;
121eb47943fSGabriel Krisman Bertazi 
122eb47943fSGabriel Krisman Bertazi 	struct wait_queue_entry wait;
123eb47943fSGabriel Krisman Bertazi 
124eb47943fSGabriel Krisman Bertazi 	struct io_wq_work *hash_tail[IO_WQ_NR_HASH_BUCKETS];
125eb47943fSGabriel Krisman Bertazi 
126eb47943fSGabriel Krisman Bertazi 	cpumask_var_t cpu_mask;
127ed29b0b4SJens Axboe };
128ed29b0b4SJens Axboe 
129ed29b0b4SJens Axboe static enum cpuhp_state io_wq_online;
130ed29b0b4SJens Axboe 
131ed29b0b4SJens Axboe struct io_cb_cancel_data {
132ed29b0b4SJens Axboe 	work_cancel_fn *fn;
133ed29b0b4SJens Axboe 	void *data;
134ed29b0b4SJens Axboe 	int nr_running;
135ed29b0b4SJens Axboe 	int nr_pending;
136ed29b0b4SJens Axboe 	bool cancel_all;
137ed29b0b4SJens Axboe };
138ed29b0b4SJens Axboe 
139eb47943fSGabriel Krisman Bertazi static bool create_io_worker(struct io_wq *wq, int index);
140eb47943fSGabriel Krisman Bertazi static void io_wq_dec_running(struct io_worker *worker);
141eb47943fSGabriel Krisman Bertazi static bool io_acct_cancel_pending_work(struct io_wq *wq,
142dfd63bafSGabriel Krisman Bertazi 					struct io_wq_acct *acct,
143ed29b0b4SJens Axboe 					struct io_cb_cancel_data *match);
144ed29b0b4SJens Axboe static void create_worker_cb(struct callback_head *cb);
145ed29b0b4SJens Axboe static void io_wq_cancel_tw_create(struct io_wq *wq);
146ed29b0b4SJens Axboe 
io_worker_get(struct io_worker * worker)147ed29b0b4SJens Axboe static bool io_worker_get(struct io_worker *worker)
148ed29b0b4SJens Axboe {
149ed29b0b4SJens Axboe 	return refcount_inc_not_zero(&worker->ref);
150ed29b0b4SJens Axboe }
151ed29b0b4SJens Axboe 
io_worker_release(struct io_worker * worker)152ed29b0b4SJens Axboe static void io_worker_release(struct io_worker *worker)
153ed29b0b4SJens Axboe {
154ed29b0b4SJens Axboe 	if (refcount_dec_and_test(&worker->ref))
155ed29b0b4SJens Axboe 		complete(&worker->ref_done);
156ed29b0b4SJens Axboe }
157ed29b0b4SJens Axboe 
io_get_acct(struct io_wq * wq,bool bound)158dfd63bafSGabriel Krisman Bertazi static inline struct io_wq_acct *io_get_acct(struct io_wq *wq, bool bound)
159ed29b0b4SJens Axboe {
160dfd63bafSGabriel Krisman Bertazi 	return &wq->acct[bound ? IO_WQ_ACCT_BOUND : IO_WQ_ACCT_UNBOUND];
161ed29b0b4SJens Axboe }
162ed29b0b4SJens Axboe 
io_work_get_acct(struct io_wq * wq,struct io_wq_work * work)163dfd63bafSGabriel Krisman Bertazi static inline struct io_wq_acct *io_work_get_acct(struct io_wq *wq,
164ed29b0b4SJens Axboe 						  struct io_wq_work *work)
165ed29b0b4SJens Axboe {
166dfd63bafSGabriel Krisman Bertazi 	return io_get_acct(wq, !(work->flags & IO_WQ_WORK_UNBOUND));
167ed29b0b4SJens Axboe }
168ed29b0b4SJens Axboe 
io_wq_get_acct(struct io_worker * worker)169dfd63bafSGabriel Krisman Bertazi static inline struct io_wq_acct *io_wq_get_acct(struct io_worker *worker)
170ed29b0b4SJens Axboe {
171ab702c34SBreno Leitao 	return io_get_acct(worker->wq, test_bit(IO_WORKER_F_BOUND, &worker->flags));
172ed29b0b4SJens Axboe }
173ed29b0b4SJens Axboe 
io_worker_ref_put(struct io_wq * wq)174ed29b0b4SJens Axboe static void io_worker_ref_put(struct io_wq *wq)
175ed29b0b4SJens Axboe {
176ed29b0b4SJens Axboe 	if (atomic_dec_and_test(&wq->worker_refs))
177ed29b0b4SJens Axboe 		complete(&wq->worker_done);
178ed29b0b4SJens Axboe }
179ed29b0b4SJens Axboe 
io_wq_worker_stopped(void)18045500dc4SPavel Begunkov bool io_wq_worker_stopped(void)
18145500dc4SPavel Begunkov {
18245500dc4SPavel Begunkov 	struct io_worker *worker = current->worker_private;
18345500dc4SPavel Begunkov 
18445500dc4SPavel Begunkov 	if (WARN_ON_ONCE(!io_wq_current_is_worker()))
18545500dc4SPavel Begunkov 		return true;
18645500dc4SPavel Begunkov 
18745500dc4SPavel Begunkov 	return test_bit(IO_WQ_BIT_EXIT, &worker->wq->state);
18845500dc4SPavel Begunkov }
18945500dc4SPavel Begunkov 
io_worker_cancel_cb(struct io_worker * worker)190ed29b0b4SJens Axboe static void io_worker_cancel_cb(struct io_worker *worker)
191ed29b0b4SJens Axboe {
192dfd63bafSGabriel Krisman Bertazi 	struct io_wq_acct *acct = io_wq_get_acct(worker);
193eb47943fSGabriel Krisman Bertazi 	struct io_wq *wq = worker->wq;
194ed29b0b4SJens Axboe 
195ed29b0b4SJens Axboe 	atomic_dec(&acct->nr_running);
196eb47943fSGabriel Krisman Bertazi 	raw_spin_lock(&wq->lock);
197ed29b0b4SJens Axboe 	acct->nr_workers--;
198eb47943fSGabriel Krisman Bertazi 	raw_spin_unlock(&wq->lock);
199ed29b0b4SJens Axboe 	io_worker_ref_put(wq);
200ed29b0b4SJens Axboe 	clear_bit_unlock(0, &worker->create_state);
201ed29b0b4SJens Axboe 	io_worker_release(worker);
202ed29b0b4SJens Axboe }
203ed29b0b4SJens Axboe 
io_task_worker_match(struct callback_head * cb,void * data)204ed29b0b4SJens Axboe static bool io_task_worker_match(struct callback_head *cb, void *data)
205ed29b0b4SJens Axboe {
206ed29b0b4SJens Axboe 	struct io_worker *worker;
207ed29b0b4SJens Axboe 
208ed29b0b4SJens Axboe 	if (cb->func != create_worker_cb)
209ed29b0b4SJens Axboe 		return false;
210ed29b0b4SJens Axboe 	worker = container_of(cb, struct io_worker, create_work);
211ed29b0b4SJens Axboe 	return worker == data;
212ed29b0b4SJens Axboe }
213ed29b0b4SJens Axboe 
io_worker_exit(struct io_worker * worker)214ed29b0b4SJens Axboe static void io_worker_exit(struct io_worker *worker)
215ed29b0b4SJens Axboe {
216eb47943fSGabriel Krisman Bertazi 	struct io_wq *wq = worker->wq;
217ed29b0b4SJens Axboe 
218ed29b0b4SJens Axboe 	while (1) {
219ed29b0b4SJens Axboe 		struct callback_head *cb = task_work_cancel_match(wq->task,
220ed29b0b4SJens Axboe 						io_task_worker_match, worker);
221ed29b0b4SJens Axboe 
222ed29b0b4SJens Axboe 		if (!cb)
223ed29b0b4SJens Axboe 			break;
224ed29b0b4SJens Axboe 		io_worker_cancel_cb(worker);
225ed29b0b4SJens Axboe 	}
226ed29b0b4SJens Axboe 
227ed29b0b4SJens Axboe 	io_worker_release(worker);
228ed29b0b4SJens Axboe 	wait_for_completion(&worker->ref_done);
229ed29b0b4SJens Axboe 
230eb47943fSGabriel Krisman Bertazi 	raw_spin_lock(&wq->lock);
231ab702c34SBreno Leitao 	if (test_bit(IO_WORKER_F_FREE, &worker->flags))
232ed29b0b4SJens Axboe 		hlist_nulls_del_rcu(&worker->nulls_node);
233ed29b0b4SJens Axboe 	list_del_rcu(&worker->all_list);
234eb47943fSGabriel Krisman Bertazi 	raw_spin_unlock(&wq->lock);
235eb47943fSGabriel Krisman Bertazi 	io_wq_dec_running(worker);
236adeaa3f2SJens Axboe 	/*
237adeaa3f2SJens Axboe 	 * this worker is a goner, clear ->worker_private to avoid any
238adeaa3f2SJens Axboe 	 * inc/dec running calls that could happen as part of exit from
239adeaa3f2SJens Axboe 	 * touching 'worker'.
240adeaa3f2SJens Axboe 	 */
241adeaa3f2SJens Axboe 	current->worker_private = NULL;
242ed29b0b4SJens Axboe 
243ed29b0b4SJens Axboe 	kfree_rcu(worker, rcu);
244eb47943fSGabriel Krisman Bertazi 	io_worker_ref_put(wq);
245ed29b0b4SJens Axboe 	do_exit(0);
246ed29b0b4SJens Axboe }
247ed29b0b4SJens Axboe 
__io_acct_run_queue(struct io_wq_acct * acct)248de36a15fSJens Axboe static inline bool __io_acct_run_queue(struct io_wq_acct *acct)
249ed29b0b4SJens Axboe {
250de36a15fSJens Axboe 	return !test_bit(IO_ACCT_STALLED_BIT, &acct->flags) &&
251de36a15fSJens Axboe 		!wq_list_empty(&acct->work_list);
252de36a15fSJens Axboe }
253ed29b0b4SJens Axboe 
254de36a15fSJens Axboe /*
255de36a15fSJens Axboe  * If there's work to do, returns true with acct->lock acquired. If not,
256de36a15fSJens Axboe  * returns false with no lock held.
257de36a15fSJens Axboe  */
io_acct_run_queue(struct io_wq_acct * acct)258de36a15fSJens Axboe static inline bool io_acct_run_queue(struct io_wq_acct *acct)
259de36a15fSJens Axboe 	__acquires(&acct->lock)
260de36a15fSJens Axboe {
261ed29b0b4SJens Axboe 	raw_spin_lock(&acct->lock);
262de36a15fSJens Axboe 	if (__io_acct_run_queue(acct))
263de36a15fSJens Axboe 		return true;
264ed29b0b4SJens Axboe 
265de36a15fSJens Axboe 	raw_spin_unlock(&acct->lock);
266de36a15fSJens Axboe 	return false;
267ed29b0b4SJens Axboe }
268ed29b0b4SJens Axboe 
269ed29b0b4SJens Axboe /*
270ed29b0b4SJens Axboe  * Check head of free list for an available worker. If one isn't available,
271ed29b0b4SJens Axboe  * caller must create one.
272ed29b0b4SJens Axboe  */
io_wq_activate_free_worker(struct io_wq * wq,struct io_wq_acct * acct)273eb47943fSGabriel Krisman Bertazi static bool io_wq_activate_free_worker(struct io_wq *wq,
274dfd63bafSGabriel Krisman Bertazi 					struct io_wq_acct *acct)
275ed29b0b4SJens Axboe 	__must_hold(RCU)
276ed29b0b4SJens Axboe {
277ed29b0b4SJens Axboe 	struct hlist_nulls_node *n;
278ed29b0b4SJens Axboe 	struct io_worker *worker;
279ed29b0b4SJens Axboe 
280ed29b0b4SJens Axboe 	/*
281ed29b0b4SJens Axboe 	 * Iterate free_list and see if we can find an idle worker to
282ed29b0b4SJens Axboe 	 * activate. If a given worker is on the free_list but in the process
283ed29b0b4SJens Axboe 	 * of exiting, keep trying.
284ed29b0b4SJens Axboe 	 */
285eb47943fSGabriel Krisman Bertazi 	hlist_nulls_for_each_entry_rcu(worker, n, &wq->free_list, nulls_node) {
286ed29b0b4SJens Axboe 		if (!io_worker_get(worker))
287ed29b0b4SJens Axboe 			continue;
288dfd63bafSGabriel Krisman Bertazi 		if (io_wq_get_acct(worker) != acct) {
289ed29b0b4SJens Axboe 			io_worker_release(worker);
290ed29b0b4SJens Axboe 			continue;
291ed29b0b4SJens Axboe 		}
29222f7fb80SJens Axboe 		/*
29322f7fb80SJens Axboe 		 * If the worker is already running, it's either already
29422f7fb80SJens Axboe 		 * starting work or finishing work. In either case, if it does
29522f7fb80SJens Axboe 		 * to go sleep, we'll kick off a new task for this work anyway.
29622f7fb80SJens Axboe 		 */
29722f7fb80SJens Axboe 		wake_up_process(worker->task);
298ed29b0b4SJens Axboe 		io_worker_release(worker);
299ed29b0b4SJens Axboe 		return true;
300ed29b0b4SJens Axboe 	}
301ed29b0b4SJens Axboe 
302ed29b0b4SJens Axboe 	return false;
303ed29b0b4SJens Axboe }
304ed29b0b4SJens Axboe 
305ed29b0b4SJens Axboe /*
306ed29b0b4SJens Axboe  * We need a worker. If we find a free one, we're good. If not, and we're
307ed29b0b4SJens Axboe  * below the max number of workers, create one.
308ed29b0b4SJens Axboe  */
io_wq_create_worker(struct io_wq * wq,struct io_wq_acct * acct)309eb47943fSGabriel Krisman Bertazi static bool io_wq_create_worker(struct io_wq *wq, struct io_wq_acct *acct)
310ed29b0b4SJens Axboe {
311ed29b0b4SJens Axboe 	/*
312ed29b0b4SJens Axboe 	 * Most likely an attempt to queue unbounded work on an io_wq that
313ed29b0b4SJens Axboe 	 * wasn't setup with any unbounded workers.
314ed29b0b4SJens Axboe 	 */
315ed29b0b4SJens Axboe 	if (unlikely(!acct->max_workers))
316ed29b0b4SJens Axboe 		pr_warn_once("io-wq is not configured for unbound workers");
317ed29b0b4SJens Axboe 
318eb47943fSGabriel Krisman Bertazi 	raw_spin_lock(&wq->lock);
319ed29b0b4SJens Axboe 	if (acct->nr_workers >= acct->max_workers) {
320eb47943fSGabriel Krisman Bertazi 		raw_spin_unlock(&wq->lock);
321ed29b0b4SJens Axboe 		return true;
322ed29b0b4SJens Axboe 	}
323ed29b0b4SJens Axboe 	acct->nr_workers++;
324eb47943fSGabriel Krisman Bertazi 	raw_spin_unlock(&wq->lock);
325ed29b0b4SJens Axboe 	atomic_inc(&acct->nr_running);
326eb47943fSGabriel Krisman Bertazi 	atomic_inc(&wq->worker_refs);
327eb47943fSGabriel Krisman Bertazi 	return create_io_worker(wq, acct->index);
328ed29b0b4SJens Axboe }
329ed29b0b4SJens Axboe 
io_wq_inc_running(struct io_worker * worker)330eb47943fSGabriel Krisman Bertazi static void io_wq_inc_running(struct io_worker *worker)
331ed29b0b4SJens Axboe {
332dfd63bafSGabriel Krisman Bertazi 	struct io_wq_acct *acct = io_wq_get_acct(worker);
333ed29b0b4SJens Axboe 
334ed29b0b4SJens Axboe 	atomic_inc(&acct->nr_running);
335ed29b0b4SJens Axboe }
336ed29b0b4SJens Axboe 
create_worker_cb(struct callback_head * cb)337ed29b0b4SJens Axboe static void create_worker_cb(struct callback_head *cb)
338ed29b0b4SJens Axboe {
339ed29b0b4SJens Axboe 	struct io_worker *worker;
340ed29b0b4SJens Axboe 	struct io_wq *wq;
341eb47943fSGabriel Krisman Bertazi 
342dfd63bafSGabriel Krisman Bertazi 	struct io_wq_acct *acct;
343ed29b0b4SJens Axboe 	bool do_create = false;
344ed29b0b4SJens Axboe 
345ed29b0b4SJens Axboe 	worker = container_of(cb, struct io_worker, create_work);
346eb47943fSGabriel Krisman Bertazi 	wq = worker->wq;
347dfd63bafSGabriel Krisman Bertazi 	acct = &wq->acct[worker->create_index];
348eb47943fSGabriel Krisman Bertazi 	raw_spin_lock(&wq->lock);
349eb47943fSGabriel Krisman Bertazi 
350ed29b0b4SJens Axboe 	if (acct->nr_workers < acct->max_workers) {
351ed29b0b4SJens Axboe 		acct->nr_workers++;
352ed29b0b4SJens Axboe 		do_create = true;
353ed29b0b4SJens Axboe 	}
354eb47943fSGabriel Krisman Bertazi 	raw_spin_unlock(&wq->lock);
355ed29b0b4SJens Axboe 	if (do_create) {
356eb47943fSGabriel Krisman Bertazi 		create_io_worker(wq, worker->create_index);
357ed29b0b4SJens Axboe 	} else {
358ed29b0b4SJens Axboe 		atomic_dec(&acct->nr_running);
359ed29b0b4SJens Axboe 		io_worker_ref_put(wq);
360ed29b0b4SJens Axboe 	}
361ed29b0b4SJens Axboe 	clear_bit_unlock(0, &worker->create_state);
362ed29b0b4SJens Axboe 	io_worker_release(worker);
363ed29b0b4SJens Axboe }
364ed29b0b4SJens Axboe 
io_queue_worker_create(struct io_worker * worker,struct io_wq_acct * acct,task_work_func_t func)365ed29b0b4SJens Axboe static bool io_queue_worker_create(struct io_worker *worker,
366dfd63bafSGabriel Krisman Bertazi 				   struct io_wq_acct *acct,
367ed29b0b4SJens Axboe 				   task_work_func_t func)
368ed29b0b4SJens Axboe {
369eb47943fSGabriel Krisman Bertazi 	struct io_wq *wq = worker->wq;
370ed29b0b4SJens Axboe 
371ed29b0b4SJens Axboe 	/* raced with exit, just ignore create call */
372ed29b0b4SJens Axboe 	if (test_bit(IO_WQ_BIT_EXIT, &wq->state))
373ed29b0b4SJens Axboe 		goto fail;
374ed29b0b4SJens Axboe 	if (!io_worker_get(worker))
375ed29b0b4SJens Axboe 		goto fail;
376ed29b0b4SJens Axboe 	/*
377ed29b0b4SJens Axboe 	 * create_state manages ownership of create_work/index. We should
378ed29b0b4SJens Axboe 	 * only need one entry per worker, as the worker going to sleep
379ed29b0b4SJens Axboe 	 * will trigger the condition, and waking will clear it once it
380ed29b0b4SJens Axboe 	 * runs the task_work.
381ed29b0b4SJens Axboe 	 */
382ed29b0b4SJens Axboe 	if (test_bit(0, &worker->create_state) ||
383ed29b0b4SJens Axboe 	    test_and_set_bit_lock(0, &worker->create_state))
384ed29b0b4SJens Axboe 		goto fail_release;
385ed29b0b4SJens Axboe 
386ed29b0b4SJens Axboe 	atomic_inc(&wq->worker_refs);
387ed29b0b4SJens Axboe 	init_task_work(&worker->create_work, func);
388ed29b0b4SJens Axboe 	worker->create_index = acct->index;
389ed29b0b4SJens Axboe 	if (!task_work_add(wq->task, &worker->create_work, TWA_SIGNAL)) {
390ed29b0b4SJens Axboe 		/*
391ed29b0b4SJens Axboe 		 * EXIT may have been set after checking it above, check after
392ed29b0b4SJens Axboe 		 * adding the task_work and remove any creation item if it is
393ed29b0b4SJens Axboe 		 * now set. wq exit does that too, but we can have added this
394ed29b0b4SJens Axboe 		 * work item after we canceled in io_wq_exit_workers().
395ed29b0b4SJens Axboe 		 */
396ed29b0b4SJens Axboe 		if (test_bit(IO_WQ_BIT_EXIT, &wq->state))
397ed29b0b4SJens Axboe 			io_wq_cancel_tw_create(wq);
398ed29b0b4SJens Axboe 		io_worker_ref_put(wq);
399ed29b0b4SJens Axboe 		return true;
400ed29b0b4SJens Axboe 	}
401ed29b0b4SJens Axboe 	io_worker_ref_put(wq);
402ed29b0b4SJens Axboe 	clear_bit_unlock(0, &worker->create_state);
403ed29b0b4SJens Axboe fail_release:
404ed29b0b4SJens Axboe 	io_worker_release(worker);
405ed29b0b4SJens Axboe fail:
406ed29b0b4SJens Axboe 	atomic_dec(&acct->nr_running);
407ed29b0b4SJens Axboe 	io_worker_ref_put(wq);
408ed29b0b4SJens Axboe 	return false;
409ed29b0b4SJens Axboe }
410ed29b0b4SJens Axboe 
io_wq_dec_running(struct io_worker * worker)411eb47943fSGabriel Krisman Bertazi static void io_wq_dec_running(struct io_worker *worker)
412ed29b0b4SJens Axboe {
413dfd63bafSGabriel Krisman Bertazi 	struct io_wq_acct *acct = io_wq_get_acct(worker);
414eb47943fSGabriel Krisman Bertazi 	struct io_wq *wq = worker->wq;
415ed29b0b4SJens Axboe 
416ab702c34SBreno Leitao 	if (!test_bit(IO_WORKER_F_UP, &worker->flags))
417ed29b0b4SJens Axboe 		return;
418ed29b0b4SJens Axboe 
419ed29b0b4SJens Axboe 	if (!atomic_dec_and_test(&acct->nr_running))
420ed29b0b4SJens Axboe 		return;
421ed29b0b4SJens Axboe 	if (!io_acct_run_queue(acct))
422ed29b0b4SJens Axboe 		return;
423ed29b0b4SJens Axboe 
424de36a15fSJens Axboe 	raw_spin_unlock(&acct->lock);
425ed29b0b4SJens Axboe 	atomic_inc(&acct->nr_running);
426eb47943fSGabriel Krisman Bertazi 	atomic_inc(&wq->worker_refs);
427ed29b0b4SJens Axboe 	io_queue_worker_create(worker, acct, create_worker_cb);
428ed29b0b4SJens Axboe }
429ed29b0b4SJens Axboe 
430ed29b0b4SJens Axboe /*
431ed29b0b4SJens Axboe  * Worker will start processing some work. Move it to the busy list, if
432ed29b0b4SJens Axboe  * it's currently on the freelist
433ed29b0b4SJens Axboe  */
__io_worker_busy(struct io_wq * wq,struct io_worker * worker)434eb47943fSGabriel Krisman Bertazi static void __io_worker_busy(struct io_wq *wq, struct io_worker *worker)
435ed29b0b4SJens Axboe {
436ab702c34SBreno Leitao 	if (test_bit(IO_WORKER_F_FREE, &worker->flags)) {
437ab702c34SBreno Leitao 		clear_bit(IO_WORKER_F_FREE, &worker->flags);
438eb47943fSGabriel Krisman Bertazi 		raw_spin_lock(&wq->lock);
439ed29b0b4SJens Axboe 		hlist_nulls_del_init_rcu(&worker->nulls_node);
440eb47943fSGabriel Krisman Bertazi 		raw_spin_unlock(&wq->lock);
441ed29b0b4SJens Axboe 	}
442ed29b0b4SJens Axboe }
443ed29b0b4SJens Axboe 
444ed29b0b4SJens Axboe /*
44507d99096SJens Axboe  * No work, worker going to sleep. Move to freelist.
446ed29b0b4SJens Axboe  */
__io_worker_idle(struct io_wq * wq,struct io_worker * worker)447eb47943fSGabriel Krisman Bertazi static void __io_worker_idle(struct io_wq *wq, struct io_worker *worker)
448eb47943fSGabriel Krisman Bertazi 	__must_hold(wq->lock)
449ed29b0b4SJens Axboe {
450ab702c34SBreno Leitao 	if (!test_bit(IO_WORKER_F_FREE, &worker->flags)) {
451ab702c34SBreno Leitao 		set_bit(IO_WORKER_F_FREE, &worker->flags);
452eb47943fSGabriel Krisman Bertazi 		hlist_nulls_add_head_rcu(&worker->nulls_node, &wq->free_list);
453ed29b0b4SJens Axboe 	}
454ed29b0b4SJens Axboe }
455ed29b0b4SJens Axboe 
io_get_work_hash(struct io_wq_work * work)456ed29b0b4SJens Axboe static inline unsigned int io_get_work_hash(struct io_wq_work *work)
457ed29b0b4SJens Axboe {
458ed29b0b4SJens Axboe 	return work->flags >> IO_WQ_HASH_SHIFT;
459ed29b0b4SJens Axboe }
460ed29b0b4SJens Axboe 
io_wait_on_hash(struct io_wq * wq,unsigned int hash)461eb47943fSGabriel Krisman Bertazi static bool io_wait_on_hash(struct io_wq *wq, unsigned int hash)
462ed29b0b4SJens Axboe {
463ed29b0b4SJens Axboe 	bool ret = false;
464ed29b0b4SJens Axboe 
465ed29b0b4SJens Axboe 	spin_lock_irq(&wq->hash->wait.lock);
466eb47943fSGabriel Krisman Bertazi 	if (list_empty(&wq->wait.entry)) {
467eb47943fSGabriel Krisman Bertazi 		__add_wait_queue(&wq->hash->wait, &wq->wait);
468ed29b0b4SJens Axboe 		if (!test_bit(hash, &wq->hash->map)) {
469ed29b0b4SJens Axboe 			__set_current_state(TASK_RUNNING);
470eb47943fSGabriel Krisman Bertazi 			list_del_init(&wq->wait.entry);
471ed29b0b4SJens Axboe 			ret = true;
472ed29b0b4SJens Axboe 		}
473ed29b0b4SJens Axboe 	}
474ed29b0b4SJens Axboe 	spin_unlock_irq(&wq->hash->wait.lock);
475ed29b0b4SJens Axboe 	return ret;
476ed29b0b4SJens Axboe }
477ed29b0b4SJens Axboe 
io_get_next_work(struct io_wq_acct * acct,struct io_worker * worker)478dfd63bafSGabriel Krisman Bertazi static struct io_wq_work *io_get_next_work(struct io_wq_acct *acct,
479ed29b0b4SJens Axboe 					   struct io_worker *worker)
480ed29b0b4SJens Axboe 	__must_hold(acct->lock)
481ed29b0b4SJens Axboe {
482ed29b0b4SJens Axboe 	struct io_wq_work_node *node, *prev;
483ed29b0b4SJens Axboe 	struct io_wq_work *work, *tail;
484ed29b0b4SJens Axboe 	unsigned int stall_hash = -1U;
485eb47943fSGabriel Krisman Bertazi 	struct io_wq *wq = worker->wq;
486ed29b0b4SJens Axboe 
487ed29b0b4SJens Axboe 	wq_list_for_each(node, prev, &acct->work_list) {
488ed29b0b4SJens Axboe 		unsigned int hash;
489ed29b0b4SJens Axboe 
490ed29b0b4SJens Axboe 		work = container_of(node, struct io_wq_work, list);
491ed29b0b4SJens Axboe 
492ed29b0b4SJens Axboe 		/* not hashed, can run anytime */
493ed29b0b4SJens Axboe 		if (!io_wq_is_hashed(work)) {
494ed29b0b4SJens Axboe 			wq_list_del(&acct->work_list, node, prev);
495ed29b0b4SJens Axboe 			return work;
496ed29b0b4SJens Axboe 		}
497ed29b0b4SJens Axboe 
498ed29b0b4SJens Axboe 		hash = io_get_work_hash(work);
499ed29b0b4SJens Axboe 		/* all items with this hash lie in [work, tail] */
500eb47943fSGabriel Krisman Bertazi 		tail = wq->hash_tail[hash];
501ed29b0b4SJens Axboe 
502ed29b0b4SJens Axboe 		/* hashed, can run if not already running */
503eb47943fSGabriel Krisman Bertazi 		if (!test_and_set_bit(hash, &wq->hash->map)) {
504eb47943fSGabriel Krisman Bertazi 			wq->hash_tail[hash] = NULL;
505ed29b0b4SJens Axboe 			wq_list_cut(&acct->work_list, &tail->list, prev);
506ed29b0b4SJens Axboe 			return work;
507ed29b0b4SJens Axboe 		}
508ed29b0b4SJens Axboe 		if (stall_hash == -1U)
509ed29b0b4SJens Axboe 			stall_hash = hash;
510ed29b0b4SJens Axboe 		/* fast forward to a next hash, for-each will fix up @prev */
511ed29b0b4SJens Axboe 		node = &tail->list;
512ed29b0b4SJens Axboe 	}
513ed29b0b4SJens Axboe 
514ed29b0b4SJens Axboe 	if (stall_hash != -1U) {
515ed29b0b4SJens Axboe 		bool unstalled;
516ed29b0b4SJens Axboe 
517ed29b0b4SJens Axboe 		/*
518ed29b0b4SJens Axboe 		 * Set this before dropping the lock to avoid racing with new
519ed29b0b4SJens Axboe 		 * work being added and clearing the stalled bit.
520ed29b0b4SJens Axboe 		 */
521ed29b0b4SJens Axboe 		set_bit(IO_ACCT_STALLED_BIT, &acct->flags);
522ed29b0b4SJens Axboe 		raw_spin_unlock(&acct->lock);
523eb47943fSGabriel Krisman Bertazi 		unstalled = io_wait_on_hash(wq, stall_hash);
524ed29b0b4SJens Axboe 		raw_spin_lock(&acct->lock);
525ed29b0b4SJens Axboe 		if (unstalled) {
526ed29b0b4SJens Axboe 			clear_bit(IO_ACCT_STALLED_BIT, &acct->flags);
527eb47943fSGabriel Krisman Bertazi 			if (wq_has_sleeper(&wq->hash->wait))
528eb47943fSGabriel Krisman Bertazi 				wake_up(&wq->hash->wait);
529ed29b0b4SJens Axboe 		}
530ed29b0b4SJens Axboe 	}
531ed29b0b4SJens Axboe 
532ed29b0b4SJens Axboe 	return NULL;
533ed29b0b4SJens Axboe }
534ed29b0b4SJens Axboe 
io_assign_current_work(struct io_worker * worker,struct io_wq_work * work)535ed29b0b4SJens Axboe static void io_assign_current_work(struct io_worker *worker,
536ed29b0b4SJens Axboe 				   struct io_wq_work *work)
537ed29b0b4SJens Axboe {
538ed29b0b4SJens Axboe 	if (work) {
539024f15e0SPavel Begunkov 		io_run_task_work();
540ed29b0b4SJens Axboe 		cond_resched();
541ed29b0b4SJens Axboe 	}
542ed29b0b4SJens Axboe 
543ed29b0b4SJens Axboe 	raw_spin_lock(&worker->lock);
544ed29b0b4SJens Axboe 	worker->cur_work = work;
545ed29b0b4SJens Axboe 	worker->next_work = NULL;
546ed29b0b4SJens Axboe 	raw_spin_unlock(&worker->lock);
547ed29b0b4SJens Axboe }
548ed29b0b4SJens Axboe 
549de36a15fSJens Axboe /*
550de36a15fSJens Axboe  * Called with acct->lock held, drops it before returning
551de36a15fSJens Axboe  */
io_worker_handle_work(struct io_wq_acct * acct,struct io_worker * worker)552de36a15fSJens Axboe static void io_worker_handle_work(struct io_wq_acct *acct,
553de36a15fSJens Axboe 				  struct io_worker *worker)
554de36a15fSJens Axboe 	__releases(&acct->lock)
555ed29b0b4SJens Axboe {
556eb47943fSGabriel Krisman Bertazi 	struct io_wq *wq = worker->wq;
557ed29b0b4SJens Axboe 	bool do_kill = test_bit(IO_WQ_BIT_EXIT, &wq->state);
558ed29b0b4SJens Axboe 
559ed29b0b4SJens Axboe 	do {
560ed29b0b4SJens Axboe 		struct io_wq_work *work;
561ed29b0b4SJens Axboe 
562ed29b0b4SJens Axboe 		/*
563ed29b0b4SJens Axboe 		 * If we got some work, mark us as busy. If we didn't, but
564ed29b0b4SJens Axboe 		 * the list isn't empty, it means we stalled on hashed work.
565ed29b0b4SJens Axboe 		 * Mark us stalled so we don't keep looking for work when we
566ed29b0b4SJens Axboe 		 * can't make progress, any work completion or insertion will
567ed29b0b4SJens Axboe 		 * clear the stalled flag.
568ed29b0b4SJens Axboe 		 */
569ed29b0b4SJens Axboe 		work = io_get_next_work(acct, worker);
570ed29b0b4SJens Axboe 		if (work) {
571ed29b0b4SJens Axboe 			/*
572ed29b0b4SJens Axboe 			 * Make sure cancelation can find this, even before
573ed29b0b4SJens Axboe 			 * it becomes the active work. That avoids a window
574ed29b0b4SJens Axboe 			 * where the work has been removed from our general
575ed29b0b4SJens Axboe 			 * work list, but isn't yet discoverable as the
576ed29b0b4SJens Axboe 			 * current work item for this worker.
577ed29b0b4SJens Axboe 			 */
578ed29b0b4SJens Axboe 			raw_spin_lock(&worker->lock);
579ed29b0b4SJens Axboe 			worker->next_work = work;
580ed29b0b4SJens Axboe 			raw_spin_unlock(&worker->lock);
581ed29b0b4SJens Axboe 		}
582f32f810dSGabriel Krisman Bertazi 
583f32f810dSGabriel Krisman Bertazi 		raw_spin_unlock(&acct->lock);
584f32f810dSGabriel Krisman Bertazi 
585f32f810dSGabriel Krisman Bertazi 		if (!work)
586f32f810dSGabriel Krisman Bertazi 			break;
587f32f810dSGabriel Krisman Bertazi 
588f32f810dSGabriel Krisman Bertazi 		__io_worker_busy(wq, worker);
589f32f810dSGabriel Krisman Bertazi 
590ed29b0b4SJens Axboe 		io_assign_current_work(worker, work);
591ed29b0b4SJens Axboe 		__set_current_state(TASK_RUNNING);
592ed29b0b4SJens Axboe 
593ed29b0b4SJens Axboe 		/* handle a whole dependent link */
594ed29b0b4SJens Axboe 		do {
595ed29b0b4SJens Axboe 			struct io_wq_work *next_hashed, *linked;
596ed29b0b4SJens Axboe 			unsigned int hash = io_get_work_hash(work);
597ed29b0b4SJens Axboe 
598ed29b0b4SJens Axboe 			next_hashed = wq_next_work(work);
599ed29b0b4SJens Axboe 
600ed29b0b4SJens Axboe 			if (unlikely(do_kill) && (work->flags & IO_WQ_WORK_UNBOUND))
601ed29b0b4SJens Axboe 				work->flags |= IO_WQ_WORK_CANCEL;
602ed29b0b4SJens Axboe 			wq->do_work(work);
603ed29b0b4SJens Axboe 			io_assign_current_work(worker, NULL);
604ed29b0b4SJens Axboe 
605ed29b0b4SJens Axboe 			linked = wq->free_work(work);
606ed29b0b4SJens Axboe 			work = next_hashed;
607ed29b0b4SJens Axboe 			if (!work && linked && !io_wq_is_hashed(linked)) {
608ed29b0b4SJens Axboe 				work = linked;
609ed29b0b4SJens Axboe 				linked = NULL;
610ed29b0b4SJens Axboe 			}
611ed29b0b4SJens Axboe 			io_assign_current_work(worker, work);
612ed29b0b4SJens Axboe 			if (linked)
613eb47943fSGabriel Krisman Bertazi 				io_wq_enqueue(wq, linked);
614ed29b0b4SJens Axboe 
615ed29b0b4SJens Axboe 			if (hash != -1U && !next_hashed) {
616ed29b0b4SJens Axboe 				/* serialize hash clear with wake_up() */
617ed29b0b4SJens Axboe 				spin_lock_irq(&wq->hash->wait.lock);
618ed29b0b4SJens Axboe 				clear_bit(hash, &wq->hash->map);
619ed29b0b4SJens Axboe 				clear_bit(IO_ACCT_STALLED_BIT, &acct->flags);
620ed29b0b4SJens Axboe 				spin_unlock_irq(&wq->hash->wait.lock);
621ed29b0b4SJens Axboe 				if (wq_has_sleeper(&wq->hash->wait))
622ed29b0b4SJens Axboe 					wake_up(&wq->hash->wait);
623ed29b0b4SJens Axboe 			}
624ed29b0b4SJens Axboe 		} while (work);
625de36a15fSJens Axboe 
626de36a15fSJens Axboe 		if (!__io_acct_run_queue(acct))
627de36a15fSJens Axboe 			break;
628de36a15fSJens Axboe 		raw_spin_lock(&acct->lock);
629ed29b0b4SJens Axboe 	} while (1);
630ed29b0b4SJens Axboe }
631ed29b0b4SJens Axboe 
io_wq_worker(void * data)632eb47943fSGabriel Krisman Bertazi static int io_wq_worker(void *data)
633ed29b0b4SJens Axboe {
634ed29b0b4SJens Axboe 	struct io_worker *worker = data;
635dfd63bafSGabriel Krisman Bertazi 	struct io_wq_acct *acct = io_wq_get_acct(worker);
636eb47943fSGabriel Krisman Bertazi 	struct io_wq *wq = worker->wq;
63701e68ce0SJens Axboe 	bool exit_mask = false, last_timeout = false;
638ed29b0b4SJens Axboe 	char buf[TASK_COMM_LEN];
639ed29b0b4SJens Axboe 
640ab702c34SBreno Leitao 	set_mask_bits(&worker->flags, 0,
641ab702c34SBreno Leitao 		      BIT(IO_WORKER_F_UP) | BIT(IO_WORKER_F_RUNNING));
642ed29b0b4SJens Axboe 
643ed29b0b4SJens Axboe 	snprintf(buf, sizeof(buf), "iou-wrk-%d", wq->task->pid);
644ed29b0b4SJens Axboe 	set_task_comm(current, buf);
645ed29b0b4SJens Axboe 
646ed29b0b4SJens Axboe 	while (!test_bit(IO_WQ_BIT_EXIT, &wq->state)) {
647ed29b0b4SJens Axboe 		long ret;
648ed29b0b4SJens Axboe 
649ed29b0b4SJens Axboe 		set_current_state(TASK_INTERRUPTIBLE);
650de36a15fSJens Axboe 
651de36a15fSJens Axboe 		/*
652de36a15fSJens Axboe 		 * If we have work to do, io_acct_run_queue() returns with
653de36a15fSJens Axboe 		 * the acct->lock held. If not, it will drop it.
654de36a15fSJens Axboe 		 */
655ed29b0b4SJens Axboe 		while (io_acct_run_queue(acct))
656de36a15fSJens Axboe 			io_worker_handle_work(acct, worker);
657ed29b0b4SJens Axboe 
658eb47943fSGabriel Krisman Bertazi 		raw_spin_lock(&wq->lock);
65901e68ce0SJens Axboe 		/*
66001e68ce0SJens Axboe 		 * Last sleep timed out. Exit if we're not the last worker,
66101e68ce0SJens Axboe 		 * or if someone modified our affinity.
66201e68ce0SJens Axboe 		 */
66301e68ce0SJens Axboe 		if (last_timeout && (exit_mask || acct->nr_workers > 1)) {
664ed29b0b4SJens Axboe 			acct->nr_workers--;
665eb47943fSGabriel Krisman Bertazi 			raw_spin_unlock(&wq->lock);
666ed29b0b4SJens Axboe 			__set_current_state(TASK_RUNNING);
667ed29b0b4SJens Axboe 			break;
668ed29b0b4SJens Axboe 		}
669ed29b0b4SJens Axboe 		last_timeout = false;
670eb47943fSGabriel Krisman Bertazi 		__io_worker_idle(wq, worker);
671eb47943fSGabriel Krisman Bertazi 		raw_spin_unlock(&wq->lock);
672024f15e0SPavel Begunkov 		if (io_run_task_work())
673ed29b0b4SJens Axboe 			continue;
674ed29b0b4SJens Axboe 		ret = schedule_timeout(WORKER_IDLE_TIMEOUT);
675ed29b0b4SJens Axboe 		if (signal_pending(current)) {
676ed29b0b4SJens Axboe 			struct ksignal ksig;
677ed29b0b4SJens Axboe 
678ed29b0b4SJens Axboe 			if (!get_signal(&ksig))
679ed29b0b4SJens Axboe 				continue;
680ed29b0b4SJens Axboe 			break;
681ed29b0b4SJens Axboe 		}
68201e68ce0SJens Axboe 		if (!ret) {
68301e68ce0SJens Axboe 			last_timeout = true;
68401e68ce0SJens Axboe 			exit_mask = !cpumask_test_cpu(raw_smp_processor_id(),
685eb47943fSGabriel Krisman Bertazi 							wq->cpu_mask);
68601e68ce0SJens Axboe 		}
687ed29b0b4SJens Axboe 	}
688ed29b0b4SJens Axboe 
689de36a15fSJens Axboe 	if (test_bit(IO_WQ_BIT_EXIT, &wq->state) && io_acct_run_queue(acct))
690de36a15fSJens Axboe 		io_worker_handle_work(acct, worker);
691ed29b0b4SJens Axboe 
692ed29b0b4SJens Axboe 	io_worker_exit(worker);
693ed29b0b4SJens Axboe 	return 0;
694ed29b0b4SJens Axboe }
695ed29b0b4SJens Axboe 
696ed29b0b4SJens Axboe /*
697ed29b0b4SJens Axboe  * Called when a worker is scheduled in. Mark us as currently running.
698ed29b0b4SJens Axboe  */
io_wq_worker_running(struct task_struct * tsk)699ed29b0b4SJens Axboe void io_wq_worker_running(struct task_struct *tsk)
700ed29b0b4SJens Axboe {
701ed29b0b4SJens Axboe 	struct io_worker *worker = tsk->worker_private;
702ed29b0b4SJens Axboe 
703ed29b0b4SJens Axboe 	if (!worker)
704ed29b0b4SJens Axboe 		return;
705ab702c34SBreno Leitao 	if (!test_bit(IO_WORKER_F_UP, &worker->flags))
706ed29b0b4SJens Axboe 		return;
707ab702c34SBreno Leitao 	if (test_bit(IO_WORKER_F_RUNNING, &worker->flags))
708ed29b0b4SJens Axboe 		return;
709ab702c34SBreno Leitao 	set_bit(IO_WORKER_F_RUNNING, &worker->flags);
710eb47943fSGabriel Krisman Bertazi 	io_wq_inc_running(worker);
711ed29b0b4SJens Axboe }
712ed29b0b4SJens Axboe 
713ed29b0b4SJens Axboe /*
714ed29b0b4SJens Axboe  * Called when worker is going to sleep. If there are no workers currently
715ed29b0b4SJens Axboe  * running and we have work pending, wake up a free one or create a new one.
716ed29b0b4SJens Axboe  */
io_wq_worker_sleeping(struct task_struct * tsk)717ed29b0b4SJens Axboe void io_wq_worker_sleeping(struct task_struct *tsk)
718ed29b0b4SJens Axboe {
719ed29b0b4SJens Axboe 	struct io_worker *worker = tsk->worker_private;
720ed29b0b4SJens Axboe 
721ed29b0b4SJens Axboe 	if (!worker)
722ed29b0b4SJens Axboe 		return;
723ab702c34SBreno Leitao 	if (!test_bit(IO_WORKER_F_UP, &worker->flags))
724ed29b0b4SJens Axboe 		return;
725ab702c34SBreno Leitao 	if (!test_bit(IO_WORKER_F_RUNNING, &worker->flags))
726ed29b0b4SJens Axboe 		return;
727ed29b0b4SJens Axboe 
728ab702c34SBreno Leitao 	clear_bit(IO_WORKER_F_RUNNING, &worker->flags);
729eb47943fSGabriel Krisman Bertazi 	io_wq_dec_running(worker);
730ed29b0b4SJens Axboe }
731ed29b0b4SJens Axboe 
io_init_new_worker(struct io_wq * wq,struct io_worker * worker,struct task_struct * tsk)732eb47943fSGabriel Krisman Bertazi static void io_init_new_worker(struct io_wq *wq, struct io_worker *worker,
733ed29b0b4SJens Axboe 			       struct task_struct *tsk)
734ed29b0b4SJens Axboe {
735ed29b0b4SJens Axboe 	tsk->worker_private = worker;
736ed29b0b4SJens Axboe 	worker->task = tsk;
737eb47943fSGabriel Krisman Bertazi 	set_cpus_allowed_ptr(tsk, wq->cpu_mask);
738ed29b0b4SJens Axboe 
739eb47943fSGabriel Krisman Bertazi 	raw_spin_lock(&wq->lock);
740eb47943fSGabriel Krisman Bertazi 	hlist_nulls_add_head_rcu(&worker->nulls_node, &wq->free_list);
741eb47943fSGabriel Krisman Bertazi 	list_add_tail_rcu(&worker->all_list, &wq->all_list);
742ab702c34SBreno Leitao 	set_bit(IO_WORKER_F_FREE, &worker->flags);
743eb47943fSGabriel Krisman Bertazi 	raw_spin_unlock(&wq->lock);
744ed29b0b4SJens Axboe 	wake_up_new_task(tsk);
745ed29b0b4SJens Axboe }
746ed29b0b4SJens Axboe 
io_wq_work_match_all(struct io_wq_work * work,void * data)747ed29b0b4SJens Axboe static bool io_wq_work_match_all(struct io_wq_work *work, void *data)
748ed29b0b4SJens Axboe {
749ed29b0b4SJens Axboe 	return true;
750ed29b0b4SJens Axboe }
751ed29b0b4SJens Axboe 
io_should_retry_thread(struct io_worker * worker,long err)7528d01e63fSPavel Begunkov static inline bool io_should_retry_thread(struct io_worker *worker, long err)
753ed29b0b4SJens Axboe {
754ed29b0b4SJens Axboe 	/*
755ed29b0b4SJens Axboe 	 * Prevent perpetual task_work retry, if the task (or its group) is
756ed29b0b4SJens Axboe 	 * exiting.
757ed29b0b4SJens Axboe 	 */
758ed29b0b4SJens Axboe 	if (fatal_signal_pending(current))
759ed29b0b4SJens Axboe 		return false;
7608d01e63fSPavel Begunkov 	if (worker->init_retries++ >= WORKER_INIT_LIMIT)
7618d01e63fSPavel Begunkov 		return false;
762ed29b0b4SJens Axboe 
763ed29b0b4SJens Axboe 	switch (err) {
764ed29b0b4SJens Axboe 	case -EAGAIN:
765ed29b0b4SJens Axboe 	case -ERESTARTSYS:
766ed29b0b4SJens Axboe 	case -ERESTARTNOINTR:
767ed29b0b4SJens Axboe 	case -ERESTARTNOHAND:
768ed29b0b4SJens Axboe 		return true;
769ed29b0b4SJens Axboe 	default:
770ed29b0b4SJens Axboe 		return false;
771ed29b0b4SJens Axboe 	}
772ed29b0b4SJens Axboe }
773ed29b0b4SJens Axboe 
create_worker_cont(struct callback_head * cb)774ed29b0b4SJens Axboe static void create_worker_cont(struct callback_head *cb)
775ed29b0b4SJens Axboe {
776ed29b0b4SJens Axboe 	struct io_worker *worker;
777ed29b0b4SJens Axboe 	struct task_struct *tsk;
778eb47943fSGabriel Krisman Bertazi 	struct io_wq *wq;
779ed29b0b4SJens Axboe 
780ed29b0b4SJens Axboe 	worker = container_of(cb, struct io_worker, create_work);
781ed29b0b4SJens Axboe 	clear_bit_unlock(0, &worker->create_state);
782eb47943fSGabriel Krisman Bertazi 	wq = worker->wq;
783eb47943fSGabriel Krisman Bertazi 	tsk = create_io_thread(io_wq_worker, worker, NUMA_NO_NODE);
784ed29b0b4SJens Axboe 	if (!IS_ERR(tsk)) {
785eb47943fSGabriel Krisman Bertazi 		io_init_new_worker(wq, worker, tsk);
786ed29b0b4SJens Axboe 		io_worker_release(worker);
787ed29b0b4SJens Axboe 		return;
7888d01e63fSPavel Begunkov 	} else if (!io_should_retry_thread(worker, PTR_ERR(tsk))) {
789dfd63bafSGabriel Krisman Bertazi 		struct io_wq_acct *acct = io_wq_get_acct(worker);
790ed29b0b4SJens Axboe 
791ed29b0b4SJens Axboe 		atomic_dec(&acct->nr_running);
792eb47943fSGabriel Krisman Bertazi 		raw_spin_lock(&wq->lock);
793ed29b0b4SJens Axboe 		acct->nr_workers--;
794ed29b0b4SJens Axboe 		if (!acct->nr_workers) {
795ed29b0b4SJens Axboe 			struct io_cb_cancel_data match = {
796ed29b0b4SJens Axboe 				.fn		= io_wq_work_match_all,
797ed29b0b4SJens Axboe 				.cancel_all	= true,
798ed29b0b4SJens Axboe 			};
799ed29b0b4SJens Axboe 
800eb47943fSGabriel Krisman Bertazi 			raw_spin_unlock(&wq->lock);
801eb47943fSGabriel Krisman Bertazi 			while (io_acct_cancel_pending_work(wq, acct, &match))
802ed29b0b4SJens Axboe 				;
803ed29b0b4SJens Axboe 		} else {
804eb47943fSGabriel Krisman Bertazi 			raw_spin_unlock(&wq->lock);
805ed29b0b4SJens Axboe 		}
806eb47943fSGabriel Krisman Bertazi 		io_worker_ref_put(wq);
807ed29b0b4SJens Axboe 		kfree(worker);
808ed29b0b4SJens Axboe 		return;
809ed29b0b4SJens Axboe 	}
810ed29b0b4SJens Axboe 
811ed29b0b4SJens Axboe 	/* re-create attempts grab a new worker ref, drop the existing one */
812ed29b0b4SJens Axboe 	io_worker_release(worker);
813ed29b0b4SJens Axboe 	schedule_work(&worker->work);
814ed29b0b4SJens Axboe }
815ed29b0b4SJens Axboe 
io_workqueue_create(struct work_struct * work)816ed29b0b4SJens Axboe static void io_workqueue_create(struct work_struct *work)
817ed29b0b4SJens Axboe {
818ed29b0b4SJens Axboe 	struct io_worker *worker = container_of(work, struct io_worker, work);
819dfd63bafSGabriel Krisman Bertazi 	struct io_wq_acct *acct = io_wq_get_acct(worker);
820ed29b0b4SJens Axboe 
821ed29b0b4SJens Axboe 	if (!io_queue_worker_create(worker, acct, create_worker_cont))
822ed29b0b4SJens Axboe 		kfree(worker);
823ed29b0b4SJens Axboe }
824ed29b0b4SJens Axboe 
create_io_worker(struct io_wq * wq,int index)825eb47943fSGabriel Krisman Bertazi static bool create_io_worker(struct io_wq *wq, int index)
826ed29b0b4SJens Axboe {
827dfd63bafSGabriel Krisman Bertazi 	struct io_wq_acct *acct = &wq->acct[index];
828ed29b0b4SJens Axboe 	struct io_worker *worker;
829ed29b0b4SJens Axboe 	struct task_struct *tsk;
830ed29b0b4SJens Axboe 
831ed29b0b4SJens Axboe 	__set_current_state(TASK_RUNNING);
832ed29b0b4SJens Axboe 
833da64d6dbSBreno Leitao 	worker = kzalloc(sizeof(*worker), GFP_KERNEL);
834ed29b0b4SJens Axboe 	if (!worker) {
835ed29b0b4SJens Axboe fail:
836ed29b0b4SJens Axboe 		atomic_dec(&acct->nr_running);
837eb47943fSGabriel Krisman Bertazi 		raw_spin_lock(&wq->lock);
838ed29b0b4SJens Axboe 		acct->nr_workers--;
839eb47943fSGabriel Krisman Bertazi 		raw_spin_unlock(&wq->lock);
840ed29b0b4SJens Axboe 		io_worker_ref_put(wq);
841ed29b0b4SJens Axboe 		return false;
842ed29b0b4SJens Axboe 	}
843ed29b0b4SJens Axboe 
844ed29b0b4SJens Axboe 	refcount_set(&worker->ref, 1);
845eb47943fSGabriel Krisman Bertazi 	worker->wq = wq;
846ed29b0b4SJens Axboe 	raw_spin_lock_init(&worker->lock);
847ed29b0b4SJens Axboe 	init_completion(&worker->ref_done);
848ed29b0b4SJens Axboe 
849ed29b0b4SJens Axboe 	if (index == IO_WQ_ACCT_BOUND)
850ab702c34SBreno Leitao 		set_bit(IO_WORKER_F_BOUND, &worker->flags);
851ed29b0b4SJens Axboe 
852eb47943fSGabriel Krisman Bertazi 	tsk = create_io_thread(io_wq_worker, worker, NUMA_NO_NODE);
853ed29b0b4SJens Axboe 	if (!IS_ERR(tsk)) {
854eb47943fSGabriel Krisman Bertazi 		io_init_new_worker(wq, worker, tsk);
8558d01e63fSPavel Begunkov 	} else if (!io_should_retry_thread(worker, PTR_ERR(tsk))) {
856ed29b0b4SJens Axboe 		kfree(worker);
857ed29b0b4SJens Axboe 		goto fail;
858ed29b0b4SJens Axboe 	} else {
859ed29b0b4SJens Axboe 		INIT_WORK(&worker->work, io_workqueue_create);
860ed29b0b4SJens Axboe 		schedule_work(&worker->work);
861ed29b0b4SJens Axboe 	}
862ed29b0b4SJens Axboe 
863ed29b0b4SJens Axboe 	return true;
864ed29b0b4SJens Axboe }
865ed29b0b4SJens Axboe 
866ed29b0b4SJens Axboe /*
867ed29b0b4SJens Axboe  * Iterate the passed in list and call the specific function for each
868ed29b0b4SJens Axboe  * worker that isn't exiting
869ed29b0b4SJens Axboe  */
io_wq_for_each_worker(struct io_wq * wq,bool (* func)(struct io_worker *,void *),void * data)870eb47943fSGabriel Krisman Bertazi static bool io_wq_for_each_worker(struct io_wq *wq,
871ed29b0b4SJens Axboe 				  bool (*func)(struct io_worker *, void *),
872ed29b0b4SJens Axboe 				  void *data)
873ed29b0b4SJens Axboe {
874ed29b0b4SJens Axboe 	struct io_worker *worker;
875ed29b0b4SJens Axboe 	bool ret = false;
876ed29b0b4SJens Axboe 
877eb47943fSGabriel Krisman Bertazi 	list_for_each_entry_rcu(worker, &wq->all_list, all_list) {
878ed29b0b4SJens Axboe 		if (io_worker_get(worker)) {
879ed29b0b4SJens Axboe 			/* no task if node is/was offline */
880ed29b0b4SJens Axboe 			if (worker->task)
881ed29b0b4SJens Axboe 				ret = func(worker, data);
882ed29b0b4SJens Axboe 			io_worker_release(worker);
883ed29b0b4SJens Axboe 			if (ret)
884ed29b0b4SJens Axboe 				break;
885ed29b0b4SJens Axboe 		}
886ed29b0b4SJens Axboe 	}
887ed29b0b4SJens Axboe 
888ed29b0b4SJens Axboe 	return ret;
889ed29b0b4SJens Axboe }
890ed29b0b4SJens Axboe 
io_wq_worker_wake(struct io_worker * worker,void * data)891ed29b0b4SJens Axboe static bool io_wq_worker_wake(struct io_worker *worker, void *data)
892ed29b0b4SJens Axboe {
893ed29b0b4SJens Axboe 	__set_notify_signal(worker->task);
894ed29b0b4SJens Axboe 	wake_up_process(worker->task);
895ed29b0b4SJens Axboe 	return false;
896ed29b0b4SJens Axboe }
897ed29b0b4SJens Axboe 
io_run_cancel(struct io_wq_work * work,struct io_wq * wq)898eb47943fSGabriel Krisman Bertazi static void io_run_cancel(struct io_wq_work *work, struct io_wq *wq)
899ed29b0b4SJens Axboe {
900ed29b0b4SJens Axboe 	do {
901ed29b0b4SJens Axboe 		work->flags |= IO_WQ_WORK_CANCEL;
902ed29b0b4SJens Axboe 		wq->do_work(work);
903ed29b0b4SJens Axboe 		work = wq->free_work(work);
904ed29b0b4SJens Axboe 	} while (work);
905ed29b0b4SJens Axboe }
906ed29b0b4SJens Axboe 
io_wq_insert_work(struct io_wq * wq,struct io_wq_work * work)907eb47943fSGabriel Krisman Bertazi static void io_wq_insert_work(struct io_wq *wq, struct io_wq_work *work)
908ed29b0b4SJens Axboe {
909eb47943fSGabriel Krisman Bertazi 	struct io_wq_acct *acct = io_work_get_acct(wq, work);
910ed29b0b4SJens Axboe 	unsigned int hash;
911ed29b0b4SJens Axboe 	struct io_wq_work *tail;
912ed29b0b4SJens Axboe 
913ed29b0b4SJens Axboe 	if (!io_wq_is_hashed(work)) {
914ed29b0b4SJens Axboe append:
915ed29b0b4SJens Axboe 		wq_list_add_tail(&work->list, &acct->work_list);
916ed29b0b4SJens Axboe 		return;
917ed29b0b4SJens Axboe 	}
918ed29b0b4SJens Axboe 
919ed29b0b4SJens Axboe 	hash = io_get_work_hash(work);
920eb47943fSGabriel Krisman Bertazi 	tail = wq->hash_tail[hash];
921eb47943fSGabriel Krisman Bertazi 	wq->hash_tail[hash] = work;
922ed29b0b4SJens Axboe 	if (!tail)
923ed29b0b4SJens Axboe 		goto append;
924ed29b0b4SJens Axboe 
925ed29b0b4SJens Axboe 	wq_list_add_after(&work->list, &tail->list, &acct->work_list);
926ed29b0b4SJens Axboe }
927ed29b0b4SJens Axboe 
io_wq_work_match_item(struct io_wq_work * work,void * data)928ed29b0b4SJens Axboe static bool io_wq_work_match_item(struct io_wq_work *work, void *data)
929ed29b0b4SJens Axboe {
930ed29b0b4SJens Axboe 	return work == data;
931ed29b0b4SJens Axboe }
932ed29b0b4SJens Axboe 
io_wq_enqueue(struct io_wq * wq,struct io_wq_work * work)933eb47943fSGabriel Krisman Bertazi void io_wq_enqueue(struct io_wq *wq, struct io_wq_work *work)
934ed29b0b4SJens Axboe {
935eb47943fSGabriel Krisman Bertazi 	struct io_wq_acct *acct = io_work_get_acct(wq, work);
936ab702c34SBreno Leitao 	unsigned long work_flags = work->flags;
9371bbadf95SSu Hui 	struct io_cb_cancel_data match = {
9381bbadf95SSu Hui 		.fn		= io_wq_work_match_item,
9391bbadf95SSu Hui 		.data		= work,
9401bbadf95SSu Hui 		.cancel_all	= false,
9411bbadf95SSu Hui 	};
942ed29b0b4SJens Axboe 	bool do_create;
943ed29b0b4SJens Axboe 
944ed29b0b4SJens Axboe 	/*
945ed29b0b4SJens Axboe 	 * If io-wq is exiting for this task, or if the request has explicitly
946ed29b0b4SJens Axboe 	 * been marked as one that should not get executed, cancel it here.
947ed29b0b4SJens Axboe 	 */
948eb47943fSGabriel Krisman Bertazi 	if (test_bit(IO_WQ_BIT_EXIT, &wq->state) ||
949ed29b0b4SJens Axboe 	    (work->flags & IO_WQ_WORK_CANCEL)) {
950eb47943fSGabriel Krisman Bertazi 		io_run_cancel(work, wq);
951ed29b0b4SJens Axboe 		return;
952ed29b0b4SJens Axboe 	}
953ed29b0b4SJens Axboe 
954ed29b0b4SJens Axboe 	raw_spin_lock(&acct->lock);
955eb47943fSGabriel Krisman Bertazi 	io_wq_insert_work(wq, work);
956ed29b0b4SJens Axboe 	clear_bit(IO_ACCT_STALLED_BIT, &acct->flags);
957ed29b0b4SJens Axboe 	raw_spin_unlock(&acct->lock);
958ed29b0b4SJens Axboe 
959ed29b0b4SJens Axboe 	rcu_read_lock();
960eb47943fSGabriel Krisman Bertazi 	do_create = !io_wq_activate_free_worker(wq, acct);
961ed29b0b4SJens Axboe 	rcu_read_unlock();
962ed29b0b4SJens Axboe 
963ed29b0b4SJens Axboe 	if (do_create && ((work_flags & IO_WQ_WORK_CONCURRENT) ||
964ed29b0b4SJens Axboe 	    !atomic_read(&acct->nr_running))) {
965ed29b0b4SJens Axboe 		bool did_create;
966ed29b0b4SJens Axboe 
967eb47943fSGabriel Krisman Bertazi 		did_create = io_wq_create_worker(wq, acct);
968ed29b0b4SJens Axboe 		if (likely(did_create))
969ed29b0b4SJens Axboe 			return;
970ed29b0b4SJens Axboe 
971eb47943fSGabriel Krisman Bertazi 		raw_spin_lock(&wq->lock);
972ed29b0b4SJens Axboe 		if (acct->nr_workers) {
973eb47943fSGabriel Krisman Bertazi 			raw_spin_unlock(&wq->lock);
974ed29b0b4SJens Axboe 			return;
975ed29b0b4SJens Axboe 		}
976eb47943fSGabriel Krisman Bertazi 		raw_spin_unlock(&wq->lock);
977ed29b0b4SJens Axboe 
978ed29b0b4SJens Axboe 		/* fatal condition, failed to create the first worker */
979eb47943fSGabriel Krisman Bertazi 		io_acct_cancel_pending_work(wq, acct, &match);
980ed29b0b4SJens Axboe 	}
981ed29b0b4SJens Axboe }
982ed29b0b4SJens Axboe 
983ed29b0b4SJens Axboe /*
984ed29b0b4SJens Axboe  * Work items that hash to the same value will not be done in parallel.
985ed29b0b4SJens Axboe  * Used to limit concurrent writes, generally hashed by inode.
986ed29b0b4SJens Axboe  */
io_wq_hash_work(struct io_wq_work * work,void * val)987ed29b0b4SJens Axboe void io_wq_hash_work(struct io_wq_work *work, void *val)
988ed29b0b4SJens Axboe {
989ed29b0b4SJens Axboe 	unsigned int bit;
990ed29b0b4SJens Axboe 
991ed29b0b4SJens Axboe 	bit = hash_ptr(val, IO_WQ_HASH_ORDER);
992ed29b0b4SJens Axboe 	work->flags |= (IO_WQ_WORK_HASHED | (bit << IO_WQ_HASH_SHIFT));
993ed29b0b4SJens Axboe }
994ed29b0b4SJens Axboe 
__io_wq_worker_cancel(struct io_worker * worker,struct io_cb_cancel_data * match,struct io_wq_work * work)995ed29b0b4SJens Axboe static bool __io_wq_worker_cancel(struct io_worker *worker,
996ed29b0b4SJens Axboe 				  struct io_cb_cancel_data *match,
997ed29b0b4SJens Axboe 				  struct io_wq_work *work)
998ed29b0b4SJens Axboe {
999ed29b0b4SJens Axboe 	if (work && match->fn(work, match->data)) {
1000ed29b0b4SJens Axboe 		work->flags |= IO_WQ_WORK_CANCEL;
1001ed29b0b4SJens Axboe 		__set_notify_signal(worker->task);
1002ed29b0b4SJens Axboe 		return true;
1003ed29b0b4SJens Axboe 	}
1004ed29b0b4SJens Axboe 
1005ed29b0b4SJens Axboe 	return false;
1006ed29b0b4SJens Axboe }
1007ed29b0b4SJens Axboe 
io_wq_worker_cancel(struct io_worker * worker,void * data)1008ed29b0b4SJens Axboe static bool io_wq_worker_cancel(struct io_worker *worker, void *data)
1009ed29b0b4SJens Axboe {
1010ed29b0b4SJens Axboe 	struct io_cb_cancel_data *match = data;
1011ed29b0b4SJens Axboe 
1012ed29b0b4SJens Axboe 	/*
1013ed29b0b4SJens Axboe 	 * Hold the lock to avoid ->cur_work going out of scope, caller
1014ed29b0b4SJens Axboe 	 * may dereference the passed in work.
1015ed29b0b4SJens Axboe 	 */
1016ed29b0b4SJens Axboe 	raw_spin_lock(&worker->lock);
1017ed29b0b4SJens Axboe 	if (__io_wq_worker_cancel(worker, match, worker->cur_work) ||
1018ed29b0b4SJens Axboe 	    __io_wq_worker_cancel(worker, match, worker->next_work))
1019ed29b0b4SJens Axboe 		match->nr_running++;
1020ed29b0b4SJens Axboe 	raw_spin_unlock(&worker->lock);
1021ed29b0b4SJens Axboe 
1022ed29b0b4SJens Axboe 	return match->nr_running && !match->cancel_all;
1023ed29b0b4SJens Axboe }
1024ed29b0b4SJens Axboe 
io_wq_remove_pending(struct io_wq * wq,struct io_wq_work * work,struct io_wq_work_node * prev)1025eb47943fSGabriel Krisman Bertazi static inline void io_wq_remove_pending(struct io_wq *wq,
1026ed29b0b4SJens Axboe 					 struct io_wq_work *work,
1027ed29b0b4SJens Axboe 					 struct io_wq_work_node *prev)
1028ed29b0b4SJens Axboe {
1029eb47943fSGabriel Krisman Bertazi 	struct io_wq_acct *acct = io_work_get_acct(wq, work);
1030ed29b0b4SJens Axboe 	unsigned int hash = io_get_work_hash(work);
1031ed29b0b4SJens Axboe 	struct io_wq_work *prev_work = NULL;
1032ed29b0b4SJens Axboe 
1033eb47943fSGabriel Krisman Bertazi 	if (io_wq_is_hashed(work) && work == wq->hash_tail[hash]) {
1034ed29b0b4SJens Axboe 		if (prev)
1035ed29b0b4SJens Axboe 			prev_work = container_of(prev, struct io_wq_work, list);
1036ed29b0b4SJens Axboe 		if (prev_work && io_get_work_hash(prev_work) == hash)
1037eb47943fSGabriel Krisman Bertazi 			wq->hash_tail[hash] = prev_work;
1038ed29b0b4SJens Axboe 		else
1039eb47943fSGabriel Krisman Bertazi 			wq->hash_tail[hash] = NULL;
1040ed29b0b4SJens Axboe 	}
1041ed29b0b4SJens Axboe 	wq_list_del(&acct->work_list, &work->list, prev);
1042ed29b0b4SJens Axboe }
1043ed29b0b4SJens Axboe 
io_acct_cancel_pending_work(struct io_wq * wq,struct io_wq_acct * acct,struct io_cb_cancel_data * match)1044eb47943fSGabriel Krisman Bertazi static bool io_acct_cancel_pending_work(struct io_wq *wq,
1045dfd63bafSGabriel Krisman Bertazi 					struct io_wq_acct *acct,
1046ed29b0b4SJens Axboe 					struct io_cb_cancel_data *match)
1047ed29b0b4SJens Axboe {
1048ed29b0b4SJens Axboe 	struct io_wq_work_node *node, *prev;
1049ed29b0b4SJens Axboe 	struct io_wq_work *work;
1050ed29b0b4SJens Axboe 
1051ed29b0b4SJens Axboe 	raw_spin_lock(&acct->lock);
1052ed29b0b4SJens Axboe 	wq_list_for_each(node, prev, &acct->work_list) {
1053ed29b0b4SJens Axboe 		work = container_of(node, struct io_wq_work, list);
1054ed29b0b4SJens Axboe 		if (!match->fn(work, match->data))
1055ed29b0b4SJens Axboe 			continue;
1056eb47943fSGabriel Krisman Bertazi 		io_wq_remove_pending(wq, work, prev);
1057ed29b0b4SJens Axboe 		raw_spin_unlock(&acct->lock);
1058eb47943fSGabriel Krisman Bertazi 		io_run_cancel(work, wq);
1059ed29b0b4SJens Axboe 		match->nr_pending++;
1060ed29b0b4SJens Axboe 		/* not safe to continue after unlock */
1061ed29b0b4SJens Axboe 		return true;
1062ed29b0b4SJens Axboe 	}
1063ed29b0b4SJens Axboe 	raw_spin_unlock(&acct->lock);
1064ed29b0b4SJens Axboe 
1065ed29b0b4SJens Axboe 	return false;
1066ed29b0b4SJens Axboe }
1067ed29b0b4SJens Axboe 
io_wq_cancel_pending_work(struct io_wq * wq,struct io_cb_cancel_data * match)1068eb47943fSGabriel Krisman Bertazi static void io_wq_cancel_pending_work(struct io_wq *wq,
1069ed29b0b4SJens Axboe 				      struct io_cb_cancel_data *match)
1070ed29b0b4SJens Axboe {
1071ed29b0b4SJens Axboe 	int i;
1072ed29b0b4SJens Axboe retry:
1073ed29b0b4SJens Axboe 	for (i = 0; i < IO_WQ_ACCT_NR; i++) {
1074eb47943fSGabriel Krisman Bertazi 		struct io_wq_acct *acct = io_get_acct(wq, i == 0);
1075ed29b0b4SJens Axboe 
1076eb47943fSGabriel Krisman Bertazi 		if (io_acct_cancel_pending_work(wq, acct, match)) {
1077ed29b0b4SJens Axboe 			if (match->cancel_all)
1078ed29b0b4SJens Axboe 				goto retry;
1079ed29b0b4SJens Axboe 			break;
1080ed29b0b4SJens Axboe 		}
1081ed29b0b4SJens Axboe 	}
1082ed29b0b4SJens Axboe }
1083ed29b0b4SJens Axboe 
io_wq_cancel_running_work(struct io_wq * wq,struct io_cb_cancel_data * match)1084eb47943fSGabriel Krisman Bertazi static void io_wq_cancel_running_work(struct io_wq *wq,
1085ed29b0b4SJens Axboe 				       struct io_cb_cancel_data *match)
1086ed29b0b4SJens Axboe {
1087ed29b0b4SJens Axboe 	rcu_read_lock();
1088eb47943fSGabriel Krisman Bertazi 	io_wq_for_each_worker(wq, io_wq_worker_cancel, match);
1089ed29b0b4SJens Axboe 	rcu_read_unlock();
1090ed29b0b4SJens Axboe }
1091ed29b0b4SJens Axboe 
io_wq_cancel_cb(struct io_wq * wq,work_cancel_fn * cancel,void * data,bool cancel_all)1092ed29b0b4SJens Axboe enum io_wq_cancel io_wq_cancel_cb(struct io_wq *wq, work_cancel_fn *cancel,
1093ed29b0b4SJens Axboe 				  void *data, bool cancel_all)
1094ed29b0b4SJens Axboe {
1095ed29b0b4SJens Axboe 	struct io_cb_cancel_data match = {
1096ed29b0b4SJens Axboe 		.fn		= cancel,
1097ed29b0b4SJens Axboe 		.data		= data,
1098ed29b0b4SJens Axboe 		.cancel_all	= cancel_all,
1099ed29b0b4SJens Axboe 	};
1100ed29b0b4SJens Axboe 
1101ed29b0b4SJens Axboe 	/*
1102ed29b0b4SJens Axboe 	 * First check pending list, if we're lucky we can just remove it
1103ed29b0b4SJens Axboe 	 * from there. CANCEL_OK means that the work is returned as-new,
1104ed29b0b4SJens Axboe 	 * no completion will be posted for it.
1105ed29b0b4SJens Axboe 	 *
1106ed29b0b4SJens Axboe 	 * Then check if a free (going busy) or busy worker has the work
1107ed29b0b4SJens Axboe 	 * currently running. If we find it there, we'll return CANCEL_RUNNING
1108ed29b0b4SJens Axboe 	 * as an indication that we attempt to signal cancellation. The
1109ed29b0b4SJens Axboe 	 * completion will run normally in this case.
1110ed29b0b4SJens Axboe 	 *
1111eb47943fSGabriel Krisman Bertazi 	 * Do both of these while holding the wq->lock, to ensure that
1112ed29b0b4SJens Axboe 	 * we'll find a work item regardless of state.
1113ed29b0b4SJens Axboe 	 */
1114eb47943fSGabriel Krisman Bertazi 	io_wq_cancel_pending_work(wq, &match);
1115ed29b0b4SJens Axboe 	if (match.nr_pending && !match.cancel_all)
1116ed29b0b4SJens Axboe 		return IO_WQ_CANCEL_OK;
1117ed29b0b4SJens Axboe 
1118eb47943fSGabriel Krisman Bertazi 	raw_spin_lock(&wq->lock);
1119eb47943fSGabriel Krisman Bertazi 	io_wq_cancel_running_work(wq, &match);
1120eb47943fSGabriel Krisman Bertazi 	raw_spin_unlock(&wq->lock);
1121ed29b0b4SJens Axboe 	if (match.nr_running && !match.cancel_all)
1122ed29b0b4SJens Axboe 		return IO_WQ_CANCEL_RUNNING;
1123ed29b0b4SJens Axboe 
1124ed29b0b4SJens Axboe 	if (match.nr_running)
1125ed29b0b4SJens Axboe 		return IO_WQ_CANCEL_RUNNING;
1126ed29b0b4SJens Axboe 	if (match.nr_pending)
1127ed29b0b4SJens Axboe 		return IO_WQ_CANCEL_OK;
1128ed29b0b4SJens Axboe 	return IO_WQ_CANCEL_NOTFOUND;
1129ed29b0b4SJens Axboe }
1130ed29b0b4SJens Axboe 
io_wq_hash_wake(struct wait_queue_entry * wait,unsigned mode,int sync,void * key)1131eb47943fSGabriel Krisman Bertazi static int io_wq_hash_wake(struct wait_queue_entry *wait, unsigned mode,
1132ed29b0b4SJens Axboe 			    int sync, void *key)
1133ed29b0b4SJens Axboe {
1134eb47943fSGabriel Krisman Bertazi 	struct io_wq *wq = container_of(wait, struct io_wq, wait);
1135ed29b0b4SJens Axboe 	int i;
1136ed29b0b4SJens Axboe 
1137ed29b0b4SJens Axboe 	list_del_init(&wait->entry);
1138ed29b0b4SJens Axboe 
1139ed29b0b4SJens Axboe 	rcu_read_lock();
1140ed29b0b4SJens Axboe 	for (i = 0; i < IO_WQ_ACCT_NR; i++) {
1141dfd63bafSGabriel Krisman Bertazi 		struct io_wq_acct *acct = &wq->acct[i];
1142ed29b0b4SJens Axboe 
1143ed29b0b4SJens Axboe 		if (test_and_clear_bit(IO_ACCT_STALLED_BIT, &acct->flags))
1144eb47943fSGabriel Krisman Bertazi 			io_wq_activate_free_worker(wq, acct);
1145ed29b0b4SJens Axboe 	}
1146ed29b0b4SJens Axboe 	rcu_read_unlock();
1147ed29b0b4SJens Axboe 	return 1;
1148ed29b0b4SJens Axboe }
1149ed29b0b4SJens Axboe 
io_wq_create(unsigned bounded,struct io_wq_data * data)1150ed29b0b4SJens Axboe struct io_wq *io_wq_create(unsigned bounded, struct io_wq_data *data)
1151ed29b0b4SJens Axboe {
1152da64d6dbSBreno Leitao 	int ret, i;
1153ed29b0b4SJens Axboe 	struct io_wq *wq;
1154ed29b0b4SJens Axboe 
1155ed29b0b4SJens Axboe 	if (WARN_ON_ONCE(!data->free_work || !data->do_work))
1156ed29b0b4SJens Axboe 		return ERR_PTR(-EINVAL);
1157ed29b0b4SJens Axboe 	if (WARN_ON_ONCE(!bounded))
1158ed29b0b4SJens Axboe 		return ERR_PTR(-EINVAL);
1159ed29b0b4SJens Axboe 
1160da64d6dbSBreno Leitao 	wq = kzalloc(sizeof(struct io_wq), GFP_KERNEL);
1161ed29b0b4SJens Axboe 	if (!wq)
1162ed29b0b4SJens Axboe 		return ERR_PTR(-ENOMEM);
1163ed29b0b4SJens Axboe 
1164ed29b0b4SJens Axboe 	refcount_inc(&data->hash->refs);
1165ed29b0b4SJens Axboe 	wq->hash = data->hash;
1166ed29b0b4SJens Axboe 	wq->free_work = data->free_work;
1167ed29b0b4SJens Axboe 	wq->do_work = data->do_work;
1168ed29b0b4SJens Axboe 
1169ed29b0b4SJens Axboe 	ret = -ENOMEM;
1170ed29b0b4SJens Axboe 
1171eb47943fSGabriel Krisman Bertazi 	if (!alloc_cpumask_var(&wq->cpu_mask, GFP_KERNEL))
1172ed29b0b4SJens Axboe 		goto err;
1173*5740c0faSFelix Moessbauer 	cpuset_cpus_allowed(data->task, wq->cpu_mask);
1174dfd63bafSGabriel Krisman Bertazi 	wq->acct[IO_WQ_ACCT_BOUND].max_workers = bounded;
1175dfd63bafSGabriel Krisman Bertazi 	wq->acct[IO_WQ_ACCT_UNBOUND].max_workers =
1176ed29b0b4SJens Axboe 				task_rlimit(current, RLIMIT_NPROC);
1177eb47943fSGabriel Krisman Bertazi 	INIT_LIST_HEAD(&wq->wait.entry);
1178eb47943fSGabriel Krisman Bertazi 	wq->wait.func = io_wq_hash_wake;
1179ed29b0b4SJens Axboe 	for (i = 0; i < IO_WQ_ACCT_NR; i++) {
1180dfd63bafSGabriel Krisman Bertazi 		struct io_wq_acct *acct = &wq->acct[i];
1181ed29b0b4SJens Axboe 
1182ed29b0b4SJens Axboe 		acct->index = i;
1183ed29b0b4SJens Axboe 		atomic_set(&acct->nr_running, 0);
1184ed29b0b4SJens Axboe 		INIT_WQ_LIST(&acct->work_list);
1185ed29b0b4SJens Axboe 		raw_spin_lock_init(&acct->lock);
1186ed29b0b4SJens Axboe 	}
1187eb47943fSGabriel Krisman Bertazi 
1188eb47943fSGabriel Krisman Bertazi 	raw_spin_lock_init(&wq->lock);
1189eb47943fSGabriel Krisman Bertazi 	INIT_HLIST_NULLS_HEAD(&wq->free_list, 0);
1190eb47943fSGabriel Krisman Bertazi 	INIT_LIST_HEAD(&wq->all_list);
1191ed29b0b4SJens Axboe 
1192ed29b0b4SJens Axboe 	wq->task = get_task_struct(data->task);
1193ed29b0b4SJens Axboe 	atomic_set(&wq->worker_refs, 1);
1194ed29b0b4SJens Axboe 	init_completion(&wq->worker_done);
11950f8baa3cSJeff Moyer 	ret = cpuhp_state_add_instance_nocalls(io_wq_online, &wq->cpuhp_node);
11960f8baa3cSJeff Moyer 	if (ret)
11970f8baa3cSJeff Moyer 		goto err;
11980f8baa3cSJeff Moyer 
1199ed29b0b4SJens Axboe 	return wq;
1200ed29b0b4SJens Axboe err:
1201ed29b0b4SJens Axboe 	io_wq_put_hash(data->hash);
1202eb47943fSGabriel Krisman Bertazi 	free_cpumask_var(wq->cpu_mask);
1203ed29b0b4SJens Axboe 	kfree(wq);
1204ed29b0b4SJens Axboe 	return ERR_PTR(ret);
1205ed29b0b4SJens Axboe }
1206ed29b0b4SJens Axboe 
io_task_work_match(struct callback_head * cb,void * data)1207ed29b0b4SJens Axboe static bool io_task_work_match(struct callback_head *cb, void *data)
1208ed29b0b4SJens Axboe {
1209ed29b0b4SJens Axboe 	struct io_worker *worker;
1210ed29b0b4SJens Axboe 
1211ed29b0b4SJens Axboe 	if (cb->func != create_worker_cb && cb->func != create_worker_cont)
1212ed29b0b4SJens Axboe 		return false;
1213ed29b0b4SJens Axboe 	worker = container_of(cb, struct io_worker, create_work);
1214eb47943fSGabriel Krisman Bertazi 	return worker->wq == data;
1215ed29b0b4SJens Axboe }
1216ed29b0b4SJens Axboe 
io_wq_exit_start(struct io_wq * wq)1217ed29b0b4SJens Axboe void io_wq_exit_start(struct io_wq *wq)
1218ed29b0b4SJens Axboe {
1219ed29b0b4SJens Axboe 	set_bit(IO_WQ_BIT_EXIT, &wq->state);
1220ed29b0b4SJens Axboe }
1221ed29b0b4SJens Axboe 
io_wq_cancel_tw_create(struct io_wq * wq)1222ed29b0b4SJens Axboe static void io_wq_cancel_tw_create(struct io_wq *wq)
1223ed29b0b4SJens Axboe {
1224ed29b0b4SJens Axboe 	struct callback_head *cb;
1225ed29b0b4SJens Axboe 
1226ed29b0b4SJens Axboe 	while ((cb = task_work_cancel_match(wq->task, io_task_work_match, wq)) != NULL) {
1227ed29b0b4SJens Axboe 		struct io_worker *worker;
1228ed29b0b4SJens Axboe 
1229ed29b0b4SJens Axboe 		worker = container_of(cb, struct io_worker, create_work);
1230ed29b0b4SJens Axboe 		io_worker_cancel_cb(worker);
1231e6db6f93SJens Axboe 		/*
1232e6db6f93SJens Axboe 		 * Only the worker continuation helper has worker allocated and
1233e6db6f93SJens Axboe 		 * hence needs freeing.
1234e6db6f93SJens Axboe 		 */
1235e6db6f93SJens Axboe 		if (cb->func == create_worker_cont)
1236af82425cSJens Axboe 			kfree(worker);
1237ed29b0b4SJens Axboe 	}
1238ed29b0b4SJens Axboe }
1239ed29b0b4SJens Axboe 
io_wq_exit_workers(struct io_wq * wq)1240ed29b0b4SJens Axboe static void io_wq_exit_workers(struct io_wq *wq)
1241ed29b0b4SJens Axboe {
1242ed29b0b4SJens Axboe 	if (!wq->task)
1243ed29b0b4SJens Axboe 		return;
1244ed29b0b4SJens Axboe 
1245ed29b0b4SJens Axboe 	io_wq_cancel_tw_create(wq);
1246ed29b0b4SJens Axboe 
1247ed29b0b4SJens Axboe 	rcu_read_lock();
1248eb47943fSGabriel Krisman Bertazi 	io_wq_for_each_worker(wq, io_wq_worker_wake, NULL);
1249ed29b0b4SJens Axboe 	rcu_read_unlock();
1250ed29b0b4SJens Axboe 	io_worker_ref_put(wq);
1251ed29b0b4SJens Axboe 	wait_for_completion(&wq->worker_done);
1252ed29b0b4SJens Axboe 
1253ed29b0b4SJens Axboe 	spin_lock_irq(&wq->hash->wait.lock);
1254eb47943fSGabriel Krisman Bertazi 	list_del_init(&wq->wait.entry);
1255ed29b0b4SJens Axboe 	spin_unlock_irq(&wq->hash->wait.lock);
1256da64d6dbSBreno Leitao 
1257ed29b0b4SJens Axboe 	put_task_struct(wq->task);
1258ed29b0b4SJens Axboe 	wq->task = NULL;
1259ed29b0b4SJens Axboe }
1260ed29b0b4SJens Axboe 
io_wq_destroy(struct io_wq * wq)1261ed29b0b4SJens Axboe static void io_wq_destroy(struct io_wq *wq)
1262ed29b0b4SJens Axboe {
1263ed29b0b4SJens Axboe 	struct io_cb_cancel_data match = {
1264ed29b0b4SJens Axboe 		.fn		= io_wq_work_match_all,
1265ed29b0b4SJens Axboe 		.cancel_all	= true,
1266ed29b0b4SJens Axboe 	};
1267da64d6dbSBreno Leitao 
1268da64d6dbSBreno Leitao 	cpuhp_state_remove_instance_nocalls(io_wq_online, &wq->cpuhp_node);
1269eb47943fSGabriel Krisman Bertazi 	io_wq_cancel_pending_work(wq, &match);
1270eb47943fSGabriel Krisman Bertazi 	free_cpumask_var(wq->cpu_mask);
1271ed29b0b4SJens Axboe 	io_wq_put_hash(wq->hash);
1272ed29b0b4SJens Axboe 	kfree(wq);
1273ed29b0b4SJens Axboe }
1274ed29b0b4SJens Axboe 
io_wq_put_and_exit(struct io_wq * wq)1275ed29b0b4SJens Axboe void io_wq_put_and_exit(struct io_wq *wq)
1276ed29b0b4SJens Axboe {
1277ed29b0b4SJens Axboe 	WARN_ON_ONCE(!test_bit(IO_WQ_BIT_EXIT, &wq->state));
1278ed29b0b4SJens Axboe 
1279ed29b0b4SJens Axboe 	io_wq_exit_workers(wq);
1280ed29b0b4SJens Axboe 	io_wq_destroy(wq);
1281ed29b0b4SJens Axboe }
1282ed29b0b4SJens Axboe 
1283ed29b0b4SJens Axboe struct online_data {
1284ed29b0b4SJens Axboe 	unsigned int cpu;
1285ed29b0b4SJens Axboe 	bool online;
1286ed29b0b4SJens Axboe };
1287ed29b0b4SJens Axboe 
io_wq_worker_affinity(struct io_worker * worker,void * data)1288ed29b0b4SJens Axboe static bool io_wq_worker_affinity(struct io_worker *worker, void *data)
1289ed29b0b4SJens Axboe {
1290ed29b0b4SJens Axboe 	struct online_data *od = data;
1291ed29b0b4SJens Axboe 
1292ed29b0b4SJens Axboe 	if (od->online)
1293eb47943fSGabriel Krisman Bertazi 		cpumask_set_cpu(od->cpu, worker->wq->cpu_mask);
1294ed29b0b4SJens Axboe 	else
1295eb47943fSGabriel Krisman Bertazi 		cpumask_clear_cpu(od->cpu, worker->wq->cpu_mask);
1296ed29b0b4SJens Axboe 	return false;
1297ed29b0b4SJens Axboe }
1298ed29b0b4SJens Axboe 
__io_wq_cpu_online(struct io_wq * wq,unsigned int cpu,bool online)1299ed29b0b4SJens Axboe static int __io_wq_cpu_online(struct io_wq *wq, unsigned int cpu, bool online)
1300ed29b0b4SJens Axboe {
1301ed29b0b4SJens Axboe 	struct online_data od = {
1302ed29b0b4SJens Axboe 		.cpu = cpu,
1303ed29b0b4SJens Axboe 		.online = online
1304ed29b0b4SJens Axboe 	};
1305ed29b0b4SJens Axboe 
1306ed29b0b4SJens Axboe 	rcu_read_lock();
1307eb47943fSGabriel Krisman Bertazi 	io_wq_for_each_worker(wq, io_wq_worker_affinity, &od);
1308ed29b0b4SJens Axboe 	rcu_read_unlock();
1309ed29b0b4SJens Axboe 	return 0;
1310ed29b0b4SJens Axboe }
1311ed29b0b4SJens Axboe 
io_wq_cpu_online(unsigned int cpu,struct hlist_node * node)1312ed29b0b4SJens Axboe static int io_wq_cpu_online(unsigned int cpu, struct hlist_node *node)
1313ed29b0b4SJens Axboe {
1314ed29b0b4SJens Axboe 	struct io_wq *wq = hlist_entry_safe(node, struct io_wq, cpuhp_node);
1315ed29b0b4SJens Axboe 
1316ed29b0b4SJens Axboe 	return __io_wq_cpu_online(wq, cpu, true);
1317ed29b0b4SJens Axboe }
1318ed29b0b4SJens Axboe 
io_wq_cpu_offline(unsigned int cpu,struct hlist_node * node)1319ed29b0b4SJens Axboe static int io_wq_cpu_offline(unsigned int cpu, struct hlist_node *node)
1320ed29b0b4SJens Axboe {
1321ed29b0b4SJens Axboe 	struct io_wq *wq = hlist_entry_safe(node, struct io_wq, cpuhp_node);
1322ed29b0b4SJens Axboe 
1323ed29b0b4SJens Axboe 	return __io_wq_cpu_online(wq, cpu, false);
1324ed29b0b4SJens Axboe }
1325ed29b0b4SJens Axboe 
io_wq_cpu_affinity(struct io_uring_task * tctx,cpumask_var_t mask)1326ebdfefc0SJens Axboe int io_wq_cpu_affinity(struct io_uring_task *tctx, cpumask_var_t mask)
1327ed29b0b4SJens Axboe {
13287b3a3558SFelix Moessbauer 	cpumask_var_t allowed_mask;
13297b3a3558SFelix Moessbauer 	int ret = 0;
13307b3a3558SFelix Moessbauer 
1331ebdfefc0SJens Axboe 	if (!tctx || !tctx->io_wq)
1332ebdfefc0SJens Axboe 		return -EINVAL;
1333ebdfefc0SJens Axboe 
13347b3a3558SFelix Moessbauer 	if (!alloc_cpumask_var(&allowed_mask, GFP_KERNEL))
13357b3a3558SFelix Moessbauer 		return -ENOMEM;
13367b3a3558SFelix Moessbauer 
1337ed29b0b4SJens Axboe 	rcu_read_lock();
13387b3a3558SFelix Moessbauer 	cpuset_cpus_allowed(tctx->io_wq->task, allowed_mask);
13397b3a3558SFelix Moessbauer 	if (mask) {
13407b3a3558SFelix Moessbauer 		if (cpumask_subset(mask, allowed_mask))
1341ebdfefc0SJens Axboe 			cpumask_copy(tctx->io_wq->cpu_mask, mask);
1342ed29b0b4SJens Axboe 		else
13437b3a3558SFelix Moessbauer 			ret = -EINVAL;
13447b3a3558SFelix Moessbauer 	} else {
13457b3a3558SFelix Moessbauer 		cpumask_copy(tctx->io_wq->cpu_mask, allowed_mask);
13467b3a3558SFelix Moessbauer 	}
1347ed29b0b4SJens Axboe 	rcu_read_unlock();
1348da64d6dbSBreno Leitao 
13497b3a3558SFelix Moessbauer 	free_cpumask_var(allowed_mask);
13507b3a3558SFelix Moessbauer 	return ret;
1351ed29b0b4SJens Axboe }
1352ed29b0b4SJens Axboe 
1353ed29b0b4SJens Axboe /*
1354ed29b0b4SJens Axboe  * Set max number of unbounded workers, returns old value. If new_count is 0,
1355ed29b0b4SJens Axboe  * then just return the old value.
1356ed29b0b4SJens Axboe  */
io_wq_max_workers(struct io_wq * wq,int * new_count)1357ed29b0b4SJens Axboe int io_wq_max_workers(struct io_wq *wq, int *new_count)
1358ed29b0b4SJens Axboe {
1359dfd63bafSGabriel Krisman Bertazi 	struct io_wq_acct *acct;
1360ed29b0b4SJens Axboe 	int prev[IO_WQ_ACCT_NR];
1361da64d6dbSBreno Leitao 	int i;
1362ed29b0b4SJens Axboe 
1363ed29b0b4SJens Axboe 	BUILD_BUG_ON((int) IO_WQ_ACCT_BOUND   != (int) IO_WQ_BOUND);
1364ed29b0b4SJens Axboe 	BUILD_BUG_ON((int) IO_WQ_ACCT_UNBOUND != (int) IO_WQ_UNBOUND);
1365ed29b0b4SJens Axboe 	BUILD_BUG_ON((int) IO_WQ_ACCT_NR      != 2);
1366ed29b0b4SJens Axboe 
1367ed29b0b4SJens Axboe 	for (i = 0; i < IO_WQ_ACCT_NR; i++) {
1368ed29b0b4SJens Axboe 		if (new_count[i] > task_rlimit(current, RLIMIT_NPROC))
1369ed29b0b4SJens Axboe 			new_count[i] = task_rlimit(current, RLIMIT_NPROC);
1370ed29b0b4SJens Axboe 	}
1371ed29b0b4SJens Axboe 
1372ed29b0b4SJens Axboe 	for (i = 0; i < IO_WQ_ACCT_NR; i++)
1373ed29b0b4SJens Axboe 		prev[i] = 0;
1374ed29b0b4SJens Axboe 
1375ed29b0b4SJens Axboe 	rcu_read_lock();
1376ed29b0b4SJens Axboe 
1377eb47943fSGabriel Krisman Bertazi 	raw_spin_lock(&wq->lock);
1378ed29b0b4SJens Axboe 	for (i = 0; i < IO_WQ_ACCT_NR; i++) {
1379dfd63bafSGabriel Krisman Bertazi 		acct = &wq->acct[i];
1380ed29b0b4SJens Axboe 		prev[i] = max_t(int, acct->max_workers, prev[i]);
1381ed29b0b4SJens Axboe 		if (new_count[i])
1382ed29b0b4SJens Axboe 			acct->max_workers = new_count[i];
1383ed29b0b4SJens Axboe 	}
1384eb47943fSGabriel Krisman Bertazi 	raw_spin_unlock(&wq->lock);
1385ed29b0b4SJens Axboe 	rcu_read_unlock();
1386ed29b0b4SJens Axboe 
1387ed29b0b4SJens Axboe 	for (i = 0; i < IO_WQ_ACCT_NR; i++)
1388ed29b0b4SJens Axboe 		new_count[i] = prev[i];
1389ed29b0b4SJens Axboe 
1390ed29b0b4SJens Axboe 	return 0;
1391ed29b0b4SJens Axboe }
1392ed29b0b4SJens Axboe 
io_wq_init(void)1393ed29b0b4SJens Axboe static __init int io_wq_init(void)
1394ed29b0b4SJens Axboe {
1395ed29b0b4SJens Axboe 	int ret;
1396ed29b0b4SJens Axboe 
1397ed29b0b4SJens Axboe 	ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "io-wq/online",
1398ed29b0b4SJens Axboe 					io_wq_cpu_online, io_wq_cpu_offline);
1399ed29b0b4SJens Axboe 	if (ret < 0)
1400ed29b0b4SJens Axboe 		return ret;
1401ed29b0b4SJens Axboe 	io_wq_online = ret;
1402ed29b0b4SJens Axboe 	return 0;
1403ed29b0b4SJens Axboe }
1404ed29b0b4SJens Axboe subsys_initcall(io_wq_init);
1405