[PATCH 1/3] rust: sync: Add Lock::is_locked()

Lyude Paul posted 3 patches 12 hours ago
[PATCH 1/3] rust: sync: Add Lock::is_locked()
Posted by Lyude Paul 12 hours ago
Now that we've added a Lock::from_raw() function and exposed Guard::new(),
it would be good to actually add the ability to assert the current state
of a lock to ensure correctness for unsafe code using these functions.

To do so, let's add Lock::is_locked() which simply returns whether or not a
Lock is acquired. We'll use this in the next few commits to add some debug
assertions.

Signed-off-by: Lyude Paul <lyude@redhat.com>
---
 rust/helpers/spinlock.c           |  5 +++++
 rust/kernel/sync/lock.rs          | 18 ++++++++++++++++++
 rust/kernel/sync/lock/mutex.rs    |  5 +++++
 rust/kernel/sync/lock/spinlock.rs |  5 +++++
 4 files changed, 33 insertions(+)

diff --git a/rust/helpers/spinlock.c b/rust/helpers/spinlock.c
index b7b0945e8b3cb..90216a69e3ea1 100644
--- a/rust/helpers/spinlock.c
+++ b/rust/helpers/spinlock.c
@@ -26,3 +26,8 @@ int rust_helper_spin_trylock(spinlock_t *lock)
 {
 	return spin_trylock(lock);
 }
+
+bool rust_helper_spin_is_locked(spinlock_t *lock)
+{
+	return spin_is_locked(lock);
+}
diff --git a/rust/kernel/sync/lock.rs b/rust/kernel/sync/lock.rs
index 139f17f2ec86b..542f846ac02b2 100644
--- a/rust/kernel/sync/lock.rs
+++ b/rust/kernel/sync/lock.rs
@@ -85,6 +85,13 @@ unsafe fn relock(ptr: *mut Self::State, guard_state: &mut Self::GuardState) {
         // SAFETY: The safety requirements ensure that the lock is initialised.
         *guard_state = unsafe { Self::lock(ptr) };
     }
+
+    /// Returns whether or not the lock is currently acquired.
+    ///
+    /// # Safety
+    ///
+    /// Callers must ensure that `ptr` is a valid initialised pointer to this lock type.
+    unsafe fn is_locked(ptr: *mut Self::State) -> bool;
 }
 
 /// A mutual exclusion primitive.
@@ -170,6 +177,17 @@ pub fn try_lock(&self) -> Option<Guard<'_, T, B>> {
         // that `init` was called.
         unsafe { B::try_lock(self.state.get()).map(|state| Guard::new(self, state)) }
     }
+
+    /// Return whether or not the lock is currently acquired.
+    ///
+    /// Keep in mind that this function is inherently racy: a lock could immediately be acquired or
+    /// released after this function returns. As such, the return value from this function should be
+    /// treated as a snapshot for debugging purposes.
+    pub fn is_locked(&self) -> bool {
+        // SAFETY: The constructor of the type calls `init`, so the existence of the object proves
+        // that `init` was called.
+        unsafe { B::is_locked(self.state.get()) }
+    }
 }
 
 /// A lock guard.
diff --git a/rust/kernel/sync/lock/mutex.rs b/rust/kernel/sync/lock/mutex.rs
index 0e946ebefce12..f21b1f14cbe1b 100644
--- a/rust/kernel/sync/lock/mutex.rs
+++ b/rust/kernel/sync/lock/mutex.rs
@@ -126,4 +126,9 @@ unsafe fn try_lock(ptr: *mut Self::State) -> Option<Self::GuardState> {
             None
         }
     }
+
+    unsafe fn is_locked(ptr: *mut Self::State) -> bool {
+        // SAFETY: The `ptr` pointer is guaranteed to be valid and initialized before use.
+        unsafe { bindings::mutex_is_locked(ptr) }
+    }
 }
diff --git a/rust/kernel/sync/lock/spinlock.rs b/rust/kernel/sync/lock/spinlock.rs
index 9f4d128bed983..cfccf5e900b80 100644
--- a/rust/kernel/sync/lock/spinlock.rs
+++ b/rust/kernel/sync/lock/spinlock.rs
@@ -125,4 +125,9 @@ unsafe fn try_lock(ptr: *mut Self::State) -> Option<Self::GuardState> {
             None
         }
     }
+
+    unsafe fn is_locked(ptr: *mut Self::State) -> bool {
+        // SAFETY: The `ptr` pointer is guaranteed to be valid and initialized before use.
+        unsafe { bindings::spin_is_locked(ptr) }
+    }
 }
