Skip to contentSkip to conversation

rust: lock: Export Guard::do_unlocked()

PATCH Superseded Lyude Paul 2 participants 2 messages Jul 24, 2025

Subsystems (3)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Lyude Paul

author reviewer
#0 lore raw

PATCH 1/1 rust: lock: Export Guard::do_unlocked()

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.

---
You can find an example usage of this here:

https://gitlab.freedesktop.org/lyudess/linux/-/blob/rvkms-slim/drivers/gpu/drm/rvkms/crtc.rs

 rust/kernel/sync/lock.rs | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
rust/kernel/sync/lock.rs +7 -1
index e82fa5be289c1..60eb98805a489 100644
@@ -228,7 +228,13 @@ pub fn lock_ref(&self) -> &'a Lock<T, B> {
         self.lock
     }
 
-    pub(crate) fn do_unlocked<U>(&mut self, cb: impl FnOnce() -> U) -> U {
+    /// Unlock this [`Guard`]'s lock temporarily, execute `cb`, and then re-lock it.
+    ///
+    /// This can be useful for situations where you may need to do a temporary unlock dance to avoid
+    /// issues like circular locking dependencies.
+    ///
+    /// If the closure returns a value, it will be returned by this function.
+    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

Signature · 1 line
2.50.0
#1 lore raw
Quoted 10 lines · Lyude Paul

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.

Quoted 4 lines

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.

Seems reasonable.

Quoted 13 lines

---
You can find an example usage of this here:

https://gitlab.freedesktop.org/lyudess/linux/-/blob/rvkms-slim/drivers/gpu/drm/rvkms/crtc.rs

 rust/kernel/sync/lock.rs | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
rust/kernel/sync/lock.rs +0 -0
index e82fa5be289c1..60eb98805a489 100644

rust/kernel/sync/lock.rs pub fn lock_ref(&self) -> &'a Lock<T, B> { · line 231

Quoted 5 lines
rust/kernel/sync/lock.rs +1 -1
@@ -228,7 +228,13 @@ pub fn lock_ref(&self) -> &'a Lock<T, B> {
         self.lock
     }
 
-    pub(crate) fn do_unlocked<U>(&mut self, cb: impl FnOnce() -> U) -> U {
+    /// Unlock this [`Guard`]'s lock temporarily, execute `cb`, and then re-lock it.

I would like to change this line as the following to be consistent with other comments in the file.

/// Releases this [Guard]'s lock temporary, executes cb and then re-acquires it.

rust/kernel/sync/lock.rs pub fn lock_ref(&self) -> &'a Lock<T, B> { · line 235

Quoted 4 lines
rust/kernel/sync/lock.rs +4 -0
+    ///
+    /// This can be useful for situations where you may need to do a temporary unlock dance to avoid
+    /// issues like circular locking dependencies.
+    ///

Could you add an "# Examples" section here with a simple example? Thanks!

Otherwise it looks good to me, thanks!

Regards
Boqun

Quoted 10 lines
rust/kernel/sync/lock.rs +2 -0
+    /// If the closure returns a value, it will be returned by this function.
+    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

Signature · 1 line
2.50.0