All of lore.kernel.org
 help / color / mirror / Atom feed
From: Boqun Feng <boqun.feng@gmail.com>
To: Lyude Paul <lyude@redhat.com>
Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org,
	"Miguel Ojeda" <ojeda@kernel.org>,
	"Alex Gaynor" <alex.gaynor@gmail.com>,
	"Gary Guo" <gary@garyguo.net>,
	"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
	"Benno Lossin" <benno.lossin@proton.me>,
	"Andreas Hindborg" <a.hindborg@kernel.org>,
	"Alice Ryhl" <aliceryhl@google.com>,
	"Trevor Gross" <tmgross@umich.edu>,
	"Martin Rodriguez Reboredo" <yakoyoku@gmail.com>,
	"Valentin Obst" <kernel@valentinobst.de>,
	"Filipe Xavier" <felipe_life@live.com>
Subject: Re: [PATCH 2/3] rust: sync: Assert Lock::is_locked in Guard::new for debug builds
Date: Wed, 20 Nov 2024 15:59:47 -0800	[thread overview]
Message-ID: <Zz5384uaZdsL306c@tardis.local> (raw)
In-Reply-To: <20241120223442.2491136-3-lyude@redhat.com>

On Wed, Nov 20, 2024 at 05:30:42PM -0500, Lyude Paul wrote:
> Since we're allowing code to unsafely claim that it's acquired a lock
> let's use the new Lock::is_locked() function so that when debug assertions
> are enabled, we can verify that the lock has actually been acquired.
> 
> Signed-off-by: Lyude Paul <lyude@redhat.com>
> ---
>  rust/kernel/sync/lock.rs | 7 +++++++
>  1 file changed, 7 insertions(+)
> 
> diff --git a/rust/kernel/sync/lock.rs b/rust/kernel/sync/lock.rs
> index 542f846ac02b2..0a7f2ed767423 100644
> --- a/rust/kernel/sync/lock.rs
> +++ b/rust/kernel/sync/lock.rs
> @@ -244,10 +244,17 @@ fn drop(&mut self) {
>  impl<'a, T: ?Sized, B: Backend> Guard<'a, T, B> {
>      /// Constructs a new immutable lock guard.
>      ///
> +    /// # Panics
> +    ///
> +    /// This function will panic if debug assertions are enabled and `lock` is not actually
> +    /// acquired.
> +    ///
>      /// # Safety
>      ///
>      /// The caller must ensure that it owns the lock.
>      pub unsafe fn new(lock: &'a Lock<T, B>, state: B::GuardState) -> Self {
> +        debug_assert!(lock.is_locked());

You should just use lockdep_assert_held() here, and there's no need for
new_unchecked().

Regards,
Boqun

> +
>          Self {
>              lock,
>              state,
> -- 
> 2.47.0
> 

  reply	other threads:[~2024-11-20 23:59 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-11-20 22:30 [PATCH 0/3] rust: sync: Add Lock::is_locked() Lyude Paul
2024-11-20 22:30 ` [PATCH 1/3] " Lyude Paul
2024-11-20 23:53   ` Boqun Feng
2024-11-20 22:30 ` [PATCH 2/3] rust: sync: Assert Lock::is_locked in Guard::new for debug builds Lyude Paul
2024-11-20 23:59   ` Boqun Feng [this message]
2024-11-22 20:30     ` Lyude Paul
2024-11-25 21:30       ` Boqun Feng
2024-11-20 22:30 ` [PATCH 3/3] rust: sync: Add Guard::new_unchecked() Lyude Paul

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Zz5384uaZdsL306c@tardis.local \
    --to=boqun.feng@gmail.com \
    --cc=a.hindborg@kernel.org \
    --cc=alex.gaynor@gmail.com \
    --cc=aliceryhl@google.com \
    --cc=benno.lossin@proton.me \
    --cc=bjorn3_gh@protonmail.com \
    --cc=felipe_life@live.com \
    --cc=gary@garyguo.net \
    --cc=kernel@valentinobst.de \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lyude@redhat.com \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=tmgross@umich.edu \
    --cc=yakoyoku@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.