Commit da123f0e authored by Daniel Almeida's avatar Daniel Almeida Committed by Peter Zijlstra
Browse files

rust: lock: guard: Add T: Unpin bound to DerefMut



A core property of pinned types is not handing a mutable reference to
the inner data in safe code, as this trivially allows that data to be
moved.

Enforce this condition by adding a bound on lock::Guard's DerefMut
implementation, so that it's only implemented for pinning-agnostic
types.

Suggested-by: default avatarBenno Lossin <lossin@kernel.org>
Suggested-by: default avatarBoqun Feng <boqun.feng@gmail.com>
Signed-off-by: default avatarDaniel Almeida <daniel.almeida@collabora.com>
Signed-off-by: default avatarBoqun Feng <boqun.feng@gmail.com>
Signed-off-by: default avatarPeter Zijlstra (Intel) <peterz@infradead.org>
Reviewed-by: default avatarBenno Lossin <lossin@kernel.org>
Reviewed-by: default avatarAlice Ryhl <aliceryhl@google.com>
Link: https://github.com/Rust-for-Linux/linux/issues/1181
parent c14ecb55
Loading
Loading
Loading
Loading
+4 −1
Original line number Diff line number Diff line
@@ -251,7 +251,10 @@ fn deref(&self) -> &Self::Target {
    }
}

impl<T: ?Sized, B: Backend> core::ops::DerefMut for Guard<'_, T, B> {
impl<T: ?Sized, B: Backend> core::ops::DerefMut for Guard<'_, T, B>
where
    T: Unpin,
{
    fn deref_mut(&mut self) -> &mut Self::Target {
        // SAFETY: The caller owns the lock, so it is safe to deref the protected data.
        unsafe { &mut *self.lock.data.get() }
+4 −1
Original line number Diff line number Diff line
@@ -106,7 +106,10 @@ fn deref(&self) -> &Self::Target {
    }
}

impl<B: GlobalLockBackend> core::ops::DerefMut for GlobalGuard<B> {
impl<B: GlobalLockBackend> core::ops::DerefMut for GlobalGuard<B>
where
    B::Item: Unpin,
{
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.inner
    }