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