* [PATCH RFC] block: rnull: use range locks for uncached storage
@ 2026-12-04 19:36 Peder Bergan via B4 Relay
2026-12-04 20:06 ` Jens Axboe
0 siblings, 1 reply; 2+ messages in thread
From: Peder Bergan via B4 Relay @ 2026-12-04 19:36 UTC (permalink / raw)
To: Andreas Hindborg, linux-block
Cc: Jens Axboe, Boqun Feng, Benno Lossin, Gary Guo, rust-for-linux,
linux-kernel
From: Peder Bergan <peder.bergan@gmail.com>
Concurrent requests to different parts of a memory-backed rnull disk
compete for one storage lock. Even with one blk-mq submission queue,
requests from different CPUs can contend on that lock.
Split uncached backing across four XArrays in 1 MiB stripes. Hold one
tree lock at a time and switch locks before accessing the next stripe.
Route uncached discard through the same mapping. Prepare the mapping
before publishing the first queue that can touch backing storage,
including zoned queues, and retain it across power cycles. Cached
storage uses one tree.
Enable range locking by default. Set shard_storage=0 at module load or
rnull_mod.shard_storage=0 at boot to use one tree.
In a four-vCPU x86-64 KVM guest with one submission queue, two writers
allocated sparse 4 KiB pages distributed across the four trees while
two readers accessed resident pages. Across eight paired blocks,
write IOPS increased by 71%, writer CPU time per I/O fell by 42%, and
read p99 fell by 23% at the median. All eight blocks improved on each
of those measures. Median write p99 fell by 36%. When all I/O was
placed on one tree, median write IOPS stayed within 1% of the control.
Signed-off-by: Peder Bergan <peder.bergan@gmail.com>
---
I'd welcome feedback on enabling range locking by default for
uncached storage, with shard_storage=0 as the opt-out.
The two patches in the posted rnull correctness RFC v2 are provisional
prerequisites. I used that series as the base for the patch and the
benchmark controls. I'm happy to adjust the range-lock patch and rebase
once the direction for the hole-read and discard fixes is clear.
https://lore.kernel.org/rust-for-linux/20261130-fix-rnull-memory-backed-correctness-v2-0-250755ece813@gmail.com/
In the four-queue mixed warm 4 KiB test, write throughput rose 99% and
read p99 fell 27%.
With 512-byte logical blocks, three concurrent 127 KiB overwriters
and a 4 KiB reader, median write bandwidth rose from 4,887 to 14,170
MiB/s at saturation. Read p99 went from 0.80 to 0.78 us. At 4,464 MiB/s,
read p99 went from 0.61 to 0.50 us across the four paired blocks that
held the rate in all arms. The same candidate with range locking off
was 6% slower than posted v2 in the saturated 127 KiB test.
If allocation of the extra trees fails, queue creation returns the
allocation error. Feedback on whether to keep that behavior or freeze
a single-tree mapping for the lifetime of the backing store is welcome.
OpenAI Codex assisted with coding and testing.
---
drivers/block/rnull/disk_storage.rs | 94 ++++++++++++++++++++++++++++++++++---
drivers/block/rnull/rnull.rs | 35 +++++++++-----
2 files changed, 109 insertions(+), 20 deletions(-)
diff --git a/drivers/block/rnull/disk_storage.rs b/drivers/block/rnull/disk_storage.rs
index 5cdef47ee1c787..246a0b387f547c 100644
--- a/drivers/block/rnull/disk_storage.rs
+++ b/drivers/block/rnull/disk_storage.rs
@@ -13,6 +13,7 @@
ordering,
Atomic, //
},
+ SetOnce,
SpinLock,
SpinLockGuard, //
},
@@ -27,11 +28,24 @@
mod page;
+const STRIPE_SECTORS: u64 = 1 << (20 - block::SECTOR_SHIFT);
+const EXTRA_TREES: usize = 3;
+type TreeLock = SpinLock<Pin<KBox<TreeContainer>>>;
+type TreeGuard<'a> = SpinLockGuard<'a, Pin<KBox<TreeContainer>>>;
+
+#[pin_data]
+#[repr(align(64))]
+struct Shard {
+ #[pin]
+ tree: TreeLock,
+}
+
#[pin_data]
pub(crate) struct DiskStorage {
// TODO: Get rid of this pointer indirection.
#[pin]
- trees: SpinLock<Pin<KBox<TreeContainer>>>,
+ trees: TreeLock,
+ shards: SetOnce<Pin<KBox<[Shard; EXTRA_TREES]>>>,
cache_size: u64,
cache_size_used: Atomic<u64>,
next_flush_sector: Atomic<u64>,
@@ -44,6 +58,7 @@ pub(crate) fn new(cache_size: u64, block_size: u32) -> impl PinInit<Self, Error>
// TODO: Get rid of the box
// https://git.kernel.org/pub/scm/linux/kernel/git/boqun/linux.git/commit/?h=locking&id=a5d84cafb3e253a11d2e078902c5b090be2f4227
trees <- new_spinlock!(KBox::pin_init(TreeContainer::new(), GFP_KERNEL)?),
+ shards: SetOnce::new(),
cache_size,
cache_size_used: Atomic::new(0),
next_flush_sector: Atomic::new(0),
@@ -51,6 +66,24 @@ pub(crate) fn new(cache_size: u64, block_size: u32) -> impl PinInit<Self, Error>
})
}
+ /// Prepare the mapping before exposing a queue that may access backing storage.
+ pub(crate) fn prepare_shards(&self) -> Result {
+ if self.cache_enabled() || self.shards.as_ref().is_some() {
+ return Ok(());
+ }
+
+ let shards = KBox::pin_init(
+ pin_init::pin_init_array_from_fn(|_| {
+ try_pin_init!(Shard {
+ tree <- new_spinlock!(KBox::pin_init(TreeContainer::new(), GFP_KERNEL)?),
+ })
+ }),
+ GFP_KERNEL,
+ )?;
+ self.shards.populate(shards);
+ Ok(())
+ }
+
pub(crate) fn access<'a, 'b, 'c>(
&'a self,
tree_guard: &'a mut SpinLockGuard<'b, Pin<KBox<TreeContainer>>>,
@@ -59,14 +92,35 @@ pub(crate) fn access<'a, 'b, 'c>(
DiskStorageAccess::new(self, tree_guard, sheaf)
}
- pub(crate) fn lock(&self) -> SpinLockGuard<'_, Pin<KBox<TreeContainer>>> {
+ fn lock(&self, sector: u64) -> TreeGuard<'_> {
+ if let Some(shards) = self.shards.as_ref() {
+ let stripe = ((sector / STRIPE_SECTORS) % (EXTRA_TREES + 1) as u64) as usize;
+ if stripe != 0 {
+ return shards[stripe - 1].tree.lock();
+ }
+ }
self.trees.lock()
}
- pub(crate) fn discard(&self, mut sector: u64, sectors: u32) {
- let tree_guard = self.lock();
+ fn stripe_last_sector(&self, sector: u64) -> u64 {
+ if self.shards.as_ref().is_some() {
+ sector | (STRIPE_SECTORS - 1)
+ } else {
+ u64::MAX
+ }
+ }
+ pub(crate) fn lock_transfer(&self, sector: u64) -> TransferGuard<'_> {
+ TransferGuard {
+ storage: self,
+ guard: Some(self.lock(sector)),
+ last_sector: self.stripe_last_sector(sector),
+ }
+ }
+
+ pub(crate) fn discard(&self, mut sector: u64, sectors: u32) {
if self.cache_enabled() {
+ let tree_guard = self.lock(sector);
let mut cache_guard = tree_guard.cache_tree.lock();
let mut disk_guard = tree_guard.disk_tree.lock();
@@ -79,9 +133,14 @@ pub(crate) fn discard(&self, mut sector: u64, sectors: u32) {
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);
+ while sector < end {
+ let tree_guard = self.lock(sector);
+ let mut disk_guard = tree_guard.disk_tree.lock();
+ let stripe_end = self.stripe_last_sector(sector).saturating_add(1).min(end);
+ Self::discard_uncached(&mut disk_guard, sector, stripe_end);
+ sector = stripe_end;
+ }
}
}
@@ -125,7 +184,7 @@ pub(crate) fn free_sector<'a>(
}
pub(crate) fn flush(&self) {
- let mut tree_guard = self.lock();
+ let mut tree_guard = self.lock(0);
let mut access = self.access(&mut tree_guard, None);
access.flush()
}
@@ -135,6 +194,27 @@ pub(crate) fn cache_enabled(&self) -> bool {
}
}
+pub(crate) struct TransferGuard<'a> {
+ storage: &'a DiskStorage,
+ guard: Option<TreeGuard<'a>>,
+ last_sector: u64,
+}
+
+impl<'a> TransferGuard<'a> {
+ pub(crate) fn at(&mut self, sector: u64) -> &mut TreeGuard<'a> {
+ if sector > self.last_sector {
+ drop(self.guard.take());
+ self.guard = Some(self.storage.lock(sector));
+ self.last_sector = self.storage.stripe_last_sector(sector);
+ }
+ self.current()
+ }
+
+ pub(crate) fn current(&mut self) -> &mut TreeGuard<'a> {
+ self.guard.as_mut().expect("transfer holds a tree lock")
+ }
+}
+
pub(crate) struct DiskStorageAccess<'a, 'c> {
cache_guard: xarray::MutGuard<'a, TreeNode>,
disk_guard: xarray::MutGuard<'a, TreeNode>,
diff --git a/drivers/block/rnull/rnull.rs b/drivers/block/rnull/rnull.rs
index 39484b9286a465..76a76c4cbcf908 100644
--- a/drivers/block/rnull/rnull.rs
+++ b/drivers/block/rnull/rnull.rs
@@ -17,6 +17,7 @@
use disk_storage::{
DiskStorage,
NullBlockPage,
+ TransferGuard,
TreeContainer, //
};
use kernel::alloc::scoped::{
@@ -91,6 +92,10 @@
description: "Rust implementation of the C null block driver",
license: "GPL v2",
params: {
+ shard_storage: bool {
+ default: true,
+ description: "Use range locks for uncached backing storage (0 for one lock).",
+ },
gb: u64 {
default: 4,
description: "Device capacity in GiB",
@@ -596,6 +601,11 @@ fn new(options: NullBlkOptions<'_>) -> Result<Arc<GenDisk<Self>>> {
.max_hw_discard_sectors(ffi::c_uint::MAX >> block::SECTOR_SHIFT);
}
+ // Zoned commands can access storage even when memory_backed is false.
+ if (memory_backed || zoned) && module_parameters::shard_storage.value() {
+ storage.prepare_shards()?;
+ }
+
let disk = builder.build(fmt!("{}", name.to_str()?), tagset, queue_data)?;
let queue_data: ArcBorrow<'_, Self> = disk.queue_data();
queue_data.disk.populate(disk.get_ref());
@@ -645,9 +655,9 @@ fn preload<'b, 'c>(
}
#[inline(always)]
- fn write<'a, 'b, 'c>(
- &'a self,
- tree_guard: &'b mut SpinLockGuard<'c, Pin<KBox<TreeContainer>>>,
+ fn write(
+ &self,
+ transfer: &mut TransferGuard<'_>,
mut sector: u64,
mut segment: Segment<'_>,
bypass_cache: bool,
@@ -657,6 +667,7 @@ fn write<'a, 'b, 'c>(
while !segment.at_end() {
// TODO: Maybe try the write without a sheaf. If it fails, allocate and fill a sheaf and retry.
+ let tree_guard = transfer.at(sector);
Self::preload(tree_guard, self.block_size_bytes, &mut sheaf)?;
let mut access = self.storage.access(tree_guard, sheaf);
@@ -693,7 +704,7 @@ fn write<'a, 'b, 'c>(
}
if let Some(sheaf) = sheaf {
- tree_guard.do_unlocked(|| {
+ transfer.current().do_unlocked(|| {
sheaf.return_refill(GFP_KERNEL);
});
}
@@ -702,15 +713,14 @@ fn write<'a, 'b, 'c>(
}
#[inline(always)]
- fn read<'a, 'b, 'c>(
- &'a self,
- tree_guard: &'b mut SpinLockGuard<'c, Pin<KBox<TreeContainer>>>,
+ fn read(
+ &self,
+ transfer: &mut TransferGuard<'_>,
mut sector: u64,
mut segment: Segment<'_>,
) -> Result {
- let access = self.storage.access(tree_guard, None);
-
while !segment.at_end() {
+ let access = self.storage.access(transfer.at(sector), None);
// CAST: Page offset always fits in 32 bits.
let page_offset =
((sector & u64::from(block::PAGE_SECTOR_MASK)) << SECTOR_SHIFT) as usize;
@@ -740,8 +750,7 @@ fn transfer(
let mut sector = rq.sector();
let max_end_sector = sector + <u32 as Into<u64>>::into(max_sectors);
- // TODO: Use `PerCpu` to get rid of this lock
- let mut tree_guard = self.storage.lock();
+ let mut transfer = self.storage.lock_transfer(sector);
let skip_cache = rq.flags().contains(mq::RequestFlag::ForcedUnitAccess);
@@ -755,9 +764,9 @@ fn transfer(
segment.truncate(length_sectors_allowed << SECTOR_SHIFT);
match command {
mq::Command::Write => {
- self.write(&mut tree_guard, sector, segment, skip_cache)?
+ self.write(&mut transfer, sector, segment, skip_cache)?
}
- mq::Command::Read => self.read(&mut tree_guard, sector, segment)?,
+ mq::Command::Read => self.read(&mut transfer, sector, segment)?,
_ => (),
}
sector += u64::from(length_sectors_allowed);
---
base-commit: 9c0fceca4feed5f1056f55abe9c7c70731402776
change-id: 20261204-rfc-rnull-range-lock-posted-v2-e8c936552ed1
prerequisite-message-id: <20261130-fix-rnull-memory-backed-correctness-v2-0-250755ece813@gmail.com>
prerequisite-patch-id: 9dc77ed5dcc6e5b0de440c2906e1c55763d7753f
prerequisite-patch-id: 89493b24d8f770d3ba47f60bcc3d1aff3b78fef4
Best regards,
--
Peder Bergan <peder.bergan@gmail.com>
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH RFC] block: rnull: use range locks for uncached storage
2026-12-04 19:36 [PATCH RFC] block: rnull: use range locks for uncached storage Peder Bergan via B4 Relay
@ 2026-12-04 20:06 ` Jens Axboe
0 siblings, 0 replies; 2+ messages in thread
From: Jens Axboe @ 2026-12-04 20:06 UTC (permalink / raw)
To: peder.bergan, Andreas Hindborg, linux-block
Cc: Boqun Feng, Benno Lossin, Gary Guo, rust-for-linux, linux-kernel
https://lore.kernel.org/linux-block/0987f045-f31c-4866-b044-8305c7fbbe74@kernel.dk/
--
Jens Axboe
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-12-04 20:06 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-04 19:36 [PATCH RFC] block: rnull: use range locks for uncached storage Peder Bergan via B4 Relay
2026-12-04 20:06 ` Jens Axboe
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox