Skip to contentSkip to conversation

rust: device: fix device context of Device::parent()

PATCH Applied Danilo Krummrich 4 participants 5 messages Oct 16, 2025 – Oct 17, 2025

Subsystems (4)
lore
Share & tools
lore

Danilo Krummrich

author maintainer
#0 lore raw

PATCH 1/1 rust: device: fix device context of Device::parent()

Regardless of the DeviceContext of a device, we can't give any guarantees about the DeviceContext of its parent device.

This is very subtle, since it's only caused by a simple typo, i.e.

Self::from_raw(parent)

which preserves the DeviceContext in this case, vs.

Device::from_raw(parent)

which discards the DeviceContext.

(I should have noticed it doing the correct thing in auxiliary::Device subsequently, but somehow missed it.)

Hence, fix both Device::parent() and auxiliary::Device::parent().

Fixes a4c9f71e3440 ("rust: device: implement Device::parent()")
---
 rust/kernel/auxiliary.rs | 8 +-------
 rust/kernel/device.rs    | 4 ++--
 2 files changed, 3 insertions(+), 9 deletions(-)
rust/kernel/auxiliary.rs +1 -7
index 4163129b4103..e12f78734606 100644
@@ -217,13 +217,7 @@ pub fn id(&self) -> u32 {
 
     /// Returns a reference to the parent [`device::Device`], if any.
     pub fn parent(&self) -> Option<&device::Device> {
-        let ptr: *const Self = self;
-        // CAST: `Device<Ctx: DeviceContext>` types are transparent to each other.
-        let ptr: *const Device = ptr.cast();
-        // SAFETY: `ptr` was derived from `&self`.
-        let this = unsafe { &*ptr };
-
-        this.as_ref().parent()
+        self.as_ref().parent()
     }
 }
 
rust/kernel/device.rs +2 -2
index 23a95324cb0f..343996027c89 100644
@@ -256,7 +256,7 @@ pub(crate) fn as_raw(&self) -> *mut bindings::device {
 
     /// Returns a reference to the parent device, if any.
     #[cfg_attr(not(CONFIG_AUXILIARY_BUS), expect(dead_code))]
-    pub(crate) fn parent(&self) -> Option<&Self> {
+    pub(crate) fn parent(&self) -> Option<&Device> {
         // SAFETY:
         // - By the type invariant `self.as_raw()` is always valid.
         // - The parent device is only ever set at device creation.
@@ -269,7 +269,7 @@ pub(crate) fn parent(&self) -> Option<&Self> {
             // - Since `parent` is not NULL, it must be a valid pointer to a `struct device`.
             // - `parent` is valid for the lifetime of `self`, since a `struct device` holds a
             //   reference count of its parent.
-            Some(unsafe { Self::from_raw(parent) })
+            Some(unsafe { Device::from_raw(parent) })
         }
     }
 
Signature · 1 line
2.51.0
maintainer Acked-by on v1 1/1
#1 lore raw
Quoted 18 lines · Danilo Krummrich

Regardless of the DeviceContext of a device, we can't give any guarantees about the DeviceContext of its parent device.

This is very subtle, since it's only caused by a simple typo, i.e.

Self::from_raw(parent)

which preserves the DeviceContext in this case, vs.

Device::from_raw(parent)

which discards the DeviceContext.

(I should have noticed it doing the correct thing in auxiliary::Device subsequently, but somehow missed it.)

Hence, fix both Device::parent() and auxiliary::Device::parent().

Quoted 7 lines
Fixes a4c9f71e3440 ("rust: device: implement Device::parent()")
---
 rust/kernel/auxiliary.rs | 8 +-------
 rust/kernel/device.rs    | 4 ++--
 2 files changed, 3 insertions(+), 9 deletions(-)
reviewer Reviewed-by on v1 1/1
#2 lore raw
In reply to Danilo Krummrich
Quoted 13 lines · Danilo Krummrich

Regardless of the DeviceContext of a device, we can't give any guarantees about the DeviceContext of its parent device.

This is very subtle, since it's only caused by a simple typo, i.e.

Self::from_raw(parent)

which preserves the DeviceContext in this case, vs.

Device::from_raw(parent)

which discards the DeviceContext.

Quoted 8 lines

(I should have noticed it doing the correct thing in auxiliary::Device subsequently, but somehow missed it.)

Hence, fix both Device::parent() and auxiliary::Device::parent().

Fixes a4c9f71e3440 ("rust: device: implement Device::parent()")

Ouch, that will make me think twice the next time I consider using Self for convenience... :)

reviewer Reviewed-by on v1 1/1
#3 lore raw
In reply to Danilo Krummrich
Quoted 13 lines · Danilo Krummrich

Regardless of the DeviceContext of a device, we can't give any guarantees about the DeviceContext of its parent device.

This is very subtle, since it's only caused by a simple typo, i.e.

Self::from_raw(parent)

which preserves the DeviceContext in this case, vs.

Device::from_raw(parent)

which discards the DeviceContext.

Quoted 8 lines

(I should have noticed it doing the correct thing in auxiliary::Device subsequently, but somehow missed it.)

Hence, fix both Device::parent() and auxiliary::Device::parent().

Fixes a4c9f71e3440 ("rust: device: implement Device::parent()")
author maintainer Applied
#4 lore raw
In reply to Danilo Krummrich
Quoted 13 lines · Danilo Krummrich

Regardless of the DeviceContext of a device, we can't give any guarantees about the DeviceContext of its parent device.

This is very subtle, since it's only caused by a simple typo, i.e.

Self::from_raw(parent)

which preserves the DeviceContext in this case, vs.

Device::from_raw(parent)

which discards the DeviceContext.

Quoted 8 lines

(I should have noticed it doing the correct thing in auxiliary::Device subsequently, but somehow missed it.)

Hence, fix both Device::parent() and auxiliary::Device::parent().

Fixes a4c9f71e3440 ("rust: device: implement Device::parent()")

Applied to driver-core-linus, thanks!