Skip to content

Commit 5b7aa38

Browse files
Hao Xuaxboe
authored andcommitted
io_uring: fix potential req refcount underflow
For multishot mode, there may be cases like: iowq original context io_poll_add _arm_poll() mask = vfs_poll() is not 0 if mask (2) io_poll_complete() compl_unlock (interruption happens tw queued to original context) io_poll_task_func() compl_lock (3) done = io_poll_complete() is true compl_unlock put req ref (1) if (poll->flags & EPOLLONESHOT) put req ref EPOLLONESHOT flag in (1) may be from (2) or (3), so there are multiple combinations that can cause ref underfow. Let's address it by: - check the return value in (2) as done - change (1) to if (done) in this way, we only do ref put in (1) if 'oneshot flag' is from (2) - do poll.done check in io_poll_task_func(), so that we won't put ref for the second time. Signed-off-by: Hao Xu <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jens Axboe <[email protected]>
1 parent a62682f commit 5b7aa38

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

fs/io_uring.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5367,6 +5367,10 @@ static void io_poll_task_func(struct io_kiocb *req, bool *locked)
53675367
} else {
53685368
bool done;
53695369

5370+
if (req->poll.done) {
5371+
spin_unlock(&ctx->completion_lock);
5372+
return;
5373+
}
53705374
done = __io_poll_complete(req, req->result);
53715375
if (done) {
53725376
io_poll_remove_double(req);
@@ -5830,6 +5834,7 @@ static int io_poll_add(struct io_kiocb *req, unsigned int issue_flags)
58305834
struct io_ring_ctx *ctx = req->ctx;
58315835
struct io_poll_table ipt;
58325836
__poll_t mask;
5837+
bool done;
58335838

58345839
ipt.pt._qproc = io_poll_queue_proc;
58355840

@@ -5838,13 +5843,13 @@ static int io_poll_add(struct io_kiocb *req, unsigned int issue_flags)
58385843

58395844
if (mask) { /* no async, we'd stolen it */
58405845
ipt.error = 0;
5841-
io_poll_complete(req, mask);
5846+
done = io_poll_complete(req, mask);
58425847
}
58435848
spin_unlock(&ctx->completion_lock);
58445849

58455850
if (mask) {
58465851
io_cqring_ev_posted(ctx);
5847-
if (poll->events & EPOLLONESHOT)
5852+
if (done)
58485853
io_put_req(req);
58495854
}
58505855
return ipt.error;

0 commit comments

Comments
 (0)