mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH RFC v2 0/2] block: rnull: Fix hole reads and uncached discard coverage
@ 2026-11-30 11:04 ` Peder Bergan via B4 Relay
  2026-11-30 11:04   ` [PATCH RFC v2 1/2] block: rnull: stop zero-filling at the backing page boundary Peder Bergan via B4 Relay
                     ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Peder Bergan via B4 Relay @ 2026-11-30 11:04 UTC (permalink / raw)
  To: Andreas Hindborg, Jens Axboe
  Cc: Boqun Feng, linux-block, rust-for-linux, linux-kernel

This RFC is based on Andreas Hindborg's rnull-v7.4-rc2 feature
branch at 9c0fceca4feed.

A read that starts in an unwritten backing page can return zeroes
for data in the next page. Stop the zero fill at the backing-page
boundary and look up the next page.

A configfs device can retain its backing storage across a logical
block size change. Track uncached occupancy in 512-byte sectors so
partial discards preserve data outside the discarded range and free
each page when its last occupied sector is discarded.

---
OpenAI Codex assisted with coding and testing.

---
Changes in v2:
- Drop the cached discard stride change from patch 2 after Sashiko
  pointed out the mismatch with cached occupancy tracking. Patch 2
  now addresses uncached discard coverage only.
- v1: https://lore.kernel.org/rust-for-linux/20261130-fix-rnull-memory-backed-correctness-v1-0-ff16977610c0@gmail.com/

---
Peder Bergan (2):
      block: rnull: stop zero-filling at the backing page boundary
      block: rnull: track uncached discard coverage by sector

 drivers/block/rnull/disk_storage.rs      | 40 +++++++++++++++++++++++++-------
 drivers/block/rnull/disk_storage/page.rs | 16 +++++++++++++
 drivers/block/rnull/rnull.rs             | 39 +++++++++++++++++--------------
 3 files changed, 69 insertions(+), 26 deletions(-)
---
base-commit: 9c0fceca4feed5f1056f55abe9c7c70731402776
change-id: 20261130-fix-rnull-memory-backed-correctness-a7d95f64ab7e

Best regards,
--  
Peder Bergan <peder.bergan@gmail.com>



^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH RFC v2 1/2] block: rnull: stop zero-filling at the backing page boundary
  2026-11-30 11:04 ` [PATCH RFC v2 0/2] block: rnull: Fix hole reads and uncached discard coverage Peder Bergan via B4 Relay
@ 2026-11-30 11:04   ` Peder Bergan via B4 Relay
  2026-11-30 11:04   ` [PATCH RFC v2 2/2] block: rnull: track uncached discard coverage by sector Peder Bergan via B4 Relay
  2026-11-30 12:19   ` [PATCH RFC v2 0/2] block: rnull: Fix hole reads and uncached discard coverage Andreas Hindborg
  2 siblings, 0 replies; 4+ messages in thread
From: Peder Bergan via B4 Relay @ 2026-11-30 11:04 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] 4+ messages in thread

* [PATCH RFC v2 2/2] block: rnull: track uncached discard coverage by sector
  2026-11-30 11:04 ` [PATCH RFC v2 0/2] block: rnull: Fix hole reads and uncached discard coverage Peder Bergan via B4 Relay
  2026-11-30 11:04   ` [PATCH RFC v2 1/2] block: rnull: stop zero-filling at the backing page boundary Peder Bergan via B4 Relay
@ 2026-11-30 11:04   ` Peder Bergan via B4 Relay
  2026-11-30 12:19   ` [PATCH RFC v2 0/2] block: rnull: Fix hole reads and uncached discard coverage Andreas Hindborg
  2 siblings, 0 replies; 4+ messages in thread
From: Peder Bergan via B4 Relay @ 2026-11-30 11:04 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
an uncached 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.

