[PATCH] rnull: do not zero past the backing page

From: Vitaly Wool

Date: Fri Dec 04 2026 - 05:45:09 EST


When a read hits a backing page that was never written, rnull fills the
segment with zeroes up to the end of the segment's current page, which
is a page of the bio, not of the backing store. If the read does not
start at the beginning of the backing page, the zeroes run on into the
next backing page, which may hold data, and the read returns zeroes for
it. For instance, with 512 byte blocks, a direct read of 4K at sector 7
into a page aligned buffer, where only sector 8 was written, returns
zeroes for sector 8. Fix this.

Signed-off-by: Vitaly Wool <vitaly.wool@xxxxxxxxxxx>
Assisted-By: Claude Opus 5.5 <noreply@xxxxxxxxxxxxx>
---
drivers/block/rnull/rnull.rs | 26 +++++++++++++-------------
rust/kernel/block/bio/vec.rs | 10 +++++-----
2 files changed, 18 insertions(+), 18 deletions(-)

diff --git a/drivers/block/rnull/rnull.rs b/drivers/block/rnull/rnull.rs
index 9f99fe46826a..88bbd97845ee 100644
--- a/drivers/block/rnull/rnull.rs
+++ b/drivers/block/rnull/rnull.rs
@@ -707,19 +707,19 @@ fn read<'a, 'b, 'c>(
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: Casting from `usize` to `u64` never overflows.
- None => sector += bytes_to_sectors(segment.zero_page() as u64),
- }
+ // CAST: Page offset always fits in 32 bits.
+ let page_offset =
+ ((sector & u64::from(block::PAGE_SECTOR_MASK)) << block::SECTOR_SHIFT) as usize;
+
+ // Copy at most up to the end of the backing page: the next one may hold data even if
+ // this one does not.
+ let copied = match page {
+ Some(page) => segment.write(&page.page().as_slice()[page_offset..]),
+ None => segment.zero_page(PAGE_SIZE - page_offset),
+ };
+
+ // CAST: Casting from `usize` to `u64` never overflows.
+ sector += copied as u64 >> block::SECTOR_SHIFT;
}

Ok(())
diff --git a/rust/kernel/block/bio/vec.rs b/rust/kernel/block/bio/vec.rs
index 4d84d07b2cf2..b5bd2d0a461e 100644
--- a/rust/kernel/block/bio/vec.rs
+++ b/rust/kernel/block/bio/vec.rs
@@ -174,15 +174,15 @@ pub fn write(&mut self, src: &[u8]) -> usize {

/// Copy zeroes to the current page of this segment.
///
- /// Copies `PAGE_SIZE - (self.offset() % PAGE_SIZE` bytes of data to this
- /// segment starting at `self.offset()`. This call will advance offset and reduce length of
- /// `self`.
+ /// Copies at most `max_len` zero bytes to this segment starting at `self.offset()`, and never
+ /// past the end of the current page, i.e. at most `PAGE_SIZE - (self.offset() % PAGE_SIZE)`
+ /// bytes. This call will advance offset and reduce length of `self`.
///
/// Returns the number of bytes written to this segment.
#[inline]
- pub fn zero_page(&mut self) -> usize {
+ pub fn zero_page(&mut self, max_len: usize) -> usize {
let offset = self.offset() % PAGE_SIZE;
- let length = (PAGE_SIZE - offset).min(self.len() as usize);
+ let length = (PAGE_SIZE - offset).min(self.len() as usize).min(max_len);
let page_idx = self.offset() / PAGE_SIZE;

let dst_page = self.page(page_idx);
--
2.39.5