Skip to content

Commit a2faec5

Browse files
hoshinolinamarcan
authored andcommitted
rust: drm: gem: Allow pinning GEM object driver data
This requires type_alias_impl_trait. Signed-off-by: Asahi Lina <lina@asahilina.net>
1 parent 9bc2928 commit a2faec5

1 file changed

Lines changed: 13 additions & 10 deletions

File tree

rust/kernel/drm/gem/shmem.rs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,10 @@ use crate::{
1010
prelude::*,
1111
};
1212
use core::{
13-
marker::PhantomData,
13+
marker::{PhantomData, PhantomPinned},
1414
mem,
1515
mem::MaybeUninit,
1616
ops::{Deref, DerefMut},
17-
ptr::addr_of_mut,
1817
slice,
1918
};
2019

@@ -66,7 +65,9 @@ const SHMEM_VM_OPS: bindings::vm_operations_struct = vm_numa_fields! {
6665

6766
/// A shmem-backed GEM object.
6867
#[repr(C)]
68+
#[pin_data]
6969
pub struct Object<T: DriverObject> {
70+
#[pin]
7071
obj: bindings::drm_gem_shmem_object,
7172
// The DRM core ensures the Device exists as long as its objects exist, so we don't need to
7273
// manage the reference count here.
@@ -75,16 +76,16 @@ pub struct Object<T: DriverObject> {
7576
inner: T,
7677
}
7778

79+
// SAFETY: drm_gem_shmem_object is safe to zero-initialize
80+
unsafe impl init::Zeroable for bindings::drm_gem_shmem_object {}
81+
7882
unsafe extern "C" fn gem_create_object<T: DriverObject>(
7983
dev: *mut bindings::drm_device,
8084
size: usize,
8185
) -> *mut bindings::drm_gem_object {
8286
let p = unsafe {
83-
bindings::krealloc(
84-
core::ptr::null(),
85-
Object::<T>::SIZE,
86-
bindings::GFP_KERNEL | bindings::__GFP_ZERO,
87-
) as *mut Object<T>
87+
bindings::krealloc(core::ptr::null(), Object::<T>::SIZE, bindings::GFP_KERNEL)
88+
as *mut Object<T>
8889
};
8990

9091
if p.is_null() {
@@ -106,8 +107,7 @@ unsafe extern "C" fn gem_create_object<T: DriverObject>(
106107
return e.to_ptr();
107108
}
108109

109-
// SAFETY: drm_gem_shmem_object is safe to zero-init, and
110-
// the rest of Object has been initialized
110+
// SAFETY: __pinned_init() guarantees the object has been initialized
111111
let new: &mut Object<T> = unsafe { &mut *(p as *mut _) };
112112

113113
new.obj.base.funcs = &Object::<T>::VTABLE;
@@ -166,7 +166,10 @@ impl<T: DriverObject> Object<T> {
166166

167167
// SAFETY: The gem_create_object callback ensures this is a valid Object<T>,
168168
// so we can take a unique reference to it.
169-
let obj_ref = gem::UniqueObjectRef { ptr: p };
169+
let obj_ref = gem::UniqueObjectRef {
170+
ptr: p,
171+
_p: PhantomPinned,
172+
};
170173

171174
Ok(obj_ref)
172175
}

0 commit comments

Comments
 (0)