[PATCH 4/7] drm/rust: allow drivers to override file operations

From: Deborah Brouwer

Date: Thu May 07 2026 - 19:34:26 EST


Add a Driver::FOPS associated constant so Rust DRM drivers can override
or extend the default file operations table.

Use the driver's FOPS in drm::Device instead of always constructing the
default GEM file operations internally.

Also make drm::gem::create_fops() public so drivers can build on top of
the default GEM handlers while overriding selected operations such as
mmap.

Signed-off-by: Deborah Brouwer <deborah.brouwer@xxxxxxxxxxxxx>
---
rust/kernel/drm/device.rs | 4 ++--
rust/kernel/drm/driver.rs | 6 ++++++
rust/kernel/drm/gem/mod.rs | 3 ++-
3 files changed, 10 insertions(+), 3 deletions(-)

diff --git a/rust/kernel/drm/device.rs b/rust/kernel/drm/device.rs
index adbafe8db54d..82760a9a426e 100644
--- a/rust/kernel/drm/device.rs
+++ b/rust/kernel/drm/device.rs
@@ -108,10 +108,10 @@ impl<T: drm::Driver> Device<T> {
driver_features: drm::driver::FEAT_GEM,
ioctls: T::IOCTLS.as_ptr(),
num_ioctls: T::IOCTLS.len() as i32,
- fops: &Self::GEM_FOPS,
+ fops: &Self::FOPS,
};

- const GEM_FOPS: bindings::file_operations = drm::gem::create_fops();
+ const FOPS: bindings::file_operations = T::FOPS;

/// Create a new `drm::Device` for a `drm::Driver`.
pub fn new(dev: &device::Device, data: impl PinInit<T::Data, Error>) -> Result<ARef<Self>> {
diff --git a/rust/kernel/drm/driver.rs b/rust/kernel/drm/driver.rs
index 5233bdebc9fc..79218c482fce 100644
--- a/rust/kernel/drm/driver.rs
+++ b/rust/kernel/drm/driver.rs
@@ -115,6 +115,12 @@ pub trait Driver {

/// IOCTL list. See `kernel::drm::ioctl::declare_drm_ioctls!{}`.
const IOCTLS: &'static [drm::ioctl::DrmIoctlDescriptor];
+
+ /// File operations for this driver.
+ ///
+ /// Override to replace or extend the default GEM file operations. The default
+ /// provides the standard GEM mmap handler via [`drm::gem::create_fops`].
+ const FOPS: bindings::file_operations = drm::gem::create_fops();
}

/// The registration type of a `drm::Device`.
diff --git a/rust/kernel/drm/gem/mod.rs b/rust/kernel/drm/gem/mod.rs
index 75acda7ba500..de7222ee0542 100644
--- a/rust/kernel/drm/gem/mod.rs
+++ b/rust/kernel/drm/gem/mod.rs
@@ -348,7 +348,8 @@ impl<T: DriverObject> AllocImpl for Object<T> {
};
}

-pub(super) const fn create_fops() -> bindings::file_operations {
+/// Returns the default GEM [`bindings::file_operations`] for a DRM driver.
+pub const fn create_fops() -> bindings::file_operations {
let mut fops: bindings::file_operations = pin_init::zeroed();

fops.owner = core::ptr::null_mut();

--
2.53.0