BPF List
 help / color / mirror / Atom feed
From: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
To: Sahil Chandna <chandna.sahil@gmail.com>
Cc: yonghong.song@linux.dev, 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.com
Subject: Re: [PATCH bpf-next v2] bpf: use preempt_disable/enable() to protect bpf_bprintf_buffers nesting
Date: Wed, 12 Nov 2025 09:23:35 +0100	[thread overview]
Message-ID: <20251112082335.cCk-pD9W@linutronix.de> (raw)
In-Reply-To: <20251111170628.410641-1-chandna.sahil@gmail.com>

On 2025-11-11 22:36:28 [+0530], Sahil Chandna wrote:
> The bpf_bprintf_prepare() and related helpers (bpf_try_get_buffers() /
> bpf_put_buffers()) rely on a per-CPU counter bpf_bprintf_nest_level to
> manage nested buffer usage. However, when invoked from different contexts
> (process, softirq, NMI), the nesting counter can become inconsistent if
> task  migration occurs between CPUs during these operations. This can
> result in warnings such as:

Not to be a nitpick but different contexts as describe above is fine. If
a preemptible task is preempted by softirq followed by NMI nothing bad
will happen, it will work as intended. That preempt_disable() has no
affect on not getting preempted by a softirq, hardirq or NMI.

The problem is that a task can be preempted by another task. The BPF
program is limited to a single CPU (so counter will always be
incremented on the same CPU) but you can still have multiple tasks
on the same CPU asking/ returning a buffer.
The first task is using slot 0, the second one slot 1. Still works (by
some definition of works since 1 can return the buffer before 2 does and
ask for a new one which will be slot 2 which is still in use).
However if you add two additional tasks to the mix then you will exceed
the available slots which leads to the warning below.

> WARNING: CPU: 1 PID: 6145 at kernel/bpf/helpers.c:781 bpf_try_get_buffers kernel/bpf/helpers.c:781 [inline]
> WARNING: CPU: 1 PID: 6145 at kernel/bpf/helpers.c:781 bpf_bprintf_prepare+0x12cf/0x13a0 kernel/bpf/helpers.c:834
> 
> Having only migrate_disable is insufficient here to prevent nesting,
> hence add preempt_disable()/enable() around buffer acquisition and release.

I prepared to following while adding the local locks which we agreed not
do:

    bpf_try_get_buffers() returns one of multiple per-CPU buffers. The
    "nesting" level is counted based on invocation to decide which buffer to
    return. This relies on disabled preemption in order to not get preempted
    while a buffer is returned and to have the buffers returned in the
    same order as they were requested. Without disabled preemption,
    multiple tasks can be preempted while a buffer is acquired,
    eventually exceeding the limit (MAX_BPRINTF_NEST_LEVEL) and
    triggering the warning.

    Multiple buffer can be acquired by the same context due users in
    different execution context (task and IRQ). It can also happen for a
    single context if after a buffer has been returned and another
    buffer needs to be returned due to an event (such as invoking
    spin_lock()).


you can take it or leave it. Here is a
Reviewed-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de>

since the change itself is okay and does not lead to any problems as far
as I am aware. I just think the description is not accurate and does not
describe the problem the syzkaller reproducer triggered. I leave to the
maintainer here…

Sebastian

  parent reply	other threads:[~2025-11-12  8:23 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-11-11 17:06 [PATCH bpf-next v2] bpf: use preempt_disable/enable() to protect bpf_bprintf_buffers nesting Sahil Chandna
2025-11-11 20:22 ` Yonghong Song
2025-11-12  8:23 ` Sebastian Andrzej Siewior [this message]
2025-11-14 21:10 ` patchwork-bot+netdevbpf

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=20251112082335.cCk-pD9W@linutronix.de \
    --to=bigeasy@linutronix.de \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=chandna.sahil@gmail.com \
    --cc=daniel@iogearbox.net \
    --cc=eddyz87@gmail.com \
    --cc=haoluo@google.com \
    --cc=john.fastabend@gmail.com \
    --cc=jolsa@kernel.org \
    --cc=kpsingh@kernel.org \
    --cc=martin.lau@linux.dev \
    --cc=sdf@fomichev.me \
    --cc=song@kernel.org \
    --cc=syzbot+b0cff308140f79a9c4cb@syzkaller.appspotmail.com \
    --cc=yonghong.song@linux.dev \
    /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