rust: Add support for reserving of ranges of IDs
Subsystems (6)
v12 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.
The bitmap and IdPool parts from earlier versions are in v7.4-rc1 now, and this version has the remaining patches: exact IdPool capacities and 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.
[1]: https://lore.kernel.org/all/[email protected]/
[2]: https://lore.kernel.org/all/[email protected]/
[3]: https://lore.kernel.org/all/[email protected]/
---
Changes in v12:
- Drop the patches that were applied to drm-rust-next and driver-core
- Rebase on drm-rust-next
- Drop the zero capacity part of the patch #1 commit message (Yury)
- Add reviewed-by tags
- Link to v11: https://patch.msgid.link/[email protected]
Changes in v11:
- Rebase on drm-rust-next (pre-reqs are merged now)
- Split patch #1 by subsystem (Danilo). Just splitting with no actual changes, so I kept the reviewed-by tags on the new patches
- Add reviewed-by tags
- Link to v10: https://patch.msgid.link/[email protected]
Changes in v10:
- Drop the patches that were applied to rust-next
- Rebase on drm-rust-next (plus pre-reqs)
- Use a capacity of 1 instead of 0 in the grow_request() example in patch #2 (Yury)
- Convert the remaining Alignment::new() users in nova-core in patch #1
- Link to v9: https://patch.msgid.link/[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 (2):
rust: id_pool: do not round capacity up to BitmapVec::MAX_INLINE_LEN
gpu: nova-core: add ChannelIdPool
drivers/gpu/nova-core/gpu.rs | 2 + drivers/gpu/nova-core/gpu/channel.rs | 198 +++++++++++++++++++++++++++++++++++ rust/kernel/id_pool.rs | 38 +++++-- 3 files changed, 232 insertions(+), 6 deletions(-)
---
base-commit: 6e557330b874b0e1fdd03e8fe4c438f36c93eefa
change-id: 20260608-chid-18fa943c6d6c
Best regards,
Signature · 1 line
Eliot Courtney <[email protected]>
PATCH v12 1/2 rust: id_pool: do not round capacity up to BitmapVec::MAX_INLINE_LEN
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.
Change grow_request to grow to at least BitmapVec::MAX_INLINE_LEN instead, 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
@@ -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(1, 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.56.0
PATCH v12 2/2 gpu: nova-core: add ChannelIdPool
In reply to Eliot CourtneyAdd 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
@@ -53,6 +53,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.56.0
Re: [PATCH v12 2/2] gpu: nova-core: add ChannelIdPool
Quoted 13 lines · Eliot Courtney
Add
ChannelIdPoolwhich adds automatic tracking and releasing of channel IDs on top ofIdPool. 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
IdPoolis 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 reimplementsbitmap_find_next_zero_area()) [2]. It is also faster than maple tree [3].
Quoted 5 lines
Reviewed-by Alice Ryhl <[email protected]>Signed-off-by Eliot Courtney <[email protected]>
This series looks ready to apply, but one question below:
drivers/gpu/nova-core/gpu/channel.rs line 88
Quoted 13 lines
drivers/gpu/nova-core/gpu/channel.rs new file +13
-0+#[kunit_tests(nova_core_channel)] +mod tests { +use super ::*; +use kernel::sizes::SizeConstants ; + +#[test] +fn chid_reservation () ->Result { +let pool =KBox ::pin_init (ChannelIdPool ::new (cv! (2048 )),GFP_KERNEL )?; + +let first = pool.reserve_ids (cv! (48 ),Alignment ::SZ_1 )?; +assert_eq! (0 , first.start); +assert_eq! (48 , first.len ()); +assert_eq! (48 , first.end);
I could simplify this into
assert_eq!(0..48, *first);
Quoted 13 lines
drivers/gpu/nova-core/gpu/channel.rs new file +13
-0+ +let second = pool.reserve_ids (cv! (48 ),Alignment ::SZ_1 )?; +assert! (first.end <= second.start || second.end <= first.start); + +let first_start = first.start; +drop (first); +assert_eq! ( + first_start, + pool.reserve_ids (cv! (48 ),Alignment ::SZ_1 )?.start + ); +Ok (()) + } +
drivers/gpu/nova-core/gpu/channel.rs line 111
Quoted 10 lines
drivers/gpu/nova-core/gpu/channel.rs new file +10
-0+#[test] +fn chid_reservation_drop () ->Result { +let pool =KBox ::pin_init (ChannelIdPool ::new (cv! (8 )),GFP_KERNEL )?; + +let a = pool.reserve_ids (cv! (3 ),Alignment ::SZ_1 )?; +let b = pool.reserve_ids (cv! (3 ),Alignment ::SZ_1 )?; +let c = pool.reserve_ids (cv! (2 ),Alignment ::SZ_1 )?; +assert_eq! (0 , a.start); +assert_eq! (3 , b.start); +assert_eq! (6 , c.start);
And these ones into:
assert_eq!(0..3, *a);
assert_eq!(3..6, *b);
assert_eq!(6..8, *c);Most of the tests could be changed to be done on ranges, which have the benefit of checking the first and last indices, as well as the length in one go and provide better coverage. I have done the change locally and checked that it worked. Would you be ok if I converted all the relevant assert_eq! statements upon applying?
Re: [PATCH v12 2/2] gpu: nova-core: add ChannelIdPool
Quoted 72 lines · Alexandre Courbot
Quoted 13 lines · Eliot Courtney
Add
ChannelIdPoolwhich adds automatic tracking and releasing of channel IDs on top ofIdPool. 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
IdPoolis 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 reimplementsbitmap_find_next_zero_area()) [2]. It is also faster than maple tree [3].Quoted 5 lines
Reviewed-by Alice Ryhl <[email protected]>Signed-off-by Eliot Courtney <[email protected]>Reviewed-by Alexandre Courbot <[email protected]>This series looks ready to apply, but one question below:
drivers/gpu/nova-core/gpu/channel.rs line 88
Quoted 13 lines
drivers/gpu/nova-core/gpu/channel.rs new file +13
-0+#[kunit_tests(nova_core_channel)] +mod tests { +use super ::*; +use kernel::sizes::SizeConstants ; + +#[test] +fn chid_reservation () ->Result { +let pool =KBox ::pin_init (ChannelIdPool ::new (cv! (2048 )),GFP_KERNEL )?; + +let first = pool.reserve_ids (cv! (48 ),Alignment ::SZ_1 )?; +assert_eq! (0 , first.start); +assert_eq! (48 , first.len ()); +assert_eq! (48 , first.end);I could simplify this into
assert_eq!(0..48, *first);Quoted 13 lines
drivers/gpu/nova-core/gpu/channel.rs new file +13
-0+ +let second = pool.reserve_ids (cv! (48 ),Alignment ::SZ_1 )?; +assert! (first.end <= second.start || second.end <= first.start); + +let first_start = first.start; +drop (first); +assert_eq! ( + first_start, + pool.reserve_ids (cv! (48 ),Alignment ::SZ_1 )?.start + ); +Ok (()) + } +drivers/gpu/nova-core/gpu/channel.rs line 111
Quoted 10 lines
drivers/gpu/nova-core/gpu/channel.rs new file +10
-0+#[test] +fn chid_reservation_drop () ->Result { +let pool =KBox ::pin_init (ChannelIdPool ::new (cv! (8 )),GFP_KERNEL )?; + +let a = pool.reserve_ids (cv! (3 ),Alignment ::SZ_1 )?; +let b = pool.reserve_ids (cv! (3 ),Alignment ::SZ_1 )?; +let c = pool.reserve_ids (cv! (2 ),Alignment ::SZ_1 )?; +assert_eq! (0 , a.start); +assert_eq! (3 , b.start); +assert_eq! (6 , c.start);And these ones into:
assert_eq!(0..3, *a); assert_eq!(3..6, *b); assert_eq!(6..8, *c);
Quoted 5 lines
Most of the tests could be changed to be done on ranges, which have the benefit of checking the first and last indices, as well as the length in one go and provide better coverage. I have done the change locally and checked that it worked. Would you be ok if I converted all the relevant
assert_eq!statements upon applying?
Yes absolutely no worries. Thanks!
Quoted 8 lines · Eliot Courtney
Add support for reserving of ranges of IDs, with a usage in nova-core for channel IDs.
The bitmap and
IdPoolparts from earlier versions are in v7.4-rc1 now, and this version has the remaining patches: exactIdPoolcapacities andChannelIdPool.[...]
Applied to drm-rust-next, thanks!
[1/2] rust: id_pool: do not round capacity up to BitmapVec::MAX_INLINE_LEN
commit: 8ea1ad9ac79575d0fb2d86b700b2db21d7149547
[2/2] gpu: nova-core: add ChannelIdPool
[acourbot: use range comparisons in tests for better coverage.]
commit: e825f4373d252339b6f66732d6f57427930bdc77
Quoted 25 lines · Eliot Courtney
Add support for reserving of ranges of IDs, with a usage in nova-core for channel IDs.
The bitmap and
IdPoolparts from earlier versions are in v7.4-rc1 now, and this version has the remaining patches: exactIdPoolcapacities andChannelIdPool.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 newmas_empty_area()bindings) that essentially reimplementsbitmap_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 1 line
This is based on drm-rust-next.
Quoted 3 lines
[1]: https://lore.kernel.org/all/[email protected]/
[2]: https://lore.kernel.org/all/[email protected]/
[3]: https://lore.kernel.org/all/[email protected]/
Maybe add this links in the commit message too?
Quoted 104 lines
Signed-off-by Eliot Courtney <[email protected]>---
Changes in v12:
- Drop the patches that were applied to drm-rust-next and driver-core
- Rebase on drm-rust-next
- Drop the zero capacity part of the patch #1 commit message (Yury)
- Add reviewed-by tags
- Link to v11: https://patch.msgid.link/[email protected]Changes in v11:
- Rebase on drm-rust-next (pre-reqs are merged now)
- Split patch #1 by subsystem (Danilo). Just splitting with no actual changes, so I kept the reviewed-by tags on the new patches
- Add reviewed-by tags
- Link to v10: https://patch.msgid.link/[email protected]Changes in v10:
- Drop the patches that were applied to rust-next
- Rebase on drm-rust-next (plus pre-reqs)
- Use a capacity of 1 instead of 0 in thegrow_request()example in patch #2 (Yury)
- Convert the remainingAlignment::new()users in nova-core in patch #1
- Link to v9: https://patch.msgid.link/[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, fixgrow_requestinstead (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 innext_zero_area_off,setandclear(Yury)
- RemoveUnusedArea,IdPool::alloc_areanow allocates directly (Yury)
- Add patch: take a NonZero capacity inIdPool::with_capacity()
- Add patch: do not round the capacity up toBitmapVec::MAX_INLINE_LEN, which also removes the bounds check inChannelIdPool::alloc_area
- Add more testing of Drop forChannelIdPool(Yury)
- Link to v5: https://patch.msgid.link/[email protected]Changes in v5:
-bitmap_assert!i32::MAXlength 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:
- Addnext_zero_area_offto match C code (Yury)
- Replace overflow checks to match C code in bitmap-for-next.
- TightenBitmapunsafe contract to disallow Bitmaps larger thani32::MAX
- Link to v3: https://patch.msgid.link/[email protected]Changes in v3:
- UseAlignmenttype inid_pooland 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_setetc 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 toChannelIdPool::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 (2):
rust:id_pool: do not round capacity up toBitmapVec::MAX_INLINE_LEN
gpu: nova-core: add ChannelIdPooldrivers/gpu/nova-core/gpu.rs | 2 + drivers/gpu/nova-core/gpu/channel.rs | 198 +++++++++++++++++++++++++++++++++++ rust/kernel/id_pool.rs | 38 +++++-- 3 files changed, 232 insertions(+), 6 deletions(-)---
base-commit:6e557330b874b0e1fdd03e8fe4c438f36c93eefa
change-id: 20260608-chid-18fa943c6d6cBest regards,
Signature · 1 line
Eliot Courtney <[email protected]>