* [PATCH v3] loop: retry short writes
@ 2026-12-01 20:49 Ville Baillie
2026-12-02 6:25 ` hch
0 siblings, 1 reply; 2+ messages in thread
From: Ville Baillie @ 2026-12-01 20:49 UTC (permalink / raw)
To: axboe@kernel.dk
Cc: linux-block@vger.kernel.org, bvanassche@acm.org,
hch@infradead.org, christian@brauner.io, Ville Baillie
When writing to its backing file, the loop driver does not correctly
handle short writes. If the backing file writes fewer bytes than
requested, loop can incorrectly complete the entire block request
successfully, resulting in silent data loss.
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.
The patch moves short I/O handling to its own helper, and handles short
writes in a similar way to short reads, advancing the request and
retrying the remaining data.
Zero-length, oversized or non-sector-aligned completions result in
BLK_STS_IOERR. If a short read cannot be completed, the remaining read
data is zeroed before failing the request.
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 v3:
- Apply suggestions from v2 mailing list feedback.
- Remove the sysfs-specific use case and test with a FUSE-backed filesystem.
Testing:
- Tested on x86-64 using kernel 9437ee8b6cf6 in a QEMU guest.
- Confirmed that the bug is present with f2fed441c69b and absent in its parent.
- Used a FUSE-based filesystem implementing short writes to reproduce the issue.
drivers/block/loop.c | 56 ++++++++++++++++++++++++++++----------------
1 file changed, 36 insertions(+), 20 deletions(-)
diff --git a/drivers/block/loop.c b/drivers/block/loop.c
index a3325ceab484..c6dd42795054 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 get some data, advance our request and retry it.
+ * If we get no data, end the request with EIO and zero any remaining read data.
+ */
+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)
--
2.43.0
^ permalink raw reply related [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-12-02 6:25 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-01 20:49 [PATCH v3] loop: retry short writes Ville Baillie
2026-12-02 6:25 ` hch
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox