Skip to content

Commit 09852b9

Browse files
committed
rust: gpuvm: add GpuVm::obtain()
This provides a mechanism to create (or look up) VMBO instances, which represent the mapping between GPUVM and GEM objects. The GpuVmBoRegistered<T> type can be considered like ARef<GpuVm<T>>, except that no way to increment the refcount is provided. The GpuVmBoAlloc<T> type is more akin to a pre-allocated GpuVmBo<T>, so it's not really a GpuVmBo<T> yet. Its destructor could call drm_gpuvm_bo_destroy_not_in_lists(), but as the type is currently private and never called anywhere, this perf optimization does not need to happen now. Pre-allocating and obtaining the gpuvm_bo object is exposed as a single step. This could theoretically be a problem if one wanted to call drm_gpuvm_bo_obtain_prealloc() during the fence signalling critical path, but that's not a possibility because: 1. Adding the BO to the extobj list requires the resv lock, so it cannot happen during the fence signalling critical path. 2. obtain() requires that the BO is not in the extobj list, so obtain() must be called before adding the BO to the extobj list. Thus, drm_gpuvm_bo_obtain_prealloc() cannot be called during the fence signalling critical path. (For extobjs at least.) Reviewed-by: Daniel Almeida <[email protected]> Signed-off-by: Alice Ryhl <[email protected]>
1 parent 3ee731c commit 09852b9

3 files changed

Lines changed: 277 additions & 3 deletions

File tree

rust/helpers/drm_gpuvm.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44

55
#include <drm/drm_gpuvm.h>
66

