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 | |
Pavel Begunkov | 60cf46a | 2020-03-14 00:31:05 +0300 | [diff] [blame] | 378 | static inline unsigned int io_get_work_hash(struct io_wq_work *work) |
| 379 | { |
| 380 | return work->flags >> IO_WQ_HASH_SHIFT; |
| 381 | } |
| 382 | |
| 383 | static struct io_wq_work *io_get_next_work(struct io_wqe *wqe) |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 384 | __must_hold(wqe->lock) |
| 385 | { |
Jens Axboe | 6206f0e | 2019-11-26 11:59:32 -0700 | [diff] [blame] | 386 | struct io_wq_work_node *node, *prev; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 387 | struct io_wq_work *work; |
Pavel Begunkov | 60cf46a | 2020-03-14 00:31:05 +0300 | [diff] [blame] | 388 | unsigned int hash; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 389 | |
Jens Axboe | 6206f0e | 2019-11-26 11:59:32 -0700 | [diff] [blame] | 390 | wq_list_for_each(node, prev, &wqe->work_list) { |
| 391 | work = container_of(node, struct io_wq_work, list); |
| 392 | |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 393 | /* not hashed, can run anytime */ |
Pavel Begunkov | 8766dd5 | 2020-03-14 00:31:04 +0300 | [diff] [blame] | 394 | if (!io_wq_is_hashed(work)) { |
Jens Axboe | 6206f0e | 2019-11-26 11:59:32 -0700 | [diff] [blame] | 395 | wq_node_del(&wqe->work_list, node, prev); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 396 | return work; |
| 397 | } |
| 398 | |
| 399 | /* hashed, can run if not already running */ |
Pavel Begunkov | 60cf46a | 2020-03-14 00:31:05 +0300 | [diff] [blame] | 400 | hash = io_get_work_hash(work); |
| 401 | if (!(wqe->hash_map & BIT(hash))) { |
| 402 | wqe->hash_map |= BIT(hash); |
Jens Axboe | 6206f0e | 2019-11-26 11:59:32 -0700 | [diff] [blame] | 403 | wq_node_del(&wqe->work_list, node, prev); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 404 | return work; |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | return NULL; |
| 409 | } |
| 410 | |
Jens Axboe | cccf0ee | 2020-01-27 16:34:48 -0700 | [diff] [blame] | 411 | static void io_wq_switch_mm(struct io_worker *worker, struct io_wq_work *work) |
| 412 | { |
| 413 | if (worker->mm) { |
| 414 | unuse_mm(worker->mm); |
| 415 | mmput(worker->mm); |
| 416 | worker->mm = NULL; |
| 417 | } |
| 418 | if (!work->mm) { |
| 419 | set_fs(KERNEL_DS); |
| 420 | return; |
| 421 | } |
| 422 | if (mmget_not_zero(work->mm)) { |
| 423 | use_mm(work->mm); |
| 424 | if (!worker->mm) |
| 425 | set_fs(USER_DS); |
| 426 | worker->mm = work->mm; |
| 427 | /* hang on to this mm */ |
| 428 | work->mm = NULL; |
| 429 | return; |
| 430 | } |
| 431 | |
| 432 | /* failed grabbing mm, ensure work gets cancelled */ |
| 433 | work->flags |= IO_WQ_WORK_CANCEL; |
| 434 | } |
| 435 | |
| 436 | static void io_wq_switch_creds(struct io_worker *worker, |
| 437 | struct io_wq_work *work) |
| 438 | { |
| 439 | const struct cred *old_creds = override_creds(work->creds); |
| 440 | |
| 441 | worker->cur_creds = work->creds; |
| 442 | if (worker->saved_creds) |
| 443 | put_cred(old_creds); /* creds set by previous switch */ |
| 444 | else |
| 445 | worker->saved_creds = old_creds; |
| 446 | } |
| 447 | |
Pavel Begunkov | dc026a7 | 2020-03-04 16:14:09 +0300 | [diff] [blame] | 448 | static void io_impersonate_work(struct io_worker *worker, |
| 449 | struct io_wq_work *work) |
| 450 | { |
| 451 | if (work->files && current->files != work->files) { |
| 452 | task_lock(current); |
| 453 | current->files = work->files; |
| 454 | task_unlock(current); |
| 455 | } |
| 456 | if (work->fs && current->fs != work->fs) |
| 457 | current->fs = work->fs; |
| 458 | if (work->mm != worker->mm) |
| 459 | io_wq_switch_mm(worker, work); |
| 460 | if (worker->cur_creds != work->creds) |
| 461 | io_wq_switch_creds(worker, work); |
| 462 | } |
| 463 | |
| 464 | static void io_assign_current_work(struct io_worker *worker, |
| 465 | struct io_wq_work *work) |
| 466 | { |
Pavel Begunkov | d78298e | 2020-03-14 00:31:03 +0300 | [diff] [blame] | 467 | if (work) { |
| 468 | /* flush pending signals before assigning new work */ |
| 469 | if (signal_pending(current)) |
| 470 | flush_signals(current); |
| 471 | cond_resched(); |
| 472 | } |
Pavel Begunkov | dc026a7 | 2020-03-04 16:14:09 +0300 | [diff] [blame] | 473 | |
| 474 | spin_lock_irq(&worker->lock); |
| 475 | worker->cur_work = work; |
| 476 | spin_unlock_irq(&worker->lock); |
| 477 | } |
| 478 | |
Pavel Begunkov | 60cf46a | 2020-03-14 00:31:05 +0300 | [diff] [blame] | 479 | static void io_wqe_enqueue(struct io_wqe *wqe, struct io_wq_work *work); |
| 480 | |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 481 | static void io_worker_handle_work(struct io_worker *worker) |
| 482 | __releases(wqe->lock) |
| 483 | { |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 484 | struct io_wqe *wqe = worker->wqe; |
| 485 | struct io_wq *wq = wqe->wq; |
| 486 | |
| 487 | do { |
Pavel Begunkov | f2cf114 | 2020-03-22 19:14:26 +0300 | [diff] [blame] | 488 | struct io_wq_work *work, *assign_work; |
Pavel Begunkov | 60cf46a | 2020-03-14 00:31:05 +0300 | [diff] [blame] | 489 | unsigned int hash; |
Pavel Begunkov | f462fd3 | 2020-03-04 16:14:11 +0300 | [diff] [blame] | 490 | get_next: |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 491 | /* |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 492 | * If we got some work, mark us as busy. If we didn't, but |
| 493 | * the list isn't empty, it means we stalled on hashed work. |
| 494 | * Mark us stalled so we don't keep looking for work when we |
| 495 | * can't make progress, any work completion or insertion will |
| 496 | * clear the stalled flag. |
| 497 | */ |
Pavel Begunkov | 60cf46a | 2020-03-14 00:31:05 +0300 | [diff] [blame] | 498 | work = io_get_next_work(wqe); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 499 | if (work) |
| 500 | __io_worker_busy(wqe, worker, work); |
Jens Axboe | 6206f0e | 2019-11-26 11:59:32 -0700 | [diff] [blame] | 501 | else if (!wq_list_empty(&wqe->work_list)) |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 502 | wqe->flags |= IO_WQE_FLAG_STALLED; |
| 503 | |
| 504 | spin_unlock_irq(&wqe->lock); |
| 505 | if (!work) |
| 506 | break; |
Pavel Begunkov | 58e3931 | 2020-03-04 16:14:10 +0300 | [diff] [blame] | 507 | io_assign_current_work(worker, work); |
Jens Axboe | 36c2f92 | 2019-11-13 09:43:34 -0700 | [diff] [blame] | 508 | |
Pavel Begunkov | dc026a7 | 2020-03-04 16:14:09 +0300 | [diff] [blame] | 509 | /* handle a whole dependent link */ |
| 510 | do { |
Pavel Begunkov | 58e3931 | 2020-03-04 16:14:10 +0300 | [diff] [blame] | 511 | struct io_wq_work *old_work; |
Hillf Danton | fd1c4bc | 2019-12-24 09:14:29 -0700 | [diff] [blame] | 512 | |
Pavel Begunkov | 58e3931 | 2020-03-04 16:14:10 +0300 | [diff] [blame] | 513 | io_impersonate_work(worker, work); |
Pavel Begunkov | dc026a7 | 2020-03-04 16:14:09 +0300 | [diff] [blame] | 514 | /* |
| 515 | * OK to set IO_WQ_WORK_CANCEL even for uncancellable |
| 516 | * work, the worker function will do the right thing. |
| 517 | */ |
| 518 | if (test_bit(IO_WQ_BIT_CANCEL, &wq->state)) |
| 519 | work->flags |= IO_WQ_WORK_CANCEL; |
Jens Axboe | 36c2f92 | 2019-11-13 09:43:34 -0700 | [diff] [blame] | 520 | |
Pavel Begunkov | dc026a7 | 2020-03-04 16:14:09 +0300 | [diff] [blame] | 521 | old_work = work; |
Pavel Begunkov | 60cf46a | 2020-03-14 00:31:05 +0300 | [diff] [blame] | 522 | hash = io_get_work_hash(work); |
Pavel Begunkov | dc026a7 | 2020-03-04 16:14:09 +0300 | [diff] [blame] | 523 | work->func(&work); |
Pavel Begunkov | 58e3931 | 2020-03-04 16:14:10 +0300 | [diff] [blame] | 524 | work = (old_work == work) ? NULL : work; |
Pavel Begunkov | f2cf114 | 2020-03-22 19:14:26 +0300 | [diff] [blame] | 525 | |
| 526 | assign_work = work; |
| 527 | if (work && io_wq_is_hashed(work)) |
| 528 | assign_work = NULL; |
| 529 | io_assign_current_work(worker, assign_work); |
Pavel Begunkov | e9fd939 | 2020-03-04 16:14:12 +0300 | [diff] [blame] | 530 | wq->free_work(old_work); |
Pavel Begunkov | dc026a7 | 2020-03-04 16:14:09 +0300 | [diff] [blame] | 531 | |
Pavel Begunkov | f2cf114 | 2020-03-22 19:14:26 +0300 | [diff] [blame] | 532 | if (work && !assign_work) { |
Pavel Begunkov | 60cf46a | 2020-03-14 00:31:05 +0300 | [diff] [blame] | 533 | io_wqe_enqueue(wqe, work); |
| 534 | work = NULL; |
| 535 | } |
Pavel Begunkov | dc026a7 | 2020-03-04 16:14:09 +0300 | [diff] [blame] | 536 | if (hash != -1U) { |
| 537 | spin_lock_irq(&wqe->lock); |
| 538 | wqe->hash_map &= ~BIT_ULL(hash); |
| 539 | wqe->flags &= ~IO_WQE_FLAG_STALLED; |
Pavel Begunkov | dc026a7 | 2020-03-04 16:14:09 +0300 | [diff] [blame] | 540 | /* dependent work is not hashed */ |
| 541 | hash = -1U; |
Pavel Begunkov | f462fd3 | 2020-03-04 16:14:11 +0300 | [diff] [blame] | 542 | /* skip unnecessary unlock-lock wqe->lock */ |
| 543 | if (!work) |
| 544 | goto get_next; |
| 545 | spin_unlock_irq(&wqe->lock); |
Pavel Begunkov | dc026a7 | 2020-03-04 16:14:09 +0300 | [diff] [blame] | 546 | } |
Pavel Begunkov | 58e3931 | 2020-03-04 16:14:10 +0300 | [diff] [blame] | 547 | } while (work); |
Jens Axboe | 36c2f92 | 2019-11-13 09:43:34 -0700 | [diff] [blame] | 548 | |
| 549 | spin_lock_irq(&wqe->lock); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 550 | } while (1); |
| 551 | } |
| 552 | |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 553 | static int io_wqe_worker(void *data) |
| 554 | { |
| 555 | struct io_worker *worker = data; |
| 556 | struct io_wqe *wqe = worker->wqe; |
| 557 | struct io_wq *wq = wqe->wq; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 558 | |
| 559 | io_worker_start(wqe, worker); |
| 560 | |
| 561 | while (!test_bit(IO_WQ_BIT_EXIT, &wq->state)) { |
Jens Axboe | 506d95f | 2019-12-07 21:03:59 -0700 | [diff] [blame] | 562 | set_current_state(TASK_INTERRUPTIBLE); |
Jens Axboe | e995d51 | 2019-12-07 21:06:46 -0700 | [diff] [blame] | 563 | loop: |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 564 | spin_lock_irq(&wqe->lock); |
| 565 | if (io_wqe_run_queue(wqe)) { |
| 566 | __set_current_state(TASK_RUNNING); |
| 567 | io_worker_handle_work(worker); |
Jens Axboe | e995d51 | 2019-12-07 21:06:46 -0700 | [diff] [blame] | 568 | goto loop; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 569 | } |
| 570 | /* drops the lock on success, retry */ |
| 571 | if (__io_worker_idle(wqe, worker)) { |
| 572 | __release(&wqe->lock); |
Jens Axboe | e995d51 | 2019-12-07 21:06:46 -0700 | [diff] [blame] | 573 | goto loop; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 574 | } |
| 575 | spin_unlock_irq(&wqe->lock); |
| 576 | if (signal_pending(current)) |
| 577 | flush_signals(current); |
| 578 | if (schedule_timeout(WORKER_IDLE_TIMEOUT)) |
| 579 | continue; |
| 580 | /* timed out, exit unless we're the fixed worker */ |
| 581 | if (test_bit(IO_WQ_BIT_EXIT, &wq->state) || |
| 582 | !(worker->flags & IO_WORKER_F_FIXED)) |
| 583 | break; |
| 584 | } |
| 585 | |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 586 | if (test_bit(IO_WQ_BIT_EXIT, &wq->state)) { |
| 587 | spin_lock_irq(&wqe->lock); |
Jens Axboe | 6206f0e | 2019-11-26 11:59:32 -0700 | [diff] [blame] | 588 | if (!wq_list_empty(&wqe->work_list)) |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 589 | io_worker_handle_work(worker); |
| 590 | else |
| 591 | spin_unlock_irq(&wqe->lock); |
| 592 | } |
| 593 | |
| 594 | io_worker_exit(worker); |
| 595 | return 0; |
| 596 | } |
| 597 | |
| 598 | /* |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 599 | * Called when a worker is scheduled in. Mark us as currently running. |
| 600 | */ |
| 601 | void io_wq_worker_running(struct task_struct *tsk) |
| 602 | { |
| 603 | struct io_worker *worker = kthread_data(tsk); |
| 604 | struct io_wqe *wqe = worker->wqe; |
| 605 | |
| 606 | if (!(worker->flags & IO_WORKER_F_UP)) |
| 607 | return; |
| 608 | if (worker->flags & IO_WORKER_F_RUNNING) |
| 609 | return; |
| 610 | worker->flags |= IO_WORKER_F_RUNNING; |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 611 | io_wqe_inc_running(wqe, worker); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | /* |
| 615 | * Called when worker is going to sleep. If there are no workers currently |
| 616 | * running and we have work pending, wake up a free one or have the manager |
| 617 | * set one up. |
| 618 | */ |
| 619 | void io_wq_worker_sleeping(struct task_struct *tsk) |
| 620 | { |
| 621 | struct io_worker *worker = kthread_data(tsk); |
| 622 | struct io_wqe *wqe = worker->wqe; |
| 623 | |
| 624 | if (!(worker->flags & IO_WORKER_F_UP)) |
| 625 | return; |
| 626 | if (!(worker->flags & IO_WORKER_F_RUNNING)) |
| 627 | return; |
| 628 | |
| 629 | worker->flags &= ~IO_WORKER_F_RUNNING; |
| 630 | |
| 631 | spin_lock_irq(&wqe->lock); |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 632 | io_wqe_dec_running(wqe, worker); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 633 | spin_unlock_irq(&wqe->lock); |
| 634 | } |
| 635 | |
Jens Axboe | b60fda6 | 2019-11-19 08:37:07 -0700 | [diff] [blame] | 636 | 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] | 637 | { |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 638 | struct io_wqe_acct *acct =&wqe->acct[index]; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 639 | struct io_worker *worker; |
| 640 | |
Jann Horn | ad6e005 | 2019-11-26 17:39:45 +0100 | [diff] [blame] | 641 | worker = kzalloc_node(sizeof(*worker), GFP_KERNEL, wqe->node); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 642 | if (!worker) |
Jens Axboe | b60fda6 | 2019-11-19 08:37:07 -0700 | [diff] [blame] | 643 | return false; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 644 | |
| 645 | refcount_set(&worker->ref, 1); |
| 646 | worker->nulls_node.pprev = NULL; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 647 | worker->wqe = wqe; |
Jens Axboe | 36c2f92 | 2019-11-13 09:43:34 -0700 | [diff] [blame] | 648 | spin_lock_init(&worker->lock); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 649 | |
| 650 | worker->task = kthread_create_on_node(io_wqe_worker, worker, wqe->node, |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 651 | "io_wqe_worker-%d/%d", index, wqe->node); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 652 | if (IS_ERR(worker->task)) { |
| 653 | kfree(worker); |
Jens Axboe | b60fda6 | 2019-11-19 08:37:07 -0700 | [diff] [blame] | 654 | return false; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 655 | } |
| 656 | |
| 657 | spin_lock_irq(&wqe->lock); |
Jens Axboe | 021d1cd | 2019-11-14 08:00:41 -0700 | [diff] [blame] | 658 | hlist_nulls_add_head_rcu(&worker->nulls_node, &wqe->free_list); |
Jens Axboe | e61df66 | 2019-11-13 13:54:49 -0700 | [diff] [blame] | 659 | list_add_tail_rcu(&worker->all_list, &wqe->all_list); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 660 | worker->flags |= IO_WORKER_F_FREE; |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 661 | if (index == IO_WQ_ACCT_BOUND) |
| 662 | worker->flags |= IO_WORKER_F_BOUND; |
| 663 | if (!acct->nr_workers && (worker->flags & IO_WORKER_F_BOUND)) |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 664 | worker->flags |= IO_WORKER_F_FIXED; |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 665 | acct->nr_workers++; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 666 | spin_unlock_irq(&wqe->lock); |
| 667 | |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 668 | if (index == IO_WQ_ACCT_UNBOUND) |
| 669 | atomic_inc(&wq->user->processes); |
| 670 | |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 671 | wake_up_process(worker->task); |
Jens Axboe | b60fda6 | 2019-11-19 08:37:07 -0700 | [diff] [blame] | 672 | return true; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 673 | } |
| 674 | |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 675 | 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] | 676 | __must_hold(wqe->lock) |
| 677 | { |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 678 | struct io_wqe_acct *acct = &wqe->acct[index]; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 679 | |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 680 | /* if we have available workers or no work, no need */ |
Jens Axboe | 021d1cd | 2019-11-14 08:00:41 -0700 | [diff] [blame] | 681 | if (!hlist_nulls_empty(&wqe->free_list) || !io_wqe_run_queue(wqe)) |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 682 | return false; |
| 683 | return acct->nr_workers < acct->max_workers; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 684 | } |
| 685 | |
| 686 | /* |
| 687 | * Manager thread. Tasked with creating new workers, if we need them. |
| 688 | */ |
| 689 | static int io_wq_manager(void *data) |
| 690 | { |
| 691 | struct io_wq *wq = data; |
Jann Horn | 3fc50ab | 2019-11-26 19:10:20 +0100 | [diff] [blame] | 692 | int workers_to_create = num_possible_nodes(); |
| 693 | int node; |
Jens Axboe | b60fda6 | 2019-11-19 08:37:07 -0700 | [diff] [blame] | 694 | |
| 695 | /* create fixed workers */ |
Jann Horn | 3fc50ab | 2019-11-26 19:10:20 +0100 | [diff] [blame] | 696 | refcount_set(&wq->refs, workers_to_create); |
| 697 | for_each_node(node) { |
Jens Axboe | 7563439 | 2020-02-11 06:30:06 -0700 | [diff] [blame] | 698 | if (!node_online(node)) |
| 699 | continue; |
Jann Horn | 3fc50ab | 2019-11-26 19:10:20 +0100 | [diff] [blame] | 700 | if (!create_io_worker(wq, wq->wqes[node], IO_WQ_ACCT_BOUND)) |
| 701 | goto err; |
| 702 | workers_to_create--; |
Jens Axboe | b60fda6 | 2019-11-19 08:37:07 -0700 | [diff] [blame] | 703 | } |
| 704 | |
Jens Axboe | 7563439 | 2020-02-11 06:30:06 -0700 | [diff] [blame] | 705 | while (workers_to_create--) |
| 706 | refcount_dec(&wq->refs); |
| 707 | |
Jens Axboe | b60fda6 | 2019-11-19 08:37:07 -0700 | [diff] [blame] | 708 | complete(&wq->done); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 709 | |
| 710 | while (!kthread_should_stop()) { |
Jann Horn | 3fc50ab | 2019-11-26 19:10:20 +0100 | [diff] [blame] | 711 | for_each_node(node) { |
| 712 | struct io_wqe *wqe = wq->wqes[node]; |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 713 | bool fork_worker[2] = { false, false }; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 714 | |
Jens Axboe | 7563439 | 2020-02-11 06:30:06 -0700 | [diff] [blame] | 715 | if (!node_online(node)) |
| 716 | continue; |
| 717 | |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 718 | spin_lock_irq(&wqe->lock); |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 719 | if (io_wqe_need_worker(wqe, IO_WQ_ACCT_BOUND)) |
| 720 | fork_worker[IO_WQ_ACCT_BOUND] = true; |
| 721 | if (io_wqe_need_worker(wqe, IO_WQ_ACCT_UNBOUND)) |
| 722 | fork_worker[IO_WQ_ACCT_UNBOUND] = true; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 723 | spin_unlock_irq(&wqe->lock); |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 724 | if (fork_worker[IO_WQ_ACCT_BOUND]) |
| 725 | create_io_worker(wq, wqe, IO_WQ_ACCT_BOUND); |
| 726 | if (fork_worker[IO_WQ_ACCT_UNBOUND]) |
| 727 | create_io_worker(wq, wqe, IO_WQ_ACCT_UNBOUND); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 728 | } |
| 729 | set_current_state(TASK_INTERRUPTIBLE); |
| 730 | schedule_timeout(HZ); |
| 731 | } |
| 732 | |
| 733 | return 0; |
Jens Axboe | b60fda6 | 2019-11-19 08:37:07 -0700 | [diff] [blame] | 734 | err: |
| 735 | set_bit(IO_WQ_BIT_ERROR, &wq->state); |
| 736 | set_bit(IO_WQ_BIT_EXIT, &wq->state); |
Jann Horn | 3fc50ab | 2019-11-26 19:10:20 +0100 | [diff] [blame] | 737 | if (refcount_sub_and_test(workers_to_create, &wq->refs)) |
Jens Axboe | b60fda6 | 2019-11-19 08:37:07 -0700 | [diff] [blame] | 738 | complete(&wq->done); |
| 739 | return 0; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 740 | } |
| 741 | |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 742 | static bool io_wq_can_queue(struct io_wqe *wqe, struct io_wqe_acct *acct, |
| 743 | struct io_wq_work *work) |
| 744 | { |
| 745 | bool free_worker; |
| 746 | |
| 747 | if (!(work->flags & IO_WQ_WORK_UNBOUND)) |
| 748 | return true; |
| 749 | if (atomic_read(&acct->nr_running)) |
| 750 | return true; |
| 751 | |
| 752 | rcu_read_lock(); |
Jens Axboe | 021d1cd | 2019-11-14 08:00:41 -0700 | [diff] [blame] | 753 | free_worker = !hlist_nulls_empty(&wqe->free_list); |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 754 | rcu_read_unlock(); |
| 755 | if (free_worker) |
| 756 | return true; |
| 757 | |
| 758 | if (atomic_read(&wqe->wq->user->processes) >= acct->max_workers && |
| 759 | !(capable(CAP_SYS_RESOURCE) || capable(CAP_SYS_ADMIN))) |
| 760 | return false; |
| 761 | |
| 762 | return true; |
| 763 | } |
| 764 | |
Pavel Begunkov | e9fd939 | 2020-03-04 16:14:12 +0300 | [diff] [blame] | 765 | 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] | 766 | { |
Pavel Begunkov | e9fd939 | 2020-03-04 16:14:12 +0300 | [diff] [blame] | 767 | struct io_wq *wq = wqe->wq; |
| 768 | |
Pavel Begunkov | fc04c39 | 2020-03-01 19:18:19 +0300 | [diff] [blame] | 769 | do { |
| 770 | struct io_wq_work *old_work = work; |
| 771 | |
| 772 | work->flags |= IO_WQ_WORK_CANCEL; |
| 773 | work->func(&work); |
| 774 | work = (work == old_work) ? NULL : work; |
Pavel Begunkov | e9fd939 | 2020-03-04 16:14:12 +0300 | [diff] [blame] | 775 | wq->free_work(old_work); |
Pavel Begunkov | fc04c39 | 2020-03-01 19:18:19 +0300 | [diff] [blame] | 776 | } while (work); |
| 777 | } |
| 778 | |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 779 | static void io_wqe_enqueue(struct io_wqe *wqe, struct io_wq_work *work) |
| 780 | { |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 781 | struct io_wqe_acct *acct = io_work_get_acct(wqe, work); |
Jens Axboe | 895e2ca | 2019-12-17 08:46:33 -0700 | [diff] [blame] | 782 | int work_flags; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 783 | unsigned long flags; |
| 784 | |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 785 | /* |
| 786 | * Do early check to see if we need a new unbound worker, and if we do, |
| 787 | * if we're allowed to do so. This isn't 100% accurate as there's a |
| 788 | * gap between this check and incrementing the value, but that's OK. |
| 789 | * It's close enough to not be an issue, fork() has the same delay. |
| 790 | */ |
| 791 | if (unlikely(!io_wq_can_queue(wqe, acct, work))) { |
Pavel Begunkov | e9fd939 | 2020-03-04 16:14:12 +0300 | [diff] [blame] | 792 | io_run_cancel(work, wqe); |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 793 | return; |
| 794 | } |
| 795 | |
Jens Axboe | 895e2ca | 2019-12-17 08:46:33 -0700 | [diff] [blame] | 796 | work_flags = work->flags; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 797 | spin_lock_irqsave(&wqe->lock, flags); |
Jens Axboe | 6206f0e | 2019-11-26 11:59:32 -0700 | [diff] [blame] | 798 | wq_list_add_tail(&work->list, &wqe->work_list); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 799 | wqe->flags &= ~IO_WQE_FLAG_STALLED; |
| 800 | spin_unlock_irqrestore(&wqe->lock, flags); |
| 801 | |
Jens Axboe | 895e2ca | 2019-12-17 08:46:33 -0700 | [diff] [blame] | 802 | if ((work_flags & IO_WQ_WORK_CONCURRENT) || |
| 803 | !atomic_read(&acct->nr_running)) |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 804 | io_wqe_wake_worker(wqe, acct); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 805 | } |
| 806 | |
| 807 | void io_wq_enqueue(struct io_wq *wq, struct io_wq_work *work) |
| 808 | { |
| 809 | struct io_wqe *wqe = wq->wqes[numa_node_id()]; |
| 810 | |
| 811 | io_wqe_enqueue(wqe, work); |
| 812 | } |
| 813 | |
| 814 | /* |
Pavel Begunkov | 8766dd5 | 2020-03-14 00:31:04 +0300 | [diff] [blame] | 815 | * Work items that hash to the same value will not be done in parallel. |
| 816 | * Used to limit concurrent writes, generally hashed by inode. |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 817 | */ |
Pavel Begunkov | 8766dd5 | 2020-03-14 00:31:04 +0300 | [diff] [blame] | 818 | void io_wq_hash_work(struct io_wq_work *work, void *val) |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 819 | { |
Pavel Begunkov | 8766dd5 | 2020-03-14 00:31:04 +0300 | [diff] [blame] | 820 | unsigned int bit; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 821 | |
| 822 | bit = hash_ptr(val, IO_WQ_HASH_ORDER); |
| 823 | work->flags |= (IO_WQ_WORK_HASHED | (bit << IO_WQ_HASH_SHIFT)); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 824 | } |
| 825 | |
| 826 | static bool io_wqe_worker_send_sig(struct io_worker *worker, void *data) |
| 827 | { |
| 828 | send_sig(SIGINT, worker->task, 1); |
| 829 | return false; |
| 830 | } |
| 831 | |
| 832 | /* |
| 833 | * Iterate the passed in list and call the specific function for each |
| 834 | * worker that isn't exiting |
| 835 | */ |
| 836 | static bool io_wq_for_each_worker(struct io_wqe *wqe, |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 837 | bool (*func)(struct io_worker *, void *), |
| 838 | void *data) |
| 839 | { |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 840 | struct io_worker *worker; |
| 841 | bool ret = false; |
| 842 | |
Jens Axboe | e61df66 | 2019-11-13 13:54:49 -0700 | [diff] [blame] | 843 | list_for_each_entry_rcu(worker, &wqe->all_list, all_list) { |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 844 | if (io_worker_get(worker)) { |
Jens Axboe | 7563439 | 2020-02-11 06:30:06 -0700 | [diff] [blame] | 845 | /* no task if node is/was offline */ |
| 846 | if (worker->task) |
| 847 | ret = func(worker, data); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 848 | io_worker_release(worker); |
| 849 | if (ret) |
| 850 | break; |
| 851 | } |
| 852 | } |
Jens Axboe | e61df66 | 2019-11-13 13:54:49 -0700 | [diff] [blame] | 853 | |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 854 | return ret; |
| 855 | } |
| 856 | |
| 857 | void io_wq_cancel_all(struct io_wq *wq) |
| 858 | { |
Jann Horn | 3fc50ab | 2019-11-26 19:10:20 +0100 | [diff] [blame] | 859 | int node; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 860 | |
| 861 | set_bit(IO_WQ_BIT_CANCEL, &wq->state); |
| 862 | |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 863 | rcu_read_lock(); |
Jann Horn | 3fc50ab | 2019-11-26 19:10:20 +0100 | [diff] [blame] | 864 | for_each_node(node) { |
| 865 | struct io_wqe *wqe = wq->wqes[node]; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 866 | |
Jens Axboe | e61df66 | 2019-11-13 13:54:49 -0700 | [diff] [blame] | 867 | io_wq_for_each_worker(wqe, io_wqe_worker_send_sig, NULL); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 868 | } |
| 869 | rcu_read_unlock(); |
| 870 | } |
| 871 | |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 872 | struct io_cb_cancel_data { |
Pavel Begunkov | 2293b41 | 2020-03-07 01:15:39 +0300 | [diff] [blame] | 873 | work_cancel_fn *fn; |
| 874 | void *data; |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 875 | }; |
| 876 | |
Pavel Begunkov | 2293b41 | 2020-03-07 01:15:39 +0300 | [diff] [blame] | 877 | static bool io_wq_worker_cancel(struct io_worker *worker, void *data) |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 878 | { |
Pavel Begunkov | 2293b41 | 2020-03-07 01:15:39 +0300 | [diff] [blame] | 879 | struct io_cb_cancel_data *match = data; |
Jens Axboe | 6f72653 | 2019-11-05 13:51:51 -0700 | [diff] [blame] | 880 | unsigned long flags; |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 881 | bool ret = false; |
| 882 | |
| 883 | /* |
| 884 | * Hold the lock to avoid ->cur_work going out of scope, caller |
Jens Axboe | 36c2f92 | 2019-11-13 09:43:34 -0700 | [diff] [blame] | 885 | * may dereference the passed in work. |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 886 | */ |
Jens Axboe | 36c2f92 | 2019-11-13 09:43:34 -0700 | [diff] [blame] | 887 | spin_lock_irqsave(&worker->lock, flags); |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 888 | if (worker->cur_work && |
Jens Axboe | 0c9d5cc | 2019-12-11 19:29:43 -0700 | [diff] [blame] | 889 | !(worker->cur_work->flags & IO_WQ_WORK_NO_CANCEL) && |
Pavel Begunkov | 2293b41 | 2020-03-07 01:15:39 +0300 | [diff] [blame] | 890 | match->fn(worker->cur_work, match->data)) { |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 891 | send_sig(SIGINT, worker->task, 1); |
Jens Axboe | 36c2f92 | 2019-11-13 09:43:34 -0700 | [diff] [blame] | 892 | ret = true; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 893 | } |
Jens Axboe | 36c2f92 | 2019-11-13 09:43:34 -0700 | [diff] [blame] | 894 | spin_unlock_irqrestore(&worker->lock, flags); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 895 | |
Jens Axboe | 36c2f92 | 2019-11-13 09:43:34 -0700 | [diff] [blame] | 896 | return ret; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 897 | } |
| 898 | |
| 899 | 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] | 900 | struct io_cb_cancel_data *match) |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 901 | { |
Jens Axboe | 6206f0e | 2019-11-26 11:59:32 -0700 | [diff] [blame] | 902 | struct io_wq_work_node *node, *prev; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 903 | struct io_wq_work *work; |
Jens Axboe | 6f72653 | 2019-11-05 13:51:51 -0700 | [diff] [blame] | 904 | unsigned long flags; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 905 | bool found = false; |
| 906 | |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 907 | /* |
| 908 | * First check pending list, if we're lucky we can just remove it |
| 909 | * from there. CANCEL_OK means that the work is returned as-new, |
| 910 | * no completion will be posted for it. |
| 911 | */ |
Jens Axboe | 6f72653 | 2019-11-05 13:51:51 -0700 | [diff] [blame] | 912 | spin_lock_irqsave(&wqe->lock, flags); |
Jens Axboe | 6206f0e | 2019-11-26 11:59:32 -0700 | [diff] [blame] | 913 | wq_list_for_each(node, prev, &wqe->work_list) { |
| 914 | work = container_of(node, struct io_wq_work, list); |
| 915 | |
Jens Axboe | 00bcda1 | 2020-02-08 19:13:32 -0700 | [diff] [blame] | 916 | if (match->fn(work, match->data)) { |
Jens Axboe | 6206f0e | 2019-11-26 11:59:32 -0700 | [diff] [blame] | 917 | wq_node_del(&wqe->work_list, node, prev); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 918 | found = true; |
| 919 | break; |
| 920 | } |
| 921 | } |
Jens Axboe | 6f72653 | 2019-11-05 13:51:51 -0700 | [diff] [blame] | 922 | spin_unlock_irqrestore(&wqe->lock, flags); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 923 | |
| 924 | if (found) { |
Pavel Begunkov | e9fd939 | 2020-03-04 16:14:12 +0300 | [diff] [blame] | 925 | io_run_cancel(work, wqe); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 926 | return IO_WQ_CANCEL_OK; |
| 927 | } |
| 928 | |
| 929 | /* |
| 930 | * Now check if a free (going busy) or busy worker has the work |
| 931 | * currently running. If we find it there, we'll return CANCEL_RUNNING |
Brian Gianforcaro | d195a66 | 2019-12-13 03:09:50 -0800 | [diff] [blame] | 932 | * as an indication that we attempt to signal cancellation. The |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 933 | * completion will run normally in this case. |
| 934 | */ |
| 935 | rcu_read_lock(); |
Jens Axboe | 00bcda1 | 2020-02-08 19:13:32 -0700 | [diff] [blame] | 936 | found = io_wq_for_each_worker(wqe, io_wq_worker_cancel, match); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 937 | rcu_read_unlock(); |
| 938 | return found ? IO_WQ_CANCEL_RUNNING : IO_WQ_CANCEL_NOTFOUND; |
| 939 | } |
| 940 | |
Pavel Begunkov | 2293b41 | 2020-03-07 01:15:39 +0300 | [diff] [blame] | 941 | enum io_wq_cancel io_wq_cancel_cb(struct io_wq *wq, work_cancel_fn *cancel, |
| 942 | void *data) |
| 943 | { |
| 944 | struct io_cb_cancel_data match = { |
| 945 | .fn = cancel, |
| 946 | .data = data, |
| 947 | }; |
| 948 | enum io_wq_cancel ret = IO_WQ_CANCEL_NOTFOUND; |
| 949 | int node; |
| 950 | |
| 951 | for_each_node(node) { |
| 952 | struct io_wqe *wqe = wq->wqes[node]; |
| 953 | |
| 954 | ret = io_wqe_cancel_work(wqe, &match); |
| 955 | if (ret != IO_WQ_CANCEL_NOTFOUND) |
| 956 | break; |
| 957 | } |
| 958 | |
| 959 | return ret; |
| 960 | } |
| 961 | |
| 962 | 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] | 963 | { |
| 964 | return work == data; |
| 965 | } |
| 966 | |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 967 | enum io_wq_cancel io_wq_cancel_work(struct io_wq *wq, struct io_wq_work *cwork) |
| 968 | { |
Pavel Begunkov | 2293b41 | 2020-03-07 01:15:39 +0300 | [diff] [blame] | 969 | 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] | 970 | } |
| 971 | |
Jens Axboe | 3628288 | 2020-02-08 19:16:39 -0700 | [diff] [blame] | 972 | static bool io_wq_pid_match(struct io_wq_work *work, void *data) |
| 973 | { |
| 974 | pid_t pid = (pid_t) (unsigned long) data; |
| 975 | |
Pavel Begunkov | 2293b41 | 2020-03-07 01:15:39 +0300 | [diff] [blame] | 976 | return work->task_pid == pid; |
Jens Axboe | 3628288 | 2020-02-08 19:16:39 -0700 | [diff] [blame] | 977 | } |
| 978 | |
| 979 | enum io_wq_cancel io_wq_cancel_pid(struct io_wq *wq, pid_t pid) |
| 980 | { |
Pavel Begunkov | 2293b41 | 2020-03-07 01:15:39 +0300 | [diff] [blame] | 981 | void *data = (void *) (unsigned long) pid; |
Jens Axboe | 3628288 | 2020-02-08 19:16:39 -0700 | [diff] [blame] | 982 | |
Pavel Begunkov | 2293b41 | 2020-03-07 01:15:39 +0300 | [diff] [blame] | 983 | return io_wq_cancel_cb(wq, io_wq_pid_match, data); |
Jens Axboe | 3628288 | 2020-02-08 19:16:39 -0700 | [diff] [blame] | 984 | } |
| 985 | |
Jens Axboe | 576a347 | 2019-11-25 08:49:20 -0700 | [diff] [blame] | 986 | 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] | 987 | { |
Jann Horn | 3fc50ab | 2019-11-26 19:10:20 +0100 | [diff] [blame] | 988 | int ret = -ENOMEM, node; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 989 | struct io_wq *wq; |
| 990 | |
Pavel Begunkov | e9fd939 | 2020-03-04 16:14:12 +0300 | [diff] [blame] | 991 | if (WARN_ON_ONCE(!data->free_work)) |
| 992 | return ERR_PTR(-EINVAL); |
| 993 | |
Jann Horn | ad6e005 | 2019-11-26 17:39:45 +0100 | [diff] [blame] | 994 | wq = kzalloc(sizeof(*wq), GFP_KERNEL); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 995 | if (!wq) |
| 996 | return ERR_PTR(-ENOMEM); |
| 997 | |
Jann Horn | 3fc50ab | 2019-11-26 19:10:20 +0100 | [diff] [blame] | 998 | wq->wqes = kcalloc(nr_node_ids, sizeof(struct io_wqe *), GFP_KERNEL); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 999 | if (!wq->wqes) { |
| 1000 | kfree(wq); |
| 1001 | return ERR_PTR(-ENOMEM); |
| 1002 | } |
| 1003 | |
Pavel Begunkov | e9fd939 | 2020-03-04 16:14:12 +0300 | [diff] [blame] | 1004 | wq->free_work = data->free_work; |
Jens Axboe | 7d72306 | 2019-11-12 22:31:31 -0700 | [diff] [blame] | 1005 | |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 1006 | /* caller must already hold a reference to this */ |
Jens Axboe | 576a347 | 2019-11-25 08:49:20 -0700 | [diff] [blame] | 1007 | wq->user = data->user; |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 1008 | |
Jann Horn | 3fc50ab | 2019-11-26 19:10:20 +0100 | [diff] [blame] | 1009 | for_each_node(node) { |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 1010 | struct io_wqe *wqe; |
Jens Axboe | 7563439 | 2020-02-11 06:30:06 -0700 | [diff] [blame] | 1011 | int alloc_node = node; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 1012 | |
Jens Axboe | 7563439 | 2020-02-11 06:30:06 -0700 | [diff] [blame] | 1013 | if (!node_online(alloc_node)) |
| 1014 | alloc_node = NUMA_NO_NODE; |
| 1015 | wqe = kzalloc_node(sizeof(struct io_wqe), GFP_KERNEL, alloc_node); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 1016 | if (!wqe) |
Jann Horn | 3fc50ab | 2019-11-26 19:10:20 +0100 | [diff] [blame] | 1017 | goto err; |
| 1018 | wq->wqes[node] = wqe; |
Jens Axboe | 7563439 | 2020-02-11 06:30:06 -0700 | [diff] [blame] | 1019 | wqe->node = alloc_node; |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 1020 | wqe->acct[IO_WQ_ACCT_BOUND].max_workers = bounded; |
| 1021 | atomic_set(&wqe->acct[IO_WQ_ACCT_BOUND].nr_running, 0); |
Jens Axboe | 576a347 | 2019-11-25 08:49:20 -0700 | [diff] [blame] | 1022 | if (wq->user) { |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 1023 | wqe->acct[IO_WQ_ACCT_UNBOUND].max_workers = |
| 1024 | task_rlimit(current, RLIMIT_NPROC); |
| 1025 | } |
| 1026 | atomic_set(&wqe->acct[IO_WQ_ACCT_UNBOUND].nr_running, 0); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 1027 | wqe->wq = wq; |
| 1028 | spin_lock_init(&wqe->lock); |
Jens Axboe | 6206f0e | 2019-11-26 11:59:32 -0700 | [diff] [blame] | 1029 | INIT_WQ_LIST(&wqe->work_list); |
Jens Axboe | 021d1cd | 2019-11-14 08:00:41 -0700 | [diff] [blame] | 1030 | INIT_HLIST_NULLS_HEAD(&wqe->free_list, 0); |
Jens Axboe | e61df66 | 2019-11-13 13:54:49 -0700 | [diff] [blame] | 1031 | INIT_LIST_HEAD(&wqe->all_list); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 1032 | } |
| 1033 | |
| 1034 | init_completion(&wq->done); |
| 1035 | |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 1036 | wq->manager = kthread_create(io_wq_manager, wq, "io_wq_manager"); |
| 1037 | if (!IS_ERR(wq->manager)) { |
| 1038 | wake_up_process(wq->manager); |
Jens Axboe | b60fda6 | 2019-11-19 08:37:07 -0700 | [diff] [blame] | 1039 | wait_for_completion(&wq->done); |
| 1040 | if (test_bit(IO_WQ_BIT_ERROR, &wq->state)) { |
| 1041 | ret = -ENOMEM; |
| 1042 | goto err; |
| 1043 | } |
Jens Axboe | 848f7e1 | 2020-01-23 15:33:32 -0700 | [diff] [blame] | 1044 | refcount_set(&wq->use_refs, 1); |
Jens Axboe | b60fda6 | 2019-11-19 08:37:07 -0700 | [diff] [blame] | 1045 | reinit_completion(&wq->done); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 1046 | return wq; |
| 1047 | } |
| 1048 | |
| 1049 | ret = PTR_ERR(wq->manager); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 1050 | complete(&wq->done); |
Jens Axboe | b60fda6 | 2019-11-19 08:37:07 -0700 | [diff] [blame] | 1051 | err: |
Jann Horn | 3fc50ab | 2019-11-26 19:10:20 +0100 | [diff] [blame] | 1052 | for_each_node(node) |
| 1053 | kfree(wq->wqes[node]); |
Jens Axboe | b60fda6 | 2019-11-19 08:37:07 -0700 | [diff] [blame] | 1054 | kfree(wq->wqes); |
| 1055 | kfree(wq); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 1056 | return ERR_PTR(ret); |
| 1057 | } |
| 1058 | |
Pavel Begunkov | eba6f5a | 2020-01-28 03:15:47 +0300 | [diff] [blame] | 1059 | bool io_wq_get(struct io_wq *wq, struct io_wq_data *data) |
| 1060 | { |
Pavel Begunkov | e9fd939 | 2020-03-04 16:14:12 +0300 | [diff] [blame] | 1061 | if (data->free_work != wq->free_work) |
Pavel Begunkov | eba6f5a | 2020-01-28 03:15:47 +0300 | [diff] [blame] | 1062 | return false; |
| 1063 | |
| 1064 | return refcount_inc_not_zero(&wq->use_refs); |
| 1065 | } |
| 1066 | |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 1067 | static bool io_wq_worker_wake(struct io_worker *worker, void *data) |
| 1068 | { |
| 1069 | wake_up_process(worker->task); |
| 1070 | return false; |
| 1071 | } |
| 1072 | |
Jens Axboe | 848f7e1 | 2020-01-23 15:33:32 -0700 | [diff] [blame] | 1073 | static void __io_wq_destroy(struct io_wq *wq) |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 1074 | { |
Jann Horn | 3fc50ab | 2019-11-26 19:10:20 +0100 | [diff] [blame] | 1075 | int node; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 1076 | |
Jens Axboe | b60fda6 | 2019-11-19 08:37:07 -0700 | [diff] [blame] | 1077 | set_bit(IO_WQ_BIT_EXIT, &wq->state); |
| 1078 | if (wq->manager) |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 1079 | kthread_stop(wq->manager); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 1080 | |
| 1081 | rcu_read_lock(); |
Jann Horn | 3fc50ab | 2019-11-26 19:10:20 +0100 | [diff] [blame] | 1082 | for_each_node(node) |
| 1083 | 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] | 1084 | rcu_read_unlock(); |
| 1085 | |
| 1086 | wait_for_completion(&wq->done); |
| 1087 | |
Jann Horn | 3fc50ab | 2019-11-26 19:10:20 +0100 | [diff] [blame] | 1088 | for_each_node(node) |
| 1089 | kfree(wq->wqes[node]); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 1090 | kfree(wq->wqes); |
| 1091 | kfree(wq); |
| 1092 | } |
Jens Axboe | 848f7e1 | 2020-01-23 15:33:32 -0700 | [diff] [blame] | 1093 | |
| 1094 | void io_wq_destroy(struct io_wq *wq) |
| 1095 | { |
| 1096 | if (refcount_dec_and_test(&wq->use_refs)) |
| 1097 | __io_wq_destroy(wq); |
| 1098 | } |