* [PATCH v2] rust: sync: add accessor for the lock behind a given guard
@ 2025-02-05 10:54 Alice Ryhl
2025-02-05 18:52 ` Fiona Behrens
` (2 more replies)
0 siblings, 3 replies; 4+ messages in thread
From: Alice Ryhl @ 2025-02-05 10:54 UTC (permalink / raw)
To: Miguel Ojeda
Cc: Boqun Feng, Gary Guo, Björn Roy Baron, Benno Lossin,
Andreas Hindborg, Trevor Gross, Peter Zijlstra, Ingo Molnar,
Will Deacon, Waiman Long, rust-for-linux, linux-kernel,
Alice Ryhl
Binder has some methods where the caller provides a lock guard, and
Binder needs to be able to assert that the guard is associated with the
right lock. To enable this, add an accessor to obtain a reference to the
underlying lock that you can pass to `ptr::eq`.
Signed-off-by: Alice Ryhl <aliceryhl@google.com>
---
Changes in v2:
- Rename to lock_ref() to avoid confusion with lock().
- Rebase on v6.14-rc1.
- Link to v1: https://lore.kernel.org/r/20250130-guard-get-lock-v1-1-8ed87899920a@google.com
---
rust/kernel/sync/lock.rs | 7 ++++++-
1 file changed, 6 insertions(+), 1 deletion(-)
diff --git a/rust/kernel/sync/lock.rs b/rust/kernel/sync/lock.rs
index eb80048e0110..8e7e6d5f61e4 100644
--- a/rust/kernel/sync/lock.rs
+++ b/rust/kernel/sync/lock.rs
@@ -199,7 +199,12 @@ pub struct Guard<'a, T: ?Sized, B: Backend> {
// SAFETY: `Guard` is sync when the data protected by the lock is also sync.
unsafe impl<T: Sync + ?Sized, B: Backend> Sync for Guard<'_, T, B> {}
-impl<T: ?Sized, B: Backend> Guard<'_, T, B> {
+impl<'a, T: ?Sized, B: Backend> Guard<'a, T, B> {
+ /// Returns the lock that this guard originates from.
+ pub fn lock_ref(&self) -> &'a Lock<T, B> {
+ self.lock
+ }
+
pub(crate) 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: 2014c95afecee3e76ca4a56956a936e23283f05b
change-id: 20250130-guard-get-lock-dd5452793d9a
Best regards,
--
Alice Ryhl <aliceryhl@google.com>
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH v2] rust: sync: add accessor for the lock behind a given guard
2025-02-05 10:54 [PATCH v2] rust: sync: add accessor for the lock behind a given guard Alice Ryhl
@ 2025-02-05 18:52 ` Fiona Behrens
2025-02-10 6:03 ` Boqun Feng
2025-03-03 18:51 ` [tip: locking/core] rust: sync: Add " tip-bot2 for Alice Ryhl
2 siblings, 0 replies; 4+ messages in thread
From: Fiona Behrens @ 2025-02-05 18:52 UTC (permalink / raw)
To: Alice Ryhl
Cc: Miguel Ojeda, Boqun Feng, Gary Guo, Björn Roy Baron,
Benno Lossin, Andreas Hindborg, Trevor Gross, Peter Zijlstra,
Ingo Molnar, Will Deacon, Waiman Long, rust-for-linux,
linux-kernel
Alice Ryhl <aliceryhl@google.com> writes:
> Binder has some methods where the caller provides a lock guard, and
> Binder needs to be able to assert that the guard is associated with the
> right lock. To enable this, add an accessor to obtain a reference to the
> underlying lock that you can pass to `ptr::eq`.
>
> Signed-off-by: Alice Ryhl <aliceryhl@google.com>
Reviewed-by: Fiona Behrens <me@kloenk.dev>
Thanks,
Fiona
> ---
> Changes in v2:
> - Rename to lock_ref() to avoid confusion with lock().
> - Rebase on v6.14-rc1.
> - Link to v1: https://lore.kernel.org/r/20250130-guard-get-lock-v1-1-8ed87899920a@google.com
> ---
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH v2] rust: sync: add accessor for the lock behind a given guard
2025-02-05 10:54 [PATCH v2] rust: sync: add accessor for the lock behind a given guard Alice Ryhl
2025-02-05 18:52 ` Fiona Behrens
@ 2025-02-10 6:03 ` Boqun Feng
2025-03-03 18:51 ` [tip: locking/core] rust: sync: Add " tip-bot2 for Alice Ryhl
2 siblings, 0 replies; 4+ messages in thread
From: Boqun Feng @ 2025-02-10 6:03 UTC (permalink / raw)
To: Alice Ryhl
Cc: Miguel Ojeda, Gary Guo, Björn Roy Baron, Benno Lossin,
Andreas Hindborg, Trevor Gross, Peter Zijlstra, Ingo Molnar,
Will Deacon, Waiman Long, rust-for-linux, linux-kernel
Hi,
On Wed, Feb 05, 2025 at 10:54:50AM +0000, Alice Ryhl wrote:
> Binder has some methods where the caller provides a lock guard, and
> Binder needs to be able to assert that the guard is associated with the
> right lock. To enable this, add an accessor to obtain a reference to the
> underlying lock that you can pass to `ptr::eq`.
>
> Signed-off-by: Alice Ryhl <aliceryhl@google.com>
> ---
> Changes in v2:
> - Rename to lock_ref() to avoid confusion with lock().
> - Rebase on v6.14-rc1.
> - Link to v1: https://lore.kernel.org/r/20250130-guard-get-lock-v1-1-8ed87899920a@google.com
> ---
> rust/kernel/sync/lock.rs | 7 ++++++-
> 1 file changed, 6 insertions(+), 1 deletion(-)
>
> diff --git a/rust/kernel/sync/lock.rs b/rust/kernel/sync/lock.rs
> index eb80048e0110..8e7e6d5f61e4 100644
> --- a/rust/kernel/sync/lock.rs
> +++ b/rust/kernel/sync/lock.rs
> @@ -199,7 +199,12 @@ pub struct Guard<'a, T: ?Sized, B: Backend> {
> // SAFETY: `Guard` is sync when the data protected by the lock is also sync.
> unsafe impl<T: Sync + ?Sized, B: Backend> Sync for Guard<'_, T, B> {}
>
> -impl<T: ?Sized, B: Backend> Guard<'_, T, B> {
> +impl<'a, T: ?Sized, B: Backend> Guard<'a, T, B> {
> + /// Returns the lock that this guard originates from.
> + pub fn lock_ref(&self) -> &'a Lock<T, B> {
> + self.lock
> + }
> +
> pub(crate) 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: 2014c95afecee3e76ca4a56956a936e23283f05b
> change-id: 20250130-guard-get-lock-dd5452793d9a
>
> Best regards,
> --
> Alice Ryhl <aliceryhl@google.com>
>
I queued this with some changes on the commit log to make it more of an
objective tone, please see below, thanks!
Regards,
Boqun
--------------------------->8
Subject: [PATCH] rust: sync: Add accessor for the lock behind a given guard
In order to assert a particular `Guard` is associated with a particular
`Lock`, add an accessor to obtain a reference to the underlying `Lock`
of a `Guard`.
Binder needs this assertion to ensure unsafe list operations are done
with the correct lock held.
[Boqun: Capitalize the title and reword the commit log]
Signed-off-by: Alice Ryhl <aliceryhl@google.com>
Reviewed-by: Fiona Behrens <me@kloenk.dev>
Signed-off-by: Boqun Feng <boqun.feng@gmail.com>
Link: https://lore.kernel.org/r/20250205-guard-get-lock-v2-1-ba32a8c1d5b7@google.com
---
rust/kernel/sync/lock.rs | 7 ++++++-
1 file changed, 6 insertions(+), 1 deletion(-)
diff --git a/rust/kernel/sync/lock.rs b/rust/kernel/sync/lock.rs
index eb80048e0110..8e7e6d5f61e4 100644
--- a/rust/kernel/sync/lock.rs
+++ b/rust/kernel/sync/lock.rs
@@ -199,7 +199,12 @@ pub struct Guard<'a, T: ?Sized, B: Backend> {
// SAFETY: `Guard` is sync when the data protected by the lock is also sync.
unsafe impl<T: Sync + ?Sized, B: Backend> Sync for Guard<'_, T, B> {}
-impl<T: ?Sized, B: Backend> Guard<'_, T, B> {
+impl<'a, T: ?Sized, B: Backend> Guard<'a, T, B> {
+ /// Returns the lock that this guard originates from.
+ pub fn lock_ref(&self) -> &'a Lock<T, B> {
+ self.lock
+ }
+
pub(crate) 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) };
--
2.39.5 (Apple Git-154)
^ permalink raw reply related [flat|nested] 4+ messages in thread
* [tip: locking/core] rust: sync: Add accessor for the lock behind a given guard
2025-02-05 10:54 [PATCH v2] rust: sync: add accessor for the lock behind a given guard Alice Ryhl
2025-02-05 18:52 ` Fiona Behrens
2025-02-10 6:03 ` Boqun Feng
@ 2025-03-03 18:51 ` tip-bot2 for Alice Ryhl
2 siblings, 0 replies; 4+ messages in thread
From: tip-bot2 for Alice Ryhl @ 2025-03-03 18:51 UTC (permalink / raw)
To: linux-tip-commits
Cc: Alice Ryhl, Fiona Behrens, Boqun Feng, x86, linux-kernel
The following commit has been merged into the locking/core branch of tip:
Commit-ID: 36dbde71fcece19699cef0d01bd2123a6d3142f4
Gitweb: https://git.kernel.org/tip/36dbde71fcece19699cef0d01bd2123a6d3142f4
Author: Alice Ryhl <aliceryhl@google.com>
AuthorDate: Wed, 05 Feb 2025 10:54:50
Committer: Boqun Feng <boqun.feng@gmail.com>
CommitterDate: Sun, 23 Feb 2025 18:24:46 -08:00
rust: sync: Add accessor for the lock behind a given guard
In order to assert a particular `Guard` is associated with a particular
`Lock`, add an accessor to obtain a reference to the underlying `Lock`
of a `Guard`.
Binder needs this assertion to ensure unsafe list operations are done
with the correct lock held.
[Boqun: Capitalize the title and reword the commit log]
Signed-off-by: Alice Ryhl <aliceryhl@google.com>
Reviewed-by: Fiona Behrens <me@kloenk.dev>
Signed-off-by: Boqun Feng <boqun.feng@gmail.com>
Link: https://lore.kernel.org/r/20250205-guard-get-lock-v2-1-ba32a8c1d5b7@google.com
---
rust/kernel/sync/lock.rs | 7 ++++++-
1 file changed, 6 insertions(+), 1 deletion(-)
diff --git a/rust/kernel/sync/lock.rs b/rust/kernel/sync/lock.rs
index eb80048..8e7e6d5 100644
--- a/rust/kernel/sync/lock.rs
+++ b/rust/kernel/sync/lock.rs
@@ -199,7 +199,12 @@ pub struct Guard<'a, T: ?Sized, B: Backend> {
// SAFETY: `Guard` is sync when the data protected by the lock is also sync.
unsafe impl<T: Sync + ?Sized, B: Backend> Sync for Guard<'_, T, B> {}
-impl<T: ?Sized, B: Backend> Guard<'_, T, B> {
+impl<'a, T: ?Sized, B: Backend> Guard<'a, T, B> {
+ /// Returns the lock that this guard originates from.
+ pub fn lock_ref(&self) -> &'a Lock<T, B> {
+ self.lock
+ }
+
pub(crate) 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) };
^ permalink raw reply related [flat|nested] 4+ messages in thread
end of thread, other threads:[~2025-03-03 18:52 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-02-05 10:54 [PATCH v2] rust: sync: add accessor for the lock behind a given guard Alice Ryhl
2025-02-05 18:52 ` Fiona Behrens
2025-02-10 6:03 ` Boqun Feng
2025-03-03 18:51 ` [tip: locking/core] rust: sync: Add " tip-bot2 for Alice Ryhl
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.