1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/kernel.h>
3 #include <linux/errno.h>
5 #include <linux/file.h>
7 #include <linux/slab.h>
8 #include <linux/poll.h>
9 #include <linux/hashtable.h>
10 #include <linux/io_uring.h>
12 #include <trace/events/io_uring.h>
14 #include <uapi/linux/io_uring.h>
16 #include "io_uring_types.h"
22 struct io_poll_update {
28 bool update_user_data;
31 struct io_poll_table {
32 struct poll_table_struct pt;
38 #define IO_POLL_CANCEL_FLAG BIT(31)
39 #define IO_POLL_REF_MASK GENMASK(30, 0)
42 * If refs part of ->poll_refs (see IO_POLL_REF_MASK) is 0, it's free. We can
43 * bump it and acquire ownership. It's disallowed to modify requests while not
44 * owning it, that prevents from races for enqueueing task_work's and b/w
45 * arming poll and wakeups.
47 static inline bool io_poll_get_ownership(struct io_kiocb *req)
49 return !(atomic_fetch_inc(&req->poll_refs) & IO_POLL_REF_MASK);
52 static void io_poll_mark_cancelled(struct io_kiocb *req)
54 atomic_or(IO_POLL_CANCEL_FLAG, &req->poll_refs);
57 static struct io_poll *io_poll_get_double(struct io_kiocb *req)
59 /* pure poll stashes this in ->async_data, poll driven retry elsewhere */
60 if (req->opcode == IORING_OP_POLL_ADD)
61 return req->async_data;
62 return req->apoll->double_poll;
65 static struct io_poll *io_poll_get_single(struct io_kiocb *req)
67 if (req->opcode == IORING_OP_POLL_ADD)
68 return io_kiocb_to_cmd(req);
69 return &req->apoll->poll;
72 static void io_poll_req_insert(struct io_kiocb *req)
74 struct io_ring_ctx *ctx = req->ctx;
75 struct hlist_head *list;
77 list = &ctx->cancel_hash[hash_long(req->cqe.user_data, ctx->cancel_hash_bits)];
78 hlist_add_head(&req->hash_node, list);
81 static void io_init_poll_iocb(struct io_poll *poll, __poll_t events,
82 wait_queue_func_t wake_func)
85 #define IO_POLL_UNMASK (EPOLLERR|EPOLLHUP|EPOLLNVAL|EPOLLRDHUP)
86 /* mask in events that we always want/need */
87 poll->events = events | IO_POLL_UNMASK;
88 INIT_LIST_HEAD(&poll->wait.entry);
89 init_waitqueue_func_entry(&poll->wait, wake_func);
92 static inline void io_poll_remove_entry(struct io_poll *poll)
94 struct wait_queue_head *head = smp_load_acquire(&poll->head);
97 spin_lock_irq(&head->lock);
98 list_del_init(&poll->wait.entry);
100 spin_unlock_irq(&head->lock);
104 static void io_poll_remove_entries(struct io_kiocb *req)
107 * Nothing to do if neither of those flags are set. Avoid dipping
108 * into the poll/apoll/double cachelines if we can.
110 if (!(req->flags & (REQ_F_SINGLE_POLL | REQ_F_DOUBLE_POLL)))
114 * While we hold the waitqueue lock and the waitqueue is nonempty,
115 * wake_up_pollfree() will wait for us. However, taking the waitqueue
116 * lock in the first place can race with the waitqueue being freed.
118 * We solve this as eventpoll does: by taking advantage of the fact that
119 * all users of wake_up_pollfree() will RCU-delay the actual free. If
120 * we enter rcu_read_lock() and see that the pointer to the queue is
121 * non-NULL, we can then lock it without the memory being freed out from
124 * Keep holding rcu_read_lock() as long as we hold the queue lock, in
125 * case the caller deletes the entry from the queue, leaving it empty.
126 * In that case, only RCU prevents the queue memory from being freed.
129 if (req->flags & REQ_F_SINGLE_POLL)
130 io_poll_remove_entry(io_poll_get_single(req));
131 if (req->flags & REQ_F_DOUBLE_POLL)
132 io_poll_remove_entry(io_poll_get_double(req));
137 * All poll tw should go through this. Checks for poll events, manages
138 * references, does rewait, etc.
140 * Returns a negative error on failure. >0 when no action require, which is
141 * either spurious wakeup or multishot CQE is served. 0 when it's done with
142 * the request, then the mask is stored in req->cqe.res.
144 static int io_poll_check_events(struct io_kiocb *req, bool *locked)
146 struct io_ring_ctx *ctx = req->ctx;
149 /* req->task == current here, checking PF_EXITING is safe */
150 if (unlikely(req->task->flags & PF_EXITING))
154 v = atomic_read(&req->poll_refs);
156 /* tw handler should be the owner, and so have some references */
157 if (WARN_ON_ONCE(!(v & IO_POLL_REF_MASK)))
159 if (v & IO_POLL_CANCEL_FLAG)
163 struct poll_table_struct pt = { ._key = req->apoll_events };
164 req->cqe.res = vfs_poll(req->file, &pt) & req->apoll_events;
167 if ((unlikely(!req->cqe.res)))
169 if (req->apoll_events & EPOLLONESHOT)
172 /* multishot, just fill a CQE and proceed */
173 if (!(req->flags & REQ_F_APOLL_MULTISHOT)) {
174 __poll_t mask = mangle_poll(req->cqe.res &
178 spin_lock(&ctx->completion_lock);
179 filled = io_fill_cqe_aux(ctx, req->cqe.user_data,
180 mask, IORING_CQE_F_MORE);
181 io_commit_cqring(ctx);
182 spin_unlock(&ctx->completion_lock);
184 io_cqring_ev_posted(ctx);
190 ret = io_poll_issue(req, locked);
195 * Release all references, retry if someone tried to restart
196 * task_work while we were executing it.
198 } while (atomic_sub_return(v & IO_POLL_REF_MASK, &req->poll_refs));
203 static void io_poll_task_func(struct io_kiocb *req, bool *locked)
205 struct io_ring_ctx *ctx = req->ctx;
208 ret = io_poll_check_events(req, locked);
213 struct io_poll *poll = io_kiocb_to_cmd(req);
215 req->cqe.res = mangle_poll(req->cqe.res & poll->events);
221 io_poll_remove_entries(req);
222 spin_lock(&ctx->completion_lock);
223 hash_del(&req->hash_node);
225 __io_req_complete_post(req);
226 io_commit_cqring(ctx);
227 spin_unlock(&ctx->completion_lock);
228 io_cqring_ev_posted(ctx);
231 static void io_apoll_task_func(struct io_kiocb *req, bool *locked)
233 struct io_ring_ctx *ctx = req->ctx;
236 ret = io_poll_check_events(req, locked);
240 io_poll_remove_entries(req);
241 spin_lock(&ctx->completion_lock);
242 hash_del(&req->hash_node);
243 spin_unlock(&ctx->completion_lock);
246 io_req_task_submit(req, locked);
248 io_req_complete_failed(req, ret);
251 static void __io_poll_execute(struct io_kiocb *req, int mask,
252 __poll_t __maybe_unused events)
254 io_req_set_res(req, mask, 0);
256 * This is useful for poll that is armed on behalf of another
257 * request, and where the wakeup path could be on a different
258 * CPU. We want to avoid pulling in req->apoll->events for that
261 if (req->opcode == IORING_OP_POLL_ADD)
262 req->io_task_work.func = io_poll_task_func;
264 req->io_task_work.func = io_apoll_task_func;
266 trace_io_uring_task_add(req->ctx, req, req->cqe.user_data, req->opcode, mask);
267 io_req_task_work_add(req);
270 static inline void io_poll_execute(struct io_kiocb *req, int res,
273 if (io_poll_get_ownership(req))
274 __io_poll_execute(req, res, events);
277 static void io_poll_cancel_req(struct io_kiocb *req)
279 io_poll_mark_cancelled(req);
280 /* kick tw, which should complete the request */
281 io_poll_execute(req, 0, 0);
284 #define wqe_to_req(wait) ((void *)((unsigned long) (wait)->private & ~1))
285 #define wqe_is_double(wait) ((unsigned long) (wait)->private & 1)
286 #define IO_ASYNC_POLL_COMMON (EPOLLONESHOT | EPOLLPRI)
288 static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
291 struct io_kiocb *req = wqe_to_req(wait);
292 struct io_poll *poll = container_of(wait, struct io_poll, wait);
293 __poll_t mask = key_to_poll(key);
295 if (unlikely(mask & POLLFREE)) {
296 io_poll_mark_cancelled(req);
297 /* we have to kick tw in case it's not already */
298 io_poll_execute(req, 0, poll->events);
301 * If the waitqueue is being freed early but someone is already
302 * holds ownership over it, we have to tear down the request as
303 * best we can. That means immediately removing the request from
304 * its waitqueue and preventing all further accesses to the
305 * waitqueue via the request.
307 list_del_init(&poll->wait.entry);
310 * Careful: this *must* be the last step, since as soon
311 * as req->head is NULL'ed out, the request can be
312 * completed and freed, since aio_poll_complete_work()
313 * will no longer need to take the waitqueue lock.
315 smp_store_release(&poll->head, NULL);
319 /* for instances that support it check for an event match first */
320 if (mask && !(mask & (poll->events & ~IO_ASYNC_POLL_COMMON)))
323 if (io_poll_get_ownership(req)) {
324 /* optional, saves extra locking for removal in tw handler */
325 if (mask && poll->events & EPOLLONESHOT) {
326 list_del_init(&poll->wait.entry);
328 if (wqe_is_double(wait))
329 req->flags &= ~REQ_F_DOUBLE_POLL;
331 req->flags &= ~REQ_F_SINGLE_POLL;
333 __io_poll_execute(req, mask, poll->events);
338 static void __io_queue_proc(struct io_poll *poll, struct io_poll_table *pt,
339 struct wait_queue_head *head,
340 struct io_poll **poll_ptr)
342 struct io_kiocb *req = pt->req;
343 unsigned long wqe_private = (unsigned long) req;
346 * The file being polled uses multiple waitqueues for poll handling
347 * (e.g. one for read, one for write). Setup a separate io_poll
350 if (unlikely(pt->nr_entries)) {
351 struct io_poll *first = poll;
353 /* double add on the same waitqueue head, ignore */
354 if (first->head == head)
356 /* already have a 2nd entry, fail a third attempt */
358 if ((*poll_ptr)->head == head)
364 poll = kmalloc(sizeof(*poll), GFP_ATOMIC);
369 /* mark as double wq entry */
371 req->flags |= REQ_F_DOUBLE_POLL;
372 io_init_poll_iocb(poll, first->events, first->wait.func);
374 if (req->opcode == IORING_OP_POLL_ADD)
375 req->flags |= REQ_F_ASYNC_DATA;
378 req->flags |= REQ_F_SINGLE_POLL;
381 poll->wait.private = (void *) wqe_private;
383 if (poll->events & EPOLLEXCLUSIVE)
384 add_wait_queue_exclusive(head, &poll->wait);
386 add_wait_queue(head, &poll->wait);
389 static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
390 struct poll_table_struct *p)
392 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
393 struct io_poll *poll = io_kiocb_to_cmd(pt->req);
395 __io_queue_proc(poll, pt, head,
396 (struct io_poll **) &pt->req->async_data);
399 static int __io_arm_poll_handler(struct io_kiocb *req,
400 struct io_poll *poll,
401 struct io_poll_table *ipt, __poll_t mask)
403 struct io_ring_ctx *ctx = req->ctx;
406 INIT_HLIST_NODE(&req->hash_node);
407 req->work.cancel_seq = atomic_read(&ctx->cancel_seq);
408 io_init_poll_iocb(poll, mask, io_poll_wake);
409 poll->file = req->file;
411 req->apoll_events = poll->events;
419 * Take the ownership to delay any tw execution up until we're done
420 * with poll arming. see io_poll_get_ownership().
422 atomic_set(&req->poll_refs, 1);
423 mask = vfs_poll(req->file, &ipt->pt) & poll->events;
425 if (mask && (poll->events & EPOLLONESHOT)) {
426 io_poll_remove_entries(req);
427 /* no one else has access to the req, forget about the ref */
430 if (!mask && unlikely(ipt->error || !ipt->nr_entries)) {
431 io_poll_remove_entries(req);
433 ipt->error = -EINVAL;
437 spin_lock(&ctx->completion_lock);
438 io_poll_req_insert(req);
439 spin_unlock(&ctx->completion_lock);
442 /* can't multishot if failed, just queue the event we've got */
443 if (unlikely(ipt->error || !ipt->nr_entries)) {
444 poll->events |= EPOLLONESHOT;
445 req->apoll_events |= EPOLLONESHOT;
448 __io_poll_execute(req, mask, poll->events);
453 * Release ownership. If someone tried to queue a tw while it was
454 * locked, kick it off for them.
456 v = atomic_dec_return(&req->poll_refs);
457 if (unlikely(v & IO_POLL_REF_MASK))
458 __io_poll_execute(req, 0, poll->events);
462 static void io_async_queue_proc(struct file *file, struct wait_queue_head *head,
463 struct poll_table_struct *p)
465 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
466 struct async_poll *apoll = pt->req->apoll;
468 __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll);
471 int io_arm_poll_handler(struct io_kiocb *req, unsigned issue_flags)
473 const struct io_op_def *def = &io_op_defs[req->opcode];
474 struct io_ring_ctx *ctx = req->ctx;
475 struct async_poll *apoll;
476 struct io_poll_table ipt;
477 __poll_t mask = POLLPRI | POLLERR;
480 if (!def->pollin && !def->pollout)
481 return IO_APOLL_ABORTED;
482 if (!file_can_poll(req->file))
483 return IO_APOLL_ABORTED;
484 if ((req->flags & (REQ_F_POLLED|REQ_F_PARTIAL_IO)) == REQ_F_POLLED)
485 return IO_APOLL_ABORTED;
486 if (!(req->flags & REQ_F_APOLL_MULTISHOT))
487 mask |= EPOLLONESHOT;
490 mask |= EPOLLIN | EPOLLRDNORM;
492 /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */
493 if (req->flags & REQ_F_CLEAR_POLLIN)
496 mask |= EPOLLOUT | EPOLLWRNORM;
498 if (def->poll_exclusive)
499 mask |= EPOLLEXCLUSIVE;
500 if (req->flags & REQ_F_POLLED) {
502 kfree(apoll->double_poll);
503 } else if (!(issue_flags & IO_URING_F_UNLOCKED) &&
504 !list_empty(&ctx->apoll_cache)) {
505 apoll = list_first_entry(&ctx->apoll_cache, struct async_poll,
507 list_del_init(&apoll->poll.wait.entry);
509 apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC);
510 if (unlikely(!apoll))
511 return IO_APOLL_ABORTED;
513 apoll->double_poll = NULL;
515 req->flags |= REQ_F_POLLED;
516 ipt.pt._qproc = io_async_queue_proc;
518 io_kbuf_recycle(req, issue_flags);
520 ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask);
521 if (ret || ipt.error)
522 return ret ? IO_APOLL_READY : IO_APOLL_ABORTED;
524 trace_io_uring_poll_arm(ctx, req, req->cqe.user_data, req->opcode,
525 mask, apoll->poll.events);
530 * Returns true if we found and killed one or more poll requests
532 __cold bool io_poll_remove_all(struct io_ring_ctx *ctx, struct task_struct *tsk,
535 struct hlist_node *tmp;
536 struct io_kiocb *req;
540 spin_lock(&ctx->completion_lock);
541 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
542 struct hlist_head *list;
544 list = &ctx->cancel_hash[i];
545 hlist_for_each_entry_safe(req, tmp, list, hash_node) {
546 if (io_match_task_safe(req, tsk, cancel_all)) {
547 hlist_del_init(&req->hash_node);
548 io_poll_cancel_req(req);
553 spin_unlock(&ctx->completion_lock);
557 static struct io_kiocb *io_poll_find(struct io_ring_ctx *ctx, bool poll_only,
558 struct io_cancel_data *cd)
559 __must_hold(&ctx->completion_lock)
561 struct hlist_head *list;
562 struct io_kiocb *req;
564 list = &ctx->cancel_hash[hash_long(cd->data, ctx->cancel_hash_bits)];
565 hlist_for_each_entry(req, list, hash_node) {
566 if (cd->data != req->cqe.user_data)
568 if (poll_only && req->opcode != IORING_OP_POLL_ADD)
570 if (cd->flags & IORING_ASYNC_CANCEL_ALL) {
571 if (cd->seq == req->work.cancel_seq)
573 req->work.cancel_seq = cd->seq;
580 static struct io_kiocb *io_poll_file_find(struct io_ring_ctx *ctx,
581 struct io_cancel_data *cd)
582 __must_hold(&ctx->completion_lock)
584 struct io_kiocb *req;
587 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
588 struct hlist_head *list;
590 list = &ctx->cancel_hash[i];
591 hlist_for_each_entry(req, list, hash_node) {
592 if (!(cd->flags & IORING_ASYNC_CANCEL_ANY) &&
593 req->file != cd->file)
595 if (cd->seq == req->work.cancel_seq)
597 req->work.cancel_seq = cd->seq;
604 static bool io_poll_disarm(struct io_kiocb *req)
605 __must_hold(&ctx->completion_lock)
607 if (!io_poll_get_ownership(req))
609 io_poll_remove_entries(req);
610 hash_del(&req->hash_node);
614 int io_poll_cancel(struct io_ring_ctx *ctx, struct io_cancel_data *cd)
615 __must_hold(&ctx->completion_lock)
617 struct io_kiocb *req;
619 if (cd->flags & (IORING_ASYNC_CANCEL_FD|IORING_ASYNC_CANCEL_ANY))
620 req = io_poll_file_find(ctx, cd);
622 req = io_poll_find(ctx, false, cd);
625 io_poll_cancel_req(req);
629 static __poll_t io_poll_parse_events(const struct io_uring_sqe *sqe,
634 events = READ_ONCE(sqe->poll32_events);
636 events = swahw32(events);
638 if (!(flags & IORING_POLL_ADD_MULTI))
639 events |= EPOLLONESHOT;
640 return demangle_poll(events) | (events & (EPOLLEXCLUSIVE|EPOLLONESHOT));
643 int io_poll_remove_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
645 struct io_poll_update *upd = io_kiocb_to_cmd(req);
648 if (sqe->buf_index || sqe->splice_fd_in)
650 flags = READ_ONCE(sqe->len);
651 if (flags & ~(IORING_POLL_UPDATE_EVENTS | IORING_POLL_UPDATE_USER_DATA |
652 IORING_POLL_ADD_MULTI))
654 /* meaningless without update */
655 if (flags == IORING_POLL_ADD_MULTI)
658 upd->old_user_data = READ_ONCE(sqe->addr);
659 upd->update_events = flags & IORING_POLL_UPDATE_EVENTS;
660 upd->update_user_data = flags & IORING_POLL_UPDATE_USER_DATA;
662 upd->new_user_data = READ_ONCE(sqe->off);
663 if (!upd->update_user_data && upd->new_user_data)
665 if (upd->update_events)
666 upd->events = io_poll_parse_events(sqe, flags);
667 else if (sqe->poll32_events)
673 int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
675 struct io_poll *poll = io_kiocb_to_cmd(req);
678 if (sqe->buf_index || sqe->off || sqe->addr)
680 flags = READ_ONCE(sqe->len);
681 if (flags & ~IORING_POLL_ADD_MULTI)
683 if ((flags & IORING_POLL_ADD_MULTI) && (req->flags & REQ_F_CQE_SKIP))
686 io_req_set_refcount(req);
687 poll->events = io_poll_parse_events(sqe, flags);
691 int io_poll_add(struct io_kiocb *req, unsigned int issue_flags)
693 struct io_poll *poll = io_kiocb_to_cmd(req);
694 struct io_poll_table ipt;
697 ipt.pt._qproc = io_poll_queue_proc;
699 ret = __io_arm_poll_handler(req, poll, &ipt, poll->events);
701 io_req_set_res(req, ret, 0);
709 return IOU_ISSUE_SKIP_COMPLETE;
712 int io_poll_remove(struct io_kiocb *req, unsigned int issue_flags)
714 struct io_poll_update *poll_update = io_kiocb_to_cmd(req);
715 struct io_cancel_data cd = { .data = poll_update->old_user_data, };
716 struct io_ring_ctx *ctx = req->ctx;
717 struct io_kiocb *preq;
721 spin_lock(&ctx->completion_lock);
722 preq = io_poll_find(ctx, true, &cd);
723 if (!preq || !io_poll_disarm(preq)) {
724 spin_unlock(&ctx->completion_lock);
725 ret = preq ? -EALREADY : -ENOENT;
728 spin_unlock(&ctx->completion_lock);
730 if (poll_update->update_events || poll_update->update_user_data) {
731 /* only mask one event flags, keep behavior flags */
732 if (poll_update->update_events) {
733 struct io_poll *poll = io_kiocb_to_cmd(preq);
735 poll->events &= ~0xffff;
736 poll->events |= poll_update->events & 0xffff;
737 poll->events |= IO_POLL_UNMASK;
739 if (poll_update->update_user_data)
740 preq->cqe.user_data = poll_update->new_user_data;
742 ret2 = io_poll_add(preq, issue_flags);
743 /* successfully updated, don't complete poll request */
744 if (!ret2 || ret2 == -EIOCBQUEUED)
749 io_req_set_res(preq, -ECANCELED, 0);
750 locked = !(issue_flags & IO_URING_F_UNLOCKED);
751 io_req_task_complete(preq, &locked);
757 /* complete update request, we're done with it */
758 io_req_set_res(req, ret, 0);