xref: /linux/io_uring/io_uring.c (revision fc66b07da5f49aed1740b2007bf69e8774e17a1b)
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
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
14  * through a control-dependency in io_get_cqe (smp_store_release to
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.
29  *
30  * Also see the examples in the liburing library:
31  *
32  *	git://git.kernel.org/pub/scm/linux/kernel/git/axboe/liburing.git
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
40  * Copyright (c) 2018-2019 Christoph Hellwig
41  */
42 #include <linux/kernel.h>
43 #include <linux/errno.h>
44 #include <linux/syscalls.h>
45 #include <linux/refcount.h>
46 #include <linux/bits.h>
47 
48 #include <linux/sched/signal.h>
49 #include <linux/fs.h>
50 #include <linux/mm.h>
51 #include <linux/percpu.h>
52 #include <linux/slab.h>
53 #include <linux/anon_inodes.h>
54 #include <linux/uaccess.h>
55 #include <linux/nospec.h>
56 #include <linux/task_work.h>
57 #include <linux/io_uring.h>
58 #include <linux/io_uring/cmd.h>
59 #include <linux/audit.h>
60 #include <linux/security.h>
61 #include <linux/jump_label.h>
62 #include <linux/kcov.h>
63 
64 #define CREATE_TRACE_POINTS
65 #include <trace/events/io_uring.h>
66 
67 #include <uapi/linux/io_uring.h>
68 
69 #include "io-wq.h"
70 
71 #include "filetable.h"
72 #include "io_uring.h"
73 #include "opdef.h"
74 #include "refs.h"
75 #include "tctx.h"
76 #include "register.h"
77 #include "sqpoll.h"
78 #include "fdinfo.h"
79 #include "kbuf.h"
80 #include "rsrc.h"
81 #include "cancel.h"
82 #include "net.h"
83 #include "notif.h"
84 #include "waitid.h"
85 #include "futex.h"
86 #include "napi.h"
87 #include "uring_cmd.h"
88 #include "msg_ring.h"
89 #include "memmap.h"
90 #include "zcrx.h"
91 #include "bpf-ops.h"
92 
93 #include "timeout.h"
94 #include "poll.h"
95 #include "rw.h"
96 #include "alloc_cache.h"
97 #include "eventfd.h"
98 #include "wait.h"
99 #include "bpf_filter.h"
100 #include "loop.h"
101 
102 #define SQE_COMMON_FLAGS (IOSQE_FIXED_FILE | IOSQE_IO_LINK | \
103 			  IOSQE_IO_HARDLINK | IOSQE_ASYNC)
104 
105 #define IO_REQ_LINK_FLAGS (REQ_F_LINK | REQ_F_HARDLINK)
106 
107 #define IO_REQ_CLEAN_FLAGS (REQ_F_BUFFER_SELECTED | REQ_F_NEED_CLEANUP | \
108 				REQ_F_INFLIGHT | REQ_F_CREDS | REQ_F_ASYNC_DATA)
109 
110 #define IO_REQ_CLEAN_SLOW_FLAGS (REQ_F_REFCOUNT | IO_REQ_LINK_FLAGS | \
111 				 REQ_F_REISSUE | REQ_F_POLLED | \
112 				 IO_REQ_CLEAN_FLAGS)
113 
114 #define IO_TCTX_REFS_CACHE_NR	(1U << 10)
115 
116 #define IO_COMPL_BATCH			32
117 #define IO_REQ_ALLOC_BATCH		8
118 
119 /* requests with any of those set should undergo io_disarm_next() */
120 #define IO_DISARM_MASK (REQ_F_ARM_LTIMEOUT | REQ_F_LINK_TIMEOUT | REQ_F_FAIL)
121 
122 static void io_queue_sqe(struct io_kiocb *req, unsigned int extra_flags);
123 static void __io_req_caches_free(struct io_ring_ctx *ctx);
124 
125 static __read_mostly DEFINE_STATIC_KEY_DEFERRED_FALSE(io_key_has_sqarray, HZ);
126 
127 struct kmem_cache *req_cachep;
128 static struct workqueue_struct *iou_wq __ro_after_init;
129 
130 static int __read_mostly sysctl_io_uring_disabled;
131 static int __read_mostly sysctl_io_uring_group = -1;
132 
133 #ifdef CONFIG_SYSCTL
134 static const struct ctl_table kernel_io_uring_disabled_table[] = {
135 	{
136 		.procname	= "io_uring_disabled",
137 		.data		= &sysctl_io_uring_disabled,
138 		.maxlen		= sizeof(sysctl_io_uring_disabled),
139 		.mode		= 0644,
140 		.proc_handler	= proc_dointvec_minmax,
141 		.extra1		= SYSCTL_ZERO,
142 		.extra2		= SYSCTL_TWO,
143 	},
144 	{
145 		.procname	= "io_uring_group",
146 		.data		= &sysctl_io_uring_group,
147 		.maxlen		= sizeof(gid_t),
148 		.mode		= 0644,
149 		.proc_handler	= proc_dointvec,
150 	},
151 };
152 #endif
153 
154 static void io_poison_cached_req(struct io_kiocb *req)
155 {
156 	req->ctx = IO_URING_PTR_POISON;
157 	req->tctx = IO_URING_PTR_POISON;
158 	req->file = IO_URING_PTR_POISON;
159 	req->creds = IO_URING_PTR_POISON;
160 	req->io_task_work.func = IO_URING_PTR_POISON;
161 	req->apoll = IO_URING_PTR_POISON;
162 }
163 
164 void io_poison_req(struct io_kiocb *req)
165 {
166 	io_poison_cached_req(req);
167 	req->async_data = IO_URING_PTR_POISON;
168 	req->kbuf = IO_URING_PTR_POISON;
169 	req->comp_list.next = IO_URING_PTR_POISON;
170 	req->file_node = IO_URING_PTR_POISON;
171 	req->link = IO_URING_PTR_POISON;
172 }
173 
174 static inline void req_fail_link_node(struct io_kiocb *req, int res)
175 {
176 	req_set_fail(req);
177 	io_req_set_res(req, res, 0);
178 }
179 
180 static inline void io_req_add_to_cache(struct io_kiocb *req, struct io_ring_ctx *ctx)
181 {
182 	if (IS_ENABLED(CONFIG_KASAN))
183 		io_poison_cached_req(req);
184 	wq_stack_add_head(&req->comp_list, &ctx->submit_state.free_list);
185 }
186 
187 static __cold void io_ring_ctx_ref_free(struct percpu_ref *ref)
188 {
189 	struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
190 
191 	complete(&ctx->ref_comp);
192 }
193 
194 static int io_alloc_hash_table(struct io_hash_table *table, unsigned bits)
195 {
196 	unsigned int hash_buckets;
197 	int i;
198 
199 	do {
200 		hash_buckets = 1U << bits;
201 		table->hbs = kvmalloc_objs(table->hbs[0], hash_buckets,
202 					   GFP_KERNEL_ACCOUNT);
203 		if (table->hbs)
204 			break;
205 		if (bits == 1)
206 			return -ENOMEM;
207 		bits--;
208 	} while (1);
209 
210 	table->hash_bits = bits;
211 	for (i = 0; i < hash_buckets; i++)
212 		INIT_HLIST_HEAD(&table->hbs[i].list);
213 	return 0;
214 }
215 
216 static void io_free_alloc_caches(struct io_ring_ctx *ctx)
217 {
218 	io_alloc_cache_free(&ctx->apoll_cache, kfree);
219 	io_alloc_cache_free(&ctx->netmsg_cache, io_netmsg_cache_free);
220 	io_alloc_cache_free(&ctx->rw_cache, io_rw_cache_free);
221 	io_alloc_cache_free(&ctx->cmd_cache, io_cmd_cache_free);
222 	io_futex_cache_free(ctx);
223 	io_rsrc_cache_free(ctx);
224 }
225 
226 static __cold struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
227 {
228 	struct io_ring_ctx *ctx;
229 	int hash_bits;
230 	bool ret;
231 
232 	ctx = kzalloc_obj(*ctx);
233 	if (!ctx)
234 		return NULL;
235 
236 	xa_init(&ctx->io_bl_xa);
237 	xa_init(&ctx->hpage_acct);
238 
239 	/*
240 	 * Use 5 bits less than the max cq entries, that should give us around
241 	 * 32 entries per hash list if totally full and uniformly spread, but
242 	 * don't keep too many buckets to not overconsume memory.
243 	 */
244 	hash_bits = ilog2(p->cq_entries) - 5;
245 	hash_bits = clamp(hash_bits, 1, 8);
246 	if (io_alloc_hash_table(&ctx->cancel_table, hash_bits))
247 		goto err;
248 	if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
249 			    0, GFP_KERNEL))
250 		goto err;
251 
252 	ctx->flags = p->flags;
253 	ctx->hybrid_poll_time = LLONG_MAX;
254 	atomic_set(&ctx->cq_wait_nr, IO_CQ_WAKE_INIT);
255 	init_waitqueue_head(&ctx->sqo_sq_wait);
256 	INIT_LIST_HEAD(&ctx->sqd_list);
257 	INIT_LIST_HEAD(&ctx->cq_overflow_list);
258 	ret = io_alloc_cache_init(&ctx->apoll_cache, IO_POLL_ALLOC_CACHE_MAX,
259 			    sizeof(struct async_poll), 0);
260 	ret |= io_alloc_cache_init(&ctx->netmsg_cache, IO_ALLOC_CACHE_MAX,
261 			    sizeof(struct io_async_msghdr),
262 			    offsetof(struct io_async_msghdr, clear));
263 	ret |= io_alloc_cache_init(&ctx->rw_cache, IO_ALLOC_CACHE_MAX,
264 			    sizeof(struct io_async_rw),
265 			    offsetof(struct io_async_rw, clear));
266 	ret |= io_alloc_cache_init(&ctx->cmd_cache, IO_ALLOC_CACHE_MAX,
267 			    sizeof(struct io_async_cmd),
268 			    sizeof(struct io_async_cmd));
269 	ret |= io_futex_cache_init(ctx);
270 	ret |= io_rsrc_cache_init(ctx);
271 	if (ret)
272 		goto free_ref;
273 	init_completion(&ctx->ref_comp);
274 	xa_init_flags(&ctx->personalities, XA_FLAGS_ALLOC1 | XA_FLAGS_ACCOUNT);
275 	mutex_init(&ctx->uring_lock);
276 	init_waitqueue_head(&ctx->cq_wait);
277 	init_waitqueue_head(&ctx->poll_wq);
278 	spin_lock_init(&ctx->completion_lock);
279 	raw_spin_lock_init(&ctx->timeout_lock);
280 	INIT_LIST_HEAD(&ctx->iopoll_list);
281 	INIT_LIST_HEAD(&ctx->defer_list);
282 	INIT_LIST_HEAD(&ctx->timeout_list);
283 	INIT_LIST_HEAD(&ctx->ltimeout_list);
284 	mpscq_init(&ctx->work_list, &ctx->work_head);
285 	INIT_LIST_HEAD(&ctx->tctx_list);
286 	mutex_init(&ctx->tctx_lock);
287 	ctx->submit_state.free_list.next = NULL;
288 	INIT_HLIST_HEAD(&ctx->waitid_list);
289 	xa_init_flags(&ctx->zcrx_ctxs, XA_FLAGS_ALLOC);
290 #ifdef CONFIG_FUTEX
291 	INIT_HLIST_HEAD(&ctx->futex_list);
292 #endif
293 	INIT_WQ_LIST(&ctx->submit_state.compl_reqs);
294 	INIT_HLIST_HEAD(&ctx->cancelable_uring_cmd);
295 	io_napi_init(ctx);
296 	mutex_init(&ctx->mmap_lock);
297 	ctx->kcov_handle = kcov_common_handle();
298 
299 	return ctx;
300 
301 free_ref:
302 	percpu_ref_exit(&ctx->refs);
303 err:
304 	io_free_alloc_caches(ctx);
305 	kvfree(ctx->cancel_table.hbs);
306 	xa_destroy(&ctx->io_bl_xa);
307 	xa_destroy(&ctx->hpage_acct);
308 	kfree(ctx);
309 	return NULL;
310 }
311 
312 static void io_clean_op(struct io_kiocb *req)
313 {
314 	if (unlikely(req->flags & REQ_F_BUFFER_SELECTED))
315 		io_kbuf_drop_legacy(req);
316 
317 	if (req->flags & REQ_F_NEED_CLEANUP) {
318 		const struct io_cold_def *def = &io_cold_defs[req->opcode];
319 
320 		if (def->cleanup)
321 			def->cleanup(req);
322 	}
323 	if (req->flags & REQ_F_INFLIGHT)
324 		atomic_dec(&req->tctx->inflight_tracked);
325 	if (req->flags & REQ_F_CREDS)
326 		put_cred(req->creds);
327 	if (req->flags & REQ_F_ASYNC_DATA) {
328 		kfree(req->async_data);
329 		req->async_data = NULL;
330 	}
331 	req->flags &= ~IO_REQ_CLEAN_FLAGS;
332 }
333 
334 /*
335  * Mark the request as inflight, so that file cancelation will find it.
336  * Can be used if the file is an io_uring instance, or if the request itself
337  * relies on ->mm being alive for the duration of the request.
338  */
339 inline void io_req_track_inflight(struct io_kiocb *req)
340 {
341 	if (!(req->flags & REQ_F_INFLIGHT)) {
342 		req->flags |= REQ_F_INFLIGHT;
343 		atomic_inc(&req->tctx->inflight_tracked);
344 	}
345 }
346 
347 static struct io_kiocb *__io_prep_linked_timeout(struct io_kiocb *req)
348 {
349 	if (WARN_ON_ONCE(!req->link))
350 		return NULL;
351 
352 	req->flags &= ~REQ_F_ARM_LTIMEOUT;
353 	req->flags |= REQ_F_LINK_TIMEOUT;
354 
355 	/* linked timeouts should have two refs once prep'ed */
356 	io_req_set_refcount(req);
357 	__io_req_set_refcount(req->link, 2);
358 	return req->link;
359 }
360 
361 static void io_prep_async_work(struct io_kiocb *req)
362 {
363 	const struct io_issue_def *def = &io_issue_defs[req->opcode];
364 
365 	if (!(req->flags & REQ_F_CREDS)) {
366 		req->flags |= REQ_F_CREDS;
367 		req->creds = get_current_cred();
368 	}
369 
370 	req->work.list.next = NULL;
371 	atomic_set(&req->work.flags, 0);
372 	if (req->flags & REQ_F_FORCE_ASYNC)
373 		atomic_or(IO_WQ_WORK_CONCURRENT, &req->work.flags);
374 
375 	if (req->file && !(req->flags & REQ_F_FIXED_FILE))
376 		req->flags |= io_file_get_flags(req->file);
377 
378 	if (req->file && (req->flags & REQ_F_ISREG)) {
379 		bool should_hash = def->hash_reg_file;
380 
381 		/* don't serialize this request if the fs doesn't need it */
382 		if (should_hash && (req->file->f_flags & O_DIRECT) &&
383 		    (req->file->f_op->fop_flags & FOP_DIO_PARALLEL_WRITE))
384 			should_hash = false;
385 		if (should_hash || (req->flags & REQ_F_IOPOLL))
386 			io_wq_hash_work(&req->work, file_inode(req->file));
387 	} else if (!req->file || !S_ISBLK(file_inode(req->file)->i_mode)) {
388 		if (def->unbound_nonreg_file)
389 			atomic_or(IO_WQ_WORK_UNBOUND, &req->work.flags);
390 	}
391 }
392 
393 static void io_prep_async_link(struct io_kiocb *req)
394 {
395 	struct io_kiocb *cur;
396 
397 	if (req->flags & REQ_F_LINK_TIMEOUT) {
398 		struct io_ring_ctx *ctx = req->ctx;
399 
400 		raw_spin_lock_irq(&ctx->timeout_lock);
401 		io_for_each_link(cur, req)
402 			io_prep_async_work(cur);
403 		raw_spin_unlock_irq(&ctx->timeout_lock);
404 	} else {
405 		io_for_each_link(cur, req)
406 			io_prep_async_work(cur);
407 	}
408 }
409 
410 /* Called for every request that is issued: keep it inline */
411 static __always_inline bool io_assign_file(struct io_kiocb *req,
412 					   const struct io_issue_def *def,
413 					   unsigned int issue_flags)
414 {
415 	if (req->file || !def->needs_file)
416 		return true;
417 
418 	if (req->flags & REQ_F_FIXED_FILE)
419 		req->file = io_file_get_fixed(req, req->cqe.fd, issue_flags);
420 	else
421 		req->file = io_file_get_normal(req, req->cqe.fd);
422 
423 	return !!req->file;
424 }
425 
426 static void io_req_queue_iowq_tw(struct io_tw_req tw_req, io_tw_token_t tw)
427 {
428 	struct io_kiocb *req = tw_req.req;
429 
430 	io_tw_lock(req->ctx, tw);
431 	if (unlikely(tw.cancel))
432 		io_req_defer_failed(req, -ECANCELED);
433 	else
434 		io_queue_iowq(req);
435 }
436 
437 void io_queue_iowq(struct io_kiocb *req)
438 {
439 	struct io_uring_task *tctx = req->tctx;
440 
441 	BUG_ON(!tctx);
442 	lockdep_assert_held(&req->ctx->uring_lock);
443 
444 	if (current->flags & PF_KTHREAD)
445 		goto cancel;
446 
447 	/* Only the submitter can safely access its io-wq during task exit. */
448 	if (unlikely(current != tctx->task)) {
449 		req->io_task_work.func = io_req_queue_iowq_tw;
450 		io_req_task_work_add(req);
451 		return;
452 	}
453 	if (!tctx->io_wq)
454 		goto cancel;
455 
456 	/*
457 	 * The file is looked up as late as possible, as it may be the result
458 	 * of an earlier request of the link. This one is up now, and what kind
459 	 * of work it is depends on the file. If that fails, leave the error
460 	 * handling to the worker.
461 	 *
462 	 * Not for a splice: whatever it writes to, it may wait for its input
463 	 * for ever. It has to remain unbound work that isn't hashed.
464 	 */
465 	if (req->opcode != IORING_OP_SPLICE)
466 		io_assign_file(req, &io_issue_defs[req->opcode], 0);
467 
468 	/* init ->work of the whole link before punting */
469 	io_prep_async_link(req);
470 
471 	/*
472 	 * Not expected to happen, but if we do have a bug where this _can_
473 	 * happen, catch it here and ensure the request is marked as
474 	 * canceled. That will make io-wq go through the usual work cancel
475 	 * procedure rather than attempt to run this request (or create a new
476 	 * worker for it).
477 	 */
478 	if (WARN_ON_ONCE(!same_thread_group(tctx->task, current)))
479 		atomic_or(IO_WQ_WORK_CANCEL, &req->work.flags);
480 
481 	trace_io_uring_queue_async_work(req, io_wq_is_hashed(&req->work));
482 	io_wq_enqueue(tctx->io_wq, &req->work);
483 	return;
484 cancel:
485 	io_req_task_queue_fail(req, -ECANCELED);
486 }
487 
488 unsigned io_linked_nr(struct io_kiocb *req)
489 {
490 	struct io_kiocb *tmp;
491 	unsigned nr = 0;
492 
493 	io_for_each_link(tmp, req)
494 		nr++;
495 	return nr;
496 }
497 
498 static __cold noinline bool io_queue_deferred(struct io_ring_ctx *ctx)
499 {
500 	bool drain_seen = false, first = true, drain_queued = false;
501 
502 	lockdep_assert_held(&ctx->uring_lock);
503 	__io_req_caches_free(ctx);
504 	/* nothing outside of ->defer_list is alive */
505 	if (ctx->nr_req_allocated == ctx->nr_drained)
506 		ctx->drain_req = NULL;
507 
508 	while (!list_empty(&ctx->defer_list)) {
509 		struct io_defer_entry *de = list_first_entry(&ctx->defer_list,
510 						struct io_defer_entry, list);
511 
512 		drain_seen |= de->req->flags & REQ_F_IO_DRAIN;
513 		if ((drain_seen || first) && ctx->nr_req_allocated != ctx->nr_drained)
514 			return drain_queued;
515 
516 		list_del_init(&de->list);
517 		ctx->nr_drained -= io_linked_nr(de->req);
518 		if (de->req->flags & REQ_F_IO_DRAIN) {
519 			drain_queued = true;
520 			/* it runs alone, track its completion unless it's a link */
521 			if (!de->req->link)
522 				ctx->drain_req = de->req;
523 		}
524 		io_req_task_queue(de->req);
525 		kfree(de);
526 		first = false;
527 	}
528 	return drain_queued;
529 }
530 
531 void __io_commit_cqring_flush(struct io_ring_ctx *ctx)
532 {
533 	if (ctx->int_flags & IO_RING_F_POLL_ACTIVATED)
534 		io_poll_wq_wake(ctx);
535 	if (ctx->int_flags & IO_RING_F_OFF_TIMEOUT_USED)
536 		io_flush_timeouts(ctx);
537 	if (ctx->int_flags & IO_RING_F_HAS_EVFD)
538 		io_eventfd_signal(ctx, true, false);
539 }
540 
541 static inline void __io_cq_lock(struct io_ring_ctx *ctx)
542 {
543 	if (!(ctx->int_flags & IO_RING_F_LOCKLESS_CQ))
544 		spin_lock(&ctx->completion_lock);
545 }
546 
547 static inline void io_cq_lock(struct io_ring_ctx *ctx)
548 	__acquires(ctx->completion_lock)
549 {
550 	spin_lock(&ctx->completion_lock);
551 }
552 
553 static inline void __io_cq_unlock_post(struct io_ring_ctx *ctx)
554 {
555 	io_commit_cqring(ctx);
556 	if (!(ctx->int_flags & IO_RING_F_TASK_COMPLETE)) {
557 		if (!(ctx->int_flags & IO_RING_F_LOCKLESS_CQ))
558 			spin_unlock(&ctx->completion_lock);
559 		/* IOPOLL rings only need to wake up if it's also SQPOLL */
560 		if (!(ctx->int_flags & IO_RING_F_SYSCALL_IOPOLL))
561 			io_cqring_wake(ctx);
562 	}
563 	io_commit_cqring_flush(ctx);
564 }
565 
566 static void io_cq_unlock_post(struct io_ring_ctx *ctx)
567 	__releases(ctx->completion_lock)
568 {
569 	io_commit_cqring(ctx);
570 	spin_unlock(&ctx->completion_lock);
571 	io_cqring_wake(ctx);
572 	io_commit_cqring_flush(ctx);
573 }
574 
575 static void __io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool dying)
576 {
577 	lockdep_assert_held(&ctx->uring_lock);
578 
579 	/* don't abort if we're dying, entries must get freed */
580 	if (!dying && __io_cqring_events(ctx) == ctx->cq_entries)
581 		return;
582 
583 	io_cq_lock(ctx);
584 	while (!list_empty(&ctx->cq_overflow_list)) {
585 		size_t cqe_size = sizeof(struct io_uring_cqe);
586 		struct io_uring_cqe *cqe;
587 		struct io_overflow_cqe *ocqe;
588 		bool is_cqe32 = false;
589 
590 		ocqe = list_first_entry(&ctx->cq_overflow_list,
591 					struct io_overflow_cqe, list);
592 		if (ocqe->cqe.flags & IORING_CQE_F_32 ||
593 		    ctx->flags & IORING_SETUP_CQE32) {
594 			is_cqe32 = true;
595 			cqe_size <<= 1;
596 		}
597 		if (ctx->flags & IORING_SETUP_CQE32)
598 			is_cqe32 = false;
599 
600 		if (!dying) {
601 			if (!io_get_cqe_overflow(ctx, &cqe, true, is_cqe32))
602 				break;
603 			memcpy(cqe, &ocqe->cqe, cqe_size);
604 		}
605 		list_del(&ocqe->list);
606 		kfree(ocqe);
607 
608 		/*
609 		 * For silly syzbot cases that deliberately overflow by huge
610 		 * amounts, check if we need to resched and drop and
611 		 * reacquire the locks if so. Nothing real would ever hit this.
612 		 * Ideally we'd have a non-posting unlock for this, but hard
613 		 * to care for a non-real case.
614 		 */
615 		if (need_resched()) {
616 			ctx->cqe_sentinel = ctx->cqe_cached;
617 			io_cq_unlock_post(ctx);
618 			mutex_unlock(&ctx->uring_lock);
619 			cond_resched();
620 			mutex_lock(&ctx->uring_lock);
621 			io_cq_lock(ctx);
622 		}
623 	}
624 
625 	if (list_empty(&ctx->cq_overflow_list)) {
626 		clear_bit(IO_CHECK_CQ_OVERFLOW_BIT, &ctx->check_cq);
627 		atomic_andnot(IORING_SQ_CQ_OVERFLOW, &ctx->rings->sq_flags);
628 	}
629 	io_cq_unlock_post(ctx);
630 }
631 
632 static void io_cqring_overflow_kill(struct io_ring_ctx *ctx)
633 {
634 	if (ctx->rings)
635 		__io_cqring_overflow_flush(ctx, true);
636 }
637 
638 void io_cqring_do_overflow_flush(struct io_ring_ctx *ctx)
639 {
640 	mutex_lock(&ctx->uring_lock);
641 	__io_cqring_overflow_flush(ctx, false);
642 	mutex_unlock(&ctx->uring_lock);
643 }
644 
645 void io_cqring_overflow_flush_locked(struct io_ring_ctx *ctx)
646 {
647 	__io_cqring_overflow_flush(ctx, false);
648 }
649 
650 /* must to be called somewhat shortly after putting a request */
651 static inline void io_put_task(struct io_kiocb *req)
652 {
653 	struct io_uring_task *tctx = req->tctx;
654 
655 	if (likely(tctx->task == current)) {
656 		tctx->cached_refs++;
657 	} else {
658 		guard(rcu)();
659 
660 		percpu_counter_sub(&tctx->inflight, 1);
661 		if (unlikely(atomic_read(&tctx->in_cancel)))
662 			wake_up(&tctx->wait);
663 		put_task_struct(tctx->task);
664 	}
665 }
666 
667 void io_task_refs_refill(struct io_uring_task *tctx)
668 {
669 	unsigned int refill = -tctx->cached_refs + IO_TCTX_REFS_CACHE_NR;
670 
671 	percpu_counter_add(&tctx->inflight, refill);
672 	refcount_add(refill, &current->usage);
673 	tctx->cached_refs += refill;
674 }
675 
676 __cold void io_uring_drop_tctx_refs(struct task_struct *task)
677 {
678 	struct io_uring_task *tctx = task->io_uring;
679 	unsigned int refs = tctx->cached_refs;
680 
681 	if (refs) {
682 		tctx->cached_refs = 0;
683 		percpu_counter_sub(&tctx->inflight, refs);
684 		put_task_struct_many(task, refs);
685 	}
686 }
687 
688 static __cold bool io_cqring_add_overflow(struct io_ring_ctx *ctx,
689 					  struct io_overflow_cqe *ocqe)
690 {
691 	lockdep_assert_held(&ctx->completion_lock);
692 
693 	if (!ocqe) {
694 		struct io_rings *r = ctx->rings;
695 
696 		/*
697 		 * If we're in ring overflow flush mode, or in task cancel mode,
698 		 * or cannot allocate an overflow entry, then we need to drop it
699 		 * on the floor.
700 		 */
701 		WRITE_ONCE(r->cq_overflow, READ_ONCE(r->cq_overflow) + 1);
702 		set_bit(IO_CHECK_CQ_DROPPED_BIT, &ctx->check_cq);
703 		return false;
704 	}
705 	if (list_empty(&ctx->cq_overflow_list)) {
706 		set_bit(IO_CHECK_CQ_OVERFLOW_BIT, &ctx->check_cq);
707 		atomic_or(IORING_SQ_CQ_OVERFLOW, &ctx->rings->sq_flags);
708 
709 	}
710 	list_add_tail(&ocqe->list, &ctx->cq_overflow_list);
711 	return true;
712 }
713 
714 static struct io_overflow_cqe *io_alloc_ocqe(struct io_ring_ctx *ctx,
715 					     struct io_cqe *cqe,
716 					     struct io_big_cqe *big_cqe, gfp_t gfp)
717 {
718 	struct io_overflow_cqe *ocqe;
719 	size_t ocq_size = sizeof(struct io_overflow_cqe);
720 	bool is_cqe32 = false;
721 
722 	if (cqe->flags & IORING_CQE_F_32 || ctx->flags & IORING_SETUP_CQE32) {
723 		is_cqe32 = true;
724 		ocq_size += sizeof(struct io_uring_cqe);
725 	}
726 
727 	ocqe = kzalloc(ocq_size, gfp | __GFP_ACCOUNT);
728 	trace_io_uring_cqe_overflow(ctx, cqe->user_data, cqe->res, cqe->flags, ocqe);
729 	if (ocqe) {
730 		ocqe->cqe.user_data = cqe->user_data;
731 		ocqe->cqe.res = cqe->res;
732 		ocqe->cqe.flags = cqe->flags;
733 		if (is_cqe32 && big_cqe) {
734 			ocqe->cqe.big_cqe[0] = big_cqe->extra1;
735 			ocqe->cqe.big_cqe[1] = big_cqe->extra2;
736 		}
737 	}
738 	if (big_cqe)
739 		big_cqe->extra1 = big_cqe->extra2 = 0;
740 	return ocqe;
741 }
742 
743 /*
744  * Compute queued CQEs for free-space calculation, clamped to cq_entries.
745  */
746 static unsigned int io_cqring_queued(struct io_ring_ctx *ctx)
747 {
748 	struct io_rings *rings = io_get_rings(ctx);
749 	int diff;
750 
751 	diff = (int)(ctx->cached_cq_tail - READ_ONCE(rings->cq.head));
752 	if (diff >= 0)
753 		return min((unsigned int)diff, ctx->cq_entries);
754 	return 0;
755 }
756 
757 /*
758  * Fill an empty dummy CQE, in case alignment is off for posting a 32b CQE
759  * because the ring is a single 16b entry away from wrapping.
760  */
761 static bool io_fill_nop_cqe(struct io_ring_ctx *ctx, unsigned int off)
762 {
763 	if (io_cqring_queued(ctx) < ctx->cq_entries) {
764 		struct io_uring_cqe *cqe = &ctx->rings->cqes[off];
765 
766 		cqe->user_data = 0;
767 		cqe->res = 0;
768 		cqe->flags = IORING_CQE_F_SKIP;
769 		ctx->cached_cq_tail++;
770 		return true;
771 	}
772 	return false;
773 }
774 
775 /*
776  * writes to the cq entry need to come after reading head; the
777  * control dependency is enough as we're using WRITE_ONCE to
778  * fill the cq entry
779  */
780 bool io_cqe_cache_refill(struct io_ring_ctx *ctx, bool overflow, bool cqe32)
781 {
782 	struct io_rings *rings = ctx->rings;
783 	unsigned int off = ctx->cached_cq_tail & (ctx->cq_entries - 1);
784 	unsigned int free, len;
785 
786 	/*
787 	 * Posting into the CQ when there are pending overflowed CQEs may break
788 	 * ordering guarantees, which will affect links, F_MORE users and more.
789 	 * Force overflow the completion.
790 	 */
791 	if (!overflow && (ctx->check_cq & BIT(IO_CHECK_CQ_OVERFLOW_BIT)))
792 		return false;
793 
794 	/*
795 	 * A single entry may be left if a 32b CQE is asked for. It must not be
796 	 * handed out later on if this fails, as the CQE is going to overflow.
797 	 */
798 	ctx->cqe_cached = NULL;
799 	ctx->cqe_sentinel = NULL;
800 
801 	/*
802 	 * Post dummy CQE if a 32b CQE is needed and there's only room for a
803 	 * 16b CQE before the ring wraps.
804 	 */
805 	if (cqe32 && (ctx->flags & IORING_SETUP_CQE_MIXED) &&
806 	    off + 1 == ctx->cq_entries) {
807 		/* one for the dummy CQE, two for the 32b CQE itself */
808 		if (ctx->cq_entries - io_cqring_queued(ctx) < 3)
809 			return false;
810 		if (!io_fill_nop_cqe(ctx, off))
811 			return false;
812 		off = 0;
813 	}
814 
815 	free = ctx->cq_entries - io_cqring_queued(ctx);
816 	/* we need a contiguous range, limit based on the current array offset */
817 	len = min(free, ctx->cq_entries - off);
818 	if (ctx->flags & IORING_SETUP_CQE32) {
819 		if (!len)
820 			return false;
821 		off <<= 1;
822 		len <<= 1;
823 	} else if (len < (cqe32 + 1)) {
824 		return false;
825 	}
826 
827 	ctx->cqe_cached = &rings->cqes[off];
828 	ctx->cqe_sentinel = ctx->cqe_cached + len;
829 	return true;
830 }
831 
832 static bool io_fill_cqe_aux32(struct io_ring_ctx *ctx,
833 			      struct io_uring_cqe src_cqe[2])
834 {
835 	struct io_uring_cqe *cqe;
836 
837 	if (WARN_ON_ONCE(!(ctx->flags & (IORING_SETUP_CQE32|IORING_SETUP_CQE_MIXED))))
838 		return false;
839 	if (unlikely(!io_get_cqe(ctx, &cqe, true)))
840 		return false;
841 
842 	memcpy(cqe, src_cqe, 2 * sizeof(*cqe));
843 	trace_io_uring_complete(ctx, NULL, cqe);
844 	return true;
845 }
846 
847 static bool io_fill_cqe_aux(struct io_ring_ctx *ctx, u64 user_data, s32 res,
848 			      u32 cflags)
849 {
850 	bool cqe32 = cflags & IORING_CQE_F_32;
851 	struct io_uring_cqe *cqe;
852 
853 	if (likely(io_get_cqe(ctx, &cqe, cqe32))) {
854 		WRITE_ONCE(cqe->user_data, user_data);
855 		WRITE_ONCE(cqe->res, res);
856 		WRITE_ONCE(cqe->flags, cflags);
857 
858 		if (cqe32 || (ctx->flags & IORING_SETUP_CQE32)) {
859 			WRITE_ONCE(cqe->big_cqe[0], 0);
860 			WRITE_ONCE(cqe->big_cqe[1], 0);
861 		}
862 
863 		trace_io_uring_complete(ctx, NULL, cqe);
864 		return true;
865 	}
866 	return false;
867 }
868 
869 static inline struct io_cqe io_init_cqe(u64 user_data, s32 res, u32 cflags)
870 {
871 	return (struct io_cqe) { .user_data = user_data, .res = res, .flags = cflags };
872 }
873 
874 static __cold void io_cqe_overflow(struct io_ring_ctx *ctx, struct io_cqe *cqe,
875 				   struct io_big_cqe *big_cqe)
876 {
877 	struct io_overflow_cqe *ocqe;
878 
879 	ocqe = io_alloc_ocqe(ctx, cqe, big_cqe, GFP_KERNEL);
880 	spin_lock(&ctx->completion_lock);
881 	io_cqring_add_overflow(ctx, ocqe);
882 	spin_unlock(&ctx->completion_lock);
883 }
884 
885 static __cold bool io_cqe_overflow_locked(struct io_ring_ctx *ctx,
886 					  struct io_cqe *cqe,
887 					  struct io_big_cqe *big_cqe)
888 {
889 	struct io_overflow_cqe *ocqe;
890 
891 	ocqe = io_alloc_ocqe(ctx, cqe, big_cqe, GFP_NOWAIT);
892 	return io_cqring_add_overflow(ctx, ocqe);
893 }
894 
895 bool io_post_aux_cqe(struct io_ring_ctx *ctx, u64 user_data, s32 res, u32 cflags)
896 {
897 	bool filled;
898 
899 	io_cq_lock(ctx);
900 	filled = io_fill_cqe_aux(ctx, user_data, res, cflags);
901 	if (unlikely(!filled)) {
902 		struct io_cqe cqe = io_init_cqe(user_data, res, cflags);
903 
904 		filled = io_cqe_overflow_locked(ctx, &cqe, NULL);
905 	}
906 	io_cq_unlock_post(ctx);
907 	return filled;
908 }
909 
910 /*
911  * Must be called from inline task_work so we know a flush will happen later,
912  * and obviously with ctx->uring_lock held (tw always has that).
913  */
914 void io_add_aux_cqe(struct io_ring_ctx *ctx, u64 user_data, s32 res, u32 cflags)
915 {
916 	lockdep_assert_held(&ctx->uring_lock);
917 	lockdep_assert(ctx->int_flags & IO_RING_F_LOCKLESS_CQ);
918 
919 	if (!io_fill_cqe_aux(ctx, user_data, res, cflags)) {
920 		struct io_cqe cqe = io_init_cqe(user_data, res, cflags);
921 
922 		io_cqe_overflow(ctx, &cqe, NULL);
923 	}
924 	ctx->submit_state.cq_flush = true;
925 }
926 
927 /*
928  * A helper for multishot requests posting additional CQEs.
929  * Should only be used from a task_work including IO_URING_F_MULTISHOT.
930  */
931 bool io_req_post_cqe(struct io_kiocb *req, s32 res, u32 cflags)
932 {
933 	struct io_ring_ctx *ctx = req->ctx;
934 	bool posted;
935 
936 	/*
937 	 * If multishot has already posted deferred completions, ensure that
938 	 * those are flushed first before posting this one. If not, CQEs
939 	 * could get reordered.
940 	 */
941 	if (!wq_list_empty(&ctx->submit_state.compl_reqs))
942 		__io_submit_flush_completions(ctx);
943 
944 	lockdep_assert(!io_wq_current_is_worker());
945 	lockdep_assert_held(&ctx->uring_lock);
946 
947 	if (!(ctx->int_flags & IO_RING_F_LOCKLESS_CQ)) {
948 		spin_lock(&ctx->completion_lock);
949 		posted = io_fill_cqe_aux(ctx, req->cqe.user_data, res, cflags);
950 		spin_unlock(&ctx->completion_lock);
951 	} else {
952 		posted = io_fill_cqe_aux(ctx, req->cqe.user_data, res, cflags);
953 	}
954 
955 	ctx->submit_state.cq_flush = true;
956 	return posted;
957 }
958 
959 /*
960  * A helper for multishot requests posting additional CQEs.
961  * Should only be used from a task_work including IO_URING_F_MULTISHOT.
962  */
963 bool io_req_post_cqe32(struct io_kiocb *req, struct io_uring_cqe cqe[2])
964 {
965 	struct io_ring_ctx *ctx = req->ctx;
966 	bool posted;
967 
968 	lockdep_assert(!io_wq_current_is_worker());
969 	lockdep_assert_held(&ctx->uring_lock);
970 
971 	cqe[0].user_data = req->cqe.user_data;
972 	if (!(ctx->int_flags & IO_RING_F_LOCKLESS_CQ)) {
973 		spin_lock(&ctx->completion_lock);
974 		posted = io_fill_cqe_aux32(ctx, cqe);
975 		spin_unlock(&ctx->completion_lock);
976 	} else {
977 		posted = io_fill_cqe_aux32(ctx, cqe);
978 	}
979 
980 	ctx->submit_state.cq_flush = true;
981 	return posted;
982 }
983 
984 /* drop the request file before the CQE is posted, not deferred after it */
985 static void io_req_put_file(struct io_kiocb *req, bool sync)
986 {
987 	struct file *file = req->file;
988 
989 	if (!file || (req->flags & (REQ_F_FIXED_FILE | REQ_F_REISSUE)))
990 		return;
991 
992 	WRITE_ONCE(req->file, NULL);
993 	/* releasing a ring may wait on other rings, keep that deferred */
994 	if (sync && !io_is_uring_fops(file))
995 		__fput_sync(file);
996 	else
997 		fput(file);
998 }
999 
1000 static void io_req_complete_post(struct io_kiocb *req, unsigned issue_flags)
1001 {
1002 	struct io_ring_ctx *ctx = req->ctx;
1003 	bool completed = true;
1004 
1005 	/*
1006 	 * All execution paths but io-wq use the deferred completions by
1007 	 * passing IO_URING_F_COMPLETE_DEFER and thus should not end up here.
1008 	 */
1009 	if (WARN_ON_ONCE(!(issue_flags & IO_URING_F_IOWQ)))
1010 		return;
1011 
1012 	io_req_put_file(req, true);
1013 
1014 	/*
1015 	 * Handle special CQ sync cases via task_work. DEFER_TASKRUN requires
1016 	 * the submitter task context, IOPOLL protects with uring_lock. A drain
1017 	 * request ends the drain under uring_lock as its CQE is posted.
1018 	 */
1019 	if ((ctx->int_flags & IO_RING_F_LOCKLESS_CQ) ||
1020 	    (req->flags & (REQ_F_REISSUE | REQ_F_IO_DRAIN))) {
1021 defer_complete:
1022 		req->io_task_work.func = io_req_task_complete;
1023 		io_req_task_work_add(req);
1024 		return;
1025 	}
1026 
1027 	io_cq_lock(ctx);
1028 	if (!(req->flags & REQ_F_CQE_SKIP))
1029 		completed = io_fill_cqe_req(ctx, req);
1030 	io_cq_unlock_post(ctx);
1031 
1032 	if (!completed)
1033 		goto defer_complete;
1034 
1035 	/*
1036 	 * Request not freed here because we know it's called from io-wq only,
1037 	 * which holds a reference. Hence it can't be the last put. The CQE
1038 	 * has been posted, last put frees it.
1039 	 */
1040 	req->flags |= REQ_F_CQE_SKIP;
1041 	req_ref_put(req);
1042 }
1043 
1044 void io_req_defer_failed(struct io_kiocb *req, s32 res)
1045 	__must_hold(&ctx->uring_lock)
1046 {
1047 	const struct io_cold_def *def = &io_cold_defs[req->opcode];
1048 
1049 	lockdep_assert_held(&req->ctx->uring_lock);
1050 
1051 	req_set_fail(req);
1052 	io_req_set_res(req, res, io_put_kbuf(req, res, NULL));
1053 	if (def->fail)
1054 		def->fail(req);
1055 	io_req_complete_defer(req);
1056 }
1057 
1058 /*
1059  * A request might get retired back into the request caches even before opcode
1060  * handlers and io_issue_sqe() are done with it, e.g. inline completion path.
1061  * Because of that, io_alloc_req() should be called only under ->uring_lock
1062  * and with extra caution to not get a request that is still worked on.
1063  */
1064 __cold bool __io_alloc_req_refill(struct io_ring_ctx *ctx)
1065 	__must_hold(&ctx->uring_lock)
1066 {
1067 	gfp_t gfp = GFP_KERNEL | __GFP_NOWARN | __GFP_ZERO;
1068 	void *reqs[IO_REQ_ALLOC_BATCH];
1069 	int nr_reqs = ARRAY_SIZE(reqs);
1070 
1071 	/*
1072 	 * Bulk alloc is all-or-nothing. If we fail to get a batch, retry a
1073 	 * single allocation to be on the safe side.
1074 	 */
1075 	if (!kmem_cache_alloc_bulk(req_cachep, gfp, nr_reqs, reqs)) {
1076 		reqs[0] = kmem_cache_alloc(req_cachep, gfp);
1077 		if (!reqs[0])
1078 			return false;
1079 		nr_reqs = 1;
1080 	}
1081 
1082 	percpu_ref_get_many(&ctx->refs, nr_reqs);
1083 	ctx->nr_req_allocated += nr_reqs;
1084 
1085 	while (nr_reqs--)
1086 		io_req_add_to_cache(reqs[nr_reqs], ctx);
1087 	return true;
1088 }
1089 
1090 __cold void io_free_req(struct io_kiocb *req)
1091 {
1092 	/* refs were already put, restore them for io_req_task_complete() */
1093 	req->flags &= ~REQ_F_REFCOUNT;
1094 	req->io_task_work.func = io_req_task_complete;
1095 	io_req_task_work_add(req);
1096 }
1097 
1098 static void __io_req_find_next_prep(struct io_kiocb *req)
1099 {
1100 	struct io_ring_ctx *ctx = req->ctx;
1101 
1102 	spin_lock(&ctx->completion_lock);
1103 	io_disarm_next(req);
1104 	spin_unlock(&ctx->completion_lock);
1105 }
1106 
1107 static inline struct io_kiocb *io_req_find_next(struct io_kiocb *req)
1108 {
1109 	struct io_kiocb *nxt;
1110 
1111 	/*
1112 	 * If LINK is set, we have dependent requests in this chain. If we
1113 	 * didn't fail this request, queue the first one up, moving any other
1114 	 * dependencies to the next request. In case of failure, fail the rest
1115 	 * of the chain.
1116 	 */
1117 	if (unlikely(req->flags & IO_DISARM_MASK))
1118 		__io_req_find_next_prep(req);
1119 	nxt = req->link;
1120 	req->link = NULL;
1121 	return nxt;
1122 }
1123 
1124 static void io_req_task_cancel(struct io_tw_req tw_req, io_tw_token_t tw)
1125 {
1126 	struct io_kiocb *req = tw_req.req;
1127 
1128 	io_tw_lock(req->ctx, tw);
1129 	io_req_defer_failed(req, req->cqe.res);
1130 }
1131 
1132 void io_req_task_submit(struct io_tw_req tw_req, io_tw_token_t tw)
1133 {
1134 	struct io_kiocb *req = tw_req.req;
1135 	struct io_ring_ctx *ctx = req->ctx;
1136 
1137 	io_tw_lock(ctx, tw);
1138 	if (unlikely(tw.cancel))
1139 		io_req_defer_failed(req, -EFAULT);
1140 	else if (req->flags & REQ_F_FORCE_ASYNC)
1141 		io_queue_iowq(req);
1142 	else
1143 		io_queue_sqe(req, 0);
1144 }
1145 
1146 void io_req_task_queue_fail(struct io_kiocb *req, int ret)
1147 {
1148 	io_req_set_res(req, ret, 0);
1149 	req->io_task_work.func = io_req_task_cancel;
1150 	io_req_task_work_add(req);
1151 }
1152 
1153 void io_req_task_queue(struct io_kiocb *req)
1154 {
1155 	req->io_task_work.func = io_req_task_submit;
1156 	io_req_task_work_add(req);
1157 }
1158 
1159 void io_queue_next(struct io_kiocb *req)
1160 {
1161 	struct io_kiocb *nxt = io_req_find_next(req);
1162 
1163 	if (nxt)
1164 		io_req_task_queue(nxt);
1165 }
1166 
1167 static inline void io_req_put_rsrc_nodes(struct io_kiocb *req)
1168 {
1169 	struct io_ring_ctx *ctx = req->ctx;
1170 
1171 	if (req->file_node) {
1172 		io_put_rsrc_node(ctx, req->file_node);
1173 		req->file_node = NULL;
1174 	}
1175 	if (req->flags & REQ_F_BUF_NODE)
1176 		io_put_rsrc_node(ctx, req->buf_node);
1177 }
1178 
1179 static void io_free_batch_list(struct io_ring_ctx *ctx,
1180 			       struct io_wq_work_node *node)
1181 	__must_hold(&ctx->uring_lock)
1182 {
1183 	do {
1184 		struct io_kiocb *req = container_of(node, struct io_kiocb,
1185 						    comp_list);
1186 
1187 		if (unlikely(req->flags & IO_REQ_CLEAN_SLOW_FLAGS)) {
1188 			if (req->flags & REQ_F_REISSUE) {
1189 				node = req->comp_list.next;
1190 				req->flags &= ~REQ_F_REISSUE;
1191 				io_queue_iowq(req);
1192 				continue;
1193 			}
1194 			if (req->flags & REQ_F_REFCOUNT) {
1195 				node = req->comp_list.next;
1196 				/* CQE posted, the last put only frees */
1197 				req->flags |= REQ_F_CQE_SKIP;
1198 				if (!req_ref_put_and_test(req))
1199 					continue;
1200 			}
1201 			if ((req->flags & REQ_F_POLLED) && req->apoll) {
1202 				struct async_poll *apoll = req->apoll;
1203 
1204 				if (apoll->double_poll)
1205 					kfree(apoll->double_poll);
1206 				io_cache_free(&ctx->apoll_cache, apoll);
1207 				req->flags &= ~REQ_F_POLLED;
1208 			}
1209 			if (req->flags & IO_REQ_LINK_FLAGS)
1210 				io_queue_next(req);
1211 			if (unlikely(req->flags & IO_REQ_CLEAN_FLAGS))
1212 				io_clean_op(req);
1213 		}
1214 		io_put_file(req);
1215 		io_req_put_rsrc_nodes(req);
1216 		io_put_task(req);
1217 
1218 		node = req->comp_list.next;
1219 		io_req_add_to_cache(req, ctx);
1220 	} while (node);
1221 }
1222 
1223 /*
1224  * The dispatched drain request ran alone, and anything submitted after it was
1225  * deferred. Once it completed, new requests may run if nothing is waiting.
1226  */
1227 static __cold bool io_drain_req_completed(struct io_ring_ctx *ctx)
1228 {
1229 	struct io_wq_work_node *node;
1230 
1231 	__wq_list_for_each(node, &ctx->submit_state.compl_reqs) {
1232 		struct io_kiocb *req = container_of(node, struct io_kiocb,
1233 					    comp_list);
1234 
1235 		/* a requeued request hasn't completed */
1236 		if (req == ctx->drain_req && !(req->flags & REQ_F_REISSUE)) {
1237 			ctx->drain_req = NULL;
1238 			return true;
1239 		}
1240 	}
1241 	return false;
1242 }
1243 
1244 void __io_submit_flush_completions(struct io_ring_ctx *ctx)
1245 	__must_hold(&ctx->uring_lock)
1246 {
1247 	struct io_submit_state *state = &ctx->submit_state;
1248 	struct io_wq_work_node *node;
1249 	bool drain_done = false;
1250 
1251 	/*
1252 	 * Drop the files before the CQEs are posted, so they're released by
1253 	 * the time the completions are visible. Not for requests that are
1254 	 * requeued or still referenced, those aren't freed below.
1255 	 */
1256 	__wq_list_for_each(node, &state->compl_reqs) {
1257 		struct io_kiocb *req = container_of(node, struct io_kiocb,
1258 					    comp_list);
1259 
1260 		if (!io_req_shared(req))
1261 			io_req_put_file(req, true);
1262 	}
1263 
1264 	__io_cq_lock(ctx);
1265 	__wq_list_for_each(node, &state->compl_reqs) {
1266 		struct io_kiocb *req = container_of(node, struct io_kiocb,
1267 					    comp_list);
1268 
1269 		/*
1270 		 * Requests marked with REQUEUE should not post a CQE, they
1271 		 * will go through the io-wq retry machinery and post one
1272 		 * later.
1273 		 */
1274 		if (!(req->flags & (REQ_F_CQE_SKIP | REQ_F_REISSUE)) &&
1275 		    unlikely(!io_fill_cqe_req(ctx, req))) {
1276 			if (ctx->int_flags & IO_RING_F_LOCKLESS_CQ)
1277 				io_cqe_overflow(ctx, &req->cqe, &req->big_cqe);
1278 			else
1279 				io_cqe_overflow_locked(ctx, &req->cqe, &req->big_cqe);
1280 		}
1281 	}
1282 	__io_cq_unlock_post(ctx);
1283 
1284 	if (unlikely(ctx->int_flags & IO_RING_F_DRAIN_ACTIVE) && ctx->drain_req)
1285 		drain_done = io_drain_req_completed(ctx);
1286 
1287 	if (!wq_list_empty(&state->compl_reqs)) {
1288 		io_free_batch_list(ctx, state->compl_reqs.first);
1289 		INIT_WQ_LIST(&state->compl_reqs);
1290 	}
1291 
1292 	if (unlikely(ctx->int_flags & IO_RING_F_DRAIN_ACTIVE)) {
1293 		/* a drain request started from the list keeps the drain going */
1294 		if (io_queue_deferred(ctx))
1295 			drain_done = false;
1296 		if (drain_done && list_empty(&ctx->defer_list))
1297 			ctx->int_flags &= ~IO_RING_F_DRAIN_ACTIVE;
1298 	}
1299 
1300 	ctx->submit_state.cq_flush = false;
1301 }
1302 
1303 /*
1304  * We can't just wait for polled events to come to us, we have to actively
1305  * find and complete them.
1306  */
1307 __cold void io_iopoll_try_reap_events(struct io_ring_ctx *ctx)
1308 {
1309 	if (!(ctx->flags & IORING_SETUP_IOPOLL))
1310 		return;
1311 
1312 	mutex_lock(&ctx->uring_lock);
1313 	while (!list_empty(&ctx->iopoll_list)) {
1314 		/* let it sleep and repeat later if can't complete a request */
1315 		if (io_do_iopoll(ctx, true) == 0)
1316 			break;
1317 		/*
1318 		 * Ensure we allow local-to-the-cpu processing to take place,
1319 		 * in this case we need to ensure that we reap all events.
1320 		 * Also let task_work, etc. to progress by releasing the mutex
1321 		 */
1322 		if (need_resched()) {
1323 			mutex_unlock(&ctx->uring_lock);
1324 			cond_resched();
1325 			mutex_lock(&ctx->uring_lock);
1326 		}
1327 	}
1328 	mutex_unlock(&ctx->uring_lock);
1329 
1330 	if (ctx->flags & IORING_SETUP_DEFER_TASKRUN)
1331 		io_cancel_local_task_work(ctx);
1332 }
1333 
1334 static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned int min_events)
1335 {
1336 	unsigned long check_cq;
1337 
1338 	min_events = min(min_events, ctx->cq_entries);
1339 
1340 	lockdep_assert_held(&ctx->uring_lock);
1341 
1342 	if (!io_allowed_run_tw(ctx))
1343 		return -EEXIST;
1344 
1345 	check_cq = READ_ONCE(ctx->check_cq);
1346 	if (unlikely(check_cq)) {
1347 		if (check_cq & BIT(IO_CHECK_CQ_OVERFLOW_BIT))
1348 			__io_cqring_overflow_flush(ctx, false);
1349 		/*
1350 		 * Similarly do not spin if we have not informed the user of any
1351 		 * dropped CQE.
1352 		 */
1353 		if (check_cq & BIT(IO_CHECK_CQ_DROPPED_BIT))
1354 			return -EBADR;
1355 	}
1356 	/*
1357 	 * Don't enter poll loop if we already have events pending.
1358 	 * If we do, we can potentially be spinning for commands that
1359 	 * already triggered a CQE (eg in error).
1360 	 */
1361 	if (io_cqring_events(ctx))
1362 		return 0;
1363 
1364 	do {
1365 		int ret = 0;
1366 
1367 		/*
1368 		 * If a submit got punted to a workqueue, we can have the
1369 		 * application entering polling for a command before it gets
1370 		 * issued. That app will hold the uring_lock for the duration
1371 		 * of the poll right here, so we need to take a breather every
1372 		 * now and then to ensure that the issue has a chance to add
1373 		 * the poll to the issued list. Otherwise we can spin here
1374 		 * forever, while the workqueue is stuck trying to acquire the
1375 		 * very same mutex.
1376 		 */
1377 		if (list_empty(&ctx->iopoll_list) || io_task_work_pending(ctx)) {
1378 			(void) io_run_local_work_locked(ctx, min_events);
1379 
1380 			if (task_work_pending(current) || list_empty(&ctx->iopoll_list)) {
1381 				mutex_unlock(&ctx->uring_lock);
1382 				io_run_task_work();
1383 				mutex_lock(&ctx->uring_lock);
1384 			}
1385 			/* some requests don't go through iopoll_list */
1386 			if (list_empty(&ctx->iopoll_list))
1387 				break;
1388 		}
1389 		ret = io_do_iopoll(ctx, !min_events);
1390 		if (unlikely(ret < 0))
1391 			return ret;
1392 
1393 		if (task_sigpending(current))
1394 			return -EINTR;
1395 		if (need_resched())
1396 			break;
1397 	} while (io_cqring_events(ctx) < min_events);
1398 
1399 	return 0;
1400 }
1401 
1402 void io_req_task_complete(struct io_tw_req tw_req, io_tw_token_t tw)
1403 {
1404 	struct io_kiocb *req = tw_req.req;
1405 
1406 	if (io_req_complete_ready(req))
1407 		io_req_complete_defer(req);
1408 }
1409 
1410 /*
1411  * After the iocb has been issued, it's safe to be found on the poll list.
1412  * Adding the kiocb to the list AFTER submission ensures that we don't
1413  * find it from a io_do_iopoll() thread before the issuer is done
1414  * accessing the kiocb cookie.
1415  */
1416 static void io_iopoll_req_issued(struct io_kiocb *req, unsigned int issue_flags)
1417 {
1418 	struct io_ring_ctx *ctx = req->ctx;
1419 	const bool needs_lock = issue_flags & IO_URING_F_UNLOCKED;
1420 
1421 	/* workqueue context doesn't hold uring_lock, grab it now */
1422 	if (unlikely(needs_lock))
1423 		mutex_lock(&ctx->uring_lock);
1424 
1425 	/*
1426 	 * Track whether we have multiple files in our lists. This will impact
1427 	 * how we do polling eventually, not spinning if we're on potentially
1428 	 * different devices.
1429 	 */
1430 	if (list_empty(&ctx->iopoll_list)) {
1431 		ctx->poll_multi_queue = false;
1432 	} else if (!ctx->poll_multi_queue) {
1433 		struct io_kiocb *list_req;
1434 
1435 		list_req = list_first_entry(&ctx->iopoll_list, struct io_kiocb, iopoll_node);
1436 		if (list_req->file != req->file)
1437 			ctx->poll_multi_queue = true;
1438 	}
1439 
1440 	list_add_tail(&req->iopoll_node, &ctx->iopoll_list);
1441 
1442 	if (unlikely(needs_lock)) {
1443 		/*
1444 		 * If IORING_SETUP_SQPOLL is enabled, sqes are either handle
1445 		 * in sq thread task context or in io worker task context. If
1446 		 * current task context is sq thread, we don't need to check
1447 		 * whether should wake up sq thread.
1448 		 */
1449 		if ((ctx->flags & IORING_SETUP_SQPOLL) &&
1450 		    wq_has_sleeper(&ctx->sq_data->wait))
1451 			wake_up(&ctx->sq_data->wait);
1452 
1453 		mutex_unlock(&ctx->uring_lock);
1454 	}
1455 }
1456 
1457 io_req_flags_t io_file_get_flags(struct file *file)
1458 {
1459 	io_req_flags_t res = 0;
1460 
1461 	BUILD_BUG_ON(REQ_F_ISREG_BIT != REQ_F_SUPPORT_NOWAIT_BIT + 1);
1462 
1463 	if (S_ISREG(file_inode(file)->i_mode))
1464 		res |= REQ_F_ISREG;
1465 	if ((file->f_flags & O_NONBLOCK) || (file->f_mode & FMODE_NOWAIT))
1466 		res |= REQ_F_SUPPORT_NOWAIT;
1467 	return res;
1468 }
1469 
1470 static __cold void io_drain_req(struct io_kiocb *req)
1471 	__must_hold(&ctx->uring_lock)
1472 {
1473 	struct io_ring_ctx *ctx = req->ctx;
1474 	bool drain = req->flags & IOSQE_IO_DRAIN;
1475 	struct io_defer_entry *de;
1476 
1477 	de = kmalloc_obj(*de, GFP_KERNEL_ACCOUNT);
1478 	if (!de) {
1479 		io_req_defer_failed(req, -ENOMEM);
1480 		return;
1481 	}
1482 
1483 	io_prep_async_link(req);
1484 	trace_io_uring_defer(req);
1485 	de->req = req;
1486 
1487 	ctx->nr_drained += io_linked_nr(req);
1488 	list_add_tail(&de->list, &ctx->defer_list);
1489 	io_queue_deferred(ctx);
1490 	if (!drain && list_empty(&ctx->defer_list))
1491 		ctx->int_flags &= ~IO_RING_F_DRAIN_ACTIVE;
1492 }
1493 
1494 #define REQ_ISSUE_SLOW_FLAGS	(REQ_F_CREDS | REQ_F_ARM_LTIMEOUT)
1495 
1496 static inline int __io_issue_sqe(struct io_kiocb *req,
1497 				 unsigned int issue_flags,
1498 				 const struct io_issue_def *def)
1499 {
1500 	const struct cred *creds = NULL;
1501 	struct io_kiocb *link = NULL;
1502 	int ret;
1503 
1504 	if (unlikely(req->flags & REQ_ISSUE_SLOW_FLAGS)) {
1505 		if ((req->flags & REQ_F_CREDS) && req->creds != current_cred())
1506 			creds = override_creds(req->creds);
1507 		if (req->flags & REQ_F_ARM_LTIMEOUT)
1508 			link = __io_prep_linked_timeout(req);
1509 	}
1510 
1511 	if (!def->audit_skip)
1512 		audit_uring_entry(req->opcode);
1513 
1514 	ret = def->issue(req, issue_flags);
1515 
1516 	if (!def->audit_skip)
1517 		audit_uring_exit(!ret, ret);
1518 
1519 	if (unlikely(creds || link)) {
1520 		if (creds)
1521 			revert_creds(creds);
1522 		if (link)
1523 			io_queue_linked_timeout(link);
1524 	}
1525 
1526 	return ret;
1527 }
1528 
1529 static int io_issue_sqe(struct io_kiocb *req, unsigned int issue_flags)
1530 {
1531 	const struct io_issue_def *def = &io_issue_defs[req->opcode];
1532 	int ret;
1533 
1534 	if (unlikely(!io_assign_file(req, def, issue_flags)))
1535 		return -EBADF;
1536 
1537 	ret = __io_issue_sqe(req, issue_flags, def);
1538 
1539 	if (ret == IOU_COMPLETE) {
1540 		if (unlikely(req->cqe.res < 0)) {
1541 			if (req->cqe.res <= -ERESTARTSYS)
1542 				req->cqe.res = io_fixup_restart_res(req->cqe.res);
1543 			req_set_fail(req);
1544 		}
1545 		if (issue_flags & IO_URING_F_COMPLETE_DEFER)
1546 			io_req_complete_defer(req);
1547 		else
1548 			io_req_complete_post(req, issue_flags);
1549 
1550 		return 0;
1551 	}
1552 
1553 	if (ret == IOU_ISSUE_SKIP_COMPLETE) {
1554 		ret = 0;
1555 
1556 		if (req->flags & REQ_F_IOPOLL)
1557 			io_iopoll_req_issued(req, issue_flags);
1558 	}
1559 	if (unlikely(ret <= -ERESTARTSYS))
1560 		ret = io_fixup_restart_res(ret);
1561 	return ret;
1562 }
1563 
1564 int io_poll_issue(struct io_kiocb *req, io_tw_token_t tw)
1565 {
1566 	const unsigned int issue_flags = IO_URING_F_NONBLOCK |
1567 					 IO_URING_F_MULTISHOT |
1568 					 IO_URING_F_COMPLETE_DEFER;
1569 	int ret;
1570 
1571 	io_tw_lock(req->ctx, tw);
1572 
1573 	WARN_ON_ONCE(!req->file);
1574 	if (WARN_ON_ONCE(req->flags & REQ_F_IOPOLL))
1575 		return -EFAULT;
1576 
1577 	ret = __io_issue_sqe(req, issue_flags, &io_issue_defs[req->opcode]);
1578 
1579 	WARN_ON_ONCE(ret == IOU_ISSUE_SKIP_COMPLETE);
1580 	return ret;
1581 }
1582 
1583 /*
1584  * io-wq is done with @work. Returns true if it held the last reference, and
1585  * hands out the next request of the link then. The caller has to make sure
1586  * that cancellations can find the latter before it calls io_wq_free_work().
1587  */
1588 bool io_wq_put_work(struct io_wq_work *work, struct io_wq_work **linked)
1589 {
1590 	struct io_kiocb *req = container_of(work, struct io_kiocb, work);
1591 	struct io_kiocb *nxt = NULL;
1592 
1593 	*linked = NULL;
1594 	if (!req_ref_put_and_test_atomic(req))
1595 		return false;
1596 
1597 	/*
1598 	 * Only continue the link from here if the completion has been posted.
1599 	 * If it's still pending, io_free_req() completes the request and the
1600 	 * next link follows from there, after the CQE.
1601 	 */
1602 	if ((req->flags & IO_REQ_LINK_FLAGS) && (req->flags & REQ_F_CQE_SKIP)) {
1603 		struct io_ring_ctx *ctx = req->ctx;
1604 
1605 		mutex_lock(&ctx->uring_lock);
1606 		nxt = io_req_find_next(req);
1607 		mutex_unlock(&ctx->uring_lock);
1608 	}
1609 	if (nxt)
1610 		*linked = &nxt->work;
1611 	return true;
1612 }
1613 
1614 void io_wq_free_work(struct io_wq_work *work)
1615 {
1616 	io_free_req(container_of(work, struct io_kiocb, work));
1617 }
1618 
1619 void io_wq_submit_work(struct io_wq_work *work)
1620 {
1621 	struct io_kiocb *req = container_of(work, struct io_kiocb, work);
1622 	const struct io_issue_def *def = &io_issue_defs[req->opcode];
1623 	unsigned int issue_flags = IO_URING_F_UNLOCKED | IO_URING_F_IOWQ;
1624 	bool needs_poll = false;
1625 	int ret = 0, err = -ECANCELED;
1626 
1627 	/* one will be dropped by io_wq_put_work() after returning to io-wq */
1628 	if (!(req->flags & REQ_F_REFCOUNT))
1629 		__io_req_set_refcount(req, 2);
1630 	else
1631 		req_ref_get(req);
1632 
1633 	/* either cancelled or io-wq is dying, so don't touch tctx->iowq */
1634 	if (atomic_read(&work->flags) & IO_WQ_WORK_CANCEL) {
1635 fail:
1636 		io_req_put_file(req, false);
1637 		io_req_task_queue_fail(req, err);
1638 		return;
1639 	}
1640 	if (!io_assign_file(req, def, issue_flags)) {
1641 		err = -EBADF;
1642 		atomic_or(IO_WQ_WORK_CANCEL, &work->flags);
1643 		goto fail;
1644 	}
1645 
1646 	/*
1647 	 * If DEFER_TASKRUN is set, it's only allowed to post CQEs from the
1648 	 * submitter task context. Final request completions are handed to the
1649 	 * right context, however this is not the case of auxiliary CQEs,
1650 	 * which is the main mean of operation for multishot requests.
1651 	 * Don't allow any multishot execution from io-wq. It's more restrictive
1652 	 * than necessary and also cleaner.
1653 	 */
1654 	if (req->flags & (REQ_F_MULTISHOT|REQ_F_APOLL_MULTISHOT)) {
1655 		err = -EBADFD;
1656 		if (!io_file_can_poll(req))
1657 			goto fail;
1658 		if (req->file->f_flags & O_NONBLOCK ||
1659 		    req->file->f_mode & FMODE_NOWAIT) {
1660 			err = -ECANCELED;
1661 			if (io_arm_poll_handler(req, issue_flags) != IO_APOLL_OK)
1662 				goto fail;
1663 			return;
1664 		} else {
1665 			req->flags &= ~(REQ_F_APOLL_MULTISHOT|REQ_F_MULTISHOT);
1666 		}
1667 	}
1668 
1669 	if (req->flags & REQ_F_FORCE_ASYNC) {
1670 		bool opcode_poll = def->pollin || def->pollout;
1671 
1672 		if (opcode_poll && io_file_can_poll(req)) {
1673 			needs_poll = true;
1674 			issue_flags |= IO_URING_F_NONBLOCK;
1675 		}
1676 	}
1677 
1678 	do {
1679 		ret = io_issue_sqe(req, issue_flags);
1680 		if (ret != -EAGAIN)
1681 			break;
1682 
1683 		/*
1684 		 * If REQ_F_NOWAIT is set, then don't wait or retry with
1685 		 * poll. -EAGAIN is final for that case.
1686 		 */
1687 		if (req->flags & REQ_F_NOWAIT)
1688 			break;
1689 
1690 		/*
1691 		 * We can get EAGAIN for iopolled IO even though we're
1692 		 * forcing a sync submission from here, since we can't
1693 		 * wait for request slots on the block side.
1694 		 */
1695 		if (!needs_poll) {
1696 			if (!(req->flags & REQ_F_IOPOLL))
1697 				break;
1698 			if (io_wq_worker_stopped())
1699 				break;
1700 			cond_resched();
1701 			continue;
1702 		}
1703 
1704 		if (io_arm_poll_handler(req, issue_flags) == IO_APOLL_OK)
1705 			return;
1706 		/* aborted or ready, in either case retry blocking */
1707 		needs_poll = false;
1708 		issue_flags &= ~IO_URING_F_NONBLOCK;
1709 	} while (1);
1710 
1711 	/* avoid locking problems by failing it from a clean context */
1712 	if (ret) {
1713 		io_req_put_file(req, true);
1714 		io_req_task_queue_fail(req, ret);
1715 	}
1716 }
1717 
1718 inline struct file *io_file_get_fixed(struct io_kiocb *req, int fd,
1719 				      unsigned int issue_flags)
1720 {
1721 	struct io_ring_ctx *ctx = req->ctx;
1722 	struct io_rsrc_node *node;
1723 	struct file *file = NULL;
1724 
1725 	io_ring_submit_lock(ctx, issue_flags);
1726 	node = io_rsrc_node_lookup(&ctx->file_table.data, fd);
1727 	if (node) {
1728 		node->refs++;
1729 		req->file_node = node;
1730 		req->flags |= io_slot_flags(node);
1731 		file = io_slot_file(node);
1732 	}
1733 	io_ring_submit_unlock(ctx, issue_flags);
1734 	return file;
1735 }
1736 
1737 struct file *io_file_get_normal(struct io_kiocb *req, int fd)
1738 {
1739 	struct file *file = fget(fd);
1740 
1741 	trace_io_uring_file_get(req, fd);
1742 
1743 	/* we don't allow fixed io_uring files */
1744 	if (file && io_is_uring_fops(file))
1745 		io_req_track_inflight(req);
1746 	return file;
1747 }
1748 
1749 static int io_req_sqe_copy(struct io_kiocb *req, unsigned int issue_flags)
1750 {
1751 	const struct io_cold_def *def = &io_cold_defs[req->opcode];
1752 
1753 	if (req->flags & REQ_F_SQE_COPIED)
1754 		return 0;
1755 	req->flags |= REQ_F_SQE_COPIED;
1756 	if (!def->sqe_copy)
1757 		return 0;
1758 	if (WARN_ON_ONCE(!(issue_flags & IO_URING_F_INLINE)))
1759 		return -EFAULT;
1760 	def->sqe_copy(req);
1761 	return 0;
1762 }
1763 
1764 static void io_queue_async(struct io_kiocb *req, unsigned int issue_flags, int ret)
1765 	__must_hold(&req->ctx->uring_lock)
1766 {
1767 	if (ret != -EAGAIN || (req->flags & REQ_F_NOWAIT)) {
1768 fail:
1769 		io_req_defer_failed(req, ret);
1770 		return;
1771 	}
1772 
1773 	ret = io_req_sqe_copy(req, issue_flags);
1774 	if (unlikely(ret))
1775 		goto fail;
1776 
1777 	switch (io_arm_poll_handler(req, 0)) {
1778 	case IO_APOLL_READY:
1779 		io_req_task_queue(req);
1780 		break;
1781 	case IO_APOLL_ABORTED:
1782 		io_queue_iowq(req);
1783 		break;
1784 	case IO_APOLL_OK:
1785 		break;
1786 	}
1787 }
1788 
1789 static inline void io_queue_sqe(struct io_kiocb *req, unsigned int extra_flags)
1790 	__must_hold(&req->ctx->uring_lock)
1791 {
1792 	unsigned int issue_flags = IO_URING_F_NONBLOCK |
1793 				   IO_URING_F_COMPLETE_DEFER | extra_flags;
1794 	int ret;
1795 
1796 	ret = io_issue_sqe(req, issue_flags);
1797 
1798 	/*
1799 	 * We async punt it if the file wasn't marked NOWAIT, or if the file
1800 	 * doesn't support non-blocking read/write attempts
1801 	 */
1802 	if (unlikely(ret))
1803 		io_queue_async(req, issue_flags, ret);
1804 }
1805 
1806 static void io_queue_sqe_fallback(struct io_kiocb *req)
1807 	__must_hold(&req->ctx->uring_lock)
1808 {
1809 	if (unlikely(req->flags & REQ_F_FAIL)) {
1810 		/*
1811 		 * We don't submit, fail them all, for that replace hardlinks
1812 		 * with normal links. Extra REQ_F_LINK is tolerated.
1813 		 */
1814 		req->flags &= ~REQ_F_HARDLINK;
1815 		req->flags |= REQ_F_LINK;
1816 		io_req_defer_failed(req, req->cqe.res);
1817 	} else {
1818 		/* can't fail with IO_URING_F_INLINE */
1819 		io_req_sqe_copy(req, IO_URING_F_INLINE);
1820 		if (unlikely(req->ctx->int_flags & IO_RING_F_DRAIN_ACTIVE))
1821 			io_drain_req(req);
1822 		else
1823 			io_queue_iowq(req);
1824 	}
1825 }
1826 
1827 static void io_init_drain(struct io_ring_ctx *ctx)
1828 {
1829 	struct io_kiocb *head = ctx->submit_state.link.head;
1830 
1831 	ctx->int_flags |= IO_RING_F_DRAIN_ACTIVE;
1832 	if (head) {
1833 		/*
1834 		 * If we need to drain a request in the middle of a link, drain
1835 		 * the head request and the next request/link after the current
1836 		 * link. Considering sequential execution of links,
1837 		 * REQ_F_IO_DRAIN will be maintained for every request of our
1838 		 * link.
1839 		 */
1840 		head->flags |= REQ_F_IO_DRAIN | REQ_F_FORCE_ASYNC;
1841 		ctx->int_flags |= IO_RING_F_DRAIN_NEXT;
1842 	}
1843 }
1844 
1845 static __cold int io_init_fail_req(struct io_kiocb *req, int err)
1846 {
1847 	/* ensure per-opcode data is cleared if we fail before prep */
1848 	memset(&req->cmd.data, 0, sizeof(req->cmd.data));
1849 	return err;
1850 }
1851 
1852 static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req,
1853 		       const struct io_uring_sqe *sqe, unsigned int *left)
1854 	__must_hold(&ctx->uring_lock)
1855 {
1856 	const struct io_issue_def *def;
1857 	unsigned int sqe_flags;
1858 	int personality;
1859 
1860 	req->ctx = ctx;
1861 	req->opcode = READ_ONCE(sqe->opcode);
1862 	/* same numerical values with corresponding REQ_F_*, safe to copy */
1863 	sqe_flags = READ_ONCE(sqe->flags);
1864 	req->flags = (__force io_req_flags_t) sqe_flags;
1865 	req->cqe.user_data = READ_ONCE(sqe->user_data);
1866 	req->file = NULL;
1867 	req->tctx = current->io_uring;
1868 	req->cancel_seq_set = false;
1869 	req->async_data = NULL;
1870 
1871 	if (unlikely(req->opcode >= IORING_OP_LAST)) {
1872 		req->opcode = 0;
1873 		return io_init_fail_req(req, -EINVAL);
1874 	}
1875 	req->opcode = array_index_nospec(req->opcode, IORING_OP_LAST);
1876 
1877 	def = &io_issue_defs[req->opcode];
1878 	if (def->is_128 && !(ctx->flags & IORING_SETUP_SQE128)) {
1879 		/*
1880 		 * A 128b op on a non-128b SQ requires mixed SQE support as
1881 		 * well as 2 contiguous entries.
1882 		 */
1883 		if (!(ctx->flags & IORING_SETUP_SQE_MIXED) ||
1884 		    (unsigned)(sqe - ctx->sq_sqes) >= ctx->sq_entries - 1)
1885 			return io_init_fail_req(req, -EINVAL);
1886 		if (*left >= 2) {
1887 			/*
1888 			 * A 128b operation on a mixed SQ uses two entries, so
1889 			 * we have to increment the head and cached refs, and
1890 			 * decrement what's left.
1891 			 */
1892 			current->io_uring->cached_refs++;
1893 			(*left)--;
1894 		} else if (!(ctx->flags & IORING_SETUP_SQPOLL) ||
1895 			   !__io_sqring_entries(ctx)) {
1896 			return io_init_fail_req(req, -EINVAL);
1897 		}
1898 		/*
1899 		 * else: the SQPOLL thread caps the number of entries it submits
1900 		 * in one go, which is none of the application's business. Take
1901 		 * the 2nd entry on top.
1902 		 */
1903 		ctx->cached_sq_head++;
1904 	}
1905 
1906 	if (unlikely(sqe_flags & ~SQE_COMMON_FLAGS)) {
1907 		/* enforce forwards compatibility on users */
1908 		if (sqe_flags & ~SQE_VALID_FLAGS)
1909 			return io_init_fail_req(req, -EINVAL);
1910 		if (sqe_flags & IOSQE_BUFFER_SELECT) {
1911 			if (!def->buffer_select)
1912 				return io_init_fail_req(req, -EOPNOTSUPP);
1913 			req->buf_index = READ_ONCE(sqe->buf_group);
1914 		}
1915 		if (sqe_flags & IOSQE_CQE_SKIP_SUCCESS)
1916 			ctx->int_flags |= IO_RING_F_DRAIN_DISABLED;
1917 		if (sqe_flags & IOSQE_IO_DRAIN) {
1918 			if (ctx->int_flags & IO_RING_F_DRAIN_DISABLED)
1919 				return io_init_fail_req(req, -EOPNOTSUPP);
1920 			io_init_drain(ctx);
1921 		}
1922 	}
1923 	if (unlikely(ctx->int_flags & (IO_RING_F_OP_RESTRICTED | IO_RING_F_DRAIN_ACTIVE | IO_RING_F_DRAIN_NEXT))) {
1924 		if (!io_check_restriction(&ctx->restrictions, req->opcode,
1925 					  sqe_flags))
1926 			return io_init_fail_req(req, -EACCES);
1927 		/* knock it to the slow queue path, will be drained there */
1928 		if (ctx->int_flags & IO_RING_F_DRAIN_ACTIVE)
1929 			req->flags |= REQ_F_FORCE_ASYNC;
1930 		/* if there is no link, we're at "next" request and need to drain */
1931 		if (unlikely(ctx->int_flags & IO_RING_F_DRAIN_NEXT) && !ctx->submit_state.link.head) {
1932 			ctx->int_flags &= ~IO_RING_F_DRAIN_NEXT;
1933 			ctx->int_flags |= IO_RING_F_DRAIN_ACTIVE;
1934 			req->flags |= REQ_F_IO_DRAIN | REQ_F_FORCE_ASYNC;
1935 		}
1936 	}
1937 
1938 	if (!def->ioprio && sqe->ioprio)
1939 		return io_init_fail_req(req, -EINVAL);
1940 	if (!def->iopoll && (ctx->flags & IORING_SETUP_IOPOLL))
1941 		return io_init_fail_req(req, -EINVAL);
1942 
1943 	if (def->needs_file) {
1944 		struct io_submit_state *state = &ctx->submit_state;
1945 
1946 		req->cqe.fd = READ_ONCE(sqe->fd);
1947 
1948 		/*
1949 		 * Plug now if we have more than 2 IO left after this, and the
1950 		 * target is potentially a read/write to block based storage.
1951 		 */
1952 		if (state->need_plug && def->plug) {
1953 			state->plug_started = true;
1954 			state->need_plug = false;
1955 			blk_start_plug_nr_ios(&state->plug, state->submit_nr);
1956 		}
1957 	}
1958 
1959 	personality = READ_ONCE(sqe->personality);
1960 	if (personality) {
1961 		int ret;
1962 
1963 		req->creds = xa_load(&ctx->personalities, personality);
1964 		if (!req->creds)
1965 			return io_init_fail_req(req, -EINVAL);
1966 		get_cred(req->creds);
1967 		ret = security_uring_override_creds(req->creds);
1968 		if (ret) {
1969 			put_cred(req->creds);
1970 			return io_init_fail_req(req, ret);
1971 		}
1972 		req->flags |= REQ_F_CREDS;
1973 	}
1974 
1975 	return def->prep(req, sqe);
1976 }
1977 
1978 static __cold int io_submit_fail_init(const struct io_uring_sqe *sqe,
1979 				      struct io_kiocb *req, int ret)
1980 {
1981 	struct io_ring_ctx *ctx = req->ctx;
1982 	struct io_submit_link *link = &ctx->submit_state.link;
1983 	struct io_kiocb *head = link->head;
1984 
1985 	trace_io_uring_req_failed(sqe, req, ret);
1986 
1987 	/*
1988 	 * Avoid breaking links in the middle as it renders links with SQPOLL
1989 	 * unusable. Instead of failing eagerly, continue assembling the link if
1990 	 * applicable and mark the head with REQ_F_FAIL. The link flushing code
1991 	 * should find the flag and handle the rest.
1992 	 */
1993 	req_fail_link_node(req, ret);
1994 	if (head && !(head->flags & REQ_F_FAIL))
1995 		req_fail_link_node(head, -ECANCELED);
1996 
1997 	if (!(req->flags & IO_REQ_LINK_FLAGS)) {
1998 		if (head) {
1999 			link->last->link = req;
2000 			link->head = NULL;
2001 			req = head;
2002 		}
2003 		io_queue_sqe_fallback(req);
2004 		return ret;
2005 	}
2006 
2007 	if (head)
2008 		link->last->link = req;
2009 	else
2010 		link->head = req;
2011 	link->last = req;
2012 	return 0;
2013 }
2014 
2015 static inline int io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req,
2016 			 const struct io_uring_sqe *sqe, unsigned int *left)
2017 	__must_hold(&ctx->uring_lock)
2018 {
2019 	struct io_submit_link *link = &ctx->submit_state.link;
2020 	int ret;
2021 
2022 	ret = io_init_req(ctx, req, sqe, left);
2023 	if (unlikely(ret))
2024 		return io_submit_fail_init(sqe, req, ret);
2025 
2026 	if (unlikely(ctx->bpf_filters)) {
2027 		ret = io_uring_run_bpf_filters(ctx->bpf_filters, req);
2028 		if (ret)
2029 			return io_submit_fail_init(sqe, req, ret);
2030 	}
2031 
2032 	trace_io_uring_submit_req(req);
2033 
2034 	/*
2035 	 * If we already have a head request, queue this one for async
2036 	 * submittal once the head completes. If we don't have a head but
2037 	 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
2038 	 * submitted sync once the chain is complete. If none of those
2039 	 * conditions are true (normal request), then just queue it.
2040 	 */
2041 	if (unlikely(link->head)) {
2042 		trace_io_uring_link(req, link->last);
2043 		io_req_sqe_copy(req, IO_URING_F_INLINE);
2044 		link->last->link = req;
2045 		link->last = req;
2046 
2047 		if (req->flags & IO_REQ_LINK_FLAGS)
2048 			return 0;
2049 		/* last request of the link, flush it */
2050 		req = link->head;
2051 		link->head = NULL;
2052 		if (req->flags & (REQ_F_FORCE_ASYNC | REQ_F_FAIL))
2053 			goto fallback;
2054 
2055 	} else if (unlikely(req->flags & (IO_REQ_LINK_FLAGS |
2056 					  REQ_F_FORCE_ASYNC | REQ_F_FAIL))) {
2057 		if (req->flags & IO_REQ_LINK_FLAGS) {
2058 			link->head = req;
2059 			link->last = req;
2060 		} else {
2061 fallback:
2062 			io_queue_sqe_fallback(req);
2063 		}
2064 		return 0;
2065 	}
2066 
2067 	io_queue_sqe(req, IO_URING_F_INLINE);
2068 	return 0;
2069 }
2070 
2071 /*
2072  * Batched submission is done, ensure local IO is flushed out.
2073  */
2074 static void io_submit_state_end(struct io_ring_ctx *ctx)
2075 {
2076 	struct io_submit_state *state = &ctx->submit_state;
2077 
2078 	if (unlikely(state->link.head))
2079 		io_queue_sqe_fallback(state->link.head);
2080 	/* flush only after queuing links as they can generate completions */
2081 	io_submit_flush_completions(ctx);
2082 	if (state->plug_started)
2083 		blk_finish_plug(&state->plug);
2084 }
2085 
2086 /*
2087  * Start submission side cache.
2088  */
2089 static void io_submit_state_start(struct io_submit_state *state,
2090 				  unsigned int max_ios)
2091 {
2092 	state->plug_started = false;
2093 	state->need_plug = max_ios > 2;
2094 	state->submit_nr = max_ios;
2095 	/* set only head, no need to init link_last in advance */
2096 	state->link.head = NULL;
2097 }
2098 
2099 static void io_commit_sqring(struct io_ring_ctx *ctx)
2100 {
2101 	struct io_rings *rings = ctx->rings;
2102 
2103 	if (ctx->flags & IORING_SETUP_SQ_REWIND) {
2104 		ctx->cached_sq_head = 0;
2105 	} else {
2106 		/*
2107 		 * Ensure any loads from the SQEs are done at this point,
2108 		 * since once we write the new head, the application could
2109 		 * write new data to them.
2110 		 */
2111 		smp_store_release(&rings->sq.head, ctx->cached_sq_head);
2112 	}
2113 }
2114 
2115 /*
2116  * Fetch an sqe, if one is available. Note this returns a pointer to memory
2117  * that is mapped by userspace. This means that care needs to be taken to
2118  * ensure that reads are stable, as we cannot rely on userspace always
2119  * being a good citizen. If members of the sqe are validated and then later
2120  * used, it's important that those reads are done through READ_ONCE() to
2121  * prevent a re-load down the line.
2122  */
2123 static bool io_get_sqe(struct io_ring_ctx *ctx, const struct io_uring_sqe **sqe)
2124 {
2125 	unsigned mask = ctx->sq_entries - 1;
2126 	unsigned head = ctx->cached_sq_head++ & mask;
2127 
2128 	if (static_branch_unlikely(&io_key_has_sqarray.key) &&
2129 	    (!(ctx->flags & IORING_SETUP_NO_SQARRAY))) {
2130 		head = READ_ONCE(ctx->sq_array[head]);
2131 		if (unlikely(head >= ctx->sq_entries)) {
2132 			WRITE_ONCE(ctx->rings->sq_dropped,
2133 				   READ_ONCE(ctx->rings->sq_dropped) + 1);
2134 			return false;
2135 		}
2136 		head = array_index_nospec(head, ctx->sq_entries);
2137 	}
2138 
2139 	/*
2140 	 * The cached sq head (or cq tail) serves two purposes:
2141 	 *
2142 	 * 1) allows us to batch the cost of updating the user visible
2143 	 *    head updates.
2144 	 * 2) allows the kernel side to track the head on its own, even
2145 	 *    though the application is the one updating it.
2146 	 */
2147 
2148 	/* double index for 128-byte SQEs, twice as long */
2149 	if (ctx->flags & IORING_SETUP_SQE128)
2150 		head <<= 1;
2151 	*sqe = &ctx->sq_sqes[head];
2152 	return true;
2153 }
2154 
2155 int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr)
2156 	__must_hold(&ctx->uring_lock)
2157 {
2158 	unsigned int entries;
2159 	unsigned int left;
2160 	int ret;
2161 
2162 	if (ctx->flags & IORING_SETUP_SQ_REWIND)
2163 		entries = ctx->sq_entries;
2164 	else
2165 		entries = __io_sqring_entries(ctx);
2166 
2167 	entries = min(nr, entries);
2168 	if (unlikely(!entries))
2169 		return 0;
2170 
2171 	ret = left = entries;
2172 	io_get_task_refs(left);
2173 	io_submit_state_start(&ctx->submit_state, left);
2174 
2175 	do {
2176 		const struct io_uring_sqe *sqe;
2177 		struct io_kiocb *req;
2178 
2179 		if (unlikely(!io_alloc_req(ctx, &req)))
2180 			break;
2181 		if (unlikely(!io_get_sqe(ctx, &sqe))) {
2182 			io_req_add_to_cache(req, ctx);
2183 			break;
2184 		}
2185 
2186 		/*
2187 		 * Continue submitting even for sqe failure if the
2188 		 * ring was setup with IORING_SETUP_SUBMIT_ALL
2189 		 */
2190 		if (unlikely(io_submit_sqe(ctx, req, sqe, &left)) &&
2191 		    !(ctx->flags & IORING_SETUP_SUBMIT_ALL)) {
2192 			left--;
2193 			break;
2194 		}
2195 	} while (--left);
2196 
2197 	if (unlikely(left)) {
2198 		ret -= left;
2199 		/* try again if it submitted nothing and can't allocate a req */
2200 		if (!ret && io_req_cache_empty(ctx))
2201 			ret = -EAGAIN;
2202 		current->io_uring->cached_refs += left;
2203 	}
2204 
2205 	io_submit_state_end(ctx);
2206 	 /* Commit SQ ring head once we've consumed and submitted all SQEs */
2207 	io_commit_sqring(ctx);
2208 	return ret;
2209 }
2210 
2211 static void io_rings_free(struct io_ring_ctx *ctx)
2212 {
2213 	/* ring memory is not charged to RLIMIT_MEMLOCK, hence no user */
2214 	io_free_region(NULL, &ctx->sq_region);
2215 	io_free_region(NULL, &ctx->ring_region);
2216 	ctx->rings = NULL;
2217 	RCU_INIT_POINTER(ctx->rings_rcu, NULL);
2218 	ctx->sq_sqes = NULL;
2219 }
2220 
2221 static int rings_size(unsigned int flags, unsigned int sq_entries,
2222 		      unsigned int cq_entries, struct io_rings_layout *rl)
2223 {
2224 	struct io_rings *rings;
2225 	size_t sqe_size;
2226 	size_t off;
2227 
2228 	if (flags & IORING_SETUP_CQE_MIXED) {
2229 		if (cq_entries < 2)
2230 			return -EOVERFLOW;
2231 	}
2232 	if (flags & IORING_SETUP_SQE_MIXED) {
2233 		if (sq_entries < 2)
2234 			return -EOVERFLOW;
2235 	}
2236 
2237 	rl->sq_array_offset = SIZE_MAX;
2238 
2239 	sqe_size = sizeof(struct io_uring_sqe);
2240 	if (flags & IORING_SETUP_SQE128)
2241 		sqe_size *= 2;
2242 
2243 	rl->sq_size = array_size(sqe_size, sq_entries);
2244 	if (rl->sq_size == SIZE_MAX)
2245 		return -EOVERFLOW;
2246 
2247 	off = struct_size(rings, cqes, cq_entries);
2248 	if (flags & IORING_SETUP_CQE32)
2249 		off = size_mul(off, 2);
2250 	if (off == SIZE_MAX)
2251 		return -EOVERFLOW;
2252 
2253 #ifdef CONFIG_SMP
2254 	off = ALIGN(off, SMP_CACHE_BYTES);
2255 	if (off == 0)
2256 		return -EOVERFLOW;
2257 #endif
2258 
2259 	if (!(flags & IORING_SETUP_NO_SQARRAY)) {
2260 		size_t sq_array_size;
2261 
2262 		rl->sq_array_offset = off;
2263 
2264 		sq_array_size = array_size(sizeof(u32), sq_entries);
2265 		off = size_add(off, sq_array_size);
2266 		if (off == SIZE_MAX)
2267 			return -EOVERFLOW;
2268 	}
2269 
2270 	rl->rings_size = off;
2271 	return 0;
2272 }
2273 
2274 static __cold void __io_req_caches_free(struct io_ring_ctx *ctx)
2275 {
2276 	struct io_kiocb *req;
2277 	int nr = 0;
2278 
2279 	while (!io_req_cache_empty(ctx)) {
2280 		req = io_extract_req(ctx);
2281 		io_poison_req(req);
2282 		kmem_cache_free(req_cachep, req);
2283 		nr++;
2284 	}
2285 	if (nr) {
2286 		ctx->nr_req_allocated -= nr;
2287 		percpu_ref_put_many(&ctx->refs, nr);
2288 	}
2289 }
2290 
2291 static __cold void io_req_caches_free(struct io_ring_ctx *ctx)
2292 {
2293 	guard(mutex)(&ctx->uring_lock);
2294 	__io_req_caches_free(ctx);
2295 }
2296 
2297 static __cold void io_ring_ctx_free(struct io_ring_ctx *ctx)
2298 {
2299 	io_unregister_bpf_ops(ctx);
2300 	io_sq_thread_finish(ctx);
2301 
2302 	mutex_lock(&ctx->uring_lock);
2303 	io_unregister_zcrx(ctx);
2304 	io_cqring_overflow_kill(ctx);
2305 	io_eventfd_unregister(ctx);
2306 	io_free_alloc_caches(ctx);
2307 	io_destroy_buffers(ctx);
2308 	io_free_region(ctx->user, &ctx->param_region);
2309 	mutex_unlock(&ctx->uring_lock);
2310 	if (ctx->sq_creds)
2311 		put_cred(ctx->sq_creds);
2312 	if (ctx->submitter_task)
2313 		put_task_struct(ctx->submitter_task);
2314 
2315 	WARN_ON_ONCE(!list_empty(&ctx->ltimeout_list));
2316 
2317 	if (ctx->mm_account) {
2318 		mmdrop(ctx->mm_account);
2319 		ctx->mm_account = NULL;
2320 	}
2321 	io_rings_free(ctx);
2322 
2323 	if (!(ctx->flags & IORING_SETUP_NO_SQARRAY))
2324 		static_branch_slow_dec_deferred(&io_key_has_sqarray);
2325 
2326 	percpu_ref_exit(&ctx->refs);
2327 	free_uid(ctx->user);
2328 	io_req_caches_free(ctx);
2329 
2330 	if (ctx->restrictions.bpf_filters) {
2331 		WARN_ON_ONCE(ctx->bpf_filters !=
2332 			     ctx->restrictions.bpf_filters->filters);
2333 	} else {
2334 		WARN_ON_ONCE(ctx->bpf_filters);
2335 	}
2336 	io_put_bpf_filters(&ctx->restrictions);
2337 
2338 	WARN_ON_ONCE(ctx->nr_req_allocated);
2339 
2340 	if (ctx->hash_map)
2341 		io_wq_put_hash(ctx->hash_map);
2342 	io_napi_free(ctx);
2343 	kvfree(ctx->cancel_table.hbs);
2344 	xa_destroy(&ctx->io_bl_xa);
2345 	xa_destroy(&ctx->hpage_acct);
2346 	xa_destroy(&ctx->personalities);
2347 	kfree(ctx);
2348 }
2349 
2350 static __cold void io_ring_ctx_free_work(struct work_struct *work)
2351 {
2352 	struct io_ring_ctx *ctx = container_of(to_rcu_work(work),
2353 					       struct io_ring_ctx, free_rwork);
2354 
2355 	io_ring_ctx_free(ctx);
2356 }
2357 
2358 static __cold void io_activate_pollwq_cb(struct callback_head *cb)
2359 {
2360 	struct io_ring_ctx *ctx = container_of(cb, struct io_ring_ctx,
2361 					       poll_wq_task_work);
2362 
2363 	mutex_lock(&ctx->uring_lock);
2364 	ctx->int_flags |= IO_RING_F_POLL_ACTIVATED;
2365 	mutex_unlock(&ctx->uring_lock);
2366 
2367 	/*
2368 	 * Wake ups for some events between start of polling and activation
2369 	 * might've been lost due to loose synchronisation.
2370 	 */
2371 	wake_up_all(&ctx->poll_wq);
2372 	percpu_ref_put(&ctx->refs);
2373 }
2374 
2375 __cold void io_activate_pollwq(struct io_ring_ctx *ctx)
2376 {
2377 	spin_lock(&ctx->completion_lock);
2378 	/* already activated or in progress */
2379 	if ((ctx->int_flags & IO_RING_F_POLL_ACTIVATED) || ctx->poll_wq_task_work.func)
2380 		goto out;
2381 	if (WARN_ON_ONCE(!(ctx->int_flags & IO_RING_F_TASK_COMPLETE)))
2382 		goto out;
2383 	if (!ctx->submitter_task)
2384 		goto out;
2385 	/*
2386 	 * with ->submitter_task only the submitter task completes requests, we
2387 	 * only need to sync with it, which is done by injecting a tw
2388 	 */
2389 	init_task_work(&ctx->poll_wq_task_work, io_activate_pollwq_cb);
2390 	percpu_ref_get(&ctx->refs);
2391 	if (task_work_add(ctx->submitter_task, &ctx->poll_wq_task_work, TWA_SIGNAL))
2392 		percpu_ref_put(&ctx->refs);
2393 out:
2394 	spin_unlock(&ctx->completion_lock);
2395 }
2396 
2397 static __poll_t io_uring_poll(struct file *file, poll_table *wait)
2398 {
2399 	struct io_ring_ctx *ctx = file->private_data;
2400 	__poll_t mask = 0;
2401 
2402 	/*
2403 	 * The poll_wait() barrier pairs with wq_has_sleeper() on the CQ
2404 	 * wakeup and ring enable paths.
2405 	 */
2406 	poll_wait(file, &ctx->poll_wq, wait);
2407 	/* A stale flag only causes a redundant activation attempt. */
2408 	if (unlikely(!(data_race(ctx->int_flags) & IO_RING_F_POLL_ACTIVATED)))
2409 		io_activate_pollwq(ctx);
2410 
2411 	rcu_read_lock();
2412 
2413 	if (!__io_sqring_full(ctx))
2414 		mask |= EPOLLOUT | EPOLLWRNORM;
2415 
2416 	/*
2417 	 * Don't flush cqring overflow list here, just do a simple check.
2418 	 * Otherwise there could possible be ABBA deadlock:
2419 	 *      CPU0                    CPU1
2420 	 *      ----                    ----
2421 	 * lock(&ctx->uring_lock);
2422 	 *                              lock(&ep->mtx);
2423 	 *                              lock(&ctx->uring_lock);
2424 	 * lock(&ep->mtx);
2425 	 *
2426 	 * Users may get EPOLLIN meanwhile seeing nothing in cqring, this
2427 	 * pushes them to do the flush.
2428 	 */
2429 
2430 	if (__io_cqring_events_user(ctx) || io_has_work(ctx))
2431 		mask |= EPOLLIN | EPOLLRDNORM;
2432 
2433 	rcu_read_unlock();
2434 	return mask;
2435 }
2436 
2437 struct io_tctx_exit {
2438 	struct callback_head		task_work;
2439 	struct completion		completion;
2440 	struct io_ring_ctx		*ctx;
2441 	bool				in_cancel;
2442 };
2443 
2444 static __cold void io_tctx_exit_cb(struct callback_head *cb)
2445 {
2446 	struct io_uring_task *tctx = current->io_uring;
2447 	struct io_tctx_exit *work;
2448 
2449 	work = container_of(cb, struct io_tctx_exit, task_work);
2450 	/*
2451 	 * When @in_cancel, we're in cancellation and it's racy to remove the
2452 	 * node. It'll be removed by the end of cancellation, just ignore it.
2453 	 * tctx can be NULL if the queueing of this task_work raced with
2454 	 * work cancelation off the exec path.
2455 	 */
2456 	work->in_cancel = tctx && atomic_read(&tctx->in_cancel);
2457 	if (tctx && !work->in_cancel)
2458 		io_uring_del_tctx_node((unsigned long)work->ctx);
2459 	complete(&work->completion);
2460 }
2461 
2462 /*
2463  * Cancel what can be canceled on a dying ring and reap what has completed.
2464  * Only waits on polled I/O, never anything else.
2465  */
2466 static __cold void io_ring_ctx_cancel(struct io_ring_ctx *ctx)
2467 {
2468 	struct io_sq_data *sqd = ctx->sq_data;
2469 
2470 	if (test_bit(IO_CHECK_CQ_OVERFLOW_BIT, &ctx->check_cq)) {
2471 		scoped_guard(mutex, &ctx->uring_lock)
2472 			io_cqring_overflow_kill(ctx);
2473 	}
2474 
2475 	/*
2476 	 * If we're doing polled IO and end up having requests being
2477 	 * submitted async (out-of-line), then completions can come in while
2478 	 * we're waiting for refs to drop. We need to reap these manually,
2479 	 * as nobody else will be looking for them.
2480 	 */
2481 	do {
2482 		if (ctx->flags & IORING_SETUP_DEFER_TASKRUN)
2483 			io_cancel_local_task_work(ctx);
2484 		cond_resched();
2485 	} while (io_uring_try_cancel_requests(ctx, NULL, IO_CANCEL_ALL));
2486 
2487 	if (sqd) {
2488 		struct task_struct *tsk;
2489 
2490 		io_sq_thread_park(sqd);
2491 		tsk = sqpoll_task_locked(sqd);
2492 		if (tsk && tsk->io_uring && tsk->io_uring->io_wq)
2493 			io_wq_cancel_cb(tsk->io_uring->io_wq,
2494 					io_cancel_ctx_cb, ctx, true);
2495 		io_sq_thread_unpark(sqd);
2496 	}
2497 
2498 	io_req_caches_free(ctx);
2499 }
2500 
2501 /* Number of requests that should be waited for */
2502 static __cold unsigned int io_ring_ctx_inflight(struct io_ring_ctx *ctx)
2503 {
2504 	guard(mutex)(&ctx->uring_lock);
2505 	__io_req_caches_free(ctx);
2506 	return ctx->nr_req_allocated - ctx->nr_notifs;
2507 }
2508 
2509 /*
2510  * Run task_work completions for current. Only do so if the io_uring callback
2511  * itself can get pruned first, otherwise we risk recursing.
2512  */
2513 static __cold bool io_ring_run_own_completions(struct io_uring_task *tctx)
2514 {
2515 	unsigned int count = 0;
2516 
2517 	if (!tctx || mpscq_empty(&tctx->task_list))
2518 		return true;
2519 	if (!task_work_cancel(current, &tctx->task_work))
2520 		return false;
2521 	tctx_task_work_run(tctx, UINT_MAX, &count);
2522 	return true;
2523 }
2524 
2525 /*
2526  * Requests may remain after cancelations have been run, as not all requests
2527  * are cancelable. Storage I/O is an example. Wait for those so that once
2528  * close(2) returns, files pinned by these requests have been released.
2529  */
2530 static __cold void io_ring_ctx_wait_inflight(struct io_ring_ctx *ctx)
2531 {
2532 	struct io_uring_task *tctx = current->io_uring;
2533 	bool ran_own = true;
2534 
2535 	if (current->flags & (PF_KTHREAD | PF_EXITING))
2536 		return;
2537 	/* Only wait if this task has attached to the ring */
2538 	if (!tctx || !xa_load(&tctx->xa, (unsigned long)ctx))
2539 		return;
2540 	/* Requests may sit in local work that only exit work can run */
2541 	if ((ctx->flags & IORING_SETUP_DEFER_TASKRUN) &&
2542 	    !io_local_work_cancel_allowed(ctx))
2543 		return;
2544 	if (atomic_read(&tctx->in_cancel))
2545 		return;
2546 
2547 	while (io_ring_ctx_inflight(ctx) && !fatal_signal_pending(current)) {
2548 		unsigned int state;
2549 
2550 		if (test_thread_flag(TIF_NOTIFY_SIGNAL)) {
2551 			clear_notify_signal();
2552 			if (task_work_pending(current))
2553 				set_notify_resume(current);
2554 		}
2555 		state = TASK_INTERRUPTIBLE;
2556 		if (signal_pending(current))
2557 			state = TASK_KILLABLE;
2558 		set_current_state(state | TASK_FREEZABLE);
2559 		/* don't sleep on work that's already there and that we can run */
2560 		if (ran_own && ((tctx && !mpscq_empty(&tctx->task_list)) ||
2561 		    io_local_work_pending(ctx)))
2562 			__set_current_state(TASK_RUNNING);
2563 		else
2564 			schedule_timeout(1);
2565 
2566 		/* completions may be queued behind us */
2567 		if (!io_ring_run_own_completions(tctx)) {
2568 			if (!ran_own)
2569 				break;
2570 			ran_own = false;
2571 		} else {
2572 			ran_own = true;
2573 		}
2574 		io_ring_ctx_cancel(ctx);
2575 	}
2576 }
2577 
2578 static __cold void io_ring_exit_work(struct work_struct *work)
2579 {
2580 	struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx, exit_work);
2581 	unsigned long timeout = jiffies + IO_URING_EXIT_WAIT_MAX;
2582 	unsigned long interval = HZ / 20;
2583 	struct io_tctx_exit exit;
2584 	struct io_tctx_node *node;
2585 	int ret;
2586 
2587 	do {
2588 		io_ring_ctx_cancel(ctx);
2589 
2590 		if (WARN_ON_ONCE(time_after(jiffies, timeout))) {
2591 			/* there is little hope left, don't run it too often */
2592 			interval = HZ * 60;
2593 		}
2594 		/*
2595 		 * This is really an uninterruptible wait, as it has to be
2596 		 * complete. But it's also run from a kworker, which doesn't
2597 		 * take signals, so it's fine to make it interruptible. This
2598 		 * avoids scenarios where we knowingly can wait much longer
2599 		 * on completions, for example if someone does a SIGSTOP on
2600 		 * a task that needs to finish task_work to make this loop
2601 		 * complete. That's a synthetic situation that should not
2602 		 * cause a stuck task backtrace, and hence a potential panic
2603 		 * on stuck tasks if that is enabled.
2604 		 */
2605 	} while (!wait_for_completion_interruptible_timeout(&ctx->ref_comp, interval));
2606 
2607 	init_completion(&exit.completion);
2608 	init_task_work(&exit.task_work, io_tctx_exit_cb);
2609 	exit.ctx = ctx;
2610 
2611 	mutex_lock(&ctx->uring_lock);
2612 	mutex_lock(&ctx->tctx_lock);
2613 	while (!list_empty(&ctx->tctx_list)) {
2614 		WARN_ON_ONCE(time_after(jiffies, timeout));
2615 
2616 		node = list_first_entry(&ctx->tctx_list, struct io_tctx_node,
2617 					ctx_node);
2618 		/* don't spin on a single task if cancellation failed */
2619 		list_rotate_left(&ctx->tctx_list);
2620 		ret = task_work_add(node->task, &exit.task_work, TWA_SIGNAL);
2621 		if (WARN_ON_ONCE(ret))
2622 			continue;
2623 
2624 		mutex_unlock(&ctx->tctx_lock);
2625 		mutex_unlock(&ctx->uring_lock);
2626 		/*
2627 		 * See comment above for
2628 		 * wait_for_completion_interruptible_timeout() on why this
2629 		 * wait is marked as interruptible.
2630 		 */
2631 		wait_for_completion_interruptible(&exit.completion);
2632 		/*
2633 		 * The task removes the node when it is done with its
2634 		 * cancellation. Give it the time for that: it doesn't get back
2635 		 * to it as long as it finds new task_work each time it has run
2636 		 * the old one, and we may be one of many who send it.
2637 		 */
2638 		if (exit.in_cancel)
2639 			schedule_timeout_idle(HZ / 20);
2640 		mutex_lock(&ctx->uring_lock);
2641 		mutex_lock(&ctx->tctx_lock);
2642 	}
2643 	mutex_unlock(&ctx->tctx_lock);
2644 	mutex_unlock(&ctx->uring_lock);
2645 	spin_lock(&ctx->completion_lock);
2646 	spin_unlock(&ctx->completion_lock);
2647 
2648 	/*
2649 	 * Free the ring after a grace period. This pairs with the RCU read
2650 	 * sections in the task_work add paths, which look at the rings and
2651 	 * the submitter task, and with the one in io_uring_del_tctx_node(),
2652 	 * which may still be unlocking ->tctx_lock.
2653 	 */
2654 	INIT_RCU_WORK(&ctx->free_rwork, io_ring_ctx_free_work);
2655 	queue_rcu_work(iou_wq, &ctx->free_rwork);
2656 }
2657 
2658 static __cold void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
2659 {
2660 	unsigned long index;
2661 	struct cred *creds;
2662 
2663 	mutex_lock(&ctx->uring_lock);
2664 	percpu_ref_kill(&ctx->refs);
2665 	xa_for_each(&ctx->personalities, index, creds)
2666 		io_unregister_personality(ctx, index);
2667 	io_terminate_zcrx(ctx);
2668 	/* Drop these now, rather than async, to unpin the files */
2669 	io_sqe_buffers_unregister(ctx);
2670 	io_sqe_files_unregister(ctx);
2671 	mutex_unlock(&ctx->uring_lock);
2672 
2673 	/*
2674 	 * Do the first round of cancelations upfront rather than leaving it
2675 	 * to to exit_work. Anything cancelable is then already on its way
2676 	 * out, and for requests owned by the task closing the ring, this
2677 	 * ensures any held files are put before close(2) returns.
2678 	 */
2679 	if (!(current->flags & PF_IO_WORKER)) {
2680 		io_ring_ctx_cancel(ctx);
2681 		io_ring_ctx_wait_inflight(ctx);
2682 		/* exit is past the drop in io_uring_cancel_generic() */
2683 		if (current->io_uring)
2684 			io_uring_drop_tctx_refs(current);
2685 	}
2686 
2687 	INIT_WORK(&ctx->exit_work, io_ring_exit_work);
2688 	/*
2689 	 * Use system_dfl_wq to avoid spawning tons of event kworkers
2690 	 * if we're exiting a ton of rings at the same time. It just adds
2691 	 * noise and overhead, there's no discernable change in runtime
2692 	 * over using system_percpu_wq.
2693 	 */
2694 	queue_work(iou_wq, &ctx->exit_work);
2695 }
2696 
2697 static int io_uring_release(struct inode *inode, struct file *file)
2698 {
2699 	struct io_ring_ctx *ctx = file->private_data;
2700 
2701 	file->private_data = NULL;
2702 	io_ring_ctx_wait_and_kill(ctx);
2703 	return 0;
2704 }
2705 
2706 static struct io_uring_reg_wait *io_get_ext_arg_reg(struct io_ring_ctx *ctx,
2707 			const struct io_uring_getevents_arg __user *uarg)
2708 {
2709 	unsigned long size = sizeof(struct io_uring_reg_wait);
2710 	unsigned long offset = (uintptr_t)uarg;
2711 	unsigned long end;
2712 
2713 	if (unlikely(offset % sizeof(long)))
2714 		return ERR_PTR(-EFAULT);
2715 
2716 	/* also protects from NULL ->cq_wait_arg as the size would be 0 */
2717 	if (unlikely(check_add_overflow(offset, size, &end) ||
2718 		     end > ctx->cq_wait_size))
2719 		return ERR_PTR(-EFAULT);
2720 
2721 	offset = array_index_nospec(offset, ctx->cq_wait_size - size);
2722 	return ctx->cq_wait_arg + offset;
2723 }
2724 
2725 static int io_validate_ext_arg(struct io_ring_ctx *ctx, unsigned flags,
2726 			       const void __user *argp, size_t argsz)
2727 {
2728 	struct io_uring_getevents_arg arg;
2729 
2730 	if (!(flags & IORING_ENTER_EXT_ARG))
2731 		return 0;
2732 	if (flags & IORING_ENTER_EXT_ARG_REG)
2733 		return -EINVAL;
2734 	if (argsz != sizeof(arg))
2735 		return -EINVAL;
2736 	if (copy_from_user(&arg, argp, sizeof(arg)))
2737 		return -EFAULT;
2738 	return 0;
2739 }
2740 
2741 static int io_get_ext_arg(struct io_ring_ctx *ctx, unsigned flags,
2742 			  const void __user *argp, struct ext_arg *ext_arg)
2743 {
2744 	const struct io_uring_getevents_arg __user *uarg = argp;
2745 	struct io_uring_getevents_arg arg;
2746 
2747 	ext_arg->iowait = !(flags & IORING_ENTER_NO_IOWAIT);
2748 
2749 	/*
2750 	 * If EXT_ARG isn't set, then we have no timespec and the argp pointer
2751 	 * is just a pointer to the sigset_t.
2752 	 */
2753 	if (!(flags & IORING_ENTER_EXT_ARG)) {
2754 		ext_arg->sig = (const sigset_t __user *) argp;
2755 		return 0;
2756 	}
2757 
2758 	if (flags & IORING_ENTER_EXT_ARG_REG) {
2759 		struct io_uring_reg_wait *w;
2760 
2761 		if (ext_arg->argsz != sizeof(struct io_uring_reg_wait))
2762 			return -EINVAL;
2763 		w = io_get_ext_arg_reg(ctx, argp);
2764 		if (IS_ERR(w))
2765 			return PTR_ERR(w);
2766 
2767 		if (w->flags & ~IORING_REG_WAIT_TS)
2768 			return -EINVAL;
2769 		ext_arg->min_time = READ_ONCE(w->min_wait_usec) * NSEC_PER_USEC;
2770 		ext_arg->sig = u64_to_user_ptr(READ_ONCE(w->sigmask));
2771 		ext_arg->argsz = READ_ONCE(w->sigmask_sz);
2772 		if (w->flags & IORING_REG_WAIT_TS) {
2773 			ext_arg->ts.tv_sec = READ_ONCE(w->ts.tv_sec);
2774 			ext_arg->ts.tv_nsec = READ_ONCE(w->ts.tv_nsec);
2775 			ext_arg->ts_set = true;
2776 		}
2777 		return 0;
2778 	}
2779 
2780 	/*
2781 	 * EXT_ARG is set - ensure we agree on the size of it and copy in our
2782 	 * timespec and sigset_t pointers if good.
2783 	 */
2784 	if (ext_arg->argsz != sizeof(arg))
2785 		return -EINVAL;
2786 #ifdef CONFIG_64BIT
2787 	if (!user_access_begin(uarg, sizeof(*uarg)))
2788 		return -EFAULT;
2789 	unsafe_get_user(arg.sigmask, &uarg->sigmask, uaccess_end);
2790 	unsafe_get_user(arg.sigmask_sz, &uarg->sigmask_sz, uaccess_end);
2791 	unsafe_get_user(arg.min_wait_usec, &uarg->min_wait_usec, uaccess_end);
2792 	unsafe_get_user(arg.ts, &uarg->ts, uaccess_end);
2793 	user_access_end();
2794 #else
2795 	if (copy_from_user(&arg, uarg, sizeof(arg)))
2796 		return -EFAULT;
2797 #endif
2798 	ext_arg->min_time = arg.min_wait_usec * NSEC_PER_USEC;
2799 	ext_arg->sig = u64_to_user_ptr(arg.sigmask);
2800 	ext_arg->argsz = arg.sigmask_sz;
2801 	if (arg.ts) {
2802 		if (get_timespec64(&ext_arg->ts, u64_to_user_ptr(arg.ts)))
2803 			return -EFAULT;
2804 		ext_arg->ts_set = true;
2805 	}
2806 	return 0;
2807 #ifdef CONFIG_64BIT
2808 uaccess_end:
2809 	user_access_end();
2810 	return -EFAULT;
2811 #endif
2812 }
2813 
2814 /*
2815  * Given an 'fd' value, return the ctx associated with if. If 'registered' is
2816  * true, then the registered index is used. Otherwise, the normal fd table.
2817  * Caller must call fput() on the returned file if it isn't a registered file,
2818  * unless it's an ERR_PTR.
2819  */
2820 struct file *io_uring_ctx_get_file(unsigned int fd, bool registered)
2821 {
2822 	struct file *file;
2823 
2824 	if (registered) {
2825 		/*
2826 		 * Ring fd has been registered via IORING_REGISTER_RING_FDS, we
2827 		 * need only dereference our task private array to find it.
2828 		 */
2829 		struct io_uring_task *tctx = current->io_uring;
2830 
2831 		if (unlikely(!tctx || fd >= IO_RINGFD_REG_MAX))
2832 			return ERR_PTR(-EINVAL);
2833 		fd = array_index_nospec(fd, IO_RINGFD_REG_MAX);
2834 		file = tctx->registered_rings[fd];
2835 	} else {
2836 		file = fget(fd);
2837 	}
2838 
2839 	if (unlikely(!file))
2840 		return ERR_PTR(-EBADF);
2841 	if (io_is_uring_fops(file))
2842 		return file;
2843 	if (!registered)
2844 		fput(file);
2845 	return ERR_PTR(-EOPNOTSUPP);
2846 }
2847 
2848 
2849 SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
2850 		u32, min_complete, u32, flags, const void __user *, argp,
2851 		size_t, argsz)
2852 {
2853 	struct io_ring_ctx *ctx;
2854 	struct file *file;
2855 	long ret;
2856 
2857 	if (unlikely(flags & ~IORING_ENTER_FLAGS))
2858 		return -EINVAL;
2859 
2860 	file = io_uring_ctx_get_file(fd, flags & IORING_ENTER_REGISTERED_RING);
2861 	if (IS_ERR(file))
2862 		return PTR_ERR(file);
2863 	ctx = file->private_data;
2864 	ret = -EBADFD;
2865 	/*
2866 	 * Keep IORING_SETUP_R_DISABLED check before submitter_task load
2867 	 * in io_uring_add_tctx_node() -> __io_uring_add_tctx_node_from_submit()
2868 	 */
2869 	if (unlikely(smp_load_acquire(&ctx->flags) & IORING_SETUP_R_DISABLED))
2870 		goto out;
2871 
2872 	if (io_has_loop_ops(ctx)) {
2873 		ret = io_run_loop(ctx);
2874 		goto out;
2875 	}
2876 
2877 	/*
2878 	 * For SQ polling, the thread will do all submissions and completions.
2879 	 * Just return the requested submit count, and wake the thread if
2880 	 * we were asked to.
2881 	 */
2882 	ret = 0;
2883 	if (ctx->flags & IORING_SETUP_SQPOLL) {
2884 		if (unlikely(ctx->sq_data->thread == NULL)) {
2885 			ret = -EOWNERDEAD;
2886 			goto out;
2887 		}
2888 		if (flags & IORING_ENTER_SQ_WAKEUP)
2889 			wake_up(&ctx->sq_data->wait);
2890 		if (flags & IORING_ENTER_SQ_WAIT)
2891 			io_sqpoll_wait_sq(ctx);
2892 
2893 		ret = to_submit;
2894 	} else if (to_submit) {
2895 		ret = io_uring_add_tctx_node(ctx);
2896 		if (unlikely(ret))
2897 			goto out;
2898 
2899 		mutex_lock(&ctx->uring_lock);
2900 		ret = io_submit_sqes(ctx, to_submit);
2901 		if (ret != to_submit) {
2902 			mutex_unlock(&ctx->uring_lock);
2903 			goto out;
2904 		}
2905 		if (flags & IORING_ENTER_GETEVENTS) {
2906 			if (ctx->int_flags & IO_RING_F_SYSCALL_IOPOLL)
2907 				goto iopoll_locked;
2908 			/*
2909 			 * Ignore errors, we'll soon call io_cqring_wait() and
2910 			 * it should handle ownership problems if any.
2911 			 */
2912 			if (ctx->flags & IORING_SETUP_DEFER_TASKRUN)
2913 				(void)io_run_local_work_locked(ctx, min_complete);
2914 		}
2915 		mutex_unlock(&ctx->uring_lock);
2916 	}
2917 
2918 	if (flags & IORING_ENTER_GETEVENTS) {
2919 		int ret2;
2920 
2921 		if (ctx->int_flags & IO_RING_F_SYSCALL_IOPOLL) {
2922 			/*
2923 			 * We disallow the app entering submit/complete with
2924 			 * polling, but we still need to lock the ring to
2925 			 * prevent racing with polled issue that got punted to
2926 			 * a workqueue.
2927 			 */
2928 			mutex_lock(&ctx->uring_lock);
2929 iopoll_locked:
2930 			ret2 = io_validate_ext_arg(ctx, flags, argp, argsz);
2931 			if (likely(!ret2))
2932 				ret2 = io_iopoll_check(ctx, min_complete);
2933 			mutex_unlock(&ctx->uring_lock);
2934 		} else {
2935 			struct ext_arg ext_arg = { .argsz = argsz };
2936 
2937 			ret2 = io_get_ext_arg(ctx, flags, argp, &ext_arg);
2938 			if (likely(!ret2))
2939 				ret2 = io_cqring_wait(ctx, min_complete, flags,
2940 						      &ext_arg);
2941 		}
2942 
2943 		if (!ret) {
2944 			ret = ret2;
2945 
2946 			/*
2947 			 * EBADR indicates that one or more CQE were dropped.
2948 			 * Once the user has been informed we can clear the bit
2949 			 * as they are obviously ok with those drops.
2950 			 */
2951 			if (unlikely(ret2 == -EBADR))
2952 				clear_bit(IO_CHECK_CQ_DROPPED_BIT,
2953 					  &ctx->check_cq);
2954 		}
2955 	}
2956 out:
2957 	if (!(flags & IORING_ENTER_REGISTERED_RING))
2958 		fput(file);
2959 	return ret;
2960 }
2961 
2962 static const struct file_operations io_uring_fops = {
2963 	.release	= io_uring_release,
2964 	.mmap		= io_uring_mmap,
2965 	.get_unmapped_area = io_uring_get_unmapped_area,
2966 #ifndef CONFIG_MMU
2967 	.mmap_capabilities = io_uring_nommu_mmap_capabilities,
2968 #endif
2969 	.poll		= io_uring_poll,
2970 #ifdef CONFIG_PROC_FS
2971 	.show_fdinfo	= io_uring_show_fdinfo,
2972 #endif
2973 };
2974 
2975 bool io_is_uring_fops(struct file *file)
2976 {
2977 	return file->f_op == &io_uring_fops;
2978 }
2979 
2980 static __cold int io_allocate_scq_urings(struct io_ring_ctx *ctx,
2981 					 struct io_ctx_config *config)
2982 {
2983 	struct io_uring_params *p = &config->p;
2984 	struct io_rings_layout *rl = &config->layout;
2985 	struct io_uring_region_desc rd;
2986 	struct io_rings *rings;
2987 	int ret;
2988 
2989 	/* make sure these are sane, as we already accounted them */
2990 	ctx->sq_entries = p->sq_entries;
2991 	ctx->cq_entries = p->cq_entries;
2992 
2993 	memset(&rd, 0, sizeof(rd));
2994 	rd.size = PAGE_ALIGN(rl->rings_size);
2995 	if (ctx->flags & IORING_SETUP_NO_MMAP) {
2996 		rd.user_addr = p->cq_off.user_addr;
2997 		rd.flags |= IORING_MEM_REGION_TYPE_USER;
2998 	}
2999 	/* ring memory is not charged to RLIMIT_MEMLOCK, hence no user */
3000 	ret = io_create_region(NULL, &ctx->ring_region, &rd, IORING_OFF_CQ_RING);
3001 	if (ret)
3002 		return ret;
3003 	ctx->rings = rings = io_region_get_ptr(&ctx->ring_region);
3004 	rcu_assign_pointer(ctx->rings_rcu, rings);
3005 	if (!(ctx->flags & IORING_SETUP_NO_SQARRAY))
3006 		ctx->sq_array = (u32 *)((char *)rings + rl->sq_array_offset);
3007 
3008 	memset(&rd, 0, sizeof(rd));
3009 	rd.size = PAGE_ALIGN(rl->sq_size);
3010 	if (ctx->flags & IORING_SETUP_NO_MMAP) {
3011 		rd.user_addr = p->sq_off.user_addr;
3012 		rd.flags |= IORING_MEM_REGION_TYPE_USER;
3013 	}
3014 	ret = io_create_region(NULL, &ctx->sq_region, &rd, IORING_OFF_SQES);
3015 	if (ret) {
3016 		io_rings_free(ctx);
3017 		return ret;
3018 	}
3019 	ctx->sq_sqes = io_region_get_ptr(&ctx->sq_region);
3020 
3021 	memset(rings, 0, sizeof(*rings));
3022 	WRITE_ONCE(rings->sq_ring_mask, ctx->sq_entries - 1);
3023 	WRITE_ONCE(rings->cq_ring_mask, ctx->cq_entries - 1);
3024 	WRITE_ONCE(rings->sq_ring_entries, ctx->sq_entries);
3025 	WRITE_ONCE(rings->cq_ring_entries, ctx->cq_entries);
3026 	return 0;
3027 }
3028 
3029 static int io_uring_install_fd(struct file *file)
3030 {
3031 	int fd;
3032 
3033 	fd = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
3034 	if (fd < 0)
3035 		return fd;
3036 	fd_install(fd, file);
3037 	return fd;
3038 }
3039 
3040 /*
3041  * Allocate an anonymous fd, this is what constitutes the application
3042  * visible backing of an io_uring instance. The application mmaps this
3043  * fd to gain access to the SQ/CQ ring details.
3044  */
3045 static struct file *io_uring_get_file(struct io_ring_ctx *ctx)
3046 {
3047 	/* Create a new inode so that the LSM can block the creation.  */
3048 	return anon_inode_create_getfile("[io_uring]", &io_uring_fops, ctx,
3049 					 O_RDWR | O_CLOEXEC, NULL);
3050 }
3051 
3052 static int io_uring_sanitise_params(struct io_uring_params *p)
3053 {
3054 	unsigned flags = p->flags;
3055 
3056 	if (flags & ~IORING_SETUP_FLAGS)
3057 		return -EINVAL;
3058 
3059 	if (flags & IORING_SETUP_SQ_REWIND) {
3060 		if ((flags & IORING_SETUP_SQPOLL) ||
3061 		    !(flags & IORING_SETUP_NO_SQARRAY))
3062 			return -EINVAL;
3063 	}
3064 
3065 	/* There is no way to mmap rings without a real fd */
3066 	if ((flags & IORING_SETUP_REGISTERED_FD_ONLY) &&
3067 	    !(flags & IORING_SETUP_NO_MMAP))
3068 		return -EINVAL;
3069 
3070 	if (flags & IORING_SETUP_SQPOLL) {
3071 		/* IPI related flags don't make sense with SQPOLL */
3072 		if (flags & (IORING_SETUP_COOP_TASKRUN |
3073 			     IORING_SETUP_TASKRUN_FLAG |
3074 			     IORING_SETUP_DEFER_TASKRUN))
3075 			return -EINVAL;
3076 	}
3077 
3078 	if (flags & IORING_SETUP_TASKRUN_FLAG) {
3079 		if (!(flags & (IORING_SETUP_COOP_TASKRUN |
3080 			       IORING_SETUP_DEFER_TASKRUN)))
3081 			return -EINVAL;
3082 	}
3083 
3084 	/* HYBRID_IOPOLL only valid with IOPOLL */
3085 	if ((flags & IORING_SETUP_HYBRID_IOPOLL) && !(flags & IORING_SETUP_IOPOLL))
3086 		return -EINVAL;
3087 
3088 	/*
3089 	 * For DEFER_TASKRUN we require the completion task to be the same as
3090 	 * the submission task. This implies that there is only one submitter.
3091 	 */
3092 	if ((flags & IORING_SETUP_DEFER_TASKRUN) &&
3093 	    !(flags & IORING_SETUP_SINGLE_ISSUER))
3094 		return -EINVAL;
3095 
3096 	/*
3097 	 * Nonsensical to ask for CQE32 and mixed CQE support, it's not
3098 	 * supported to post 16b CQEs on a ring setup with CQE32.
3099 	 */
3100 	if ((flags & (IORING_SETUP_CQE32|IORING_SETUP_CQE_MIXED)) ==
3101 	    (IORING_SETUP_CQE32|IORING_SETUP_CQE_MIXED))
3102 		return -EINVAL;
3103 	/*
3104 	 * Nonsensical to ask for SQE128 and mixed SQE support, it's not
3105 	 * supported to post 64b SQEs on a ring setup with SQE128.
3106 	 */
3107 	if ((flags & (IORING_SETUP_SQE128|IORING_SETUP_SQE_MIXED)) ==
3108 	    (IORING_SETUP_SQE128|IORING_SETUP_SQE_MIXED))
3109 		return -EINVAL;
3110 
3111 	return 0;
3112 }
3113 
3114 static int io_uring_fill_params(struct io_uring_params *p)
3115 {
3116 	unsigned entries = p->sq_entries;
3117 
3118 	if (!entries)
3119 		return -EINVAL;
3120 	if (entries > IORING_MAX_ENTRIES) {
3121 		if (!(p->flags & IORING_SETUP_CLAMP))
3122 			return -EINVAL;
3123 		entries = IORING_MAX_ENTRIES;
3124 	}
3125 
3126 	/*
3127 	 * Use twice as many entries for the CQ ring. It's possible for the
3128 	 * application to drive a higher depth than the size of the SQ ring,
3129 	 * since the sqes are only used at submission time. This allows for
3130 	 * some flexibility in overcommitting a bit. If the application has
3131 	 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
3132 	 * of CQ ring entries manually.
3133 	 */
3134 	p->sq_entries = roundup_pow_of_two(entries);
3135 	if (p->flags & IORING_SETUP_CQSIZE) {
3136 		/*
3137 		 * If IORING_SETUP_CQSIZE is set, we do the same roundup
3138 		 * to a power-of-two, if it isn't already. We do NOT impose
3139 		 * any cq vs sq ring sizing.
3140 		 */
3141 		if (!p->cq_entries)
3142 			return -EINVAL;
3143 		if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
3144 			if (!(p->flags & IORING_SETUP_CLAMP))
3145 				return -EINVAL;
3146 			p->cq_entries = IORING_MAX_CQ_ENTRIES;
3147 		}
3148 		p->cq_entries = roundup_pow_of_two(p->cq_entries);
3149 		if (p->cq_entries < p->sq_entries)
3150 			return -EINVAL;
3151 	} else {
3152 		p->cq_entries = 2 * p->sq_entries;
3153 	}
3154 
3155 	return 0;
3156 }
3157 
3158 int io_prepare_config(struct io_ctx_config *config)
3159 {
3160 	struct io_uring_params *p = &config->p;
3161 	int ret;
3162 
3163 	ret = io_uring_sanitise_params(p);
3164 	if (ret)
3165 		return ret;
3166 
3167 	ret = io_uring_fill_params(p);
3168 	if (ret)
3169 		return ret;
3170 
3171 	ret = rings_size(p->flags, p->sq_entries, p->cq_entries,
3172 			 &config->layout);
3173 	if (ret)
3174 		return ret;
3175 
3176 	p->sq_off.head = offsetof(struct io_rings, sq.head);
3177 	p->sq_off.tail = offsetof(struct io_rings, sq.tail);
3178 	p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
3179 	p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
3180 	p->sq_off.flags = offsetof(struct io_rings, sq_flags);
3181 	p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
3182 	p->sq_off.resv1 = 0;
3183 	if (!(p->flags & IORING_SETUP_NO_MMAP))
3184 		p->sq_off.user_addr = 0;
3185 
3186 	p->cq_off.head = offsetof(struct io_rings, cq.head);
3187 	p->cq_off.tail = offsetof(struct io_rings, cq.tail);
3188 	p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
3189 	p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
3190 	p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
3191 	p->cq_off.cqes = offsetof(struct io_rings, cqes);
3192 	p->cq_off.flags = offsetof(struct io_rings, cq_flags);
3193 	p->cq_off.resv1 = 0;
3194 	if (!(p->flags & IORING_SETUP_NO_MMAP))
3195 		p->cq_off.user_addr = 0;
3196 	if (!(p->flags & IORING_SETUP_NO_SQARRAY))
3197 		p->sq_off.array = config->layout.sq_array_offset;
3198 
3199 	return 0;
3200 }
3201 
3202 void io_restriction_clone(struct io_restriction *dst, struct io_restriction *src)
3203 {
3204 	memcpy(&dst->register_op, &src->register_op, sizeof(dst->register_op));
3205 	memcpy(&dst->sqe_op, &src->sqe_op, sizeof(dst->sqe_op));
3206 	dst->sqe_flags_allowed = src->sqe_flags_allowed;
3207 	dst->sqe_flags_required = src->sqe_flags_required;
3208 	dst->op_registered = src->op_registered;
3209 	dst->reg_registered = src->reg_registered;
3210 
3211 	io_bpf_filter_clone(dst, src);
3212 }
3213 
3214 static void io_ctx_restriction_clone(struct io_ring_ctx *ctx,
3215 				     struct io_restriction *src)
3216 {
3217 	struct io_restriction *dst = &ctx->restrictions;
3218 
3219 	io_restriction_clone(dst, src);
3220 	if (dst->bpf_filters)
3221 		WRITE_ONCE(ctx->bpf_filters, dst->bpf_filters->filters);
3222 	if (dst->op_registered)
3223 		ctx->int_flags |= IO_RING_F_OP_RESTRICTED;
3224 	if (dst->reg_registered)
3225 		ctx->int_flags |= IO_RING_F_REG_RESTRICTED |
3226 				  IO_RING_F_RESTRICT_INHERITED;
3227 }
3228 
3229 static __cold int io_uring_create(struct io_ctx_config *config)
3230 {
3231 	struct io_uring_params *p = &config->p;
3232 	struct io_ring_ctx *ctx;
3233 	struct io_uring_task *tctx;
3234 	struct file *file;
3235 	int ret;
3236 
3237 	ret = io_prepare_config(config);
3238 	if (ret)
3239 		return ret;
3240 
3241 	ctx = io_ring_ctx_alloc(p);
3242 	if (!ctx)
3243 		return -ENOMEM;
3244 
3245 	ctx->clockid = CLOCK_MONOTONIC;
3246 	ctx->clock_offset = 0;
3247 
3248 	if (!(ctx->flags & IORING_SETUP_NO_SQARRAY))
3249 		static_branch_deferred_inc(&io_key_has_sqarray);
3250 
3251 	if ((ctx->flags & IORING_SETUP_DEFER_TASKRUN) &&
3252 	    !(ctx->flags & IORING_SETUP_IOPOLL))
3253 		ctx->int_flags |= IO_RING_F_TASK_COMPLETE;
3254 
3255 	if ((ctx->int_flags & IO_RING_F_TASK_COMPLETE) ||
3256 	    (ctx->flags & IORING_SETUP_IOPOLL))
3257 		ctx->int_flags |= IO_RING_F_LOCKLESS_CQ;
3258 
3259 	/*
3260 	 * lazy poll_wq activation relies on ->task_complete for synchronisation
3261 	 * purposes, see io_activate_pollwq()
3262 	 */
3263 	if (!(ctx->int_flags & IO_RING_F_TASK_COMPLETE))
3264 		ctx->int_flags |= IO_RING_F_POLL_ACTIVATED;
3265 
3266 	/*
3267 	 * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user
3268 	 * space applications don't need to do io completion events
3269 	 * polling again, they can rely on io_sq_thread to do polling
3270 	 * work, which can reduce cpu usage and uring_lock contention.
3271 	 */
3272 	if (ctx->flags & IORING_SETUP_IOPOLL &&
3273 	    !(ctx->flags & IORING_SETUP_SQPOLL))
3274 		ctx->int_flags |= IO_RING_F_SYSCALL_IOPOLL;
3275 
3276 	if (in_compat_syscall())
3277 		ctx->int_flags |= IO_RING_F_COMPAT;
3278 	if (!ns_capable_noaudit(&init_user_ns, CAP_IPC_LOCK))
3279 		ctx->user = get_uid(current_user());
3280 
3281 	/*
3282 	 * For SQPOLL, we just need a wakeup, always. For !SQPOLL, if
3283 	 * COOP_TASKRUN is set, then IPIs are never needed by the app.
3284 	 */
3285 	if (ctx->flags & (IORING_SETUP_SQPOLL|IORING_SETUP_COOP_TASKRUN))
3286 		ctx->notify_method = TWA_SIGNAL_NO_IPI;
3287 	else
3288 		ctx->notify_method = TWA_SIGNAL;
3289 
3290 	/*
3291 	 * If the current task has restrictions enabled, then copy them to
3292 	 * our newly created ring and mark it as registered.
3293 	 */
3294 	if (current->io_uring_restrict)
3295 		io_ctx_restriction_clone(ctx, current->io_uring_restrict);
3296 
3297 	/*
3298 	 * This is just grabbed for accounting purposes. When a process exits,
3299 	 * the mm is exited and dropped before the files, hence we need to hang
3300 	 * on to this mm purely for the purposes of being able to unaccount
3301 	 * memory (locked/pinned vm). It's not used for anything else.
3302 	 */
3303 	mmgrab(current->mm);
3304 	ctx->mm_account = current->mm;
3305 
3306 	ret = io_allocate_scq_urings(ctx, config);
3307 	if (ret)
3308 		goto err;
3309 
3310 	ret = io_sq_offload_create(ctx, p);
3311 	if (ret)
3312 		goto err;
3313 
3314 	p->features = IORING_FEAT_FLAGS;
3315 
3316 	if (copy_to_user(config->uptr, p, sizeof(*p))) {
3317 		ret = -EFAULT;
3318 		goto err;
3319 	}
3320 
3321 	if (ctx->flags & IORING_SETUP_SINGLE_ISSUER
3322 	    && !(ctx->flags & IORING_SETUP_R_DISABLED))
3323 		ctx->submitter_task = get_task_struct(current);
3324 
3325 	file = io_uring_get_file(ctx);
3326 	if (IS_ERR(file)) {
3327 		ret = PTR_ERR(file);
3328 		goto err;
3329 	}
3330 
3331 	ret = __io_uring_add_tctx_node(ctx);
3332 	if (ret)
3333 		goto err_fput;
3334 	tctx = current->io_uring;
3335 
3336 	/*
3337 	 * Install ring fd as the very last thing, so we don't risk someone
3338 	 * having closed it before we finish setup
3339 	 */
3340 	if (p->flags & IORING_SETUP_REGISTERED_FD_ONLY)
3341 		ret = io_ring_add_registered_file(tctx, file, 0, IO_RINGFD_REG_MAX);
3342 	else
3343 		ret = io_uring_install_fd(file);
3344 	if (ret < 0)
3345 		goto err_fput;
3346 
3347 	trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
3348 	return ret;
3349 err:
3350 	io_ring_ctx_wait_and_kill(ctx);
3351 	return ret;
3352 err_fput:
3353 	fput(file);
3354 	return ret;
3355 }
3356 
3357 /*
3358  * Sets up an aio uring context, and returns the fd. Applications asks for a
3359  * ring size, we return the actual sq/cq ring sizes (among other things) in the
3360  * params structure passed in.
3361  */
3362 static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
3363 {
3364 	struct io_ctx_config config;
3365 
3366 	memset(&config, 0, sizeof(config));
3367 
3368 	if (copy_from_user(&config.p, params, sizeof(config.p)))
3369 		return -EFAULT;
3370 
3371 	if (!mem_is_zero(&config.p.resv, sizeof(config.p.resv)))
3372 		return -EINVAL;
3373 
3374 	config.p.sq_entries = entries;
3375 	config.uptr = params;
3376 	return io_uring_create(&config);
3377 }
3378 
3379 static inline int io_uring_allowed(void)
3380 {
3381 	int disabled = READ_ONCE(sysctl_io_uring_disabled);
3382 	kgid_t io_uring_group;
3383 
3384 	if (disabled == 2)
3385 		return -EPERM;
3386 
3387 	if (disabled == 0 || capable(CAP_SYS_ADMIN))
3388 		goto allowed_lsm;
3389 
3390 	io_uring_group = make_kgid(&init_user_ns, sysctl_io_uring_group);
3391 	if (!gid_valid(io_uring_group))
3392 		return -EPERM;
3393 
3394 	if (!in_group_p(io_uring_group))
3395 		return -EPERM;
3396 
3397 allowed_lsm:
3398 	return security_uring_allowed();
3399 }
3400 
3401 SYSCALL_DEFINE2(io_uring_setup, u32, entries,
3402 		struct io_uring_params __user *, params)
3403 {
3404 	int ret;
3405 
3406 	ret = io_uring_allowed();
3407 	if (ret)
3408 		return ret;
3409 
3410 	return io_uring_setup(entries, params);
3411 }
3412 
3413 static int __init io_uring_init(void)
3414 {
3415 	struct kmem_cache_args kmem_args = {
3416 		.useroffset = offsetof(struct io_kiocb, cmd.data),
3417 		.usersize = sizeof_field(struct io_kiocb, cmd.data),
3418 		.freeptr_offset = offsetof(struct io_kiocb, work),
3419 		.use_freeptr_offset = true,
3420 	};
3421 
3422 #define __BUILD_BUG_VERIFY_OFFSET_SIZE(stype, eoffset, esize, ename) do { \
3423 	BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
3424 	BUILD_BUG_ON(sizeof_field(stype, ename) != esize); \
3425 } while (0)
3426 
3427 #define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
3428 	__BUILD_BUG_VERIFY_OFFSET_SIZE(struct io_uring_sqe, eoffset, sizeof(etype), ename)
3429 #define BUILD_BUG_SQE_ELEM_SIZE(eoffset, esize, ename) \
3430 	__BUILD_BUG_VERIFY_OFFSET_SIZE(struct io_uring_sqe, eoffset, esize, ename)
3431 	BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
3432 	BUILD_BUG_SQE_ELEM(0,  __u8,   opcode);
3433 	BUILD_BUG_SQE_ELEM(1,  __u8,   flags);
3434 	BUILD_BUG_SQE_ELEM(2,  __u16,  ioprio);
3435 	BUILD_BUG_SQE_ELEM(4,  __s32,  fd);
3436 	BUILD_BUG_SQE_ELEM(8,  __u64,  off);
3437 	BUILD_BUG_SQE_ELEM(8,  __u64,  addr2);
3438 	BUILD_BUG_SQE_ELEM(8,  __u32,  cmd_op);
3439 	BUILD_BUG_SQE_ELEM(12, __u32, __pad1);
3440 	BUILD_BUG_SQE_ELEM(16, __u64,  addr);
3441 	BUILD_BUG_SQE_ELEM(16, __u64,  splice_off_in);
3442 	BUILD_BUG_SQE_ELEM(24, __u32,  len);
3443 	BUILD_BUG_SQE_ELEM(28,     __kernel_rwf_t, rw_flags);
3444 	BUILD_BUG_SQE_ELEM(28, /* compat */   int, rw_flags);
3445 	BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
3446 	BUILD_BUG_SQE_ELEM(28, __u32,  fsync_flags);
3447 	BUILD_BUG_SQE_ELEM(28, /* compat */ __u16,  poll_events);
3448 	BUILD_BUG_SQE_ELEM(28, __u32,  poll32_events);
3449 	BUILD_BUG_SQE_ELEM(28, __u32,  sync_range_flags);
3450 	BUILD_BUG_SQE_ELEM(28, __u32,  msg_flags);
3451 	BUILD_BUG_SQE_ELEM(28, __u32,  timeout_flags);
3452 	BUILD_BUG_SQE_ELEM(28, __u32,  accept_flags);
3453 	BUILD_BUG_SQE_ELEM(28, __u32,  cancel_flags);
3454 	BUILD_BUG_SQE_ELEM(28, __u32,  open_flags);
3455 	BUILD_BUG_SQE_ELEM(28, __u32,  statx_flags);
3456 	BUILD_BUG_SQE_ELEM(28, __u32,  fadvise_advice);
3457 	BUILD_BUG_SQE_ELEM(28, __u32,  splice_flags);
3458 	BUILD_BUG_SQE_ELEM(28, __u32,  rename_flags);
3459 	BUILD_BUG_SQE_ELEM(28, __u32,  unlink_flags);
3460 	BUILD_BUG_SQE_ELEM(28, __u32,  hardlink_flags);
3461 	BUILD_BUG_SQE_ELEM(28, __u32,  xattr_flags);
3462 	BUILD_BUG_SQE_ELEM(28, __u32,  msg_ring_flags);
3463 	BUILD_BUG_SQE_ELEM(32, __u64,  user_data);
3464 	BUILD_BUG_SQE_ELEM(40, __u16,  buf_index);
3465 	BUILD_BUG_SQE_ELEM(40, __u16,  buf_group);
3466 	BUILD_BUG_SQE_ELEM(42, __u16,  personality);
3467 	BUILD_BUG_SQE_ELEM(44, __s32,  splice_fd_in);
3468 	BUILD_BUG_SQE_ELEM(44, __u32,  file_index);
3469 	BUILD_BUG_SQE_ELEM(44, __u16,  addr_len);
3470 	BUILD_BUG_SQE_ELEM(44, __u8,   write_stream);
3471 	BUILD_BUG_SQE_ELEM(45, __u8,   __pad4[0]);
3472 	BUILD_BUG_SQE_ELEM(46, __u16,  __pad3[0]);
3473 	BUILD_BUG_SQE_ELEM(48, __u64,  addr3);
3474 	BUILD_BUG_SQE_ELEM_SIZE(48, 0, cmd);
3475 	BUILD_BUG_SQE_ELEM(48, __u64, attr_ptr);
3476 	BUILD_BUG_SQE_ELEM(56, __u64, attr_type_mask);
3477 	BUILD_BUG_SQE_ELEM(56, __u64,  __pad2);
3478 
3479 	BUILD_BUG_ON(sizeof(struct io_uring_files_update) !=
3480 		     sizeof(struct io_uring_rsrc_update));
3481 	BUILD_BUG_ON(sizeof(struct io_uring_rsrc_update) >
3482 		     sizeof(struct io_uring_rsrc_update2));
3483 
3484 	/* ->buf_index is u16 */
3485 	BUILD_BUG_ON(offsetof(struct io_uring_buf_ring, bufs) != 0);
3486 	BUILD_BUG_ON(offsetof(struct io_uring_buf, resv) !=
3487 		     offsetof(struct io_uring_buf_ring, tail));
3488 
3489 	/* should fit into one byte */
3490 	BUILD_BUG_ON(SQE_VALID_FLAGS >= (1 << 8));
3491 	BUILD_BUG_ON(SQE_COMMON_FLAGS >= (1 << 8));
3492 	BUILD_BUG_ON((SQE_VALID_FLAGS | SQE_COMMON_FLAGS) != SQE_VALID_FLAGS);
3493 
3494 	BUILD_BUG_ON(__REQ_F_LAST_BIT > 8 * sizeof_field(struct io_kiocb, flags));
3495 
3496 	BUILD_BUG_ON(sizeof(atomic_t) != sizeof(u32));
3497 
3498 	/* top 8bits are for internal use */
3499 	BUILD_BUG_ON((IORING_URING_CMD_MASK & 0xff000000) != 0);
3500 
3501 	io_uring_optable_init();
3502 
3503 	/* imu->dir is u8 */
3504 	BUILD_BUG_ON((IO_BUF_DEST | IO_BUF_SOURCE) > U8_MAX);
3505 
3506 	/*
3507 	 * Allow user copy in the per-command field, which starts after the
3508 	 * file in io_kiocb and until the opcode field. The openat2 handling
3509 	 * requires copying in user memory into the io_kiocb object in that
3510 	 * range, and HARDENED_USERCOPY will complain if we haven't
3511 	 * correctly annotated this range.
3512 	 */
3513 	req_cachep = kmem_cache_create("io_kiocb", sizeof(struct io_kiocb), &kmem_args,
3514 				SLAB_HWCACHE_ALIGN | SLAB_PANIC | SLAB_ACCOUNT |
3515 				SLAB_TYPESAFE_BY_RCU);
3516 
3517 	iou_wq = alloc_workqueue("iou_exit", WQ_UNBOUND, 64);
3518 	BUG_ON(!iou_wq);
3519 
3520 #ifdef CONFIG_SYSCTL
3521 	register_sysctl_init("kernel", kernel_io_uring_disabled_table);
3522 #endif
3523 
3524 	return 0;
3525 };
3526 __initcall(io_uring_init);
3527