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