Skip to content

Commit 724cb4f

Browse files
Hao Xuaxboe
authored andcommitted
io_uring: check sqring and iopoll_list before shedule
do this to avoid race below: userspace kernel | check sqring and iopoll_list submit sqe | check IORING_SQ_NEED_WAKEUP | (which is not set) | | | set IORING_SQ_NEED_WAKEUP wait cqe | schedule(never wakeup again) Signed-off-by: Hao Xu <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jens Axboe <[email protected]>
1 parent f2a48dd commit 724cb4f

File tree

1 file changed

+19
-17
lines changed

1 file changed

+19
-17
lines changed

fs/io_uring.c

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6839,27 +6839,29 @@ static int io_sq_thread(void *data)
68396839
continue;
68406840
}
68416841

6842-
needs_sched = true;
68436842
prepare_to_wait(&sqd->wait, &wait, TASK_INTERRUPTIBLE);
6844-
list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
6845-
if ((ctx->flags & IORING_SETUP_IOPOLL) &&
6846-
!list_empty_careful(&ctx->iopoll_list)) {
6847-
needs_sched = false;
6848-
break;
6849-
}
6850-
if (io_sqring_entries(ctx)) {
6851-
needs_sched = false;
6852-
break;
6853-
}
6854-
}
6855-
6856-
if (needs_sched && !test_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state)) {
6843+
if (!test_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state)) {
68576844
list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
68586845
io_ring_set_wakeup_flag(ctx);
68596846

6860-
mutex_unlock(&sqd->lock);
6861-
schedule();
6862-
mutex_lock(&sqd->lock);
6847+
needs_sched = true;
6848+
list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
6849+
if ((ctx->flags & IORING_SETUP_IOPOLL) &&
6850+
!list_empty_careful(&ctx->iopoll_list)) {
6851+
needs_sched = false;
6852+
break;
6853+
}
6854+
if (io_sqring_entries(ctx)) {
6855+
needs_sched = false;
6856+
break;
6857+
}
6858+
}
6859+
6860+
if (needs_sched) {
6861+
mutex_unlock(&sqd->lock);
6862+
schedule();
6863+
mutex_lock(&sqd->lock);
6864+
}
68636865
list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
68646866
io_ring_clear_wakeup_flag(ctx);
68656867
}

0 commit comments

Comments
 (0)