Skip to content

Commit 634578f

Browse files
isilenceaxboe
authored andcommitted
io_uring: fix racy IOPOLL flush overflow
It's not safe to call io_cqring_overflow_flush() for IOPOLL mode without hodling uring_lock, because it does synchronisation differently. Make sure we have it. As for io_ring_exit_work(), we don't even need it there because io_ring_ctx_wait_and_kill() already set force flag making all overflowed requests to be dropped. Cc: <[email protected]> # 5.5+ Signed-off-by: Pavel Begunkov <[email protected]> Signed-off-by: Jens Axboe <[email protected]>
1 parent 31bff9a commit 634578f

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

fs/io_uring.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8654,8 +8654,6 @@ static void io_ring_exit_work(struct work_struct *work)
86548654
* as nobody else will be looking for them.
86558655
*/
86568656
do {
8657-
if (ctx->rings)
8658-
io_cqring_overflow_flush(ctx, true, NULL, NULL);
86598657
io_iopoll_try_reap_events(ctx);
86608658
} while (!wait_for_completion_timeout(&ctx->ref_comp, HZ/20));
86618659
io_ring_ctx_free(ctx);
@@ -8665,6 +8663,8 @@ static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
86658663
{
86668664
mutex_lock(&ctx->uring_lock);
86678665
percpu_ref_kill(&ctx->refs);
8666+
if (ctx->rings)
8667+
io_cqring_overflow_flush(ctx, true, NULL, NULL);
86688668
mutex_unlock(&ctx->uring_lock);
86698669

86708670
io_kill_timeouts(ctx, NULL, NULL);
@@ -8674,8 +8674,6 @@ static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
86748674
io_wq_cancel_all(ctx->io_wq);
86758675

86768676
/* if we failed setting up the ctx, we might not have any rings */
8677-
if (ctx->rings)
8678-
io_cqring_overflow_flush(ctx, true, NULL, NULL);
86798677
io_iopoll_try_reap_events(ctx);
86808678
idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx);
86818679

@@ -8840,7 +8838,9 @@ static void io_uring_cancel_task_requests(struct io_ring_ctx *ctx,
88408838
}
88418839

88428840
io_cancel_defer_files(ctx, task, files);
8841+
io_ring_submit_lock(ctx, (ctx->flags & IORING_SETUP_IOPOLL));
88438842
io_cqring_overflow_flush(ctx, true, task, files);
8843+
io_ring_submit_unlock(ctx, (ctx->flags & IORING_SETUP_IOPOLL));
88448844

88458845
if (!files)
88468846
__io_uring_cancel_task_requests(ctx, task);
@@ -9172,8 +9172,10 @@ SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
91729172
*/
91739173
ret = 0;
91749174
if (ctx->flags & IORING_SETUP_SQPOLL) {
9175+
io_ring_submit_lock(ctx, (ctx->flags & IORING_SETUP_IOPOLL));
91759176
if (!list_empty_careful(&ctx->cq_overflow_list))
91769177
io_cqring_overflow_flush(ctx, false, NULL, NULL);
9178+
io_ring_submit_unlock(ctx, (ctx->flags & IORING_SETUP_IOPOLL));
91779179
if (flags & IORING_ENTER_SQ_WAKEUP)
91789180
wake_up(&ctx->sq_data->wait);
91799181
if (flags & IORING_ENTER_SQ_WAIT)

0 commit comments

Comments
 (0)