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;
427 ((poll->events & (EPOLLET|EPOLLONESHOT)) == (EPOLLET|EPOLLONESHOT))) {
428 io_poll_remove_entries(req);
429 /* no one else has access to the req, forget about the ref */
433 if (!mask && unlikely(ipt->error || !ipt->nr_entries)) {
434 io_poll_remove_entries(req);
436 ipt->error = -EINVAL;
440 spin_lock(&ctx->completion_lock);
441 io_poll_req_insert(req);
442 spin_unlock(&ctx->completion_lock);
444 if (mask && (poll->events & EPOLLET)) {
445 /* can't multishot if failed, just queue the event we've got */
446 if (unlikely(ipt->error || !ipt->nr_entries)) {
447 poll->events |= EPOLLONESHOT;
448 req->apoll_events |= EPOLLONESHOT;
451 __io_poll_execute(req, mask, poll->events);
456 * Release ownership. If someone tried to queue a tw while it was
457 * locked, kick it off for them.
459 v = atomic_dec_return(&req->poll_refs);
460 if (unlikely(v & IO_POLL_REF_MASK))
461 __io_poll_execute(req, 0, poll->events);
465 static void io_async_queue_proc(struct file *file, struct wait_queue_head *head,
466 struct poll_table_struct *p)
468 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
469 struct async_poll *apoll = pt->req->apoll;
471 __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll);
474 int io_arm_poll_handler(struct io_kiocb *req, unsigned issue_flags)
476 const struct io_op_def *def = &io_op_defs[req->opcode];
477 struct io_ring_ctx *ctx = req->ctx;
478 struct async_poll *apoll;
479 struct io_poll_table ipt;
480 __poll_t mask = POLLPRI | POLLERR | EPOLLET;
483 if (!def->pollin && !def->pollout)
484 return IO_APOLL_ABORTED;
485 if (!file_can_poll(req->file))
486 return IO_APOLL_ABORTED;
487 if ((req->flags & (REQ_F_POLLED|REQ_F_PARTIAL_IO)) == REQ_F_POLLED)
488 return IO_APOLL_ABORTED;
489 if (!(req->flags & REQ_F_APOLL_MULTISHOT))
490 mask |= EPOLLONESHOT;
493 mask |= EPOLLIN | EPOLLRDNORM;
495 /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */
496 if (req->flags & REQ_F_CLEAR_POLLIN)
499 mask |= EPOLLOUT | EPOLLWRNORM;
501 if (def->poll_exclusive)
502 mask |= EPOLLEXCLUSIVE;
503 if (req->flags & REQ_F_POLLED) {
505 kfree(apoll->double_poll);
506 } else if (!(issue_flags & IO_URING_F_UNLOCKED) &&
507 !list_empty(&ctx->apoll_cache)) {
508 apoll = list_first_entry(&ctx->apoll_cache, struct async_poll,
510 list_del_init(&apoll->poll.wait.entry);
512 apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC);
513 if (unlikely(!apoll))
514 return IO_APOLL_ABORTED;
516 apoll->double_poll = NULL;
518 req->flags |= REQ_F_POLLED;
519 ipt.pt._qproc = io_async_queue_proc;
521 io_kbuf_recycle(req, issue_flags);
523 ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask);
524 if (ret || ipt.error)
525 return ret ? IO_APOLL_READY : IO_APOLL_ABORTED;
527 trace_io_uring_poll_arm(ctx, req, req->cqe.user_data, req->opcode,
528 mask, apoll->poll.events);
533 * Returns true if we found and killed one or more poll requests
535 __cold bool io_poll_remove_all(struct io_ring_ctx *ctx, struct task_struct *tsk,
538 struct hlist_node *tmp;
539 struct io_kiocb *req;
543 spin_lock(&ctx->completion_lock);
544 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
545 struct hlist_head *list;
547 list = &ctx->cancel_hash[i];
548 hlist_for_each_entry_safe(req, tmp, list, hash_node) {
549 if (io_match_task_safe(req, tsk, cancel_all)) {
550 hlist_del_init(&req->hash_node);
551 io_poll_cancel_req(req);
556 spin_unlock(&ctx->completion_lock);
560 static struct io_kiocb *io_poll_find(struct io_ring_ctx *ctx, bool poll_only,
561 struct io_cancel_data *cd)
562 __must_hold(&ctx->completion_lock)
564 struct hlist_head *list;
565 struct io_kiocb *req;
567 list = &ctx->cancel_hash[hash_long(cd->data, ctx->cancel_hash_bits)];
568 hlist_for_each_entry(req, list, hash_node) {
569 if (cd->data != req->cqe.user_data)
571 if (poll_only && req->opcode != IORING_OP_POLL_ADD)
573 if (cd->flags & IORING_ASYNC_CANCEL_ALL) {
574 if (cd->seq == req->work.cancel_seq)
576 req->work.cancel_seq = cd->seq;
583 static struct io_kiocb *io_poll_file_find(struct io_ring_ctx *ctx,
584 struct io_cancel_data *cd)
585 __must_hold(&ctx->completion_lock)
587 struct io_kiocb *req;
590 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
591 struct hlist_head *list;
593 list = &ctx->cancel_hash[i];
594 hlist_for_each_entry(req, list, hash_node) {
595 if (!(cd->flags & IORING_ASYNC_CANCEL_ANY) &&
596 req->file != cd->file)
598 if (cd->seq == req->work.cancel_seq)
600 req->work.cancel_seq = cd->seq;
607 static bool io_poll_disarm(struct io_kiocb *req)
608 __must_hold(&ctx->completion_lock)
610 if (!io_poll_get_ownership(req))
612 io_poll_remove_entries(req);
613 hash_del(&req->hash_node);
617 int io_poll_cancel(struct io_ring_ctx *ctx, struct io_cancel_data *cd)
618 __must_hold(&ctx->completion_lock)
620 struct io_kiocb *req;
622 if (cd->flags & (IORING_ASYNC_CANCEL_FD|IORING_ASYNC_CANCEL_ANY))
623 req = io_poll_file_find(ctx, cd);
625 req = io_poll_find(ctx, false, cd);
628 io_poll_cancel_req(req);
632 static __poll_t io_poll_parse_events(const struct io_uring_sqe *sqe,
637 events = READ_ONCE(sqe->poll32_events);
639 events = swahw32(events);
641 if (!(flags & IORING_POLL_ADD_MULTI))
642 events |= EPOLLONESHOT;
643 if (!(flags & IORING_POLL_ADD_LEVEL))
645 return demangle_poll(events) |
646 (events & (EPOLLEXCLUSIVE|EPOLLONESHOT|EPOLLET));
649 int io_poll_remove_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
651 struct io_poll_update *upd = io_kiocb_to_cmd(req);
654 if (sqe->buf_index || sqe->splice_fd_in)
656 flags = READ_ONCE(sqe->len);
657 if (flags & ~(IORING_POLL_UPDATE_EVENTS | IORING_POLL_UPDATE_USER_DATA |
658 IORING_POLL_ADD_MULTI))
660 /* meaningless without update */
661 if (flags == IORING_POLL_ADD_MULTI)
664 upd->old_user_data = READ_ONCE(sqe->addr);
665 upd->update_events = flags & IORING_POLL_UPDATE_EVENTS;
666 upd->update_user_data = flags & IORING_POLL_UPDATE_USER_DATA;
668 upd->new_user_data = READ_ONCE(sqe->off);
669 if (!upd->update_user_data && upd->new_user_data)
671 if (upd->update_events)
672 upd->events = io_poll_parse_events(sqe, flags);
673 else if (sqe->poll32_events)
679 int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
681 struct io_poll *poll = io_kiocb_to_cmd(req);
684 if (sqe->buf_index || sqe->off || sqe->addr)
686 flags = READ_ONCE(sqe->len);
687 if (flags & ~(IORING_POLL_ADD_MULTI|IORING_POLL_ADD_LEVEL))
689 if ((flags & IORING_POLL_ADD_MULTI) && (req->flags & REQ_F_CQE_SKIP))
692 poll->events = io_poll_parse_events(sqe, flags);
696 int io_poll_add(struct io_kiocb *req, unsigned int issue_flags)
698 struct io_poll *poll = io_kiocb_to_cmd(req);
699 struct io_poll_table ipt;
702 ipt.pt._qproc = io_poll_queue_proc;
704 ret = __io_arm_poll_handler(req, poll, &ipt, poll->events);
706 io_req_set_res(req, ret, 0);
714 return IOU_ISSUE_SKIP_COMPLETE;
717 int io_poll_remove(struct io_kiocb *req, unsigned int issue_flags)
719 struct io_poll_update *poll_update = io_kiocb_to_cmd(req);
720 struct io_cancel_data cd = { .data = poll_update->old_user_data, };
721 struct io_ring_ctx *ctx = req->ctx;
722 struct io_kiocb *preq;
726 spin_lock(&ctx->completion_lock);
727 preq = io_poll_find(ctx, true, &cd);
728 if (!preq || !io_poll_disarm(preq)) {
729 spin_unlock(&ctx->completion_lock);
730 ret = preq ? -EALREADY : -ENOENT;
733 spin_unlock(&ctx->completion_lock);
735 if (poll_update->update_events || poll_update->update_user_data) {
736 /* only mask one event flags, keep behavior flags */
737 if (poll_update->update_events) {
738 struct io_poll *poll = io_kiocb_to_cmd(preq);
740 poll->events &= ~0xffff;
741 poll->events |= poll_update->events & 0xffff;
742 poll->events |= IO_POLL_UNMASK;
744 if (poll_update->update_user_data)
745 preq->cqe.user_data = poll_update->new_user_data;
747 ret2 = io_poll_add(preq, issue_flags);
748 /* successfully updated, don't complete poll request */
749 if (!ret2 || ret2 == -EIOCBQUEUED)
754 io_req_set_res(preq, -ECANCELED, 0);
755 locked = !(issue_flags & IO_URING_F_UNLOCKED);
756 io_req_task_complete(preq, &locked);
762 /* complete update request, we're done with it */
763 io_req_set_res(req, ret, 0);