xref: /openbmc/linux/io_uring/poll.c (revision 4cbc5e93)
1329061d3SJens Axboe // SPDX-License-Identifier: GPL-2.0
2329061d3SJens Axboe #include <linux/kernel.h>
3329061d3SJens Axboe #include <linux/errno.h>
4329061d3SJens Axboe #include <linux/fs.h>
5329061d3SJens Axboe #include <linux/file.h>
6329061d3SJens Axboe #include <linux/mm.h>
7329061d3SJens Axboe #include <linux/slab.h>
8329061d3SJens Axboe #include <linux/poll.h>
9329061d3SJens Axboe #include <linux/hashtable.h>
10329061d3SJens Axboe #include <linux/io_uring.h>
11329061d3SJens Axboe 
12329061d3SJens Axboe #include <trace/events/io_uring.h>
13329061d3SJens Axboe 
14329061d3SJens Axboe #include <uapi/linux/io_uring.h>
15329061d3SJens Axboe 
16329061d3SJens Axboe #include "io_uring.h"
17329061d3SJens Axboe #include "refs.h"
18329061d3SJens Axboe #include "opdef.h"
193b77495aSJens Axboe #include "kbuf.h"
20329061d3SJens Axboe #include "poll.h"
2138513c46SHao Xu #include "cancel.h"
22329061d3SJens Axboe 
23329061d3SJens Axboe struct io_poll_update {
24329061d3SJens Axboe 	struct file			*file;
25329061d3SJens Axboe 	u64				old_user_data;
26329061d3SJens Axboe 	u64				new_user_data;
27329061d3SJens Axboe 	__poll_t			events;
28329061d3SJens Axboe 	bool				update_events;
29329061d3SJens Axboe 	bool				update_user_data;
30329061d3SJens Axboe };
31329061d3SJens Axboe 
32329061d3SJens Axboe struct io_poll_table {
33329061d3SJens Axboe 	struct poll_table_struct pt;
34329061d3SJens Axboe 	struct io_kiocb *req;
35329061d3SJens Axboe 	int nr_entries;
36329061d3SJens Axboe 	int error;
3749f1c68eSPavel Begunkov 	bool owning;
38063a0079SPavel Begunkov 	/* output value, set only if arm poll returns >0 */
39063a0079SPavel Begunkov 	__poll_t result_mask;
40329061d3SJens Axboe };
41329061d3SJens Axboe 
42329061d3SJens Axboe #define IO_POLL_CANCEL_FLAG	BIT(31)
43a26a35e9SPavel Begunkov #define IO_POLL_RETRY_FLAG	BIT(30)
44a26a35e9SPavel Begunkov #define IO_POLL_REF_MASK	GENMASK(29, 0)
45a26a35e9SPavel Begunkov 
46a26a35e9SPavel Begunkov /*
47a26a35e9SPavel Begunkov  * We usually have 1-2 refs taken, 128 is more than enough and we want to
48a26a35e9SPavel Begunkov  * maximise the margin between this amount and the moment when it overflows.
49a26a35e9SPavel Begunkov  */
50a26a35e9SPavel Begunkov #define IO_POLL_REF_BIAS	128
51329061d3SJens Axboe 
520638cd7bSPavel Begunkov #define IO_WQE_F_DOUBLE		1
530638cd7bSPavel Begunkov 
541947ddf9SJens Axboe static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
551947ddf9SJens Axboe 			void *key);
561947ddf9SJens Axboe 
wqe_to_req(struct wait_queue_entry * wqe)570638cd7bSPavel Begunkov static inline struct io_kiocb *wqe_to_req(struct wait_queue_entry *wqe)
580638cd7bSPavel Begunkov {
590638cd7bSPavel Begunkov 	unsigned long priv = (unsigned long)wqe->private;
600638cd7bSPavel Begunkov 
610638cd7bSPavel Begunkov 	return (struct io_kiocb *)(priv & ~IO_WQE_F_DOUBLE);
620638cd7bSPavel Begunkov }
630638cd7bSPavel Begunkov 
wqe_is_double(struct wait_queue_entry * wqe)640638cd7bSPavel Begunkov static inline bool wqe_is_double(struct wait_queue_entry *wqe)
650638cd7bSPavel Begunkov {
660638cd7bSPavel Begunkov 	unsigned long priv = (unsigned long)wqe->private;
670638cd7bSPavel Begunkov 
680638cd7bSPavel Begunkov 	return priv & IO_WQE_F_DOUBLE;
690638cd7bSPavel Begunkov }
700638cd7bSPavel Begunkov 
io_poll_get_ownership_slowpath(struct io_kiocb * req)71a26a35e9SPavel Begunkov static bool io_poll_get_ownership_slowpath(struct io_kiocb *req)
72a26a35e9SPavel Begunkov {
73a26a35e9SPavel Begunkov 	int v;
74a26a35e9SPavel Begunkov 
75a26a35e9SPavel Begunkov 	/*
76a26a35e9SPavel Begunkov 	 * poll_refs are already elevated and we don't have much hope for
77a26a35e9SPavel Begunkov 	 * grabbing the ownership. Instead of incrementing set a retry flag
78a26a35e9SPavel Begunkov 	 * to notify the loop that there might have been some change.
79a26a35e9SPavel Begunkov 	 */
80a26a35e9SPavel Begunkov 	v = atomic_fetch_or(IO_POLL_RETRY_FLAG, &req->poll_refs);
81a26a35e9SPavel Begunkov 	if (v & IO_POLL_REF_MASK)
82a26a35e9SPavel Begunkov 		return false;
83a26a35e9SPavel Begunkov 	return !(atomic_fetch_inc(&req->poll_refs) & IO_POLL_REF_MASK);
84a26a35e9SPavel Begunkov }
85a26a35e9SPavel Begunkov 
86329061d3SJens Axboe /*
87329061d3SJens Axboe  * If refs part of ->poll_refs (see IO_POLL_REF_MASK) is 0, it's free. We can
88329061d3SJens Axboe  * bump it and acquire ownership. It's disallowed to modify requests while not
89329061d3SJens Axboe  * owning it, that prevents from races for enqueueing task_work's and b/w
90329061d3SJens Axboe  * arming poll and wakeups.
91329061d3SJens Axboe  */
io_poll_get_ownership(struct io_kiocb * req)92329061d3SJens Axboe static inline bool io_poll_get_ownership(struct io_kiocb *req)
93329061d3SJens Axboe {
94a26a35e9SPavel Begunkov 	if (unlikely(atomic_read(&req->poll_refs) >= IO_POLL_REF_BIAS))
95a26a35e9SPavel Begunkov 		return io_poll_get_ownership_slowpath(req);
96329061d3SJens Axboe 	return !(atomic_fetch_inc(&req->poll_refs) & IO_POLL_REF_MASK);
97329061d3SJens Axboe }
98329061d3SJens Axboe 
io_poll_mark_cancelled(struct io_kiocb * req)99329061d3SJens Axboe static void io_poll_mark_cancelled(struct io_kiocb *req)
100329061d3SJens Axboe {
101329061d3SJens Axboe 	atomic_or(IO_POLL_CANCEL_FLAG, &req->poll_refs);
102329061d3SJens Axboe }
103329061d3SJens Axboe 
io_poll_get_double(struct io_kiocb * req)104329061d3SJens Axboe static struct io_poll *io_poll_get_double(struct io_kiocb *req)
105329061d3SJens Axboe {
106329061d3SJens Axboe 	/* pure poll stashes this in ->async_data, poll driven retry elsewhere */
107329061d3SJens Axboe 	if (req->opcode == IORING_OP_POLL_ADD)
108329061d3SJens Axboe 		return req->async_data;
109329061d3SJens Axboe 	return req->apoll->double_poll;
110329061d3SJens Axboe }
111329061d3SJens Axboe 
io_poll_get_single(struct io_kiocb * req)112329061d3SJens Axboe static struct io_poll *io_poll_get_single(struct io_kiocb *req)
113329061d3SJens Axboe {
114329061d3SJens Axboe 	if (req->opcode == IORING_OP_POLL_ADD)
115f2ccb5aeSStefan Metzmacher 		return io_kiocb_to_cmd(req, struct io_poll);
116329061d3SJens Axboe 	return &req->apoll->poll;
117329061d3SJens Axboe }
118329061d3SJens Axboe 
io_poll_req_insert(struct io_kiocb * req)119329061d3SJens Axboe static void io_poll_req_insert(struct io_kiocb *req)
120329061d3SJens Axboe {
121e6f89be6SPavel Begunkov 	struct io_hash_table *table = &req->ctx->cancel_table;
122e6f89be6SPavel Begunkov 	u32 index = hash_long(req->cqe.user_data, table->hash_bits);
123e6f89be6SPavel Begunkov 	struct io_hash_bucket *hb = &table->hbs[index];
124329061d3SJens Axboe 
12538513c46SHao Xu 	spin_lock(&hb->lock);
12638513c46SHao Xu 	hlist_add_head(&req->hash_node, &hb->list);
12738513c46SHao Xu 	spin_unlock(&hb->lock);
12838513c46SHao Xu }
12938513c46SHao Xu 
io_poll_req_delete(struct io_kiocb * req,struct io_ring_ctx * ctx)13038513c46SHao Xu static void io_poll_req_delete(struct io_kiocb *req, struct io_ring_ctx *ctx)
13138513c46SHao Xu {
132e6f89be6SPavel Begunkov 	struct io_hash_table *table = &req->ctx->cancel_table;
133e6f89be6SPavel Begunkov 	u32 index = hash_long(req->cqe.user_data, table->hash_bits);
134e6f89be6SPavel Begunkov 	spinlock_t *lock = &table->hbs[index].lock;
13538513c46SHao Xu 
13638513c46SHao Xu 	spin_lock(lock);
13738513c46SHao Xu 	hash_del(&req->hash_node);
13838513c46SHao Xu 	spin_unlock(lock);
139329061d3SJens Axboe }
140329061d3SJens Axboe 
io_poll_req_insert_locked(struct io_kiocb * req)1419ca9fb24SPavel Begunkov static void io_poll_req_insert_locked(struct io_kiocb *req)
1429ca9fb24SPavel Begunkov {
1439ca9fb24SPavel Begunkov 	struct io_hash_table *table = &req->ctx->cancel_table_locked;
1449ca9fb24SPavel Begunkov 	u32 index = hash_long(req->cqe.user_data, table->hash_bits);
1459ca9fb24SPavel Begunkov 
1465576035fSPavel Begunkov 	lockdep_assert_held(&req->ctx->uring_lock);
1475576035fSPavel Begunkov 
1489ca9fb24SPavel Begunkov 	hlist_add_head(&req->hash_node, &table->hbs[index].list);
1499ca9fb24SPavel Begunkov }
1509ca9fb24SPavel Begunkov 
io_poll_tw_hash_eject(struct io_kiocb * req,struct io_tw_state * ts)151a282967cSPavel Begunkov static void io_poll_tw_hash_eject(struct io_kiocb *req, struct io_tw_state *ts)
1529ca9fb24SPavel Begunkov {
1539ca9fb24SPavel Begunkov 	struct io_ring_ctx *ctx = req->ctx;
1549ca9fb24SPavel Begunkov 
1559ca9fb24SPavel Begunkov 	if (req->flags & REQ_F_HASH_LOCKED) {
1569ca9fb24SPavel Begunkov 		/*
1579ca9fb24SPavel Begunkov 		 * ->cancel_table_locked is protected by ->uring_lock in
1589ca9fb24SPavel Begunkov 		 * contrast to per bucket spinlocks. Likely, tctx_task_work()
1599ca9fb24SPavel Begunkov 		 * already grabbed the mutex for us, but there is a chance it
1609ca9fb24SPavel Begunkov 		 * failed.
1619ca9fb24SPavel Begunkov 		 */
162a282967cSPavel Begunkov 		io_tw_lock(ctx, ts);
1639ca9fb24SPavel Begunkov 		hash_del(&req->hash_node);
164b21a51e2SPavel Begunkov 		req->flags &= ~REQ_F_HASH_LOCKED;
1659ca9fb24SPavel Begunkov 	} else {
1669ca9fb24SPavel Begunkov 		io_poll_req_delete(req, ctx);
1679ca9fb24SPavel Begunkov 	}
1689ca9fb24SPavel Begunkov }
1699ca9fb24SPavel Begunkov 
io_init_poll_iocb(struct io_poll * poll,__poll_t events)1701947ddf9SJens Axboe static void io_init_poll_iocb(struct io_poll *poll, __poll_t events)
171329061d3SJens Axboe {
172329061d3SJens Axboe 	poll->head = NULL;
173329061d3SJens Axboe #define IO_POLL_UNMASK	(EPOLLERR|EPOLLHUP|EPOLLNVAL|EPOLLRDHUP)
174329061d3SJens Axboe 	/* mask in events that we always want/need */
175329061d3SJens Axboe 	poll->events = events | IO_POLL_UNMASK;
176329061d3SJens Axboe 	INIT_LIST_HEAD(&poll->wait.entry);
1771947ddf9SJens Axboe 	init_waitqueue_func_entry(&poll->wait, io_poll_wake);
178329061d3SJens Axboe }
179329061d3SJens Axboe 
io_poll_remove_entry(struct io_poll * poll)180329061d3SJens Axboe static inline void io_poll_remove_entry(struct io_poll *poll)
181329061d3SJens Axboe {
182329061d3SJens Axboe 	struct wait_queue_head *head = smp_load_acquire(&poll->head);
183329061d3SJens Axboe 
184329061d3SJens Axboe 	if (head) {
185329061d3SJens Axboe 		spin_lock_irq(&head->lock);
186329061d3SJens Axboe 		list_del_init(&poll->wait.entry);
187329061d3SJens Axboe 		poll->head = NULL;
188329061d3SJens Axboe 		spin_unlock_irq(&head->lock);
189329061d3SJens Axboe 	}
190329061d3SJens Axboe }
191329061d3SJens Axboe 
io_poll_remove_entries(struct io_kiocb * req)192329061d3SJens Axboe static void io_poll_remove_entries(struct io_kiocb *req)
193329061d3SJens Axboe {
194329061d3SJens Axboe 	/*
195329061d3SJens Axboe 	 * Nothing to do if neither of those flags are set. Avoid dipping
196329061d3SJens Axboe 	 * into the poll/apoll/double cachelines if we can.
197329061d3SJens Axboe 	 */
198329061d3SJens Axboe 	if (!(req->flags & (REQ_F_SINGLE_POLL | REQ_F_DOUBLE_POLL)))
199329061d3SJens Axboe 		return;
200329061d3SJens Axboe 
201329061d3SJens Axboe 	/*
202329061d3SJens Axboe 	 * While we hold the waitqueue lock and the waitqueue is nonempty,
203329061d3SJens Axboe 	 * wake_up_pollfree() will wait for us.  However, taking the waitqueue
204329061d3SJens Axboe 	 * lock in the first place can race with the waitqueue being freed.
205329061d3SJens Axboe 	 *
206329061d3SJens Axboe 	 * We solve this as eventpoll does: by taking advantage of the fact that
207329061d3SJens Axboe 	 * all users of wake_up_pollfree() will RCU-delay the actual free.  If
208329061d3SJens Axboe 	 * we enter rcu_read_lock() and see that the pointer to the queue is
209329061d3SJens Axboe 	 * non-NULL, we can then lock it without the memory being freed out from
210329061d3SJens Axboe 	 * under us.
211329061d3SJens Axboe 	 *
212329061d3SJens Axboe 	 * Keep holding rcu_read_lock() as long as we hold the queue lock, in
213329061d3SJens Axboe 	 * case the caller deletes the entry from the queue, leaving it empty.
214329061d3SJens Axboe 	 * In that case, only RCU prevents the queue memory from being freed.
215329061d3SJens Axboe 	 */
216329061d3SJens Axboe 	rcu_read_lock();
217329061d3SJens Axboe 	if (req->flags & REQ_F_SINGLE_POLL)
218329061d3SJens Axboe 		io_poll_remove_entry(io_poll_get_single(req));
219329061d3SJens Axboe 	if (req->flags & REQ_F_DOUBLE_POLL)
220329061d3SJens Axboe 		io_poll_remove_entry(io_poll_get_double(req));
221329061d3SJens Axboe 	rcu_read_unlock();
222329061d3SJens Axboe }
223329061d3SJens Axboe 
2242ba69707SDylan Yudaken enum {
2252ba69707SDylan Yudaken 	IOU_POLL_DONE = 0,
2262ba69707SDylan Yudaken 	IOU_POLL_NO_ACTION = 1,
227114eccdfSDylan Yudaken 	IOU_POLL_REMOVE_POLL_USE_RES = 2,
2286e5aedb9SJens Axboe 	IOU_POLL_REISSUE = 3,
2297cbd3aa5SJens Axboe 	IOU_POLL_REQUEUE = 4,
2302ba69707SDylan Yudaken };
2312ba69707SDylan Yudaken 
__io_poll_execute(struct io_kiocb * req,int mask)2320848bf7eSJens Axboe static void __io_poll_execute(struct io_kiocb *req, int mask)
2330848bf7eSJens Axboe {
2340848bf7eSJens Axboe 	io_req_set_res(req, mask, 0);
2350848bf7eSJens Axboe 	req->io_task_work.func = io_poll_task_func;
2360848bf7eSJens Axboe 
2370848bf7eSJens Axboe 	trace_io_uring_task_add(req, mask);
2380848bf7eSJens Axboe 	io_req_task_work_add(req);
2390848bf7eSJens Axboe }
2400848bf7eSJens Axboe 
io_poll_execute(struct io_kiocb * req,int res)2410848bf7eSJens Axboe static inline void io_poll_execute(struct io_kiocb *req, int res)
2420848bf7eSJens Axboe {
2430848bf7eSJens Axboe 	if (io_poll_get_ownership(req))
2440848bf7eSJens Axboe 		__io_poll_execute(req, res);
2450848bf7eSJens Axboe }
2460848bf7eSJens Axboe 
247329061d3SJens Axboe /*
248329061d3SJens Axboe  * All poll tw should go through this. Checks for poll events, manages
249329061d3SJens Axboe  * references, does rewait, etc.
250329061d3SJens Axboe  *
2516e5aedb9SJens Axboe  * Returns a negative error on failure. IOU_POLL_NO_ACTION when no action
2526e5aedb9SJens Axboe  * require, which is either spurious wakeup or multishot CQE is served.
2536e5aedb9SJens Axboe  * IOU_POLL_DONE when it's done with the request, then the mask is stored in
2546e5aedb9SJens Axboe  * req->cqe.res. IOU_POLL_REMOVE_POLL_USE_RES indicates to remove multishot
2556e5aedb9SJens Axboe  * poll and that the result is stored in req->cqe.
256329061d3SJens Axboe  */
io_poll_check_events(struct io_kiocb * req,struct io_tw_state * ts)257a282967cSPavel Begunkov static int io_poll_check_events(struct io_kiocb *req, struct io_tw_state *ts)
258329061d3SJens Axboe {
2596e5aedb9SJens Axboe 	int v;
260329061d3SJens Axboe 
261329061d3SJens Axboe 	/* req->task == current here, checking PF_EXITING is safe */
262329061d3SJens Axboe 	if (unlikely(req->task->flags & PF_EXITING))
263329061d3SJens Axboe 		return -ECANCELED;
264329061d3SJens Axboe 
265329061d3SJens Axboe 	do {
266329061d3SJens Axboe 		v = atomic_read(&req->poll_refs);
267329061d3SJens Axboe 
2689805fa2dSPavel Begunkov 		if (unlikely(v != 1)) {
2699805fa2dSPavel Begunkov 			/* tw should be the owner and so have some refs */
270329061d3SJens Axboe 			if (WARN_ON_ONCE(!(v & IO_POLL_REF_MASK)))
271c3bfb57eSPavel Begunkov 				return IOU_POLL_NO_ACTION;
272329061d3SJens Axboe 			if (v & IO_POLL_CANCEL_FLAG)
273329061d3SJens Axboe 				return -ECANCELED;
274539bcb57SPavel Begunkov 			/*
275539bcb57SPavel Begunkov 			 * cqe.res contains only events of the first wake up
2769805fa2dSPavel Begunkov 			 * and all others are to be lost. Redo vfs_poll() to get
277539bcb57SPavel Begunkov 			 * up to date state.
278539bcb57SPavel Begunkov 			 */
279539bcb57SPavel Begunkov 			if ((v & IO_POLL_REF_MASK) != 1)
280539bcb57SPavel Begunkov 				req->cqe.res = 0;
2819805fa2dSPavel Begunkov 
282a26a35e9SPavel Begunkov 			if (v & IO_POLL_RETRY_FLAG) {
283a26a35e9SPavel Begunkov 				req->cqe.res = 0;
284a26a35e9SPavel Begunkov 				/*
285a26a35e9SPavel Begunkov 				 * We won't find new events that came in between
2869805fa2dSPavel Begunkov 				 * vfs_poll and the ref put unless we clear the
2879805fa2dSPavel Begunkov 				 * flag in advance.
288a26a35e9SPavel Begunkov 				 */
289a26a35e9SPavel Begunkov 				atomic_andnot(IO_POLL_RETRY_FLAG, &req->poll_refs);
290a26a35e9SPavel Begunkov 				v &= ~IO_POLL_RETRY_FLAG;
291a26a35e9SPavel Begunkov 			}
2929805fa2dSPavel Begunkov 		}
293329061d3SJens Axboe 
2942ba69707SDylan Yudaken 		/* the mask was stashed in __io_poll_execute */
295329061d3SJens Axboe 		if (!req->cqe.res) {
296329061d3SJens Axboe 			struct poll_table_struct pt = { ._key = req->apoll_events };
297329061d3SJens Axboe 			req->cqe.res = vfs_poll(req->file, &pt) & req->apoll_events;
2986e5aedb9SJens Axboe 			/*
2996e5aedb9SJens Axboe 			 * We got woken with a mask, but someone else got to
3006e5aedb9SJens Axboe 			 * it first. The above vfs_poll() doesn't add us back
3016e5aedb9SJens Axboe 			 * to the waitqueue, so if we get nothing back, we
3026e5aedb9SJens Axboe 			 * should be safe and attempt a reissue.
3036e5aedb9SJens Axboe 			 */
3048caa03f1SJens Axboe 			if (unlikely(!req->cqe.res)) {
3058caa03f1SJens Axboe 				/* Multishot armed need not reissue */
3068caa03f1SJens Axboe 				if (!(req->apoll_events & EPOLLONESHOT))
3078caa03f1SJens Axboe 					continue;
3086e5aedb9SJens Axboe 				return IOU_POLL_REISSUE;
309329061d3SJens Axboe 			}
3108caa03f1SJens Axboe 		}
311329061d3SJens Axboe 		if (req->apoll_events & EPOLLONESHOT)
3122ba69707SDylan Yudaken 			return IOU_POLL_DONE;
313329061d3SJens Axboe 
314329061d3SJens Axboe 		/* multishot, just fill a CQE and proceed */
315329061d3SJens Axboe 		if (!(req->flags & REQ_F_APOLL_MULTISHOT)) {
316329061d3SJens Axboe 			__poll_t mask = mangle_poll(req->cqe.res &
317329061d3SJens Axboe 						    req->apoll_events);
318329061d3SJens Axboe 
319b6b2bb58SPavel Begunkov 			if (!io_fill_cqe_req_aux(req, ts->locked, mask,
320b6b2bb58SPavel Begunkov 						 IORING_CQE_F_MORE)) {
321a2da6763SDylan Yudaken 				io_req_set_res(req, mask, 0);
322a2da6763SDylan Yudaken 				return IOU_POLL_REMOVE_POLL_USE_RES;
323a2da6763SDylan Yudaken 			}
324d245bca6SPavel Begunkov 		} else {
325a282967cSPavel Begunkov 			int ret = io_poll_issue(req, ts);
326114eccdfSDylan Yudaken 			if (ret == IOU_STOP_MULTISHOT)
327114eccdfSDylan Yudaken 				return IOU_POLL_REMOVE_POLL_USE_RES;
3287cbd3aa5SJens Axboe 			else if (ret == IOU_REQUEUE)
3297cbd3aa5SJens Axboe 				return IOU_POLL_REQUEUE;
3302ba69707SDylan Yudaken 			if (ret < 0)
331329061d3SJens Axboe 				return ret;
332d245bca6SPavel Begunkov 		}
333329061d3SJens Axboe 
334b98186aeSPavel Begunkov 		/* force the next iteration to vfs_poll() */
335b98186aeSPavel Begunkov 		req->cqe.res = 0;
336b98186aeSPavel Begunkov 
337329061d3SJens Axboe 		/*
338329061d3SJens Axboe 		 * Release all references, retry if someone tried to restart
339329061d3SJens Axboe 		 * task_work while we were executing it.
340329061d3SJens Axboe 		 */
34112ad3d2dSLin Ma 	} while (atomic_sub_return(v & IO_POLL_REF_MASK, &req->poll_refs) &
34212ad3d2dSLin Ma 					IO_POLL_REF_MASK);
343329061d3SJens Axboe 
3442ba69707SDylan Yudaken 	return IOU_POLL_NO_ACTION;
345329061d3SJens Axboe }
346329061d3SJens Axboe 
io_poll_task_func(struct io_kiocb * req,struct io_tw_state * ts)347c92fcfc2SJens Axboe void io_poll_task_func(struct io_kiocb *req, struct io_tw_state *ts)
348329061d3SJens Axboe {
349329061d3SJens Axboe 	int ret;
350329061d3SJens Axboe 
351a282967cSPavel Begunkov 	ret = io_poll_check_events(req, ts);
3527cbd3aa5SJens Axboe 	if (ret == IOU_POLL_NO_ACTION) {
353329061d3SJens Axboe 		return;
3547cbd3aa5SJens Axboe 	} else if (ret == IOU_POLL_REQUEUE) {
3557cbd3aa5SJens Axboe 		__io_poll_execute(req, 0);
3567cbd3aa5SJens Axboe 		return;
3577cbd3aa5SJens Axboe 	}
358443e5755SPavel Begunkov 	io_poll_remove_entries(req);
359a282967cSPavel Begunkov 	io_poll_tw_hash_eject(req, ts);
360329061d3SJens Axboe 
361443e5755SPavel Begunkov 	if (req->opcode == IORING_OP_POLL_ADD) {
3622ba69707SDylan Yudaken 		if (ret == IOU_POLL_DONE) {
363443e5755SPavel Begunkov 			struct io_poll *poll;
364443e5755SPavel Begunkov 
365443e5755SPavel Begunkov 			poll = io_kiocb_to_cmd(req, struct io_poll);
366329061d3SJens Axboe 			req->cqe.res = mangle_poll(req->cqe.res & poll->events);
3676e5aedb9SJens Axboe 		} else if (ret == IOU_POLL_REISSUE) {
368a282967cSPavel Begunkov 			io_req_task_submit(req, ts);
3696e5aedb9SJens Axboe 			return;
370114eccdfSDylan Yudaken 		} else if (ret != IOU_POLL_REMOVE_POLL_USE_RES) {
371329061d3SJens Axboe 			req->cqe.res = ret;
372329061d3SJens Axboe 			req_set_fail(req);
373329061d3SJens Axboe 		}
374329061d3SJens Axboe 
3750ec6dca2SPavel Begunkov 		io_req_set_res(req, req->cqe.res, 0);
376a282967cSPavel Begunkov 		io_req_task_complete(req, ts);
377443e5755SPavel Begunkov 	} else {
378a282967cSPavel Begunkov 		io_tw_lock(req->ctx, ts);
379329061d3SJens Axboe 
380114eccdfSDylan Yudaken 		if (ret == IOU_POLL_REMOVE_POLL_USE_RES)
381a282967cSPavel Begunkov 			io_req_task_complete(req, ts);
3826e5aedb9SJens Axboe 		else if (ret == IOU_POLL_DONE || ret == IOU_POLL_REISSUE)
383a282967cSPavel Begunkov 			io_req_task_submit(req, ts);
384329061d3SJens Axboe 		else
385973fc83fSDylan Yudaken 			io_req_defer_failed(req, ret);
386329061d3SJens Axboe 	}
387443e5755SPavel Begunkov }
388329061d3SJens Axboe 
io_poll_cancel_req(struct io_kiocb * req)389329061d3SJens Axboe static void io_poll_cancel_req(struct io_kiocb *req)
390329061d3SJens Axboe {
391329061d3SJens Axboe 	io_poll_mark_cancelled(req);
392329061d3SJens Axboe 	/* kick tw, which should complete the request */
39313a99017SPavel Begunkov 	io_poll_execute(req, 0);
394329061d3SJens Axboe }
395329061d3SJens Axboe 
396329061d3SJens Axboe #define IO_ASYNC_POLL_COMMON	(EPOLLONESHOT | EPOLLPRI)
397329061d3SJens Axboe 
io_pollfree_wake(struct io_kiocb * req,struct io_poll * poll)398fe991a76SJens Axboe static __cold int io_pollfree_wake(struct io_kiocb *req, struct io_poll *poll)
399329061d3SJens Axboe {
400329061d3SJens Axboe 	io_poll_mark_cancelled(req);
401329061d3SJens Axboe 	/* we have to kick tw in case it's not already */
40213a99017SPavel Begunkov 	io_poll_execute(req, 0);
403329061d3SJens Axboe 
404329061d3SJens Axboe 	/*
405329061d3SJens Axboe 	 * If the waitqueue is being freed early but someone is already
406329061d3SJens Axboe 	 * holds ownership over it, we have to tear down the request as
407329061d3SJens Axboe 	 * best we can. That means immediately removing the request from
408329061d3SJens Axboe 	 * its waitqueue and preventing all further accesses to the
409329061d3SJens Axboe 	 * waitqueue via the request.
410329061d3SJens Axboe 	 */
411329061d3SJens Axboe 	list_del_init(&poll->wait.entry);
412329061d3SJens Axboe 
413329061d3SJens Axboe 	/*
414329061d3SJens Axboe 	 * Careful: this *must* be the last step, since as soon
415329061d3SJens Axboe 	 * as req->head is NULL'ed out, the request can be
416329061d3SJens Axboe 	 * completed and freed, since aio_poll_complete_work()
417329061d3SJens Axboe 	 * will no longer need to take the waitqueue lock.
418329061d3SJens Axboe 	 */
419329061d3SJens Axboe 	smp_store_release(&poll->head, NULL);
420329061d3SJens Axboe 	return 1;
421329061d3SJens Axboe }
422329061d3SJens Axboe 
io_poll_wake(struct wait_queue_entry * wait,unsigned mode,int sync,void * key)423fe991a76SJens Axboe static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
424fe991a76SJens Axboe 			void *key)
425fe991a76SJens Axboe {
426fe991a76SJens Axboe 	struct io_kiocb *req = wqe_to_req(wait);
427fe991a76SJens Axboe 	struct io_poll *poll = container_of(wait, struct io_poll, wait);
428fe991a76SJens Axboe 	__poll_t mask = key_to_poll(key);
429fe991a76SJens Axboe 
430fe991a76SJens Axboe 	if (unlikely(mask & POLLFREE))
431fe991a76SJens Axboe 		return io_pollfree_wake(req, poll);
432fe991a76SJens Axboe 
433329061d3SJens Axboe 	/* for instances that support it check for an event match first */
434329061d3SJens Axboe 	if (mask && !(mask & (poll->events & ~IO_ASYNC_POLL_COMMON)))
435329061d3SJens Axboe 		return 0;
436329061d3SJens Axboe 
437329061d3SJens Axboe 	if (io_poll_get_ownership(req)) {
43844648532SJens Axboe 		/*
43944648532SJens Axboe 		 * If we trigger a multishot poll off our own wakeup path,
44044648532SJens Axboe 		 * disable multishot as there is a circular dependency between
44144648532SJens Axboe 		 * CQ posting and triggering the event.
44244648532SJens Axboe 		 */
44344648532SJens Axboe 		if (mask & EPOLL_URING_WAKE)
44444648532SJens Axboe 			poll->events |= EPOLLONESHOT;
44544648532SJens Axboe 
446329061d3SJens Axboe 		/* optional, saves extra locking for removal in tw handler */
447329061d3SJens Axboe 		if (mask && poll->events & EPOLLONESHOT) {
448329061d3SJens Axboe 			list_del_init(&poll->wait.entry);
449329061d3SJens Axboe 			poll->head = NULL;
450329061d3SJens Axboe 			if (wqe_is_double(wait))
451329061d3SJens Axboe 				req->flags &= ~REQ_F_DOUBLE_POLL;
452329061d3SJens Axboe 			else
453329061d3SJens Axboe 				req->flags &= ~REQ_F_SINGLE_POLL;
454329061d3SJens Axboe 		}
45513a99017SPavel Begunkov 		__io_poll_execute(req, mask);
456329061d3SJens Axboe 	}
457329061d3SJens Axboe 	return 1;
458329061d3SJens Axboe }
459329061d3SJens Axboe 
46030a33669SPavel Begunkov /* fails only when polling is already completing by the first entry */
io_poll_double_prepare(struct io_kiocb * req)46130a33669SPavel Begunkov static bool io_poll_double_prepare(struct io_kiocb *req)
46249f1c68eSPavel Begunkov {
46349f1c68eSPavel Begunkov 	struct wait_queue_head *head;
46449f1c68eSPavel Begunkov 	struct io_poll *poll = io_poll_get_single(req);
46549f1c68eSPavel Begunkov 
46649f1c68eSPavel Begunkov 	/* head is RCU protected, see io_poll_remove_entries() comments */
46749f1c68eSPavel Begunkov 	rcu_read_lock();
46849f1c68eSPavel Begunkov 	head = smp_load_acquire(&poll->head);
46949f1c68eSPavel Begunkov 	/*
47030a33669SPavel Begunkov 	 * poll arm might not hold ownership and so race for req->flags with
47130a33669SPavel Begunkov 	 * io_poll_wake(). There is only one poll entry queued, serialise with
47230a33669SPavel Begunkov 	 * it by taking its head lock. As we're still arming the tw hanlder
47330a33669SPavel Begunkov 	 * is not going to be run, so there are no races with it.
47449f1c68eSPavel Begunkov 	 */
47530a33669SPavel Begunkov 	if (head) {
47649f1c68eSPavel Begunkov 		spin_lock_irq(&head->lock);
47749f1c68eSPavel Begunkov 		req->flags |= REQ_F_DOUBLE_POLL;
478ceff5017SPavel Begunkov 		if (req->opcode == IORING_OP_POLL_ADD)
479ceff5017SPavel Begunkov 			req->flags |= REQ_F_ASYNC_DATA;
48049f1c68eSPavel Begunkov 		spin_unlock_irq(&head->lock);
48130a33669SPavel Begunkov 	}
48249f1c68eSPavel Begunkov 	rcu_read_unlock();
48330a33669SPavel Begunkov 	return !!head;
48449f1c68eSPavel Begunkov }
48549f1c68eSPavel Begunkov 
__io_queue_proc(struct io_poll * poll,struct io_poll_table * pt,struct wait_queue_head * head,struct io_poll ** poll_ptr)486329061d3SJens Axboe static void __io_queue_proc(struct io_poll *poll, struct io_poll_table *pt,
487329061d3SJens Axboe 			    struct wait_queue_head *head,
488329061d3SJens Axboe 			    struct io_poll **poll_ptr)
489329061d3SJens Axboe {
490329061d3SJens Axboe 	struct io_kiocb *req = pt->req;
491329061d3SJens Axboe 	unsigned long wqe_private = (unsigned long) req;
492329061d3SJens Axboe 
493329061d3SJens Axboe 	/*
494329061d3SJens Axboe 	 * The file being polled uses multiple waitqueues for poll handling
495329061d3SJens Axboe 	 * (e.g. one for read, one for write). Setup a separate io_poll
496329061d3SJens Axboe 	 * if this happens.
497329061d3SJens Axboe 	 */
498329061d3SJens Axboe 	if (unlikely(pt->nr_entries)) {
499329061d3SJens Axboe 		struct io_poll *first = poll;
500329061d3SJens Axboe 
501329061d3SJens Axboe 		/* double add on the same waitqueue head, ignore */
502329061d3SJens Axboe 		if (first->head == head)
503329061d3SJens Axboe 			return;
504329061d3SJens Axboe 		/* already have a 2nd entry, fail a third attempt */
505329061d3SJens Axboe 		if (*poll_ptr) {
506329061d3SJens Axboe 			if ((*poll_ptr)->head == head)
507329061d3SJens Axboe 				return;
508329061d3SJens Axboe 			pt->error = -EINVAL;
509329061d3SJens Axboe 			return;
510329061d3SJens Axboe 		}
511329061d3SJens Axboe 
512329061d3SJens Axboe 		poll = kmalloc(sizeof(*poll), GFP_ATOMIC);
513329061d3SJens Axboe 		if (!poll) {
514329061d3SJens Axboe 			pt->error = -ENOMEM;
515329061d3SJens Axboe 			return;
516329061d3SJens Axboe 		}
51749f1c68eSPavel Begunkov 
518329061d3SJens Axboe 		/* mark as double wq entry */
5190638cd7bSPavel Begunkov 		wqe_private |= IO_WQE_F_DOUBLE;
5201947ddf9SJens Axboe 		io_init_poll_iocb(poll, first->events);
52130a33669SPavel Begunkov 		if (!io_poll_double_prepare(req)) {
52230a33669SPavel Begunkov 			/* the request is completing, just back off */
52330a33669SPavel Begunkov 			kfree(poll);
52430a33669SPavel Begunkov 			return;
52530a33669SPavel Begunkov 		}
526329061d3SJens Axboe 		*poll_ptr = poll;
52749f1c68eSPavel Begunkov 	} else {
52849f1c68eSPavel Begunkov 		/* fine to modify, there is no poll queued to race with us */
52949f1c68eSPavel Begunkov 		req->flags |= REQ_F_SINGLE_POLL;
530329061d3SJens Axboe 	}
531329061d3SJens Axboe 
532329061d3SJens Axboe 	pt->nr_entries++;
533329061d3SJens Axboe 	poll->head = head;
534329061d3SJens Axboe 	poll->wait.private = (void *) wqe_private;
535329061d3SJens Axboe 
536329061d3SJens Axboe 	if (poll->events & EPOLLEXCLUSIVE)
537329061d3SJens Axboe 		add_wait_queue_exclusive(head, &poll->wait);
538329061d3SJens Axboe 	else
539329061d3SJens Axboe 		add_wait_queue(head, &poll->wait);
540329061d3SJens Axboe }
541329061d3SJens Axboe 
io_poll_queue_proc(struct file * file,struct wait_queue_head * head,struct poll_table_struct * p)542329061d3SJens Axboe static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
543329061d3SJens Axboe 			       struct poll_table_struct *p)
544329061d3SJens Axboe {
545329061d3SJens Axboe 	struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
546f2ccb5aeSStefan Metzmacher 	struct io_poll *poll = io_kiocb_to_cmd(pt->req, struct io_poll);
547329061d3SJens Axboe 
548329061d3SJens Axboe 	__io_queue_proc(poll, pt, head,
549329061d3SJens Axboe 			(struct io_poll **) &pt->req->async_data);
550329061d3SJens Axboe }
551329061d3SJens Axboe 
io_poll_can_finish_inline(struct io_kiocb * req,struct io_poll_table * pt)55249f1c68eSPavel Begunkov static bool io_poll_can_finish_inline(struct io_kiocb *req,
55349f1c68eSPavel Begunkov 				      struct io_poll_table *pt)
55449f1c68eSPavel Begunkov {
55549f1c68eSPavel Begunkov 	return pt->owning || io_poll_get_ownership(req);
55649f1c68eSPavel Begunkov }
55749f1c68eSPavel Begunkov 
io_poll_add_hash(struct io_kiocb * req)558febb985cSJens Axboe static void io_poll_add_hash(struct io_kiocb *req)
559febb985cSJens Axboe {
560febb985cSJens Axboe 	if (req->flags & REQ_F_HASH_LOCKED)
561febb985cSJens Axboe 		io_poll_req_insert_locked(req);
562febb985cSJens Axboe 	else
563febb985cSJens Axboe 		io_poll_req_insert(req);
564febb985cSJens Axboe }
565febb985cSJens Axboe 
566de08356fSPavel Begunkov /*
567de08356fSPavel Begunkov  * Returns 0 when it's handed over for polling. The caller owns the requests if
568de08356fSPavel Begunkov  * it returns non-zero, but otherwise should not touch it. Negative values
569de08356fSPavel Begunkov  * contain an error code. When the result is >0, the polling has completed
570de08356fSPavel Begunkov  * inline and ipt.result_mask is set to the mask.
571de08356fSPavel Begunkov  */
__io_arm_poll_handler(struct io_kiocb * req,struct io_poll * poll,struct io_poll_table * ipt,__poll_t mask,unsigned issue_flags)572329061d3SJens Axboe static int __io_arm_poll_handler(struct io_kiocb *req,
573329061d3SJens Axboe 				 struct io_poll *poll,
57449f1c68eSPavel Begunkov 				 struct io_poll_table *ipt, __poll_t mask,
57549f1c68eSPavel Begunkov 				 unsigned issue_flags)
576329061d3SJens Axboe {
577329061d3SJens Axboe 	struct io_ring_ctx *ctx = req->ctx;
578329061d3SJens Axboe 
579329061d3SJens Axboe 	INIT_HLIST_NODE(&req->hash_node);
580329061d3SJens Axboe 	req->work.cancel_seq = atomic_read(&ctx->cancel_seq);
5811947ddf9SJens Axboe 	io_init_poll_iocb(poll, mask);
582329061d3SJens Axboe 	poll->file = req->file;
583329061d3SJens Axboe 	req->apoll_events = poll->events;
584329061d3SJens Axboe 
585329061d3SJens Axboe 	ipt->pt._key = mask;
586329061d3SJens Axboe 	ipt->req = req;
587329061d3SJens Axboe 	ipt->error = 0;
588329061d3SJens Axboe 	ipt->nr_entries = 0;
589329061d3SJens Axboe 	/*
59049f1c68eSPavel Begunkov 	 * Polling is either completed here or via task_work, so if we're in the
59149f1c68eSPavel Begunkov 	 * task context we're naturally serialised with tw by merit of running
59249f1c68eSPavel Begunkov 	 * the same task. When it's io-wq, take the ownership to prevent tw
59349f1c68eSPavel Begunkov 	 * from running. However, when we're in the task context, skip taking
59449f1c68eSPavel Begunkov 	 * it as an optimisation.
59549f1c68eSPavel Begunkov 	 *
59649f1c68eSPavel Begunkov 	 * Note: even though the request won't be completed/freed, without
59749f1c68eSPavel Begunkov 	 * ownership we still can race with io_poll_wake().
59849f1c68eSPavel Begunkov 	 * io_poll_can_finish_inline() tries to deal with that.
599329061d3SJens Axboe 	 */
60049f1c68eSPavel Begunkov 	ipt->owning = issue_flags & IO_URING_F_UNLOCKED;
60149f1c68eSPavel Begunkov 	atomic_set(&req->poll_refs, (int)ipt->owning);
602e8375e43SPavel Begunkov 
603e8375e43SPavel Begunkov 	/* io-wq doesn't hold uring_lock */
604e8375e43SPavel Begunkov 	if (issue_flags & IO_URING_F_UNLOCKED)
605e8375e43SPavel Begunkov 		req->flags &= ~REQ_F_HASH_LOCKED;
606e8375e43SPavel Begunkov 
607329061d3SJens Axboe 	mask = vfs_poll(req->file, &ipt->pt) & poll->events;
608329061d3SJens Axboe 
609de08356fSPavel Begunkov 	if (unlikely(ipt->error || !ipt->nr_entries)) {
610de08356fSPavel Begunkov 		io_poll_remove_entries(req);
611de08356fSPavel Begunkov 
61249f1c68eSPavel Begunkov 		if (!io_poll_can_finish_inline(req, ipt)) {
61349f1c68eSPavel Begunkov 			io_poll_mark_cancelled(req);
61449f1c68eSPavel Begunkov 			return 0;
61549f1c68eSPavel Begunkov 		} else if (mask && (poll->events & EPOLLET)) {
616de08356fSPavel Begunkov 			ipt->result_mask = mask;
617de08356fSPavel Begunkov 			return 1;
618de08356fSPavel Begunkov 		}
61949f1c68eSPavel Begunkov 		return ipt->error ?: -EINVAL;
620de08356fSPavel Begunkov 	}
621de08356fSPavel Begunkov 
622b9ba8a44SJens Axboe 	if (mask &&
623b9ba8a44SJens Axboe 	   ((poll->events & (EPOLLET|EPOLLONESHOT)) == (EPOLLET|EPOLLONESHOT))) {
624febb985cSJens Axboe 		if (!io_poll_can_finish_inline(req, ipt)) {
625febb985cSJens Axboe 			io_poll_add_hash(req);
62649f1c68eSPavel Begunkov 			return 0;
627febb985cSJens Axboe 		}
628329061d3SJens Axboe 		io_poll_remove_entries(req);
629063a0079SPavel Begunkov 		ipt->result_mask = mask;
630329061d3SJens Axboe 		/* no one else has access to the req, forget about the ref */
631063a0079SPavel Begunkov 		return 1;
632329061d3SJens Axboe 	}
633b9ba8a44SJens Axboe 
634febb985cSJens Axboe 	io_poll_add_hash(req);
635329061d3SJens Axboe 
63649f1c68eSPavel Begunkov 	if (mask && (poll->events & EPOLLET) &&
63749f1c68eSPavel Begunkov 	    io_poll_can_finish_inline(req, ipt)) {
63813a99017SPavel Begunkov 		__io_poll_execute(req, mask);
639329061d3SJens Axboe 		return 0;
640329061d3SJens Axboe 	}
641329061d3SJens Axboe 
64249f1c68eSPavel Begunkov 	if (ipt->owning) {
643329061d3SJens Axboe 		/*
6442f389343SPavel Begunkov 		 * Try to release ownership. If we see a change of state, e.g.
6452f389343SPavel Begunkov 		 * poll was waken up, queue up a tw, it'll deal with it.
646329061d3SJens Axboe 		 */
6472f389343SPavel Begunkov 		if (atomic_cmpxchg(&req->poll_refs, 1, 0) != 1)
64813a99017SPavel Begunkov 			__io_poll_execute(req, 0);
64949f1c68eSPavel Begunkov 	}
650329061d3SJens Axboe 	return 0;
651329061d3SJens Axboe }
652329061d3SJens Axboe 
io_async_queue_proc(struct file * file,struct wait_queue_head * head,struct poll_table_struct * p)653329061d3SJens Axboe static void io_async_queue_proc(struct file *file, struct wait_queue_head *head,
654329061d3SJens Axboe 			       struct poll_table_struct *p)
655329061d3SJens Axboe {
656329061d3SJens Axboe 	struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
657329061d3SJens Axboe 	struct async_poll *apoll = pt->req->apoll;
658329061d3SJens Axboe 
659329061d3SJens Axboe 	__io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll);
660329061d3SJens Axboe }
661329061d3SJens Axboe 
662c16bda37SJens Axboe /*
663c16bda37SJens Axboe  * We can't reliably detect loops in repeated poll triggers and issue
664c16bda37SJens Axboe  * subsequently failing. But rather than fail these immediately, allow a
665c16bda37SJens Axboe  * certain amount of retries before we give up. Given that this condition
666c16bda37SJens Axboe  * should _rarely_ trigger even once, we should be fine with a larger value.
667c16bda37SJens Axboe  */
668c16bda37SJens Axboe #define APOLL_MAX_RETRY		128
669c16bda37SJens Axboe 
io_req_alloc_apoll(struct io_kiocb * req,unsigned issue_flags)6705204aa8cSPavel Begunkov static struct async_poll *io_req_alloc_apoll(struct io_kiocb *req,
6715204aa8cSPavel Begunkov 					     unsigned issue_flags)
6725204aa8cSPavel Begunkov {
6735204aa8cSPavel Begunkov 	struct io_ring_ctx *ctx = req->ctx;
6749b797a37SJens Axboe 	struct io_cache_entry *entry;
6755204aa8cSPavel Begunkov 	struct async_poll *apoll;
6765204aa8cSPavel Begunkov 
6775204aa8cSPavel Begunkov 	if (req->flags & REQ_F_POLLED) {
6785204aa8cSPavel Begunkov 		apoll = req->apoll;
6795204aa8cSPavel Begunkov 		kfree(apoll->double_poll);
680df730ec2SXinghui Li 	} else if (!(issue_flags & IO_URING_F_UNLOCKED)) {
681df730ec2SXinghui Li 		entry = io_alloc_cache_get(&ctx->apoll_cache);
682df730ec2SXinghui Li 		if (entry == NULL)
683df730ec2SXinghui Li 			goto alloc_apoll;
6849b797a37SJens Axboe 		apoll = container_of(entry, struct async_poll, cache);
685c16bda37SJens Axboe 		apoll->poll.retries = APOLL_MAX_RETRY;
6865204aa8cSPavel Begunkov 	} else {
687df730ec2SXinghui Li alloc_apoll:
6885204aa8cSPavel Begunkov 		apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC);
6895204aa8cSPavel Begunkov 		if (unlikely(!apoll))
6905204aa8cSPavel Begunkov 			return NULL;
691c16bda37SJens Axboe 		apoll->poll.retries = APOLL_MAX_RETRY;
6925204aa8cSPavel Begunkov 	}
6935204aa8cSPavel Begunkov 	apoll->double_poll = NULL;
6945204aa8cSPavel Begunkov 	req->apoll = apoll;
695c16bda37SJens Axboe 	if (unlikely(!--apoll->poll.retries))
696c16bda37SJens Axboe 		return NULL;
6975204aa8cSPavel Begunkov 	return apoll;
6985204aa8cSPavel Begunkov }
6995204aa8cSPavel Begunkov 
io_arm_poll_handler(struct io_kiocb * req,unsigned issue_flags)700329061d3SJens Axboe int io_arm_poll_handler(struct io_kiocb *req, unsigned issue_flags)
701329061d3SJens Axboe {
702a7dd2782SBreno Leitao 	const struct io_issue_def *def = &io_issue_defs[req->opcode];
703329061d3SJens Axboe 	struct async_poll *apoll;
704329061d3SJens Axboe 	struct io_poll_table ipt;
705b9ba8a44SJens Axboe 	__poll_t mask = POLLPRI | POLLERR | EPOLLET;
706329061d3SJens Axboe 	int ret;
707329061d3SJens Axboe 
7089ca9fb24SPavel Begunkov 	/*
7099ca9fb24SPavel Begunkov 	 * apoll requests already grab the mutex to complete in the tw handler,
7109ca9fb24SPavel Begunkov 	 * so removal from the mutex-backed hash is free, use it by default.
7119ca9fb24SPavel Begunkov 	 */
7129ca9fb24SPavel Begunkov 	req->flags |= REQ_F_HASH_LOCKED;
7139ca9fb24SPavel Begunkov 
714329061d3SJens Axboe 	if (!def->pollin && !def->pollout)
715329061d3SJens Axboe 		return IO_APOLL_ABORTED;
716329061d3SJens Axboe 	if (!file_can_poll(req->file))
717329061d3SJens Axboe 		return IO_APOLL_ABORTED;
718329061d3SJens Axboe 	if (!(req->flags & REQ_F_APOLL_MULTISHOT))
719329061d3SJens Axboe 		mask |= EPOLLONESHOT;
720329061d3SJens Axboe 
721329061d3SJens Axboe 	if (def->pollin) {
722329061d3SJens Axboe 		mask |= EPOLLIN | EPOLLRDNORM;
723329061d3SJens Axboe 
724329061d3SJens Axboe 		/* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */
725329061d3SJens Axboe 		if (req->flags & REQ_F_CLEAR_POLLIN)
726329061d3SJens Axboe 			mask &= ~EPOLLIN;
727329061d3SJens Axboe 	} else {
728329061d3SJens Axboe 		mask |= EPOLLOUT | EPOLLWRNORM;
729329061d3SJens Axboe 	}
730329061d3SJens Axboe 	if (def->poll_exclusive)
731329061d3SJens Axboe 		mask |= EPOLLEXCLUSIVE;
7325204aa8cSPavel Begunkov 
7335204aa8cSPavel Begunkov 	apoll = io_req_alloc_apoll(req, issue_flags);
7345204aa8cSPavel Begunkov 	if (!apoll)
735329061d3SJens Axboe 		return IO_APOLL_ABORTED;
736005308f7SJens Axboe 	req->flags &= ~(REQ_F_SINGLE_POLL | REQ_F_DOUBLE_POLL);
737329061d3SJens Axboe 	req->flags |= REQ_F_POLLED;
738329061d3SJens Axboe 	ipt.pt._qproc = io_async_queue_proc;
739329061d3SJens Axboe 
740329061d3SJens Axboe 	io_kbuf_recycle(req, issue_flags);
741329061d3SJens Axboe 
74249f1c68eSPavel Begunkov 	ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask, issue_flags);
743de08356fSPavel Begunkov 	if (ret)
744de08356fSPavel Begunkov 		return ret > 0 ? IO_APOLL_READY : IO_APOLL_ABORTED;
74548863ffdSPavel Begunkov 	trace_io_uring_poll_arm(req, mask, apoll->poll.events);
746329061d3SJens Axboe 	return IO_APOLL_OK;
747329061d3SJens Axboe }
748329061d3SJens Axboe 
io_poll_remove_all_table(struct task_struct * tsk,struct io_hash_table * table,bool cancel_all)7499ca9fb24SPavel Begunkov static __cold bool io_poll_remove_all_table(struct task_struct *tsk,
7509ca9fb24SPavel Begunkov 					    struct io_hash_table *table,
751329061d3SJens Axboe 					    bool cancel_all)
752329061d3SJens Axboe {
753e6f89be6SPavel Begunkov 	unsigned nr_buckets = 1U << table->hash_bits;
754329061d3SJens Axboe 	struct hlist_node *tmp;
755329061d3SJens Axboe 	struct io_kiocb *req;
756329061d3SJens Axboe 	bool found = false;
757329061d3SJens Axboe 	int i;
758329061d3SJens Axboe 
759e6f89be6SPavel Begunkov 	for (i = 0; i < nr_buckets; i++) {
760e6f89be6SPavel Begunkov 		struct io_hash_bucket *hb = &table->hbs[i];
761329061d3SJens Axboe 
76238513c46SHao Xu 		spin_lock(&hb->lock);
76338513c46SHao Xu 		hlist_for_each_entry_safe(req, tmp, &hb->list, hash_node) {
764329061d3SJens Axboe 			if (io_match_task_safe(req, tsk, cancel_all)) {
765329061d3SJens Axboe 				hlist_del_init(&req->hash_node);
766329061d3SJens Axboe 				io_poll_cancel_req(req);
767329061d3SJens Axboe 				found = true;
768329061d3SJens Axboe 			}
769329061d3SJens Axboe 		}
77038513c46SHao Xu 		spin_unlock(&hb->lock);
771329061d3SJens Axboe 	}
772329061d3SJens Axboe 	return found;
773329061d3SJens Axboe }
774329061d3SJens Axboe 
7759ca9fb24SPavel Begunkov /*
7769ca9fb24SPavel Begunkov  * Returns true if we found and killed one or more poll requests
7779ca9fb24SPavel Begunkov  */
io_poll_remove_all(struct io_ring_ctx * ctx,struct task_struct * tsk,bool cancel_all)7789ca9fb24SPavel Begunkov __cold bool io_poll_remove_all(struct io_ring_ctx *ctx, struct task_struct *tsk,
7799ca9fb24SPavel Begunkov 			       bool cancel_all)
7809ca9fb24SPavel Begunkov 	__must_hold(&ctx->uring_lock)
7819ca9fb24SPavel Begunkov {
782b321823aSPavel Begunkov 	bool ret;
783b321823aSPavel Begunkov 
784b321823aSPavel Begunkov 	ret = io_poll_remove_all_table(tsk, &ctx->cancel_table, cancel_all);
785b321823aSPavel Begunkov 	ret |= io_poll_remove_all_table(tsk, &ctx->cancel_table_locked, cancel_all);
786b321823aSPavel Begunkov 	return ret;
7879ca9fb24SPavel Begunkov }
7889ca9fb24SPavel Begunkov 
io_poll_find(struct io_ring_ctx * ctx,bool poll_only,struct io_cancel_data * cd,struct io_hash_table * table,struct io_hash_bucket ** out_bucket)789329061d3SJens Axboe static struct io_kiocb *io_poll_find(struct io_ring_ctx *ctx, bool poll_only,
7901ab1edb0SPavel Begunkov 				     struct io_cancel_data *cd,
791e6f89be6SPavel Begunkov 				     struct io_hash_table *table,
7921ab1edb0SPavel Begunkov 				     struct io_hash_bucket **out_bucket)
793329061d3SJens Axboe {
794329061d3SJens Axboe 	struct io_kiocb *req;
795e6f89be6SPavel Begunkov 	u32 index = hash_long(cd->data, table->hash_bits);
796e6f89be6SPavel Begunkov 	struct io_hash_bucket *hb = &table->hbs[index];
797329061d3SJens Axboe 
7981ab1edb0SPavel Begunkov 	*out_bucket = NULL;
7991ab1edb0SPavel Begunkov 
80038513c46SHao Xu 	spin_lock(&hb->lock);
80138513c46SHao Xu 	hlist_for_each_entry(req, &hb->list, hash_node) {
802329061d3SJens Axboe 		if (cd->data != req->cqe.user_data)
803329061d3SJens Axboe 			continue;
804329061d3SJens Axboe 		if (poll_only && req->opcode != IORING_OP_POLL_ADD)
805329061d3SJens Axboe 			continue;
806329061d3SJens Axboe 		if (cd->flags & IORING_ASYNC_CANCEL_ALL) {
807329061d3SJens Axboe 			if (cd->seq == req->work.cancel_seq)
808329061d3SJens Axboe 				continue;
809329061d3SJens Axboe 			req->work.cancel_seq = cd->seq;
810329061d3SJens Axboe 		}
8111ab1edb0SPavel Begunkov 		*out_bucket = hb;
812329061d3SJens Axboe 		return req;
813329061d3SJens Axboe 	}
81438513c46SHao Xu 	spin_unlock(&hb->lock);
815329061d3SJens Axboe 	return NULL;
816329061d3SJens Axboe }
817329061d3SJens Axboe 
io_poll_file_find(struct io_ring_ctx * ctx,struct io_cancel_data * cd,struct io_hash_table * table,struct io_hash_bucket ** out_bucket)818329061d3SJens Axboe static struct io_kiocb *io_poll_file_find(struct io_ring_ctx *ctx,
8191ab1edb0SPavel Begunkov 					  struct io_cancel_data *cd,
820e6f89be6SPavel Begunkov 					  struct io_hash_table *table,
8211ab1edb0SPavel Begunkov 					  struct io_hash_bucket **out_bucket)
822329061d3SJens Axboe {
823e6f89be6SPavel Begunkov 	unsigned nr_buckets = 1U << table->hash_bits;
824329061d3SJens Axboe 	struct io_kiocb *req;
825329061d3SJens Axboe 	int i;
826329061d3SJens Axboe 
8271ab1edb0SPavel Begunkov 	*out_bucket = NULL;
8281ab1edb0SPavel Begunkov 
829e6f89be6SPavel Begunkov 	for (i = 0; i < nr_buckets; i++) {
830e6f89be6SPavel Begunkov 		struct io_hash_bucket *hb = &table->hbs[i];
831329061d3SJens Axboe 
83238513c46SHao Xu 		spin_lock(&hb->lock);
83338513c46SHao Xu 		hlist_for_each_entry(req, &hb->list, hash_node) {
834a30badf6SJens Axboe 			if (io_cancel_req_match(req, cd)) {
8351ab1edb0SPavel Begunkov 				*out_bucket = hb;
836329061d3SJens Axboe 				return req;
837329061d3SJens Axboe 			}
838a30badf6SJens Axboe 		}
83938513c46SHao Xu 		spin_unlock(&hb->lock);
840329061d3SJens Axboe 	}
841329061d3SJens Axboe 	return NULL;
842329061d3SJens Axboe }
843329061d3SJens Axboe 
io_poll_disarm(struct io_kiocb * req)8449ca9fb24SPavel Begunkov static int io_poll_disarm(struct io_kiocb *req)
845329061d3SJens Axboe {
8469ca9fb24SPavel Begunkov 	if (!req)
8479ca9fb24SPavel Begunkov 		return -ENOENT;
848329061d3SJens Axboe 	if (!io_poll_get_ownership(req))
8499ca9fb24SPavel Begunkov 		return -EALREADY;
850329061d3SJens Axboe 	io_poll_remove_entries(req);
851329061d3SJens Axboe 	hash_del(&req->hash_node);
8529ca9fb24SPavel Begunkov 	return 0;
853329061d3SJens Axboe }
854329061d3SJens Axboe 
__io_poll_cancel(struct io_ring_ctx * ctx,struct io_cancel_data * cd,struct io_hash_table * table)855a2cdd519SPavel Begunkov static int __io_poll_cancel(struct io_ring_ctx *ctx, struct io_cancel_data *cd,
856e6f89be6SPavel Begunkov 			    struct io_hash_table *table)
857329061d3SJens Axboe {
8581ab1edb0SPavel Begunkov 	struct io_hash_bucket *bucket;
859329061d3SJens Axboe 	struct io_kiocb *req;
860329061d3SJens Axboe 
861d7b8b079SJens Axboe 	if (cd->flags & (IORING_ASYNC_CANCEL_FD | IORING_ASYNC_CANCEL_OP |
862d7b8b079SJens Axboe 			 IORING_ASYNC_CANCEL_ANY))
863e6f89be6SPavel Begunkov 		req = io_poll_file_find(ctx, cd, table, &bucket);
864329061d3SJens Axboe 	else
865e6f89be6SPavel Begunkov 		req = io_poll_find(ctx, false, cd, table, &bucket);
8661ab1edb0SPavel Begunkov 
8671ab1edb0SPavel Begunkov 	if (req)
868329061d3SJens Axboe 		io_poll_cancel_req(req);
8691ab1edb0SPavel Begunkov 	if (bucket)
8701ab1edb0SPavel Begunkov 		spin_unlock(&bucket->lock);
8711ab1edb0SPavel Begunkov 	return req ? 0 : -ENOENT;
872329061d3SJens Axboe }
873329061d3SJens Axboe 
io_poll_cancel(struct io_ring_ctx * ctx,struct io_cancel_data * cd,unsigned issue_flags)8745d7943d9SPavel Begunkov int io_poll_cancel(struct io_ring_ctx *ctx, struct io_cancel_data *cd,
8755d7943d9SPavel Begunkov 		   unsigned issue_flags)
876a2cdd519SPavel Begunkov {
8779ca9fb24SPavel Begunkov 	int ret;
8789ca9fb24SPavel Begunkov 
8799ca9fb24SPavel Begunkov 	ret = __io_poll_cancel(ctx, cd, &ctx->cancel_table);
8809ca9fb24SPavel Begunkov 	if (ret != -ENOENT)
8819ca9fb24SPavel Begunkov 		return ret;
8829ca9fb24SPavel Begunkov 
8839ca9fb24SPavel Begunkov 	io_ring_submit_lock(ctx, issue_flags);
8849ca9fb24SPavel Begunkov 	ret = __io_poll_cancel(ctx, cd, &ctx->cancel_table_locked);
8859ca9fb24SPavel Begunkov 	io_ring_submit_unlock(ctx, issue_flags);
8869ca9fb24SPavel Begunkov 	return ret;
887a2cdd519SPavel Begunkov }
888a2cdd519SPavel Begunkov 
io_poll_parse_events(const struct io_uring_sqe * sqe,unsigned int flags)889329061d3SJens Axboe static __poll_t io_poll_parse_events(const struct io_uring_sqe *sqe,
890329061d3SJens Axboe 				     unsigned int flags)
891329061d3SJens Axboe {
892329061d3SJens Axboe 	u32 events;
893329061d3SJens Axboe 
894329061d3SJens Axboe 	events = READ_ONCE(sqe->poll32_events);
895329061d3SJens Axboe #ifdef __BIG_ENDIAN
896329061d3SJens Axboe 	events = swahw32(events);
897329061d3SJens Axboe #endif
898329061d3SJens Axboe 	if (!(flags & IORING_POLL_ADD_MULTI))
899329061d3SJens Axboe 		events |= EPOLLONESHOT;
900b9ba8a44SJens Axboe 	if (!(flags & IORING_POLL_ADD_LEVEL))
901b9ba8a44SJens Axboe 		events |= EPOLLET;
902b9ba8a44SJens Axboe 	return demangle_poll(events) |
903b9ba8a44SJens Axboe 		(events & (EPOLLEXCLUSIVE|EPOLLONESHOT|EPOLLET));
904329061d3SJens Axboe }
905329061d3SJens Axboe 
io_poll_remove_prep(struct io_kiocb * req,const struct io_uring_sqe * sqe)906329061d3SJens Axboe int io_poll_remove_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
907329061d3SJens Axboe {
908f2ccb5aeSStefan Metzmacher 	struct io_poll_update *upd = io_kiocb_to_cmd(req, struct io_poll_update);
909329061d3SJens Axboe 	u32 flags;
910329061d3SJens Axboe 
911329061d3SJens Axboe 	if (sqe->buf_index || sqe->splice_fd_in)
912329061d3SJens Axboe 		return -EINVAL;
913329061d3SJens Axboe 	flags = READ_ONCE(sqe->len);
914329061d3SJens Axboe 	if (flags & ~(IORING_POLL_UPDATE_EVENTS | IORING_POLL_UPDATE_USER_DATA |
915329061d3SJens Axboe 		      IORING_POLL_ADD_MULTI))
916329061d3SJens Axboe 		return -EINVAL;
917329061d3SJens Axboe 	/* meaningless without update */
918329061d3SJens Axboe 	if (flags == IORING_POLL_ADD_MULTI)
919329061d3SJens Axboe 		return -EINVAL;
920329061d3SJens Axboe 
921329061d3SJens Axboe 	upd->old_user_data = READ_ONCE(sqe->addr);
922329061d3SJens Axboe 	upd->update_events = flags & IORING_POLL_UPDATE_EVENTS;
923329061d3SJens Axboe 	upd->update_user_data = flags & IORING_POLL_UPDATE_USER_DATA;
924329061d3SJens Axboe 
925329061d3SJens Axboe 	upd->new_user_data = READ_ONCE(sqe->off);
926329061d3SJens Axboe 	if (!upd->update_user_data && upd->new_user_data)
927329061d3SJens Axboe 		return -EINVAL;
928329061d3SJens Axboe 	if (upd->update_events)
929329061d3SJens Axboe 		upd->events = io_poll_parse_events(sqe, flags);
930329061d3SJens Axboe 	else if (sqe->poll32_events)
931329061d3SJens Axboe 		return -EINVAL;
932329061d3SJens Axboe 
933329061d3SJens Axboe 	return 0;
934329061d3SJens Axboe }
935329061d3SJens Axboe 
io_poll_add_prep(struct io_kiocb * req,const struct io_uring_sqe * sqe)936329061d3SJens Axboe int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
937329061d3SJens Axboe {
938f2ccb5aeSStefan Metzmacher 	struct io_poll *poll = io_kiocb_to_cmd(req, struct io_poll);
939329061d3SJens Axboe 	u32 flags;
940329061d3SJens Axboe 
941329061d3SJens Axboe 	if (sqe->buf_index || sqe->off || sqe->addr)
942329061d3SJens Axboe 		return -EINVAL;
943329061d3SJens Axboe 	flags = READ_ONCE(sqe->len);
944d59bd748SJens Axboe 	if (flags & ~IORING_POLL_ADD_MULTI)
945329061d3SJens Axboe 		return -EINVAL;
946329061d3SJens Axboe 	if ((flags & IORING_POLL_ADD_MULTI) && (req->flags & REQ_F_CQE_SKIP))
947329061d3SJens Axboe 		return -EINVAL;
948329061d3SJens Axboe 
949329061d3SJens Axboe 	poll->events = io_poll_parse_events(sqe, flags);
950329061d3SJens Axboe 	return 0;
951329061d3SJens Axboe }
952329061d3SJens Axboe 
io_poll_add(struct io_kiocb * req,unsigned int issue_flags)953329061d3SJens Axboe int io_poll_add(struct io_kiocb *req, unsigned int issue_flags)
954329061d3SJens Axboe {
955f2ccb5aeSStefan Metzmacher 	struct io_poll *poll = io_kiocb_to_cmd(req, struct io_poll);
956329061d3SJens Axboe 	struct io_poll_table ipt;
957329061d3SJens Axboe 	int ret;
958329061d3SJens Axboe 
959329061d3SJens Axboe 	ipt.pt._qproc = io_poll_queue_proc;
960329061d3SJens Axboe 
9619ca9fb24SPavel Begunkov 	/*
9629ca9fb24SPavel Begunkov 	 * If sqpoll or single issuer, there is no contention for ->uring_lock
9639ca9fb24SPavel Begunkov 	 * and we'll end up holding it in tw handlers anyway.
9649ca9fb24SPavel Begunkov 	 */
965e8375e43SPavel Begunkov 	if (req->ctx->flags & (IORING_SETUP_SQPOLL|IORING_SETUP_SINGLE_ISSUER))
9669ca9fb24SPavel Begunkov 		req->flags |= REQ_F_HASH_LOCKED;
9679ca9fb24SPavel Begunkov 
96849f1c68eSPavel Begunkov 	ret = __io_arm_poll_handler(req, poll, &ipt, poll->events, issue_flags);
969de08356fSPavel Begunkov 	if (ret > 0) {
970063a0079SPavel Begunkov 		io_req_set_res(req, ipt.result_mask, 0);
971329061d3SJens Axboe 		return IOU_OK;
972329061d3SJens Axboe 	}
973de08356fSPavel Begunkov 	return ret ?: IOU_ISSUE_SKIP_COMPLETE;
974329061d3SJens Axboe }
975329061d3SJens Axboe 
io_poll_remove(struct io_kiocb * req,unsigned int issue_flags)976329061d3SJens Axboe int io_poll_remove(struct io_kiocb *req, unsigned int issue_flags)
977329061d3SJens Axboe {
978f2ccb5aeSStefan Metzmacher 	struct io_poll_update *poll_update = io_kiocb_to_cmd(req, struct io_poll_update);
979329061d3SJens Axboe 	struct io_ring_ctx *ctx = req->ctx;
980ad711c5dSJens Axboe 	struct io_cancel_data cd = { .ctx = ctx, .data = poll_update->old_user_data, };
9811ab1edb0SPavel Begunkov 	struct io_hash_bucket *bucket;
982329061d3SJens Axboe 	struct io_kiocb *preq;
983329061d3SJens Axboe 	int ret2, ret = 0;
984329061d3SJens Axboe 
985ef7dfac5SJens Axboe 	io_ring_submit_lock(ctx, issue_flags);
986e6f89be6SPavel Begunkov 	preq = io_poll_find(ctx, true, &cd, &ctx->cancel_table, &bucket);
9871ab1edb0SPavel Begunkov 	ret2 = io_poll_disarm(preq);
9881ab1edb0SPavel Begunkov 	if (bucket)
9891ab1edb0SPavel Begunkov 		spin_unlock(&bucket->lock);
9909ca9fb24SPavel Begunkov 	if (!ret2)
9919ca9fb24SPavel Begunkov 		goto found;
9929ca9fb24SPavel Begunkov 	if (ret2 != -ENOENT) {
9939ca9fb24SPavel Begunkov 		ret = ret2;
99438513c46SHao Xu 		goto out;
99538513c46SHao Xu 	}
996329061d3SJens Axboe 
9979ca9fb24SPavel Begunkov 	preq = io_poll_find(ctx, true, &cd, &ctx->cancel_table_locked, &bucket);
9989ca9fb24SPavel Begunkov 	ret2 = io_poll_disarm(preq);
9999ca9fb24SPavel Begunkov 	if (bucket)
10009ca9fb24SPavel Begunkov 		spin_unlock(&bucket->lock);
10019ca9fb24SPavel Begunkov 	if (ret2) {
10029ca9fb24SPavel Begunkov 		ret = ret2;
10039ca9fb24SPavel Begunkov 		goto out;
10049ca9fb24SPavel Begunkov 	}
10059ca9fb24SPavel Begunkov 
10069ca9fb24SPavel Begunkov found:
1007bce5d70cSPavel Begunkov 	if (WARN_ON_ONCE(preq->opcode != IORING_OP_POLL_ADD)) {
1008bce5d70cSPavel Begunkov 		ret = -EFAULT;
1009bce5d70cSPavel Begunkov 		goto out;
1010bce5d70cSPavel Begunkov 	}
1011bce5d70cSPavel Begunkov 
1012329061d3SJens Axboe 	if (poll_update->update_events || poll_update->update_user_data) {
1013329061d3SJens Axboe 		/* only mask one event flags, keep behavior flags */
1014329061d3SJens Axboe 		if (poll_update->update_events) {
1015f2ccb5aeSStefan Metzmacher 			struct io_poll *poll = io_kiocb_to_cmd(preq, struct io_poll);
1016329061d3SJens Axboe 
1017329061d3SJens Axboe 			poll->events &= ~0xffff;
1018329061d3SJens Axboe 			poll->events |= poll_update->events & 0xffff;
1019329061d3SJens Axboe 			poll->events |= IO_POLL_UNMASK;
1020329061d3SJens Axboe 		}
1021329061d3SJens Axboe 		if (poll_update->update_user_data)
1022329061d3SJens Axboe 			preq->cqe.user_data = poll_update->new_user_data;
1023329061d3SJens Axboe 
1024ef7dfac5SJens Axboe 		ret2 = io_poll_add(preq, issue_flags & ~IO_URING_F_UNLOCKED);
1025329061d3SJens Axboe 		/* successfully updated, don't complete poll request */
1026329061d3SJens Axboe 		if (!ret2 || ret2 == -EIOCBQUEUED)
1027329061d3SJens Axboe 			goto out;
1028329061d3SJens Axboe 	}
1029329061d3SJens Axboe 
1030329061d3SJens Axboe 	req_set_fail(preq);
1031329061d3SJens Axboe 	io_req_set_res(preq, -ECANCELED, 0);
1032*4cbc5e93SPavel Begunkov 	preq->io_task_work.func = io_req_task_complete;
1033*4cbc5e93SPavel Begunkov 	io_req_task_work_add(preq);
1034329061d3SJens Axboe out:
1035ef7dfac5SJens Axboe 	io_ring_submit_unlock(ctx, issue_flags);
1036329061d3SJens Axboe 	if (ret < 0) {
1037329061d3SJens Axboe 		req_set_fail(req);
1038329061d3SJens Axboe 		return ret;
1039329061d3SJens Axboe 	}
1040329061d3SJens Axboe 	/* complete update request, we're done with it */
1041329061d3SJens Axboe 	io_req_set_res(req, ret, 0);
1042329061d3SJens Axboe 	return IOU_OK;
1043329061d3SJens Axboe }
10449da7471eSJens Axboe 
io_apoll_cache_free(struct io_cache_entry * entry)10459b797a37SJens Axboe void io_apoll_cache_free(struct io_cache_entry *entry)
10469da7471eSJens Axboe {
10479b797a37SJens Axboe 	kfree(container_of(entry, struct async_poll, cache));
10489da7471eSJens Axboe }
1049