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