mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Mehtab Zafar <hello@mzfr.me>
To: Jens Axboe <axboe@kernel.dk>, io-uring@vger.kernel.org
Cc: Christian Brauner <brauner@kernel.org>,
	Al Viro <viro@zeniv.linux.org.uk>,
	linux-kernel@vger.kernel.org
Subject: [PATCH] io_uring/rw: complete partial writes from io-wq
Date: Sat, 28 Nov 2026 13:34:14 +0000	[thread overview]
Message-ID: <20261128133400.62759-1-hello@mzfr.me> (raw)

[-- Attachment #1: Type: text/plain, Size: 5344 bytes --]

IORING_OP_WRITE with an offset of -1 uses and advances the current file
position. A partial write is currently returned as -EAGAIN so the remainder
can be retried from io-wq.

That is only appropriate for the initial nonblocking attempt. If the
partial write already happened in an io-wq worker, returning -EAGAIN ends
the worker attempt, drops req->file, and reaches io_rw_fail(). The CQE is
fixed up to report the successful bytes, but kiocb_done() never updates
f_pos.

Only punt partial writes from the nonblocking submission path. A partial
write from a blocking worker then completes normally through kiocb_done(),
which updates f_pos and performs the usual write completion accounting.

Keep the last known-good position for a partial nonblocking write. If its
later retry fails through a normal completion path, commit that saved
position rather than trusting ki_pos from the failed write.

This bug was found with AI assistance, which also helped
prepare the fix, tests, a
nd description.

Fixes: 1939316bf988 ("io_uring: kiocb_done() should *not* trust ->ki_pos if ->{read,write}_iter() failed")
Cc: stable@vger.kernel.org
Signed-off-by: Mehtab Zafar <hello@mzfr.me>
---
Testing on x86_64 against axboe/for-next at e00ba7334fc2:
- Three fresh KVM boots per kernel, using identical userspace binaries
  and initramfs, with no guest disk or NIC.
- Baseline: a 100-byte current-position write limited to 10 bytes returned
  10 in the CQE but left f_pos at 0. Patched: CQE=10 and f_pos=10.
- Wholly failed writes remained -EFBIG with f_pos=0; complete writes
  remained successful with the expected position advance.
- liburing fpos.t and read-write.t passed on the patched kernel.
- A partial-read control returned 4096 bytes and advanced f_pos correctly
  on both kernels; no analogous read-side regression was reproduced.
- W=1 io_uring/rw.o and full bzImage builds passed. Strict checkpatch
  reported no errors, warnings, or checks; git diff --check was 
clean.

 io_uring/rw.c | 43 +++++++++++++++++++++++++++++++++----------
 io_uring/rw.h |  1 +
 2 files changed, 34 insertions(+), 10 deletions(-)

diff --git a/io_uring/rw.c b/io_uring/rw.c
index 3ecb842147a4..e658a9b2e1b4 100644
--- a/io_uring/rw.c
+++ b/io_uring/rw.c
@@ -200,6 +200,7 @@ static int io_rw_alloc_async(struct io_kiocb *req)
 	if (rw->vec.iovec)
 		io_req_set_flags(req, REQ_F_NEED_CLEANUP);
 	rw->bytes_done = 0;
+	rw->partial_pos = -1;
 	return 0;
 }
 
@@ -551,6 +552,24 @@ static void __io_complete_rw_common(struct io_kiocb *req, long res)
 	}
 }
 
+static inline void io_save_partial_pos(struct io_kiocb *req,
+				       struct kiocb *kiocb)
+{
+	struct io_async_rw *io = req->async_data;
+
+	if (req->flags & REQ_F_CUR_POS)
+		io->partial_pos = kiocb->ki_pos;
+}
+
+static inline void io_commit_partial_pos(struct io_kiocb *req, long res)
+{
+	struct io_async_rw *io = req->async_data;
+
+	if (res < 0 && req_has_async_data(req) && io->bytes_
done > 0 &&
+	    (req->flags & REQ_F_CUR_POS) && io->partial_pos >= 0)
+		req->file->f_pos = io->partial_pos;
+}
+
 static inline int io_fixup_rw_res(struct io_kiocb *req, long res)
 {
 	struct io_async_rw *io = req->async_data;
@@ -590,6 +609,8 @@ static void io_complete_rw(struct kiocb *kiocb, long res)
 		io_req_end_write(req);
 
 	__io_complete_rw_common(req, res);
+	if (!(req->flags & REQ_F_REISSUE))
+		io_commit_partial_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);
@@ -604,11 +625,14 @@ static void io_complete_rw_iopoll(struct kiocb *kiocb, long res)
 	if (kiocb->ki_flags & IOCB_WRITE)
 		io_req_end_write(req);
 
-	if (res == -EAGAIN && io_rw_should_reissue(req))
+	if (res == -EAGAIN && io_rw_should_reissue(req)) {
 		io_req_set_flags(req, REQ_F_REISSUE | REQ_F_BL_NO_RECYCLE);
-	else if (unlikely(final_res != req->cqe.res)) {
-		req_se
t_fail(req);
-		req->cqe.res = final_res;
+	} else {
+		io_commit_partial_pos(req, res);
+		if (unlikely(final_res != req->cqe.res)) {
+			req_set_fail(req);
+			req->cqe.res = final_res;
+		}
 	}
 
 	/* order with io_iopoll_complete() checking ->iopoll_completed */
@@ -1178,15 +1202,14 @@ int io_write(struct io_kiocb *req, unsigned int issue_flags)
 		if (ret2 == -EAGAIN && (req->flags & REQ_F_IOPOLL))
 			goto ret_eagain;
 
-		if (ret2 != req->cqe.res && ret2 >= 0 && need_complete_io(req)) {
+		if (force_nonblock && ret2 != req->cqe.res && ret2 >= 0 &&
+		    need_complete_io(req)) {
 			trace_io_uring_short_write(req->ctx, kiocb->ki_pos - ret2,
 						req->cqe.res, ret2);
 
-			/* This is a partial write. The file pos has already been
-			 * updated, setup the async struct to complete the request
-			 * in the worker. Also update bytes_done to account for
-			 * the bytes already written.
-			 */
+			/* Save the last known-good position before retrying. */

+			if (ret2 > 0)
+				io_save_partial_pos(req, kiocb);
 			iov_iter_save_state(&io->iter, &io->iter_state);
 			io->bytes_done += ret2;
 
diff --git a/io_uring/rw.h b/io_uring/rw.h
index 1179506f929f..63c53cd21286 100644
--- a/io_uring/rw.h
+++ b/io_uring/rw.h
@@ -12,6 +12,7 @@ struct io_meta_state {
 struct io_async_rw {
 	struct iou_vec			vec;
 	size_t				bytes_done;
+	loff_t				partial_pos;
 
 	struct_group(clear,
 		struct iov_iter			iter;
-- 
2.34.1


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 563 bytes --]

                 reply	other threads:[~2026-11-28 13:34 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261128133400.62759-1-hello@mzfr.me \
    --to=hello@mzfr.me \
    --cc=axboe@kernel.dk \
    --cc=brauner@kernel.org \
    --cc=io-uring@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=viro@zeniv.linux.org.uk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®