rust: device: fix device context of Device::parent()
commit cfec502b3d091ff7c24df6ccf8079470584315a0 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: stable@vger.kernel.org
Fixes: a4c9f71e34 ("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>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
This commit is contained in:
committed by
Greg Kroah-Hartman
parent
d3c352207f
commit
2b55b5be1d
@@ -217,13 +217,7 @@ impl<Ctx: device::DeviceContext> Device<Ctx> {
|
|||||||
|
|
||||||
/// Returns a reference to the parent [`device::Device`], if any.
|
/// Returns a reference to the parent [`device::Device`], if any.
|
||||||
pub fn parent(&self) -> Option<&device::Device> {
|
pub fn parent(&self) -> Option<&device::Device> {
|
||||||
let ptr: *const Self = self;
|
self.as_ref().parent()
|
||||||
// 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()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -250,7 +250,7 @@ impl<Ctx: DeviceContext> Device<Ctx> {
|
|||||||
|
|
||||||
/// Returns a reference to the parent device, if any.
|
/// Returns a reference to the parent device, if any.
|
||||||
#[cfg_attr(not(CONFIG_AUXILIARY_BUS), expect(dead_code))]
|
#[cfg_attr(not(CONFIG_AUXILIARY_BUS), expect(dead_code))]
|
||||||
pub(crate) fn parent(&self) -> Option<&Self> {
|
pub(crate) fn parent(&self) -> Option<&Device> {
|
||||||
// SAFETY:
|
// SAFETY:
|
||||||
// - By the type invariant `self.as_raw()` is always valid.
|
// - By the type invariant `self.as_raw()` is always valid.
|
||||||
// - The parent device is only ever set at device creation.
|
// - The parent device is only ever set at device creation.
|
||||||
@@ -263,7 +263,7 @@ impl<Ctx: DeviceContext> Device<Ctx> {
|
|||||||
// - Since `parent` is not NULL, it must be a valid pointer to a `struct device`.
|
// - 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
|
// - `parent` is valid for the lifetime of `self`, since a `struct device` holds a
|
||||||
// reference count of its parent.
|
// reference count of its parent.
|
||||||
Some(unsafe { Self::from_raw(parent) })
|
Some(unsafe { Device::from_raw(parent) })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user