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