Skip to content

Commit cfec502

Browse files
author
Danilo Krummrich
committed
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: [email protected] Fixes: a4c9f71 ("rust: device: implement Device::parent()") Reviewed-by: Alice Ryhl <[email protected]> Reviewed-by: Alexandre Courbot <[email protected]> Acked-by: Greg Kroah-Hartman <[email protected]> Signed-off-by: Danilo Krummrich <[email protected]>
1 parent c7fbb82 commit cfec502

File tree

2 files changed

+3
-9
lines changed

2 files changed

+3
-9
lines changed

rust/kernel/auxiliary.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -217,13 +217,7 @@ impl<Ctx: device::DeviceContext> Device<Ctx> {
217217

218218
/// Returns a reference to the parent [`device::Device`], if any.
219219
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()
227221
}
228222
}
229223

rust/kernel/device.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ impl<Ctx: DeviceContext> Device<Ctx> {
251251

252252
/// Returns a reference to the parent device, if any.
253253
#[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> {
255255
// SAFETY:
256256
// - By the type invariant `self.as_raw()` is always valid.
257257
// - The parent device is only ever set at device creation.
@@ -264,7 +264,7 @@ impl<Ctx: DeviceContext> Device<Ctx> {
264264
// - Since `parent` is not NULL, it must be a valid pointer to a `struct device`.
265265
// - `parent` is valid for the lifetime of `self`, since a `struct device` holds a
266266
// reference count of its parent.
267-
Some(unsafe { Self::from_raw(parent) })
267+
Some(unsafe { Device::from_raw(parent) })
268268
}
269269
}
270270

0 commit comments

Comments
 (0)