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

Cc: stable@vger.kernel.org
Fixes: a4c9f71e3440 ("rust: device: implement Device::parent()")
Reviewed-by: Alice Ryhl <aliceryhl@google.com>
Reviewed-by: Alexandre Courbot <acourbot@nvidia.com>
Acked-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: Danilo Krummrich <dakr@kernel.org>

+3 -9
+1 -7
rust/kernel/auxiliary.rs
··· 217 217 218 218 /// Returns a reference to the parent [`device::Device`], if any. 219 219 pub fn parent(&self) -> Option<&device::Device> { 220 - let ptr: *const Self = self; 221 - // CAST: `Device<Ctx: DeviceContext>` types are transparent to each other. 222 - let ptr: *const Device = ptr.cast(); 223 - // SAFETY: `ptr` was derived from `&self`. 224 - let this = unsafe { &*ptr }; 225 - 226 - this.as_ref().parent() 220 + self.as_ref().parent() 227 221 } 228 222 } 229 223
+2 -2
rust/kernel/device.rs
··· 251 251 252 252 /// Returns a reference to the parent device, if any. 253 253 #[cfg_attr(not(CONFIG_AUXILIARY_BUS), expect(dead_code))] 254 - pub(crate) fn parent(&self) -> Option<&Self> { 254 + pub(crate) fn parent(&self) -> Option<&Device> { 255 255 // SAFETY: 256 256 // - By the type invariant `self.as_raw()` is always valid. 257 257 // - The parent device is only ever set at device creation. ··· 264 264 // - Since `parent` is not NULL, it must be a valid pointer to a `struct device`. 265 265 // - `parent` is valid for the lifetime of `self`, since a `struct device` holds a 266 266 // reference count of its parent. 267 - Some(unsafe { Self::from_raw(parent) }) 267 + Some(unsafe { Device::from_raw(parent) }) 268 268 } 269 269 } 270 270