rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
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: Mon, 07 Oct 2024 14:01:39 +0200	[thread overview]
Message-ID: <87a5fgunoc.ffs@tglx> (raw)
In-Reply-To: <0a802e5fc0623ac8ae4653a398d0dfd73c479b96.camel@redhat.com>

On Fri, Oct 04 2024 at 14:48, Lyude Paul wrote:
> On Wed, 2024-10-02 at 22:53 +0200, Thomas Gleixner wrote:
>> At this phase of rust integration there is no need to wrap
>> raw_spinlock_t, so you have two options to solve that:
>> 
>>    1) Map Rust's SpinLockIrq() to spin_lock_irqsave() and
>>       spin_unlock_irqrestore() which does the right thing
>> 
>>    2) Play all the PREEMPT_RT games in the local irq disable abstraction
>
> I would very strongly rather #2. The problem with #1 is that one of the goals
> with the way I designed this abstraction with was to make it so that we could
> have lock guards that share the lifetime of the IrqDisabled token - which
> means the compiler can stop you from holding the lock outside of an
> IrqDisabled context. We have a powerful type system in rust, so IMO we should
> use it.
>
> I don't think this is as difficult to do as it seems either. One thing we
> could do is have two different versions of the with_irqs_disabled functions:
>
> with_irqs_disabled_on_nort
> with_irqs_disabled
>
> And as well, have each respectively return a different token type:
>
> IrqsDisabledNoRt -> Local interrupts are disabled on non-RT kernels
> IrqsDisabled -> Local interrupts are disabled always
>
> I think this actually is a nice solution, because it provides a number of
> benefits:
>
>  * It makes it much more clear that interrupts won't always be disabled. I'll
>    be honest, I've been working on drivers for almost a decade in the upstream
>    kernel and as you can see I don't think any of us actually realized
>    interrupts being turned off here wasn't a given :P. I'm sure it's
>    documented, but when you've been working on this stuff for so long you
>    don't always default to going back to documentation for stuff like this.
>  * Having two different token types would prevent raw spinlocks from being
>    used in contexts where it's not guaranteed local IRQs would be disabled -
>    and vice versa.

You really want to have two distinct lock types: spinlock and
raw_spinlock. On a non-RT kernel spinlock maps to raw_spinlock, but
that's an implementation detail.

>> #1 is the right thing to do because no driver should rely on actually
>> disabling interrupts on the CPU. If there is a driver which does that,
>> then it's not compatible with RT and should use a local lock instead.
>
> FWIW too - that seems reasonable. The reason I still see benefit in with
> with_irqs_disabled_on_nort though is that this feels a bit closer to some of
> the goals of the C API to me. We have spin_lock_irqsave and spin_lock, with
> the intention that on non-RT kernels IRQs should only need to be disabled a
> single time even if multiple spinlocks are acquired within the scope of a
> single function. I'd like to ensure we can still do that on rust since it's
> possible to do.

Sure. That's not the problem. The problem is:

      local_irq_save();
      spin_lock();

instead of

      spin_lock_irqsave();

The latter allows RT kernels to substitute spin_lock_irqsave() with:

      rt_spin_lock();

which maps to a rtmutex variant and does neither disable interrupts nor
preemption. It only disables migration to guarantee that the task stays
on the CPU, which in turn is a prerequisite for protecting per CPU data
with the lock.

The former does not work on RT because then the rtmutex is acquired with
interrupts disabled, which is a nono because the acquire can sleep.

There is another problem with this split. The example in your spinlock
patch is exactly what we don't want:

> +/// // Accessing an `Example` from a context where IRQs may not be disabled already.
> +/// let b = with_irqs_disabled(|irq| {
> +///     noirq_work(&e, irq);
> +///     e.d.lock_with(irq).b
> +/// });

Why? 

This pattern is in 99% of the cases wrong to begin with independent of
RT because noirq_work() can only be safe if it operates strictly on per
CPU data. If it accesses any shared resource including hardware it's
broken on SMP.

Outside of a very narrow part of core code which uses raw spinlocks,
there is absolutely zero reason for such a construct. We've educated
driver writers to avoid this pattern and now Rust tries to reintroduce
it.

Please do not encourage people to do the wrong thing.

I completely understand and agree with the goal of taking advantage of
Rust's safety, but not for the price of misguiding people.

So you want to make this work:

   spin_lock_irqsave(A);
   spin_lock(B);

and let the compiler validate that the nested spin_lock() is invoked in
a context which has interrupts disabled, right?

To do that you split the spin_lock_irqsave() into

   local_irq_save();     #1
     spin_lock(A);       #2
       spin_lock(B);     #3
       spin_unlock(B);
     spin_unlock(A);
   local_irq_restore();

That makes sense as it gives you three distinct guard contexts, but the
outermost guard context (interrupt disable) is an illusion in most cases
as it does not provide a guard for anything. It merely provides the
prerequisite for locking lock A.

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.

Thanks,

        tglx



  parent reply	other threads:[~2024-10-07 12:01 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 [this message]
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             ` [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=87a5fgunoc.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).