From: "Onur Özkan" <work@onurozkan.dev>
To: rust-for-linux@vger.kernel.org
Cc: ojeda@kernel.org, alex.gaynor@gmail.com, boqun.feng@gmail.com,
gary@garyguo.net, bjorn3_gh@protonmail.com, lossin@kernel.org,
aliceryhl@google.com, tmgross@umich.edu, dakr@kernel.org,
linux-kernel@vger.kernel.org, acourbot@nvidia.com,
airlied@gmail.com, simona@ffwll.ch,
maarten.lankhorst@linux.intel.com, mripard@kernel.org,
tzimmermann@suse.de, corbet@lwn.net, lyude@redhat.com,
linux-doc@vger.kernel.org, linux-mm@kvack.org,
"Onur Özkan" <work@onurozkan.dev>
Subject: [PATCH v2 3/4] rust: xarray: abstract `xa_alloc_cyclic`
Date: Wed, 8 Oct 2025 15:46:18 +0300 [thread overview]
Message-ID: <20251008124619.3160-4-work@onurozkan.dev> (raw)
In-Reply-To: <20251008124619.3160-1-work@onurozkan.dev>
Implements `alloc_cyclic` function to `XArray<T>` that
wraps `xa_alloc_cyclic` 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@onurozkan.dev>
---
rust/kernel/xarray.rs | 50 +++++++++++++++++++++++++++++++++++++++++++
1 file changed, 50 insertions(+)
diff --git a/rust/kernel/xarray.rs b/rust/kernel/xarray.rs
index 0711ccf99fb4..8ac7210afd6b 100644
--- a/rust/kernel/xarray.rs
+++ b/rust/kernel/xarray.rs
@@ -307,6 +307,56 @@ 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 index and the next pointer respectively.
+ ///
+ /// On failure, returns the element which was attempted to be stored.
+ pub fn alloc_cyclic(
+ &mut self,
+ limit: Range<u32>,
+ mut next: u32,
+ value: T,
+ gfp: alloc::Flags,
+ ) -> Result<(u32, u32), StoreError<T>> {
+ let new = value.into_foreign();
+
+ let limit = bindings::xa_limit {
+ min: limit.start,
+ max: limit.end,
+ };
+
+ // `__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,
+ &mut 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, next))
+ }
}
// SAFETY: `XArray<T>` has no shared mutable state so it is `Send` iff `T` is `Send`.
--
2.51.0
next prev parent reply other threads:[~2025-10-08 12:55 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-10-08 12:46 [PATCH v2 0/4] rust: xarray: abstract xa_alloc and xa_alloc_cyclic Onur Özkan
2025-10-08 12:46 ` [PATCH v2 1/4] rust: xarray: move pointer check into `XArray::new` Onur Özkan
2025-10-08 12:46 ` [PATCH v2 2/4] rust: xarray: abstract `xa_alloc` Onur Özkan
2025-10-08 13:04 ` Alice Ryhl
2025-10-08 13:40 ` Matthew Wilcox
2025-10-08 14:05 ` Alice Ryhl
2025-10-08 16:59 ` Tamir Duberstein
2025-10-08 19:50 ` Onur Özkan
2025-10-08 20:45 ` Tamir Duberstein
2025-10-09 4:50 ` Onur Özkan
2025-10-08 12:46 ` Onur Özkan [this message]
2025-10-08 12:46 ` [PATCH v2 4/4] remove completed task from nova-core task list Onur Özkan
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20251008124619.3160-4-work@onurozkan.dev \
--to=work@onurozkan.dev \
--cc=acourbot@nvidia.com \
--cc=airlied@gmail.com \
--cc=alex.gaynor@gmail.com \
--cc=aliceryhl@google.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun.feng@gmail.com \
--cc=corbet@lwn.net \
--cc=dakr@kernel.org \
--cc=gary@garyguo.net \
--cc=linux-doc@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=lossin@kernel.org \
--cc=lyude@redhat.com \
--cc=maarten.lankhorst@linux.intel.com \
--cc=mripard@kernel.org \
--cc=ojeda@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=simona@ffwll.ch \
--cc=tmgross@umich.edu \
--cc=tzimmermann@suse.de \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).