* [PATCH bpf-next v5 01/21] bpf: Pack bpf_insn_aux_data flags into bit fields
2026-09-23 4:58 [PATCH bpf-next v5 00/21] bpf: Run exception cleanup landing pads when bpf_throw() unwinds Yonghong Song
@ 2026-09-23 4:58 ` Yonghong Song
2026-09-23 5:40 ` bot+bpf-ci
2026-09-23 19:16 ` Eduard Zingerman
2026-09-23 4:58 ` [PATCH bpf-next v5 02/21] bpf: Accept the compiler's exception cleanup table at program load Yonghong Song
` (19 subsequent siblings)
20 siblings, 2 replies; 49+ messages in thread
From: Yonghong Song @ 2026-09-23 4:58 UTC (permalink / raw)
To: bpf
Cc: Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann,
Eduard Zingerman, kernel-team
struct bpf_insn_aux_data spreads its flags over three places: seven bools a
byte each, a u8 carrying three bit fields, and a u32 group with 25 of its
32 bits spare. Put them all in one u64 word, alu_state with them, and move
orig_idx below it. The structure goes from 144 bytes to 128 with no holes,
and 34 bits are left for later flags.
No functional change: a one-bit unsigned field holds 0 and 1 the way the
bool did, and nothing takes the address of any of them.
Suggested-by: Eduard Zingerman <eddyz87@gmail.com>
Signed-off-by: Yonghong Song <yonghong.song@linux.dev>
---
include/linux/bpf_verifier.h | 41 ++++++++++++++++++------------------
1 file changed, 21 insertions(+), 20 deletions(-)
diff --git a/include/linux/bpf_verifier.h b/include/linux/bpf_verifier.h
index 92f528c45605..15bc61de61ef 100644
--- a/include/linux/bpf_verifier.h
+++ b/include/linux/bpf_verifier.h
@@ -675,40 +675,41 @@ struct bpf_insn_aux_data {
u64 map_key_state; /* constant (32 bit) key tracking for maps */
int ctx_field_size; /* the ctx field size for load insn, maybe 0 */
u32 seen; /* this insn was processed by the verifier at env->pass_cnt */
- bool nospec; /* do not execute this instruction speculatively */
- bool nospec_result; /* result is unsafe under speculation, nospec must follow */
- bool zext_dst; /* this insn zero extends dst reg */
- bool needs_zext; /* alu op needs to clear upper bits */
- bool non_sleepable; /* helper/kfunc may be called from non-sleepable context */
- bool is_iter_next; /* bpf_iter_<type>_next() kfunc call */
- bool call_with_percpu_alloc_ptr; /* {this,per}_cpu_ptr() with prog percpu alloc */
- u8 alu_state; /* used in combination with alu_limit */
+ u64 nospec:1; /* do not execute this instruction speculatively */
+ u64 nospec_result:1; /* result is unsafe under speculation, nospec must follow */
+ u64 zext_dst:1; /* this insn zero extends dst reg */
+ u64 needs_zext:1; /* alu op needs to clear upper bits */
+ u64 non_sleepable:1; /* helper/kfunc may be called from non-sleepable context */
+ u64 is_iter_next:1; /* bpf_iter_<type>_next() kfunc call */
+ u64 call_with_percpu_alloc_ptr:1; /* {this,per}_cpu_ptr() with prog percpu alloc */
+ u64 alu_state:8; /* used in combination with alu_limit */
/* true if STX or LDX instruction is a part of a spill/fill
* pattern for a bpf_fastcall call.
*/
- u8 fastcall_pattern:1;
+ u64 fastcall_pattern:1;
/* for CALL instructions, a number of spill/fill pairs in the
* bpf_fastcall pattern.
*/
- u8 fastcall_spills_num:3;
- u8 arg_prog:4;
+ u64 fastcall_spills_num:3;
+ u64 arg_prog:4;
- /* below fields are initialized once */
- unsigned int orig_idx; /* original instruction index */
- u32 jmp_point:1;
- u32 prune_point:1;
+ /* below flags are initialized once */
+ u64 jmp_point:1;
+ u64 prune_point:1;
/* ensure we check state equivalence and save state checkpoint and
* this instruction, regardless of any heuristics
*/
- u32 force_checkpoint:1;
+ u64 force_checkpoint:1;
/* true if instruction is a call to a helper function that
* accepts callback function as a parameter.
*/
- u32 calls_callback:1;
- u32 indirect_target:1; /* if it is an indirect jump target */
- u32 non_stack_access:1; /* instruction can access non-stack memory */
+ u64 calls_callback:1;
+ u64 indirect_target:1; /* if it is an indirect jump target */
+ u64 non_stack_access:1; /* instruction can access non-stack memory */
/* true if some jump or call instruction targets this instruction */
- u32 jump_target:1;
+ u64 jump_target:1;
+
+ unsigned int orig_idx; /* original instruction index, initialized once */
/*
* CFG strongly connected component this instruction belongs to,
* zero if it is a singleton SCC.
--
2.53.0-Meta
^ permalink raw reply related [flat|nested] 49+ messages in thread* Re: [PATCH bpf-next v5 01/21] bpf: Pack bpf_insn_aux_data flags into bit fields
2026-09-23 4:58 ` [PATCH bpf-next v5 01/21] bpf: Pack bpf_insn_aux_data flags into bit fields Yonghong Song
@ 2026-09-23 5:40 ` bot+bpf-ci
2026-09-23 19:16 ` Eduard Zingerman
1 sibling, 0 replies; 49+ messages in thread
From: bot+bpf-ci @ 2026-09-23 5:40 UTC (permalink / raw)
To: yonghong.song, bpf
Cc: ast, andrii, daniel, eddyz87, kernel-team, ast, andrii, daniel,
martin.lau, eddyz87, yonghong.song, mason, ihor.solodrai
[-- Attachment #1: Type: text/plain, Size: 1437 bytes --]
> bpf: Pack bpf_insn_aux_data flags into bit fields
>
> struct bpf_insn_aux_data spreads its flags over three places: seven bools a
> byte each, a u8 carrying three bit fields, and a u32 group with 25 of its
> 32 bits spare. Put them all in one u64 word, alu_state with them, and move
> orig_idx below it. The structure goes from 144 bytes to 128 with no holes,
> and 34 bits are left for later flags.
Is the old size correct? The pre-patch struct appears to be 136 bytes on
x86-64, not 144, which would make the savings 8 bytes rather than 16.
Looking at the layout starting at offset 60 (where the naturally packed head
ends with u32 seen):
Old flag area:
60..66 7 bools (nospec through call_with_percpu_alloc_ptr)
67 u8 alu_state
68 u8 with three bit fields
69..71 3 padding bytes
72..75 unsigned int orig_idx
76..79 u32 with bit fields
80..131 u32 scc + 4x u16 + u32 const_reg_vals[10]
That gives sizeof 136 (aligned to 8). The new layout removes the 3-byte pad
at offset 69 by packing all flags into one u64 at 60..67, followed directly
by unsigned int orig_idx at 68..71, giving sizeof 128.
Could the commit message be updated to reflect the actual delta?
---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md
CI run summary: https://github.com/kernel-patches/bpf/actions/runs/35821138073
^ permalink raw reply [flat|nested] 49+ messages in thread
* Re: [PATCH bpf-next v5 01/21] bpf: Pack bpf_insn_aux_data flags into bit fields
2026-09-23 4:58 ` [PATCH bpf-next v5 01/21] bpf: Pack bpf_insn_aux_data flags into bit fields Yonghong Song
2026-09-23 5:40 ` bot+bpf-ci
@ 2026-09-23 19:16 ` Eduard Zingerman
1 sibling, 0 replies; 49+ messages in thread
From: Eduard Zingerman @ 2026-09-23 19:16 UTC (permalink / raw)
To: Yonghong Song, bpf
Cc: Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann, kernel-team
On Tue, 2026-09-22 at 21:58 -0700, Yonghong Song wrote:
> struct bpf_insn_aux_data spreads its flags over three places: seven bools a
> byte each, a u8 carrying three bit fields, and a u32 group with 25 of its
> 32 bits spare. Put them all in one u64 word, alu_state with them, and move
> orig_idx below it. The structure goes from 144 bytes to 128 with no holes,
The bot is right, it's 136 on master and 144 when v4 of this series is applied.
> and 34 bits are left for later flags.
>
> No functional change: a one-bit unsigned field holds 0 and 1 the way the
> bool did, and nothing takes the address of any of them.
>
> Suggested-by: Eduard Zingerman <eddyz87@gmail.com>
> Signed-off-by: Yonghong Song <yonghong.song@linux.dev>
> ---
Acked-by: Eduard Zingerman <eddyz87@gmail.com>
...
^ permalink raw reply [flat|nested] 49+ messages in thread
* [PATCH bpf-next v5 02/21] bpf: Accept the compiler's exception cleanup table at program load
2026-09-23 4:58 [PATCH bpf-next v5 00/21] bpf: Run exception cleanup landing pads when bpf_throw() unwinds Yonghong Song
2026-09-23 4:58 ` [PATCH bpf-next v5 01/21] bpf: Pack bpf_insn_aux_data flags into bit fields Yonghong Song
@ 2026-09-23 4:58 ` Yonghong Song
2026-09-23 4:59 ` [PATCH bpf-next v5 03/21] bpf: Add the bpf_unwind_resume() kfunc Yonghong Song
` (18 subsequent siblings)
20 siblings, 0 replies; 49+ messages in thread
From: Yonghong Song @ 2026-09-23 4:58 UTC (permalink / raw)
To: bpf
Cc: Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann,
Eduard Zingerman, kernel-team
LLVM 23 added exception handling support for BPF with the .bpf_cleanup
section ([1]). Rust code compiled with panic=unwind runs cleanup code
(Drop glue) when bpf_throw() fires, and the LLVM BPF backend emits that
section from the landing pads the frontend produced. Plain C cannot
generate .bpf_cleanup unless inline asm is used. The Rust compiler does not
*properly* support BPF exception handling yet, but the kernel can support
the table today, and inline assembly is enough to test it.
Add the UAPI to carry the .bpf_cleanup table into the kernel. BPF_PROG_LOAD
grows cleanup_info, cleanup_info_cnt and cleanup_info_rec_size, and struct
bpf_cleanup_info describes one record as a triple of instruction indices:
the half-open call-site range [begin_off, end_off) and the landing_pad_off
the frame resumes at. The table arrives sorted by begin_off, with disjoint
ranges and each record's three offsets inside one subprogram;
check_cleanup_info() holds it to that at load time. Nothing reads it yet;
the patches that follow -- the CFG walk, the unwind walk and the JITs --
are its consumers.
Link: https://github.com/llvm/llvm-project/pull/192164 [1]
Signed-off-by: Yonghong Song <yonghong.song@linux.dev>
---
include/linux/bpf_verifier.h | 2 +
include/uapi/linux/bpf.h | 9 +++
kernel/bpf/check_btf.c | 134 +++++++++++++++++++++++++++++++++
kernel/bpf/syscall.c | 2 +-
kernel/bpf/verifier.c | 1 +
tools/include/uapi/linux/bpf.h | 9 +++
6 files changed, 156 insertions(+), 1 deletion(-)
diff --git a/include/linux/bpf_verifier.h b/include/linux/bpf_verifier.h
index 15bc61de61ef..2b6b480c059e 100644
--- a/include/linux/bpf_verifier.h
+++ b/include/linux/bpf_verifier.h
@@ -988,6 +988,8 @@ struct bpf_verifier_env {
struct arg_track **callsite_at_stack;
u32 pass_cnt; /* number of times do_check() was called */
u32 subprog_cnt;
+ struct bpf_cleanup_info *cleanup_info;
+ u32 cleanup_info_cnt;
/* number of instructions analyzed by the verifier */
u32 prev_insn_processed, insn_processed;
/* number of jmps, calls, exits analyzed so far */
diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h
index 0aaa54359aeb..646aa8758d48 100644
--- a/include/uapi/linux/bpf.h
+++ b/include/uapi/linux/bpf.h
@@ -1669,6 +1669,9 @@ union bpf_attr {
* verification.
*/
__s32 keyring_id;
+ __aligned_u64 cleanup_info; /* exception cleanup table */
+ __u32 cleanup_info_rec_size; /* userspace bpf_cleanup_info size */
+ __u32 cleanup_info_cnt; /* number of bpf_cleanup_info records */
};
struct { /* anonymous struct used by BPF_OBJ_* commands */
@@ -7599,6 +7602,12 @@ struct bpf_line_info {
__u32 line_col;
};
+struct bpf_cleanup_info {
+ __u32 begin_off;
+ __u32 end_off;
+ __u32 landing_pad_off;
+};
+
struct bpf_spin_lock {
__u32 val;
};
diff --git a/kernel/bpf/check_btf.c b/kernel/bpf/check_btf.c
index 0e8b3ccc7a5b..bc569fa5f97f 100644
--- a/kernel/bpf/check_btf.c
+++ b/kernel/bpf/check_btf.c
@@ -407,6 +407,136 @@ static int check_core_relo(struct bpf_verifier_env *env,
return err;
}
+#define MIN_BPF_CLEANUP_INFO_SIZE 12
+#define MAX_CLEANUP_INFO_REC_SIZE MAX_FUNCINFO_REC_SIZE
+
+static int check_cleanup_info(struct bpf_verifier_env *env,
+ const union bpf_attr *attr,
+ bpfptr_t uattr)
+{
+ u32 krec_size = sizeof(struct bpf_cleanup_info);
+ u32 i, nrec, urec_size, min_size, prev_end = 0;
+ struct bpf_cleanup_info *krecord;
+ bpfptr_t urecord;
+ int ret = -EINVAL;
+
+ nrec = attr->cleanup_info_cnt;
+ if (!nrec)
+ return 0;
+ if (nrec > env->prog->len) {
+ verbose(env, "cleanup info has %u records for %u instructions\n",
+ nrec, env->prog->len);
+ return -EINVAL;
+ }
+
+ urec_size = attr->cleanup_info_rec_size;
+ if (urec_size < MIN_BPF_CLEANUP_INFO_SIZE ||
+ urec_size > MAX_CLEANUP_INFO_REC_SIZE ||
+ urec_size % sizeof(u32)) {
+ verbose(env, "invalid cleanup info rec size %u\n", urec_size);
+ return -EINVAL;
+ }
+
+ krecord = kvcalloc(nrec, krec_size, GFP_KERNEL_ACCOUNT | __GFP_NOWARN);
+ if (!krecord)
+ return -ENOMEM;
+
+ min_size = min_t(u32, krec_size, urec_size);
+ urecord = make_bpfptr(attr->cleanup_info, uattr.is_kernel);
+ for (i = 0; i < nrec; i++) {
+ struct bpf_subprog_info *sb, *se, *sl;
+ struct bpf_cleanup_info *rec = &krecord[i];
+
+ ret = bpf_check_uarg_tail_zero(urecord, krec_size, urec_size);
+ if (ret) {
+ if (ret == -E2BIG) {
+ verbose(env, "nonzero tailing record in cleanup info\n");
+ if (copy_to_bpfptr_offset(uattr,
+ offsetof(union bpf_attr,
+ cleanup_info_rec_size),
+ &min_size, sizeof(min_size)))
+ ret = -EFAULT;
+ }
+ goto err_free;
+ }
+
+ if (copy_from_bpfptr(rec, urecord, min_size)) {
+ ret = -EFAULT;
+ goto err_free;
+ }
+ bpfptr_add(&urecord, urec_size);
+
+ ret = -EINVAL;
+ if (rec->begin_off >= rec->end_off) {
+ verbose(env, "cleanup_info[%u]: begin %u >= end %u\n",
+ i, rec->begin_off, rec->end_off);
+ goto err_free;
+ }
+ if (i && rec->begin_off < prev_end) {
+ verbose(env,
+ "cleanup_info[%u]: range [%u,%u) is unsorted or overlaps the previous record\n",
+ i, rec->begin_off, rec->end_off);
+ goto err_free;
+ }
+ prev_end = rec->end_off;
+
+ sb = bpf_find_containing_subprog(env, rec->begin_off);
+ se = bpf_find_containing_subprog(env, rec->end_off - 1);
+ sl = bpf_find_containing_subprog(env, rec->landing_pad_off);
+ if (!sb || !se || !sl) {
+ verbose(env, "cleanup_info[%u]: offset out of range\n", i);
+ goto err_free;
+ }
+ if (sb != se || sb != sl) {
+ verbose(env,
+ "cleanup_info[%u]: range/landing pad span multiple subprogs\n",
+ i);
+ goto err_free;
+ }
+ /*
+ * A zero opcode is the second half of a 16-byte insn, not an
+ * insn. end_off is exclusive, so it may be one past the last.
+ */
+ if (!env->prog->insnsi[rec->begin_off].code ||
+ !env->prog->insnsi[rec->landing_pad_off].code ||
+ (rec->end_off < env->prog->len &&
+ !env->prog->insnsi[rec->end_off].code)) {
+ verbose(env, "cleanup_info[%u]: points at invalid insn\n", i);
+ goto err_free;
+ }
+ }
+
+ /* Reject a landing pad inside any call-site range, its own included. */
+ ret = -EINVAL;
+ for (i = 0; i < nrec; i++) {
+ u32 pad = krecord[i].landing_pad_off;
+ u32 l = 0, r = nrec;
+
+ while (l < r) {
+ u32 m = l + (r - l) / 2;
+
+ if (pad < krecord[m].begin_off) {
+ r = m;
+ } else if (pad >= krecord[m].end_off) {
+ l = m + 1;
+ } else {
+ verbose(env,
+ "cleanup_info[%u]: landing pad %u is inside the call-site range of cleanup_info[%u]\n",
+ i, pad, m);
+ goto err_free;
+ }
+ }
+ }
+
+ env->cleanup_info = krecord;
+ env->cleanup_info_cnt = nrec;
+ return 0;
+
+err_free:
+ kvfree(krecord);
+ return ret;
+}
+
int bpf_prepare_btf_info(struct bpf_verifier_env *env,
const union bpf_attr *attr,
bpfptr_t uattr)
@@ -441,6 +571,10 @@ int bpf_check_btf_info(struct bpf_verifier_env *env,
{
int err;
+ err = check_cleanup_info(env, attr, uattr);
+ if (err)
+ return err;
+
if (!attr->func_info_cnt && !attr->line_info_cnt) {
if (check_abnormal_return(env))
return -EINVAL;
diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
index 74496fd716d3..63c84a60c1b7 100644
--- a/kernel/bpf/syscall.c
+++ b/kernel/bpf/syscall.c
@@ -2924,7 +2924,7 @@ int __init __used bpf_multi_func(void) { return 0; }
BTF_ID_LIST_GLOBAL_SINGLE(bpf_multi_func_btf_id, func, bpf_multi_func)
/* last field in 'union bpf_attr' used by this command */
-#define BPF_PROG_LOAD_LAST_FIELD keyring_id
+#define BPF_PROG_LOAD_LAST_FIELD cleanup_info_cnt
static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr, struct bpf_log_attr *attr_log)
{
diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index a7c9e2d8965d..c65103152320 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -21999,6 +21999,7 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr,
kvfree(env->succ);
kvfree(env->gotox_tmp_buf);
bpf_diag_free(env);
+ kvfree(env->cleanup_info);
kvfree(env);
return ret;
}
diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h
index 0aaa54359aeb..646aa8758d48 100644
--- a/tools/include/uapi/linux/bpf.h
+++ b/tools/include/uapi/linux/bpf.h
@@ -1669,6 +1669,9 @@ union bpf_attr {
* verification.
*/
__s32 keyring_id;
+ __aligned_u64 cleanup_info; /* exception cleanup table */
+ __u32 cleanup_info_rec_size; /* userspace bpf_cleanup_info size */
+ __u32 cleanup_info_cnt; /* number of bpf_cleanup_info records */
};
struct { /* anonymous struct used by BPF_OBJ_* commands */
@@ -7599,6 +7602,12 @@ struct bpf_line_info {
__u32 line_col;
};
+struct bpf_cleanup_info {
+ __u32 begin_off;
+ __u32 end_off;
+ __u32 landing_pad_off;
+};
+
struct bpf_spin_lock {
__u32 val;
};
--
2.53.0-Meta
^ permalink raw reply related [flat|nested] 49+ messages in thread* [PATCH bpf-next v5 03/21] bpf: Add the bpf_unwind_resume() kfunc
2026-09-23 4:58 [PATCH bpf-next v5 00/21] bpf: Run exception cleanup landing pads when bpf_throw() unwinds Yonghong Song
2026-09-23 4:58 ` [PATCH bpf-next v5 01/21] bpf: Pack bpf_insn_aux_data flags into bit fields Yonghong Song
2026-09-23 4:58 ` [PATCH bpf-next v5 02/21] bpf: Accept the compiler's exception cleanup table at program load Yonghong Song
@ 2026-09-23 4:59 ` Yonghong Song
2026-09-23 6:13 ` Kumar Kartikeya Dwivedi
2026-09-23 4:59 ` [PATCH bpf-next v5 04/21] bpf: Add lookups for exception cleanup resumes and landing pads Yonghong Song
` (17 subsequent siblings)
20 siblings, 1 reply; 49+ messages in thread
From: Yonghong Song @ 2026-09-23 4:59 UTC (permalink / raw)
To: bpf
Cc: Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann,
Eduard Zingerman, kernel-team
A compiler-emitted cleanup landing pad ends with a call to
_Unwind_Resume(), which carries the unwind on once that frame's cleanups
have run. The kernel provides the same terminator as a kfunc, named
bpf_unwind_resume() to keep the 'bpf_' prefix kfunc convention; a later
libbpf patch resolves the compiler's name to it.
The kfunc is defined here but not registered with any program type yet. A
call to it is only ever valid inside a landing pad, and the verifier cannot
say that until it knows what a landing pad is, so registration waits for
the patch that adds the rule.
Signed-off-by: Yonghong Song <yonghong.song@linux.dev>
---
kernel/bpf/helpers.c | 13 +++++++++++++
1 file changed, 13 insertions(+)
diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c
index 301b35bd85c8..249418e60ee4 100644
--- a/kernel/bpf/helpers.c
+++ b/kernel/bpf/helpers.c
@@ -3445,6 +3445,19 @@ __bpf_kfunc void bpf_throw(u64 cookie)
WARN(1, "A call to BPF exception callback should never return\n");
}
+/*
+ * Terminator of a compiler-emitted cleanup landing pad. The compiler names
+ * this _Unwind_Resume, the base unwind ABI's entry point for carrying an
+ * unwind on once a frame's cleanups have run. To match kernel kfunc
+ * convention, the kernel calls it bpf_unwind_resume and libbpf maps the
+ * compiler's name onto it.
+ */
+__bpf_kfunc void bpf_unwind_resume(void)
+{
+ /* Never reached: a JIT emits the way out of a landing pad instead. */
+ WARN_ONCE(1, "A JIT should have replaced the exception cleanup resume\n");
+}
+
__bpf_kfunc int bpf_wq_init(struct bpf_wq *wq, void *p__const_map, unsigned int flags)
{
struct bpf_async_kern *async = (struct bpf_async_kern *)wq;
--
2.53.0-Meta
^ permalink raw reply related [flat|nested] 49+ messages in thread* Re: [PATCH bpf-next v5 03/21] bpf: Add the bpf_unwind_resume() kfunc
2026-09-23 4:59 ` [PATCH bpf-next v5 03/21] bpf: Add the bpf_unwind_resume() kfunc Yonghong Song
@ 2026-09-23 6:13 ` Kumar Kartikeya Dwivedi
0 siblings, 0 replies; 49+ messages in thread
From: Kumar Kartikeya Dwivedi @ 2026-09-23 6:13 UTC (permalink / raw)
To: Yonghong Song, bpf
Cc: Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann,
Eduard Zingerman, kernel-team
On Wed Sep 23, 2026 at 6:59 AM CEST, Yonghong Song wrote:
> A compiler-emitted cleanup landing pad ends with a call to
> _Unwind_Resume(), which carries the unwind on once that frame's cleanups
> have run. The kernel provides the same terminator as a kfunc, named
> bpf_unwind_resume() to keep the 'bpf_' prefix kfunc convention; a later
> libbpf patch resolves the compiler's name to it.
>
> The kfunc is defined here but not registered with any program type yet. A
> call to it is only ever valid inside a landing pad, and the verifier cannot
> say that until it knows what a landing pad is, so registration waits for
> the patch that adds the rule.
>
> Signed-off-by: Yonghong Song <yonghong.song@linux.dev>
> ---
> kernel/bpf/helpers.c | 13 +++++++++++++
> 1 file changed, 13 insertions(+)
>
> diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c
> index 301b35bd85c8..249418e60ee4 100644
> --- a/kernel/bpf/helpers.c
> +++ b/kernel/bpf/helpers.c
> @@ -3445,6 +3445,19 @@ __bpf_kfunc void bpf_throw(u64 cookie)
> WARN(1, "A call to BPF exception callback should never return\n");
> }
>
> +/*
> + * Terminator of a compiler-emitted cleanup landing pad. The compiler names
> + * this _Unwind_Resume, the base unwind ABI's entry point for carrying an
> + * unwind on once a frame's cleanups have run. To match kernel kfunc
> + * convention, the kernel calls it bpf_unwind_resume and libbpf maps the
> + * compiler's name onto it.
> + */
> +__bpf_kfunc void bpf_unwind_resume(void)
> +{
> + /* Never reached: a JIT emits the way out of a landing pad instead. */
> + WARN_ONCE(1, "A JIT should have replaced the exception cleanup resume\n");
> +}
> +
Can we change this to void bpf_unwind_resume(void *ptr__ign)?
Itanium ABI passes the exception descriptor to _Unwind_Resume. We do not need it
right now, but I feel like it might be better to take and ignore it. Changing it
later might be more expensive, while it costs nothing to change for now.
^ permalink raw reply [flat|nested] 49+ messages in thread
* [PATCH bpf-next v5 04/21] bpf: Add lookups for exception cleanup resumes and landing pads
2026-09-23 4:58 [PATCH bpf-next v5 00/21] bpf: Run exception cleanup landing pads when bpf_throw() unwinds Yonghong Song
` (2 preceding siblings ...)
2026-09-23 4:59 ` [PATCH bpf-next v5 03/21] bpf: Add the bpf_unwind_resume() kfunc Yonghong Song
@ 2026-09-23 4:59 ` Yonghong Song
2026-09-23 4:59 ` [PATCH bpf-next v5 05/21] bpf: Prepare for an exception cleanup table before the CFG walk Yonghong Song
` (16 subsequent siblings)
20 siblings, 0 replies; 49+ messages in thread
From: Yonghong Song @ 2026-09-23 4:59 UTC (permalink / raw)
To: bpf
Cc: Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann,
Eduard Zingerman, kernel-team
Add two new files, exception.h and exception.c, to host the exception
handling code. Only a few helpers so far: recognising a call to
bpf_unwind_resume(), and asking which landing pad, if any, a call site
unwinds to.
The pad of a call site is kept in insn_aux_data, so the two places that
move instructions around -- bpf_patch_insn_data() and
verifier_remove_insns() -- learn to keep it in step.
Signed-off-by: Yonghong Song <yonghong.song@linux.dev>
---
include/linux/bpf_verifier.h | 6 ++++++
kernel/bpf/Makefile | 2 +-
kernel/bpf/exception.c | 23 +++++++++++++++++++++++
kernel/bpf/exception.h | 12 ++++++++++++
kernel/bpf/fixups.c | 26 +++++++++++++++++++++++++-
5 files changed, 67 insertions(+), 2 deletions(-)
create mode 100644 kernel/bpf/exception.c
create mode 100644 kernel/bpf/exception.h
diff --git a/include/linux/bpf_verifier.h b/include/linux/bpf_verifier.h
index 2b6b480c059e..666c73308b32 100644
--- a/include/linux/bpf_verifier.h
+++ b/include/linux/bpf_verifier.h
@@ -710,6 +710,11 @@ struct bpf_insn_aux_data {
u64 jump_target:1;
unsigned int orig_idx; /* original instruction index, initialized once */
+ /*
+ * 1 + the instruction index of the exception cleanup landing pad
+ * this call site unwinds to, or 0 for none.
+ */
+ u32 cleanup_pad;
/*
* CFG strongly connected component this instruction belongs to,
* zero if it is a singleton SCC.
@@ -1519,6 +1524,7 @@ u32 btf_func_arg_align(const struct btf *btf, const struct btf_type *t);
int bpf_find_subprog(struct bpf_verifier_env *env, int off);
bool bpf_is_throw_kfunc(struct bpf_insn *insn);
+bool bpf_is_unwind_resume_kfunc(const struct bpf_insn *insn);
int bpf_compute_const_regs(struct bpf_verifier_env *env);
int bpf_prune_dead_branches(struct bpf_verifier_env *env);
int bpf_check_cfg(struct bpf_verifier_env *env);
diff --git a/kernel/bpf/Makefile b/kernel/bpf/Makefile
index 9a92c348bbda..af9bc60428ad 100644
--- a/kernel/bpf/Makefile
+++ b/kernel/bpf/Makefile
@@ -11,7 +11,7 @@ obj-$(CONFIG_BPF_SYSCALL) += bpf_iter.o map_iter.o task_iter.o prog_iter.o link_
obj-$(CONFIG_BPF_SYSCALL) += hashtab.o arraymap.o percpu_freelist.o bpf_lru_list.o lpm_trie.o map_in_map.o bloom_filter.o
obj-$(CONFIG_BPF_SYSCALL) += local_storage.o queue_stack_maps.o ringbuf.o bpf_insn_array.o
obj-$(CONFIG_BPF_SYSCALL) += bpf_local_storage.o bpf_task_storage.o
-obj-$(CONFIG_BPF_SYSCALL) += fixups.o cfg.o states.o backtrack.o check_btf.o
+obj-$(CONFIG_BPF_SYSCALL) += fixups.o cfg.o states.o backtrack.o check_btf.o exception.o
obj-${CONFIG_BPF_LSM} += bpf_inode_storage.o
obj-$(CONFIG_BPF_SYSCALL) += disasm.o mprog.o
obj-$(CONFIG_BPF_JIT) += trampoline.o
diff --git a/kernel/bpf/exception.c b/kernel/bpf/exception.c
new file mode 100644
index 000000000000..6989f31aa6c9
--- /dev/null
+++ b/kernel/bpf/exception.c
@@ -0,0 +1,23 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
+#include <linux/bpf.h>
+#include <linux/bpf_verifier.h>
+#include <linux/btf.h>
+#include <linux/btf_ids.h>
+#include <linux/filter.h>
+#include "exception.h"
+
+BTF_ID_LIST_SINGLE(bpf_unwind_resume_id, func, bpf_unwind_resume)
+
+bool bpf_is_unwind_resume_kfunc(const struct bpf_insn *insn)
+{
+ return bpf_pseudo_kfunc_call(insn) && insn->off == 0 &&
+ insn->imm == bpf_unwind_resume_id[0];
+}
+
+int bpf_exc_pad_of_call(struct bpf_verifier_env *env, u32 idx)
+{
+ u32 pad = env->insn_aux_data[idx].cleanup_pad;
+
+ return pad ? (int)pad - 1 : -1;
+}
diff --git a/kernel/bpf/exception.h b/kernel/bpf/exception.h
new file mode 100644
index 000000000000..634cb2c15ffc
--- /dev/null
+++ b/kernel/bpf/exception.h
@@ -0,0 +1,12 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
+#ifndef _LINUX_BPF_EXCEPTION_H
+#define _LINUX_BPF_EXCEPTION_H
+
+#include <linux/types.h>
+
+struct bpf_verifier_env;
+
+int bpf_exc_pad_of_call(struct bpf_verifier_env *env, u32 idx);
+
+#endif /* _LINUX_BPF_EXCEPTION_H */
diff --git a/kernel/bpf/fixups.c b/kernel/bpf/fixups.c
index 2add8001c3ec..e3459123803c 100644
--- a/kernel/bpf/fixups.c
+++ b/kernel/bpf/fixups.c
@@ -256,11 +256,18 @@ static void adjust_insn_aux_data(struct bpf_verifier_env *env,
data[i].non_stack_access =
data[off + cnt - 1].non_stack_access;
data[off + cnt - 1].non_stack_access = false;
+ data[i].cleanup_pad = data[off + cnt - 1].cleanup_pad;
+ data[off + cnt - 1].cleanup_pad = 0;
} else if (bpf_is_mem_insn(insn + i)) {
data[i].non_stack_access = true;
}
}
+ if (env->cleanup_info_cnt)
+ for (i = 0; i < prog_len; i++)
+ if (data[i].cleanup_pad > off + 1)
+ data[i].cleanup_pad += cnt - 1;
+
/*
* Last slot instruction could be a newly generated
* BPF_ST/BPF_LDX/BPF_STX, systematically mark it for non-stack access
@@ -549,6 +556,7 @@ static int verifier_remove_insns(struct bpf_verifier_env *env, u32 off, u32 cnt)
struct bpf_insn_aux_data *aux_data = env->insn_aux_data;
unsigned int orig_prog_len = env->prog->len;
int err;
+ u32 i;
if (bpf_prog_is_offloaded(env->prog->aux))
bpf_prog_offload_remove_insns(env, off, cnt);
@@ -573,6 +581,17 @@ static int verifier_remove_insns(struct bpf_verifier_env *env, u32 off, u32 cnt)
sizeof(*aux_data) * (orig_prog_len - off - cnt));
env->insn_aux_data_len -= cnt;
+ if (env->cleanup_info_cnt) {
+ for (i = 0; i < env->insn_aux_data_len; i++) {
+ u32 pad = aux_data[i].cleanup_pad;
+
+ if (pad > off + cnt)
+ aux_data[i].cleanup_pad = pad - cnt;
+ else if (pad > off)
+ aux_data[i].cleanup_pad = 0;
+ }
+ }
+
return 0;
}
@@ -648,7 +667,7 @@ int bpf_opt_remove_nops(struct bpf_verifier_env *env)
struct bpf_insn *insn = env->prog->insnsi;
int insn_cnt = env->prog->len;
bool is_may_goto_0, is_ja;
- int i, err;
+ int i, j, err;
for (i = 0; i < insn_cnt; i++) {
is_may_goto_0 = !memcmp(&insn[i], &MAY_GOTO_0, sizeof(MAY_GOTO_0));
@@ -657,6 +676,11 @@ int bpf_opt_remove_nops(struct bpf_verifier_env *env)
if (!is_may_goto_0 && !is_ja)
continue;
+ if (env->cleanup_info_cnt)
+ for (j = 0; j < insn_cnt; j++)
+ if (env->insn_aux_data[j].cleanup_pad == i + 1)
+ env->insn_aux_data[j].cleanup_pad = i + 2;
+
err = verifier_remove_insns(env, i, 1);
if (err)
return err;
--
2.53.0-Meta
^ permalink raw reply related [flat|nested] 49+ messages in thread* [PATCH bpf-next v5 05/21] bpf: Prepare for an exception cleanup table before the CFG walk
2026-09-23 4:58 [PATCH bpf-next v5 00/21] bpf: Run exception cleanup landing pads when bpf_throw() unwinds Yonghong Song
` (3 preceding siblings ...)
2026-09-23 4:59 ` [PATCH bpf-next v5 04/21] bpf: Add lookups for exception cleanup resumes and landing pads Yonghong Song
@ 2026-09-23 4:59 ` Yonghong Song
2026-09-23 5:40 ` bot+bpf-ci
2026-09-23 4:59 ` [PATCH bpf-next v5 06/21] bpf: Make exception landing pads reachable in the CFG Yonghong Song
` (15 subsequent siblings)
20 siblings, 1 reply; 49+ messages in thread
From: Yonghong Song @ 2026-09-23 4:59 UTC (permalink / raw)
To: bpf
Cc: Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann,
Eduard Zingerman, kernel-team
Record in insn_aux_data what the later passes need from the cleanup table:
throw_call for every bpf_throw() and resume_call for every
bpf_unwind_resume() -- the two calls a JIT lowers its own way rather than
as calls -- and cleanup_pad, the landing pad a frame resumes at, for every
call within the [begin_off, end_off) range of a cleanup record. Subsequent
commits consume all three.
Marking the two calls here, rather than recognising them in the JIT, is
what makes the recognition exact: by the time a JIT runs,
bpf_fixup_kfunc_call() has rewritten every other kfunc's imm into an offset
from __bpf_call_base, and a BTF id compared against one of those offsets
could match an unrelated call.
bpf_prepare_cleanup_exceptions() runs before bpf_check_cfg(), whose walk
consumes what it produces. It refuses a table on an offloaded program, on
one whose JIT cannot dispatch landing pads or was not asked to compile it,
and on one that also installs an exception callback -- two different
answers to what runs on the way out. What survives is marked jit_required:
the interpreter cannot dispatch a pad. bpf_jit_supports_cleanup_pads() is
weak here and says no; the arch patches provide the real ones.
Acked-by: Eduard Zingerman <eddyz87@gmail.com>
Signed-off-by: Yonghong Song <yonghong.song@linux.dev>
---
include/linux/bpf_verifier.h | 2 ++
include/linux/filter.h | 1 +
kernel/bpf/core.c | 5 +++
kernel/bpf/exception.c | 62 ++++++++++++++++++++++++++++++++++++
kernel/bpf/exception.h | 1 +
kernel/bpf/fixups.c | 6 ++++
kernel/bpf/verifier.c | 6 ++++
7 files changed, 83 insertions(+)
diff --git a/include/linux/bpf_verifier.h b/include/linux/bpf_verifier.h
index 666c73308b32..e12ec91b8150 100644
--- a/include/linux/bpf_verifier.h
+++ b/include/linux/bpf_verifier.h
@@ -692,6 +692,8 @@ struct bpf_insn_aux_data {
*/
u64 fastcall_spills_num:3;
u64 arg_prog:4;
+ u64 throw_call:1; /* call to bpf_throw() */
+ u64 resume_call:1; /* call to bpf_unwind_resume() */
/* below flags are initialized once */
u64 jmp_point:1;
diff --git a/include/linux/filter.h b/include/linux/filter.h
index 422284b4fa96..2582a7606e46 100644
--- a/include/linux/filter.h
+++ b/include/linux/filter.h
@@ -1242,6 +1242,7 @@ bool bpf_jit_supports_stack_args(void);
bool bpf_jit_supports_arena_args(void);
bool bpf_jit_supports_far_kfunc_call(void);
bool bpf_jit_supports_exceptions(void);
+bool bpf_jit_supports_cleanup_pads(void);
bool bpf_jit_supports_ptr_xchg(void);
bool bpf_jit_supports_arena(void);
bool bpf_jit_supports_insn(struct bpf_insn *insn, bool in_arena);
diff --git a/kernel/bpf/core.c b/kernel/bpf/core.c
index 227211166dcc..a379cd1ec4c6 100644
--- a/kernel/bpf/core.c
+++ b/kernel/bpf/core.c
@@ -3475,6 +3475,11 @@ void __weak arch_bpf_stack_walk(bool (*consume_fn)(void *cookie, u64 ip, u64 sp,
{
}
+bool __weak bpf_jit_supports_cleanup_pads(void)
+{
+ return false;
+}
+
bool __weak bpf_jit_supports_timed_may_goto(void)
{
return false;
diff --git a/kernel/bpf/exception.c b/kernel/bpf/exception.c
index 6989f31aa6c9..244ea1b5417c 100644
--- a/kernel/bpf/exception.c
+++ b/kernel/bpf/exception.c
@@ -7,8 +7,70 @@
#include <linux/filter.h>
#include "exception.h"
+#define verbose(env, fmt, args...) bpf_verifier_log_write(env, fmt, ##args)
+
BTF_ID_LIST_SINGLE(bpf_unwind_resume_id, func, bpf_unwind_resume)
+static void mark_kfunc_sites(struct bpf_verifier_env *env)
+{
+ u32 i;
+
+ for (i = 0; i < env->prog->len; i++) {
+ struct bpf_insn *insn = &env->prog->insnsi[i];
+
+ if (bpf_is_throw_kfunc(insn))
+ env->insn_aux_data[i].throw_call = true;
+ else if (bpf_is_unwind_resume_kfunc(insn))
+ env->insn_aux_data[i].resume_call = true;
+ }
+}
+
+static void mark_call_sites(struct bpf_verifier_env *env)
+{
+ u32 i, j;
+
+ for (i = 0; i < env->cleanup_info_cnt; i++) {
+ struct bpf_cleanup_info *rec = &env->cleanup_info[i];
+
+ for (j = rec->begin_off; j < rec->end_off; j++) {
+ struct bpf_insn *insn = &env->prog->insnsi[j];
+
+ if (!bpf_pseudo_call(insn) && !bpf_is_throw_kfunc(insn))
+ continue;
+ env->insn_aux_data[j].cleanup_pad = rec->landing_pad_off + 1;
+ }
+ }
+}
+
+int bpf_prepare_cleanup_exceptions(struct bpf_verifier_env *env)
+{
+ if (!env->cleanup_info_cnt)
+ return 0;
+
+ if (bpf_prog_is_offloaded(env->prog->aux)) {
+ verbose(env,
+ "exception cleanup is not supported for offloaded programs\n");
+ return -EINVAL;
+ }
+
+ if (!bpf_jit_supports_cleanup_pads() || !env->prog->jit_requested) {
+ verbose(env,
+ "exception cleanup needs a JIT that can dispatch landing pads\n");
+ return -EOPNOTSUPP;
+ }
+ env->prog->jit_required = 1;
+
+ if (env->exception_callback_subprog) {
+ verbose(env,
+ "exception cleanup table cannot be combined with an exception callback\n");
+ return -EINVAL;
+ }
+
+ mark_kfunc_sites(env);
+ mark_call_sites(env);
+ return 0;
+}
+
bool bpf_is_unwind_resume_kfunc(const struct bpf_insn *insn)
{
return bpf_pseudo_kfunc_call(insn) && insn->off == 0 &&
diff --git a/kernel/bpf/exception.h b/kernel/bpf/exception.h
index 634cb2c15ffc..b96b2c429e22 100644
--- a/kernel/bpf/exception.h
+++ b/kernel/bpf/exception.h
@@ -7,6 +7,7 @@
struct bpf_verifier_env;
+int bpf_prepare_cleanup_exceptions(struct bpf_verifier_env *env);
int bpf_exc_pad_of_call(struct bpf_verifier_env *env, u32 idx);
#endif /* _LINUX_BPF_EXCEPTION_H */
diff --git a/kernel/bpf/fixups.c b/kernel/bpf/fixups.c
index e3459123803c..fed5b66ed3b7 100644
--- a/kernel/bpf/fixups.c
+++ b/kernel/bpf/fixups.c
@@ -256,6 +256,12 @@ static void adjust_insn_aux_data(struct bpf_verifier_env *env,
data[i].non_stack_access =
data[off + cnt - 1].non_stack_access;
data[off + cnt - 1].non_stack_access = false;
+ data[i].throw_call =
+ data[off + cnt - 1].throw_call;
+ data[off + cnt - 1].throw_call = false;
+ data[i].resume_call =
+ data[off + cnt - 1].resume_call;
+ data[off + cnt - 1].resume_call = false;
data[i].cleanup_pad = data[off + cnt - 1].cleanup_pad;
data[off + cnt - 1].cleanup_pad = 0;
} else if (bpf_is_mem_insn(insn + i)) {
diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index c65103152320..8163c75ec3fa 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -37,6 +37,7 @@
#include "diagnostics.h"
#include "disasm.h"
+#include "exception.h"
static const struct bpf_verifier_ops * const bpf_verifier_ops[] = {
#define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \
@@ -21792,6 +21793,11 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr,
if (ret < 0)
goto skip_full_check;
+ /* The CFG needs an edge from a call in a cleanup range to its pad. */
+ ret = bpf_prepare_cleanup_exceptions(env);
+ if (ret < 0)
+ goto skip_full_check;
+
/* Validate instructions and resolve the program's referenced resources. */
ret = check_and_resolve_insns(env);
if (ret < 0)
--
2.53.0-Meta
^ permalink raw reply related [flat|nested] 49+ messages in thread* Re: [PATCH bpf-next v5 05/21] bpf: Prepare for an exception cleanup table before the CFG walk
2026-09-23 4:59 ` [PATCH bpf-next v5 05/21] bpf: Prepare for an exception cleanup table before the CFG walk Yonghong Song
@ 2026-09-23 5:40 ` bot+bpf-ci
0 siblings, 0 replies; 49+ messages in thread
From: bot+bpf-ci @ 2026-09-23 5:40 UTC (permalink / raw)
To: yonghong.song, bpf
Cc: ast, andrii, daniel, eddyz87, kernel-team, ast, andrii, daniel,
martin.lau, eddyz87, yonghong.song, mason, ihor.solodrai
[-- Attachment #1: Type: text/plain, Size: 2632 bytes --]
> Record in insn_aux_data what the later passes need from the cleanup table:
> throw_call for every bpf_throw() and resume_call for every
> bpf_unwind_resume() -- the two calls a JIT lowers its own way rather than
> as calls -- and cleanup_pad, the landing pad a frame resumes at, for every
> call within the [begin_off, end_off) range of a cleanup record. Subsequent
> commits consume all three.
This isn't a bug, but would it help to say "for every call that can
unwind -- a BPF-to-BPF call or bpf_throw()" here, so the changelog
matches the filter in mark_call_sites()? The code marks only
bpf_pseudo_call() and bpf_is_throw_kfunc() inside the range, skipping
helper and kfunc calls that cannot unwind.
> diff --git a/include/linux/bpf_verifier.h b/include/linux/bpf_verifier.h
> index 666c73308b32..e12ec91b8150 100644
> --- a/include/linux/bpf_verifier.h
> +++ b/include/linux/bpf_verifier.h
> @@ -692,6 +692,8 @@ struct bpf_insn_aux_data {
> */
> u64 fastcall_spills_num:3;
> u64 arg_prog:4;
> + u64 throw_call:1; /* call to bpf_throw() */
> + u64 resume_call:1; /* call to bpf_unwind_resume() */
>
> /* below flags are initialized once */
> u64 jmp_point:1;
This isn't a bug, but would these two bits sit more naturally below the
"initialized once" comment, next to cleanup_pad which the same pass
fills in? Both throw_call and resume_call are set exactly once by
bpf_prepare_cleanup_exceptions() before any verification pass.
[ ... ]
> diff --git a/kernel/bpf/fixups.c b/kernel/bpf/fixups.c
> index e3459123803c..fed5b66ed3b7 100644
> --- a/kernel/bpf/fixups.c
> +++ b/kernel/bpf/fixups.c
> @@ -256,6 +256,12 @@ static void adjust_insn_aux_data(struct bpf_verifier_env *env,
> data[i].non_stack_access =
> data[off + cnt - 1].non_stack_access;
> data[off + cnt - 1].non_stack_access = false;
> + data[i].throw_call =
> + data[off + cnt - 1].throw_call;
> + data[off + cnt - 1].throw_call = false;
> + data[i].resume_call =
> + data[off + cnt - 1].resume_call;
> + data[off + cnt - 1].resume_call = false;
> data[i].cleanup_pad = data[off + cnt - 1].cleanup_pad;
> data[off + cnt - 1].cleanup_pad = 0;
> } else if (bpf_is_mem_insn(insn + i)) {
This isn't a bug, but could these two assignments be single lines like
the cleanup_pad one just below them? Unwrapped, they are 76 and 78
columns including the three tabs, so they fit.
---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md
CI run summary: https://github.com/kernel-patches/bpf/actions/runs/35821138073
^ permalink raw reply [flat|nested] 49+ messages in thread
* [PATCH bpf-next v5 06/21] bpf: Make exception landing pads reachable in the CFG
2026-09-23 4:58 [PATCH bpf-next v5 00/21] bpf: Run exception cleanup landing pads when bpf_throw() unwinds Yonghong Song
` (4 preceding siblings ...)
2026-09-23 4:59 ` [PATCH bpf-next v5 05/21] bpf: Prepare for an exception cleanup table before the CFG walk Yonghong Song
@ 2026-09-23 4:59 ` Yonghong Song
2026-09-23 5:51 ` bot+bpf-ci
2026-09-23 19:25 ` Eduard Zingerman
2026-09-23 4:59 ` [PATCH bpf-next v5 07/21] bpf: Explore the landing pads no call site reaches Yonghong Song
` (14 subsequent siblings)
20 siblings, 2 replies; 49+ messages in thread
From: Yonghong Song @ 2026-09-23 4:59 UTC (permalink / raw)
To: bpf
Cc: Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann,
Eduard Zingerman, kernel-team
A bpf_throw() or a bpf2bpf call inside the [begin_off, end_off) range of a
cleanup record can reach that record's landing pad. Add that edge to the
CFG walk, which explores the pad and makes both ends prune points, and to
bpf_insn_successors(), which liveness and the SCC passes walk.
Liveness needs one more thing. When bpf_stack_slot_alive()'s
is_live_before() says an outer frame's slot has no reader after the call
the frame is suspended at, the landing pad can still be one -- and usually
is, since only four registers survive a call and a pad reloads the rest.
Ask about the pad as well when the call site names one; otherwise
clean_verifier_state() poisons the slot while the callee runs and the pad
is rejected for reading it.
Signed-off-by: Yonghong Song <yonghong.song@linux.dev>
---
kernel/bpf/cfg.c | 54 ++++++++++++++++++++++++++++++++++++++++---
kernel/bpf/liveness.c | 16 +++++++++++++
2 files changed, 67 insertions(+), 3 deletions(-)
diff --git a/kernel/bpf/cfg.c b/kernel/bpf/cfg.c
index 842c7d1eabcc..aa56d38f2e37 100644
--- a/kernel/bpf/cfg.c
+++ b/kernel/bpf/cfg.c
@@ -6,6 +6,7 @@
#include <linux/sort.h>
#include "diagnostics.h"
+#include "exception.h"
#define verbose(env, fmt, args...) bpf_verifier_log_write(env, fmt, ##args)
@@ -158,17 +159,64 @@ static int push_insn(int t, int w, int e, struct bpf_verifier_env *env)
return DONE_EXPLORING;
}
+static int visit_cleanup_pad_edge(int t, struct bpf_verifier_env *env)
+{
+ int *insn_stack = env->cfg.insn_stack;
+ int *insn_state = env->cfg.insn_state;
+ int w;
+
+ if (!env->cleanup_info_cnt)
+ return DONE_EXPLORING;
+ w = bpf_exc_pad_of_call(env, t);
+ if (w < 0)
+ return DONE_EXPLORING;
+
+ /*
+ * @t is a call that may branch here, and @w is the target of that
+ * branch, so both are prune points. @w especially: every covered call
+ * site in a region unwinds to the same pad, and without a prune point
+ * at its head the verifier walks the pad again for each of them.
+ */
+ mark_prune_point(env, t);
+ mark_prune_point(env, w);
+ mark_jmp_point(env, w);
+ mark_jump_target(env, w);
+
+ if (insn_state[w])
+ return DONE_EXPLORING;
+ if (env->cfg.cur_stack >= env->prog->len)
+ return -E2BIG;
+ insn_stack[env->cfg.cur_stack++] = w;
+ insn_state[w] |= DISCOVERED;
+ return KEEP_EXPLORING;
+}
+
+static int merge_visit_ret(int a, int b)
+{
+ if (a < 0)
+ return a;
+ if (b < 0)
+ return b;
+ if (a == KEEP_EXPLORING || b == KEEP_EXPLORING)
+ return KEEP_EXPLORING;
+ return DONE_EXPLORING;
+}
+
static int visit_func_call_insn(int t, struct bpf_insn *insns,
struct bpf_verifier_env *env,
bool visit_callee)
{
- int ret, insn_sz;
+ int ret, insn_sz, pad_ret;
int w;
+ pad_ret = visit_cleanup_pad_edge(t, env);
+ if (pad_ret < 0)
+ return pad_ret;
+
insn_sz = bpf_is_ldimm64(&insns[t]) ? 2 : 1;
ret = push_insn(t, t + insn_sz, FALLTHROUGH, env);
if (ret)
- return ret;
+ return merge_visit_ret(pad_ret, ret);
mark_prune_point(env, t + insn_sz);
/* when we exit from subprog, we need to record non-linear history */
@@ -180,7 +228,7 @@ static int visit_func_call_insn(int t, struct bpf_insn *insns,
merge_callee_effects(env, t, w);
ret = push_insn(t, w, BRANCH, env);
}
- return ret;
+ return merge_visit_ret(pad_ret, ret);
}
struct bpf_iarray *bpf_iarray_realloc(struct bpf_iarray *old, size_t n_elem)
diff --git a/kernel/bpf/liveness.c b/kernel/bpf/liveness.c
index 44ecdc5b4ec2..9d44a7dafe5f 100644
--- a/kernel/bpf/liveness.c
+++ b/kernel/bpf/liveness.c
@@ -8,6 +8,8 @@
#include <linux/slab.h>
#include <linux/sort.h>
+#include "exception.h"
+
#define verbose(env, fmt, args...) bpf_verifier_log_write(env, fmt, ##args)
struct per_frame_masks {
@@ -264,6 +266,13 @@ bpf_insn_successors(struct bpf_verifier_env *env, u32 idx)
if (opcode_info->can_jump)
succ->items[succ->cnt++] = idx + bpf_jmp_offset(insn) + 1;
+ if (unlikely(env->cleanup_info_cnt)) {
+ int pad = bpf_exc_pad_of_call(env, idx);
+
+ if (pad >= 0)
+ succ->items[succ->cnt++] = pad;
+ }
+
return succ;
}
@@ -397,6 +406,13 @@ bool bpf_stack_slot_alive(struct bpf_verifier_env *env, u32 frameno, u32 half_sp
alive = bpf_calls_callback(env, callsite)
? is_live_before(instance, callsite, rel, half_spi)
: is_live_before(instance, callsite + 1, rel, half_spi);
+
+ if (!alive && unlikely(env->cleanup_info_cnt)) {
+ int pad = bpf_exc_pad_of_call(env, callsite);
+
+ if (pad >= 0)
+ alive = is_live_before(instance, pad, rel, half_spi);
+ }
if (alive)
return true;
}
--
2.53.0-Meta
^ permalink raw reply related [flat|nested] 49+ messages in thread* Re: [PATCH bpf-next v5 06/21] bpf: Make exception landing pads reachable in the CFG
2026-09-23 4:59 ` [PATCH bpf-next v5 06/21] bpf: Make exception landing pads reachable in the CFG Yonghong Song
@ 2026-09-23 5:51 ` bot+bpf-ci
2026-09-23 19:25 ` Eduard Zingerman
1 sibling, 0 replies; 49+ messages in thread
From: bot+bpf-ci @ 2026-09-23 5:51 UTC (permalink / raw)
To: yonghong.song, bpf
Cc: ast, andrii, daniel, eddyz87, kernel-team, ast, andrii, daniel,
martin.lau, eddyz87, yonghong.song, mason, ihor.solodrai
[-- Attachment #1: Type: text/plain, Size: 1322 bytes --]
> bpf: Make exception landing pads reachable in the CFG
>
> A bpf_throw() or a bpf2bpf call inside the [begin_off, end_off) range of a
> cleanup record can reach that record's landing pad. Add that edge to the
> CFG walk, which explores the pad and makes both ends prune points, and to
> bpf_insn_successors(), which liveness and the SCC passes walk.
>
> Liveness needs one more thing. When bpf_stack_slot_alive()'s
> is_live_before() says an outer frame's slot has no reader after the call
> the frame is suspended at, the landing pad can still be one -- and usually
> is, since only four registers survive a call and a pad reloads the rest.
> Ask about the pad as well when the call site names one; otherwise
> clean_verifier_state() poisons the slot while the callee runs and the pad
> is rejected for reading it.
>
> Signed-off-by: Yonghong Song <yonghong.song@linux.dev>
This looks like a bug fix that completes exception handling support from
an earlier commit. Should this include:
Fixes: 6e225ddc8aa6 ("bpf: Prepare for an exception cleanup table before the CFG walk")
---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md
CI run summary: https://github.com/kernel-patches/bpf/actions/runs/35821138073
^ permalink raw reply [flat|nested] 49+ messages in thread* Re: [PATCH bpf-next v5 06/21] bpf: Make exception landing pads reachable in the CFG
2026-09-23 4:59 ` [PATCH bpf-next v5 06/21] bpf: Make exception landing pads reachable in the CFG Yonghong Song
2026-09-23 5:51 ` bot+bpf-ci
@ 2026-09-23 19:25 ` Eduard Zingerman
1 sibling, 0 replies; 49+ messages in thread
From: Eduard Zingerman @ 2026-09-23 19:25 UTC (permalink / raw)
To: Yonghong Song, bpf
Cc: Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann, kernel-team
On Tue, 2026-09-22 at 21:59 -0700, Yonghong Song wrote:
...
> diff --git a/kernel/bpf/liveness.c b/kernel/bpf/liveness.c
> index 44ecdc5b4ec2..9d44a7dafe5f 100644
> --- a/kernel/bpf/liveness.c
> +++ b/kernel/bpf/liveness.c
...
> @@ -397,6 +406,13 @@ bool bpf_stack_slot_alive(struct bpf_verifier_env *env, u32 frameno, u32 half_sp
> alive = bpf_calls_callback(env, callsite)
> ? is_live_before(instance, callsite, rel, half_spi)
> : is_live_before(instance, callsite + 1, rel, half_spi);
> +
> + if (!alive && unlikely(env->cleanup_info_cnt)) {
> + int pad = bpf_exc_pad_of_call(env, callsite);
> +
> + if (pad >= 0)
> + alive = is_live_before(instance, pad, rel, half_spi);
> + }
Why is this hunk necessary?
If bpf_insn_succ correctly traverses call successors including the
landing pad the information should already be accounted for by the
is_live_before(instance, callsite, rel, half_spi).
> if (alive)
> return true;
> }
^ permalink raw reply [flat|nested] 49+ messages in thread
* [PATCH bpf-next v5 07/21] bpf: Explore the landing pads no call site reaches
2026-09-23 4:58 [PATCH bpf-next v5 00/21] bpf: Run exception cleanup landing pads when bpf_throw() unwinds Yonghong Song
` (5 preceding siblings ...)
2026-09-23 4:59 ` [PATCH bpf-next v5 06/21] bpf: Make exception landing pads reachable in the CFG Yonghong Song
@ 2026-09-23 4:59 ` Yonghong Song
2026-09-23 16:26 ` Eduard Zingerman
2026-09-23 4:59 ` [PATCH bpf-next v5 08/21] bpf: Walk the exception unwind in the verifier Yonghong Song
` (13 subsequent siblings)
20 siblings, 1 reply; 49+ messages in thread
From: Yonghong Song @ 2026-09-23 4:59 UTC (permalink / raw)
To: bpf
Cc: Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann,
Eduard Zingerman, kernel-team
A cleanup record need not cover a call an exception can unwind out of: a
frontend is free to emit a region around a helper or an ordinary kfunc,
both nounwind here. Nothing marks a call site then, and that record's
landing pad is reached by nothing at all -- leaving bpf_check_cfg() to
refuse the program over code its own frontend had no way not to emit:
0: call bpf_preempt_disable
1: call bpf_preempt_enable record = { begin = 1, end = 2, pad = 4 }
2: r0 = 0
3: exit
4: r1 = pads_ran ll landing pad
6: r2 = *(u64 *)(r1 + 0)
7: r2 |= RAN_NOUNWIND_REC
8: *(u64 *)(r1 + 0) = r2
9: call bpf_unwind_resume
10: exit
The range [1,2) holds one call, and it is a kfunc, so an exception cannot
come out of it. mark_call_sites() marks nothing, nothing pushes an edge to
4, and 4 through 10 are reachable from nothing: "unreachable insn 4". The
pad is dead, which is correct -- no exception can ever arrive at it -- but
the program is fine and has to load.
Walk every pad the table names that the edges did not reach, the way the
walk is already re-seeded at an exception callback. From there the pad is
code like any other: do_check() never enters it, because no call site
dispatches to it, so the dead code sweep removes it along with everything
else that was not reached.
Signed-off-by: Yonghong Song <yonghong.song@linux.dev>
---
kernel/bpf/cfg.c | 17 +++++++++++++++++
1 file changed, 17 insertions(+)
diff --git a/kernel/bpf/cfg.c b/kernel/bpf/cfg.c
index aa56d38f2e37..1c3b4165e18c 100644
--- a/kernel/bpf/cfg.c
+++ b/kernel/bpf/cfg.c
@@ -640,6 +640,7 @@ int bpf_check_cfg(struct bpf_verifier_env *env)
int insn_cnt = env->prog->len;
int *insn_stack, *insn_state;
int ex_insn_beg, i, ret = 0;
+ u32 pad_idx = 0;
insn_state = env->cfg.insn_state = kvzalloc_objs(int, insn_cnt,
GFP_KERNEL_ACCOUNT);
@@ -695,6 +696,22 @@ int bpf_check_cfg(struct bpf_verifier_env *env)
goto walk_cfg;
}
+ /*
+ * A landing pad no call site was marked with -- a record whose range
+ * holds no bpf2bpf call and no bpf_throw() -- is reached by nothing.
+ * Walk it from here, and let the dead code sweep remove it.
+ */
+ while (pad_idx < env->cleanup_info_cnt) {
+ u32 pad = env->cleanup_info[pad_idx++].landing_pad_off;
+
+ if (insn_state[pad] != EXPLORED) {
+ insn_state[pad] = DISCOVERED;
+ insn_stack[0] = pad;
+ env->cfg.cur_stack = 1;
+ goto walk_cfg;
+ }
+ }
+
for (i = 0; i < insn_cnt; i++) {
struct bpf_insn *insn = &env->prog->insnsi[i];
--
2.53.0-Meta
^ permalink raw reply related [flat|nested] 49+ messages in thread* Re: [PATCH bpf-next v5 07/21] bpf: Explore the landing pads no call site reaches
2026-09-23 4:59 ` [PATCH bpf-next v5 07/21] bpf: Explore the landing pads no call site reaches Yonghong Song
@ 2026-09-23 16:26 ` Eduard Zingerman
2026-09-23 17:27 ` Yonghong Song
2026-09-23 21:28 ` Alexei Starovoitov
0 siblings, 2 replies; 49+ messages in thread
From: Eduard Zingerman @ 2026-09-23 16:26 UTC (permalink / raw)
To: Yonghong Song, bpf
Cc: Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann, kernel-team
On Tue, 2026-09-22 at 21:59 -0700, Yonghong Song wrote:
> A cleanup record need not cover a call an exception can unwind out of: a
> frontend is free to emit a region around a helper or an ordinary kfunc,
> both nounwind here. Nothing marks a call site then, and that record's
> landing pad is reached by nothing at all -- leaving bpf_check_cfg() to
> refuse the program over code its own frontend had no way not to emit:
>
> 0: call bpf_preempt_disable
> 1: call bpf_preempt_enable record = { begin = 1, end = 2, pad = 4 }
> 2: r0 = 0
> 3: exit
> 4: r1 = pads_ran ll landing pad
> 6: r2 = *(u64 *)(r1 + 0)
> 7: r2 |= RAN_NOUNWIND_REC
> 8: *(u64 *)(r1 + 0) = r2
> 9: call bpf_unwind_resume
> 10: exit
>
> The range [1,2) holds one call, and it is a kfunc, so an exception cannot
> come out of it. mark_call_sites() marks nothing, nothing pushes an edge to
> 4, and 4 through 10 are reachable from nothing: "unreachable insn 4". The
> pad is dead, which is correct -- no exception can ever arrive at it -- but
> the program is fine and has to load.
>
> Walk every pad the table names that the edges did not reach, the way the
> walk is already re-seeded at an exception callback. From there the pad is
> code like any other: do_check() never enters it, because no call site
> dispatches to it, so the dead code sweep removes it along with everything
> else that was not reached.
>
> Signed-off-by: Yonghong Song <yonghong.song@linux.dev>
> ---
Yonghong, in v4 you said that rustc does not generate dead landing pads.
Why do we need this patch?
...
^ permalink raw reply [flat|nested] 49+ messages in thread* Re: [PATCH bpf-next v5 07/21] bpf: Explore the landing pads no call site reaches
2026-09-23 16:26 ` Eduard Zingerman
@ 2026-09-23 17:27 ` Yonghong Song
2026-09-23 18:06 ` Eduard Zingerman
2026-09-23 21:28 ` Alexei Starovoitov
1 sibling, 1 reply; 49+ messages in thread
From: Yonghong Song @ 2026-09-23 17:27 UTC (permalink / raw)
To: Eduard Zingerman, bpf
Cc: Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann, kernel-team
On 9/23/26 9:26 AM, Eduard Zingerman wrote:
> On Tue, 2026-09-22 at 21:59 -0700, Yonghong Song wrote:
>> A cleanup record need not cover a call an exception can unwind out of: a
>> frontend is free to emit a region around a helper or an ordinary kfunc,
>> both nounwind here. Nothing marks a call site then, and that record's
>> landing pad is reached by nothing at all -- leaving bpf_check_cfg() to
>> refuse the program over code its own frontend had no way not to emit:
>>
>> 0: call bpf_preempt_disable
>> 1: call bpf_preempt_enable record = { begin = 1, end = 2, pad = 4 }
>> 2: r0 = 0
>> 3: exit
>> 4: r1 = pads_ran ll landing pad
>> 6: r2 = *(u64 *)(r1 + 0)
>> 7: r2 |= RAN_NOUNWIND_REC
>> 8: *(u64 *)(r1 + 0) = r2
>> 9: call bpf_unwind_resume
>> 10: exit
>>
>> The range [1,2) holds one call, and it is a kfunc, so an exception cannot
>> come out of it. mark_call_sites() marks nothing, nothing pushes an edge to
>> 4, and 4 through 10 are reachable from nothing: "unreachable insn 4". The
>> pad is dead, which is correct -- no exception can ever arrive at it -- but
>> the program is fine and has to load.
>>
>> Walk every pad the table names that the edges did not reach, the way the
>> walk is already re-seeded at an exception callback. From there the pad is
>> code like any other: do_check() never enters it, because no call site
>> dispatches to it, so the dead code sweep removes it along with everything
>> else that was not reached.
>>
>> Signed-off-by: Yonghong Song <yonghong.song@linux.dev>
>> ---
> Yonghong, in v4 you said that rustc does not generate dead landing pads.
> Why do we need this patch?
This patch is to avoid a verification failure. Let us say, we remove this
patch and run the following
./test_progs -t exceptions_cleanup
and we will get the following failure:
test_exceptions_cleanup:PASS:open 0 nsec
test_light_skeleton:PASS:light open_and_load 0 nsec
test_light_skeleton:PASS:run 0 nsec
test_light_skeleton:PASS:retval 0 nsec
test_light_skeleton:PASS:pads_ran 0 nsec
libbpf: prog 'entry_nounwind_rec': BPF program load failed: -EINVAL
libbpf: prog 'entry_nounwind_rec': -- BEGIN PROG LOAD LOG --
unreachable insn 6
Verification failed: Program Structure: Unreachable instruction
Reason:
Instruction 6 is not reachable from the program entry point.
At:
nounwind_rec_frame @ exceptions_cleanup_shapes.c:663:2
Source context:
661 | ...
662 | ...
>>> 663 | asm volatile (
| ^-- error: unreachable instruction
664 | ...
665 | ...
Instruction context:
4 | (b7) r0 = 0
5 | (95) exit
>>> 6 | (18) r1 = 0xffa0000001d94010
8 | (79) r2 = *(u64 *)(r1 +0)
Suggestion:
Remove the unreachable instruction or add valid control flow that reaches it.
processed 0 insns (limit 1000000) max_states_per_insn 0 total_states 0 peak_states 0 mark_read 0
-- END PROG LOAD LOG --
libbpf: prog 'entry_nounwind_rec': failed to load: -EINVAL
libbpf: failed to load object 'exceptions_cleanup_shapes'
libbpf: failed to load BPF skeleton 'exceptions_cleanup_shapes': -EINVAL
test_shapes:FAIL:shapes open_and_load unexpected error: -22
tester_init:PASS:tester_log_buf 0 nsec
process_subtest:PASS:obj_open_mem 0 nsec
process_subtest:PASS:specs_alloc 0 nsec
#128/4 exceptions_cleanup/light_skeleton:FAIL
#128 exceptions_cleanup:FAIL
This happens in cfg.c:
for (i = 0; i < insn_cnt; i++) {
struct bpf_insn *insn = &env->prog->insnsi[i];
if (insn_state[i] != EXPLORED) {
verbose(env, "unreachable insn %d\n", i);
bpf_diag_program_structure(
env, i, "unreachable instruction",
"Remove the unreachable instruction or add valid control flow that reaches it.",
"Instruction %d is not reachable from the program entry point.", i);
ret = -EINVAL;
goto err_free;
}
if (bpf_is_ldimm64(insn)) {
if (insn_state[i + 1] != 0) {
verbose(env, "jump into the middle of ldimm64 insn %d\n", i);
bpf_diag_program_structure(
env, i, "jump into ldimm64 immediate",
"Target the first instruction of the ldimm64 pair, or restructure the jump target.",
"Control flow reaches the second half of the ldimm64 instruction pair that starts at instruction %d.",
i);
ret = -EINVAL;
goto err_free;
}
i++; /* skip second half of ldimm64 */
}
}
This patch intends to explore *unreachable* insns to avoid verification failure.
I think due to "record = { begin = 1, end = 2, pad = 4 }", it is considered that the code at 'pad = 4'
is not dead, but actually it does dead later so we have the above failure.
So I add this patch to avoid failure. But maybe we should just remove this patch,
and mark this test as failure as indeed landing pad is not reachable.
WDYT?
^ permalink raw reply [flat|nested] 49+ messages in thread* Re: [PATCH bpf-next v5 07/21] bpf: Explore the landing pads no call site reaches
2026-09-23 17:27 ` Yonghong Song
@ 2026-09-23 18:06 ` Eduard Zingerman
2026-09-23 18:09 ` Yonghong Song
0 siblings, 1 reply; 49+ messages in thread
From: Eduard Zingerman @ 2026-09-23 18:06 UTC (permalink / raw)
To: Yonghong Song, bpf
Cc: Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann, kernel-team
On Wed, 2026-09-23 at 10:27 -0700, Yonghong Song wrote:
...
> This patch is to avoid a verification failure. Let us say, we remove this
> patch and run the following
> ./test_progs -t exceptions_cleanup
> and we will get the following failure:
...
> This patch intends to explore *unreachable* insns to avoid verification failure.
>
> I think due to "record = { begin = 1, end = 2, pad = 4 }", it is considered that the code at 'pad = 4'
> is not dead, but actually it does dead later so we have the above failure.
>
> So I add this patch to avoid failure. But maybe we should just remove this patch,
> and mark this test as failure as indeed landing pad is not reachable.
>
> WDYT?
But that's a self-fulfilling thing, the test you are pointing at looks
as follows:
/*
* 15. A region around a call the kernel knows cannot unwind: no call site is
* marked, nothing reaches the pad, and the sweep removes it. The program is
* otherwise ordinary and has to load.
*/
static __used __naked __noinline __u64 nounwind_rec_frame(void)
{
...
}
So, the test is added to test this cfg pass, the cfg pass is added to
make this test pass, meanwhile compiler does not generate such code.
I don't see a point, tbh.
^ permalink raw reply [flat|nested] 49+ messages in thread* Re: [PATCH bpf-next v5 07/21] bpf: Explore the landing pads no call site reaches
2026-09-23 18:06 ` Eduard Zingerman
@ 2026-09-23 18:09 ` Yonghong Song
0 siblings, 0 replies; 49+ messages in thread
From: Yonghong Song @ 2026-09-23 18:09 UTC (permalink / raw)
To: Eduard Zingerman, bpf
Cc: Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann, kernel-team
On 9/23/26 11:06 AM, Eduard Zingerman wrote:
> On Wed, 2026-09-23 at 10:27 -0700, Yonghong Song wrote:
>
> ...
>
>> This patch is to avoid a verification failure. Let us say, we remove this
>> patch and run the following
>> ./test_progs -t exceptions_cleanup
>> and we will get the following failure:
> ...
>
>> This patch intends to explore *unreachable* insns to avoid verification failure.
>>
>> I think due to "record = { begin = 1, end = 2, pad = 4 }", it is considered that the code at 'pad = 4'
>> is not dead, but actually it does dead later so we have the above failure.
>>
>> So I add this patch to avoid failure. But maybe we should just remove this patch,
>> and mark this test as failure as indeed landing pad is not reachable.
>>
>> WDYT?
> But that's a self-fulfilling thing, the test you are pointing at looks
> as follows:
>
> /*
> * 15. A region around a call the kernel knows cannot unwind: no call site is
> * marked, nothing reaches the pad, and the sweep removes it. The program is
> * otherwise ordinary and has to load.
> */
> static __used __naked __noinline __u64 nounwind_rec_frame(void)
> {
> ...
> }
>
> So, the test is added to test this cfg pass, the cfg pass is added to
> make this test pass, meanwhile compiler does not generate such code.
> I don't see a point, tbh.
Confirmed. Will remove this patch.
^ permalink raw reply [flat|nested] 49+ messages in thread
* Re: [PATCH bpf-next v5 07/21] bpf: Explore the landing pads no call site reaches
2026-09-23 16:26 ` Eduard Zingerman
2026-09-23 17:27 ` Yonghong Song
@ 2026-09-23 21:28 ` Alexei Starovoitov
2026-09-23 22:19 ` Eduard Zingerman
2026-09-23 23:09 ` Yonghong Song
1 sibling, 2 replies; 49+ messages in thread
From: Alexei Starovoitov @ 2026-09-23 21:28 UTC (permalink / raw)
To: Eduard Zingerman, Yonghong Song, bpf
Cc: Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann, kernel-team
On Wed Sep 23, 2026 at 4:26 PM UTC, Eduard Zingerman wrote:
> On Tue, 2026-09-22 at 21:59 -0700, Yonghong Song wrote:
>> A cleanup record need not cover a call an exception can unwind out of: a
>> frontend is free to emit a region around a helper or an ordinary kfunc,
>> both nounwind here. Nothing marks a call site then, and that record's
>> landing pad is reached by nothing at all -- leaving bpf_check_cfg() to
>> refuse the program over code its own frontend had no way not to emit:
>>
>> 0: call bpf_preempt_disable
>> 1: call bpf_preempt_enable record = { begin = 1, end = 2, pad = 4 }
>> 2: r0 = 0
>> 3: exit
>> 4: r1 = pads_ran ll landing pad
>> 6: r2 = *(u64 *)(r1 + 0)
>> 7: r2 |= RAN_NOUNWIND_REC
>> 8: *(u64 *)(r1 + 0) = r2
>> 9: call bpf_unwind_resume
>> 10: exit
>>
>> The range [1,2) holds one call, and it is a kfunc, so an exception cannot
>> come out of it. mark_call_sites() marks nothing, nothing pushes an edge to
>> 4, and 4 through 10 are reachable from nothing: "unreachable insn 4". The
>> pad is dead, which is correct -- no exception can ever arrive at it -- but
>> the program is fine and has to load.
>>
>> Walk every pad the table names that the edges did not reach, the way the
>> walk is already re-seeded at an exception callback. From there the pad is
>> code like any other: do_check() never enters it, because no call site
>> dispatches to it, so the dead code sweep removes it along with everything
>> else that was not reached.
>>
>> Signed-off-by: Yonghong Song <yonghong.song@linux.dev>
>> ---
>
> Yonghong, in v4 you said that rustc does not generate dead landing pads.
> Why do we need this patch?
It's not dead. commit log is incorrect.
I bet C code that caused such asm didn't mark bpf_preempt_enable as nothrow,
so compiler correctly emitted exception table.
Now commit log argues that this helper doesn't throw in reality.
Well, too late, the mistake was made earlier.
I don't like the unwind approach taken by later patches in the series.
imo the following is cleaner:
- do not repurpose bpf_throw() for this new thing.
Introduce new kfunc that will do the unwind, let's name it bpf_unwind() ?
- replace all 'call bpf_unwind_resume' with NOP by the verifier (or may with bpf_exit. tbd)
Technically rustc or llvm can do that too, but it's cleaner to do in the verifier.
- In bpf_unwind() walk all exception tables and replace return addresses
in corresponding frames to landing_pad_ip-s.
- just return from bpf_unwind().
restoring callee saved registers will happen automatically by corresponding
frames and there is no need to search exception tables at each step.
That's what typical eh unwinder does, but it's doing it due to C++ logic
that is more complex that Rust. For Rust unwinds we don't need all that.
The above algorithm will do.
Also please add an example where [ip_start, ip_end] range is more than just a call
insn. I mentioned it couple times as the reason why 'invoke' aka 16-byte insn
approach doesn't work.
I'm pretty sure I saw such IP ranges generated by rustc.
^ permalink raw reply [flat|nested] 49+ messages in thread* Re: [PATCH bpf-next v5 07/21] bpf: Explore the landing pads no call site reaches
2026-09-23 21:28 ` Alexei Starovoitov
@ 2026-09-23 22:19 ` Eduard Zingerman
2026-09-23 23:20 ` Alexei Starovoitov
2026-09-23 23:09 ` Yonghong Song
1 sibling, 1 reply; 49+ messages in thread
From: Eduard Zingerman @ 2026-09-23 22:19 UTC (permalink / raw)
To: Alexei Starovoitov, Yonghong Song, bpf
Cc: Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann, kernel-team
On Wed, 2026-09-23 at 21:28 +0000, Alexei Starovoitov wrote:
...
> imo the following is cleaner:
> - do not repurpose bpf_throw() for this new thing.
> Introduce new kfunc that will do the unwind, let's name it bpf_unwind() ?
> - replace all 'call bpf_unwind_resume' with NOP by the verifier (or may with bpf_exit. tbd)
> Technically rustc or llvm can do that too, but it's cleaner to do in the verifier.
> - In bpf_unwind() walk all exception tables and replace return addresses
> in corresponding frames to landing_pad_ip-s.
> - just return from bpf_unwind().
> restoring callee saved registers will happen automatically by corresponding
> frames and there is no need to search exception tables at each step.
> That's what typical eh unwinder does, but it's doing it due to C++ logic
> that is more complex that Rust. For Rust unwinds we don't need all that.
> The above algorithm will do.
+1, makes sense.
After return address rewrite the unwind would have to jump to it's epilogue, right?
Why not reusing bpf_throw() though, is it because of the exception cb mechanics?
^ permalink raw reply [flat|nested] 49+ messages in thread
* Re: [PATCH bpf-next v5 07/21] bpf: Explore the landing pads no call site reaches
2026-09-23 22:19 ` Eduard Zingerman
@ 2026-09-23 23:20 ` Alexei Starovoitov
2026-09-24 0:13 ` Kumar Kartikeya Dwivedi
0 siblings, 1 reply; 49+ messages in thread
From: Alexei Starovoitov @ 2026-09-23 23:20 UTC (permalink / raw)
To: Eduard Zingerman, Yonghong Song, bpf
Cc: Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann, kernel-team
On Wed Sep 23, 2026 at 10:19 PM UTC, Eduard Zingerman wrote:
> On Wed, 2026-09-23 at 21:28 +0000, Alexei Starovoitov wrote:
>
> ...
>
>> imo the following is cleaner:
>> - do not repurpose bpf_throw() for this new thing.
>> Introduce new kfunc that will do the unwind, let's name it bpf_unwind() ?
>> - replace all 'call bpf_unwind_resume' with NOP by the verifier (or may with bpf_exit. tbd)
>> Technically rustc or llvm can do that too, but it's cleaner to do in the verifier.
>> - In bpf_unwind() walk all exception tables and replace return addresses
>> in corresponding frames to landing_pad_ip-s.
>> - just return from bpf_unwind().
>> restoring callee saved registers will happen automatically by corresponding
>> frames and there is no need to search exception tables at each step.
>> That's what typical eh unwinder does, but it's doing it due to C++ logic
>> that is more complex that Rust. For Rust unwinds we don't need all that.
>> The above algorithm will do.
>
> +1, makes sense.
> After return address rewrite the unwind would have to jump to it's epilogue, right?
yes. I feel there will be no need in asm() tricks in such kfunc.
unwind will rewrite its own return address, and will return through normal C
and will restore callee saved (whatever it needed),
then immediate callee in bpf prog should have had a landing pad for this kfunc,
since this kfunc is throwing.
so bpf_unwind() will jump to whatever is necessary to unwind in that bpf prog.
And so on till the end.
> Why not reusing bpf_throw() though, is it because of the exception cb mechanics?
yes. bpf_throw() is not seen as throwing from compiler pov.
I believe all kfuncs are not throwing. (that was a bug in this patch I alluded earlier).
So we need a new kfunc and mark it attr(may_throw) or whatever the attr is called,
so that compiler will generate exception tables for that bpf prog.
Last time I checked there were bpf_cleanup section for rust-c in that case,
but that was back in April.
There is also a case of 'catching' abort in rust. iirc it only stops unwind and
rust-c generates something else instead of 'call bpf_unwind_resume'.
Memory is vague. I think that frame will continue as normal after processing
landing pad drop()s.
^ permalink raw reply [flat|nested] 49+ messages in thread
* Re: [PATCH bpf-next v5 07/21] bpf: Explore the landing pads no call site reaches
2026-09-23 23:20 ` Alexei Starovoitov
@ 2026-09-24 0:13 ` Kumar Kartikeya Dwivedi
2026-09-24 1:53 ` Alexei Starovoitov
0 siblings, 1 reply; 49+ messages in thread
From: Kumar Kartikeya Dwivedi @ 2026-09-24 0:13 UTC (permalink / raw)
To: Alexei Starovoitov, Eduard Zingerman, Yonghong Song, bpf
Cc: Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann, kernel-team
On Thu Sep 24, 2026 at 1:20 AM CEST, Alexei Starovoitov wrote:
> On Wed Sep 23, 2026 at 10:19 PM UTC, Eduard Zingerman wrote:
>> On Wed, 2026-09-23 at 21:28 +0000, Alexei Starovoitov wrote:
>>
>> ...
>>
>>> imo the following is cleaner:
>>> - do not repurpose bpf_throw() for this new thing.
>>> Introduce new kfunc that will do the unwind, let's name it bpf_unwind() ?
>>> - replace all 'call bpf_unwind_resume' with NOP by the verifier (or may with bpf_exit. tbd)
>>> Technically rustc or llvm can do that too, but it's cleaner to do in the verifier.
>>> - In bpf_unwind() walk all exception tables and replace return addresses
>>> in corresponding frames to landing_pad_ip-s.
>>> - just return from bpf_unwind().
>>> restoring callee saved registers will happen automatically by corresponding
>>> frames and there is no need to search exception tables at each step.
>>> That's what typical eh unwinder does, but it's doing it due to C++ logic
>>> that is more complex that Rust. For Rust unwinds we don't need all that.
>>> The above algorithm will do.
>>
>> +1, makes sense.
>> After return address rewrite the unwind would have to jump to it's epilogue, right?
>
> yes. I feel there will be no need in asm() tricks in such kfunc.
> unwind will rewrite its own return address, and will return through normal C
> and will restore callee saved (whatever it needed),
> then immediate callee in bpf prog should have had a landing pad for this kfunc,
> since this kfunc is throwing.
> so bpf_unwind() will jump to whatever is necessary to unwind in that bpf prog.
> And so on till the end.
>
>> Why not reusing bpf_throw() though, is it because of the exception cb mechanics?
>
> yes. bpf_throw() is not seen as throwing from compiler pov.
> I believe all kfuncs are not throwing. (that was a bug in this patch I alluded earlier).
> So we need a new kfunc and mark it attr(may_throw) or whatever the attr is called,
> so that compiler will generate exception tables for that bpf prog.
> Last time I checked there were bpf_cleanup section for rust-c in that case,
> but that was back in April.
Hm, isn't this dependent on how you annotate the kfunc on the Rust side? Like,
you could make the same annotation on bpf_throw() that you would for the new
kfunc, no? I got confused by this part.
>
> There is also a case of 'catching' abort in rust. iirc it only stops unwind and
> rust-c generates something else instead of 'call bpf_unwind_resume'.
> Memory is vague. I think that frame will continue as normal after processing
> landing pad drop()s.
^ permalink raw reply [flat|nested] 49+ messages in thread
* Re: [PATCH bpf-next v5 07/21] bpf: Explore the landing pads no call site reaches
2026-09-24 0:13 ` Kumar Kartikeya Dwivedi
@ 2026-09-24 1:53 ` Alexei Starovoitov
2026-09-24 6:12 ` Kumar Kartikeya Dwivedi
0 siblings, 1 reply; 49+ messages in thread
From: Alexei Starovoitov @ 2026-09-24 1:53 UTC (permalink / raw)
To: Kumar Kartikeya Dwivedi, Eduard Zingerman, Yonghong Song, bpf
Cc: Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann, kernel-team
On Thu Sep 24, 2026 at 12:13 AM UTC, Kumar Kartikeya Dwivedi wrote:
> On Thu Sep 24, 2026 at 1:20 AM CEST, Alexei Starovoitov wrote:
>> On Wed Sep 23, 2026 at 10:19 PM UTC, Eduard Zingerman wrote:
>>> On Wed, 2026-09-23 at 21:28 +0000, Alexei Starovoitov wrote:
>>>
>>> ...
>>>
>>>> imo the following is cleaner:
>>>> - do not repurpose bpf_throw() for this new thing.
>>>> Introduce new kfunc that will do the unwind, let's name it bpf_unwind() ?
>>>> - replace all 'call bpf_unwind_resume' with NOP by the verifier (or may with bpf_exit. tbd)
>>>> Technically rustc or llvm can do that too, but it's cleaner to do in the verifier.
>>>> - In bpf_unwind() walk all exception tables and replace return addresses
>>>> in corresponding frames to landing_pad_ip-s.
>>>> - just return from bpf_unwind().
>>>> restoring callee saved registers will happen automatically by corresponding
>>>> frames and there is no need to search exception tables at each step.
>>>> That's what typical eh unwinder does, but it's doing it due to C++ logic
>>>> that is more complex that Rust. For Rust unwinds we don't need all that.
>>>> The above algorithm will do.
>>>
>>> +1, makes sense.
>>> After return address rewrite the unwind would have to jump to it's epilogue, right?
>>
>> yes. I feel there will be no need in asm() tricks in such kfunc.
>> unwind will rewrite its own return address, and will return through normal C
>> and will restore callee saved (whatever it needed),
>> then immediate callee in bpf prog should have had a landing pad for this kfunc,
>> since this kfunc is throwing.
>> so bpf_unwind() will jump to whatever is necessary to unwind in that bpf prog.
>> And so on till the end.
>>
>>> Why not reusing bpf_throw() though, is it because of the exception cb mechanics?
>>
>> yes. bpf_throw() is not seen as throwing from compiler pov.
>> I believe all kfuncs are not throwing. (that was a bug in this patch I alluded earlier).
>> So we need a new kfunc and mark it attr(may_throw) or whatever the attr is called,
>> so that compiler will generate exception tables for that bpf prog.
>> Last time I checked there were bpf_cleanup section for rust-c in that case,
>> but that was back in April.
>
> Hm, isn't this dependent on how you annotate the kfunc on the Rust side? Like,
> you could make the same annotation on bpf_throw() that you would for the new
> kfunc, no? I got confused by this part.
bpf_throw() has a specific semantic already.
Like it suppose to call that bpf prog callback once it unwinds
everything.
This new kfunc is different. It will initiate the unwind the way rust wants it to
and may stop in the middle.
See below point about 'catching'.
>> There is also a case of 'catching' abort in rust. iirc it only stops unwind and
>> rust-c generates something else instead of 'call bpf_unwind_resume'.
>> Memory is vague. I think that frame will continue as normal after processing
>> landing pad drop()s.
^ permalink raw reply [flat|nested] 49+ messages in thread
* Re: [PATCH bpf-next v5 07/21] bpf: Explore the landing pads no call site reaches
2026-09-24 1:53 ` Alexei Starovoitov
@ 2026-09-24 6:12 ` Kumar Kartikeya Dwivedi
0 siblings, 0 replies; 49+ messages in thread
From: Kumar Kartikeya Dwivedi @ 2026-09-24 6:12 UTC (permalink / raw)
To: Alexei Starovoitov, Eduard Zingerman, Yonghong Song, bpf
Cc: Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann, kernel-team
On Thu Sep 24, 2026 at 3:53 AM CEST, Alexei Starovoitov wrote:
> On Thu Sep 24, 2026 at 12:13 AM UTC, Kumar Kartikeya Dwivedi wrote:
>> On Thu Sep 24, 2026 at 1:20 AM CEST, Alexei Starovoitov wrote:
>>> On Wed Sep 23, 2026 at 10:19 PM UTC, Eduard Zingerman wrote:
>>>> On Wed, 2026-09-23 at 21:28 +0000, Alexei Starovoitov wrote:
>>>>
>>>> ...
>>>>
>>>>> imo the following is cleaner:
>>>>> - do not repurpose bpf_throw() for this new thing.
>>>>> Introduce new kfunc that will do the unwind, let's name it bpf_unwind() ?
>>>>> - replace all 'call bpf_unwind_resume' with NOP by the verifier (or may with bpf_exit. tbd)
>>>>> Technically rustc or llvm can do that too, but it's cleaner to do in the verifier.
>>>>> - In bpf_unwind() walk all exception tables and replace return addresses
>>>>> in corresponding frames to landing_pad_ip-s.
>>>>> - just return from bpf_unwind().
>>>>> restoring callee saved registers will happen automatically by corresponding
>>>>> frames and there is no need to search exception tables at each step.
>>>>> That's what typical eh unwinder does, but it's doing it due to C++ logic
>>>>> that is more complex that Rust. For Rust unwinds we don't need all that.
>>>>> The above algorithm will do.
>>>>
>>>> +1, makes sense.
>>>> After return address rewrite the unwind would have to jump to it's epilogue, right?
>>>
>>> yes. I feel there will be no need in asm() tricks in such kfunc.
>>> unwind will rewrite its own return address, and will return through normal C
>>> and will restore callee saved (whatever it needed),
>>> then immediate callee in bpf prog should have had a landing pad for this kfunc,
>>> since this kfunc is throwing.
>>> so bpf_unwind() will jump to whatever is necessary to unwind in that bpf prog.
>>> And so on till the end.
>>>
>>>> Why not reusing bpf_throw() though, is it because of the exception cb mechanics?
>>>
>>> yes. bpf_throw() is not seen as throwing from compiler pov.
>>> I believe all kfuncs are not throwing. (that was a bug in this patch I alluded earlier).
>>> So we need a new kfunc and mark it attr(may_throw) or whatever the attr is called,
>>> so that compiler will generate exception tables for that bpf prog.
>>> Last time I checked there were bpf_cleanup section for rust-c in that case,
>>> but that was back in April.
>>
>> Hm, isn't this dependent on how you annotate the kfunc on the Rust side? Like,
>> you could make the same annotation on bpf_throw() that you would for the new
>> kfunc, no? I got confused by this part.
>
> bpf_throw() has a specific semantic already.
> Like it suppose to call that bpf prog callback once it unwinds
> everything.
> This new kfunc is different. It will initiate the unwind the way rust wants it to
> and may stop in the middle.
> See below point about 'catching'.
>
Right, ok, that makes sense. I do think it is cleaner to have landing pad based
cleanup initiated using a new entrypoint.
I just wasn't sure how much point there was in keeping the old unwinding scheme
around, given without being able to release any resources it's not very useful
in practice, so we could just drop the exception cb stuff completely and just
change its behavior and make its working conditional on landing pads being
present.
>>> There is also a case of 'catching' abort in rust. iirc it only stops unwind and
>>> rust-c generates something else instead of 'call bpf_unwind_resume'.
>>> Memory is vague. I think that frame will continue as normal after processing
>>> landing pad drop()s.
^ permalink raw reply [flat|nested] 49+ messages in thread
* Re: [PATCH bpf-next v5 07/21] bpf: Explore the landing pads no call site reaches
2026-09-23 21:28 ` Alexei Starovoitov
2026-09-23 22:19 ` Eduard Zingerman
@ 2026-09-23 23:09 ` Yonghong Song
1 sibling, 0 replies; 49+ messages in thread
From: Yonghong Song @ 2026-09-23 23:09 UTC (permalink / raw)
To: Alexei Starovoitov, Eduard Zingerman, bpf
Cc: Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann, kernel-team
On 9/23/26 2:28 PM, Alexei Starovoitov wrote:
> On Wed Sep 23, 2026 at 4:26 PM UTC, Eduard Zingerman wrote:
>> On Tue, 2026-09-22 at 21:59 -0700, Yonghong Song wrote:
>>> A cleanup record need not cover a call an exception can unwind out of: a
>>> frontend is free to emit a region around a helper or an ordinary kfunc,
>>> both nounwind here. Nothing marks a call site then, and that record's
>>> landing pad is reached by nothing at all -- leaving bpf_check_cfg() to
>>> refuse the program over code its own frontend had no way not to emit:
>>>
>>> 0: call bpf_preempt_disable
>>> 1: call bpf_preempt_enable record = { begin = 1, end = 2, pad = 4 }
>>> 2: r0 = 0
>>> 3: exit
>>> 4: r1 = pads_ran ll landing pad
>>> 6: r2 = *(u64 *)(r1 + 0)
>>> 7: r2 |= RAN_NOUNWIND_REC
>>> 8: *(u64 *)(r1 + 0) = r2
>>> 9: call bpf_unwind_resume
>>> 10: exit
>>>
>>> The range [1,2) holds one call, and it is a kfunc, so an exception cannot
>>> come out of it. mark_call_sites() marks nothing, nothing pushes an edge to
>>> 4, and 4 through 10 are reachable from nothing: "unreachable insn 4". The
>>> pad is dead, which is correct -- no exception can ever arrive at it -- but
>>> the program is fine and has to load.
>>>
>>> Walk every pad the table names that the edges did not reach, the way the
>>> walk is already re-seeded at an exception callback. From there the pad is
>>> code like any other: do_check() never enters it, because no call site
>>> dispatches to it, so the dead code sweep removes it along with everything
>>> else that was not reached.
>>>
>>> Signed-off-by: Yonghong Song <yonghong.song@linux.dev>
>>> ---
>> Yonghong, in v4 you said that rustc does not generate dead landing pads.
>> Why do we need this patch?
> It's not dead. commit log is incorrect.
>
> I bet C code that caused such asm didn't mark bpf_preempt_enable as nothrow,
> so compiler correctly emitted exception table.
> Now commit log argues that this helper doesn't throw in reality.
> Well, too late, the mistake was made earlier.
>
> I don't like the unwind approach taken by later patches in the series.
>
> imo the following is cleaner:
> - do not repurpose bpf_throw() for this new thing.
> Introduce new kfunc that will do the unwind, let's name it bpf_unwind() ?
> - replace all 'call bpf_unwind_resume' with NOP by the verifier (or may with bpf_exit. tbd)
> Technically rustc or llvm can do that too, but it's cleaner to do in the verifier.
> - In bpf_unwind() walk all exception tables and replace return addresses
> in corresponding frames to landing_pad_ip-s.
> - just return from bpf_unwind().
> restoring callee saved registers will happen automatically by corresponding
> frames and there is no need to search exception tables at each step.
> That's what typical eh unwinder does, but it's doing it due to C++ logic
> that is more complex that Rust. For Rust unwinds we don't need all that.
> The above algorithm will do.
>
> Also please add an example where [ip_start, ip_end] range is more than just a call
> insn. I mentioned it couple times as the reason why 'invoke' aka 16-byte insn
> approach doesn't work.
> I'm pretty sure I saw such IP ranges generated by rustc.
>
Thanks for explaining what should be implemented in a simpler way. I will try
to implement this as above steps.
^ permalink raw reply [flat|nested] 49+ messages in thread
* [PATCH bpf-next v5 08/21] bpf: Walk the exception unwind in the verifier
2026-09-23 4:58 [PATCH bpf-next v5 00/21] bpf: Run exception cleanup landing pads when bpf_throw() unwinds Yonghong Song
` (6 preceding siblings ...)
2026-09-23 4:59 ` [PATCH bpf-next v5 07/21] bpf: Explore the landing pads no call site reaches Yonghong Song
@ 2026-09-23 4:59 ` Yonghong Song
2026-09-23 16:34 ` Eduard Zingerman
2026-09-23 4:59 ` [PATCH bpf-next v5 09/21] bpf: Refuse exception cleanup shapes bpf_throw() cannot dispatch Yonghong Song
` (12 subsequent siblings)
20 siblings, 1 reply; 49+ messages in thread
From: Yonghong Song @ 2026-09-23 4:59 UTC (permalink / raw)
To: bpf
Cc: Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann,
Eduard Zingerman, kernel-team
bpf_throw() is about to stop discarding the frames it unwinds and start
running their landing pads instead. Have the verifier walk the same thing,
step for step:
at a throw, with the frame chain in env->cur_state->frame[]:
1. does a record cover the call site control is at?
2. yes: run that record's landing pad in this frame, and on
reaching its resume, pop the frame and go back to 1
3. no: pop the frame and go back to 1
4. frame 0 asks with nothing left to pop: deliver
Doing it this way is what keeps the resource rules unchanged. Whatever a
pad releases is released in the verifier state too, so by the time the walk
reaches the boundary the state says exactly what the program will really
hold there -- and check_resource_leak(), which used to fire the moment a
throw was seen, simply moves to the end of the walk. A frame that no record
covers contributes nothing, so anything it held is still held when the walk
ends, and that is what gets reported.
A pad starts in the state the walker hands it: its own frame's stack and
callee-saved registers, which the walker puts back, and nothing in the
caller-saved ones. r0 is an unknown scalar: LLVM names it as both the
exception pointer and the exception selector register, so a pad reads it
before anything else, and the dispatchers leave a defined value there
rather than whatever the kernel had.
Two things about that walk have to be said out loud, because the
instruction stream does not say them.
A resume belongs to the frame whose landing pad the walker called:
- a JIT lowers it as the way back out of a pad -- a bare return on
x86-64, a branch to the saved address on arm64 -- so it reaches the
walker only from there
- an exception in flight does not say which frame that is: a pad may call
a subprogram whose own pad reaches a resume by ordinary control flow
- so the state records the frame the unwind entered a pad in,
states_equal() keeps two such states apart, a resume in any other frame
is refused, and bpf_insn_successors() reports none for it
The edge from a throw to a pad crosses frames with no instruction in
between, and mark_chain_precision() walks that history backwards:
- left alone it stays in the pad's frame while reading the callee's
instructions: a request for the pad frame's r6 is cleared by the
callee's own write to r6, or reaches a call still set and trips "static
subprog unexpected regs"
- so the history entry for a pad records how many frames the unwind
popped, and the backtrack enters that many, the way it enters one for
BPF_EXIT
- the pad's r0 is whatever the walker left rather than anything an
instruction wrote, so a request for it ends at the edge
- a throwing global subprogram gets no frame of its own, so its exception
arrives at the pad rather than the insn after the call, which
backtrack_insn() has to allow for
This is also where bpf_unwind_resume() becomes callable: the kfunc is
registered here, beside the rule that refuses it outside a pad. Earlier,
nothing refused the call and a JIT rewrites a resume only in a pad, so a
program could reach the kernel body -- a WARN_ONCE whose only job is to say
it should never have been reached.
Signed-off-by: Yonghong Song <yonghong.song@linux.dev>
---
include/linux/bpf_verifier.h | 8 ++-
kernel/bpf/backtrack.c | 41 +++++++++--
kernel/bpf/exception.c | 1 +
kernel/bpf/helpers.c | 1 +
kernel/bpf/liveness.c | 3 +
kernel/bpf/states.c | 6 ++
kernel/bpf/verifier.c | 133 +++++++++++++++++++++++++++--------
7 files changed, 158 insertions(+), 35 deletions(-)
diff --git a/include/linux/bpf_verifier.h b/include/linux/bpf_verifier.h
index e12ec91b8150..3146f707e030 100644
--- a/include/linux/bpf_verifier.h
+++ b/include/linux/bpf_verifier.h
@@ -429,7 +429,8 @@ struct bpf_jmp_history_entry {
u32 prev_idx : 20;
/* special INSN_F_xxx flags */
u32 flags : 4;
- u32 : 8;
+ u32 unwind_frames : 4; /* frames popped to reach this landing pad */
+ u32 : 4;
/*
* additional registers that need precision tracking when this
* jump is backtracked, vector of five 11-bit records
@@ -510,6 +511,9 @@ struct bpf_verifier_state {
bool speculative;
bool in_sleepable;
+ bool unwinding; /* an exception is in flight */
+ u8 unwind_frameno; /* the frame whose landing pad is running */
+
/* first and last insn idx of this verifier state */
u32 first_insn_idx;
u32 last_insn_idx;
@@ -991,6 +995,8 @@ struct bpf_verifier_env {
} cfg;
struct backtrack_state bt;
struct bpf_jmp_history_entry *cur_hist_ent;
+ u8 cur_unwind_frames; /* frames popped to reach this insn, if a pad */
+ u8 unwind_frames; /* frames popped, staged for the next insn */
/* Per-callsite copy of parent's converged at_stack_in for cross-frame fills. */
struct arg_track **callsite_at_stack;
u32 pass_cnt; /* number of times do_check() was called */
diff --git a/kernel/bpf/backtrack.c b/kernel/bpf/backtrack.c
index 507a366dffa4..88da73345f01 100644
--- a/kernel/bpf/backtrack.c
+++ b/kernel/bpf/backtrack.c
@@ -4,6 +4,7 @@
#include <linux/bpf_verifier.h>
#include <linux/filter.h>
#include <linux/bitmap.h>
+#include "exception.h"
#define verbose(env, fmt, args...) bpf_verifier_log_write(env, fmt, ##args)
@@ -47,6 +48,7 @@ int bpf_push_jmp_history(struct bpf_verifier_env *env, struct bpf_verifier_state
p->flags = insn_flags;
p->spi = spi;
p->frame = frame;
+ p->unwind_frames = env->cur_unwind_frames;
p->linked_regs = linked_regs;
cur->jmp_history_cnt = cnt;
env->cur_hist_ent = p;
@@ -419,10 +421,12 @@ static int backtrack_insn(struct bpf_verifier_env *env, int idx, int subseq_idx,
* extra instructions from subprog; the next
* instruction after call to global subprog
* should be literally next instruction in
- * caller program
+ * caller program -- or, if the callee threw,
+ * the landing pad of this call site
*/
- verifier_bug_if(idx + 1 != subseq_idx, env,
- "extra insn from subprog");
+ verifier_bug_if(idx + 1 != subseq_idx &&
+ bpf_exc_pad_of_call(env, idx) != subseq_idx,
+ env, "extra insn from subprog");
/* global subprog always sets R0 */
bt_clear_reg(bt, BPF_REG_0);
/* and if it does not set R2, main pass would catch it */
@@ -823,6 +827,7 @@ int bpf_mark_chain_precision(struct bpf_verifier_env *env,
struct bpf_func_state *func;
bool tmp, skip_first = true;
struct bpf_reg_state *reg;
+ u32 pending_unwind = 0;
int i, fr, err;
if (!env->bpf_capable)
@@ -888,11 +893,19 @@ int bpf_mark_chain_precision(struct bpf_verifier_env *env,
}
for (i = last_idx;;) {
+ hist = get_jmp_hist_entry(st, history, i);
+ /*
+ * The insn about to be read sits on the far side of an
+ * unwind edge, so enter the frames that edge crossed
+ * before reading it.
+ */
+ for (; pending_unwind; pending_unwind--)
+ if (bt_subprog_enter(bt))
+ return -EFAULT;
if (skip_first) {
err = 0;
skip_first = false;
} else {
- hist = get_jmp_hist_entry(st, history, i);
err = backtrack_insn(env, i, subseq_idx, hist, bt);
}
if (err == -ENOTSUPP) {
@@ -909,6 +922,21 @@ int bpf_mark_chain_precision(struct bpf_verifier_env *env,
*/
return 0;
subseq_idx = i;
+ if (hist && hist->unwind_frames) {
+ /*
+ * This insn is a landing pad the unwind reached
+ * from a throw or a resume with
+ * hist->unwind_frames frames deeper. The walker
+ * writes r0 on the way in, so no insn defines
+ * it: left set, the request reaches the call
+ * that created this frame, where only r1-r5 may
+ * still be.
+ */
+ bt_clear_reg(bt, BPF_REG_0);
+ if (bt_empty(bt))
+ return 0;
+ pending_unwind = hist->unwind_frames;
+ }
i = get_prev_insn_idx(st, i, &history);
if (i == -ENOENT)
break;
@@ -927,6 +955,11 @@ int bpf_mark_chain_precision(struct bpf_verifier_env *env,
if (!st)
break;
+ if (verifier_bug_if(bt->frame > st->curframe, env,
+ "backtrack frame %d, state curframe %d",
+ bt->frame, st->curframe))
+ return -EFAULT;
+
for (fr = bt->frame; fr >= 0; fr--) {
func = st->frame[fr];
bitmap_from_u64(mask, bt_frame_reg_mask(bt, fr));
diff --git a/kernel/bpf/exception.c b/kernel/bpf/exception.c
index 244ea1b5417c..3270e7ae2e7c 100644
--- a/kernel/bpf/exception.c
+++ b/kernel/bpf/exception.c
@@ -5,6 +5,7 @@
#include <linux/btf.h>
#include <linux/btf_ids.h>
#include <linux/filter.h>
+#include <linux/slab.h>
#include "exception.h"
#define verbose(env, fmt, args...) bpf_verifier_log_write(env, fmt, ##args)
diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c
index 249418e60ee4..5eadab4dfa3f 100644
--- a/kernel/bpf/helpers.c
+++ b/kernel/bpf/helpers.c
@@ -5094,6 +5094,7 @@ BTF_ID_FLAGS(func, bpf_task_get_cgroup1, KF_ACQUIRE | KF_RCU | KF_RET_NULL)
BTF_ID_FLAGS(func, bpf_task_from_pid, KF_ACQUIRE | KF_RET_NULL)
BTF_ID_FLAGS(func, bpf_task_from_vpid, KF_ACQUIRE | KF_RET_NULL)
BTF_ID_FLAGS(func, bpf_throw)
+BTF_ID_FLAGS(func, bpf_unwind_resume)
#ifdef CONFIG_BPF_EVENTS
BTF_ID_FLAGS(func, bpf_send_signal_task)
#endif
diff --git a/kernel/bpf/liveness.c b/kernel/bpf/liveness.c
index 9d44a7dafe5f..00ac8917a716 100644
--- a/kernel/bpf/liveness.c
+++ b/kernel/bpf/liveness.c
@@ -258,6 +258,9 @@ bpf_insn_successors(struct bpf_verifier_env *env, u32 idx)
succ = env->succ;
succ->cnt = 0;
+ if (unlikely(bpf_is_unwind_resume_kfunc(insn)))
+ return succ;
+
opcode_info = &opcode_info_tbl[BPF_CLASS(insn->code) | BPF_OP(insn->code)];
insn_sz = bpf_is_ldimm64(insn) ? 2 : 1;
if (opcode_info->can_fallthrough)
diff --git a/kernel/bpf/states.c b/kernel/bpf/states.c
index 66fb11b6c6a7..b101baa43171 100644
--- a/kernel/bpf/states.c
+++ b/kernel/bpf/states.c
@@ -996,6 +996,12 @@ static bool states_equal(struct bpf_verifier_env *env,
if (old->in_sleepable != cur->in_sleepable)
return false;
+ if (old->unwinding != cur->unwinding)
+ return false;
+
+ if (old->unwinding && old->unwind_frameno != cur->unwind_frameno)
+ return false;
+
if (!refsafe(old, cur, &env->idmap_scratch))
return false;
diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index 8163c75ec3fa..53edce3d64ea 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -1726,6 +1726,8 @@ int bpf_copy_verifier_state(struct bpf_verifier_state *dst_state,
return err;
dst_state->speculative = src->speculative;
dst_state->in_sleepable = src->in_sleepable;
+ dst_state->unwinding = src->unwinding;
+ dst_state->unwind_frameno = src->unwind_frameno;
dst_state->curframe = src->curframe;
dst_state->branches = src->branches;
dst_state->parent = src->parent;
@@ -10598,8 +10600,8 @@ static int push_callback_call(struct bpf_verifier_env *env, struct bpf_insn *ins
return 0;
}
-static int process_bpf_exit_full(struct bpf_verifier_env *env,
- bool *do_print_state, bool exception_exit);
+static int process_bpf_exit_full(struct bpf_verifier_env *env, bool *do_print_state);
+static int unwind_step(struct bpf_verifier_env *env, u32 callsite, int *insn_idx);
static int check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
int *insn_idx)
@@ -10689,7 +10691,7 @@ static int check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
verbose(env, "failed to push state for global subprog exception path\n");
return PTR_ERR(branch);
}
- return process_bpf_exit_full(env, NULL, true);
+ return unwind_step(env, *insn_idx, insn_idx);
}
/* continue with next insn after call */
@@ -11078,6 +11080,20 @@ static bool retval_range_within(struct bpf_retval_range range, const struct bpf_
return range.minval <= reg_smin(reg) && reg_smax(reg) <= range.maxval;
}
+static u32 pop_frame(struct bpf_verifier_env *env)
+{
+ struct bpf_verifier_state *state = env->cur_state;
+ struct bpf_func_state *callee = state->frame[state->curframe];
+ struct bpf_func_state *caller = state->frame[state->curframe - 1];
+ u32 callsite = callee->callsite;
+
+ account_processed_insns(env, callee, caller);
+ free_func_state(callee);
+ state->frame[state->curframe--] = NULL;
+ invalidate_outgoing_stack_args(env, caller);
+ return callsite;
+}
+
static int prepare_func_exit(struct bpf_verifier_env *env, int *insn_idx)
{
struct bpf_verifier_state *state = env->cur_state, *prev_st;
@@ -11157,12 +11173,7 @@ static int prepare_func_exit(struct bpf_verifier_env *env, int *insn_idx)
verbose(env, "to caller at %d:\n", *insn_idx);
print_verifier_state(env, state, caller->frameno, true);
}
- account_processed_insns(env, callee, caller);
- /* clear everything in the callee. In case of exceptional exits using
- * bpf_throw, this will be done by copy_verifier_state for extra frames. */
- free_func_state(callee);
- state->frame[state->curframe--] = NULL;
- invalidate_outgoing_stack_args(env, caller);
+ pop_frame(env);
/* for callbacks widen imprecise scalars to make programs like below verify:
*
@@ -14661,7 +14672,7 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
env->prog->call_session_cookie = true;
if (bpf_is_throw_kfunc(insn))
- return process_bpf_exit_full(env, NULL, true);
+ return unwind_step(env, insn_idx, &env->insn_idx);
return 0;
}
@@ -18586,9 +18597,80 @@ enum {
INSN_IDX_UPDATED = 2,
};
-static int process_bpf_exit_full(struct bpf_verifier_env *env,
- bool *do_print_state,
- bool exception_exit)
+static void unwind_enter_pad(struct bpf_verifier_env *env)
+{
+ struct bpf_verifier_state *state = env->cur_state;
+ struct bpf_func_state *frame = cur_func(env);
+
+ state->unwind_frameno = state->curframe;
+ clear_caller_saved_regs(env, frame->regs);
+ mark_reg_unknown(env, frame->regs, BPF_REG_0);
+}
+
+static int unwind_finish(struct bpf_verifier_env *env)
+{
+ int err = check_resource_leak(env, true, true, "bpf_throw");
+
+ if (err)
+ return err;
+ return PROCESS_BPF_EXIT;
+}
+
+static int unwind_step(struct bpf_verifier_env *env, u32 callsite, int *insn_idx)
+{
+ struct bpf_verifier_state *state = env->cur_state;
+ u8 popped = 0;
+
+ state->unwinding = true;
+ for (;;) {
+ int pad = bpf_exc_pad_of_call(env, callsite);
+
+ if (pad >= 0) {
+ unwind_enter_pad(env);
+ /*
+ * The edge from @callsite to the pad crosses @popped
+ * frames, and nothing in the instruction stream says
+ * so. Record it for mark_chain_precision(), which has
+ * to walk back through the same frames.
+ */
+ env->unwind_frames = popped;
+ *insn_idx = pad;
+ return INSN_IDX_UPDATED;
+ }
+ if (!state->curframe)
+ return unwind_finish(env);
+ callsite = pop_frame(env);
+ popped++;
+ }
+}
+
+static int process_cleanup_resume(struct bpf_verifier_env *env, int *insn_idx)
+{
+ struct bpf_verifier_state *state = env->cur_state;
+ int err;
+
+ if (!state->unwinding) {
+ verbose(env,
+ "bpf_unwind_resume() at insn %u reached without an exception in flight\n",
+ (u32)*insn_idx);
+ return -EINVAL;
+ }
+ if (state->curframe != state->unwind_frameno) {
+ verbose(env,
+ "bpf_unwind_resume() at insn %u is in frame %u, not frame %u whose landing pad the exception entered\n",
+ (u32)*insn_idx, state->curframe, state->unwind_frameno);
+ return -EINVAL;
+ }
+ if (!state->curframe)
+ return unwind_finish(env);
+ err = unwind_step(env, pop_frame(env), insn_idx);
+ /* unwind_step() counted the frames it popped, not the one popped here. */
+ if (err == INSN_IDX_UPDATED)
+ env->unwind_frames++;
+ return err;
+}
+
+static int process_bpf_exit_full(struct bpf_verifier_env *env, bool *do_print_state)
{
struct bpf_func_state *cur_frame = cur_func(env);
@@ -18598,25 +18680,11 @@ static int process_bpf_exit_full(struct bpf_verifier_env *env,
* for which reference_state must match caller reference
* state when it exits.
*/
- int err = check_resource_leak(env, exception_exit,
- exception_exit || !env->cur_state->curframe,
- exception_exit ? "bpf_throw" :
+ int err = check_resource_leak(env, false, !env->cur_state->curframe,
"BPF_EXIT instruction in main prog");
if (err)
return err;
- /* The side effect of the prepare_func_exit which is
- * being skipped is that it frees bpf_func_state.
- * Typically, process_bpf_exit will only be hit with
- * outermost exit. copy_verifier_state in pop_stack will
- * handle freeing of any extra bpf_func_state left over
- * from not processing all nested function exits. We
- * also skip return code checks as they are not needed
- * for exceptional exits.
- */
- if (exception_exit)
- return PROCESS_BPF_EXIT;
-
if (env->cur_state->curframe) {
/* exit from nested function */
err = prepare_func_exit(env, &env->insn_idx);
@@ -18790,6 +18858,8 @@ static int do_check_insn(struct bpf_verifier_env *env, bool *do_print_state)
env->jmps_processed++;
if (opcode == BPF_CALL) {
+ if (bpf_is_unwind_resume_kfunc(insn))
+ return process_cleanup_resume(env, &env->insn_idx);
if (env->cur_state->active_locks) {
if ((insn->src_reg == BPF_REG_0 &&
insn->imm != BPF_FUNC_spin_unlock &&
@@ -18823,7 +18893,7 @@ static int do_check_insn(struct bpf_verifier_env *env, bool *do_print_state)
env->insn_idx += insn->imm + 1;
return INSN_IDX_UPDATED;
} else if (opcode == BPF_EXIT) {
- return process_bpf_exit_full(env, do_print_state, false);
+ return process_bpf_exit_full(env, do_print_state);
}
return check_cond_jmp_op(env, insn, &env->insn_idx);
}
@@ -18864,6 +18934,9 @@ static int do_check(struct bpf_verifier_env *env)
/* reset current history entry on each new instruction */
env->cur_hist_ent = NULL;
+ /* frames the unwind popped to reach this insn, if it is a pad */
+ env->cur_unwind_frames = env->unwind_frames;
+ env->unwind_frames = 0;
env->prev_insn_idx = prev_insn_idx;
if (env->insn_idx >= insn_cnt) {
@@ -18927,7 +19000,7 @@ static int do_check(struct bpf_verifier_env *env)
}
}
- if (bpf_is_jmp_point(env, env->insn_idx)) {
+ if (bpf_is_jmp_point(env, env->insn_idx) || env->cur_unwind_frames) {
err = bpf_push_jmp_history(env, state, 0, 0, 0, 0);
if (err)
return err;
--
2.53.0-Meta
^ permalink raw reply related [flat|nested] 49+ messages in thread* Re: [PATCH bpf-next v5 08/21] bpf: Walk the exception unwind in the verifier
2026-09-23 4:59 ` [PATCH bpf-next v5 08/21] bpf: Walk the exception unwind in the verifier Yonghong Song
@ 2026-09-23 16:34 ` Eduard Zingerman
2026-09-23 18:01 ` Yonghong Song
0 siblings, 1 reply; 49+ messages in thread
From: Eduard Zingerman @ 2026-09-23 16:34 UTC (permalink / raw)
To: Yonghong Song, bpf
Cc: Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann, kernel-team
On Tue, 2026-09-22 at 21:59 -0700, Yonghong Song wrote:
...
> diff --git a/include/linux/bpf_verifier.h b/include/linux/bpf_verifier.h
> index e12ec91b8150..3146f707e030 100644
> --- a/include/linux/bpf_verifier.h
> +++ b/include/linux/bpf_verifier.h
> @@ -429,7 +429,8 @@ struct bpf_jmp_history_entry {
> u32 prev_idx : 20;
> /* special INSN_F_xxx flags */
> u32 flags : 4;
> - u32 : 8;
> + u32 unwind_frames : 4; /* frames popped to reach this landing pad */
> + u32 : 4;
> /*
> * additional registers that need precision tracking when this
> * jump is backtracked, vector of five 11-bit records
> @@ -510,6 +511,9 @@ struct bpf_verifier_state {
> bool speculative;
> bool in_sleepable;
>
> + bool unwinding; /* an exception is in flight */
> + u8 unwind_frameno; /* the frame whose landing pad is running */
> +
> /* first and last insn idx of this verifier state */
> u32 first_insn_idx;
> u32 last_insn_idx;
> @@ -991,6 +995,8 @@ struct bpf_verifier_env {
> } cfg;
> struct backtrack_state bt;
> struct bpf_jmp_history_entry *cur_hist_ent;
> + u8 cur_unwind_frames; /* frames popped to reach this insn, if a pad */
> + u8 unwind_frames; /* frames popped, staged for the next insn */
Similarly to v4, stashing these variables into env in one place and
reading them in another in order to push to jump history is an
obfuscation step. Why don't you want to push to the jump history right
away? Like in [1].
So far the only field in env that changes with current verifier state is insn_idx.
I don't see why this feature requires adding two more such fields.
[1] https://github.com/kernel-patches/bpf/commit/fbc89e90cacd207b4271da25ed7a380f6b56125e#diff-edbb57adf10d1ce1fbb830a34fa92712fd01db1fbd9b6f2504001eb7bcc7b9d0R5604-R18577
(with corresponding changes in backtrack.c in the same commit)
...
^ permalink raw reply [flat|nested] 49+ messages in thread* Re: [PATCH bpf-next v5 08/21] bpf: Walk the exception unwind in the verifier
2026-09-23 16:34 ` Eduard Zingerman
@ 2026-09-23 18:01 ` Yonghong Song
0 siblings, 0 replies; 49+ messages in thread
From: Yonghong Song @ 2026-09-23 18:01 UTC (permalink / raw)
To: Eduard Zingerman, bpf
Cc: Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann, kernel-team
On 9/23/26 9:34 AM, Eduard Zingerman wrote:
> On Tue, 2026-09-22 at 21:59 -0700, Yonghong Song wrote:
>
> ...
>
>> diff --git a/include/linux/bpf_verifier.h b/include/linux/bpf_verifier.h
>> index e12ec91b8150..3146f707e030 100644
>> --- a/include/linux/bpf_verifier.h
>> +++ b/include/linux/bpf_verifier.h
>> @@ -429,7 +429,8 @@ struct bpf_jmp_history_entry {
>> u32 prev_idx : 20;
>> /* special INSN_F_xxx flags */
>> u32 flags : 4;
>> - u32 : 8;
>> + u32 unwind_frames : 4; /* frames popped to reach this landing pad */
>> + u32 : 4;
>> /*
>> * additional registers that need precision tracking when this
>> * jump is backtracked, vector of five 11-bit records
>> @@ -510,6 +511,9 @@ struct bpf_verifier_state {
>> bool speculative;
>> bool in_sleepable;
>>
>> + bool unwinding; /* an exception is in flight */
>> + u8 unwind_frameno; /* the frame whose landing pad is running */
>> +
>> /* first and last insn idx of this verifier state */
>> u32 first_insn_idx;
>> u32 last_insn_idx;
>> @@ -991,6 +995,8 @@ struct bpf_verifier_env {
>> } cfg;
>> struct backtrack_state bt;
>> struct bpf_jmp_history_entry *cur_hist_ent;
>> + u8 cur_unwind_frames; /* frames popped to reach this insn, if a pad */
>> + u8 unwind_frames; /* frames popped, staged for the next insn */
> Similarly to v4, stashing these variables into env in one place and
> reading them in another in order to push to jump history is an
> obfuscation step. Why don't you want to push to the jump history right
> away? Like in [1].
>
> So far the only field in env that changes with current verifier state is insn_idx.
> I don't see why this feature requires adding two more such fields.
>
> [1] https://github.com/kernel-patches/bpf/commit/fbc89e90cacd207b4271da25ed7a380f6b56125e#diff-edbb57adf10d1ce1fbb830a34fa92712fd01db1fbd9b6f2504001eb7bcc7b9d0R5604-R18577
> (with corresponding changes in backtrack.c in the same commit)
Okay, I looked at your backtrack.c and verifier.c related code.
I will use jump history then.
>
> ...
^ permalink raw reply [flat|nested] 49+ messages in thread
* [PATCH bpf-next v5 09/21] bpf: Refuse exception cleanup shapes bpf_throw() cannot dispatch
2026-09-23 4:58 [PATCH bpf-next v5 00/21] bpf: Run exception cleanup landing pads when bpf_throw() unwinds Yonghong Song
` (7 preceding siblings ...)
2026-09-23 4:59 ` [PATCH bpf-next v5 08/21] bpf: Walk the exception unwind in the verifier Yonghong Song
@ 2026-09-23 4:59 ` Yonghong Song
2026-09-23 4:59 ` [PATCH bpf-next v5 10/21] bpf: Refuse a private stack for a program with an exception cleanup table Yonghong Song
` (11 subsequent siblings)
20 siblings, 0 replies; 49+ messages in thread
From: Yonghong Song @ 2026-09-23 4:59 UTC (permalink / raw)
To: bpf
Cc: Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann,
Eduard Zingerman, kernel-team
The verifier now walks the unwind, so at every instruction it knows whether
an exception is in flight and, if it is, which frame's landing pad it
entered. do_check() asks bpf_exc_check_insn() about each instruction of
a program that carries a table, and it refuses:
- bpf_throw() in any frame while unwinding: a second unwind over frames
the first is still discarding. A static callee is walked from the pad,
so its own throw trips this too; a global one is not walked, which is
what subprog_info.might_throw is for
- in the frame whose landing pad the unwind entered, where the code runs
on the walker's stack: a plain exit, which is what a catch pad ends in,
a tail call, an indirect jump, an outgoing on-stack call argument, and
a BPF_LD_[ABS|IND], whose failure path leaves the frame through the
epilogue
- an instruction reached both inside and outside a pad: the arm64 JIT
addresses the frame through a register only a pad entry computes. This
also turns away a jump into the middle of a pad
bpf_exc_check_callback() refuses a subprogram that may unwind as a
helper callback, whose helper frame would end the walk before it found a
boundary.
Signed-off-by: Yonghong Song <yonghong.song@linux.dev>
---
include/linux/bpf_verifier.h | 2 +
kernel/bpf/exception.c | 101 +++++++++++++++++++++++++++++++++++
kernel/bpf/exception.h | 3 ++
kernel/bpf/fixups.c | 1 +
kernel/bpf/verifier.c | 10 ++++
5 files changed, 117 insertions(+)
diff --git a/include/linux/bpf_verifier.h b/include/linux/bpf_verifier.h
index 3146f707e030..736304d912af 100644
--- a/include/linux/bpf_verifier.h
+++ b/include/linux/bpf_verifier.h
@@ -698,6 +698,8 @@ struct bpf_insn_aux_data {
u64 arg_prog:4;
u64 throw_call:1; /* call to bpf_throw() */
u64 resume_call:1; /* call to bpf_unwind_resume() */
+ u64 in_cleanup_pad:1; /* runs with an exception in flight, in the pad's frame */
+ u64 outside_cleanup_pad:1; /* ... and the other way round */
/* below flags are initialized once */
u64 jmp_point:1;
diff --git a/kernel/bpf/exception.c b/kernel/bpf/exception.c
index 3270e7ae2e7c..3aafbb612e7f 100644
--- a/kernel/bpf/exception.c
+++ b/kernel/bpf/exception.c
@@ -26,6 +26,15 @@ static void mark_kfunc_sites(struct bpf_verifier_env *env)
}
}
+int bpf_exc_check_callback(struct bpf_verifier_env *env, int subprog)
+{
+ if (!env->cleanup_info_cnt || !env->subprog_info[subprog].might_throw)
+ return 0;
+
+ verbose(env, "subprog %d may unwind and is used as a callback\n", subprog);
+ return -EINVAL;
+}
+
static void mark_call_sites(struct bpf_verifier_env *env)
{
u32 i, j;
@@ -72,6 +81,98 @@ int bpf_prepare_cleanup_exceptions(struct bpf_verifier_env *env)
return 0;
}
+int bpf_exc_check_insn(struct bpf_verifier_env *env, struct bpf_insn *insn)
+{
+ struct bpf_verifier_state *state = env->cur_state;
+ struct bpf_insn_aux_data *aux;
+ u32 i = env->insn_idx;
+ bool in_pad;
+
+ aux = &env->insn_aux_data[i];
+ in_pad = state->unwinding && state->curframe == state->unwind_frameno;
+
+ if (in_pad ? aux->outside_cleanup_pad : aux->in_cleanup_pad) {
+ verbose(env,
+ "insn %u runs both inside and outside an exception cleanup landing pad\n",
+ i);
+ return -EINVAL;
+ }
+ if (in_pad)
+ aux->in_cleanup_pad = true;
+ else
+ aux->outside_cleanup_pad = true;
+
+ if (!state->unwinding)
+ return 0;
+
+ if (bpf_is_throw_kfunc(insn)) {
+ verbose(env,
+ "bpf_throw() at insn %u throws while an exception is in flight\n",
+ i);
+ return -EINVAL;
+ }
+ if (bpf_pseudo_call(insn)) {
+ int subprog = bpf_find_subprog(env, i + insn->imm + 1);
+
+ if (subprog >= 0 && bpf_subprog_is_global(env, subprog) &&
+ env->subprog_info[subprog].might_throw) {
+ verbose(env,
+ "insn %u calls global subprog %d, which can throw while an exception is in flight\n",
+ i, subprog);
+ return -EINVAL;
+ }
+ }
+
+ if (!in_pad)
+ return 0;
+
+ if (insn->code == (BPF_JMP | BPF_EXIT)) {
+ verbose(env,
+ "exit at insn %u ends an exception cleanup landing pad: a catch pad is not supported yet, only cleanup pads that resume\n",
+ i);
+ return -EOPNOTSUPP;
+ }
+ if (bpf_helper_call(insn) && insn->imm == BPF_FUNC_tail_call) {
+ verbose(env,
+ "bpf_tail_call() at insn %u is in an exception cleanup landing pad\n",
+ i);
+ return -EINVAL;
+ }
+ if (insn->code == (BPF_JMP | BPF_JA | BPF_X) ||
+ insn->code == (BPF_JMP32 | BPF_JA | BPF_X)) {
+ verbose(env,
+ "indirect jump at insn %u is in an exception cleanup landing pad\n",
+ i);
+ return -EINVAL;
+ }
+ /* A BPF_LD_[ABS|IND] can leave the frame through its epilogue. */
+ if (BPF_CLASS(insn->code) == BPF_LD &&
+ (BPF_MODE(insn->code) == BPF_ABS || BPF_MODE(insn->code) == BPF_IND)) {
+ verbose(env,
+ "BPF_LD_[ABS|IND] at insn %u is in an exception cleanup landing pad\n",
+ i);
+ return -EINVAL;
+ }
+ if (is_stack_arg_st(insn) || is_stack_arg_stx(insn)) {
+ verbose(env,
+ "insn %u stages an on-stack call argument in an exception cleanup landing pad\n",
+ i);
+ return -EINVAL;
+ }
+ if (bpf_pseudo_kfunc_call(insn)) {
+ struct bpf_call_summary cs;
+
+ if (bpf_get_call_summary(env, insn, &cs) &&
+ cs.arg_slot_cnt > MAX_BPF_FUNC_REG_ARGS) {
+ verbose(env,
+ "insn %u calls a kfunc with an on-stack argument in an exception cleanup landing pad\n",
+ i);
+ return -EINVAL;
+ }
+ }
+ return 0;
+}
+
bool bpf_is_unwind_resume_kfunc(const struct bpf_insn *insn)
{
return bpf_pseudo_kfunc_call(insn) && insn->off == 0 &&
diff --git a/kernel/bpf/exception.h b/kernel/bpf/exception.h
index b96b2c429e22..0872a3f70583 100644
--- a/kernel/bpf/exception.h
+++ b/kernel/bpf/exception.h
@@ -5,9 +5,12 @@
#include <linux/types.h>
+struct bpf_insn;
struct bpf_verifier_env;
int bpf_prepare_cleanup_exceptions(struct bpf_verifier_env *env);
+int bpf_exc_check_insn(struct bpf_verifier_env *env, struct bpf_insn *insn);
+int bpf_exc_check_callback(struct bpf_verifier_env *env, int subprog);
int bpf_exc_pad_of_call(struct bpf_verifier_env *env, u32 idx);
#endif /* _LINUX_BPF_EXCEPTION_H */
diff --git a/kernel/bpf/fixups.c b/kernel/bpf/fixups.c
index fed5b66ed3b7..53a737c88dcc 100644
--- a/kernel/bpf/fixups.c
+++ b/kernel/bpf/fixups.c
@@ -252,6 +252,7 @@ static void adjust_insn_aux_data(struct bpf_verifier_env *env,
/* Expand insni[off]'s seen count to the patched range. */
data[i].seen = old_seen;
data[i].zext_dst = bpf_insn_def32(new_prog, insn + i) >= 0;
+ data[i].in_cleanup_pad = data[off + cnt - 1].in_cleanup_pad;
if (!memcmp(insn + i, original_insn, sizeof(struct bpf_insn))) {
data[i].non_stack_access =
data[off + cnt - 1].non_stack_access;
diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index 53edce3d64ea..7b74dd9b9f48 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -10549,6 +10549,10 @@ static int push_callback_call(struct bpf_verifier_env *env, struct bpf_insn *ins
* callbacks
*/
env->subprog_info[subprog].is_cb = true;
+ err = bpf_exc_check_callback(env, subprog);
+ if (err)
+ return err;
+
if (bpf_pseudo_kfunc_call(insn) &&
!is_callback_calling_kfunc(insn->imm)) {
verifier_bug(env, "kfunc %s#%d not marked as callback-calling",
@@ -19041,6 +19045,12 @@ static int do_check(struct bpf_verifier_env *env)
return err;
}
+ if (unlikely(env->cleanup_info_cnt)) {
+ err = bpf_exc_check_insn(env, insn);
+ if (err)
+ return err;
+ }
+
sanitize_mark_insn_seen(env);
prev_insn_idx = env->insn_idx;
--
2.53.0-Meta
^ permalink raw reply related [flat|nested] 49+ messages in thread* [PATCH bpf-next v5 10/21] bpf: Refuse a private stack for a program with an exception cleanup table
2026-09-23 4:58 [PATCH bpf-next v5 00/21] bpf: Run exception cleanup landing pads when bpf_throw() unwinds Yonghong Song
` (8 preceding siblings ...)
2026-09-23 4:59 ` [PATCH bpf-next v5 09/21] bpf: Refuse exception cleanup shapes bpf_throw() cannot dispatch Yonghong Song
@ 2026-09-23 4:59 ` Yonghong Song
2026-09-23 4:59 ` [PATCH bpf-next v5 11/21] bpf: Dispatch exception cleanup pads from bpf_throw() Yonghong Song
` (10 subsequent siblings)
20 siblings, 0 replies; 49+ messages in thread
From: Yonghong Song @ 2026-09-23 4:59 UTC (permalink / raw)
To: bpf
Cc: Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann,
Eduard Zingerman, kernel-team
A private stack is hard to support together with an exception cleanup
table on x86-64: a landing pad runs on the bpf_throw() walker's stack with
its frame's registers put back from a spill area, and carrying the private
stack's frame pointer through there as well would cost a register that
architecture cannot spare.
Force NO_PRIV_STACK in check_max_stack_depth(), where the choice is made,
and do it on every architecture for now rather than just x86-64. The
program still loads, but more than an optimization can be lost:
check_max_stack_depth_subprog() checks a PRIV_STACK_ADAPTIVE subprogram
against MAX_BPF_STACK on its own, and adds a NO_PRIV_STACK one to the
depth its callers have to fit in -- so a bpf2bpf chain deep enough to need
the private stack is now refused with "combined stack size of %d calls is
%d. Too large".
Signed-off-by: Yonghong Song <yonghong.song@linux.dev>
---
kernel/bpf/verifier.c | 11 +++++++++++
1 file changed, 11 insertions(+)
diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index 7b74dd9b9f48..6462e9f2ffdc 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -5602,6 +5602,17 @@ static int check_max_stack_depth(struct bpf_verifier_env *env)
}
}
+ /*
+ * A pad rebuilds its frame from a spill area, and on x86-64 a private
+ * stack's frame pointer lives in r9, which no spill area holds.
+ * Refused on every arch rather than just that one. The subprograms
+ * below are then checked against MAX_BPF_STACK together rather than
+ * one at a time, so this can turn a program that would have loaded
+ * with a private stack into one that is too deep.
+ */
+ if (env->cleanup_info_cnt)
+ priv_stack_mode = NO_PRIV_STACK;
+
if (priv_stack_mode == PRIV_STACK_UNKNOWN)
priv_stack_mode = bpf_enable_priv_stack(env->prog);
--
2.53.0-Meta
^ permalink raw reply related [flat|nested] 49+ messages in thread* [PATCH bpf-next v5 11/21] bpf: Dispatch exception cleanup pads from bpf_throw()
2026-09-23 4:58 [PATCH bpf-next v5 00/21] bpf: Run exception cleanup landing pads when bpf_throw() unwinds Yonghong Song
` (9 preceding siblings ...)
2026-09-23 4:59 ` [PATCH bpf-next v5 10/21] bpf: Refuse a private stack for a program with an exception cleanup table Yonghong Song
@ 2026-09-23 4:59 ` Yonghong Song
2026-09-23 4:59 ` [PATCH bpf-next v5 12/21] bpf, x86: Dispatch exception cleanup pads at run time Yonghong Song
` (9 subsequent siblings)
20 siblings, 0 replies; 49+ messages in thread
From: Yonghong Song @ 2026-09-23 4:59 UTC (permalink / raw)
To: bpf
Cc: Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann,
Eduard Zingerman, kernel-team
Stop discarding the frames an exception unwinds through. bpf_throw()
already walks the BPF call stack with arch_bpf_stack_walk() to find the
exception boundary; have it look each frame's return address up in that
(sub)program's cleanup table on the way and run the landing pad a matching
record names.
A pad is run as a subroutine of the walker, not jumped to. It executes with
the unwinding frame's frame pointer and BPF callee-saved registers, so
everything it reads is that frame's, but on the current stack far below it,
so nothing it calls can disturb the frame it is cleaning up after. It ends
in what the JIT emits for its bpf_unwind_resume(), which hands control back
to the walker rather than to the frame's caller. That is exactly the
procedure the LLVM commit emitting the section describes.
Restoring the frame's r6-r9 is what makes this work, and it is only
possible because the callee about to be discarded spilled them in its own
prologue. bpf_exc_force_spill() tells a JIT to make that spill
unconditional and of a known shape for every subprogram of a program
carrying a cleanup table, and aux->exc->spill_off records where it starts,
so the walker needs no per-frame metadata. The frame that called
bpf_throw() has no callee to have spilled anything and never runs its own
epilogue, so the JIT spills that frame's registers at the throw site
instead, in the area aux->exc->throw_spill_off names.
The main program needs the table handed to it rather than built for it.
jit_subprogs() compiles it as func[0], but the ksym covering that image is
the one bpf_prog_load() registers for the outer bpf_prog, and that is what
the walker finds -- so without the handover a landing pad in the main
program's own frame is never dispatched, silently, since the exception
still reaches the boundary and the cookie still comes back.
Which instructions a JIT has to lower its own way -- a bpf_throw(), a
resume, the head and the body of a pad -- it reads from insn_aux_data,
which bpf_int_jit_compile() already has and which instruction patching
keeps in step. A subprogram's instructions are indexed from its
aux->subprog_start.
Signed-off-by: Yonghong Song <yonghong.song@linux.dev>
---
include/linux/bpf.h | 88 ++++++++++++++++++++++
include/linux/bpf_verifier.h | 1 +
include/linux/filter.h | 1 +
kernel/bpf/core.c | 30 +++++++-
kernel/bpf/exception.c | 141 +++++++++++++++++++++++++++++++++++
kernel/bpf/exception.h | 7 ++
kernel/bpf/fixups.c | 65 ++++++++++++++++
kernel/bpf/helpers.c | 34 +++++++++
8 files changed, 364 insertions(+), 3 deletions(-)
diff --git a/include/linux/bpf.h b/include/linux/bpf.h
index e7c5e203eddd..c11dc224973e 100644
--- a/include/linux/bpf.h
+++ b/include/linux/bpf.h
@@ -1787,6 +1787,93 @@ enum bpf_sig_keyring {
BPF_SIG_KEYRING_BPF,
};
+/*
+ * One cleanup region of a JITed (sub)program: @pad is the landing pad to run
+ * for a return address in (begin, end], the native code of its call sites.
+ */
+struct bpf_cleanup_range {
+ u64 begin;
+ u64 end;
+ u64 pad;
+};
+
+struct bpf_exception_info {
+ struct bpf_cleanup_info *info;
+ struct bpf_cleanup_range *ranges;
+ u32 nr_info;
+ u32 nr_ranges;
+ /* Set if any instruction of this subprogram calls bpf_throw(). */
+ bool has_throw;
+ /* Offset from a frame's FP to the caller's spilled r6-r9. */
+ s32 spill_off;
+ /* Likewise, to the registers a frame spills before calling bpf_throw(). */
+ s32 throw_spill_off;
+};
+
+#ifdef CONFIG_BPF_SYSCALL
+bool bpf_exc_force_spill(const struct bpf_prog *prog);
+bool bpf_exc_needs_throw_spill(const struct bpf_prog *prog);
+bool bpf_exc_insn_is_pad(const struct bpf_verifier_env *env,
+ const struct bpf_prog *prog, u32 idx);
+bool bpf_exc_insn_in_pad(const struct bpf_verifier_env *env,
+ const struct bpf_prog *prog, u32 idx);
+bool bpf_exc_insn_is_throw(const struct bpf_verifier_env *env,
+ const struct bpf_prog *prog, u32 idx);
+bool bpf_exc_insn_is_resume(const struct bpf_verifier_env *env,
+ const struct bpf_prog *prog, u32 idx);
+int bpf_exc_attach_main_prog(struct bpf_verifier_env *env, struct bpf_prog *prog);
+void bpf_exc_fill_native_ranges(struct bpf_prog *prog, u32 *addrs, void *image);
+void bpf_exc_free_info(struct bpf_prog_aux *aux);
+#else
+static inline bool bpf_exc_force_spill(const struct bpf_prog *prog)
+{
+ return false;
+}
+
+static inline bool bpf_exc_needs_throw_spill(const struct bpf_prog *prog)
+{
+ return false;
+}
+
+static inline bool bpf_exc_insn_is_pad(const struct bpf_verifier_env *env,
+ const struct bpf_prog *prog, u32 idx)
+{
+ return false;
+}
+
+static inline bool bpf_exc_insn_in_pad(const struct bpf_verifier_env *env,
+ const struct bpf_prog *prog, u32 idx)
+{
+ return false;
+}
+
+static inline bool bpf_exc_insn_is_throw(const struct bpf_verifier_env *env,
+ const struct bpf_prog *prog, u32 idx)
+{
+ return false;
+}
+
+static inline bool bpf_exc_insn_is_resume(const struct bpf_verifier_env *env,
+ const struct bpf_prog *prog, u32 idx)
+{
+ return false;
+}
+
+static inline int bpf_exc_attach_main_prog(struct bpf_verifier_env *env,
+ struct bpf_prog *prog)
+{
+ return 0;
+}
+
+static inline void bpf_exc_fill_native_ranges(struct bpf_prog *prog, u32 *addrs, void *image)
+{
+}
+
+static inline void bpf_exc_free_info(struct bpf_prog_aux *aux)
+{
+}
+#endif
+
struct bpf_prog_aux {
atomic64_t refcnt;
u32 used_map_cnt;
@@ -1867,6 +1954,7 @@ struct bpf_prog_aux {
char name[BPF_OBJ_NAME_LEN];
u64 (*bpf_exception_cb)(u64 cookie, u64 sp, u64 bp, u64, u64);
u16 stack_arg_sp_adjust;
+ struct bpf_exception_info *exc;
#ifdef CONFIG_SECURITY
void *security;
#endif
diff --git a/include/linux/bpf_verifier.h b/include/linux/bpf_verifier.h
index 736304d912af..500a1e253579 100644
--- a/include/linux/bpf_verifier.h
+++ b/include/linux/bpf_verifier.h
@@ -700,6 +700,7 @@ struct bpf_insn_aux_data {
u64 resume_call:1; /* call to bpf_unwind_resume() */
u64 in_cleanup_pad:1; /* runs with an exception in flight, in the pad's frame */
u64 outside_cleanup_pad:1; /* ... and the other way round */
+ u64 cleanup_pad_head:1; /* first insn of a landing pad */
/* below flags are initialized once */
u64 jmp_point:1;
diff --git a/include/linux/filter.h b/include/linux/filter.h
index 2582a7606e46..b0c495e94f74 100644
--- a/include/linux/filter.h
+++ b/include/linux/filter.h
@@ -1243,6 +1243,7 @@ bool bpf_jit_supports_arena_args(void);
bool bpf_jit_supports_far_kfunc_call(void);
bool bpf_jit_supports_exceptions(void);
bool bpf_jit_supports_cleanup_pads(void);
+void arch_bpf_run_cleanup_pad(u64 pad, u64 frame_fp, u64 spill_base);
bool bpf_jit_supports_ptr_xchg(void);
bool bpf_jit_supports_arena(void);
bool bpf_jit_supports_insn(struct bpf_insn *insn, bool in_arena);
diff --git a/kernel/bpf/core.c b/kernel/bpf/core.c
index a379cd1ec4c6..16e5210a56d5 100644
--- a/kernel/bpf/core.c
+++ b/kernel/bpf/core.c
@@ -292,6 +292,7 @@ void __bpf_prog_free(struct bpf_prog *fp)
mutex_destroy(&fp->aux->dst_mutex);
mutex_destroy(&fp->aux->st_ops_assoc_mutex);
kfree(fp->aux->poke_tab);
+ bpf_exc_free_info(fp->aux);
kfree(fp->aux);
}
free_percpu(fp->stats);
@@ -2625,13 +2626,21 @@ static bool bpf_prog_select_interpreter(struct bpf_prog *fp)
return select_interpreter;
}
-static struct bpf_prog *bpf_prog_jit_compile(struct bpf_verifier_env *env, struct bpf_prog *prog)
+static struct bpf_prog *bpf_prog_jit_compile(struct bpf_verifier_env *env, struct bpf_prog *prog,
+ int *err)
{
#ifdef CONFIG_BPF_JIT
struct bpf_prog *orig_prog;
+ int ret;
- if (!bpf_prog_need_blind(prog))
+ if (!bpf_prog_need_blind(prog)) {
+ ret = bpf_exc_attach_main_prog(env, prog);
+ if (ret) {
+ *err = ret;
+ return prog;
+ }
return bpf_int_jit_compile(env, prog);
+ }
orig_prog = prog;
prog = bpf_jit_blind_constants(env, prog);
@@ -2642,6 +2651,13 @@ static struct bpf_prog *bpf_prog_jit_compile(struct bpf_verifier_env *env, struc
if (IS_ERR(prog))
goto out_restore;
+ ret = bpf_exc_attach_main_prog(env, prog);
+ if (ret) {
+ *err = ret;
+ bpf_jit_prog_release_other(orig_prog, prog);
+ goto out_restore;
+ }
+
prog = bpf_int_jit_compile(env, prog);
if (prog->jited) {
bpf_jit_prog_release_other(prog, orig_prog);
@@ -2681,8 +2697,10 @@ struct bpf_prog *__bpf_prog_select_runtime(struct bpf_verifier_env *env, struct
if (*err)
return fp;
- fp = bpf_prog_jit_compile(env, fp);
+ fp = bpf_prog_jit_compile(env, fp, err);
bpf_prog_jit_attempt_done(fp);
+ if (*err)
+ return fp;
if (!fp->jited && jit_needed) {
*err = -ENOTSUPP;
return fp;
@@ -3480,6 +3498,12 @@ bool __weak bpf_jit_supports_cleanup_pads(void)
return false;
}
+/* Call @pad with the frame pointer @frame_fp and r6-r9 spilled at @spill_base. */
+void __weak arch_bpf_run_cleanup_pad(u64 pad, u64 frame_fp, u64 spill_base)
+{
+ WARN_ON_ONCE(1);
+}
+
bool __weak bpf_jit_supports_timed_may_goto(void)
{
return false;
diff --git a/kernel/bpf/exception.c b/kernel/bpf/exception.c
index 3aafbb612e7f..28aba6788847 100644
--- a/kernel/bpf/exception.c
+++ b/kernel/bpf/exception.c
@@ -1,11 +1,14 @@
// SPDX-License-Identifier: GPL-2.0-only
/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
+#include <linux/bitmap.h>
#include <linux/bpf.h>
#include <linux/bpf_verifier.h>
+#include <linux/bsearch.h>
#include <linux/btf.h>
#include <linux/btf_ids.h>
#include <linux/filter.h>
#include <linux/slab.h>
+#include <linux/sort.h>
#include "exception.h"
#define verbose(env, fmt, args...) bpf_verifier_log_write(env, fmt, ##args)
@@ -185,3 +188,141 @@ int bpf_exc_pad_of_call(struct bpf_verifier_env *env, u32 idx)
return pad ? (int)pad - 1 : -1;
}
+
+/*
+ * Every subprogram of a cleanup-carrying program spills the BPF callee-saved
+ * registers, even one that never throws: a frame's spill holds its caller's
+ * registers, and that is what the walker restores before running the caller's
+ * pad. The exception callback does not, because it reuses the boundary frame
+ * rather than building one of its own.
+ */
+bool bpf_exc_force_spill(const struct bpf_prog *prog)
+{
+ return prog->aux->exc && !prog->aux->exception_cb;
+}
+
+bool bpf_exc_needs_throw_spill(const struct bpf_prog *prog)
+{
+ return bpf_exc_force_spill(prog) && prog->aux->exc->has_throw;
+}
+
+const struct bpf_cleanup_range *bpf_exc_pad_for_ip(const struct bpf_prog *prog, u64 ip)
+{
+ const struct bpf_exception_info *exc = prog->aux->exc;
+ u32 l = 0, r = exc ? exc->nr_ranges : 0;
+
+ while (l < r) {
+ u32 m = l + (r - l) / 2;
+ const struct bpf_cleanup_range *rec = &exc->ranges[m];
+
+ if (ip <= rec->begin)
+ r = m;
+ else if (ip > rec->end)
+ l = m + 1;
+ else
+ return rec;
+ }
+ return NULL;
+}
+
+int bpf_exc_alloc_info(struct bpf_prog_aux *aux)
+{
+ if (aux->exc)
+ return 0;
+ aux->exc = kzalloc_obj(struct bpf_exception_info, GFP_KERNEL_ACCOUNT | __GFP_NOWARN);
+ return aux->exc ? 0 : -ENOMEM;
+}
+
+int bpf_exc_attach_info(struct bpf_prog_aux *aux, struct bpf_cleanup_info *recs, u32 cnt)
+{
+ struct bpf_exception_info *exc = aux->exc;
+ struct bpf_cleanup_range *ranges;
+
+ ranges = kvcalloc(cnt, sizeof(*ranges), GFP_KERNEL_ACCOUNT | __GFP_NOWARN);
+ if (!ranges) {
+ kvfree(recs);
+ return -ENOMEM;
+ }
+
+ exc->info = recs;
+ exc->nr_info = cnt;
+ exc->ranges = ranges;
+ /* Withheld until the JIT has filled the table in. */
+ exc->nr_ranges = 0;
+ return 0;
+}
+
+void bpf_exc_fill_native_ranges(struct bpf_prog *prog, u32 *addrs, void *image)
+{
+ struct bpf_exception_info *exc = prog->aux->exc;
+ u32 i, n;
+
+ if (!exc || !exc->nr_info || !exc->ranges)
+ return;
+
+ n = exc->nr_info;
+ for (i = 0; i < n; i++) {
+ const struct bpf_cleanup_info *rec = &exc->info[i];
+
+ if (WARN_ON_ONCE(rec->begin_off >= prog->len ||
+ rec->end_off > prog->len ||
+ rec->landing_pad_off >= prog->len))
+ return;
+ exc->ranges[i].begin = (u64)(long)image + addrs[rec->begin_off];
+ exc->ranges[i].end = (u64)(long)image + addrs[rec->end_off];
+ exc->ranges[i].pad = (u64)(long)image + addrs[rec->landing_pad_off];
+ }
+ exc->nr_ranges = n;
+}
+
+void bpf_exc_free_info(struct bpf_prog_aux *aux)
+{
+ struct bpf_exception_info *exc = aux->exc;
+
+ if (!exc)
+ return;
+ kvfree(exc->ranges);
+ kvfree(exc->info);
+ kfree(exc);
+ aux->exc = NULL;
+}
+
+static const struct bpf_insn_aux_data *subprog_insn_aux(const struct bpf_verifier_env *env,
+ const struct bpf_prog *prog, u32 idx)
+{
+ if (!env || !prog->aux->exc)
+ return NULL;
+ return &env->insn_aux_data[idx + prog->aux->subprog_start];
+}
+
+bool bpf_exc_insn_is_pad(const struct bpf_verifier_env *env,
+ const struct bpf_prog *prog, u32 idx)
+{
+ const struct bpf_insn_aux_data *aux = subprog_insn_aux(env, prog, idx);
+
+ return aux && aux->cleanup_pad_head;
+}
+
+bool bpf_exc_insn_is_throw(const struct bpf_verifier_env *env,
+ const struct bpf_prog *prog, u32 idx)
+{
+ const struct bpf_insn_aux_data *aux = subprog_insn_aux(env, prog, idx);
+
+ return aux && aux->throw_call;
+}
+
+bool bpf_exc_insn_is_resume(const struct bpf_verifier_env *env,
+ const struct bpf_prog *prog, u32 idx)
+{
+ const struct bpf_insn_aux_data *aux = subprog_insn_aux(env, prog, idx);
+
+ return aux && aux->resume_call;
+}
+
+bool bpf_exc_insn_in_pad(const struct bpf_verifier_env *env,
+ const struct bpf_prog *prog, u32 idx)
+{
+ const struct bpf_insn_aux_data *aux = subprog_insn_aux(env, prog, idx);
+
+ return aux && aux->in_cleanup_pad;
+}
diff --git a/kernel/bpf/exception.h b/kernel/bpf/exception.h
index 0872a3f70583..7a4bfc201e1c 100644
--- a/kernel/bpf/exception.h
+++ b/kernel/bpf/exception.h
@@ -5,6 +5,10 @@
#include <linux/types.h>
+struct bpf_cleanup_info;
+struct bpf_cleanup_range;
+struct bpf_prog;
+struct bpf_prog_aux;
struct bpf_insn;
struct bpf_verifier_env;
@@ -12,5 +16,8 @@ int bpf_prepare_cleanup_exceptions(struct bpf_verifier_env *env);
int bpf_exc_check_insn(struct bpf_verifier_env *env, struct bpf_insn *insn);
int bpf_exc_check_callback(struct bpf_verifier_env *env, int subprog);
int bpf_exc_pad_of_call(struct bpf_verifier_env *env, u32 idx);
+int bpf_exc_alloc_info(struct bpf_prog_aux *aux);
+int bpf_exc_attach_info(struct bpf_prog_aux *aux, struct bpf_cleanup_info *recs, u32 cnt);
+const struct bpf_cleanup_range *bpf_exc_pad_for_ip(const struct bpf_prog *prog, u64 ip);
#endif /* _LINUX_BPF_EXCEPTION_H */
diff --git a/kernel/bpf/fixups.c b/kernel/bpf/fixups.c
index 53a737c88dcc..02e65c57bf0a 100644
--- a/kernel/bpf/fixups.c
+++ b/kernel/bpf/fixups.c
@@ -1,5 +1,6 @@
// SPDX-License-Identifier: GPL-2.0-only
/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
+#include <linux/bitmap.h>
#include <linux/bpf.h>
#include <linux/btf.h>
#include <linux/bpf_verifier.h>
@@ -10,6 +11,7 @@
#include <linux/perf_event.h>
#include <net/xdp.h>
#include "disasm.h"
+#include "exception.h"
#define verbose(env, fmt, args...) bpf_verifier_log_write(env, fmt, ##args)
@@ -1126,6 +1128,61 @@ static void bpf_restore_subprog_starts(struct bpf_verifier_env *env, u32 *orig_s
env->subprog_info[env->subprog_cnt].start = env->prog->len;
}
+static int exc_info_for_subprog(struct bpf_verifier_env *env, struct bpf_prog *sub,
+ u32 start, u32 end)
+{
+ struct bpf_cleanup_info *recs;
+ u32 i, cnt = 0;
+ int err;
+
+ if (!env->cleanup_info_cnt)
+ return 0;
+
+ err = bpf_exc_alloc_info(sub->aux);
+ if (err)
+ return err;
+
+ for (i = start; i < end; i++) {
+ if (env->insn_aux_data[i].throw_call)
+ sub->aux->exc->has_throw = true;
+ if (env->insn_aux_data[i].cleanup_pad)
+ cnt++;
+ }
+ if (!cnt)
+ return 0;
+
+ recs = kvmalloc_array(cnt, sizeof(*recs), GFP_KERNEL_ACCOUNT | __GFP_NOWARN);
+ if (!recs)
+ return -ENOMEM;
+
+ for (i = start, cnt = 0; i < end; i++) {
+ u32 pad = env->insn_aux_data[i].cleanup_pad;
+
+ if (!pad)
+ continue;
+ pad--;
+ if (verifier_bug_if(pad < start || pad >= end, env,
+ "insn %u is covered by a landing pad at %u outside its subprog [%u, %u)",
+ i, pad, start, end)) {
+ kvfree(recs);
+ return -EFAULT;
+ }
+ env->insn_aux_data[pad].cleanup_pad_head = true;
+ recs[cnt].begin_off = i - start;
+ recs[cnt].end_off = i - start + 1;
+ recs[cnt].landing_pad_off = pad - start;
+ cnt++;
+ }
+ return bpf_exc_attach_info(sub->aux, recs, cnt);
+}
+
+int bpf_exc_attach_main_prog(struct bpf_verifier_env *env, struct bpf_prog *prog)
+{
+ if (!env || env->subprog_cnt > 1)
+ return 0;
+ return exc_info_for_subprog(env, prog, 0, prog->len);
+}
+
static int jit_subprogs(struct bpf_verifier_env *env)
{
struct bpf_prog *prog = env->prog, **func, *tmp;
@@ -1263,6 +1320,10 @@ static int jit_subprogs(struct bpf_verifier_env *env)
func[i]->aux->token = prog->aux->token;
if (!i)
func[i]->aux->exception_boundary = env->seen_exception;
+ err = exc_info_for_subprog(env, func[i], subprog_start,
+ subprog_end);
+ if (err)
+ goto out_free;
func[i] = bpf_int_jit_compile(env, func[i]);
if (!func[i]->jited) {
err = -ENOTSUPP;
@@ -1367,6 +1428,8 @@ static int jit_subprogs(struct bpf_verifier_env *env)
prog->aux->bpf_exception_cb = (void *)func[env->exception_callback_subprog]->bpf_func;
prog->aux->exception_boundary = func[0]->aux->exception_boundary;
prog->aux->stack_arg_sp_adjust = func[0]->aux->stack_arg_sp_adjust;
+ prog->aux->exc = func[0]->aux->exc;
+ func[0]->aux->exc = NULL;
bpf_prog_jit_attempt_done(prog);
return 0;
out_free:
@@ -1947,6 +2010,8 @@ int bpf_do_misc_fixups(struct bpf_verifier_env *env)
goto next_insn;
if (insn->src_reg == BPF_PSEUDO_CALL)
goto next_insn;
+ if (env->insn_aux_data[i + delta].resume_call)
+ goto next_insn;
if (insn->src_reg == BPF_PSEUDO_KFUNC_CALL) {
ret = bpf_fixup_kfunc_call(env, insn, insn_buf, i + delta, &cnt);
if (ret)
diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c
index 5eadab4dfa3f..03af739b9909 100644
--- a/kernel/bpf/helpers.c
+++ b/kernel/bpf/helpers.c
@@ -31,6 +31,7 @@
#include <linux/buildid.h>
#include "../../lib/kstrtox.h"
+#include "exception.h"
/* If kernel subsystem is allowing eBPF programs to call this function,
* inside its own verifier_ops->get_func_proto() callback it should return
@@ -3398,8 +3399,36 @@ struct bpf_throw_ctx {
u64 sp;
u64 bp;
int cnt;
+ const struct bpf_prog *callee;
+ u64 callee_fp;
};
+static void bpf_run_cleanup_pad(struct bpf_throw_ctx *ctx, const struct bpf_prog *prog,
+ u64 ip, u64 fp)
+{
+ const struct bpf_exception_info *exc = prog->aux->exc;
+ const struct bpf_cleanup_range *rec;
+ u64 spill_base;
+
+ if (!exc || !exc->nr_ranges)
+ return;
+ rec = bpf_exc_pad_for_ip(prog, ip);
+ if (!rec)
+ return;
+
+ /*
+ * The callee is always another subprogram of this program -- the walk
+ * ends at any frame that is not one -- so its prologue spilled these
+ * registers and its exc is there to say where.
+ */
+ if (ctx->callee)
+ spill_base = ctx->callee_fp + ctx->callee->aux->exc->spill_off;
+ else
+ spill_base = fp + exc->throw_spill_off;
+
+ arch_bpf_run_cleanup_pad(rec->pad, fp, spill_base);
+}
+
static bool bpf_stack_walker(void *cookie, u64 ip, u64 sp, u64 bp)
{
struct bpf_throw_ctx *ctx = cookie;
@@ -3416,6 +3445,11 @@ static bool bpf_stack_walker(void *cookie, u64 ip, u64 sp, u64 bp)
if (!prog)
return !ctx->cnt;
ctx->cnt++;
+
+ bpf_run_cleanup_pad(ctx, prog, ip, bp);
+ ctx->callee = prog;
+ ctx->callee_fp = bp;
+
if (bpf_is_subprog(prog))
return true;
ctx->aux = prog->aux;
--
2.53.0-Meta
^ permalink raw reply related [flat|nested] 49+ messages in thread* [PATCH bpf-next v5 12/21] bpf, x86: Dispatch exception cleanup pads at run time
2026-09-23 4:58 [PATCH bpf-next v5 00/21] bpf: Run exception cleanup landing pads when bpf_throw() unwinds Yonghong Song
` (10 preceding siblings ...)
2026-09-23 4:59 ` [PATCH bpf-next v5 11/21] bpf: Dispatch exception cleanup pads from bpf_throw() Yonghong Song
@ 2026-09-23 4:59 ` Yonghong Song
2026-09-23 6:45 ` Eduard Zingerman
2026-09-23 4:59 ` [PATCH bpf-next v5 13/21] bpf, arm64: " Yonghong Song
` (8 subsequent siblings)
20 siblings, 1 reply; 49+ messages in thread
From: Yonghong Song @ 2026-09-23 4:59 UTC (permalink / raw)
To: bpf
Cc: Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann,
Eduard Zingerman, kernel-team
Provide the arch half for x86-64: force the full callee-saved spill for a
program carrying a cleanup table so the walker can find a frame's r6-r9 in
its callee's prologue, record where that spill area starts, build the
native cleanup table from the JIT's addrs[], emit a bare return for a pad's
bpf_unwind_resume(), and hand control to a pad from
arch_bpf_run_cleanup_pad().
Support is gated on CONFIG_UNWINDER_ORC, the same requirement
arch_bpf_stack_walk() and therefore bpf_throw() already have here, and
bpf_cleanup_pad.o is built only there. Nothing else can call it, and a
frame-pointer build would have objtool validate a routine that has to carry
BPF r10 in rbp rather than a frame pointer ("call without frame pointer
save/setup", an error under CONFIG_OBJTOOL_WERROR).
Signed-off-by: Yonghong Song <yonghong.song@linux.dev>
---
arch/x86/net/Makefile | 3 ++
arch/x86/net/bpf_cleanup_pad.S | 79 +++++++++++++++++++++++++++++++++
arch/x86/net/bpf_jit_comp.c | 81 +++++++++++++++++++++++++++++-----
3 files changed, 151 insertions(+), 12 deletions(-)
create mode 100644 arch/x86/net/bpf_cleanup_pad.S
diff --git a/arch/x86/net/Makefile b/arch/x86/net/Makefile
index dddbefc0f439..8bb22644ba10 100644
--- a/arch/x86/net/Makefile
+++ b/arch/x86/net/Makefile
@@ -7,4 +7,7 @@ ifeq ($(CONFIG_X86_32),y)
obj-$(CONFIG_BPF_JIT) += bpf_jit_comp32.o
else
obj-$(CONFIG_BPF_JIT) += bpf_jit_comp.o bpf_timed_may_goto.o
+ ifdef CONFIG_UNWINDER_ORC
+ obj-$(CONFIG_BPF_JIT) += bpf_cleanup_pad.o
+ endif
endif
diff --git a/arch/x86/net/bpf_cleanup_pad.S b/arch/x86/net/bpf_cleanup_pad.S
new file mode 100644
index 000000000000..10d612b11f36
--- /dev/null
+++ b/arch/x86/net/bpf_cleanup_pad.S
@@ -0,0 +1,79 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
+
+#include <linux/linkage.h>
+#include <asm/nospec-branch.h>
+
+/*
+ * The x86-64 BPF JIT prologue spills, once bpf_exc_force_spill() makes it
+ * unconditional, r12, rbx, r13, r14 and r15 in that order -- so within the
+ * spill area the lowest address holds r15 and the highest r12. The throw-site
+ * spill the JIT emits uses the same layout, so the routine below reads both
+ * the same way:
+ *
+ * spill_base + 0 BPF r9 (r15)
+ * spill_base + 8 BPF r8 (r14)
+ * spill_base + 16 BPF r7 (r13)
+ * spill_base + 24 BPF r6 (rbx)
+ * spill_base + 32 r12 (arena base, not a BPF register)
+ */
+
+ .code64
+ .section .text, "ax"
+
+/*
+ * void arch_bpf_run_cleanup_pad(u64 pad, u64 frame_fp, u64 spill_base)
+ *
+ * rdi = native address of the landing pad
+ * rsi = frame pointer of the frame the pad belongs to
+ * rdx = spill area holding that frame's BPF callee-saved registers
+ *
+ * Give the pad the register state of its own frame and call it. The pad ends
+ * in the bare return the JIT emits for its bpf_unwind_resume(), so it comes
+ * back here rather than returning to its frame's caller. It runs on this
+ * stack, far below the frame it is cleaning up after, so nothing it calls can
+ * reach into that frame.
+ *
+ * rbp addresses the pad's frame and rsp this one, so the two are not the
+ * neighbours a JITed frame expects them to be. That is why
+ * cleanup_check_pad_insn() refuses a call in a pad that passes an argument on
+ * the stack: the JIT stages those relative to rbp, and the callee reads them
+ * relative to rsp.
+ */
+SYM_FUNC_START(arch_bpf_run_cleanup_pad)
+ ANNOTATE_NOENDBR
+
+ pushq %rbp
+ pushq %rbx
+ pushq %r12
+ pushq %r13
+ pushq %r14
+ pushq %r15
+ /* Keep the pad's entry rsp congruent to a normal call's. */
+ subq $8, %rsp
+
+ movq 0(%rdx), %r15
+ movq 8(%rdx), %r14
+ movq 16(%rdx), %r13
+ movq 24(%rdx), %rbx
+ movq 32(%rdx), %r12
+ /* rbp is BPF r10, so this is the whole of the pad's frame setup. */
+ movq %rsi, %rbp
+
+ /* CALL_NOSPEC needs the target in a register; rcx is BPF r4, dead. */
+ movq %rdi, %rcx
+
+ /* BPF r0 on the way into a pad, not whatever the kernel left in rax. */
+ movl $1, %eax
+
+ CALL_NOSPEC rcx
+
+ addq $8, %rsp
+ popq %r15
+ popq %r14
+ popq %r13
+ popq %r12
+ popq %rbx
+ popq %rbp
+ RET
+SYM_FUNC_END(arch_bpf_run_cleanup_pad)
diff --git a/arch/x86/net/bpf_jit_comp.c b/arch/x86/net/bpf_jit_comp.c
index d4a980140b48..26df52050cd7 100644
--- a/arch/x86/net/bpf_jit_comp.c
+++ b/arch/x86/net/bpf_jit_comp.c
@@ -357,6 +357,12 @@ struct jit_context {
/* Number of bytes that will be skipped on tailcall */
#define X86_TAIL_CALL_OFFSET (12 + ENDBR_INSN_SIZE)
+/*
+ * Throw-site spill: r15, r14, r13, rbx, r12 low to high, the layout the
+ * prologue's pushes leave, so arch_bpf_run_cleanup_pad() reads both alike.
+ */
+#define X86_CLEANUP_SPILL_SZ (5 * 8)
+
static void push_r9(u8 **pprog)
{
u8 *prog = *pprog;
@@ -832,7 +838,7 @@ static void emit_bpf_tail_call_indirect(struct bpf_prog *bpf_prog,
/* Inc tail_call_cnt if the slot is populated. */
EMIT4(0x48, 0x83, 0x00, 0x01); /* add qword ptr [rax], 1 */
- if (bpf_prog->aux->exception_boundary) {
+ if (bpf_prog->aux->exception_boundary || bpf_exc_force_spill(bpf_prog)) {
pop_callee_regs(&prog, all_callee_regs_used);
pop_r12(&prog);
} else {
@@ -899,7 +905,7 @@ static void emit_bpf_tail_call_direct(struct bpf_prog *bpf_prog,
/* Inc tail_call_cnt if the slot is populated. */
EMIT4(0x48, 0x83, 0x00, 0x01); /* add qword ptr [rax], 1 */
- if (bpf_prog->aux->exception_boundary) {
+ if (bpf_prog->aux->exception_boundary || bpf_exc_force_spill(bpf_prog)) {
pop_callee_regs(&prog, all_callee_regs_used);
pop_r12(&prog);
} else {
@@ -1977,6 +1983,7 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int *
u8 *ip, *prog = temp;
u32 stack_depth;
int callee_saved_size;
+ u32 throw_spill, prologue_depth;
s32 outgoing_arg_base;
int err;
@@ -2015,7 +2022,10 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int *
detect_reg_usage(insn, insn_cnt, callee_regs_used);
- emit_prologue(&prog, image, stack_depth,
+ throw_spill = bpf_exc_needs_throw_spill(bpf_prog) ? X86_CLEANUP_SPILL_SZ : 0;
+ prologue_depth = stack_depth + throw_spill;
+
+ emit_prologue(&prog, image, prologue_depth,
bpf_prog_was_classic(bpf_prog), tail_call_reachable,
bpf_is_subprog(bpf_prog), bpf_prog->aux->exception_cb);
@@ -2024,7 +2034,7 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int *
/* Exception callback will clobber callee regs for its own use, and
* restore the original callee regs from main prog's stack frame.
*/
- if (bpf_prog->aux->exception_boundary) {
+ if (bpf_prog->aux->exception_boundary || bpf_exc_force_spill(bpf_prog)) {
/* We also need to save r12, which is not mapped to any BPF
* register, as we throw after entry into the kernel, which may
* overwrite r12.
@@ -2039,9 +2049,10 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int *
/* Compute callee-saved register area size. */
callee_saved_size = 0;
- if (bpf_prog->aux->exception_boundary || arena_vm_start)
+ if (bpf_prog->aux->exception_boundary || bpf_exc_force_spill(bpf_prog) ||
+ arena_vm_start)
callee_saved_size += 8; /* r12 */
- if (bpf_prog->aux->exception_boundary) {
+ if (bpf_prog->aux->exception_boundary || bpf_exc_force_spill(bpf_prog)) {
callee_saved_size += 4 * 8; /* rbx, r13, r14, r15 */
} else {
int j;
@@ -2063,7 +2074,21 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int *
* Note that tail_call_reachable is guaranteed to be false when
* stack args exist, so tcc pushes need not be accounted for.
*/
- outgoing_arg_base = -(round_up(stack_depth, 8) + callee_saved_size);
+ outgoing_arg_base = -(round_up(stack_depth, 8) + throw_spill + callee_saved_size);
+
+ /*
+ * Lowest address of each spill area, as an offset from rbp; see
+ * bpf_cleanup_pad.S for the layout. The 16 is the tail call counter
+ * pair emit_prologue_tail_call() pushes above the callee-saved one.
+ * Only a frame that throws has a throw-site area.
+ */
+ if (bpf_exc_force_spill(bpf_prog))
+ bpf_prog->aux->exc->spill_off = -(round_up(stack_depth, 8) + throw_spill +
+ (tail_call_reachable ? 16 : 0) +
+ callee_saved_size);
+ if (bpf_exc_needs_throw_spill(bpf_prog))
+ bpf_prog->aux->exc->throw_spill_off =
+ -(round_up(stack_depth, 8) + throw_spill);
/*
* Allocate outgoing stack arg area for args 7+ only.
@@ -2110,7 +2135,8 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int *
dst_reg = X86_REG_R9;
}
- if (bpf_insn_is_indirect_target(env, bpf_prog, i - 1))
+ if (bpf_insn_is_indirect_target(env, bpf_prog, i - 1) ||
+ bpf_exc_insn_is_pad(env, bpf_prog, i - 1))
EMIT_ENDBR();
ip = image + addrs[i - 1] + (prog - temp);
@@ -2903,9 +2929,27 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int *
case BPF_JMP | BPF_CALL: {
const struct btf_func_model *fm = NULL;
+ if (bpf_exc_insn_is_throw(env, bpf_prog, i - 1)) {
+ /* Spill r6-r9 and r12 where the bpf_throw() walker looks. */
+ s32 off = bpf_prog->aux->exc->throw_spill_off;
+ u8 *spill = prog;
+
+ emit_stx(&prog, BPF_DW, BPF_REG_FP, BPF_REG_9, off + 0);
+ emit_stx(&prog, BPF_DW, BPF_REG_FP, BPF_REG_8, off + 8);
+ emit_stx(&prog, BPF_DW, BPF_REG_FP, BPF_REG_7, off + 16);
+ emit_stx(&prog, BPF_DW, BPF_REG_FP, BPF_REG_6, off + 24);
+ emit_stx(&prog, BPF_DW, BPF_REG_FP, X86_REG_R12, off + 32);
+ ip += prog - spill;
+ }
+
+ if (bpf_exc_insn_is_resume(env, bpf_prog, i - 1)) {
+ emit_return(&prog, image + addrs[i - 1] + (prog - temp));
+ break;
+ }
+
func = (u8 *) __bpf_call_base + imm32;
if (src_reg == BPF_PSEUDO_CALL && tail_call_reachable) {
- LOAD_TAIL_CALL_CNT_PTR(stack_depth);
+ LOAD_TAIL_CALL_CNT_PTR(prologue_depth);
ip += 7;
}
if (!imm32)
@@ -2948,13 +2992,13 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int *
&prog,
ip,
callee_regs_used,
- stack_depth,
+ prologue_depth,
ctx);
else
emit_bpf_tail_call_indirect(bpf_prog,
&prog,
callee_regs_used,
- stack_depth,
+ prologue_depth,
ip,
ctx);
break;
@@ -3215,7 +3259,8 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int *
}
/* Deallocate outgoing args 7+ area. */
emit_add_rsp(&prog, outgoing_rsp);
- if (bpf_prog->aux->exception_boundary) {
+ if (bpf_prog->aux->exception_boundary ||
+ bpf_exc_force_spill(bpf_prog)) {
pop_callee_regs(&prog, all_callee_regs_used);
pop_r12(&prog);
} else {
@@ -4385,6 +4430,13 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_verifier_env *env, struct bpf_pr
*/
bpf_prog_update_insn_ptrs(prog, addrs, image);
+ /*
+ * Same mapping, consumed by the bpf_throw() frame walker:
+ * turn the cleanup records into native address ranges now
+ * that the image is final.
+ */
+ bpf_exc_fill_native_ranges(prog, addrs, image);
+
/*
* ctx.prog_offset is used when CFI preambles put code *before*
* the function. See emit_cfi(). For FineIBT specifically this code
@@ -4501,6 +4553,11 @@ bool bpf_jit_supports_exceptions(void)
return IS_ENABLED(CONFIG_UNWINDER_ORC);
}
+bool bpf_jit_supports_cleanup_pads(void)
+{
+ return IS_ENABLED(CONFIG_UNWINDER_ORC);
+}
+
bool bpf_jit_supports_private_stack(void)
{
return true;
--
2.53.0-Meta
^ permalink raw reply related [flat|nested] 49+ messages in thread* Re: [PATCH bpf-next v5 12/21] bpf, x86: Dispatch exception cleanup pads at run time
2026-09-23 4:59 ` [PATCH bpf-next v5 12/21] bpf, x86: Dispatch exception cleanup pads at run time Yonghong Song
@ 2026-09-23 6:45 ` Eduard Zingerman
2026-09-23 20:45 ` Yonghong Song
0 siblings, 1 reply; 49+ messages in thread
From: Eduard Zingerman @ 2026-09-23 6:45 UTC (permalink / raw)
To: Yonghong Song, bpf
Cc: Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann, kernel-team
On Tue, 2026-09-22 at 21:59 -0700, Yonghong Song wrote:
...
> diff --git a/arch/x86/net/bpf_jit_comp.c b/arch/x86/net/bpf_jit_comp.c
> index d4a980140b48..26df52050cd7 100644
> --- a/arch/x86/net/bpf_jit_comp.c
> +++ b/arch/x86/net/bpf_jit_comp.c
...
> @@ -2024,7 +2034,7 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int *
> /* Exception callback will clobber callee regs for its own use, and
> * restore the original callee regs from main prog's stack frame.
> */
> - if (bpf_prog->aux->exception_boundary) {
> + if (bpf_prog->aux->exception_boundary || bpf_exc_force_spill(bpf_prog)) {
On option to avoid spilling all the registers would be to use register
liveness information at call sites and collect a union of registers
alive at landing pads for all callers of this particular function.
Wdyt?
> /* We also need to save r12, which is not mapped to any BPF
> * register, as we throw after entry into the kernel, which may
> * overwrite r12.
...
^ permalink raw reply [flat|nested] 49+ messages in thread* Re: [PATCH bpf-next v5 12/21] bpf, x86: Dispatch exception cleanup pads at run time
2026-09-23 6:45 ` Eduard Zingerman
@ 2026-09-23 20:45 ` Yonghong Song
0 siblings, 0 replies; 49+ messages in thread
From: Yonghong Song @ 2026-09-23 20:45 UTC (permalink / raw)
To: Eduard Zingerman, bpf
Cc: Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann, kernel-team
On 9/22/26 11:45 PM, Eduard Zingerman wrote:
> On Tue, 2026-09-22 at 21:59 -0700, Yonghong Song wrote:
>
> ...
>
>> diff --git a/arch/x86/net/bpf_jit_comp.c b/arch/x86/net/bpf_jit_comp.c
>> index d4a980140b48..26df52050cd7 100644
>> --- a/arch/x86/net/bpf_jit_comp.c
>> +++ b/arch/x86/net/bpf_jit_comp.c
> ...
>
>> @@ -2024,7 +2034,7 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int *
>> /* Exception callback will clobber callee regs for its own use, and
>> * restore the original callee regs from main prog's stack frame.
>> */
>> - if (bpf_prog->aux->exception_boundary) {
>> + if (bpf_prog->aux->exception_boundary || bpf_exc_force_spill(bpf_prog)) {
> On option to avoid spilling all the registers would be to use register
> liveness information at call sites and collect a union of registers
> alive at landing pads for all callers of this particular function.
> Wdyt?
I will try to fix the above to avoid saving unnecessary callee saved registers.
The below is the code in x86 bpf_jit_comp.c:
push_callee_regs(&prog, callee_regs_used);
>
>> /* We also need to save r12, which is not mapped to any BPF
>> * register, as we throw after entry into the kernel, which may
>> * overwrite r12.
> ...
^ permalink raw reply [flat|nested] 49+ messages in thread
* [PATCH bpf-next v5 13/21] bpf, arm64: Dispatch exception cleanup pads at run time
2026-09-23 4:58 [PATCH bpf-next v5 00/21] bpf: Run exception cleanup landing pads when bpf_throw() unwinds Yonghong Song
` (11 preceding siblings ...)
2026-09-23 4:59 ` [PATCH bpf-next v5 12/21] bpf, x86: Dispatch exception cleanup pads at run time Yonghong Song
@ 2026-09-23 4:59 ` Yonghong Song
2026-09-23 4:59 ` [PATCH bpf-next v5 14/21] libbpf: Resolve the compiler's _Unwind_Resume to the kernel's kfunc Yonghong Song
` (7 subsequent siblings)
20 siblings, 0 replies; 49+ messages in thread
From: Yonghong Song @ 2026-09-23 4:59 UTC (permalink / raw)
To: bpf
Cc: Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann,
Eduard Zingerman, kernel-team
The same arch half for arm64: force the full callee-saved spill, record
where it starts, build the native table from the JIT's byte offsets, and
hand control to a pad from arch_bpf_run_cleanup_pad().
Unlike x86-64 a pad cannot simply return: every call it makes clobbers x30,
so no return address survives to its resume. x23 carries it instead.
bpf2a64[] maps nothing to x23 or x24 -- they are pushed only to keep the
frame shape the exception callback expects -- so nothing else in generated
code touches them, and being callee-saved they survive every kfunc the pad
calls. The JIT emits "br x23" for a pad's bpf_unwind_resume().
x24 is the other one, and it is what makes a pad able to touch its own
frame at all. Generated code addresses the BPF frame through the stack
pointer -- the frame sits directly on top of it, which turns every offset
positive and each access into one instruction -- and in a pad the stack
pointer is the walker's. So the JIT has a pad recompute the equivalent of
its frame's stack pointer from BPF r10 on entry, into x24, and addresses
the frame off x24 for every instruction the previous patches marked as
running only while unwinding. One instruction per pad, and the accesses
keep the shape and the immediate range they have everywhere else.
Signed-off-by: Yonghong Song <yonghong.song@linux.dev>
---
arch/arm64/net/Makefile | 2 +-
arch/arm64/net/bpf_cleanup_pad.S | 94 ++++++++++++++++++++++++++++++++
arch/arm64/net/bpf_jit_comp.c | 92 +++++++++++++++++++++++++++++--
3 files changed, 181 insertions(+), 7 deletions(-)
create mode 100644 arch/arm64/net/bpf_cleanup_pad.S
diff --git a/arch/arm64/net/Makefile b/arch/arm64/net/Makefile
index 3ae382bfca87..ebec2a44a52b 100644
--- a/arch/arm64/net/Makefile
+++ b/arch/arm64/net/Makefile
@@ -2,4 +2,4 @@
#
# ARM64 networking code
#
-obj-$(CONFIG_BPF_JIT) += bpf_jit_comp.o bpf_timed_may_goto.o
+obj-$(CONFIG_BPF_JIT) += bpf_jit_comp.o bpf_timed_may_goto.o bpf_cleanup_pad.o
diff --git a/arch/arm64/net/bpf_cleanup_pad.S b/arch/arm64/net/bpf_cleanup_pad.S
new file mode 100644
index 000000000000..729b1ef3e585
--- /dev/null
+++ b/arch/arm64/net/bpf_cleanup_pad.S
@@ -0,0 +1,94 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
+
+#include <linux/linkage.h>
+
+/*
+ * A frame's prologue pushes the tail call counter pair, and then -- once
+ * bpf_exc_force_spill() says so -- x19/x20, x21/x22, x23/x24, x25/x26 and
+ * x27/x28. Each A64_PUSH pre-decrements, so the lowest address of the spill
+ * area holds x27 and the highest x20:
+ *
+ * spill_base + 0 x27 (private stack pointer)
+ * spill_base + 8 x28 (arena base)
+ * spill_base + 16 x25 (BPF r10, the frame pointer)
+ * spill_base + 24 x26 (tail call counter pointer)
+ * spill_base + 32 x23 -- the pad's own, see below
+ * spill_base + 40 x24 -- likewise
+ * spill_base + 48 x21 (BPF r8)
+ * spill_base + 56 x22 (BPF r9)
+ * spill_base + 64 x19 (BPF r6)
+ * spill_base + 72 x20 (BPF r7)
+ *
+ * Neither x23 nor x24 is restored from that spill: the pad has its own use for
+ * both. bpf2a64[] maps nothing to either -- they are pushed only to keep the
+ * frame shape the exception callback expects -- so nothing else in generated
+ * code touches them, and being callee-saved they survive every call the pad
+ * makes.
+ *
+ * x23 is the pad's return address. Unlike x86-64 a pad cannot simply return:
+ * every call it makes clobbers x30, so nothing is left to return through by
+ * the time it reaches its resume. The JIT emits "br x23" for the pad's
+ * bpf_unwind_resume() and this routine puts .Lcleanup_pad_done there.
+ *
+ * x24 is where the pad's frame is anchored. Generated code addresses the BPF
+ * frame through the stack pointer, which here is this routine's rather than
+ * the unwinding frame's, so the JIT has the pad recompute the equivalent from
+ * BPF r10 on entry and address its frame off x24 for as long as it runs.
+ *
+ * Both of those branches are indirect, so both targets carry a BTI landing
+ * marker: the JIT emits one at each pad, and .Lcleanup_pad_done below has one
+ * of its own.
+ */
+
+ .text
+
+/*
+ * void arch_bpf_run_cleanup_pad(u64 pad, u64 frame_fp, u64 spill_base)
+ *
+ * x0 = native address of the landing pad
+ * x1 = frame pointer of the frame the pad belongs to (unused here: BPF r10 is
+ * x25, which the spill area already holds)
+ * x2 = spill area holding that frame's BPF callee-saved registers
+ *
+ * Give the pad the register state of its own frame and call it. It runs on
+ * this stack, far below the frame it is cleaning up after, so nothing it
+ * calls can reach into that frame.
+ */
+SYM_FUNC_START(arch_bpf_run_cleanup_pad)
+ /* Save the kernel's callee-saved registers; the pad owns them next. */
+ stp x29, x30, [sp, #-96]!
+ mov x29, sp
+ stp x19, x20, [sp, #16]
+ stp x21, x22, [sp, #32]
+ stp x23, x24, [sp, #48]
+ stp x25, x26, [sp, #64]
+ stp x27, x28, [sp, #80]
+
+ /* x9 is BPF_REG_AX, so the pad's address does not stay in BPF r1. */
+ mov x9, x0
+
+ ldp x27, x28, [x2, #0]
+ ldp x25, x26, [x2, #16]
+ ldp x21, x22, [x2, #48]
+ ldp x19, x20, [x2, #64]
+
+ /* BPF r0 (x8) on the way into a pad, not whatever the kernel left. */
+ mov x8, #1
+
+ /* Where the pad's resume branches back to. */
+ adr x23, .Lcleanup_pad_done
+
+ br x9
+
+.Lcleanup_pad_done:
+ /* Reached by the pad's "br x23", so it is an indirect branch target. */
+ bti j
+ ldp x19, x20, [sp, #16]
+ ldp x21, x22, [sp, #32]
+ ldp x23, x24, [sp, #48]
+ ldp x25, x26, [sp, #64]
+ ldp x27, x28, [sp, #80]
+ ldp x29, x30, [sp], #96
+ ret
+SYM_FUNC_END(arch_bpf_run_cleanup_pad)
diff --git a/arch/arm64/net/bpf_jit_comp.c b/arch/arm64/net/bpf_jit_comp.c
index 6c04fee46876..c7209c26b235 100644
--- a/arch/arm64/net/bpf_jit_comp.c
+++ b/arch/arm64/net/bpf_jit_comp.c
@@ -10,6 +10,7 @@
#include <linux/arm-smccc.h>
#include <linux/bitfield.h>
#include <linux/bpf.h>
+#include <linux/bpf_verifier.h>
#include <linux/cfi.h>
#include <linux/filter.h>
#include <linux/memory.h>
@@ -75,6 +76,19 @@ static const int bpf2a64[] = {
[ARENA_VM_START] = A64_R(28),
};
+/*
+ * Throw-site spill: the five pairs push_callee_regs() forces on, same size and
+ * slot order, so arch_bpf_run_cleanup_pad() reads both alike.
+ */
+#define A64_CLEANUP_SPILL_SZ (5 * 16)
+
+/*
+ * Where a landing pad's frame is anchored, since the stack pointer generated
+ * code normally addresses it through is the walker's inside a pad. bpf2a64[]
+ * maps nothing to x24, so nothing else in generated code touches it.
+ */
+#define A64_CLEANUP_FP A64_R(24)
+
struct jit_ctx {
const struct bpf_prog *prog;
int idx;
@@ -87,6 +101,8 @@ struct jit_ctx {
__le32 *ro_image;
u32 stack_size;
u16 stack_arg_size;
+ /* Bytes reserved for the throw-site spill; 0 unless this frame throws. */
+ u32 throw_spill;
u64 user_vm_start;
u64 arena_vm_start;
bool fp_used;
@@ -432,7 +448,7 @@ static void push_callee_regs(struct jit_ctx *ctx)
* Callee-saved registers as the exception callback needs to recover
* all ARM64 Callee-saved registers in its epilogue.
*/
- if (ctx->prog->aux->exception_boundary) {
+ if (ctx->prog->aux->exception_boundary || bpf_exc_force_spill(ctx->prog)) {
emit(A64_PUSH(A64_R(19), A64_R(20), A64_SP), ctx);
emit(A64_PUSH(A64_R(21), A64_R(22), A64_SP), ctx);
emit(A64_PUSH(A64_R(23), A64_R(24), A64_SP), ctx);
@@ -466,7 +482,8 @@ static void pop_callee_regs(struct jit_ctx *ctx)
* program's stack frame, so recover these extra registers in the above
* two cases.
*/
- if (aux->exception_boundary || aux->exception_cb) {
+ if (aux->exception_boundary || aux->exception_cb ||
+ bpf_exc_force_spill(ctx->prog)) {
emit(A64_POP(A64_R(27), A64_R(28), A64_SP), ctx);
emit(A64_POP(A64_R(25), A64_R(26), A64_SP), ctx);
emit(A64_POP(A64_R(23), A64_R(24), A64_SP), ctx);
@@ -602,6 +619,22 @@ static int build_prologue(struct jit_ctx *ctx, bool ebpf_from_cbpf)
emit(A64_SUB_I(1, A64_SP, A64_FP, 96), ctx);
}
+ /*
+ * Lowest address of each spill area, as an offset from A64_FP; see
+ * bpf_cleanup_pad.S for the layout. The 16 is the tail call counter
+ * pair pushed just below the frame record, and the throw-site area,
+ * which only a frame that throws has, sits below the callee-saved one
+ * rather than in the program stack.
+ */
+ if (bpf_exc_force_spill(prog))
+ prog->aux->exc->spill_off = -(16 + A64_CLEANUP_SPILL_SZ);
+ if (bpf_exc_needs_throw_spill(prog)) {
+ ctx->throw_spill = A64_CLEANUP_SPILL_SZ;
+ emit(A64_SUB_I(1, A64_SP, A64_SP, ctx->throw_spill), ctx);
+ prog->aux->exc->throw_spill_off =
+ -(16 + A64_CLEANUP_SPILL_SZ) - ctx->throw_spill;
+ }
+
/* Stack must be multiples of 16B */
ctx->stack_size = round_up(prog->aux->stack_depth, 16);
@@ -691,6 +724,9 @@ static int emit_bpf_tail_call(struct jit_ctx *ctx)
if (ctx->stack_size && !ctx->priv_sp_used)
emit(A64_ADD_I(1, A64_SP, A64_SP, ctx->stack_size), ctx);
+ if (ctx->throw_spill)
+ emit(A64_ADD_I(1, A64_SP, A64_SP, ctx->throw_spill), ctx);
+
pop_callee_regs(ctx);
/* goto *(prog->bpf_func + prologue_offset); */
@@ -1055,6 +1091,9 @@ static void build_epilogue(struct jit_ctx *ctx, bool was_classic)
if (ctx->stack_size && !ctx->priv_sp_used)
emit(A64_ADD_I(1, A64_SP, A64_SP, ctx->stack_size), ctx);
+ if (ctx->throw_spill)
+ emit(A64_ADD_I(1, A64_SP, A64_SP, ctx->throw_spill), ctx);
+
pop_callee_regs(ctx);
emit(A64_POP(A64_ZR, ptr, A64_SP), ctx);
@@ -1367,6 +1406,8 @@ static int build_insn(const struct bpf_verifier_env *env, const struct bpf_insn
const s16 off = insn->off;
const s32 imm = insn->imm;
const int i = insn - ctx->prog->insnsi;
+ const bool in_pad = bpf_exc_insn_in_pad(env, ctx->prog, i);
+ const bool pad_head = bpf_exc_insn_is_pad(env, ctx->prog, i);
const bool is64 = BPF_CLASS(code) == BPF_ALU64 ||
BPF_CLASS(code) == BPF_JMP;
u8 jmp_cond;
@@ -1378,9 +1419,13 @@ static int build_insn(const struct bpf_verifier_env *env, const struct bpf_insn
int ret;
bool sign_extend;
- if (bpf_insn_is_indirect_target(env, ctx->prog, i))
+ if (bpf_insn_is_indirect_target(env, ctx->prog, i) || pad_head)
emit_bti(A64_BTI_J, ctx);
+ if (pad_head)
+ emit(A64_SUB_I(1, A64_CLEANUP_FP, fp,
+ ctx->stack_size + ctx->stack_arg_size), ctx);
+
switch (code) {
/* dst = src */
case BPF_ALU | BPF_MOV | BPF_X:
@@ -1743,6 +1788,26 @@ static int build_insn(const struct bpf_verifier_env *env, const struct bpf_insn
u64 func_addr;
u32 cpu_offset;
+ if (bpf_exc_insn_is_throw(env, ctx->prog, i)) {
+ /* Spill where the bpf_throw() walker looks. */
+ const s32 off = ctx->prog->aux->exc->throw_spill_off;
+
+ emit(A64_SUB_I(1, tmp, A64_FP, -off), ctx);
+ emit(A64_STR64I(bpf2a64[PRIVATE_SP], tmp, 0), ctx);
+ emit(A64_STR64I(bpf2a64[ARENA_VM_START], tmp, 8), ctx);
+ emit(A64_STR64I(bpf2a64[BPF_REG_FP], tmp, 16), ctx);
+ emit(A64_STR64I(bpf2a64[TCCNT_PTR], tmp, 24), ctx);
+ emit(A64_STR64I(bpf2a64[BPF_REG_8], tmp, 48), ctx);
+ emit(A64_STR64I(bpf2a64[BPF_REG_9], tmp, 56), ctx);
+ emit(A64_STR64I(bpf2a64[BPF_REG_6], tmp, 64), ctx);
+ emit(A64_STR64I(bpf2a64[BPF_REG_7], tmp, 72), ctx);
+ }
+
+ if (bpf_exc_insn_is_resume(env, ctx->prog, i)) {
+ emit(A64_BR(A64_R(23)), ctx);
+ break;
+ }
+
/* Implement helper call to bpf_get_smp_processor_id() inline */
if (insn->src_reg == 0 && insn->imm == BPF_FUNC_get_smp_processor_id) {
cpu_offset = offsetof(struct thread_info, cpu);
@@ -1854,7 +1919,8 @@ static int build_insn(const struct bpf_verifier_env *env, const struct bpf_insn
src = tmp2;
}
if (src == fp) {
- src_adj = ctx->priv_sp_used ? priv_sp : A64_SP;
+ src_adj = ctx->priv_sp_used ? priv_sp :
+ in_pad ? A64_CLEANUP_FP : A64_SP;
off_adj = off + ctx->stack_size;
if (!ctx->priv_sp_used)
off_adj += ctx->stack_arg_size;
@@ -1952,7 +2018,8 @@ static int build_insn(const struct bpf_verifier_env *env, const struct bpf_insn
dst = tmp3;
}
if (dst == fp) {
- dst_adj = ctx->priv_sp_used ? priv_sp : A64_SP;
+ dst_adj = ctx->priv_sp_used ? priv_sp :
+ in_pad ? A64_CLEANUP_FP : A64_SP;
off_adj = off + ctx->stack_size;
if (!ctx->priv_sp_used)
off_adj += ctx->stack_arg_size;
@@ -2021,7 +2088,8 @@ static int build_insn(const struct bpf_verifier_env *env, const struct bpf_insn
dst = tmp2;
}
if (dst == fp) {
- dst_adj = ctx->priv_sp_used ? priv_sp : A64_SP;
+ dst_adj = ctx->priv_sp_used ? priv_sp :
+ in_pad ? A64_CLEANUP_FP : A64_SP;
off_adj = off + ctx->stack_size;
if (!ctx->priv_sp_used)
off_adj += ctx->stack_arg_size;
@@ -2410,6 +2478,13 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_verifier_env *env, struct bpf_pr
* reasons, expects to point to the next instruction)
*/
bpf_prog_update_insn_ptrs(prog, ctx.offset, ctx.ro_image);
+
+ /*
+ * Same byte offsets, consumed by the bpf_throw() frame walker:
+ * turn the cleanup records into native address ranges now that
+ * the image is final.
+ */
+ bpf_exc_fill_native_ranges(prog, ctx.offset, ctx.ro_image);
out_off:
if (!ro_header && priv_stack_ptr) {
free_percpu(priv_stack_ptr);
@@ -3385,6 +3460,11 @@ bool bpf_jit_supports_exceptions(void)
return true;
}
+bool bpf_jit_supports_cleanup_pads(void)
+{
+ return true;
+}
+
bool bpf_jit_supports_arena(void)
{
return true;
--
2.53.0-Meta
^ permalink raw reply related [flat|nested] 49+ messages in thread* [PATCH bpf-next v5 14/21] libbpf: Resolve the compiler's _Unwind_Resume to the kernel's kfunc
2026-09-23 4:58 [PATCH bpf-next v5 00/21] bpf: Run exception cleanup landing pads when bpf_throw() unwinds Yonghong Song
` (12 preceding siblings ...)
2026-09-23 4:59 ` [PATCH bpf-next v5 13/21] bpf, arm64: " Yonghong Song
@ 2026-09-23 4:59 ` Yonghong Song
2026-09-23 8:36 ` Kumar Kartikeya Dwivedi
2026-09-23 5:00 ` [PATCH bpf-next v5 15/21] libbpf: Add cleanup_info to bpf_prog_load_opts Yonghong Song
` (6 subsequent siblings)
20 siblings, 1 reply; 49+ messages in thread
From: Yonghong Song @ 2026-09-23 4:59 UTC (permalink / raw)
To: bpf
Cc: Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann,
Eduard Zingerman, kernel-team
LLVM terminates a cleanup landing pad with a call to _Unwind_Resume: the
base unwind ABI's entry point for carrying an unwind on once a frame's
cleanups have run. The kernel provides that terminator as a kfunc, but
not under that name: claiming _Unwind_Resume in the kernel's own symbol
table, for a function whose body never runs, would be needlessly confusing,
so it is called bpf_unwind_resume.
Both load paths take the detour. A direct load resolves the name against
the kernel's BTF while libbpf runs. A light skeleton instead writes the
name into the loader program's blob of bytes, for that program to resolve
when it runs, so the name recorded there has to be translated as well.
Signed-off-by: Yonghong Song <yonghong.song@linux.dev>
---
tools/lib/bpf/libbpf.c | 23 ++++++++++++++++++-----
1 file changed, 18 insertions(+), 5 deletions(-)
diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index cd1ea1bb53cb..2d0081784445 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -8379,6 +8379,16 @@ static void fixup_verifier_log(struct bpf_program *prog, char *buf, size_t buf_s
}
}
+/*
+ * LLVM terminates a cleanup landing pad with a call to _Unwind_Resume, the
+ * base unwind ABI's entry point for carrying an unwind on once a frame's
+ * cleanups have run. The kernel knows it as bpf_unwind_resume.
+ */
+static const char *kern_extern_name(const char *name)
+{
+ return strcmp(name, "_Unwind_Resume") ? name : "bpf_unwind_resume";
+}
+
static int bpf_program_record_relos(struct bpf_program *prog)
{
struct bpf_object *obj = prog->obj;
@@ -8395,12 +8405,12 @@ static int bpf_program_record_relos(struct bpf_program *prog)
continue;
kind = btf_is_var(btf__type_by_id(obj->btf, ext->btf_id)) ?
BTF_KIND_VAR : BTF_KIND_FUNC;
- bpf_gen__record_extern(obj->gen_loader, ext->name,
+ bpf_gen__record_extern(obj->gen_loader, kern_extern_name(ext->name),
ext->is_weak, !ext->ksym.type_id,
true, kind, relo->insn_idx);
break;
case RELO_EXTERN_CALL:
- bpf_gen__record_extern(obj->gen_loader, ext->name,
+ bpf_gen__record_extern(obj->gen_loader, kern_extern_name(ext->name),
ext->is_weak, false, false, BTF_KIND_FUNC,
relo->insn_idx);
break;
@@ -8876,17 +8886,20 @@ static int bpf_object__resolve_ksym_func_btf_id(struct bpf_object *obj,
struct module_btf *mod_btf = NULL;
const struct btf_type *kern_func;
struct btf *kern_btf = NULL;
+ const char *local_name, *kern_name;
int ret;
local_func_proto_id = ext->ksym.type_id;
- kfunc_id = find_ksym_btf_id(obj, ext->essent_name ?: ext->name, BTF_KIND_FUNC, &kern_btf,
- &mod_btf);
+ local_name = ext->essent_name ?: ext->name;
+ kern_name = kern_extern_name(local_name);
+
+ kfunc_id = find_ksym_btf_id(obj, kern_name, BTF_KIND_FUNC, &kern_btf, &mod_btf);
if (kfunc_id < 0) {
if (kfunc_id == -ESRCH && ext->is_weak)
return 0;
pr_warn("extern (func ksym) '%s': not found in kernel or module BTFs\n",
- ext->name);
+ kern_name != local_name ? kern_name : ext->name);
return kfunc_id;
}
--
2.53.0-Meta
^ permalink raw reply related [flat|nested] 49+ messages in thread* Re: [PATCH bpf-next v5 14/21] libbpf: Resolve the compiler's _Unwind_Resume to the kernel's kfunc
2026-09-23 4:59 ` [PATCH bpf-next v5 14/21] libbpf: Resolve the compiler's _Unwind_Resume to the kernel's kfunc Yonghong Song
@ 2026-09-23 8:36 ` Kumar Kartikeya Dwivedi
0 siblings, 0 replies; 49+ messages in thread
From: Kumar Kartikeya Dwivedi @ 2026-09-23 8:36 UTC (permalink / raw)
To: Yonghong Song, bpf
Cc: Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann,
Eduard Zingerman, kernel-team
On Wed Sep 23, 2026 at 6:59 AM CEST, Yonghong Song wrote:
> LLVM terminates a cleanup landing pad with a call to _Unwind_Resume: the
> base unwind ABI's entry point for carrying an unwind on once a frame's
> cleanups have run. The kernel provides that terminator as a kfunc, but
> not under that name: claiming _Unwind_Resume in the kernel's own symbol
> table, for a function whose body never runs, would be needlessly confusing,
> so it is called bpf_unwind_resume.
>
> Both load paths take the detour. A direct load resolves the name against
> the kernel's BTF while libbpf runs. A light skeleton instead writes the
> name into the loader program's blob of bytes, for that program to resolve
> when it runs, so the name recorded there has to be translated as well.
>
> Signed-off-by: Yonghong Song <yonghong.song@linux.dev>
> ---
> tools/lib/bpf/libbpf.c | 23 ++++++++++++++++++-----
> 1 file changed, 18 insertions(+), 5 deletions(-)
>
> diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
> index cd1ea1bb53cb..2d0081784445 100644
> --- a/tools/lib/bpf/libbpf.c
> +++ b/tools/lib/bpf/libbpf.c
> @@ -8379,6 +8379,16 @@ static void fixup_verifier_log(struct bpf_program *prog, char *buf, size_t buf_s
> }
> }
>
> +/*
> + * LLVM terminates a cleanup landing pad with a call to _Unwind_Resume, the
> + * base unwind ABI's entry point for carrying an unwind on once a frame's
> + * cleanups have run. The kernel knows it as bpf_unwind_resume.
> + */
> +static const char *kern_extern_name(const char *name)
> +{
> + return strcmp(name, "_Unwind_Resume") ? name : "bpf_unwind_resume";
> +}
> +
> static int bpf_program_record_relos(struct bpf_program *prog)
> {
> struct bpf_object *obj = prog->obj;
> @@ -8395,12 +8405,12 @@ static int bpf_program_record_relos(struct bpf_program *prog)
> continue;
> kind = btf_is_var(btf__type_by_id(obj->btf, ext->btf_id)) ?
> BTF_KIND_VAR : BTF_KIND_FUNC;
> - bpf_gen__record_extern(obj->gen_loader, ext->name,
> + bpf_gen__record_extern(obj->gen_loader, kern_extern_name(ext->name),
> ext->is_weak, !ext->ksym.type_id,
> true, kind, relo->insn_idx);
> break;
> case RELO_EXTERN_CALL:
> - bpf_gen__record_extern(obj->gen_loader, ext->name,
> + bpf_gen__record_extern(obj->gen_loader, kern_extern_name(ext->name),
> ext->is_weak, false, false, BTF_KIND_FUNC,
> relo->insn_idx);
> break;
> @@ -8876,17 +8886,20 @@ static int bpf_object__resolve_ksym_func_btf_id(struct bpf_object *obj,
> struct module_btf *mod_btf = NULL;
> const struct btf_type *kern_func;
> struct btf *kern_btf = NULL;
> + const char *local_name, *kern_name;
> int ret;
>
> local_func_proto_id = ext->ksym.type_id;
>
> - kfunc_id = find_ksym_btf_id(obj, ext->essent_name ?: ext->name, BTF_KIND_FUNC, &kern_btf,
> - &mod_btf);
> + local_name = ext->essent_name ?: ext->name;
> + kern_name = kern_extern_name(local_name);
> +
> + kfunc_id = find_ksym_btf_id(obj, kern_name, BTF_KIND_FUNC, &kern_btf, &mod_btf);
Compiler doesn't directly emit it in .ksyms section, right? How does this work
in practice? It's a bit confusing and not clear from the commit log.
> [...]
^ permalink raw reply [flat|nested] 49+ messages in thread
* [PATCH bpf-next v5 15/21] libbpf: Add cleanup_info to bpf_prog_load_opts
2026-09-23 4:58 [PATCH bpf-next v5 00/21] bpf: Run exception cleanup landing pads when bpf_throw() unwinds Yonghong Song
` (13 preceding siblings ...)
2026-09-23 4:59 ` [PATCH bpf-next v5 14/21] libbpf: Resolve the compiler's _Unwind_Resume to the kernel's kfunc Yonghong Song
@ 2026-09-23 5:00 ` Yonghong Song
2026-09-23 5:00 ` [PATCH bpf-next v5 16/21] libbpf: Collect .bpf_cleanup records and pass them to the kernel Yonghong Song
` (5 subsequent siblings)
20 siblings, 0 replies; 49+ messages in thread
From: Yonghong Song @ 2026-09-23 5:00 UTC (permalink / raw)
To: bpf
Cc: Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann,
Eduard Zingerman, kernel-team
Let a caller hand the kernel an exception cleanup table: struct
bpf_prog_load_opts grows cleanup_info, cleanup_info_cnt and
cleanup_info_rec_size, and bpf_prog_load() passes all three on to
BPF_PROG_LOAD.
The attr size it computes grows with them, by one field rather than
three: cleanup_info_cnt is the last of the three in the BPF_PROG_LOAD
attr, so offsetofend() on it already covers cleanup_info and
cleanup_info_rec_size.
Signed-off-by: Yonghong Song <yonghong.song@linux.dev>
---
tools/lib/bpf/bpf.c | 6 +++++-
tools/lib/bpf/bpf.h | 7 ++++++-
2 files changed, 11 insertions(+), 2 deletions(-)
diff --git a/tools/lib/bpf/bpf.c b/tools/lib/bpf/bpf.c
index a9de7f107cf7..03315b3a3a19 100644
--- a/tools/lib/bpf/bpf.c
+++ b/tools/lib/bpf/bpf.c
@@ -295,7 +295,7 @@ int bpf_prog_load(enum bpf_prog_type prog_type,
const struct bpf_insn *insns, size_t insn_cnt,
struct bpf_prog_load_opts *opts)
{
- const size_t attr_sz = offsetofend(union bpf_attr, keyring_id);
+ const size_t attr_sz = offsetofend(union bpf_attr, cleanup_info_cnt);
void *finfo = NULL, *linfo = NULL;
const char *func_info, *line_info;
__u32 log_size, log_level, attach_prog_fd, attach_btf_obj_fd;
@@ -370,6 +370,10 @@ int bpf_prog_load(enum bpf_prog_type prog_type,
attr.fd_array = ptr_to_u64(OPTS_GET(opts, fd_array, NULL));
attr.fd_array_cnt = OPTS_GET(opts, fd_array_cnt, 0);
+ attr.cleanup_info_rec_size = OPTS_GET(opts, cleanup_info_rec_size, 0);
+ attr.cleanup_info = ptr_to_u64(OPTS_GET(opts, cleanup_info, NULL));
+ attr.cleanup_info_cnt = OPTS_GET(opts, cleanup_info_cnt, 0);
+
if (log_level) {
attr.log_buf = ptr_to_u64(log_buf);
attr.log_size = log_size;
diff --git a/tools/lib/bpf/bpf.h b/tools/lib/bpf/bpf.h
index 490e8cb4ba53..ec542fd68f63 100644
--- a/tools/lib/bpf/bpf.h
+++ b/tools/lib/bpf/bpf.h
@@ -128,9 +128,14 @@ struct bpf_prog_load_opts {
/* if set, provides the length of fd_array */
__u32 fd_array_cnt;
+
+ /* exception cleanup table, from the .bpf_cleanup section */
+ const void *cleanup_info;
+ __u32 cleanup_info_cnt;
+ __u32 cleanup_info_rec_size;
size_t :0;
};
-#define bpf_prog_load_opts__last_field fd_array_cnt
+#define bpf_prog_load_opts__last_field cleanup_info_rec_size
LIBBPF_API int bpf_prog_load(enum bpf_prog_type prog_type,
const char *prog_name, const char *license,
--
2.53.0-Meta
^ permalink raw reply related [flat|nested] 49+ messages in thread* [PATCH bpf-next v5 16/21] libbpf: Collect .bpf_cleanup records and pass them to the kernel
2026-09-23 4:58 [PATCH bpf-next v5 00/21] bpf: Run exception cleanup landing pads when bpf_throw() unwinds Yonghong Song
` (14 preceding siblings ...)
2026-09-23 5:00 ` [PATCH bpf-next v5 15/21] libbpf: Add cleanup_info to bpf_prog_load_opts Yonghong Song
@ 2026-09-23 5:00 ` Yonghong Song
2026-09-23 5:52 ` Kumar Kartikeya Dwivedi
2026-09-23 5:00 ` [PATCH bpf-next v5 17/21] libbpf: Carry the exception cleanup table through the light skeleton Yonghong Song
` (4 subsequent siblings)
20 siblings, 1 reply; 49+ messages in thread
From: Yonghong Song @ 2026-09-23 5:00 UTC (permalink / raw)
To: bpf
Cc: Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann,
Eduard Zingerman, kernel-team
Parse the compiler-emitted .bpf_cleanup section and hand the resulting
table to BPF_PROG_LOAD.
Each record is three 4-byte fields, and each field is a byte offset into
some code section named by a matching .rel.bpf_cleanup relocation.
bpf_object__init_cleanup_info() resolves both halves once at open time and
keeps (section, instruction index) pairs; it rejects a record whose field
has no relocation, whose relocation is not one of the two 32-bit types that
spell a data reference to a code section -- R_BPF_64_NODYLD32 from LLVM,
R_BPF_64_ABS32 from GNU as -- or whose offset is not instruction aligned.
The records are sorted by begin_off once the offsets are final: the kernel
wants the table sorted with disjoint ranges so that it can find the record
covering a call site with a binary search, and records arrive in
.bpf_cleanup order, which says nothing about where the subprograms they
describe were appended. Overlapping ranges are reported here, where the
program name and both regions are still at hand.
bpf_object_load_prog() then passes the per-program table through the
bpf_prog_load() options added in the previous patch, with the record size
carried on the program the way func_info and line_info carry theirs rather
than taken from a sizeof() at the call site. bpf_program__clone() carries
it too. That is a second load path -- the one veristat uses -- and without
the table the kernel sees landing pads nothing reaches and refuses the
program with "unreachable insn".
Signed-off-by: Yonghong Song <yonghong.song@linux.dev>
---
tools/lib/bpf/libbpf.c | 309 ++++++++++++++++++++++++++++++++
tools/lib/bpf/libbpf_internal.h | 3 +
2 files changed, 312 insertions(+)
diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index 2d0081784445..d0e601b3499c 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -514,6 +514,11 @@ struct bpf_program {
void *line_info;
__u32 line_info_rec_size;
__u32 line_info_cnt;
+
+ struct bpf_cleanup_info *cleanup_info;
+ __u32 cleanup_info_rec_size;
+ __u32 cleanup_info_cnt;
+
__u32 prog_flags;
__u8 hash[SHA256_DIGEST_LENGTH];
@@ -549,6 +554,7 @@ struct bpf_struct_ops {
#define STRUCT_OPS_SEC ".struct_ops"
#define STRUCT_OPS_LINK_SEC ".struct_ops.link"
#define ARENA_SEC ".addr_space.1"
+#define CLEANUP_SEC ".bpf_cleanup"
enum libbpf_map_type {
LIBBPF_MAP_UNSPEC,
@@ -677,6 +683,26 @@ struct elf_sec_desc {
Elf_Data *data;
};
+#define CLEANUP_REC_FIELDS (sizeof(struct bpf_cleanup_info) / sizeof(__u32))
+
+/* Index of each field of struct bpf_cleanup_info, read as an array of __u32. */
+enum {
+ CLEANUP_REC_BEGIN,
+ CLEANUP_REC_END,
+ CLEANUP_REC_PAD,
+};
+
+/*
+ * One (begin, end, landing_pad) triple from .bpf_cleanup, with each field
+ * resolved from its relocation to an ELF section plus a section-relative
+ * instruction index. The mapping to final program instruction indices can only
+ * happen after subprogram placement, which differs per main program.
+ */
+struct cleanup_raw_rec {
+ int sec_idx[CLEANUP_REC_FIELDS];
+ size_t insn_idx[CLEANUP_REC_FIELDS];
+};
+
struct elf_state {
int fd;
const void *obj_buf;
@@ -696,6 +722,8 @@ struct elf_state {
bool has_st_ops;
int arena_data_shndx;
int jumptables_data_shndx;
+ Elf_Data *cleanup_data;
+ int cleanup_shndx;
};
struct usdt_manager;
@@ -773,6 +801,9 @@ struct bpf_object {
void *jumptables_data;
size_t jumptables_data_sz;
+ struct cleanup_raw_rec *cleanup_recs;
+ size_t cleanup_rec_cnt;
+
struct {
struct bpf_program *prog;
unsigned int sym_off;
@@ -819,7 +850,10 @@ static void bpf_program__exit(struct bpf_program *prog)
zfree(&prog->sec_name);
zfree(&prog->insns);
zfree(&prog->reloc_desc);
+ zfree(&prog->cleanup_info);
+ prog->cleanup_info_rec_size = 0;
+ prog->cleanup_info_cnt = 0;
prog->nr_reloc = 0;
prog->insns_cnt = 0;
prog->sec_idx = -1;
@@ -1561,6 +1595,7 @@ static struct bpf_object *bpf_object__new(const char *path,
obj->efile.obj_buf = obj_buf;
obj->efile.obj_buf_sz = obj_buf_sz;
obj->efile.btf_maps_shndx = -1;
+ obj->efile.cleanup_shndx = -1;
obj->kconfig_map_idx = -1;
obj->arena_map_idx = -1;
@@ -4047,6 +4082,9 @@ static int bpf_object__elf_collect(struct bpf_object *obj)
sec_desc->shdr = sh;
sec_desc->data = data;
obj->efile.has_st_ops = true;
+ } else if (strcmp(name, CLEANUP_SEC) == 0) {
+ obj->efile.cleanup_data = data;
+ obj->efile.cleanup_shndx = idx;
} else if (strcmp(name, ARENA_SEC) == 0) {
obj->efile.arena_data = data;
obj->efile.arena_data_shndx = idx;
@@ -4074,6 +4112,7 @@ static int bpf_object__elf_collect(struct bpf_object *obj)
strcmp(name, ".rel" STRUCT_OPS_LINK_SEC) &&
strcmp(name, ".rel?" STRUCT_OPS_SEC) &&
strcmp(name, ".rel?" STRUCT_OPS_LINK_SEC) &&
+ strcmp(name, ".rel" CLEANUP_SEC) &&
strcmp(name, ".rel" MAPS_ELF_SEC)) {
pr_info("elf: skipping relo section(%d) %s for section(%d) %s\n",
idx, name, targ_sec_idx,
@@ -4854,6 +4893,238 @@ static struct bpf_program *find_prog_by_sec_insn(const struct bpf_object *obj,
return NULL;
}
+static int bpf_object__init_cleanup_info(struct bpf_object *obj)
+{
+ Elf_Data *data = obj->efile.cleanup_data;
+ Elf_Data *relo = NULL;
+ size_t i, nrels, nslots, nrecs;
+ struct cleanup_raw_rec *recs;
+ int *slot_sec, ret = 0;
+ size_t *slot_val;
+ const __u32 *vals;
+ bool native;
+
+ if (!data || obj->efile.cleanup_shndx < 0)
+ return 0;
+
+ native = is_native_endianness(obj);
+
+ for (i = 0; i < obj->efile.sec_cnt; i++) {
+ struct elf_sec_desc *sd = &obj->efile.secs[i];
+
+ if (sd->sec_type == SEC_RELO && sd->shdr &&
+ sd->shdr->sh_info == (Elf64_Word)obj->efile.cleanup_shndx) {
+ relo = sd->data;
+ break;
+ }
+ }
+ if (!relo) {
+ pr_warn("%s present without relocations\n", CLEANUP_SEC);
+ return -LIBBPF_ERRNO__FORMAT;
+ }
+ if (data->d_size % sizeof(struct bpf_cleanup_info)) {
+ pr_warn("%s size %zu is not a multiple of the record size %zu\n",
+ CLEANUP_SEC, data->d_size, sizeof(struct bpf_cleanup_info));
+ return -LIBBPF_ERRNO__FORMAT;
+ }
+
+ vals = data->d_buf;
+ nslots = data->d_size / sizeof(__u32);
+ nrecs = data->d_size / sizeof(struct bpf_cleanup_info);
+
+ slot_sec = calloc(nslots, sizeof(*slot_sec));
+ slot_val = calloc(nslots, sizeof(*slot_val));
+ recs = calloc(nrecs ?: 1, sizeof(*recs));
+ if (!slot_sec || !slot_val || !recs) {
+ ret = -ENOMEM;
+ goto out;
+ }
+ for (i = 0; i < nslots; i++)
+ slot_sec[i] = -1;
+
+ /* One relocation per 4-byte field, naming the section it points into. */
+ nrels = relo->d_size / sizeof(Elf64_Rel);
+ for (i = 0; i < nrels; i++) {
+ Elf64_Rel *rel = elf_rel_by_idx(relo, i);
+ Elf64_Sym *sym = elf_sym_by_idx(obj, ELF64_R_SYM(rel->r_info));
+ size_t type = ELF64_R_TYPE(rel->r_info);
+ size_t slot = rel->r_offset / sizeof(__u32);
+
+ if (type != R_BPF_64_NODYLD32 && type != R_BPF_64_ABS32) {
+ pr_warn("%s: relocation %zu has unexpected type %zu\n",
+ CLEANUP_SEC, i, type);
+ ret = -LIBBPF_ERRNO__FORMAT;
+ goto out;
+ }
+ if (!sym || slot >= nslots || rel->r_offset % sizeof(__u32)) {
+ pr_warn("%s: bad relocation %zu\n", CLEANUP_SEC, i);
+ ret = -LIBBPF_ERRNO__FORMAT;
+ goto out;
+ }
+ slot_sec[slot] = sym->st_shndx;
+ /*
+ * The addend lives in the section data, which libelf leaves in
+ * the object's byte order; a non-section symbol additionally
+ * contributes its own value.
+ */
+ slot_val[slot] = (native ? vals[slot] : bswap_32(vals[slot])) +
+ sym->st_value;
+ }
+
+ for (i = 0; i < nslots; i++) {
+ struct cleanup_raw_rec *rec = &recs[i / CLEANUP_REC_FIELDS];
+ size_t field = i % CLEANUP_REC_FIELDS;
+
+ if (slot_sec[i] < 0) {
+ pr_warn("%s: field %zu has no relocation\n", CLEANUP_SEC, i);
+ ret = -LIBBPF_ERRNO__FORMAT;
+ goto out;
+ }
+ if (slot_val[i] % BPF_INSN_SZ) {
+ pr_warn("%s: field %zu offset %zu is not instruction aligned\n",
+ CLEANUP_SEC, i, slot_val[i]);
+ ret = -LIBBPF_ERRNO__FORMAT;
+ goto out;
+ }
+ rec->sec_idx[field] = slot_sec[i];
+ rec->insn_idx[field] = slot_val[i] / BPF_INSN_SZ;
+ }
+
+ obj->cleanup_recs = recs;
+ obj->cleanup_rec_cnt = nrecs;
+ recs = NULL;
+out:
+ free(recs);
+ free(slot_val);
+ free(slot_sec);
+ return ret;
+}
+
+static int cmp_cleanup_info(const void *a, const void *b)
+{
+ const struct bpf_cleanup_info *x = a, *y = b;
+
+ if (x->begin_off == y->begin_off)
+ return 0;
+ return x->begin_off < y->begin_off ? -1 : 1;
+}
+
+static int bpf_prog_collect_cleanup_info(struct bpf_object *obj,
+ struct bpf_program *prog)
+{
+ size_t i;
+ int j;
+
+ for (i = 0; i < obj->cleanup_rec_cnt; i++) {
+ struct cleanup_raw_rec *raw = &obj->cleanup_recs[i];
+ struct bpf_program *owner = NULL;
+ struct bpf_cleanup_info ci = {};
+ __u32 fields[CLEANUP_REC_FIELDS];
+ void *tmp;
+
+ if (raw->sec_idx[CLEANUP_REC_BEGIN] == raw->sec_idx[CLEANUP_REC_END] &&
+ raw->insn_idx[CLEANUP_REC_BEGIN] >= raw->insn_idx[CLEANUP_REC_END]) {
+ pr_warn("%s: record %zu is an empty range [%zu,%zu)\n",
+ CLEANUP_SEC, i, raw->insn_idx[CLEANUP_REC_BEGIN],
+ raw->insn_idx[CLEANUP_REC_END]);
+ return -LIBBPF_ERRNO__FORMAT;
+ }
+
+ for (j = 0; j < CLEANUP_REC_FIELDS; j++) {
+ size_t idx = raw->insn_idx[j], final;
+ struct bpf_program *p;
+
+ /*
+ * The end of a range is exclusive, so it may name the
+ * instruction just past the last one of a function,
+ * which belongs to the next function or to nothing at
+ * all. Ask about the last instruction the range covers,
+ * the way the kernel does.
+ */
+ if (j == CLEANUP_REC_END) {
+ if (!idx) {
+ pr_warn("%s: record %zu ends at instruction 0\n",
+ CLEANUP_SEC, i);
+ return -LIBBPF_ERRNO__FORMAT;
+ }
+ idx--;
+ }
+
+ p = find_prog_by_sec_insn(obj, raw->sec_idx[j], idx);
+ if (!p) {
+ pr_warn("%s: record %zu field %d is not inside a function\n",
+ CLEANUP_SEC, i, j);
+ return -LIBBPF_ERRNO__FORMAT;
+ }
+ if (!owner) {
+ owner = p;
+ } else if (owner != p) {
+ pr_warn("%s: record %zu spans functions '%s' and '%s'\n",
+ CLEANUP_SEC, i, owner->name, p->name);
+ return -LIBBPF_ERRNO__FORMAT;
+ }
+
+ if (owner == prog) {
+ final = raw->insn_idx[j] - prog->sec_insn_off;
+ } else if (prog_is_subprog(obj, owner) && owner->sub_insn_off) {
+ /*
+ * sub_insn_off is where this subprogram was
+ * appended to the main program being relocated;
+ * zero means it is not part of it.
+ */
+ final = owner->sub_insn_off +
+ raw->insn_idx[j] - owner->sec_insn_off;
+ } else {
+ owner = NULL;
+ break;
+ }
+ fields[j] = final;
+ }
+ if (!owner)
+ continue;
+
+ ci.begin_off = fields[CLEANUP_REC_BEGIN];
+ ci.end_off = fields[CLEANUP_REC_END];
+ ci.landing_pad_off = fields[CLEANUP_REC_PAD];
+
+ tmp = libbpf_reallocarray(prog->cleanup_info, prog->cleanup_info_cnt + 1,
+ sizeof(*prog->cleanup_info));
+ if (!tmp)
+ return -ENOMEM;
+ prog->cleanup_info = tmp;
+ prog->cleanup_info_rec_size = sizeof(struct bpf_cleanup_info);
+ prog->cleanup_info[prog->cleanup_info_cnt++] = ci;
+
+ pr_debug("prog '%s': cleanup region [%u,%u) -> landing pad %u\n",
+ prog->name, ci.begin_off, ci.end_off, ci.landing_pad_off);
+ }
+
+ if (!prog->cleanup_info_cnt)
+ return 0;
+
+ if (prog->cleanup_info_cnt > INT32_MAX / sizeof(struct bpf_cleanup_info)) {
+ pr_warn("prog '%s': too many cleanup records: %u\n",
+ prog->name, prog->cleanup_info_cnt);
+ return -LIBBPF_ERRNO__FORMAT;
+ }
+
+ qsort(prog->cleanup_info, prog->cleanup_info_cnt,
+ sizeof(*prog->cleanup_info), cmp_cleanup_info);
+ for (i = 1; i < prog->cleanup_info_cnt; i++) {
+ struct bpf_cleanup_info *prev = &prog->cleanup_info[i - 1];
+ struct bpf_cleanup_info *cur = &prog->cleanup_info[i];
+
+ if (cur->begin_off < prev->end_off) {
+ pr_warn("prog '%s': overlapping cleanup regions [%u,%u) and [%u,%u)\n",
+ prog->name, prev->begin_off, prev->end_off,
+ cur->begin_off, cur->end_off);
+ return -LIBBPF_ERRNO__FORMAT;
+ }
+ }
+
+ return 0;
+}
+
static int
bpf_object__collect_prog_relos(struct bpf_object *obj, Elf64_Shdr *shdr, Elf_Data *data)
{
@@ -7589,6 +7860,13 @@ static int bpf_object__relocate(struct bpf_object *obj, const char *targ_btf_pat
return err;
}
}
+
+ err = bpf_prog_collect_cleanup_info(obj, prog);
+ if (err) {
+ pr_warn("prog '%s': failed to collect cleanup info: %s\n",
+ prog->name, errstr(err));
+ return err;
+ }
}
for (i = 0; i < obj->nr_programs; i++) {
prog = &obj->programs[i];
@@ -7779,6 +8057,9 @@ static int bpf_object__collect_relos(struct bpf_object *obj)
return -LIBBPF_ERRNO__INTERNAL;
}
+ if (idx == obj->efile.cleanup_shndx)
+ continue;
+
if (obj->efile.secs[idx].sec_type == SEC_ST_OPS)
err = bpf_object__collect_st_ops_relos(obj, shdr, data);
else if (idx == obj->efile.btf_maps_shndx)
@@ -8051,6 +8332,11 @@ static int bpf_object_load_prog(struct bpf_object *obj, struct bpf_program *prog
load_attr.line_info_rec_size = prog->line_info_rec_size;
load_attr.line_info_cnt = prog->line_info_cnt;
}
+ if (prog->cleanup_info_cnt) {
+ load_attr.cleanup_info = prog->cleanup_info;
+ load_attr.cleanup_info_cnt = prog->cleanup_info_cnt;
+ load_attr.cleanup_info_rec_size = prog->cleanup_info_rec_size;
+ }
load_attr.log_level = log_level;
load_attr.prog_flags = prog->prog_flags;
load_attr.fd_array = obj->fd_array;
@@ -8644,6 +8930,7 @@ static struct bpf_object *bpf_object_open(const char *path, const void *obj_buf,
err = err ? : bpf_object__init_maps(obj, opts);
err = err ? : bpf_object_init_progs(obj, opts);
err = err ? : bpf_object__collect_relos(obj);
+ err = err ? : bpf_object__init_cleanup_info(obj);
if (err)
goto out;
@@ -9758,6 +10045,9 @@ void bpf_object__close(struct bpf_object *obj)
zfree(&obj->jumptables_data);
obj->jumptables_data_sz = 0;
+ zfree(&obj->cleanup_recs);
+ obj->cleanup_rec_cnt = 0;
+
for (i = 0; i < obj->jumptable_map_cnt; i++)
close(obj->jumptable_maps[i].fd);
zfree(&obj->jumptable_maps);
@@ -10156,6 +10446,25 @@ int bpf_program__clone(struct bpf_program *prog, const struct bpf_prog_load_opts
attr.line_info_rec_size = info ? info_rec_size : prog->line_info_rec_size;
}
+ /* exception cleanup table */
+ info = OPTS_GET(opts, cleanup_info, NULL);
+ info_cnt = OPTS_GET(opts, cleanup_info_cnt, 0);
+ info_rec_size = OPTS_GET(opts, cleanup_info_rec_size, 0);
+ if (!!info != !!info_cnt || !!info != !!info_rec_size) {
+ pr_warn("prog '%s': cleanup_info, cleanup_info_cnt, and cleanup_info_rec_size must all be specified or all omitted\n",
+ prog->name);
+ return libbpf_err(-EINVAL);
+ }
+ if (info) {
+ attr.cleanup_info = info;
+ attr.cleanup_info_cnt = info_cnt;
+ attr.cleanup_info_rec_size = info_rec_size;
+ } else if (prog->cleanup_info_cnt) {
+ attr.cleanup_info = prog->cleanup_info;
+ attr.cleanup_info_cnt = prog->cleanup_info_cnt;
+ attr.cleanup_info_rec_size = prog->cleanup_info_rec_size;
+ }
+
/* Logging is caller-controlled; no fallback to prog/obj log settings */
attr.log_buf = OPTS_GET(opts, log_buf, NULL);
attr.log_size = OPTS_GET(opts, log_size, 0);
diff --git a/tools/lib/bpf/libbpf_internal.h b/tools/lib/bpf/libbpf_internal.h
index cb4d96233844..3ba6d9090368 100644
--- a/tools/lib/bpf/libbpf_internal.h
+++ b/tools/lib/bpf/libbpf_internal.h
@@ -56,6 +56,9 @@
#ifndef R_BPF_64_ABS32
#define R_BPF_64_ABS32 3
#endif
+#ifndef R_BPF_64_NODYLD32
+#define R_BPF_64_NODYLD32 4
+#endif
#ifndef R_BPF_64_32
#define R_BPF_64_32 10
#endif
--
2.53.0-Meta
^ permalink raw reply related [flat|nested] 49+ messages in thread* Re: [PATCH bpf-next v5 16/21] libbpf: Collect .bpf_cleanup records and pass them to the kernel
2026-09-23 5:00 ` [PATCH bpf-next v5 16/21] libbpf: Collect .bpf_cleanup records and pass them to the kernel Yonghong Song
@ 2026-09-23 5:52 ` Kumar Kartikeya Dwivedi
0 siblings, 0 replies; 49+ messages in thread
From: Kumar Kartikeya Dwivedi @ 2026-09-23 5:52 UTC (permalink / raw)
To: Yonghong Song, bpf
Cc: Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann,
Eduard Zingerman, kernel-team
On Wed Sep 23, 2026 at 7:00 AM CEST, Yonghong Song wrote:
> Parse the compiler-emitted .bpf_cleanup section and hand the resulting
> table to BPF_PROG_LOAD.
>
> Each record is three 4-byte fields, and each field is a byte offset into
> some code section named by a matching .rel.bpf_cleanup relocation.
> bpf_object__init_cleanup_info() resolves both halves once at open time and
> keeps (section, instruction index) pairs; it rejects a record whose field
> has no relocation, whose relocation is not one of the two 32-bit types that
> spell a data reference to a code section -- R_BPF_64_NODYLD32 from LLVM,
> R_BPF_64_ABS32 from GNU as -- or whose offset is not instruction aligned.
>
> The records are sorted by begin_off once the offsets are final: the kernel
> wants the table sorted with disjoint ranges so that it can find the record
> covering a call site with a binary search, and records arrive in
> .bpf_cleanup order, which says nothing about where the subprograms they
> describe were appended. Overlapping ranges are reported here, where the
> program name and both regions are still at hand.
>
> bpf_object_load_prog() then passes the per-program table through the
> bpf_prog_load() options added in the previous patch, with the record size
> carried on the program the way func_info and line_info carry theirs rather
> than taken from a sizeof() at the call site. bpf_program__clone() carries
> it too. That is a second load path -- the one veristat uses -- and without
> the table the kernel sees landing pads nothing reaches and refuses the
> program with "unreachable insn".
>
> Signed-off-by: Yonghong Song <yonghong.song@linux.dev>
> ---
Should we treat sh_entsize == 0 as 12 (or change LLVM to set it), just so that
wider records, if necessary for any reason in the future, end up being easier to
reject?
> [...]
^ permalink raw reply [flat|nested] 49+ messages in thread
* [PATCH bpf-next v5 17/21] libbpf: Carry the exception cleanup table through the light skeleton
2026-09-23 4:58 [PATCH bpf-next v5 00/21] bpf: Run exception cleanup landing pads when bpf_throw() unwinds Yonghong Song
` (15 preceding siblings ...)
2026-09-23 5:00 ` [PATCH bpf-next v5 16/21] libbpf: Collect .bpf_cleanup records and pass them to the kernel Yonghong Song
@ 2026-09-23 5:00 ` Yonghong Song
2026-09-23 5:00 ` [PATCH bpf-next v5 18/21] libbpf: Let the static linker carry .bpf_cleanup relocations Yonghong Song
` (3 subsequent siblings)
20 siblings, 0 replies; 49+ messages in thread
From: Yonghong Song @ 2026-09-23 5:00 UTC (permalink / raw)
To: bpf
Cc: Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann,
Eduard Zingerman, kernel-team
A light skeleton does not call bpf_prog_load(). bpf_gen__prog_load() builds
its own union bpf_attr field by field, and the loader program it emits is
what issues BPF_PROG_LOAD when the skeleton runs -- so a program loaded
this way reached the kernel without the table the previous patch collected
for it. The load then fails, because the landing pads are code nothing
reaches and the verifier says so. It says "unreachable insn", which names
neither the skeleton nor the table.
Carry it the way func_info and line_info are carried: the records go into
the loader's blob of bytes, the count and record size into the attr, and a
relocation stores the blob's address into attr.cleanup_info once that
address is known. The attr grows to its new last field, cleanup_info_cnt.
Records are 4-byte fields like the other info blobs, so a cross-endian
build has to swap them too.
Signed-off-by: Yonghong Song <yonghong.song@linux.dev>
---
tools/lib/bpf/gen_loader.c | 29 +++++++++++++++++++++++++----
tools/lib/bpf/libbpf_internal.h | 7 +++++++
2 files changed, 32 insertions(+), 4 deletions(-)
diff --git a/tools/lib/bpf/gen_loader.c b/tools/lib/bpf/gen_loader.c
index af3a04f161ac..2345fbdd46f5 100644
--- a/tools/lib/bpf/gen_loader.c
+++ b/tools/lib/bpf/gen_loader.c
@@ -981,13 +981,15 @@ static void cleanup_relos(struct bpf_gen *gen, int insns)
cleanup_core_relo(gen);
}
-/* Convert func, line, and core relo info blobs to target endianness */
+/* Convert func, line, core relo and cleanup info blobs to target endianness */
static void info_blob_bswap(struct bpf_gen *gen, int func_info, int line_info,
- int core_relos, struct bpf_prog_load_opts *load_attr)
+ int core_relos, int cleanup_info,
+ struct bpf_prog_load_opts *load_attr)
{
struct bpf_func_info *fi = gen->data_start + func_info;
struct bpf_line_info *li = gen->data_start + line_info;
struct bpf_core_relo *cr = gen->data_start + core_relos;
+ struct bpf_cleanup_info *ci = gen->data_start + cleanup_info;
int i;
for (i = 0; i < load_attr->func_info_cnt; i++)
@@ -998,6 +1000,9 @@ static void info_blob_bswap(struct bpf_gen *gen, int func_info, int line_info,
for (i = 0; i < gen->core_relo_cnt; i++)
bpf_core_relo_bswap(cr++);
+
+ for (i = 0; i < load_attr->cleanup_info_cnt; i++)
+ bpf_cleanup_info_bswap(ci++);
}
void bpf_gen__prog_load(struct bpf_gen *gen,
@@ -1011,8 +1016,11 @@ void bpf_gen__prog_load(struct bpf_gen *gen,
load_attr->line_info_rec_size;
int core_relo_tot_sz = gen->core_relo_cnt *
sizeof(struct bpf_core_relo);
+ int cleanup_info_tot_sz = load_attr->cleanup_info_cnt *
+ load_attr->cleanup_info_rec_size;
int prog_load_attr, license_off, insns_off, func_info, line_info, core_relos;
- int attr_size = offsetofend(union bpf_attr, core_relo_rec_size);
+ int attr_size = offsetofend(union bpf_attr, cleanup_info_cnt);
+ int cleanup_info;
union bpf_attr attr;
memset(&attr, 0, attr_size);
@@ -1061,9 +1069,17 @@ void bpf_gen__prog_load(struct bpf_gen *gen,
core_relos, gen->core_relo_cnt,
sizeof(struct bpf_core_relo));
+ attr.cleanup_info_rec_size = tgt_endian(load_attr->cleanup_info_rec_size);
+ attr.cleanup_info_cnt = tgt_endian(load_attr->cleanup_info_cnt);
+ cleanup_info = add_data(gen, load_attr->cleanup_info, cleanup_info_tot_sz);
+ pr_debug("gen: prog_load: cleanup_info: off %d cnt %u rec size %u\n",
+ cleanup_info, load_attr->cleanup_info_cnt,
+ load_attr->cleanup_info_rec_size);
+
/* convert all info blobs to target endianness */
if (gen->swapped_endian && !gen->error)
- info_blob_bswap(gen, func_info, line_info, core_relos, load_attr);
+ info_blob_bswap(gen, func_info, line_info, core_relos, cleanup_info,
+ load_attr);
libbpf_strlcpy(attr.prog_name, prog_name, sizeof(attr.prog_name));
prog_load_attr = add_data(gen, &attr, attr_size);
@@ -1085,6 +1101,11 @@ void bpf_gen__prog_load(struct bpf_gen *gen,
/* populate union bpf_attr with a pointer to core_relos */
emit_rel_store(gen, attr_field(prog_load_attr, core_relos), core_relos);
+ /* populate union bpf_attr with a pointer to cleanup_info, if there is one */
+ if (load_attr->cleanup_info_cnt)
+ emit_rel_store(gen, attr_field(prog_load_attr, cleanup_info),
+ cleanup_info);
+
/* populate union bpf_attr fd_array with a pointer to data where map_fds are saved */
emit_rel_store(gen, attr_field(prog_load_attr, fd_array), gen->fd_array);
diff --git a/tools/lib/bpf/libbpf_internal.h b/tools/lib/bpf/libbpf_internal.h
index 3ba6d9090368..78519f24fb40 100644
--- a/tools/lib/bpf/libbpf_internal.h
+++ b/tools/lib/bpf/libbpf_internal.h
@@ -572,6 +572,13 @@ static inline void bpf_core_relo_bswap(struct bpf_core_relo *i)
i->kind = bswap_32(i->kind);
}
+static inline void bpf_cleanup_info_bswap(struct bpf_cleanup_info *i)
+{
+ i->begin_off = bswap_32(i->begin_off);
+ i->end_off = bswap_32(i->end_off);
+ i->landing_pad_off = bswap_32(i->landing_pad_off);
+}
+
enum btf_field_iter_kind {
BTF_FIELD_ITER_IDS,
BTF_FIELD_ITER_STRS,
--
2.53.0-Meta
^ permalink raw reply related [flat|nested] 49+ messages in thread* [PATCH bpf-next v5 18/21] libbpf: Let the static linker carry .bpf_cleanup relocations
2026-09-23 4:58 [PATCH bpf-next v5 00/21] bpf: Run exception cleanup landing pads when bpf_throw() unwinds Yonghong Song
` (16 preceding siblings ...)
2026-09-23 5:00 ` [PATCH bpf-next v5 17/21] libbpf: Carry the exception cleanup table through the light skeleton Yonghong Song
@ 2026-09-23 5:00 ` Yonghong Song
2026-09-23 5:40 ` bot+bpf-ci
2026-09-23 5:00 ` [PATCH bpf-next v5 19/21] selftests/bpf: Add an end-to-end .bpf_cleanup exception test Yonghong Song
` (2 subsequent siblings)
20 siblings, 1 reply; 49+ messages in thread
From: Yonghong Song @ 2026-09-23 5:00 UTC (permalink / raw)
To: bpf
Cc: Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann,
Eduard Zingerman, kernel-team
An object that carries a compiler-emitted exception cleanup table cannot be
linked today. The table's fields are byte offsets into a code section,
materialised by a 32-bit relocation against that section's symbol with the
offset itself as the implicit addend, and the linker rejects both halves of
that: the relocation type is not in the list it accepts, and a relocation
against an STT_SECTION symbol from a non-executable section is an outright
error.
Both spellings of that relocation have to be taken. LLVM emits
R_BPF_64_NODYLD32 for a .long against a section symbol; GNU as emits
R_BPF_64_ABS32, which is what bpf_reloc_type_lookup() maps BFD_RELOC_32 to.
They describe the same value, and the selftests are built with both
compilers.
Taking them is keyed on the relocation type rather than on the section
name, so any non-executable section could reach the new arm, and what it
replaces is an unconditional refusal. Check what the refusal used to make
unnecessary: a relocated section may be SHT_NOBITS, which extend_sec()
leaves with no raw_data, and r_offset is checked for alignment only where
the section holds instructions. Refuse those rather than write through
them.
Signed-off-by: Yonghong Song <yonghong.song@linux.dev>
---
tools/lib/bpf/linker.c | 38 +++++++++++++++++++++++++++++++++++++-
1 file changed, 37 insertions(+), 1 deletion(-)
diff --git a/tools/lib/bpf/linker.c b/tools/lib/bpf/linker.c
index 78f92c39290a..95f2a5243eda 100644
--- a/tools/lib/bpf/linker.c
+++ b/tools/lib/bpf/linker.c
@@ -1036,7 +1036,8 @@ static int linker_sanity_check_elf_relos(struct src_obj *obj, struct src_sec *se
size_t sym_type = ELF64_R_TYPE(relo->r_info);
if (sym_type != R_BPF_64_64 && sym_type != R_BPF_64_32 &&
- sym_type != R_BPF_64_ABS64 && sym_type != R_BPF_64_ABS32) {
+ sym_type != R_BPF_64_ABS64 && sym_type != R_BPF_64_ABS32 &&
+ sym_type != R_BPF_64_NODYLD32) {
pr_warn("ELF relo #%d in section #%zu has unexpected type %zu in %s\n",
i, sec->sec_idx, sym_type, obj->filename);
return -EINVAL;
@@ -2258,6 +2259,7 @@ static int linker_append_elf_relos(struct bpf_linker *linker, struct src_obj *ob
if (ELF64_ST_TYPE(src_sym->st_info) == STT_SECTION) {
struct src_sec *sec = &obj->secs[src_sym->st_shndx];
struct bpf_insn *insn;
+ __u32 *val;
if (src_linked_sec->shdr->sh_flags & SHF_EXECINSTR) {
/* calls to the very first static function inside
@@ -2274,6 +2276,40 @@ static int linker_append_elf_relos(struct bpf_linker *linker, struct src_obj *ob
insn->imm += sec->dst_off / sizeof(struct bpf_insn);
else
insn->imm += sec->dst_off;
+ } else if (sym_type == R_BPF_64_NODYLD32 ||
+ sym_type == R_BPF_64_ABS32) {
+ /*
+ * Two spellings of the one thing: LLVM
+ * emits NODYLD32 for a .long against a
+ * section symbol, GNU as emits ABS32
+ * (bpf_reloc_type_lookup() maps
+ * BFD_RELOC_32 to it), and the value
+ * they describe is the same.
+ */
+
+ /*
+ * Only an executable section has its
+ * r_offset checked, and even there only
+ * for alignment; SHT_NOBITS has no
+ * raw_data at all. Bound it here,
+ * subtracting so it cannot wrap.
+ */
+ if (!dst_linked_sec->raw_data ||
+ dst_linked_sec->sec_sz < (int)sizeof(*val) ||
+ dst_rel->r_offset % sizeof(*val) ||
+ dst_rel->r_offset >
+ (size_t)dst_linked_sec->sec_sz - sizeof(*val)) {
+ pr_warn("ELF relo #%d in section #%zu points outside the data of section '%s' in %s\n",
+ j, src_sec->sec_idx,
+ dst_linked_sec->sec_name,
+ obj->filename);
+ return -EINVAL;
+ }
+ val = dst_linked_sec->raw_data + dst_rel->r_offset;
+ if (linker->swapped_endian)
+ *val = bswap_32(bswap_32(*val) + sec->dst_off);
+ else
+ *val += sec->dst_off;
} else {
pr_warn("relocation against STT_SECTION in non-exec section is not supported!\n");
return -EINVAL;
--
2.53.0-Meta
^ permalink raw reply related [flat|nested] 49+ messages in thread* Re: [PATCH bpf-next v5 18/21] libbpf: Let the static linker carry .bpf_cleanup relocations
2026-09-23 5:00 ` [PATCH bpf-next v5 18/21] libbpf: Let the static linker carry .bpf_cleanup relocations Yonghong Song
@ 2026-09-23 5:40 ` bot+bpf-ci
0 siblings, 0 replies; 49+ messages in thread
From: bot+bpf-ci @ 2026-09-23 5:40 UTC (permalink / raw)
To: yonghong.song, bpf
Cc: ast, andrii, daniel, eddyz87, kernel-team, ast, andrii, daniel,
martin.lau, eddyz87, yonghong.song, mason, ihor.solodrai
[-- Attachment #1: Type: text/plain, Size: 1396 bytes --]
> Taking them is keyed on the relocation type rather than on the section
> name, so any non-executable section could reach the new arm, and what it
> replaces is an unconditional refusal. Check what the refusal used to make
> unnecessary: a relocated section may be SHT_NOBITS, which extend_sec()
> leaves with no raw_data, and r_offset is checked for alignment only where
> the section holds instructions. Refuse those rather than write through
> them.
This isn't a bug, but could this paragraph state what the code does rather
than walking through the review reasoning?
The paragraph opens with an imperative ("Check what...") and describes the
thought process that led to the bounds check. The facts it conveys are
correct: an SHT_NOBITS target has no raw_data, and r_offset alignment is
only checked for executable sections. Would it read more like neighbouring
changelogs if it stated the conclusion directly, something like: "the
unconditional refusal also covered two cases the new arm must handle
itself: an SHT_NOBITS target has no raw_data, and r_offset is only
alignment-checked for executable sections, so both are now rejected
explicitly"?
---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md
CI run summary: https://github.com/kernel-patches/bpf/actions/runs/35821138073
^ permalink raw reply [flat|nested] 49+ messages in thread
* [PATCH bpf-next v5 19/21] selftests/bpf: Add an end-to-end .bpf_cleanup exception test
2026-09-23 4:58 [PATCH bpf-next v5 00/21] bpf: Run exception cleanup landing pads when bpf_throw() unwinds Yonghong Song
` (17 preceding siblings ...)
2026-09-23 5:00 ` [PATCH bpf-next v5 18/21] libbpf: Let the static linker carry .bpf_cleanup relocations Yonghong Song
@ 2026-09-23 5:00 ` Yonghong Song
2026-09-23 5:51 ` bot+bpf-ci
2026-09-23 5:00 ` [PATCH bpf-next v5 20/21] selftests/bpf: Cover the exception cleanup shapes the chain does not reach Yonghong Song
2026-09-23 5:00 ` [PATCH bpf-next v5 21/21] selftests/bpf: Load an exception cleanup program from a light skeleton Yonghong Song
20 siblings, 1 reply; 49+ messages in thread
From: Yonghong Song @ 2026-09-23 5:00 UTC (permalink / raw)
To: bpf
Cc: Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann,
Eduard Zingerman, kernel-team
C has no unwinding, so nothing here comes out of the frontend: the frames
that own a resource are written as __naked inline assembly, which spells
out by hand exactly what a frontend emits -- a call site bracketed by two
labels, a landing pad unreachable in the compiler's CFG, and a .bpf_cleanup
record tying them together. The assembler turns ".long <text label>" into
the same R_BPF_64_NODYLD32 relocation the BPF AsmPrinter emits, so libbpf
and the kernel see an object indistinguishable from a compiler-generated
one.
Call chain: entry -> foo1 -> foo1v -> foo2 -> foo3. foo3 holds a
non-preemptible section and throws inside it; foo2 holds an RCU read lock
and has two call sites sharing one pad, one of them its own throw; foo1v is
a void frame whose pad ends in a jump to a resume block placed after an
unrelated block that ends in a plain exit; foo1 owns nothing and gets no
record; entry is the boundary.
There are also some shapes the kernel refuses -- the ones with no correct
answer, and the ones a pad running on the bpf_throw() walker's stack cannot
express -- plus the return-value rejection that delivering an exception at
the boundary of a program type which constrains its return value produces.
The test skips rather than fails where the JIT cannot dispatch a landing
pad at all.
Signed-off-by: Yonghong Song <yonghong.song@linux.dev>
---
.../selftests/bpf/exceptions_cleanup.h | 29 +
.../bpf/prog_tests/exceptions_cleanup.c | 128 ++++
.../selftests/bpf/progs/exceptions_cleanup.c | 164 +++++
.../bpf/progs/exceptions_cleanup_fail.c | 678 ++++++++++++++++++
4 files changed, 999 insertions(+)
create mode 100644 tools/testing/selftests/bpf/exceptions_cleanup.h
create mode 100644 tools/testing/selftests/bpf/prog_tests/exceptions_cleanup.c
create mode 100644 tools/testing/selftests/bpf/progs/exceptions_cleanup.c
create mode 100644 tools/testing/selftests/bpf/progs/exceptions_cleanup_fail.c
diff --git a/tools/testing/selftests/bpf/exceptions_cleanup.h b/tools/testing/selftests/bpf/exceptions_cleanup.h
new file mode 100644
index 000000000000..630d2e207119
--- /dev/null
+++ b/tools/testing/selftests/bpf/exceptions_cleanup.h
@@ -0,0 +1,29 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
+#ifndef __EXCEPTIONS_CLEANUP_H__
+#define __EXCEPTIONS_CLEANUP_H__
+
+#define THROW_COOKIE 0x100
+
+/* progs/exceptions_cleanup.c: one bit per frame that reports it ran. */
+#define RAN_FOO3_PREEMPT 0x1
+#define RAN_FOO2_RCU 0x2
+#define RAN_FOO1V_PREEMPT 0x4
+#define RAN_FOO2_DROP 0x8
+#define RAN_BUMP 0x10
+
+#define CLEANUP_REC(begin, end, landing_pad) \
+ ".pushsection .bpf_cleanup,\"a\",@progbits;" \
+ ".long " begin ";" \
+ ".long " end ";" \
+ ".long " landing_pad ";" \
+ ".popsection;"
+
+/* Set a bit in @pads_ran. */
+#define PAD_RAN(bit) \
+ "r1 = %[pads_ran] ll;" \
+ "r2 = *(u64 *)(r1 + 0);" \
+ "r2 |= " bit ";" \
+ "*(u64 *)(r1 + 0) = r2;"
+
+#endif /* __EXCEPTIONS_CLEANUP_H__ */
diff --git a/tools/testing/selftests/bpf/prog_tests/exceptions_cleanup.c b/tools/testing/selftests/bpf/prog_tests/exceptions_cleanup.c
new file mode 100644
index 000000000000..d9823023ec04
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/exceptions_cleanup.c
@@ -0,0 +1,128 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
+#include <test_progs.h>
+#include "exceptions_cleanup.h"
+#include "exceptions_cleanup.skel.h"
+#include "exceptions_cleanup_fail.skel.h"
+
+/* foo3 threw: every frame that has a pad ran it. */
+#define PADS_FOO3_THREW \
+ (RAN_FOO3_PREEMPT | RAN_FOO2_RCU | RAN_FOO1V_PREEMPT | RAN_FOO2_DROP)
+
+/* foo2 threw after foo3 returned normally: foo3's pad must not run. */
+#define PADS_FOO2_THREW \
+ (RAN_FOO2_RCU | RAN_FOO1V_PREEMPT | RAN_FOO2_DROP)
+
+static void run(struct exceptions_cleanup *skel, __u64 input, __u32 retval,
+ __u64 pads)
+{
+ __u64 ctx = 0;
+ int err;
+
+ LIBBPF_OPTS(bpf_test_run_opts, topts,
+ .ctx_in = &ctx,
+ .ctx_size_in = sizeof(ctx),
+ );
+
+ skel->bss->input = input;
+ skel->bss->pads_ran = 0;
+ skel->bss->result = 0;
+
+ err = bpf_prog_test_run_opts(bpf_program__fd(skel->progs.entry), &topts);
+ if (!ASSERT_OK(err, "run"))
+ return;
+ ASSERT_EQ(topts.retval, retval, "retval");
+ /* bump() is not a landing pad; it sets its bit on every run. */
+ ASSERT_EQ(skel->bss->pads_ran, pads | RAN_BUMP, "pads_ran");
+}
+
+/*
+ * A table with more records than the program has instructions: no valid one
+ * can look like that, and it is refused before the kernel allocates for it.
+ */
+static void test_cleanup_info_cnt(void)
+{
+ struct bpf_insn insns[] = {
+ BPF_MOV64_IMM(BPF_REG_0, 0),
+ BPF_EXIT_INSN(),
+ };
+ struct bpf_cleanup_info rec = {
+ .begin_off = 0,
+ .end_off = 1,
+ .landing_pad_off = 1,
+ };
+ char log[512] = {};
+
+ LIBBPF_OPTS(bpf_prog_load_opts, opts,
+ .log_buf = log,
+ .log_size = sizeof(log),
+ .log_level = 1,
+ .cleanup_info = &rec,
+ .cleanup_info_cnt = 1 << 20,
+ .cleanup_info_rec_size = sizeof(rec));
+ int fd;
+
+ fd = bpf_prog_load(BPF_PROG_TYPE_SOCKET_FILTER, NULL, "GPL",
+ insns, ARRAY_SIZE(insns), &opts);
+ if (!ASSERT_LT(fd, 0, "load")) {
+ close(fd);
+ return;
+ }
+ /*
+ * Turned away on the count, rather than on whatever the records past
+ * the one below happen to hold.
+ */
+ ASSERT_HAS_SUBSTR(log, "cleanup info has 1048576 records for 2 instructions",
+ "log");
+}
+
+void test_exceptions_cleanup(void)
+{
+ char log[8192] = {};
+
+ LIBBPF_OPTS(bpf_object_open_opts, opts,
+ .kernel_log_buf = log,
+ .kernel_log_size = sizeof(log));
+ struct exceptions_cleanup *skel;
+ int err;
+
+ skel = exceptions_cleanup__open_opts(&opts);
+ if (!ASSERT_OK_PTR(skel, "open"))
+ return;
+
+ err = exceptions_cleanup__load(skel);
+ if (err) {
+ if (err == -EOPNOTSUPP &&
+ strstr(log, "exception cleanup needs a JIT that can dispatch landing pads")) {
+ printf("%s:SKIP:JIT cannot dispatch exception cleanup landing pads\n",
+ __func__);
+ test__skip();
+ } else if (!ASSERT_OK(err, "load")) {
+ fprintf(stderr, "%s", log);
+ }
+ exceptions_cleanup__destroy(skel);
+ return;
+ }
+
+ /* No throw: foo3 returns 1 ^ 1 == 0, foo2 adds one, no pad runs. */
+ if (test__start_subtest("no_throw"))
+ run(skel, 1, 1, 0);
+
+ /* foo3 throws; every pad runs and the cookie is delivered at entry. */
+ if (test__start_subtest("throw_from_foo3"))
+ run(skel, 101, THROW_COOKIE, PADS_FOO3_THREW);
+
+ /*
+ * foo3 returns 2 ^ 1 == 3, so foo2 throws from its own second region;
+ * foo3's frame is long gone, so its pad must not run.
+ */
+ if (test__start_subtest("throw_from_foo2"))
+ run(skel, 2, THROW_COOKIE, PADS_FOO2_THREW);
+
+ exceptions_cleanup__destroy(skel);
+
+ RUN_TESTS(exceptions_cleanup_fail);
+
+ if (test__start_subtest("cleanup_info_cnt"))
+ test_cleanup_info_cnt();
+}
diff --git a/tools/testing/selftests/bpf/progs/exceptions_cleanup.c b/tools/testing/selftests/bpf/progs/exceptions_cleanup.c
new file mode 100644
index 000000000000..3b7966357912
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/exceptions_cleanup.c
@@ -0,0 +1,164 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
+#include <vmlinux.h>
+#include <bpf/bpf_helpers.h>
+#include "bpf_misc.h"
+#include "exceptions_cleanup.h"
+
+static __used __noinline void __kfunc_btf_anchor(void)
+{
+ bpf_throw(0);
+ bpf_rcu_read_lock();
+ bpf_rcu_read_unlock();
+ bpf_preempt_disable();
+ bpf_preempt_enable();
+ bpf_unwind_resume();
+}
+
+__u64 input = 0;
+__u64 pads_ran = 0;
+__u64 result = 0;
+
+static __used __noinline __u64 foo3(__u64 x)
+{
+ bpf_preempt_disable();
+ if (x > 100)
+ asm volatile (
+ "r1 = %[cookie];"
+ "1:" "call bpf_throw;" /* cleanup region */
+ "2:"
+ "goto 3f;"
+ "4:" /* landing pad */
+ /*
+ * r0 at pad entry is whatever the walker leaves
+ * there, carried across the call in a callee-saved
+ * register and handed to the resume, the way a
+ * compiler-emitted pad passes the exception pointer to
+ * _Unwind_Resume. The kfunc ignores it, and the two pads
+ * below do without the shuffle.
+ */
+ "r7 = r0;"
+ "call bpf_preempt_enable;"
+ PAD_RAN("%[ran]")
+ "r1 = r7;"
+ "call bpf_unwind_resume;"
+ "3:"
+ CLEANUP_REC("1b", "2b", "4b")
+ :
+ : [cookie]"i"(THROW_COOKIE), [ran]"i"(RAN_FOO3_PREEMPT),
+ __imm_addr(pads_ran)
+ : __clobber_all);
+ bpf_preempt_enable();
+ return x ^ 1;
+}
+
+__u64 never = 0;
+
+static __used __naked __noinline void drop_glue(void)
+{
+ asm volatile (
+ PAD_RAN("%[ran]")
+ "exit;"
+ :
+ : [ran]"i"(RAN_FOO2_DROP), __imm_addr(pads_ran)
+ : __clobber_all);
+}
+
+static __used __naked __noinline __u64 foo2(void)
+{
+ asm volatile (
+ "r6 = r1;"
+ "call bpf_rcu_read_lock;"
+ "r1 = r6;"
+"1:" "call foo3;" /* cleanup region #1 */
+"2:"
+ "r6 = r0;"
+ "if r6 == 0 goto 5f;"
+ "r1 = %[cookie];"
+"3:" "call bpf_throw;" /* cleanup region #2 */
+"4:"
+ "r0 = 0;"
+ "exit;"
+"5:"
+ "call bpf_rcu_read_unlock;"
+ "r0 = r6;"
+ "r0 += 1;"
+ "exit;"
+"6:" /* landing pad, shared by both regions */
+ "call drop_glue;"
+ "call bpf_rcu_read_unlock;"
+ PAD_RAN("%[ran_rcu]")
+ "call bpf_unwind_resume;"
+ "exit;"
+ CLEANUP_REC("1b", "2b", "6b")
+ CLEANUP_REC("3b", "4b", "6b")
+ :
+ : [cookie]"i"(THROW_COOKIE), [ran_rcu]"i"(RAN_FOO2_RCU),
+ __imm_addr(pads_ran)
+ : __clobber_all);
+}
+
+static __used __naked __noinline void foo1v(void)
+{
+ asm volatile (
+ "call bpf_preempt_disable;"
+ "r1 = %[input] ll;"
+ "r1 = *(u64 *)(r1 + 0);"
+"1:" "call foo2;" /* cleanup region */
+"2:"
+ "r6 = r0;"
+ "call bpf_preempt_enable;"
+ "r1 = %[result] ll;"
+ "*(u64 *)(r1 + 0) = r6;"
+ "goto 7f;"
+"8:" /* landing pad */
+ "call bpf_preempt_enable;"
+ PAD_RAN("%[ran]")
+ "goto 9f;"
+"7:" /* the frame's own exit block */
+ "r0 = 0;"
+ "exit;"
+"9:" /* shared resume block */
+ "call bpf_unwind_resume;"
+ "exit;"
+ CLEANUP_REC("1b", "2b", "8b")
+ :
+ : [ran]"i"(RAN_FOO1V_PREEMPT), __imm_addr(input),
+ __imm_addr(result), __imm_addr(pads_ran)
+ : __clobber_all);
+}
+
+/*
+ * A frame with no cleanup record: an exception leaving it runs no pad. The
+ * throw never fires -- @never is global -- and the bit marks the return path.
+ */
+static __used __naked __noinline void bump(void)
+{
+ asm volatile (
+ PAD_RAN("%[ran]")
+ "r1 = %[never] ll;"
+ "r1 = *(u64 *)(r1 + 0);"
+ "if r1 == 0 goto 1f;"
+ "r1 = 0;"
+ "call bpf_throw;"
+"1:"
+ "exit;" /* r0 deliberately left alone */
+ :
+ : [ran]"i"(RAN_BUMP), __imm_addr(never), __imm_addr(pads_ran)
+ : __clobber_all);
+}
+
+__noinline __u64 foo1(void)
+{
+ bump();
+ foo1v();
+ return result;
+}
+
+SEC("syscall")
+int entry(void *ctx)
+{
+ return foo1();
+}
+
+char _license[] SEC("license") = "GPL";
diff --git a/tools/testing/selftests/bpf/progs/exceptions_cleanup_fail.c b/tools/testing/selftests/bpf/progs/exceptions_cleanup_fail.c
new file mode 100644
index 000000000000..a7569f98a98d
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/exceptions_cleanup_fail.c
@@ -0,0 +1,678 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
+#include <vmlinux.h>
+#include <bpf/bpf_helpers.h>
+#include "bpf_experimental.h"
+#include "bpf_misc.h"
+#include "../test_kmods/bpf_testmod_kfunc.h"
+#include "exceptions_cleanup.h"
+
+__u64 input = 0;
+
+static __used __noinline void __kfunc_btf_anchor(void)
+{
+ bpf_throw(0);
+ bpf_preempt_disable();
+ bpf_preempt_enable();
+ bpf_unwind_resume();
+}
+
+/*
+ * 1. A subprogram that may unwind, also used as a helper callback:
+ * bpf_loop()'s own kernel frame would end the walk before it found a
+ * boundary.
+ */
+static int throwing_cb(__u32 idx, void *ctx)
+{
+ bpf_throw(0xbad);
+ return 0;
+}
+
+static __used __naked __noinline __u64 cb_frame(void)
+{
+ asm volatile (
+ "call bpf_preempt_disable;"
+"1:" "call throwing_cb;" /* cleanup region */
+"2:"
+ "call bpf_preempt_enable;"
+ "r0 = 0;"
+ "exit;"
+"3:" /* landing pad */
+ "call bpf_preempt_enable;"
+ "call bpf_unwind_resume;"
+ "exit;"
+ CLEANUP_REC("1b", "2b", "3b")
+ ::: __clobber_all);
+}
+
+SEC("?syscall")
+__failure __msg("may unwind and is used as a callback")
+int callback_may_unwind(void *ctx)
+{
+ bpf_loop(1, throwing_cb, NULL, 0);
+ return cb_frame();
+}
+
+/*
+ * 2. A landing pad that reaches both an unwind resume and a plain exit. The
+ * walk takes both, and the exit is refused the same way a catch pad's is.
+ */
+static __used __naked __noinline __u64 inner_throw(void)
+{
+ asm volatile (
+ "r1 = 1;"
+ "call bpf_throw;"
+ "r0 = 0;"
+ "exit;"
+ ::: __clobber_all);
+}
+
+static __used __naked __noinline __u64 ambiguous_pad_frame(void)
+{
+ asm volatile (
+ "r6 = r1;"
+ "call bpf_preempt_disable;"
+"1:" "call inner_throw;" /* cleanup region */
+"2:"
+ "call bpf_preempt_enable;"
+ "r0 = 0;"
+ "exit;"
+"3:" /* landing pad: two ways out */
+ "call bpf_preempt_enable;"
+ "if r6 > 10 goto 4f;"
+ "call bpf_unwind_resume;"
+ "exit;"
+"4:"
+ "r0 = 0;"
+ "exit;"
+ CLEANUP_REC("1b", "2b", "3b")
+ ::: __clobber_all);
+}
+
+SEC("?syscall")
+__failure __msg("a catch pad is not supported yet")
+int ambiguous_landing_pad(void *ctx)
+{
+ return ambiguous_pad_frame();
+}
+
+/*
+ * 3. A throw from inside a landing pad: a second walk over the frames the
+ * first one is in the middle of discarding.
+ */
+static __used __naked __noinline __u64 throw_in_pad_frame(void)
+{
+ asm volatile (
+ "call bpf_preempt_disable;"
+"1:" "call inner_throw;" /* cleanup region */
+"2:"
+ "call bpf_preempt_enable;"
+ "r0 = 0;"
+ "exit;"
+"3:" /* landing pad that throws again */
+ "call bpf_preempt_enable;"
+ "r1 = 2;"
+ "call bpf_throw;"
+ "call bpf_unwind_resume;"
+ "exit;"
+ CLEANUP_REC("1b", "2b", "3b")
+ ::: __clobber_all);
+}
+
+SEC("?syscall")
+__failure __msg("throws while an exception is in flight")
+int throw_from_landing_pad(void *ctx)
+{
+ return throw_in_pad_frame();
+}
+
+/*
+ * 4. A cleanup table in a program that also installs an exception callback,
+ * two different answers to what runs on the way out.
+ */
+__noinline int unused_exc_cb(u64 cookie)
+{
+ return 0;
+}
+
+static __used __naked __noinline __u64 cb_and_table_frame(void)
+{
+ asm volatile (
+ "call bpf_preempt_disable;"
+ "r1 = 9;"
+"1:" "call bpf_throw;" /* cleanup region */
+"2:"
+ "r0 = 0;"
+ "exit;"
+"3:" /* landing pad */
+ "call bpf_preempt_enable;"
+ "call bpf_unwind_resume;"
+ "exit;"
+ CLEANUP_REC("1b", "2b", "3b")
+ ::: __clobber_all);
+}
+
+SEC("?syscall")
+__exception_cb(unused_exc_cb)
+__failure __msg("cannot be combined with an exception callback")
+int table_with_exception_cb(void *ctx)
+{
+ return cb_and_table_frame();
+}
+
+__u64 never;
+
+/*
+ * 5. A landing pad that calls a global subprogram which can throw. Not case 3:
+ * the verifier does not walk a global subprogram, so the throw inside it is
+ * never reached, and what refuses the call is subprog_info.might_throw.
+ */
+__noinline void pad_callee_that_throws(void)
+{
+ if (never)
+ bpf_throw(0);
+}
+
+static __used __naked __noinline __u64 pad_calls_thrower_frame(void)
+{
+ asm volatile (
+ "call bpf_preempt_disable;"
+ "r1 = 11;"
+"1:" "call bpf_throw;" /* cleanup region */
+"2:"
+ "r0 = 0;"
+ "exit;"
+"3:" /* landing pad */
+ "call pad_callee_that_throws;" /* ...which can throw: refused */
+ "call bpf_preempt_enable;"
+ "call bpf_unwind_resume;"
+ "exit;"
+ CLEANUP_REC("1b", "2b", "3b")
+ ::: __clobber_all);
+}
+
+SEC("?syscall")
+__failure __msg("which can throw while an exception is in flight")
+int pad_calls_thrower(void *ctx)
+{
+ return pad_calls_thrower_frame();
+}
+
+/*
+ * 6. A catch pad: it ends in a plain exit rather than a resume, and a walker
+ * that calls pads as subroutines cannot hand a frame back its own execution.
+ */
+static __used __naked __noinline __u64 catch_pad_frame(void)
+{
+ asm volatile (
+ "call bpf_preempt_disable;"
+ "r1 = 12;"
+"1:" "call bpf_throw;" /* cleanup region */
+"2:"
+ "r0 = 0;"
+ "exit;"
+"3:" /* catch pad: no resume, it stops here */
+ "call bpf_preempt_enable;"
+ "r0 = 0;"
+ "exit;"
+ CLEANUP_REC("1b", "2b", "3b")
+ ::: __clobber_all);
+}
+
+SEC("?syscall")
+__failure __msg("is not supported yet, only cleanup pads that resume")
+int catch_landing_pad(void *ctx)
+{
+ return catch_pad_frame();
+}
+
+/*
+ * 7. An exception reaching the boundary of a program type that constrains its
+ * return value: delivery makes the cookie that return value, and fentry has
+ * to return 0.
+ */
+static __used __naked __noinline __u64 boundary_throw_frame(void)
+{
+ asm volatile (
+ "call bpf_preempt_disable;"
+ "r1 = 7;"
+"1:" "call bpf_throw;" /* cleanup region */
+"2:"
+ "r0 = 0;"
+ "exit;"
+"3:" /* landing pad */
+ "call bpf_preempt_enable;"
+ "call bpf_unwind_resume;"
+ "exit;"
+ CLEANUP_REC("1b", "2b", "3b")
+ ::: __clobber_all);
+}
+
+SEC("?fentry/bpf_fentry_test1")
+__failure __msg("the register R1 has smin=7 smax=7 should have been in [0, 0]")
+int boundary_delivers(void *ctx)
+{
+ return boundary_throw_frame();
+}
+
+/*
+ * 8. A bpf_unwind_resume() outside any landing pad. Both JITs lower it as the
+ * way back out of a pad, which in ordinary code leaves a live frame standing
+ * with its epilogue skipped; no exception is in flight when the walk gets
+ * there.
+ */
+static __used __naked __noinline __u64 stray_resume_frame(void)
+{
+ asm volatile (
+ "call bpf_preempt_disable;"
+ "r1 = 13;"
+"1:" "call bpf_throw;" /* cleanup region */
+"2:"
+ "r0 = 0;"
+ "exit;"
+"3:" /* landing pad */
+ "call bpf_preempt_enable;"
+ "call bpf_unwind_resume;"
+ "exit;"
+ CLEANUP_REC("1b", "2b", "3b")
+ ::: __clobber_all);
+}
+
+SEC("?syscall")
+__failure __msg("reached without an exception in flight")
+int resume_outside_pad(void *ctx)
+{
+ /* Never taken, but reachable, which is all the verifier needs. */
+ if (never)
+ bpf_unwind_resume();
+ return stray_resume_frame();
+}
+
+/*
+ * 9. A bpf_unwind_resume() in a subprogram a landing pad calls. An exception is
+ * in flight, so what refuses it is the frame it is in: a resume reaches the
+ * walker only from the frame whose pad the walker called.
+ */
+static __used __naked __noinline void resume_in_callee(void)
+{
+ asm volatile (
+ "call bpf_unwind_resume;"
+ "exit;"
+ ::: __clobber_all);
+}
+
+static __used __naked __noinline __u64 pad_calls_resumer_frame(void)
+{
+ asm volatile (
+ "call bpf_preempt_disable;"
+ "r1 = 14;"
+"1:" "call bpf_throw;" /* cleanup region */
+"2:"
+ "r0 = 0;"
+ "exit;"
+"3:" /* landing pad */
+ "call bpf_preempt_enable;"
+ "call resume_in_callee;" /* ...which resumes: refused */
+ "call bpf_unwind_resume;"
+ "exit;"
+ CLEANUP_REC("1b", "2b", "3b")
+ ::: __clobber_all);
+}
+
+SEC("?syscall")
+__failure __msg("is in frame 2, not frame 1 whose landing pad the exception entered")
+int resume_in_pad_callee(void *ctx)
+{
+ return pad_calls_resumer_frame();
+}
+
+/*
+ * 10. A bpf_unwind_resume() in a program carrying no cleanup table, where
+ * that static rule does not run at all. do_check() refuses it on the state
+ * not unwinding, and has to: the JITs lower every one of these the same way.
+ */
+static __used __naked __noinline __u64 no_table_resume_frame(void)
+{
+ asm volatile (
+ "call bpf_unwind_resume;"
+ "r0 = 0;"
+ "exit;"
+ ::: __clobber_all);
+}
+
+SEC("?syscall")
+__failure __msg("reached without an exception in flight")
+int resume_without_table(void *ctx)
+{
+ return no_table_resume_frame();
+}
+
+/*
+ * 11. A landing pad that is itself a covered call site, so an exception out
+ * of it would have nowhere to go. Hand-written only: LLVM sinks a function's
+ * pads past every range it emits.
+ */
+static __used __naked __noinline __u64 nested_pad_frame(void)
+{
+ asm volatile (
+ "call bpf_preempt_disable;"
+"1:" "call inner_throw;" /* first cleanup region */
+"2:"
+ "call bpf_preempt_enable;"
+ "r0 = 0;"
+ "exit;"
+"3:" /* first pad, second region's call */
+ "call bpf_preempt_enable;"
+"4:"
+ "call bpf_unwind_resume;"
+ "exit;"
+"5:" /* second pad */
+ "call bpf_preempt_enable;"
+ "call bpf_unwind_resume;"
+ "exit;"
+ CLEANUP_REC("1b", "2b", "3b")
+ CLEANUP_REC("3b", "4b", "5b")
+ ::: __clobber_all);
+}
+
+SEC("?syscall")
+__failure __msg("is inside the call-site range of")
+int nested_landing_pad(void *ctx)
+{
+ return nested_pad_frame();
+}
+
+/*
+ * 12. A tail call in a landing pad: it unwinds the prologue off the stack
+ * pointer, which in a pad is the walker's.
+ */
+struct {
+ __uint(type, BPF_MAP_TYPE_PROG_ARRAY);
+ __uint(max_entries, 1);
+ __uint(key_size, sizeof(__u32));
+ __uint(value_size, sizeof(__u32));
+} tc_map SEC(".maps");
+
+static __used __naked __noinline __u64 tail_call_pad_frame(void)
+{
+ asm volatile (
+ "r6 = r1;"
+"1:" "call inner_throw;" /* cleanup region */
+"2:"
+ "r0 = 0;"
+ "exit;"
+"3:" /* landing pad */
+ "r1 = r6;"
+ "r2 = %[tc_map] ll;"
+ "r3 = 0;"
+ "call %[bpf_tail_call];"
+ "call bpf_unwind_resume;"
+ "exit;"
+ CLEANUP_REC("1b", "2b", "3b")
+ :
+ : __imm(bpf_tail_call), __imm_addr(tc_map)
+ : __clobber_all);
+}
+
+SEC("?syscall")
+__failure __msg("is in an exception cleanup landing pad")
+int tail_call_in_pad(void *ctx)
+{
+ return tail_call_pad_frame();
+}
+
+#if defined(__BPF_FEATURE_STACK_ARGUMENT)
+
+/*
+ * 13. A call that passes an argument on the stack, in a landing pad: the
+ * outgoing area the callee reads is not the one the caller wrote, the frame
+ * being the unwinding one and the stack pointer the walker's.
+ */
+static __used __noinline __u64 six_args(__u64 a, __u64 b, __u64 c, __u64 d,
+ __u64 e, __u64 f)
+{
+ return a + b + c + d + e + f;
+}
+
+static __used __naked __noinline __u64 stack_arg_pad_frame(void)
+{
+ asm volatile (
+ "call bpf_preempt_disable;"
+"1:" "call inner_throw;" /* cleanup region */
+"2:"
+ "call bpf_preempt_enable;"
+ "r0 = 0;"
+ "exit;"
+"3:" /* landing pad */
+ "call bpf_preempt_enable;"
+ "r1 = 1;"
+ "r2 = 2;"
+ "r3 = 3;"
+ "r4 = 4;"
+ "r5 = 5;"
+ "*(u64 *)(r11 - 8) = 6;" /* the sixth argument */
+ "call six_args;"
+ "call bpf_unwind_resume;"
+ "exit;"
+ CLEANUP_REC("1b", "2b", "3b")
+ ::: __clobber_all);
+}
+
+SEC("?syscall")
+__failure __msg("on-stack call argument in an exception cleanup landing pad")
+int stack_arg_in_pad(void *ctx)
+{
+ return stack_arg_pad_frame();
+}
+
+/*
+ * 14. The same, reached the other way: a kfunc whose by-value argument runs
+ * past the five argument registers, where the JIT fills the outgoing area and
+ * the rule above has no store to catch. The C call gives the extern its BTF.
+ */
+static __used __noinline void __nofit_btf_anchor(void)
+{
+ struct prog_test_pair_arg s = {};
+
+ bpf_kfunc_call_test_pair_arg_nofit(1, 2, 3, 4, s);
+}
+
+static __used __naked __noinline __u64 kfunc_arg_pad_frame(void)
+{
+ asm volatile (
+ "call bpf_preempt_disable;"
+"1:" "call inner_throw;" /* cleanup region */
+"2:"
+ "call bpf_preempt_enable;"
+ "r0 = 0;"
+ "exit;"
+"3:" /* landing pad */
+ "call bpf_preempt_enable;"
+ "call bpf_kfunc_call_test_pair_arg_nofit;"
+ "call bpf_unwind_resume;"
+ "exit;"
+ CLEANUP_REC("1b", "2b", "3b")
+ ::: __clobber_all);
+}
+
+SEC("?syscall")
+__failure __msg("calls a kfunc with an on-stack argument in an exception cleanup landing pad")
+int kfunc_stack_arg_in_pad(void *ctx)
+{
+ return kfunc_arg_pad_frame();
+}
+
+#endif /* __BPF_FEATURE_STACK_ARGUMENT */
+
+/*
+ * 15. A landing pad entered by ordinary control flow, arriving with none of
+ * what the walker sets up. The instruction it lands on would have to run both
+ * with an exception in flight and without, which the walk refuses.
+ */
+static __used __naked __noinline __u64 jump_into_pad_frame(void)
+{
+ asm volatile (
+ "r1 = %[input] ll;"
+ "r6 = *(u64 *)(r1 + 0);"
+ "if r6 > 7 goto 4f;" /* an ordinary branch into the pad */
+"1:" "call inner_throw;" /* cleanup region */
+"2:"
+ "r0 = 0;"
+ "exit;"
+"3:" /* landing pad */
+ "r7 = r0;"
+"4:" /* ... and its second instruction */
+ "call bpf_unwind_resume;"
+ "exit;"
+ CLEANUP_REC("1b", "2b", "3b")
+ :
+ : __imm_addr(input)
+ : __clobber_all);
+}
+
+SEC("?syscall")
+__failure __msg("runs both inside and outside an exception cleanup landing pad")
+int jump_into_pad(void *ctx)
+{
+ return jump_into_pad_frame();
+}
+
+#if defined(__TARGET_ARCH_x86) || defined(__TARGET_ARCH_arm64)
+
+/*
+ * 16. An indirect jump in a landing pad: the pad does not own the stack its
+ * targets would run on. A jump table entry is an offset from the program's
+ * section symbol, which has to be spelled in quotes here.
+ */
+static __used __naked __noinline void gotox_thrower(void)
+{
+ asm volatile (
+ "r1 = 15;"
+ "call bpf_throw;"
+ "exit;"
+ ::: __clobber_all);
+}
+
+SEC("?syscall")
+__failure __msg("indirect jump at insn")
+__naked void gotox_in_pad(void)
+{
+ asm volatile (
+ ".pushsection .jumptables,\"\",@progbits;"
+"jt0_%=:"
+ ".quad l0_%= - \"?syscall\";"
+ ".quad l1_%= - \"?syscall\";"
+ ".size jt0_%=, 16;"
+ ".global jt0_%=;"
+ ".popsection;"
+
+"1:" "call gotox_thrower;" /* cleanup region */
+"2:"
+ "r0 = 0;"
+ "exit;"
+"3:" /* landing pad */
+ "r1 = jt0_%= ll;"
+ "r1 += 8;"
+ "r2 = *(u64 *)(r1 + 0);"
+ /*
+ * gotox r2, as raw bytes: the mnemonic only reached the LLVM
+ * assembler in llvm 22, and BPF_RAW_INSN() needs <linux/bpf.h>, which
+ * vmlinux.h rules out. dst_reg is the other nibble on a big-endian
+ * target.
+ */
+#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
+ ".byte 0x0d, 0x20, 0, 0, 0, 0, 0, 0;"
+#else
+ ".byte 0x0d, 0x02, 0, 0, 0, 0, 0, 0;"
+#endif
+"l0_%=:"
+ "call bpf_unwind_resume;"
+ "exit;"
+"l1_%=:"
+ "call bpf_unwind_resume;"
+ "exit;"
+ CLEANUP_REC("1b", "2b", "3b")
+ ::: __clobber_all);
+}
+
+#endif /* x86 || arm64 */
+
+/*
+ * 17. A BPF_LD_[ABS|IND] in a landing pad. A failed load leaves the
+ * subprogram through the hidden "r0 = 0; exit" gen_ld_abs() patches in, and
+ * that exit is the epilogue, which unwinds a stack the pad does not own.
+ */
+static __used __naked __noinline __u64 ld_abs_pad_frame(void)
+{
+ asm volatile (
+ "r6 = r1;" /* the skb BPF_LD_ABS reads */
+"1:" "call inner_throw;" /* cleanup region */
+"2:"
+ "r0 = 0;"
+ "exit;"
+"3:" /* landing pad */
+ "r0 = *(u32 *)skb[0];"
+ "call bpf_unwind_resume;"
+ "exit;"
+ CLEANUP_REC("1b", "2b", "3b")
+ ::: __clobber_all);
+}
+
+SEC("?tc")
+__failure __msg("is in an exception cleanup landing pad")
+__naked void ld_abs_in_pad(void)
+{
+ asm volatile (
+ "call ld_abs_pad_frame;"
+ "exit;"
+ ::: __clobber_all);
+}
+
+/*
+ * 18. A bpf_unwind_resume() in a subprogram a landing pad called, which has a
+ * pad of its own and reached it by ordinary control flow. Not case 9: an
+ * exception is in flight, but this is not the frame whose pad the walker ran.
+ */
+static __used __naked __noinline __u64 own_pad_callee(void)
+{
+ asm volatile (
+ "r1 = %[input] ll;"
+ "r1 = *(u64 *)(r1 + 0);"
+ "if r1 == 0 goto 3f;" /* an ordinary branch into its pad */
+"1:" "call bpf_preempt_disable;" /* cleanup region: nothing that throws */
+ "call bpf_preempt_enable;"
+"2:"
+ "r0 = 0;"
+ "exit;"
+"3:" /* its landing pad */
+ "call bpf_unwind_resume;"
+ "exit;"
+ CLEANUP_REC("1b", "2b", "3b")
+ :
+ : __imm_addr(input)
+ : __clobber_all);
+}
+
+static __used __naked __noinline __u64 pad_calls_own_pad_frame(void)
+{
+ asm volatile (
+"1:" "call inner_throw;" /* cleanup region */
+"2:"
+ "r0 = 0;"
+ "exit;"
+"3:" /* landing pad, which calls the above */
+ "call own_pad_callee;"
+ "call bpf_unwind_resume;"
+ "exit;"
+ CLEANUP_REC("1b", "2b", "3b")
+ ::: __clobber_all);
+}
+
+SEC("?syscall")
+__failure __msg("is in frame 2, not frame 1 whose landing pad the exception entered")
+int resume_in_callee_own_pad(void *ctx)
+{
+ return pad_calls_own_pad_frame();
+}
+
+char _license[] SEC("license") = "GPL";
--
2.53.0-Meta
^ permalink raw reply related [flat|nested] 49+ messages in thread* Re: [PATCH bpf-next v5 19/21] selftests/bpf: Add an end-to-end .bpf_cleanup exception test
2026-09-23 5:00 ` [PATCH bpf-next v5 19/21] selftests/bpf: Add an end-to-end .bpf_cleanup exception test Yonghong Song
@ 2026-09-23 5:51 ` bot+bpf-ci
0 siblings, 0 replies; 49+ messages in thread
From: bot+bpf-ci @ 2026-09-23 5:51 UTC (permalink / raw)
To: yonghong.song, bpf
Cc: ast, andrii, daniel, eddyz87, kernel-team, ast, andrii, daniel,
martin.lau, eddyz87, yonghong.song, mason, ihor.solodrai
[-- Attachment #1: Type: text/plain, Size: 5243 bytes --]
> diff --git a/tools/testing/selftests/bpf/prog_tests/exceptions_cleanup.c b/tools/testing/selftests/bpf/prog_tests/exceptions_cleanup.c
> new file mode 100644
> index 000000000000..d9823023ec04
> --- /dev/null
> +++ b/tools/testing/selftests/bpf/prog_tests/exceptions_cleanup.c
[ ... ]
> @@ -0,0 +1,128 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
> +#include <test_progs.h>
> +#include "exceptions_cleanup.h"
> +#include "exceptions_cleanup.skel.h"
> +#include "exceptions_cleanup_fail.skel.h"
> +
> +/* foo3 threw: every frame that has a pad ran it. */
> +#define PADS_FOO3_THREW \
> + (RAN_FOO3_PREEMPT | RAN_FOO2_RCU | RAN_FOO1V_PREEMPT | RAN_FOO2_DROP)
> +
> +/* foo2 threw after foo3 returned normally: foo3's pad must not run. */
> +#define PADS_FOO2_THREW \
> + (RAN_FOO2_RCU | RAN_FOO1V_PREEMPT | RAN_FOO2_DROP)
[ ... ]
> +/*
> + * A table with more records than the program has instructions: no valid one
> + * can look like that, and it is refused before the kernel allocates for it.
> + */
> +static void test_cleanup_info_cnt(void)
> +{
> + struct bpf_insn insns[] = {
> + BPF_MOV64_IMM(BPF_REG_0, 0),
> + BPF_EXIT_INSN(),
> + };
> + struct bpf_cleanup_info rec = {
> + .begin_off = 0,
> + .end_off = 1,
> + .landing_pad_off = 1,
> + };
> + char log[512] = {};
> +
> + LIBBPF_OPTS(bpf_prog_load_opts, opts,
> + .log_buf = log,
> + .log_size = sizeof(log),
> + .log_level = 1,
> + .cleanup_info = &rec,
> + .cleanup_info_cnt = 1 << 20,
> + .cleanup_info_rec_size = sizeof(rec));
> + int fd;
> +
> + fd = bpf_prog_load(BPF_PROG_TYPE_SOCKET_FILTER, NULL, "GPL",
> + insns, ARRAY_SIZE(insns), &opts);
> + if (!ASSERT_LT(fd, 0, "load")) {
> + close(fd);
> + return;
> + }
> + /*
> + * Turned away on the count, rather than on whatever the records past
> + * the one below happen to hold.
> + */
> + ASSERT_HAS_SUBSTR(log, "cleanup info has 1048576 records for 2 instructions",
> + "log");
> +}
Does the comment point in the right direction? It says "the one below"
but the single supplied record 'rec' is declared roughly twenty lines
above this comment, not below it. Should this read "above" instead of
"below", or name 'rec' directly?
The substance is correct - check_cleanup_info() rejects on the count
before reading past the first record - only the direction word seems
inverted.
> +
> +void test_exceptions_cleanup(void)
> +{
> + char log[8192] = {};
> +
> + LIBBPF_OPTS(bpf_object_open_opts, opts,
> + .kernel_log_buf = log,
> + .kernel_log_size = sizeof(log));
> + struct exceptions_cleanup *skel;
> + int err;
> +
> + skel = exceptions_cleanup__open_opts(&opts);
> + if (!ASSERT_OK_PTR(skel, "open"))
> + return;
> +
> + err = exceptions_cleanup__load(skel);
> + if (err) {
> + if (err == -EOPNOTSUPP &&
> + strstr(log, "exception cleanup needs a JIT that can dispatch landing pads")) {
> + printf("%s:SKIP:JIT cannot dispatch exception cleanup landing pads\n",
> + __func__);
> + test__skip();
> + } else if (!ASSERT_OK(err, "load")) {
> + fprintf(stderr, "%s", log);
> + }
> + exceptions_cleanup__destroy(skel);
> + return;
> + }
Is the cleanup_info_cnt subtest reachable on architectures whose JIT
cannot dispatch landing pads?
The skip path here returns immediately after destroying the skeleton,
which means the code after this block never runs, including:
if (test__start_subtest("cleanup_info_cnt"))
test_cleanup_info_cnt();
bpf_jit_supports_cleanup_pads() is only implemented on x86 and arm64;
everywhere else the __weak stub returns false. When that happens
exceptions_cleanup__load() returns -EOPNOTSUPP, the test calls
test__skip() and returns early, skipping everything below.
test_cleanup_info_cnt() only exercises the UAPI count check in
check_cleanup_info(), which runs from bpf_check_btf_info() before
bpf_prepare_cleanup_exceptions() and therefore never reaches the JIT
gate. It loads its own 2-instruction BPF_PROG_TYPE_SOCKET_FILTER program
and would pass on any architecture.
Could test_cleanup_info_cnt() run before the skeleton load, or could
the skip path fall through to it, so the UAPI validation stays covered
on non-x86/arm64 builds?
> +
> + /* No throw: foo3 returns 1 ^ 1 == 0, foo2 adds one, no pad runs. */
> + if (test__start_subtest("no_throw"))
> + run(skel, 1, 1, 0);
> +
> + /* foo3 throws; every pad runs and the cookie is delivered at entry. */
> + if (test__start_subtest("throw_from_foo3"))
> + run(skel, 101, THROW_COOKIE, PADS_FOO3_THREW);
> +
> + /*
> + * foo3 returns 2 ^ 1 == 3, so foo2 throws from its own second region;
> + * foo3's frame is long gone, so its pad must not run.
> + */
> + if (test__start_subtest("throw_from_foo2"))
> + run(skel, 2, THROW_COOKIE, PADS_FOO2_THREW);
> +
> + exceptions_cleanup__destroy(skel);
> +
> + RUN_TESTS(exceptions_cleanup_fail);
> +
> + if (test__start_subtest("cleanup_info_cnt"))
> + test_cleanup_info_cnt();
> +}
---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md
CI run summary: https://github.com/kernel-patches/bpf/actions/runs/35821138073
^ permalink raw reply [flat|nested] 49+ messages in thread
* [PATCH bpf-next v5 20/21] selftests/bpf: Cover the exception cleanup shapes the chain does not reach
2026-09-23 4:58 [PATCH bpf-next v5 00/21] bpf: Run exception cleanup landing pads when bpf_throw() unwinds Yonghong Song
` (18 preceding siblings ...)
2026-09-23 5:00 ` [PATCH bpf-next v5 19/21] selftests/bpf: Add an end-to-end .bpf_cleanup exception test Yonghong Song
@ 2026-09-23 5:00 ` Yonghong Song
2026-09-23 5:51 ` bot+bpf-ci
2026-09-23 7:50 ` Eduard Zingerman
2026-09-23 5:00 ` [PATCH bpf-next v5 21/21] selftests/bpf: Load an exception cleanup program from a light skeleton Yonghong Song
20 siblings, 2 replies; 49+ messages in thread
From: Yonghong Song @ 2026-09-23 5:00 UTC (permalink / raw)
To: bpf
Cc: Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann,
Eduard Zingerman, kernel-team
The end-to-end test walks one call chain with a pad in most of its frames.
This adds the shapes that chain does not reach, numbered in the new file in
the order they are listed here:
- what a cleanup table leaves dead
- a callee called from both a covered and an uncovered site
- a pad that reads its frame's callee-saved registers
- a tail-call-reachable callee
- a pad in the main program's own frame
- a tail call that is taken
- an extension standing in for a covered call
- a throwing subprogram named by a BPF_PSEUDO_FUNC
- a record covering bpf_throw() itself
- a pad that calls a subprogram an extension can replace
- a covered bpf_throw() the sweep leaves last
- a region ending on a 16-byte instruction
- a pad that reloads from and writes to its own frame
- a pad two frames up
- a record covering only a nounwind call
- a pad terminated by _Unwind_Resume rather than bpf_unwind_resume
- a pad that calls a subprogram which tail calls
- the tail-call target carrying a table of its own
- a pad whose first instruction is a nop
- a pad that indexes its frame by a register the frame set before the
throwing call
- the same over a throwing global subprogram
- a pad that indexes its frame by r0, which no instruction wrote
One more shape needs an object of its own: exceptions_cleanup_ext_table.c
is an extension carrying a cleanup table of its own, attached over the
target the extension shape above already provides. Shapes 7 and 10 bring
small extension programs of their own.
Signed-off-by: Yonghong Song <yonghong.song@linux.dev>
---
.../selftests/bpf/exceptions_cleanup.h | 24 +
.../bpf/prog_tests/exceptions_cleanup.c | 368 ++++++
.../bpf/progs/exceptions_cleanup_ext_table.c | 48 +
.../bpf/progs/exceptions_cleanup_freplace.c | 17 +
.../progs/exceptions_cleanup_pad_freplace.c | 17 +
.../bpf/progs/exceptions_cleanup_shapes.c | 1049 +++++++++++++++++
6 files changed, 1523 insertions(+)
create mode 100644 tools/testing/selftests/bpf/progs/exceptions_cleanup_ext_table.c
create mode 100644 tools/testing/selftests/bpf/progs/exceptions_cleanup_freplace.c
create mode 100644 tools/testing/selftests/bpf/progs/exceptions_cleanup_pad_freplace.c
create mode 100644 tools/testing/selftests/bpf/progs/exceptions_cleanup_shapes.c
diff --git a/tools/testing/selftests/bpf/exceptions_cleanup.h b/tools/testing/selftests/bpf/exceptions_cleanup.h
index 630d2e207119..d6f63f147c4a 100644
--- a/tools/testing/selftests/bpf/exceptions_cleanup.h
+++ b/tools/testing/selftests/bpf/exceptions_cleanup.h
@@ -4,6 +4,7 @@
#define __EXCEPTIONS_CLEANUP_H__
#define THROW_COOKIE 0x100
+#define INNER_COOKIE 0x200
/* progs/exceptions_cleanup.c: one bit per frame that reports it ran. */
#define RAN_FOO3_PREEMPT 0x1
@@ -12,6 +13,29 @@
#define RAN_FOO2_DROP 0x8
#define RAN_BUMP 0x10
+/* progs/exceptions_cleanup_shapes.c: one bit per shape, numbered its own way. */
+#define RAN_SWEEP 0x1
+#define RAN_SHARED 0x2
+#define RAN_REGS 0x4
+#define RAN_TAIL_CALL 0x8
+#define RAN_MAIN_PAD 0x10
+#define RAN_TC_TAKEN 0x20
+#define RAN_FREPLACE 0x40
+#define RAN_ADDR_TAKEN 0x80
+#define RAN_NO_SUBPROG 0x100
+#define RAN_PAD_CALLS 0x200
+#define RAN_PAD_FIRST 0x400
+#define RAN_WIDE_REC 0x800
+#define RAN_PAD_STACK 0x1000
+#define RAN_DEEP_PAD 0x2000
+#define RAN_NOUNWIND_REC 0x4000
+#define RAN_RESUME_ALIAS 0x8000
+#define RAN_PAD_TAIL_CALL 0x10000
+#define RAN_NOP_PAD 0x20000
+#define RAN_VAR_STACK 0x40000
+#define RAN_GLOBAL_PAD 0x80000
+#define RAN_PAD_R0 0x100000
+
#define CLEANUP_REC(begin, end, landing_pad) \
".pushsection .bpf_cleanup,\"a\",@progbits;" \
".long " begin ";" \
diff --git a/tools/testing/selftests/bpf/prog_tests/exceptions_cleanup.c b/tools/testing/selftests/bpf/prog_tests/exceptions_cleanup.c
index d9823023ec04..dbdb78b791e0 100644
--- a/tools/testing/selftests/bpf/prog_tests/exceptions_cleanup.c
+++ b/tools/testing/selftests/bpf/prog_tests/exceptions_cleanup.c
@@ -4,6 +4,10 @@
#include "exceptions_cleanup.h"
#include "exceptions_cleanup.skel.h"
#include "exceptions_cleanup_fail.skel.h"
+#include "exceptions_cleanup_shapes.skel.h"
+#include "exceptions_cleanup_freplace.skel.h"
+#include "exceptions_cleanup_pad_freplace.skel.h"
+#include "exceptions_cleanup_ext_table.skel.h"
/* foo3 threw: every frame that has a pad ran it. */
#define PADS_FOO3_THREW \
@@ -36,6 +40,368 @@ static void run(struct exceptions_cleanup *skel, __u64 input, __u32 retval,
ASSERT_EQ(skel->bss->pads_ran, pads | RAN_BUMP, "pads_ran");
}
+static void run_shape(struct exceptions_cleanup_shapes *skel, struct bpf_program *prog,
+ __u64 input, __u32 retval, __u64 pads)
+{
+ __u64 ctx = 0;
+ int err;
+
+ LIBBPF_OPTS(bpf_test_run_opts, topts,
+ .ctx_in = &ctx,
+ .ctx_size_in = sizeof(ctx),
+ );
+
+ skel->bss->input = input;
+ skel->bss->pads_ran = 0;
+
+ err = bpf_prog_test_run_opts(bpf_program__fd(prog), &topts);
+ if (!ASSERT_OK(err, "run"))
+ return;
+ ASSERT_EQ(topts.retval, retval, "retval");
+ ASSERT_EQ(skel->bss->pads_ran, pads, "pads_ran");
+}
+
+static void test_freplace(struct exceptions_cleanup_shapes *skel)
+{
+ struct exceptions_cleanup_freplace *fr;
+ struct bpf_link *link;
+ int tgt_fd;
+
+ tgt_fd = bpf_program__fd(skel->progs.entry_freplace);
+
+ fr = exceptions_cleanup_freplace__open();
+ if (!ASSERT_OK_PTR(fr, "freplace open"))
+ return;
+
+ if (!ASSERT_OK(bpf_program__set_attach_target(fr->progs.new_fr_callee,
+ tgt_fd, "fr_callee"),
+ "set_attach_target"))
+ goto out;
+ if (!ASSERT_OK(exceptions_cleanup_freplace__load(fr), "freplace load"))
+ goto out;
+
+ link = bpf_program__attach_freplace(fr->progs.new_fr_callee, tgt_fd,
+ "fr_callee");
+ if (!ASSERT_OK_PTR(link, "attach_freplace"))
+ goto out;
+
+ run_shape(skel, skel->progs.entry_freplace, 101, THROW_COOKIE, 0);
+ bpf_link__destroy(link);
+out:
+ exceptions_cleanup_freplace__destroy(fr);
+}
+
+static void test_pad_calls_freplace(struct exceptions_cleanup_shapes *skel)
+{
+ struct exceptions_cleanup_pad_freplace *fr;
+ struct bpf_link *link;
+ __u64 ctx = 0;
+ int tgt_fd, err;
+
+ LIBBPF_OPTS(bpf_test_run_opts, topts,
+ .ctx_in = &ctx,
+ .ctx_size_in = sizeof(ctx),
+ );
+
+ tgt_fd = bpf_program__fd(skel->progs.entry_pad_calls);
+
+ fr = exceptions_cleanup_pad_freplace__open();
+ if (!ASSERT_OK_PTR(fr, "pad freplace open"))
+ return;
+
+ if (!ASSERT_OK(bpf_program__set_attach_target(fr->progs.new_pad_callee,
+ tgt_fd, "pad_callee"),
+ "set_attach_target"))
+ goto out;
+ if (!ASSERT_OK(exceptions_cleanup_pad_freplace__load(fr), "pad freplace load"))
+ goto out;
+
+ link = bpf_program__attach_freplace(fr->progs.new_pad_callee, tgt_fd,
+ "pad_callee");
+ if (!ASSERT_OK_PTR(link, "attach_freplace"))
+ goto out;
+
+ skel->bss->input = 101;
+ skel->bss->pads_ran = 0;
+ skel->bss->pad_runs = 0;
+
+ err = bpf_prog_test_run_opts(tgt_fd, &topts);
+ if (!ASSERT_OK(err, "run"))
+ goto out_link;
+
+ ASSERT_EQ(skel->bss->pad_runs, 1, "pad_runs");
+ ASSERT_EQ(skel->bss->pads_ran, RAN_PAD_CALLS, "pads_ran");
+ ASSERT_EQ(topts.retval, THROW_COOKIE, "retval");
+out_link:
+ bpf_link__destroy(link);
+out:
+ exceptions_cleanup_pad_freplace__destroy(fr);
+}
+
+static void test_ext_table(struct exceptions_cleanup_shapes *skel)
+{
+ struct exceptions_cleanup_ext_table *fr;
+ struct bpf_link *link;
+ int tgt_fd;
+
+ tgt_fd = bpf_program__fd(skel->progs.entry_freplace);
+
+ fr = exceptions_cleanup_ext_table__open();
+ if (!ASSERT_OK_PTR(fr, "ext table open"))
+ return;
+
+ if (!ASSERT_OK(bpf_program__set_attach_target(fr->progs.new_fr_callee,
+ tgt_fd, "fr_callee"),
+ "set_attach_target"))
+ goto out;
+ if (!ASSERT_OK(exceptions_cleanup_ext_table__load(fr), "ext table load"))
+ goto out;
+
+ link = bpf_program__attach_freplace(fr->progs.new_fr_callee, tgt_fd,
+ "fr_callee");
+ if (!ASSERT_OK_PTR(link, "attach_freplace"))
+ goto out;
+
+ fr->bss->ext_pad_ran = 0;
+ run_shape(skel, skel->progs.entry_freplace, 101, THROW_COOKIE, 0);
+ ASSERT_EQ(fr->bss->ext_pad_ran, 1, "ext_pad_ran");
+
+ bpf_link__destroy(link);
+out:
+ exceptions_cleanup_ext_table__destroy(fr);
+}
+
+static void test_shapes(void)
+{
+ struct exceptions_cleanup_shapes *skel;
+
+ skel = exceptions_cleanup_shapes__open_and_load();
+ if (!ASSERT_OK_PTR(skel, "shapes open_and_load"))
+ return;
+
+ /* The frame loads at all only if everything unreachable in it went. */
+ if (test__start_subtest("sweep_no_throw"))
+ run_shape(skel, skel->progs.entry_sweep, 1, 0, 0);
+ if (test__start_subtest("sweep_throw"))
+ run_shape(skel, skel->progs.entry_sweep, 101, THROW_COOKIE, RAN_SWEEP);
+
+ /* The covered call unwinds to the pad; the uncovered one walks past. */
+ if (test__start_subtest("shared_callee_no_throw")) {
+ skel->bss->outer_input = 0;
+ run_shape(skel, skel->progs.entry_shared, 1, 2, 0);
+ }
+ if (test__start_subtest("shared_callee_throw")) {
+ skel->bss->outer_input = 0;
+ run_shape(skel, skel->progs.entry_shared, 101, THROW_COOKIE, RAN_SHARED);
+ }
+ if (test__start_subtest("shared_callee_uncovered_throw")) {
+ skel->bss->outer_input = 101;
+ run_shape(skel, skel->progs.entry_shared, 1, THROW_COOKIE, 0);
+ skel->bss->outer_input = 0;
+ }
+
+ /* The pad only sets its bit if it got the frame's own r6-r9 back. */
+ if (test__start_subtest("pad_sees_callee_saved"))
+ run_shape(skel, skel->progs.entry_regs, 101, THROW_COOKIE, RAN_REGS);
+
+ /* Same check, with a tail-call-reachable callee: its spill moves. */
+ if (test__start_subtest("tail_call_no_throw"))
+ run_shape(skel, skel->progs.entry_tail_call, 1, 0, 0);
+ if (test__start_subtest("tail_call_throw"))
+ run_shape(skel, skel->progs.entry_tail_call, 101, THROW_COOKIE,
+ RAN_TAIL_CALL);
+
+ /* A region around a nounwind call: no pad dispatched, still loads. */
+ if (test__start_subtest("nounwind_region"))
+ run_shape(skel, skel->progs.entry_nounwind_rec, 1, 0, 0);
+
+ /* A pad in the main program's own frame, not in a subprogram. */
+ if (test__start_subtest("main_program_pad"))
+ run_shape(skel, skel->progs.entry_main_pad, 101, THROW_COOKIE,
+ RAN_MAIN_PAD);
+
+ /*
+ * The same call site either way: the subprogram's throw unwinds into
+ * this frame and runs its pad, an extension's stops at its own boundary.
+ */
+ if (test__start_subtest("freplace_subprog_throws"))
+ run_shape(skel, skel->progs.entry_freplace, 7, THROW_COOKIE,
+ RAN_FREPLACE);
+ if (test__start_subtest("freplace_extension_throws"))
+ test_freplace(skel);
+
+ /*
+ * A tail call that is taken: the walk ends at the target, so the cookie
+ * comes back from there and this frame's pad does not run -- though it
+ * is reachable, so a walk past the boundary would find it.
+ */
+ if (test__start_subtest("tail_call_taken")) {
+ int key = 0, prog_fd = bpf_program__fd(skel->progs.tc_target);
+
+ if (ASSERT_OK(bpf_map_update_elem(bpf_map__fd(skel->maps.taken_table),
+ &key, &prog_fd, BPF_ANY),
+ "populate taken_table"))
+ run_shape(skel, skel->progs.entry_tail_taken, 101,
+ THROW_COOKIE, 0);
+ }
+
+ /*
+ * A throwing subprog named by a BPF_PSEUDO_FUNC and handed to a
+ * bpf_loop() the verifier never reaches: the callback check has to fire
+ * on the helper call, not on the ld_imm64.
+ */
+ if (test__start_subtest("addr_taken_no_throw"))
+ run_shape(skel, skel->progs.entry_addr_taken, 1, 2, 0);
+ if (test__start_subtest("addr_taken_throw"))
+ run_shape(skel, skel->progs.entry_addr_taken, 101, THROW_COOKIE,
+ RAN_ADDR_TAKEN);
+
+ /*
+ * A record covering bpf_throw() itself rather than a call to a frame
+ * that throws: raised, caught up with and delivered in one frame.
+ */
+ if (test__start_subtest("no_subprog_no_throw"))
+ run_shape(skel, skel->progs.entry_no_subprog, 1, 0, 0);
+ if (test__start_subtest("no_subprog_throw"))
+ run_shape(skel, skel->progs.entry_no_subprog, 101, THROW_COOKIE,
+ RAN_NO_SUBPROG);
+
+ /*
+ * A pad that calls a subprogram; with a throwing extension in its place,
+ * the nested exception has to stop there, not restart this pad.
+ */
+ if (test__start_subtest("pad_calls_subprog")) {
+ skel->bss->pad_runs = 0;
+ run_shape(skel, skel->progs.entry_pad_calls, 101, THROW_COOKIE,
+ RAN_PAD_CALLS);
+ ASSERT_EQ(skel->bss->pad_runs, 1, "pad_runs");
+ }
+ if (test__start_subtest("pad_calls_throwing_extension"))
+ test_pad_calls_freplace(skel);
+
+ /*
+ * A covered throw the sweep leaves last, where the default exception
+ * callback is patched in; the pad's bit needs r6-r9 still spilled.
+ */
+ if (test__start_subtest("pad_before_throw"))
+ run_shape(skel, skel->progs.entry_pad_first, 101, THROW_COOKIE,
+ RAN_PAD_FIRST);
+
+ /*
+ * A region whose last instruction is a 16-byte one, so that end - 1
+ * names the half of it that is not an instruction.
+ */
+ if (test__start_subtest("region_ends_on_ldimm64"))
+ run_shape(skel, skel->progs.entry_wide_rec, 101, THROW_COOKIE,
+ RAN_WIDE_REC);
+
+ /*
+ * A pad that reloads from and writes to its own frame's stack, which a
+ * JIT addressing the frame through the stack pointer gets wrong.
+ */
+ if (test__start_subtest("pad_uses_own_frame"))
+ run_shape(skel, skel->progs.entry_pad_stack, 101, THROW_COOKIE,
+ RAN_PAD_STACK);
+
+ /* The same, with an uncovered frame between the throw and the pad. */
+ if (test__start_subtest("pad_two_frames_up"))
+ run_shape(skel, skel->progs.entry_deep_pad, 101, THROW_COOKIE,
+ RAN_DEEP_PAD);
+
+ /* An extension program with a cleanup table of its own. */
+ if (test__start_subtest("extension_carries_table"))
+ test_ext_table(skel);
+
+ /*
+ * A pad terminated by _Unwind_Resume, which libbpf maps onto the kfunc;
+ * every other program here calls bpf_unwind_resume directly.
+ */
+ if (test__start_subtest("resume_alias"))
+ run_shape(skel, skel->progs.entry_resume_alias, 101,
+ THROW_COOKIE, RAN_RESUME_ALIAS);
+
+ /*
+ * A pad that calls a subprogram which tail calls, array empty and then
+ * populated: the tail call releases only the callee's own prologue.
+ */
+ if (test__start_subtest("pad_callee_tail_call")) {
+ int key = 0, prog_fd = bpf_program__fd(skel->progs.pad_tc_target);
+
+ skel->bss->pad_tc_target_ran = 0;
+ skel->bss->pad_runs = 0;
+ run_shape(skel, skel->progs.entry_pad_tail_call, 101,
+ THROW_COOKIE, RAN_PAD_TAIL_CALL);
+ ASSERT_EQ(skel->bss->pad_tc_target_ran, 0, "target not run");
+ ASSERT_EQ(skel->bss->pad_runs, 1, "pad_runs");
+
+ if (ASSERT_OK(bpf_map_update_elem(bpf_map__fd(skel->maps.pad_tc_table),
+ &key, &prog_fd, BPF_ANY),
+ "populate pad_tc_table")) {
+ skel->bss->pad_runs = 0;
+ run_shape(skel, skel->progs.entry_pad_tail_call, 101,
+ THROW_COOKIE, RAN_PAD_TAIL_CALL);
+ ASSERT_EQ(skel->bss->pad_tc_target_ran, 1, "target ran");
+ ASSERT_EQ(skel->bss->pad_runs, 1, "pad_runs");
+ }
+ }
+
+ /*
+ * The same, into a target that carries a table and throws: that target
+ * is a boundary, so the outer pad runs once, not twice.
+ */
+ if (test__start_subtest("pad_callee_tail_call_throws")) {
+ int key = 0, prog_fd = bpf_program__fd(skel->progs.pad_tc_throw_target);
+
+ if (ASSERT_OK(bpf_map_update_elem(bpf_map__fd(skel->maps.pad_tc_table),
+ &key, &prog_fd, BPF_ANY),
+ "populate pad_tc_table")) {
+ skel->bss->pad_runs = 0;
+ skel->bss->tc_target_pad_runs = 0;
+ run_shape(skel, skel->progs.entry_pad_tail_call, 101,
+ THROW_COOKIE, RAN_PAD_TAIL_CALL);
+ /* The target cleaned up after itself, once. */
+ ASSERT_EQ(skel->bss->tc_target_pad_runs, 1,
+ "tc_target_pad_runs");
+ /* And the outer pad was not started over. */
+ ASSERT_EQ(skel->bss->pad_runs, 1, "pad_runs");
+ }
+ }
+
+ /*
+ * A pad whose first instruction opt_remove_nops() deletes: the record
+ * has to follow the pad rather than be dropped with the nop.
+ */
+ if (test__start_subtest("nop_at_pad_head"))
+ run_shape(skel, skel->progs.entry_nop_pad, 101, THROW_COOKIE,
+ RAN_NOP_PAD);
+
+ /*
+ * A pad that indexes its frame's stack by a register the frame set
+ * before the throwing call, marked precise back across the unwind edge.
+ */
+ if (test__start_subtest("pad_var_stack_offset"))
+ run_shape(skel, skel->progs.entry_var_stack, 101, THROW_COOKIE,
+ RAN_VAR_STACK);
+
+ /*
+ * The same, where the throw is in a global subprogram, which the
+ * verifier walks without a frame of its own.
+ */
+ if (test__start_subtest("pad_over_global_subprog"))
+ run_shape(skel, skel->progs.entry_global_pad, 101, THROW_COOKIE,
+ RAN_GLOBAL_PAD);
+
+ /*
+ * A pad that indexes its frame by r0, which no insn in the frame
+ * wrote: the precision request for it must not cross the unwind edge.
+ */
+ if (test__start_subtest("pad_r0_precision"))
+ run_shape(skel, skel->progs.entry_pad_r0, 101, THROW_COOKIE,
+ RAN_PAD_R0);
+
+ exceptions_cleanup_shapes__destroy(skel);
+}
+
/*
* A table with more records than the program has instructions: no valid one
* can look like that, and it is refused before the kernel allocates for it.
@@ -121,6 +487,8 @@ void test_exceptions_cleanup(void)
exceptions_cleanup__destroy(skel);
+ test_shapes();
+
RUN_TESTS(exceptions_cleanup_fail);
if (test__start_subtest("cleanup_info_cnt"))
diff --git a/tools/testing/selftests/bpf/progs/exceptions_cleanup_ext_table.c b/tools/testing/selftests/bpf/progs/exceptions_cleanup_ext_table.c
new file mode 100644
index 000000000000..d14db48d6b29
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/exceptions_cleanup_ext_table.c
@@ -0,0 +1,48 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
+#include <vmlinux.h>
+#include <bpf/bpf_helpers.h>
+#include "bpf_misc.h"
+#include "exceptions_cleanup.h"
+
+__u64 ext_pad_ran = 0;
+
+/* Without a 32-bit int in BTF, libbpf's dummy_ksym var gets type id 0. */
+int btf_int_anchor;
+
+static __used __noinline void __kfunc_btf_anchor(void)
+{
+ bpf_throw(0);
+ bpf_preempt_disable();
+ bpf_preempt_enable();
+ bpf_unwind_resume();
+}
+
+static __used __naked __noinline __u64 ext_frame(void)
+{
+ asm volatile (
+ "call bpf_preempt_disable;"
+ "r1 = %[cookie];"
+"1:" "call bpf_throw;" /* cleanup region */
+"2:"
+ "exit;"
+"3:" /* landing pad */
+ "call bpf_preempt_enable;"
+ "r1 = %[ext_pad_ran] ll;"
+ "r2 = 1;"
+ "*(u64 *)(r1 + 0) = r2;"
+ "call bpf_unwind_resume;"
+ "exit;"
+ CLEANUP_REC("1b", "2b", "3b")
+ :
+ : [cookie]"i"(THROW_COOKIE), __imm_addr(ext_pad_ran)
+ : __clobber_all);
+}
+
+SEC("freplace/fr_callee")
+__u64 new_fr_callee(__u64 x)
+{
+ return ext_frame();
+}
+
+char _license[] SEC("license") = "GPL";
diff --git a/tools/testing/selftests/bpf/progs/exceptions_cleanup_freplace.c b/tools/testing/selftests/bpf/progs/exceptions_cleanup_freplace.c
new file mode 100644
index 000000000000..afb358fd3d40
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/exceptions_cleanup_freplace.c
@@ -0,0 +1,17 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
+#include <vmlinux.h>
+#include <bpf/bpf_helpers.h>
+#include "exceptions_cleanup.h"
+
+/* Without a 32-bit int in BTF, libbpf's dummy_ksym var gets type id 0. */
+int btf_int_anchor;
+
+SEC("freplace/fr_callee")
+__u64 new_fr_callee(__u64 x)
+{
+ bpf_throw(THROW_COOKIE);
+ return 0;
+}
+
+char _license[] SEC("license") = "GPL";
diff --git a/tools/testing/selftests/bpf/progs/exceptions_cleanup_pad_freplace.c b/tools/testing/selftests/bpf/progs/exceptions_cleanup_pad_freplace.c
new file mode 100644
index 000000000000..eabac6baabb7
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/exceptions_cleanup_pad_freplace.c
@@ -0,0 +1,17 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
+#include <vmlinux.h>
+#include <bpf/bpf_helpers.h>
+#include "exceptions_cleanup.h"
+
+/* Without a 32-bit int in BTF, libbpf's dummy_ksym var gets type id 0. */
+int btf_int_anchor;
+
+SEC("freplace/pad_callee")
+__u64 new_pad_callee(__u64 x)
+{
+ bpf_throw(INNER_COOKIE);
+ return 0;
+}
+
+char _license[] SEC("license") = "GPL";
diff --git a/tools/testing/selftests/bpf/progs/exceptions_cleanup_shapes.c b/tools/testing/selftests/bpf/progs/exceptions_cleanup_shapes.c
new file mode 100644
index 000000000000..f44af8be4fda
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/exceptions_cleanup_shapes.c
@@ -0,0 +1,1049 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
+#include <vmlinux.h>
+#include <bpf/bpf_helpers.h>
+#include "bpf_misc.h"
+#include "exceptions_cleanup.h"
+
+#define PAD_COUNT \
+ "r1 = %[pad_runs] ll;" \
+ "r2 = *(u64 *)(r1 + 0);" \
+ "r2 += 1;" \
+ "*(u64 *)(r1 + 0) = r2;"
+
+static __used __noinline void __kfunc_btf_anchor(void)
+{
+ bpf_throw(0);
+ bpf_rcu_read_lock();
+ bpf_rcu_read_unlock();
+ bpf_preempt_disable();
+ bpf_preempt_enable();
+ bpf_unwind_resume();
+}
+
+__u64 input = 0;
+__u64 outer_input = 0;
+__u64 magic = 0x5eed;
+__u64 pads_ran = 0;
+__u64 pad_runs = 0;
+
+/*
+ * 1. Everything a cleanup table leaves dead: a throw's continuation, the tail
+ * after a resume with an ld_imm64 and a branch in it, and the block only that
+ * continuation reaches.
+ */
+static __used __naked __noinline __u64 sweep_frame(void)
+{
+ asm volatile (
+ "r1 = %[input] ll;"
+ "r6 = *(u64 *)(r1 + 0);"
+ "call bpf_preempt_disable;"
+ "if r6 < 101 goto 6f;"
+ "r1 = %[cookie];"
+"1:" "call bpf_throw;" /* cleanup region */
+"2:"
+ "goto 3f;"
+"4:" /* landing pad */
+ "r7 = r0;"
+ "call bpf_preempt_enable;"
+ PAD_RAN("%[ran]")
+ "r1 = r7;"
+ "call bpf_unwind_resume;"
+ "r1 = %[pads_ran] ll;"
+ "r2 = *(u64 *)(r1 + 0);"
+ "if r2 == 0 goto 5f;"
+ "call bpf_preempt_enable;"
+ "r0 = 7;"
+ "exit;"
+"5:"
+ "r0 = 8;"
+ "exit;"
+"3:" /* dead: only the dead goto reaches it */
+ "r0 = 9;"
+ "exit;"
+"6:" /* live: the ordinary return */
+ "call bpf_preempt_enable;"
+ "r0 = 0;"
+ "exit;"
+ CLEANUP_REC("1b", "2b", "4b")
+ :
+ : [cookie]"i"(THROW_COOKIE), [ran]"i"(RAN_SWEEP),
+ __imm_addr(input), __imm_addr(pads_ran)
+ : __clobber_all);
+}
+
+SEC("syscall")
+int entry_sweep(void *ctx)
+{
+ return sweep_frame();
+}
+
+/*
+ * 2. A callee called from both a covered and an uncovered site: the pad
+ * belongs to the call site, not the callee. Either site can throw; the RCU
+ * lock is taken between them, so only the covered call unwinds holding it.
+ */
+static __used __noinline __u64 shared_callee(__u64 x)
+{
+ if (x > 100)
+ bpf_throw(THROW_COOKIE);
+ return x + 1;
+}
+
+static __used __naked __noinline __u64 shared_frame(void)
+{
+ asm volatile (
+ "r1 = %[input] ll;"
+ "r6 = *(u64 *)(r1 + 0);"
+ "r1 = %[outer_input] ll;"
+ "r1 = *(u64 *)(r1 + 0);"
+ "call shared_callee;" /* uncovered: no pad for its unwind */
+ "call bpf_rcu_read_lock;"
+ "r1 = r6;"
+"1:" "call shared_callee;" /* cleanup region */
+"2:"
+ "r6 = r0;"
+ "call bpf_rcu_read_unlock;"
+ "r0 = r6;"
+ "exit;"
+"3:" /* landing pad */
+ "r7 = r0;"
+ "call bpf_rcu_read_unlock;"
+ PAD_RAN("%[ran]")
+ "r1 = r7;"
+ "call bpf_unwind_resume;"
+ "exit;"
+ CLEANUP_REC("1b", "2b", "3b")
+ :
+ : [ran]"i"(RAN_SHARED), __imm_addr(input), __imm_addr(outer_input),
+ __imm_addr(pads_ran)
+ : __clobber_all);
+}
+
+SEC("syscall")
+int entry_shared(void *ctx)
+{
+ return shared_frame();
+}
+
+/*
+ * 3. A landing pad that reads its frame's callee-saved registers: the callee
+ * overwrites r6-r9 before it throws, so the check passes only if the walker
+ * found the spill in the discarded callee's prologue.
+ */
+#define LOAD_MAGIC_REGS \
+ "r1 = %[magic] ll;" \
+ "r6 = *(u64 *)(r1 + 0);" \
+ "r7 = r6;" \
+ "r7 += 1;" \
+ "r8 = r6;" \
+ "r8 += 2;" \
+ "r9 = r6;" \
+ "r9 += 3;"
+
+/* Set @bit only if r6-r9 still hold what LOAD_MAGIC_REGS put there. */
+#define CHECK_MAGIC_REGS(bit) \
+ "r1 = %[magic] ll;" \
+ "r2 = *(u64 *)(r1 + 0);" \
+ "if r6 != r2 goto 9f;" \
+ "r2 += 1;" \
+ "if r7 != r2 goto 9f;" \
+ "r2 += 1;" \
+ "if r8 != r2 goto 9f;" \
+ "r2 += 1;" \
+ "if r9 != r2 goto 9f;" \
+ PAD_RAN(bit) \
+ "9:"
+
+static __used __naked __noinline __u64 regs_thrower(void)
+{
+ asm volatile (
+ /* Not this frame's to keep, and that is the point. */
+ "r6 = 0xdead;"
+ "r7 = 0xbeef;"
+ "r8 = 0xcafe;"
+ "r9 = 0xf00d;"
+ "r1 = %[cookie];"
+ "call bpf_throw;"
+ "r0 = 0;"
+ "exit;"
+ :
+ : [cookie]"i"(THROW_COOKIE)
+ : __clobber_all);
+}
+
+static __used __naked __noinline __u64 regs_frame(void)
+{
+ asm volatile (
+ LOAD_MAGIC_REGS
+ "call bpf_preempt_disable;"
+"1:" "call regs_thrower;" /* cleanup region */
+"2:"
+ "call bpf_preempt_enable;"
+ "r0 = 0;"
+ "exit;"
+"3:" /* landing pad */
+ "call bpf_preempt_enable;"
+ CHECK_MAGIC_REGS("%[ran]")
+ "call bpf_unwind_resume;"
+ "exit;"
+ CLEANUP_REC("1b", "2b", "3b")
+ :
+ : [cookie]"i"(THROW_COOKIE), [ran]"i"(RAN_REGS),
+ __imm_addr(magic), __imm_addr(pads_ran)
+ : __clobber_all);
+}
+
+SEC("syscall")
+int entry_regs(void *ctx)
+{
+ return regs_frame();
+}
+
+/*
+ * 4. The same, with a tail-call-reachable callee: its prologue pushes the tail
+ * call counter, which moves the spill the walker reads. The array is left
+ * empty; being reachable is the point.
+ */
+struct {
+ __uint(type, BPF_MAP_TYPE_PROG_ARRAY);
+ __uint(max_entries, 1);
+ __uint(key_size, sizeof(__u32));
+ __uint(value_size, sizeof(__u32));
+} jmp_table SEC(".maps");
+
+static __used __noinline __u64 tc_thrower(void *ctx)
+{
+ /*
+ * Never taken; its presence is what makes this frame, whose spill the
+ * walker reads, tail-call-reachable.
+ */
+ bpf_tail_call_static(ctx, &jmp_table, 0);
+ asm volatile (
+ "r6 = 0xdead;"
+ "r7 = 0xbeef;"
+ "r8 = 0xcafe;"
+ "r9 = 0xf00d;"
+ "r1 = %[cookie];"
+ "call bpf_throw;"
+ :
+ : [cookie]"i"(THROW_COOKIE)
+ : __clobber_all);
+ return 0;
+}
+
+/*
+ * The frame with the pad is the program itself, and __naked: r1 at entry is
+ * the only place to get a context for bpf_tail_call().
+ */
+SEC("syscall")
+__naked int entry_tail_call(void)
+{
+ asm volatile (
+ "*(u64 *)(r10 - 8) = r1;" /* the context, straight from entry */
+ LOAD_MAGIC_REGS
+ "r1 = %[input] ll;"
+ "r1 = *(u64 *)(r1 + 0);"
+ "if r1 < 101 goto 8f;"
+ "r1 = *(u64 *)(r10 - 8);"
+"1:" "call tc_thrower;" /* cleanup region */
+"2:"
+"8:"
+ "r0 = 0;"
+ "exit;"
+"3:" /* landing pad */
+ CHECK_MAGIC_REGS("%[ran]")
+ "call bpf_unwind_resume;"
+ "exit;"
+ CLEANUP_REC("1b", "2b", "3b")
+ :
+ : [ran]"i"(RAN_TAIL_CALL), __imm_addr(input),
+ __imm_addr(magic), __imm_addr(pads_ran)
+ : __clobber_all);
+}
+
+/*
+ * 5. A landing pad in the main program's own frame. jit_subprogs() compiles it
+ * as func[0], but the walker finds the outer bpf_prog's ksym, so the table has
+ * to be handed over or the pad is never dispatched.
+ */
+SEC("syscall")
+__naked int entry_main_pad(void)
+{
+ asm volatile (
+ LOAD_MAGIC_REGS
+ "r1 = %[input] ll;"
+ "r1 = *(u64 *)(r1 + 0);"
+ "if r1 < 101 goto 8f;"
+"1:" "call regs_thrower;" /* cleanup region */
+"2:"
+"8:"
+ "r0 = 0;"
+ "exit;"
+"3:" /* landing pad */
+ CHECK_MAGIC_REGS("%[ran]")
+ "call bpf_unwind_resume;"
+ "exit;"
+ CLEANUP_REC("1b", "2b", "3b")
+ :
+ : [ran]"i"(RAN_MAIN_PAD), __imm_addr(input),
+ __imm_addr(magic), __imm_addr(pads_ran)
+ : __clobber_all);
+}
+
+/*
+ * 6. A tail call that is really taken: the target is a program in its own
+ * right, so the walk ends there and this frame's pad does not run. The callee
+ * can also throw, which keeps the pad out of the sweep.
+ */
+struct {
+ __uint(type, BPF_MAP_TYPE_PROG_ARRAY);
+ __uint(max_entries, 1);
+ __uint(key_size, sizeof(__u32));
+ __uint(value_size, sizeof(__u32));
+} taken_table SEC(".maps");
+
+SEC("syscall")
+int tc_target(void *ctx)
+{
+ bpf_throw(THROW_COOKIE);
+ return 0;
+}
+
+static __used __noinline __u64 tc_taken_callee(void *ctx, __u64 x)
+{
+ /*
+ * Never true at run time; the verifier cannot know that, and its
+ * unwind out of here is what keeps the caller's pad alive.
+ */
+ if (x == 7)
+ bpf_throw(THROW_COOKIE);
+ bpf_tail_call_static(ctx, &taken_table, 0);
+ return 0;
+}
+
+SEC("syscall")
+__naked int entry_tail_taken(void)
+{
+ asm volatile (
+ "*(u64 *)(r10 - 8) = r1;" /* the context, straight from entry */
+ "r1 = %[input] ll;"
+ /*
+ * Unnarrowed, the way entry_freplace hands fr_callee its argument: a
+ * guard here would prune the callee's throw and let the sweep take the
+ * pad, leaving no record for a walk past the boundary to match.
+ */
+ "r2 = *(u64 *)(r1 + 0);"
+ "r1 = *(u64 *)(r10 - 8);"
+"1:" "call tc_taken_callee;" /* cleanup region */
+"2:"
+ "exit;" /* the cookie, delivered at tc_target */
+"3:" /* landing pad: must not run */
+ PAD_RAN("%[ran]")
+ "call bpf_unwind_resume;"
+ "exit;"
+ CLEANUP_REC("1b", "2b", "3b")
+ :
+ : [ran]"i"(RAN_TC_TAKEN), __imm_addr(input), __imm_addr(pads_ran)
+ : __clobber_all);
+}
+
+/*
+ * 7. An extension program over the callee of a covered call. The walk ends in
+ * the extension's frame, as it does for a tail call target, so the pad does
+ * not run; fr_callee() can also throw by itself.
+ */
+__noinline __u64 fr_callee(__u64 x)
+{
+ if (x == 7)
+ bpf_throw(THROW_COOKIE);
+ return x + 1;
+}
+
+SEC("syscall")
+__naked int entry_freplace(void)
+{
+ asm volatile (
+ "r1 = %[input] ll;"
+ "r1 = *(u64 *)(r1 + 0);"
+"1:" "call fr_callee;" /* cleanup region */
+"2:"
+ "exit;"
+"3:" /* landing pad */
+ PAD_RAN("%[ran]")
+ "call bpf_unwind_resume;"
+ "exit;"
+ CLEANUP_REC("1b", "2b", "3b")
+ :
+ : [ran]"i"(RAN_FREPLACE), __imm_addr(input), __imm_addr(pads_ran)
+ : __clobber_all);
+}
+
+/* 8. A throwing subprogram named by a BPF_PSEUDO_FUNC on a path never taken. */
+static __used __noinline int cb_thrower(__u32 idx, void *ctx)
+{
+ bpf_throw(THROW_COOKIE);
+ return 0;
+}
+
+static __used __noinline __u64 addr_taken_callee(__u64 x)
+{
+ if (x <= 100)
+ return x + 1;
+ bpf_throw(THROW_COOKIE);
+ return bpf_loop(1, cb_thrower, NULL, 0);
+}
+
+SEC("syscall")
+__naked int entry_addr_taken(void)
+{
+ asm volatile (
+ "r1 = %[input] ll;"
+ "r1 = *(u64 *)(r1 + 0);"
+"1:" "call addr_taken_callee;" /* cleanup region */
+"2:"
+ "exit;"
+"3:" /* landing pad */
+ PAD_RAN("%[ran]")
+ "call bpf_unwind_resume;"
+ "exit;"
+ CLEANUP_REC("1b", "2b", "3b")
+ :
+ : [ran]"i"(RAN_ADDR_TAKEN), __imm_addr(input), __imm_addr(pads_ran)
+ : __clobber_all);
+}
+
+/*
+ * 9. A record that covers bpf_throw() itself: the throwing frame is both the
+ * frame the record covers and the boundary, so the pad runs on the way to
+ * delivering the cookie.
+ */
+SEC("syscall")
+__naked int entry_no_subprog(void)
+{
+ asm volatile (
+ "r1 = %[input] ll;"
+ "r1 = *(u64 *)(r1 + 0);"
+ "if r1 < 101 goto 8f;"
+ "call bpf_preempt_disable;"
+ "r1 = %[cookie];"
+"1:" "call bpf_throw;" /* cleanup region */
+"2:"
+ "exit;"
+"8:"
+ "r0 = 0;"
+ "exit;"
+"3:" /* landing pad */
+ "call bpf_preempt_enable;"
+ PAD_RAN("%[ran]")
+ "call bpf_unwind_resume;"
+ "exit;"
+ CLEANUP_REC("1b", "2b", "3b")
+ :
+ : [cookie]"i"(THROW_COOKIE), [ran]"i"(RAN_NO_SUBPROG),
+ __imm_addr(input), __imm_addr(pads_ran)
+ : __clobber_all);
+}
+
+/*
+ * 10. A landing pad that calls a subprogram an extension can replace. The
+ * load-time rule cannot see it coming, so what stops a nested exception is the
+ * walk ending in the extension's frame; pad_runs says the pad ran once.
+ */
+__noinline __u64 pad_callee(__u64 x)
+{
+ return x + 1;
+}
+
+static __used __noinline __u64 pc_thrower(__u64 x)
+{
+ if (x > 100)
+ bpf_throw(THROW_COOKIE);
+ return x + 1;
+}
+
+static __used __naked __noinline __u64 pad_calls_frame(void)
+{
+ asm volatile (
+ "r1 = %[input] ll;"
+ "r1 = *(u64 *)(r1 + 0);"
+"1:" "call pc_thrower;" /* cleanup region */
+"2:"
+ "exit;"
+"3:" /* landing pad */
+ "r6 = r0;"
+ "r1 = 1;"
+ "call pad_callee;" /* an extension can stand in here */
+ PAD_COUNT
+ PAD_RAN("%[ran]")
+ "r1 = r6;"
+ "call bpf_unwind_resume;"
+ "exit;"
+ CLEANUP_REC("1b", "2b", "3b")
+ :
+ : [ran]"i"(RAN_PAD_CALLS), __imm_addr(input), __imm_addr(pads_ran),
+ __imm_addr(pad_runs)
+ : __clobber_all);
+}
+
+SEC("syscall")
+int entry_pad_calls(void *ctx)
+{
+ return pad_calls_frame();
+}
+
+/*
+ * 11. A covered bpf_throw() the sweep leaves last, where the exception
+ * callback patchlet -- the one that does not keep the call it replaced in the
+ * last slot -- has to carry the marks with it; r6-r9 reports a lost one.
+ */
+SEC("syscall")
+__naked int entry_pad_first(void)
+{
+ asm volatile (
+ LOAD_MAGIC_REGS
+ "r1 = %[input] ll;"
+ "r1 = *(u64 *)(r1 + 0);"
+ "if r1 < 101 goto 7f;"
+ "goto 4f;"
+"3:" /* landing pad, ahead of the call */
+ CHECK_MAGIC_REGS("%[ran]")
+ "call bpf_unwind_resume;"
+ "exit;"
+"7:"
+ "r0 = 0;"
+ "exit;"
+"4:"
+ "r1 = %[cookie];"
+"1:" "call bpf_throw;" /* cleanup region */
+"2:"
+ "exit;" /* dead: swept, leaving the call last */
+ CLEANUP_REC("1b", "2b", "3b")
+ :
+ : [cookie]"i"(THROW_COOKIE), [ran]"i"(RAN_PAD_FIRST),
+ __imm_addr(input), __imm_addr(magic), __imm_addr(pads_ran)
+ : __clobber_all);
+}
+
+/*
+ * 12. A cleanup region whose last instruction is a 16-byte one, so end - 1
+ * names the half that is not an instruction of its own. Well formed, and a
+ * rule against it would turn it away.
+ */
+static __used __naked __noinline __u64 wide_rec_frame(void)
+{
+ asm volatile (
+ "r1 = %[input] ll;"
+ "r6 = *(u64 *)(r1 + 0);"
+ "call bpf_rcu_read_lock;"
+ "r1 = r6;"
+"1:" "call shared_callee;" /* cleanup region begins */
+ "r1 = %[magic] ll;" /* ... and ends on this pair */
+"2:"
+ "r6 = r0;"
+ "call bpf_rcu_read_unlock;"
+ "r0 = r6;"
+ "exit;"
+"3:" /* landing pad */
+ "r7 = r0;"
+ "call bpf_rcu_read_unlock;"
+ PAD_RAN("%[ran]")
+ "r1 = r7;"
+ "call bpf_unwind_resume;"
+ "exit;"
+ CLEANUP_REC("1b", "2b", "3b")
+ :
+ : [ran]"i"(RAN_WIDE_REC), __imm_addr(input), __imm_addr(magic),
+ __imm_addr(pads_ran)
+ : __clobber_all);
+}
+
+SEC("syscall")
+int entry_wide_rec(void *ctx)
+{
+ return wide_rec_frame();
+}
+
+/*
+ * 13. A pad that reloads from and stores to its own frame -- the shape every
+ * compiler-generated pad has. A JIT that addresses the frame through the stack
+ * pointer, arm64, has to find a pad's frame another way.
+ */
+static __used __naked __noinline __u64 pad_stack_frame(void)
+{
+ asm volatile (
+ "r1 = %[magic] ll;"
+ "r1 = *(u64 *)(r1 + 0);"
+ "*(u64 *)(r10 - 8) = r1;" /* what the pad will want */
+ "r1 = %[input] ll;"
+ "r1 = *(u64 *)(r1 + 0);"
+"1:" "call pc_thrower;" /* cleanup region */
+"2:"
+ "r0 = 0;"
+ "exit;"
+"3:" /* landing pad */
+ "r6 = r0;"
+ "r7 = *(u64 *)(r10 - 8);" /* reload it out of the frame */
+ "*(u64 *)(r10 - 16) = r7;" /* and write the frame while here */
+ "r1 = %[magic] ll;"
+ "r2 = *(u64 *)(r1 + 0);"
+ "if r7 != r2 goto 9f;"
+ "r3 = *(u64 *)(r10 - 16);"
+ "if r3 != r2 goto 9f;"
+ PAD_RAN("%[ran]")
+"9:"
+ "r1 = r6;"
+ "call bpf_unwind_resume;"
+ "exit;"
+ CLEANUP_REC("1b", "2b", "3b")
+ :
+ : [ran]"i"(RAN_PAD_STACK), __imm_addr(input), __imm_addr(magic),
+ __imm_addr(pads_ran)
+ : __clobber_all);
+}
+
+SEC("syscall")
+int entry_pad_stack(void *ctx)
+{
+ return pad_stack_frame();
+}
+
+/*
+ * 14. The same, with a frame in between that has no pad of its own, so the
+ * liveness query for an outer frame has more than one frame to walk.
+ */
+static __used __noinline __u64 deep_mid(__u64 x)
+{
+ return pc_thrower(x) + 1;
+}
+
+static __used __naked __noinline __u64 deep_frame(void)
+{
+ asm volatile (
+ "r1 = %[magic] ll;"
+ "r1 = *(u64 *)(r1 + 0);"
+ "*(u64 *)(r10 - 8) = r1;" /* nothing but the pad reads this */
+ "r1 = %[input] ll;"
+ "r1 = *(u64 *)(r1 + 0);"
+"1:" "call deep_mid;" /* cleanup region */
+"2:"
+ "r0 = 0;"
+ "exit;"
+"3:" /* landing pad */
+ "r6 = r0;"
+ "r7 = *(u64 *)(r10 - 8);"
+ "r1 = %[magic] ll;"
+ "r2 = *(u64 *)(r1 + 0);"
+ "if r7 != r2 goto 9f;"
+ PAD_RAN("%[ran]")
+"9:"
+ "r1 = r6;"
+ "call bpf_unwind_resume;"
+ "exit;"
+ CLEANUP_REC("1b", "2b", "3b")
+ :
+ : [ran]"i"(RAN_DEEP_PAD), __imm_addr(input), __imm_addr(magic),
+ __imm_addr(pads_ran)
+ : __clobber_all);
+}
+
+SEC("syscall")
+int entry_deep_pad(void *ctx)
+{
+ return deep_frame();
+}
+
+/*
+ * 15. A region around a call the kernel knows cannot unwind: no call site is
+ * marked, nothing reaches the pad, and the sweep removes it. The program is
+ * otherwise ordinary and has to load.
+ */
+static __used __naked __noinline __u64 nounwind_rec_frame(void)
+{
+ asm volatile (
+ "call bpf_preempt_disable;"
+"1:" "call bpf_preempt_enable;" /* cleanup region: nounwind */
+"2:"
+ "r0 = 0;"
+ "exit;"
+"3:" /* landing pad, never dispatched */
+ PAD_RAN("%[ran]")
+ "call bpf_unwind_resume;"
+ "exit;"
+ CLEANUP_REC("1b", "2b", "3b")
+ :
+ : [ran]"i"(RAN_NOUNWIND_REC), __imm_addr(pads_ran)
+ : __clobber_all);
+}
+
+SEC("syscall")
+int entry_nounwind_rec(void *ctx)
+{
+ return nounwind_rec_frame();
+}
+
+/*
+ * 16. The name a frontend gives the resume: LLVM emits _Unwind_Resume() and
+ * libbpf maps it onto bpf_unwind_resume(), which every other pad here calls.
+ */
+extern void _Unwind_Resume(void) __ksym;
+
+static __used __noinline void __resume_alias_btf_anchor(void)
+{
+ _Unwind_Resume();
+}
+
+static __used __naked __noinline __u64 resume_alias_frame(void)
+{
+ asm volatile (
+"1:" "call regs_thrower;" /* cleanup region */
+"2:"
+ "r0 = 0;"
+ "exit;"
+"3:" /* landing pad */
+ PAD_RAN("%[ran]")
+ "call _Unwind_Resume;" /* the frontend's name for it */
+ "exit;"
+ CLEANUP_REC("1b", "2b", "3b")
+ :
+ : [ran]"i"(RAN_RESUME_ALIAS), __imm_addr(pads_ran)
+ : __clobber_all);
+}
+
+SEC("syscall")
+int entry_resume_alias(void *ctx)
+{
+ return resume_alias_frame();
+}
+
+/*
+ * 17. A landing pad that calls a subprogram which tail calls. A tail call in
+ * the pad itself is refused, but the callee's prologue really did run, so its
+ * tail call releases exactly that and the target returns into the pad.
+ */
+struct {
+ __uint(type, BPF_MAP_TYPE_PROG_ARRAY);
+ __uint(max_entries, 1);
+ __uint(key_size, sizeof(__u32));
+ __uint(value_size, sizeof(__u32));
+} pad_tc_table SEC(".maps");
+
+__u64 pad_tc_target_ran = 0;
+
+SEC("syscall")
+int pad_tc_target(void *ctx)
+{
+ pad_tc_target_ran += 1;
+ return 0;
+}
+
+static __used __noinline __u64 pad_tc_callee(void *ctx)
+{
+ /* Taken only once the test has populated the array. */
+ bpf_tail_call_static(ctx, &pad_tc_table, 0);
+ return 0;
+}
+
+/*
+ * The frame with the pad is the program itself, and __naked: r1 at entry is
+ * the only place to get a context for bpf_tail_call(); the pad reloads it
+ * from the frame's stack.
+ */
+SEC("syscall")
+__naked int entry_pad_tail_call(void)
+{
+ asm volatile (
+ "*(u64 *)(r10 - 8) = r1;" /* the context, straight from entry */
+ LOAD_MAGIC_REGS
+ "r1 = %[input] ll;"
+ "r1 = *(u64 *)(r1 + 0);"
+ "if r1 < 101 goto 8f;"
+"1:" "call regs_thrower;" /* cleanup region */
+"2:"
+"8:"
+ "r0 = 0;"
+ "exit;"
+"3:" /* landing pad */
+ PAD_COUNT
+ "r1 = *(u64 *)(r10 - 8);"
+ "call pad_tc_callee;"
+ /* Only if the frame survived the callee's tail call. */
+ CHECK_MAGIC_REGS("%[ran]")
+ "call bpf_unwind_resume;"
+ "exit;"
+ CLEANUP_REC("1b", "2b", "3b")
+ :
+ : [ran]"i"(RAN_PAD_TAIL_CALL), __imm_addr(input),
+ __imm_addr(magic), __imm_addr(pads_ran), __imm_addr(pad_runs)
+ : __clobber_all);
+}
+
+/*
+ * 18. The other target for that same tail call: a program with a table of its
+ * own, throwing inside the outer exception. The tail call made it a boundary,
+ * so the inner walk ends there and the outer pad, cookie and r6-r9 survive.
+ */
+__u64 tc_target_pad_runs = 0;
+__u64 inner_magic = 0xd00d;
+
+static __used __naked __noinline __u64 inner_thrower(void)
+{
+ asm volatile (
+ /* Not this frame's to keep, the same as regs_thrower. */
+ "r6 = 0xf00d;"
+ "r7 = 0xcafe;"
+ "r8 = 0xbeef;"
+ "r9 = 0xdead;"
+ "r1 = %[cookie];"
+ "call bpf_throw;"
+ "r0 = 0;"
+ "exit;"
+ :
+ : [cookie]"i"(INNER_COOKIE)
+ : __clobber_all);
+}
+
+SEC("syscall")
+__naked int pad_tc_throw_target(void)
+{
+ asm volatile (
+ /* Distinct from the outer pad's, so neither can stand in for it. */
+ "r1 = %[inner_magic] ll;"
+ "r6 = *(u64 *)(r1 + 0);"
+ "r7 = r6;"
+ "r7 += 1;"
+ "r8 = r6;"
+ "r8 += 2;"
+ "r9 = r6;"
+ "r9 += 3;"
+ "r1 = %[input] ll;"
+ "r1 = *(u64 *)(r1 + 0);"
+ "if r1 < 101 goto 8f;"
+"1:" "call inner_thrower;" /* cleanup region */
+"2:"
+"8:"
+ "r0 = 0;"
+ "exit;"
+"3:" /* landing pad */
+ /* This frame's own r6-r9, not the outer pad's. */
+ "r1 = %[inner_magic] ll;"
+ "r2 = *(u64 *)(r1 + 0);"
+ "if r6 != r2 goto 9f;"
+ "r2 += 1;"
+ "if r7 != r2 goto 9f;"
+ "r2 += 1;"
+ "if r8 != r2 goto 9f;"
+ "r2 += 1;"
+ "if r9 != r2 goto 9f;"
+ "r1 = %[tc_target_pad_runs] ll;"
+ "r2 = *(u64 *)(r1 + 0);"
+ "r2 += 1;"
+ "*(u64 *)(r1 + 0) = r2;"
+"9:"
+ "call bpf_unwind_resume;"
+ "exit;"
+ CLEANUP_REC("1b", "2b", "3b")
+ :
+ : __imm_addr(input), __imm_addr(inner_magic),
+ __imm_addr(tc_target_pad_runs)
+ : __clobber_all);
+}
+
+/*
+ * 19. A landing pad whose first instruction is a nop. opt_remove_nops() runs
+ * long after the cleanup walk, so the record has to follow the pad to the
+ * instruction that takes its place rather than be dropped with the nop.
+ */
+static __used __naked __noinline __u64 nop_pad_frame(void)
+{
+ asm volatile (
+ "r1 = %[input] ll;"
+ "r6 = *(u64 *)(r1 + 0);"
+ "if r6 < 101 goto 6f;"
+ "r1 = %[cookie];"
+"1:" "call bpf_throw;" /* cleanup region */
+"2:"
+"6:"
+ "r0 = 0;"
+ "exit;"
+"3:" /* landing pad: a nop, then its body */
+ "goto +0;"
+ PAD_RAN("%[ran]")
+ "call bpf_unwind_resume;"
+ "exit;"
+ CLEANUP_REC("1b", "2b", "3b")
+ :
+ : [cookie]"i"(THROW_COOKIE), [ran]"i"(RAN_NOP_PAD),
+ __imm_addr(input), __imm_addr(pads_ran)
+ : __clobber_all);
+}
+
+SEC("syscall")
+int entry_nop_pad(void *ctx)
+{
+ return nop_pad_frame();
+}
+
+/*
+ * 20. A landing pad that indexes its own frame's stack by a register the frame
+ * set before the throwing call. The offset is not a constant, so r6 has to be
+ * marked precise from inside the pad, back across the unwind edge.
+ */
+/*
+ * A thrower that touches none of r6-r9, so nothing in it can answer for the
+ * pad frame's r6 and the walk has to leave this frame to look.
+ */
+static __used __naked __noinline __u64 var_thrower(void)
+{
+ asm volatile (
+ "if r1 < 101 goto 1f;"
+ "r1 = %[cookie];"
+ "call bpf_throw;"
+"1:"
+ "r0 = 0;"
+ "exit;"
+ :
+ : [cookie]"i"(THROW_COOKIE)
+ : __clobber_all);
+}
+
+static __used __naked __noinline __u64 var_stack_frame(void)
+{
+ asm volatile (
+ "r1 = %[magic] ll;"
+ "r1 = *(u64 *)(r1 + 0);"
+ "*(u64 *)(r10 - 8) = r1;" /* the slot the pad will read... */
+ "*(u64 *)(r10 - 16) = r1;" /* ...whichever of the two it is */
+ "r1 = %[input] ll;"
+ "r6 = *(u64 *)(r1 + 0);"
+ "r6 &= 1;" /* an unknown slot number... */
+ "r6 <<= 3;" /* ...as an aligned byte offset */
+ "r1 = %[input] ll;"
+ "r1 = *(u64 *)(r1 + 0);"
+"1:" "call var_thrower;" /* cleanup region */
+"2:"
+ "r0 = 0;"
+ "exit;"
+"3:" /* landing pad */
+ "r7 = r0;"
+ "r1 = r10;"
+ "r1 += r6;" /* variable offset into the frame */
+ "r2 = *(u64 *)(r1 - 16);"
+ "r3 = %[magic] ll;"
+ "r3 = *(u64 *)(r3 + 0);"
+ "if r2 != r3 goto 9f;"
+ PAD_RAN("%[ran]")
+"9:"
+ "r1 = r7;"
+ "call bpf_unwind_resume;"
+ "exit;"
+ CLEANUP_REC("1b", "2b", "3b")
+ :
+ : [ran]"i"(RAN_VAR_STACK), __imm_addr(input), __imm_addr(magic),
+ __imm_addr(pads_ran)
+ : __clobber_all);
+}
+
+SEC("syscall")
+int entry_var_stack(void *ctx)
+{
+ return var_stack_frame();
+}
+
+/*
+ * 21. The same walk, over a call to a global subprogram: the verifier enters
+ * no frame for one, so the exception arrives at the pad from the call site
+ * rather than from a throw some frames deeper.
+ */
+__noinline __u64 global_thrower(__u64 x)
+{
+ if (x > 100)
+ bpf_throw(THROW_COOKIE);
+ return x + 1;
+}
+
+static __used __naked __noinline __u64 global_pad_frame(void)
+{
+ asm volatile (
+ "r1 = %[magic] ll;"
+ "r1 = *(u64 *)(r1 + 0);"
+ "*(u64 *)(r10 - 8) = r1;"
+ "*(u64 *)(r10 - 16) = r1;"
+ "r1 = %[input] ll;"
+ "r6 = *(u64 *)(r1 + 0);"
+ "r6 &= 1;"
+ "r6 <<= 3;"
+ "r1 = %[input] ll;"
+ "r1 = *(u64 *)(r1 + 0);"
+"1:" "call global_thrower;" /* cleanup region */
+"2:"
+ "r0 = 0;"
+ "exit;"
+"3:" /* landing pad */
+ "r7 = r0;"
+ "r1 = r10;"
+ "r1 += r6;"
+ "r2 = *(u64 *)(r1 - 16);"
+ "r3 = %[magic] ll;"
+ "r3 = *(u64 *)(r3 + 0);"
+ "if r2 != r3 goto 9f;"
+ PAD_RAN("%[ran]")
+"9:"
+ "r1 = r7;"
+ "call bpf_unwind_resume;"
+ "exit;"
+ CLEANUP_REC("1b", "2b", "3b")
+ :
+ : [ran]"i"(RAN_GLOBAL_PAD), __imm_addr(input), __imm_addr(magic),
+ __imm_addr(pads_ran)
+ : __clobber_all);
+}
+
+SEC("syscall")
+int entry_global_pad(void *ctx)
+{
+ return global_pad_frame();
+}
+
+/*
+ * 22. A landing pad that uses r0 as a variable stack offset. No instruction
+ * in the frame writes r0 -- the walker leaves it on the way in -- so the
+ * precision that access asks for has to end at the unwind edge, not cross it.
+ */
+static __used __naked __noinline __u64 pad_r0_frame(void)
+{
+ asm volatile (
+ "r1 = %[magic] ll;"
+ "r1 = *(u64 *)(r1 + 0);"
+ "*(u64 *)(r10 - 8) = r1;" /* the slot the pad will read... */
+ "*(u64 *)(r10 - 16) = r1;" /* ...whichever of the two it is */
+"1:" "call regs_thrower;" /* cleanup region */
+"2:"
+ "r0 = 0;"
+ "exit;"
+"3:" /* landing pad */
+ "r0 &= 1;" /* an unknown slot number... */
+ "r0 <<= 3;" /* ...as an aligned byte offset */
+ "r1 = r10;"
+ "r1 += r0;" /* variable offset into the frame */
+ "r2 = *(u64 *)(r1 - 16);"
+ "r3 = %[magic] ll;"
+ "r3 = *(u64 *)(r3 + 0);"
+ "if r2 != r3 goto 9f;"
+ PAD_RAN("%[ran]")
+"9:"
+ "call bpf_unwind_resume;"
+ "exit;"
+ CLEANUP_REC("1b", "2b", "3b")
+ :
+ : [ran]"i"(RAN_PAD_R0), __imm_addr(magic), __imm_addr(pads_ran)
+ : __clobber_all);
+}
+
+SEC("syscall")
+int entry_pad_r0(void *ctx)
+{
+ return pad_r0_frame();
+}
+
+char _license[] SEC("license") = "GPL";
--
2.53.0-Meta
^ permalink raw reply related [flat|nested] 49+ messages in thread* Re: [PATCH bpf-next v5 20/21] selftests/bpf: Cover the exception cleanup shapes the chain does not reach
2026-09-23 5:00 ` [PATCH bpf-next v5 20/21] selftests/bpf: Cover the exception cleanup shapes the chain does not reach Yonghong Song
@ 2026-09-23 5:51 ` bot+bpf-ci
2026-09-23 7:50 ` Eduard Zingerman
1 sibling, 0 replies; 49+ messages in thread
From: bot+bpf-ci @ 2026-09-23 5:51 UTC (permalink / raw)
To: yonghong.song, bpf
Cc: ast, andrii, daniel, eddyz87, kernel-team, ast, andrii, daniel,
martin.lau, eddyz87, yonghong.song, mason, ihor.solodrai
[-- Attachment #1: Type: text/plain, Size: 7138 bytes --]
> diff --git a/tools/testing/selftests/bpf/prog_tests/exceptions_cleanup.c b/tools/testing/selftests/bpf/prog_tests/exceptions_cleanup.c
> index d9823023ec04..dbdb78b791e0 100644
> --- a/tools/testing/selftests/bpf/prog_tests/exceptions_cleanup.c
> +++ b/tools/testing/selftests/bpf/prog_tests/exceptions_cleanup.c
[ ... ]
> +static void run_shape(struct exceptions_cleanup_shapes *skel, struct bpf_program *prog,
> + __u64 input, __u32 retval, __u64 pads)
> +{
> + __u64 ctx = 0;
> + int err;
> +
> + LIBBPF_OPTS(bpf_test_run_opts, topts,
> + .ctx_in = &ctx,
> + .ctx_size_in = sizeof(ctx),
> + );
> +
> + skel->bss->input = input;
> + skel->bss->pads_ran = 0;
> +
> + err = bpf_prog_test_run_opts(bpf_program__fd(prog), &topts);
> + if (!ASSERT_OK(err, "run"))
> + return;
> + ASSERT_EQ(topts.retval, retval, "retval");
> + ASSERT_EQ(skel->bss->pads_ran, pads, "pads_ran");
> +}
> +
> +static void test_freplace(struct exceptions_cleanup_shapes *skel)
> +{
> + struct exceptions_cleanup_freplace *fr;
> + struct bpf_link *link;
> + int tgt_fd;
> +
> + tgt_fd = bpf_program__fd(skel->progs.entry_freplace);
> +
> + fr = exceptions_cleanup_freplace__open();
> + if (!ASSERT_OK_PTR(fr, "freplace open"))
> + return;
> +
> + if (!ASSERT_OK(bpf_program__set_attach_target(fr->progs.new_fr_callee,
> + tgt_fd, "fr_callee"),
> + "set_attach_target"))
> + goto out;
> + if (!ASSERT_OK(exceptions_cleanup_freplace__load(fr), "freplace load"))
> + goto out;
> +
> + link = bpf_program__attach_freplace(fr->progs.new_fr_callee, tgt_fd,
> + "fr_callee");
> + if (!ASSERT_OK_PTR(link, "attach_freplace"))
> + goto out;
> +
> + run_shape(skel, skel->progs.entry_freplace, 101, THROW_COOKIE, 0);
> + bpf_link__destroy(link);
> +out:
> + exceptions_cleanup_freplace__destroy(fr);
> +}
> +
> +static void test_pad_calls_freplace(struct exceptions_cleanup_shapes *skel)
> +{
> + struct exceptions_cleanup_pad_freplace *fr;
> + struct bpf_link *link;
> + __u64 ctx = 0;
> + int tgt_fd, err;
> +
> + LIBBPF_OPTS(bpf_test_run_opts, topts,
> + .ctx_in = &ctx,
> + .ctx_size_in = sizeof(ctx),
> + );
> +
> + tgt_fd = bpf_program__fd(skel->progs.entry_pad_calls);
> +
> + fr = exceptions_cleanup_pad_freplace__open();
> + if (!ASSERT_OK_PTR(fr, "pad freplace open"))
> + return;
> +
> + if (!ASSERT_OK(bpf_program__set_attach_target(fr->progs.new_pad_callee,
> + tgt_fd, "pad_callee"),
> + "set_attach_target"))
> + goto out;
> + if (!ASSERT_OK(exceptions_cleanup_pad_freplace__load(fr), "pad freplace load"))
> + goto out;
> +
> + link = bpf_program__attach_freplace(fr->progs.new_pad_callee, tgt_fd,
> + "pad_callee");
> + if (!ASSERT_OK_PTR(link, "attach_freplace"))
> + goto out;
> +
> + skel->bss->input = 101;
> + skel->bss->pads_ran = 0;
> + skel->bss->pad_runs = 0;
> +
> + err = bpf_prog_test_run_opts(tgt_fd, &topts);
> + if (!ASSERT_OK(err, "run"))
> + goto out_link;
> +
> + ASSERT_EQ(skel->bss->pad_runs, 1, "pad_runs");
> + ASSERT_EQ(skel->bss->pads_ran, RAN_PAD_CALLS, "pads_ran");
> + ASSERT_EQ(topts.retval, THROW_COOKIE, "retval");
> +out_link:
> + bpf_link__destroy(link);
> +out:
> + exceptions_cleanup_pad_freplace__destroy(fr);
> +}
This isn't a bug, but test_pad_calls_freplace() repeats the body of
run_shape(): it declares its own ctx and LIBBPF_OPTS(bpf_test_run_opts,
topts, ...), sets input and pads_ran, calls bpf_prog_test_run_opts(),
and asserts retval and pads_ran.
test_ext_table() gets its extra ext_pad_ran assertion by wrapping
run_shape(); could test_pad_calls_freplace() use run_shape() the same
way and keep only the pad_runs reset and assertion around it, instead
of open-coding the run?
[ ... ]
> diff --git a/tools/testing/selftests/bpf/progs/exceptions_cleanup_freplace.c b/tools/testing/selftests/bpf/progs/exceptions_cleanup_freplace.c
> new file mode 100644
> index 000000000000..afb358fd3d40
> --- /dev/null
> +++ b/tools/testing/selftests/bpf/progs/exceptions_cleanup_freplace.c
> @@ -0,0 +1,17 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
> +#include <vmlinux.h>
> +#include <bpf/bpf_helpers.h>
> +#include "exceptions_cleanup.h"
> +
> +/* Without a 32-bit int in BTF, libbpf's dummy_ksym var gets type id 0. */
> +int btf_int_anchor;
> +
> +SEC("freplace/fr_callee")
> +__u64 new_fr_callee(__u64 x)
> +{
> + bpf_throw(THROW_COOKIE);
> + return 0;
> +}
> +
> +char _license[] SEC("license") = "GPL";
> diff --git a/tools/testing/selftests/bpf/progs/exceptions_cleanup_pad_freplace.c b/tools/testing/selftests/bpf/progs/exceptions_cleanup_pad_freplace.c
> new file mode 100644
> index 000000000000..eabac6baabb7
> --- /dev/null
> +++ b/tools/testing/selftests/bpf/progs/exceptions_cleanup_pad_freplace.c
> @@ -0,0 +1,17 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
> +#include <vmlinux.h>
> +#include <bpf/bpf_helpers.h>
> +#include "exceptions_cleanup.h"
> +
> +/* Without a 32-bit int in BTF, libbpf's dummy_ksym var gets type id 0. */
> +int btf_int_anchor;
> +
> +SEC("freplace/pad_callee")
> +__u64 new_pad_callee(__u64 x)
> +{
> + bpf_throw(INNER_COOKIE);
> + return 0;
> +}
> +
> +char _license[] SEC("license") = "GPL";
This isn't a bug, but these two files are nearly identical, differing
only in the SEC() target (pad_callee vs fr_callee), the program name
(new_pad_callee vs new_fr_callee) and the cookie (INNER_COOKIE vs
THROW_COOKIE).
Would the SEC("?freplace") + bpf_program__set_autoload() idiom that
exceptions_ext.c uses let both replacements (and one copy of the
btf_int_anchor note) live in a single object?
[ ... ]
> diff --git a/tools/testing/selftests/bpf/progs/exceptions_cleanup_shapes.c b/tools/testing/selftests/bpf/progs/exceptions_cleanup_shapes.c
> new file mode 100644
> index 000000000000..bea2c8e95db3
> --- /dev/null
> +++ b/tools/testing/selftests/bpf/progs/exceptions_cleanup_shapes.c
[ ... ]
> +static __used __naked __noinline __u64 regs_frame(void)
> +{
> + asm volatile (
> + LOAD_MAGIC_REGS
> + "call bpf_preempt_disable;"
> +"1:" "call regs_thrower;" /* cleanup region */
> +"2:"
> + "call bpf_preempt_enable;"
> + "r0 = 0;"
> + "exit;"
> +"3:" /* landing pad */
> + "call bpf_preempt_enable;"
> + CHECK_MAGIC_REGS("%[ran]")
> + "call bpf_unwind_resume;"
> + "exit;"
> + CLEANUP_REC("1b", "2b", "3b")
> + :
> + : [cookie]"i"(THROW_COOKIE), [ran]"i"(RAN_REGS),
> + __imm_addr(magic), __imm_addr(pads_ran)
> + : __clobber_all);
> +}
This isn't a bug, but regs_frame()'s asm operand list declares
[cookie]"i"(THROW_COOKIE), yet the template never references %[cookie].
The cookie is supplied by regs_thrower() one frame down, which has its
own [cookie] operand.
Is the [cookie] operand in regs_frame() still needed now that
regs_thrower() supplies the cookie, or is it a leftover from the split?
---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md
CI run summary: https://github.com/kernel-patches/bpf/actions/runs/35821138073
^ permalink raw reply [flat|nested] 49+ messages in thread* Re: [PATCH bpf-next v5 20/21] selftests/bpf: Cover the exception cleanup shapes the chain does not reach
2026-09-23 5:00 ` [PATCH bpf-next v5 20/21] selftests/bpf: Cover the exception cleanup shapes the chain does not reach Yonghong Song
2026-09-23 5:51 ` bot+bpf-ci
@ 2026-09-23 7:50 ` Eduard Zingerman
1 sibling, 0 replies; 49+ messages in thread
From: Eduard Zingerman @ 2026-09-23 7:50 UTC (permalink / raw)
To: Yonghong Song, bpf
Cc: Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann, kernel-team
On Tue, 2026-09-22 at 22:00 -0700, Yonghong Song wrote:
...
> +static void test_shapes(void)
> +{
Maybe consider adding an annotation __retbss(var, value)?
Similar to __retval(), but checking the value of a variable in .bss
after the test execution?
Similar to how veristat does it, it can:
- look for a map with a name ".bss" and enumerate
- look for a btf_is_datasec() with a name ".bss"
- iterate over variables in the section and look
for one with a name specified in the annotation
- read the value if it is e.g. u32
(we can limit support by u32 or u64 at the moment).
This would remove the necessity for test_shapes(),
as generic RUN_TESTS() would suffice.
But also, Yonghong, this series adds 2.5K lines of tests,
are you sure all these tests are necessary?
Is there a more compact way to encode these?
> + struct exceptions_cleanup_shapes *skel;
> +
> + skel = exceptions_cleanup_shapes__open_and_load();
> + if (!ASSERT_OK_PTR(skel, "shapes open_and_load"))
> + return;
> +
> + /* The frame loads at all only if everything unreachable in it went. */
> + if (test__start_subtest("sweep_no_throw"))
> + run_shape(skel, skel->progs.entry_sweep, 1, 0, 0);
> + if (test__start_subtest("sweep_throw"))
> + run_shape(skel, skel->progs.entry_sweep, 101, THROW_COOKIE, RAN_SWEEP);
> +
> + /* The covered call unwinds to the pad; the uncovered one walks past. */
> + if (test__start_subtest("shared_callee_no_throw")) {
> + skel->bss->outer_input = 0;
> + run_shape(skel, skel->progs.entry_shared, 1, 2, 0);
> + }
> + if (test__start_subtest("shared_callee_throw")) {
> + skel->bss->outer_input = 0;
> + run_shape(skel, skel->progs.entry_shared, 101, THROW_COOKIE, RAN_SHARED);
> + }
> + if (test__start_subtest("shared_callee_uncovered_throw")) {
> + skel->bss->outer_input = 101;
> + run_shape(skel, skel->progs.entry_shared, 1, THROW_COOKIE, 0);
> + skel->bss->outer_input = 0;
> + }
> +
> + /* The pad only sets its bit if it got the frame's own r6-r9 back. */
> + if (test__start_subtest("pad_sees_callee_saved"))
> + run_shape(skel, skel->progs.entry_regs, 101, THROW_COOKIE, RAN_REGS);
> +
> + /* Same check, with a tail-call-reachable callee: its spill moves. */
> + if (test__start_subtest("tail_call_no_throw"))
> + run_shape(skel, skel->progs.entry_tail_call, 1, 0, 0);
> + if (test__start_subtest("tail_call_throw"))
> + run_shape(skel, skel->progs.entry_tail_call, 101, THROW_COOKIE,
> + RAN_TAIL_CALL);
> +
> + /* A region around a nounwind call: no pad dispatched, still loads. */
> + if (test__start_subtest("nounwind_region"))
> + run_shape(skel, skel->progs.entry_nounwind_rec, 1, 0, 0);
> +
> + /* A pad in the main program's own frame, not in a subprogram. */
> + if (test__start_subtest("main_program_pad"))
> + run_shape(skel, skel->progs.entry_main_pad, 101, THROW_COOKIE,
> + RAN_MAIN_PAD);
> +
> + /*
> + * The same call site either way: the subprogram's throw unwinds into
> + * this frame and runs its pad, an extension's stops at its own boundary.
> + */
> + if (test__start_subtest("freplace_subprog_throws"))
> + run_shape(skel, skel->progs.entry_freplace, 7, THROW_COOKIE,
> + RAN_FREPLACE);
> + if (test__start_subtest("freplace_extension_throws"))
> + test_freplace(skel);
> +
> + /*
> + * A tail call that is taken: the walk ends at the target, so the cookie
> + * comes back from there and this frame's pad does not run -- though it
> + * is reachable, so a walk past the boundary would find it.
> + */
> + if (test__start_subtest("tail_call_taken")) {
> + int key = 0, prog_fd = bpf_program__fd(skel->progs.tc_target);
> +
> + if (ASSERT_OK(bpf_map_update_elem(bpf_map__fd(skel->maps.taken_table),
> + &key, &prog_fd, BPF_ANY),
> + "populate taken_table"))
> + run_shape(skel, skel->progs.entry_tail_taken, 101,
> + THROW_COOKIE, 0);
> + }
> +
> + /*
> + * A throwing subprog named by a BPF_PSEUDO_FUNC and handed to a
> + * bpf_loop() the verifier never reaches: the callback check has to fire
> + * on the helper call, not on the ld_imm64.
> + */
> + if (test__start_subtest("addr_taken_no_throw"))
> + run_shape(skel, skel->progs.entry_addr_taken, 1, 2, 0);
> + if (test__start_subtest("addr_taken_throw"))
> + run_shape(skel, skel->progs.entry_addr_taken, 101, THROW_COOKIE,
> + RAN_ADDR_TAKEN);
> +
> + /*
> + * A record covering bpf_throw() itself rather than a call to a frame
> + * that throws: raised, caught up with and delivered in one frame.
> + */
> + if (test__start_subtest("no_subprog_no_throw"))
> + run_shape(skel, skel->progs.entry_no_subprog, 1, 0, 0);
> + if (test__start_subtest("no_subprog_throw"))
> + run_shape(skel, skel->progs.entry_no_subprog, 101, THROW_COOKIE,
> + RAN_NO_SUBPROG);
> +
> + /*
> + * A pad that calls a subprogram; with a throwing extension in its place,
> + * the nested exception has to stop there, not restart this pad.
> + */
> + if (test__start_subtest("pad_calls_subprog")) {
> + skel->bss->pad_runs = 0;
> + run_shape(skel, skel->progs.entry_pad_calls, 101, THROW_COOKIE,
> + RAN_PAD_CALLS);
> + ASSERT_EQ(skel->bss->pad_runs, 1, "pad_runs");
> + }
> + if (test__start_subtest("pad_calls_throwing_extension"))
> + test_pad_calls_freplace(skel);
> +
> + /*
> + * A covered throw the sweep leaves last, where the default exception
> + * callback is patched in; the pad's bit needs r6-r9 still spilled.
> + */
> + if (test__start_subtest("pad_before_throw"))
> + run_shape(skel, skel->progs.entry_pad_first, 101, THROW_COOKIE,
> + RAN_PAD_FIRST);
> +
> + /*
> + * A region whose last instruction is a 16-byte one, so that end - 1
> + * names the half of it that is not an instruction.
> + */
> + if (test__start_subtest("region_ends_on_ldimm64"))
> + run_shape(skel, skel->progs.entry_wide_rec, 101, THROW_COOKIE,
> + RAN_WIDE_REC);
> +
> + /*
> + * A pad that reloads from and writes to its own frame's stack, which a
> + * JIT addressing the frame through the stack pointer gets wrong.
> + */
> + if (test__start_subtest("pad_uses_own_frame"))
> + run_shape(skel, skel->progs.entry_pad_stack, 101, THROW_COOKIE,
> + RAN_PAD_STACK);
> +
> + /* The same, with an uncovered frame between the throw and the pad. */
> + if (test__start_subtest("pad_two_frames_up"))
> + run_shape(skel, skel->progs.entry_deep_pad, 101, THROW_COOKIE,
> + RAN_DEEP_PAD);
> +
> + /* An extension program with a cleanup table of its own. */
> + if (test__start_subtest("extension_carries_table"))
> + test_ext_table(skel);
> +
> + /*
> + * A pad terminated by _Unwind_Resume, which libbpf maps onto the kfunc;
> + * every other program here calls bpf_unwind_resume directly.
> + */
> + if (test__start_subtest("resume_alias"))
> + run_shape(skel, skel->progs.entry_resume_alias, 101,
> + THROW_COOKIE, RAN_RESUME_ALIAS);
> +
> + /*
> + * A pad that calls a subprogram which tail calls, array empty and then
> + * populated: the tail call releases only the callee's own prologue.
> + */
> + if (test__start_subtest("pad_callee_tail_call")) {
> + int key = 0, prog_fd = bpf_program__fd(skel->progs.pad_tc_target);
> +
> + skel->bss->pad_tc_target_ran = 0;
> + skel->bss->pad_runs = 0;
> + run_shape(skel, skel->progs.entry_pad_tail_call, 101,
> + THROW_COOKIE, RAN_PAD_TAIL_CALL);
> + ASSERT_EQ(skel->bss->pad_tc_target_ran, 0, "target not run");
> + ASSERT_EQ(skel->bss->pad_runs, 1, "pad_runs");
> +
> + if (ASSERT_OK(bpf_map_update_elem(bpf_map__fd(skel->maps.pad_tc_table),
> + &key, &prog_fd, BPF_ANY),
> + "populate pad_tc_table")) {
> + skel->bss->pad_runs = 0;
> + run_shape(skel, skel->progs.entry_pad_tail_call, 101,
> + THROW_COOKIE, RAN_PAD_TAIL_CALL);
> + ASSERT_EQ(skel->bss->pad_tc_target_ran, 1, "target ran");
> + ASSERT_EQ(skel->bss->pad_runs, 1, "pad_runs");
> + }
> + }
> +
> + /*
> + * The same, into a target that carries a table and throws: that target
> + * is a boundary, so the outer pad runs once, not twice.
> + */
> + if (test__start_subtest("pad_callee_tail_call_throws")) {
> + int key = 0, prog_fd = bpf_program__fd(skel->progs.pad_tc_throw_target);
> +
> + if (ASSERT_OK(bpf_map_update_elem(bpf_map__fd(skel->maps.pad_tc_table),
> + &key, &prog_fd, BPF_ANY),
> + "populate pad_tc_table")) {
> + skel->bss->pad_runs = 0;
> + skel->bss->tc_target_pad_runs = 0;
> + run_shape(skel, skel->progs.entry_pad_tail_call, 101,
> + THROW_COOKIE, RAN_PAD_TAIL_CALL);
> + /* The target cleaned up after itself, once. */
> + ASSERT_EQ(skel->bss->tc_target_pad_runs, 1,
> + "tc_target_pad_runs");
> + /* And the outer pad was not started over. */
> + ASSERT_EQ(skel->bss->pad_runs, 1, "pad_runs");
> + }
> + }
> +
> + /*
> + * A pad whose first instruction opt_remove_nops() deletes: the record
> + * has to follow the pad rather than be dropped with the nop.
> + */
> + if (test__start_subtest("nop_at_pad_head"))
> + run_shape(skel, skel->progs.entry_nop_pad, 101, THROW_COOKIE,
> + RAN_NOP_PAD);
> +
> + /*
> + * A pad that indexes its frame's stack by a register the frame set
> + * before the throwing call, marked precise back across the unwind edge.
> + */
> + if (test__start_subtest("pad_var_stack_offset"))
> + run_shape(skel, skel->progs.entry_var_stack, 101, THROW_COOKIE,
> + RAN_VAR_STACK);
> +
> + /*
> + * The same, where the throw is in a global subprogram, which the
> + * verifier walks without a frame of its own.
> + */
> + if (test__start_subtest("pad_over_global_subprog"))
> + run_shape(skel, skel->progs.entry_global_pad, 101, THROW_COOKIE,
> + RAN_GLOBAL_PAD);
> +
> + /*
> + * A pad that indexes its frame by r0, which no insn in the frame
> + * wrote: the precision request for it must not cross the unwind edge.
> + */
> + if (test__start_subtest("pad_r0_precision"))
> + run_shape(skel, skel->progs.entry_pad_r0, 101, THROW_COOKIE,
> + RAN_PAD_R0);
> +
> + exceptions_cleanup_shapes__destroy(skel);
> +}
> +
...
^ permalink raw reply [flat|nested] 49+ messages in thread
* [PATCH bpf-next v5 21/21] selftests/bpf: Load an exception cleanup program from a light skeleton
2026-09-23 4:58 [PATCH bpf-next v5 00/21] bpf: Run exception cleanup landing pads when bpf_throw() unwinds Yonghong Song
` (19 preceding siblings ...)
2026-09-23 5:00 ` [PATCH bpf-next v5 20/21] selftests/bpf: Cover the exception cleanup shapes the chain does not reach Yonghong Song
@ 2026-09-23 5:00 ` Yonghong Song
20 siblings, 0 replies; 49+ messages in thread
From: Yonghong Song @ 2026-09-23 5:00 UTC (permalink / raw)
To: bpf
Cc: Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann,
Eduard Zingerman, kernel-team
For a light skeleton, libbpf hands the records to bpf_gen__prog_load(),
which writes them into a blob and emits a loader program that issues
BPF_PROG_LOAD from inside the kernel. Nothing about that is shared with the
ordinary path: the attr is built field by field, and the kfunc names are
resolved by the loader program when it runs.
exceptions_cleanup_light.c is the smallest program that can tell whether a
table survives that trip: one record covering the bpf_throw() call itself,
one pad that sets a bit and resumes. If the table arrives, the pad runs and
the cookie comes back; if it does not, the program does not load at all.
Signed-off-by: Yonghong Song <yonghong.song@linux.dev>
---
tools/testing/selftests/bpf/Makefile.skel | 2 +-
.../selftests/bpf/exceptions_cleanup.h | 3 ++
.../bpf/prog_tests/exceptions_cleanup.c | 28 +++++++++++++
.../bpf/progs/exceptions_cleanup_light.c | 39 +++++++++++++++++++
4 files changed, 71 insertions(+), 1 deletion(-)
create mode 100644 tools/testing/selftests/bpf/progs/exceptions_cleanup_light.c
diff --git a/tools/testing/selftests/bpf/Makefile.skel b/tools/testing/selftests/bpf/Makefile.skel
index 580d1d82c186..6d7518fa4018 100644
--- a/tools/testing/selftests/bpf/Makefile.skel
+++ b/tools/testing/selftests/bpf/Makefile.skel
@@ -33,7 +33,7 @@ LINKED_SKELS := test_static_linked.skel.h linked_funcs.skel.h \
LSKELS := fexit_sleep.c trace_printk.c trace_vprintk.c map_ptr_kern.c \
core_kern.c core_kern_overflow.c test_ringbuf.c \
test_ringbuf_n.c test_ringbuf_map_key.c test_ringbuf_write.c \
- test_ringbuf_overwrite.c
+ test_ringbuf_overwrite.c exceptions_cleanup_light.c
LSKELS_SIGNED := fentry_test.c fexit_test.c atomics.c
diff --git a/tools/testing/selftests/bpf/exceptions_cleanup.h b/tools/testing/selftests/bpf/exceptions_cleanup.h
index d6f63f147c4a..68f2d61a694b 100644
--- a/tools/testing/selftests/bpf/exceptions_cleanup.h
+++ b/tools/testing/selftests/bpf/exceptions_cleanup.h
@@ -36,6 +36,9 @@
#define RAN_GLOBAL_PAD 0x80000
#define RAN_PAD_R0 0x100000
+/* progs/exceptions_cleanup_light.c: the one pad it has. */
+#define RAN_LIGHT 0x1
+
#define CLEANUP_REC(begin, end, landing_pad) \
".pushsection .bpf_cleanup,\"a\",@progbits;" \
".long " begin ";" \
diff --git a/tools/testing/selftests/bpf/prog_tests/exceptions_cleanup.c b/tools/testing/selftests/bpf/prog_tests/exceptions_cleanup.c
index dbdb78b791e0..323ee8d72c91 100644
--- a/tools/testing/selftests/bpf/prog_tests/exceptions_cleanup.c
+++ b/tools/testing/selftests/bpf/prog_tests/exceptions_cleanup.c
@@ -8,6 +8,7 @@
#include "exceptions_cleanup_freplace.skel.h"
#include "exceptions_cleanup_pad_freplace.skel.h"
#include "exceptions_cleanup_ext_table.skel.h"
+#include "exceptions_cleanup_light.lskel.h"
/* foo3 threw: every frame that has a pad ran it. */
#define PADS_FOO3_THREW \
@@ -171,6 +172,30 @@ static void test_ext_table(struct exceptions_cleanup_shapes *skel)
exceptions_cleanup_ext_table__destroy(fr);
}
+static void test_light_skeleton(void)
+{
+ struct exceptions_cleanup_light_lskel *skel;
+ __u64 ctx = 0;
+ int err;
+
+ LIBBPF_OPTS(bpf_test_run_opts, topts,
+ .ctx_in = &ctx,
+ .ctx_size_in = sizeof(ctx),
+ );
+
+ skel = exceptions_cleanup_light_lskel__open_and_load();
+ if (!ASSERT_OK_PTR(skel, "light open_and_load"))
+ return;
+
+ err = bpf_prog_test_run_opts(skel->progs.entry_light.prog_fd, &topts);
+ if (!ASSERT_OK(err, "run"))
+ goto out;
+ ASSERT_EQ(topts.retval, THROW_COOKIE, "retval");
+ ASSERT_EQ(skel->bss->pads_ran, RAN_LIGHT, "pads_ran");
+out:
+ exceptions_cleanup_light_lskel__destroy(skel);
+}
+
static void test_shapes(void)
{
struct exceptions_cleanup_shapes *skel;
@@ -487,6 +512,9 @@ void test_exceptions_cleanup(void)
exceptions_cleanup__destroy(skel);
+ if (test__start_subtest("light_skeleton"))
+ test_light_skeleton();
+
test_shapes();
RUN_TESTS(exceptions_cleanup_fail);
diff --git a/tools/testing/selftests/bpf/progs/exceptions_cleanup_light.c b/tools/testing/selftests/bpf/progs/exceptions_cleanup_light.c
new file mode 100644
index 000000000000..de7ae478d67f
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/exceptions_cleanup_light.c
@@ -0,0 +1,39 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
+#include <vmlinux.h>
+#include <bpf/bpf_helpers.h>
+#include "bpf_misc.h"
+#include "exceptions_cleanup.h"
+
+static __used __noinline void __kfunc_btf_anchor(void)
+{
+ bpf_throw(0);
+ bpf_preempt_disable();
+ bpf_preempt_enable();
+ bpf_unwind_resume();
+}
+
+__u64 pads_ran = 0;
+
+SEC("syscall")
+__naked int entry_light(void)
+{
+ asm volatile (
+ "call bpf_preempt_disable;"
+ "r1 = %[cookie];"
+"1:" "call bpf_throw;" /* cleanup region */
+"2:"
+ "exit;"
+"3:" /* landing pad */
+ "call bpf_preempt_enable;"
+ PAD_RAN("%[ran]")
+ "call bpf_unwind_resume;"
+ "exit;"
+ CLEANUP_REC("1b", "2b", "3b")
+ :
+ : [cookie]"i"(THROW_COOKIE), [ran]"i"(RAN_LIGHT),
+ __imm_addr(pads_ran)
+ : __clobber_all);
+}
+
+char _license[] SEC("license") = "GPL";
--
2.53.0-Meta
^ permalink raw reply related [flat|nested] 49+ messages in thread