xref: /openbmc/linux/io_uring/io_uring.c (revision 8501fe70)
1ed29b0b4SJens Axboe // SPDX-License-Identifier: GPL-2.0
2ed29b0b4SJens Axboe /*
3ed29b0b4SJens Axboe  * Shared application/kernel submission and completion ring pairs, for
4ed29b0b4SJens Axboe  * supporting fast/efficient IO.
5ed29b0b4SJens Axboe  *
6ed29b0b4SJens Axboe  * A note on the read/write ordering memory barriers that are matched between
7ed29b0b4SJens Axboe  * the application and kernel side.
8ed29b0b4SJens Axboe  *
9ed29b0b4SJens Axboe  * After the application reads the CQ ring tail, it must use an
10ed29b0b4SJens Axboe  * appropriate smp_rmb() to pair with the smp_wmb() the kernel uses
11ed29b0b4SJens Axboe  * before writing the tail (using smp_load_acquire to read the tail will
12ed29b0b4SJens Axboe  * do). It also needs a smp_mb() before updating CQ head (ordering the
13ed29b0b4SJens Axboe  * entry load(s) with the head store), pairing with an implicit barrier
14ed29b0b4SJens Axboe  * through a control-dependency in io_get_cqe (smp_store_release to
15ed29b0b4SJens Axboe  * store head will do). Failure to do so could lead to reading invalid
16ed29b0b4SJens Axboe  * CQ entries.
17ed29b0b4SJens Axboe  *
18ed29b0b4SJens Axboe  * Likewise, the application must use an appropriate smp_wmb() before
19ed29b0b4SJens Axboe  * writing the SQ tail (ordering SQ entry stores with the tail store),
20ed29b0b4SJens Axboe  * which pairs with smp_load_acquire in io_get_sqring (smp_store_release
21ed29b0b4SJens Axboe  * to store the tail will do). And it needs a barrier ordering the SQ
22ed29b0b4SJens Axboe  * head load before writing new SQ entries (smp_load_acquire to read
23ed29b0b4SJens Axboe  * head will do).
24ed29b0b4SJens Axboe  *
25ed29b0b4SJens Axboe  * When using the SQ poll thread (IORING_SETUP_SQPOLL), the application
26ed29b0b4SJens Axboe  * needs to check the SQ flags for IORING_SQ_NEED_WAKEUP *after*
27ed29b0b4SJens Axboe  * updating the SQ tail; a full memory barrier smp_mb() is needed
28ed29b0b4SJens Axboe  * between.
29ed29b0b4SJens Axboe  *
30ed29b0b4SJens Axboe  * Also see the examples in the liburing library:
31ed29b0b4SJens Axboe  *
32ed29b0b4SJens Axboe  *	git://git.kernel.dk/liburing
33ed29b0b4SJens Axboe  *
34ed29b0b4SJens Axboe  * io_uring also uses READ/WRITE_ONCE() for _any_ store or load that happens
35ed29b0b4SJens Axboe  * from data shared between the kernel and application. This is done both
36ed29b0b4SJens Axboe  * for ordering purposes, but also to ensure that once a value is loaded from
37ed29b0b4SJens Axboe  * data that the application could potentially modify, it remains stable.
38ed29b0b4SJens Axboe  *
39ed29b0b4SJens Axboe  * Copyright (C) 2018-2019 Jens Axboe
40ed29b0b4SJens Axboe  * Copyright (c) 2018-2019 Christoph Hellwig
41ed29b0b4SJens Axboe  */
42ed29b0b4SJens Axboe #include <linux/kernel.h>
43ed29b0b4SJens Axboe #include <linux/init.h>
44ed29b0b4SJens Axboe #include <linux/errno.h>
45ed29b0b4SJens Axboe #include <linux/syscalls.h>
46ed29b0b4SJens Axboe #include <net/compat.h>
47ed29b0b4SJens Axboe #include <linux/refcount.h>
48ed29b0b4SJens Axboe #include <linux/uio.h>
49ed29b0b4SJens Axboe #include <linux/bits.h>
50ed29b0b4SJens Axboe 
51ed29b0b4SJens Axboe #include <linux/sched/signal.h>
52ed29b0b4SJens Axboe #include <linux/fs.h>
53ed29b0b4SJens Axboe #include <linux/file.h>
54ed29b0b4SJens Axboe #include <linux/fdtable.h>
55ed29b0b4SJens Axboe #include <linux/mm.h>
56ed29b0b4SJens Axboe #include <linux/mman.h>
57ed29b0b4SJens Axboe #include <linux/percpu.h>
58ed29b0b4SJens Axboe #include <linux/slab.h>
59ed29b0b4SJens Axboe #include <linux/bvec.h>
60ed29b0b4SJens Axboe #include <linux/net.h>
61ed29b0b4SJens Axboe #include <net/sock.h>
62ed29b0b4SJens Axboe #include <net/af_unix.h>
63ed29b0b4SJens Axboe #include <net/scm.h>
64ed29b0b4SJens Axboe #include <linux/anon_inodes.h>
65ed29b0b4SJens Axboe #include <linux/sched/mm.h>
66ed29b0b4SJens Axboe #include <linux/uaccess.h>
67ed29b0b4SJens Axboe #include <linux/nospec.h>
68ed29b0b4SJens Axboe #include <linux/highmem.h>
69ed29b0b4SJens Axboe #include <linux/fsnotify.h>
70ed29b0b4SJens Axboe #include <linux/fadvise.h>
71ed29b0b4SJens Axboe #include <linux/task_work.h>
72ed29b0b4SJens Axboe #include <linux/io_uring.h>
73ed29b0b4SJens Axboe #include <linux/audit.h>
74ed29b0b4SJens Axboe #include <linux/security.h>
75d808459bSHelge Deller #include <asm/shmparam.h>
76ed29b0b4SJens Axboe 
77ed29b0b4SJens Axboe #define CREATE_TRACE_POINTS
78ed29b0b4SJens Axboe #include <trace/events/io_uring.h>
79ed29b0b4SJens Axboe 
80ed29b0b4SJens Axboe #include <uapi/linux/io_uring.h>
81ed29b0b4SJens Axboe 
82ed29b0b4SJens Axboe #include "io-wq.h"
83ed29b0b4SJens Axboe 
84de23077eSJens Axboe #include "io_uring.h"
85329061d3SJens Axboe #include "opdef.h"
86e418bbc9SJens Axboe #include "refs.h"
87c9f06aa7SJens Axboe #include "tctx.h"
8817437f31SJens Axboe #include "sqpoll.h"
89a4ad4f74SJens Axboe #include "fdinfo.h"
903b77495aSJens Axboe #include "kbuf.h"
9173572984SJens Axboe #include "rsrc.h"
9238513c46SHao Xu #include "cancel.h"
9343e0bbbdSJens Axboe #include "net.h"
94eb42cebbSPavel Begunkov #include "notif.h"
95e27f928eSJens Axboe 
9659915143SJens Axboe #include "timeout.h"
97329061d3SJens Axboe #include "poll.h"
989b797a37SJens Axboe #include "alloc_cache.h"
995e2a18d9SJens Axboe 
100ed29b0b4SJens Axboe #define IORING_MAX_ENTRIES	32768
101ed29b0b4SJens Axboe #define IORING_MAX_CQ_ENTRIES	(2 * IORING_MAX_ENTRIES)
102ed29b0b4SJens Axboe 
103ed29b0b4SJens Axboe #define IORING_MAX_RESTRICTIONS	(IORING_RESTRICTION_LAST + \
104ed29b0b4SJens Axboe 				 IORING_REGISTER_LAST + IORING_OP_LAST)
105ed29b0b4SJens Axboe 
106ed29b0b4SJens Axboe #define SQE_COMMON_FLAGS (IOSQE_FIXED_FILE | IOSQE_IO_LINK | \
107ed29b0b4SJens Axboe 			  IOSQE_IO_HARDLINK | IOSQE_ASYNC)
108ed29b0b4SJens Axboe 
109ed29b0b4SJens Axboe #define SQE_VALID_FLAGS	(SQE_COMMON_FLAGS | IOSQE_BUFFER_SELECT | \
110ed29b0b4SJens Axboe 			IOSQE_IO_DRAIN | IOSQE_CQE_SKIP_SUCCESS)
111ed29b0b4SJens Axboe 
112ed29b0b4SJens Axboe #define IO_REQ_CLEAN_FLAGS (REQ_F_BUFFER_SELECTED | REQ_F_NEED_CLEANUP | \
113ed29b0b4SJens Axboe 				REQ_F_POLLED | REQ_F_INFLIGHT | REQ_F_CREDS | \
114ed29b0b4SJens Axboe 				REQ_F_ASYNC_DATA)
115ed29b0b4SJens Axboe 
116ed29b0b4SJens Axboe #define IO_REQ_CLEAN_SLOW_FLAGS (REQ_F_REFCOUNT | REQ_F_LINK | REQ_F_HARDLINK |\
117ed29b0b4SJens Axboe 				 IO_REQ_CLEAN_FLAGS)
118ed29b0b4SJens Axboe 
119ed29b0b4SJens Axboe #define IO_TCTX_REFS_CACHE_NR	(1U << 10)
120ed29b0b4SJens Axboe 
121ed29b0b4SJens Axboe #define IO_COMPL_BATCH			32
122ed29b0b4SJens Axboe #define IO_REQ_ALLOC_BATCH		8
123ed29b0b4SJens Axboe 
124ed29b0b4SJens Axboe enum {
125ed29b0b4SJens Axboe 	IO_CHECK_CQ_OVERFLOW_BIT,
126ed29b0b4SJens Axboe 	IO_CHECK_CQ_DROPPED_BIT,
127ed29b0b4SJens Axboe };
128ed29b0b4SJens Axboe 
12921a091b9SDylan Yudaken enum {
13021a091b9SDylan Yudaken 	IO_EVENTFD_OP_SIGNAL_BIT,
13121a091b9SDylan Yudaken 	IO_EVENTFD_OP_FREE_BIT,
13221a091b9SDylan Yudaken };
13321a091b9SDylan Yudaken 
134ed29b0b4SJens Axboe struct io_defer_entry {
135ed29b0b4SJens Axboe 	struct list_head	list;
136ed29b0b4SJens Axboe 	struct io_kiocb		*req;
137ed29b0b4SJens Axboe 	u32			seq;
138ed29b0b4SJens Axboe };
139ed29b0b4SJens Axboe 
140ed29b0b4SJens Axboe /* requests with any of those set should undergo io_disarm_next() */
141ed29b0b4SJens Axboe #define IO_DISARM_MASK (REQ_F_ARM_LTIMEOUT | REQ_F_LINK_TIMEOUT | REQ_F_FAIL)
142ed29b0b4SJens Axboe #define IO_REQ_LINK_FLAGS (REQ_F_LINK | REQ_F_HARDLINK)
143ed29b0b4SJens Axboe 
144affa87dbSPavel Begunkov static bool io_uring_try_cancel_requests(struct io_ring_ctx *ctx,
145ed29b0b4SJens Axboe 					 struct task_struct *task,
146ed29b0b4SJens Axboe 					 bool cancel_all);
147ed29b0b4SJens Axboe 
148ed29b0b4SJens Axboe static void io_dismantle_req(struct io_kiocb *req);
149ed29b0b4SJens Axboe static void io_clean_op(struct io_kiocb *req);
150ed29b0b4SJens Axboe static void io_queue_sqe(struct io_kiocb *req);
151c0e0d6baSDylan Yudaken static void io_move_task_work_from_local(struct io_ring_ctx *ctx);
152ed29b0b4SJens Axboe static void __io_submit_flush_completions(struct io_ring_ctx *ctx);
15377e443abSPavel Begunkov static __cold void io_fallback_tw(struct io_uring_task *tctx);
154ed29b0b4SJens Axboe 
155c1755c25SBreno Leitao struct kmem_cache *req_cachep;
156ed29b0b4SJens Axboe 
157ed29b0b4SJens Axboe struct sock *io_uring_get_socket(struct file *file)
158ed29b0b4SJens Axboe {
159ed29b0b4SJens Axboe #if defined(CONFIG_UNIX)
160cd40cae2SJens Axboe 	if (io_is_uring_fops(file)) {
161ed29b0b4SJens Axboe 		struct io_ring_ctx *ctx = file->private_data;
162ed29b0b4SJens Axboe 
163ed29b0b4SJens Axboe 		return ctx->ring_sock->sk;
164ed29b0b4SJens Axboe 	}
165ed29b0b4SJens Axboe #endif
166ed29b0b4SJens Axboe 	return NULL;
167ed29b0b4SJens Axboe }
168ed29b0b4SJens Axboe EXPORT_SYMBOL(io_uring_get_socket);
169ed29b0b4SJens Axboe 
170ed29b0b4SJens Axboe static inline void io_submit_flush_completions(struct io_ring_ctx *ctx)
171ed29b0b4SJens Axboe {
172931147ddSDylan Yudaken 	if (!wq_list_empty(&ctx->submit_state.compl_reqs) ||
173931147ddSDylan Yudaken 	    ctx->submit_state.cqes_count)
174ed29b0b4SJens Axboe 		__io_submit_flush_completions(ctx);
175ed29b0b4SJens Axboe }
176ed29b0b4SJens Axboe 
177faf88ddeSPavel Begunkov static inline unsigned int __io_cqring_events(struct io_ring_ctx *ctx)
178faf88ddeSPavel Begunkov {
179faf88ddeSPavel Begunkov 	return ctx->cached_cq_tail - READ_ONCE(ctx->rings->cq.head);
180faf88ddeSPavel Begunkov }
181faf88ddeSPavel Begunkov 
1820fc8c2acSDylan Yudaken static inline unsigned int __io_cqring_events_user(struct io_ring_ctx *ctx)
1830fc8c2acSDylan Yudaken {
1840fc8c2acSDylan Yudaken 	return READ_ONCE(ctx->rings->cq.tail) - READ_ONCE(ctx->rings->cq.head);
1850fc8c2acSDylan Yudaken }
1860fc8c2acSDylan Yudaken 
187ed29b0b4SJens Axboe static bool io_match_linked(struct io_kiocb *head)
188ed29b0b4SJens Axboe {
189ed29b0b4SJens Axboe 	struct io_kiocb *req;
190ed29b0b4SJens Axboe 
191ed29b0b4SJens Axboe 	io_for_each_link(req, head) {
192ed29b0b4SJens Axboe 		if (req->flags & REQ_F_INFLIGHT)
193ed29b0b4SJens Axboe 			return true;
194ed29b0b4SJens Axboe 	}
195ed29b0b4SJens Axboe 	return false;
196ed29b0b4SJens Axboe }
197ed29b0b4SJens Axboe 
198ed29b0b4SJens Axboe /*
199ed29b0b4SJens Axboe  * As io_match_task() but protected against racing with linked timeouts.
200ed29b0b4SJens Axboe  * User must not hold timeout_lock.
201ed29b0b4SJens Axboe  */
202329061d3SJens Axboe bool io_match_task_safe(struct io_kiocb *head, struct task_struct *task,
203ed29b0b4SJens Axboe 			bool cancel_all)
204ed29b0b4SJens Axboe {
205ed29b0b4SJens Axboe 	bool matched;
206ed29b0b4SJens Axboe 
207ed29b0b4SJens Axboe 	if (task && head->task != task)
208ed29b0b4SJens Axboe 		return false;
209ed29b0b4SJens Axboe 	if (cancel_all)
210ed29b0b4SJens Axboe 		return true;
211ed29b0b4SJens Axboe 
212ed29b0b4SJens Axboe 	if (head->flags & REQ_F_LINK_TIMEOUT) {
213ed29b0b4SJens Axboe 		struct io_ring_ctx *ctx = head->ctx;
214ed29b0b4SJens Axboe 
215ed29b0b4SJens Axboe 		/* protect against races with linked timeouts */
216ed29b0b4SJens Axboe 		spin_lock_irq(&ctx->timeout_lock);
217ed29b0b4SJens Axboe 		matched = io_match_linked(head);
218ed29b0b4SJens Axboe 		spin_unlock_irq(&ctx->timeout_lock);
219ed29b0b4SJens Axboe 	} else {
220ed29b0b4SJens Axboe 		matched = io_match_linked(head);
221ed29b0b4SJens Axboe 	}
222ed29b0b4SJens Axboe 	return matched;
223ed29b0b4SJens Axboe }
224ed29b0b4SJens Axboe 
225ed29b0b4SJens Axboe static inline void req_fail_link_node(struct io_kiocb *req, int res)
226ed29b0b4SJens Axboe {
227ed29b0b4SJens Axboe 	req_set_fail(req);
22897b388d7SJens Axboe 	io_req_set_res(req, res, 0);
229ed29b0b4SJens Axboe }
230ed29b0b4SJens Axboe 
231ed29b0b4SJens Axboe static inline void io_req_add_to_cache(struct io_kiocb *req, struct io_ring_ctx *ctx)
232ed29b0b4SJens Axboe {
233ed29b0b4SJens Axboe 	wq_stack_add_head(&req->comp_list, &ctx->submit_state.free_list);
234c1755c25SBreno Leitao 	kasan_poison_object_data(req_cachep, req);
235ed29b0b4SJens Axboe }
236ed29b0b4SJens Axboe 
237ed29b0b4SJens Axboe static __cold void io_ring_ctx_ref_free(struct percpu_ref *ref)
238ed29b0b4SJens Axboe {
239ed29b0b4SJens Axboe 	struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
240ed29b0b4SJens Axboe 
241ed29b0b4SJens Axboe 	complete(&ctx->ref_comp);
242ed29b0b4SJens Axboe }
243ed29b0b4SJens Axboe 
244ed29b0b4SJens Axboe static __cold void io_fallback_req_func(struct work_struct *work)
245ed29b0b4SJens Axboe {
246ed29b0b4SJens Axboe 	struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx,
247ed29b0b4SJens Axboe 						fallback_work.work);
248ed29b0b4SJens Axboe 	struct llist_node *node = llist_del_all(&ctx->fallback_llist);
249ed29b0b4SJens Axboe 	struct io_kiocb *req, *tmp;
250a282967cSPavel Begunkov 	struct io_tw_state ts = { .locked = true, };
251ed29b0b4SJens Axboe 
25231f084b7SPavel Begunkov 	mutex_lock(&ctx->uring_lock);
2533218e5d3SPavel Begunkov 	llist_for_each_entry_safe(req, tmp, node, io_task_work.node)
254a282967cSPavel Begunkov 		req->io_task_work.func(req, &ts);
255a282967cSPavel Begunkov 	if (WARN_ON_ONCE(!ts.locked))
25631f084b7SPavel Begunkov 		return;
257ed29b0b4SJens Axboe 	io_submit_flush_completions(ctx);
258ed29b0b4SJens Axboe 	mutex_unlock(&ctx->uring_lock);
259ed29b0b4SJens Axboe }
260ed29b0b4SJens Axboe 
261e6f89be6SPavel Begunkov static int io_alloc_hash_table(struct io_hash_table *table, unsigned bits)
262e6f89be6SPavel Begunkov {
263e6f89be6SPavel Begunkov 	unsigned hash_buckets = 1U << bits;
264e6f89be6SPavel Begunkov 	size_t hash_size = hash_buckets * sizeof(table->hbs[0]);
265e6f89be6SPavel Begunkov 
266e6f89be6SPavel Begunkov 	table->hbs = kmalloc(hash_size, GFP_KERNEL);
267e6f89be6SPavel Begunkov 	if (!table->hbs)
268e6f89be6SPavel Begunkov 		return -ENOMEM;
269e6f89be6SPavel Begunkov 
270e6f89be6SPavel Begunkov 	table->hash_bits = bits;
271e6f89be6SPavel Begunkov 	init_hash_table(table, hash_buckets);
272e6f89be6SPavel Begunkov 	return 0;
273e6f89be6SPavel Begunkov }
274e6f89be6SPavel Begunkov 
275ed29b0b4SJens Axboe static __cold struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
276ed29b0b4SJens Axboe {
277ed29b0b4SJens Axboe 	struct io_ring_ctx *ctx;
278ed29b0b4SJens Axboe 	int hash_bits;
279ed29b0b4SJens Axboe 
280ed29b0b4SJens Axboe 	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
281ed29b0b4SJens Axboe 	if (!ctx)
282ed29b0b4SJens Axboe 		return NULL;
283ed29b0b4SJens Axboe 
284ed29b0b4SJens Axboe 	xa_init(&ctx->io_bl_xa);
285ed29b0b4SJens Axboe 
286ed29b0b4SJens Axboe 	/*
287ed29b0b4SJens Axboe 	 * Use 5 bits less than the max cq entries, that should give us around
2884a07723fSPavel Begunkov 	 * 32 entries per hash list if totally full and uniformly spread, but
2894a07723fSPavel Begunkov 	 * don't keep too many buckets to not overconsume memory.
290ed29b0b4SJens Axboe 	 */
2914a07723fSPavel Begunkov 	hash_bits = ilog2(p->cq_entries) - 5;
2924a07723fSPavel Begunkov 	hash_bits = clamp(hash_bits, 1, 8);
293e6f89be6SPavel Begunkov 	if (io_alloc_hash_table(&ctx->cancel_table, hash_bits))
294ed29b0b4SJens Axboe 		goto err;
2959ca9fb24SPavel Begunkov 	if (io_alloc_hash_table(&ctx->cancel_table_locked, hash_bits))
2969ca9fb24SPavel Begunkov 		goto err;
29738513c46SHao Xu 
298ed29b0b4SJens Axboe 	ctx->dummy_ubuf = kzalloc(sizeof(*ctx->dummy_ubuf), GFP_KERNEL);
299ed29b0b4SJens Axboe 	if (!ctx->dummy_ubuf)
300ed29b0b4SJens Axboe 		goto err;
301ed29b0b4SJens Axboe 	/* set invalid range, so io_import_fixed() fails meeting it */
302ed29b0b4SJens Axboe 	ctx->dummy_ubuf->ubuf = -1UL;
303ed29b0b4SJens Axboe 
304ed29b0b4SJens Axboe 	if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
30548904229SMichal Koutný 			    0, GFP_KERNEL))
306ed29b0b4SJens Axboe 		goto err;
307ed29b0b4SJens Axboe 
308ed29b0b4SJens Axboe 	ctx->flags = p->flags;
309ed29b0b4SJens Axboe 	init_waitqueue_head(&ctx->sqo_sq_wait);
310ed29b0b4SJens Axboe 	INIT_LIST_HEAD(&ctx->sqd_list);
311ed29b0b4SJens Axboe 	INIT_LIST_HEAD(&ctx->cq_overflow_list);
312ed29b0b4SJens Axboe 	INIT_LIST_HEAD(&ctx->io_buffers_cache);
31369bbc6adSPavel Begunkov 	io_alloc_cache_init(&ctx->rsrc_node_cache, IO_NODE_ALLOC_CACHE_MAX,
31469bbc6adSPavel Begunkov 			    sizeof(struct io_rsrc_node));
31569bbc6adSPavel Begunkov 	io_alloc_cache_init(&ctx->apoll_cache, IO_ALLOC_CACHE_MAX,
31669bbc6adSPavel Begunkov 			    sizeof(struct async_poll));
31769bbc6adSPavel Begunkov 	io_alloc_cache_init(&ctx->netmsg_cache, IO_ALLOC_CACHE_MAX,
31869bbc6adSPavel Begunkov 			    sizeof(struct io_async_msghdr));
319ed29b0b4SJens Axboe 	init_completion(&ctx->ref_comp);
320ed29b0b4SJens Axboe 	xa_init_flags(&ctx->personalities, XA_FLAGS_ALLOC1);
321ed29b0b4SJens Axboe 	mutex_init(&ctx->uring_lock);
322ed29b0b4SJens Axboe 	init_waitqueue_head(&ctx->cq_wait);
3237b235dd8SPavel Begunkov 	init_waitqueue_head(&ctx->poll_wq);
324ed29b0b4SJens Axboe 	spin_lock_init(&ctx->completion_lock);
325ed29b0b4SJens Axboe 	spin_lock_init(&ctx->timeout_lock);
326ed29b0b4SJens Axboe 	INIT_WQ_LIST(&ctx->iopoll_list);
327ed29b0b4SJens Axboe 	INIT_LIST_HEAD(&ctx->io_buffers_pages);
328ed29b0b4SJens Axboe 	INIT_LIST_HEAD(&ctx->io_buffers_comp);
329ed29b0b4SJens Axboe 	INIT_LIST_HEAD(&ctx->defer_list);
330ed29b0b4SJens Axboe 	INIT_LIST_HEAD(&ctx->timeout_list);
331ed29b0b4SJens Axboe 	INIT_LIST_HEAD(&ctx->ltimeout_list);
332ed29b0b4SJens Axboe 	INIT_LIST_HEAD(&ctx->rsrc_ref_list);
333c0e0d6baSDylan Yudaken 	init_llist_head(&ctx->work_llist);
334ed29b0b4SJens Axboe 	INIT_LIST_HEAD(&ctx->tctx_list);
335ed29b0b4SJens Axboe 	ctx->submit_state.free_list.next = NULL;
336ed29b0b4SJens Axboe 	INIT_WQ_LIST(&ctx->locked_free_list);
337ed29b0b4SJens Axboe 	INIT_DELAYED_WORK(&ctx->fallback_work, io_fallback_req_func);
338ed29b0b4SJens Axboe 	INIT_WQ_LIST(&ctx->submit_state.compl_reqs);
339ed29b0b4SJens Axboe 	return ctx;
340ed29b0b4SJens Axboe err:
341ed29b0b4SJens Axboe 	kfree(ctx->dummy_ubuf);
342e6f89be6SPavel Begunkov 	kfree(ctx->cancel_table.hbs);
3439ca9fb24SPavel Begunkov 	kfree(ctx->cancel_table_locked.hbs);
344ed29b0b4SJens Axboe 	kfree(ctx->io_bl);
345ed29b0b4SJens Axboe 	xa_destroy(&ctx->io_bl_xa);
346ed29b0b4SJens Axboe 	kfree(ctx);
347ed29b0b4SJens Axboe 	return NULL;
348ed29b0b4SJens Axboe }
349ed29b0b4SJens Axboe 
350ed29b0b4SJens Axboe static void io_account_cq_overflow(struct io_ring_ctx *ctx)
351ed29b0b4SJens Axboe {
352ed29b0b4SJens Axboe 	struct io_rings *r = ctx->rings;
353ed29b0b4SJens Axboe 
354ed29b0b4SJens Axboe 	WRITE_ONCE(r->cq_overflow, READ_ONCE(r->cq_overflow) + 1);
355ed29b0b4SJens Axboe 	ctx->cq_extra--;
356ed29b0b4SJens Axboe }
357ed29b0b4SJens Axboe 
358ed29b0b4SJens Axboe static bool req_need_defer(struct io_kiocb *req, u32 seq)
359ed29b0b4SJens Axboe {
360ed29b0b4SJens Axboe 	if (unlikely(req->flags & REQ_F_IO_DRAIN)) {
361ed29b0b4SJens Axboe 		struct io_ring_ctx *ctx = req->ctx;
362ed29b0b4SJens Axboe 
363ed29b0b4SJens Axboe 		return seq + READ_ONCE(ctx->cq_extra) != ctx->cached_cq_tail;
364ed29b0b4SJens Axboe 	}
365ed29b0b4SJens Axboe 
366ed29b0b4SJens Axboe 	return false;
367ed29b0b4SJens Axboe }
368ed29b0b4SJens Axboe 
369ed29b0b4SJens Axboe static inline void io_req_track_inflight(struct io_kiocb *req)
370ed29b0b4SJens Axboe {
371ed29b0b4SJens Axboe 	if (!(req->flags & REQ_F_INFLIGHT)) {
372ed29b0b4SJens Axboe 		req->flags |= REQ_F_INFLIGHT;
373ed29b0b4SJens Axboe 		atomic_inc(&req->task->io_uring->inflight_tracked);
374ed29b0b4SJens Axboe 	}
375ed29b0b4SJens Axboe }
376ed29b0b4SJens Axboe 
377ed29b0b4SJens Axboe static struct io_kiocb *__io_prep_linked_timeout(struct io_kiocb *req)
378ed29b0b4SJens Axboe {
379ed29b0b4SJens Axboe 	if (WARN_ON_ONCE(!req->link))
380ed29b0b4SJens Axboe 		return NULL;
381ed29b0b4SJens Axboe 
382ed29b0b4SJens Axboe 	req->flags &= ~REQ_F_ARM_LTIMEOUT;
383ed29b0b4SJens Axboe 	req->flags |= REQ_F_LINK_TIMEOUT;
384ed29b0b4SJens Axboe 
385ed29b0b4SJens Axboe 	/* linked timeouts should have two refs once prep'ed */
386ed29b0b4SJens Axboe 	io_req_set_refcount(req);
387ed29b0b4SJens Axboe 	__io_req_set_refcount(req->link, 2);
388ed29b0b4SJens Axboe 	return req->link;
389ed29b0b4SJens Axboe }
390ed29b0b4SJens Axboe 
391ed29b0b4SJens Axboe static inline struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
392ed29b0b4SJens Axboe {
393ed29b0b4SJens Axboe 	if (likely(!(req->flags & REQ_F_ARM_LTIMEOUT)))
394ed29b0b4SJens Axboe 		return NULL;
395ed29b0b4SJens Axboe 	return __io_prep_linked_timeout(req);
396ed29b0b4SJens Axboe }
397ed29b0b4SJens Axboe 
398ed29b0b4SJens Axboe static noinline void __io_arm_ltimeout(struct io_kiocb *req)
399ed29b0b4SJens Axboe {
400ed29b0b4SJens Axboe 	io_queue_linked_timeout(__io_prep_linked_timeout(req));
401ed29b0b4SJens Axboe }
402ed29b0b4SJens Axboe 
403ed29b0b4SJens Axboe static inline void io_arm_ltimeout(struct io_kiocb *req)
404ed29b0b4SJens Axboe {
405ed29b0b4SJens Axboe 	if (unlikely(req->flags & REQ_F_ARM_LTIMEOUT))
406ed29b0b4SJens Axboe 		__io_arm_ltimeout(req);
407ed29b0b4SJens Axboe }
408ed29b0b4SJens Axboe 
409ed29b0b4SJens Axboe static void io_prep_async_work(struct io_kiocb *req)
410ed29b0b4SJens Axboe {
411a7dd2782SBreno Leitao 	const struct io_issue_def *def = &io_issue_defs[req->opcode];
412ed29b0b4SJens Axboe 	struct io_ring_ctx *ctx = req->ctx;
413ed29b0b4SJens Axboe 
414ed29b0b4SJens Axboe 	if (!(req->flags & REQ_F_CREDS)) {
415ed29b0b4SJens Axboe 		req->flags |= REQ_F_CREDS;
416ed29b0b4SJens Axboe 		req->creds = get_current_cred();
417ed29b0b4SJens Axboe 	}
418ed29b0b4SJens Axboe 
419ed29b0b4SJens Axboe 	req->work.list.next = NULL;
420ed29b0b4SJens Axboe 	req->work.flags = 0;
421ed29b0b4SJens Axboe 	req->work.cancel_seq = atomic_read(&ctx->cancel_seq);
422ed29b0b4SJens Axboe 	if (req->flags & REQ_F_FORCE_ASYNC)
423ed29b0b4SJens Axboe 		req->work.flags |= IO_WQ_WORK_CONCURRENT;
424ed29b0b4SJens Axboe 
425f6b543fdSJens Axboe 	if (req->file && !io_req_ffs_set(req))
426f6b543fdSJens Axboe 		req->flags |= io_file_get_flags(req->file) << REQ_F_SUPPORT_NOWAIT_BIT;
427f6b543fdSJens Axboe 
428ed29b0b4SJens Axboe 	if (req->flags & REQ_F_ISREG) {
429d4755e15SJens Axboe 		bool should_hash = def->hash_reg_file;
430d4755e15SJens Axboe 
431d4755e15SJens Axboe 		/* don't serialize this request if the fs doesn't need it */
432d4755e15SJens Axboe 		if (should_hash && (req->file->f_flags & O_DIRECT) &&
433d4755e15SJens Axboe 		    (req->file->f_mode & FMODE_DIO_PARALLEL_WRITE))
434d4755e15SJens Axboe 			should_hash = false;
435d4755e15SJens Axboe 		if (should_hash || (ctx->flags & IORING_SETUP_IOPOLL))
436ed29b0b4SJens Axboe 			io_wq_hash_work(&req->work, file_inode(req->file));
437ed29b0b4SJens Axboe 	} else if (!req->file || !S_ISBLK(file_inode(req->file)->i_mode)) {
438ed29b0b4SJens Axboe 		if (def->unbound_nonreg_file)
439ed29b0b4SJens Axboe 			req->work.flags |= IO_WQ_WORK_UNBOUND;
440ed29b0b4SJens Axboe 	}
441ed29b0b4SJens Axboe }
442ed29b0b4SJens Axboe 
443ed29b0b4SJens Axboe static void io_prep_async_link(struct io_kiocb *req)
444ed29b0b4SJens Axboe {
445ed29b0b4SJens Axboe 	struct io_kiocb *cur;
446ed29b0b4SJens Axboe 
447ed29b0b4SJens Axboe 	if (req->flags & REQ_F_LINK_TIMEOUT) {
448ed29b0b4SJens Axboe 		struct io_ring_ctx *ctx = req->ctx;
449ed29b0b4SJens Axboe 
450ed29b0b4SJens Axboe 		spin_lock_irq(&ctx->timeout_lock);
451ed29b0b4SJens Axboe 		io_for_each_link(cur, req)
452ed29b0b4SJens Axboe 			io_prep_async_work(cur);
453ed29b0b4SJens Axboe 		spin_unlock_irq(&ctx->timeout_lock);
454ed29b0b4SJens Axboe 	} else {
455ed29b0b4SJens Axboe 		io_for_each_link(cur, req)
456ed29b0b4SJens Axboe 			io_prep_async_work(cur);
457ed29b0b4SJens Axboe 	}
458ed29b0b4SJens Axboe }
459ed29b0b4SJens Axboe 
460a282967cSPavel Begunkov void io_queue_iowq(struct io_kiocb *req, struct io_tw_state *ts_dont_use)
461ed29b0b4SJens Axboe {
462ed29b0b4SJens Axboe 	struct io_kiocb *link = io_prep_linked_timeout(req);
463ed29b0b4SJens Axboe 	struct io_uring_task *tctx = req->task->io_uring;
464ed29b0b4SJens Axboe 
465ed29b0b4SJens Axboe 	BUG_ON(!tctx);
466ed29b0b4SJens Axboe 	BUG_ON(!tctx->io_wq);
467ed29b0b4SJens Axboe 
468ed29b0b4SJens Axboe 	/* init ->work of the whole link before punting */
469ed29b0b4SJens Axboe 	io_prep_async_link(req);
470ed29b0b4SJens Axboe 
471ed29b0b4SJens Axboe 	/*
472ed29b0b4SJens Axboe 	 * Not expected to happen, but if we do have a bug where this _can_
473ed29b0b4SJens Axboe 	 * happen, catch it here and ensure the request is marked as
474ed29b0b4SJens Axboe 	 * canceled. That will make io-wq go through the usual work cancel
475ed29b0b4SJens Axboe 	 * procedure rather than attempt to run this request (or create a new
476ed29b0b4SJens Axboe 	 * worker for it).
477ed29b0b4SJens Axboe 	 */
478ed29b0b4SJens Axboe 	if (WARN_ON_ONCE(!same_thread_group(req->task, current)))
479ed29b0b4SJens Axboe 		req->work.flags |= IO_WQ_WORK_CANCEL;
480ed29b0b4SJens Axboe 
48148863ffdSPavel Begunkov 	trace_io_uring_queue_async_work(req, io_wq_is_hashed(&req->work));
482ed29b0b4SJens Axboe 	io_wq_enqueue(tctx->io_wq, &req->work);
483ed29b0b4SJens Axboe 	if (link)
484ed29b0b4SJens Axboe 		io_queue_linked_timeout(link);
485ed29b0b4SJens Axboe }
486ed29b0b4SJens Axboe 
487ed29b0b4SJens Axboe static __cold void io_queue_deferred(struct io_ring_ctx *ctx)
488ed29b0b4SJens Axboe {
489ed29b0b4SJens Axboe 	while (!list_empty(&ctx->defer_list)) {
490ed29b0b4SJens Axboe 		struct io_defer_entry *de = list_first_entry(&ctx->defer_list,
491ed29b0b4SJens Axboe 						struct io_defer_entry, list);
492ed29b0b4SJens Axboe 
493ed29b0b4SJens Axboe 		if (req_need_defer(de->req, de->seq))
494ed29b0b4SJens Axboe 			break;
495ed29b0b4SJens Axboe 		list_del_init(&de->list);
496ed29b0b4SJens Axboe 		io_req_task_queue(de->req);
497ed29b0b4SJens Axboe 		kfree(de);
498ed29b0b4SJens Axboe 	}
499ed29b0b4SJens Axboe }
500ed29b0b4SJens Axboe 
50121a091b9SDylan Yudaken 
50221a091b9SDylan Yudaken static void io_eventfd_ops(struct rcu_head *rcu)
503d8e9214fSDylan Yudaken {
504d8e9214fSDylan Yudaken 	struct io_ev_fd *ev_fd = container_of(rcu, struct io_ev_fd, rcu);
50521a091b9SDylan Yudaken 	int ops = atomic_xchg(&ev_fd->ops, 0);
506d8e9214fSDylan Yudaken 
50721a091b9SDylan Yudaken 	if (ops & BIT(IO_EVENTFD_OP_SIGNAL_BIT))
50844648532SJens Axboe 		eventfd_signal_mask(ev_fd->cq_ev_fd, 1, EPOLL_URING_WAKE);
50921a091b9SDylan Yudaken 
51021a091b9SDylan Yudaken 	/* IO_EVENTFD_OP_FREE_BIT may not be set here depending on callback
51121a091b9SDylan Yudaken 	 * ordering in a race but if references are 0 we know we have to free
51221a091b9SDylan Yudaken 	 * it regardless.
51321a091b9SDylan Yudaken 	 */
51421a091b9SDylan Yudaken 	if (atomic_dec_and_test(&ev_fd->refs)) {
515d8e9214fSDylan Yudaken 		eventfd_ctx_put(ev_fd->cq_ev_fd);
516d8e9214fSDylan Yudaken 		kfree(ev_fd);
517d8e9214fSDylan Yudaken 	}
51821a091b9SDylan Yudaken }
519d8e9214fSDylan Yudaken 
520ed29b0b4SJens Axboe static void io_eventfd_signal(struct io_ring_ctx *ctx)
521ed29b0b4SJens Axboe {
52221a091b9SDylan Yudaken 	struct io_ev_fd *ev_fd = NULL;
523ed29b0b4SJens Axboe 
524ed29b0b4SJens Axboe 	rcu_read_lock();
525ed29b0b4SJens Axboe 	/*
526ed29b0b4SJens Axboe 	 * rcu_dereference ctx->io_ev_fd once and use it for both for checking
527ed29b0b4SJens Axboe 	 * and eventfd_signal
528ed29b0b4SJens Axboe 	 */
529ed29b0b4SJens Axboe 	ev_fd = rcu_dereference(ctx->io_ev_fd);
530ed29b0b4SJens Axboe 
531ed29b0b4SJens Axboe 	/*
532ed29b0b4SJens Axboe 	 * Check again if ev_fd exists incase an io_eventfd_unregister call
533ed29b0b4SJens Axboe 	 * completed between the NULL check of ctx->io_ev_fd at the start of
534ed29b0b4SJens Axboe 	 * the function and rcu_read_lock.
535ed29b0b4SJens Axboe 	 */
536ed29b0b4SJens Axboe 	if (unlikely(!ev_fd))
537ed29b0b4SJens Axboe 		goto out;
538ed29b0b4SJens Axboe 	if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED)
539ed29b0b4SJens Axboe 		goto out;
54021a091b9SDylan Yudaken 	if (ev_fd->eventfd_async && !io_wq_current_is_worker())
54121a091b9SDylan Yudaken 		goto out;
542ed29b0b4SJens Axboe 
54321a091b9SDylan Yudaken 	if (likely(eventfd_signal_allowed())) {
54444648532SJens Axboe 		eventfd_signal_mask(ev_fd->cq_ev_fd, 1, EPOLL_URING_WAKE);
54521a091b9SDylan Yudaken 	} else {
54621a091b9SDylan Yudaken 		atomic_inc(&ev_fd->refs);
54721a091b9SDylan Yudaken 		if (!atomic_fetch_or(BIT(IO_EVENTFD_OP_SIGNAL_BIT), &ev_fd->ops))
54844a84da4SDylan Yudaken 			call_rcu_hurry(&ev_fd->rcu, io_eventfd_ops);
54921a091b9SDylan Yudaken 		else
55021a091b9SDylan Yudaken 			atomic_dec(&ev_fd->refs);
55121a091b9SDylan Yudaken 	}
55221a091b9SDylan Yudaken 
553ed29b0b4SJens Axboe out:
554ed29b0b4SJens Axboe 	rcu_read_unlock();
555ed29b0b4SJens Axboe }
556ed29b0b4SJens Axboe 
55721a091b9SDylan Yudaken static void io_eventfd_flush_signal(struct io_ring_ctx *ctx)
55821a091b9SDylan Yudaken {
55921a091b9SDylan Yudaken 	bool skip;
56021a091b9SDylan Yudaken 
56121a091b9SDylan Yudaken 	spin_lock(&ctx->completion_lock);
56221a091b9SDylan Yudaken 
56321a091b9SDylan Yudaken 	/*
56421a091b9SDylan Yudaken 	 * Eventfd should only get triggered when at least one event has been
56521a091b9SDylan Yudaken 	 * posted. Some applications rely on the eventfd notification count
56621a091b9SDylan Yudaken 	 * only changing IFF a new CQE has been added to the CQ ring. There's
56721a091b9SDylan Yudaken 	 * no depedency on 1:1 relationship between how many times this
56821a091b9SDylan Yudaken 	 * function is called (and hence the eventfd count) and number of CQEs
56921a091b9SDylan Yudaken 	 * posted to the CQ ring.
57021a091b9SDylan Yudaken 	 */
57121a091b9SDylan Yudaken 	skip = ctx->cached_cq_tail == ctx->evfd_last_cq_tail;
57221a091b9SDylan Yudaken 	ctx->evfd_last_cq_tail = ctx->cached_cq_tail;
57321a091b9SDylan Yudaken 	spin_unlock(&ctx->completion_lock);
57421a091b9SDylan Yudaken 	if (skip)
57521a091b9SDylan Yudaken 		return;
57621a091b9SDylan Yudaken 
57721a091b9SDylan Yudaken 	io_eventfd_signal(ctx);
57821a091b9SDylan Yudaken }
57921a091b9SDylan Yudaken 
580a830ffd2SPavel Begunkov void __io_commit_cqring_flush(struct io_ring_ctx *ctx)
581a830ffd2SPavel Begunkov {
582bca39f39SPavel Begunkov 	if (ctx->poll_activated)
583bca39f39SPavel Begunkov 		io_poll_wq_wake(ctx);
584a830ffd2SPavel Begunkov 	if (ctx->off_timeout_used)
585a830ffd2SPavel Begunkov 		io_flush_timeouts(ctx);
586e5f30f6fSPavel Begunkov 	if (ctx->drain_active) {
587e5f30f6fSPavel Begunkov 		spin_lock(&ctx->completion_lock);
588a830ffd2SPavel Begunkov 		io_queue_deferred(ctx);
589a830ffd2SPavel Begunkov 		spin_unlock(&ctx->completion_lock);
590a830ffd2SPavel Begunkov 	}
591a830ffd2SPavel Begunkov 	if (ctx->has_evfd)
59221a091b9SDylan Yudaken 		io_eventfd_flush_signal(ctx);
593a830ffd2SPavel Begunkov }
594a830ffd2SPavel Begunkov 
595f66f7342SPavel Begunkov static inline void __io_cq_lock(struct io_ring_ctx *ctx)
596f66f7342SPavel Begunkov 	__acquires(ctx->completion_lock)
597f66f7342SPavel Begunkov {
598f66f7342SPavel Begunkov 	if (!ctx->task_complete)
599f66f7342SPavel Begunkov 		spin_lock(&ctx->completion_lock);
600f66f7342SPavel Begunkov }
601f66f7342SPavel Begunkov 
602f66f7342SPavel Begunkov static inline void __io_cq_unlock(struct io_ring_ctx *ctx)
603f66f7342SPavel Begunkov {
604f66f7342SPavel Begunkov 	if (!ctx->task_complete)
605f66f7342SPavel Begunkov 		spin_unlock(&ctx->completion_lock);
606f66f7342SPavel Begunkov }
607f66f7342SPavel Begunkov 
6086971253fSPavel Begunkov static inline void io_cq_lock(struct io_ring_ctx *ctx)
6096971253fSPavel Begunkov 	__acquires(ctx->completion_lock)
6106971253fSPavel Begunkov {
6116971253fSPavel Begunkov 	spin_lock(&ctx->completion_lock);
6126971253fSPavel Begunkov }
6136971253fSPavel Begunkov 
6146971253fSPavel Begunkov static inline void io_cq_unlock(struct io_ring_ctx *ctx)
6156971253fSPavel Begunkov 	__releases(ctx->completion_lock)
6166971253fSPavel Begunkov {
6176971253fSPavel Begunkov 	spin_unlock(&ctx->completion_lock);
6186971253fSPavel Begunkov }
6196971253fSPavel Begunkov 
6205d772916SPavel Begunkov /* keep it inlined for io_submit_flush_completions() */
621f66f7342SPavel Begunkov static inline void __io_cq_unlock_post(struct io_ring_ctx *ctx)
62225399321SPavel Begunkov 	__releases(ctx->completion_lock)
62325399321SPavel Begunkov {
62425399321SPavel Begunkov 	io_commit_cqring(ctx);
625f66f7342SPavel Begunkov 	__io_cq_unlock(ctx);
6266c16fe3cSJens Axboe 	io_commit_cqring_flush(ctx);
6276c16fe3cSJens Axboe 	io_cqring_wake(ctx);
62825399321SPavel Begunkov }
62925399321SPavel Begunkov 
6303181e22fSPavel Begunkov static inline void __io_cq_unlock_post_flush(struct io_ring_ctx *ctx)
6313181e22fSPavel Begunkov 	__releases(ctx->completion_lock)
6323181e22fSPavel Begunkov {
6333181e22fSPavel Begunkov 	io_commit_cqring(ctx);
6343181e22fSPavel Begunkov 	__io_cq_unlock(ctx);
6353181e22fSPavel Begunkov 	io_commit_cqring_flush(ctx);
6363181e22fSPavel Begunkov 
6373181e22fSPavel Begunkov 	/*
6383181e22fSPavel Begunkov 	 * As ->task_complete implies that the ring is single tasked, cq_wait
6393181e22fSPavel Begunkov 	 * may only be waited on by the current in io_cqring_wait(), but since
6403181e22fSPavel Begunkov 	 * it will re-check the wakeup conditions once we return we can safely
6413181e22fSPavel Begunkov 	 * skip waking it up.
6423181e22fSPavel Begunkov 	 */
6436e7248adSPavel Begunkov 	if (!(ctx->flags & IORING_SETUP_DEFER_TASKRUN))
6446e7248adSPavel Begunkov 		io_cqring_wake(ctx);
6453181e22fSPavel Begunkov }
6463181e22fSPavel Begunkov 
64725399321SPavel Begunkov void io_cq_unlock_post(struct io_ring_ctx *ctx)
6485d772916SPavel Begunkov 	__releases(ctx->completion_lock)
64925399321SPavel Begunkov {
650f66f7342SPavel Begunkov 	io_commit_cqring(ctx);
651f66f7342SPavel Begunkov 	spin_unlock(&ctx->completion_lock);
652f66f7342SPavel Begunkov 	io_commit_cqring_flush(ctx);
653f66f7342SPavel Begunkov 	io_cqring_wake(ctx);
65425399321SPavel Begunkov }
65525399321SPavel Begunkov 
656ed29b0b4SJens Axboe /* Returns true if there are no backlogged entries after the flush */
657a85381d8SPavel Begunkov static void io_cqring_overflow_kill(struct io_ring_ctx *ctx)
658ed29b0b4SJens Axboe {
659a85381d8SPavel Begunkov 	struct io_overflow_cqe *ocqe;
660a85381d8SPavel Begunkov 	LIST_HEAD(list);
661a85381d8SPavel Begunkov 
662a85381d8SPavel Begunkov 	io_cq_lock(ctx);
663a85381d8SPavel Begunkov 	list_splice_init(&ctx->cq_overflow_list, &list);
664a85381d8SPavel Begunkov 	clear_bit(IO_CHECK_CQ_OVERFLOW_BIT, &ctx->check_cq);
665a85381d8SPavel Begunkov 	io_cq_unlock(ctx);
666a85381d8SPavel Begunkov 
667a85381d8SPavel Begunkov 	while (!list_empty(&list)) {
668a85381d8SPavel Begunkov 		ocqe = list_first_entry(&list, struct io_overflow_cqe, list);
669a85381d8SPavel Begunkov 		list_del(&ocqe->list);
670a85381d8SPavel Begunkov 		kfree(ocqe);
671a85381d8SPavel Begunkov 	}
672a85381d8SPavel Begunkov }
673a85381d8SPavel Begunkov 
6741b346e4aSPavel Begunkov static void __io_cqring_overflow_flush(struct io_ring_ctx *ctx)
675ed29b0b4SJens Axboe {
676ed29b0b4SJens Axboe 	size_t cqe_size = sizeof(struct io_uring_cqe);
677ed29b0b4SJens Axboe 
678a85381d8SPavel Begunkov 	if (__io_cqring_events(ctx) == ctx->cq_entries)
6791b346e4aSPavel Begunkov 		return;
680ed29b0b4SJens Axboe 
681ed29b0b4SJens Axboe 	if (ctx->flags & IORING_SETUP_CQE32)
682ed29b0b4SJens Axboe 		cqe_size <<= 1;
683ed29b0b4SJens Axboe 
68425399321SPavel Begunkov 	io_cq_lock(ctx);
685ed29b0b4SJens Axboe 	while (!list_empty(&ctx->cq_overflow_list)) {
686aa1df3a3SPavel Begunkov 		struct io_uring_cqe *cqe = io_get_cqe_overflow(ctx, true);
687ed29b0b4SJens Axboe 		struct io_overflow_cqe *ocqe;
688ed29b0b4SJens Axboe 
689a85381d8SPavel Begunkov 		if (!cqe)
690ed29b0b4SJens Axboe 			break;
691ed29b0b4SJens Axboe 		ocqe = list_first_entry(&ctx->cq_overflow_list,
692ed29b0b4SJens Axboe 					struct io_overflow_cqe, list);
693ed29b0b4SJens Axboe 		memcpy(cqe, &ocqe->cqe, cqe_size);
694ed29b0b4SJens Axboe 		list_del(&ocqe->list);
695ed29b0b4SJens Axboe 		kfree(ocqe);
696ed29b0b4SJens Axboe 	}
697ed29b0b4SJens Axboe 
6981b346e4aSPavel Begunkov 	if (list_empty(&ctx->cq_overflow_list)) {
699ed29b0b4SJens Axboe 		clear_bit(IO_CHECK_CQ_OVERFLOW_BIT, &ctx->check_cq);
700ed29b0b4SJens Axboe 		atomic_andnot(IORING_SQ_CQ_OVERFLOW, &ctx->rings->sq_flags);
701ed29b0b4SJens Axboe 	}
70225399321SPavel Begunkov 	io_cq_unlock_post(ctx);
703ed29b0b4SJens Axboe }
704ed29b0b4SJens Axboe 
70552ea806aSJens Axboe static void io_cqring_do_overflow_flush(struct io_ring_ctx *ctx)
706ed29b0b4SJens Axboe {
707ed29b0b4SJens Axboe 	/* iopoll syncs against uring_lock, not completion_lock */
708ed29b0b4SJens Axboe 	if (ctx->flags & IORING_SETUP_IOPOLL)
709ed29b0b4SJens Axboe 		mutex_lock(&ctx->uring_lock);
7101b346e4aSPavel Begunkov 	__io_cqring_overflow_flush(ctx);
711ed29b0b4SJens Axboe 	if (ctx->flags & IORING_SETUP_IOPOLL)
712ed29b0b4SJens Axboe 		mutex_unlock(&ctx->uring_lock);
713ed29b0b4SJens Axboe }
71452ea806aSJens Axboe 
71552ea806aSJens Axboe static void io_cqring_overflow_flush(struct io_ring_ctx *ctx)
71652ea806aSJens Axboe {
71752ea806aSJens Axboe 	if (test_bit(IO_CHECK_CQ_OVERFLOW_BIT, &ctx->check_cq))
71852ea806aSJens Axboe 		io_cqring_do_overflow_flush(ctx);
719ed29b0b4SJens Axboe }
720ed29b0b4SJens Axboe 
7215afa4650SPavel Begunkov /* can be called by any task */
7225afa4650SPavel Begunkov static void io_put_task_remote(struct task_struct *task, int nr)
723ed29b0b4SJens Axboe {
724ed29b0b4SJens Axboe 	struct io_uring_task *tctx = task->io_uring;
725ed29b0b4SJens Axboe 
726ed29b0b4SJens Axboe 	percpu_counter_sub(&tctx->inflight, nr);
7278d664282SJens Axboe 	if (unlikely(atomic_read(&tctx->in_cancel)))
728ed29b0b4SJens Axboe 		wake_up(&tctx->wait);
729ed29b0b4SJens Axboe 	put_task_struct_many(task, nr);
730ed29b0b4SJens Axboe }
731ed29b0b4SJens Axboe 
7325afa4650SPavel Begunkov /* used by a task to put its own references */
7335afa4650SPavel Begunkov static void io_put_task_local(struct task_struct *task, int nr)
7345afa4650SPavel Begunkov {
7355afa4650SPavel Begunkov 	task->io_uring->cached_refs += nr;
7365afa4650SPavel Begunkov }
7375afa4650SPavel Begunkov 
73889800a2dSPavel Begunkov /* must to be called somewhat shortly after putting a request */
73989800a2dSPavel Begunkov static inline void io_put_task(struct task_struct *task, int nr)
74089800a2dSPavel Begunkov {
74189800a2dSPavel Begunkov 	if (likely(task == current))
7425afa4650SPavel Begunkov 		io_put_task_local(task, nr);
74389800a2dSPavel Begunkov 	else
7445afa4650SPavel Begunkov 		io_put_task_remote(task, nr);
74589800a2dSPavel Begunkov }
74689800a2dSPavel Begunkov 
74763809137SPavel Begunkov void io_task_refs_refill(struct io_uring_task *tctx)
748ed29b0b4SJens Axboe {
749ed29b0b4SJens Axboe 	unsigned int refill = -tctx->cached_refs + IO_TCTX_REFS_CACHE_NR;
750ed29b0b4SJens Axboe 
751ed29b0b4SJens Axboe 	percpu_counter_add(&tctx->inflight, refill);
752ed29b0b4SJens Axboe 	refcount_add(refill, &current->usage);
753ed29b0b4SJens Axboe 	tctx->cached_refs += refill;
754ed29b0b4SJens Axboe }
755ed29b0b4SJens Axboe 
756ed29b0b4SJens Axboe static __cold void io_uring_drop_tctx_refs(struct task_struct *task)
757ed29b0b4SJens Axboe {
758ed29b0b4SJens Axboe 	struct io_uring_task *tctx = task->io_uring;
759ed29b0b4SJens Axboe 	unsigned int refs = tctx->cached_refs;
760ed29b0b4SJens Axboe 
761ed29b0b4SJens Axboe 	if (refs) {
762ed29b0b4SJens Axboe 		tctx->cached_refs = 0;
763ed29b0b4SJens Axboe 		percpu_counter_sub(&tctx->inflight, refs);
764ed29b0b4SJens Axboe 		put_task_struct_many(task, refs);
765ed29b0b4SJens Axboe 	}
766ed29b0b4SJens Axboe }
767ed29b0b4SJens Axboe 
76868494a65SPavel Begunkov static bool io_cqring_event_overflow(struct io_ring_ctx *ctx, u64 user_data,
76968494a65SPavel Begunkov 				     s32 res, u32 cflags, u64 extra1, u64 extra2)
770ed29b0b4SJens Axboe {
771ed29b0b4SJens Axboe 	struct io_overflow_cqe *ocqe;
772ed29b0b4SJens Axboe 	size_t ocq_size = sizeof(struct io_overflow_cqe);
773ed29b0b4SJens Axboe 	bool is_cqe32 = (ctx->flags & IORING_SETUP_CQE32);
774ed29b0b4SJens Axboe 
775f26cc959SPavel Begunkov 	lockdep_assert_held(&ctx->completion_lock);
776f26cc959SPavel Begunkov 
777ed29b0b4SJens Axboe 	if (is_cqe32)
778ed29b0b4SJens Axboe 		ocq_size += sizeof(struct io_uring_cqe);
779ed29b0b4SJens Axboe 
780ed29b0b4SJens Axboe 	ocqe = kmalloc(ocq_size, GFP_ATOMIC | __GFP_ACCOUNT);
781ed29b0b4SJens Axboe 	trace_io_uring_cqe_overflow(ctx, user_data, res, cflags, ocqe);
782ed29b0b4SJens Axboe 	if (!ocqe) {
783ed29b0b4SJens Axboe 		/*
784ed29b0b4SJens Axboe 		 * If we're in ring overflow flush mode, or in task cancel mode,
785ed29b0b4SJens Axboe 		 * or cannot allocate an overflow entry, then we need to drop it
786ed29b0b4SJens Axboe 		 * on the floor.
787ed29b0b4SJens Axboe 		 */
788ed29b0b4SJens Axboe 		io_account_cq_overflow(ctx);
789ed29b0b4SJens Axboe 		set_bit(IO_CHECK_CQ_DROPPED_BIT, &ctx->check_cq);
790ed29b0b4SJens Axboe 		return false;
791ed29b0b4SJens Axboe 	}
792ed29b0b4SJens Axboe 	if (list_empty(&ctx->cq_overflow_list)) {
793ed29b0b4SJens Axboe 		set_bit(IO_CHECK_CQ_OVERFLOW_BIT, &ctx->check_cq);
794ed29b0b4SJens Axboe 		atomic_or(IORING_SQ_CQ_OVERFLOW, &ctx->rings->sq_flags);
795ed29b0b4SJens Axboe 
796ed29b0b4SJens Axboe 	}
797ed29b0b4SJens Axboe 	ocqe->cqe.user_data = user_data;
798ed29b0b4SJens Axboe 	ocqe->cqe.res = res;
799ed29b0b4SJens Axboe 	ocqe->cqe.flags = cflags;
800ed29b0b4SJens Axboe 	if (is_cqe32) {
801ed29b0b4SJens Axboe 		ocqe->cqe.big_cqe[0] = extra1;
802ed29b0b4SJens Axboe 		ocqe->cqe.big_cqe[1] = extra2;
803ed29b0b4SJens Axboe 	}
804ed29b0b4SJens Axboe 	list_add_tail(&ocqe->list, &ctx->cq_overflow_list);
805ed29b0b4SJens Axboe 	return true;
806ed29b0b4SJens Axboe }
807ed29b0b4SJens Axboe 
80868494a65SPavel Begunkov bool io_req_cqe_overflow(struct io_kiocb *req)
80968494a65SPavel Begunkov {
81068494a65SPavel Begunkov 	if (!(req->flags & REQ_F_CQE32_INIT)) {
81168494a65SPavel Begunkov 		req->extra1 = 0;
81268494a65SPavel Begunkov 		req->extra2 = 0;
81368494a65SPavel Begunkov 	}
81468494a65SPavel Begunkov 	return io_cqring_event_overflow(req->ctx, req->cqe.user_data,
81568494a65SPavel Begunkov 					req->cqe.res, req->cqe.flags,
81668494a65SPavel Begunkov 					req->extra1, req->extra2);
81768494a65SPavel Begunkov }
81868494a65SPavel Begunkov 
819faf88ddeSPavel Begunkov /*
820faf88ddeSPavel Begunkov  * writes to the cq entry need to come after reading head; the
821faf88ddeSPavel Begunkov  * control dependency is enough as we're using WRITE_ONCE to
822faf88ddeSPavel Begunkov  * fill the cq entry
823faf88ddeSPavel Begunkov  */
824aa1df3a3SPavel Begunkov struct io_uring_cqe *__io_get_cqe(struct io_ring_ctx *ctx, bool overflow)
825faf88ddeSPavel Begunkov {
826faf88ddeSPavel Begunkov 	struct io_rings *rings = ctx->rings;
827faf88ddeSPavel Begunkov 	unsigned int off = ctx->cached_cq_tail & (ctx->cq_entries - 1);
828faf88ddeSPavel Begunkov 	unsigned int free, queued, len;
829faf88ddeSPavel Begunkov 
830aa1df3a3SPavel Begunkov 	/*
831aa1df3a3SPavel Begunkov 	 * Posting into the CQ when there are pending overflowed CQEs may break
832aa1df3a3SPavel Begunkov 	 * ordering guarantees, which will affect links, F_MORE users and more.
833aa1df3a3SPavel Begunkov 	 * Force overflow the completion.
834aa1df3a3SPavel Begunkov 	 */
835aa1df3a3SPavel Begunkov 	if (!overflow && (ctx->check_cq & BIT(IO_CHECK_CQ_OVERFLOW_BIT)))
836aa1df3a3SPavel Begunkov 		return NULL;
837faf88ddeSPavel Begunkov 
838faf88ddeSPavel Begunkov 	/* userspace may cheat modifying the tail, be safe and do min */
839faf88ddeSPavel Begunkov 	queued = min(__io_cqring_events(ctx), ctx->cq_entries);
840faf88ddeSPavel Begunkov 	free = ctx->cq_entries - queued;
841faf88ddeSPavel Begunkov 	/* we need a contiguous range, limit based on the current array offset */
842faf88ddeSPavel Begunkov 	len = min(free, ctx->cq_entries - off);
843faf88ddeSPavel Begunkov 	if (!len)
844faf88ddeSPavel Begunkov 		return NULL;
845faf88ddeSPavel Begunkov 
846b3659a65SPavel Begunkov 	if (ctx->flags & IORING_SETUP_CQE32) {
847b3659a65SPavel Begunkov 		off <<= 1;
848b3659a65SPavel Begunkov 		len <<= 1;
849b3659a65SPavel Begunkov 	}
850b3659a65SPavel Begunkov 
851faf88ddeSPavel Begunkov 	ctx->cqe_cached = &rings->cqes[off];
852faf88ddeSPavel Begunkov 	ctx->cqe_sentinel = ctx->cqe_cached + len;
853b3659a65SPavel Begunkov 
854b3659a65SPavel Begunkov 	ctx->cached_cq_tail++;
855faf88ddeSPavel Begunkov 	ctx->cqe_cached++;
856b3659a65SPavel Begunkov 	if (ctx->flags & IORING_SETUP_CQE32)
857b3659a65SPavel Begunkov 		ctx->cqe_cached++;
858b3659a65SPavel Begunkov 	return &rings->cqes[off];
859faf88ddeSPavel Begunkov }
860faf88ddeSPavel Begunkov 
861f66f7342SPavel Begunkov static bool io_fill_cqe_aux(struct io_ring_ctx *ctx, u64 user_data, s32 res,
862f66f7342SPavel Begunkov 			      u32 cflags)
863ed29b0b4SJens Axboe {
864ed29b0b4SJens Axboe 	struct io_uring_cqe *cqe;
865ed29b0b4SJens Axboe 
866ed29b0b4SJens Axboe 	ctx->cq_extra++;
867ed29b0b4SJens Axboe 
868ed29b0b4SJens Axboe 	/*
869ed29b0b4SJens Axboe 	 * If we can't get a cq entry, userspace overflowed the
870ed29b0b4SJens Axboe 	 * submission (by quite a lot). Increment the overflow count in
871ed29b0b4SJens Axboe 	 * the ring.
872ed29b0b4SJens Axboe 	 */
873ed29b0b4SJens Axboe 	cqe = io_get_cqe(ctx);
874ed29b0b4SJens Axboe 	if (likely(cqe)) {
875e0486f3fSDylan Yudaken 		trace_io_uring_complete(ctx, NULL, user_data, res, cflags, 0, 0);
876e0486f3fSDylan Yudaken 
877ed29b0b4SJens Axboe 		WRITE_ONCE(cqe->user_data, user_data);
878ed29b0b4SJens Axboe 		WRITE_ONCE(cqe->res, res);
879ed29b0b4SJens Axboe 		WRITE_ONCE(cqe->flags, cflags);
880ed29b0b4SJens Axboe 
881ed29b0b4SJens Axboe 		if (ctx->flags & IORING_SETUP_CQE32) {
882ed29b0b4SJens Axboe 			WRITE_ONCE(cqe->big_cqe[0], 0);
883ed29b0b4SJens Axboe 			WRITE_ONCE(cqe->big_cqe[1], 0);
884ed29b0b4SJens Axboe 		}
885ed29b0b4SJens Axboe 		return true;
886ed29b0b4SJens Axboe 	}
88752120f0fSDylan Yudaken 	return false;
888ed29b0b4SJens Axboe }
889ed29b0b4SJens Axboe 
890931147ddSDylan Yudaken static void __io_flush_post_cqes(struct io_ring_ctx *ctx)
891931147ddSDylan Yudaken 	__must_hold(&ctx->uring_lock)
892931147ddSDylan Yudaken {
893931147ddSDylan Yudaken 	struct io_submit_state *state = &ctx->submit_state;
894931147ddSDylan Yudaken 	unsigned int i;
895931147ddSDylan Yudaken 
896931147ddSDylan Yudaken 	lockdep_assert_held(&ctx->uring_lock);
897931147ddSDylan Yudaken 	for (i = 0; i < state->cqes_count; i++) {
898931147ddSDylan Yudaken 		struct io_uring_cqe *cqe = &state->cqes[i];
899931147ddSDylan Yudaken 
900f66f7342SPavel Begunkov 		if (!io_fill_cqe_aux(ctx, cqe->user_data, cqe->res, cqe->flags)) {
901f66f7342SPavel Begunkov 			if (ctx->task_complete) {
902f66f7342SPavel Begunkov 				spin_lock(&ctx->completion_lock);
903f66f7342SPavel Begunkov 				io_cqring_event_overflow(ctx, cqe->user_data,
904f66f7342SPavel Begunkov 							cqe->res, cqe->flags, 0, 0);
905f66f7342SPavel Begunkov 				spin_unlock(&ctx->completion_lock);
906f66f7342SPavel Begunkov 			} else {
907f66f7342SPavel Begunkov 				io_cqring_event_overflow(ctx, cqe->user_data,
908f66f7342SPavel Begunkov 							cqe->res, cqe->flags, 0, 0);
909f66f7342SPavel Begunkov 			}
910f66f7342SPavel Begunkov 		}
911931147ddSDylan Yudaken 	}
912931147ddSDylan Yudaken 	state->cqes_count = 0;
913931147ddSDylan Yudaken }
914931147ddSDylan Yudaken 
915b529c96aSDylan Yudaken static bool __io_post_aux_cqe(struct io_ring_ctx *ctx, u64 user_data, s32 res, u32 cflags,
91652120f0fSDylan Yudaken 			      bool allow_overflow)
917d245bca6SPavel Begunkov {
918d245bca6SPavel Begunkov 	bool filled;
919d245bca6SPavel Begunkov 
92025399321SPavel Begunkov 	io_cq_lock(ctx);
921f66f7342SPavel Begunkov 	filled = io_fill_cqe_aux(ctx, user_data, res, cflags);
922f66f7342SPavel Begunkov 	if (!filled && allow_overflow)
923f66f7342SPavel Begunkov 		filled = io_cqring_event_overflow(ctx, user_data, res, cflags, 0, 0);
924f66f7342SPavel Begunkov 
92525399321SPavel Begunkov 	io_cq_unlock_post(ctx);
926d245bca6SPavel Begunkov 	return filled;
927d245bca6SPavel Begunkov }
928d245bca6SPavel Begunkov 
929b529c96aSDylan Yudaken bool io_post_aux_cqe(struct io_ring_ctx *ctx, u64 user_data, s32 res, u32 cflags)
930ed29b0b4SJens Axboe {
931b529c96aSDylan Yudaken 	return __io_post_aux_cqe(ctx, user_data, res, cflags, true);
932b529c96aSDylan Yudaken }
933b529c96aSDylan Yudaken 
9349b8c5475SDylan Yudaken bool io_aux_cqe(struct io_ring_ctx *ctx, bool defer, u64 user_data, s32 res, u32 cflags,
9359b8c5475SDylan Yudaken 		bool allow_overflow)
9369b8c5475SDylan Yudaken {
9379b8c5475SDylan Yudaken 	struct io_uring_cqe *cqe;
9389b8c5475SDylan Yudaken 	unsigned int length;
9399b8c5475SDylan Yudaken 
9409b8c5475SDylan Yudaken 	if (!defer)
941b529c96aSDylan Yudaken 		return __io_post_aux_cqe(ctx, user_data, res, cflags, allow_overflow);
9429b8c5475SDylan Yudaken 
9439b8c5475SDylan Yudaken 	length = ARRAY_SIZE(ctx->submit_state.cqes);
9449b8c5475SDylan Yudaken 
9459b8c5475SDylan Yudaken 	lockdep_assert_held(&ctx->uring_lock);
9469b8c5475SDylan Yudaken 
9479b8c5475SDylan Yudaken 	if (ctx->submit_state.cqes_count == length) {
948f66f7342SPavel Begunkov 		__io_cq_lock(ctx);
9499b8c5475SDylan Yudaken 		__io_flush_post_cqes(ctx);
9509b8c5475SDylan Yudaken 		/* no need to flush - flush is deferred */
951f66f7342SPavel Begunkov 		__io_cq_unlock_post(ctx);
9529b8c5475SDylan Yudaken 	}
9539b8c5475SDylan Yudaken 
9549b8c5475SDylan Yudaken 	/* For defered completions this is not as strict as it is otherwise,
9559b8c5475SDylan Yudaken 	 * however it's main job is to prevent unbounded posted completions,
9569b8c5475SDylan Yudaken 	 * and in that it works just as well.
9579b8c5475SDylan Yudaken 	 */
9589b8c5475SDylan Yudaken 	if (!allow_overflow && test_bit(IO_CHECK_CQ_OVERFLOW_BIT, &ctx->check_cq))
9599b8c5475SDylan Yudaken 		return false;
9609b8c5475SDylan Yudaken 
9619b8c5475SDylan Yudaken 	cqe = &ctx->submit_state.cqes[ctx->submit_state.cqes_count++];
9629b8c5475SDylan Yudaken 	cqe->user_data = user_data;
9639b8c5475SDylan Yudaken 	cqe->res = res;
9649b8c5475SDylan Yudaken 	cqe->flags = cflags;
9659b8c5475SDylan Yudaken 	return true;
9669b8c5475SDylan Yudaken }
9679b8c5475SDylan Yudaken 
968ef8ae64fSPavel Begunkov static void __io_req_complete_post(struct io_kiocb *req, unsigned issue_flags)
969ed29b0b4SJens Axboe {
970fa18fa22SPavel Begunkov 	struct io_ring_ctx *ctx = req->ctx;
9712ad4c6d0SPavel Begunkov 	struct io_rsrc_node *rsrc_node = NULL;
972fa18fa22SPavel Begunkov 
973fa18fa22SPavel Begunkov 	io_cq_lock(ctx);
974fa18fa22SPavel Begunkov 	if (!(req->flags & REQ_F_CQE_SKIP))
975a8cf95f9SPavel Begunkov 		io_fill_cqe_req(ctx, req);
976fa18fa22SPavel Begunkov 
977ed29b0b4SJens Axboe 	/*
978ed29b0b4SJens Axboe 	 * If we're the last reference to this request, add to our locked
979ed29b0b4SJens Axboe 	 * free_list cache.
980ed29b0b4SJens Axboe 	 */
981ed29b0b4SJens Axboe 	if (req_ref_put_and_test(req)) {
982ed29b0b4SJens Axboe 		if (req->flags & IO_REQ_LINK_FLAGS) {
983ed29b0b4SJens Axboe 			if (req->flags & IO_DISARM_MASK)
984ed29b0b4SJens Axboe 				io_disarm_next(req);
985ed29b0b4SJens Axboe 			if (req->link) {
986ed29b0b4SJens Axboe 				io_req_task_queue(req->link);
987ed29b0b4SJens Axboe 				req->link = NULL;
988ed29b0b4SJens Axboe 			}
989ed29b0b4SJens Axboe 		}
99068a2cc1bSPavel Begunkov 		io_put_kbuf_comp(req);
99168a2cc1bSPavel Begunkov 		io_dismantle_req(req);
9922ad4c6d0SPavel Begunkov 		rsrc_node = req->rsrc_node;
993ed29b0b4SJens Axboe 		/*
994ed29b0b4SJens Axboe 		 * Selected buffer deallocation in io_clean_op() assumes that
995ed29b0b4SJens Axboe 		 * we don't hold ->completion_lock. Clean them here to avoid
996ed29b0b4SJens Axboe 		 * deadlocks.
997ed29b0b4SJens Axboe 		 */
9985afa4650SPavel Begunkov 		io_put_task_remote(req->task, 1);
999ed29b0b4SJens Axboe 		wq_list_add_head(&req->comp_list, &ctx->locked_free_list);
1000ed29b0b4SJens Axboe 		ctx->locked_free_nr++;
1001ed29b0b4SJens Axboe 	}
100225399321SPavel Begunkov 	io_cq_unlock_post(ctx);
10032ad4c6d0SPavel Begunkov 
1004ef8ae64fSPavel Begunkov 	if (rsrc_node) {
1005ef8ae64fSPavel Begunkov 		io_ring_submit_lock(ctx, issue_flags);
10061f2c8f61SPavel Begunkov 		io_put_rsrc_node(ctx, rsrc_node);
1007ef8ae64fSPavel Begunkov 		io_ring_submit_unlock(ctx, issue_flags);
1008ef8ae64fSPavel Begunkov 	}
1009ed29b0b4SJens Axboe }
1010ed29b0b4SJens Axboe 
10111bec951cSPavel Begunkov void io_req_complete_post(struct io_kiocb *req, unsigned issue_flags)
1012ed29b0b4SJens Axboe {
1013e6aeb272SPavel Begunkov 	if (req->ctx->task_complete && (issue_flags & IO_URING_F_IOWQ)) {
1014e6aeb272SPavel Begunkov 		req->io_task_work.func = io_req_task_complete;
1015e6aeb272SPavel Begunkov 		io_req_task_work_add(req);
1016e6aeb272SPavel Begunkov 	} else if (!(issue_flags & IO_URING_F_UNLOCKED) ||
10171bec951cSPavel Begunkov 		   !(req->ctx->flags & IORING_SETUP_IOPOLL)) {
1018ef8ae64fSPavel Begunkov 		__io_req_complete_post(req, issue_flags);
10191bec951cSPavel Begunkov 	} else {
10201bec951cSPavel Begunkov 		struct io_ring_ctx *ctx = req->ctx;
10211bec951cSPavel Begunkov 
10221bec951cSPavel Begunkov 		mutex_lock(&ctx->uring_lock);
1023ef8ae64fSPavel Begunkov 		__io_req_complete_post(req, issue_flags & ~IO_URING_F_UNLOCKED);
10241bec951cSPavel Begunkov 		mutex_unlock(&ctx->uring_lock);
10251bec951cSPavel Begunkov 	}
1026ed29b0b4SJens Axboe }
1027ed29b0b4SJens Axboe 
1028973fc83fSDylan Yudaken void io_req_defer_failed(struct io_kiocb *req, s32 res)
1029e276ae34SPavel Begunkov 	__must_hold(&ctx->uring_lock)
1030ed29b0b4SJens Axboe {
1031f30bd4d0SBreno Leitao 	const struct io_cold_def *def = &io_cold_defs[req->opcode];
1032a47b255eSPavel Begunkov 
1033e276ae34SPavel Begunkov 	lockdep_assert_held(&req->ctx->uring_lock);
1034e276ae34SPavel Begunkov 
1035ed29b0b4SJens Axboe 	req_set_fail(req);
103697b388d7SJens Axboe 	io_req_set_res(req, res, io_put_kbuf(req, IO_URING_F_UNLOCKED));
1037a47b255eSPavel Begunkov 	if (def->fail)
1038a47b255eSPavel Begunkov 		def->fail(req);
1039973fc83fSDylan Yudaken 	io_req_complete_defer(req);
1040ed29b0b4SJens Axboe }
1041ed29b0b4SJens Axboe 
1042ed29b0b4SJens Axboe /*
1043ed29b0b4SJens Axboe  * Don't initialise the fields below on every allocation, but do that in
1044ed29b0b4SJens Axboe  * advance and keep them valid across allocations.
1045ed29b0b4SJens Axboe  */
1046ed29b0b4SJens Axboe static void io_preinit_req(struct io_kiocb *req, struct io_ring_ctx *ctx)
1047ed29b0b4SJens Axboe {
1048ed29b0b4SJens Axboe 	req->ctx = ctx;
1049ed29b0b4SJens Axboe 	req->link = NULL;
1050ed29b0b4SJens Axboe 	req->async_data = NULL;
1051ed29b0b4SJens Axboe 	/* not necessary, but safer to zero */
1052ed29b0b4SJens Axboe 	req->cqe.res = 0;
1053ed29b0b4SJens Axboe }
1054ed29b0b4SJens Axboe 
1055ed29b0b4SJens Axboe static void io_flush_cached_locked_reqs(struct io_ring_ctx *ctx,
1056ed29b0b4SJens Axboe 					struct io_submit_state *state)
1057ed29b0b4SJens Axboe {
1058ed29b0b4SJens Axboe 	spin_lock(&ctx->completion_lock);
1059ed29b0b4SJens Axboe 	wq_list_splice(&ctx->locked_free_list, &state->free_list);
1060ed29b0b4SJens Axboe 	ctx->locked_free_nr = 0;
1061ed29b0b4SJens Axboe 	spin_unlock(&ctx->completion_lock);
1062ed29b0b4SJens Axboe }
1063ed29b0b4SJens Axboe 
1064ed29b0b4SJens Axboe /*
1065ed29b0b4SJens Axboe  * A request might get retired back into the request caches even before opcode
1066ed29b0b4SJens Axboe  * handlers and io_issue_sqe() are done with it, e.g. inline completion path.
1067ed29b0b4SJens Axboe  * Because of that, io_alloc_req() should be called only under ->uring_lock
1068ed29b0b4SJens Axboe  * and with extra caution to not get a request that is still worked on.
1069ed29b0b4SJens Axboe  */
1070bd1a3783SPavel Begunkov __cold bool __io_alloc_req_refill(struct io_ring_ctx *ctx)
1071ed29b0b4SJens Axboe 	__must_hold(&ctx->uring_lock)
1072ed29b0b4SJens Axboe {
1073ed29b0b4SJens Axboe 	gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
1074ed29b0b4SJens Axboe 	void *reqs[IO_REQ_ALLOC_BATCH];
1075ed29b0b4SJens Axboe 	int ret, i;
1076ed29b0b4SJens Axboe 
1077ed29b0b4SJens Axboe 	/*
1078ed29b0b4SJens Axboe 	 * If we have more than a batch's worth of requests in our IRQ side
1079ed29b0b4SJens Axboe 	 * locked cache, grab the lock and move them over to our submission
1080ed29b0b4SJens Axboe 	 * side cache.
1081ed29b0b4SJens Axboe 	 */
1082ed29b0b4SJens Axboe 	if (data_race(ctx->locked_free_nr) > IO_COMPL_BATCH) {
1083ed29b0b4SJens Axboe 		io_flush_cached_locked_reqs(ctx, &ctx->submit_state);
1084ed29b0b4SJens Axboe 		if (!io_req_cache_empty(ctx))
1085ed29b0b4SJens Axboe 			return true;
1086ed29b0b4SJens Axboe 	}
1087ed29b0b4SJens Axboe 
1088ed29b0b4SJens Axboe 	ret = kmem_cache_alloc_bulk(req_cachep, gfp, ARRAY_SIZE(reqs), reqs);
1089ed29b0b4SJens Axboe 
1090ed29b0b4SJens Axboe 	/*
1091ed29b0b4SJens Axboe 	 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1092ed29b0b4SJens Axboe 	 * retry single alloc to be on the safe side.
1093ed29b0b4SJens Axboe 	 */
1094ed29b0b4SJens Axboe 	if (unlikely(ret <= 0)) {
1095ed29b0b4SJens Axboe 		reqs[0] = kmem_cache_alloc(req_cachep, gfp);
1096ed29b0b4SJens Axboe 		if (!reqs[0])
1097ed29b0b4SJens Axboe 			return false;
1098ed29b0b4SJens Axboe 		ret = 1;
1099ed29b0b4SJens Axboe 	}
1100ed29b0b4SJens Axboe 
1101ed29b0b4SJens Axboe 	percpu_ref_get_many(&ctx->refs, ret);
1102ed29b0b4SJens Axboe 	for (i = 0; i < ret; i++) {
1103ed29b0b4SJens Axboe 		struct io_kiocb *req = reqs[i];
1104ed29b0b4SJens Axboe 
1105ed29b0b4SJens Axboe 		io_preinit_req(req, ctx);
1106ed29b0b4SJens Axboe 		io_req_add_to_cache(req, ctx);
1107ed29b0b4SJens Axboe 	}
1108ed29b0b4SJens Axboe 	return true;
1109ed29b0b4SJens Axboe }
1110ed29b0b4SJens Axboe 
1111ed29b0b4SJens Axboe static inline void io_dismantle_req(struct io_kiocb *req)
1112ed29b0b4SJens Axboe {
1113ed29b0b4SJens Axboe 	unsigned int flags = req->flags;
1114ed29b0b4SJens Axboe 
1115ed29b0b4SJens Axboe 	if (unlikely(flags & IO_REQ_CLEAN_FLAGS))
1116ed29b0b4SJens Axboe 		io_clean_op(req);
1117ed29b0b4SJens Axboe 	if (!(flags & REQ_F_FIXED_FILE))
1118ed29b0b4SJens Axboe 		io_put_file(req->file);
1119ed29b0b4SJens Axboe }
1120ed29b0b4SJens Axboe 
112103adabe8SPavel Begunkov static __cold void io_free_req_tw(struct io_kiocb *req, struct io_tw_state *ts)
1122ed29b0b4SJens Axboe {
1123ed29b0b4SJens Axboe 	struct io_ring_ctx *ctx = req->ctx;
1124ed29b0b4SJens Axboe 
1125ef8ae64fSPavel Begunkov 	if (req->rsrc_node) {
1126ef8ae64fSPavel Begunkov 		io_tw_lock(ctx, ts);
11271f2c8f61SPavel Begunkov 		io_put_rsrc_node(ctx, req->rsrc_node);
1128ef8ae64fSPavel Begunkov 	}
1129ed29b0b4SJens Axboe 	io_dismantle_req(req);
11305afa4650SPavel Begunkov 	io_put_task_remote(req->task, 1);
1131ed29b0b4SJens Axboe 
1132ed29b0b4SJens Axboe 	spin_lock(&ctx->completion_lock);
1133ed29b0b4SJens Axboe 	wq_list_add_head(&req->comp_list, &ctx->locked_free_list);
1134ed29b0b4SJens Axboe 	ctx->locked_free_nr++;
1135ed29b0b4SJens Axboe 	spin_unlock(&ctx->completion_lock);
1136ed29b0b4SJens Axboe }
1137ed29b0b4SJens Axboe 
113803adabe8SPavel Begunkov __cold void io_free_req(struct io_kiocb *req)
113903adabe8SPavel Begunkov {
114003adabe8SPavel Begunkov 	req->io_task_work.func = io_free_req_tw;
114103adabe8SPavel Begunkov 	io_req_task_work_add(req);
114203adabe8SPavel Begunkov }
114303adabe8SPavel Begunkov 
1144ed29b0b4SJens Axboe static void __io_req_find_next_prep(struct io_kiocb *req)
1145ed29b0b4SJens Axboe {
1146ed29b0b4SJens Axboe 	struct io_ring_ctx *ctx = req->ctx;
1147ed29b0b4SJens Axboe 
11486971253fSPavel Begunkov 	spin_lock(&ctx->completion_lock);
1149305bef98SPavel Begunkov 	io_disarm_next(req);
11506971253fSPavel Begunkov 	spin_unlock(&ctx->completion_lock);
1151ed29b0b4SJens Axboe }
1152ed29b0b4SJens Axboe 
1153ed29b0b4SJens Axboe static inline struct io_kiocb *io_req_find_next(struct io_kiocb *req)
1154ed29b0b4SJens Axboe {
1155ed29b0b4SJens Axboe 	struct io_kiocb *nxt;
1156ed29b0b4SJens Axboe 
1157ed29b0b4SJens Axboe 	/*
1158ed29b0b4SJens Axboe 	 * If LINK is set, we have dependent requests in this chain. If we
1159ed29b0b4SJens Axboe 	 * didn't fail this request, queue the first one up, moving any other
1160ed29b0b4SJens Axboe 	 * dependencies to the next request. In case of failure, fail the rest
1161ed29b0b4SJens Axboe 	 * of the chain.
1162ed29b0b4SJens Axboe 	 */
1163ed29b0b4SJens Axboe 	if (unlikely(req->flags & IO_DISARM_MASK))
1164ed29b0b4SJens Axboe 		__io_req_find_next_prep(req);
1165ed29b0b4SJens Axboe 	nxt = req->link;
1166ed29b0b4SJens Axboe 	req->link = NULL;
1167ed29b0b4SJens Axboe 	return nxt;
1168ed29b0b4SJens Axboe }
1169ed29b0b4SJens Axboe 
1170a282967cSPavel Begunkov static void ctx_flush_and_put(struct io_ring_ctx *ctx, struct io_tw_state *ts)
1171ed29b0b4SJens Axboe {
1172ed29b0b4SJens Axboe 	if (!ctx)
1173ed29b0b4SJens Axboe 		return;
1174ed29b0b4SJens Axboe 	if (ctx->flags & IORING_SETUP_TASKRUN_FLAG)
1175ed29b0b4SJens Axboe 		atomic_andnot(IORING_SQ_TASKRUN, &ctx->rings->sq_flags);
1176a282967cSPavel Begunkov 	if (ts->locked) {
1177ed29b0b4SJens Axboe 		io_submit_flush_completions(ctx);
1178ed29b0b4SJens Axboe 		mutex_unlock(&ctx->uring_lock);
1179a282967cSPavel Begunkov 		ts->locked = false;
1180ed29b0b4SJens Axboe 	}
1181ed29b0b4SJens Axboe 	percpu_ref_put(&ctx->refs);
1182ed29b0b4SJens Axboe }
1183ed29b0b4SJens Axboe 
1184c6dd763cSDylan Yudaken static unsigned int handle_tw_list(struct llist_node *node,
1185a282967cSPavel Begunkov 				   struct io_ring_ctx **ctx,
1186a282967cSPavel Begunkov 				   struct io_tw_state *ts,
11873a0c037bSDylan Yudaken 				   struct llist_node *last)
1188ed29b0b4SJens Axboe {
1189c6dd763cSDylan Yudaken 	unsigned int count = 0;
1190c6dd763cSDylan Yudaken 
1191cb6bf7f2SPavel Begunkov 	while (node && node != last) {
1192f88262e6SDylan Yudaken 		struct llist_node *next = node->next;
1193ed29b0b4SJens Axboe 		struct io_kiocb *req = container_of(node, struct io_kiocb,
1194ed29b0b4SJens Axboe 						    io_task_work.node);
1195ed29b0b4SJens Axboe 
1196ed29b0b4SJens Axboe 		prefetch(container_of(next, struct io_kiocb, io_task_work.node));
1197ed29b0b4SJens Axboe 
1198ed29b0b4SJens Axboe 		if (req->ctx != *ctx) {
1199a282967cSPavel Begunkov 			ctx_flush_and_put(*ctx, ts);
1200ed29b0b4SJens Axboe 			*ctx = req->ctx;
1201ed29b0b4SJens Axboe 			/* if not contended, grab and improve batching */
1202a282967cSPavel Begunkov 			ts->locked = mutex_trylock(&(*ctx)->uring_lock);
1203ed29b0b4SJens Axboe 			percpu_ref_get(&(*ctx)->refs);
120413bfa6f1SPavel Begunkov 		}
1205a282967cSPavel Begunkov 		req->io_task_work.func(req, ts);
1206ed29b0b4SJens Axboe 		node = next;
1207c6dd763cSDylan Yudaken 		count++;
1208f5868008SJens Axboe 		if (unlikely(need_resched())) {
1209a282967cSPavel Begunkov 			ctx_flush_and_put(*ctx, ts);
1210f5868008SJens Axboe 			*ctx = NULL;
1211f5868008SJens Axboe 			cond_resched();
1212f5868008SJens Axboe 		}
12133a0c037bSDylan Yudaken 	}
1214c6dd763cSDylan Yudaken 
1215c6dd763cSDylan Yudaken 	return count;
1216ed29b0b4SJens Axboe }
1217ed29b0b4SJens Axboe 
1218923d1592SDylan Yudaken /**
1219923d1592SDylan Yudaken  * io_llist_xchg - swap all entries in a lock-less list
1220923d1592SDylan Yudaken  * @head:	the head of lock-less list to delete all entries
1221923d1592SDylan Yudaken  * @new:	new entry as the head of the list
1222923d1592SDylan Yudaken  *
1223923d1592SDylan Yudaken  * If list is empty, return NULL, otherwise, return the pointer to the first entry.
1224923d1592SDylan Yudaken  * The order of entries returned is from the newest to the oldest added one.
1225923d1592SDylan Yudaken  */
1226923d1592SDylan Yudaken static inline struct llist_node *io_llist_xchg(struct llist_head *head,
1227923d1592SDylan Yudaken 					       struct llist_node *new)
1228923d1592SDylan Yudaken {
1229923d1592SDylan Yudaken 	return xchg(&head->first, new);
1230923d1592SDylan Yudaken }
1231923d1592SDylan Yudaken 
1232923d1592SDylan Yudaken /**
1233923d1592SDylan Yudaken  * io_llist_cmpxchg - possibly swap all entries in a lock-less list
1234923d1592SDylan Yudaken  * @head:	the head of lock-less list to delete all entries
1235923d1592SDylan Yudaken  * @old:	expected old value of the first entry of the list
1236923d1592SDylan Yudaken  * @new:	new entry as the head of the list
1237923d1592SDylan Yudaken  *
1238923d1592SDylan Yudaken  * perform a cmpxchg on the first entry of the list.
1239923d1592SDylan Yudaken  */
1240923d1592SDylan Yudaken 
1241923d1592SDylan Yudaken static inline struct llist_node *io_llist_cmpxchg(struct llist_head *head,
1242923d1592SDylan Yudaken 						  struct llist_node *old,
1243923d1592SDylan Yudaken 						  struct llist_node *new)
1244923d1592SDylan Yudaken {
1245923d1592SDylan Yudaken 	return cmpxchg(&head->first, old, new);
1246923d1592SDylan Yudaken }
1247923d1592SDylan Yudaken 
1248c9f06aa7SJens Axboe void tctx_task_work(struct callback_head *cb)
1249ed29b0b4SJens Axboe {
1250a282967cSPavel Begunkov 	struct io_tw_state ts = {};
1251ed29b0b4SJens Axboe 	struct io_ring_ctx *ctx = NULL;
1252ed29b0b4SJens Axboe 	struct io_uring_task *tctx = container_of(cb, struct io_uring_task,
1253ed29b0b4SJens Axboe 						  task_work);
12543a0c037bSDylan Yudaken 	struct llist_node fake = {};
125577e443abSPavel Begunkov 	struct llist_node *node;
1256cb6bf7f2SPavel Begunkov 	unsigned int loops = 0;
1257cb6bf7f2SPavel Begunkov 	unsigned int count = 0;
1258ed29b0b4SJens Axboe 
125977e443abSPavel Begunkov 	if (unlikely(current->flags & PF_EXITING)) {
126077e443abSPavel Begunkov 		io_fallback_tw(tctx);
126177e443abSPavel Begunkov 		return;
126277e443abSPavel Begunkov 	}
126377e443abSPavel Begunkov 
1264cb6bf7f2SPavel Begunkov 	do {
1265c6dd763cSDylan Yudaken 		loops++;
12663a0c037bSDylan Yudaken 		node = io_llist_xchg(&tctx->task_list, &fake);
1267a282967cSPavel Begunkov 		count += handle_tw_list(node, &ctx, &ts, &fake);
126850470fc5SPavel Begunkov 
126950470fc5SPavel Begunkov 		/* skip expensive cmpxchg if there are items in the list */
127050470fc5SPavel Begunkov 		if (READ_ONCE(tctx->task_list.first) != &fake)
127150470fc5SPavel Begunkov 			continue;
1272a282967cSPavel Begunkov 		if (ts.locked && !wq_list_empty(&ctx->submit_state.compl_reqs)) {
127350470fc5SPavel Begunkov 			io_submit_flush_completions(ctx);
127450470fc5SPavel Begunkov 			if (READ_ONCE(tctx->task_list.first) != &fake)
127550470fc5SPavel Begunkov 				continue;
127650470fc5SPavel Begunkov 		}
12773a0c037bSDylan Yudaken 		node = io_llist_cmpxchg(&tctx->task_list, &fake, NULL);
1278cb6bf7f2SPavel Begunkov 	} while (node != &fake);
1279ed29b0b4SJens Axboe 
1280a282967cSPavel Begunkov 	ctx_flush_and_put(ctx, &ts);
1281ed29b0b4SJens Axboe 
12828d664282SJens Axboe 	/* relaxed read is enough as only the task itself sets ->in_cancel */
12838d664282SJens Axboe 	if (unlikely(atomic_read(&tctx->in_cancel)))
1284ed29b0b4SJens Axboe 		io_uring_drop_tctx_refs(current);
1285c6dd763cSDylan Yudaken 
1286c6dd763cSDylan Yudaken 	trace_io_uring_task_work_run(tctx, count, loops);
1287ed29b0b4SJens Axboe }
1288ed29b0b4SJens Axboe 
1289d7593606SPavel Begunkov static __cold void io_fallback_tw(struct io_uring_task *tctx)
1290d7593606SPavel Begunkov {
1291d7593606SPavel Begunkov 	struct llist_node *node = llist_del_all(&tctx->task_list);
1292d7593606SPavel Begunkov 	struct io_kiocb *req;
1293d7593606SPavel Begunkov 
1294d7593606SPavel Begunkov 	while (node) {
1295d7593606SPavel Begunkov 		req = container_of(node, struct io_kiocb, io_task_work.node);
1296d7593606SPavel Begunkov 		node = node->next;
1297d7593606SPavel Begunkov 		if (llist_add(&req->io_task_work.node,
1298d7593606SPavel Begunkov 			      &req->ctx->fallback_llist))
1299d7593606SPavel Begunkov 			schedule_delayed_work(&req->ctx->fallback_work, 1);
1300d7593606SPavel Begunkov 	}
1301d7593606SPavel Begunkov }
1302d7593606SPavel Begunkov 
1303c0e0d6baSDylan Yudaken static void io_req_local_work_add(struct io_kiocb *req)
1304c0e0d6baSDylan Yudaken {
1305c0e0d6baSDylan Yudaken 	struct io_ring_ctx *ctx = req->ctx;
1306c0e0d6baSDylan Yudaken 
1307ce8e04f6SJens Axboe 	if (!llist_add(&req->io_task_work.node, &ctx->work_llist))
1308ab1c590fSPavel Begunkov 		return;
1309ce8e04f6SJens Axboe 
1310130bd686SPavel Begunkov 	/* needed for the following wake up */
1311fc86f9d3SPavel Begunkov 	smp_mb__after_atomic();
1312c0e0d6baSDylan Yudaken 
13138d664282SJens Axboe 	if (unlikely(atomic_read(&req->task->io_uring->in_cancel))) {
1314c0e0d6baSDylan Yudaken 		io_move_task_work_from_local(ctx);
1315ab1c590fSPavel Begunkov 		return;
1316c0e0d6baSDylan Yudaken 	}
1317c0e0d6baSDylan Yudaken 
1318c0e0d6baSDylan Yudaken 	if (ctx->flags & IORING_SETUP_TASKRUN_FLAG)
1319c0e0d6baSDylan Yudaken 		atomic_or(IORING_SQ_TASKRUN, &ctx->rings->sq_flags);
132021a091b9SDylan Yudaken 	if (ctx->has_evfd)
132121a091b9SDylan Yudaken 		io_eventfd_signal(ctx);
1322d80c0f00SPavel Begunkov 
1323d80c0f00SPavel Begunkov 	if (READ_ONCE(ctx->cq_waiting))
1324130bd686SPavel Begunkov 		wake_up_state(ctx->submitter_task, TASK_INTERRUPTIBLE);
1325c0e0d6baSDylan Yudaken }
1326c0e0d6baSDylan Yudaken 
1327*8501fe70SPavel Begunkov void __io_req_task_work_add(struct io_kiocb *req, unsigned flags)
1328ed29b0b4SJens Axboe {
1329c34398a8SDylan Yudaken 	struct io_uring_task *tctx = req->task->io_uring;
1330ed29b0b4SJens Axboe 	struct io_ring_ctx *ctx = req->ctx;
1331ed29b0b4SJens Axboe 
1332*8501fe70SPavel Begunkov 	if (!(flags & IOU_F_TWQ_FORCE_NORMAL) &&
1333*8501fe70SPavel Begunkov 	    (ctx->flags & IORING_SETUP_DEFER_TASKRUN)) {
1334d73a572dSPavel Begunkov 		rcu_read_lock();
1335c0e0d6baSDylan Yudaken 		io_req_local_work_add(req);
1336d73a572dSPavel Begunkov 		rcu_read_unlock();
1337c0e0d6baSDylan Yudaken 		return;
1338c0e0d6baSDylan Yudaken 	}
1339ed29b0b4SJens Axboe 
1340ed29b0b4SJens Axboe 	/* task_work already pending, we're done */
134132d91f05SDylan Yudaken 	if (!llist_add(&req->io_task_work.node, &tctx->task_list))
1342ed29b0b4SJens Axboe 		return;
1343ed29b0b4SJens Axboe 
1344ed29b0b4SJens Axboe 	if (ctx->flags & IORING_SETUP_TASKRUN_FLAG)
1345ed29b0b4SJens Axboe 		atomic_or(IORING_SQ_TASKRUN, &ctx->rings->sq_flags);
1346ed29b0b4SJens Axboe 
1347ed29b0b4SJens Axboe 	if (likely(!task_work_add(req->task, &tctx->task_work, ctx->notify_method)))
1348ed29b0b4SJens Axboe 		return;
1349ed29b0b4SJens Axboe 
1350d7593606SPavel Begunkov 	io_fallback_tw(tctx);
1351c0e0d6baSDylan Yudaken }
1352c0e0d6baSDylan Yudaken 
1353c0e0d6baSDylan Yudaken static void __cold io_move_task_work_from_local(struct io_ring_ctx *ctx)
1354c0e0d6baSDylan Yudaken {
1355c0e0d6baSDylan Yudaken 	struct llist_node *node;
1356c0e0d6baSDylan Yudaken 
1357c0e0d6baSDylan Yudaken 	node = llist_del_all(&ctx->work_llist);
1358c0e0d6baSDylan Yudaken 	while (node) {
1359c0e0d6baSDylan Yudaken 		struct io_kiocb *req = container_of(node, struct io_kiocb,
1360c0e0d6baSDylan Yudaken 						    io_task_work.node);
1361c0e0d6baSDylan Yudaken 
1362c0e0d6baSDylan Yudaken 		node = node->next;
1363*8501fe70SPavel Begunkov 		__io_req_task_work_add(req, IOU_F_TWQ_FORCE_NORMAL);
1364c0e0d6baSDylan Yudaken 	}
1365c0e0d6baSDylan Yudaken }
1366c0e0d6baSDylan Yudaken 
1367a282967cSPavel Begunkov static int __io_run_local_work(struct io_ring_ctx *ctx, struct io_tw_state *ts)
1368c0e0d6baSDylan Yudaken {
1369c0e0d6baSDylan Yudaken 	struct llist_node *node;
1370c3f4d39eSPavel Begunkov 	unsigned int loops = 0;
1371140102aeSPavel Begunkov 	int ret = 0;
1372c0e0d6baSDylan Yudaken 
1373140102aeSPavel Begunkov 	if (WARN_ON_ONCE(ctx->submitter_task != current))
1374c0e0d6baSDylan Yudaken 		return -EEXIST;
1375c3f4d39eSPavel Begunkov 	if (ctx->flags & IORING_SETUP_TASKRUN_FLAG)
1376c3f4d39eSPavel Begunkov 		atomic_andnot(IORING_SQ_TASKRUN, &ctx->rings->sq_flags);
1377c0e0d6baSDylan Yudaken again:
1378c3f4d39eSPavel Begunkov 	node = io_llist_xchg(&ctx->work_llist, NULL);
1379c3f4d39eSPavel Begunkov 	while (node) {
1380c0e0d6baSDylan Yudaken 		struct llist_node *next = node->next;
1381c0e0d6baSDylan Yudaken 		struct io_kiocb *req = container_of(node, struct io_kiocb,
1382c0e0d6baSDylan Yudaken 						    io_task_work.node);
1383c0e0d6baSDylan Yudaken 		prefetch(container_of(next, struct io_kiocb, io_task_work.node));
1384a282967cSPavel Begunkov 		req->io_task_work.func(req, ts);
1385c0e0d6baSDylan Yudaken 		ret++;
1386c0e0d6baSDylan Yudaken 		node = next;
1387c0e0d6baSDylan Yudaken 	}
1388f75d5036SDylan Yudaken 	loops++;
1389c0e0d6baSDylan Yudaken 
1390c3f4d39eSPavel Begunkov 	if (!llist_empty(&ctx->work_llist))
1391c3f4d39eSPavel Begunkov 		goto again;
1392a282967cSPavel Begunkov 	if (ts->locked) {
1393c0e0d6baSDylan Yudaken 		io_submit_flush_completions(ctx);
1394b0b7a7d2SPavel Begunkov 		if (!llist_empty(&ctx->work_llist))
1395b0b7a7d2SPavel Begunkov 			goto again;
1396b0b7a7d2SPavel Begunkov 	}
1397f75d5036SDylan Yudaken 	trace_io_uring_local_work_run(ctx, ret, loops);
1398c0e0d6baSDylan Yudaken 	return ret;
13998ac5d85aSJens Axboe }
14008ac5d85aSJens Axboe 
1401360173abSPavel Begunkov static inline int io_run_local_work_locked(struct io_ring_ctx *ctx)
1402360173abSPavel Begunkov {
1403a282967cSPavel Begunkov 	struct io_tw_state ts = { .locked = true, };
1404360173abSPavel Begunkov 	int ret;
1405360173abSPavel Begunkov 
1406360173abSPavel Begunkov 	if (llist_empty(&ctx->work_llist))
1407360173abSPavel Begunkov 		return 0;
1408360173abSPavel Begunkov 
1409a282967cSPavel Begunkov 	ret = __io_run_local_work(ctx, &ts);
1410360173abSPavel Begunkov 	/* shouldn't happen! */
1411a282967cSPavel Begunkov 	if (WARN_ON_ONCE(!ts.locked))
1412360173abSPavel Begunkov 		mutex_lock(&ctx->uring_lock);
1413360173abSPavel Begunkov 	return ret;
1414360173abSPavel Begunkov }
1415360173abSPavel Begunkov 
14163e565555SPavel Begunkov static int io_run_local_work(struct io_ring_ctx *ctx)
14178ac5d85aSJens Axboe {
1418a282967cSPavel Begunkov 	struct io_tw_state ts = {};
14198ac5d85aSJens Axboe 	int ret;
14208ac5d85aSJens Axboe 
1421a282967cSPavel Begunkov 	ts.locked = mutex_trylock(&ctx->uring_lock);
1422a282967cSPavel Begunkov 	ret = __io_run_local_work(ctx, &ts);
1423a282967cSPavel Begunkov 	if (ts.locked)
14248ac5d85aSJens Axboe 		mutex_unlock(&ctx->uring_lock);
14258ac5d85aSJens Axboe 
14268ac5d85aSJens Axboe 	return ret;
1427c0e0d6baSDylan Yudaken }
1428c0e0d6baSDylan Yudaken 
1429a282967cSPavel Begunkov static void io_req_task_cancel(struct io_kiocb *req, struct io_tw_state *ts)
1430ed29b0b4SJens Axboe {
1431a282967cSPavel Begunkov 	io_tw_lock(req->ctx, ts);
1432973fc83fSDylan Yudaken 	io_req_defer_failed(req, req->cqe.res);
1433ed29b0b4SJens Axboe }
1434ed29b0b4SJens Axboe 
1435a282967cSPavel Begunkov void io_req_task_submit(struct io_kiocb *req, struct io_tw_state *ts)
1436ed29b0b4SJens Axboe {
1437a282967cSPavel Begunkov 	io_tw_lock(req->ctx, ts);
1438ed29b0b4SJens Axboe 	/* req->task == current here, checking PF_EXITING is safe */
14396bb30855SDylan Yudaken 	if (unlikely(req->task->flags & PF_EXITING))
1440973fc83fSDylan Yudaken 		io_req_defer_failed(req, -EFAULT);
14416bb30855SDylan Yudaken 	else if (req->flags & REQ_F_FORCE_ASYNC)
1442a282967cSPavel Begunkov 		io_queue_iowq(req, ts);
14436bb30855SDylan Yudaken 	else
14446bb30855SDylan Yudaken 		io_queue_sqe(req);
1445ed29b0b4SJens Axboe }
1446ed29b0b4SJens Axboe 
144759915143SJens Axboe void io_req_task_queue_fail(struct io_kiocb *req, int ret)
1448ed29b0b4SJens Axboe {
144997b388d7SJens Axboe 	io_req_set_res(req, ret, 0);
1450ed29b0b4SJens Axboe 	req->io_task_work.func = io_req_task_cancel;
1451ed29b0b4SJens Axboe 	io_req_task_work_add(req);
1452ed29b0b4SJens Axboe }
1453ed29b0b4SJens Axboe 
1454f3b44f92SJens Axboe void io_req_task_queue(struct io_kiocb *req)
1455ed29b0b4SJens Axboe {
1456ed29b0b4SJens Axboe 	req->io_task_work.func = io_req_task_submit;
1457ed29b0b4SJens Axboe 	io_req_task_work_add(req);
1458ed29b0b4SJens Axboe }
1459ed29b0b4SJens Axboe 
146059915143SJens Axboe void io_queue_next(struct io_kiocb *req)
1461ed29b0b4SJens Axboe {
1462ed29b0b4SJens Axboe 	struct io_kiocb *nxt = io_req_find_next(req);
1463ed29b0b4SJens Axboe 
1464ed29b0b4SJens Axboe 	if (nxt)
1465ed29b0b4SJens Axboe 		io_req_task_queue(nxt);
1466ed29b0b4SJens Axboe }
1467ed29b0b4SJens Axboe 
1468f3b44f92SJens Axboe void io_free_batch_list(struct io_ring_ctx *ctx, struct io_wq_work_node *node)
1469ed29b0b4SJens Axboe 	__must_hold(&ctx->uring_lock)
1470ed29b0b4SJens Axboe {
1471ed29b0b4SJens Axboe 	struct task_struct *task = NULL;
1472ed29b0b4SJens Axboe 	int task_refs = 0;
1473ed29b0b4SJens Axboe 
1474ed29b0b4SJens Axboe 	do {
1475ed29b0b4SJens Axboe 		struct io_kiocb *req = container_of(node, struct io_kiocb,
1476ed29b0b4SJens Axboe 						    comp_list);
1477ed29b0b4SJens Axboe 
1478ed29b0b4SJens Axboe 		if (unlikely(req->flags & IO_REQ_CLEAN_SLOW_FLAGS)) {
1479ed29b0b4SJens Axboe 			if (req->flags & REQ_F_REFCOUNT) {
1480ed29b0b4SJens Axboe 				node = req->comp_list.next;
1481ed29b0b4SJens Axboe 				if (!req_ref_put_and_test(req))
1482ed29b0b4SJens Axboe 					continue;
1483ed29b0b4SJens Axboe 			}
1484ed29b0b4SJens Axboe 			if ((req->flags & REQ_F_POLLED) && req->apoll) {
1485ed29b0b4SJens Axboe 				struct async_poll *apoll = req->apoll;
1486ed29b0b4SJens Axboe 
1487ed29b0b4SJens Axboe 				if (apoll->double_poll)
1488ed29b0b4SJens Axboe 					kfree(apoll->double_poll);
14899731bc98SJens Axboe 				if (!io_alloc_cache_put(&ctx->apoll_cache, &apoll->cache))
14909731bc98SJens Axboe 					kfree(apoll);
1491ed29b0b4SJens Axboe 				req->flags &= ~REQ_F_POLLED;
1492ed29b0b4SJens Axboe 			}
1493ed29b0b4SJens Axboe 			if (req->flags & IO_REQ_LINK_FLAGS)
1494ed29b0b4SJens Axboe 				io_queue_next(req);
1495ed29b0b4SJens Axboe 			if (unlikely(req->flags & IO_REQ_CLEAN_FLAGS))
1496ed29b0b4SJens Axboe 				io_clean_op(req);
1497ed29b0b4SJens Axboe 		}
1498ed29b0b4SJens Axboe 		if (!(req->flags & REQ_F_FIXED_FILE))
1499ed29b0b4SJens Axboe 			io_put_file(req->file);
1500ed29b0b4SJens Axboe 
1501ed29b0b4SJens Axboe 		io_req_put_rsrc_locked(req, ctx);
1502ed29b0b4SJens Axboe 
1503ed29b0b4SJens Axboe 		if (req->task != task) {
1504ed29b0b4SJens Axboe 			if (task)
1505ed29b0b4SJens Axboe 				io_put_task(task, task_refs);
1506ed29b0b4SJens Axboe 			task = req->task;
1507ed29b0b4SJens Axboe 			task_refs = 0;
1508ed29b0b4SJens Axboe 		}
1509ed29b0b4SJens Axboe 		task_refs++;
1510ed29b0b4SJens Axboe 		node = req->comp_list.next;
1511ed29b0b4SJens Axboe 		io_req_add_to_cache(req, ctx);
1512ed29b0b4SJens Axboe 	} while (node);
1513ed29b0b4SJens Axboe 
1514ed29b0b4SJens Axboe 	if (task)
1515ed29b0b4SJens Axboe 		io_put_task(task, task_refs);
1516ed29b0b4SJens Axboe }
1517ed29b0b4SJens Axboe 
1518ed29b0b4SJens Axboe static void __io_submit_flush_completions(struct io_ring_ctx *ctx)
1519ed29b0b4SJens Axboe 	__must_hold(&ctx->uring_lock)
1520ed29b0b4SJens Axboe {
1521ed29b0b4SJens Axboe 	struct io_submit_state *state = &ctx->submit_state;
1522fa780334SJens Axboe 	struct io_wq_work_node *node;
1523ed29b0b4SJens Axboe 
1524f66f7342SPavel Begunkov 	__io_cq_lock(ctx);
1525931147ddSDylan Yudaken 	/* must come first to preserve CQE ordering in failure cases */
1526931147ddSDylan Yudaken 	if (state->cqes_count)
1527931147ddSDylan Yudaken 		__io_flush_post_cqes(ctx);
1528fa780334SJens Axboe 	__wq_list_for_each(node, &state->compl_reqs) {
1529ed29b0b4SJens Axboe 		struct io_kiocb *req = container_of(node, struct io_kiocb,
1530ed29b0b4SJens Axboe 					    comp_list);
1531ed29b0b4SJens Axboe 
1532f66f7342SPavel Begunkov 		if (!(req->flags & REQ_F_CQE_SKIP) &&
1533f66f7342SPavel Begunkov 		    unlikely(!__io_fill_cqe_req(ctx, req))) {
1534f66f7342SPavel Begunkov 			if (ctx->task_complete) {
1535f66f7342SPavel Begunkov 				spin_lock(&ctx->completion_lock);
1536f66f7342SPavel Begunkov 				io_req_cqe_overflow(req);
1537f66f7342SPavel Begunkov 				spin_unlock(&ctx->completion_lock);
1538f66f7342SPavel Begunkov 			} else {
1539f66f7342SPavel Begunkov 				io_req_cqe_overflow(req);
1540ed29b0b4SJens Axboe 			}
1541f66f7342SPavel Begunkov 		}
1542f66f7342SPavel Begunkov 	}
15433181e22fSPavel Begunkov 	__io_cq_unlock_post_flush(ctx);
1544ed29b0b4SJens Axboe 
1545931147ddSDylan Yudaken 	if (!wq_list_empty(&ctx->submit_state.compl_reqs)) {
1546ed29b0b4SJens Axboe 		io_free_batch_list(ctx, state->compl_reqs.first);
1547ed29b0b4SJens Axboe 		INIT_WQ_LIST(&state->compl_reqs);
1548ed29b0b4SJens Axboe 	}
1549931147ddSDylan Yudaken }
1550ed29b0b4SJens Axboe 
1551ed29b0b4SJens Axboe /*
1552ed29b0b4SJens Axboe  * Drop reference to request, return next in chain (if there is one) if this
1553ed29b0b4SJens Axboe  * was the last reference to this request.
1554ed29b0b4SJens Axboe  */
1555ed29b0b4SJens Axboe static inline struct io_kiocb *io_put_req_find_next(struct io_kiocb *req)
1556ed29b0b4SJens Axboe {
1557ed29b0b4SJens Axboe 	struct io_kiocb *nxt = NULL;
1558ed29b0b4SJens Axboe 
1559ed29b0b4SJens Axboe 	if (req_ref_put_and_test(req)) {
1560ed29b0b4SJens Axboe 		if (unlikely(req->flags & IO_REQ_LINK_FLAGS))
1561ed29b0b4SJens Axboe 			nxt = io_req_find_next(req);
1562ed29b0b4SJens Axboe 		io_free_req(req);
1563ed29b0b4SJens Axboe 	}
1564ed29b0b4SJens Axboe 	return nxt;
1565ed29b0b4SJens Axboe }
1566ed29b0b4SJens Axboe 
1567ed29b0b4SJens Axboe static unsigned io_cqring_events(struct io_ring_ctx *ctx)
1568ed29b0b4SJens Axboe {
1569ed29b0b4SJens Axboe 	/* See comment at the top of this file */
1570ed29b0b4SJens Axboe 	smp_rmb();
1571ed29b0b4SJens Axboe 	return __io_cqring_events(ctx);
1572ed29b0b4SJens Axboe }
1573ed29b0b4SJens Axboe 
1574ed29b0b4SJens Axboe /*
1575ed29b0b4SJens Axboe  * We can't just wait for polled events to come to us, we have to actively
1576ed29b0b4SJens Axboe  * find and complete them.
1577ed29b0b4SJens Axboe  */
1578ed29b0b4SJens Axboe static __cold void io_iopoll_try_reap_events(struct io_ring_ctx *ctx)
1579ed29b0b4SJens Axboe {
1580ed29b0b4SJens Axboe 	if (!(ctx->flags & IORING_SETUP_IOPOLL))
1581ed29b0b4SJens Axboe 		return;
1582ed29b0b4SJens Axboe 
1583ed29b0b4SJens Axboe 	mutex_lock(&ctx->uring_lock);
1584ed29b0b4SJens Axboe 	while (!wq_list_empty(&ctx->iopoll_list)) {
1585ed29b0b4SJens Axboe 		/* let it sleep and repeat later if can't complete a request */
1586ed29b0b4SJens Axboe 		if (io_do_iopoll(ctx, true) == 0)
1587ed29b0b4SJens Axboe 			break;
1588ed29b0b4SJens Axboe 		/*
1589ed29b0b4SJens Axboe 		 * Ensure we allow local-to-the-cpu processing to take place,
1590ed29b0b4SJens Axboe 		 * in this case we need to ensure that we reap all events.
1591ed29b0b4SJens Axboe 		 * Also let task_work, etc. to progress by releasing the mutex
1592ed29b0b4SJens Axboe 		 */
1593ed29b0b4SJens Axboe 		if (need_resched()) {
1594ed29b0b4SJens Axboe 			mutex_unlock(&ctx->uring_lock);
1595ed29b0b4SJens Axboe 			cond_resched();
1596ed29b0b4SJens Axboe 			mutex_lock(&ctx->uring_lock);
1597ed29b0b4SJens Axboe 		}
1598ed29b0b4SJens Axboe 	}
1599ed29b0b4SJens Axboe 	mutex_unlock(&ctx->uring_lock);
1600ed29b0b4SJens Axboe }
1601ed29b0b4SJens Axboe 
1602ed29b0b4SJens Axboe static int io_iopoll_check(struct io_ring_ctx *ctx, long min)
1603ed29b0b4SJens Axboe {
1604ed29b0b4SJens Axboe 	unsigned int nr_events = 0;
1605ed29b0b4SJens Axboe 	int ret = 0;
1606ed29b0b4SJens Axboe 	unsigned long check_cq;
1607ed29b0b4SJens Axboe 
160876de6749SPavel Begunkov 	if (!io_allowed_run_tw(ctx))
160976de6749SPavel Begunkov 		return -EEXIST;
161076de6749SPavel Begunkov 
16113a08576bSPavel Begunkov 	check_cq = READ_ONCE(ctx->check_cq);
16123a08576bSPavel Begunkov 	if (unlikely(check_cq)) {
16133a08576bSPavel Begunkov 		if (check_cq & BIT(IO_CHECK_CQ_OVERFLOW_BIT))
1614a85381d8SPavel Begunkov 			__io_cqring_overflow_flush(ctx);
16153a08576bSPavel Begunkov 		/*
16163a08576bSPavel Begunkov 		 * Similarly do not spin if we have not informed the user of any
16173a08576bSPavel Begunkov 		 * dropped CQE.
16183a08576bSPavel Begunkov 		 */
16193a08576bSPavel Begunkov 		if (check_cq & BIT(IO_CHECK_CQ_DROPPED_BIT))
16203a08576bSPavel Begunkov 			return -EBADR;
16213a08576bSPavel Begunkov 	}
1622ed29b0b4SJens Axboe 	/*
1623ed29b0b4SJens Axboe 	 * Don't enter poll loop if we already have events pending.
1624ed29b0b4SJens Axboe 	 * If we do, we can potentially be spinning for commands that
1625ed29b0b4SJens Axboe 	 * already triggered a CQE (eg in error).
1626ed29b0b4SJens Axboe 	 */
1627ed29b0b4SJens Axboe 	if (io_cqring_events(ctx))
1628ed29b0b4SJens Axboe 		return 0;
1629ed29b0b4SJens Axboe 
1630ed29b0b4SJens Axboe 	do {
1631ed29b0b4SJens Axboe 		/*
1632ed29b0b4SJens Axboe 		 * If a submit got punted to a workqueue, we can have the
1633ed29b0b4SJens Axboe 		 * application entering polling for a command before it gets
1634ed29b0b4SJens Axboe 		 * issued. That app will hold the uring_lock for the duration
1635ed29b0b4SJens Axboe 		 * of the poll right here, so we need to take a breather every
1636ed29b0b4SJens Axboe 		 * now and then to ensure that the issue has a chance to add
1637ed29b0b4SJens Axboe 		 * the poll to the issued list. Otherwise we can spin here
1638ed29b0b4SJens Axboe 		 * forever, while the workqueue is stuck trying to acquire the
1639ed29b0b4SJens Axboe 		 * very same mutex.
1640ed29b0b4SJens Axboe 		 */
1641dac6a0eaSJens Axboe 		if (wq_list_empty(&ctx->iopoll_list) ||
1642dac6a0eaSJens Axboe 		    io_task_work_pending(ctx)) {
1643ed29b0b4SJens Axboe 			u32 tail = ctx->cached_cq_tail;
1644ed29b0b4SJens Axboe 
16458de11cdcSDylan Yudaken 			(void) io_run_local_work_locked(ctx);
16461f8d5bbeSPavel Begunkov 
16471f8d5bbeSPavel Begunkov 			if (task_work_pending(current) ||
16481f8d5bbeSPavel Begunkov 			    wq_list_empty(&ctx->iopoll_list)) {
1649ed29b0b4SJens Axboe 				mutex_unlock(&ctx->uring_lock);
1650ed29b0b4SJens Axboe 				io_run_task_work();
1651ed29b0b4SJens Axboe 				mutex_lock(&ctx->uring_lock);
16521f8d5bbeSPavel Begunkov 			}
1653ed29b0b4SJens Axboe 			/* some requests don't go through iopoll_list */
1654ed29b0b4SJens Axboe 			if (tail != ctx->cached_cq_tail ||
1655ed29b0b4SJens Axboe 			    wq_list_empty(&ctx->iopoll_list))
1656ed29b0b4SJens Axboe 				break;
1657ed29b0b4SJens Axboe 		}
1658ed29b0b4SJens Axboe 		ret = io_do_iopoll(ctx, !min);
1659ed29b0b4SJens Axboe 		if (ret < 0)
1660ed29b0b4SJens Axboe 			break;
1661ed29b0b4SJens Axboe 		nr_events += ret;
1662ed29b0b4SJens Axboe 		ret = 0;
1663ed29b0b4SJens Axboe 	} while (nr_events < min && !need_resched());
1664ed29b0b4SJens Axboe 
1665ed29b0b4SJens Axboe 	return ret;
1666ed29b0b4SJens Axboe }
16677012c815SPavel Begunkov 
1668a282967cSPavel Begunkov void io_req_task_complete(struct io_kiocb *req, struct io_tw_state *ts)
1669ed29b0b4SJens Axboe {
1670a282967cSPavel Begunkov 	if (ts->locked)
16719da070b1SPavel Begunkov 		io_req_complete_defer(req);
16727012c815SPavel Begunkov 	else
167327f35fe9SDylan Yudaken 		io_req_complete_post(req, IO_URING_F_UNLOCKED);
1674ed29b0b4SJens Axboe }
1675ed29b0b4SJens Axboe 
1676ed29b0b4SJens Axboe /*
1677ed29b0b4SJens Axboe  * After the iocb has been issued, it's safe to be found on the poll list.
1678ed29b0b4SJens Axboe  * Adding the kiocb to the list AFTER submission ensures that we don't
1679ed29b0b4SJens Axboe  * find it from a io_do_iopoll() thread before the issuer is done
1680ed29b0b4SJens Axboe  * accessing the kiocb cookie.
1681ed29b0b4SJens Axboe  */
1682ed29b0b4SJens Axboe static void io_iopoll_req_issued(struct io_kiocb *req, unsigned int issue_flags)
1683ed29b0b4SJens Axboe {
1684ed29b0b4SJens Axboe 	struct io_ring_ctx *ctx = req->ctx;
1685ed29b0b4SJens Axboe 	const bool needs_lock = issue_flags & IO_URING_F_UNLOCKED;
1686ed29b0b4SJens Axboe 
1687ed29b0b4SJens Axboe 	/* workqueue context doesn't hold uring_lock, grab it now */
1688ed29b0b4SJens Axboe 	if (unlikely(needs_lock))
1689ed29b0b4SJens Axboe 		mutex_lock(&ctx->uring_lock);
1690ed29b0b4SJens Axboe 
1691ed29b0b4SJens Axboe 	/*
1692ed29b0b4SJens Axboe 	 * Track whether we have multiple files in our lists. This will impact
1693ed29b0b4SJens Axboe 	 * how we do polling eventually, not spinning if we're on potentially
1694ed29b0b4SJens Axboe 	 * different devices.
1695ed29b0b4SJens Axboe 	 */
1696ed29b0b4SJens Axboe 	if (wq_list_empty(&ctx->iopoll_list)) {
1697ed29b0b4SJens Axboe 		ctx->poll_multi_queue = false;
1698ed29b0b4SJens Axboe 	} else if (!ctx->poll_multi_queue) {
1699ed29b0b4SJens Axboe 		struct io_kiocb *list_req;
1700ed29b0b4SJens Axboe 
1701ed29b0b4SJens Axboe 		list_req = container_of(ctx->iopoll_list.first, struct io_kiocb,
1702ed29b0b4SJens Axboe 					comp_list);
1703ed29b0b4SJens Axboe 		if (list_req->file != req->file)
1704ed29b0b4SJens Axboe 			ctx->poll_multi_queue = true;
1705ed29b0b4SJens Axboe 	}
1706ed29b0b4SJens Axboe 
1707ed29b0b4SJens Axboe 	/*
1708ed29b0b4SJens Axboe 	 * For fast devices, IO may have already completed. If it has, add
1709ed29b0b4SJens Axboe 	 * it to the front so we find it first.
1710ed29b0b4SJens Axboe 	 */
1711ed29b0b4SJens Axboe 	if (READ_ONCE(req->iopoll_completed))
1712ed29b0b4SJens Axboe 		wq_list_add_head(&req->comp_list, &ctx->iopoll_list);
1713ed29b0b4SJens Axboe 	else
1714ed29b0b4SJens Axboe 		wq_list_add_tail(&req->comp_list, &ctx->iopoll_list);
1715ed29b0b4SJens Axboe 
1716ed29b0b4SJens Axboe 	if (unlikely(needs_lock)) {
1717ed29b0b4SJens Axboe 		/*
1718ed29b0b4SJens Axboe 		 * If IORING_SETUP_SQPOLL is enabled, sqes are either handle
1719ed29b0b4SJens Axboe 		 * in sq thread task context or in io worker task context. If
1720ed29b0b4SJens Axboe 		 * current task context is sq thread, we don't need to check
1721ed29b0b4SJens Axboe 		 * whether should wake up sq thread.
1722ed29b0b4SJens Axboe 		 */
1723ed29b0b4SJens Axboe 		if ((ctx->flags & IORING_SETUP_SQPOLL) &&
1724ed29b0b4SJens Axboe 		    wq_has_sleeper(&ctx->sq_data->wait))
1725ed29b0b4SJens Axboe 			wake_up(&ctx->sq_data->wait);
1726ed29b0b4SJens Axboe 
1727ed29b0b4SJens Axboe 		mutex_unlock(&ctx->uring_lock);
1728ed29b0b4SJens Axboe 	}
1729ed29b0b4SJens Axboe }
1730ed29b0b4SJens Axboe 
1731ed29b0b4SJens Axboe static bool io_bdev_nowait(struct block_device *bdev)
1732ed29b0b4SJens Axboe {
1733568ec936SChristoph Hellwig 	return !bdev || bdev_nowait(bdev);
1734ed29b0b4SJens Axboe }
1735ed29b0b4SJens Axboe 
1736ed29b0b4SJens Axboe /*
1737ed29b0b4SJens Axboe  * If we tracked the file through the SCM inflight mechanism, we could support
1738ed29b0b4SJens Axboe  * any file. For now, just ensure that anything potentially problematic is done
1739ed29b0b4SJens Axboe  * inline.
1740ed29b0b4SJens Axboe  */
1741ed29b0b4SJens Axboe static bool __io_file_supports_nowait(struct file *file, umode_t mode)
1742ed29b0b4SJens Axboe {
1743ed29b0b4SJens Axboe 	if (S_ISBLK(mode)) {
1744ed29b0b4SJens Axboe 		if (IS_ENABLED(CONFIG_BLOCK) &&
1745ed29b0b4SJens Axboe 		    io_bdev_nowait(I_BDEV(file->f_mapping->host)))
1746ed29b0b4SJens Axboe 			return true;
1747ed29b0b4SJens Axboe 		return false;
1748ed29b0b4SJens Axboe 	}
1749ed29b0b4SJens Axboe 	if (S_ISSOCK(mode))
1750ed29b0b4SJens Axboe 		return true;
1751ed29b0b4SJens Axboe 	if (S_ISREG(mode)) {
1752ed29b0b4SJens Axboe 		if (IS_ENABLED(CONFIG_BLOCK) &&
1753ed29b0b4SJens Axboe 		    io_bdev_nowait(file->f_inode->i_sb->s_bdev) &&
1754e5550a14SJens Axboe 		    !io_is_uring_fops(file))
1755ed29b0b4SJens Axboe 			return true;
1756ed29b0b4SJens Axboe 		return false;
1757ed29b0b4SJens Axboe 	}
1758ed29b0b4SJens Axboe 
1759ed29b0b4SJens Axboe 	/* any ->read/write should understand O_NONBLOCK */
1760ed29b0b4SJens Axboe 	if (file->f_flags & O_NONBLOCK)
1761ed29b0b4SJens Axboe 		return true;
1762ed29b0b4SJens Axboe 	return file->f_mode & FMODE_NOWAIT;
1763ed29b0b4SJens Axboe }
1764ed29b0b4SJens Axboe 
1765ed29b0b4SJens Axboe /*
1766ed29b0b4SJens Axboe  * If we tracked the file through the SCM inflight mechanism, we could support
1767ed29b0b4SJens Axboe  * any file. For now, just ensure that anything potentially problematic is done
1768ed29b0b4SJens Axboe  * inline.
1769ed29b0b4SJens Axboe  */
1770a4ad4f74SJens Axboe unsigned int io_file_get_flags(struct file *file)
1771ed29b0b4SJens Axboe {
1772ed29b0b4SJens Axboe 	umode_t mode = file_inode(file)->i_mode;
1773ed29b0b4SJens Axboe 	unsigned int res = 0;
1774ed29b0b4SJens Axboe 
1775ed29b0b4SJens Axboe 	if (S_ISREG(mode))
1776ed29b0b4SJens Axboe 		res |= FFS_ISREG;
1777ed29b0b4SJens Axboe 	if (__io_file_supports_nowait(file, mode))
1778ed29b0b4SJens Axboe 		res |= FFS_NOWAIT;
1779ed29b0b4SJens Axboe 	return res;
1780ed29b0b4SJens Axboe }
1781ed29b0b4SJens Axboe 
178299f15d8dSJens Axboe bool io_alloc_async_data(struct io_kiocb *req)
1783ed29b0b4SJens Axboe {
1784f30bd4d0SBreno Leitao 	WARN_ON_ONCE(!io_cold_defs[req->opcode].async_size);
1785f30bd4d0SBreno Leitao 	req->async_data = kmalloc(io_cold_defs[req->opcode].async_size, GFP_KERNEL);
1786ed29b0b4SJens Axboe 	if (req->async_data) {
1787ed29b0b4SJens Axboe 		req->flags |= REQ_F_ASYNC_DATA;
1788ed29b0b4SJens Axboe 		return false;
1789ed29b0b4SJens Axboe 	}
1790ed29b0b4SJens Axboe 	return true;
1791ed29b0b4SJens Axboe }
1792ed29b0b4SJens Axboe 
1793f3b44f92SJens Axboe int io_req_prep_async(struct io_kiocb *req)
1794ed29b0b4SJens Axboe {
1795f30bd4d0SBreno Leitao 	const struct io_cold_def *cdef = &io_cold_defs[req->opcode];
1796a7dd2782SBreno Leitao 	const struct io_issue_def *def = &io_issue_defs[req->opcode];
1797ed29b0b4SJens Axboe 
1798ed29b0b4SJens Axboe 	/* assign early for deferred execution for non-fixed file */
179954aa7f23SJoseph Qi 	if (def->needs_file && !(req->flags & REQ_F_FIXED_FILE) && !req->file)
1800ed29b0b4SJens Axboe 		req->file = io_file_get_normal(req, req->cqe.fd);
1801f30bd4d0SBreno Leitao 	if (!cdef->prep_async)
1802ed29b0b4SJens Axboe 		return 0;
1803ed29b0b4SJens Axboe 	if (WARN_ON_ONCE(req_has_async_data(req)))
1804ed29b0b4SJens Axboe 		return -EFAULT;
1805f30bd4d0SBreno Leitao 	if (!def->manual_alloc) {
1806ed29b0b4SJens Axboe 		if (io_alloc_async_data(req))
1807ed29b0b4SJens Axboe 			return -EAGAIN;
180859169439SPavel Begunkov 	}
1809f30bd4d0SBreno Leitao 	return cdef->prep_async(req);
1810ed29b0b4SJens Axboe }
1811ed29b0b4SJens Axboe 
1812ed29b0b4SJens Axboe static u32 io_get_sequence(struct io_kiocb *req)
1813ed29b0b4SJens Axboe {
1814ed29b0b4SJens Axboe 	u32 seq = req->ctx->cached_sq_head;
1815ed29b0b4SJens Axboe 	struct io_kiocb *cur;
1816ed29b0b4SJens Axboe 
1817ed29b0b4SJens Axboe 	/* need original cached_sq_head, but it was increased for each req */
1818ed29b0b4SJens Axboe 	io_for_each_link(cur, req)
1819ed29b0b4SJens Axboe 		seq--;
1820ed29b0b4SJens Axboe 	return seq;
1821ed29b0b4SJens Axboe }
1822ed29b0b4SJens Axboe 
1823ed29b0b4SJens Axboe static __cold void io_drain_req(struct io_kiocb *req)
1824e276ae34SPavel Begunkov 	__must_hold(&ctx->uring_lock)
1825ed29b0b4SJens Axboe {
1826ed29b0b4SJens Axboe 	struct io_ring_ctx *ctx = req->ctx;
1827ed29b0b4SJens Axboe 	struct io_defer_entry *de;
1828ed29b0b4SJens Axboe 	int ret;
1829ed29b0b4SJens Axboe 	u32 seq = io_get_sequence(req);
1830ed29b0b4SJens Axboe 
1831ed29b0b4SJens Axboe 	/* Still need defer if there is pending req in defer list. */
1832ed29b0b4SJens Axboe 	spin_lock(&ctx->completion_lock);
1833ed29b0b4SJens Axboe 	if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list)) {
1834ed29b0b4SJens Axboe 		spin_unlock(&ctx->completion_lock);
1835ed29b0b4SJens Axboe queue:
1836ed29b0b4SJens Axboe 		ctx->drain_active = false;
1837ed29b0b4SJens Axboe 		io_req_task_queue(req);
1838ed29b0b4SJens Axboe 		return;
1839ed29b0b4SJens Axboe 	}
1840ed29b0b4SJens Axboe 	spin_unlock(&ctx->completion_lock);
1841ed29b0b4SJens Axboe 
1842ed29b0b4SJens Axboe 	io_prep_async_link(req);
1843ed29b0b4SJens Axboe 	de = kmalloc(sizeof(*de), GFP_KERNEL);
1844ed29b0b4SJens Axboe 	if (!de) {
1845ed29b0b4SJens Axboe 		ret = -ENOMEM;
1846ef5c600aSDylan Yudaken 		io_req_defer_failed(req, ret);
1847ef5c600aSDylan Yudaken 		return;
1848ed29b0b4SJens Axboe 	}
1849ed29b0b4SJens Axboe 
1850ed29b0b4SJens Axboe 	spin_lock(&ctx->completion_lock);
1851ed29b0b4SJens Axboe 	if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) {
1852ed29b0b4SJens Axboe 		spin_unlock(&ctx->completion_lock);
1853ed29b0b4SJens Axboe 		kfree(de);
1854ed29b0b4SJens Axboe 		goto queue;
1855ed29b0b4SJens Axboe 	}
1856ed29b0b4SJens Axboe 
185748863ffdSPavel Begunkov 	trace_io_uring_defer(req);
1858ed29b0b4SJens Axboe 	de->req = req;
1859ed29b0b4SJens Axboe 	de->seq = seq;
1860ed29b0b4SJens Axboe 	list_add_tail(&de->list, &ctx->defer_list);
1861ed29b0b4SJens Axboe 	spin_unlock(&ctx->completion_lock);
1862ed29b0b4SJens Axboe }
1863ed29b0b4SJens Axboe 
1864ed29b0b4SJens Axboe static void io_clean_op(struct io_kiocb *req)
1865ed29b0b4SJens Axboe {
1866ed29b0b4SJens Axboe 	if (req->flags & REQ_F_BUFFER_SELECTED) {
1867ed29b0b4SJens Axboe 		spin_lock(&req->ctx->completion_lock);
1868ed29b0b4SJens Axboe 		io_put_kbuf_comp(req);
1869ed29b0b4SJens Axboe 		spin_unlock(&req->ctx->completion_lock);
1870ed29b0b4SJens Axboe 	}
1871ed29b0b4SJens Axboe 
1872ed29b0b4SJens Axboe 	if (req->flags & REQ_F_NEED_CLEANUP) {
1873f30bd4d0SBreno Leitao 		const struct io_cold_def *def = &io_cold_defs[req->opcode];
1874ed29b0b4SJens Axboe 
18754d4c9cffSJens Axboe 		if (def->cleanup)
18764d4c9cffSJens Axboe 			def->cleanup(req);
1877ed29b0b4SJens Axboe 	}
1878ed29b0b4SJens Axboe 	if ((req->flags & REQ_F_POLLED) && req->apoll) {
1879ed29b0b4SJens Axboe 		kfree(req->apoll->double_poll);
1880ed29b0b4SJens Axboe 		kfree(req->apoll);
1881ed29b0b4SJens Axboe 		req->apoll = NULL;
1882ed29b0b4SJens Axboe 	}
1883ed29b0b4SJens Axboe 	if (req->flags & REQ_F_INFLIGHT) {
1884ed29b0b4SJens Axboe 		struct io_uring_task *tctx = req->task->io_uring;
1885ed29b0b4SJens Axboe 
1886ed29b0b4SJens Axboe 		atomic_dec(&tctx->inflight_tracked);
1887ed29b0b4SJens Axboe 	}
1888ed29b0b4SJens Axboe 	if (req->flags & REQ_F_CREDS)
1889ed29b0b4SJens Axboe 		put_cred(req->creds);
1890ed29b0b4SJens Axboe 	if (req->flags & REQ_F_ASYNC_DATA) {
1891ed29b0b4SJens Axboe 		kfree(req->async_data);
1892ed29b0b4SJens Axboe 		req->async_data = NULL;
1893ed29b0b4SJens Axboe 	}
1894ed29b0b4SJens Axboe 	req->flags &= ~IO_REQ_CLEAN_FLAGS;
1895ed29b0b4SJens Axboe }
1896ed29b0b4SJens Axboe 
1897f4992544SJens Axboe static bool io_assign_file(struct io_kiocb *req, const struct io_issue_def *def,
1898f4992544SJens Axboe 			   unsigned int issue_flags)
1899ed29b0b4SJens Axboe {
1900f4992544SJens Axboe 	if (req->file || !def->needs_file)
1901ed29b0b4SJens Axboe 		return true;
1902ed29b0b4SJens Axboe 
1903ed29b0b4SJens Axboe 	if (req->flags & REQ_F_FIXED_FILE)
1904ed29b0b4SJens Axboe 		req->file = io_file_get_fixed(req, req->cqe.fd, issue_flags);
1905ed29b0b4SJens Axboe 	else
1906ed29b0b4SJens Axboe 		req->file = io_file_get_normal(req, req->cqe.fd);
1907ed29b0b4SJens Axboe 
1908ed29b0b4SJens Axboe 	return !!req->file;
1909ed29b0b4SJens Axboe }
1910ed29b0b4SJens Axboe 
1911ed29b0b4SJens Axboe static int io_issue_sqe(struct io_kiocb *req, unsigned int issue_flags)
1912ed29b0b4SJens Axboe {
1913a7dd2782SBreno Leitao 	const struct io_issue_def *def = &io_issue_defs[req->opcode];
1914ed29b0b4SJens Axboe 	const struct cred *creds = NULL;
1915ed29b0b4SJens Axboe 	int ret;
1916ed29b0b4SJens Axboe 
1917f4992544SJens Axboe 	if (unlikely(!io_assign_file(req, def, issue_flags)))
1918ed29b0b4SJens Axboe 		return -EBADF;
1919ed29b0b4SJens Axboe 
1920ed29b0b4SJens Axboe 	if (unlikely((req->flags & REQ_F_CREDS) && req->creds != current_cred()))
1921ed29b0b4SJens Axboe 		creds = override_creds(req->creds);
1922ed29b0b4SJens Axboe 
1923ed29b0b4SJens Axboe 	if (!def->audit_skip)
1924ed29b0b4SJens Axboe 		audit_uring_entry(req->opcode);
1925ed29b0b4SJens Axboe 
1926ed29b0b4SJens Axboe 	ret = def->issue(req, issue_flags);
1927ed29b0b4SJens Axboe 
1928ed29b0b4SJens Axboe 	if (!def->audit_skip)
1929ed29b0b4SJens Axboe 		audit_uring_exit(!ret, ret);
1930ed29b0b4SJens Axboe 
1931ed29b0b4SJens Axboe 	if (creds)
1932ed29b0b4SJens Axboe 		revert_creds(creds);
193397b388d7SJens Axboe 
193475d7b3aeSPavel Begunkov 	if (ret == IOU_OK) {
193575d7b3aeSPavel Begunkov 		if (issue_flags & IO_URING_F_COMPLETE_DEFER)
19369da070b1SPavel Begunkov 			io_req_complete_defer(req);
193775d7b3aeSPavel Begunkov 		else
19381bec951cSPavel Begunkov 			io_req_complete_post(req, issue_flags);
193975d7b3aeSPavel Begunkov 	} else if (ret != IOU_ISSUE_SKIP_COMPLETE)
1940ed29b0b4SJens Axboe 		return ret;
194197b388d7SJens Axboe 
1942ed29b0b4SJens Axboe 	/* If the op doesn't have a file, we're not polling for it */
1943ef0ec1adSPavel Begunkov 	if ((req->ctx->flags & IORING_SETUP_IOPOLL) && def->iopoll_queue)
1944ed29b0b4SJens Axboe 		io_iopoll_req_issued(req, issue_flags);
1945ed29b0b4SJens Axboe 
1946ed29b0b4SJens Axboe 	return 0;
1947ed29b0b4SJens Axboe }
1948ed29b0b4SJens Axboe 
1949a282967cSPavel Begunkov int io_poll_issue(struct io_kiocb *req, struct io_tw_state *ts)
1950329061d3SJens Axboe {
1951a282967cSPavel Begunkov 	io_tw_lock(req->ctx, ts);
19529a692451SDylan Yudaken 	return io_issue_sqe(req, IO_URING_F_NONBLOCK|IO_URING_F_MULTISHOT|
19539a692451SDylan Yudaken 				 IO_URING_F_COMPLETE_DEFER);
1954329061d3SJens Axboe }
1955329061d3SJens Axboe 
1956c9f06aa7SJens Axboe struct io_wq_work *io_wq_free_work(struct io_wq_work *work)
1957ed29b0b4SJens Axboe {
1958ed29b0b4SJens Axboe 	struct io_kiocb *req = container_of(work, struct io_kiocb, work);
1959ed29b0b4SJens Axboe 
1960ed29b0b4SJens Axboe 	req = io_put_req_find_next(req);
1961ed29b0b4SJens Axboe 	return req ? &req->work : NULL;
1962ed29b0b4SJens Axboe }
1963ed29b0b4SJens Axboe 
1964c9f06aa7SJens Axboe void io_wq_submit_work(struct io_wq_work *work)
1965ed29b0b4SJens Axboe {
1966ed29b0b4SJens Axboe 	struct io_kiocb *req = container_of(work, struct io_kiocb, work);
1967a7dd2782SBreno Leitao 	const struct io_issue_def *def = &io_issue_defs[req->opcode];
1968e6aeb272SPavel Begunkov 	unsigned int issue_flags = IO_URING_F_UNLOCKED | IO_URING_F_IOWQ;
1969ed29b0b4SJens Axboe 	bool needs_poll = false;
1970ed29b0b4SJens Axboe 	int ret = 0, err = -ECANCELED;
1971ed29b0b4SJens Axboe 
197223a6c9acSLin Ma 	/* one will be dropped by ->io_wq_free_work() after returning to io-wq */
1973ed29b0b4SJens Axboe 	if (!(req->flags & REQ_F_REFCOUNT))
1974ed29b0b4SJens Axboe 		__io_req_set_refcount(req, 2);
1975ed29b0b4SJens Axboe 	else
1976ed29b0b4SJens Axboe 		req_ref_get(req);
1977ed29b0b4SJens Axboe 
1978ed29b0b4SJens Axboe 	io_arm_ltimeout(req);
1979ed29b0b4SJens Axboe 
1980ed29b0b4SJens Axboe 	/* either cancelled or io-wq is dying, so don't touch tctx->iowq */
1981ed29b0b4SJens Axboe 	if (work->flags & IO_WQ_WORK_CANCEL) {
1982ed29b0b4SJens Axboe fail:
1983ed29b0b4SJens Axboe 		io_req_task_queue_fail(req, err);
1984ed29b0b4SJens Axboe 		return;
1985ed29b0b4SJens Axboe 	}
1986f4992544SJens Axboe 	if (!io_assign_file(req, def, issue_flags)) {
1987ed29b0b4SJens Axboe 		err = -EBADF;
1988ed29b0b4SJens Axboe 		work->flags |= IO_WQ_WORK_CANCEL;
1989ed29b0b4SJens Axboe 		goto fail;
1990ed29b0b4SJens Axboe 	}
1991ed29b0b4SJens Axboe 
1992ed29b0b4SJens Axboe 	if (req->flags & REQ_F_FORCE_ASYNC) {
1993ed29b0b4SJens Axboe 		bool opcode_poll = def->pollin || def->pollout;
1994ed29b0b4SJens Axboe 
1995ed29b0b4SJens Axboe 		if (opcode_poll && file_can_poll(req->file)) {
1996ed29b0b4SJens Axboe 			needs_poll = true;
1997ed29b0b4SJens Axboe 			issue_flags |= IO_URING_F_NONBLOCK;
1998ed29b0b4SJens Axboe 		}
1999ed29b0b4SJens Axboe 	}
2000ed29b0b4SJens Axboe 
2001ed29b0b4SJens Axboe 	do {
2002ed29b0b4SJens Axboe 		ret = io_issue_sqe(req, issue_flags);
2003ed29b0b4SJens Axboe 		if (ret != -EAGAIN)
2004ed29b0b4SJens Axboe 			break;
2005ed29b0b4SJens Axboe 		/*
2006ed29b0b4SJens Axboe 		 * We can get EAGAIN for iopolled IO even though we're
2007ed29b0b4SJens Axboe 		 * forcing a sync submission from here, since we can't
2008ed29b0b4SJens Axboe 		 * wait for request slots on the block side.
2009ed29b0b4SJens Axboe 		 */
2010ed29b0b4SJens Axboe 		if (!needs_poll) {
2011ed29b0b4SJens Axboe 			if (!(req->ctx->flags & IORING_SETUP_IOPOLL))
2012ed29b0b4SJens Axboe 				break;
2013ed29b0b4SJens Axboe 			cond_resched();
2014ed29b0b4SJens Axboe 			continue;
2015ed29b0b4SJens Axboe 		}
2016ed29b0b4SJens Axboe 
2017ed29b0b4SJens Axboe 		if (io_arm_poll_handler(req, issue_flags) == IO_APOLL_OK)
2018ed29b0b4SJens Axboe 			return;
2019ed29b0b4SJens Axboe 		/* aborted or ready, in either case retry blocking */
2020ed29b0b4SJens Axboe 		needs_poll = false;
2021ed29b0b4SJens Axboe 		issue_flags &= ~IO_URING_F_NONBLOCK;
2022ed29b0b4SJens Axboe 	} while (1);
2023ed29b0b4SJens Axboe 
2024ed29b0b4SJens Axboe 	/* avoid locking problems by failing it from a clean context */
202597b388d7SJens Axboe 	if (ret < 0)
2026ed29b0b4SJens Axboe 		io_req_task_queue_fail(req, ret);
2027ed29b0b4SJens Axboe }
2028ed29b0b4SJens Axboe 
2029531113bbSJens Axboe inline struct file *io_file_get_fixed(struct io_kiocb *req, int fd,
2030ed29b0b4SJens Axboe 				      unsigned int issue_flags)
2031ed29b0b4SJens Axboe {
2032ed29b0b4SJens Axboe 	struct io_ring_ctx *ctx = req->ctx;
2033ed29b0b4SJens Axboe 	struct file *file = NULL;
2034ed29b0b4SJens Axboe 	unsigned long file_ptr;
2035ed29b0b4SJens Axboe 
2036ed29b0b4SJens Axboe 	io_ring_submit_lock(ctx, issue_flags);
2037ed29b0b4SJens Axboe 
2038ed29b0b4SJens Axboe 	if (unlikely((unsigned int)fd >= ctx->nr_user_files))
2039ed29b0b4SJens Axboe 		goto out;
2040ed29b0b4SJens Axboe 	fd = array_index_nospec(fd, ctx->nr_user_files);
2041ed29b0b4SJens Axboe 	file_ptr = io_fixed_file_slot(&ctx->file_table, fd)->file_ptr;
2042ed29b0b4SJens Axboe 	file = (struct file *) (file_ptr & FFS_MASK);
2043ed29b0b4SJens Axboe 	file_ptr &= ~FFS_MASK;
2044ed29b0b4SJens Axboe 	/* mask in overlapping REQ_F and FFS bits */
2045ed29b0b4SJens Axboe 	req->flags |= (file_ptr << REQ_F_SUPPORT_NOWAIT_BIT);
2046ed29b0b4SJens Axboe 	io_req_set_rsrc_node(req, ctx, 0);
2047ed29b0b4SJens Axboe out:
2048ed29b0b4SJens Axboe 	io_ring_submit_unlock(ctx, issue_flags);
2049ed29b0b4SJens Axboe 	return file;
2050ed29b0b4SJens Axboe }
2051ed29b0b4SJens Axboe 
2052531113bbSJens Axboe struct file *io_file_get_normal(struct io_kiocb *req, int fd)
2053ed29b0b4SJens Axboe {
2054ed29b0b4SJens Axboe 	struct file *file = fget(fd);
2055ed29b0b4SJens Axboe 
205648863ffdSPavel Begunkov 	trace_io_uring_file_get(req, fd);
2057ed29b0b4SJens Axboe 
2058ed29b0b4SJens Axboe 	/* we don't allow fixed io_uring files */
2059e5550a14SJens Axboe 	if (file && io_is_uring_fops(file))
2060ed29b0b4SJens Axboe 		io_req_track_inflight(req);
2061ed29b0b4SJens Axboe 	return file;
2062ed29b0b4SJens Axboe }
2063ed29b0b4SJens Axboe 
2064ed29b0b4SJens Axboe static void io_queue_async(struct io_kiocb *req, int ret)
2065ed29b0b4SJens Axboe 	__must_hold(&req->ctx->uring_lock)
2066ed29b0b4SJens Axboe {
2067ed29b0b4SJens Axboe 	struct io_kiocb *linked_timeout;
2068ed29b0b4SJens Axboe 
2069ed29b0b4SJens Axboe 	if (ret != -EAGAIN || (req->flags & REQ_F_NOWAIT)) {
2070973fc83fSDylan Yudaken 		io_req_defer_failed(req, ret);
2071ed29b0b4SJens Axboe 		return;
2072ed29b0b4SJens Axboe 	}
2073ed29b0b4SJens Axboe 
2074ed29b0b4SJens Axboe 	linked_timeout = io_prep_linked_timeout(req);
2075ed29b0b4SJens Axboe 
2076ed29b0b4SJens Axboe 	switch (io_arm_poll_handler(req, 0)) {
2077ed29b0b4SJens Axboe 	case IO_APOLL_READY:
2078336d28a8SPavel Begunkov 		io_kbuf_recycle(req, 0);
2079ed29b0b4SJens Axboe 		io_req_task_queue(req);
2080ed29b0b4SJens Axboe 		break;
2081ed29b0b4SJens Axboe 	case IO_APOLL_ABORTED:
2082ed29b0b4SJens Axboe 		io_kbuf_recycle(req, 0);
2083ed29b0b4SJens Axboe 		io_queue_iowq(req, NULL);
2084ed29b0b4SJens Axboe 		break;
2085ed29b0b4SJens Axboe 	case IO_APOLL_OK:
2086ed29b0b4SJens Axboe 		break;
2087ed29b0b4SJens Axboe 	}
2088ed29b0b4SJens Axboe 
2089ed29b0b4SJens Axboe 	if (linked_timeout)
2090ed29b0b4SJens Axboe 		io_queue_linked_timeout(linked_timeout);
2091ed29b0b4SJens Axboe }
2092ed29b0b4SJens Axboe 
2093ed29b0b4SJens Axboe static inline void io_queue_sqe(struct io_kiocb *req)
2094ed29b0b4SJens Axboe 	__must_hold(&req->ctx->uring_lock)
2095ed29b0b4SJens Axboe {
2096ed29b0b4SJens Axboe 	int ret;
2097ed29b0b4SJens Axboe 
2098ed29b0b4SJens Axboe 	ret = io_issue_sqe(req, IO_URING_F_NONBLOCK|IO_URING_F_COMPLETE_DEFER);
2099ed29b0b4SJens Axboe 
2100ed29b0b4SJens Axboe 	/*
2101ed29b0b4SJens Axboe 	 * We async punt it if the file wasn't marked NOWAIT, or if the file
2102ed29b0b4SJens Axboe 	 * doesn't support non-blocking read/write attempts
2103ed29b0b4SJens Axboe 	 */
2104ed29b0b4SJens Axboe 	if (likely(!ret))
2105ed29b0b4SJens Axboe 		io_arm_ltimeout(req);
2106ed29b0b4SJens Axboe 	else
2107ed29b0b4SJens Axboe 		io_queue_async(req, ret);
2108ed29b0b4SJens Axboe }
2109ed29b0b4SJens Axboe 
2110ed29b0b4SJens Axboe static void io_queue_sqe_fallback(struct io_kiocb *req)
2111ed29b0b4SJens Axboe 	__must_hold(&req->ctx->uring_lock)
2112ed29b0b4SJens Axboe {
2113ed29b0b4SJens Axboe 	if (unlikely(req->flags & REQ_F_FAIL)) {
2114ed29b0b4SJens Axboe 		/*
2115ed29b0b4SJens Axboe 		 * We don't submit, fail them all, for that replace hardlinks
2116ed29b0b4SJens Axboe 		 * with normal links. Extra REQ_F_LINK is tolerated.
2117ed29b0b4SJens Axboe 		 */
2118ed29b0b4SJens Axboe 		req->flags &= ~REQ_F_HARDLINK;
2119ed29b0b4SJens Axboe 		req->flags |= REQ_F_LINK;
2120973fc83fSDylan Yudaken 		io_req_defer_failed(req, req->cqe.res);
2121ed29b0b4SJens Axboe 	} else {
2122ed29b0b4SJens Axboe 		int ret = io_req_prep_async(req);
2123ed29b0b4SJens Axboe 
2124ef5c600aSDylan Yudaken 		if (unlikely(ret)) {
2125973fc83fSDylan Yudaken 			io_req_defer_failed(req, ret);
2126ef5c600aSDylan Yudaken 			return;
2127ef5c600aSDylan Yudaken 		}
2128ef5c600aSDylan Yudaken 
2129ef5c600aSDylan Yudaken 		if (unlikely(req->ctx->drain_active))
2130ef5c600aSDylan Yudaken 			io_drain_req(req);
2131ed29b0b4SJens Axboe 		else
2132ed29b0b4SJens Axboe 			io_queue_iowq(req, NULL);
2133ed29b0b4SJens Axboe 	}
2134ed29b0b4SJens Axboe }
2135ed29b0b4SJens Axboe 
2136ed29b0b4SJens Axboe /*
2137ed29b0b4SJens Axboe  * Check SQE restrictions (opcode and flags).
2138ed29b0b4SJens Axboe  *
2139ed29b0b4SJens Axboe  * Returns 'true' if SQE is allowed, 'false' otherwise.
2140ed29b0b4SJens Axboe  */
2141ed29b0b4SJens Axboe static inline bool io_check_restriction(struct io_ring_ctx *ctx,
2142ed29b0b4SJens Axboe 					struct io_kiocb *req,
2143ed29b0b4SJens Axboe 					unsigned int sqe_flags)
2144ed29b0b4SJens Axboe {
2145ed29b0b4SJens Axboe 	if (!test_bit(req->opcode, ctx->restrictions.sqe_op))
2146ed29b0b4SJens Axboe 		return false;
2147ed29b0b4SJens Axboe 
2148ed29b0b4SJens Axboe 	if ((sqe_flags & ctx->restrictions.sqe_flags_required) !=
2149ed29b0b4SJens Axboe 	    ctx->restrictions.sqe_flags_required)
2150ed29b0b4SJens Axboe 		return false;
2151ed29b0b4SJens Axboe 
2152ed29b0b4SJens Axboe 	if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed |
2153ed29b0b4SJens Axboe 			  ctx->restrictions.sqe_flags_required))
2154ed29b0b4SJens Axboe 		return false;
2155ed29b0b4SJens Axboe 
2156ed29b0b4SJens Axboe 	return true;
2157ed29b0b4SJens Axboe }
2158ed29b0b4SJens Axboe 
2159ed29b0b4SJens Axboe static void io_init_req_drain(struct io_kiocb *req)
2160ed29b0b4SJens Axboe {
2161ed29b0b4SJens Axboe 	struct io_ring_ctx *ctx = req->ctx;
2162ed29b0b4SJens Axboe 	struct io_kiocb *head = ctx->submit_state.link.head;
2163ed29b0b4SJens Axboe 
2164ed29b0b4SJens Axboe 	ctx->drain_active = true;
2165ed29b0b4SJens Axboe 	if (head) {
2166ed29b0b4SJens Axboe 		/*
2167ed29b0b4SJens Axboe 		 * If we need to drain a request in the middle of a link, drain
2168ed29b0b4SJens Axboe 		 * the head request and the next request/link after the current
2169ed29b0b4SJens Axboe 		 * link. Considering sequential execution of links,
2170ed29b0b4SJens Axboe 		 * REQ_F_IO_DRAIN will be maintained for every request of our
2171ed29b0b4SJens Axboe 		 * link.
2172ed29b0b4SJens Axboe 		 */
2173ed29b0b4SJens Axboe 		head->flags |= REQ_F_IO_DRAIN | REQ_F_FORCE_ASYNC;
2174ed29b0b4SJens Axboe 		ctx->drain_next = true;
2175ed29b0b4SJens Axboe 	}
2176ed29b0b4SJens Axboe }
2177ed29b0b4SJens Axboe 
2178ed29b0b4SJens Axboe static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req,
2179ed29b0b4SJens Axboe 		       const struct io_uring_sqe *sqe)
2180ed29b0b4SJens Axboe 	__must_hold(&ctx->uring_lock)
2181ed29b0b4SJens Axboe {
2182a7dd2782SBreno Leitao 	const struct io_issue_def *def;
2183ed29b0b4SJens Axboe 	unsigned int sqe_flags;
2184ed29b0b4SJens Axboe 	int personality;
2185ed29b0b4SJens Axboe 	u8 opcode;
2186ed29b0b4SJens Axboe 
2187ed29b0b4SJens Axboe 	/* req is partially pre-initialised, see io_preinit_req() */
2188ed29b0b4SJens Axboe 	req->opcode = opcode = READ_ONCE(sqe->opcode);
2189ed29b0b4SJens Axboe 	/* same numerical values with corresponding REQ_F_*, safe to copy */
2190ed29b0b4SJens Axboe 	req->flags = sqe_flags = READ_ONCE(sqe->flags);
2191ed29b0b4SJens Axboe 	req->cqe.user_data = READ_ONCE(sqe->user_data);
2192ed29b0b4SJens Axboe 	req->file = NULL;
2193ed29b0b4SJens Axboe 	req->rsrc_node = NULL;
2194ed29b0b4SJens Axboe 	req->task = current;
2195ed29b0b4SJens Axboe 
2196ed29b0b4SJens Axboe 	if (unlikely(opcode >= IORING_OP_LAST)) {
2197ed29b0b4SJens Axboe 		req->opcode = 0;
2198ed29b0b4SJens Axboe 		return -EINVAL;
2199ed29b0b4SJens Axboe 	}
2200a7dd2782SBreno Leitao 	def = &io_issue_defs[opcode];
2201ed29b0b4SJens Axboe 	if (unlikely(sqe_flags & ~SQE_COMMON_FLAGS)) {
2202ed29b0b4SJens Axboe 		/* enforce forwards compatibility on users */
2203ed29b0b4SJens Axboe 		if (sqe_flags & ~SQE_VALID_FLAGS)
2204ed29b0b4SJens Axboe 			return -EINVAL;
2205ed29b0b4SJens Axboe 		if (sqe_flags & IOSQE_BUFFER_SELECT) {
2206ed29b0b4SJens Axboe 			if (!def->buffer_select)
2207ed29b0b4SJens Axboe 				return -EOPNOTSUPP;
2208ed29b0b4SJens Axboe 			req->buf_index = READ_ONCE(sqe->buf_group);
2209ed29b0b4SJens Axboe 		}
2210ed29b0b4SJens Axboe 		if (sqe_flags & IOSQE_CQE_SKIP_SUCCESS)
2211ed29b0b4SJens Axboe 			ctx->drain_disabled = true;
2212ed29b0b4SJens Axboe 		if (sqe_flags & IOSQE_IO_DRAIN) {
2213ed29b0b4SJens Axboe 			if (ctx->drain_disabled)
2214ed29b0b4SJens Axboe 				return -EOPNOTSUPP;
2215ed29b0b4SJens Axboe 			io_init_req_drain(req);
2216ed29b0b4SJens Axboe 		}
2217ed29b0b4SJens Axboe 	}
2218ed29b0b4SJens Axboe 	if (unlikely(ctx->restricted || ctx->drain_active || ctx->drain_next)) {
2219ed29b0b4SJens Axboe 		if (ctx->restricted && !io_check_restriction(ctx, req, sqe_flags))
2220ed29b0b4SJens Axboe 			return -EACCES;
2221ed29b0b4SJens Axboe 		/* knock it to the slow queue path, will be drained there */
2222ed29b0b4SJens Axboe 		if (ctx->drain_active)
2223ed29b0b4SJens Axboe 			req->flags |= REQ_F_FORCE_ASYNC;
2224ed29b0b4SJens Axboe 		/* if there is no link, we're at "next" request and need to drain */
2225ed29b0b4SJens Axboe 		if (unlikely(ctx->drain_next) && !ctx->submit_state.link.head) {
2226ed29b0b4SJens Axboe 			ctx->drain_next = false;
2227ed29b0b4SJens Axboe 			ctx->drain_active = true;
2228ed29b0b4SJens Axboe 			req->flags |= REQ_F_IO_DRAIN | REQ_F_FORCE_ASYNC;
2229ed29b0b4SJens Axboe 		}
2230ed29b0b4SJens Axboe 	}
2231ed29b0b4SJens Axboe 
2232ed29b0b4SJens Axboe 	if (!def->ioprio && sqe->ioprio)
2233ed29b0b4SJens Axboe 		return -EINVAL;
2234ed29b0b4SJens Axboe 	if (!def->iopoll && (ctx->flags & IORING_SETUP_IOPOLL))
2235ed29b0b4SJens Axboe 		return -EINVAL;
2236ed29b0b4SJens Axboe 
2237ed29b0b4SJens Axboe 	if (def->needs_file) {
2238ed29b0b4SJens Axboe 		struct io_submit_state *state = &ctx->submit_state;
2239ed29b0b4SJens Axboe 
2240ed29b0b4SJens Axboe 		req->cqe.fd = READ_ONCE(sqe->fd);
2241ed29b0b4SJens Axboe 
2242ed29b0b4SJens Axboe 		/*
2243ed29b0b4SJens Axboe 		 * Plug now if we have more than 2 IO left after this, and the
2244ed29b0b4SJens Axboe 		 * target is potentially a read/write to block based storage.
2245ed29b0b4SJens Axboe 		 */
2246ed29b0b4SJens Axboe 		if (state->need_plug && def->plug) {
2247ed29b0b4SJens Axboe 			state->plug_started = true;
2248ed29b0b4SJens Axboe 			state->need_plug = false;
2249ed29b0b4SJens Axboe 			blk_start_plug_nr_ios(&state->plug, state->submit_nr);
2250ed29b0b4SJens Axboe 		}
2251ed29b0b4SJens Axboe 	}
2252ed29b0b4SJens Axboe 
2253ed29b0b4SJens Axboe 	personality = READ_ONCE(sqe->personality);
2254ed29b0b4SJens Axboe 	if (personality) {
2255ed29b0b4SJens Axboe 		int ret;
2256ed29b0b4SJens Axboe 
2257ed29b0b4SJens Axboe 		req->creds = xa_load(&ctx->personalities, personality);
2258ed29b0b4SJens Axboe 		if (!req->creds)
2259ed29b0b4SJens Axboe 			return -EINVAL;
2260ed29b0b4SJens Axboe 		get_cred(req->creds);
2261ed29b0b4SJens Axboe 		ret = security_uring_override_creds(req->creds);
2262ed29b0b4SJens Axboe 		if (ret) {
2263ed29b0b4SJens Axboe 			put_cred(req->creds);
2264ed29b0b4SJens Axboe 			return ret;
2265ed29b0b4SJens Axboe 		}
2266ed29b0b4SJens Axboe 		req->flags |= REQ_F_CREDS;
2267ed29b0b4SJens Axboe 	}
2268ed29b0b4SJens Axboe 
2269ed29b0b4SJens Axboe 	return def->prep(req, sqe);
2270ed29b0b4SJens Axboe }
2271ed29b0b4SJens Axboe 
2272ed29b0b4SJens Axboe static __cold int io_submit_fail_init(const struct io_uring_sqe *sqe,
2273ed29b0b4SJens Axboe 				      struct io_kiocb *req, int ret)
2274ed29b0b4SJens Axboe {
2275ed29b0b4SJens Axboe 	struct io_ring_ctx *ctx = req->ctx;
2276ed29b0b4SJens Axboe 	struct io_submit_link *link = &ctx->submit_state.link;
2277ed29b0b4SJens Axboe 	struct io_kiocb *head = link->head;
2278ed29b0b4SJens Axboe 
227948863ffdSPavel Begunkov 	trace_io_uring_req_failed(sqe, req, ret);
2280ed29b0b4SJens Axboe 
2281ed29b0b4SJens Axboe 	/*
2282ed29b0b4SJens Axboe 	 * Avoid breaking links in the middle as it renders links with SQPOLL
2283ed29b0b4SJens Axboe 	 * unusable. Instead of failing eagerly, continue assembling the link if
2284ed29b0b4SJens Axboe 	 * applicable and mark the head with REQ_F_FAIL. The link flushing code
2285ed29b0b4SJens Axboe 	 * should find the flag and handle the rest.
2286ed29b0b4SJens Axboe 	 */
2287ed29b0b4SJens Axboe 	req_fail_link_node(req, ret);
2288ed29b0b4SJens Axboe 	if (head && !(head->flags & REQ_F_FAIL))
2289ed29b0b4SJens Axboe 		req_fail_link_node(head, -ECANCELED);
2290ed29b0b4SJens Axboe 
2291ed29b0b4SJens Axboe 	if (!(req->flags & IO_REQ_LINK_FLAGS)) {
2292ed29b0b4SJens Axboe 		if (head) {
2293ed29b0b4SJens Axboe 			link->last->link = req;
2294ed29b0b4SJens Axboe 			link->head = NULL;
2295ed29b0b4SJens Axboe 			req = head;
2296ed29b0b4SJens Axboe 		}
2297ed29b0b4SJens Axboe 		io_queue_sqe_fallback(req);
2298ed29b0b4SJens Axboe 		return ret;
2299ed29b0b4SJens Axboe 	}
2300ed29b0b4SJens Axboe 
2301ed29b0b4SJens Axboe 	if (head)
2302ed29b0b4SJens Axboe 		link->last->link = req;
2303ed29b0b4SJens Axboe 	else
2304ed29b0b4SJens Axboe 		link->head = req;
2305ed29b0b4SJens Axboe 	link->last = req;
2306ed29b0b4SJens Axboe 	return 0;
2307ed29b0b4SJens Axboe }
2308ed29b0b4SJens Axboe 
2309ed29b0b4SJens Axboe static inline int io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req,
2310ed29b0b4SJens Axboe 			 const struct io_uring_sqe *sqe)
2311ed29b0b4SJens Axboe 	__must_hold(&ctx->uring_lock)
2312ed29b0b4SJens Axboe {
2313ed29b0b4SJens Axboe 	struct io_submit_link *link = &ctx->submit_state.link;
2314ed29b0b4SJens Axboe 	int ret;
2315ed29b0b4SJens Axboe 
2316ed29b0b4SJens Axboe 	ret = io_init_req(ctx, req, sqe);
2317ed29b0b4SJens Axboe 	if (unlikely(ret))
2318ed29b0b4SJens Axboe 		return io_submit_fail_init(sqe, req, ret);
2319ed29b0b4SJens Axboe 
23202ad57931SJens Axboe 	trace_io_uring_submit_req(req);
2321ed29b0b4SJens Axboe 
2322ed29b0b4SJens Axboe 	/*
2323ed29b0b4SJens Axboe 	 * If we already have a head request, queue this one for async
2324ed29b0b4SJens Axboe 	 * submittal once the head completes. If we don't have a head but
2325ed29b0b4SJens Axboe 	 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
2326ed29b0b4SJens Axboe 	 * submitted sync once the chain is complete. If none of those
2327ed29b0b4SJens Axboe 	 * conditions are true (normal request), then just queue it.
2328ed29b0b4SJens Axboe 	 */
2329ed29b0b4SJens Axboe 	if (unlikely(link->head)) {
2330ed29b0b4SJens Axboe 		ret = io_req_prep_async(req);
2331ed29b0b4SJens Axboe 		if (unlikely(ret))
2332ed29b0b4SJens Axboe 			return io_submit_fail_init(sqe, req, ret);
2333ed29b0b4SJens Axboe 
233448863ffdSPavel Begunkov 		trace_io_uring_link(req, link->head);
2335ed29b0b4SJens Axboe 		link->last->link = req;
2336ed29b0b4SJens Axboe 		link->last = req;
2337ed29b0b4SJens Axboe 
2338ed29b0b4SJens Axboe 		if (req->flags & IO_REQ_LINK_FLAGS)
2339ed29b0b4SJens Axboe 			return 0;
2340ed29b0b4SJens Axboe 		/* last request of the link, flush it */
2341ed29b0b4SJens Axboe 		req = link->head;
2342ed29b0b4SJens Axboe 		link->head = NULL;
2343ed29b0b4SJens Axboe 		if (req->flags & (REQ_F_FORCE_ASYNC | REQ_F_FAIL))
2344ed29b0b4SJens Axboe 			goto fallback;
2345ed29b0b4SJens Axboe 
2346ed29b0b4SJens Axboe 	} else if (unlikely(req->flags & (IO_REQ_LINK_FLAGS |
2347ed29b0b4SJens Axboe 					  REQ_F_FORCE_ASYNC | REQ_F_FAIL))) {
2348ed29b0b4SJens Axboe 		if (req->flags & IO_REQ_LINK_FLAGS) {
2349ed29b0b4SJens Axboe 			link->head = req;
2350ed29b0b4SJens Axboe 			link->last = req;
2351ed29b0b4SJens Axboe 		} else {
2352ed29b0b4SJens Axboe fallback:
2353ed29b0b4SJens Axboe 			io_queue_sqe_fallback(req);
2354ed29b0b4SJens Axboe 		}
2355ed29b0b4SJens Axboe 		return 0;
2356ed29b0b4SJens Axboe 	}
2357ed29b0b4SJens Axboe 
2358ed29b0b4SJens Axboe 	io_queue_sqe(req);
2359ed29b0b4SJens Axboe 	return 0;
2360ed29b0b4SJens Axboe }
2361ed29b0b4SJens Axboe 
2362ed29b0b4SJens Axboe /*
2363ed29b0b4SJens Axboe  * Batched submission is done, ensure local IO is flushed out.
2364ed29b0b4SJens Axboe  */
2365ed29b0b4SJens Axboe static void io_submit_state_end(struct io_ring_ctx *ctx)
2366ed29b0b4SJens Axboe {
2367ed29b0b4SJens Axboe 	struct io_submit_state *state = &ctx->submit_state;
2368ed29b0b4SJens Axboe 
2369ed29b0b4SJens Axboe 	if (unlikely(state->link.head))
2370ed29b0b4SJens Axboe 		io_queue_sqe_fallback(state->link.head);
2371ed29b0b4SJens Axboe 	/* flush only after queuing links as they can generate completions */
2372ed29b0b4SJens Axboe 	io_submit_flush_completions(ctx);
2373ed29b0b4SJens Axboe 	if (state->plug_started)
2374ed29b0b4SJens Axboe 		blk_finish_plug(&state->plug);
2375ed29b0b4SJens Axboe }
2376ed29b0b4SJens Axboe 
2377ed29b0b4SJens Axboe /*
2378ed29b0b4SJens Axboe  * Start submission side cache.
2379ed29b0b4SJens Axboe  */
2380ed29b0b4SJens Axboe static void io_submit_state_start(struct io_submit_state *state,
2381ed29b0b4SJens Axboe 				  unsigned int max_ios)
2382ed29b0b4SJens Axboe {
2383ed29b0b4SJens Axboe 	state->plug_started = false;
2384ed29b0b4SJens Axboe 	state->need_plug = max_ios > 2;
2385ed29b0b4SJens Axboe 	state->submit_nr = max_ios;
2386ed29b0b4SJens Axboe 	/* set only head, no need to init link_last in advance */
2387ed29b0b4SJens Axboe 	state->link.head = NULL;
2388ed29b0b4SJens Axboe }
2389ed29b0b4SJens Axboe 
2390ed29b0b4SJens Axboe static void io_commit_sqring(struct io_ring_ctx *ctx)
2391ed29b0b4SJens Axboe {
2392ed29b0b4SJens Axboe 	struct io_rings *rings = ctx->rings;
2393ed29b0b4SJens Axboe 
2394ed29b0b4SJens Axboe 	/*
2395ed29b0b4SJens Axboe 	 * Ensure any loads from the SQEs are done at this point,
2396ed29b0b4SJens Axboe 	 * since once we write the new head, the application could
2397ed29b0b4SJens Axboe 	 * write new data to them.
2398ed29b0b4SJens Axboe 	 */
2399ed29b0b4SJens Axboe 	smp_store_release(&rings->sq.head, ctx->cached_sq_head);
2400ed29b0b4SJens Axboe }
2401ed29b0b4SJens Axboe 
2402ed29b0b4SJens Axboe /*
2403ed29b0b4SJens Axboe  * Fetch an sqe, if one is available. Note this returns a pointer to memory
2404ed29b0b4SJens Axboe  * that is mapped by userspace. This means that care needs to be taken to
2405ed29b0b4SJens Axboe  * ensure that reads are stable, as we cannot rely on userspace always
2406ed29b0b4SJens Axboe  * being a good citizen. If members of the sqe are validated and then later
2407ed29b0b4SJens Axboe  * used, it's important that those reads are done through READ_ONCE() to
2408ed29b0b4SJens Axboe  * prevent a re-load down the line.
2409ed29b0b4SJens Axboe  */
2410b5083dfaSPavel Begunkov static bool io_get_sqe(struct io_ring_ctx *ctx, const struct io_uring_sqe **sqe)
2411ed29b0b4SJens Axboe {
2412ed29b0b4SJens Axboe 	unsigned head, mask = ctx->sq_entries - 1;
2413ed29b0b4SJens Axboe 	unsigned sq_idx = ctx->cached_sq_head++ & mask;
2414ed29b0b4SJens Axboe 
2415ed29b0b4SJens Axboe 	/*
2416ed29b0b4SJens Axboe 	 * The cached sq head (or cq tail) serves two purposes:
2417ed29b0b4SJens Axboe 	 *
2418ed29b0b4SJens Axboe 	 * 1) allows us to batch the cost of updating the user visible
2419ed29b0b4SJens Axboe 	 *    head updates.
2420ed29b0b4SJens Axboe 	 * 2) allows the kernel side to track the head on its own, even
2421ed29b0b4SJens Axboe 	 *    though the application is the one updating it.
2422ed29b0b4SJens Axboe 	 */
2423ed29b0b4SJens Axboe 	head = READ_ONCE(ctx->sq_array[sq_idx]);
2424ed29b0b4SJens Axboe 	if (likely(head < ctx->sq_entries)) {
2425ed29b0b4SJens Axboe 		/* double index for 128-byte SQEs, twice as long */
2426ed29b0b4SJens Axboe 		if (ctx->flags & IORING_SETUP_SQE128)
2427ed29b0b4SJens Axboe 			head <<= 1;
2428b5083dfaSPavel Begunkov 		*sqe = &ctx->sq_sqes[head];
2429b5083dfaSPavel Begunkov 		return true;
2430ed29b0b4SJens Axboe 	}
2431ed29b0b4SJens Axboe 
2432ed29b0b4SJens Axboe 	/* drop invalid entries */
2433ed29b0b4SJens Axboe 	ctx->cq_extra--;
2434ed29b0b4SJens Axboe 	WRITE_ONCE(ctx->rings->sq_dropped,
2435ed29b0b4SJens Axboe 		   READ_ONCE(ctx->rings->sq_dropped) + 1);
2436b5083dfaSPavel Begunkov 	return false;
2437ed29b0b4SJens Axboe }
2438ed29b0b4SJens Axboe 
243917437f31SJens Axboe int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr)
2440ed29b0b4SJens Axboe 	__must_hold(&ctx->uring_lock)
2441ed29b0b4SJens Axboe {
2442ed29b0b4SJens Axboe 	unsigned int entries = io_sqring_entries(ctx);
2443ed29b0b4SJens Axboe 	unsigned int left;
2444ed29b0b4SJens Axboe 	int ret;
2445ed29b0b4SJens Axboe 
2446ed29b0b4SJens Axboe 	if (unlikely(!entries))
2447ed29b0b4SJens Axboe 		return 0;
2448ed29b0b4SJens Axboe 	/* make sure SQ entry isn't read before tail */
2449e3ef728fSJens Axboe 	ret = left = min(nr, entries);
2450ed29b0b4SJens Axboe 	io_get_task_refs(left);
2451ed29b0b4SJens Axboe 	io_submit_state_start(&ctx->submit_state, left);
2452ed29b0b4SJens Axboe 
2453ed29b0b4SJens Axboe 	do {
2454ed29b0b4SJens Axboe 		const struct io_uring_sqe *sqe;
2455ed29b0b4SJens Axboe 		struct io_kiocb *req;
2456ed29b0b4SJens Axboe 
2457c8576f3eSPavel Begunkov 		if (unlikely(!io_alloc_req(ctx, &req)))
2458ed29b0b4SJens Axboe 			break;
2459b5083dfaSPavel Begunkov 		if (unlikely(!io_get_sqe(ctx, &sqe))) {
2460ed29b0b4SJens Axboe 			io_req_add_to_cache(req, ctx);
2461ed29b0b4SJens Axboe 			break;
2462ed29b0b4SJens Axboe 		}
2463ed29b0b4SJens Axboe 
2464ed29b0b4SJens Axboe 		/*
2465ed29b0b4SJens Axboe 		 * Continue submitting even for sqe failure if the
2466ed29b0b4SJens Axboe 		 * ring was setup with IORING_SETUP_SUBMIT_ALL
2467ed29b0b4SJens Axboe 		 */
2468ed29b0b4SJens Axboe 		if (unlikely(io_submit_sqe(ctx, req, sqe)) &&
2469ed29b0b4SJens Axboe 		    !(ctx->flags & IORING_SETUP_SUBMIT_ALL)) {
2470ed29b0b4SJens Axboe 			left--;
2471ed29b0b4SJens Axboe 			break;
2472ed29b0b4SJens Axboe 		}
2473ed29b0b4SJens Axboe 	} while (--left);
2474ed29b0b4SJens Axboe 
2475ed29b0b4SJens Axboe 	if (unlikely(left)) {
2476ed29b0b4SJens Axboe 		ret -= left;
2477ed29b0b4SJens Axboe 		/* try again if it submitted nothing and can't allocate a req */
2478ed29b0b4SJens Axboe 		if (!ret && io_req_cache_empty(ctx))
2479ed29b0b4SJens Axboe 			ret = -EAGAIN;
2480ed29b0b4SJens Axboe 		current->io_uring->cached_refs += left;
2481ed29b0b4SJens Axboe 	}
2482ed29b0b4SJens Axboe 
2483ed29b0b4SJens Axboe 	io_submit_state_end(ctx);
2484ed29b0b4SJens Axboe 	 /* Commit SQ ring head once we've consumed and submitted all SQEs */
2485ed29b0b4SJens Axboe 	io_commit_sqring(ctx);
2486ed29b0b4SJens Axboe 	return ret;
2487ed29b0b4SJens Axboe }
2488ed29b0b4SJens Axboe 
2489ed29b0b4SJens Axboe struct io_wait_queue {
2490ed29b0b4SJens Axboe 	struct wait_queue_entry wq;
2491ed29b0b4SJens Axboe 	struct io_ring_ctx *ctx;
2492ed29b0b4SJens Axboe 	unsigned cq_tail;
2493ed29b0b4SJens Axboe 	unsigned nr_timeouts;
2494d33a39e5SPavel Begunkov 	ktime_t timeout;
2495ed29b0b4SJens Axboe };
2496ed29b0b4SJens Axboe 
2497b4c98d59SDylan Yudaken static inline bool io_has_work(struct io_ring_ctx *ctx)
2498b4c98d59SDylan Yudaken {
2499c0e0d6baSDylan Yudaken 	return test_bit(IO_CHECK_CQ_OVERFLOW_BIT, &ctx->check_cq) ||
2500490c00ebSPavel Begunkov 	       !llist_empty(&ctx->work_llist);
2501b4c98d59SDylan Yudaken }
2502b4c98d59SDylan Yudaken 
2503ed29b0b4SJens Axboe static inline bool io_should_wake(struct io_wait_queue *iowq)
2504ed29b0b4SJens Axboe {
2505ed29b0b4SJens Axboe 	struct io_ring_ctx *ctx = iowq->ctx;
25060fc8c2acSDylan Yudaken 	int dist = READ_ONCE(ctx->rings->cq.tail) - (int) iowq->cq_tail;
2507ed29b0b4SJens Axboe 
2508ed29b0b4SJens Axboe 	/*
2509ed29b0b4SJens Axboe 	 * Wake up if we have enough events, or if a timeout occurred since we
2510ed29b0b4SJens Axboe 	 * started waiting. For timeouts, we always want to return to userspace,
2511ed29b0b4SJens Axboe 	 * regardless of event count.
2512ed29b0b4SJens Axboe 	 */
2513ed29b0b4SJens Axboe 	return dist >= 0 || atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
2514ed29b0b4SJens Axboe }
2515ed29b0b4SJens Axboe 
2516ed29b0b4SJens Axboe static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
2517ed29b0b4SJens Axboe 			    int wake_flags, void *key)
2518ed29b0b4SJens Axboe {
2519bd550173SPavel Begunkov 	struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue, wq);
2520ed29b0b4SJens Axboe 
2521ed29b0b4SJens Axboe 	/*
2522ed29b0b4SJens Axboe 	 * Cannot safely flush overflowed CQEs from here, ensure we wake up
2523ed29b0b4SJens Axboe 	 * the task, and the next invocation will do it.
2524ed29b0b4SJens Axboe 	 */
2525bd550173SPavel Begunkov 	if (io_should_wake(iowq) || io_has_work(iowq->ctx))
2526ed29b0b4SJens Axboe 		return autoremove_wake_function(curr, mode, wake_flags, key);
2527ed29b0b4SJens Axboe 	return -1;
2528ed29b0b4SJens Axboe }
2529ed29b0b4SJens Axboe 
2530c0e0d6baSDylan Yudaken int io_run_task_work_sig(struct io_ring_ctx *ctx)
2531ed29b0b4SJens Axboe {
25321414d629SPavel Begunkov 	if (!llist_empty(&ctx->work_llist)) {
25332f413956SPavel Begunkov 		__set_current_state(TASK_RUNNING);
25341414d629SPavel Begunkov 		if (io_run_local_work(ctx) > 0)
25351414d629SPavel Begunkov 			return 1;
25361414d629SPavel Begunkov 	}
25371414d629SPavel Begunkov 	if (io_run_task_work() > 0)
2538ed29b0b4SJens Axboe 		return 1;
2539ed29b0b4SJens Axboe 	if (task_sigpending(current))
2540ed29b0b4SJens Axboe 		return -EINTR;
2541ed29b0b4SJens Axboe 	return 0;
2542ed29b0b4SJens Axboe }
2543ed29b0b4SJens Axboe 
2544ed29b0b4SJens Axboe /* when returns >0, the caller should retry */
2545ed29b0b4SJens Axboe static inline int io_cqring_wait_schedule(struct io_ring_ctx *ctx,
2546d33a39e5SPavel Begunkov 					  struct io_wait_queue *iowq)
2547ed29b0b4SJens Axboe {
25483fcf19d5SPavel Begunkov 	if (unlikely(READ_ONCE(ctx->check_cq)))
25493fcf19d5SPavel Begunkov 		return 1;
2550846072f1SPavel Begunkov 	if (unlikely(!llist_empty(&ctx->work_llist)))
2551846072f1SPavel Begunkov 		return 1;
2552846072f1SPavel Begunkov 	if (unlikely(test_thread_flag(TIF_NOTIFY_SIGNAL)))
2553846072f1SPavel Begunkov 		return 1;
2554846072f1SPavel Begunkov 	if (unlikely(task_sigpending(current)))
2555846072f1SPavel Begunkov 		return -EINTR;
2556846072f1SPavel Begunkov 	if (unlikely(io_should_wake(iowq)))
2557846072f1SPavel Begunkov 		return 0;
2558d33a39e5SPavel Begunkov 	if (iowq->timeout == KTIME_MAX)
255946ae7eefSPavel Begunkov 		schedule();
2560d33a39e5SPavel Begunkov 	else if (!schedule_hrtimeout(&iowq->timeout, HRTIMER_MODE_ABS))
2561ed29b0b4SJens Axboe 		return -ETIME;
2562846072f1SPavel Begunkov 	return 0;
2563ed29b0b4SJens Axboe }
2564ed29b0b4SJens Axboe 
2565ed29b0b4SJens Axboe /*
2566ed29b0b4SJens Axboe  * Wait until events become available, if we don't already have some. The
2567ed29b0b4SJens Axboe  * application must reap them itself, as they reside on the shared cq ring.
2568ed29b0b4SJens Axboe  */
2569ed29b0b4SJens Axboe static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
2570ed29b0b4SJens Axboe 			  const sigset_t __user *sig, size_t sigsz,
2571ed29b0b4SJens Axboe 			  struct __kernel_timespec __user *uts)
2572ed29b0b4SJens Axboe {
2573ed29b0b4SJens Axboe 	struct io_wait_queue iowq;
2574ed29b0b4SJens Axboe 	struct io_rings *rings = ctx->rings;
2575ed29b0b4SJens Axboe 	int ret;
2576ed29b0b4SJens Axboe 
257776de6749SPavel Begunkov 	if (!io_allowed_run_tw(ctx))
257876de6749SPavel Begunkov 		return -EEXIST;
2579140102aeSPavel Begunkov 	if (!llist_empty(&ctx->work_llist))
2580140102aeSPavel Begunkov 		io_run_local_work(ctx);
2581f36ba6cfSPavel Begunkov 	io_run_task_work();
2582ed29b0b4SJens Axboe 	io_cqring_overflow_flush(ctx);
25830fc8c2acSDylan Yudaken 	/* if user messes with these they will just get an early return */
25840fc8c2acSDylan Yudaken 	if (__io_cqring_events_user(ctx) >= min_events)
2585ed29b0b4SJens Axboe 		return 0;
2586ed29b0b4SJens Axboe 
2587ed29b0b4SJens Axboe 	if (sig) {
2588ed29b0b4SJens Axboe #ifdef CONFIG_COMPAT
2589ed29b0b4SJens Axboe 		if (in_compat_syscall())
2590ed29b0b4SJens Axboe 			ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
2591ed29b0b4SJens Axboe 						      sigsz);
2592ed29b0b4SJens Axboe 		else
2593ed29b0b4SJens Axboe #endif
2594ed29b0b4SJens Axboe 			ret = set_user_sigmask(sig, sigsz);
2595ed29b0b4SJens Axboe 
2596ed29b0b4SJens Axboe 		if (ret)
2597ed29b0b4SJens Axboe 			return ret;
2598ed29b0b4SJens Axboe 	}
2599ed29b0b4SJens Axboe 
2600ed29b0b4SJens Axboe 	init_waitqueue_func_entry(&iowq.wq, io_wake_function);
2601ed29b0b4SJens Axboe 	iowq.wq.private = current;
2602ed29b0b4SJens Axboe 	INIT_LIST_HEAD(&iowq.wq.entry);
2603ed29b0b4SJens Axboe 	iowq.ctx = ctx;
2604ed29b0b4SJens Axboe 	iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
2605ed29b0b4SJens Axboe 	iowq.cq_tail = READ_ONCE(ctx->rings->cq.head) + min_events;
2606d33a39e5SPavel Begunkov 	iowq.timeout = KTIME_MAX;
2607d33a39e5SPavel Begunkov 
2608d33a39e5SPavel Begunkov 	if (uts) {
2609d33a39e5SPavel Begunkov 		struct timespec64 ts;
2610d33a39e5SPavel Begunkov 
2611d33a39e5SPavel Begunkov 		if (get_timespec64(&ts, uts))
2612d33a39e5SPavel Begunkov 			return -EFAULT;
2613d33a39e5SPavel Begunkov 		iowq.timeout = ktime_add_ns(timespec64_to_ktime(ts), ktime_get_ns());
2614d33a39e5SPavel Begunkov 	}
2615ed29b0b4SJens Axboe 
2616ed29b0b4SJens Axboe 	trace_io_uring_cqring_wait(ctx, min_events);
2617ed29b0b4SJens Axboe 	do {
26183fcf19d5SPavel Begunkov 		unsigned long check_cq;
26193fcf19d5SPavel Begunkov 
2620130bd686SPavel Begunkov 		if (ctx->flags & IORING_SETUP_DEFER_TASKRUN) {
2621d80c0f00SPavel Begunkov 			WRITE_ONCE(ctx->cq_waiting, 1);
2622130bd686SPavel Begunkov 			set_current_state(TASK_INTERRUPTIBLE);
2623130bd686SPavel Begunkov 		} else {
2624ed29b0b4SJens Axboe 			prepare_to_wait_exclusive(&ctx->cq_wait, &iowq.wq,
2625ed29b0b4SJens Axboe 							TASK_INTERRUPTIBLE);
2626130bd686SPavel Begunkov 		}
2627130bd686SPavel Begunkov 
2628d33a39e5SPavel Begunkov 		ret = io_cqring_wait_schedule(ctx, &iowq);
2629130bd686SPavel Begunkov 		__set_current_state(TASK_RUNNING);
2630d80c0f00SPavel Begunkov 		WRITE_ONCE(ctx->cq_waiting, 0);
2631d80c0f00SPavel Begunkov 
2632846072f1SPavel Begunkov 		if (ret < 0)
2633846072f1SPavel Begunkov 			break;
2634846072f1SPavel Begunkov 		/*
2635846072f1SPavel Begunkov 		 * Run task_work after scheduling and before io_should_wake().
2636846072f1SPavel Begunkov 		 * If we got woken because of task_work being processed, run it
2637846072f1SPavel Begunkov 		 * now rather than let the caller do another wait loop.
2638846072f1SPavel Begunkov 		 */
2639846072f1SPavel Begunkov 		io_run_task_work();
2640846072f1SPavel Begunkov 		if (!llist_empty(&ctx->work_llist))
2641846072f1SPavel Begunkov 			io_run_local_work(ctx);
26423fcf19d5SPavel Begunkov 
26433fcf19d5SPavel Begunkov 		check_cq = READ_ONCE(ctx->check_cq);
26443fcf19d5SPavel Begunkov 		if (unlikely(check_cq)) {
26453fcf19d5SPavel Begunkov 			/* let the caller flush overflows, retry */
2646326a9e48SPavel Begunkov 			if (check_cq & BIT(IO_CHECK_CQ_OVERFLOW_BIT))
26473fcf19d5SPavel Begunkov 				io_cqring_do_overflow_flush(ctx);
26483fcf19d5SPavel Begunkov 			if (check_cq & BIT(IO_CHECK_CQ_DROPPED_BIT)) {
26493fcf19d5SPavel Begunkov 				ret = -EBADR;
26503fcf19d5SPavel Begunkov 				break;
26513fcf19d5SPavel Begunkov 			}
26523fcf19d5SPavel Begunkov 		}
26533fcf19d5SPavel Begunkov 
2654846072f1SPavel Begunkov 		if (io_should_wake(&iowq)) {
2655846072f1SPavel Begunkov 			ret = 0;
265635d90f95SJens Axboe 			break;
2657846072f1SPavel Begunkov 		}
2658ed29b0b4SJens Axboe 		cond_resched();
2659846072f1SPavel Begunkov 	} while (1);
2660ed29b0b4SJens Axboe 
2661130bd686SPavel Begunkov 	if (!(ctx->flags & IORING_SETUP_DEFER_TASKRUN))
2662ed29b0b4SJens Axboe 		finish_wait(&ctx->cq_wait, &iowq.wq);
2663ed29b0b4SJens Axboe 	restore_saved_sigmask_unless(ret == -EINTR);
2664ed29b0b4SJens Axboe 
2665ed29b0b4SJens Axboe 	return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
2666ed29b0b4SJens Axboe }
2667ed29b0b4SJens Axboe 
2668ed29b0b4SJens Axboe static void io_mem_free(void *ptr)
2669ed29b0b4SJens Axboe {
2670ed29b0b4SJens Axboe 	struct page *page;
2671ed29b0b4SJens Axboe 
2672ed29b0b4SJens Axboe 	if (!ptr)
2673ed29b0b4SJens Axboe 		return;
2674ed29b0b4SJens Axboe 
2675ed29b0b4SJens Axboe 	page = virt_to_head_page(ptr);
2676ed29b0b4SJens Axboe 	if (put_page_testzero(page))
2677ed29b0b4SJens Axboe 		free_compound_page(page);
2678ed29b0b4SJens Axboe }
2679ed29b0b4SJens Axboe 
2680ed29b0b4SJens Axboe static void *io_mem_alloc(size_t size)
2681ed29b0b4SJens Axboe {
2682ed29b0b4SJens Axboe 	gfp_t gfp = GFP_KERNEL_ACCOUNT | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP;
2683ed29b0b4SJens Axboe 
2684ed29b0b4SJens Axboe 	return (void *) __get_free_pages(gfp, get_order(size));
2685ed29b0b4SJens Axboe }
2686ed29b0b4SJens Axboe 
2687ed29b0b4SJens Axboe static unsigned long rings_size(struct io_ring_ctx *ctx, unsigned int sq_entries,
2688ed29b0b4SJens Axboe 				unsigned int cq_entries, size_t *sq_offset)
2689ed29b0b4SJens Axboe {
2690ed29b0b4SJens Axboe 	struct io_rings *rings;
2691ed29b0b4SJens Axboe 	size_t off, sq_array_size;
2692ed29b0b4SJens Axboe 
2693ed29b0b4SJens Axboe 	off = struct_size(rings, cqes, cq_entries);
2694ed29b0b4SJens Axboe 	if (off == SIZE_MAX)
2695ed29b0b4SJens Axboe 		return SIZE_MAX;
2696ed29b0b4SJens Axboe 	if (ctx->flags & IORING_SETUP_CQE32) {
2697ed29b0b4SJens Axboe 		if (check_shl_overflow(off, 1, &off))
2698ed29b0b4SJens Axboe 			return SIZE_MAX;
2699ed29b0b4SJens Axboe 	}
2700ed29b0b4SJens Axboe 
2701ed29b0b4SJens Axboe #ifdef CONFIG_SMP
2702ed29b0b4SJens Axboe 	off = ALIGN(off, SMP_CACHE_BYTES);
2703ed29b0b4SJens Axboe 	if (off == 0)
2704ed29b0b4SJens Axboe 		return SIZE_MAX;
2705ed29b0b4SJens Axboe #endif
2706ed29b0b4SJens Axboe 
2707ed29b0b4SJens Axboe 	if (sq_offset)
2708ed29b0b4SJens Axboe 		*sq_offset = off;
2709ed29b0b4SJens Axboe 
2710ed29b0b4SJens Axboe 	sq_array_size = array_size(sizeof(u32), sq_entries);
2711ed29b0b4SJens Axboe 	if (sq_array_size == SIZE_MAX)
2712ed29b0b4SJens Axboe 		return SIZE_MAX;
2713ed29b0b4SJens Axboe 
2714ed29b0b4SJens Axboe 	if (check_add_overflow(off, sq_array_size, &off))
2715ed29b0b4SJens Axboe 		return SIZE_MAX;
2716ed29b0b4SJens Axboe 
2717ed29b0b4SJens Axboe 	return off;
2718ed29b0b4SJens Axboe }
2719ed29b0b4SJens Axboe 
2720ed29b0b4SJens Axboe static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg,
2721ed29b0b4SJens Axboe 			       unsigned int eventfd_async)
2722ed29b0b4SJens Axboe {
2723ed29b0b4SJens Axboe 	struct io_ev_fd *ev_fd;
2724ed29b0b4SJens Axboe 	__s32 __user *fds = arg;
2725ed29b0b4SJens Axboe 	int fd;
2726ed29b0b4SJens Axboe 
2727ed29b0b4SJens Axboe 	ev_fd = rcu_dereference_protected(ctx->io_ev_fd,
2728ed29b0b4SJens Axboe 					lockdep_is_held(&ctx->uring_lock));
2729ed29b0b4SJens Axboe 	if (ev_fd)
2730ed29b0b4SJens Axboe 		return -EBUSY;
2731ed29b0b4SJens Axboe 
2732ed29b0b4SJens Axboe 	if (copy_from_user(&fd, fds, sizeof(*fds)))
2733ed29b0b4SJens Axboe 		return -EFAULT;
2734ed29b0b4SJens Axboe 
2735ed29b0b4SJens Axboe 	ev_fd = kmalloc(sizeof(*ev_fd), GFP_KERNEL);
2736ed29b0b4SJens Axboe 	if (!ev_fd)
2737ed29b0b4SJens Axboe 		return -ENOMEM;
2738ed29b0b4SJens Axboe 
2739ed29b0b4SJens Axboe 	ev_fd->cq_ev_fd = eventfd_ctx_fdget(fd);
2740ed29b0b4SJens Axboe 	if (IS_ERR(ev_fd->cq_ev_fd)) {
2741ed29b0b4SJens Axboe 		int ret = PTR_ERR(ev_fd->cq_ev_fd);
2742ed29b0b4SJens Axboe 		kfree(ev_fd);
2743ed29b0b4SJens Axboe 		return ret;
2744ed29b0b4SJens Axboe 	}
2745305bef98SPavel Begunkov 
2746305bef98SPavel Begunkov 	spin_lock(&ctx->completion_lock);
2747305bef98SPavel Begunkov 	ctx->evfd_last_cq_tail = ctx->cached_cq_tail;
2748305bef98SPavel Begunkov 	spin_unlock(&ctx->completion_lock);
2749305bef98SPavel Begunkov 
2750ed29b0b4SJens Axboe 	ev_fd->eventfd_async = eventfd_async;
2751ed29b0b4SJens Axboe 	ctx->has_evfd = true;
2752ed29b0b4SJens Axboe 	rcu_assign_pointer(ctx->io_ev_fd, ev_fd);
275321a091b9SDylan Yudaken 	atomic_set(&ev_fd->refs, 1);
275421a091b9SDylan Yudaken 	atomic_set(&ev_fd->ops, 0);
2755ed29b0b4SJens Axboe 	return 0;
2756ed29b0b4SJens Axboe }
2757ed29b0b4SJens Axboe 
2758ed29b0b4SJens Axboe static int io_eventfd_unregister(struct io_ring_ctx *ctx)
2759ed29b0b4SJens Axboe {
2760ed29b0b4SJens Axboe 	struct io_ev_fd *ev_fd;
2761ed29b0b4SJens Axboe 
2762ed29b0b4SJens Axboe 	ev_fd = rcu_dereference_protected(ctx->io_ev_fd,
2763ed29b0b4SJens Axboe 					lockdep_is_held(&ctx->uring_lock));
2764ed29b0b4SJens Axboe 	if (ev_fd) {
2765ed29b0b4SJens Axboe 		ctx->has_evfd = false;
2766ed29b0b4SJens Axboe 		rcu_assign_pointer(ctx->io_ev_fd, NULL);
276721a091b9SDylan Yudaken 		if (!atomic_fetch_or(BIT(IO_EVENTFD_OP_FREE_BIT), &ev_fd->ops))
276821a091b9SDylan Yudaken 			call_rcu(&ev_fd->rcu, io_eventfd_ops);
2769ed29b0b4SJens Axboe 		return 0;
2770ed29b0b4SJens Axboe 	}
2771ed29b0b4SJens Axboe 
2772ed29b0b4SJens Axboe 	return -ENXIO;
2773ed29b0b4SJens Axboe }
2774ed29b0b4SJens Axboe 
2775ed29b0b4SJens Axboe static void io_req_caches_free(struct io_ring_ctx *ctx)
2776ed29b0b4SJens Axboe {
2777c8576f3eSPavel Begunkov 	struct io_kiocb *req;
2778ed29b0b4SJens Axboe 	int nr = 0;
2779ed29b0b4SJens Axboe 
2780ed29b0b4SJens Axboe 	mutex_lock(&ctx->uring_lock);
278134f0bc42SPavel Begunkov 	io_flush_cached_locked_reqs(ctx, &ctx->submit_state);
2782ed29b0b4SJens Axboe 
2783ed29b0b4SJens Axboe 	while (!io_req_cache_empty(ctx)) {
2784c8576f3eSPavel Begunkov 		req = io_extract_req(ctx);
2785ed29b0b4SJens Axboe 		kmem_cache_free(req_cachep, req);
2786ed29b0b4SJens Axboe 		nr++;
2787ed29b0b4SJens Axboe 	}
2788ed29b0b4SJens Axboe 	if (nr)
2789ed29b0b4SJens Axboe 		percpu_ref_put_many(&ctx->refs, nr);
2790ed29b0b4SJens Axboe 	mutex_unlock(&ctx->uring_lock);
2791ed29b0b4SJens Axboe }
2792ed29b0b4SJens Axboe 
27939eae8655SPavel Begunkov static void io_rsrc_node_cache_free(struct io_cache_entry *entry)
27949eae8655SPavel Begunkov {
27959eae8655SPavel Begunkov 	kfree(container_of(entry, struct io_rsrc_node, cache));
27969eae8655SPavel Begunkov }
27979eae8655SPavel Begunkov 
2798ed29b0b4SJens Axboe static __cold void io_ring_ctx_free(struct io_ring_ctx *ctx)
2799ed29b0b4SJens Axboe {
2800ed29b0b4SJens Axboe 	io_sq_thread_finish(ctx);
2801ed29b0b4SJens Axboe 	/* __io_rsrc_put_work() may need uring_lock to progress, wait w/o it */
2802ed29b0b4SJens Axboe 	io_wait_rsrc_data(ctx->buf_data);
2803ed29b0b4SJens Axboe 	io_wait_rsrc_data(ctx->file_data);
2804ed29b0b4SJens Axboe 
2805ed29b0b4SJens Axboe 	mutex_lock(&ctx->uring_lock);
2806ed29b0b4SJens Axboe 	if (ctx->buf_data)
2807ed29b0b4SJens Axboe 		__io_sqe_buffers_unregister(ctx);
2808ed29b0b4SJens Axboe 	if (ctx->file_data)
2809ed29b0b4SJens Axboe 		__io_sqe_files_unregister(ctx);
2810a85381d8SPavel Begunkov 	io_cqring_overflow_kill(ctx);
2811ed29b0b4SJens Axboe 	io_eventfd_unregister(ctx);
28129b797a37SJens Axboe 	io_alloc_cache_free(&ctx->apoll_cache, io_apoll_cache_free);
281343e0bbbdSJens Axboe 	io_alloc_cache_free(&ctx->netmsg_cache, io_netmsg_cache_free);
2814ed29b0b4SJens Axboe 	mutex_unlock(&ctx->uring_lock);
2815ed29b0b4SJens Axboe 	io_destroy_buffers(ctx);
2816ed29b0b4SJens Axboe 	if (ctx->sq_creds)
2817ed29b0b4SJens Axboe 		put_cred(ctx->sq_creds);
281897bbdc06SPavel Begunkov 	if (ctx->submitter_task)
281997bbdc06SPavel Begunkov 		put_task_struct(ctx->submitter_task);
2820ed29b0b4SJens Axboe 
2821ed29b0b4SJens Axboe 	/* there are no registered resources left, nobody uses it */
2822ed29b0b4SJens Axboe 	if (ctx->rsrc_node)
28239eae8655SPavel Begunkov 		io_rsrc_node_destroy(ctx, ctx->rsrc_node);
2824ed29b0b4SJens Axboe 	if (ctx->rsrc_backup_node)
28259eae8655SPavel Begunkov 		io_rsrc_node_destroy(ctx, ctx->rsrc_backup_node);
2826ed29b0b4SJens Axboe 
2827ed29b0b4SJens Axboe 	WARN_ON_ONCE(!list_empty(&ctx->rsrc_ref_list));
2828ed29b0b4SJens Axboe 
2829ed29b0b4SJens Axboe #if defined(CONFIG_UNIX)
2830ed29b0b4SJens Axboe 	if (ctx->ring_sock) {
2831ed29b0b4SJens Axboe 		ctx->ring_sock->file = NULL; /* so that iput() is called */
2832ed29b0b4SJens Axboe 		sock_release(ctx->ring_sock);
2833ed29b0b4SJens Axboe 	}
2834ed29b0b4SJens Axboe #endif
2835ed29b0b4SJens Axboe 	WARN_ON_ONCE(!list_empty(&ctx->ltimeout_list));
2836ed29b0b4SJens Axboe 
28379eae8655SPavel Begunkov 	io_alloc_cache_free(&ctx->rsrc_node_cache, io_rsrc_node_cache_free);
283842b6419dSPavel Begunkov 	if (ctx->mm_account) {
283942b6419dSPavel Begunkov 		mmdrop(ctx->mm_account);
284042b6419dSPavel Begunkov 		ctx->mm_account = NULL;
284142b6419dSPavel Begunkov 	}
2842ed29b0b4SJens Axboe 	io_mem_free(ctx->rings);
2843ed29b0b4SJens Axboe 	io_mem_free(ctx->sq_sqes);
2844ed29b0b4SJens Axboe 
2845ed29b0b4SJens Axboe 	percpu_ref_exit(&ctx->refs);
2846ed29b0b4SJens Axboe 	free_uid(ctx->user);
2847ed29b0b4SJens Axboe 	io_req_caches_free(ctx);
2848ed29b0b4SJens Axboe 	if (ctx->hash_map)
2849ed29b0b4SJens Axboe 		io_wq_put_hash(ctx->hash_map);
2850e6f89be6SPavel Begunkov 	kfree(ctx->cancel_table.hbs);
28519ca9fb24SPavel Begunkov 	kfree(ctx->cancel_table_locked.hbs);
2852ed29b0b4SJens Axboe 	kfree(ctx->dummy_ubuf);
2853ed29b0b4SJens Axboe 	kfree(ctx->io_bl);
2854ed29b0b4SJens Axboe 	xa_destroy(&ctx->io_bl_xa);
2855ed29b0b4SJens Axboe 	kfree(ctx);
2856ed29b0b4SJens Axboe }
2857ed29b0b4SJens Axboe 
2858bca39f39SPavel Begunkov static __cold void io_activate_pollwq_cb(struct callback_head *cb)
2859bca39f39SPavel Begunkov {
2860bca39f39SPavel Begunkov 	struct io_ring_ctx *ctx = container_of(cb, struct io_ring_ctx,
2861bca39f39SPavel Begunkov 					       poll_wq_task_work);
2862bca39f39SPavel Begunkov 
2863bca39f39SPavel Begunkov 	mutex_lock(&ctx->uring_lock);
2864bca39f39SPavel Begunkov 	ctx->poll_activated = true;
2865bca39f39SPavel Begunkov 	mutex_unlock(&ctx->uring_lock);
2866bca39f39SPavel Begunkov 
2867bca39f39SPavel Begunkov 	/*
2868bca39f39SPavel Begunkov 	 * Wake ups for some events between start of polling and activation
2869bca39f39SPavel Begunkov 	 * might've been lost due to loose synchronisation.
2870bca39f39SPavel Begunkov 	 */
2871bca39f39SPavel Begunkov 	wake_up_all(&ctx->poll_wq);
2872bca39f39SPavel Begunkov 	percpu_ref_put(&ctx->refs);
2873bca39f39SPavel Begunkov }
2874bca39f39SPavel Begunkov 
2875bca39f39SPavel Begunkov static __cold void io_activate_pollwq(struct io_ring_ctx *ctx)
2876bca39f39SPavel Begunkov {
2877bca39f39SPavel Begunkov 	spin_lock(&ctx->completion_lock);
2878bca39f39SPavel Begunkov 	/* already activated or in progress */
2879bca39f39SPavel Begunkov 	if (ctx->poll_activated || ctx->poll_wq_task_work.func)
2880bca39f39SPavel Begunkov 		goto out;
2881bca39f39SPavel Begunkov 	if (WARN_ON_ONCE(!ctx->task_complete))
2882bca39f39SPavel Begunkov 		goto out;
2883bca39f39SPavel Begunkov 	if (!ctx->submitter_task)
2884bca39f39SPavel Begunkov 		goto out;
2885bca39f39SPavel Begunkov 	/*
2886bca39f39SPavel Begunkov 	 * with ->submitter_task only the submitter task completes requests, we
2887bca39f39SPavel Begunkov 	 * only need to sync with it, which is done by injecting a tw
2888bca39f39SPavel Begunkov 	 */
2889bca39f39SPavel Begunkov 	init_task_work(&ctx->poll_wq_task_work, io_activate_pollwq_cb);
2890bca39f39SPavel Begunkov 	percpu_ref_get(&ctx->refs);
2891bca39f39SPavel Begunkov 	if (task_work_add(ctx->submitter_task, &ctx->poll_wq_task_work, TWA_SIGNAL))
2892bca39f39SPavel Begunkov 		percpu_ref_put(&ctx->refs);
2893bca39f39SPavel Begunkov out:
2894bca39f39SPavel Begunkov 	spin_unlock(&ctx->completion_lock);
2895bca39f39SPavel Begunkov }
2896bca39f39SPavel Begunkov 
2897ed29b0b4SJens Axboe static __poll_t io_uring_poll(struct file *file, poll_table *wait)
2898ed29b0b4SJens Axboe {
2899ed29b0b4SJens Axboe 	struct io_ring_ctx *ctx = file->private_data;
2900ed29b0b4SJens Axboe 	__poll_t mask = 0;
2901ed29b0b4SJens Axboe 
2902bca39f39SPavel Begunkov 	if (unlikely(!ctx->poll_activated))
2903bca39f39SPavel Begunkov 		io_activate_pollwq(ctx);
2904bca39f39SPavel Begunkov 
29057b235dd8SPavel Begunkov 	poll_wait(file, &ctx->poll_wq, wait);
2906ed29b0b4SJens Axboe 	/*
2907ed29b0b4SJens Axboe 	 * synchronizes with barrier from wq_has_sleeper call in
2908ed29b0b4SJens Axboe 	 * io_commit_cqring
2909ed29b0b4SJens Axboe 	 */
2910ed29b0b4SJens Axboe 	smp_rmb();
2911ed29b0b4SJens Axboe 	if (!io_sqring_full(ctx))
2912ed29b0b4SJens Axboe 		mask |= EPOLLOUT | EPOLLWRNORM;
2913ed29b0b4SJens Axboe 
2914ed29b0b4SJens Axboe 	/*
2915ed29b0b4SJens Axboe 	 * Don't flush cqring overflow list here, just do a simple check.
2916ed29b0b4SJens Axboe 	 * Otherwise there could possible be ABBA deadlock:
2917ed29b0b4SJens Axboe 	 *      CPU0                    CPU1
2918ed29b0b4SJens Axboe 	 *      ----                    ----
2919ed29b0b4SJens Axboe 	 * lock(&ctx->uring_lock);
2920ed29b0b4SJens Axboe 	 *                              lock(&ep->mtx);
2921ed29b0b4SJens Axboe 	 *                              lock(&ctx->uring_lock);
2922ed29b0b4SJens Axboe 	 * lock(&ep->mtx);
2923ed29b0b4SJens Axboe 	 *
2924ed29b0b4SJens Axboe 	 * Users may get EPOLLIN meanwhile seeing nothing in cqring, this
292510d8bc35SDylan Yudaken 	 * pushes them to do the flush.
2926ed29b0b4SJens Axboe 	 */
2927b4c98d59SDylan Yudaken 
2928c10bb646SPavel Begunkov 	if (__io_cqring_events_user(ctx) || io_has_work(ctx))
2929ed29b0b4SJens Axboe 		mask |= EPOLLIN | EPOLLRDNORM;
2930ed29b0b4SJens Axboe 
2931ed29b0b4SJens Axboe 	return mask;
2932ed29b0b4SJens Axboe }
2933ed29b0b4SJens Axboe 
2934ed29b0b4SJens Axboe static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
2935ed29b0b4SJens Axboe {
2936ed29b0b4SJens Axboe 	const struct cred *creds;
2937ed29b0b4SJens Axboe 
2938ed29b0b4SJens Axboe 	creds = xa_erase(&ctx->personalities, id);
2939ed29b0b4SJens Axboe 	if (creds) {
2940ed29b0b4SJens Axboe 		put_cred(creds);
2941ed29b0b4SJens Axboe 		return 0;
2942ed29b0b4SJens Axboe 	}
2943ed29b0b4SJens Axboe 
2944ed29b0b4SJens Axboe 	return -EINVAL;
2945ed29b0b4SJens Axboe }
2946ed29b0b4SJens Axboe 
2947ed29b0b4SJens Axboe struct io_tctx_exit {
2948ed29b0b4SJens Axboe 	struct callback_head		task_work;
2949ed29b0b4SJens Axboe 	struct completion		completion;
2950ed29b0b4SJens Axboe 	struct io_ring_ctx		*ctx;
2951ed29b0b4SJens Axboe };
2952ed29b0b4SJens Axboe 
2953ed29b0b4SJens Axboe static __cold void io_tctx_exit_cb(struct callback_head *cb)
2954ed29b0b4SJens Axboe {
2955ed29b0b4SJens Axboe 	struct io_uring_task *tctx = current->io_uring;
2956ed29b0b4SJens Axboe 	struct io_tctx_exit *work;
2957ed29b0b4SJens Axboe 
2958ed29b0b4SJens Axboe 	work = container_of(cb, struct io_tctx_exit, task_work);
2959ed29b0b4SJens Axboe 	/*
29608d664282SJens Axboe 	 * When @in_cancel, we're in cancellation and it's racy to remove the
2961ed29b0b4SJens Axboe 	 * node. It'll be removed by the end of cancellation, just ignore it.
2962998b30c3SHarshit Mogalapalli 	 * tctx can be NULL if the queueing of this task_work raced with
2963998b30c3SHarshit Mogalapalli 	 * work cancelation off the exec path.
2964ed29b0b4SJens Axboe 	 */
29658d664282SJens Axboe 	if (tctx && !atomic_read(&tctx->in_cancel))
2966ed29b0b4SJens Axboe 		io_uring_del_tctx_node((unsigned long)work->ctx);
2967ed29b0b4SJens Axboe 	complete(&work->completion);
2968ed29b0b4SJens Axboe }
2969ed29b0b4SJens Axboe 
2970ed29b0b4SJens Axboe static __cold bool io_cancel_ctx_cb(struct io_wq_work *work, void *data)
2971ed29b0b4SJens Axboe {
2972ed29b0b4SJens Axboe 	struct io_kiocb *req = container_of(work, struct io_kiocb, work);
2973ed29b0b4SJens Axboe 
2974ed29b0b4SJens Axboe 	return req->ctx == data;
2975ed29b0b4SJens Axboe }
2976ed29b0b4SJens Axboe 
2977ed29b0b4SJens Axboe static __cold void io_ring_exit_work(struct work_struct *work)
2978ed29b0b4SJens Axboe {
2979ed29b0b4SJens Axboe 	struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx, exit_work);
2980ed29b0b4SJens Axboe 	unsigned long timeout = jiffies + HZ * 60 * 5;
2981ed29b0b4SJens Axboe 	unsigned long interval = HZ / 20;
2982ed29b0b4SJens Axboe 	struct io_tctx_exit exit;
2983ed29b0b4SJens Axboe 	struct io_tctx_node *node;
2984ed29b0b4SJens Axboe 	int ret;
2985ed29b0b4SJens Axboe 
2986ed29b0b4SJens Axboe 	/*
2987ed29b0b4SJens Axboe 	 * If we're doing polled IO and end up having requests being
2988ed29b0b4SJens Axboe 	 * submitted async (out-of-line), then completions can come in while
2989ed29b0b4SJens Axboe 	 * we're waiting for refs to drop. We need to reap these manually,
2990ed29b0b4SJens Axboe 	 * as nobody else will be looking for them.
2991ed29b0b4SJens Axboe 	 */
2992ed29b0b4SJens Axboe 	do {
2993a85381d8SPavel Begunkov 		if (test_bit(IO_CHECK_CQ_OVERFLOW_BIT, &ctx->check_cq)) {
2994a85381d8SPavel Begunkov 			mutex_lock(&ctx->uring_lock);
2995a85381d8SPavel Begunkov 			io_cqring_overflow_kill(ctx);
2996a85381d8SPavel Begunkov 			mutex_unlock(&ctx->uring_lock);
2997a85381d8SPavel Begunkov 		}
2998a85381d8SPavel Begunkov 
2999c0e0d6baSDylan Yudaken 		if (ctx->flags & IORING_SETUP_DEFER_TASKRUN)
3000c0e0d6baSDylan Yudaken 			io_move_task_work_from_local(ctx);
3001c0e0d6baSDylan Yudaken 
3002affa87dbSPavel Begunkov 		while (io_uring_try_cancel_requests(ctx, NULL, true))
3003affa87dbSPavel Begunkov 			cond_resched();
3004affa87dbSPavel Begunkov 
3005ed29b0b4SJens Axboe 		if (ctx->sq_data) {
3006ed29b0b4SJens Axboe 			struct io_sq_data *sqd = ctx->sq_data;
3007ed29b0b4SJens Axboe 			struct task_struct *tsk;
3008ed29b0b4SJens Axboe 
3009ed29b0b4SJens Axboe 			io_sq_thread_park(sqd);
3010ed29b0b4SJens Axboe 			tsk = sqd->thread;
3011ed29b0b4SJens Axboe 			if (tsk && tsk->io_uring && tsk->io_uring->io_wq)
3012ed29b0b4SJens Axboe 				io_wq_cancel_cb(tsk->io_uring->io_wq,
3013ed29b0b4SJens Axboe 						io_cancel_ctx_cb, ctx, true);
3014ed29b0b4SJens Axboe 			io_sq_thread_unpark(sqd);
3015ed29b0b4SJens Axboe 		}
3016ed29b0b4SJens Axboe 
3017ed29b0b4SJens Axboe 		io_req_caches_free(ctx);
3018ed29b0b4SJens Axboe 
3019ed29b0b4SJens Axboe 		if (WARN_ON_ONCE(time_after(jiffies, timeout))) {
3020ed29b0b4SJens Axboe 			/* there is little hope left, don't run it too often */
3021ed29b0b4SJens Axboe 			interval = HZ * 60;
3022ed29b0b4SJens Axboe 		}
3023ed29b0b4SJens Axboe 	} while (!wait_for_completion_timeout(&ctx->ref_comp, interval));
3024ed29b0b4SJens Axboe 
3025ed29b0b4SJens Axboe 	init_completion(&exit.completion);
3026ed29b0b4SJens Axboe 	init_task_work(&exit.task_work, io_tctx_exit_cb);
3027ed29b0b4SJens Axboe 	exit.ctx = ctx;
3028ed29b0b4SJens Axboe 	/*
3029ed29b0b4SJens Axboe 	 * Some may use context even when all refs and requests have been put,
3030ed29b0b4SJens Axboe 	 * and they are free to do so while still holding uring_lock or
3031ed29b0b4SJens Axboe 	 * completion_lock, see io_req_task_submit(). Apart from other work,
3032ed29b0b4SJens Axboe 	 * this lock/unlock section also waits them to finish.
3033ed29b0b4SJens Axboe 	 */
3034ed29b0b4SJens Axboe 	mutex_lock(&ctx->uring_lock);
3035ed29b0b4SJens Axboe 	while (!list_empty(&ctx->tctx_list)) {
3036ed29b0b4SJens Axboe 		WARN_ON_ONCE(time_after(jiffies, timeout));
3037ed29b0b4SJens Axboe 
3038ed29b0b4SJens Axboe 		node = list_first_entry(&ctx->tctx_list, struct io_tctx_node,
3039ed29b0b4SJens Axboe 					ctx_node);
3040ed29b0b4SJens Axboe 		/* don't spin on a single task if cancellation failed */
3041ed29b0b4SJens Axboe 		list_rotate_left(&ctx->tctx_list);
3042ed29b0b4SJens Axboe 		ret = task_work_add(node->task, &exit.task_work, TWA_SIGNAL);
3043ed29b0b4SJens Axboe 		if (WARN_ON_ONCE(ret))
3044ed29b0b4SJens Axboe 			continue;
3045ed29b0b4SJens Axboe 
3046ed29b0b4SJens Axboe 		mutex_unlock(&ctx->uring_lock);
3047ed29b0b4SJens Axboe 		wait_for_completion(&exit.completion);
3048ed29b0b4SJens Axboe 		mutex_lock(&ctx->uring_lock);
3049ed29b0b4SJens Axboe 	}
3050ed29b0b4SJens Axboe 	mutex_unlock(&ctx->uring_lock);
3051ed29b0b4SJens Axboe 	spin_lock(&ctx->completion_lock);
3052ed29b0b4SJens Axboe 	spin_unlock(&ctx->completion_lock);
3053ed29b0b4SJens Axboe 
3054d73a572dSPavel Begunkov 	/* pairs with RCU read section in io_req_local_work_add() */
3055d73a572dSPavel Begunkov 	if (ctx->flags & IORING_SETUP_DEFER_TASKRUN)
3056d73a572dSPavel Begunkov 		synchronize_rcu();
3057d73a572dSPavel Begunkov 
3058ed29b0b4SJens Axboe 	io_ring_ctx_free(ctx);
3059ed29b0b4SJens Axboe }
3060ed29b0b4SJens Axboe 
3061ed29b0b4SJens Axboe static __cold void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
3062ed29b0b4SJens Axboe {
3063ed29b0b4SJens Axboe 	unsigned long index;
3064ed29b0b4SJens Axboe 	struct creds *creds;
3065ed29b0b4SJens Axboe 
3066ed29b0b4SJens Axboe 	mutex_lock(&ctx->uring_lock);
3067ed29b0b4SJens Axboe 	percpu_ref_kill(&ctx->refs);
3068ed29b0b4SJens Axboe 	xa_for_each(&ctx->personalities, index, creds)
3069ed29b0b4SJens Axboe 		io_unregister_personality(ctx, index);
30709ca9fb24SPavel Begunkov 	if (ctx->rings)
30719ca9fb24SPavel Begunkov 		io_poll_remove_all(ctx, NULL, true);
3072ed29b0b4SJens Axboe 	mutex_unlock(&ctx->uring_lock);
3073ed29b0b4SJens Axboe 
307402bac94bSPavel Begunkov 	/*
307502bac94bSPavel Begunkov 	 * If we failed setting up the ctx, we might not have any rings
307602bac94bSPavel Begunkov 	 * and therefore did not submit any requests
307702bac94bSPavel Begunkov 	 */
307802bac94bSPavel Begunkov 	if (ctx->rings)
3079ed29b0b4SJens Axboe 		io_kill_timeouts(ctx, NULL, true);
3080ed29b0b4SJens Axboe 
3081ed29b0b4SJens Axboe 	INIT_WORK(&ctx->exit_work, io_ring_exit_work);
3082ed29b0b4SJens Axboe 	/*
3083ed29b0b4SJens Axboe 	 * Use system_unbound_wq to avoid spawning tons of event kworkers
3084ed29b0b4SJens Axboe 	 * if we're exiting a ton of rings at the same time. It just adds
3085ed29b0b4SJens Axboe 	 * noise and overhead, there's no discernable change in runtime
3086ed29b0b4SJens Axboe 	 * over using system_wq.
3087ed29b0b4SJens Axboe 	 */
3088ed29b0b4SJens Axboe 	queue_work(system_unbound_wq, &ctx->exit_work);
3089ed29b0b4SJens Axboe }
3090ed29b0b4SJens Axboe 
3091ed29b0b4SJens Axboe static int io_uring_release(struct inode *inode, struct file *file)
3092ed29b0b4SJens Axboe {
3093ed29b0b4SJens Axboe 	struct io_ring_ctx *ctx = file->private_data;
3094ed29b0b4SJens Axboe 
3095ed29b0b4SJens Axboe 	file->private_data = NULL;
3096ed29b0b4SJens Axboe 	io_ring_ctx_wait_and_kill(ctx);
3097ed29b0b4SJens Axboe 	return 0;
3098ed29b0b4SJens Axboe }
3099ed29b0b4SJens Axboe 
3100ed29b0b4SJens Axboe struct io_task_cancel {
3101ed29b0b4SJens Axboe 	struct task_struct *task;
3102ed29b0b4SJens Axboe 	bool all;
3103ed29b0b4SJens Axboe };
3104ed29b0b4SJens Axboe 
3105ed29b0b4SJens Axboe static bool io_cancel_task_cb(struct io_wq_work *work, void *data)
3106ed29b0b4SJens Axboe {
3107ed29b0b4SJens Axboe 	struct io_kiocb *req = container_of(work, struct io_kiocb, work);
3108ed29b0b4SJens Axboe 	struct io_task_cancel *cancel = data;
3109ed29b0b4SJens Axboe 
3110ed29b0b4SJens Axboe 	return io_match_task_safe(req, cancel->task, cancel->all);
3111ed29b0b4SJens Axboe }
3112ed29b0b4SJens Axboe 
3113ed29b0b4SJens Axboe static __cold bool io_cancel_defer_files(struct io_ring_ctx *ctx,
3114ed29b0b4SJens Axboe 					 struct task_struct *task,
3115ed29b0b4SJens Axboe 					 bool cancel_all)
3116ed29b0b4SJens Axboe {
3117ed29b0b4SJens Axboe 	struct io_defer_entry *de;
3118ed29b0b4SJens Axboe 	LIST_HEAD(list);
3119ed29b0b4SJens Axboe 
3120ed29b0b4SJens Axboe 	spin_lock(&ctx->completion_lock);
3121ed29b0b4SJens Axboe 	list_for_each_entry_reverse(de, &ctx->defer_list, list) {
3122ed29b0b4SJens Axboe 		if (io_match_task_safe(de->req, task, cancel_all)) {
3123ed29b0b4SJens Axboe 			list_cut_position(&list, &ctx->defer_list, &de->list);
3124ed29b0b4SJens Axboe 			break;
3125ed29b0b4SJens Axboe 		}
3126ed29b0b4SJens Axboe 	}
3127ed29b0b4SJens Axboe 	spin_unlock(&ctx->completion_lock);
3128ed29b0b4SJens Axboe 	if (list_empty(&list))
3129ed29b0b4SJens Axboe 		return false;
3130ed29b0b4SJens Axboe 
3131ed29b0b4SJens Axboe 	while (!list_empty(&list)) {
3132ed29b0b4SJens Axboe 		de = list_first_entry(&list, struct io_defer_entry, list);
3133ed29b0b4SJens Axboe 		list_del_init(&de->list);
3134e276ae34SPavel Begunkov 		io_req_task_queue_fail(de->req, -ECANCELED);
3135ed29b0b4SJens Axboe 		kfree(de);
3136ed29b0b4SJens Axboe 	}
3137ed29b0b4SJens Axboe 	return true;
3138ed29b0b4SJens Axboe }
3139ed29b0b4SJens Axboe 
3140ed29b0b4SJens Axboe static __cold bool io_uring_try_cancel_iowq(struct io_ring_ctx *ctx)
3141ed29b0b4SJens Axboe {
3142ed29b0b4SJens Axboe 	struct io_tctx_node *node;
3143ed29b0b4SJens Axboe 	enum io_wq_cancel cret;
3144ed29b0b4SJens Axboe 	bool ret = false;
3145ed29b0b4SJens Axboe 
3146ed29b0b4SJens Axboe 	mutex_lock(&ctx->uring_lock);
3147ed29b0b4SJens Axboe 	list_for_each_entry(node, &ctx->tctx_list, ctx_node) {
3148ed29b0b4SJens Axboe 		struct io_uring_task *tctx = node->task->io_uring;
3149ed29b0b4SJens Axboe 
3150ed29b0b4SJens Axboe 		/*
3151ed29b0b4SJens Axboe 		 * io_wq will stay alive while we hold uring_lock, because it's
3152ed29b0b4SJens Axboe 		 * killed after ctx nodes, which requires to take the lock.
3153ed29b0b4SJens Axboe 		 */
3154ed29b0b4SJens Axboe 		if (!tctx || !tctx->io_wq)
3155ed29b0b4SJens Axboe 			continue;
3156ed29b0b4SJens Axboe 		cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_ctx_cb, ctx, true);
3157ed29b0b4SJens Axboe 		ret |= (cret != IO_WQ_CANCEL_NOTFOUND);
3158ed29b0b4SJens Axboe 	}
3159ed29b0b4SJens Axboe 	mutex_unlock(&ctx->uring_lock);
3160ed29b0b4SJens Axboe 
3161ed29b0b4SJens Axboe 	return ret;
3162ed29b0b4SJens Axboe }
3163ed29b0b4SJens Axboe 
3164affa87dbSPavel Begunkov static __cold bool io_uring_try_cancel_requests(struct io_ring_ctx *ctx,
3165ed29b0b4SJens Axboe 						struct task_struct *task,
3166ed29b0b4SJens Axboe 						bool cancel_all)
3167ed29b0b4SJens Axboe {
3168ed29b0b4SJens Axboe 	struct io_task_cancel cancel = { .task = task, .all = cancel_all, };
3169ed29b0b4SJens Axboe 	struct io_uring_task *tctx = task ? task->io_uring : NULL;
3170affa87dbSPavel Begunkov 	enum io_wq_cancel cret;
3171affa87dbSPavel Begunkov 	bool ret = false;
3172ed29b0b4SJens Axboe 
3173ed29b0b4SJens Axboe 	/* failed during ring init, it couldn't have issued any requests */
3174ed29b0b4SJens Axboe 	if (!ctx->rings)
3175affa87dbSPavel Begunkov 		return false;
3176ed29b0b4SJens Axboe 
3177ed29b0b4SJens Axboe 	if (!task) {
3178ed29b0b4SJens Axboe 		ret |= io_uring_try_cancel_iowq(ctx);
3179ed29b0b4SJens Axboe 	} else if (tctx && tctx->io_wq) {
3180ed29b0b4SJens Axboe 		/*
3181ed29b0b4SJens Axboe 		 * Cancels requests of all rings, not only @ctx, but
3182ed29b0b4SJens Axboe 		 * it's fine as the task is in exit/exec.
3183ed29b0b4SJens Axboe 		 */
3184ed29b0b4SJens Axboe 		cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_task_cb,
3185ed29b0b4SJens Axboe 				       &cancel, true);
3186ed29b0b4SJens Axboe 		ret |= (cret != IO_WQ_CANCEL_NOTFOUND);
3187ed29b0b4SJens Axboe 	}
3188ed29b0b4SJens Axboe 
3189ed29b0b4SJens Axboe 	/* SQPOLL thread does its own polling */
3190ed29b0b4SJens Axboe 	if ((!(ctx->flags & IORING_SETUP_SQPOLL) && cancel_all) ||
3191ed29b0b4SJens Axboe 	    (ctx->sq_data && ctx->sq_data->thread == current)) {
3192ed29b0b4SJens Axboe 		while (!wq_list_empty(&ctx->iopoll_list)) {
3193ed29b0b4SJens Axboe 			io_iopoll_try_reap_events(ctx);
3194ed29b0b4SJens Axboe 			ret = true;
3195fcc926bbSJens Axboe 			cond_resched();
3196ed29b0b4SJens Axboe 		}
3197ed29b0b4SJens Axboe 	}
3198ed29b0b4SJens Axboe 
3199140102aeSPavel Begunkov 	if ((ctx->flags & IORING_SETUP_DEFER_TASKRUN) &&
3200140102aeSPavel Begunkov 	    io_allowed_defer_tw_run(ctx))
3201c0e0d6baSDylan Yudaken 		ret |= io_run_local_work(ctx) > 0;
3202ed29b0b4SJens Axboe 	ret |= io_cancel_defer_files(ctx, task, cancel_all);
32039ca9fb24SPavel Begunkov 	mutex_lock(&ctx->uring_lock);
3204ed29b0b4SJens Axboe 	ret |= io_poll_remove_all(ctx, task, cancel_all);
32059ca9fb24SPavel Begunkov 	mutex_unlock(&ctx->uring_lock);
3206ed29b0b4SJens Axboe 	ret |= io_kill_timeouts(ctx, task, cancel_all);
3207ed29b0b4SJens Axboe 	if (task)
3208c0e0d6baSDylan Yudaken 		ret |= io_run_task_work() > 0;
3209affa87dbSPavel Begunkov 	return ret;
3210ed29b0b4SJens Axboe }
3211ed29b0b4SJens Axboe 
3212ed29b0b4SJens Axboe static s64 tctx_inflight(struct io_uring_task *tctx, bool tracked)
3213ed29b0b4SJens Axboe {
3214ed29b0b4SJens Axboe 	if (tracked)
3215ed29b0b4SJens Axboe 		return atomic_read(&tctx->inflight_tracked);
3216ed29b0b4SJens Axboe 	return percpu_counter_sum(&tctx->inflight);
3217ed29b0b4SJens Axboe }
3218ed29b0b4SJens Axboe 
3219ed29b0b4SJens Axboe /*
3220ed29b0b4SJens Axboe  * Find any io_uring ctx that this task has registered or done IO on, and cancel
3221ed29b0b4SJens Axboe  * requests. @sqd should be not-null IFF it's an SQPOLL thread cancellation.
3222ed29b0b4SJens Axboe  */
322317437f31SJens Axboe __cold void io_uring_cancel_generic(bool cancel_all, struct io_sq_data *sqd)
3224ed29b0b4SJens Axboe {
3225ed29b0b4SJens Axboe 	struct io_uring_task *tctx = current->io_uring;
3226ed29b0b4SJens Axboe 	struct io_ring_ctx *ctx;
3227ed29b0b4SJens Axboe 	s64 inflight;
3228ed29b0b4SJens Axboe 	DEFINE_WAIT(wait);
3229ed29b0b4SJens Axboe 
3230ed29b0b4SJens Axboe 	WARN_ON_ONCE(sqd && sqd->thread != current);
3231ed29b0b4SJens Axboe 
3232ed29b0b4SJens Axboe 	if (!current->io_uring)
3233ed29b0b4SJens Axboe 		return;
3234ed29b0b4SJens Axboe 	if (tctx->io_wq)
3235ed29b0b4SJens Axboe 		io_wq_exit_start(tctx->io_wq);
3236ed29b0b4SJens Axboe 
32378d664282SJens Axboe 	atomic_inc(&tctx->in_cancel);
3238ed29b0b4SJens Axboe 	do {
3239affa87dbSPavel Begunkov 		bool loop = false;
3240affa87dbSPavel Begunkov 
3241ed29b0b4SJens Axboe 		io_uring_drop_tctx_refs(current);
3242ed29b0b4SJens Axboe 		/* read completions before cancelations */
3243ed29b0b4SJens Axboe 		inflight = tctx_inflight(tctx, !cancel_all);
3244ed29b0b4SJens Axboe 		if (!inflight)
3245ed29b0b4SJens Axboe 			break;
3246ed29b0b4SJens Axboe 
3247ed29b0b4SJens Axboe 		if (!sqd) {
3248ed29b0b4SJens Axboe 			struct io_tctx_node *node;
3249ed29b0b4SJens Axboe 			unsigned long index;
3250ed29b0b4SJens Axboe 
3251ed29b0b4SJens Axboe 			xa_for_each(&tctx->xa, index, node) {
3252ed29b0b4SJens Axboe 				/* sqpoll task will cancel all its requests */
3253ed29b0b4SJens Axboe 				if (node->ctx->sq_data)
3254ed29b0b4SJens Axboe 					continue;
3255affa87dbSPavel Begunkov 				loop |= io_uring_try_cancel_requests(node->ctx,
3256affa87dbSPavel Begunkov 							current, cancel_all);
3257ed29b0b4SJens Axboe 			}
3258ed29b0b4SJens Axboe 		} else {
3259ed29b0b4SJens Axboe 			list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
3260affa87dbSPavel Begunkov 				loop |= io_uring_try_cancel_requests(ctx,
3261affa87dbSPavel Begunkov 								     current,
3262ed29b0b4SJens Axboe 								     cancel_all);
3263ed29b0b4SJens Axboe 		}
3264ed29b0b4SJens Axboe 
3265affa87dbSPavel Begunkov 		if (loop) {
3266affa87dbSPavel Begunkov 			cond_resched();
3267affa87dbSPavel Begunkov 			continue;
3268affa87dbSPavel Begunkov 		}
3269affa87dbSPavel Begunkov 
3270ed29b0b4SJens Axboe 		prepare_to_wait(&tctx->wait, &wait, TASK_INTERRUPTIBLE);
3271ed29b0b4SJens Axboe 		io_run_task_work();
3272ed29b0b4SJens Axboe 		io_uring_drop_tctx_refs(current);
3273ed29b0b4SJens Axboe 
3274ed29b0b4SJens Axboe 		/*
3275ed29b0b4SJens Axboe 		 * If we've seen completions, retry without waiting. This
3276ed29b0b4SJens Axboe 		 * avoids a race where a completion comes in before we did
3277ed29b0b4SJens Axboe 		 * prepare_to_wait().
3278ed29b0b4SJens Axboe 		 */
3279ed29b0b4SJens Axboe 		if (inflight == tctx_inflight(tctx, !cancel_all))
3280ed29b0b4SJens Axboe 			schedule();
3281ed29b0b4SJens Axboe 		finish_wait(&tctx->wait, &wait);
3282ed29b0b4SJens Axboe 	} while (1);
3283ed29b0b4SJens Axboe 
3284ed29b0b4SJens Axboe 	io_uring_clean_tctx(tctx);
3285ed29b0b4SJens Axboe 	if (cancel_all) {
3286ed29b0b4SJens Axboe 		/*
3287ed29b0b4SJens Axboe 		 * We shouldn't run task_works after cancel, so just leave
32888d664282SJens Axboe 		 * ->in_cancel set for normal exit.
3289ed29b0b4SJens Axboe 		 */
32908d664282SJens Axboe 		atomic_dec(&tctx->in_cancel);
3291ed29b0b4SJens Axboe 		/* for exec all current's requests should be gone, kill tctx */
3292ed29b0b4SJens Axboe 		__io_uring_free(current);
3293ed29b0b4SJens Axboe 	}
3294ed29b0b4SJens Axboe }
3295ed29b0b4SJens Axboe 
3296ed29b0b4SJens Axboe void __io_uring_cancel(bool cancel_all)
3297ed29b0b4SJens Axboe {
3298ed29b0b4SJens Axboe 	io_uring_cancel_generic(cancel_all, NULL);
3299ed29b0b4SJens Axboe }
3300ed29b0b4SJens Axboe 
3301ed29b0b4SJens Axboe static void *io_uring_validate_mmap_request(struct file *file,
3302ed29b0b4SJens Axboe 					    loff_t pgoff, size_t sz)
3303ed29b0b4SJens Axboe {
3304ed29b0b4SJens Axboe 	struct io_ring_ctx *ctx = file->private_data;
3305ed29b0b4SJens Axboe 	loff_t offset = pgoff << PAGE_SHIFT;
3306ed29b0b4SJens Axboe 	struct page *page;
3307ed29b0b4SJens Axboe 	void *ptr;
3308ed29b0b4SJens Axboe 
3309c56e022cSJens Axboe 	switch (offset & IORING_OFF_MMAP_MASK) {
3310ed29b0b4SJens Axboe 	case IORING_OFF_SQ_RING:
3311ed29b0b4SJens Axboe 	case IORING_OFF_CQ_RING:
3312ed29b0b4SJens Axboe 		ptr = ctx->rings;
3313ed29b0b4SJens Axboe 		break;
3314ed29b0b4SJens Axboe 	case IORING_OFF_SQES:
3315ed29b0b4SJens Axboe 		ptr = ctx->sq_sqes;
3316ed29b0b4SJens Axboe 		break;
3317c56e022cSJens Axboe 	case IORING_OFF_PBUF_RING: {
3318c56e022cSJens Axboe 		unsigned int bgid;
3319c56e022cSJens Axboe 
3320c56e022cSJens Axboe 		bgid = (offset & ~IORING_OFF_MMAP_MASK) >> IORING_OFF_PBUF_SHIFT;
3321c56e022cSJens Axboe 		mutex_lock(&ctx->uring_lock);
3322c56e022cSJens Axboe 		ptr = io_pbuf_get_address(ctx, bgid);
3323c56e022cSJens Axboe 		mutex_unlock(&ctx->uring_lock);
3324c56e022cSJens Axboe 		if (!ptr)
3325c56e022cSJens Axboe 			return ERR_PTR(-EINVAL);
3326c56e022cSJens Axboe 		break;
3327c56e022cSJens Axboe 		}
3328ed29b0b4SJens Axboe 	default:
3329ed29b0b4SJens Axboe 		return ERR_PTR(-EINVAL);
3330ed29b0b4SJens Axboe 	}
3331ed29b0b4SJens Axboe 
3332ed29b0b4SJens Axboe 	page = virt_to_head_page(ptr);
3333ed29b0b4SJens Axboe 	if (sz > page_size(page))
3334ed29b0b4SJens Axboe 		return ERR_PTR(-EINVAL);
3335ed29b0b4SJens Axboe 
3336ed29b0b4SJens Axboe 	return ptr;
3337ed29b0b4SJens Axboe }
3338ed29b0b4SJens Axboe 
3339ed29b0b4SJens Axboe #ifdef CONFIG_MMU
3340ed29b0b4SJens Axboe 
3341ed29b0b4SJens Axboe static __cold int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
3342ed29b0b4SJens Axboe {
3343ed29b0b4SJens Axboe 	size_t sz = vma->vm_end - vma->vm_start;
3344ed29b0b4SJens Axboe 	unsigned long pfn;
3345ed29b0b4SJens Axboe 	void *ptr;
3346ed29b0b4SJens Axboe 
3347ed29b0b4SJens Axboe 	ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
3348ed29b0b4SJens Axboe 	if (IS_ERR(ptr))
3349ed29b0b4SJens Axboe 		return PTR_ERR(ptr);
3350ed29b0b4SJens Axboe 
3351ed29b0b4SJens Axboe 	pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
3352ed29b0b4SJens Axboe 	return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
3353ed29b0b4SJens Axboe }
3354ed29b0b4SJens Axboe 
3355d808459bSHelge Deller static unsigned long io_uring_mmu_get_unmapped_area(struct file *filp,
3356d808459bSHelge Deller 			unsigned long addr, unsigned long len,
3357d808459bSHelge Deller 			unsigned long pgoff, unsigned long flags)
3358d808459bSHelge Deller {
3359d808459bSHelge Deller 	const unsigned long mmap_end = arch_get_mmap_end(addr, len, flags);
3360d808459bSHelge Deller 	struct vm_unmapped_area_info info;
3361d808459bSHelge Deller 	void *ptr;
3362d808459bSHelge Deller 
3363d808459bSHelge Deller 	/*
3364d808459bSHelge Deller 	 * Do not allow to map to user-provided address to avoid breaking the
3365d808459bSHelge Deller 	 * aliasing rules. Userspace is not able to guess the offset address of
3366d808459bSHelge Deller 	 * kernel kmalloc()ed memory area.
3367d808459bSHelge Deller 	 */
3368d808459bSHelge Deller 	if (addr)
3369d808459bSHelge Deller 		return -EINVAL;
3370d808459bSHelge Deller 
3371d808459bSHelge Deller 	ptr = io_uring_validate_mmap_request(filp, pgoff, len);
3372d808459bSHelge Deller 	if (IS_ERR(ptr))
3373d808459bSHelge Deller 		return -ENOMEM;
3374d808459bSHelge Deller 
3375d808459bSHelge Deller 	info.flags = VM_UNMAPPED_AREA_TOPDOWN;
3376d808459bSHelge Deller 	info.length = len;
3377d808459bSHelge Deller 	info.low_limit = max(PAGE_SIZE, mmap_min_addr);
3378d808459bSHelge Deller 	info.high_limit = arch_get_mmap_base(addr, current->mm->mmap_base);
3379d808459bSHelge Deller #ifdef SHM_COLOUR
3380d808459bSHelge Deller 	info.align_mask = PAGE_MASK & (SHM_COLOUR - 1UL);
3381d808459bSHelge Deller #else
3382d808459bSHelge Deller 	info.align_mask = PAGE_MASK & (SHMLBA - 1UL);
3383d808459bSHelge Deller #endif
3384d808459bSHelge Deller 	info.align_offset = (unsigned long) ptr;
3385d808459bSHelge Deller 
3386d808459bSHelge Deller 	/*
3387d808459bSHelge Deller 	 * A failed mmap() very likely causes application failure,
3388d808459bSHelge Deller 	 * so fall back to the bottom-up function here. This scenario
3389d808459bSHelge Deller 	 * can happen with large stack limits and large mmap()
3390d808459bSHelge Deller 	 * allocations.
3391d808459bSHelge Deller 	 */
3392d808459bSHelge Deller 	addr = vm_unmapped_area(&info);
3393d808459bSHelge Deller 	if (offset_in_page(addr)) {
3394d808459bSHelge Deller 		info.flags = 0;
3395d808459bSHelge Deller 		info.low_limit = TASK_UNMAPPED_BASE;
3396d808459bSHelge Deller 		info.high_limit = mmap_end;
3397d808459bSHelge Deller 		addr = vm_unmapped_area(&info);
3398d808459bSHelge Deller 	}
3399d808459bSHelge Deller 
3400d808459bSHelge Deller 	return addr;
3401d808459bSHelge Deller }
3402d808459bSHelge Deller 
3403ed29b0b4SJens Axboe #else /* !CONFIG_MMU */
3404ed29b0b4SJens Axboe 
3405ed29b0b4SJens Axboe static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
3406ed29b0b4SJens Axboe {
3407fc4f4be9SDavid Hildenbrand 	return is_nommu_shared_mapping(vma->vm_flags) ? 0 : -EINVAL;
3408ed29b0b4SJens Axboe }
3409ed29b0b4SJens Axboe 
3410ed29b0b4SJens Axboe static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
3411ed29b0b4SJens Axboe {
3412ed29b0b4SJens Axboe 	return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
3413ed29b0b4SJens Axboe }
3414ed29b0b4SJens Axboe 
3415ed29b0b4SJens Axboe static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
3416ed29b0b4SJens Axboe 	unsigned long addr, unsigned long len,
3417ed29b0b4SJens Axboe 	unsigned long pgoff, unsigned long flags)
3418ed29b0b4SJens Axboe {
3419ed29b0b4SJens Axboe 	void *ptr;
3420ed29b0b4SJens Axboe 
3421ed29b0b4SJens Axboe 	ptr = io_uring_validate_mmap_request(file, pgoff, len);
3422ed29b0b4SJens Axboe 	if (IS_ERR(ptr))
3423ed29b0b4SJens Axboe 		return PTR_ERR(ptr);
3424ed29b0b4SJens Axboe 
3425ed29b0b4SJens Axboe 	return (unsigned long) ptr;
3426ed29b0b4SJens Axboe }
3427ed29b0b4SJens Axboe 
3428ed29b0b4SJens Axboe #endif /* !CONFIG_MMU */
3429ed29b0b4SJens Axboe 
3430ed29b0b4SJens Axboe static int io_validate_ext_arg(unsigned flags, const void __user *argp, size_t argsz)
3431ed29b0b4SJens Axboe {
3432ed29b0b4SJens Axboe 	if (flags & IORING_ENTER_EXT_ARG) {
3433ed29b0b4SJens Axboe 		struct io_uring_getevents_arg arg;
3434ed29b0b4SJens Axboe 
3435ed29b0b4SJens Axboe 		if (argsz != sizeof(arg))
3436ed29b0b4SJens Axboe 			return -EINVAL;
3437ed29b0b4SJens Axboe 		if (copy_from_user(&arg, argp, sizeof(arg)))
3438ed29b0b4SJens Axboe 			return -EFAULT;
3439ed29b0b4SJens Axboe 	}
3440ed29b0b4SJens Axboe 	return 0;
3441ed29b0b4SJens Axboe }
3442ed29b0b4SJens Axboe 
3443ed29b0b4SJens Axboe static int io_get_ext_arg(unsigned flags, const void __user *argp, size_t *argsz,
3444ed29b0b4SJens Axboe 			  struct __kernel_timespec __user **ts,
3445ed29b0b4SJens Axboe 			  const sigset_t __user **sig)
3446ed29b0b4SJens Axboe {
3447ed29b0b4SJens Axboe 	struct io_uring_getevents_arg arg;
3448ed29b0b4SJens Axboe 
3449ed29b0b4SJens Axboe 	/*
3450ed29b0b4SJens Axboe 	 * If EXT_ARG isn't set, then we have no timespec and the argp pointer
3451ed29b0b4SJens Axboe 	 * is just a pointer to the sigset_t.
3452ed29b0b4SJens Axboe 	 */
3453ed29b0b4SJens Axboe 	if (!(flags & IORING_ENTER_EXT_ARG)) {
3454ed29b0b4SJens Axboe 		*sig = (const sigset_t __user *) argp;
3455ed29b0b4SJens Axboe 		*ts = NULL;
3456ed29b0b4SJens Axboe 		return 0;
3457ed29b0b4SJens Axboe 	}
3458ed29b0b4SJens Axboe 
3459ed29b0b4SJens Axboe 	/*
3460ed29b0b4SJens Axboe 	 * EXT_ARG is set - ensure we agree on the size of it and copy in our
3461ed29b0b4SJens Axboe 	 * timespec and sigset_t pointers if good.
3462ed29b0b4SJens Axboe 	 */
3463ed29b0b4SJens Axboe 	if (*argsz != sizeof(arg))
3464ed29b0b4SJens Axboe 		return -EINVAL;
3465ed29b0b4SJens Axboe 	if (copy_from_user(&arg, argp, sizeof(arg)))
3466ed29b0b4SJens Axboe 		return -EFAULT;
3467ed29b0b4SJens Axboe 	if (arg.pad)
3468ed29b0b4SJens Axboe 		return -EINVAL;
3469ed29b0b4SJens Axboe 	*sig = u64_to_user_ptr(arg.sigmask);
3470ed29b0b4SJens Axboe 	*argsz = arg.sigmask_sz;
3471ed29b0b4SJens Axboe 	*ts = u64_to_user_ptr(arg.ts);
3472ed29b0b4SJens Axboe 	return 0;
3473ed29b0b4SJens Axboe }
3474ed29b0b4SJens Axboe 
3475ed29b0b4SJens Axboe SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
3476ed29b0b4SJens Axboe 		u32, min_complete, u32, flags, const void __user *, argp,
3477ed29b0b4SJens Axboe 		size_t, argsz)
3478ed29b0b4SJens Axboe {
3479ed29b0b4SJens Axboe 	struct io_ring_ctx *ctx;
3480ed29b0b4SJens Axboe 	struct fd f;
3481ed29b0b4SJens Axboe 	long ret;
3482ed29b0b4SJens Axboe 
3483ed29b0b4SJens Axboe 	if (unlikely(flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP |
3484ed29b0b4SJens Axboe 			       IORING_ENTER_SQ_WAIT | IORING_ENTER_EXT_ARG |
3485ed29b0b4SJens Axboe 			       IORING_ENTER_REGISTERED_RING)))
3486ed29b0b4SJens Axboe 		return -EINVAL;
3487ed29b0b4SJens Axboe 
3488ed29b0b4SJens Axboe 	/*
3489ed29b0b4SJens Axboe 	 * Ring fd has been registered via IORING_REGISTER_RING_FDS, we
3490ed29b0b4SJens Axboe 	 * need only dereference our task private array to find it.
3491ed29b0b4SJens Axboe 	 */
3492ed29b0b4SJens Axboe 	if (flags & IORING_ENTER_REGISTERED_RING) {
3493ed29b0b4SJens Axboe 		struct io_uring_task *tctx = current->io_uring;
3494ed29b0b4SJens Axboe 
34953273c440SPavel Begunkov 		if (unlikely(!tctx || fd >= IO_RINGFD_REG_MAX))
3496ed29b0b4SJens Axboe 			return -EINVAL;
3497ed29b0b4SJens Axboe 		fd = array_index_nospec(fd, IO_RINGFD_REG_MAX);
3498ed29b0b4SJens Axboe 		f.file = tctx->registered_rings[fd];
3499ed29b0b4SJens Axboe 		f.flags = 0;
3500ed29b0b4SJens Axboe 		if (unlikely(!f.file))
3501ed29b0b4SJens Axboe 			return -EBADF;
35023273c440SPavel Begunkov 	} else {
35033273c440SPavel Begunkov 		f = fdget(fd);
35043273c440SPavel Begunkov 		if (unlikely(!f.file))
35053273c440SPavel Begunkov 			return -EBADF;
3506ed29b0b4SJens Axboe 		ret = -EOPNOTSUPP;
3507e5550a14SJens Axboe 		if (unlikely(!io_is_uring_fops(f.file)))
3508fbb8bb02SPavel Begunkov 			goto out;
35093273c440SPavel Begunkov 	}
3510ed29b0b4SJens Axboe 
3511ed29b0b4SJens Axboe 	ctx = f.file->private_data;
3512ed29b0b4SJens Axboe 	ret = -EBADFD;
3513ed29b0b4SJens Axboe 	if (unlikely(ctx->flags & IORING_SETUP_R_DISABLED))
3514ed29b0b4SJens Axboe 		goto out;
3515ed29b0b4SJens Axboe 
3516ed29b0b4SJens Axboe 	/*
3517ed29b0b4SJens Axboe 	 * For SQ polling, the thread will do all submissions and completions.
3518ed29b0b4SJens Axboe 	 * Just return the requested submit count, and wake the thread if
3519ed29b0b4SJens Axboe 	 * we were asked to.
3520ed29b0b4SJens Axboe 	 */
3521ed29b0b4SJens Axboe 	ret = 0;
3522ed29b0b4SJens Axboe 	if (ctx->flags & IORING_SETUP_SQPOLL) {
3523ed29b0b4SJens Axboe 		io_cqring_overflow_flush(ctx);
3524ed29b0b4SJens Axboe 
3525ed29b0b4SJens Axboe 		if (unlikely(ctx->sq_data->thread == NULL)) {
3526ed29b0b4SJens Axboe 			ret = -EOWNERDEAD;
3527ed29b0b4SJens Axboe 			goto out;
3528ed29b0b4SJens Axboe 		}
3529ed29b0b4SJens Axboe 		if (flags & IORING_ENTER_SQ_WAKEUP)
3530ed29b0b4SJens Axboe 			wake_up(&ctx->sq_data->wait);
353188b80534SQuanfa Fu 		if (flags & IORING_ENTER_SQ_WAIT)
353288b80534SQuanfa Fu 			io_sqpoll_wait_sq(ctx);
353388b80534SQuanfa Fu 
3534ed29b0b4SJens Axboe 		ret = to_submit;
3535ed29b0b4SJens Axboe 	} else if (to_submit) {
3536ed29b0b4SJens Axboe 		ret = io_uring_add_tctx_node(ctx);
3537ed29b0b4SJens Axboe 		if (unlikely(ret))
3538ed29b0b4SJens Axboe 			goto out;
3539ed29b0b4SJens Axboe 
3540ed29b0b4SJens Axboe 		mutex_lock(&ctx->uring_lock);
3541ed29b0b4SJens Axboe 		ret = io_submit_sqes(ctx, to_submit);
3542ed29b0b4SJens Axboe 		if (ret != to_submit) {
3543ed29b0b4SJens Axboe 			mutex_unlock(&ctx->uring_lock);
3544ed29b0b4SJens Axboe 			goto out;
3545ed29b0b4SJens Axboe 		}
354644f87745SPavel Begunkov 		if (flags & IORING_ENTER_GETEVENTS) {
354744f87745SPavel Begunkov 			if (ctx->syscall_iopoll)
3548ed29b0b4SJens Axboe 				goto iopoll_locked;
354944f87745SPavel Begunkov 			/*
355044f87745SPavel Begunkov 			 * Ignore errors, we'll soon call io_cqring_wait() and
355144f87745SPavel Begunkov 			 * it should handle ownership problems if any.
355244f87745SPavel Begunkov 			 */
355344f87745SPavel Begunkov 			if (ctx->flags & IORING_SETUP_DEFER_TASKRUN)
355444f87745SPavel Begunkov 				(void)io_run_local_work_locked(ctx);
355544f87745SPavel Begunkov 		}
3556ed29b0b4SJens Axboe 		mutex_unlock(&ctx->uring_lock);
3557ed29b0b4SJens Axboe 	}
3558c0e0d6baSDylan Yudaken 
3559ed29b0b4SJens Axboe 	if (flags & IORING_ENTER_GETEVENTS) {
3560ed29b0b4SJens Axboe 		int ret2;
3561c0e0d6baSDylan Yudaken 
3562ed29b0b4SJens Axboe 		if (ctx->syscall_iopoll) {
3563ed29b0b4SJens Axboe 			/*
3564ed29b0b4SJens Axboe 			 * We disallow the app entering submit/complete with
3565ed29b0b4SJens Axboe 			 * polling, but we still need to lock the ring to
3566ed29b0b4SJens Axboe 			 * prevent racing with polled issue that got punted to
3567ed29b0b4SJens Axboe 			 * a workqueue.
3568ed29b0b4SJens Axboe 			 */
3569ed29b0b4SJens Axboe 			mutex_lock(&ctx->uring_lock);
3570ed29b0b4SJens Axboe iopoll_locked:
3571ed29b0b4SJens Axboe 			ret2 = io_validate_ext_arg(flags, argp, argsz);
3572ed29b0b4SJens Axboe 			if (likely(!ret2)) {
3573ed29b0b4SJens Axboe 				min_complete = min(min_complete,
3574ed29b0b4SJens Axboe 						   ctx->cq_entries);
3575ed29b0b4SJens Axboe 				ret2 = io_iopoll_check(ctx, min_complete);
3576ed29b0b4SJens Axboe 			}
3577ed29b0b4SJens Axboe 			mutex_unlock(&ctx->uring_lock);
3578ed29b0b4SJens Axboe 		} else {
3579ed29b0b4SJens Axboe 			const sigset_t __user *sig;
3580ed29b0b4SJens Axboe 			struct __kernel_timespec __user *ts;
3581ed29b0b4SJens Axboe 
3582ed29b0b4SJens Axboe 			ret2 = io_get_ext_arg(flags, argp, &argsz, &ts, &sig);
3583ed29b0b4SJens Axboe 			if (likely(!ret2)) {
3584ed29b0b4SJens Axboe 				min_complete = min(min_complete,
3585ed29b0b4SJens Axboe 						   ctx->cq_entries);
3586ed29b0b4SJens Axboe 				ret2 = io_cqring_wait(ctx, min_complete, sig,
3587ed29b0b4SJens Axboe 						      argsz, ts);
3588ed29b0b4SJens Axboe 			}
3589ed29b0b4SJens Axboe 		}
3590ed29b0b4SJens Axboe 
3591ed29b0b4SJens Axboe 		if (!ret) {
3592ed29b0b4SJens Axboe 			ret = ret2;
3593ed29b0b4SJens Axboe 
3594ed29b0b4SJens Axboe 			/*
3595ed29b0b4SJens Axboe 			 * EBADR indicates that one or more CQE were dropped.
3596ed29b0b4SJens Axboe 			 * Once the user has been informed we can clear the bit
3597ed29b0b4SJens Axboe 			 * as they are obviously ok with those drops.
3598ed29b0b4SJens Axboe 			 */
3599ed29b0b4SJens Axboe 			if (unlikely(ret2 == -EBADR))
3600ed29b0b4SJens Axboe 				clear_bit(IO_CHECK_CQ_DROPPED_BIT,
3601ed29b0b4SJens Axboe 					  &ctx->check_cq);
3602ed29b0b4SJens Axboe 		}
3603ed29b0b4SJens Axboe 	}
3604ed29b0b4SJens Axboe out:
3605ed29b0b4SJens Axboe 	fdput(f);
3606ed29b0b4SJens Axboe 	return ret;
3607ed29b0b4SJens Axboe }
3608ed29b0b4SJens Axboe 
3609ed29b0b4SJens Axboe static const struct file_operations io_uring_fops = {
3610ed29b0b4SJens Axboe 	.release	= io_uring_release,
3611ed29b0b4SJens Axboe 	.mmap		= io_uring_mmap,
3612ed29b0b4SJens Axboe #ifndef CONFIG_MMU
3613ed29b0b4SJens Axboe 	.get_unmapped_area = io_uring_nommu_get_unmapped_area,
3614ed29b0b4SJens Axboe 	.mmap_capabilities = io_uring_nommu_mmap_capabilities,
3615d808459bSHelge Deller #else
3616d808459bSHelge Deller 	.get_unmapped_area = io_uring_mmu_get_unmapped_area,
3617ed29b0b4SJens Axboe #endif
3618ed29b0b4SJens Axboe 	.poll		= io_uring_poll,
3619ed29b0b4SJens Axboe #ifdef CONFIG_PROC_FS
3620ed29b0b4SJens Axboe 	.show_fdinfo	= io_uring_show_fdinfo,
3621ed29b0b4SJens Axboe #endif
3622ed29b0b4SJens Axboe };
3623ed29b0b4SJens Axboe 
362492ac8beaSJens Axboe bool io_is_uring_fops(struct file *file)
362592ac8beaSJens Axboe {
362692ac8beaSJens Axboe 	return file->f_op == &io_uring_fops;
362792ac8beaSJens Axboe }
362892ac8beaSJens Axboe 
3629ed29b0b4SJens Axboe static __cold int io_allocate_scq_urings(struct io_ring_ctx *ctx,
3630ed29b0b4SJens Axboe 					 struct io_uring_params *p)
3631ed29b0b4SJens Axboe {
3632ed29b0b4SJens Axboe 	struct io_rings *rings;
3633ed29b0b4SJens Axboe 	size_t size, sq_array_offset;
3634ed29b0b4SJens Axboe 
3635ed29b0b4SJens Axboe 	/* make sure these are sane, as we already accounted them */
3636ed29b0b4SJens Axboe 	ctx->sq_entries = p->sq_entries;
3637ed29b0b4SJens Axboe 	ctx->cq_entries = p->cq_entries;
3638ed29b0b4SJens Axboe 
3639ed29b0b4SJens Axboe 	size = rings_size(ctx, p->sq_entries, p->cq_entries, &sq_array_offset);
3640ed29b0b4SJens Axboe 	if (size == SIZE_MAX)
3641ed29b0b4SJens Axboe 		return -EOVERFLOW;
3642ed29b0b4SJens Axboe 
3643ed29b0b4SJens Axboe 	rings = io_mem_alloc(size);
3644ed29b0b4SJens Axboe 	if (!rings)
3645ed29b0b4SJens Axboe 		return -ENOMEM;
3646ed29b0b4SJens Axboe 
3647ed29b0b4SJens Axboe 	ctx->rings = rings;
3648ed29b0b4SJens Axboe 	ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
3649ed29b0b4SJens Axboe 	rings->sq_ring_mask = p->sq_entries - 1;
3650ed29b0b4SJens Axboe 	rings->cq_ring_mask = p->cq_entries - 1;
3651ed29b0b4SJens Axboe 	rings->sq_ring_entries = p->sq_entries;
3652ed29b0b4SJens Axboe 	rings->cq_ring_entries = p->cq_entries;
3653ed29b0b4SJens Axboe 
3654ed29b0b4SJens Axboe 	if (p->flags & IORING_SETUP_SQE128)
3655ed29b0b4SJens Axboe 		size = array_size(2 * sizeof(struct io_uring_sqe), p->sq_entries);
3656ed29b0b4SJens Axboe 	else
3657ed29b0b4SJens Axboe 		size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
3658ed29b0b4SJens Axboe 	if (size == SIZE_MAX) {
3659ed29b0b4SJens Axboe 		io_mem_free(ctx->rings);
3660ed29b0b4SJens Axboe 		ctx->rings = NULL;
3661ed29b0b4SJens Axboe 		return -EOVERFLOW;
3662ed29b0b4SJens Axboe 	}
3663ed29b0b4SJens Axboe 
3664ed29b0b4SJens Axboe 	ctx->sq_sqes = io_mem_alloc(size);
3665ed29b0b4SJens Axboe 	if (!ctx->sq_sqes) {
3666ed29b0b4SJens Axboe 		io_mem_free(ctx->rings);
3667ed29b0b4SJens Axboe 		ctx->rings = NULL;
3668ed29b0b4SJens Axboe 		return -ENOMEM;
3669ed29b0b4SJens Axboe 	}
3670ed29b0b4SJens Axboe 
3671ed29b0b4SJens Axboe 	return 0;
3672ed29b0b4SJens Axboe }
3673ed29b0b4SJens Axboe 
3674ed29b0b4SJens Axboe static int io_uring_install_fd(struct io_ring_ctx *ctx, struct file *file)
3675ed29b0b4SJens Axboe {
3676ed29b0b4SJens Axboe 	int ret, fd;
3677ed29b0b4SJens Axboe 
3678ed29b0b4SJens Axboe 	fd = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
3679ed29b0b4SJens Axboe 	if (fd < 0)
3680ed29b0b4SJens Axboe 		return fd;
3681ed29b0b4SJens Axboe 
368297c96e9fSDylan Yudaken 	ret = __io_uring_add_tctx_node(ctx);
3683ed29b0b4SJens Axboe 	if (ret) {
3684ed29b0b4SJens Axboe 		put_unused_fd(fd);
3685ed29b0b4SJens Axboe 		return ret;
3686ed29b0b4SJens Axboe 	}
3687ed29b0b4SJens Axboe 	fd_install(fd, file);
3688ed29b0b4SJens Axboe 	return fd;
3689ed29b0b4SJens Axboe }
3690ed29b0b4SJens Axboe 
3691ed29b0b4SJens Axboe /*
3692ed29b0b4SJens Axboe  * Allocate an anonymous fd, this is what constitutes the application
3693ed29b0b4SJens Axboe  * visible backing of an io_uring instance. The application mmaps this
3694ed29b0b4SJens Axboe  * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
3695ed29b0b4SJens Axboe  * we have to tie this fd to a socket for file garbage collection purposes.
3696ed29b0b4SJens Axboe  */
3697ed29b0b4SJens Axboe static struct file *io_uring_get_file(struct io_ring_ctx *ctx)
3698ed29b0b4SJens Axboe {
3699ed29b0b4SJens Axboe 	struct file *file;
3700ed29b0b4SJens Axboe #if defined(CONFIG_UNIX)
3701ed29b0b4SJens Axboe 	int ret;
3702ed29b0b4SJens Axboe 
3703ed29b0b4SJens Axboe 	ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
3704ed29b0b4SJens Axboe 				&ctx->ring_sock);
3705ed29b0b4SJens Axboe 	if (ret)
3706ed29b0b4SJens Axboe 		return ERR_PTR(ret);
3707ed29b0b4SJens Axboe #endif
3708ed29b0b4SJens Axboe 
3709ed29b0b4SJens Axboe 	file = anon_inode_getfile_secure("[io_uring]", &io_uring_fops, ctx,
3710ed29b0b4SJens Axboe 					 O_RDWR | O_CLOEXEC, NULL);
3711ed29b0b4SJens Axboe #if defined(CONFIG_UNIX)
3712ed29b0b4SJens Axboe 	if (IS_ERR(file)) {
3713ed29b0b4SJens Axboe 		sock_release(ctx->ring_sock);
3714ed29b0b4SJens Axboe 		ctx->ring_sock = NULL;
3715ed29b0b4SJens Axboe 	} else {
3716ed29b0b4SJens Axboe 		ctx->ring_sock->file = file;
3717ed29b0b4SJens Axboe 	}
3718ed29b0b4SJens Axboe #endif
3719ed29b0b4SJens Axboe 	return file;
3720ed29b0b4SJens Axboe }
3721ed29b0b4SJens Axboe 
3722ed29b0b4SJens Axboe static __cold int io_uring_create(unsigned entries, struct io_uring_params *p,
3723ed29b0b4SJens Axboe 				  struct io_uring_params __user *params)
3724ed29b0b4SJens Axboe {
3725ed29b0b4SJens Axboe 	struct io_ring_ctx *ctx;
3726ed29b0b4SJens Axboe 	struct file *file;
3727ed29b0b4SJens Axboe 	int ret;
3728ed29b0b4SJens Axboe 
3729ed29b0b4SJens Axboe 	if (!entries)
3730ed29b0b4SJens Axboe 		return -EINVAL;
3731ed29b0b4SJens Axboe 	if (entries > IORING_MAX_ENTRIES) {
3732ed29b0b4SJens Axboe 		if (!(p->flags & IORING_SETUP_CLAMP))
3733ed29b0b4SJens Axboe 			return -EINVAL;
3734ed29b0b4SJens Axboe 		entries = IORING_MAX_ENTRIES;
3735ed29b0b4SJens Axboe 	}
3736ed29b0b4SJens Axboe 
3737ed29b0b4SJens Axboe 	/*
3738ed29b0b4SJens Axboe 	 * Use twice as many entries for the CQ ring. It's possible for the
3739ed29b0b4SJens Axboe 	 * application to drive a higher depth than the size of the SQ ring,
3740ed29b0b4SJens Axboe 	 * since the sqes are only used at submission time. This allows for
3741ed29b0b4SJens Axboe 	 * some flexibility in overcommitting a bit. If the application has
3742ed29b0b4SJens Axboe 	 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
3743ed29b0b4SJens Axboe 	 * of CQ ring entries manually.
3744ed29b0b4SJens Axboe 	 */
3745ed29b0b4SJens Axboe 	p->sq_entries = roundup_pow_of_two(entries);
3746ed29b0b4SJens Axboe 	if (p->flags & IORING_SETUP_CQSIZE) {
3747ed29b0b4SJens Axboe 		/*
3748ed29b0b4SJens Axboe 		 * If IORING_SETUP_CQSIZE is set, we do the same roundup
3749ed29b0b4SJens Axboe 		 * to a power-of-two, if it isn't already. We do NOT impose
3750ed29b0b4SJens Axboe 		 * any cq vs sq ring sizing.
3751ed29b0b4SJens Axboe 		 */
3752ed29b0b4SJens Axboe 		if (!p->cq_entries)
3753ed29b0b4SJens Axboe 			return -EINVAL;
3754ed29b0b4SJens Axboe 		if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
3755ed29b0b4SJens Axboe 			if (!(p->flags & IORING_SETUP_CLAMP))
3756ed29b0b4SJens Axboe 				return -EINVAL;
3757ed29b0b4SJens Axboe 			p->cq_entries = IORING_MAX_CQ_ENTRIES;
3758ed29b0b4SJens Axboe 		}
3759ed29b0b4SJens Axboe 		p->cq_entries = roundup_pow_of_two(p->cq_entries);
3760ed29b0b4SJens Axboe 		if (p->cq_entries < p->sq_entries)
3761ed29b0b4SJens Axboe 			return -EINVAL;
3762ed29b0b4SJens Axboe 	} else {
3763ed29b0b4SJens Axboe 		p->cq_entries = 2 * p->sq_entries;
3764ed29b0b4SJens Axboe 	}
3765ed29b0b4SJens Axboe 
3766ed29b0b4SJens Axboe 	ctx = io_ring_ctx_alloc(p);
3767ed29b0b4SJens Axboe 	if (!ctx)
3768ed29b0b4SJens Axboe 		return -ENOMEM;
3769ed29b0b4SJens Axboe 
3770e6aeb272SPavel Begunkov 	if ((ctx->flags & IORING_SETUP_DEFER_TASKRUN) &&
3771e6aeb272SPavel Begunkov 	    !(ctx->flags & IORING_SETUP_IOPOLL) &&
3772e6aeb272SPavel Begunkov 	    !(ctx->flags & IORING_SETUP_SQPOLL))
3773e6aeb272SPavel Begunkov 		ctx->task_complete = true;
3774e6aeb272SPavel Begunkov 
3775ed29b0b4SJens Axboe 	/*
3776bca39f39SPavel Begunkov 	 * lazy poll_wq activation relies on ->task_complete for synchronisation
3777bca39f39SPavel Begunkov 	 * purposes, see io_activate_pollwq()
3778bca39f39SPavel Begunkov 	 */
3779bca39f39SPavel Begunkov 	if (!ctx->task_complete)
3780bca39f39SPavel Begunkov 		ctx->poll_activated = true;
3781bca39f39SPavel Begunkov 
3782bca39f39SPavel Begunkov 	/*
3783ed29b0b4SJens Axboe 	 * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user
3784ed29b0b4SJens Axboe 	 * space applications don't need to do io completion events
3785ed29b0b4SJens Axboe 	 * polling again, they can rely on io_sq_thread to do polling
3786ed29b0b4SJens Axboe 	 * work, which can reduce cpu usage and uring_lock contention.
3787ed29b0b4SJens Axboe 	 */
3788ed29b0b4SJens Axboe 	if (ctx->flags & IORING_SETUP_IOPOLL &&
3789ed29b0b4SJens Axboe 	    !(ctx->flags & IORING_SETUP_SQPOLL))
3790ed29b0b4SJens Axboe 		ctx->syscall_iopoll = 1;
3791ed29b0b4SJens Axboe 
3792ed29b0b4SJens Axboe 	ctx->compat = in_compat_syscall();
3793ed29b0b4SJens Axboe 	if (!capable(CAP_IPC_LOCK))
3794ed29b0b4SJens Axboe 		ctx->user = get_uid(current_user());
3795ed29b0b4SJens Axboe 
3796ed29b0b4SJens Axboe 	/*
3797ed29b0b4SJens Axboe 	 * For SQPOLL, we just need a wakeup, always. For !SQPOLL, if
3798ed29b0b4SJens Axboe 	 * COOP_TASKRUN is set, then IPIs are never needed by the app.
3799ed29b0b4SJens Axboe 	 */
3800ed29b0b4SJens Axboe 	ret = -EINVAL;
3801ed29b0b4SJens Axboe 	if (ctx->flags & IORING_SETUP_SQPOLL) {
3802ed29b0b4SJens Axboe 		/* IPI related flags don't make sense with SQPOLL */
3803ed29b0b4SJens Axboe 		if (ctx->flags & (IORING_SETUP_COOP_TASKRUN |
3804c0e0d6baSDylan Yudaken 				  IORING_SETUP_TASKRUN_FLAG |
3805c0e0d6baSDylan Yudaken 				  IORING_SETUP_DEFER_TASKRUN))
3806ed29b0b4SJens Axboe 			goto err;
3807ed29b0b4SJens Axboe 		ctx->notify_method = TWA_SIGNAL_NO_IPI;
3808ed29b0b4SJens Axboe 	} else if (ctx->flags & IORING_SETUP_COOP_TASKRUN) {
3809ed29b0b4SJens Axboe 		ctx->notify_method = TWA_SIGNAL_NO_IPI;
3810ed29b0b4SJens Axboe 	} else {
3811c0e0d6baSDylan Yudaken 		if (ctx->flags & IORING_SETUP_TASKRUN_FLAG &&
3812c0e0d6baSDylan Yudaken 		    !(ctx->flags & IORING_SETUP_DEFER_TASKRUN))
3813ed29b0b4SJens Axboe 			goto err;
3814ed29b0b4SJens Axboe 		ctx->notify_method = TWA_SIGNAL;
3815ed29b0b4SJens Axboe 	}
3816ed29b0b4SJens Axboe 
3817ed29b0b4SJens Axboe 	/*
3818c0e0d6baSDylan Yudaken 	 * For DEFER_TASKRUN we require the completion task to be the same as the
3819c0e0d6baSDylan Yudaken 	 * submission task. This implies that there is only one submitter, so enforce
3820c0e0d6baSDylan Yudaken 	 * that.
3821c0e0d6baSDylan Yudaken 	 */
3822c0e0d6baSDylan Yudaken 	if (ctx->flags & IORING_SETUP_DEFER_TASKRUN &&
3823c0e0d6baSDylan Yudaken 	    !(ctx->flags & IORING_SETUP_SINGLE_ISSUER)) {
3824c0e0d6baSDylan Yudaken 		goto err;
3825c0e0d6baSDylan Yudaken 	}
3826c0e0d6baSDylan Yudaken 
3827c0e0d6baSDylan Yudaken 	/*
3828ed29b0b4SJens Axboe 	 * This is just grabbed for accounting purposes. When a process exits,
3829ed29b0b4SJens Axboe 	 * the mm is exited and dropped before the files, hence we need to hang
3830ed29b0b4SJens Axboe 	 * on to this mm purely for the purposes of being able to unaccount
3831ed29b0b4SJens Axboe 	 * memory (locked/pinned vm). It's not used for anything else.
3832ed29b0b4SJens Axboe 	 */
3833ed29b0b4SJens Axboe 	mmgrab(current->mm);
3834ed29b0b4SJens Axboe 	ctx->mm_account = current->mm;
3835ed29b0b4SJens Axboe 
3836ed29b0b4SJens Axboe 	ret = io_allocate_scq_urings(ctx, p);
3837ed29b0b4SJens Axboe 	if (ret)
3838ed29b0b4SJens Axboe 		goto err;
3839ed29b0b4SJens Axboe 
3840ed29b0b4SJens Axboe 	ret = io_sq_offload_create(ctx, p);
3841ed29b0b4SJens Axboe 	if (ret)
3842ed29b0b4SJens Axboe 		goto err;
3843ed29b0b4SJens Axboe 	/* always set a rsrc node */
3844ed29b0b4SJens Axboe 	ret = io_rsrc_node_switch_start(ctx);
3845ed29b0b4SJens Axboe 	if (ret)
3846ed29b0b4SJens Axboe 		goto err;
3847ed29b0b4SJens Axboe 	io_rsrc_node_switch(ctx, NULL);
3848ed29b0b4SJens Axboe 
3849ed29b0b4SJens Axboe 	memset(&p->sq_off, 0, sizeof(p->sq_off));
3850ed29b0b4SJens Axboe 	p->sq_off.head = offsetof(struct io_rings, sq.head);
3851ed29b0b4SJens Axboe 	p->sq_off.tail = offsetof(struct io_rings, sq.tail);
3852ed29b0b4SJens Axboe 	p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
3853ed29b0b4SJens Axboe 	p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
3854ed29b0b4SJens Axboe 	p->sq_off.flags = offsetof(struct io_rings, sq_flags);
3855ed29b0b4SJens Axboe 	p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
3856ed29b0b4SJens Axboe 	p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
3857ed29b0b4SJens Axboe 
3858ed29b0b4SJens Axboe 	memset(&p->cq_off, 0, sizeof(p->cq_off));
3859ed29b0b4SJens Axboe 	p->cq_off.head = offsetof(struct io_rings, cq.head);
3860ed29b0b4SJens Axboe 	p->cq_off.tail = offsetof(struct io_rings, cq.tail);
3861ed29b0b4SJens Axboe 	p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
3862ed29b0b4SJens Axboe 	p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
3863ed29b0b4SJens Axboe 	p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
3864ed29b0b4SJens Axboe 	p->cq_off.cqes = offsetof(struct io_rings, cqes);
3865ed29b0b4SJens Axboe 	p->cq_off.flags = offsetof(struct io_rings, cq_flags);
3866ed29b0b4SJens Axboe 
3867ed29b0b4SJens Axboe 	p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
3868ed29b0b4SJens Axboe 			IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
3869ed29b0b4SJens Axboe 			IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL |
3870ed29b0b4SJens Axboe 			IORING_FEAT_POLL_32BITS | IORING_FEAT_SQPOLL_NONFIXED |
3871ed29b0b4SJens Axboe 			IORING_FEAT_EXT_ARG | IORING_FEAT_NATIVE_WORKERS |
3872ed29b0b4SJens Axboe 			IORING_FEAT_RSRC_TAGS | IORING_FEAT_CQE_SKIP |
38737d3fd88dSJosh Triplett 			IORING_FEAT_LINKED_FILE | IORING_FEAT_REG_REG_RING;
3874ed29b0b4SJens Axboe 
3875ed29b0b4SJens Axboe 	if (copy_to_user(params, p, sizeof(*p))) {
3876ed29b0b4SJens Axboe 		ret = -EFAULT;
3877ed29b0b4SJens Axboe 		goto err;
3878ed29b0b4SJens Axboe 	}
3879ed29b0b4SJens Axboe 
38807cae596bSDylan Yudaken 	if (ctx->flags & IORING_SETUP_SINGLE_ISSUER
38817cae596bSDylan Yudaken 	    && !(ctx->flags & IORING_SETUP_R_DISABLED))
38828579538cSPavel Begunkov 		WRITE_ONCE(ctx->submitter_task, get_task_struct(current));
38837cae596bSDylan Yudaken 
3884ed29b0b4SJens Axboe 	file = io_uring_get_file(ctx);
3885ed29b0b4SJens Axboe 	if (IS_ERR(file)) {
3886ed29b0b4SJens Axboe 		ret = PTR_ERR(file);
3887ed29b0b4SJens Axboe 		goto err;
3888ed29b0b4SJens Axboe 	}
3889ed29b0b4SJens Axboe 
3890ed29b0b4SJens Axboe 	/*
3891ed29b0b4SJens Axboe 	 * Install ring fd as the very last thing, so we don't risk someone
3892ed29b0b4SJens Axboe 	 * having closed it before we finish setup
3893ed29b0b4SJens Axboe 	 */
3894ed29b0b4SJens Axboe 	ret = io_uring_install_fd(ctx, file);
3895ed29b0b4SJens Axboe 	if (ret < 0) {
3896ed29b0b4SJens Axboe 		/* fput will clean it up */
3897ed29b0b4SJens Axboe 		fput(file);
3898ed29b0b4SJens Axboe 		return ret;
3899ed29b0b4SJens Axboe 	}
3900ed29b0b4SJens Axboe 
3901ed29b0b4SJens Axboe 	trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
3902ed29b0b4SJens Axboe 	return ret;
3903ed29b0b4SJens Axboe err:
3904ed29b0b4SJens Axboe 	io_ring_ctx_wait_and_kill(ctx);
3905ed29b0b4SJens Axboe 	return ret;
3906ed29b0b4SJens Axboe }
3907ed29b0b4SJens Axboe 
3908ed29b0b4SJens Axboe /*
3909ed29b0b4SJens Axboe  * Sets up an aio uring context, and returns the fd. Applications asks for a
3910ed29b0b4SJens Axboe  * ring size, we return the actual sq/cq ring sizes (among other things) in the
3911ed29b0b4SJens Axboe  * params structure passed in.
3912ed29b0b4SJens Axboe  */
3913ed29b0b4SJens Axboe static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
3914ed29b0b4SJens Axboe {
3915ed29b0b4SJens Axboe 	struct io_uring_params p;
3916ed29b0b4SJens Axboe 	int i;
3917ed29b0b4SJens Axboe 
3918ed29b0b4SJens Axboe 	if (copy_from_user(&p, params, sizeof(p)))
3919ed29b0b4SJens Axboe 		return -EFAULT;
3920ed29b0b4SJens Axboe 	for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
3921ed29b0b4SJens Axboe 		if (p.resv[i])
3922ed29b0b4SJens Axboe 			return -EINVAL;
3923ed29b0b4SJens Axboe 	}
3924ed29b0b4SJens Axboe 
3925ed29b0b4SJens Axboe 	if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
3926ed29b0b4SJens Axboe 			IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
3927ed29b0b4SJens Axboe 			IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ |
3928ed29b0b4SJens Axboe 			IORING_SETUP_R_DISABLED | IORING_SETUP_SUBMIT_ALL |
3929ed29b0b4SJens Axboe 			IORING_SETUP_COOP_TASKRUN | IORING_SETUP_TASKRUN_FLAG |
393097bbdc06SPavel Begunkov 			IORING_SETUP_SQE128 | IORING_SETUP_CQE32 |
3931c0e0d6baSDylan Yudaken 			IORING_SETUP_SINGLE_ISSUER | IORING_SETUP_DEFER_TASKRUN))
3932ed29b0b4SJens Axboe 		return -EINVAL;
3933ed29b0b4SJens Axboe 
3934ed29b0b4SJens Axboe 	return io_uring_create(entries, &p, params);
3935ed29b0b4SJens Axboe }
3936ed29b0b4SJens Axboe 
3937ed29b0b4SJens Axboe SYSCALL_DEFINE2(io_uring_setup, u32, entries,
3938ed29b0b4SJens Axboe 		struct io_uring_params __user *, params)
3939ed29b0b4SJens Axboe {
3940ed29b0b4SJens Axboe 	return io_uring_setup(entries, params);
3941ed29b0b4SJens Axboe }
3942ed29b0b4SJens Axboe 
3943ed29b0b4SJens Axboe static __cold int io_probe(struct io_ring_ctx *ctx, void __user *arg,
3944ed29b0b4SJens Axboe 			   unsigned nr_args)
3945ed29b0b4SJens Axboe {
3946ed29b0b4SJens Axboe 	struct io_uring_probe *p;
3947ed29b0b4SJens Axboe 	size_t size;
3948ed29b0b4SJens Axboe 	int i, ret;
3949ed29b0b4SJens Axboe 
3950ed29b0b4SJens Axboe 	size = struct_size(p, ops, nr_args);
3951ed29b0b4SJens Axboe 	if (size == SIZE_MAX)
3952ed29b0b4SJens Axboe 		return -EOVERFLOW;
3953ed29b0b4SJens Axboe 	p = kzalloc(size, GFP_KERNEL);
3954ed29b0b4SJens Axboe 	if (!p)
3955ed29b0b4SJens Axboe 		return -ENOMEM;
3956ed29b0b4SJens Axboe 
3957ed29b0b4SJens Axboe 	ret = -EFAULT;
3958ed29b0b4SJens Axboe 	if (copy_from_user(p, arg, size))
3959ed29b0b4SJens Axboe 		goto out;
3960ed29b0b4SJens Axboe 	ret = -EINVAL;
3961ed29b0b4SJens Axboe 	if (memchr_inv(p, 0, size))
3962ed29b0b4SJens Axboe 		goto out;
3963ed29b0b4SJens Axboe 
3964ed29b0b4SJens Axboe 	p->last_op = IORING_OP_LAST - 1;
3965ed29b0b4SJens Axboe 	if (nr_args > IORING_OP_LAST)
3966ed29b0b4SJens Axboe 		nr_args = IORING_OP_LAST;
3967ed29b0b4SJens Axboe 
3968ed29b0b4SJens Axboe 	for (i = 0; i < nr_args; i++) {
3969ed29b0b4SJens Axboe 		p->ops[i].op = i;
3970a7dd2782SBreno Leitao 		if (!io_issue_defs[i].not_supported)
3971ed29b0b4SJens Axboe 			p->ops[i].flags = IO_URING_OP_SUPPORTED;
3972ed29b0b4SJens Axboe 	}
3973ed29b0b4SJens Axboe 	p->ops_len = i;
3974ed29b0b4SJens Axboe 
3975ed29b0b4SJens Axboe 	ret = 0;
3976ed29b0b4SJens Axboe 	if (copy_to_user(arg, p, size))
3977ed29b0b4SJens Axboe 		ret = -EFAULT;
3978ed29b0b4SJens Axboe out:
3979ed29b0b4SJens Axboe 	kfree(p);
3980ed29b0b4SJens Axboe 	return ret;
3981ed29b0b4SJens Axboe }
3982ed29b0b4SJens Axboe 
3983ed29b0b4SJens Axboe static int io_register_personality(struct io_ring_ctx *ctx)
3984ed29b0b4SJens Axboe {
3985ed29b0b4SJens Axboe 	const struct cred *creds;
3986ed29b0b4SJens Axboe 	u32 id;
3987ed29b0b4SJens Axboe 	int ret;
3988ed29b0b4SJens Axboe 
3989ed29b0b4SJens Axboe 	creds = get_current_cred();
3990ed29b0b4SJens Axboe 
3991ed29b0b4SJens Axboe 	ret = xa_alloc_cyclic(&ctx->personalities, &id, (void *)creds,
3992ed29b0b4SJens Axboe 			XA_LIMIT(0, USHRT_MAX), &ctx->pers_next, GFP_KERNEL);
3993ed29b0b4SJens Axboe 	if (ret < 0) {
3994ed29b0b4SJens Axboe 		put_cred(creds);
3995ed29b0b4SJens Axboe 		return ret;
3996ed29b0b4SJens Axboe 	}
3997ed29b0b4SJens Axboe 	return id;
3998ed29b0b4SJens Axboe }
3999ed29b0b4SJens Axboe 
4000ed29b0b4SJens Axboe static __cold int io_register_restrictions(struct io_ring_ctx *ctx,
4001ed29b0b4SJens Axboe 					   void __user *arg, unsigned int nr_args)
4002ed29b0b4SJens Axboe {
4003ed29b0b4SJens Axboe 	struct io_uring_restriction *res;
4004ed29b0b4SJens Axboe 	size_t size;
4005ed29b0b4SJens Axboe 	int i, ret;
4006ed29b0b4SJens Axboe 
4007ed29b0b4SJens Axboe 	/* Restrictions allowed only if rings started disabled */
4008ed29b0b4SJens Axboe 	if (!(ctx->flags & IORING_SETUP_R_DISABLED))
4009ed29b0b4SJens Axboe 		return -EBADFD;
4010ed29b0b4SJens Axboe 
4011ed29b0b4SJens Axboe 	/* We allow only a single restrictions registration */
4012ed29b0b4SJens Axboe 	if (ctx->restrictions.registered)
4013ed29b0b4SJens Axboe 		return -EBUSY;
4014ed29b0b4SJens Axboe 
4015ed29b0b4SJens Axboe 	if (!arg || nr_args > IORING_MAX_RESTRICTIONS)
4016ed29b0b4SJens Axboe 		return -EINVAL;
4017ed29b0b4SJens Axboe 
4018ed29b0b4SJens Axboe 	size = array_size(nr_args, sizeof(*res));
4019ed29b0b4SJens Axboe 	if (size == SIZE_MAX)
4020ed29b0b4SJens Axboe 		return -EOVERFLOW;
4021ed29b0b4SJens Axboe 
4022ed29b0b4SJens Axboe 	res = memdup_user(arg, size);
4023ed29b0b4SJens Axboe 	if (IS_ERR(res))
4024ed29b0b4SJens Axboe 		return PTR_ERR(res);
4025ed29b0b4SJens Axboe 
4026ed29b0b4SJens Axboe 	ret = 0;
4027ed29b0b4SJens Axboe 
4028ed29b0b4SJens Axboe 	for (i = 0; i < nr_args; i++) {
4029ed29b0b4SJens Axboe 		switch (res[i].opcode) {
4030ed29b0b4SJens Axboe 		case IORING_RESTRICTION_REGISTER_OP:
4031ed29b0b4SJens Axboe 			if (res[i].register_op >= IORING_REGISTER_LAST) {
4032ed29b0b4SJens Axboe 				ret = -EINVAL;
4033ed29b0b4SJens Axboe 				goto out;
4034ed29b0b4SJens Axboe 			}
4035ed29b0b4SJens Axboe 
4036ed29b0b4SJens Axboe 			__set_bit(res[i].register_op,
4037ed29b0b4SJens Axboe 				  ctx->restrictions.register_op);
4038ed29b0b4SJens Axboe 			break;
4039ed29b0b4SJens Axboe 		case IORING_RESTRICTION_SQE_OP:
4040ed29b0b4SJens Axboe 			if (res[i].sqe_op >= IORING_OP_LAST) {
4041ed29b0b4SJens Axboe 				ret = -EINVAL;
4042ed29b0b4SJens Axboe 				goto out;
4043ed29b0b4SJens Axboe 			}
4044ed29b0b4SJens Axboe 
4045ed29b0b4SJens Axboe 			__set_bit(res[i].sqe_op, ctx->restrictions.sqe_op);
4046ed29b0b4SJens Axboe 			break;
4047ed29b0b4SJens Axboe 		case IORING_RESTRICTION_SQE_FLAGS_ALLOWED:
4048ed29b0b4SJens Axboe 			ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags;
4049ed29b0b4SJens Axboe 			break;
4050ed29b0b4SJens Axboe 		case IORING_RESTRICTION_SQE_FLAGS_REQUIRED:
4051ed29b0b4SJens Axboe 			ctx->restrictions.sqe_flags_required = res[i].sqe_flags;
4052ed29b0b4SJens Axboe 			break;
4053ed29b0b4SJens Axboe 		default:
4054ed29b0b4SJens Axboe 			ret = -EINVAL;
4055ed29b0b4SJens Axboe 			goto out;
4056ed29b0b4SJens Axboe 		}
4057ed29b0b4SJens Axboe 	}
4058ed29b0b4SJens Axboe 
4059ed29b0b4SJens Axboe out:
4060ed29b0b4SJens Axboe 	/* Reset all restrictions if an error happened */
4061ed29b0b4SJens Axboe 	if (ret != 0)
4062ed29b0b4SJens Axboe 		memset(&ctx->restrictions, 0, sizeof(ctx->restrictions));
4063ed29b0b4SJens Axboe 	else
4064ed29b0b4SJens Axboe 		ctx->restrictions.registered = true;
4065ed29b0b4SJens Axboe 
4066ed29b0b4SJens Axboe 	kfree(res);
4067ed29b0b4SJens Axboe 	return ret;
4068ed29b0b4SJens Axboe }
4069ed29b0b4SJens Axboe 
4070ed29b0b4SJens Axboe static int io_register_enable_rings(struct io_ring_ctx *ctx)
4071ed29b0b4SJens Axboe {
4072ed29b0b4SJens Axboe 	if (!(ctx->flags & IORING_SETUP_R_DISABLED))
4073ed29b0b4SJens Axboe 		return -EBADFD;
4074ed29b0b4SJens Axboe 
4075bca39f39SPavel Begunkov 	if (ctx->flags & IORING_SETUP_SINGLE_ISSUER && !ctx->submitter_task) {
40768579538cSPavel Begunkov 		WRITE_ONCE(ctx->submitter_task, get_task_struct(current));
4077bca39f39SPavel Begunkov 		/*
4078bca39f39SPavel Begunkov 		 * Lazy activation attempts would fail if it was polled before
4079bca39f39SPavel Begunkov 		 * submitter_task is set.
4080bca39f39SPavel Begunkov 		 */
4081bca39f39SPavel Begunkov 		if (wq_has_sleeper(&ctx->poll_wq))
4082bca39f39SPavel Begunkov 			io_activate_pollwq(ctx);
4083bca39f39SPavel Begunkov 	}
40847cae596bSDylan Yudaken 
4085ed29b0b4SJens Axboe 	if (ctx->restrictions.registered)
4086ed29b0b4SJens Axboe 		ctx->restricted = 1;
4087ed29b0b4SJens Axboe 
4088ed29b0b4SJens Axboe 	ctx->flags &= ~IORING_SETUP_R_DISABLED;
4089ed29b0b4SJens Axboe 	if (ctx->sq_data && wq_has_sleeper(&ctx->sq_data->wait))
4090ed29b0b4SJens Axboe 		wake_up(&ctx->sq_data->wait);
4091ed29b0b4SJens Axboe 	return 0;
4092ed29b0b4SJens Axboe }
4093ed29b0b4SJens Axboe 
4094ed29b0b4SJens Axboe static __cold int io_register_iowq_aff(struct io_ring_ctx *ctx,
4095ed29b0b4SJens Axboe 				       void __user *arg, unsigned len)
4096ed29b0b4SJens Axboe {
4097ed29b0b4SJens Axboe 	struct io_uring_task *tctx = current->io_uring;
4098ed29b0b4SJens Axboe 	cpumask_var_t new_mask;
4099ed29b0b4SJens Axboe 	int ret;
4100ed29b0b4SJens Axboe 
4101ed29b0b4SJens Axboe 	if (!tctx || !tctx->io_wq)
4102ed29b0b4SJens Axboe 		return -EINVAL;
4103ed29b0b4SJens Axboe 
4104ed29b0b4SJens Axboe 	if (!alloc_cpumask_var(&new_mask, GFP_KERNEL))
4105ed29b0b4SJens Axboe 		return -ENOMEM;
4106ed29b0b4SJens Axboe 
4107ed29b0b4SJens Axboe 	cpumask_clear(new_mask);
4108ed29b0b4SJens Axboe 	if (len > cpumask_size())
4109ed29b0b4SJens Axboe 		len = cpumask_size();
4110ed29b0b4SJens Axboe 
4111ed29b0b4SJens Axboe 	if (in_compat_syscall()) {
4112ed29b0b4SJens Axboe 		ret = compat_get_bitmap(cpumask_bits(new_mask),
4113ed29b0b4SJens Axboe 					(const compat_ulong_t __user *)arg,
4114ed29b0b4SJens Axboe 					len * 8 /* CHAR_BIT */);
4115ed29b0b4SJens Axboe 	} else {
4116ed29b0b4SJens Axboe 		ret = copy_from_user(new_mask, arg, len);
4117ed29b0b4SJens Axboe 	}
4118ed29b0b4SJens Axboe 
4119ed29b0b4SJens Axboe 	if (ret) {
4120ed29b0b4SJens Axboe 		free_cpumask_var(new_mask);
4121ed29b0b4SJens Axboe 		return -EFAULT;
4122ed29b0b4SJens Axboe 	}
4123ed29b0b4SJens Axboe 
4124ed29b0b4SJens Axboe 	ret = io_wq_cpu_affinity(tctx->io_wq, new_mask);
4125ed29b0b4SJens Axboe 	free_cpumask_var(new_mask);
4126ed29b0b4SJens Axboe 	return ret;
4127ed29b0b4SJens Axboe }
4128ed29b0b4SJens Axboe 
4129ed29b0b4SJens Axboe static __cold int io_unregister_iowq_aff(struct io_ring_ctx *ctx)
4130ed29b0b4SJens Axboe {
4131ed29b0b4SJens Axboe 	struct io_uring_task *tctx = current->io_uring;
4132ed29b0b4SJens Axboe 
4133ed29b0b4SJens Axboe 	if (!tctx || !tctx->io_wq)
4134ed29b0b4SJens Axboe 		return -EINVAL;
4135ed29b0b4SJens Axboe 
4136ed29b0b4SJens Axboe 	return io_wq_cpu_affinity(tctx->io_wq, NULL);
4137ed29b0b4SJens Axboe }
4138ed29b0b4SJens Axboe 
4139ed29b0b4SJens Axboe static __cold int io_register_iowq_max_workers(struct io_ring_ctx *ctx,
4140ed29b0b4SJens Axboe 					       void __user *arg)
4141ed29b0b4SJens Axboe 	__must_hold(&ctx->uring_lock)
4142ed29b0b4SJens Axboe {
4143ed29b0b4SJens Axboe 	struct io_tctx_node *node;
4144ed29b0b4SJens Axboe 	struct io_uring_task *tctx = NULL;
4145ed29b0b4SJens Axboe 	struct io_sq_data *sqd = NULL;
4146ed29b0b4SJens Axboe 	__u32 new_count[2];
4147ed29b0b4SJens Axboe 	int i, ret;
4148ed29b0b4SJens Axboe 
4149ed29b0b4SJens Axboe 	if (copy_from_user(new_count, arg, sizeof(new_count)))
4150ed29b0b4SJens Axboe 		return -EFAULT;
4151ed29b0b4SJens Axboe 	for (i = 0; i < ARRAY_SIZE(new_count); i++)
4152ed29b0b4SJens Axboe 		if (new_count[i] > INT_MAX)
4153ed29b0b4SJens Axboe 			return -EINVAL;
4154ed29b0b4SJens Axboe 
4155ed29b0b4SJens Axboe 	if (ctx->flags & IORING_SETUP_SQPOLL) {
4156ed29b0b4SJens Axboe 		sqd = ctx->sq_data;
4157ed29b0b4SJens Axboe 		if (sqd) {
4158ed29b0b4SJens Axboe 			/*
4159ed29b0b4SJens Axboe 			 * Observe the correct sqd->lock -> ctx->uring_lock
4160ed29b0b4SJens Axboe 			 * ordering. Fine to drop uring_lock here, we hold
4161ed29b0b4SJens Axboe 			 * a ref to the ctx.
4162ed29b0b4SJens Axboe 			 */
4163ed29b0b4SJens Axboe 			refcount_inc(&sqd->refs);
4164ed29b0b4SJens Axboe 			mutex_unlock(&ctx->uring_lock);
4165ed29b0b4SJens Axboe 			mutex_lock(&sqd->lock);
4166ed29b0b4SJens Axboe 			mutex_lock(&ctx->uring_lock);
4167ed29b0b4SJens Axboe 			if (sqd->thread)
4168ed29b0b4SJens Axboe 				tctx = sqd->thread->io_uring;
4169ed29b0b4SJens Axboe 		}
4170ed29b0b4SJens Axboe 	} else {
4171ed29b0b4SJens Axboe 		tctx = current->io_uring;
4172ed29b0b4SJens Axboe 	}
4173ed29b0b4SJens Axboe 
4174ed29b0b4SJens Axboe 	BUILD_BUG_ON(sizeof(new_count) != sizeof(ctx->iowq_limits));
4175ed29b0b4SJens Axboe 
4176ed29b0b4SJens Axboe 	for (i = 0; i < ARRAY_SIZE(new_count); i++)
4177ed29b0b4SJens Axboe 		if (new_count[i])
4178ed29b0b4SJens Axboe 			ctx->iowq_limits[i] = new_count[i];
4179ed29b0b4SJens Axboe 	ctx->iowq_limits_set = true;
4180ed29b0b4SJens Axboe 
4181ed29b0b4SJens Axboe 	if (tctx && tctx->io_wq) {
4182ed29b0b4SJens Axboe 		ret = io_wq_max_workers(tctx->io_wq, new_count);
4183ed29b0b4SJens Axboe 		if (ret)
4184ed29b0b4SJens Axboe 			goto err;
4185ed29b0b4SJens Axboe 	} else {
4186ed29b0b4SJens Axboe 		memset(new_count, 0, sizeof(new_count));
4187ed29b0b4SJens Axboe 	}
4188ed29b0b4SJens Axboe 
4189ed29b0b4SJens Axboe 	if (sqd) {
4190ed29b0b4SJens Axboe 		mutex_unlock(&sqd->lock);
4191ed29b0b4SJens Axboe 		io_put_sq_data(sqd);
4192ed29b0b4SJens Axboe 	}
4193ed29b0b4SJens Axboe 
4194ed29b0b4SJens Axboe 	if (copy_to_user(arg, new_count, sizeof(new_count)))
4195ed29b0b4SJens Axboe 		return -EFAULT;
4196ed29b0b4SJens Axboe 
4197ed29b0b4SJens Axboe 	/* that's it for SQPOLL, only the SQPOLL task creates requests */
4198ed29b0b4SJens Axboe 	if (sqd)
4199ed29b0b4SJens Axboe 		return 0;
4200ed29b0b4SJens Axboe 
4201ed29b0b4SJens Axboe 	/* now propagate the restriction to all registered users */
4202ed29b0b4SJens Axboe 	list_for_each_entry(node, &ctx->tctx_list, ctx_node) {
4203ed29b0b4SJens Axboe 		struct io_uring_task *tctx = node->task->io_uring;
4204ed29b0b4SJens Axboe 
4205ed29b0b4SJens Axboe 		if (WARN_ON_ONCE(!tctx->io_wq))
4206ed29b0b4SJens Axboe 			continue;
4207ed29b0b4SJens Axboe 
4208ed29b0b4SJens Axboe 		for (i = 0; i < ARRAY_SIZE(new_count); i++)
4209ed29b0b4SJens Axboe 			new_count[i] = ctx->iowq_limits[i];
4210ed29b0b4SJens Axboe 		/* ignore errors, it always returns zero anyway */
4211ed29b0b4SJens Axboe 		(void)io_wq_max_workers(tctx->io_wq, new_count);
4212ed29b0b4SJens Axboe 	}
4213ed29b0b4SJens Axboe 	return 0;
4214ed29b0b4SJens Axboe err:
4215ed29b0b4SJens Axboe 	if (sqd) {
4216ed29b0b4SJens Axboe 		mutex_unlock(&sqd->lock);
4217ed29b0b4SJens Axboe 		io_put_sq_data(sqd);
4218ed29b0b4SJens Axboe 	}
4219ed29b0b4SJens Axboe 	return ret;
4220ed29b0b4SJens Axboe }
4221ed29b0b4SJens Axboe 
4222ed29b0b4SJens Axboe static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
4223ed29b0b4SJens Axboe 			       void __user *arg, unsigned nr_args)
4224ed29b0b4SJens Axboe 	__releases(ctx->uring_lock)
4225ed29b0b4SJens Axboe 	__acquires(ctx->uring_lock)
4226ed29b0b4SJens Axboe {
4227ed29b0b4SJens Axboe 	int ret;
4228ed29b0b4SJens Axboe 
4229ed29b0b4SJens Axboe 	/*
4230fbb8bb02SPavel Begunkov 	 * We don't quiesce the refs for register anymore and so it can't be
4231fbb8bb02SPavel Begunkov 	 * dying as we're holding a file ref here.
4232ed29b0b4SJens Axboe 	 */
4233fbb8bb02SPavel Begunkov 	if (WARN_ON_ONCE(percpu_ref_is_dying(&ctx->refs)))
4234ed29b0b4SJens Axboe 		return -ENXIO;
4235ed29b0b4SJens Axboe 
4236d7cce96cSPavel Begunkov 	if (ctx->submitter_task && ctx->submitter_task != current)
4237d7cce96cSPavel Begunkov 		return -EEXIST;
4238d7cce96cSPavel Begunkov 
4239ed29b0b4SJens Axboe 	if (ctx->restricted) {
4240ed29b0b4SJens Axboe 		opcode = array_index_nospec(opcode, IORING_REGISTER_LAST);
4241ed29b0b4SJens Axboe 		if (!test_bit(opcode, ctx->restrictions.register_op))
4242ed29b0b4SJens Axboe 			return -EACCES;
4243ed29b0b4SJens Axboe 	}
4244ed29b0b4SJens Axboe 
4245ed29b0b4SJens Axboe 	switch (opcode) {
4246ed29b0b4SJens Axboe 	case IORING_REGISTER_BUFFERS:
4247ed29b0b4SJens Axboe 		ret = -EFAULT;
4248ed29b0b4SJens Axboe 		if (!arg)
4249ed29b0b4SJens Axboe 			break;
4250ed29b0b4SJens Axboe 		ret = io_sqe_buffers_register(ctx, arg, nr_args, NULL);
4251ed29b0b4SJens Axboe 		break;
4252ed29b0b4SJens Axboe 	case IORING_UNREGISTER_BUFFERS:
4253ed29b0b4SJens Axboe 		ret = -EINVAL;
4254ed29b0b4SJens Axboe 		if (arg || nr_args)
4255ed29b0b4SJens Axboe 			break;
4256ed29b0b4SJens Axboe 		ret = io_sqe_buffers_unregister(ctx);
4257ed29b0b4SJens Axboe 		break;
4258ed29b0b4SJens Axboe 	case IORING_REGISTER_FILES:
4259ed29b0b4SJens Axboe 		ret = -EFAULT;
4260ed29b0b4SJens Axboe 		if (!arg)
4261ed29b0b4SJens Axboe 			break;
4262ed29b0b4SJens Axboe 		ret = io_sqe_files_register(ctx, arg, nr_args, NULL);
4263ed29b0b4SJens Axboe 		break;
4264ed29b0b4SJens Axboe 	case IORING_UNREGISTER_FILES:
4265ed29b0b4SJens Axboe 		ret = -EINVAL;
4266ed29b0b4SJens Axboe 		if (arg || nr_args)
4267ed29b0b4SJens Axboe 			break;
4268ed29b0b4SJens Axboe 		ret = io_sqe_files_unregister(ctx);
4269ed29b0b4SJens Axboe 		break;
4270ed29b0b4SJens Axboe 	case IORING_REGISTER_FILES_UPDATE:
4271ed29b0b4SJens Axboe 		ret = io_register_files_update(ctx, arg, nr_args);
4272ed29b0b4SJens Axboe 		break;
4273ed29b0b4SJens Axboe 	case IORING_REGISTER_EVENTFD:
4274ed29b0b4SJens Axboe 		ret = -EINVAL;
4275ed29b0b4SJens Axboe 		if (nr_args != 1)
4276ed29b0b4SJens Axboe 			break;
4277ed29b0b4SJens Axboe 		ret = io_eventfd_register(ctx, arg, 0);
4278ed29b0b4SJens Axboe 		break;
4279ed29b0b4SJens Axboe 	case IORING_REGISTER_EVENTFD_ASYNC:
4280ed29b0b4SJens Axboe 		ret = -EINVAL;
4281ed29b0b4SJens Axboe 		if (nr_args != 1)
4282ed29b0b4SJens Axboe 			break;
4283ed29b0b4SJens Axboe 		ret = io_eventfd_register(ctx, arg, 1);
4284ed29b0b4SJens Axboe 		break;
4285ed29b0b4SJens Axboe 	case IORING_UNREGISTER_EVENTFD:
4286ed29b0b4SJens Axboe 		ret = -EINVAL;
4287ed29b0b4SJens Axboe 		if (arg || nr_args)
4288ed29b0b4SJens Axboe 			break;
4289ed29b0b4SJens Axboe 		ret = io_eventfd_unregister(ctx);
4290ed29b0b4SJens Axboe 		break;
4291ed29b0b4SJens Axboe 	case IORING_REGISTER_PROBE:
4292ed29b0b4SJens Axboe 		ret = -EINVAL;
4293ed29b0b4SJens Axboe 		if (!arg || nr_args > 256)
4294ed29b0b4SJens Axboe 			break;
4295ed29b0b4SJens Axboe 		ret = io_probe(ctx, arg, nr_args);
4296ed29b0b4SJens Axboe 		break;
4297ed29b0b4SJens Axboe 	case IORING_REGISTER_PERSONALITY:
4298ed29b0b4SJens Axboe 		ret = -EINVAL;
4299ed29b0b4SJens Axboe 		if (arg || nr_args)
4300ed29b0b4SJens Axboe 			break;
4301ed29b0b4SJens Axboe 		ret = io_register_personality(ctx);
4302ed29b0b4SJens Axboe 		break;
4303ed29b0b4SJens Axboe 	case IORING_UNREGISTER_PERSONALITY:
4304ed29b0b4SJens Axboe 		ret = -EINVAL;
4305ed29b0b4SJens Axboe 		if (arg)
4306ed29b0b4SJens Axboe 			break;
4307ed29b0b4SJens Axboe 		ret = io_unregister_personality(ctx, nr_args);
4308ed29b0b4SJens Axboe 		break;
4309ed29b0b4SJens Axboe 	case IORING_REGISTER_ENABLE_RINGS:
4310ed29b0b4SJens Axboe 		ret = -EINVAL;
4311ed29b0b4SJens Axboe 		if (arg || nr_args)
4312ed29b0b4SJens Axboe 			break;
4313ed29b0b4SJens Axboe 		ret = io_register_enable_rings(ctx);
4314ed29b0b4SJens Axboe 		break;
4315ed29b0b4SJens Axboe 	case IORING_REGISTER_RESTRICTIONS:
4316ed29b0b4SJens Axboe 		ret = io_register_restrictions(ctx, arg, nr_args);
4317ed29b0b4SJens Axboe 		break;
4318ed29b0b4SJens Axboe 	case IORING_REGISTER_FILES2:
4319ed29b0b4SJens Axboe 		ret = io_register_rsrc(ctx, arg, nr_args, IORING_RSRC_FILE);
4320ed29b0b4SJens Axboe 		break;
4321ed29b0b4SJens Axboe 	case IORING_REGISTER_FILES_UPDATE2:
4322ed29b0b4SJens Axboe 		ret = io_register_rsrc_update(ctx, arg, nr_args,
4323ed29b0b4SJens Axboe 					      IORING_RSRC_FILE);
4324ed29b0b4SJens Axboe 		break;
4325ed29b0b4SJens Axboe 	case IORING_REGISTER_BUFFERS2:
4326ed29b0b4SJens Axboe 		ret = io_register_rsrc(ctx, arg, nr_args, IORING_RSRC_BUFFER);
4327ed29b0b4SJens Axboe 		break;
4328ed29b0b4SJens Axboe 	case IORING_REGISTER_BUFFERS_UPDATE:
4329ed29b0b4SJens Axboe 		ret = io_register_rsrc_update(ctx, arg, nr_args,
4330ed29b0b4SJens Axboe 					      IORING_RSRC_BUFFER);
4331ed29b0b4SJens Axboe 		break;
4332ed29b0b4SJens Axboe 	case IORING_REGISTER_IOWQ_AFF:
4333ed29b0b4SJens Axboe 		ret = -EINVAL;
4334ed29b0b4SJens Axboe 		if (!arg || !nr_args)
4335ed29b0b4SJens Axboe 			break;
4336ed29b0b4SJens Axboe 		ret = io_register_iowq_aff(ctx, arg, nr_args);
4337ed29b0b4SJens Axboe 		break;
4338ed29b0b4SJens Axboe 	case IORING_UNREGISTER_IOWQ_AFF:
4339ed29b0b4SJens Axboe 		ret = -EINVAL;
4340ed29b0b4SJens Axboe 		if (arg || nr_args)
4341ed29b0b4SJens Axboe 			break;
4342ed29b0b4SJens Axboe 		ret = io_unregister_iowq_aff(ctx);
4343ed29b0b4SJens Axboe 		break;
4344ed29b0b4SJens Axboe 	case IORING_REGISTER_IOWQ_MAX_WORKERS:
4345ed29b0b4SJens Axboe 		ret = -EINVAL;
4346ed29b0b4SJens Axboe 		if (!arg || nr_args != 2)
4347ed29b0b4SJens Axboe 			break;
4348ed29b0b4SJens Axboe 		ret = io_register_iowq_max_workers(ctx, arg);
4349ed29b0b4SJens Axboe 		break;
4350ed29b0b4SJens Axboe 	case IORING_REGISTER_RING_FDS:
4351ed29b0b4SJens Axboe 		ret = io_ringfd_register(ctx, arg, nr_args);
4352ed29b0b4SJens Axboe 		break;
4353ed29b0b4SJens Axboe 	case IORING_UNREGISTER_RING_FDS:
4354ed29b0b4SJens Axboe 		ret = io_ringfd_unregister(ctx, arg, nr_args);
4355ed29b0b4SJens Axboe 		break;
4356ed29b0b4SJens Axboe 	case IORING_REGISTER_PBUF_RING:
4357ed29b0b4SJens Axboe 		ret = -EINVAL;
4358ed29b0b4SJens Axboe 		if (!arg || nr_args != 1)
4359ed29b0b4SJens Axboe 			break;
4360ed29b0b4SJens Axboe 		ret = io_register_pbuf_ring(ctx, arg);
4361ed29b0b4SJens Axboe 		break;
4362ed29b0b4SJens Axboe 	case IORING_UNREGISTER_PBUF_RING:
4363ed29b0b4SJens Axboe 		ret = -EINVAL;
4364ed29b0b4SJens Axboe 		if (!arg || nr_args != 1)
4365ed29b0b4SJens Axboe 			break;
4366ed29b0b4SJens Axboe 		ret = io_unregister_pbuf_ring(ctx, arg);
4367ed29b0b4SJens Axboe 		break;
436878a861b9SJens Axboe 	case IORING_REGISTER_SYNC_CANCEL:
436978a861b9SJens Axboe 		ret = -EINVAL;
437078a861b9SJens Axboe 		if (!arg || nr_args != 1)
437178a861b9SJens Axboe 			break;
437278a861b9SJens Axboe 		ret = io_sync_cancel(ctx, arg);
437378a861b9SJens Axboe 		break;
43746e73dffbSPavel Begunkov 	case IORING_REGISTER_FILE_ALLOC_RANGE:
43756e73dffbSPavel Begunkov 		ret = -EINVAL;
43766e73dffbSPavel Begunkov 		if (!arg || nr_args)
43776e73dffbSPavel Begunkov 			break;
43786e73dffbSPavel Begunkov 		ret = io_register_file_alloc_range(ctx, arg);
43796e73dffbSPavel Begunkov 		break;
4380ed29b0b4SJens Axboe 	default:
4381ed29b0b4SJens Axboe 		ret = -EINVAL;
4382ed29b0b4SJens Axboe 		break;
4383ed29b0b4SJens Axboe 	}
4384ed29b0b4SJens Axboe 
4385ed29b0b4SJens Axboe 	return ret;
4386ed29b0b4SJens Axboe }
4387ed29b0b4SJens Axboe 
4388ed29b0b4SJens Axboe SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
4389ed29b0b4SJens Axboe 		void __user *, arg, unsigned int, nr_args)
4390ed29b0b4SJens Axboe {
4391ed29b0b4SJens Axboe 	struct io_ring_ctx *ctx;
4392ed29b0b4SJens Axboe 	long ret = -EBADF;
4393ed29b0b4SJens Axboe 	struct fd f;
43947d3fd88dSJosh Triplett 	bool use_registered_ring;
43957d3fd88dSJosh Triplett 
43967d3fd88dSJosh Triplett 	use_registered_ring = !!(opcode & IORING_REGISTER_USE_REGISTERED_RING);
43977d3fd88dSJosh Triplett 	opcode &= ~IORING_REGISTER_USE_REGISTERED_RING;
4398ed29b0b4SJens Axboe 
439934319084SJens Axboe 	if (opcode >= IORING_REGISTER_LAST)
440034319084SJens Axboe 		return -EINVAL;
440134319084SJens Axboe 
44027d3fd88dSJosh Triplett 	if (use_registered_ring) {
44037d3fd88dSJosh Triplett 		/*
44047d3fd88dSJosh Triplett 		 * Ring fd has been registered via IORING_REGISTER_RING_FDS, we
44057d3fd88dSJosh Triplett 		 * need only dereference our task private array to find it.
44067d3fd88dSJosh Triplett 		 */
44077d3fd88dSJosh Triplett 		struct io_uring_task *tctx = current->io_uring;
4408ed29b0b4SJens Axboe 
44097d3fd88dSJosh Triplett 		if (unlikely(!tctx || fd >= IO_RINGFD_REG_MAX))
44107d3fd88dSJosh Triplett 			return -EINVAL;
44117d3fd88dSJosh Triplett 		fd = array_index_nospec(fd, IO_RINGFD_REG_MAX);
44127d3fd88dSJosh Triplett 		f.file = tctx->registered_rings[fd];
44137d3fd88dSJosh Triplett 		f.flags = 0;
44147d3fd88dSJosh Triplett 		if (unlikely(!f.file))
44157d3fd88dSJosh Triplett 			return -EBADF;
44167d3fd88dSJosh Triplett 	} else {
44177d3fd88dSJosh Triplett 		f = fdget(fd);
44187d3fd88dSJosh Triplett 		if (unlikely(!f.file))
44197d3fd88dSJosh Triplett 			return -EBADF;
4420ed29b0b4SJens Axboe 		ret = -EOPNOTSUPP;
4421e5550a14SJens Axboe 		if (!io_is_uring_fops(f.file))
4422ed29b0b4SJens Axboe 			goto out_fput;
44237d3fd88dSJosh Triplett 	}
4424ed29b0b4SJens Axboe 
4425ed29b0b4SJens Axboe 	ctx = f.file->private_data;
4426ed29b0b4SJens Axboe 
4427ed29b0b4SJens Axboe 	mutex_lock(&ctx->uring_lock);
4428ed29b0b4SJens Axboe 	ret = __io_uring_register(ctx, opcode, arg, nr_args);
4429ed29b0b4SJens Axboe 	mutex_unlock(&ctx->uring_lock);
4430ed29b0b4SJens Axboe 	trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs, ret);
4431ed29b0b4SJens Axboe out_fput:
4432ed29b0b4SJens Axboe 	fdput(f);
4433ed29b0b4SJens Axboe 	return ret;
4434ed29b0b4SJens Axboe }
4435ed29b0b4SJens Axboe 
4436ed29b0b4SJens Axboe static int __init io_uring_init(void)
4437ed29b0b4SJens Axboe {
44389c71d39aSStefan Metzmacher #define __BUILD_BUG_VERIFY_OFFSET_SIZE(stype, eoffset, esize, ename) do { \
4439ed29b0b4SJens Axboe 	BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
44409c71d39aSStefan Metzmacher 	BUILD_BUG_ON(sizeof_field(stype, ename) != esize); \
4441ed29b0b4SJens Axboe } while (0)
4442ed29b0b4SJens Axboe 
4443ed29b0b4SJens Axboe #define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
44449c71d39aSStefan Metzmacher 	__BUILD_BUG_VERIFY_OFFSET_SIZE(struct io_uring_sqe, eoffset, sizeof(etype), ename)
44459c71d39aSStefan Metzmacher #define BUILD_BUG_SQE_ELEM_SIZE(eoffset, esize, ename) \
44469c71d39aSStefan Metzmacher 	__BUILD_BUG_VERIFY_OFFSET_SIZE(struct io_uring_sqe, eoffset, esize, ename)
4447ed29b0b4SJens Axboe 	BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
4448ed29b0b4SJens Axboe 	BUILD_BUG_SQE_ELEM(0,  __u8,   opcode);
4449ed29b0b4SJens Axboe 	BUILD_BUG_SQE_ELEM(1,  __u8,   flags);
4450ed29b0b4SJens Axboe 	BUILD_BUG_SQE_ELEM(2,  __u16,  ioprio);
4451ed29b0b4SJens Axboe 	BUILD_BUG_SQE_ELEM(4,  __s32,  fd);
4452ed29b0b4SJens Axboe 	BUILD_BUG_SQE_ELEM(8,  __u64,  off);
4453ed29b0b4SJens Axboe 	BUILD_BUG_SQE_ELEM(8,  __u64,  addr2);
44549c71d39aSStefan Metzmacher 	BUILD_BUG_SQE_ELEM(8,  __u32,  cmd_op);
44559c71d39aSStefan Metzmacher 	BUILD_BUG_SQE_ELEM(12, __u32, __pad1);
4456ed29b0b4SJens Axboe 	BUILD_BUG_SQE_ELEM(16, __u64,  addr);
4457ed29b0b4SJens Axboe 	BUILD_BUG_SQE_ELEM(16, __u64,  splice_off_in);
4458ed29b0b4SJens Axboe 	BUILD_BUG_SQE_ELEM(24, __u32,  len);
4459ed29b0b4SJens Axboe 	BUILD_BUG_SQE_ELEM(28,     __kernel_rwf_t, rw_flags);
4460ed29b0b4SJens Axboe 	BUILD_BUG_SQE_ELEM(28, /* compat */   int, rw_flags);
4461ed29b0b4SJens Axboe 	BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
4462ed29b0b4SJens Axboe 	BUILD_BUG_SQE_ELEM(28, __u32,  fsync_flags);
4463ed29b0b4SJens Axboe 	BUILD_BUG_SQE_ELEM(28, /* compat */ __u16,  poll_events);
4464ed29b0b4SJens Axboe 	BUILD_BUG_SQE_ELEM(28, __u32,  poll32_events);
4465ed29b0b4SJens Axboe 	BUILD_BUG_SQE_ELEM(28, __u32,  sync_range_flags);
4466ed29b0b4SJens Axboe 	BUILD_BUG_SQE_ELEM(28, __u32,  msg_flags);
4467ed29b0b4SJens Axboe 	BUILD_BUG_SQE_ELEM(28, __u32,  timeout_flags);
4468ed29b0b4SJens Axboe 	BUILD_BUG_SQE_ELEM(28, __u32,  accept_flags);
4469ed29b0b4SJens Axboe 	BUILD_BUG_SQE_ELEM(28, __u32,  cancel_flags);
4470ed29b0b4SJens Axboe 	BUILD_BUG_SQE_ELEM(28, __u32,  open_flags);
4471ed29b0b4SJens Axboe 	BUILD_BUG_SQE_ELEM(28, __u32,  statx_flags);
4472ed29b0b4SJens Axboe 	BUILD_BUG_SQE_ELEM(28, __u32,  fadvise_advice);
4473ed29b0b4SJens Axboe 	BUILD_BUG_SQE_ELEM(28, __u32,  splice_flags);
44749c71d39aSStefan Metzmacher 	BUILD_BUG_SQE_ELEM(28, __u32,  rename_flags);
44759c71d39aSStefan Metzmacher 	BUILD_BUG_SQE_ELEM(28, __u32,  unlink_flags);
44769c71d39aSStefan Metzmacher 	BUILD_BUG_SQE_ELEM(28, __u32,  hardlink_flags);
44779c71d39aSStefan Metzmacher 	BUILD_BUG_SQE_ELEM(28, __u32,  xattr_flags);
44789c71d39aSStefan Metzmacher 	BUILD_BUG_SQE_ELEM(28, __u32,  msg_ring_flags);
4479ed29b0b4SJens Axboe 	BUILD_BUG_SQE_ELEM(32, __u64,  user_data);
4480ed29b0b4SJens Axboe 	BUILD_BUG_SQE_ELEM(40, __u16,  buf_index);
4481ed29b0b4SJens Axboe 	BUILD_BUG_SQE_ELEM(40, __u16,  buf_group);
4482ed29b0b4SJens Axboe 	BUILD_BUG_SQE_ELEM(42, __u16,  personality);
4483ed29b0b4SJens Axboe 	BUILD_BUG_SQE_ELEM(44, __s32,  splice_fd_in);
4484ed29b0b4SJens Axboe 	BUILD_BUG_SQE_ELEM(44, __u32,  file_index);
4485b48c312bSPavel Begunkov 	BUILD_BUG_SQE_ELEM(44, __u16,  addr_len);
4486b48c312bSPavel Begunkov 	BUILD_BUG_SQE_ELEM(46, __u16,  __pad3[0]);
4487ed29b0b4SJens Axboe 	BUILD_BUG_SQE_ELEM(48, __u64,  addr3);
44889c71d39aSStefan Metzmacher 	BUILD_BUG_SQE_ELEM_SIZE(48, 0, cmd);
44899c71d39aSStefan Metzmacher 	BUILD_BUG_SQE_ELEM(56, __u64,  __pad2);
4490ed29b0b4SJens Axboe 
4491ed29b0b4SJens Axboe 	BUILD_BUG_ON(sizeof(struct io_uring_files_update) !=
4492ed29b0b4SJens Axboe 		     sizeof(struct io_uring_rsrc_update));
4493ed29b0b4SJens Axboe 	BUILD_BUG_ON(sizeof(struct io_uring_rsrc_update) >
4494ed29b0b4SJens Axboe 		     sizeof(struct io_uring_rsrc_update2));
4495ed29b0b4SJens Axboe 
4496ed29b0b4SJens Axboe 	/* ->buf_index is u16 */
4497ed29b0b4SJens Axboe 	BUILD_BUG_ON(offsetof(struct io_uring_buf_ring, bufs) != 0);
4498ed29b0b4SJens Axboe 	BUILD_BUG_ON(offsetof(struct io_uring_buf, resv) !=
4499ed29b0b4SJens Axboe 		     offsetof(struct io_uring_buf_ring, tail));
4500ed29b0b4SJens Axboe 
4501ed29b0b4SJens Axboe 	/* should fit into one byte */
4502ed29b0b4SJens Axboe 	BUILD_BUG_ON(SQE_VALID_FLAGS >= (1 << 8));
4503ed29b0b4SJens Axboe 	BUILD_BUG_ON(SQE_COMMON_FLAGS >= (1 << 8));
4504ed29b0b4SJens Axboe 	BUILD_BUG_ON((SQE_VALID_FLAGS | SQE_COMMON_FLAGS) != SQE_VALID_FLAGS);
4505ed29b0b4SJens Axboe 
4506ed29b0b4SJens Axboe 	BUILD_BUG_ON(__REQ_F_LAST_BIT > 8 * sizeof(int));
4507ed29b0b4SJens Axboe 
4508ed29b0b4SJens Axboe 	BUILD_BUG_ON(sizeof(atomic_t) != sizeof(u32));
4509ed29b0b4SJens Axboe 
4510d9b57aa3SJens Axboe 	io_uring_optable_init();
4511ed29b0b4SJens Axboe 
4512ed29b0b4SJens Axboe 	req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC |
4513ed29b0b4SJens Axboe 				SLAB_ACCOUNT);
4514ed29b0b4SJens Axboe 	return 0;
4515ed29b0b4SJens Axboe };
4516ed29b0b4SJens Axboe __initcall(io_uring_init);
4517