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