All of lore.kernel.org
 help / color / mirror / Atom feed
From: Yonghong Song <yonghong.song@linux.dev>
To: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
Cc: bot+bpf-ci@kernel.org, chandna.sahil@gmail.com, ast@kernel.org,
	daniel@iogearbox.net, andrii@kernel.org, martin.lau@linux.dev,
	eddyz87@gmail.com, song@kernel.org, john.fastabend@gmail.com,
	kpsingh@kernel.org, sdf@fomichev.me, haoluo@google.com,
	jolsa@kernel.org, bpf@vger.kernel.org,
	syzbot+b0cff308140f79a9c4cb@syzkaller.appspotmail.comi,
	martin.lau@kernel.org, clm@meta.com, ihor.solodrai@linux.dev
Subject: Re: [PATCH bpf-next] bpf: use preempt_disable/enable() to protect bpf_bprintf_buffers nesting
Date: Mon, 10 Nov 2025 09:44:55 -0800	[thread overview]
Message-ID: <03bc2787-b5e7-42e7-9812-8c50da912c0b@linux.dev> (raw)
In-Reply-To: <20251110132546.eE4o18h6@linutronix.de>



On 11/10/25 5:25 AM, Sebastian Andrzej Siewior wrote:
> On 2025-11-09 11:44:48 [-0800], Yonghong Song wrote:
>
> Could we do this instead?
> There is  __bpf_stream_push_str() => bpf_stream_page_reserve_elem() =>
> bpf_stream_page_replace() => alloc_pages_nolock().

I would suggest to stick to preempt_disable/enable().
In the bpf-next (newer change), for function
bpf_stream_elem_alloc(), kmalloc_nolock() is used
and no local_lock usage any more.


>
> diff --git a/include/linux/sched.h b/include/linux/sched.h
> index b469878de25c8..5a4965724c374 100644
> --- a/include/linux/sched.h
> +++ b/include/linux/sched.h
> @@ -1598,6 +1598,7 @@ struct task_struct {
>   	void				*security;
>   #endif
>   #ifdef CONFIG_BPF_SYSCALL
> +	s8				bpf_bprintf_idx;
>   	/* Used by BPF task local storage */
>   	struct bpf_local_storage __rcu	*bpf_storage;
>   	/* Used for BPF run context */
> diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c
> index eb25e70e0bdc0..62e37c845ec5a 100644
> --- a/kernel/bpf/helpers.c
> +++ b/kernel/bpf/helpers.c
> @@ -770,28 +770,39 @@ static int bpf_trace_copy_string(char *buf, void *unsafe_ptr, char fmt_ptype,
>   /* Support executing three nested bprintf helper calls on a given CPU */
>   #define MAX_BPRINTF_NEST_LEVEL	3
>   
> -static DEFINE_PER_CPU(struct bpf_bprintf_buffers[MAX_BPRINTF_NEST_LEVEL], bpf_bprintf_bufs);
> -static DEFINE_PER_CPU(int, bpf_bprintf_nest_level);
> +struct bpf_cpu_buffer {
> +	struct bpf_bprintf_buffers bufs[MAX_BPRINTF_NEST_LEVEL];
> +	local_lock_t	lock[MAX_BPRINTF_NEST_LEVEL];
> +};
> +
> +static DEFINE_PER_CPU(struct bpf_cpu_buffer, bpf_cpu_bprintf) = {
> +	.lock = { [0 ... MAX_BPRINTF_NEST_LEVEL - 1] = INIT_LOCAL_LOCK(bpf_cpu_bprintf.lock) },
> +};
>   
>   int bpf_try_get_buffers(struct bpf_bprintf_buffers **bufs)
>   {
> -	int nest_level;
> +	s8 nest_level;
>   
> -	nest_level = this_cpu_inc_return(bpf_bprintf_nest_level);
> -	if (WARN_ON_ONCE(nest_level > MAX_BPRINTF_NEST_LEVEL)) {
> -		this_cpu_dec(bpf_bprintf_nest_level);
> +	nest_level = current->bpf_bprintf_idx++;
> +	if (WARN_ON_ONCE(nest_level >= MAX_BPRINTF_NEST_LEVEL)) {
> +		current->bpf_bprintf_idx--;
>   		return -EBUSY;
>   	}
> -	*bufs = this_cpu_ptr(&bpf_bprintf_bufs[nest_level - 1]);
>   
> +	local_lock(&bpf_cpu_bprintf.lock[nest_level]);
> +	*bufs = this_cpu_ptr(&bpf_cpu_bprintf.bufs[nest_level]);
>   	return 0;
>   }
>   
>   void bpf_put_buffers(void)
>   {
> -	if (WARN_ON_ONCE(this_cpu_read(bpf_bprintf_nest_level) == 0))
> +	s8 nest_level;
> +
> +	nest_level = current->bpf_bprintf_idx;
> +	if (WARN_ON_ONCE(nest_level == 0))
>   		return;
> -	this_cpu_dec(bpf_bprintf_nest_level);
> +	local_unlock(&bpf_cpu_bprintf.lock[nest_level - 1]);
> +	current->bpf_bprintf_idx--;
>   }
>   
>   void bpf_bprintf_cleanup(struct bpf_bprintf_data *data)


  reply	other threads:[~2025-11-10 17:45 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-11-09 17:36 [PATCH bpf-next] bpf: use preempt_disable/enable() to protect bpf_bprintf_buffers nesting Sahil Chandna
2025-11-09 18:07 ` bot+bpf-ci
2025-11-09 19:44   ` Yonghong Song
2025-11-10 13:25     ` Sebastian Andrzej Siewior
2025-11-10 17:44       ` Yonghong Song [this message]
2025-11-11 10:37         ` Sebastian Andrzej Siewior

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=03bc2787-b5e7-42e7-9812-8c50da912c0b@linux.dev \
    --to=yonghong.song@linux.dev \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bigeasy@linutronix.de \
    --cc=bot+bpf-ci@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=chandna.sahil@gmail.com \
    --cc=clm@meta.com \
    --cc=daniel@iogearbox.net \
    --cc=eddyz87@gmail.com \
    --cc=haoluo@google.com \
    --cc=ihor.solodrai@linux.dev \
    --cc=john.fastabend@gmail.com \
    --cc=jolsa@kernel.org \
    --cc=kpsingh@kernel.org \
    --cc=martin.lau@kernel.org \
    --cc=martin.lau@linux.dev \
    --cc=sdf@fomichev.me \
    --cc=song@kernel.org \
    --cc=syzbot+b0cff308140f79a9c4cb@syzkaller.appspotmail.comi \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.