2022-05-26 02:31:09 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/errno.h>
|
|
|
|
#include <linux/fs.h>
|
|
|
|
#include <linux/file.h>
|
|
|
|
#include <linux/mm.h>
|
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/poll.h>
|
|
|
|
#include <linux/hashtable.h>
|
|
|
|
#include <linux/io_uring.h>
|
|
|
|
|
|
|
|
#include <trace/events/io_uring.h>
|
|
|
|
|
|
|
|
#include <uapi/linux/io_uring.h>
|
|
|
|
|
|
|
|
#include "io_uring.h"
|
2024-03-20 21:19:44 +00:00
|
|
|
#include "alloc_cache.h"
|
2022-05-26 02:31:09 +00:00
|
|
|
#include "refs.h"
|
2023-06-08 16:38:36 +00:00
|
|
|
#include "napi.h"
|
2022-05-26 02:31:09 +00:00
|
|
|
#include "opdef.h"
|
2022-06-13 13:07:23 +00:00
|
|
|
#include "kbuf.h"
|
2022-05-26 02:31:09 +00:00
|
|
|
#include "poll.h"
|
2022-06-16 09:22:02 +00:00
|
|
|
#include "cancel.h"
|
2022-05-26 02:31:09 +00:00
|
|
|
|
|
|
|
struct io_poll_update {
|
|
|
|
struct file *file;
|
|
|
|
u64 old_user_data;
|
|
|
|
u64 new_user_data;
|
|
|
|
__poll_t events;
|
|
|
|
bool update_events;
|
|
|
|
bool update_user_data;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct io_poll_table {
|
|
|
|
struct poll_table_struct pt;
|
|
|
|
struct io_kiocb *req;
|
|
|
|
int nr_entries;
|
|
|
|
int error;
|
2022-06-23 13:24:49 +00:00
|
|
|
bool owning;
|
2022-06-23 13:24:47 +00:00
|
|
|
/* output value, set only if arm poll returns >0 */
|
|
|
|
__poll_t result_mask;
|
2022-05-26 02:31:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#define IO_POLL_CANCEL_FLAG BIT(31)
|
2022-11-20 16:57:42 +00:00
|
|
|
#define IO_POLL_RETRY_FLAG BIT(30)
|
|
|
|
#define IO_POLL_REF_MASK GENMASK(29, 0)
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We usually have 1-2 refs taken, 128 is more than enough and we want to
|
|
|
|
* maximise the margin between this amount and the moment when it overflows.
|
|
|
|
*/
|
|
|
|
#define IO_POLL_REF_BIAS 128
|
2022-05-26 02:31:09 +00:00
|
|
|
|
2022-06-23 13:24:44 +00:00
|
|
|
#define IO_WQE_F_DOUBLE 1
|
|
|
|
|
2023-02-27 16:41:20 +00:00
|
|
|
static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
|
|
|
|
void *key);
|
|
|
|
|
2022-06-23 13:24:44 +00:00
|
|
|
static inline struct io_kiocb *wqe_to_req(struct wait_queue_entry *wqe)
|
|
|
|
{
|
|
|
|
unsigned long priv = (unsigned long)wqe->private;
|
|
|
|
|
|
|
|
return (struct io_kiocb *)(priv & ~IO_WQE_F_DOUBLE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool wqe_is_double(struct wait_queue_entry *wqe)
|
|
|
|
{
|
|
|
|
unsigned long priv = (unsigned long)wqe->private;
|
|
|
|
|
|
|
|
return priv & IO_WQE_F_DOUBLE;
|
|
|
|
}
|
|
|
|
|
2022-11-20 16:57:42 +00:00
|
|
|
static bool io_poll_get_ownership_slowpath(struct io_kiocb *req)
|
|
|
|
{
|
|
|
|
int v;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* poll_refs are already elevated and we don't have much hope for
|
|
|
|
* grabbing the ownership. Instead of incrementing set a retry flag
|
|
|
|
* to notify the loop that there might have been some change.
|
|
|
|
*/
|
|
|
|
v = atomic_fetch_or(IO_POLL_RETRY_FLAG, &req->poll_refs);
|
|
|
|
if (v & IO_POLL_REF_MASK)
|
|
|
|
return false;
|
|
|
|
return !(atomic_fetch_inc(&req->poll_refs) & IO_POLL_REF_MASK);
|
|
|
|
}
|
|
|
|
|
2022-05-26 02:31:09 +00:00
|
|
|
/*
|
|
|
|
* If refs part of ->poll_refs (see IO_POLL_REF_MASK) is 0, it's free. We can
|
|
|
|
* bump it and acquire ownership. It's disallowed to modify requests while not
|
|
|
|
* owning it, that prevents from races for enqueueing task_work's and b/w
|
|
|
|
* arming poll and wakeups.
|
|
|
|
*/
|
|
|
|
static inline bool io_poll_get_ownership(struct io_kiocb *req)
|
|
|
|
{
|
2022-11-20 16:57:42 +00:00
|
|
|
if (unlikely(atomic_read(&req->poll_refs) >= IO_POLL_REF_BIAS))
|
|
|
|
return io_poll_get_ownership_slowpath(req);
|
2022-05-26 02:31:09 +00:00
|
|
|
return !(atomic_fetch_inc(&req->poll_refs) & IO_POLL_REF_MASK);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void io_poll_mark_cancelled(struct io_kiocb *req)
|
|
|
|
{
|
|
|
|
atomic_or(IO_POLL_CANCEL_FLAG, &req->poll_refs);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct io_poll *io_poll_get_double(struct io_kiocb *req)
|
|
|
|
{
|
|
|
|
/* pure poll stashes this in ->async_data, poll driven retry elsewhere */
|
|
|
|
if (req->opcode == IORING_OP_POLL_ADD)
|
|
|
|
return req->async_data;
|
|
|
|
return req->apoll->double_poll;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct io_poll *io_poll_get_single(struct io_kiocb *req)
|
|
|
|
{
|
|
|
|
if (req->opcode == IORING_OP_POLL_ADD)
|
2022-08-11 07:11:15 +00:00
|
|
|
return io_kiocb_to_cmd(req, struct io_poll);
|
2022-05-26 02:31:09 +00:00
|
|
|
return &req->apoll->poll;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void io_poll_req_insert(struct io_kiocb *req)
|
|
|
|
{
|
2022-06-16 09:22:10 +00:00
|
|
|
struct io_hash_table *table = &req->ctx->cancel_table;
|
|
|
|
u32 index = hash_long(req->cqe.user_data, table->hash_bits);
|
2022-06-16 09:22:12 +00:00
|
|
|
|
2022-11-11 16:51:30 +00:00
|
|
|
lockdep_assert_held(&req->ctx->uring_lock);
|
|
|
|
|
2022-06-16 09:22:12 +00:00
|
|
|
hlist_add_head(&req->hash_node, &table->hbs[index].list);
|
|
|
|
}
|
|
|
|
|
2023-02-27 16:41:20 +00:00
|
|
|
static void io_init_poll_iocb(struct io_poll *poll, __poll_t events)
|
2022-05-26 02:31:09 +00:00
|
|
|
{
|
|
|
|
poll->head = NULL;
|
|
|
|
#define IO_POLL_UNMASK (EPOLLERR|EPOLLHUP|EPOLLNVAL|EPOLLRDHUP)
|
|
|
|
/* mask in events that we always want/need */
|
|
|
|
poll->events = events | IO_POLL_UNMASK;
|
|
|
|
INIT_LIST_HEAD(&poll->wait.entry);
|
2023-02-27 16:41:20 +00:00
|
|
|
init_waitqueue_func_entry(&poll->wait, io_poll_wake);
|
2022-05-26 02:31:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline void io_poll_remove_entry(struct io_poll *poll)
|
|
|
|
{
|
|
|
|
struct wait_queue_head *head = smp_load_acquire(&poll->head);
|
|
|
|
|
|
|
|
if (head) {
|
|
|
|
spin_lock_irq(&head->lock);
|
|
|
|
list_del_init(&poll->wait.entry);
|
|
|
|
poll->head = NULL;
|
|
|
|
spin_unlock_irq(&head->lock);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void io_poll_remove_entries(struct io_kiocb *req)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Nothing to do if neither of those flags are set. Avoid dipping
|
|
|
|
* into the poll/apoll/double cachelines if we can.
|
|
|
|
*/
|
|
|
|
if (!(req->flags & (REQ_F_SINGLE_POLL | REQ_F_DOUBLE_POLL)))
|
|
|
|
return;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* While we hold the waitqueue lock and the waitqueue is nonempty,
|
|
|
|
* wake_up_pollfree() will wait for us. However, taking the waitqueue
|
|
|
|
* lock in the first place can race with the waitqueue being freed.
|
|
|
|
*
|
|
|
|
* We solve this as eventpoll does: by taking advantage of the fact that
|
|
|
|
* all users of wake_up_pollfree() will RCU-delay the actual free. If
|
|
|
|
* we enter rcu_read_lock() and see that the pointer to the queue is
|
|
|
|
* non-NULL, we can then lock it without the memory being freed out from
|
|
|
|
* under us.
|
|
|
|
*
|
|
|
|
* Keep holding rcu_read_lock() as long as we hold the queue lock, in
|
|
|
|
* case the caller deletes the entry from the queue, leaving it empty.
|
|
|
|
* In that case, only RCU prevents the queue memory from being freed.
|
|
|
|
*/
|
|
|
|
rcu_read_lock();
|
|
|
|
if (req->flags & REQ_F_SINGLE_POLL)
|
|
|
|
io_poll_remove_entry(io_poll_get_single(req));
|
|
|
|
if (req->flags & REQ_F_DOUBLE_POLL)
|
|
|
|
io_poll_remove_entry(io_poll_get_double(req));
|
|
|
|
rcu_read_unlock();
|
|
|
|
}
|
|
|
|
|
2022-06-30 09:12:24 +00:00
|
|
|
enum {
|
|
|
|
IOU_POLL_DONE = 0,
|
|
|
|
IOU_POLL_NO_ACTION = 1,
|
2022-06-30 09:12:25 +00:00
|
|
|
IOU_POLL_REMOVE_POLL_USE_RES = 2,
|
2023-01-10 17:44:37 +00:00
|
|
|
IOU_POLL_REISSUE = 3,
|
2024-01-29 18:57:11 +00:00
|
|
|
IOU_POLL_REQUEUE = 4,
|
2022-06-30 09:12:24 +00:00
|
|
|
};
|
|
|
|
|
2024-01-29 18:52:54 +00:00
|
|
|
static void __io_poll_execute(struct io_kiocb *req, int mask)
|
|
|
|
{
|
|
|
|
unsigned flags = 0;
|
|
|
|
|
|
|
|
io_req_set_res(req, mask, 0);
|
|
|
|
req->io_task_work.func = io_poll_task_func;
|
|
|
|
|
|
|
|
trace_io_uring_task_add(req, mask);
|
|
|
|
|
|
|
|
if (!(req->flags & REQ_F_POLL_NO_LAZY))
|
|
|
|
flags = IOU_F_TWQ_LAZY_WAKE;
|
|
|
|
__io_req_task_work_add(req, flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void io_poll_execute(struct io_kiocb *req, int res)
|
|
|
|
{
|
|
|
|
if (io_poll_get_ownership(req))
|
|
|
|
__io_poll_execute(req, res);
|
|
|
|
}
|
|
|
|
|
2022-05-26 02:31:09 +00:00
|
|
|
/*
|
|
|
|
* All poll tw should go through this. Checks for poll events, manages
|
|
|
|
* references, does rewait, etc.
|
|
|
|
*
|
2023-01-10 17:44:37 +00:00
|
|
|
* Returns a negative error on failure. IOU_POLL_NO_ACTION when no action
|
|
|
|
* require, which is either spurious wakeup or multishot CQE is served.
|
|
|
|
* IOU_POLL_DONE when it's done with the request, then the mask is stored in
|
|
|
|
* req->cqe.res. IOU_POLL_REMOVE_POLL_USE_RES indicates to remove multishot
|
|
|
|
* poll and that the result is stored in req->cqe.
|
2022-05-26 02:31:09 +00:00
|
|
|
*/
|
2023-03-27 15:38:15 +00:00
|
|
|
static int io_poll_check_events(struct io_kiocb *req, struct io_tw_state *ts)
|
2022-05-26 02:31:09 +00:00
|
|
|
{
|
2023-01-10 17:44:37 +00:00
|
|
|
int v;
|
2022-05-26 02:31:09 +00:00
|
|
|
|
|
|
|
/* req->task == current here, checking PF_EXITING is safe */
|
|
|
|
if (unlikely(req->task->flags & PF_EXITING))
|
|
|
|
return -ECANCELED;
|
|
|
|
|
|
|
|
do {
|
|
|
|
v = atomic_read(&req->poll_refs);
|
|
|
|
|
2022-11-30 15:21:52 +00:00
|
|
|
if (unlikely(v != 1)) {
|
|
|
|
/* tw should be the owner and so have some refs */
|
|
|
|
if (WARN_ON_ONCE(!(v & IO_POLL_REF_MASK)))
|
2022-11-30 15:21:54 +00:00
|
|
|
return IOU_POLL_NO_ACTION;
|
2022-11-30 15:21:52 +00:00
|
|
|
if (v & IO_POLL_CANCEL_FLAG)
|
|
|
|
return -ECANCELED;
|
2022-11-20 16:57:42 +00:00
|
|
|
/*
|
2022-11-30 15:21:52 +00:00
|
|
|
* cqe.res contains only events of the first wake up
|
|
|
|
* and all others are to be lost. Redo vfs_poll() to get
|
|
|
|
* up to date state.
|
2022-11-20 16:57:42 +00:00
|
|
|
*/
|
2022-11-30 15:21:52 +00:00
|
|
|
if ((v & IO_POLL_REF_MASK) != 1)
|
|
|
|
req->cqe.res = 0;
|
|
|
|
|
|
|
|
if (v & IO_POLL_RETRY_FLAG) {
|
|
|
|
req->cqe.res = 0;
|
|
|
|
/*
|
|
|
|
* We won't find new events that came in between
|
|
|
|
* vfs_poll and the ref put unless we clear the
|
|
|
|
* flag in advance.
|
|
|
|
*/
|
|
|
|
atomic_andnot(IO_POLL_RETRY_FLAG, &req->poll_refs);
|
|
|
|
v &= ~IO_POLL_RETRY_FLAG;
|
|
|
|
}
|
2022-11-20 16:57:42 +00:00
|
|
|
}
|
2022-05-26 02:31:09 +00:00
|
|
|
|
2022-06-30 09:12:24 +00:00
|
|
|
/* the mask was stashed in __io_poll_execute */
|
2022-05-26 02:31:09 +00:00
|
|
|
if (!req->cqe.res) {
|
|
|
|
struct poll_table_struct pt = { ._key = req->apoll_events };
|
|
|
|
req->cqe.res = vfs_poll(req->file, &pt) & req->apoll_events;
|
2023-01-10 17:44:37 +00:00
|
|
|
/*
|
|
|
|
* We got woken with a mask, but someone else got to
|
|
|
|
* it first. The above vfs_poll() doesn't add us back
|
|
|
|
* to the waitqueue, so if we get nothing back, we
|
|
|
|
* should be safe and attempt a reissue.
|
|
|
|
*/
|
2023-01-20 22:08:21 +00:00
|
|
|
if (unlikely(!req->cqe.res)) {
|
|
|
|
/* Multishot armed need not reissue */
|
|
|
|
if (!(req->apoll_events & EPOLLONESHOT))
|
|
|
|
continue;
|
2023-01-10 17:44:37 +00:00
|
|
|
return IOU_POLL_REISSUE;
|
2023-01-20 22:08:21 +00:00
|
|
|
}
|
2022-05-26 02:31:09 +00:00
|
|
|
}
|
|
|
|
if (req->apoll_events & EPOLLONESHOT)
|
2022-06-30 09:12:24 +00:00
|
|
|
return IOU_POLL_DONE;
|
2022-05-26 02:31:09 +00:00
|
|
|
|
|
|
|
/* multishot, just fill a CQE and proceed */
|
|
|
|
if (!(req->flags & REQ_F_APOLL_MULTISHOT)) {
|
|
|
|
__poll_t mask = mangle_poll(req->cqe.res &
|
|
|
|
req->apoll_events);
|
|
|
|
|
2024-03-18 22:00:31 +00:00
|
|
|
if (!io_req_post_cqe(req, mask, IORING_CQE_F_MORE)) {
|
2022-06-30 09:12:27 +00:00
|
|
|
io_req_set_res(req, mask, 0);
|
|
|
|
return IOU_POLL_REMOVE_POLL_USE_RES;
|
|
|
|
}
|
2022-06-17 08:48:00 +00:00
|
|
|
} else {
|
2023-03-27 15:38:15 +00:00
|
|
|
int ret = io_poll_issue(req, ts);
|
2022-06-30 09:12:25 +00:00
|
|
|
if (ret == IOU_STOP_MULTISHOT)
|
|
|
|
return IOU_POLL_REMOVE_POLL_USE_RES;
|
2024-01-29 18:57:11 +00:00
|
|
|
else if (ret == IOU_REQUEUE)
|
|
|
|
return IOU_POLL_REQUEUE;
|
2022-06-30 09:12:24 +00:00
|
|
|
if (ret < 0)
|
2022-06-17 08:48:00 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2022-05-26 02:31:09 +00:00
|
|
|
|
2022-11-17 18:40:14 +00:00
|
|
|
/* force the next iteration to vfs_poll() */
|
|
|
|
req->cqe.res = 0;
|
|
|
|
|
2022-05-26 02:31:09 +00:00
|
|
|
/*
|
|
|
|
* Release all references, retry if someone tried to restart
|
|
|
|
* task_work while we were executing it.
|
|
|
|
*/
|
2024-02-01 19:49:37 +00:00
|
|
|
v &= IO_POLL_REF_MASK;
|
|
|
|
} while (atomic_sub_return(v, &req->poll_refs) & IO_POLL_REF_MASK);
|
2022-05-26 02:31:09 +00:00
|
|
|
|
2024-07-29 23:03:33 +00:00
|
|
|
io_napi_add(req);
|
2022-06-30 09:12:24 +00:00
|
|
|
return IOU_POLL_NO_ACTION;
|
2022-05-26 02:31:09 +00:00
|
|
|
}
|
|
|
|
|
2023-06-02 14:41:46 +00:00
|
|
|
void io_poll_task_func(struct io_kiocb *req, struct io_tw_state *ts)
|
2022-05-26 02:31:09 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2023-03-27 15:38:15 +00:00
|
|
|
ret = io_poll_check_events(req, ts);
|
2024-01-29 18:57:11 +00:00
|
|
|
if (ret == IOU_POLL_NO_ACTION) {
|
2022-05-26 02:31:09 +00:00
|
|
|
return;
|
2024-01-29 18:57:11 +00:00
|
|
|
} else if (ret == IOU_POLL_REQUEUE) {
|
|
|
|
__io_poll_execute(req, 0);
|
|
|
|
return;
|
|
|
|
}
|
2022-05-26 02:31:09 +00:00
|
|
|
io_poll_remove_entries(req);
|
2024-09-30 20:35:52 +00:00
|
|
|
/* task_work always has ->uring_lock held */
|
|
|
|
hash_del(&req->hash_node);
|
2022-06-16 09:22:12 +00:00
|
|
|
|
2022-11-30 15:21:55 +00:00
|
|
|
if (req->opcode == IORING_OP_POLL_ADD) {
|
|
|
|
if (ret == IOU_POLL_DONE) {
|
|
|
|
struct io_poll *poll;
|
2022-05-26 02:31:09 +00:00
|
|
|
|
2022-11-30 15:21:55 +00:00
|
|
|
poll = io_kiocb_to_cmd(req, struct io_poll);
|
|
|
|
req->cqe.res = mangle_poll(req->cqe.res & poll->events);
|
2023-01-10 17:44:37 +00:00
|
|
|
} else if (ret == IOU_POLL_REISSUE) {
|
2023-03-27 15:38:15 +00:00
|
|
|
io_req_task_submit(req, ts);
|
2023-01-10 17:44:37 +00:00
|
|
|
return;
|
2022-11-30 15:21:55 +00:00
|
|
|
} else if (ret != IOU_POLL_REMOVE_POLL_USE_RES) {
|
|
|
|
req->cqe.res = ret;
|
|
|
|
req_set_fail(req);
|
|
|
|
}
|
2022-05-26 02:31:09 +00:00
|
|
|
|
2022-11-30 15:21:55 +00:00
|
|
|
io_req_set_res(req, req->cqe.res, 0);
|
2023-03-27 15:38:15 +00:00
|
|
|
io_req_task_complete(req, ts);
|
2022-11-30 15:21:55 +00:00
|
|
|
} else {
|
2023-03-27 15:38:15 +00:00
|
|
|
io_tw_lock(req->ctx, ts);
|
2022-11-30 15:21:55 +00:00
|
|
|
|
|
|
|
if (ret == IOU_POLL_REMOVE_POLL_USE_RES)
|
2023-03-27 15:38:15 +00:00
|
|
|
io_req_task_complete(req, ts);
|
2023-01-10 17:44:37 +00:00
|
|
|
else if (ret == IOU_POLL_DONE || ret == IOU_POLL_REISSUE)
|
2023-03-27 15:38:15 +00:00
|
|
|
io_req_task_submit(req, ts);
|
2022-11-30 15:21:55 +00:00
|
|
|
else
|
|
|
|
io_req_defer_failed(req, ret);
|
|
|
|
}
|
2022-05-26 02:31:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void io_poll_cancel_req(struct io_kiocb *req)
|
|
|
|
{
|
|
|
|
io_poll_mark_cancelled(req);
|
|
|
|
/* kick tw, which should complete the request */
|
2022-06-23 13:24:45 +00:00
|
|
|
io_poll_execute(req, 0);
|
2022-05-26 02:31:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#define IO_ASYNC_POLL_COMMON (EPOLLONESHOT | EPOLLPRI)
|
|
|
|
|
2022-06-21 20:34:15 +00:00
|
|
|
static __cold int io_pollfree_wake(struct io_kiocb *req, struct io_poll *poll)
|
|
|
|
{
|
|
|
|
io_poll_mark_cancelled(req);
|
|
|
|
/* we have to kick tw in case it's not already */
|
|
|
|
io_poll_execute(req, 0);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the waitqueue is being freed early but someone is already
|
|
|
|
* holds ownership over it, we have to tear down the request as
|
|
|
|
* best we can. That means immediately removing the request from
|
|
|
|
* its waitqueue and preventing all further accesses to the
|
|
|
|
* waitqueue via the request.
|
|
|
|
*/
|
|
|
|
list_del_init(&poll->wait.entry);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Careful: this *must* be the last step, since as soon
|
|
|
|
* as req->head is NULL'ed out, the request can be
|
|
|
|
* completed and freed, since aio_poll_complete_work()
|
|
|
|
* will no longer need to take the waitqueue lock.
|
|
|
|
*/
|
|
|
|
smp_store_release(&poll->head, NULL);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2022-05-26 02:31:09 +00:00
|
|
|
static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
|
|
|
|
void *key)
|
|
|
|
{
|
|
|
|
struct io_kiocb *req = wqe_to_req(wait);
|
|
|
|
struct io_poll *poll = container_of(wait, struct io_poll, wait);
|
|
|
|
__poll_t mask = key_to_poll(key);
|
|
|
|
|
2022-06-21 20:34:15 +00:00
|
|
|
if (unlikely(mask & POLLFREE))
|
|
|
|
return io_pollfree_wake(req, poll);
|
2022-05-26 02:31:09 +00:00
|
|
|
|
|
|
|
/* for instances that support it check for an event match first */
|
|
|
|
if (mask && !(mask & (poll->events & ~IO_ASYNC_POLL_COMMON)))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (io_poll_get_ownership(req)) {
|
2022-11-20 17:18:45 +00:00
|
|
|
/*
|
|
|
|
* If we trigger a multishot poll off our own wakeup path,
|
|
|
|
* disable multishot as there is a circular dependency between
|
|
|
|
* CQ posting and triggering the event.
|
|
|
|
*/
|
|
|
|
if (mask & EPOLL_URING_WAKE)
|
|
|
|
poll->events |= EPOLLONESHOT;
|
|
|
|
|
2022-05-26 02:31:09 +00:00
|
|
|
/* optional, saves extra locking for removal in tw handler */
|
|
|
|
if (mask && poll->events & EPOLLONESHOT) {
|
|
|
|
list_del_init(&poll->wait.entry);
|
|
|
|
poll->head = NULL;
|
|
|
|
if (wqe_is_double(wait))
|
|
|
|
req->flags &= ~REQ_F_DOUBLE_POLL;
|
|
|
|
else
|
|
|
|
req->flags &= ~REQ_F_SINGLE_POLL;
|
|
|
|
}
|
2022-06-23 13:24:45 +00:00
|
|
|
__io_poll_execute(req, mask);
|
2022-05-26 02:31:09 +00:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2022-11-11 16:51:29 +00:00
|
|
|
/* fails only when polling is already completing by the first entry */
|
|
|
|
static bool io_poll_double_prepare(struct io_kiocb *req)
|
2022-06-23 13:24:49 +00:00
|
|
|
{
|
|
|
|
struct wait_queue_head *head;
|
|
|
|
struct io_poll *poll = io_poll_get_single(req);
|
|
|
|
|
|
|
|
/* head is RCU protected, see io_poll_remove_entries() comments */
|
|
|
|
rcu_read_lock();
|
|
|
|
head = smp_load_acquire(&poll->head);
|
2022-07-07 14:13:14 +00:00
|
|
|
/*
|
2022-11-11 16:51:29 +00:00
|
|
|
* poll arm might not hold ownership and so race for req->flags with
|
|
|
|
* io_poll_wake(). There is only one poll entry queued, serialise with
|
|
|
|
* it by taking its head lock. As we're still arming the tw hanlder
|
|
|
|
* is not going to be run, so there are no races with it.
|
2022-07-07 14:13:14 +00:00
|
|
|
*/
|
2022-11-11 16:51:29 +00:00
|
|
|
if (head) {
|
2022-06-23 13:24:49 +00:00
|
|
|
spin_lock_irq(&head->lock);
|
2022-11-11 16:51:29 +00:00
|
|
|
req->flags |= REQ_F_DOUBLE_POLL;
|
|
|
|
if (req->opcode == IORING_OP_POLL_ADD)
|
|
|
|
req->flags |= REQ_F_ASYNC_DATA;
|
2022-06-23 13:24:49 +00:00
|
|
|
spin_unlock_irq(&head->lock);
|
2022-11-11 16:51:29 +00:00
|
|
|
}
|
2022-06-23 13:24:49 +00:00
|
|
|
rcu_read_unlock();
|
2022-11-11 16:51:29 +00:00
|
|
|
return !!head;
|
2022-06-23 13:24:49 +00:00
|
|
|
}
|
|
|
|
|
2022-05-26 02:31:09 +00:00
|
|
|
static void __io_queue_proc(struct io_poll *poll, struct io_poll_table *pt,
|
|
|
|
struct wait_queue_head *head,
|
|
|
|
struct io_poll **poll_ptr)
|
|
|
|
{
|
|
|
|
struct io_kiocb *req = pt->req;
|
|
|
|
unsigned long wqe_private = (unsigned long) req;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The file being polled uses multiple waitqueues for poll handling
|
|
|
|
* (e.g. one for read, one for write). Setup a separate io_poll
|
|
|
|
* if this happens.
|
|
|
|
*/
|
|
|
|
if (unlikely(pt->nr_entries)) {
|
|
|
|
struct io_poll *first = poll;
|
|
|
|
|
|
|
|
/* double add on the same waitqueue head, ignore */
|
|
|
|
if (first->head == head)
|
|
|
|
return;
|
|
|
|
/* already have a 2nd entry, fail a third attempt */
|
|
|
|
if (*poll_ptr) {
|
|
|
|
if ((*poll_ptr)->head == head)
|
|
|
|
return;
|
|
|
|
pt->error = -EINVAL;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
poll = kmalloc(sizeof(*poll), GFP_ATOMIC);
|
|
|
|
if (!poll) {
|
|
|
|
pt->error = -ENOMEM;
|
|
|
|
return;
|
|
|
|
}
|
2022-06-23 13:24:49 +00:00
|
|
|
|
2022-05-26 02:31:09 +00:00
|
|
|
/* mark as double wq entry */
|
2022-06-23 13:24:44 +00:00
|
|
|
wqe_private |= IO_WQE_F_DOUBLE;
|
2023-02-27 16:41:20 +00:00
|
|
|
io_init_poll_iocb(poll, first->events);
|
2022-11-11 16:51:29 +00:00
|
|
|
if (!io_poll_double_prepare(req)) {
|
|
|
|
/* the request is completing, just back off */
|
|
|
|
kfree(poll);
|
|
|
|
return;
|
|
|
|
}
|
2022-05-26 02:31:09 +00:00
|
|
|
*poll_ptr = poll;
|
2022-06-23 13:24:49 +00:00
|
|
|
} else {
|
|
|
|
/* fine to modify, there is no poll queued to race with us */
|
|
|
|
req->flags |= REQ_F_SINGLE_POLL;
|
2022-05-26 02:31:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pt->nr_entries++;
|
|
|
|
poll->head = head;
|
|
|
|
poll->wait.private = (void *) wqe_private;
|
|
|
|
|
2023-12-13 15:58:15 +00:00
|
|
|
if (poll->events & EPOLLEXCLUSIVE) {
|
2022-05-26 02:31:09 +00:00
|
|
|
add_wait_queue_exclusive(head, &poll->wait);
|
2023-12-13 15:58:15 +00:00
|
|
|
} else {
|
2022-05-26 02:31:09 +00:00
|
|
|
add_wait_queue(head, &poll->wait);
|
2023-12-13 15:58:15 +00:00
|
|
|
}
|
2022-05-26 02:31:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
|
|
|
|
struct poll_table_struct *p)
|
|
|
|
{
|
|
|
|
struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
|
2022-08-11 07:11:15 +00:00
|
|
|
struct io_poll *poll = io_kiocb_to_cmd(pt->req, struct io_poll);
|
2022-05-26 02:31:09 +00:00
|
|
|
|
|
|
|
__io_queue_proc(poll, pt, head,
|
|
|
|
(struct io_poll **) &pt->req->async_data);
|
|
|
|
}
|
|
|
|
|
2022-06-23 13:24:49 +00:00
|
|
|
static bool io_poll_can_finish_inline(struct io_kiocb *req,
|
|
|
|
struct io_poll_table *pt)
|
|
|
|
{
|
|
|
|
return pt->owning || io_poll_get_ownership(req);
|
|
|
|
}
|
|
|
|
|
2024-09-30 20:22:36 +00:00
|
|
|
static void io_poll_add_hash(struct io_kiocb *req, unsigned int issue_flags)
|
2023-01-09 21:46:10 +00:00
|
|
|
{
|
2024-09-30 20:22:36 +00:00
|
|
|
struct io_ring_ctx *ctx = req->ctx;
|
|
|
|
|
|
|
|
io_ring_submit_lock(ctx, issue_flags);
|
|
|
|
io_poll_req_insert(req);
|
|
|
|
io_ring_submit_unlock(ctx, issue_flags);
|
2023-01-09 21:46:10 +00:00
|
|
|
}
|
|
|
|
|
2022-06-23 13:24:48 +00:00
|
|
|
/*
|
|
|
|
* Returns 0 when it's handed over for polling. The caller owns the requests if
|
|
|
|
* it returns non-zero, but otherwise should not touch it. Negative values
|
|
|
|
* contain an error code. When the result is >0, the polling has completed
|
|
|
|
* inline and ipt.result_mask is set to the mask.
|
|
|
|
*/
|
2022-05-26 02:31:09 +00:00
|
|
|
static int __io_arm_poll_handler(struct io_kiocb *req,
|
|
|
|
struct io_poll *poll,
|
2022-06-23 13:24:49 +00:00
|
|
|
struct io_poll_table *ipt, __poll_t mask,
|
|
|
|
unsigned issue_flags)
|
2022-05-26 02:31:09 +00:00
|
|
|
{
|
|
|
|
INIT_HLIST_NODE(&req->hash_node);
|
2023-02-27 16:41:20 +00:00
|
|
|
io_init_poll_iocb(poll, mask);
|
2022-05-26 02:31:09 +00:00
|
|
|
poll->file = req->file;
|
|
|
|
req->apoll_events = poll->events;
|
|
|
|
|
|
|
|
ipt->pt._key = mask;
|
|
|
|
ipt->req = req;
|
|
|
|
ipt->error = 0;
|
|
|
|
ipt->nr_entries = 0;
|
|
|
|
/*
|
2022-06-23 13:24:49 +00:00
|
|
|
* Polling is either completed here or via task_work, so if we're in the
|
|
|
|
* task context we're naturally serialised with tw by merit of running
|
|
|
|
* the same task. When it's io-wq, take the ownership to prevent tw
|
|
|
|
* from running. However, when we're in the task context, skip taking
|
|
|
|
* it as an optimisation.
|
|
|
|
*
|
|
|
|
* Note: even though the request won't be completed/freed, without
|
|
|
|
* ownership we still can race with io_poll_wake().
|
|
|
|
* io_poll_can_finish_inline() tries to deal with that.
|
2022-05-26 02:31:09 +00:00
|
|
|
*/
|
2022-06-23 13:24:49 +00:00
|
|
|
ipt->owning = issue_flags & IO_URING_F_UNLOCKED;
|
|
|
|
atomic_set(&req->poll_refs, (int)ipt->owning);
|
2022-07-07 14:13:17 +00:00
|
|
|
|
2024-03-07 18:06:32 +00:00
|
|
|
/*
|
|
|
|
* Exclusive waits may only wake a limited amount of entries
|
|
|
|
* rather than all of them, this may interfere with lazy
|
|
|
|
* wake if someone does wait(events > 1). Ensure we don't do
|
|
|
|
* lazy wake for those, as we need to process each one as they
|
|
|
|
* come in.
|
|
|
|
*/
|
|
|
|
if (poll->events & EPOLLEXCLUSIVE)
|
|
|
|
req->flags |= REQ_F_POLL_NO_LAZY;
|
|
|
|
|
2022-05-26 02:31:09 +00:00
|
|
|
mask = vfs_poll(req->file, &ipt->pt) & poll->events;
|
|
|
|
|
2022-06-23 13:24:48 +00:00
|
|
|
if (unlikely(ipt->error || !ipt->nr_entries)) {
|
|
|
|
io_poll_remove_entries(req);
|
|
|
|
|
2022-06-23 13:24:49 +00:00
|
|
|
if (!io_poll_can_finish_inline(req, ipt)) {
|
|
|
|
io_poll_mark_cancelled(req);
|
|
|
|
return 0;
|
|
|
|
} else if (mask && (poll->events & EPOLLET)) {
|
2022-06-23 13:24:48 +00:00
|
|
|
ipt->result_mask = mask;
|
|
|
|
return 1;
|
|
|
|
}
|
2022-06-23 13:24:49 +00:00
|
|
|
return ipt->error ?: -EINVAL;
|
2022-06-23 13:24:48 +00:00
|
|
|
}
|
|
|
|
|
2022-05-27 16:55:07 +00:00
|
|
|
if (mask &&
|
|
|
|
((poll->events & (EPOLLET|EPOLLONESHOT)) == (EPOLLET|EPOLLONESHOT))) {
|
2023-01-09 21:46:10 +00:00
|
|
|
if (!io_poll_can_finish_inline(req, ipt)) {
|
2024-09-30 20:22:36 +00:00
|
|
|
io_poll_add_hash(req, issue_flags);
|
2022-06-23 13:24:49 +00:00
|
|
|
return 0;
|
2023-01-09 21:46:10 +00:00
|
|
|
}
|
2022-05-26 02:31:09 +00:00
|
|
|
io_poll_remove_entries(req);
|
2022-06-23 13:24:47 +00:00
|
|
|
ipt->result_mask = mask;
|
2022-05-26 02:31:09 +00:00
|
|
|
/* no one else has access to the req, forget about the ref */
|
2022-06-23 13:24:47 +00:00
|
|
|
return 1;
|
2022-05-26 02:31:09 +00:00
|
|
|
}
|
2022-05-27 16:55:07 +00:00
|
|
|
|
2024-09-30 20:22:36 +00:00
|
|
|
io_poll_add_hash(req, issue_flags);
|
2022-05-26 02:31:09 +00:00
|
|
|
|
2022-06-23 13:24:49 +00:00
|
|
|
if (mask && (poll->events & EPOLLET) &&
|
|
|
|
io_poll_can_finish_inline(req, ipt)) {
|
2022-06-23 13:24:45 +00:00
|
|
|
__io_poll_execute(req, mask);
|
2022-05-26 02:31:09 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2023-06-08 16:38:36 +00:00
|
|
|
io_napi_add(req);
|
2022-05-26 02:31:09 +00:00
|
|
|
|
2022-06-23 13:24:49 +00:00
|
|
|
if (ipt->owning) {
|
|
|
|
/*
|
2022-11-20 16:57:41 +00:00
|
|
|
* Try to release ownership. If we see a change of state, e.g.
|
|
|
|
* poll was waken up, queue up a tw, it'll deal with it.
|
2022-06-23 13:24:49 +00:00
|
|
|
*/
|
2022-11-20 16:57:41 +00:00
|
|
|
if (atomic_cmpxchg(&req->poll_refs, 1, 0) != 1)
|
2022-06-23 13:24:49 +00:00
|
|
|
__io_poll_execute(req, 0);
|
|
|
|
}
|
2022-05-26 02:31:09 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void io_async_queue_proc(struct file *file, struct wait_queue_head *head,
|
|
|
|
struct poll_table_struct *p)
|
|
|
|
{
|
|
|
|
struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
|
|
|
|
struct async_poll *apoll = pt->req->apoll;
|
|
|
|
|
|
|
|
__io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll);
|
|
|
|
}
|
|
|
|
|
io_uring/poll: allow some retries for poll triggering spuriously
If we get woken spuriously when polling and fail the operation with
-EAGAIN again, then we generally only allow polling again if data
had been transferred at some point. This is indicated with
REQ_F_PARTIAL_IO. However, if the spurious poll triggers when the socket
was originally empty, then we haven't transferred data yet and we will
fail the poll re-arm. This either punts the socket to io-wq if it's
blocking, or it fails the request with -EAGAIN if not. Neither condition
is desirable, as the former will slow things down, while the latter
will make the application confused.
We want to ensure that a repeated poll trigger doesn't lead to infinite
work making no progress, that's what the REQ_F_PARTIAL_IO check was
for. But it doesn't protect against a loop post the first receive, and
it's unnecessarily strict if we started out with an empty socket.
Add a somewhat random retry count, just to put an upper limit on the
potential number of retries that will be done. This should be high enough
that we won't really hit it in practice, unless something needs to be
aborted anyway.
Cc: stable@vger.kernel.org # v5.10+
Link: https://github.com/axboe/liburing/issues/364
Signed-off-by: Jens Axboe <axboe@kernel.dk>
2023-02-25 19:53:53 +00:00
|
|
|
/*
|
|
|
|
* We can't reliably detect loops in repeated poll triggers and issue
|
|
|
|
* subsequently failing. But rather than fail these immediately, allow a
|
|
|
|
* certain amount of retries before we give up. Given that this condition
|
|
|
|
* should _rarely_ trigger even once, we should be fine with a larger value.
|
|
|
|
*/
|
|
|
|
#define APOLL_MAX_RETRY 128
|
|
|
|
|
2022-06-23 13:24:46 +00:00
|
|
|
static struct async_poll *io_req_alloc_apoll(struct io_kiocb *req,
|
|
|
|
unsigned issue_flags)
|
|
|
|
{
|
|
|
|
struct io_ring_ctx *ctx = req->ctx;
|
|
|
|
struct async_poll *apoll;
|
|
|
|
|
|
|
|
if (req->flags & REQ_F_POLLED) {
|
|
|
|
apoll = req->apoll;
|
|
|
|
kfree(apoll->double_poll);
|
2022-11-02 08:25:03 +00:00
|
|
|
} else if (!(issue_flags & IO_URING_F_UNLOCKED)) {
|
2024-03-20 21:19:44 +00:00
|
|
|
apoll = io_alloc_cache_get(&ctx->apoll_cache);
|
|
|
|
if (!apoll)
|
2022-11-02 08:25:03 +00:00
|
|
|
goto alloc_apoll;
|
io_uring/poll: allow some retries for poll triggering spuriously
If we get woken spuriously when polling and fail the operation with
-EAGAIN again, then we generally only allow polling again if data
had been transferred at some point. This is indicated with
REQ_F_PARTIAL_IO. However, if the spurious poll triggers when the socket
was originally empty, then we haven't transferred data yet and we will
fail the poll re-arm. This either punts the socket to io-wq if it's
blocking, or it fails the request with -EAGAIN if not. Neither condition
is desirable, as the former will slow things down, while the latter
will make the application confused.
We want to ensure that a repeated poll trigger doesn't lead to infinite
work making no progress, that's what the REQ_F_PARTIAL_IO check was
for. But it doesn't protect against a loop post the first receive, and
it's unnecessarily strict if we started out with an empty socket.
Add a somewhat random retry count, just to put an upper limit on the
potential number of retries that will be done. This should be high enough
that we won't really hit it in practice, unless something needs to be
aborted anyway.
Cc: stable@vger.kernel.org # v5.10+
Link: https://github.com/axboe/liburing/issues/364
Signed-off-by: Jens Axboe <axboe@kernel.dk>
2023-02-25 19:53:53 +00:00
|
|
|
apoll->poll.retries = APOLL_MAX_RETRY;
|
2022-06-23 13:24:46 +00:00
|
|
|
} else {
|
2022-11-02 08:25:03 +00:00
|
|
|
alloc_apoll:
|
2022-06-23 13:24:46 +00:00
|
|
|
apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC);
|
|
|
|
if (unlikely(!apoll))
|
|
|
|
return NULL;
|
io_uring/poll: allow some retries for poll triggering spuriously
If we get woken spuriously when polling and fail the operation with
-EAGAIN again, then we generally only allow polling again if data
had been transferred at some point. This is indicated with
REQ_F_PARTIAL_IO. However, if the spurious poll triggers when the socket
was originally empty, then we haven't transferred data yet and we will
fail the poll re-arm. This either punts the socket to io-wq if it's
blocking, or it fails the request with -EAGAIN if not. Neither condition
is desirable, as the former will slow things down, while the latter
will make the application confused.
We want to ensure that a repeated poll trigger doesn't lead to infinite
work making no progress, that's what the REQ_F_PARTIAL_IO check was
for. But it doesn't protect against a loop post the first receive, and
it's unnecessarily strict if we started out with an empty socket.
Add a somewhat random retry count, just to put an upper limit on the
potential number of retries that will be done. This should be high enough
that we won't really hit it in practice, unless something needs to be
aborted anyway.
Cc: stable@vger.kernel.org # v5.10+
Link: https://github.com/axboe/liburing/issues/364
Signed-off-by: Jens Axboe <axboe@kernel.dk>
2023-02-25 19:53:53 +00:00
|
|
|
apoll->poll.retries = APOLL_MAX_RETRY;
|
2022-06-23 13:24:46 +00:00
|
|
|
}
|
|
|
|
apoll->double_poll = NULL;
|
|
|
|
req->apoll = apoll;
|
io_uring/poll: allow some retries for poll triggering spuriously
If we get woken spuriously when polling and fail the operation with
-EAGAIN again, then we generally only allow polling again if data
had been transferred at some point. This is indicated with
REQ_F_PARTIAL_IO. However, if the spurious poll triggers when the socket
was originally empty, then we haven't transferred data yet and we will
fail the poll re-arm. This either punts the socket to io-wq if it's
blocking, or it fails the request with -EAGAIN if not. Neither condition
is desirable, as the former will slow things down, while the latter
will make the application confused.
We want to ensure that a repeated poll trigger doesn't lead to infinite
work making no progress, that's what the REQ_F_PARTIAL_IO check was
for. But it doesn't protect against a loop post the first receive, and
it's unnecessarily strict if we started out with an empty socket.
Add a somewhat random retry count, just to put an upper limit on the
potential number of retries that will be done. This should be high enough
that we won't really hit it in practice, unless something needs to be
aborted anyway.
Cc: stable@vger.kernel.org # v5.10+
Link: https://github.com/axboe/liburing/issues/364
Signed-off-by: Jens Axboe <axboe@kernel.dk>
2023-02-25 19:53:53 +00:00
|
|
|
if (unlikely(!--apoll->poll.retries))
|
|
|
|
return NULL;
|
2022-06-23 13:24:46 +00:00
|
|
|
return apoll;
|
|
|
|
}
|
|
|
|
|
2022-05-26 02:31:09 +00:00
|
|
|
int io_arm_poll_handler(struct io_kiocb *req, unsigned issue_flags)
|
|
|
|
{
|
2023-01-12 14:44:10 +00:00
|
|
|
const struct io_issue_def *def = &io_issue_defs[req->opcode];
|
2022-05-26 02:31:09 +00:00
|
|
|
struct async_poll *apoll;
|
|
|
|
struct io_poll_table ipt;
|
2022-05-27 16:55:07 +00:00
|
|
|
__poll_t mask = POLLPRI | POLLERR | EPOLLET;
|
2022-05-26 02:31:09 +00:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (!def->pollin && !def->pollout)
|
|
|
|
return IO_APOLL_ABORTED;
|
2024-01-29 03:08:24 +00:00
|
|
|
if (!io_file_can_poll(req))
|
2022-05-26 02:31:09 +00:00
|
|
|
return IO_APOLL_ABORTED;
|
|
|
|
if (!(req->flags & REQ_F_APOLL_MULTISHOT))
|
|
|
|
mask |= EPOLLONESHOT;
|
|
|
|
|
|
|
|
if (def->pollin) {
|
|
|
|
mask |= EPOLLIN | EPOLLRDNORM;
|
|
|
|
|
|
|
|
/* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */
|
|
|
|
if (req->flags & REQ_F_CLEAR_POLLIN)
|
|
|
|
mask &= ~EPOLLIN;
|
|
|
|
} else {
|
|
|
|
mask |= EPOLLOUT | EPOLLWRNORM;
|
|
|
|
}
|
|
|
|
if (def->poll_exclusive)
|
|
|
|
mask |= EPOLLEXCLUSIVE;
|
2022-06-23 13:24:46 +00:00
|
|
|
|
|
|
|
apoll = io_req_alloc_apoll(req, issue_flags);
|
|
|
|
if (!apoll)
|
|
|
|
return IO_APOLL_ABORTED;
|
2023-03-28 01:56:18 +00:00
|
|
|
req->flags &= ~(REQ_F_SINGLE_POLL | REQ_F_DOUBLE_POLL);
|
2022-05-26 02:31:09 +00:00
|
|
|
req->flags |= REQ_F_POLLED;
|
|
|
|
ipt.pt._qproc = io_async_queue_proc;
|
|
|
|
|
|
|
|
io_kbuf_recycle(req, issue_flags);
|
|
|
|
|
2022-06-23 13:24:49 +00:00
|
|
|
ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask, issue_flags);
|
2022-06-23 13:24:48 +00:00
|
|
|
if (ret)
|
|
|
|
return ret > 0 ? IO_APOLL_READY : IO_APOLL_ABORTED;
|
2022-06-16 12:57:20 +00:00
|
|
|
trace_io_uring_poll_arm(req, mask, apoll->poll.events);
|
2022-05-26 02:31:09 +00:00
|
|
|
return IO_APOLL_OK;
|
|
|
|
}
|
|
|
|
|
2024-09-30 20:22:36 +00:00
|
|
|
/*
|
|
|
|
* Returns true if we found and killed one or more poll requests
|
|
|
|
*/
|
2024-11-03 17:22:43 +00:00
|
|
|
__cold bool io_poll_remove_all(struct io_ring_ctx *ctx, struct io_uring_task *tctx,
|
2024-09-30 20:22:36 +00:00
|
|
|
bool cancel_all)
|
2022-05-26 02:31:09 +00:00
|
|
|
{
|
2024-09-30 20:22:36 +00:00
|
|
|
unsigned nr_buckets = 1U << ctx->cancel_table.hash_bits;
|
2022-05-26 02:31:09 +00:00
|
|
|
struct hlist_node *tmp;
|
|
|
|
struct io_kiocb *req;
|
|
|
|
bool found = false;
|
|
|
|
int i;
|
|
|
|
|
2024-09-30 20:22:36 +00:00
|
|
|
lockdep_assert_held(&ctx->uring_lock);
|
|
|
|
|
2022-06-16 09:22:10 +00:00
|
|
|
for (i = 0; i < nr_buckets; i++) {
|
2024-09-30 20:22:36 +00:00
|
|
|
struct io_hash_bucket *hb = &ctx->cancel_table.hbs[i];
|
2022-05-26 02:31:09 +00:00
|
|
|
|
2022-06-16 09:22:02 +00:00
|
|
|
hlist_for_each_entry_safe(req, tmp, &hb->list, hash_node) {
|
2024-11-03 17:22:43 +00:00
|
|
|
if (io_match_task_safe(req, tctx, cancel_all)) {
|
2022-05-26 02:31:09 +00:00
|
|
|
hlist_del_init(&req->hash_node);
|
|
|
|
io_poll_cancel_req(req);
|
|
|
|
found = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct io_kiocb *io_poll_find(struct io_ring_ctx *ctx, bool poll_only,
|
2024-09-30 20:29:06 +00:00
|
|
|
struct io_cancel_data *cd)
|
2022-05-26 02:31:09 +00:00
|
|
|
{
|
|
|
|
struct io_kiocb *req;
|
2024-09-30 20:22:36 +00:00
|
|
|
u32 index = hash_long(cd->data, ctx->cancel_table.hash_bits);
|
|
|
|
struct io_hash_bucket *hb = &ctx->cancel_table.hbs[index];
|
2022-05-26 02:31:09 +00:00
|
|
|
|
2022-06-16 09:22:02 +00:00
|
|
|
hlist_for_each_entry(req, &hb->list, hash_node) {
|
2022-05-26 02:31:09 +00:00
|
|
|
if (cd->data != req->cqe.user_data)
|
|
|
|
continue;
|
|
|
|
if (poll_only && req->opcode != IORING_OP_POLL_ADD)
|
|
|
|
continue;
|
|
|
|
if (cd->flags & IORING_ASYNC_CANCEL_ALL) {
|
2024-01-29 03:11:55 +00:00
|
|
|
if (io_cancel_match_sequence(req, cd->seq))
|
2022-05-26 02:31:09 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
return req;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct io_kiocb *io_poll_file_find(struct io_ring_ctx *ctx,
|
2024-09-30 20:29:06 +00:00
|
|
|
struct io_cancel_data *cd)
|
2022-05-26 02:31:09 +00:00
|
|
|
{
|
2024-09-30 20:22:36 +00:00
|
|
|
unsigned nr_buckets = 1U << ctx->cancel_table.hash_bits;
|
2022-05-26 02:31:09 +00:00
|
|
|
struct io_kiocb *req;
|
|
|
|
int i;
|
|
|
|
|
2022-06-16 09:22:10 +00:00
|
|
|
for (i = 0; i < nr_buckets; i++) {
|
2024-09-30 20:22:36 +00:00
|
|
|
struct io_hash_bucket *hb = &ctx->cancel_table.hbs[i];
|
2022-05-26 02:31:09 +00:00
|
|
|
|
2022-06-16 09:22:02 +00:00
|
|
|
hlist_for_each_entry(req, &hb->list, hash_node) {
|
2024-09-30 20:29:06 +00:00
|
|
|
if (io_cancel_req_match(req, cd))
|
2023-06-23 15:04:35 +00:00
|
|
|
return req;
|
2022-05-26 02:31:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2022-06-16 09:22:12 +00:00
|
|
|
static int io_poll_disarm(struct io_kiocb *req)
|
2022-05-26 02:31:09 +00:00
|
|
|
{
|
2022-06-16 09:22:12 +00:00
|
|
|
if (!req)
|
|
|
|
return -ENOENT;
|
2022-05-26 02:31:09 +00:00
|
|
|
if (!io_poll_get_ownership(req))
|
2022-06-16 09:22:12 +00:00
|
|
|
return -EALREADY;
|
2022-05-26 02:31:09 +00:00
|
|
|
io_poll_remove_entries(req);
|
|
|
|
hash_del(&req->hash_node);
|
2022-06-16 09:22:12 +00:00
|
|
|
return 0;
|
2022-05-26 02:31:09 +00:00
|
|
|
}
|
|
|
|
|
2024-09-30 20:22:36 +00:00
|
|
|
static int __io_poll_cancel(struct io_ring_ctx *ctx, struct io_cancel_data *cd)
|
2022-05-26 02:31:09 +00:00
|
|
|
{
|
|
|
|
struct io_kiocb *req;
|
|
|
|
|
2023-06-23 16:36:43 +00:00
|
|
|
if (cd->flags & (IORING_ASYNC_CANCEL_FD | IORING_ASYNC_CANCEL_OP |
|
|
|
|
IORING_ASYNC_CANCEL_ANY))
|
2024-09-30 20:29:06 +00:00
|
|
|
req = io_poll_file_find(ctx, cd);
|
2022-05-26 02:31:09 +00:00
|
|
|
else
|
2024-09-30 20:29:06 +00:00
|
|
|
req = io_poll_find(ctx, false, cd);
|
2022-06-16 09:22:03 +00:00
|
|
|
|
2024-09-30 20:29:06 +00:00
|
|
|
if (req) {
|
2022-06-16 09:22:03 +00:00
|
|
|
io_poll_cancel_req(req);
|
2024-09-30 20:29:06 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return -ENOENT;
|
2022-05-26 02:31:09 +00:00
|
|
|
}
|
|
|
|
|
2022-06-16 09:22:11 +00:00
|
|
|
int io_poll_cancel(struct io_ring_ctx *ctx, struct io_cancel_data *cd,
|
|
|
|
unsigned issue_flags)
|
2022-06-16 09:22:09 +00:00
|
|
|
{
|
2022-06-16 09:22:12 +00:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
io_ring_submit_lock(ctx, issue_flags);
|
2024-09-30 20:22:36 +00:00
|
|
|
ret = __io_poll_cancel(ctx, cd);
|
2022-06-16 09:22:12 +00:00
|
|
|
io_ring_submit_unlock(ctx, issue_flags);
|
|
|
|
return ret;
|
2022-06-16 09:22:09 +00:00
|
|
|
}
|
|
|
|
|
2022-05-26 02:31:09 +00:00
|
|
|
static __poll_t io_poll_parse_events(const struct io_uring_sqe *sqe,
|
|
|
|
unsigned int flags)
|
|
|
|
{
|
|
|
|
u32 events;
|
|
|
|
|
|
|
|
events = READ_ONCE(sqe->poll32_events);
|
|
|
|
#ifdef __BIG_ENDIAN
|
|
|
|
events = swahw32(events);
|
|
|
|
#endif
|
|
|
|
if (!(flags & IORING_POLL_ADD_MULTI))
|
|
|
|
events |= EPOLLONESHOT;
|
2022-05-27 16:55:07 +00:00
|
|
|
if (!(flags & IORING_POLL_ADD_LEVEL))
|
|
|
|
events |= EPOLLET;
|
|
|
|
return demangle_poll(events) |
|
|
|
|
(events & (EPOLLEXCLUSIVE|EPOLLONESHOT|EPOLLET));
|
2022-05-26 02:31:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int io_poll_remove_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
|
|
|
|
{
|
2022-08-11 07:11:15 +00:00
|
|
|
struct io_poll_update *upd = io_kiocb_to_cmd(req, struct io_poll_update);
|
2022-05-26 02:31:09 +00:00
|
|
|
u32 flags;
|
|
|
|
|
|
|
|
if (sqe->buf_index || sqe->splice_fd_in)
|
|
|
|
return -EINVAL;
|
|
|
|
flags = READ_ONCE(sqe->len);
|
|
|
|
if (flags & ~(IORING_POLL_UPDATE_EVENTS | IORING_POLL_UPDATE_USER_DATA |
|
|
|
|
IORING_POLL_ADD_MULTI))
|
|
|
|
return -EINVAL;
|
|
|
|
/* meaningless without update */
|
|
|
|
if (flags == IORING_POLL_ADD_MULTI)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
upd->old_user_data = READ_ONCE(sqe->addr);
|
|
|
|
upd->update_events = flags & IORING_POLL_UPDATE_EVENTS;
|
|
|
|
upd->update_user_data = flags & IORING_POLL_UPDATE_USER_DATA;
|
|
|
|
|
|
|
|
upd->new_user_data = READ_ONCE(sqe->off);
|
|
|
|
if (!upd->update_user_data && upd->new_user_data)
|
|
|
|
return -EINVAL;
|
|
|
|
if (upd->update_events)
|
|
|
|
upd->events = io_poll_parse_events(sqe, flags);
|
|
|
|
else if (sqe->poll32_events)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
|
|
|
|
{
|
2022-08-11 07:11:15 +00:00
|
|
|
struct io_poll *poll = io_kiocb_to_cmd(req, struct io_poll);
|
2022-05-26 02:31:09 +00:00
|
|
|
u32 flags;
|
|
|
|
|
|
|
|
if (sqe->buf_index || sqe->off || sqe->addr)
|
|
|
|
return -EINVAL;
|
|
|
|
flags = READ_ONCE(sqe->len);
|
2022-09-28 15:49:28 +00:00
|
|
|
if (flags & ~IORING_POLL_ADD_MULTI)
|
2022-05-26 02:31:09 +00:00
|
|
|
return -EINVAL;
|
|
|
|
if ((flags & IORING_POLL_ADD_MULTI) && (req->flags & REQ_F_CQE_SKIP))
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
poll->events = io_poll_parse_events(sqe, flags);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int io_poll_add(struct io_kiocb *req, unsigned int issue_flags)
|
|
|
|
{
|
2022-08-11 07:11:15 +00:00
|
|
|
struct io_poll *poll = io_kiocb_to_cmd(req, struct io_poll);
|
2022-05-26 02:31:09 +00:00
|
|
|
struct io_poll_table ipt;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ipt.pt._qproc = io_poll_queue_proc;
|
|
|
|
|
2022-06-23 13:24:49 +00:00
|
|
|
ret = __io_arm_poll_handler(req, poll, &ipt, poll->events, issue_flags);
|
2022-06-23 13:24:48 +00:00
|
|
|
if (ret > 0) {
|
2022-06-23 13:24:47 +00:00
|
|
|
io_req_set_res(req, ipt.result_mask, 0);
|
2022-05-26 02:31:09 +00:00
|
|
|
return IOU_OK;
|
|
|
|
}
|
2022-06-23 13:24:48 +00:00
|
|
|
return ret ?: IOU_ISSUE_SKIP_COMPLETE;
|
2022-05-26 02:31:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int io_poll_remove(struct io_kiocb *req, unsigned int issue_flags)
|
|
|
|
{
|
2022-08-11 07:11:15 +00:00
|
|
|
struct io_poll_update *poll_update = io_kiocb_to_cmd(req, struct io_poll_update);
|
2022-05-26 02:31:09 +00:00
|
|
|
struct io_ring_ctx *ctx = req->ctx;
|
2023-06-23 15:54:56 +00:00
|
|
|
struct io_cancel_data cd = { .ctx = ctx, .data = poll_update->old_user_data, };
|
2022-05-26 02:31:09 +00:00
|
|
|
struct io_kiocb *preq;
|
|
|
|
int ret2, ret = 0;
|
|
|
|
|
2023-06-18 01:50:24 +00:00
|
|
|
io_ring_submit_lock(ctx, issue_flags);
|
2024-09-30 20:29:06 +00:00
|
|
|
preq = io_poll_find(ctx, true, &cd);
|
2022-06-16 09:22:12 +00:00
|
|
|
ret2 = io_poll_disarm(preq);
|
|
|
|
if (ret2) {
|
|
|
|
ret = ret2;
|
2022-05-26 02:31:09 +00:00
|
|
|
goto out;
|
|
|
|
}
|
2022-06-20 00:26:01 +00:00
|
|
|
if (WARN_ON_ONCE(preq->opcode != IORING_OP_POLL_ADD)) {
|
|
|
|
ret = -EFAULT;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2022-05-26 02:31:09 +00:00
|
|
|
if (poll_update->update_events || poll_update->update_user_data) {
|
|
|
|
/* only mask one event flags, keep behavior flags */
|
|
|
|
if (poll_update->update_events) {
|
2022-08-11 07:11:15 +00:00
|
|
|
struct io_poll *poll = io_kiocb_to_cmd(preq, struct io_poll);
|
2022-05-26 02:31:09 +00:00
|
|
|
|
|
|
|
poll->events &= ~0xffff;
|
|
|
|
poll->events |= poll_update->events & 0xffff;
|
|
|
|
poll->events |= IO_POLL_UNMASK;
|
|
|
|
}
|
|
|
|
if (poll_update->update_user_data)
|
|
|
|
preq->cqe.user_data = poll_update->new_user_data;
|
|
|
|
|
2023-06-18 01:50:24 +00:00
|
|
|
ret2 = io_poll_add(preq, issue_flags & ~IO_URING_F_UNLOCKED);
|
2022-05-26 02:31:09 +00:00
|
|
|
/* successfully updated, don't complete poll request */
|
|
|
|
if (!ret2 || ret2 == -EIOCBQUEUED)
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
req_set_fail(preq);
|
|
|
|
io_req_set_res(preq, -ECANCELED, 0);
|
2024-03-15 15:29:51 +00:00
|
|
|
preq->io_task_work.func = io_req_task_complete;
|
|
|
|
io_req_task_work_add(preq);
|
2022-05-26 02:31:09 +00:00
|
|
|
out:
|
2023-06-18 01:50:24 +00:00
|
|
|
io_ring_submit_unlock(ctx, issue_flags);
|
2022-05-26 02:31:09 +00:00
|
|
|
if (ret < 0) {
|
|
|
|
req_set_fail(req);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
/* complete update request, we're done with it */
|
|
|
|
io_req_set_res(req, ret, 0);
|
|
|
|
return IOU_OK;
|
|
|
|
}
|