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>
160997aa54SFelix 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>
201f293098SJens Axboe #include <linux/sched/sysctl.h>
21ed29b0b4SJens Axboe #include <uapi/linux/io_uring.h>
22ed29b0b4SJens Axboe
23ed29b0b4SJens Axboe #include "io-wq.h"
24a6b21fbbSPavel Begunkov #include "slist.h"
25024f15e0SPavel Begunkov #include "io_uring.h"
26ed29b0b4SJens Axboe
27ed29b0b4SJens Axboe #define WORKER_IDLE_TIMEOUT (5 * HZ)
280453aad6SPavel Begunkov #define WORKER_INIT_LIMIT 3
29ed29b0b4SJens Axboe
30ed29b0b4SJens Axboe enum {
318a565304SBreno Leitao IO_WORKER_F_UP = 0, /* up and active */
328a565304SBreno Leitao IO_WORKER_F_RUNNING = 1, /* account as running */
338a565304SBreno Leitao IO_WORKER_F_FREE = 2, /* worker on free list */
34ed29b0b4SJens Axboe };
35ed29b0b4SJens Axboe
36ed29b0b4SJens Axboe enum {
37ed29b0b4SJens Axboe IO_WQ_BIT_EXIT = 0, /* wq exiting */
3838aa434aSLi Chen IO_WQ_BIT_EXIT_ON_IDLE = 1, /* allow all workers to exit on idle */
39ed29b0b4SJens Axboe };
40ed29b0b4SJens Axboe
41ed29b0b4SJens Axboe enum {
42ed29b0b4SJens Axboe IO_ACCT_STALLED_BIT = 0, /* stalled on hash */
43ed29b0b4SJens Axboe };
44ed29b0b4SJens Axboe
45ed29b0b4SJens Axboe /*
46eb47943fSGabriel Krisman Bertazi * One for each thread in a wq pool
47ed29b0b4SJens Axboe */
48ed29b0b4SJens Axboe struct io_worker {
49ed29b0b4SJens Axboe refcount_t ref;
508a565304SBreno 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;
553d3bafd3SMax Kellermann struct io_wq_acct *acct;
56ed29b0b4SJens Axboe
57ed29b0b4SJens Axboe struct io_wq_work *cur_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;
640453aad6SPavel Begunkov int init_retries;
65ed29b0b4SJens Axboe
66ed29b0b4SJens Axboe union {
67ed29b0b4SJens Axboe struct rcu_head rcu;
6813918315SUday Shankar struct delayed_work 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 {
81751eedc4SMax Kellermann /**
82751eedc4SMax Kellermann * Protects access to the worker lists.
83751eedc4SMax Kellermann */
84751eedc4SMax Kellermann raw_spinlock_t workers_lock;
85751eedc4SMax Kellermann
86ed29b0b4SJens Axboe unsigned nr_workers;
87ed29b0b4SJens Axboe unsigned max_workers;
88ed29b0b4SJens Axboe atomic_t nr_running;
89751eedc4SMax Kellermann
90751eedc4SMax Kellermann /**
91751eedc4SMax Kellermann * The list of free workers. Protected by #workers_lock
92751eedc4SMax Kellermann * (write) and RCU (read).
93751eedc4SMax Kellermann */
94751eedc4SMax Kellermann struct hlist_nulls_head free_list;
95751eedc4SMax Kellermann
96751eedc4SMax Kellermann /**
97751eedc4SMax Kellermann * The list of all workers. Protected by #workers_lock
98751eedc4SMax Kellermann * (write) and RCU (read).
99751eedc4SMax Kellermann */
100751eedc4SMax Kellermann struct list_head all_list;
101751eedc4SMax Kellermann
102ed29b0b4SJens Axboe raw_spinlock_t lock;
103ed29b0b4SJens Axboe struct io_wq_work_list work_list;
104ed29b0b4SJens Axboe unsigned long flags;
105ed29b0b4SJens Axboe };
106ed29b0b4SJens Axboe
107ed29b0b4SJens Axboe enum {
108ed29b0b4SJens Axboe IO_WQ_ACCT_BOUND,
109ed29b0b4SJens Axboe IO_WQ_ACCT_UNBOUND,
110ed29b0b4SJens Axboe IO_WQ_ACCT_NR,
111ed29b0b4SJens Axboe };
112ed29b0b4SJens Axboe
113ed29b0b4SJens Axboe /*
114ed29b0b4SJens Axboe * Per io_wq state
115ed29b0b4SJens Axboe */
116ed29b0b4SJens Axboe struct io_wq {
117ed29b0b4SJens Axboe unsigned long state;
118ed29b0b4SJens Axboe
119ed29b0b4SJens Axboe struct io_wq_hash *hash;
120ed29b0b4SJens Axboe
121ed29b0b4SJens Axboe atomic_t worker_refs;
122ed29b0b4SJens Axboe struct completion worker_done;
123ed29b0b4SJens Axboe
124ed29b0b4SJens Axboe struct hlist_node cpuhp_node;
125ed29b0b4SJens Axboe
126ed29b0b4SJens Axboe struct task_struct *task;
127ed29b0b4SJens Axboe
128dfd63bafSGabriel Krisman Bertazi struct io_wq_acct acct[IO_WQ_ACCT_NR];
129dfd63bafSGabriel Krisman Bertazi
130eb47943fSGabriel Krisman Bertazi struct wait_queue_entry wait;
131eb47943fSGabriel Krisman Bertazi
132eb47943fSGabriel Krisman Bertazi struct io_wq_work *hash_tail[IO_WQ_NR_HASH_BUCKETS];
133eb47943fSGabriel Krisman Bertazi
134eb47943fSGabriel Krisman Bertazi cpumask_var_t cpu_mask;
135ed29b0b4SJens Axboe };
136ed29b0b4SJens Axboe
137ed29b0b4SJens Axboe static enum cpuhp_state io_wq_online;
138ed29b0b4SJens Axboe
139ed29b0b4SJens Axboe struct io_cb_cancel_data {
140ed29b0b4SJens Axboe work_cancel_fn *fn;
141ed29b0b4SJens Axboe void *data;
142ed29b0b4SJens Axboe int nr_running;
143ed29b0b4SJens Axboe int nr_pending;
144ed29b0b4SJens Axboe bool cancel_all;
145ed29b0b4SJens Axboe };
146ed29b0b4SJens Axboe
1473d3bafd3SMax Kellermann static bool create_io_worker(struct io_wq *wq, struct io_wq_acct *acct);
148eb47943fSGabriel Krisman Bertazi static void io_wq_dec_running(struct io_worker *worker);
149eb47943fSGabriel Krisman Bertazi static bool io_acct_cancel_pending_work(struct io_wq *wq,
150dfd63bafSGabriel Krisman Bertazi struct io_wq_acct *acct,
151ed29b0b4SJens Axboe struct io_cb_cancel_data *match);
152ed29b0b4SJens Axboe static void create_worker_cb(struct callback_head *cb);
153ed29b0b4SJens Axboe static void io_wq_cancel_tw_create(struct io_wq *wq);
154ed29b0b4SJens Axboe
__io_get_work_hash(unsigned int work_flags)155e37dfc05SJens Axboe static inline unsigned int __io_get_work_hash(unsigned int work_flags)
156e37dfc05SJens Axboe {
157e37dfc05SJens Axboe return work_flags >> IO_WQ_HASH_SHIFT;
158e37dfc05SJens Axboe }
159e37dfc05SJens Axboe
io_get_work_hash(struct io_wq_work * work)160e37dfc05SJens Axboe static inline unsigned int io_get_work_hash(struct io_wq_work *work)
161e37dfc05SJens Axboe {
162e37dfc05SJens Axboe return __io_get_work_hash(atomic_read(&work->flags));
163e37dfc05SJens Axboe }
164e37dfc05SJens Axboe
io_worker_get(struct io_worker * worker)165ed29b0b4SJens Axboe static bool io_worker_get(struct io_worker *worker)
166ed29b0b4SJens Axboe {
167ed29b0b4SJens Axboe return refcount_inc_not_zero(&worker->ref);
168ed29b0b4SJens Axboe }
169ed29b0b4SJens Axboe
io_worker_release(struct io_worker * worker)170ed29b0b4SJens Axboe static void io_worker_release(struct io_worker *worker)
171ed29b0b4SJens Axboe {
172ed29b0b4SJens Axboe if (refcount_dec_and_test(&worker->ref))
173ed29b0b4SJens Axboe complete(&worker->ref_done);
174ed29b0b4SJens Axboe }
175ed29b0b4SJens Axboe
io_get_acct(struct io_wq * wq,bool bound)176dfd63bafSGabriel Krisman Bertazi static inline struct io_wq_acct *io_get_acct(struct io_wq *wq, bool bound)
177ed29b0b4SJens Axboe {
178dfd63bafSGabriel Krisman Bertazi return &wq->acct[bound ? IO_WQ_ACCT_BOUND : IO_WQ_ACCT_UNBOUND];
179ed29b0b4SJens Axboe }
180ed29b0b4SJens Axboe
io_work_get_acct(struct io_wq * wq,unsigned int work_flags)181dfd63bafSGabriel Krisman Bertazi static inline struct io_wq_acct *io_work_get_acct(struct io_wq *wq,
1826ee78354SMax Kellermann unsigned int work_flags)
183ed29b0b4SJens Axboe {
1846ee78354SMax Kellermann return io_get_acct(wq, !(work_flags & IO_WQ_WORK_UNBOUND));
185ed29b0b4SJens Axboe }
186ed29b0b4SJens Axboe
io_wq_get_acct(struct io_worker * worker)187dfd63bafSGabriel Krisman Bertazi static inline struct io_wq_acct *io_wq_get_acct(struct io_worker *worker)
188ed29b0b4SJens Axboe {
1893d3bafd3SMax Kellermann return worker->acct;
190ed29b0b4SJens Axboe }
191ed29b0b4SJens Axboe
io_worker_ref_put(struct io_wq * wq)192ed29b0b4SJens Axboe static void io_worker_ref_put(struct io_wq *wq)
193ed29b0b4SJens Axboe {
194ed29b0b4SJens Axboe if (atomic_dec_and_test(&wq->worker_refs))
195ed29b0b4SJens Axboe complete(&wq->worker_done);
196ed29b0b4SJens Axboe }
197ed29b0b4SJens Axboe
io_wq_worker_stopped(void)19845500dc4SPavel Begunkov bool io_wq_worker_stopped(void)
19945500dc4SPavel Begunkov {
20045500dc4SPavel Begunkov struct io_worker *worker = current->worker_private;
20145500dc4SPavel Begunkov
20245500dc4SPavel Begunkov if (WARN_ON_ONCE(!io_wq_current_is_worker()))
20345500dc4SPavel Begunkov return true;
20445500dc4SPavel Begunkov
20545500dc4SPavel Begunkov return test_bit(IO_WQ_BIT_EXIT, &worker->wq->state);
20645500dc4SPavel Begunkov }
20745500dc4SPavel Begunkov
io_worker_cancel_cb(struct io_worker * worker)208ed29b0b4SJens Axboe static void io_worker_cancel_cb(struct io_worker *worker)
209ed29b0b4SJens Axboe {
210dfd63bafSGabriel Krisman Bertazi struct io_wq_acct *acct = io_wq_get_acct(worker);
211eb47943fSGabriel Krisman Bertazi struct io_wq *wq = worker->wq;
212ed29b0b4SJens Axboe
213ed29b0b4SJens Axboe atomic_dec(&acct->nr_running);
214751eedc4SMax Kellermann raw_spin_lock(&acct->workers_lock);
215ed29b0b4SJens Axboe acct->nr_workers--;
216751eedc4SMax Kellermann raw_spin_unlock(&acct->workers_lock);
217ed29b0b4SJens Axboe io_worker_ref_put(wq);
218ed29b0b4SJens Axboe clear_bit_unlock(0, &worker->create_state);
219ed29b0b4SJens Axboe io_worker_release(worker);
220ed29b0b4SJens Axboe }
221ed29b0b4SJens Axboe
io_task_worker_match(struct callback_head * cb,void * data)222ed29b0b4SJens Axboe static bool io_task_worker_match(struct callback_head *cb, void *data)
223ed29b0b4SJens Axboe {
224ed29b0b4SJens Axboe struct io_worker *worker;
225ed29b0b4SJens Axboe
226ed29b0b4SJens Axboe if (cb->func != create_worker_cb)
227ed29b0b4SJens Axboe return false;
228ed29b0b4SJens Axboe worker = container_of(cb, struct io_worker, create_work);
229ed29b0b4SJens Axboe return worker == data;
230ed29b0b4SJens Axboe }
231ed29b0b4SJens Axboe
io_worker_exit(struct io_worker * worker)232ed29b0b4SJens Axboe static void io_worker_exit(struct io_worker *worker)
233ed29b0b4SJens Axboe {
234eb47943fSGabriel Krisman Bertazi struct io_wq *wq = worker->wq;
235751eedc4SMax Kellermann struct io_wq_acct *acct = io_wq_get_acct(worker);
236ed29b0b4SJens Axboe
237ed29b0b4SJens Axboe while (1) {
238ed29b0b4SJens Axboe struct callback_head *cb = task_work_cancel_match(wq->task,
239ed29b0b4SJens Axboe io_task_worker_match, worker);
240ed29b0b4SJens Axboe
241ed29b0b4SJens Axboe if (!cb)
242ed29b0b4SJens Axboe break;
243ed29b0b4SJens Axboe io_worker_cancel_cb(worker);
244ed29b0b4SJens Axboe }
245ed29b0b4SJens Axboe
246ed29b0b4SJens Axboe io_worker_release(worker);
247ed29b0b4SJens Axboe wait_for_completion(&worker->ref_done);
248ed29b0b4SJens Axboe
249751eedc4SMax Kellermann raw_spin_lock(&acct->workers_lock);
2508a565304SBreno Leitao if (test_bit(IO_WORKER_F_FREE, &worker->flags))
251ed29b0b4SJens Axboe hlist_nulls_del_rcu(&worker->nulls_node);
252ed29b0b4SJens Axboe list_del_rcu(&worker->all_list);
253751eedc4SMax Kellermann raw_spin_unlock(&acct->workers_lock);
254eb47943fSGabriel Krisman Bertazi io_wq_dec_running(worker);
255adeaa3f2SJens Axboe /*
256adeaa3f2SJens Axboe * this worker is a goner, clear ->worker_private to avoid any
257adeaa3f2SJens Axboe * inc/dec running calls that could happen as part of exit from
258adeaa3f2SJens Axboe * touching 'worker'.
259adeaa3f2SJens Axboe */
260adeaa3f2SJens Axboe current->worker_private = NULL;
261ed29b0b4SJens Axboe
262ed29b0b4SJens Axboe kfree_rcu(worker, rcu);
263eb47943fSGabriel Krisman Bertazi io_worker_ref_put(wq);
264ed29b0b4SJens Axboe do_exit(0);
265ed29b0b4SJens Axboe }
266ed29b0b4SJens Axboe
__io_acct_run_queue(struct io_wq_acct * acct)267de36a15fSJens Axboe static inline bool __io_acct_run_queue(struct io_wq_acct *acct)
268ed29b0b4SJens Axboe {
269de36a15fSJens Axboe return !test_bit(IO_ACCT_STALLED_BIT, &acct->flags) &&
270de36a15fSJens Axboe !wq_list_empty(&acct->work_list);
271de36a15fSJens Axboe }
272ed29b0b4SJens Axboe
273de36a15fSJens Axboe /*
274de36a15fSJens Axboe * If there's work to do, returns true with acct->lock acquired. If not,
275de36a15fSJens Axboe * returns false with no lock held.
276de36a15fSJens Axboe */
io_acct_run_queue(struct io_wq_acct * acct)277de36a15fSJens Axboe static inline bool io_acct_run_queue(struct io_wq_acct *acct)
278de36a15fSJens Axboe __acquires(&acct->lock)
279de36a15fSJens Axboe {
280ed29b0b4SJens Axboe raw_spin_lock(&acct->lock);
281de36a15fSJens Axboe if (__io_acct_run_queue(acct))
282de36a15fSJens Axboe return true;
283ed29b0b4SJens Axboe
284de36a15fSJens Axboe raw_spin_unlock(&acct->lock);
285de36a15fSJens Axboe return false;
286ed29b0b4SJens Axboe }
287ed29b0b4SJens Axboe
288ed29b0b4SJens Axboe /*
289ed29b0b4SJens Axboe * Check head of free list for an available worker. If one isn't available,
290ed29b0b4SJens Axboe * caller must create one.
291ed29b0b4SJens Axboe */
io_acct_activate_free_worker(struct io_wq_acct * acct)292751eedc4SMax Kellermann static bool io_acct_activate_free_worker(struct io_wq_acct *acct)
293ed29b0b4SJens Axboe __must_hold(RCU)
294ed29b0b4SJens Axboe {
295ed29b0b4SJens Axboe struct hlist_nulls_node *n;
296ed29b0b4SJens Axboe struct io_worker *worker;
297ed29b0b4SJens Axboe
298ed29b0b4SJens Axboe /*
299ed29b0b4SJens Axboe * Iterate free_list and see if we can find an idle worker to
300ed29b0b4SJens Axboe * activate. If a given worker is on the free_list but in the process
301ed29b0b4SJens Axboe * of exiting, keep trying.
302ed29b0b4SJens Axboe */
303751eedc4SMax Kellermann hlist_nulls_for_each_entry_rcu(worker, n, &acct->free_list, nulls_node) {
304ed29b0b4SJens Axboe if (!io_worker_get(worker))
305ed29b0b4SJens Axboe continue;
30622f7fb80SJens Axboe /*
30722f7fb80SJens Axboe * If the worker is already running, it's either already
30822f7fb80SJens Axboe * starting work or finishing work. In either case, if it does
30922f7fb80SJens Axboe * to go sleep, we'll kick off a new task for this work anyway.
31022f7fb80SJens Axboe */
31122f7fb80SJens Axboe wake_up_process(worker->task);
312ed29b0b4SJens Axboe io_worker_release(worker);
313ed29b0b4SJens Axboe return true;
314ed29b0b4SJens Axboe }
315ed29b0b4SJens Axboe
316ed29b0b4SJens Axboe return false;
317ed29b0b4SJens Axboe }
318ed29b0b4SJens Axboe
319ed29b0b4SJens Axboe /*
320ed29b0b4SJens Axboe * We need a worker. If we find a free one, we're good. If not, and we're
321ed29b0b4SJens Axboe * below the max number of workers, create one.
322ed29b0b4SJens Axboe */
io_wq_create_worker(struct io_wq * wq,struct io_wq_acct * acct)323eb47943fSGabriel Krisman Bertazi static bool io_wq_create_worker(struct io_wq *wq, struct io_wq_acct *acct)
324ed29b0b4SJens Axboe {
325ed29b0b4SJens Axboe /*
326ed29b0b4SJens Axboe * Most likely an attempt to queue unbounded work on an io_wq that
327ed29b0b4SJens Axboe * wasn't setup with any unbounded workers.
328ed29b0b4SJens Axboe */
329ed29b0b4SJens Axboe if (unlikely(!acct->max_workers))
330ed29b0b4SJens Axboe pr_warn_once("io-wq is not configured for unbound workers");
331ed29b0b4SJens Axboe
332751eedc4SMax Kellermann raw_spin_lock(&acct->workers_lock);
333ed29b0b4SJens Axboe if (acct->nr_workers >= acct->max_workers) {
334751eedc4SMax Kellermann raw_spin_unlock(&acct->workers_lock);
335ed29b0b4SJens Axboe return true;
336ed29b0b4SJens Axboe }
337ed29b0b4SJens Axboe acct->nr_workers++;
338751eedc4SMax Kellermann raw_spin_unlock(&acct->workers_lock);
339ed29b0b4SJens Axboe atomic_inc(&acct->nr_running);
340eb47943fSGabriel Krisman Bertazi atomic_inc(&wq->worker_refs);
3413d3bafd3SMax Kellermann return create_io_worker(wq, acct);
342ed29b0b4SJens Axboe }
343ed29b0b4SJens Axboe
io_wq_inc_running(struct io_worker * worker)344eb47943fSGabriel Krisman Bertazi static void io_wq_inc_running(struct io_worker *worker)
345ed29b0b4SJens Axboe {
346dfd63bafSGabriel Krisman Bertazi struct io_wq_acct *acct = io_wq_get_acct(worker);
347ed29b0b4SJens Axboe
348ed29b0b4SJens Axboe atomic_inc(&acct->nr_running);
349ed29b0b4SJens Axboe }
350ed29b0b4SJens Axboe
create_worker_cb(struct callback_head * cb)351ed29b0b4SJens Axboe static void create_worker_cb(struct callback_head *cb)
352ed29b0b4SJens Axboe {
353ed29b0b4SJens Axboe struct io_worker *worker;
354ed29b0b4SJens Axboe struct io_wq *wq;
355eb47943fSGabriel Krisman Bertazi
356dfd63bafSGabriel Krisman Bertazi struct io_wq_acct *acct;
357cd4ea81bSMax Kellermann bool activated_free_worker, do_create = false;
358ed29b0b4SJens Axboe
359ed29b0b4SJens Axboe worker = container_of(cb, struct io_worker, create_work);
360eb47943fSGabriel Krisman Bertazi wq = worker->wq;
3613d3bafd3SMax Kellermann acct = worker->acct;
3629d83e1f0SFengnan Chang
3639d83e1f0SFengnan Chang rcu_read_lock();
364cd4ea81bSMax Kellermann activated_free_worker = io_acct_activate_free_worker(acct);
3659d83e1f0SFengnan Chang rcu_read_unlock();
366cd4ea81bSMax Kellermann if (activated_free_worker)
3679d83e1f0SFengnan Chang goto no_need_create;
3689d83e1f0SFengnan Chang
369751eedc4SMax Kellermann raw_spin_lock(&acct->workers_lock);
370eb47943fSGabriel Krisman Bertazi
371ed29b0b4SJens Axboe if (acct->nr_workers < acct->max_workers) {
372ed29b0b4SJens Axboe acct->nr_workers++;
373ed29b0b4SJens Axboe do_create = true;
374ed29b0b4SJens Axboe }
375751eedc4SMax Kellermann raw_spin_unlock(&acct->workers_lock);
376ed29b0b4SJens Axboe if (do_create) {
3773d3bafd3SMax Kellermann create_io_worker(wq, acct);
378ed29b0b4SJens Axboe } else {
3799d83e1f0SFengnan Chang no_need_create:
380ed29b0b4SJens Axboe atomic_dec(&acct->nr_running);
381ed29b0b4SJens Axboe io_worker_ref_put(wq);
382ed29b0b4SJens Axboe }
383ed29b0b4SJens Axboe clear_bit_unlock(0, &worker->create_state);
384ed29b0b4SJens Axboe io_worker_release(worker);
385ed29b0b4SJens Axboe }
386ed29b0b4SJens Axboe
io_queue_worker_create(struct io_worker * worker,struct io_wq_acct * acct,task_work_func_t func)387ed29b0b4SJens Axboe static bool io_queue_worker_create(struct io_worker *worker,
388dfd63bafSGabriel Krisman Bertazi struct io_wq_acct *acct,
389ed29b0b4SJens Axboe task_work_func_t func)
390ed29b0b4SJens Axboe {
391eb47943fSGabriel Krisman Bertazi struct io_wq *wq = worker->wq;
392ed29b0b4SJens Axboe
393ed29b0b4SJens Axboe /* raced with exit, just ignore create call */
394ed29b0b4SJens Axboe if (test_bit(IO_WQ_BIT_EXIT, &wq->state))
395ed29b0b4SJens Axboe goto fail;
396ed29b0b4SJens Axboe if (!io_worker_get(worker))
397ed29b0b4SJens Axboe goto fail;
398ed29b0b4SJens Axboe /*
399ed29b0b4SJens Axboe * create_state manages ownership of create_work/index. We should
400ed29b0b4SJens Axboe * only need one entry per worker, as the worker going to sleep
401ed29b0b4SJens Axboe * will trigger the condition, and waking will clear it once it
402ed29b0b4SJens Axboe * runs the task_work.
403ed29b0b4SJens Axboe */
404ed29b0b4SJens Axboe if (test_bit(0, &worker->create_state) ||
405ed29b0b4SJens Axboe test_and_set_bit_lock(0, &worker->create_state))
406ed29b0b4SJens Axboe goto fail_release;
407ed29b0b4SJens Axboe
408ed29b0b4SJens Axboe atomic_inc(&wq->worker_refs);
409ed29b0b4SJens Axboe init_task_work(&worker->create_work, func);
410ed29b0b4SJens Axboe if (!task_work_add(wq->task, &worker->create_work, TWA_SIGNAL)) {
411ed29b0b4SJens Axboe /*
412ed29b0b4SJens Axboe * EXIT may have been set after checking it above, check after
413ed29b0b4SJens Axboe * adding the task_work and remove any creation item if it is
414ed29b0b4SJens Axboe * now set. wq exit does that too, but we can have added this
415ed29b0b4SJens Axboe * work item after we canceled in io_wq_exit_workers().
416ed29b0b4SJens Axboe */
417ed29b0b4SJens Axboe if (test_bit(IO_WQ_BIT_EXIT, &wq->state))
418ed29b0b4SJens Axboe io_wq_cancel_tw_create(wq);
419ed29b0b4SJens Axboe io_worker_ref_put(wq);
420ed29b0b4SJens Axboe return true;
421ed29b0b4SJens Axboe }
422ed29b0b4SJens Axboe io_worker_ref_put(wq);
423ed29b0b4SJens Axboe clear_bit_unlock(0, &worker->create_state);
424ed29b0b4SJens Axboe fail_release:
425ed29b0b4SJens Axboe io_worker_release(worker);
426ed29b0b4SJens Axboe fail:
427ed29b0b4SJens Axboe atomic_dec(&acct->nr_running);
428ed29b0b4SJens Axboe io_worker_ref_put(wq);
429ed29b0b4SJens Axboe return false;
430ed29b0b4SJens Axboe }
431ed29b0b4SJens Axboe
4320b2b066fSJens Axboe /* Defer if current and next work are both hashed to the same chain */
io_wq_hash_defer(struct io_wq_work * work,struct io_wq_acct * acct)4330b2b066fSJens Axboe static bool io_wq_hash_defer(struct io_wq_work *work, struct io_wq_acct *acct)
4340b2b066fSJens Axboe {
4350b2b066fSJens Axboe unsigned int hash, work_flags;
4360b2b066fSJens Axboe struct io_wq_work *next;
4370b2b066fSJens Axboe
4380b2b066fSJens Axboe lockdep_assert_held(&acct->lock);
4390b2b066fSJens Axboe
4400b2b066fSJens Axboe work_flags = atomic_read(&work->flags);
4410b2b066fSJens Axboe if (!__io_wq_is_hashed(work_flags))
4420b2b066fSJens Axboe return false;
4430b2b066fSJens Axboe
4440b2b066fSJens Axboe /* should not happen, io_acct_run_queue() said we had work */
4450b2b066fSJens Axboe if (wq_list_empty(&acct->work_list))
4460b2b066fSJens Axboe return true;
4470b2b066fSJens Axboe
4480b2b066fSJens Axboe hash = __io_get_work_hash(work_flags);
4490b2b066fSJens Axboe next = container_of(acct->work_list.first, struct io_wq_work, list);
4500b2b066fSJens Axboe work_flags = atomic_read(&next->flags);
4510b2b066fSJens Axboe if (!__io_wq_is_hashed(work_flags))
4520b2b066fSJens Axboe return false;
4530b2b066fSJens Axboe return hash == __io_get_work_hash(work_flags);
4540b2b066fSJens Axboe }
4550b2b066fSJens Axboe
io_wq_dec_running(struct io_worker * worker)456eb47943fSGabriel Krisman Bertazi static void io_wq_dec_running(struct io_worker *worker)
457ed29b0b4SJens Axboe {
458dfd63bafSGabriel Krisman Bertazi struct io_wq_acct *acct = io_wq_get_acct(worker);
459eb47943fSGabriel Krisman Bertazi struct io_wq *wq = worker->wq;
460ed29b0b4SJens Axboe
4618a565304SBreno Leitao if (!test_bit(IO_WORKER_F_UP, &worker->flags))
462ed29b0b4SJens Axboe return;
463ed29b0b4SJens Axboe
464ed29b0b4SJens Axboe if (!atomic_dec_and_test(&acct->nr_running))
465ed29b0b4SJens Axboe return;
4668343cae3SJens Axboe if (!worker->cur_work)
4678343cae3SJens Axboe return;
468ed29b0b4SJens Axboe if (!io_acct_run_queue(acct))
469ed29b0b4SJens Axboe return;
4700b2b066fSJens Axboe if (io_wq_hash_defer(worker->cur_work, acct)) {
4710b2b066fSJens Axboe raw_spin_unlock(&acct->lock);
4720b2b066fSJens Axboe return;
4730b2b066fSJens Axboe }
474ed29b0b4SJens Axboe
475de36a15fSJens Axboe raw_spin_unlock(&acct->lock);
476ed29b0b4SJens Axboe atomic_inc(&acct->nr_running);
477eb47943fSGabriel Krisman Bertazi atomic_inc(&wq->worker_refs);
478ed29b0b4SJens Axboe io_queue_worker_create(worker, acct, create_worker_cb);
479ed29b0b4SJens Axboe }
480ed29b0b4SJens Axboe
481ed29b0b4SJens Axboe /*
482ed29b0b4SJens Axboe * Worker will start processing some work. Move it to the busy list, if
483ed29b0b4SJens Axboe * it's currently on the freelist
484ed29b0b4SJens Axboe */
__io_worker_busy(struct io_wq_acct * acct,struct io_worker * worker)485751eedc4SMax Kellermann static void __io_worker_busy(struct io_wq_acct *acct, struct io_worker *worker)
486ed29b0b4SJens Axboe {
4878a565304SBreno Leitao if (test_bit(IO_WORKER_F_FREE, &worker->flags)) {
4888a565304SBreno Leitao clear_bit(IO_WORKER_F_FREE, &worker->flags);
489751eedc4SMax Kellermann raw_spin_lock(&acct->workers_lock);
490ed29b0b4SJens Axboe hlist_nulls_del_init_rcu(&worker->nulls_node);
491751eedc4SMax Kellermann raw_spin_unlock(&acct->workers_lock);
492ed29b0b4SJens Axboe }
493ed29b0b4SJens Axboe }
494ed29b0b4SJens Axboe
495ed29b0b4SJens Axboe /*
49607d99096SJens Axboe * No work, worker going to sleep. Move to freelist.
497ed29b0b4SJens Axboe */
__io_worker_idle(struct io_wq_acct * acct,struct io_worker * worker)498751eedc4SMax Kellermann static void __io_worker_idle(struct io_wq_acct *acct, struct io_worker *worker)
499751eedc4SMax Kellermann __must_hold(acct->workers_lock)
500ed29b0b4SJens Axboe {
5018a565304SBreno Leitao if (!test_bit(IO_WORKER_F_FREE, &worker->flags)) {
5028a565304SBreno Leitao set_bit(IO_WORKER_F_FREE, &worker->flags);
503751eedc4SMax Kellermann hlist_nulls_add_head_rcu(&worker->nulls_node, &acct->free_list);
504ed29b0b4SJens Axboe }
505ed29b0b4SJens Axboe }
506ed29b0b4SJens Axboe
io_wait_on_hash(struct io_wq * wq,unsigned int hash)507eb47943fSGabriel Krisman Bertazi static bool io_wait_on_hash(struct io_wq *wq, unsigned int hash)
508ed29b0b4SJens Axboe {
509ed29b0b4SJens Axboe bool ret = false;
510ed29b0b4SJens Axboe
511ed29b0b4SJens Axboe spin_lock_irq(&wq->hash->wait.lock);
512eb47943fSGabriel Krisman Bertazi if (list_empty(&wq->wait.entry)) {
513eb47943fSGabriel Krisman Bertazi __add_wait_queue(&wq->hash->wait, &wq->wait);
514ed29b0b4SJens Axboe if (!test_bit(hash, &wq->hash->map)) {
515ed29b0b4SJens Axboe __set_current_state(TASK_RUNNING);
516eb47943fSGabriel Krisman Bertazi list_del_init(&wq->wait.entry);
517ed29b0b4SJens Axboe ret = true;
518ed29b0b4SJens Axboe }
519ed29b0b4SJens Axboe }
520ed29b0b4SJens Axboe spin_unlock_irq(&wq->hash->wait.lock);
521ed29b0b4SJens Axboe return ret;
522ed29b0b4SJens Axboe }
523ed29b0b4SJens Axboe
io_get_next_work(struct io_wq_acct * acct,struct io_wq * wq)524dfd63bafSGabriel Krisman Bertazi static struct io_wq_work *io_get_next_work(struct io_wq_acct *acct,
5257d568502SMax Kellermann struct io_wq *wq)
526ed29b0b4SJens Axboe __must_hold(acct->lock)
527ed29b0b4SJens Axboe {
528ed29b0b4SJens Axboe struct io_wq_work_node *node, *prev;
529ed29b0b4SJens Axboe struct io_wq_work *work, *tail;
530ed29b0b4SJens Axboe unsigned int stall_hash = -1U;
531ed29b0b4SJens Axboe
532ed29b0b4SJens Axboe wq_list_for_each(node, prev, &acct->work_list) {
5336ee78354SMax Kellermann unsigned int work_flags;
534ed29b0b4SJens Axboe unsigned int hash;
535ed29b0b4SJens Axboe
536ed29b0b4SJens Axboe work = container_of(node, struct io_wq_work, list);
537ed29b0b4SJens Axboe
538ed29b0b4SJens Axboe /* not hashed, can run anytime */
5396ee78354SMax Kellermann work_flags = atomic_read(&work->flags);
5406ee78354SMax Kellermann if (!__io_wq_is_hashed(work_flags)) {
541ed29b0b4SJens Axboe wq_list_del(&acct->work_list, node, prev);
542ed29b0b4SJens Axboe return work;
543ed29b0b4SJens Axboe }
544ed29b0b4SJens Axboe
5456ee78354SMax Kellermann hash = __io_get_work_hash(work_flags);
546ed29b0b4SJens Axboe /* all items with this hash lie in [work, tail] */
547eb47943fSGabriel Krisman Bertazi tail = wq->hash_tail[hash];
548ed29b0b4SJens Axboe
549ed29b0b4SJens Axboe /* hashed, can run if not already running */
550eb47943fSGabriel Krisman Bertazi if (!test_and_set_bit(hash, &wq->hash->map)) {
551eb47943fSGabriel Krisman Bertazi wq->hash_tail[hash] = NULL;
552ed29b0b4SJens Axboe wq_list_cut(&acct->work_list, &tail->list, prev);
553ed29b0b4SJens Axboe return work;
554ed29b0b4SJens Axboe }
555ed29b0b4SJens Axboe if (stall_hash == -1U)
556ed29b0b4SJens Axboe stall_hash = hash;
557ed29b0b4SJens Axboe /* fast forward to a next hash, for-each will fix up @prev */
558ed29b0b4SJens Axboe node = &tail->list;
559ed29b0b4SJens Axboe }
560ed29b0b4SJens Axboe
561ed29b0b4SJens Axboe if (stall_hash != -1U) {
562ed29b0b4SJens Axboe bool unstalled;
563ed29b0b4SJens Axboe
564ed29b0b4SJens Axboe /*
565ed29b0b4SJens Axboe * Set this before dropping the lock to avoid racing with new
566ed29b0b4SJens Axboe * work being added and clearing the stalled bit.
567ed29b0b4SJens Axboe */
568ed29b0b4SJens Axboe set_bit(IO_ACCT_STALLED_BIT, &acct->flags);
569ed29b0b4SJens Axboe raw_spin_unlock(&acct->lock);
570eb47943fSGabriel Krisman Bertazi unstalled = io_wait_on_hash(wq, stall_hash);
571ed29b0b4SJens Axboe raw_spin_lock(&acct->lock);
572ed29b0b4SJens Axboe if (unstalled) {
573ed29b0b4SJens Axboe clear_bit(IO_ACCT_STALLED_BIT, &acct->flags);
574eb47943fSGabriel Krisman Bertazi if (wq_has_sleeper(&wq->hash->wait))
575eb47943fSGabriel Krisman Bertazi wake_up(&wq->hash->wait);
576ed29b0b4SJens Axboe }
577ed29b0b4SJens Axboe }
578ed29b0b4SJens Axboe
579ed29b0b4SJens Axboe return NULL;
580ed29b0b4SJens Axboe }
581ed29b0b4SJens Axboe
io_assign_current_work(struct io_worker * worker,struct io_wq_work * work)582ed29b0b4SJens Axboe static void io_assign_current_work(struct io_worker *worker,
583ed29b0b4SJens Axboe struct io_wq_work *work)
584ed29b0b4SJens Axboe {
585ed29b0b4SJens Axboe if (work) {
586024f15e0SPavel Begunkov io_run_task_work();
587ed29b0b4SJens Axboe cond_resched();
588ed29b0b4SJens Axboe }
589ed29b0b4SJens Axboe
590ed29b0b4SJens Axboe raw_spin_lock(&worker->lock);
591ed29b0b4SJens Axboe worker->cur_work = work;
592ed29b0b4SJens Axboe raw_spin_unlock(&worker->lock);
593ed29b0b4SJens Axboe }
594ed29b0b4SJens Axboe
595de36a15fSJens Axboe /*
596de36a15fSJens Axboe * Called with acct->lock held, drops it before returning
597de36a15fSJens Axboe */
io_worker_handle_work(struct io_wq_acct * acct,struct io_worker * worker)598de36a15fSJens Axboe static void io_worker_handle_work(struct io_wq_acct *acct,
599de36a15fSJens Axboe struct io_worker *worker)
600de36a15fSJens Axboe __releases(&acct->lock)
601ed29b0b4SJens Axboe {
602eb47943fSGabriel Krisman Bertazi struct io_wq *wq = worker->wq;
603ed29b0b4SJens Axboe
604ed29b0b4SJens Axboe do {
60510dc9593SJens Axboe bool do_kill = test_bit(IO_WQ_BIT_EXIT, &wq->state);
606ed29b0b4SJens Axboe struct io_wq_work *work;
607ed29b0b4SJens Axboe
608ed29b0b4SJens Axboe /*
609ed29b0b4SJens Axboe * If we got some work, mark us as busy. If we didn't, but
610ed29b0b4SJens Axboe * the list isn't empty, it means we stalled on hashed work.
611ed29b0b4SJens Axboe * Mark us stalled so we don't keep looking for work when we
612ed29b0b4SJens Axboe * can't make progress, any work completion or insertion will
613ed29b0b4SJens Axboe * clear the stalled flag.
614ed29b0b4SJens Axboe */
6157d568502SMax Kellermann work = io_get_next_work(acct, wq);
616ed29b0b4SJens Axboe if (work) {
617ed29b0b4SJens Axboe /*
618ed29b0b4SJens Axboe * Make sure cancelation can find this, even before
619ed29b0b4SJens Axboe * it becomes the active work. That avoids a window
620ed29b0b4SJens Axboe * where the work has been removed from our general
621ed29b0b4SJens Axboe * work list, but isn't yet discoverable as the
622ed29b0b4SJens Axboe * current work item for this worker.
623ed29b0b4SJens Axboe */
624ed29b0b4SJens Axboe raw_spin_lock(&worker->lock);
62524c3fc5cSGabriel Krisman Bertazi worker->cur_work = work;
626ed29b0b4SJens Axboe raw_spin_unlock(&worker->lock);
627ed29b0b4SJens Axboe }
628068c27e3SGabriel Krisman Bertazi
629068c27e3SGabriel Krisman Bertazi raw_spin_unlock(&acct->lock);
630068c27e3SGabriel Krisman Bertazi
631068c27e3SGabriel Krisman Bertazi if (!work)
632068c27e3SGabriel Krisman Bertazi break;
633068c27e3SGabriel Krisman Bertazi
634751eedc4SMax Kellermann __io_worker_busy(acct, worker);
635068c27e3SGabriel Krisman Bertazi
636ed29b0b4SJens Axboe io_assign_current_work(worker, work);
637ed29b0b4SJens Axboe __set_current_state(TASK_RUNNING);
638ed29b0b4SJens Axboe
639ed29b0b4SJens Axboe /* handle a whole dependent link */
640ed29b0b4SJens Axboe do {
641ed29b0b4SJens Axboe struct io_wq_work *next_hashed, *linked;
6426ee78354SMax Kellermann unsigned int work_flags = atomic_read(&work->flags);
643486ba4d8SMax Kellermann unsigned int hash = __io_wq_is_hashed(work_flags)
644486ba4d8SMax Kellermann ? __io_get_work_hash(work_flags)
645486ba4d8SMax Kellermann : -1U;
646ed29b0b4SJens Axboe
647ed29b0b4SJens Axboe next_hashed = wq_next_work(work);
648ed29b0b4SJens Axboe
6493474d1b9SJens Axboe if (do_kill &&
6506ee78354SMax Kellermann (work_flags & IO_WQ_WORK_UNBOUND))
6513474d1b9SJens Axboe atomic_or(IO_WQ_WORK_CANCEL, &work->flags);
6529fe99eedSCaleb Sander Mateos io_wq_submit_work(work);
653ed29b0b4SJens Axboe io_assign_current_work(worker, NULL);
654ed29b0b4SJens Axboe
6559fe99eedSCaleb Sander Mateos linked = io_wq_free_work(work);
656ed29b0b4SJens Axboe work = next_hashed;
657ed29b0b4SJens Axboe if (!work && linked && !io_wq_is_hashed(linked)) {
658ed29b0b4SJens Axboe work = linked;
659ed29b0b4SJens Axboe linked = NULL;
660ed29b0b4SJens Axboe }
661ed29b0b4SJens Axboe io_assign_current_work(worker, work);
662ed29b0b4SJens Axboe if (linked)
663eb47943fSGabriel Krisman Bertazi io_wq_enqueue(wq, linked);
664ed29b0b4SJens Axboe
665ed29b0b4SJens Axboe if (hash != -1U && !next_hashed) {
666ed29b0b4SJens Axboe /* serialize hash clear with wake_up() */
667ed29b0b4SJens Axboe spin_lock_irq(&wq->hash->wait.lock);
668ed29b0b4SJens Axboe clear_bit(hash, &wq->hash->map);
669ed29b0b4SJens Axboe clear_bit(IO_ACCT_STALLED_BIT, &acct->flags);
670ed29b0b4SJens Axboe spin_unlock_irq(&wq->hash->wait.lock);
671ed29b0b4SJens Axboe if (wq_has_sleeper(&wq->hash->wait))
672ed29b0b4SJens Axboe wake_up(&wq->hash->wait);
673ed29b0b4SJens Axboe }
674ed29b0b4SJens Axboe } while (work);
675de36a15fSJens Axboe
676de36a15fSJens Axboe if (!__io_acct_run_queue(acct))
677de36a15fSJens Axboe break;
678de36a15fSJens Axboe raw_spin_lock(&acct->lock);
679ed29b0b4SJens Axboe } while (1);
680ed29b0b4SJens Axboe }
681ed29b0b4SJens Axboe
io_wq_worker(void * data)682eb47943fSGabriel Krisman Bertazi static int io_wq_worker(void *data)
683ed29b0b4SJens Axboe {
684ed29b0b4SJens Axboe struct io_worker *worker = data;
685dfd63bafSGabriel Krisman Bertazi struct io_wq_acct *acct = io_wq_get_acct(worker);
686eb47943fSGabriel Krisman Bertazi struct io_wq *wq = worker->wq;
68701e68ce0SJens Axboe bool exit_mask = false, last_timeout = false;
6883a3f61ceSKees Cook char buf[TASK_COMM_LEN] = {};
689ed29b0b4SJens Axboe
6908a565304SBreno Leitao set_mask_bits(&worker->flags, 0,
6918a565304SBreno Leitao BIT(IO_WORKER_F_UP) | BIT(IO_WORKER_F_RUNNING));
692ed29b0b4SJens Axboe
693ed29b0b4SJens Axboe snprintf(buf, sizeof(buf), "iou-wrk-%d", wq->task->pid);
694ed29b0b4SJens Axboe set_task_comm(current, buf);
695ed29b0b4SJens Axboe
696ed29b0b4SJens Axboe while (!test_bit(IO_WQ_BIT_EXIT, &wq->state)) {
697ed29b0b4SJens Axboe long ret;
698ed29b0b4SJens Axboe
699ed29b0b4SJens Axboe set_current_state(TASK_INTERRUPTIBLE);
700de36a15fSJens Axboe
701de36a15fSJens Axboe /*
702de36a15fSJens Axboe * If we have work to do, io_acct_run_queue() returns with
703de36a15fSJens Axboe * the acct->lock held. If not, it will drop it.
704de36a15fSJens Axboe */
705ed29b0b4SJens Axboe while (io_acct_run_queue(acct))
706de36a15fSJens Axboe io_worker_handle_work(acct, worker);
707ed29b0b4SJens Axboe
708751eedc4SMax Kellermann raw_spin_lock(&acct->workers_lock);
70901e68ce0SJens Axboe /*
71001e68ce0SJens Axboe * Last sleep timed out. Exit if we're not the last worker,
71138aa434aSLi Chen * or if someone modified our affinity. If wq is marked
71238aa434aSLi Chen * idle-exit, drop the worker as well. This is used to avoid
71338aa434aSLi Chen * keeping io-wq workers around for tasks that no longer have
71438aa434aSLi Chen * any active io_uring instances.
71501e68ce0SJens Axboe */
71638aa434aSLi Chen if ((last_timeout && (exit_mask || acct->nr_workers > 1)) ||
71738aa434aSLi Chen test_bit(IO_WQ_BIT_EXIT_ON_IDLE, &wq->state)) {
718ed29b0b4SJens Axboe acct->nr_workers--;
719751eedc4SMax Kellermann raw_spin_unlock(&acct->workers_lock);
720ed29b0b4SJens Axboe __set_current_state(TASK_RUNNING);
721ed29b0b4SJens Axboe break;
722ed29b0b4SJens Axboe }
723ed29b0b4SJens Axboe last_timeout = false;
724751eedc4SMax Kellermann __io_worker_idle(acct, worker);
725751eedc4SMax Kellermann raw_spin_unlock(&acct->workers_lock);
726024f15e0SPavel Begunkov if (io_run_task_work())
727ed29b0b4SJens Axboe continue;
728ed29b0b4SJens Axboe ret = schedule_timeout(WORKER_IDLE_TIMEOUT);
729ed29b0b4SJens Axboe if (signal_pending(current)) {
730ed29b0b4SJens Axboe struct ksignal ksig;
731ed29b0b4SJens Axboe
732ed29b0b4SJens Axboe if (!get_signal(&ksig))
733ed29b0b4SJens Axboe continue;
734ed29b0b4SJens Axboe break;
735ed29b0b4SJens Axboe }
73601e68ce0SJens Axboe if (!ret) {
73701e68ce0SJens Axboe last_timeout = true;
73801e68ce0SJens Axboe exit_mask = !cpumask_test_cpu(raw_smp_processor_id(),
739eb47943fSGabriel Krisman Bertazi wq->cpu_mask);
74001e68ce0SJens Axboe }
741ed29b0b4SJens Axboe }
742ed29b0b4SJens Axboe
743de36a15fSJens Axboe if (test_bit(IO_WQ_BIT_EXIT, &wq->state) && io_acct_run_queue(acct))
744de36a15fSJens Axboe io_worker_handle_work(acct, worker);
745ed29b0b4SJens Axboe
746ed29b0b4SJens Axboe io_worker_exit(worker);
747ed29b0b4SJens Axboe return 0;
748ed29b0b4SJens Axboe }
749ed29b0b4SJens Axboe
750ed29b0b4SJens Axboe /*
751ed29b0b4SJens Axboe * Called when a worker is scheduled in. Mark us as currently running.
752ed29b0b4SJens Axboe */
io_wq_worker_running(struct task_struct * tsk)753ed29b0b4SJens Axboe void io_wq_worker_running(struct task_struct *tsk)
754ed29b0b4SJens Axboe {
755ed29b0b4SJens Axboe struct io_worker *worker = tsk->worker_private;
756ed29b0b4SJens Axboe
757ed29b0b4SJens Axboe if (!worker)
758ed29b0b4SJens Axboe return;
7598a565304SBreno Leitao if (!test_bit(IO_WORKER_F_UP, &worker->flags))
760ed29b0b4SJens Axboe return;
7618a565304SBreno Leitao if (test_bit(IO_WORKER_F_RUNNING, &worker->flags))
762ed29b0b4SJens Axboe return;
7638a565304SBreno Leitao set_bit(IO_WORKER_F_RUNNING, &worker->flags);
764eb47943fSGabriel Krisman Bertazi io_wq_inc_running(worker);
765ed29b0b4SJens Axboe }
766ed29b0b4SJens Axboe
767ed29b0b4SJens Axboe /*
768ed29b0b4SJens Axboe * Called when worker is going to sleep. If there are no workers currently
769ed29b0b4SJens Axboe * running and we have work pending, wake up a free one or create a new one.
770ed29b0b4SJens Axboe */
io_wq_worker_sleeping(struct task_struct * tsk)771ed29b0b4SJens Axboe void io_wq_worker_sleeping(struct task_struct *tsk)
772ed29b0b4SJens Axboe {
773ed29b0b4SJens Axboe struct io_worker *worker = tsk->worker_private;
774ed29b0b4SJens Axboe
775ed29b0b4SJens Axboe if (!worker)
776ed29b0b4SJens Axboe return;
7778a565304SBreno Leitao if (!test_bit(IO_WORKER_F_UP, &worker->flags))
778ed29b0b4SJens Axboe return;
7798a565304SBreno Leitao if (!test_bit(IO_WORKER_F_RUNNING, &worker->flags))
780ed29b0b4SJens Axboe return;
781ed29b0b4SJens Axboe
7828a565304SBreno Leitao clear_bit(IO_WORKER_F_RUNNING, &worker->flags);
783eb47943fSGabriel Krisman Bertazi io_wq_dec_running(worker);
784ed29b0b4SJens Axboe }
785ed29b0b4SJens Axboe
io_init_new_worker(struct io_wq * wq,struct io_wq_acct * acct,struct io_worker * worker,struct task_struct * tsk)786751eedc4SMax Kellermann static void io_init_new_worker(struct io_wq *wq, struct io_wq_acct *acct, struct io_worker *worker,
787ed29b0b4SJens Axboe struct task_struct *tsk)
788ed29b0b4SJens Axboe {
789ed29b0b4SJens Axboe tsk->worker_private = worker;
790ed29b0b4SJens Axboe worker->task = tsk;
791eb47943fSGabriel Krisman Bertazi set_cpus_allowed_ptr(tsk, wq->cpu_mask);
792ed29b0b4SJens Axboe
793751eedc4SMax Kellermann raw_spin_lock(&acct->workers_lock);
794751eedc4SMax Kellermann hlist_nulls_add_head_rcu(&worker->nulls_node, &acct->free_list);
795751eedc4SMax Kellermann list_add_tail_rcu(&worker->all_list, &acct->all_list);
7968a565304SBreno Leitao set_bit(IO_WORKER_F_FREE, &worker->flags);
797751eedc4SMax Kellermann raw_spin_unlock(&acct->workers_lock);
798ed29b0b4SJens Axboe wake_up_new_task(tsk);
799ed29b0b4SJens Axboe }
800ed29b0b4SJens Axboe
io_wq_work_match_all(struct io_wq_work * work,void * data)801ed29b0b4SJens Axboe static bool io_wq_work_match_all(struct io_wq_work *work, void *data)
802ed29b0b4SJens Axboe {
803ed29b0b4SJens Axboe return true;
804ed29b0b4SJens Axboe }
805ed29b0b4SJens Axboe
io_should_retry_thread(struct io_worker * worker,long err)8060453aad6SPavel Begunkov static inline bool io_should_retry_thread(struct io_worker *worker, long err)
807ed29b0b4SJens Axboe {
808ed29b0b4SJens Axboe /*
809ed29b0b4SJens Axboe * Prevent perpetual task_work retry, if the task (or its group) is
810ed29b0b4SJens Axboe * exiting.
811ed29b0b4SJens Axboe */
812ed29b0b4SJens Axboe if (fatal_signal_pending(current))
813ed29b0b4SJens Axboe return false;
814ed29b0b4SJens Axboe
81534c78b86SCaleb Sander Mateos worker->init_retries++;
816ed29b0b4SJens Axboe switch (err) {
817ed29b0b4SJens Axboe case -EAGAIN:
81834c78b86SCaleb Sander Mateos return worker->init_retries <= WORKER_INIT_LIMIT;
81934c78b86SCaleb Sander Mateos /* Analogous to a fork() syscall, always retry on a restartable error */
820ed29b0b4SJens Axboe case -ERESTARTSYS:
821ed29b0b4SJens Axboe case -ERESTARTNOINTR:
822ed29b0b4SJens Axboe case -ERESTARTNOHAND:
823ed29b0b4SJens Axboe return true;
824ed29b0b4SJens Axboe default:
825ed29b0b4SJens Axboe return false;
826ed29b0b4SJens Axboe }
827ed29b0b4SJens Axboe }
828ed29b0b4SJens Axboe
queue_create_worker_retry(struct io_worker * worker)82913918315SUday Shankar static void queue_create_worker_retry(struct io_worker *worker)
83013918315SUday Shankar {
83113918315SUday Shankar /*
83213918315SUday Shankar * We only bother retrying because there's a chance that the
83313918315SUday Shankar * failure to create a worker is due to some temporary condition
83413918315SUday Shankar * in the forking task (e.g. outstanding signal); give the task
83513918315SUday Shankar * some time to clear that condition.
83613918315SUday Shankar */
83713918315SUday Shankar schedule_delayed_work(&worker->work,
83813918315SUday Shankar msecs_to_jiffies(worker->init_retries * 5));
83913918315SUday Shankar }
84013918315SUday Shankar
create_worker_cont(struct callback_head * cb)841ed29b0b4SJens Axboe static void create_worker_cont(struct callback_head *cb)
842ed29b0b4SJens Axboe {
843ed29b0b4SJens Axboe struct io_worker *worker;
844ed29b0b4SJens Axboe struct task_struct *tsk;
845eb47943fSGabriel Krisman Bertazi struct io_wq *wq;
846751eedc4SMax Kellermann struct io_wq_acct *acct;
847ed29b0b4SJens Axboe
848ed29b0b4SJens Axboe worker = container_of(cb, struct io_worker, create_work);
849ed29b0b4SJens Axboe clear_bit_unlock(0, &worker->create_state);
850eb47943fSGabriel Krisman Bertazi wq = worker->wq;
851751eedc4SMax Kellermann acct = io_wq_get_acct(worker);
852eb47943fSGabriel Krisman Bertazi tsk = create_io_thread(io_wq_worker, worker, NUMA_NO_NODE);
853ed29b0b4SJens Axboe if (!IS_ERR(tsk)) {
854751eedc4SMax Kellermann io_init_new_worker(wq, acct, worker, tsk);
855ed29b0b4SJens Axboe io_worker_release(worker);
856ed29b0b4SJens Axboe return;
8570453aad6SPavel Begunkov } else if (!io_should_retry_thread(worker, PTR_ERR(tsk))) {
858ed29b0b4SJens Axboe atomic_dec(&acct->nr_running);
859751eedc4SMax Kellermann raw_spin_lock(&acct->workers_lock);
860ed29b0b4SJens Axboe acct->nr_workers--;
861ed29b0b4SJens Axboe if (!acct->nr_workers) {
862ed29b0b4SJens Axboe struct io_cb_cancel_data match = {
863ed29b0b4SJens Axboe .fn = io_wq_work_match_all,
864ed29b0b4SJens Axboe .cancel_all = true,
865ed29b0b4SJens Axboe };
866ed29b0b4SJens Axboe
867751eedc4SMax Kellermann raw_spin_unlock(&acct->workers_lock);
868eb47943fSGabriel Krisman Bertazi while (io_acct_cancel_pending_work(wq, acct, &match))
869ed29b0b4SJens Axboe ;
870ed29b0b4SJens Axboe } else {
871751eedc4SMax Kellermann raw_spin_unlock(&acct->workers_lock);
872ed29b0b4SJens Axboe }
873eb47943fSGabriel Krisman Bertazi io_worker_ref_put(wq);
874ed29b0b4SJens Axboe kfree(worker);
875ed29b0b4SJens Axboe return;
876ed29b0b4SJens Axboe }
877ed29b0b4SJens Axboe
878ed29b0b4SJens Axboe /* re-create attempts grab a new worker ref, drop the existing one */
879ed29b0b4SJens Axboe io_worker_release(worker);
88013918315SUday Shankar queue_create_worker_retry(worker);
881ed29b0b4SJens Axboe }
882ed29b0b4SJens Axboe
io_workqueue_create(struct work_struct * work)883ed29b0b4SJens Axboe static void io_workqueue_create(struct work_struct *work)
884ed29b0b4SJens Axboe {
88513918315SUday Shankar struct io_worker *worker = container_of(work, struct io_worker,
88613918315SUday Shankar work.work);
887dfd63bafSGabriel Krisman Bertazi struct io_wq_acct *acct = io_wq_get_acct(worker);
888ed29b0b4SJens Axboe
889ed29b0b4SJens Axboe if (!io_queue_worker_create(worker, acct, create_worker_cont))
890ed29b0b4SJens Axboe kfree(worker);
891ed29b0b4SJens Axboe }
892ed29b0b4SJens Axboe
create_io_worker(struct io_wq * wq,struct io_wq_acct * acct)8933d3bafd3SMax Kellermann static bool create_io_worker(struct io_wq *wq, struct io_wq_acct *acct)
894ed29b0b4SJens Axboe {
895ed29b0b4SJens Axboe struct io_worker *worker;
896ed29b0b4SJens Axboe struct task_struct *tsk;
897ed29b0b4SJens Axboe
898ed29b0b4SJens Axboe __set_current_state(TASK_RUNNING);
899ed29b0b4SJens Axboe
900*bf4afc53SLinus Torvalds worker = kzalloc_obj(*worker);
901ed29b0b4SJens Axboe if (!worker) {
902ed29b0b4SJens Axboe fail:
903ed29b0b4SJens Axboe atomic_dec(&acct->nr_running);
904751eedc4SMax Kellermann raw_spin_lock(&acct->workers_lock);
905ed29b0b4SJens Axboe acct->nr_workers--;
906751eedc4SMax Kellermann raw_spin_unlock(&acct->workers_lock);
907ed29b0b4SJens Axboe io_worker_ref_put(wq);
908ed29b0b4SJens Axboe return false;
909ed29b0b4SJens Axboe }
910ed29b0b4SJens Axboe
911ed29b0b4SJens Axboe refcount_set(&worker->ref, 1);
912eb47943fSGabriel Krisman Bertazi worker->wq = wq;
9133d3bafd3SMax Kellermann worker->acct = acct;
914ed29b0b4SJens Axboe raw_spin_lock_init(&worker->lock);
915ed29b0b4SJens Axboe init_completion(&worker->ref_done);
916ed29b0b4SJens Axboe
917eb47943fSGabriel Krisman Bertazi tsk = create_io_thread(io_wq_worker, worker, NUMA_NO_NODE);
918ed29b0b4SJens Axboe if (!IS_ERR(tsk)) {
919751eedc4SMax Kellermann io_init_new_worker(wq, acct, worker, tsk);
9200453aad6SPavel Begunkov } else if (!io_should_retry_thread(worker, PTR_ERR(tsk))) {
921ed29b0b4SJens Axboe kfree(worker);
922ed29b0b4SJens Axboe goto fail;
923ed29b0b4SJens Axboe } else {
92413918315SUday Shankar INIT_DELAYED_WORK(&worker->work, io_workqueue_create);
92513918315SUday Shankar queue_create_worker_retry(worker);
926ed29b0b4SJens Axboe }
927ed29b0b4SJens Axboe
928ed29b0b4SJens Axboe return true;
929ed29b0b4SJens Axboe }
930ed29b0b4SJens Axboe
931ed29b0b4SJens Axboe /*
932ed29b0b4SJens Axboe * Iterate the passed in list and call the specific function for each
933ed29b0b4SJens Axboe * worker that isn't exiting
934ed29b0b4SJens Axboe */
io_acct_for_each_worker(struct io_wq_acct * acct,bool (* func)(struct io_worker *,void *),void * data)935751eedc4SMax Kellermann static bool io_acct_for_each_worker(struct io_wq_acct *acct,
936ed29b0b4SJens Axboe bool (*func)(struct io_worker *, void *),
937ed29b0b4SJens Axboe void *data)
938ed29b0b4SJens Axboe {
939ed29b0b4SJens Axboe struct io_worker *worker;
940ed29b0b4SJens Axboe bool ret = false;
941ed29b0b4SJens Axboe
942751eedc4SMax Kellermann list_for_each_entry_rcu(worker, &acct->all_list, all_list) {
943ed29b0b4SJens Axboe if (io_worker_get(worker)) {
944ed29b0b4SJens Axboe /* no task if node is/was offline */
945ed29b0b4SJens Axboe if (worker->task)
946ed29b0b4SJens Axboe ret = func(worker, data);
947ed29b0b4SJens Axboe io_worker_release(worker);
948ed29b0b4SJens Axboe if (ret)
949ed29b0b4SJens Axboe break;
950ed29b0b4SJens Axboe }
951ed29b0b4SJens Axboe }
952ed29b0b4SJens Axboe
953ed29b0b4SJens Axboe return ret;
954ed29b0b4SJens Axboe }
955ed29b0b4SJens Axboe
io_wq_for_each_worker(struct io_wq * wq,bool (* func)(struct io_worker *,void *),void * data)956e4fdbca2SJens Axboe static void io_wq_for_each_worker(struct io_wq *wq,
957751eedc4SMax Kellermann bool (*func)(struct io_worker *, void *),
958751eedc4SMax Kellermann void *data)
959751eedc4SMax Kellermann {
960e4fdbca2SJens Axboe for (int i = 0; i < IO_WQ_ACCT_NR; i++)
961e0392a10SJens Axboe if (io_acct_for_each_worker(&wq->acct[i], func, data))
962e4fdbca2SJens Axboe break;
963751eedc4SMax Kellermann }
964751eedc4SMax Kellermann
io_wq_worker_wake(struct io_worker * worker,void * data)965ed29b0b4SJens Axboe static bool io_wq_worker_wake(struct io_worker *worker, void *data)
966ed29b0b4SJens Axboe {
967ed29b0b4SJens Axboe __set_notify_signal(worker->task);
968ed29b0b4SJens Axboe wake_up_process(worker->task);
969ed29b0b4SJens Axboe return false;
970ed29b0b4SJens Axboe }
971ed29b0b4SJens Axboe
io_wq_set_exit_on_idle(struct io_wq * wq,bool enable)97238aa434aSLi Chen void io_wq_set_exit_on_idle(struct io_wq *wq, bool enable)
97338aa434aSLi Chen {
97438aa434aSLi Chen if (!wq->task)
97538aa434aSLi Chen return;
97638aa434aSLi Chen
97738aa434aSLi Chen if (!enable) {
97838aa434aSLi Chen clear_bit(IO_WQ_BIT_EXIT_ON_IDLE, &wq->state);
97938aa434aSLi Chen return;
98038aa434aSLi Chen }
98138aa434aSLi Chen
98238aa434aSLi Chen if (test_and_set_bit(IO_WQ_BIT_EXIT_ON_IDLE, &wq->state))
98338aa434aSLi Chen return;
98438aa434aSLi Chen
98538aa434aSLi Chen rcu_read_lock();
98638aa434aSLi Chen io_wq_for_each_worker(wq, io_wq_worker_wake, NULL);
98738aa434aSLi Chen rcu_read_unlock();
98838aa434aSLi Chen }
98938aa434aSLi Chen
io_run_cancel(struct io_wq_work * work,struct io_wq * wq)990eb47943fSGabriel Krisman Bertazi static void io_run_cancel(struct io_wq_work *work, struct io_wq *wq)
991ed29b0b4SJens Axboe {
992ed29b0b4SJens Axboe do {
9933474d1b9SJens Axboe atomic_or(IO_WQ_WORK_CANCEL, &work->flags);
9949fe99eedSCaleb Sander Mateos io_wq_submit_work(work);
9959fe99eedSCaleb Sander Mateos work = io_wq_free_work(work);
996ed29b0b4SJens Axboe } while (work);
997ed29b0b4SJens Axboe }
998ed29b0b4SJens Axboe
io_wq_insert_work(struct io_wq * wq,struct io_wq_acct * acct,struct io_wq_work * work,unsigned int work_flags)9996ee78354SMax Kellermann static void io_wq_insert_work(struct io_wq *wq, struct io_wq_acct *acct,
10006ee78354SMax Kellermann struct io_wq_work *work, unsigned int work_flags)
1001ed29b0b4SJens Axboe {
1002ed29b0b4SJens Axboe unsigned int hash;
1003ed29b0b4SJens Axboe struct io_wq_work *tail;
1004ed29b0b4SJens Axboe
10056ee78354SMax Kellermann if (!__io_wq_is_hashed(work_flags)) {
1006ed29b0b4SJens Axboe append:
1007ed29b0b4SJens Axboe wq_list_add_tail(&work->list, &acct->work_list);
1008ed29b0b4SJens Axboe return;
1009ed29b0b4SJens Axboe }
1010ed29b0b4SJens Axboe
10116ee78354SMax Kellermann hash = __io_get_work_hash(work_flags);
1012eb47943fSGabriel Krisman Bertazi tail = wq->hash_tail[hash];
1013eb47943fSGabriel Krisman Bertazi wq->hash_tail[hash] = work;
1014ed29b0b4SJens Axboe if (!tail)
1015ed29b0b4SJens Axboe goto append;
1016ed29b0b4SJens Axboe
1017ed29b0b4SJens Axboe wq_list_add_after(&work->list, &tail->list, &acct->work_list);
1018ed29b0b4SJens Axboe }
1019ed29b0b4SJens Axboe
io_wq_work_match_item(struct io_wq_work * work,void * data)1020ed29b0b4SJens Axboe static bool io_wq_work_match_item(struct io_wq_work *work, void *data)
1021ed29b0b4SJens Axboe {
1022ed29b0b4SJens Axboe return work == data;
1023ed29b0b4SJens Axboe }
1024ed29b0b4SJens Axboe
io_wq_enqueue(struct io_wq * wq,struct io_wq_work * work)1025eb47943fSGabriel Krisman Bertazi void io_wq_enqueue(struct io_wq *wq, struct io_wq_work *work)
1026ed29b0b4SJens Axboe {
10273474d1b9SJens Axboe unsigned int work_flags = atomic_read(&work->flags);
10286ee78354SMax Kellermann struct io_wq_acct *acct = io_work_get_acct(wq, work_flags);
102991215f70SSu Hui struct io_cb_cancel_data match = {
103091215f70SSu Hui .fn = io_wq_work_match_item,
103191215f70SSu Hui .data = work,
103291215f70SSu Hui .cancel_all = false,
103391215f70SSu Hui };
1034ed29b0b4SJens Axboe bool do_create;
1035ed29b0b4SJens Axboe
1036ed29b0b4SJens Axboe /*
1037ed29b0b4SJens Axboe * If io-wq is exiting for this task, or if the request has explicitly
1038ed29b0b4SJens Axboe * been marked as one that should not get executed, cancel it here.
1039ed29b0b4SJens Axboe */
1040eb47943fSGabriel Krisman Bertazi if (test_bit(IO_WQ_BIT_EXIT, &wq->state) ||
10413474d1b9SJens Axboe (work_flags & IO_WQ_WORK_CANCEL)) {
1042eb47943fSGabriel Krisman Bertazi io_run_cancel(work, wq);
1043ed29b0b4SJens Axboe return;
1044ed29b0b4SJens Axboe }
1045ed29b0b4SJens Axboe
1046ed29b0b4SJens Axboe raw_spin_lock(&acct->lock);
10476ee78354SMax Kellermann io_wq_insert_work(wq, acct, work, work_flags);
1048ed29b0b4SJens Axboe clear_bit(IO_ACCT_STALLED_BIT, &acct->flags);
1049ed29b0b4SJens Axboe raw_spin_unlock(&acct->lock);
1050ed29b0b4SJens Axboe
1051ed29b0b4SJens Axboe rcu_read_lock();
1052751eedc4SMax Kellermann do_create = !io_acct_activate_free_worker(acct);
1053ed29b0b4SJens Axboe rcu_read_unlock();
1054ed29b0b4SJens Axboe
1055ed29b0b4SJens Axboe if (do_create && ((work_flags & IO_WQ_WORK_CONCURRENT) ||
1056ed29b0b4SJens Axboe !atomic_read(&acct->nr_running))) {
1057ed29b0b4SJens Axboe bool did_create;
1058ed29b0b4SJens Axboe
1059eb47943fSGabriel Krisman Bertazi did_create = io_wq_create_worker(wq, acct);
1060ed29b0b4SJens Axboe if (likely(did_create))
1061ed29b0b4SJens Axboe return;
1062ed29b0b4SJens Axboe
1063751eedc4SMax Kellermann raw_spin_lock(&acct->workers_lock);
1064ed29b0b4SJens Axboe if (acct->nr_workers) {
1065751eedc4SMax Kellermann raw_spin_unlock(&acct->workers_lock);
1066ed29b0b4SJens Axboe return;
1067ed29b0b4SJens Axboe }
1068751eedc4SMax Kellermann raw_spin_unlock(&acct->workers_lock);
1069ed29b0b4SJens Axboe
1070ed29b0b4SJens Axboe /* fatal condition, failed to create the first worker */
1071eb47943fSGabriel Krisman Bertazi io_acct_cancel_pending_work(wq, acct, &match);
1072ed29b0b4SJens Axboe }
1073ed29b0b4SJens Axboe }
1074ed29b0b4SJens Axboe
1075ed29b0b4SJens Axboe /*
1076ed29b0b4SJens Axboe * Work items that hash to the same value will not be done in parallel.
1077ed29b0b4SJens Axboe * Used to limit concurrent writes, generally hashed by inode.
1078ed29b0b4SJens Axboe */
io_wq_hash_work(struct io_wq_work * work,void * val)1079ed29b0b4SJens Axboe void io_wq_hash_work(struct io_wq_work *work, void *val)
1080ed29b0b4SJens Axboe {
1081ed29b0b4SJens Axboe unsigned int bit;
1082ed29b0b4SJens Axboe
1083ed29b0b4SJens Axboe bit = hash_ptr(val, IO_WQ_HASH_ORDER);
10843474d1b9SJens Axboe atomic_or(IO_WQ_WORK_HASHED | (bit << IO_WQ_HASH_SHIFT), &work->flags);
1085ed29b0b4SJens Axboe }
1086ed29b0b4SJens Axboe
__io_wq_worker_cancel(struct io_worker * worker,struct io_cb_cancel_data * match,struct io_wq_work * work)1087ed29b0b4SJens Axboe static bool __io_wq_worker_cancel(struct io_worker *worker,
1088ed29b0b4SJens Axboe struct io_cb_cancel_data *match,
1089ed29b0b4SJens Axboe struct io_wq_work *work)
1090ed29b0b4SJens Axboe {
1091ed29b0b4SJens Axboe if (work && match->fn(work, match->data)) {
10923474d1b9SJens Axboe atomic_or(IO_WQ_WORK_CANCEL, &work->flags);
1093ed29b0b4SJens Axboe __set_notify_signal(worker->task);
1094ed29b0b4SJens Axboe return true;
1095ed29b0b4SJens Axboe }
1096ed29b0b4SJens Axboe
1097ed29b0b4SJens Axboe return false;
1098ed29b0b4SJens Axboe }
1099ed29b0b4SJens Axboe
io_wq_worker_cancel(struct io_worker * worker,void * data)1100ed29b0b4SJens Axboe static bool io_wq_worker_cancel(struct io_worker *worker, void *data)
1101ed29b0b4SJens Axboe {
1102ed29b0b4SJens Axboe struct io_cb_cancel_data *match = data;
1103ed29b0b4SJens Axboe
1104ed29b0b4SJens Axboe /*
1105ed29b0b4SJens Axboe * Hold the lock to avoid ->cur_work going out of scope, caller
1106ed29b0b4SJens Axboe * may dereference the passed in work.
1107ed29b0b4SJens Axboe */
1108ed29b0b4SJens Axboe raw_spin_lock(&worker->lock);
110924c3fc5cSGabriel Krisman Bertazi if (__io_wq_worker_cancel(worker, match, worker->cur_work))
1110ed29b0b4SJens Axboe match->nr_running++;
1111ed29b0b4SJens Axboe raw_spin_unlock(&worker->lock);
1112ed29b0b4SJens Axboe
1113ed29b0b4SJens Axboe return match->nr_running && !match->cancel_all;
1114ed29b0b4SJens Axboe }
1115ed29b0b4SJens Axboe
io_wq_remove_pending(struct io_wq * wq,struct io_wq_acct * acct,struct io_wq_work * work,struct io_wq_work_node * prev)1116eb47943fSGabriel Krisman Bertazi static inline void io_wq_remove_pending(struct io_wq *wq,
11173c75635fSMax Kellermann struct io_wq_acct *acct,
1118ed29b0b4SJens Axboe struct io_wq_work *work,
1119ed29b0b4SJens Axboe struct io_wq_work_node *prev)
1120ed29b0b4SJens Axboe {
1121ed29b0b4SJens Axboe unsigned int hash = io_get_work_hash(work);
1122ed29b0b4SJens Axboe struct io_wq_work *prev_work = NULL;
1123ed29b0b4SJens Axboe
1124eb47943fSGabriel Krisman Bertazi if (io_wq_is_hashed(work) && work == wq->hash_tail[hash]) {
1125ed29b0b4SJens Axboe if (prev)
1126ed29b0b4SJens Axboe prev_work = container_of(prev, struct io_wq_work, list);
1127ed29b0b4SJens Axboe if (prev_work && io_get_work_hash(prev_work) == hash)
1128eb47943fSGabriel Krisman Bertazi wq->hash_tail[hash] = prev_work;
1129ed29b0b4SJens Axboe else
1130eb47943fSGabriel Krisman Bertazi wq->hash_tail[hash] = NULL;
1131ed29b0b4SJens Axboe }
1132ed29b0b4SJens Axboe wq_list_del(&acct->work_list, &work->list, prev);
1133ed29b0b4SJens Axboe }
1134ed29b0b4SJens Axboe
io_acct_cancel_pending_work(struct io_wq * wq,struct io_wq_acct * acct,struct io_cb_cancel_data * match)1135eb47943fSGabriel Krisman Bertazi static bool io_acct_cancel_pending_work(struct io_wq *wq,
1136dfd63bafSGabriel Krisman Bertazi struct io_wq_acct *acct,
1137ed29b0b4SJens Axboe struct io_cb_cancel_data *match)
1138ed29b0b4SJens Axboe {
1139ed29b0b4SJens Axboe struct io_wq_work_node *node, *prev;
1140ed29b0b4SJens Axboe struct io_wq_work *work;
1141ed29b0b4SJens Axboe
1142ed29b0b4SJens Axboe raw_spin_lock(&acct->lock);
1143ed29b0b4SJens Axboe wq_list_for_each(node, prev, &acct->work_list) {
1144ed29b0b4SJens Axboe work = container_of(node, struct io_wq_work, list);
1145ed29b0b4SJens Axboe if (!match->fn(work, match->data))
1146ed29b0b4SJens Axboe continue;
11473c75635fSMax Kellermann io_wq_remove_pending(wq, acct, work, prev);
1148ed29b0b4SJens Axboe raw_spin_unlock(&acct->lock);
1149eb47943fSGabriel Krisman Bertazi io_run_cancel(work, wq);
1150ed29b0b4SJens Axboe match->nr_pending++;
1151ed29b0b4SJens Axboe /* not safe to continue after unlock */
1152ed29b0b4SJens Axboe return true;
1153ed29b0b4SJens Axboe }
1154ed29b0b4SJens Axboe raw_spin_unlock(&acct->lock);
1155ed29b0b4SJens Axboe
1156ed29b0b4SJens Axboe return false;
1157ed29b0b4SJens Axboe }
1158ed29b0b4SJens Axboe
io_wq_cancel_pending_work(struct io_wq * wq,struct io_cb_cancel_data * match)1159eb47943fSGabriel Krisman Bertazi static void io_wq_cancel_pending_work(struct io_wq *wq,
1160ed29b0b4SJens Axboe struct io_cb_cancel_data *match)
1161ed29b0b4SJens Axboe {
1162ed29b0b4SJens Axboe int i;
1163ed29b0b4SJens Axboe retry:
1164ed29b0b4SJens Axboe for (i = 0; i < IO_WQ_ACCT_NR; i++) {
1165eb47943fSGabriel Krisman Bertazi struct io_wq_acct *acct = io_get_acct(wq, i == 0);
1166ed29b0b4SJens Axboe
1167eb47943fSGabriel Krisman Bertazi if (io_acct_cancel_pending_work(wq, acct, match)) {
1168ed29b0b4SJens Axboe if (match->cancel_all)
1169ed29b0b4SJens Axboe goto retry;
1170ed29b0b4SJens Axboe break;
1171ed29b0b4SJens Axboe }
1172ed29b0b4SJens Axboe }
1173ed29b0b4SJens Axboe }
1174ed29b0b4SJens Axboe
io_acct_cancel_running_work(struct io_wq_acct * acct,struct io_cb_cancel_data * match)1175751eedc4SMax Kellermann static void io_acct_cancel_running_work(struct io_wq_acct *acct,
1176751eedc4SMax Kellermann struct io_cb_cancel_data *match)
1177751eedc4SMax Kellermann {
1178751eedc4SMax Kellermann raw_spin_lock(&acct->workers_lock);
1179751eedc4SMax Kellermann io_acct_for_each_worker(acct, io_wq_worker_cancel, match);
1180751eedc4SMax Kellermann raw_spin_unlock(&acct->workers_lock);
1181751eedc4SMax Kellermann }
1182751eedc4SMax Kellermann
io_wq_cancel_running_work(struct io_wq * wq,struct io_cb_cancel_data * match)1183eb47943fSGabriel Krisman Bertazi static void io_wq_cancel_running_work(struct io_wq *wq,
1184ed29b0b4SJens Axboe struct io_cb_cancel_data *match)
1185ed29b0b4SJens Axboe {
1186ed29b0b4SJens Axboe rcu_read_lock();
1187751eedc4SMax Kellermann
1188751eedc4SMax Kellermann for (int i = 0; i < IO_WQ_ACCT_NR; i++)
1189751eedc4SMax Kellermann io_acct_cancel_running_work(&wq->acct[i], match);
1190751eedc4SMax Kellermann
1191ed29b0b4SJens Axboe rcu_read_unlock();
1192ed29b0b4SJens Axboe }
1193ed29b0b4SJens Axboe
io_wq_cancel_cb(struct io_wq * wq,work_cancel_fn * cancel,void * data,bool cancel_all)1194ed29b0b4SJens Axboe enum io_wq_cancel io_wq_cancel_cb(struct io_wq *wq, work_cancel_fn *cancel,
1195ed29b0b4SJens Axboe void *data, bool cancel_all)
1196ed29b0b4SJens Axboe {
1197ed29b0b4SJens Axboe struct io_cb_cancel_data match = {
1198ed29b0b4SJens Axboe .fn = cancel,
1199ed29b0b4SJens Axboe .data = data,
1200ed29b0b4SJens Axboe .cancel_all = cancel_all,
1201ed29b0b4SJens Axboe };
1202ed29b0b4SJens Axboe
1203ed29b0b4SJens Axboe /*
1204ed29b0b4SJens Axboe * First check pending list, if we're lucky we can just remove it
1205ed29b0b4SJens Axboe * from there. CANCEL_OK means that the work is returned as-new,
1206ed29b0b4SJens Axboe * no completion will be posted for it.
1207ed29b0b4SJens Axboe *
1208ed29b0b4SJens Axboe * Then check if a free (going busy) or busy worker has the work
1209ed29b0b4SJens Axboe * currently running. If we find it there, we'll return CANCEL_RUNNING
1210ed29b0b4SJens Axboe * as an indication that we attempt to signal cancellation. The
1211ed29b0b4SJens Axboe * completion will run normally in this case.
1212ed29b0b4SJens Axboe *
1213751eedc4SMax Kellermann * Do both of these while holding the acct->workers_lock, to ensure that
1214ed29b0b4SJens Axboe * we'll find a work item regardless of state.
1215ed29b0b4SJens Axboe */
1216eb47943fSGabriel Krisman Bertazi io_wq_cancel_pending_work(wq, &match);
1217ed29b0b4SJens Axboe if (match.nr_pending && !match.cancel_all)
1218ed29b0b4SJens Axboe return IO_WQ_CANCEL_OK;
1219ed29b0b4SJens Axboe
1220eb47943fSGabriel Krisman Bertazi io_wq_cancel_running_work(wq, &match);
1221ed29b0b4SJens Axboe if (match.nr_running && !match.cancel_all)
1222ed29b0b4SJens Axboe return IO_WQ_CANCEL_RUNNING;
1223ed29b0b4SJens Axboe
1224ed29b0b4SJens Axboe if (match.nr_running)
1225ed29b0b4SJens Axboe return IO_WQ_CANCEL_RUNNING;
1226ed29b0b4SJens Axboe if (match.nr_pending)
1227ed29b0b4SJens Axboe return IO_WQ_CANCEL_OK;
1228ed29b0b4SJens Axboe return IO_WQ_CANCEL_NOTFOUND;
1229ed29b0b4SJens Axboe }
1230ed29b0b4SJens Axboe
io_wq_hash_wake(struct wait_queue_entry * wait,unsigned mode,int sync,void * key)1231eb47943fSGabriel Krisman Bertazi static int io_wq_hash_wake(struct wait_queue_entry *wait, unsigned mode,
1232ed29b0b4SJens Axboe int sync, void *key)
1233ed29b0b4SJens Axboe {
1234eb47943fSGabriel Krisman Bertazi struct io_wq *wq = container_of(wait, struct io_wq, wait);
1235ed29b0b4SJens Axboe int i;
1236ed29b0b4SJens Axboe
1237ed29b0b4SJens Axboe list_del_init(&wait->entry);
1238ed29b0b4SJens Axboe
1239ed29b0b4SJens Axboe rcu_read_lock();
1240ed29b0b4SJens Axboe for (i = 0; i < IO_WQ_ACCT_NR; i++) {
1241dfd63bafSGabriel Krisman Bertazi struct io_wq_acct *acct = &wq->acct[i];
1242ed29b0b4SJens Axboe
1243ed29b0b4SJens Axboe if (test_and_clear_bit(IO_ACCT_STALLED_BIT, &acct->flags))
1244751eedc4SMax Kellermann io_acct_activate_free_worker(acct);
1245ed29b0b4SJens Axboe }
1246ed29b0b4SJens Axboe rcu_read_unlock();
1247ed29b0b4SJens Axboe return 1;
1248ed29b0b4SJens Axboe }
1249ed29b0b4SJens Axboe
io_wq_create(unsigned bounded,struct io_wq_data * data)1250ed29b0b4SJens Axboe struct io_wq *io_wq_create(unsigned bounded, struct io_wq_data *data)
1251ed29b0b4SJens Axboe {
1252da64d6dbSBreno Leitao int ret, i;
1253ed29b0b4SJens Axboe struct io_wq *wq;
1254ed29b0b4SJens Axboe
1255ed29b0b4SJens Axboe if (WARN_ON_ONCE(!bounded))
1256ed29b0b4SJens Axboe return ERR_PTR(-EINVAL);
1257ed29b0b4SJens Axboe
1258*bf4afc53SLinus Torvalds wq = kzalloc_obj(struct io_wq);
1259ed29b0b4SJens Axboe if (!wq)
1260ed29b0b4SJens Axboe return ERR_PTR(-ENOMEM);
1261ed29b0b4SJens Axboe
1262ed29b0b4SJens Axboe refcount_inc(&data->hash->refs);
1263ed29b0b4SJens Axboe wq->hash = data->hash;
1264ed29b0b4SJens Axboe
1265ed29b0b4SJens Axboe ret = -ENOMEM;
1266ed29b0b4SJens Axboe
1267eb47943fSGabriel Krisman Bertazi if (!alloc_cpumask_var(&wq->cpu_mask, GFP_KERNEL))
1268ed29b0b4SJens Axboe goto err;
126984eacf17SFelix Moessbauer cpuset_cpus_allowed(data->task, wq->cpu_mask);
1270dfd63bafSGabriel Krisman Bertazi wq->acct[IO_WQ_ACCT_BOUND].max_workers = bounded;
1271dfd63bafSGabriel Krisman Bertazi wq->acct[IO_WQ_ACCT_UNBOUND].max_workers =
1272ed29b0b4SJens Axboe task_rlimit(current, RLIMIT_NPROC);
1273eb47943fSGabriel Krisman Bertazi INIT_LIST_HEAD(&wq->wait.entry);
1274eb47943fSGabriel Krisman Bertazi wq->wait.func = io_wq_hash_wake;
1275ed29b0b4SJens Axboe for (i = 0; i < IO_WQ_ACCT_NR; i++) {
1276dfd63bafSGabriel Krisman Bertazi struct io_wq_acct *acct = &wq->acct[i];
1277ed29b0b4SJens Axboe
1278ed29b0b4SJens Axboe atomic_set(&acct->nr_running, 0);
1279751eedc4SMax Kellermann
1280751eedc4SMax Kellermann raw_spin_lock_init(&acct->workers_lock);
1281751eedc4SMax Kellermann INIT_HLIST_NULLS_HEAD(&acct->free_list, 0);
1282751eedc4SMax Kellermann INIT_LIST_HEAD(&acct->all_list);
1283751eedc4SMax Kellermann
1284ed29b0b4SJens Axboe INIT_WQ_LIST(&acct->work_list);
1285ed29b0b4SJens Axboe raw_spin_lock_init(&acct->lock);
1286ed29b0b4SJens Axboe }
1287eb47943fSGabriel Krisman Bertazi
1288ed29b0b4SJens Axboe wq->task = get_task_struct(data->task);
1289ed29b0b4SJens Axboe atomic_set(&wq->worker_refs, 1);
1290ed29b0b4SJens Axboe init_completion(&wq->worker_done);
12910f8baa3cSJeff Moyer ret = cpuhp_state_add_instance_nocalls(io_wq_online, &wq->cpuhp_node);
129289465d92SPenglei Jiang if (ret) {
129389465d92SPenglei Jiang put_task_struct(wq->task);
12940f8baa3cSJeff Moyer goto err;
129589465d92SPenglei Jiang }
12960f8baa3cSJeff Moyer
1297ed29b0b4SJens Axboe return wq;
1298ed29b0b4SJens Axboe err:
1299ed29b0b4SJens Axboe io_wq_put_hash(data->hash);
1300eb47943fSGabriel Krisman Bertazi free_cpumask_var(wq->cpu_mask);
1301ed29b0b4SJens Axboe kfree(wq);
1302ed29b0b4SJens Axboe return ERR_PTR(ret);
1303ed29b0b4SJens Axboe }
1304ed29b0b4SJens Axboe
io_task_work_match(struct callback_head * cb,void * data)1305ed29b0b4SJens Axboe static bool io_task_work_match(struct callback_head *cb, void *data)
1306ed29b0b4SJens Axboe {
1307ed29b0b4SJens Axboe struct io_worker *worker;
1308ed29b0b4SJens Axboe
1309ed29b0b4SJens Axboe if (cb->func != create_worker_cb && cb->func != create_worker_cont)
1310ed29b0b4SJens Axboe return false;
1311ed29b0b4SJens Axboe worker = container_of(cb, struct io_worker, create_work);
1312eb47943fSGabriel Krisman Bertazi return worker->wq == data;
1313ed29b0b4SJens Axboe }
1314ed29b0b4SJens Axboe
io_wq_exit_start(struct io_wq * wq)1315ed29b0b4SJens Axboe void io_wq_exit_start(struct io_wq *wq)
1316ed29b0b4SJens Axboe {
1317ed29b0b4SJens Axboe set_bit(IO_WQ_BIT_EXIT, &wq->state);
1318ed29b0b4SJens Axboe }
1319ed29b0b4SJens Axboe
io_wq_cancel_tw_create(struct io_wq * wq)1320ed29b0b4SJens Axboe static void io_wq_cancel_tw_create(struct io_wq *wq)
1321ed29b0b4SJens Axboe {
1322ed29b0b4SJens Axboe struct callback_head *cb;
1323ed29b0b4SJens Axboe
1324ed29b0b4SJens Axboe while ((cb = task_work_cancel_match(wq->task, io_task_work_match, wq)) != NULL) {
1325ed29b0b4SJens Axboe struct io_worker *worker;
1326ed29b0b4SJens Axboe
1327ed29b0b4SJens Axboe worker = container_of(cb, struct io_worker, create_work);
1328ed29b0b4SJens Axboe io_worker_cancel_cb(worker);
1329e6db6f93SJens Axboe /*
1330e6db6f93SJens Axboe * Only the worker continuation helper has worker allocated and
1331e6db6f93SJens Axboe * hence needs freeing.
1332e6db6f93SJens Axboe */
1333e6db6f93SJens Axboe if (cb->func == create_worker_cont)
1334af82425cSJens Axboe kfree(worker);
1335ed29b0b4SJens Axboe }
1336ed29b0b4SJens Axboe }
1337ed29b0b4SJens Axboe
io_wq_exit_workers(struct io_wq * wq)1338ed29b0b4SJens Axboe static void io_wq_exit_workers(struct io_wq *wq)
1339ed29b0b4SJens Axboe {
13401f293098SJens Axboe unsigned long timeout, warn_timeout;
13411f293098SJens Axboe
1342ed29b0b4SJens Axboe if (!wq->task)
1343ed29b0b4SJens Axboe return;
1344ed29b0b4SJens Axboe
1345ed29b0b4SJens Axboe io_wq_cancel_tw_create(wq);
1346ed29b0b4SJens Axboe
1347ed29b0b4SJens Axboe rcu_read_lock();
1348eb47943fSGabriel Krisman Bertazi io_wq_for_each_worker(wq, io_wq_worker_wake, NULL);
1349ed29b0b4SJens Axboe rcu_read_unlock();
1350ed29b0b4SJens Axboe io_worker_ref_put(wq);
13511f293098SJens Axboe
13521f293098SJens Axboe /*
13531f293098SJens Axboe * Shut up hung task complaint, see for example
13541f293098SJens Axboe *
13551f293098SJens Axboe * https://lore.kernel.org/all/696fc9e7.a70a0220.111c58.0006.GAE@google.com/
13561f293098SJens Axboe *
13571f293098SJens Axboe * where completely overloading the system with tons of long running
13581f293098SJens Axboe * io-wq items can easily trigger the hung task timeout. Only sleep
13591f293098SJens Axboe * uninterruptibly for half that time, and warn if we exceeded end
13601f293098SJens Axboe * up waiting more than IO_URING_EXIT_WAIT_MAX.
13611f293098SJens Axboe */
13621f293098SJens Axboe timeout = sysctl_hung_task_timeout_secs * HZ / 2;
136381609589SJens Axboe if (!timeout)
136481609589SJens Axboe timeout = MAX_SCHEDULE_TIMEOUT;
13651f293098SJens Axboe warn_timeout = jiffies + IO_URING_EXIT_WAIT_MAX;
13661f293098SJens Axboe do {
13671f293098SJens Axboe if (wait_for_completion_timeout(&wq->worker_done, timeout))
13681f293098SJens Axboe break;
13691f293098SJens Axboe WARN_ON_ONCE(time_after(jiffies, warn_timeout));
13701f293098SJens Axboe } while (1);
1371ed29b0b4SJens Axboe
1372ed29b0b4SJens Axboe spin_lock_irq(&wq->hash->wait.lock);
1373eb47943fSGabriel Krisman Bertazi list_del_init(&wq->wait.entry);
1374ed29b0b4SJens Axboe spin_unlock_irq(&wq->hash->wait.lock);
1375da64d6dbSBreno Leitao
1376ed29b0b4SJens Axboe put_task_struct(wq->task);
1377ed29b0b4SJens Axboe wq->task = NULL;
1378ed29b0b4SJens Axboe }
1379ed29b0b4SJens Axboe
io_wq_destroy(struct io_wq * wq)1380ed29b0b4SJens Axboe static void io_wq_destroy(struct io_wq *wq)
1381ed29b0b4SJens Axboe {
1382ed29b0b4SJens Axboe struct io_cb_cancel_data match = {
1383ed29b0b4SJens Axboe .fn = io_wq_work_match_all,
1384ed29b0b4SJens Axboe .cancel_all = true,
1385ed29b0b4SJens Axboe };
1386da64d6dbSBreno Leitao
1387da64d6dbSBreno Leitao cpuhp_state_remove_instance_nocalls(io_wq_online, &wq->cpuhp_node);
1388eb47943fSGabriel Krisman Bertazi io_wq_cancel_pending_work(wq, &match);
1389eb47943fSGabriel Krisman Bertazi free_cpumask_var(wq->cpu_mask);
1390ed29b0b4SJens Axboe io_wq_put_hash(wq->hash);
1391ed29b0b4SJens Axboe kfree(wq);
1392ed29b0b4SJens Axboe }
1393ed29b0b4SJens Axboe
io_wq_put_and_exit(struct io_wq * wq)1394ed29b0b4SJens Axboe void io_wq_put_and_exit(struct io_wq *wq)
1395ed29b0b4SJens Axboe {
1396ed29b0b4SJens Axboe WARN_ON_ONCE(!test_bit(IO_WQ_BIT_EXIT, &wq->state));
1397ed29b0b4SJens Axboe
1398ed29b0b4SJens Axboe io_wq_exit_workers(wq);
1399ed29b0b4SJens Axboe io_wq_destroy(wq);
1400ed29b0b4SJens Axboe }
1401ed29b0b4SJens Axboe
1402ed29b0b4SJens Axboe struct online_data {
1403ed29b0b4SJens Axboe unsigned int cpu;
1404ed29b0b4SJens Axboe bool online;
1405ed29b0b4SJens Axboe };
1406ed29b0b4SJens Axboe
io_wq_worker_affinity(struct io_worker * worker,void * data)1407ed29b0b4SJens Axboe static bool io_wq_worker_affinity(struct io_worker *worker, void *data)
1408ed29b0b4SJens Axboe {
1409ed29b0b4SJens Axboe struct online_data *od = data;
1410ed29b0b4SJens Axboe
1411ed29b0b4SJens Axboe if (od->online)
1412eb47943fSGabriel Krisman Bertazi cpumask_set_cpu(od->cpu, worker->wq->cpu_mask);
1413ed29b0b4SJens Axboe else
1414eb47943fSGabriel Krisman Bertazi cpumask_clear_cpu(od->cpu, worker->wq->cpu_mask);
1415ed29b0b4SJens Axboe return false;
1416ed29b0b4SJens Axboe }
1417ed29b0b4SJens Axboe
__io_wq_cpu_online(struct io_wq * wq,unsigned int cpu,bool online)1418ed29b0b4SJens Axboe static int __io_wq_cpu_online(struct io_wq *wq, unsigned int cpu, bool online)
1419ed29b0b4SJens Axboe {
1420ed29b0b4SJens Axboe struct online_data od = {
1421ed29b0b4SJens Axboe .cpu = cpu,
1422ed29b0b4SJens Axboe .online = online
1423ed29b0b4SJens Axboe };
1424ed29b0b4SJens Axboe
1425ed29b0b4SJens Axboe rcu_read_lock();
1426eb47943fSGabriel Krisman Bertazi io_wq_for_each_worker(wq, io_wq_worker_affinity, &od);
1427ed29b0b4SJens Axboe rcu_read_unlock();
1428ed29b0b4SJens Axboe return 0;
1429ed29b0b4SJens Axboe }
1430ed29b0b4SJens Axboe
io_wq_cpu_online(unsigned int cpu,struct hlist_node * node)1431ed29b0b4SJens Axboe static int io_wq_cpu_online(unsigned int cpu, struct hlist_node *node)
1432ed29b0b4SJens Axboe {
1433ed29b0b4SJens Axboe struct io_wq *wq = hlist_entry_safe(node, struct io_wq, cpuhp_node);
1434ed29b0b4SJens Axboe
1435ed29b0b4SJens Axboe return __io_wq_cpu_online(wq, cpu, true);
1436ed29b0b4SJens Axboe }
1437ed29b0b4SJens Axboe
io_wq_cpu_offline(unsigned int cpu,struct hlist_node * node)1438ed29b0b4SJens Axboe static int io_wq_cpu_offline(unsigned int cpu, struct hlist_node *node)
1439ed29b0b4SJens Axboe {
1440ed29b0b4SJens Axboe struct io_wq *wq = hlist_entry_safe(node, struct io_wq, cpuhp_node);
1441ed29b0b4SJens Axboe
1442ed29b0b4SJens Axboe return __io_wq_cpu_online(wq, cpu, false);
1443ed29b0b4SJens Axboe }
1444ed29b0b4SJens Axboe
io_wq_cpu_affinity(struct io_uring_task * tctx,cpumask_var_t mask)1445ebdfefc0SJens Axboe int io_wq_cpu_affinity(struct io_uring_task *tctx, cpumask_var_t mask)
1446ed29b0b4SJens Axboe {
14470997aa54SFelix Moessbauer cpumask_var_t allowed_mask;
14480997aa54SFelix Moessbauer int ret = 0;
14490997aa54SFelix Moessbauer
1450ebdfefc0SJens Axboe if (!tctx || !tctx->io_wq)
1451ebdfefc0SJens Axboe return -EINVAL;
1452ebdfefc0SJens Axboe
14530997aa54SFelix Moessbauer if (!alloc_cpumask_var(&allowed_mask, GFP_KERNEL))
14540997aa54SFelix Moessbauer return -ENOMEM;
14550997aa54SFelix Moessbauer
1456ed29b0b4SJens Axboe rcu_read_lock();
14570997aa54SFelix Moessbauer cpuset_cpus_allowed(tctx->io_wq->task, allowed_mask);
14580997aa54SFelix Moessbauer if (mask) {
14590997aa54SFelix Moessbauer if (cpumask_subset(mask, allowed_mask))
1460ebdfefc0SJens Axboe cpumask_copy(tctx->io_wq->cpu_mask, mask);
1461ed29b0b4SJens Axboe else
14620997aa54SFelix Moessbauer ret = -EINVAL;
14630997aa54SFelix Moessbauer } else {
14640997aa54SFelix Moessbauer cpumask_copy(tctx->io_wq->cpu_mask, allowed_mask);
14650997aa54SFelix Moessbauer }
1466ed29b0b4SJens Axboe rcu_read_unlock();
1467da64d6dbSBreno Leitao
14680997aa54SFelix Moessbauer free_cpumask_var(allowed_mask);
14690997aa54SFelix Moessbauer return ret;
1470ed29b0b4SJens Axboe }
1471ed29b0b4SJens Axboe
1472ed29b0b4SJens Axboe /*
1473ed29b0b4SJens Axboe * Set max number of unbounded workers, returns old value. If new_count is 0,
1474ed29b0b4SJens Axboe * then just return the old value.
1475ed29b0b4SJens Axboe */
io_wq_max_workers(struct io_wq * wq,int * new_count)1476ed29b0b4SJens Axboe int io_wq_max_workers(struct io_wq *wq, int *new_count)
1477ed29b0b4SJens Axboe {
1478dfd63bafSGabriel Krisman Bertazi struct io_wq_acct *acct;
1479ed29b0b4SJens Axboe int prev[IO_WQ_ACCT_NR];
1480da64d6dbSBreno Leitao int i;
1481ed29b0b4SJens Axboe
1482ed29b0b4SJens Axboe BUILD_BUG_ON((int) IO_WQ_ACCT_BOUND != (int) IO_WQ_BOUND);
1483ed29b0b4SJens Axboe BUILD_BUG_ON((int) IO_WQ_ACCT_UNBOUND != (int) IO_WQ_UNBOUND);
1484ed29b0b4SJens Axboe BUILD_BUG_ON((int) IO_WQ_ACCT_NR != 2);
1485ed29b0b4SJens Axboe
1486ed29b0b4SJens Axboe for (i = 0; i < IO_WQ_ACCT_NR; i++) {
1487ed29b0b4SJens Axboe if (new_count[i] > task_rlimit(current, RLIMIT_NPROC))
1488ed29b0b4SJens Axboe new_count[i] = task_rlimit(current, RLIMIT_NPROC);
1489ed29b0b4SJens Axboe }
1490ed29b0b4SJens Axboe
1491ed29b0b4SJens Axboe for (i = 0; i < IO_WQ_ACCT_NR; i++)
1492ed29b0b4SJens Axboe prev[i] = 0;
1493ed29b0b4SJens Axboe
1494ed29b0b4SJens Axboe rcu_read_lock();
1495ed29b0b4SJens Axboe
1496ed29b0b4SJens Axboe for (i = 0; i < IO_WQ_ACCT_NR; i++) {
1497dfd63bafSGabriel Krisman Bertazi acct = &wq->acct[i];
1498751eedc4SMax Kellermann raw_spin_lock(&acct->workers_lock);
1499ed29b0b4SJens Axboe prev[i] = max_t(int, acct->max_workers, prev[i]);
1500ed29b0b4SJens Axboe if (new_count[i])
1501ed29b0b4SJens Axboe acct->max_workers = new_count[i];
1502751eedc4SMax Kellermann raw_spin_unlock(&acct->workers_lock);
1503ed29b0b4SJens Axboe }
1504ed29b0b4SJens Axboe rcu_read_unlock();
1505ed29b0b4SJens Axboe
1506ed29b0b4SJens Axboe for (i = 0; i < IO_WQ_ACCT_NR; i++)
1507ed29b0b4SJens Axboe new_count[i] = prev[i];
1508ed29b0b4SJens Axboe
1509ed29b0b4SJens Axboe return 0;
1510ed29b0b4SJens Axboe }
1511ed29b0b4SJens Axboe
io_wq_init(void)1512ed29b0b4SJens Axboe static __init int io_wq_init(void)
1513ed29b0b4SJens Axboe {
1514ed29b0b4SJens Axboe int ret;
1515ed29b0b4SJens Axboe
1516ed29b0b4SJens Axboe ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "io-wq/online",
1517ed29b0b4SJens Axboe io_wq_cpu_online, io_wq_cpu_offline);
1518ed29b0b4SJens Axboe if (ret < 0)
1519ed29b0b4SJens Axboe return ret;
1520ed29b0b4SJens Axboe io_wq_online = ret;
1521ed29b0b4SJens Axboe return 0;
1522ed29b0b4SJens Axboe }
1523ed29b0b4SJens Axboe subsys_initcall(io_wq_init);
1524