[PATCH 3/7] drm/rust: add File::device() helper
From: Deborah Brouwer
Date: Thu May 07 2026 - 19:33:40 EST
Add a helper to retrieve the DRM device associated with an open
`drm::file::File<T>`.
This wraps the common pattern of traversing `drm_file.minor->dev` and
returning the corresponding `drm::Device<T::Driver>`.
This is useful for DRM file operation callbacks that need access to the
owning DRM device from a `File<T>` reference, avoiding repeated open-coded
raw pointer traversal in drivers.
Signed-off-by: Deborah Brouwer <deborah.brouwer@xxxxxxxxxxxxx>
---
rust/kernel/drm/file.rs | 12 ++++++++++++
1 file changed, 12 insertions(+)
diff --git a/rust/kernel/drm/file.rs b/rust/kernel/drm/file.rs
index 10160601ce5a..5af28a9558e9 100644
--- a/rust/kernel/drm/file.rs
+++ b/rust/kernel/drm/file.rs
@@ -45,6 +45,18 @@ pub(super) fn as_raw(&self) -> *mut bindings::drm_file {
self.0.get()
}
+ /// Returns the DRM device associated with this open file.
+ pub fn device(&self) -> &drm::Device<T::Driver> {
+ // SAFETY: By the type invariant, `self.as_raw()` points to a valid open
+ // `struct drm_file`. DRM initializes `minor` for open DRM files, and
+ // `minor->dev` points to the registered DRM device associated with this
+ // file.
+ unsafe {
+ let minor = (*self.as_raw()).minor;
+ drm::Device::from_raw((*minor).dev)
+ }
+ }
+
fn driver_priv(&self) -> *mut T {
// SAFETY: By the type invariants of `Self`, `self.as_raw()` is always valid.
unsafe { (*self.as_raw()).driver_priv }.cast()
--
2.53.0