linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Alexei Starovoitov <alexei.starovoitov@gmail.com>
To: Vlastimil Babka <vbabka@suse.cz>
Cc: bpf <bpf@vger.kernel.org>, Andrii Nakryiko <andrii@kernel.org>,
	 Kumar Kartikeya Dwivedi <memxor@gmail.com>,
	Andrew Morton <akpm@linux-foundation.org>,
	 Peter Zijlstra <peterz@infradead.org>,
	Sebastian Sewior <bigeasy@linutronix.de>,
	 Steven Rostedt <rostedt@goodmis.org>,
	Hou Tao <houtao1@huawei.com>,
	 Johannes Weiner <hannes@cmpxchg.org>,
	Shakeel Butt <shakeel.butt@linux.dev>,
	 Michal Hocko <mhocko@suse.com>,
	Matthew Wilcox <willy@infradead.org>,
	 Thomas Gleixner <tglx@linutronix.de>,
	Jann Horn <jannh@google.com>, Tejun Heo <tj@kernel.org>,
	 linux-mm <linux-mm@kvack.org>, Kernel Team <kernel-team@fb.com>
Subject: Re: [PATCH bpf-next v8 3/6] locking/local_lock: Introduce localtry_lock_t
Date: Thu, 13 Feb 2025 07:23:01 -0800	[thread overview]
Message-ID: <CAADnVQLHRb8fu9J6Yd63ZDBtJFzZN1oWfwSDA_QXFqzXyr9F5g@mail.gmail.com> (raw)
In-Reply-To: <1fda7391-228d-4e10-8449-189be36eb27c@suse.cz>

