All of lore.kernel.org
 help / color / mirror / Atom feed
From: Dirk Behme <dirk.behme@gmail.com>
To: Lyude Paul <lyude@redhat.com>,
	rust-for-linux@vger.kernel.org,
	Thomas Gleixner <tglx@linutronix.de>
Cc: "Boqun Feng" <boqun.feng@gmail.com>,
	"Peter Zijlstra" <peterz@infradead.org>,
	"Ingo Molnar" <mingo@redhat.com>, "Will Deacon" <will@kernel.org>,
	"Waiman Long" <longman@redhat.com>,
	"Miguel Ojeda" <ojeda@kernel.org>,
	"Alex Gaynor" <alex.gaynor@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@kernel.org>,
	"Alice Ryhl" <aliceryhl@google.com>,
	"Trevor Gross" <tmgross@umich.edu>,
	"open list:LOCKING PRIMITIVES" <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH v9 8/9] rust: sync: lock: Add `Backend::BackendInContext`
Date: Mon, 3 Mar 2025 15:23:08 +0100	[thread overview]
Message-ID: <daffef96-7c50-4162-bd93-e7bc20af9ee6@gmail.com> (raw)
In-Reply-To: <20250227221924.265259-9-lyude@redhat.com>

On 27.02.25 23:10, Lyude Paul wrote:
> From: Boqun Feng <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


Use the it -> Use it (drop "the")?


> `SpinLockIrq` can avoid disabling interrupts by using `SpinLock`'s
> backend.
> 
> Signed-off-by: Boqun Feng <boqun.feng@gmail.com>
> Signed-off-by: Lyude Paul <lyude@redhat.com>
> ---
>  rust/kernel/sync/lock.rs          | 26 ++++++++++++++++++--
>  rust/kernel/sync/lock/mutex.rs    |  1 +
>  rust/kernel/sync/lock/spinlock.rs | 41 +++++++++++++++++++++++++++++++
>  3 files changed, 66 insertions(+), 2 deletions(-)
> 
> diff --git a/rust/kernel/sync/lock.rs b/rust/kernel/sync/lock.rs
> index e7c1fd028435e..54c77972c83f8 100644
> --- a/rust/kernel/sync/lock.rs
> +++ b/rust/kernel/sync/lock.rs
> @@ -32,10 +32,15 @@
>  ///   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


to acquire the lock (add "the")?


> +///   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
> +/// [`State`]: Backend::State
>  pub unsafe trait Backend {
>      /// The state required by the lock.
>      type State;
> @@ -49,6 +54,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`](Backend::Context) is provided.
> +    type BackendInContext: Sized;
> +
>      /// Initialises the lock.
>      ///
>      /// # Safety
> @@ -170,8 +178,22 @@ pub unsafe fn from_raw<'a>(ptr: *mut B::State) -> &'a Self {
>  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

the -> to (safe to convert)?

Cheers,

Dirk


  reply	other threads:[~2025-03-03 14:23 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-02-27 22:10 [PATCH v9 0/9] Refcounted interrupts, SpinLockIrq for rust Lyude Paul
2025-02-27 22:10 ` [PATCH v9 1/9] preempt: Introduce HARDIRQ_DISABLE_BITS Lyude Paul
2025-02-27 23:09   ` Steven Rostedt
2025-02-28  1:33     ` Boqun Feng
2025-03-03 21:55       ` Lyude Paul
2025-02-28  7:57   ` Peter Zijlstra
2025-02-27 22:10 ` [PATCH v9 2/9] preempt: Introduce __preempt_count_{sub, add}_return() Lyude Paul
2025-02-28  1:49   ` Boqun Feng
2025-02-28  9:15   ` Heiko Carstens
2025-02-28  9:24     ` Peter Zijlstra
2025-04-30 21:38     ` Lyude Paul
2025-05-05  9:56       ` Heiko Carstens
2025-03-01 18:49   ` kernel test robot
2025-03-01 19:00   ` kernel test robot
2025-02-27 22:10 ` [PATCH v9 3/9] irq & spin_lock: Add counted interrupt disabling/enabling Lyude Paul
2025-03-01 20:19   ` kernel test robot
2025-02-27 22:10 ` [PATCH v9 4/9] rust: Introduce interrupt module Lyude Paul
2025-03-02 16:56   ` Dirk Behme
2025-02-27 22:10 ` [PATCH v9 5/9] rust: helper: Add spin_{un,}lock_irq_{enable,disable}() helpers Lyude Paul
2025-02-27 22:10 ` [PATCH v9 6/9] rust: sync: Add SpinLockIrq Lyude Paul
2025-03-02 11:51   ` Guangbo Cui
2025-03-03 22:15     ` Lyude Paul
2025-03-02 17:07   ` Dirk Behme
2025-04-04 21:56     ` Lyude Paul
2025-02-27 22:10 ` [PATCH v9 7/9] rust: sync: Introduce lock::Backend::Context Lyude Paul
2025-03-03 14:22   ` Dirk Behme
2025-02-27 22:10 ` [PATCH v9 8/9] rust: sync: lock: Add `Backend::BackendInContext` Lyude Paul
2025-03-03 14:23   ` Dirk Behme [this message]
2025-02-27 22:10 ` [PATCH v9 9/9] locking: Switch to _irq_{disable,enable}() variants in cleanup guards Lyude Paul
2025-04-05  8:25   ` Guangbo Cui
2025-04-05  8:55     ` Guangbo Cui

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=daffef96-7c50-4162-bd93-e7bc20af9ee6@gmail.com \
    --to=dirk.behme@gmail.com \
    --cc=a.hindborg@kernel.org \
    --cc=alex.gaynor@gmail.com \
    --cc=aliceryhl@google.com \
    --cc=benno.lossin@proton.me \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun.feng@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=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 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.