Commit | Line | Data |
---|---|---|
2b188cc1 JA |
1 | // SPDX-License-Identifier: GPL-2.0 |
2 | /* | |
3 | * Shared application/kernel submission and completion ring pairs, for | |
4 | * supporting fast/efficient IO. | |
5 | * | |
6 | * A note on the read/write ordering memory barriers that are matched between | |
1e84b97b SB |
7 | * the application and kernel side. |
8 | * | |
9 | * After the application reads the CQ ring tail, it must use an | |
10 | * appropriate smp_rmb() to pair with the smp_wmb() the kernel uses | |
11 | * before writing the tail (using smp_load_acquire to read the tail will | |
12 | * do). It also needs a smp_mb() before updating CQ head (ordering the | |
13 | * entry load(s) with the head store), pairing with an implicit barrier | |
d068b506 | 14 | * through a control-dependency in io_get_cqe (smp_store_release to |
1e84b97b SB |
15 | * store head will do). Failure to do so could lead to reading invalid |
16 | * CQ entries. | |
17 | * | |
18 | * Likewise, the application must use an appropriate smp_wmb() before | |
19 | * writing the SQ tail (ordering SQ entry stores with the tail store), | |
20 | * which pairs with smp_load_acquire in io_get_sqring (smp_store_release | |
21 | * to store the tail will do). And it needs a barrier ordering the SQ | |
22 | * head load before writing new SQ entries (smp_load_acquire to read | |
23 | * head will do). | |
24 | * | |
25 | * When using the SQ poll thread (IORING_SETUP_SQPOLL), the application | |
26 | * needs to check the SQ flags for IORING_SQ_NEED_WAKEUP *after* | |
27 | * updating the SQ tail; a full memory barrier smp_mb() is needed | |
28 | * between. | |
2b188cc1 JA |
29 | * |
30 | * Also see the examples in the liburing library: | |
31 | * | |
32 | * git://git.kernel.dk/liburing | |
33 | * | |
34 | * io_uring also uses READ/WRITE_ONCE() for _any_ store or load that happens | |
35 | * from data shared between the kernel and application. This is done both | |
36 | * for ordering purposes, but also to ensure that once a value is loaded from | |
37 | * data that the application could potentially modify, it remains stable. | |
38 | * | |
39 | * Copyright (C) 2018-2019 Jens Axboe | |
c992fe29 | 40 | * Copyright (c) 2018-2019 Christoph Hellwig |
2b188cc1 JA |
41 | */ |
42 | #include <linux/kernel.h> | |
43 | #include <linux/init.h> | |
44 | #include <linux/errno.h> | |
45 | #include <linux/syscalls.h> | |
52de1fe1 | 46 | #include <net/compat.h> |
2b188cc1 JA |
47 | #include <linux/refcount.h> |
48 | #include <linux/uio.h> | |
6b47ee6e | 49 | #include <linux/bits.h> |
2b188cc1 JA |
50 | |
51 | #include <linux/sched/signal.h> | |
52 | #include <linux/fs.h> | |
53 | #include <linux/file.h> | |
2b188cc1 JA |
54 | #include <linux/mm.h> |
55 | #include <linux/mman.h> | |
2b188cc1 JA |
56 | #include <linux/percpu.h> |
57 | #include <linux/slab.h> | |
edafccee | 58 | #include <linux/bvec.h> |
2b188cc1 JA |
59 | #include <linux/net.h> |
60 | #include <net/sock.h> | |
2b188cc1 JA |
61 | #include <linux/anon_inodes.h> |
62 | #include <linux/sched/mm.h> | |
63 | #include <linux/uaccess.h> | |
64 | #include <linux/nospec.h> | |
15b71abe | 65 | #include <linux/fsnotify.h> |
4840e418 | 66 | #include <linux/fadvise.h> |
b41e9852 | 67 | #include <linux/task_work.h> |
0f212204 | 68 | #include <linux/io_uring.h> |
b66509b8 | 69 | #include <linux/io_uring/cmd.h> |
5bd2182d | 70 | #include <linux/audit.h> |
cdc1404a | 71 | #include <linux/security.h> |
9b296c62 | 72 | #include <linux/jump_label.h> |
d808459b | 73 | #include <asm/shmparam.h> |
2b188cc1 | 74 | |
c826bd7a DD |
75 | #define CREATE_TRACE_POINTS |
76 | #include <trace/events/io_uring.h> | |
77 | ||
2b188cc1 JA |
78 | #include <uapi/linux/io_uring.h> |
79 | ||
561fb04a | 80 | #include "io-wq.h" |
2b188cc1 | 81 | |
de23077e | 82 | #include "io_uring.h" |
329061d3 | 83 | #include "opdef.h" |
e418bbc9 | 84 | #include "refs.h" |
c9f06aa7 | 85 | #include "tctx.h" |
c4320315 | 86 | #include "register.h" |
17437f31 | 87 | #include "sqpoll.h" |
a4ad4f74 | 88 | #include "fdinfo.h" |
3b77495a | 89 | #include "kbuf.h" |
73572984 | 90 | #include "rsrc.h" |
38513c46 | 91 | #include "cancel.h" |
43e0bbbd | 92 | #include "net.h" |
eb42cebb | 93 | #include "notif.h" |
f31ecf67 | 94 | #include "waitid.h" |
194bb58c | 95 | #include "futex.h" |
8d0c12a8 | 96 | #include "napi.h" |
da12d9ab | 97 | #include "uring_cmd.h" |
50cf5f38 | 98 | #include "msg_ring.h" |
f15ed8b4 | 99 | #include "memmap.h" |
6f377873 | 100 | #include "zcrx.h" |
e27f928e | 101 | |
59915143 | 102 | #include "timeout.h" |
329061d3 | 103 | #include "poll.h" |
c92fcfc2 | 104 | #include "rw.h" |
9b797a37 | 105 | #include "alloc_cache.h" |
200f3abd | 106 | #include "eventfd.h" |
5e2a18d9 | 107 | |
68fe256a PB |
108 | #define SQE_COMMON_FLAGS (IOSQE_FIXED_FILE | IOSQE_IO_LINK | \ |
109 | IOSQE_IO_HARDLINK | IOSQE_ASYNC) | |
110 | ||
5562a8d7 PB |
111 | #define SQE_VALID_FLAGS (SQE_COMMON_FLAGS | IOSQE_BUFFER_SELECT | \ |
112 | IOSQE_IO_DRAIN | IOSQE_CQE_SKIP_SUCCESS) | |
68fe256a | 113 | |
0e893472 CSM |
114 | #define IO_REQ_LINK_FLAGS (REQ_F_LINK | REQ_F_HARDLINK) |
115 | ||
c854357b | 116 | #define IO_REQ_CLEAN_FLAGS (REQ_F_BUFFER_SELECTED | REQ_F_NEED_CLEANUP | \ |
9cae36a0 JA |
117 | REQ_F_POLLED | REQ_F_INFLIGHT | REQ_F_CREDS | \ |
118 | REQ_F_ASYNC_DATA) | |
b16fed66 | 119 | |
0e893472 | 120 | #define IO_REQ_CLEAN_SLOW_FLAGS (REQ_F_REFCOUNT | IO_REQ_LINK_FLAGS | \ |
d803d123 | 121 | REQ_F_REISSUE | IO_REQ_CLEAN_FLAGS) |
a538be5b | 122 | |
09899b19 PB |
123 | #define IO_TCTX_REFS_CACHE_NR (1U << 10) |
124 | ||
6dd0be1e | 125 | #define IO_COMPL_BATCH 32 |
bf019da7 | 126 | #define IO_REQ_ALLOC_BATCH 8 |
f46b9cdb | 127 | #define IO_LOCAL_TW_DEFAULT_MAX 20 |
258b29a9 | 128 | |
27dc8338 PB |
129 | struct io_defer_entry { |
130 | struct list_head list; | |
131 | struct io_kiocb *req; | |
2b188cc1 JA |
132 | }; |
133 | ||
0756a869 PB |
134 | /* requests with any of those set should undergo io_disarm_next() */ |
135 | #define IO_DISARM_MASK (REQ_F_ARM_LTIMEOUT | REQ_F_LINK_TIMEOUT | REQ_F_FAIL) | |
136 | ||
b4bc35cf PB |
137 | /* |
138 | * No waiters. It's larger than any valid value of the tw counter | |
139 | * so that tests against ->cq_wait_nr would fail and skip wake_up(). | |
140 | */ | |
141 | #define IO_CQ_WAKE_INIT (-1U) | |
142 | /* Forced wake up if there is a waiter regardless of ->cq_wait_nr */ | |
143 | #define IO_CQ_WAKE_FORCE (IO_CQ_WAKE_INIT >> 1) | |
144 | ||
affa87db | 145 | static bool io_uring_try_cancel_requests(struct io_ring_ctx *ctx, |
f03baece | 146 | struct io_uring_task *tctx, |
a13030fd BQM |
147 | bool cancel_all, |
148 | bool is_sqpoll_thread); | |
1ffc5422 | 149 | |
cbc2e203 | 150 | static void io_queue_sqe(struct io_kiocb *req); |
8fb7aee0 | 151 | static void __io_req_caches_free(struct io_ring_ctx *ctx); |
de0617e4 | 152 | |
9b296c62 PB |
153 | static __read_mostly DEFINE_STATIC_KEY_FALSE(io_key_has_sqarray); |
154 | ||
c1755c25 | 155 | struct kmem_cache *req_cachep; |
73eaa2b5 | 156 | static struct workqueue_struct *iou_wq __ro_after_init; |
2b188cc1 | 157 | |
76d3ccec MR |
158 | static int __read_mostly sysctl_io_uring_disabled; |
159 | static int __read_mostly sysctl_io_uring_group = -1; | |
160 | ||
161 | #ifdef CONFIG_SYSCTL | |
1751f872 | 162 | static const struct ctl_table kernel_io_uring_disabled_table[] = { |
76d3ccec MR |
163 | { |
164 | .procname = "io_uring_disabled", | |
165 | .data = &sysctl_io_uring_disabled, | |
166 | .maxlen = sizeof(sysctl_io_uring_disabled), | |
167 | .mode = 0644, | |
168 | .proc_handler = proc_dointvec_minmax, | |
169 | .extra1 = SYSCTL_ZERO, | |
170 | .extra2 = SYSCTL_TWO, | |
171 | }, | |
172 | { | |
173 | .procname = "io_uring_group", | |
174 | .data = &sysctl_io_uring_group, | |
175 | .maxlen = sizeof(gid_t), | |
176 | .mode = 0644, | |
177 | .proc_handler = proc_dointvec, | |
178 | }, | |
76d3ccec MR |
179 | }; |
180 | #endif | |
181 | ||
faf88dde PB |
182 | static inline unsigned int __io_cqring_events(struct io_ring_ctx *ctx) |
183 | { | |
184 | return ctx->cached_cq_tail - READ_ONCE(ctx->rings->cq.head); | |
185 | } | |
186 | ||
0fc8c2ac DY |
187 | static inline unsigned int __io_cqring_events_user(struct io_ring_ctx *ctx) |
188 | { | |
189 | return READ_ONCE(ctx->rings->cq.tail) - READ_ONCE(ctx->rings->cq.head); | |
190 | } | |
191 | ||
9cae36a0 JA |
192 | static bool io_match_linked(struct io_kiocb *head) |
193 | { | |
194 | struct io_kiocb *req; | |
195 | ||
196 | io_for_each_link(req, head) { | |
197 | if (req->flags & REQ_F_INFLIGHT) | |
198 | return true; | |
199 | } | |
200 | return false; | |
6af3f48b PB |
201 | } |
202 | ||
203 | /* | |
204 | * As io_match_task() but protected against racing with linked timeouts. | |
205 | * User must not hold timeout_lock. | |
206 | */ | |
f03baece | 207 | bool io_match_task_safe(struct io_kiocb *head, struct io_uring_task *tctx, |
329061d3 | 208 | bool cancel_all) |
6af3f48b | 209 | { |
9cae36a0 JA |
210 | bool matched; |
211 | ||
b6f58a3f | 212 | if (tctx && head->tctx != tctx) |
6af3f48b | 213 | return false; |
9cae36a0 JA |
214 | if (cancel_all) |
215 | return true; | |
216 | ||
217 | if (head->flags & REQ_F_LINK_TIMEOUT) { | |
218 | struct io_ring_ctx *ctx = head->ctx; | |
219 | ||
220 | /* protect against races with linked timeouts */ | |
020b40f3 | 221 | raw_spin_lock_irq(&ctx->timeout_lock); |
9cae36a0 | 222 | matched = io_match_linked(head); |
020b40f3 | 223 | raw_spin_unlock_irq(&ctx->timeout_lock); |
9cae36a0 JA |
224 | } else { |
225 | matched = io_match_linked(head); | |
226 | } | |
227 | return matched; | |
6af3f48b PB |
228 | } |
229 | ||
a8295b98 HX |
230 | static inline void req_fail_link_node(struct io_kiocb *req, int res) |
231 | { | |
232 | req_set_fail(req); | |
97b388d7 | 233 | io_req_set_res(req, res, 0); |
a8295b98 HX |
234 | } |
235 | ||
fa05457a PB |
236 | static inline void io_req_add_to_cache(struct io_kiocb *req, struct io_ring_ctx *ctx) |
237 | { | |
238 | wq_stack_add_head(&req->comp_list, &ctx->submit_state.free_list); | |
a8295b98 HX |
239 | } |
240 | ||
c072481d | 241 | static __cold void io_ring_ctx_ref_free(struct percpu_ref *ref) |
2b188cc1 JA |
242 | { |
243 | struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs); | |
244 | ||
0f158b4c | 245 | complete(&ctx->ref_comp); |
2b188cc1 JA |
246 | } |
247 | ||
c072481d | 248 | static __cold void io_fallback_req_func(struct work_struct *work) |
f56165e6 PB |
249 | { |
250 | struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx, | |
251 | fallback_work.work); | |
252 | struct llist_node *node = llist_del_all(&ctx->fallback_llist); | |
253 | struct io_kiocb *req, *tmp; | |
8e5b3b89 | 254 | struct io_tw_state ts = {}; |
f56165e6 | 255 | |
f7b32e78 | 256 | percpu_ref_get(&ctx->refs); |
31f084b7 | 257 | mutex_lock(&ctx->uring_lock); |
3218e5d3 | 258 | llist_for_each_entry_safe(req, tmp, node, io_task_work.node) |
94a4274b | 259 | req->io_task_work.func(req, ts); |
31f084b7 PB |
260 | io_submit_flush_completions(ctx); |
261 | mutex_unlock(&ctx->uring_lock); | |
f7b32e78 | 262 | percpu_ref_put(&ctx->refs); |
f56165e6 PB |
263 | } |
264 | ||
e6f89be6 PB |
265 | static int io_alloc_hash_table(struct io_hash_table *table, unsigned bits) |
266 | { | |
b6b3eb19 | 267 | unsigned int hash_buckets; |
8abf47a8 | 268 | int i; |
e6f89be6 | 269 | |
b6b3eb19 JA |
270 | do { |
271 | hash_buckets = 1U << bits; | |
272 | table->hbs = kvmalloc_array(hash_buckets, sizeof(table->hbs[0]), | |
273 | GFP_KERNEL_ACCOUNT); | |
274 | if (table->hbs) | |
275 | break; | |
276 | if (bits == 1) | |
277 | return -ENOMEM; | |
278 | bits--; | |
279 | } while (1); | |
e6f89be6 PB |
280 | |
281 | table->hash_bits = bits; | |
8abf47a8 JA |
282 | for (i = 0; i < hash_buckets; i++) |
283 | INIT_HLIST_HEAD(&table->hbs[i].list); | |
e6f89be6 PB |
284 | return 0; |
285 | } | |
286 | ||
40b99183 PB |
287 | static void io_free_alloc_caches(struct io_ring_ctx *ctx) |
288 | { | |
289 | io_alloc_cache_free(&ctx->apoll_cache, kfree); | |
290 | io_alloc_cache_free(&ctx->netmsg_cache, io_netmsg_cache_free); | |
291 | io_alloc_cache_free(&ctx->rw_cache, io_rw_cache_free); | |
3a4689ac | 292 | io_alloc_cache_free(&ctx->cmd_cache, io_cmd_cache_free); |
40b99183 PB |
293 | io_alloc_cache_free(&ctx->msg_cache, kfree); |
294 | io_futex_cache_free(ctx); | |
ed9f3112 | 295 | io_rsrc_cache_free(ctx); |
40b99183 PB |
296 | } |
297 | ||
c072481d | 298 | static __cold struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p) |
2b188cc1 JA |
299 | { |
300 | struct io_ring_ctx *ctx; | |
9cfc7e94 | 301 | int hash_bits; |
414d0f45 | 302 | bool ret; |
2b188cc1 JA |
303 | |
304 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); | |
305 | if (!ctx) | |
306 | return NULL; | |
307 | ||
9cfc7e94 JA |
308 | xa_init(&ctx->io_bl_xa); |
309 | ||
78076bb6 JA |
310 | /* |
311 | * Use 5 bits less than the max cq entries, that should give us around | |
4a07723f PB |
312 | * 32 entries per hash list if totally full and uniformly spread, but |
313 | * don't keep too many buckets to not overconsume memory. | |
78076bb6 | 314 | */ |
4a07723f PB |
315 | hash_bits = ilog2(p->cq_entries) - 5; |
316 | hash_bits = clamp(hash_bits, 1, 8); | |
e6f89be6 | 317 | if (io_alloc_hash_table(&ctx->cancel_table, hash_bits)) |
78076bb6 | 318 | goto err; |
21482896 | 319 | if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free, |
48904229 | 320 | 0, GFP_KERNEL)) |
206aefde | 321 | goto err; |
2b188cc1 JA |
322 | |
323 | ctx->flags = p->flags; | |
01ee194d | 324 | ctx->hybrid_poll_time = LLONG_MAX; |
b4bc35cf | 325 | atomic_set(&ctx->cq_wait_nr, IO_CQ_WAKE_INIT); |
90554200 | 326 | init_waitqueue_head(&ctx->sqo_sq_wait); |
69fb2131 | 327 | INIT_LIST_HEAD(&ctx->sqd_list); |
1d7bb1d5 | 328 | INIT_LIST_HEAD(&ctx->cq_overflow_list); |
fbbb8e99 | 329 | ret = io_alloc_cache_init(&ctx->apoll_cache, IO_POLL_ALLOC_CACHE_MAX, |
fa359552 | 330 | sizeof(struct async_poll), 0); |
414d0f45 | 331 | ret |= io_alloc_cache_init(&ctx->netmsg_cache, IO_ALLOC_CACHE_MAX, |
fa359552 JA |
332 | sizeof(struct io_async_msghdr), |
333 | offsetof(struct io_async_msghdr, clear)); | |
414d0f45 | 334 | ret |= io_alloc_cache_init(&ctx->rw_cache, IO_ALLOC_CACHE_MAX, |
fa359552 JA |
335 | sizeof(struct io_async_rw), |
336 | offsetof(struct io_async_rw, clear)); | |
575e7b06 | 337 | ret |= io_alloc_cache_init(&ctx->cmd_cache, IO_ALLOC_CACHE_MAX, |
3a4689ac PB |
338 | sizeof(struct io_async_cmd), |
339 | sizeof(struct io_async_cmd)); | |
50cf5f38 JA |
340 | spin_lock_init(&ctx->msg_lock); |
341 | ret |= io_alloc_cache_init(&ctx->msg_cache, IO_ALLOC_CACHE_MAX, | |
fa359552 | 342 | sizeof(struct io_kiocb), 0); |
414d0f45 | 343 | ret |= io_futex_cache_init(ctx); |
ed9f3112 | 344 | ret |= io_rsrc_cache_init(ctx); |
414d0f45 | 345 | if (ret) |
3a87e264 | 346 | goto free_ref; |
0f158b4c | 347 | init_completion(&ctx->ref_comp); |
61cf9370 | 348 | xa_init_flags(&ctx->personalities, XA_FLAGS_ALLOC1); |
2b188cc1 | 349 | mutex_init(&ctx->uring_lock); |
311997b3 | 350 | init_waitqueue_head(&ctx->cq_wait); |
7b235dd8 | 351 | init_waitqueue_head(&ctx->poll_wq); |
2b188cc1 | 352 | spin_lock_init(&ctx->completion_lock); |
020b40f3 | 353 | raw_spin_lock_init(&ctx->timeout_lock); |
5eef4e87 | 354 | INIT_WQ_LIST(&ctx->iopoll_list); |
de0617e4 | 355 | INIT_LIST_HEAD(&ctx->defer_list); |
5262f567 | 356 | INIT_LIST_HEAD(&ctx->timeout_list); |
ef9dd637 | 357 | INIT_LIST_HEAD(&ctx->ltimeout_list); |
c0e0d6ba | 358 | init_llist_head(&ctx->work_llist); |
13bf43f5 | 359 | INIT_LIST_HEAD(&ctx->tctx_list); |
c2b6c6bc | 360 | ctx->submit_state.free_list.next = NULL; |
f31ecf67 | 361 | INIT_HLIST_HEAD(&ctx->waitid_list); |
76f1cc98 | 362 | xa_init_flags(&ctx->zcrx_ctxs, XA_FLAGS_ALLOC); |
194bb58c JA |
363 | #ifdef CONFIG_FUTEX |
364 | INIT_HLIST_HEAD(&ctx->futex_list); | |
365 | #endif | |
9011bf9a | 366 | INIT_DELAYED_WORK(&ctx->fallback_work, io_fallback_req_func); |
6f33b0bc | 367 | INIT_WQ_LIST(&ctx->submit_state.compl_reqs); |
93b8cc60 | 368 | INIT_HLIST_HEAD(&ctx->cancelable_uring_cmd); |
8d0c12a8 | 369 | io_napi_init(ctx); |
943d0609 | 370 | mutex_init(&ctx->mmap_lock); |
8d0c12a8 | 371 | |
2b188cc1 | 372 | return ctx; |
3a87e264 GL |
373 | |
374 | free_ref: | |
375 | percpu_ref_exit(&ctx->refs); | |
206aefde | 376 | err: |
40b99183 | 377 | io_free_alloc_caches(ctx); |
b6b3eb19 | 378 | kvfree(ctx->cancel_table.hbs); |
9cfc7e94 | 379 | xa_destroy(&ctx->io_bl_xa); |
206aefde JA |
380 | kfree(ctx); |
381 | return NULL; | |
2b188cc1 JA |
382 | } |
383 | ||
5a754dea PB |
384 | static void io_clean_op(struct io_kiocb *req) |
385 | { | |
dd4fbb11 | 386 | if (unlikely(req->flags & REQ_F_BUFFER_SELECTED)) |
54e00d9a | 387 | io_kbuf_drop_legacy(req); |
5a754dea PB |
388 | |
389 | if (req->flags & REQ_F_NEED_CLEANUP) { | |
390 | const struct io_cold_def *def = &io_cold_defs[req->opcode]; | |
391 | ||
392 | if (def->cleanup) | |
393 | def->cleanup(req); | |
394 | } | |
395 | if ((req->flags & REQ_F_POLLED) && req->apoll) { | |
396 | kfree(req->apoll->double_poll); | |
397 | kfree(req->apoll); | |
398 | req->apoll = NULL; | |
399 | } | |
b6f58a3f JA |
400 | if (req->flags & REQ_F_INFLIGHT) |
401 | atomic_dec(&req->tctx->inflight_tracked); | |
5a754dea PB |
402 | if (req->flags & REQ_F_CREDS) |
403 | put_cred(req->creds); | |
404 | if (req->flags & REQ_F_ASYNC_DATA) { | |
405 | kfree(req->async_data); | |
406 | req->async_data = NULL; | |
407 | } | |
408 | req->flags &= ~IO_REQ_CLEAN_FLAGS; | |
409 | } | |
410 | ||
079afb08 JA |
411 | /* |
412 | * Mark the request as inflight, so that file cancelation will find it. | |
413 | * Can be used if the file is an io_uring instance, or if the request itself | |
414 | * relies on ->mm being alive for the duration of the request. | |
415 | */ | |
416 | inline void io_req_track_inflight(struct io_kiocb *req) | |
9cae36a0 JA |
417 | { |
418 | if (!(req->flags & REQ_F_INFLIGHT)) { | |
419 | req->flags |= REQ_F_INFLIGHT; | |
b6f58a3f | 420 | atomic_inc(&req->tctx->inflight_tracked); |
9cae36a0 JA |
421 | } |
422 | } | |
423 | ||
fd08e530 PB |
424 | static struct io_kiocb *__io_prep_linked_timeout(struct io_kiocb *req) |
425 | { | |
906c6caa PB |
426 | if (WARN_ON_ONCE(!req->link)) |
427 | return NULL; | |
428 | ||
4d13d1a4 PB |
429 | req->flags &= ~REQ_F_ARM_LTIMEOUT; |
430 | req->flags |= REQ_F_LINK_TIMEOUT; | |
fd08e530 PB |
431 | |
432 | /* linked timeouts should have two refs once prep'ed */ | |
48dcd38d | 433 | io_req_set_refcount(req); |
4d13d1a4 PB |
434 | __io_req_set_refcount(req->link, 2); |
435 | return req->link; | |
fd08e530 PB |
436 | } |
437 | ||
1e6fa521 JA |
438 | static void io_prep_async_work(struct io_kiocb *req) |
439 | { | |
a7dd2782 | 440 | const struct io_issue_def *def = &io_issue_defs[req->opcode]; |
1e6fa521 JA |
441 | struct io_ring_ctx *ctx = req->ctx; |
442 | ||
b8e64b53 PB |
443 | if (!(req->flags & REQ_F_CREDS)) { |
444 | req->flags |= REQ_F_CREDS; | |
c10d1f98 | 445 | req->creds = get_current_cred(); |
b8e64b53 | 446 | } |
003e8dcc | 447 | |
e1d675df | 448 | req->work.list.next = NULL; |
3474d1b9 | 449 | atomic_set(&req->work.flags, 0); |
feaadc4f | 450 | if (req->flags & REQ_F_FORCE_ASYNC) |
3474d1b9 | 451 | atomic_or(IO_WQ_WORK_CONCURRENT, &req->work.flags); |
feaadc4f | 452 | |
3beed235 | 453 | if (req->file && !(req->flags & REQ_F_FIXED_FILE)) |
8487f083 | 454 | req->flags |= io_file_get_flags(req->file); |
f6b543fd | 455 | |
8b1df11f | 456 | if (req->file && (req->flags & REQ_F_ISREG)) { |
d4755e15 JA |
457 | bool should_hash = def->hash_reg_file; |
458 | ||
459 | /* don't serialize this request if the fs doesn't need it */ | |
460 | if (should_hash && (req->file->f_flags & O_DIRECT) && | |
210a03c9 | 461 | (req->file->f_op->fop_flags & FOP_DIO_PARALLEL_WRITE)) |
d4755e15 JA |
462 | should_hash = false; |
463 | if (should_hash || (ctx->flags & IORING_SETUP_IOPOLL)) | |
1e6fa521 | 464 | io_wq_hash_work(&req->work, file_inode(req->file)); |
4b982bd0 | 465 | } else if (!req->file || !S_ISBLK(file_inode(req->file)->i_mode)) { |
1e6fa521 | 466 | if (def->unbound_nonreg_file) |
3474d1b9 | 467 | atomic_or(IO_WQ_WORK_UNBOUND, &req->work.flags); |
1e6fa521 | 468 | } |
561fb04a | 469 | } |
cccf0ee8 | 470 | |
cbdcb435 | 471 | static void io_prep_async_link(struct io_kiocb *req) |
561fb04a | 472 | { |
cbdcb435 | 473 | struct io_kiocb *cur; |
54a91f3b | 474 | |
44eff40a PB |
475 | if (req->flags & REQ_F_LINK_TIMEOUT) { |
476 | struct io_ring_ctx *ctx = req->ctx; | |
477 | ||
020b40f3 | 478 | raw_spin_lock_irq(&ctx->timeout_lock); |
44eff40a PB |
479 | io_for_each_link(cur, req) |
480 | io_prep_async_work(cur); | |
020b40f3 | 481 | raw_spin_unlock_irq(&ctx->timeout_lock); |
44eff40a PB |
482 | } else { |
483 | io_for_each_link(cur, req) | |
484 | io_prep_async_work(cur); | |
485 | } | |
561fb04a JA |
486 | } |
487 | ||
6e6b8c62 | 488 | static void io_queue_iowq(struct io_kiocb *req) |
561fb04a | 489 | { |
b6f58a3f | 490 | struct io_uring_task *tctx = req->tctx; |
561fb04a | 491 | |
3bfe6106 | 492 | BUG_ON(!tctx); |
dbd2ca93 PB |
493 | |
494 | if ((current->flags & PF_KTHREAD) || !tctx->io_wq) { | |
495 | io_req_task_queue_fail(req, -ECANCELED); | |
496 | return; | |
497 | } | |
561fb04a | 498 | |
cbdcb435 PB |
499 | /* init ->work of the whole link before punting */ |
500 | io_prep_async_link(req); | |
991468dc JA |
501 | |
502 | /* | |
503 | * Not expected to happen, but if we do have a bug where this _can_ | |
504 | * happen, catch it here and ensure the request is marked as | |
505 | * canceled. That will make io-wq go through the usual work cancel | |
506 | * procedure rather than attempt to run this request (or create a new | |
507 | * worker for it). | |
508 | */ | |
b6f58a3f | 509 | if (WARN_ON_ONCE(!same_thread_group(tctx->task, current))) |
3474d1b9 | 510 | atomic_or(IO_WQ_WORK_CANCEL, &req->work.flags); |
991468dc | 511 | |
48863ffd | 512 | trace_io_uring_queue_async_work(req, io_wq_is_hashed(&req->work)); |
ebf93667 | 513 | io_wq_enqueue(tctx->io_wq, &req->work); |
cbdcb435 PB |
514 | } |
515 | ||
bcf8a029 | 516 | static void io_req_queue_iowq_tw(struct io_kiocb *req, io_tw_token_t tw) |
6746ee4c PB |
517 | { |
518 | io_queue_iowq(req); | |
519 | } | |
520 | ||
521 | void io_req_queue_iowq(struct io_kiocb *req) | |
522 | { | |
523 | req->io_task_work.func = io_req_queue_iowq_tw; | |
524 | io_req_task_work_add(req); | |
525 | } | |
526 | ||
8fb7aee0 | 527 | static unsigned io_linked_nr(struct io_kiocb *req) |
fde04c7e | 528 | { |
8fb7aee0 PB |
529 | struct io_kiocb *tmp; |
530 | unsigned nr = 0; | |
fde04c7e | 531 | |
8fb7aee0 PB |
532 | io_for_each_link(tmp, req) |
533 | nr++; | |
534 | return nr; | |
fde04c7e PB |
535 | } |
536 | ||
d62c2f0d | 537 | static __cold noinline void io_queue_deferred(struct io_ring_ctx *ctx) |
de0617e4 | 538 | { |
fde04c7e PB |
539 | bool drain_seen = false, first = true; |
540 | ||
8fb7aee0 PB |
541 | lockdep_assert_held(&ctx->uring_lock); |
542 | __io_req_caches_free(ctx); | |
543 | ||
441b8a78 | 544 | while (!list_empty(&ctx->defer_list)) { |
27dc8338 PB |
545 | struct io_defer_entry *de = list_first_entry(&ctx->defer_list, |
546 | struct io_defer_entry, list); | |
de0617e4 | 547 | |
fde04c7e | 548 | drain_seen |= de->req->flags & REQ_F_IO_DRAIN; |
8fb7aee0 PB |
549 | if ((drain_seen || first) && ctx->nr_req_allocated != ctx->nr_drained) |
550 | return; | |
fde04c7e | 551 | |
27dc8338 | 552 | list_del_init(&de->list); |
8fb7aee0 | 553 | ctx->nr_drained -= io_linked_nr(de->req); |
907d1df3 | 554 | io_req_task_queue(de->req); |
27dc8338 | 555 | kfree(de); |
fde04c7e | 556 | first = false; |
441b8a78 | 557 | } |
04518945 PB |
558 | } |
559 | ||
a830ffd2 PB |
560 | void __io_commit_cqring_flush(struct io_ring_ctx *ctx) |
561 | { | |
bca39f39 PB |
562 | if (ctx->poll_activated) |
563 | io_poll_wq_wake(ctx); | |
e5f30f6f PB |
564 | if (ctx->off_timeout_used) |
565 | io_flush_timeouts(ctx); | |
a830ffd2 | 566 | if (ctx->has_evfd) |
62f666df | 567 | io_eventfd_signal(ctx, true); |
a830ffd2 PB |
568 | } |
569 | ||
f66f7342 | 570 | static inline void __io_cq_lock(struct io_ring_ctx *ctx) |
f66f7342 | 571 | { |
ec26c225 | 572 | if (!ctx->lockless_cq) |
f66f7342 PB |
573 | spin_lock(&ctx->completion_lock); |
574 | } | |
575 | ||
6971253f PB |
576 | static inline void io_cq_lock(struct io_ring_ctx *ctx) |
577 | __acquires(ctx->completion_lock) | |
578 | { | |
579 | spin_lock(&ctx->completion_lock); | |
580 | } | |
581 | ||
f66f7342 | 582 | static inline void __io_cq_unlock_post(struct io_ring_ctx *ctx) |
3181e22f PB |
583 | { |
584 | io_commit_cqring(ctx); | |
54927baf | 585 | if (!ctx->task_complete) { |
ec26c225 PB |
586 | if (!ctx->lockless_cq) |
587 | spin_unlock(&ctx->completion_lock); | |
588 | /* IOPOLL rings only need to wake up if it's also SQPOLL */ | |
589 | if (!ctx->syscall_iopoll) | |
590 | io_cqring_wake(ctx); | |
3181e22f | 591 | } |
54927baf | 592 | io_commit_cqring_flush(ctx); |
3181e22f PB |
593 | } |
594 | ||
0fdb9a19 | 595 | static void io_cq_unlock_post(struct io_ring_ctx *ctx) |
5d772916 | 596 | __releases(ctx->completion_lock) |
25399321 | 597 | { |
f66f7342 PB |
598 | io_commit_cqring(ctx); |
599 | spin_unlock(&ctx->completion_lock); | |
f66f7342 | 600 | io_cqring_wake(ctx); |
54927baf | 601 | io_commit_cqring_flush(ctx); |
25399321 PB |
602 | } |
603 | ||
6b231248 | 604 | static void __io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool dying) |
1d7bb1d5 | 605 | { |
e45a3e05 | 606 | size_t cqe_size = sizeof(struct io_uring_cqe); |
1d7bb1d5 | 607 | |
8d09a88e PB |
608 | lockdep_assert_held(&ctx->uring_lock); |
609 | ||
686b56cb JA |
610 | /* don't abort if we're dying, entries must get freed */ |
611 | if (!dying && __io_cqring_events(ctx) == ctx->cq_entries) | |
1b346e4a | 612 | return; |
1d7bb1d5 | 613 | |
e45a3e05 SR |
614 | if (ctx->flags & IORING_SETUP_CQE32) |
615 | cqe_size <<= 1; | |
616 | ||
25399321 | 617 | io_cq_lock(ctx); |
6c2450ae | 618 | while (!list_empty(&ctx->cq_overflow_list)) { |
59fbc409 | 619 | struct io_uring_cqe *cqe; |
6c2450ae | 620 | struct io_overflow_cqe *ocqe; |
e6c8aa9a | 621 | |
6c2450ae PB |
622 | ocqe = list_first_entry(&ctx->cq_overflow_list, |
623 | struct io_overflow_cqe, list); | |
6b231248 PB |
624 | |
625 | if (!dying) { | |
626 | if (!io_get_cqe_overflow(ctx, &cqe, true)) | |
627 | break; | |
628 | memcpy(cqe, &ocqe->cqe, cqe_size); | |
629 | } | |
6c2450ae PB |
630 | list_del(&ocqe->list); |
631 | kfree(ocqe); | |
eac2ca2d JA |
632 | |
633 | /* | |
634 | * For silly syzbot cases that deliberately overflow by huge | |
635 | * amounts, check if we need to resched and drop and | |
636 | * reacquire the locks if so. Nothing real would ever hit this. | |
637 | * Ideally we'd have a non-posting unlock for this, but hard | |
638 | * to care for a non-real case. | |
639 | */ | |
640 | if (need_resched()) { | |
a7d755ed | 641 | ctx->cqe_sentinel = ctx->cqe_cached; |
eac2ca2d JA |
642 | io_cq_unlock_post(ctx); |
643 | mutex_unlock(&ctx->uring_lock); | |
644 | cond_resched(); | |
645 | mutex_lock(&ctx->uring_lock); | |
646 | io_cq_lock(ctx); | |
647 | } | |
1d7bb1d5 JA |
648 | } |
649 | ||
1b346e4a | 650 | if (list_empty(&ctx->cq_overflow_list)) { |
10988a0a | 651 | clear_bit(IO_CHECK_CQ_OVERFLOW_BIT, &ctx->check_cq); |
3a4b89a2 | 652 | atomic_andnot(IORING_SQ_CQ_OVERFLOW, &ctx->rings->sq_flags); |
09e88404 | 653 | } |
25399321 | 654 | io_cq_unlock_post(ctx); |
1d7bb1d5 JA |
655 | } |
656 | ||
6b231248 | 657 | static void io_cqring_overflow_kill(struct io_ring_ctx *ctx) |
52ea806a | 658 | { |
6b231248 PB |
659 | if (ctx->rings) |
660 | __io_cqring_overflow_flush(ctx, true); | |
52ea806a JA |
661 | } |
662 | ||
52ea806a | 663 | static void io_cqring_do_overflow_flush(struct io_ring_ctx *ctx) |
6c503150 | 664 | { |
8d09a88e | 665 | mutex_lock(&ctx->uring_lock); |
6b231248 | 666 | __io_cqring_overflow_flush(ctx, false); |
8d09a88e | 667 | mutex_unlock(&ctx->uring_lock); |
6c503150 PB |
668 | } |
669 | ||
89800a2d | 670 | /* must to be called somewhat shortly after putting a request */ |
b6f58a3f | 671 | static inline void io_put_task(struct io_kiocb *req) |
89800a2d | 672 | { |
b6f58a3f | 673 | struct io_uring_task *tctx = req->tctx; |
6ed368cc | 674 | |
b6f58a3f | 675 | if (likely(tctx->task == current)) { |
6ed368cc JA |
676 | tctx->cached_refs++; |
677 | } else { | |
678 | percpu_counter_sub(&tctx->inflight, 1); | |
679 | if (unlikely(atomic_read(&tctx->in_cancel))) | |
680 | wake_up(&tctx->wait); | |
b6f58a3f | 681 | put_task_struct(tctx->task); |
6ed368cc | 682 | } |
89800a2d PB |
683 | } |
684 | ||
63809137 | 685 | void io_task_refs_refill(struct io_uring_task *tctx) |
9a10867a PB |
686 | { |
687 | unsigned int refill = -tctx->cached_refs + IO_TCTX_REFS_CACHE_NR; | |
688 | ||
689 | percpu_counter_add(&tctx->inflight, refill); | |
690 | refcount_add(refill, ¤t->usage); | |
691 | tctx->cached_refs += refill; | |
692 | } | |
693 | ||
3cc7fdb9 PB |
694 | static __cold void io_uring_drop_tctx_refs(struct task_struct *task) |
695 | { | |
696 | struct io_uring_task *tctx = task->io_uring; | |
697 | unsigned int refs = tctx->cached_refs; | |
698 | ||
699 | if (refs) { | |
700 | tctx->cached_refs = 0; | |
701 | percpu_counter_sub(&tctx->inflight, refs); | |
702 | put_task_struct_many(task, refs); | |
703 | } | |
704 | } | |
705 | ||
f660fd2c JA |
706 | static __cold bool io_cqring_add_overflow(struct io_ring_ctx *ctx, |
707 | struct io_overflow_cqe *ocqe) | |
2b188cc1 | 708 | { |
f26cc959 PB |
709 | lockdep_assert_held(&ctx->completion_lock); |
710 | ||
cce4b8b0 | 711 | if (!ocqe) { |
b0c8a640 PB |
712 | struct io_rings *r = ctx->rings; |
713 | ||
cce4b8b0 PB |
714 | /* |
715 | * If we're in ring overflow flush mode, or in task cancel mode, | |
716 | * or cannot allocate an overflow entry, then we need to drop it | |
717 | * on the floor. | |
718 | */ | |
b0c8a640 | 719 | WRITE_ONCE(r->cq_overflow, READ_ONCE(r->cq_overflow) + 1); |
155bc950 | 720 | set_bit(IO_CHECK_CQ_DROPPED_BIT, &ctx->check_cq); |
cce4b8b0 | 721 | return false; |
2b188cc1 | 722 | } |
cce4b8b0 | 723 | if (list_empty(&ctx->cq_overflow_list)) { |
10988a0a | 724 | set_bit(IO_CHECK_CQ_OVERFLOW_BIT, &ctx->check_cq); |
3a4b89a2 | 725 | atomic_or(IORING_SQ_CQ_OVERFLOW, &ctx->rings->sq_flags); |
20c0b380 | 726 | |
cce4b8b0 | 727 | } |
cce4b8b0 PB |
728 | list_add_tail(&ocqe->list, &ctx->cq_overflow_list); |
729 | return true; | |
2b188cc1 JA |
730 | } |
731 | ||
10f466ab | 732 | static struct io_overflow_cqe *io_alloc_ocqe(struct io_ring_ctx *ctx, |
c80bdb1c JA |
733 | struct io_cqe *cqe, |
734 | struct io_big_cqe *big_cqe, gfp_t gfp) | |
68494a65 | 735 | { |
10f466ab JA |
736 | struct io_overflow_cqe *ocqe; |
737 | size_t ocq_size = sizeof(struct io_overflow_cqe); | |
738 | bool is_cqe32 = (ctx->flags & IORING_SETUP_CQE32); | |
739 | ||
740 | if (is_cqe32) | |
741 | ocq_size += sizeof(struct io_uring_cqe); | |
742 | ||
c80bdb1c | 743 | ocqe = kzalloc(ocq_size, gfp | __GFP_ACCOUNT); |
072d37b5 | 744 | trace_io_uring_cqe_overflow(ctx, cqe->user_data, cqe->res, cqe->flags, ocqe); |
10f466ab | 745 | if (ocqe) { |
072d37b5 JA |
746 | ocqe->cqe.user_data = cqe->user_data; |
747 | ocqe->cqe.res = cqe->res; | |
748 | ocqe->cqe.flags = cqe->flags; | |
c80bdb1c JA |
749 | if (is_cqe32 && big_cqe) { |
750 | ocqe->cqe.big_cqe[0] = big_cqe->extra1; | |
751 | ocqe->cqe.big_cqe[1] = big_cqe->extra2; | |
10f466ab JA |
752 | } |
753 | } | |
c80bdb1c JA |
754 | if (big_cqe) |
755 | big_cqe->extra1 = big_cqe->extra2 = 0; | |
10f466ab | 756 | return ocqe; |
68494a65 PB |
757 | } |
758 | ||
faf88dde PB |
759 | /* |
760 | * writes to the cq entry need to come after reading head; the | |
761 | * control dependency is enough as we're using WRITE_ONCE to | |
762 | * fill the cq entry | |
763 | */ | |
20d6b633 | 764 | bool io_cqe_cache_refill(struct io_ring_ctx *ctx, bool overflow) |
faf88dde PB |
765 | { |
766 | struct io_rings *rings = ctx->rings; | |
767 | unsigned int off = ctx->cached_cq_tail & (ctx->cq_entries - 1); | |
faf88dde PB |
768 | unsigned int free, queued, len; |
769 | ||
aa1df3a3 PB |
770 | /* |
771 | * Posting into the CQ when there are pending overflowed CQEs may break | |
772 | * ordering guarantees, which will affect links, F_MORE users and more. | |
773 | * Force overflow the completion. | |
774 | */ | |
775 | if (!overflow && (ctx->check_cq & BIT(IO_CHECK_CQ_OVERFLOW_BIT))) | |
20d6b633 | 776 | return false; |
faf88dde PB |
777 | |
778 | /* userspace may cheat modifying the tail, be safe and do min */ | |
779 | queued = min(__io_cqring_events(ctx), ctx->cq_entries); | |
780 | free = ctx->cq_entries - queued; | |
781 | /* we need a contiguous range, limit based on the current array offset */ | |
782 | len = min(free, ctx->cq_entries - off); | |
783 | if (!len) | |
20d6b633 | 784 | return false; |
faf88dde | 785 | |
b3659a65 PB |
786 | if (ctx->flags & IORING_SETUP_CQE32) { |
787 | off <<= 1; | |
788 | len <<= 1; | |
789 | } | |
790 | ||
faf88dde PB |
791 | ctx->cqe_cached = &rings->cqes[off]; |
792 | ctx->cqe_sentinel = ctx->cqe_cached + len; | |
20d6b633 | 793 | return true; |
faf88dde PB |
794 | } |
795 | ||
f66f7342 PB |
796 | static bool io_fill_cqe_aux(struct io_ring_ctx *ctx, u64 user_data, s32 res, |
797 | u32 cflags) | |
bcda7baa | 798 | { |
cd94903d PB |
799 | struct io_uring_cqe *cqe; |
800 | ||
59fbc409 | 801 | if (likely(io_get_cqe(ctx, &cqe))) { |
cd94903d PB |
802 | WRITE_ONCE(cqe->user_data, user_data); |
803 | WRITE_ONCE(cqe->res, res); | |
804 | WRITE_ONCE(cqe->flags, cflags); | |
c5595975 PB |
805 | |
806 | if (ctx->flags & IORING_SETUP_CQE32) { | |
807 | WRITE_ONCE(cqe->big_cqe[0], 0); | |
808 | WRITE_ONCE(cqe->big_cqe[1], 0); | |
809 | } | |
2946f08a PB |
810 | |
811 | trace_io_uring_complete(ctx, NULL, cqe); | |
cd94903d PB |
812 | return true; |
813 | } | |
52120f0f | 814 | return false; |
bcda7baa JA |
815 | } |
816 | ||
072d37b5 JA |
817 | static inline struct io_cqe io_init_cqe(u64 user_data, s32 res, u32 cflags) |
818 | { | |
819 | return (struct io_cqe) { .user_data = user_data, .res = res, .flags = cflags }; | |
820 | } | |
821 | ||
f660fd2c JA |
822 | static __cold void io_cqe_overflow(struct io_ring_ctx *ctx, struct io_cqe *cqe, |
823 | struct io_big_cqe *big_cqe) | |
824 | { | |
825 | struct io_overflow_cqe *ocqe; | |
826 | ||
827 | ocqe = io_alloc_ocqe(ctx, cqe, big_cqe, GFP_KERNEL); | |
828 | spin_lock(&ctx->completion_lock); | |
829 | io_cqring_add_overflow(ctx, ocqe); | |
830 | spin_unlock(&ctx->completion_lock); | |
831 | } | |
832 | ||
833 | static __cold bool io_cqe_overflow_locked(struct io_ring_ctx *ctx, | |
834 | struct io_cqe *cqe, | |
835 | struct io_big_cqe *big_cqe) | |
836 | { | |
837 | struct io_overflow_cqe *ocqe; | |
838 | ||
839 | ocqe = io_alloc_ocqe(ctx, cqe, big_cqe, GFP_ATOMIC); | |
840 | return io_cqring_add_overflow(ctx, ocqe); | |
841 | } | |
842 | ||
4c76de42 | 843 | bool io_post_aux_cqe(struct io_ring_ctx *ctx, u64 user_data, s32 res, u32 cflags) |
d245bca6 PB |
844 | { |
845 | bool filled; | |
846 | ||
4c76de42 | 847 | io_cq_lock(ctx); |
f66f7342 | 848 | filled = io_fill_cqe_aux(ctx, user_data, res, cflags); |
10f466ab | 849 | if (unlikely(!filled)) { |
072d37b5 | 850 | struct io_cqe cqe = io_init_cqe(user_data, res, cflags); |
10f466ab | 851 | |
f660fd2c | 852 | filled = io_cqe_overflow_locked(ctx, &cqe, NULL); |
10f466ab | 853 | } |
25399321 | 854 | io_cq_unlock_post(ctx); |
d245bca6 PB |
855 | return filled; |
856 | } | |
857 | ||
f33096a3 JA |
858 | /* |
859 | * Must be called from inline task_work so we now a flush will happen later, | |
860 | * and obviously with ctx->uring_lock held (tw always has that). | |
861 | */ | |
862 | void io_add_aux_cqe(struct io_ring_ctx *ctx, u64 user_data, s32 res, u32 cflags) | |
863 | { | |
81a22c86 PB |
864 | lockdep_assert_held(&ctx->uring_lock); |
865 | lockdep_assert(ctx->lockless_cq); | |
866 | ||
3b7c16be | 867 | if (!io_fill_cqe_aux(ctx, user_data, res, cflags)) { |
072d37b5 | 868 | struct io_cqe cqe = io_init_cqe(user_data, res, cflags); |
10f466ab | 869 | |
f660fd2c | 870 | io_cqe_overflow(ctx, &cqe, NULL); |
3b7c16be | 871 | } |
f33096a3 JA |
872 | ctx->submit_state.cq_flush = true; |
873 | } | |
874 | ||
b6b2bb58 PB |
875 | /* |
876 | * A helper for multishot requests posting additional CQEs. | |
877 | * Should only be used from a task_work including IO_URING_F_MULTISHOT. | |
878 | */ | |
e5c12945 | 879 | bool io_req_post_cqe(struct io_kiocb *req, s32 res, u32 cflags) |
2b188cc1 | 880 | { |
d86eaed1 | 881 | struct io_ring_ctx *ctx = req->ctx; |
902ce82c | 882 | bool posted; |
9b8c5475 | 883 | |
687b2bae JA |
884 | /* |
885 | * If multishot has already posted deferred completions, ensure that | |
886 | * those are flushed first before posting this one. If not, CQEs | |
887 | * could get reordered. | |
888 | */ | |
889 | if (!wq_list_empty(&ctx->submit_state.compl_reqs)) | |
890 | __io_submit_flush_completions(ctx); | |
891 | ||
e0e4ab52 | 892 | lockdep_assert(!io_wq_current_is_worker()); |
9b8c5475 DY |
893 | lockdep_assert_held(&ctx->uring_lock); |
894 | ||
5e16f1a6 PB |
895 | if (!ctx->lockless_cq) { |
896 | spin_lock(&ctx->completion_lock); | |
897 | posted = io_fill_cqe_aux(ctx, req->cqe.user_data, res, cflags); | |
898 | spin_unlock(&ctx->completion_lock); | |
899 | } else { | |
900 | posted = io_fill_cqe_aux(ctx, req->cqe.user_data, res, cflags); | |
901 | } | |
902 | ||
902ce82c | 903 | ctx->submit_state.cq_flush = true; |
902ce82c | 904 | return posted; |
9b8c5475 DY |
905 | } |
906 | ||
0667db14 | 907 | static void io_req_complete_post(struct io_kiocb *req, unsigned issue_flags) |
2b188cc1 | 908 | { |
fa18fa22 | 909 | struct io_ring_ctx *ctx = req->ctx; |
3afcb3b2 | 910 | bool completed = true; |
fa18fa22 | 911 | |
de96e9ae PB |
912 | /* |
913 | * All execution paths but io-wq use the deferred completions by | |
914 | * passing IO_URING_F_COMPLETE_DEFER and thus should not end up here. | |
915 | */ | |
916 | if (WARN_ON_ONCE(!(issue_flags & IO_URING_F_IOWQ))) | |
917 | return; | |
918 | ||
0667db14 PB |
919 | /* |
920 | * Handle special CQ sync cases via task_work. DEFER_TASKRUN requires | |
921 | * the submitter task context, IOPOLL protects with uring_lock. | |
922 | */ | |
3f0cb8de | 923 | if (ctx->lockless_cq || (req->flags & REQ_F_REISSUE)) { |
3afcb3b2 | 924 | defer_complete: |
0667db14 PB |
925 | req->io_task_work.func = io_req_task_complete; |
926 | io_req_task_work_add(req); | |
927 | return; | |
928 | } | |
fa18fa22 PB |
929 | |
930 | io_cq_lock(ctx); | |
3afcb3b2 PB |
931 | if (!(req->flags & REQ_F_CQE_SKIP)) |
932 | completed = io_fill_cqe_req(ctx, req); | |
25399321 | 933 | io_cq_unlock_post(ctx); |
fa18fa22 | 934 | |
3afcb3b2 PB |
935 | if (!completed) |
936 | goto defer_complete; | |
937 | ||
c7dae4ba | 938 | /* |
de96e9ae PB |
939 | * We don't free the request here because we know it's called from |
940 | * io-wq only, which holds a reference, so it cannot be the last put. | |
c7dae4ba | 941 | */ |
f3913000 | 942 | req_ref_put(req); |
0ddf92e8 JA |
943 | } |
944 | ||
973fc83f | 945 | void io_req_defer_failed(struct io_kiocb *req, s32 res) |
e276ae34 | 946 | __must_hold(&ctx->uring_lock) |
f41db273 | 947 | { |
f30bd4d0 | 948 | const struct io_cold_def *def = &io_cold_defs[req->opcode]; |
a47b255e | 949 | |
e276ae34 PB |
950 | lockdep_assert_held(&req->ctx->uring_lock); |
951 | ||
93d2bcd2 | 952 | req_set_fail(req); |
6733e678 | 953 | io_req_set_res(req, res, io_put_kbuf(req, res, IO_URING_F_UNLOCKED)); |
a47b255e PB |
954 | if (def->fail) |
955 | def->fail(req); | |
973fc83f | 956 | io_req_complete_defer(req); |
f41db273 PB |
957 | } |
958 | ||
5d5901a3 PB |
959 | /* |
960 | * A request might get retired back into the request caches even before opcode | |
961 | * handlers and io_issue_sqe() are done with it, e.g. inline completion path. | |
962 | * Because of that, io_alloc_req() should be called only under ->uring_lock | |
963 | * and with extra caution to not get a request that is still worked on. | |
964 | */ | |
bd1a3783 | 965 | __cold bool __io_alloc_req_refill(struct io_ring_ctx *ctx) |
5d5901a3 | 966 | __must_hold(&ctx->uring_lock) |
2b188cc1 | 967 | { |
9c2ff3f9 | 968 | gfp_t gfp = GFP_KERNEL | __GFP_NOWARN | __GFP_ZERO; |
3ab665b7 | 969 | void *reqs[IO_REQ_ALLOC_BATCH]; |
05eb5fe2 | 970 | int ret; |
e5d1bc0a | 971 | |
3ab665b7 | 972 | ret = kmem_cache_alloc_bulk(req_cachep, gfp, ARRAY_SIZE(reqs), reqs); |
fd6fab2c | 973 | |
864ea921 PB |
974 | /* |
975 | * Bulk alloc is all-or-nothing. If we fail to get a batch, | |
976 | * retry single alloc to be on the safe side. | |
977 | */ | |
978 | if (unlikely(ret <= 0)) { | |
3ab665b7 PB |
979 | reqs[0] = kmem_cache_alloc(req_cachep, gfp); |
980 | if (!reqs[0]) | |
a33ae9ce | 981 | return false; |
864ea921 | 982 | ret = 1; |
2b188cc1 | 983 | } |
864ea921 | 984 | |
37f0e767 | 985 | percpu_ref_get_many(&ctx->refs, ret); |
63de899c PB |
986 | ctx->nr_req_allocated += ret; |
987 | ||
05eb5fe2 JA |
988 | while (ret--) { |
989 | struct io_kiocb *req = reqs[ret]; | |
3ab665b7 | 990 | |
fa05457a | 991 | io_req_add_to_cache(req, ctx); |
3ab665b7 | 992 | } |
a33ae9ce PB |
993 | return true; |
994 | } | |
995 | ||
03adabe8 PB |
996 | __cold void io_free_req(struct io_kiocb *req) |
997 | { | |
6ec9afc7 PB |
998 | /* refs were already put, restore them for io_req_task_complete() */ |
999 | req->flags &= ~REQ_F_REFCOUNT; | |
1000 | /* we only want to free it, don't post CQEs */ | |
1001 | req->flags |= REQ_F_CQE_SKIP; | |
1002 | req->io_task_work.func = io_req_task_complete; | |
03adabe8 PB |
1003 | io_req_task_work_add(req); |
1004 | } | |
1005 | ||
d81499bf PB |
1006 | static void __io_req_find_next_prep(struct io_kiocb *req) |
1007 | { | |
1008 | struct io_ring_ctx *ctx = req->ctx; | |
d81499bf | 1009 | |
6971253f | 1010 | spin_lock(&ctx->completion_lock); |
305bef98 | 1011 | io_disarm_next(req); |
6971253f | 1012 | spin_unlock(&ctx->completion_lock); |
d81499bf PB |
1013 | } |
1014 | ||
1015 | static inline struct io_kiocb *io_req_find_next(struct io_kiocb *req) | |
c69f8dbe | 1016 | { |
33cc89a9 | 1017 | struct io_kiocb *nxt; |
944e58bf | 1018 | |
9e645e11 JA |
1019 | /* |
1020 | * If LINK is set, we have dependent requests in this chain. If we | |
1021 | * didn't fail this request, queue the first one up, moving any other | |
1022 | * dependencies to the next request. In case of failure, fail the rest | |
1023 | * of the chain. | |
1024 | */ | |
d81499bf PB |
1025 | if (unlikely(req->flags & IO_DISARM_MASK)) |
1026 | __io_req_find_next_prep(req); | |
33cc89a9 PB |
1027 | nxt = req->link; |
1028 | req->link = NULL; | |
1029 | return nxt; | |
4d7dd462 | 1030 | } |
9e645e11 | 1031 | |
bcf8a029 | 1032 | static void ctx_flush_and_put(struct io_ring_ctx *ctx, io_tw_token_t tw) |
2c32395d PB |
1033 | { |
1034 | if (!ctx) | |
1035 | return; | |
ef060ea9 JA |
1036 | if (ctx->flags & IORING_SETUP_TASKRUN_FLAG) |
1037 | atomic_andnot(IORING_SQ_TASKRUN, &ctx->rings->sq_flags); | |
8e5b3b89 PB |
1038 | |
1039 | io_submit_flush_completions(ctx); | |
1040 | mutex_unlock(&ctx->uring_lock); | |
2c32395d PB |
1041 | percpu_ref_put(&ctx->refs); |
1042 | } | |
1043 | ||
af5d68f8 JA |
1044 | /* |
1045 | * Run queued task_work, returning the number of entries processed in *count. | |
1046 | * If more entries than max_entries are available, stop processing once this | |
1047 | * is reached and return the rest of the list. | |
1048 | */ | |
1049 | struct llist_node *io_handle_tw_list(struct llist_node *node, | |
1050 | unsigned int *count, | |
1051 | unsigned int max_entries) | |
9f8d032a | 1052 | { |
42c0905f JA |
1053 | struct io_ring_ctx *ctx = NULL; |
1054 | struct io_tw_state ts = { }; | |
c6dd763c | 1055 | |
592b4805 | 1056 | do { |
f88262e6 | 1057 | struct llist_node *next = node->next; |
9f8d032a HX |
1058 | struct io_kiocb *req = container_of(node, struct io_kiocb, |
1059 | io_task_work.node); | |
1060 | ||
42c0905f | 1061 | if (req->ctx != ctx) { |
94a4274b | 1062 | ctx_flush_and_put(ctx, ts); |
42c0905f | 1063 | ctx = req->ctx; |
92219afb | 1064 | mutex_lock(&ctx->uring_lock); |
42c0905f | 1065 | percpu_ref_get(&ctx->refs); |
13bfa6f1 | 1066 | } |
c92fcfc2 JA |
1067 | INDIRECT_CALL_2(req->io_task_work.func, |
1068 | io_poll_task_func, io_req_rw_complete, | |
94a4274b | 1069 | req, ts); |
9f8d032a | 1070 | node = next; |
2708af1a | 1071 | (*count)++; |
f5868008 | 1072 | if (unlikely(need_resched())) { |
94a4274b | 1073 | ctx_flush_and_put(ctx, ts); |
42c0905f | 1074 | ctx = NULL; |
f5868008 JA |
1075 | cond_resched(); |
1076 | } | |
af5d68f8 | 1077 | } while (node && *count < max_entries); |
c6dd763c | 1078 | |
94a4274b | 1079 | ctx_flush_and_put(ctx, ts); |
af5d68f8 | 1080 | return node; |
9f8d032a HX |
1081 | } |
1082 | ||
af0a2ffe | 1083 | static __cold void __io_fallback_tw(struct llist_node *node, bool sync) |
10e1c0d5 | 1084 | { |
dfbe5561 | 1085 | struct io_ring_ctx *last_ctx = NULL; |
10e1c0d5 JA |
1086 | struct io_kiocb *req; |
1087 | ||
1088 | while (node) { | |
1089 | req = container_of(node, struct io_kiocb, io_task_work.node); | |
1090 | node = node->next; | |
edd43f4d | 1091 | if (last_ctx != req->ctx) { |
dfbe5561 | 1092 | if (last_ctx) { |
edd43f4d JA |
1093 | if (sync) |
1094 | flush_delayed_work(&last_ctx->fallback_work); | |
dfbe5561 JA |
1095 | percpu_ref_put(&last_ctx->refs); |
1096 | } | |
1097 | last_ctx = req->ctx; | |
1098 | percpu_ref_get(&last_ctx->refs); | |
1099 | } | |
edd43f4d JA |
1100 | if (llist_add(&req->io_task_work.node, &last_ctx->fallback_llist)) |
1101 | schedule_delayed_work(&last_ctx->fallback_work, 1); | |
10e1c0d5 | 1102 | } |
dfbe5561 JA |
1103 | |
1104 | if (last_ctx) { | |
edd43f4d JA |
1105 | if (sync) |
1106 | flush_delayed_work(&last_ctx->fallback_work); | |
dfbe5561 JA |
1107 | percpu_ref_put(&last_ctx->refs); |
1108 | } | |
10e1c0d5 JA |
1109 | } |
1110 | ||
af0a2ffe PB |
1111 | static void io_fallback_tw(struct io_uring_task *tctx, bool sync) |
1112 | { | |
1113 | struct llist_node *node = llist_del_all(&tctx->task_list); | |
1114 | ||
1115 | __io_fallback_tw(node, sync); | |
1116 | } | |
1117 | ||
af5d68f8 JA |
1118 | struct llist_node *tctx_task_work_run(struct io_uring_task *tctx, |
1119 | unsigned int max_entries, | |
1120 | unsigned int *count) | |
c40f6379 | 1121 | { |
77e443ab | 1122 | struct llist_node *node; |
3a0c037b | 1123 | |
77e443ab | 1124 | if (unlikely(current->flags & PF_EXITING)) { |
dfbe5561 | 1125 | io_fallback_tw(tctx, true); |
af5d68f8 | 1126 | return NULL; |
77e443ab | 1127 | } |
3a0c037b | 1128 | |
592b4805 | 1129 | node = llist_del_all(&tctx->task_list); |
af5d68f8 JA |
1130 | if (node) { |
1131 | node = llist_reverse_order(node); | |
1132 | node = io_handle_tw_list(node, count, max_entries); | |
1133 | } | |
3cc7fdb9 | 1134 | |
8d664282 JA |
1135 | /* relaxed read is enough as only the task itself sets ->in_cancel */ |
1136 | if (unlikely(atomic_read(&tctx->in_cancel))) | |
3cc7fdb9 | 1137 | io_uring_drop_tctx_refs(current); |
c6dd763c | 1138 | |
af5d68f8 JA |
1139 | trace_io_uring_task_work_run(tctx, *count); |
1140 | return node; | |
1141 | } | |
1142 | ||
1143 | void tctx_task_work(struct callback_head *cb) | |
1144 | { | |
1145 | struct io_uring_task *tctx; | |
1146 | struct llist_node *ret; | |
1147 | unsigned int count = 0; | |
1148 | ||
1149 | tctx = container_of(cb, struct io_uring_task, task_work); | |
1150 | ret = tctx_task_work_run(tctx, UINT_MAX, &count); | |
1151 | /* can't happen */ | |
1152 | WARN_ON_ONCE(ret); | |
7cbf1722 JA |
1153 | } |
1154 | ||
ea910678 | 1155 | static void io_req_local_work_add(struct io_kiocb *req, unsigned flags) |
c0e0d6ba | 1156 | { |
ea910678 | 1157 | struct io_ring_ctx *ctx = req->ctx; |
8751d154 | 1158 | unsigned nr_wait, nr_tw, nr_tw_prev; |
e8c40771 | 1159 | struct llist_node *head; |
c0e0d6ba | 1160 | |
b4bc35cf PB |
1161 | /* See comment above IO_CQ_WAKE_INIT */ |
1162 | BUILD_BUG_ON(IO_CQ_WAKE_FORCE <= IORING_MAX_CQ_ENTRIES); | |
c0e0d6ba | 1163 | |
b4bc35cf PB |
1164 | /* |
1165 | * We don't know how many reuqests is there in the link and whether | |
1166 | * they can even be queued lazily, fall back to non-lazy. | |
1167 | */ | |
0e893472 | 1168 | if (req->flags & IO_REQ_LINK_FLAGS) |
8751d154 | 1169 | flags &= ~IOU_F_TWQ_LAZY_WAKE; |
ce8e04f6 | 1170 | |
c3ac76f9 JA |
1171 | guard(rcu)(); |
1172 | ||
e8c40771 | 1173 | head = READ_ONCE(ctx->work_llist.first); |
51509400 | 1174 | do { |
8751d154 | 1175 | nr_tw_prev = 0; |
e8c40771 PB |
1176 | if (head) { |
1177 | struct io_kiocb *first_req = container_of(head, | |
8751d154 PB |
1178 | struct io_kiocb, |
1179 | io_task_work.node); | |
1180 | /* | |
1181 | * Might be executed at any moment, rely on | |
1182 | * SLAB_TYPESAFE_BY_RCU to keep it alive. | |
1183 | */ | |
1184 | nr_tw_prev = READ_ONCE(first_req->nr_tw); | |
1185 | } | |
b4bc35cf PB |
1186 | |
1187 | /* | |
1188 | * Theoretically, it can overflow, but that's fine as one of | |
1189 | * previous adds should've tried to wake the task. | |
1190 | */ | |
8751d154 | 1191 | nr_tw = nr_tw_prev + 1; |
8751d154 | 1192 | if (!(flags & IOU_F_TWQ_LAZY_WAKE)) |
b4bc35cf | 1193 | nr_tw = IO_CQ_WAKE_FORCE; |
8751d154 PB |
1194 | |
1195 | req->nr_tw = nr_tw; | |
e8c40771 PB |
1196 | req->io_task_work.node.next = head; |
1197 | } while (!try_cmpxchg(&ctx->work_llist.first, &head, | |
51509400 PB |
1198 | &req->io_task_work.node)); |
1199 | ||
d381099f PB |
1200 | /* |
1201 | * cmpxchg implies a full barrier, which pairs with the barrier | |
1202 | * in set_current_state() on the io_cqring_wait() side. It's used | |
1203 | * to ensure that either we see updated ->cq_wait_nr, or waiters | |
1204 | * going to sleep will observe the work added to the list, which | |
1205 | * is similar to the wait/wawke task state sync. | |
1206 | */ | |
1207 | ||
e8c40771 | 1208 | if (!head) { |
8751d154 PB |
1209 | if (ctx->flags & IORING_SETUP_TASKRUN_FLAG) |
1210 | atomic_or(IORING_SQ_TASKRUN, &ctx->rings->sq_flags); | |
1211 | if (ctx->has_evfd) | |
62f666df | 1212 | io_eventfd_signal(ctx, false); |
c0e0d6ba DY |
1213 | } |
1214 | ||
8751d154 | 1215 | nr_wait = atomic_read(&ctx->cq_wait_nr); |
b4bc35cf PB |
1216 | /* not enough or no one is waiting */ |
1217 | if (nr_tw < nr_wait) | |
8751d154 | 1218 | return; |
b4bc35cf PB |
1219 | /* the previous add has already woken it up */ |
1220 | if (nr_tw_prev >= nr_wait) | |
8751d154 | 1221 | return; |
8751d154 | 1222 | wake_up_state(ctx->submitter_task, TASK_INTERRUPTIBLE); |
c0e0d6ba DY |
1223 | } |
1224 | ||
91c7884a | 1225 | static void io_req_normal_work_add(struct io_kiocb *req) |
7cbf1722 | 1226 | { |
b6f58a3f | 1227 | struct io_uring_task *tctx = req->tctx; |
9f010507 | 1228 | struct io_ring_ctx *ctx = req->ctx; |
7cbf1722 | 1229 | |
7cbf1722 | 1230 | /* task_work already pending, we're done */ |
32d91f05 | 1231 | if (!llist_add(&req->io_task_work.node, &tctx->task_list)) |
e09ee510 | 1232 | return; |
7cbf1722 | 1233 | |
ef060ea9 JA |
1234 | if (ctx->flags & IORING_SETUP_TASKRUN_FLAG) |
1235 | atomic_or(IORING_SQ_TASKRUN, &ctx->rings->sq_flags); | |
1236 | ||
af5d68f8 | 1237 | /* SQPOLL doesn't need the task_work added, it'll run it itself */ |
78f9b61b | 1238 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
bd2703b4 | 1239 | __set_notify_signal(tctx->task); |
af5d68f8 | 1240 | return; |
78f9b61b | 1241 | } |
af5d68f8 | 1242 | |
b6f58a3f | 1243 | if (likely(!task_work_add(tctx->task, &tctx->task_work, ctx->notify_method))) |
e09ee510 | 1244 | return; |
2215bed9 | 1245 | |
dfbe5561 | 1246 | io_fallback_tw(tctx, false); |
c0e0d6ba DY |
1247 | } |
1248 | ||
91c7884a PB |
1249 | void __io_req_task_work_add(struct io_kiocb *req, unsigned flags) |
1250 | { | |
c3ac76f9 | 1251 | if (req->ctx->flags & IORING_SETUP_DEFER_TASKRUN) |
ea910678 | 1252 | io_req_local_work_add(req, flags); |
c3ac76f9 | 1253 | else |
91c7884a | 1254 | io_req_normal_work_add(req); |
c3ac76f9 JA |
1255 | } |
1256 | ||
ea910678 | 1257 | void io_req_task_work_add_remote(struct io_kiocb *req, unsigned flags) |
c3ac76f9 | 1258 | { |
ea910678 | 1259 | if (WARN_ON_ONCE(!(req->ctx->flags & IORING_SETUP_DEFER_TASKRUN))) |
c3ac76f9 | 1260 | return; |
ea910678 | 1261 | __io_req_task_work_add(req, flags); |
91c7884a PB |
1262 | } |
1263 | ||
c0e0d6ba DY |
1264 | static void __cold io_move_task_work_from_local(struct io_ring_ctx *ctx) |
1265 | { | |
af0a2ffe | 1266 | struct llist_node *node = llist_del_all(&ctx->work_llist); |
c0e0d6ba | 1267 | |
af0a2ffe | 1268 | __io_fallback_tw(node, false); |
f46b9cdb DW |
1269 | node = llist_del_all(&ctx->retry_llist); |
1270 | __io_fallback_tw(node, false); | |
c0e0d6ba DY |
1271 | } |
1272 | ||
9fe3eaea JA |
1273 | static bool io_run_local_work_continue(struct io_ring_ctx *ctx, int events, |
1274 | int min_events) | |
1275 | { | |
40cfe553 | 1276 | if (!io_local_work_pending(ctx)) |
9fe3eaea JA |
1277 | return false; |
1278 | if (events < min_events) | |
1279 | return true; | |
1280 | if (ctx->flags & IORING_SETUP_TASKRUN_FLAG) | |
1281 | atomic_or(IORING_SQ_TASKRUN, &ctx->rings->sq_flags); | |
1282 | return false; | |
1283 | } | |
1284 | ||
f46b9cdb | 1285 | static int __io_run_local_work_loop(struct llist_node **node, |
bcf8a029 | 1286 | io_tw_token_t tw, |
f46b9cdb DW |
1287 | int events) |
1288 | { | |
49c5c63d JA |
1289 | int ret = 0; |
1290 | ||
f46b9cdb DW |
1291 | while (*node) { |
1292 | struct llist_node *next = (*node)->next; | |
1293 | struct io_kiocb *req = container_of(*node, struct io_kiocb, | |
1294 | io_task_work.node); | |
1295 | INDIRECT_CALL_2(req->io_task_work.func, | |
1296 | io_poll_task_func, io_req_rw_complete, | |
bcf8a029 | 1297 | req, tw); |
f46b9cdb | 1298 | *node = next; |
49c5c63d | 1299 | if (++ret >= events) |
f46b9cdb DW |
1300 | break; |
1301 | } | |
1302 | ||
49c5c63d | 1303 | return ret; |
f46b9cdb DW |
1304 | } |
1305 | ||
bcf8a029 | 1306 | static int __io_run_local_work(struct io_ring_ctx *ctx, io_tw_token_t tw, |
49c5c63d | 1307 | int min_events, int max_events) |
c0e0d6ba | 1308 | { |
c0e0d6ba | 1309 | struct llist_node *node; |
c3f4d39e | 1310 | unsigned int loops = 0; |
140102ae | 1311 | int ret = 0; |
c0e0d6ba | 1312 | |
140102ae | 1313 | if (WARN_ON_ONCE(ctx->submitter_task != current)) |
c0e0d6ba | 1314 | return -EEXIST; |
c3f4d39e PB |
1315 | if (ctx->flags & IORING_SETUP_TASKRUN_FLAG) |
1316 | atomic_andnot(IORING_SQ_TASKRUN, &ctx->rings->sq_flags); | |
c0e0d6ba | 1317 | again: |
49c5c63d | 1318 | min_events -= ret; |
bcf8a029 | 1319 | ret = __io_run_local_work_loop(&ctx->retry_llist.first, tw, max_events); |
f46b9cdb DW |
1320 | if (ctx->retry_llist.first) |
1321 | goto retry_done; | |
1322 | ||
3af0356c JA |
1323 | /* |
1324 | * llists are in reverse order, flip it back the right way before | |
1325 | * running the pending items. | |
1326 | */ | |
1e6e7602 | 1327 | node = llist_reverse_order(llist_del_all(&ctx->work_llist)); |
bcf8a029 | 1328 | ret += __io_run_local_work_loop(&node, tw, max_events - ret); |
f46b9cdb | 1329 | ctx->retry_llist.first = node; |
c3f4d39e | 1330 | loops++; |
c0e0d6ba | 1331 | |
9fe3eaea | 1332 | if (io_run_local_work_continue(ctx, ret, min_events)) |
c0e0d6ba | 1333 | goto again; |
f46b9cdb | 1334 | retry_done: |
92219afb PB |
1335 | io_submit_flush_completions(ctx); |
1336 | if (io_run_local_work_continue(ctx, ret, min_events)) | |
1337 | goto again; | |
9fe3eaea | 1338 | |
f75d5036 | 1339 | trace_io_uring_local_work_run(ctx, ret, loops); |
c0e0d6ba | 1340 | return ret; |
8ac5d85a JA |
1341 | } |
1342 | ||
9fe3eaea JA |
1343 | static inline int io_run_local_work_locked(struct io_ring_ctx *ctx, |
1344 | int min_events) | |
360173ab | 1345 | { |
8e5b3b89 | 1346 | struct io_tw_state ts = {}; |
360173ab | 1347 | |
40cfe553 | 1348 | if (!io_local_work_pending(ctx)) |
360173ab | 1349 | return 0; |
94a4274b | 1350 | return __io_run_local_work(ctx, ts, min_events, |
49c5c63d | 1351 | max(IO_LOCAL_TW_DEFAULT_MAX, min_events)); |
360173ab PB |
1352 | } |
1353 | ||
49c5c63d JA |
1354 | static int io_run_local_work(struct io_ring_ctx *ctx, int min_events, |
1355 | int max_events) | |
8ac5d85a | 1356 | { |
a282967c | 1357 | struct io_tw_state ts = {}; |
8ac5d85a JA |
1358 | int ret; |
1359 | ||
92219afb | 1360 | mutex_lock(&ctx->uring_lock); |
94a4274b | 1361 | ret = __io_run_local_work(ctx, ts, min_events, max_events); |
92219afb | 1362 | mutex_unlock(&ctx->uring_lock); |
8ac5d85a | 1363 | return ret; |
c0e0d6ba DY |
1364 | } |
1365 | ||
bcf8a029 | 1366 | static void io_req_task_cancel(struct io_kiocb *req, io_tw_token_t tw) |
c40f6379 | 1367 | { |
bcf8a029 | 1368 | io_tw_lock(req->ctx, tw); |
973fc83f | 1369 | io_req_defer_failed(req, req->cqe.res); |
c40f6379 JA |
1370 | } |
1371 | ||
bcf8a029 | 1372 | void io_req_task_submit(struct io_kiocb *req, io_tw_token_t tw) |
c40f6379 | 1373 | { |
bcf8a029 | 1374 | io_tw_lock(req->ctx, tw); |
b6f58a3f | 1375 | if (unlikely(io_should_terminate_tw())) |
973fc83f | 1376 | io_req_defer_failed(req, -EFAULT); |
6bb30855 | 1377 | else if (req->flags & REQ_F_FORCE_ASYNC) |
6e6b8c62 | 1378 | io_queue_iowq(req); |
6bb30855 DY |
1379 | else |
1380 | io_queue_sqe(req); | |
c40f6379 JA |
1381 | } |
1382 | ||
59915143 | 1383 | void io_req_task_queue_fail(struct io_kiocb *req, int ret) |
c40f6379 | 1384 | { |
97b388d7 | 1385 | io_req_set_res(req, ret, 0); |
5b0a6acc | 1386 | req->io_task_work.func = io_req_task_cancel; |
3fe07bcd | 1387 | io_req_task_work_add(req); |
c40f6379 JA |
1388 | } |
1389 | ||
f3b44f92 | 1390 | void io_req_task_queue(struct io_kiocb *req) |
a3df7698 | 1391 | { |
5b0a6acc | 1392 | req->io_task_work.func = io_req_task_submit; |
3fe07bcd | 1393 | io_req_task_work_add(req); |
a3df7698 PB |
1394 | } |
1395 | ||
59915143 | 1396 | void io_queue_next(struct io_kiocb *req) |
c69f8dbe | 1397 | { |
9b5f7bd9 | 1398 | struct io_kiocb *nxt = io_req_find_next(req); |
944e58bf PB |
1399 | |
1400 | if (nxt) | |
906a8c3f | 1401 | io_req_task_queue(nxt); |
c69f8dbe JL |
1402 | } |
1403 | ||
35adea1d PB |
1404 | static inline void io_req_put_rsrc_nodes(struct io_kiocb *req) |
1405 | { | |
1406 | if (req->file_node) { | |
1407 | io_put_rsrc_node(req->ctx, req->file_node); | |
1408 | req->file_node = NULL; | |
1409 | } | |
1410 | if (req->flags & REQ_F_BUF_NODE) | |
1411 | io_put_rsrc_node(req->ctx, req->buf_node); | |
1412 | } | |
1413 | ||
ec26c225 PB |
1414 | static void io_free_batch_list(struct io_ring_ctx *ctx, |
1415 | struct io_wq_work_node *node) | |
3aa83bfb | 1416 | __must_hold(&ctx->uring_lock) |
5af1d13e | 1417 | { |
3aa83bfb PB |
1418 | do { |
1419 | struct io_kiocb *req = container_of(node, struct io_kiocb, | |
1420 | comp_list); | |
2d6500d4 | 1421 | |
a538be5b | 1422 | if (unlikely(req->flags & IO_REQ_CLEAN_SLOW_FLAGS)) { |
d803d123 JA |
1423 | if (req->flags & REQ_F_REISSUE) { |
1424 | node = req->comp_list.next; | |
1425 | req->flags &= ~REQ_F_REISSUE; | |
1426 | io_queue_iowq(req); | |
1427 | continue; | |
1428 | } | |
a538be5b PB |
1429 | if (req->flags & REQ_F_REFCOUNT) { |
1430 | node = req->comp_list.next; | |
1431 | if (!req_ref_put_and_test(req)) | |
1432 | continue; | |
1433 | } | |
b605a7fa PB |
1434 | if ((req->flags & REQ_F_POLLED) && req->apoll) { |
1435 | struct async_poll *apoll = req->apoll; | |
1436 | ||
1437 | if (apoll->double_poll) | |
1438 | kfree(apoll->double_poll); | |
0d83b8a9 | 1439 | io_cache_free(&ctx->apoll_cache, apoll); |
b605a7fa PB |
1440 | req->flags &= ~REQ_F_POLLED; |
1441 | } | |
da1a08c5 | 1442 | if (req->flags & IO_REQ_LINK_FLAGS) |
57859f4d | 1443 | io_queue_next(req); |
a538be5b PB |
1444 | if (unlikely(req->flags & IO_REQ_CLEAN_FLAGS)) |
1445 | io_clean_op(req); | |
c1e53a69 | 1446 | } |
17bc2837 | 1447 | io_put_file(req); |
7029acd8 | 1448 | io_req_put_rsrc_nodes(req); |
b6f58a3f | 1449 | io_put_task(req); |
c29006a2 | 1450 | |
c1e53a69 | 1451 | node = req->comp_list.next; |
fa05457a | 1452 | io_req_add_to_cache(req, ctx); |
3aa83bfb | 1453 | } while (node); |
7a743e22 PB |
1454 | } |
1455 | ||
ec26c225 | 1456 | void __io_submit_flush_completions(struct io_ring_ctx *ctx) |
a141dd89 | 1457 | __must_hold(&ctx->uring_lock) |
905c172f | 1458 | { |
cd0ca2e0 | 1459 | struct io_submit_state *state = &ctx->submit_state; |
fa780334 | 1460 | struct io_wq_work_node *node; |
905c172f | 1461 | |
f66f7342 | 1462 | __io_cq_lock(ctx); |
fa780334 | 1463 | __wq_list_for_each(node, &state->compl_reqs) { |
d9dee430 PB |
1464 | struct io_kiocb *req = container_of(node, struct io_kiocb, |
1465 | comp_list); | |
3d4aeb9f | 1466 | |
d803d123 JA |
1467 | /* |
1468 | * Requests marked with REQUEUE should not post a CQE, they | |
1469 | * will go through the io-wq retry machinery and post one | |
1470 | * later. | |
1471 | */ | |
1472 | if (!(req->flags & (REQ_F_CQE_SKIP | REQ_F_REISSUE)) && | |
00b0db56 | 1473 | unlikely(!io_fill_cqe_req(ctx, req))) { |
f660fd2c JA |
1474 | if (ctx->lockless_cq) |
1475 | io_cqe_overflow(ctx, &req->cqe, &req->big_cqe); | |
1476 | else | |
1477 | io_cqe_overflow_locked(ctx, &req->cqe, &req->big_cqe); | |
f66f7342 | 1478 | } |
905c172f | 1479 | } |
c98c81a4 | 1480 | __io_cq_unlock_post(ctx); |
d9dee430 | 1481 | |
f2a93294 | 1482 | if (!wq_list_empty(&state->compl_reqs)) { |
931147dd DY |
1483 | io_free_batch_list(ctx, state->compl_reqs.first); |
1484 | INIT_WQ_LIST(&state->compl_reqs); | |
1485 | } | |
8fb7aee0 PB |
1486 | |
1487 | if (unlikely(ctx->drain_active)) | |
1488 | io_queue_deferred(ctx); | |
1489 | ||
902ce82c | 1490 | ctx->submit_state.cq_flush = false; |
7a743e22 PB |
1491 | } |
1492 | ||
6c503150 | 1493 | static unsigned io_cqring_events(struct io_ring_ctx *ctx) |
a3a0e43f JA |
1494 | { |
1495 | /* See comment at the top of this file */ | |
1496 | smp_rmb(); | |
e23de15f | 1497 | return __io_cqring_events(ctx); |
a3a0e43f JA |
1498 | } |
1499 | ||
def596e9 JA |
1500 | /* |
1501 | * We can't just wait for polled events to come to us, we have to actively | |
1502 | * find and complete them. | |
1503 | */ | |
c072481d | 1504 | static __cold void io_iopoll_try_reap_events(struct io_ring_ctx *ctx) |
def596e9 JA |
1505 | { |
1506 | if (!(ctx->flags & IORING_SETUP_IOPOLL)) | |
1507 | return; | |
1508 | ||
1509 | mutex_lock(&ctx->uring_lock); | |
5eef4e87 | 1510 | while (!wq_list_empty(&ctx->iopoll_list)) { |
b2edc0a7 | 1511 | /* let it sleep and repeat later if can't complete a request */ |
5ba3c874 | 1512 | if (io_do_iopoll(ctx, true) == 0) |
b2edc0a7 | 1513 | break; |
08f5439f JA |
1514 | /* |
1515 | * Ensure we allow local-to-the-cpu processing to take place, | |
1516 | * in this case we need to ensure that we reap all events. | |
3fcee5a6 | 1517 | * Also let task_work, etc. to progress by releasing the mutex |
08f5439f | 1518 | */ |
3fcee5a6 PB |
1519 | if (need_resched()) { |
1520 | mutex_unlock(&ctx->uring_lock); | |
1521 | cond_resched(); | |
1522 | mutex_lock(&ctx->uring_lock); | |
1523 | } | |
def596e9 JA |
1524 | } |
1525 | mutex_unlock(&ctx->uring_lock); | |
b62e0efd JA |
1526 | |
1527 | if (ctx->flags & IORING_SETUP_DEFER_TASKRUN) | |
1528 | io_move_task_work_from_local(ctx); | |
def596e9 JA |
1529 | } |
1530 | ||
81661978 | 1531 | static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned int min_events) |
def596e9 | 1532 | { |
7668b92a | 1533 | unsigned int nr_events = 0; |
155bc950 | 1534 | unsigned long check_cq; |
500f9fba | 1535 | |
81661978 PB |
1536 | min_events = min(min_events, ctx->cq_entries); |
1537 | ||
8d09a88e PB |
1538 | lockdep_assert_held(&ctx->uring_lock); |
1539 | ||
76de6749 PB |
1540 | if (!io_allowed_run_tw(ctx)) |
1541 | return -EEXIST; | |
1542 | ||
3a08576b PB |
1543 | check_cq = READ_ONCE(ctx->check_cq); |
1544 | if (unlikely(check_cq)) { | |
1545 | if (check_cq & BIT(IO_CHECK_CQ_OVERFLOW_BIT)) | |
6b231248 | 1546 | __io_cqring_overflow_flush(ctx, false); |
3a08576b PB |
1547 | /* |
1548 | * Similarly do not spin if we have not informed the user of any | |
1549 | * dropped CQE. | |
1550 | */ | |
1551 | if (check_cq & BIT(IO_CHECK_CQ_DROPPED_BIT)) | |
1552 | return -EBADR; | |
1553 | } | |
f39c8a5b PB |
1554 | /* |
1555 | * Don't enter poll loop if we already have events pending. | |
1556 | * If we do, we can potentially be spinning for commands that | |
1557 | * already triggered a CQE (eg in error). | |
1558 | */ | |
f39c8a5b | 1559 | if (io_cqring_events(ctx)) |
d487b43c | 1560 | return 0; |
155bc950 | 1561 | |
def596e9 | 1562 | do { |
9e4bef2b JA |
1563 | int ret = 0; |
1564 | ||
500f9fba JA |
1565 | /* |
1566 | * If a submit got punted to a workqueue, we can have the | |
1567 | * application entering polling for a command before it gets | |
1568 | * issued. That app will hold the uring_lock for the duration | |
1569 | * of the poll right here, so we need to take a breather every | |
1570 | * now and then to ensure that the issue has a chance to add | |
1571 | * the poll to the issued list. Otherwise we can spin here | |
1572 | * forever, while the workqueue is stuck trying to acquire the | |
1573 | * very same mutex. | |
1574 | */ | |
dac6a0ea JA |
1575 | if (wq_list_empty(&ctx->iopoll_list) || |
1576 | io_task_work_pending(ctx)) { | |
8f487ef2 PB |
1577 | u32 tail = ctx->cached_cq_tail; |
1578 | ||
d73acd7a | 1579 | (void) io_run_local_work_locked(ctx, min_events); |
def596e9 | 1580 | |
dac6a0ea JA |
1581 | if (task_work_pending(current) || |
1582 | wq_list_empty(&ctx->iopoll_list)) { | |
dac6a0ea | 1583 | mutex_unlock(&ctx->uring_lock); |
9d54bd6a | 1584 | io_run_task_work(); |
dac6a0ea | 1585 | mutex_lock(&ctx->uring_lock); |
dac6a0ea | 1586 | } |
8f487ef2 PB |
1587 | /* some requests don't go through iopoll_list */ |
1588 | if (tail != ctx->cached_cq_tail || | |
5eef4e87 | 1589 | wq_list_empty(&ctx->iopoll_list)) |
e9979b36 | 1590 | break; |
500f9fba | 1591 | } |
d73acd7a | 1592 | ret = io_do_iopoll(ctx, !min_events); |
9e4bef2b JA |
1593 | if (unlikely(ret < 0)) |
1594 | return ret; | |
dc314886 PB |
1595 | |
1596 | if (task_sigpending(current)) | |
1597 | return -EINTR; | |
9e4bef2b | 1598 | if (need_resched()) |
5ba3c874 | 1599 | break; |
d487b43c | 1600 | |
5ba3c874 | 1601 | nr_events += ret; |
d73acd7a | 1602 | } while (nr_events < min_events); |
d487b43c | 1603 | |
9e4bef2b | 1604 | return 0; |
def596e9 | 1605 | } |
7012c815 | 1606 | |
bcf8a029 | 1607 | void io_req_task_complete(struct io_kiocb *req, io_tw_token_t tw) |
8ef12efe | 1608 | { |
8e5b3b89 | 1609 | io_req_complete_defer(req); |
8ef12efe JA |
1610 | } |
1611 | ||
def596e9 JA |
1612 | /* |
1613 | * After the iocb has been issued, it's safe to be found on the poll list. | |
1614 | * Adding the kiocb to the list AFTER submission ensures that we don't | |
f39c8a5b | 1615 | * find it from a io_do_iopoll() thread before the issuer is done |
def596e9 JA |
1616 | * accessing the kiocb cookie. |
1617 | */ | |
9882131c | 1618 | static void io_iopoll_req_issued(struct io_kiocb *req, unsigned int issue_flags) |
def596e9 JA |
1619 | { |
1620 | struct io_ring_ctx *ctx = req->ctx; | |
3b44b371 | 1621 | const bool needs_lock = issue_flags & IO_URING_F_UNLOCKED; |
cb3d8972 PB |
1622 | |
1623 | /* workqueue context doesn't hold uring_lock, grab it now */ | |
3b44b371 | 1624 | if (unlikely(needs_lock)) |
cb3d8972 | 1625 | mutex_lock(&ctx->uring_lock); |
def596e9 JA |
1626 | |
1627 | /* | |
1628 | * Track whether we have multiple files in our lists. This will impact | |
1629 | * how we do polling eventually, not spinning if we're on potentially | |
1630 | * different devices. | |
1631 | */ | |
5eef4e87 | 1632 | if (wq_list_empty(&ctx->iopoll_list)) { |
915b3dde HX |
1633 | ctx->poll_multi_queue = false; |
1634 | } else if (!ctx->poll_multi_queue) { | |
def596e9 JA |
1635 | struct io_kiocb *list_req; |
1636 | ||
5eef4e87 PB |
1637 | list_req = container_of(ctx->iopoll_list.first, struct io_kiocb, |
1638 | comp_list); | |
30da1b45 | 1639 | if (list_req->file != req->file) |
915b3dde | 1640 | ctx->poll_multi_queue = true; |
def596e9 JA |
1641 | } |
1642 | ||
1643 | /* | |
1644 | * For fast devices, IO may have already completed. If it has, add | |
1645 | * it to the front so we find it first. | |
1646 | */ | |
65a6543d | 1647 | if (READ_ONCE(req->iopoll_completed)) |
5eef4e87 | 1648 | wq_list_add_head(&req->comp_list, &ctx->iopoll_list); |
def596e9 | 1649 | else |
5eef4e87 | 1650 | wq_list_add_tail(&req->comp_list, &ctx->iopoll_list); |
bdcd3eab | 1651 | |
3b44b371 | 1652 | if (unlikely(needs_lock)) { |
cb3d8972 PB |
1653 | /* |
1654 | * If IORING_SETUP_SQPOLL is enabled, sqes are either handle | |
1655 | * in sq thread task context or in io worker task context. If | |
1656 | * current task context is sq thread, we don't need to check | |
1657 | * whether should wake up sq thread. | |
1658 | */ | |
1659 | if ((ctx->flags & IORING_SETUP_SQPOLL) && | |
1660 | wq_has_sleeper(&ctx->sq_data->wait)) | |
1661 | wake_up(&ctx->sq_data->wait); | |
1662 | ||
1663 | mutex_unlock(&ctx->uring_lock); | |
1664 | } | |
def596e9 JA |
1665 | } |
1666 | ||
4bcb982c | 1667 | io_req_flags_t io_file_get_flags(struct file *file) |
88459b50 | 1668 | { |
6f11adcc | 1669 | struct inode *inode = file_inode(file); |
4bcb982c | 1670 | io_req_flags_t res = 0; |
af197f50 | 1671 | |
697b2876 PB |
1672 | BUILD_BUG_ON(REQ_F_ISREG_BIT != REQ_F_SUPPORT_NOWAIT_BIT + 1); |
1673 | ||
6f11adcc | 1674 | if (S_ISREG(inode->i_mode) && !(inode->i_flags & S_ANON_INODE)) |
8487f083 | 1675 | res |= REQ_F_ISREG; |
b9a6c945 | 1676 | if ((file->f_flags & O_NONBLOCK) || (file->f_mode & FMODE_NOWAIT)) |
8487f083 | 1677 | res |= REQ_F_SUPPORT_NOWAIT; |
88459b50 | 1678 | return res; |
2b188cc1 JA |
1679 | } |
1680 | ||
c072481d | 1681 | static __cold void io_drain_req(struct io_kiocb *req) |
e276ae34 | 1682 | __must_hold(&ctx->uring_lock) |
de0617e4 | 1683 | { |
a197f664 | 1684 | struct io_ring_ctx *ctx = req->ctx; |
19a94da4 | 1685 | bool drain = req->flags & IOSQE_IO_DRAIN; |
27dc8338 | 1686 | struct io_defer_entry *de; |
9cf7c104 | 1687 | |
f979c205 | 1688 | de = kmalloc(sizeof(*de), GFP_KERNEL_ACCOUNT); |
76cc33d7 | 1689 | if (!de) { |
05b33411 | 1690 | io_req_defer_failed(req, -ENOMEM); |
ef5c600a | 1691 | return; |
76cc33d7 | 1692 | } |
2d28390a | 1693 | |
19a94da4 | 1694 | io_prep_async_link(req); |
48863ffd | 1695 | trace_io_uring_defer(req); |
27dc8338 | 1696 | de->req = req; |
19a94da4 | 1697 | |
8fb7aee0 | 1698 | ctx->nr_drained += io_linked_nr(req); |
27dc8338 | 1699 | list_add_tail(&de->list, &ctx->defer_list); |
8fb7aee0 PB |
1700 | io_queue_deferred(ctx); |
1701 | if (!drain && list_empty(&ctx->defer_list)) | |
1702 | ctx->drain_active = false; | |
de0617e4 JA |
1703 | } |
1704 | ||
f4992544 JA |
1705 | static bool io_assign_file(struct io_kiocb *req, const struct io_issue_def *def, |
1706 | unsigned int issue_flags) | |
6bf9c47a | 1707 | { |
f4992544 | 1708 | if (req->file || !def->needs_file) |
6bf9c47a JA |
1709 | return true; |
1710 | ||
1711 | if (req->flags & REQ_F_FIXED_FILE) | |
cef216fc | 1712 | req->file = io_file_get_fixed(req, req->cqe.fd, issue_flags); |
6bf9c47a | 1713 | else |
cef216fc | 1714 | req->file = io_file_get_normal(req, req->cqe.fd); |
6bf9c47a | 1715 | |
772f5e00 | 1716 | return !!req->file; |
6bf9c47a JA |
1717 | } |
1718 | ||
b53e5232 JA |
1719 | #define REQ_ISSUE_SLOW_FLAGS (REQ_F_CREDS | REQ_F_ARM_LTIMEOUT) |
1720 | ||
c457eed5 PB |
1721 | static inline int __io_issue_sqe(struct io_kiocb *req, |
1722 | unsigned int issue_flags, | |
1723 | const struct io_issue_def *def) | |
2b188cc1 | 1724 | { |
5730b27e | 1725 | const struct cred *creds = NULL; |
b53e5232 | 1726 | struct io_kiocb *link = NULL; |
d625c6ee | 1727 | int ret; |
2b188cc1 | 1728 | |
b53e5232 JA |
1729 | if (unlikely(req->flags & REQ_ISSUE_SLOW_FLAGS)) { |
1730 | if ((req->flags & REQ_F_CREDS) && req->creds != current_cred()) | |
1731 | creds = override_creds(req->creds); | |
1732 | if (req->flags & REQ_F_ARM_LTIMEOUT) | |
1733 | link = __io_prep_linked_timeout(req); | |
1734 | } | |
5730b27e | 1735 | |
fcde59fe | 1736 | if (!def->audit_skip) |
5bd2182d PM |
1737 | audit_uring_entry(req->opcode); |
1738 | ||
0702e536 | 1739 | ret = def->issue(req, issue_flags); |
2b188cc1 | 1740 | |
fcde59fe | 1741 | if (!def->audit_skip) |
5bd2182d PM |
1742 | audit_uring_exit(!ret, ret); |
1743 | ||
b53e5232 JA |
1744 | if (unlikely(creds || link)) { |
1745 | if (creds) | |
1746 | revert_creds(creds); | |
1747 | if (link) | |
1748 | io_queue_linked_timeout(link); | |
1749 | } | |
97b388d7 | 1750 | |
c457eed5 PB |
1751 | return ret; |
1752 | } | |
1753 | ||
1754 | static int io_issue_sqe(struct io_kiocb *req, unsigned int issue_flags) | |
1755 | { | |
1756 | const struct io_issue_def *def = &io_issue_defs[req->opcode]; | |
1757 | int ret; | |
1758 | ||
1759 | if (unlikely(!io_assign_file(req, def, issue_flags))) | |
1760 | return -EBADF; | |
1761 | ||
1762 | ret = __io_issue_sqe(req, issue_flags, def); | |
1763 | ||
8bb9d6cc | 1764 | if (ret == IOU_COMPLETE) { |
75d7b3ae | 1765 | if (issue_flags & IO_URING_F_COMPLETE_DEFER) |
9da070b1 | 1766 | io_req_complete_defer(req); |
75d7b3ae | 1767 | else |
1bec951c | 1768 | io_req_complete_post(req, issue_flags); |
97b388d7 | 1769 | |
9b43ef3d PB |
1770 | return 0; |
1771 | } | |
def596e9 | 1772 | |
e0b23d99 PB |
1773 | if (ret == IOU_ISSUE_SKIP_COMPLETE) { |
1774 | ret = 0; | |
def596e9 | 1775 | |
e0b23d99 PB |
1776 | /* If the op doesn't have a file, we're not polling for it */ |
1777 | if ((req->ctx->flags & IORING_SETUP_IOPOLL) && def->iopoll_queue) | |
1778 | io_iopoll_req_issued(req, issue_flags); | |
1779 | } | |
1780 | return ret; | |
2b188cc1 JA |
1781 | } |
1782 | ||
bcf8a029 | 1783 | int io_poll_issue(struct io_kiocb *req, io_tw_token_t tw) |
329061d3 | 1784 | { |
c457eed5 PB |
1785 | const unsigned int issue_flags = IO_URING_F_NONBLOCK | |
1786 | IO_URING_F_MULTISHOT | | |
1787 | IO_URING_F_COMPLETE_DEFER; | |
1788 | int ret; | |
1789 | ||
bcf8a029 | 1790 | io_tw_lock(req->ctx, tw); |
c457eed5 PB |
1791 | |
1792 | WARN_ON_ONCE(!req->file); | |
1793 | if (WARN_ON_ONCE(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
1794 | return -EFAULT; | |
1795 | ||
1796 | ret = __io_issue_sqe(req, issue_flags, &io_issue_defs[req->opcode]); | |
1797 | ||
7a9dcb05 | 1798 | WARN_ON_ONCE(ret == IOU_ISSUE_SKIP_COMPLETE); |
c457eed5 | 1799 | return ret; |
329061d3 JA |
1800 | } |
1801 | ||
c9f06aa7 | 1802 | struct io_wq_work *io_wq_free_work(struct io_wq_work *work) |
ebc11b6c PB |
1803 | { |
1804 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
247f97a5 | 1805 | struct io_kiocb *nxt = NULL; |
ebc11b6c | 1806 | |
39051364 | 1807 | if (req_ref_put_and_test_atomic(req)) { |
247f97a5 PB |
1808 | if (req->flags & IO_REQ_LINK_FLAGS) |
1809 | nxt = io_req_find_next(req); | |
1810 | io_free_req(req); | |
1811 | } | |
1812 | return nxt ? &nxt->work : NULL; | |
ebc11b6c PB |
1813 | } |
1814 | ||
c9f06aa7 | 1815 | void io_wq_submit_work(struct io_wq_work *work) |
2b188cc1 JA |
1816 | { |
1817 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
a7dd2782 | 1818 | const struct io_issue_def *def = &io_issue_defs[req->opcode]; |
e6aeb272 | 1819 | unsigned int issue_flags = IO_URING_F_UNLOCKED | IO_URING_F_IOWQ; |
d01905db | 1820 | bool needs_poll = false; |
6bf9c47a | 1821 | int ret = 0, err = -ECANCELED; |
2b188cc1 | 1822 | |
9fe99eed | 1823 | /* one will be dropped by io_wq_free_work() after returning to io-wq */ |
48dcd38d PB |
1824 | if (!(req->flags & REQ_F_REFCOUNT)) |
1825 | __io_req_set_refcount(req, 2); | |
1826 | else | |
1827 | req_ref_get(req); | |
5d5901a3 | 1828 | |
dadebc35 | 1829 | /* either cancelled or io-wq is dying, so don't touch tctx->iowq */ |
3474d1b9 | 1830 | if (atomic_read(&work->flags) & IO_WQ_WORK_CANCEL) { |
0f8da75b | 1831 | fail: |
6bf9c47a | 1832 | io_req_task_queue_fail(req, err); |
d01905db PB |
1833 | return; |
1834 | } | |
f4992544 | 1835 | if (!io_assign_file(req, def, issue_flags)) { |
0f8da75b | 1836 | err = -EBADF; |
3474d1b9 | 1837 | atomic_or(IO_WQ_WORK_CANCEL, &work->flags); |
0f8da75b PB |
1838 | goto fail; |
1839 | } | |
31b51510 | 1840 | |
e0e4ab52 PB |
1841 | /* |
1842 | * If DEFER_TASKRUN is set, it's only allowed to post CQEs from the | |
1843 | * submitter task context. Final request completions are handed to the | |
1844 | * right context, however this is not the case of auxiliary CQEs, | |
1845 | * which is the main mean of operation for multishot requests. | |
1846 | * Don't allow any multishot execution from io-wq. It's more restrictive | |
1847 | * than necessary and also cleaner. | |
1848 | */ | |
6889ae1b | 1849 | if (req->flags & (REQ_F_MULTISHOT|REQ_F_APOLL_MULTISHOT)) { |
e0e4ab52 PB |
1850 | err = -EBADFD; |
1851 | if (!io_file_can_poll(req)) | |
1852 | goto fail; | |
bee1d5be JA |
1853 | if (req->file->f_flags & O_NONBLOCK || |
1854 | req->file->f_mode & FMODE_NOWAIT) { | |
1855 | err = -ECANCELED; | |
1856 | if (io_arm_poll_handler(req, issue_flags) != IO_APOLL_OK) | |
1857 | goto fail; | |
1858 | return; | |
1859 | } else { | |
6889ae1b | 1860 | req->flags &= ~(REQ_F_APOLL_MULTISHOT|REQ_F_MULTISHOT); |
bee1d5be | 1861 | } |
e0e4ab52 PB |
1862 | } |
1863 | ||
d01905db | 1864 | if (req->flags & REQ_F_FORCE_ASYNC) { |
afb7f56f PB |
1865 | bool opcode_poll = def->pollin || def->pollout; |
1866 | ||
95041b93 | 1867 | if (opcode_poll && io_file_can_poll(req)) { |
afb7f56f | 1868 | needs_poll = true; |
d01905db | 1869 | issue_flags |= IO_URING_F_NONBLOCK; |
afb7f56f | 1870 | } |
561fb04a | 1871 | } |
31b51510 | 1872 | |
d01905db PB |
1873 | do { |
1874 | ret = io_issue_sqe(req, issue_flags); | |
1875 | if (ret != -EAGAIN) | |
1876 | break; | |
a9be2022 JA |
1877 | |
1878 | /* | |
1879 | * If REQ_F_NOWAIT is set, then don't wait or retry with | |
1880 | * poll. -EAGAIN is final for that case. | |
1881 | */ | |
1882 | if (req->flags & REQ_F_NOWAIT) | |
1883 | break; | |
1884 | ||
d01905db PB |
1885 | /* |
1886 | * We can get EAGAIN for iopolled IO even though we're | |
1887 | * forcing a sync submission from here, since we can't | |
1888 | * wait for request slots on the block side. | |
1889 | */ | |
1890 | if (!needs_poll) { | |
e0deb6a0 PB |
1891 | if (!(req->ctx->flags & IORING_SETUP_IOPOLL)) |
1892 | break; | |
45500dc4 PB |
1893 | if (io_wq_worker_stopped()) |
1894 | break; | |
d01905db PB |
1895 | cond_resched(); |
1896 | continue; | |
90fa0288 HX |
1897 | } |
1898 | ||
4d9237e3 | 1899 | if (io_arm_poll_handler(req, issue_flags) == IO_APOLL_OK) |
d01905db PB |
1900 | return; |
1901 | /* aborted or ready, in either case retry blocking */ | |
1902 | needs_poll = false; | |
1903 | issue_flags &= ~IO_URING_F_NONBLOCK; | |
1904 | } while (1); | |
31b51510 | 1905 | |
a3df7698 | 1906 | /* avoid locking problems by failing it from a clean context */ |
29d63b94 | 1907 | if (ret) |
a3df7698 | 1908 | io_req_task_queue_fail(req, ret); |
2b188cc1 JA |
1909 | } |
1910 | ||
531113bb JA |
1911 | inline struct file *io_file_get_fixed(struct io_kiocb *req, int fd, |
1912 | unsigned int issue_flags) | |
09bb8394 | 1913 | { |
5106dd6e | 1914 | struct io_ring_ctx *ctx = req->ctx; |
7029acd8 | 1915 | struct io_rsrc_node *node; |
5106dd6e | 1916 | struct file *file = NULL; |
09bb8394 | 1917 | |
93f052cb | 1918 | io_ring_submit_lock(ctx, issue_flags); |
b54a1404 | 1919 | node = io_rsrc_node_lookup(&ctx->file_table.data, fd); |
7029acd8 | 1920 | if (node) { |
8a2dacd4 JA |
1921 | node->refs++; |
1922 | req->file_node = node; | |
7029acd8 JA |
1923 | req->flags |= io_slot_flags(node); |
1924 | file = io_slot_file(node); | |
1925 | } | |
93f052cb | 1926 | io_ring_submit_unlock(ctx, issue_flags); |
ac177053 PB |
1927 | return file; |
1928 | } | |
d44f554e | 1929 | |
531113bb | 1930 | struct file *io_file_get_normal(struct io_kiocb *req, int fd) |
ac177053 | 1931 | { |
62906e89 | 1932 | struct file *file = fget(fd); |
ac177053 | 1933 | |
48863ffd | 1934 | trace_io_uring_file_get(req, fd); |
09bb8394 | 1935 | |
ac177053 | 1936 | /* we don't allow fixed io_uring files */ |
e5550a14 | 1937 | if (file && io_is_uring_fops(file)) |
9cae36a0 | 1938 | io_req_track_inflight(req); |
8371adf5 | 1939 | return file; |
09bb8394 JA |
1940 | } |
1941 | ||
7bfa9bad | 1942 | static void io_queue_async(struct io_kiocb *req, int ret) |
d475a9a6 PB |
1943 | __must_hold(&req->ctx->uring_lock) |
1944 | { | |
7bfa9bad | 1945 | if (ret != -EAGAIN || (req->flags & REQ_F_NOWAIT)) { |
973fc83f | 1946 | io_req_defer_failed(req, ret); |
7bfa9bad PB |
1947 | return; |
1948 | } | |
1949 | ||
4d9237e3 | 1950 | switch (io_arm_poll_handler(req, 0)) { |
d475a9a6 | 1951 | case IO_APOLL_READY: |
336d28a8 | 1952 | io_kbuf_recycle(req, 0); |
d475a9a6 PB |
1953 | io_req_task_queue(req); |
1954 | break; | |
1955 | case IO_APOLL_ABORTED: | |
6436c770 | 1956 | io_kbuf_recycle(req, 0); |
6e6b8c62 | 1957 | io_queue_iowq(req); |
d475a9a6 | 1958 | break; |
b1c62645 | 1959 | case IO_APOLL_OK: |
b1c62645 | 1960 | break; |
d475a9a6 | 1961 | } |
d475a9a6 PB |
1962 | } |
1963 | ||
cbc2e203 | 1964 | static inline void io_queue_sqe(struct io_kiocb *req) |
282cdc86 | 1965 | __must_hold(&req->ctx->uring_lock) |
2b188cc1 | 1966 | { |
e0c5c576 | 1967 | int ret; |
2b188cc1 | 1968 | |
c5eef2b9 | 1969 | ret = io_issue_sqe(req, IO_URING_F_NONBLOCK|IO_URING_F_COMPLETE_DEFER); |
193155c8 | 1970 | |
491381ce JA |
1971 | /* |
1972 | * We async punt it if the file wasn't marked NOWAIT, or if the file | |
1973 | * doesn't support non-blocking read/write attempts | |
1974 | */ | |
e0b23d99 | 1975 | if (unlikely(ret)) |
7bfa9bad | 1976 | io_queue_async(req, ret); |
2b188cc1 JA |
1977 | } |
1978 | ||
4652fe3f | 1979 | static void io_queue_sqe_fallback(struct io_kiocb *req) |
282cdc86 | 1980 | __must_hold(&req->ctx->uring_lock) |
4fe2c963 | 1981 | { |
17b147f6 PB |
1982 | if (unlikely(req->flags & REQ_F_FAIL)) { |
1983 | /* | |
1984 | * We don't submit, fail them all, for that replace hardlinks | |
1985 | * with normal links. Extra REQ_F_LINK is tolerated. | |
1986 | */ | |
1987 | req->flags &= ~REQ_F_HARDLINK; | |
1988 | req->flags |= REQ_F_LINK; | |
973fc83f | 1989 | io_req_defer_failed(req, req->cqe.res); |
76cc33d7 | 1990 | } else { |
ef5c600a DY |
1991 | if (unlikely(req->ctx->drain_active)) |
1992 | io_drain_req(req); | |
76cc33d7 | 1993 | else |
6e6b8c62 | 1994 | io_queue_iowq(req); |
ce35a47a | 1995 | } |
4fe2c963 JL |
1996 | } |
1997 | ||
b16fed66 PB |
1998 | /* |
1999 | * Check SQE restrictions (opcode and flags). | |
2000 | * | |
2001 | * Returns 'true' if SQE is allowed, 'false' otherwise. | |
2002 | */ | |
2003 | static inline bool io_check_restriction(struct io_ring_ctx *ctx, | |
2004 | struct io_kiocb *req, | |
2005 | unsigned int sqe_flags) | |
4fe2c963 | 2006 | { |
b16fed66 PB |
2007 | if (!test_bit(req->opcode, ctx->restrictions.sqe_op)) |
2008 | return false; | |
2009 | ||
2010 | if ((sqe_flags & ctx->restrictions.sqe_flags_required) != | |
2011 | ctx->restrictions.sqe_flags_required) | |
2012 | return false; | |
2013 | ||
2014 | if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed | | |
2015 | ctx->restrictions.sqe_flags_required)) | |
2016 | return false; | |
2017 | ||
2018 | return true; | |
4fe2c963 JL |
2019 | } |
2020 | ||
60e6ce74 | 2021 | static void io_init_drain(struct io_ring_ctx *ctx) |
22b2ca31 | 2022 | { |
22b2ca31 PB |
2023 | struct io_kiocb *head = ctx->submit_state.link.head; |
2024 | ||
2025 | ctx->drain_active = true; | |
2026 | if (head) { | |
2027 | /* | |
2028 | * If we need to drain a request in the middle of a link, drain | |
2029 | * the head request and the next request/link after the current | |
2030 | * link. Considering sequential execution of links, | |
b6c7db32 | 2031 | * REQ_F_IO_DRAIN will be maintained for every request of our |
22b2ca31 PB |
2032 | * link. |
2033 | */ | |
b6c7db32 | 2034 | head->flags |= REQ_F_IO_DRAIN | REQ_F_FORCE_ASYNC; |
22b2ca31 PB |
2035 | ctx->drain_next = true; |
2036 | } | |
2037 | } | |
2038 | ||
e21e1c45 JA |
2039 | static __cold int io_init_fail_req(struct io_kiocb *req, int err) |
2040 | { | |
2041 | /* ensure per-opcode data is cleared if we fail before prep */ | |
2042 | memset(&req->cmd.data, 0, sizeof(req->cmd.data)); | |
2043 | return err; | |
2044 | } | |
2045 | ||
b16fed66 PB |
2046 | static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req, |
2047 | const struct io_uring_sqe *sqe) | |
282cdc86 | 2048 | __must_hold(&ctx->uring_lock) |
b16fed66 | 2049 | { |
a7dd2782 | 2050 | const struct io_issue_def *def; |
b16fed66 | 2051 | unsigned int sqe_flags; |
fc0ae024 | 2052 | int personality; |
4a04d1d1 | 2053 | u8 opcode; |
b16fed66 | 2054 | |
9c2ff3f9 | 2055 | req->ctx = ctx; |
4a04d1d1 | 2056 | req->opcode = opcode = READ_ONCE(sqe->opcode); |
b16fed66 | 2057 | /* same numerical values with corresponding REQ_F_*, safe to copy */ |
4bcb982c | 2058 | sqe_flags = READ_ONCE(sqe->flags); |
17ea56b7 | 2059 | req->flags = (__force io_req_flags_t) sqe_flags; |
cef216fc | 2060 | req->cqe.user_data = READ_ONCE(sqe->user_data); |
b16fed66 | 2061 | req->file = NULL; |
b6f58a3f | 2062 | req->tctx = current->io_uring; |
f4a1254f | 2063 | req->cancel_seq_set = false; |
b16fed66 | 2064 | |
4a04d1d1 PB |
2065 | if (unlikely(opcode >= IORING_OP_LAST)) { |
2066 | req->opcode = 0; | |
e21e1c45 | 2067 | return io_init_fail_req(req, -EINVAL); |
4a04d1d1 | 2068 | } |
1e988c3f PB |
2069 | opcode = array_index_nospec(opcode, IORING_OP_LAST); |
2070 | ||
a7dd2782 | 2071 | def = &io_issue_defs[opcode]; |
68fe256a PB |
2072 | if (unlikely(sqe_flags & ~SQE_COMMON_FLAGS)) { |
2073 | /* enforce forwards compatibility on users */ | |
2074 | if (sqe_flags & ~SQE_VALID_FLAGS) | |
e21e1c45 | 2075 | return io_init_fail_req(req, -EINVAL); |
4e906702 | 2076 | if (sqe_flags & IOSQE_BUFFER_SELECT) { |
fcde59fe | 2077 | if (!def->buffer_select) |
e21e1c45 | 2078 | return io_init_fail_req(req, -EOPNOTSUPP); |
4e906702 JA |
2079 | req->buf_index = READ_ONCE(sqe->buf_group); |
2080 | } | |
5562a8d7 PB |
2081 | if (sqe_flags & IOSQE_CQE_SKIP_SUCCESS) |
2082 | ctx->drain_disabled = true; | |
2083 | if (sqe_flags & IOSQE_IO_DRAIN) { | |
2084 | if (ctx->drain_disabled) | |
e21e1c45 | 2085 | return io_init_fail_req(req, -EOPNOTSUPP); |
60e6ce74 | 2086 | io_init_drain(ctx); |
5562a8d7 | 2087 | } |
2a56a9bd PB |
2088 | } |
2089 | if (unlikely(ctx->restricted || ctx->drain_active || ctx->drain_next)) { | |
2090 | if (ctx->restricted && !io_check_restriction(ctx, req, sqe_flags)) | |
e21e1c45 | 2091 | return io_init_fail_req(req, -EACCES); |
2a56a9bd PB |
2092 | /* knock it to the slow queue path, will be drained there */ |
2093 | if (ctx->drain_active) | |
2094 | req->flags |= REQ_F_FORCE_ASYNC; | |
2095 | /* if there is no link, we're at "next" request and need to drain */ | |
2096 | if (unlikely(ctx->drain_next) && !ctx->submit_state.link.head) { | |
2097 | ctx->drain_next = false; | |
2098 | ctx->drain_active = true; | |
b6c7db32 | 2099 | req->flags |= REQ_F_IO_DRAIN | REQ_F_FORCE_ASYNC; |
2a56a9bd | 2100 | } |
68fe256a | 2101 | } |
b16fed66 | 2102 | |
fcde59fe | 2103 | if (!def->ioprio && sqe->ioprio) |
e21e1c45 | 2104 | return io_init_fail_req(req, -EINVAL); |
fcde59fe | 2105 | if (!def->iopoll && (ctx->flags & IORING_SETUP_IOPOLL)) |
e21e1c45 | 2106 | return io_init_fail_req(req, -EINVAL); |
73911426 | 2107 | |
fcde59fe | 2108 | if (def->needs_file) { |
6d63416d PB |
2109 | struct io_submit_state *state = &ctx->submit_state; |
2110 | ||
cef216fc | 2111 | req->cqe.fd = READ_ONCE(sqe->fd); |
6bf9c47a | 2112 | |
6d63416d PB |
2113 | /* |
2114 | * Plug now if we have more than 2 IO left after this, and the | |
2115 | * target is potentially a read/write to block based storage. | |
2116 | */ | |
fcde59fe | 2117 | if (state->need_plug && def->plug) { |
6d63416d PB |
2118 | state->plug_started = true; |
2119 | state->need_plug = false; | |
5ca7a8b3 | 2120 | blk_start_plug_nr_ios(&state->plug, state->submit_nr); |
6d63416d | 2121 | } |
b16fed66 | 2122 | } |
863e0560 | 2123 | |
003e8dcc JA |
2124 | personality = READ_ONCE(sqe->personality); |
2125 | if (personality) { | |
cdab10bf LT |
2126 | int ret; |
2127 | ||
c10d1f98 PB |
2128 | req->creds = xa_load(&ctx->personalities, personality); |
2129 | if (!req->creds) | |
e21e1c45 | 2130 | return io_init_fail_req(req, -EINVAL); |
c10d1f98 | 2131 | get_cred(req->creds); |
cdc1404a PM |
2132 | ret = security_uring_override_creds(req->creds); |
2133 | if (ret) { | |
2134 | put_cred(req->creds); | |
e21e1c45 | 2135 | return io_init_fail_req(req, ret); |
cdc1404a | 2136 | } |
b8e64b53 | 2137 | req->flags |= REQ_F_CREDS; |
003e8dcc | 2138 | } |
b16fed66 | 2139 | |
0702e536 | 2140 | return def->prep(req, sqe); |
b16fed66 PB |
2141 | } |
2142 | ||
df3becde PB |
2143 | static __cold int io_submit_fail_init(const struct io_uring_sqe *sqe, |
2144 | struct io_kiocb *req, int ret) | |
2145 | { | |
2146 | struct io_ring_ctx *ctx = req->ctx; | |
2147 | struct io_submit_link *link = &ctx->submit_state.link; | |
2148 | struct io_kiocb *head = link->head; | |
2149 | ||
48863ffd | 2150 | trace_io_uring_req_failed(sqe, req, ret); |
df3becde PB |
2151 | |
2152 | /* | |
2153 | * Avoid breaking links in the middle as it renders links with SQPOLL | |
2154 | * unusable. Instead of failing eagerly, continue assembling the link if | |
2155 | * applicable and mark the head with REQ_F_FAIL. The link flushing code | |
2156 | * should find the flag and handle the rest. | |
2157 | */ | |
2158 | req_fail_link_node(req, ret); | |
2159 | if (head && !(head->flags & REQ_F_FAIL)) | |
2160 | req_fail_link_node(head, -ECANCELED); | |
2161 | ||
2162 | if (!(req->flags & IO_REQ_LINK_FLAGS)) { | |
2163 | if (head) { | |
2164 | link->last->link = req; | |
2165 | link->head = NULL; | |
2166 | req = head; | |
2167 | } | |
2168 | io_queue_sqe_fallback(req); | |
2169 | return ret; | |
2170 | } | |
2171 | ||
2172 | if (head) | |
2173 | link->last->link = req; | |
2174 | else | |
2175 | link->head = req; | |
2176 | link->last = req; | |
2177 | return 0; | |
2178 | } | |
2179 | ||
2180 | static inline int io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req, | |
a1ab7b35 | 2181 | const struct io_uring_sqe *sqe) |
282cdc86 | 2182 | __must_hold(&ctx->uring_lock) |
9e645e11 | 2183 | { |
a1ab7b35 | 2184 | struct io_submit_link *link = &ctx->submit_state.link; |
ef4ff581 | 2185 | int ret; |
9e645e11 | 2186 | |
a6b8cadc | 2187 | ret = io_init_req(ctx, req, sqe); |
df3becde PB |
2188 | if (unlikely(ret)) |
2189 | return io_submit_fail_init(sqe, req, ret); | |
441b8a78 | 2190 | |
2ad57931 | 2191 | trace_io_uring_submit_req(req); |
a6b8cadc | 2192 | |
9e645e11 JA |
2193 | /* |
2194 | * If we already have a head request, queue this one for async | |
2195 | * submittal once the head completes. If we don't have a head but | |
2196 | * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be | |
2197 | * submitted sync once the chain is complete. If none of those | |
2198 | * conditions are true (normal request), then just queue it. | |
2199 | */ | |
924a07e4 | 2200 | if (unlikely(link->head)) { |
eed138d6 | 2201 | trace_io_uring_link(req, link->last); |
f2f87370 | 2202 | link->last->link = req; |
863e0560 | 2203 | link->last = req; |
32fe525b | 2204 | |
da1a08c5 | 2205 | if (req->flags & IO_REQ_LINK_FLAGS) |
f15a3431 | 2206 | return 0; |
df3becde PB |
2207 | /* last request of the link, flush it */ |
2208 | req = link->head; | |
f15a3431 | 2209 | link->head = NULL; |
924a07e4 PB |
2210 | if (req->flags & (REQ_F_FORCE_ASYNC | REQ_F_FAIL)) |
2211 | goto fallback; | |
2212 | ||
2213 | } else if (unlikely(req->flags & (IO_REQ_LINK_FLAGS | | |
2214 | REQ_F_FORCE_ASYNC | REQ_F_FAIL))) { | |
2215 | if (req->flags & IO_REQ_LINK_FLAGS) { | |
2216 | link->head = req; | |
2217 | link->last = req; | |
2218 | } else { | |
2219 | fallback: | |
2220 | io_queue_sqe_fallback(req); | |
2221 | } | |
f15a3431 | 2222 | return 0; |
9e645e11 | 2223 | } |
2e6e1fde | 2224 | |
f15a3431 | 2225 | io_queue_sqe(req); |
1d4240cc | 2226 | return 0; |
9e645e11 JA |
2227 | } |
2228 | ||
9a56a232 JA |
2229 | /* |
2230 | * Batched submission is done, ensure local IO is flushed out. | |
2231 | */ | |
553deffd | 2232 | static void io_submit_state_end(struct io_ring_ctx *ctx) |
9a56a232 | 2233 | { |
553deffd PB |
2234 | struct io_submit_state *state = &ctx->submit_state; |
2235 | ||
e126391c PB |
2236 | if (unlikely(state->link.head)) |
2237 | io_queue_sqe_fallback(state->link.head); | |
553deffd | 2238 | /* flush only after queuing links as they can generate completions */ |
c450178d | 2239 | io_submit_flush_completions(ctx); |
27926b68 JA |
2240 | if (state->plug_started) |
2241 | blk_finish_plug(&state->plug); | |
9a56a232 JA |
2242 | } |
2243 | ||
2244 | /* | |
2245 | * Start submission side cache. | |
2246 | */ | |
2247 | static void io_submit_state_start(struct io_submit_state *state, | |
ba88ff11 | 2248 | unsigned int max_ios) |
9a56a232 | 2249 | { |
27926b68 | 2250 | state->plug_started = false; |
4b628aeb | 2251 | state->need_plug = max_ios > 2; |
5ca7a8b3 | 2252 | state->submit_nr = max_ios; |
a1ab7b35 PB |
2253 | /* set only head, no need to init link_last in advance */ |
2254 | state->link.head = NULL; | |
9a56a232 JA |
2255 | } |
2256 | ||
2b188cc1 JA |
2257 | static void io_commit_sqring(struct io_ring_ctx *ctx) |
2258 | { | |
75b28aff | 2259 | struct io_rings *rings = ctx->rings; |
2b188cc1 | 2260 | |
caf582c6 PB |
2261 | /* |
2262 | * Ensure any loads from the SQEs are done at this point, | |
2263 | * since once we write the new head, the application could | |
2264 | * write new data to them. | |
2265 | */ | |
2266 | smp_store_release(&rings->sq.head, ctx->cached_sq_head); | |
2b188cc1 JA |
2267 | } |
2268 | ||
2b188cc1 | 2269 | /* |
dd9ae8a0 | 2270 | * Fetch an sqe, if one is available. Note this returns a pointer to memory |
2b188cc1 JA |
2271 | * that is mapped by userspace. This means that care needs to be taken to |
2272 | * ensure that reads are stable, as we cannot rely on userspace always | |
2273 | * being a good citizen. If members of the sqe are validated and then later | |
2274 | * used, it's important that those reads are done through READ_ONCE() to | |
2275 | * prevent a re-load down the line. | |
2276 | */ | |
b5083dfa | 2277 | static bool io_get_sqe(struct io_ring_ctx *ctx, const struct io_uring_sqe **sqe) |
2b188cc1 | 2278 | { |
2af89abd PB |
2279 | unsigned mask = ctx->sq_entries - 1; |
2280 | unsigned head = ctx->cached_sq_head++ & mask; | |
2281 | ||
9b296c62 PB |
2282 | if (static_branch_unlikely(&io_key_has_sqarray) && |
2283 | (!(ctx->flags & IORING_SETUP_NO_SQARRAY))) { | |
2af89abd PB |
2284 | head = READ_ONCE(ctx->sq_array[head]); |
2285 | if (unlikely(head >= ctx->sq_entries)) { | |
2af89abd PB |
2286 | WRITE_ONCE(ctx->rings->sq_dropped, |
2287 | READ_ONCE(ctx->rings->sq_dropped) + 1); | |
2288 | return false; | |
2289 | } | |
48324271 | 2290 | head = array_index_nospec(head, ctx->sq_entries); |
2af89abd | 2291 | } |
2b188cc1 JA |
2292 | |
2293 | /* | |
2294 | * The cached sq head (or cq tail) serves two purposes: | |
2295 | * | |
2296 | * 1) allows us to batch the cost of updating the user visible | |
2297 | * head updates. | |
2298 | * 2) allows the kernel side to track the head on its own, even | |
2299 | * though the application is the one updating it. | |
2300 | */ | |
2b188cc1 | 2301 | |
2af89abd PB |
2302 | /* double index for 128-byte SQEs, twice as long */ |
2303 | if (ctx->flags & IORING_SETUP_SQE128) | |
2304 | head <<= 1; | |
2305 | *sqe = &ctx->sq_sqes[head]; | |
2306 | return true; | |
709b302f PB |
2307 | } |
2308 | ||
17437f31 | 2309 | int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr) |
282cdc86 | 2310 | __must_hold(&ctx->uring_lock) |
6c271ce2 | 2311 | { |
69629809 | 2312 | unsigned int entries = io_sqring_entries(ctx); |
8e6971a8 PB |
2313 | unsigned int left; |
2314 | int ret; | |
6c271ce2 | 2315 | |
51d48dab | 2316 | if (unlikely(!entries)) |
69629809 | 2317 | return 0; |
ee7d46d9 | 2318 | /* make sure SQ entry isn't read before tail */ |
e3ef728f | 2319 | ret = left = min(nr, entries); |
8e6971a8 PB |
2320 | io_get_task_refs(left); |
2321 | io_submit_state_start(&ctx->submit_state, left); | |
6c271ce2 | 2322 | |
69629809 | 2323 | do { |
3529d8c2 | 2324 | const struct io_uring_sqe *sqe; |
196be95c | 2325 | struct io_kiocb *req; |
fb5ccc98 | 2326 | |
c8576f3e | 2327 | if (unlikely(!io_alloc_req(ctx, &req))) |
fb5ccc98 | 2328 | break; |
b5083dfa | 2329 | if (unlikely(!io_get_sqe(ctx, &sqe))) { |
fa05457a | 2330 | io_req_add_to_cache(req, ctx); |
4fccfcbb PB |
2331 | break; |
2332 | } | |
6c271ce2 | 2333 | |
1cd15904 PB |
2334 | /* |
2335 | * Continue submitting even for sqe failure if the | |
2336 | * ring was setup with IORING_SETUP_SUBMIT_ALL | |
2337 | */ | |
2338 | if (unlikely(io_submit_sqe(ctx, req, sqe)) && | |
2339 | !(ctx->flags & IORING_SETUP_SUBMIT_ALL)) { | |
2340 | left--; | |
2341 | break; | |
bcbb7bf6 | 2342 | } |
1cd15904 | 2343 | } while (--left); |
9466f437 | 2344 | |
8e6971a8 PB |
2345 | if (unlikely(left)) { |
2346 | ret -= left; | |
2347 | /* try again if it submitted nothing and can't allocate a req */ | |
2348 | if (!ret && io_req_cache_empty(ctx)) | |
2349 | ret = -EAGAIN; | |
2350 | current->io_uring->cached_refs += left; | |
9466f437 | 2351 | } |
6c271ce2 | 2352 | |
553deffd | 2353 | io_submit_state_end(ctx); |
ae9428ca PB |
2354 | /* Commit SQ ring head once we've consumed and submitted all SQEs */ |
2355 | io_commit_sqring(ctx); | |
8e6971a8 | 2356 | return ret; |
6c271ce2 JA |
2357 | } |
2358 | ||
bda52162 JA |
2359 | static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode, |
2360 | int wake_flags, void *key) | |
2361 | { | |
bd550173 | 2362 | struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue, wq); |
bda52162 | 2363 | |
6c503150 PB |
2364 | /* |
2365 | * Cannot safely flush overflowed CQEs from here, ensure we wake up | |
2366 | * the task, and the next invocation will do it. | |
2367 | */ | |
bd550173 | 2368 | if (io_should_wake(iowq) || io_has_work(iowq->ctx)) |
6c503150 PB |
2369 | return autoremove_wake_function(curr, mode, wake_flags, key); |
2370 | return -1; | |
bda52162 JA |
2371 | } |
2372 | ||
c0e0d6ba | 2373 | int io_run_task_work_sig(struct io_ring_ctx *ctx) |
af9c1a44 | 2374 | { |
40cfe553 | 2375 | if (io_local_work_pending(ctx)) { |
2f413956 | 2376 | __set_current_state(TASK_RUNNING); |
49c5c63d | 2377 | if (io_run_local_work(ctx, INT_MAX, IO_LOCAL_TW_DEFAULT_MAX) > 0) |
d246c759 | 2378 | return 0; |
1414d629 PB |
2379 | } |
2380 | if (io_run_task_work() > 0) | |
d246c759 | 2381 | return 0; |
c5020bc8 OL |
2382 | if (task_sigpending(current)) |
2383 | return -EINTR; | |
2384 | return 0; | |
af9c1a44 JA |
2385 | } |
2386 | ||
7b72d661 JA |
2387 | static bool current_pending_io(void) |
2388 | { | |
2389 | struct io_uring_task *tctx = current->io_uring; | |
2390 | ||
2391 | if (!tctx) | |
2392 | return false; | |
2393 | return percpu_counter_read_positive(&tctx->inflight); | |
2394 | } | |
2395 | ||
cebf123c JA |
2396 | static enum hrtimer_restart io_cqring_timer_wakeup(struct hrtimer *timer) |
2397 | { | |
2398 | struct io_wait_queue *iowq = container_of(timer, struct io_wait_queue, t); | |
2399 | ||
2400 | WRITE_ONCE(iowq->hit_timeout, 1); | |
1100c4a2 | 2401 | iowq->min_timeout = 0; |
cebf123c JA |
2402 | wake_up_process(iowq->wq.private); |
2403 | return HRTIMER_NORESTART; | |
2404 | } | |
2405 | ||
1100c4a2 JA |
2406 | /* |
2407 | * Doing min_timeout portion. If we saw any timeouts, events, or have work, | |
2408 | * wake up. If not, and we have a normal timeout, switch to that and keep | |
2409 | * sleeping. | |
2410 | */ | |
2411 | static enum hrtimer_restart io_cqring_min_timer_wakeup(struct hrtimer *timer) | |
2412 | { | |
2413 | struct io_wait_queue *iowq = container_of(timer, struct io_wait_queue, t); | |
2414 | struct io_ring_ctx *ctx = iowq->ctx; | |
2415 | ||
2416 | /* no general timeout, or shorter (or equal), we are done */ | |
2417 | if (iowq->timeout == KTIME_MAX || | |
2418 | ktime_compare(iowq->min_timeout, iowq->timeout) >= 0) | |
2419 | goto out_wake; | |
2420 | /* work we may need to run, wake function will see if we need to wake */ | |
2421 | if (io_has_work(ctx)) | |
2422 | goto out_wake; | |
2423 | /* got events since we started waiting, min timeout is done */ | |
2424 | if (iowq->cq_min_tail != READ_ONCE(ctx->rings->cq.tail)) | |
2425 | goto out_wake; | |
2426 | /* if we have any events and min timeout expired, we're done */ | |
2427 | if (io_cqring_events(ctx)) | |
2428 | goto out_wake; | |
2429 | ||
2430 | /* | |
2431 | * If using deferred task_work running and application is waiting on | |
2432 | * more than one request, ensure we reset it now where we are switching | |
2433 | * to normal sleeps. Any request completion post min_wait should wake | |
2434 | * the task and return. | |
2435 | */ | |
2436 | if (ctx->flags & IORING_SETUP_DEFER_TASKRUN) { | |
2437 | atomic_set(&ctx->cq_wait_nr, 1); | |
2438 | smp_mb(); | |
2439 | if (!llist_empty(&ctx->work_llist)) | |
2440 | goto out_wake; | |
2441 | } | |
2442 | ||
3f8d93d1 | 2443 | hrtimer_update_function(&iowq->t, io_cqring_timer_wakeup); |
1100c4a2 JA |
2444 | hrtimer_set_expires(timer, iowq->timeout); |
2445 | return HRTIMER_RESTART; | |
2446 | out_wake: | |
2447 | return io_cqring_timer_wakeup(timer); | |
2448 | } | |
2449 | ||
cebf123c | 2450 | static int io_cqring_schedule_timeout(struct io_wait_queue *iowq, |
1100c4a2 | 2451 | clockid_t clock_id, ktime_t start_time) |
cebf123c | 2452 | { |
1100c4a2 JA |
2453 | ktime_t timeout; |
2454 | ||
1100c4a2 JA |
2455 | if (iowq->min_timeout) { |
2456 | timeout = ktime_add_ns(iowq->min_timeout, start_time); | |
fc9f59de NC |
2457 | hrtimer_setup_on_stack(&iowq->t, io_cqring_min_timer_wakeup, clock_id, |
2458 | HRTIMER_MODE_ABS); | |
1100c4a2 JA |
2459 | } else { |
2460 | timeout = iowq->timeout; | |
fc9f59de NC |
2461 | hrtimer_setup_on_stack(&iowq->t, io_cqring_timer_wakeup, clock_id, |
2462 | HRTIMER_MODE_ABS); | |
1100c4a2 JA |
2463 | } |
2464 | ||
2465 | hrtimer_set_expires_range_ns(&iowq->t, timeout, 0); | |
cebf123c JA |
2466 | hrtimer_start_expires(&iowq->t, HRTIMER_MODE_ABS); |
2467 | ||
2468 | if (!READ_ONCE(iowq->hit_timeout)) | |
2469 | schedule(); | |
2470 | ||
2471 | hrtimer_cancel(&iowq->t); | |
2472 | destroy_hrtimer_on_stack(&iowq->t); | |
2473 | __set_current_state(TASK_RUNNING); | |
2474 | ||
2475 | return READ_ONCE(iowq->hit_timeout) ? -ETIME : 0; | |
2476 | } | |
2477 | ||
07754bfd JA |
2478 | struct ext_arg { |
2479 | size_t argsz; | |
2480 | struct timespec64 ts; | |
2481 | const sigset_t __user *sig; | |
2482 | ktime_t min_time; | |
2483 | bool ts_set; | |
2484 | bool iowait; | |
2485 | }; | |
2486 | ||
45a41e74 | 2487 | static int __io_cqring_wait_schedule(struct io_ring_ctx *ctx, |
1100c4a2 | 2488 | struct io_wait_queue *iowq, |
07754bfd | 2489 | struct ext_arg *ext_arg, |
1100c4a2 | 2490 | ktime_t start_time) |
eeb60b9a | 2491 | { |
45a41e74 | 2492 | int ret = 0; |
8a796565 AF |
2493 | |
2494 | /* | |
7b72d661 JA |
2495 | * Mark us as being in io_wait if we have pending requests, so cpufreq |
2496 | * can take into account that the task is waiting for IO - turns out | |
2497 | * to be important for low QD IO. | |
8a796565 | 2498 | */ |
07754bfd | 2499 | if (ext_arg->iowait && current_pending_io()) |
7b72d661 | 2500 | current->in_iowait = 1; |
1100c4a2 JA |
2501 | if (iowq->timeout != KTIME_MAX || iowq->min_timeout) |
2502 | ret = io_cqring_schedule_timeout(iowq, ctx->clockid, start_time); | |
cebf123c | 2503 | else |
46ae7eef | 2504 | schedule(); |
6f0974ec | 2505 | current->in_iowait = 0; |
8a796565 | 2506 | return ret; |
eeb60b9a PB |
2507 | } |
2508 | ||
45a41e74 JA |
2509 | /* If this returns > 0, the caller should retry */ |
2510 | static inline int io_cqring_wait_schedule(struct io_ring_ctx *ctx, | |
1100c4a2 | 2511 | struct io_wait_queue *iowq, |
07754bfd | 2512 | struct ext_arg *ext_arg, |
1100c4a2 | 2513 | ktime_t start_time) |
45a41e74 JA |
2514 | { |
2515 | if (unlikely(READ_ONCE(ctx->check_cq))) | |
2516 | return 1; | |
40cfe553 | 2517 | if (unlikely(io_local_work_pending(ctx))) |
45a41e74 | 2518 | return 1; |
04beb6e0 | 2519 | if (unlikely(task_work_pending(current))) |
45a41e74 JA |
2520 | return 1; |
2521 | if (unlikely(task_sigpending(current))) | |
2522 | return -EINTR; | |
2523 | if (unlikely(io_should_wake(iowq))) | |
2524 | return 0; | |
2525 | ||
07754bfd | 2526 | return __io_cqring_wait_schedule(ctx, iowq, ext_arg, start_time); |
45a41e74 JA |
2527 | } |
2528 | ||
2b188cc1 JA |
2529 | /* |
2530 | * Wait until events become available, if we don't already have some. The | |
2531 | * application must reap them itself, as they reside on the shared cq ring. | |
2532 | */ | |
d29cb372 | 2533 | static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events, u32 flags, |
f42b58e4 | 2534 | struct ext_arg *ext_arg) |
2b188cc1 | 2535 | { |
90291099 | 2536 | struct io_wait_queue iowq; |
75b28aff | 2537 | struct io_rings *rings = ctx->rings; |
1100c4a2 | 2538 | ktime_t start_time; |
c1d5a224 | 2539 | int ret; |
2b188cc1 | 2540 | |
81661978 PB |
2541 | min_events = min_t(int, min_events, ctx->cq_entries); |
2542 | ||
76de6749 PB |
2543 | if (!io_allowed_run_tw(ctx)) |
2544 | return -EEXIST; | |
40cfe553 | 2545 | if (io_local_work_pending(ctx)) |
49c5c63d JA |
2546 | io_run_local_work(ctx, min_events, |
2547 | max(IO_LOCAL_TW_DEFAULT_MAX, min_events)); | |
f36ba6cf | 2548 | io_run_task_work(); |
408024b9 PB |
2549 | |
2550 | if (unlikely(test_bit(IO_CHECK_CQ_OVERFLOW_BIT, &ctx->check_cq))) | |
2551 | io_cqring_do_overflow_flush(ctx); | |
f36ba6cf PB |
2552 | if (__io_cqring_events_user(ctx) >= min_events) |
2553 | return 0; | |
2b188cc1 | 2554 | |
90291099 PB |
2555 | init_waitqueue_func_entry(&iowq.wq, io_wake_function); |
2556 | iowq.wq.private = current; | |
2557 | INIT_LIST_HEAD(&iowq.wq.entry); | |
2558 | iowq.ctx = ctx; | |
5fd46178 | 2559 | iowq.cq_tail = READ_ONCE(ctx->rings->cq.head) + min_events; |
1100c4a2 JA |
2560 | iowq.cq_min_tail = READ_ONCE(ctx->rings->cq.tail); |
2561 | iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts); | |
2562 | iowq.hit_timeout = 0; | |
7ed9e09e | 2563 | iowq.min_timeout = ext_arg->min_time; |
d33a39e5 | 2564 | iowq.timeout = KTIME_MAX; |
1100c4a2 | 2565 | start_time = io_get_time(ctx); |
d33a39e5 | 2566 | |
0a54a7dd JA |
2567 | if (ext_arg->ts_set) { |
2568 | iowq.timeout = timespec64_to_ktime(ext_arg->ts); | |
d29cb372 | 2569 | if (!(flags & IORING_ENTER_ABS_TIMER)) |
1100c4a2 | 2570 | iowq.timeout = ktime_add(iowq.timeout, start_time); |
d33a39e5 | 2571 | } |
90291099 | 2572 | |
f42b58e4 | 2573 | if (ext_arg->sig) { |
978e5c19 AI |
2574 | #ifdef CONFIG_COMPAT |
2575 | if (in_compat_syscall()) | |
f42b58e4 JA |
2576 | ret = set_compat_user_sigmask((const compat_sigset_t __user *)ext_arg->sig, |
2577 | ext_arg->argsz); | |
978e5c19 AI |
2578 | else |
2579 | #endif | |
f42b58e4 | 2580 | ret = set_user_sigmask(ext_arg->sig, ext_arg->argsz); |
978e5c19 AI |
2581 | |
2582 | if (ret) | |
2583 | return ret; | |
2584 | } | |
2585 | ||
8d0c12a8 SR |
2586 | io_napi_busy_loop(ctx, &iowq); |
2587 | ||
c826bd7a | 2588 | trace_io_uring_cqring_wait(ctx, min_events); |
bda52162 | 2589 | do { |
3fcf19d5 | 2590 | unsigned long check_cq; |
1100c4a2 JA |
2591 | int nr_wait; |
2592 | ||
2593 | /* if min timeout has been hit, don't reset wait count */ | |
2594 | if (!iowq.hit_timeout) | |
2595 | nr_wait = (int) iowq.cq_tail - | |
2596 | READ_ONCE(ctx->rings->cq.tail); | |
2597 | else | |
2598 | nr_wait = 1; | |
3fcf19d5 | 2599 | |
130bd686 | 2600 | if (ctx->flags & IORING_SETUP_DEFER_TASKRUN) { |
8751d154 | 2601 | atomic_set(&ctx->cq_wait_nr, nr_wait); |
130bd686 PB |
2602 | set_current_state(TASK_INTERRUPTIBLE); |
2603 | } else { | |
2604 | prepare_to_wait_exclusive(&ctx->cq_wait, &iowq.wq, | |
2605 | TASK_INTERRUPTIBLE); | |
2606 | } | |
2607 | ||
07754bfd | 2608 | ret = io_cqring_wait_schedule(ctx, &iowq, ext_arg, start_time); |
130bd686 | 2609 | __set_current_state(TASK_RUNNING); |
b4bc35cf | 2610 | atomic_set(&ctx->cq_wait_nr, IO_CQ_WAKE_INIT); |
d80c0f00 | 2611 | |
846072f1 PB |
2612 | /* |
2613 | * Run task_work after scheduling and before io_should_wake(). | |
2614 | * If we got woken because of task_work being processed, run it | |
2615 | * now rather than let the caller do another wait loop. | |
2616 | */ | |
40cfe553 | 2617 | if (io_local_work_pending(ctx)) |
49c5c63d | 2618 | io_run_local_work(ctx, nr_wait, nr_wait); |
04beb6e0 | 2619 | io_run_task_work(); |
3fcf19d5 | 2620 | |
6ff1407e JA |
2621 | /* |
2622 | * Non-local task_work will be run on exit to userspace, but | |
2623 | * if we're using DEFER_TASKRUN, then we could have waited | |
2624 | * with a timeout for a number of requests. If the timeout | |
2625 | * hits, we could have some requests ready to process. Ensure | |
2626 | * this break is _after_ we have run task_work, to avoid | |
2627 | * deferring running potentially pending requests until the | |
2628 | * next time we wait for events. | |
2629 | */ | |
2630 | if (ret < 0) | |
2631 | break; | |
2632 | ||
3fcf19d5 PB |
2633 | check_cq = READ_ONCE(ctx->check_cq); |
2634 | if (unlikely(check_cq)) { | |
2635 | /* let the caller flush overflows, retry */ | |
326a9e48 | 2636 | if (check_cq & BIT(IO_CHECK_CQ_OVERFLOW_BIT)) |
3fcf19d5 | 2637 | io_cqring_do_overflow_flush(ctx); |
3fcf19d5 PB |
2638 | if (check_cq & BIT(IO_CHECK_CQ_DROPPED_BIT)) { |
2639 | ret = -EBADR; | |
2640 | break; | |
2641 | } | |
2642 | } | |
2643 | ||
846072f1 PB |
2644 | if (io_should_wake(&iowq)) { |
2645 | ret = 0; | |
35d90f95 | 2646 | break; |
846072f1 | 2647 | } |
ca0a2651 | 2648 | cond_resched(); |
846072f1 | 2649 | } while (1); |
bda52162 | 2650 | |
130bd686 PB |
2651 | if (!(ctx->flags & IORING_SETUP_DEFER_TASKRUN)) |
2652 | finish_wait(&ctx->cq_wait, &iowq.wq); | |
b7db41c9 | 2653 | restore_saved_sigmask_unless(ret == -EINTR); |
2b188cc1 | 2654 | |
75b28aff | 2655 | return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0; |
2b188cc1 JA |
2656 | } |
2657 | ||
9c189eee JA |
2658 | static void io_rings_free(struct io_ring_ctx *ctx) |
2659 | { | |
8078486e | 2660 | io_free_region(ctx, &ctx->sq_region); |
81a4058e | 2661 | io_free_region(ctx, &ctx->ring_region); |
cef59d1e PB |
2662 | ctx->rings = NULL; |
2663 | ctx->sq_sqes = NULL; | |
9c189eee JA |
2664 | } |
2665 | ||
09d0a8ea JA |
2666 | unsigned long rings_size(unsigned int flags, unsigned int sq_entries, |
2667 | unsigned int cq_entries, size_t *sq_offset) | |
6b06314c | 2668 | { |
73572984 JA |
2669 | struct io_rings *rings; |
2670 | size_t off, sq_array_size; | |
6b06314c | 2671 | |
73572984 JA |
2672 | off = struct_size(rings, cqes, cq_entries); |
2673 | if (off == SIZE_MAX) | |
2674 | return SIZE_MAX; | |
09d0a8ea | 2675 | if (flags & IORING_SETUP_CQE32) { |
73572984 JA |
2676 | if (check_shl_overflow(off, 1, &off)) |
2677 | return SIZE_MAX; | |
2678 | } | |
ab409402 | 2679 | |
73572984 JA |
2680 | #ifdef CONFIG_SMP |
2681 | off = ALIGN(off, SMP_CACHE_BYTES); | |
2682 | if (off == 0) | |
2683 | return SIZE_MAX; | |
2684 | #endif | |
82fbcfa9 | 2685 | |
09d0a8ea | 2686 | if (flags & IORING_SETUP_NO_SQARRAY) { |
547988ad | 2687 | *sq_offset = SIZE_MAX; |
2af89abd PB |
2688 | return off; |
2689 | } | |
2690 | ||
547988ad | 2691 | *sq_offset = off; |
82fbcfa9 | 2692 | |
73572984 JA |
2693 | sq_array_size = array_size(sizeof(u32), sq_entries); |
2694 | if (sq_array_size == SIZE_MAX) | |
2695 | return SIZE_MAX; | |
6b06314c | 2696 | |
73572984 JA |
2697 | if (check_add_overflow(off, sq_array_size, &off)) |
2698 | return SIZE_MAX; | |
8bad28d8 | 2699 | |
73572984 | 2700 | return off; |
8bad28d8 HX |
2701 | } |
2702 | ||
8fb7aee0 | 2703 | static __cold void __io_req_caches_free(struct io_ring_ctx *ctx) |
2b188cc1 | 2704 | { |
c8576f3e | 2705 | struct io_kiocb *req; |
37f0e767 | 2706 | int nr = 0; |
bf019da7 | 2707 | |
88ab95be | 2708 | while (!io_req_cache_empty(ctx)) { |
c8576f3e | 2709 | req = io_extract_req(ctx); |
c2b6c6bc | 2710 | kmem_cache_free(req_cachep, req); |
37f0e767 | 2711 | nr++; |
c2b6c6bc | 2712 | } |
63de899c PB |
2713 | if (nr) { |
2714 | ctx->nr_req_allocated -= nr; | |
37f0e767 | 2715 | percpu_ref_put_many(&ctx->refs, nr); |
63de899c | 2716 | } |
8fb7aee0 PB |
2717 | } |
2718 | ||
2719 | static __cold void io_req_caches_free(struct io_ring_ctx *ctx) | |
2720 | { | |
2721 | guard(mutex)(&ctx->uring_lock); | |
2722 | __io_req_caches_free(ctx); | |
9a4fdbd8 JA |
2723 | } |
2724 | ||
c072481d | 2725 | static __cold void io_ring_ctx_free(struct io_ring_ctx *ctx) |
2b188cc1 | 2726 | { |
37d1e2e3 | 2727 | io_sq_thread_finish(ctx); |
43597aac | 2728 | |
8bad28d8 | 2729 | mutex_lock(&ctx->uring_lock); |
7029acd8 JA |
2730 | io_sqe_buffers_unregister(ctx); |
2731 | io_sqe_files_unregister(ctx); | |
6f377873 | 2732 | io_unregister_zcrx_ifqs(ctx); |
a85381d8 | 2733 | io_cqring_overflow_kill(ctx); |
9b402849 | 2734 | io_eventfd_unregister(ctx); |
40b99183 | 2735 | io_free_alloc_caches(ctx); |
5a2e745d | 2736 | io_destroy_buffers(ctx); |
93238e66 | 2737 | io_free_region(ctx, &ctx->param_region); |
b4a72c05 | 2738 | mutex_unlock(&ctx->uring_lock); |
07db298a PB |
2739 | if (ctx->sq_creds) |
2740 | put_cred(ctx->sq_creds); | |
97bbdc06 PB |
2741 | if (ctx->submitter_task) |
2742 | put_task_struct(ctx->submitter_task); | |
def596e9 | 2743 | |
ef9dd637 | 2744 | WARN_ON_ONCE(!list_empty(&ctx->ltimeout_list)); |
2b188cc1 | 2745 | |
42b6419d PB |
2746 | if (ctx->mm_account) { |
2747 | mmdrop(ctx->mm_account); | |
2748 | ctx->mm_account = NULL; | |
2749 | } | |
9c189eee | 2750 | io_rings_free(ctx); |
2b188cc1 | 2751 | |
9b296c62 PB |
2752 | if (!(ctx->flags & IORING_SETUP_NO_SQARRAY)) |
2753 | static_branch_dec(&io_key_has_sqarray); | |
2754 | ||
2b188cc1 | 2755 | percpu_ref_exit(&ctx->refs); |
2b188cc1 | 2756 | free_uid(ctx->user); |
4010fec4 | 2757 | io_req_caches_free(ctx); |
63de899c PB |
2758 | |
2759 | WARN_ON_ONCE(ctx->nr_req_allocated); | |
2760 | ||
e941894e JA |
2761 | if (ctx->hash_map) |
2762 | io_wq_put_hash(ctx->hash_map); | |
8d0c12a8 | 2763 | io_napi_free(ctx); |
b6b3eb19 | 2764 | kvfree(ctx->cancel_table.hbs); |
9cfc7e94 | 2765 | xa_destroy(&ctx->io_bl_xa); |
2b188cc1 JA |
2766 | kfree(ctx); |
2767 | } | |
2768 | ||
bca39f39 PB |
2769 | static __cold void io_activate_pollwq_cb(struct callback_head *cb) |
2770 | { | |
2771 | struct io_ring_ctx *ctx = container_of(cb, struct io_ring_ctx, | |
2772 | poll_wq_task_work); | |
2773 | ||
2774 | mutex_lock(&ctx->uring_lock); | |
2775 | ctx->poll_activated = true; | |
2776 | mutex_unlock(&ctx->uring_lock); | |
2777 | ||
2778 | /* | |
2779 | * Wake ups for some events between start of polling and activation | |
2780 | * might've been lost due to loose synchronisation. | |
2781 | */ | |
2782 | wake_up_all(&ctx->poll_wq); | |
2783 | percpu_ref_put(&ctx->refs); | |
2784 | } | |
2785 | ||
c4320315 | 2786 | __cold void io_activate_pollwq(struct io_ring_ctx *ctx) |
bca39f39 PB |
2787 | { |
2788 | spin_lock(&ctx->completion_lock); | |
2789 | /* already activated or in progress */ | |
2790 | if (ctx->poll_activated || ctx->poll_wq_task_work.func) | |
2791 | goto out; | |
2792 | if (WARN_ON_ONCE(!ctx->task_complete)) | |
2793 | goto out; | |
2794 | if (!ctx->submitter_task) | |
2795 | goto out; | |
2796 | /* | |
2797 | * with ->submitter_task only the submitter task completes requests, we | |
2798 | * only need to sync with it, which is done by injecting a tw | |
2799 | */ | |
2800 | init_task_work(&ctx->poll_wq_task_work, io_activate_pollwq_cb); | |
2801 | percpu_ref_get(&ctx->refs); | |
2802 | if (task_work_add(ctx->submitter_task, &ctx->poll_wq_task_work, TWA_SIGNAL)) | |
2803 | percpu_ref_put(&ctx->refs); | |
2804 | out: | |
2805 | spin_unlock(&ctx->completion_lock); | |
2806 | } | |
2807 | ||
2b188cc1 JA |
2808 | static __poll_t io_uring_poll(struct file *file, poll_table *wait) |
2809 | { | |
2810 | struct io_ring_ctx *ctx = file->private_data; | |
2811 | __poll_t mask = 0; | |
2812 | ||
bca39f39 PB |
2813 | if (unlikely(!ctx->poll_activated)) |
2814 | io_activate_pollwq(ctx); | |
4f7067c3 | 2815 | /* |
4e15fa83 ON |
2816 | * provides mb() which pairs with barrier from wq_has_sleeper |
2817 | * call in io_commit_cqring | |
4f7067c3 | 2818 | */ |
4e15fa83 ON |
2819 | poll_wait(file, &ctx->poll_wq, wait); |
2820 | ||
90554200 | 2821 | if (!io_sqring_full(ctx)) |
2b188cc1 | 2822 | mask |= EPOLLOUT | EPOLLWRNORM; |
ed670c3f HX |
2823 | |
2824 | /* | |
2825 | * Don't flush cqring overflow list here, just do a simple check. | |
2826 | * Otherwise there could possible be ABBA deadlock: | |
2827 | * CPU0 CPU1 | |
2828 | * ---- ---- | |
2829 | * lock(&ctx->uring_lock); | |
2830 | * lock(&ep->mtx); | |
2831 | * lock(&ctx->uring_lock); | |
2832 | * lock(&ep->mtx); | |
2833 | * | |
2834 | * Users may get EPOLLIN meanwhile seeing nothing in cqring, this | |
10d8bc35 | 2835 | * pushes them to do the flush. |
ed670c3f | 2836 | */ |
b4c98d59 | 2837 | |
c10bb646 | 2838 | if (__io_cqring_events_user(ctx) || io_has_work(ctx)) |
2b188cc1 JA |
2839 | mask |= EPOLLIN | EPOLLRDNORM; |
2840 | ||
2841 | return mask; | |
2842 | } | |
2843 | ||
d56d938b PB |
2844 | struct io_tctx_exit { |
2845 | struct callback_head task_work; | |
2846 | struct completion completion; | |
baf186c4 | 2847 | struct io_ring_ctx *ctx; |
d56d938b PB |
2848 | }; |
2849 | ||
c072481d | 2850 | static __cold void io_tctx_exit_cb(struct callback_head *cb) |
d56d938b PB |
2851 | { |
2852 | struct io_uring_task *tctx = current->io_uring; | |
2853 | struct io_tctx_exit *work; | |
2854 | ||
2855 | work = container_of(cb, struct io_tctx_exit, task_work); | |
2856 | /* | |
8d664282 | 2857 | * When @in_cancel, we're in cancellation and it's racy to remove the |
d56d938b | 2858 | * node. It'll be removed by the end of cancellation, just ignore it. |
998b30c3 HM |
2859 | * tctx can be NULL if the queueing of this task_work raced with |
2860 | * work cancelation off the exec path. | |
d56d938b | 2861 | */ |
8d664282 | 2862 | if (tctx && !atomic_read(&tctx->in_cancel)) |
eef51daa | 2863 | io_uring_del_tctx_node((unsigned long)work->ctx); |
d56d938b PB |
2864 | complete(&work->completion); |
2865 | } | |
2866 | ||
c072481d | 2867 | static __cold bool io_cancel_ctx_cb(struct io_wq_work *work, void *data) |
28090c13 PB |
2868 | { |
2869 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
2870 | ||
2871 | return req->ctx == data; | |
2872 | } | |
2873 | ||
c072481d | 2874 | static __cold void io_ring_exit_work(struct work_struct *work) |
85faa7b8 | 2875 | { |
d56d938b | 2876 | struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx, exit_work); |
b5bb3a24 | 2877 | unsigned long timeout = jiffies + HZ * 60 * 5; |
58d3be2c | 2878 | unsigned long interval = HZ / 20; |
d56d938b PB |
2879 | struct io_tctx_exit exit; |
2880 | struct io_tctx_node *node; | |
2881 | int ret; | |
85faa7b8 | 2882 | |
56952e91 JA |
2883 | /* |
2884 | * If we're doing polled IO and end up having requests being | |
2885 | * submitted async (out-of-line), then completions can come in while | |
2886 | * we're waiting for refs to drop. We need to reap these manually, | |
2887 | * as nobody else will be looking for them. | |
2888 | */ | |
b2edc0a7 | 2889 | do { |
a85381d8 PB |
2890 | if (test_bit(IO_CHECK_CQ_OVERFLOW_BIT, &ctx->check_cq)) { |
2891 | mutex_lock(&ctx->uring_lock); | |
2892 | io_cqring_overflow_kill(ctx); | |
2893 | mutex_unlock(&ctx->uring_lock); | |
2894 | } | |
76f1cc98 | 2895 | if (!xa_empty(&ctx->zcrx_ctxs)) { |
6f377873 DW |
2896 | mutex_lock(&ctx->uring_lock); |
2897 | io_shutdown_zcrx_ifqs(ctx); | |
2898 | mutex_unlock(&ctx->uring_lock); | |
2899 | } | |
a85381d8 | 2900 | |
c0e0d6ba DY |
2901 | if (ctx->flags & IORING_SETUP_DEFER_TASKRUN) |
2902 | io_move_task_work_from_local(ctx); | |
2903 | ||
a13030fd BQM |
2904 | /* The SQPOLL thread never reaches this path */ |
2905 | while (io_uring_try_cancel_requests(ctx, NULL, true, false)) | |
affa87db PB |
2906 | cond_resched(); |
2907 | ||
28090c13 PB |
2908 | if (ctx->sq_data) { |
2909 | struct io_sq_data *sqd = ctx->sq_data; | |
2910 | struct task_struct *tsk; | |
2911 | ||
2912 | io_sq_thread_park(sqd); | |
c538f400 | 2913 | tsk = sqpoll_task_locked(sqd); |
28090c13 PB |
2914 | if (tsk && tsk->io_uring && tsk->io_uring->io_wq) |
2915 | io_wq_cancel_cb(tsk->io_uring->io_wq, | |
2916 | io_cancel_ctx_cb, ctx, true); | |
2917 | io_sq_thread_unpark(sqd); | |
2918 | } | |
b5bb3a24 | 2919 | |
37f0e767 PB |
2920 | io_req_caches_free(ctx); |
2921 | ||
58d3be2c PB |
2922 | if (WARN_ON_ONCE(time_after(jiffies, timeout))) { |
2923 | /* there is little hope left, don't run it too often */ | |
2924 | interval = HZ * 60; | |
2925 | } | |
4826c594 JA |
2926 | /* |
2927 | * This is really an uninterruptible wait, as it has to be | |
2928 | * complete. But it's also run from a kworker, which doesn't | |
2929 | * take signals, so it's fine to make it interruptible. This | |
2930 | * avoids scenarios where we knowingly can wait much longer | |
2931 | * on completions, for example if someone does a SIGSTOP on | |
2932 | * a task that needs to finish task_work to make this loop | |
2933 | * complete. That's a synthetic situation that should not | |
2934 | * cause a stuck task backtrace, and hence a potential panic | |
2935 | * on stuck tasks if that is enabled. | |
2936 | */ | |
2937 | } while (!wait_for_completion_interruptible_timeout(&ctx->ref_comp, interval)); | |
d56d938b | 2938 | |
7f00651a PB |
2939 | init_completion(&exit.completion); |
2940 | init_task_work(&exit.task_work, io_tctx_exit_cb); | |
2941 | exit.ctx = ctx; | |
f7b32e78 | 2942 | |
d56d938b PB |
2943 | mutex_lock(&ctx->uring_lock); |
2944 | while (!list_empty(&ctx->tctx_list)) { | |
b5bb3a24 PB |
2945 | WARN_ON_ONCE(time_after(jiffies, timeout)); |
2946 | ||
d56d938b PB |
2947 | node = list_first_entry(&ctx->tctx_list, struct io_tctx_node, |
2948 | ctx_node); | |
7f00651a PB |
2949 | /* don't spin on a single task if cancellation failed */ |
2950 | list_rotate_left(&ctx->tctx_list); | |
d56d938b PB |
2951 | ret = task_work_add(node->task, &exit.task_work, TWA_SIGNAL); |
2952 | if (WARN_ON_ONCE(ret)) | |
2953 | continue; | |
d56d938b PB |
2954 | |
2955 | mutex_unlock(&ctx->uring_lock); | |
4826c594 JA |
2956 | /* |
2957 | * See comment above for | |
2958 | * wait_for_completion_interruptible_timeout() on why this | |
2959 | * wait is marked as interruptible. | |
2960 | */ | |
2961 | wait_for_completion_interruptible(&exit.completion); | |
d56d938b PB |
2962 | mutex_lock(&ctx->uring_lock); |
2963 | } | |
2964 | mutex_unlock(&ctx->uring_lock); | |
79ebeaee JA |
2965 | spin_lock(&ctx->completion_lock); |
2966 | spin_unlock(&ctx->completion_lock); | |
d56d938b | 2967 | |
d73a572d PB |
2968 | /* pairs with RCU read section in io_req_local_work_add() */ |
2969 | if (ctx->flags & IORING_SETUP_DEFER_TASKRUN) | |
2970 | synchronize_rcu(); | |
2971 | ||
85faa7b8 JA |
2972 | io_ring_ctx_free(ctx); |
2973 | } | |
2974 | ||
c072481d | 2975 | static __cold void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx) |
2b188cc1 | 2976 | { |
61cf9370 MWO |
2977 | unsigned long index; |
2978 | struct creds *creds; | |
2979 | ||
2b188cc1 JA |
2980 | mutex_lock(&ctx->uring_lock); |
2981 | percpu_ref_kill(&ctx->refs); | |
61cf9370 MWO |
2982 | xa_for_each(&ctx->personalities, index, creds) |
2983 | io_unregister_personality(ctx, index); | |
2b188cc1 JA |
2984 | mutex_unlock(&ctx->uring_lock); |
2985 | ||
dfbe5561 JA |
2986 | flush_delayed_work(&ctx->fallback_work); |
2987 | ||
85faa7b8 | 2988 | INIT_WORK(&ctx->exit_work, io_ring_exit_work); |
fc666777 JA |
2989 | /* |
2990 | * Use system_unbound_wq to avoid spawning tons of event kworkers | |
2991 | * if we're exiting a ton of rings at the same time. It just adds | |
2992 | * noise and overhead, there's no discernable change in runtime | |
2993 | * over using system_wq. | |
2994 | */ | |
73eaa2b5 | 2995 | queue_work(iou_wq, &ctx->exit_work); |
2b188cc1 JA |
2996 | } |
2997 | ||
2998 | static int io_uring_release(struct inode *inode, struct file *file) | |
2999 | { | |
3000 | struct io_ring_ctx *ctx = file->private_data; | |
3001 | ||
3002 | file->private_data = NULL; | |
3003 | io_ring_ctx_wait_and_kill(ctx); | |
3004 | return 0; | |
3005 | } | |
3006 | ||
f6edbabb | 3007 | struct io_task_cancel { |
f03baece | 3008 | struct io_uring_task *tctx; |
3dd0c97a | 3009 | bool all; |
f6edbabb | 3010 | }; |
f254ac04 | 3011 | |
f6edbabb | 3012 | static bool io_cancel_task_cb(struct io_wq_work *work, void *data) |
b711d4ea | 3013 | { |
9a472ef7 | 3014 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
f6edbabb | 3015 | struct io_task_cancel *cancel = data; |
9a472ef7 | 3016 | |
f03baece | 3017 | return io_match_task_safe(req, cancel->tctx, cancel->all); |
b711d4ea JA |
3018 | } |
3019 | ||
c072481d | 3020 | static __cold bool io_cancel_defer_files(struct io_ring_ctx *ctx, |
f03baece | 3021 | struct io_uring_task *tctx, |
c072481d | 3022 | bool cancel_all) |
b7ddce3c | 3023 | { |
e1915f76 | 3024 | struct io_defer_entry *de; |
b7ddce3c PB |
3025 | LIST_HEAD(list); |
3026 | ||
b7ddce3c | 3027 | list_for_each_entry_reverse(de, &ctx->defer_list, list) { |
f03baece | 3028 | if (io_match_task_safe(de->req, tctx, cancel_all)) { |
b7ddce3c PB |
3029 | list_cut_position(&list, &ctx->defer_list, &de->list); |
3030 | break; | |
3031 | } | |
3032 | } | |
e1915f76 PB |
3033 | if (list_empty(&list)) |
3034 | return false; | |
b7ddce3c PB |
3035 | |
3036 | while (!list_empty(&list)) { | |
3037 | de = list_first_entry(&list, struct io_defer_entry, list); | |
3038 | list_del_init(&de->list); | |
8fb7aee0 | 3039 | ctx->nr_drained -= io_linked_nr(de->req); |
e276ae34 | 3040 | io_req_task_queue_fail(de->req, -ECANCELED); |
b7ddce3c PB |
3041 | kfree(de); |
3042 | } | |
e1915f76 | 3043 | return true; |
b7ddce3c PB |
3044 | } |
3045 | ||
c072481d | 3046 | static __cold bool io_uring_try_cancel_iowq(struct io_ring_ctx *ctx) |
1b00764f PB |
3047 | { |
3048 | struct io_tctx_node *node; | |
3049 | enum io_wq_cancel cret; | |
3050 | bool ret = false; | |
3051 | ||
3052 | mutex_lock(&ctx->uring_lock); | |
3053 | list_for_each_entry(node, &ctx->tctx_list, ctx_node) { | |
3054 | struct io_uring_task *tctx = node->task->io_uring; | |
3055 | ||
3056 | /* | |
3057 | * io_wq will stay alive while we hold uring_lock, because it's | |
3058 | * killed after ctx nodes, which requires to take the lock. | |
3059 | */ | |
3060 | if (!tctx || !tctx->io_wq) | |
3061 | continue; | |
3062 | cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_ctx_cb, ctx, true); | |
3063 | ret |= (cret != IO_WQ_CANCEL_NOTFOUND); | |
3064 | } | |
3065 | mutex_unlock(&ctx->uring_lock); | |
3066 | ||
3067 | return ret; | |
3068 | } | |
3069 | ||
affa87db | 3070 | static __cold bool io_uring_try_cancel_requests(struct io_ring_ctx *ctx, |
f03baece | 3071 | struct io_uring_task *tctx, |
a13030fd BQM |
3072 | bool cancel_all, |
3073 | bool is_sqpoll_thread) | |
9936c7c2 | 3074 | { |
f03baece | 3075 | struct io_task_cancel cancel = { .tctx = tctx, .all = cancel_all, }; |
affa87db PB |
3076 | enum io_wq_cancel cret; |
3077 | bool ret = false; | |
9936c7c2 | 3078 | |
360cd42c PB |
3079 | /* set it so io_req_local_work_add() would wake us up */ |
3080 | if (ctx->flags & IORING_SETUP_DEFER_TASKRUN) { | |
3081 | atomic_set(&ctx->cq_wait_nr, 1); | |
3082 | smp_mb(); | |
3083 | } | |
3084 | ||
60053be8 PB |
3085 | /* failed during ring init, it couldn't have issued any requests */ |
3086 | if (!ctx->rings) | |
affa87db | 3087 | return false; |
60053be8 | 3088 | |
f03baece | 3089 | if (!tctx) { |
affa87db | 3090 | ret |= io_uring_try_cancel_iowq(ctx); |
f03baece | 3091 | } else if (tctx->io_wq) { |
affa87db PB |
3092 | /* |
3093 | * Cancels requests of all rings, not only @ctx, but | |
3094 | * it's fine as the task is in exit/exec. | |
3095 | */ | |
3096 | cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_task_cb, | |
3097 | &cancel, true); | |
3098 | ret |= (cret != IO_WQ_CANCEL_NOTFOUND); | |
3099 | } | |
9936c7c2 | 3100 | |
affa87db PB |
3101 | /* SQPOLL thread does its own polling */ |
3102 | if ((!(ctx->flags & IORING_SETUP_SQPOLL) && cancel_all) || | |
a13030fd | 3103 | is_sqpoll_thread) { |
affa87db PB |
3104 | while (!wq_list_empty(&ctx->iopoll_list)) { |
3105 | io_iopoll_try_reap_events(ctx); | |
3106 | ret = true; | |
fcc926bb | 3107 | cond_resched(); |
9936c7c2 | 3108 | } |
9936c7c2 | 3109 | } |
affa87db | 3110 | |
140102ae PB |
3111 | if ((ctx->flags & IORING_SETUP_DEFER_TASKRUN) && |
3112 | io_allowed_defer_tw_run(ctx)) | |
49c5c63d | 3113 | ret |= io_run_local_work(ctx, INT_MAX, INT_MAX) > 0; |
affa87db | 3114 | mutex_lock(&ctx->uring_lock); |
8fb7aee0 | 3115 | ret |= io_cancel_defer_files(ctx, tctx, cancel_all); |
f03baece JA |
3116 | ret |= io_poll_remove_all(ctx, tctx, cancel_all); |
3117 | ret |= io_waitid_remove_all(ctx, tctx, cancel_all); | |
3118 | ret |= io_futex_remove_all(ctx, tctx, cancel_all); | |
3119 | ret |= io_uring_try_cancel_uring_cmd(ctx, tctx, cancel_all); | |
affa87db | 3120 | mutex_unlock(&ctx->uring_lock); |
f03baece JA |
3121 | ret |= io_kill_timeouts(ctx, tctx, cancel_all); |
3122 | if (tctx) | |
c0e0d6ba | 3123 | ret |= io_run_task_work() > 0; |
25417623 JA |
3124 | else |
3125 | ret |= flush_delayed_work(&ctx->fallback_work); | |
affa87db | 3126 | return ret; |
9936c7c2 PB |
3127 | } |
3128 | ||
3f48cf18 | 3129 | static s64 tctx_inflight(struct io_uring_task *tctx, bool tracked) |
521d6a73 | 3130 | { |
3f48cf18 | 3131 | if (tracked) |
9cae36a0 | 3132 | return atomic_read(&tctx->inflight_tracked); |
521d6a73 PB |
3133 | return percpu_counter_sum(&tctx->inflight); |
3134 | } | |
3135 | ||
78cc687b PB |
3136 | /* |
3137 | * Find any io_uring ctx that this task has registered or done IO on, and cancel | |
78a78060 | 3138 | * requests. @sqd should be not-null IFF it's an SQPOLL thread cancellation. |
78cc687b | 3139 | */ |
17437f31 | 3140 | __cold void io_uring_cancel_generic(bool cancel_all, struct io_sq_data *sqd) |
0e9ddb39 | 3141 | { |
521d6a73 | 3142 | struct io_uring_task *tctx = current->io_uring; |
734551df | 3143 | struct io_ring_ctx *ctx; |
360cd42c PB |
3144 | struct io_tctx_node *node; |
3145 | unsigned long index; | |
0e9ddb39 PB |
3146 | s64 inflight; |
3147 | DEFINE_WAIT(wait); | |
fdaf083c | 3148 | |
c538f400 | 3149 | WARN_ON_ONCE(sqd && sqpoll_task_locked(sqd) != current); |
78cc687b | 3150 | |
6d042ffb PO |
3151 | if (!current->io_uring) |
3152 | return; | |
17a91051 PB |
3153 | if (tctx->io_wq) |
3154 | io_wq_exit_start(tctx->io_wq); | |
3155 | ||
8d664282 | 3156 | atomic_inc(&tctx->in_cancel); |
0e9ddb39 | 3157 | do { |
affa87db PB |
3158 | bool loop = false; |
3159 | ||
e9dbe221 | 3160 | io_uring_drop_tctx_refs(current); |
f8b632e8 PB |
3161 | if (!tctx_inflight(tctx, !cancel_all)) |
3162 | break; | |
3163 | ||
0e9ddb39 | 3164 | /* read completions before cancelations */ |
f8b632e8 | 3165 | inflight = tctx_inflight(tctx, false); |
0e9ddb39 PB |
3166 | if (!inflight) |
3167 | break; | |
fdaf083c | 3168 | |
78cc687b | 3169 | if (!sqd) { |
78cc687b PB |
3170 | xa_for_each(&tctx->xa, index, node) { |
3171 | /* sqpoll task will cancel all its requests */ | |
3172 | if (node->ctx->sq_data) | |
3173 | continue; | |
affa87db | 3174 | loop |= io_uring_try_cancel_requests(node->ctx, |
f03baece | 3175 | current->io_uring, |
a13030fd BQM |
3176 | cancel_all, |
3177 | false); | |
78cc687b PB |
3178 | } |
3179 | } else { | |
3180 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) | |
affa87db | 3181 | loop |= io_uring_try_cancel_requests(ctx, |
f03baece | 3182 | current->io_uring, |
a13030fd BQM |
3183 | cancel_all, |
3184 | true); | |
affa87db PB |
3185 | } |
3186 | ||
3187 | if (loop) { | |
3188 | cond_resched(); | |
3189 | continue; | |
78cc687b | 3190 | } |
17a91051 | 3191 | |
78a78060 JA |
3192 | prepare_to_wait(&tctx->wait, &wait, TASK_INTERRUPTIBLE); |
3193 | io_run_task_work(); | |
e9dbe221 | 3194 | io_uring_drop_tctx_refs(current); |
360cd42c | 3195 | xa_for_each(&tctx->xa, index, node) { |
40cfe553 | 3196 | if (io_local_work_pending(node->ctx)) { |
360cd42c PB |
3197 | WARN_ON_ONCE(node->ctx->submitter_task && |
3198 | node->ctx->submitter_task != current); | |
3199 | goto end_wait; | |
3200 | } | |
3201 | } | |
0f212204 | 3202 | /* |
a1bb3cd5 PB |
3203 | * If we've seen completions, retry without waiting. This |
3204 | * avoids a race where a completion comes in before we did | |
3205 | * prepare_to_wait(). | |
0f212204 | 3206 | */ |
3dd0c97a | 3207 | if (inflight == tctx_inflight(tctx, !cancel_all)) |
a1bb3cd5 | 3208 | schedule(); |
360cd42c | 3209 | end_wait: |
f57555ed | 3210 | finish_wait(&tctx->wait, &wait); |
d8a6df10 | 3211 | } while (1); |
de7f1d9e | 3212 | |
8452d4a6 | 3213 | io_uring_clean_tctx(tctx); |
3dd0c97a | 3214 | if (cancel_all) { |
3cc7fdb9 PB |
3215 | /* |
3216 | * We shouldn't run task_works after cancel, so just leave | |
8d664282 | 3217 | * ->in_cancel set for normal exit. |
3cc7fdb9 | 3218 | */ |
8d664282 | 3219 | atomic_dec(&tctx->in_cancel); |
3f48cf18 PB |
3220 | /* for exec all current's requests should be gone, kill tctx */ |
3221 | __io_uring_free(current); | |
3222 | } | |
44e728b8 PB |
3223 | } |
3224 | ||
f552a27a | 3225 | void __io_uring_cancel(bool cancel_all) |
78cc687b | 3226 | { |
12d90811 | 3227 | io_uring_unreg_ringfd(); |
f552a27a | 3228 | io_uring_cancel_generic(cancel_all, NULL); |
78cc687b PB |
3229 | } |
3230 | ||
aa00f67a JA |
3231 | static struct io_uring_reg_wait *io_get_ext_arg_reg(struct io_ring_ctx *ctx, |
3232 | const struct io_uring_getevents_arg __user *uarg) | |
f81440d3 | 3233 | { |
d617b314 PB |
3234 | unsigned long size = sizeof(struct io_uring_reg_wait); |
3235 | unsigned long offset = (uintptr_t)uarg; | |
3236 | unsigned long end; | |
f81440d3 | 3237 | |
d617b314 PB |
3238 | if (unlikely(offset % sizeof(long))) |
3239 | return ERR_PTR(-EFAULT); | |
3240 | ||
3241 | /* also protects from NULL ->cq_wait_arg as the size would be 0 */ | |
3242 | if (unlikely(check_add_overflow(offset, size, &end) || | |
3243 | end > ctx->cq_wait_size)) | |
3244 | return ERR_PTR(-EFAULT); | |
3245 | ||
29b95ac9 | 3246 | offset = array_index_nospec(offset, ctx->cq_wait_size - size); |
d617b314 | 3247 | return ctx->cq_wait_arg + offset; |
aa00f67a JA |
3248 | } |
3249 | ||
3250 | static int io_validate_ext_arg(struct io_ring_ctx *ctx, unsigned flags, | |
3251 | const void __user *argp, size_t argsz) | |
3252 | { | |
3253 | struct io_uring_getevents_arg arg; | |
3254 | ||
3255 | if (!(flags & IORING_ENTER_EXT_ARG)) | |
3256 | return 0; | |
3730aebb PB |
3257 | if (flags & IORING_ENTER_EXT_ARG_REG) |
3258 | return -EINVAL; | |
aa00f67a JA |
3259 | if (argsz != sizeof(arg)) |
3260 | return -EINVAL; | |
3261 | if (copy_from_user(&arg, argp, sizeof(arg))) | |
3262 | return -EFAULT; | |
f81440d3 PB |
3263 | return 0; |
3264 | } | |
3265 | ||
aa00f67a JA |
3266 | static int io_get_ext_arg(struct io_ring_ctx *ctx, unsigned flags, |
3267 | const void __user *argp, struct ext_arg *ext_arg) | |
c73ebb68 | 3268 | { |
371b47da | 3269 | const struct io_uring_getevents_arg __user *uarg = argp; |
c73ebb68 HX |
3270 | struct io_uring_getevents_arg arg; |
3271 | ||
07754bfd JA |
3272 | ext_arg->iowait = !(flags & IORING_ENTER_NO_IOWAIT); |
3273 | ||
c73ebb68 HX |
3274 | /* |
3275 | * If EXT_ARG isn't set, then we have no timespec and the argp pointer | |
3276 | * is just a pointer to the sigset_t. | |
3277 | */ | |
3278 | if (!(flags & IORING_ENTER_EXT_ARG)) { | |
f42b58e4 | 3279 | ext_arg->sig = (const sigset_t __user *) argp; |
c73ebb68 HX |
3280 | return 0; |
3281 | } | |
3282 | ||
aa00f67a JA |
3283 | if (flags & IORING_ENTER_EXT_ARG_REG) { |
3284 | struct io_uring_reg_wait *w; | |
3285 | ||
3286 | if (ext_arg->argsz != sizeof(struct io_uring_reg_wait)) | |
3287 | return -EINVAL; | |
3288 | w = io_get_ext_arg_reg(ctx, argp); | |
3289 | if (IS_ERR(w)) | |
3290 | return PTR_ERR(w); | |
3291 | ||
3292 | if (w->flags & ~IORING_REG_WAIT_TS) | |
3293 | return -EINVAL; | |
3294 | ext_arg->min_time = READ_ONCE(w->min_wait_usec) * NSEC_PER_USEC; | |
3295 | ext_arg->sig = u64_to_user_ptr(READ_ONCE(w->sigmask)); | |
3296 | ext_arg->argsz = READ_ONCE(w->sigmask_sz); | |
3297 | if (w->flags & IORING_REG_WAIT_TS) { | |
3298 | ext_arg->ts.tv_sec = READ_ONCE(w->ts.tv_sec); | |
3299 | ext_arg->ts.tv_nsec = READ_ONCE(w->ts.tv_nsec); | |
3300 | ext_arg->ts_set = true; | |
3301 | } | |
c73ebb68 HX |
3302 | return 0; |
3303 | } | |
3304 | ||
3305 | /* | |
3306 | * EXT_ARG is set - ensure we agree on the size of it and copy in our | |
3307 | * timespec and sigset_t pointers if good. | |
3308 | */ | |
f42b58e4 | 3309 | if (ext_arg->argsz != sizeof(arg)) |
c73ebb68 | 3310 | return -EINVAL; |
371b47da JA |
3311 | #ifdef CONFIG_64BIT |
3312 | if (!user_access_begin(uarg, sizeof(*uarg))) | |
c73ebb68 | 3313 | return -EFAULT; |
371b47da JA |
3314 | unsafe_get_user(arg.sigmask, &uarg->sigmask, uaccess_end); |
3315 | unsafe_get_user(arg.sigmask_sz, &uarg->sigmask_sz, uaccess_end); | |
3316 | unsafe_get_user(arg.min_wait_usec, &uarg->min_wait_usec, uaccess_end); | |
3317 | unsafe_get_user(arg.ts, &uarg->ts, uaccess_end); | |
3318 | user_access_end(); | |
3319 | #else | |
3320 | if (copy_from_user(&arg, uarg, sizeof(arg))) | |
3321 | return -EFAULT; | |
3322 | #endif | |
7ed9e09e | 3323 | ext_arg->min_time = arg.min_wait_usec * NSEC_PER_USEC; |
f42b58e4 JA |
3324 | ext_arg->sig = u64_to_user_ptr(arg.sigmask); |
3325 | ext_arg->argsz = arg.sigmask_sz; | |
0a54a7dd JA |
3326 | if (arg.ts) { |
3327 | if (get_timespec64(&ext_arg->ts, u64_to_user_ptr(arg.ts))) | |
3328 | return -EFAULT; | |
3329 | ext_arg->ts_set = true; | |
3330 | } | |
c73ebb68 | 3331 | return 0; |
371b47da JA |
3332 | #ifdef CONFIG_64BIT |
3333 | uaccess_end: | |
3334 | user_access_end(); | |
3335 | return -EFAULT; | |
3336 | #endif | |
c73ebb68 HX |
3337 | } |
3338 | ||
2b188cc1 | 3339 | SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, |
c73ebb68 HX |
3340 | u32, min_complete, u32, flags, const void __user *, argp, |
3341 | size_t, argsz) | |
2b188cc1 JA |
3342 | { |
3343 | struct io_ring_ctx *ctx; | |
73363c26 | 3344 | struct file *file; |
33f993da | 3345 | long ret; |
2b188cc1 | 3346 | |
33f993da | 3347 | if (unlikely(flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP | |
e7a6c00d | 3348 | IORING_ENTER_SQ_WAIT | IORING_ENTER_EXT_ARG | |
d29cb372 | 3349 | IORING_ENTER_REGISTERED_RING | |
aa00f67a | 3350 | IORING_ENTER_ABS_TIMER | |
07754bfd JA |
3351 | IORING_ENTER_EXT_ARG_REG | |
3352 | IORING_ENTER_NO_IOWAIT))) | |
2b188cc1 JA |
3353 | return -EINVAL; |
3354 | ||
e7a6c00d JA |
3355 | /* |
3356 | * Ring fd has been registered via IORING_REGISTER_RING_FDS, we | |
3357 | * need only dereference our task private array to find it. | |
3358 | */ | |
3359 | if (flags & IORING_ENTER_REGISTERED_RING) { | |
3360 | struct io_uring_task *tctx = current->io_uring; | |
3361 | ||
3273c440 | 3362 | if (unlikely(!tctx || fd >= IO_RINGFD_REG_MAX)) |
e7a6c00d JA |
3363 | return -EINVAL; |
3364 | fd = array_index_nospec(fd, IO_RINGFD_REG_MAX); | |
73363c26 JA |
3365 | file = tctx->registered_rings[fd]; |
3366 | if (unlikely(!file)) | |
3273c440 | 3367 | return -EBADF; |
e7a6c00d | 3368 | } else { |
73363c26 JA |
3369 | file = fget(fd); |
3370 | if (unlikely(!file)) | |
3273c440 PB |
3371 | return -EBADF; |
3372 | ret = -EOPNOTSUPP; | |
73363c26 | 3373 | if (unlikely(!io_is_uring_fops(file))) |
fbb8bb02 | 3374 | goto out; |
e7a6c00d | 3375 | } |
2b188cc1 | 3376 | |
73363c26 | 3377 | ctx = file->private_data; |
7e84e1c7 | 3378 | ret = -EBADFD; |
33f993da | 3379 | if (unlikely(ctx->flags & IORING_SETUP_R_DISABLED)) |
7e84e1c7 SG |
3380 | goto out; |
3381 | ||
6c271ce2 JA |
3382 | /* |
3383 | * For SQ polling, the thread will do all submissions and completions. | |
3384 | * Just return the requested submit count, and wake the thread if | |
3385 | * we were asked to. | |
3386 | */ | |
b2a9eada | 3387 | ret = 0; |
6c271ce2 | 3388 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
21f96522 JA |
3389 | if (unlikely(ctx->sq_data->thread == NULL)) { |
3390 | ret = -EOWNERDEAD; | |
04147488 | 3391 | goto out; |
21f96522 | 3392 | } |
6c271ce2 | 3393 | if (flags & IORING_ENTER_SQ_WAKEUP) |
534ca6d6 | 3394 | wake_up(&ctx->sq_data->wait); |
88b80534 QF |
3395 | if (flags & IORING_ENTER_SQ_WAIT) |
3396 | io_sqpoll_wait_sq(ctx); | |
3397 | ||
3e813c90 | 3398 | ret = to_submit; |
b2a9eada | 3399 | } else if (to_submit) { |
eef51daa | 3400 | ret = io_uring_add_tctx_node(ctx); |
0f212204 JA |
3401 | if (unlikely(ret)) |
3402 | goto out; | |
7c504e65 | 3403 | |
2b188cc1 | 3404 | mutex_lock(&ctx->uring_lock); |
3e813c90 DY |
3405 | ret = io_submit_sqes(ctx, to_submit); |
3406 | if (ret != to_submit) { | |
d487b43c | 3407 | mutex_unlock(&ctx->uring_lock); |
7c504e65 | 3408 | goto out; |
d487b43c | 3409 | } |
44f87745 PB |
3410 | if (flags & IORING_ENTER_GETEVENTS) { |
3411 | if (ctx->syscall_iopoll) | |
3412 | goto iopoll_locked; | |
3413 | /* | |
3414 | * Ignore errors, we'll soon call io_cqring_wait() and | |
3415 | * it should handle ownership problems if any. | |
3416 | */ | |
3417 | if (ctx->flags & IORING_SETUP_DEFER_TASKRUN) | |
9fe3eaea | 3418 | (void)io_run_local_work_locked(ctx, min_complete); |
44f87745 | 3419 | } |
d487b43c | 3420 | mutex_unlock(&ctx->uring_lock); |
2b188cc1 | 3421 | } |
c0e0d6ba | 3422 | |
2b188cc1 | 3423 | if (flags & IORING_ENTER_GETEVENTS) { |
3e813c90 | 3424 | int ret2; |
c0e0d6ba | 3425 | |
773697b6 | 3426 | if (ctx->syscall_iopoll) { |
d487b43c PB |
3427 | /* |
3428 | * We disallow the app entering submit/complete with | |
3429 | * polling, but we still need to lock the ring to | |
3430 | * prevent racing with polled issue that got punted to | |
3431 | * a workqueue. | |
3432 | */ | |
3433 | mutex_lock(&ctx->uring_lock); | |
3434 | iopoll_locked: | |
aa00f67a | 3435 | ret2 = io_validate_ext_arg(ctx, flags, argp, argsz); |
81661978 | 3436 | if (likely(!ret2)) |
3e813c90 | 3437 | ret2 = io_iopoll_check(ctx, min_complete); |
d487b43c | 3438 | mutex_unlock(&ctx->uring_lock); |
def596e9 | 3439 | } else { |
f42b58e4 | 3440 | struct ext_arg ext_arg = { .argsz = argsz }; |
f81440d3 | 3441 | |
aa00f67a | 3442 | ret2 = io_get_ext_arg(ctx, flags, argp, &ext_arg); |
81661978 | 3443 | if (likely(!ret2)) |
d29cb372 | 3444 | ret2 = io_cqring_wait(ctx, min_complete, flags, |
f42b58e4 | 3445 | &ext_arg); |
def596e9 | 3446 | } |
c73ebb68 | 3447 | |
155bc950 | 3448 | if (!ret) { |
3e813c90 | 3449 | ret = ret2; |
2b188cc1 | 3450 | |
155bc950 DY |
3451 | /* |
3452 | * EBADR indicates that one or more CQE were dropped. | |
3453 | * Once the user has been informed we can clear the bit | |
3454 | * as they are obviously ok with those drops. | |
3455 | */ | |
3456 | if (unlikely(ret2 == -EBADR)) | |
3457 | clear_bit(IO_CHECK_CQ_DROPPED_BIT, | |
3458 | &ctx->check_cq); | |
def596e9 | 3459 | } |
2b188cc1 | 3460 | } |
7c504e65 | 3461 | out: |
73363c26 JA |
3462 | if (!(flags & IORING_ENTER_REGISTERED_RING)) |
3463 | fput(file); | |
3e813c90 | 3464 | return ret; |
2b188cc1 JA |
3465 | } |
3466 | ||
3467 | static const struct file_operations io_uring_fops = { | |
3468 | .release = io_uring_release, | |
3469 | .mmap = io_uring_mmap, | |
f15ed8b4 | 3470 | .get_unmapped_area = io_uring_get_unmapped_area, |
6c5c240e | 3471 | #ifndef CONFIG_MMU |
6c5c240e RP |
3472 | .mmap_capabilities = io_uring_nommu_mmap_capabilities, |
3473 | #endif | |
2b188cc1 | 3474 | .poll = io_uring_poll, |
bebdb65e | 3475 | #ifdef CONFIG_PROC_FS |
87ce955b | 3476 | .show_fdinfo = io_uring_show_fdinfo, |
bebdb65e | 3477 | #endif |
2b188cc1 JA |
3478 | }; |
3479 | ||
92ac8bea JA |
3480 | bool io_is_uring_fops(struct file *file) |
3481 | { | |
3482 | return file->f_op == &io_uring_fops; | |
3483 | } | |
3484 | ||
c072481d PB |
3485 | static __cold int io_allocate_scq_urings(struct io_ring_ctx *ctx, |
3486 | struct io_uring_params *p) | |
2b188cc1 | 3487 | { |
8078486e | 3488 | struct io_uring_region_desc rd; |
75b28aff HV |
3489 | struct io_rings *rings; |
3490 | size_t size, sq_array_offset; | |
8078486e | 3491 | int ret; |
2b188cc1 | 3492 | |
bd740481 JA |
3493 | /* make sure these are sane, as we already accounted them */ |
3494 | ctx->sq_entries = p->sq_entries; | |
3495 | ctx->cq_entries = p->cq_entries; | |
3496 | ||
09d0a8ea JA |
3497 | size = rings_size(ctx->flags, p->sq_entries, p->cq_entries, |
3498 | &sq_array_offset); | |
75b28aff HV |
3499 | if (size == SIZE_MAX) |
3500 | return -EOVERFLOW; | |
3501 | ||
81a4058e PB |
3502 | memset(&rd, 0, sizeof(rd)); |
3503 | rd.size = PAGE_ALIGN(size); | |
3504 | if (ctx->flags & IORING_SETUP_NO_MMAP) { | |
3505 | rd.user_addr = p->cq_off.user_addr; | |
3506 | rd.flags |= IORING_MEM_REGION_TYPE_USER; | |
3507 | } | |
3508 | ret = io_create_region(ctx, &ctx->ring_region, &rd, IORING_OFF_CQ_RING); | |
3509 | if (ret) | |
3510 | return ret; | |
3511 | ctx->rings = rings = io_region_get_ptr(&ctx->ring_region); | |
2b188cc1 | 3512 | |
2af89abd PB |
3513 | if (!(ctx->flags & IORING_SETUP_NO_SQARRAY)) |
3514 | ctx->sq_array = (u32 *)((char *)rings + sq_array_offset); | |
75b28aff HV |
3515 | rings->sq_ring_mask = p->sq_entries - 1; |
3516 | rings->cq_ring_mask = p->cq_entries - 1; | |
3517 | rings->sq_ring_entries = p->sq_entries; | |
3518 | rings->cq_ring_entries = p->cq_entries; | |
2b188cc1 | 3519 | |
ebdeb7c0 JA |
3520 | if (p->flags & IORING_SETUP_SQE128) |
3521 | size = array_size(2 * sizeof(struct io_uring_sqe), p->sq_entries); | |
3522 | else | |
3523 | size = array_size(sizeof(struct io_uring_sqe), p->sq_entries); | |
eb065d30 | 3524 | if (size == SIZE_MAX) { |
9c189eee | 3525 | io_rings_free(ctx); |
2b188cc1 | 3526 | return -EOVERFLOW; |
eb065d30 | 3527 | } |
2b188cc1 | 3528 | |
8078486e PB |
3529 | memset(&rd, 0, sizeof(rd)); |
3530 | rd.size = PAGE_ALIGN(size); | |
3531 | if (ctx->flags & IORING_SETUP_NO_MMAP) { | |
3532 | rd.user_addr = p->sq_off.user_addr; | |
3533 | rd.flags |= IORING_MEM_REGION_TYPE_USER; | |
3534 | } | |
3535 | ret = io_create_region(ctx, &ctx->sq_region, &rd, IORING_OFF_SQES); | |
3536 | if (ret) { | |
9c189eee | 3537 | io_rings_free(ctx); |
8078486e | 3538 | return ret; |
eb065d30 | 3539 | } |
8078486e | 3540 | ctx->sq_sqes = io_region_get_ptr(&ctx->sq_region); |
2b188cc1 JA |
3541 | return 0; |
3542 | } | |
3543 | ||
6e76ac59 | 3544 | static int io_uring_install_fd(struct file *file) |
9faadcc8 | 3545 | { |
6e76ac59 | 3546 | int fd; |
9faadcc8 PB |
3547 | |
3548 | fd = get_unused_fd_flags(O_RDWR | O_CLOEXEC); | |
3549 | if (fd < 0) | |
3550 | return fd; | |
9faadcc8 PB |
3551 | fd_install(fd, file); |
3552 | return fd; | |
3553 | } | |
3554 | ||
2b188cc1 JA |
3555 | /* |
3556 | * Allocate an anonymous fd, this is what constitutes the application | |
3557 | * visible backing of an io_uring instance. The application mmaps this | |
6e5e6d27 | 3558 | * fd to gain access to the SQ/CQ ring details. |
2b188cc1 | 3559 | */ |
9faadcc8 | 3560 | static struct file *io_uring_get_file(struct io_ring_ctx *ctx) |
2b188cc1 | 3561 | { |
4f0b9194 | 3562 | /* Create a new inode so that the LSM can block the creation. */ |
09d1c6a8 | 3563 | return anon_inode_create_getfile("[io_uring]", &io_uring_fops, ctx, |
91a9ab7c | 3564 | O_RDWR | O_CLOEXEC, NULL); |
2b188cc1 JA |
3565 | } |
3566 | ||
92a3bac9 PB |
3567 | static int io_uring_sanitise_params(struct io_uring_params *p) |
3568 | { | |
3569 | unsigned flags = p->flags; | |
3570 | ||
3571 | /* There is no way to mmap rings without a real fd */ | |
3572 | if ((flags & IORING_SETUP_REGISTERED_FD_ONLY) && | |
3573 | !(flags & IORING_SETUP_NO_MMAP)) | |
3574 | return -EINVAL; | |
3575 | ||
3576 | if (flags & IORING_SETUP_SQPOLL) { | |
3577 | /* IPI related flags don't make sense with SQPOLL */ | |
3578 | if (flags & (IORING_SETUP_COOP_TASKRUN | | |
3579 | IORING_SETUP_TASKRUN_FLAG | | |
3580 | IORING_SETUP_DEFER_TASKRUN)) | |
3581 | return -EINVAL; | |
3582 | } | |
3583 | ||
3584 | if (flags & IORING_SETUP_TASKRUN_FLAG) { | |
3585 | if (!(flags & (IORING_SETUP_COOP_TASKRUN | | |
3586 | IORING_SETUP_DEFER_TASKRUN))) | |
3587 | return -EINVAL; | |
3588 | } | |
3589 | ||
3590 | /* HYBRID_IOPOLL only valid with IOPOLL */ | |
3591 | if ((flags & IORING_SETUP_HYBRID_IOPOLL) && !(flags & IORING_SETUP_IOPOLL)) | |
3592 | return -EINVAL; | |
3593 | ||
3594 | /* | |
3595 | * For DEFER_TASKRUN we require the completion task to be the same as | |
3596 | * the submission task. This implies that there is only one submitter. | |
3597 | */ | |
3598 | if ((flags & IORING_SETUP_DEFER_TASKRUN) && | |
3599 | !(flags & IORING_SETUP_SINGLE_ISSUER)) | |
3600 | return -EINVAL; | |
3601 | ||
3602 | return 0; | |
3603 | } | |
3604 | ||
81d8191e | 3605 | int io_uring_fill_params(unsigned entries, struct io_uring_params *p) |
2b188cc1 | 3606 | { |
8110c1a6 | 3607 | if (!entries) |
2b188cc1 | 3608 | return -EINVAL; |
8110c1a6 JA |
3609 | if (entries > IORING_MAX_ENTRIES) { |
3610 | if (!(p->flags & IORING_SETUP_CLAMP)) | |
3611 | return -EINVAL; | |
3612 | entries = IORING_MAX_ENTRIES; | |
3613 | } | |
2b188cc1 JA |
3614 | |
3615 | /* | |
3616 | * Use twice as many entries for the CQ ring. It's possible for the | |
3617 | * application to drive a higher depth than the size of the SQ ring, | |
3618 | * since the sqes are only used at submission time. This allows for | |
33a107f0 JA |
3619 | * some flexibility in overcommitting a bit. If the application has |
3620 | * set IORING_SETUP_CQSIZE, it will have passed in the desired number | |
3621 | * of CQ ring entries manually. | |
2b188cc1 JA |
3622 | */ |
3623 | p->sq_entries = roundup_pow_of_two(entries); | |
33a107f0 JA |
3624 | if (p->flags & IORING_SETUP_CQSIZE) { |
3625 | /* | |
3626 | * If IORING_SETUP_CQSIZE is set, we do the same roundup | |
3627 | * to a power-of-two, if it isn't already. We do NOT impose | |
3628 | * any cq vs sq ring sizing. | |
3629 | */ | |
eb2667b3 | 3630 | if (!p->cq_entries) |
33a107f0 | 3631 | return -EINVAL; |
8110c1a6 JA |
3632 | if (p->cq_entries > IORING_MAX_CQ_ENTRIES) { |
3633 | if (!(p->flags & IORING_SETUP_CLAMP)) | |
3634 | return -EINVAL; | |
3635 | p->cq_entries = IORING_MAX_CQ_ENTRIES; | |
3636 | } | |
eb2667b3 JQ |
3637 | p->cq_entries = roundup_pow_of_two(p->cq_entries); |
3638 | if (p->cq_entries < p->sq_entries) | |
3639 | return -EINVAL; | |
33a107f0 JA |
3640 | } else { |
3641 | p->cq_entries = 2 * p->sq_entries; | |
3642 | } | |
2b188cc1 | 3643 | |
81d8191e JA |
3644 | p->sq_off.head = offsetof(struct io_rings, sq.head); |
3645 | p->sq_off.tail = offsetof(struct io_rings, sq.tail); | |
3646 | p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask); | |
3647 | p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries); | |
3648 | p->sq_off.flags = offsetof(struct io_rings, sq_flags); | |
3649 | p->sq_off.dropped = offsetof(struct io_rings, sq_dropped); | |
3650 | p->sq_off.resv1 = 0; | |
3651 | if (!(p->flags & IORING_SETUP_NO_MMAP)) | |
3652 | p->sq_off.user_addr = 0; | |
3653 | ||
3654 | p->cq_off.head = offsetof(struct io_rings, cq.head); | |
3655 | p->cq_off.tail = offsetof(struct io_rings, cq.tail); | |
3656 | p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask); | |
3657 | p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries); | |
3658 | p->cq_off.overflow = offsetof(struct io_rings, cq_overflow); | |
3659 | p->cq_off.cqes = offsetof(struct io_rings, cqes); | |
3660 | p->cq_off.flags = offsetof(struct io_rings, cq_flags); | |
3661 | p->cq_off.resv1 = 0; | |
3662 | if (!(p->flags & IORING_SETUP_NO_MMAP)) | |
3663 | p->cq_off.user_addr = 0; | |
3664 | ||
3665 | return 0; | |
3666 | } | |
3667 | ||
3668 | static __cold int io_uring_create(unsigned entries, struct io_uring_params *p, | |
3669 | struct io_uring_params __user *params) | |
3670 | { | |
3671 | struct io_ring_ctx *ctx; | |
3672 | struct io_uring_task *tctx; | |
3673 | struct file *file; | |
3674 | int ret; | |
3675 | ||
92a3bac9 PB |
3676 | ret = io_uring_sanitise_params(p); |
3677 | if (ret) | |
3678 | return ret; | |
3679 | ||
81d8191e JA |
3680 | ret = io_uring_fill_params(entries, p); |
3681 | if (unlikely(ret)) | |
3682 | return ret; | |
3683 | ||
2b188cc1 | 3684 | ctx = io_ring_ctx_alloc(p); |
62e398be | 3685 | if (!ctx) |
2b188cc1 | 3686 | return -ENOMEM; |
773697b6 | 3687 | |
2b8e976b PB |
3688 | ctx->clockid = CLOCK_MONOTONIC; |
3689 | ctx->clock_offset = 0; | |
3690 | ||
9b296c62 PB |
3691 | if (!(ctx->flags & IORING_SETUP_NO_SQARRAY)) |
3692 | static_branch_inc(&io_key_has_sqarray); | |
3693 | ||
e6aeb272 PB |
3694 | if ((ctx->flags & IORING_SETUP_DEFER_TASKRUN) && |
3695 | !(ctx->flags & IORING_SETUP_IOPOLL) && | |
3696 | !(ctx->flags & IORING_SETUP_SQPOLL)) | |
3697 | ctx->task_complete = true; | |
3698 | ||
ec26c225 PB |
3699 | if (ctx->task_complete || (ctx->flags & IORING_SETUP_IOPOLL)) |
3700 | ctx->lockless_cq = true; | |
3701 | ||
bca39f39 PB |
3702 | /* |
3703 | * lazy poll_wq activation relies on ->task_complete for synchronisation | |
3704 | * purposes, see io_activate_pollwq() | |
3705 | */ | |
3706 | if (!ctx->task_complete) | |
3707 | ctx->poll_activated = true; | |
3708 | ||
773697b6 PB |
3709 | /* |
3710 | * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user | |
3711 | * space applications don't need to do io completion events | |
3712 | * polling again, they can rely on io_sq_thread to do polling | |
3713 | * work, which can reduce cpu usage and uring_lock contention. | |
3714 | */ | |
3715 | if (ctx->flags & IORING_SETUP_IOPOLL && | |
3716 | !(ctx->flags & IORING_SETUP_SQPOLL)) | |
3717 | ctx->syscall_iopoll = 1; | |
3718 | ||
2b188cc1 | 3719 | ctx->compat = in_compat_syscall(); |
6adc2272 | 3720 | if (!ns_capable_noaudit(&init_user_ns, CAP_IPC_LOCK)) |
62e398be | 3721 | ctx->user = get_uid(current_user()); |
2aede0e4 | 3722 | |
9f010507 | 3723 | /* |
e1169f06 JA |
3724 | * For SQPOLL, we just need a wakeup, always. For !SQPOLL, if |
3725 | * COOP_TASKRUN is set, then IPIs are never needed by the app. | |
9f010507 | 3726 | */ |
92a3bac9 | 3727 | if (ctx->flags & (IORING_SETUP_SQPOLL|IORING_SETUP_COOP_TASKRUN)) |
e1169f06 | 3728 | ctx->notify_method = TWA_SIGNAL_NO_IPI; |
92a3bac9 | 3729 | else |
9f010507 | 3730 | ctx->notify_method = TWA_SIGNAL; |
c0e0d6ba | 3731 | |
2aede0e4 JA |
3732 | /* |
3733 | * This is just grabbed for accounting purposes. When a process exits, | |
3734 | * the mm is exited and dropped before the files, hence we need to hang | |
3735 | * on to this mm purely for the purposes of being able to unaccount | |
3736 | * memory (locked/pinned vm). It's not used for anything else. | |
3737 | */ | |
6b7898eb | 3738 | mmgrab(current->mm); |
2aede0e4 | 3739 | ctx->mm_account = current->mm; |
6b7898eb | 3740 | |
2b188cc1 JA |
3741 | ret = io_allocate_scq_urings(ctx, p); |
3742 | if (ret) | |
3743 | goto err; | |
3744 | ||
81d8191e JA |
3745 | if (!(p->flags & IORING_SETUP_NO_SQARRAY)) |
3746 | p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings; | |
2933ae6e | 3747 | |
7e84e1c7 | 3748 | ret = io_sq_offload_create(ctx, p); |
47b228ce PB |
3749 | if (ret) |
3750 | goto err; | |
2b188cc1 | 3751 | |
7f13657d XW |
3752 | p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP | |
3753 | IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS | | |
5769a351 | 3754 | IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL | |
c73ebb68 | 3755 | IORING_FEAT_POLL_32BITS | IORING_FEAT_SQPOLL_NONFIXED | |
9690557e | 3756 | IORING_FEAT_EXT_ARG | IORING_FEAT_NATIVE_WORKERS | |
c4212f3e | 3757 | IORING_FEAT_RSRC_TAGS | IORING_FEAT_CQE_SKIP | |
2f9c9515 | 3758 | IORING_FEAT_LINKED_FILE | IORING_FEAT_REG_REG_RING | |
94d57442 | 3759 | IORING_FEAT_RECVSEND_BUNDLE | IORING_FEAT_MIN_TIMEOUT | |
07754bfd | 3760 | IORING_FEAT_RW_ATTR | IORING_FEAT_NO_IOWAIT; |
7f13657d XW |
3761 | |
3762 | if (copy_to_user(params, p, sizeof(*p))) { | |
3763 | ret = -EFAULT; | |
3764 | goto err; | |
3765 | } | |
d1719f70 | 3766 | |
7cae596b DY |
3767 | if (ctx->flags & IORING_SETUP_SINGLE_ISSUER |
3768 | && !(ctx->flags & IORING_SETUP_R_DISABLED)) | |
8579538c | 3769 | WRITE_ONCE(ctx->submitter_task, get_task_struct(current)); |
7cae596b | 3770 | |
9faadcc8 PB |
3771 | file = io_uring_get_file(ctx); |
3772 | if (IS_ERR(file)) { | |
3773 | ret = PTR_ERR(file); | |
3774 | goto err; | |
3775 | } | |
3776 | ||
6e76ac59 JT |
3777 | ret = __io_uring_add_tctx_node(ctx); |
3778 | if (ret) | |
3779 | goto err_fput; | |
3780 | tctx = current->io_uring; | |
3781 | ||
044c1ab3 JA |
3782 | /* |
3783 | * Install ring fd as the very last thing, so we don't risk someone | |
3784 | * having closed it before we finish setup | |
3785 | */ | |
6e76ac59 JT |
3786 | if (p->flags & IORING_SETUP_REGISTERED_FD_ONLY) |
3787 | ret = io_ring_add_registered_file(tctx, file, 0, IO_RINGFD_REG_MAX); | |
3788 | else | |
3789 | ret = io_uring_install_fd(file); | |
3790 | if (ret < 0) | |
3791 | goto err_fput; | |
044c1ab3 | 3792 | |
c826bd7a | 3793 | trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags); |
2b188cc1 JA |
3794 | return ret; |
3795 | err: | |
3796 | io_ring_ctx_wait_and_kill(ctx); | |
3797 | return ret; | |
6e76ac59 JT |
3798 | err_fput: |
3799 | fput(file); | |
3800 | return ret; | |
2b188cc1 JA |
3801 | } |
3802 | ||
3803 | /* | |
3804 | * Sets up an aio uring context, and returns the fd. Applications asks for a | |
3805 | * ring size, we return the actual sq/cq ring sizes (among other things) in the | |
3806 | * params structure passed in. | |
3807 | */ | |
3808 | static long io_uring_setup(u32 entries, struct io_uring_params __user *params) | |
3809 | { | |
3810 | struct io_uring_params p; | |
2b188cc1 JA |
3811 | int i; |
3812 | ||
3813 | if (copy_from_user(&p, params, sizeof(p))) | |
3814 | return -EFAULT; | |
3815 | for (i = 0; i < ARRAY_SIZE(p.resv); i++) { | |
3816 | if (p.resv[i]) | |
3817 | return -EINVAL; | |
3818 | } | |
3819 | ||
6c271ce2 | 3820 | if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL | |
8110c1a6 | 3821 | IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE | |
7e84e1c7 | 3822 | IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ | |
e1169f06 | 3823 | IORING_SETUP_R_DISABLED | IORING_SETUP_SUBMIT_ALL | |
ebdeb7c0 | 3824 | IORING_SETUP_COOP_TASKRUN | IORING_SETUP_TASKRUN_FLAG | |
97bbdc06 | 3825 | IORING_SETUP_SQE128 | IORING_SETUP_CQE32 | |
03d89a2d | 3826 | IORING_SETUP_SINGLE_ISSUER | IORING_SETUP_DEFER_TASKRUN | |
2af89abd | 3827 | IORING_SETUP_NO_MMAP | IORING_SETUP_REGISTERED_FD_ONLY | |
01ee194d | 3828 | IORING_SETUP_NO_SQARRAY | IORING_SETUP_HYBRID_IOPOLL)) |
2b188cc1 JA |
3829 | return -EINVAL; |
3830 | ||
ef060ea9 | 3831 | return io_uring_create(entries, &p, params); |
2b188cc1 JA |
3832 | } |
3833 | ||
b8a468e0 | 3834 | static inline int io_uring_allowed(void) |
76d3ccec MR |
3835 | { |
3836 | int disabled = READ_ONCE(sysctl_io_uring_disabled); | |
3837 | kgid_t io_uring_group; | |
3838 | ||
3839 | if (disabled == 2) | |
b8a468e0 | 3840 | return -EPERM; |
76d3ccec MR |
3841 | |
3842 | if (disabled == 0 || capable(CAP_SYS_ADMIN)) | |
c6ad9fdb | 3843 | goto allowed_lsm; |
76d3ccec MR |
3844 | |
3845 | io_uring_group = make_kgid(&init_user_ns, sysctl_io_uring_group); | |
3846 | if (!gid_valid(io_uring_group)) | |
b8a468e0 HM |
3847 | return -EPERM; |
3848 | ||
3849 | if (!in_group_p(io_uring_group)) | |
3850 | return -EPERM; | |
76d3ccec | 3851 | |
c6ad9fdb HM |
3852 | allowed_lsm: |
3853 | return security_uring_allowed(); | |
76d3ccec MR |
3854 | } |
3855 | ||
2b188cc1 JA |
3856 | SYSCALL_DEFINE2(io_uring_setup, u32, entries, |
3857 | struct io_uring_params __user *, params) | |
3858 | { | |
b8a468e0 HM |
3859 | int ret; |
3860 | ||
3861 | ret = io_uring_allowed(); | |
3862 | if (ret) | |
3863 | return ret; | |
76d3ccec | 3864 | |
2b188cc1 JA |
3865 | return io_uring_setup(entries, params); |
3866 | } | |
3867 | ||
3868 | static int __init io_uring_init(void) | |
3869 | { | |
a6711d1c CB |
3870 | struct kmem_cache_args kmem_args = { |
3871 | .useroffset = offsetof(struct io_kiocb, cmd.data), | |
3872 | .usersize = sizeof_field(struct io_kiocb, cmd.data), | |
aaa736b1 JA |
3873 | .freeptr_offset = offsetof(struct io_kiocb, work), |
3874 | .use_freeptr_offset = true, | |
a6711d1c CB |
3875 | }; |
3876 | ||
9c71d39a | 3877 | #define __BUILD_BUG_VERIFY_OFFSET_SIZE(stype, eoffset, esize, ename) do { \ |
d7f62e82 | 3878 | BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \ |
9c71d39a | 3879 | BUILD_BUG_ON(sizeof_field(stype, ename) != esize); \ |
d7f62e82 SM |
3880 | } while (0) |
3881 | ||
3882 | #define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \ | |
9c71d39a SM |
3883 | __BUILD_BUG_VERIFY_OFFSET_SIZE(struct io_uring_sqe, eoffset, sizeof(etype), ename) |
3884 | #define BUILD_BUG_SQE_ELEM_SIZE(eoffset, esize, ename) \ | |
3885 | __BUILD_BUG_VERIFY_OFFSET_SIZE(struct io_uring_sqe, eoffset, esize, ename) | |
d7f62e82 SM |
3886 | BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64); |
3887 | BUILD_BUG_SQE_ELEM(0, __u8, opcode); | |
3888 | BUILD_BUG_SQE_ELEM(1, __u8, flags); | |
3889 | BUILD_BUG_SQE_ELEM(2, __u16, ioprio); | |
3890 | BUILD_BUG_SQE_ELEM(4, __s32, fd); | |
3891 | BUILD_BUG_SQE_ELEM(8, __u64, off); | |
3892 | BUILD_BUG_SQE_ELEM(8, __u64, addr2); | |
9c71d39a SM |
3893 | BUILD_BUG_SQE_ELEM(8, __u32, cmd_op); |
3894 | BUILD_BUG_SQE_ELEM(12, __u32, __pad1); | |
d7f62e82 | 3895 | BUILD_BUG_SQE_ELEM(16, __u64, addr); |
7d67af2c | 3896 | BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in); |
d7f62e82 SM |
3897 | BUILD_BUG_SQE_ELEM(24, __u32, len); |
3898 | BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags); | |
3899 | BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags); | |
3900 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags); | |
3901 | BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags); | |
5769a351 JX |
3902 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events); |
3903 | BUILD_BUG_SQE_ELEM(28, __u32, poll32_events); | |
d7f62e82 SM |
3904 | BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags); |
3905 | BUILD_BUG_SQE_ELEM(28, __u32, msg_flags); | |
3906 | BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags); | |
3907 | BUILD_BUG_SQE_ELEM(28, __u32, accept_flags); | |
3908 | BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags); | |
3909 | BUILD_BUG_SQE_ELEM(28, __u32, open_flags); | |
3910 | BUILD_BUG_SQE_ELEM(28, __u32, statx_flags); | |
3911 | BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice); | |
7d67af2c | 3912 | BUILD_BUG_SQE_ELEM(28, __u32, splice_flags); |
9c71d39a SM |
3913 | BUILD_BUG_SQE_ELEM(28, __u32, rename_flags); |
3914 | BUILD_BUG_SQE_ELEM(28, __u32, unlink_flags); | |
3915 | BUILD_BUG_SQE_ELEM(28, __u32, hardlink_flags); | |
3916 | BUILD_BUG_SQE_ELEM(28, __u32, xattr_flags); | |
3917 | BUILD_BUG_SQE_ELEM(28, __u32, msg_ring_flags); | |
d7f62e82 SM |
3918 | BUILD_BUG_SQE_ELEM(32, __u64, user_data); |
3919 | BUILD_BUG_SQE_ELEM(40, __u16, buf_index); | |
16340eab | 3920 | BUILD_BUG_SQE_ELEM(40, __u16, buf_group); |
d7f62e82 | 3921 | BUILD_BUG_SQE_ELEM(42, __u16, personality); |
7d67af2c | 3922 | BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in); |
b9445598 | 3923 | BUILD_BUG_SQE_ELEM(44, __u32, file_index); |
b48c312b | 3924 | BUILD_BUG_SQE_ELEM(44, __u16, addr_len); |
02040353 KB |
3925 | BUILD_BUG_SQE_ELEM(44, __u8, write_stream); |
3926 | BUILD_BUG_SQE_ELEM(45, __u8, __pad4[0]); | |
b48c312b | 3927 | BUILD_BUG_SQE_ELEM(46, __u16, __pad3[0]); |
e9621e2b | 3928 | BUILD_BUG_SQE_ELEM(48, __u64, addr3); |
9c71d39a | 3929 | BUILD_BUG_SQE_ELEM_SIZE(48, 0, cmd); |
59a7d12a AG |
3930 | BUILD_BUG_SQE_ELEM(48, __u64, attr_ptr); |
3931 | BUILD_BUG_SQE_ELEM(56, __u64, attr_type_mask); | |
9c71d39a | 3932 | BUILD_BUG_SQE_ELEM(56, __u64, __pad2); |
d7f62e82 | 3933 | |
b0d658ec PB |
3934 | BUILD_BUG_ON(sizeof(struct io_uring_files_update) != |
3935 | sizeof(struct io_uring_rsrc_update)); | |
3936 | BUILD_BUG_ON(sizeof(struct io_uring_rsrc_update) > | |
3937 | sizeof(struct io_uring_rsrc_update2)); | |
90499ad0 PB |
3938 | |
3939 | /* ->buf_index is u16 */ | |
c7fb1942 JA |
3940 | BUILD_BUG_ON(offsetof(struct io_uring_buf_ring, bufs) != 0); |
3941 | BUILD_BUG_ON(offsetof(struct io_uring_buf, resv) != | |
3942 | offsetof(struct io_uring_buf_ring, tail)); | |
90499ad0 | 3943 | |
b0d658ec PB |
3944 | /* should fit into one byte */ |
3945 | BUILD_BUG_ON(SQE_VALID_FLAGS >= (1 << 8)); | |
68fe256a PB |
3946 | BUILD_BUG_ON(SQE_COMMON_FLAGS >= (1 << 8)); |
3947 | BUILD_BUG_ON((SQE_VALID_FLAGS | SQE_COMMON_FLAGS) != SQE_VALID_FLAGS); | |
b0d658ec | 3948 | |
4bcb982c | 3949 | BUILD_BUG_ON(__REQ_F_LAST_BIT > 8 * sizeof_field(struct io_kiocb, flags)); |
16340eab | 3950 | |
3a4b89a2 JA |
3951 | BUILD_BUG_ON(sizeof(atomic_t) != sizeof(u32)); |
3952 | ||
528ce678 ML |
3953 | /* top 8bits are for internal use */ |
3954 | BUILD_BUG_ON((IORING_URING_CMD_MASK & 0xff000000) != 0); | |
3955 | ||
d9b57aa3 | 3956 | io_uring_optable_init(); |
0702e536 | 3957 | |
27cb27b6 KB |
3958 | /* imu->dir is u8 */ |
3959 | BUILD_BUG_ON((IO_IMU_DEST | IO_IMU_SOURCE) > U8_MAX); | |
3960 | ||
b97f96e2 JA |
3961 | /* |
3962 | * Allow user copy in the per-command field, which starts after the | |
3963 | * file in io_kiocb and until the opcode field. The openat2 handling | |
3964 | * requires copying in user memory into the io_kiocb object in that | |
3965 | * range, and HARDENED_USERCOPY will complain if we haven't | |
3966 | * correctly annotated this range. | |
3967 | */ | |
a6711d1c CB |
3968 | req_cachep = kmem_cache_create("io_kiocb", sizeof(struct io_kiocb), &kmem_args, |
3969 | SLAB_HWCACHE_ALIGN | SLAB_PANIC | SLAB_ACCOUNT | | |
3970 | SLAB_TYPESAFE_BY_RCU); | |
b97f96e2 | 3971 | |
73eaa2b5 | 3972 | iou_wq = alloc_workqueue("iou_exit", WQ_UNBOUND, 64); |
72154696 | 3973 | BUG_ON(!iou_wq); |
73eaa2b5 | 3974 | |
76d3ccec MR |
3975 | #ifdef CONFIG_SYSCTL |
3976 | register_sysctl_init("kernel", kernel_io_uring_disabled_table); | |
3977 | #endif | |
3978 | ||
2b188cc1 JA |
3979 | return 0; |
3980 | }; | |
3981 | __initcall(io_uring_init); |