Re: [PATCH bpf-next v1 1/4] bpf: Add call for selecting kmalloc or kmalloc_nolock at call time
From: Harry Yoo
Date: Wed Nov 25 2026 - 09:31:05 EST
On Wed, Nov 25, 2026 at 03:09:02PM +0100, Peter Zijlstra wrote:
> On Tue, Nov 24, 2026 at 01:59:31PM +0000, Harry Yoo wrote:
> > [+Cc Relevant folks]
> > 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@xxxxxxxxxxxxxxx> 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.
Uh, right. We went through this when kmalloc_nolock() was introduced.
> This should not work; if it does, that is an accident we need to fix.
Current implementation handles this by can_spin_trylock() in
mm/internal.h.
One remaining accident we're aware is that we cannot attempt a
spin_trylock or local_trylock under pi_lock (even on !RT).
Currently I don't have a good idea to fix this :/
--
Cheers,
Harry / Hyeonggon