From: Thomas Gleixner <tglx@linutronix.de>
To: Lyude Paul <lyude@redhat.com>, rust-for-linux@vger.kernel.org
Cc: "Danilo Krummrich" <dakr@redhat.com>,
airlied@redhat.com, "Ingo Molnar" <mingo@redhat.com>,
"Will Deacon" <will@kernel.org>,
"Waiman Long" <longman@redhat.com>,
"Peter Zijlstra" <peterz@infradead.org>,
linux-kernel@vger.kernel.org,
"Benno Lossin" <benno.lossin@proton.me>,
"Daniel Almeida" <daniel.almeida@collabora.com>,
"Gary Guo" <gary@garyguo.net>, "Miguel Ojeda" <ojeda@kernel.org>,
"Alex Gaynor" <alex.gaynor@gmail.com>,
"Wedson Almeida Filho" <wedsonaf@gmail.com>,
"Boqun Feng" <boqun.feng@gmail.com>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Andreas Hindborg" <a.hindborg@samsung.com>,
"Alice Ryhl" <aliceryhl@google.com>,
"Trevor Gross" <tmgross@umich.edu>,
"Martin Rodriguez Reboredo" <yakoyoku@gmail.com>,
"Valentin Obst" <kernel@valentinobst.de>
Subject: Re: [PATCH v6 3/3] rust: sync: Add SpinLockIrq
Date: Tue, 08 Oct 2024 17:21:19 +0200 [thread overview]
Message-ID: <878quytyc0.ffs@tglx> (raw)
In-Reply-To: <9f580260866a202d7608f902464b7fae087dd6c6.camel@redhat.com>
On Mon, Oct 07 2024 at 14:30, Lyude Paul wrote:
> On Mon, 2024-10-07 at 14:01 +0200, Thomas Gleixner wrote:
> So actually the new solution I suggested a little after that original email
> wouldn't need to call local_irq_save() directly - sorry, I just explained it
> kind of poorly and it hadn't been in my head for very long. I think you'll
> like this solution a lot more though, lemme explain:
>
> Basically instead of having functions like with_interrupts_disabled, we would
> instead introduce a new trait that can be implemented by locks with context
> tokens: BackendWithContext:
>
> pub trait BackendWithContext: Backend {
> type ContextState;
>
> unsafe fn lock_first(ptr: *Self::State)
> -> (Self::Context, Self::ContextState, Self::GuardState);
>
> unsafe fn unlock_last(
> ptr: *Self::State,
> context_state: Self::ContextState,
> guard_state: &Self::GuardState
> );
> }
>
> Where the idea is that a type like SpinlockIrq would define ContextState to be
> a u64 (holding the flags argument from spin_lock_irqsave). lock_first() would
> use spin_lock_irqsave and create the token, unlock_last() would use
> spin_unlock_irqrestore with the saved ContextState. Then we could use those
> unsafe primitives to implement a method on Lock like this:
>
> impl<T: ?Sized, B: BackendWithContext> Lock<T, B> {
> pub fn lock_with_new<'a>(
> &self,
> cb: impl FnOnce(Self::Context, &mut Guard<'a, T, B>) -> R
> ) -> R;
> }
>
> What lock_with_new would do is:
>
> * call B::first_lock() (which would be spin_lock_irqsave)
> * call cb() with a LocalInterruptsDisabled token and a &mut to the Guard (so
> that the caller can't drop the lock before exiting the noirq context)
> * Call B::last_unlock() with the ContextState that was passed to first_lock()
> (which would be spin_unlock_irqrestore)
>
> So we'd absolutely still be modeling around the locking primitives
> spin_lock_irqsave() and spin_unlock_irqrestore(). And subsequently we could
> still nest lock contexts like normal. with_irqs_disabled() wouldn't be needed
> in this arrangement - but we would still need the Interrupt tokens (which
> would be fine since they're just for enforcing correctness anyway).
Makes sense.
>> The above example really should not end up in 3 guard contexts, but in
>> two by combining #1 and #2 into one. In C this looks like:
>>
>> scoped_guard(spinlock_irqsave)(&A) {
>> // Allows to operate on resources which are exclusively
>> // protected by A (DataA)
>>
>> scoped_guard(spinlock)(&B) {
>> // Allows to operate on resources which are exclusively
>> // protected by B (DataB)
>> }
>> }
>>
>> Nesting B into lock A is required to keep some aspects of DataA and
>> DataB consistent. But the other parts of DataB require only B to be
>> held.
>>
>> For extended fun lock B is not necessarily required to be acquired with
>> interrupts disabled. The fact that it nests into lock A does not make it
>> mandatory.
>>
>> A lock is only required to be acquired with interrupts disabled if it
>> can be taken in interrupt context. That's a per lock property.
>
> I think you misunderstood something somewhere - this has always been the case
> with the bindings I submitted that you don't need a context for all locks,
> only locks that define one. That is why we reimplement lock() to look like
> this (where T is the data protected by the lock and B is the backend):
>
> pub fn lock<'a>(&'a self) -> Guard<'a, T, B>
> where
> B::Context<'a>: Default
> {
> self.lock_with(Default::default())
> }
>
> So SpinLock's B::Context is (), which implements Default - meaning you can
> acquire it simply like this:
>
> some_lock.lock();
>
> But that wouldn't work for SpinLockIrq with a context of IrqDisabled<'a>,
> since IrqDisabled doesn't implement Default.
Thanks for clarification. It's clear now.
Thanks,
tglx
next prev parent reply other threads:[~2024-10-08 15:21 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 [this message]
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 ` [POC 6/6] rust: sync: lock: Add `Backend::BackendInContext` Boqun Feng
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=878quytyc0.ffs@tglx \
--to=tglx@linutronix.de \
--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=boqun.feng@gmail.com \
--cc=dakr@redhat.com \
--cc=daniel.almeida@collabora.com \
--cc=gary@garyguo.net \
--cc=kernel@valentinobst.de \
--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=tmgross@umich.edu \
--cc=wedsonaf@gmail.com \
--cc=will@kernel.org \
--cc=yakoyoku@gmail.com \
/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).