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