-- 
2.47.0
Re: [PATCH 1/3] rust: sync: Add Lock::is_locked()
Posted by Boqun Feng 11 hours ago
On Wed, Nov 20, 2024 at 05:30:41PM -0500, Lyude Paul wrote:
> Now that we've added a Lock::from_raw() function and exposed Guard::new(),
> it would be good to actually add the ability to assert the current state
> of a lock to ensure correctness for unsafe code using these functions.
> 
> To do so, let's add Lock::is_locked() which simply returns whether or not a
> Lock is acquired. We'll use this in the next few commits to add some debug
> assertions.
> 
> Signed-off-by: Lyude Paul <lyude@redhat.com>
> ---
>  rust/helpers/spinlock.c           |  5 +++++
>  rust/kernel/sync/lock.rs          | 18 ++++++++++++++++++
>  rust/kernel/sync/lock/mutex.rs    |  5 +++++
>  rust/kernel/sync/lock/spinlock.rs |  5 +++++
>  4 files changed, 33 insertions(+)
> 
> diff --git a/rust/helpers/spinlock.c b/rust/helpers/spinlock.c
> index b7b0945e8b3cb..90216a69e3ea1 100644
> --- a/rust/helpers/spinlock.c
> +++ b/rust/helpers/spinlock.c
> @@ -26,3 +26,8 @@ int rust_helper_spin_trylock(spinlock_t *lock)
>  {
>  	return spin_trylock(lock);
>  }
> +
> +bool rust_helper_spin_is_locked(spinlock_t *lock)
> +{
> +	return spin_is_locked(lock);
> +}
> diff --git a/rust/kernel/sync/lock.rs b/rust/kernel/sync/lock.rs
> index 139f17f2ec86b..542f846ac02b2 100644
> --- a/rust/kernel/sync/lock.rs
> +++ b/rust/kernel/sync/lock.rs
> @@ -85,6 +85,13 @@ unsafe fn relock(ptr: *mut Self::State, guard_state: &mut Self::GuardState) {
>          // SAFETY: The safety requirements ensure that the lock is initialised.
>          *guard_state = unsafe { Self::lock(ptr) };
>      }
> +
> +    /// Returns whether or not the lock is currently acquired.
> +    ///
> +    /// # Safety
> +    ///
> +    /// Callers must ensure that `ptr` is a valid initialised pointer to this lock type.
> +    unsafe fn is_locked(ptr: *mut Self::State) -> bool;
>  }
>  
>  /// A mutual exclusion primitive.
> @@ -170,6 +177,17 @@ pub fn try_lock(&self) -> Option<Guard<'_, T, B>> {
>          // that `init` was called.
>          unsafe { B::try_lock(self.state.get()).map(|state| Guard::new(self, state)) }
>      }
> +
> +    /// Return whether or not the lock is currently acquired.
> +    ///
> +    /// Keep in mind that this function is inherently racy: a lock could immediately be acquired or
> +    /// released after this function returns. As such, the return value from this function should be
> +    /// treated as a snapshot for debugging purposes.

Then why don't we use the function provided by lockdep? I.e.
lockdep_is_held() and its friends?

Regards,
Boqun

> +    pub fn is_locked(&self) -> bool {

> +        // SAFETY: The constructor of the type calls `init`, so the existence of the object proves
> +        // that `init` was called.
> +        unsafe { B::is_locked(self.state.get()) }
> +    }
>  }
>  
>  /// A lock guard.
> diff --git a/rust/kernel/sync/lock/mutex.rs b/rust/kernel/sync/lock/mutex.rs
> index 0e946ebefce12..f21b1f14cbe1b 100644
> --- a/rust/kernel/sync/lock/mutex.rs
> +++ b/rust/kernel/sync/lock/mutex.rs
> @@ -126,4 +126,9 @@ unsafe fn try_lock(ptr: *mut Self::State) -> Option<Self::GuardState> {
>              None
>          }
>      }
> +
> +    unsafe fn is_locked(ptr: *mut Self::State) -> bool {
> +        // SAFETY: The `ptr` pointer is guaranteed to be valid and initialized before use.
> +        unsafe { bindings::mutex_is_locked(ptr) }
> +    }
>  }
> diff --git a/rust/kernel/sync/lock/spinlock.rs b/rust/kernel/sync/lock/spinlock.rs
> index 9f4d128bed983..cfccf5e900b80 100644
> --- a/rust/kernel/sync/lock/spinlock.rs
> +++ b/rust/kernel/sync/lock/spinlock.rs
> @@ -125,4 +125,9 @@ unsafe fn try_lock(ptr: *mut Self::State) -> Option<Self::GuardState> {
>              None
>          }
>      }
> +
> +    unsafe fn is_locked(ptr: *mut Self::State) -> bool {
> +        // SAFETY: The `ptr` pointer is guaranteed to be valid and initialized before use.
> +        unsafe { bindings::spin_is_locked(ptr) }
> +    }
>  }
> -- 
> 2.47.0
>