* [PATCH RFC 1/2] block: rnull: stop zero-filling at the backing page boundary
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
2026-11-30 11:22 ` Andreas Hindborg
2026-11-30 9:31 ` [PATCH RFC 2/2] block: rnull: track uncached discard coverage by sector Peder Bergan via B4 Relay
1 sibling, 1 reply; 5+ messages in thread
From: Peder Bergan via B4 Relay @ 2026-11-30 9:31 UTC (permalink / raw)
To: Andreas Hindborg, Jens Axboe
Cc: Boqun Feng, linux-block, rust-for-linux, linux-kernel
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
^ permalink raw reply [flat|nested] 5+ messages in thread* [PATCH RFC 2/2] block: rnull: track uncached discard coverage by sector
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 ` [PATCH RFC 1/2] block: rnull: stop zero-filling at the backing page boundary Peder Bergan via B4 Relay
@ 2026-11-30 9:31 ` Peder Bergan via B4 Relay
2026-11-30 12:15 ` Andreas Hindborg
1 sibling, 1 reply; 5+ messages in thread
From: Peder Bergan via B4 Relay @ 2026-11-30 9:31 UTC (permalink / raw)
To: Andreas Hindborg, Jens Axboe
Cc: Boqun Feng, linux-block, rust-for-linux, linux-kernel
From: Peder Bergan <peder.bergan@gmail.com>
A configfs device can retain its backing storage when it is
powered off and brought back with a different logical block
size. Block-based occupancy cannot then describe which sectors
a discard covers. A partial discard can drop a page with data
outside the requested range, or leave a fully discarded page
allocated.
Mark the sectors actually written to uncached pages. Step through
uncached discard ranges by backing page and free a page when
its final occupied sector is discarded.
Keep block-based occupancy for cached storage. Iterate cached
discard ranges in sectors, fixing a byte-to-sector conversion
that shifted in the wrong direction.
Fixes: b2e667324057 ("block: rnull: add volatile cache emulation")
Fixes: 7b0bda2a523b ("block: rnull: add zoned storage support")
Signed-off-by: Peder Bergan <peder.bergan@gmail.com>
---
drivers/block/rnull/disk_storage.rs | 36 +++++++++++++++++++++++---------
drivers/block/rnull/disk_storage/page.rs | 16 ++++++++++++++
drivers/block/rnull/rnull.rs | 12 ++++++++---
drivers/block/rnull/util.rs | 7 -------
4 files changed, 51 insertions(+), 20 deletions(-)
diff --git a/drivers/block/rnull/disk_storage.rs b/drivers/block/rnull/disk_storage.rs
index bb50a1c6cb9ee..2e71500cf26ac 100644
--- a/drivers/block/rnull/disk_storage.rs
+++ b/drivers/block/rnull/disk_storage.rs
@@ -1,6 +1,5 @@
// SPDX-License-Identifier: GPL-2.0
-use crate::util::*;
use core::pin::Pin;
use kernel::{
block,
@@ -63,18 +62,35 @@ pub(crate) fn lock(&self) -> SpinLockGuard<'_, Pin<KBox<TreeContainer>>> {
self.trees.lock()
}
- pub(crate) fn discard(&self, mut sector: u64, sectors: u32) {
+ pub(crate) fn discard(&self, sector: u64, sectors: u32) {
+ let end = sector + u64::from(sectors);
let tree_guard = self.lock();
- let mut cache_guard = tree_guard.cache_tree.lock();
- let mut disk_guard = tree_guard.disk_tree.lock();
- let mut remaining_bytes = sectors_to_bytes(sectors);
+ if self.cache_enabled() {
+ let mut cache_guard = tree_guard.cache_tree.lock();
+ let mut disk_guard = tree_guard.disk_tree.lock();
+ let stride = (self.block_size >> block::SECTOR_SHIFT) as usize;
+ for sector in (sector..end).step_by(stride) {
+ self.free_sector(&mut cache_guard, &mut disk_guard, sector);
+ }
+ } else {
+ let mut disk_guard = tree_guard.disk_tree.lock();
+ Self::discard_uncached(&mut disk_guard, sector, end);
+ }
+ }
- while remaining_bytes > 0 {
- self.free_sector(&mut cache_guard, &mut disk_guard, sector);
- let processed = remaining_bytes.min(self.block_size);
- sector += Into::<u64>::into(bytes_to_sectors(processed));
- remaining_bytes -= processed;
+ fn discard_uncached(tree: &mut xarray::MutGuard<'_, TreeNode>, mut sector: u64, end: u64) {
+ while sector < end {
+ let offset = sector & u64::from(block::PAGE_SECTOR_MASK);
+ let count = (end - sector).min(u64::from(PAGE_SECTORS) - offset) as u32;
+ let index = DiskStorageAccess::to_index(sector);
+ if let Some(page) = tree.get_mut(index) {
+ page.free_sectors(sector, count);
+ if page.is_empty() {
+ tree.remove(index);
+ }
+ }
+ sector += u64::from(count);
}
}
diff --git a/drivers/block/rnull/disk_storage/page.rs b/drivers/block/rnull/disk_storage/page.rs
index 1b21b44dd2a37..091573e602db7 100644
--- a/drivers/block/rnull/disk_storage/page.rs
+++ b/drivers/block/rnull/disk_storage/page.rs
@@ -47,6 +47,22 @@ pub(crate) fn set_occupied(&mut self, sector: u64) {
self.status |= 1 << idx;
}
+ fn sector_mask(sector: u64, count: u32) -> u64 {
+ if count == 0 {
+ return 0;
+ }
+ let offset = sector & u64::from(PAGE_SECTOR_MASK);
+ (u64::MAX >> (u64::BITS - count)) << offset
+ }
+
+ pub(crate) fn set_occupied_sectors(&mut self, sector: u64, count: u32) {
+ self.status |= Self::sector_mask(sector, count);
+ }
+
+ pub(crate) fn free_sectors(&mut self, sector: u64, count: u32) {
+ self.status &= !Self::sector_mask(sector, count);
+ }
+
pub(crate) fn set_free(&mut self, sector: u64) {
let idx = sector & u64::from(PAGE_SECTOR_MASK);
self.status &= !(1 << idx);
diff --git a/drivers/block/rnull/rnull.rs b/drivers/block/rnull/rnull.rs
index 0ce1c27b53db6..39484b9286a46 100644
--- a/drivers/block/rnull/rnull.rs
+++ b/drivers/block/rnull/rnull.rs
@@ -653,6 +653,7 @@ fn write<'a, 'b, 'c>(
bypass_cache: bool,
) -> Result {
let mut sheaf: Option<XArraySheaf<'_>> = None;
+ let cached = self.storage.cache_enabled();
while !segment.at_end() {
// TODO: Maybe try the write without a sheaf. If it fails, allocate and fill a sheaf and retry.
@@ -668,7 +669,6 @@ fn write<'a, 'b, 'c>(
}
let page = access.get_write_page(sector, bypass_cache)?;
- page.set_occupied(sector);
// TODO: use utility functions instead of shifting.
// CAST: Page offset always fits in 32 bits.
@@ -680,8 +680,14 @@ fn write<'a, 'b, 'c>(
let page_end = (page_offset + block_size).min(PAGE_SIZE);
let dst = &mut page.page_mut().as_pin_mut().as_mut_slice()[page_offset..page_end];
- // CAST: Casting from `usize` to `u64` never overflows.
- sector += segment.read(dst) as u64 >> block::SECTOR_SHIFT;
+ // CAST: A segment copy is no larger than a page.
+ let written = (segment.read(dst) as u32) >> SECTOR_SHIFT;
+ if cached {
+ page.set_occupied(sector);
+ } else {
+ page.set_occupied_sectors(sector, written);
+ }
+ sector += u64::from(written);
sheaf = access.sheaf;
}
diff --git a/drivers/block/rnull/util.rs b/drivers/block/rnull/util.rs
index 044926c8e2840..49f8c9cbaed7a 100644
--- a/drivers/block/rnull/util.rs
+++ b/drivers/block/rnull/util.rs
@@ -50,13 +50,6 @@ pub(crate) fn mib_to_sectors<T>(mib: T) -> T
mib << (20 - kernel::block::SECTOR_SHIFT)
}
-pub(crate) fn sectors_to_bytes<T>(sectors: T) -> T
-where
- T: core::ops::Shl<u32, Output = T>,
-{
- sectors << kernel::block::SECTOR_SHIFT
-}
-
pub(crate) fn bytes_to_sectors<T>(bytes: T) -> T
where
T: core::ops::Shl<u32, Output = T>,
--
2.56.1
^ permalink raw reply [flat|nested] 5+ messages in thread