diff --git a/rust/kernel/drm/kms/connector.rs b/rust/kernel/drm/kms/connector.rs index 4ca973d03d171f2c372ccc1c1a3057c1f6a74165..3682a39910034ab513d4a9f3526489c0213cf960 100644 --- a/rust/kernel/drm/kms/connector.rs +++ b/rust/kernel/drm/kms/connector.rs @@ -537,22 +537,24 @@ pub(super) use private::AsRawConnectorState as AsRawConnectorStatePrivate; /// [`struct drm_connector_state`]: srctree/include/drm/drm_connector.h pub trait FromRawConnectorState: AsRawConnectorState { /// Get an immutable reference to this type from the given raw [`struct drm_connector_state`] - /// pointer + /// pointer. /// /// # Safety /// - /// The caller guarantees `ptr` is contained within a valid instance of `Self` + /// - The caller guarantees `ptr` is contained within a valid instance of `Self`. + /// - The caller guarantees that `ptr` cannot not be modified for the lifetime of `'a`. /// /// [`struct drm_connector_state`]: srctree/include/drm/drm_connector.h unsafe fn from_raw<'a>(ptr: *const bindings::drm_connector_state) -> &'a Self; /// Get a mutable reference to this type from the given raw [`struct drm_connector_state`] - /// pointer + /// pointer. /// /// # Safety /// - /// The caller guarantees `ptr` is contained within a valid instance of `Self`, and that no - /// other references (mutable or immutable) to `ptr` exist. + /// - The caller guarantees that `ptr` is contained within a valid instance of `Self`. + /// - The caller guarantees that `ptr` cannot have any other references taken out for the + /// lifetime of `'a`. /// /// [`struct drm_connector_state`]: srctree/include/drm/drm_connector.h unsafe fn from_raw_mut<'a>(ptr: *mut bindings::drm_connector_state) -> &'a mut Self; @@ -633,13 +635,25 @@ impl<T: DriverConnectorState> private::AsRawConnectorState for ConnectorState<T> impl<T: DriverConnectorState> FromRawConnectorState for ConnectorState<T> { unsafe fn from_raw<'a>(ptr: *const bindings::drm_connector_state) -> &'a Self { - // SAFETY: Our data layout starts with `bindings::drm_connector_state` - unsafe { &*ptr.cast() } + // Our data layout starts with `bindings::drm_connector_state`. + let ptr: *const Self = ptr.cast(); + + // SAFETY: + // - Our safety contract requires that `ptr` be contained within `Self`. + // - Our safety contract requires the caller ensure that it is safe for us to take an + // immutable reference. + unsafe { &*ptr } } unsafe fn from_raw_mut<'a>(ptr: *mut bindings::drm_connector_state) -> &'a mut Self { - // SAFETY: Our data layout starts with `bindings::drm_connector_state` - unsafe { &mut *ptr.cast() } + // Our data layout starts with `bindings::drm_connector_state`. + let ptr: *mut Self = ptr.cast(); + + // SAFETY: + // - Our safety contract requires that `ptr` be contained within `Self`. + // - Our safety contract requires the caller ensure it is safe for us to take a mutable + // reference. + unsafe { &mut *ptr } } }