From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=198.137.202.133 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796027766; b=OKA5DdmiEy1Cr4BEVrv0+2C/Pb9PbehimE2jJ5cfjW3Ynzu7ycrCIMlXqyvSlicmk79C OEWrhDwbYZYy2YVxkLn5bcwRnn9287TFy+pLR2qoQ7u0kRZM/olcfbVR7bEYO1w6M+HNQ pB4X7G36/Oaqjh0vVV6sMruJFqhIHZ8D7rHtNv4mX3dT9WNiGAEqiYbNIJS7cPzhI7vFm vqVtvHuGyRy2aDYTsI7iyUgqqwUFzcvQeownRDU8OxPehqKLsi4taiDOEq2kaA5AR3vXK ADczbrpUp7yh51JL5GWLlm5ga97UBh7Oxkx7wB29BNpkKz4OM3TLDi33KJMbEujRjDw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796027766; h=DMARC-Filter:DKIM-Signature:Received:Date:From:To:Cc:Subject: Message-ID:References:MIME-Version:Content-Type:Content-Disposition: In-Reply-To:X-SRS-Rewrite; bh=wN7Z2hspARhLmGFl0sLXrQd5IlVUK3UB6GkpR0EyvK0=; b=S7Fd5degni8jl6I2XLFju7CvowGLDg7of7WDGglCFKQLYEFTiknDnWr7KIUFEu4TzcbZ ibnZapas66TyrQmbuBEaY7+Mh3zi/z1OHVCo8/Agzo28yHm7EOQaPCXAVOSDqsEqVTswi /k+mxj2WwG9fw/d3Mup+8Us15iAL5zzs1xTxFLNOAc8zGu1lQkrYLKFaEA2xjhRE4S63S 9utjQRt1H1qz1a0jUXjmnld/66JjOD97dSuXu9BUzm+UKI6B53V9OdrxUSMv88bm5Z9/S L6/cl40d0E4G7LxjcqIVOCyhkc0z/i6XCtlv33zO0JwTpK+cquGo02Z6qU3UZc6eVug== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=infradead.org; spf=none smtp.mailfrom=bombadil.srs.infradead.org; dkim=pass header.d=infradead.org header.i=@infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=qr3638sS; arc=none smtp.remote-ip=198.137.202.133 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6F61060A261C; Mon, 30 Nov 2026 08:36:05 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 6F61060A261C Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=infradead.org Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=bombadil.srs.infradead.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=qr3638sS DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20210309; h=In-Reply-To:Content-Type:MIME-Version :References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=wN7Z2hspARhLmGFl0sLXrQd5IlVUK3UB6GkpR0EyvK0=; b=qr3638sSESGT4er+pQbZetPsoj GAD/eXoJnaqoAnalIrMnhZ2iitXooHAH35Vm2D/ZZoz9a5MeLGYhuZQxnXCihkVxQK/oZK82lsMOB IdAXDk7DKMTt+yuifNvCvoapvVatonaQM1UVwXgaUMXrvrtFYytIChAuwHj879cQCSJiujY6BXDNv wxNxwDZRkTHgcwb591Sdvo6rNXD9f4mRC6FShY7nrnksqiU8ycKcvOB5USZ9Cat9ly7spA2CE2RGL LbWXRDUkMouC1hw+SGelSJUV0/n+zuTmCkc3PVdQjtnnYMAMA02ma15IF0oSGD+Y1BE809GVeCLdD dpSk84rQ==; Received: from hch by bombadil.infradead.org with local (Exim 4.100.1 #2 (Red Hat Linux)) id 1xXwrk-00000003mYD-31u0; Mon, 30 Nov 2026 08:36:04 +0000 Date: Mon, 30 Nov 2026 00:36:04 -0800 From: Christoph Hellwig To: Ville Baillie Cc: "axboe@kernel.dk" , "linux-block@vger.kernel.org" , "bvanassche@acm.org" , Christian Brauner , linux-fsdevel@vger.kernel.org Subject: Re: [PATCH v2] loop: retry short writes Message-ID: References: <20261129111351.2240027-1-ville@vmb-tech.com> X-Mailing-List: linux-block@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20261129111351.2240027-1-ville@vmb-tech.com> X-SRS-Rewrite: SMTP reverse-path rewritten from by bombadil.infradead.org. See http://www.infradead.org/rpr.html 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)