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"
24 struct io_poll_update {
30 bool update_user_data;
33 struct io_poll_table {
34 struct poll_table_struct pt;
40 #define IO_POLL_CANCEL_FLAG BIT(31)
41 #define IO_POLL_REF_MASK GENMASK(30, 0)
44 * If refs part of ->poll_refs (see IO_POLL_REF_MASK) is 0, it's free. We can
45 * bump it and acquire ownership. It's disallowed to modify requests while not
46 * owning it, that prevents from races for enqueueing task_work's and b/w
47 * arming poll and wakeups.
49 static inline bool io_poll_get_ownership(struct io_kiocb *req)
51 return !(atomic_fetch_inc(&req->poll_refs) & IO_POLL_REF_MASK);
54 static void io_poll_mark_cancelled(struct io_kiocb *req)
56 atomic_or(IO_POLL_CANCEL_FLAG, &req->poll_refs);
59 static struct io_poll *io_poll_get_double(struct io_kiocb *req)
61 /* pure poll stashes this in ->async_data, poll driven retry elsewhere */
62 if (req->opcode == IORING_OP_POLL_ADD)
63 return req->async_data;
64 return req->apoll->double_poll;
67 static struct io_poll *io_poll_get_single(struct io_kiocb *req)
69 if (req->opcode == IORING_OP_POLL_ADD)
70 return io_kiocb_to_cmd(req);
71 return &req->apoll->poll;
74 static void io_poll_req_insert(struct io_kiocb *req)
76 struct io_ring_ctx *ctx = req->ctx;
77 u32 index = hash_long(req->cqe.user_data, ctx->cancel_hash_bits);
78 struct io_hash_bucket *hb = &ctx->cancel_hash[index];
81 hlist_add_head(&req->hash_node, &hb->list);
82 spin_unlock(&hb->lock);
85 static void io_poll_req_delete(struct io_kiocb *req, struct io_ring_ctx *ctx)
87 u32 index = hash_long(req->cqe.user_data, ctx->cancel_hash_bits);
88 spinlock_t *lock = &ctx->cancel_hash[index].lock;
91 hash_del(&req->hash_node);
95 static void io_init_poll_iocb(struct io_poll *poll, __poll_t events,
96 wait_queue_func_t wake_func)
99 #define IO_POLL_UNMASK (EPOLLERR|EPOLLHUP|EPOLLNVAL|EPOLLRDHUP)
100 /* mask in events that we always want/need */
101 poll->events = events | IO_POLL_UNMASK;
102 INIT_LIST_HEAD(&poll->wait.entry);
103 init_waitqueue_func_entry(&poll->wait, wake_func);
106 static inline void io_poll_remove_entry(struct io_poll *poll)
108 struct wait_queue_head *head = smp_load_acquire(&poll->head);
111 spin_lock_irq(&head->lock);
112 list_del_init(&poll->wait.entry);
114 spin_unlock_irq(&head->lock);
118 static void io_poll_remove_entries(struct io_kiocb *req)
121 * Nothing to do if neither of those flags are set. Avoid dipping
122 * into the poll/apoll/double cachelines if we can.
124 if (!(req->flags & (REQ_F_SINGLE_POLL | REQ_F_DOUBLE_POLL)))
128 * While we hold the waitqueue lock and the waitqueue is nonempty,
129 * wake_up_pollfree() will wait for us. However, taking the waitqueue
130 * lock in the first place can race with the waitqueue being freed.
132 * We solve this as eventpoll does: by taking advantage of the fact that
133 * all users of wake_up_pollfree() will RCU-delay the actual free. If
134 * we enter rcu_read_lock() and see that the pointer to the queue is
135 * non-NULL, we can then lock it without the memory being freed out from
138 * Keep holding rcu_read_lock() as long as we hold the queue lock, in
139 * case the caller deletes the entry from the queue, leaving it empty.
140 * In that case, only RCU prevents the queue memory from being freed.
143 if (req->flags & REQ_F_SINGLE_POLL)
144 io_poll_remove_entry(io_poll_get_single(req));
145 if (req->flags & REQ_F_DOUBLE_POLL)
146 io_poll_remove_entry(io_poll_get_double(req));
151 * All poll tw should go through this. Checks for poll events, manages
152 * references, does rewait, etc.
154 * Returns a negative error on failure. >0 when no action require, which is
155 * either spurious wakeup or multishot CQE is served. 0 when it's done with
156 * the request, then the mask is stored in req->cqe.res.
158 static int io_poll_check_events(struct io_kiocb *req, bool *locked)
160 struct io_ring_ctx *ctx = req->ctx;
163 /* req->task == current here, checking PF_EXITING is safe */
164 if (unlikely(req->task->flags & PF_EXITING))
168 v = atomic_read(&req->poll_refs);
170 /* tw handler should be the owner, and so have some references */
171 if (WARN_ON_ONCE(!(v & IO_POLL_REF_MASK)))
173 if (v & IO_POLL_CANCEL_FLAG)
177 struct poll_table_struct pt = { ._key = req->apoll_events };
178 req->cqe.res = vfs_poll(req->file, &pt) & req->apoll_events;
181 if ((unlikely(!req->cqe.res)))
183 if (req->apoll_events & EPOLLONESHOT)
186 /* multishot, just fill a CQE and proceed */
187 if (!(req->flags & REQ_F_APOLL_MULTISHOT)) {
188 __poll_t mask = mangle_poll(req->cqe.res &
192 spin_lock(&ctx->completion_lock);
193 filled = io_fill_cqe_aux(ctx, req->cqe.user_data,
194 mask, IORING_CQE_F_MORE);
195 io_commit_cqring(ctx);
196 spin_unlock(&ctx->completion_lock);
198 io_cqring_ev_posted(ctx);
204 ret = io_poll_issue(req, locked);
209 * Release all references, retry if someone tried to restart
210 * task_work while we were executing it.
212 } while (atomic_sub_return(v & IO_POLL_REF_MASK, &req->poll_refs));
217 static void io_poll_task_func(struct io_kiocb *req, bool *locked)
219 struct io_ring_ctx *ctx = req->ctx;
222 ret = io_poll_check_events(req, locked);
227 struct io_poll *poll = io_kiocb_to_cmd(req);
229 req->cqe.res = mangle_poll(req->cqe.res & poll->events);
235 io_poll_remove_entries(req);
236 io_poll_req_delete(req, ctx);
237 io_req_set_res(req, req->cqe.res, 0);
238 io_req_task_complete(req, locked);
241 static void io_apoll_task_func(struct io_kiocb *req, bool *locked)
245 ret = io_poll_check_events(req, locked);
249 io_poll_remove_entries(req);
250 io_poll_req_delete(req, req->ctx);
253 io_req_task_submit(req, locked);
255 io_req_complete_failed(req, ret);
258 static void __io_poll_execute(struct io_kiocb *req, int mask,
259 __poll_t __maybe_unused events)
261 io_req_set_res(req, mask, 0);
263 * This is useful for poll that is armed on behalf of another
264 * request, and where the wakeup path could be on a different
265 * CPU. We want to avoid pulling in req->apoll->events for that
268 if (req->opcode == IORING_OP_POLL_ADD)
269 req->io_task_work.func = io_poll_task_func;
271 req->io_task_work.func = io_apoll_task_func;
273 trace_io_uring_task_add(req->ctx, req, req->cqe.user_data, req->opcode, mask);
274 io_req_task_work_add(req);
277 static inline void io_poll_execute(struct io_kiocb *req, int res,
280 if (io_poll_get_ownership(req))
281 __io_poll_execute(req, res, events);
284 static void io_poll_cancel_req(struct io_kiocb *req)
286 io_poll_mark_cancelled(req);
287 /* kick tw, which should complete the request */
288 io_poll_execute(req, 0, 0);
291 #define wqe_to_req(wait) ((void *)((unsigned long) (wait)->private & ~1))
292 #define wqe_is_double(wait) ((unsigned long) (wait)->private & 1)
293 #define IO_ASYNC_POLL_COMMON (EPOLLONESHOT | EPOLLPRI)
295 static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
298 struct io_kiocb *req = wqe_to_req(wait);
299 struct io_poll *poll = container_of(wait, struct io_poll, wait);
300 __poll_t mask = key_to_poll(key);
302 if (unlikely(mask & POLLFREE)) {
303 io_poll_mark_cancelled(req);
304 /* we have to kick tw in case it's not already */
305 io_poll_execute(req, 0, poll->events);
308 * If the waitqueue is being freed early but someone is already
309 * holds ownership over it, we have to tear down the request as
310 * best we can. That means immediately removing the request from
311 * its waitqueue and preventing all further accesses to the
312 * waitqueue via the request.
314 list_del_init(&poll->wait.entry);
317 * Careful: this *must* be the last step, since as soon
318 * as req->head is NULL'ed out, the request can be
319 * completed and freed, since aio_poll_complete_work()
320 * will no longer need to take the waitqueue lock.
322 smp_store_release(&poll->head, NULL);
326 /* for instances that support it check for an event match first */
327 if (mask && !(mask & (poll->events & ~IO_ASYNC_POLL_COMMON)))
330 if (io_poll_get_ownership(req)) {
331 /* optional, saves extra locking for removal in tw handler */
332 if (mask && poll->events & EPOLLONESHOT) {
333 list_del_init(&poll->wait.entry);
335 if (wqe_is_double(wait))
336 req->flags &= ~REQ_F_DOUBLE_POLL;
338 req->flags &= ~REQ_F_SINGLE_POLL;
340 __io_poll_execute(req, mask, poll->events);
345 static void __io_queue_proc(struct io_poll *poll, struct io_poll_table *pt,
346 struct wait_queue_head *head,
347 struct io_poll **poll_ptr)
349 struct io_kiocb *req = pt->req;
350 unsigned long wqe_private = (unsigned long) req;
353 * The file being polled uses multiple waitqueues for poll handling
354 * (e.g. one for read, one for write). Setup a separate io_poll
357 if (unlikely(pt->nr_entries)) {
358 struct io_poll *first = poll;
360 /* double add on the same waitqueue head, ignore */
361 if (first->head == head)
363 /* already have a 2nd entry, fail a third attempt */
365 if ((*poll_ptr)->head == head)
371 poll = kmalloc(sizeof(*poll), GFP_ATOMIC);
376 /* mark as double wq entry */
378 req->flags |= REQ_F_DOUBLE_POLL;
379 io_init_poll_iocb(poll, first->events, first->wait.func);
381 if (req->opcode == IORING_OP_POLL_ADD)
382 req->flags |= REQ_F_ASYNC_DATA;
385 req->flags |= REQ_F_SINGLE_POLL;
388 poll->wait.private = (void *) wqe_private;
390 if (poll->events & EPOLLEXCLUSIVE)
391 add_wait_queue_exclusive(head, &poll->wait);
393 add_wait_queue(head, &poll->wait);
396 static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
397 struct poll_table_struct *p)
399 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
400 struct io_poll *poll = io_kiocb_to_cmd(pt->req);
402 __io_queue_proc(poll, pt, head,
403 (struct io_poll **) &pt->req->async_data);
406 static int __io_arm_poll_handler(struct io_kiocb *req,
407 struct io_poll *poll,
408 struct io_poll_table *ipt, __poll_t mask)
410 struct io_ring_ctx *ctx = req->ctx;
413 INIT_HLIST_NODE(&req->hash_node);
414 req->work.cancel_seq = atomic_read(&ctx->cancel_seq);
415 io_init_poll_iocb(poll, mask, io_poll_wake);
416 poll->file = req->file;
418 req->apoll_events = poll->events;
426 * Take the ownership to delay any tw execution up until we're done
427 * with poll arming. see io_poll_get_ownership().
429 atomic_set(&req->poll_refs, 1);
430 mask = vfs_poll(req->file, &ipt->pt) & poll->events;
433 ((poll->events & (EPOLLET|EPOLLONESHOT)) == (EPOLLET|EPOLLONESHOT))) {
434 io_poll_remove_entries(req);
435 /* no one else has access to the req, forget about the ref */
439 if (!mask && unlikely(ipt->error || !ipt->nr_entries)) {
440 io_poll_remove_entries(req);
442 ipt->error = -EINVAL;
446 io_poll_req_insert(req);
448 if (mask && (poll->events & EPOLLET)) {
449 /* can't multishot if failed, just queue the event we've got */
450 if (unlikely(ipt->error || !ipt->nr_entries)) {
451 poll->events |= EPOLLONESHOT;
452 req->apoll_events |= EPOLLONESHOT;
455 __io_poll_execute(req, mask, poll->events);
460 * Release ownership. If someone tried to queue a tw while it was
461 * locked, kick it off for them.
463 v = atomic_dec_return(&req->poll_refs);
464 if (unlikely(v & IO_POLL_REF_MASK))
465 __io_poll_execute(req, 0, poll->events);
469 static void io_async_queue_proc(struct file *file, struct wait_queue_head *head,
470 struct poll_table_struct *p)
472 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
473 struct async_poll *apoll = pt->req->apoll;
475 __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll);
478 int io_arm_poll_handler(struct io_kiocb *req, unsigned issue_flags)
480 const struct io_op_def *def = &io_op_defs[req->opcode];
481 struct io_ring_ctx *ctx = req->ctx;
482 struct async_poll *apoll;
483 struct io_poll_table ipt;
484 __poll_t mask = POLLPRI | POLLERR | EPOLLET;
487 if (!def->pollin && !def->pollout)
488 return IO_APOLL_ABORTED;
489 if (!file_can_poll(req->file))
490 return IO_APOLL_ABORTED;
491 if ((req->flags & (REQ_F_POLLED|REQ_F_PARTIAL_IO)) == REQ_F_POLLED)
492 return IO_APOLL_ABORTED;
493 if (!(req->flags & REQ_F_APOLL_MULTISHOT))
494 mask |= EPOLLONESHOT;
497 mask |= EPOLLIN | EPOLLRDNORM;
499 /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */
500 if (req->flags & REQ_F_CLEAR_POLLIN)
503 mask |= EPOLLOUT | EPOLLWRNORM;
505 if (def->poll_exclusive)
506 mask |= EPOLLEXCLUSIVE;
507 if (req->flags & REQ_F_POLLED) {
509 kfree(apoll->double_poll);
510 } else if (!(issue_flags & IO_URING_F_UNLOCKED) &&
511 !list_empty(&ctx->apoll_cache)) {
512 apoll = list_first_entry(&ctx->apoll_cache, struct async_poll,
514 list_del_init(&apoll->poll.wait.entry);
516 apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC);
517 if (unlikely(!apoll))
518 return IO_APOLL_ABORTED;
520 apoll->double_poll = NULL;
522 req->flags |= REQ_F_POLLED;
523 ipt.pt._qproc = io_async_queue_proc;
525 io_kbuf_recycle(req, issue_flags);
527 ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask);
528 if (ret || ipt.error)
529 return ret ? IO_APOLL_READY : IO_APOLL_ABORTED;
531 trace_io_uring_poll_arm(ctx, req, req->cqe.user_data, req->opcode,
532 mask, apoll->poll.events);
537 * Returns true if we found and killed one or more poll requests
539 __cold bool io_poll_remove_all(struct io_ring_ctx *ctx, struct task_struct *tsk,
542 struct hlist_node *tmp;
543 struct io_kiocb *req;
547 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
548 struct io_hash_bucket *hb = &ctx->cancel_hash[i];
550 spin_lock(&hb->lock);
551 hlist_for_each_entry_safe(req, tmp, &hb->list, hash_node) {
552 if (io_match_task_safe(req, tsk, cancel_all)) {
553 hlist_del_init(&req->hash_node);
554 io_poll_cancel_req(req);
558 spin_unlock(&hb->lock);
563 static struct io_kiocb *io_poll_find(struct io_ring_ctx *ctx, bool poll_only,
564 struct io_cancel_data *cd,
565 struct io_hash_bucket hash_table[],
566 struct io_hash_bucket **out_bucket)
568 struct io_kiocb *req;
569 u32 index = hash_long(cd->data, ctx->cancel_hash_bits);
570 struct io_hash_bucket *hb = &hash_table[index];
574 spin_lock(&hb->lock);
575 hlist_for_each_entry(req, &hb->list, hash_node) {
576 if (cd->data != req->cqe.user_data)
578 if (poll_only && req->opcode != IORING_OP_POLL_ADD)
580 if (cd->flags & IORING_ASYNC_CANCEL_ALL) {
581 if (cd->seq == req->work.cancel_seq)
583 req->work.cancel_seq = cd->seq;
588 spin_unlock(&hb->lock);
592 static struct io_kiocb *io_poll_file_find(struct io_ring_ctx *ctx,
593 struct io_cancel_data *cd,
594 struct io_hash_bucket hash_table[],
595 struct io_hash_bucket **out_bucket)
597 struct io_kiocb *req;
602 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
603 struct io_hash_bucket *hb = &hash_table[i];
605 spin_lock(&hb->lock);
606 hlist_for_each_entry(req, &hb->list, hash_node) {
607 if (!(cd->flags & IORING_ASYNC_CANCEL_ANY) &&
608 req->file != cd->file)
610 if (cd->seq == req->work.cancel_seq)
612 req->work.cancel_seq = cd->seq;
616 spin_unlock(&hb->lock);
621 static bool io_poll_disarm(struct io_kiocb *req)
623 if (!io_poll_get_ownership(req))
625 io_poll_remove_entries(req);
626 hash_del(&req->hash_node);
630 static int __io_poll_cancel(struct io_ring_ctx *ctx, struct io_cancel_data *cd,
631 struct io_hash_bucket hash_table[])
633 struct io_hash_bucket *bucket;
634 struct io_kiocb *req;
636 if (cd->flags & (IORING_ASYNC_CANCEL_FD|IORING_ASYNC_CANCEL_ANY))
637 req = io_poll_file_find(ctx, cd, ctx->cancel_hash, &bucket);
639 req = io_poll_find(ctx, false, cd, ctx->cancel_hash, &bucket);
642 io_poll_cancel_req(req);
644 spin_unlock(&bucket->lock);
645 return req ? 0 : -ENOENT;
648 int io_poll_cancel(struct io_ring_ctx *ctx, struct io_cancel_data *cd)
650 return __io_poll_cancel(ctx, cd, ctx->cancel_hash);
653 static __poll_t io_poll_parse_events(const struct io_uring_sqe *sqe,
658 events = READ_ONCE(sqe->poll32_events);
660 events = swahw32(events);
662 if (!(flags & IORING_POLL_ADD_MULTI))
663 events |= EPOLLONESHOT;
664 if (!(flags & IORING_POLL_ADD_LEVEL))
666 return demangle_poll(events) |
667 (events & (EPOLLEXCLUSIVE|EPOLLONESHOT|EPOLLET));
670 int io_poll_remove_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
672 struct io_poll_update *upd = io_kiocb_to_cmd(req);
675 if (sqe->buf_index || sqe->splice_fd_in)
677 flags = READ_ONCE(sqe->len);
678 if (flags & ~(IORING_POLL_UPDATE_EVENTS | IORING_POLL_UPDATE_USER_DATA |
679 IORING_POLL_ADD_MULTI))
681 /* meaningless without update */
682 if (flags == IORING_POLL_ADD_MULTI)
685 upd->old_user_data = READ_ONCE(sqe->addr);
686 upd->update_events = flags & IORING_POLL_UPDATE_EVENTS;
687 upd->update_user_data = flags & IORING_POLL_UPDATE_USER_DATA;
689 upd->new_user_data = READ_ONCE(sqe->off);
690 if (!upd->update_user_data && upd->new_user_data)
692 if (upd->update_events)
693 upd->events = io_poll_parse_events(sqe, flags);
694 else if (sqe->poll32_events)
700 int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
702 struct io_poll *poll = io_kiocb_to_cmd(req);
705 if (sqe->buf_index || sqe->off || sqe->addr)
707 flags = READ_ONCE(sqe->len);
708 if (flags & ~(IORING_POLL_ADD_MULTI|IORING_POLL_ADD_LEVEL))
710 if ((flags & IORING_POLL_ADD_MULTI) && (req->flags & REQ_F_CQE_SKIP))
713 poll->events = io_poll_parse_events(sqe, flags);
717 int io_poll_add(struct io_kiocb *req, unsigned int issue_flags)
719 struct io_poll *poll = io_kiocb_to_cmd(req);
720 struct io_poll_table ipt;
723 ipt.pt._qproc = io_poll_queue_proc;
725 ret = __io_arm_poll_handler(req, poll, &ipt, poll->events);
727 io_req_set_res(req, ret, 0);
735 return IOU_ISSUE_SKIP_COMPLETE;
738 int io_poll_remove(struct io_kiocb *req, unsigned int issue_flags)
740 struct io_poll_update *poll_update = io_kiocb_to_cmd(req);
741 struct io_cancel_data cd = { .data = poll_update->old_user_data, };
742 struct io_ring_ctx *ctx = req->ctx;
743 struct io_hash_bucket *bucket;
744 struct io_kiocb *preq;
748 preq = io_poll_find(ctx, true, &cd, ctx->cancel_hash, &bucket);
750 ret2 = io_poll_disarm(preq);
752 spin_unlock(&bucket->lock);
763 if (poll_update->update_events || poll_update->update_user_data) {
764 /* only mask one event flags, keep behavior flags */
765 if (poll_update->update_events) {
766 struct io_poll *poll = io_kiocb_to_cmd(preq);
768 poll->events &= ~0xffff;
769 poll->events |= poll_update->events & 0xffff;
770 poll->events |= IO_POLL_UNMASK;
772 if (poll_update->update_user_data)
773 preq->cqe.user_data = poll_update->new_user_data;
775 ret2 = io_poll_add(preq, issue_flags);
776 /* successfully updated, don't complete poll request */
777 if (!ret2 || ret2 == -EIOCBQUEUED)
782 io_req_set_res(preq, -ECANCELED, 0);
783 locked = !(issue_flags & IO_URING_F_UNLOCKED);
784 io_req_task_complete(preq, &locked);
790 /* complete update request, we're done with it */
791 io_req_set_res(req, ret, 0);