mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* Re: [PATCH bpf-next v1 1/4] bpf: Add call for selecting kmalloc or kmalloc_nolock at call time
       [not found]   ` <DN38HGVL8JHQ.2ZVZQCKFXVA9T@gmail.com>
@ 2026-11-24 13:59     ` Harry Yoo
  2026-11-24 18:07       ` Emil Tsalapatis
  0 siblings, 1 reply; 2+ messages in thread
From: Harry Yoo @ 2026-11-24 13:59 UTC (permalink / raw)
  To: Alexei Starovoitov
  Cc: Emil Tsalapatis, bpf, andrii, eddyz87, memxor, daniel,
	Sebastian Andrzej Siewior, Clark Williams, Steven Rostedt,
	linux-rt-devel, Vlastimil Babka, Hao Li, Andrew Morton,
	Suren Baghdasaryan, Michal Hocko, Brendan Jackman,
	Johannes Weiner, Zi Yan, linux-mm, David Hildenbrand,
	Peter Zijlstra, Ingo Molnar, Will Deacon, Boqun Feng,
	Waiman Long, linux-kernel

[+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.

>  /*
>   * 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

-- 
Cheers,
Harry / Hyeonggon

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH bpf-next v1 1/4] bpf: Add call for selecting kmalloc or kmalloc_nolock at call time
  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
  0 siblings, 0 replies; 2+ messages in thread
From: Emil Tsalapatis @ 2026-11-24 18:07 UTC (permalink / raw)
  To: Harry Yoo, Alexei Starovoitov
  Cc: Emil Tsalapatis, bpf, andrii, eddyz87, memxor, daniel,
	Sebastian Andrzej Siewior, Clark Williams, Steven Rostedt,
	linux-rt-devel, Vlastimil Babka, Hao Li, Andrew Morton,
	Suren Baghdasaryan, Michal Hocko, Brendan Jackman,
	Johannes Weiner, Zi Yan, linux-mm, David Hildenbrand,
	Peter Zijlstra, Ingo Molnar, Will Deacon, Boqun Feng,
	Waiman Long, linux-kernel

On Tue Nov 24, 2026 at 1:59 PM UTC, 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.

Yes, we considered the page_alloc approach with Harry offlist and I think what
it comes down to is that one approach is the most turnkey for users while
the other has the clearest semantics. I went with the latter because
making the selection straight in the BPF code makes the behavior of the
top-level local storage alloc call ends up easier to understand and
explicit in the BPF subsystem.

>
> 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.
>
>>  /*
>>   * 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


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-24 18:07 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®