Re: [PATCH v5 2/3] rust: alloc: add KUnit tests for KVVec shrink_to

From: Danilo Krummrich

Date: Sun Feb 15 2026 - 17:10:52 EST


On Sat Feb 14, 2026 at 9:35 PM CET, Shivam Kalra via B4 Relay wrote:
> diff --git a/rust/kernel/alloc/kvec.rs b/rust/kernel/alloc/kvec.rs
> index e7bc439538e4..0d5d69296a9f 100644
> --- a/rust/kernel/alloc/kvec.rs
> +++ b/rust/kernel/alloc/kvec.rs
> @@ -1510,4 +1510,116 @@ fn add(value: &mut [bool]) {
> func.push_within_capacity(false).unwrap();
> }
> }
> +
> + #[test]
> + fn test_kvvec_shrink_to() {
> + use crate::page::PAGE_SIZE;
> +
> + // Calculate elements per page for u32.
> + let elements_per_page = PAGE_SIZE / core::mem::size_of::<u32>();

NIT: Wouldn't it be a bit more straight forward to just use u8 for all the
tests?

> + #[test]
> + fn test_kvvec_shrink_to_empty() {
> + use crate::page::PAGE_SIZE;
> +
> + let elements_per_page = PAGE_SIZE / core::mem::size_of::<u64>();
> +
> + // Create a vector with large capacity but no elements.
> + let mut v = KVVec::<u64>::with_capacity(elements_per_page * 4, GFP_KERNEL).unwrap();
> +
> + assert!(v.is_empty());
> + let initial_capacity = v.capacity();
> +
> + // Shrink empty vector to zero.
> + v.shrink_to(0, GFP_KERNEL).unwrap();
> +
> + // Should have freed the allocation.
> + assert!(v.capacity() < initial_capacity);

I think this assert!() should rather check for v.capacity() == 0.

> + assert!(v.is_empty());
> + }