From: "Alexei Starovoitov" <alexei.starovoitov@gmail.com>
To: "Harry Yoo" <harry@kernel.org>
Cc: "Emil Tsalapatis" <emil@etsalapatis.com>, <bpf@vger.kernel.org>,
<andrii@kernel.org>, <eddyz87@gmail.com>, <memxor@gmail.com>,
<daniel@iogearbox.net>,
"Sebastian Andrzej Siewior" <bigeasy@linutronix.de>,
"Clark Williams" <clrkwllms@kernel.org>,
"Steven Rostedt" <rostedt@goodmis.org>,
<linux-rt-devel@lists.linux.dev>,
"Vlastimil Babka" <vbabka@kernel.org>,
"Hao Li" <hao.li@linux.dev>,
"Andrew Morton" <akpm@linux-foundation.org>,
"Suren Baghdasaryan" <surenb@google.com>,
"Michal Hocko" <mhocko@suse.com>,
"Brendan Jackman" <brendan.jackman@linux.dev>,
"Johannes Weiner" <hannes@cmpxchg.org>, "Zi Yan" <ziy@nvidia.com>,
<linux-mm@kvack.org>, "David Hildenbrand" <david@kernel.org>,
"Peter Zijlstra" <peterz@infradead.org>,
"Ingo Molnar" <mingo@redhat.com>, "Will Deacon" <will@kernel.org>,
"Boqun Feng" <boqun@kernel.org>,
"Waiman Long" <longman@redhat.com>,
<linux-kernel@vger.kernel.org>
Subject: Re: [PATCH bpf-next v1 1/4] bpf: Add call for selecting kmalloc or kmalloc_nolock at call time
Date: Wed, 25 Nov 2026 04:03:49 +0000 [thread overview]
Message-ID: <DN3ZSA718QIA.11EAQE8I69WJ8@gmail.com> (raw)
In-Reply-To: <awWRZ9xCzWLnj_R6@thinkstation>
On Tue, Nov 24, 2026 at 05:59 AM Harry Yoo <harry@kernel.org> wrote:
>> + /* The caller may hold a raw_spinlock_t. */
>> + lock_map_acquire_try(&zone_nolock_map);
>> + spin_lock_irqsave(&zone->lock, *flags);
>> + lock_map_release(&zone_nolock_map);
>> + return true;
>> +}
>
> I'm not sure this is a better solution unless this could be generalized
> or abstracted further.
Alright. Here is generalized one.
diff --git a/mm/internal.h b/mm/internal.h
index 85cf7be..1de6292 100644
--- a/mm/internal.h
+++ b/mm/internal.h
@@ -1688,6 +1688,44 @@ static inline bool can_spin_trylock(void)
return true;
}
+/*
+ * An IRQ handler can allocate at any point where IRQs are enabled, so a
+ * _nolock() caller can wait there for a lock that is IRQ-safe.
+ */
+static inline bool nolock_can_wait(void)
+{
+ return !IS_ENABLED(CONFIG_PREEMPT_RT) && !irqs_disabled() && !in_nmi();
+}
+
+/* @lock must always be held with IRQs disabled. */
+static inline bool spin_lock_nolock(spinlock_t *lock, unsigned long *flags)
+ __cond_acquires(true, lock)
+{
+ if (!nolock_can_wait())
+ return spin_trylock_irqsave(lock, *flags);
+
+ spin_lock_irqsave(lock, *flags);
+ return true;
+}
+
+/*
+ * The caller may hold a raw_spinlock_t. The map has to cover the critical
+ * sections of spin_lock_nolock() up to the unlock.
+ */
+extern struct lockdep_map nolock_wait_map;
+
+static inline void nolock_wait_begin(void)
+{
+ if (IS_ENABLED(CONFIG_PROVE_RAW_LOCK_NESTING) && nolock_can_wait())
+ lock_map_acquire_try(&nolock_wait_map);
+}
+
+static inline void nolock_wait_end(void)
+{
+ if (IS_ENABLED(CONFIG_PROVE_RAW_LOCK_NESTING) && nolock_can_wait())
+ lock_map_release(&nolock_wait_map);
+}
+
/* char-mem.c */
bool file_is_dev_zero(const struct file *file);
diff --git a/mm/page_alloc.c b/mm/page_alloc.c
index 2fef409..f22ae49 100644
--- a/mm/page_alloc.c
+++ b/mm/page_alloc.c
@@ -2550,7 +2550,7 @@ static int rmqueue_bulk(struct zone *zone, unsigned int order,
int i;
if (unlikely(alloc_flags & ALLOC_NOLOCK)) {
- if (!spin_trylock_irqsave(&zone->lock, flags))
+ if (!spin_lock_nolock(&zone->lock, &flags))
return 0;
} else {
spin_lock_irqsave(&zone->lock, flags);
@@ -3237,7 +3237,7 @@ struct page *rmqueue_buddy(struct zone *preferred_zone, struct zone *zone,
do {
page = NULL;
if (unlikely(alloc_flags & ALLOC_NOLOCK)) {
- if (!spin_trylock_irqsave(&zone->lock, flags))
+ if (!spin_lock_nolock(&zone->lock, &flags))
return NULL;
} else {
spin_lock_irqsave(&zone->lock, flags);
@@ -8115,12 +8115,19 @@ static bool __free_unaccepted(struct page *page)
#endif /* CONFIG_UNACCEPTED_MEMORY */
+DEFINE_WAIT_OVERRIDE_MAP(nolock_wait_map, LD_WAIT_CONFIG);
+
struct page *alloc_frozen_pages_nolock_noprof(gfp_t gfp_flags, int nid, unsigned int order)
{
+ struct page *page;
+
if (nid == NUMA_NO_NODE)
nid = numa_node_id();
- return __alloc_frozen_pages_noprof(gfp_flags, order, nid, NULL, ALLOC_NOLOCK);
+ nolock_wait_begin();
+ page = __alloc_frozen_pages_noprof(gfp_flags, order, nid, NULL, ALLOC_NOLOCK);
+ nolock_wait_end();
+ return page;
}
/**
* alloc_pages_nolock - opportunistic reentrant allocation from any context
diff --git a/mm/slub.c b/mm/slub.c
index 5c33d12..5bd00d1 100644
--- a/mm/slub.c
+++ b/mm/slub.c
@@ -3821,7 +3821,7 @@ static void *alloc_single_from_new_slab(struct kmem_cache *s, struct slab *slab,
n = get_node(s, slab_nid(slab));
if (allow_spin) {
spin_lock_irqsave(&n->list_lock, flags);
- } else if (!spin_trylock_irqsave(&n->list_lock, flags)) {
+ } else if (!spin_lock_nolock(&n->list_lock, &flags)) {
/*
* Unlucky, discard newly allocated slab.
* The slab is not fully free, but it's fine as
@@ -3939,7 +3939,7 @@ static void *get_from_partial_node(struct kmem_cache *s,
if (alloc_flags_allow_spinning(ac->alloc_flags))
spin_lock_irqsave(&n->list_lock, flags);
- else if (!spin_trylock_irqsave(&n->list_lock, flags))
+ else if (!spin_lock_nolock(&n->list_lock, &flags))
return NULL;
list_for_each_entry_safe(slab, slab2, &n->partial, slab_list) {
@@ -4543,7 +4543,7 @@ static unsigned int alloc_from_new_slab(struct kmem_cache *s, struct slab *slab,
if (allow_spin) {
spin_lock_irqsave(&n->list_lock, flags);
- } else if (!spin_trylock_irqsave(&n->list_lock, flags)) {
+ } else if (!spin_lock_nolock(&n->list_lock, &flags)) {
/*
* Unlucky, discard newly allocated slab.
* The slab is not fully free, but it's fine as
@@ -5608,7 +5608,9 @@ retry:
* kfence_alloc. Hence call ___slab_alloc() (at most twice)
* and slab_post_alloc_hook() directly.
*/
+ nolock_wait_begin();
ret = ___slab_alloc(s, gfp_flags, node, ac);
+ nolock_wait_end();
/*
* It's possible we failed due to trylock as we preempted someone with
prev parent reply other threads:[~2026-11-25 4:03 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <20261124060357.11700-1-emil@etsalapatis.com>
[not found] ` <20261124060357.11700-2-emil@etsalapatis.com>
[not found] ` <DN38HGVL8JHQ.2ZVZQCKFXVA9T@gmail.com>
2026-11-24 13:59 ` [PATCH bpf-next v1 1/4] bpf: Add call for selecting kmalloc or kmalloc_nolock at call time Harry Yoo
2026-11-24 18:07 ` Emil Tsalapatis
2026-11-25 4:03 ` Alexei Starovoitov [this message]
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=DN3ZSA718QIA.11EAQE8I69WJ8@gmail.com \
--to=alexei.starovoitov@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=andrii@kernel.org \
--cc=bigeasy@linutronix.de \
--cc=boqun@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=brendan.jackman@linux.dev \
--cc=clrkwllms@kernel.org \
--cc=daniel@iogearbox.net \
--cc=david@kernel.org \
--cc=eddyz87@gmail.com \
--cc=emil@etsalapatis.com \
--cc=hannes@cmpxchg.org \
--cc=hao.li@linux.dev \
--cc=harry@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=linux-rt-devel@lists.linux.dev \
--cc=longman@redhat.com \
--cc=memxor@gmail.com \
--cc=mhocko@suse.com \
--cc=mingo@redhat.com \
--cc=peterz@infradead.org \
--cc=rostedt@goodmis.org \
--cc=surenb@google.com \
--cc=vbabka@kernel.org \
--cc=will@kernel.org \
--cc=ziy@nvidia.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