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 }
311*130675a2SJens Axboe if (unlikely(req->cqe.res & EPOLLERR))
312*130675a2SJens Axboe req_set_fail(req);
313329061d3SJens Axboe if (req->apoll_events & EPOLLONESHOT)
3142ba69707SDylan Yudaken return IOU_POLL_DONE;
315329061d3SJens Axboe
316329061d3SJens Axboe /* multishot, just fill a CQE and proceed */
317329061d3SJens Axboe if (!(req->flags & REQ_F_APOLL_MULTISHOT)) {
318329061d3SJens Axboe __poll_t mask = mangle_poll(req->cqe.res &
319329061d3SJens Axboe req->apoll_events);
320329061d3SJens Axboe
321b6b2bb58SPavel Begunkov if (!io_fill_cqe_req_aux(req, ts->locked, mask,
322b6b2bb58SPavel Begunkov IORING_CQE_F_MORE)) {
323a2da6763SDylan Yudaken io_req_set_res(req, mask, 0);
324a2da6763SDylan Yudaken return IOU_POLL_REMOVE_POLL_USE_RES;
325a2da6763SDylan Yudaken }
326d245bca6SPavel Begunkov } else {
327a282967cSPavel Begunkov int ret = io_poll_issue(req, ts);
328114eccdfSDylan Yudaken if (ret == IOU_STOP_MULTISHOT)
329114eccdfSDylan Yudaken return IOU_POLL_REMOVE_POLL_USE_RES;
3307cbd3aa5SJens Axboe else if (ret == IOU_REQUEUE)
3317cbd3aa5SJens Axboe return IOU_POLL_REQUEUE;
3322ba69707SDylan Yudaken if (ret < 0)
333329061d3SJens Axboe return ret;
334d245bca6SPavel Begunkov }
335329061d3SJens Axboe
336b98186aeSPavel Begunkov /* force the next iteration to vfs_poll() */
337b98186aeSPavel Begunkov req->cqe.res = 0;
338b98186aeSPavel Begunkov
339329061d3SJens Axboe /*
340329061d3SJens Axboe * Release all references, retry if someone tried to restart
341329061d3SJens Axboe * task_work while we were executing it.
342329061d3SJens Axboe */
34312ad3d2dSLin Ma } while (atomic_sub_return(v & IO_POLL_REF_MASK, &req->poll_refs) &
34412ad3d2dSLin Ma IO_POLL_REF_MASK);
345329061d3SJens Axboe
3462ba69707SDylan Yudaken return IOU_POLL_NO_ACTION;
347329061d3SJens Axboe }
348329061d3SJens Axboe
io_poll_task_func(struct io_kiocb * req,struct io_tw_state * ts)349c92fcfc2SJens Axboe void io_poll_task_func(struct io_kiocb *req, struct io_tw_state *ts)
350329061d3SJens Axboe {
351329061d3SJens Axboe int ret;
352329061d3SJens Axboe
353a282967cSPavel Begunkov ret = io_poll_check_events(req, ts);
3547cbd3aa5SJens Axboe if (ret == IOU_POLL_NO_ACTION) {
355b86f1d51SPavel Begunkov io_kbuf_recycle(req, 0);
356329061d3SJens Axboe return;
3577cbd3aa5SJens Axboe } else if (ret == IOU_POLL_REQUEUE) {
358b86f1d51SPavel Begunkov io_kbuf_recycle(req, 0);
3597cbd3aa5SJens Axboe __io_poll_execute(req, 0);
3607cbd3aa5SJens Axboe return;
3617cbd3aa5SJens Axboe }
362443e5755SPavel Begunkov io_poll_remove_entries(req);
363a282967cSPavel Begunkov io_poll_tw_hash_eject(req, ts);
364329061d3SJens Axboe
365443e5755SPavel Begunkov if (req->opcode == IORING_OP_POLL_ADD) {
3662ba69707SDylan Yudaken if (ret == IOU_POLL_DONE) {
367443e5755SPavel Begunkov struct io_poll *poll;
368443e5755SPavel Begunkov
369443e5755SPavel Begunkov poll = io_kiocb_to_cmd(req, struct io_poll);
370329061d3SJens Axboe req->cqe.res = mangle_poll(req->cqe.res & poll->events);
3716e5aedb9SJens Axboe } else if (ret == IOU_POLL_REISSUE) {
372a282967cSPavel Begunkov io_req_task_submit(req, ts);
3736e5aedb9SJens Axboe return;
374114eccdfSDylan Yudaken } else if (ret != IOU_POLL_REMOVE_POLL_USE_RES) {
375329061d3SJens Axboe req->cqe.res = ret;
376329061d3SJens Axboe req_set_fail(req);
377329061d3SJens Axboe }
378329061d3SJens Axboe
3790ec6dca2SPavel Begunkov io_req_set_res(req, req->cqe.res, 0);
380a282967cSPavel Begunkov io_req_task_complete(req, ts);
381443e5755SPavel Begunkov } else {
382a282967cSPavel Begunkov io_tw_lock(req->ctx, ts);
383329061d3SJens Axboe
384114eccdfSDylan Yudaken if (ret == IOU_POLL_REMOVE_POLL_USE_RES)
385a282967cSPavel Begunkov io_req_task_complete(req, ts);
3866e5aedb9SJens Axboe else if (ret == IOU_POLL_DONE || ret == IOU_POLL_REISSUE)
387a282967cSPavel Begunkov io_req_task_submit(req, ts);
388329061d3SJens Axboe else
389973fc83fSDylan Yudaken io_req_defer_failed(req, ret);
390329061d3SJens Axboe }
391443e5755SPavel Begunkov }
392329061d3SJens Axboe
io_poll_cancel_req(struct io_kiocb * req)393329061d3SJens Axboe static void io_poll_cancel_req(struct io_kiocb *req)
394329061d3SJens Axboe {
395329061d3SJens Axboe io_poll_mark_cancelled(req);
396329061d3SJens Axboe /* kick tw, which should complete the request */
39713a99017SPavel Begunkov io_poll_execute(req, 0);
398329061d3SJens Axboe }
399329061d3SJens Axboe
400329061d3SJens Axboe #define IO_ASYNC_POLL_COMMON (EPOLLONESHOT | EPOLLPRI)
401329061d3SJens Axboe
io_pollfree_wake(struct io_kiocb * req,struct io_poll * poll)402fe991a76SJens Axboe static __cold int io_pollfree_wake(struct io_kiocb *req, struct io_poll *poll)
403329061d3SJens Axboe {
404329061d3SJens Axboe io_poll_mark_cancelled(req);
405329061d3SJens Axboe /* we have to kick tw in case it's not already */
40613a99017SPavel Begunkov io_poll_execute(req, 0);
407329061d3SJens Axboe
408329061d3SJens Axboe /*
409329061d3SJens Axboe * If the waitqueue is being freed early but someone is already
410329061d3SJens Axboe * holds ownership over it, we have to tear down the request as
411329061d3SJens Axboe * best we can. That means immediately removing the request from
412329061d3SJens Axboe * its waitqueue and preventing all further accesses to the
413329061d3SJens Axboe * waitqueue via the request.
414329061d3SJens Axboe */
415329061d3SJens Axboe list_del_init(&poll->wait.entry);
416329061d3SJens Axboe
417329061d3SJens Axboe /*
418329061d3SJens Axboe * Careful: this *must* be the last step, since as soon
419329061d3SJens Axboe * as req->head is NULL'ed out, the request can be
420329061d3SJens Axboe * completed and freed, since aio_poll_complete_work()
421329061d3SJens Axboe * will no longer need to take the waitqueue lock.
422329061d3SJens Axboe */
423329061d3SJens Axboe smp_store_release(&poll->head, NULL);
424329061d3SJens Axboe return 1;
425329061d3SJens Axboe }
426329061d3SJens Axboe
io_poll_wake(struct wait_queue_entry * wait,unsigned mode,int sync,void * key)427fe991a76SJens Axboe static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
428fe991a76SJens Axboe void *key)
429fe991a76SJens Axboe {
430fe991a76SJens Axboe struct io_kiocb *req = wqe_to_req(wait);
431fe991a76SJens Axboe struct io_poll *poll = container_of(wait, struct io_poll, wait);
432fe991a76SJens Axboe __poll_t mask = key_to_poll(key);
433fe991a76SJens Axboe
434fe991a76SJens Axboe if (unlikely(mask & POLLFREE))
435fe991a76SJens Axboe return io_pollfree_wake(req, poll);
436fe991a76SJens Axboe
437329061d3SJens Axboe /* for instances that support it check for an event match first */
438329061d3SJens Axboe if (mask && !(mask & (poll->events & ~IO_ASYNC_POLL_COMMON)))
439329061d3SJens Axboe return 0;
440329061d3SJens Axboe
441329061d3SJens Axboe if (io_poll_get_ownership(req)) {
44244648532SJens Axboe /*
44344648532SJens Axboe * If we trigger a multishot poll off our own wakeup path,
44444648532SJens Axboe * disable multishot as there is a circular dependency between
44544648532SJens Axboe * CQ posting and triggering the event.
44644648532SJens Axboe */
44744648532SJens Axboe if (mask & EPOLL_URING_WAKE)
44844648532SJens Axboe poll->events |= EPOLLONESHOT;
44944648532SJens Axboe
450329061d3SJens Axboe /* optional, saves extra locking for removal in tw handler */
451329061d3SJens Axboe if (mask && poll->events & EPOLLONESHOT) {
452329061d3SJens Axboe list_del_init(&poll->wait.entry);
453329061d3SJens Axboe poll->head = NULL;
454329061d3SJens Axboe if (wqe_is_double(wait))
455329061d3SJens Axboe req->flags &= ~REQ_F_DOUBLE_POLL;
456329061d3SJens Axboe else
457329061d3SJens Axboe req->flags &= ~REQ_F_SINGLE_POLL;
458329061d3SJens Axboe }
45913a99017SPavel Begunkov __io_poll_execute(req, mask);
460329061d3SJens Axboe }
461329061d3SJens Axboe return 1;
462329061d3SJens Axboe }
463329061d3SJens Axboe
46430a33669SPavel Begunkov /* fails only when polling is already completing by the first entry */
io_poll_double_prepare(struct io_kiocb * req)46530a33669SPavel Begunkov static bool io_poll_double_prepare(struct io_kiocb *req)
46649f1c68eSPavel Begunkov {
46749f1c68eSPavel Begunkov struct wait_queue_head *head;
46849f1c68eSPavel Begunkov struct io_poll *poll = io_poll_get_single(req);
46949f1c68eSPavel Begunkov
47049f1c68eSPavel Begunkov /* head is RCU protected, see io_poll_remove_entries() comments */
47149f1c68eSPavel Begunkov rcu_read_lock();
47249f1c68eSPavel Begunkov head = smp_load_acquire(&poll->head);
47349f1c68eSPavel Begunkov /*
47430a33669SPavel Begunkov * poll arm might not hold ownership and so race for req->flags with
47530a33669SPavel Begunkov * io_poll_wake(). There is only one poll entry queued, serialise with
47630a33669SPavel Begunkov * it by taking its head lock. As we're still arming the tw hanlder
47730a33669SPavel Begunkov * is not going to be run, so there are no races with it.
47849f1c68eSPavel Begunkov */
47930a33669SPavel Begunkov if (head) {
48049f1c68eSPavel Begunkov spin_lock_irq(&head->lock);
48149f1c68eSPavel Begunkov req->flags |= REQ_F_DOUBLE_POLL;
482ceff5017SPavel Begunkov if (req->opcode == IORING_OP_POLL_ADD)
483ceff5017SPavel Begunkov req->flags |= REQ_F_ASYNC_DATA;
48449f1c68eSPavel Begunkov spin_unlock_irq(&head->lock);
48530a33669SPavel Begunkov }
48649f1c68eSPavel Begunkov rcu_read_unlock();
48730a33669SPavel Begunkov return !!head;
48849f1c68eSPavel Begunkov }
48949f1c68eSPavel Begunkov
__io_queue_proc(struct io_poll * poll,struct io_poll_table * pt,struct wait_queue_head * head,struct io_poll ** poll_ptr)490329061d3SJens Axboe static void __io_queue_proc(struct io_poll *poll, struct io_poll_table *pt,
491329061d3SJens Axboe struct wait_queue_head *head,
492329061d3SJens Axboe struct io_poll **poll_ptr)
493329061d3SJens Axboe {
494329061d3SJens Axboe struct io_kiocb *req = pt->req;
495329061d3SJens Axboe unsigned long wqe_private = (unsigned long) req;
496329061d3SJens Axboe
497329061d3SJens Axboe /*
498329061d3SJens Axboe * The file being polled uses multiple waitqueues for poll handling
499329061d3SJens Axboe * (e.g. one for read, one for write). Setup a separate io_poll
500329061d3SJens Axboe * if this happens.
501329061d3SJens Axboe */
502329061d3SJens Axboe if (unlikely(pt->nr_entries)) {
503329061d3SJens Axboe struct io_poll *first = poll;
504329061d3SJens Axboe
505329061d3SJens Axboe /* double add on the same waitqueue head, ignore */
506329061d3SJens Axboe if (first->head == head)
507329061d3SJens Axboe return;
508329061d3SJens Axboe /* already have a 2nd entry, fail a third attempt */
509329061d3SJens Axboe if (*poll_ptr) {
510329061d3SJens Axboe if ((*poll_ptr)->head == head)
511329061d3SJens Axboe return;
512329061d3SJens Axboe pt->error = -EINVAL;
513329061d3SJens Axboe return;
514329061d3SJens Axboe }
515329061d3SJens Axboe
516329061d3SJens Axboe poll = kmalloc(sizeof(*poll), GFP_ATOMIC);
517329061d3SJens Axboe if (!poll) {
518329061d3SJens Axboe pt->error = -ENOMEM;
519329061d3SJens Axboe return;
520329061d3SJens Axboe }
52149f1c68eSPavel Begunkov
522329061d3SJens Axboe /* mark as double wq entry */
5230638cd7bSPavel Begunkov wqe_private |= IO_WQE_F_DOUBLE;
5241947ddf9SJens Axboe io_init_poll_iocb(poll, first->events);
52530a33669SPavel Begunkov if (!io_poll_double_prepare(req)) {
52630a33669SPavel Begunkov /* the request is completing, just back off */
52730a33669SPavel Begunkov kfree(poll);
52830a33669SPavel Begunkov return;
52930a33669SPavel Begunkov }
530329061d3SJens Axboe *poll_ptr = poll;
53149f1c68eSPavel Begunkov } else {
53249f1c68eSPavel Begunkov /* fine to modify, there is no poll queued to race with us */
53349f1c68eSPavel Begunkov req->flags |= REQ_F_SINGLE_POLL;
534329061d3SJens Axboe }
535329061d3SJens Axboe
536329061d3SJens Axboe pt->nr_entries++;
537329061d3SJens Axboe poll->head = head;
538329061d3SJens Axboe poll->wait.private = (void *) wqe_private;
539329061d3SJens Axboe
540329061d3SJens Axboe if (poll->events & EPOLLEXCLUSIVE)
541329061d3SJens Axboe add_wait_queue_exclusive(head, &poll->wait);
542329061d3SJens Axboe else
543329061d3SJens Axboe add_wait_queue(head, &poll->wait);
544329061d3SJens Axboe }
545329061d3SJens Axboe
io_poll_queue_proc(struct file * file,struct wait_queue_head * head,struct poll_table_struct * p)546329061d3SJens Axboe static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
547329061d3SJens Axboe struct poll_table_struct *p)
548329061d3SJens Axboe {
549329061d3SJens Axboe struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
550f2ccb5aeSStefan Metzmacher struct io_poll *poll = io_kiocb_to_cmd(pt->req, struct io_poll);
551329061d3SJens Axboe
552329061d3SJens Axboe __io_queue_proc(poll, pt, head,
553329061d3SJens Axboe (struct io_poll **) &pt->req->async_data);
554329061d3SJens Axboe }
555329061d3SJens Axboe
io_poll_can_finish_inline(struct io_kiocb * req,struct io_poll_table * pt)55649f1c68eSPavel Begunkov static bool io_poll_can_finish_inline(struct io_kiocb *req,
55749f1c68eSPavel Begunkov struct io_poll_table *pt)
55849f1c68eSPavel Begunkov {
55949f1c68eSPavel Begunkov return pt->owning || io_poll_get_ownership(req);
56049f1c68eSPavel Begunkov }
56149f1c68eSPavel Begunkov
io_poll_add_hash(struct io_kiocb * req)562febb985cSJens Axboe static void io_poll_add_hash(struct io_kiocb *req)
563febb985cSJens Axboe {
564febb985cSJens Axboe if (req->flags & REQ_F_HASH_LOCKED)
565febb985cSJens Axboe io_poll_req_insert_locked(req);
566febb985cSJens Axboe else
567febb985cSJens Axboe io_poll_req_insert(req);
568febb985cSJens Axboe }
569febb985cSJens Axboe
570de08356fSPavel Begunkov /*
571de08356fSPavel Begunkov * Returns 0 when it's handed over for polling. The caller owns the requests if
572de08356fSPavel Begunkov * it returns non-zero, but otherwise should not touch it. Negative values
573de08356fSPavel Begunkov * contain an error code. When the result is >0, the polling has completed
574de08356fSPavel Begunkov * inline and ipt.result_mask is set to the mask.
575de08356fSPavel Begunkov */
__io_arm_poll_handler(struct io_kiocb * req,struct io_poll * poll,struct io_poll_table * ipt,__poll_t mask,unsigned issue_flags)576329061d3SJens Axboe static int __io_arm_poll_handler(struct io_kiocb *req,
577329061d3SJens Axboe struct io_poll *poll,
57849f1c68eSPavel Begunkov struct io_poll_table *ipt, __poll_t mask,
57949f1c68eSPavel Begunkov unsigned issue_flags)
580329061d3SJens Axboe {
581329061d3SJens Axboe struct io_ring_ctx *ctx = req->ctx;
582329061d3SJens Axboe
583329061d3SJens Axboe INIT_HLIST_NODE(&req->hash_node);
584329061d3SJens Axboe req->work.cancel_seq = atomic_read(&ctx->cancel_seq);
5851947ddf9SJens Axboe io_init_poll_iocb(poll, mask);
586329061d3SJens Axboe poll->file = req->file;
587329061d3SJens Axboe req->apoll_events = poll->events;
588329061d3SJens Axboe
589329061d3SJens Axboe ipt->pt._key = mask;
590329061d3SJens Axboe ipt->req = req;
591329061d3SJens Axboe ipt->error = 0;
592329061d3SJens Axboe ipt->nr_entries = 0;
593329061d3SJens Axboe /*
59449f1c68eSPavel Begunkov * Polling is either completed here or via task_work, so if we're in the
59549f1c68eSPavel Begunkov * task context we're naturally serialised with tw by merit of running
59649f1c68eSPavel Begunkov * the same task. When it's io-wq, take the ownership to prevent tw
59749f1c68eSPavel Begunkov * from running. However, when we're in the task context, skip taking
59849f1c68eSPavel Begunkov * it as an optimisation.
59949f1c68eSPavel Begunkov *
60049f1c68eSPavel Begunkov * Note: even though the request won't be completed/freed, without
60149f1c68eSPavel Begunkov * ownership we still can race with io_poll_wake().
60249f1c68eSPavel Begunkov * io_poll_can_finish_inline() tries to deal with that.
603329061d3SJens Axboe */
60449f1c68eSPavel Begunkov ipt->owning = issue_flags & IO_URING_F_UNLOCKED;
60549f1c68eSPavel Begunkov atomic_set(&req->poll_refs, (int)ipt->owning);
606e8375e43SPavel Begunkov
607e8375e43SPavel Begunkov /* io-wq doesn't hold uring_lock */
608e8375e43SPavel Begunkov if (issue_flags & IO_URING_F_UNLOCKED)
609e8375e43SPavel Begunkov req->flags &= ~REQ_F_HASH_LOCKED;
610e8375e43SPavel Begunkov
611329061d3SJens Axboe mask = vfs_poll(req->file, &ipt->pt) & poll->events;
612329061d3SJens Axboe
613de08356fSPavel Begunkov if (unlikely(ipt->error || !ipt->nr_entries)) {
614de08356fSPavel Begunkov io_poll_remove_entries(req);
615de08356fSPavel Begunkov
61649f1c68eSPavel Begunkov if (!io_poll_can_finish_inline(req, ipt)) {
61749f1c68eSPavel Begunkov io_poll_mark_cancelled(req);
61849f1c68eSPavel Begunkov return 0;
61949f1c68eSPavel Begunkov } else if (mask && (poll->events & EPOLLET)) {
620de08356fSPavel Begunkov ipt->result_mask = mask;
621de08356fSPavel Begunkov return 1;
622de08356fSPavel Begunkov }
62349f1c68eSPavel Begunkov return ipt->error ?: -EINVAL;
624de08356fSPavel Begunkov }
625de08356fSPavel Begunkov
626b9ba8a44SJens Axboe if (mask &&
627b9ba8a44SJens Axboe ((poll->events & (EPOLLET|EPOLLONESHOT)) == (EPOLLET|EPOLLONESHOT))) {
628febb985cSJens Axboe if (!io_poll_can_finish_inline(req, ipt)) {
629febb985cSJens Axboe io_poll_add_hash(req);
63049f1c68eSPavel Begunkov return 0;
631febb985cSJens Axboe }
632329061d3SJens Axboe io_poll_remove_entries(req);
633063a0079SPavel Begunkov ipt->result_mask = mask;
634329061d3SJens Axboe /* no one else has access to the req, forget about the ref */
635063a0079SPavel Begunkov return 1;
636329061d3SJens Axboe }
637b9ba8a44SJens Axboe
638febb985cSJens Axboe io_poll_add_hash(req);
639329061d3SJens Axboe
64049f1c68eSPavel Begunkov if (mask && (poll->events & EPOLLET) &&
64149f1c68eSPavel Begunkov io_poll_can_finish_inline(req, ipt)) {
64213a99017SPavel Begunkov __io_poll_execute(req, mask);
643329061d3SJens Axboe return 0;
644329061d3SJens Axboe }
645329061d3SJens Axboe
64649f1c68eSPavel Begunkov if (ipt->owning) {
647329061d3SJens Axboe /*
6482f389343SPavel Begunkov * Try to release ownership. If we see a change of state, e.g.
6492f389343SPavel Begunkov * poll was waken up, queue up a tw, it'll deal with it.
650329061d3SJens Axboe */
6512f389343SPavel Begunkov if (atomic_cmpxchg(&req->poll_refs, 1, 0) != 1)
65213a99017SPavel Begunkov __io_poll_execute(req, 0);
65349f1c68eSPavel Begunkov }
654329061d3SJens Axboe return 0;
655329061d3SJens Axboe }
656329061d3SJens Axboe
io_async_queue_proc(struct file * file,struct wait_queue_head * head,struct poll_table_struct * p)657329061d3SJens Axboe static void io_async_queue_proc(struct file *file, struct wait_queue_head *head,
658329061d3SJens Axboe struct poll_table_struct *p)
659329061d3SJens Axboe {
660329061d3SJens Axboe struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
661329061d3SJens Axboe struct async_poll *apoll = pt->req->apoll;
662329061d3SJens Axboe
663329061d3SJens Axboe __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll);
664329061d3SJens Axboe }
665329061d3SJens Axboe
666c16bda37SJens Axboe /*
667c16bda37SJens Axboe * We can't reliably detect loops in repeated poll triggers and issue
668c16bda37SJens Axboe * subsequently failing. But rather than fail these immediately, allow a
669c16bda37SJens Axboe * certain amount of retries before we give up. Given that this condition
670c16bda37SJens Axboe * should _rarely_ trigger even once, we should be fine with a larger value.
671c16bda37SJens Axboe */
672c16bda37SJens Axboe #define APOLL_MAX_RETRY 128
673c16bda37SJens Axboe
io_req_alloc_apoll(struct io_kiocb * req,unsigned issue_flags)6745204aa8cSPavel Begunkov static struct async_poll *io_req_alloc_apoll(struct io_kiocb *req,
6755204aa8cSPavel Begunkov unsigned issue_flags)
6765204aa8cSPavel Begunkov {
6775204aa8cSPavel Begunkov struct io_ring_ctx *ctx = req->ctx;
6789b797a37SJens Axboe struct io_cache_entry *entry;
6795204aa8cSPavel Begunkov struct async_poll *apoll;
6805204aa8cSPavel Begunkov
6815204aa8cSPavel Begunkov if (req->flags & REQ_F_POLLED) {
6825204aa8cSPavel Begunkov apoll = req->apoll;
6835204aa8cSPavel Begunkov kfree(apoll->double_poll);
684df730ec2SXinghui Li } else if (!(issue_flags & IO_URING_F_UNLOCKED)) {
685df730ec2SXinghui Li entry = io_alloc_cache_get(&ctx->apoll_cache);
686df730ec2SXinghui Li if (entry == NULL)
687df730ec2SXinghui Li goto alloc_apoll;
6889b797a37SJens Axboe apoll = container_of(entry, struct async_poll, cache);
689c16bda37SJens Axboe apoll->poll.retries = APOLL_MAX_RETRY;
6905204aa8cSPavel Begunkov } else {
691df730ec2SXinghui Li alloc_apoll:
6925204aa8cSPavel Begunkov apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC);
6935204aa8cSPavel Begunkov if (unlikely(!apoll))
6945204aa8cSPavel Begunkov return NULL;
695c16bda37SJens Axboe apoll->poll.retries = APOLL_MAX_RETRY;
6965204aa8cSPavel Begunkov }
6975204aa8cSPavel Begunkov apoll->double_poll = NULL;
6985204aa8cSPavel Begunkov req->apoll = apoll;
699c16bda37SJens Axboe if (unlikely(!--apoll->poll.retries))
700c16bda37SJens Axboe return NULL;
7015204aa8cSPavel Begunkov return apoll;
7025204aa8cSPavel Begunkov }
7035204aa8cSPavel Begunkov
io_arm_poll_handler(struct io_kiocb * req,unsigned issue_flags)704329061d3SJens Axboe int io_arm_poll_handler(struct io_kiocb *req, unsigned issue_flags)
705329061d3SJens Axboe {
706a7dd2782SBreno Leitao const struct io_issue_def *def = &io_issue_defs[req->opcode];
707329061d3SJens Axboe struct async_poll *apoll;
708329061d3SJens Axboe struct io_poll_table ipt;
709b9ba8a44SJens Axboe __poll_t mask = POLLPRI | POLLERR | EPOLLET;
710329061d3SJens Axboe int ret;
711329061d3SJens Axboe
7129ca9fb24SPavel Begunkov /*
7139ca9fb24SPavel Begunkov * apoll requests already grab the mutex to complete in the tw handler,
7149ca9fb24SPavel Begunkov * so removal from the mutex-backed hash is free, use it by default.
7159ca9fb24SPavel Begunkov */
7169ca9fb24SPavel Begunkov req->flags |= REQ_F_HASH_LOCKED;
7179ca9fb24SPavel Begunkov
718329061d3SJens Axboe if (!def->pollin && !def->pollout)
719329061d3SJens Axboe return IO_APOLL_ABORTED;
720329061d3SJens Axboe if (!file_can_poll(req->file))
721329061d3SJens Axboe return IO_APOLL_ABORTED;
722329061d3SJens Axboe if (!(req->flags & REQ_F_APOLL_MULTISHOT))
723329061d3SJens Axboe mask |= EPOLLONESHOT;
724329061d3SJens Axboe
725329061d3SJens Axboe if (def->pollin) {
726329061d3SJens Axboe mask |= EPOLLIN | EPOLLRDNORM;
727329061d3SJens Axboe
728329061d3SJens Axboe /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */
729329061d3SJens Axboe if (req->flags & REQ_F_CLEAR_POLLIN)
730329061d3SJens Axboe mask &= ~EPOLLIN;
731329061d3SJens Axboe } else {
732329061d3SJens Axboe mask |= EPOLLOUT | EPOLLWRNORM;
733329061d3SJens Axboe }
734329061d3SJens Axboe if (def->poll_exclusive)
735329061d3SJens Axboe mask |= EPOLLEXCLUSIVE;
7365204aa8cSPavel Begunkov
7375204aa8cSPavel Begunkov apoll = io_req_alloc_apoll(req, issue_flags);
7385204aa8cSPavel Begunkov if (!apoll)
739329061d3SJens Axboe return IO_APOLL_ABORTED;
740005308f7SJens Axboe req->flags &= ~(REQ_F_SINGLE_POLL | REQ_F_DOUBLE_POLL);
741329061d3SJens Axboe req->flags |= REQ_F_POLLED;
742329061d3SJens Axboe ipt.pt._qproc = io_async_queue_proc;
743329061d3SJens Axboe
744329061d3SJens Axboe io_kbuf_recycle(req, issue_flags);
745329061d3SJens Axboe
74649f1c68eSPavel Begunkov ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask, issue_flags);
747de08356fSPavel Begunkov if (ret)
748de08356fSPavel Begunkov return ret > 0 ? IO_APOLL_READY : IO_APOLL_ABORTED;
74948863ffdSPavel Begunkov trace_io_uring_poll_arm(req, mask, apoll->poll.events);
750329061d3SJens Axboe return IO_APOLL_OK;
751329061d3SJens Axboe }
752329061d3SJens Axboe
io_poll_remove_all_table(struct task_struct * tsk,struct io_hash_table * table,bool cancel_all)7539ca9fb24SPavel Begunkov static __cold bool io_poll_remove_all_table(struct task_struct *tsk,
7549ca9fb24SPavel Begunkov struct io_hash_table *table,
755329061d3SJens Axboe bool cancel_all)
756329061d3SJens Axboe {
757e6f89be6SPavel Begunkov unsigned nr_buckets = 1U << table->hash_bits;
758329061d3SJens Axboe struct hlist_node *tmp;
759329061d3SJens Axboe struct io_kiocb *req;
760329061d3SJens Axboe bool found = false;
761329061d3SJens Axboe int i;
762329061d3SJens Axboe
763e6f89be6SPavel Begunkov for (i = 0; i < nr_buckets; i++) {
764e6f89be6SPavel Begunkov struct io_hash_bucket *hb = &table->hbs[i];
765329061d3SJens Axboe
76638513c46SHao Xu spin_lock(&hb->lock);
76738513c46SHao Xu hlist_for_each_entry_safe(req, tmp, &hb->list, hash_node) {
768329061d3SJens Axboe if (io_match_task_safe(req, tsk, cancel_all)) {
769329061d3SJens Axboe hlist_del_init(&req->hash_node);
770329061d3SJens Axboe io_poll_cancel_req(req);
771329061d3SJens Axboe found = true;
772329061d3SJens Axboe }
773329061d3SJens Axboe }
77438513c46SHao Xu spin_unlock(&hb->lock);
775329061d3SJens Axboe }
776329061d3SJens Axboe return found;
777329061d3SJens Axboe }
778329061d3SJens Axboe
7799ca9fb24SPavel Begunkov /*
7809ca9fb24SPavel Begunkov * Returns true if we found and killed one or more poll requests
7819ca9fb24SPavel Begunkov */
io_poll_remove_all(struct io_ring_ctx * ctx,struct task_struct * tsk,bool cancel_all)7829ca9fb24SPavel Begunkov __cold bool io_poll_remove_all(struct io_ring_ctx *ctx, struct task_struct *tsk,
7839ca9fb24SPavel Begunkov bool cancel_all)
7849ca9fb24SPavel Begunkov __must_hold(&ctx->uring_lock)
7859ca9fb24SPavel Begunkov {
786b321823aSPavel Begunkov bool ret;
787b321823aSPavel Begunkov
788b321823aSPavel Begunkov ret = io_poll_remove_all_table(tsk, &ctx->cancel_table, cancel_all);
789b321823aSPavel Begunkov ret |= io_poll_remove_all_table(tsk, &ctx->cancel_table_locked, cancel_all);
790b321823aSPavel Begunkov return ret;
7919ca9fb24SPavel Begunkov }
7929ca9fb24SPavel 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)793329061d3SJens Axboe static struct io_kiocb *io_poll_find(struct io_ring_ctx *ctx, bool poll_only,
7941ab1edb0SPavel Begunkov struct io_cancel_data *cd,
795e6f89be6SPavel Begunkov struct io_hash_table *table,
7961ab1edb0SPavel Begunkov struct io_hash_bucket **out_bucket)
797329061d3SJens Axboe {
798329061d3SJens Axboe struct io_kiocb *req;
799e6f89be6SPavel Begunkov u32 index = hash_long(cd->data, table->hash_bits);
800e6f89be6SPavel Begunkov struct io_hash_bucket *hb = &table->hbs[index];
801329061d3SJens Axboe
8021ab1edb0SPavel Begunkov *out_bucket = NULL;
8031ab1edb0SPavel Begunkov
80438513c46SHao Xu spin_lock(&hb->lock);
80538513c46SHao Xu hlist_for_each_entry(req, &hb->list, hash_node) {
806329061d3SJens Axboe if (cd->data != req->cqe.user_data)
807329061d3SJens Axboe continue;
808329061d3SJens Axboe if (poll_only && req->opcode != IORING_OP_POLL_ADD)
809329061d3SJens Axboe continue;
810329061d3SJens Axboe if (cd->flags & IORING_ASYNC_CANCEL_ALL) {
811329061d3SJens Axboe if (cd->seq == req->work.cancel_seq)
812329061d3SJens Axboe continue;
813329061d3SJens Axboe req->work.cancel_seq = cd->seq;
814329061d3SJens Axboe }
8151ab1edb0SPavel Begunkov *out_bucket = hb;
816329061d3SJens Axboe return req;
817329061d3SJens Axboe }
81838513c46SHao Xu spin_unlock(&hb->lock);
819329061d3SJens Axboe return NULL;
820329061d3SJens Axboe }
821329061d3SJens 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)822329061d3SJens Axboe static struct io_kiocb *io_poll_file_find(struct io_ring_ctx *ctx,
8231ab1edb0SPavel Begunkov struct io_cancel_data *cd,
824e6f89be6SPavel Begunkov struct io_hash_table *table,
8251ab1edb0SPavel Begunkov struct io_hash_bucket **out_bucket)
826329061d3SJens Axboe {
827e6f89be6SPavel Begunkov unsigned nr_buckets = 1U << table->hash_bits;
828329061d3SJens Axboe struct io_kiocb *req;
829329061d3SJens Axboe int i;
830329061d3SJens Axboe
8311ab1edb0SPavel Begunkov *out_bucket = NULL;
8321ab1edb0SPavel Begunkov
833e6f89be6SPavel Begunkov for (i = 0; i < nr_buckets; i++) {
834e6f89be6SPavel Begunkov struct io_hash_bucket *hb = &table->hbs[i];
835329061d3SJens Axboe
83638513c46SHao Xu spin_lock(&hb->lock);
83738513c46SHao Xu hlist_for_each_entry(req, &hb->list, hash_node) {
838a30badf6SJens Axboe if (io_cancel_req_match(req, cd)) {
8391ab1edb0SPavel Begunkov *out_bucket = hb;
840329061d3SJens Axboe return req;
841329061d3SJens Axboe }
842a30badf6SJens Axboe }
84338513c46SHao Xu spin_unlock(&hb->lock);
844329061d3SJens Axboe }
845329061d3SJens Axboe return NULL;
846329061d3SJens Axboe }
847329061d3SJens Axboe
io_poll_disarm(struct io_kiocb * req)8489ca9fb24SPavel Begunkov static int io_poll_disarm(struct io_kiocb *req)
849329061d3SJens Axboe {
8509ca9fb24SPavel Begunkov if (!req)
8519ca9fb24SPavel Begunkov return -ENOENT;
852329061d3SJens Axboe if (!io_poll_get_ownership(req))
8539ca9fb24SPavel Begunkov return -EALREADY;
854329061d3SJens Axboe io_poll_remove_entries(req);
855329061d3SJens Axboe hash_del(&req->hash_node);
8569ca9fb24SPavel Begunkov return 0;
857329061d3SJens Axboe }
858329061d3SJens Axboe
__io_poll_cancel(struct io_ring_ctx * ctx,struct io_cancel_data * cd,struct io_hash_table * table)859a2cdd519SPavel Begunkov static int __io_poll_cancel(struct io_ring_ctx *ctx, struct io_cancel_data *cd,
860e6f89be6SPavel Begunkov struct io_hash_table *table)
861329061d3SJens Axboe {
8621ab1edb0SPavel Begunkov struct io_hash_bucket *bucket;
863329061d3SJens Axboe struct io_kiocb *req;
864329061d3SJens Axboe
865d7b8b079SJens Axboe if (cd->flags & (IORING_ASYNC_CANCEL_FD | IORING_ASYNC_CANCEL_OP |
866d7b8b079SJens Axboe IORING_ASYNC_CANCEL_ANY))
867e6f89be6SPavel Begunkov req = io_poll_file_find(ctx, cd, table, &bucket);
868329061d3SJens Axboe else
869e6f89be6SPavel Begunkov req = io_poll_find(ctx, false, cd, table, &bucket);
8701ab1edb0SPavel Begunkov
8711ab1edb0SPavel Begunkov if (req)
872329061d3SJens Axboe io_poll_cancel_req(req);
8731ab1edb0SPavel Begunkov if (bucket)
8741ab1edb0SPavel Begunkov spin_unlock(&bucket->lock);
8751ab1edb0SPavel Begunkov return req ? 0 : -ENOENT;
876329061d3SJens Axboe }
877329061d3SJens Axboe
io_poll_cancel(struct io_ring_ctx * ctx,struct io_cancel_data * cd,unsigned issue_flags)8785d7943d9SPavel Begunkov int io_poll_cancel(struct io_ring_ctx *ctx, struct io_cancel_data *cd,
8795d7943d9SPavel Begunkov unsigned issue_flags)
880a2cdd519SPavel Begunkov {
8819ca9fb24SPavel Begunkov int ret;
8829ca9fb24SPavel Begunkov
8839ca9fb24SPavel Begunkov ret = __io_poll_cancel(ctx, cd, &ctx->cancel_table);
8849ca9fb24SPavel Begunkov if (ret != -ENOENT)
8859ca9fb24SPavel Begunkov return ret;
8869ca9fb24SPavel Begunkov
8879ca9fb24SPavel Begunkov io_ring_submit_lock(ctx, issue_flags);
8889ca9fb24SPavel Begunkov ret = __io_poll_cancel(ctx, cd, &ctx->cancel_table_locked);
8899ca9fb24SPavel Begunkov io_ring_submit_unlock(ctx, issue_flags);
8909ca9fb24SPavel Begunkov return ret;
891a2cdd519SPavel Begunkov }
892a2cdd519SPavel Begunkov
io_poll_parse_events(const struct io_uring_sqe * sqe,unsigned int flags)893329061d3SJens Axboe static __poll_t io_poll_parse_events(const struct io_uring_sqe *sqe,
894329061d3SJens Axboe unsigned int flags)
895329061d3SJens Axboe {
896329061d3SJens Axboe u32 events;
897329061d3SJens Axboe
898329061d3SJens Axboe events = READ_ONCE(sqe->poll32_events);
899329061d3SJens Axboe #ifdef __BIG_ENDIAN
900329061d3SJens Axboe events = swahw32(events);
901329061d3SJens Axboe #endif
902329061d3SJens Axboe if (!(flags & IORING_POLL_ADD_MULTI))
903329061d3SJens Axboe events |= EPOLLONESHOT;
904b9ba8a44SJens Axboe if (!(flags & IORING_POLL_ADD_LEVEL))
905b9ba8a44SJens Axboe events |= EPOLLET;
906b9ba8a44SJens Axboe return demangle_poll(events) |
907b9ba8a44SJens Axboe (events & (EPOLLEXCLUSIVE|EPOLLONESHOT|EPOLLET));
908329061d3SJens Axboe }
909329061d3SJens Axboe
io_poll_remove_prep(struct io_kiocb * req,const struct io_uring_sqe * sqe)910329061d3SJens Axboe int io_poll_remove_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
911329061d3SJens Axboe {
912f2ccb5aeSStefan Metzmacher struct io_poll_update *upd = io_kiocb_to_cmd(req, struct io_poll_update);
913329061d3SJens Axboe u32 flags;
914329061d3SJens Axboe
915329061d3SJens Axboe if (sqe->buf_index || sqe->splice_fd_in)
916329061d3SJens Axboe return -EINVAL;
917329061d3SJens Axboe flags = READ_ONCE(sqe->len);
918329061d3SJens Axboe if (flags & ~(IORING_POLL_UPDATE_EVENTS | IORING_POLL_UPDATE_USER_DATA |
919329061d3SJens Axboe IORING_POLL_ADD_MULTI))
920329061d3SJens Axboe return -EINVAL;
921329061d3SJens Axboe /* meaningless without update */
922329061d3SJens Axboe if (flags == IORING_POLL_ADD_MULTI)
923329061d3SJens Axboe return -EINVAL;
924329061d3SJens Axboe
925329061d3SJens Axboe upd->old_user_data = READ_ONCE(sqe->addr);
926329061d3SJens Axboe upd->update_events = flags & IORING_POLL_UPDATE_EVENTS;
927329061d3SJens Axboe upd->update_user_data = flags & IORING_POLL_UPDATE_USER_DATA;
928329061d3SJens Axboe
929329061d3SJens Axboe upd->new_user_data = READ_ONCE(sqe->off);
930329061d3SJens Axboe if (!upd->update_user_data && upd->new_user_data)
931329061d3SJens Axboe return -EINVAL;
932329061d3SJens Axboe if (upd->update_events)
933329061d3SJens Axboe upd->events = io_poll_parse_events(sqe, flags);
934329061d3SJens Axboe else if (sqe->poll32_events)
935329061d3SJens Axboe return -EINVAL;
936329061d3SJens Axboe
937329061d3SJens Axboe return 0;
938329061d3SJens Axboe }
939329061d3SJens Axboe
io_poll_add_prep(struct io_kiocb * req,const struct io_uring_sqe * sqe)940329061d3SJens Axboe int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
941329061d3SJens Axboe {
942f2ccb5aeSStefan Metzmacher struct io_poll *poll = io_kiocb_to_cmd(req, struct io_poll);
943329061d3SJens Axboe u32 flags;
944329061d3SJens Axboe
945329061d3SJens Axboe if (sqe->buf_index || sqe->off || sqe->addr)
946329061d3SJens Axboe return -EINVAL;
947329061d3SJens Axboe flags = READ_ONCE(sqe->len);
948d59bd748SJens Axboe if (flags & ~IORING_POLL_ADD_MULTI)
949329061d3SJens Axboe return -EINVAL;
950329061d3SJens Axboe if ((flags & IORING_POLL_ADD_MULTI) && (req->flags & REQ_F_CQE_SKIP))
951329061d3SJens Axboe return -EINVAL;
952329061d3SJens Axboe
953329061d3SJens Axboe poll->events = io_poll_parse_events(sqe, flags);
954329061d3SJens Axboe return 0;
955329061d3SJens Axboe }
956329061d3SJens Axboe
io_poll_add(struct io_kiocb * req,unsigned int issue_flags)957329061d3SJens Axboe int io_poll_add(struct io_kiocb *req, unsigned int issue_flags)
958329061d3SJens Axboe {
959f2ccb5aeSStefan Metzmacher struct io_poll *poll = io_kiocb_to_cmd(req, struct io_poll);
960329061d3SJens Axboe struct io_poll_table ipt;
961329061d3SJens Axboe int ret;
962329061d3SJens Axboe
963329061d3SJens Axboe ipt.pt._qproc = io_poll_queue_proc;
964329061d3SJens Axboe
9659ca9fb24SPavel Begunkov /*
9669ca9fb24SPavel Begunkov * If sqpoll or single issuer, there is no contention for ->uring_lock
9679ca9fb24SPavel Begunkov * and we'll end up holding it in tw handlers anyway.
9689ca9fb24SPavel Begunkov */
969e8375e43SPavel Begunkov if (req->ctx->flags & (IORING_SETUP_SQPOLL|IORING_SETUP_SINGLE_ISSUER))
9709ca9fb24SPavel Begunkov req->flags |= REQ_F_HASH_LOCKED;
9719ca9fb24SPavel Begunkov
97249f1c68eSPavel Begunkov ret = __io_arm_poll_handler(req, poll, &ipt, poll->events, issue_flags);
973de08356fSPavel Begunkov if (ret > 0) {
974063a0079SPavel Begunkov io_req_set_res(req, ipt.result_mask, 0);
975329061d3SJens Axboe return IOU_OK;
976329061d3SJens Axboe }
977de08356fSPavel Begunkov return ret ?: IOU_ISSUE_SKIP_COMPLETE;
978329061d3SJens Axboe }
979329061d3SJens Axboe
io_poll_remove(struct io_kiocb * req,unsigned int issue_flags)980329061d3SJens Axboe int io_poll_remove(struct io_kiocb *req, unsigned int issue_flags)
981329061d3SJens Axboe {
982f2ccb5aeSStefan Metzmacher struct io_poll_update *poll_update = io_kiocb_to_cmd(req, struct io_poll_update);
983329061d3SJens Axboe struct io_ring_ctx *ctx = req->ctx;
984ad711c5dSJens Axboe struct io_cancel_data cd = { .ctx = ctx, .data = poll_update->old_user_data, };
9851ab1edb0SPavel Begunkov struct io_hash_bucket *bucket;
986329061d3SJens Axboe struct io_kiocb *preq;
987329061d3SJens Axboe int ret2, ret = 0;
988329061d3SJens Axboe
989ef7dfac5SJens Axboe io_ring_submit_lock(ctx, issue_flags);
990e6f89be6SPavel Begunkov preq = io_poll_find(ctx, true, &cd, &ctx->cancel_table, &bucket);
9911ab1edb0SPavel Begunkov ret2 = io_poll_disarm(preq);
9921ab1edb0SPavel Begunkov if (bucket)
9931ab1edb0SPavel Begunkov spin_unlock(&bucket->lock);
9949ca9fb24SPavel Begunkov if (!ret2)
9959ca9fb24SPavel Begunkov goto found;
9969ca9fb24SPavel Begunkov if (ret2 != -ENOENT) {
9979ca9fb24SPavel Begunkov ret = ret2;
99838513c46SHao Xu goto out;
99938513c46SHao Xu }
1000329061d3SJens Axboe
10019ca9fb24SPavel Begunkov preq = io_poll_find(ctx, true, &cd, &ctx->cancel_table_locked, &bucket);
10029ca9fb24SPavel Begunkov ret2 = io_poll_disarm(preq);
10039ca9fb24SPavel Begunkov if (bucket)
10049ca9fb24SPavel Begunkov spin_unlock(&bucket->lock);
10059ca9fb24SPavel Begunkov if (ret2) {
10069ca9fb24SPavel Begunkov ret = ret2;
10079ca9fb24SPavel Begunkov goto out;
10089ca9fb24SPavel Begunkov }
10099ca9fb24SPavel Begunkov
10109ca9fb24SPavel Begunkov found:
1011bce5d70cSPavel Begunkov if (WARN_ON_ONCE(preq->opcode != IORING_OP_POLL_ADD)) {
1012bce5d70cSPavel Begunkov ret = -EFAULT;
1013bce5d70cSPavel Begunkov goto out;
1014bce5d70cSPavel Begunkov }
1015bce5d70cSPavel Begunkov
1016329061d3SJens Axboe if (poll_update->update_events || poll_update->update_user_data) {
1017329061d3SJens Axboe /* only mask one event flags, keep behavior flags */
1018329061d3SJens Axboe if (poll_update->update_events) {
1019f2ccb5aeSStefan Metzmacher struct io_poll *poll = io_kiocb_to_cmd(preq, struct io_poll);
1020329061d3SJens Axboe
1021329061d3SJens Axboe poll->events &= ~0xffff;
1022329061d3SJens Axboe poll->events |= poll_update->events & 0xffff;
1023329061d3SJens Axboe poll->events |= IO_POLL_UNMASK;
1024329061d3SJens Axboe }
1025329061d3SJens Axboe if (poll_update->update_user_data)
1026329061d3SJens Axboe preq->cqe.user_data = poll_update->new_user_data;
1027329061d3SJens Axboe
1028ef7dfac5SJens Axboe ret2 = io_poll_add(preq, issue_flags & ~IO_URING_F_UNLOCKED);
1029329061d3SJens Axboe /* successfully updated, don't complete poll request */
1030329061d3SJens Axboe if (!ret2 || ret2 == -EIOCBQUEUED)
1031329061d3SJens Axboe goto out;
1032329061d3SJens Axboe }
1033329061d3SJens Axboe
1034329061d3SJens Axboe req_set_fail(preq);
1035329061d3SJens Axboe io_req_set_res(preq, -ECANCELED, 0);
10364cbc5e93SPavel Begunkov preq->io_task_work.func = io_req_task_complete;
10374cbc5e93SPavel Begunkov io_req_task_work_add(preq);
1038329061d3SJens Axboe out:
1039ef7dfac5SJens Axboe io_ring_submit_unlock(ctx, issue_flags);
1040329061d3SJens Axboe if (ret < 0) {
1041329061d3SJens Axboe req_set_fail(req);
1042329061d3SJens Axboe return ret;
1043329061d3SJens Axboe }
1044329061d3SJens Axboe /* complete update request, we're done with it */
1045329061d3SJens Axboe io_req_set_res(req, ret, 0);
1046329061d3SJens Axboe return IOU_OK;
1047329061d3SJens Axboe }
10489da7471eSJens Axboe
io_apoll_cache_free(struct io_cache_entry * entry)10499b797a37SJens Axboe void io_apoll_cache_free(struct io_cache_entry *entry)
10509da7471eSJens Axboe {
10519b797a37SJens Axboe kfree(container_of(entry, struct async_poll, cache));
10529da7471eSJens Axboe }
1053