Skip to content

Commit 2b55b5b

Browse files
Danilo Krummrichgregkh
authored andcommitted
rust: device: fix device context of Device::parent()
commit cfec502 upstream. 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]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent d3c3522 commit 2b55b5b

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
@@ -250,7 +250,7 @@ impl<Ctx: DeviceContext> Device<Ctx> {
250250

251251
/// Returns a reference to the parent device, if any.
252252
#[cfg_attr(not(CONFIG_AUXILIARY_BUS), expect(dead_code))]
253-
pub(crate) fn parent(&self) -> Option<&Self> {
253+
pub(crate) fn parent(&self) -> Option<&Device> {
254254
// SAFETY:
255255
// - By the type invariant `self.as_raw()` is always valid.
256256
// - The parent device is only ever set at device creation.
@@ -263,7 +263,7 @@ impl<Ctx: DeviceContext> Device<Ctx> {
263263
// - Since `parent` is not NULL, it must be a valid pointer to a `struct device`.
264264
// - `parent` is valid for the lifetime of `self`, since a `struct device` holds a
265265
// reference count of its parent.
266-
Some(unsafe { Self::from_raw(parent) })
266+
Some(unsafe { Device::from_raw(parent) })
267267
}
268268
}
269269

0 commit comments

Comments
 (0)