rust: drm: ioctl: fix call to unsafe function
Subsystems (3)
PATCH 1/1 rust: drm: ioctl: fix call to unsafe function
dev is created via an unsafe pointer dereference. Following the convention, it should be wrapped in an unsafe block, but it is not.
The compiler only properly lints declare_drm_ioctls! when its defined in the same crate as it expands in. Since upstream drivers are all separate crates, this is silently ignored.
This has been present since commit 9a69570682b1 ("rust: drm: ioctl:
Add DRM ioctl abstraction").
--------------------
Based on drm-rust-next.
I encountered this while trying to build a rust driver out of tree. I copied the macro to an out-of-tree crate so that our uapi can see the same generated values without needing the generated bindings.
Ai has told me that this is because the macro expands from the same crate its defined in, which sounds sensible.
I see:
error[E0133]: call to unsafe function kernel::drm::Device::<T, C>::from_raw is unsafe and requires unsafe block
--> driver/drm_ioctl.rs:77:33
| 77 | ... ::kernel::drm::device::Device::from_raw(raw_dev); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ call to unsafe function |
Plus a number of warnings. But warnings are warnings so those are whatever... Its all silently ignored upstream anyways, so doesn't need a cc-stable either.
--- rust/kernel/drm/ioctl.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
rust/kernel/drm/ioctl.rs +1 -1
@@ -152,7 +152,7 @@ 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 <_ , $crate::drm::Ioctl > = - $crate::drm::device::Device ::from_raw (raw_dev); +unsafe { $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
Signature · 1 line
2.55.0
Quoted 5 lines · Vishal Moola (Fractile)
Fixes 9a69570682b1 ("rust: drm: ioctl: Add DRM ioctl abstraction")Signed-off-by Vishal Moola (Fractile) <[email protected]>--------------------
Based on drm-rust-next.
Note that patch commentary needs to go after a three-dash --- line rather than --------------------, otherwise git am / b4 includes everything above --- in the commit message. No need to resend just for that as we can strip it when applying.
Alice
Quoted 11 lines · Alice Ryhl
Quoted 5 lines · Vishal Moola (Fractile)
Fixes 9a69570682b1 ("rust: drm: ioctl: Add DRM ioctl abstraction")Signed-off-by Vishal Moola (Fractile) <[email protected]>--------------------
Based on drm-rust-next.Note that patch commentary needs to go after a three-dash
---line rather than--------------------, otherwisegit am/b4includes everything above---in the commit message. No need to resend just for that as we can strip it when applying.
Ah, I never realized the tools were that particular about exactly three dashes. Thanks for pointing that out, I'll keep that in mind for the future :).
Quoted 3 lines
Reviewed-by Alice Ryhl <[email protected]>Alice
Danilo Krummrich
maintainerrust/kernel/drm/ioctl.rs macro_rules! declare_drm_ioctls { · line 155
Quoted 10 lines · Vishal Moola (Fractile)
rust/kernel/drm/ioctl.rs +1
-1@@ -152,7 +152,7 @@ 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 <_ , $crate::drm::Ioctl > = - $crate::drm::device::Device ::from_raw (raw_dev); +unsafe { $crate::drm::device::Device ::from_raw (raw_dev) };
I remember there has been at least one compiler version in the supported range that complained when the safety comment isn't directly above the unsafe block. Has this been fixed in the meantime?
Quoted 15 lines · Danilo Krummrich
rust/kernel/drm/ioctl.rs macro_rules! declare_drm_ioctls { · line 155
Quoted 10 lines · Vishal Moola (Fractile)
rust/kernel/drm/ioctl.rs +1
-1@@ -152,7 +152,7 @@ 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 <_ , $crate::drm::Ioctl > = - $crate::drm::device::Device ::from_raw (raw_dev); +unsafe { $crate::drm::device::Device ::from_raw (raw_dev) };I remember there has been at least one compiler version in the supported range that complained when the safety comment isn't directly above the unsafe block. Has this been fixed in the meantime?
I'm not sure? I'm compiling with rustc 1.98.1 (48a229cea 2026-09-01) (Fedora 1.98.1-1.fc44) which gives me no errors like this.
Hopefully the build-bots catch it if there's a specific compiler version that still dislikes it.
The following command shows a few other occurences in the kernel like this, so if its still an issue I imagine it'd likely be caught somewhere:
grep -rn -B1 '^\s*unsafe {' rust/kernel | grep '=$'Gary Guo
reviewerQuoted 20 lines · Vishal Moola (Fractile)
Quoted 15 lines · Danilo Krummrich
rust/kernel/drm/ioctl.rs macro_rules! declare_drm_ioctls { · line 155
Quoted 10 lines · Vishal Moola (Fractile)
rust/kernel/drm/ioctl.rs +1
-1@@ -152,7 +152,7 @@ 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 <_ , $crate::drm::Ioctl > = - $crate::drm::device::Device ::from_raw (raw_dev); +unsafe { $crate::drm::device::Device ::from_raw (raw_dev) };I remember there has been at least one compiler version in the supported range that complained when the safety comment isn't directly above the unsafe block. Has this been fixed in the meantime?
I'm not sure? I'm compiling with rustc 1.98.1 (
48a229cea2026-09-01) (Fedora 1.98.1-1.fc44) which gives me no errors like this.
Quoted 7 lines
Hopefully the build-bots catch it if there's a specific compiler version that still dislikes it.
The following command shows a few other occurences in the kernel like this, so if its still an issue I imagine it'd likely be caught somewhere:
grep -rn -B1 '^\s*unsafe {' rust/kernel | grep '=$'
In this particular case, Clippy 1.85 doesn't complain even if SAFETY comment is removed, so we are all good.
Old version of clippy (the minimum version we require is 1.85) have both false positive and false negatives and sometimes things just cancel out :)
Best,
Gary