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