io_uring: allow defer completion for aux posted cqes
[linux-block.git] / io_uring / poll.c
CommitLineData
329061d3
JA
1// SPDX-License-Identifier: GPL-2.0
2#include <linux/kernel.h>
3#include <linux/errno.h>
4#include <linux/fs.h>
5#include <linux/file.h>
6#include <linux/mm.h>
7#include <linux/slab.h>
8#include <linux/poll.h>
9#include <linux/hashtable.h>
10#include <linux/io_uring.h>
11
12#include <trace/events/io_uring.h>
13
14#include <uapi/linux/io_uring.h>
15
329061d3
JA
16#include "io_uring.h"
17#include "refs.h"
18#include "opdef.h"
3b77495a 19#include "kbuf.h"
329061d3 20#include "poll.h"
38513c46 21#include "cancel.h"
329061d3
JA
22
23struct io_poll_update {
24 struct file *file;
25 u64 old_user_data;
26 u64 new_user_data;
27 __poll_t events;
28 bool update_events;
29 bool update_user_data;
30};
31
32struct io_poll_table {
33 struct poll_table_struct pt;
34 struct io_kiocb *req;
35 int nr_entries;
36 int error;
49f1c68e 37 bool owning;
063a0079
PB
38 /* output value, set only if arm poll returns >0 */
39 __poll_t result_mask;
329061d3
JA
40};
41
42#define IO_POLL_CANCEL_FLAG BIT(31)
43#define IO_POLL_REF_MASK GENMASK(30, 0)
44
0638cd7b
PB
45#define IO_WQE_F_DOUBLE 1
46
47static inline struct io_kiocb *wqe_to_req(struct wait_queue_entry *wqe)
48{
49 unsigned long priv = (unsigned long)wqe->private;
50
51 return (struct io_kiocb *)(priv & ~IO_WQE_F_DOUBLE);
52}
53
54static inline bool wqe_is_double(struct wait_queue_entry *wqe)
55{
56 unsigned long priv = (unsigned long)wqe->private;
57
58 return priv & IO_WQE_F_DOUBLE;
59}
60
329061d3
JA
61/*
62 * If refs part of ->poll_refs (see IO_POLL_REF_MASK) is 0, it's free. We can
63 * bump it and acquire ownership. It's disallowed to modify requests while not
64 * owning it, that prevents from races for enqueueing task_work's and b/w
65 * arming poll and wakeups.
66 */
67static inline bool io_poll_get_ownership(struct io_kiocb *req)
68{
69 return !(atomic_fetch_inc(&req->poll_refs) & IO_POLL_REF_MASK);
70}
71
72static void io_poll_mark_cancelled(struct io_kiocb *req)
73{
74 atomic_or(IO_POLL_CANCEL_FLAG, &req->poll_refs);
75}
76
77static struct io_poll *io_poll_get_double(struct io_kiocb *req)
78{
79 /* pure poll stashes this in ->async_data, poll driven retry elsewhere */
80 if (req->opcode == IORING_OP_POLL_ADD)
81 return req->async_data;
82 return req->apoll->double_poll;
83}
84
85static struct io_poll *io_poll_get_single(struct io_kiocb *req)
86{
87 if (req->opcode == IORING_OP_POLL_ADD)
f2ccb5ae 88 return io_kiocb_to_cmd(req, struct io_poll);
329061d3
JA
89 return &req->apoll->poll;
90}
91
92static void io_poll_req_insert(struct io_kiocb *req)
93{
e6f89be6
PB
94 struct io_hash_table *table = &req->ctx->cancel_table;
95 u32 index = hash_long(req->cqe.user_data, table->hash_bits);
96 struct io_hash_bucket *hb = &table->hbs[index];
329061d3 97
38513c46
HX
98 spin_lock(&hb->lock);
99 hlist_add_head(&req->hash_node, &hb->list);
100 spin_unlock(&hb->lock);
101}
102
103static void io_poll_req_delete(struct io_kiocb *req, struct io_ring_ctx *ctx)
104{
e6f89be6
PB
105 struct io_hash_table *table = &req->ctx->cancel_table;
106 u32 index = hash_long(req->cqe.user_data, table->hash_bits);
107 spinlock_t *lock = &table->hbs[index].lock;
38513c46
HX
108
109 spin_lock(lock);
110 hash_del(&req->hash_node);
111 spin_unlock(lock);
329061d3
JA
112}
113
9ca9fb24
PB
114static void io_poll_req_insert_locked(struct io_kiocb *req)
115{
116 struct io_hash_table *table = &req->ctx->cancel_table_locked;
117 u32 index = hash_long(req->cqe.user_data, table->hash_bits);
118
5576035f
PB
119 lockdep_assert_held(&req->ctx->uring_lock);
120
9ca9fb24
PB
121 hlist_add_head(&req->hash_node, &table->hbs[index].list);
122}
123
124static void io_poll_tw_hash_eject(struct io_kiocb *req, bool *locked)
125{
126 struct io_ring_ctx *ctx = req->ctx;
127
128 if (req->flags & REQ_F_HASH_LOCKED) {
129 /*
130 * ->cancel_table_locked is protected by ->uring_lock in
131 * contrast to per bucket spinlocks. Likely, tctx_task_work()
132 * already grabbed the mutex for us, but there is a chance it
133 * failed.
134 */
135 io_tw_lock(ctx, locked);
136 hash_del(&req->hash_node);
b21a51e2 137 req->flags &= ~REQ_F_HASH_LOCKED;
9ca9fb24
PB
138 } else {
139 io_poll_req_delete(req, ctx);
140 }
141}
142
329061d3
JA
143static void io_init_poll_iocb(struct io_poll *poll, __poll_t events,
144 wait_queue_func_t wake_func)
145{
146 poll->head = NULL;
147#define IO_POLL_UNMASK (EPOLLERR|EPOLLHUP|EPOLLNVAL|EPOLLRDHUP)
148 /* mask in events that we always want/need */
149 poll->events = events | IO_POLL_UNMASK;
150 INIT_LIST_HEAD(&poll->wait.entry);
151 init_waitqueue_func_entry(&poll->wait, wake_func);
152}
153
154static inline void io_poll_remove_entry(struct io_poll *poll)
155{
156 struct wait_queue_head *head = smp_load_acquire(&poll->head);
157
158 if (head) {
159 spin_lock_irq(&head->lock);
160 list_del_init(&poll->wait.entry);
161 poll->head = NULL;
162 spin_unlock_irq(&head->lock);
163 }
164}
165
166static void io_poll_remove_entries(struct io_kiocb *req)
167{
168 /*
169 * Nothing to do if neither of those flags are set. Avoid dipping
170 * into the poll/apoll/double cachelines if we can.
171 */
172 if (!(req->flags & (REQ_F_SINGLE_POLL | REQ_F_DOUBLE_POLL)))
173 return;
174
175 /*
176 * While we hold the waitqueue lock and the waitqueue is nonempty,
177 * wake_up_pollfree() will wait for us. However, taking the waitqueue
178 * lock in the first place can race with the waitqueue being freed.
179 *
180 * We solve this as eventpoll does: by taking advantage of the fact that
181 * all users of wake_up_pollfree() will RCU-delay the actual free. If
182 * we enter rcu_read_lock() and see that the pointer to the queue is
183 * non-NULL, we can then lock it without the memory being freed out from
184 * under us.
185 *
186 * Keep holding rcu_read_lock() as long as we hold the queue lock, in
187 * case the caller deletes the entry from the queue, leaving it empty.
188 * In that case, only RCU prevents the queue memory from being freed.
189 */
190 rcu_read_lock();
191 if (req->flags & REQ_F_SINGLE_POLL)
192 io_poll_remove_entry(io_poll_get_single(req));
193 if (req->flags & REQ_F_DOUBLE_POLL)
194 io_poll_remove_entry(io_poll_get_double(req));
195 rcu_read_unlock();
196}
197
2ba69707
DY
198enum {
199 IOU_POLL_DONE = 0,
200 IOU_POLL_NO_ACTION = 1,
114eccdf 201 IOU_POLL_REMOVE_POLL_USE_RES = 2,
2ba69707
DY
202};
203
329061d3
JA
204/*
205 * All poll tw should go through this. Checks for poll events, manages
206 * references, does rewait, etc.
207 *
2ba69707
DY
208 * Returns a negative error on failure. IOU_POLL_NO_ACTION when no action require,
209 * which is either spurious wakeup or multishot CQE is served.
210 * IOU_POLL_DONE when it's done with the request, then the mask is stored in req->cqe.res.
114eccdf
DY
211 * IOU_POLL_REMOVE_POLL_USE_RES indicates to remove multishot poll and that the result
212 * is stored in req->cqe.
329061d3
JA
213 */
214static int io_poll_check_events(struct io_kiocb *req, bool *locked)
215{
216 struct io_ring_ctx *ctx = req->ctx;
217 int v, ret;
218
219 /* req->task == current here, checking PF_EXITING is safe */
220 if (unlikely(req->task->flags & PF_EXITING))
221 return -ECANCELED;
222
223 do {
224 v = atomic_read(&req->poll_refs);
225
226 /* tw handler should be the owner, and so have some references */
227 if (WARN_ON_ONCE(!(v & IO_POLL_REF_MASK)))
2ba69707 228 return IOU_POLL_DONE;
329061d3
JA
229 if (v & IO_POLL_CANCEL_FLAG)
230 return -ECANCELED;
539bcb57
PB
231 /*
232 * cqe.res contains only events of the first wake up
233 * and all others are be lost. Redo vfs_poll() to get
234 * up to date state.
235 */
236 if ((v & IO_POLL_REF_MASK) != 1)
237 req->cqe.res = 0;
329061d3 238
2ba69707 239 /* the mask was stashed in __io_poll_execute */
329061d3
JA
240 if (!req->cqe.res) {
241 struct poll_table_struct pt = { ._key = req->apoll_events };
242 req->cqe.res = vfs_poll(req->file, &pt) & req->apoll_events;
243 }
244
245 if ((unlikely(!req->cqe.res)))
246 continue;
247 if (req->apoll_events & EPOLLONESHOT)
2ba69707 248 return IOU_POLL_DONE;
329061d3
JA
249
250 /* multishot, just fill a CQE and proceed */
251 if (!(req->flags & REQ_F_APOLL_MULTISHOT)) {
252 __poll_t mask = mangle_poll(req->cqe.res &
253 req->apoll_events);
329061d3 254
d245bca6 255 if (!io_post_aux_cqe(ctx, req->cqe.user_data,
a2da6763
DY
256 mask, IORING_CQE_F_MORE, false)) {
257 io_req_set_res(req, mask, 0);
258 return IOU_POLL_REMOVE_POLL_USE_RES;
259 }
d245bca6
PB
260 } else {
261 ret = io_poll_issue(req, locked);
114eccdf
DY
262 if (ret == IOU_STOP_MULTISHOT)
263 return IOU_POLL_REMOVE_POLL_USE_RES;
2ba69707 264 if (ret < 0)
d245bca6
PB
265 return ret;
266 }
329061d3 267
b98186ae
PB
268 /* force the next iteration to vfs_poll() */
269 req->cqe.res = 0;
270
329061d3
JA
271 /*
272 * Release all references, retry if someone tried to restart
273 * task_work while we were executing it.
274 */
275 } while (atomic_sub_return(v & IO_POLL_REF_MASK, &req->poll_refs));
276
2ba69707 277 return IOU_POLL_NO_ACTION;
329061d3
JA
278}
279
280static void io_poll_task_func(struct io_kiocb *req, bool *locked)
281{
329061d3
JA
282 int ret;
283
284 ret = io_poll_check_events(req, locked);
2ba69707 285 if (ret == IOU_POLL_NO_ACTION)
329061d3
JA
286 return;
287
2ba69707 288 if (ret == IOU_POLL_DONE) {
f2ccb5ae 289 struct io_poll *poll = io_kiocb_to_cmd(req, struct io_poll);
329061d3 290 req->cqe.res = mangle_poll(req->cqe.res & poll->events);
114eccdf 291 } else if (ret != IOU_POLL_REMOVE_POLL_USE_RES) {
329061d3
JA
292 req->cqe.res = ret;
293 req_set_fail(req);
294 }
295
296 io_poll_remove_entries(req);
9ca9fb24
PB
297 io_poll_tw_hash_eject(req, locked);
298
0ec6dca2
PB
299 io_req_set_res(req, req->cqe.res, 0);
300 io_req_task_complete(req, locked);
329061d3
JA
301}
302
303static void io_apoll_task_func(struct io_kiocb *req, bool *locked)
304{
329061d3
JA
305 int ret;
306
307 ret = io_poll_check_events(req, locked);
2ba69707 308 if (ret == IOU_POLL_NO_ACTION)
329061d3
JA
309 return;
310
c06c6c5d 311 io_tw_lock(req->ctx, locked);
329061d3 312 io_poll_remove_entries(req);
9ca9fb24 313 io_poll_tw_hash_eject(req, locked);
329061d3 314
114eccdf 315 if (ret == IOU_POLL_REMOVE_POLL_USE_RES)
c06c6c5d 316 io_req_task_complete(req, locked);
114eccdf 317 else if (ret == IOU_POLL_DONE)
329061d3
JA
318 io_req_task_submit(req, locked);
319 else
973fc83f 320 io_req_defer_failed(req, ret);
329061d3
JA
321}
322
13a99017 323static void __io_poll_execute(struct io_kiocb *req, int mask)
329061d3
JA
324{
325 io_req_set_res(req, mask, 0);
cd42a53d 326
329061d3
JA
327 if (req->opcode == IORING_OP_POLL_ADD)
328 req->io_task_work.func = io_poll_task_func;
329 else
330 req->io_task_work.func = io_apoll_task_func;
331
48863ffd 332 trace_io_uring_task_add(req, mask);
329061d3
JA
333 io_req_task_work_add(req);
334}
335
13a99017 336static inline void io_poll_execute(struct io_kiocb *req, int res)
329061d3
JA
337{
338 if (io_poll_get_ownership(req))
13a99017 339 __io_poll_execute(req, res);
329061d3
JA
340}
341
342static void io_poll_cancel_req(struct io_kiocb *req)
343{
344 io_poll_mark_cancelled(req);
345 /* kick tw, which should complete the request */
13a99017 346 io_poll_execute(req, 0);
329061d3
JA
347}
348
329061d3
JA
349#define IO_ASYNC_POLL_COMMON (EPOLLONESHOT | EPOLLPRI)
350
fe991a76
JA
351static __cold int io_pollfree_wake(struct io_kiocb *req, struct io_poll *poll)
352{
353 io_poll_mark_cancelled(req);
354 /* we have to kick tw in case it's not already */
355 io_poll_execute(req, 0);
356
357 /*
358 * If the waitqueue is being freed early but someone is already
359 * holds ownership over it, we have to tear down the request as
360 * best we can. That means immediately removing the request from
361 * its waitqueue and preventing all further accesses to the
362 * waitqueue via the request.
363 */
364 list_del_init(&poll->wait.entry);
365
366 /*
367 * Careful: this *must* be the last step, since as soon
368 * as req->head is NULL'ed out, the request can be
369 * completed and freed, since aio_poll_complete_work()
370 * will no longer need to take the waitqueue lock.
371 */
372 smp_store_release(&poll->head, NULL);
373 return 1;
374}
375
329061d3
JA
376static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
377 void *key)
378{
379 struct io_kiocb *req = wqe_to_req(wait);
380 struct io_poll *poll = container_of(wait, struct io_poll, wait);
381 __poll_t mask = key_to_poll(key);
382
fe991a76
JA
383 if (unlikely(mask & POLLFREE))
384 return io_pollfree_wake(req, poll);
329061d3
JA
385
386 /* for instances that support it check for an event match first */
387 if (mask && !(mask & (poll->events & ~IO_ASYNC_POLL_COMMON)))
388 return 0;
389
390 if (io_poll_get_ownership(req)) {
44648532
JA
391 /*
392 * If we trigger a multishot poll off our own wakeup path,
393 * disable multishot as there is a circular dependency between
394 * CQ posting and triggering the event.
395 */
396 if (mask & EPOLL_URING_WAKE)
397 poll->events |= EPOLLONESHOT;
398
329061d3
JA
399 /* optional, saves extra locking for removal in tw handler */
400 if (mask && poll->events & EPOLLONESHOT) {
401 list_del_init(&poll->wait.entry);
402 poll->head = NULL;
403 if (wqe_is_double(wait))
404 req->flags &= ~REQ_F_DOUBLE_POLL;
405 else
406 req->flags &= ~REQ_F_SINGLE_POLL;
407 }
13a99017 408 __io_poll_execute(req, mask);
329061d3
JA
409 }
410 return 1;
411}
412
30a33669
PB
413/* fails only when polling is already completing by the first entry */
414static bool io_poll_double_prepare(struct io_kiocb *req)
49f1c68e
PB
415{
416 struct wait_queue_head *head;
417 struct io_poll *poll = io_poll_get_single(req);
418
419 /* head is RCU protected, see io_poll_remove_entries() comments */
420 rcu_read_lock();
421 head = smp_load_acquire(&poll->head);
7a121ced 422 /*
30a33669
PB
423 * poll arm might not hold ownership and so race for req->flags with
424 * io_poll_wake(). There is only one poll entry queued, serialise with
425 * it by taking its head lock. As we're still arming the tw hanlder
426 * is not going to be run, so there are no races with it.
7a121ced 427 */
30a33669 428 if (head) {
49f1c68e 429 spin_lock_irq(&head->lock);
30a33669
PB
430 req->flags |= REQ_F_DOUBLE_POLL;
431 if (req->opcode == IORING_OP_POLL_ADD)
432 req->flags |= REQ_F_ASYNC_DATA;
49f1c68e 433 spin_unlock_irq(&head->lock);
30a33669 434 }
49f1c68e 435 rcu_read_unlock();
30a33669 436 return !!head;
49f1c68e
PB
437}
438
329061d3
JA
439static void __io_queue_proc(struct io_poll *poll, struct io_poll_table *pt,
440 struct wait_queue_head *head,
441 struct io_poll **poll_ptr)
442{
443 struct io_kiocb *req = pt->req;
444 unsigned long wqe_private = (unsigned long) req;
445
446 /*
447 * The file being polled uses multiple waitqueues for poll handling
448 * (e.g. one for read, one for write). Setup a separate io_poll
449 * if this happens.
450 */
451 if (unlikely(pt->nr_entries)) {
452 struct io_poll *first = poll;
453
454 /* double add on the same waitqueue head, ignore */
455 if (first->head == head)
456 return;
457 /* already have a 2nd entry, fail a third attempt */
458 if (*poll_ptr) {
459 if ((*poll_ptr)->head == head)
460 return;
461 pt->error = -EINVAL;
462 return;
463 }
464
465 poll = kmalloc(sizeof(*poll), GFP_ATOMIC);
466 if (!poll) {
467 pt->error = -ENOMEM;
468 return;
469 }
49f1c68e 470
329061d3 471 /* mark as double wq entry */
0638cd7b 472 wqe_private |= IO_WQE_F_DOUBLE;
329061d3 473 io_init_poll_iocb(poll, first->events, first->wait.func);
30a33669
PB
474 if (!io_poll_double_prepare(req)) {
475 /* the request is completing, just back off */
476 kfree(poll);
477 return;
478 }
329061d3 479 *poll_ptr = poll;
49f1c68e
PB
480 } else {
481 /* fine to modify, there is no poll queued to race with us */
482 req->flags |= REQ_F_SINGLE_POLL;
329061d3
JA
483 }
484
329061d3
JA
485 pt->nr_entries++;
486 poll->head = head;
487 poll->wait.private = (void *) wqe_private;
488
489 if (poll->events & EPOLLEXCLUSIVE)
490 add_wait_queue_exclusive(head, &poll->wait);
491 else
492 add_wait_queue(head, &poll->wait);
493}
494
495static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
496 struct poll_table_struct *p)
497{
498 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
f2ccb5ae 499 struct io_poll *poll = io_kiocb_to_cmd(pt->req, struct io_poll);
329061d3
JA
500
501 __io_queue_proc(poll, pt, head,
502 (struct io_poll **) &pt->req->async_data);
503}
504
49f1c68e
PB
505static bool io_poll_can_finish_inline(struct io_kiocb *req,
506 struct io_poll_table *pt)
507{
508 return pt->owning || io_poll_get_ownership(req);
509}
510
de08356f
PB
511/*
512 * Returns 0 when it's handed over for polling. The caller owns the requests if
513 * it returns non-zero, but otherwise should not touch it. Negative values
514 * contain an error code. When the result is >0, the polling has completed
515 * inline and ipt.result_mask is set to the mask.
516 */
329061d3
JA
517static int __io_arm_poll_handler(struct io_kiocb *req,
518 struct io_poll *poll,
49f1c68e
PB
519 struct io_poll_table *ipt, __poll_t mask,
520 unsigned issue_flags)
329061d3
JA
521{
522 struct io_ring_ctx *ctx = req->ctx;
523 int v;
524
525 INIT_HLIST_NODE(&req->hash_node);
526 req->work.cancel_seq = atomic_read(&ctx->cancel_seq);
527 io_init_poll_iocb(poll, mask, io_poll_wake);
528 poll->file = req->file;
329061d3
JA
529 req->apoll_events = poll->events;
530
531 ipt->pt._key = mask;
532 ipt->req = req;
533 ipt->error = 0;
534 ipt->nr_entries = 0;
329061d3 535 /*
49f1c68e
PB
536 * Polling is either completed here or via task_work, so if we're in the
537 * task context we're naturally serialised with tw by merit of running
538 * the same task. When it's io-wq, take the ownership to prevent tw
539 * from running. However, when we're in the task context, skip taking
540 * it as an optimisation.
541 *
542 * Note: even though the request won't be completed/freed, without
543 * ownership we still can race with io_poll_wake().
544 * io_poll_can_finish_inline() tries to deal with that.
329061d3 545 */
49f1c68e 546 ipt->owning = issue_flags & IO_URING_F_UNLOCKED;
49f1c68e 547 atomic_set(&req->poll_refs, (int)ipt->owning);
e8375e43
PB
548
549 /* io-wq doesn't hold uring_lock */
550 if (issue_flags & IO_URING_F_UNLOCKED)
551 req->flags &= ~REQ_F_HASH_LOCKED;
552
329061d3
JA
553 mask = vfs_poll(req->file, &ipt->pt) & poll->events;
554
de08356f
PB
555 if (unlikely(ipt->error || !ipt->nr_entries)) {
556 io_poll_remove_entries(req);
557
49f1c68e
PB
558 if (!io_poll_can_finish_inline(req, ipt)) {
559 io_poll_mark_cancelled(req);
560 return 0;
561 } else if (mask && (poll->events & EPOLLET)) {
de08356f
PB
562 ipt->result_mask = mask;
563 return 1;
de08356f 564 }
49f1c68e 565 return ipt->error ?: -EINVAL;
de08356f
PB
566 }
567
b9ba8a44
JA
568 if (mask &&
569 ((poll->events & (EPOLLET|EPOLLONESHOT)) == (EPOLLET|EPOLLONESHOT))) {
49f1c68e
PB
570 if (!io_poll_can_finish_inline(req, ipt))
571 return 0;
329061d3 572 io_poll_remove_entries(req);
063a0079 573 ipt->result_mask = mask;
329061d3 574 /* no one else has access to the req, forget about the ref */
063a0079 575 return 1;
329061d3 576 }
b9ba8a44 577
9ca9fb24
PB
578 if (req->flags & REQ_F_HASH_LOCKED)
579 io_poll_req_insert_locked(req);
580 else
581 io_poll_req_insert(req);
329061d3 582
49f1c68e
PB
583 if (mask && (poll->events & EPOLLET) &&
584 io_poll_can_finish_inline(req, ipt)) {
13a99017 585 __io_poll_execute(req, mask);
329061d3
JA
586 return 0;
587 }
588
49f1c68e
PB
589 if (ipt->owning) {
590 /*
591 * Release ownership. If someone tried to queue a tw while it was
592 * locked, kick it off for them.
593 */
594 v = atomic_dec_return(&req->poll_refs);
595 if (unlikely(v & IO_POLL_REF_MASK))
596 __io_poll_execute(req, 0);
597 }
329061d3
JA
598 return 0;
599}
600
601static void io_async_queue_proc(struct file *file, struct wait_queue_head *head,
602 struct poll_table_struct *p)
603{
604 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
605 struct async_poll *apoll = pt->req->apoll;
606
607 __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll);
608}
609
5204aa8c
PB
610static struct async_poll *io_req_alloc_apoll(struct io_kiocb *req,
611 unsigned issue_flags)
612{
613 struct io_ring_ctx *ctx = req->ctx;
9b797a37 614 struct io_cache_entry *entry;
5204aa8c
PB
615 struct async_poll *apoll;
616
617 if (req->flags & REQ_F_POLLED) {
618 apoll = req->apoll;
619 kfree(apoll->double_poll);
df730ec2
XL
620 } else if (!(issue_flags & IO_URING_F_UNLOCKED)) {
621 entry = io_alloc_cache_get(&ctx->apoll_cache);
622 if (entry == NULL)
623 goto alloc_apoll;
9b797a37 624 apoll = container_of(entry, struct async_poll, cache);
5204aa8c 625 } else {
df730ec2 626alloc_apoll:
5204aa8c
PB
627 apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC);
628 if (unlikely(!apoll))
629 return NULL;
630 }
631 apoll->double_poll = NULL;
632 req->apoll = apoll;
633 return apoll;
634}
635
329061d3
JA
636int io_arm_poll_handler(struct io_kiocb *req, unsigned issue_flags)
637{
638 const struct io_op_def *def = &io_op_defs[req->opcode];
329061d3
JA
639 struct async_poll *apoll;
640 struct io_poll_table ipt;
b9ba8a44 641 __poll_t mask = POLLPRI | POLLERR | EPOLLET;
329061d3
JA
642 int ret;
643
9ca9fb24
PB
644 /*
645 * apoll requests already grab the mutex to complete in the tw handler,
646 * so removal from the mutex-backed hash is free, use it by default.
647 */
e8375e43 648 req->flags |= REQ_F_HASH_LOCKED;
9ca9fb24 649
329061d3
JA
650 if (!def->pollin && !def->pollout)
651 return IO_APOLL_ABORTED;
652 if (!file_can_poll(req->file))
653 return IO_APOLL_ABORTED;
654 if ((req->flags & (REQ_F_POLLED|REQ_F_PARTIAL_IO)) == REQ_F_POLLED)
655 return IO_APOLL_ABORTED;
656 if (!(req->flags & REQ_F_APOLL_MULTISHOT))
657 mask |= EPOLLONESHOT;
658
659 if (def->pollin) {
660 mask |= EPOLLIN | EPOLLRDNORM;
661
662 /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */
663 if (req->flags & REQ_F_CLEAR_POLLIN)
664 mask &= ~EPOLLIN;
665 } else {
666 mask |= EPOLLOUT | EPOLLWRNORM;
667 }
668 if (def->poll_exclusive)
669 mask |= EPOLLEXCLUSIVE;
5204aa8c
PB
670
671 apoll = io_req_alloc_apoll(req, issue_flags);
672 if (!apoll)
673 return IO_APOLL_ABORTED;
329061d3
JA
674 req->flags |= REQ_F_POLLED;
675 ipt.pt._qproc = io_async_queue_proc;
676
677 io_kbuf_recycle(req, issue_flags);
678
49f1c68e 679 ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask, issue_flags);
de08356f
PB
680 if (ret)
681 return ret > 0 ? IO_APOLL_READY : IO_APOLL_ABORTED;
48863ffd 682 trace_io_uring_poll_arm(req, mask, apoll->poll.events);
329061d3
JA
683 return IO_APOLL_OK;
684}
685
9ca9fb24
PB
686static __cold bool io_poll_remove_all_table(struct task_struct *tsk,
687 struct io_hash_table *table,
688 bool cancel_all)
329061d3 689{
e6f89be6 690 unsigned nr_buckets = 1U << table->hash_bits;
329061d3
JA
691 struct hlist_node *tmp;
692 struct io_kiocb *req;
693 bool found = false;
694 int i;
695
e6f89be6
PB
696 for (i = 0; i < nr_buckets; i++) {
697 struct io_hash_bucket *hb = &table->hbs[i];
329061d3 698
38513c46
HX
699 spin_lock(&hb->lock);
700 hlist_for_each_entry_safe(req, tmp, &hb->list, hash_node) {
329061d3
JA
701 if (io_match_task_safe(req, tsk, cancel_all)) {
702 hlist_del_init(&req->hash_node);
703 io_poll_cancel_req(req);
704 found = true;
705 }
706 }
38513c46 707 spin_unlock(&hb->lock);
329061d3 708 }
329061d3
JA
709 return found;
710}
711
9ca9fb24
PB
712/*
713 * Returns true if we found and killed one or more poll requests
714 */
715__cold bool io_poll_remove_all(struct io_ring_ctx *ctx, struct task_struct *tsk,
716 bool cancel_all)
717 __must_hold(&ctx->uring_lock)
718{
b321823a
PB
719 bool ret;
720
721 ret = io_poll_remove_all_table(tsk, &ctx->cancel_table, cancel_all);
722 ret |= io_poll_remove_all_table(tsk, &ctx->cancel_table_locked, cancel_all);
723 return ret;
9ca9fb24
PB
724}
725
329061d3 726static struct io_kiocb *io_poll_find(struct io_ring_ctx *ctx, bool poll_only,
1ab1edb0 727 struct io_cancel_data *cd,
e6f89be6 728 struct io_hash_table *table,
1ab1edb0 729 struct io_hash_bucket **out_bucket)
329061d3 730{
329061d3 731 struct io_kiocb *req;
e6f89be6
PB
732 u32 index = hash_long(cd->data, table->hash_bits);
733 struct io_hash_bucket *hb = &table->hbs[index];
329061d3 734
1ab1edb0
PB
735 *out_bucket = NULL;
736
38513c46
HX
737 spin_lock(&hb->lock);
738 hlist_for_each_entry(req, &hb->list, hash_node) {
329061d3
JA
739 if (cd->data != req->cqe.user_data)
740 continue;
741 if (poll_only && req->opcode != IORING_OP_POLL_ADD)
742 continue;
743 if (cd->flags & IORING_ASYNC_CANCEL_ALL) {
744 if (cd->seq == req->work.cancel_seq)
745 continue;
746 req->work.cancel_seq = cd->seq;
747 }
1ab1edb0 748 *out_bucket = hb;
329061d3
JA
749 return req;
750 }
38513c46 751 spin_unlock(&hb->lock);
329061d3
JA
752 return NULL;
753}
754
755static struct io_kiocb *io_poll_file_find(struct io_ring_ctx *ctx,
1ab1edb0 756 struct io_cancel_data *cd,
e6f89be6 757 struct io_hash_table *table,
1ab1edb0 758 struct io_hash_bucket **out_bucket)
329061d3 759{
e6f89be6 760 unsigned nr_buckets = 1U << table->hash_bits;
329061d3
JA
761 struct io_kiocb *req;
762 int i;
763
1ab1edb0
PB
764 *out_bucket = NULL;
765
e6f89be6
PB
766 for (i = 0; i < nr_buckets; i++) {
767 struct io_hash_bucket *hb = &table->hbs[i];
329061d3 768
38513c46
HX
769 spin_lock(&hb->lock);
770 hlist_for_each_entry(req, &hb->list, hash_node) {
329061d3
JA
771 if (!(cd->flags & IORING_ASYNC_CANCEL_ANY) &&
772 req->file != cd->file)
773 continue;
774 if (cd->seq == req->work.cancel_seq)
775 continue;
776 req->work.cancel_seq = cd->seq;
1ab1edb0 777 *out_bucket = hb;
329061d3
JA
778 return req;
779 }
38513c46 780 spin_unlock(&hb->lock);
329061d3
JA
781 }
782 return NULL;
783}
784
9ca9fb24 785static int io_poll_disarm(struct io_kiocb *req)
329061d3 786{
9ca9fb24
PB
787 if (!req)
788 return -ENOENT;
329061d3 789 if (!io_poll_get_ownership(req))
9ca9fb24 790 return -EALREADY;
329061d3
JA
791 io_poll_remove_entries(req);
792 hash_del(&req->hash_node);
9ca9fb24 793 return 0;
329061d3
JA
794}
795
a2cdd519 796static int __io_poll_cancel(struct io_ring_ctx *ctx, struct io_cancel_data *cd,
e6f89be6 797 struct io_hash_table *table)
329061d3 798{
1ab1edb0 799 struct io_hash_bucket *bucket;
329061d3
JA
800 struct io_kiocb *req;
801
802 if (cd->flags & (IORING_ASYNC_CANCEL_FD|IORING_ASYNC_CANCEL_ANY))
e6f89be6 803 req = io_poll_file_find(ctx, cd, table, &bucket);
329061d3 804 else
e6f89be6 805 req = io_poll_find(ctx, false, cd, table, &bucket);
1ab1edb0
PB
806
807 if (req)
808 io_poll_cancel_req(req);
809 if (bucket)
810 spin_unlock(&bucket->lock);
811 return req ? 0 : -ENOENT;
329061d3
JA
812}
813
5d7943d9
PB
814int io_poll_cancel(struct io_ring_ctx *ctx, struct io_cancel_data *cd,
815 unsigned issue_flags)
a2cdd519 816{
9ca9fb24
PB
817 int ret;
818
819 ret = __io_poll_cancel(ctx, cd, &ctx->cancel_table);
820 if (ret != -ENOENT)
821 return ret;
822
823 io_ring_submit_lock(ctx, issue_flags);
824 ret = __io_poll_cancel(ctx, cd, &ctx->cancel_table_locked);
825 io_ring_submit_unlock(ctx, issue_flags);
826 return ret;
a2cdd519
PB
827}
828
329061d3
JA
829static __poll_t io_poll_parse_events(const struct io_uring_sqe *sqe,
830 unsigned int flags)
831{
832 u32 events;
833
834 events = READ_ONCE(sqe->poll32_events);
835#ifdef __BIG_ENDIAN
836 events = swahw32(events);
837#endif
838 if (!(flags & IORING_POLL_ADD_MULTI))
839 events |= EPOLLONESHOT;
b9ba8a44
JA
840 if (!(flags & IORING_POLL_ADD_LEVEL))
841 events |= EPOLLET;
842 return demangle_poll(events) |
843 (events & (EPOLLEXCLUSIVE|EPOLLONESHOT|EPOLLET));
329061d3
JA
844}
845
846int io_poll_remove_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
847{
f2ccb5ae 848 struct io_poll_update *upd = io_kiocb_to_cmd(req, struct io_poll_update);
329061d3
JA
849 u32 flags;
850
851 if (sqe->buf_index || sqe->splice_fd_in)
852 return -EINVAL;
853 flags = READ_ONCE(sqe->len);
854 if (flags & ~(IORING_POLL_UPDATE_EVENTS | IORING_POLL_UPDATE_USER_DATA |
855 IORING_POLL_ADD_MULTI))
856 return -EINVAL;
857 /* meaningless without update */
858 if (flags == IORING_POLL_ADD_MULTI)
859 return -EINVAL;
860
861 upd->old_user_data = READ_ONCE(sqe->addr);
862 upd->update_events = flags & IORING_POLL_UPDATE_EVENTS;
863 upd->update_user_data = flags & IORING_POLL_UPDATE_USER_DATA;
864
865 upd->new_user_data = READ_ONCE(sqe->off);
866 if (!upd->update_user_data && upd->new_user_data)
867 return -EINVAL;
868 if (upd->update_events)
869 upd->events = io_poll_parse_events(sqe, flags);
870 else if (sqe->poll32_events)
871 return -EINVAL;
872
873 return 0;
874}
875
876int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
877{
f2ccb5ae 878 struct io_poll *poll = io_kiocb_to_cmd(req, struct io_poll);
329061d3
JA
879 u32 flags;
880
881 if (sqe->buf_index || sqe->off || sqe->addr)
882 return -EINVAL;
883 flags = READ_ONCE(sqe->len);
d59bd748 884 if (flags & ~IORING_POLL_ADD_MULTI)
329061d3
JA
885 return -EINVAL;
886 if ((flags & IORING_POLL_ADD_MULTI) && (req->flags & REQ_F_CQE_SKIP))
887 return -EINVAL;
888
329061d3
JA
889 poll->events = io_poll_parse_events(sqe, flags);
890 return 0;
891}
892
893int io_poll_add(struct io_kiocb *req, unsigned int issue_flags)
894{
f2ccb5ae 895 struct io_poll *poll = io_kiocb_to_cmd(req, struct io_poll);
329061d3
JA
896 struct io_poll_table ipt;
897 int ret;
898
899 ipt.pt._qproc = io_poll_queue_proc;
900
9ca9fb24
PB
901 /*
902 * If sqpoll or single issuer, there is no contention for ->uring_lock
903 * and we'll end up holding it in tw handlers anyway.
904 */
e8375e43 905 if (req->ctx->flags & (IORING_SETUP_SQPOLL|IORING_SETUP_SINGLE_ISSUER))
9ca9fb24 906 req->flags |= REQ_F_HASH_LOCKED;
9ca9fb24 907
49f1c68e 908 ret = __io_arm_poll_handler(req, poll, &ipt, poll->events, issue_flags);
de08356f 909 if (ret > 0) {
063a0079 910 io_req_set_res(req, ipt.result_mask, 0);
329061d3
JA
911 return IOU_OK;
912 }
de08356f 913 return ret ?: IOU_ISSUE_SKIP_COMPLETE;
329061d3
JA
914}
915
916int io_poll_remove(struct io_kiocb *req, unsigned int issue_flags)
917{
f2ccb5ae 918 struct io_poll_update *poll_update = io_kiocb_to_cmd(req, struct io_poll_update);
329061d3
JA
919 struct io_cancel_data cd = { .data = poll_update->old_user_data, };
920 struct io_ring_ctx *ctx = req->ctx;
1ab1edb0 921 struct io_hash_bucket *bucket;
329061d3
JA
922 struct io_kiocb *preq;
923 int ret2, ret = 0;
924 bool locked;
925
e6f89be6 926 preq = io_poll_find(ctx, true, &cd, &ctx->cancel_table, &bucket);
9ca9fb24 927 ret2 = io_poll_disarm(preq);
1ab1edb0
PB
928 if (bucket)
929 spin_unlock(&bucket->lock);
9ca9fb24
PB
930 if (!ret2)
931 goto found;
932 if (ret2 != -ENOENT) {
933 ret = ret2;
38513c46
HX
934 goto out;
935 }
9ca9fb24
PB
936
937 io_ring_submit_lock(ctx, issue_flags);
938 preq = io_poll_find(ctx, true, &cd, &ctx->cancel_table_locked, &bucket);
939 ret2 = io_poll_disarm(preq);
940 if (bucket)
941 spin_unlock(&bucket->lock);
942 io_ring_submit_unlock(ctx, issue_flags);
943 if (ret2) {
944 ret = ret2;
329061d3
JA
945 goto out;
946 }
329061d3 947
9ca9fb24 948found:
bce5d70c
PB
949 if (WARN_ON_ONCE(preq->opcode != IORING_OP_POLL_ADD)) {
950 ret = -EFAULT;
951 goto out;
952 }
953
329061d3
JA
954 if (poll_update->update_events || poll_update->update_user_data) {
955 /* only mask one event flags, keep behavior flags */
956 if (poll_update->update_events) {
f2ccb5ae 957 struct io_poll *poll = io_kiocb_to_cmd(preq, struct io_poll);
329061d3
JA
958
959 poll->events &= ~0xffff;
960 poll->events |= poll_update->events & 0xffff;
961 poll->events |= IO_POLL_UNMASK;
962 }
963 if (poll_update->update_user_data)
964 preq->cqe.user_data = poll_update->new_user_data;
965
966 ret2 = io_poll_add(preq, issue_flags);
967 /* successfully updated, don't complete poll request */
968 if (!ret2 || ret2 == -EIOCBQUEUED)
969 goto out;
970 }
971
972 req_set_fail(preq);
973 io_req_set_res(preq, -ECANCELED, 0);
974 locked = !(issue_flags & IO_URING_F_UNLOCKED);
975 io_req_task_complete(preq, &locked);
976out:
977 if (ret < 0) {
978 req_set_fail(req);
979 return ret;
980 }
981 /* complete update request, we're done with it */
982 io_req_set_res(req, ret, 0);
983 return IOU_OK;
984}
9da7471e 985
9b797a37 986void io_apoll_cache_free(struct io_cache_entry *entry)
9da7471e 987{
9b797a37 988 kfree(container_of(entry, struct async_poll, cache));
9da7471e 989}