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