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> |
| 13 | #include <linux/mmu_context.h> |
| 14 | #include <linux/sched/mm.h> |
| 15 | #include <linux/percpu.h> |
| 16 | #include <linux/slab.h> |
| 17 | #include <linux/kthread.h> |
| 18 | #include <linux/rculist_nulls.h> |
| 19 | |
| 20 | #include "io-wq.h" |
| 21 | |
| 22 | #define WORKER_IDLE_TIMEOUT (5 * HZ) |
| 23 | |
| 24 | enum { |
| 25 | IO_WORKER_F_UP = 1, /* up and active */ |
| 26 | IO_WORKER_F_RUNNING = 2, /* account as running */ |
| 27 | IO_WORKER_F_FREE = 4, /* worker on free list */ |
| 28 | IO_WORKER_F_EXITING = 8, /* worker exiting */ |
| 29 | IO_WORKER_F_FIXED = 16, /* static idle worker */ |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 30 | IO_WORKER_F_BOUND = 32, /* 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 */ |
| 35 | IO_WQ_BIT_CANCEL = 1, /* cancel work on list */ |
Jens Axboe | b60fda6 | 2019-11-19 08:37:07 -0700 | [diff] [blame] | 36 | IO_WQ_BIT_ERROR = 2, /* error on setup */ |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 37 | }; |
| 38 | |
| 39 | enum { |
| 40 | IO_WQE_FLAG_STALLED = 1, /* stalled on hash */ |
| 41 | }; |
| 42 | |
| 43 | /* |
| 44 | * One for each thread in a wqe pool |
| 45 | */ |
| 46 | struct io_worker { |
| 47 | refcount_t ref; |
| 48 | unsigned flags; |
| 49 | struct hlist_nulls_node nulls_node; |
Jens Axboe | e61df66 | 2019-11-13 13:54:49 -0700 | [diff] [blame] | 50 | struct list_head all_list; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 51 | struct task_struct *task; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 52 | struct io_wqe *wqe; |
Jens Axboe | 36c2f92 | 2019-11-13 09:43:34 -0700 | [diff] [blame] | 53 | |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 54 | struct io_wq_work *cur_work; |
Jens Axboe | 36c2f92 | 2019-11-13 09:43:34 -0700 | [diff] [blame] | 55 | spinlock_t lock; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 56 | |
| 57 | struct rcu_head rcu; |
| 58 | struct mm_struct *mm; |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 59 | const struct cred *creds; |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 60 | struct files_struct *restore_files; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 61 | }; |
| 62 | |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 63 | #if BITS_PER_LONG == 64 |
| 64 | #define IO_WQ_HASH_ORDER 6 |
| 65 | #else |
| 66 | #define IO_WQ_HASH_ORDER 5 |
| 67 | #endif |
| 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 { |
| 85 | 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 long hash_map; |
| 88 | unsigned flags; |
| 89 | } ____cacheline_aligned_in_smp; |
| 90 | |
| 91 | int node; |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 92 | struct io_wqe_acct acct[2]; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 93 | |
Jens Axboe | 021d1cd | 2019-11-14 08:00:41 -0700 | [diff] [blame] | 94 | struct hlist_nulls_head free_list; |
Jens Axboe | e61df66 | 2019-11-13 13:54:49 -0700 | [diff] [blame] | 95 | struct list_head all_list; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 96 | |
| 97 | struct io_wq *wq; |
| 98 | }; |
| 99 | |
| 100 | /* |
| 101 | * Per io_wq state |
| 102 | */ |
| 103 | struct io_wq { |
| 104 | struct io_wqe **wqes; |
| 105 | unsigned long state; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 106 | |
Jens Axboe | 7d72306 | 2019-11-12 22:31:31 -0700 | [diff] [blame] | 107 | get_work_fn *get_work; |
| 108 | put_work_fn *put_work; |
| 109 | |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 110 | struct task_struct *manager; |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 111 | struct user_struct *user; |
Jens Axboe | 0b8c0ec | 2019-12-02 08:50:00 -0700 | [diff] [blame] | 112 | const struct cred *creds; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 113 | struct mm_struct *mm; |
| 114 | refcount_t refs; |
| 115 | struct completion done; |
| 116 | }; |
| 117 | |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 118 | static bool io_worker_get(struct io_worker *worker) |
| 119 | { |
| 120 | return refcount_inc_not_zero(&worker->ref); |
| 121 | } |
| 122 | |
| 123 | static void io_worker_release(struct io_worker *worker) |
| 124 | { |
| 125 | if (refcount_dec_and_test(&worker->ref)) |
| 126 | wake_up_process(worker->task); |
| 127 | } |
| 128 | |
| 129 | /* |
| 130 | * Note: drops the wqe->lock if returning true! The caller must re-acquire |
| 131 | * the lock in that case. Some callers need to restart handling if this |
| 132 | * happens, so we can't just re-acquire the lock on behalf of the caller. |
| 133 | */ |
| 134 | static bool __io_worker_unuse(struct io_wqe *wqe, struct io_worker *worker) |
| 135 | { |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 136 | bool dropped_lock = false; |
| 137 | |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 138 | if (worker->creds) { |
| 139 | revert_creds(worker->creds); |
| 140 | worker->creds = NULL; |
| 141 | } |
| 142 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 143 | if (current->files != worker->restore_files) { |
| 144 | __acquire(&wqe->lock); |
| 145 | spin_unlock_irq(&wqe->lock); |
| 146 | dropped_lock = true; |
| 147 | |
| 148 | task_lock(current); |
| 149 | current->files = worker->restore_files; |
| 150 | task_unlock(current); |
| 151 | } |
| 152 | |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 153 | /* |
| 154 | * If we have an active mm, we need to drop the wq lock before unusing |
| 155 | * it. If we do, return true and let the caller retry the idle loop. |
| 156 | */ |
| 157 | if (worker->mm) { |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 158 | if (!dropped_lock) { |
| 159 | __acquire(&wqe->lock); |
| 160 | spin_unlock_irq(&wqe->lock); |
| 161 | dropped_lock = true; |
| 162 | } |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 163 | __set_current_state(TASK_RUNNING); |
| 164 | set_fs(KERNEL_DS); |
| 165 | unuse_mm(worker->mm); |
| 166 | mmput(worker->mm); |
| 167 | worker->mm = NULL; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 168 | } |
| 169 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 170 | return dropped_lock; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 171 | } |
| 172 | |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 173 | static inline struct io_wqe_acct *io_work_get_acct(struct io_wqe *wqe, |
| 174 | struct io_wq_work *work) |
| 175 | { |
| 176 | if (work->flags & IO_WQ_WORK_UNBOUND) |
| 177 | return &wqe->acct[IO_WQ_ACCT_UNBOUND]; |
| 178 | |
| 179 | return &wqe->acct[IO_WQ_ACCT_BOUND]; |
| 180 | } |
| 181 | |
| 182 | static inline struct io_wqe_acct *io_wqe_get_acct(struct io_wqe *wqe, |
| 183 | struct io_worker *worker) |
| 184 | { |
| 185 | if (worker->flags & IO_WORKER_F_BOUND) |
| 186 | return &wqe->acct[IO_WQ_ACCT_BOUND]; |
| 187 | |
| 188 | return &wqe->acct[IO_WQ_ACCT_UNBOUND]; |
| 189 | } |
| 190 | |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 191 | static void io_worker_exit(struct io_worker *worker) |
| 192 | { |
| 193 | struct io_wqe *wqe = worker->wqe; |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 194 | struct io_wqe_acct *acct = io_wqe_get_acct(wqe, worker); |
| 195 | unsigned nr_workers; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 196 | |
| 197 | /* |
| 198 | * If we're not at zero, someone else is holding a brief reference |
| 199 | * to the worker. Wait for that to go away. |
| 200 | */ |
| 201 | set_current_state(TASK_INTERRUPTIBLE); |
| 202 | if (!refcount_dec_and_test(&worker->ref)) |
| 203 | schedule(); |
| 204 | __set_current_state(TASK_RUNNING); |
| 205 | |
| 206 | preempt_disable(); |
| 207 | current->flags &= ~PF_IO_WORKER; |
| 208 | if (worker->flags & IO_WORKER_F_RUNNING) |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 209 | atomic_dec(&acct->nr_running); |
| 210 | if (!(worker->flags & IO_WORKER_F_BOUND)) |
| 211 | atomic_dec(&wqe->wq->user->processes); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 212 | worker->flags = 0; |
| 213 | preempt_enable(); |
| 214 | |
| 215 | spin_lock_irq(&wqe->lock); |
| 216 | hlist_nulls_del_rcu(&worker->nulls_node); |
Jens Axboe | e61df66 | 2019-11-13 13:54:49 -0700 | [diff] [blame] | 217 | list_del_rcu(&worker->all_list); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 218 | if (__io_worker_unuse(wqe, worker)) { |
| 219 | __release(&wqe->lock); |
| 220 | spin_lock_irq(&wqe->lock); |
| 221 | } |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 222 | acct->nr_workers--; |
| 223 | nr_workers = wqe->acct[IO_WQ_ACCT_BOUND].nr_workers + |
| 224 | wqe->acct[IO_WQ_ACCT_UNBOUND].nr_workers; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 225 | spin_unlock_irq(&wqe->lock); |
| 226 | |
| 227 | /* all workers gone, wq exit can proceed */ |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 228 | if (!nr_workers && refcount_dec_and_test(&wqe->wq->refs)) |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 229 | complete(&wqe->wq->done); |
| 230 | |
YueHaibing | 364b05f | 2019-11-02 15:55:01 +0800 | [diff] [blame] | 231 | kfree_rcu(worker, rcu); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 232 | } |
| 233 | |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 234 | static inline bool io_wqe_run_queue(struct io_wqe *wqe) |
| 235 | __must_hold(wqe->lock) |
| 236 | { |
Jens Axboe | 6206f0e | 2019-11-26 11:59:32 -0700 | [diff] [blame] | 237 | if (!wq_list_empty(&wqe->work_list) && |
| 238 | !(wqe->flags & IO_WQE_FLAG_STALLED)) |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 239 | return true; |
| 240 | return false; |
| 241 | } |
| 242 | |
| 243 | /* |
| 244 | * Check head of free list for an available worker. If one isn't available, |
| 245 | * caller must wake up the wq manager to create one. |
| 246 | */ |
| 247 | static bool io_wqe_activate_free_worker(struct io_wqe *wqe) |
| 248 | __must_hold(RCU) |
| 249 | { |
| 250 | struct hlist_nulls_node *n; |
| 251 | struct io_worker *worker; |
| 252 | |
Jens Axboe | 021d1cd | 2019-11-14 08:00:41 -0700 | [diff] [blame] | 253 | n = rcu_dereference(hlist_nulls_first_rcu(&wqe->free_list)); |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 254 | if (is_a_nulls(n)) |
| 255 | return false; |
| 256 | |
| 257 | worker = hlist_nulls_entry(n, struct io_worker, nulls_node); |
| 258 | if (io_worker_get(worker)) { |
Jens Axboe | 506d95f | 2019-12-07 21:03:59 -0700 | [diff] [blame] | 259 | wake_up_process(worker->task); |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 260 | io_worker_release(worker); |
| 261 | return true; |
| 262 | } |
| 263 | |
| 264 | return false; |
| 265 | } |
| 266 | |
| 267 | /* |
| 268 | * We need a worker. If we find a free one, we're good. If not, and we're |
| 269 | * below the max number of workers, wake up the manager to create one. |
| 270 | */ |
| 271 | static void io_wqe_wake_worker(struct io_wqe *wqe, struct io_wqe_acct *acct) |
| 272 | { |
| 273 | bool ret; |
| 274 | |
| 275 | /* |
| 276 | * Most likely an attempt to queue unbounded work on an io_wq that |
| 277 | * wasn't setup with any unbounded workers. |
| 278 | */ |
| 279 | WARN_ON_ONCE(!acct->max_workers); |
| 280 | |
| 281 | rcu_read_lock(); |
| 282 | ret = io_wqe_activate_free_worker(wqe); |
| 283 | rcu_read_unlock(); |
| 284 | |
| 285 | if (!ret && acct->nr_workers < acct->max_workers) |
| 286 | wake_up_process(wqe->wq->manager); |
| 287 | } |
| 288 | |
| 289 | static void io_wqe_inc_running(struct io_wqe *wqe, struct io_worker *worker) |
| 290 | { |
| 291 | struct io_wqe_acct *acct = io_wqe_get_acct(wqe, worker); |
| 292 | |
| 293 | atomic_inc(&acct->nr_running); |
| 294 | } |
| 295 | |
| 296 | static void io_wqe_dec_running(struct io_wqe *wqe, struct io_worker *worker) |
| 297 | __must_hold(wqe->lock) |
| 298 | { |
| 299 | struct io_wqe_acct *acct = io_wqe_get_acct(wqe, worker); |
| 300 | |
| 301 | if (atomic_dec_and_test(&acct->nr_running) && io_wqe_run_queue(wqe)) |
| 302 | io_wqe_wake_worker(wqe, acct); |
| 303 | } |
| 304 | |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 305 | static void io_worker_start(struct io_wqe *wqe, struct io_worker *worker) |
| 306 | { |
| 307 | allow_kernel_signal(SIGINT); |
| 308 | |
| 309 | current->flags |= PF_IO_WORKER; |
| 310 | |
| 311 | worker->flags |= (IO_WORKER_F_UP | IO_WORKER_F_RUNNING); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 312 | worker->restore_files = current->files; |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 313 | io_wqe_inc_running(wqe, worker); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | /* |
| 317 | * Worker will start processing some work. Move it to the busy list, if |
| 318 | * it's currently on the freelist |
| 319 | */ |
| 320 | static void __io_worker_busy(struct io_wqe *wqe, struct io_worker *worker, |
| 321 | struct io_wq_work *work) |
| 322 | __must_hold(wqe->lock) |
| 323 | { |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 324 | bool worker_bound, work_bound; |
| 325 | |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 326 | if (worker->flags & IO_WORKER_F_FREE) { |
| 327 | worker->flags &= ~IO_WORKER_F_FREE; |
| 328 | hlist_nulls_del_init_rcu(&worker->nulls_node); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 329 | } |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 330 | |
| 331 | /* |
| 332 | * If worker is moving from bound to unbound (or vice versa), then |
| 333 | * ensure we update the running accounting. |
| 334 | */ |
Dan Carpenter | b2e9c7d6 | 2019-11-19 09:22:16 +0300 | [diff] [blame] | 335 | worker_bound = (worker->flags & IO_WORKER_F_BOUND) != 0; |
| 336 | work_bound = (work->flags & IO_WQ_WORK_UNBOUND) == 0; |
| 337 | if (worker_bound != work_bound) { |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 338 | io_wqe_dec_running(wqe, worker); |
| 339 | if (work_bound) { |
| 340 | worker->flags |= IO_WORKER_F_BOUND; |
| 341 | wqe->acct[IO_WQ_ACCT_UNBOUND].nr_workers--; |
| 342 | wqe->acct[IO_WQ_ACCT_BOUND].nr_workers++; |
| 343 | atomic_dec(&wqe->wq->user->processes); |
| 344 | } else { |
| 345 | worker->flags &= ~IO_WORKER_F_BOUND; |
| 346 | wqe->acct[IO_WQ_ACCT_UNBOUND].nr_workers++; |
| 347 | wqe->acct[IO_WQ_ACCT_BOUND].nr_workers--; |
| 348 | atomic_inc(&wqe->wq->user->processes); |
| 349 | } |
| 350 | io_wqe_inc_running(wqe, worker); |
| 351 | } |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | /* |
| 355 | * No work, worker going to sleep. Move to freelist, and unuse mm if we |
| 356 | * have one attached. Dropping the mm may potentially sleep, so we drop |
| 357 | * the lock in that case and return success. Since the caller has to |
| 358 | * retry the loop in that case (we changed task state), we don't regrab |
| 359 | * the lock if we return success. |
| 360 | */ |
| 361 | static bool __io_worker_idle(struct io_wqe *wqe, struct io_worker *worker) |
| 362 | __must_hold(wqe->lock) |
| 363 | { |
| 364 | if (!(worker->flags & IO_WORKER_F_FREE)) { |
| 365 | worker->flags |= IO_WORKER_F_FREE; |
Jens Axboe | 021d1cd | 2019-11-14 08:00:41 -0700 | [diff] [blame] | 366 | hlist_nulls_add_head_rcu(&worker->nulls_node, &wqe->free_list); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | return __io_worker_unuse(wqe, worker); |
| 370 | } |
| 371 | |
| 372 | static struct io_wq_work *io_get_next_work(struct io_wqe *wqe, unsigned *hash) |
| 373 | __must_hold(wqe->lock) |
| 374 | { |
Jens Axboe | 6206f0e | 2019-11-26 11:59:32 -0700 | [diff] [blame] | 375 | struct io_wq_work_node *node, *prev; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 376 | struct io_wq_work *work; |
| 377 | |
Jens Axboe | 6206f0e | 2019-11-26 11:59:32 -0700 | [diff] [blame] | 378 | wq_list_for_each(node, prev, &wqe->work_list) { |
| 379 | work = container_of(node, struct io_wq_work, list); |
| 380 | |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 381 | /* not hashed, can run anytime */ |
| 382 | if (!(work->flags & IO_WQ_WORK_HASHED)) { |
Jens Axboe | 6206f0e | 2019-11-26 11:59:32 -0700 | [diff] [blame] | 383 | wq_node_del(&wqe->work_list, node, prev); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 384 | return work; |
| 385 | } |
| 386 | |
| 387 | /* hashed, can run if not already running */ |
| 388 | *hash = work->flags >> IO_WQ_HASH_SHIFT; |
| 389 | if (!(wqe->hash_map & BIT_ULL(*hash))) { |
| 390 | wqe->hash_map |= BIT_ULL(*hash); |
Jens Axboe | 6206f0e | 2019-11-26 11:59:32 -0700 | [diff] [blame] | 391 | wq_node_del(&wqe->work_list, node, prev); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 392 | return work; |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | return NULL; |
| 397 | } |
| 398 | |
| 399 | static void io_worker_handle_work(struct io_worker *worker) |
| 400 | __releases(wqe->lock) |
| 401 | { |
Jens Axboe | 7d72306 | 2019-11-12 22:31:31 -0700 | [diff] [blame] | 402 | struct io_wq_work *work, *old_work = NULL, *put_work = NULL; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 403 | struct io_wqe *wqe = worker->wqe; |
| 404 | struct io_wq *wq = wqe->wq; |
| 405 | |
| 406 | do { |
| 407 | unsigned hash = -1U; |
| 408 | |
| 409 | /* |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 410 | * If we got some work, mark us as busy. If we didn't, but |
| 411 | * the list isn't empty, it means we stalled on hashed work. |
| 412 | * Mark us stalled so we don't keep looking for work when we |
| 413 | * can't make progress, any work completion or insertion will |
| 414 | * clear the stalled flag. |
| 415 | */ |
| 416 | work = io_get_next_work(wqe, &hash); |
| 417 | if (work) |
| 418 | __io_worker_busy(wqe, worker, work); |
Jens Axboe | 6206f0e | 2019-11-26 11:59:32 -0700 | [diff] [blame] | 419 | else if (!wq_list_empty(&wqe->work_list)) |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 420 | wqe->flags |= IO_WQE_FLAG_STALLED; |
| 421 | |
| 422 | spin_unlock_irq(&wqe->lock); |
Jens Axboe | 7d72306 | 2019-11-12 22:31:31 -0700 | [diff] [blame] | 423 | if (put_work && wq->put_work) |
| 424 | wq->put_work(old_work); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 425 | if (!work) |
| 426 | break; |
| 427 | next: |
Jens Axboe | 36c2f92 | 2019-11-13 09:43:34 -0700 | [diff] [blame] | 428 | /* flush any pending signals before assigning new work */ |
| 429 | if (signal_pending(current)) |
| 430 | flush_signals(current); |
| 431 | |
Hillf Danton | fd1c4bc | 2019-12-24 09:14:29 -0700 | [diff] [blame] | 432 | cond_resched(); |
| 433 | |
Jens Axboe | 36c2f92 | 2019-11-13 09:43:34 -0700 | [diff] [blame] | 434 | spin_lock_irq(&worker->lock); |
| 435 | worker->cur_work = work; |
| 436 | spin_unlock_irq(&worker->lock); |
| 437 | |
Jens Axboe | b76da70 | 2019-11-20 13:05:32 -0700 | [diff] [blame] | 438 | if (work->flags & IO_WQ_WORK_CB) |
| 439 | work->func(&work); |
| 440 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 441 | if ((work->flags & IO_WQ_WORK_NEEDS_FILES) && |
| 442 | current->files != work->files) { |
| 443 | task_lock(current); |
| 444 | current->files = work->files; |
| 445 | task_unlock(current); |
| 446 | } |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 447 | if ((work->flags & IO_WQ_WORK_NEEDS_USER) && !worker->mm && |
| 448 | wq->mm && mmget_not_zero(wq->mm)) { |
| 449 | use_mm(wq->mm); |
| 450 | set_fs(USER_DS); |
| 451 | worker->mm = wq->mm; |
| 452 | } |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 453 | if (!worker->creds) |
| 454 | worker->creds = override_creds(wq->creds); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 455 | if (test_bit(IO_WQ_BIT_CANCEL, &wq->state)) |
| 456 | work->flags |= IO_WQ_WORK_CANCEL; |
| 457 | if (worker->mm) |
| 458 | work->flags |= IO_WQ_WORK_HAS_MM; |
| 459 | |
Jens Axboe | 7d72306 | 2019-11-12 22:31:31 -0700 | [diff] [blame] | 460 | if (wq->get_work && !(work->flags & IO_WQ_WORK_INTERNAL)) { |
| 461 | put_work = work; |
| 462 | wq->get_work(work); |
| 463 | } |
| 464 | |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 465 | old_work = work; |
| 466 | work->func(&work); |
| 467 | |
Jens Axboe | 36c2f92 | 2019-11-13 09:43:34 -0700 | [diff] [blame] | 468 | spin_lock_irq(&worker->lock); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 469 | worker->cur_work = NULL; |
Jens Axboe | 36c2f92 | 2019-11-13 09:43:34 -0700 | [diff] [blame] | 470 | spin_unlock_irq(&worker->lock); |
| 471 | |
| 472 | spin_lock_irq(&wqe->lock); |
| 473 | |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 474 | if (hash != -1U) { |
| 475 | wqe->hash_map &= ~BIT_ULL(hash); |
| 476 | wqe->flags &= ~IO_WQE_FLAG_STALLED; |
| 477 | } |
| 478 | if (work && work != old_work) { |
| 479 | spin_unlock_irq(&wqe->lock); |
Jens Axboe | 7d72306 | 2019-11-12 22:31:31 -0700 | [diff] [blame] | 480 | |
| 481 | if (put_work && wq->put_work) { |
| 482 | wq->put_work(put_work); |
| 483 | put_work = NULL; |
| 484 | } |
| 485 | |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 486 | /* dependent work not hashed */ |
| 487 | hash = -1U; |
| 488 | goto next; |
| 489 | } |
| 490 | } while (1); |
| 491 | } |
| 492 | |
Jens Axboe | e995d51 | 2019-12-07 21:06:46 -0700 | [diff] [blame] | 493 | static inline void io_worker_spin_for_work(struct io_wqe *wqe) |
| 494 | { |
| 495 | int i = 0; |
| 496 | |
| 497 | while (++i < 1000) { |
| 498 | if (io_wqe_run_queue(wqe)) |
| 499 | break; |
| 500 | if (need_resched()) |
| 501 | break; |
| 502 | cpu_relax(); |
| 503 | } |
| 504 | } |
| 505 | |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 506 | static int io_wqe_worker(void *data) |
| 507 | { |
| 508 | struct io_worker *worker = data; |
| 509 | struct io_wqe *wqe = worker->wqe; |
| 510 | struct io_wq *wq = wqe->wq; |
Jens Axboe | e995d51 | 2019-12-07 21:06:46 -0700 | [diff] [blame] | 511 | bool did_work; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 512 | |
| 513 | io_worker_start(wqe, worker); |
| 514 | |
Jens Axboe | e995d51 | 2019-12-07 21:06:46 -0700 | [diff] [blame] | 515 | did_work = false; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 516 | while (!test_bit(IO_WQ_BIT_EXIT, &wq->state)) { |
Jens Axboe | 506d95f | 2019-12-07 21:03:59 -0700 | [diff] [blame] | 517 | set_current_state(TASK_INTERRUPTIBLE); |
Jens Axboe | e995d51 | 2019-12-07 21:06:46 -0700 | [diff] [blame] | 518 | loop: |
| 519 | if (did_work) |
| 520 | io_worker_spin_for_work(wqe); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 521 | spin_lock_irq(&wqe->lock); |
| 522 | if (io_wqe_run_queue(wqe)) { |
| 523 | __set_current_state(TASK_RUNNING); |
| 524 | io_worker_handle_work(worker); |
Jens Axboe | e995d51 | 2019-12-07 21:06:46 -0700 | [diff] [blame] | 525 | did_work = true; |
| 526 | goto loop; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 527 | } |
Jens Axboe | e995d51 | 2019-12-07 21:06:46 -0700 | [diff] [blame] | 528 | did_work = false; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 529 | /* drops the lock on success, retry */ |
| 530 | if (__io_worker_idle(wqe, worker)) { |
| 531 | __release(&wqe->lock); |
Jens Axboe | e995d51 | 2019-12-07 21:06:46 -0700 | [diff] [blame] | 532 | goto loop; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 533 | } |
| 534 | spin_unlock_irq(&wqe->lock); |
| 535 | if (signal_pending(current)) |
| 536 | flush_signals(current); |
| 537 | if (schedule_timeout(WORKER_IDLE_TIMEOUT)) |
| 538 | continue; |
| 539 | /* timed out, exit unless we're the fixed worker */ |
| 540 | if (test_bit(IO_WQ_BIT_EXIT, &wq->state) || |
| 541 | !(worker->flags & IO_WORKER_F_FIXED)) |
| 542 | break; |
| 543 | } |
| 544 | |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 545 | if (test_bit(IO_WQ_BIT_EXIT, &wq->state)) { |
| 546 | spin_lock_irq(&wqe->lock); |
Jens Axboe | 6206f0e | 2019-11-26 11:59:32 -0700 | [diff] [blame] | 547 | if (!wq_list_empty(&wqe->work_list)) |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 548 | io_worker_handle_work(worker); |
| 549 | else |
| 550 | spin_unlock_irq(&wqe->lock); |
| 551 | } |
| 552 | |
| 553 | io_worker_exit(worker); |
| 554 | return 0; |
| 555 | } |
| 556 | |
| 557 | /* |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 558 | * Called when a worker is scheduled in. Mark us as currently running. |
| 559 | */ |
| 560 | void io_wq_worker_running(struct task_struct *tsk) |
| 561 | { |
| 562 | struct io_worker *worker = kthread_data(tsk); |
| 563 | struct io_wqe *wqe = worker->wqe; |
| 564 | |
| 565 | if (!(worker->flags & IO_WORKER_F_UP)) |
| 566 | return; |
| 567 | if (worker->flags & IO_WORKER_F_RUNNING) |
| 568 | return; |
| 569 | worker->flags |= IO_WORKER_F_RUNNING; |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 570 | io_wqe_inc_running(wqe, worker); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 571 | } |
| 572 | |
| 573 | /* |
| 574 | * Called when worker is going to sleep. If there are no workers currently |
| 575 | * running and we have work pending, wake up a free one or have the manager |
| 576 | * set one up. |
| 577 | */ |
| 578 | void io_wq_worker_sleeping(struct task_struct *tsk) |
| 579 | { |
| 580 | struct io_worker *worker = kthread_data(tsk); |
| 581 | struct io_wqe *wqe = worker->wqe; |
| 582 | |
| 583 | if (!(worker->flags & IO_WORKER_F_UP)) |
| 584 | return; |
| 585 | if (!(worker->flags & IO_WORKER_F_RUNNING)) |
| 586 | return; |
| 587 | |
| 588 | worker->flags &= ~IO_WORKER_F_RUNNING; |
| 589 | |
| 590 | spin_lock_irq(&wqe->lock); |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 591 | io_wqe_dec_running(wqe, worker); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 592 | spin_unlock_irq(&wqe->lock); |
| 593 | } |
| 594 | |
Jens Axboe | b60fda6 | 2019-11-19 08:37:07 -0700 | [diff] [blame] | 595 | static bool create_io_worker(struct io_wq *wq, struct io_wqe *wqe, int index) |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 596 | { |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 597 | struct io_wqe_acct *acct =&wqe->acct[index]; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 598 | struct io_worker *worker; |
| 599 | |
Jann Horn | ad6e005 | 2019-11-26 17:39:45 +0100 | [diff] [blame] | 600 | worker = kzalloc_node(sizeof(*worker), GFP_KERNEL, wqe->node); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 601 | if (!worker) |
Jens Axboe | b60fda6 | 2019-11-19 08:37:07 -0700 | [diff] [blame] | 602 | return false; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 603 | |
| 604 | refcount_set(&worker->ref, 1); |
| 605 | worker->nulls_node.pprev = NULL; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 606 | worker->wqe = wqe; |
Jens Axboe | 36c2f92 | 2019-11-13 09:43:34 -0700 | [diff] [blame] | 607 | spin_lock_init(&worker->lock); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 608 | |
| 609 | worker->task = kthread_create_on_node(io_wqe_worker, worker, wqe->node, |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 610 | "io_wqe_worker-%d/%d", index, wqe->node); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 611 | if (IS_ERR(worker->task)) { |
| 612 | kfree(worker); |
Jens Axboe | b60fda6 | 2019-11-19 08:37:07 -0700 | [diff] [blame] | 613 | return false; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | spin_lock_irq(&wqe->lock); |
Jens Axboe | 021d1cd | 2019-11-14 08:00:41 -0700 | [diff] [blame] | 617 | hlist_nulls_add_head_rcu(&worker->nulls_node, &wqe->free_list); |
Jens Axboe | e61df66 | 2019-11-13 13:54:49 -0700 | [diff] [blame] | 618 | list_add_tail_rcu(&worker->all_list, &wqe->all_list); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 619 | worker->flags |= IO_WORKER_F_FREE; |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 620 | if (index == IO_WQ_ACCT_BOUND) |
| 621 | worker->flags |= IO_WORKER_F_BOUND; |
| 622 | if (!acct->nr_workers && (worker->flags & IO_WORKER_F_BOUND)) |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 623 | worker->flags |= IO_WORKER_F_FIXED; |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 624 | acct->nr_workers++; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 625 | spin_unlock_irq(&wqe->lock); |
| 626 | |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 627 | if (index == IO_WQ_ACCT_UNBOUND) |
| 628 | atomic_inc(&wq->user->processes); |
| 629 | |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 630 | wake_up_process(worker->task); |
Jens Axboe | b60fda6 | 2019-11-19 08:37:07 -0700 | [diff] [blame] | 631 | return true; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 632 | } |
| 633 | |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 634 | 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] | 635 | __must_hold(wqe->lock) |
| 636 | { |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 637 | struct io_wqe_acct *acct = &wqe->acct[index]; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 638 | |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 639 | /* if we have available workers or no work, no need */ |
Jens Axboe | 021d1cd | 2019-11-14 08:00:41 -0700 | [diff] [blame] | 640 | if (!hlist_nulls_empty(&wqe->free_list) || !io_wqe_run_queue(wqe)) |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 641 | return false; |
| 642 | return acct->nr_workers < acct->max_workers; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 643 | } |
| 644 | |
| 645 | /* |
| 646 | * Manager thread. Tasked with creating new workers, if we need them. |
| 647 | */ |
| 648 | static int io_wq_manager(void *data) |
| 649 | { |
| 650 | struct io_wq *wq = data; |
Jann Horn | 3fc50ab | 2019-11-26 19:10:20 +0100 | [diff] [blame] | 651 | int workers_to_create = num_possible_nodes(); |
| 652 | int node; |
Jens Axboe | b60fda6 | 2019-11-19 08:37:07 -0700 | [diff] [blame] | 653 | |
| 654 | /* create fixed workers */ |
Jann Horn | 3fc50ab | 2019-11-26 19:10:20 +0100 | [diff] [blame] | 655 | refcount_set(&wq->refs, workers_to_create); |
| 656 | for_each_node(node) { |
| 657 | if (!create_io_worker(wq, wq->wqes[node], IO_WQ_ACCT_BOUND)) |
| 658 | goto err; |
| 659 | workers_to_create--; |
Jens Axboe | b60fda6 | 2019-11-19 08:37:07 -0700 | [diff] [blame] | 660 | } |
| 661 | |
| 662 | complete(&wq->done); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 663 | |
| 664 | while (!kthread_should_stop()) { |
Jann Horn | 3fc50ab | 2019-11-26 19:10:20 +0100 | [diff] [blame] | 665 | for_each_node(node) { |
| 666 | struct io_wqe *wqe = wq->wqes[node]; |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 667 | bool fork_worker[2] = { false, false }; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 668 | |
| 669 | spin_lock_irq(&wqe->lock); |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 670 | if (io_wqe_need_worker(wqe, IO_WQ_ACCT_BOUND)) |
| 671 | fork_worker[IO_WQ_ACCT_BOUND] = true; |
| 672 | if (io_wqe_need_worker(wqe, IO_WQ_ACCT_UNBOUND)) |
| 673 | fork_worker[IO_WQ_ACCT_UNBOUND] = true; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 674 | spin_unlock_irq(&wqe->lock); |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 675 | if (fork_worker[IO_WQ_ACCT_BOUND]) |
| 676 | create_io_worker(wq, wqe, IO_WQ_ACCT_BOUND); |
| 677 | if (fork_worker[IO_WQ_ACCT_UNBOUND]) |
| 678 | create_io_worker(wq, wqe, IO_WQ_ACCT_UNBOUND); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 679 | } |
| 680 | set_current_state(TASK_INTERRUPTIBLE); |
| 681 | schedule_timeout(HZ); |
| 682 | } |
| 683 | |
| 684 | return 0; |
Jens Axboe | b60fda6 | 2019-11-19 08:37:07 -0700 | [diff] [blame] | 685 | err: |
| 686 | set_bit(IO_WQ_BIT_ERROR, &wq->state); |
| 687 | set_bit(IO_WQ_BIT_EXIT, &wq->state); |
Jann Horn | 3fc50ab | 2019-11-26 19:10:20 +0100 | [diff] [blame] | 688 | if (refcount_sub_and_test(workers_to_create, &wq->refs)) |
Jens Axboe | b60fda6 | 2019-11-19 08:37:07 -0700 | [diff] [blame] | 689 | complete(&wq->done); |
| 690 | return 0; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 691 | } |
| 692 | |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 693 | static bool io_wq_can_queue(struct io_wqe *wqe, struct io_wqe_acct *acct, |
| 694 | struct io_wq_work *work) |
| 695 | { |
| 696 | bool free_worker; |
| 697 | |
| 698 | if (!(work->flags & IO_WQ_WORK_UNBOUND)) |
| 699 | return true; |
| 700 | if (atomic_read(&acct->nr_running)) |
| 701 | return true; |
| 702 | |
| 703 | rcu_read_lock(); |
Jens Axboe | 021d1cd | 2019-11-14 08:00:41 -0700 | [diff] [blame] | 704 | free_worker = !hlist_nulls_empty(&wqe->free_list); |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 705 | rcu_read_unlock(); |
| 706 | if (free_worker) |
| 707 | return true; |
| 708 | |
| 709 | if (atomic_read(&wqe->wq->user->processes) >= acct->max_workers && |
| 710 | !(capable(CAP_SYS_RESOURCE) || capable(CAP_SYS_ADMIN))) |
| 711 | return false; |
| 712 | |
| 713 | return true; |
| 714 | } |
| 715 | |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 716 | static void io_wqe_enqueue(struct io_wqe *wqe, struct io_wq_work *work) |
| 717 | { |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 718 | struct io_wqe_acct *acct = io_work_get_acct(wqe, work); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 719 | unsigned long flags; |
| 720 | |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 721 | /* |
| 722 | * Do early check to see if we need a new unbound worker, and if we do, |
| 723 | * if we're allowed to do so. This isn't 100% accurate as there's a |
| 724 | * gap between this check and incrementing the value, but that's OK. |
| 725 | * It's close enough to not be an issue, fork() has the same delay. |
| 726 | */ |
| 727 | if (unlikely(!io_wq_can_queue(wqe, acct, work))) { |
| 728 | work->flags |= IO_WQ_WORK_CANCEL; |
| 729 | work->func(&work); |
| 730 | return; |
| 731 | } |
| 732 | |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 733 | spin_lock_irqsave(&wqe->lock, flags); |
Jens Axboe | 6206f0e | 2019-11-26 11:59:32 -0700 | [diff] [blame] | 734 | wq_list_add_tail(&work->list, &wqe->work_list); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 735 | wqe->flags &= ~IO_WQE_FLAG_STALLED; |
| 736 | spin_unlock_irqrestore(&wqe->lock, flags); |
| 737 | |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 738 | if (!atomic_read(&acct->nr_running)) |
| 739 | io_wqe_wake_worker(wqe, acct); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 740 | } |
| 741 | |
| 742 | void io_wq_enqueue(struct io_wq *wq, struct io_wq_work *work) |
| 743 | { |
| 744 | struct io_wqe *wqe = wq->wqes[numa_node_id()]; |
| 745 | |
| 746 | io_wqe_enqueue(wqe, work); |
| 747 | } |
| 748 | |
| 749 | /* |
| 750 | * Enqueue work, hashed by some key. Work items that hash to the same value |
| 751 | * will not be done in parallel. Used to limit concurrent writes, generally |
| 752 | * hashed by inode. |
| 753 | */ |
| 754 | void io_wq_enqueue_hashed(struct io_wq *wq, struct io_wq_work *work, void *val) |
| 755 | { |
| 756 | struct io_wqe *wqe = wq->wqes[numa_node_id()]; |
| 757 | unsigned bit; |
| 758 | |
| 759 | |
| 760 | bit = hash_ptr(val, IO_WQ_HASH_ORDER); |
| 761 | work->flags |= (IO_WQ_WORK_HASHED | (bit << IO_WQ_HASH_SHIFT)); |
| 762 | io_wqe_enqueue(wqe, work); |
| 763 | } |
| 764 | |
| 765 | static bool io_wqe_worker_send_sig(struct io_worker *worker, void *data) |
| 766 | { |
| 767 | send_sig(SIGINT, worker->task, 1); |
| 768 | return false; |
| 769 | } |
| 770 | |
| 771 | /* |
| 772 | * Iterate the passed in list and call the specific function for each |
| 773 | * worker that isn't exiting |
| 774 | */ |
| 775 | static bool io_wq_for_each_worker(struct io_wqe *wqe, |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 776 | bool (*func)(struct io_worker *, void *), |
| 777 | void *data) |
| 778 | { |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 779 | struct io_worker *worker; |
| 780 | bool ret = false; |
| 781 | |
Jens Axboe | e61df66 | 2019-11-13 13:54:49 -0700 | [diff] [blame] | 782 | list_for_each_entry_rcu(worker, &wqe->all_list, all_list) { |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 783 | if (io_worker_get(worker)) { |
| 784 | ret = func(worker, data); |
| 785 | io_worker_release(worker); |
| 786 | if (ret) |
| 787 | break; |
| 788 | } |
| 789 | } |
Jens Axboe | e61df66 | 2019-11-13 13:54:49 -0700 | [diff] [blame] | 790 | |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 791 | return ret; |
| 792 | } |
| 793 | |
| 794 | void io_wq_cancel_all(struct io_wq *wq) |
| 795 | { |
Jann Horn | 3fc50ab | 2019-11-26 19:10:20 +0100 | [diff] [blame] | 796 | int node; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 797 | |
| 798 | set_bit(IO_WQ_BIT_CANCEL, &wq->state); |
| 799 | |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 800 | rcu_read_lock(); |
Jann Horn | 3fc50ab | 2019-11-26 19:10:20 +0100 | [diff] [blame] | 801 | for_each_node(node) { |
| 802 | struct io_wqe *wqe = wq->wqes[node]; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 803 | |
Jens Axboe | e61df66 | 2019-11-13 13:54:49 -0700 | [diff] [blame] | 804 | io_wq_for_each_worker(wqe, io_wqe_worker_send_sig, NULL); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 805 | } |
| 806 | rcu_read_unlock(); |
| 807 | } |
| 808 | |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 809 | struct io_cb_cancel_data { |
| 810 | struct io_wqe *wqe; |
| 811 | work_cancel_fn *cancel; |
| 812 | void *caller_data; |
| 813 | }; |
| 814 | |
| 815 | static bool io_work_cancel(struct io_worker *worker, void *cancel_data) |
| 816 | { |
| 817 | struct io_cb_cancel_data *data = cancel_data; |
Jens Axboe | 6f72653 | 2019-11-05 13:51:51 -0700 | [diff] [blame] | 818 | unsigned long flags; |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 819 | bool ret = false; |
| 820 | |
| 821 | /* |
| 822 | * Hold the lock to avoid ->cur_work going out of scope, caller |
Jens Axboe | 36c2f92 | 2019-11-13 09:43:34 -0700 | [diff] [blame] | 823 | * may dereference the passed in work. |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 824 | */ |
Jens Axboe | 36c2f92 | 2019-11-13 09:43:34 -0700 | [diff] [blame] | 825 | spin_lock_irqsave(&worker->lock, flags); |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 826 | if (worker->cur_work && |
| 827 | data->cancel(worker->cur_work, data->caller_data)) { |
| 828 | send_sig(SIGINT, worker->task, 1); |
| 829 | ret = true; |
| 830 | } |
Jens Axboe | 36c2f92 | 2019-11-13 09:43:34 -0700 | [diff] [blame] | 831 | spin_unlock_irqrestore(&worker->lock, flags); |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 832 | |
| 833 | return ret; |
| 834 | } |
| 835 | |
| 836 | static enum io_wq_cancel io_wqe_cancel_cb_work(struct io_wqe *wqe, |
| 837 | work_cancel_fn *cancel, |
| 838 | void *cancel_data) |
| 839 | { |
| 840 | struct io_cb_cancel_data data = { |
| 841 | .wqe = wqe, |
| 842 | .cancel = cancel, |
| 843 | .caller_data = cancel_data, |
| 844 | }; |
Jens Axboe | 6206f0e | 2019-11-26 11:59:32 -0700 | [diff] [blame] | 845 | struct io_wq_work_node *node, *prev; |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 846 | struct io_wq_work *work; |
Jens Axboe | 6f72653 | 2019-11-05 13:51:51 -0700 | [diff] [blame] | 847 | unsigned long flags; |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 848 | bool found = false; |
| 849 | |
Jens Axboe | 6f72653 | 2019-11-05 13:51:51 -0700 | [diff] [blame] | 850 | spin_lock_irqsave(&wqe->lock, flags); |
Jens Axboe | 6206f0e | 2019-11-26 11:59:32 -0700 | [diff] [blame] | 851 | wq_list_for_each(node, prev, &wqe->work_list) { |
| 852 | work = container_of(node, struct io_wq_work, list); |
| 853 | |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 854 | if (cancel(work, cancel_data)) { |
Jens Axboe | 6206f0e | 2019-11-26 11:59:32 -0700 | [diff] [blame] | 855 | wq_node_del(&wqe->work_list, node, prev); |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 856 | found = true; |
| 857 | break; |
| 858 | } |
| 859 | } |
Jens Axboe | 6f72653 | 2019-11-05 13:51:51 -0700 | [diff] [blame] | 860 | spin_unlock_irqrestore(&wqe->lock, flags); |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 861 | |
| 862 | if (found) { |
| 863 | work->flags |= IO_WQ_WORK_CANCEL; |
| 864 | work->func(&work); |
| 865 | return IO_WQ_CANCEL_OK; |
| 866 | } |
| 867 | |
| 868 | rcu_read_lock(); |
Jens Axboe | e61df66 | 2019-11-13 13:54:49 -0700 | [diff] [blame] | 869 | found = io_wq_for_each_worker(wqe, io_work_cancel, &data); |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 870 | rcu_read_unlock(); |
| 871 | return found ? IO_WQ_CANCEL_RUNNING : IO_WQ_CANCEL_NOTFOUND; |
| 872 | } |
| 873 | |
| 874 | enum io_wq_cancel io_wq_cancel_cb(struct io_wq *wq, work_cancel_fn *cancel, |
| 875 | void *data) |
| 876 | { |
| 877 | enum io_wq_cancel ret = IO_WQ_CANCEL_NOTFOUND; |
Jann Horn | 3fc50ab | 2019-11-26 19:10:20 +0100 | [diff] [blame] | 878 | int node; |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 879 | |
Jann Horn | 3fc50ab | 2019-11-26 19:10:20 +0100 | [diff] [blame] | 880 | for_each_node(node) { |
| 881 | struct io_wqe *wqe = wq->wqes[node]; |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 882 | |
| 883 | ret = io_wqe_cancel_cb_work(wqe, cancel, data); |
| 884 | if (ret != IO_WQ_CANCEL_NOTFOUND) |
| 885 | break; |
| 886 | } |
| 887 | |
| 888 | return ret; |
| 889 | } |
| 890 | |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 891 | static bool io_wq_worker_cancel(struct io_worker *worker, void *data) |
| 892 | { |
| 893 | struct io_wq_work *work = data; |
Jens Axboe | 36c2f92 | 2019-11-13 09:43:34 -0700 | [diff] [blame] | 894 | unsigned long flags; |
| 895 | bool ret = false; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 896 | |
Jens Axboe | 36c2f92 | 2019-11-13 09:43:34 -0700 | [diff] [blame] | 897 | if (worker->cur_work != work) |
| 898 | return false; |
| 899 | |
| 900 | spin_lock_irqsave(&worker->lock, flags); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 901 | if (worker->cur_work == work) { |
| 902 | send_sig(SIGINT, worker->task, 1); |
Jens Axboe | 36c2f92 | 2019-11-13 09:43:34 -0700 | [diff] [blame] | 903 | ret = true; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 904 | } |
Jens Axboe | 36c2f92 | 2019-11-13 09:43:34 -0700 | [diff] [blame] | 905 | spin_unlock_irqrestore(&worker->lock, flags); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 906 | |
Jens Axboe | 36c2f92 | 2019-11-13 09:43:34 -0700 | [diff] [blame] | 907 | return ret; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 908 | } |
| 909 | |
| 910 | static enum io_wq_cancel io_wqe_cancel_work(struct io_wqe *wqe, |
| 911 | struct io_wq_work *cwork) |
| 912 | { |
Jens Axboe | 6206f0e | 2019-11-26 11:59:32 -0700 | [diff] [blame] | 913 | struct io_wq_work_node *node, *prev; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 914 | struct io_wq_work *work; |
Jens Axboe | 6f72653 | 2019-11-05 13:51:51 -0700 | [diff] [blame] | 915 | unsigned long flags; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 916 | bool found = false; |
| 917 | |
| 918 | cwork->flags |= IO_WQ_WORK_CANCEL; |
| 919 | |
| 920 | /* |
| 921 | * First check pending list, if we're lucky we can just remove it |
| 922 | * from there. CANCEL_OK means that the work is returned as-new, |
| 923 | * no completion will be posted for it. |
| 924 | */ |
Jens Axboe | 6f72653 | 2019-11-05 13:51:51 -0700 | [diff] [blame] | 925 | spin_lock_irqsave(&wqe->lock, flags); |
Jens Axboe | 6206f0e | 2019-11-26 11:59:32 -0700 | [diff] [blame] | 926 | wq_list_for_each(node, prev, &wqe->work_list) { |
| 927 | work = container_of(node, struct io_wq_work, list); |
| 928 | |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 929 | if (work == cwork) { |
Jens Axboe | 6206f0e | 2019-11-26 11:59:32 -0700 | [diff] [blame] | 930 | wq_node_del(&wqe->work_list, node, prev); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 931 | found = true; |
| 932 | break; |
| 933 | } |
| 934 | } |
Jens Axboe | 6f72653 | 2019-11-05 13:51:51 -0700 | [diff] [blame] | 935 | spin_unlock_irqrestore(&wqe->lock, flags); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 936 | |
| 937 | if (found) { |
| 938 | work->flags |= IO_WQ_WORK_CANCEL; |
| 939 | work->func(&work); |
| 940 | return IO_WQ_CANCEL_OK; |
| 941 | } |
| 942 | |
| 943 | /* |
| 944 | * Now check if a free (going busy) or busy worker has the work |
| 945 | * currently running. If we find it there, we'll return CANCEL_RUNNING |
Brian Gianforcaro | d195a66 | 2019-12-13 03:09:50 -0800 | [diff] [blame] | 946 | * as an indication that we attempt to signal cancellation. The |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 947 | * completion will run normally in this case. |
| 948 | */ |
| 949 | rcu_read_lock(); |
Jens Axboe | e61df66 | 2019-11-13 13:54:49 -0700 | [diff] [blame] | 950 | found = io_wq_for_each_worker(wqe, io_wq_worker_cancel, cwork); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 951 | rcu_read_unlock(); |
| 952 | return found ? IO_WQ_CANCEL_RUNNING : IO_WQ_CANCEL_NOTFOUND; |
| 953 | } |
| 954 | |
| 955 | enum io_wq_cancel io_wq_cancel_work(struct io_wq *wq, struct io_wq_work *cwork) |
| 956 | { |
| 957 | enum io_wq_cancel ret = IO_WQ_CANCEL_NOTFOUND; |
Jann Horn | 3fc50ab | 2019-11-26 19:10:20 +0100 | [diff] [blame] | 958 | int node; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 959 | |
Jann Horn | 3fc50ab | 2019-11-26 19:10:20 +0100 | [diff] [blame] | 960 | for_each_node(node) { |
| 961 | struct io_wqe *wqe = wq->wqes[node]; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 962 | |
| 963 | ret = io_wqe_cancel_work(wqe, cwork); |
| 964 | if (ret != IO_WQ_CANCEL_NOTFOUND) |
| 965 | break; |
| 966 | } |
| 967 | |
| 968 | return ret; |
| 969 | } |
| 970 | |
| 971 | struct io_wq_flush_data { |
| 972 | struct io_wq_work work; |
| 973 | struct completion done; |
| 974 | }; |
| 975 | |
| 976 | static void io_wq_flush_func(struct io_wq_work **workptr) |
| 977 | { |
| 978 | struct io_wq_work *work = *workptr; |
| 979 | struct io_wq_flush_data *data; |
| 980 | |
| 981 | data = container_of(work, struct io_wq_flush_data, work); |
| 982 | complete(&data->done); |
| 983 | } |
| 984 | |
| 985 | /* |
| 986 | * Doesn't wait for previously queued work to finish. When this completes, |
| 987 | * it just means that previously queued work was started. |
| 988 | */ |
| 989 | void io_wq_flush(struct io_wq *wq) |
| 990 | { |
| 991 | struct io_wq_flush_data data; |
Jann Horn | 3fc50ab | 2019-11-26 19:10:20 +0100 | [diff] [blame] | 992 | int node; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 993 | |
Jann Horn | 3fc50ab | 2019-11-26 19:10:20 +0100 | [diff] [blame] | 994 | for_each_node(node) { |
| 995 | struct io_wqe *wqe = wq->wqes[node]; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 996 | |
| 997 | init_completion(&data.done); |
| 998 | INIT_IO_WORK(&data.work, io_wq_flush_func); |
Jens Axboe | 7d72306 | 2019-11-12 22:31:31 -0700 | [diff] [blame] | 999 | data.work.flags |= IO_WQ_WORK_INTERNAL; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 1000 | io_wqe_enqueue(wqe, &data.work); |
| 1001 | wait_for_completion(&data.done); |
| 1002 | } |
| 1003 | } |
| 1004 | |
Jens Axboe | 576a347 | 2019-11-25 08:49:20 -0700 | [diff] [blame] | 1005 | 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] | 1006 | { |
Jann Horn | 3fc50ab | 2019-11-26 19:10:20 +0100 | [diff] [blame] | 1007 | int ret = -ENOMEM, node; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 1008 | struct io_wq *wq; |
| 1009 | |
Jann Horn | ad6e005 | 2019-11-26 17:39:45 +0100 | [diff] [blame] | 1010 | wq = kzalloc(sizeof(*wq), GFP_KERNEL); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 1011 | if (!wq) |
| 1012 | return ERR_PTR(-ENOMEM); |
| 1013 | |
Jann Horn | 3fc50ab | 2019-11-26 19:10:20 +0100 | [diff] [blame] | 1014 | wq->wqes = kcalloc(nr_node_ids, sizeof(struct io_wqe *), GFP_KERNEL); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 1015 | if (!wq->wqes) { |
| 1016 | kfree(wq); |
| 1017 | return ERR_PTR(-ENOMEM); |
| 1018 | } |
| 1019 | |
Jens Axboe | 576a347 | 2019-11-25 08:49:20 -0700 | [diff] [blame] | 1020 | wq->get_work = data->get_work; |
| 1021 | wq->put_work = data->put_work; |
Jens Axboe | 7d72306 | 2019-11-12 22:31:31 -0700 | [diff] [blame] | 1022 | |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 1023 | /* caller must already hold a reference to this */ |
Jens Axboe | 576a347 | 2019-11-25 08:49:20 -0700 | [diff] [blame] | 1024 | wq->user = data->user; |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 1025 | wq->creds = data->creds; |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 1026 | |
Jann Horn | 3fc50ab | 2019-11-26 19:10:20 +0100 | [diff] [blame] | 1027 | for_each_node(node) { |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 1028 | struct io_wqe *wqe; |
| 1029 | |
Jann Horn | ad6e005 | 2019-11-26 17:39:45 +0100 | [diff] [blame] | 1030 | wqe = kzalloc_node(sizeof(struct io_wqe), GFP_KERNEL, node); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 1031 | if (!wqe) |
Jann Horn | 3fc50ab | 2019-11-26 19:10:20 +0100 | [diff] [blame] | 1032 | goto err; |
| 1033 | wq->wqes[node] = wqe; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 1034 | wqe->node = node; |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 1035 | wqe->acct[IO_WQ_ACCT_BOUND].max_workers = bounded; |
| 1036 | atomic_set(&wqe->acct[IO_WQ_ACCT_BOUND].nr_running, 0); |
Jens Axboe | 576a347 | 2019-11-25 08:49:20 -0700 | [diff] [blame] | 1037 | if (wq->user) { |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 1038 | wqe->acct[IO_WQ_ACCT_UNBOUND].max_workers = |
| 1039 | task_rlimit(current, RLIMIT_NPROC); |
| 1040 | } |
| 1041 | atomic_set(&wqe->acct[IO_WQ_ACCT_UNBOUND].nr_running, 0); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 1042 | wqe->node = node; |
| 1043 | wqe->wq = wq; |
| 1044 | spin_lock_init(&wqe->lock); |
Jens Axboe | 6206f0e | 2019-11-26 11:59:32 -0700 | [diff] [blame] | 1045 | INIT_WQ_LIST(&wqe->work_list); |
Jens Axboe | 021d1cd | 2019-11-14 08:00:41 -0700 | [diff] [blame] | 1046 | INIT_HLIST_NULLS_HEAD(&wqe->free_list, 0); |
Jens Axboe | e61df66 | 2019-11-13 13:54:49 -0700 | [diff] [blame] | 1047 | INIT_LIST_HEAD(&wqe->all_list); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 1048 | } |
| 1049 | |
| 1050 | init_completion(&wq->done); |
| 1051 | |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 1052 | /* caller must have already done mmgrab() on this mm */ |
Jens Axboe | 576a347 | 2019-11-25 08:49:20 -0700 | [diff] [blame] | 1053 | wq->mm = data->mm; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 1054 | |
| 1055 | wq->manager = kthread_create(io_wq_manager, wq, "io_wq_manager"); |
| 1056 | if (!IS_ERR(wq->manager)) { |
| 1057 | wake_up_process(wq->manager); |
Jens Axboe | b60fda6 | 2019-11-19 08:37:07 -0700 | [diff] [blame] | 1058 | wait_for_completion(&wq->done); |
| 1059 | if (test_bit(IO_WQ_BIT_ERROR, &wq->state)) { |
| 1060 | ret = -ENOMEM; |
| 1061 | goto err; |
| 1062 | } |
| 1063 | reinit_completion(&wq->done); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 1064 | return wq; |
| 1065 | } |
| 1066 | |
| 1067 | ret = PTR_ERR(wq->manager); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 1068 | complete(&wq->done); |
Jens Axboe | b60fda6 | 2019-11-19 08:37:07 -0700 | [diff] [blame] | 1069 | err: |
Jann Horn | 3fc50ab | 2019-11-26 19:10:20 +0100 | [diff] [blame] | 1070 | for_each_node(node) |
| 1071 | kfree(wq->wqes[node]); |
Jens Axboe | b60fda6 | 2019-11-19 08:37:07 -0700 | [diff] [blame] | 1072 | kfree(wq->wqes); |
| 1073 | kfree(wq); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 1074 | return ERR_PTR(ret); |
| 1075 | } |
| 1076 | |
| 1077 | static bool io_wq_worker_wake(struct io_worker *worker, void *data) |
| 1078 | { |
| 1079 | wake_up_process(worker->task); |
| 1080 | return false; |
| 1081 | } |
| 1082 | |
| 1083 | void io_wq_destroy(struct io_wq *wq) |
| 1084 | { |
Jann Horn | 3fc50ab | 2019-11-26 19:10:20 +0100 | [diff] [blame] | 1085 | int node; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 1086 | |
Jens Axboe | b60fda6 | 2019-11-19 08:37:07 -0700 | [diff] [blame] | 1087 | set_bit(IO_WQ_BIT_EXIT, &wq->state); |
| 1088 | if (wq->manager) |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 1089 | kthread_stop(wq->manager); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 1090 | |
| 1091 | rcu_read_lock(); |
Jann Horn | 3fc50ab | 2019-11-26 19:10:20 +0100 | [diff] [blame] | 1092 | for_each_node(node) |
| 1093 | io_wq_for_each_worker(wq->wqes[node], io_wq_worker_wake, NULL); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 1094 | rcu_read_unlock(); |
| 1095 | |
| 1096 | wait_for_completion(&wq->done); |
| 1097 | |
Jann Horn | 3fc50ab | 2019-11-26 19:10:20 +0100 | [diff] [blame] | 1098 | for_each_node(node) |
| 1099 | kfree(wq->wqes[node]); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 1100 | kfree(wq->wqes); |
| 1101 | kfree(wq); |
| 1102 | } |