Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 1 | // 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 Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 13 | #include <linux/sched/mm.h> |
| 14 | #include <linux/percpu.h> |
| 15 | #include <linux/slab.h> |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 16 | #include <linux/rculist_nulls.h> |
Jens Axboe | 43c01fb | 2020-10-22 09:02:50 -0600 | [diff] [blame] | 17 | #include <linux/cpu.h> |
Jens Axboe | 3bfe610 | 2021-02-16 14:15:30 -0700 | [diff] [blame] | 18 | #include <linux/tracehook.h> |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 19 | |
| 20 | #include "io-wq.h" |
| 21 | |
| 22 | #define WORKER_IDLE_TIMEOUT (5 * HZ) |
| 23 | |
| 24 | enum { |
| 25 | IO_WORKER_F_UP = 1, /* up and active */ |
| 26 | IO_WORKER_F_RUNNING = 2, /* account as running */ |
| 27 | IO_WORKER_F_FREE = 4, /* worker on free list */ |
Jens Axboe | 145cc8c | 2020-09-26 12:37:46 -0600 | [diff] [blame] | 28 | IO_WORKER_F_FIXED = 8, /* static idle worker */ |
| 29 | IO_WORKER_F_BOUND = 16, /* is doing bounded work */ |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 30 | }; |
| 31 | |
| 32 | enum { |
| 33 | IO_WQ_BIT_EXIT = 0, /* wq exiting */ |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 34 | }; |
| 35 | |
| 36 | enum { |
| 37 | IO_WQE_FLAG_STALLED = 1, /* stalled on hash */ |
| 38 | }; |
| 39 | |
| 40 | /* |
| 41 | * One for each thread in a wqe pool |
| 42 | */ |
| 43 | struct io_worker { |
| 44 | refcount_t ref; |
| 45 | unsigned flags; |
| 46 | struct hlist_nulls_node nulls_node; |
Jens Axboe | e61df66 | 2019-11-13 13:54:49 -0700 | [diff] [blame] | 47 | struct list_head all_list; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 48 | struct task_struct *task; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 49 | struct io_wqe *wqe; |
Jens Axboe | 36c2f92 | 2019-11-13 09:43:34 -0700 | [diff] [blame] | 50 | |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 51 | struct io_wq_work *cur_work; |
Jens Axboe | 36c2f92 | 2019-11-13 09:43:34 -0700 | [diff] [blame] | 52 | spinlock_t lock; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 53 | |
Jens Axboe | eb2de94 | 2021-02-23 19:59:06 -0700 | [diff] [blame] | 54 | struct completion ref_done; |
| 55 | |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 56 | struct rcu_head rcu; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 57 | }; |
| 58 | |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 59 | #if BITS_PER_LONG == 64 |
| 60 | #define IO_WQ_HASH_ORDER 6 |
| 61 | #else |
| 62 | #define IO_WQ_HASH_ORDER 5 |
| 63 | #endif |
| 64 | |
Pavel Begunkov | 86f3cd1 | 2020-03-23 22:57:22 +0300 | [diff] [blame] | 65 | #define IO_WQ_NR_HASH_BUCKETS (1u << IO_WQ_HASH_ORDER) |
| 66 | |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 67 | struct io_wqe_acct { |
| 68 | unsigned nr_workers; |
| 69 | unsigned max_workers; |
Jens Axboe | 685fe7f | 2021-03-08 09:37:51 -0700 | [diff] [blame] | 70 | int index; |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 71 | atomic_t nr_running; |
| 72 | }; |
| 73 | |
| 74 | enum { |
| 75 | IO_WQ_ACCT_BOUND, |
| 76 | IO_WQ_ACCT_UNBOUND, |
| 77 | }; |
| 78 | |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 79 | /* |
| 80 | * Per-node worker thread pool |
| 81 | */ |
| 82 | struct io_wqe { |
| 83 | struct { |
Sebastian Andrzej Siewior | 95da846 | 2020-09-01 10:41:46 +0200 | [diff] [blame] | 84 | raw_spinlock_t lock; |
Jens Axboe | 6206f0e | 2019-11-26 11:59:32 -0700 | [diff] [blame] | 85 | struct io_wq_work_list work_list; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 86 | unsigned flags; |
| 87 | } ____cacheline_aligned_in_smp; |
| 88 | |
| 89 | int node; |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 90 | struct io_wqe_acct acct[2]; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 91 | |
Jens Axboe | 021d1cd | 2019-11-14 08:00:41 -0700 | [diff] [blame] | 92 | struct hlist_nulls_head free_list; |
Jens Axboe | e61df66 | 2019-11-13 13:54:49 -0700 | [diff] [blame] | 93 | struct list_head all_list; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 94 | |
Jens Axboe | e941894 | 2021-02-19 12:33:30 -0700 | [diff] [blame] | 95 | struct wait_queue_entry wait; |
| 96 | |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 97 | struct io_wq *wq; |
Pavel Begunkov | 86f3cd1 | 2020-03-23 22:57:22 +0300 | [diff] [blame] | 98 | struct io_wq_work *hash_tail[IO_WQ_NR_HASH_BUCKETS]; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 99 | }; |
| 100 | |
| 101 | /* |
| 102 | * Per io_wq state |
| 103 | */ |
| 104 | struct io_wq { |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 105 | unsigned long state; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 106 | |
Pavel Begunkov | e9fd939 | 2020-03-04 16:14:12 +0300 | [diff] [blame] | 107 | free_work_fn *free_work; |
Pavel Begunkov | f5fa38c | 2020-06-08 21:08:20 +0300 | [diff] [blame] | 108 | io_wq_work_fn *do_work; |
Jens Axboe | 7d72306 | 2019-11-12 22:31:31 -0700 | [diff] [blame] | 109 | |
Jens Axboe | e941894 | 2021-02-19 12:33:30 -0700 | [diff] [blame] | 110 | struct io_wq_hash *hash; |
| 111 | |
Jens Axboe | fb3a1f6 | 2021-02-26 09:47:20 -0700 | [diff] [blame] | 112 | atomic_t worker_refs; |
| 113 | struct completion worker_done; |
| 114 | |
Jens Axboe | 43c01fb | 2020-10-22 09:02:50 -0600 | [diff] [blame] | 115 | struct hlist_node cpuhp_node; |
Jens Axboe | 3bfe610 | 2021-02-16 14:15:30 -0700 | [diff] [blame] | 116 | |
Jens Axboe | 685fe7f | 2021-03-08 09:37:51 -0700 | [diff] [blame] | 117 | struct task_struct *task; |
Pavel Begunkov | c7f405d | 2021-06-14 02:36:12 +0100 | [diff] [blame] | 118 | |
| 119 | struct io_wqe *wqes[]; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 120 | }; |
| 121 | |
Jens Axboe | 43c01fb | 2020-10-22 09:02:50 -0600 | [diff] [blame] | 122 | static enum cpuhp_state io_wq_online; |
| 123 | |
Jens Axboe | f012725 | 2021-03-03 15:47:04 -0700 | [diff] [blame] | 124 | struct io_cb_cancel_data { |
| 125 | work_cancel_fn *fn; |
| 126 | void *data; |
| 127 | int nr_running; |
| 128 | int nr_pending; |
| 129 | bool cancel_all; |
| 130 | }; |
| 131 | |
Jens Axboe | 685fe7f | 2021-03-08 09:37:51 -0700 | [diff] [blame] | 132 | static void create_io_worker(struct io_wq *wq, struct io_wqe *wqe, int index); |
Jens Axboe | f012725 | 2021-03-03 15:47:04 -0700 | [diff] [blame] | 133 | |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 134 | static bool io_worker_get(struct io_worker *worker) |
| 135 | { |
| 136 | return refcount_inc_not_zero(&worker->ref); |
| 137 | } |
| 138 | |
| 139 | static void io_worker_release(struct io_worker *worker) |
| 140 | { |
| 141 | if (refcount_dec_and_test(&worker->ref)) |
Jens Axboe | eb2de94 | 2021-02-23 19:59:06 -0700 | [diff] [blame] | 142 | complete(&worker->ref_done); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 143 | } |
| 144 | |
Pavel Begunkov | 8418f22 | 2021-03-22 01:58:28 +0000 | [diff] [blame] | 145 | static inline struct io_wqe_acct *io_get_acct(struct io_wqe *wqe, bool bound) |
| 146 | { |
| 147 | return &wqe->acct[bound ? IO_WQ_ACCT_BOUND : IO_WQ_ACCT_UNBOUND]; |
| 148 | } |
| 149 | |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 150 | static inline struct io_wqe_acct *io_work_get_acct(struct io_wqe *wqe, |
| 151 | struct io_wq_work *work) |
| 152 | { |
Pavel Begunkov | 8418f22 | 2021-03-22 01:58:28 +0000 | [diff] [blame] | 153 | return io_get_acct(wqe, !(work->flags & IO_WQ_WORK_UNBOUND)); |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 154 | } |
| 155 | |
Jens Axboe | 958234d | 2021-02-17 09:00:57 -0700 | [diff] [blame] | 156 | static inline struct io_wqe_acct *io_wqe_get_acct(struct io_worker *worker) |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 157 | { |
Pavel Begunkov | 8418f22 | 2021-03-22 01:58:28 +0000 | [diff] [blame] | 158 | return io_get_acct(worker->wqe, worker->flags & IO_WORKER_F_BOUND); |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 159 | } |
| 160 | |
Jens Axboe | 685fe7f | 2021-03-08 09:37:51 -0700 | [diff] [blame] | 161 | static void io_worker_ref_put(struct io_wq *wq) |
| 162 | { |
| 163 | if (atomic_dec_and_test(&wq->worker_refs)) |
| 164 | complete(&wq->worker_done); |
| 165 | } |
| 166 | |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 167 | static void io_worker_exit(struct io_worker *worker) |
| 168 | { |
| 169 | struct io_wqe *wqe = worker->wqe; |
Jens Axboe | 958234d | 2021-02-17 09:00:57 -0700 | [diff] [blame] | 170 | struct io_wqe_acct *acct = io_wqe_get_acct(worker); |
Jens Axboe | bf1daa4 | 2021-02-16 18:00:55 -0700 | [diff] [blame] | 171 | unsigned flags; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 172 | |
Jens Axboe | eb2de94 | 2021-02-23 19:59:06 -0700 | [diff] [blame] | 173 | if (refcount_dec_and_test(&worker->ref)) |
| 174 | complete(&worker->ref_done); |
| 175 | wait_for_completion(&worker->ref_done); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 176 | |
| 177 | preempt_disable(); |
| 178 | current->flags &= ~PF_IO_WORKER; |
Jens Axboe | bf1daa4 | 2021-02-16 18:00:55 -0700 | [diff] [blame] | 179 | flags = worker->flags; |
| 180 | worker->flags = 0; |
| 181 | if (flags & IO_WORKER_F_RUNNING) |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 182 | atomic_dec(&acct->nr_running); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 183 | worker->flags = 0; |
| 184 | preempt_enable(); |
| 185 | |
Sebastian Andrzej Siewior | 95da846 | 2020-09-01 10:41:46 +0200 | [diff] [blame] | 186 | raw_spin_lock_irq(&wqe->lock); |
Jens Axboe | bf1daa4 | 2021-02-16 18:00:55 -0700 | [diff] [blame] | 187 | if (flags & IO_WORKER_F_FREE) |
| 188 | hlist_nulls_del_rcu(&worker->nulls_node); |
Jens Axboe | e61df66 | 2019-11-13 13:54:49 -0700 | [diff] [blame] | 189 | list_del_rcu(&worker->all_list); |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 190 | acct->nr_workers--; |
Sebastian Andrzej Siewior | 95da846 | 2020-09-01 10:41:46 +0200 | [diff] [blame] | 191 | raw_spin_unlock_irq(&wqe->lock); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 192 | |
YueHaibing | 364b05f | 2019-11-02 15:55:01 +0800 | [diff] [blame] | 193 | kfree_rcu(worker, rcu); |
Jens Axboe | 685fe7f | 2021-03-08 09:37:51 -0700 | [diff] [blame] | 194 | io_worker_ref_put(wqe->wq); |
Jens Axboe | 46fe18b | 2021-03-04 12:39:36 -0700 | [diff] [blame] | 195 | do_exit(0); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 196 | } |
| 197 | |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 198 | static inline bool io_wqe_run_queue(struct io_wqe *wqe) |
| 199 | __must_hold(wqe->lock) |
| 200 | { |
Jens Axboe | 6206f0e | 2019-11-26 11:59:32 -0700 | [diff] [blame] | 201 | if (!wq_list_empty(&wqe->work_list) && |
| 202 | !(wqe->flags & IO_WQE_FLAG_STALLED)) |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 203 | return true; |
| 204 | return false; |
| 205 | } |
| 206 | |
| 207 | /* |
| 208 | * Check head of free list for an available worker. If one isn't available, |
Jens Axboe | 685fe7f | 2021-03-08 09:37:51 -0700 | [diff] [blame] | 209 | * caller must create one. |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 210 | */ |
| 211 | static bool io_wqe_activate_free_worker(struct io_wqe *wqe) |
| 212 | __must_hold(RCU) |
| 213 | { |
| 214 | struct hlist_nulls_node *n; |
| 215 | struct io_worker *worker; |
| 216 | |
Jens Axboe | 021d1cd | 2019-11-14 08:00:41 -0700 | [diff] [blame] | 217 | n = rcu_dereference(hlist_nulls_first_rcu(&wqe->free_list)); |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 218 | if (is_a_nulls(n)) |
| 219 | return false; |
| 220 | |
| 221 | worker = hlist_nulls_entry(n, struct io_worker, nulls_node); |
| 222 | if (io_worker_get(worker)) { |
Jens Axboe | 506d95f | 2019-12-07 21:03:59 -0700 | [diff] [blame] | 223 | wake_up_process(worker->task); |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 224 | io_worker_release(worker); |
| 225 | return true; |
| 226 | } |
| 227 | |
| 228 | return false; |
| 229 | } |
| 230 | |
| 231 | /* |
| 232 | * We need a worker. If we find a free one, we're good. If not, and we're |
Jens Axboe | 685fe7f | 2021-03-08 09:37:51 -0700 | [diff] [blame] | 233 | * below the max number of workers, create one. |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 234 | */ |
| 235 | static void io_wqe_wake_worker(struct io_wqe *wqe, struct io_wqe_acct *acct) |
| 236 | { |
| 237 | bool ret; |
| 238 | |
| 239 | /* |
| 240 | * Most likely an attempt to queue unbounded work on an io_wq that |
| 241 | * wasn't setup with any unbounded workers. |
| 242 | */ |
| 243 | WARN_ON_ONCE(!acct->max_workers); |
| 244 | |
| 245 | rcu_read_lock(); |
| 246 | ret = io_wqe_activate_free_worker(wqe); |
| 247 | rcu_read_unlock(); |
| 248 | |
Jens Axboe | 685fe7f | 2021-03-08 09:37:51 -0700 | [diff] [blame] | 249 | if (!ret && acct->nr_workers < acct->max_workers) { |
| 250 | atomic_inc(&acct->nr_running); |
| 251 | atomic_inc(&wqe->wq->worker_refs); |
| 252 | create_io_worker(wqe->wq, wqe, acct->index); |
| 253 | } |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 254 | } |
| 255 | |
Jens Axboe | 958234d | 2021-02-17 09:00:57 -0700 | [diff] [blame] | 256 | static void io_wqe_inc_running(struct io_worker *worker) |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 257 | { |
Jens Axboe | 958234d | 2021-02-17 09:00:57 -0700 | [diff] [blame] | 258 | struct io_wqe_acct *acct = io_wqe_get_acct(worker); |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 259 | |
| 260 | atomic_inc(&acct->nr_running); |
| 261 | } |
| 262 | |
Jens Axboe | 685fe7f | 2021-03-08 09:37:51 -0700 | [diff] [blame] | 263 | struct create_worker_data { |
| 264 | struct callback_head work; |
| 265 | struct io_wqe *wqe; |
| 266 | int index; |
| 267 | }; |
| 268 | |
| 269 | static void create_worker_cb(struct callback_head *cb) |
| 270 | { |
| 271 | struct create_worker_data *cwd; |
| 272 | struct io_wq *wq; |
| 273 | |
| 274 | cwd = container_of(cb, struct create_worker_data, work); |
| 275 | wq = cwd->wqe->wq; |
| 276 | create_io_worker(wq, cwd->wqe, cwd->index); |
| 277 | kfree(cwd); |
| 278 | } |
| 279 | |
| 280 | static void io_queue_worker_create(struct io_wqe *wqe, struct io_wqe_acct *acct) |
| 281 | { |
| 282 | struct create_worker_data *cwd; |
| 283 | struct io_wq *wq = wqe->wq; |
| 284 | |
| 285 | /* raced with exit, just ignore create call */ |
| 286 | if (test_bit(IO_WQ_BIT_EXIT, &wq->state)) |
| 287 | goto fail; |
| 288 | |
| 289 | cwd = kmalloc(sizeof(*cwd), GFP_ATOMIC); |
| 290 | if (cwd) { |
| 291 | init_task_work(&cwd->work, create_worker_cb); |
| 292 | cwd->wqe = wqe; |
| 293 | cwd->index = acct->index; |
| 294 | if (!task_work_add(wq->task, &cwd->work, TWA_SIGNAL)) |
| 295 | return; |
| 296 | |
| 297 | kfree(cwd); |
| 298 | } |
| 299 | fail: |
| 300 | atomic_dec(&acct->nr_running); |
| 301 | io_worker_ref_put(wq); |
| 302 | } |
| 303 | |
Jens Axboe | 958234d | 2021-02-17 09:00:57 -0700 | [diff] [blame] | 304 | static void io_wqe_dec_running(struct io_worker *worker) |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 305 | __must_hold(wqe->lock) |
| 306 | { |
Jens Axboe | 958234d | 2021-02-17 09:00:57 -0700 | [diff] [blame] | 307 | struct io_wqe_acct *acct = io_wqe_get_acct(worker); |
| 308 | struct io_wqe *wqe = worker->wqe; |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 309 | |
Jens Axboe | 685fe7f | 2021-03-08 09:37:51 -0700 | [diff] [blame] | 310 | if (!(worker->flags & IO_WORKER_F_UP)) |
| 311 | return; |
| 312 | |
| 313 | if (atomic_dec_and_test(&acct->nr_running) && io_wqe_run_queue(wqe)) { |
| 314 | atomic_inc(&acct->nr_running); |
| 315 | atomic_inc(&wqe->wq->worker_refs); |
| 316 | io_queue_worker_create(wqe, acct); |
| 317 | } |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 318 | } |
| 319 | |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 320 | /* |
| 321 | * Worker will start processing some work. Move it to the busy list, if |
| 322 | * it's currently on the freelist |
| 323 | */ |
| 324 | static void __io_worker_busy(struct io_wqe *wqe, struct io_worker *worker, |
| 325 | struct io_wq_work *work) |
| 326 | __must_hold(wqe->lock) |
| 327 | { |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 328 | bool worker_bound, work_bound; |
| 329 | |
Hao Xu | 417b505 | 2021-04-06 11:08:45 +0800 | [diff] [blame] | 330 | BUILD_BUG_ON((IO_WQ_ACCT_UNBOUND ^ IO_WQ_ACCT_BOUND) != 1); |
| 331 | |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 332 | if (worker->flags & IO_WORKER_F_FREE) { |
| 333 | worker->flags &= ~IO_WORKER_F_FREE; |
| 334 | hlist_nulls_del_init_rcu(&worker->nulls_node); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 335 | } |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 336 | |
| 337 | /* |
| 338 | * If worker is moving from bound to unbound (or vice versa), then |
| 339 | * ensure we update the running accounting. |
| 340 | */ |
Dan Carpenter | b2e9c7d6 | 2019-11-19 09:22:16 +0300 | [diff] [blame] | 341 | worker_bound = (worker->flags & IO_WORKER_F_BOUND) != 0; |
| 342 | work_bound = (work->flags & IO_WQ_WORK_UNBOUND) == 0; |
| 343 | if (worker_bound != work_bound) { |
Hao Xu | 417b505 | 2021-04-06 11:08:45 +0800 | [diff] [blame] | 344 | int index = work_bound ? IO_WQ_ACCT_UNBOUND : IO_WQ_ACCT_BOUND; |
Jens Axboe | 958234d | 2021-02-17 09:00:57 -0700 | [diff] [blame] | 345 | io_wqe_dec_running(worker); |
Hao Xu | 417b505 | 2021-04-06 11:08:45 +0800 | [diff] [blame] | 346 | worker->flags ^= IO_WORKER_F_BOUND; |
| 347 | wqe->acct[index].nr_workers--; |
| 348 | wqe->acct[index ^ 1].nr_workers++; |
Jens Axboe | 958234d | 2021-02-17 09:00:57 -0700 | [diff] [blame] | 349 | io_wqe_inc_running(worker); |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 350 | } |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | /* |
| 354 | * No work, worker going to sleep. Move to freelist, and unuse mm if we |
| 355 | * have one attached. Dropping the mm may potentially sleep, so we drop |
| 356 | * the lock in that case and return success. Since the caller has to |
| 357 | * retry the loop in that case (we changed task state), we don't regrab |
| 358 | * the lock if we return success. |
| 359 | */ |
Jens Axboe | c6d77d9 | 2021-02-15 13:26:34 -0700 | [diff] [blame] | 360 | static void __io_worker_idle(struct io_wqe *wqe, struct io_worker *worker) |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 361 | __must_hold(wqe->lock) |
| 362 | { |
| 363 | if (!(worker->flags & IO_WORKER_F_FREE)) { |
| 364 | worker->flags |= IO_WORKER_F_FREE; |
Jens Axboe | 021d1cd | 2019-11-14 08:00:41 -0700 | [diff] [blame] | 365 | hlist_nulls_add_head_rcu(&worker->nulls_node, &wqe->free_list); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 366 | } |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 367 | } |
| 368 | |
Pavel Begunkov | 60cf46a | 2020-03-14 00:31:05 +0300 | [diff] [blame] | 369 | static inline unsigned int io_get_work_hash(struct io_wq_work *work) |
| 370 | { |
| 371 | return work->flags >> IO_WQ_HASH_SHIFT; |
| 372 | } |
| 373 | |
Jens Axboe | e941894 | 2021-02-19 12:33:30 -0700 | [diff] [blame] | 374 | static void io_wait_on_hash(struct io_wqe *wqe, unsigned int hash) |
| 375 | { |
| 376 | struct io_wq *wq = wqe->wq; |
| 377 | |
| 378 | spin_lock(&wq->hash->wait.lock); |
| 379 | if (list_empty(&wqe->wait.entry)) { |
| 380 | __add_wait_queue(&wq->hash->wait, &wqe->wait); |
| 381 | if (!test_bit(hash, &wq->hash->map)) { |
| 382 | __set_current_state(TASK_RUNNING); |
| 383 | list_del_init(&wqe->wait.entry); |
| 384 | } |
| 385 | } |
| 386 | spin_unlock(&wq->hash->wait.lock); |
| 387 | } |
| 388 | |
Pavel Begunkov | 60cf46a | 2020-03-14 00:31:05 +0300 | [diff] [blame] | 389 | static struct io_wq_work *io_get_next_work(struct io_wqe *wqe) |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 390 | __must_hold(wqe->lock) |
| 391 | { |
Jens Axboe | 6206f0e | 2019-11-26 11:59:32 -0700 | [diff] [blame] | 392 | struct io_wq_work_node *node, *prev; |
Pavel Begunkov | 86f3cd1 | 2020-03-23 22:57:22 +0300 | [diff] [blame] | 393 | struct io_wq_work *work, *tail; |
Jens Axboe | e941894 | 2021-02-19 12:33:30 -0700 | [diff] [blame] | 394 | unsigned int stall_hash = -1U; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 395 | |
Jens Axboe | 6206f0e | 2019-11-26 11:59:32 -0700 | [diff] [blame] | 396 | wq_list_for_each(node, prev, &wqe->work_list) { |
Jens Axboe | e941894 | 2021-02-19 12:33:30 -0700 | [diff] [blame] | 397 | unsigned int hash; |
| 398 | |
Jens Axboe | 6206f0e | 2019-11-26 11:59:32 -0700 | [diff] [blame] | 399 | work = container_of(node, struct io_wq_work, list); |
| 400 | |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 401 | /* not hashed, can run anytime */ |
Pavel Begunkov | 8766dd5 | 2020-03-14 00:31:04 +0300 | [diff] [blame] | 402 | if (!io_wq_is_hashed(work)) { |
Pavel Begunkov | 86f3cd1 | 2020-03-23 22:57:22 +0300 | [diff] [blame] | 403 | wq_list_del(&wqe->work_list, node, prev); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 404 | return work; |
| 405 | } |
| 406 | |
Pavel Begunkov | 60cf46a | 2020-03-14 00:31:05 +0300 | [diff] [blame] | 407 | hash = io_get_work_hash(work); |
Jens Axboe | e941894 | 2021-02-19 12:33:30 -0700 | [diff] [blame] | 408 | /* all items with this hash lie in [work, tail] */ |
| 409 | tail = wqe->hash_tail[hash]; |
| 410 | |
| 411 | /* hashed, can run if not already running */ |
| 412 | if (!test_and_set_bit(hash, &wqe->wq->hash->map)) { |
Pavel Begunkov | 86f3cd1 | 2020-03-23 22:57:22 +0300 | [diff] [blame] | 413 | wqe->hash_tail[hash] = NULL; |
| 414 | wq_list_cut(&wqe->work_list, &tail->list, prev); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 415 | return work; |
| 416 | } |
Jens Axboe | e941894 | 2021-02-19 12:33:30 -0700 | [diff] [blame] | 417 | if (stall_hash == -1U) |
| 418 | stall_hash = hash; |
| 419 | /* fast forward to a next hash, for-each will fix up @prev */ |
| 420 | node = &tail->list; |
| 421 | } |
| 422 | |
| 423 | if (stall_hash != -1U) { |
| 424 | raw_spin_unlock(&wqe->lock); |
| 425 | io_wait_on_hash(wqe, stall_hash); |
| 426 | raw_spin_lock(&wqe->lock); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | return NULL; |
| 430 | } |
| 431 | |
Jens Axboe | 00ddff4 | 2021-03-21 07:06:56 -0600 | [diff] [blame] | 432 | static bool io_flush_signals(void) |
Jens Axboe | cccf0ee | 2020-01-27 16:34:48 -0700 | [diff] [blame] | 433 | { |
Jens Axboe | 0b8cfa9 | 2021-03-21 14:16:08 -0600 | [diff] [blame] | 434 | if (unlikely(test_thread_flag(TIF_NOTIFY_SIGNAL))) { |
Jens Axboe | 00ddff4 | 2021-03-21 07:06:56 -0600 | [diff] [blame] | 435 | __set_current_state(TASK_RUNNING); |
Jens Axboe | 0b8cfa9 | 2021-03-21 14:16:08 -0600 | [diff] [blame] | 436 | tracehook_notify_signal(); |
Jens Axboe | 00ddff4 | 2021-03-21 07:06:56 -0600 | [diff] [blame] | 437 | return true; |
Jens Axboe | cccf0ee | 2020-01-27 16:34:48 -0700 | [diff] [blame] | 438 | } |
Jens Axboe | 00ddff4 | 2021-03-21 07:06:56 -0600 | [diff] [blame] | 439 | return false; |
Pavel Begunkov | dc026a7 | 2020-03-04 16:14:09 +0300 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | static void io_assign_current_work(struct io_worker *worker, |
| 443 | struct io_wq_work *work) |
| 444 | { |
Pavel Begunkov | d78298e | 2020-03-14 00:31:03 +0300 | [diff] [blame] | 445 | if (work) { |
Jens Axboe | 3bfe610 | 2021-02-16 14:15:30 -0700 | [diff] [blame] | 446 | io_flush_signals(); |
Pavel Begunkov | d78298e | 2020-03-14 00:31:03 +0300 | [diff] [blame] | 447 | cond_resched(); |
| 448 | } |
Pavel Begunkov | dc026a7 | 2020-03-04 16:14:09 +0300 | [diff] [blame] | 449 | |
| 450 | spin_lock_irq(&worker->lock); |
| 451 | worker->cur_work = work; |
| 452 | spin_unlock_irq(&worker->lock); |
| 453 | } |
| 454 | |
Pavel Begunkov | 60cf46a | 2020-03-14 00:31:05 +0300 | [diff] [blame] | 455 | static void io_wqe_enqueue(struct io_wqe *wqe, struct io_wq_work *work); |
| 456 | |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 457 | static void io_worker_handle_work(struct io_worker *worker) |
| 458 | __releases(wqe->lock) |
| 459 | { |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 460 | struct io_wqe *wqe = worker->wqe; |
| 461 | struct io_wq *wq = wqe->wq; |
Pavel Begunkov | c60eb04 | 2021-04-08 01:54:42 +0100 | [diff] [blame] | 462 | bool do_kill = test_bit(IO_WQ_BIT_EXIT, &wq->state); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 463 | |
| 464 | do { |
Pavel Begunkov | 86f3cd1 | 2020-03-23 22:57:22 +0300 | [diff] [blame] | 465 | struct io_wq_work *work; |
Pavel Begunkov | f462fd3 | 2020-03-04 16:14:11 +0300 | [diff] [blame] | 466 | get_next: |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 467 | /* |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 468 | * If we got some work, mark us as busy. If we didn't, but |
| 469 | * the list isn't empty, it means we stalled on hashed work. |
| 470 | * Mark us stalled so we don't keep looking for work when we |
| 471 | * can't make progress, any work completion or insertion will |
| 472 | * clear the stalled flag. |
| 473 | */ |
Pavel Begunkov | 60cf46a | 2020-03-14 00:31:05 +0300 | [diff] [blame] | 474 | work = io_get_next_work(wqe); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 475 | if (work) |
| 476 | __io_worker_busy(wqe, worker, work); |
Jens Axboe | 6206f0e | 2019-11-26 11:59:32 -0700 | [diff] [blame] | 477 | else if (!wq_list_empty(&wqe->work_list)) |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 478 | wqe->flags |= IO_WQE_FLAG_STALLED; |
| 479 | |
Sebastian Andrzej Siewior | 95da846 | 2020-09-01 10:41:46 +0200 | [diff] [blame] | 480 | raw_spin_unlock_irq(&wqe->lock); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 481 | if (!work) |
| 482 | break; |
Pavel Begunkov | 58e3931 | 2020-03-04 16:14:10 +0300 | [diff] [blame] | 483 | io_assign_current_work(worker, work); |
Jens Axboe | e941894 | 2021-02-19 12:33:30 -0700 | [diff] [blame] | 484 | __set_current_state(TASK_RUNNING); |
Jens Axboe | 36c2f92 | 2019-11-13 09:43:34 -0700 | [diff] [blame] | 485 | |
Pavel Begunkov | dc026a7 | 2020-03-04 16:14:09 +0300 | [diff] [blame] | 486 | /* handle a whole dependent link */ |
| 487 | do { |
Pavel Begunkov | 5280f7e | 2021-02-04 13:52:08 +0000 | [diff] [blame] | 488 | struct io_wq_work *next_hashed, *linked; |
Pavel Begunkov | b089ed39 | 2020-07-25 14:42:00 +0300 | [diff] [blame] | 489 | unsigned int hash = io_get_work_hash(work); |
Hillf Danton | fd1c4bc | 2019-12-24 09:14:29 -0700 | [diff] [blame] | 490 | |
Pavel Begunkov | 86f3cd1 | 2020-03-23 22:57:22 +0300 | [diff] [blame] | 491 | next_hashed = wq_next_work(work); |
Pavel Begunkov | c60eb04 | 2021-04-08 01:54:42 +0100 | [diff] [blame] | 492 | |
| 493 | if (unlikely(do_kill) && (work->flags & IO_WQ_WORK_UNBOUND)) |
| 494 | work->flags |= IO_WQ_WORK_CANCEL; |
Pavel Begunkov | 5280f7e | 2021-02-04 13:52:08 +0000 | [diff] [blame] | 495 | wq->do_work(work); |
| 496 | io_assign_current_work(worker, NULL); |
Jens Axboe | 36c2f92 | 2019-11-13 09:43:34 -0700 | [diff] [blame] | 497 | |
Pavel Begunkov | 5280f7e | 2021-02-04 13:52:08 +0000 | [diff] [blame] | 498 | linked = wq->free_work(work); |
Pavel Begunkov | 86f3cd1 | 2020-03-23 22:57:22 +0300 | [diff] [blame] | 499 | work = next_hashed; |
| 500 | if (!work && linked && !io_wq_is_hashed(linked)) { |
| 501 | work = linked; |
| 502 | linked = NULL; |
| 503 | } |
| 504 | io_assign_current_work(worker, work); |
Pavel Begunkov | 86f3cd1 | 2020-03-23 22:57:22 +0300 | [diff] [blame] | 505 | if (linked) |
| 506 | io_wqe_enqueue(wqe, linked); |
| 507 | |
| 508 | if (hash != -1U && !next_hashed) { |
Jens Axboe | e941894 | 2021-02-19 12:33:30 -0700 | [diff] [blame] | 509 | clear_bit(hash, &wq->hash->map); |
| 510 | if (wq_has_sleeper(&wq->hash->wait)) |
| 511 | wake_up(&wq->hash->wait); |
Sebastian Andrzej Siewior | 95da846 | 2020-09-01 10:41:46 +0200 | [diff] [blame] | 512 | raw_spin_lock_irq(&wqe->lock); |
Pavel Begunkov | dc026a7 | 2020-03-04 16:14:09 +0300 | [diff] [blame] | 513 | wqe->flags &= ~IO_WQE_FLAG_STALLED; |
Pavel Begunkov | f462fd3 | 2020-03-04 16:14:11 +0300 | [diff] [blame] | 514 | /* skip unnecessary unlock-lock wqe->lock */ |
| 515 | if (!work) |
| 516 | goto get_next; |
Sebastian Andrzej Siewior | 95da846 | 2020-09-01 10:41:46 +0200 | [diff] [blame] | 517 | raw_spin_unlock_irq(&wqe->lock); |
Pavel Begunkov | dc026a7 | 2020-03-04 16:14:09 +0300 | [diff] [blame] | 518 | } |
Pavel Begunkov | 58e3931 | 2020-03-04 16:14:10 +0300 | [diff] [blame] | 519 | } while (work); |
Jens Axboe | 36c2f92 | 2019-11-13 09:43:34 -0700 | [diff] [blame] | 520 | |
Sebastian Andrzej Siewior | 95da846 | 2020-09-01 10:41:46 +0200 | [diff] [blame] | 521 | raw_spin_lock_irq(&wqe->lock); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 522 | } while (1); |
| 523 | } |
| 524 | |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 525 | static int io_wqe_worker(void *data) |
| 526 | { |
| 527 | struct io_worker *worker = data; |
| 528 | struct io_wqe *wqe = worker->wqe; |
| 529 | struct io_wq *wq = wqe->wq; |
Jens Axboe | 46fe18b | 2021-03-04 12:39:36 -0700 | [diff] [blame] | 530 | char buf[TASK_COMM_LEN]; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 531 | |
Jens Axboe | 46fe18b | 2021-03-04 12:39:36 -0700 | [diff] [blame] | 532 | worker->flags |= (IO_WORKER_F_UP | IO_WORKER_F_RUNNING); |
Jens Axboe | 46fe18b | 2021-03-04 12:39:36 -0700 | [diff] [blame] | 533 | |
Jens Axboe | 685fe7f | 2021-03-08 09:37:51 -0700 | [diff] [blame] | 534 | snprintf(buf, sizeof(buf), "iou-wrk-%d", wq->task->pid); |
Jens Axboe | 46fe18b | 2021-03-04 12:39:36 -0700 | [diff] [blame] | 535 | set_task_comm(current, buf); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 536 | |
| 537 | while (!test_bit(IO_WQ_BIT_EXIT, &wq->state)) { |
Jens Axboe | 16efa4f | 2021-03-12 20:26:13 -0700 | [diff] [blame] | 538 | long ret; |
| 539 | |
Jens Axboe | 506d95f | 2019-12-07 21:03:59 -0700 | [diff] [blame] | 540 | set_current_state(TASK_INTERRUPTIBLE); |
Jens Axboe | e995d51 | 2019-12-07 21:06:46 -0700 | [diff] [blame] | 541 | loop: |
Sebastian Andrzej Siewior | 95da846 | 2020-09-01 10:41:46 +0200 | [diff] [blame] | 542 | raw_spin_lock_irq(&wqe->lock); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 543 | if (io_wqe_run_queue(wqe)) { |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 544 | io_worker_handle_work(worker); |
Jens Axboe | e995d51 | 2019-12-07 21:06:46 -0700 | [diff] [blame] | 545 | goto loop; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 546 | } |
Jens Axboe | c6d77d9 | 2021-02-15 13:26:34 -0700 | [diff] [blame] | 547 | __io_worker_idle(wqe, worker); |
Sebastian Andrzej Siewior | 95da846 | 2020-09-01 10:41:46 +0200 | [diff] [blame] | 548 | raw_spin_unlock_irq(&wqe->lock); |
Jens Axboe | 00ddff4 | 2021-03-21 07:06:56 -0600 | [diff] [blame] | 549 | if (io_flush_signals()) |
| 550 | continue; |
Jens Axboe | 16efa4f | 2021-03-12 20:26:13 -0700 | [diff] [blame] | 551 | ret = schedule_timeout(WORKER_IDLE_TIMEOUT); |
Jens Axboe | dbe1bdb | 2021-03-25 18:16:06 -0600 | [diff] [blame] | 552 | if (signal_pending(current)) { |
| 553 | struct ksignal ksig; |
| 554 | |
| 555 | if (!get_signal(&ksig)) |
| 556 | continue; |
Jens Axboe | 3bfe610 | 2021-02-16 14:15:30 -0700 | [diff] [blame] | 557 | break; |
Jens Axboe | dbe1bdb | 2021-03-25 18:16:06 -0600 | [diff] [blame] | 558 | } |
| 559 | if (ret) |
| 560 | continue; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 561 | /* timed out, exit unless we're the fixed worker */ |
Pavel Begunkov | 769e683 | 2021-06-14 02:36:16 +0100 | [diff] [blame^] | 562 | if (!(worker->flags & IO_WORKER_F_FIXED)) |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 563 | break; |
| 564 | } |
| 565 | |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 566 | if (test_bit(IO_WQ_BIT_EXIT, &wq->state)) { |
Sebastian Andrzej Siewior | 95da846 | 2020-09-01 10:41:46 +0200 | [diff] [blame] | 567 | raw_spin_lock_irq(&wqe->lock); |
Jens Axboe | 6206f0e | 2019-11-26 11:59:32 -0700 | [diff] [blame] | 568 | if (!wq_list_empty(&wqe->work_list)) |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 569 | io_worker_handle_work(worker); |
| 570 | else |
Sebastian Andrzej Siewior | 95da846 | 2020-09-01 10:41:46 +0200 | [diff] [blame] | 571 | raw_spin_unlock_irq(&wqe->lock); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | io_worker_exit(worker); |
| 575 | return 0; |
| 576 | } |
| 577 | |
| 578 | /* |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 579 | * Called when a worker is scheduled in. Mark us as currently running. |
| 580 | */ |
| 581 | void io_wq_worker_running(struct task_struct *tsk) |
| 582 | { |
Jens Axboe | 3bfe610 | 2021-02-16 14:15:30 -0700 | [diff] [blame] | 583 | struct io_worker *worker = tsk->pf_io_worker; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 584 | |
Jens Axboe | 3bfe610 | 2021-02-16 14:15:30 -0700 | [diff] [blame] | 585 | if (!worker) |
| 586 | return; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 587 | if (!(worker->flags & IO_WORKER_F_UP)) |
| 588 | return; |
| 589 | if (worker->flags & IO_WORKER_F_RUNNING) |
| 590 | return; |
| 591 | worker->flags |= IO_WORKER_F_RUNNING; |
Jens Axboe | 958234d | 2021-02-17 09:00:57 -0700 | [diff] [blame] | 592 | io_wqe_inc_running(worker); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 593 | } |
| 594 | |
| 595 | /* |
| 596 | * Called when worker is going to sleep. If there are no workers currently |
Jens Axboe | 685fe7f | 2021-03-08 09:37:51 -0700 | [diff] [blame] | 597 | * running and we have work pending, wake up a free one or create a new one. |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 598 | */ |
| 599 | void io_wq_worker_sleeping(struct task_struct *tsk) |
| 600 | { |
Jens Axboe | 3bfe610 | 2021-02-16 14:15:30 -0700 | [diff] [blame] | 601 | struct io_worker *worker = tsk->pf_io_worker; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 602 | |
Jens Axboe | 3bfe610 | 2021-02-16 14:15:30 -0700 | [diff] [blame] | 603 | if (!worker) |
| 604 | return; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 605 | if (!(worker->flags & IO_WORKER_F_UP)) |
| 606 | return; |
| 607 | if (!(worker->flags & IO_WORKER_F_RUNNING)) |
| 608 | return; |
| 609 | |
| 610 | worker->flags &= ~IO_WORKER_F_RUNNING; |
| 611 | |
Jens Axboe | 3bfe610 | 2021-02-16 14:15:30 -0700 | [diff] [blame] | 612 | raw_spin_lock_irq(&worker->wqe->lock); |
Jens Axboe | 958234d | 2021-02-17 09:00:57 -0700 | [diff] [blame] | 613 | io_wqe_dec_running(worker); |
Jens Axboe | 3bfe610 | 2021-02-16 14:15:30 -0700 | [diff] [blame] | 614 | raw_spin_unlock_irq(&worker->wqe->lock); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 615 | } |
| 616 | |
Jens Axboe | 685fe7f | 2021-03-08 09:37:51 -0700 | [diff] [blame] | 617 | static void create_io_worker(struct io_wq *wq, struct io_wqe *wqe, int index) |
Jens Axboe | 3bfe610 | 2021-02-16 14:15:30 -0700 | [diff] [blame] | 618 | { |
Jens Axboe | 46fe18b | 2021-03-04 12:39:36 -0700 | [diff] [blame] | 619 | struct io_wqe_acct *acct = &wqe->acct[index]; |
Jens Axboe | 3bfe610 | 2021-02-16 14:15:30 -0700 | [diff] [blame] | 620 | struct io_worker *worker; |
Jens Axboe | 46fe18b | 2021-03-04 12:39:36 -0700 | [diff] [blame] | 621 | struct task_struct *tsk; |
Jens Axboe | 3bfe610 | 2021-02-16 14:15:30 -0700 | [diff] [blame] | 622 | |
Jens Axboe | 8b3e78b | 2021-02-23 15:34:06 -0700 | [diff] [blame] | 623 | __set_current_state(TASK_RUNNING); |
| 624 | |
Jens Axboe | 3bfe610 | 2021-02-16 14:15:30 -0700 | [diff] [blame] | 625 | worker = kzalloc_node(sizeof(*worker), GFP_KERNEL, wqe->node); |
| 626 | if (!worker) |
Jens Axboe | 685fe7f | 2021-03-08 09:37:51 -0700 | [diff] [blame] | 627 | goto fail; |
Jens Axboe | 3bfe610 | 2021-02-16 14:15:30 -0700 | [diff] [blame] | 628 | |
| 629 | refcount_set(&worker->ref, 1); |
| 630 | worker->nulls_node.pprev = NULL; |
| 631 | worker->wqe = wqe; |
| 632 | spin_lock_init(&worker->lock); |
Jens Axboe | eb2de94 | 2021-02-23 19:59:06 -0700 | [diff] [blame] | 633 | init_completion(&worker->ref_done); |
Jens Axboe | 3bfe610 | 2021-02-16 14:15:30 -0700 | [diff] [blame] | 634 | |
Jens Axboe | 46fe18b | 2021-03-04 12:39:36 -0700 | [diff] [blame] | 635 | tsk = create_io_thread(io_wqe_worker, worker, wqe->node); |
| 636 | if (IS_ERR(tsk)) { |
Jens Axboe | 3bfe610 | 2021-02-16 14:15:30 -0700 | [diff] [blame] | 637 | kfree(worker); |
Jens Axboe | 685fe7f | 2021-03-08 09:37:51 -0700 | [diff] [blame] | 638 | fail: |
| 639 | atomic_dec(&acct->nr_running); |
| 640 | io_worker_ref_put(wq); |
| 641 | return; |
Jens Axboe | 3bfe610 | 2021-02-16 14:15:30 -0700 | [diff] [blame] | 642 | } |
Jens Axboe | 46fe18b | 2021-03-04 12:39:36 -0700 | [diff] [blame] | 643 | |
| 644 | tsk->pf_io_worker = worker; |
| 645 | worker->task = tsk; |
| 646 | set_cpus_allowed_ptr(tsk, cpumask_of_node(wqe->node)); |
Jens Axboe | e22bc9b | 2021-03-09 19:49:02 -0700 | [diff] [blame] | 647 | tsk->flags |= PF_NO_SETAFFINITY; |
Jens Axboe | 46fe18b | 2021-03-04 12:39:36 -0700 | [diff] [blame] | 648 | |
| 649 | raw_spin_lock_irq(&wqe->lock); |
| 650 | hlist_nulls_add_head_rcu(&worker->nulls_node, &wqe->free_list); |
| 651 | list_add_tail_rcu(&worker->all_list, &wqe->all_list); |
| 652 | worker->flags |= IO_WORKER_F_FREE; |
| 653 | if (index == IO_WQ_ACCT_BOUND) |
| 654 | worker->flags |= IO_WORKER_F_BOUND; |
| 655 | if (!acct->nr_workers && (worker->flags & IO_WORKER_F_BOUND)) |
| 656 | worker->flags |= IO_WORKER_F_FIXED; |
| 657 | acct->nr_workers++; |
| 658 | raw_spin_unlock_irq(&wqe->lock); |
| 659 | wake_up_new_task(tsk); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 660 | } |
| 661 | |
Hillf Danton | c4068bf | 2020-09-26 21:26:55 +0800 | [diff] [blame] | 662 | /* |
| 663 | * Iterate the passed in list and call the specific function for each |
| 664 | * worker that isn't exiting |
| 665 | */ |
| 666 | static bool io_wq_for_each_worker(struct io_wqe *wqe, |
| 667 | bool (*func)(struct io_worker *, void *), |
| 668 | void *data) |
| 669 | { |
| 670 | struct io_worker *worker; |
| 671 | bool ret = false; |
| 672 | |
| 673 | list_for_each_entry_rcu(worker, &wqe->all_list, all_list) { |
| 674 | if (io_worker_get(worker)) { |
| 675 | /* no task if node is/was offline */ |
| 676 | if (worker->task) |
| 677 | ret = func(worker, data); |
| 678 | io_worker_release(worker); |
| 679 | if (ret) |
| 680 | break; |
| 681 | } |
| 682 | } |
| 683 | |
| 684 | return ret; |
| 685 | } |
| 686 | |
| 687 | static bool io_wq_worker_wake(struct io_worker *worker, void *data) |
| 688 | { |
Jens Axboe | 46fe18b | 2021-03-04 12:39:36 -0700 | [diff] [blame] | 689 | set_notify_signal(worker->task); |
Hillf Danton | c4068bf | 2020-09-26 21:26:55 +0800 | [diff] [blame] | 690 | wake_up_process(worker->task); |
| 691 | return false; |
| 692 | } |
| 693 | |
Jens Axboe | f012725 | 2021-03-03 15:47:04 -0700 | [diff] [blame] | 694 | static bool io_wq_work_match_all(struct io_wq_work *work, void *data) |
| 695 | { |
| 696 | return true; |
| 697 | } |
| 698 | |
Pavel Begunkov | e9fd939 | 2020-03-04 16:14:12 +0300 | [diff] [blame] | 699 | static void io_run_cancel(struct io_wq_work *work, struct io_wqe *wqe) |
Pavel Begunkov | fc04c39 | 2020-03-01 19:18:19 +0300 | [diff] [blame] | 700 | { |
Pavel Begunkov | e9fd939 | 2020-03-04 16:14:12 +0300 | [diff] [blame] | 701 | struct io_wq *wq = wqe->wq; |
| 702 | |
Pavel Begunkov | fc04c39 | 2020-03-01 19:18:19 +0300 | [diff] [blame] | 703 | do { |
Pavel Begunkov | fc04c39 | 2020-03-01 19:18:19 +0300 | [diff] [blame] | 704 | work->flags |= IO_WQ_WORK_CANCEL; |
Pavel Begunkov | 5280f7e | 2021-02-04 13:52:08 +0000 | [diff] [blame] | 705 | wq->do_work(work); |
| 706 | work = wq->free_work(work); |
Pavel Begunkov | fc04c39 | 2020-03-01 19:18:19 +0300 | [diff] [blame] | 707 | } while (work); |
| 708 | } |
| 709 | |
Pavel Begunkov | 86f3cd1 | 2020-03-23 22:57:22 +0300 | [diff] [blame] | 710 | static void io_wqe_insert_work(struct io_wqe *wqe, struct io_wq_work *work) |
| 711 | { |
| 712 | unsigned int hash; |
| 713 | struct io_wq_work *tail; |
| 714 | |
| 715 | if (!io_wq_is_hashed(work)) { |
| 716 | append: |
| 717 | wq_list_add_tail(&work->list, &wqe->work_list); |
| 718 | return; |
| 719 | } |
| 720 | |
| 721 | hash = io_get_work_hash(work); |
| 722 | tail = wqe->hash_tail[hash]; |
| 723 | wqe->hash_tail[hash] = work; |
| 724 | if (!tail) |
| 725 | goto append; |
| 726 | |
| 727 | wq_list_add_after(&work->list, &tail->list, &wqe->work_list); |
| 728 | } |
| 729 | |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 730 | static void io_wqe_enqueue(struct io_wqe *wqe, struct io_wq_work *work) |
| 731 | { |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 732 | struct io_wqe_acct *acct = io_work_get_acct(wqe, work); |
Jens Axboe | 895e2ca | 2019-12-17 08:46:33 -0700 | [diff] [blame] | 733 | int work_flags; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 734 | unsigned long flags; |
| 735 | |
Jens Axboe | 685fe7f | 2021-03-08 09:37:51 -0700 | [diff] [blame] | 736 | if (test_bit(IO_WQ_BIT_EXIT, &wqe->wq->state)) { |
yangerkun | 70e3512 | 2021-03-09 11:04:10 +0800 | [diff] [blame] | 737 | io_run_cancel(work, wqe); |
Jens Axboe | 4fb6ac3 | 2021-02-25 10:17:09 -0700 | [diff] [blame] | 738 | return; |
| 739 | } |
| 740 | |
Jens Axboe | 895e2ca | 2019-12-17 08:46:33 -0700 | [diff] [blame] | 741 | work_flags = work->flags; |
Sebastian Andrzej Siewior | 95da846 | 2020-09-01 10:41:46 +0200 | [diff] [blame] | 742 | raw_spin_lock_irqsave(&wqe->lock, flags); |
Pavel Begunkov | 86f3cd1 | 2020-03-23 22:57:22 +0300 | [diff] [blame] | 743 | io_wqe_insert_work(wqe, work); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 744 | wqe->flags &= ~IO_WQE_FLAG_STALLED; |
Sebastian Andrzej Siewior | 95da846 | 2020-09-01 10:41:46 +0200 | [diff] [blame] | 745 | raw_spin_unlock_irqrestore(&wqe->lock, flags); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 746 | |
Jens Axboe | 895e2ca | 2019-12-17 08:46:33 -0700 | [diff] [blame] | 747 | if ((work_flags & IO_WQ_WORK_CONCURRENT) || |
| 748 | !atomic_read(&acct->nr_running)) |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 749 | io_wqe_wake_worker(wqe, acct); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 750 | } |
| 751 | |
| 752 | void io_wq_enqueue(struct io_wq *wq, struct io_wq_work *work) |
| 753 | { |
| 754 | struct io_wqe *wqe = wq->wqes[numa_node_id()]; |
| 755 | |
| 756 | io_wqe_enqueue(wqe, work); |
| 757 | } |
| 758 | |
| 759 | /* |
Pavel Begunkov | 8766dd5 | 2020-03-14 00:31:04 +0300 | [diff] [blame] | 760 | * Work items that hash to the same value will not be done in parallel. |
| 761 | * Used to limit concurrent writes, generally hashed by inode. |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 762 | */ |
Pavel Begunkov | 8766dd5 | 2020-03-14 00:31:04 +0300 | [diff] [blame] | 763 | void io_wq_hash_work(struct io_wq_work *work, void *val) |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 764 | { |
Pavel Begunkov | 8766dd5 | 2020-03-14 00:31:04 +0300 | [diff] [blame] | 765 | unsigned int bit; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 766 | |
| 767 | bit = hash_ptr(val, IO_WQ_HASH_ORDER); |
| 768 | work->flags |= (IO_WQ_WORK_HASHED | (bit << IO_WQ_HASH_SHIFT)); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 769 | } |
| 770 | |
Pavel Begunkov | 2293b41 | 2020-03-07 01:15:39 +0300 | [diff] [blame] | 771 | static bool io_wq_worker_cancel(struct io_worker *worker, void *data) |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 772 | { |
Pavel Begunkov | 2293b41 | 2020-03-07 01:15:39 +0300 | [diff] [blame] | 773 | struct io_cb_cancel_data *match = data; |
Jens Axboe | 6f72653 | 2019-11-05 13:51:51 -0700 | [diff] [blame] | 774 | unsigned long flags; |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 775 | |
| 776 | /* |
| 777 | * Hold the lock to avoid ->cur_work going out of scope, caller |
Jens Axboe | 36c2f92 | 2019-11-13 09:43:34 -0700 | [diff] [blame] | 778 | * may dereference the passed in work. |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 779 | */ |
Jens Axboe | 36c2f92 | 2019-11-13 09:43:34 -0700 | [diff] [blame] | 780 | spin_lock_irqsave(&worker->lock, flags); |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 781 | if (worker->cur_work && |
Pavel Begunkov | 2293b41 | 2020-03-07 01:15:39 +0300 | [diff] [blame] | 782 | match->fn(worker->cur_work, match->data)) { |
Jens Axboe | 3bfe610 | 2021-02-16 14:15:30 -0700 | [diff] [blame] | 783 | set_notify_signal(worker->task); |
Pavel Begunkov | 4f26bda | 2020-06-15 10:24:03 +0300 | [diff] [blame] | 784 | match->nr_running++; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 785 | } |
Jens Axboe | 36c2f92 | 2019-11-13 09:43:34 -0700 | [diff] [blame] | 786 | spin_unlock_irqrestore(&worker->lock, flags); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 787 | |
Pavel Begunkov | 4f26bda | 2020-06-15 10:24:03 +0300 | [diff] [blame] | 788 | return match->nr_running && !match->cancel_all; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 789 | } |
| 790 | |
Pavel Begunkov | 204361a | 2020-08-23 20:33:10 +0300 | [diff] [blame] | 791 | static inline void io_wqe_remove_pending(struct io_wqe *wqe, |
| 792 | struct io_wq_work *work, |
| 793 | struct io_wq_work_node *prev) |
| 794 | { |
| 795 | unsigned int hash = io_get_work_hash(work); |
| 796 | struct io_wq_work *prev_work = NULL; |
| 797 | |
| 798 | if (io_wq_is_hashed(work) && work == wqe->hash_tail[hash]) { |
| 799 | if (prev) |
| 800 | prev_work = container_of(prev, struct io_wq_work, list); |
| 801 | if (prev_work && io_get_work_hash(prev_work) == hash) |
| 802 | wqe->hash_tail[hash] = prev_work; |
| 803 | else |
| 804 | wqe->hash_tail[hash] = NULL; |
| 805 | } |
| 806 | wq_list_del(&wqe->work_list, &work->list, prev); |
| 807 | } |
| 808 | |
Pavel Begunkov | 4f26bda | 2020-06-15 10:24:03 +0300 | [diff] [blame] | 809 | static void io_wqe_cancel_pending_work(struct io_wqe *wqe, |
Pavel Begunkov | f4c2665 | 2020-06-15 10:24:02 +0300 | [diff] [blame] | 810 | struct io_cb_cancel_data *match) |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 811 | { |
Jens Axboe | 6206f0e | 2019-11-26 11:59:32 -0700 | [diff] [blame] | 812 | struct io_wq_work_node *node, *prev; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 813 | struct io_wq_work *work; |
Jens Axboe | 6f72653 | 2019-11-05 13:51:51 -0700 | [diff] [blame] | 814 | unsigned long flags; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 815 | |
Pavel Begunkov | 4f26bda | 2020-06-15 10:24:03 +0300 | [diff] [blame] | 816 | retry: |
Sebastian Andrzej Siewior | 95da846 | 2020-09-01 10:41:46 +0200 | [diff] [blame] | 817 | raw_spin_lock_irqsave(&wqe->lock, flags); |
Jens Axboe | 6206f0e | 2019-11-26 11:59:32 -0700 | [diff] [blame] | 818 | wq_list_for_each(node, prev, &wqe->work_list) { |
| 819 | work = container_of(node, struct io_wq_work, list); |
Pavel Begunkov | 4f26bda | 2020-06-15 10:24:03 +0300 | [diff] [blame] | 820 | if (!match->fn(work, match->data)) |
| 821 | continue; |
Pavel Begunkov | 204361a | 2020-08-23 20:33:10 +0300 | [diff] [blame] | 822 | io_wqe_remove_pending(wqe, work, prev); |
Sebastian Andrzej Siewior | 95da846 | 2020-09-01 10:41:46 +0200 | [diff] [blame] | 823 | raw_spin_unlock_irqrestore(&wqe->lock, flags); |
Pavel Begunkov | 4f26bda | 2020-06-15 10:24:03 +0300 | [diff] [blame] | 824 | io_run_cancel(work, wqe); |
| 825 | match->nr_pending++; |
| 826 | if (!match->cancel_all) |
| 827 | return; |
| 828 | |
| 829 | /* not safe to continue after unlock */ |
| 830 | goto retry; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 831 | } |
Sebastian Andrzej Siewior | 95da846 | 2020-09-01 10:41:46 +0200 | [diff] [blame] | 832 | raw_spin_unlock_irqrestore(&wqe->lock, flags); |
Pavel Begunkov | f4c2665 | 2020-06-15 10:24:02 +0300 | [diff] [blame] | 833 | } |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 834 | |
Pavel Begunkov | 4f26bda | 2020-06-15 10:24:03 +0300 | [diff] [blame] | 835 | static void io_wqe_cancel_running_work(struct io_wqe *wqe, |
Pavel Begunkov | f4c2665 | 2020-06-15 10:24:02 +0300 | [diff] [blame] | 836 | struct io_cb_cancel_data *match) |
| 837 | { |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 838 | rcu_read_lock(); |
Pavel Begunkov | 4f26bda | 2020-06-15 10:24:03 +0300 | [diff] [blame] | 839 | io_wq_for_each_worker(wqe, io_wq_worker_cancel, match); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 840 | rcu_read_unlock(); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 841 | } |
| 842 | |
Pavel Begunkov | 2293b41 | 2020-03-07 01:15:39 +0300 | [diff] [blame] | 843 | enum io_wq_cancel io_wq_cancel_cb(struct io_wq *wq, work_cancel_fn *cancel, |
Pavel Begunkov | 4f26bda | 2020-06-15 10:24:03 +0300 | [diff] [blame] | 844 | void *data, bool cancel_all) |
Pavel Begunkov | 2293b41 | 2020-03-07 01:15:39 +0300 | [diff] [blame] | 845 | { |
| 846 | struct io_cb_cancel_data match = { |
Pavel Begunkov | 4f26bda | 2020-06-15 10:24:03 +0300 | [diff] [blame] | 847 | .fn = cancel, |
| 848 | .data = data, |
| 849 | .cancel_all = cancel_all, |
Pavel Begunkov | 2293b41 | 2020-03-07 01:15:39 +0300 | [diff] [blame] | 850 | }; |
Pavel Begunkov | 2293b41 | 2020-03-07 01:15:39 +0300 | [diff] [blame] | 851 | int node; |
| 852 | |
Pavel Begunkov | f4c2665 | 2020-06-15 10:24:02 +0300 | [diff] [blame] | 853 | /* |
| 854 | * First check pending list, if we're lucky we can just remove it |
| 855 | * from there. CANCEL_OK means that the work is returned as-new, |
| 856 | * no completion will be posted for it. |
| 857 | */ |
Pavel Begunkov | 2293b41 | 2020-03-07 01:15:39 +0300 | [diff] [blame] | 858 | for_each_node(node) { |
| 859 | struct io_wqe *wqe = wq->wqes[node]; |
| 860 | |
Pavel Begunkov | 4f26bda | 2020-06-15 10:24:03 +0300 | [diff] [blame] | 861 | io_wqe_cancel_pending_work(wqe, &match); |
| 862 | if (match.nr_pending && !match.cancel_all) |
Pavel Begunkov | f4c2665 | 2020-06-15 10:24:02 +0300 | [diff] [blame] | 863 | return IO_WQ_CANCEL_OK; |
Pavel Begunkov | 2293b41 | 2020-03-07 01:15:39 +0300 | [diff] [blame] | 864 | } |
| 865 | |
Pavel Begunkov | f4c2665 | 2020-06-15 10:24:02 +0300 | [diff] [blame] | 866 | /* |
| 867 | * Now check if a free (going busy) or busy worker has the work |
| 868 | * currently running. If we find it there, we'll return CANCEL_RUNNING |
| 869 | * as an indication that we attempt to signal cancellation. The |
| 870 | * completion will run normally in this case. |
| 871 | */ |
| 872 | for_each_node(node) { |
| 873 | struct io_wqe *wqe = wq->wqes[node]; |
| 874 | |
Pavel Begunkov | 4f26bda | 2020-06-15 10:24:03 +0300 | [diff] [blame] | 875 | io_wqe_cancel_running_work(wqe, &match); |
| 876 | if (match.nr_running && !match.cancel_all) |
Pavel Begunkov | f4c2665 | 2020-06-15 10:24:02 +0300 | [diff] [blame] | 877 | return IO_WQ_CANCEL_RUNNING; |
| 878 | } |
| 879 | |
Pavel Begunkov | 4f26bda | 2020-06-15 10:24:03 +0300 | [diff] [blame] | 880 | if (match.nr_running) |
| 881 | return IO_WQ_CANCEL_RUNNING; |
| 882 | if (match.nr_pending) |
| 883 | return IO_WQ_CANCEL_OK; |
Pavel Begunkov | f4c2665 | 2020-06-15 10:24:02 +0300 | [diff] [blame] | 884 | return IO_WQ_CANCEL_NOTFOUND; |
Pavel Begunkov | 2293b41 | 2020-03-07 01:15:39 +0300 | [diff] [blame] | 885 | } |
| 886 | |
Jens Axboe | e941894 | 2021-02-19 12:33:30 -0700 | [diff] [blame] | 887 | static int io_wqe_hash_wake(struct wait_queue_entry *wait, unsigned mode, |
| 888 | int sync, void *key) |
| 889 | { |
| 890 | struct io_wqe *wqe = container_of(wait, struct io_wqe, wait); |
Jens Axboe | e941894 | 2021-02-19 12:33:30 -0700 | [diff] [blame] | 891 | |
| 892 | list_del_init(&wait->entry); |
| 893 | |
| 894 | rcu_read_lock(); |
Jens Axboe | 685fe7f | 2021-03-08 09:37:51 -0700 | [diff] [blame] | 895 | io_wqe_activate_free_worker(wqe); |
Jens Axboe | e941894 | 2021-02-19 12:33:30 -0700 | [diff] [blame] | 896 | rcu_read_unlock(); |
Jens Axboe | e941894 | 2021-02-19 12:33:30 -0700 | [diff] [blame] | 897 | return 1; |
| 898 | } |
| 899 | |
Jens Axboe | 576a347 | 2019-11-25 08:49:20 -0700 | [diff] [blame] | 900 | struct io_wq *io_wq_create(unsigned bounded, struct io_wq_data *data) |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 901 | { |
Jann Horn | 3fc50ab | 2019-11-26 19:10:20 +0100 | [diff] [blame] | 902 | int ret = -ENOMEM, node; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 903 | struct io_wq *wq; |
| 904 | |
Pavel Begunkov | f5fa38c | 2020-06-08 21:08:20 +0300 | [diff] [blame] | 905 | if (WARN_ON_ONCE(!data->free_work || !data->do_work)) |
Pavel Begunkov | e9fd939 | 2020-03-04 16:14:12 +0300 | [diff] [blame] | 906 | return ERR_PTR(-EINVAL); |
| 907 | |
Pavel Begunkov | c7f405d | 2021-06-14 02:36:12 +0100 | [diff] [blame] | 908 | wq = kzalloc(struct_size(wq, wqes, nr_node_ids), GFP_KERNEL); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 909 | if (!wq) |
| 910 | return ERR_PTR(-ENOMEM); |
Jens Axboe | 43c01fb | 2020-10-22 09:02:50 -0600 | [diff] [blame] | 911 | ret = cpuhp_state_add_instance_nocalls(io_wq_online, &wq->cpuhp_node); |
| 912 | if (ret) |
Pavel Begunkov | c7f405d | 2021-06-14 02:36:12 +0100 | [diff] [blame] | 913 | goto err_wq; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 914 | |
Jens Axboe | e941894 | 2021-02-19 12:33:30 -0700 | [diff] [blame] | 915 | refcount_inc(&data->hash->refs); |
| 916 | wq->hash = data->hash; |
Pavel Begunkov | e9fd939 | 2020-03-04 16:14:12 +0300 | [diff] [blame] | 917 | wq->free_work = data->free_work; |
Pavel Begunkov | f5fa38c | 2020-06-08 21:08:20 +0300 | [diff] [blame] | 918 | wq->do_work = data->do_work; |
Jens Axboe | 7d72306 | 2019-11-12 22:31:31 -0700 | [diff] [blame] | 919 | |
Jens Axboe | 43c01fb | 2020-10-22 09:02:50 -0600 | [diff] [blame] | 920 | ret = -ENOMEM; |
Jann Horn | 3fc50ab | 2019-11-26 19:10:20 +0100 | [diff] [blame] | 921 | for_each_node(node) { |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 922 | struct io_wqe *wqe; |
Jens Axboe | 7563439 | 2020-02-11 06:30:06 -0700 | [diff] [blame] | 923 | int alloc_node = node; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 924 | |
Jens Axboe | 7563439 | 2020-02-11 06:30:06 -0700 | [diff] [blame] | 925 | if (!node_online(alloc_node)) |
| 926 | alloc_node = NUMA_NO_NODE; |
| 927 | wqe = kzalloc_node(sizeof(struct io_wqe), GFP_KERNEL, alloc_node); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 928 | if (!wqe) |
Jann Horn | 3fc50ab | 2019-11-26 19:10:20 +0100 | [diff] [blame] | 929 | goto err; |
| 930 | wq->wqes[node] = wqe; |
Jens Axboe | 7563439 | 2020-02-11 06:30:06 -0700 | [diff] [blame] | 931 | wqe->node = alloc_node; |
Jens Axboe | 685fe7f | 2021-03-08 09:37:51 -0700 | [diff] [blame] | 932 | wqe->acct[IO_WQ_ACCT_BOUND].index = IO_WQ_ACCT_BOUND; |
| 933 | wqe->acct[IO_WQ_ACCT_UNBOUND].index = IO_WQ_ACCT_UNBOUND; |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 934 | wqe->acct[IO_WQ_ACCT_BOUND].max_workers = bounded; |
| 935 | atomic_set(&wqe->acct[IO_WQ_ACCT_BOUND].nr_running, 0); |
Jens Axboe | 728f13e | 2021-02-21 16:02:53 -0700 | [diff] [blame] | 936 | wqe->acct[IO_WQ_ACCT_UNBOUND].max_workers = |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 937 | task_rlimit(current, RLIMIT_NPROC); |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 938 | atomic_set(&wqe->acct[IO_WQ_ACCT_UNBOUND].nr_running, 0); |
Jens Axboe | e941894 | 2021-02-19 12:33:30 -0700 | [diff] [blame] | 939 | wqe->wait.func = io_wqe_hash_wake; |
| 940 | INIT_LIST_HEAD(&wqe->wait.entry); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 941 | wqe->wq = wq; |
Sebastian Andrzej Siewior | 95da846 | 2020-09-01 10:41:46 +0200 | [diff] [blame] | 942 | raw_spin_lock_init(&wqe->lock); |
Jens Axboe | 6206f0e | 2019-11-26 11:59:32 -0700 | [diff] [blame] | 943 | INIT_WQ_LIST(&wqe->work_list); |
Jens Axboe | 021d1cd | 2019-11-14 08:00:41 -0700 | [diff] [blame] | 944 | INIT_HLIST_NULLS_HEAD(&wqe->free_list, 0); |
Jens Axboe | e61df66 | 2019-11-13 13:54:49 -0700 | [diff] [blame] | 945 | INIT_LIST_HEAD(&wqe->all_list); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 946 | } |
| 947 | |
Jens Axboe | 685fe7f | 2021-03-08 09:37:51 -0700 | [diff] [blame] | 948 | wq->task = get_task_struct(data->task); |
Jens Axboe | 685fe7f | 2021-03-08 09:37:51 -0700 | [diff] [blame] | 949 | atomic_set(&wq->worker_refs, 1); |
| 950 | init_completion(&wq->worker_done); |
| 951 | return wq; |
Jens Axboe | b60fda6 | 2019-11-19 08:37:07 -0700 | [diff] [blame] | 952 | err: |
Jens Axboe | dc7bbc9 | 2021-03-01 09:09:56 -0700 | [diff] [blame] | 953 | io_wq_put_hash(data->hash); |
Jens Axboe | 43c01fb | 2020-10-22 09:02:50 -0600 | [diff] [blame] | 954 | cpuhp_state_remove_instance_nocalls(io_wq_online, &wq->cpuhp_node); |
Jann Horn | 3fc50ab | 2019-11-26 19:10:20 +0100 | [diff] [blame] | 955 | for_each_node(node) |
| 956 | kfree(wq->wqes[node]); |
Jens Axboe | 43c01fb | 2020-10-22 09:02:50 -0600 | [diff] [blame] | 957 | err_wq: |
Jens Axboe | b60fda6 | 2019-11-19 08:37:07 -0700 | [diff] [blame] | 958 | kfree(wq); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 959 | return ERR_PTR(ret); |
| 960 | } |
| 961 | |
Jens Axboe | c80ca47 | 2021-04-01 19:57:07 -0600 | [diff] [blame] | 962 | static bool io_task_work_match(struct callback_head *cb, void *data) |
| 963 | { |
| 964 | struct create_worker_data *cwd; |
| 965 | |
| 966 | if (cb->func != create_worker_cb) |
| 967 | return false; |
| 968 | cwd = container_of(cb, struct create_worker_data, work); |
| 969 | return cwd->wqe->wq == data; |
| 970 | } |
| 971 | |
Pavel Begunkov | 17a9105 | 2021-05-23 15:48:39 +0100 | [diff] [blame] | 972 | void io_wq_exit_start(struct io_wq *wq) |
| 973 | { |
| 974 | set_bit(IO_WQ_BIT_EXIT, &wq->state); |
| 975 | } |
| 976 | |
Jens Axboe | 685fe7f | 2021-03-08 09:37:51 -0700 | [diff] [blame] | 977 | static void io_wq_exit_workers(struct io_wq *wq) |
Jens Axboe | afcc401 | 2021-02-26 13:48:19 -0700 | [diff] [blame] | 978 | { |
Jens Axboe | 685fe7f | 2021-03-08 09:37:51 -0700 | [diff] [blame] | 979 | struct callback_head *cb; |
| 980 | int node; |
| 981 | |
Jens Axboe | 685fe7f | 2021-03-08 09:37:51 -0700 | [diff] [blame] | 982 | if (!wq->task) |
| 983 | return; |
| 984 | |
Jens Axboe | c80ca47 | 2021-04-01 19:57:07 -0600 | [diff] [blame] | 985 | while ((cb = task_work_cancel_match(wq->task, io_task_work_match, wq)) != NULL) { |
Jens Axboe | 685fe7f | 2021-03-08 09:37:51 -0700 | [diff] [blame] | 986 | struct create_worker_data *cwd; |
| 987 | |
| 988 | cwd = container_of(cb, struct create_worker_data, work); |
| 989 | atomic_dec(&cwd->wqe->acct[cwd->index].nr_running); |
| 990 | io_worker_ref_put(wq); |
| 991 | kfree(cwd); |
Jens Axboe | afcc401 | 2021-02-26 13:48:19 -0700 | [diff] [blame] | 992 | } |
Jens Axboe | 685fe7f | 2021-03-08 09:37:51 -0700 | [diff] [blame] | 993 | |
| 994 | rcu_read_lock(); |
| 995 | for_each_node(node) { |
| 996 | struct io_wqe *wqe = wq->wqes[node]; |
| 997 | |
| 998 | io_wq_for_each_worker(wqe, io_wq_worker_wake, NULL); |
Jens Axboe | 685fe7f | 2021-03-08 09:37:51 -0700 | [diff] [blame] | 999 | } |
| 1000 | rcu_read_unlock(); |
| 1001 | io_worker_ref_put(wq); |
| 1002 | wait_for_completion(&wq->worker_done); |
Zqiang | 3743c17 | 2021-05-26 13:08:26 +0800 | [diff] [blame] | 1003 | |
| 1004 | for_each_node(node) { |
| 1005 | spin_lock_irq(&wq->hash->wait.lock); |
| 1006 | list_del_init(&wq->wqes[node]->wait.entry); |
| 1007 | spin_unlock_irq(&wq->hash->wait.lock); |
| 1008 | } |
Jens Axboe | 685fe7f | 2021-03-08 09:37:51 -0700 | [diff] [blame] | 1009 | put_task_struct(wq->task); |
| 1010 | wq->task = NULL; |
Jens Axboe | afcc401 | 2021-02-26 13:48:19 -0700 | [diff] [blame] | 1011 | } |
| 1012 | |
Jens Axboe | 4fb6ac3 | 2021-02-25 10:17:09 -0700 | [diff] [blame] | 1013 | static void io_wq_destroy(struct io_wq *wq) |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 1014 | { |
Jann Horn | 3fc50ab | 2019-11-26 19:10:20 +0100 | [diff] [blame] | 1015 | int node; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 1016 | |
Jens Axboe | 43c01fb | 2020-10-22 09:02:50 -0600 | [diff] [blame] | 1017 | cpuhp_state_remove_instance_nocalls(io_wq_online, &wq->cpuhp_node); |
| 1018 | |
Jens Axboe | e941894 | 2021-02-19 12:33:30 -0700 | [diff] [blame] | 1019 | for_each_node(node) { |
| 1020 | struct io_wqe *wqe = wq->wqes[node]; |
Jens Axboe | f5d2d23 | 2021-03-25 10:16:12 -0600 | [diff] [blame] | 1021 | struct io_cb_cancel_data match = { |
| 1022 | .fn = io_wq_work_match_all, |
| 1023 | .cancel_all = true, |
| 1024 | }; |
| 1025 | io_wqe_cancel_pending_work(wqe, &match); |
Jens Axboe | e941894 | 2021-02-19 12:33:30 -0700 | [diff] [blame] | 1026 | kfree(wqe); |
| 1027 | } |
Jens Axboe | e941894 | 2021-02-19 12:33:30 -0700 | [diff] [blame] | 1028 | io_wq_put_hash(wq->hash); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 1029 | kfree(wq); |
Jens Axboe | 4fb6ac3 | 2021-02-25 10:17:09 -0700 | [diff] [blame] | 1030 | } |
| 1031 | |
Jens Axboe | afcc401 | 2021-02-26 13:48:19 -0700 | [diff] [blame] | 1032 | void io_wq_put_and_exit(struct io_wq *wq) |
| 1033 | { |
Pavel Begunkov | 17a9105 | 2021-05-23 15:48:39 +0100 | [diff] [blame] | 1034 | WARN_ON_ONCE(!test_bit(IO_WQ_BIT_EXIT, &wq->state)); |
| 1035 | |
Jens Axboe | 685fe7f | 2021-03-08 09:37:51 -0700 | [diff] [blame] | 1036 | io_wq_exit_workers(wq); |
Pavel Begunkov | 382cb03 | 2021-06-14 02:36:13 +0100 | [diff] [blame] | 1037 | io_wq_destroy(wq); |
Jens Axboe | afcc401 | 2021-02-26 13:48:19 -0700 | [diff] [blame] | 1038 | } |
| 1039 | |
Jens Axboe | 43c01fb | 2020-10-22 09:02:50 -0600 | [diff] [blame] | 1040 | static bool io_wq_worker_affinity(struct io_worker *worker, void *data) |
| 1041 | { |
Peter Zijlstra | e0051d7 | 2021-04-08 11:44:50 +0200 | [diff] [blame] | 1042 | set_cpus_allowed_ptr(worker->task, cpumask_of_node(worker->wqe->node)); |
Jens Axboe | 43c01fb | 2020-10-22 09:02:50 -0600 | [diff] [blame] | 1043 | |
Jens Axboe | 43c01fb | 2020-10-22 09:02:50 -0600 | [diff] [blame] | 1044 | return false; |
| 1045 | } |
| 1046 | |
| 1047 | static int io_wq_cpu_online(unsigned int cpu, struct hlist_node *node) |
| 1048 | { |
| 1049 | struct io_wq *wq = hlist_entry_safe(node, struct io_wq, cpuhp_node); |
| 1050 | int i; |
| 1051 | |
| 1052 | rcu_read_lock(); |
| 1053 | for_each_node(i) |
| 1054 | io_wq_for_each_worker(wq->wqes[i], io_wq_worker_affinity, NULL); |
| 1055 | rcu_read_unlock(); |
| 1056 | return 0; |
| 1057 | } |
| 1058 | |
| 1059 | static __init int io_wq_init(void) |
| 1060 | { |
| 1061 | int ret; |
| 1062 | |
| 1063 | ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "io-wq/online", |
| 1064 | io_wq_cpu_online, NULL); |
| 1065 | if (ret < 0) |
| 1066 | return ret; |
| 1067 | io_wq_online = ret; |
| 1068 | return 0; |
| 1069 | } |
| 1070 | subsys_initcall(io_wq_init); |