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