xref: /linux/rust/kernel/drm/gpuvm/vm_bo.rs (revision 99676aed1fec109d62822e21a06760eb098dc5f4)
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     pub(crate) unsafe fn from_raw<'a>(ptr: *mut bindings::drm_gpuvm_bo) -> &'a Self {
118         // SAFETY: `drm_gpuvm_bo` is first field and `repr(C)`.
119         unsafe { &*ptr.cast() }
120     }
121 
122     /// Returns a raw pointer to underlying C value.
123     #[inline]
124     pub fn as_raw(&self) -> *mut bindings::drm_gpuvm_bo {
125         self.inner.get()
126     }
127 
128     /// The [`GpuVm`] that this GEM object is mapped in.
129     #[inline]
130     pub fn gpuvm(&self) -> &GpuVm<T> {
131         // SAFETY: The `obj` pointer is guaranteed to be valid.
132         unsafe { GpuVm::<T>::from_raw((*self.inner.get()).vm) }
133     }
134 
135     /// The [`drm_gem_object`](DriverGpuVm::Object) for these mappings.
136     #[inline]
137     pub fn obj(&self) -> &T::Object {
138         // SAFETY: The `obj` pointer is guaranteed to be valid.
139         unsafe { <T::Object as IntoGEMObject>::from_raw((*self.inner.get()).obj) }
140     }
141 
142     /// The driver data with this buffer object.
143     #[inline]
144     pub fn data(&self) -> &T::VmBoData {
145         &self.data
146     }
147 
148     pub(super) fn lock_gpuva(&self) -> crate::sync::MutexGuard<'_, ()> {
149         // SAFETY: The GEM object is valid.
150         let ptr = unsafe { &raw mut (*self.obj().as_raw()).gpuva.lock };
151         // SAFETY: The GEM object is valid, so the mutex is properly initialized.
152         let mutex = unsafe { crate::sync::Mutex::from_raw(ptr) };
153         mutex.lock()
154     }
155 }
156 
157 /// A pre-allocated [`GpuVmBo`] object.
158 ///
159 /// # Invariants
160 ///
161 /// Points at a `drm_gpuvm_bo` that contains a valid `T::VmBoData`, has a refcount of one, and is
162 /// absent from any gem, extobj, or evict lists.
163 pub(super) struct GpuVmBoAlloc<T: DriverGpuVm>(NonNull<GpuVmBo<T>>);
164 
165 impl<T: DriverGpuVm> GpuVmBoAlloc<T> {
166     /// Create a new pre-allocated [`GpuVmBo`].
167     ///
168     /// It's intentional that the initializer is infallible because `drm_gpuvm_bo_put` will call
169     /// drop on the data, so we don't have a way to free it when the data is missing.
170     #[inline]
171     pub(super) fn new(
172         gpuvm: &GpuVm<T>,
173         gem: &T::Object,
174         value: impl PinInit<T::VmBoData>,
175     ) -> Result<GpuVmBoAlloc<T>, AllocError> {
176         // CAST: `GpuVmBoAlloc::vm_bo_alloc` ensures that this memory was allocated with the layout
177         // of `GpuVmBo<T>`. The type is repr(C), so `container_of` is not required.
178         // SAFETY: The provided gpuvm and gem ptrs are valid for the duration of this call.
179         let raw_ptr = unsafe {
180             bindings::drm_gpuvm_bo_create(gpuvm.as_raw(), gem.as_raw()).cast::<GpuVmBo<T>>()
181         };
182         let ptr = NonNull::new(raw_ptr).ok_or(AllocError)?;
183         // SAFETY: `ptr->data` is a valid pinned location.
184         let Ok(()) = unsafe { value.__pinned_init(&raw mut (*raw_ptr).data) };
185         // INVARIANTS: We just created the vm_bo so it's absent from lists, and the data is valid
186         // as we just initialized it.
187         Ok(GpuVmBoAlloc(ptr))
188     }
189 
190     /// Returns a raw pointer to underlying C value.
191     #[inline]
192     pub(super) fn as_raw(&self) -> *mut bindings::drm_gpuvm_bo {
193         // SAFETY: The pointer references a valid `drm_gpuvm_bo`.
194         unsafe { (*self.0.as_ptr()).inner.get() }
195     }
196 
197     /// Look up whether there is an existing [`GpuVmBo`] for this gem object.
198     ///
199     /// The caller should not hold the GEM mutex or DMA resv lock.
200     #[inline]
201     pub(super) fn obtain(self) -> ARef<GpuVmBo<T>> {
202         let me = ManuallyDrop::new(self);
203         // SAFETY: Valid `drm_gpuvm_bo` not already in the lists. We do not access `me` after this
204         // call.
205         let ptr = unsafe { bindings::drm_gpuvm_bo_obtain_prealloc(me.as_raw()) };
206 
207         // SAFETY: `drm_gpuvm_bo_obtain_prealloc` always returns a non-null ptr
208         let nonnull = unsafe { NonNull::new_unchecked(ptr.cast()) };
209 
210         // INVARIANTS: `drm_gpuvm_bo_obtain_prealloc` ensures that the bo is in the GEM list.
211         // SAFETY: We received one refcount from `drm_gpuvm_bo_obtain_prealloc`.
212         let ret = unsafe { ARef::<GpuVmBo<T>>::from_raw(nonnull) };
213 
214         // Ensure that external objects are in the extobj list.
215         //
216         // Note that we must call `extobj_add` even if `ptr != me` to avoid a race condition where
217         // we could end up using the extobj before the thread with `ptr == me` calls extobj_add.
218         if ret.gpuvm().is_extobj(ret.obj()) {
219             let resv_lock = ret.gpuvm().raw_resv();
220             // TODO: Use a proper lock guard here once a dma_resv lock abstraction exists.
221             // SAFETY: The GPUVM is still alive, so its resv lock is too.
222             unsafe { bindings::dma_resv_lock(resv_lock, ptr::null_mut()) };
223             // SAFETY: We hold the GPUVMs resv lock.
224             unsafe { bindings::drm_gpuvm_bo_extobj_add(ptr) };
225             // SAFETY: We took the lock, so we can unlock it.
226             unsafe { bindings::dma_resv_unlock(resv_lock) };
227         }
228 
229         ret
230     }
231 }
232 
233 impl<T: DriverGpuVm> Deref for GpuVmBoAlloc<T> {
234     type Target = GpuVmBo<T>;
235     #[inline]
236     fn deref(&self) -> &GpuVmBo<T> {
237         // SAFETY: By the type invariants we may deref while `Self` exists.
238         unsafe { self.0.as_ref() }
239     }
240 }
241 
242 impl<T: DriverGpuVm> Drop for GpuVmBoAlloc<T> {
243     #[inline]
244     fn drop(&mut self) {
245         // TODO: Call drm_gpuvm_bo_destroy_not_in_lists() directly.
246         // SAFETY: It's safe to perform a deferred put in any context.
247         unsafe { bindings::drm_gpuvm_bo_put_deferred(self.as_raw()) };
248     }
249 }
250