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"
23 struct io_poll_update {
29 bool update_user_data;
32 struct io_poll_table {
33 struct poll_table_struct pt;
39 #define IO_POLL_CANCEL_FLAG BIT(31)
40 #define IO_POLL_REF_MASK GENMASK(30, 0)
43 * If refs part of ->poll_refs (see IO_POLL_REF_MASK) is 0, it's free. We can
44 * bump it and acquire ownership. It's disallowed to modify requests while not
45 * owning it, that prevents from races for enqueueing task_work's and b/w
46 * arming poll and wakeups.
48 static inline bool io_poll_get_ownership(struct io_kiocb *req)
50 return !(atomic_fetch_inc(&req->poll_refs) & IO_POLL_REF_MASK);
53 static void io_poll_mark_cancelled(struct io_kiocb *req)
55 atomic_or(IO_POLL_CANCEL_FLAG, &req->poll_refs);
58 static struct io_poll *io_poll_get_double(struct io_kiocb *req)
60 /* pure poll stashes this in ->async_data, poll driven retry elsewhere */
61 if (req->opcode == IORING_OP_POLL_ADD)
62 return req->async_data;
63 return req->apoll->double_poll;
66 static struct io_poll *io_poll_get_single(struct io_kiocb *req)
68 if (req->opcode == IORING_OP_POLL_ADD)
69 return io_kiocb_to_cmd(req);
70 return &req->apoll->poll;
73 static void io_poll_req_insert(struct io_kiocb *req)
75 struct io_ring_ctx *ctx = req->ctx;
76 struct hlist_head *list;
78 list = &ctx->cancel_hash[hash_long(req->cqe.user_data, ctx->cancel_hash_bits)];
79 hlist_add_head(&req->hash_node, list);
82 static void io_init_poll_iocb(struct io_poll *poll, __poll_t events,
83 wait_queue_func_t wake_func)
86 #define IO_POLL_UNMASK (EPOLLERR|EPOLLHUP|EPOLLNVAL|EPOLLRDHUP)
87 /* mask in events that we always want/need */
88 poll->events = events | IO_POLL_UNMASK;
89 INIT_LIST_HEAD(&poll->wait.entry);
90 init_waitqueue_func_entry(&poll->wait, wake_func);
93 static inline void io_poll_remove_entry(struct io_poll *poll)
95 struct wait_queue_head *head = smp_load_acquire(&poll->head);
98 spin_lock_irq(&head->lock);
99 list_del_init(&poll->wait.entry);
101 spin_unlock_irq(&head->lock);
105 static void io_poll_remove_entries(struct io_kiocb *req)
108 * Nothing to do if neither of those flags are set. Avoid dipping
109 * into the poll/apoll/double cachelines if we can.
111 if (!(req->flags & (REQ_F_SINGLE_POLL | REQ_F_DOUBLE_POLL)))
115 * While we hold the waitqueue lock and the waitqueue is nonempty,
116 * wake_up_pollfree() will wait for us. However, taking the waitqueue
117 * lock in the first place can race with the waitqueue being freed.
119 * We solve this as eventpoll does: by taking advantage of the fact that
120 * all users of wake_up_pollfree() will RCU-delay the actual free. If
121 * we enter rcu_read_lock() and see that the pointer to the queue is
122 * non-NULL, we can then lock it without the memory being freed out from
125 * Keep holding rcu_read_lock() as long as we hold the queue lock, in
126 * case the caller deletes the entry from the queue, leaving it empty.
127 * In that case, only RCU prevents the queue memory from being freed.
130 if (req->flags & REQ_F_SINGLE_POLL)
131 io_poll_remove_entry(io_poll_get_single(req));
132 if (req->flags & REQ_F_DOUBLE_POLL)
133 io_poll_remove_entry(io_poll_get_double(req));
138 * All poll tw should go through this. Checks for poll events, manages
139 * references, does rewait, etc.
141 * Returns a negative error on failure. >0 when no action require, which is
142 * either spurious wakeup or multishot CQE is served. 0 when it's done with
143 * the request, then the mask is stored in req->cqe.res.
145 static int io_poll_check_events(struct io_kiocb *req, bool *locked)
147 struct io_ring_ctx *ctx = req->ctx;
150 /* req->task == current here, checking PF_EXITING is safe */
151 if (unlikely(req->task->flags & PF_EXITING))
155 v = atomic_read(&req->poll_refs);
157 /* tw handler should be the owner, and so have some references */
158 if (WARN_ON_ONCE(!(v & IO_POLL_REF_MASK)))
160 if (v & IO_POLL_CANCEL_FLAG)
164 struct poll_table_struct pt = { ._key = req->apoll_events };
165 req->cqe.res = vfs_poll(req->file, &pt) & req->apoll_events;
168 if ((unlikely(!req->cqe.res)))
170 if (req->apoll_events & EPOLLONESHOT)
173 /* multishot, just fill a CQE and proceed */
174 if (!(req->flags & REQ_F_APOLL_MULTISHOT)) {
175 __poll_t mask = mangle_poll(req->cqe.res &
179 spin_lock(&ctx->completion_lock);
180 filled = io_fill_cqe_aux(ctx, req->cqe.user_data,
181 mask, IORING_CQE_F_MORE);
182 io_commit_cqring(ctx);
183 spin_unlock(&ctx->completion_lock);
185 io_cqring_ev_posted(ctx);
191 ret = io_poll_issue(req, locked);
196 * Release all references, retry if someone tried to restart
197 * task_work while we were executing it.
199 } while (atomic_sub_return(v & IO_POLL_REF_MASK, &req->poll_refs));
204 static void io_poll_task_func(struct io_kiocb *req, bool *locked)
206 struct io_ring_ctx *ctx = req->ctx;
209 ret = io_poll_check_events(req, locked);
214 struct io_poll *poll = io_kiocb_to_cmd(req);
216 req->cqe.res = mangle_poll(req->cqe.res & poll->events);
222 io_poll_remove_entries(req);
223 spin_lock(&ctx->completion_lock);
224 hash_del(&req->hash_node);
226 __io_req_complete_post(req);
227 io_commit_cqring(ctx);
228 spin_unlock(&ctx->completion_lock);
229 io_cqring_ev_posted(ctx);
232 static void io_apoll_task_func(struct io_kiocb *req, bool *locked)
234 struct io_ring_ctx *ctx = req->ctx;
237 ret = io_poll_check_events(req, locked);
241 io_poll_remove_entries(req);
242 spin_lock(&ctx->completion_lock);
243 hash_del(&req->hash_node);
244 spin_unlock(&ctx->completion_lock);
247 io_req_task_submit(req, locked);
249 io_req_complete_failed(req, ret);
252 static void __io_poll_execute(struct io_kiocb *req, int mask,
253 __poll_t __maybe_unused events)
255 io_req_set_res(req, mask, 0);
257 * This is useful for poll that is armed on behalf of another
258 * request, and where the wakeup path could be on a different
259 * CPU. We want to avoid pulling in req->apoll->events for that
262 if (req->opcode == IORING_OP_POLL_ADD)
263 req->io_task_work.func = io_poll_task_func;
265 req->io_task_work.func = io_apoll_task_func;
267 trace_io_uring_task_add(req->ctx, req, req->cqe.user_data, req->opcode, mask);
268 io_req_task_work_add(req);
271 static inline void io_poll_execute(struct io_kiocb *req, int res,
274 if (io_poll_get_ownership(req))
275 __io_poll_execute(req, res, events);
278 static void io_poll_cancel_req(struct io_kiocb *req)
280 io_poll_mark_cancelled(req);
281 /* kick tw, which should complete the request */
282 io_poll_execute(req, 0, 0);
285 #define wqe_to_req(wait) ((void *)((unsigned long) (wait)->private & ~1))
286 #define wqe_is_double(wait) ((unsigned long) (wait)->private & 1)
287 #define IO_ASYNC_POLL_COMMON (EPOLLONESHOT | EPOLLPRI)
289 static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
292 struct io_kiocb *req = wqe_to_req(wait);
293 struct io_poll *poll = container_of(wait, struct io_poll, wait);
294 __poll_t mask = key_to_poll(key);
296 if (unlikely(mask & POLLFREE)) {
297 io_poll_mark_cancelled(req);
298 /* we have to kick tw in case it's not already */
299 io_poll_execute(req, 0, poll->events);
302 * If the waitqueue is being freed early but someone is already
303 * holds ownership over it, we have to tear down the request as
304 * best we can. That means immediately removing the request from
305 * its waitqueue and preventing all further accesses to the
306 * waitqueue via the request.
308 list_del_init(&poll->wait.entry);
311 * Careful: this *must* be the last step, since as soon
312 * as req->head is NULL'ed out, the request can be
313 * completed and freed, since aio_poll_complete_work()
314 * will no longer need to take the waitqueue lock.
316 smp_store_release(&poll->head, NULL);
320 /* for instances that support it check for an event match first */
321 if (mask && !(mask & (poll->events & ~IO_ASYNC_POLL_COMMON)))
324 if (io_poll_get_ownership(req)) {
325 /* optional, saves extra locking for removal in tw handler */
326 if (mask && poll->events & EPOLLONESHOT) {
327 list_del_init(&poll->wait.entry);
329 if (wqe_is_double(wait))
330 req->flags &= ~REQ_F_DOUBLE_POLL;
332 req->flags &= ~REQ_F_SINGLE_POLL;
334 __io_poll_execute(req, mask, poll->events);
339 static void __io_queue_proc(struct io_poll *poll, struct io_poll_table *pt,
340 struct wait_queue_head *head,
341 struct io_poll **poll_ptr)
343 struct io_kiocb *req = pt->req;
344 unsigned long wqe_private = (unsigned long) req;
347 * The file being polled uses multiple waitqueues for poll handling
348 * (e.g. one for read, one for write). Setup a separate io_poll
351 if (unlikely(pt->nr_entries)) {
352 struct io_poll *first = poll;
354 /* double add on the same waitqueue head, ignore */
355 if (first->head == head)
357 /* already have a 2nd entry, fail a third attempt */
359 if ((*poll_ptr)->head == head)
365 poll = kmalloc(sizeof(*poll), GFP_ATOMIC);
370 /* mark as double wq entry */
372 req->flags |= REQ_F_DOUBLE_POLL;
373 io_init_poll_iocb(poll, first->events, first->wait.func);
375 if (req->opcode == IORING_OP_POLL_ADD)
376 req->flags |= REQ_F_ASYNC_DATA;
379 req->flags |= REQ_F_SINGLE_POLL;
382 poll->wait.private = (void *) wqe_private;
384 if (poll->events & EPOLLEXCLUSIVE)
385 add_wait_queue_exclusive(head, &poll->wait);
387 add_wait_queue(head, &poll->wait);
390 static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
391 struct poll_table_struct *p)
393 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
394 struct io_poll *poll = io_kiocb_to_cmd(pt->req);
396 __io_queue_proc(poll, pt, head,
397 (struct io_poll **) &pt->req->async_data);
400 static int __io_arm_poll_handler(struct io_kiocb *req,
401 struct io_poll *poll,
402 struct io_poll_table *ipt, __poll_t mask)
404 struct io_ring_ctx *ctx = req->ctx;
407 INIT_HLIST_NODE(&req->hash_node);
408 req->work.cancel_seq = atomic_read(&ctx->cancel_seq);
409 io_init_poll_iocb(poll, mask, io_poll_wake);
410 poll->file = req->file;
412 req->apoll_events = poll->events;
420 * Take the ownership to delay any tw execution up until we're done
421 * with poll arming. see io_poll_get_ownership().
423 atomic_set(&req->poll_refs, 1);
424 mask = vfs_poll(req->file, &ipt->pt) & poll->events;
426 if (mask && (poll->events & EPOLLONESHOT)) {
427 io_poll_remove_entries(req);
428 /* no one else has access to the req, forget about the ref */
431 if (!mask && unlikely(ipt->error || !ipt->nr_entries)) {
432 io_poll_remove_entries(req);
434 ipt->error = -EINVAL;
438 spin_lock(&ctx->completion_lock);
439 io_poll_req_insert(req);
440 spin_unlock(&ctx->completion_lock);
443 /* can't multishot if failed, just queue the event we've got */
444 if (unlikely(ipt->error || !ipt->nr_entries)) {
445 poll->events |= EPOLLONESHOT;
446 req->apoll_events |= EPOLLONESHOT;
449 __io_poll_execute(req, mask, poll->events);
454 * Release ownership. If someone tried to queue a tw while it was
455 * locked, kick it off for them.
457 v = atomic_dec_return(&req->poll_refs);
458 if (unlikely(v & IO_POLL_REF_MASK))
459 __io_poll_execute(req, 0, poll->events);
463 static void io_async_queue_proc(struct file *file, struct wait_queue_head *head,
464 struct poll_table_struct *p)
466 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
467 struct async_poll *apoll = pt->req->apoll;
469 __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll);
472 int io_arm_poll_handler(struct io_kiocb *req, unsigned issue_flags)
474 const struct io_op_def *def = &io_op_defs[req->opcode];
475 struct io_ring_ctx *ctx = req->ctx;
476 struct async_poll *apoll;
477 struct io_poll_table ipt;
478 __poll_t mask = POLLPRI | POLLERR;
481 if (!def->pollin && !def->pollout)
482 return IO_APOLL_ABORTED;
483 if (!file_can_poll(req->file))
484 return IO_APOLL_ABORTED;
485 if ((req->flags & (REQ_F_POLLED|REQ_F_PARTIAL_IO)) == REQ_F_POLLED)
486 return IO_APOLL_ABORTED;
487 if (!(req->flags & REQ_F_APOLL_MULTISHOT))
488 mask |= EPOLLONESHOT;
491 mask |= EPOLLIN | EPOLLRDNORM;
493 /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */
494 if (req->flags & REQ_F_CLEAR_POLLIN)
497 mask |= EPOLLOUT | EPOLLWRNORM;
499 if (def->poll_exclusive)
500 mask |= EPOLLEXCLUSIVE;
501 if (req->flags & REQ_F_POLLED) {
503 kfree(apoll->double_poll);
504 } else if (!(issue_flags & IO_URING_F_UNLOCKED) &&
505 !list_empty(&ctx->apoll_cache)) {
506 apoll = list_first_entry(&ctx->apoll_cache, struct async_poll,
508 list_del_init(&apoll->poll.wait.entry);
510 apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC);
511 if (unlikely(!apoll))
512 return IO_APOLL_ABORTED;
514 apoll->double_poll = NULL;
516 req->flags |= REQ_F_POLLED;
517 ipt.pt._qproc = io_async_queue_proc;
519 io_kbuf_recycle(req, issue_flags);
521 ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask);
522 if (ret || ipt.error)
523 return ret ? IO_APOLL_READY : IO_APOLL_ABORTED;
525 trace_io_uring_poll_arm(ctx, req, req->cqe.user_data, req->opcode,
526 mask, apoll->poll.events);
531 * Returns true if we found and killed one or more poll requests
533 __cold bool io_poll_remove_all(struct io_ring_ctx *ctx, struct task_struct *tsk,
536 struct hlist_node *tmp;
537 struct io_kiocb *req;
541 spin_lock(&ctx->completion_lock);
542 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
543 struct hlist_head *list;
545 list = &ctx->cancel_hash[i];
546 hlist_for_each_entry_safe(req, tmp, list, hash_node) {
547 if (io_match_task_safe(req, tsk, cancel_all)) {
548 hlist_del_init(&req->hash_node);
549 io_poll_cancel_req(req);
554 spin_unlock(&ctx->completion_lock);
558 static struct io_kiocb *io_poll_find(struct io_ring_ctx *ctx, bool poll_only,
559 struct io_cancel_data *cd)
560 __must_hold(&ctx->completion_lock)
562 struct hlist_head *list;
563 struct io_kiocb *req;
565 list = &ctx->cancel_hash[hash_long(cd->data, ctx->cancel_hash_bits)];
566 hlist_for_each_entry(req, list, hash_node) {
567 if (cd->data != req->cqe.user_data)
569 if (poll_only && req->opcode != IORING_OP_POLL_ADD)
571 if (cd->flags & IORING_ASYNC_CANCEL_ALL) {
572 if (cd->seq == req->work.cancel_seq)
574 req->work.cancel_seq = cd->seq;
581 static struct io_kiocb *io_poll_file_find(struct io_ring_ctx *ctx,
582 struct io_cancel_data *cd)
583 __must_hold(&ctx->completion_lock)
585 struct io_kiocb *req;
588 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
589 struct hlist_head *list;
591 list = &ctx->cancel_hash[i];
592 hlist_for_each_entry(req, list, hash_node) {
593 if (!(cd->flags & IORING_ASYNC_CANCEL_ANY) &&
594 req->file != cd->file)
596 if (cd->seq == req->work.cancel_seq)
598 req->work.cancel_seq = cd->seq;
605 static bool io_poll_disarm(struct io_kiocb *req)
606 __must_hold(&ctx->completion_lock)
608 if (!io_poll_get_ownership(req))
610 io_poll_remove_entries(req);
611 hash_del(&req->hash_node);
615 int io_poll_cancel(struct io_ring_ctx *ctx, struct io_cancel_data *cd)
616 __must_hold(&ctx->completion_lock)
618 struct io_kiocb *req;
620 if (cd->flags & (IORING_ASYNC_CANCEL_FD|IORING_ASYNC_CANCEL_ANY))
621 req = io_poll_file_find(ctx, cd);
623 req = io_poll_find(ctx, false, cd);
626 io_poll_cancel_req(req);
630 static __poll_t io_poll_parse_events(const struct io_uring_sqe *sqe,
635 events = READ_ONCE(sqe->poll32_events);
637 events = swahw32(events);
639 if (!(flags & IORING_POLL_ADD_MULTI))
640 events |= EPOLLONESHOT;
641 return demangle_poll(events) | (events & (EPOLLEXCLUSIVE|EPOLLONESHOT));
644 int io_poll_remove_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
646 struct io_poll_update *upd = io_kiocb_to_cmd(req);
649 if (sqe->buf_index || sqe->splice_fd_in)
651 flags = READ_ONCE(sqe->len);
652 if (flags & ~(IORING_POLL_UPDATE_EVENTS | IORING_POLL_UPDATE_USER_DATA |
653 IORING_POLL_ADD_MULTI))
655 /* meaningless without update */
656 if (flags == IORING_POLL_ADD_MULTI)
659 upd->old_user_data = READ_ONCE(sqe->addr);
660 upd->update_events = flags & IORING_POLL_UPDATE_EVENTS;
661 upd->update_user_data = flags & IORING_POLL_UPDATE_USER_DATA;
663 upd->new_user_data = READ_ONCE(sqe->off);
664 if (!upd->update_user_data && upd->new_user_data)
666 if (upd->update_events)
667 upd->events = io_poll_parse_events(sqe, flags);
668 else if (sqe->poll32_events)
674 int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
676 struct io_poll *poll = io_kiocb_to_cmd(req);
679 if (sqe->buf_index || sqe->off || sqe->addr)
681 flags = READ_ONCE(sqe->len);
682 if (flags & ~IORING_POLL_ADD_MULTI)
684 if ((flags & IORING_POLL_ADD_MULTI) && (req->flags & REQ_F_CQE_SKIP))
687 io_req_set_refcount(req);
688 poll->events = io_poll_parse_events(sqe, flags);
692 int io_poll_add(struct io_kiocb *req, unsigned int issue_flags)
694 struct io_poll *poll = io_kiocb_to_cmd(req);
695 struct io_poll_table ipt;
698 ipt.pt._qproc = io_poll_queue_proc;
700 ret = __io_arm_poll_handler(req, poll, &ipt, poll->events);
702 io_req_set_res(req, ret, 0);
710 return IOU_ISSUE_SKIP_COMPLETE;
713 int io_poll_remove(struct io_kiocb *req, unsigned int issue_flags)
715 struct io_poll_update *poll_update = io_kiocb_to_cmd(req);
716 struct io_cancel_data cd = { .data = poll_update->old_user_data, };
717 struct io_ring_ctx *ctx = req->ctx;
718 struct io_kiocb *preq;
722 spin_lock(&ctx->completion_lock);
723 preq = io_poll_find(ctx, true, &cd);
724 if (!preq || !io_poll_disarm(preq)) {
725 spin_unlock(&ctx->completion_lock);
726 ret = preq ? -EALREADY : -ENOENT;
729 spin_unlock(&ctx->completion_lock);
731 if (poll_update->update_events || poll_update->update_user_data) {
732 /* only mask one event flags, keep behavior flags */
733 if (poll_update->update_events) {
734 struct io_poll *poll = io_kiocb_to_cmd(preq);
736 poll->events &= ~0xffff;
737 poll->events |= poll_update->events & 0xffff;
738 poll->events |= IO_POLL_UNMASK;
740 if (poll_update->update_user_data)
741 preq->cqe.user_data = poll_update->new_user_data;
743 ret2 = io_poll_add(preq, issue_flags);
744 /* successfully updated, don't complete poll request */
745 if (!ret2 || ret2 == -EIOCBQUEUED)
750 io_req_set_res(preq, -ECANCELED, 0);
751 locked = !(issue_flags & IO_URING_F_UNLOCKED);
752 io_req_task_complete(preq, &locked);
758 /* complete update request, we're done with it */
759 io_req_set_res(req, ret, 0);