mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Peder Bergan via B4 Relay <devnull+peder.bergan.gmail.com@kernel.org>
To: Andreas Hindborg <a.hindborg@kernel.org>, Jens Axboe <axboe@kernel.dk>
Cc: Boqun Feng <boqun@kernel.org>,
	linux-block@vger.kernel.org,  rust-for-linux@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: [PATCH RFC 1/2] block: rnull: stop zero-filling at the backing page boundary
Date: Mon, 30 Nov 2026 10:31:33 +0100	[thread overview]
Message-ID: <20261130-fix-rnull-memory-backed-correctness-v1-1-ff16977610c0@gmail.com> (raw)
In-Reply-To: <20261130-fix-rnull-memory-backed-correctness-v1-0-ff16977610c0@gmail.com>

From: Peder Bergan <peder.bergan@gmail.com>

A sector-aligned read can start in an unwritten backing page and
continue into a resident page within the same bio segment. Filling
the segment page with zeroes can overwrite the part of the read
that should have come from the resident page.

Limit zero-filling to the current backing page, then look up the
next page. Advance the storage position by the sectors copied.

Fixes: 15484843cc92 ("block: rnull: add memory backing")
Signed-off-by: Peder Bergan <peder.bergan@gmail.com>
---
 drivers/block/rnull/rnull.rs | 27 +++++++++++++--------------
 1 file changed, 13 insertions(+), 14 deletions(-)

diff --git a/drivers/block/rnull/rnull.rs b/drivers/block/rnull/rnull.rs
index 9f99fe46826a1..0ce1c27b53db6 100644
--- a/drivers/block/rnull/rnull.rs
+++ b/drivers/block/rnull/rnull.rs
@@ -705,21 +705,20 @@ fn read<'a, 'b, 'c>(
         let access = self.storage.access(tree_guard, None);
 
         while !segment.at_end() {
-            let page = access.get_read_page(sector);
-
-            match page {
-                Some(page) => {
-                    // CAST: Page offset always fits in 32 bits.
-                    let page_offset = ((sector & u64::from(block::PAGE_SECTOR_MASK))
-                        << block::SECTOR_SHIFT) as usize;
-
-                    // CAST: Casting from `usize` to `u64` never overflows.
-                    sector += segment.write(&page.page().as_slice()[page_offset..]) as u64
-                        >> block::SECTOR_SHIFT;
+            // CAST: Page offset always fits in 32 bits.
+            let page_offset =
+                ((sector & u64::from(block::PAGE_SECTOR_MASK)) << SECTOR_SHIFT) as usize;
+            let page_remaining = PAGE_SIZE - page_offset;
+            let copied = match access.get_read_page(sector) {
+                Some(page) => segment.write(&page.page().as_slice()[page_offset..]),
+                None if segment.len() as usize > page_remaining => {
+                    static ZEROES: [u8; PAGE_SIZE] = [0; PAGE_SIZE];
+                    segment.write(&ZEROES[..page_remaining])
                 }
-                // CAST: Casting from `usize` to `u64` never overflows.
-                None => sector += bytes_to_sectors(segment.zero_page() as u64),
-            }
+                None => segment.zero_page(),
+            };
+            // CAST: A segment copy is no larger than a page.
+            sector += (copied as u64) >> SECTOR_SHIFT;
         }
 
         Ok(())

-- 
2.56.1



  reply	other threads:[~2026-11-30  9:31 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-30  9:31 [PATCH RFC 0/2] block: rnull: Fix hole reads and discard coverage Peder Bergan via B4 Relay
2026-11-30  9:31 ` Peder Bergan via B4 Relay [this message]
2026-11-30 11:22   ` [PATCH RFC 1/2] block: rnull: stop zero-filling at the backing page boundary Andreas Hindborg
2026-11-30  9:31 ` [PATCH RFC 2/2] block: rnull: track uncached discard coverage by sector Peder Bergan via B4 Relay
2026-11-30 12:15   ` Andreas Hindborg

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=20261130-fix-rnull-memory-backed-correctness-v1-1-ff16977610c0@gmail.com \
    --to=devnull+peder.bergan.gmail.com@kernel.org \
    --cc=a.hindborg@kernel.org \
    --cc=axboe@kernel.dk \
    --cc=boqun@kernel.org \
    --cc=linux-block@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=peder.bergan@gmail.com \
    --cc=rust-for-linux@vger.kernel.org \
    /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®