Skip to content

Commit da123f0

Browse files
danielalmeida-collaboraPeter Zijlstra
authored andcommitted
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: Benno Lossin <lossin@kernel.org> Suggested-by: Boqun Feng <boqun.feng@gmail.com> Signed-off-by: Daniel Almeida <daniel.almeida@collabora.com> Signed-off-by: Boqun Feng <boqun.feng@gmail.com> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Reviewed-by: Benno Lossin <lossin@kernel.org> Reviewed-by: Alice Ryhl <aliceryhl@google.com> Link: #1181
1 parent c14ecb5 commit da123f0

2 files changed

Lines changed: 8 additions & 2 deletions

File tree

rust/kernel/sync/lock.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,10 @@ impl<T: ?Sized, B: Backend> core::ops::Deref for Guard<'_, T, B> {
251251
}
252252
}
253253

254-
impl<T: ?Sized, B: Backend> core::ops::DerefMut for Guard<'_, T, B> {
254+
impl<T: ?Sized, B: Backend> core::ops::DerefMut for Guard<'_, T, B>
255+
where
256+
T: Unpin,
257+
{
255258
fn deref_mut(&mut self) -> &mut Self::Target {
256259
// SAFETY: The caller owns the lock, so it is safe to deref the protected data.
257260
unsafe { &mut *self.lock.data.get() }

rust/kernel/sync/lock/global.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,10 @@ impl<B: GlobalLockBackend> core::ops::Deref for GlobalGuard<B> {
106106
}
107107
}
108108

109-
impl<B: GlobalLockBackend> core::ops::DerefMut for GlobalGuard<B> {
109+
impl<B: GlobalLockBackend> core::ops::DerefMut for GlobalGuard<B>
110+
where
111+
B::Item: Unpin,
112+
{
110113
fn deref_mut(&mut self) -> &mut Self::Target {
111114
&mut self.inner
112115
}

0 commit comments

Comments
 (0)