blob: b5ae8080a41eefa332f189f52c87025e39524f5b [file] [log] [blame]
Jens Axboe771b53d02019-10-22 10:25:58 -06001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Basic worker thread pool for io_uring
4 *
5 * Copyright (C) 2019 Jens Axboe
6 *
7 */
8#include <linux/kernel.h>
9#include <linux/init.h>
10#include <linux/errno.h>
11#include <linux/sched/signal.h>
12#include <linux/mm.h>
Jens Axboe771b53d02019-10-22 10:25:58 -060013#include <linux/sched/mm.h>
14#include <linux/percpu.h>
15#include <linux/slab.h>
Jens Axboe771b53d02019-10-22 10:25:58 -060016#include <linux/rculist_nulls.h>
Jens Axboe43c01fb2020-10-22 09:02:50 -060017#include <linux/cpu.h>
Jens Axboe3bfe6102021-02-16 14:15:30 -070018#include <linux/tracehook.h>
Jens Axboe771b53d02019-10-22 10:25:58 -060019
Jens Axboe43c01fb2020-10-22 09:02:50 -060020#include "../kernel/sched/sched.h"
Jens Axboe771b53d02019-10-22 10:25:58 -060021#include "io-wq.h"
22
23#define WORKER_IDLE_TIMEOUT (5 * HZ)
24
25enum {
26 IO_WORKER_F_UP = 1, /* up and active */
27 IO_WORKER_F_RUNNING = 2, /* account as running */
28 IO_WORKER_F_FREE = 4, /* worker on free list */
Jens Axboe145cc8c2020-09-26 12:37:46 -060029 IO_WORKER_F_FIXED = 8, /* static idle worker */
30 IO_WORKER_F_BOUND = 16, /* is doing bounded work */
Jens Axboe771b53d02019-10-22 10:25:58 -060031};
32
33enum {
34 IO_WQ_BIT_EXIT = 0, /* wq exiting */
Jens Axboe446bc1c2020-12-20 10:47:42 -070035 IO_WQ_BIT_ERROR = 1, /* error on setup */
Jens Axboe771b53d02019-10-22 10:25:58 -060036};
37
38enum {
39 IO_WQE_FLAG_STALLED = 1, /* stalled on hash */
40};
41
42/*
43 * One for each thread in a wqe pool
44 */
45struct io_worker {
46 refcount_t ref;
47 unsigned flags;
48 struct hlist_nulls_node nulls_node;
Jens Axboee61df662019-11-13 13:54:49 -070049 struct list_head all_list;
Jens Axboe771b53d02019-10-22 10:25:58 -060050 struct task_struct *task;
Jens Axboe771b53d02019-10-22 10:25:58 -060051 struct io_wqe *wqe;
Jens Axboe36c2f922019-11-13 09:43:34 -070052
Jens Axboe771b53d02019-10-22 10:25:58 -060053 struct io_wq_work *cur_work;
Jens Axboe36c2f922019-11-13 09:43:34 -070054 spinlock_t lock;
Jens Axboe771b53d02019-10-22 10:25:58 -060055
Jens Axboe4379bf82021-02-15 13:40:22 -070056 const struct cred *cur_creds;
57 const struct cred *saved_creds;
58
Jens Axboe771b53d02019-10-22 10:25:58 -060059 struct rcu_head rcu;
Jens Axboe771b53d02019-10-22 10:25:58 -060060};
61
Jens Axboe771b53d02019-10-22 10:25:58 -060062#if BITS_PER_LONG == 64
63#define IO_WQ_HASH_ORDER 6
64#else
65#define IO_WQ_HASH_ORDER 5
66#endif
67
Pavel Begunkov86f3cd12020-03-23 22:57:22 +030068#define IO_WQ_NR_HASH_BUCKETS (1u << IO_WQ_HASH_ORDER)
69
Jens Axboec5def4a2019-11-07 11:41:16 -070070struct io_wqe_acct {
71 unsigned nr_workers;
72 unsigned max_workers;
73 atomic_t nr_running;
74};
75
76enum {
77 IO_WQ_ACCT_BOUND,
78 IO_WQ_ACCT_UNBOUND,
79};
80
Jens Axboe771b53d02019-10-22 10:25:58 -060081/*
82 * Per-node worker thread pool
83 */
84struct io_wqe {
85 struct {
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +020086 raw_spinlock_t lock;
Jens Axboe6206f0e2019-11-26 11:59:32 -070087 struct io_wq_work_list work_list;
Jens Axboe771b53d02019-10-22 10:25:58 -060088 unsigned long hash_map;
89 unsigned flags;
90 } ____cacheline_aligned_in_smp;
91
92 int node;
Jens Axboec5def4a2019-11-07 11:41:16 -070093 struct io_wqe_acct acct[2];
Jens Axboe771b53d02019-10-22 10:25:58 -060094
Jens Axboe021d1cd2019-11-14 08:00:41 -070095 struct hlist_nulls_head free_list;
Jens Axboee61df662019-11-13 13:54:49 -070096 struct list_head all_list;
Jens Axboe771b53d02019-10-22 10:25:58 -060097
98 struct io_wq *wq;
Pavel Begunkov86f3cd12020-03-23 22:57:22 +030099 struct io_wq_work *hash_tail[IO_WQ_NR_HASH_BUCKETS];
Jens Axboe771b53d02019-10-22 10:25:58 -0600100};
101
102/*
103 * Per io_wq state
104 */
105struct io_wq {
106 struct io_wqe **wqes;
107 unsigned long state;
Jens Axboe771b53d02019-10-22 10:25:58 -0600108
Pavel Begunkove9fd9392020-03-04 16:14:12 +0300109 free_work_fn *free_work;
Pavel Begunkovf5fa38c2020-06-08 21:08:20 +0300110 io_wq_work_fn *do_work;
Jens Axboe7d723062019-11-12 22:31:31 -0700111
Jens Axboe771b53d02019-10-22 10:25:58 -0600112 struct task_struct *manager;
Jens Axboec5def4a2019-11-07 11:41:16 -0700113 struct user_struct *user;
Jens Axboe771b53d02019-10-22 10:25:58 -0600114 refcount_t refs;
115 struct completion done;
Jens Axboe848f7e12020-01-23 15:33:32 -0700116
Jens Axboe43c01fb2020-10-22 09:02:50 -0600117 struct hlist_node cpuhp_node;
Jens Axboe3bfe6102021-02-16 14:15:30 -0700118
119 pid_t task_pid;
Jens Axboe771b53d02019-10-22 10:25:58 -0600120};
121
Jens Axboe43c01fb2020-10-22 09:02:50 -0600122static enum cpuhp_state io_wq_online;
123
Jens Axboe771b53d02019-10-22 10:25:58 -0600124static bool io_worker_get(struct io_worker *worker)
125{
126 return refcount_inc_not_zero(&worker->ref);
127}
128
129static void io_worker_release(struct io_worker *worker)
130{
131 if (refcount_dec_and_test(&worker->ref))
132 wake_up_process(worker->task);
133}
134
Jens Axboec5def4a2019-11-07 11:41:16 -0700135static inline struct io_wqe_acct *io_work_get_acct(struct io_wqe *wqe,
136 struct io_wq_work *work)
137{
138 if (work->flags & IO_WQ_WORK_UNBOUND)
139 return &wqe->acct[IO_WQ_ACCT_UNBOUND];
140
141 return &wqe->acct[IO_WQ_ACCT_BOUND];
142}
143
Jens Axboe958234d2021-02-17 09:00:57 -0700144static inline struct io_wqe_acct *io_wqe_get_acct(struct io_worker *worker)
Jens Axboec5def4a2019-11-07 11:41:16 -0700145{
Jens Axboe958234d2021-02-17 09:00:57 -0700146 struct io_wqe *wqe = worker->wqe;
147
Jens Axboec5def4a2019-11-07 11:41:16 -0700148 if (worker->flags & IO_WORKER_F_BOUND)
149 return &wqe->acct[IO_WQ_ACCT_BOUND];
150
151 return &wqe->acct[IO_WQ_ACCT_UNBOUND];
152}
153
Jens Axboe771b53d02019-10-22 10:25:58 -0600154static void io_worker_exit(struct io_worker *worker)
155{
156 struct io_wqe *wqe = worker->wqe;
Jens Axboe958234d2021-02-17 09:00:57 -0700157 struct io_wqe_acct *acct = io_wqe_get_acct(worker);
Jens Axboebf1daa42021-02-16 18:00:55 -0700158 unsigned flags;
Jens Axboe771b53d02019-10-22 10:25:58 -0600159
160 /*
161 * If we're not at zero, someone else is holding a brief reference
162 * to the worker. Wait for that to go away.
163 */
164 set_current_state(TASK_INTERRUPTIBLE);
165 if (!refcount_dec_and_test(&worker->ref))
166 schedule();
167 __set_current_state(TASK_RUNNING);
168
169 preempt_disable();
170 current->flags &= ~PF_IO_WORKER;
Jens Axboebf1daa42021-02-16 18:00:55 -0700171 flags = worker->flags;
172 worker->flags = 0;
173 if (flags & IO_WORKER_F_RUNNING)
Jens Axboec5def4a2019-11-07 11:41:16 -0700174 atomic_dec(&acct->nr_running);
Jens Axboe771b53d02019-10-22 10:25:58 -0600175 worker->flags = 0;
176 preempt_enable();
177
Jens Axboe4379bf82021-02-15 13:40:22 -0700178 if (worker->saved_creds) {
179 revert_creds(worker->saved_creds);
180 worker->cur_creds = worker->saved_creds = NULL;
181 }
182
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200183 raw_spin_lock_irq(&wqe->lock);
Jens Axboebf1daa42021-02-16 18:00:55 -0700184 if (flags & IO_WORKER_F_FREE)
185 hlist_nulls_del_rcu(&worker->nulls_node);
Jens Axboee61df662019-11-13 13:54:49 -0700186 list_del_rcu(&worker->all_list);
Jens Axboec5def4a2019-11-07 11:41:16 -0700187 acct->nr_workers--;
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200188 raw_spin_unlock_irq(&wqe->lock);
Jens Axboe771b53d02019-10-22 10:25:58 -0600189
YueHaibing364b05f2019-11-02 15:55:01 +0800190 kfree_rcu(worker, rcu);
Hillf Dantonc4068bf2020-09-26 21:26:55 +0800191 if (refcount_dec_and_test(&wqe->wq->refs))
192 complete(&wqe->wq->done);
Jens Axboe771b53d02019-10-22 10:25:58 -0600193}
194
Jens Axboec5def4a2019-11-07 11:41:16 -0700195static inline bool io_wqe_run_queue(struct io_wqe *wqe)
196 __must_hold(wqe->lock)
197{
Jens Axboe6206f0e2019-11-26 11:59:32 -0700198 if (!wq_list_empty(&wqe->work_list) &&
199 !(wqe->flags & IO_WQE_FLAG_STALLED))
Jens Axboec5def4a2019-11-07 11:41:16 -0700200 return true;
201 return false;
202}
203
204/*
205 * Check head of free list for an available worker. If one isn't available,
206 * caller must wake up the wq manager to create one.
207 */
208static bool io_wqe_activate_free_worker(struct io_wqe *wqe)
209 __must_hold(RCU)
210{
211 struct hlist_nulls_node *n;
212 struct io_worker *worker;
213
Jens Axboe021d1cd2019-11-14 08:00:41 -0700214 n = rcu_dereference(hlist_nulls_first_rcu(&wqe->free_list));
Jens Axboec5def4a2019-11-07 11:41:16 -0700215 if (is_a_nulls(n))
216 return false;
217
218 worker = hlist_nulls_entry(n, struct io_worker, nulls_node);
219 if (io_worker_get(worker)) {
Jens Axboe506d95f2019-12-07 21:03:59 -0700220 wake_up_process(worker->task);
Jens Axboec5def4a2019-11-07 11:41:16 -0700221 io_worker_release(worker);
222 return true;
223 }
224
225 return false;
226}
227
228/*
229 * We need a worker. If we find a free one, we're good. If not, and we're
230 * below the max number of workers, wake up the manager to create one.
231 */
232static void io_wqe_wake_worker(struct io_wqe *wqe, struct io_wqe_acct *acct)
233{
234 bool ret;
235
236 /*
237 * Most likely an attempt to queue unbounded work on an io_wq that
238 * wasn't setup with any unbounded workers.
239 */
240 WARN_ON_ONCE(!acct->max_workers);
241
242 rcu_read_lock();
243 ret = io_wqe_activate_free_worker(wqe);
244 rcu_read_unlock();
245
246 if (!ret && acct->nr_workers < acct->max_workers)
247 wake_up_process(wqe->wq->manager);
248}
249
Jens Axboe958234d2021-02-17 09:00:57 -0700250static void io_wqe_inc_running(struct io_worker *worker)
Jens Axboec5def4a2019-11-07 11:41:16 -0700251{
Jens Axboe958234d2021-02-17 09:00:57 -0700252 struct io_wqe_acct *acct = io_wqe_get_acct(worker);
Jens Axboec5def4a2019-11-07 11:41:16 -0700253
254 atomic_inc(&acct->nr_running);
255}
256
Jens Axboe958234d2021-02-17 09:00:57 -0700257static void io_wqe_dec_running(struct io_worker *worker)
Jens Axboec5def4a2019-11-07 11:41:16 -0700258 __must_hold(wqe->lock)
259{
Jens Axboe958234d2021-02-17 09:00:57 -0700260 struct io_wqe_acct *acct = io_wqe_get_acct(worker);
261 struct io_wqe *wqe = worker->wqe;
Jens Axboec5def4a2019-11-07 11:41:16 -0700262
263 if (atomic_dec_and_test(&acct->nr_running) && io_wqe_run_queue(wqe))
264 io_wqe_wake_worker(wqe, acct);
265}
266
Jens Axboe958234d2021-02-17 09:00:57 -0700267static void io_worker_start(struct io_worker *worker)
Jens Axboe771b53d02019-10-22 10:25:58 -0600268{
Jens Axboe771b53d02019-10-22 10:25:58 -0600269 worker->flags |= (IO_WORKER_F_UP | IO_WORKER_F_RUNNING);
Jens Axboe958234d2021-02-17 09:00:57 -0700270 io_wqe_inc_running(worker);
Jens Axboe771b53d02019-10-22 10:25:58 -0600271}
272
273/*
274 * Worker will start processing some work. Move it to the busy list, if
275 * it's currently on the freelist
276 */
277static void __io_worker_busy(struct io_wqe *wqe, struct io_worker *worker,
278 struct io_wq_work *work)
279 __must_hold(wqe->lock)
280{
Jens Axboec5def4a2019-11-07 11:41:16 -0700281 bool worker_bound, work_bound;
282
Jens Axboe771b53d02019-10-22 10:25:58 -0600283 if (worker->flags & IO_WORKER_F_FREE) {
284 worker->flags &= ~IO_WORKER_F_FREE;
285 hlist_nulls_del_init_rcu(&worker->nulls_node);
Jens Axboe771b53d02019-10-22 10:25:58 -0600286 }
Jens Axboec5def4a2019-11-07 11:41:16 -0700287
288 /*
289 * If worker is moving from bound to unbound (or vice versa), then
290 * ensure we update the running accounting.
291 */
Dan Carpenterb2e9c7d62019-11-19 09:22:16 +0300292 worker_bound = (worker->flags & IO_WORKER_F_BOUND) != 0;
293 work_bound = (work->flags & IO_WQ_WORK_UNBOUND) == 0;
294 if (worker_bound != work_bound) {
Jens Axboe958234d2021-02-17 09:00:57 -0700295 io_wqe_dec_running(worker);
Jens Axboec5def4a2019-11-07 11:41:16 -0700296 if (work_bound) {
297 worker->flags |= IO_WORKER_F_BOUND;
298 wqe->acct[IO_WQ_ACCT_UNBOUND].nr_workers--;
299 wqe->acct[IO_WQ_ACCT_BOUND].nr_workers++;
Jens Axboec5def4a2019-11-07 11:41:16 -0700300 } else {
301 worker->flags &= ~IO_WORKER_F_BOUND;
302 wqe->acct[IO_WQ_ACCT_UNBOUND].nr_workers++;
303 wqe->acct[IO_WQ_ACCT_BOUND].nr_workers--;
Jens Axboec5def4a2019-11-07 11:41:16 -0700304 }
Jens Axboe958234d2021-02-17 09:00:57 -0700305 io_wqe_inc_running(worker);
Jens Axboec5def4a2019-11-07 11:41:16 -0700306 }
Jens Axboe771b53d02019-10-22 10:25:58 -0600307}
308
309/*
310 * No work, worker going to sleep. Move to freelist, and unuse mm if we
311 * have one attached. Dropping the mm may potentially sleep, so we drop
312 * the lock in that case and return success. Since the caller has to
313 * retry the loop in that case (we changed task state), we don't regrab
314 * the lock if we return success.
315 */
Jens Axboec6d77d92021-02-15 13:26:34 -0700316static void __io_worker_idle(struct io_wqe *wqe, struct io_worker *worker)
Jens Axboe771b53d02019-10-22 10:25:58 -0600317 __must_hold(wqe->lock)
318{
319 if (!(worker->flags & IO_WORKER_F_FREE)) {
320 worker->flags |= IO_WORKER_F_FREE;
Jens Axboe021d1cd2019-11-14 08:00:41 -0700321 hlist_nulls_add_head_rcu(&worker->nulls_node, &wqe->free_list);
Jens Axboe771b53d02019-10-22 10:25:58 -0600322 }
Jens Axboe4379bf82021-02-15 13:40:22 -0700323 if (worker->saved_creds) {
324 revert_creds(worker->saved_creds);
325 worker->cur_creds = worker->saved_creds = NULL;
326 }
Jens Axboe771b53d02019-10-22 10:25:58 -0600327}
328
Pavel Begunkov60cf46a2020-03-14 00:31:05 +0300329static inline unsigned int io_get_work_hash(struct io_wq_work *work)
330{
331 return work->flags >> IO_WQ_HASH_SHIFT;
332}
333
334static struct io_wq_work *io_get_next_work(struct io_wqe *wqe)
Jens Axboe771b53d02019-10-22 10:25:58 -0600335 __must_hold(wqe->lock)
336{
Jens Axboe6206f0e2019-11-26 11:59:32 -0700337 struct io_wq_work_node *node, *prev;
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300338 struct io_wq_work *work, *tail;
Pavel Begunkov60cf46a2020-03-14 00:31:05 +0300339 unsigned int hash;
Jens Axboe771b53d02019-10-22 10:25:58 -0600340
Jens Axboe6206f0e2019-11-26 11:59:32 -0700341 wq_list_for_each(node, prev, &wqe->work_list) {
342 work = container_of(node, struct io_wq_work, list);
343
Jens Axboe771b53d02019-10-22 10:25:58 -0600344 /* not hashed, can run anytime */
Pavel Begunkov8766dd52020-03-14 00:31:04 +0300345 if (!io_wq_is_hashed(work)) {
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300346 wq_list_del(&wqe->work_list, node, prev);
Jens Axboe771b53d02019-10-22 10:25:58 -0600347 return work;
348 }
349
350 /* hashed, can run if not already running */
Pavel Begunkov60cf46a2020-03-14 00:31:05 +0300351 hash = io_get_work_hash(work);
352 if (!(wqe->hash_map & BIT(hash))) {
353 wqe->hash_map |= BIT(hash);
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300354 /* all items with this hash lie in [work, tail] */
355 tail = wqe->hash_tail[hash];
356 wqe->hash_tail[hash] = NULL;
357 wq_list_cut(&wqe->work_list, &tail->list, prev);
Jens Axboe771b53d02019-10-22 10:25:58 -0600358 return work;
359 }
360 }
361
362 return NULL;
363}
364
Jens Axboe3bfe6102021-02-16 14:15:30 -0700365static void io_flush_signals(void)
Jens Axboecccf0ee2020-01-27 16:34:48 -0700366{
Jens Axboe3bfe6102021-02-16 14:15:30 -0700367 if (unlikely(test_tsk_thread_flag(current, TIF_NOTIFY_SIGNAL))) {
368 if (current->task_works)
369 task_work_run();
370 clear_tsk_thread_flag(current, TIF_NOTIFY_SIGNAL);
Jens Axboecccf0ee2020-01-27 16:34:48 -0700371 }
Pavel Begunkovdc026a72020-03-04 16:14:09 +0300372}
373
Jens Axboe4379bf82021-02-15 13:40:22 -0700374static void io_wq_switch_creds(struct io_worker *worker,
375 struct io_wq_work *work)
376{
377 const struct cred *old_creds = override_creds(work->creds);
378
379 worker->cur_creds = work->creds;
380 if (worker->saved_creds)
381 put_cred(old_creds); /* creds set by previous switch */
382 else
383 worker->saved_creds = old_creds;
384}
385
Pavel Begunkovdc026a72020-03-04 16:14:09 +0300386static void io_assign_current_work(struct io_worker *worker,
387 struct io_wq_work *work)
388{
Pavel Begunkovd78298e2020-03-14 00:31:03 +0300389 if (work) {
Jens Axboe3bfe6102021-02-16 14:15:30 -0700390 io_flush_signals();
Pavel Begunkovd78298e2020-03-14 00:31:03 +0300391 cond_resched();
392 }
Pavel Begunkovdc026a72020-03-04 16:14:09 +0300393
394 spin_lock_irq(&worker->lock);
395 worker->cur_work = work;
396 spin_unlock_irq(&worker->lock);
397}
398
Pavel Begunkov60cf46a2020-03-14 00:31:05 +0300399static void io_wqe_enqueue(struct io_wqe *wqe, struct io_wq_work *work);
400
Jens Axboe771b53d02019-10-22 10:25:58 -0600401static void io_worker_handle_work(struct io_worker *worker)
402 __releases(wqe->lock)
403{
Jens Axboe771b53d02019-10-22 10:25:58 -0600404 struct io_wqe *wqe = worker->wqe;
405 struct io_wq *wq = wqe->wq;
406
407 do {
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300408 struct io_wq_work *work;
Pavel Begunkovf462fd32020-03-04 16:14:11 +0300409get_next:
Jens Axboe771b53d02019-10-22 10:25:58 -0600410 /*
Jens Axboe771b53d02019-10-22 10:25:58 -0600411 * If we got some work, mark us as busy. If we didn't, but
412 * the list isn't empty, it means we stalled on hashed work.
413 * Mark us stalled so we don't keep looking for work when we
414 * can't make progress, any work completion or insertion will
415 * clear the stalled flag.
416 */
Pavel Begunkov60cf46a2020-03-14 00:31:05 +0300417 work = io_get_next_work(wqe);
Jens Axboe771b53d02019-10-22 10:25:58 -0600418 if (work)
419 __io_worker_busy(wqe, worker, work);
Jens Axboe6206f0e2019-11-26 11:59:32 -0700420 else if (!wq_list_empty(&wqe->work_list))
Jens Axboe771b53d02019-10-22 10:25:58 -0600421 wqe->flags |= IO_WQE_FLAG_STALLED;
422
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200423 raw_spin_unlock_irq(&wqe->lock);
Jens Axboe771b53d02019-10-22 10:25:58 -0600424 if (!work)
425 break;
Pavel Begunkov58e39312020-03-04 16:14:10 +0300426 io_assign_current_work(worker, work);
Jens Axboe36c2f922019-11-13 09:43:34 -0700427
Pavel Begunkovdc026a72020-03-04 16:14:09 +0300428 /* handle a whole dependent link */
429 do {
Pavel Begunkov5280f7e2021-02-04 13:52:08 +0000430 struct io_wq_work *next_hashed, *linked;
Pavel Begunkovb089ed392020-07-25 14:42:00 +0300431 unsigned int hash = io_get_work_hash(work);
Hillf Dantonfd1c4bc2019-12-24 09:14:29 -0700432
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300433 next_hashed = wq_next_work(work);
Jens Axboe4379bf82021-02-15 13:40:22 -0700434 if (work->creds && worker->cur_creds != work->creds)
435 io_wq_switch_creds(worker, work);
Pavel Begunkov5280f7e2021-02-04 13:52:08 +0000436 wq->do_work(work);
437 io_assign_current_work(worker, NULL);
Jens Axboe36c2f922019-11-13 09:43:34 -0700438
Pavel Begunkov5280f7e2021-02-04 13:52:08 +0000439 linked = wq->free_work(work);
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300440 work = next_hashed;
441 if (!work && linked && !io_wq_is_hashed(linked)) {
442 work = linked;
443 linked = NULL;
444 }
445 io_assign_current_work(worker, work);
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300446 if (linked)
447 io_wqe_enqueue(wqe, linked);
448
449 if (hash != -1U && !next_hashed) {
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200450 raw_spin_lock_irq(&wqe->lock);
Pavel Begunkovdc026a72020-03-04 16:14:09 +0300451 wqe->hash_map &= ~BIT_ULL(hash);
452 wqe->flags &= ~IO_WQE_FLAG_STALLED;
Pavel Begunkovf462fd32020-03-04 16:14:11 +0300453 /* skip unnecessary unlock-lock wqe->lock */
454 if (!work)
455 goto get_next;
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200456 raw_spin_unlock_irq(&wqe->lock);
Pavel Begunkovdc026a72020-03-04 16:14:09 +0300457 }
Pavel Begunkov58e39312020-03-04 16:14:10 +0300458 } while (work);
Jens Axboe36c2f922019-11-13 09:43:34 -0700459
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200460 raw_spin_lock_irq(&wqe->lock);
Jens Axboe771b53d02019-10-22 10:25:58 -0600461 } while (1);
462}
463
Jens Axboe771b53d02019-10-22 10:25:58 -0600464static int io_wqe_worker(void *data)
465{
466 struct io_worker *worker = data;
467 struct io_wqe *wqe = worker->wqe;
468 struct io_wq *wq = wqe->wq;
Jens Axboe771b53d02019-10-22 10:25:58 -0600469
Jens Axboe958234d2021-02-17 09:00:57 -0700470 io_worker_start(worker);
Jens Axboe771b53d02019-10-22 10:25:58 -0600471
472 while (!test_bit(IO_WQ_BIT_EXIT, &wq->state)) {
Jens Axboe506d95f2019-12-07 21:03:59 -0700473 set_current_state(TASK_INTERRUPTIBLE);
Jens Axboee995d512019-12-07 21:06:46 -0700474loop:
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200475 raw_spin_lock_irq(&wqe->lock);
Jens Axboe771b53d02019-10-22 10:25:58 -0600476 if (io_wqe_run_queue(wqe)) {
477 __set_current_state(TASK_RUNNING);
478 io_worker_handle_work(worker);
Jens Axboee995d512019-12-07 21:06:46 -0700479 goto loop;
Jens Axboe771b53d02019-10-22 10:25:58 -0600480 }
Jens Axboec6d77d92021-02-15 13:26:34 -0700481 __io_worker_idle(wqe, worker);
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200482 raw_spin_unlock_irq(&wqe->lock);
Jens Axboe3bfe6102021-02-16 14:15:30 -0700483 io_flush_signals();
Jens Axboe771b53d02019-10-22 10:25:58 -0600484 if (schedule_timeout(WORKER_IDLE_TIMEOUT))
485 continue;
Jens Axboe3bfe6102021-02-16 14:15:30 -0700486 if (fatal_signal_pending(current))
487 break;
Jens Axboe771b53d02019-10-22 10:25:58 -0600488 /* timed out, exit unless we're the fixed worker */
489 if (test_bit(IO_WQ_BIT_EXIT, &wq->state) ||
490 !(worker->flags & IO_WORKER_F_FIXED))
491 break;
492 }
493
Jens Axboe771b53d02019-10-22 10:25:58 -0600494 if (test_bit(IO_WQ_BIT_EXIT, &wq->state)) {
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200495 raw_spin_lock_irq(&wqe->lock);
Jens Axboe6206f0e2019-11-26 11:59:32 -0700496 if (!wq_list_empty(&wqe->work_list))
Jens Axboe771b53d02019-10-22 10:25:58 -0600497 io_worker_handle_work(worker);
498 else
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200499 raw_spin_unlock_irq(&wqe->lock);
Jens Axboe771b53d02019-10-22 10:25:58 -0600500 }
501
502 io_worker_exit(worker);
503 return 0;
504}
505
506/*
Jens Axboe771b53d02019-10-22 10:25:58 -0600507 * Called when a worker is scheduled in. Mark us as currently running.
508 */
509void io_wq_worker_running(struct task_struct *tsk)
510{
Jens Axboe3bfe6102021-02-16 14:15:30 -0700511 struct io_worker *worker = tsk->pf_io_worker;
Jens Axboe771b53d02019-10-22 10:25:58 -0600512
Jens Axboe3bfe6102021-02-16 14:15:30 -0700513 if (!worker)
514 return;
Jens Axboe771b53d02019-10-22 10:25:58 -0600515 if (!(worker->flags & IO_WORKER_F_UP))
516 return;
517 if (worker->flags & IO_WORKER_F_RUNNING)
518 return;
519 worker->flags |= IO_WORKER_F_RUNNING;
Jens Axboe958234d2021-02-17 09:00:57 -0700520 io_wqe_inc_running(worker);
Jens Axboe771b53d02019-10-22 10:25:58 -0600521}
522
523/*
524 * Called when worker is going to sleep. If there are no workers currently
525 * running and we have work pending, wake up a free one or have the manager
526 * set one up.
527 */
528void io_wq_worker_sleeping(struct task_struct *tsk)
529{
Jens Axboe3bfe6102021-02-16 14:15:30 -0700530 struct io_worker *worker = tsk->pf_io_worker;
Jens Axboe771b53d02019-10-22 10:25:58 -0600531
Jens Axboe3bfe6102021-02-16 14:15:30 -0700532 if (!worker)
533 return;
Jens Axboe771b53d02019-10-22 10:25:58 -0600534 if (!(worker->flags & IO_WORKER_F_UP))
535 return;
536 if (!(worker->flags & IO_WORKER_F_RUNNING))
537 return;
538
539 worker->flags &= ~IO_WORKER_F_RUNNING;
540
Jens Axboe3bfe6102021-02-16 14:15:30 -0700541 raw_spin_lock_irq(&worker->wqe->lock);
Jens Axboe958234d2021-02-17 09:00:57 -0700542 io_wqe_dec_running(worker);
Jens Axboe3bfe6102021-02-16 14:15:30 -0700543 raw_spin_unlock_irq(&worker->wqe->lock);
Jens Axboe771b53d02019-10-22 10:25:58 -0600544}
545
Jens Axboe3bfe6102021-02-16 14:15:30 -0700546static int task_thread(void *data, int index)
Jens Axboe771b53d02019-10-22 10:25:58 -0600547{
Jens Axboe3bfe6102021-02-16 14:15:30 -0700548 struct io_worker *worker = data;
549 struct io_wqe *wqe = worker->wqe;
Hillf Dantonc4068bf2020-09-26 21:26:55 +0800550 struct io_wqe_acct *acct = &wqe->acct[index];
Jens Axboe3bfe6102021-02-16 14:15:30 -0700551 struct io_wq *wq = wqe->wq;
552 char buf[TASK_COMM_LEN];
Jens Axboe771b53d02019-10-22 10:25:58 -0600553
Jens Axboe3bfe6102021-02-16 14:15:30 -0700554 sprintf(buf, "iou-wrk-%d", wq->task_pid);
555 set_task_comm(current, buf);
Jens Axboe771b53d02019-10-22 10:25:58 -0600556
Jens Axboe3bfe6102021-02-16 14:15:30 -0700557 current->pf_io_worker = worker;
558 worker->task = current;
Jens Axboe771b53d02019-10-22 10:25:58 -0600559
Jens Axboe3bfe6102021-02-16 14:15:30 -0700560 set_cpus_allowed_ptr(current, cpumask_of_node(wqe->node));
561 current->flags |= PF_NO_SETAFFINITY;
Jens Axboe771b53d02019-10-22 10:25:58 -0600562
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200563 raw_spin_lock_irq(&wqe->lock);
Jens Axboe021d1cd2019-11-14 08:00:41 -0700564 hlist_nulls_add_head_rcu(&worker->nulls_node, &wqe->free_list);
Jens Axboee61df662019-11-13 13:54:49 -0700565 list_add_tail_rcu(&worker->all_list, &wqe->all_list);
Jens Axboe771b53d02019-10-22 10:25:58 -0600566 worker->flags |= IO_WORKER_F_FREE;
Jens Axboec5def4a2019-11-07 11:41:16 -0700567 if (index == IO_WQ_ACCT_BOUND)
568 worker->flags |= IO_WORKER_F_BOUND;
569 if (!acct->nr_workers && (worker->flags & IO_WORKER_F_BOUND))
Jens Axboe771b53d02019-10-22 10:25:58 -0600570 worker->flags |= IO_WORKER_F_FIXED;
Jens Axboec5def4a2019-11-07 11:41:16 -0700571 acct->nr_workers++;
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200572 raw_spin_unlock_irq(&wqe->lock);
Jens Axboe771b53d02019-10-22 10:25:58 -0600573
Jens Axboe3bfe6102021-02-16 14:15:30 -0700574 io_wqe_worker(data);
575 do_exit(0);
576}
577
578static int task_thread_bound(void *data)
579{
580 return task_thread(data, IO_WQ_ACCT_BOUND);
581}
582
583static int task_thread_unbound(void *data)
584{
585 return task_thread(data, IO_WQ_ACCT_UNBOUND);
586}
587
Jens Axboe843bbfd2021-02-17 21:05:41 -0700588pid_t io_wq_fork_thread(int (*fn)(void *), void *arg)
Jens Axboe3bfe6102021-02-16 14:15:30 -0700589{
590 unsigned long flags = CLONE_FS|CLONE_FILES|CLONE_SIGHAND|CLONE_THREAD|
591 CLONE_IO|SIGCHLD;
592 struct kernel_clone_args args = {
593 .flags = ((lower_32_bits(flags) | CLONE_VM |
594 CLONE_UNTRACED) & ~CSIGNAL),
595 .exit_signal = (lower_32_bits(flags) & CSIGNAL),
596 .stack = (unsigned long)fn,
597 .stack_size = (unsigned long)arg,
598 };
599
600 return kernel_clone(&args);
601}
602
603static bool create_io_worker(struct io_wq *wq, struct io_wqe *wqe, int index)
604{
605 struct io_worker *worker;
606 pid_t pid;
607
608 worker = kzalloc_node(sizeof(*worker), GFP_KERNEL, wqe->node);
609 if (!worker)
610 return false;
611
612 refcount_set(&worker->ref, 1);
613 worker->nulls_node.pprev = NULL;
614 worker->wqe = wqe;
615 spin_lock_init(&worker->lock);
616
617 if (index == IO_WQ_ACCT_BOUND)
Jens Axboe843bbfd2021-02-17 21:05:41 -0700618 pid = io_wq_fork_thread(task_thread_bound, worker);
Jens Axboe3bfe6102021-02-16 14:15:30 -0700619 else
Jens Axboe843bbfd2021-02-17 21:05:41 -0700620 pid = io_wq_fork_thread(task_thread_unbound, worker);
Jens Axboe3bfe6102021-02-16 14:15:30 -0700621 if (pid < 0) {
622 kfree(worker);
623 return false;
624 }
Hillf Dantonc4068bf2020-09-26 21:26:55 +0800625 refcount_inc(&wq->refs);
Jens Axboeb60fda62019-11-19 08:37:07 -0700626 return true;
Jens Axboe771b53d02019-10-22 10:25:58 -0600627}
628
Jens Axboec5def4a2019-11-07 11:41:16 -0700629static inline bool io_wqe_need_worker(struct io_wqe *wqe, int index)
Jens Axboe771b53d02019-10-22 10:25:58 -0600630 __must_hold(wqe->lock)
631{
Jens Axboec5def4a2019-11-07 11:41:16 -0700632 struct io_wqe_acct *acct = &wqe->acct[index];
Jens Axboe771b53d02019-10-22 10:25:58 -0600633
Jens Axboec5def4a2019-11-07 11:41:16 -0700634 /* if we have available workers or no work, no need */
Jens Axboe021d1cd2019-11-14 08:00:41 -0700635 if (!hlist_nulls_empty(&wqe->free_list) || !io_wqe_run_queue(wqe))
Jens Axboec5def4a2019-11-07 11:41:16 -0700636 return false;
637 return acct->nr_workers < acct->max_workers;
Jens Axboe771b53d02019-10-22 10:25:58 -0600638}
639
Hillf Dantonc4068bf2020-09-26 21:26:55 +0800640/*
641 * Iterate the passed in list and call the specific function for each
642 * worker that isn't exiting
643 */
644static bool io_wq_for_each_worker(struct io_wqe *wqe,
645 bool (*func)(struct io_worker *, void *),
646 void *data)
647{
648 struct io_worker *worker;
649 bool ret = false;
650
651 list_for_each_entry_rcu(worker, &wqe->all_list, all_list) {
652 if (io_worker_get(worker)) {
653 /* no task if node is/was offline */
654 if (worker->task)
655 ret = func(worker, data);
656 io_worker_release(worker);
657 if (ret)
658 break;
659 }
660 }
661
662 return ret;
663}
664
665static bool io_wq_worker_wake(struct io_worker *worker, void *data)
666{
667 wake_up_process(worker->task);
668 return false;
669}
670
Jens Axboe771b53d02019-10-22 10:25:58 -0600671/*
672 * Manager thread. Tasked with creating new workers, if we need them.
673 */
674static int io_wq_manager(void *data)
675{
676 struct io_wq *wq = data;
Jens Axboe3bfe6102021-02-16 14:15:30 -0700677 char buf[TASK_COMM_LEN];
Jann Horn3fc50ab2019-11-26 19:10:20 +0100678 int node;
Jens Axboeb60fda62019-11-19 08:37:07 -0700679
Jens Axboe3bfe6102021-02-16 14:15:30 -0700680 sprintf(buf, "iou-mgr-%d", wq->task_pid);
681 set_task_comm(current, buf);
682 current->flags |= PF_IO_WORKER;
683 wq->manager = current;
684
Jens Axboeb60fda62019-11-19 08:37:07 -0700685 complete(&wq->done);
Jens Axboe771b53d02019-10-22 10:25:58 -0600686
Jens Axboe3bfe6102021-02-16 14:15:30 -0700687 while (!test_bit(IO_WQ_BIT_EXIT, &wq->state)) {
Jann Horn3fc50ab2019-11-26 19:10:20 +0100688 for_each_node(node) {
689 struct io_wqe *wqe = wq->wqes[node];
Jens Axboec5def4a2019-11-07 11:41:16 -0700690 bool fork_worker[2] = { false, false };
Jens Axboe771b53d02019-10-22 10:25:58 -0600691
Jens Axboe75634392020-02-11 06:30:06 -0700692 if (!node_online(node))
693 continue;
694
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200695 raw_spin_lock_irq(&wqe->lock);
Jens Axboec5def4a2019-11-07 11:41:16 -0700696 if (io_wqe_need_worker(wqe, IO_WQ_ACCT_BOUND))
697 fork_worker[IO_WQ_ACCT_BOUND] = true;
698 if (io_wqe_need_worker(wqe, IO_WQ_ACCT_UNBOUND))
699 fork_worker[IO_WQ_ACCT_UNBOUND] = true;
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200700 raw_spin_unlock_irq(&wqe->lock);
Jens Axboec5def4a2019-11-07 11:41:16 -0700701 if (fork_worker[IO_WQ_ACCT_BOUND])
702 create_io_worker(wq, wqe, IO_WQ_ACCT_BOUND);
703 if (fork_worker[IO_WQ_ACCT_UNBOUND])
704 create_io_worker(wq, wqe, IO_WQ_ACCT_UNBOUND);
Jens Axboe771b53d02019-10-22 10:25:58 -0600705 }
706 set_current_state(TASK_INTERRUPTIBLE);
707 schedule_timeout(HZ);
Jens Axboe3bfe6102021-02-16 14:15:30 -0700708 if (fatal_signal_pending(current))
709 set_bit(IO_WQ_BIT_EXIT, &wq->state);
Jens Axboe771b53d02019-10-22 10:25:58 -0600710 }
711
Hillf Dantonc4068bf2020-09-26 21:26:55 +0800712 if (refcount_dec_and_test(&wq->refs)) {
Jens Axboeb60fda62019-11-19 08:37:07 -0700713 complete(&wq->done);
Jens Axboe3bfe6102021-02-16 14:15:30 -0700714 do_exit(0);
Hillf Dantonc4068bf2020-09-26 21:26:55 +0800715 }
716 /* if ERROR is set and we get here, we have workers to wake */
717 if (test_bit(IO_WQ_BIT_ERROR, &wq->state)) {
718 rcu_read_lock();
719 for_each_node(node)
720 io_wq_for_each_worker(wq->wqes[node], io_wq_worker_wake, NULL);
721 rcu_read_unlock();
722 }
Jens Axboe3bfe6102021-02-16 14:15:30 -0700723 do_exit(0);
Jens Axboe771b53d02019-10-22 10:25:58 -0600724}
725
Pavel Begunkove9fd9392020-03-04 16:14:12 +0300726static void io_run_cancel(struct io_wq_work *work, struct io_wqe *wqe)
Pavel Begunkovfc04c392020-03-01 19:18:19 +0300727{
Pavel Begunkove9fd9392020-03-04 16:14:12 +0300728 struct io_wq *wq = wqe->wq;
729
Pavel Begunkovfc04c392020-03-01 19:18:19 +0300730 do {
Pavel Begunkovfc04c392020-03-01 19:18:19 +0300731 work->flags |= IO_WQ_WORK_CANCEL;
Pavel Begunkov5280f7e2021-02-04 13:52:08 +0000732 wq->do_work(work);
733 work = wq->free_work(work);
Pavel Begunkovfc04c392020-03-01 19:18:19 +0300734 } while (work);
735}
736
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300737static void io_wqe_insert_work(struct io_wqe *wqe, struct io_wq_work *work)
738{
739 unsigned int hash;
740 struct io_wq_work *tail;
741
742 if (!io_wq_is_hashed(work)) {
743append:
744 wq_list_add_tail(&work->list, &wqe->work_list);
745 return;
746 }
747
748 hash = io_get_work_hash(work);
749 tail = wqe->hash_tail[hash];
750 wqe->hash_tail[hash] = work;
751 if (!tail)
752 goto append;
753
754 wq_list_add_after(&work->list, &tail->list, &wqe->work_list);
755}
756
Jens Axboe771b53d02019-10-22 10:25:58 -0600757static void io_wqe_enqueue(struct io_wqe *wqe, struct io_wq_work *work)
758{
Jens Axboec5def4a2019-11-07 11:41:16 -0700759 struct io_wqe_acct *acct = io_work_get_acct(wqe, work);
Jens Axboe895e2ca2019-12-17 08:46:33 -0700760 int work_flags;
Jens Axboe771b53d02019-10-22 10:25:58 -0600761 unsigned long flags;
762
Jens Axboe895e2ca2019-12-17 08:46:33 -0700763 work_flags = work->flags;
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200764 raw_spin_lock_irqsave(&wqe->lock, flags);
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300765 io_wqe_insert_work(wqe, work);
Jens Axboe771b53d02019-10-22 10:25:58 -0600766 wqe->flags &= ~IO_WQE_FLAG_STALLED;
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200767 raw_spin_unlock_irqrestore(&wqe->lock, flags);
Jens Axboe771b53d02019-10-22 10:25:58 -0600768
Jens Axboe895e2ca2019-12-17 08:46:33 -0700769 if ((work_flags & IO_WQ_WORK_CONCURRENT) ||
770 !atomic_read(&acct->nr_running))
Jens Axboec5def4a2019-11-07 11:41:16 -0700771 io_wqe_wake_worker(wqe, acct);
Jens Axboe771b53d02019-10-22 10:25:58 -0600772}
773
774void io_wq_enqueue(struct io_wq *wq, struct io_wq_work *work)
775{
776 struct io_wqe *wqe = wq->wqes[numa_node_id()];
777
778 io_wqe_enqueue(wqe, work);
779}
780
781/*
Pavel Begunkov8766dd52020-03-14 00:31:04 +0300782 * Work items that hash to the same value will not be done in parallel.
783 * Used to limit concurrent writes, generally hashed by inode.
Jens Axboe771b53d02019-10-22 10:25:58 -0600784 */
Pavel Begunkov8766dd52020-03-14 00:31:04 +0300785void io_wq_hash_work(struct io_wq_work *work, void *val)
Jens Axboe771b53d02019-10-22 10:25:58 -0600786{
Pavel Begunkov8766dd52020-03-14 00:31:04 +0300787 unsigned int bit;
Jens Axboe771b53d02019-10-22 10:25:58 -0600788
789 bit = hash_ptr(val, IO_WQ_HASH_ORDER);
790 work->flags |= (IO_WQ_WORK_HASHED | (bit << IO_WQ_HASH_SHIFT));
Jens Axboe771b53d02019-10-22 10:25:58 -0600791}
792
Jens Axboe62755e32019-10-28 21:49:21 -0600793struct io_cb_cancel_data {
Pavel Begunkov2293b412020-03-07 01:15:39 +0300794 work_cancel_fn *fn;
795 void *data;
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300796 int nr_running;
797 int nr_pending;
798 bool cancel_all;
Jens Axboe62755e32019-10-28 21:49:21 -0600799};
800
Pavel Begunkov2293b412020-03-07 01:15:39 +0300801static bool io_wq_worker_cancel(struct io_worker *worker, void *data)
Jens Axboe62755e32019-10-28 21:49:21 -0600802{
Pavel Begunkov2293b412020-03-07 01:15:39 +0300803 struct io_cb_cancel_data *match = data;
Jens Axboe6f726532019-11-05 13:51:51 -0700804 unsigned long flags;
Jens Axboe62755e32019-10-28 21:49:21 -0600805
806 /*
807 * Hold the lock to avoid ->cur_work going out of scope, caller
Jens Axboe36c2f922019-11-13 09:43:34 -0700808 * may dereference the passed in work.
Jens Axboe62755e32019-10-28 21:49:21 -0600809 */
Jens Axboe36c2f922019-11-13 09:43:34 -0700810 spin_lock_irqsave(&worker->lock, flags);
Jens Axboe62755e32019-10-28 21:49:21 -0600811 if (worker->cur_work &&
Pavel Begunkov2293b412020-03-07 01:15:39 +0300812 match->fn(worker->cur_work, match->data)) {
Jens Axboe3bfe6102021-02-16 14:15:30 -0700813 set_notify_signal(worker->task);
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300814 match->nr_running++;
Jens Axboe771b53d02019-10-22 10:25:58 -0600815 }
Jens Axboe36c2f922019-11-13 09:43:34 -0700816 spin_unlock_irqrestore(&worker->lock, flags);
Jens Axboe771b53d02019-10-22 10:25:58 -0600817
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300818 return match->nr_running && !match->cancel_all;
Jens Axboe771b53d02019-10-22 10:25:58 -0600819}
820
Pavel Begunkov204361a2020-08-23 20:33:10 +0300821static inline void io_wqe_remove_pending(struct io_wqe *wqe,
822 struct io_wq_work *work,
823 struct io_wq_work_node *prev)
824{
825 unsigned int hash = io_get_work_hash(work);
826 struct io_wq_work *prev_work = NULL;
827
828 if (io_wq_is_hashed(work) && work == wqe->hash_tail[hash]) {
829 if (prev)
830 prev_work = container_of(prev, struct io_wq_work, list);
831 if (prev_work && io_get_work_hash(prev_work) == hash)
832 wqe->hash_tail[hash] = prev_work;
833 else
834 wqe->hash_tail[hash] = NULL;
835 }
836 wq_list_del(&wqe->work_list, &work->list, prev);
837}
838
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300839static void io_wqe_cancel_pending_work(struct io_wqe *wqe,
Pavel Begunkovf4c26652020-06-15 10:24:02 +0300840 struct io_cb_cancel_data *match)
Jens Axboe771b53d02019-10-22 10:25:58 -0600841{
Jens Axboe6206f0e2019-11-26 11:59:32 -0700842 struct io_wq_work_node *node, *prev;
Jens Axboe771b53d02019-10-22 10:25:58 -0600843 struct io_wq_work *work;
Jens Axboe6f726532019-11-05 13:51:51 -0700844 unsigned long flags;
Jens Axboe771b53d02019-10-22 10:25:58 -0600845
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300846retry:
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200847 raw_spin_lock_irqsave(&wqe->lock, flags);
Jens Axboe6206f0e2019-11-26 11:59:32 -0700848 wq_list_for_each(node, prev, &wqe->work_list) {
849 work = container_of(node, struct io_wq_work, list);
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300850 if (!match->fn(work, match->data))
851 continue;
Pavel Begunkov204361a2020-08-23 20:33:10 +0300852 io_wqe_remove_pending(wqe, work, prev);
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200853 raw_spin_unlock_irqrestore(&wqe->lock, flags);
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300854 io_run_cancel(work, wqe);
855 match->nr_pending++;
856 if (!match->cancel_all)
857 return;
858
859 /* not safe to continue after unlock */
860 goto retry;
Jens Axboe771b53d02019-10-22 10:25:58 -0600861 }
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200862 raw_spin_unlock_irqrestore(&wqe->lock, flags);
Pavel Begunkovf4c26652020-06-15 10:24:02 +0300863}
Jens Axboe771b53d02019-10-22 10:25:58 -0600864
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300865static void io_wqe_cancel_running_work(struct io_wqe *wqe,
Pavel Begunkovf4c26652020-06-15 10:24:02 +0300866 struct io_cb_cancel_data *match)
867{
Jens Axboe771b53d02019-10-22 10:25:58 -0600868 rcu_read_lock();
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300869 io_wq_for_each_worker(wqe, io_wq_worker_cancel, match);
Jens Axboe771b53d02019-10-22 10:25:58 -0600870 rcu_read_unlock();
Jens Axboe771b53d02019-10-22 10:25:58 -0600871}
872
Pavel Begunkov2293b412020-03-07 01:15:39 +0300873enum io_wq_cancel io_wq_cancel_cb(struct io_wq *wq, work_cancel_fn *cancel,
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300874 void *data, bool cancel_all)
Pavel Begunkov2293b412020-03-07 01:15:39 +0300875{
876 struct io_cb_cancel_data match = {
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300877 .fn = cancel,
878 .data = data,
879 .cancel_all = cancel_all,
Pavel Begunkov2293b412020-03-07 01:15:39 +0300880 };
Pavel Begunkov2293b412020-03-07 01:15:39 +0300881 int node;
882
Pavel Begunkovf4c26652020-06-15 10:24:02 +0300883 /*
884 * First check pending list, if we're lucky we can just remove it
885 * from there. CANCEL_OK means that the work is returned as-new,
886 * no completion will be posted for it.
887 */
Pavel Begunkov2293b412020-03-07 01:15:39 +0300888 for_each_node(node) {
889 struct io_wqe *wqe = wq->wqes[node];
890
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300891 io_wqe_cancel_pending_work(wqe, &match);
892 if (match.nr_pending && !match.cancel_all)
Pavel Begunkovf4c26652020-06-15 10:24:02 +0300893 return IO_WQ_CANCEL_OK;
Pavel Begunkov2293b412020-03-07 01:15:39 +0300894 }
895
Pavel Begunkovf4c26652020-06-15 10:24:02 +0300896 /*
897 * Now check if a free (going busy) or busy worker has the work
898 * currently running. If we find it there, we'll return CANCEL_RUNNING
899 * as an indication that we attempt to signal cancellation. The
900 * completion will run normally in this case.
901 */
902 for_each_node(node) {
903 struct io_wqe *wqe = wq->wqes[node];
904
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300905 io_wqe_cancel_running_work(wqe, &match);
906 if (match.nr_running && !match.cancel_all)
Pavel Begunkovf4c26652020-06-15 10:24:02 +0300907 return IO_WQ_CANCEL_RUNNING;
908 }
909
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300910 if (match.nr_running)
911 return IO_WQ_CANCEL_RUNNING;
912 if (match.nr_pending)
913 return IO_WQ_CANCEL_OK;
Pavel Begunkovf4c26652020-06-15 10:24:02 +0300914 return IO_WQ_CANCEL_NOTFOUND;
Pavel Begunkov2293b412020-03-07 01:15:39 +0300915}
916
Jens Axboe576a3472019-11-25 08:49:20 -0700917struct io_wq *io_wq_create(unsigned bounded, struct io_wq_data *data)
Jens Axboe771b53d02019-10-22 10:25:58 -0600918{
Jann Horn3fc50ab2019-11-26 19:10:20 +0100919 int ret = -ENOMEM, node;
Jens Axboe771b53d02019-10-22 10:25:58 -0600920 struct io_wq *wq;
921
Pavel Begunkovf5fa38c2020-06-08 21:08:20 +0300922 if (WARN_ON_ONCE(!data->free_work || !data->do_work))
Pavel Begunkove9fd9392020-03-04 16:14:12 +0300923 return ERR_PTR(-EINVAL);
924
Jann Hornad6e0052019-11-26 17:39:45 +0100925 wq = kzalloc(sizeof(*wq), GFP_KERNEL);
Jens Axboe771b53d02019-10-22 10:25:58 -0600926 if (!wq)
927 return ERR_PTR(-ENOMEM);
928
Jann Horn3fc50ab2019-11-26 19:10:20 +0100929 wq->wqes = kcalloc(nr_node_ids, sizeof(struct io_wqe *), GFP_KERNEL);
Jens Axboe43c01fb2020-10-22 09:02:50 -0600930 if (!wq->wqes)
931 goto err_wq;
932
933 ret = cpuhp_state_add_instance_nocalls(io_wq_online, &wq->cpuhp_node);
934 if (ret)
935 goto err_wqes;
Jens Axboe771b53d02019-10-22 10:25:58 -0600936
Pavel Begunkove9fd9392020-03-04 16:14:12 +0300937 wq->free_work = data->free_work;
Pavel Begunkovf5fa38c2020-06-08 21:08:20 +0300938 wq->do_work = data->do_work;
Jens Axboe7d723062019-11-12 22:31:31 -0700939
Jens Axboe43c01fb2020-10-22 09:02:50 -0600940 ret = -ENOMEM;
Jann Horn3fc50ab2019-11-26 19:10:20 +0100941 for_each_node(node) {
Jens Axboe771b53d02019-10-22 10:25:58 -0600942 struct io_wqe *wqe;
Jens Axboe75634392020-02-11 06:30:06 -0700943 int alloc_node = node;
Jens Axboe771b53d02019-10-22 10:25:58 -0600944
Jens Axboe75634392020-02-11 06:30:06 -0700945 if (!node_online(alloc_node))
946 alloc_node = NUMA_NO_NODE;
947 wqe = kzalloc_node(sizeof(struct io_wqe), GFP_KERNEL, alloc_node);
Jens Axboe771b53d02019-10-22 10:25:58 -0600948 if (!wqe)
Jann Horn3fc50ab2019-11-26 19:10:20 +0100949 goto err;
950 wq->wqes[node] = wqe;
Jens Axboe75634392020-02-11 06:30:06 -0700951 wqe->node = alloc_node;
Jens Axboec5def4a2019-11-07 11:41:16 -0700952 wqe->acct[IO_WQ_ACCT_BOUND].max_workers = bounded;
953 atomic_set(&wqe->acct[IO_WQ_ACCT_BOUND].nr_running, 0);
Jens Axboe728f13e2021-02-21 16:02:53 -0700954 wqe->acct[IO_WQ_ACCT_UNBOUND].max_workers =
Jens Axboec5def4a2019-11-07 11:41:16 -0700955 task_rlimit(current, RLIMIT_NPROC);
Jens Axboec5def4a2019-11-07 11:41:16 -0700956 atomic_set(&wqe->acct[IO_WQ_ACCT_UNBOUND].nr_running, 0);
Jens Axboe771b53d02019-10-22 10:25:58 -0600957 wqe->wq = wq;
Sebastian Andrzej Siewior95da8462020-09-01 10:41:46 +0200958 raw_spin_lock_init(&wqe->lock);
Jens Axboe6206f0e2019-11-26 11:59:32 -0700959 INIT_WQ_LIST(&wqe->work_list);
Jens Axboe021d1cd2019-11-14 08:00:41 -0700960 INIT_HLIST_NULLS_HEAD(&wqe->free_list, 0);
Jens Axboee61df662019-11-13 13:54:49 -0700961 INIT_LIST_HEAD(&wqe->all_list);
Jens Axboe771b53d02019-10-22 10:25:58 -0600962 }
963
Jens Axboe3bfe6102021-02-16 14:15:30 -0700964 wq->task_pid = current->pid;
Jens Axboe771b53d02019-10-22 10:25:58 -0600965 init_completion(&wq->done);
Jens Axboe3bfe6102021-02-16 14:15:30 -0700966 refcount_set(&wq->refs, 1);
Jens Axboe771b53d02019-10-22 10:25:58 -0600967
Jens Axboe3bfe6102021-02-16 14:15:30 -0700968 current->flags |= PF_IO_WORKER;
Jens Axboe843bbfd2021-02-17 21:05:41 -0700969 ret = io_wq_fork_thread(io_wq_manager, wq);
Jens Axboe3bfe6102021-02-16 14:15:30 -0700970 current->flags &= ~PF_IO_WORKER;
971 if (ret >= 0) {
Jens Axboeb60fda62019-11-19 08:37:07 -0700972 wait_for_completion(&wq->done);
Jens Axboeb60fda62019-11-19 08:37:07 -0700973 reinit_completion(&wq->done);
Jens Axboe771b53d02019-10-22 10:25:58 -0600974 return wq;
975 }
976
Jens Axboe3bfe6102021-02-16 14:15:30 -0700977 if (refcount_dec_and_test(&wq->refs))
978 complete(&wq->done);
Jens Axboeb60fda62019-11-19 08:37:07 -0700979err:
Jens Axboe43c01fb2020-10-22 09:02:50 -0600980 cpuhp_state_remove_instance_nocalls(io_wq_online, &wq->cpuhp_node);
Jann Horn3fc50ab2019-11-26 19:10:20 +0100981 for_each_node(node)
982 kfree(wq->wqes[node]);
Jens Axboe43c01fb2020-10-22 09:02:50 -0600983err_wqes:
Jens Axboeb60fda62019-11-19 08:37:07 -0700984 kfree(wq->wqes);
Jens Axboe43c01fb2020-10-22 09:02:50 -0600985err_wq:
Jens Axboeb60fda62019-11-19 08:37:07 -0700986 kfree(wq);
Jens Axboe771b53d02019-10-22 10:25:58 -0600987 return ERR_PTR(ret);
988}
989
Jens Axboe3b094e72021-02-16 15:42:24 -0700990void io_wq_destroy(struct io_wq *wq)
Jens Axboe771b53d02019-10-22 10:25:58 -0600991{
Jann Horn3fc50ab2019-11-26 19:10:20 +0100992 int node;
Jens Axboe771b53d02019-10-22 10:25:58 -0600993
Jens Axboe43c01fb2020-10-22 09:02:50 -0600994 cpuhp_state_remove_instance_nocalls(io_wq_online, &wq->cpuhp_node);
995
Jens Axboeb60fda62019-11-19 08:37:07 -0700996 set_bit(IO_WQ_BIT_EXIT, &wq->state);
997 if (wq->manager)
Jens Axboe3bfe6102021-02-16 14:15:30 -0700998 wake_up_process(wq->manager);
Jens Axboe771b53d02019-10-22 10:25:58 -0600999
1000 rcu_read_lock();
Jann Horn3fc50ab2019-11-26 19:10:20 +01001001 for_each_node(node)
1002 io_wq_for_each_worker(wq->wqes[node], io_wq_worker_wake, NULL);
Jens Axboe771b53d02019-10-22 10:25:58 -06001003 rcu_read_unlock();
1004
1005 wait_for_completion(&wq->done);
1006
Jann Horn3fc50ab2019-11-26 19:10:20 +01001007 for_each_node(node)
1008 kfree(wq->wqes[node]);
Jens Axboe771b53d02019-10-22 10:25:58 -06001009 kfree(wq->wqes);
1010 kfree(wq);
1011}
Jens Axboe848f7e12020-01-23 15:33:32 -07001012
Jens Axboe43c01fb2020-10-22 09:02:50 -06001013static bool io_wq_worker_affinity(struct io_worker *worker, void *data)
1014{
1015 struct task_struct *task = worker->task;
1016 struct rq_flags rf;
1017 struct rq *rq;
1018
1019 rq = task_rq_lock(task, &rf);
1020 do_set_cpus_allowed(task, cpumask_of_node(worker->wqe->node));
1021 task->flags |= PF_NO_SETAFFINITY;
1022 task_rq_unlock(rq, task, &rf);
1023 return false;
1024}
1025
1026static int io_wq_cpu_online(unsigned int cpu, struct hlist_node *node)
1027{
1028 struct io_wq *wq = hlist_entry_safe(node, struct io_wq, cpuhp_node);
1029 int i;
1030
1031 rcu_read_lock();
1032 for_each_node(i)
1033 io_wq_for_each_worker(wq->wqes[i], io_wq_worker_affinity, NULL);
1034 rcu_read_unlock();
1035 return 0;
1036}
1037
1038static __init int io_wq_init(void)
1039{
1040 int ret;
1041
1042 ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "io-wq/online",
1043 io_wq_cpu_online, NULL);
1044 if (ret < 0)
1045 return ret;
1046 io_wq_online = ret;
1047 return 0;
1048}
1049subsys_initcall(io_wq_init);