xref: /openbmc/linux/io_uring/io_uring.c (revision c92fcfc2)
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"
98*c92fcfc2SJens Axboe #include "rw.h"
999b797a37SJens Axboe #include "alloc_cache.h"
1005e2a18d9SJens Axboe 
101ed29b0b4SJens Axboe #define IORING_MAX_ENTRIES	32768
102ed29b0b4SJens Axboe #define IORING_MAX_CQ_ENTRIES	(2 * IORING_MAX_ENTRIES)
103ed29b0b4SJens Axboe 
104ed29b0b4SJens Axboe #define IORING_MAX_RESTRICTIONS	(IORING_RESTRICTION_LAST + \
105ed29b0b4SJens Axboe 				 IORING_REGISTER_LAST + IORING_OP_LAST)
106ed29b0b4SJens Axboe 
107ed29b0b4SJens Axboe #define SQE_COMMON_FLAGS (IOSQE_FIXED_FILE | IOSQE_IO_LINK | \
108ed29b0b4SJens Axboe 			  IOSQE_IO_HARDLINK | IOSQE_ASYNC)
109ed29b0b4SJens Axboe 
110ed29b0b4SJens Axboe #define SQE_VALID_FLAGS	(SQE_COMMON_FLAGS | IOSQE_BUFFER_SELECT | \
111ed29b0b4SJens Axboe 			IOSQE_IO_DRAIN | IOSQE_CQE_SKIP_SUCCESS)
112ed29b0b4SJens Axboe 
113ed29b0b4SJens Axboe #define IO_REQ_CLEAN_FLAGS (REQ_F_BUFFER_SELECTED | REQ_F_NEED_CLEANUP | \
114ed29b0b4SJens Axboe 				REQ_F_POLLED | REQ_F_INFLIGHT | REQ_F_CREDS | \
115ed29b0b4SJens Axboe 				REQ_F_ASYNC_DATA)
116ed29b0b4SJens Axboe 
117ed29b0b4SJens Axboe #define IO_REQ_CLEAN_SLOW_FLAGS (REQ_F_REFCOUNT | REQ_F_LINK | REQ_F_HARDLINK |\
118ed29b0b4SJens Axboe 				 IO_REQ_CLEAN_FLAGS)
119ed29b0b4SJens Axboe 
120ed29b0b4SJens Axboe #define IO_TCTX_REFS_CACHE_NR	(1U << 10)
121ed29b0b4SJens Axboe 
122ed29b0b4SJens Axboe #define IO_COMPL_BATCH			32
123ed29b0b4SJens Axboe #define IO_REQ_ALLOC_BATCH		8
124ed29b0b4SJens Axboe 
125ed29b0b4SJens Axboe enum {
126ed29b0b4SJens Axboe 	IO_CHECK_CQ_OVERFLOW_BIT,
127ed29b0b4SJens Axboe 	IO_CHECK_CQ_DROPPED_BIT,
128ed29b0b4SJens Axboe };
129ed29b0b4SJens Axboe 
13021a091b9SDylan Yudaken enum {
13121a091b9SDylan Yudaken 	IO_EVENTFD_OP_SIGNAL_BIT,
13221a091b9SDylan Yudaken 	IO_EVENTFD_OP_FREE_BIT,
13321a091b9SDylan Yudaken };
13421a091b9SDylan Yudaken 
135ed29b0b4SJens Axboe struct io_defer_entry {
136ed29b0b4SJens Axboe 	struct list_head	list;
137ed29b0b4SJens Axboe 	struct io_kiocb		*req;
138ed29b0b4SJens Axboe 	u32			seq;
139ed29b0b4SJens Axboe };
140ed29b0b4SJens Axboe 
141ed29b0b4SJens Axboe /* requests with any of those set should undergo io_disarm_next() */
142ed29b0b4SJens Axboe #define IO_DISARM_MASK (REQ_F_ARM_LTIMEOUT | REQ_F_LINK_TIMEOUT | REQ_F_FAIL)
143ed29b0b4SJens Axboe #define IO_REQ_LINK_FLAGS (REQ_F_LINK | REQ_F_HARDLINK)
144ed29b0b4SJens Axboe 
145affa87dbSPavel Begunkov static bool io_uring_try_cancel_requests(struct io_ring_ctx *ctx,
146ed29b0b4SJens Axboe 					 struct task_struct *task,
147ed29b0b4SJens Axboe 					 bool cancel_all);
148ed29b0b4SJens Axboe 
149ed29b0b4SJens Axboe static void io_dismantle_req(struct io_kiocb *req);
150ed29b0b4SJens Axboe static void io_clean_op(struct io_kiocb *req);
151ed29b0b4SJens Axboe static void io_queue_sqe(struct io_kiocb *req);
152c0e0d6baSDylan Yudaken static void io_move_task_work_from_local(struct io_ring_ctx *ctx);
153ed29b0b4SJens Axboe static void __io_submit_flush_completions(struct io_ring_ctx *ctx);
15477e443abSPavel Begunkov static __cold void io_fallback_tw(struct io_uring_task *tctx);
155ed29b0b4SJens Axboe 
156c1755c25SBreno Leitao struct kmem_cache *req_cachep;
157ed29b0b4SJens Axboe 
158ed29b0b4SJens Axboe struct sock *io_uring_get_socket(struct file *file)
159ed29b0b4SJens Axboe {
160ed29b0b4SJens Axboe #if defined(CONFIG_UNIX)
161cd40cae2SJens Axboe 	if (io_is_uring_fops(file)) {
162ed29b0b4SJens Axboe 		struct io_ring_ctx *ctx = file->private_data;
163ed29b0b4SJens Axboe 
164ed29b0b4SJens Axboe 		return ctx->ring_sock->sk;
165ed29b0b4SJens Axboe 	}
166ed29b0b4SJens Axboe #endif
167ed29b0b4SJens Axboe 	return NULL;
168ed29b0b4SJens Axboe }
169ed29b0b4SJens Axboe EXPORT_SYMBOL(io_uring_get_socket);
170ed29b0b4SJens Axboe 
171ed29b0b4SJens Axboe static inline void io_submit_flush_completions(struct io_ring_ctx *ctx)
172ed29b0b4SJens Axboe {
173931147ddSDylan Yudaken 	if (!wq_list_empty(&ctx->submit_state.compl_reqs) ||
174931147ddSDylan Yudaken 	    ctx->submit_state.cqes_count)
175ed29b0b4SJens Axboe 		__io_submit_flush_completions(ctx);
176ed29b0b4SJens Axboe }
177ed29b0b4SJens Axboe 
178faf88ddeSPavel Begunkov static inline unsigned int __io_cqring_events(struct io_ring_ctx *ctx)
179faf88ddeSPavel Begunkov {
180faf88ddeSPavel Begunkov 	return ctx->cached_cq_tail - READ_ONCE(ctx->rings->cq.head);
181faf88ddeSPavel Begunkov }
182faf88ddeSPavel Begunkov 
1830fc8c2acSDylan Yudaken static inline unsigned int __io_cqring_events_user(struct io_ring_ctx *ctx)
1840fc8c2acSDylan Yudaken {
1850fc8c2acSDylan Yudaken 	return READ_ONCE(ctx->rings->cq.tail) - READ_ONCE(ctx->rings->cq.head);
1860fc8c2acSDylan Yudaken }
1870fc8c2acSDylan Yudaken 
188ed29b0b4SJens Axboe static bool io_match_linked(struct io_kiocb *head)
189ed29b0b4SJens Axboe {
190ed29b0b4SJens Axboe 	struct io_kiocb *req;
191ed29b0b4SJens Axboe 
192ed29b0b4SJens Axboe 	io_for_each_link(req, head) {
193ed29b0b4SJens Axboe 		if (req->flags & REQ_F_INFLIGHT)
194ed29b0b4SJens Axboe 			return true;
195ed29b0b4SJens Axboe 	}
196ed29b0b4SJens Axboe 	return false;
197ed29b0b4SJens Axboe }
198ed29b0b4SJens Axboe 
199ed29b0b4SJens Axboe /*
200ed29b0b4SJens Axboe  * As io_match_task() but protected against racing with linked timeouts.
201ed29b0b4SJens Axboe  * User must not hold timeout_lock.
202ed29b0b4SJens Axboe  */
203329061d3SJens Axboe bool io_match_task_safe(struct io_kiocb *head, struct task_struct *task,
204ed29b0b4SJens Axboe 			bool cancel_all)
205ed29b0b4SJens Axboe {
206ed29b0b4SJens Axboe 	bool matched;
207ed29b0b4SJens Axboe 
208ed29b0b4SJens Axboe 	if (task && head->task != task)
209ed29b0b4SJens Axboe 		return false;
210ed29b0b4SJens Axboe 	if (cancel_all)
211ed29b0b4SJens Axboe 		return true;
212ed29b0b4SJens Axboe 
213ed29b0b4SJens Axboe 	if (head->flags & REQ_F_LINK_TIMEOUT) {
214ed29b0b4SJens Axboe 		struct io_ring_ctx *ctx = head->ctx;
215ed29b0b4SJens Axboe 
216ed29b0b4SJens Axboe 		/* protect against races with linked timeouts */
217ed29b0b4SJens Axboe 		spin_lock_irq(&ctx->timeout_lock);
218ed29b0b4SJens Axboe 		matched = io_match_linked(head);
219ed29b0b4SJens Axboe 		spin_unlock_irq(&ctx->timeout_lock);
220ed29b0b4SJens Axboe 	} else {
221ed29b0b4SJens Axboe 		matched = io_match_linked(head);
222ed29b0b4SJens Axboe 	}
223ed29b0b4SJens Axboe 	return matched;
224ed29b0b4SJens Axboe }
225ed29b0b4SJens Axboe 
226ed29b0b4SJens Axboe static inline void req_fail_link_node(struct io_kiocb *req, int res)
227ed29b0b4SJens Axboe {
228ed29b0b4SJens Axboe 	req_set_fail(req);
22997b388d7SJens Axboe 	io_req_set_res(req, res, 0);
230ed29b0b4SJens Axboe }
231ed29b0b4SJens Axboe 
232ed29b0b4SJens Axboe static inline void io_req_add_to_cache(struct io_kiocb *req, struct io_ring_ctx *ctx)
233ed29b0b4SJens Axboe {
234ed29b0b4SJens Axboe 	wq_stack_add_head(&req->comp_list, &ctx->submit_state.free_list);
235c1755c25SBreno Leitao 	kasan_poison_object_data(req_cachep, req);
236ed29b0b4SJens Axboe }
237ed29b0b4SJens Axboe 
238ed29b0b4SJens Axboe static __cold void io_ring_ctx_ref_free(struct percpu_ref *ref)
239ed29b0b4SJens Axboe {
240ed29b0b4SJens Axboe 	struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
241ed29b0b4SJens Axboe 
242ed29b0b4SJens Axboe 	complete(&ctx->ref_comp);
243ed29b0b4SJens Axboe }
244ed29b0b4SJens Axboe 
245ed29b0b4SJens Axboe static __cold void io_fallback_req_func(struct work_struct *work)
246ed29b0b4SJens Axboe {
247ed29b0b4SJens Axboe 	struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx,
248ed29b0b4SJens Axboe 						fallback_work.work);
249ed29b0b4SJens Axboe 	struct llist_node *node = llist_del_all(&ctx->fallback_llist);
250ed29b0b4SJens Axboe 	struct io_kiocb *req, *tmp;
251a282967cSPavel Begunkov 	struct io_tw_state ts = { .locked = true, };
252ed29b0b4SJens Axboe 
25331f084b7SPavel Begunkov 	mutex_lock(&ctx->uring_lock);
2543218e5d3SPavel Begunkov 	llist_for_each_entry_safe(req, tmp, node, io_task_work.node)
255a282967cSPavel Begunkov 		req->io_task_work.func(req, &ts);
256a282967cSPavel Begunkov 	if (WARN_ON_ONCE(!ts.locked))
25731f084b7SPavel Begunkov 		return;
258ed29b0b4SJens Axboe 	io_submit_flush_completions(ctx);
259ed29b0b4SJens Axboe 	mutex_unlock(&ctx->uring_lock);
260ed29b0b4SJens Axboe }
261ed29b0b4SJens Axboe 
262e6f89be6SPavel Begunkov static int io_alloc_hash_table(struct io_hash_table *table, unsigned bits)
263e6f89be6SPavel Begunkov {
264e6f89be6SPavel Begunkov 	unsigned hash_buckets = 1U << bits;
265e6f89be6SPavel Begunkov 	size_t hash_size = hash_buckets * sizeof(table->hbs[0]);
266e6f89be6SPavel Begunkov 
267e6f89be6SPavel Begunkov 	table->hbs = kmalloc(hash_size, GFP_KERNEL);
268e6f89be6SPavel Begunkov 	if (!table->hbs)
269e6f89be6SPavel Begunkov 		return -ENOMEM;
270e6f89be6SPavel Begunkov 
271e6f89be6SPavel Begunkov 	table->hash_bits = bits;
272e6f89be6SPavel Begunkov 	init_hash_table(table, hash_buckets);
273e6f89be6SPavel Begunkov 	return 0;
274e6f89be6SPavel Begunkov }
275e6f89be6SPavel Begunkov 
276ed29b0b4SJens Axboe static __cold struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
277ed29b0b4SJens Axboe {
278ed29b0b4SJens Axboe 	struct io_ring_ctx *ctx;
279ed29b0b4SJens Axboe 	int hash_bits;
280ed29b0b4SJens Axboe 
281ed29b0b4SJens Axboe 	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
282ed29b0b4SJens Axboe 	if (!ctx)
283ed29b0b4SJens Axboe 		return NULL;
284ed29b0b4SJens Axboe 
285ed29b0b4SJens Axboe 	xa_init(&ctx->io_bl_xa);
286ed29b0b4SJens Axboe 
287ed29b0b4SJens Axboe 	/*
288ed29b0b4SJens Axboe 	 * Use 5 bits less than the max cq entries, that should give us around
2894a07723fSPavel Begunkov 	 * 32 entries per hash list if totally full and uniformly spread, but
2904a07723fSPavel Begunkov 	 * don't keep too many buckets to not overconsume memory.
291ed29b0b4SJens Axboe 	 */
2924a07723fSPavel Begunkov 	hash_bits = ilog2(p->cq_entries) - 5;
2934a07723fSPavel Begunkov 	hash_bits = clamp(hash_bits, 1, 8);
294e6f89be6SPavel Begunkov 	if (io_alloc_hash_table(&ctx->cancel_table, hash_bits))
295ed29b0b4SJens Axboe 		goto err;
2969ca9fb24SPavel Begunkov 	if (io_alloc_hash_table(&ctx->cancel_table_locked, hash_bits))
2979ca9fb24SPavel Begunkov 		goto err;
29838513c46SHao Xu 
299ed29b0b4SJens Axboe 	ctx->dummy_ubuf = kzalloc(sizeof(*ctx->dummy_ubuf), GFP_KERNEL);
300ed29b0b4SJens Axboe 	if (!ctx->dummy_ubuf)
301ed29b0b4SJens Axboe 		goto err;
302ed29b0b4SJens Axboe 	/* set invalid range, so io_import_fixed() fails meeting it */
303ed29b0b4SJens Axboe 	ctx->dummy_ubuf->ubuf = -1UL;
304ed29b0b4SJens Axboe 
305ed29b0b4SJens Axboe 	if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
30648904229SMichal Koutný 			    0, GFP_KERNEL))
307ed29b0b4SJens Axboe 		goto err;
308ed29b0b4SJens Axboe 
309ed29b0b4SJens Axboe 	ctx->flags = p->flags;
310ed29b0b4SJens Axboe 	init_waitqueue_head(&ctx->sqo_sq_wait);
311ed29b0b4SJens Axboe 	INIT_LIST_HEAD(&ctx->sqd_list);
312ed29b0b4SJens Axboe 	INIT_LIST_HEAD(&ctx->cq_overflow_list);
313ed29b0b4SJens Axboe 	INIT_LIST_HEAD(&ctx->io_buffers_cache);
31469bbc6adSPavel Begunkov 	io_alloc_cache_init(&ctx->rsrc_node_cache, IO_NODE_ALLOC_CACHE_MAX,
31569bbc6adSPavel Begunkov 			    sizeof(struct io_rsrc_node));
31669bbc6adSPavel Begunkov 	io_alloc_cache_init(&ctx->apoll_cache, IO_ALLOC_CACHE_MAX,
31769bbc6adSPavel Begunkov 			    sizeof(struct async_poll));
31869bbc6adSPavel Begunkov 	io_alloc_cache_init(&ctx->netmsg_cache, IO_ALLOC_CACHE_MAX,
31969bbc6adSPavel Begunkov 			    sizeof(struct io_async_msghdr));
320ed29b0b4SJens Axboe 	init_completion(&ctx->ref_comp);
321ed29b0b4SJens Axboe 	xa_init_flags(&ctx->personalities, XA_FLAGS_ALLOC1);
322ed29b0b4SJens Axboe 	mutex_init(&ctx->uring_lock);
323ed29b0b4SJens Axboe 	init_waitqueue_head(&ctx->cq_wait);
3247b235dd8SPavel Begunkov 	init_waitqueue_head(&ctx->poll_wq);
3254ea15b56SPavel Begunkov 	init_waitqueue_head(&ctx->rsrc_quiesce_wq);
326ed29b0b4SJens Axboe 	spin_lock_init(&ctx->completion_lock);
327ed29b0b4SJens Axboe 	spin_lock_init(&ctx->timeout_lock);
328ed29b0b4SJens Axboe 	INIT_WQ_LIST(&ctx->iopoll_list);
329ed29b0b4SJens Axboe 	INIT_LIST_HEAD(&ctx->io_buffers_pages);
330ed29b0b4SJens Axboe 	INIT_LIST_HEAD(&ctx->io_buffers_comp);
331ed29b0b4SJens Axboe 	INIT_LIST_HEAD(&ctx->defer_list);
332ed29b0b4SJens Axboe 	INIT_LIST_HEAD(&ctx->timeout_list);
333ed29b0b4SJens Axboe 	INIT_LIST_HEAD(&ctx->ltimeout_list);
334ed29b0b4SJens Axboe 	INIT_LIST_HEAD(&ctx->rsrc_ref_list);
335c0e0d6baSDylan Yudaken 	init_llist_head(&ctx->work_llist);
336ed29b0b4SJens Axboe 	INIT_LIST_HEAD(&ctx->tctx_list);
337ed29b0b4SJens Axboe 	ctx->submit_state.free_list.next = NULL;
338ed29b0b4SJens Axboe 	INIT_WQ_LIST(&ctx->locked_free_list);
339ed29b0b4SJens Axboe 	INIT_DELAYED_WORK(&ctx->fallback_work, io_fallback_req_func);
340ed29b0b4SJens Axboe 	INIT_WQ_LIST(&ctx->submit_state.compl_reqs);
341ed29b0b4SJens Axboe 	return ctx;
342ed29b0b4SJens Axboe err:
343ed29b0b4SJens Axboe 	kfree(ctx->dummy_ubuf);
344e6f89be6SPavel Begunkov 	kfree(ctx->cancel_table.hbs);
3459ca9fb24SPavel Begunkov 	kfree(ctx->cancel_table_locked.hbs);
346ed29b0b4SJens Axboe 	kfree(ctx->io_bl);
347ed29b0b4SJens Axboe 	xa_destroy(&ctx->io_bl_xa);
348ed29b0b4SJens Axboe 	kfree(ctx);
349ed29b0b4SJens Axboe 	return NULL;
350ed29b0b4SJens Axboe }
351ed29b0b4SJens Axboe 
352ed29b0b4SJens Axboe static void io_account_cq_overflow(struct io_ring_ctx *ctx)
353ed29b0b4SJens Axboe {
354ed29b0b4SJens Axboe 	struct io_rings *r = ctx->rings;
355ed29b0b4SJens Axboe 
356ed29b0b4SJens Axboe 	WRITE_ONCE(r->cq_overflow, READ_ONCE(r->cq_overflow) + 1);
357ed29b0b4SJens Axboe 	ctx->cq_extra--;
358ed29b0b4SJens Axboe }
359ed29b0b4SJens Axboe 
360ed29b0b4SJens Axboe static bool req_need_defer(struct io_kiocb *req, u32 seq)
361ed29b0b4SJens Axboe {
362ed29b0b4SJens Axboe 	if (unlikely(req->flags & REQ_F_IO_DRAIN)) {
363ed29b0b4SJens Axboe 		struct io_ring_ctx *ctx = req->ctx;
364ed29b0b4SJens Axboe 
365ed29b0b4SJens Axboe 		return seq + READ_ONCE(ctx->cq_extra) != ctx->cached_cq_tail;
366ed29b0b4SJens Axboe 	}
367ed29b0b4SJens Axboe 
368ed29b0b4SJens Axboe 	return false;
369ed29b0b4SJens Axboe }
370ed29b0b4SJens Axboe 
371ed29b0b4SJens Axboe static inline void io_req_track_inflight(struct io_kiocb *req)
372ed29b0b4SJens Axboe {
373ed29b0b4SJens Axboe 	if (!(req->flags & REQ_F_INFLIGHT)) {
374ed29b0b4SJens Axboe 		req->flags |= REQ_F_INFLIGHT;
375ed29b0b4SJens Axboe 		atomic_inc(&req->task->io_uring->inflight_tracked);
376ed29b0b4SJens Axboe 	}
377ed29b0b4SJens Axboe }
378ed29b0b4SJens Axboe 
379ed29b0b4SJens Axboe static struct io_kiocb *__io_prep_linked_timeout(struct io_kiocb *req)
380ed29b0b4SJens Axboe {
381ed29b0b4SJens Axboe 	if (WARN_ON_ONCE(!req->link))
382ed29b0b4SJens Axboe 		return NULL;
383ed29b0b4SJens Axboe 
384ed29b0b4SJens Axboe 	req->flags &= ~REQ_F_ARM_LTIMEOUT;
385ed29b0b4SJens Axboe 	req->flags |= REQ_F_LINK_TIMEOUT;
386ed29b0b4SJens Axboe 
387ed29b0b4SJens Axboe 	/* linked timeouts should have two refs once prep'ed */
388ed29b0b4SJens Axboe 	io_req_set_refcount(req);
389ed29b0b4SJens Axboe 	__io_req_set_refcount(req->link, 2);
390ed29b0b4SJens Axboe 	return req->link;
391ed29b0b4SJens Axboe }
392ed29b0b4SJens Axboe 
393ed29b0b4SJens Axboe static inline struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
394ed29b0b4SJens Axboe {
395ed29b0b4SJens Axboe 	if (likely(!(req->flags & REQ_F_ARM_LTIMEOUT)))
396ed29b0b4SJens Axboe 		return NULL;
397ed29b0b4SJens Axboe 	return __io_prep_linked_timeout(req);
398ed29b0b4SJens Axboe }
399ed29b0b4SJens Axboe 
400ed29b0b4SJens Axboe static noinline void __io_arm_ltimeout(struct io_kiocb *req)
401ed29b0b4SJens Axboe {
402ed29b0b4SJens Axboe 	io_queue_linked_timeout(__io_prep_linked_timeout(req));
403ed29b0b4SJens Axboe }
404ed29b0b4SJens Axboe 
405ed29b0b4SJens Axboe static inline void io_arm_ltimeout(struct io_kiocb *req)
406ed29b0b4SJens Axboe {
407ed29b0b4SJens Axboe 	if (unlikely(req->flags & REQ_F_ARM_LTIMEOUT))
408ed29b0b4SJens Axboe 		__io_arm_ltimeout(req);
409ed29b0b4SJens Axboe }
410ed29b0b4SJens Axboe 
411ed29b0b4SJens Axboe static void io_prep_async_work(struct io_kiocb *req)
412ed29b0b4SJens Axboe {
413a7dd2782SBreno Leitao 	const struct io_issue_def *def = &io_issue_defs[req->opcode];
414ed29b0b4SJens Axboe 	struct io_ring_ctx *ctx = req->ctx;
415ed29b0b4SJens Axboe 
416ed29b0b4SJens Axboe 	if (!(req->flags & REQ_F_CREDS)) {
417ed29b0b4SJens Axboe 		req->flags |= REQ_F_CREDS;
418ed29b0b4SJens Axboe 		req->creds = get_current_cred();
419ed29b0b4SJens Axboe 	}
420ed29b0b4SJens Axboe 
421ed29b0b4SJens Axboe 	req->work.list.next = NULL;
422ed29b0b4SJens Axboe 	req->work.flags = 0;
423ed29b0b4SJens Axboe 	req->work.cancel_seq = atomic_read(&ctx->cancel_seq);
424ed29b0b4SJens Axboe 	if (req->flags & REQ_F_FORCE_ASYNC)
425ed29b0b4SJens Axboe 		req->work.flags |= IO_WQ_WORK_CONCURRENT;
426ed29b0b4SJens Axboe 
427f6b543fdSJens Axboe 	if (req->file && !io_req_ffs_set(req))
428f6b543fdSJens Axboe 		req->flags |= io_file_get_flags(req->file) << REQ_F_SUPPORT_NOWAIT_BIT;
429f6b543fdSJens Axboe 
4308b1df11fSPavel Begunkov 	if (req->file && (req->flags & REQ_F_ISREG)) {
431d4755e15SJens Axboe 		bool should_hash = def->hash_reg_file;
432d4755e15SJens Axboe 
433d4755e15SJens Axboe 		/* don't serialize this request if the fs doesn't need it */
434d4755e15SJens Axboe 		if (should_hash && (req->file->f_flags & O_DIRECT) &&
435d4755e15SJens Axboe 		    (req->file->f_mode & FMODE_DIO_PARALLEL_WRITE))
436d4755e15SJens Axboe 			should_hash = false;
437d4755e15SJens Axboe 		if (should_hash || (ctx->flags & IORING_SETUP_IOPOLL))
438ed29b0b4SJens Axboe 			io_wq_hash_work(&req->work, file_inode(req->file));
439ed29b0b4SJens Axboe 	} else if (!req->file || !S_ISBLK(file_inode(req->file)->i_mode)) {
440ed29b0b4SJens Axboe 		if (def->unbound_nonreg_file)
441ed29b0b4SJens Axboe 			req->work.flags |= IO_WQ_WORK_UNBOUND;
442ed29b0b4SJens Axboe 	}
443ed29b0b4SJens Axboe }
444ed29b0b4SJens Axboe 
445ed29b0b4SJens Axboe static void io_prep_async_link(struct io_kiocb *req)
446ed29b0b4SJens Axboe {
447ed29b0b4SJens Axboe 	struct io_kiocb *cur;
448ed29b0b4SJens Axboe 
449ed29b0b4SJens Axboe 	if (req->flags & REQ_F_LINK_TIMEOUT) {
450ed29b0b4SJens Axboe 		struct io_ring_ctx *ctx = req->ctx;
451ed29b0b4SJens Axboe 
452ed29b0b4SJens Axboe 		spin_lock_irq(&ctx->timeout_lock);
453ed29b0b4SJens Axboe 		io_for_each_link(cur, req)
454ed29b0b4SJens Axboe 			io_prep_async_work(cur);
455ed29b0b4SJens Axboe 		spin_unlock_irq(&ctx->timeout_lock);
456ed29b0b4SJens Axboe 	} else {
457ed29b0b4SJens Axboe 		io_for_each_link(cur, req)
458ed29b0b4SJens Axboe 			io_prep_async_work(cur);
459ed29b0b4SJens Axboe 	}
460ed29b0b4SJens Axboe }
461ed29b0b4SJens Axboe 
462a282967cSPavel Begunkov void io_queue_iowq(struct io_kiocb *req, struct io_tw_state *ts_dont_use)
463ed29b0b4SJens Axboe {
464ed29b0b4SJens Axboe 	struct io_kiocb *link = io_prep_linked_timeout(req);
465ed29b0b4SJens Axboe 	struct io_uring_task *tctx = req->task->io_uring;
466ed29b0b4SJens Axboe 
467ed29b0b4SJens Axboe 	BUG_ON(!tctx);
468ed29b0b4SJens Axboe 	BUG_ON(!tctx->io_wq);
469ed29b0b4SJens Axboe 
470ed29b0b4SJens Axboe 	/* init ->work of the whole link before punting */
471ed29b0b4SJens Axboe 	io_prep_async_link(req);
472ed29b0b4SJens Axboe 
473ed29b0b4SJens Axboe 	/*
474ed29b0b4SJens Axboe 	 * Not expected to happen, but if we do have a bug where this _can_
475ed29b0b4SJens Axboe 	 * happen, catch it here and ensure the request is marked as
476ed29b0b4SJens Axboe 	 * canceled. That will make io-wq go through the usual work cancel
477ed29b0b4SJens Axboe 	 * procedure rather than attempt to run this request (or create a new
478ed29b0b4SJens Axboe 	 * worker for it).
479ed29b0b4SJens Axboe 	 */
480ed29b0b4SJens Axboe 	if (WARN_ON_ONCE(!same_thread_group(req->task, current)))
481ed29b0b4SJens Axboe 		req->work.flags |= IO_WQ_WORK_CANCEL;
482ed29b0b4SJens Axboe 
48348863ffdSPavel Begunkov 	trace_io_uring_queue_async_work(req, io_wq_is_hashed(&req->work));
484ed29b0b4SJens Axboe 	io_wq_enqueue(tctx->io_wq, &req->work);
485ed29b0b4SJens Axboe 	if (link)
486ed29b0b4SJens Axboe 		io_queue_linked_timeout(link);
487ed29b0b4SJens Axboe }
488ed29b0b4SJens Axboe 
489ed29b0b4SJens Axboe static __cold void io_queue_deferred(struct io_ring_ctx *ctx)
490ed29b0b4SJens Axboe {
491ed29b0b4SJens Axboe 	while (!list_empty(&ctx->defer_list)) {
492ed29b0b4SJens Axboe 		struct io_defer_entry *de = list_first_entry(&ctx->defer_list,
493ed29b0b4SJens Axboe 						struct io_defer_entry, list);
494ed29b0b4SJens Axboe 
495ed29b0b4SJens Axboe 		if (req_need_defer(de->req, de->seq))
496ed29b0b4SJens Axboe 			break;
497ed29b0b4SJens Axboe 		list_del_init(&de->list);
498ed29b0b4SJens Axboe 		io_req_task_queue(de->req);
499ed29b0b4SJens Axboe 		kfree(de);
500ed29b0b4SJens Axboe 	}
501ed29b0b4SJens Axboe }
502ed29b0b4SJens Axboe 
50321a091b9SDylan Yudaken 
50421a091b9SDylan Yudaken static void io_eventfd_ops(struct rcu_head *rcu)
505d8e9214fSDylan Yudaken {
506d8e9214fSDylan Yudaken 	struct io_ev_fd *ev_fd = container_of(rcu, struct io_ev_fd, rcu);
50721a091b9SDylan Yudaken 	int ops = atomic_xchg(&ev_fd->ops, 0);
508d8e9214fSDylan Yudaken 
50921a091b9SDylan Yudaken 	if (ops & BIT(IO_EVENTFD_OP_SIGNAL_BIT))
51044648532SJens Axboe 		eventfd_signal_mask(ev_fd->cq_ev_fd, 1, EPOLL_URING_WAKE);
51121a091b9SDylan Yudaken 
51221a091b9SDylan Yudaken 	/* IO_EVENTFD_OP_FREE_BIT may not be set here depending on callback
51321a091b9SDylan Yudaken 	 * ordering in a race but if references are 0 we know we have to free
51421a091b9SDylan Yudaken 	 * it regardless.
51521a091b9SDylan Yudaken 	 */
51621a091b9SDylan Yudaken 	if (atomic_dec_and_test(&ev_fd->refs)) {
517d8e9214fSDylan Yudaken 		eventfd_ctx_put(ev_fd->cq_ev_fd);
518d8e9214fSDylan Yudaken 		kfree(ev_fd);
519d8e9214fSDylan Yudaken 	}
52021a091b9SDylan Yudaken }
521d8e9214fSDylan Yudaken 
522ed29b0b4SJens Axboe static void io_eventfd_signal(struct io_ring_ctx *ctx)
523ed29b0b4SJens Axboe {
52421a091b9SDylan Yudaken 	struct io_ev_fd *ev_fd = NULL;
525ed29b0b4SJens Axboe 
526ed29b0b4SJens Axboe 	rcu_read_lock();
527ed29b0b4SJens Axboe 	/*
528ed29b0b4SJens Axboe 	 * rcu_dereference ctx->io_ev_fd once and use it for both for checking
529ed29b0b4SJens Axboe 	 * and eventfd_signal
530ed29b0b4SJens Axboe 	 */
531ed29b0b4SJens Axboe 	ev_fd = rcu_dereference(ctx->io_ev_fd);
532ed29b0b4SJens Axboe 
533ed29b0b4SJens Axboe 	/*
534ed29b0b4SJens Axboe 	 * Check again if ev_fd exists incase an io_eventfd_unregister call
535ed29b0b4SJens Axboe 	 * completed between the NULL check of ctx->io_ev_fd at the start of
536ed29b0b4SJens Axboe 	 * the function and rcu_read_lock.
537ed29b0b4SJens Axboe 	 */
538ed29b0b4SJens Axboe 	if (unlikely(!ev_fd))
539ed29b0b4SJens Axboe 		goto out;
540ed29b0b4SJens Axboe 	if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED)
541ed29b0b4SJens Axboe 		goto out;
54221a091b9SDylan Yudaken 	if (ev_fd->eventfd_async && !io_wq_current_is_worker())
54321a091b9SDylan Yudaken 		goto out;
544ed29b0b4SJens Axboe 
54521a091b9SDylan Yudaken 	if (likely(eventfd_signal_allowed())) {
54644648532SJens Axboe 		eventfd_signal_mask(ev_fd->cq_ev_fd, 1, EPOLL_URING_WAKE);
54721a091b9SDylan Yudaken 	} else {
54821a091b9SDylan Yudaken 		atomic_inc(&ev_fd->refs);
54921a091b9SDylan Yudaken 		if (!atomic_fetch_or(BIT(IO_EVENTFD_OP_SIGNAL_BIT), &ev_fd->ops))
55044a84da4SDylan Yudaken 			call_rcu_hurry(&ev_fd->rcu, io_eventfd_ops);
55121a091b9SDylan Yudaken 		else
55221a091b9SDylan Yudaken 			atomic_dec(&ev_fd->refs);
55321a091b9SDylan Yudaken 	}
55421a091b9SDylan Yudaken 
555ed29b0b4SJens Axboe out:
556ed29b0b4SJens Axboe 	rcu_read_unlock();
557ed29b0b4SJens Axboe }
558ed29b0b4SJens Axboe 
55921a091b9SDylan Yudaken static void io_eventfd_flush_signal(struct io_ring_ctx *ctx)
56021a091b9SDylan Yudaken {
56121a091b9SDylan Yudaken 	bool skip;
56221a091b9SDylan Yudaken 
56321a091b9SDylan Yudaken 	spin_lock(&ctx->completion_lock);
56421a091b9SDylan Yudaken 
56521a091b9SDylan Yudaken 	/*
56621a091b9SDylan Yudaken 	 * Eventfd should only get triggered when at least one event has been
56721a091b9SDylan Yudaken 	 * posted. Some applications rely on the eventfd notification count
56821a091b9SDylan Yudaken 	 * only changing IFF a new CQE has been added to the CQ ring. There's
56921a091b9SDylan Yudaken 	 * no depedency on 1:1 relationship between how many times this
57021a091b9SDylan Yudaken 	 * function is called (and hence the eventfd count) and number of CQEs
57121a091b9SDylan Yudaken 	 * posted to the CQ ring.
57221a091b9SDylan Yudaken 	 */
57321a091b9SDylan Yudaken 	skip = ctx->cached_cq_tail == ctx->evfd_last_cq_tail;
57421a091b9SDylan Yudaken 	ctx->evfd_last_cq_tail = ctx->cached_cq_tail;
57521a091b9SDylan Yudaken 	spin_unlock(&ctx->completion_lock);
57621a091b9SDylan Yudaken 	if (skip)
57721a091b9SDylan Yudaken 		return;
57821a091b9SDylan Yudaken 
57921a091b9SDylan Yudaken 	io_eventfd_signal(ctx);
58021a091b9SDylan Yudaken }
58121a091b9SDylan Yudaken 
582a830ffd2SPavel Begunkov void __io_commit_cqring_flush(struct io_ring_ctx *ctx)
583a830ffd2SPavel Begunkov {
584bca39f39SPavel Begunkov 	if (ctx->poll_activated)
585bca39f39SPavel Begunkov 		io_poll_wq_wake(ctx);
586a830ffd2SPavel Begunkov 	if (ctx->off_timeout_used)
587a830ffd2SPavel Begunkov 		io_flush_timeouts(ctx);
588e5f30f6fSPavel Begunkov 	if (ctx->drain_active) {
589e5f30f6fSPavel Begunkov 		spin_lock(&ctx->completion_lock);
590a830ffd2SPavel Begunkov 		io_queue_deferred(ctx);
591a830ffd2SPavel Begunkov 		spin_unlock(&ctx->completion_lock);
592a830ffd2SPavel Begunkov 	}
593a830ffd2SPavel Begunkov 	if (ctx->has_evfd)
59421a091b9SDylan Yudaken 		io_eventfd_flush_signal(ctx);
595a830ffd2SPavel Begunkov }
596a830ffd2SPavel Begunkov 
597f66f7342SPavel Begunkov static inline void __io_cq_lock(struct io_ring_ctx *ctx)
598f66f7342SPavel Begunkov 	__acquires(ctx->completion_lock)
599f66f7342SPavel Begunkov {
600f66f7342SPavel Begunkov 	if (!ctx->task_complete)
601f66f7342SPavel Begunkov 		spin_lock(&ctx->completion_lock);
602f66f7342SPavel Begunkov }
603f66f7342SPavel Begunkov 
604f66f7342SPavel Begunkov static inline void __io_cq_unlock(struct io_ring_ctx *ctx)
605f66f7342SPavel Begunkov {
606f66f7342SPavel Begunkov 	if (!ctx->task_complete)
607f66f7342SPavel Begunkov 		spin_unlock(&ctx->completion_lock);
608f66f7342SPavel Begunkov }
609f66f7342SPavel Begunkov 
6106971253fSPavel Begunkov static inline void io_cq_lock(struct io_ring_ctx *ctx)
6116971253fSPavel Begunkov 	__acquires(ctx->completion_lock)
6126971253fSPavel Begunkov {
6136971253fSPavel Begunkov 	spin_lock(&ctx->completion_lock);
6146971253fSPavel Begunkov }
6156971253fSPavel Begunkov 
6166971253fSPavel Begunkov static inline void io_cq_unlock(struct io_ring_ctx *ctx)
6176971253fSPavel Begunkov 	__releases(ctx->completion_lock)
6186971253fSPavel Begunkov {
6196971253fSPavel Begunkov 	spin_unlock(&ctx->completion_lock);
6206971253fSPavel Begunkov }
6216971253fSPavel Begunkov 
6225d772916SPavel Begunkov /* keep it inlined for io_submit_flush_completions() */
623f66f7342SPavel Begunkov static inline void __io_cq_unlock_post(struct io_ring_ctx *ctx)
62425399321SPavel Begunkov 	__releases(ctx->completion_lock)
62525399321SPavel Begunkov {
62625399321SPavel Begunkov 	io_commit_cqring(ctx);
627f66f7342SPavel Begunkov 	__io_cq_unlock(ctx);
6286c16fe3cSJens Axboe 	io_commit_cqring_flush(ctx);
6296c16fe3cSJens Axboe 	io_cqring_wake(ctx);
63025399321SPavel Begunkov }
63125399321SPavel Begunkov 
632c66ae3ecSPavel Begunkov static void __io_cq_unlock_post_flush(struct io_ring_ctx *ctx)
6333181e22fSPavel Begunkov 	__releases(ctx->completion_lock)
6343181e22fSPavel Begunkov {
6353181e22fSPavel Begunkov 	io_commit_cqring(ctx);
636c66ae3ecSPavel Begunkov 
637c66ae3ecSPavel Begunkov 	if (ctx->task_complete) {
638c66ae3ecSPavel Begunkov 		/*
639c66ae3ecSPavel Begunkov 		 * ->task_complete implies that only current might be waiting
640c66ae3ecSPavel Begunkov 		 * for CQEs, and obviously, we currently don't. No one is
641c66ae3ecSPavel Begunkov 		 * waiting, wakeups are futile, skip them.
642c66ae3ecSPavel Begunkov 		 */
643c66ae3ecSPavel Begunkov 		io_commit_cqring_flush(ctx);
644c66ae3ecSPavel Begunkov 	} else {
6453181e22fSPavel Begunkov 		__io_cq_unlock(ctx);
6463181e22fSPavel Begunkov 		io_commit_cqring_flush(ctx);
6476e7248adSPavel Begunkov 		io_cqring_wake(ctx);
6483181e22fSPavel Begunkov 	}
6493181e22fSPavel Begunkov }
6503181e22fSPavel Begunkov 
65125399321SPavel Begunkov void io_cq_unlock_post(struct io_ring_ctx *ctx)
6525d772916SPavel Begunkov 	__releases(ctx->completion_lock)
65325399321SPavel Begunkov {
654f66f7342SPavel Begunkov 	io_commit_cqring(ctx);
655f66f7342SPavel Begunkov 	spin_unlock(&ctx->completion_lock);
656f66f7342SPavel Begunkov 	io_commit_cqring_flush(ctx);
657f66f7342SPavel Begunkov 	io_cqring_wake(ctx);
65825399321SPavel Begunkov }
65925399321SPavel Begunkov 
660ed29b0b4SJens Axboe /* Returns true if there are no backlogged entries after the flush */
661a85381d8SPavel Begunkov static void io_cqring_overflow_kill(struct io_ring_ctx *ctx)
662ed29b0b4SJens Axboe {
663a85381d8SPavel Begunkov 	struct io_overflow_cqe *ocqe;
664a85381d8SPavel Begunkov 	LIST_HEAD(list);
665a85381d8SPavel Begunkov 
666a85381d8SPavel Begunkov 	io_cq_lock(ctx);
667a85381d8SPavel Begunkov 	list_splice_init(&ctx->cq_overflow_list, &list);
668a85381d8SPavel Begunkov 	clear_bit(IO_CHECK_CQ_OVERFLOW_BIT, &ctx->check_cq);
669a85381d8SPavel Begunkov 	io_cq_unlock(ctx);
670a85381d8SPavel Begunkov 
671a85381d8SPavel Begunkov 	while (!list_empty(&list)) {
672a85381d8SPavel Begunkov 		ocqe = list_first_entry(&list, struct io_overflow_cqe, list);
673a85381d8SPavel Begunkov 		list_del(&ocqe->list);
674a85381d8SPavel Begunkov 		kfree(ocqe);
675a85381d8SPavel Begunkov 	}
676a85381d8SPavel Begunkov }
677a85381d8SPavel Begunkov 
6781b346e4aSPavel Begunkov static void __io_cqring_overflow_flush(struct io_ring_ctx *ctx)
679ed29b0b4SJens Axboe {
680ed29b0b4SJens Axboe 	size_t cqe_size = sizeof(struct io_uring_cqe);
681ed29b0b4SJens Axboe 
682a85381d8SPavel Begunkov 	if (__io_cqring_events(ctx) == ctx->cq_entries)
6831b346e4aSPavel Begunkov 		return;
684ed29b0b4SJens Axboe 
685ed29b0b4SJens Axboe 	if (ctx->flags & IORING_SETUP_CQE32)
686ed29b0b4SJens Axboe 		cqe_size <<= 1;
687ed29b0b4SJens Axboe 
68825399321SPavel Begunkov 	io_cq_lock(ctx);
689ed29b0b4SJens Axboe 	while (!list_empty(&ctx->cq_overflow_list)) {
690aa1df3a3SPavel Begunkov 		struct io_uring_cqe *cqe = io_get_cqe_overflow(ctx, true);
691ed29b0b4SJens Axboe 		struct io_overflow_cqe *ocqe;
692ed29b0b4SJens Axboe 
693a85381d8SPavel Begunkov 		if (!cqe)
694ed29b0b4SJens Axboe 			break;
695ed29b0b4SJens Axboe 		ocqe = list_first_entry(&ctx->cq_overflow_list,
696ed29b0b4SJens Axboe 					struct io_overflow_cqe, list);
697ed29b0b4SJens Axboe 		memcpy(cqe, &ocqe->cqe, cqe_size);
698ed29b0b4SJens Axboe 		list_del(&ocqe->list);
699ed29b0b4SJens Axboe 		kfree(ocqe);
700ed29b0b4SJens Axboe 	}
701ed29b0b4SJens Axboe 
7021b346e4aSPavel Begunkov 	if (list_empty(&ctx->cq_overflow_list)) {
703ed29b0b4SJens Axboe 		clear_bit(IO_CHECK_CQ_OVERFLOW_BIT, &ctx->check_cq);
704ed29b0b4SJens Axboe 		atomic_andnot(IORING_SQ_CQ_OVERFLOW, &ctx->rings->sq_flags);
705ed29b0b4SJens Axboe 	}
70625399321SPavel Begunkov 	io_cq_unlock_post(ctx);
707ed29b0b4SJens Axboe }
708ed29b0b4SJens Axboe 
70952ea806aSJens Axboe static void io_cqring_do_overflow_flush(struct io_ring_ctx *ctx)
710ed29b0b4SJens Axboe {
711ed29b0b4SJens Axboe 	/* iopoll syncs against uring_lock, not completion_lock */
712ed29b0b4SJens Axboe 	if (ctx->flags & IORING_SETUP_IOPOLL)
713ed29b0b4SJens Axboe 		mutex_lock(&ctx->uring_lock);
7141b346e4aSPavel Begunkov 	__io_cqring_overflow_flush(ctx);
715ed29b0b4SJens Axboe 	if (ctx->flags & IORING_SETUP_IOPOLL)
716ed29b0b4SJens Axboe 		mutex_unlock(&ctx->uring_lock);
717ed29b0b4SJens Axboe }
71852ea806aSJens Axboe 
71952ea806aSJens Axboe static void io_cqring_overflow_flush(struct io_ring_ctx *ctx)
72052ea806aSJens Axboe {
72152ea806aSJens Axboe 	if (test_bit(IO_CHECK_CQ_OVERFLOW_BIT, &ctx->check_cq))
72252ea806aSJens Axboe 		io_cqring_do_overflow_flush(ctx);
723ed29b0b4SJens Axboe }
724ed29b0b4SJens Axboe 
7255afa4650SPavel Begunkov /* can be called by any task */
7265afa4650SPavel Begunkov static void io_put_task_remote(struct task_struct *task, int nr)
727ed29b0b4SJens Axboe {
728ed29b0b4SJens Axboe 	struct io_uring_task *tctx = task->io_uring;
729ed29b0b4SJens Axboe 
730ed29b0b4SJens Axboe 	percpu_counter_sub(&tctx->inflight, nr);
7318d664282SJens Axboe 	if (unlikely(atomic_read(&tctx->in_cancel)))
732ed29b0b4SJens Axboe 		wake_up(&tctx->wait);
733ed29b0b4SJens Axboe 	put_task_struct_many(task, nr);
734ed29b0b4SJens Axboe }
735ed29b0b4SJens Axboe 
7365afa4650SPavel Begunkov /* used by a task to put its own references */
7375afa4650SPavel Begunkov static void io_put_task_local(struct task_struct *task, int nr)
7385afa4650SPavel Begunkov {
7395afa4650SPavel Begunkov 	task->io_uring->cached_refs += nr;
7405afa4650SPavel Begunkov }
7415afa4650SPavel Begunkov 
74289800a2dSPavel Begunkov /* must to be called somewhat shortly after putting a request */
74389800a2dSPavel Begunkov static inline void io_put_task(struct task_struct *task, int nr)
74489800a2dSPavel Begunkov {
74589800a2dSPavel Begunkov 	if (likely(task == current))
7465afa4650SPavel Begunkov 		io_put_task_local(task, nr);
74789800a2dSPavel Begunkov 	else
7485afa4650SPavel Begunkov 		io_put_task_remote(task, nr);
74989800a2dSPavel Begunkov }
75089800a2dSPavel Begunkov 
75163809137SPavel Begunkov void io_task_refs_refill(struct io_uring_task *tctx)
752ed29b0b4SJens Axboe {
753ed29b0b4SJens Axboe 	unsigned int refill = -tctx->cached_refs + IO_TCTX_REFS_CACHE_NR;
754ed29b0b4SJens Axboe 
755ed29b0b4SJens Axboe 	percpu_counter_add(&tctx->inflight, refill);
756ed29b0b4SJens Axboe 	refcount_add(refill, &current->usage);
757ed29b0b4SJens Axboe 	tctx->cached_refs += refill;
758ed29b0b4SJens Axboe }
759ed29b0b4SJens Axboe 
760ed29b0b4SJens Axboe static __cold void io_uring_drop_tctx_refs(struct task_struct *task)
761ed29b0b4SJens Axboe {
762ed29b0b4SJens Axboe 	struct io_uring_task *tctx = task->io_uring;
763ed29b0b4SJens Axboe 	unsigned int refs = tctx->cached_refs;
764ed29b0b4SJens Axboe 
765ed29b0b4SJens Axboe 	if (refs) {
766ed29b0b4SJens Axboe 		tctx->cached_refs = 0;
767ed29b0b4SJens Axboe 		percpu_counter_sub(&tctx->inflight, refs);
768ed29b0b4SJens Axboe 		put_task_struct_many(task, refs);
769ed29b0b4SJens Axboe 	}
770ed29b0b4SJens Axboe }
771ed29b0b4SJens Axboe 
77268494a65SPavel Begunkov static bool io_cqring_event_overflow(struct io_ring_ctx *ctx, u64 user_data,
77368494a65SPavel Begunkov 				     s32 res, u32 cflags, u64 extra1, u64 extra2)
774ed29b0b4SJens Axboe {
775ed29b0b4SJens Axboe 	struct io_overflow_cqe *ocqe;
776ed29b0b4SJens Axboe 	size_t ocq_size = sizeof(struct io_overflow_cqe);
777ed29b0b4SJens Axboe 	bool is_cqe32 = (ctx->flags & IORING_SETUP_CQE32);
778ed29b0b4SJens Axboe 
779f26cc959SPavel Begunkov 	lockdep_assert_held(&ctx->completion_lock);
780f26cc959SPavel Begunkov 
781ed29b0b4SJens Axboe 	if (is_cqe32)
782ed29b0b4SJens Axboe 		ocq_size += sizeof(struct io_uring_cqe);
783ed29b0b4SJens Axboe 
784ed29b0b4SJens Axboe 	ocqe = kmalloc(ocq_size, GFP_ATOMIC | __GFP_ACCOUNT);
785ed29b0b4SJens Axboe 	trace_io_uring_cqe_overflow(ctx, user_data, res, cflags, ocqe);
786ed29b0b4SJens Axboe 	if (!ocqe) {
787ed29b0b4SJens Axboe 		/*
788ed29b0b4SJens Axboe 		 * If we're in ring overflow flush mode, or in task cancel mode,
789ed29b0b4SJens Axboe 		 * or cannot allocate an overflow entry, then we need to drop it
790ed29b0b4SJens Axboe 		 * on the floor.
791ed29b0b4SJens Axboe 		 */
792ed29b0b4SJens Axboe 		io_account_cq_overflow(ctx);
793ed29b0b4SJens Axboe 		set_bit(IO_CHECK_CQ_DROPPED_BIT, &ctx->check_cq);
794ed29b0b4SJens Axboe 		return false;
795ed29b0b4SJens Axboe 	}
796ed29b0b4SJens Axboe 	if (list_empty(&ctx->cq_overflow_list)) {
797ed29b0b4SJens Axboe 		set_bit(IO_CHECK_CQ_OVERFLOW_BIT, &ctx->check_cq);
798ed29b0b4SJens Axboe 		atomic_or(IORING_SQ_CQ_OVERFLOW, &ctx->rings->sq_flags);
799ed29b0b4SJens Axboe 
800ed29b0b4SJens Axboe 	}
801ed29b0b4SJens Axboe 	ocqe->cqe.user_data = user_data;
802ed29b0b4SJens Axboe 	ocqe->cqe.res = res;
803ed29b0b4SJens Axboe 	ocqe->cqe.flags = cflags;
804ed29b0b4SJens Axboe 	if (is_cqe32) {
805ed29b0b4SJens Axboe 		ocqe->cqe.big_cqe[0] = extra1;
806ed29b0b4SJens Axboe 		ocqe->cqe.big_cqe[1] = extra2;
807ed29b0b4SJens Axboe 	}
808ed29b0b4SJens Axboe 	list_add_tail(&ocqe->list, &ctx->cq_overflow_list);
809ed29b0b4SJens Axboe 	return true;
810ed29b0b4SJens Axboe }
811ed29b0b4SJens Axboe 
81268494a65SPavel Begunkov bool io_req_cqe_overflow(struct io_kiocb *req)
81368494a65SPavel Begunkov {
81468494a65SPavel Begunkov 	if (!(req->flags & REQ_F_CQE32_INIT)) {
81568494a65SPavel Begunkov 		req->extra1 = 0;
81668494a65SPavel Begunkov 		req->extra2 = 0;
81768494a65SPavel Begunkov 	}
81868494a65SPavel Begunkov 	return io_cqring_event_overflow(req->ctx, req->cqe.user_data,
81968494a65SPavel Begunkov 					req->cqe.res, req->cqe.flags,
82068494a65SPavel Begunkov 					req->extra1, req->extra2);
82168494a65SPavel Begunkov }
82268494a65SPavel Begunkov 
823faf88ddeSPavel Begunkov /*
824faf88ddeSPavel Begunkov  * writes to the cq entry need to come after reading head; the
825faf88ddeSPavel Begunkov  * control dependency is enough as we're using WRITE_ONCE to
826faf88ddeSPavel Begunkov  * fill the cq entry
827faf88ddeSPavel Begunkov  */
828aa1df3a3SPavel Begunkov struct io_uring_cqe *__io_get_cqe(struct io_ring_ctx *ctx, bool overflow)
829faf88ddeSPavel Begunkov {
830faf88ddeSPavel Begunkov 	struct io_rings *rings = ctx->rings;
831faf88ddeSPavel Begunkov 	unsigned int off = ctx->cached_cq_tail & (ctx->cq_entries - 1);
832faf88ddeSPavel Begunkov 	unsigned int free, queued, len;
833faf88ddeSPavel Begunkov 
834aa1df3a3SPavel Begunkov 	/*
835aa1df3a3SPavel Begunkov 	 * Posting into the CQ when there are pending overflowed CQEs may break
836aa1df3a3SPavel Begunkov 	 * ordering guarantees, which will affect links, F_MORE users and more.
837aa1df3a3SPavel Begunkov 	 * Force overflow the completion.
838aa1df3a3SPavel Begunkov 	 */
839aa1df3a3SPavel Begunkov 	if (!overflow && (ctx->check_cq & BIT(IO_CHECK_CQ_OVERFLOW_BIT)))
840aa1df3a3SPavel Begunkov 		return NULL;
841faf88ddeSPavel Begunkov 
842faf88ddeSPavel Begunkov 	/* userspace may cheat modifying the tail, be safe and do min */
843faf88ddeSPavel Begunkov 	queued = min(__io_cqring_events(ctx), ctx->cq_entries);
844faf88ddeSPavel Begunkov 	free = ctx->cq_entries - queued;
845faf88ddeSPavel Begunkov 	/* we need a contiguous range, limit based on the current array offset */
846faf88ddeSPavel Begunkov 	len = min(free, ctx->cq_entries - off);
847faf88ddeSPavel Begunkov 	if (!len)
848faf88ddeSPavel Begunkov 		return NULL;
849faf88ddeSPavel Begunkov 
850b3659a65SPavel Begunkov 	if (ctx->flags & IORING_SETUP_CQE32) {
851b3659a65SPavel Begunkov 		off <<= 1;
852b3659a65SPavel Begunkov 		len <<= 1;
853b3659a65SPavel Begunkov 	}
854b3659a65SPavel Begunkov 
855faf88ddeSPavel Begunkov 	ctx->cqe_cached = &rings->cqes[off];
856faf88ddeSPavel Begunkov 	ctx->cqe_sentinel = ctx->cqe_cached + len;
857b3659a65SPavel Begunkov 
858b3659a65SPavel Begunkov 	ctx->cached_cq_tail++;
859faf88ddeSPavel Begunkov 	ctx->cqe_cached++;
860b3659a65SPavel Begunkov 	if (ctx->flags & IORING_SETUP_CQE32)
861b3659a65SPavel Begunkov 		ctx->cqe_cached++;
862b3659a65SPavel Begunkov 	return &rings->cqes[off];
863faf88ddeSPavel Begunkov }
864faf88ddeSPavel Begunkov 
865f66f7342SPavel Begunkov static bool io_fill_cqe_aux(struct io_ring_ctx *ctx, u64 user_data, s32 res,
866f66f7342SPavel Begunkov 			      u32 cflags)
867ed29b0b4SJens Axboe {
868ed29b0b4SJens Axboe 	struct io_uring_cqe *cqe;
869ed29b0b4SJens Axboe 
870ed29b0b4SJens Axboe 	ctx->cq_extra++;
871ed29b0b4SJens Axboe 
872ed29b0b4SJens Axboe 	/*
873ed29b0b4SJens Axboe 	 * If we can't get a cq entry, userspace overflowed the
874ed29b0b4SJens Axboe 	 * submission (by quite a lot). Increment the overflow count in
875ed29b0b4SJens Axboe 	 * the ring.
876ed29b0b4SJens Axboe 	 */
877ed29b0b4SJens Axboe 	cqe = io_get_cqe(ctx);
878ed29b0b4SJens Axboe 	if (likely(cqe)) {
879e0486f3fSDylan Yudaken 		trace_io_uring_complete(ctx, NULL, user_data, res, cflags, 0, 0);
880e0486f3fSDylan Yudaken 
881ed29b0b4SJens Axboe 		WRITE_ONCE(cqe->user_data, user_data);
882ed29b0b4SJens Axboe 		WRITE_ONCE(cqe->res, res);
883ed29b0b4SJens Axboe 		WRITE_ONCE(cqe->flags, cflags);
884ed29b0b4SJens Axboe 
885ed29b0b4SJens Axboe 		if (ctx->flags & IORING_SETUP_CQE32) {
886ed29b0b4SJens Axboe 			WRITE_ONCE(cqe->big_cqe[0], 0);
887ed29b0b4SJens Axboe 			WRITE_ONCE(cqe->big_cqe[1], 0);
888ed29b0b4SJens Axboe 		}
889ed29b0b4SJens Axboe 		return true;
890ed29b0b4SJens Axboe 	}
89152120f0fSDylan Yudaken 	return false;
892ed29b0b4SJens Axboe }
893ed29b0b4SJens Axboe 
894931147ddSDylan Yudaken static void __io_flush_post_cqes(struct io_ring_ctx *ctx)
895931147ddSDylan Yudaken 	__must_hold(&ctx->uring_lock)
896931147ddSDylan Yudaken {
897931147ddSDylan Yudaken 	struct io_submit_state *state = &ctx->submit_state;
898931147ddSDylan Yudaken 	unsigned int i;
899931147ddSDylan Yudaken 
900931147ddSDylan Yudaken 	lockdep_assert_held(&ctx->uring_lock);
901931147ddSDylan Yudaken 	for (i = 0; i < state->cqes_count; i++) {
902931147ddSDylan Yudaken 		struct io_uring_cqe *cqe = &state->cqes[i];
903931147ddSDylan Yudaken 
904f66f7342SPavel Begunkov 		if (!io_fill_cqe_aux(ctx, cqe->user_data, cqe->res, cqe->flags)) {
905f66f7342SPavel Begunkov 			if (ctx->task_complete) {
906f66f7342SPavel Begunkov 				spin_lock(&ctx->completion_lock);
907f66f7342SPavel Begunkov 				io_cqring_event_overflow(ctx, cqe->user_data,
908f66f7342SPavel Begunkov 							cqe->res, cqe->flags, 0, 0);
909f66f7342SPavel Begunkov 				spin_unlock(&ctx->completion_lock);
910f66f7342SPavel Begunkov 			} else {
911f66f7342SPavel Begunkov 				io_cqring_event_overflow(ctx, cqe->user_data,
912f66f7342SPavel Begunkov 							cqe->res, cqe->flags, 0, 0);
913f66f7342SPavel Begunkov 			}
914f66f7342SPavel Begunkov 		}
915931147ddSDylan Yudaken 	}
916931147ddSDylan Yudaken 	state->cqes_count = 0;
917931147ddSDylan Yudaken }
918931147ddSDylan Yudaken 
919b529c96aSDylan Yudaken static bool __io_post_aux_cqe(struct io_ring_ctx *ctx, u64 user_data, s32 res, u32 cflags,
92052120f0fSDylan Yudaken 			      bool allow_overflow)
921d245bca6SPavel Begunkov {
922d245bca6SPavel Begunkov 	bool filled;
923d245bca6SPavel Begunkov 
92425399321SPavel Begunkov 	io_cq_lock(ctx);
925f66f7342SPavel Begunkov 	filled = io_fill_cqe_aux(ctx, user_data, res, cflags);
926f66f7342SPavel Begunkov 	if (!filled && allow_overflow)
927f66f7342SPavel Begunkov 		filled = io_cqring_event_overflow(ctx, user_data, res, cflags, 0, 0);
928f66f7342SPavel Begunkov 
92925399321SPavel Begunkov 	io_cq_unlock_post(ctx);
930d245bca6SPavel Begunkov 	return filled;
931d245bca6SPavel Begunkov }
932d245bca6SPavel Begunkov 
933b529c96aSDylan Yudaken bool io_post_aux_cqe(struct io_ring_ctx *ctx, u64 user_data, s32 res, u32 cflags)
934ed29b0b4SJens Axboe {
935b529c96aSDylan Yudaken 	return __io_post_aux_cqe(ctx, user_data, res, cflags, true);
936b529c96aSDylan Yudaken }
937b529c96aSDylan Yudaken 
9389b8c5475SDylan Yudaken bool io_aux_cqe(struct io_ring_ctx *ctx, bool defer, u64 user_data, s32 res, u32 cflags,
9399b8c5475SDylan Yudaken 		bool allow_overflow)
9409b8c5475SDylan Yudaken {
9419b8c5475SDylan Yudaken 	struct io_uring_cqe *cqe;
9429b8c5475SDylan Yudaken 	unsigned int length;
9439b8c5475SDylan Yudaken 
9449b8c5475SDylan Yudaken 	if (!defer)
945b529c96aSDylan Yudaken 		return __io_post_aux_cqe(ctx, user_data, res, cflags, allow_overflow);
9469b8c5475SDylan Yudaken 
9479b8c5475SDylan Yudaken 	length = ARRAY_SIZE(ctx->submit_state.cqes);
9489b8c5475SDylan Yudaken 
9499b8c5475SDylan Yudaken 	lockdep_assert_held(&ctx->uring_lock);
9509b8c5475SDylan Yudaken 
9519b8c5475SDylan Yudaken 	if (ctx->submit_state.cqes_count == length) {
952f66f7342SPavel Begunkov 		__io_cq_lock(ctx);
9539b8c5475SDylan Yudaken 		__io_flush_post_cqes(ctx);
9549b8c5475SDylan Yudaken 		/* no need to flush - flush is deferred */
955f66f7342SPavel Begunkov 		__io_cq_unlock_post(ctx);
9569b8c5475SDylan Yudaken 	}
9579b8c5475SDylan Yudaken 
9589b8c5475SDylan Yudaken 	/* For defered completions this is not as strict as it is otherwise,
9599b8c5475SDylan Yudaken 	 * however it's main job is to prevent unbounded posted completions,
9609b8c5475SDylan Yudaken 	 * and in that it works just as well.
9619b8c5475SDylan Yudaken 	 */
9629b8c5475SDylan Yudaken 	if (!allow_overflow && test_bit(IO_CHECK_CQ_OVERFLOW_BIT, &ctx->check_cq))
9639b8c5475SDylan Yudaken 		return false;
9649b8c5475SDylan Yudaken 
9659b8c5475SDylan Yudaken 	cqe = &ctx->submit_state.cqes[ctx->submit_state.cqes_count++];
9669b8c5475SDylan Yudaken 	cqe->user_data = user_data;
9679b8c5475SDylan Yudaken 	cqe->res = res;
9689b8c5475SDylan Yudaken 	cqe->flags = cflags;
9699b8c5475SDylan Yudaken 	return true;
9709b8c5475SDylan Yudaken }
9719b8c5475SDylan Yudaken 
972ef8ae64fSPavel Begunkov static void __io_req_complete_post(struct io_kiocb *req, unsigned issue_flags)
973ed29b0b4SJens Axboe {
974fa18fa22SPavel Begunkov 	struct io_ring_ctx *ctx = req->ctx;
9752ad4c6d0SPavel Begunkov 	struct io_rsrc_node *rsrc_node = NULL;
976fa18fa22SPavel Begunkov 
977fa18fa22SPavel Begunkov 	io_cq_lock(ctx);
978fa18fa22SPavel Begunkov 	if (!(req->flags & REQ_F_CQE_SKIP))
979a8cf95f9SPavel Begunkov 		io_fill_cqe_req(ctx, req);
980fa18fa22SPavel Begunkov 
981ed29b0b4SJens Axboe 	/*
982ed29b0b4SJens Axboe 	 * If we're the last reference to this request, add to our locked
983ed29b0b4SJens Axboe 	 * free_list cache.
984ed29b0b4SJens Axboe 	 */
985ed29b0b4SJens Axboe 	if (req_ref_put_and_test(req)) {
986ed29b0b4SJens Axboe 		if (req->flags & IO_REQ_LINK_FLAGS) {
987ed29b0b4SJens Axboe 			if (req->flags & IO_DISARM_MASK)
988ed29b0b4SJens Axboe 				io_disarm_next(req);
989ed29b0b4SJens Axboe 			if (req->link) {
990ed29b0b4SJens Axboe 				io_req_task_queue(req->link);
991ed29b0b4SJens Axboe 				req->link = NULL;
992ed29b0b4SJens Axboe 			}
993ed29b0b4SJens Axboe 		}
99468a2cc1bSPavel Begunkov 		io_put_kbuf_comp(req);
99568a2cc1bSPavel Begunkov 		io_dismantle_req(req);
9962ad4c6d0SPavel Begunkov 		rsrc_node = req->rsrc_node;
997ed29b0b4SJens Axboe 		/*
998ed29b0b4SJens Axboe 		 * Selected buffer deallocation in io_clean_op() assumes that
999ed29b0b4SJens Axboe 		 * we don't hold ->completion_lock. Clean them here to avoid
1000ed29b0b4SJens Axboe 		 * deadlocks.
1001ed29b0b4SJens Axboe 		 */
10025afa4650SPavel Begunkov 		io_put_task_remote(req->task, 1);
1003ed29b0b4SJens Axboe 		wq_list_add_head(&req->comp_list, &ctx->locked_free_list);
1004ed29b0b4SJens Axboe 		ctx->locked_free_nr++;
1005ed29b0b4SJens Axboe 	}
100625399321SPavel Begunkov 	io_cq_unlock_post(ctx);
10072ad4c6d0SPavel Begunkov 
1008ef8ae64fSPavel Begunkov 	if (rsrc_node) {
1009ef8ae64fSPavel Begunkov 		io_ring_submit_lock(ctx, issue_flags);
10101f2c8f61SPavel Begunkov 		io_put_rsrc_node(ctx, rsrc_node);
1011ef8ae64fSPavel Begunkov 		io_ring_submit_unlock(ctx, issue_flags);
1012ef8ae64fSPavel Begunkov 	}
1013ed29b0b4SJens Axboe }
1014ed29b0b4SJens Axboe 
10151bec951cSPavel Begunkov void io_req_complete_post(struct io_kiocb *req, unsigned issue_flags)
1016ed29b0b4SJens Axboe {
1017860e1c7fSMing Lei 	if (req->ctx->task_complete && req->ctx->submitter_task != current) {
1018e6aeb272SPavel Begunkov 		req->io_task_work.func = io_req_task_complete;
1019e6aeb272SPavel Begunkov 		io_req_task_work_add(req);
1020e6aeb272SPavel Begunkov 	} else if (!(issue_flags & IO_URING_F_UNLOCKED) ||
10211bec951cSPavel Begunkov 		   !(req->ctx->flags & IORING_SETUP_IOPOLL)) {
1022ef8ae64fSPavel Begunkov 		__io_req_complete_post(req, issue_flags);
10231bec951cSPavel Begunkov 	} else {
10241bec951cSPavel Begunkov 		struct io_ring_ctx *ctx = req->ctx;
10251bec951cSPavel Begunkov 
10261bec951cSPavel Begunkov 		mutex_lock(&ctx->uring_lock);
1027ef8ae64fSPavel Begunkov 		__io_req_complete_post(req, issue_flags & ~IO_URING_F_UNLOCKED);
10281bec951cSPavel Begunkov 		mutex_unlock(&ctx->uring_lock);
10291bec951cSPavel Begunkov 	}
1030ed29b0b4SJens Axboe }
1031ed29b0b4SJens Axboe 
1032973fc83fSDylan Yudaken void io_req_defer_failed(struct io_kiocb *req, s32 res)
1033e276ae34SPavel Begunkov 	__must_hold(&ctx->uring_lock)
1034ed29b0b4SJens Axboe {
1035f30bd4d0SBreno Leitao 	const struct io_cold_def *def = &io_cold_defs[req->opcode];
1036a47b255eSPavel Begunkov 
1037e276ae34SPavel Begunkov 	lockdep_assert_held(&req->ctx->uring_lock);
1038e276ae34SPavel Begunkov 
1039ed29b0b4SJens Axboe 	req_set_fail(req);
104097b388d7SJens Axboe 	io_req_set_res(req, res, io_put_kbuf(req, IO_URING_F_UNLOCKED));
1041a47b255eSPavel Begunkov 	if (def->fail)
1042a47b255eSPavel Begunkov 		def->fail(req);
1043973fc83fSDylan Yudaken 	io_req_complete_defer(req);
1044ed29b0b4SJens Axboe }
1045ed29b0b4SJens Axboe 
1046ed29b0b4SJens Axboe /*
1047ed29b0b4SJens Axboe  * Don't initialise the fields below on every allocation, but do that in
1048ed29b0b4SJens Axboe  * advance and keep them valid across allocations.
1049ed29b0b4SJens Axboe  */
1050ed29b0b4SJens Axboe static void io_preinit_req(struct io_kiocb *req, struct io_ring_ctx *ctx)
1051ed29b0b4SJens Axboe {
1052ed29b0b4SJens Axboe 	req->ctx = ctx;
1053ed29b0b4SJens Axboe 	req->link = NULL;
1054ed29b0b4SJens Axboe 	req->async_data = NULL;
1055ed29b0b4SJens Axboe 	/* not necessary, but safer to zero */
1056ed29b0b4SJens Axboe 	req->cqe.res = 0;
1057ed29b0b4SJens Axboe }
1058ed29b0b4SJens Axboe 
1059ed29b0b4SJens Axboe static void io_flush_cached_locked_reqs(struct io_ring_ctx *ctx,
1060ed29b0b4SJens Axboe 					struct io_submit_state *state)
1061ed29b0b4SJens Axboe {
1062ed29b0b4SJens Axboe 	spin_lock(&ctx->completion_lock);
1063ed29b0b4SJens Axboe 	wq_list_splice(&ctx->locked_free_list, &state->free_list);
1064ed29b0b4SJens Axboe 	ctx->locked_free_nr = 0;
1065ed29b0b4SJens Axboe 	spin_unlock(&ctx->completion_lock);
1066ed29b0b4SJens Axboe }
1067ed29b0b4SJens Axboe 
1068ed29b0b4SJens Axboe /*
1069ed29b0b4SJens Axboe  * A request might get retired back into the request caches even before opcode
1070ed29b0b4SJens Axboe  * handlers and io_issue_sqe() are done with it, e.g. inline completion path.
1071ed29b0b4SJens Axboe  * Because of that, io_alloc_req() should be called only under ->uring_lock
1072ed29b0b4SJens Axboe  * and with extra caution to not get a request that is still worked on.
1073ed29b0b4SJens Axboe  */
1074bd1a3783SPavel Begunkov __cold bool __io_alloc_req_refill(struct io_ring_ctx *ctx)
1075ed29b0b4SJens Axboe 	__must_hold(&ctx->uring_lock)
1076ed29b0b4SJens Axboe {
1077ed29b0b4SJens Axboe 	gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
1078ed29b0b4SJens Axboe 	void *reqs[IO_REQ_ALLOC_BATCH];
1079ed29b0b4SJens Axboe 	int ret, i;
1080ed29b0b4SJens Axboe 
1081ed29b0b4SJens Axboe 	/*
1082ed29b0b4SJens Axboe 	 * If we have more than a batch's worth of requests in our IRQ side
1083ed29b0b4SJens Axboe 	 * locked cache, grab the lock and move them over to our submission
1084ed29b0b4SJens Axboe 	 * side cache.
1085ed29b0b4SJens Axboe 	 */
1086ed29b0b4SJens Axboe 	if (data_race(ctx->locked_free_nr) > IO_COMPL_BATCH) {
1087ed29b0b4SJens Axboe 		io_flush_cached_locked_reqs(ctx, &ctx->submit_state);
1088ed29b0b4SJens Axboe 		if (!io_req_cache_empty(ctx))
1089ed29b0b4SJens Axboe 			return true;
1090ed29b0b4SJens Axboe 	}
1091ed29b0b4SJens Axboe 
1092ed29b0b4SJens Axboe 	ret = kmem_cache_alloc_bulk(req_cachep, gfp, ARRAY_SIZE(reqs), reqs);
1093ed29b0b4SJens Axboe 
1094ed29b0b4SJens Axboe 	/*
1095ed29b0b4SJens Axboe 	 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1096ed29b0b4SJens Axboe 	 * retry single alloc to be on the safe side.
1097ed29b0b4SJens Axboe 	 */
1098ed29b0b4SJens Axboe 	if (unlikely(ret <= 0)) {
1099ed29b0b4SJens Axboe 		reqs[0] = kmem_cache_alloc(req_cachep, gfp);
1100ed29b0b4SJens Axboe 		if (!reqs[0])
1101ed29b0b4SJens Axboe 			return false;
1102ed29b0b4SJens Axboe 		ret = 1;
1103ed29b0b4SJens Axboe 	}
1104ed29b0b4SJens Axboe 
1105ed29b0b4SJens Axboe 	percpu_ref_get_many(&ctx->refs, ret);
1106ed29b0b4SJens Axboe 	for (i = 0; i < ret; i++) {
1107ed29b0b4SJens Axboe 		struct io_kiocb *req = reqs[i];
1108ed29b0b4SJens Axboe 
1109ed29b0b4SJens Axboe 		io_preinit_req(req, ctx);
1110ed29b0b4SJens Axboe 		io_req_add_to_cache(req, ctx);
1111ed29b0b4SJens Axboe 	}
1112ed29b0b4SJens Axboe 	return true;
1113ed29b0b4SJens Axboe }
1114ed29b0b4SJens Axboe 
1115ed29b0b4SJens Axboe static inline void io_dismantle_req(struct io_kiocb *req)
1116ed29b0b4SJens Axboe {
1117ed29b0b4SJens Axboe 	unsigned int flags = req->flags;
1118ed29b0b4SJens Axboe 
1119ed29b0b4SJens Axboe 	if (unlikely(flags & IO_REQ_CLEAN_FLAGS))
1120ed29b0b4SJens Axboe 		io_clean_op(req);
1121ed29b0b4SJens Axboe 	if (!(flags & REQ_F_FIXED_FILE))
1122ed29b0b4SJens Axboe 		io_put_file(req->file);
1123ed29b0b4SJens Axboe }
1124ed29b0b4SJens Axboe 
112503adabe8SPavel Begunkov static __cold void io_free_req_tw(struct io_kiocb *req, struct io_tw_state *ts)
1126ed29b0b4SJens Axboe {
1127ed29b0b4SJens Axboe 	struct io_ring_ctx *ctx = req->ctx;
1128ed29b0b4SJens Axboe 
1129ef8ae64fSPavel Begunkov 	if (req->rsrc_node) {
1130ef8ae64fSPavel Begunkov 		io_tw_lock(ctx, ts);
11311f2c8f61SPavel Begunkov 		io_put_rsrc_node(ctx, req->rsrc_node);
1132ef8ae64fSPavel Begunkov 	}
1133ed29b0b4SJens Axboe 	io_dismantle_req(req);
11345afa4650SPavel Begunkov 	io_put_task_remote(req->task, 1);
1135ed29b0b4SJens Axboe 
1136ed29b0b4SJens Axboe 	spin_lock(&ctx->completion_lock);
1137ed29b0b4SJens Axboe 	wq_list_add_head(&req->comp_list, &ctx->locked_free_list);
1138ed29b0b4SJens Axboe 	ctx->locked_free_nr++;
1139ed29b0b4SJens Axboe 	spin_unlock(&ctx->completion_lock);
1140ed29b0b4SJens Axboe }
1141ed29b0b4SJens Axboe 
114203adabe8SPavel Begunkov __cold void io_free_req(struct io_kiocb *req)
114303adabe8SPavel Begunkov {
114403adabe8SPavel Begunkov 	req->io_task_work.func = io_free_req_tw;
114503adabe8SPavel Begunkov 	io_req_task_work_add(req);
114603adabe8SPavel Begunkov }
114703adabe8SPavel Begunkov 
1148ed29b0b4SJens Axboe static void __io_req_find_next_prep(struct io_kiocb *req)
1149ed29b0b4SJens Axboe {
1150ed29b0b4SJens Axboe 	struct io_ring_ctx *ctx = req->ctx;
1151ed29b0b4SJens Axboe 
11526971253fSPavel Begunkov 	spin_lock(&ctx->completion_lock);
1153305bef98SPavel Begunkov 	io_disarm_next(req);
11546971253fSPavel Begunkov 	spin_unlock(&ctx->completion_lock);
1155ed29b0b4SJens Axboe }
1156ed29b0b4SJens Axboe 
1157ed29b0b4SJens Axboe static inline struct io_kiocb *io_req_find_next(struct io_kiocb *req)
1158ed29b0b4SJens Axboe {
1159ed29b0b4SJens Axboe 	struct io_kiocb *nxt;
1160ed29b0b4SJens Axboe 
1161ed29b0b4SJens Axboe 	/*
1162ed29b0b4SJens Axboe 	 * If LINK is set, we have dependent requests in this chain. If we
1163ed29b0b4SJens Axboe 	 * didn't fail this request, queue the first one up, moving any other
1164ed29b0b4SJens Axboe 	 * dependencies to the next request. In case of failure, fail the rest
1165ed29b0b4SJens Axboe 	 * of the chain.
1166ed29b0b4SJens Axboe 	 */
1167ed29b0b4SJens Axboe 	if (unlikely(req->flags & IO_DISARM_MASK))
1168ed29b0b4SJens Axboe 		__io_req_find_next_prep(req);
1169ed29b0b4SJens Axboe 	nxt = req->link;
1170ed29b0b4SJens Axboe 	req->link = NULL;
1171ed29b0b4SJens Axboe 	return nxt;
1172ed29b0b4SJens Axboe }
1173ed29b0b4SJens Axboe 
1174a282967cSPavel Begunkov static void ctx_flush_and_put(struct io_ring_ctx *ctx, struct io_tw_state *ts)
1175ed29b0b4SJens Axboe {
1176ed29b0b4SJens Axboe 	if (!ctx)
1177ed29b0b4SJens Axboe 		return;
1178ed29b0b4SJens Axboe 	if (ctx->flags & IORING_SETUP_TASKRUN_FLAG)
1179ed29b0b4SJens Axboe 		atomic_andnot(IORING_SQ_TASKRUN, &ctx->rings->sq_flags);
1180a282967cSPavel Begunkov 	if (ts->locked) {
1181ed29b0b4SJens Axboe 		io_submit_flush_completions(ctx);
1182ed29b0b4SJens Axboe 		mutex_unlock(&ctx->uring_lock);
1183a282967cSPavel Begunkov 		ts->locked = false;
1184ed29b0b4SJens Axboe 	}
1185ed29b0b4SJens Axboe 	percpu_ref_put(&ctx->refs);
1186ed29b0b4SJens Axboe }
1187ed29b0b4SJens Axboe 
1188c6dd763cSDylan Yudaken static unsigned int handle_tw_list(struct llist_node *node,
1189a282967cSPavel Begunkov 				   struct io_ring_ctx **ctx,
1190a282967cSPavel Begunkov 				   struct io_tw_state *ts,
11913a0c037bSDylan Yudaken 				   struct llist_node *last)
1192ed29b0b4SJens Axboe {
1193c6dd763cSDylan Yudaken 	unsigned int count = 0;
1194c6dd763cSDylan Yudaken 
1195cb6bf7f2SPavel Begunkov 	while (node && node != last) {
1196f88262e6SDylan Yudaken 		struct llist_node *next = node->next;
1197ed29b0b4SJens Axboe 		struct io_kiocb *req = container_of(node, struct io_kiocb,
1198ed29b0b4SJens Axboe 						    io_task_work.node);
1199ed29b0b4SJens Axboe 
1200ed29b0b4SJens Axboe 		prefetch(container_of(next, struct io_kiocb, io_task_work.node));
1201ed29b0b4SJens Axboe 
1202ed29b0b4SJens Axboe 		if (req->ctx != *ctx) {
1203a282967cSPavel Begunkov 			ctx_flush_and_put(*ctx, ts);
1204ed29b0b4SJens Axboe 			*ctx = req->ctx;
1205ed29b0b4SJens Axboe 			/* if not contended, grab and improve batching */
1206a282967cSPavel Begunkov 			ts->locked = mutex_trylock(&(*ctx)->uring_lock);
1207ed29b0b4SJens Axboe 			percpu_ref_get(&(*ctx)->refs);
120813bfa6f1SPavel Begunkov 		}
1209*c92fcfc2SJens Axboe 		INDIRECT_CALL_2(req->io_task_work.func,
1210*c92fcfc2SJens Axboe 				io_poll_task_func, io_req_rw_complete,
1211*c92fcfc2SJens Axboe 				req, ts);
1212ed29b0b4SJens Axboe 		node = next;
1213c6dd763cSDylan Yudaken 		count++;
1214f5868008SJens Axboe 		if (unlikely(need_resched())) {
1215a282967cSPavel Begunkov 			ctx_flush_and_put(*ctx, ts);
1216f5868008SJens Axboe 			*ctx = NULL;
1217f5868008SJens Axboe 			cond_resched();
1218f5868008SJens Axboe 		}
12193a0c037bSDylan Yudaken 	}
1220c6dd763cSDylan Yudaken 
1221c6dd763cSDylan Yudaken 	return count;
1222ed29b0b4SJens Axboe }
1223ed29b0b4SJens Axboe 
1224923d1592SDylan Yudaken /**
1225923d1592SDylan Yudaken  * io_llist_xchg - swap all entries in a lock-less list
1226923d1592SDylan Yudaken  * @head:	the head of lock-less list to delete all entries
1227923d1592SDylan Yudaken  * @new:	new entry as the head of the list
1228923d1592SDylan Yudaken  *
1229923d1592SDylan Yudaken  * If list is empty, return NULL, otherwise, return the pointer to the first entry.
1230923d1592SDylan Yudaken  * The order of entries returned is from the newest to the oldest added one.
1231923d1592SDylan Yudaken  */
1232923d1592SDylan Yudaken static inline struct llist_node *io_llist_xchg(struct llist_head *head,
1233923d1592SDylan Yudaken 					       struct llist_node *new)
1234923d1592SDylan Yudaken {
1235923d1592SDylan Yudaken 	return xchg(&head->first, new);
1236923d1592SDylan Yudaken }
1237923d1592SDylan Yudaken 
1238923d1592SDylan Yudaken /**
1239923d1592SDylan Yudaken  * io_llist_cmpxchg - possibly swap all entries in a lock-less list
1240923d1592SDylan Yudaken  * @head:	the head of lock-less list to delete all entries
1241923d1592SDylan Yudaken  * @old:	expected old value of the first entry of the list
1242923d1592SDylan Yudaken  * @new:	new entry as the head of the list
1243923d1592SDylan Yudaken  *
1244923d1592SDylan Yudaken  * perform a cmpxchg on the first entry of the list.
1245923d1592SDylan Yudaken  */
1246923d1592SDylan Yudaken 
1247923d1592SDylan Yudaken static inline struct llist_node *io_llist_cmpxchg(struct llist_head *head,
1248923d1592SDylan Yudaken 						  struct llist_node *old,
1249923d1592SDylan Yudaken 						  struct llist_node *new)
1250923d1592SDylan Yudaken {
1251923d1592SDylan Yudaken 	return cmpxchg(&head->first, old, new);
1252923d1592SDylan Yudaken }
1253923d1592SDylan Yudaken 
1254c9f06aa7SJens Axboe void tctx_task_work(struct callback_head *cb)
1255ed29b0b4SJens Axboe {
1256a282967cSPavel Begunkov 	struct io_tw_state ts = {};
1257ed29b0b4SJens Axboe 	struct io_ring_ctx *ctx = NULL;
1258ed29b0b4SJens Axboe 	struct io_uring_task *tctx = container_of(cb, struct io_uring_task,
1259ed29b0b4SJens Axboe 						  task_work);
12603a0c037bSDylan Yudaken 	struct llist_node fake = {};
126177e443abSPavel Begunkov 	struct llist_node *node;
1262cb6bf7f2SPavel Begunkov 	unsigned int loops = 0;
1263cb6bf7f2SPavel Begunkov 	unsigned int count = 0;
1264ed29b0b4SJens Axboe 
126577e443abSPavel Begunkov 	if (unlikely(current->flags & PF_EXITING)) {
126677e443abSPavel Begunkov 		io_fallback_tw(tctx);
126777e443abSPavel Begunkov 		return;
126877e443abSPavel Begunkov 	}
126977e443abSPavel Begunkov 
1270cb6bf7f2SPavel Begunkov 	do {
1271c6dd763cSDylan Yudaken 		loops++;
12723a0c037bSDylan Yudaken 		node = io_llist_xchg(&tctx->task_list, &fake);
1273a282967cSPavel Begunkov 		count += handle_tw_list(node, &ctx, &ts, &fake);
127450470fc5SPavel Begunkov 
127550470fc5SPavel Begunkov 		/* skip expensive cmpxchg if there are items in the list */
127650470fc5SPavel Begunkov 		if (READ_ONCE(tctx->task_list.first) != &fake)
127750470fc5SPavel Begunkov 			continue;
1278a282967cSPavel Begunkov 		if (ts.locked && !wq_list_empty(&ctx->submit_state.compl_reqs)) {
127950470fc5SPavel Begunkov 			io_submit_flush_completions(ctx);
128050470fc5SPavel Begunkov 			if (READ_ONCE(tctx->task_list.first) != &fake)
128150470fc5SPavel Begunkov 				continue;
128250470fc5SPavel Begunkov 		}
12833a0c037bSDylan Yudaken 		node = io_llist_cmpxchg(&tctx->task_list, &fake, NULL);
1284cb6bf7f2SPavel Begunkov 	} while (node != &fake);
1285ed29b0b4SJens Axboe 
1286a282967cSPavel Begunkov 	ctx_flush_and_put(ctx, &ts);
1287ed29b0b4SJens Axboe 
12888d664282SJens Axboe 	/* relaxed read is enough as only the task itself sets ->in_cancel */
12898d664282SJens Axboe 	if (unlikely(atomic_read(&tctx->in_cancel)))
1290ed29b0b4SJens Axboe 		io_uring_drop_tctx_refs(current);
1291c6dd763cSDylan Yudaken 
1292c6dd763cSDylan Yudaken 	trace_io_uring_task_work_run(tctx, count, loops);
1293ed29b0b4SJens Axboe }
1294ed29b0b4SJens Axboe 
1295d7593606SPavel Begunkov static __cold void io_fallback_tw(struct io_uring_task *tctx)
1296d7593606SPavel Begunkov {
1297d7593606SPavel Begunkov 	struct llist_node *node = llist_del_all(&tctx->task_list);
1298d7593606SPavel Begunkov 	struct io_kiocb *req;
1299d7593606SPavel Begunkov 
1300d7593606SPavel Begunkov 	while (node) {
1301d7593606SPavel Begunkov 		req = container_of(node, struct io_kiocb, io_task_work.node);
1302d7593606SPavel Begunkov 		node = node->next;
1303d7593606SPavel Begunkov 		if (llist_add(&req->io_task_work.node,
1304d7593606SPavel Begunkov 			      &req->ctx->fallback_llist))
1305d7593606SPavel Begunkov 			schedule_delayed_work(&req->ctx->fallback_work, 1);
1306d7593606SPavel Begunkov 	}
1307d7593606SPavel Begunkov }
1308d7593606SPavel Begunkov 
13098751d154SPavel Begunkov static void io_req_local_work_add(struct io_kiocb *req, unsigned flags)
1310c0e0d6baSDylan Yudaken {
1311c0e0d6baSDylan Yudaken 	struct io_ring_ctx *ctx = req->ctx;
13128751d154SPavel Begunkov 	unsigned nr_wait, nr_tw, nr_tw_prev;
131351509400SPavel Begunkov 	struct llist_node *first;
1314c0e0d6baSDylan Yudaken 
13158751d154SPavel Begunkov 	if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK))
13168751d154SPavel Begunkov 		flags &= ~IOU_F_TWQ_LAZY_WAKE;
13179ffa13ffSPavel Begunkov 
131851509400SPavel Begunkov 	first = READ_ONCE(ctx->work_llist.first);
131951509400SPavel Begunkov 	do {
13208751d154SPavel Begunkov 		nr_tw_prev = 0;
13218751d154SPavel Begunkov 		if (first) {
13228751d154SPavel Begunkov 			struct io_kiocb *first_req = container_of(first,
13238751d154SPavel Begunkov 							struct io_kiocb,
13248751d154SPavel Begunkov 							io_task_work.node);
13258751d154SPavel Begunkov 			/*
13268751d154SPavel Begunkov 			 * Might be executed at any moment, rely on
13278751d154SPavel Begunkov 			 * SLAB_TYPESAFE_BY_RCU to keep it alive.
13288751d154SPavel Begunkov 			 */
13298751d154SPavel Begunkov 			nr_tw_prev = READ_ONCE(first_req->nr_tw);
1330c0e0d6baSDylan Yudaken 		}
13318751d154SPavel Begunkov 		nr_tw = nr_tw_prev + 1;
13328751d154SPavel Begunkov 		/* Large enough to fail the nr_wait comparison below */
13338751d154SPavel Begunkov 		if (!(flags & IOU_F_TWQ_LAZY_WAKE))
13348751d154SPavel Begunkov 			nr_tw = -1U;
1335c0e0d6baSDylan Yudaken 
13368751d154SPavel Begunkov 		req->nr_tw = nr_tw;
133751509400SPavel Begunkov 		req->io_task_work.node.next = first;
133851509400SPavel Begunkov 	} while (!try_cmpxchg(&ctx->work_llist.first, &first,
133951509400SPavel Begunkov 			      &req->io_task_work.node));
134051509400SPavel Begunkov 
13418751d154SPavel Begunkov 	if (!first) {
1342c0e0d6baSDylan Yudaken 		if (ctx->flags & IORING_SETUP_TASKRUN_FLAG)
1343c0e0d6baSDylan Yudaken 			atomic_or(IORING_SQ_TASKRUN, &ctx->rings->sq_flags);
134421a091b9SDylan Yudaken 		if (ctx->has_evfd)
134521a091b9SDylan Yudaken 			io_eventfd_signal(ctx);
1346c0e0d6baSDylan Yudaken 	}
1347c0e0d6baSDylan Yudaken 
13488751d154SPavel Begunkov 	nr_wait = atomic_read(&ctx->cq_wait_nr);
13498751d154SPavel Begunkov 	/* no one is waiting */
13508751d154SPavel Begunkov 	if (!nr_wait)
13518751d154SPavel Begunkov 		return;
13528751d154SPavel Begunkov 	/* either not enough or the previous add has already woken it up */
13538751d154SPavel Begunkov 	if (nr_wait > nr_tw || nr_tw_prev >= nr_wait)
13548751d154SPavel Begunkov 		return;
13558751d154SPavel Begunkov 	/* pairs with set_current_state() in io_cqring_wait() */
13568751d154SPavel Begunkov 	smp_mb__after_atomic();
1357e52d2e58SPavel Begunkov 	wake_up_state(ctx->submitter_task, TASK_INTERRUPTIBLE);
1358c0e0d6baSDylan Yudaken }
1359c0e0d6baSDylan Yudaken 
13608501fe70SPavel Begunkov void __io_req_task_work_add(struct io_kiocb *req, unsigned flags)
1361ed29b0b4SJens Axboe {
1362c34398a8SDylan Yudaken 	struct io_uring_task *tctx = req->task->io_uring;
1363ed29b0b4SJens Axboe 	struct io_ring_ctx *ctx = req->ctx;
1364ed29b0b4SJens Axboe 
13658501fe70SPavel Begunkov 	if (!(flags & IOU_F_TWQ_FORCE_NORMAL) &&
13668501fe70SPavel Begunkov 	    (ctx->flags & IORING_SETUP_DEFER_TASKRUN)) {
1367d73a572dSPavel Begunkov 		rcu_read_lock();
13688751d154SPavel Begunkov 		io_req_local_work_add(req, flags);
1369d73a572dSPavel Begunkov 		rcu_read_unlock();
1370c0e0d6baSDylan Yudaken 		return;
1371c0e0d6baSDylan Yudaken 	}
1372ed29b0b4SJens Axboe 
1373ed29b0b4SJens Axboe 	/* task_work already pending, we're done */
137432d91f05SDylan Yudaken 	if (!llist_add(&req->io_task_work.node, &tctx->task_list))
1375ed29b0b4SJens Axboe 		return;
1376ed29b0b4SJens Axboe 
1377ed29b0b4SJens Axboe 	if (ctx->flags & IORING_SETUP_TASKRUN_FLAG)
1378ed29b0b4SJens Axboe 		atomic_or(IORING_SQ_TASKRUN, &ctx->rings->sq_flags);
1379ed29b0b4SJens Axboe 
1380ed29b0b4SJens Axboe 	if (likely(!task_work_add(req->task, &tctx->task_work, ctx->notify_method)))
1381ed29b0b4SJens Axboe 		return;
1382ed29b0b4SJens Axboe 
1383d7593606SPavel Begunkov 	io_fallback_tw(tctx);
1384c0e0d6baSDylan Yudaken }
1385c0e0d6baSDylan Yudaken 
1386c0e0d6baSDylan Yudaken static void __cold io_move_task_work_from_local(struct io_ring_ctx *ctx)
1387c0e0d6baSDylan Yudaken {
1388c0e0d6baSDylan Yudaken 	struct llist_node *node;
1389c0e0d6baSDylan Yudaken 
1390c0e0d6baSDylan Yudaken 	node = llist_del_all(&ctx->work_llist);
1391c0e0d6baSDylan Yudaken 	while (node) {
1392c0e0d6baSDylan Yudaken 		struct io_kiocb *req = container_of(node, struct io_kiocb,
1393c0e0d6baSDylan Yudaken 						    io_task_work.node);
1394c0e0d6baSDylan Yudaken 
1395c0e0d6baSDylan Yudaken 		node = node->next;
13968501fe70SPavel Begunkov 		__io_req_task_work_add(req, IOU_F_TWQ_FORCE_NORMAL);
1397c0e0d6baSDylan Yudaken 	}
1398c0e0d6baSDylan Yudaken }
1399c0e0d6baSDylan Yudaken 
1400a282967cSPavel Begunkov static int __io_run_local_work(struct io_ring_ctx *ctx, struct io_tw_state *ts)
1401c0e0d6baSDylan Yudaken {
1402c0e0d6baSDylan Yudaken 	struct llist_node *node;
1403c3f4d39eSPavel Begunkov 	unsigned int loops = 0;
1404140102aeSPavel Begunkov 	int ret = 0;
1405c0e0d6baSDylan Yudaken 
1406140102aeSPavel Begunkov 	if (WARN_ON_ONCE(ctx->submitter_task != current))
1407c0e0d6baSDylan Yudaken 		return -EEXIST;
1408c3f4d39eSPavel Begunkov 	if (ctx->flags & IORING_SETUP_TASKRUN_FLAG)
1409c3f4d39eSPavel Begunkov 		atomic_andnot(IORING_SQ_TASKRUN, &ctx->rings->sq_flags);
1410c0e0d6baSDylan Yudaken again:
14113af0356cSJens Axboe 	/*
14123af0356cSJens Axboe 	 * llists are in reverse order, flip it back the right way before
14133af0356cSJens Axboe 	 * running the pending items.
14143af0356cSJens Axboe 	 */
14153af0356cSJens Axboe 	node = llist_reverse_order(io_llist_xchg(&ctx->work_llist, NULL));
1416c3f4d39eSPavel Begunkov 	while (node) {
1417c0e0d6baSDylan Yudaken 		struct llist_node *next = node->next;
1418c0e0d6baSDylan Yudaken 		struct io_kiocb *req = container_of(node, struct io_kiocb,
1419c0e0d6baSDylan Yudaken 						    io_task_work.node);
1420c0e0d6baSDylan Yudaken 		prefetch(container_of(next, struct io_kiocb, io_task_work.node));
1421*c92fcfc2SJens Axboe 		INDIRECT_CALL_2(req->io_task_work.func,
1422*c92fcfc2SJens Axboe 				io_poll_task_func, io_req_rw_complete,
1423*c92fcfc2SJens Axboe 				req, ts);
1424c0e0d6baSDylan Yudaken 		ret++;
1425c0e0d6baSDylan Yudaken 		node = next;
1426c0e0d6baSDylan Yudaken 	}
1427f75d5036SDylan Yudaken 	loops++;
1428c0e0d6baSDylan Yudaken 
1429c3f4d39eSPavel Begunkov 	if (!llist_empty(&ctx->work_llist))
1430c3f4d39eSPavel Begunkov 		goto again;
1431a282967cSPavel Begunkov 	if (ts->locked) {
1432c0e0d6baSDylan Yudaken 		io_submit_flush_completions(ctx);
1433b0b7a7d2SPavel Begunkov 		if (!llist_empty(&ctx->work_llist))
1434b0b7a7d2SPavel Begunkov 			goto again;
1435b0b7a7d2SPavel Begunkov 	}
1436f75d5036SDylan Yudaken 	trace_io_uring_local_work_run(ctx, ret, loops);
1437c0e0d6baSDylan Yudaken 	return ret;
14388ac5d85aSJens Axboe }
14398ac5d85aSJens Axboe 
1440360173abSPavel Begunkov static inline int io_run_local_work_locked(struct io_ring_ctx *ctx)
1441360173abSPavel Begunkov {
1442a282967cSPavel Begunkov 	struct io_tw_state ts = { .locked = true, };
1443360173abSPavel Begunkov 	int ret;
1444360173abSPavel Begunkov 
1445360173abSPavel Begunkov 	if (llist_empty(&ctx->work_llist))
1446360173abSPavel Begunkov 		return 0;
1447360173abSPavel Begunkov 
1448a282967cSPavel Begunkov 	ret = __io_run_local_work(ctx, &ts);
1449360173abSPavel Begunkov 	/* shouldn't happen! */
1450a282967cSPavel Begunkov 	if (WARN_ON_ONCE(!ts.locked))
1451360173abSPavel Begunkov 		mutex_lock(&ctx->uring_lock);
1452360173abSPavel Begunkov 	return ret;
1453360173abSPavel Begunkov }
1454360173abSPavel Begunkov 
14553e565555SPavel Begunkov static int io_run_local_work(struct io_ring_ctx *ctx)
14568ac5d85aSJens Axboe {
1457a282967cSPavel Begunkov 	struct io_tw_state ts = {};
14588ac5d85aSJens Axboe 	int ret;
14598ac5d85aSJens Axboe 
1460a282967cSPavel Begunkov 	ts.locked = mutex_trylock(&ctx->uring_lock);
1461a282967cSPavel Begunkov 	ret = __io_run_local_work(ctx, &ts);
1462a282967cSPavel Begunkov 	if (ts.locked)
14638ac5d85aSJens Axboe 		mutex_unlock(&ctx->uring_lock);
14648ac5d85aSJens Axboe 
14658ac5d85aSJens Axboe 	return ret;
1466c0e0d6baSDylan Yudaken }
1467c0e0d6baSDylan Yudaken 
1468a282967cSPavel Begunkov static void io_req_task_cancel(struct io_kiocb *req, struct io_tw_state *ts)
1469ed29b0b4SJens Axboe {
1470a282967cSPavel Begunkov 	io_tw_lock(req->ctx, ts);
1471973fc83fSDylan Yudaken 	io_req_defer_failed(req, req->cqe.res);
1472ed29b0b4SJens Axboe }
1473ed29b0b4SJens Axboe 
1474a282967cSPavel Begunkov void io_req_task_submit(struct io_kiocb *req, struct io_tw_state *ts)
1475ed29b0b4SJens Axboe {
1476a282967cSPavel Begunkov 	io_tw_lock(req->ctx, ts);
1477ed29b0b4SJens Axboe 	/* req->task == current here, checking PF_EXITING is safe */
14786bb30855SDylan Yudaken 	if (unlikely(req->task->flags & PF_EXITING))
1479973fc83fSDylan Yudaken 		io_req_defer_failed(req, -EFAULT);
14806bb30855SDylan Yudaken 	else if (req->flags & REQ_F_FORCE_ASYNC)
1481a282967cSPavel Begunkov 		io_queue_iowq(req, ts);
14826bb30855SDylan Yudaken 	else
14836bb30855SDylan Yudaken 		io_queue_sqe(req);
1484ed29b0b4SJens Axboe }
1485ed29b0b4SJens Axboe 
148659915143SJens Axboe void io_req_task_queue_fail(struct io_kiocb *req, int ret)
1487ed29b0b4SJens Axboe {
148897b388d7SJens Axboe 	io_req_set_res(req, ret, 0);
1489ed29b0b4SJens Axboe 	req->io_task_work.func = io_req_task_cancel;
1490ed29b0b4SJens Axboe 	io_req_task_work_add(req);
1491ed29b0b4SJens Axboe }
1492ed29b0b4SJens Axboe 
1493f3b44f92SJens Axboe void io_req_task_queue(struct io_kiocb *req)
1494ed29b0b4SJens Axboe {
1495ed29b0b4SJens Axboe 	req->io_task_work.func = io_req_task_submit;
1496ed29b0b4SJens Axboe 	io_req_task_work_add(req);
1497ed29b0b4SJens Axboe }
1498ed29b0b4SJens Axboe 
149959915143SJens Axboe void io_queue_next(struct io_kiocb *req)
1500ed29b0b4SJens Axboe {
1501ed29b0b4SJens Axboe 	struct io_kiocb *nxt = io_req_find_next(req);
1502ed29b0b4SJens Axboe 
1503ed29b0b4SJens Axboe 	if (nxt)
1504ed29b0b4SJens Axboe 		io_req_task_queue(nxt);
1505ed29b0b4SJens Axboe }
1506ed29b0b4SJens Axboe 
1507f3b44f92SJens Axboe void io_free_batch_list(struct io_ring_ctx *ctx, struct io_wq_work_node *node)
1508ed29b0b4SJens Axboe 	__must_hold(&ctx->uring_lock)
1509ed29b0b4SJens Axboe {
1510ed29b0b4SJens Axboe 	struct task_struct *task = NULL;
1511ed29b0b4SJens Axboe 	int task_refs = 0;
1512ed29b0b4SJens Axboe 
1513ed29b0b4SJens Axboe 	do {
1514ed29b0b4SJens Axboe 		struct io_kiocb *req = container_of(node, struct io_kiocb,
1515ed29b0b4SJens Axboe 						    comp_list);
1516ed29b0b4SJens Axboe 
1517ed29b0b4SJens Axboe 		if (unlikely(req->flags & IO_REQ_CLEAN_SLOW_FLAGS)) {
1518ed29b0b4SJens Axboe 			if (req->flags & REQ_F_REFCOUNT) {
1519ed29b0b4SJens Axboe 				node = req->comp_list.next;
1520ed29b0b4SJens Axboe 				if (!req_ref_put_and_test(req))
1521ed29b0b4SJens Axboe 					continue;
1522ed29b0b4SJens Axboe 			}
1523ed29b0b4SJens Axboe 			if ((req->flags & REQ_F_POLLED) && req->apoll) {
1524ed29b0b4SJens Axboe 				struct async_poll *apoll = req->apoll;
1525ed29b0b4SJens Axboe 
1526ed29b0b4SJens Axboe 				if (apoll->double_poll)
1527ed29b0b4SJens Axboe 					kfree(apoll->double_poll);
15289731bc98SJens Axboe 				if (!io_alloc_cache_put(&ctx->apoll_cache, &apoll->cache))
15299731bc98SJens Axboe 					kfree(apoll);
1530ed29b0b4SJens Axboe 				req->flags &= ~REQ_F_POLLED;
1531ed29b0b4SJens Axboe 			}
1532ed29b0b4SJens Axboe 			if (req->flags & IO_REQ_LINK_FLAGS)
1533ed29b0b4SJens Axboe 				io_queue_next(req);
1534ed29b0b4SJens Axboe 			if (unlikely(req->flags & IO_REQ_CLEAN_FLAGS))
1535ed29b0b4SJens Axboe 				io_clean_op(req);
1536ed29b0b4SJens Axboe 		}
1537ed29b0b4SJens Axboe 		if (!(req->flags & REQ_F_FIXED_FILE))
1538ed29b0b4SJens Axboe 			io_put_file(req->file);
1539ed29b0b4SJens Axboe 
1540ed29b0b4SJens Axboe 		io_req_put_rsrc_locked(req, ctx);
1541ed29b0b4SJens Axboe 
1542ed29b0b4SJens Axboe 		if (req->task != task) {
1543ed29b0b4SJens Axboe 			if (task)
1544ed29b0b4SJens Axboe 				io_put_task(task, task_refs);
1545ed29b0b4SJens Axboe 			task = req->task;
1546ed29b0b4SJens Axboe 			task_refs = 0;
1547ed29b0b4SJens Axboe 		}
1548ed29b0b4SJens Axboe 		task_refs++;
1549ed29b0b4SJens Axboe 		node = req->comp_list.next;
1550ed29b0b4SJens Axboe 		io_req_add_to_cache(req, ctx);
1551ed29b0b4SJens Axboe 	} while (node);
1552ed29b0b4SJens Axboe 
1553ed29b0b4SJens Axboe 	if (task)
1554ed29b0b4SJens Axboe 		io_put_task(task, task_refs);
1555ed29b0b4SJens Axboe }
1556ed29b0b4SJens Axboe 
1557ed29b0b4SJens Axboe static void __io_submit_flush_completions(struct io_ring_ctx *ctx)
1558ed29b0b4SJens Axboe 	__must_hold(&ctx->uring_lock)
1559ed29b0b4SJens Axboe {
1560ed29b0b4SJens Axboe 	struct io_submit_state *state = &ctx->submit_state;
1561fa780334SJens Axboe 	struct io_wq_work_node *node;
1562ed29b0b4SJens Axboe 
1563f66f7342SPavel Begunkov 	__io_cq_lock(ctx);
1564931147ddSDylan Yudaken 	/* must come first to preserve CQE ordering in failure cases */
1565931147ddSDylan Yudaken 	if (state->cqes_count)
1566931147ddSDylan Yudaken 		__io_flush_post_cqes(ctx);
1567fa780334SJens Axboe 	__wq_list_for_each(node, &state->compl_reqs) {
1568ed29b0b4SJens Axboe 		struct io_kiocb *req = container_of(node, struct io_kiocb,
1569ed29b0b4SJens Axboe 					    comp_list);
1570ed29b0b4SJens Axboe 
1571f66f7342SPavel Begunkov 		if (!(req->flags & REQ_F_CQE_SKIP) &&
1572f66f7342SPavel Begunkov 		    unlikely(!__io_fill_cqe_req(ctx, req))) {
1573f66f7342SPavel Begunkov 			if (ctx->task_complete) {
1574f66f7342SPavel Begunkov 				spin_lock(&ctx->completion_lock);
1575f66f7342SPavel Begunkov 				io_req_cqe_overflow(req);
1576f66f7342SPavel Begunkov 				spin_unlock(&ctx->completion_lock);
1577f66f7342SPavel Begunkov 			} else {
1578f66f7342SPavel Begunkov 				io_req_cqe_overflow(req);
1579ed29b0b4SJens Axboe 			}
1580f66f7342SPavel Begunkov 		}
1581f66f7342SPavel Begunkov 	}
15823181e22fSPavel Begunkov 	__io_cq_unlock_post_flush(ctx);
1583ed29b0b4SJens Axboe 
1584931147ddSDylan Yudaken 	if (!wq_list_empty(&ctx->submit_state.compl_reqs)) {
1585ed29b0b4SJens Axboe 		io_free_batch_list(ctx, state->compl_reqs.first);
1586ed29b0b4SJens Axboe 		INIT_WQ_LIST(&state->compl_reqs);
1587ed29b0b4SJens Axboe 	}
1588931147ddSDylan Yudaken }
1589ed29b0b4SJens Axboe 
1590ed29b0b4SJens Axboe /*
1591ed29b0b4SJens Axboe  * Drop reference to request, return next in chain (if there is one) if this
1592ed29b0b4SJens Axboe  * was the last reference to this request.
1593ed29b0b4SJens Axboe  */
1594ed29b0b4SJens Axboe static inline struct io_kiocb *io_put_req_find_next(struct io_kiocb *req)
1595ed29b0b4SJens Axboe {
1596ed29b0b4SJens Axboe 	struct io_kiocb *nxt = NULL;
1597ed29b0b4SJens Axboe 
1598ed29b0b4SJens Axboe 	if (req_ref_put_and_test(req)) {
1599ed29b0b4SJens Axboe 		if (unlikely(req->flags & IO_REQ_LINK_FLAGS))
1600ed29b0b4SJens Axboe 			nxt = io_req_find_next(req);
1601ed29b0b4SJens Axboe 		io_free_req(req);
1602ed29b0b4SJens Axboe 	}
1603ed29b0b4SJens Axboe 	return nxt;
1604ed29b0b4SJens Axboe }
1605ed29b0b4SJens Axboe 
1606ed29b0b4SJens Axboe static unsigned io_cqring_events(struct io_ring_ctx *ctx)
1607ed29b0b4SJens Axboe {
1608ed29b0b4SJens Axboe 	/* See comment at the top of this file */
1609ed29b0b4SJens Axboe 	smp_rmb();
1610ed29b0b4SJens Axboe 	return __io_cqring_events(ctx);
1611ed29b0b4SJens Axboe }
1612ed29b0b4SJens Axboe 
1613ed29b0b4SJens Axboe /*
1614ed29b0b4SJens Axboe  * We can't just wait for polled events to come to us, we have to actively
1615ed29b0b4SJens Axboe  * find and complete them.
1616ed29b0b4SJens Axboe  */
1617ed29b0b4SJens Axboe static __cold void io_iopoll_try_reap_events(struct io_ring_ctx *ctx)
1618ed29b0b4SJens Axboe {
1619ed29b0b4SJens Axboe 	if (!(ctx->flags & IORING_SETUP_IOPOLL))
1620ed29b0b4SJens Axboe 		return;
1621ed29b0b4SJens Axboe 
1622ed29b0b4SJens Axboe 	mutex_lock(&ctx->uring_lock);
1623ed29b0b4SJens Axboe 	while (!wq_list_empty(&ctx->iopoll_list)) {
1624ed29b0b4SJens Axboe 		/* let it sleep and repeat later if can't complete a request */
1625ed29b0b4SJens Axboe 		if (io_do_iopoll(ctx, true) == 0)
1626ed29b0b4SJens Axboe 			break;
1627ed29b0b4SJens Axboe 		/*
1628ed29b0b4SJens Axboe 		 * Ensure we allow local-to-the-cpu processing to take place,
1629ed29b0b4SJens Axboe 		 * in this case we need to ensure that we reap all events.
1630ed29b0b4SJens Axboe 		 * Also let task_work, etc. to progress by releasing the mutex
1631ed29b0b4SJens Axboe 		 */
1632ed29b0b4SJens Axboe 		if (need_resched()) {
1633ed29b0b4SJens Axboe 			mutex_unlock(&ctx->uring_lock);
1634ed29b0b4SJens Axboe 			cond_resched();
1635ed29b0b4SJens Axboe 			mutex_lock(&ctx->uring_lock);
1636ed29b0b4SJens Axboe 		}
1637ed29b0b4SJens Axboe 	}
1638ed29b0b4SJens Axboe 	mutex_unlock(&ctx->uring_lock);
1639ed29b0b4SJens Axboe }
1640ed29b0b4SJens Axboe 
1641ed29b0b4SJens Axboe static int io_iopoll_check(struct io_ring_ctx *ctx, long min)
1642ed29b0b4SJens Axboe {
1643ed29b0b4SJens Axboe 	unsigned int nr_events = 0;
1644ed29b0b4SJens Axboe 	int ret = 0;
1645ed29b0b4SJens Axboe 	unsigned long check_cq;
1646ed29b0b4SJens Axboe 
164776de6749SPavel Begunkov 	if (!io_allowed_run_tw(ctx))
164876de6749SPavel Begunkov 		return -EEXIST;
164976de6749SPavel Begunkov 
16503a08576bSPavel Begunkov 	check_cq = READ_ONCE(ctx->check_cq);
16513a08576bSPavel Begunkov 	if (unlikely(check_cq)) {
16523a08576bSPavel Begunkov 		if (check_cq & BIT(IO_CHECK_CQ_OVERFLOW_BIT))
1653a85381d8SPavel Begunkov 			__io_cqring_overflow_flush(ctx);
16543a08576bSPavel Begunkov 		/*
16553a08576bSPavel Begunkov 		 * Similarly do not spin if we have not informed the user of any
16563a08576bSPavel Begunkov 		 * dropped CQE.
16573a08576bSPavel Begunkov 		 */
16583a08576bSPavel Begunkov 		if (check_cq & BIT(IO_CHECK_CQ_DROPPED_BIT))
16593a08576bSPavel Begunkov 			return -EBADR;
16603a08576bSPavel Begunkov 	}
1661ed29b0b4SJens Axboe 	/*
1662ed29b0b4SJens Axboe 	 * Don't enter poll loop if we already have events pending.
1663ed29b0b4SJens Axboe 	 * If we do, we can potentially be spinning for commands that
1664ed29b0b4SJens Axboe 	 * already triggered a CQE (eg in error).
1665ed29b0b4SJens Axboe 	 */
1666ed29b0b4SJens Axboe 	if (io_cqring_events(ctx))
1667ed29b0b4SJens Axboe 		return 0;
1668ed29b0b4SJens Axboe 
1669ed29b0b4SJens Axboe 	do {
1670ed29b0b4SJens Axboe 		/*
1671ed29b0b4SJens Axboe 		 * If a submit got punted to a workqueue, we can have the
1672ed29b0b4SJens Axboe 		 * application entering polling for a command before it gets
1673ed29b0b4SJens Axboe 		 * issued. That app will hold the uring_lock for the duration
1674ed29b0b4SJens Axboe 		 * of the poll right here, so we need to take a breather every
1675ed29b0b4SJens Axboe 		 * now and then to ensure that the issue has a chance to add
1676ed29b0b4SJens Axboe 		 * the poll to the issued list. Otherwise we can spin here
1677ed29b0b4SJens Axboe 		 * forever, while the workqueue is stuck trying to acquire the
1678ed29b0b4SJens Axboe 		 * very same mutex.
1679ed29b0b4SJens Axboe 		 */
1680dac6a0eaSJens Axboe 		if (wq_list_empty(&ctx->iopoll_list) ||
1681dac6a0eaSJens Axboe 		    io_task_work_pending(ctx)) {
1682ed29b0b4SJens Axboe 			u32 tail = ctx->cached_cq_tail;
1683ed29b0b4SJens Axboe 
16848de11cdcSDylan Yudaken 			(void) io_run_local_work_locked(ctx);
16851f8d5bbeSPavel Begunkov 
16861f8d5bbeSPavel Begunkov 			if (task_work_pending(current) ||
16871f8d5bbeSPavel Begunkov 			    wq_list_empty(&ctx->iopoll_list)) {
1688ed29b0b4SJens Axboe 				mutex_unlock(&ctx->uring_lock);
1689ed29b0b4SJens Axboe 				io_run_task_work();
1690ed29b0b4SJens Axboe 				mutex_lock(&ctx->uring_lock);
16911f8d5bbeSPavel Begunkov 			}
1692ed29b0b4SJens Axboe 			/* some requests don't go through iopoll_list */
1693ed29b0b4SJens Axboe 			if (tail != ctx->cached_cq_tail ||
1694ed29b0b4SJens Axboe 			    wq_list_empty(&ctx->iopoll_list))
1695ed29b0b4SJens Axboe 				break;
1696ed29b0b4SJens Axboe 		}
1697ed29b0b4SJens Axboe 		ret = io_do_iopoll(ctx, !min);
1698ed29b0b4SJens Axboe 		if (ret < 0)
1699ed29b0b4SJens Axboe 			break;
1700ed29b0b4SJens Axboe 		nr_events += ret;
1701ed29b0b4SJens Axboe 		ret = 0;
1702ed29b0b4SJens Axboe 	} while (nr_events < min && !need_resched());
1703ed29b0b4SJens Axboe 
1704ed29b0b4SJens Axboe 	return ret;
1705ed29b0b4SJens Axboe }
17067012c815SPavel Begunkov 
1707a282967cSPavel Begunkov void io_req_task_complete(struct io_kiocb *req, struct io_tw_state *ts)
1708ed29b0b4SJens Axboe {
1709a282967cSPavel Begunkov 	if (ts->locked)
17109da070b1SPavel Begunkov 		io_req_complete_defer(req);
17117012c815SPavel Begunkov 	else
171227f35fe9SDylan Yudaken 		io_req_complete_post(req, IO_URING_F_UNLOCKED);
1713ed29b0b4SJens Axboe }
1714ed29b0b4SJens Axboe 
1715ed29b0b4SJens Axboe /*
1716ed29b0b4SJens Axboe  * After the iocb has been issued, it's safe to be found on the poll list.
1717ed29b0b4SJens Axboe  * Adding the kiocb to the list AFTER submission ensures that we don't
1718ed29b0b4SJens Axboe  * find it from a io_do_iopoll() thread before the issuer is done
1719ed29b0b4SJens Axboe  * accessing the kiocb cookie.
1720ed29b0b4SJens Axboe  */
1721ed29b0b4SJens Axboe static void io_iopoll_req_issued(struct io_kiocb *req, unsigned int issue_flags)
1722ed29b0b4SJens Axboe {
1723ed29b0b4SJens Axboe 	struct io_ring_ctx *ctx = req->ctx;
1724ed29b0b4SJens Axboe 	const bool needs_lock = issue_flags & IO_URING_F_UNLOCKED;
1725ed29b0b4SJens Axboe 
1726ed29b0b4SJens Axboe 	/* workqueue context doesn't hold uring_lock, grab it now */
1727ed29b0b4SJens Axboe 	if (unlikely(needs_lock))
1728ed29b0b4SJens Axboe 		mutex_lock(&ctx->uring_lock);
1729ed29b0b4SJens Axboe 
1730ed29b0b4SJens Axboe 	/*
1731ed29b0b4SJens Axboe 	 * Track whether we have multiple files in our lists. This will impact
1732ed29b0b4SJens Axboe 	 * how we do polling eventually, not spinning if we're on potentially
1733ed29b0b4SJens Axboe 	 * different devices.
1734ed29b0b4SJens Axboe 	 */
1735ed29b0b4SJens Axboe 	if (wq_list_empty(&ctx->iopoll_list)) {
1736ed29b0b4SJens Axboe 		ctx->poll_multi_queue = false;
1737ed29b0b4SJens Axboe 	} else if (!ctx->poll_multi_queue) {
1738ed29b0b4SJens Axboe 		struct io_kiocb *list_req;
1739ed29b0b4SJens Axboe 
1740ed29b0b4SJens Axboe 		list_req = container_of(ctx->iopoll_list.first, struct io_kiocb,
1741ed29b0b4SJens Axboe 					comp_list);
1742ed29b0b4SJens Axboe 		if (list_req->file != req->file)
1743ed29b0b4SJens Axboe 			ctx->poll_multi_queue = true;
1744ed29b0b4SJens Axboe 	}
1745ed29b0b4SJens Axboe 
1746ed29b0b4SJens Axboe 	/*
1747ed29b0b4SJens Axboe 	 * For fast devices, IO may have already completed. If it has, add
1748ed29b0b4SJens Axboe 	 * it to the front so we find it first.
1749ed29b0b4SJens Axboe 	 */
1750ed29b0b4SJens Axboe 	if (READ_ONCE(req->iopoll_completed))
1751ed29b0b4SJens Axboe 		wq_list_add_head(&req->comp_list, &ctx->iopoll_list);
1752ed29b0b4SJens Axboe 	else
1753ed29b0b4SJens Axboe 		wq_list_add_tail(&req->comp_list, &ctx->iopoll_list);
1754ed29b0b4SJens Axboe 
1755ed29b0b4SJens Axboe 	if (unlikely(needs_lock)) {
1756ed29b0b4SJens Axboe 		/*
1757ed29b0b4SJens Axboe 		 * If IORING_SETUP_SQPOLL is enabled, sqes are either handle
1758ed29b0b4SJens Axboe 		 * in sq thread task context or in io worker task context. If
1759ed29b0b4SJens Axboe 		 * current task context is sq thread, we don't need to check
1760ed29b0b4SJens Axboe 		 * whether should wake up sq thread.
1761ed29b0b4SJens Axboe 		 */
1762ed29b0b4SJens Axboe 		if ((ctx->flags & IORING_SETUP_SQPOLL) &&
1763ed29b0b4SJens Axboe 		    wq_has_sleeper(&ctx->sq_data->wait))
1764ed29b0b4SJens Axboe 			wake_up(&ctx->sq_data->wait);
1765ed29b0b4SJens Axboe 
1766ed29b0b4SJens Axboe 		mutex_unlock(&ctx->uring_lock);
1767ed29b0b4SJens Axboe 	}
1768ed29b0b4SJens Axboe }
1769ed29b0b4SJens Axboe 
1770ed29b0b4SJens Axboe /*
1771ed29b0b4SJens Axboe  * If we tracked the file through the SCM inflight mechanism, we could support
1772ed29b0b4SJens Axboe  * any file. For now, just ensure that anything potentially problematic is done
1773ed29b0b4SJens Axboe  * inline.
1774ed29b0b4SJens Axboe  */
1775ed29b0b4SJens Axboe static bool __io_file_supports_nowait(struct file *file, umode_t mode)
1776ed29b0b4SJens Axboe {
1777ed29b0b4SJens Axboe 	/* any ->read/write should understand O_NONBLOCK */
1778ed29b0b4SJens Axboe 	if (file->f_flags & O_NONBLOCK)
1779ed29b0b4SJens Axboe 		return true;
1780ed29b0b4SJens Axboe 	return file->f_mode & FMODE_NOWAIT;
1781ed29b0b4SJens Axboe }
1782ed29b0b4SJens Axboe 
1783ed29b0b4SJens Axboe /*
1784ed29b0b4SJens Axboe  * If we tracked the file through the SCM inflight mechanism, we could support
1785ed29b0b4SJens Axboe  * any file. For now, just ensure that anything potentially problematic is done
1786ed29b0b4SJens Axboe  * inline.
1787ed29b0b4SJens Axboe  */
1788a4ad4f74SJens Axboe unsigned int io_file_get_flags(struct file *file)
1789ed29b0b4SJens Axboe {
1790ed29b0b4SJens Axboe 	umode_t mode = file_inode(file)->i_mode;
1791ed29b0b4SJens Axboe 	unsigned int res = 0;
1792ed29b0b4SJens Axboe 
1793ed29b0b4SJens Axboe 	if (S_ISREG(mode))
1794ed29b0b4SJens Axboe 		res |= FFS_ISREG;
1795ed29b0b4SJens Axboe 	if (__io_file_supports_nowait(file, mode))
1796ed29b0b4SJens Axboe 		res |= FFS_NOWAIT;
1797ed29b0b4SJens Axboe 	return res;
1798ed29b0b4SJens Axboe }
1799ed29b0b4SJens Axboe 
180099f15d8dSJens Axboe bool io_alloc_async_data(struct io_kiocb *req)
1801ed29b0b4SJens Axboe {
1802f30bd4d0SBreno Leitao 	WARN_ON_ONCE(!io_cold_defs[req->opcode].async_size);
1803f30bd4d0SBreno Leitao 	req->async_data = kmalloc(io_cold_defs[req->opcode].async_size, GFP_KERNEL);
1804ed29b0b4SJens Axboe 	if (req->async_data) {
1805ed29b0b4SJens Axboe 		req->flags |= REQ_F_ASYNC_DATA;
1806ed29b0b4SJens Axboe 		return false;
1807ed29b0b4SJens Axboe 	}
1808ed29b0b4SJens Axboe 	return true;
1809ed29b0b4SJens Axboe }
1810ed29b0b4SJens Axboe 
1811f3b44f92SJens Axboe int io_req_prep_async(struct io_kiocb *req)
1812ed29b0b4SJens Axboe {
1813f30bd4d0SBreno Leitao 	const struct io_cold_def *cdef = &io_cold_defs[req->opcode];
1814a7dd2782SBreno Leitao 	const struct io_issue_def *def = &io_issue_defs[req->opcode];
1815ed29b0b4SJens Axboe 
1816ed29b0b4SJens Axboe 	/* assign early for deferred execution for non-fixed file */
181754aa7f23SJoseph Qi 	if (def->needs_file && !(req->flags & REQ_F_FIXED_FILE) && !req->file)
1818ed29b0b4SJens Axboe 		req->file = io_file_get_normal(req, req->cqe.fd);
1819f30bd4d0SBreno Leitao 	if (!cdef->prep_async)
1820ed29b0b4SJens Axboe 		return 0;
1821ed29b0b4SJens Axboe 	if (WARN_ON_ONCE(req_has_async_data(req)))
1822ed29b0b4SJens Axboe 		return -EFAULT;
1823f30bd4d0SBreno Leitao 	if (!def->manual_alloc) {
1824ed29b0b4SJens Axboe 		if (io_alloc_async_data(req))
1825ed29b0b4SJens Axboe 			return -EAGAIN;
182659169439SPavel Begunkov 	}
1827f30bd4d0SBreno Leitao 	return cdef->prep_async(req);
1828ed29b0b4SJens Axboe }
1829ed29b0b4SJens Axboe 
1830ed29b0b4SJens Axboe static u32 io_get_sequence(struct io_kiocb *req)
1831ed29b0b4SJens Axboe {
1832ed29b0b4SJens Axboe 	u32 seq = req->ctx->cached_sq_head;
1833ed29b0b4SJens Axboe 	struct io_kiocb *cur;
1834ed29b0b4SJens Axboe 
1835ed29b0b4SJens Axboe 	/* need original cached_sq_head, but it was increased for each req */
1836ed29b0b4SJens Axboe 	io_for_each_link(cur, req)
1837ed29b0b4SJens Axboe 		seq--;
1838ed29b0b4SJens Axboe 	return seq;
1839ed29b0b4SJens Axboe }
1840ed29b0b4SJens Axboe 
1841ed29b0b4SJens Axboe static __cold void io_drain_req(struct io_kiocb *req)
1842e276ae34SPavel Begunkov 	__must_hold(&ctx->uring_lock)
1843ed29b0b4SJens Axboe {
1844ed29b0b4SJens Axboe 	struct io_ring_ctx *ctx = req->ctx;
1845ed29b0b4SJens Axboe 	struct io_defer_entry *de;
1846ed29b0b4SJens Axboe 	int ret;
1847ed29b0b4SJens Axboe 	u32 seq = io_get_sequence(req);
1848ed29b0b4SJens Axboe 
1849ed29b0b4SJens Axboe 	/* Still need defer if there is pending req in defer list. */
1850ed29b0b4SJens Axboe 	spin_lock(&ctx->completion_lock);
1851ed29b0b4SJens Axboe 	if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list)) {
1852ed29b0b4SJens Axboe 		spin_unlock(&ctx->completion_lock);
1853ed29b0b4SJens Axboe queue:
1854ed29b0b4SJens Axboe 		ctx->drain_active = false;
1855ed29b0b4SJens Axboe 		io_req_task_queue(req);
1856ed29b0b4SJens Axboe 		return;
1857ed29b0b4SJens Axboe 	}
1858ed29b0b4SJens Axboe 	spin_unlock(&ctx->completion_lock);
1859ed29b0b4SJens Axboe 
1860ed29b0b4SJens Axboe 	io_prep_async_link(req);
1861ed29b0b4SJens Axboe 	de = kmalloc(sizeof(*de), GFP_KERNEL);
1862ed29b0b4SJens Axboe 	if (!de) {
1863ed29b0b4SJens Axboe 		ret = -ENOMEM;
1864ef5c600aSDylan Yudaken 		io_req_defer_failed(req, ret);
1865ef5c600aSDylan Yudaken 		return;
1866ed29b0b4SJens Axboe 	}
1867ed29b0b4SJens Axboe 
1868ed29b0b4SJens Axboe 	spin_lock(&ctx->completion_lock);
1869ed29b0b4SJens Axboe 	if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) {
1870ed29b0b4SJens Axboe 		spin_unlock(&ctx->completion_lock);
1871ed29b0b4SJens Axboe 		kfree(de);
1872ed29b0b4SJens Axboe 		goto queue;
1873ed29b0b4SJens Axboe 	}
1874ed29b0b4SJens Axboe 
187548863ffdSPavel Begunkov 	trace_io_uring_defer(req);
1876ed29b0b4SJens Axboe 	de->req = req;
1877ed29b0b4SJens Axboe 	de->seq = seq;
1878ed29b0b4SJens Axboe 	list_add_tail(&de->list, &ctx->defer_list);
1879ed29b0b4SJens Axboe 	spin_unlock(&ctx->completion_lock);
1880ed29b0b4SJens Axboe }
1881ed29b0b4SJens Axboe 
1882ed29b0b4SJens Axboe static void io_clean_op(struct io_kiocb *req)
1883ed29b0b4SJens Axboe {
1884ed29b0b4SJens Axboe 	if (req->flags & REQ_F_BUFFER_SELECTED) {
1885ed29b0b4SJens Axboe 		spin_lock(&req->ctx->completion_lock);
1886ed29b0b4SJens Axboe 		io_put_kbuf_comp(req);
1887ed29b0b4SJens Axboe 		spin_unlock(&req->ctx->completion_lock);
1888ed29b0b4SJens Axboe 	}
1889ed29b0b4SJens Axboe 
1890ed29b0b4SJens Axboe 	if (req->flags & REQ_F_NEED_CLEANUP) {
1891f30bd4d0SBreno Leitao 		const struct io_cold_def *def = &io_cold_defs[req->opcode];
1892ed29b0b4SJens Axboe 
18934d4c9cffSJens Axboe 		if (def->cleanup)
18944d4c9cffSJens Axboe 			def->cleanup(req);
1895ed29b0b4SJens Axboe 	}
1896ed29b0b4SJens Axboe 	if ((req->flags & REQ_F_POLLED) && req->apoll) {
1897ed29b0b4SJens Axboe 		kfree(req->apoll->double_poll);
1898ed29b0b4SJens Axboe 		kfree(req->apoll);
1899ed29b0b4SJens Axboe 		req->apoll = NULL;
1900ed29b0b4SJens Axboe 	}
1901ed29b0b4SJens Axboe 	if (req->flags & REQ_F_INFLIGHT) {
1902ed29b0b4SJens Axboe 		struct io_uring_task *tctx = req->task->io_uring;
1903ed29b0b4SJens Axboe 
1904ed29b0b4SJens Axboe 		atomic_dec(&tctx->inflight_tracked);
1905ed29b0b4SJens Axboe 	}
1906ed29b0b4SJens Axboe 	if (req->flags & REQ_F_CREDS)
1907ed29b0b4SJens Axboe 		put_cred(req->creds);
1908ed29b0b4SJens Axboe 	if (req->flags & REQ_F_ASYNC_DATA) {
1909ed29b0b4SJens Axboe 		kfree(req->async_data);
1910ed29b0b4SJens Axboe 		req->async_data = NULL;
1911ed29b0b4SJens Axboe 	}
1912ed29b0b4SJens Axboe 	req->flags &= ~IO_REQ_CLEAN_FLAGS;
1913ed29b0b4SJens Axboe }
1914ed29b0b4SJens Axboe 
1915f4992544SJens Axboe static bool io_assign_file(struct io_kiocb *req, const struct io_issue_def *def,
1916f4992544SJens Axboe 			   unsigned int issue_flags)
1917ed29b0b4SJens Axboe {
1918f4992544SJens Axboe 	if (req->file || !def->needs_file)
1919ed29b0b4SJens Axboe 		return true;
1920ed29b0b4SJens Axboe 
1921ed29b0b4SJens Axboe 	if (req->flags & REQ_F_FIXED_FILE)
1922ed29b0b4SJens Axboe 		req->file = io_file_get_fixed(req, req->cqe.fd, issue_flags);
1923ed29b0b4SJens Axboe 	else
1924ed29b0b4SJens Axboe 		req->file = io_file_get_normal(req, req->cqe.fd);
1925ed29b0b4SJens Axboe 
1926ed29b0b4SJens Axboe 	return !!req->file;
1927ed29b0b4SJens Axboe }
1928ed29b0b4SJens Axboe 
1929ed29b0b4SJens Axboe static int io_issue_sqe(struct io_kiocb *req, unsigned int issue_flags)
1930ed29b0b4SJens Axboe {
1931a7dd2782SBreno Leitao 	const struct io_issue_def *def = &io_issue_defs[req->opcode];
1932ed29b0b4SJens Axboe 	const struct cred *creds = NULL;
1933ed29b0b4SJens Axboe 	int ret;
1934ed29b0b4SJens Axboe 
1935f4992544SJens Axboe 	if (unlikely(!io_assign_file(req, def, issue_flags)))
1936ed29b0b4SJens Axboe 		return -EBADF;
1937ed29b0b4SJens Axboe 
1938ed29b0b4SJens Axboe 	if (unlikely((req->flags & REQ_F_CREDS) && req->creds != current_cred()))
1939ed29b0b4SJens Axboe 		creds = override_creds(req->creds);
1940ed29b0b4SJens Axboe 
1941ed29b0b4SJens Axboe 	if (!def->audit_skip)
1942ed29b0b4SJens Axboe 		audit_uring_entry(req->opcode);
1943ed29b0b4SJens Axboe 
1944ed29b0b4SJens Axboe 	ret = def->issue(req, issue_flags);
1945ed29b0b4SJens Axboe 
1946ed29b0b4SJens Axboe 	if (!def->audit_skip)
1947ed29b0b4SJens Axboe 		audit_uring_exit(!ret, ret);
1948ed29b0b4SJens Axboe 
1949ed29b0b4SJens Axboe 	if (creds)
1950ed29b0b4SJens Axboe 		revert_creds(creds);
195197b388d7SJens Axboe 
195275d7b3aeSPavel Begunkov 	if (ret == IOU_OK) {
195375d7b3aeSPavel Begunkov 		if (issue_flags & IO_URING_F_COMPLETE_DEFER)
19549da070b1SPavel Begunkov 			io_req_complete_defer(req);
195575d7b3aeSPavel Begunkov 		else
19561bec951cSPavel Begunkov 			io_req_complete_post(req, issue_flags);
195775d7b3aeSPavel Begunkov 	} else if (ret != IOU_ISSUE_SKIP_COMPLETE)
1958ed29b0b4SJens Axboe 		return ret;
195997b388d7SJens Axboe 
1960ed29b0b4SJens Axboe 	/* If the op doesn't have a file, we're not polling for it */
1961ef0ec1adSPavel Begunkov 	if ((req->ctx->flags & IORING_SETUP_IOPOLL) && def->iopoll_queue)
1962ed29b0b4SJens Axboe 		io_iopoll_req_issued(req, issue_flags);
1963ed29b0b4SJens Axboe 
1964ed29b0b4SJens Axboe 	return 0;
1965ed29b0b4SJens Axboe }
1966ed29b0b4SJens Axboe 
1967a282967cSPavel Begunkov int io_poll_issue(struct io_kiocb *req, struct io_tw_state *ts)
1968329061d3SJens Axboe {
1969a282967cSPavel Begunkov 	io_tw_lock(req->ctx, ts);
19709a692451SDylan Yudaken 	return io_issue_sqe(req, IO_URING_F_NONBLOCK|IO_URING_F_MULTISHOT|
19719a692451SDylan Yudaken 				 IO_URING_F_COMPLETE_DEFER);
1972329061d3SJens Axboe }
1973329061d3SJens Axboe 
1974c9f06aa7SJens Axboe struct io_wq_work *io_wq_free_work(struct io_wq_work *work)
1975ed29b0b4SJens Axboe {
1976ed29b0b4SJens Axboe 	struct io_kiocb *req = container_of(work, struct io_kiocb, work);
1977ed29b0b4SJens Axboe 
1978ed29b0b4SJens Axboe 	req = io_put_req_find_next(req);
1979ed29b0b4SJens Axboe 	return req ? &req->work : NULL;
1980ed29b0b4SJens Axboe }
1981ed29b0b4SJens Axboe 
1982c9f06aa7SJens Axboe void io_wq_submit_work(struct io_wq_work *work)
1983ed29b0b4SJens Axboe {
1984ed29b0b4SJens Axboe 	struct io_kiocb *req = container_of(work, struct io_kiocb, work);
1985a7dd2782SBreno Leitao 	const struct io_issue_def *def = &io_issue_defs[req->opcode];
1986e6aeb272SPavel Begunkov 	unsigned int issue_flags = IO_URING_F_UNLOCKED | IO_URING_F_IOWQ;
1987ed29b0b4SJens Axboe 	bool needs_poll = false;
1988ed29b0b4SJens Axboe 	int ret = 0, err = -ECANCELED;
1989ed29b0b4SJens Axboe 
199023a6c9acSLin Ma 	/* one will be dropped by ->io_wq_free_work() after returning to io-wq */
1991ed29b0b4SJens Axboe 	if (!(req->flags & REQ_F_REFCOUNT))
1992ed29b0b4SJens Axboe 		__io_req_set_refcount(req, 2);
1993ed29b0b4SJens Axboe 	else
1994ed29b0b4SJens Axboe 		req_ref_get(req);
1995ed29b0b4SJens Axboe 
1996ed29b0b4SJens Axboe 	io_arm_ltimeout(req);
1997ed29b0b4SJens Axboe 
1998ed29b0b4SJens Axboe 	/* either cancelled or io-wq is dying, so don't touch tctx->iowq */
1999ed29b0b4SJens Axboe 	if (work->flags & IO_WQ_WORK_CANCEL) {
2000ed29b0b4SJens Axboe fail:
2001ed29b0b4SJens Axboe 		io_req_task_queue_fail(req, err);
2002ed29b0b4SJens Axboe 		return;
2003ed29b0b4SJens Axboe 	}
2004f4992544SJens Axboe 	if (!io_assign_file(req, def, issue_flags)) {
2005ed29b0b4SJens Axboe 		err = -EBADF;
2006ed29b0b4SJens Axboe 		work->flags |= IO_WQ_WORK_CANCEL;
2007ed29b0b4SJens Axboe 		goto fail;
2008ed29b0b4SJens Axboe 	}
2009ed29b0b4SJens Axboe 
2010ed29b0b4SJens Axboe 	if (req->flags & REQ_F_FORCE_ASYNC) {
2011ed29b0b4SJens Axboe 		bool opcode_poll = def->pollin || def->pollout;
2012ed29b0b4SJens Axboe 
2013ed29b0b4SJens Axboe 		if (opcode_poll && file_can_poll(req->file)) {
2014ed29b0b4SJens Axboe 			needs_poll = true;
2015ed29b0b4SJens Axboe 			issue_flags |= IO_URING_F_NONBLOCK;
2016ed29b0b4SJens Axboe 		}
2017ed29b0b4SJens Axboe 	}
2018ed29b0b4SJens Axboe 
2019ed29b0b4SJens Axboe 	do {
2020ed29b0b4SJens Axboe 		ret = io_issue_sqe(req, issue_flags);
2021ed29b0b4SJens Axboe 		if (ret != -EAGAIN)
2022ed29b0b4SJens Axboe 			break;
2023ed29b0b4SJens Axboe 		/*
2024ed29b0b4SJens Axboe 		 * We can get EAGAIN for iopolled IO even though we're
2025ed29b0b4SJens Axboe 		 * forcing a sync submission from here, since we can't
2026ed29b0b4SJens Axboe 		 * wait for request slots on the block side.
2027ed29b0b4SJens Axboe 		 */
2028ed29b0b4SJens Axboe 		if (!needs_poll) {
2029ed29b0b4SJens Axboe 			if (!(req->ctx->flags & IORING_SETUP_IOPOLL))
2030ed29b0b4SJens Axboe 				break;
2031ed29b0b4SJens Axboe 			cond_resched();
2032ed29b0b4SJens Axboe 			continue;
2033ed29b0b4SJens Axboe 		}
2034ed29b0b4SJens Axboe 
2035ed29b0b4SJens Axboe 		if (io_arm_poll_handler(req, issue_flags) == IO_APOLL_OK)
2036ed29b0b4SJens Axboe 			return;
2037ed29b0b4SJens Axboe 		/* aborted or ready, in either case retry blocking */
2038ed29b0b4SJens Axboe 		needs_poll = false;
2039ed29b0b4SJens Axboe 		issue_flags &= ~IO_URING_F_NONBLOCK;
2040ed29b0b4SJens Axboe 	} while (1);
2041ed29b0b4SJens Axboe 
2042ed29b0b4SJens Axboe 	/* avoid locking problems by failing it from a clean context */
204397b388d7SJens Axboe 	if (ret < 0)
2044ed29b0b4SJens Axboe 		io_req_task_queue_fail(req, ret);
2045ed29b0b4SJens Axboe }
2046ed29b0b4SJens Axboe 
2047531113bbSJens Axboe inline struct file *io_file_get_fixed(struct io_kiocb *req, int fd,
2048ed29b0b4SJens Axboe 				      unsigned int issue_flags)
2049ed29b0b4SJens Axboe {
2050ed29b0b4SJens Axboe 	struct io_ring_ctx *ctx = req->ctx;
2051ed29b0b4SJens Axboe 	struct file *file = NULL;
2052ed29b0b4SJens Axboe 	unsigned long file_ptr;
2053ed29b0b4SJens Axboe 
2054ed29b0b4SJens Axboe 	io_ring_submit_lock(ctx, issue_flags);
2055ed29b0b4SJens Axboe 
2056ed29b0b4SJens Axboe 	if (unlikely((unsigned int)fd >= ctx->nr_user_files))
2057ed29b0b4SJens Axboe 		goto out;
2058ed29b0b4SJens Axboe 	fd = array_index_nospec(fd, ctx->nr_user_files);
2059ed29b0b4SJens Axboe 	file_ptr = io_fixed_file_slot(&ctx->file_table, fd)->file_ptr;
2060ed29b0b4SJens Axboe 	file = (struct file *) (file_ptr & FFS_MASK);
2061ed29b0b4SJens Axboe 	file_ptr &= ~FFS_MASK;
2062ed29b0b4SJens Axboe 	/* mask in overlapping REQ_F and FFS bits */
2063ed29b0b4SJens Axboe 	req->flags |= (file_ptr << REQ_F_SUPPORT_NOWAIT_BIT);
2064ed29b0b4SJens Axboe 	io_req_set_rsrc_node(req, ctx, 0);
2065ed29b0b4SJens Axboe out:
2066ed29b0b4SJens Axboe 	io_ring_submit_unlock(ctx, issue_flags);
2067ed29b0b4SJens Axboe 	return file;
2068ed29b0b4SJens Axboe }
2069ed29b0b4SJens Axboe 
2070531113bbSJens Axboe struct file *io_file_get_normal(struct io_kiocb *req, int fd)
2071ed29b0b4SJens Axboe {
2072ed29b0b4SJens Axboe 	struct file *file = fget(fd);
2073ed29b0b4SJens Axboe 
207448863ffdSPavel Begunkov 	trace_io_uring_file_get(req, fd);
2075ed29b0b4SJens Axboe 
2076ed29b0b4SJens Axboe 	/* we don't allow fixed io_uring files */
2077e5550a14SJens Axboe 	if (file && io_is_uring_fops(file))
2078ed29b0b4SJens Axboe 		io_req_track_inflight(req);
2079ed29b0b4SJens Axboe 	return file;
2080ed29b0b4SJens Axboe }
2081ed29b0b4SJens Axboe 
2082ed29b0b4SJens Axboe static void io_queue_async(struct io_kiocb *req, int ret)
2083ed29b0b4SJens Axboe 	__must_hold(&req->ctx->uring_lock)
2084ed29b0b4SJens Axboe {
2085ed29b0b4SJens Axboe 	struct io_kiocb *linked_timeout;
2086ed29b0b4SJens Axboe 
2087ed29b0b4SJens Axboe 	if (ret != -EAGAIN || (req->flags & REQ_F_NOWAIT)) {
2088973fc83fSDylan Yudaken 		io_req_defer_failed(req, ret);
2089ed29b0b4SJens Axboe 		return;
2090ed29b0b4SJens Axboe 	}
2091ed29b0b4SJens Axboe 
2092ed29b0b4SJens Axboe 	linked_timeout = io_prep_linked_timeout(req);
2093ed29b0b4SJens Axboe 
2094ed29b0b4SJens Axboe 	switch (io_arm_poll_handler(req, 0)) {
2095ed29b0b4SJens Axboe 	case IO_APOLL_READY:
2096336d28a8SPavel Begunkov 		io_kbuf_recycle(req, 0);
2097ed29b0b4SJens Axboe 		io_req_task_queue(req);
2098ed29b0b4SJens Axboe 		break;
2099ed29b0b4SJens Axboe 	case IO_APOLL_ABORTED:
2100ed29b0b4SJens Axboe 		io_kbuf_recycle(req, 0);
2101ed29b0b4SJens Axboe 		io_queue_iowq(req, NULL);
2102ed29b0b4SJens Axboe 		break;
2103ed29b0b4SJens Axboe 	case IO_APOLL_OK:
2104ed29b0b4SJens Axboe 		break;
2105ed29b0b4SJens Axboe 	}
2106ed29b0b4SJens Axboe 
2107ed29b0b4SJens Axboe 	if (linked_timeout)
2108ed29b0b4SJens Axboe 		io_queue_linked_timeout(linked_timeout);
2109ed29b0b4SJens Axboe }
2110ed29b0b4SJens Axboe 
2111ed29b0b4SJens Axboe static inline void io_queue_sqe(struct io_kiocb *req)
2112ed29b0b4SJens Axboe 	__must_hold(&req->ctx->uring_lock)
2113ed29b0b4SJens Axboe {
2114ed29b0b4SJens Axboe 	int ret;
2115ed29b0b4SJens Axboe 
2116ed29b0b4SJens Axboe 	ret = io_issue_sqe(req, IO_URING_F_NONBLOCK|IO_URING_F_COMPLETE_DEFER);
2117ed29b0b4SJens Axboe 
2118ed29b0b4SJens Axboe 	/*
2119ed29b0b4SJens Axboe 	 * We async punt it if the file wasn't marked NOWAIT, or if the file
2120ed29b0b4SJens Axboe 	 * doesn't support non-blocking read/write attempts
2121ed29b0b4SJens Axboe 	 */
2122ed29b0b4SJens Axboe 	if (likely(!ret))
2123ed29b0b4SJens Axboe 		io_arm_ltimeout(req);
2124ed29b0b4SJens Axboe 	else
2125ed29b0b4SJens Axboe 		io_queue_async(req, ret);
2126ed29b0b4SJens Axboe }
2127ed29b0b4SJens Axboe 
2128ed29b0b4SJens Axboe static void io_queue_sqe_fallback(struct io_kiocb *req)
2129ed29b0b4SJens Axboe 	__must_hold(&req->ctx->uring_lock)
2130ed29b0b4SJens Axboe {
2131ed29b0b4SJens Axboe 	if (unlikely(req->flags & REQ_F_FAIL)) {
2132ed29b0b4SJens Axboe 		/*
2133ed29b0b4SJens Axboe 		 * We don't submit, fail them all, for that replace hardlinks
2134ed29b0b4SJens Axboe 		 * with normal links. Extra REQ_F_LINK is tolerated.
2135ed29b0b4SJens Axboe 		 */
2136ed29b0b4SJens Axboe 		req->flags &= ~REQ_F_HARDLINK;
2137ed29b0b4SJens Axboe 		req->flags |= REQ_F_LINK;
2138973fc83fSDylan Yudaken 		io_req_defer_failed(req, req->cqe.res);
2139ed29b0b4SJens Axboe 	} else {
2140ed29b0b4SJens Axboe 		int ret = io_req_prep_async(req);
2141ed29b0b4SJens Axboe 
2142ef5c600aSDylan Yudaken 		if (unlikely(ret)) {
2143973fc83fSDylan Yudaken 			io_req_defer_failed(req, ret);
2144ef5c600aSDylan Yudaken 			return;
2145ef5c600aSDylan Yudaken 		}
2146ef5c600aSDylan Yudaken 
2147ef5c600aSDylan Yudaken 		if (unlikely(req->ctx->drain_active))
2148ef5c600aSDylan Yudaken 			io_drain_req(req);
2149ed29b0b4SJens Axboe 		else
2150ed29b0b4SJens Axboe 			io_queue_iowq(req, NULL);
2151ed29b0b4SJens Axboe 	}
2152ed29b0b4SJens Axboe }
2153ed29b0b4SJens Axboe 
2154ed29b0b4SJens Axboe /*
2155ed29b0b4SJens Axboe  * Check SQE restrictions (opcode and flags).
2156ed29b0b4SJens Axboe  *
2157ed29b0b4SJens Axboe  * Returns 'true' if SQE is allowed, 'false' otherwise.
2158ed29b0b4SJens Axboe  */
2159ed29b0b4SJens Axboe static inline bool io_check_restriction(struct io_ring_ctx *ctx,
2160ed29b0b4SJens Axboe 					struct io_kiocb *req,
2161ed29b0b4SJens Axboe 					unsigned int sqe_flags)
2162ed29b0b4SJens Axboe {
2163ed29b0b4SJens Axboe 	if (!test_bit(req->opcode, ctx->restrictions.sqe_op))
2164ed29b0b4SJens Axboe 		return false;
2165ed29b0b4SJens Axboe 
2166ed29b0b4SJens Axboe 	if ((sqe_flags & ctx->restrictions.sqe_flags_required) !=
2167ed29b0b4SJens Axboe 	    ctx->restrictions.sqe_flags_required)
2168ed29b0b4SJens Axboe 		return false;
2169ed29b0b4SJens Axboe 
2170ed29b0b4SJens Axboe 	if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed |
2171ed29b0b4SJens Axboe 			  ctx->restrictions.sqe_flags_required))
2172ed29b0b4SJens Axboe 		return false;
2173ed29b0b4SJens Axboe 
2174ed29b0b4SJens Axboe 	return true;
2175ed29b0b4SJens Axboe }
2176ed29b0b4SJens Axboe 
2177ed29b0b4SJens Axboe static void io_init_req_drain(struct io_kiocb *req)
2178ed29b0b4SJens Axboe {
2179ed29b0b4SJens Axboe 	struct io_ring_ctx *ctx = req->ctx;
2180ed29b0b4SJens Axboe 	struct io_kiocb *head = ctx->submit_state.link.head;
2181ed29b0b4SJens Axboe 
2182ed29b0b4SJens Axboe 	ctx->drain_active = true;
2183ed29b0b4SJens Axboe 	if (head) {
2184ed29b0b4SJens Axboe 		/*
2185ed29b0b4SJens Axboe 		 * If we need to drain a request in the middle of a link, drain
2186ed29b0b4SJens Axboe 		 * the head request and the next request/link after the current
2187ed29b0b4SJens Axboe 		 * link. Considering sequential execution of links,
2188ed29b0b4SJens Axboe 		 * REQ_F_IO_DRAIN will be maintained for every request of our
2189ed29b0b4SJens Axboe 		 * link.
2190ed29b0b4SJens Axboe 		 */
2191ed29b0b4SJens Axboe 		head->flags |= REQ_F_IO_DRAIN | REQ_F_FORCE_ASYNC;
2192ed29b0b4SJens Axboe 		ctx->drain_next = true;
2193ed29b0b4SJens Axboe 	}
2194ed29b0b4SJens Axboe }
2195ed29b0b4SJens Axboe 
2196ed29b0b4SJens Axboe static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req,
2197ed29b0b4SJens Axboe 		       const struct io_uring_sqe *sqe)
2198ed29b0b4SJens Axboe 	__must_hold(&ctx->uring_lock)
2199ed29b0b4SJens Axboe {
2200a7dd2782SBreno Leitao 	const struct io_issue_def *def;
2201ed29b0b4SJens Axboe 	unsigned int sqe_flags;
2202ed29b0b4SJens Axboe 	int personality;
2203ed29b0b4SJens Axboe 	u8 opcode;
2204ed29b0b4SJens Axboe 
2205ed29b0b4SJens Axboe 	/* req is partially pre-initialised, see io_preinit_req() */
2206ed29b0b4SJens Axboe 	req->opcode = opcode = READ_ONCE(sqe->opcode);
2207ed29b0b4SJens Axboe 	/* same numerical values with corresponding REQ_F_*, safe to copy */
2208ed29b0b4SJens Axboe 	req->flags = sqe_flags = READ_ONCE(sqe->flags);
2209ed29b0b4SJens Axboe 	req->cqe.user_data = READ_ONCE(sqe->user_data);
2210ed29b0b4SJens Axboe 	req->file = NULL;
2211ed29b0b4SJens Axboe 	req->rsrc_node = NULL;
2212ed29b0b4SJens Axboe 	req->task = current;
2213ed29b0b4SJens Axboe 
2214ed29b0b4SJens Axboe 	if (unlikely(opcode >= IORING_OP_LAST)) {
2215ed29b0b4SJens Axboe 		req->opcode = 0;
2216ed29b0b4SJens Axboe 		return -EINVAL;
2217ed29b0b4SJens Axboe 	}
2218a7dd2782SBreno Leitao 	def = &io_issue_defs[opcode];
2219ed29b0b4SJens Axboe 	if (unlikely(sqe_flags & ~SQE_COMMON_FLAGS)) {
2220ed29b0b4SJens Axboe 		/* enforce forwards compatibility on users */
2221ed29b0b4SJens Axboe 		if (sqe_flags & ~SQE_VALID_FLAGS)
2222ed29b0b4SJens Axboe 			return -EINVAL;
2223ed29b0b4SJens Axboe 		if (sqe_flags & IOSQE_BUFFER_SELECT) {
2224ed29b0b4SJens Axboe 			if (!def->buffer_select)
2225ed29b0b4SJens Axboe 				return -EOPNOTSUPP;
2226ed29b0b4SJens Axboe 			req->buf_index = READ_ONCE(sqe->buf_group);
2227ed29b0b4SJens Axboe 		}
2228ed29b0b4SJens Axboe 		if (sqe_flags & IOSQE_CQE_SKIP_SUCCESS)
2229ed29b0b4SJens Axboe 			ctx->drain_disabled = true;
2230ed29b0b4SJens Axboe 		if (sqe_flags & IOSQE_IO_DRAIN) {
2231ed29b0b4SJens Axboe 			if (ctx->drain_disabled)
2232ed29b0b4SJens Axboe 				return -EOPNOTSUPP;
2233ed29b0b4SJens Axboe 			io_init_req_drain(req);
2234ed29b0b4SJens Axboe 		}
2235ed29b0b4SJens Axboe 	}
2236ed29b0b4SJens Axboe 	if (unlikely(ctx->restricted || ctx->drain_active || ctx->drain_next)) {
2237ed29b0b4SJens Axboe 		if (ctx->restricted && !io_check_restriction(ctx, req, sqe_flags))
2238ed29b0b4SJens Axboe 			return -EACCES;
2239ed29b0b4SJens Axboe 		/* knock it to the slow queue path, will be drained there */
2240ed29b0b4SJens Axboe 		if (ctx->drain_active)
2241ed29b0b4SJens Axboe 			req->flags |= REQ_F_FORCE_ASYNC;
2242ed29b0b4SJens Axboe 		/* if there is no link, we're at "next" request and need to drain */
2243ed29b0b4SJens Axboe 		if (unlikely(ctx->drain_next) && !ctx->submit_state.link.head) {
2244ed29b0b4SJens Axboe 			ctx->drain_next = false;
2245ed29b0b4SJens Axboe 			ctx->drain_active = true;
2246ed29b0b4SJens Axboe 			req->flags |= REQ_F_IO_DRAIN | REQ_F_FORCE_ASYNC;
2247ed29b0b4SJens Axboe 		}
2248ed29b0b4SJens Axboe 	}
2249ed29b0b4SJens Axboe 
2250ed29b0b4SJens Axboe 	if (!def->ioprio && sqe->ioprio)
2251ed29b0b4SJens Axboe 		return -EINVAL;
2252ed29b0b4SJens Axboe 	if (!def->iopoll && (ctx->flags & IORING_SETUP_IOPOLL))
2253ed29b0b4SJens Axboe 		return -EINVAL;
2254ed29b0b4SJens Axboe 
2255ed29b0b4SJens Axboe 	if (def->needs_file) {
2256ed29b0b4SJens Axboe 		struct io_submit_state *state = &ctx->submit_state;
2257ed29b0b4SJens Axboe 
2258ed29b0b4SJens Axboe 		req->cqe.fd = READ_ONCE(sqe->fd);
2259ed29b0b4SJens Axboe 
2260ed29b0b4SJens Axboe 		/*
2261ed29b0b4SJens Axboe 		 * Plug now if we have more than 2 IO left after this, and the
2262ed29b0b4SJens Axboe 		 * target is potentially a read/write to block based storage.
2263ed29b0b4SJens Axboe 		 */
2264ed29b0b4SJens Axboe 		if (state->need_plug && def->plug) {
2265ed29b0b4SJens Axboe 			state->plug_started = true;
2266ed29b0b4SJens Axboe 			state->need_plug = false;
2267ed29b0b4SJens Axboe 			blk_start_plug_nr_ios(&state->plug, state->submit_nr);
2268ed29b0b4SJens Axboe 		}
2269ed29b0b4SJens Axboe 	}
2270ed29b0b4SJens Axboe 
2271ed29b0b4SJens Axboe 	personality = READ_ONCE(sqe->personality);
2272ed29b0b4SJens Axboe 	if (personality) {
2273ed29b0b4SJens Axboe 		int ret;
2274ed29b0b4SJens Axboe 
2275ed29b0b4SJens Axboe 		req->creds = xa_load(&ctx->personalities, personality);
2276ed29b0b4SJens Axboe 		if (!req->creds)
2277ed29b0b4SJens Axboe 			return -EINVAL;
2278ed29b0b4SJens Axboe 		get_cred(req->creds);
2279ed29b0b4SJens Axboe 		ret = security_uring_override_creds(req->creds);
2280ed29b0b4SJens Axboe 		if (ret) {
2281ed29b0b4SJens Axboe 			put_cred(req->creds);
2282ed29b0b4SJens Axboe 			return ret;
2283ed29b0b4SJens Axboe 		}
2284ed29b0b4SJens Axboe 		req->flags |= REQ_F_CREDS;
2285ed29b0b4SJens Axboe 	}
2286ed29b0b4SJens Axboe 
2287ed29b0b4SJens Axboe 	return def->prep(req, sqe);
2288ed29b0b4SJens Axboe }
2289ed29b0b4SJens Axboe 
2290ed29b0b4SJens Axboe static __cold int io_submit_fail_init(const struct io_uring_sqe *sqe,
2291ed29b0b4SJens Axboe 				      struct io_kiocb *req, int ret)
2292ed29b0b4SJens Axboe {
2293ed29b0b4SJens Axboe 	struct io_ring_ctx *ctx = req->ctx;
2294ed29b0b4SJens Axboe 	struct io_submit_link *link = &ctx->submit_state.link;
2295ed29b0b4SJens Axboe 	struct io_kiocb *head = link->head;
2296ed29b0b4SJens Axboe 
229748863ffdSPavel Begunkov 	trace_io_uring_req_failed(sqe, req, ret);
2298ed29b0b4SJens Axboe 
2299ed29b0b4SJens Axboe 	/*
2300ed29b0b4SJens Axboe 	 * Avoid breaking links in the middle as it renders links with SQPOLL
2301ed29b0b4SJens Axboe 	 * unusable. Instead of failing eagerly, continue assembling the link if
2302ed29b0b4SJens Axboe 	 * applicable and mark the head with REQ_F_FAIL. The link flushing code
2303ed29b0b4SJens Axboe 	 * should find the flag and handle the rest.
2304ed29b0b4SJens Axboe 	 */
2305ed29b0b4SJens Axboe 	req_fail_link_node(req, ret);
2306ed29b0b4SJens Axboe 	if (head && !(head->flags & REQ_F_FAIL))
2307ed29b0b4SJens Axboe 		req_fail_link_node(head, -ECANCELED);
2308ed29b0b4SJens Axboe 
2309ed29b0b4SJens Axboe 	if (!(req->flags & IO_REQ_LINK_FLAGS)) {
2310ed29b0b4SJens Axboe 		if (head) {
2311ed29b0b4SJens Axboe 			link->last->link = req;
2312ed29b0b4SJens Axboe 			link->head = NULL;
2313ed29b0b4SJens Axboe 			req = head;
2314ed29b0b4SJens Axboe 		}
2315ed29b0b4SJens Axboe 		io_queue_sqe_fallback(req);
2316ed29b0b4SJens Axboe 		return ret;
2317ed29b0b4SJens Axboe 	}
2318ed29b0b4SJens Axboe 
2319ed29b0b4SJens Axboe 	if (head)
2320ed29b0b4SJens Axboe 		link->last->link = req;
2321ed29b0b4SJens Axboe 	else
2322ed29b0b4SJens Axboe 		link->head = req;
2323ed29b0b4SJens Axboe 	link->last = req;
2324ed29b0b4SJens Axboe 	return 0;
2325ed29b0b4SJens Axboe }
2326ed29b0b4SJens Axboe 
2327ed29b0b4SJens Axboe static inline int io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req,
2328ed29b0b4SJens Axboe 			 const struct io_uring_sqe *sqe)
2329ed29b0b4SJens Axboe 	__must_hold(&ctx->uring_lock)
2330ed29b0b4SJens Axboe {
2331ed29b0b4SJens Axboe 	struct io_submit_link *link = &ctx->submit_state.link;
2332ed29b0b4SJens Axboe 	int ret;
2333ed29b0b4SJens Axboe 
2334ed29b0b4SJens Axboe 	ret = io_init_req(ctx, req, sqe);
2335ed29b0b4SJens Axboe 	if (unlikely(ret))
2336ed29b0b4SJens Axboe 		return io_submit_fail_init(sqe, req, ret);
2337ed29b0b4SJens Axboe 
23382ad57931SJens Axboe 	trace_io_uring_submit_req(req);
2339ed29b0b4SJens Axboe 
2340ed29b0b4SJens Axboe 	/*
2341ed29b0b4SJens Axboe 	 * If we already have a head request, queue this one for async
2342ed29b0b4SJens Axboe 	 * submittal once the head completes. If we don't have a head but
2343ed29b0b4SJens Axboe 	 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
2344ed29b0b4SJens Axboe 	 * submitted sync once the chain is complete. If none of those
2345ed29b0b4SJens Axboe 	 * conditions are true (normal request), then just queue it.
2346ed29b0b4SJens Axboe 	 */
2347ed29b0b4SJens Axboe 	if (unlikely(link->head)) {
2348ed29b0b4SJens Axboe 		ret = io_req_prep_async(req);
2349ed29b0b4SJens Axboe 		if (unlikely(ret))
2350ed29b0b4SJens Axboe 			return io_submit_fail_init(sqe, req, ret);
2351ed29b0b4SJens Axboe 
235248863ffdSPavel Begunkov 		trace_io_uring_link(req, link->head);
2353ed29b0b4SJens Axboe 		link->last->link = req;
2354ed29b0b4SJens Axboe 		link->last = req;
2355ed29b0b4SJens Axboe 
2356ed29b0b4SJens Axboe 		if (req->flags & IO_REQ_LINK_FLAGS)
2357ed29b0b4SJens Axboe 			return 0;
2358ed29b0b4SJens Axboe 		/* last request of the link, flush it */
2359ed29b0b4SJens Axboe 		req = link->head;
2360ed29b0b4SJens Axboe 		link->head = NULL;
2361ed29b0b4SJens Axboe 		if (req->flags & (REQ_F_FORCE_ASYNC | REQ_F_FAIL))
2362ed29b0b4SJens Axboe 			goto fallback;
2363ed29b0b4SJens Axboe 
2364ed29b0b4SJens Axboe 	} else if (unlikely(req->flags & (IO_REQ_LINK_FLAGS |
2365ed29b0b4SJens Axboe 					  REQ_F_FORCE_ASYNC | REQ_F_FAIL))) {
2366ed29b0b4SJens Axboe 		if (req->flags & IO_REQ_LINK_FLAGS) {
2367ed29b0b4SJens Axboe 			link->head = req;
2368ed29b0b4SJens Axboe 			link->last = req;
2369ed29b0b4SJens Axboe 		} else {
2370ed29b0b4SJens Axboe fallback:
2371ed29b0b4SJens Axboe 			io_queue_sqe_fallback(req);
2372ed29b0b4SJens Axboe 		}
2373ed29b0b4SJens Axboe 		return 0;
2374ed29b0b4SJens Axboe 	}
2375ed29b0b4SJens Axboe 
2376ed29b0b4SJens Axboe 	io_queue_sqe(req);
2377ed29b0b4SJens Axboe 	return 0;
2378ed29b0b4SJens Axboe }
2379ed29b0b4SJens Axboe 
2380ed29b0b4SJens Axboe /*
2381ed29b0b4SJens Axboe  * Batched submission is done, ensure local IO is flushed out.
2382ed29b0b4SJens Axboe  */
2383ed29b0b4SJens Axboe static void io_submit_state_end(struct io_ring_ctx *ctx)
2384ed29b0b4SJens Axboe {
2385ed29b0b4SJens Axboe 	struct io_submit_state *state = &ctx->submit_state;
2386ed29b0b4SJens Axboe 
2387ed29b0b4SJens Axboe 	if (unlikely(state->link.head))
2388ed29b0b4SJens Axboe 		io_queue_sqe_fallback(state->link.head);
2389ed29b0b4SJens Axboe 	/* flush only after queuing links as they can generate completions */
2390ed29b0b4SJens Axboe 	io_submit_flush_completions(ctx);
2391ed29b0b4SJens Axboe 	if (state->plug_started)
2392ed29b0b4SJens Axboe 		blk_finish_plug(&state->plug);
2393ed29b0b4SJens Axboe }
2394ed29b0b4SJens Axboe 
2395ed29b0b4SJens Axboe /*
2396ed29b0b4SJens Axboe  * Start submission side cache.
2397ed29b0b4SJens Axboe  */
2398ed29b0b4SJens Axboe static void io_submit_state_start(struct io_submit_state *state,
2399ed29b0b4SJens Axboe 				  unsigned int max_ios)
2400ed29b0b4SJens Axboe {
2401ed29b0b4SJens Axboe 	state->plug_started = false;
2402ed29b0b4SJens Axboe 	state->need_plug = max_ios > 2;
2403ed29b0b4SJens Axboe 	state->submit_nr = max_ios;
2404ed29b0b4SJens Axboe 	/* set only head, no need to init link_last in advance */
2405ed29b0b4SJens Axboe 	state->link.head = NULL;
2406ed29b0b4SJens Axboe }
2407ed29b0b4SJens Axboe 
2408ed29b0b4SJens Axboe static void io_commit_sqring(struct io_ring_ctx *ctx)
2409ed29b0b4SJens Axboe {
2410ed29b0b4SJens Axboe 	struct io_rings *rings = ctx->rings;
2411ed29b0b4SJens Axboe 
2412ed29b0b4SJens Axboe 	/*
2413ed29b0b4SJens Axboe 	 * Ensure any loads from the SQEs are done at this point,
2414ed29b0b4SJens Axboe 	 * since once we write the new head, the application could
2415ed29b0b4SJens Axboe 	 * write new data to them.
2416ed29b0b4SJens Axboe 	 */
2417ed29b0b4SJens Axboe 	smp_store_release(&rings->sq.head, ctx->cached_sq_head);
2418ed29b0b4SJens Axboe }
2419ed29b0b4SJens Axboe 
2420ed29b0b4SJens Axboe /*
2421ed29b0b4SJens Axboe  * Fetch an sqe, if one is available. Note this returns a pointer to memory
2422ed29b0b4SJens Axboe  * that is mapped by userspace. This means that care needs to be taken to
2423ed29b0b4SJens Axboe  * ensure that reads are stable, as we cannot rely on userspace always
2424ed29b0b4SJens Axboe  * being a good citizen. If members of the sqe are validated and then later
2425ed29b0b4SJens Axboe  * used, it's important that those reads are done through READ_ONCE() to
2426ed29b0b4SJens Axboe  * prevent a re-load down the line.
2427ed29b0b4SJens Axboe  */
2428b5083dfaSPavel Begunkov static bool io_get_sqe(struct io_ring_ctx *ctx, const struct io_uring_sqe **sqe)
2429ed29b0b4SJens Axboe {
2430ed29b0b4SJens Axboe 	unsigned head, mask = ctx->sq_entries - 1;
2431ed29b0b4SJens Axboe 	unsigned sq_idx = ctx->cached_sq_head++ & mask;
2432ed29b0b4SJens Axboe 
2433ed29b0b4SJens Axboe 	/*
2434ed29b0b4SJens Axboe 	 * The cached sq head (or cq tail) serves two purposes:
2435ed29b0b4SJens Axboe 	 *
2436ed29b0b4SJens Axboe 	 * 1) allows us to batch the cost of updating the user visible
2437ed29b0b4SJens Axboe 	 *    head updates.
2438ed29b0b4SJens Axboe 	 * 2) allows the kernel side to track the head on its own, even
2439ed29b0b4SJens Axboe 	 *    though the application is the one updating it.
2440ed29b0b4SJens Axboe 	 */
2441ed29b0b4SJens Axboe 	head = READ_ONCE(ctx->sq_array[sq_idx]);
2442ed29b0b4SJens Axboe 	if (likely(head < ctx->sq_entries)) {
2443ed29b0b4SJens Axboe 		/* double index for 128-byte SQEs, twice as long */
2444ed29b0b4SJens Axboe 		if (ctx->flags & IORING_SETUP_SQE128)
2445ed29b0b4SJens Axboe 			head <<= 1;
2446b5083dfaSPavel Begunkov 		*sqe = &ctx->sq_sqes[head];
2447b5083dfaSPavel Begunkov 		return true;
2448ed29b0b4SJens Axboe 	}
2449ed29b0b4SJens Axboe 
2450ed29b0b4SJens Axboe 	/* drop invalid entries */
2451ed29b0b4SJens Axboe 	ctx->cq_extra--;
2452ed29b0b4SJens Axboe 	WRITE_ONCE(ctx->rings->sq_dropped,
2453ed29b0b4SJens Axboe 		   READ_ONCE(ctx->rings->sq_dropped) + 1);
2454b5083dfaSPavel Begunkov 	return false;
2455ed29b0b4SJens Axboe }
2456ed29b0b4SJens Axboe 
245717437f31SJens Axboe int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr)
2458ed29b0b4SJens Axboe 	__must_hold(&ctx->uring_lock)
2459ed29b0b4SJens Axboe {
2460ed29b0b4SJens Axboe 	unsigned int entries = io_sqring_entries(ctx);
2461ed29b0b4SJens Axboe 	unsigned int left;
2462ed29b0b4SJens Axboe 	int ret;
2463ed29b0b4SJens Axboe 
2464ed29b0b4SJens Axboe 	if (unlikely(!entries))
2465ed29b0b4SJens Axboe 		return 0;
2466ed29b0b4SJens Axboe 	/* make sure SQ entry isn't read before tail */
2467e3ef728fSJens Axboe 	ret = left = min(nr, entries);
2468ed29b0b4SJens Axboe 	io_get_task_refs(left);
2469ed29b0b4SJens Axboe 	io_submit_state_start(&ctx->submit_state, left);
2470ed29b0b4SJens Axboe 
2471ed29b0b4SJens Axboe 	do {
2472ed29b0b4SJens Axboe 		const struct io_uring_sqe *sqe;
2473ed29b0b4SJens Axboe 		struct io_kiocb *req;
2474ed29b0b4SJens Axboe 
2475c8576f3eSPavel Begunkov 		if (unlikely(!io_alloc_req(ctx, &req)))
2476ed29b0b4SJens Axboe 			break;
2477b5083dfaSPavel Begunkov 		if (unlikely(!io_get_sqe(ctx, &sqe))) {
2478ed29b0b4SJens Axboe 			io_req_add_to_cache(req, ctx);
2479ed29b0b4SJens Axboe 			break;
2480ed29b0b4SJens Axboe 		}
2481ed29b0b4SJens Axboe 
2482ed29b0b4SJens Axboe 		/*
2483ed29b0b4SJens Axboe 		 * Continue submitting even for sqe failure if the
2484ed29b0b4SJens Axboe 		 * ring was setup with IORING_SETUP_SUBMIT_ALL
2485ed29b0b4SJens Axboe 		 */
2486ed29b0b4SJens Axboe 		if (unlikely(io_submit_sqe(ctx, req, sqe)) &&
2487ed29b0b4SJens Axboe 		    !(ctx->flags & IORING_SETUP_SUBMIT_ALL)) {
2488ed29b0b4SJens Axboe 			left--;
2489ed29b0b4SJens Axboe 			break;
2490ed29b0b4SJens Axboe 		}
2491ed29b0b4SJens Axboe 	} while (--left);
2492ed29b0b4SJens Axboe 
2493ed29b0b4SJens Axboe 	if (unlikely(left)) {
2494ed29b0b4SJens Axboe 		ret -= left;
2495ed29b0b4SJens Axboe 		/* try again if it submitted nothing and can't allocate a req */
2496ed29b0b4SJens Axboe 		if (!ret && io_req_cache_empty(ctx))
2497ed29b0b4SJens Axboe 			ret = -EAGAIN;
2498ed29b0b4SJens Axboe 		current->io_uring->cached_refs += left;
2499ed29b0b4SJens Axboe 	}
2500ed29b0b4SJens Axboe 
2501ed29b0b4SJens Axboe 	io_submit_state_end(ctx);
2502ed29b0b4SJens Axboe 	 /* Commit SQ ring head once we've consumed and submitted all SQEs */
2503ed29b0b4SJens Axboe 	io_commit_sqring(ctx);
2504ed29b0b4SJens Axboe 	return ret;
2505ed29b0b4SJens Axboe }
2506ed29b0b4SJens Axboe 
2507ed29b0b4SJens Axboe struct io_wait_queue {
2508ed29b0b4SJens Axboe 	struct wait_queue_entry wq;
2509ed29b0b4SJens Axboe 	struct io_ring_ctx *ctx;
2510ed29b0b4SJens Axboe 	unsigned cq_tail;
2511ed29b0b4SJens Axboe 	unsigned nr_timeouts;
2512d33a39e5SPavel Begunkov 	ktime_t timeout;
2513ed29b0b4SJens Axboe };
2514ed29b0b4SJens Axboe 
2515b4c98d59SDylan Yudaken static inline bool io_has_work(struct io_ring_ctx *ctx)
2516b4c98d59SDylan Yudaken {
2517c0e0d6baSDylan Yudaken 	return test_bit(IO_CHECK_CQ_OVERFLOW_BIT, &ctx->check_cq) ||
2518490c00ebSPavel Begunkov 	       !llist_empty(&ctx->work_llist);
2519b4c98d59SDylan Yudaken }
2520b4c98d59SDylan Yudaken 
2521ed29b0b4SJens Axboe static inline bool io_should_wake(struct io_wait_queue *iowq)
2522ed29b0b4SJens Axboe {
2523ed29b0b4SJens Axboe 	struct io_ring_ctx *ctx = iowq->ctx;
25240fc8c2acSDylan Yudaken 	int dist = READ_ONCE(ctx->rings->cq.tail) - (int) iowq->cq_tail;
2525ed29b0b4SJens Axboe 
2526ed29b0b4SJens Axboe 	/*
2527ed29b0b4SJens Axboe 	 * Wake up if we have enough events, or if a timeout occurred since we
2528ed29b0b4SJens Axboe 	 * started waiting. For timeouts, we always want to return to userspace,
2529ed29b0b4SJens Axboe 	 * regardless of event count.
2530ed29b0b4SJens Axboe 	 */
2531ed29b0b4SJens Axboe 	return dist >= 0 || atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
2532ed29b0b4SJens Axboe }
2533ed29b0b4SJens Axboe 
2534ed29b0b4SJens Axboe static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
2535ed29b0b4SJens Axboe 			    int wake_flags, void *key)
2536ed29b0b4SJens Axboe {
2537bd550173SPavel Begunkov 	struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue, wq);
2538ed29b0b4SJens Axboe 
2539ed29b0b4SJens Axboe 	/*
2540ed29b0b4SJens Axboe 	 * Cannot safely flush overflowed CQEs from here, ensure we wake up
2541ed29b0b4SJens Axboe 	 * the task, and the next invocation will do it.
2542ed29b0b4SJens Axboe 	 */
2543bd550173SPavel Begunkov 	if (io_should_wake(iowq) || io_has_work(iowq->ctx))
2544ed29b0b4SJens Axboe 		return autoremove_wake_function(curr, mode, wake_flags, key);
2545ed29b0b4SJens Axboe 	return -1;
2546ed29b0b4SJens Axboe }
2547ed29b0b4SJens Axboe 
2548c0e0d6baSDylan Yudaken int io_run_task_work_sig(struct io_ring_ctx *ctx)
2549ed29b0b4SJens Axboe {
25501414d629SPavel Begunkov 	if (!llist_empty(&ctx->work_llist)) {
25512f413956SPavel Begunkov 		__set_current_state(TASK_RUNNING);
25521414d629SPavel Begunkov 		if (io_run_local_work(ctx) > 0)
25531414d629SPavel Begunkov 			return 1;
25541414d629SPavel Begunkov 	}
25551414d629SPavel Begunkov 	if (io_run_task_work() > 0)
2556ed29b0b4SJens Axboe 		return 1;
2557ed29b0b4SJens Axboe 	if (task_sigpending(current))
2558ed29b0b4SJens Axboe 		return -EINTR;
2559ed29b0b4SJens Axboe 	return 0;
2560ed29b0b4SJens Axboe }
2561ed29b0b4SJens Axboe 
2562ed29b0b4SJens Axboe /* when returns >0, the caller should retry */
2563ed29b0b4SJens Axboe static inline int io_cqring_wait_schedule(struct io_ring_ctx *ctx,
2564d33a39e5SPavel Begunkov 					  struct io_wait_queue *iowq)
2565ed29b0b4SJens Axboe {
25663fcf19d5SPavel Begunkov 	if (unlikely(READ_ONCE(ctx->check_cq)))
25673fcf19d5SPavel Begunkov 		return 1;
2568846072f1SPavel Begunkov 	if (unlikely(!llist_empty(&ctx->work_llist)))
2569846072f1SPavel Begunkov 		return 1;
2570846072f1SPavel Begunkov 	if (unlikely(test_thread_flag(TIF_NOTIFY_SIGNAL)))
2571846072f1SPavel Begunkov 		return 1;
2572846072f1SPavel Begunkov 	if (unlikely(task_sigpending(current)))
2573846072f1SPavel Begunkov 		return -EINTR;
2574846072f1SPavel Begunkov 	if (unlikely(io_should_wake(iowq)))
2575846072f1SPavel Begunkov 		return 0;
2576d33a39e5SPavel Begunkov 	if (iowq->timeout == KTIME_MAX)
257746ae7eefSPavel Begunkov 		schedule();
2578d33a39e5SPavel Begunkov 	else if (!schedule_hrtimeout(&iowq->timeout, HRTIMER_MODE_ABS))
2579ed29b0b4SJens Axboe 		return -ETIME;
2580846072f1SPavel Begunkov 	return 0;
2581ed29b0b4SJens Axboe }
2582ed29b0b4SJens Axboe 
2583ed29b0b4SJens Axboe /*
2584ed29b0b4SJens Axboe  * Wait until events become available, if we don't already have some. The
2585ed29b0b4SJens Axboe  * application must reap them itself, as they reside on the shared cq ring.
2586ed29b0b4SJens Axboe  */
2587ed29b0b4SJens Axboe static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
2588ed29b0b4SJens Axboe 			  const sigset_t __user *sig, size_t sigsz,
2589ed29b0b4SJens Axboe 			  struct __kernel_timespec __user *uts)
2590ed29b0b4SJens Axboe {
2591ed29b0b4SJens Axboe 	struct io_wait_queue iowq;
2592ed29b0b4SJens Axboe 	struct io_rings *rings = ctx->rings;
2593ed29b0b4SJens Axboe 	int ret;
2594ed29b0b4SJens Axboe 
259576de6749SPavel Begunkov 	if (!io_allowed_run_tw(ctx))
259676de6749SPavel Begunkov 		return -EEXIST;
2597140102aeSPavel Begunkov 	if (!llist_empty(&ctx->work_llist))
2598140102aeSPavel Begunkov 		io_run_local_work(ctx);
2599f36ba6cfSPavel Begunkov 	io_run_task_work();
2600ed29b0b4SJens Axboe 	io_cqring_overflow_flush(ctx);
26010fc8c2acSDylan Yudaken 	/* if user messes with these they will just get an early return */
26020fc8c2acSDylan Yudaken 	if (__io_cqring_events_user(ctx) >= min_events)
2603ed29b0b4SJens Axboe 		return 0;
2604ed29b0b4SJens Axboe 
2605ed29b0b4SJens Axboe 	if (sig) {
2606ed29b0b4SJens Axboe #ifdef CONFIG_COMPAT
2607ed29b0b4SJens Axboe 		if (in_compat_syscall())
2608ed29b0b4SJens Axboe 			ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
2609ed29b0b4SJens Axboe 						      sigsz);
2610ed29b0b4SJens Axboe 		else
2611ed29b0b4SJens Axboe #endif
2612ed29b0b4SJens Axboe 			ret = set_user_sigmask(sig, sigsz);
2613ed29b0b4SJens Axboe 
2614ed29b0b4SJens Axboe 		if (ret)
2615ed29b0b4SJens Axboe 			return ret;
2616ed29b0b4SJens Axboe 	}
2617ed29b0b4SJens Axboe 
2618ed29b0b4SJens Axboe 	init_waitqueue_func_entry(&iowq.wq, io_wake_function);
2619ed29b0b4SJens Axboe 	iowq.wq.private = current;
2620ed29b0b4SJens Axboe 	INIT_LIST_HEAD(&iowq.wq.entry);
2621ed29b0b4SJens Axboe 	iowq.ctx = ctx;
2622ed29b0b4SJens Axboe 	iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
2623ed29b0b4SJens Axboe 	iowq.cq_tail = READ_ONCE(ctx->rings->cq.head) + min_events;
2624d33a39e5SPavel Begunkov 	iowq.timeout = KTIME_MAX;
2625d33a39e5SPavel Begunkov 
2626d33a39e5SPavel Begunkov 	if (uts) {
2627d33a39e5SPavel Begunkov 		struct timespec64 ts;
2628d33a39e5SPavel Begunkov 
2629d33a39e5SPavel Begunkov 		if (get_timespec64(&ts, uts))
2630d33a39e5SPavel Begunkov 			return -EFAULT;
2631d33a39e5SPavel Begunkov 		iowq.timeout = ktime_add_ns(timespec64_to_ktime(ts), ktime_get_ns());
2632d33a39e5SPavel Begunkov 	}
2633ed29b0b4SJens Axboe 
2634ed29b0b4SJens Axboe 	trace_io_uring_cqring_wait(ctx, min_events);
2635ed29b0b4SJens Axboe 	do {
26363fcf19d5SPavel Begunkov 		unsigned long check_cq;
26373fcf19d5SPavel Begunkov 
2638130bd686SPavel Begunkov 		if (ctx->flags & IORING_SETUP_DEFER_TASKRUN) {
26398751d154SPavel Begunkov 			int nr_wait = (int) iowq.cq_tail - READ_ONCE(ctx->rings->cq.tail);
26408751d154SPavel Begunkov 
26418751d154SPavel Begunkov 			atomic_set(&ctx->cq_wait_nr, nr_wait);
2642130bd686SPavel Begunkov 			set_current_state(TASK_INTERRUPTIBLE);
2643130bd686SPavel Begunkov 		} else {
2644ed29b0b4SJens Axboe 			prepare_to_wait_exclusive(&ctx->cq_wait, &iowq.wq,
2645ed29b0b4SJens Axboe 							TASK_INTERRUPTIBLE);
2646130bd686SPavel Begunkov 		}
2647130bd686SPavel Begunkov 
2648d33a39e5SPavel Begunkov 		ret = io_cqring_wait_schedule(ctx, &iowq);
2649130bd686SPavel Begunkov 		__set_current_state(TASK_RUNNING);
26508751d154SPavel Begunkov 		atomic_set(&ctx->cq_wait_nr, 0);
2651d80c0f00SPavel Begunkov 
2652846072f1SPavel Begunkov 		if (ret < 0)
2653846072f1SPavel Begunkov 			break;
2654846072f1SPavel Begunkov 		/*
2655846072f1SPavel Begunkov 		 * Run task_work after scheduling and before io_should_wake().
2656846072f1SPavel Begunkov 		 * If we got woken because of task_work being processed, run it
2657846072f1SPavel Begunkov 		 * now rather than let the caller do another wait loop.
2658846072f1SPavel Begunkov 		 */
2659846072f1SPavel Begunkov 		io_run_task_work();
2660846072f1SPavel Begunkov 		if (!llist_empty(&ctx->work_llist))
2661846072f1SPavel Begunkov 			io_run_local_work(ctx);
26623fcf19d5SPavel Begunkov 
26633fcf19d5SPavel Begunkov 		check_cq = READ_ONCE(ctx->check_cq);
26643fcf19d5SPavel Begunkov 		if (unlikely(check_cq)) {
26653fcf19d5SPavel Begunkov 			/* let the caller flush overflows, retry */
2666326a9e48SPavel Begunkov 			if (check_cq & BIT(IO_CHECK_CQ_OVERFLOW_BIT))
26673fcf19d5SPavel Begunkov 				io_cqring_do_overflow_flush(ctx);
26683fcf19d5SPavel Begunkov 			if (check_cq & BIT(IO_CHECK_CQ_DROPPED_BIT)) {
26693fcf19d5SPavel Begunkov 				ret = -EBADR;
26703fcf19d5SPavel Begunkov 				break;
26713fcf19d5SPavel Begunkov 			}
26723fcf19d5SPavel Begunkov 		}
26733fcf19d5SPavel Begunkov 
2674846072f1SPavel Begunkov 		if (io_should_wake(&iowq)) {
2675846072f1SPavel Begunkov 			ret = 0;
267635d90f95SJens Axboe 			break;
2677846072f1SPavel Begunkov 		}
2678ed29b0b4SJens Axboe 		cond_resched();
2679846072f1SPavel Begunkov 	} while (1);
2680ed29b0b4SJens Axboe 
2681130bd686SPavel Begunkov 	if (!(ctx->flags & IORING_SETUP_DEFER_TASKRUN))
2682ed29b0b4SJens Axboe 		finish_wait(&ctx->cq_wait, &iowq.wq);
2683ed29b0b4SJens Axboe 	restore_saved_sigmask_unless(ret == -EINTR);
2684ed29b0b4SJens Axboe 
2685ed29b0b4SJens Axboe 	return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
2686ed29b0b4SJens Axboe }
2687ed29b0b4SJens Axboe 
2688ed29b0b4SJens Axboe static void io_mem_free(void *ptr)
2689ed29b0b4SJens Axboe {
2690ed29b0b4SJens Axboe 	struct page *page;
2691ed29b0b4SJens Axboe 
2692ed29b0b4SJens Axboe 	if (!ptr)
2693ed29b0b4SJens Axboe 		return;
2694ed29b0b4SJens Axboe 
2695ed29b0b4SJens Axboe 	page = virt_to_head_page(ptr);
2696ed29b0b4SJens Axboe 	if (put_page_testzero(page))
2697ed29b0b4SJens Axboe 		free_compound_page(page);
2698ed29b0b4SJens Axboe }
2699ed29b0b4SJens Axboe 
270003d89a2dSJens Axboe static void io_pages_free(struct page ***pages, int npages)
270103d89a2dSJens Axboe {
270203d89a2dSJens Axboe 	struct page **page_array;
270303d89a2dSJens Axboe 	int i;
270403d89a2dSJens Axboe 
270503d89a2dSJens Axboe 	if (!pages)
270603d89a2dSJens Axboe 		return;
270703d89a2dSJens Axboe 	page_array = *pages;
270803d89a2dSJens Axboe 	for (i = 0; i < npages; i++)
270903d89a2dSJens Axboe 		unpin_user_page(page_array[i]);
271003d89a2dSJens Axboe 	kvfree(page_array);
271103d89a2dSJens Axboe 	*pages = NULL;
271203d89a2dSJens Axboe }
271303d89a2dSJens Axboe 
271403d89a2dSJens Axboe static void *__io_uaddr_map(struct page ***pages, unsigned short *npages,
271503d89a2dSJens Axboe 			    unsigned long uaddr, size_t size)
271603d89a2dSJens Axboe {
271703d89a2dSJens Axboe 	struct page **page_array;
271803d89a2dSJens Axboe 	unsigned int nr_pages;
271903d89a2dSJens Axboe 	int ret;
272003d89a2dSJens Axboe 
272103d89a2dSJens Axboe 	*npages = 0;
272203d89a2dSJens Axboe 
272303d89a2dSJens Axboe 	if (uaddr & (PAGE_SIZE - 1) || !size)
272403d89a2dSJens Axboe 		return ERR_PTR(-EINVAL);
272503d89a2dSJens Axboe 
272603d89a2dSJens Axboe 	nr_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
272703d89a2dSJens Axboe 	if (nr_pages > USHRT_MAX)
272803d89a2dSJens Axboe 		return ERR_PTR(-EINVAL);
272903d89a2dSJens Axboe 	page_array = kvmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL);
273003d89a2dSJens Axboe 	if (!page_array)
273103d89a2dSJens Axboe 		return ERR_PTR(-ENOMEM);
273203d89a2dSJens Axboe 
273303d89a2dSJens Axboe 	ret = pin_user_pages_fast(uaddr, nr_pages, FOLL_WRITE | FOLL_LONGTERM,
273403d89a2dSJens Axboe 					page_array);
273503d89a2dSJens Axboe 	if (ret != nr_pages) {
273603d89a2dSJens Axboe err:
273703d89a2dSJens Axboe 		io_pages_free(&page_array, ret > 0 ? ret : 0);
273803d89a2dSJens Axboe 		return ret < 0 ? ERR_PTR(ret) : ERR_PTR(-EFAULT);
273903d89a2dSJens Axboe 	}
274003d89a2dSJens Axboe 	/*
274103d89a2dSJens Axboe 	 * Should be a single page. If the ring is small enough that we can
274203d89a2dSJens Axboe 	 * use a normal page, that is fine. If we need multiple pages, then
274303d89a2dSJens Axboe 	 * userspace should use a huge page. That's the only way to guarantee
274403d89a2dSJens Axboe 	 * that we get contigious memory, outside of just being lucky or
274503d89a2dSJens Axboe 	 * (currently) having low memory fragmentation.
274603d89a2dSJens Axboe 	 */
274703d89a2dSJens Axboe 	if (page_array[0] != page_array[ret - 1])
274803d89a2dSJens Axboe 		goto err;
274903d89a2dSJens Axboe 	*pages = page_array;
275003d89a2dSJens Axboe 	*npages = nr_pages;
275103d89a2dSJens Axboe 	return page_to_virt(page_array[0]);
275203d89a2dSJens Axboe }
275303d89a2dSJens Axboe 
275403d89a2dSJens Axboe static void *io_rings_map(struct io_ring_ctx *ctx, unsigned long uaddr,
275503d89a2dSJens Axboe 			  size_t size)
275603d89a2dSJens Axboe {
275703d89a2dSJens Axboe 	return __io_uaddr_map(&ctx->ring_pages, &ctx->n_ring_pages, uaddr,
275803d89a2dSJens Axboe 				size);
275903d89a2dSJens Axboe }
276003d89a2dSJens Axboe 
276103d89a2dSJens Axboe static void *io_sqes_map(struct io_ring_ctx *ctx, unsigned long uaddr,
276203d89a2dSJens Axboe 			 size_t size)
276303d89a2dSJens Axboe {
276403d89a2dSJens Axboe 	return __io_uaddr_map(&ctx->sqe_pages, &ctx->n_sqe_pages, uaddr,
276503d89a2dSJens Axboe 				size);
276603d89a2dSJens Axboe }
276703d89a2dSJens Axboe 
27689c189eeeSJens Axboe static void io_rings_free(struct io_ring_ctx *ctx)
27699c189eeeSJens Axboe {
277003d89a2dSJens Axboe 	if (!(ctx->flags & IORING_SETUP_NO_MMAP)) {
27719c189eeeSJens Axboe 		io_mem_free(ctx->rings);
27729c189eeeSJens Axboe 		io_mem_free(ctx->sq_sqes);
27739c189eeeSJens Axboe 		ctx->rings = NULL;
27749c189eeeSJens Axboe 		ctx->sq_sqes = NULL;
277503d89a2dSJens Axboe 	} else {
277603d89a2dSJens Axboe 		io_pages_free(&ctx->ring_pages, ctx->n_ring_pages);
277703d89a2dSJens Axboe 		io_pages_free(&ctx->sqe_pages, ctx->n_sqe_pages);
277803d89a2dSJens Axboe 	}
27799c189eeeSJens Axboe }
27809c189eeeSJens Axboe 
2781ed29b0b4SJens Axboe static void *io_mem_alloc(size_t size)
2782ed29b0b4SJens Axboe {
2783ed29b0b4SJens Axboe 	gfp_t gfp = GFP_KERNEL_ACCOUNT | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP;
2784e27cef86SJens Axboe 	void *ret;
2785ed29b0b4SJens Axboe 
2786e27cef86SJens Axboe 	ret = (void *) __get_free_pages(gfp, get_order(size));
2787e27cef86SJens Axboe 	if (ret)
2788e27cef86SJens Axboe 		return ret;
2789e27cef86SJens Axboe 	return ERR_PTR(-ENOMEM);
2790ed29b0b4SJens Axboe }
2791ed29b0b4SJens Axboe 
2792ed29b0b4SJens Axboe static unsigned long rings_size(struct io_ring_ctx *ctx, unsigned int sq_entries,
2793ed29b0b4SJens Axboe 				unsigned int cq_entries, size_t *sq_offset)
2794ed29b0b4SJens Axboe {
2795ed29b0b4SJens Axboe 	struct io_rings *rings;
2796ed29b0b4SJens Axboe 	size_t off, sq_array_size;
2797ed29b0b4SJens Axboe 
2798ed29b0b4SJens Axboe 	off = struct_size(rings, cqes, cq_entries);
2799ed29b0b4SJens Axboe 	if (off == SIZE_MAX)
2800ed29b0b4SJens Axboe 		return SIZE_MAX;
2801ed29b0b4SJens Axboe 	if (ctx->flags & IORING_SETUP_CQE32) {
2802ed29b0b4SJens Axboe 		if (check_shl_overflow(off, 1, &off))
2803ed29b0b4SJens Axboe 			return SIZE_MAX;
2804ed29b0b4SJens Axboe 	}
2805ed29b0b4SJens Axboe 
2806ed29b0b4SJens Axboe #ifdef CONFIG_SMP
2807ed29b0b4SJens Axboe 	off = ALIGN(off, SMP_CACHE_BYTES);
2808ed29b0b4SJens Axboe 	if (off == 0)
2809ed29b0b4SJens Axboe 		return SIZE_MAX;
2810ed29b0b4SJens Axboe #endif
2811ed29b0b4SJens Axboe 
2812ed29b0b4SJens Axboe 	if (sq_offset)
2813ed29b0b4SJens Axboe 		*sq_offset = off;
2814ed29b0b4SJens Axboe 
2815ed29b0b4SJens Axboe 	sq_array_size = array_size(sizeof(u32), sq_entries);
2816ed29b0b4SJens Axboe 	if (sq_array_size == SIZE_MAX)
2817ed29b0b4SJens Axboe 		return SIZE_MAX;
2818ed29b0b4SJens Axboe 
2819ed29b0b4SJens Axboe 	if (check_add_overflow(off, sq_array_size, &off))
2820ed29b0b4SJens Axboe 		return SIZE_MAX;
2821ed29b0b4SJens Axboe 
2822ed29b0b4SJens Axboe 	return off;
2823ed29b0b4SJens Axboe }
2824ed29b0b4SJens Axboe 
2825ed29b0b4SJens Axboe static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg,
2826ed29b0b4SJens Axboe 			       unsigned int eventfd_async)
2827ed29b0b4SJens Axboe {
2828ed29b0b4SJens Axboe 	struct io_ev_fd *ev_fd;
2829ed29b0b4SJens Axboe 	__s32 __user *fds = arg;
2830ed29b0b4SJens Axboe 	int fd;
2831ed29b0b4SJens Axboe 
2832ed29b0b4SJens Axboe 	ev_fd = rcu_dereference_protected(ctx->io_ev_fd,
2833ed29b0b4SJens Axboe 					lockdep_is_held(&ctx->uring_lock));
2834ed29b0b4SJens Axboe 	if (ev_fd)
2835ed29b0b4SJens Axboe 		return -EBUSY;
2836ed29b0b4SJens Axboe 
2837ed29b0b4SJens Axboe 	if (copy_from_user(&fd, fds, sizeof(*fds)))
2838ed29b0b4SJens Axboe 		return -EFAULT;
2839ed29b0b4SJens Axboe 
2840ed29b0b4SJens Axboe 	ev_fd = kmalloc(sizeof(*ev_fd), GFP_KERNEL);
2841ed29b0b4SJens Axboe 	if (!ev_fd)
2842ed29b0b4SJens Axboe 		return -ENOMEM;
2843ed29b0b4SJens Axboe 
2844ed29b0b4SJens Axboe 	ev_fd->cq_ev_fd = eventfd_ctx_fdget(fd);
2845ed29b0b4SJens Axboe 	if (IS_ERR(ev_fd->cq_ev_fd)) {
2846ed29b0b4SJens Axboe 		int ret = PTR_ERR(ev_fd->cq_ev_fd);
2847ed29b0b4SJens Axboe 		kfree(ev_fd);
2848ed29b0b4SJens Axboe 		return ret;
2849ed29b0b4SJens Axboe 	}
2850305bef98SPavel Begunkov 
2851305bef98SPavel Begunkov 	spin_lock(&ctx->completion_lock);
2852305bef98SPavel Begunkov 	ctx->evfd_last_cq_tail = ctx->cached_cq_tail;
2853305bef98SPavel Begunkov 	spin_unlock(&ctx->completion_lock);
2854305bef98SPavel Begunkov 
2855ed29b0b4SJens Axboe 	ev_fd->eventfd_async = eventfd_async;
2856ed29b0b4SJens Axboe 	ctx->has_evfd = true;
2857ed29b0b4SJens Axboe 	rcu_assign_pointer(ctx->io_ev_fd, ev_fd);
285821a091b9SDylan Yudaken 	atomic_set(&ev_fd->refs, 1);
285921a091b9SDylan Yudaken 	atomic_set(&ev_fd->ops, 0);
2860ed29b0b4SJens Axboe 	return 0;
2861ed29b0b4SJens Axboe }
2862ed29b0b4SJens Axboe 
2863ed29b0b4SJens Axboe static int io_eventfd_unregister(struct io_ring_ctx *ctx)
2864ed29b0b4SJens Axboe {
2865ed29b0b4SJens Axboe 	struct io_ev_fd *ev_fd;
2866ed29b0b4SJens Axboe 
2867ed29b0b4SJens Axboe 	ev_fd = rcu_dereference_protected(ctx->io_ev_fd,
2868ed29b0b4SJens Axboe 					lockdep_is_held(&ctx->uring_lock));
2869ed29b0b4SJens Axboe 	if (ev_fd) {
2870ed29b0b4SJens Axboe 		ctx->has_evfd = false;
2871ed29b0b4SJens Axboe 		rcu_assign_pointer(ctx->io_ev_fd, NULL);
287221a091b9SDylan Yudaken 		if (!atomic_fetch_or(BIT(IO_EVENTFD_OP_FREE_BIT), &ev_fd->ops))
287321a091b9SDylan Yudaken 			call_rcu(&ev_fd->rcu, io_eventfd_ops);
2874ed29b0b4SJens Axboe 		return 0;
2875ed29b0b4SJens Axboe 	}
2876ed29b0b4SJens Axboe 
2877ed29b0b4SJens Axboe 	return -ENXIO;
2878ed29b0b4SJens Axboe }
2879ed29b0b4SJens Axboe 
2880ed29b0b4SJens Axboe static void io_req_caches_free(struct io_ring_ctx *ctx)
2881ed29b0b4SJens Axboe {
2882c8576f3eSPavel Begunkov 	struct io_kiocb *req;
2883ed29b0b4SJens Axboe 	int nr = 0;
2884ed29b0b4SJens Axboe 
2885ed29b0b4SJens Axboe 	mutex_lock(&ctx->uring_lock);
288634f0bc42SPavel Begunkov 	io_flush_cached_locked_reqs(ctx, &ctx->submit_state);
2887ed29b0b4SJens Axboe 
2888ed29b0b4SJens Axboe 	while (!io_req_cache_empty(ctx)) {
2889c8576f3eSPavel Begunkov 		req = io_extract_req(ctx);
2890ed29b0b4SJens Axboe 		kmem_cache_free(req_cachep, req);
2891ed29b0b4SJens Axboe 		nr++;
2892ed29b0b4SJens Axboe 	}
2893ed29b0b4SJens Axboe 	if (nr)
2894ed29b0b4SJens Axboe 		percpu_ref_put_many(&ctx->refs, nr);
2895ed29b0b4SJens Axboe 	mutex_unlock(&ctx->uring_lock);
2896ed29b0b4SJens Axboe }
2897ed29b0b4SJens Axboe 
28989eae8655SPavel Begunkov static void io_rsrc_node_cache_free(struct io_cache_entry *entry)
28999eae8655SPavel Begunkov {
29009eae8655SPavel Begunkov 	kfree(container_of(entry, struct io_rsrc_node, cache));
29019eae8655SPavel Begunkov }
29029eae8655SPavel Begunkov 
2903ed29b0b4SJens Axboe static __cold void io_ring_ctx_free(struct io_ring_ctx *ctx)
2904ed29b0b4SJens Axboe {
2905ed29b0b4SJens Axboe 	io_sq_thread_finish(ctx);
2906ed29b0b4SJens Axboe 	/* __io_rsrc_put_work() may need uring_lock to progress, wait w/o it */
29070b222eebSPavel Begunkov 	if (WARN_ON_ONCE(!list_empty(&ctx->rsrc_ref_list)))
29080b222eebSPavel Begunkov 		return;
2909ed29b0b4SJens Axboe 
2910ed29b0b4SJens Axboe 	mutex_lock(&ctx->uring_lock);
2911ed29b0b4SJens Axboe 	if (ctx->buf_data)
2912ed29b0b4SJens Axboe 		__io_sqe_buffers_unregister(ctx);
2913ed29b0b4SJens Axboe 	if (ctx->file_data)
2914ed29b0b4SJens Axboe 		__io_sqe_files_unregister(ctx);
2915a85381d8SPavel Begunkov 	io_cqring_overflow_kill(ctx);
2916ed29b0b4SJens Axboe 	io_eventfd_unregister(ctx);
29179b797a37SJens Axboe 	io_alloc_cache_free(&ctx->apoll_cache, io_apoll_cache_free);
291843e0bbbdSJens Axboe 	io_alloc_cache_free(&ctx->netmsg_cache, io_netmsg_cache_free);
2919ed29b0b4SJens Axboe 	io_destroy_buffers(ctx);
2920b4a72c05SWojciech Lukowicz 	mutex_unlock(&ctx->uring_lock);
2921ed29b0b4SJens Axboe 	if (ctx->sq_creds)
2922ed29b0b4SJens Axboe 		put_cred(ctx->sq_creds);
292397bbdc06SPavel Begunkov 	if (ctx->submitter_task)
292497bbdc06SPavel Begunkov 		put_task_struct(ctx->submitter_task);
2925ed29b0b4SJens Axboe 
2926ed29b0b4SJens Axboe 	/* there are no registered resources left, nobody uses it */
2927ed29b0b4SJens Axboe 	if (ctx->rsrc_node)
29289eae8655SPavel Begunkov 		io_rsrc_node_destroy(ctx, ctx->rsrc_node);
2929ed29b0b4SJens Axboe 
2930ed29b0b4SJens Axboe 	WARN_ON_ONCE(!list_empty(&ctx->rsrc_ref_list));
2931ed29b0b4SJens Axboe 
2932ed29b0b4SJens Axboe #if defined(CONFIG_UNIX)
2933ed29b0b4SJens Axboe 	if (ctx->ring_sock) {
2934ed29b0b4SJens Axboe 		ctx->ring_sock->file = NULL; /* so that iput() is called */
2935ed29b0b4SJens Axboe 		sock_release(ctx->ring_sock);
2936ed29b0b4SJens Axboe 	}
2937ed29b0b4SJens Axboe #endif
2938ed29b0b4SJens Axboe 	WARN_ON_ONCE(!list_empty(&ctx->ltimeout_list));
2939ed29b0b4SJens Axboe 
29409eae8655SPavel Begunkov 	io_alloc_cache_free(&ctx->rsrc_node_cache, io_rsrc_node_cache_free);
294142b6419dSPavel Begunkov 	if (ctx->mm_account) {
294242b6419dSPavel Begunkov 		mmdrop(ctx->mm_account);
294342b6419dSPavel Begunkov 		ctx->mm_account = NULL;
294442b6419dSPavel Begunkov 	}
29459c189eeeSJens Axboe 	io_rings_free(ctx);
2946ed29b0b4SJens Axboe 
2947ed29b0b4SJens Axboe 	percpu_ref_exit(&ctx->refs);
2948ed29b0b4SJens Axboe 	free_uid(ctx->user);
2949ed29b0b4SJens Axboe 	io_req_caches_free(ctx);
2950ed29b0b4SJens Axboe 	if (ctx->hash_map)
2951ed29b0b4SJens Axboe 		io_wq_put_hash(ctx->hash_map);
2952e6f89be6SPavel Begunkov 	kfree(ctx->cancel_table.hbs);
29539ca9fb24SPavel Begunkov 	kfree(ctx->cancel_table_locked.hbs);
2954ed29b0b4SJens Axboe 	kfree(ctx->dummy_ubuf);
2955ed29b0b4SJens Axboe 	kfree(ctx->io_bl);
2956ed29b0b4SJens Axboe 	xa_destroy(&ctx->io_bl_xa);
2957ed29b0b4SJens Axboe 	kfree(ctx);
2958ed29b0b4SJens Axboe }
2959ed29b0b4SJens Axboe 
2960bca39f39SPavel Begunkov static __cold void io_activate_pollwq_cb(struct callback_head *cb)
2961bca39f39SPavel Begunkov {
2962bca39f39SPavel Begunkov 	struct io_ring_ctx *ctx = container_of(cb, struct io_ring_ctx,
2963bca39f39SPavel Begunkov 					       poll_wq_task_work);
2964bca39f39SPavel Begunkov 
2965bca39f39SPavel Begunkov 	mutex_lock(&ctx->uring_lock);
2966bca39f39SPavel Begunkov 	ctx->poll_activated = true;
2967bca39f39SPavel Begunkov 	mutex_unlock(&ctx->uring_lock);
2968bca39f39SPavel Begunkov 
2969bca39f39SPavel Begunkov 	/*
2970bca39f39SPavel Begunkov 	 * Wake ups for some events between start of polling and activation
2971bca39f39SPavel Begunkov 	 * might've been lost due to loose synchronisation.
2972bca39f39SPavel Begunkov 	 */
2973bca39f39SPavel Begunkov 	wake_up_all(&ctx->poll_wq);
2974bca39f39SPavel Begunkov 	percpu_ref_put(&ctx->refs);
2975bca39f39SPavel Begunkov }
2976bca39f39SPavel Begunkov 
2977bca39f39SPavel Begunkov static __cold void io_activate_pollwq(struct io_ring_ctx *ctx)
2978bca39f39SPavel Begunkov {
2979bca39f39SPavel Begunkov 	spin_lock(&ctx->completion_lock);
2980bca39f39SPavel Begunkov 	/* already activated or in progress */
2981bca39f39SPavel Begunkov 	if (ctx->poll_activated || ctx->poll_wq_task_work.func)
2982bca39f39SPavel Begunkov 		goto out;
2983bca39f39SPavel Begunkov 	if (WARN_ON_ONCE(!ctx->task_complete))
2984bca39f39SPavel Begunkov 		goto out;
2985bca39f39SPavel Begunkov 	if (!ctx->submitter_task)
2986bca39f39SPavel Begunkov 		goto out;
2987bca39f39SPavel Begunkov 	/*
2988bca39f39SPavel Begunkov 	 * with ->submitter_task only the submitter task completes requests, we
2989bca39f39SPavel Begunkov 	 * only need to sync with it, which is done by injecting a tw
2990bca39f39SPavel Begunkov 	 */
2991bca39f39SPavel Begunkov 	init_task_work(&ctx->poll_wq_task_work, io_activate_pollwq_cb);
2992bca39f39SPavel Begunkov 	percpu_ref_get(&ctx->refs);
2993bca39f39SPavel Begunkov 	if (task_work_add(ctx->submitter_task, &ctx->poll_wq_task_work, TWA_SIGNAL))
2994bca39f39SPavel Begunkov 		percpu_ref_put(&ctx->refs);
2995bca39f39SPavel Begunkov out:
2996bca39f39SPavel Begunkov 	spin_unlock(&ctx->completion_lock);
2997bca39f39SPavel Begunkov }
2998bca39f39SPavel Begunkov 
2999ed29b0b4SJens Axboe static __poll_t io_uring_poll(struct file *file, poll_table *wait)
3000ed29b0b4SJens Axboe {
3001ed29b0b4SJens Axboe 	struct io_ring_ctx *ctx = file->private_data;
3002ed29b0b4SJens Axboe 	__poll_t mask = 0;
3003ed29b0b4SJens Axboe 
3004bca39f39SPavel Begunkov 	if (unlikely(!ctx->poll_activated))
3005bca39f39SPavel Begunkov 		io_activate_pollwq(ctx);
3006bca39f39SPavel Begunkov 
30077b235dd8SPavel Begunkov 	poll_wait(file, &ctx->poll_wq, wait);
3008ed29b0b4SJens Axboe 	/*
3009ed29b0b4SJens Axboe 	 * synchronizes with barrier from wq_has_sleeper call in
3010ed29b0b4SJens Axboe 	 * io_commit_cqring
3011ed29b0b4SJens Axboe 	 */
3012ed29b0b4SJens Axboe 	smp_rmb();
3013ed29b0b4SJens Axboe 	if (!io_sqring_full(ctx))
3014ed29b0b4SJens Axboe 		mask |= EPOLLOUT | EPOLLWRNORM;
3015ed29b0b4SJens Axboe 
3016ed29b0b4SJens Axboe 	/*
3017ed29b0b4SJens Axboe 	 * Don't flush cqring overflow list here, just do a simple check.
3018ed29b0b4SJens Axboe 	 * Otherwise there could possible be ABBA deadlock:
3019ed29b0b4SJens Axboe 	 *      CPU0                    CPU1
3020ed29b0b4SJens Axboe 	 *      ----                    ----
3021ed29b0b4SJens Axboe 	 * lock(&ctx->uring_lock);
3022ed29b0b4SJens Axboe 	 *                              lock(&ep->mtx);
3023ed29b0b4SJens Axboe 	 *                              lock(&ctx->uring_lock);
3024ed29b0b4SJens Axboe 	 * lock(&ep->mtx);
3025ed29b0b4SJens Axboe 	 *
3026ed29b0b4SJens Axboe 	 * Users may get EPOLLIN meanwhile seeing nothing in cqring, this
302710d8bc35SDylan Yudaken 	 * pushes them to do the flush.
3028ed29b0b4SJens Axboe 	 */
3029b4c98d59SDylan Yudaken 
3030c10bb646SPavel Begunkov 	if (__io_cqring_events_user(ctx) || io_has_work(ctx))
3031ed29b0b4SJens Axboe 		mask |= EPOLLIN | EPOLLRDNORM;
3032ed29b0b4SJens Axboe 
3033ed29b0b4SJens Axboe 	return mask;
3034ed29b0b4SJens Axboe }
3035ed29b0b4SJens Axboe 
3036ed29b0b4SJens Axboe static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
3037ed29b0b4SJens Axboe {
3038ed29b0b4SJens Axboe 	const struct cred *creds;
3039ed29b0b4SJens Axboe 
3040ed29b0b4SJens Axboe 	creds = xa_erase(&ctx->personalities, id);
3041ed29b0b4SJens Axboe 	if (creds) {
3042ed29b0b4SJens Axboe 		put_cred(creds);
3043ed29b0b4SJens Axboe 		return 0;
3044ed29b0b4SJens Axboe 	}
3045ed29b0b4SJens Axboe 
3046ed29b0b4SJens Axboe 	return -EINVAL;
3047ed29b0b4SJens Axboe }
3048ed29b0b4SJens Axboe 
3049ed29b0b4SJens Axboe struct io_tctx_exit {
3050ed29b0b4SJens Axboe 	struct callback_head		task_work;
3051ed29b0b4SJens Axboe 	struct completion		completion;
3052ed29b0b4SJens Axboe 	struct io_ring_ctx		*ctx;
3053ed29b0b4SJens Axboe };
3054ed29b0b4SJens Axboe 
3055ed29b0b4SJens Axboe static __cold void io_tctx_exit_cb(struct callback_head *cb)
3056ed29b0b4SJens Axboe {
3057ed29b0b4SJens Axboe 	struct io_uring_task *tctx = current->io_uring;
3058ed29b0b4SJens Axboe 	struct io_tctx_exit *work;
3059ed29b0b4SJens Axboe 
3060ed29b0b4SJens Axboe 	work = container_of(cb, struct io_tctx_exit, task_work);
3061ed29b0b4SJens Axboe 	/*
30628d664282SJens Axboe 	 * When @in_cancel, we're in cancellation and it's racy to remove the
3063ed29b0b4SJens Axboe 	 * node. It'll be removed by the end of cancellation, just ignore it.
3064998b30c3SHarshit Mogalapalli 	 * tctx can be NULL if the queueing of this task_work raced with
3065998b30c3SHarshit Mogalapalli 	 * work cancelation off the exec path.
3066ed29b0b4SJens Axboe 	 */
30678d664282SJens Axboe 	if (tctx && !atomic_read(&tctx->in_cancel))
3068ed29b0b4SJens Axboe 		io_uring_del_tctx_node((unsigned long)work->ctx);
3069ed29b0b4SJens Axboe 	complete(&work->completion);
3070ed29b0b4SJens Axboe }
3071ed29b0b4SJens Axboe 
3072ed29b0b4SJens Axboe static __cold bool io_cancel_ctx_cb(struct io_wq_work *work, void *data)
3073ed29b0b4SJens Axboe {
3074ed29b0b4SJens Axboe 	struct io_kiocb *req = container_of(work, struct io_kiocb, work);
3075ed29b0b4SJens Axboe 
3076ed29b0b4SJens Axboe 	return req->ctx == data;
3077ed29b0b4SJens Axboe }
3078ed29b0b4SJens Axboe 
3079ed29b0b4SJens Axboe static __cold void io_ring_exit_work(struct work_struct *work)
3080ed29b0b4SJens Axboe {
3081ed29b0b4SJens Axboe 	struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx, exit_work);
3082ed29b0b4SJens Axboe 	unsigned long timeout = jiffies + HZ * 60 * 5;
3083ed29b0b4SJens Axboe 	unsigned long interval = HZ / 20;
3084ed29b0b4SJens Axboe 	struct io_tctx_exit exit;
3085ed29b0b4SJens Axboe 	struct io_tctx_node *node;
3086ed29b0b4SJens Axboe 	int ret;
3087ed29b0b4SJens Axboe 
3088ed29b0b4SJens Axboe 	/*
3089ed29b0b4SJens Axboe 	 * If we're doing polled IO and end up having requests being
3090ed29b0b4SJens Axboe 	 * submitted async (out-of-line), then completions can come in while
3091ed29b0b4SJens Axboe 	 * we're waiting for refs to drop. We need to reap these manually,
3092ed29b0b4SJens Axboe 	 * as nobody else will be looking for them.
3093ed29b0b4SJens Axboe 	 */
3094ed29b0b4SJens Axboe 	do {
3095a85381d8SPavel Begunkov 		if (test_bit(IO_CHECK_CQ_OVERFLOW_BIT, &ctx->check_cq)) {
3096a85381d8SPavel Begunkov 			mutex_lock(&ctx->uring_lock);
3097a85381d8SPavel Begunkov 			io_cqring_overflow_kill(ctx);
3098a85381d8SPavel Begunkov 			mutex_unlock(&ctx->uring_lock);
3099a85381d8SPavel Begunkov 		}
3100a85381d8SPavel Begunkov 
3101c0e0d6baSDylan Yudaken 		if (ctx->flags & IORING_SETUP_DEFER_TASKRUN)
3102c0e0d6baSDylan Yudaken 			io_move_task_work_from_local(ctx);
3103c0e0d6baSDylan Yudaken 
3104affa87dbSPavel Begunkov 		while (io_uring_try_cancel_requests(ctx, NULL, true))
3105affa87dbSPavel Begunkov 			cond_resched();
3106affa87dbSPavel Begunkov 
3107ed29b0b4SJens Axboe 		if (ctx->sq_data) {
3108ed29b0b4SJens Axboe 			struct io_sq_data *sqd = ctx->sq_data;
3109ed29b0b4SJens Axboe 			struct task_struct *tsk;
3110ed29b0b4SJens Axboe 
3111ed29b0b4SJens Axboe 			io_sq_thread_park(sqd);
3112ed29b0b4SJens Axboe 			tsk = sqd->thread;
3113ed29b0b4SJens Axboe 			if (tsk && tsk->io_uring && tsk->io_uring->io_wq)
3114ed29b0b4SJens Axboe 				io_wq_cancel_cb(tsk->io_uring->io_wq,
3115ed29b0b4SJens Axboe 						io_cancel_ctx_cb, ctx, true);
3116ed29b0b4SJens Axboe 			io_sq_thread_unpark(sqd);
3117ed29b0b4SJens Axboe 		}
3118ed29b0b4SJens Axboe 
3119ed29b0b4SJens Axboe 		io_req_caches_free(ctx);
3120ed29b0b4SJens Axboe 
3121ed29b0b4SJens Axboe 		if (WARN_ON_ONCE(time_after(jiffies, timeout))) {
3122ed29b0b4SJens Axboe 			/* there is little hope left, don't run it too often */
3123ed29b0b4SJens Axboe 			interval = HZ * 60;
3124ed29b0b4SJens Axboe 		}
3125ed29b0b4SJens Axboe 	} while (!wait_for_completion_timeout(&ctx->ref_comp, interval));
3126ed29b0b4SJens Axboe 
3127ed29b0b4SJens Axboe 	init_completion(&exit.completion);
3128ed29b0b4SJens Axboe 	init_task_work(&exit.task_work, io_tctx_exit_cb);
3129ed29b0b4SJens Axboe 	exit.ctx = ctx;
3130ed29b0b4SJens Axboe 	/*
3131ed29b0b4SJens Axboe 	 * Some may use context even when all refs and requests have been put,
3132ed29b0b4SJens Axboe 	 * and they are free to do so while still holding uring_lock or
3133ed29b0b4SJens Axboe 	 * completion_lock, see io_req_task_submit(). Apart from other work,
3134ed29b0b4SJens Axboe 	 * this lock/unlock section also waits them to finish.
3135ed29b0b4SJens Axboe 	 */
3136ed29b0b4SJens Axboe 	mutex_lock(&ctx->uring_lock);
3137ed29b0b4SJens Axboe 	while (!list_empty(&ctx->tctx_list)) {
3138ed29b0b4SJens Axboe 		WARN_ON_ONCE(time_after(jiffies, timeout));
3139ed29b0b4SJens Axboe 
3140ed29b0b4SJens Axboe 		node = list_first_entry(&ctx->tctx_list, struct io_tctx_node,
3141ed29b0b4SJens Axboe 					ctx_node);
3142ed29b0b4SJens Axboe 		/* don't spin on a single task if cancellation failed */
3143ed29b0b4SJens Axboe 		list_rotate_left(&ctx->tctx_list);
3144ed29b0b4SJens Axboe 		ret = task_work_add(node->task, &exit.task_work, TWA_SIGNAL);
3145ed29b0b4SJens Axboe 		if (WARN_ON_ONCE(ret))
3146ed29b0b4SJens Axboe 			continue;
3147ed29b0b4SJens Axboe 
3148ed29b0b4SJens Axboe 		mutex_unlock(&ctx->uring_lock);
3149ed29b0b4SJens Axboe 		wait_for_completion(&exit.completion);
3150ed29b0b4SJens Axboe 		mutex_lock(&ctx->uring_lock);
3151ed29b0b4SJens Axboe 	}
3152ed29b0b4SJens Axboe 	mutex_unlock(&ctx->uring_lock);
3153ed29b0b4SJens Axboe 	spin_lock(&ctx->completion_lock);
3154ed29b0b4SJens Axboe 	spin_unlock(&ctx->completion_lock);
3155ed29b0b4SJens Axboe 
3156d73a572dSPavel Begunkov 	/* pairs with RCU read section in io_req_local_work_add() */
3157d73a572dSPavel Begunkov 	if (ctx->flags & IORING_SETUP_DEFER_TASKRUN)
3158d73a572dSPavel Begunkov 		synchronize_rcu();
3159d73a572dSPavel Begunkov 
3160ed29b0b4SJens Axboe 	io_ring_ctx_free(ctx);
3161ed29b0b4SJens Axboe }
3162ed29b0b4SJens Axboe 
3163ed29b0b4SJens Axboe static __cold void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
3164ed29b0b4SJens Axboe {
3165ed29b0b4SJens Axboe 	unsigned long index;
3166ed29b0b4SJens Axboe 	struct creds *creds;
3167ed29b0b4SJens Axboe 
3168ed29b0b4SJens Axboe 	mutex_lock(&ctx->uring_lock);
3169ed29b0b4SJens Axboe 	percpu_ref_kill(&ctx->refs);
3170ed29b0b4SJens Axboe 	xa_for_each(&ctx->personalities, index, creds)
3171ed29b0b4SJens Axboe 		io_unregister_personality(ctx, index);
31729ca9fb24SPavel Begunkov 	if (ctx->rings)
31739ca9fb24SPavel Begunkov 		io_poll_remove_all(ctx, NULL, true);
3174ed29b0b4SJens Axboe 	mutex_unlock(&ctx->uring_lock);
3175ed29b0b4SJens Axboe 
317602bac94bSPavel Begunkov 	/*
317702bac94bSPavel Begunkov 	 * If we failed setting up the ctx, we might not have any rings
317802bac94bSPavel Begunkov 	 * and therefore did not submit any requests
317902bac94bSPavel Begunkov 	 */
318002bac94bSPavel Begunkov 	if (ctx->rings)
3181ed29b0b4SJens Axboe 		io_kill_timeouts(ctx, NULL, true);
3182ed29b0b4SJens Axboe 
3183ed29b0b4SJens Axboe 	INIT_WORK(&ctx->exit_work, io_ring_exit_work);
3184ed29b0b4SJens Axboe 	/*
3185ed29b0b4SJens Axboe 	 * Use system_unbound_wq to avoid spawning tons of event kworkers
3186ed29b0b4SJens Axboe 	 * if we're exiting a ton of rings at the same time. It just adds
3187ed29b0b4SJens Axboe 	 * noise and overhead, there's no discernable change in runtime
3188ed29b0b4SJens Axboe 	 * over using system_wq.
3189ed29b0b4SJens Axboe 	 */
3190ed29b0b4SJens Axboe 	queue_work(system_unbound_wq, &ctx->exit_work);
3191ed29b0b4SJens Axboe }
3192ed29b0b4SJens Axboe 
3193ed29b0b4SJens Axboe static int io_uring_release(struct inode *inode, struct file *file)
3194ed29b0b4SJens Axboe {
3195ed29b0b4SJens Axboe 	struct io_ring_ctx *ctx = file->private_data;
3196ed29b0b4SJens Axboe 
3197ed29b0b4SJens Axboe 	file->private_data = NULL;
3198ed29b0b4SJens Axboe 	io_ring_ctx_wait_and_kill(ctx);
3199ed29b0b4SJens Axboe 	return 0;
3200ed29b0b4SJens Axboe }
3201ed29b0b4SJens Axboe 
3202ed29b0b4SJens Axboe struct io_task_cancel {
3203ed29b0b4SJens Axboe 	struct task_struct *task;
3204ed29b0b4SJens Axboe 	bool all;
3205ed29b0b4SJens Axboe };
3206ed29b0b4SJens Axboe 
3207ed29b0b4SJens Axboe static bool io_cancel_task_cb(struct io_wq_work *work, void *data)
3208ed29b0b4SJens Axboe {
3209ed29b0b4SJens Axboe 	struct io_kiocb *req = container_of(work, struct io_kiocb, work);
3210ed29b0b4SJens Axboe 	struct io_task_cancel *cancel = data;
3211ed29b0b4SJens Axboe 
3212ed29b0b4SJens Axboe 	return io_match_task_safe(req, cancel->task, cancel->all);
3213ed29b0b4SJens Axboe }
3214ed29b0b4SJens Axboe 
3215ed29b0b4SJens Axboe static __cold bool io_cancel_defer_files(struct io_ring_ctx *ctx,
3216ed29b0b4SJens Axboe 					 struct task_struct *task,
3217ed29b0b4SJens Axboe 					 bool cancel_all)
3218ed29b0b4SJens Axboe {
3219ed29b0b4SJens Axboe 	struct io_defer_entry *de;
3220ed29b0b4SJens Axboe 	LIST_HEAD(list);
3221ed29b0b4SJens Axboe 
3222ed29b0b4SJens Axboe 	spin_lock(&ctx->completion_lock);
3223ed29b0b4SJens Axboe 	list_for_each_entry_reverse(de, &ctx->defer_list, list) {
3224ed29b0b4SJens Axboe 		if (io_match_task_safe(de->req, task, cancel_all)) {
3225ed29b0b4SJens Axboe 			list_cut_position(&list, &ctx->defer_list, &de->list);
3226ed29b0b4SJens Axboe 			break;
3227ed29b0b4SJens Axboe 		}
3228ed29b0b4SJens Axboe 	}
3229ed29b0b4SJens Axboe 	spin_unlock(&ctx->completion_lock);
3230ed29b0b4SJens Axboe 	if (list_empty(&list))
3231ed29b0b4SJens Axboe 		return false;
3232ed29b0b4SJens Axboe 
3233ed29b0b4SJens Axboe 	while (!list_empty(&list)) {
3234ed29b0b4SJens Axboe 		de = list_first_entry(&list, struct io_defer_entry, list);
3235ed29b0b4SJens Axboe 		list_del_init(&de->list);
3236e276ae34SPavel Begunkov 		io_req_task_queue_fail(de->req, -ECANCELED);
3237ed29b0b4SJens Axboe 		kfree(de);
3238ed29b0b4SJens Axboe 	}
3239ed29b0b4SJens Axboe 	return true;
3240ed29b0b4SJens Axboe }
3241ed29b0b4SJens Axboe 
3242ed29b0b4SJens Axboe static __cold bool io_uring_try_cancel_iowq(struct io_ring_ctx *ctx)
3243ed29b0b4SJens Axboe {
3244ed29b0b4SJens Axboe 	struct io_tctx_node *node;
3245ed29b0b4SJens Axboe 	enum io_wq_cancel cret;
3246ed29b0b4SJens Axboe 	bool ret = false;
3247ed29b0b4SJens Axboe 
3248ed29b0b4SJens Axboe 	mutex_lock(&ctx->uring_lock);
3249ed29b0b4SJens Axboe 	list_for_each_entry(node, &ctx->tctx_list, ctx_node) {
3250ed29b0b4SJens Axboe 		struct io_uring_task *tctx = node->task->io_uring;
3251ed29b0b4SJens Axboe 
3252ed29b0b4SJens Axboe 		/*
3253ed29b0b4SJens Axboe 		 * io_wq will stay alive while we hold uring_lock, because it's
3254ed29b0b4SJens Axboe 		 * killed after ctx nodes, which requires to take the lock.
3255ed29b0b4SJens Axboe 		 */
3256ed29b0b4SJens Axboe 		if (!tctx || !tctx->io_wq)
3257ed29b0b4SJens Axboe 			continue;
3258ed29b0b4SJens Axboe 		cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_ctx_cb, ctx, true);
3259ed29b0b4SJens Axboe 		ret |= (cret != IO_WQ_CANCEL_NOTFOUND);
3260ed29b0b4SJens Axboe 	}
3261ed29b0b4SJens Axboe 	mutex_unlock(&ctx->uring_lock);
3262ed29b0b4SJens Axboe 
3263ed29b0b4SJens Axboe 	return ret;
3264ed29b0b4SJens Axboe }
3265ed29b0b4SJens Axboe 
3266affa87dbSPavel Begunkov static __cold bool io_uring_try_cancel_requests(struct io_ring_ctx *ctx,
3267ed29b0b4SJens Axboe 						struct task_struct *task,
3268ed29b0b4SJens Axboe 						bool cancel_all)
3269ed29b0b4SJens Axboe {
3270ed29b0b4SJens Axboe 	struct io_task_cancel cancel = { .task = task, .all = cancel_all, };
3271ed29b0b4SJens Axboe 	struct io_uring_task *tctx = task ? task->io_uring : NULL;
3272affa87dbSPavel Begunkov 	enum io_wq_cancel cret;
3273affa87dbSPavel Begunkov 	bool ret = false;
3274ed29b0b4SJens Axboe 
3275360cd42cSPavel Begunkov 	/* set it so io_req_local_work_add() would wake us up */
3276360cd42cSPavel Begunkov 	if (ctx->flags & IORING_SETUP_DEFER_TASKRUN) {
3277360cd42cSPavel Begunkov 		atomic_set(&ctx->cq_wait_nr, 1);
3278360cd42cSPavel Begunkov 		smp_mb();
3279360cd42cSPavel Begunkov 	}
3280360cd42cSPavel Begunkov 
3281ed29b0b4SJens Axboe 	/* failed during ring init, it couldn't have issued any requests */
3282ed29b0b4SJens Axboe 	if (!ctx->rings)
3283affa87dbSPavel Begunkov 		return false;
3284ed29b0b4SJens Axboe 
3285ed29b0b4SJens Axboe 	if (!task) {
3286ed29b0b4SJens Axboe 		ret |= io_uring_try_cancel_iowq(ctx);
3287ed29b0b4SJens Axboe 	} else if (tctx && tctx->io_wq) {
3288ed29b0b4SJens Axboe 		/*
3289ed29b0b4SJens Axboe 		 * Cancels requests of all rings, not only @ctx, but
3290ed29b0b4SJens Axboe 		 * it's fine as the task is in exit/exec.
3291ed29b0b4SJens Axboe 		 */
3292ed29b0b4SJens Axboe 		cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_task_cb,
3293ed29b0b4SJens Axboe 				       &cancel, true);
3294ed29b0b4SJens Axboe 		ret |= (cret != IO_WQ_CANCEL_NOTFOUND);
3295ed29b0b4SJens Axboe 	}
3296ed29b0b4SJens Axboe 
3297ed29b0b4SJens Axboe 	/* SQPOLL thread does its own polling */
3298ed29b0b4SJens Axboe 	if ((!(ctx->flags & IORING_SETUP_SQPOLL) && cancel_all) ||
3299ed29b0b4SJens Axboe 	    (ctx->sq_data && ctx->sq_data->thread == current)) {
3300ed29b0b4SJens Axboe 		while (!wq_list_empty(&ctx->iopoll_list)) {
3301ed29b0b4SJens Axboe 			io_iopoll_try_reap_events(ctx);
3302ed29b0b4SJens Axboe 			ret = true;
3303fcc926bbSJens Axboe 			cond_resched();
3304ed29b0b4SJens Axboe 		}
3305ed29b0b4SJens Axboe 	}
3306ed29b0b4SJens Axboe 
3307140102aeSPavel Begunkov 	if ((ctx->flags & IORING_SETUP_DEFER_TASKRUN) &&
3308140102aeSPavel Begunkov 	    io_allowed_defer_tw_run(ctx))
3309c0e0d6baSDylan Yudaken 		ret |= io_run_local_work(ctx) > 0;
3310ed29b0b4SJens Axboe 	ret |= io_cancel_defer_files(ctx, task, cancel_all);
33119ca9fb24SPavel Begunkov 	mutex_lock(&ctx->uring_lock);
3312ed29b0b4SJens Axboe 	ret |= io_poll_remove_all(ctx, task, cancel_all);
33139ca9fb24SPavel Begunkov 	mutex_unlock(&ctx->uring_lock);
3314ed29b0b4SJens Axboe 	ret |= io_kill_timeouts(ctx, task, cancel_all);
3315ed29b0b4SJens Axboe 	if (task)
3316c0e0d6baSDylan Yudaken 		ret |= io_run_task_work() > 0;
3317affa87dbSPavel Begunkov 	return ret;
3318ed29b0b4SJens Axboe }
3319ed29b0b4SJens Axboe 
3320ed29b0b4SJens Axboe static s64 tctx_inflight(struct io_uring_task *tctx, bool tracked)
3321ed29b0b4SJens Axboe {
3322ed29b0b4SJens Axboe 	if (tracked)
3323ed29b0b4SJens Axboe 		return atomic_read(&tctx->inflight_tracked);
3324ed29b0b4SJens Axboe 	return percpu_counter_sum(&tctx->inflight);
3325ed29b0b4SJens Axboe }
3326ed29b0b4SJens Axboe 
3327ed29b0b4SJens Axboe /*
3328ed29b0b4SJens Axboe  * Find any io_uring ctx that this task has registered or done IO on, and cancel
3329ed29b0b4SJens Axboe  * requests. @sqd should be not-null IFF it's an SQPOLL thread cancellation.
3330ed29b0b4SJens Axboe  */
333117437f31SJens Axboe __cold void io_uring_cancel_generic(bool cancel_all, struct io_sq_data *sqd)
3332ed29b0b4SJens Axboe {
3333ed29b0b4SJens Axboe 	struct io_uring_task *tctx = current->io_uring;
3334ed29b0b4SJens Axboe 	struct io_ring_ctx *ctx;
3335360cd42cSPavel Begunkov 	struct io_tctx_node *node;
3336360cd42cSPavel Begunkov 	unsigned long index;
3337ed29b0b4SJens Axboe 	s64 inflight;
3338ed29b0b4SJens Axboe 	DEFINE_WAIT(wait);
3339ed29b0b4SJens Axboe 
3340ed29b0b4SJens Axboe 	WARN_ON_ONCE(sqd && sqd->thread != current);
3341ed29b0b4SJens Axboe 
3342ed29b0b4SJens Axboe 	if (!current->io_uring)
3343ed29b0b4SJens Axboe 		return;
3344ed29b0b4SJens Axboe 	if (tctx->io_wq)
3345ed29b0b4SJens Axboe 		io_wq_exit_start(tctx->io_wq);
3346ed29b0b4SJens Axboe 
33478d664282SJens Axboe 	atomic_inc(&tctx->in_cancel);
3348ed29b0b4SJens Axboe 	do {
3349affa87dbSPavel Begunkov 		bool loop = false;
3350affa87dbSPavel Begunkov 
3351ed29b0b4SJens Axboe 		io_uring_drop_tctx_refs(current);
3352ed29b0b4SJens Axboe 		/* read completions before cancelations */
3353ed29b0b4SJens Axboe 		inflight = tctx_inflight(tctx, !cancel_all);
3354ed29b0b4SJens Axboe 		if (!inflight)
3355ed29b0b4SJens Axboe 			break;
3356ed29b0b4SJens Axboe 
3357ed29b0b4SJens Axboe 		if (!sqd) {
3358ed29b0b4SJens Axboe 			xa_for_each(&tctx->xa, index, node) {
3359ed29b0b4SJens Axboe 				/* sqpoll task will cancel all its requests */
3360ed29b0b4SJens Axboe 				if (node->ctx->sq_data)
3361ed29b0b4SJens Axboe 					continue;
3362affa87dbSPavel Begunkov 				loop |= io_uring_try_cancel_requests(node->ctx,
3363affa87dbSPavel Begunkov 							current, cancel_all);
3364ed29b0b4SJens Axboe 			}
3365ed29b0b4SJens Axboe 		} else {
3366ed29b0b4SJens Axboe 			list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
3367affa87dbSPavel Begunkov 				loop |= io_uring_try_cancel_requests(ctx,
3368affa87dbSPavel Begunkov 								     current,
3369ed29b0b4SJens Axboe 								     cancel_all);
3370ed29b0b4SJens Axboe 		}
3371ed29b0b4SJens Axboe 
3372affa87dbSPavel Begunkov 		if (loop) {
3373affa87dbSPavel Begunkov 			cond_resched();
3374affa87dbSPavel Begunkov 			continue;
3375affa87dbSPavel Begunkov 		}
3376affa87dbSPavel Begunkov 
3377ed29b0b4SJens Axboe 		prepare_to_wait(&tctx->wait, &wait, TASK_INTERRUPTIBLE);
3378ed29b0b4SJens Axboe 		io_run_task_work();
3379ed29b0b4SJens Axboe 		io_uring_drop_tctx_refs(current);
3380360cd42cSPavel Begunkov 		xa_for_each(&tctx->xa, index, node) {
3381360cd42cSPavel Begunkov 			if (!llist_empty(&node->ctx->work_llist)) {
3382360cd42cSPavel Begunkov 				WARN_ON_ONCE(node->ctx->submitter_task &&
3383360cd42cSPavel Begunkov 					     node->ctx->submitter_task != current);
3384360cd42cSPavel Begunkov 				goto end_wait;
3385360cd42cSPavel Begunkov 			}
3386360cd42cSPavel Begunkov 		}
3387ed29b0b4SJens Axboe 		/*
3388ed29b0b4SJens Axboe 		 * If we've seen completions, retry without waiting. This
3389ed29b0b4SJens Axboe 		 * avoids a race where a completion comes in before we did
3390ed29b0b4SJens Axboe 		 * prepare_to_wait().
3391ed29b0b4SJens Axboe 		 */
3392ed29b0b4SJens Axboe 		if (inflight == tctx_inflight(tctx, !cancel_all))
3393ed29b0b4SJens Axboe 			schedule();
3394360cd42cSPavel Begunkov end_wait:
3395ed29b0b4SJens Axboe 		finish_wait(&tctx->wait, &wait);
3396ed29b0b4SJens Axboe 	} while (1);
3397ed29b0b4SJens Axboe 
3398ed29b0b4SJens Axboe 	io_uring_clean_tctx(tctx);
3399ed29b0b4SJens Axboe 	if (cancel_all) {
3400ed29b0b4SJens Axboe 		/*
3401ed29b0b4SJens Axboe 		 * We shouldn't run task_works after cancel, so just leave
34028d664282SJens Axboe 		 * ->in_cancel set for normal exit.
3403ed29b0b4SJens Axboe 		 */
34048d664282SJens Axboe 		atomic_dec(&tctx->in_cancel);
3405ed29b0b4SJens Axboe 		/* for exec all current's requests should be gone, kill tctx */
3406ed29b0b4SJens Axboe 		__io_uring_free(current);
3407ed29b0b4SJens Axboe 	}
3408ed29b0b4SJens Axboe }
3409ed29b0b4SJens Axboe 
3410ed29b0b4SJens Axboe void __io_uring_cancel(bool cancel_all)
3411ed29b0b4SJens Axboe {
3412ed29b0b4SJens Axboe 	io_uring_cancel_generic(cancel_all, NULL);
3413ed29b0b4SJens Axboe }
3414ed29b0b4SJens Axboe 
3415ed29b0b4SJens Axboe static void *io_uring_validate_mmap_request(struct file *file,
3416ed29b0b4SJens Axboe 					    loff_t pgoff, size_t sz)
3417ed29b0b4SJens Axboe {
3418ed29b0b4SJens Axboe 	struct io_ring_ctx *ctx = file->private_data;
3419ed29b0b4SJens Axboe 	loff_t offset = pgoff << PAGE_SHIFT;
3420ed29b0b4SJens Axboe 	struct page *page;
3421ed29b0b4SJens Axboe 	void *ptr;
3422ed29b0b4SJens Axboe 
342303d89a2dSJens Axboe 	/* Don't allow mmap if the ring was setup without it */
342403d89a2dSJens Axboe 	if (ctx->flags & IORING_SETUP_NO_MMAP)
342503d89a2dSJens Axboe 		return ERR_PTR(-EINVAL);
342603d89a2dSJens Axboe 
3427c56e022cSJens Axboe 	switch (offset & IORING_OFF_MMAP_MASK) {
3428ed29b0b4SJens Axboe 	case IORING_OFF_SQ_RING:
3429ed29b0b4SJens Axboe 	case IORING_OFF_CQ_RING:
3430ed29b0b4SJens Axboe 		ptr = ctx->rings;
3431ed29b0b4SJens Axboe 		break;
3432ed29b0b4SJens Axboe 	case IORING_OFF_SQES:
3433ed29b0b4SJens Axboe 		ptr = ctx->sq_sqes;
3434ed29b0b4SJens Axboe 		break;
3435c56e022cSJens Axboe 	case IORING_OFF_PBUF_RING: {
3436c56e022cSJens Axboe 		unsigned int bgid;
3437c56e022cSJens Axboe 
3438c56e022cSJens Axboe 		bgid = (offset & ~IORING_OFF_MMAP_MASK) >> IORING_OFF_PBUF_SHIFT;
3439c56e022cSJens Axboe 		mutex_lock(&ctx->uring_lock);
3440c56e022cSJens Axboe 		ptr = io_pbuf_get_address(ctx, bgid);
3441c56e022cSJens Axboe 		mutex_unlock(&ctx->uring_lock);
3442c56e022cSJens Axboe 		if (!ptr)
3443c56e022cSJens Axboe 			return ERR_PTR(-EINVAL);
3444c56e022cSJens Axboe 		break;
3445c56e022cSJens Axboe 		}
3446ed29b0b4SJens Axboe 	default:
3447ed29b0b4SJens Axboe 		return ERR_PTR(-EINVAL);
3448ed29b0b4SJens Axboe 	}
3449ed29b0b4SJens Axboe 
3450ed29b0b4SJens Axboe 	page = virt_to_head_page(ptr);
3451ed29b0b4SJens Axboe 	if (sz > page_size(page))
3452ed29b0b4SJens Axboe 		return ERR_PTR(-EINVAL);
3453ed29b0b4SJens Axboe 
3454ed29b0b4SJens Axboe 	return ptr;
3455ed29b0b4SJens Axboe }
3456ed29b0b4SJens Axboe 
3457ed29b0b4SJens Axboe #ifdef CONFIG_MMU
3458ed29b0b4SJens Axboe 
3459ed29b0b4SJens Axboe static __cold int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
3460ed29b0b4SJens Axboe {
3461ed29b0b4SJens Axboe 	size_t sz = vma->vm_end - vma->vm_start;
3462ed29b0b4SJens Axboe 	unsigned long pfn;
3463ed29b0b4SJens Axboe 	void *ptr;
3464ed29b0b4SJens Axboe 
3465ed29b0b4SJens Axboe 	ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
3466ed29b0b4SJens Axboe 	if (IS_ERR(ptr))
3467ed29b0b4SJens Axboe 		return PTR_ERR(ptr);
3468ed29b0b4SJens Axboe 
3469ed29b0b4SJens Axboe 	pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
3470ed29b0b4SJens Axboe 	return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
3471ed29b0b4SJens Axboe }
3472ed29b0b4SJens Axboe 
3473d808459bSHelge Deller static unsigned long io_uring_mmu_get_unmapped_area(struct file *filp,
3474d808459bSHelge Deller 			unsigned long addr, unsigned long len,
3475d808459bSHelge Deller 			unsigned long pgoff, unsigned long flags)
3476d808459bSHelge Deller {
3477d808459bSHelge Deller 	const unsigned long mmap_end = arch_get_mmap_end(addr, len, flags);
3478d808459bSHelge Deller 	struct vm_unmapped_area_info info;
3479d808459bSHelge Deller 	void *ptr;
3480d808459bSHelge Deller 
3481d808459bSHelge Deller 	/*
3482d808459bSHelge Deller 	 * Do not allow to map to user-provided address to avoid breaking the
3483d808459bSHelge Deller 	 * aliasing rules. Userspace is not able to guess the offset address of
3484d808459bSHelge Deller 	 * kernel kmalloc()ed memory area.
3485d808459bSHelge Deller 	 */
3486d808459bSHelge Deller 	if (addr)
3487d808459bSHelge Deller 		return -EINVAL;
3488d808459bSHelge Deller 
3489d808459bSHelge Deller 	ptr = io_uring_validate_mmap_request(filp, pgoff, len);
3490d808459bSHelge Deller 	if (IS_ERR(ptr))
3491d808459bSHelge Deller 		return -ENOMEM;
3492d808459bSHelge Deller 
3493d808459bSHelge Deller 	info.flags = VM_UNMAPPED_AREA_TOPDOWN;
3494d808459bSHelge Deller 	info.length = len;
3495d808459bSHelge Deller 	info.low_limit = max(PAGE_SIZE, mmap_min_addr);
3496d808459bSHelge Deller 	info.high_limit = arch_get_mmap_base(addr, current->mm->mmap_base);
3497d808459bSHelge Deller #ifdef SHM_COLOUR
3498d808459bSHelge Deller 	info.align_mask = PAGE_MASK & (SHM_COLOUR - 1UL);
3499d808459bSHelge Deller #else
3500d808459bSHelge Deller 	info.align_mask = PAGE_MASK & (SHMLBA - 1UL);
3501d808459bSHelge Deller #endif
3502d808459bSHelge Deller 	info.align_offset = (unsigned long) ptr;
3503d808459bSHelge Deller 
3504d808459bSHelge Deller 	/*
3505d808459bSHelge Deller 	 * A failed mmap() very likely causes application failure,
3506d808459bSHelge Deller 	 * so fall back to the bottom-up function here. This scenario
3507d808459bSHelge Deller 	 * can happen with large stack limits and large mmap()
3508d808459bSHelge Deller 	 * allocations.
3509d808459bSHelge Deller 	 */
3510d808459bSHelge Deller 	addr = vm_unmapped_area(&info);
3511d808459bSHelge Deller 	if (offset_in_page(addr)) {
3512d808459bSHelge Deller 		info.flags = 0;
3513d808459bSHelge Deller 		info.low_limit = TASK_UNMAPPED_BASE;
3514d808459bSHelge Deller 		info.high_limit = mmap_end;
3515d808459bSHelge Deller 		addr = vm_unmapped_area(&info);
3516d808459bSHelge Deller 	}
3517d808459bSHelge Deller 
3518d808459bSHelge Deller 	return addr;
3519d808459bSHelge Deller }
3520d808459bSHelge Deller 
3521ed29b0b4SJens Axboe #else /* !CONFIG_MMU */
3522ed29b0b4SJens Axboe 
3523ed29b0b4SJens Axboe static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
3524ed29b0b4SJens Axboe {
3525fc4f4be9SDavid Hildenbrand 	return is_nommu_shared_mapping(vma->vm_flags) ? 0 : -EINVAL;
3526ed29b0b4SJens Axboe }
3527ed29b0b4SJens Axboe 
3528ed29b0b4SJens Axboe static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
3529ed29b0b4SJens Axboe {
3530ed29b0b4SJens Axboe 	return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
3531ed29b0b4SJens Axboe }
3532ed29b0b4SJens Axboe 
3533ed29b0b4SJens Axboe static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
3534ed29b0b4SJens Axboe 	unsigned long addr, unsigned long len,
3535ed29b0b4SJens Axboe 	unsigned long pgoff, unsigned long flags)
3536ed29b0b4SJens Axboe {
3537ed29b0b4SJens Axboe 	void *ptr;
3538ed29b0b4SJens Axboe 
3539ed29b0b4SJens Axboe 	ptr = io_uring_validate_mmap_request(file, pgoff, len);
3540ed29b0b4SJens Axboe 	if (IS_ERR(ptr))
3541ed29b0b4SJens Axboe 		return PTR_ERR(ptr);
3542ed29b0b4SJens Axboe 
3543ed29b0b4SJens Axboe 	return (unsigned long) ptr;
3544ed29b0b4SJens Axboe }
3545ed29b0b4SJens Axboe 
3546ed29b0b4SJens Axboe #endif /* !CONFIG_MMU */
3547ed29b0b4SJens Axboe 
3548ed29b0b4SJens Axboe static int io_validate_ext_arg(unsigned flags, const void __user *argp, size_t argsz)
3549ed29b0b4SJens Axboe {
3550ed29b0b4SJens Axboe 	if (flags & IORING_ENTER_EXT_ARG) {
3551ed29b0b4SJens Axboe 		struct io_uring_getevents_arg arg;
3552ed29b0b4SJens Axboe 
3553ed29b0b4SJens Axboe 		if (argsz != sizeof(arg))
3554ed29b0b4SJens Axboe 			return -EINVAL;
3555ed29b0b4SJens Axboe 		if (copy_from_user(&arg, argp, sizeof(arg)))
3556ed29b0b4SJens Axboe 			return -EFAULT;
3557ed29b0b4SJens Axboe 	}
3558ed29b0b4SJens Axboe 	return 0;
3559ed29b0b4SJens Axboe }
3560ed29b0b4SJens Axboe 
3561ed29b0b4SJens Axboe static int io_get_ext_arg(unsigned flags, const void __user *argp, size_t *argsz,
3562ed29b0b4SJens Axboe 			  struct __kernel_timespec __user **ts,
3563ed29b0b4SJens Axboe 			  const sigset_t __user **sig)
3564ed29b0b4SJens Axboe {
3565ed29b0b4SJens Axboe 	struct io_uring_getevents_arg arg;
3566ed29b0b4SJens Axboe 
3567ed29b0b4SJens Axboe 	/*
3568ed29b0b4SJens Axboe 	 * If EXT_ARG isn't set, then we have no timespec and the argp pointer
3569ed29b0b4SJens Axboe 	 * is just a pointer to the sigset_t.
3570ed29b0b4SJens Axboe 	 */
3571ed29b0b4SJens Axboe 	if (!(flags & IORING_ENTER_EXT_ARG)) {
3572ed29b0b4SJens Axboe 		*sig = (const sigset_t __user *) argp;
3573ed29b0b4SJens Axboe 		*ts = NULL;
3574ed29b0b4SJens Axboe 		return 0;
3575ed29b0b4SJens Axboe 	}
3576ed29b0b4SJens Axboe 
3577ed29b0b4SJens Axboe 	/*
3578ed29b0b4SJens Axboe 	 * EXT_ARG is set - ensure we agree on the size of it and copy in our
3579ed29b0b4SJens Axboe 	 * timespec and sigset_t pointers if good.
3580ed29b0b4SJens Axboe 	 */
3581ed29b0b4SJens Axboe 	if (*argsz != sizeof(arg))
3582ed29b0b4SJens Axboe 		return -EINVAL;
3583ed29b0b4SJens Axboe 	if (copy_from_user(&arg, argp, sizeof(arg)))
3584ed29b0b4SJens Axboe 		return -EFAULT;
3585ed29b0b4SJens Axboe 	if (arg.pad)
3586ed29b0b4SJens Axboe 		return -EINVAL;
3587ed29b0b4SJens Axboe 	*sig = u64_to_user_ptr(arg.sigmask);
3588ed29b0b4SJens Axboe 	*argsz = arg.sigmask_sz;
3589ed29b0b4SJens Axboe 	*ts = u64_to_user_ptr(arg.ts);
3590ed29b0b4SJens Axboe 	return 0;
3591ed29b0b4SJens Axboe }
3592ed29b0b4SJens Axboe 
3593ed29b0b4SJens Axboe SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
3594ed29b0b4SJens Axboe 		u32, min_complete, u32, flags, const void __user *, argp,
3595ed29b0b4SJens Axboe 		size_t, argsz)
3596ed29b0b4SJens Axboe {
3597ed29b0b4SJens Axboe 	struct io_ring_ctx *ctx;
3598ed29b0b4SJens Axboe 	struct fd f;
3599ed29b0b4SJens Axboe 	long ret;
3600ed29b0b4SJens Axboe 
3601ed29b0b4SJens Axboe 	if (unlikely(flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP |
3602ed29b0b4SJens Axboe 			       IORING_ENTER_SQ_WAIT | IORING_ENTER_EXT_ARG |
3603ed29b0b4SJens Axboe 			       IORING_ENTER_REGISTERED_RING)))
3604ed29b0b4SJens Axboe 		return -EINVAL;
3605ed29b0b4SJens Axboe 
3606ed29b0b4SJens Axboe 	/*
3607ed29b0b4SJens Axboe 	 * Ring fd has been registered via IORING_REGISTER_RING_FDS, we
3608ed29b0b4SJens Axboe 	 * need only dereference our task private array to find it.
3609ed29b0b4SJens Axboe 	 */
3610ed29b0b4SJens Axboe 	if (flags & IORING_ENTER_REGISTERED_RING) {
3611ed29b0b4SJens Axboe 		struct io_uring_task *tctx = current->io_uring;
3612ed29b0b4SJens Axboe 
36133273c440SPavel Begunkov 		if (unlikely(!tctx || fd >= IO_RINGFD_REG_MAX))
3614ed29b0b4SJens Axboe 			return -EINVAL;
3615ed29b0b4SJens Axboe 		fd = array_index_nospec(fd, IO_RINGFD_REG_MAX);
3616ed29b0b4SJens Axboe 		f.file = tctx->registered_rings[fd];
3617ed29b0b4SJens Axboe 		f.flags = 0;
3618ed29b0b4SJens Axboe 		if (unlikely(!f.file))
3619ed29b0b4SJens Axboe 			return -EBADF;
36203273c440SPavel Begunkov 	} else {
36213273c440SPavel Begunkov 		f = fdget(fd);
36223273c440SPavel Begunkov 		if (unlikely(!f.file))
36233273c440SPavel Begunkov 			return -EBADF;
3624ed29b0b4SJens Axboe 		ret = -EOPNOTSUPP;
3625e5550a14SJens Axboe 		if (unlikely(!io_is_uring_fops(f.file)))
3626fbb8bb02SPavel Begunkov 			goto out;
36273273c440SPavel Begunkov 	}
3628ed29b0b4SJens Axboe 
3629ed29b0b4SJens Axboe 	ctx = f.file->private_data;
3630ed29b0b4SJens Axboe 	ret = -EBADFD;
3631ed29b0b4SJens Axboe 	if (unlikely(ctx->flags & IORING_SETUP_R_DISABLED))
3632ed29b0b4SJens Axboe 		goto out;
3633ed29b0b4SJens Axboe 
3634ed29b0b4SJens Axboe 	/*
3635ed29b0b4SJens Axboe 	 * For SQ polling, the thread will do all submissions and completions.
3636ed29b0b4SJens Axboe 	 * Just return the requested submit count, and wake the thread if
3637ed29b0b4SJens Axboe 	 * we were asked to.
3638ed29b0b4SJens Axboe 	 */
3639ed29b0b4SJens Axboe 	ret = 0;
3640ed29b0b4SJens Axboe 	if (ctx->flags & IORING_SETUP_SQPOLL) {
3641ed29b0b4SJens Axboe 		io_cqring_overflow_flush(ctx);
3642ed29b0b4SJens Axboe 
3643ed29b0b4SJens Axboe 		if (unlikely(ctx->sq_data->thread == NULL)) {
3644ed29b0b4SJens Axboe 			ret = -EOWNERDEAD;
3645ed29b0b4SJens Axboe 			goto out;
3646ed29b0b4SJens Axboe 		}
3647ed29b0b4SJens Axboe 		if (flags & IORING_ENTER_SQ_WAKEUP)
3648ed29b0b4SJens Axboe 			wake_up(&ctx->sq_data->wait);
364988b80534SQuanfa Fu 		if (flags & IORING_ENTER_SQ_WAIT)
365088b80534SQuanfa Fu 			io_sqpoll_wait_sq(ctx);
365188b80534SQuanfa Fu 
3652ed29b0b4SJens Axboe 		ret = to_submit;
3653ed29b0b4SJens Axboe 	} else if (to_submit) {
3654ed29b0b4SJens Axboe 		ret = io_uring_add_tctx_node(ctx);
3655ed29b0b4SJens Axboe 		if (unlikely(ret))
3656ed29b0b4SJens Axboe 			goto out;
3657ed29b0b4SJens Axboe 
3658ed29b0b4SJens Axboe 		mutex_lock(&ctx->uring_lock);
3659ed29b0b4SJens Axboe 		ret = io_submit_sqes(ctx, to_submit);
3660ed29b0b4SJens Axboe 		if (ret != to_submit) {
3661ed29b0b4SJens Axboe 			mutex_unlock(&ctx->uring_lock);
3662ed29b0b4SJens Axboe 			goto out;
3663ed29b0b4SJens Axboe 		}
366444f87745SPavel Begunkov 		if (flags & IORING_ENTER_GETEVENTS) {
366544f87745SPavel Begunkov 			if (ctx->syscall_iopoll)
3666ed29b0b4SJens Axboe 				goto iopoll_locked;
366744f87745SPavel Begunkov 			/*
366844f87745SPavel Begunkov 			 * Ignore errors, we'll soon call io_cqring_wait() and
366944f87745SPavel Begunkov 			 * it should handle ownership problems if any.
367044f87745SPavel Begunkov 			 */
367144f87745SPavel Begunkov 			if (ctx->flags & IORING_SETUP_DEFER_TASKRUN)
367244f87745SPavel Begunkov 				(void)io_run_local_work_locked(ctx);
367344f87745SPavel Begunkov 		}
3674ed29b0b4SJens Axboe 		mutex_unlock(&ctx->uring_lock);
3675ed29b0b4SJens Axboe 	}
3676c0e0d6baSDylan Yudaken 
3677ed29b0b4SJens Axboe 	if (flags & IORING_ENTER_GETEVENTS) {
3678ed29b0b4SJens Axboe 		int ret2;
3679c0e0d6baSDylan Yudaken 
3680ed29b0b4SJens Axboe 		if (ctx->syscall_iopoll) {
3681ed29b0b4SJens Axboe 			/*
3682ed29b0b4SJens Axboe 			 * We disallow the app entering submit/complete with
3683ed29b0b4SJens Axboe 			 * polling, but we still need to lock the ring to
3684ed29b0b4SJens Axboe 			 * prevent racing with polled issue that got punted to
3685ed29b0b4SJens Axboe 			 * a workqueue.
3686ed29b0b4SJens Axboe 			 */
3687ed29b0b4SJens Axboe 			mutex_lock(&ctx->uring_lock);
3688ed29b0b4SJens Axboe iopoll_locked:
3689ed29b0b4SJens Axboe 			ret2 = io_validate_ext_arg(flags, argp, argsz);
3690ed29b0b4SJens Axboe 			if (likely(!ret2)) {
3691ed29b0b4SJens Axboe 				min_complete = min(min_complete,
3692ed29b0b4SJens Axboe 						   ctx->cq_entries);
3693ed29b0b4SJens Axboe 				ret2 = io_iopoll_check(ctx, min_complete);
3694ed29b0b4SJens Axboe 			}
3695ed29b0b4SJens Axboe 			mutex_unlock(&ctx->uring_lock);
3696ed29b0b4SJens Axboe 		} else {
3697ed29b0b4SJens Axboe 			const sigset_t __user *sig;
3698ed29b0b4SJens Axboe 			struct __kernel_timespec __user *ts;
3699ed29b0b4SJens Axboe 
3700ed29b0b4SJens Axboe 			ret2 = io_get_ext_arg(flags, argp, &argsz, &ts, &sig);
3701ed29b0b4SJens Axboe 			if (likely(!ret2)) {
3702ed29b0b4SJens Axboe 				min_complete = min(min_complete,
3703ed29b0b4SJens Axboe 						   ctx->cq_entries);
3704ed29b0b4SJens Axboe 				ret2 = io_cqring_wait(ctx, min_complete, sig,
3705ed29b0b4SJens Axboe 						      argsz, ts);
3706ed29b0b4SJens Axboe 			}
3707ed29b0b4SJens Axboe 		}
3708ed29b0b4SJens Axboe 
3709ed29b0b4SJens Axboe 		if (!ret) {
3710ed29b0b4SJens Axboe 			ret = ret2;
3711ed29b0b4SJens Axboe 
3712ed29b0b4SJens Axboe 			/*
3713ed29b0b4SJens Axboe 			 * EBADR indicates that one or more CQE were dropped.
3714ed29b0b4SJens Axboe 			 * Once the user has been informed we can clear the bit
3715ed29b0b4SJens Axboe 			 * as they are obviously ok with those drops.
3716ed29b0b4SJens Axboe 			 */
3717ed29b0b4SJens Axboe 			if (unlikely(ret2 == -EBADR))
3718ed29b0b4SJens Axboe 				clear_bit(IO_CHECK_CQ_DROPPED_BIT,
3719ed29b0b4SJens Axboe 					  &ctx->check_cq);
3720ed29b0b4SJens Axboe 		}
3721ed29b0b4SJens Axboe 	}
3722ed29b0b4SJens Axboe out:
3723ed29b0b4SJens Axboe 	fdput(f);
3724ed29b0b4SJens Axboe 	return ret;
3725ed29b0b4SJens Axboe }
3726ed29b0b4SJens Axboe 
3727ed29b0b4SJens Axboe static const struct file_operations io_uring_fops = {
3728ed29b0b4SJens Axboe 	.release	= io_uring_release,
3729ed29b0b4SJens Axboe 	.mmap		= io_uring_mmap,
3730ed29b0b4SJens Axboe #ifndef CONFIG_MMU
3731ed29b0b4SJens Axboe 	.get_unmapped_area = io_uring_nommu_get_unmapped_area,
3732ed29b0b4SJens Axboe 	.mmap_capabilities = io_uring_nommu_mmap_capabilities,
3733d808459bSHelge Deller #else
3734d808459bSHelge Deller 	.get_unmapped_area = io_uring_mmu_get_unmapped_area,
3735ed29b0b4SJens Axboe #endif
3736ed29b0b4SJens Axboe 	.poll		= io_uring_poll,
3737ed29b0b4SJens Axboe #ifdef CONFIG_PROC_FS
3738ed29b0b4SJens Axboe 	.show_fdinfo	= io_uring_show_fdinfo,
3739ed29b0b4SJens Axboe #endif
3740ed29b0b4SJens Axboe };
3741ed29b0b4SJens Axboe 
374292ac8beaSJens Axboe bool io_is_uring_fops(struct file *file)
374392ac8beaSJens Axboe {
374492ac8beaSJens Axboe 	return file->f_op == &io_uring_fops;
374592ac8beaSJens Axboe }
374692ac8beaSJens Axboe 
3747ed29b0b4SJens Axboe static __cold int io_allocate_scq_urings(struct io_ring_ctx *ctx,
3748ed29b0b4SJens Axboe 					 struct io_uring_params *p)
3749ed29b0b4SJens Axboe {
3750ed29b0b4SJens Axboe 	struct io_rings *rings;
3751ed29b0b4SJens Axboe 	size_t size, sq_array_offset;
3752e27cef86SJens Axboe 	void *ptr;
3753ed29b0b4SJens Axboe 
3754ed29b0b4SJens Axboe 	/* make sure these are sane, as we already accounted them */
3755ed29b0b4SJens Axboe 	ctx->sq_entries = p->sq_entries;
3756ed29b0b4SJens Axboe 	ctx->cq_entries = p->cq_entries;
3757ed29b0b4SJens Axboe 
3758ed29b0b4SJens Axboe 	size = rings_size(ctx, p->sq_entries, p->cq_entries, &sq_array_offset);
3759ed29b0b4SJens Axboe 	if (size == SIZE_MAX)
3760ed29b0b4SJens Axboe 		return -EOVERFLOW;
3761ed29b0b4SJens Axboe 
376203d89a2dSJens Axboe 	if (!(ctx->flags & IORING_SETUP_NO_MMAP))
3763ed29b0b4SJens Axboe 		rings = io_mem_alloc(size);
376403d89a2dSJens Axboe 	else
376503d89a2dSJens Axboe 		rings = io_rings_map(ctx, p->cq_off.user_addr, size);
376603d89a2dSJens Axboe 
3767e27cef86SJens Axboe 	if (IS_ERR(rings))
3768e27cef86SJens Axboe 		return PTR_ERR(rings);
3769ed29b0b4SJens Axboe 
3770ed29b0b4SJens Axboe 	ctx->rings = rings;
3771ed29b0b4SJens Axboe 	ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
3772ed29b0b4SJens Axboe 	rings->sq_ring_mask = p->sq_entries - 1;
3773ed29b0b4SJens Axboe 	rings->cq_ring_mask = p->cq_entries - 1;
3774ed29b0b4SJens Axboe 	rings->sq_ring_entries = p->sq_entries;
3775ed29b0b4SJens Axboe 	rings->cq_ring_entries = p->cq_entries;
3776ed29b0b4SJens Axboe 
3777ed29b0b4SJens Axboe 	if (p->flags & IORING_SETUP_SQE128)
3778ed29b0b4SJens Axboe 		size = array_size(2 * sizeof(struct io_uring_sqe), p->sq_entries);
3779ed29b0b4SJens Axboe 	else
3780ed29b0b4SJens Axboe 		size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
3781ed29b0b4SJens Axboe 	if (size == SIZE_MAX) {
37829c189eeeSJens Axboe 		io_rings_free(ctx);
3783ed29b0b4SJens Axboe 		return -EOVERFLOW;
3784ed29b0b4SJens Axboe 	}
3785ed29b0b4SJens Axboe 
378603d89a2dSJens Axboe 	if (!(ctx->flags & IORING_SETUP_NO_MMAP))
3787e27cef86SJens Axboe 		ptr = io_mem_alloc(size);
378803d89a2dSJens Axboe 	else
378903d89a2dSJens Axboe 		ptr = io_sqes_map(ctx, p->sq_off.user_addr, size);
379003d89a2dSJens Axboe 
3791e27cef86SJens Axboe 	if (IS_ERR(ptr)) {
37929c189eeeSJens Axboe 		io_rings_free(ctx);
3793e27cef86SJens Axboe 		return PTR_ERR(ptr);
3794ed29b0b4SJens Axboe 	}
3795ed29b0b4SJens Axboe 
3796e27cef86SJens Axboe 	ctx->sq_sqes = ptr;
3797ed29b0b4SJens Axboe 	return 0;
3798ed29b0b4SJens Axboe }
3799ed29b0b4SJens Axboe 
38006e76ac59SJosh Triplett static int io_uring_install_fd(struct file *file)
3801ed29b0b4SJens Axboe {
38026e76ac59SJosh Triplett 	int fd;
3803ed29b0b4SJens Axboe 
3804ed29b0b4SJens Axboe 	fd = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
3805ed29b0b4SJens Axboe 	if (fd < 0)
3806ed29b0b4SJens Axboe 		return fd;
3807ed29b0b4SJens Axboe 	fd_install(fd, file);
3808ed29b0b4SJens Axboe 	return fd;
3809ed29b0b4SJens Axboe }
3810ed29b0b4SJens Axboe 
3811ed29b0b4SJens Axboe /*
3812ed29b0b4SJens Axboe  * Allocate an anonymous fd, this is what constitutes the application
3813ed29b0b4SJens Axboe  * visible backing of an io_uring instance. The application mmaps this
3814ed29b0b4SJens Axboe  * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
3815ed29b0b4SJens Axboe  * we have to tie this fd to a socket for file garbage collection purposes.
3816ed29b0b4SJens Axboe  */
3817ed29b0b4SJens Axboe static struct file *io_uring_get_file(struct io_ring_ctx *ctx)
3818ed29b0b4SJens Axboe {
3819ed29b0b4SJens Axboe 	struct file *file;
3820ed29b0b4SJens Axboe #if defined(CONFIG_UNIX)
3821ed29b0b4SJens Axboe 	int ret;
3822ed29b0b4SJens Axboe 
3823ed29b0b4SJens Axboe 	ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
3824ed29b0b4SJens Axboe 				&ctx->ring_sock);
3825ed29b0b4SJens Axboe 	if (ret)
3826ed29b0b4SJens Axboe 		return ERR_PTR(ret);
3827ed29b0b4SJens Axboe #endif
3828ed29b0b4SJens Axboe 
3829ed29b0b4SJens Axboe 	file = anon_inode_getfile_secure("[io_uring]", &io_uring_fops, ctx,
3830ed29b0b4SJens Axboe 					 O_RDWR | O_CLOEXEC, NULL);
3831ed29b0b4SJens Axboe #if defined(CONFIG_UNIX)
3832ed29b0b4SJens Axboe 	if (IS_ERR(file)) {
3833ed29b0b4SJens Axboe 		sock_release(ctx->ring_sock);
3834ed29b0b4SJens Axboe 		ctx->ring_sock = NULL;
3835ed29b0b4SJens Axboe 	} else {
3836ed29b0b4SJens Axboe 		ctx->ring_sock->file = file;
3837ed29b0b4SJens Axboe 	}
3838ed29b0b4SJens Axboe #endif
3839ed29b0b4SJens Axboe 	return file;
3840ed29b0b4SJens Axboe }
3841ed29b0b4SJens Axboe 
3842ed29b0b4SJens Axboe static __cold int io_uring_create(unsigned entries, struct io_uring_params *p,
3843ed29b0b4SJens Axboe 				  struct io_uring_params __user *params)
3844ed29b0b4SJens Axboe {
3845ed29b0b4SJens Axboe 	struct io_ring_ctx *ctx;
38466e76ac59SJosh Triplett 	struct io_uring_task *tctx;
3847ed29b0b4SJens Axboe 	struct file *file;
3848ed29b0b4SJens Axboe 	int ret;
3849ed29b0b4SJens Axboe 
3850ed29b0b4SJens Axboe 	if (!entries)
3851ed29b0b4SJens Axboe 		return -EINVAL;
3852ed29b0b4SJens Axboe 	if (entries > IORING_MAX_ENTRIES) {
3853ed29b0b4SJens Axboe 		if (!(p->flags & IORING_SETUP_CLAMP))
3854ed29b0b4SJens Axboe 			return -EINVAL;
3855ed29b0b4SJens Axboe 		entries = IORING_MAX_ENTRIES;
3856ed29b0b4SJens Axboe 	}
3857ed29b0b4SJens Axboe 
38586e76ac59SJosh Triplett 	if ((p->flags & IORING_SETUP_REGISTERED_FD_ONLY)
38596e76ac59SJosh Triplett 	    && !(p->flags & IORING_SETUP_NO_MMAP))
38606e76ac59SJosh Triplett 		return -EINVAL;
38616e76ac59SJosh Triplett 
3862ed29b0b4SJens Axboe 	/*
3863ed29b0b4SJens Axboe 	 * Use twice as many entries for the CQ ring. It's possible for the
3864ed29b0b4SJens Axboe 	 * application to drive a higher depth than the size of the SQ ring,
3865ed29b0b4SJens Axboe 	 * since the sqes are only used at submission time. This allows for
3866ed29b0b4SJens Axboe 	 * some flexibility in overcommitting a bit. If the application has
3867ed29b0b4SJens Axboe 	 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
3868ed29b0b4SJens Axboe 	 * of CQ ring entries manually.
3869ed29b0b4SJens Axboe 	 */
3870ed29b0b4SJens Axboe 	p->sq_entries = roundup_pow_of_two(entries);
3871ed29b0b4SJens Axboe 	if (p->flags & IORING_SETUP_CQSIZE) {
3872ed29b0b4SJens Axboe 		/*
3873ed29b0b4SJens Axboe 		 * If IORING_SETUP_CQSIZE is set, we do the same roundup
3874ed29b0b4SJens Axboe 		 * to a power-of-two, if it isn't already. We do NOT impose
3875ed29b0b4SJens Axboe 		 * any cq vs sq ring sizing.
3876ed29b0b4SJens Axboe 		 */
3877ed29b0b4SJens Axboe 		if (!p->cq_entries)
3878ed29b0b4SJens Axboe 			return -EINVAL;
3879ed29b0b4SJens Axboe 		if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
3880ed29b0b4SJens Axboe 			if (!(p->flags & IORING_SETUP_CLAMP))
3881ed29b0b4SJens Axboe 				return -EINVAL;
3882ed29b0b4SJens Axboe 			p->cq_entries = IORING_MAX_CQ_ENTRIES;
3883ed29b0b4SJens Axboe 		}
3884ed29b0b4SJens Axboe 		p->cq_entries = roundup_pow_of_two(p->cq_entries);
3885ed29b0b4SJens Axboe 		if (p->cq_entries < p->sq_entries)
3886ed29b0b4SJens Axboe 			return -EINVAL;
3887ed29b0b4SJens Axboe 	} else {
3888ed29b0b4SJens Axboe 		p->cq_entries = 2 * p->sq_entries;
3889ed29b0b4SJens Axboe 	}
3890ed29b0b4SJens Axboe 
3891ed29b0b4SJens Axboe 	ctx = io_ring_ctx_alloc(p);
3892ed29b0b4SJens Axboe 	if (!ctx)
3893ed29b0b4SJens Axboe 		return -ENOMEM;
3894ed29b0b4SJens Axboe 
3895e6aeb272SPavel Begunkov 	if ((ctx->flags & IORING_SETUP_DEFER_TASKRUN) &&
3896e6aeb272SPavel Begunkov 	    !(ctx->flags & IORING_SETUP_IOPOLL) &&
3897e6aeb272SPavel Begunkov 	    !(ctx->flags & IORING_SETUP_SQPOLL))
3898e6aeb272SPavel Begunkov 		ctx->task_complete = true;
3899e6aeb272SPavel Begunkov 
3900ed29b0b4SJens Axboe 	/*
3901bca39f39SPavel Begunkov 	 * lazy poll_wq activation relies on ->task_complete for synchronisation
3902bca39f39SPavel Begunkov 	 * purposes, see io_activate_pollwq()
3903bca39f39SPavel Begunkov 	 */
3904bca39f39SPavel Begunkov 	if (!ctx->task_complete)
3905bca39f39SPavel Begunkov 		ctx->poll_activated = true;
3906bca39f39SPavel Begunkov 
3907bca39f39SPavel Begunkov 	/*
3908ed29b0b4SJens Axboe 	 * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user
3909ed29b0b4SJens Axboe 	 * space applications don't need to do io completion events
3910ed29b0b4SJens Axboe 	 * polling again, they can rely on io_sq_thread to do polling
3911ed29b0b4SJens Axboe 	 * work, which can reduce cpu usage and uring_lock contention.
3912ed29b0b4SJens Axboe 	 */
3913ed29b0b4SJens Axboe 	if (ctx->flags & IORING_SETUP_IOPOLL &&
3914ed29b0b4SJens Axboe 	    !(ctx->flags & IORING_SETUP_SQPOLL))
3915ed29b0b4SJens Axboe 		ctx->syscall_iopoll = 1;
3916ed29b0b4SJens Axboe 
3917ed29b0b4SJens Axboe 	ctx->compat = in_compat_syscall();
3918ed29b0b4SJens Axboe 	if (!capable(CAP_IPC_LOCK))
3919ed29b0b4SJens Axboe 		ctx->user = get_uid(current_user());
3920ed29b0b4SJens Axboe 
3921ed29b0b4SJens Axboe 	/*
3922ed29b0b4SJens Axboe 	 * For SQPOLL, we just need a wakeup, always. For !SQPOLL, if
3923ed29b0b4SJens Axboe 	 * COOP_TASKRUN is set, then IPIs are never needed by the app.
3924ed29b0b4SJens Axboe 	 */
3925ed29b0b4SJens Axboe 	ret = -EINVAL;
3926ed29b0b4SJens Axboe 	if (ctx->flags & IORING_SETUP_SQPOLL) {
3927ed29b0b4SJens Axboe 		/* IPI related flags don't make sense with SQPOLL */
3928ed29b0b4SJens Axboe 		if (ctx->flags & (IORING_SETUP_COOP_TASKRUN |
3929c0e0d6baSDylan Yudaken 				  IORING_SETUP_TASKRUN_FLAG |
3930c0e0d6baSDylan Yudaken 				  IORING_SETUP_DEFER_TASKRUN))
3931ed29b0b4SJens Axboe 			goto err;
3932ed29b0b4SJens Axboe 		ctx->notify_method = TWA_SIGNAL_NO_IPI;
3933ed29b0b4SJens Axboe 	} else if (ctx->flags & IORING_SETUP_COOP_TASKRUN) {
3934ed29b0b4SJens Axboe 		ctx->notify_method = TWA_SIGNAL_NO_IPI;
3935ed29b0b4SJens Axboe 	} else {
3936c0e0d6baSDylan Yudaken 		if (ctx->flags & IORING_SETUP_TASKRUN_FLAG &&
3937c0e0d6baSDylan Yudaken 		    !(ctx->flags & IORING_SETUP_DEFER_TASKRUN))
3938ed29b0b4SJens Axboe 			goto err;
3939ed29b0b4SJens Axboe 		ctx->notify_method = TWA_SIGNAL;
3940ed29b0b4SJens Axboe 	}
3941ed29b0b4SJens Axboe 
3942ed29b0b4SJens Axboe 	/*
3943c0e0d6baSDylan Yudaken 	 * For DEFER_TASKRUN we require the completion task to be the same as the
3944c0e0d6baSDylan Yudaken 	 * submission task. This implies that there is only one submitter, so enforce
3945c0e0d6baSDylan Yudaken 	 * that.
3946c0e0d6baSDylan Yudaken 	 */
3947c0e0d6baSDylan Yudaken 	if (ctx->flags & IORING_SETUP_DEFER_TASKRUN &&
3948c0e0d6baSDylan Yudaken 	    !(ctx->flags & IORING_SETUP_SINGLE_ISSUER)) {
3949c0e0d6baSDylan Yudaken 		goto err;
3950c0e0d6baSDylan Yudaken 	}
3951c0e0d6baSDylan Yudaken 
3952c0e0d6baSDylan Yudaken 	/*
3953ed29b0b4SJens Axboe 	 * This is just grabbed for accounting purposes. When a process exits,
3954ed29b0b4SJens Axboe 	 * the mm is exited and dropped before the files, hence we need to hang
3955ed29b0b4SJens Axboe 	 * on to this mm purely for the purposes of being able to unaccount
3956ed29b0b4SJens Axboe 	 * memory (locked/pinned vm). It's not used for anything else.
3957ed29b0b4SJens Axboe 	 */
3958ed29b0b4SJens Axboe 	mmgrab(current->mm);
3959ed29b0b4SJens Axboe 	ctx->mm_account = current->mm;
3960ed29b0b4SJens Axboe 
3961ed29b0b4SJens Axboe 	ret = io_allocate_scq_urings(ctx, p);
3962ed29b0b4SJens Axboe 	if (ret)
3963ed29b0b4SJens Axboe 		goto err;
3964ed29b0b4SJens Axboe 
3965ed29b0b4SJens Axboe 	ret = io_sq_offload_create(ctx, p);
3966ed29b0b4SJens Axboe 	if (ret)
3967ed29b0b4SJens Axboe 		goto err;
39682933ae6eSPavel Begunkov 
39692933ae6eSPavel Begunkov 	ret = io_rsrc_init(ctx);
3970ed29b0b4SJens Axboe 	if (ret)
3971ed29b0b4SJens Axboe 		goto err;
3972ed29b0b4SJens Axboe 
3973ed29b0b4SJens Axboe 	p->sq_off.head = offsetof(struct io_rings, sq.head);
3974ed29b0b4SJens Axboe 	p->sq_off.tail = offsetof(struct io_rings, sq.tail);
3975ed29b0b4SJens Axboe 	p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
3976ed29b0b4SJens Axboe 	p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
3977ed29b0b4SJens Axboe 	p->sq_off.flags = offsetof(struct io_rings, sq_flags);
3978ed29b0b4SJens Axboe 	p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
3979ed29b0b4SJens Axboe 	p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
39809b1b58caSJens Axboe 	p->sq_off.resv1 = 0;
398103d89a2dSJens Axboe 	if (!(ctx->flags & IORING_SETUP_NO_MMAP))
398203d89a2dSJens Axboe 		p->sq_off.user_addr = 0;
3983ed29b0b4SJens Axboe 
3984ed29b0b4SJens Axboe 	p->cq_off.head = offsetof(struct io_rings, cq.head);
3985ed29b0b4SJens Axboe 	p->cq_off.tail = offsetof(struct io_rings, cq.tail);
3986ed29b0b4SJens Axboe 	p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
3987ed29b0b4SJens Axboe 	p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
3988ed29b0b4SJens Axboe 	p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
3989ed29b0b4SJens Axboe 	p->cq_off.cqes = offsetof(struct io_rings, cqes);
3990ed29b0b4SJens Axboe 	p->cq_off.flags = offsetof(struct io_rings, cq_flags);
39919b1b58caSJens Axboe 	p->cq_off.resv1 = 0;
399203d89a2dSJens Axboe 	if (!(ctx->flags & IORING_SETUP_NO_MMAP))
399303d89a2dSJens Axboe 		p->cq_off.user_addr = 0;
3994ed29b0b4SJens Axboe 
3995ed29b0b4SJens Axboe 	p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
3996ed29b0b4SJens Axboe 			IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
3997ed29b0b4SJens Axboe 			IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL |
3998ed29b0b4SJens Axboe 			IORING_FEAT_POLL_32BITS | IORING_FEAT_SQPOLL_NONFIXED |
3999ed29b0b4SJens Axboe 			IORING_FEAT_EXT_ARG | IORING_FEAT_NATIVE_WORKERS |
4000ed29b0b4SJens Axboe 			IORING_FEAT_RSRC_TAGS | IORING_FEAT_CQE_SKIP |
40017d3fd88dSJosh Triplett 			IORING_FEAT_LINKED_FILE | IORING_FEAT_REG_REG_RING;
4002ed29b0b4SJens Axboe 
4003ed29b0b4SJens Axboe 	if (copy_to_user(params, p, sizeof(*p))) {
4004ed29b0b4SJens Axboe 		ret = -EFAULT;
4005ed29b0b4SJens Axboe 		goto err;
4006ed29b0b4SJens Axboe 	}
4007ed29b0b4SJens Axboe 
40087cae596bSDylan Yudaken 	if (ctx->flags & IORING_SETUP_SINGLE_ISSUER
40097cae596bSDylan Yudaken 	    && !(ctx->flags & IORING_SETUP_R_DISABLED))
40108579538cSPavel Begunkov 		WRITE_ONCE(ctx->submitter_task, get_task_struct(current));
40117cae596bSDylan Yudaken 
4012ed29b0b4SJens Axboe 	file = io_uring_get_file(ctx);
4013ed29b0b4SJens Axboe 	if (IS_ERR(file)) {
4014ed29b0b4SJens Axboe 		ret = PTR_ERR(file);
4015ed29b0b4SJens Axboe 		goto err;
4016ed29b0b4SJens Axboe 	}
4017ed29b0b4SJens Axboe 
40186e76ac59SJosh Triplett 	ret = __io_uring_add_tctx_node(ctx);
40196e76ac59SJosh Triplett 	if (ret)
40206e76ac59SJosh Triplett 		goto err_fput;
40216e76ac59SJosh Triplett 	tctx = current->io_uring;
40226e76ac59SJosh Triplett 
4023ed29b0b4SJens Axboe 	/*
4024ed29b0b4SJens Axboe 	 * Install ring fd as the very last thing, so we don't risk someone
4025ed29b0b4SJens Axboe 	 * having closed it before we finish setup
4026ed29b0b4SJens Axboe 	 */
40276e76ac59SJosh Triplett 	if (p->flags & IORING_SETUP_REGISTERED_FD_ONLY)
40286e76ac59SJosh Triplett 		ret = io_ring_add_registered_file(tctx, file, 0, IO_RINGFD_REG_MAX);
40296e76ac59SJosh Triplett 	else
40306e76ac59SJosh Triplett 		ret = io_uring_install_fd(file);
40316e76ac59SJosh Triplett 	if (ret < 0)
40326e76ac59SJosh Triplett 		goto err_fput;
4033ed29b0b4SJens Axboe 
4034ed29b0b4SJens Axboe 	trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
4035ed29b0b4SJens Axboe 	return ret;
4036ed29b0b4SJens Axboe err:
4037ed29b0b4SJens Axboe 	io_ring_ctx_wait_and_kill(ctx);
4038ed29b0b4SJens Axboe 	return ret;
40396e76ac59SJosh Triplett err_fput:
40406e76ac59SJosh Triplett 	fput(file);
40416e76ac59SJosh Triplett 	return ret;
4042ed29b0b4SJens Axboe }
4043ed29b0b4SJens Axboe 
4044ed29b0b4SJens Axboe /*
4045ed29b0b4SJens Axboe  * Sets up an aio uring context, and returns the fd. Applications asks for a
4046ed29b0b4SJens Axboe  * ring size, we return the actual sq/cq ring sizes (among other things) in the
4047ed29b0b4SJens Axboe  * params structure passed in.
4048ed29b0b4SJens Axboe  */
4049ed29b0b4SJens Axboe static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
4050ed29b0b4SJens Axboe {
4051ed29b0b4SJens Axboe 	struct io_uring_params p;
4052ed29b0b4SJens Axboe 	int i;
4053ed29b0b4SJens Axboe 
4054ed29b0b4SJens Axboe 	if (copy_from_user(&p, params, sizeof(p)))
4055ed29b0b4SJens Axboe 		return -EFAULT;
4056ed29b0b4SJens Axboe 	for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
4057ed29b0b4SJens Axboe 		if (p.resv[i])
4058ed29b0b4SJens Axboe 			return -EINVAL;
4059ed29b0b4SJens Axboe 	}
4060ed29b0b4SJens Axboe 
4061ed29b0b4SJens Axboe 	if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
4062ed29b0b4SJens Axboe 			IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
4063ed29b0b4SJens Axboe 			IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ |
4064ed29b0b4SJens Axboe 			IORING_SETUP_R_DISABLED | IORING_SETUP_SUBMIT_ALL |
4065ed29b0b4SJens Axboe 			IORING_SETUP_COOP_TASKRUN | IORING_SETUP_TASKRUN_FLAG |
406697bbdc06SPavel Begunkov 			IORING_SETUP_SQE128 | IORING_SETUP_CQE32 |
406703d89a2dSJens Axboe 			IORING_SETUP_SINGLE_ISSUER | IORING_SETUP_DEFER_TASKRUN |
40686e76ac59SJosh Triplett 			IORING_SETUP_NO_MMAP | IORING_SETUP_REGISTERED_FD_ONLY))
4069ed29b0b4SJens Axboe 		return -EINVAL;
4070ed29b0b4SJens Axboe 
4071ed29b0b4SJens Axboe 	return io_uring_create(entries, &p, params);
4072ed29b0b4SJens Axboe }
4073ed29b0b4SJens Axboe 
4074ed29b0b4SJens Axboe SYSCALL_DEFINE2(io_uring_setup, u32, entries,
4075ed29b0b4SJens Axboe 		struct io_uring_params __user *, params)
4076ed29b0b4SJens Axboe {
4077ed29b0b4SJens Axboe 	return io_uring_setup(entries, params);
4078ed29b0b4SJens Axboe }
4079ed29b0b4SJens Axboe 
4080ed29b0b4SJens Axboe static __cold int io_probe(struct io_ring_ctx *ctx, void __user *arg,
4081ed29b0b4SJens Axboe 			   unsigned nr_args)
4082ed29b0b4SJens Axboe {
4083ed29b0b4SJens Axboe 	struct io_uring_probe *p;
4084ed29b0b4SJens Axboe 	size_t size;
4085ed29b0b4SJens Axboe 	int i, ret;
4086ed29b0b4SJens Axboe 
4087ed29b0b4SJens Axboe 	size = struct_size(p, ops, nr_args);
4088ed29b0b4SJens Axboe 	if (size == SIZE_MAX)
4089ed29b0b4SJens Axboe 		return -EOVERFLOW;
4090ed29b0b4SJens Axboe 	p = kzalloc(size, GFP_KERNEL);
4091ed29b0b4SJens Axboe 	if (!p)
4092ed29b0b4SJens Axboe 		return -ENOMEM;
4093ed29b0b4SJens Axboe 
4094ed29b0b4SJens Axboe 	ret = -EFAULT;
4095ed29b0b4SJens Axboe 	if (copy_from_user(p, arg, size))
4096ed29b0b4SJens Axboe 		goto out;
4097ed29b0b4SJens Axboe 	ret = -EINVAL;
4098ed29b0b4SJens Axboe 	if (memchr_inv(p, 0, size))
4099ed29b0b4SJens Axboe 		goto out;
4100ed29b0b4SJens Axboe 
4101ed29b0b4SJens Axboe 	p->last_op = IORING_OP_LAST - 1;
4102ed29b0b4SJens Axboe 	if (nr_args > IORING_OP_LAST)
4103ed29b0b4SJens Axboe 		nr_args = IORING_OP_LAST;
4104ed29b0b4SJens Axboe 
4105ed29b0b4SJens Axboe 	for (i = 0; i < nr_args; i++) {
4106ed29b0b4SJens Axboe 		p->ops[i].op = i;
4107a7dd2782SBreno Leitao 		if (!io_issue_defs[i].not_supported)
4108ed29b0b4SJens Axboe 			p->ops[i].flags = IO_URING_OP_SUPPORTED;
4109ed29b0b4SJens Axboe 	}
4110ed29b0b4SJens Axboe 	p->ops_len = i;
4111ed29b0b4SJens Axboe 
4112ed29b0b4SJens Axboe 	ret = 0;
4113ed29b0b4SJens Axboe 	if (copy_to_user(arg, p, size))
4114ed29b0b4SJens Axboe 		ret = -EFAULT;
4115ed29b0b4SJens Axboe out:
4116ed29b0b4SJens Axboe 	kfree(p);
4117ed29b0b4SJens Axboe 	return ret;
4118ed29b0b4SJens Axboe }
4119ed29b0b4SJens Axboe 
4120ed29b0b4SJens Axboe static int io_register_personality(struct io_ring_ctx *ctx)
4121ed29b0b4SJens Axboe {
4122ed29b0b4SJens Axboe 	const struct cred *creds;
4123ed29b0b4SJens Axboe 	u32 id;
4124ed29b0b4SJens Axboe 	int ret;
4125ed29b0b4SJens Axboe 
4126ed29b0b4SJens Axboe 	creds = get_current_cred();
4127ed29b0b4SJens Axboe 
4128ed29b0b4SJens Axboe 	ret = xa_alloc_cyclic(&ctx->personalities, &id, (void *)creds,
4129ed29b0b4SJens Axboe 			XA_LIMIT(0, USHRT_MAX), &ctx->pers_next, GFP_KERNEL);
4130ed29b0b4SJens Axboe 	if (ret < 0) {
4131ed29b0b4SJens Axboe 		put_cred(creds);
4132ed29b0b4SJens Axboe 		return ret;
4133ed29b0b4SJens Axboe 	}
4134ed29b0b4SJens Axboe 	return id;
4135ed29b0b4SJens Axboe }
4136ed29b0b4SJens Axboe 
4137ed29b0b4SJens Axboe static __cold int io_register_restrictions(struct io_ring_ctx *ctx,
4138ed29b0b4SJens Axboe 					   void __user *arg, unsigned int nr_args)
4139ed29b0b4SJens Axboe {
4140ed29b0b4SJens Axboe 	struct io_uring_restriction *res;
4141ed29b0b4SJens Axboe 	size_t size;
4142ed29b0b4SJens Axboe 	int i, ret;
4143ed29b0b4SJens Axboe 
4144ed29b0b4SJens Axboe 	/* Restrictions allowed only if rings started disabled */
4145ed29b0b4SJens Axboe 	if (!(ctx->flags & IORING_SETUP_R_DISABLED))
4146ed29b0b4SJens Axboe 		return -EBADFD;
4147ed29b0b4SJens Axboe 
4148ed29b0b4SJens Axboe 	/* We allow only a single restrictions registration */
4149ed29b0b4SJens Axboe 	if (ctx->restrictions.registered)
4150ed29b0b4SJens Axboe 		return -EBUSY;
4151ed29b0b4SJens Axboe 
4152ed29b0b4SJens Axboe 	if (!arg || nr_args > IORING_MAX_RESTRICTIONS)
4153ed29b0b4SJens Axboe 		return -EINVAL;
4154ed29b0b4SJens Axboe 
4155ed29b0b4SJens Axboe 	size = array_size(nr_args, sizeof(*res));
4156ed29b0b4SJens Axboe 	if (size == SIZE_MAX)
4157ed29b0b4SJens Axboe 		return -EOVERFLOW;
4158ed29b0b4SJens Axboe 
4159ed29b0b4SJens Axboe 	res = memdup_user(arg, size);
4160ed29b0b4SJens Axboe 	if (IS_ERR(res))
4161ed29b0b4SJens Axboe 		return PTR_ERR(res);
4162ed29b0b4SJens Axboe 
4163ed29b0b4SJens Axboe 	ret = 0;
4164ed29b0b4SJens Axboe 
4165ed29b0b4SJens Axboe 	for (i = 0; i < nr_args; i++) {
4166ed29b0b4SJens Axboe 		switch (res[i].opcode) {
4167ed29b0b4SJens Axboe 		case IORING_RESTRICTION_REGISTER_OP:
4168ed29b0b4SJens Axboe 			if (res[i].register_op >= IORING_REGISTER_LAST) {
4169ed29b0b4SJens Axboe 				ret = -EINVAL;
4170ed29b0b4SJens Axboe 				goto out;
4171ed29b0b4SJens Axboe 			}
4172ed29b0b4SJens Axboe 
4173ed29b0b4SJens Axboe 			__set_bit(res[i].register_op,
4174ed29b0b4SJens Axboe 				  ctx->restrictions.register_op);
4175ed29b0b4SJens Axboe 			break;
4176ed29b0b4SJens Axboe 		case IORING_RESTRICTION_SQE_OP:
4177ed29b0b4SJens Axboe 			if (res[i].sqe_op >= IORING_OP_LAST) {
4178ed29b0b4SJens Axboe 				ret = -EINVAL;
4179ed29b0b4SJens Axboe 				goto out;
4180ed29b0b4SJens Axboe 			}
4181ed29b0b4SJens Axboe 
4182ed29b0b4SJens Axboe 			__set_bit(res[i].sqe_op, ctx->restrictions.sqe_op);
4183ed29b0b4SJens Axboe 			break;
4184ed29b0b4SJens Axboe 		case IORING_RESTRICTION_SQE_FLAGS_ALLOWED:
4185ed29b0b4SJens Axboe 			ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags;
4186ed29b0b4SJens Axboe 			break;
4187ed29b0b4SJens Axboe 		case IORING_RESTRICTION_SQE_FLAGS_REQUIRED:
4188ed29b0b4SJens Axboe 			ctx->restrictions.sqe_flags_required = res[i].sqe_flags;
4189ed29b0b4SJens Axboe 			break;
4190ed29b0b4SJens Axboe 		default:
4191ed29b0b4SJens Axboe 			ret = -EINVAL;
4192ed29b0b4SJens Axboe 			goto out;
4193ed29b0b4SJens Axboe 		}
4194ed29b0b4SJens Axboe 	}
4195ed29b0b4SJens Axboe 
4196ed29b0b4SJens Axboe out:
4197ed29b0b4SJens Axboe 	/* Reset all restrictions if an error happened */
4198ed29b0b4SJens Axboe 	if (ret != 0)
4199ed29b0b4SJens Axboe 		memset(&ctx->restrictions, 0, sizeof(ctx->restrictions));
4200ed29b0b4SJens Axboe 	else
4201ed29b0b4SJens Axboe 		ctx->restrictions.registered = true;
4202ed29b0b4SJens Axboe 
4203ed29b0b4SJens Axboe 	kfree(res);
4204ed29b0b4SJens Axboe 	return ret;
4205ed29b0b4SJens Axboe }
4206ed29b0b4SJens Axboe 
4207ed29b0b4SJens Axboe static int io_register_enable_rings(struct io_ring_ctx *ctx)
4208ed29b0b4SJens Axboe {
4209ed29b0b4SJens Axboe 	if (!(ctx->flags & IORING_SETUP_R_DISABLED))
4210ed29b0b4SJens Axboe 		return -EBADFD;
4211ed29b0b4SJens Axboe 
4212bca39f39SPavel Begunkov 	if (ctx->flags & IORING_SETUP_SINGLE_ISSUER && !ctx->submitter_task) {
42138579538cSPavel Begunkov 		WRITE_ONCE(ctx->submitter_task, get_task_struct(current));
4214bca39f39SPavel Begunkov 		/*
4215bca39f39SPavel Begunkov 		 * Lazy activation attempts would fail if it was polled before
4216bca39f39SPavel Begunkov 		 * submitter_task is set.
4217bca39f39SPavel Begunkov 		 */
4218bca39f39SPavel Begunkov 		if (wq_has_sleeper(&ctx->poll_wq))
4219bca39f39SPavel Begunkov 			io_activate_pollwq(ctx);
4220bca39f39SPavel Begunkov 	}
42217cae596bSDylan Yudaken 
4222ed29b0b4SJens Axboe 	if (ctx->restrictions.registered)
4223ed29b0b4SJens Axboe 		ctx->restricted = 1;
4224ed29b0b4SJens Axboe 
4225ed29b0b4SJens Axboe 	ctx->flags &= ~IORING_SETUP_R_DISABLED;
4226ed29b0b4SJens Axboe 	if (ctx->sq_data && wq_has_sleeper(&ctx->sq_data->wait))
4227ed29b0b4SJens Axboe 		wake_up(&ctx->sq_data->wait);
4228ed29b0b4SJens Axboe 	return 0;
4229ed29b0b4SJens Axboe }
4230ed29b0b4SJens Axboe 
4231ed29b0b4SJens Axboe static __cold int io_register_iowq_aff(struct io_ring_ctx *ctx,
4232ed29b0b4SJens Axboe 				       void __user *arg, unsigned len)
4233ed29b0b4SJens Axboe {
4234ed29b0b4SJens Axboe 	struct io_uring_task *tctx = current->io_uring;
4235ed29b0b4SJens Axboe 	cpumask_var_t new_mask;
4236ed29b0b4SJens Axboe 	int ret;
4237ed29b0b4SJens Axboe 
4238ed29b0b4SJens Axboe 	if (!tctx || !tctx->io_wq)
4239ed29b0b4SJens Axboe 		return -EINVAL;
4240ed29b0b4SJens Axboe 
4241ed29b0b4SJens Axboe 	if (!alloc_cpumask_var(&new_mask, GFP_KERNEL))
4242ed29b0b4SJens Axboe 		return -ENOMEM;
4243ed29b0b4SJens Axboe 
4244ed29b0b4SJens Axboe 	cpumask_clear(new_mask);
4245ed29b0b4SJens Axboe 	if (len > cpumask_size())
4246ed29b0b4SJens Axboe 		len = cpumask_size();
4247ed29b0b4SJens Axboe 
4248ed29b0b4SJens Axboe 	if (in_compat_syscall()) {
4249ed29b0b4SJens Axboe 		ret = compat_get_bitmap(cpumask_bits(new_mask),
4250ed29b0b4SJens Axboe 					(const compat_ulong_t __user *)arg,
4251ed29b0b4SJens Axboe 					len * 8 /* CHAR_BIT */);
4252ed29b0b4SJens Axboe 	} else {
4253ed29b0b4SJens Axboe 		ret = copy_from_user(new_mask, arg, len);
4254ed29b0b4SJens Axboe 	}
4255ed29b0b4SJens Axboe 
4256ed29b0b4SJens Axboe 	if (ret) {
4257ed29b0b4SJens Axboe 		free_cpumask_var(new_mask);
4258ed29b0b4SJens Axboe 		return -EFAULT;
4259ed29b0b4SJens Axboe 	}
4260ed29b0b4SJens Axboe 
4261ed29b0b4SJens Axboe 	ret = io_wq_cpu_affinity(tctx->io_wq, new_mask);
4262ed29b0b4SJens Axboe 	free_cpumask_var(new_mask);
4263ed29b0b4SJens Axboe 	return ret;
4264ed29b0b4SJens Axboe }
4265ed29b0b4SJens Axboe 
4266ed29b0b4SJens Axboe static __cold int io_unregister_iowq_aff(struct io_ring_ctx *ctx)
4267ed29b0b4SJens Axboe {
4268ed29b0b4SJens Axboe 	struct io_uring_task *tctx = current->io_uring;
4269ed29b0b4SJens Axboe 
4270ed29b0b4SJens Axboe 	if (!tctx || !tctx->io_wq)
4271ed29b0b4SJens Axboe 		return -EINVAL;
4272ed29b0b4SJens Axboe 
4273ed29b0b4SJens Axboe 	return io_wq_cpu_affinity(tctx->io_wq, NULL);
4274ed29b0b4SJens Axboe }
4275ed29b0b4SJens Axboe 
4276ed29b0b4SJens Axboe static __cold int io_register_iowq_max_workers(struct io_ring_ctx *ctx,
4277ed29b0b4SJens Axboe 					       void __user *arg)
4278ed29b0b4SJens Axboe 	__must_hold(&ctx->uring_lock)
4279ed29b0b4SJens Axboe {
4280ed29b0b4SJens Axboe 	struct io_tctx_node *node;
4281ed29b0b4SJens Axboe 	struct io_uring_task *tctx = NULL;
4282ed29b0b4SJens Axboe 	struct io_sq_data *sqd = NULL;
4283ed29b0b4SJens Axboe 	__u32 new_count[2];
4284ed29b0b4SJens Axboe 	int i, ret;
4285ed29b0b4SJens Axboe 
4286ed29b0b4SJens Axboe 	if (copy_from_user(new_count, arg, sizeof(new_count)))
4287ed29b0b4SJens Axboe 		return -EFAULT;
4288ed29b0b4SJens Axboe 	for (i = 0; i < ARRAY_SIZE(new_count); i++)
4289ed29b0b4SJens Axboe 		if (new_count[i] > INT_MAX)
4290ed29b0b4SJens Axboe 			return -EINVAL;
4291ed29b0b4SJens Axboe 
4292ed29b0b4SJens Axboe 	if (ctx->flags & IORING_SETUP_SQPOLL) {
4293ed29b0b4SJens Axboe 		sqd = ctx->sq_data;
4294ed29b0b4SJens Axboe 		if (sqd) {
4295ed29b0b4SJens Axboe 			/*
4296ed29b0b4SJens Axboe 			 * Observe the correct sqd->lock -> ctx->uring_lock
4297ed29b0b4SJens Axboe 			 * ordering. Fine to drop uring_lock here, we hold
4298ed29b0b4SJens Axboe 			 * a ref to the ctx.
4299ed29b0b4SJens Axboe 			 */
4300ed29b0b4SJens Axboe 			refcount_inc(&sqd->refs);
4301ed29b0b4SJens Axboe 			mutex_unlock(&ctx->uring_lock);
4302ed29b0b4SJens Axboe 			mutex_lock(&sqd->lock);
4303ed29b0b4SJens Axboe 			mutex_lock(&ctx->uring_lock);
4304ed29b0b4SJens Axboe 			if (sqd->thread)
4305ed29b0b4SJens Axboe 				tctx = sqd->thread->io_uring;
4306ed29b0b4SJens Axboe 		}
4307ed29b0b4SJens Axboe 	} else {
4308ed29b0b4SJens Axboe 		tctx = current->io_uring;
4309ed29b0b4SJens Axboe 	}
4310ed29b0b4SJens Axboe 
4311ed29b0b4SJens Axboe 	BUILD_BUG_ON(sizeof(new_count) != sizeof(ctx->iowq_limits));
4312ed29b0b4SJens Axboe 
4313ed29b0b4SJens Axboe 	for (i = 0; i < ARRAY_SIZE(new_count); i++)
4314ed29b0b4SJens Axboe 		if (new_count[i])
4315ed29b0b4SJens Axboe 			ctx->iowq_limits[i] = new_count[i];
4316ed29b0b4SJens Axboe 	ctx->iowq_limits_set = true;
4317ed29b0b4SJens Axboe 
4318ed29b0b4SJens Axboe 	if (tctx && tctx->io_wq) {
4319ed29b0b4SJens Axboe 		ret = io_wq_max_workers(tctx->io_wq, new_count);
4320ed29b0b4SJens Axboe 		if (ret)
4321ed29b0b4SJens Axboe 			goto err;
4322ed29b0b4SJens Axboe 	} else {
4323ed29b0b4SJens Axboe 		memset(new_count, 0, sizeof(new_count));
4324ed29b0b4SJens Axboe 	}
4325ed29b0b4SJens Axboe 
4326ed29b0b4SJens Axboe 	if (sqd) {
4327ed29b0b4SJens Axboe 		mutex_unlock(&sqd->lock);
4328ed29b0b4SJens Axboe 		io_put_sq_data(sqd);
4329ed29b0b4SJens Axboe 	}
4330ed29b0b4SJens Axboe 
4331ed29b0b4SJens Axboe 	if (copy_to_user(arg, new_count, sizeof(new_count)))
4332ed29b0b4SJens Axboe 		return -EFAULT;
4333ed29b0b4SJens Axboe 
4334ed29b0b4SJens Axboe 	/* that's it for SQPOLL, only the SQPOLL task creates requests */
4335ed29b0b4SJens Axboe 	if (sqd)
4336ed29b0b4SJens Axboe 		return 0;
4337ed29b0b4SJens Axboe 
4338ed29b0b4SJens Axboe 	/* now propagate the restriction to all registered users */
4339ed29b0b4SJens Axboe 	list_for_each_entry(node, &ctx->tctx_list, ctx_node) {
4340ed29b0b4SJens Axboe 		struct io_uring_task *tctx = node->task->io_uring;
4341ed29b0b4SJens Axboe 
4342ed29b0b4SJens Axboe 		if (WARN_ON_ONCE(!tctx->io_wq))
4343ed29b0b4SJens Axboe 			continue;
4344ed29b0b4SJens Axboe 
4345ed29b0b4SJens Axboe 		for (i = 0; i < ARRAY_SIZE(new_count); i++)
4346ed29b0b4SJens Axboe 			new_count[i] = ctx->iowq_limits[i];
4347ed29b0b4SJens Axboe 		/* ignore errors, it always returns zero anyway */
4348ed29b0b4SJens Axboe 		(void)io_wq_max_workers(tctx->io_wq, new_count);
4349ed29b0b4SJens Axboe 	}
4350ed29b0b4SJens Axboe 	return 0;
4351ed29b0b4SJens Axboe err:
4352ed29b0b4SJens Axboe 	if (sqd) {
4353ed29b0b4SJens Axboe 		mutex_unlock(&sqd->lock);
4354ed29b0b4SJens Axboe 		io_put_sq_data(sqd);
4355ed29b0b4SJens Axboe 	}
4356ed29b0b4SJens Axboe 	return ret;
4357ed29b0b4SJens Axboe }
4358ed29b0b4SJens Axboe 
4359ed29b0b4SJens Axboe static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
4360ed29b0b4SJens Axboe 			       void __user *arg, unsigned nr_args)
4361ed29b0b4SJens Axboe 	__releases(ctx->uring_lock)
4362ed29b0b4SJens Axboe 	__acquires(ctx->uring_lock)
4363ed29b0b4SJens Axboe {
4364ed29b0b4SJens Axboe 	int ret;
4365ed29b0b4SJens Axboe 
4366ed29b0b4SJens Axboe 	/*
4367fbb8bb02SPavel Begunkov 	 * We don't quiesce the refs for register anymore and so it can't be
4368fbb8bb02SPavel Begunkov 	 * dying as we're holding a file ref here.
4369ed29b0b4SJens Axboe 	 */
4370fbb8bb02SPavel Begunkov 	if (WARN_ON_ONCE(percpu_ref_is_dying(&ctx->refs)))
4371ed29b0b4SJens Axboe 		return -ENXIO;
4372ed29b0b4SJens Axboe 
4373d7cce96cSPavel Begunkov 	if (ctx->submitter_task && ctx->submitter_task != current)
4374d7cce96cSPavel Begunkov 		return -EEXIST;
4375d7cce96cSPavel Begunkov 
4376ed29b0b4SJens Axboe 	if (ctx->restricted) {
4377ed29b0b4SJens Axboe 		opcode = array_index_nospec(opcode, IORING_REGISTER_LAST);
4378ed29b0b4SJens Axboe 		if (!test_bit(opcode, ctx->restrictions.register_op))
4379ed29b0b4SJens Axboe 			return -EACCES;
4380ed29b0b4SJens Axboe 	}
4381ed29b0b4SJens Axboe 
4382ed29b0b4SJens Axboe 	switch (opcode) {
4383ed29b0b4SJens Axboe 	case IORING_REGISTER_BUFFERS:
4384ed29b0b4SJens Axboe 		ret = -EFAULT;
4385ed29b0b4SJens Axboe 		if (!arg)
4386ed29b0b4SJens Axboe 			break;
4387ed29b0b4SJens Axboe 		ret = io_sqe_buffers_register(ctx, arg, nr_args, NULL);
4388ed29b0b4SJens Axboe 		break;
4389ed29b0b4SJens Axboe 	case IORING_UNREGISTER_BUFFERS:
4390ed29b0b4SJens Axboe 		ret = -EINVAL;
4391ed29b0b4SJens Axboe 		if (arg || nr_args)
4392ed29b0b4SJens Axboe 			break;
4393ed29b0b4SJens Axboe 		ret = io_sqe_buffers_unregister(ctx);
4394ed29b0b4SJens Axboe 		break;
4395ed29b0b4SJens Axboe 	case IORING_REGISTER_FILES:
4396ed29b0b4SJens Axboe 		ret = -EFAULT;
4397ed29b0b4SJens Axboe 		if (!arg)
4398ed29b0b4SJens Axboe 			break;
4399ed29b0b4SJens Axboe 		ret = io_sqe_files_register(ctx, arg, nr_args, NULL);
4400ed29b0b4SJens Axboe 		break;
4401ed29b0b4SJens Axboe 	case IORING_UNREGISTER_FILES:
4402ed29b0b4SJens Axboe 		ret = -EINVAL;
4403ed29b0b4SJens Axboe 		if (arg || nr_args)
4404ed29b0b4SJens Axboe 			break;
4405ed29b0b4SJens Axboe 		ret = io_sqe_files_unregister(ctx);
4406ed29b0b4SJens Axboe 		break;
4407ed29b0b4SJens Axboe 	case IORING_REGISTER_FILES_UPDATE:
4408ed29b0b4SJens Axboe 		ret = io_register_files_update(ctx, arg, nr_args);
4409ed29b0b4SJens Axboe 		break;
4410ed29b0b4SJens Axboe 	case IORING_REGISTER_EVENTFD:
4411ed29b0b4SJens Axboe 		ret = -EINVAL;
4412ed29b0b4SJens Axboe 		if (nr_args != 1)
4413ed29b0b4SJens Axboe 			break;
4414ed29b0b4SJens Axboe 		ret = io_eventfd_register(ctx, arg, 0);
4415ed29b0b4SJens Axboe 		break;
4416ed29b0b4SJens Axboe 	case IORING_REGISTER_EVENTFD_ASYNC:
4417ed29b0b4SJens Axboe 		ret = -EINVAL;
4418ed29b0b4SJens Axboe 		if (nr_args != 1)
4419ed29b0b4SJens Axboe 			break;
4420ed29b0b4SJens Axboe 		ret = io_eventfd_register(ctx, arg, 1);
4421ed29b0b4SJens Axboe 		break;
4422ed29b0b4SJens Axboe 	case IORING_UNREGISTER_EVENTFD:
4423ed29b0b4SJens Axboe 		ret = -EINVAL;
4424ed29b0b4SJens Axboe 		if (arg || nr_args)
4425ed29b0b4SJens Axboe 			break;
4426ed29b0b4SJens Axboe 		ret = io_eventfd_unregister(ctx);
4427ed29b0b4SJens Axboe 		break;
4428ed29b0b4SJens Axboe 	case IORING_REGISTER_PROBE:
4429ed29b0b4SJens Axboe 		ret = -EINVAL;
4430ed29b0b4SJens Axboe 		if (!arg || nr_args > 256)
4431ed29b0b4SJens Axboe 			break;
4432ed29b0b4SJens Axboe 		ret = io_probe(ctx, arg, nr_args);
4433ed29b0b4SJens Axboe 		break;
4434ed29b0b4SJens Axboe 	case IORING_REGISTER_PERSONALITY:
4435ed29b0b4SJens Axboe 		ret = -EINVAL;
4436ed29b0b4SJens Axboe 		if (arg || nr_args)
4437ed29b0b4SJens Axboe 			break;
4438ed29b0b4SJens Axboe 		ret = io_register_personality(ctx);
4439ed29b0b4SJens Axboe 		break;
4440ed29b0b4SJens Axboe 	case IORING_UNREGISTER_PERSONALITY:
4441ed29b0b4SJens Axboe 		ret = -EINVAL;
4442ed29b0b4SJens Axboe 		if (arg)
4443ed29b0b4SJens Axboe 			break;
4444ed29b0b4SJens Axboe 		ret = io_unregister_personality(ctx, nr_args);
4445ed29b0b4SJens Axboe 		break;
4446ed29b0b4SJens Axboe 	case IORING_REGISTER_ENABLE_RINGS:
4447ed29b0b4SJens Axboe 		ret = -EINVAL;
4448ed29b0b4SJens Axboe 		if (arg || nr_args)
4449ed29b0b4SJens Axboe 			break;
4450ed29b0b4SJens Axboe 		ret = io_register_enable_rings(ctx);
4451ed29b0b4SJens Axboe 		break;
4452ed29b0b4SJens Axboe 	case IORING_REGISTER_RESTRICTIONS:
4453ed29b0b4SJens Axboe 		ret = io_register_restrictions(ctx, arg, nr_args);
4454ed29b0b4SJens Axboe 		break;
4455ed29b0b4SJens Axboe 	case IORING_REGISTER_FILES2:
4456ed29b0b4SJens Axboe 		ret = io_register_rsrc(ctx, arg, nr_args, IORING_RSRC_FILE);
4457ed29b0b4SJens Axboe 		break;
4458ed29b0b4SJens Axboe 	case IORING_REGISTER_FILES_UPDATE2:
4459ed29b0b4SJens Axboe 		ret = io_register_rsrc_update(ctx, arg, nr_args,
4460ed29b0b4SJens Axboe 					      IORING_RSRC_FILE);
4461ed29b0b4SJens Axboe 		break;
4462ed29b0b4SJens Axboe 	case IORING_REGISTER_BUFFERS2:
4463ed29b0b4SJens Axboe 		ret = io_register_rsrc(ctx, arg, nr_args, IORING_RSRC_BUFFER);
4464ed29b0b4SJens Axboe 		break;
4465ed29b0b4SJens Axboe 	case IORING_REGISTER_BUFFERS_UPDATE:
4466ed29b0b4SJens Axboe 		ret = io_register_rsrc_update(ctx, arg, nr_args,
4467ed29b0b4SJens Axboe 					      IORING_RSRC_BUFFER);
4468ed29b0b4SJens Axboe 		break;
4469ed29b0b4SJens Axboe 	case IORING_REGISTER_IOWQ_AFF:
4470ed29b0b4SJens Axboe 		ret = -EINVAL;
4471ed29b0b4SJens Axboe 		if (!arg || !nr_args)
4472ed29b0b4SJens Axboe 			break;
4473ed29b0b4SJens Axboe 		ret = io_register_iowq_aff(ctx, arg, nr_args);
4474ed29b0b4SJens Axboe 		break;
4475ed29b0b4SJens Axboe 	case IORING_UNREGISTER_IOWQ_AFF:
4476ed29b0b4SJens Axboe 		ret = -EINVAL;
4477ed29b0b4SJens Axboe 		if (arg || nr_args)
4478ed29b0b4SJens Axboe 			break;
4479ed29b0b4SJens Axboe 		ret = io_unregister_iowq_aff(ctx);
4480ed29b0b4SJens Axboe 		break;
4481ed29b0b4SJens Axboe 	case IORING_REGISTER_IOWQ_MAX_WORKERS:
4482ed29b0b4SJens Axboe 		ret = -EINVAL;
4483ed29b0b4SJens Axboe 		if (!arg || nr_args != 2)
4484ed29b0b4SJens Axboe 			break;
4485ed29b0b4SJens Axboe 		ret = io_register_iowq_max_workers(ctx, arg);
4486ed29b0b4SJens Axboe 		break;
4487ed29b0b4SJens Axboe 	case IORING_REGISTER_RING_FDS:
4488ed29b0b4SJens Axboe 		ret = io_ringfd_register(ctx, arg, nr_args);
4489ed29b0b4SJens Axboe 		break;
4490ed29b0b4SJens Axboe 	case IORING_UNREGISTER_RING_FDS:
4491ed29b0b4SJens Axboe 		ret = io_ringfd_unregister(ctx, arg, nr_args);
4492ed29b0b4SJens Axboe 		break;
4493ed29b0b4SJens Axboe 	case IORING_REGISTER_PBUF_RING:
4494ed29b0b4SJens Axboe 		ret = -EINVAL;
4495ed29b0b4SJens Axboe 		if (!arg || nr_args != 1)
4496ed29b0b4SJens Axboe 			break;
4497ed29b0b4SJens Axboe 		ret = io_register_pbuf_ring(ctx, arg);
4498ed29b0b4SJens Axboe 		break;
4499ed29b0b4SJens Axboe 	case IORING_UNREGISTER_PBUF_RING:
4500ed29b0b4SJens Axboe 		ret = -EINVAL;
4501ed29b0b4SJens Axboe 		if (!arg || nr_args != 1)
4502ed29b0b4SJens Axboe 			break;
4503ed29b0b4SJens Axboe 		ret = io_unregister_pbuf_ring(ctx, arg);
4504ed29b0b4SJens Axboe 		break;
450578a861b9SJens Axboe 	case IORING_REGISTER_SYNC_CANCEL:
450678a861b9SJens Axboe 		ret = -EINVAL;
450778a861b9SJens Axboe 		if (!arg || nr_args != 1)
450878a861b9SJens Axboe 			break;
450978a861b9SJens Axboe 		ret = io_sync_cancel(ctx, arg);
451078a861b9SJens Axboe 		break;
45116e73dffbSPavel Begunkov 	case IORING_REGISTER_FILE_ALLOC_RANGE:
45126e73dffbSPavel Begunkov 		ret = -EINVAL;
45136e73dffbSPavel Begunkov 		if (!arg || nr_args)
45146e73dffbSPavel Begunkov 			break;
45156e73dffbSPavel Begunkov 		ret = io_register_file_alloc_range(ctx, arg);
45166e73dffbSPavel Begunkov 		break;
4517ed29b0b4SJens Axboe 	default:
4518ed29b0b4SJens Axboe 		ret = -EINVAL;
4519ed29b0b4SJens Axboe 		break;
4520ed29b0b4SJens Axboe 	}
4521ed29b0b4SJens Axboe 
4522ed29b0b4SJens Axboe 	return ret;
4523ed29b0b4SJens Axboe }
4524ed29b0b4SJens Axboe 
4525ed29b0b4SJens Axboe SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
4526ed29b0b4SJens Axboe 		void __user *, arg, unsigned int, nr_args)
4527ed29b0b4SJens Axboe {
4528ed29b0b4SJens Axboe 	struct io_ring_ctx *ctx;
4529ed29b0b4SJens Axboe 	long ret = -EBADF;
4530ed29b0b4SJens Axboe 	struct fd f;
45317d3fd88dSJosh Triplett 	bool use_registered_ring;
45327d3fd88dSJosh Triplett 
45337d3fd88dSJosh Triplett 	use_registered_ring = !!(opcode & IORING_REGISTER_USE_REGISTERED_RING);
45347d3fd88dSJosh Triplett 	opcode &= ~IORING_REGISTER_USE_REGISTERED_RING;
4535ed29b0b4SJens Axboe 
453634319084SJens Axboe 	if (opcode >= IORING_REGISTER_LAST)
453734319084SJens Axboe 		return -EINVAL;
453834319084SJens Axboe 
45397d3fd88dSJosh Triplett 	if (use_registered_ring) {
45407d3fd88dSJosh Triplett 		/*
45417d3fd88dSJosh Triplett 		 * Ring fd has been registered via IORING_REGISTER_RING_FDS, we
45427d3fd88dSJosh Triplett 		 * need only dereference our task private array to find it.
45437d3fd88dSJosh Triplett 		 */
45447d3fd88dSJosh Triplett 		struct io_uring_task *tctx = current->io_uring;
4545ed29b0b4SJens Axboe 
45467d3fd88dSJosh Triplett 		if (unlikely(!tctx || fd >= IO_RINGFD_REG_MAX))
45477d3fd88dSJosh Triplett 			return -EINVAL;
45487d3fd88dSJosh Triplett 		fd = array_index_nospec(fd, IO_RINGFD_REG_MAX);
45497d3fd88dSJosh Triplett 		f.file = tctx->registered_rings[fd];
45507d3fd88dSJosh Triplett 		f.flags = 0;
45517d3fd88dSJosh Triplett 		if (unlikely(!f.file))
45527d3fd88dSJosh Triplett 			return -EBADF;
45537d3fd88dSJosh Triplett 	} else {
45547d3fd88dSJosh Triplett 		f = fdget(fd);
45557d3fd88dSJosh Triplett 		if (unlikely(!f.file))
45567d3fd88dSJosh Triplett 			return -EBADF;
4557ed29b0b4SJens Axboe 		ret = -EOPNOTSUPP;
4558e5550a14SJens Axboe 		if (!io_is_uring_fops(f.file))
4559ed29b0b4SJens Axboe 			goto out_fput;
45607d3fd88dSJosh Triplett 	}
4561ed29b0b4SJens Axboe 
4562ed29b0b4SJens Axboe 	ctx = f.file->private_data;
4563ed29b0b4SJens Axboe 
4564ed29b0b4SJens Axboe 	mutex_lock(&ctx->uring_lock);
4565ed29b0b4SJens Axboe 	ret = __io_uring_register(ctx, opcode, arg, nr_args);
4566ed29b0b4SJens Axboe 	mutex_unlock(&ctx->uring_lock);
4567ed29b0b4SJens Axboe 	trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs, ret);
4568ed29b0b4SJens Axboe out_fput:
4569ed29b0b4SJens Axboe 	fdput(f);
4570ed29b0b4SJens Axboe 	return ret;
4571ed29b0b4SJens Axboe }
4572ed29b0b4SJens Axboe 
4573ed29b0b4SJens Axboe static int __init io_uring_init(void)
4574ed29b0b4SJens Axboe {
45759c71d39aSStefan Metzmacher #define __BUILD_BUG_VERIFY_OFFSET_SIZE(stype, eoffset, esize, ename) do { \
4576ed29b0b4SJens Axboe 	BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
45779c71d39aSStefan Metzmacher 	BUILD_BUG_ON(sizeof_field(stype, ename) != esize); \
4578ed29b0b4SJens Axboe } while (0)
4579ed29b0b4SJens Axboe 
4580ed29b0b4SJens Axboe #define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
45819c71d39aSStefan Metzmacher 	__BUILD_BUG_VERIFY_OFFSET_SIZE(struct io_uring_sqe, eoffset, sizeof(etype), ename)
45829c71d39aSStefan Metzmacher #define BUILD_BUG_SQE_ELEM_SIZE(eoffset, esize, ename) \
45839c71d39aSStefan Metzmacher 	__BUILD_BUG_VERIFY_OFFSET_SIZE(struct io_uring_sqe, eoffset, esize, ename)
4584ed29b0b4SJens Axboe 	BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
4585ed29b0b4SJens Axboe 	BUILD_BUG_SQE_ELEM(0,  __u8,   opcode);
4586ed29b0b4SJens Axboe 	BUILD_BUG_SQE_ELEM(1,  __u8,   flags);
4587ed29b0b4SJens Axboe 	BUILD_BUG_SQE_ELEM(2,  __u16,  ioprio);
4588ed29b0b4SJens Axboe 	BUILD_BUG_SQE_ELEM(4,  __s32,  fd);
4589ed29b0b4SJens Axboe 	BUILD_BUG_SQE_ELEM(8,  __u64,  off);
4590ed29b0b4SJens Axboe 	BUILD_BUG_SQE_ELEM(8,  __u64,  addr2);
45919c71d39aSStefan Metzmacher 	BUILD_BUG_SQE_ELEM(8,  __u32,  cmd_op);
45929c71d39aSStefan Metzmacher 	BUILD_BUG_SQE_ELEM(12, __u32, __pad1);
4593ed29b0b4SJens Axboe 	BUILD_BUG_SQE_ELEM(16, __u64,  addr);
4594ed29b0b4SJens Axboe 	BUILD_BUG_SQE_ELEM(16, __u64,  splice_off_in);
4595ed29b0b4SJens Axboe 	BUILD_BUG_SQE_ELEM(24, __u32,  len);
4596ed29b0b4SJens Axboe 	BUILD_BUG_SQE_ELEM(28,     __kernel_rwf_t, rw_flags);
4597ed29b0b4SJens Axboe 	BUILD_BUG_SQE_ELEM(28, /* compat */   int, rw_flags);
4598ed29b0b4SJens Axboe 	BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
4599ed29b0b4SJens Axboe 	BUILD_BUG_SQE_ELEM(28, __u32,  fsync_flags);
4600ed29b0b4SJens Axboe 	BUILD_BUG_SQE_ELEM(28, /* compat */ __u16,  poll_events);
4601ed29b0b4SJens Axboe 	BUILD_BUG_SQE_ELEM(28, __u32,  poll32_events);
4602ed29b0b4SJens Axboe 	BUILD_BUG_SQE_ELEM(28, __u32,  sync_range_flags);
4603ed29b0b4SJens Axboe 	BUILD_BUG_SQE_ELEM(28, __u32,  msg_flags);
4604ed29b0b4SJens Axboe 	BUILD_BUG_SQE_ELEM(28, __u32,  timeout_flags);
4605ed29b0b4SJens Axboe 	BUILD_BUG_SQE_ELEM(28, __u32,  accept_flags);
4606ed29b0b4SJens Axboe 	BUILD_BUG_SQE_ELEM(28, __u32,  cancel_flags);
4607ed29b0b4SJens Axboe 	BUILD_BUG_SQE_ELEM(28, __u32,  open_flags);
4608ed29b0b4SJens Axboe 	BUILD_BUG_SQE_ELEM(28, __u32,  statx_flags);
4609ed29b0b4SJens Axboe 	BUILD_BUG_SQE_ELEM(28, __u32,  fadvise_advice);
4610ed29b0b4SJens Axboe 	BUILD_BUG_SQE_ELEM(28, __u32,  splice_flags);
46119c71d39aSStefan Metzmacher 	BUILD_BUG_SQE_ELEM(28, __u32,  rename_flags);
46129c71d39aSStefan Metzmacher 	BUILD_BUG_SQE_ELEM(28, __u32,  unlink_flags);
46139c71d39aSStefan Metzmacher 	BUILD_BUG_SQE_ELEM(28, __u32,  hardlink_flags);
46149c71d39aSStefan Metzmacher 	BUILD_BUG_SQE_ELEM(28, __u32,  xattr_flags);
46159c71d39aSStefan Metzmacher 	BUILD_BUG_SQE_ELEM(28, __u32,  msg_ring_flags);
4616ed29b0b4SJens Axboe 	BUILD_BUG_SQE_ELEM(32, __u64,  user_data);
4617ed29b0b4SJens Axboe 	BUILD_BUG_SQE_ELEM(40, __u16,  buf_index);
4618ed29b0b4SJens Axboe 	BUILD_BUG_SQE_ELEM(40, __u16,  buf_group);
4619ed29b0b4SJens Axboe 	BUILD_BUG_SQE_ELEM(42, __u16,  personality);
4620ed29b0b4SJens Axboe 	BUILD_BUG_SQE_ELEM(44, __s32,  splice_fd_in);
4621ed29b0b4SJens Axboe 	BUILD_BUG_SQE_ELEM(44, __u32,  file_index);
4622b48c312bSPavel Begunkov 	BUILD_BUG_SQE_ELEM(44, __u16,  addr_len);
4623b48c312bSPavel Begunkov 	BUILD_BUG_SQE_ELEM(46, __u16,  __pad3[0]);
4624ed29b0b4SJens Axboe 	BUILD_BUG_SQE_ELEM(48, __u64,  addr3);
46259c71d39aSStefan Metzmacher 	BUILD_BUG_SQE_ELEM_SIZE(48, 0, cmd);
46269c71d39aSStefan Metzmacher 	BUILD_BUG_SQE_ELEM(56, __u64,  __pad2);
4627ed29b0b4SJens Axboe 
4628ed29b0b4SJens Axboe 	BUILD_BUG_ON(sizeof(struct io_uring_files_update) !=
4629ed29b0b4SJens Axboe 		     sizeof(struct io_uring_rsrc_update));
4630ed29b0b4SJens Axboe 	BUILD_BUG_ON(sizeof(struct io_uring_rsrc_update) >
4631ed29b0b4SJens Axboe 		     sizeof(struct io_uring_rsrc_update2));
4632ed29b0b4SJens Axboe 
4633ed29b0b4SJens Axboe 	/* ->buf_index is u16 */
4634ed29b0b4SJens Axboe 	BUILD_BUG_ON(offsetof(struct io_uring_buf_ring, bufs) != 0);
4635ed29b0b4SJens Axboe 	BUILD_BUG_ON(offsetof(struct io_uring_buf, resv) !=
4636ed29b0b4SJens Axboe 		     offsetof(struct io_uring_buf_ring, tail));
4637ed29b0b4SJens Axboe 
4638ed29b0b4SJens Axboe 	/* should fit into one byte */
4639ed29b0b4SJens Axboe 	BUILD_BUG_ON(SQE_VALID_FLAGS >= (1 << 8));
4640ed29b0b4SJens Axboe 	BUILD_BUG_ON(SQE_COMMON_FLAGS >= (1 << 8));
4641ed29b0b4SJens Axboe 	BUILD_BUG_ON((SQE_VALID_FLAGS | SQE_COMMON_FLAGS) != SQE_VALID_FLAGS);
4642ed29b0b4SJens Axboe 
4643ed29b0b4SJens Axboe 	BUILD_BUG_ON(__REQ_F_LAST_BIT > 8 * sizeof(int));
4644ed29b0b4SJens Axboe 
4645ed29b0b4SJens Axboe 	BUILD_BUG_ON(sizeof(atomic_t) != sizeof(u32));
4646ed29b0b4SJens Axboe 
4647d9b57aa3SJens Axboe 	io_uring_optable_init();
4648ed29b0b4SJens Axboe 
4649ed29b0b4SJens Axboe 	req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC |
46508751d154SPavel Begunkov 				SLAB_ACCOUNT | SLAB_TYPESAFE_BY_RCU);
4651ed29b0b4SJens Axboe 	return 0;
4652ed29b0b4SJens Axboe };
4653ed29b0b4SJens Axboe __initcall(io_uring_init);
4654