Skip to content

Commit bdcd3ea

Browse files
Xiaoguang Wangaxboe
authored andcommitted
io_uring: fix poll_list race for SETUP_IOPOLL|SETUP_SQPOLL
After making ext4 support iopoll method: let ext4_file_operations's iopoll method be iomap_dio_iopoll(), we found fio can easily hang in fio_ioring_getevents() with below fio job: rm -f testfile; sync; sudo fio -name=fiotest -filename=testfile -iodepth=128 -thread -rw=write -ioengine=io_uring -hipri=1 -sqthread_poll=1 -direct=1 -bs=4k -size=10G -numjobs=8 -runtime=2000 -group_reporting with IORING_SETUP_SQPOLL and IORING_SETUP_IOPOLL enabled. There are two issues that results in this hang, one reason is that when IORING_SETUP_SQPOLL and IORING_SETUP_IOPOLL are enabled, fio does not use io_uring_enter to get completed events, it relies on kernel io_sq_thread to poll for completed events. Another reason is that there is a race: when io_submit_sqes() in io_sq_thread() submits a batch of sqes, variable 'inflight' will record the number of submitted reqs, then io_sq_thread will poll for reqs which have been added to poll_list. But note, if some previous reqs have been punted to io worker, these reqs will won't be in poll_list timely. io_sq_thread() will only poll for a part of previous submitted reqs, and then find poll_list is empty, reset variable 'inflight' to be zero. If app just waits these deferred reqs and does not wake up io_sq_thread again, then hang happens. For app that entirely relies on io_sq_thread to poll completed requests, let io_iopoll_req_issued() wake up io_sq_thread properly when adding new element to poll_list, and when io_sq_thread prepares to sleep, check whether poll_list is empty again, if not empty, continue to poll. Signed-off-by: Xiaoguang Wang <[email protected]> Signed-off-by: Jens Axboe <[email protected]>
1 parent 41726c9 commit bdcd3ea

File tree

1 file changed

+27
-32
lines changed

1 file changed

+27
-32
lines changed

fs/io_uring.c

Lines changed: 27 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1821,6 +1821,10 @@ static void io_iopoll_req_issued(struct io_kiocb *req)
18211821
list_add(&req->list, &ctx->poll_list);
18221822
else
18231823
list_add_tail(&req->list, &ctx->poll_list);
1824+
1825+
if ((ctx->flags & IORING_SETUP_SQPOLL) &&
1826+
wq_has_sleeper(&ctx->sqo_wait))
1827+
wake_up(&ctx->sqo_wait);
18241828
}
18251829

18261830
static void io_file_put(struct io_submit_state *state)
@@ -5086,49 +5090,28 @@ static int io_sq_thread(void *data)
50865090
const struct cred *old_cred;
50875091
mm_segment_t old_fs;
50885092
DEFINE_WAIT(wait);
5089-
unsigned inflight;
50905093
unsigned long timeout;
5091-
int ret;
5094+
int ret = 0;
50925095

50935096
complete(&ctx->completions[1]);
50945097

50955098
old_fs = get_fs();
50965099
set_fs(USER_DS);
50975100
old_cred = override_creds(ctx->creds);
50985101

5099-
ret = timeout = inflight = 0;
5102+
timeout = jiffies + ctx->sq_thread_idle;
51005103
while (!kthread_should_park()) {
51015104
unsigned int to_submit;
51025105

5103-
if (inflight) {
5106+
if (!list_empty(&ctx->poll_list)) {
51045107
unsigned nr_events = 0;
51055108

5106-
if (ctx->flags & IORING_SETUP_IOPOLL) {
5107-
/*
5108-
* inflight is the count of the maximum possible
5109-
* entries we submitted, but it can be smaller
5110-
* if we dropped some of them. If we don't have
5111-
* poll entries available, then we know that we
5112-
* have nothing left to poll for. Reset the
5113-
* inflight count to zero in that case.
5114-
*/
5115-
mutex_lock(&ctx->uring_lock);
5116-
if (!list_empty(&ctx->poll_list))
5117-
io_iopoll_getevents(ctx, &nr_events, 0);
5118-
else
5119-
inflight = 0;
5120-
mutex_unlock(&ctx->uring_lock);
5121-
} else {
5122-
/*
5123-
* Normal IO, just pretend everything completed.
5124-
* We don't have to poll completions for that.
5125-
*/
5126-
nr_events = inflight;
5127-
}
5128-
5129-
inflight -= nr_events;
5130-
if (!inflight)
5109+
mutex_lock(&ctx->uring_lock);
5110+
if (!list_empty(&ctx->poll_list))
5111+
io_iopoll_getevents(ctx, &nr_events, 0);
5112+
else
51315113
timeout = jiffies + ctx->sq_thread_idle;
5114+
mutex_unlock(&ctx->uring_lock);
51325115
}
51335116

51345117
to_submit = io_sqring_entries(ctx);
@@ -5157,7 +5140,7 @@ static int io_sq_thread(void *data)
51575140
* more IO, we should wait for the application to
51585141
* reap events and wake us up.
51595142
*/
5160-
if (inflight ||
5143+
if (!list_empty(&ctx->poll_list) ||
51615144
(!time_after(jiffies, timeout) && ret != -EBUSY &&
51625145
!percpu_ref_is_dying(&ctx->refs))) {
51635146
cond_resched();
@@ -5167,6 +5150,19 @@ static int io_sq_thread(void *data)
51675150
prepare_to_wait(&ctx->sqo_wait, &wait,
51685151
TASK_INTERRUPTIBLE);
51695152

5153+
/*
5154+
* While doing polled IO, before going to sleep, we need
5155+
* to check if there are new reqs added to poll_list, it
5156+
* is because reqs may have been punted to io worker and
5157+
* will be added to poll_list later, hence check the
5158+
* poll_list again.
5159+
*/
5160+
if ((ctx->flags & IORING_SETUP_IOPOLL) &&
5161+
!list_empty_careful(&ctx->poll_list)) {
5162+
finish_wait(&ctx->sqo_wait, &wait);
5163+
continue;
5164+
}
5165+
51705166
/* Tell userspace we may need a wakeup call */
51715167
ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
51725168
/* make sure to read SQ tail after writing flags */
@@ -5194,8 +5190,7 @@ static int io_sq_thread(void *data)
51945190
mutex_lock(&ctx->uring_lock);
51955191
ret = io_submit_sqes(ctx, to_submit, NULL, -1, &cur_mm, true);
51965192
mutex_unlock(&ctx->uring_lock);
5197-
if (ret > 0)
5198-
inflight += ret;
5193+
timeout = jiffies + ctx->sq_thread_idle;
51995194
}
52005195

52015196
set_fs(old_fs);

0 commit comments

Comments
 (0)