7+
__rust_helper
8+
struct drm_gpuvm_bo *rust_helper_drm_gpuvm_bo_get(struct drm_gpuvm_bo *vm_bo)
9+
{
10+
return drm_gpuvm_bo_get(vm_bo);
11+
}
12+
713
__rust_helper
814
struct drm_gpuvm *rust_helper_drm_gpuvm_get(struct drm_gpuvm *obj)
915
{

rust/kernel/drm/gpuvm/mod.rs

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,20 @@ use kernel::{
2525

2626
use core::{
2727
cell::UnsafeCell,
28+
mem::ManuallyDrop,
2829
ops::{
2930
Deref,
3031
Range, //
3132
},
32-
ptr::NonNull, //
33+
ptr::{
34+
self,
35+
NonNull, //
36+
}, //
3337
};
3438

39+
mod vm_bo;
40+
pub use self::vm_bo::*;
41+
3542
/// A DRM GPU VA manager.
3643
///
3744
/// This object is refcounted, but the locations of mapped ranges may only be accessed or changed
@@ -83,8 +90,8 @@ impl<T: DriverGpuVm> GpuVm<T> {
8390
vm_free: Some(Self::vm_free),
8491
op_alloc: None,
8592
op_free: None,
86-
vm_bo_alloc: None,
87-
vm_bo_free: None,
93+
vm_bo_alloc: GpuVmBo::<T>::ALLOC_FN,
94+
vm_bo_free: GpuVmBo::<T>::FREE_FN,
8895
vm_bo_validate: None,
8996
sm_step_map: None,
9097
sm_step_unmap: None,
@@ -184,6 +191,16 @@ impl<T: DriverGpuVm> GpuVm<T> {
184191
Range { start, end }
185192
}
186193

194+
/// Get or create the [`GpuVmBo`] for this gem object.
195+
#[inline]
196+
pub fn obtain(
197+
&self,
198+
obj: &T::Object,
199+
data: impl PinInit<T::VmBoData>,
200+
) -> Result<ARef<GpuVmBo<T>>, AllocError> {
201+
Ok(GpuVmBoAlloc::new(self, obj, data)?.obtain())
202+
}
203+
187204
/// Clean up buffer objects that are no longer used.
188205
#[inline]
189206
pub fn deferred_cleanup(&self) {
@@ -209,6 +226,12 @@ impl<T: DriverGpuVm> GpuVm<T> {
209226
// SAFETY: By type invariants we can free it when refcount hits zero.
210227
drop(unsafe { KBox::from_raw(me) })
211228
}
229+
230+
#[inline]
231+
fn raw_resv(&self) -> *mut bindings::dma_resv {
232+
// SAFETY: `r_obj` is immutable and valid for duration of GPUVM.
233+
unsafe { (*(*self.as_raw()).r_obj).resv }
234+
}
212235
}
213236

214237
/// The manager for a GPUVM.
@@ -218,6 +241,9 @@ pub trait DriverGpuVm: Sized + Send {
218241

219242
/// The kind of GEM object stored in this GPUVM.
220243
type Object: IntoGEMObject;
244+
245+
/// Data stored with each [`struct drm_gpuvm_bo`](struct@GpuVmBo).
246+
type VmBoData;
221247
}
222248

223249
/// The core of the DRM GPU VA manager.

rust/kernel/drm/gpuvm/vm_bo.rs

Lines changed: 242 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,242 @@
1+
// SPDX-License-Identifier: GPL-2.0 OR MIT
2+
3+
use super::*;
4+
5+
/// Represents that a given GEM object has at least one mapping on this [`GpuVm`] instance.
6+
///
7+
/// Does not assume that GEM lock is held.
8+
///
9+
/// # Invariants
10+
///
11+
/// * Allocated with `kmalloc` and refcounted via `inner`.
12+
/// * Is present in the gem list.
13+
#[repr(C)]
14+
#[pin_data]
15+
pub struct GpuVmBo<T: DriverGpuVm> {
16+
#[pin]
17+
inner: Opaque<bindings::drm_gpuvm_bo>,
18+
#[pin]
19+
data: T::VmBoData,
20+
}
21+
22+
// SAFETY: By type invariants, the allocation is managed by the refcount in `self.inner`.
23+
unsafe impl<T: DriverGpuVm> AlwaysRefCounted for GpuVmBo<T> {
24+
fn inc_ref(&self) {
25+
// SAFETY: By type invariants, the allocation is managed by the refcount in `self.inner`.
26+
unsafe { bindings::drm_gpuvm_bo_get(self.inner.get()) };
27+
}
28+
29+
unsafe fn dec_ref(obj: NonNull<Self>) {
30+
// CAST: `drm_gpuvm_bo` is first field of repr(C) struct.
31+
// SAFETY: By type invariants, the allocation is managed by the refcount in `self.inner`.
32+
// This GPUVM instance uses immediate mode, so we may put the refcount using the deferred
33+
// mechanism.
34+
unsafe { bindings::drm_gpuvm_bo_put_deferred(obj.as_ptr().cast()) };
35+
}
36+
}
37+
38+
impl<T: DriverGpuVm> PartialEq for GpuVmBo<T> {
39+
#[inline]
40+
fn eq(&self, other: &Self) -> bool {
41+
core::ptr::eq(self.as_raw(), other.as_raw())
42+
}
43+
}
44+
impl<T: DriverGpuVm> Eq for GpuVmBo<T> {}
45+
46+
impl<T: DriverGpuVm> GpuVmBo<T> {
47+
/// The function pointer for allocating a GpuVmBo stored in the gpuvm vtable.
48+
///
49+
/// Allocation is always implemented according to [`Self::vm_bo_alloc`], but it is set to
50+
/// `None` if the default gpuvm behavior is the same as `vm_bo_alloc`.
51+
///
52+
/// This may be `Some` even if `FREE_FN` is `None`, or vice-versa.
53+
pub(super) const ALLOC_FN: Option<unsafe extern "C" fn() -> *mut bindings::drm_gpuvm_bo> = {
54+
use core::alloc::Layout;
55+
let base = Layout::new::<bindings::drm_gpuvm_bo>();
56+
let rust = Layout::new::<Self>();
57+
assert!(base.size() <= rust.size());
58+
if base.size() != rust.size() || base.align() != rust.align() {
59+
Some(Self::vm_bo_alloc)
60+
} else {
61+
// This causes GPUVM to allocate a `GpuVmBo<T>` with `kzalloc(sizeof(drm_gpuvm_bo))`.
62+
None
63+
}
64+
};
65+
66+
/// The function pointer for freeing a GpuVmBo stored in the gpuvm vtable.
67+
///
68+
/// Freeing is always implemented according to [`Self::vm_bo_free`], but it is set to `None` if
69+
/// the default gpuvm behavior is the same as `vm_bo_free`.
70+
///
71+
/// This may be `Some` even if `ALLOC_FN` is `None`, or vice-versa.
72+
pub(super) const FREE_FN: Option<unsafe extern "C" fn(*mut bindings::drm_gpuvm_bo)> = {
73+
if core::mem::needs_drop::<Self>() {
74+
Some(Self::vm_bo_free)
75+
} else {
76+
// This causes GPUVM to free a `GpuVmBo<T>` with `kfree`.
77+
None
78+
}
79+
};
80+
81+
/// Custom function for allocating a `drm_gpuvm_bo`.
82+
///
83+
/// # Safety
84+
///
85+
/// Always safe to call.
86+
unsafe extern "C" fn vm_bo_alloc() -> *mut bindings::drm_gpuvm_bo {
87+
let raw_ptr = KBox::<Self>::new_uninit(GFP_KERNEL | __GFP_ZERO)
88+
.map(KBox::into_raw)
89+
.unwrap_or(ptr::null_mut());
90+
91+
// CAST: `drm_gpuvm_bo` is first field of `Self`.
92+
raw_ptr.cast()
93+
}
94+
95+
/// Custom function for freeing a `drm_gpuvm_bo`.
96+
///
97+
/// # Safety
98+
///
99+
/// The pointer must have been allocated with [`GpuVmBo::ALLOC_FN`], and must not be used after
100+
/// this call.
101+
unsafe extern "C" fn vm_bo_free(ptr: *mut bindings::drm_gpuvm_bo) {
102+
// CAST: `drm_gpuvm_bo` is first field of `Self`.
103+
// SAFETY:
104+
// * The ptr was allocated from kmalloc with the layout of `GpuVmBo<T>`.
105+
// * `ptr->inner` has no destructor.
106+
// * `ptr->data` contains a valid `T::VmBoData` that we can drop.
107+
drop(unsafe { KBox::<Self>::from_raw(ptr.cast()) });
108+
}
109+
110+
/// Access this [`GpuVmBo`] from a raw pointer.
111+
///
112+
/// # Safety
113+
///
114+
/// For the duration of `'a`, the pointer must reference a valid `drm_gpuvm_bo` associated with
115+
/// a [`GpuVm<T>`]. The BO must also be present in the GEM list.
116+
#[inline]
117+
#[expect(dead_code)]
118+
pub(crate) unsafe fn from_raw<'a>(ptr: *mut bindings::drm_gpuvm_bo) -> &'a Self {
119+
// SAFETY: `drm_gpuvm_bo` is first field and `repr(C)`.
120+
unsafe { &*ptr.cast() }
121+
}
122+
123+
/// Returns a raw pointer to underlying C value.
124+
#[inline]
125+
pub fn as_raw(&self) -> *mut bindings::drm_gpuvm_bo {
126+
self.inner.get()
127+
}
128+
129+
/// The [`GpuVm`] that this GEM object is mapped in.
130+
#[inline]
131+
pub fn gpuvm(&self) -> &GpuVm<T> {
132+
// SAFETY: The `obj` pointer is guaranteed to be valid.
133+
unsafe { GpuVm::<T>::from_raw((*self.inner.get()).vm) }
134+
}
135+
136+
/// The [`drm_gem_object`](DriverGpuVm::Object) for these mappings.
137+
#[inline]
138+
pub fn obj(&self) -> &T::Object {
139+
// SAFETY: The `obj` pointer is guaranteed to be valid.
140+
unsafe { <T::Object as IntoGEMObject>::from_raw((*self.inner.get()).obj) }
141+
}
142+
143+
/// The driver data with this buffer object.
144+
#[inline]
145+
pub fn data(&self) -> &T::VmBoData {
146+
&self.data
147+
}
148+
}
149+
150+
/// A pre-allocated [`GpuVmBo`] object.
151+
///
152+
/// # Invariants
153+
///
154+
/// Points at a `drm_gpuvm_bo` that contains a valid `T::VmBoData`, has a refcount of one, and is
155+
/// absent from any gem, extobj, or evict lists.
156+
pub(super) struct GpuVmBoAlloc<T: DriverGpuVm>(NonNull<GpuVmBo<T>>);
157+
158+
impl<T: DriverGpuVm> GpuVmBoAlloc<T> {
159+
/// Create a new pre-allocated [`GpuVmBo`].
160+
///
161+
/// It's intentional that the initializer is infallible because `drm_gpuvm_bo_put` will call
162+
/// drop on the data, so we don't have a way to free it when the data is missing.
163+
#[inline]
164+
pub(super) fn new(
165+
gpuvm: &GpuVm<T>,
166+
gem: &T::Object,
167+
value: impl PinInit<T::VmBoData>,
168+
) -> Result<GpuVmBoAlloc<T>, AllocError> {
169+
// CAST: `GpuVmBoAlloc::vm_bo_alloc` ensures that this memory was allocated with the layout
170+
// of `GpuVmBo<T>`. The type is repr(C), so `container_of` is not required.
171+
// SAFETY: The provided gpuvm and gem ptrs are valid for the duration of this call.
172+
let raw_ptr = unsafe {
173+
bindings::drm_gpuvm_bo_create(gpuvm.as_raw(), gem.as_raw()).cast::<GpuVmBo<T>>()
174+
};
175+
let ptr = NonNull::new(raw_ptr).ok_or(AllocError)?;
176+
// SAFETY: `ptr->data` is a valid pinned location.
177+
let Ok(()) = unsafe { value.__pinned_init(&raw mut (*raw_ptr).data) };
178+
// INVARIANTS: We just created the vm_bo so it's absent from lists, and the data is valid
179+
// as we just initialized it.
180+
Ok(GpuVmBoAlloc(ptr))
181+
}
182+
183+
/// Returns a raw pointer to underlying C value.
184+
#[inline]
185+
pub(super) fn as_raw(&self) -> *mut bindings::drm_gpuvm_bo {
186+
// SAFETY: The pointer references a valid `drm_gpuvm_bo`.
187+
unsafe { (*self.0.as_ptr()).inner.get() }
188+
}
189+
190+
/// Look up whether there is an existing [`GpuVmBo`] for this gem object.
191+
///
192+
/// The caller should not hold the GEM mutex or DMA resv lock.
193+
#[inline]
194+
pub(super) fn obtain(self) -> ARef<GpuVmBo<T>> {
195+
let me = ManuallyDrop::new(self);
196+
// SAFETY: Valid `drm_gpuvm_bo` not already in the lists. We do not access `me` after this
197+
// call.
198+
let ptr = unsafe { bindings::drm_gpuvm_bo_obtain_prealloc(me.as_raw()) };
199+
200+
// SAFETY: `drm_gpuvm_bo_obtain_prealloc` always returns a non-null ptr
201+
let nonnull = unsafe { NonNull::new_unchecked(ptr.cast()) };
202+
203+
// INVARIANTS: `drm_gpuvm_bo_obtain_prealloc` ensures that the bo is in the GEM list.
204+
// SAFETY: We received one refcount from `drm_gpuvm_bo_obtain_prealloc`.
205+
let ret = unsafe { ARef::<GpuVmBo<T>>::from_raw(nonnull) };
206+
207+
// Ensure that external objects are in the extobj list.
208+
//
209+
// Note that we must call `extobj_add` even if `ptr != me` to avoid a race condition where
210+
// we could end up using the extobj before the thread with `ptr == me` calls extobj_add.
211+
if ret.gpuvm().is_extobj(ret.obj()) {
212+
let resv_lock = ret.gpuvm().raw_resv();
213+
// TODO: Use a proper lock guard here once a dma_resv lock abstraction exists.
214+
// SAFETY: The GPUVM is still alive, so its resv lock is too.
215+
unsafe { bindings::dma_resv_lock(resv_lock, ptr::null_mut()) };
216+
// SAFETY: We hold the GPUVMs resv lock.
217+
unsafe { bindings::drm_gpuvm_bo_extobj_add(ptr) };
218+
// SAFETY: We took the lock, so we can unlock it.
219+
unsafe { bindings::dma_resv_unlock(resv_lock) };
220+
}
221+
222+
ret
223+
}
224+
}
225+
226+
impl<T: DriverGpuVm> Deref for GpuVmBoAlloc<T> {
227+
type Target = GpuVmBo<T>;
228+
#[inline]
229+
fn deref(&self) -> &GpuVmBo<T> {
230+
// SAFETY: By the type invariants we may deref while `Self` exists.
231+
unsafe { self.0.as_ref() }
232+
}
233+
}
234+
235+
impl<T: DriverGpuVm> Drop for GpuVmBoAlloc<T> {
236+
#[inline]
237+
fn drop(&mut self) {
238+
// TODO: Call drm_gpuvm_bo_destroy_not_in_lists() directly.
239+
// SAFETY: It's safe to perform a deferred put in any context.
240+
unsafe { bindings::drm_gpuvm_bo_put_deferred(self.as_raw()) };
241+
}
242+
}

0 commit comments

Comments
 (0)