Skip to contentSkip to conversation

rust: Add support for reserving of ranges of IDs

PATCH Superseded Eliot Courtney 7 participants 34 messages Sep 30 – Oct 16

Subsystems (8)

v9 of 12 Superseded Latest is v12 →

lore
Share & tools
lore
#0 lore raw

v9 cover rust: Add support for reserving of ranges of IDs

Add support for reserving of ranges of IDs, with a usage in nova-core for channel IDs. This entails adding bindings for the C bitmap API for ranges of bits, then users of that in IdPool, and finally a user of IdPool in nova-core, ChannelIdPool.

Channel ID tracking is needed for allotting ranges of channel IDs to vGPU guests, and later for regular host channel ID reservation. nova-core needs allocation of a contiguous sequence of IDs with a specific length and sometimes a specific alignment [1].

About the tradeoffs between different data structures:
- IDA/xarray do not support allocating a contiguous sequence of IDs (ida_alloc_range() allocates a single ID within a range, not a contiguous sequence).
- A maple tree works, but is not as good a fit. The ID space is small (limited to 2048) and aligned allocation needs an alloc_range()+erase() retry loop (plus a Mutex around it, or new mas_empty_area() bindings) that essentially reimplements bitmap_find_next_zero_area(). See the maple tree version at [2]. For 2048 IDs a bitmap is also considerably faster and smaller [3].
- The bitmap API natively supports aligned contiguous area allocation (bitmap_find_next_zero_area()).

This is based on drm-rust-next and depends on the cv! series [4].

[1]: https://lore.kernel.org/all/[email protected]/
[2]: https://lore.kernel.org/all/[email protected]/
[3]: https://lore.kernel.org/all/[email protected]/
[4]: https://lore.kernel.org/all/[email protected]/

---
Changes in v9:
- Rebase on drm-rust-next
- Moved cv! to a separate series which this now depends on
- Reference Link: tag from the text + indent code in patch #4 commit message (Miguel)
- Add reviewed-by tags
- Link to v8: https://patch.msgid.link/[email protected]

Changes in v8:
- Clarify overflow cases in patch #2 (Alex)
- Replace nz! with generalized cv! macro (Gary)
- Add cv! macro to prelude
- Split SizeConstants and adding new constants patch (Alex)
- Move alignment size constants conersions patch (Alex)
- Drop NonZero on IdPool, fix grow_request instead (Alice, Alex)
- Add shrink/grow doctests (Burak)
- Link to v7: https://patch.msgid.link/[email protected]

Changes in v7:
- Add nz! macro for compile time NonZero
- Implement SizeConstants for Alignment + add sub 1k size constants
- Use Alignment constants + nz! in this series
- Add final patch converting existing callers to use Alignment constants
- Rename alloc_area/release_area -> reserve_ids / release_ids
- Link to v6: https://patch.msgid.link/[email protected]

Changes in v6:
- Take a NonZero nbits in next_zero_area_off, set and clear (Yury)
- Remove UnusedArea, IdPool::alloc_area now allocates directly (Yury)
- Add patch: take a NonZero capacity in IdPool::with_capacity()
- Add patch: do not round the capacity up to BitmapVec::MAX_INLINE_LEN, which also removes the bounds check in ChannelIdPool::alloc_area
- Add more testing of Drop for ChannelIdPool (Yury)
- Link to v5: https://patch.msgid.link/[email protected]

Changes in v5:
- bitmap_assert! i32::MAX length for Bitmap::from_raw* (Yury)
- Only run overflow check on 32-bit (Yury)
- Link to v4: https://patch.msgid.link/[email protected]

Changes in v4:
- Add next_zero_area_off to match C code (Yury)
- Replace overflow checks to match C code in bitmap-for-next.
- Tighten Bitmap unsafe contract to disallow Bitmaps larger than i32::MAX
- Link to v3: https://patch.msgid.link/[email protected]

Changes in v3:
- Use Alignment type in id_pool and bitmap (Alice)
- Remove hang check on the basis that it's extraordinarily rare.
- Link to v2: https://patch.msgid.link/[email protected]

Changes in v2:
- Collected Alice's Reviewed-by on patch 1.
- Address Yury's comments w.r.t. using __bitmap_set etc directly.
- Address Yury's comments w.r.t. following the C names
- Additionally check for an overflow case that causes a hang
- Added more info to cover letter + patch 4 w.r.t. channel ID allottment requirements
- Add align parameter to ChannelIdPool::alloc_area() plus an aligned allocation test
- Add missing INVARIANT comment when constructing UnusedArea
- Link to v1:
https://patch.msgid.link/[email protected]

---
Eliot Courtney (9):
rust: bitmap: use function-level cfg on kunit test
rust: bitmap: restrict bitmap length to at most i32::MAX
rust: sizes: add sub-1K size constants
rust: sizes: implement SizeConstants for Alignment
rust: use Alignment size constants
rust: bitmap: add contiguous area operations
rust: id_pool: add contiguous ID reservation
rust: id_pool: do not round capacity up to BitmapVec::MAX_INLINE_LEN
gpu: nova-core: add ChannelIdPool

 drivers/gpu/nova-core/fb.rs           |  10 +-
 drivers/gpu/nova-core/fb/hal/gb100.rs |   2 +-
 drivers/gpu/nova-core/fsp.rs          |   4 +-
 drivers/gpu/nova-core/gpu.rs          |   2 +
 drivers/gpu/nova-core/gpu/channel.rs  | 198 ++++++++++++++++++++
 drivers/gpu/nova-core/gsp/fw.rs       |  12 +-
 drivers/gpu/nova-core/vbios.rs        |   7 +-
 rust/kernel/bitmap.rs                 | 336 ++++++++++++++++++++++++++++++----
 rust/kernel/gpu/buddy.rs              |  26 +--
 rust/kernel/id_pool.rs                |  70 ++++++-
 rust/kernel/io.rs                     |   5 +-
 rust/kernel/sizes.rs                  |  47 ++++-
 12 files changed, 641 insertions(+), 78 deletions(-)

---
base-commit: 10a6623a24a85708650efad7be15182289403cd7
change-id: 20260608-chid-18fa943c6d6c
prerequisite-change-id: 20260828-cv-8d4e952fc14c:v4
prerequisite-patch-id: b913a24359e679cb354298102cf5de13760a3c27
prerequisite-patch-id: 01ec2e5a44b393a7ce1ee820654750dd4743fcb5

Best regards,

Signature · 1 line
Eliot Courtney <[email protected]>
#1 lore raw

PATCH v9 1/9 rust: bitmap: use function-level cfg on kunit test

Since commit c652dc44192d ("rust: kunit: allow cfg on tests"), we no longer need this workaround.

---
 rust/kernel/bitmap.rs | 25 +++++++++++--------------
 1 file changed, 11 insertions(+), 14 deletions(-)
rust/kernel/bitmap.rs +11 -14
index b27e0ec80d64..a43bfe0ec3dc 100644
@@ -572,24 +572,21 @@ fn bitmap_set_clear_find() -> Result<(), AllocError> {
     }
 
     #[test]
+    #[cfg(not(CONFIG_RUST_BITMAP_HARDENED))]
     fn owned_bitmap_out_of_bounds() -> Result<(), AllocError> {
-        // TODO: Kunit #[test]s do not support `cfg` yet,
-        // so we add it here in the body.
-        #[cfg(not(CONFIG_RUST_BITMAP_HARDENED))]
-        {
-            let mut b = BitmapVec::new(128, GFP_KERNEL)?;
-            b.set_bit(2048);
-            b.set_bit_atomic(2048);
-            b.clear_bit(2048);
-            b.clear_bit_atomic(2048);
-            assert_eq!(None, b.next_bit(2048));
-            assert_eq!(None, b.next_zero_bit(2048));
-            assert_eq!(None, b.last_bit());
-        }
+        let mut b = BitmapVec::new(128, GFP_KERNEL)?;
+
+        b.set_bit(2048);
+        b.set_bit_atomic(2048);
+        b.clear_bit(2048);
+        b.clear_bit_atomic(2048);
+        assert_eq!(None, b.next_bit(2048));
+        assert_eq!(None, b.next_zero_bit(2048));
+        assert_eq!(None, b.last_bit());
         Ok(())
     }
 
-    // TODO: uncomment once kunit supports [should_panic] and `cfg`.
+    // TODO: uncomment once kunit supports `#[should_panic]`.
     // #[cfg(CONFIG_RUST_BITMAP_HARDENED)]
     // #[test]
     // #[should_panic]
Signature · 1 line
2.55.0
#2 lore raw

PATCH v9 2/9 rust: bitmap: restrict bitmap length to at most i32::MAX

In reply to Eliot Courtney

It is currently possible to construct a non-BitmapVec backed Bitmap using Bitmap::from_raw that is larger than i32::MAX, and it is not part of the unsafe requirements. Restricting all bitmaps (even non-BitmapVec backed ones) to a maximum size of i32::MAX simplifies a few things and matches BitmapVec::MAX_LEN. For example, copy_and_extend truncates len to u32, which is wrong for > u32::MAX size. __bitmap_set and __bitmap_clear need i32 for size and u32 for start - so they can't be run on a Bitmap with a > i32::MAX size. Rather than adding runtime checks to account for the case of a non BitmapVec backed Bitmap, just include that in the requirements for Bitmap.

Add that requirement to the unsafe requirements on Bitmap::from_raw and Bitmap::from_raw_mut, and to the invariants on Bitmap.

This also fixes u32 casts truncating in copy_and_extend, which could otherwise lead to OOB writes.

---
 rust/kernel/bitmap.rs | 70 +++++++++++++++++++++++++++++++++++----------------
 1 file changed, 49 insertions(+), 21 deletions(-)
rust/kernel/bitmap.rs +49 -21
index a43bfe0ec3dc..df5505ec7a96 100644
@@ -17,24 +17,59 @@
 /// # Invariants
 ///
 /// Must reference a `[c_ulong]` long enough to fit `data.len()` bits.
+/// Must not be longer than `i32::MAX` bits, so offsets and lengths used with
+/// `Bitmap` functions fit in the int and unsigned int arguments of the C bitmap API.
+/// This also matches [`BitmapVec::MAX_LEN`].
 #[cfg_attr(CONFIG_64BIT, repr(align(8)))]
 #[cfg_attr(not(CONFIG_64BIT), repr(align(4)))]
 pub struct Bitmap {
     data: [()],
 }
 
