xref: /linux/rust/kernel/drm/gpuvm/vm_bo.rs (revision 71a3921111bd05298988fad1c58241db13384ea7)
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 }
243