[PATCH 2/2] rnull: size the sector status bitmap for any page size

From: Vitaly Wool

Date: Wed Dec 02 2026 - 13:55:30 EST


`NullBlockPage` tracks the sectors of a page in a single `u64`, and a
static assertion limits pages to 64 sectors. With 64K pages, a page has
128 sectors, so rnull does not build on arm64 with
CONFIG_ARM64_64K_PAGES, or on other architectures with pages larger
than 32K.

Store the status in a `BitmapArray` sized for the sectors of a page,
which resolves the TODO to use the Rust bitmap. Its storage is inline
in the page, so this adds no allocation, where a `BitmapVec` would need
one for every backing page once a page has more than `usize::BITS`
sectors.

Signed-off-by: Vitaly Wool <vitaly.wool@xxxxxxxxxxx>
---
drivers/block/rnull/disk_storage/page.rs | 44 +++++++++++++-----------
1 file changed, 24 insertions(+), 20 deletions(-)

diff --git a/drivers/block/rnull/disk_storage/page.rs b/drivers/block/rnull/disk_storage/page.rs
index 1b21b44dd2a3..3a62cec7d72f 100644
--- a/drivers/block/rnull/disk_storage/page.rs
+++ b/drivers/block/rnull/disk_storage/page.rs
@@ -5,6 +5,7 @@
NoIo,
ScopeKind, //
},
+ bitmap::BitmapArray,
block::{
PAGE_SECTOR_MASK,
SECTOR_SHIFT, //
@@ -15,16 +16,19 @@
PAGE_SIZE, //
},
prelude::*,
- types::Owned,
- uapi::PAGE_SECTORS, //
+ types::Owned, //
};

-// TODO: Use rust bitmap
-static_assert!((PAGE_SIZE >> SECTOR_SHIFT) <= 64);
+/// The number of sectors in a page.
+const SECTORS: usize = PAGE_SIZE >> SECTOR_SHIFT;
+
+/// The number of words in the sector status bitmap of a page.
+const STATUS_WORDS: usize = SECTORS.div_ceil(usize::BITS as usize);

pub(crate) struct NullBlockPage {
page: Owned<Page<Exclusive>>,
- status: u64,
+ /// Bitmap of `SECTORS` bits, set for the first sector of every block that holds data.
+ status: BitmapArray<STATUS_WORDS>,
block_size: u32,
}

@@ -34,7 +38,7 @@ pub(crate) fn new(block_size: u32) -> Result<KBox<Self>> {
Ok(KBox::new(
Self {
page: Page::alloc_exclusive(GFP_KERNEL)?,
- status: 0,
+ status: BitmapArray::new(SECTORS)?,
block_size,
},
GFP_KERNEL,
@@ -42,35 +46,35 @@ pub(crate) fn new(block_size: u32) -> Result<KBox<Self>> {
})
}

+ /// Returns the index of `sector` within its page.
+ fn index(sector: u64) -> usize {
+ // CAST: The index is less than `SECTORS`, which fits in `usize`.
+ (sector & u64::from(PAGE_SECTOR_MASK)) as usize
+ }
+
pub(crate) fn set_occupied(&mut self, sector: u64) {
- let idx = sector & u64::from(PAGE_SECTOR_MASK);
- self.status |= 1 << idx;
+ self.status.set_bit(Self::index(sector));
}

pub(crate) fn set_free(&mut self, sector: u64) {
- let idx = sector & u64::from(PAGE_SECTOR_MASK);
- self.status &= !(1 << idx);
+ self.status.clear_bit(Self::index(sector));
}

pub(crate) fn is_empty(&self) -> bool {
- self.status == 0
+ self.status.next_bit(0).is_none()
}

pub(crate) fn reset(&mut self) {
- self.status = 0;
+ self.status.clear_all();
}

pub(crate) fn is_full(&self) -> bool {
let blocks_per_page = PAGE_SIZE >> self.block_size.trailing_zeros();
- let shift = PAGE_SECTORS as usize / blocks_per_page;
-
- for i in 0..blocks_per_page {
- if self.status & (1 << (i * shift)) == 0 {
- return false;
- }
- }
+ let shift = SECTORS / blocks_per_page;

- true
+ (0..SECTORS)
+ .step_by(shift)
+ .all(|idx| self.status.next_bit(idx) == Some(idx))
}

pub(crate) fn page_mut(&mut self) -> &mut Owned<Page<Exclusive>> {
--
2.39.5