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 1/2] rust: drm: ioctl: return Result<i32> from ioctl handlers
Date: Mon, 30 Nov 2026 10:17:44 +0000	[thread overview]
Message-ID: <20261130-drm-ioctl-return-i32-v1-1-2134e8274300@google.com> (raw)
In-Reply-To: <20261130-drm-ioctl-return-i32-v1-0-2134e8274300@google.com>

Currently drm ioctl handlers return a Result<u32> where errors are
translated to negative numbers and values are converted to a
non-negative i32. The values above i32::MAX are considered erroronous
and mapped to -ERANGE.

While teaching the Rust for Linux course, we ended up discussing the
above after a student found it confusing and asked about this return
value. Of course, the students knew that the C side returns a signed
integer, so it's not clear what values about i32::MAX would mean.

Based on this discussion, I'm suggesting here that we change the return
value to Result<i32>. This means that there are two ways of returning a
negative value to userspace (either Err(err) or Ok(-x)), and that
userspace cannot distinguish whether the driver did one or the other.
But in the course, people generally thought this was less confusing.

Thus, change `declare_drm_ioctls!` and its in-tree handlers in `nova`
and `tyr` to return `Result<i32>`.

Note this matches miscdevice's ioctl API that already behaves like this.

Signed-off-by: Alice Ryhl <aliceryhl@google.com>
---
 drivers/gpu/drm/nova/file.rs | 6 +++---
 drivers/gpu/drm/tyr/file.rs  | 2 +-
 rust/kernel/drm/ioctl.rs     | 5 ++---
 3 files changed, 6 insertions(+), 7 deletions(-)

diff --git a/drivers/gpu/drm/nova/file.rs b/drivers/gpu/drm/nova/file.rs
index 8b800f821e2c..e001bd814764 100644
--- a/drivers/gpu/drm/nova/file.rs
+++ b/drivers/gpu/drm/nova/file.rs
@@ -117,7 +117,7 @@ pub(crate) fn gem_create(
         _reg_data: &DrmRegData<'_>,
         req: &mut uapi::drm_nova_gem_create,
         file: &drm::File<NovaDriver>,
-    ) -> Result<u32> {
+    ) -> Result<i32> {
         let obj = NovaObject::new(dev, req.size.try_into()?)?;
 
         req.handle = obj.create_handle(file)?;
@@ -131,7 +131,7 @@ pub(crate) fn gem_info(
         _reg_data: &DrmRegData<'_>,
         req: &mut uapi::drm_nova_gem_info,
         file: &drm::File<NovaDriver>,
-    ) -> Result<u32> {
+    ) -> Result<i32> {
         let bo = NovaObject::lookup_handle(file, req.handle)?;
 
         req.size = bo.size().try_into()?;
@@ -145,7 +145,7 @@ pub(crate) fn info(
         reg_data: &DrmRegData<'_>,
         info: &mut uapi::drm_nova_info,
         _file: &drm::File<NovaDriver>,
-    ) -> Result<u32> {
+    ) -> Result<i32> {
         match info.id {
             uapi::DRM_NOVA_INFO_GPU => write_info(info, GpuInfo::new(reg_data)?)?,
             _ => return Err(EINVAL),
diff --git a/drivers/gpu/drm/tyr/file.rs b/drivers/gpu/drm/tyr/file.rs
index 9030e535b7d0..f328f4706b5e 100644
--- a/drivers/gpu/drm/tyr/file.rs
+++ b/drivers/gpu/drm/tyr/file.rs
@@ -75,7 +75,7 @@ pub(crate) fn dev_query(
         reg_data: &TyrDrmRegistrationData<'_>,
         devquery: &mut uapi::drm_panthor_dev_query,
         _file: &TyrDrmFile,
-    ) -> Result<u32> {
+    ) -> Result<i32> {
         if devquery.pointer == 0 {
             match devquery.type_ {
                 uapi::drm_panthor_dev_query_type_DRM_PANTHOR_DEV_QUERY_GPU_INFO => {
diff --git a/rust/kernel/drm/ioctl.rs b/rust/kernel/drm/ioctl.rs
index 0ed7dfaac851..b2e7244bd5c0 100644
--- a/rust/kernel/drm/ioctl.rs
+++ b/rust/kernel/drm/ioctl.rs
@@ -98,7 +98,7 @@ pub const fn __dev_ctx_cast<T: crate::drm::Driver>(
 ///        reg_data: &Self::RegistrationData<'_>,
 ///        data: &mut uapi::argument_type,
 ///        file: &kernel::drm::File<Self>,
-/// ) -> Result<u32>
+/// ) -> Result<i32>
 /// ```
 /// where `Self` is the drm::drv::Driver implementation these ioctls are being declared within.
 ///
@@ -201,8 +201,7 @@ macro_rules! declare_drm_ioctls {
                                 $func(&*guard, reg_data, data, file)
                             }) {
                                 Err(e) => e.to_errno(),
-                                Ok(i) => i.try_into()
-                                            .unwrap_or($crate::error::code::ERANGE.to_errno()),
+                                Ok(i) => i,
                             }
                         }
                         Some($cmd)

-- 
2.56.1.839.g22a8290db9-goog


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

Thread overview: 12+ 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 ` Alice Ryhl [this message]
2026-11-30 11:43   ` [PATCH 1/2] rust: drm: ioctl: return Result<i32> from ioctl handlers 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 15:35         ` Gary Guo
2026-11-30 16:04           ` Danilo Krummrich
2026-11-30 13:15     ` Gary Guo
2026-11-30 10:17 ` [PATCH 2/2] rust: drm: ioctl: document declare_drm_ioctls! arguments and assertions Alice Ryhl
2026-11-30 11:45   ` 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-1-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®