* [PATCH v3] rust: gpu: buddy: store the allocator inside the Mutex
@ 2026-11-22 7:20 Ivan Tverdokhleb
2026-11-22 10:47 ` Miguel Ojeda
2026-11-23 21:24 ` Danilo Krummrich
0 siblings, 2 replies; 4+ messages in thread
From: Ivan Tverdokhleb @ 2026-11-22 7:20 UTC (permalink / raw)
To: rust-for-linux
Cc: Joel Fernandes, Matthew Auld, Arun Pravin, Danilo Krummrich,
Alice Ryhl, Miguel Ojeda, Boqun Feng, Gary Guo,
Björn Roy Baron, Benno Lossin, Andreas Hindborg,
Trevor Gross, Daniel Almeida, Tamir Duberstein, Alexandre Courbot,
Onur Özkan, dri-devel
GpuBuddyInner keeps the C gpu_buddy structure next to a separate
Mutex<()>, so nothing in the type system ties the data to the lock.
A TODO asked to replace Mutex<()> with Mutex<Opaque<..>> once
Mutex::new() accepts a fallible initializer.
That is now possible, so put the Opaque<gpu_buddy> directly inside the
Mutex. The allocator can then only be reached through the lock guard,
which lets the compiler enforce that the lock is held. This also
removes the now unneeded `inner` field from GpuBuddyGuard.
Signed-off-by: Ivan Tverdokhleb <mbkchelofficialdev@gmail.com>
Reviewed-by: Joel Fernandes <jfern@kernel.org>
---
Changes in v2:
- Dropped patch 1/2 ("rust: sync: lock: allow fallible initializers
in Lock::new"): the same change is already in linux-next
(Philipp Stanner's "rust: sync: Accept errors for Lock payload
data"), as Gary pointed out. This patch now depends on it.
- Rebased on next-20261118.
- Fixed the indentation of the new_mutex!() initializer.
- Build-tested on x86_64 (defconfig with CONFIG_RUST=y,
CONFIG_GPU_BUDDY=y and CONFIG_NOVA_CORE=y). Not
tested at runtime.
Changes in v3:
- Kept the name `inner` for the mutex field instead of renaming it
to `lock`. (Danilo)
- The SAFETY comment updates from v2 are gone, as the field keeps its
original name.
- Collected Reviewed-by from Joel.
v1: https://lore.kernel.org/rust-for-linux/20261114200703.33229-1-mbkchelofficialdev@gmail.com/
v2: https://lore.kernel.org/rust-for-linux/20261119111850.33270-1-mbkchelofficialdev@gmail.com/
rust/kernel/gpu/buddy.rs | 22 +++++++---------------
1 file changed, 7 insertions(+), 15 deletions(-)
diff --git a/rust/kernel/gpu/buddy.rs b/rust/kernel/gpu/buddy.rs
index 691bb4062..4e08ec319 100644
--- a/rust/kernel/gpu/buddy.rs
+++ b/rust/kernel/gpu/buddy.rs
@@ -313,16 +313,11 @@ pub struct GpuBuddyParams {
///
/// # Invariants
///
-/// The inner [`Opaque`] contains an initialized buddy allocator.
+/// The `inner` field contains an initialized buddy allocator wrapped in `Opaque`.
#[pin_data(PinnedDrop)]
struct GpuBuddyInner {
#[pin]
- inner: Opaque<bindings::gpu_buddy>,
-
- // TODO: Replace `Mutex<()>` with `Mutex<Opaque<..>>` once `Mutex::new()`
- // accepts `impl PinInit<T>`.
- #[pin]
- lock: Mutex<()>,
+ inner: Mutex<Opaque<bindings::gpu_buddy>>,
/// Cached creation parameters (do not change after init).
params: GpuBuddyParams,
}
@@ -338,14 +333,13 @@ fn new(params: GpuBuddyParams) -> impl PinInit<Self, Error> {
// `gpu_buddy_*` APIs. `try_pin_init!` only completes if all fields succeed,
// so the invariant holds when construction finishes.
try_pin_init!(Self {
- inner <- Opaque::try_ffi_init(|ptr| {
+ inner <- new_mutex!(Opaque::try_ffi_init(|ptr| {
// SAFETY: `ptr` points to valid uninitialized memory from the pin-init
// infrastructure. `gpu_buddy_init` will initialize the structure.
to_result(unsafe {
bindings::gpu_buddy_init(ptr, size, chunk_size.as_usize() as u64)
})
- }),
- lock <- new_mutex!(()),
+ })),
params,
})
}
@@ -353,8 +347,7 @@ fn new(params: GpuBuddyParams) -> impl PinInit<Self, Error> {
/// Lock the mutex and return a guard for accessing the allocator.
fn lock(&self) -> GpuBuddyGuard<'_> {
GpuBuddyGuard {
- inner: self,
- _guard: self.lock.lock(),
+ _guard: self.inner.lock(),
}
}
}
@@ -381,14 +374,13 @@ unsafe impl Sync for GpuBuddyInner {}
///
/// The `_guard` holds the lock for the duration of this guard's lifetime.
struct GpuBuddyGuard<'a> {
- inner: &'a GpuBuddyInner,
- _guard: MutexGuard<'a, ()>,
+ _guard: MutexGuard<'a, Opaque<bindings::gpu_buddy>>,
}
impl GpuBuddyGuard<'_> {
/// Get a raw pointer to the underlying C `gpu_buddy` structure.
fn as_raw(&self) -> *mut bindings::gpu_buddy {
- self.inner.inner.get()
+ self._guard.get()
}
}
--
2.56.1
^ permalink raw reply related [flat|nested] 4+ messages in thread* Re: [PATCH v3] rust: gpu: buddy: store the allocator inside the Mutex
2026-11-22 7:20 [PATCH v3] rust: gpu: buddy: store the allocator inside the Mutex Ivan Tverdokhleb
@ 2026-11-22 10:47 ` Miguel Ojeda
[not found] ` <CA+YDrKBYCB+AzOgDY6M6cczSyVNkY8me9UiYB-tZNz1bbanPnQ@mail.gmail.com>
2026-11-23 21:24 ` Danilo Krummrich
1 sibling, 1 reply; 4+ messages in thread
From: Miguel Ojeda @ 2026-11-22 10:47 UTC (permalink / raw)
To: Ivan Tverdokhleb
Cc: rust-for-linux, Joel Fernandes, Matthew Auld, Arun Pravin,
Danilo Krummrich, Alice Ryhl, Miguel Ojeda, Boqun Feng, Gary Guo,
Björn Roy Baron, Benno Lossin, Andreas Hindborg,
Trevor Gross, Daniel Almeida, Tamir Duberstein, Alexandre Courbot,
Onur Özkan, dri-devel
On Sun, Nov 22, 2026 at 8:20 AM Ivan Tverdokhleb
<mbkchelofficialdev@gmail.com> wrote:
>
> -/// The inner [`Opaque`] contains an initialized buddy allocator.
> +/// The `inner` field contains an initialized buddy allocator wrapped in `Opaque`.
No need for a new version (I just noticed this skimming around), but
please keep the intra-doc link when applying the patch. Or is there a
reason why it is removed?
Thanks!
Cheers,
Miguel
^ permalink raw reply [flat|nested] 4+ messages in thread* Re: [PATCH v3] rust: gpu: buddy: store the allocator inside the Mutex
2026-11-22 7:20 [PATCH v3] rust: gpu: buddy: store the allocator inside the Mutex Ivan Tverdokhleb
2026-11-22 10:47 ` Miguel Ojeda
@ 2026-11-23 21:24 ` Danilo Krummrich
1 sibling, 0 replies; 4+ messages in thread
From: Danilo Krummrich @ 2026-11-23 21:24 UTC (permalink / raw)
To: Ivan Tverdokhleb
Cc: rust-for-linux, Joel Fernandes, Matthew Auld, Arun Pravin,
Danilo Krummrich, Alice Ryhl, Miguel Ojeda, Boqun Feng, Gary Guo,
Björn Roy Baron, Benno Lossin, Andreas Hindborg,
Trevor Gross, Daniel Almeida, Tamir Duberstein, Alexandre Courbot,
Onur Özkan, dri-devel
On Sun, 22 Nov 2026 09:20:07 +0200, Ivan Tverdokhleb wrote:
> [PATCH v3] rust: gpu: buddy: store the allocator inside the Mutex
Applied, thanks!
Branch: drm-rust-next
Tree: https://gitlab.freedesktop.org/drm/rust/kernel.git
[1/1] rust: gpu: buddy: store the allocator inside the Mutex
commit: 4325f4aad972
[ Restore intra-doc link for Opaque. - Danilo ]
The patch will appear in the next linux-next integration (typically within 24
hours on weekdays).
The patch is queued up for the upcoming merge window for the next major kernel
release.
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-11-23 21:24 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-22 7:20 [PATCH v3] rust: gpu: buddy: store the allocator inside the Mutex Ivan Tverdokhleb
2026-11-22 10:47 ` Miguel Ojeda
[not found] ` <CA+YDrKBYCB+AzOgDY6M6cczSyVNkY8me9UiYB-tZNz1bbanPnQ@mail.gmail.com>
2026-11-22 12:37 ` Ivan Tverdokhleb
2026-11-23 21:24 ` Danilo Krummrich
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox