mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Alice Ryhl <aliceryhl@google.com>
To: Danilo Krummrich <dakr@kernel.org>
Cc: "Daniel Almeida" <daniel.almeida@collabora.com>,
	"Miguel Ojeda" <ojeda@kernel.org>,
	"Boqun Feng" <boqun@kernel.org>, "Gary Guo" <gary@garyguo.net>,
	"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
	"Benno Lossin" <lossin@kernel.org>,
	"Andreas Hindborg" <a.hindborg@kernel.org>,
	"Trevor Gross" <tmgross@umich.edu>,
	"Tamir Duberstein" <tamird@kernel.org>,
	"Alexandre Courbot" <acourbot@nvidia.com>,
	"Onur Özkan" <work@onurozkan.dev>,
	nova-gpu@lists.linux.dev, dri-devel@lists.freedesktop.org,
	linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org,
	"Alice Ryhl" <aliceryhl@google.com>
Subject: [PATCH 2/2] rust: drm: ioctl: document declare_drm_ioctls! arguments and assertions
Date: Mon, 30 Nov 2026 10:17:45 +0000	[thread overview]
Message-ID: <20261130-drm-ioctl-return-i32-v1-2-2134e8274300@google.com> (raw)
In-Reply-To: <20261130-drm-ioctl-return-i32-v1-0-2134e8274300@google.com>

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 <aliceryhl@google.com>
---
 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;
                 )*
             };

-- 
2.56.1.839.g22a8290db9-goog


  parent reply	other threads:[~2026-11-30 10:18 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-30 10:17 [PATCH 0/2] rust: drm: ioctl: make DRM ioctls easier to understand Alice Ryhl
2026-11-30 10:17 ` [PATCH 1/2] rust: drm: ioctl: return Result<i32> from ioctl handlers Alice Ryhl
2026-11-30 11:43   ` Janne Grunau
2026-11-30 12:33     ` Alice Ryhl
2026-11-30 11:48   ` Miguel Ojeda
2026-11-30 12:32     ` Alice Ryhl
2026-11-30 14:19       ` Janne Grunau
2026-11-30 13:15     ` Gary Guo
2026-11-30 10:17 ` Alice Ryhl [this message]
2026-11-30 11:45   ` [PATCH 2/2] rust: drm: ioctl: document declare_drm_ioctls! arguments and assertions Janne Grunau

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261130-drm-ioctl-return-i32-v1-2-2134e8274300@google.com \
    --to=aliceryhl@google.com \
    --cc=a.hindborg@kernel.org \
    --cc=acourbot@nvidia.com \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun@kernel.org \
    --cc=dakr@kernel.org \
    --cc=daniel.almeida@collabora.com \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=gary@garyguo.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lossin@kernel.org \
    --cc=nova-gpu@lists.linux.dev \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=tamird@kernel.org \
    --cc=tmgross@umich.edu \
    --cc=work@onurozkan.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®