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