Skip to content

Commit 36b1ccb

Browse files
LyudeDanilo Krummrich
authored andcommitted
rust: drm: gem: Refactor IntoGEMObject::from_gem_obj() to as_ref()
There's a few issues with this function, mainly: * This function -probably- should have been unsafe from the start. Pointers are not always necessarily valid, but you want a function that does field-projection for a pointer that can travel outside of the original struct to be unsafe, at least if I understand properly. * *mut Self is not terribly useful in this context, the majority of uses of from_gem_obj() grab a *mut Self and then immediately convert it into a &'a Self. It also goes against the ffi conventions we've set in the rest of the kernel thus far. * from_gem_obj() also doesn't follow the naming conventions in the rest of the DRM bindings at the moment, as_ref() would be a better name. So, let's: * Make from_gem_obj() unsafe * Convert it to return &'a Self * Rename it to as_ref() * Update all call locations Signed-off-by: Lyude Paul <[email protected]> Reviewed-by: Daniel Almeida <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Danilo Krummrich <[email protected]>
1 parent 6ee48ae commit 36b1ccb

File tree

1 file changed

+43
-26
lines changed

1 file changed

+43
-26
lines changed

rust/kernel/drm/gem/mod.rs

Lines changed: 43 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,14 @@ pub trait IntoGEMObject: Sized + super::private::Sealed {
4545
#[allow(clippy::wrong_self_convention)]
4646
fn into_gem_obj(&self) -> &Opaque<bindings::drm_gem_object>;
4747

48-
/// Converts a pointer to a `struct drm_gem_object` into a pointer to `Self`.
49-
fn from_gem_obj(obj: *mut bindings::drm_gem_object) -> *mut Self;
48+
/// Converts a pointer to a `struct drm_gem_object` into a reference to `Self`.
49+
///
50+
/// # Safety
51+
///
52+
/// - `self_ptr` must be a valid pointer to `Self`.
53+
/// - The caller promises that holding the immutable reference returned by this function does
54+
/// not violate rust's data aliasing rules and remains valid throughout the lifetime of `'a`.
55+
unsafe fn as_ref<'a>(self_ptr: *mut bindings::drm_gem_object) -> &'a Self;
5056
}
5157

5258
/// Trait which must be implemented by drivers using base GEM objects.
@@ -63,14 +69,13 @@ extern "C" fn open_callback<T: BaseDriverObject<U>, U: BaseObject>(
6369
let file = unsafe {
6470
drm::File::<<<U as IntoGEMObject>::Driver as drm::Driver>::File>::as_ref(raw_file)
6571
};
66-
let obj =
67-
<<<U as IntoGEMObject>::Driver as drm::Driver>::Object as IntoGEMObject>::from_gem_obj(
68-
raw_obj,
69-
);
70-
71-
// SAFETY: `from_gem_obj()` returns a valid pointer as long as the type is correct and the
72-
// `raw_obj` we got is valid.
73-
match T::open(unsafe { &*obj }, file) {
72+
// SAFETY: `open_callback` is specified in the AllocOps structure for `Object<T>`, ensuring that
73+
// `raw_obj` is indeed contained within a `Object<T>`.
74+
let obj = unsafe {
75+
<<<U as IntoGEMObject>::Driver as drm::Driver>::Object as IntoGEMObject>::as_ref(raw_obj)
76+
};
77+
78+
match T::open(obj, file) {
7479
Err(e) => e.to_errno(),
7580
Ok(()) => 0,
7681
}
@@ -84,14 +89,13 @@ extern "C" fn close_callback<T: BaseDriverObject<U>, U: BaseObject>(
8489
let file = unsafe {
8590
drm::File::<<<U as IntoGEMObject>::Driver as drm::Driver>::File>::as_ref(raw_file)
8691
};
87-
let obj =
88-
<<<U as IntoGEMObject>::Driver as drm::Driver>::Object as IntoGEMObject>::from_gem_obj(
89-
raw_obj,
90-
);
91-
92-
// SAFETY: `from_gem_obj()` returns a valid pointer as long as the type is correct and the
93-
// `raw_obj` we got is valid.
94-
T::close(unsafe { &*obj }, file);
92+
// SAFETY: `close_callback` is specified in the AllocOps structure for `Object<T>`, ensuring
93+
// that `raw_obj` is indeed contained within a `Object<T>`.
94+
let obj = unsafe {
95+
<<<U as IntoGEMObject>::Driver as drm::Driver>::Object as IntoGEMObject>::as_ref(raw_obj)
96+
};
97+
98+
T::close(obj, file);
9599
}
96100

97101
impl<T: DriverObject> IntoGEMObject for Object<T> {
@@ -101,9 +105,10 @@ impl<T: DriverObject> IntoGEMObject for Object<T> {
101105
&self.obj
102106
}
103107

104-
fn from_gem_obj(obj: *mut bindings::drm_gem_object) -> *mut Self {
105-
// SAFETY: All of our objects are Object<T>.
106-
unsafe { crate::container_of!(obj, Object<T>, obj).cast_mut() }
108+
unsafe fn as_ref<'a>(self_ptr: *mut bindings::drm_gem_object) -> &'a Self {
109+
// SAFETY: `obj` is guaranteed to be in an `Object<T>` via the safety contract of this
110+
// function
111+
unsafe { &*crate::container_of!(self_ptr, Object<T>, obj) }
107112
}
108113
}
109114

@@ -144,11 +149,23 @@ where
144149
) -> Result<ARef<Self>> {
145150
// SAFETY: The arguments are all valid per the type invariants.
146151
let ptr = unsafe { bindings::drm_gem_object_lookup(file.as_raw().cast(), handle) };
147-
let ptr = <Self as IntoGEMObject>::from_gem_obj(ptr);
148-
let ptr = NonNull::new(ptr).ok_or(ENOENT)?;
149-
150-
// SAFETY: We take ownership of the reference of `drm_gem_object_lookup()`.
151-
Ok(unsafe { ARef::from_raw(ptr) })
152+
if ptr.is_null() {
153+
return Err(ENOENT);
154+
}
155+
156+
// SAFETY:
157+
// - A `drm::Driver` can only have a single `File` implementation.
158+
// - `file` uses the same `drm::Driver` as `Self`.
159+
// - Therefore, we're guaranteed that `ptr` must be a gem object embedded within `Self`.
160+
// - And we check if the pointer is null befoe calling as_ref(), ensuring that `ptr` is a
161+
// valid pointer to an initialized `Self`.
162+
let obj = unsafe { Self::as_ref(ptr) };
163+
164+
// SAFETY:
165+
// - We take ownership of the reference of `drm_gem_object_lookup()`.
166+
// - Our `NonNull` comes from an immutable reference, thus ensuring it is a valid pointer to
167+
// `Self`.
168+
Ok(unsafe { ARef::from_raw(obj.into()) })
152169
}
153170

154171
/// Creates an mmap offset to map the object from userspace.

0 commit comments

Comments
 (0)