[PATCH v2 16/83] block: rnull: add memory backing

From: Andreas Hindborg

Date: Tue Jun 09 2026 - 15:14:40 EST


Add memory backing to the rust null block driver. This implementation will
always allocate a page on write, even though a page backing the written
sector is already allocated, in which case the page will be released again.
A later patch will fix this inefficiency.

Signed-off-by: Andreas Hindborg <a.hindborg@xxxxxxxxxx>
---
drivers/block/rnull/configfs.rs | 8 ++-
drivers/block/rnull/rnull.rs | 126 ++++++++++++++++++++++++++++++++++------
2 files changed, 116 insertions(+), 18 deletions(-)

diff --git a/drivers/block/rnull/configfs.rs b/drivers/block/rnull/configfs.rs
index 83b474f6da60..8daf2ca409ba 100644
--- a/drivers/block/rnull/configfs.rs
+++ b/drivers/block/rnull/configfs.rs
@@ -60,7 +60,7 @@ impl AttributeOperations<0> for Config {

fn show(_this: &Config, page: &mut [u8; PAGE_SIZE]) -> Result<usize> {
let mut writer = kernel::str::Formatter::new(page);
- writer.write_str("blocksize,size,rotational,irqmode,completion_nsec\n")?;
+ writer.write_str("blocksize,size,rotational,irqmode,completion_nsec,memory_backed\n")?;
Ok(writer.bytes_written())
}
}
@@ -84,6 +84,7 @@ fn make_group(
size: 3,
irqmode: 4,
completion_nsec: 5,
+ memory_backed: 6,
],
};

@@ -101,6 +102,7 @@ fn make_group(
irq_mode: IRQMode::None,
completion_time: time::Delta::ZERO,
name: name.try_into()?,
+ memory_backed: false,
}),
}),
core::iter::empty(),
@@ -165,6 +167,7 @@ struct DeviceConfigInner {
irq_mode: IRQMode,
completion_time: time::Delta,
disk: Option<GenDisk<NullBlkDevice>>,
+ memory_backed: bool,
}

#[vtable]
@@ -195,6 +198,7 @@ fn store(this: &DeviceConfig, page: &[u8]) -> Result {
guard.capacity_mib,
guard.irq_mode,
guard.completion_time,
+ guard.memory_backed,
)?);
guard.powered = true;
} else if guard.powered && !power_op {
@@ -226,3 +230,5 @@ fn from_str(s: &str) -> Result<Self> {
value.try_into()
}
}
+
+configfs_simple_bool_field!(DeviceConfig, 6, memory_backed);
diff --git a/drivers/block/rnull/rnull.rs b/drivers/block/rnull/rnull.rs
index 746ddadd11f0..8e4d2b270bcf 100644
--- a/drivers/block/rnull/rnull.rs
+++ b/drivers/block/rnull/rnull.rs
@@ -6,8 +6,10 @@

use configfs::IRQMode;
use kernel::{
+ bindings,
block::{
self,
+ bio::Segment,
mq::{
self,
gen_disk::{
@@ -19,15 +21,12 @@
},
},
error::Result,
- new_mutex,
+ memalloc_scope, new_mutex, new_xarray,
+ page::SafePage,
pr_info,
prelude::*,
str::CString,
- sync::{
- aref::ARef,
- Arc,
- Mutex, //
- },
+ sync::{aref::ARef, Arc, Mutex},
time::{
hrtimer::{
HrTimerCallback,
@@ -40,7 +39,8 @@
types::{
OwnableRefCounted,
Owned, //
- }, //
+ },
+ xarray::XArray,
};

module! {
@@ -74,6 +74,10 @@
default: 10_000,
description: "Time in ns to complete a request in hardware. Default: 10,000ns",
},
+ memory_backed: bool {
+ default: false,
+ description: "Create a memory-backed block device.",
+ },
},
}

