[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20251006163024.18473-3-work@onurozkan.dev>
Date: Mon, 6 Oct 2025 19:30:23 +0300
From: Onur Özkan <work@...rozkan.dev>
To: rust-for-linux@...r.kernel.org
Cc: ojeda@...nel.org,
alex.gaynor@...il.com,
boqun.feng@...il.com,
gary@...yguo.net,
bjorn3_gh@...tonmail.com,
lossin@...nel.org,
aliceryhl@...gle.com,
tmgross@...ch.edu,
dakr@...nel.org,
linux-kernel@...r.kernel.org,
acourbot@...dia.com,
airlied@...il.com,
simona@...ll.ch,
maarten.lankhorst@...ux.intel.com,
mripard@...nel.org,
tzimmermann@...e.de,
corbet@....net,
lyude@...hat.com,
linux-doc@...r.kernel.org,
Onur Özkan <work@...rozkan.dev>
Subject: [PATCH 2/3] rust: xarray: abstract `xa_alloc_cyclic`
Implements `alloc_cyclic` function to `XArray<T>` that
wraps `xa_alloc_cyclic` safely.
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@...rozkan.dev>
---
rust/kernel/xarray.rs | 43 +++++++++++++++++++++++++++++++++++++++++++
1 file changed, 43 insertions(+)
diff --git a/rust/kernel/xarray.rs b/rust/kernel/xarray.rs
index 1b882cd2f58b..4c2fdf53c7af 100644
--- a/rust/kernel/xarray.rs
+++ b/rust/kernel/xarray.rs
@@ -305,6 +305,49 @@ pub fn alloc(
Ok(id)
}
+
+ /// Allocates an empty slot within the given `limit`, storing `value` and cycling from `*next`.
+ ///
+ /// May drop the lock if needed to allocate memory, and then reacquire it afterwards.
+ ///
+ /// On success, returns the allocated id.
+ ///
+ /// On failure, returns the element which was attempted to be stored.
+ pub fn alloc_cyclic(
+ &mut self,
+ limit: bindings::xa_limit,
+ next: &mut u32,
+ value: T,
+ gfp: alloc::Flags,
+ ) -> Result<u32, StoreError<T>> {
+ build_assert!(
+ T::FOREIGN_ALIGN >= 4,
+ "pointers stored in XArray must be 4-byte aligned"
+ );
+
+ let new = value.into_foreign();
+
+ // `__xa_alloc_cyclic` overwrites this.
+ let mut id: u32 = 0;
+
+ // SAFETY:
+ // - `self.xa.xa` is valid by the type invariant.
+ // - `new` came from `T::into_foreign`.
+ let ret = unsafe {
+ bindings::__xa_alloc_cyclic(self.xa.xa.get(), &mut id, new, limit, next, gfp.as_raw())
+ };
+
+ if ret < 0 {
+ // SAFETY: `__xa_alloc_cyclic` doesn't take ownership on error.
+ let value = unsafe { T::from_foreign(new) };
+ return Err(StoreError {
+ value,
+ error: Error::from_errno(ret),
+ });
+ }
+
+ Ok(id)
+ }
}
// SAFETY: `XArray<T>` has no shared mutable state so it is `Send` iff `T` is `Send`.
--
2.51.0
Powered by blists - more mailing lists