diff --git a/rust/kernel/sync/guard.rs b/rust/kernel/sync/guard.rs index 79f6bd708e1dea..3af735d363571d 100644 --- a/rust/kernel/sync/guard.rs +++ b/rust/kernel/sync/guard.rs @@ -64,7 +64,12 @@ impl<'a, L: Lock + ?Sized> Guard<'a, L> { /// /// [`Guard`] is written such that any mutual exclusion primitive that can implement this trait can /// also benefit from having an automatic way to unlock itself. -pub trait Lock { +/// +/// # Safety +/// +/// Implementers of this trait must ensure that only one thread/CPU may access the protected data +/// once the lock is held, that is, between calls to `lock_noguard` and `unlock`. +pub unsafe trait Lock { /// The type of the data protected by the lock. type Inner: ?Sized; diff --git a/rust/kernel/sync/mutex.rs b/rust/kernel/sync/mutex.rs index 4ceca07b201e89..7053a5136abb94 100644 --- a/rust/kernel/sync/mutex.rs +++ b/rust/kernel/sync/mutex.rs @@ -77,7 +77,8 @@ impl NeedsLockClass for Mutex { } } -impl Lock for Mutex { +// SAFETY: The underlying kernel `struct mutex` object ensures mutual exclusion. +unsafe impl Lock for Mutex { type Inner = T; type GuardContext = (); diff --git a/rust/kernel/sync/spinlock.rs b/rust/kernel/sync/spinlock.rs index 11b918dab3a04c..6fa00a514aac95 100644 --- a/rust/kernel/sync/spinlock.rs +++ b/rust/kernel/sync/spinlock.rs @@ -80,7 +80,8 @@ impl NeedsLockClass for SpinLock { } } -impl Lock for SpinLock { +// SAFETY: The underlying kernel `spinlock_t` object ensures mutual exclusion. +unsafe impl Lock for SpinLock { type Inner = T; type GuardContext = ();