From: Philipp Stanner <phasta@kernel.org>
To: "Miguel Ojeda" <ojeda@kernel.org>,
"Boqun Feng" <boqun@kernel.org>, "Gary Guo" <gary@garyguo.net>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Benno Lossin" <lossin@kernel.org>,
"Andreas Hindborg" <a.hindborg@kernel.org>,
"Alice Ryhl" <aliceryhl@google.com>,
"Trevor Gross" <tmgross@umich.edu>,
"Danilo Krummrich" <dakr@kernel.org>,
"Daniel Almeida" <daniel.almeida@collabora.com>,
"Tamir Duberstein" <tamird@kernel.org>,
"Alexandre Courbot" <acourbot@nvidia.com>,
"Onur Özkan" <work@onurozkan.dev>,
"Sumit Semwal" <sumit.semwal@linaro.org>,
"Christian König" <christian.koenig@amd.com>,
"Philipp Stanner" <phasta@kernel.org>,
"Lyude Paul" <lyude@redhat.com>,
"Paul E. McKenney" <paulmck@kernel.org>,
"Frederic Weisbecker" <frederic@kernel.org>,
"Neeraj Upadhyay" <neeraj.upadhyay@kernel.org>,
"Joel Fernandes" <joelagnelf@nvidia.com>,
"Josh Triplett" <josh@joshtriplett.org>,
"Uladzislau Rezki" <urezki@gmail.com>,
"Steven Rostedt" <rostedt@goodmis.org>,
"Mathieu Desnoyers" <mathieu.desnoyers@efficios.com>,
"Lai Jiangshan" <jiangshanlai@gmail.com>,
Zqiang <qiang.zhang@linux.dev>,
"Greg Kroah-Hartman" <gregkh@linuxfoundation.org>,
"Yury Norov (NVIDIA)" <yury.norov@gmail.com>,
"Asahi Lina" <lina+kernel@asahilina.net>,
"Lorenzo Stoakes" <ljs@kernel.org>,
"FUJITA Tomonori" <fujita.tomonori@gmail.com>,
"Eliot Courtney" <ecourtney@nvidia.com>,
"Mirko Adzic" <adzicmirko97@gmail.com>,
"Daniel del Castillo" <delcastillodelarosadaniel@gmail.com>,
"Alistair Francis" <alistair.francis@wdc.com>
Cc: linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org,
linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org,
linaro-mm-sig@lists.linaro.org, rcu@vger.kernel.org
Subject: [PATCH v6 4/6] rust: xarray: Add support for index reservations
Date: Wed, 22 Jul 2026 16:49:28 +0200 [thread overview]
Message-ID: <20260722144931.1070312-6-phasta@kernel.org> (raw)
In-Reply-To: <20260722144931.1070312-2-phasta@kernel.org>
One often cannot allocate in the kernel with the desired flags, most
notably in atomic context. Pre-allocating the memory is the preferred
solution in such situations.
Add support for xa_reserve() in the Rust abstractions of xarray. Create
a Reservation object similar to a lock-guard, that can be dropped once
the reservation is no longer needed or once the index was stored to.
Signed-off-by: Philipp Stanner <phasta@kernel.org>
---
Please regard this more as an RFC.
I need pre-allocating in XArray for DmaFence. How exactly we achieve
this is open for discussion.
P.
---
rust/helpers/xarray.c | 10 ++++++++++
rust/kernel/xarray.rs | 45 ++++++++++++++++++++++++++++++++++++++++++-
2 files changed, 54 insertions(+), 1 deletion(-)
diff --git a/rust/helpers/xarray.c b/rust/helpers/xarray.c
index 08979b304341..1504ab242949 100644
--- a/rust/helpers/xarray.c
+++ b/rust/helpers/xarray.c
@@ -26,3 +26,13 @@ __rust_helper void rust_helper_xa_unlock(struct xarray *xa)
{
return xa_unlock(xa);
}
+
+__rust_helper int rust_helper_xa_reserve(struct xarray *xa, unsigned long index, gfp_t flags)
+{
+ return xa_reserve(xa, index, flags);
+}
+
+__rust_helper void rust_helper_xa_release(struct xarray *xa, unsigned long index)
+{
+ xa_release(xa, index);
+}
diff --git a/rust/kernel/xarray.rs b/rust/kernel/xarray.rs
index 987c9c0c2198..fa5d47935f0a 100644
--- a/rust/kernel/xarray.rs
+++ b/rust/kernel/xarray.rs
@@ -8,7 +8,10 @@
alloc,
bindings,
build_assert::build_assert,
- error::{Error, Result},
+ error::{
+ Error,
+ Result, //
+ },
ffi::c_void,
types::{
ForeignOwnable,
@@ -147,6 +150,46 @@ pub fn lock(&self) -> Guard<'_, T> {
_not_send: NotThreadSafe,
}
}
+
+ /// Reserve memory in the XArray at the appropriate index. If this call
+ /// succeeds, later storing at the index will not require an allocation.
+ ///
+ /// Loading from reserved entries will return `None`.
+ pub fn reserve(&self, index: usize, gfp: alloc::Flags) -> Result<Reservation<'_, T>> {
+ // SAFETY:
+ // - `self.xa.xa` is always valid by the type invariant.
+ // - The caller holds the lock.
+ //
+ // INVARIANT: `new` came from `T::into_foreign`.
+ let ret = unsafe { bindings::xa_reserve(self.xa.get(), index, gfp.as_raw()) };
+ match ret {
+ 0 => Ok(Reservation { xa: self, index }),
+ _ => Err(Error::from_errno(ret)),
+ }
+ }
+}
+
+/// An index reservation object.
+///
+/// As long as you keep this object alive, your reservation will stay valid. If
+/// someone has stored to this index in the meantime, dropping your reservation
+/// does nothing, which is why you can always drop this reservation object once
+/// you performed your store operation.
+///
+/// Refer to the main C xarray documentation for more details.
+pub struct Reservation<'a, T: ForeignOwnable> {
+ xa: &'a XArray<T>,
+ index: usize,
+}
+
+impl<T: ForeignOwnable> Drop for Reservation<'_, T> {
+ fn drop(&mut self) {
+ // SAFETY:
+ // - `self.xa` is always valid by the type invariant.
+ // - If `self.id` is not used or has been stored to by a racing party,
+ // `xa_release()` will do nothing.
+ unsafe { bindings::xa_release(self.xa.xa.get(), self.index) };
+ }
}
/// A lock guard.
--
2.55.0
next prev parent reply other threads:[~2026-07-22 14:50 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-07-22 14:49 [PATCH v6 0/6] rust / dma_buf: Add abstractions for dma_fence Philipp Stanner
2026-07-22 14:49 ` [PATCH v6 1/6] rust: types: implement ForeignOwnable for ARef<T> Philipp Stanner
2026-07-22 14:49 ` [PATCH v6 2/6] rust: error: Add ECANCELED error code Philipp Stanner
2026-07-22 17:44 ` Alistair Francis
2026-07-22 18:38 ` Timur Tabi
2026-07-22 14:49 ` [PATCH v6 3/6] rust: sync: Add abstraction for rcu_barrier() Philipp Stanner
2026-07-22 14:49 ` Philipp Stanner [this message]
2026-07-22 14:49 ` [PATCH v6 5/6] rust: Add dma_fence abstractions Philipp Stanner
2026-07-22 14:49 ` [PATCH v6 6/6] MAINTAINERS: Add entry for Rust dma-buf Philipp Stanner
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=20260722144931.1070312-6-phasta@kernel.org \
--to=phasta@kernel.org \
--cc=a.hindborg@kernel.org \
--cc=acourbot@nvidia.com \
--cc=adzicmirko97@gmail.com \
--cc=aliceryhl@google.com \
--cc=alistair.francis@wdc.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun@kernel.org \
--cc=christian.koenig@amd.com \
--cc=dakr@kernel.org \
--cc=daniel.almeida@collabora.com \
--cc=delcastillodelarosadaniel@gmail.com \
--cc=dri-devel@lists.freedesktop.org \
--cc=ecourtney@nvidia.com \
--cc=frederic@kernel.org \
--cc=fujita.tomonori@gmail.com \
--cc=gary@garyguo.net \
--cc=gregkh@linuxfoundation.org \
--cc=jiangshanlai@gmail.com \
--cc=joelagnelf@nvidia.com \
--cc=josh@joshtriplett.org \
--cc=lina+kernel@asahilina.net \
--cc=linaro-mm-sig@lists.linaro.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-media@vger.kernel.org \
--cc=ljs@kernel.org \
--cc=lossin@kernel.org \
--cc=lyude@redhat.com \
--cc=mathieu.desnoyers@efficios.com \
--cc=neeraj.upadhyay@kernel.org \
--cc=ojeda@kernel.org \
--cc=paulmck@kernel.org \
--cc=qiang.zhang@linux.dev \
--cc=rcu@vger.kernel.org \
--cc=rostedt@goodmis.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=sumit.semwal@linaro.org \
--cc=tamird@kernel.org \
--cc=tmgross@umich.edu \
--cc=urezki@gmail.com \
--cc=work@onurozkan.dev \
--cc=yury.norov@gmail.com \
/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