Re: [PATCH v2 2/4] rust: xarray: abstract `xa_alloc`

From: Alice Ryhl
Date: Wed Oct 08 2025 - 09:05:35 EST


On Wed, Oct 08, 2025 at 03:46:17PM +0300, Onur Özkan wrote:
> Implements `alloc` function to `XArray<T>` that wraps
> `xa_alloc` safely, which will be used to generate the
> auxiliary device IDs.
>
> Resolves a task from the nova/core task list under the "XArray
> bindings [XARR]" section in "Documentation/gpu/nova/core/todo.rst"
> file.
>
> Signed-off-by: Onur Özkan <work@xxxxxxxxxxxxx>
> ---
> rust/kernel/xarray.rs | 41 ++++++++++++++++++++++++++++++++++++++++-
> 1 file changed, 40 insertions(+), 1 deletion(-)
>
> diff --git a/rust/kernel/xarray.rs b/rust/kernel/xarray.rs
> index 90e27cd5197e..0711ccf99fb4 100644
> --- a/rust/kernel/xarray.rs
> +++ b/rust/kernel/xarray.rs
> @@ -10,7 +10,7 @@
> ffi::c_void,
> types::{ForeignOwnable, NotThreadSafe, Opaque},
> };
> -use core::{iter, marker::PhantomData, pin::Pin, ptr::NonNull};
> +use core::{iter, marker::PhantomData, ops::Range, pin::Pin, ptr::NonNull};
> use pin_init::{pin_data, pin_init, pinned_drop, PinInit};
>
> /// An array which efficiently maps sparse integer indices to owned objects.
> @@ -268,6 +268,45 @@ pub fn store(
> Ok(unsafe { T::try_from_foreign(old) })
> }
> }
> +
> + /// Allocates an empty slot within the given `limit` and stores `value` there.
> + ///
> + /// May drop the lock if needed to allocate memory, and then reacquire it afterwards.
> + ///
> + /// On success, returns the allocated index.
> + ///
> + /// On failure, returns the element which was attempted to be stored.
> + pub fn alloc(
> + &mut self,
> + limit: Range<u32>,

The Range type is inclusive/exclusive but xa_limit is
inclusive/inclusive. They should match to avoid confusion.

Alice