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 2/2] block: rnull: track uncached discard coverage by sector
Date: Mon, 30 Nov 2026 10:31:34 +0100 [thread overview]
Message-ID: <20261130-fix-rnull-memory-backed-correctness-v1-2-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 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
prev parent reply other threads:[~2026-11-30 9:31 UTC|newest]
Thread overview: 3+ 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 ` [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 [this message]
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-2-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