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