[PATCH v3 0/3] rust: block: add rbrd, a RAM backed block driver

From: Vitaly Wool

Date: Fri Dec 04 2026 - 06:42:14 EST


This series adds rbrd, a RAM backed block driver written in Rust that
works like brd, together with the Rust abstractions it needs:

1) a `Guarded` page kind: a reference counted page whose data is only
accessed under a bit spinlock in `page->private`. A guard gives
access to the page data as a byte slice while the lock is held, and
`try_get_raw()` takes a speculative reference for lock-free lookups;
2) a `discard_granularity` option for `GenDiskBuilder`;
3) the driver itself, configured through configfs like rnull. Its pages
are kept in a flat array of atomic page pointers, so finding, adding
or removing a page takes no lock.

Unlike brd, rbrd never lets a read and a write copy the same page at
the same time: each copy holds the page's bit spinlock. brd allows
this race, so a read that overlaps a write can return a torn mix of
old and new data. In Rust the race would also be undefined behaviour.

This guarantee costs very little now. Under parallel load rbrd matches
brd: with 16 or more threads, writes run at 98-100% of brd's rate, and
64k I/O runs at 99-103% (O_DIRECT, 1 GiB device, 24 vCPU KVM guest on
a Xeon host pinned to one NUMA node, both drivers on the same kernel,
median of 3 boots, kIOPS):

test brd rbrd rbrd/brd
write 4k, 16 threads 5572 5468 98%
write 4k, 48 threads 5444 5333 98%
randwrite 4k, 24 thr 5797 5789 100%
randwrite 4k, 48 thr 5727 5735 100%
read 64k, 24 threads 264 270 102%
read 64k, 48 threads 262 266 102%
write 64k, 24 threads 258 259 101%
randwrite 64k, 24 thr 263 270 103%

rbrd was stress tested with data verification on 4k and 512-byte
block devices while a third device was created, used and removed in a
loop, on a normal kernel and on a KASAN, lockdep and kmemleak build:
no errors, no leaks and a clean dmesg. The Rust doctests, including
the new `alloc_guarded()` example, pass.

Changes since v2:
- The module is built from rbrd.o, so that its name is rbrd, as in
module!. v2 built rbrd_mod.o: when built in, modules.builtin listed
rbrd_mod.ko while the modinfo strings were prefixed with "rbrd.", so
modinfo could not show the module's information under either name. The
Makefile also gets its SPDX line.
- New patch 2 adds GenDiskBuilder::discard_granularity(), and rbrd sets
the granularity to PAGE_SIZE, like brd. It defaulted to the physical
block size, so a device with 512-byte blocks advertised a 512-byte
granularity although rbrd only frees whole pages, and the block layer
split discards larger than the maximum discard size at 512-byte
boundaries, leaving the page across the split allocated.
- lookup_page() retries, like brd_lookup_page(), when try_get_raw()
finds the page's reference count at zero. A discard has emptied the
slot by then, but a writer may have refilled it, and returning None
reported that slot as empty: reads returned zeroes, and writes made a
needless GFP_NOWAIT allocation, which could fail the write.
- The size is a usize rather than a u64: core formats a u64 by dividing
it, which 32-bit kernels do not provide (on 32-bit ARM,
__aeabi_uldivmod is a stub that BUG()s), so reading back a size of
1000 MiB or more oopsed, with the device's configuration mutex held.
The number of pages of a device has to fit in a usize anyway.

Changes since v1:
- Rebased onto rnull-v7.4-rc2, which provides bio segment iteration
and request accessors, so the v1 patch adding them is dropped.
- The per-page locking of v1 is replaced by a new `Guarded` page kind:
a reference counted page whose data is only accessed under a bit
spinlock in `page->private`. A `Page<Guarded>` only dereferences to
`Page<Base>`, so safe code cannot reach its data in other ways, such
as `with_page_as_sysmem_local()` or mapping it with
`vm_insert_page()`, and `lock_data()` is safe. The lock guard
exposes the page data as `&[u8]` / `&mut [u8]`.
- rbrd copies with the `Segment` read/write API, reads unwritten
sectors from a zeroed page and allocates zeroed pages with
`Page::alloc_guarded()`.

The series is based on rnull-v7.4-rc2 from
https://github.com/metaspace/linux. The patches were written with the
Claude Code AI coding assistant, which also did the testing.

Igor Belousov (2):
rust: page: add guarded pages
block: rust: add `discard_granularity` option to `GenDiskBuilder`

Vitaly Wool (2):
rust: block: add rbrd, a RAM backed block driver

MAINTAINERS | 1 +
drivers/block/Kconfig | 1 +
drivers/block/Makefile | 1 +
drivers/block/rbrd/Kconfig | 17 ++
drivers/block/rbrd/Makefile | 3 +
drivers/block/rbrd/configfs.rs | 195 +++++++++++++
drivers/block/rbrd/rbrd.rs | 483 +++++++++++++++++++++++++++++++
rust/helpers/bit_spinlock.c | 13 +
rust/helpers/helpers.c | 1 +
rust/helpers/page.c | 5 +
rust/kernel/block/mq/gen_disk.rs | 13 +
rust/kernel/page.rs | 235 +++++++++++++++
12 files changed, 968 insertions(+)
create mode 100644 drivers/block/rbrd/Kconfig
create mode 100644 drivers/block/rbrd/Makefile
create mode 100644 drivers/block/rbrd/configfs.rs
create mode 100644 drivers/block/rbrd/rbrd.rs
create mode 100644 rust/helpers/bit_spinlock.c


base-commit: 9c0fceca4feed5f1056f55abe9c7c70731402776
--
2.39.5