Linux block layer
 help / color / mirror / Atom feed
* [PATCH v2] loop: retry short writes
@ 2026-11-29 11:14 Ville Baillie
  2026-11-30  8:36 ` Christoph Hellwig
  0 siblings, 1 reply; 2+ messages in thread
From: Ville Baillie @ 2026-11-29 11:14 UTC (permalink / raw)
  To: axboe@kernel.dk
  Cc: linux-block@vger.kernel.org, bvanassche@acm.org, Ville Baillie

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

This can occur when the backing file limits the amount of data accepted
by a single write. For example, kernfs binary attributes limit writes to
PAGE_SIZE in kernfs_fop_write_iter().

This was observed after upgrading a vendor kernel from 6.6 to 6.18,
writes greater than PAGE_SIZE to a sysfs backed loop device would
silently fail, with only the first PAGE_SIZE bytes reaching the backing
file.

Commit bc07c10a3603 ("block: loop: support DIO & AIO") introduced an
asynchronous completion path that did not handle short reads/writes
correctly.

Commit f9de14bc7e7a ("loop: handle short DIO reads") added handling for
short reads, but did not address short writes.

Commit f2fed441c69b ("loop: stop using vfs_iter_{read,write} for
buffered I/O") changed buffered I/O to use the asynchronous path, where
short writes were incorrectly treated as successful.

This patch handles short writes in a similar way to short reads,
advancing the request and retrying the remaining data. Zero byte writes
will return BLK_STS_IOERR, and oversized or non-sector aligned
completions are now rejected for both reads and writes.

Fixes: bc07c10a3603 ("block: loop: support DIO & AIO")
Fixes: f2fed441c69b ("loop: stop using vfs_iter_{read,write} for buffered I/O")
Signed-off-by: Ville Baillie <ville@vmb-tech.com>
---
Changes in v2:
- Expand the commit description and add the direct I/O Fixes tag.
- Check negative completions first and simplify the early exits.
- Explain the alignment check, use IS_ALIGNED(), and warn on oversized
  completions.

Testing:
- Tested patch on x86-64 with kernel 9437ee8b6cf6 in a QEMU guest. Then
  tested that f2fed441c69b fails without the patch, but parent passes.
- Tested on aarch64 target that first highlighted the bug, this patch
  was applied on a 6.18 vendor kernel.
- Retested all of the above for v2.

 drivers/block/loop.c | 41 +++++++++++++++++++++++++++++------------
 1 file changed, 29 insertions(+), 12 deletions(-)

diff --git a/drivers/block/loop.c b/drivers/block/loop.c
index a3325ceab484..be396324be40 100644
--- a/drivers/block/loop.c
+++ b/drivers/block/loop.c
@@ -287,33 +287,50 @@ 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 < 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_rq_bytes(rq) ||
+	    (req_op(rq) != REQ_OP_READ && req_op(rq) != REQ_OP_WRITE))
+		goto end_io;
+
 	if (cmd->ret) {
+		/*
+		 * Short read/write: reject oversized and unaligned completions,
+		 * as blk_update_request() advances in sectors only.
+		 *
+		 * Warn once if the backing file returns more bytes than
+		 * requested.
+		 */
+		if (WARN_ON_ONCE(cmd->ret > blk_rq_bytes(rq)) ||
+		    !IS_ALIGNED(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);
-	} else {
+		return;
+	}
+
+	/*
+	 * 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;
+	ret = BLK_STS_IOERR;
 end_io:
-		blk_mq_end_request(rq, ret);
-	}
+	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

* Re: [PATCH v2] loop: retry short writes
  2026-11-29 11:14 [PATCH v2] loop: retry short writes Ville Baillie
@ 2026-11-30  8:36 ` Christoph Hellwig
  0 siblings, 0 replies; 2+ messages in thread
From: Christoph Hellwig @ 2026-11-30  8:36 UTC (permalink / raw)
  To: Ville Baillie
  Cc: axboe@kernel.dk, linux-block@vger.kernel.org, bvanassche@acm.org,
	Christian Brauner, linux-fsdevel

On Sun, Nov 29, 2026 at 11:14:16AM +0000, Ville Baillie wrote:
> The loop driver can receive short writes, but the current implementation
> treats these as successful, potentially resulting in silent data loss.

