[PATCH] io_uring/rw: update the file position when an IO completes asynchronously

From: Hang Zhengyang

Date: Sun Dec 06 2026 - 04:34:55 EST


A read or write with an offset of -1 uses and advances the file
position, but kiocb_done() only writes ->ki_pos back to ->f_pos when the
IO completes inline. If ->read_iter() or ->write_iter() returns
-EIOCBQUEUED and the IO completes through ->ki_complete(), as O_DIRECT
on block devices and iomap filesystems can, ->f_pos is never updated.
Eight 4K O_DIRECT reads at offset -1 on ext4, issued one at a time, all
return the first block, and the file position stays at 0.

Write ->f_pos when the request completes, in io_req_rw_complete() and
when iopoll reaps it, not in ->ki_complete(), which can run in interrupt
context. As in kiocb_done(), a failed IO doesn't update it. A request
that will be reissued skips it, as the reissue writes it. So does a
completion run by a kthread, which only happens when the task or ring is
going away; io_uring must not touch files from kthread context [1].

Like kiocb_done(), this doesn't take f_pos_lock. Requests that complete
through ->ki_complete() now overwrite an lseek(2) done while they are in
flight.

Found, fixed and tested by Claude Code, I handled the review process.

Link: https://lore.kernel.org/all/CAHk-=wijcZGxrw8+aukW-m2YRGn5AUWfZsPSscez7w7_EqfuGQ@xxxxxxxxxxxxxx/ [1]
Fixes: ba04291eb66e ("io_uring: allow use of offset == -1 to mean file position")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Hang Zhengyang <hang.zhengyang1010@xxxxxxxxx>
---
Tested on for-7.5/io_uring in a KVM guest with three cases at offset -1:
eight 4K reads issued one at a time, the same reads as one linked chain,
and eight 4K writes issued one at a time. Each case ran with O_DIRECT on
ext4, on a loop device and on null_blk, with IOPOLL on null_blk with and
without IOSQE_ASYNC, and without O_DIRECT on 9p mounted without a cache.
On ext4 and 9p they also ran with IOSQE_ASYNC, and some with
DEFER_TASKRUN or with a short read at EOF. Without the patch all 32 runs
read or write the wrong blocks and leave the file position at 0. With it
all 32 pass. Buffered IO on tmpfs and ext4 completes inline and passes
either way. liburing's fpos.t fails on 9p without the patch ("f_pos
incorrect, expected 14336 have 0") and passes with it. iopoll.t,
read-write.t and fpos.t pass on ext4 on null_blk with poll queues, and
the full liburing suite shows no new failures. Not tested: an IO that
fails after -EIOCBQUEUED, a reissue, an iopoll request that completes
inline, SQPOLL, and completions that run in a kthread.

Mehtab Zafar's "io_uring/rw: complete partial writes from io-wq" [2]
changes io_complete_rw() and io_complete_rw_iopoll() too, so the two
patches conflict. Its io_commit_partial_pos() checks REQ_F_CUR_POS, so
when they are combined it has to run before io_rw_drop_pos(). I can
rebase on it if it goes in first.

The patch applies to 7.4-rc and 7.3.y but doesn't build there without
io_req_clear_flags() (3e5dd5a14521).

[2] https://lore.kernel.org/io-uring/20261128133400.62759-1-hello@xxxxxxx/

io_uring/rw.c | 40 ++++++++++++++++++++++++++++++++++++++--
1 file changed, 38 insertions(+), 2 deletions(-)

diff --git a/io_uring/rw.c b/io_uring/rw.c
index 3ecb842147a4..cba1e896ea92 100644
--- a/io_uring/rw.c
+++ b/io_uring/rw.c
@@ -551,6 +551,34 @@ static void __io_complete_rw_common(struct io_kiocb *req, long res)
}
}

+/*
+ * ->ki_pos can't be trusted if ->{read,write}_iter() failed, see
+ * kiocb_done(). Don't let a failed request update the file position.
+ */
+static inline void io_rw_drop_pos(struct io_kiocb *req, long res)
+{
+ if (unlikely(res < 0) && !(req->flags & REQ_F_REISSUE))
+ io_req_clear_flags(req, REQ_F_CUR_POS);
+}
+
+/*
+ * An IO that used and must advance the file position completed
+ * asynchronously, update ->f_pos like kiocb_done() does for inline
+ * completions. Not from a kthread: the file position must not be touched
+ * from kthread context, and a kthread only completes requests of a task or
+ * ring that is going away.
+ */
+static inline void io_rw_update_pos(struct io_kiocb *req)
+{
+ struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw);
+
+ if ((req->flags & (REQ_F_CUR_POS | REQ_F_REISSUE)) != REQ_F_CUR_POS)
+ return;
+ if (current->flags & PF_KTHREAD)
+ return;
+ req->file->f_pos = rw->kiocb.ki_pos;
+}
+
static inline int io_fixup_rw_res(struct io_kiocb *req, long res)
{
struct io_async_rw *io = req->async_data;
@@ -570,6 +598,7 @@ void io_req_rw_complete(struct io_tw_req tw_req, io_tw_token_t tw)
struct io_kiocb *req = tw_req.req;

io_req_io_notify(req);
+ io_rw_update_pos(req);

if (req->flags & (REQ_F_BUFFER_SELECTED|REQ_F_BUFFER_RING))
req->cqe.flags |= io_put_kbuf(req, max(req->cqe.res, 0), NULL);
@@ -590,6 +619,7 @@ static void io_complete_rw(struct kiocb *kiocb, long res)
io_req_end_write(req);

__io_complete_rw_common(req, res);
+ io_rw_drop_pos(req, res);
io_req_set_res(req, io_fixup_rw_res(req, res), 0);
req->io_task_work.func = io_req_rw_complete;
__io_req_task_work_add(req, IOU_F_TWQ_LAZY_WAKE);
@@ -610,6 +640,7 @@ static void io_complete_rw_iopoll(struct kiocb *kiocb, long res)
req_set_fail(req);
req->cqe.res = final_res;
}
+ io_rw_drop_pos(req, res);

/* order with io_iopoll_complete() checking ->iopoll_completed */
smp_store_release(&req->iopoll_completed, 1);
@@ -639,8 +670,11 @@ static int kiocb_done(struct io_kiocb *req, ssize_t ret,
struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw);
unsigned final_ret = io_fixup_rw_res(req, ret);

- if (ret >= 0 && req->flags & REQ_F_CUR_POS)
+ if (ret >= 0 && req->flags & REQ_F_CUR_POS) {
req->file->f_pos = rw->kiocb.ki_pos;
+ /* don't write it again when an iopoll request is reaped */
+ io_req_clear_flags(req, REQ_F_CUR_POS);
+ }
if (ret >= 0 && !(req->flags & REQ_F_IOPOLL)) {
u32 cflags = 0;

@@ -1377,8 +1411,10 @@ int io_do_iopoll(struct io_ring_ctx *ctx, bool force_nonspin)
wq_list_add_tail(&req->comp_list, &ctx->submit_state.compl_reqs);
nr_events++;
req->cqe.flags |= io_put_kbuf(req, max(req->cqe.res, 0), NULL);
- if (!io_is_uring_cmd(req))
+ if (!io_is_uring_cmd(req)) {
+ io_rw_update_pos(req);
io_req_rw_cleanup(req, 0);
+ }
}
if (nr_events)
__io_submit_flush_completions(ctx);

base-commit: 416f1d31a8acea8b45b5da50e78147b83ccf63a8
--
2.53.0