From: Vlastimil Babka <vbabka@suse.cz>
To: Alexei Starovoitov <alexei.starovoitov@gmail.com>, bpf@vger.kernel.org
Cc: andrii@kernel.org, memxor@gmail.com, akpm@linux-foundation.org,
peterz@infradead.org, bigeasy@linutronix.de, rostedt@goodmis.org,
houtao1@huawei.com, hannes@cmpxchg.org, shakeel.butt@linux.dev,
mhocko@suse.com, willy@infradead.org, tglx@linutronix.de,
jannh@google.com, tj@kernel.org, linux-mm@kvack.org,
kernel-team@fb.com
Subject: Re: [PATCH bpf-next v8 3/6] locking/local_lock: Introduce localtry_lock_t
Date: Thu, 13 Feb 2025 16:03:56 +0100 [thread overview]
Message-ID: <1fda7391-228d-4e10-8449-189be36eb27c@suse.cz> (raw)
In-Reply-To: <20250213033556.9534-4-alexei.starovoitov@gmail.com>
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?
> + } else { \
> + migrate_disable(); \
> + __locked = spin_trylock(this_cpu_ptr((lock))); \
> + if (!__locked) \
> + migrate_enable(); \
> + } \
> + __locked; \
> + })
> +
> #endif /* CONFIG_PREEMPT_RT */
next prev parent reply other threads:[~2025-02-13 15:04 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 [this message]
2025-02-13 15:23 ` Alexei Starovoitov
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=1fda7391-228d-4e10-8449-189be36eb27c@suse.cz \
--to=vbabka@suse.cz \
--cc=akpm@linux-foundation.org \
--cc=alexei.starovoitov@gmail.com \
--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=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