BPF List
 help / color / mirror / Atom feed
From: "Florent Revest" <florent.revest@linux.dev>
To: "Jiri Olsa" <olsajiri@gmail.com>
Cc: <bpf@vger.kernel.org>, "Alexei Starovoitov" <ast@kernel.org>,
	"Daniel Borkmann" <daniel@iogearbox.net>,
	"Andrii Nakryiko" <andrii@kernel.org>,
	"Martin KaFai Lau" <martin.lau@linux.dev>,
	"Eduard Zingerman" <eddyz87@gmail.com>,
	"Kumar Kartikeya Dwivedi" <memxor@gmail.com>,
	"Song Liu" <song@kernel.org>,
	"Yonghong Song" <yonghong.song@linux.dev>,
	"KP Singh" <kpsingh@kernel.org>,
	"Emil Tsalapatis" <emil@etsalapatis.com>,
	"John Fastabend" <john.fastabend@gmail.com>,
	"Paul E. McKenney" <paulmck@kernel.org>,
	"Jose Fernandez" <jose.fernandez@linux.dev>,
	<linux-kernel@vger.kernel.org>
Subject: Re: [PATCH bpf] bpf: Keep progs alive until the trampoline image calling them is freed
Date: Mon, 31 Aug 2026 20:43:39 +0000	[thread overview]
Message-ID: <DL3F6YTBCK35.3I96GFRVMKE2P@linux.dev> (raw)
In-Reply-To: <apWuYPCVCvBeaCKr@krava>

On Mon Aug 31, 2026 at 4:40 PM UTC, Jiri Olsa wrote:
> On Wed, Aug 19, 2026 at 12:22:50PM +0000, Florent Revest (Anthropic) wrote:
> > bpf_tramp_image_put() makes sure a trampoline image is not freed while
> > a task may still be running in it (call_rcu_tasks() + im->pcref), but
> > nothing similar is done for the progs called by that image. Since
> > commit e21aa341785c ("bpf: Fix fexit trampoline."), detach patches the
> > return path so that a task still in the original function skips the
> > fexit progs when it comes back, and counts on the prog's own RCU flavor
> > to cover a task that is inside a prog. On that basis the last prog
> > reference is dropped right away and the prog is freed after a single
> > RCU / RCU tasks trace grace period.
> > 
> > That leaves out a task in the trampoline glue itself: between two
> > progs, or already past the patched jump but not yet in the first fexit
> > prog's enter helper. On !PREEMPT kernels this is a few instructions
> > that cannot be preempted, so it did not matter. With CONFIG_PREEMPTION
> > a task can sit there, in no RCU read section of any flavor and holding
> > only im->pcref, for longer than it takes to free the prog it is about
> > to call:
> > 
> >   CPU 0                               CPU 1
> >   in image I, orig_call() returned
> >   [preempted before lsm.s prog A]
> >                                       bpf_tracing_link_release()
> >                                        -> bpf_tramp_image_put(I)
> >                                       bpf_link_dealloc()
> >                                        bpf_prog_put(A), last ref
> >                                       tasks trace GP, A's text freed
> >   __bpf_prog_enter_sleepable(A)
> >   call A->bpf_func
>
> hi,
> do you have a selftest or another reproducer for this?

I originally only reproduced that with a stress test (an attach/detach
loop during an exec storm).

But actually, preemption isn't needed! If a task sleeps in a sleepable
prog and the prog that runs after it in the same image gets detached,
it calls the freed prog when it wakes up. That case could be made
deterministic with userfaultfd so I could write a selftest for it.
I'll include it in v2. :)

      reply	other threads:[~2026-08-31 20:43 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-19 12:22 [PATCH bpf] bpf: Keep progs alive until the trampoline image calling them is freed Florent Revest (Anthropic)
2026-08-20 15:26 ` Junseo Lim
     [not found]   ` <bc0edb11e07e0f6147e9c552805d0029c7aec7fc@linux.dev>
2026-08-21  7:58     ` Junseo Lim
2026-08-31 21:07       ` Florent Revest
2026-08-20 16:19 ` Leon Hwang
2026-08-30 10:40 ` Kumar Kartikeya Dwivedi
2026-08-30 13:21   ` Alexei Starovoitov
2026-08-31  2:43     ` Kumar Kartikeya Dwivedi
2026-08-31 20:58       ` Florent Revest
2026-09-02  5:57         ` Alexei Starovoitov
2026-09-02 10:15           ` Florent Revest
2026-09-02 22:06             ` Alexei Starovoitov
2026-09-02 23:25               ` Florent Revest
2026-09-03  0:48                 ` Alexei Starovoitov
2026-08-31 16:40 ` Jiri Olsa
2026-08-31 20:43   ` Florent Revest [this message]

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=DL3F6YTBCK35.3I96GFRVMKE2P@linux.dev \
    --to=florent.revest@linux.dev \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=eddyz87@gmail.com \
    --cc=emil@etsalapatis.com \
    --cc=john.fastabend@gmail.com \
    --cc=jose.fernandez@linux.dev \
    --cc=kpsingh@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=martin.lau@linux.dev \
    --cc=memxor@gmail.com \
    --cc=olsajiri@gmail.com \
    --cc=paulmck@kernel.org \
    --cc=song@kernel.org \
    --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