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; |
| 49 | struct task_struct *task; |
| 50 | wait_queue_head_t wait; |
| 51 | struct io_wqe *wqe; |
| 52 | struct io_wq_work *cur_work; |
| 53 | |
| 54 | struct rcu_head rcu; |
| 55 | struct mm_struct *mm; |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 56 | struct files_struct *restore_files; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 57 | }; |
| 58 | |
| 59 | struct io_wq_nulls_list { |
| 60 | struct hlist_nulls_head head; |
| 61 | unsigned long nulls; |
| 62 | }; |
| 63 | |
| 64 | #if BITS_PER_LONG == 64 |
| 65 | #define IO_WQ_HASH_ORDER 6 |
| 66 | #else |
| 67 | #define IO_WQ_HASH_ORDER 5 |
| 68 | #endif |
| 69 | |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 70 | struct io_wqe_acct { |
| 71 | unsigned nr_workers; |
| 72 | unsigned max_workers; |
| 73 | atomic_t nr_running; |
| 74 | }; |
| 75 | |
| 76 | enum { |
| 77 | IO_WQ_ACCT_BOUND, |
| 78 | IO_WQ_ACCT_UNBOUND, |
| 79 | }; |
| 80 | |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 81 | /* |
| 82 | * Per-node worker thread pool |
| 83 | */ |
| 84 | struct io_wqe { |
| 85 | struct { |
| 86 | spinlock_t lock; |
| 87 | struct list_head work_list; |
| 88 | unsigned long hash_map; |
| 89 | unsigned flags; |
| 90 | } ____cacheline_aligned_in_smp; |
| 91 | |
| 92 | int node; |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 93 | struct io_wqe_acct acct[2]; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 94 | |
| 95 | struct io_wq_nulls_list free_list; |
| 96 | struct io_wq_nulls_list busy_list; |
| 97 | |
| 98 | struct io_wq *wq; |
| 99 | }; |
| 100 | |
| 101 | /* |
| 102 | * Per io_wq state |
| 103 | */ |
| 104 | struct io_wq { |
| 105 | struct io_wqe **wqes; |
| 106 | unsigned long state; |
| 107 | unsigned nr_wqes; |
| 108 | |
Jens Axboe | 7d72306 | 2019-11-12 22:31:31 -0700 | [diff] [blame^] | 109 | get_work_fn *get_work; |
| 110 | put_work_fn *put_work; |
| 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 | struct mm_struct *mm; |
| 115 | refcount_t refs; |
| 116 | struct completion done; |
| 117 | }; |
| 118 | |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 119 | static bool io_worker_get(struct io_worker *worker) |
| 120 | { |
| 121 | return refcount_inc_not_zero(&worker->ref); |
| 122 | } |
| 123 | |
| 124 | static void io_worker_release(struct io_worker *worker) |
| 125 | { |
| 126 | if (refcount_dec_and_test(&worker->ref)) |
| 127 | wake_up_process(worker->task); |
| 128 | } |
| 129 | |
| 130 | /* |
| 131 | * Note: drops the wqe->lock if returning true! The caller must re-acquire |
| 132 | * the lock in that case. Some callers need to restart handling if this |
| 133 | * happens, so we can't just re-acquire the lock on behalf of the caller. |
| 134 | */ |
| 135 | static bool __io_worker_unuse(struct io_wqe *wqe, struct io_worker *worker) |
| 136 | { |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 137 | bool dropped_lock = false; |
| 138 | |
| 139 | if (current->files != worker->restore_files) { |
| 140 | __acquire(&wqe->lock); |
| 141 | spin_unlock_irq(&wqe->lock); |
| 142 | dropped_lock = true; |
| 143 | |
| 144 | task_lock(current); |
| 145 | current->files = worker->restore_files; |
| 146 | task_unlock(current); |
| 147 | } |
| 148 | |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 149 | /* |
| 150 | * If we have an active mm, we need to drop the wq lock before unusing |
| 151 | * it. If we do, return true and let the caller retry the idle loop. |
| 152 | */ |
| 153 | if (worker->mm) { |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 154 | if (!dropped_lock) { |
| 155 | __acquire(&wqe->lock); |
| 156 | spin_unlock_irq(&wqe->lock); |
| 157 | dropped_lock = true; |
| 158 | } |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 159 | __set_current_state(TASK_RUNNING); |
| 160 | set_fs(KERNEL_DS); |
| 161 | unuse_mm(worker->mm); |
| 162 | mmput(worker->mm); |
| 163 | worker->mm = NULL; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 164 | } |
| 165 | |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 166 | return dropped_lock; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 167 | } |
| 168 | |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 169 | static inline struct io_wqe_acct *io_work_get_acct(struct io_wqe *wqe, |
| 170 | struct io_wq_work *work) |
| 171 | { |
| 172 | if (work->flags & IO_WQ_WORK_UNBOUND) |
| 173 | return &wqe->acct[IO_WQ_ACCT_UNBOUND]; |
| 174 | |
| 175 | return &wqe->acct[IO_WQ_ACCT_BOUND]; |
| 176 | } |
| 177 | |
| 178 | static inline struct io_wqe_acct *io_wqe_get_acct(struct io_wqe *wqe, |
| 179 | struct io_worker *worker) |
| 180 | { |
| 181 | if (worker->flags & IO_WORKER_F_BOUND) |
| 182 | return &wqe->acct[IO_WQ_ACCT_BOUND]; |
| 183 | |
| 184 | return &wqe->acct[IO_WQ_ACCT_UNBOUND]; |
| 185 | } |
| 186 | |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 187 | static void io_worker_exit(struct io_worker *worker) |
| 188 | { |
| 189 | struct io_wqe *wqe = worker->wqe; |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 190 | struct io_wqe_acct *acct = io_wqe_get_acct(wqe, worker); |
| 191 | unsigned nr_workers; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 192 | |
| 193 | /* |
| 194 | * If we're not at zero, someone else is holding a brief reference |
| 195 | * to the worker. Wait for that to go away. |
| 196 | */ |
| 197 | set_current_state(TASK_INTERRUPTIBLE); |
| 198 | if (!refcount_dec_and_test(&worker->ref)) |
| 199 | schedule(); |
| 200 | __set_current_state(TASK_RUNNING); |
| 201 | |
| 202 | preempt_disable(); |
| 203 | current->flags &= ~PF_IO_WORKER; |
| 204 | if (worker->flags & IO_WORKER_F_RUNNING) |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 205 | atomic_dec(&acct->nr_running); |
| 206 | if (!(worker->flags & IO_WORKER_F_BOUND)) |
| 207 | atomic_dec(&wqe->wq->user->processes); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 208 | worker->flags = 0; |
| 209 | preempt_enable(); |
| 210 | |
| 211 | spin_lock_irq(&wqe->lock); |
| 212 | hlist_nulls_del_rcu(&worker->nulls_node); |
| 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 | |
| 247 | n = rcu_dereference(hlist_nulls_first_rcu(&wqe->free_list.head)); |
| 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); |
| 323 | hlist_nulls_add_head_rcu(&worker->nulls_node, |
| 324 | &wqe->busy_list.head); |
| 325 | } |
| 326 | worker->cur_work = work; |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 327 | |
| 328 | /* |
| 329 | * If worker is moving from bound to unbound (or vice versa), then |
| 330 | * ensure we update the running accounting. |
| 331 | */ |
| 332 | worker_bound = (worker->flags & IO_WORKER_F_BOUND) != 0; |
| 333 | work_bound = (work->flags & IO_WQ_WORK_UNBOUND) == 0; |
| 334 | if (worker_bound != work_bound) { |
| 335 | io_wqe_dec_running(wqe, worker); |
| 336 | if (work_bound) { |
| 337 | worker->flags |= IO_WORKER_F_BOUND; |
| 338 | wqe->acct[IO_WQ_ACCT_UNBOUND].nr_workers--; |
| 339 | wqe->acct[IO_WQ_ACCT_BOUND].nr_workers++; |
| 340 | atomic_dec(&wqe->wq->user->processes); |
| 341 | } else { |
| 342 | worker->flags &= ~IO_WORKER_F_BOUND; |
| 343 | wqe->acct[IO_WQ_ACCT_UNBOUND].nr_workers++; |
| 344 | wqe->acct[IO_WQ_ACCT_BOUND].nr_workers--; |
| 345 | atomic_inc(&wqe->wq->user->processes); |
| 346 | } |
| 347 | io_wqe_inc_running(wqe, worker); |
| 348 | } |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | /* |
| 352 | * No work, worker going to sleep. Move to freelist, and unuse mm if we |
| 353 | * have one attached. Dropping the mm may potentially sleep, so we drop |
| 354 | * the lock in that case and return success. Since the caller has to |
| 355 | * retry the loop in that case (we changed task state), we don't regrab |
| 356 | * the lock if we return success. |
| 357 | */ |
| 358 | static bool __io_worker_idle(struct io_wqe *wqe, struct io_worker *worker) |
| 359 | __must_hold(wqe->lock) |
| 360 | { |
| 361 | if (!(worker->flags & IO_WORKER_F_FREE)) { |
| 362 | worker->flags |= IO_WORKER_F_FREE; |
| 363 | hlist_nulls_del_init_rcu(&worker->nulls_node); |
| 364 | hlist_nulls_add_head_rcu(&worker->nulls_node, |
| 365 | &wqe->free_list.head); |
| 366 | } |
| 367 | |
| 368 | return __io_worker_unuse(wqe, worker); |
| 369 | } |
| 370 | |
| 371 | static struct io_wq_work *io_get_next_work(struct io_wqe *wqe, unsigned *hash) |
| 372 | __must_hold(wqe->lock) |
| 373 | { |
| 374 | struct io_wq_work *work; |
| 375 | |
| 376 | list_for_each_entry(work, &wqe->work_list, list) { |
| 377 | /* not hashed, can run anytime */ |
| 378 | if (!(work->flags & IO_WQ_WORK_HASHED)) { |
| 379 | list_del(&work->list); |
| 380 | return work; |
| 381 | } |
| 382 | |
| 383 | /* hashed, can run if not already running */ |
| 384 | *hash = work->flags >> IO_WQ_HASH_SHIFT; |
| 385 | if (!(wqe->hash_map & BIT_ULL(*hash))) { |
| 386 | wqe->hash_map |= BIT_ULL(*hash); |
| 387 | list_del(&work->list); |
| 388 | return work; |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | return NULL; |
| 393 | } |
| 394 | |
| 395 | static void io_worker_handle_work(struct io_worker *worker) |
| 396 | __releases(wqe->lock) |
| 397 | { |
Jens Axboe | 7d72306 | 2019-11-12 22:31:31 -0700 | [diff] [blame^] | 398 | struct io_wq_work *work, *old_work = NULL, *put_work = NULL; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 399 | struct io_wqe *wqe = worker->wqe; |
| 400 | struct io_wq *wq = wqe->wq; |
| 401 | |
| 402 | do { |
| 403 | unsigned hash = -1U; |
| 404 | |
| 405 | /* |
| 406 | * Signals are either sent to cancel specific work, or to just |
| 407 | * cancel all work items. For the former, ->cur_work must |
| 408 | * match. ->cur_work is NULL at this point, since we haven't |
| 409 | * assigned any work, so it's safe to flush signals for that |
| 410 | * case. For the latter case of cancelling all work, the caller |
| 411 | * wil have set IO_WQ_BIT_CANCEL. |
| 412 | */ |
| 413 | if (signal_pending(current)) |
| 414 | flush_signals(current); |
| 415 | |
| 416 | /* |
| 417 | * If we got some work, mark us as busy. If we didn't, but |
| 418 | * the list isn't empty, it means we stalled on hashed work. |
| 419 | * Mark us stalled so we don't keep looking for work when we |
| 420 | * can't make progress, any work completion or insertion will |
| 421 | * clear the stalled flag. |
| 422 | */ |
| 423 | work = io_get_next_work(wqe, &hash); |
| 424 | if (work) |
| 425 | __io_worker_busy(wqe, worker, work); |
| 426 | else if (!list_empty(&wqe->work_list)) |
| 427 | wqe->flags |= IO_WQE_FLAG_STALLED; |
| 428 | |
| 429 | spin_unlock_irq(&wqe->lock); |
Jens Axboe | 7d72306 | 2019-11-12 22:31:31 -0700 | [diff] [blame^] | 430 | if (put_work && wq->put_work) |
| 431 | wq->put_work(old_work); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 432 | if (!work) |
| 433 | break; |
| 434 | next: |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 435 | if ((work->flags & IO_WQ_WORK_NEEDS_FILES) && |
| 436 | current->files != work->files) { |
| 437 | task_lock(current); |
| 438 | current->files = work->files; |
| 439 | task_unlock(current); |
| 440 | } |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 441 | if ((work->flags & IO_WQ_WORK_NEEDS_USER) && !worker->mm && |
| 442 | wq->mm && mmget_not_zero(wq->mm)) { |
| 443 | use_mm(wq->mm); |
| 444 | set_fs(USER_DS); |
| 445 | worker->mm = wq->mm; |
| 446 | } |
| 447 | if (test_bit(IO_WQ_BIT_CANCEL, &wq->state)) |
| 448 | work->flags |= IO_WQ_WORK_CANCEL; |
| 449 | if (worker->mm) |
| 450 | work->flags |= IO_WQ_WORK_HAS_MM; |
| 451 | |
Jens Axboe | 7d72306 | 2019-11-12 22:31:31 -0700 | [diff] [blame^] | 452 | if (wq->get_work && !(work->flags & IO_WQ_WORK_INTERNAL)) { |
| 453 | put_work = work; |
| 454 | wq->get_work(work); |
| 455 | } |
| 456 | |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 457 | old_work = work; |
| 458 | work->func(&work); |
| 459 | |
| 460 | spin_lock_irq(&wqe->lock); |
| 461 | worker->cur_work = NULL; |
| 462 | if (hash != -1U) { |
| 463 | wqe->hash_map &= ~BIT_ULL(hash); |
| 464 | wqe->flags &= ~IO_WQE_FLAG_STALLED; |
| 465 | } |
| 466 | if (work && work != old_work) { |
| 467 | spin_unlock_irq(&wqe->lock); |
Jens Axboe | 7d72306 | 2019-11-12 22:31:31 -0700 | [diff] [blame^] | 468 | |
| 469 | if (put_work && wq->put_work) { |
| 470 | wq->put_work(put_work); |
| 471 | put_work = NULL; |
| 472 | } |
| 473 | |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 474 | /* dependent work not hashed */ |
| 475 | hash = -1U; |
| 476 | goto next; |
| 477 | } |
| 478 | } while (1); |
| 479 | } |
| 480 | |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 481 | static int io_wqe_worker(void *data) |
| 482 | { |
| 483 | struct io_worker *worker = data; |
| 484 | struct io_wqe *wqe = worker->wqe; |
| 485 | struct io_wq *wq = wqe->wq; |
| 486 | DEFINE_WAIT(wait); |
| 487 | |
| 488 | io_worker_start(wqe, worker); |
| 489 | |
| 490 | while (!test_bit(IO_WQ_BIT_EXIT, &wq->state)) { |
| 491 | prepare_to_wait(&worker->wait, &wait, TASK_INTERRUPTIBLE); |
| 492 | |
| 493 | spin_lock_irq(&wqe->lock); |
| 494 | if (io_wqe_run_queue(wqe)) { |
| 495 | __set_current_state(TASK_RUNNING); |
| 496 | io_worker_handle_work(worker); |
| 497 | continue; |
| 498 | } |
| 499 | /* drops the lock on success, retry */ |
| 500 | if (__io_worker_idle(wqe, worker)) { |
| 501 | __release(&wqe->lock); |
| 502 | continue; |
| 503 | } |
| 504 | spin_unlock_irq(&wqe->lock); |
| 505 | if (signal_pending(current)) |
| 506 | flush_signals(current); |
| 507 | if (schedule_timeout(WORKER_IDLE_TIMEOUT)) |
| 508 | continue; |
| 509 | /* timed out, exit unless we're the fixed worker */ |
| 510 | if (test_bit(IO_WQ_BIT_EXIT, &wq->state) || |
| 511 | !(worker->flags & IO_WORKER_F_FIXED)) |
| 512 | break; |
| 513 | } |
| 514 | |
| 515 | finish_wait(&worker->wait, &wait); |
| 516 | |
| 517 | if (test_bit(IO_WQ_BIT_EXIT, &wq->state)) { |
| 518 | spin_lock_irq(&wqe->lock); |
| 519 | if (!list_empty(&wqe->work_list)) |
| 520 | io_worker_handle_work(worker); |
| 521 | else |
| 522 | spin_unlock_irq(&wqe->lock); |
| 523 | } |
| 524 | |
| 525 | io_worker_exit(worker); |
| 526 | return 0; |
| 527 | } |
| 528 | |
| 529 | /* |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 530 | * Called when a worker is scheduled in. Mark us as currently running. |
| 531 | */ |
| 532 | void io_wq_worker_running(struct task_struct *tsk) |
| 533 | { |
| 534 | struct io_worker *worker = kthread_data(tsk); |
| 535 | struct io_wqe *wqe = worker->wqe; |
| 536 | |
| 537 | if (!(worker->flags & IO_WORKER_F_UP)) |
| 538 | return; |
| 539 | if (worker->flags & IO_WORKER_F_RUNNING) |
| 540 | return; |
| 541 | worker->flags |= IO_WORKER_F_RUNNING; |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 542 | io_wqe_inc_running(wqe, worker); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | /* |
| 546 | * Called when worker is going to sleep. If there are no workers currently |
| 547 | * running and we have work pending, wake up a free one or have the manager |
| 548 | * set one up. |
| 549 | */ |
| 550 | void io_wq_worker_sleeping(struct task_struct *tsk) |
| 551 | { |
| 552 | struct io_worker *worker = kthread_data(tsk); |
| 553 | struct io_wqe *wqe = worker->wqe; |
| 554 | |
| 555 | if (!(worker->flags & IO_WORKER_F_UP)) |
| 556 | return; |
| 557 | if (!(worker->flags & IO_WORKER_F_RUNNING)) |
| 558 | return; |
| 559 | |
| 560 | worker->flags &= ~IO_WORKER_F_RUNNING; |
| 561 | |
| 562 | spin_lock_irq(&wqe->lock); |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 563 | io_wqe_dec_running(wqe, worker); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 564 | spin_unlock_irq(&wqe->lock); |
| 565 | } |
| 566 | |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 567 | 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] | 568 | { |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 569 | struct io_wqe_acct *acct =&wqe->acct[index]; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 570 | struct io_worker *worker; |
| 571 | |
| 572 | worker = kcalloc_node(1, sizeof(*worker), GFP_KERNEL, wqe->node); |
| 573 | if (!worker) |
| 574 | return; |
| 575 | |
| 576 | refcount_set(&worker->ref, 1); |
| 577 | worker->nulls_node.pprev = NULL; |
| 578 | init_waitqueue_head(&worker->wait); |
| 579 | worker->wqe = wqe; |
| 580 | |
| 581 | worker->task = kthread_create_on_node(io_wqe_worker, worker, wqe->node, |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 582 | "io_wqe_worker-%d/%d", index, wqe->node); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 583 | if (IS_ERR(worker->task)) { |
| 584 | kfree(worker); |
| 585 | return; |
| 586 | } |
| 587 | |
| 588 | spin_lock_irq(&wqe->lock); |
| 589 | hlist_nulls_add_head_rcu(&worker->nulls_node, &wqe->free_list.head); |
| 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 */ |
| 613 | if (!hlist_nulls_empty(&wqe->free_list.head) || !io_wqe_run_queue(wqe)) |
| 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(); |
| 661 | free_worker = !hlist_nulls_empty(&wqe->free_list.head); |
| 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, |
| 733 | struct io_wq_nulls_list *list, |
| 734 | bool (*func)(struct io_worker *, void *), |
| 735 | void *data) |
| 736 | { |
| 737 | struct hlist_nulls_node *n; |
| 738 | struct io_worker *worker; |
| 739 | bool ret = false; |
| 740 | |
| 741 | restart: |
| 742 | hlist_nulls_for_each_entry_rcu(worker, n, &list->head, nulls_node) { |
| 743 | if (io_worker_get(worker)) { |
| 744 | ret = func(worker, data); |
| 745 | io_worker_release(worker); |
| 746 | if (ret) |
| 747 | break; |
| 748 | } |
| 749 | } |
| 750 | if (!ret && get_nulls_value(n) != list->nulls) |
| 751 | goto restart; |
| 752 | return ret; |
| 753 | } |
| 754 | |
| 755 | void io_wq_cancel_all(struct io_wq *wq) |
| 756 | { |
| 757 | int i; |
| 758 | |
| 759 | set_bit(IO_WQ_BIT_CANCEL, &wq->state); |
| 760 | |
| 761 | /* |
| 762 | * Browse both lists, as there's a gap between handing work off |
| 763 | * to a worker and the worker putting itself on the busy_list |
| 764 | */ |
| 765 | rcu_read_lock(); |
| 766 | for (i = 0; i < wq->nr_wqes; i++) { |
| 767 | struct io_wqe *wqe = wq->wqes[i]; |
| 768 | |
| 769 | io_wq_for_each_worker(wqe, &wqe->busy_list, |
| 770 | io_wqe_worker_send_sig, NULL); |
| 771 | io_wq_for_each_worker(wqe, &wqe->free_list, |
| 772 | io_wqe_worker_send_sig, NULL); |
| 773 | } |
| 774 | rcu_read_unlock(); |
| 775 | } |
| 776 | |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 777 | struct io_cb_cancel_data { |
| 778 | struct io_wqe *wqe; |
| 779 | work_cancel_fn *cancel; |
| 780 | void *caller_data; |
| 781 | }; |
| 782 | |
| 783 | static bool io_work_cancel(struct io_worker *worker, void *cancel_data) |
| 784 | { |
| 785 | struct io_cb_cancel_data *data = cancel_data; |
| 786 | struct io_wqe *wqe = data->wqe; |
Jens Axboe | 6f72653 | 2019-11-05 13:51:51 -0700 | [diff] [blame] | 787 | unsigned long flags; |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 788 | bool ret = false; |
| 789 | |
| 790 | /* |
| 791 | * Hold the lock to avoid ->cur_work going out of scope, caller |
| 792 | * may deference the passed in work. |
| 793 | */ |
Jens Axboe | 6f72653 | 2019-11-05 13:51:51 -0700 | [diff] [blame] | 794 | spin_lock_irqsave(&wqe->lock, flags); |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 795 | if (worker->cur_work && |
| 796 | data->cancel(worker->cur_work, data->caller_data)) { |
| 797 | send_sig(SIGINT, worker->task, 1); |
| 798 | ret = true; |
| 799 | } |
Jens Axboe | 6f72653 | 2019-11-05 13:51:51 -0700 | [diff] [blame] | 800 | spin_unlock_irqrestore(&wqe->lock, flags); |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 801 | |
| 802 | return ret; |
| 803 | } |
| 804 | |
| 805 | static enum io_wq_cancel io_wqe_cancel_cb_work(struct io_wqe *wqe, |
| 806 | work_cancel_fn *cancel, |
| 807 | void *cancel_data) |
| 808 | { |
| 809 | struct io_cb_cancel_data data = { |
| 810 | .wqe = wqe, |
| 811 | .cancel = cancel, |
| 812 | .caller_data = cancel_data, |
| 813 | }; |
| 814 | struct io_wq_work *work; |
Jens Axboe | 6f72653 | 2019-11-05 13:51:51 -0700 | [diff] [blame] | 815 | unsigned long flags; |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 816 | bool found = false; |
| 817 | |
Jens Axboe | 6f72653 | 2019-11-05 13:51:51 -0700 | [diff] [blame] | 818 | spin_lock_irqsave(&wqe->lock, flags); |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 819 | list_for_each_entry(work, &wqe->work_list, list) { |
| 820 | if (cancel(work, cancel_data)) { |
| 821 | list_del(&work->list); |
| 822 | found = true; |
| 823 | break; |
| 824 | } |
| 825 | } |
Jens Axboe | 6f72653 | 2019-11-05 13:51:51 -0700 | [diff] [blame] | 826 | spin_unlock_irqrestore(&wqe->lock, flags); |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 827 | |
| 828 | if (found) { |
| 829 | work->flags |= IO_WQ_WORK_CANCEL; |
| 830 | work->func(&work); |
| 831 | return IO_WQ_CANCEL_OK; |
| 832 | } |
| 833 | |
| 834 | rcu_read_lock(); |
| 835 | found = io_wq_for_each_worker(wqe, &wqe->free_list, io_work_cancel, |
| 836 | &data); |
| 837 | if (found) |
| 838 | goto done; |
| 839 | |
| 840 | found = io_wq_for_each_worker(wqe, &wqe->busy_list, io_work_cancel, |
| 841 | &data); |
| 842 | done: |
| 843 | rcu_read_unlock(); |
| 844 | return found ? IO_WQ_CANCEL_RUNNING : IO_WQ_CANCEL_NOTFOUND; |
| 845 | } |
| 846 | |
| 847 | enum io_wq_cancel io_wq_cancel_cb(struct io_wq *wq, work_cancel_fn *cancel, |
| 848 | void *data) |
| 849 | { |
| 850 | enum io_wq_cancel ret = IO_WQ_CANCEL_NOTFOUND; |
| 851 | int i; |
| 852 | |
| 853 | for (i = 0; i < wq->nr_wqes; i++) { |
| 854 | struct io_wqe *wqe = wq->wqes[i]; |
| 855 | |
| 856 | ret = io_wqe_cancel_cb_work(wqe, cancel, data); |
| 857 | if (ret != IO_WQ_CANCEL_NOTFOUND) |
| 858 | break; |
| 859 | } |
| 860 | |
| 861 | return ret; |
| 862 | } |
| 863 | |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 864 | static bool io_wq_worker_cancel(struct io_worker *worker, void *data) |
| 865 | { |
| 866 | struct io_wq_work *work = data; |
| 867 | |
| 868 | if (worker->cur_work == work) { |
| 869 | send_sig(SIGINT, worker->task, 1); |
| 870 | return true; |
| 871 | } |
| 872 | |
| 873 | return false; |
| 874 | } |
| 875 | |
| 876 | static enum io_wq_cancel io_wqe_cancel_work(struct io_wqe *wqe, |
| 877 | struct io_wq_work *cwork) |
| 878 | { |
| 879 | struct io_wq_work *work; |
Jens Axboe | 6f72653 | 2019-11-05 13:51:51 -0700 | [diff] [blame] | 880 | unsigned long flags; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 881 | bool found = false; |
| 882 | |
| 883 | cwork->flags |= IO_WQ_WORK_CANCEL; |
| 884 | |
| 885 | /* |
| 886 | * First check pending list, if we're lucky we can just remove it |
| 887 | * from there. CANCEL_OK means that the work is returned as-new, |
| 888 | * no completion will be posted for it. |
| 889 | */ |
Jens Axboe | 6f72653 | 2019-11-05 13:51:51 -0700 | [diff] [blame] | 890 | spin_lock_irqsave(&wqe->lock, flags); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 891 | list_for_each_entry(work, &wqe->work_list, list) { |
| 892 | if (work == cwork) { |
| 893 | list_del(&work->list); |
| 894 | found = true; |
| 895 | break; |
| 896 | } |
| 897 | } |
Jens Axboe | 6f72653 | 2019-11-05 13:51:51 -0700 | [diff] [blame] | 898 | spin_unlock_irqrestore(&wqe->lock, flags); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 899 | |
| 900 | if (found) { |
| 901 | work->flags |= IO_WQ_WORK_CANCEL; |
| 902 | work->func(&work); |
| 903 | return IO_WQ_CANCEL_OK; |
| 904 | } |
| 905 | |
| 906 | /* |
| 907 | * Now check if a free (going busy) or busy worker has the work |
| 908 | * currently running. If we find it there, we'll return CANCEL_RUNNING |
| 909 | * as an indication that we attempte to signal cancellation. The |
| 910 | * completion will run normally in this case. |
| 911 | */ |
| 912 | rcu_read_lock(); |
| 913 | found = io_wq_for_each_worker(wqe, &wqe->free_list, io_wq_worker_cancel, |
| 914 | cwork); |
| 915 | if (found) |
| 916 | goto done; |
| 917 | |
| 918 | found = io_wq_for_each_worker(wqe, &wqe->busy_list, io_wq_worker_cancel, |
| 919 | cwork); |
| 920 | done: |
| 921 | rcu_read_unlock(); |
| 922 | return found ? IO_WQ_CANCEL_RUNNING : IO_WQ_CANCEL_NOTFOUND; |
| 923 | } |
| 924 | |
| 925 | enum io_wq_cancel io_wq_cancel_work(struct io_wq *wq, struct io_wq_work *cwork) |
| 926 | { |
| 927 | enum io_wq_cancel ret = IO_WQ_CANCEL_NOTFOUND; |
| 928 | int i; |
| 929 | |
| 930 | for (i = 0; i < wq->nr_wqes; i++) { |
| 931 | struct io_wqe *wqe = wq->wqes[i]; |
| 932 | |
| 933 | ret = io_wqe_cancel_work(wqe, cwork); |
| 934 | if (ret != IO_WQ_CANCEL_NOTFOUND) |
| 935 | break; |
| 936 | } |
| 937 | |
| 938 | return ret; |
| 939 | } |
| 940 | |
| 941 | struct io_wq_flush_data { |
| 942 | struct io_wq_work work; |
| 943 | struct completion done; |
| 944 | }; |
| 945 | |
| 946 | static void io_wq_flush_func(struct io_wq_work **workptr) |
| 947 | { |
| 948 | struct io_wq_work *work = *workptr; |
| 949 | struct io_wq_flush_data *data; |
| 950 | |
| 951 | data = container_of(work, struct io_wq_flush_data, work); |
| 952 | complete(&data->done); |
| 953 | } |
| 954 | |
| 955 | /* |
| 956 | * Doesn't wait for previously queued work to finish. When this completes, |
| 957 | * it just means that previously queued work was started. |
| 958 | */ |
| 959 | void io_wq_flush(struct io_wq *wq) |
| 960 | { |
| 961 | struct io_wq_flush_data data; |
| 962 | int i; |
| 963 | |
| 964 | for (i = 0; i < wq->nr_wqes; i++) { |
| 965 | struct io_wqe *wqe = wq->wqes[i]; |
| 966 | |
| 967 | init_completion(&data.done); |
| 968 | INIT_IO_WORK(&data.work, io_wq_flush_func); |
Jens Axboe | 7d72306 | 2019-11-12 22:31:31 -0700 | [diff] [blame^] | 969 | data.work.flags |= IO_WQ_WORK_INTERNAL; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 970 | io_wqe_enqueue(wqe, &data.work); |
| 971 | wait_for_completion(&data.done); |
| 972 | } |
| 973 | } |
| 974 | |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 975 | struct io_wq *io_wq_create(unsigned bounded, struct mm_struct *mm, |
Jens Axboe | 7d72306 | 2019-11-12 22:31:31 -0700 | [diff] [blame^] | 976 | struct user_struct *user, get_work_fn *get_work, |
| 977 | put_work_fn *put_work) |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 978 | { |
| 979 | int ret = -ENOMEM, i, node; |
| 980 | struct io_wq *wq; |
| 981 | |
| 982 | wq = kcalloc(1, sizeof(*wq), GFP_KERNEL); |
| 983 | if (!wq) |
| 984 | return ERR_PTR(-ENOMEM); |
| 985 | |
| 986 | wq->nr_wqes = num_online_nodes(); |
| 987 | wq->wqes = kcalloc(wq->nr_wqes, sizeof(struct io_wqe *), GFP_KERNEL); |
| 988 | if (!wq->wqes) { |
| 989 | kfree(wq); |
| 990 | return ERR_PTR(-ENOMEM); |
| 991 | } |
| 992 | |
Jens Axboe | 7d72306 | 2019-11-12 22:31:31 -0700 | [diff] [blame^] | 993 | wq->get_work = get_work; |
| 994 | wq->put_work = put_work; |
| 995 | |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 996 | /* caller must already hold a reference to this */ |
| 997 | wq->user = user; |
| 998 | |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 999 | i = 0; |
| 1000 | refcount_set(&wq->refs, wq->nr_wqes); |
| 1001 | for_each_online_node(node) { |
| 1002 | struct io_wqe *wqe; |
| 1003 | |
| 1004 | wqe = kcalloc_node(1, sizeof(struct io_wqe), GFP_KERNEL, node); |
| 1005 | if (!wqe) |
| 1006 | break; |
| 1007 | wq->wqes[i] = wqe; |
| 1008 | wqe->node = node; |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 1009 | wqe->acct[IO_WQ_ACCT_BOUND].max_workers = bounded; |
| 1010 | atomic_set(&wqe->acct[IO_WQ_ACCT_BOUND].nr_running, 0); |
| 1011 | if (user) { |
| 1012 | wqe->acct[IO_WQ_ACCT_UNBOUND].max_workers = |
| 1013 | task_rlimit(current, RLIMIT_NPROC); |
| 1014 | } |
| 1015 | atomic_set(&wqe->acct[IO_WQ_ACCT_UNBOUND].nr_running, 0); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 1016 | wqe->node = node; |
| 1017 | wqe->wq = wq; |
| 1018 | spin_lock_init(&wqe->lock); |
| 1019 | INIT_LIST_HEAD(&wqe->work_list); |
| 1020 | INIT_HLIST_NULLS_HEAD(&wqe->free_list.head, 0); |
| 1021 | wqe->free_list.nulls = 0; |
| 1022 | INIT_HLIST_NULLS_HEAD(&wqe->busy_list.head, 1); |
| 1023 | wqe->busy_list.nulls = 1; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 1024 | |
| 1025 | i++; |
| 1026 | } |
| 1027 | |
| 1028 | init_completion(&wq->done); |
| 1029 | |
| 1030 | if (i != wq->nr_wqes) |
| 1031 | goto err; |
| 1032 | |
| 1033 | /* caller must have already done mmgrab() on this mm */ |
| 1034 | wq->mm = mm; |
| 1035 | |
| 1036 | wq->manager = kthread_create(io_wq_manager, wq, "io_wq_manager"); |
| 1037 | if (!IS_ERR(wq->manager)) { |
| 1038 | wake_up_process(wq->manager); |
| 1039 | return wq; |
| 1040 | } |
| 1041 | |
| 1042 | ret = PTR_ERR(wq->manager); |
| 1043 | wq->manager = NULL; |
| 1044 | err: |
| 1045 | complete(&wq->done); |
| 1046 | io_wq_destroy(wq); |
| 1047 | return ERR_PTR(ret); |
| 1048 | } |
| 1049 | |
| 1050 | static bool io_wq_worker_wake(struct io_worker *worker, void *data) |
| 1051 | { |
| 1052 | wake_up_process(worker->task); |
| 1053 | return false; |
| 1054 | } |
| 1055 | |
| 1056 | void io_wq_destroy(struct io_wq *wq) |
| 1057 | { |
| 1058 | int i; |
| 1059 | |
| 1060 | if (wq->manager) { |
| 1061 | set_bit(IO_WQ_BIT_EXIT, &wq->state); |
| 1062 | kthread_stop(wq->manager); |
| 1063 | } |
| 1064 | |
| 1065 | rcu_read_lock(); |
| 1066 | for (i = 0; i < wq->nr_wqes; i++) { |
| 1067 | struct io_wqe *wqe = wq->wqes[i]; |
| 1068 | |
| 1069 | if (!wqe) |
| 1070 | continue; |
| 1071 | io_wq_for_each_worker(wqe, &wqe->free_list, io_wq_worker_wake, |
| 1072 | NULL); |
| 1073 | io_wq_for_each_worker(wqe, &wqe->busy_list, io_wq_worker_wake, |
| 1074 | NULL); |
| 1075 | } |
| 1076 | rcu_read_unlock(); |
| 1077 | |
| 1078 | wait_for_completion(&wq->done); |
| 1079 | |
| 1080 | for (i = 0; i < wq->nr_wqes; i++) |
| 1081 | kfree(wq->wqes[i]); |
| 1082 | kfree(wq->wqes); |
| 1083 | kfree(wq); |
| 1084 | } |