rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] rust: device: fix device context of Device::parent()
@ 2025-10-16 13:31 Danilo Krummrich
  2025-10-16 13:47 ` Greg KH
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: Danilo Krummrich @ 2025-10-16 13:31 UTC (permalink / raw)
  To: gregkh, rafael, david.m.ertman, ira.weiny, leon, ojeda,
	alex.gaynor, boqun.feng, gary, bjorn3_gh, lossin, a.hindborg,
	aliceryhl, tmgross
  Cc: rust-for-linux, linux-kernel, Danilo Krummrich, stable

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().

Cc: stable@vger.kernel.org
Fixes: a4c9f71e3440 ("rust: device: implement Device::parent()")
Signed-off-by: Danilo Krummrich <dakr@kernel.org>
---
 rust/kernel/auxiliary.rs | 8 +-------
 rust/kernel/device.rs    | 4 ++--
 2 files changed, 3 insertions(+), 9 deletions(-)

diff --git a/rust/kernel/auxiliary.rs b/rust/kernel/auxiliary.rs
index 4163129b4103..e12f78734606 100644
--- a/rust/kernel/auxiliary.rs
+++ b/rust/kernel/auxiliary.rs
@@ -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()
     }
 }
 
diff --git a/rust/kernel/device.rs b/rust/kernel/device.rs
index 23a95324cb0f..343996027c89 100644
--- a/rust/kernel/device.rs
+++ b/rust/kernel/device.rs
@@ -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) })
         }
     }
 
-- 
2.51.0


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] rust: device: fix device context of Device::parent()
  2025-10-16 13:31 [PATCH] rust: device: fix device context of Device::parent() Danilo Krummrich
@ 2025-10-16 13:47 ` Greg KH
  2025-10-17  2:55 ` Alexandre Courbot
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: Greg KH @ 2025-10-16 13:47 UTC (permalink / raw)
  To: Danilo Krummrich
  Cc: rafael, david.m.ertman, ira.weiny, leon, ojeda, alex.gaynor,
	boqun.feng, gary, bjorn3_gh, lossin, a.hindborg, aliceryhl,
	tmgross, rust-for-linux, linux-kernel, stable

On Thu, Oct 16, 2025 at 03:31:44PM +0200, Danilo Krummrich wrote:
> 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().
> 
> Cc: stable@vger.kernel.org
> Fixes: a4c9f71e3440 ("rust: device: implement Device::parent()")
> Signed-off-by: Danilo Krummrich <dakr@kernel.org>
> ---
>  rust/kernel/auxiliary.rs | 8 +-------
>  rust/kernel/device.rs    | 4 ++--
>  2 files changed, 3 insertions(+), 9 deletions(-)

Acked-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] rust: device: fix device context of Device::parent()
  2025-10-16 13:31 [PATCH] rust: device: fix device context of Device::parent() Danilo Krummrich
  2025-10-16 13:47 ` Greg KH
@ 2025-10-17  2:55 ` Alexandre Courbot
  2025-10-17 13:07 ` Alice Ryhl
  2025-10-17 21:25 ` Danilo Krummrich
  3 siblings, 0 replies; 5+ messages in thread
From: Alexandre Courbot @ 2025-10-17  2:55 UTC (permalink / raw)
  To: Danilo Krummrich, gregkh, rafael, david.m.ertman, ira.weiny, leon,
	ojeda, alex.gaynor, boqun.feng, gary, bjorn3_gh, lossin,
	a.hindborg, aliceryhl, tmgross
  Cc: rust-for-linux, linux-kernel, stable

On Thu Oct 16, 2025 at 10:31 PM JST, Danilo Krummrich wrote:
> 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().
>
> Cc: stable@vger.kernel.org
> Fixes: a4c9f71e3440 ("rust: device: implement Device::parent()")
> Signed-off-by: Danilo Krummrich <dakr@kernel.org>

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

Reviewed-by: Alexandre Courbot <acourbot@nvidia.com>


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] rust: device: fix device context of Device::parent()
  2025-10-16 13:31 [PATCH] rust: device: fix device context of Device::parent() Danilo Krummrich
  2025-10-16 13:47 ` Greg KH
  2025-10-17  2:55 ` Alexandre Courbot
@ 2025-10-17 13:07 ` Alice Ryhl
  2025-10-17 21:25 ` Danilo Krummrich
  3 siblings, 0 replies; 5+ messages in thread
From: Alice Ryhl @ 2025-10-17 13:07 UTC (permalink / raw)
  To: Danilo Krummrich
  Cc: gregkh, rafael, david.m.ertman, ira.weiny, leon, ojeda,
	alex.gaynor, boqun.feng, gary, bjorn3_gh, lossin, a.hindborg,
	tmgross, rust-for-linux, linux-kernel, stable

On Thu, Oct 16, 2025 at 03:31:44PM +0200, Danilo Krummrich wrote:
> 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().
> 
> Cc: stable@vger.kernel.org
> Fixes: a4c9f71e3440 ("rust: device: implement Device::parent()")
> Signed-off-by: Danilo Krummrich <dakr@kernel.org>

Reviewed-by: Alice Ryhl <aliceryhl@google.com>

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] rust: device: fix device context of Device::parent()
  2025-10-16 13:31 [PATCH] rust: device: fix device context of Device::parent() Danilo Krummrich
                   ` (2 preceding siblings ...)
  2025-10-17 13:07 ` Alice Ryhl
@ 2025-10-17 21:25 ` Danilo Krummrich
  3 siblings, 0 replies; 5+ messages in thread
From: Danilo Krummrich @ 2025-10-17 21:25 UTC (permalink / raw)
  To: gregkh, rafael, david.m.ertman, ira.weiny, leon, ojeda,
	alex.gaynor, boqun.feng, gary, bjorn3_gh, lossin, a.hindborg,
	aliceryhl, tmgross
  Cc: rust-for-linux, linux-kernel, stable

On Thu Oct 16, 2025 at 3:31 PM CEST, Danilo Krummrich wrote:
> 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().
>
> Cc: stable@vger.kernel.org
> Fixes: a4c9f71e3440 ("rust: device: implement Device::parent()")
> Signed-off-by: Danilo Krummrich <dakr@kernel.org>

Applied to driver-core-linus, thanks!

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2025-10-17 21:25 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-10-16 13:31 [PATCH] rust: device: fix device context of Device::parent() Danilo Krummrich
2025-10-16 13:47 ` Greg KH
2025-10-17  2:55 ` Alexandre Courbot
2025-10-17 13:07 ` Alice Ryhl
2025-10-17 21:25 ` Danilo Krummrich

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).