+macro_rules! bitmap_assert {
+    ($cond:expr, $($arg:tt)+) => {
+        #[cfg(CONFIG_RUST_BITMAP_HARDENED)]
+        assert!($cond, $($arg)*);
+    }
+}
+
+macro_rules! bitmap_assert_return {
+    ($cond:expr, $($arg:tt)+) => {
+        #[cfg(CONFIG_RUST_BITMAP_HARDENED)]
+        assert!($cond, $($arg)*);
+
+        #[cfg(not(CONFIG_RUST_BITMAP_HARDENED))]
+        if !($cond) {
+            pr_err!($($arg)*);
+            return
+        }
+    }
+}
+
 impl Bitmap {
     /// Borrows a C bitmap.
     ///
+    /// # Panics
+    ///
+    /// Panics if CONFIG_RUST_BITMAP_HARDENED is enabled and `nbits` exceeds `i32::MAX`.
+    ///
     /// # Safety
     ///
     /// * `ptr` holds a non-null address of an initialized array of `unsigned long`
     ///   that is large enough to hold `nbits` bits.
+    /// * `nbits` must not exceed `i32::MAX`.
     /// * the array must not be freed for the lifetime of this [`Bitmap`]
     /// * concurrent access only happens through atomic operations
     pub unsafe fn from_raw<'a>(ptr: *const usize, nbits: usize) -> &'a Bitmap {
+        bitmap_assert!(
+            nbits <= i32::MAX as usize,
+            "`nbits` must be <= {}, was {}",
+            i32::MAX,
+            nbits
+        );
         let data: *const [()] = core::ptr::slice_from_raw_parts(ptr.cast(), nbits);
         // INVARIANT: `data` references an initialized array that can hold `nbits` bits.
+        // INVARIANT: the caller guarantees that `nbits` does not exceed `i32::MAX`.
         // SAFETY:
         // The caller guarantees that `data` (derived from `ptr` and `nbits`)
         // points to a valid, initialized, and appropriately sized memory region
@@ -51,15 +86,27 @@ pub unsafe fn from_raw<'a>(ptr: *const usize, nbits: usize) -> &'a Bitmap {
 
     /// Borrows a C bitmap exclusively.
     ///
+    /// # Panics
+    ///
+    /// Panics if CONFIG_RUST_BITMAP_HARDENED is enabled and `nbits` exceeds `i32::MAX`.
+    ///
     /// # Safety
     ///
     /// * `ptr` holds a non-null address of an initialized array of `unsigned long`
     ///   that is large enough to hold `nbits` bits.
+    /// * `nbits` must not exceed `i32::MAX`.
     /// * the array must not be freed for the lifetime of this [`Bitmap`]
     /// * no concurrent access may happen.
     pub unsafe fn from_raw_mut<'a>(ptr: *mut usize, nbits: usize) -> &'a mut Bitmap {
+        bitmap_assert!(
+            nbits <= i32::MAX as usize,
+            "`nbits` must be <= {}, was {}",
+            i32::MAX,
+            nbits
+        );
         let data: *mut [()] = core::ptr::slice_from_raw_parts_mut(ptr.cast(), nbits);
         // INVARIANT: `data` references an initialized array that can hold `nbits` bits.
+        // INVARIANT: the caller guarantees that `nbits` does not exceed `i32::MAX`.
         // SAFETY:
         // The caller guarantees that `data` (derived from `ptr` and `nbits`)
         // points to a valid, initialized, and appropriately sized memory region
@@ -96,26 +143,6 @@ union BitmapRepr {
     ptr: NonNull<usize>,
 }
 
-macro_rules! bitmap_assert {
-    ($cond:expr, $($arg:tt)+) => {
-        #[cfg(CONFIG_RUST_BITMAP_HARDENED)]
-        assert!($cond, $($arg)*);
-    }
-}
-
-macro_rules! bitmap_assert_return {
-    ($cond:expr, $($arg:tt)+) => {
-        #[cfg(CONFIG_RUST_BITMAP_HARDENED)]
-        assert!($cond, $($arg)*);
-
-        #[cfg(not(CONFIG_RUST_BITMAP_HARDENED))]
-        if !($cond) {
-            pr_err!($($arg)*);
-            return
-        }
-    }
-}
-
 /// Represents an owned bitmap.
 ///
 /// Wraps underlying C bitmap API. See [`Bitmap`] for available
@@ -415,7 +442,8 @@ pub fn clear_bit_atomic(&self, index: usize) {
     #[inline]
     pub fn copy_and_extend(&mut self, src: &Bitmap) {
         let len = core::cmp::min(src.len(), self.len());
-        // SAFETY: access to `self` and `src` is within bounds.
+        // SAFETY: access to `self` and `src` is within bounds. Both lengths fit in `u32`
+        // because a `Bitmap` is at most `i32::MAX` bits, so the casts are lossless.
         unsafe {
             bindings::bitmap_copy_and_extend(
                 self.as_mut_ptr(),
Signature · 1 line
2.55.0
#3 lore raw

PATCH v9 3/9 rust: sizes: add sub-1K size constants

In reply to Eliot Courtney

Add some more size constants, mirroring include/linux/sizes.h. This is useful for making Alignment implement SizeConstants in a following patch, because these are more common alignment values.

---
 rust/kernel/sizes.rs | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
rust/kernel/sizes.rs +20 -0
index 521b2b38bfe7..7d03361eae3d 100644
@@ -35,6 +35,26 @@
 macro_rules! define_sizes {
     ($($type:ty),* $(,)?) => {
         define_sizes!(@internal [$($type),*]
+            /// `0x0000_0001`.
+            SZ_1,
+            /// `0x0000_0002`.
+            SZ_2,
+            /// `0x0000_0004`.
+            SZ_4,
+            /// `0x0000_0008`.
+            SZ_8,
+            /// `0x0000_0010`.
+            SZ_16,
+            /// `0x0000_0020`.
+            SZ_32,
+            /// `0x0000_0040`.
+            SZ_64,
+            /// `0x0000_0080`.
+            SZ_128,
+            /// `0x0000_0100`.
+            SZ_256,
+            /// `0x0000_0200`.
+            SZ_512,
             /// `0x0000_0400`.
             SZ_1K,
             /// `0x0000_0800`.
Signature · 1 line
2.55.0
Acked-by on v9 3/9
#4 lore raw

Re: [PATCH v9 3/9] rust: sizes: add sub-1K size constants

Quoted 7 lines · Eliot Courtney

Add some more size constants, mirroring include/linux/sizes.h. This is useful for making Alignment implement SizeConstants in a following patch, because these are more common alignment values.

If this ends up going through bitmap or DRM or similar this cycle:

Otherwise, I can pick this one up this cycle.

Thanks!

Cheers,
Miguel

#5 lore raw

Re: [PATCH v9 3/9] rust: sizes: add sub-1K size constants

Hi Miguel,

Quoted 6 lines · Miguel Ojeda
Quoted 5 lines · Eliot Courtney

Add some more size constants, mirroring include/linux/sizes.h. This is useful for making Alignment implement SizeConstants in a following patch, because these are more common alignment values.

Quoted 8 lines
Quoted 2 lines

If this ends up going through bitmap or DRM or similar this cycle:

Otherwise, I can pick this one up this cycle.

I am considering picking the series through DRM, but it also has a dependency on the cv! macro [1].

Is this ok if we take the patch introducing cv! through DRM as well?

[1] https://lore.kernel.org/all/[email protected]/

#6 lore raw

Re: [PATCH v9 3/9] rust: sizes: add sub-1K size constants

Quoted 7 lines · Alexandre Courbot

I am considering picking the series through DRM, but it also has a dependency on the cv! macro [1].

Is this ok if we take the patch introducing cv! through DRM as well?

[1] https://lore.kernel.org/all/[email protected]/

I asked Gary yesterday if there was any more recent discussion on the tag thing from v3. Does anyone want to do some cleanups this cycle through rust-next with cv!?

Cheers,
Miguel

reviewer
#7 lore raw

Re: [PATCH v9 3/9] rust: sizes: add sub-1K size constants

Quoted 5 lines · Miguel Ojeda
Quoted 4 lines · Alexandre Courbot

I am considering picking the series through DRM, but it also has a dependency on the cv! macro [1].

Quoted 7 lines
Quoted 3 lines

Is this ok if we take the patch introducing cv! through DRM as well?

[1] https://lore.kernel.org/all/[email protected]/

I asked Gary yesterday if there was any more recent discussion on the tag thing from v3. Does anyone want to do some cleanups this cycle through rust-next with cv!?

Given close proximity to LPC probably not.

Best,
Gary

#8 lore raw

Re: [PATCH v9 3/9] rust: sizes: add sub-1K size constants

Quoted 10 lines · Gary Guo
Quoted 9 lines · Miguel Ojeda
Quoted 7 lines · Alexandre Courbot

I am considering picking the series through DRM, but it also has a dependency on the cv! macro [1].

Is this ok if we take the patch introducing cv! through DRM as well?

[1] https://lore.kernel.org/all/[email protected]/

Quoted 8 lines
Quoted 3 lines

I asked Gary yesterday if there was any more recent discussion on the tag thing from v3. Does anyone want to do some cleanups this cycle through rust-next with cv!?

Given close proximity to LPC probably not.

Best,
Gary

I have a cleanup series locally already, I was planning to send it next cycle.

#9 lore raw

PATCH v9 4/9 rust: sizes: implement SizeConstants for Alignment

In reply to Eliot Courtney

Currently, constructing an alignment is quite verbose [1]:

    Alignment::new::<8>()

It's unfortunate because it disincentivizes using it at interface boundaries. Implement SizeConstants for Alignment so we can write e.g. Alignment::SZ_8 instead.

---
 rust/kernel/sizes.rs | 27 ++++++++++++++++++++++++++-
 1 file changed, 26 insertions(+), 1 deletion(-)
rust/kernel/sizes.rs +26 -1
index 7d03361eae3d..188e00c2b8b4 100644
@@ -13,6 +13,11 @@
 //! these constants as [`u64`] (or [`u32`]) rather than [`usize`], because
 //! device address spaces are sized independently of the CPU pointer width.
 //!
+//! The trait is also implemented for [`Alignment`], providing each size as a
+//! compile-time validated alignment.
+//!
+//! [`Alignment`]: crate::ptr::Alignment
+//!
 //! # Examples
 //!
 //! ```
@@ -105,6 +110,7 @@ macro_rules! define_sizes {
     (@internal [$($type:ty),*] $($names_and_metas:tt)*) => {
         define_sizes!(@consts_and_trait $($names_and_metas)*);
         define_sizes!(@impls [$($type),*] $($names_and_metas)*);
+        define_sizes!(@impl_alignment $($names_and_metas)*);
     };
 
     (@consts_and_trait $($(#[$meta:meta])* $name:ident,)*) => {
@@ -119,13 +125,22 @@ macro_rules! define_sizes {
         /// choose the width that matches their hardware. All `SZ_*` values fit
         /// in a [`u32`], so all implementations are lossless.
         ///
+        /// Also implemented for [`Alignment`], providing each size as a
+        /// compile-time validated alignment.
+        ///
+        /// [`Alignment`]: crate::ptr::Alignment
+        ///
         /// # Examples
         ///
         /// ```
-        /// use kernel::sizes::SizeConstants;
+        /// use kernel::{
+        ///     ptr::Alignment,
+        ///     sizes::SizeConstants, //
+        /// };
         ///
         /// let gpu_heap = 14 * u64::SZ_1M;
         /// let mmio_window = u32::SZ_16M;
+        /// let page_align = Alignment::SZ_4K;
         /// ```
         pub trait SizeConstants {
             $(
@@ -137,6 +152,16 @@ pub trait SizeConstants {
 
     (@impls [] $($(#[$meta:meta])* $name:ident,)*) => {};
 
+    (@impl_alignment $($(#[$meta:meta])* $name:ident,)*) => {
+        impl SizeConstants for crate::ptr::Alignment {
+            $(
+                $(#[$meta])*
+                // A non-power-of-two constant will fail the build here if used.
+                const $name: Self = crate::ptr::Alignment::new::<{ self::$name }>();
+            )*
+        }
+    };
+
     (@impls [$first:ty $(, $rest:ty)*] $($(#[$meta:meta])* $name:ident,)*) => {
         impl SizeConstants for $first {
             $(
Signature · 1 line
2.55.0
Acked-by on v9 4/9
#10 lore raw

Re: [PATCH v9 4/9] rust: sizes: implement SizeConstants for Alignment

Quoted 8 lines · Eliot Courtney

Currently, constructing an alignment is quite verbose [1]:

    Alignment::new::<8>()

It's unfortunate because it disincentivizes using it at interface boundaries. Implement SizeConstants for Alignment so we can write e.g. Alignment::SZ_8 instead.

I don't find the former disincentivizing -- it is how I would expect to have to write such a call in general. We do have the other size constants already, and SZ_ is well-known, so the short form is fine too here, but I just wanted to mention that I don't think we should be scared of seeing colons and angle brackets. :)

If this ends up going through bitmap or DRM or similar this cycle:

Otherwise, I can pick this one up this cycle.

Thanks!

Cheers,
Miguel

#11 lore raw

PATCH v9 5/9 rust: use Alignment size constants

In reply to Eliot Courtney

Use SizeConstants for Alignment that are implemented now.

---
 drivers/gpu/nova-core/fb.rs           | 10 +++++-----
 drivers/gpu/nova-core/fb/hal/gb100.rs |  2 +-
 drivers/gpu/nova-core/fsp.rs          |  4 ++--
 drivers/gpu/nova-core/gsp/fw.rs       | 12 +++---------
 drivers/gpu/nova-core/vbios.rs        |  7 +++++--
 rust/kernel/gpu/buddy.rs              | 26 +++++++++++++-------------
 rust/kernel/io.rs                     |  5 +++--
 7 files changed, 32 insertions(+), 34 deletions(-)
drivers/gpu/nova-core/fb.rs +5 -5
index b0c0b8fe6008..5ceb7760c78e 100644
@@ -219,7 +219,7 @@ pub(crate) fn new(
         };
 
         let frts = {
-            const FRTS_DOWN_ALIGN: Alignment = Alignment::new::<SZ_128K>();
+            const FRTS_DOWN_ALIGN: Alignment = Alignment::SZ_128K;
             let frts_size: u64 = hal.frts_size();
             let frts_base = vga_workspace.start.align_down(FRTS_DOWN_ALIGN) - frts_size;
 
@@ -227,7 +227,7 @@ pub(crate) fn new(
         };
 
         let boot = {
-            const BOOTLOADER_DOWN_ALIGN: Alignment = Alignment::new::<SZ_4K>();
+            const BOOTLOADER_DOWN_ALIGN: Alignment = Alignment::SZ_4K;
             let bootloader_size = u64::from_safe_cast(gsp_fw.bootloader.ucode.size());
             let bootloader_base = (frts.start - bootloader_size).align_down(BOOTLOADER_DOWN_ALIGN);
 
@@ -235,7 +235,7 @@ pub(crate) fn new(
         };
 
         let fw_image = {
-            const FW_IMAGE_DOWN_ALIGN: Alignment = Alignment::new::<SZ_64K>();
+            const FW_IMAGE_DOWN_ALIGN: Alignment = Alignment::SZ_64K;
             let fw_image_size = u64::from_safe_cast(gsp_fw.size());
             let fw_image_addr = (boot.start - fw_image_size).align_down(FW_IMAGE_DOWN_ALIGN);
 
@@ -245,7 +245,7 @@ pub(crate) fn new(
         let (vf_partition_count, wpr2_heap_size) = wpr2_heap_params(chipset, vgpu_state, fb.end)?;
 
         let wpr2_heap = {
-            const WPR2_HEAP_DOWN_ALIGN: Alignment = Alignment::new::<SZ_1M>();
+            const WPR2_HEAP_DOWN_ALIGN: Alignment = Alignment::SZ_1M;
             let wpr2_heap_addr = fw_image
                 .start
                 .checked_sub(wpr2_heap_size)
@@ -256,7 +256,7 @@ pub(crate) fn new(
         };
 
         let wpr2 = {
-            const WPR2_DOWN_ALIGN: Alignment = Alignment::new::<SZ_1M>();
+            const WPR2_DOWN_ALIGN: Alignment = Alignment::SZ_1M;
             let wpr2_addr = (wpr2_heap.start - u64::from_safe_cast(size_of::<gsp::GspFwWprMeta>()))
                 .align_down(WPR2_DOWN_ALIGN);
 
drivers/gpu/nova-core/fb/hal/gb100.rs +1 -1
index 612f70333c11..37b2ae762df4 100644
@@ -80,7 +80,7 @@ fn write_sysmem_flush_page_gb100(hshub0: Mmio<'_, regs::Hshub0Registers>, addr:
 
 // This PMU reservation size is r570-specific.
 pub(super) const fn pmu_reserved_size_gb100() -> u32 {
-    cv!(const_align_up(SZ_8M + SZ_16M + SZ_4K, Alignment::new::<SZ_128K>()).unwrap())
+    cv!(const_align_up(SZ_8M + SZ_16M + SZ_4K, Alignment::SZ_128K).unwrap())
 }
 
 impl FbHal for Gb100 {
drivers/gpu/nova-core/fsp.rs +2 -2
index 3a4a10487f33..637a00b2c951 100644
@@ -20,7 +20,7 @@
         Alignable,
         Alignment, //
     },
-    sizes::SZ_2M,
+    sizes::SizeConstants,
     time::Delta,
     transmute::{
         AsBytes,
@@ -259,7 +259,7 @@ fn frts_vidmem_offset(hal: &dyn hal::FspHal, fb_info: &FbSizes) -> Result<u64> {
         if fb_info.pmu_reserved_size != 0 {
             offset = (offset + u64::from(fb_info.pmu_reserved_size))
                 // The 2 MiB alignment is r570-specific.
-                .align_up(Alignment::new::<SZ_2M>())
+                .align_up(Alignment::SZ_2M)
                 .ok_or(EINVAL)?;
         }
 
drivers/gpu/nova-core/gsp/fw.rs +3 -9
index 1b548bbf146e..b4866ad21f6e 100644
@@ -25,10 +25,7 @@
         Alignment,
         KnownSize, //
     },
-    sizes::{
-        SizeConstants,
-        SZ_128K, //
-    },
+    sizes::SizeConstants,
     transmute::{
         AsBytes,
         FromBytes, //
@@ -63,7 +60,7 @@
 enum GspFwHeapParams {}
 
 /// Minimum required alignment for the GSP heap.
-const GSP_HEAP_ALIGNMENT: Alignment = Alignment::new::<{ 1 << 20 }>();
+const GSP_HEAP_ALIGNMENT: Alignment = Alignment::SZ_1M;
 
 impl GspFwHeapParams {
     /// Returns the amount of GSP-RM heap memory used during GSP-RM boot and initialization (up to
@@ -207,10 +204,7 @@ pub(crate) fn from_ranges<'a>(
             bootBinOffset: ranges.boot.start,
             frtsOffset: ranges.frts.start,
             frtsSize: ranges.frts.len(),
-            gspFwWprEnd: ranges
-                .vga_workspace
-                .start
-                .align_down(Alignment::new::<SZ_128K>()),
+            gspFwWprEnd: ranges.vga_workspace.start.align_down(Alignment::SZ_128K),
             gspFwHeapVfPartitionCount: ranges.vf_partition_count,
             fbSize: ranges.fb.len(),
             vgaWorkspaceOffset: ranges.vga_workspace.start,
drivers/gpu/nova-core/vbios.rs +5 -2
index 9c214b9f4dd9..a2d2d170b501 100644
@@ -11,7 +11,10 @@
         Alignment, //
     },
     register,
-    sizes::SZ_4K,
+    sizes::{
+        SizeConstants,
+        SZ_4K, //
+    },
     sync::aref::ARef,
 };
 
@@ -291,7 +294,7 @@ fn next(&mut self) -> Option<Self::Item> {
 
         // Advance to next image (aligned to 512 bytes).
         self.current_offset += image_size;
-        self.current_offset = self.current_offset.align_up(Alignment::new::<512>())?;
+        self.current_offset = self.current_offset.align_up(Alignment::SZ_512)?;
 
         Some(Ok(full_image))
     }
rust/kernel/gpu/buddy.rs +13 -13
index d502ada6ebbd..691bb40629d4 100644
@@ -31,11 +31,11 @@
 //! let buddy = GpuBuddy::new(GpuBuddyParams {
 //!     base_offset: 0,
 //!     size: SZ_1G as u64,
-//!     chunk_size: Alignment::new::<SZ_4K>(),
+//!     chunk_size: Alignment::SZ_4K,
 //! })?;
 //!
 //! assert_eq!(buddy.size(), SZ_1G as u64);
-//! assert_eq!(buddy.chunk_size(), Alignment::new::<SZ_4K>());
+//! assert_eq!(buddy.chunk_size(), Alignment::SZ_4K);
 //! let initial_free = buddy.avail();
 //!
 //! // Allocate 16MB. Block lands at the top of the address range.
@@ -43,7 +43,7 @@
 //!     buddy.alloc_blocks(
 //!         GpuBuddyAllocMode::Simple,
 //!         SZ_16M as u64,
-//!         Alignment::new::<SZ_16M>(),
+//!         Alignment::SZ_16M,
 //!         GpuBuddyAllocFlags::default(),
 //!     ),
 //!     GFP_KERNEL,
@@ -74,14 +74,14 @@
 //! # let buddy = GpuBuddy::new(GpuBuddyParams {
 //! #     base_offset: 0,
 //! #     size: SZ_1G as u64,
-//! #     chunk_size: Alignment::new::<SZ_4K>(),
+//! #     chunk_size: Alignment::SZ_4K,
 //! # })?;
 //! # let initial_free = buddy.avail();
 //! let topdown = KBox::pin_init(
 //!     buddy.alloc_blocks(
 //!         GpuBuddyAllocMode::TopDown,
 //!         SZ_16M as u64,
-//!         Alignment::new::<SZ_16M>(),
+//!         Alignment::SZ_16M,
 //!         GpuBuddyAllocFlags::default(),
 //!     ),
 //!     GFP_KERNEL,
@@ -114,7 +114,7 @@
 //! # let buddy = GpuBuddy::new(GpuBuddyParams {
 //! #     base_offset: 0,
 //! #     size: SZ_1G as u64,
-//! #     chunk_size: Alignment::new::<SZ_4K>(),
+//! #     chunk_size: Alignment::SZ_4K,
 //! # })?;
 //! # let initial_free = buddy.avail();
 //! // Create fragmentation by allocating 4MB blocks at [0,4M) and [8M,12M).
@@ -122,7 +122,7 @@
 //!     buddy.alloc_blocks(
 //!         GpuBuddyAllocMode::Range(0..SZ_4M as u64),
 //!         SZ_4M as u64,
-//!         Alignment::new::<SZ_4M>(),
+//!         Alignment::SZ_4M,
 //!         GpuBuddyAllocFlags::default(),
 //!     ),
 //!     GFP_KERNEL,
@@ -133,7 +133,7 @@
 //!     buddy.alloc_blocks(
 //!         GpuBuddyAllocMode::Range(SZ_8M as u64..(SZ_8M + SZ_4M) as u64),
 //!         SZ_4M as u64,
-//!         Alignment::new::<SZ_4M>(),
+//!         Alignment::SZ_4M,
 //!         GpuBuddyAllocFlags::default(),
 //!     ),
 //!     GFP_KERNEL,
@@ -145,7 +145,7 @@
 //!     buddy.alloc_blocks(
 //!         GpuBuddyAllocMode::Range(0..SZ_16M as u64),
 //!         SZ_8M as u64,
-//!         Alignment::new::<SZ_4M>(),
+//!         Alignment::SZ_4M,
 //!         GpuBuddyAllocFlags::default(),
 //!     ),
 //!     GFP_KERNEL,
@@ -178,14 +178,14 @@
 //! let small = GpuBuddy::new(GpuBuddyParams {
 //!     base_offset: 0,
 //!     size: SZ_16M as u64,
-//!     chunk_size: Alignment::new::<SZ_4K>(),
+//!     chunk_size: Alignment::SZ_4K,
 //! })?;
 //!
 //! let _hole1 = KBox::pin_init(
 //!     small.alloc_blocks(
 //!         GpuBuddyAllocMode::Range(0..SZ_4M as u64),
 //!         SZ_4M as u64,
-//!         Alignment::new::<SZ_4M>(),
+//!         Alignment::SZ_4M,
 //!         GpuBuddyAllocFlags::default(),
 //!     ),
 //!     GFP_KERNEL,
@@ -195,7 +195,7 @@
 //!     small.alloc_blocks(
 //!         GpuBuddyAllocMode::Range(SZ_8M as u64..(SZ_8M + SZ_4M) as u64),
 //!         SZ_4M as u64,
-//!         Alignment::new::<SZ_4M>(),
+//!         Alignment::SZ_4M,
 //!         GpuBuddyAllocFlags::default(),
 //!     ),
 //!     GFP_KERNEL,
@@ -206,7 +206,7 @@
 //!     small.alloc_blocks(
 //!         GpuBuddyAllocMode::Simple,
 //!         SZ_8M as u64,
-//!         Alignment::new::<SZ_4M>(),
+//!         Alignment::SZ_4M,
 //!         GpuBuddyAllocFlag::Contiguous,
 //!     ),
 //!     GFP_KERNEL,
rust/kernel/io.rs +3 -2
index de8ef8e2aec4..5712478a5461 100644
@@ -19,7 +19,8 @@
     ptr::{
         Alignment,
         KnownSize, //
-    }, //
+    },
+    sizes::SizeConstants, //
 };
 
 #[cfg(CONFIG_HAS_IOMEM)]
@@ -90,7 +91,7 @@ pub fn ptr_try_from_raw_parts_mut(base: *mut u8, size: usize) -> Result<*mut Sel
 impl<const SIZE: usize> KnownSize for Region<SIZE> {
     const MIN_SIZE: usize = SIZE;
     // Alignment of 4 is the most common; different base types can be added once required.
-    const MIN_ALIGN: Alignment = Alignment::new::<4>();
+    const MIN_ALIGN: Alignment = Alignment::SZ_4;
 
     #[inline(always)]
     fn size(p: *const Self) -> usize {
Signature · 1 line
2.55.0
#12 lore raw

PATCH v9 6/9 rust: bitmap: add contiguous area operations

In reply to Eliot Courtney

Add bindings for area operations on bitmaps. Each one is made safe by adding some extra checks compared to the underlying C code (for example, checking bounds) and with additional checks to catch likely erroneous usage if CONFIG_RUST_BITMAP_HARDENED is on.

Add tests demonstrating the edge cases.

---
 rust/kernel/bitmap.rs | 241 +++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 239 insertions(+), 2 deletions(-)
rust/kernel/bitmap.rs +239 -2
index df5505ec7a96..23c2b43a98ac 100644
@@ -10,7 +10,11 @@
 use crate::bindings;
 #[cfg(not(CONFIG_RUST_BITMAP_HARDENED))]
 use crate::pr_err;
-use core::ptr::NonNull;
+use crate::ptr::Alignment;
+use core::{
+    num::NonZero,
+    ptr::NonNull, //
+};
 
 /// Represents a C bitmap. Wraps underlying C bitmap API.
 ///
@@ -525,13 +529,159 @@ pub fn next_zero_bit(&self, start: usize) -> Option<usize> {
             Some(index)
         }
     }
+
+    /// Finds a contiguous area of `nbits` zero bits at or after `start`, where the area plus
+    /// `align_offset` is aligned to `align`.
+    ///
+    /// Returns the bit index of the start of the area, or [`None`] if no such area fitting in
+    /// the bitmap exists.
+    ///
+    /// The returned index plus `align_offset` is a multiple of `align`.
+    ///
+    /// # Panics
+    ///
+    /// Panics if CONFIG_RUST_BITMAP_HARDENED is enabled and `start` is out of bounds.
+    #[inline]
+    pub fn next_zero_area_off(
+        &self,
+        start: usize,
+        nbits: NonZero<usize>,
+        align: Alignment,
+        align_offset: usize,
+    ) -> Option<usize> {
+        bitmap_assert!(
+            start < self.len(),
+            "`start` must be < {}, was {}",
+            self.len(),
+            start
+        );
+
+        let nr = u32::try_from(nbits.get()).ok()?;
+        let align_mask = align.as_usize() - 1;
+
+        // The C alignment and end arithmetic must not overflow, or it can read out of bounds.
+        // Overflow is only possible on 32-bit.
+        #[cfg(not(CONFIG_64BIT))]
+        align_mask
+            .checked_add(self.len())?
+            .checked_add(nbits.get())?;
+
+        // SAFETY: `bitmap_find_next_zero_area_off` is safe to use with an out of bounds `start`
+        // value and, given the overflow check above, never reads beyond `self.len()` bits.
+        let index = unsafe {
+            bindings::bitmap_find_next_zero_area_off(
+                self.as_ptr().cast_mut(),
+                self.len(),
+                start,
+                nr,
+                align_mask,
+                align_offset,
+            )
+        };
+
+        (index < self.len()).then_some(index)
+    }
+
+    /// Finds a contiguous area of `nbits` zero bits at or after `start`, aligned to `align`.
+    ///
+    /// Returns the bit index of the start of the area, or [`None`] if no such area fitting in
+    /// the bitmap exists.
+    ///
+    /// The returned index is a multiple of `align`.
+    ///
+    /// # Panics
+    ///
+    /// Panics if CONFIG_RUST_BITMAP_HARDENED is enabled and `start` is out of bounds.
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// use kernel::{
+    ///     alloc::{AllocError, flags::GFP_KERNEL},
+    ///     bitmap::BitmapVec,
+    ///     ptr::Alignment,
+    ///     sizes::SizeConstants, //
+    /// };
+    ///
+    /// let mut b = BitmapVec::new(64, GFP_KERNEL)?;
+    ///
+    /// assert_eq!(Some(0), b.next_zero_area(0, cv!(8), Alignment::SZ_1));
+    /// b.set(0, cv!(5));
+    /// assert_eq!(Some(5), b.next_zero_area(0, cv!(8), Alignment::SZ_1));
+    /// assert_eq!(Some(8), b.next_zero_area(0, cv!(8), Alignment::SZ_8));
+    /// assert_eq!(None, b.next_zero_area(0, cv!(65), Alignment::SZ_1));
+    /// # Ok::<(), AllocError>(())
+    /// ```
+    #[inline]
+    pub fn next_zero_area(
+        &self,
+        start: usize,
+        nbits: NonZero<usize>,
+        align: Alignment,
+    ) -> Option<usize> {
+        self.next_zero_area_off(start, nbits, align, 0)
+    }
+
+    /// Sets a contiguous area of `nbits` bits starting at `start`.
+    ///
+    /// If CONFIG_RUST_BITMAP_HARDENED is not enabled and the area `start..start + nbits` is out of
+    /// bounds, does nothing.
+    ///
+    /// # Panics
+    ///
+    /// Panics if CONFIG_RUST_BITMAP_HARDENED is enabled and the area `start..start + nbits` is out
+    /// of bounds.
+    #[inline]
+    pub fn set(&mut self, start: usize, nbits: NonZero<usize>) {
+        bitmap_assert_return!(
+            start
+                .checked_add(nbits.get())
+                .is_some_and(|end| end <= self.len()),
+            "Area `start..start + nbits` ({}..{}) must be within bounds {}",
+            start,
+            start.saturating_add(nbits.get()),
+            self.len()
+        );
+        // SAFETY: The area `start..start + nbits` is within bounds and a `Bitmap` is at most
+        // `i32::MAX` bits, so the casts are lossless.
+        unsafe { bindings::__bitmap_set(self.as_mut_ptr(), start as u32, nbits.get() as i32) };
+    }
+
+    /// Clears a contiguous area of `nbits` bits starting at `start`.
+    ///
+    /// If CONFIG_RUST_BITMAP_HARDENED is not enabled and the area `start..start + nbits` is out of
+    /// bounds, does nothing.
+    ///
+    /// # Panics
+    ///
+    /// Panics if CONFIG_RUST_BITMAP_HARDENED is enabled and the area `start..start + nbits` is out
+    /// of bounds.
+    #[inline]
+    pub fn clear(&mut self, start: usize, nbits: NonZero<usize>) {
+        bitmap_assert_return!(
+            start
+                .checked_add(nbits.get())
+                .is_some_and(|end| end <= self.len()),
+            "Area `start..start + nbits` ({}..{}) must be within bounds {}",
+            start,
+            start.saturating_add(nbits.get()),
+            self.len()
+        );
+        // SAFETY: The area `start..start + nbits` is within bounds and a `Bitmap` is at most
+        // `i32::MAX` bits, so the casts are lossless.
+        unsafe { bindings::__bitmap_clear(self.as_mut_ptr(), start as u32, nbits.get() as i32) };
+    }
 }
 
 #[cfg(CONFIG_RUST_BITMAP_KUNIT_TEST)]
 #[macros::kunit_tests(rust_kernel_bitmap)]
 mod tests {
     use super::*;
-    use kernel::alloc::flags::GFP_KERNEL;
+    use kernel::{
+        alloc::flags::GFP_KERNEL,
+        num::cv,
+        sizes::SizeConstants, //
+    };
 
     #[test]
     fn bitmap_borrow() {
@@ -642,4 +792,91 @@ fn bitmap_copy_and_extend() -> Result<(), AllocError> {
         assert_eq!(Some(17), long_bitmap.last_bit());
         Ok(())
     }
+
+    #[test]
+    fn bitmap_area_set_clear_find() -> Result<(), AllocError> {
+        let mut b = BitmapVec::new(128, GFP_KERNEL)?;
+
+        assert_eq!(Some(0), b.next_zero_area(0, cv!(5), Alignment::SZ_1));
+        b.set(0, cv!(5)); // Now contains {[0, 5)}.
+
+        assert_eq!(Some(0), b.next_bit(0));
+        assert_eq!(Some(4), b.next_bit(4));
+        assert_eq!(Some(5), b.next_zero_bit(0));
+        assert_eq!(Some(5), b.next_zero_area(0, cv!(5), Alignment::SZ_1));
+        assert_eq!(Some(8), b.next_zero_area(0, cv!(5), Alignment::SZ_8));
+
+        b.set(8, cv!(8)); // Now contains {[0, 5), [8, 16)}.
+        assert_eq!(Some(16), b.next_zero_area(0, cv!(4), Alignment::SZ_16));
+        assert_eq!(Some(16), b.next_zero_area(0, cv!(4), Alignment::SZ_1));
+
+        b.clear(0, cv!(5)); // Now contains {[8, 16)}.
+        assert_eq!(Some(0), b.next_zero_area(0, cv!(5), Alignment::SZ_1));
+        assert_eq!(Some(8), b.next_bit(0));
+        assert_eq!(Some(15), b.last_bit());
+
+        b.set(60, cv!(10)); // Now contains {[8, 16), [60, 70)}.
+        assert_eq!(Some(60), b.next_bit(16));
+        assert_eq!(Some(69), b.last_bit());
+        assert_eq!(Some(16), b.next_zero_area(9, cv!(40), Alignment::SZ_1));
+        assert_eq!(Some(70), b.next_zero_area(0, cv!(45), Alignment::SZ_1));
+
+        b.clear(62, cv!(6)); // Now contains {[8, 16), [60, 62), [68, 70)}.
+        assert_eq!(Some(62), b.next_zero_area(60, cv!(6), Alignment::SZ_1));
+        assert_eq!(Some(61), b.next_bit(61));
+        assert_eq!(Some(69), b.last_bit());
+        Ok(())
+    }
+
+    #[test]
+    fn bitmap_area_exhaustion() -> Result<(), AllocError> {
+        let mut b = BitmapVec::new(64, GFP_KERNEL)?;
+
+        assert_eq!(None, b.next_zero_area(0, cv!(65), Alignment::SZ_1));
+        assert_eq!(None, b.next_zero_area(0, cv!(usize::MAX), Alignment::SZ_1));
+        assert_eq!(None, b.next_zero_area(1, cv!(usize::MAX), Alignment::SZ_1));
+
+        b.set_bit(0); // Now contains {[0, 1)}.
+        assert_eq!(None, b.next_zero_area(0, cv!(usize::MAX), Alignment::SZ_1));
+
+        b.set(0, cv!(61)); // Now contains {[0, 61)}.
+        assert_eq!(None, b.next_zero_area(0, cv!(4), Alignment::SZ_1));
+        assert_eq!(Some(61), b.next_zero_area(0, cv!(3), Alignment::SZ_1));
+        assert_eq!(None, b.next_zero_area(0, cv!(1), Alignment::SZ_64));
+        Ok(())
+    }
+
+    #[test]
+    fn bitmap_area_off() -> Result<(), AllocError> {
+        let mut b = BitmapVec::new(64, GFP_KERNEL)?;
+
+        b.set(0, cv!(5)); // Now contains {[0, 5)}.
+
+        // The area plus align_offset starts at a multiple of the alignment.
+        assert_eq!(Some(7), b.next_zero_area_off(0, cv!(8), Alignment::SZ_8, 1));
+        assert_eq!(Some(5), b.next_zero_area_off(0, cv!(8), Alignment::SZ_8, 3));
+
+        // A zero offset behaves like next_zero_area().
+        assert_eq!(
+            b.next_zero_area(0, cv!(8), Alignment::SZ_8),
+            b.next_zero_area_off(0, cv!(8), Alignment::SZ_8, 0)
+        );
+        Ok(())
+    }
+
+    #[test]
+    #[cfg(not(CONFIG_RUST_BITMAP_HARDENED))]
+    fn owned_bitmap_area_out_of_bounds() -> Result<(), AllocError> {
+        let mut b = BitmapVec::new(64, GFP_KERNEL)?;
+
+        // Should be ignored since out of bounds.
+        b.set(64, cv!(4));
+        b.set(62, cv!(8));
+        b.set(usize::MAX, cv!(1));
+        b.clear(usize::MAX, cv!(1));
+        b.clear(2048, cv!(8));
+        assert_eq!(None, b.next_bit(0));
+        assert_eq!(None, b.next_zero_area(64, cv!(1), Alignment::SZ_1));
+        Ok(())
+    }
 }
Signature · 1 line
2.55.0
Reviewed-by on v9 6/9
#13 lore raw

Re: [PATCH v9 6/9] rust: bitmap: add contiguous area operations

Quoted 153 lines · Eliot Courtney

Add bindings for area operations on bitmaps. Each one is made safe by adding some extra checks compared to the underlying C code (for example, checking bounds) and with additional checks to catch likely erroneous usage if CONFIG_RUST_BITMAP_HARDENED is on.

Add tests demonstrating the edge cases.

---
 rust/kernel/bitmap.rs | 241 +++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 239 insertions(+), 2 deletions(-)
rust/kernel/bitmap.rs +124 -1
index df5505ec7a96..23c2b43a98ac 100644
@@ -10,7 +10,11 @@
 use crate::bindings;
 #[cfg(not(CONFIG_RUST_BITMAP_HARDENED))]
 use crate::pr_err;
-use core::ptr::NonNull;
+use crate::ptr::Alignment;
+use core::{
+    num::NonZero,
+    ptr::NonNull, //
+};
 
 /// Represents a C bitmap. Wraps underlying C bitmap API.
 ///
@@ -525,13 +529,159 @@ pub fn next_zero_bit(&self, start: usize) -> Option<usize> {
             Some(index)
         }
     }
+
+    /// Finds a contiguous area of `nbits` zero bits at or after `start`, where the area plus
+    /// `align_offset` is aligned to `align`.
+    ///
+    /// Returns the bit index of the start of the area, or [`None`] if no such area fitting in
+    /// the bitmap exists.
+    ///
+    /// The returned index plus `align_offset` is a multiple of `align`.
+    ///
+    /// # Panics
+    ///
+    /// Panics if CONFIG_RUST_BITMAP_HARDENED is enabled and `start` is out of bounds.
+    #[inline]
+    pub fn next_zero_area_off(
+        &self,
+        start: usize,
+        nbits: NonZero<usize>,
+        align: Alignment,
+        align_offset: usize,
+    ) -> Option<usize> {
+        bitmap_assert!(
+            start < self.len(),
+            "`start` must be < {}, was {}",
+            self.len(),
+            start
+        );
+
+        let nr = u32::try_from(nbits.get()).ok()?;
+        let align_mask = align.as_usize() - 1;
+
+        // The C alignment and end arithmetic must not overflow, or it can read out of bounds.
+        // Overflow is only possible on 32-bit.
+        #[cfg(not(CONFIG_64BIT))]
+        align_mask
+            .checked_add(self.len())?
+            .checked_add(nbits.get())?;
+
+        // SAFETY: `bitmap_find_next_zero_area_off` is safe to use with an out of bounds `start`
+        // value and, given the overflow check above, never reads beyond `self.len()` bits.
+        let index = unsafe {
+            bindings::bitmap_find_next_zero_area_off(
+                self.as_ptr().cast_mut(),
+                self.len(),
+                start,
+                nr,
+                align_mask,
+                align_offset,
+            )
+        };
+
+        (index < self.len()).then_some(index)
+    }
+
+    /// Finds a contiguous area of `nbits` zero bits at or after `start`, aligned to `align`.
+    ///
+    /// Returns the bit index of the start of the area, or [`None`] if no such area fitting in
+    /// the bitmap exists.
+    ///
+    /// The returned index is a multiple of `align`.
+    ///
+    /// # Panics
+    ///
+    /// Panics if CONFIG_RUST_BITMAP_HARDENED is enabled and `start` is out of bounds.
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// use kernel::{
+    ///     alloc::{AllocError, flags::GFP_KERNEL},
+    ///     bitmap::BitmapVec,
+    ///     ptr::Alignment,
+    ///     sizes::SizeConstants, //
+    /// };
+    ///
+    /// let mut b = BitmapVec::new(64, GFP_KERNEL)?;
+    ///
+    /// assert_eq!(Some(0), b.next_zero_area(0, cv!(8), Alignment::SZ_1));
+    /// b.set(0, cv!(5));
+    /// assert_eq!(Some(5), b.next_zero_area(0, cv!(8), Alignment::SZ_1));
+    /// assert_eq!(Some(8), b.next_zero_area(0, cv!(8), Alignment::SZ_8));
+    /// assert_eq!(None, b.next_zero_area(0, cv!(65), Alignment::SZ_1));
+    /// # Ok::<(), AllocError>(())
+    /// ```
+    #[inline]
+    pub fn next_zero_area(
+        &self,
+        start: usize,
+        nbits: NonZero<usize>,
+        align: Alignment,
+    ) -> Option<usize> {
+        self.next_zero_area_off(start, nbits, align, 0)
+    }
+
+    /// Sets a contiguous area of `nbits` bits starting at `start`.
+    ///
+    /// If CONFIG_RUST_BITMAP_HARDENED is not enabled and the area `start..start + nbits` is out of
+    /// bounds, does nothing.
+    ///
+    /// # Panics
+    ///
+    /// Panics if CONFIG_RUST_BITMAP_HARDENED is enabled and the area `start..start + nbits` is out
+    /// of bounds.
+    #[inline]
+    pub fn set(&mut self, start: usize, nbits: NonZero<usize>) {
+        bitmap_assert_return!(
+            start
+                .checked_add(nbits.get())
+                .is_some_and(|end| end <= self.len()),
+            "Area `start..start + nbits` ({}..{}) must be within bounds {}",
+            start,
+            start.saturating_add(nbits.get()),
+            self.len()
+        );
+        // SAFETY: The area `start..start + nbits` is within bounds and a `Bitmap` is at most
+        // `i32::MAX` bits, so the casts are lossless.
+        unsafe { bindings::__bitmap_set(self.as_mut_ptr(), start as u32, nbits.get() as i32) };
+    }
+
+    /// Clears a contiguous area of `nbits` bits starting at `start`.

rust/kernel/bitmap.rs pub fn next_zero_bit(&self, start: usize) -> Option<usize> { · line 660

Quoted 10 lines
rust/kernel/bitmap.rs +10 -0
+    ///
+    /// If CONFIG_RUST_BITMAP_HARDENED is not enabled and the area `start..start + nbits` is out of
+    /// bounds, does nothing.
+    ///
+    /// # Panics
+    ///
+    /// Panics if CONFIG_RUST_BITMAP_HARDENED is enabled and the area `start..start + nbits` is out
+    /// of bounds.
+    #[inline]
+    pub fn clear(&mut self, start: usize, nbits: NonZero<usize>) {

I'm still not convinced about having this non-standard parameter types in function declaration, but ... let's give it a try.

The rest looks OK, so

Quoted 123 lines
rust/kernel/bitmap.rs +105 -1
+        bitmap_assert_return!(
+            start
+                .checked_add(nbits.get())
+                .is_some_and(|end| end <= self.len()),
+            "Area `start..start + nbits` ({}..{}) must be within bounds {}",
+            start,
+            start.saturating_add(nbits.get()),
+            self.len()
+        );
+        // SAFETY: The area `start..start + nbits` is within bounds and a `Bitmap` is at most
+        // `i32::MAX` bits, so the casts are lossless.
+        unsafe { bindings::__bitmap_clear(self.as_mut_ptr(), start as u32, nbits.get() as i32) };
+    }
 }
 
 #[cfg(CONFIG_RUST_BITMAP_KUNIT_TEST)]
 #[macros::kunit_tests(rust_kernel_bitmap)]
 mod tests {
     use super::*;
-    use kernel::alloc::flags::GFP_KERNEL;
+    use kernel::{
+        alloc::flags::GFP_KERNEL,
+        num::cv,
+        sizes::SizeConstants, //
+    };
 
     #[test]
     fn bitmap_borrow() {
@@ -642,4 +792,91 @@ fn bitmap_copy_and_extend() -> Result<(), AllocError> {
         assert_eq!(Some(17), long_bitmap.last_bit());
         Ok(())
     }
+
+    #[test]
+    fn bitmap_area_set_clear_find() -> Result<(), AllocError> {
+        let mut b = BitmapVec::new(128, GFP_KERNEL)?;
+
+        assert_eq!(Some(0), b.next_zero_area(0, cv!(5), Alignment::SZ_1));
+        b.set(0, cv!(5)); // Now contains {[0, 5)}.
+
+        assert_eq!(Some(0), b.next_bit(0));
+        assert_eq!(Some(4), b.next_bit(4));
+        assert_eq!(Some(5), b.next_zero_bit(0));
+        assert_eq!(Some(5), b.next_zero_area(0, cv!(5), Alignment::SZ_1));
+        assert_eq!(Some(8), b.next_zero_area(0, cv!(5), Alignment::SZ_8));
+
+        b.set(8, cv!(8)); // Now contains {[0, 5), [8, 16)}.
+        assert_eq!(Some(16), b.next_zero_area(0, cv!(4), Alignment::SZ_16));
+        assert_eq!(Some(16), b.next_zero_area(0, cv!(4), Alignment::SZ_1));
+
+        b.clear(0, cv!(5)); // Now contains {[8, 16)}.
+        assert_eq!(Some(0), b.next_zero_area(0, cv!(5), Alignment::SZ_1));
+        assert_eq!(Some(8), b.next_bit(0));
+        assert_eq!(Some(15), b.last_bit());
+
+        b.set(60, cv!(10)); // Now contains {[8, 16), [60, 70)}.
+        assert_eq!(Some(60), b.next_bit(16));
+        assert_eq!(Some(69), b.last_bit());
+        assert_eq!(Some(16), b.next_zero_area(9, cv!(40), Alignment::SZ_1));
+        assert_eq!(Some(70), b.next_zero_area(0, cv!(45), Alignment::SZ_1));
+
+        b.clear(62, cv!(6)); // Now contains {[8, 16), [60, 62), [68, 70)}.
+        assert_eq!(Some(62), b.next_zero_area(60, cv!(6), Alignment::SZ_1));
+        assert_eq!(Some(61), b.next_bit(61));
+        assert_eq!(Some(69), b.last_bit());
+        Ok(())
+    }
+
+    #[test]
+    fn bitmap_area_exhaustion() -> Result<(), AllocError> {
+        let mut b = BitmapVec::new(64, GFP_KERNEL)?;
+
+        assert_eq!(None, b.next_zero_area(0, cv!(65), Alignment::SZ_1));
+        assert_eq!(None, b.next_zero_area(0, cv!(usize::MAX), Alignment::SZ_1));
+        assert_eq!(None, b.next_zero_area(1, cv!(usize::MAX), Alignment::SZ_1));
+
+        b.set_bit(0); // Now contains {[0, 1)}.
+        assert_eq!(None, b.next_zero_area(0, cv!(usize::MAX), Alignment::SZ_1));
+
+        b.set(0, cv!(61)); // Now contains {[0, 61)}.
+        assert_eq!(None, b.next_zero_area(0, cv!(4), Alignment::SZ_1));
+        assert_eq!(Some(61), b.next_zero_area(0, cv!(3), Alignment::SZ_1));
+        assert_eq!(None, b.next_zero_area(0, cv!(1), Alignment::SZ_64));
+        Ok(())
+    }
+
+    #[test]
+    fn bitmap_area_off() -> Result<(), AllocError> {
+        let mut b = BitmapVec::new(64, GFP_KERNEL)?;
+
+        b.set(0, cv!(5)); // Now contains {[0, 5)}.
+
+        // The area plus align_offset starts at a multiple of the alignment.
+        assert_eq!(Some(7), b.next_zero_area_off(0, cv!(8), Alignment::SZ_8, 1));
+        assert_eq!(Some(5), b.next_zero_area_off(0, cv!(8), Alignment::SZ_8, 3));
+
+        // A zero offset behaves like next_zero_area().
+        assert_eq!(
+            b.next_zero_area(0, cv!(8), Alignment::SZ_8),
+            b.next_zero_area_off(0, cv!(8), Alignment::SZ_8, 0)
+        );
+        Ok(())
+    }
+
+    #[test]
+    #[cfg(not(CONFIG_RUST_BITMAP_HARDENED))]
+    fn owned_bitmap_area_out_of_bounds() -> Result<(), AllocError> {
+        let mut b = BitmapVec::new(64, GFP_KERNEL)?;
+
+        // Should be ignored since out of bounds.
+        b.set(64, cv!(4));
+        b.set(62, cv!(8));
+        b.set(usize::MAX, cv!(1));
+        b.clear(usize::MAX, cv!(1));
+        b.clear(2048, cv!(8));
+        assert_eq!(None, b.next_bit(0));
+        assert_eq!(None, b.next_zero_area(64, cv!(1), Alignment::SZ_1));
+        Ok(())
+    }
 }
Signature · 2 lines
-- 
2.55.0
#14 lore raw

PATCH v9 7/9 rust: id_pool: add contiguous ID reservation

In reply to Eliot Courtney

Add IdPool::reserve_ids which allocates a contiguous range with the given offset, count, and alignment.

---
 rust/kernel/id_pool.rs | 32 ++++++++++++++++++++++++++++++++
 1 file changed, 32 insertions(+)
rust/kernel/id_pool.rs +32 -0
index 384753fe0e44..06a4c71c4c6c 100644
@@ -4,8 +4,14 @@
 
 //! Rust API for an ID pool backed by a [`BitmapVec`].
 
+use core::{
+    num::NonZero,
+    ops::Range, //
+};
+
 use crate::alloc::{AllocError, Flags};
 use crate::bitmap::BitmapVec;
+use crate::ptr::Alignment;
 
 /// Represents a dynamic ID pool backed by a [`BitmapVec`].
 ///
@@ -240,6 +246,32 @@ pub fn find_unused_id(&mut self, offset: usize) -> Option<UnusedId<'_>> {
     pub fn release_id(&mut self, id: usize) {
         self.map.clear_bit(id);
     }
+
+    /// Reserves a contiguous area of `count` IDs at or after `offset`.
+    ///
+    /// The start of the returned area is a multiple of `align`.
+    ///
+    /// Returns the reserved range upon success, or [`None`] if no such area could be found.
+    #[inline]
+    #[must_use]
+    pub fn reserve_ids(
+        &mut self,
+        offset: usize,
+        count: NonZero<usize>,
+        align: Alignment,
+    ) -> Option<Range<usize>> {
+        let start = self.map.next_zero_area(offset, count, align)?;
+        self.map.set(start, count);
+        Some(start..start + count.get())
+    }
+
+    /// Releases a contiguous area of IDs.
+    #[inline]
+    pub fn release_ids(&mut self, range: &Range<usize>) {
+        if let Some(nbits) = NonZero::new(range.len()) {
+            self.map.clear(range.start, nbits);
+        }
+    }
 }
 
 /// Represents an unused id in an [`IdPool`].
Signature · 1 line
2.55.0
#15 lore raw

Re: [PATCH v9 7/9] rust: id_pool: add contiguous ID reservation

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] rust: id_pool: reserve_ids() and find_unused_id() panic when offset >= capacity --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] rust: id_pool: reserve_ids() and find_unused_id() panic when offset >= capacity --

commit aa26fdd2bd4ce814afc1e6755fd4a71b9333439f
Author: Eliot Courtney <[email protected]>

rust: id_pool: add contiguous ID reservation

Add IdPool::reserve_ids which allocates a contiguous range with the given offset, count, and alignment.

Quoted 4 lines
rust/kernel/id_pool.rs +0 -0
index 384753fe0e44a..06a4c71c4c6c9 100644

[ ... ]

Quoted 9 lines
rust/kernel/id_pool.rs +5 -0
@@ -240,6 +246,32 @@ pub fn find_unused_id(&mut self, offset: usize) -> Option<UnusedId<'_>> {
     pub fn release_id(&mut self, id: usize) {
         self.map.clear_bit(id);
     }
+
+    /// Reserves a contiguous area of `count` IDs at or after `offset`.
+    ///
+    /// The start of the returned area is a multiple of `align`.
+    ///

rust/kernel/id_pool.rs pub fn find_unused_id(&mut self, offset: usize) -> Option<UnusedId<'_>> { · line 263

Quoted 10 lines
rust/kernel/id_pool.rs +10 -0
+    /// Returns the reserved range upon success, or [`None`] if no such area could be found.
+    #[inline]
+    #[must_use]
+    pub fn reserve_ids(
+        &mut self,
+        offset: usize,
+        count: NonZero<usize>,
+        align: Alignment,
+    ) -> Option<Range<usize>> {
+        let start = self.map.next_zero_area(offset, count, align)?;

[Severity: High]
Does this code panic when offset is greater than or equal to pool.capacity()?

If a caller attempts to reserve an ID in an IdPool where the requested offset is greater than or equal to pool.capacity(), IdPool::reserve_ids() passes offset directly to BitmapVec::next_zero_area() without bounds validation.

This can trivially occur if a caller instantiates an empty pool (IdPool::with_capacity(0)) or resumes searching for an ID by passing offset = last_id + 1 when last_id was the last available ID.

The underlying BitmapVec::next_zero_area() function calls next_zero_area_off() in rust/kernel/bitmap.rs, which contains a bitmap_assert!(start < self.len()) that expands to a hard assert! under CONFIG_RUST_BITMAP_HARDENED, leading to a kernel panic (denial of service).

Since IdPool is documented to return None to prompt a grow_request when no IDs are available, failing to handle offset >= self.capacity() gracefully breaks the resizing API and causes panics on legitimate resource pool usage patterns.

It appears IdPool::find_unused_id() also shares this behavior when calling self.map.next_zero_bit(offset)? without bounds checking.

Quoted 12 lines
rust/kernel/id_pool.rs +11 -0
+        self.map.set(start, count);
+        Some(start..start + count.get())
+    }
+
+    /// Releases a contiguous area of IDs.
+    #[inline]
+    pub fn release_ids(&mut self, range: &Range<usize>) {
+        if let Some(nbits) = NonZero::new(range.len()) {
+            self.map.clear(range.start, nbits);
+        }
+    }
 }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
Reviewed-by on v9 7/9
#16 lore raw

Re: [PATCH v9 7/9] rust: id_pool: add contiguous ID reservation

In reply to Eliot Courtney
Quoted 6 lines · Eliot Courtney

Add IdPool::reserve_ids which allocates a contiguous range with the given offset, count, and alignment.

Quoted 59 lines
---
 rust/kernel/id_pool.rs | 32 ++++++++++++++++++++++++++++++++
 1 file changed, 32 insertions(+)
rust/kernel/id_pool.rs +32 -0
index 384753fe0e44..06a4c71c4c6c 100644
@@ -4,8 +4,14 @@
 
 //! Rust API for an ID pool backed by a [`BitmapVec`].
 
+use core::{
+    num::NonZero,
+    ops::Range, //
+};
+
 use crate::alloc::{AllocError, Flags};
 use crate::bitmap::BitmapVec;
+use crate::ptr::Alignment;
 
 /// Represents a dynamic ID pool backed by a [`BitmapVec`].
 ///
@@ -240,6 +246,32 @@ pub fn find_unused_id(&mut self, offset: usize) -> Option<UnusedId<'_>> {
     pub fn release_id(&mut self, id: usize) {
         self.map.clear_bit(id);
     }
+
+    /// Reserves a contiguous area of `count` IDs at or after `offset`.
+    ///
+    /// The start of the returned area is a multiple of `align`.
+    ///
+    /// Returns the reserved range upon success, or [`None`] if no such area could be found.
+    #[inline]
+    #[must_use]
+    pub fn reserve_ids(
+        &mut self,
+        offset: usize,
+        count: NonZero<usize>,
+        align: Alignment,
+    ) -> Option<Range<usize>> {
+        let start = self.map.next_zero_area(offset, count, align)?;
+        self.map.set(start, count);
+        Some(start..start + count.get())
+    }
+
+    /// Releases a contiguous area of IDs.
+    #[inline]
+    pub fn release_ids(&mut self, range: &Range<usize>) {
+        if let Some(nbits) = NonZero::new(range.len()) {
+            self.map.clear(range.start, nbits);
+        }
+    }
 }
 
 /// Represents an unused id in an [`IdPool`].
Signature · 2 lines
-- 
2.55.0
#17 lore raw

PATCH v9 8/9 rust: id_pool: do not round capacity up to BitmapVec::MAX_INLINE_LEN

In reply to Eliot Courtney

Current code in IdPool::with_capacity rounds the capacity up to BitmapVec::MAX_INLINE_LEN, but BitmapVec::new works fine with values smaller than this and still uses an inline representation. Remove this behaviour.

This allows specifying a real capacity of 0, which was not previously possible. This breaks grow_request in this case, so change it to grow to at least BitmapVec::MAX_INLINE_LEN, mirroring the capacity floor in shrink_request.

---
 rust/kernel/id_pool.rs | 38 ++++++++++++++++++++++++++++++++------
 1 file changed, 32 insertions(+), 6 deletions(-)
rust/kernel/id_pool.rs +32 -6
index 06a4c71c4c6c..4f329249df9d 100644
@@ -112,13 +112,8 @@ pub fn new() -> Self {
     }
 
     /// Constructs a new [`IdPool`] with space for a specific number of bits.
-    ///
-    /// A capacity below [`MAX_INLINE_LEN`] is adjusted to [`MAX_INLINE_LEN`].
-    ///
-    /// [`MAX_INLINE_LEN`]: BitmapVec::MAX_INLINE_LEN
     #[inline]
     pub fn with_capacity(num_ids: usize, flags: Flags) -> Result<Self, AllocError> {
-        let num_ids = usize::max(num_ids, BitmapVec::MAX_INLINE_LEN);
         let map = BitmapVec::new(num_ids, flags)?;
         Ok(Self { map })
     }
@@ -152,6 +147,13 @@ pub fn capacity(&self) -> usize {
     /// let resizer = alloc_request.realloc(GFP_KERNEL)?;
     /// pool.shrink(resizer);
     /// assert_eq!(pool.capacity(), BitmapVec::MAX_INLINE_LEN);
+    ///
+    /// // A pool at the `MAX_INLINE_LEN` floor cannot shrink further.
+    /// assert!(pool.shrink_request().is_none());
+    ///
+    /// // Neither can a pool with a capacity below `MAX_INLINE_LEN`.
+    /// let small = IdPool::with_capacity(8, GFP_KERNEL)?;
+    /// assert!(small.shrink_request().is_none());
     /// # Ok::<(), AllocError>(())
     /// ```
     #[inline]
@@ -198,12 +200,36 @@ pub fn shrink(&mut self, mut resizer: PoolResizer) {
 
     /// Returns a [`ReallocRequest`] for growing this [`IdPool`], if possible.
     ///
+    /// Grows to at least [`MAX_INLINE_LEN`].
     /// The capacity of an [`IdPool`] cannot be grown above [`MAX_LEN`].
     ///
+    /// [`MAX_INLINE_LEN`]: BitmapVec::MAX_INLINE_LEN
     /// [`MAX_LEN`]: BitmapVec::MAX_LEN
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// use kernel::{
+    ///     alloc::AllocError,
+    ///     bitmap::BitmapVec,
+    ///     id_pool::IdPool, //
+    /// };
+    ///
+    /// // Grow goes to at least BitmapVec::MAX_INLINE_LEN.
+    /// let mut pool = IdPool::with_capacity(0, GFP_KERNEL)?;
+    /// let resizer = pool.grow_request().ok_or(AllocError)?.realloc(GFP_KERNEL)?;
+    /// pool.grow(resizer);
+    /// assert_eq!(pool.capacity(), BitmapVec::MAX_INLINE_LEN);
+    ///
+    /// // Grow doubles if at least BitmapVec::MAX_INLINE_LEN.
+    /// let resizer = pool.grow_request().ok_or(AllocError)?.realloc(GFP_KERNEL)?;
+    /// pool.grow(resizer);
+    /// assert_eq!(pool.capacity(), 2 * BitmapVec::MAX_INLINE_LEN);
+    /// # Ok::<(), AllocError>(())
+    /// ```
     #[inline]
     pub fn grow_request(&self) -> Option<ReallocRequest> {
-        let num_ids = self.capacity() * 2;
+        let num_ids = usize::max(BitmapVec::MAX_INLINE_LEN, self.capacity() * 2);
         if num_ids > BitmapVec::MAX_LEN {
             return None;
         }
Signature · 1 line
2.55.0
#18 lore raw

Re: [PATCH v9 8/9] rust: id_pool: do not round capacity up to BitmapVec::MAX_INLINE_LEN

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] rust: id_pool: panic on find_unused_id with 0-capacity pool --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] rust: id_pool: panic on find_unused_id with 0-capacity pool
--

commit 424a508011ab0d65530fe9badebd792ee11ae3f5
Author: Eliot Courtney <[email protected]>

rust: id_pool: do not round capacity up to BitmapVec::MAX_INLINE_LEN

This patch modifies IdPool::with_capacity to no longer round up the initial capacity to BitmapVec::MAX_INLINE_LEN, allowing for a capacity of 0. It also adjusts grow_request to ensure that a 0-capacity pool grows to at least BitmapVec::MAX_INLINE_LEN instead of remaining 0.

Quoted 8 lines
rust/kernel/id_pool.rs +0 -0
index 06a4c71c4c6c9..4f329249df9d0 100644
@@ -112,13 +112,8 @@ pub fn new() -> Self {
     }
 
     /// Constructs a new [`IdPool`] with space for a specific number of bits.

rust/kernel/id_pool.rs pub fn new() -> Self { · line 119

Quoted 10 lines
rust/kernel/id_pool.rs +0 -5
-    ///
-    /// A capacity below [`MAX_INLINE_LEN`] is adjusted to [`MAX_INLINE_LEN`].
-    ///
-    /// [`MAX_INLINE_LEN`]: BitmapVec::MAX_INLINE_LEN
     #[inline]
     pub fn with_capacity(num_ids: usize, flags: Flags) -> Result<Self, AllocError> {
-        let num_ids = usize::max(num_ids, BitmapVec::MAX_INLINE_LEN);
         let map = BitmapVec::new(num_ids, flags)?;
         Ok(Self { map })
     }

[Severity: High]
Can allowing a capacity of 0 here introduce a regression where finding unused IDs triggers a kernel panic?

If a user initializes a 0-capacity pool and calls find_unused_id() or reserve_ids(), the offset is passed directly to the underlying Bitmap functions.

For example, in IdPool::find_unused_id():

rust/kernel/id_pool.rs:IdPool::find_unused_id() {
    ...
    Some(UnusedId {
        id: self.map.next_zero_bit(offset)?,
        pool: self,
    })
    ...
}

The underlying Bitmap::next_zero_bit() function enforces a bounds check via bitmap_assert!(start < self.len()).

For a 0-capacity pool with start=0, this evaluates to 0 < 0, triggering an unconditional panic when the CONFIG_RUST_BITMAP_HARDENED configuration is enabled.

Should we handle 0-capacity gracefully and return None to signal the need for a grow_request() instead?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=8
#19 lore raw

Re: [PATCH v9 8/9] rust: id_pool: do not round capacity up to BitmapVec::MAX_INLINE_LEN

In reply to Eliot Courtney
Quoted 58 lines · Eliot Courtney

Current code in IdPool::with_capacity rounds the capacity up to BitmapVec::MAX_INLINE_LEN, but BitmapVec::new works fine with values smaller than this and still uses an inline representation. Remove this behaviour.

This allows specifying a real capacity of 0, which was not previously possible. This breaks grow_request in this case, so change it to grow to at least BitmapVec::MAX_INLINE_LEN, mirroring the capacity floor in shrink_request.

---
 rust/kernel/id_pool.rs | 38 ++++++++++++++++++++++++++++++++------
 1 file changed, 32 insertions(+), 6 deletions(-)
rust/kernel/id_pool.rs +11 -5
index 06a4c71c4c6c..4f329249df9d 100644
@@ -112,13 +112,8 @@ pub fn new() -> Self {
     }
 
     /// Constructs a new [`IdPool`] with space for a specific number of bits.
-    ///
-    /// A capacity below [`MAX_INLINE_LEN`] is adjusted to [`MAX_INLINE_LEN`].
-    ///
-    /// [`MAX_INLINE_LEN`]: BitmapVec::MAX_INLINE_LEN
     #[inline]
     pub fn with_capacity(num_ids: usize, flags: Flags) -> Result<Self, AllocError> {
-        let num_ids = usize::max(num_ids, BitmapVec::MAX_INLINE_LEN);
         let map = BitmapVec::new(num_ids, flags)?;
         Ok(Self { map })
     }
@@ -152,6 +147,13 @@ pub fn capacity(&self) -> usize {
     /// let resizer = alloc_request.realloc(GFP_KERNEL)?;
     /// pool.shrink(resizer);
     /// assert_eq!(pool.capacity(), BitmapVec::MAX_INLINE_LEN);
+    ///
+    /// // A pool at the `MAX_INLINE_LEN` floor cannot shrink further.
+    /// assert!(pool.shrink_request().is_none());
+    ///
+    /// // Neither can a pool with a capacity below `MAX_INLINE_LEN`.
+    /// let small = IdPool::with_capacity(8, GFP_KERNEL)?;
+    /// assert!(small.shrink_request().is_none());
     /// # Ok::<(), AllocError>(())
     /// ```
     #[inline]
@@ -198,12 +200,36 @@ pub fn shrink(&mut self, mut resizer: PoolResizer) {
 
     /// Returns a [`ReallocRequest`] for growing this [`IdPool`], if possible.
     ///
+    /// Grows to at least [`MAX_INLINE_LEN`].
     /// The capacity of an [`IdPool`] cannot be grown above [`MAX_LEN`].
     ///
+    /// [`MAX_INLINE_LEN`]: BitmapVec::MAX_INLINE_LEN
     /// [`MAX_LEN`]: BitmapVec::MAX_LEN
+    ///
+    /// # Examples

rust/kernel/id_pool.rs pub fn shrink(&mut self, mut resizer: PoolResizer) { · line 219

Quoted 10 lines
rust/kernel/id_pool.rs +10 -0
+    ///
+    /// ```
+    /// use kernel::{
+    ///     alloc::AllocError,
+    ///     bitmap::BitmapVec,
+    ///     id_pool::IdPool, //
+    /// };
+    ///
+    /// // Grow goes to at least BitmapVec::MAX_INLINE_LEN.
+    /// let mut pool = IdPool::with_capacity(0, GFP_KERNEL)?;

Allocating a pool with 0-bit capacity is wrong. Please don't put it in the examples. I recall I pointed that this object would panic the kernel if, for example, you call pool.next_zero_bit(0) immediately after this. Sorry, but NAK.

This .with_capacity() should take num_ids: NonZero, after all...

Quoted 20 lines
rust/kernel/id_pool.rs +11 -1
+    /// let resizer = pool.grow_request().ok_or(AllocError)?.realloc(GFP_KERNEL)?;
+    /// pool.grow(resizer);
+    /// assert_eq!(pool.capacity(), BitmapVec::MAX_INLINE_LEN);
+    ///
+    /// // Grow doubles if at least BitmapVec::MAX_INLINE_LEN.
+    /// let resizer = pool.grow_request().ok_or(AllocError)?.realloc(GFP_KERNEL)?;
+    /// pool.grow(resizer);
+    /// assert_eq!(pool.capacity(), 2 * BitmapVec::MAX_INLINE_LEN);
+    /// # Ok::<(), AllocError>(())
+    /// ```
     #[inline]
     pub fn grow_request(&self) -> Option<ReallocRequest> {
-        let num_ids = self.capacity() * 2;
+        let num_ids = usize::max(BitmapVec::MAX_INLINE_LEN, self.capacity() * 2);
         if num_ids > BitmapVec::MAX_LEN {
             return None;
         }
Signature · 2 lines
-- 
2.55.0
#20 lore raw

Re: [PATCH v9 8/9] rust: id_pool: do not round capacity up to BitmapVec::MAX_INLINE_LEN

Quoted 75 lines · Yury Norov
Quoted 58 lines · Eliot Courtney

Current code in IdPool::with_capacity rounds the capacity up to BitmapVec::MAX_INLINE_LEN, but BitmapVec::new works fine with values smaller than this and still uses an inline representation. Remove this behaviour.

This allows specifying a real capacity of 0, which was not previously possible. This breaks grow_request in this case, so change it to grow to at least BitmapVec::MAX_INLINE_LEN, mirroring the capacity floor in shrink_request.

---
 rust/kernel/id_pool.rs | 38 ++++++++++++++++++++++++++++++++------
 1 file changed, 32 insertions(+), 6 deletions(-)
rust/kernel/id_pool.rs +11 -5
index 06a4c71c4c6c..4f329249df9d 100644
@@ -112,13 +112,8 @@ pub fn new() -> Self {
     }
 
     /// Constructs a new [`IdPool`] with space for a specific number of bits.
-    ///
-    /// A capacity below [`MAX_INLINE_LEN`] is adjusted to [`MAX_INLINE_LEN`].
-    ///
-    /// [`MAX_INLINE_LEN`]: BitmapVec::MAX_INLINE_LEN
     #[inline]
     pub fn with_capacity(num_ids: usize, flags: Flags) -> Result<Self, AllocError> {
-        let num_ids = usize::max(num_ids, BitmapVec::MAX_INLINE_LEN);
         let map = BitmapVec::new(num_ids, flags)?;
         Ok(Self { map })
     }
@@ -152,6 +147,13 @@ pub fn capacity(&self) -> usize {
     /// let resizer = alloc_request.realloc(GFP_KERNEL)?;
     /// pool.shrink(resizer);
     /// assert_eq!(pool.capacity(), BitmapVec::MAX_INLINE_LEN);
+    ///
+    /// // A pool at the `MAX_INLINE_LEN` floor cannot shrink further.
+    /// assert!(pool.shrink_request().is_none());
+    ///
+    /// // Neither can a pool with a capacity below `MAX_INLINE_LEN`.
+    /// let small = IdPool::with_capacity(8, GFP_KERNEL)?;
+    /// assert!(small.shrink_request().is_none());
     /// # Ok::<(), AllocError>(())
     /// ```
     #[inline]
@@ -198,12 +200,36 @@ pub fn shrink(&mut self, mut resizer: PoolResizer) {
 
     /// Returns a [`ReallocRequest`] for growing this [`IdPool`], if possible.
     ///
+    /// Grows to at least [`MAX_INLINE_LEN`].
     /// The capacity of an [`IdPool`] cannot be grown above [`MAX_LEN`].
     ///
+    /// [`MAX_INLINE_LEN`]: BitmapVec::MAX_INLINE_LEN
     /// [`MAX_LEN`]: BitmapVec::MAX_LEN
+    ///
+    /// # Examples

rust/kernel/id_pool.rs pub fn shrink(&mut self, mut resizer: PoolResizer) { · line 219

Quoted 10 lines
rust/kernel/id_pool.rs +10 -0
+    ///
+    /// ```
+    /// use kernel::{
+    ///     alloc::AllocError,
+    ///     bitmap::BitmapVec,
+    ///     id_pool::IdPool, //
+    /// };
+    ///
+    /// // Grow goes to at least BitmapVec::MAX_INLINE_LEN.
+    /// let mut pool = IdPool::with_capacity(0, GFP_KERNEL)?;

Allocating a pool with 0-bit capacity is wrong. Please don't put it in the examples. I recall I pointed that this object would panic the kernel if, for example, you call pool.next_zero_bit(0) immediately after this. Sorry, but NAK.

Quoted 1 line

This .with_capacity() should take num_ids: NonZero, after all...

This panic is not specific to the size zero, any size triggers the same behavior when accessed out of bounds. A size of zero has nothing special in that respect, so why make an exception and forbid it? We had this discussion some time ago [1][2], and I'd recommend instead making e.g. next_zero_bit return None on out-of-bounds accesses, which is semantically correct.

[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/

#21 lore raw

Re: [PATCH v9 8/9] rust: id_pool: do not round capacity up to BitmapVec::MAX_INLINE_LEN

Quoted 1 line · Alexandre Courbot

On Wed Sep 30, 2026 at 2:05 PM JST, Yury Norov wrote:

...

Quoted 9 lines
Quoted 6 lines

Allocating a pool with 0-bit capacity is wrong. Please don't put it in the examples. I recall I pointed that this object would panic the kernel if, for example, you call pool.next_zero_bit(0) immediately after this. Sorry, but NAK.

This .with_capacity() should take num_ids: NonZero, after all...

This panic is not specific to the size zero, any size triggers the same behavior when accessed out of bounds.

In C, malloc(0) is implementation defined behavior, i.e. it can return a pointer valid for free(), or NULL (which is also valid for free).

This is a very old legacy coming from K&R implementation, then rejected in C89, and later this all became an impl-def, mostly for compatibility reasons. See 7.20.3 in

https://www.open-std.org/jtc1/sc22/wg14/www/docs/n937.pdf

Rust adopted C bitmaps, thus creating 0-bit bitmap may go through, and hit that questionable behavior. You add this example without any discussion about all that possible complications, and with no protection for users.

Interestingly, you're doing it for the reason that has been considered a bad practice for over 30 years ago - malloc(0) with the immediate realloc(). See the above link for details.

To me it looks like pulling legacy with a potential of undefined behavior into Rust.

Bitmaps is a way more simple case than the generic malloc(). There's the only user of bitmaps - the Linux kernel, so we know exactly all users and their user patterns. I'm not aware of any in-tree user allocating 0-length bitmap for whatever reason, and such a coding style is highly unwelcome nowadays (30+ years).

When it comes to rust, things are even simpler. Rust has much stricter memory policy - no undefined behavior, no implementation-defined behavior is allowed, no 50-years old legacy has to be considered.

Rust community decided to take the existing in-kernel implementation of bitmaps written in C, for a reason. But with that it pulls all undefined and poorly defined behavior associate to C language. We did quite well spotting such places and fencing them with safety checks.

The 0-length bitmaps is just another case that should be resolved.

If you still think that you need 0-length bitmaps in Rust, can you please give the clear and thorough explanation why rust needs those 0-length bitmaps. Are there any in-kernel examples? Any language concepts requiring it? If not, it's still a NAK.

Quoted 5 lines

A size of zero has nothing special
in that respect, so why make an exception and forbid it? We had this discussion some time ago [1][2], and I'd recommend instead making e.g. next_zero_bit return None on out-of-bounds accesses, which is semantically correct.

No. out-of-bound access should panic because every caller of bitmap API knows the length of that bitmap.

But if you make that 0-length bitmap a valid case, we need to revisit every function and make sure it returns ENOENT or something instead of panicking. That, again, must be very well explained and justified, and all this has to be done before adding 0-length bitmap support in code and examples.

Thanks,
Yury

Quoted 2 lines

[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/

reviewer
#22 lore raw

Re: [PATCH v9 8/9] rust: id_pool: do not round capacity up to BitmapVec::MAX_INLINE_LEN

Quoted 29 lines · Yury Norov
Quoted 1 line · Alexandre Courbot

On Wed Sep 30, 2026 at 2:05 PM JST, Yury Norov wrote:

...

Quoted 9 lines
Quoted 6 lines

Allocating a pool with 0-bit capacity is wrong. Please don't put it in the examples. I recall I pointed that this object would panic the kernel if, for example, you call pool.next_zero_bit(0) immediately after this. Sorry, but NAK.

This .with_capacity() should take num_ids: NonZero, after all...

This panic is not specific to the size zero, any size triggers the same behavior when accessed out of bounds.

In C, malloc(0) is implementation defined behavior, i.e. it can return a pointer valid for free(), or NULL (which is also valid for free).

This is a very old legacy coming from K&R implementation, then rejected in C89, and later this all became an impl-def, mostly for compatibility reasons. See 7.20.3 in

https://www.open-std.org/jtc1/sc22/wg14/www/docs/n937.pdf

Rust adopted C bitmaps, thus creating 0-bit bitmap may go through, and hit that questionable behavior. You add this example without any discussion about all that possible complications, and with no protection for users.

Quoted 6 lines

Interestingly, you're doing it for the reason that has been considered a bad practice for over 30 years ago - malloc(0) with the immediate realloc(). See the above link for details.

To me it looks like pulling legacy with a potential of undefined behavior into Rust.

A bad historical design in C standard shouldn't be affecting a modern API design.

Special casing 0 as a size is bad, because all code would then need to care if something is 0 or not. Imagine that I have a code that want unconditionally put a generic piece of data to heap via KBox, I should be able to write KBox::new(data) directly without having to do if size_of::<T>() == 0. Special casing 0 break the nice code composition properties.

Rust standard library does not repeat the mistake in its allocation implementations (and same for our in-kernel allocator abstraction). All zero-sized allocation are well-defined to return a pointer that is valid for, well, 0 bytes.

In fact, in kernel we also did the sensible thing -- make 0 byte allocation defined. kmalloc(0) is not a bug and it'll return you ZERO_SIZE_PTR which you can then give to kfree. For krealloc, both reallocating from 0 to non-zero or the other way is defined.

Best,
Gary

#23 lore raw

Re: [PATCH v9 8/9] rust: id_pool: do not round capacity up to BitmapVec::MAX_INLINE_LEN

In reply to Yury Norov
Quoted 46 lines · Yury Norov
Quoted 1 line · Alexandre Courbot

On Wed Sep 30, 2026 at 2:05 PM JST, Yury Norov wrote:

...

Quoted 9 lines
Quoted 6 lines

Allocating a pool with 0-bit capacity is wrong. Please don't put it in the examples. I recall I pointed that this object would panic the kernel if, for example, you call pool.next_zero_bit(0) immediately after this. Sorry, but NAK.

This .with_capacity() should take num_ids: NonZero, after all...

This panic is not specific to the size zero, any size triggers the same behavior when accessed out of bounds.

In C, malloc(0) is implementation defined behavior, i.e. it can return a pointer valid for free(), or NULL (which is also valid for free).

This is a very old legacy coming from K&R implementation, then rejected in C89, and later this all became an impl-def, mostly for compatibility reasons. See 7.20.3 in

https://www.open-std.org/jtc1/sc22/wg14/www/docs/n937.pdf

Rust adopted C bitmaps, thus creating 0-bit bitmap may go through, and hit that questionable behavior. You add this example without any discussion about all that possible complications, and with no protection for users.

Interestingly, you're doing it for the reason that has been considered a bad practice for over 30 years ago - malloc(0) with the immediate realloc(). See the above link for details.

To me it looks like pulling legacy with a potential of undefined behavior into Rust.

Bitmaps is a way more simple case than the generic malloc(). There's the only user of bitmaps - the Linux kernel, so we know exactly all users and their user patterns. I'm not aware of any in-tree user allocating 0-length bitmap for whatever reason, and such a coding style is highly unwelcome nowadays (30+ years).

When it comes to rust, things are even simpler. Rust has much stricter memory policy - no undefined behavior, no implementation-defined behavior is allowed, no 50-years old legacy has to be considered.

Quoted 6 lines

Rust community decided to take the existing in-kernel implementation of bitmaps written in C, for a reason. But with that it pulls all undefined and poorly defined behavior associate to C language. We did quite well spotting such places and fencing them with safety checks.

The 0-length bitmaps is just another case that should be resolved.

As it turns out we would never perform a zero-sized malloc, even for a zero-sized bitmap. IdPool is backed by a BitmapVec, which up to usize::BITS uses an inline member as backing storage. So we would never call bitmap_zalloc with a value of 0, making the safety concern moot.

Quoted 5 lines

If you still think that you need 0-length bitmaps in Rust, can you please give the clear and thorough explanation why rust needs those 0-length bitmaps. Are there any in-kernel examples? Any language concepts requiring it? If not, it's still a NAK.

I don't know of an in-kernel example, but please look at the bitmap_vec_new test which has been here since the API was initially merged last year: the first thing it does is create a zero-sized bitmap.

It is also easy to imagine a user starting with an empty pool and growing it on-demand. Having the ability to create a zero-sized pool is convenient to avoid special-casing user code, and in this case I'd say expected, just like you can create a zero-sized vector. Again a size of zero does not trigger anything that a larger capacity cannot trigger, so I don't see a reason to forbid it.

Quoted 9 lines
Quoted 6 lines

A size of zero has nothing special
in that respect, so why make an exception and forbid it? We had this discussion some time ago [1][2], and I'd recommend instead making e.g. next_zero_bit return None on out-of-bounds accesses, which is semantically correct.

No. out-of-bound access should panic because every caller of bitmap API knows the length of that bitmap.

Right now out-of-bound accesses are allowed if CONFIG_RUST_BITMAP_HARDENED is not set. The Kconfig documentation for that option even says "if unsure, say N", which suggests that not panicking (and thus the behavior I described above for next_zero_bit) is the default. If out-of-bound accesses should panic, then the Kconfig does not reflect that.

Quoted 6 lines

But if you make that 0-length bitmap a valid case, we need to revisit every function and make sure it returns ENOENT or something instead of panicking. That, again, must be very well explained and justified, and all this has to be done before adding 0-length bitmap support in code and examples.

With hardening off, every function already behaves in that way, and existing user code is already written to not trigger that condition anyway (because of the possibility of a panic), so that would be a pretty innocuous change.

maintainer
#24 lore raw

Re: [PATCH v9 8/9] rust: id_pool: do not round capacity up to BitmapVec::MAX_INLINE_LEN

Quoted 11 lines · Alexandre Courbot
Quoted 4 lines · Yury Norov

If you still think that you need 0-length bitmaps in Rust, can you please give the clear and thorough explanation why rust needs those 0-length bitmaps. Are there any in-kernel examples? Any language concepts requiring it? If not, it's still a NAK.

I don't know of an in-kernel example, but please look at the bitmap_vec_new test which has been here since the API was initially merged last year: the first thing it does is create a zero-sized bitmap.

Well, yeah: I wanted the behavior that the implementation rounds up documented so it can be recognized as intentional. Later, the new_inline method was added, which is a clearer way to get the largest non-allocating BitmapVec.

I think the problem in this discussion is that we want to treat capacity like length, which is maybe suggested by the BitmapVec name and its len method.

With the minimum capacity behavior documented, it is as if we have a specification right now that no IdPool instances can ever have less capacity than BitmapVec::MAX_INLINE_LEN bits. This patch here proposes to change that.

It is not clear though why client code would be happy about *less capacity*?

In other words, why should the client of the IdPool have the right to control the *exact* initial capacity, even if it is below documented, free-of-charge minimum capacity?

The IdPool, despite being backed by a BitmapVec, is not meant to be used like a vector: it does not grow by itself but client code calls grow and shrink explicitly, and client code has no control over how much that growing and shrinking happens.

Like, you cannot grow to 65 and expect to get a capacity 65. So why insist on exact small sizes?

Quoted 6 lines

It is also easy to imagine a user starting with an empty pool and growing it on-demand. Having the ability to create a zero-sized pool is convenient to avoid special-casing user code, and in this case I'd say expected, just like you can create a zero-sized vector. Again a size of zero does not trigger anything that a larger capacity cannot trigger, so I don't see a reason to forbid it.

With an initial capacity of minimum MAX_INLINE_LEN capacity, it is not about forbidding, it is that the added capacity already comes for free.
You could say "not enabling" instead of "forbidding", but pretending that the capacity is somehow not there does not seem to buy us anything.

Do the other patches in this series need this, or can we just drop this patch? If they do, maybe a demonstration of how significant the special-case handling is that can be avoided with the exact small capacity could be part of the commit message?

Cheers,
Burak

#25 lore raw

Re: [PATCH v9 8/9] rust: id_pool: do not round capacity up to BitmapVec::MAX_INLINE_LEN

Hi Burak,

Quoted 27 lines · Burak Emir
Quoted 11 lines · Alexandre Courbot
Quoted 4 lines · Yury Norov

If you still think that you need 0-length bitmaps in Rust, can you please give the clear and thorough explanation why rust needs those 0-length bitmaps. Are there any in-kernel examples? Any language concepts requiring it? If not, it's still a NAK.

I don't know of an in-kernel example, but please look at the bitmap_vec_new test which has been here since the API was initially merged last year: the first thing it does is create a zero-sized bitmap.

Well, yeah: I wanted the behavior that the implementation rounds up documented so it can be recognized as intentional. Later, the new_inline method was added, which is a clearer way to get the largest non-allocating BitmapVec.

I think the problem in this discussion is that we want to treat capacity like length, which is maybe suggested by the BitmapVec name and its len method.

With the minimum capacity behavior documented, it is as if we have a specification right now that no IdPool instances can ever have less capacity than BitmapVec::MAX_INLINE_LEN bits. This patch here proposes to change that.

Quoted 5 lines

It is not clear though why client code would be happy about *less capacity*?

In other words, why should the client of the IdPool have the right to control the *exact* initial capacity, even if it is below documented, free-of-charge minimum capacity?

The ID pool is used in Nova to track the number of hardware channels, of which we have an exact, fixed number. For this use-case rounding up is not "free-of-charge", it just allows to allocate IDs that are invalid as there is no hardware resource to back them up.

Quoted 8 lines

The IdPool, despite being backed by a BitmapVec, is not meant to be used like a vector: it does not grow by itself but client code calls grow and shrink explicitly, and client code has no control over how much that growing and shrinking happens.

Like, you cannot grow to 65 and expect to get a capacity 65. So why insist on exact small sizes?

Actually with the current code you can call IdPool::with_capacity(65) and you will get exactly 65 IDs. And if you call grow the new capacity will be 65 * 2.

The point being that in the current code the initial capacity is exact for every value *except* those below MAX_INLINE_LEN, which doesn't come across as consistent. All this patch does is remove that exception for which I don't see a justification.

I think we are conflating capacity with length to some degree. Actually the code does exactly that:

  impl IdPool {
      pub fn capacity(&self) -> usize {
          self.map.len()
      }
  }
Quoted 8 lines
Quoted 7 lines

It is also easy to imagine a user starting with an empty pool and growing it on-demand. Having the ability to create a zero-sized pool is convenient to avoid special-casing user code, and in this case I'd say expected, just like you can create a zero-sized vector. Again a size of zero does not trigger anything that a larger capacity cannot trigger, so I don't see a reason to forbid it.

Quoted 6 lines

With an initial capacity of minimum MAX_INLINE_LEN capacity, it is not about forbidding, it is that the added capacity already comes for free.
You could say "not enabling" instead of "forbidding", but pretending that the capacity is somehow not there does not seem to buy us anything.

The capacity is not necessarily there when allocating IDs for finite resources. I suspect that's where the misunderstanding stems from, as Nova is using the ID pool for a new use-case. But I think we can agree that this use-case is not particularly specific and matches what one can expect from an ID allocator, so it makes sense to support it, especially as it doesn't affect other users (with_capacity has no caller in the tree at the moment).

Quoted 5 lines

Do the other patches in this series need this, or can we just drop this patch? If they do, maybe a demonstration of how significant the special-case handling is that can be avoided with the exact small capacity could be part of the commit message?

The next patch in this series needs this to implement ChannelIdPool. The alternative would be to track the actual length of the pool and return an error if IdPool returns an ID after that value, which is what v5 [1] was doing (see num_chids in the v5 code).

That's the special-case handling that this patch is designed to avoid, and I hope we can agree that it isn't a satisfying workaround, especially when the backing BitmapVec *does* support exact sizes and that artificial limitation is introduced by IdPool itself.

[1] https://lore.kernel.org/all/[email protected]/

maintainer
#26 lore raw

Re: [PATCH v9 8/9] rust: id_pool: do not round capacity up to BitmapVec::MAX_INLINE_LEN

HI Alexandre!

Quoted 33 lines · Alexandre Courbot

Hi Burak,

Quoted 29 lines · Burak Emir
Quoted 11 lines · Alexandre Courbot
Quoted 4 lines · Yury Norov

If you still think that you need 0-length bitmaps in Rust, can you please give the clear and thorough explanation why rust needs those 0-length bitmaps. Are there any in-kernel examples? Any language concepts requiring it? If not, it's still a NAK.

I don't know of an in-kernel example, but please look at the bitmap_vec_new test which has been here since the API was initially merged last year: the first thing it does is create a zero-sized bitmap.

Well, yeah: I wanted the behavior that the implementation rounds up documented so it can be recognized as intentional. Later, the new_inline method was added, which is a clearer way to get the largest non-allocating BitmapVec.

I think the problem in this discussion is that we want to treat capacity like length, which is maybe suggested by the BitmapVec name and its len method.

With the minimum capacity behavior documented, it is as if we have a specification right now that no IdPool instances can ever have less capacity than BitmapVec::MAX_INLINE_LEN bits. This patch here proposes to change that.

It is not clear though why client code would be happy about *less capacity*?

Quoted 8 lines
Quoted 3 lines

In other words, why should the client of the IdPool have the right to control the *exact* initial capacity, even if it is below documented, free-of-charge minimum capacity?

The ID pool is used in Nova to track the number of hardware channels, of which we have an exact, fixed number. For this use-case rounding up is not "free-of-charge", it just allows to allocate IDs that are invalid as there is no hardware resource to back them up.

This makes sense, and I'm glad you can reuse the code. The origin of the bitmap IdPool to isolate the Binder-specific spin-lock-compatible resizing functionality.
In the Binder use cases, an "allocated ID" corresponds to a bit that is set to 1.

Quoted 6 lines
Quoted 6 lines

The IdPool, despite being backed by a BitmapVec, is not meant to be used like a vector: it does not grow by itself but client code calls grow and shrink explicitly, and client code has no control over how much that growing and shrinking happens.

Quoted 6 lines
Quoted 2 lines

Like, you cannot grow to 65 and expect to get a capacity 65. So why insist on exact small sizes?

Actually with the current code you can call IdPool::with_capacity(65) and you will get exactly 65 IDs. And if you call grow the new capacity will be 65 * 2.

Touche, I was too anchored in my "IdPool is just for resizing" thinking. I meant, when starting from a no-allocation size that one only gets multiples of word length.

However, I do understand that you would like IdPool to track for sizes below 64, so ChannelIdPool does not have to.

Quoted 7 lines

The point being that in the current code the initial capacity is exact for every value *except* those below MAX_INLINE_LEN, which doesn't come across as consistent. All this patch does is remove that exception for which I don't see a justification.
I think we are conflating capacity with length to some degree. Actually the code does exactly that:

Quoted 6 lines
  impl IdPool {
      pub fn capacity(&self) -> usize {
          self.map.len()
      }
  }

Yes, the original IdPool is "conflated by design", since for Binder ID allocation means something different.
- "length" for BitmapVec is length of the underlying bitmap in bits.
- "capacity" in (binder-specific...) IdPool means "we can allocate this many IDs in total without resizing.

Quoted 15 lines
Quoted 8 lines
Quoted 7 lines

It is also easy to imagine a user starting with an empty pool and growing it on-demand. Having the ability to create a zero-sized pool is convenient to avoid special-casing user code, and in this case I'd say expected, just like you can create a zero-sized vector. Again a size of zero does not trigger anything that a larger capacity cannot trigger, so I don't see a reason to forbid it.

Quoted 6 lines

With an initial capacity of minimum MAX_INLINE_LEN capacity, it is not about forbidding, it is that the added capacity already comes for free.
You could say "not enabling" instead of "forbidding", but pretending that the capacity is somehow not there does not seem to buy us anything.

Quoted 7 lines

The capacity is not necessarily there when allocating IDs for finite resources. I suspect that's where the misunderstanding stems from, as Nova is using the ID pool for a new use-case. But I think we can agree that this use-case is not particularly specific and matches what one can expect from an ID allocator, so it makes sense to support it, especially as it doesn't affect other users (with_capacity has no caller in the tree at the moment).

It makes complete sense to support it.
IMO it is less of a misunderstanding, but a design decision to juggle the requirements.
This IdPool thing was not meant as a general-purpose ID allocator. But I do see the appeal to generality.

Quoted 11 lines
Quoted 5 lines

Do the other patches in this series need this, or can we just drop this patch? If they do, maybe a demonstration of how significant the special-case handling is that can be avoided with the exact small capacity could be part of the commit message?

The next patch in this series needs this to implement ChannelIdPool. The alternative would be to track the actual length of the pool and return an error if IdPool returns an ID after that value, which is what v5 [1] was doing (see num_chids in the v5 code).

Quoted 6 lines

That's the special-case handling that this patch is designed to avoid, and I hope we can agree that it isn't a satisfying workaround, especially when the backing BitmapVec *does* support exact sizes and that artificial limitation is introduced by IdPool itself.

[1] https://lore.kernel.org/all/[email protected]/

Thanks for the link.
To me, having IdPool support exact size seems nice-to-have.

We are changing the design in light of different requirements, it is a defensible but not critical generalization.
I was worried that we may need to have proper separate length and capacity fields (which contradicts the binder use case), but that is not the case.

I checked the grow_request and grow dance now, it seems that even with exact small sizes we avoid allocating a bitmap.
So I am not opposed. Like Yury, I find the 0 case slightly odd, but unlike Yury, I find that tolerable.

The phrasing as "dropping an artificial limitation" does not adequately reflect that IdPool was done for a specific use case where the design made sense and is not actually a limitation.

I wanted to highlight this perspective, because notwithstanding the name, IdPool was not meant to join the set of kernel's dedicated supported ID allocation mechanisms.

cheers,
Burak

#27 lore raw

Re: [PATCH v9 8/9] rust: id_pool: do not round capacity up to BitmapVec::MAX_INLINE_LEN

Hi Burak!

On Wed Oct 14, 2026 at 4:20 PM JST, Burak Emir wrote:
<...>

Quoted 32 lines
Quoted 11 lines
Quoted 5 lines

Do the other patches in this series need this, or can we just drop this patch? If they do, maybe a demonstration of how significant the special-case handling is that can be avoided with the exact small capacity could be part of the commit message?

The next patch in this series needs this to implement ChannelIdPool. The alternative would be to track the actual length of the pool and return an error if IdPool returns an ID after that value, which is what v5 [1] was doing (see num_chids in the v5 code).

Quoted 6 lines

That's the special-case handling that this patch is designed to avoid, and I hope we can agree that it isn't a satisfying workaround, especially when the backing BitmapVec *does* support exact sizes and that artificial limitation is introduced by IdPool itself.

[1] https://lore.kernel.org/all/[email protected]/

Thanks for the link.
To me, having IdPool support exact size seems nice-to-have.

We are changing the design in light of different requirements, it is a defensible but not critical generalization.
I was worried that we may need to have proper separate length and capacity fields (which contradicts the binder use case), but that is not the case.

I checked the grow_request and grow dance now, it seems that even with exact small sizes we avoid allocating a bitmap.
So I am not opposed. Like Yury, I find the 0 case slightly odd, but unlike Yury, I find that tolerable.

Quoted 7 lines

The phrasing as "dropping an artificial limitation" does not adequately reflect that IdPool was done for a specific use case where the design made sense and is not actually a limitation.

I wanted to highlight this perspective, because notwithstanding the name, IdPool was not meant to join the set of kernel's dedicated supported ID allocation mechanisms.

Given its name and location, I guess it was inevitable. :) But thanks for clarifying its history, that helps understand the current design.

This also means we have two different use styles for this now:

- The binder style, where the pool grows as needed and we don't care about its exact size,
- The Nova (and likely others in the future) style, where we manage a set of finite resources that is not supposed to grow.

Which is what this patch tried to prepare for. Does it look acceptable to you? I think we still want to merge it or something close to it.

#28 lore raw

Re: [PATCH v9 8/9] rust: id_pool: do not round capacity up to BitmapVec::MAX_INLINE_LEN

In reply to Alexandre Courbot
Quoted 52 lines · Alexandre Courbot
Quoted 51 lines · Yury Norov
Quoted 1 line · Alexandre Courbot

On Wed Sep 30, 2026 at 2:05 PM JST, Yury Norov wrote:

...

Quoted 9 lines
Quoted 6 lines

Allocating a pool with 0-bit capacity is wrong. Please don't put it in the examples. I recall I pointed that this object would panic the kernel if, for example, you call pool.next_zero_bit(0) immediately after this. Sorry, but NAK.

This .with_capacity() should take num_ids: NonZero, after all...

This panic is not specific to the size zero, any size triggers the same behavior when accessed out of bounds.

In C, malloc(0) is implementation defined behavior, i.e. it can return a pointer valid for free(), or NULL (which is also valid for free).

This is a very old legacy coming from K&R implementation, then rejected in C89, and later this all became an impl-def, mostly for compatibility reasons. See 7.20.3 in

https://www.open-std.org/jtc1/sc22/wg14/www/docs/n937.pdf

Rust adopted C bitmaps, thus creating 0-bit bitmap may go through, and hit that questionable behavior. You add this example without any discussion about all that possible complications, and with no protection for users.

Interestingly, you're doing it for the reason that has been considered a bad practice for over 30 years ago - malloc(0) with the immediate realloc(). See the above link for details.

To me it looks like pulling legacy with a potential of undefined behavior into Rust.

Bitmaps is a way more simple case than the generic malloc(). There's the only user of bitmaps - the Linux kernel, so we know exactly all users and their user patterns. I'm not aware of any in-tree user allocating 0-length bitmap for whatever reason, and such a coding style is highly unwelcome nowadays (30+ years).

When it comes to rust, things are even simpler. Rust has much stricter memory policy - no undefined behavior, no implementation-defined behavior is allowed, no 50-years old legacy has to be considered.

Rust community decided to take the existing in-kernel implementation of bitmaps written in C, for a reason. But with that it pulls all undefined and poorly defined behavior associate to C language. We did quite well spotting such places and fencing them with safety checks.

Quoted 7 lines
Quoted 1 line

The 0-length bitmaps is just another case that should be resolved.

As it turns out we would never perform a zero-sized malloc, even for a zero-sized bitmap. IdPool is backed by a BitmapVec, which up to usize::BITS uses an inline member as backing storage. So we would never call bitmap_zalloc with a value of 0, making the safety concern moot.

OK. Then please remove the 0-length alloc with the following re-alloc example from the patch. use 1-bit allocation, as an extreme value, if needed.

Quoted 9 lines
Quoted 4 lines

If you still think that you need 0-length bitmaps in Rust, can you please give the clear and thorough explanation why rust needs those 0-length bitmaps. Are there any in-kernel examples? Any language concepts requiring it? If not, it's still a NAK.

I don't know of an in-kernel example, but please look at the bitmap_vec_new test which has been here since the API was initially merged last year: the first thing it does is create a zero-sized bitmap.

Quoted 6 lines

It is also easy to imagine a user starting with an empty pool and growing it on-demand. Having the ability to create a zero-sized pool is convenient to avoid special-casing user code, and in this case I'd say expected, just like you can create a zero-sized vector. Again a size of zero does not trigger anything that a larger capacity cannot trigger, so I don't see a reason to forbid it.

That's a weak argument. We can imagine pretty much everything, it doesn't mean we should support every idea in the code.

Supporting 0-sized bitmaps would simply move maintenance burden from end user of likely a poorly written logic to the core kernel subsystem.

Quoted 9 lines
Quoted 8 lines
Quoted 5 lines

A size of zero has nothing special
in that respect, so why make an exception and forbid it? We had this discussion some time ago [1][2], and I'd recommend instead making e.g. next_zero_bit return None on out-of-bounds accesses, which is semantically correct.

No. out-of-bound access should panic because every caller of bitmap API knows the length of that bitmap.

Quoted 6 lines

Right now out-of-bound accesses are allowed if CONFIG_RUST_BITMAP_HARDENED is not set. The Kconfig documentation for that option even says "if unsure, say N", which suggests that not panicking (and thus the behavior I described above for next_zero_bit) is the default. If out-of-bound accesses should panic, then the Kconfig does not reflect that.

Whoa? The panic is under config because it has non-zero performance impact. If it was completely free, it would be unconditional, like some compile-time checks out there. The "N" suggestion is for admins and users who build their own kernels. If you're a developer, and actively use bitmaps in your rust driver, you'd say "YES PLEASE".

Quoted 10 lines
Quoted 5 lines

But if you make that 0-length bitmap a valid case, we need to revisit every function and make sure it returns ENOENT or something instead of panicking. That, again, must be very well explained and justified, and all this has to be done before adding 0-length bitmap support in code and examples.

With hardening off, every function already behaves in that way, and existing user code is already written to not trigger that condition anyway (because of the possibility of a panic), so that would be a pretty innocuous change.

The rule of thumb is to enable all the hardening you can during development phase to ensure integrity, then disable it in production build to maximize performance. For the matter of this discussion, we'd consider the hardening enabled, of course.

Thanks,
Yury

#29 lore raw

Re: [PATCH v9 8/9] rust: id_pool: do not round capacity up to BitmapVec::MAX_INLINE_LEN

On Thu Oct 15, 2026 at 11:00 PM JST, Yury Norov wrote:
[...]

Quoted 9 lines
Quoted 5 lines

As it turns out we would never perform a zero-sized malloc, even for a zero-sized bitmap. IdPool is backed by a BitmapVec, which up to usize::BITS uses an inline member as backing storage. So we would never call bitmap_zalloc with a value of 0, making the safety concern moot.

OK. Then please remove the 0-length alloc with the following re-alloc example from the patch. use 1-bit allocation, as an extreme value, if needed.

Sure, I will update the example.

#30 lore raw

PATCH v9 9/9 gpu: nova-core: add ChannelIdPool

In reply to Eliot Courtney

Add ChannelIdPool which adds automatic tracking and releasing of channel IDs on top of IdPool. This is necessary for apportioning ranges of channel IDs to be used in e.g. vGPU.

Channel IDs are allocated as a contiguous sequence with a specific length and sometimes a specific alignment [1] for vGPU. The ID space is small (limited to 2048) and allocation is not on a hot path, so a bitmap-backed IdPool is a better fit than IDA/xarray (which allocate a single ID within a range, not a contiguous sequence) or a maple tree (where aligned allocation needs an alloc_range()+erase() retry loop that essentially reimplements bitmap_find_next_zero_area()) [2]. It is also faster than maple tree [3].

---
 drivers/gpu/nova-core/gpu.rs         |   2 +
 drivers/gpu/nova-core/gpu/channel.rs | 198 +++++++++++++++++++++++++++++++++++
 2 files changed, 200 insertions(+)
drivers/gpu/nova-core/gpu.rs +2 -0
index fb6f8a86a503..0571d7c9fcd3 100644
@@ -47,6 +47,8 @@
     vgpu::VgpuManager, //
 };
 
+#[cfg_attr(not(CONFIG_KUNIT = "y"), expect(dead_code))]
+mod channel;
 mod hal;
 mod regs;
 
drivers/gpu/nova-core/gpu/channel.rs new file +198 -0

198 lines not on this page · raw message

Signature · 1 line
2.55.0
#31 lore raw
In reply to Eliot Courtney

On Wed Sep 30, 2026 at 11:42 AM JST, Eliot Courtney wrote:
<...>

Quoted 10 lines

Eliot Courtney (9):
rust: bitmap: use function-level cfg on kunit test
rust: bitmap: restrict bitmap length to at most i32::MAX
rust: sizes: add sub-1K size constants
rust: sizes: implement SizeConstants for Alignment
rust: use Alignment size constants
rust: bitmap: add contiguous area operations
rust: id_pool: add contiguous ID reservation
rust: id_pool: do not round capacity up to BitmapVec::MAX_INLINE_LEN
gpu: nova-core: add ChannelIdPool

I am wondering about the merge path for this series (and the cv one). We would like to use it for 7.4 in drm-rust-next, but given the current timing we can either merge it there after -rc1 is tagged, or take the bits that apply on nust-next now.

The bits that apply being:

- Patch 1 of the cv! series [1],
- Patches 1-4 and 6-7 of this series.

(patch 5 touches mostly nova-core and doesn't apply on rust-next, patch 8 lacks the required tags, and patch 9 is a nova-core patch)

Since that's quite a bit of code outside of nova-core, maybe it is better to merge the non-nova bits mentioned above through the Rust tree in time for the 7.4 merge window. All the patches I have listed should have the required tags, but the cv! patch needs one last respin to address the remaining comments and add a "Pitfalls" section at the very least. Miguel, does that sound doable?

[1] https://lore.kernel.org/all/[email protected]/

#32 lore raw
Quoted 7 lines · Alexandre Courbot

Since that's quite a bit of code outside of nova-core, maybe it is better to merge the non-nova bits mentioned above through the Rust tree in time for the 7.4 merge window. All the patches I have listed should have the required tags, but the cv! patch needs one last respin to address the remaining comments and add a "Pitfalls" section at the very least. Miguel, does that sound doable?

These things have been around for quite a while, so yeah, if we can land part of this now, then we should definitely try. :)

The "Pitfalls" section can wait, if that simplifies the discussion/review, since the rest are nits.

Cheers,
Miguel

#33 lore raw
In reply to Eliot Courtney
Quoted 24 lines · Eliot Courtney

Add support for reserving of ranges of IDs, with a usage in nova-core for channel IDs. This entails adding bindings for the C bitmap API for ranges of bits, then users of that in IdPool, and finally a user of IdPool in nova-core, ChannelIdPool.

Channel ID tracking is needed for allotting ranges of channel IDs to vGPU guests, and later for regular host channel ID reservation. nova-core needs allocation of a contiguous sequence of IDs with a specific length and sometimes a specific alignment [1].

About the tradeoffs between different data structures:
- IDA/xarray do not support allocating a contiguous sequence of IDs (ida_alloc_range() allocates a single ID within a range, not a contiguous sequence).
- A maple tree works, but is not as good a fit. The ID space is small (limited to 2048) and aligned allocation needs an alloc_range()+erase() retry loop (plus a Mutex around it, or new mas_empty_area() bindings) that essentially reimplements bitmap_find_next_zero_area(). See the maple tree version at [2]. For 2048 IDs a bitmap is also considerably faster and smaller [3].
- The bitmap API natively supports aligned contiguous area allocation (bitmap_find_next_zero_area()).

Quoted 8 lines

This is based on drm-rust-next and depends on the cv! series [4].

[1]: https://lore.kernel.org/all/[email protected]/
[2]: https://lore.kernel.org/all/[email protected]/
[3]: https://lore.kernel.org/all/[email protected]/
[4]: https://lore.kernel.org/all/[email protected]/

Applied (the following subset that Alexandre suggested in a sibling reply) to rust-next -- thanks everyone!

Quoted 6 lines

rust: bitmap: use function-level cfg on kunit test
rust: bitmap: restrict bitmap length to at most i32::MAX
rust: sizes: add sub-1K size constants
rust: sizes: implement SizeConstants for Alignment
rust: bitmap: add contiguous area operations
rust: id_pool: add contiguous ID reservation

If someone does not want them to be applied for some reason, please shout.

For the second one:

Cc: [email protected]
[ Added a few intra-doc links. Added backticks to C types. Capitalize
tagged comments. - Miguel ]

By the way, I didn't modify the patch for the following, but we should migrate id_pool.rs to the vertical kernel style. Also, before I forget, we can clean up a few consistency bits in bitmap.rs before we keep extending it (like in one patch here) -- I can copy this to a "good first issue" or I may just send it:

- Fix the broken intra-doc link for c_ulong.
- Add backticks around configs like CONFIG_RUST_BITMAP_HARDENED.
- Capitalize tagged comments like // INVARIANT: ....
- Make periods consistent (adding them) at the end of bullet points.

Cheers,
Miguel