From: Peter Zijlstra <peterz@infradead.org>
To: Harry Yoo <harry@kernel.org>
Cc: Alexei Starovoitov <alexei.starovoitov@gmail.com>,
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>,
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 15:09:02 +0100 [thread overview]
Message-ID: <20261125140902.GE2668804@noisy.programming.kicks-ass.net> (raw)
In-Reply-To: <awWRZ9xCzWLnj_R6@thinkstation>
On Tue, Nov 24, 2026 at 01:59:31PM +0000, Harry Yoo wrote:
> [+Cc Relevant folks]
>
> And a bit of context on the underlying problem.
>
> kmalloc_nolock() could fail when there is a contention on locks
> shared across CPUs (e.g., n->list_lock in slab, zone->lock in buddy),
> or there is a decent amount of memory pressure. It's more restrictive
> than GFP_ATOMIC.
>
> Unconditionally using kmalloc_nolock() in BPF local storage is
> unnecessarily restrictive when the program can sleep or spin, and
> Emil's patchset teaches the local storage whether the BPF program
> accessing the local storage could sleep or spin on a lock.
>
> Answering the question "Does this context allow sleeping, or spinning
> on a lock" is extremely tricky in the Linux kernel as BPF programs
> could run and call kfuncs almost at any point in the kernel code and
> the "context" is usually encoded in the GFP flags or the process flags.
>
> Tracking whether certain locks are taken by current context is not
> ideal either as it adds unnecessary overhead for everybody.
>
> On Tue, Nov 24, 2026 at 06:40:11AM +0000, Alexei Starovoitov wrote:
> > 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;
> > +}
>
> I'm not sure this is a better solution unless this could be generalized
> or abstracted further.
This is absolute atrocious shite. You cannot attempt a spin_trylock from
NMI context. That thing might be a mutex.
This should not work; if it does, that is an accident we need to fix.
next prev parent reply other threads:[~2026-11-25 14:09 UTC|newest]
Thread overview: 5+ 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
2026-11-25 14:09 ` Peter Zijlstra [this message]
2026-11-25 14:30 ` Harry Yoo
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=20261125140902.GE2668804@noisy.programming.kicks-ass.net \
--to=peterz@infradead.org \
--cc=akpm@linux-foundation.org \
--cc=alexei.starovoitov@gmail.com \
--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=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