* [PATCH v2] rust: sync: add #[must_use] to GlobalGuard and GlobalLock::try_lock
@ 2026-05-02 16:00 Ashutosh Desai
2026-05-05 12:38 ` Gary Guo
` (2 more replies)
0 siblings, 3 replies; 5+ messages in thread
From: Ashutosh Desai @ 2026-05-02 16:00 UTC (permalink / raw)
To: ojeda
Cc: peterz, mingo, will, boqun, wl, gary, bjorn3_gh, lossin,
a.hindborg, aliceryhl, tmgross, dakr, linux-kernel,
rust-for-linux, Ashutosh Desai
Guard is marked #[must_use] since dropping it releases the lock. GlobalGuard
wraps Guard with identical semantics but was missing the annotation, so
discarding it would silently compile without warning.
Similarly, GlobalLock::try_lock was missing #[must_use]. Option<T> does not
propagate #[must_use] from T, so the attribute needs to be on the function
directly - same reason Lock::try_lock has it.
Reviewed-by: Alice Ryhl <aliceryhl@google.com>
Signed-off-by: Ashutosh Desai <ashutoshdesai993@gmail.com>
---
Changes in v2:
- Remove explanatory comment above #[must_use] on try_lock (Alice Ryhl)
Link to v1: https://lore.kernel.org/rust-for-linux/20260419001141.211935-1-ashutoshdesai993@gmail.com/
rust/kernel/sync/lock/global.rs | 2 ++
1 file changed, 2 insertions(+)
diff --git a/rust/kernel/sync/lock/global.rs b/rust/kernel/sync/lock/global.rs
index aecbdc34738f..ec2dd84316fc 100644
--- a/rust/kernel/sync/lock/global.rs
+++ b/rust/kernel/sync/lock/global.rs
@@ -85,6 +85,7 @@ pub fn lock(&'static self) -> GlobalGuard<B> {
}
/// Try to lock this global lock.
+ #[must_use = "if unused, the lock will be immediately unlocked"]
#[inline]
pub fn try_lock(&'static self) -> Option<GlobalGuard<B>> {
Some(GlobalGuard {
@@ -96,6 +97,7 @@ pub fn try_lock(&'static self) -> Option<GlobalGuard<B>> {
/// A guard for a [`GlobalLock`].
///
/// See [`global_lock!`] for examples.
+#[must_use = "the lock unlocks immediately when the guard is unused"]
pub struct GlobalGuard<B: GlobalLockBackend> {
inner: Guard<'static, B::Item, B::Backend>,
}
--
2.34.1
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH v2] rust: sync: add #[must_use] to GlobalGuard and GlobalLock::try_lock
2026-05-02 16:00 [PATCH v2] rust: sync: add #[must_use] to GlobalGuard and GlobalLock::try_lock Ashutosh Desai
@ 2026-05-05 12:38 ` Gary Guo
2026-06-04 18:44 ` Miguel Ojeda
2026-06-04 20:23 ` Miguel Ojeda
2 siblings, 0 replies; 5+ messages in thread
From: Gary Guo @ 2026-05-05 12:38 UTC (permalink / raw)
To: Ashutosh Desai, ojeda
Cc: peterz, mingo, will, boqun, wl, gary, bjorn3_gh, lossin,
a.hindborg, aliceryhl, tmgross, dakr, linux-kernel,
rust-for-linux
On Sat May 2, 2026 at 5:00 PM BST, Ashutosh Desai wrote:
> Guard is marked #[must_use] since dropping it releases the lock. GlobalGuard
> wraps Guard with identical semantics but was missing the annotation, so
> discarding it would silently compile without warning.
>
> Similarly, GlobalLock::try_lock was missing #[must_use]. Option<T> does not
> propagate #[must_use] from T, so the attribute needs to be on the function
> directly - same reason Lock::try_lock has it.
>
> Reviewed-by: Alice Ryhl <aliceryhl@google.com>
> Signed-off-by: Ashutosh Desai <ashutoshdesai993@gmail.com>
Reviewed-by: Gary Guo <gary@garyguo.net>
> ---
> Changes in v2:
> - Remove explanatory comment above #[must_use] on try_lock (Alice Ryhl)
>
> Link to v1: https://lore.kernel.org/rust-for-linux/20260419001141.211935-1-ashutoshdesai993@gmail.com/
>
> rust/kernel/sync/lock/global.rs | 2 ++
> 1 file changed, 2 insertions(+)
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH v2] rust: sync: add #[must_use] to GlobalGuard and GlobalLock::try_lock
2026-05-02 16:00 [PATCH v2] rust: sync: add #[must_use] to GlobalGuard and GlobalLock::try_lock Ashutosh Desai
2026-05-05 12:38 ` Gary Guo
@ 2026-06-04 18:44 ` Miguel Ojeda
2026-06-04 19:30 ` Boqun Feng
2026-06-04 20:23 ` Miguel Ojeda
2 siblings, 1 reply; 5+ messages in thread
From: Miguel Ojeda @ 2026-06-04 18:44 UTC (permalink / raw)
To: Ashutosh Desai, boqun
Cc: ojeda, peterz, mingo, will, wl, gary, bjorn3_gh, lossin,
a.hindborg, aliceryhl, tmgross, dakr, linux-kernel,
rust-for-linux
On Sat, May 2, 2026 at 6:02 PM Ashutosh Desai
<ashutoshdesai993@gmail.com> wrote:
>
> Guard is marked #[must_use] since dropping it releases the lock. GlobalGuard
> wraps Guard with identical semantics but was missing the annotation, so
> discarding it would silently compile without warning.
>
> Similarly, GlobalLock::try_lock was missing #[must_use]. Option<T> does not
> propagate #[must_use] from T, so the attribute needs to be on the function
> directly - same reason Lock::try_lock has it.
>
> Reviewed-by: Alice Ryhl <aliceryhl@google.com>
> Signed-off-by: Ashutosh Desai <ashutoshdesai993@gmail.com>
I could pick this one -- do you want me to, Boqun?
Cheers,
Miguel
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH v2] rust: sync: add #[must_use] to GlobalGuard and GlobalLock::try_lock
2026-06-04 18:44 ` Miguel Ojeda
@ 2026-06-04 19:30 ` Boqun Feng
0 siblings, 0 replies; 5+ messages in thread
From: Boqun Feng @ 2026-06-04 19:30 UTC (permalink / raw)
To: Miguel Ojeda, Ashutosh Desai
Cc: Miguel Ojeda, Peter Zijlstra, Ingo Molnar, Will Deacon, wl,
Gary Guo, bjorn3_gh, Benno Lossin, Andreas Hindborg, Alice Ryhl,
Trevor Gross, Danilo Krummrich, linux-kernel, rust-for-linux
On Thu, Jun 4, 2026, at 11:44 AM, Miguel Ojeda wrote:
> On Sat, May 2, 2026 at 6:02 PM Ashutosh Desai
> <ashutoshdesai993@gmail.com> wrote:
>>
>> Guard is marked #[must_use] since dropping it releases the lock. GlobalGuard
>> wraps Guard with identical semantics but was missing the annotation, so
>> discarding it would silently compile without warning.
>>
>> Similarly, GlobalLock::try_lock was missing #[must_use]. Option<T> does not
>> propagate #[must_use] from T, so the attribute needs to be on the function
>> directly - same reason Lock::try_lock has it.
>>
>> Reviewed-by: Alice Ryhl <aliceryhl@google.com>
>> Signed-off-by: Ashutosh Desai <ashutoshdesai993@gmail.com>
>
> I could pick this one -- do you want me to, Boqun?
>
Yes, please! Thank you!
Regards,
Boqun
> Cheers,
> Miguel
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH v2] rust: sync: add #[must_use] to GlobalGuard and GlobalLock::try_lock
2026-05-02 16:00 [PATCH v2] rust: sync: add #[must_use] to GlobalGuard and GlobalLock::try_lock Ashutosh Desai
2026-05-05 12:38 ` Gary Guo
2026-06-04 18:44 ` Miguel Ojeda
@ 2026-06-04 20:23 ` Miguel Ojeda
2 siblings, 0 replies; 5+ messages in thread
From: Miguel Ojeda @ 2026-06-04 20:23 UTC (permalink / raw)
To: Ashutosh Desai
Cc: ojeda, peterz, mingo, will, boqun, wl, gary, bjorn3_gh, lossin,
a.hindborg, aliceryhl, tmgross, dakr, linux-kernel,
rust-for-linux
On Sat, May 2, 2026 at 6:02 PM Ashutosh Desai
<ashutoshdesai993@gmail.com> wrote:
>
> Guard is marked #[must_use] since dropping it releases the lock. GlobalGuard
> wraps Guard with identical semantics but was missing the annotation, so
> discarding it would silently compile without warning.
>
> Similarly, GlobalLock::try_lock was missing #[must_use]. Option<T> does not
> propagate #[must_use] from T, so the attribute needs to be on the function
> directly - same reason Lock::try_lock has it.
>
> Reviewed-by: Alice Ryhl <aliceryhl@google.com>
> Signed-off-by: Ashutosh Desai <ashutoshdesai993@gmail.com>
Applied to `rust-next` -- thanks everyone!
Cheers,
Miguel
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-06-04 20:23 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-05-02 16:00 [PATCH v2] rust: sync: add #[must_use] to GlobalGuard and GlobalLock::try_lock Ashutosh Desai
2026-05-05 12:38 ` Gary Guo
2026-06-04 18:44 ` Miguel Ojeda
2026-06-04 19:30 ` Boqun Feng
2026-06-04 20:23 ` Miguel Ojeda
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®