[PATCH 2/3] rust: lock: guard: add T: Unpin bound to DerefMut

Daniel Almeida posted 3 patches 2 months, 1 week ago
There is a newer version of this series
[PATCH 2/3] rust: lock: guard: add T: Unpin bound to DerefMut
Posted by Daniel Almeida 2 months, 1 week ago
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.

Link: https://github.com/Rust-for-Linux/linux/issues/1181
Suggested-by: Benno Lossin <lossin@kernel.org>
Suggested-by: Boqun Feng <boqun.feng@gmail.com>
Signed-off-by: Daniel Almeida <daniel.almeida@collabora.com>
---
 rust/kernel/sync/lock.rs        | 5 ++++-
 rust/kernel/sync/lock/global.rs | 5 ++++-
 2 files changed, 8 insertions(+), 2 deletions(-)

diff --git a/rust/kernel/sync/lock.rs b/rust/kernel/sync/lock.rs
index 9715463cbab867a2cf59e75f03001d75e91bd7b6..087bc0391f92a73b9af18ca31461b513bb5a9bcd 100644
--- a/rust/kernel/sync/lock.rs
+++ b/rust/kernel/sync/lock.rs
@@ -254,7 +254,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() }
diff --git a/rust/kernel/sync/lock/global.rs b/rust/kernel/sync/lock/global.rs
index d65f94b5caf2668586088417323496629492932f..38b44803279986275616eef499fd40b8d4e97fdf 100644
--- a/rust/kernel/sync/lock/global.rs
+++ b/rust/kernel/sync/lock/global.rs
@@ -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
     }

-- 
2.50.1
Re: [PATCH 2/3] rust: lock: guard: add T: Unpin bound to DerefMut
Posted by Benno Lossin 2 months ago
On Wed Jul 30, 2025 at 7:14 PM CEST, Daniel Almeida wrote:
> 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.

This patch should probably be the first one in this series, as only
applying patch 1, but not this one is unsound...

> Link: https://github.com/Rust-for-Linux/linux/issues/1181
> Suggested-by: Benno Lossin <lossin@kernel.org>
> Suggested-by: Boqun Feng <boqun.feng@gmail.com>
> Signed-off-by: Daniel Almeida <daniel.almeida@collabora.com>

With this moved:

Reviewed-by: Benno Lossin <lossin@kernel.org>

---
Cheers,
Benno

> ---
>  rust/kernel/sync/lock.rs        | 5 ++++-
>  rust/kernel/sync/lock/global.rs | 5 ++++-
>  2 files changed, 8 insertions(+), 2 deletions(-)