Skip to content

Commit 2e9fdbe

Browse files
author
Danilo Krummrich
committed
rust: drm: device: drop_in_place() the drm::Device in release()
In drm::Device::new() we allocate with __drm_dev_alloc() and return an ARef<drm::Device>. When the reference count of the drm::Device falls to zero, the C code automatically calls drm_dev_release(), which eventually frees the memory allocated in drm::Device::new(). However, due to that, drm::Device::drop() is never called. As a result the destructor of the user's private data, i.e. drm::Device::data is never called. Hence, fix this by calling drop_in_place() from the DRM device's release callback. Fixes: 1e4b889 ("rust: drm: add device abstraction") Reviewed-by: Alice Ryhl <[email protected]> Signed-off-by: Danilo Krummrich <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent e79d0ba commit 2e9fdbe

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

rust/kernel/drm/device.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ impl<T: drm::Driver> Device<T> {
6666
open: Some(drm::File::<T::File>::open_callback),
6767
postclose: Some(drm::File::<T::File>::postclose_callback),
6868
unload: None,
69-
release: None,
69+
release: Some(Self::release),
7070
master_set: None,
7171
master_drop: None,
7272
debugfs_init: None,
@@ -162,6 +162,16 @@ impl<T: drm::Driver> Device<T> {
162162
// SAFETY: `ptr` is valid by the safety requirements of this function.
163163
unsafe { &*ptr.cast() }
164164
}
165+
166+
extern "C" fn release(ptr: *mut bindings::drm_device) {
167+
// SAFETY: `ptr` is a valid pointer to a `struct drm_device` and embedded in `Self`.
168+
let this = unsafe { Self::from_drm_device(ptr) };
169+
170+
// SAFETY:
171+
// - When `release` runs it is guaranteed that there is no further access to `this`.
172+
// - `this` is valid for dropping.
173+
unsafe { core::ptr::drop_in_place(this) };
174+
}
165175
}
166176

167177
impl<T: drm::Driver> Deref for Device<T> {

0 commit comments

Comments
 (0)