[PATCH 1/2] rust: drm: ioctl: return Result<i32> from ioctl handlers

From: Alice Ryhl

Date: Mon Nov 30 2026 - 05:18:22 EST


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@xxxxxxxxxx>
---
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