[PATCH 2/2] rust: sync: require `Sync` for `Backend::GuardState`

From: Benno Lossin
Date: Tue Sep 03 2024 - 05:17:37 EST


`Guard<T, B>` implements `Sync` when `T` is `Sync`. Since this does not
depend on `B`, creating a `Guard` that is `Sync`, but with `!Sync` state
is possible. This is a soundness issue, thus add the bounds to the
respective impls.

Signed-off-by: Benno Lossin <benno.lossin@xxxxxxxxx>
---
rust/kernel/sync/lock.rs | 9 +++++++--
1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/rust/kernel/sync/lock.rs b/rust/kernel/sync/lock.rs
index e73ac9d97b29..336ad2f0ec06 100644
--- a/rust/kernel/sync/lock.rs
+++ b/rust/kernel/sync/lock.rs
@@ -153,8 +153,13 @@ pub struct Guard<'a, T: ?Sized, B: Backend> {
_not_send: PhantomData<*mut ()>,
}

-// SAFETY: `Guard` is sync when the data protected by the lock is also sync.
-unsafe impl<T: Sync + ?Sized, B: Backend> Sync for Guard<'_, T, B> {}
+// SAFETY: `Guard` is sync when the data protected by the lock and the guard state is also sync.
+unsafe impl<T: ?Sized, B: Backend> Sync for Guard<'_, T, B>
+where
+ T: Sync,
+ B::GuardState: Sync,
+{
+}

impl<T: ?Sized, B: Backend> Guard<'_, T, B> {
pub(crate) fn do_unlocked<U>(&mut self, cb: impl FnOnce() -> U) -> U {
--
2.46.0