On Thu, Feb 13, 2025 at 7:04 AM Vlastimil Babka <vbabka@suse.cz> wrote:
>
> On 2/13/25 04:35, Alexei Starovoitov wrote:
> > From: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
> >
> > In !PREEMPT_RT local_lock_irqsave() disables interrupts to protect
> > critical section, but it doesn't prevent NMI, so the fully reentrant
> > code cannot use local_lock_irqsave() for exclusive access.
> >
> > Introduce localtry_lock_t and localtry_lock_irqsave() that
> > disables interrupts and sets acquired=1, so localtry_lock_irqsave()
> > from NMI attempting to acquire the same lock will return false.
> >
> > In PREEMPT_RT local_lock_irqsave() maps to preemptible spin_lock().
> > Map localtry_lock_irqsave() to preemptible spin_trylock().
> > When in hard IRQ or NMI return false right away, since
> > spin_trylock() is not safe due to PI issues.
> >
> > Note there is no need to use local_inc for acquired variable,
> > since it's a percpu variable with strict nesting scopes.
> >
> > Signed-off-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
> > Signed-off-by: Alexei Starovoitov <ast@kernel.org>
> > ---
> >  include/linux/local_lock.h          |  59 +++++++++++++
> >  include/linux/local_lock_internal.h | 123 ++++++++++++++++++++++++++++
> >  2 files changed, 182 insertions(+)
> >
> > diff --git a/include/linux/local_lock.h b/include/linux/local_lock.h
> > index 091dc0b6bdfb..05c254a5d7d3 100644
> > --- a/include/linux/local_lock.h
> > +++ b/include/linux/local_lock.h
> > @@ -51,6 +51,65 @@
> >  #define local_unlock_irqrestore(lock, flags)                 \
> >       __local_unlock_irqrestore(lock, flags)
> >
> > +/**
> > + * localtry_lock_init - Runtime initialize a lock instance
> > + */
> > +#define localtry_lock_init(lock)             __localtry_lock_init(lock)
> > +
> > +/**
> > + * localtry_lock - Acquire a per CPU local lock
> > + * @lock:    The lock variable
> > + */
> > +#define localtry_lock(lock)          __localtry_lock(lock)
> > +
> > +/**
> > + * localtry_lock_irq - Acquire a per CPU local lock and disable interrupts
> > + * @lock:    The lock variable
> > + */
> > +#define localtry_lock_irq(lock)              __localtry_lock_irq(lock)
> > +
> > +/**
> > + * localtry_lock_irqsave - Acquire a per CPU local lock, save and disable
> > + *                    interrupts
> > + * @lock:    The lock variable
> > + * @flags:   Storage for interrupt flags
> > + */
> > +#define localtry_lock_irqsave(lock, flags)                           \
> > +     __localtry_lock_irqsave(lock, flags)
> > +
> > +/**
> > + * localtry_trylock_irqsave - Try to acquire a per CPU local lock, save and disable
> > + *                         interrupts if acquired
> > + * @lock:    The lock variable
> > + * @flags:   Storage for interrupt flags
> > + *
> > + * The function can be used in any context such as NMI or HARDIRQ. Due to
> > + * locking constrains it will _always_ fail to acquire the lock on PREEMPT_RT.
>
> The "always fail" applies only to the NMI and HARDIRQ contexts, right? It's
> not entirely obvious so it sounds worse than it is.
>
> > +
> > +#define __localtry_trylock_irqsave(lock, flags)                      \
> > +     ({                                                      \
> > +             int __locked;                                   \
> > +                                                             \
> > +             typecheck(unsigned long, flags);                \
> > +             flags = 0;                                      \
> > +             if (in_nmi() | in_hardirq()) {                  \
> > +                     __locked = 0;                           \
>
> Because of this, IIUC?

Right.
It's part of commit log:
+ In PREEMPT_RT local_lock_irqsave() maps to preemptible spin_lock().
+ Map localtry_lock_irqsave() to preemptible spin_trylock().
+ When in hard IRQ or NMI return false right away, since
+ spin_trylock() is not safe due to PI issues.

Steven explained it in detail in some earlier thread.

realtime is hard. bpf and realtime together are even harder.
Things got much better over the years, but plenty of work ahead.
I can go in detail, but offtopic for this thread.


  reply	other threads:[~2025-02-13 15:23 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-02-13  3:35 [PATCH bpf-next v8 0/6] bpf, mm: Introduce try_alloc_pages() Alexei Starovoitov
2025-02-13  3:35 ` [PATCH bpf-next v8 1/6] mm, bpf: Introduce try_alloc_pages() for opportunistic page allocation Alexei Starovoitov
2025-02-13  3:35 ` [PATCH bpf-next v8 2/6] mm, bpf: Introduce free_pages_nolock() Alexei Starovoitov
2025-02-13  3:35 ` [PATCH bpf-next v8 3/6] locking/local_lock: Introduce localtry_lock_t Alexei Starovoitov
2025-02-13 15:03   ` Vlastimil Babka
2025-02-13 15:23     ` Alexei Starovoitov [this message]
2025-02-13 15:28       ` Steven Rostedt
2025-02-14 12:15       ` Vlastimil Babka
2025-02-14 12:11   ` Vlastimil Babka
2025-02-14 18:32     ` Alexei Starovoitov
2025-02-14 18:48       ` Vlastimil Babka
2025-02-17 15:17         ` Sebastian Sewior
2025-02-18 15:17   ` Vlastimil Babka
2025-02-13  3:35 ` [PATCH bpf-next v8 4/6] memcg: Use trylock to access memcg stock_lock Alexei Starovoitov
2025-02-13  3:35 ` [PATCH bpf-next v8 5/6] mm, bpf: Use memcg in try_alloc_pages() Alexei Starovoitov
2025-02-13  3:35 ` [PATCH bpf-next v8 6/6] bpf: Use try_alloc_pages() to allocate pages for bpf needs Alexei Starovoitov
2025-02-18 15:36   ` Vlastimil Babka
2025-02-19  2:38     ` Alexei Starovoitov

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=CAADnVQLHRb8fu9J6Yd63ZDBtJFzZN1oWfwSDA_QXFqzXyr9F5g@mail.gmail.com \
    --to=alexei.starovoitov@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=andrii@kernel.org \
    --cc=bigeasy@linutronix.de \
    --cc=bpf@vger.kernel.org \
    --cc=hannes@cmpxchg.org \
    --cc=houtao1@huawei.com \
    --cc=jannh@google.com \
    --cc=kernel-team@fb.com \
    --cc=linux-mm@kvack.org \
    --cc=memxor@gmail.com \
    --cc=mhocko@suse.com \
    --cc=peterz@infradead.org \
    --cc=rostedt@goodmis.org \
    --cc=shakeel.butt@linux.dev \
    --cc=tglx@linutronix.de \
    --cc=tj@kernel.org \
    --cc=vbabka@suse.cz \
    --cc=willy@infradead.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