From: Boqun Feng <boqun.feng@gmail.com>
To: "Thomas Gleixner" <tglx@linutronix.de>
Cc: "Dirk Behme" <dirk.behme@gmail.com>,
"Lyude Paul" <lyude@redhat.com>,
rust-for-linux@vger.kernel.org,
"Danilo Krummrich" <dakr@redhat.com>,
airlied@redhat.com, "Ingo Molnar" <mingo@redhat.com>,
will@kernel.org, "Waiman Long" <longman@redhat.com>,
"Peter Zijlstra" <peterz@infradead.org>,
linux-kernel@vger.kernel.org, "Miguel Ojeda" <ojeda@kernel.org>,
"Alex Gaynor" <alex.gaynor@gmail.com>,
wedsonaf@gmail.com, "Gary Guo" <gary@garyguo.net>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Benno Lossin" <benno.lossin@proton.me>,
"Andreas Hindborg" <a.hindborg@samsung.com>,
aliceryhl@google.com, "Trevor Gross" <tmgross@umich.edu>,
"Boqun Feng" <boqun.feng@gmail.com>
Subject: [POC 6/6] rust: sync: lock: Add `Backend::BackendInContext`
Date: Thu, 17 Oct 2024 22:51:25 -0700 [thread overview]
Message-ID: <20241018055125.2784186-7-boqun.feng@gmail.com> (raw)
In-Reply-To: <20241018055125.2784186-1-boqun.feng@gmail.com>
`SpinLock`'s backend can be used for `SpinLockIrq`, if the interrupts
are disabled. And it actually provides performance gains since
interrupts are not needed to be disabled anymore. So add
`Backend::BackendInContext` to describe the case where one backend can
be used for another. Use the it to implement the `lock_with()` so that
`SpinLockIrq` can avoid disabling interrupts by using `SpinLock`'s
backend.
Signed-off-by: Boqun Feng <boqun.feng@gmail.com>
---
rust/kernel/sync/lock.rs | 25 +++++++++++++++++++++++--
rust/kernel/sync/lock/mutex.rs | 1 +
rust/kernel/sync/lock/spinlock.rs | 9 +++++++++
3 files changed, 33 insertions(+), 2 deletions(-)
diff --git a/rust/kernel/sync/lock.rs b/rust/kernel/sync/lock.rs
index 49b53433201c..4e3316feb497 100644
--- a/rust/kernel/sync/lock.rs
+++ b/rust/kernel/sync/lock.rs
@@ -24,10 +24,14 @@
/// is owned, that is, between calls to [`lock`] and [`unlock`].
/// - Implementers must also ensure that [`relock`] uses the same locking method as the original
/// lock operation.
+/// - Implementers must ensure if [`BackendInContext`] is a [`Backend`], it's safe to acquire lock
+/// under the [`Context`], the [`State`] of two backends must be the same.
///
/// [`lock`]: Backend::lock
/// [`unlock`]: Backend::unlock
/// [`relock`]: Backend::relock
+/// [`BackendInContext`]: Backend::BackendInContext
+/// [`Context`]: Backend::Context
pub unsafe trait Backend {
/// The state required by the lock.
type State;
@@ -41,6 +45,9 @@ pub unsafe trait Backend {
/// The context which can be provided to acquire the lock with a different backend.
type Context<'a>;
+ /// The alternative backend we can use if a [`Context`] is provided.
+ type BackendInContext: Sized;
+
/// Initialises the lock.
///
/// # Safety
@@ -125,8 +132,22 @@ pub fn new(t: T, name: &'static CStr, key: &'static LockClassKey) -> impl PinIni
impl<T: ?Sized, B: Backend> Lock<T, B> {
/// Acquires the lock with the given context and gives the caller access to the data protected
/// by it.
- pub fn lock_with<'a>(&'a self, _context: B::Context<'a>) -> Guard<'a, T, B> {
- todo!()
+ pub fn lock_with<'a>(&'a self, _context: B::Context<'a>) -> Guard<'a, T, B::BackendInContext>
+ where
+ B::BackendInContext: Backend,
+ {
+ // SAFETY: Per the safety guarantee of `Backend`, if `B::BackendIncontext` and `B` should
+ // have the same state, therefore the layout of the lock is the same so it's safe the
+ // convert one to another.
+ let lock = unsafe { &*(self as *const _ as *const Lock<T, B::BackendInContext>) };
+ // SAFETY: The constructor of the type calls `init`, so the existence of the object proves
+ // that `init` was called. Plus the safety guarantee of `Backend` guarantees that `B::state`
+ // is the same as `B::BackendInContext::state`, also it's safe to call another backend
+ // because there is `B::Context<'a>`.
+ let state = unsafe { B::BackendInContext::lock(lock.state.get()) };
+
+ // SAFETY: The lock was just acquired.
+ unsafe { Guard::new(lock, state) }
}
/// Acquires the lock and gives the caller access to the data protected by it.
diff --git a/rust/kernel/sync/lock/mutex.rs b/rust/kernel/sync/lock/mutex.rs
index 7c2c23994493..ddb7d06676f7 100644
--- a/rust/kernel/sync/lock/mutex.rs
+++ b/rust/kernel/sync/lock/mutex.rs
@@ -94,6 +94,7 @@ unsafe impl super::Backend for MutexBackend {
type State = bindings::mutex;
type GuardState = ();
type Context<'a> = ();
+ type BackendInContext = ();
unsafe fn init(
ptr: *mut Self::State,
diff --git a/rust/kernel/sync/lock/spinlock.rs b/rust/kernel/sync/lock/spinlock.rs
index 8f9e1b27e474..3bec25b65c55 100644
--- a/rust/kernel/sync/lock/spinlock.rs
+++ b/rust/kernel/sync/lock/spinlock.rs
@@ -94,6 +94,7 @@ unsafe impl super::Backend for SpinLockBackend {
type State = bindings::spinlock_t;
type GuardState = ();
type Context<'a> = ();
+ type BackendInContext = ();
unsafe fn init(
ptr: *mut Self::State,
@@ -146,6 +147,7 @@ macro_rules! new_spinlock_irq {
///
/// ```
/// use kernel::sync::{new_spinlock_irq, SpinLockIrq};
+/// use kernel::interrupt::InterruptDisabled;
///
/// struct Inner {
/// a: u32,
@@ -168,6 +170,12 @@ macro_rules! new_spinlock_irq {
/// }
/// }
///
+/// // Accessing an `Example` from a function that can only be called in no-irq contexts
+/// fn noirq_work(e: &Example, irq: &InterruptDisabled) {
+/// assert_eq!(e.c, 10);
+/// assert_eq!(e.d.lock_with(irq).a, 20);
+/// }
+///
/// // Allocate a boxed `Example`
/// let e = Box::pin_init(Example::new(), GFP_KERNEL)?;
///
@@ -186,6 +194,7 @@ unsafe impl super::Backend for SpinLockIrqBackend {
type State = bindings::spinlock_t;
type GuardState = ();
type Context<'a> = &'a InterruptDisabled;
+ type BackendInContext = SpinLockBackend;
unsafe fn init(
ptr: *mut Self::State,
--
2.45.2
next prev parent reply other threads:[~2024-10-18 5:52 UTC|newest]
Thread overview: 75+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-09-16 21:28 [PATCH v6 0/3] rust: Add irq abstraction, SpinLockIrq Lyude Paul
2024-09-16 21:28 ` [PATCH v6 1/3] rust: Introduce irq module Lyude Paul
2024-09-29 20:36 ` Trevor Gross
2024-09-29 23:45 ` Boqun Feng
2024-10-02 20:20 ` Thomas Gleixner
2024-10-04 8:58 ` Benno Lossin
2024-10-04 17:18 ` Lyude Paul
2024-10-17 18:51 ` Lyude Paul
2024-10-04 17:02 ` Lyude Paul
2024-10-10 21:00 ` Daniel Almeida
2024-09-16 21:28 ` [PATCH v6 2/3] rust: sync: Introduce lock::Backend::Context Lyude Paul
2024-09-29 20:40 ` Trevor Gross
2024-09-29 23:52 ` Boqun Feng
2024-09-16 21:28 ` [PATCH v6 3/3] rust: sync: Add SpinLockIrq Lyude Paul
2024-09-29 20:50 ` Trevor Gross
2024-09-29 23:59 ` Boqun Feng
2024-10-02 20:53 ` Thomas Gleixner
2024-10-03 12:51 ` Boqun Feng
2024-10-04 18:48 ` Lyude Paul
2024-10-05 18:19 ` Lyude Paul
2024-10-07 12:42 ` Boqun Feng
2024-10-07 18:13 ` Lyude Paul
2024-10-15 12:57 ` Andreas Hindborg
2024-10-15 20:17 ` Boqun Feng
2024-10-15 20:21 ` Boqun Feng
2024-10-16 20:57 ` Lyude Paul
2024-10-17 13:34 ` Andreas Hindborg
2024-10-07 12:01 ` Thomas Gleixner
2024-10-07 18:30 ` Lyude Paul
2024-10-08 15:21 ` Thomas Gleixner
2024-10-12 8:01 ` Boqun Feng
2024-10-10 16:39 ` [PATCH v6 0/3] rust: Add irq abstraction, SpinLockIrq Daniel Almeida
2024-10-12 5:29 ` Dirk Behme
2024-10-13 19:06 ` Thomas Gleixner
2024-10-13 21:43 ` Boqun Feng
2024-10-16 21:00 ` Thomas Gleixner
2024-10-16 21:31 ` Boqun Feng
2024-10-17 20:49 ` Lyude Paul
2024-10-17 22:27 ` Boqun Feng
2024-10-18 5:51 ` [POC 0/6] Allow SpinLockIrq to use a normal Guard interface Boqun Feng
2024-10-18 5:51 ` [POC 1/6] irq & spin_lock: Add counted interrupt disabling/enabling Boqun Feng
2024-10-21 7:04 ` kernel test robot
2024-10-21 7:35 ` kernel test robot
2024-10-21 20:44 ` Lyude Paul
2024-10-24 16:18 ` Peter Zijlstra
2024-10-23 19:34 ` Thomas Gleixner
2024-10-23 19:51 ` Peter Zijlstra
2024-10-23 20:38 ` Thomas Gleixner
2024-10-24 10:05 ` Peter Zijlstra
2024-10-24 17:22 ` Thomas Gleixner
2024-10-24 21:57 ` Boqun Feng
2024-10-25 15:04 ` Thomas Gleixner
2024-10-25 18:28 ` Peter Zijlstra
2024-10-24 19:12 ` Lyude Paul
2025-07-24 20:36 ` w/r/t "irq & spin_lock: Add counted interrupt disabling/enabling": holes in pcpu_hot? Lyude Paul
2025-07-24 21:59 ` Thomas Gleixner
2024-10-24 5:05 ` [POC 1/6] irq & spin_lock: Add counted interrupt disabling/enabling Boqun Feng
2024-10-24 8:17 ` Thomas Gleixner
2024-10-24 16:20 ` Boqun Feng
2024-10-18 5:51 ` [POC 2/6] rust: Introduce interrupt module Boqun Feng
2024-10-31 20:45 ` Lyude Paul
2024-10-31 20:47 ` Lyude Paul
2024-10-18 5:51 ` [POC 3/6] rust: helper: Add spin_{un,}lock_irq_{enable,disable}() helpers Boqun Feng
2024-10-18 5:51 ` [POC 4/6] rust: sync: Add SpinLockIrq Boqun Feng
2024-10-18 19:23 ` Lyude Paul
2024-10-18 20:22 ` Boqun Feng
2024-10-18 5:51 ` [POC 5/6] rust: sync: Introduce lock::Backend::Context Boqun Feng
2024-10-31 20:54 ` Lyude Paul
2024-10-18 5:51 ` Boqun Feng [this message]
2024-10-18 10:22 ` [POC 0/6] Allow SpinLockIrq to use a normal Guard interface Andreas Hindborg
2024-10-18 12:42 ` Boqun Feng
2024-10-18 11:16 ` Andreas Hindborg
2024-10-18 16:05 ` Dirk Behme
2024-10-31 20:56 ` Lyude Paul
2024-10-17 20:42 ` [PATCH v6 0/3] rust: Add irq abstraction, SpinLockIrq 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=20241018055125.2784186-7-boqun.feng@gmail.com \
--to=boqun.feng@gmail.com \
--cc=a.hindborg@samsung.com \
--cc=airlied@redhat.com \
--cc=alex.gaynor@gmail.com \
--cc=aliceryhl@google.com \
--cc=benno.lossin@proton.me \
--cc=bjorn3_gh@protonmail.com \
--cc=dakr@redhat.com \
--cc=dirk.behme@gmail.com \
--cc=gary@garyguo.net \
--cc=linux-kernel@vger.kernel.org \
--cc=longman@redhat.com \
--cc=lyude@redhat.com \
--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=wedsonaf@gmail.com \
--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).