Receive short writes sound like the come from above.  Please reword
this sentence to state the problem more clearly.

> This can occur when the backing file limits the amount of data accepted
> by a single write. For example, kernfs binary attributes limit writes to
> PAGE_SIZE in kernfs_fop_write_iter().
> 
> This was observed after upgrading a vendor kernel from 6.6 to 6.18,
> writes greater than PAGE_SIZE to a sysfs backed loop device would
> silently fail, with only the first PAGE_SIZE bytes reaching the backing
> file.

Christian has an outstanding series to prohibit use of the loop driver
on pseudo file systems.  Adding him so that you can explain this use
case to him.

> - Tested patch on x86-64 with kernel 9437ee8b6cf6 in a QEMU guest. Then
>   tested that f2fed441c69b fails without the patch, but parent passes.
> - Tested on aarch64 target that first highlighted the bug, this patch
>   was applied on a 6.18 vendor kernel.
> - Retested all of the above for v2.

Btw, if we end up supporting loop on sysfs in the long run, we probably
want a testcase in blktests to exercise this.

> +	if (cmd->ret == blk_rq_bytes(rq) ||
> +	    (req_op(rq) != REQ_OP_READ && req_op(rq) != REQ_OP_WRITE))
> +		goto end_io;

This now has a branch and goto for the regular fast path, and
in general a very odd structure of the function.  See my alternative
suggestion with a helper for slow path below.

> +		if (WARN_ON_ONCE(cmd->ret > blk_rq_bytes(rq)) ||
> +		    !IS_ALIGNED(cmd->ret, SECTOR_SIZE)) {
> +			ret = BLK_STS_IOERR;
> +			goto end_io;
> +		}

This also needs the zeroing of the remaining data for reads.

diff --git a/drivers/block/loop.c b/drivers/block/loop.c
index a3325ceab484..0df56683fbd8 100644
--- a/drivers/block/loop.c
+++ b/drivers/block/loop.c
@@ -282,38 +282,54 @@ static int lo_req_flush(struct loop_device *lo, struct request *rq)
 	return ret;
 }
 
-static void lo_complete_rq(struct request *rq)
+/*
+ * Short read or write - if we got some data, advance our request and retry it.
+ * If we got no data, end the rest with EIO.
+ */
+static void lo_handle_short_io(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);
-		goto end_io;
-	}
+	if (!cmd->ret)
+		goto fail;
+	if (WARN_ON_ONCE(cmd->ret > blk_rq_bytes(rq)))
+		goto fail;
+	if (WARN_ON_ONCE(!IS_ALIGNED(cmd->ret, SECTOR_SIZE)))
+		goto fail;
 
-	/*
-	 * 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 {
+	blk_update_request(rq, BLK_STS_OK, cmd->ret);
+	cmd->ret = 0;
+	blk_mq_requeue_request(rq, true);
+	return;
+
+fail:
+	if (req_op(rq) == REQ_OP_READ) {
 		struct bio *bio = rq->bio;
 
 		while (bio) {
 			zero_fill_bio(bio);
 			bio = bio->bi_next;
 		}
+	}
+	blk_mq_end_request(rq, BLK_STS_IOERR);
+}
 
-		ret = BLK_STS_IOERR;
-end_io:
-		blk_mq_end_request(rq, ret);
+static void lo_complete_rq(struct request *rq)
+{
+	struct loop_cmd *cmd = blk_mq_rq_to_pdu(rq);
+
+	if (cmd->ret < 0) {
+		blk_mq_end_request(rq, errno_to_blk_status(cmd->ret));
+		return;
 	}
+
+	if (cmd->ret != blk_rq_bytes(rq) &&
+	    (req_op(rq) == REQ_OP_READ || req_op(rq) == REQ_OP_WRITE)) {
+		lo_handle_short_io(rq);
+		return;
+	}
+
+	blk_mq_end_request(rq, BLK_STS_OK);
 }
 
 static void lo_rw_aio_do_completion(struct loop_cmd *cmd)

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

end of thread, other threads:[~2026-11-30  8:36 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-29 11:14 [PATCH v2] loop: retry short writes Ville Baillie
2026-11-30  8:36 ` Christoph Hellwig

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