Skip to contentSkip to conversation

rust: sync: document that Guard is not a stable lock guard

PATCH In review Alice Ryhl 2 participants 5 messages Dec 5, 2024 – Dec 9, 2024

Subsystems (3)
lore
Share & tools
lore

Alice Ryhl

author maintainer
#0 lore raw

PATCH 1/1 rust: sync: document that Guard is not a stable lock guard

Most locks in the linux kernel are stable, which means that holding the lock is sufficient to keep the value from being freed. For example, this means that if you acquire a lock on a refcounted value during rcu, then you do not need to acquire a refcount to keep it alive past rcu_read_unlock().

However, the Rust Guard type is written in a way where it cannot be used with this pattern. One reason for this is the existence of the do_unlocked method that is used with Condvar. The method allows you to unlock the lock, run some code, and then reacquire the lock. This operation is not okay if the lock itself is what keeps the value alive, as it could be freed right after the unlock call.

If we want to support stable locks, we'll need a different guard type that does not have a do_unlocked operation.

---
 rust/kernel/sync/lock.rs | 11 +++++++++++
 1 file changed, 11 insertions(+)
rust/kernel/sync/lock.rs +11 -0
index 41dcddac69e2..7eab46d4060a 100644
@@ -159,6 +159,17 @@ pub fn try_lock(&self) -> Option<Guard<'_, T, B>> {
 /// Allows mutual exclusion primitives that implement the [`Backend`] trait to automatically unlock
 /// when a guard goes out of scope. It also provides a safe and convenient way to access the data
 /// protected by the lock.
+///
+/// This guard may be released and reacquired with [`do_unlocked`]. Note that this implies that
+/// this `Guard` type is _not_ stable, that is, holding this lock is not sufficient to keep the
+/// underlying [`Lock`] alive. That must be done by some other mechanism such as a refcount or
+/// ownership.
+///
+/// # Invariants
+///
+/// This `Guard` owns the lock as defined by the [`Backend`] trait.
+///
+/// [`do_unlocked`]: Guard::do_unlocked
 #[must_use = "the lock unlocks immediately when the guard is unused"]
 pub struct Guard<'a, T: ?Sized, B: Backend> {
     pub(crate) lock: &'a Lock<T, B>,

---
base-commit: 40384c840ea1944d7c5a392e8975ed088ecf0b37
change-id: 20241205-guard-stable-doc-efad6812d0cb

Best regards,

Signature · 1 line
Alice Ryhl <[email protected]>
#1 lore raw
Quoted 6 lines · Alice Ryhl

Most locks in the linux kernel are stable, which means that holding the lock is sufficient to keep the value from being freed. For example, this means that if you acquire a lock on a refcounted value during rcu, then you do not need to acquire a refcount to keep it alive past rcu_read_unlock().

Quoted 7 lines

However, the Rust Guard type is written in a way where it cannot be used with this pattern. One reason for this is the existence of the do_unlocked method that is used with Condvar. The method allows you to unlock the lock, run some code, and then reacquire the lock. This operation is not okay if the lock itself is what keeps the value alive, as it could be freed right after the unlock call.

Hmm... but Guard holds a reference to the corresponding Lock. How could this happen? Do you have an example?

Regards,
Boqun

Quoted 39 lines

If we want to support stable locks, we'll need a different guard type that does not have a do_unlocked operation.

---
 rust/kernel/sync/lock.rs | 11 +++++++++++
 1 file changed, 11 insertions(+)
rust/kernel/sync/lock.rs +11 -1
index 41dcddac69e2..7eab46d4060a 100644
@@ -159,6 +159,17 @@ pub fn try_lock(&self) -> Option<Guard<'_, T, B>> {
 /// Allows mutual exclusion primitives that implement the [`Backend`] trait to automatically unlock
 /// when a guard goes out of scope. It also provides a safe and convenient way to access the data
 /// protected by the lock.
