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 v2 2/2] block: rnull: track uncached discard coverage by sector
Date: Mon, 30 Nov 2026 12:04:48 +0100	[thread overview]
Message-ID: <20261130-fix-rnull-memory-backed-correctness-v2-2-250755ece813@gmail.com> (raw)
In-Reply-To: <20261130-fix-rnull-memory-backed-correctness-v2-0-250755ece813@gmail.com>

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



  parent reply	other threads:[~2026-11-30 11:05 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [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   ` Peder Bergan via B4 Relay [this message]
2026-11-30 12:19   ` [PATCH RFC v2 0/2] block: rnull: Fix hole reads and uncached discard coverage 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-v2-2-250755ece813@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®