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