From: "Alexei Starovoitov" <alexei.starovoitov@gmail.com>
To: "Emil Tsalapatis" <emil@etsalapatis.com>, <bpf@vger.kernel.org>
Cc: <andrii@kernel.org>, <eddyz87@gmail.com>, <memxor@gmail.com>,
<daniel@iogearbox.net>
Subject: Re: [PATCH bpf-next v1 1/4] bpf: Add call for selecting kmalloc or kmalloc_nolock at call time
Date: Tue, 24 Nov 2026 06:40:11 +0000 [thread overview]
Message-ID: <DN38HGVL8JHQ.2ZVZQCKFXVA9T@gmail.com> (raw)
In-Reply-To: <20261124060357.11700-2-emil@etsalapatis.com>
On Mon, Nov 23, 2026 at 10:03 PM Emil Tsalapatis <emil@etsalapatis.com> wrote:
> + /* Only try the spinning allocation when preemption disables are tracked. */
> + if (IS_ENABLED(CONFIG_PREEMPT_COUNT) && can_spin_in_alloc() &&
> + size <= KMALLOC_MAX_CACHE_SIZE && !(flags & ~nolock_flags))
> + ptr = kmalloc(size, flags | GFP_NOWAIT | __GFP_NOMEMALLOC);
It works, but I after a bit of brainstorming the following
might be a better fix, since it will address the root cause:
Pls run it by Vlastimil, Harry, Sebastian.
diff --git a/mm/page_alloc.c b/mm/page_alloc.c
index 2fef409..4368ab5 100644
--- a/mm/page_alloc.c
+++ b/mm/page_alloc.c
@@ -2536,6 +2536,24 @@ __rmqueue(struct zone *zone, unsigned int order, int migratetype,
return NULL;
}
+static DEFINE_WAIT_OVERRIDE_MAP(zone_nolock_map, LD_WAIT_CONFIG);
+
+/*
+ * zone->lock is always held with IRQs disabled. A caller that runs with IRQs
+ * enabled is not nested inside of it, so it can wait for the owner.
+ */
+static bool zone_lock_nolock(struct zone *zone, unsigned long *flags)
+{
+ if (IS_ENABLED(CONFIG_PREEMPT_RT) || irqs_disabled() || in_nmi())
+ return spin_trylock_irqsave(&zone->lock, *flags);
+
+ /* 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;
+}
+
/*
* Obtain a specified number of elements from the buddy allocator, all under
* a single hold of the lock, for efficiency. Add them to the supplied list.
@@ -2550,7 +2568,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 (!zone_lock_nolock(zone, &flags))
return 0;
} else {
spin_lock_irqsave(&zone->lock, flags);
@@ -3237,7 +3255,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 (!zone_lock_nolock(zone, &flags))
return NULL;
} else {
spin_lock_irqsave(&zone->lock, flags);
pw-bot: cr
next prev parent reply other threads:[~2026-11-24 6:40 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-24 6:03 [PATCH bpf-next v1 0/4] bpf: Use kmalloc for local storage if possible Emil Tsalapatis
2026-11-24 6:03 ` [PATCH bpf-next v1 1/4] bpf: Add call for selecting kmalloc or kmalloc_nolock at call time Emil Tsalapatis
2026-11-24 6:21 ` sashiko-bot
2026-11-24 6:40 ` Alexei Starovoitov [this message]
2026-11-24 6:40 ` bot+bpf-ci
2026-11-24 6:03 ` [PATCH bpf-next v1 2/4] bpf: Plumb local storage context Emil Tsalapatis
2026-11-24 6:17 ` sashiko-bot
2026-11-24 6:03 ` [PATCH bpf-next v1 3/4] bpf: Add sleepable local storage Emil Tsalapatis
2026-11-24 6:40 ` bot+bpf-ci
2026-11-24 6:41 ` sashiko-bot
2026-11-24 6:03 ` [PATCH bpf-next v1 4/4] selftests/bpf: Test kmalloc variant selection for task storage Emil Tsalapatis
2026-11-24 6:40 ` bot+bpf-ci
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=DN38HGVL8JHQ.2ZVZQCKFXVA9T@gmail.com \
--to=alexei.starovoitov@gmail.com \
--cc=andrii@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=eddyz87@gmail.com \
--cc=emil@etsalapatis.com \
--cc=memxor@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