Skip to content

Commit d4e7cd3

Browse files
committed
io_uring: sanitize double poll handling
There's a bit of confusion on the matching pairs of poll vs double poll, depending on if the request is a pure poll (IORING_OP_POLL_ADD) or poll driven retry. Add io_poll_get_double() that returns the double poll waitqueue, if any, and io_poll_get_single() that returns the original poll waitqueue. With that, remove the argument to io_poll_remove_double(). Finally ensure that wait->private is cleared once the double poll handler has run, so that remove knows it's already been seen. Cc: [email protected] # v5.8 Reported-by: [email protected] Fixes: 18bceab ("io_uring: allow POLL_ADD with double poll_wait() users") Signed-off-by: Jens Axboe <[email protected]>
1 parent 227c0c9 commit d4e7cd3

File tree

1 file changed

+25
-9
lines changed

1 file changed

+25
-9
lines changed

fs/io_uring.c

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4649,9 +4649,24 @@ static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll)
46494649
return false;
46504650
}
46514651

4652-
static void io_poll_remove_double(struct io_kiocb *req, void *data)
4652+
static struct io_poll_iocb *io_poll_get_double(struct io_kiocb *req)
46534653
{
4654-
struct io_poll_iocb *poll = data;
4654+
/* pure poll stashes this in ->io, poll driven retry elsewhere */
4655+
if (req->opcode == IORING_OP_POLL_ADD)
4656+
return (struct io_poll_iocb *) req->io;
4657+
return req->apoll->double_poll;
4658+
}
4659+
4660+
static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req)
4661+
{
4662+
if (req->opcode == IORING_OP_POLL_ADD)
4663+
return &req->poll;
4664+
return &req->apoll->poll;
4665+
}
4666+
4667+
static void io_poll_remove_double(struct io_kiocb *req)
4668+
{
4669+
struct io_poll_iocb *poll = io_poll_get_double(req);
46554670

46564671
lockdep_assert_held(&req->ctx->completion_lock);
46574672

@@ -4671,7 +4686,7 @@ static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
46714686
{
46724687
struct io_ring_ctx *ctx = req->ctx;
46734688

4674-
io_poll_remove_double(req, req->io);
4689+
io_poll_remove_double(req);
46754690
req->poll.done = true;
46764691
io_cqring_fill_event(req, error ? error : mangle_poll(mask));
46774692
io_commit_cqring(ctx);
@@ -4711,7 +4726,7 @@ static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode,
47114726
int sync, void *key)
47124727
{
47134728
struct io_kiocb *req = wait->private;
4714-
struct io_poll_iocb *poll = req->apoll->double_poll;
4729+
struct io_poll_iocb *poll = io_poll_get_single(req);
47154730
__poll_t mask = key_to_poll(key);
47164731

47174732
/* for instances that support it check for an event match first: */
@@ -4725,6 +4740,8 @@ static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode,
47254740
done = list_empty(&poll->wait.entry);
47264741
if (!done)
47274742
list_del_init(&poll->wait.entry);
4743+
/* make sure double remove sees this as being gone */
4744+
wait->private = NULL;
47284745
spin_unlock(&poll->head->lock);
47294746
if (!done)
47304747
__io_async_wake(req, poll, mask, io_poll_task_func);
@@ -4808,7 +4825,7 @@ static void io_async_task_func(struct callback_head *cb)
48084825
if (hash_hashed(&req->hash_node))
48094826
hash_del(&req->hash_node);
48104827

4811-
io_poll_remove_double(req, apoll->double_poll);
4828+
io_poll_remove_double(req);
48124829
spin_unlock_irq(&ctx->completion_lock);
48134830

48144831
if (!READ_ONCE(apoll->poll.canceled))
@@ -4919,7 +4936,7 @@ static bool io_arm_poll_handler(struct io_kiocb *req)
49194936
ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask,
49204937
io_async_wake);
49214938
if (ret || ipt.error) {
4922-
io_poll_remove_double(req, apoll->double_poll);
4939+
io_poll_remove_double(req);
49234940
spin_unlock_irq(&ctx->completion_lock);
49244941
kfree(apoll->double_poll);
49254942
kfree(apoll);
@@ -4951,14 +4968,13 @@ static bool io_poll_remove_one(struct io_kiocb *req)
49514968
{
49524969
bool do_complete;
49534970

4971+
io_poll_remove_double(req);
4972+
49544973
if (req->opcode == IORING_OP_POLL_ADD) {
4955-
io_poll_remove_double(req, req->io);
49564974
do_complete = __io_poll_remove_one(req, &req->poll);
49574975
} else {
49584976
struct async_poll *apoll = req->apoll;
49594977

4960-
io_poll_remove_double(req, apoll->double_poll);
4961-
49624978
/* non-poll requests have submit ref still */
49634979
do_complete = __io_poll_remove_one(req, &apoll->poll);
49644980
if (do_complete) {

0 commit comments

Comments
 (0)