From d7e25fedf0e422cb2f4fd57faa0a6c80a14ac1f0 Mon Sep 17 00:00:00 2001 From: Satya Benson Date: Sat, 21 Nov 2026 07:30:52 -0500 Subject: [PATCH] aio: sleep as iowait while io_getevents() waits for completions A task waiting in io_getevents() for AIO completions sleeps in plain schedule(), so it never wakes with in_iowait set and schedutil's iowait boost never applies to it. Synchronous direct I/O waits in io_schedule() and io_uring sets in_iowait while the waiter has requests in flight ("turns out to be important for low QD IO"), so a libaio loop is the one common low queue depth path the boost misses. On a T6050 (M5 Max) the difference is large because an idle cluster drops to 300 MHz, and a task that sleeps most of the time keeps the utilization of its CPU too low for schedutil to raise the clock. 4 KiB O_DIRECT random reads at queue depth 1 (fio 3.43, ext4 on the internal ANS SSD), the fio job pinned to an S core at the default governor: ioengine IOPS mean latency libaio 11,146 87.4 us pvsync2 16,553 60.2 us (io_schedule(): boosted) posixaio 16,103 61.9 us (glibc helpers in pread(): boosted) libaio, cluster at top clock 16,885 59.0 us Use io_schedule() in read_events() when the context has a request in flight. Count accepted request slots until completion is posted or submission fails. This also covers I/O submitted by another thread sharing the context. An idle context uses schedule() so a parked event loop incurs no iowait accounting or iowait boost. Signed-off-by: Satya Benson --- fs/aio.c | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/fs/aio.c b/fs/aio.c index d78acc69f4870..77bc01fd01335 100644 --- a/fs/aio.c +++ b/fs/aio.c @@ -142,6 +142,9 @@ struct kioctx { * We batch accesses to it with a percpu version. */ atomic_t reqs_available; + + /* Requests whose completion has yet to be posted. */ + atomic_t reqs_in_flight; } ____cacheline_aligned_in_smp; struct { @@ -847,6 +850,7 @@ static struct kioctx *ioctx_alloc(unsigned nr_events) goto err; atomic_set(&ctx->reqs_available, ctx->nr_events - 1); + atomic_set(&ctx->reqs_in_flight, 0); ctx->req_batch = (ctx->nr_events - 1) / (num_possible_cpus() * 4); if (ctx->req_batch < 1) ctx->req_batch = 1; @@ -1118,6 +1122,7 @@ static inline struct aio_kiocb *aio_get_req(struct kioctx *ctx) } percpu_ref_get(&ctx->reqs); + atomic_inc(&ctx->reqs_in_flight); req->ki_ctx = ctx; INIT_LIST_HEAD(&req->ki_list); refcount_set(&req->ki_refcnt, 2); @@ -1206,6 +1211,7 @@ static void aio_complete(struct aio_kiocb *iocb) /* after flagging the request as done, we * must never even look at it again */ + atomic_dec(&ctx->reqs_in_flight); smp_wmb(); /* make event visible before updating tail */ ctx->tail = tail; @@ -1408,8 +1414,17 @@ static long read_events(struct kioctx *ctx, long min_nr, long nr, if (aio_read_events(ctx, min_nr, nr, event, &ret) || ret2) break; - if (nr_got == ret) - schedule(); + if (nr_got == ret) { + /* + * Mark the wait as iowait when this context has a + * request in flight, even if another thread submitted + * it. An idle event loop sleeps normally. + */ + if (atomic_read(&ctx->reqs_in_flight)) + io_schedule(); + else + schedule(); + } } finish_wait(&ctx->wait, &w.w); @@ -2114,6 +2129,7 @@ static int io_submit_one(struct kioctx *ctx, struct iocb __user *user_iocb, * means that we need to destroy req ourselves. */ if (unlikely(err)) { + atomic_dec(&ctx->reqs_in_flight); iocb_destroy(req); put_reqs_available(ctx, 1); }