Run every ioctl handler inside a drm_dev_enter/exit critical section via
RegistrationGuard. If the device has been unplugged, the ioctl returns
ENODEV without calling the handler.

A never-called closure anchors the driver type for the compiler by tying
dev's type to the handler's first parameter, which the compiler cannot
infer through method resolution and associated-type projections alone.

Signed-off-by: Danilo Krummrich <[email protected]>
---
 rust/kernel/drm/ioctl.rs | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)

diff --git a/rust/kernel/drm/ioctl.rs b/rust/kernel/drm/ioctl.rs
index cf328101dde4..c6a03be018e6 100644
--- a/rust/kernel/drm/ioctl.rs
+++ b/rust/kernel/drm/ioctl.rs
@@ -87,7 +87,10 @@ pub mod internal {
 ///        file: &kernel::drm::File<Self::File>,
 /// ) -> Result<u32>
 /// ```
-/// where `Self` is the drm::drv::Driver implementation these ioctls are being 
declared within.
+/// where `Self` is the `drm::Driver` implementation these ioctls are being 
declared within.
+///
+/// The ioctl runs inside a `drm_dev_enter/exit` critical section. If the 
device has been
+/// unplugged, the ioctl returns `ENODEV` without calling the handler.
 ///
 /// # Examples
 ///
@@ -135,6 +138,16 @@ macro_rules! declare_drm_ioctls {
                             // dev/file match the current driver these ioctls 
are being declared
                             // for, and it's not clear how to enforce this 
within the type system.
                             let dev = 
$crate::drm::device::Device::from_raw(raw_dev);
+
+                            // Type-inference anchor: the closure is never 
called but ties `dev`'s
+                            // type to `$func`'s first parameter, which the 
compiler cannot infer
+                            // through method resolution and associated-type 
projections alone.
+                            #[allow(unreachable_code)]
+                            let _ = || $func(dev, unreachable!(), 
unreachable!());
+
+                            let Some(_guard) = dev.registration_guard() else {
+                                return $crate::error::code::ENODEV.to_errno();
+                            };
                             // SAFETY: The ioctl argument has size 
`_IOC_SIZE(cmd)`, which we
                             // asserted above matches the size of this type, 
and all bit patterns of
                             // UAPI structs must be valid.
-- 
2.54.0

Reply via email to