Linux block layer
 help / color / mirror / Atom feed
* [PATCH 0/1] loop: retry short writes
@ 2026-11-28 15:20 Ville Baillie
  2026-11-28 15:20 ` [PATCH 1/1] " Ville Baillie
  0 siblings, 1 reply; 2+ messages in thread
From: Ville Baillie @ 2026-11-28 15:20 UTC (permalink / raw)
  To: axboe@kernel.dk
  Cc: ming.lei@redhat.com, linux-block@vger.kernel.org, Ville Baillie

From: Ville Baillie <ville@vmb-tech.com>

This fixes a silent data-loss issue uncovered when using a sysfs
attribute as the backing file.

I was able to reproduce the issue with writes larger than 4094 through
the loop device. The write would report success but bytes after 4096
would be lost.

The issue was noticed when moving from 6.6 to 6.18.

This patch handles short writes in the loop driver by retrying the
remaining data.

The bug was reproduced on real hardware and with an emulator. This patch
was tested on both, and with the emulator it was proven that the parent
of the "fixes" commit also passed.

Ville Baillie (1):
  loop: retry short writes

 drivers/block/loop.c | 54 ++++++++++++++++++++++++++++----------------
 1 file changed, 35 insertions(+), 19 deletions(-)

-- 
2.43.0


^ permalink raw reply	[flat|nested] 2+ messages in thread

* [PATCH 1/1] loop: retry short writes
  2026-11-28 15:20 [PATCH 0/1] loop: retry short writes Ville Baillie
@ 2026-11-28 15:20 ` Ville Baillie
  0 siblings, 0 replies; 2+ messages in thread
From: Ville Baillie @ 2026-11-28 15:20 UTC (permalink / raw)
  To: axboe@kernel.dk
  Cc: ming.lei@redhat.com, linux-block@vger.kernel.org, Ville Baillie

From: Ville Baillie <ville@vmb-tech.com>

The loop driver can receive short writes, but the current implementation
treats these as successful, potentially resulting in silent data loss.

Handle short writes in a similar way to short reads, advancing the
request and retrying the remaining data.

Fixes: f2fed441c69b ("loop: stop using vfs_iter_{read,write} for buffered I/O")
Signed-off-by: Ville Baillie <ville@vmb-tech.com>
---
 drivers/block/loop.c | 54 ++++++++++++++++++++++++++++----------------
 1 file changed, 35 insertions(+), 19 deletions(-)

diff --git a/drivers/block/loop.c b/drivers/block/loop.c
index a3325ceab484..3dc6965912e1 100644
--- a/drivers/block/loop.c
+++ b/drivers/block/loop.c
@@ -287,33 +287,49 @@ static void lo_complete_rq(struct request *rq)
 	struct loop_cmd *cmd = blk_mq_rq_to_pdu(rq);
 	blk_status_t ret = BLK_STS_OK;
 
-	if (cmd->ret < 0 || cmd->ret == blk_rq_bytes(rq) ||
-	    req_op(rq) != REQ_OP_READ) {
-		if (cmd->ret < 0)
-			ret = errno_to_blk_status(cmd->ret);
+	if (cmd->ret == blk_rq_bytes(rq))
+		goto end_io;
+
+	if (cmd->ret < 0) {
+		ret = errno_to_blk_status(cmd->ret);
 		goto end_io;
 	}
 
-	/*
-	 * Short READ - if we got some data, advance our request and
-	 * retry it. If we got no data, end the rest with EIO.
-	 */
-	if (cmd->ret) {
-		blk_update_request(rq, BLK_STS_OK, cmd->ret);
-		cmd->ret = 0;
-		blk_mq_requeue_request(rq, true);
-	} else {
-		struct bio *bio = rq->bio;
+	if (req_op(rq) == REQ_OP_READ || req_op(rq) == REQ_OP_WRITE) {
+		if (cmd->ret) {
+			/*
+			 * Short read/write: reject oversized completions,
+			 * otherwise advance the request and retry it.
+			 */
+			if (cmd->ret > blk_rq_bytes(rq) ||
+			    cmd->ret % SECTOR_SIZE) {
+				ret = BLK_STS_IOERR;
+				goto end_io;
+			}
+			blk_update_request(rq, BLK_STS_OK, cmd->ret);
+			cmd->ret = 0;
+			blk_mq_requeue_request(rq, true);
+			return;
+		}
 
-		while (bio) {
-			zero_fill_bio(bio);
-			bio = bio->bi_next;
+		/*
+		 * Zero return on read/write: on read fill the buffer with
+		 * zeros, then end the request with an error.
+		 */
+		if (req_op(rq) == REQ_OP_READ) {
+			struct bio *bio = rq->bio;
+
+			while (bio) {
+				zero_fill_bio(bio);
+				bio = bio->bi_next;
+			}
 		}
 
 		ret = BLK_STS_IOERR;
-end_io:
-		blk_mq_end_request(rq, ret);
 	}
+
+end_io:
+	blk_mq_end_request(rq, ret);
 }
 
 static void lo_rw_aio_do_completion(struct loop_cmd *cmd)
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-28 15:20 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-28 15:20 [PATCH 0/1] loop: retry short writes Ville Baillie
2026-11-28 15:20 ` [PATCH 1/1] " Ville Baillie

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox