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