+///
+/// This guard may be released and reacquired with [`do_unlocked`]. Note that this implies that
+/// this `Guard` type is _not_ stable, that is, holding this lock is not sufficient to keep the
+/// underlying [`Lock`] alive. That must be done by some other mechanism such as a refcount or
+/// ownership.
+///
+/// # Invariants
+///
+/// This `Guard` owns the lock as defined by the [`Backend`] trait.
+///
+/// [`do_unlocked`]: Guard::do_unlocked
 #[must_use = "the lock unlocks immediately when the guard is unused"]
 pub struct Guard<'a, T: ?Sized, B: Backend> {
     pub(crate) lock: &'a Lock<T, B>,
 
---

base-commit: 40384c840ea1944d7c5a392e8975ed088ecf0b37
change-id: 20241205-guard-stable-doc-efad6812d0cb

Best regards,

Signature · 1 line
Alice Ryhl <[email protected]>
author maintainer
#2 lore raw
Quoted 16 lines · Boqun Feng
Quoted 6 lines · Alice Ryhl

Most locks in the linux kernel are stable, which means that holding the lock is sufficient to keep the value from being freed. For example, this means that if you acquire a lock on a refcounted value during rcu, then you do not need to acquire a refcount to keep it alive past rcu_read_unlock().

Quoted 7 lines

However, the Rust Guard type is written in a way where it cannot be used with this pattern. One reason for this is the existence of the do_unlocked method that is used with Condvar. The method allows you to unlock the lock, run some code, and then reacquire the lock. This operation is not okay if the lock itself is what keeps the value alive, as it could be freed right after the unlock call.

Quoted 2 lines

Hmm... but Guard holds a reference to the corresponding Lock. How could this happen? Do you have an example?

Well it can't. The reference is yet another reason that Guard can't be used for stable locking.

This doc change arises out of me needing a stable lock for something.

Alice

#3 lore raw
Quoted 17 lines · Alice Ryhl
Quoted 16 lines · Boqun Feng
Quoted 13 lines · Alice Ryhl

Most locks in the linux kernel are stable, which means that holding the lock is sufficient to keep the value from being freed. For example, this means that if you acquire a lock on a refcounted value during rcu, then you do not need to acquire a refcount to keep it alive past rcu_read_unlock().

However, the Rust Guard type is written in a way where it cannot be used with this pattern. One reason for this is the existence of the do_unlocked method that is used with Condvar. The method allows you to unlock the lock, run some code, and then reacquire the lock. This operation is not okay if the lock itself is what keeps the value alive, as it could be freed right after the unlock call.

Quoted 8 lines
Quoted 2 lines

Hmm... but Guard holds a reference to the corresponding Lock. How could this happen? Do you have an example?

Well it can't. The reference is yet another reason that Guard can't be used for stable locking.

This doc change arises out of me needing a stable lock for something.

Maybe it's better to put together this patch and the stable locking you are working on? It's better for reviewing in that way.

I can see what a "stable lock' means, but want to make sure we change the doc to reflect the exact requirement of a stable lock.

Regards,
Boqun

Quoted 1 line

Alice

author maintainer
#4 lore raw
Quoted 22 lines · Boqun Feng
Quoted 20 lines · Alice Ryhl
Quoted 18 lines · Boqun Feng
Quoted 6 lines · Alice Ryhl

Most locks in the linux kernel are stable, which means that holding the lock is sufficient to keep the value from being freed. For example, this means that if you acquire a lock on a refcounted value during rcu, then you do not need to acquire a refcount to keep it alive past rcu_read_unlock().

Quoted 7 lines

However, the Rust Guard type is written in a way where it cannot be used with this pattern. One reason for this is the existence of the do_unlocked method that is used with Condvar. The method allows you to unlock the lock, run some code, and then reacquire the lock. This operation is not okay if the lock itself is what keeps the value alive, as it could be freed right after the unlock call.

Hmm... but Guard holds a reference to the corresponding Lock. How could this happen? Do you have an example?

Quoted 8 lines
Quoted 5 lines

Well it can't. The reference is yet another reason that Guard can't be used for stable locking.

This doc change arises out of me needing a stable lock for something.

Maybe it's better to put together this patch and the stable locking you are working on? It's better for reviewing in that way.

I'm not sure when that will happen.

Quoted 7 lines

I can see what a "stable lock' means, but want to make sure we change the doc to reflect the exact requirement of a stable lock.

Regards,
Boqun

Quoted 1 line

Alice