From: "Florent Revest" <florent.revest@linux.dev>
To: <bot+bpf-ci@kernel.org>, <bpf@vger.kernel.org>, <ast@kernel.org>,
<daniel@iogearbox.net>, <andrii@kernel.org>
Cc: <martin.lau@linux.dev>, <eddyz87@gmail.com>, <memxor@gmail.com>,
<song@kernel.org>, <yonghong.song@linux.dev>, <jolsa@kernel.org>,
<kpsingh@kernel.org>, <john.fastabend@gmail.com>,
<leon.hwang@linux.dev>, <zirajs7@gmail.com>,
<rhkrqnwk98@gmail.com>, <puranjay@kernel.org>,
<xukuohai@huaweicloud.com>, <iii@linux.ibm.com>,
<hbathini@linux.ibm.com>, <chleroy@kernel.org>,
<naveen@kernel.org>, <bjorn@kernel.org>, <pulehui@huawei.com>,
<yangtiezhu@loongson.cn>, <hengqi.chen@gmail.com>,
<linux-kernel@vger.kernel.org>, <martin.lau@kernel.org>,
<eddyz87@gmail.com>, <yonghong.song@linux.dev>,
<mason@kernel.org>, <ihor.solodrai@linux.dev>
Subject: Re: [PATCH bpf v2 1/2] bpf: Skip detached progs in trampoline images that are still in use
Date: Tue, 15 Sep 2026 19:13:15 +0000 [thread overview]
Message-ID: <DLG4NX9R5VTO.3QXM13LGWVURY@linux.dev> (raw)
In-Reply-To: <701db29c4de4f408d9c82041d51586a88ccbd9c19f4dc739253733c4cc5edd7a@mail.kernel.org>
On Sat Sep 12, 2026 at 11:11 AM UTC, wrote:
> > diff --git a/arch/loongarch/net/bpf_jit.c b/arch/loongarch/net/bpf_jit.c
> > index 4da278900938..0e6f1ad36c2d 100644
> > --- a/arch/loongarch/net/bpf_jit.c
> > +++ b/arch/loongarch/net/bpf_jit.c
>
> [ ... ]
>
> > @@ -2178,7 +2189,7 @@ int arch_bpf_trampoline_size(const struct btf_func_model *m, u32 flags,
> > {
> > int ret;
> > struct jit_ctx ctx;
> > - struct bpf_tramp_image im;
> > + struct bpf_tramp_image im = {};
> >
> > ctx.image = NULL;
> > ctx.idx = 0;
>
> Does arch_bpf_trampoline_size() need to initialize ctx.ro_image?
> invoke_bpf_prog() now reads ctx->ro_image unconditionally:
>
> arch/loongarch/net/bpf_jit.c:invoke_bpf_prog()
> void *skip = ctx->ro_image + ctx->idx;
>
> but arch_bpf_trampoline_size() only assigns ctx.image and ctx.idx. Before
> this patch the sizing path read ctx->ro_image only under
> BPF_TRAMP_F_CALL_ORIG, so fentry-only trampolines never touched it. The
> result is discarded (bpf_tramp_image_add_skip() returns immediately
> because im.skips == NULL), so there is no functional misbehaviour, but it
> is a read of an uninitialized automatic variable.
>
> The arm64 side of the same patch already uses a designated initializer:
>
> arch/arm64/net/bpf_jit_comp.c:arch_bpf_trampoline_size()
> struct jit_ctx ctx = { .image = NULL, .idx = 0, };
>
> Making the loongarch declaration 'struct jit_ctx ctx = {};' would avoid
> the issue.
Fair, will address in v3.
> > diff --git a/arch/riscv/net/bpf_jit_comp64.c b/arch/riscv/net/bpf_jit_comp64.c
> > index 151031e97a24..21f8ce2ec89a 100644
> > --- a/arch/riscv/net/bpf_jit_comp64.c
> > +++ b/arch/riscv/net/bpf_jit_comp64.c
> > @@ -904,12 +904,18 @@ static void emit_store_stack_imm64(u8 reg, int stack_off, u64 imm64,
> > emit_sd(RV_REG_FP, stack_off, reg, ctx);
> > }
> >
> > -static int invoke_bpf_prog(struct bpf_tramp_node *node, int args_off, int retval_off,
> > - int run_ctx_off, bool save_ret, struct rv_jit_context *ctx)
> > +static int invoke_bpf_prog(struct bpf_tramp_image *im, struct bpf_tramp_node *node,
> > + int args_off, int retval_off, int run_ctx_off, bool save_ret,
> > + struct rv_jit_context *ctx)
> > {
> > int ret, branch_off;
> > struct bpf_prog *p = node->link->prog;
> > int cookie_off = offsetof(struct bpf_tramp_run_ctx, bpf_cookie);
> > + void *skip = ctx->ro_insns + ctx->ninsns;
> > +
> > + /* 2 nops for auipc+jalr, patched to skip this prog when it is detached */
> > + emit(rv_nop(), ctx);
> > + emit(rv_nop(), ctx);
>
> This isn't a bug, but should BPF_MAX_TRAMP_LINKS be lowered for riscv64?
>
> The commit message states "With the extra nops, BPF_MAX_TRAMP_LINKS progs
> no longer fit in a page on arm64 and loongarch ... so lower the limit
> there like s390 does." The limits in include/linux/bpf.h were changed:
>
> arch added per prog BPF_MAX_TRAMP_LINKS change
> arm64 4 bytes 38 -> 37
> loongarch 20 bytes 38 -> 33
> powerpc64 4 bytes 38 -> 27
> s390x 6 bytes 27 (already lowered)
> riscv64 8 bytes 38 (unchanged)
> x86_64 5 bytes 38 (unchanged)
>
> riscv64 gains 8 bytes per prog (two rv_nop()s) but its limit is
> unchanged. For 38 progs that is 304 extra bytes, which could push
> trampolines close to the PAGE_SIZE limit over the boundary.
> bpf_trampoline_update() rejects the update with -E2BIG if size exceeds
> PAGE_SIZE, so this is a graceful reduction in the maximum attachable
> progs on riscv64 rather than memory corruption.
38 progs still fit on riscv64 with the extra nops so that's ok.
> > diff --git a/kernel/bpf/trampoline.c b/kernel/bpf/trampoline.c
> > index 90b70ea0d370..b82d8592319a 100644
> > --- a/kernel/bpf/trampoline.c
> > +++ b/kernel/bpf/trampoline.c
>
> [ ... ]
>
> > @@ -565,14 +566,21 @@ static void bpf_tramp_image_free(struct bpf_tramp_image *im)
> > arch_free_bpf_trampoline(im->image, im->size);
> > bpf_jit_uncharge_modmem(im->size);
> > percpu_ref_exit(&im->pcref);
> > + kfree(im->skips);
> > kfree_rcu(im, rcu);
> > }
> >
> > static void __bpf_tramp_image_put_deferred(struct work_struct *work)
> > {
> > + struct bpf_trampoline *tr;
> > struct bpf_tramp_image *im;
> >
> > im = container_of(work, struct bpf_tramp_image, work);
> > + tr = im->tr;
> > + trampoline_lock(tr);
> > + list_del(&im->list);
> > + trampoline_unlock(tr);
> > + bpf_trampoline_put(tr);
> > bpf_tramp_image_free(im);
> > }
>
> [ ... ]
>
> This isn't a bug, but the comment at the call site of
> __bpf_tramp_image_put_deferred() appears stale. bpf_trampoline_update()
> now does:
>
> kernel/bpf/trampoline.c:bpf_trampoline_update()
> /* track the image until it is freed, for bpf_trampoline_skip_prog() */
> refcount_inc(&tr->refcnt);
> im->tr = tr;
> list_add(&im->list, &tr->images);
>
> so every live bpf_tramp_image holds a reference on the trampoline and
> tr->refcnt cannot reach 0 while any image is alive. The comment in
> bpf_trampoline_put() says:
>
> kernel/bpf/trampoline.c:bpf_trampoline_put()
> /* This code will be executed even when the last bpf_tramp_image
> * is alive. All progs are detached from the trampoline and the
> * trampoline image is patched with jmp into epilogue to skip
> * fexit progs. The fentry-only trampoline will be freed via
> * multiple rcu callbacks.
> */
> hlist_del(&tr->hlist_key);
>
> but the guarded block is now reached only after the last image has been
> unlinked, typically from __bpf_tramp_image_put_deferred() rather than
> from the detach syscall.
Ok, will update in v3 if we keep the list.
next prev parent reply other threads:[~2026-09-15 19:13 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-12 9:59 [PATCH bpf v2 0/2] bpf: Fix use-after-free of progs detached from busy trampolines Florent Revest (Anthropic)
2026-09-12 9:59 ` [PATCH bpf v2 1/2] bpf: Skip detached progs in trampoline images that are still in use Florent Revest (Anthropic)
2026-09-12 11:11 ` bot+bpf-ci
2026-09-15 19:13 ` Florent Revest [this message]
2026-09-12 16:42 ` Alexei Starovoitov
2026-09-15 18:39 ` Florent Revest
2026-09-16 5:29 ` Alexei Starovoitov
2026-09-16 7:52 ` Florent Revest
2026-09-16 18:03 ` Alexei Starovoitov
2026-09-14 4:13 ` Junseo Lim
2026-09-15 17:59 ` Florent Revest
2026-09-12 9:59 ` [PATCH bpf v2 2/2] selftests/bpf: Detach a trampoline prog while a task sleeps before it Florent Revest (Anthropic)
2026-09-12 10:53 ` bot+bpf-ci
2026-09-15 19:14 ` Florent Revest
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=DLG4NX9R5VTO.3QXM13LGWVURY@linux.dev \
--to=florent.revest@linux.dev \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bjorn@kernel.org \
--cc=bot+bpf-ci@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=chleroy@kernel.org \
--cc=daniel@iogearbox.net \
--cc=eddyz87@gmail.com \
--cc=hbathini@linux.ibm.com \
--cc=hengqi.chen@gmail.com \
--cc=ihor.solodrai@linux.dev \
--cc=iii@linux.ibm.com \
--cc=john.fastabend@gmail.com \
--cc=jolsa@kernel.org \
--cc=kpsingh@kernel.org \
--cc=leon.hwang@linux.dev \
--cc=linux-kernel@vger.kernel.org \
--cc=martin.lau@kernel.org \
--cc=martin.lau@linux.dev \
--cc=mason@kernel.org \
--cc=memxor@gmail.com \
--cc=naveen@kernel.org \
--cc=pulehui@huawei.com \
--cc=puranjay@kernel.org \
--cc=rhkrqnwk98@gmail.com \
--cc=song@kernel.org \
--cc=xukuohai@huaweicloud.com \
--cc=yangtiezhu@loongson.cn \
--cc=yonghong.song@linux.dev \
--cc=zirajs7@gmail.com \
/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
all inboxes | Powered by JetHome®