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