From: Lyude Paul <lyude@redhat.com>
To: rust-for-linux@vger.kernel.org,
Thomas Gleixner <tglx@linutronix.de>,
Boqun Feng <boqun.feng@gmail.com>,
linux-kernel@vger.kernel.org
Cc: "Peter Zijlstra" <peterz@infradead.org>,
"Ingo Molnar" <mingo@redhat.com>, "Will Deacon" <will@kernel.org>,
"Waiman Long" <longman@redhat.com>,
"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" <lossin@kernel.org>,
"Andreas Hindborg" <a.hindborg@kernel.org>,
"Alice Ryhl" <aliceryhl@google.com>,
"Trevor Gross" <tmgross@umich.edu>,
"Danilo Krummrich" <dakr@kernel.org>
Subject: [PATCH v3] rust: lock: Export Guard::do_unlocked()
Date: Thu, 24 Jul 2025 17:15:56 -0400 [thread overview]
Message-ID: <20250724211634.586808-1-lyude@redhat.com> (raw)
In RVKMS, I discovered a silly issue where as a result of our HrTimer for
vblank emulation and our vblank enable/disable callbacks sharing a
spinlock, it was possible to deadlock while trying to disable the vblank
timer.
The solution for this ended up being simple: keep track of when the HrTimer
could potentially acquire the shared spinlock, and simply drop the spinlock
temporarily from our vblank enable/disable callbacks when stopping the
timer. And do_unlocked() ended up being perfect for this.
Since this seems like it's useful, let's export this for use by the rest of
the world and write short documentation for it.
Signed-off-by: Lyude Paul <lyude@redhat.com>
---
V2:
* Fix documentation for do_unlocked
* Add an example
V3:
* Documentation changes from Miguel
rust/kernel/sync/lock.rs | 35 ++++++++++++++++++++++++++++++++++-
1 file changed, 34 insertions(+), 1 deletion(-)
diff --git a/rust/kernel/sync/lock.rs b/rust/kernel/sync/lock.rs
index e82fa5be289c1..800cdd16dce6e 100644
--- a/rust/kernel/sync/lock.rs
+++ b/rust/kernel/sync/lock.rs
@@ -228,7 +228,40 @@ pub fn lock_ref(&self) -> &'a Lock<T, B> {
self.lock
}
- pub(crate) fn do_unlocked<U>(&mut self, cb: impl FnOnce() -> U) -> U {
+ /// Releases this [`Guard`]'s lock temporarily, executes `cb` and then re-acquires it.
+ ///
+ /// This can be useful for situations where you may need to do a temporary unlock dance to avoid
+ /// issues like circular locking dependencies.
+ ///
+ /// It returns the value returned by the closure.
+ ///
+ /// # Examples
+ ///
+ /// The following example shows how to use [`Guard::do_unlocked`] to temporarily release a lock,
+ /// do some work, then re-lock it.
+ ///
+ /// ```
+ /// # use kernel::{new_spinlock, sync::lock::{Backend, Guard, Lock}};
+ /// # use pin_init::stack_pin_init;
+ /// fn assert_held<T, B: Backend>(guard: &Guard<'_, T, B>, lock: &Lock<T, B>) {
+ /// // Address-equal means the same lock.
+ /// assert!(core::ptr::eq(guard.lock_ref(), lock));
+ /// }
+ ///
+ /// stack_pin_init! {
+ /// let l = new_spinlock!(42)
+ /// }
+ ///
+ /// let mut g = l.lock();
+ /// let val = *g;
+ ///
+ /// // The lock will be released, but only temporarily
+ /// g.do_unlocked(|| assert_eq!(val, 42));
+ ///
+ /// // `g` originates from `l` and should be relocked now.
+ /// assert_held(&g, &l);
+ /// ```
+ pub fn do_unlocked<U>(&mut self, cb: impl FnOnce() -> U) -> U {
// SAFETY: The caller owns the lock, so it is safe to unlock it.
unsafe { B::unlock(self.lock.state.get(), &self.state) };
base-commit: dff64b072708ffef23c117fa1ee1ea59eb417807
--
2.50.0
next reply other threads:[~2025-07-24 21:16 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-07-24 21:15 Lyude Paul [this message]
2025-07-25 10:51 ` [PATCH v3] rust: lock: Export Guard::do_unlocked() Benno Lossin
2025-08-06 19:57 ` 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=20250724211634.586808-1-lyude@redhat.com \
--to=lyude@redhat.com \
--cc=a.hindborg@kernel.org \
--cc=alex.gaynor@gmail.com \
--cc=aliceryhl@google.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun.feng@gmail.com \
--cc=dakr@kernel.org \
--cc=gary@garyguo.net \
--cc=linux-kernel@vger.kernel.org \
--cc=longman@redhat.com \
--cc=lossin@kernel.org \
--cc=mingo@redhat.com \
--cc=ojeda@kernel.org \
--cc=peterz@infradead.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=tglx@linutronix.de \
--cc=tmgross@umich.edu \
--cc=will@kernel.org \
/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;
as well as URLs for NNTP newsgroup(s).