From: Lyude Paul <lyude@redhat.com>
To: rust-for-linux@vger.kernel.org
Cc: linux-kernel@vger.kernel.org, "Miguel Ojeda" <ojeda@kernel.org>,
"Alex Gaynor" <alex.gaynor@gmail.com>,
"Boqun Feng" <boqun.feng@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: [PATCH 3/3] rust: sync: Add Guard::new_unchecked()
Date: Wed, 20 Nov 2024 17:30:43 -0500 [thread overview]
Message-ID: <20241120223442.2491136-4-lyude@redhat.com> (raw)
In-Reply-To: <20241120223442.2491136-1-lyude@redhat.com>
Asserting is_locked() for unsafe Guard::new() calls is quite useful for
verifying safety for callers outside the lock module. But in the lock
module, it's a bit unnecessary and a potential performance hit for safe
rust code. Mainly because it implies all safe lock acquisitions in rust
will have to run this debug assertion.
So, let's split out Guard::new() by adding a Guard::new_unchecked()
function that skips this debug assertion. Of course, we leave this function
as private and note that it is only ever intended for use in this specific
module.
Signed-off-by: Lyude Paul <lyude@redhat.com>
---
rust/kernel/sync/lock.rs | 17 +++++++++++++++--
1 file changed, 15 insertions(+), 2 deletions(-)
diff --git a/rust/kernel/sync/lock.rs b/rust/kernel/sync/lock.rs
index 0a7f2ed767423..2fd4b665ffc9a 100644
--- a/rust/kernel/sync/lock.rs
+++ b/rust/kernel/sync/lock.rs
@@ -166,7 +166,7 @@ pub fn lock(&self) -> Guard<'_, T, B> {
// that `init` was called.
let state = unsafe { B::lock(self.state.get()) };
// SAFETY: The lock was just acquired.
- unsafe { Guard::new(self, state) }
+ unsafe { Guard::new_unchecked(self, state) }
}
/// Tries to acquire the lock.
@@ -175,7 +175,7 @@ pub fn lock(&self) -> Guard<'_, T, B> {
pub fn try_lock(&self) -> Option<Guard<'_, T, B>> {
// SAFETY: The constructor of the type calls `init`, so the existence of the object proves
// that `init` was called.
- unsafe { B::try_lock(self.state.get()).map(|state| Guard::new(self, state)) }
+ unsafe { B::try_lock(self.state.get()).map(|state| Guard::new_unchecked(self, state)) }
}
/// Return whether or not the lock is currently acquired.
@@ -255,6 +255,19 @@ impl<'a, T: ?Sized, B: Backend> Guard<'a, T, B> {
pub unsafe fn new(lock: &'a Lock<T, B>, state: B::GuardState) -> Self {
debug_assert!(lock.is_locked());
+ // SAFETY: Our safety requirements fulfill the requirements of this function.
+ unsafe { Self::new_unchecked(lock, state) }
+ }
+
+ /// Constructs a new immutable lock guard without assertions.
+ ///
+ /// Unlike [`Guard::new`], this function does not run a debug assertion to ensure the lock has
+ /// been acquired. It should only be used in this module.
+ ///
+ /// # Safety
+ ///
+ /// The caller must ensure that it owns the lock.
+ unsafe fn new_unchecked(lock: &'a Lock<T, B>, state: B::GuardState) -> Self {
Self {
lock,
state,
--
2.47.0
prev parent reply other threads:[~2024-11-20 22:35 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
2024-11-22 20:30 ` Lyude Paul
2024-11-25 21:30 ` Boqun Feng
2024-11-20 22:30 ` Lyude Paul [this message]
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=20241120223442.2491136-4-lyude@redhat.com \
--to=lyude@redhat.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=boqun.feng@gmail.com \
--cc=felipe_life@live.com \
--cc=gary@garyguo.net \
--cc=kernel@valentinobst.de \
--cc=linux-kernel@vger.kernel.org \
--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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox