On Mon, Nov 30, 2026 at 10:17:45AM +0000, Alice Ryhl wrote: > The documentation for `declare_drm_ioctls!` is currently quite > incomplete, and the assertions do not provide a easily readable error > message. Thus update the docs and assertions to better document how to > create drm ioctls. > > This came up while I was teaching my Rust for Linux course. > > Signed-off-by: Alice Ryhl <[email protected]> > --- > rust/kernel/drm/ioctl.rs | 52 > ++++++++++++++++++++++++++++++++++++++---------- > 1 file changed, 42 insertions(+), 10 deletions(-) > > diff --git a/rust/kernel/drm/ioctl.rs b/rust/kernel/drm/ioctl.rs > index b2e7244bd5c0..d6a5a560824a 100644 > --- a/rust/kernel/drm/ioctl.rs > +++ b/rust/kernel/drm/ioctl.rs > @@ -90,17 +90,43 @@ pub const fn __dev_ctx_cast<T: crate::drm::Driver>( > /// > /// `(ioctl_number, argument_type, flags, user_callback),` > /// > -/// `argument_type` is the type name within the `bindings` crate. > -/// `user_callback` should have the following prototype: > +/// where: > +/// > +/// - `ioctl_number` is the ioctl name without the `DRM_IOCTL_` prefix; the > macro prepends > +/// `DRM_IOCTL_` and looks up the resulting constant in the uapi header > (the header must be > +/// included in `rust/uapi/uapi_helper.h`). Entries must be listed in > ascending order with no > +/// gaps. > +/// - `argument_type` is the type name of the ioctl argument struct within > `uapi`. Its size must > +/// match the size encoded in the ioctl number. > +/// - `flags` is a bitmask of DRM ioctl flags such as > [`AUTH`](crate::drm::ioctl::AUTH), > +/// [`MASTER`](crate::drm::ioctl::MASTER), > [`ROOT_ONLY`](crate::drm::ioctl::ROOT_ONLY), or > +/// [`RENDER_ALLOW`](crate::drm::ioctl::RENDER_ALLOW). > +/// - `user_callback` is the handler function, which should have the > following prototype: > /// > /// ```ignore > -/// fn foo(device: &kernel::drm::Device<Self, kernel::drm::Registered>, > -/// reg_data: &Self::RegistrationData<'_>, > -/// data: &mut uapi::argument_type, > -/// file: &kernel::drm::File<Self>, > +/// fn foo( > +/// device: &kernel::drm::Device<Self, kernel::drm::Registered>, > +/// reg_data: &Self::RegistrationData<'_>, > +/// data: &mut uapi::argument_type, > +/// file: &kernel::drm::File<Self>, > /// ) -> Result<i32> > /// ``` > -/// where `Self` is the drm::drv::Driver implementation these ioctls are > being declared within. > +/// > +/// where `Self` is the [`drm::Driver`](crate::drm::Driver) implementation > these ioctls are being > +/// declared within. The callback receives four arguments: > +/// > +/// - `device`: the registered [`drm::Device`](crate::drm::Device) on which > the ioctl was invoked. > +/// - `reg_data`: the driver's > [`RegistrationData`](crate::drm::Driver::RegistrationData) provided > +/// when the device was registered. > +/// - `data`: a mutable reference to the ioctl argument struct copied from > userspace (or > +/// zero-initialized when the ioctl has no input). This parameter is > always `&mut` regardless of > +/// the ioctl direction, even for write-only (`DRM_IOW`) ioctls; > modifications are only copied > +/// back to userspace when the ioctl command includes `DRM_IOR` or > `DRM_IOWR`. > +/// - `file`: the [`drm::File`](crate::drm::File) representing the open file > descriptor on which > +/// the ioctl was called. > +/// > +/// Returning `Ok(val)` returns `val` as the `ioctl(2)` return value > (typically `0`), whereas > +/// `Err(err)` returns the corresponding negative errno. > /// > /// # Examples > /// > @@ -121,9 +147,15 @@ macro_rules! declare_drm_ioctls { > // and that the size of the specified type is correct. > $( > let cmd: u32 = > $crate::macros::concat_idents!(DRM_IOCTL_, $cmd); > - ::core::assert!(i == $crate::ioctl::_IOC_NR(cmd)); > - > ::core::assert!(core::mem::size_of::<$crate::uapi::$struct>() == > - $crate::ioctl::_IOC_SIZE(cmd)); > + ::core::assert!( > + i == $crate::ioctl::_IOC_NR(cmd), > + "DRM ioctls must be listed in order starting from > DRM_COMMAND_BASE with no gaps" > + ); > + ::core::assert!( > + ::core::mem::size_of::<$crate::uapi::$struct>() == > + $crate::ioctl::_IOC_SIZE(cmd), > + "DRM ioctl argument type size does not match ioctl > command size" > + ); > let i: u32 = i + 1; > )* > };
Reviewed-by: Janne Grunau <[email protected]> Janne
