lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <Zz52kNp4_jRxBv_G@tardis.local>
Date: Wed, 20 Nov 2024 15:53:52 -0800
From: Boqun Feng <boqun.feng@...il.com>
To: Lyude Paul <lyude@...hat.com>
Cc: rust-for-linux@...r.kernel.org, linux-kernel@...r.kernel.org,
	Miguel Ojeda <ojeda@...nel.org>,
	Alex Gaynor <alex.gaynor@...il.com>, Gary Guo <gary@...yguo.net>,
	Björn Roy Baron <bjorn3_gh@...tonmail.com>,
	Benno Lossin <benno.lossin@...ton.me>,
	Andreas Hindborg <a.hindborg@...nel.org>,
	Alice Ryhl <aliceryhl@...gle.com>, Trevor Gross <tmgross@...ch.edu>,
	Tamir Duberstein <tamird@...il.com>,
	Filipe Xavier <felipe_life@...e.com>,
	Martin Rodriguez Reboredo <yakoyoku@...il.com>,
	Valentin Obst <kernel@...entinobst.de>,
	Danilo Krummrich <dakr@...nel.org>,
	Wedson Almeida Filho <walmeida@...rosoft.com>
Subject: Re: [PATCH 1/3] rust: sync: Add Lock::is_locked()

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@...hat.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
> 

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