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