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> |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 12 | #include <linux/percpu.h> |
| 13 | #include <linux/slab.h> |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 14 | #include <linux/rculist_nulls.h> |
Jens Axboe | 43c01fb | 2020-10-22 09:02:50 -0600 | [diff] [blame] | 15 | #include <linux/cpu.h> |
Jens Axboe | 3bfe610 | 2021-02-16 14:15:30 -0700 | [diff] [blame] | 16 | #include <linux/tracehook.h> |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 17 | |
| 18 | #include "io-wq.h" |
| 19 | |
| 20 | #define WORKER_IDLE_TIMEOUT (5 * HZ) |
| 21 | |
| 22 | enum { |
| 23 | IO_WORKER_F_UP = 1, /* up and active */ |
| 24 | IO_WORKER_F_RUNNING = 2, /* account as running */ |
| 25 | IO_WORKER_F_FREE = 4, /* worker on free list */ |
Jens Axboe | 05c5f4e | 2021-09-01 13:01:17 -0600 | [diff] [blame] | 26 | IO_WORKER_F_BOUND = 8, /* is doing bounded work */ |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 27 | }; |
| 28 | |
| 29 | enum { |
| 30 | IO_WQ_BIT_EXIT = 0, /* wq exiting */ |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 31 | }; |
| 32 | |
| 33 | enum { |
Jens Axboe | f95dc20 | 2021-08-31 13:57:32 -0600 | [diff] [blame] | 34 | IO_ACCT_STALLED_BIT = 0, /* stalled on hash */ |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 35 | }; |
| 36 | |
| 37 | /* |
| 38 | * One for each thread in a wqe pool |
| 39 | */ |
| 40 | struct io_worker { |
| 41 | refcount_t ref; |
| 42 | unsigned flags; |
| 43 | struct hlist_nulls_node nulls_node; |
Jens Axboe | e61df66 | 2019-11-13 13:54:49 -0700 | [diff] [blame] | 44 | struct list_head all_list; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 45 | struct task_struct *task; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 46 | struct io_wqe *wqe; |
Jens Axboe | 36c2f92 | 2019-11-13 09:43:34 -0700 | [diff] [blame] | 47 | |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 48 | struct io_wq_work *cur_work; |
Jens Axboe | 36c2f92 | 2019-11-13 09:43:34 -0700 | [diff] [blame] | 49 | spinlock_t lock; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 50 | |
Jens Axboe | eb2de94 | 2021-02-23 19:59:06 -0700 | [diff] [blame] | 51 | struct completion ref_done; |
| 52 | |
Jens Axboe | d3e9f73 | 2021-08-04 08:37:25 -0600 | [diff] [blame] | 53 | unsigned long create_state; |
| 54 | struct callback_head create_work; |
| 55 | int create_index; |
| 56 | |
Jens Axboe | 3146cba | 2021-09-01 11:20:10 -0600 | [diff] [blame] | 57 | union { |
| 58 | struct rcu_head rcu; |
| 59 | struct work_struct work; |
| 60 | }; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 61 | }; |
| 62 | |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 63 | #if BITS_PER_LONG == 64 |
| 64 | #define IO_WQ_HASH_ORDER 6 |
| 65 | #else |
| 66 | #define IO_WQ_HASH_ORDER 5 |
| 67 | #endif |
| 68 | |
Pavel Begunkov | 86f3cd1 | 2020-03-23 22:57:22 +0300 | [diff] [blame] | 69 | #define IO_WQ_NR_HASH_BUCKETS (1u << IO_WQ_HASH_ORDER) |
| 70 | |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 71 | struct io_wqe_acct { |
| 72 | unsigned nr_workers; |
| 73 | unsigned max_workers; |
Jens Axboe | 685fe7f | 2021-03-08 09:37:51 -0700 | [diff] [blame] | 74 | int index; |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 75 | atomic_t nr_running; |
Jens Axboe | f95dc20 | 2021-08-31 13:57:32 -0600 | [diff] [blame] | 76 | struct io_wq_work_list work_list; |
| 77 | unsigned long flags; |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 78 | }; |
| 79 | |
| 80 | enum { |
| 81 | IO_WQ_ACCT_BOUND, |
| 82 | IO_WQ_ACCT_UNBOUND, |
Jens Axboe | f95dc20 | 2021-08-31 13:57:32 -0600 | [diff] [blame] | 83 | IO_WQ_ACCT_NR, |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 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 { |
Jens Axboe | f95dc20 | 2021-08-31 13:57:32 -0600 | [diff] [blame] | 90 | raw_spinlock_t lock; |
| 91 | struct io_wqe_acct acct[2]; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 92 | |
| 93 | int node; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 94 | |
Jens Axboe | 021d1cd | 2019-11-14 08:00:41 -0700 | [diff] [blame] | 95 | struct hlist_nulls_head free_list; |
Jens Axboe | e61df66 | 2019-11-13 13:54:49 -0700 | [diff] [blame] | 96 | struct list_head all_list; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 97 | |
Jens Axboe | e941894 | 2021-02-19 12:33:30 -0700 | [diff] [blame] | 98 | struct wait_queue_entry wait; |
| 99 | |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 100 | struct io_wq *wq; |
Pavel Begunkov | 86f3cd1 | 2020-03-23 22:57:22 +0300 | [diff] [blame] | 101 | struct io_wq_work *hash_tail[IO_WQ_NR_HASH_BUCKETS]; |
Jens Axboe | 0e03496 | 2021-06-17 10:08:11 -0600 | [diff] [blame] | 102 | |
| 103 | cpumask_var_t cpu_mask; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 104 | }; |
| 105 | |
| 106 | /* |
| 107 | * Per io_wq state |
| 108 | */ |
| 109 | struct io_wq { |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 110 | unsigned long state; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 111 | |
Pavel Begunkov | e9fd939 | 2020-03-04 16:14:12 +0300 | [diff] [blame] | 112 | free_work_fn *free_work; |
Pavel Begunkov | f5fa38c | 2020-06-08 21:08:20 +0300 | [diff] [blame] | 113 | io_wq_work_fn *do_work; |
Jens Axboe | 7d72306 | 2019-11-12 22:31:31 -0700 | [diff] [blame] | 114 | |
Jens Axboe | e941894 | 2021-02-19 12:33:30 -0700 | [diff] [blame] | 115 | struct io_wq_hash *hash; |
| 116 | |
Jens Axboe | fb3a1f6 | 2021-02-26 09:47:20 -0700 | [diff] [blame] | 117 | atomic_t worker_refs; |
| 118 | struct completion worker_done; |
| 119 | |
Jens Axboe | 43c01fb | 2020-10-22 09:02:50 -0600 | [diff] [blame] | 120 | struct hlist_node cpuhp_node; |
Jens Axboe | 3bfe610 | 2021-02-16 14:15:30 -0700 | [diff] [blame] | 121 | |
Jens Axboe | 685fe7f | 2021-03-08 09:37:51 -0700 | [diff] [blame] | 122 | struct task_struct *task; |
Pavel Begunkov | c7f405d | 2021-06-14 02:36:12 +0100 | [diff] [blame] | 123 | |
| 124 | struct io_wqe *wqes[]; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 125 | }; |
| 126 | |
Jens Axboe | 43c01fb | 2020-10-22 09:02:50 -0600 | [diff] [blame] | 127 | static enum cpuhp_state io_wq_online; |
| 128 | |
Jens Axboe | f012725 | 2021-03-03 15:47:04 -0700 | [diff] [blame] | 129 | struct io_cb_cancel_data { |
| 130 | work_cancel_fn *fn; |
| 131 | void *data; |
| 132 | int nr_running; |
| 133 | int nr_pending; |
| 134 | bool cancel_all; |
| 135 | }; |
| 136 | |
Jens Axboe | 3146cba | 2021-09-01 11:20:10 -0600 | [diff] [blame] | 137 | static bool create_io_worker(struct io_wq *wq, struct io_wqe *wqe, int index); |
Jens Axboe | 83d6c39 | 2021-08-03 09:14:35 -0600 | [diff] [blame] | 138 | static void io_wqe_dec_running(struct io_worker *worker); |
Jens Axboe | 3146cba | 2021-09-01 11:20:10 -0600 | [diff] [blame] | 139 | static bool io_acct_cancel_pending_work(struct io_wqe *wqe, |
| 140 | struct io_wqe_acct *acct, |
| 141 | struct io_cb_cancel_data *match); |
Jens Axboe | f012725 | 2021-03-03 15:47:04 -0700 | [diff] [blame] | 142 | |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 143 | static bool io_worker_get(struct io_worker *worker) |
| 144 | { |
| 145 | return refcount_inc_not_zero(&worker->ref); |
| 146 | } |
| 147 | |
| 148 | static void io_worker_release(struct io_worker *worker) |
| 149 | { |
| 150 | if (refcount_dec_and_test(&worker->ref)) |
Jens Axboe | eb2de94 | 2021-02-23 19:59:06 -0700 | [diff] [blame] | 151 | complete(&worker->ref_done); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 152 | } |
| 153 | |
Pavel Begunkov | 8418f22 | 2021-03-22 01:58:28 +0000 | [diff] [blame] | 154 | static inline struct io_wqe_acct *io_get_acct(struct io_wqe *wqe, bool bound) |
| 155 | { |
| 156 | return &wqe->acct[bound ? IO_WQ_ACCT_BOUND : IO_WQ_ACCT_UNBOUND]; |
| 157 | } |
| 158 | |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 159 | static inline struct io_wqe_acct *io_work_get_acct(struct io_wqe *wqe, |
| 160 | struct io_wq_work *work) |
| 161 | { |
Pavel Begunkov | 8418f22 | 2021-03-22 01:58:28 +0000 | [diff] [blame] | 162 | return io_get_acct(wqe, !(work->flags & IO_WQ_WORK_UNBOUND)); |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 163 | } |
| 164 | |
Jens Axboe | 958234d | 2021-02-17 09:00:57 -0700 | [diff] [blame] | 165 | static inline struct io_wqe_acct *io_wqe_get_acct(struct io_worker *worker) |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 166 | { |
Pavel Begunkov | 8418f22 | 2021-03-22 01:58:28 +0000 | [diff] [blame] | 167 | return io_get_acct(worker->wqe, worker->flags & IO_WORKER_F_BOUND); |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 168 | } |
| 169 | |
Jens Axboe | 685fe7f | 2021-03-08 09:37:51 -0700 | [diff] [blame] | 170 | static void io_worker_ref_put(struct io_wq *wq) |
| 171 | { |
| 172 | if (atomic_dec_and_test(&wq->worker_refs)) |
| 173 | complete(&wq->worker_done); |
| 174 | } |
| 175 | |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 176 | static void io_worker_exit(struct io_worker *worker) |
| 177 | { |
| 178 | struct io_wqe *wqe = worker->wqe; |
Jens Axboe | 958234d | 2021-02-17 09:00:57 -0700 | [diff] [blame] | 179 | struct io_wqe_acct *acct = io_wqe_get_acct(worker); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 180 | |
Jens Axboe | eb2de94 | 2021-02-23 19:59:06 -0700 | [diff] [blame] | 181 | if (refcount_dec_and_test(&worker->ref)) |
| 182 | complete(&worker->ref_done); |
| 183 | wait_for_completion(&worker->ref_done); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 184 | |
Jens Axboe | a9a4aa9 | 2021-08-30 06:33:08 -0600 | [diff] [blame] | 185 | raw_spin_lock(&wqe->lock); |
Jens Axboe | 83d6c39 | 2021-08-03 09:14:35 -0600 | [diff] [blame] | 186 | if (worker->flags & IO_WORKER_F_FREE) |
Jens Axboe | bf1daa4 | 2021-02-16 18:00:55 -0700 | [diff] [blame] | 187 | hlist_nulls_del_rcu(&worker->nulls_node); |
Jens Axboe | e61df66 | 2019-11-13 13:54:49 -0700 | [diff] [blame] | 188 | list_del_rcu(&worker->all_list); |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 189 | acct->nr_workers--; |
Jens Axboe | 83d6c39 | 2021-08-03 09:14:35 -0600 | [diff] [blame] | 190 | preempt_disable(); |
| 191 | io_wqe_dec_running(worker); |
| 192 | worker->flags = 0; |
| 193 | current->flags &= ~PF_IO_WORKER; |
| 194 | preempt_enable(); |
Jens Axboe | a9a4aa9 | 2021-08-30 06:33:08 -0600 | [diff] [blame] | 195 | raw_spin_unlock(&wqe->lock); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 196 | |
YueHaibing | 364b05f | 2019-11-02 15:55:01 +0800 | [diff] [blame] | 197 | kfree_rcu(worker, rcu); |
Jens Axboe | 685fe7f | 2021-03-08 09:37:51 -0700 | [diff] [blame] | 198 | io_worker_ref_put(wqe->wq); |
Jens Axboe | 46fe18b | 2021-03-04 12:39:36 -0700 | [diff] [blame] | 199 | do_exit(0); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 200 | } |
| 201 | |
Jens Axboe | f95dc20 | 2021-08-31 13:57:32 -0600 | [diff] [blame] | 202 | static inline bool io_acct_run_queue(struct io_wqe_acct *acct) |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 203 | { |
Jens Axboe | f95dc20 | 2021-08-31 13:57:32 -0600 | [diff] [blame] | 204 | if (!wq_list_empty(&acct->work_list) && |
| 205 | !test_bit(IO_ACCT_STALLED_BIT, &acct->flags)) |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 206 | return true; |
| 207 | return false; |
| 208 | } |
| 209 | |
| 210 | /* |
| 211 | * Check head of free list for an available worker. If one isn't available, |
Jens Axboe | 685fe7f | 2021-03-08 09:37:51 -0700 | [diff] [blame] | 212 | * caller must create one. |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 213 | */ |
Jens Axboe | f95dc20 | 2021-08-31 13:57:32 -0600 | [diff] [blame] | 214 | static bool io_wqe_activate_free_worker(struct io_wqe *wqe, |
| 215 | struct io_wqe_acct *acct) |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 216 | __must_hold(RCU) |
| 217 | { |
| 218 | struct hlist_nulls_node *n; |
| 219 | struct io_worker *worker; |
| 220 | |
Jens Axboe | 83d6c39 | 2021-08-03 09:14:35 -0600 | [diff] [blame] | 221 | /* |
| 222 | * Iterate free_list and see if we can find an idle worker to |
| 223 | * activate. If a given worker is on the free_list but in the process |
| 224 | * of exiting, keep trying. |
| 225 | */ |
| 226 | hlist_nulls_for_each_entry_rcu(worker, n, &wqe->free_list, nulls_node) { |
| 227 | if (!io_worker_get(worker)) |
| 228 | continue; |
Jens Axboe | f95dc20 | 2021-08-31 13:57:32 -0600 | [diff] [blame] | 229 | if (io_wqe_get_acct(worker) != acct) { |
| 230 | io_worker_release(worker); |
| 231 | continue; |
| 232 | } |
Jens Axboe | 83d6c39 | 2021-08-03 09:14:35 -0600 | [diff] [blame] | 233 | if (wake_up_process(worker->task)) { |
| 234 | io_worker_release(worker); |
| 235 | return true; |
| 236 | } |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 237 | io_worker_release(worker); |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | return false; |
| 241 | } |
| 242 | |
| 243 | /* |
| 244 | * We need a worker. If we find a free one, we're good. If not, and we're |
Jens Axboe | 685fe7f | 2021-03-08 09:37:51 -0700 | [diff] [blame] | 245 | * below the max number of workers, create one. |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 246 | */ |
Jens Axboe | 3146cba | 2021-09-01 11:20:10 -0600 | [diff] [blame] | 247 | static bool io_wqe_create_worker(struct io_wqe *wqe, struct io_wqe_acct *acct) |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 248 | { |
Jens Axboe | 05c5f4e | 2021-09-01 13:01:17 -0600 | [diff] [blame] | 249 | bool do_create = false; |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 250 | |
| 251 | /* |
| 252 | * Most likely an attempt to queue unbounded work on an io_wq that |
| 253 | * wasn't setup with any unbounded workers. |
| 254 | */ |
Pavel Begunkov | e6ab899 | 2021-06-17 18:13:59 +0100 | [diff] [blame] | 255 | if (unlikely(!acct->max_workers)) |
| 256 | pr_warn_once("io-wq is not configured for unbound workers"); |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 257 | |
Jens Axboe | 94ffb0a | 2021-08-30 11:55:22 -0600 | [diff] [blame] | 258 | raw_spin_lock(&wqe->lock); |
| 259 | if (acct->nr_workers < acct->max_workers) { |
Jens Axboe | 94ffb0a | 2021-08-30 11:55:22 -0600 | [diff] [blame] | 260 | acct->nr_workers++; |
| 261 | do_create = true; |
| 262 | } |
| 263 | raw_spin_unlock(&wqe->lock); |
| 264 | if (do_create) { |
| 265 | atomic_inc(&acct->nr_running); |
| 266 | atomic_inc(&wqe->wq->worker_refs); |
Jens Axboe | 3146cba | 2021-09-01 11:20:10 -0600 | [diff] [blame] | 267 | return create_io_worker(wqe->wq, wqe, acct->index); |
Jens Axboe | 685fe7f | 2021-03-08 09:37:51 -0700 | [diff] [blame] | 268 | } |
Jens Axboe | 3146cba | 2021-09-01 11:20:10 -0600 | [diff] [blame] | 269 | |
| 270 | return true; |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 271 | } |
| 272 | |
Jens Axboe | 958234d | 2021-02-17 09:00:57 -0700 | [diff] [blame] | 273 | static void io_wqe_inc_running(struct io_worker *worker) |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 274 | { |
Jens Axboe | 958234d | 2021-02-17 09:00:57 -0700 | [diff] [blame] | 275 | struct io_wqe_acct *acct = io_wqe_get_acct(worker); |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 276 | |
| 277 | atomic_inc(&acct->nr_running); |
| 278 | } |
| 279 | |
Jens Axboe | 685fe7f | 2021-03-08 09:37:51 -0700 | [diff] [blame] | 280 | static void create_worker_cb(struct callback_head *cb) |
| 281 | { |
Jens Axboe | d3e9f73 | 2021-08-04 08:37:25 -0600 | [diff] [blame] | 282 | struct io_worker *worker; |
Jens Axboe | 685fe7f | 2021-03-08 09:37:51 -0700 | [diff] [blame] | 283 | struct io_wq *wq; |
Hao Xu | 2169827 | 2021-08-05 18:05:38 +0800 | [diff] [blame] | 284 | struct io_wqe *wqe; |
| 285 | struct io_wqe_acct *acct; |
Jens Axboe | 05c5f4e | 2021-09-01 13:01:17 -0600 | [diff] [blame] | 286 | bool do_create = false; |
Jens Axboe | 685fe7f | 2021-03-08 09:37:51 -0700 | [diff] [blame] | 287 | |
Jens Axboe | d3e9f73 | 2021-08-04 08:37:25 -0600 | [diff] [blame] | 288 | worker = container_of(cb, struct io_worker, create_work); |
| 289 | wqe = worker->wqe; |
Hao Xu | 2169827 | 2021-08-05 18:05:38 +0800 | [diff] [blame] | 290 | wq = wqe->wq; |
Jens Axboe | d3e9f73 | 2021-08-04 08:37:25 -0600 | [diff] [blame] | 291 | acct = &wqe->acct[worker->create_index]; |
Jens Axboe | a9a4aa9 | 2021-08-30 06:33:08 -0600 | [diff] [blame] | 292 | raw_spin_lock(&wqe->lock); |
Hao Xu | 49e7f0c | 2021-08-08 21:54:33 +0800 | [diff] [blame] | 293 | if (acct->nr_workers < acct->max_workers) { |
Hao Xu | 2169827 | 2021-08-05 18:05:38 +0800 | [diff] [blame] | 294 | acct->nr_workers++; |
Hao Xu | 49e7f0c | 2021-08-08 21:54:33 +0800 | [diff] [blame] | 295 | do_create = true; |
| 296 | } |
Jens Axboe | a9a4aa9 | 2021-08-30 06:33:08 -0600 | [diff] [blame] | 297 | raw_spin_unlock(&wqe->lock); |
Hao Xu | 49e7f0c | 2021-08-08 21:54:33 +0800 | [diff] [blame] | 298 | if (do_create) { |
Jens Axboe | 05c5f4e | 2021-09-01 13:01:17 -0600 | [diff] [blame] | 299 | create_io_worker(wq, wqe, worker->create_index); |
Hao Xu | 49e7f0c | 2021-08-08 21:54:33 +0800 | [diff] [blame] | 300 | } else { |
| 301 | atomic_dec(&acct->nr_running); |
| 302 | io_worker_ref_put(wq); |
| 303 | } |
Jens Axboe | d3e9f73 | 2021-08-04 08:37:25 -0600 | [diff] [blame] | 304 | clear_bit_unlock(0, &worker->create_state); |
| 305 | io_worker_release(worker); |
Jens Axboe | 685fe7f | 2021-03-08 09:37:51 -0700 | [diff] [blame] | 306 | } |
| 307 | |
Jens Axboe | 3146cba | 2021-09-01 11:20:10 -0600 | [diff] [blame] | 308 | static bool io_queue_worker_create(struct io_worker *worker, |
| 309 | struct io_wqe_acct *acct, |
| 310 | task_work_func_t func) |
Jens Axboe | 685fe7f | 2021-03-08 09:37:51 -0700 | [diff] [blame] | 311 | { |
Jens Axboe | 3146cba | 2021-09-01 11:20:10 -0600 | [diff] [blame] | 312 | struct io_wqe *wqe = worker->wqe; |
Jens Axboe | 685fe7f | 2021-03-08 09:37:51 -0700 | [diff] [blame] | 313 | struct io_wq *wq = wqe->wq; |
| 314 | |
| 315 | /* raced with exit, just ignore create call */ |
| 316 | if (test_bit(IO_WQ_BIT_EXIT, &wq->state)) |
| 317 | goto fail; |
Jens Axboe | d3e9f73 | 2021-08-04 08:37:25 -0600 | [diff] [blame] | 318 | if (!io_worker_get(worker)) |
| 319 | goto fail; |
| 320 | /* |
| 321 | * create_state manages ownership of create_work/index. We should |
| 322 | * only need one entry per worker, as the worker going to sleep |
| 323 | * will trigger the condition, and waking will clear it once it |
| 324 | * runs the task_work. |
| 325 | */ |
| 326 | if (test_bit(0, &worker->create_state) || |
| 327 | test_and_set_bit_lock(0, &worker->create_state)) |
| 328 | goto fail_release; |
Jens Axboe | 685fe7f | 2021-03-08 09:37:51 -0700 | [diff] [blame] | 329 | |
Jens Axboe | 3146cba | 2021-09-01 11:20:10 -0600 | [diff] [blame] | 330 | init_task_work(&worker->create_work, func); |
Jens Axboe | d3e9f73 | 2021-08-04 08:37:25 -0600 | [diff] [blame] | 331 | worker->create_index = acct->index; |
| 332 | if (!task_work_add(wq->task, &worker->create_work, TWA_SIGNAL)) |
Jens Axboe | 3146cba | 2021-09-01 11:20:10 -0600 | [diff] [blame] | 333 | return true; |
Jens Axboe | d3e9f73 | 2021-08-04 08:37:25 -0600 | [diff] [blame] | 334 | clear_bit_unlock(0, &worker->create_state); |
| 335 | fail_release: |
| 336 | io_worker_release(worker); |
Jens Axboe | 685fe7f | 2021-03-08 09:37:51 -0700 | [diff] [blame] | 337 | fail: |
| 338 | atomic_dec(&acct->nr_running); |
| 339 | io_worker_ref_put(wq); |
Jens Axboe | 3146cba | 2021-09-01 11:20:10 -0600 | [diff] [blame] | 340 | return false; |
Jens Axboe | 685fe7f | 2021-03-08 09:37:51 -0700 | [diff] [blame] | 341 | } |
| 342 | |
Jens Axboe | 958234d | 2021-02-17 09:00:57 -0700 | [diff] [blame] | 343 | static void io_wqe_dec_running(struct io_worker *worker) |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 344 | __must_hold(wqe->lock) |
| 345 | { |
Jens Axboe | 958234d | 2021-02-17 09:00:57 -0700 | [diff] [blame] | 346 | struct io_wqe_acct *acct = io_wqe_get_acct(worker); |
| 347 | struct io_wqe *wqe = worker->wqe; |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 348 | |
Jens Axboe | 685fe7f | 2021-03-08 09:37:51 -0700 | [diff] [blame] | 349 | if (!(worker->flags & IO_WORKER_F_UP)) |
| 350 | return; |
| 351 | |
Jens Axboe | f95dc20 | 2021-08-31 13:57:32 -0600 | [diff] [blame] | 352 | if (atomic_dec_and_test(&acct->nr_running) && io_acct_run_queue(acct)) { |
Jens Axboe | 685fe7f | 2021-03-08 09:37:51 -0700 | [diff] [blame] | 353 | atomic_inc(&acct->nr_running); |
| 354 | atomic_inc(&wqe->wq->worker_refs); |
Jens Axboe | 3146cba | 2021-09-01 11:20:10 -0600 | [diff] [blame] | 355 | io_queue_worker_create(worker, acct, create_worker_cb); |
Jens Axboe | 685fe7f | 2021-03-08 09:37:51 -0700 | [diff] [blame] | 356 | } |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 357 | } |
| 358 | |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 359 | /* |
| 360 | * Worker will start processing some work. Move it to the busy list, if |
| 361 | * it's currently on the freelist |
| 362 | */ |
| 363 | static void __io_worker_busy(struct io_wqe *wqe, struct io_worker *worker, |
| 364 | struct io_wq_work *work) |
| 365 | __must_hold(wqe->lock) |
| 366 | { |
| 367 | if (worker->flags & IO_WORKER_F_FREE) { |
| 368 | worker->flags &= ~IO_WORKER_F_FREE; |
| 369 | hlist_nulls_del_init_rcu(&worker->nulls_node); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 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 | */ |
Jens Axboe | c6d77d9 | 2021-02-15 13:26:34 -0700 | [diff] [blame] | 380 | static void __io_worker_idle(struct io_wqe *wqe, struct io_worker *worker) |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 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 | } |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 387 | } |
| 388 | |
Pavel Begunkov | 60cf46a | 2020-03-14 00:31:05 +0300 | [diff] [blame] | 389 | static inline unsigned int io_get_work_hash(struct io_wq_work *work) |
| 390 | { |
| 391 | return work->flags >> IO_WQ_HASH_SHIFT; |
| 392 | } |
| 393 | |
Jens Axboe | e941894 | 2021-02-19 12:33:30 -0700 | [diff] [blame] | 394 | static void io_wait_on_hash(struct io_wqe *wqe, unsigned int hash) |
| 395 | { |
| 396 | struct io_wq *wq = wqe->wq; |
| 397 | |
Jens Axboe | 08bdbd3 | 2021-08-31 06:57:25 -0600 | [diff] [blame] | 398 | spin_lock_irq(&wq->hash->wait.lock); |
Jens Axboe | e941894 | 2021-02-19 12:33:30 -0700 | [diff] [blame] | 399 | if (list_empty(&wqe->wait.entry)) { |
| 400 | __add_wait_queue(&wq->hash->wait, &wqe->wait); |
| 401 | if (!test_bit(hash, &wq->hash->map)) { |
| 402 | __set_current_state(TASK_RUNNING); |
| 403 | list_del_init(&wqe->wait.entry); |
| 404 | } |
| 405 | } |
Jens Axboe | 08bdbd3 | 2021-08-31 06:57:25 -0600 | [diff] [blame] | 406 | spin_unlock_irq(&wq->hash->wait.lock); |
Jens Axboe | e941894 | 2021-02-19 12:33:30 -0700 | [diff] [blame] | 407 | } |
| 408 | |
Jens Axboe | f95dc20 | 2021-08-31 13:57:32 -0600 | [diff] [blame] | 409 | static struct io_wq_work *io_get_next_work(struct io_wqe_acct *acct, |
Jens Axboe | 0242f64 | 2021-08-31 13:53:00 -0600 | [diff] [blame] | 410 | struct io_worker *worker) |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 411 | __must_hold(wqe->lock) |
| 412 | { |
Jens Axboe | 6206f0e | 2019-11-26 11:59:32 -0700 | [diff] [blame] | 413 | struct io_wq_work_node *node, *prev; |
Pavel Begunkov | 86f3cd1 | 2020-03-23 22:57:22 +0300 | [diff] [blame] | 414 | struct io_wq_work *work, *tail; |
Jens Axboe | e941894 | 2021-02-19 12:33:30 -0700 | [diff] [blame] | 415 | unsigned int stall_hash = -1U; |
Jens Axboe | f95dc20 | 2021-08-31 13:57:32 -0600 | [diff] [blame] | 416 | struct io_wqe *wqe = worker->wqe; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 417 | |
Jens Axboe | f95dc20 | 2021-08-31 13:57:32 -0600 | [diff] [blame] | 418 | wq_list_for_each(node, prev, &acct->work_list) { |
Jens Axboe | e941894 | 2021-02-19 12:33:30 -0700 | [diff] [blame] | 419 | unsigned int hash; |
| 420 | |
Jens Axboe | 6206f0e | 2019-11-26 11:59:32 -0700 | [diff] [blame] | 421 | work = container_of(node, struct io_wq_work, list); |
| 422 | |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 423 | /* not hashed, can run anytime */ |
Pavel Begunkov | 8766dd5 | 2020-03-14 00:31:04 +0300 | [diff] [blame] | 424 | if (!io_wq_is_hashed(work)) { |
Jens Axboe | f95dc20 | 2021-08-31 13:57:32 -0600 | [diff] [blame] | 425 | wq_list_del(&acct->work_list, node, prev); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 426 | return work; |
| 427 | } |
| 428 | |
Pavel Begunkov | 60cf46a | 2020-03-14 00:31:05 +0300 | [diff] [blame] | 429 | hash = io_get_work_hash(work); |
Jens Axboe | e941894 | 2021-02-19 12:33:30 -0700 | [diff] [blame] | 430 | /* all items with this hash lie in [work, tail] */ |
| 431 | tail = wqe->hash_tail[hash]; |
| 432 | |
| 433 | /* hashed, can run if not already running */ |
| 434 | if (!test_and_set_bit(hash, &wqe->wq->hash->map)) { |
Pavel Begunkov | 86f3cd1 | 2020-03-23 22:57:22 +0300 | [diff] [blame] | 435 | wqe->hash_tail[hash] = NULL; |
Jens Axboe | f95dc20 | 2021-08-31 13:57:32 -0600 | [diff] [blame] | 436 | wq_list_cut(&acct->work_list, &tail->list, prev); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 437 | return work; |
| 438 | } |
Jens Axboe | e941894 | 2021-02-19 12:33:30 -0700 | [diff] [blame] | 439 | if (stall_hash == -1U) |
| 440 | stall_hash = hash; |
| 441 | /* fast forward to a next hash, for-each will fix up @prev */ |
| 442 | node = &tail->list; |
| 443 | } |
| 444 | |
| 445 | if (stall_hash != -1U) { |
Jens Axboe | 0242f64 | 2021-08-31 13:53:00 -0600 | [diff] [blame] | 446 | /* |
| 447 | * Set this before dropping the lock to avoid racing with new |
| 448 | * work being added and clearing the stalled bit. |
| 449 | */ |
Jens Axboe | f95dc20 | 2021-08-31 13:57:32 -0600 | [diff] [blame] | 450 | set_bit(IO_ACCT_STALLED_BIT, &acct->flags); |
Jens Axboe | e941894 | 2021-02-19 12:33:30 -0700 | [diff] [blame] | 451 | raw_spin_unlock(&wqe->lock); |
| 452 | io_wait_on_hash(wqe, stall_hash); |
| 453 | raw_spin_lock(&wqe->lock); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | return NULL; |
| 457 | } |
| 458 | |
Jens Axboe | 00ddff4 | 2021-03-21 07:06:56 -0600 | [diff] [blame] | 459 | static bool io_flush_signals(void) |
Jens Axboe | cccf0ee | 2020-01-27 16:34:48 -0700 | [diff] [blame] | 460 | { |
Jens Axboe | 0b8cfa9 | 2021-03-21 14:16:08 -0600 | [diff] [blame] | 461 | if (unlikely(test_thread_flag(TIF_NOTIFY_SIGNAL))) { |
Jens Axboe | 00ddff4 | 2021-03-21 07:06:56 -0600 | [diff] [blame] | 462 | __set_current_state(TASK_RUNNING); |
Jens Axboe | 0b8cfa9 | 2021-03-21 14:16:08 -0600 | [diff] [blame] | 463 | tracehook_notify_signal(); |
Jens Axboe | 00ddff4 | 2021-03-21 07:06:56 -0600 | [diff] [blame] | 464 | return true; |
Jens Axboe | cccf0ee | 2020-01-27 16:34:48 -0700 | [diff] [blame] | 465 | } |
Jens Axboe | 00ddff4 | 2021-03-21 07:06:56 -0600 | [diff] [blame] | 466 | return false; |
Pavel Begunkov | dc026a7 | 2020-03-04 16:14:09 +0300 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | static void io_assign_current_work(struct io_worker *worker, |
| 470 | struct io_wq_work *work) |
| 471 | { |
Pavel Begunkov | d78298e | 2020-03-14 00:31:03 +0300 | [diff] [blame] | 472 | if (work) { |
Jens Axboe | 3bfe610 | 2021-02-16 14:15:30 -0700 | [diff] [blame] | 473 | io_flush_signals(); |
Pavel Begunkov | d78298e | 2020-03-14 00:31:03 +0300 | [diff] [blame] | 474 | cond_resched(); |
| 475 | } |
Pavel Begunkov | dc026a7 | 2020-03-04 16:14:09 +0300 | [diff] [blame] | 476 | |
Jens Axboe | a9a4aa9 | 2021-08-30 06:33:08 -0600 | [diff] [blame] | 477 | spin_lock(&worker->lock); |
Pavel Begunkov | dc026a7 | 2020-03-04 16:14:09 +0300 | [diff] [blame] | 478 | worker->cur_work = work; |
Jens Axboe | a9a4aa9 | 2021-08-30 06:33:08 -0600 | [diff] [blame] | 479 | spin_unlock(&worker->lock); |
Pavel Begunkov | dc026a7 | 2020-03-04 16:14:09 +0300 | [diff] [blame] | 480 | } |
| 481 | |
Pavel Begunkov | 60cf46a | 2020-03-14 00:31:05 +0300 | [diff] [blame] | 482 | static void io_wqe_enqueue(struct io_wqe *wqe, struct io_wq_work *work); |
| 483 | |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 484 | static void io_worker_handle_work(struct io_worker *worker) |
| 485 | __releases(wqe->lock) |
| 486 | { |
Jens Axboe | f95dc20 | 2021-08-31 13:57:32 -0600 | [diff] [blame] | 487 | struct io_wqe_acct *acct = io_wqe_get_acct(worker); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 488 | struct io_wqe *wqe = worker->wqe; |
| 489 | struct io_wq *wq = wqe->wq; |
Pavel Begunkov | c60eb04 | 2021-04-08 01:54:42 +0100 | [diff] [blame] | 490 | bool do_kill = test_bit(IO_WQ_BIT_EXIT, &wq->state); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 491 | |
| 492 | do { |
Pavel Begunkov | 86f3cd1 | 2020-03-23 22:57:22 +0300 | [diff] [blame] | 493 | struct io_wq_work *work; |
Pavel Begunkov | f462fd3 | 2020-03-04 16:14:11 +0300 | [diff] [blame] | 494 | get_next: |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 495 | /* |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 496 | * If we got some work, mark us as busy. If we didn't, but |
| 497 | * the list isn't empty, it means we stalled on hashed work. |
| 498 | * Mark us stalled so we don't keep looking for work when we |
| 499 | * can't make progress, any work completion or insertion will |
| 500 | * clear the stalled flag. |
| 501 | */ |
Jens Axboe | f95dc20 | 2021-08-31 13:57:32 -0600 | [diff] [blame] | 502 | work = io_get_next_work(acct, worker); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 503 | if (work) |
| 504 | __io_worker_busy(wqe, worker, work); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 505 | |
Jens Axboe | a9a4aa9 | 2021-08-30 06:33:08 -0600 | [diff] [blame] | 506 | raw_spin_unlock(&wqe->lock); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 507 | if (!work) |
| 508 | break; |
Pavel Begunkov | 58e3931 | 2020-03-04 16:14:10 +0300 | [diff] [blame] | 509 | io_assign_current_work(worker, work); |
Jens Axboe | e941894 | 2021-02-19 12:33:30 -0700 | [diff] [blame] | 510 | __set_current_state(TASK_RUNNING); |
Jens Axboe | 36c2f92 | 2019-11-13 09:43:34 -0700 | [diff] [blame] | 511 | |
Pavel Begunkov | dc026a7 | 2020-03-04 16:14:09 +0300 | [diff] [blame] | 512 | /* handle a whole dependent link */ |
| 513 | do { |
Pavel Begunkov | 5280f7e | 2021-02-04 13:52:08 +0000 | [diff] [blame] | 514 | struct io_wq_work *next_hashed, *linked; |
Pavel Begunkov | b089ed39 | 2020-07-25 14:42:00 +0300 | [diff] [blame] | 515 | unsigned int hash = io_get_work_hash(work); |
Hillf Danton | fd1c4bc | 2019-12-24 09:14:29 -0700 | [diff] [blame] | 516 | |
Pavel Begunkov | 86f3cd1 | 2020-03-23 22:57:22 +0300 | [diff] [blame] | 517 | next_hashed = wq_next_work(work); |
Pavel Begunkov | c60eb04 | 2021-04-08 01:54:42 +0100 | [diff] [blame] | 518 | |
| 519 | if (unlikely(do_kill) && (work->flags & IO_WQ_WORK_UNBOUND)) |
| 520 | work->flags |= IO_WQ_WORK_CANCEL; |
Pavel Begunkov | 5280f7e | 2021-02-04 13:52:08 +0000 | [diff] [blame] | 521 | wq->do_work(work); |
| 522 | io_assign_current_work(worker, NULL); |
Jens Axboe | 36c2f92 | 2019-11-13 09:43:34 -0700 | [diff] [blame] | 523 | |
Pavel Begunkov | 5280f7e | 2021-02-04 13:52:08 +0000 | [diff] [blame] | 524 | linked = wq->free_work(work); |
Pavel Begunkov | 86f3cd1 | 2020-03-23 22:57:22 +0300 | [diff] [blame] | 525 | work = next_hashed; |
| 526 | if (!work && linked && !io_wq_is_hashed(linked)) { |
| 527 | work = linked; |
| 528 | linked = NULL; |
| 529 | } |
| 530 | io_assign_current_work(worker, work); |
Pavel Begunkov | 86f3cd1 | 2020-03-23 22:57:22 +0300 | [diff] [blame] | 531 | if (linked) |
| 532 | io_wqe_enqueue(wqe, linked); |
| 533 | |
| 534 | if (hash != -1U && !next_hashed) { |
Jens Axboe | e941894 | 2021-02-19 12:33:30 -0700 | [diff] [blame] | 535 | clear_bit(hash, &wq->hash->map); |
Jens Axboe | f95dc20 | 2021-08-31 13:57:32 -0600 | [diff] [blame] | 536 | clear_bit(IO_ACCT_STALLED_BIT, &acct->flags); |
Jens Axboe | e941894 | 2021-02-19 12:33:30 -0700 | [diff] [blame] | 537 | if (wq_has_sleeper(&wq->hash->wait)) |
| 538 | wake_up(&wq->hash->wait); |
Jens Axboe | a9a4aa9 | 2021-08-30 06:33:08 -0600 | [diff] [blame] | 539 | raw_spin_lock(&wqe->lock); |
Pavel Begunkov | f462fd3 | 2020-03-04 16:14:11 +0300 | [diff] [blame] | 540 | /* skip unnecessary unlock-lock wqe->lock */ |
| 541 | if (!work) |
| 542 | goto get_next; |
Jens Axboe | a9a4aa9 | 2021-08-30 06:33:08 -0600 | [diff] [blame] | 543 | raw_spin_unlock(&wqe->lock); |
Pavel Begunkov | dc026a7 | 2020-03-04 16:14:09 +0300 | [diff] [blame] | 544 | } |
Pavel Begunkov | 58e3931 | 2020-03-04 16:14:10 +0300 | [diff] [blame] | 545 | } while (work); |
Jens Axboe | 36c2f92 | 2019-11-13 09:43:34 -0700 | [diff] [blame] | 546 | |
Jens Axboe | a9a4aa9 | 2021-08-30 06:33:08 -0600 | [diff] [blame] | 547 | raw_spin_lock(&wqe->lock); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 548 | } while (1); |
| 549 | } |
| 550 | |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 551 | static int io_wqe_worker(void *data) |
| 552 | { |
| 553 | struct io_worker *worker = data; |
Jens Axboe | f95dc20 | 2021-08-31 13:57:32 -0600 | [diff] [blame] | 554 | struct io_wqe_acct *acct = io_wqe_get_acct(worker); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 555 | struct io_wqe *wqe = worker->wqe; |
| 556 | struct io_wq *wq = wqe->wq; |
Jens Axboe | 05c5f4e | 2021-09-01 13:01:17 -0600 | [diff] [blame] | 557 | bool last_timeout = false; |
Jens Axboe | 46fe18b | 2021-03-04 12:39:36 -0700 | [diff] [blame] | 558 | char buf[TASK_COMM_LEN]; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 559 | |
Jens Axboe | 46fe18b | 2021-03-04 12:39:36 -0700 | [diff] [blame] | 560 | worker->flags |= (IO_WORKER_F_UP | IO_WORKER_F_RUNNING); |
Jens Axboe | 46fe18b | 2021-03-04 12:39:36 -0700 | [diff] [blame] | 561 | |
Jens Axboe | 685fe7f | 2021-03-08 09:37:51 -0700 | [diff] [blame] | 562 | snprintf(buf, sizeof(buf), "iou-wrk-%d", wq->task->pid); |
Jens Axboe | 46fe18b | 2021-03-04 12:39:36 -0700 | [diff] [blame] | 563 | set_task_comm(current, buf); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 564 | |
| 565 | while (!test_bit(IO_WQ_BIT_EXIT, &wq->state)) { |
Jens Axboe | 16efa4f | 2021-03-12 20:26:13 -0700 | [diff] [blame] | 566 | long ret; |
| 567 | |
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 | a9a4aa9 | 2021-08-30 06:33:08 -0600 | [diff] [blame] | 570 | raw_spin_lock(&wqe->lock); |
Jens Axboe | f95dc20 | 2021-08-31 13:57:32 -0600 | [diff] [blame] | 571 | if (io_acct_run_queue(acct)) { |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 572 | io_worker_handle_work(worker); |
Jens Axboe | e995d51 | 2019-12-07 21:06:46 -0700 | [diff] [blame] | 573 | goto loop; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 574 | } |
Jens Axboe | 05c5f4e | 2021-09-01 13:01:17 -0600 | [diff] [blame] | 575 | /* timed out, exit unless we're the last worker */ |
| 576 | if (last_timeout && acct->nr_workers > 1) { |
| 577 | raw_spin_unlock(&wqe->lock); |
| 578 | __set_current_state(TASK_RUNNING); |
| 579 | break; |
| 580 | } |
| 581 | last_timeout = false; |
Jens Axboe | c6d77d9 | 2021-02-15 13:26:34 -0700 | [diff] [blame] | 582 | __io_worker_idle(wqe, worker); |
Jens Axboe | a9a4aa9 | 2021-08-30 06:33:08 -0600 | [diff] [blame] | 583 | raw_spin_unlock(&wqe->lock); |
Jens Axboe | 00ddff4 | 2021-03-21 07:06:56 -0600 | [diff] [blame] | 584 | if (io_flush_signals()) |
| 585 | continue; |
Jens Axboe | 16efa4f | 2021-03-12 20:26:13 -0700 | [diff] [blame] | 586 | ret = schedule_timeout(WORKER_IDLE_TIMEOUT); |
Jens Axboe | dbe1bdb | 2021-03-25 18:16:06 -0600 | [diff] [blame] | 587 | if (signal_pending(current)) { |
| 588 | struct ksignal ksig; |
| 589 | |
| 590 | if (!get_signal(&ksig)) |
| 591 | continue; |
Jens Axboe | 15e20db | 2021-09-01 11:18:41 -0600 | [diff] [blame] | 592 | if (fatal_signal_pending(current)) |
| 593 | break; |
| 594 | continue; |
Jens Axboe | dbe1bdb | 2021-03-25 18:16:06 -0600 | [diff] [blame] | 595 | } |
Jens Axboe | 05c5f4e | 2021-09-01 13:01:17 -0600 | [diff] [blame] | 596 | last_timeout = !ret; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 597 | } |
| 598 | |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 599 | if (test_bit(IO_WQ_BIT_EXIT, &wq->state)) { |
Jens Axboe | a9a4aa9 | 2021-08-30 06:33:08 -0600 | [diff] [blame] | 600 | raw_spin_lock(&wqe->lock); |
Pavel Begunkov | e587227 | 2021-06-14 02:36:17 +0100 | [diff] [blame] | 601 | io_worker_handle_work(worker); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 602 | } |
| 603 | |
| 604 | io_worker_exit(worker); |
| 605 | return 0; |
| 606 | } |
| 607 | |
| 608 | /* |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 609 | * Called when a worker is scheduled in. Mark us as currently running. |
| 610 | */ |
| 611 | void io_wq_worker_running(struct task_struct *tsk) |
| 612 | { |
Jens Axboe | 3bfe610 | 2021-02-16 14:15:30 -0700 | [diff] [blame] | 613 | struct io_worker *worker = tsk->pf_io_worker; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 614 | |
Jens Axboe | 3bfe610 | 2021-02-16 14:15:30 -0700 | [diff] [blame] | 615 | if (!worker) |
| 616 | return; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 617 | if (!(worker->flags & IO_WORKER_F_UP)) |
| 618 | return; |
| 619 | if (worker->flags & IO_WORKER_F_RUNNING) |
| 620 | return; |
| 621 | worker->flags |= IO_WORKER_F_RUNNING; |
Jens Axboe | 958234d | 2021-02-17 09:00:57 -0700 | [diff] [blame] | 622 | io_wqe_inc_running(worker); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 623 | } |
| 624 | |
| 625 | /* |
| 626 | * Called when worker is going to sleep. If there are no workers currently |
Jens Axboe | 685fe7f | 2021-03-08 09:37:51 -0700 | [diff] [blame] | 627 | * running and we have work pending, wake up a free one or create a new one. |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 628 | */ |
| 629 | void io_wq_worker_sleeping(struct task_struct *tsk) |
| 630 | { |
Jens Axboe | 3bfe610 | 2021-02-16 14:15:30 -0700 | [diff] [blame] | 631 | struct io_worker *worker = tsk->pf_io_worker; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 632 | |
Jens Axboe | 3bfe610 | 2021-02-16 14:15:30 -0700 | [diff] [blame] | 633 | if (!worker) |
| 634 | return; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 635 | if (!(worker->flags & IO_WORKER_F_UP)) |
| 636 | return; |
| 637 | if (!(worker->flags & IO_WORKER_F_RUNNING)) |
| 638 | return; |
| 639 | |
| 640 | worker->flags &= ~IO_WORKER_F_RUNNING; |
| 641 | |
Jens Axboe | a9a4aa9 | 2021-08-30 06:33:08 -0600 | [diff] [blame] | 642 | raw_spin_lock(&worker->wqe->lock); |
Jens Axboe | 958234d | 2021-02-17 09:00:57 -0700 | [diff] [blame] | 643 | io_wqe_dec_running(worker); |
Jens Axboe | a9a4aa9 | 2021-08-30 06:33:08 -0600 | [diff] [blame] | 644 | raw_spin_unlock(&worker->wqe->lock); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 645 | } |
| 646 | |
Jens Axboe | 3146cba | 2021-09-01 11:20:10 -0600 | [diff] [blame] | 647 | static void io_init_new_worker(struct io_wqe *wqe, struct io_worker *worker, |
| 648 | struct task_struct *tsk) |
Jens Axboe | 3bfe610 | 2021-02-16 14:15:30 -0700 | [diff] [blame] | 649 | { |
Jens Axboe | 46fe18b | 2021-03-04 12:39:36 -0700 | [diff] [blame] | 650 | tsk->pf_io_worker = worker; |
| 651 | worker->task = tsk; |
Jens Axboe | 0e03496 | 2021-06-17 10:08:11 -0600 | [diff] [blame] | 652 | set_cpus_allowed_ptr(tsk, wqe->cpu_mask); |
Jens Axboe | e22bc9b | 2021-03-09 19:49:02 -0700 | [diff] [blame] | 653 | tsk->flags |= PF_NO_SETAFFINITY; |
Jens Axboe | 46fe18b | 2021-03-04 12:39:36 -0700 | [diff] [blame] | 654 | |
Jens Axboe | a9a4aa9 | 2021-08-30 06:33:08 -0600 | [diff] [blame] | 655 | raw_spin_lock(&wqe->lock); |
Jens Axboe | 46fe18b | 2021-03-04 12:39:36 -0700 | [diff] [blame] | 656 | hlist_nulls_add_head_rcu(&worker->nulls_node, &wqe->free_list); |
| 657 | list_add_tail_rcu(&worker->all_list, &wqe->all_list); |
| 658 | worker->flags |= IO_WORKER_F_FREE; |
Jens Axboe | a9a4aa9 | 2021-08-30 06:33:08 -0600 | [diff] [blame] | 659 | raw_spin_unlock(&wqe->lock); |
Jens Axboe | 46fe18b | 2021-03-04 12:39:36 -0700 | [diff] [blame] | 660 | wake_up_new_task(tsk); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 661 | } |
| 662 | |
Jens Axboe | 3146cba | 2021-09-01 11:20:10 -0600 | [diff] [blame] | 663 | static bool io_wq_work_match_all(struct io_wq_work *work, void *data) |
| 664 | { |
| 665 | return true; |
| 666 | } |
| 667 | |
| 668 | static inline bool io_should_retry_thread(long err) |
| 669 | { |
| 670 | switch (err) { |
| 671 | case -EAGAIN: |
| 672 | case -ERESTARTSYS: |
| 673 | case -ERESTARTNOINTR: |
| 674 | case -ERESTARTNOHAND: |
| 675 | return true; |
| 676 | default: |
| 677 | return false; |
| 678 | } |
| 679 | } |
| 680 | |
| 681 | static void create_worker_cont(struct callback_head *cb) |
| 682 | { |
| 683 | struct io_worker *worker; |
| 684 | struct task_struct *tsk; |
| 685 | struct io_wqe *wqe; |
| 686 | |
| 687 | worker = container_of(cb, struct io_worker, create_work); |
| 688 | clear_bit_unlock(0, &worker->create_state); |
| 689 | wqe = worker->wqe; |
| 690 | tsk = create_io_thread(io_wqe_worker, worker, wqe->node); |
| 691 | if (!IS_ERR(tsk)) { |
| 692 | io_init_new_worker(wqe, worker, tsk); |
| 693 | io_worker_release(worker); |
| 694 | return; |
| 695 | } else if (!io_should_retry_thread(PTR_ERR(tsk))) { |
| 696 | struct io_wqe_acct *acct = io_wqe_get_acct(worker); |
| 697 | |
| 698 | atomic_dec(&acct->nr_running); |
| 699 | raw_spin_lock(&wqe->lock); |
| 700 | acct->nr_workers--; |
| 701 | if (!acct->nr_workers) { |
| 702 | struct io_cb_cancel_data match = { |
| 703 | .fn = io_wq_work_match_all, |
| 704 | .cancel_all = true, |
| 705 | }; |
| 706 | |
| 707 | while (io_acct_cancel_pending_work(wqe, acct, &match)) |
| 708 | raw_spin_lock(&wqe->lock); |
| 709 | } |
| 710 | raw_spin_unlock(&wqe->lock); |
| 711 | io_worker_ref_put(wqe->wq); |
Qiang.zhang | 66e70be | 2021-09-09 19:58:22 +0800 | [diff] [blame^] | 712 | kfree(worker); |
Jens Axboe | 3146cba | 2021-09-01 11:20:10 -0600 | [diff] [blame] | 713 | return; |
| 714 | } |
| 715 | |
| 716 | /* re-create attempts grab a new worker ref, drop the existing one */ |
| 717 | io_worker_release(worker); |
| 718 | schedule_work(&worker->work); |
| 719 | } |
| 720 | |
| 721 | static void io_workqueue_create(struct work_struct *work) |
| 722 | { |
| 723 | struct io_worker *worker = container_of(work, struct io_worker, work); |
| 724 | struct io_wqe_acct *acct = io_wqe_get_acct(worker); |
| 725 | |
| 726 | if (!io_queue_worker_create(worker, acct, create_worker_cont)) { |
| 727 | clear_bit_unlock(0, &worker->create_state); |
| 728 | io_worker_release(worker); |
Qiang.zhang | 66e70be | 2021-09-09 19:58:22 +0800 | [diff] [blame^] | 729 | kfree(worker); |
Jens Axboe | 3146cba | 2021-09-01 11:20:10 -0600 | [diff] [blame] | 730 | } |
| 731 | } |
| 732 | |
| 733 | static bool create_io_worker(struct io_wq *wq, struct io_wqe *wqe, int index) |
| 734 | { |
| 735 | struct io_wqe_acct *acct = &wqe->acct[index]; |
| 736 | struct io_worker *worker; |
| 737 | struct task_struct *tsk; |
| 738 | |
| 739 | __set_current_state(TASK_RUNNING); |
| 740 | |
| 741 | worker = kzalloc_node(sizeof(*worker), GFP_KERNEL, wqe->node); |
| 742 | if (!worker) { |
| 743 | fail: |
| 744 | atomic_dec(&acct->nr_running); |
| 745 | raw_spin_lock(&wqe->lock); |
| 746 | acct->nr_workers--; |
| 747 | raw_spin_unlock(&wqe->lock); |
| 748 | io_worker_ref_put(wq); |
| 749 | return false; |
| 750 | } |
| 751 | |
| 752 | refcount_set(&worker->ref, 1); |
| 753 | worker->wqe = wqe; |
| 754 | spin_lock_init(&worker->lock); |
| 755 | init_completion(&worker->ref_done); |
| 756 | |
| 757 | if (index == IO_WQ_ACCT_BOUND) |
| 758 | worker->flags |= IO_WORKER_F_BOUND; |
| 759 | |
| 760 | tsk = create_io_thread(io_wqe_worker, worker, wqe->node); |
| 761 | if (!IS_ERR(tsk)) { |
| 762 | io_init_new_worker(wqe, worker, tsk); |
| 763 | } else if (!io_should_retry_thread(PTR_ERR(tsk))) { |
Qiang.zhang | 66e70be | 2021-09-09 19:58:22 +0800 | [diff] [blame^] | 764 | kfree(worker); |
Jens Axboe | 3146cba | 2021-09-01 11:20:10 -0600 | [diff] [blame] | 765 | goto fail; |
| 766 | } else { |
| 767 | INIT_WORK(&worker->work, io_workqueue_create); |
| 768 | schedule_work(&worker->work); |
| 769 | } |
| 770 | |
| 771 | return true; |
| 772 | } |
| 773 | |
Hillf Danton | c4068bf | 2020-09-26 21:26:55 +0800 | [diff] [blame] | 774 | /* |
| 775 | * Iterate the passed in list and call the specific function for each |
| 776 | * worker that isn't exiting |
| 777 | */ |
| 778 | static bool io_wq_for_each_worker(struct io_wqe *wqe, |
| 779 | bool (*func)(struct io_worker *, void *), |
| 780 | void *data) |
| 781 | { |
| 782 | struct io_worker *worker; |
| 783 | bool ret = false; |
| 784 | |
| 785 | list_for_each_entry_rcu(worker, &wqe->all_list, all_list) { |
| 786 | if (io_worker_get(worker)) { |
| 787 | /* no task if node is/was offline */ |
| 788 | if (worker->task) |
| 789 | ret = func(worker, data); |
| 790 | io_worker_release(worker); |
| 791 | if (ret) |
| 792 | break; |
| 793 | } |
| 794 | } |
| 795 | |
| 796 | return ret; |
| 797 | } |
| 798 | |
| 799 | static bool io_wq_worker_wake(struct io_worker *worker, void *data) |
| 800 | { |
Jens Axboe | 46fe18b | 2021-03-04 12:39:36 -0700 | [diff] [blame] | 801 | set_notify_signal(worker->task); |
Hillf Danton | c4068bf | 2020-09-26 21:26:55 +0800 | [diff] [blame] | 802 | wake_up_process(worker->task); |
| 803 | return false; |
| 804 | } |
| 805 | |
Pavel Begunkov | e9fd939 | 2020-03-04 16:14:12 +0300 | [diff] [blame] | 806 | 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] | 807 | { |
Pavel Begunkov | e9fd939 | 2020-03-04 16:14:12 +0300 | [diff] [blame] | 808 | struct io_wq *wq = wqe->wq; |
| 809 | |
Pavel Begunkov | fc04c39 | 2020-03-01 19:18:19 +0300 | [diff] [blame] | 810 | do { |
Pavel Begunkov | fc04c39 | 2020-03-01 19:18:19 +0300 | [diff] [blame] | 811 | work->flags |= IO_WQ_WORK_CANCEL; |
Pavel Begunkov | 5280f7e | 2021-02-04 13:52:08 +0000 | [diff] [blame] | 812 | wq->do_work(work); |
| 813 | work = wq->free_work(work); |
Pavel Begunkov | fc04c39 | 2020-03-01 19:18:19 +0300 | [diff] [blame] | 814 | } while (work); |
| 815 | } |
| 816 | |
Pavel Begunkov | 86f3cd1 | 2020-03-23 22:57:22 +0300 | [diff] [blame] | 817 | static void io_wqe_insert_work(struct io_wqe *wqe, struct io_wq_work *work) |
| 818 | { |
Jens Axboe | f95dc20 | 2021-08-31 13:57:32 -0600 | [diff] [blame] | 819 | struct io_wqe_acct *acct = io_work_get_acct(wqe, work); |
Pavel Begunkov | 86f3cd1 | 2020-03-23 22:57:22 +0300 | [diff] [blame] | 820 | unsigned int hash; |
| 821 | struct io_wq_work *tail; |
| 822 | |
| 823 | if (!io_wq_is_hashed(work)) { |
| 824 | append: |
Jens Axboe | f95dc20 | 2021-08-31 13:57:32 -0600 | [diff] [blame] | 825 | wq_list_add_tail(&work->list, &acct->work_list); |
Pavel Begunkov | 86f3cd1 | 2020-03-23 22:57:22 +0300 | [diff] [blame] | 826 | return; |
| 827 | } |
| 828 | |
| 829 | hash = io_get_work_hash(work); |
| 830 | tail = wqe->hash_tail[hash]; |
| 831 | wqe->hash_tail[hash] = work; |
| 832 | if (!tail) |
| 833 | goto append; |
| 834 | |
Jens Axboe | f95dc20 | 2021-08-31 13:57:32 -0600 | [diff] [blame] | 835 | wq_list_add_after(&work->list, &tail->list, &acct->work_list); |
Pavel Begunkov | 86f3cd1 | 2020-03-23 22:57:22 +0300 | [diff] [blame] | 836 | } |
| 837 | |
Pavel Begunkov | 713b982 | 2021-09-08 10:09:29 +0100 | [diff] [blame] | 838 | static bool io_wq_work_match_item(struct io_wq_work *work, void *data) |
| 839 | { |
| 840 | return work == data; |
| 841 | } |
| 842 | |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 843 | static void io_wqe_enqueue(struct io_wqe *wqe, struct io_wq_work *work) |
| 844 | { |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 845 | struct io_wqe_acct *acct = io_work_get_acct(wqe, work); |
Jens Axboe | 94ffb0a | 2021-08-30 11:55:22 -0600 | [diff] [blame] | 846 | unsigned work_flags = work->flags; |
| 847 | bool do_create; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 848 | |
Jens Axboe | 991468d | 2021-07-23 11:53:54 -0600 | [diff] [blame] | 849 | /* |
| 850 | * If io-wq is exiting for this task, or if the request has explicitly |
| 851 | * been marked as one that should not get executed, cancel it here. |
| 852 | */ |
| 853 | if (test_bit(IO_WQ_BIT_EXIT, &wqe->wq->state) || |
| 854 | (work->flags & IO_WQ_WORK_CANCEL)) { |
yangerkun | 70e3512 | 2021-03-09 11:04:10 +0800 | [diff] [blame] | 855 | io_run_cancel(work, wqe); |
Jens Axboe | 4fb6ac3 | 2021-02-25 10:17:09 -0700 | [diff] [blame] | 856 | return; |
| 857 | } |
| 858 | |
Jens Axboe | a9a4aa9 | 2021-08-30 06:33:08 -0600 | [diff] [blame] | 859 | raw_spin_lock(&wqe->lock); |
Pavel Begunkov | 86f3cd1 | 2020-03-23 22:57:22 +0300 | [diff] [blame] | 860 | io_wqe_insert_work(wqe, work); |
Jens Axboe | f95dc20 | 2021-08-31 13:57:32 -0600 | [diff] [blame] | 861 | clear_bit(IO_ACCT_STALLED_BIT, &acct->flags); |
Jens Axboe | 94ffb0a | 2021-08-30 11:55:22 -0600 | [diff] [blame] | 862 | |
| 863 | rcu_read_lock(); |
Jens Axboe | f95dc20 | 2021-08-31 13:57:32 -0600 | [diff] [blame] | 864 | do_create = !io_wqe_activate_free_worker(wqe, acct); |
Jens Axboe | 94ffb0a | 2021-08-30 11:55:22 -0600 | [diff] [blame] | 865 | rcu_read_unlock(); |
| 866 | |
Jens Axboe | a9a4aa9 | 2021-08-30 06:33:08 -0600 | [diff] [blame] | 867 | raw_spin_unlock(&wqe->lock); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 868 | |
Jens Axboe | 94ffb0a | 2021-08-30 11:55:22 -0600 | [diff] [blame] | 869 | if (do_create && ((work_flags & IO_WQ_WORK_CONCURRENT) || |
Jens Axboe | 3146cba | 2021-09-01 11:20:10 -0600 | [diff] [blame] | 870 | !atomic_read(&acct->nr_running))) { |
| 871 | bool did_create; |
| 872 | |
| 873 | did_create = io_wqe_create_worker(wqe, acct); |
Pavel Begunkov | 713b982 | 2021-09-08 10:09:29 +0100 | [diff] [blame] | 874 | if (likely(did_create)) |
| 875 | return; |
| 876 | |
| 877 | raw_spin_lock(&wqe->lock); |
| 878 | /* fatal condition, failed to create the first worker */ |
| 879 | if (!acct->nr_workers) { |
| 880 | struct io_cb_cancel_data match = { |
| 881 | .fn = io_wq_work_match_item, |
| 882 | .data = work, |
| 883 | .cancel_all = false, |
| 884 | }; |
| 885 | |
| 886 | if (io_acct_cancel_pending_work(wqe, acct, &match)) |
| 887 | raw_spin_lock(&wqe->lock); |
Jens Axboe | 3146cba | 2021-09-01 11:20:10 -0600 | [diff] [blame] | 888 | } |
Pavel Begunkov | 713b982 | 2021-09-08 10:09:29 +0100 | [diff] [blame] | 889 | raw_spin_unlock(&wqe->lock); |
Jens Axboe | 3146cba | 2021-09-01 11:20:10 -0600 | [diff] [blame] | 890 | } |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 891 | } |
| 892 | |
| 893 | void io_wq_enqueue(struct io_wq *wq, struct io_wq_work *work) |
| 894 | { |
| 895 | struct io_wqe *wqe = wq->wqes[numa_node_id()]; |
| 896 | |
| 897 | io_wqe_enqueue(wqe, work); |
| 898 | } |
| 899 | |
| 900 | /* |
Pavel Begunkov | 8766dd5 | 2020-03-14 00:31:04 +0300 | [diff] [blame] | 901 | * Work items that hash to the same value will not be done in parallel. |
| 902 | * Used to limit concurrent writes, generally hashed by inode. |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 903 | */ |
Pavel Begunkov | 8766dd5 | 2020-03-14 00:31:04 +0300 | [diff] [blame] | 904 | void io_wq_hash_work(struct io_wq_work *work, void *val) |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 905 | { |
Pavel Begunkov | 8766dd5 | 2020-03-14 00:31:04 +0300 | [diff] [blame] | 906 | unsigned int bit; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 907 | |
| 908 | bit = hash_ptr(val, IO_WQ_HASH_ORDER); |
| 909 | work->flags |= (IO_WQ_WORK_HASHED | (bit << IO_WQ_HASH_SHIFT)); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 910 | } |
| 911 | |
Pavel Begunkov | 2293b41 | 2020-03-07 01:15:39 +0300 | [diff] [blame] | 912 | static bool io_wq_worker_cancel(struct io_worker *worker, void *data) |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 913 | { |
Pavel Begunkov | 2293b41 | 2020-03-07 01:15:39 +0300 | [diff] [blame] | 914 | struct io_cb_cancel_data *match = data; |
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 | a9a4aa9 | 2021-08-30 06:33:08 -0600 | [diff] [blame] | 920 | spin_lock(&worker->lock); |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 921 | if (worker->cur_work && |
Pavel Begunkov | 2293b41 | 2020-03-07 01:15:39 +0300 | [diff] [blame] | 922 | match->fn(worker->cur_work, match->data)) { |
Jens Axboe | 3bfe610 | 2021-02-16 14:15:30 -0700 | [diff] [blame] | 923 | set_notify_signal(worker->task); |
Pavel Begunkov | 4f26bda | 2020-06-15 10:24:03 +0300 | [diff] [blame] | 924 | match->nr_running++; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 925 | } |
Jens Axboe | a9a4aa9 | 2021-08-30 06:33:08 -0600 | [diff] [blame] | 926 | spin_unlock(&worker->lock); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 927 | |
Pavel Begunkov | 4f26bda | 2020-06-15 10:24:03 +0300 | [diff] [blame] | 928 | return match->nr_running && !match->cancel_all; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 929 | } |
| 930 | |
Pavel Begunkov | 204361a | 2020-08-23 20:33:10 +0300 | [diff] [blame] | 931 | static inline void io_wqe_remove_pending(struct io_wqe *wqe, |
| 932 | struct io_wq_work *work, |
| 933 | struct io_wq_work_node *prev) |
| 934 | { |
Jens Axboe | f95dc20 | 2021-08-31 13:57:32 -0600 | [diff] [blame] | 935 | struct io_wqe_acct *acct = io_work_get_acct(wqe, work); |
Pavel Begunkov | 204361a | 2020-08-23 20:33:10 +0300 | [diff] [blame] | 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 | } |
Jens Axboe | f95dc20 | 2021-08-31 13:57:32 -0600 | [diff] [blame] | 947 | wq_list_del(&acct->work_list, &work->list, prev); |
Pavel Begunkov | 204361a | 2020-08-23 20:33:10 +0300 | [diff] [blame] | 948 | } |
| 949 | |
Jens Axboe | 3146cba | 2021-09-01 11:20:10 -0600 | [diff] [blame] | 950 | static bool io_acct_cancel_pending_work(struct io_wqe *wqe, |
| 951 | struct io_wqe_acct *acct, |
| 952 | struct io_cb_cancel_data *match) |
| 953 | __releases(wqe->lock) |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 954 | { |
Jens Axboe | 6206f0e | 2019-11-26 11:59:32 -0700 | [diff] [blame] | 955 | struct io_wq_work_node *node, *prev; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 956 | struct io_wq_work *work; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 957 | |
Jens Axboe | 3146cba | 2021-09-01 11:20:10 -0600 | [diff] [blame] | 958 | wq_list_for_each(node, prev, &acct->work_list) { |
| 959 | work = container_of(node, struct io_wq_work, list); |
| 960 | if (!match->fn(work, match->data)) |
| 961 | continue; |
| 962 | io_wqe_remove_pending(wqe, work, prev); |
| 963 | raw_spin_unlock(&wqe->lock); |
| 964 | io_run_cancel(work, wqe); |
| 965 | match->nr_pending++; |
| 966 | /* not safe to continue after unlock */ |
| 967 | return true; |
| 968 | } |
| 969 | |
| 970 | return false; |
| 971 | } |
| 972 | |
| 973 | static void io_wqe_cancel_pending_work(struct io_wqe *wqe, |
| 974 | struct io_cb_cancel_data *match) |
| 975 | { |
| 976 | int i; |
Pavel Begunkov | 4f26bda | 2020-06-15 10:24:03 +0300 | [diff] [blame] | 977 | retry: |
Jens Axboe | a9a4aa9 | 2021-08-30 06:33:08 -0600 | [diff] [blame] | 978 | raw_spin_lock(&wqe->lock); |
Jens Axboe | f95dc20 | 2021-08-31 13:57:32 -0600 | [diff] [blame] | 979 | for (i = 0; i < IO_WQ_ACCT_NR; i++) { |
| 980 | struct io_wqe_acct *acct = io_get_acct(wqe, i == 0); |
Pavel Begunkov | 4f26bda | 2020-06-15 10:24:03 +0300 | [diff] [blame] | 981 | |
Jens Axboe | 3146cba | 2021-09-01 11:20:10 -0600 | [diff] [blame] | 982 | if (io_acct_cancel_pending_work(wqe, acct, match)) { |
| 983 | if (match->cancel_all) |
| 984 | goto retry; |
| 985 | return; |
Jens Axboe | f95dc20 | 2021-08-31 13:57:32 -0600 | [diff] [blame] | 986 | } |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 987 | } |
Jens Axboe | a9a4aa9 | 2021-08-30 06:33:08 -0600 | [diff] [blame] | 988 | raw_spin_unlock(&wqe->lock); |
Pavel Begunkov | f4c2665 | 2020-06-15 10:24:02 +0300 | [diff] [blame] | 989 | } |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 990 | |
Pavel Begunkov | 4f26bda | 2020-06-15 10:24:03 +0300 | [diff] [blame] | 991 | static void io_wqe_cancel_running_work(struct io_wqe *wqe, |
Pavel Begunkov | f4c2665 | 2020-06-15 10:24:02 +0300 | [diff] [blame] | 992 | struct io_cb_cancel_data *match) |
| 993 | { |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 994 | rcu_read_lock(); |
Pavel Begunkov | 4f26bda | 2020-06-15 10:24:03 +0300 | [diff] [blame] | 995 | io_wq_for_each_worker(wqe, io_wq_worker_cancel, match); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 996 | rcu_read_unlock(); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 997 | } |
| 998 | |
Pavel Begunkov | 2293b41 | 2020-03-07 01:15:39 +0300 | [diff] [blame] | 999 | 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] | 1000 | void *data, bool cancel_all) |
Pavel Begunkov | 2293b41 | 2020-03-07 01:15:39 +0300 | [diff] [blame] | 1001 | { |
| 1002 | struct io_cb_cancel_data match = { |
Pavel Begunkov | 4f26bda | 2020-06-15 10:24:03 +0300 | [diff] [blame] | 1003 | .fn = cancel, |
| 1004 | .data = data, |
| 1005 | .cancel_all = cancel_all, |
Pavel Begunkov | 2293b41 | 2020-03-07 01:15:39 +0300 | [diff] [blame] | 1006 | }; |
Pavel Begunkov | 2293b41 | 2020-03-07 01:15:39 +0300 | [diff] [blame] | 1007 | int node; |
| 1008 | |
Pavel Begunkov | f4c2665 | 2020-06-15 10:24:02 +0300 | [diff] [blame] | 1009 | /* |
| 1010 | * First check pending list, if we're lucky we can just remove it |
| 1011 | * from there. CANCEL_OK means that the work is returned as-new, |
| 1012 | * no completion will be posted for it. |
| 1013 | */ |
Pavel Begunkov | 2293b41 | 2020-03-07 01:15:39 +0300 | [diff] [blame] | 1014 | for_each_node(node) { |
| 1015 | struct io_wqe *wqe = wq->wqes[node]; |
| 1016 | |
Pavel Begunkov | 4f26bda | 2020-06-15 10:24:03 +0300 | [diff] [blame] | 1017 | io_wqe_cancel_pending_work(wqe, &match); |
| 1018 | if (match.nr_pending && !match.cancel_all) |
Pavel Begunkov | f4c2665 | 2020-06-15 10:24:02 +0300 | [diff] [blame] | 1019 | return IO_WQ_CANCEL_OK; |
Pavel Begunkov | 2293b41 | 2020-03-07 01:15:39 +0300 | [diff] [blame] | 1020 | } |
| 1021 | |
Pavel Begunkov | f4c2665 | 2020-06-15 10:24:02 +0300 | [diff] [blame] | 1022 | /* |
| 1023 | * Now check if a free (going busy) or busy worker has the work |
| 1024 | * currently running. If we find it there, we'll return CANCEL_RUNNING |
| 1025 | * as an indication that we attempt to signal cancellation. The |
| 1026 | * completion will run normally in this case. |
| 1027 | */ |
| 1028 | for_each_node(node) { |
| 1029 | struct io_wqe *wqe = wq->wqes[node]; |
| 1030 | |
Pavel Begunkov | 4f26bda | 2020-06-15 10:24:03 +0300 | [diff] [blame] | 1031 | io_wqe_cancel_running_work(wqe, &match); |
| 1032 | if (match.nr_running && !match.cancel_all) |
Pavel Begunkov | f4c2665 | 2020-06-15 10:24:02 +0300 | [diff] [blame] | 1033 | return IO_WQ_CANCEL_RUNNING; |
| 1034 | } |
| 1035 | |
Pavel Begunkov | 4f26bda | 2020-06-15 10:24:03 +0300 | [diff] [blame] | 1036 | if (match.nr_running) |
| 1037 | return IO_WQ_CANCEL_RUNNING; |
| 1038 | if (match.nr_pending) |
| 1039 | return IO_WQ_CANCEL_OK; |
Pavel Begunkov | f4c2665 | 2020-06-15 10:24:02 +0300 | [diff] [blame] | 1040 | return IO_WQ_CANCEL_NOTFOUND; |
Pavel Begunkov | 2293b41 | 2020-03-07 01:15:39 +0300 | [diff] [blame] | 1041 | } |
| 1042 | |
Jens Axboe | e941894 | 2021-02-19 12:33:30 -0700 | [diff] [blame] | 1043 | static int io_wqe_hash_wake(struct wait_queue_entry *wait, unsigned mode, |
| 1044 | int sync, void *key) |
| 1045 | { |
| 1046 | struct io_wqe *wqe = container_of(wait, struct io_wqe, wait); |
Jens Axboe | f95dc20 | 2021-08-31 13:57:32 -0600 | [diff] [blame] | 1047 | int i; |
Jens Axboe | e941894 | 2021-02-19 12:33:30 -0700 | [diff] [blame] | 1048 | |
| 1049 | list_del_init(&wait->entry); |
| 1050 | |
| 1051 | rcu_read_lock(); |
Jens Axboe | f95dc20 | 2021-08-31 13:57:32 -0600 | [diff] [blame] | 1052 | for (i = 0; i < IO_WQ_ACCT_NR; i++) { |
| 1053 | struct io_wqe_acct *acct = &wqe->acct[i]; |
| 1054 | |
| 1055 | if (test_and_clear_bit(IO_ACCT_STALLED_BIT, &acct->flags)) |
| 1056 | io_wqe_activate_free_worker(wqe, acct); |
| 1057 | } |
Jens Axboe | e941894 | 2021-02-19 12:33:30 -0700 | [diff] [blame] | 1058 | rcu_read_unlock(); |
Jens Axboe | e941894 | 2021-02-19 12:33:30 -0700 | [diff] [blame] | 1059 | return 1; |
| 1060 | } |
| 1061 | |
Jens Axboe | 576a347 | 2019-11-25 08:49:20 -0700 | [diff] [blame] | 1062 | struct io_wq *io_wq_create(unsigned bounded, struct io_wq_data *data) |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 1063 | { |
Jens Axboe | f95dc20 | 2021-08-31 13:57:32 -0600 | [diff] [blame] | 1064 | int ret, node, i; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 1065 | struct io_wq *wq; |
| 1066 | |
Pavel Begunkov | f5fa38c | 2020-06-08 21:08:20 +0300 | [diff] [blame] | 1067 | if (WARN_ON_ONCE(!data->free_work || !data->do_work)) |
Pavel Begunkov | e9fd939 | 2020-03-04 16:14:12 +0300 | [diff] [blame] | 1068 | return ERR_PTR(-EINVAL); |
Pavel Begunkov | e6ab899 | 2021-06-17 18:13:59 +0100 | [diff] [blame] | 1069 | if (WARN_ON_ONCE(!bounded)) |
| 1070 | return ERR_PTR(-EINVAL); |
Pavel Begunkov | e9fd939 | 2020-03-04 16:14:12 +0300 | [diff] [blame] | 1071 | |
Pavel Begunkov | c7f405d | 2021-06-14 02:36:12 +0100 | [diff] [blame] | 1072 | wq = kzalloc(struct_size(wq, wqes, nr_node_ids), GFP_KERNEL); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 1073 | if (!wq) |
| 1074 | return ERR_PTR(-ENOMEM); |
Jens Axboe | 43c01fb | 2020-10-22 09:02:50 -0600 | [diff] [blame] | 1075 | ret = cpuhp_state_add_instance_nocalls(io_wq_online, &wq->cpuhp_node); |
| 1076 | if (ret) |
Pavel Begunkov | c7f405d | 2021-06-14 02:36:12 +0100 | [diff] [blame] | 1077 | goto err_wq; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 1078 | |
Jens Axboe | e941894 | 2021-02-19 12:33:30 -0700 | [diff] [blame] | 1079 | refcount_inc(&data->hash->refs); |
| 1080 | wq->hash = data->hash; |
Pavel Begunkov | e9fd939 | 2020-03-04 16:14:12 +0300 | [diff] [blame] | 1081 | wq->free_work = data->free_work; |
Pavel Begunkov | f5fa38c | 2020-06-08 21:08:20 +0300 | [diff] [blame] | 1082 | wq->do_work = data->do_work; |
Jens Axboe | 7d72306 | 2019-11-12 22:31:31 -0700 | [diff] [blame] | 1083 | |
Jens Axboe | 43c01fb | 2020-10-22 09:02:50 -0600 | [diff] [blame] | 1084 | ret = -ENOMEM; |
Jann Horn | 3fc50ab | 2019-11-26 19:10:20 +0100 | [diff] [blame] | 1085 | for_each_node(node) { |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 1086 | struct io_wqe *wqe; |
Jens Axboe | 7563439 | 2020-02-11 06:30:06 -0700 | [diff] [blame] | 1087 | int alloc_node = node; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 1088 | |
Jens Axboe | 7563439 | 2020-02-11 06:30:06 -0700 | [diff] [blame] | 1089 | if (!node_online(alloc_node)) |
| 1090 | alloc_node = NUMA_NO_NODE; |
| 1091 | wqe = kzalloc_node(sizeof(struct io_wqe), GFP_KERNEL, alloc_node); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 1092 | if (!wqe) |
Jann Horn | 3fc50ab | 2019-11-26 19:10:20 +0100 | [diff] [blame] | 1093 | goto err; |
Jens Axboe | 0e03496 | 2021-06-17 10:08:11 -0600 | [diff] [blame] | 1094 | if (!alloc_cpumask_var(&wqe->cpu_mask, GFP_KERNEL)) |
| 1095 | goto err; |
| 1096 | cpumask_copy(wqe->cpu_mask, cpumask_of_node(node)); |
Jann Horn | 3fc50ab | 2019-11-26 19:10:20 +0100 | [diff] [blame] | 1097 | wq->wqes[node] = wqe; |
Jens Axboe | 7563439 | 2020-02-11 06:30:06 -0700 | [diff] [blame] | 1098 | wqe->node = alloc_node; |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 1099 | wqe->acct[IO_WQ_ACCT_BOUND].max_workers = bounded; |
Jens Axboe | 728f13e | 2021-02-21 16:02:53 -0700 | [diff] [blame] | 1100 | wqe->acct[IO_WQ_ACCT_UNBOUND].max_workers = |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 1101 | task_rlimit(current, RLIMIT_NPROC); |
Jens Axboe | e941894 | 2021-02-19 12:33:30 -0700 | [diff] [blame] | 1102 | INIT_LIST_HEAD(&wqe->wait.entry); |
Jens Axboe | f95dc20 | 2021-08-31 13:57:32 -0600 | [diff] [blame] | 1103 | wqe->wait.func = io_wqe_hash_wake; |
| 1104 | for (i = 0; i < IO_WQ_ACCT_NR; i++) { |
| 1105 | struct io_wqe_acct *acct = &wqe->acct[i]; |
| 1106 | |
| 1107 | acct->index = i; |
| 1108 | atomic_set(&acct->nr_running, 0); |
| 1109 | INIT_WQ_LIST(&acct->work_list); |
| 1110 | } |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 1111 | wqe->wq = wq; |
Sebastian Andrzej Siewior | 95da846 | 2020-09-01 10:41:46 +0200 | [diff] [blame] | 1112 | raw_spin_lock_init(&wqe->lock); |
Jens Axboe | 021d1cd | 2019-11-14 08:00:41 -0700 | [diff] [blame] | 1113 | INIT_HLIST_NULLS_HEAD(&wqe->free_list, 0); |
Jens Axboe | e61df66 | 2019-11-13 13:54:49 -0700 | [diff] [blame] | 1114 | INIT_LIST_HEAD(&wqe->all_list); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 1115 | } |
| 1116 | |
Jens Axboe | 685fe7f | 2021-03-08 09:37:51 -0700 | [diff] [blame] | 1117 | wq->task = get_task_struct(data->task); |
Jens Axboe | 685fe7f | 2021-03-08 09:37:51 -0700 | [diff] [blame] | 1118 | atomic_set(&wq->worker_refs, 1); |
| 1119 | init_completion(&wq->worker_done); |
| 1120 | return wq; |
Jens Axboe | b60fda6 | 2019-11-19 08:37:07 -0700 | [diff] [blame] | 1121 | err: |
Jens Axboe | dc7bbc9 | 2021-03-01 09:09:56 -0700 | [diff] [blame] | 1122 | io_wq_put_hash(data->hash); |
Jens Axboe | 43c01fb | 2020-10-22 09:02:50 -0600 | [diff] [blame] | 1123 | cpuhp_state_remove_instance_nocalls(io_wq_online, &wq->cpuhp_node); |
Jens Axboe | 0e03496 | 2021-06-17 10:08:11 -0600 | [diff] [blame] | 1124 | for_each_node(node) { |
| 1125 | if (!wq->wqes[node]) |
| 1126 | continue; |
| 1127 | free_cpumask_var(wq->wqes[node]->cpu_mask); |
Jann Horn | 3fc50ab | 2019-11-26 19:10:20 +0100 | [diff] [blame] | 1128 | kfree(wq->wqes[node]); |
Jens Axboe | 0e03496 | 2021-06-17 10:08:11 -0600 | [diff] [blame] | 1129 | } |
Jens Axboe | 43c01fb | 2020-10-22 09:02:50 -0600 | [diff] [blame] | 1130 | err_wq: |
Jens Axboe | b60fda6 | 2019-11-19 08:37:07 -0700 | [diff] [blame] | 1131 | kfree(wq); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 1132 | return ERR_PTR(ret); |
| 1133 | } |
| 1134 | |
Jens Axboe | c80ca47 | 2021-04-01 19:57:07 -0600 | [diff] [blame] | 1135 | static bool io_task_work_match(struct callback_head *cb, void *data) |
| 1136 | { |
Jens Axboe | d3e9f73 | 2021-08-04 08:37:25 -0600 | [diff] [blame] | 1137 | struct io_worker *worker; |
Jens Axboe | c80ca47 | 2021-04-01 19:57:07 -0600 | [diff] [blame] | 1138 | |
Jens Axboe | 3b33e3f | 2021-09-08 19:57:26 -0600 | [diff] [blame] | 1139 | if (cb->func != create_worker_cb && cb->func != create_worker_cont) |
Jens Axboe | c80ca47 | 2021-04-01 19:57:07 -0600 | [diff] [blame] | 1140 | return false; |
Jens Axboe | d3e9f73 | 2021-08-04 08:37:25 -0600 | [diff] [blame] | 1141 | worker = container_of(cb, struct io_worker, create_work); |
| 1142 | return worker->wqe->wq == data; |
Jens Axboe | c80ca47 | 2021-04-01 19:57:07 -0600 | [diff] [blame] | 1143 | } |
| 1144 | |
Pavel Begunkov | 17a9105 | 2021-05-23 15:48:39 +0100 | [diff] [blame] | 1145 | void io_wq_exit_start(struct io_wq *wq) |
| 1146 | { |
| 1147 | set_bit(IO_WQ_BIT_EXIT, &wq->state); |
| 1148 | } |
| 1149 | |
Jens Axboe | 685fe7f | 2021-03-08 09:37:51 -0700 | [diff] [blame] | 1150 | static void io_wq_exit_workers(struct io_wq *wq) |
Jens Axboe | afcc401 | 2021-02-26 13:48:19 -0700 | [diff] [blame] | 1151 | { |
Jens Axboe | 685fe7f | 2021-03-08 09:37:51 -0700 | [diff] [blame] | 1152 | struct callback_head *cb; |
| 1153 | int node; |
| 1154 | |
Jens Axboe | 685fe7f | 2021-03-08 09:37:51 -0700 | [diff] [blame] | 1155 | if (!wq->task) |
| 1156 | return; |
| 1157 | |
Jens Axboe | c80ca47 | 2021-04-01 19:57:07 -0600 | [diff] [blame] | 1158 | while ((cb = task_work_cancel_match(wq->task, io_task_work_match, wq)) != NULL) { |
Jens Axboe | d3e9f73 | 2021-08-04 08:37:25 -0600 | [diff] [blame] | 1159 | struct io_worker *worker; |
Jens Axboe | 3b33e3f | 2021-09-08 19:57:26 -0600 | [diff] [blame] | 1160 | struct io_wqe_acct *acct; |
Jens Axboe | 685fe7f | 2021-03-08 09:37:51 -0700 | [diff] [blame] | 1161 | |
Jens Axboe | d3e9f73 | 2021-08-04 08:37:25 -0600 | [diff] [blame] | 1162 | worker = container_of(cb, struct io_worker, create_work); |
Jens Axboe | 3b33e3f | 2021-09-08 19:57:26 -0600 | [diff] [blame] | 1163 | acct = io_wqe_get_acct(worker); |
| 1164 | atomic_dec(&acct->nr_running); |
| 1165 | raw_spin_lock(&worker->wqe->lock); |
| 1166 | acct->nr_workers--; |
| 1167 | raw_spin_unlock(&worker->wqe->lock); |
Jens Axboe | 685fe7f | 2021-03-08 09:37:51 -0700 | [diff] [blame] | 1168 | io_worker_ref_put(wq); |
Jens Axboe | d3e9f73 | 2021-08-04 08:37:25 -0600 | [diff] [blame] | 1169 | clear_bit_unlock(0, &worker->create_state); |
| 1170 | io_worker_release(worker); |
Jens Axboe | afcc401 | 2021-02-26 13:48:19 -0700 | [diff] [blame] | 1171 | } |
Jens Axboe | 685fe7f | 2021-03-08 09:37:51 -0700 | [diff] [blame] | 1172 | |
| 1173 | rcu_read_lock(); |
| 1174 | for_each_node(node) { |
| 1175 | struct io_wqe *wqe = wq->wqes[node]; |
| 1176 | |
| 1177 | io_wq_for_each_worker(wqe, io_wq_worker_wake, NULL); |
Jens Axboe | 685fe7f | 2021-03-08 09:37:51 -0700 | [diff] [blame] | 1178 | } |
| 1179 | rcu_read_unlock(); |
| 1180 | io_worker_ref_put(wq); |
| 1181 | wait_for_completion(&wq->worker_done); |
Zqiang | 3743c17 | 2021-05-26 13:08:26 +0800 | [diff] [blame] | 1182 | |
| 1183 | for_each_node(node) { |
| 1184 | spin_lock_irq(&wq->hash->wait.lock); |
| 1185 | list_del_init(&wq->wqes[node]->wait.entry); |
| 1186 | spin_unlock_irq(&wq->hash->wait.lock); |
| 1187 | } |
Jens Axboe | 685fe7f | 2021-03-08 09:37:51 -0700 | [diff] [blame] | 1188 | put_task_struct(wq->task); |
| 1189 | wq->task = NULL; |
Jens Axboe | afcc401 | 2021-02-26 13:48:19 -0700 | [diff] [blame] | 1190 | } |
| 1191 | |
Jens Axboe | 4fb6ac3 | 2021-02-25 10:17:09 -0700 | [diff] [blame] | 1192 | static void io_wq_destroy(struct io_wq *wq) |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 1193 | { |
Jann Horn | 3fc50ab | 2019-11-26 19:10:20 +0100 | [diff] [blame] | 1194 | int node; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 1195 | |
Jens Axboe | 43c01fb | 2020-10-22 09:02:50 -0600 | [diff] [blame] | 1196 | cpuhp_state_remove_instance_nocalls(io_wq_online, &wq->cpuhp_node); |
| 1197 | |
Jens Axboe | e941894 | 2021-02-19 12:33:30 -0700 | [diff] [blame] | 1198 | for_each_node(node) { |
| 1199 | struct io_wqe *wqe = wq->wqes[node]; |
Jens Axboe | f5d2d23 | 2021-03-25 10:16:12 -0600 | [diff] [blame] | 1200 | struct io_cb_cancel_data match = { |
| 1201 | .fn = io_wq_work_match_all, |
| 1202 | .cancel_all = true, |
| 1203 | }; |
| 1204 | io_wqe_cancel_pending_work(wqe, &match); |
Jens Axboe | 0e03496 | 2021-06-17 10:08:11 -0600 | [diff] [blame] | 1205 | free_cpumask_var(wqe->cpu_mask); |
Jens Axboe | e941894 | 2021-02-19 12:33:30 -0700 | [diff] [blame] | 1206 | kfree(wqe); |
| 1207 | } |
Jens Axboe | e941894 | 2021-02-19 12:33:30 -0700 | [diff] [blame] | 1208 | io_wq_put_hash(wq->hash); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 1209 | kfree(wq); |
Jens Axboe | 4fb6ac3 | 2021-02-25 10:17:09 -0700 | [diff] [blame] | 1210 | } |
| 1211 | |
Jens Axboe | afcc401 | 2021-02-26 13:48:19 -0700 | [diff] [blame] | 1212 | void io_wq_put_and_exit(struct io_wq *wq) |
| 1213 | { |
Pavel Begunkov | 17a9105 | 2021-05-23 15:48:39 +0100 | [diff] [blame] | 1214 | WARN_ON_ONCE(!test_bit(IO_WQ_BIT_EXIT, &wq->state)); |
| 1215 | |
Jens Axboe | 685fe7f | 2021-03-08 09:37:51 -0700 | [diff] [blame] | 1216 | io_wq_exit_workers(wq); |
Pavel Begunkov | 382cb03 | 2021-06-14 02:36:13 +0100 | [diff] [blame] | 1217 | io_wq_destroy(wq); |
Jens Axboe | afcc401 | 2021-02-26 13:48:19 -0700 | [diff] [blame] | 1218 | } |
| 1219 | |
Jens Axboe | 0e03496 | 2021-06-17 10:08:11 -0600 | [diff] [blame] | 1220 | struct online_data { |
| 1221 | unsigned int cpu; |
| 1222 | bool online; |
| 1223 | }; |
| 1224 | |
Jens Axboe | 43c01fb | 2020-10-22 09:02:50 -0600 | [diff] [blame] | 1225 | static bool io_wq_worker_affinity(struct io_worker *worker, void *data) |
| 1226 | { |
Jens Axboe | 0e03496 | 2021-06-17 10:08:11 -0600 | [diff] [blame] | 1227 | struct online_data *od = data; |
Jens Axboe | 43c01fb | 2020-10-22 09:02:50 -0600 | [diff] [blame] | 1228 | |
Jens Axboe | 0e03496 | 2021-06-17 10:08:11 -0600 | [diff] [blame] | 1229 | if (od->online) |
| 1230 | cpumask_set_cpu(od->cpu, worker->wqe->cpu_mask); |
| 1231 | else |
| 1232 | cpumask_clear_cpu(od->cpu, worker->wqe->cpu_mask); |
Jens Axboe | 43c01fb | 2020-10-22 09:02:50 -0600 | [diff] [blame] | 1233 | return false; |
| 1234 | } |
| 1235 | |
Jens Axboe | 0e03496 | 2021-06-17 10:08:11 -0600 | [diff] [blame] | 1236 | static int __io_wq_cpu_online(struct io_wq *wq, unsigned int cpu, bool online) |
| 1237 | { |
| 1238 | struct online_data od = { |
| 1239 | .cpu = cpu, |
| 1240 | .online = online |
| 1241 | }; |
| 1242 | int i; |
| 1243 | |
| 1244 | rcu_read_lock(); |
| 1245 | for_each_node(i) |
| 1246 | io_wq_for_each_worker(wq->wqes[i], io_wq_worker_affinity, &od); |
| 1247 | rcu_read_unlock(); |
| 1248 | return 0; |
| 1249 | } |
| 1250 | |
Jens Axboe | 43c01fb | 2020-10-22 09:02:50 -0600 | [diff] [blame] | 1251 | static int io_wq_cpu_online(unsigned int cpu, struct hlist_node *node) |
| 1252 | { |
| 1253 | struct io_wq *wq = hlist_entry_safe(node, struct io_wq, cpuhp_node); |
Jens Axboe | 43c01fb | 2020-10-22 09:02:50 -0600 | [diff] [blame] | 1254 | |
Jens Axboe | 0e03496 | 2021-06-17 10:08:11 -0600 | [diff] [blame] | 1255 | return __io_wq_cpu_online(wq, cpu, true); |
| 1256 | } |
| 1257 | |
| 1258 | static int io_wq_cpu_offline(unsigned int cpu, struct hlist_node *node) |
| 1259 | { |
| 1260 | struct io_wq *wq = hlist_entry_safe(node, struct io_wq, cpuhp_node); |
| 1261 | |
| 1262 | return __io_wq_cpu_online(wq, cpu, false); |
Jens Axboe | 43c01fb | 2020-10-22 09:02:50 -0600 | [diff] [blame] | 1263 | } |
| 1264 | |
Jens Axboe | fe76421 | 2021-06-17 10:19:54 -0600 | [diff] [blame] | 1265 | int io_wq_cpu_affinity(struct io_wq *wq, cpumask_var_t mask) |
| 1266 | { |
| 1267 | int i; |
| 1268 | |
| 1269 | rcu_read_lock(); |
| 1270 | for_each_node(i) { |
| 1271 | struct io_wqe *wqe = wq->wqes[i]; |
| 1272 | |
| 1273 | if (mask) |
| 1274 | cpumask_copy(wqe->cpu_mask, mask); |
| 1275 | else |
| 1276 | cpumask_copy(wqe->cpu_mask, cpumask_of_node(i)); |
| 1277 | } |
| 1278 | rcu_read_unlock(); |
| 1279 | return 0; |
| 1280 | } |
| 1281 | |
Jens Axboe | 2e48005 | 2021-08-27 11:33:19 -0600 | [diff] [blame] | 1282 | /* |
| 1283 | * Set max number of unbounded workers, returns old value. If new_count is 0, |
| 1284 | * then just return the old value. |
| 1285 | */ |
| 1286 | int io_wq_max_workers(struct io_wq *wq, int *new_count) |
| 1287 | { |
| 1288 | int i, node, prev = 0; |
| 1289 | |
| 1290 | for (i = 0; i < 2; i++) { |
| 1291 | if (new_count[i] > task_rlimit(current, RLIMIT_NPROC)) |
| 1292 | new_count[i] = task_rlimit(current, RLIMIT_NPROC); |
| 1293 | } |
| 1294 | |
| 1295 | rcu_read_lock(); |
| 1296 | for_each_node(node) { |
| 1297 | struct io_wqe_acct *acct; |
| 1298 | |
Jens Axboe | f95dc20 | 2021-08-31 13:57:32 -0600 | [diff] [blame] | 1299 | for (i = 0; i < IO_WQ_ACCT_NR; i++) { |
Jens Axboe | 2e48005 | 2021-08-27 11:33:19 -0600 | [diff] [blame] | 1300 | acct = &wq->wqes[node]->acct[i]; |
| 1301 | prev = max_t(int, acct->max_workers, prev); |
| 1302 | if (new_count[i]) |
| 1303 | acct->max_workers = new_count[i]; |
| 1304 | new_count[i] = prev; |
| 1305 | } |
| 1306 | } |
| 1307 | rcu_read_unlock(); |
| 1308 | return 0; |
| 1309 | } |
| 1310 | |
Jens Axboe | 43c01fb | 2020-10-22 09:02:50 -0600 | [diff] [blame] | 1311 | static __init int io_wq_init(void) |
| 1312 | { |
| 1313 | int ret; |
| 1314 | |
| 1315 | ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "io-wq/online", |
Jens Axboe | 0e03496 | 2021-06-17 10:08:11 -0600 | [diff] [blame] | 1316 | io_wq_cpu_online, io_wq_cpu_offline); |
Jens Axboe | 43c01fb | 2020-10-22 09:02:50 -0600 | [diff] [blame] | 1317 | if (ret < 0) |
| 1318 | return ret; |
| 1319 | io_wq_online = ret; |
| 1320 | return 0; |
| 1321 | } |
| 1322 | subsys_initcall(io_wq_init); |