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