@@ -103,6 +107,7 @@ fn init(_module: &'static ThisModule) -> impl PinInit<Self, Error> {
module_parameters::gb.value() * 1024,
module_parameters::irqmode.value().try_into()?,
Delta::from_nanos(completion_time),
+ module_parameters::memory_backed.value(),
)?;
disks.push(disk, GFP_KERNEL)?;
}
@@ -127,17 +132,23 @@ fn new(
capacity_mib: u64,
irq_mode: IRQMode,
completion_time: Delta,
+ memory_backed: bool,
) -> Result<GenDisk<Self>> {
- let tagset = Arc::pin_init(
- TagSet::new(1, 256, 1, mq::tag_set::Flags::default()),
- GFP_KERNEL,
- )?;
+ let flags = if memory_backed {
+ mq::tag_set::Flag::Blocking.into()
+ } else {
+ mq::tag_set::Flags::default()
+ };
+
+ let tagset = Arc::pin_init(TagSet::new(1, 256, 1, flags), GFP_KERNEL)?;

- let queue_data = Box::new(
- QueueData {
+ let queue_data = Box::pin_init(
+ pin_init!(QueueData {
+ tree <- new_xarray!(kernel::xarray::AllocKind::Alloc),
irq_mode,
completion_time,
- },
+ memory_backed,
+ }),
GFP_KERNEL,
)?;

@@ -148,11 +159,72 @@ fn new(
.rotational(rotational)
.build(fmt!("{}", name.to_str()?), tagset, queue_data)
}
+
+ #[inline(always)]
+ fn write(tree: &XArray<TreeNode>, mut sector: usize, mut segment: Segment<'_>) -> Result {
+ while !segment.is_empty() {
+ let page = SafePage::alloc_page(GFP_KERNEL)?;
+ let mut tree = tree.lock();
+
+ let page_idx = sector >> block::PAGE_SECTORS_SHIFT;
+
+ let page = if let Some(page) = tree.get_mut(page_idx) {
+ page
+ } else {
+ tree.store(page_idx, page, GFP_KERNEL)?;
+ tree.get_mut(page_idx).unwrap()
+ };
+
+ let page_offset = (sector & block::PAGE_SECTOR_MASK as usize) << block::SECTOR_SHIFT;
+ sector += segment.copy_to_page(page, page_offset) >> block::SECTOR_SHIFT;
+ }
+ Ok(())
+ }
+
+ #[inline(always)]
+ fn read(tree: &XArray<TreeNode>, mut sector: usize, mut segment: Segment<'_>) -> Result {
+ let tree = tree.lock();
+
+ while !segment.is_empty() {
+ let idx = sector >> block::PAGE_SECTORS_SHIFT;
+
+ if let Some(page) = tree.get(idx) {
+ let page_offset =
+ (sector & block::PAGE_SECTOR_MASK as usize) << block::SECTOR_SHIFT;
+ sector += segment.copy_from_page(page, page_offset) >> block::SECTOR_SHIFT;
+ } else {
+ sector += segment.zero_page() >> block::SECTOR_SHIFT;
+ }
+ }
+
+ Ok(())
+ }
+
+ #[inline(never)]
+ fn transfer(
+ command: bindings::req_op,
+ tree: &XArray<TreeNode>,
+ sector: usize,
+ segment: Segment<'_>,
+ ) -> Result {
+ match command {
+ bindings::req_op_REQ_OP_WRITE => Self::write(tree, sector, segment)?,
+ bindings::req_op_REQ_OP_READ => Self::read(tree, sector, segment)?,
+ _ => (),
+ }
+ Ok(())
+ }
}

+type TreeNode = Owned<SafePage>;
+
+#[pin_data]
struct QueueData {
+ #[pin]
+ tree: XArray<TreeNode>,
irq_mode: IRQMode,
completion_time: Delta,
+ memory_backed: bool,
}

#[pin_data]
@@ -182,7 +254,7 @@ impl HasHrTimer<Self> for Pdu {

#[vtable]
impl Operations for NullBlkDevice {
- type QueueData = KBox<QueueData>;
+ type QueueData = Pin<KBox<QueueData>>;
type RequestData = Pdu;

fn new_request_data() -> impl PinInit<Self::RequestData> {
@@ -192,7 +264,27 @@ fn new_request_data() -> impl PinInit<Self::RequestData> {
}

#[inline(always)]
- fn queue_rq(queue_data: &QueueData, rq: Owned<mq::Request<Self>>, _is_last: bool) -> Result {
+ fn queue_rq(
+ queue_data: Pin<&QueueData>,
+ mut rq: Owned<mq::Request<Self>>,
+ _is_last: bool,
+ ) -> Result {
+ if queue_data.memory_backed {
+ memalloc_scope!(let _noio: NoIo);
+ let tree = &queue_data.tree;
+ let command = rq.command();
+ let mut sector = rq.sector();
+
+ for bio in rq.bio_iter_mut() {
+ let segment_iter = bio.segment_iter();
+ for segment in segment_iter {
+ let length = segment.len();
+ Self::transfer(command, tree, sector, segment)?;
+ sector += length as usize >> block::SECTOR_SHIFT;
+ }
+ }
+ }
+
match queue_data.irq_mode {
IRQMode::None => rq.end_ok(),
IRQMode::Soft => mq::Request::complete(rq.into()),
@@ -205,7 +297,7 @@ fn queue_rq(queue_data: &QueueData, rq: Owned<mq::Request<Self>>, _is_last: bool
Ok(())
}

- fn commit_rqs(_queue_data: &QueueData) {}
+ fn commit_rqs(_queue_data: Pin<&QueueData>) {}

fn complete(rq: ARef<mq::Request<Self>>) {
OwnableRefCounted::try_from_shared(rq)

--
2.51.2