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