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