Fixes: b2e667324057 ("block: rnull: add volatile cache emulation")
Signed-off-by: Peder Bergan <peder.bergan@gmail.com>
---
 drivers/block/rnull/disk_storage.rs      | 40 +++++++++++++++++++++++++-------
 drivers/block/rnull/disk_storage/page.rs | 16 +++++++++++++
 drivers/block/rnull/rnull.rs             | 12 +++++++---
 3 files changed, 56 insertions(+), 12 deletions(-)

diff --git a/drivers/block/rnull/disk_storage.rs b/drivers/block/rnull/disk_storage.rs
index bb50a1c6cb9ee..5cdef47ee1c78 100644
--- a/drivers/block/rnull/disk_storage.rs
+++ b/drivers/block/rnull/disk_storage.rs
@@ -1,6 +1,6 @@
 // SPDX-License-Identifier: GPL-2.0
 
-use crate::util::*;
+use crate::util::{bytes_to_sectors, sectors_to_bytes};
 use core::pin::Pin;
 use kernel::{
     block,
@@ -65,16 +65,38 @@ pub(crate) fn lock(&self) -> SpinLockGuard<'_, Pin<KBox<TreeContainer>>> {
 
     pub(crate) fn discard(&self, mut sector: u64, sectors: u32) {
         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();
 
-        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;
+            let mut remaining_bytes = sectors_to_bytes(sectors);
+
+            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;
+            }
+        } else {
+            let mut disk_guard = tree_guard.disk_tree.lock();
+            let end = sector + u64::from(sectors);
+            Self::discard_uncached(&mut disk_guard, sector, end);
+        }
+    }
+
+    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;
         }

-- 
2.56.1



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH RFC v2 0/2] block: rnull: Fix hole reads and uncached discard coverage
  2026-11-30 11:04 ` [PATCH RFC v2 0/2] block: rnull: Fix hole reads and uncached discard coverage Peder Bergan via B4 Relay
  2026-11-30 11:04   ` [PATCH RFC v2 1/2] block: rnull: stop zero-filling at the backing page boundary Peder Bergan via B4 Relay
  2026-11-30 11:04   ` [PATCH RFC v2 2/2] block: rnull: track uncached discard coverage by sector Peder Bergan via B4 Relay
@ 2026-11-30 12:19   ` Andreas Hindborg
  2 siblings, 0 replies; 4+ messages in thread
From: Andreas Hindborg @ 2026-11-30 12:19 UTC (permalink / raw)
  To: peder.bergan, Jens Axboe
  Cc: Boqun Feng, linux-block, rust-for-linux, linux-kernel

"Peder Bergan via B4 Relay" <devnull+peder.bergan.gmail.com@kernel.org>
writes:

> This RFC is based on Andreas Hindborg's rnull-v7.4-rc2 feature
> branch at 9c0fceca4feed.
>
> A read that starts in an unwritten backing page can return zeroes
> for data in the next page. Stop the zero fill at the backing-page
> boundary and look up the next page.
>
> A configfs device can retain its backing storage across a logical
> block size change. Track uncached occupancy in 512-byte sectors so
> partial discards preserve data outside the discarded range and free
> each page when its last occupied sector is discarded.

Please rate limit your re-submission to at least 24 hours.

Best regards,
Andreas Hindborg




^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-11-30 12:19 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <f_e360qdKgdE7zFpZt3Por-bXWTYnkqqVuWpKBq7tl390jTLK3PxFTEOk4T22t4ISYr53mAHhXu0KJvru7p1Ag==@protonmail.internalid>
2026-11-30 11:04 ` [PATCH RFC v2 0/2] block: rnull: Fix hole reads and uncached discard coverage Peder Bergan via B4 Relay
2026-11-30 11:04   ` [PATCH RFC v2 1/2] block: rnull: stop zero-filling at the backing page boundary Peder Bergan via B4 Relay
2026-11-30 11:04   ` [PATCH RFC v2 2/2] block: rnull: track uncached discard coverage by sector Peder Bergan via B4 Relay
2026-11-30 12:19   ` [PATCH RFC v2 0/2] block: rnull: Fix hole reads and uncached discard coverage Andreas Hindborg

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®