bpf: Trampolines: the size of the image, the type of the return value
cover bpf: Trampolines: the size of the image, the type of the return value
From: Alexei Starovoitov <[email protected]>
Two things that the x86-64 and arm64 trampolines get wrong, and a limit that does not hold what it promises.
Patches 1 - 3, the size of the image. BPF_MAX_TRAMP_LINKS programs are meant to fit in a page. They may not: on x86-64 with call depth tracking the pass that finds the size warns or writes past its scratch page. Let that pass count without an image, as the other JITs do. Then the size is the limit on each architecture, and BPF_MAX_TRAMP_LINKS can go.
Patches 4 - 5, the return value. A BPF program returns 64 bits. The trampoline hands them to a caller that was compiled for a function that returns int or bool, and it tests all 64 bits to tell whether an fmod_ret program wants the traced function skipped.
program returns before after fmod_ret, int f(): 1 << 32 f() is skipped, f() is called
the caller reads 0
fmod_ret, bool f(): 2 the caller gets 2 1
struct_ops, bool member: 2 the caller gets 2 1
Extend the value from the width of the return type, as s390 does, and make a bool 0 or 1. The trampolines of the other architectures do not get that.
Alexei Starovoitov (5):
bpf, x86: Count the size of a trampoline, do not emit it selftests/bpf: Don't use BPF_MAX_TRAMP_LINKS
bpf: Remove BPF_MAX_TRAMP_LINKS
bpf, x86, arm64: Fit the return value of a prog to the return type selftests/bpf: Test the width of the value of an fmod_ret program
arch/arm64/net/bpf_jit_comp.c | 39 ++++++-- arch/s390/net/bpf_jit_comp.c | 18 +--- arch/x86/net/bpf_jit_comp.c | 87 ++++++++++++------ include/linux/bpf.h | 24 +++-- kernel/bpf/bpf_struct_ops.c | 2 +- kernel/bpf/btf.c | 2 + kernel/bpf/trampoline.c | 25 +++--- kernel/bpf/verifier.c | 12 ++- .../selftests/bpf/prog_tests/fexit_stress.c | 29 +++--- .../selftests/bpf/prog_tests/modify_return.c | 51 +++++++++++ .../selftests/bpf/prog_tests/tracing_multi.c | 35 ++++---- .../bpf/prog_tests/trampoline_count.c | 89 ------------------- .../selftests/bpf/progs/fmodret_retval.c | 2 +- .../selftests/bpf/progs/modify_return_width.c | 39 ++++++++ .../bpf/progs/test_trampoline_count.c | 24 ----- .../selftests/bpf/test_kmods/bpf_testmod.c | 19 ++-- tools/testing/selftests/bpf/test_progs.c | 37 -------- tools/testing/selftests/bpf/test_progs.h | 2 - 18 files changed, 256 insertions(+), 280 deletions(-) delete mode 100644 tools/testing/selftests/bpf/prog_tests/trampoline_count.c create mode 100644 tools/testing/selftests/bpf/progs/modify_return_width.c delete mode 100644 tools/testing/selftests/bpf/progs/test_trampoline_count.c
base-commit: 32b66b94c86f009b49668de7340f2ed65aad2dc5
Signature · 1 line
2.55.0
PATCH 1/5 bpf, x86: Count the size of a trampoline, do not emit it
From: Alexei Starovoitov <[email protected]>
arch_bpf_trampoline_size() emits the trampoline into a scratch page to learn its size. The emitter does not look at the end of the image while it writes. Only the WARN_ON_ONCE() behind the last instruction does.
BPF_MAX_TRAMP_LINKS programs fit in a page in the common case. With call depth tracking each call takes 9 more bytes, and they may not fit. Then the attach fails with the warning and -EFAULT, or the size pass writes past its page.
The JITs of the other architectures have a pass that counts and stores nothing. Do the same. Without an image the cursor starts at NULL, and emit_code() only moves it. Two places of the trampoline code stored on their own: the template of the call depth accounting and the jump over a program. They go through emit_code() now.
There is nothing to overflow. bpf_trampoline_update() sees a size above PAGE_SIZE and returns -E2BIG, as on arm64. The scratch page is gone.
--- arch/x86/net/bpf_jit_comp.c | 54 +++++++++++++++++++------------------ 1 file changed, 28 insertions(+), 26 deletions(-)
arch/x86/net/bpf_jit_comp.c +28 -26
@@ -9,12 +9,10 @@#include <linux/filter.h> #include <linux/if_vlan.h> #include <linux/bitfield.h> -#include <linux/init.h> #include <linux/bpf.h> #include <linux/bpf_verifier.h> #include <linux/memory.h> #include <linux/sort.h> -#include <linux/execmem.h> #include <linux/extable.h> #include <asm/extable.h> #include <asm/ftrace.h>
@@ -37,23 +35,15 @@ void __asan_store8(void *p);static bool all_callee_regs_used[4 ] = {true, true, true, true}; -static void *trampoline_size_image; - -static int __init init_trampoline_size_image (void ) +static u8 *emit_code (u8 *ptr,u32 bytes,unsigned int len) {/* -* The generated trampoline contains calls and jumps with 32bit relative -* offsets, so the scratch image must be in the execmem range. -* On x86, module data and executable memory share the same address range, -* so using EXECMEM_MODULE_DATA to get writable memory. +* arch_bpf_trampoline_size() has no image. Its cursor starts at NULL, +* so it is not a kernel address: count, do not store. */ - trampoline_size_image =execmem_alloc (EXECMEM_MODULE_DATA ,PAGE_SIZE ); -return trampoline_size_image ?0 : -ENOMEM ; -} -late_initcall (init_trampoline_size_image); +if ((long )ptr >=0 ) +return ptr + len; -static u8 *emit_code (u8 *ptr,u32 bytes,unsigned int len) -{if (len ==1 ) *ptr = bytes;else if (len ==2 )
@@ -598,10 +588,24 @@ static int emit_call(u8 **pprog, void *func, void *ip)return emit_patch (pprog, func, ip,0xE8 ); } +/* x86_call_depth_emit_accounting() stores. Let emit_code() decide about that. */ +static int emit_call_depth_accounting (u8 **pprog,void *func,void *ip) +{ +u8 temp[BPF_MAX_INSN_SIZE +BPF_INSN_SAFETY ]; +u8 *prog = *pprog, *end = temp; +int i, len; + + len =x86_call_depth_emit_accounting (&end, func, ip); +for (i =0 ; i < len; i++) +EMIT1 (temp[i]); + *pprog = prog; +return len; +} +static int emit_rsb_call (u8 **pprog,void *func,void *ip) {OPTIMIZER_HIDE_VAR (func); - ip +=x86_call_depth_emit_accounting (pprog, func, ip); + ip +=emit_call_depth_accounting (pprog, func, ip);return emit_patch (pprog, func, ip,0xE8 ); }
@@ -4075,8 +4079,7 @@ static int invoke_bpf_prog(const struct btf_func_model *m, u8 **pprog,emit_stx (&prog,BPF_DW ,BPF_REG_FP ,BPF_REG_0 ,-8 );/* replace 2 nops with JE insn, since jmp target is known */ - jmp_insn[0 ] =X86_JE ; - jmp_insn[1 ] = prog - jmp_insn -2 ; +emit_code (jmp_insn,X86_JE + ((prog - jmp_insn -2 ) <<8 ),2 );/* arg1: mov rdi, progs[i] */ emit_mov_imm64 (&prog,BPF_REG_1 , (long ) p >>32 , (u32 ) (long ) p);
@@ -4392,7 +4395,7 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *rw_im* Direct-call fentry stub, as such it needs accounting for the * __fentry__ call. */ -x86_call_depth_emit_accounting (&prog,NULL , image); +emit_call_depth_accounting (&prog,NULL , image); }EMIT1 (0x55 );/* push rbp */ EMIT3 (0x48 ,0x89 ,0xE5 );/* mov rbp, rsp */
@@ -4547,7 +4550,7 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *rw_im }emit_return (&prog, image + (prog - (u8 *)rw_image));/* Make sure the trampoline generation logic doesn't overflow */ -if (WARN_ON_ONCE (prog > (u8 *)rw_image_end -BPF_INSN_SAFETY )) { +if (rw_image &&WARN_ON_ONCE (prog > (u8 *)rw_image_end -BPF_INSN_SAFETY )) { ret = -EFAULT ; goto cleanup; }
@@ -4607,12 +4610,11 @@ int arch_bpf_trampoline_size(const struct btf_func_model *m, u32 flags, {struct bpf_tramp_image im = {}; -if (!trampoline_size_image) -return -ENOMEM ; - -return __arch_prepare_bpf_trampoline (&im, trampoline_size_image, - trampoline_size_image +PAGE_SIZE , - trampoline_size_image, m, flags, +/* +* No image: emit_code() counts. The calls and jumps have 32-bit offsets +* from where the image is going to be. func_addr is in their range. +*/ +return __arch_prepare_bpf_trampoline (&im,NULL ,NULL , func_addr, m, flags, tnodes, func_addr); }
Signature · 1 line
2.55.0
PATCH 2/5 selftests/bpf: Don't use BPF_MAX_TRAMP_LINKS
In reply to Alexei StarovoitovFrom: Alexei Starovoitov <[email protected]>
BPF_MAX_TRAMP_LINKS does not tell how many programs a trampoline takes. Its image fills up earlier or later: that depends on the programs, on the traced function and on the kernel. Three tests read the number from the BTF of the kernel.
fexit_stress and the rollback test of tracing_multi need a trampoline that is full. Let them attach programs until the attach fails with -E2BIG. That is right with the limit and without it, and fexit_stress now covers the attach that does not fit.
trampoline_count attaches BPF_MAX_TRAMP_LINKS programs and expects -E2BIG for the next one. Drop it with its programs and its function in bpf_testmod.
--- .../selftests/bpf/prog_tests/fexit_stress.c | 29 +++--- .../selftests/bpf/prog_tests/tracing_multi.c | 35 ++++---- .../bpf/prog_tests/trampoline_count.c | 89 ------------------- .../bpf/progs/test_trampoline_count.c | 24 ----- .../selftests/bpf/test_kmods/bpf_testmod.c | 18 ---- tools/testing/selftests/bpf/test_progs.c | 37 -------- tools/testing/selftests/bpf/test_progs.h | 2 - 7 files changed, 32 insertions(+), 202 deletions(-) delete mode 100644 tools/testing/selftests/bpf/prog_tests/trampoline_count.c delete mode 100644 tools/testing/selftests/bpf/progs/test_trampoline_count.c
tools/testing/selftests/bpf/prog_tests/fexit_stress.c +13 -16
@@ -3,19 +3,13 @@#include <test_progs.h> #include "bpf_util.h" +/* More programs than fit into the image of a trampoline */ +#define CNT 128 +void serial_test_fexit_stress (void ) { -int bpf_max_tramp_links, err, i; -int *fd, *fexit_fd, *link_fd; - - bpf_max_tramp_links =get_bpf_max_tramp_links (); -if (!ASSERT_GE (bpf_max_tramp_links,1 ,"bpf_max_tramp_links" )) -return ; - fd =calloc (bpf_max_tramp_links *2 ,sizeof (*fd)); -if (!ASSERT_OK_PTR (fd,"fd" )) -return ; - fexit_fd = fd; - link_fd = fd + bpf_max_tramp_links; +int fexit_fd[CNT ] = {}, link_fd[CNT ] = {}; +int err, i;const struct bpf_insn trace_program[] = {BPF_MOV64_IMM (BPF_REG_0 ,0 ),
@@ -34,7 +28,7 @@ void serial_test_fexit_stress(void) goto out; trace_opts.attach_btf_id = err; -for (i =0 ; i < bpf_max_tramp_links; i++) { +for (i =0 ; i <CNT ; i++) { fexit_fd[i] = bpf_prog_load(BPF_PROG_TYPE_TRACING, NULL, "GPL", trace_program, ARRAY_SIZE(trace_program),
@@ -42,19 +36,22 @@ void serial_test_fexit_stress(void)if (!ASSERT_GE (fexit_fd[i],0 ,"fexit load" )) goto out; link_fd[i] =bpf_link_create (fexit_fd[i],0 ,BPF_TRACE_FEXIT ,NULL ); -if (!ASSERT_GE (link_fd[i],0 ,"fexit attach" )) - goto out; +if (link_fd[i] <0 ) +break ; } +/* The image is full */ +if (!ASSERT_GT (i,1 ,"attached" ) || !ASSERT_LT (i,CNT ,"attached" ) || + !ASSERT_EQ (link_fd[i], -E2BIG ,"fexit attach" )) + goto out; err =bpf_prog_test_run_opts (fexit_fd[0 ], &topts);ASSERT_OK (err,"bpf_prog_test_run_opts" ); out: -for (i =0 ; i < bpf_max_tramp_links; i++) { +for (i =0 ; i <CNT ; i++) {if (link_fd[i] >0 )close (link_fd[i]);if (fexit_fd[i] >0 )close (fexit_fd[i]); } -free (fd); }
tools/testing/selftests/bpf/prog_tests/tracing_multi.c +19 -16
@@ -984,16 +984,20 @@ static struct tracing_multi_rollback *extra_load_and_link(void)return NULL ; } -static struct tracing_multi_rollback **fillers_load_and_link (int max) +/* More programs than fit into the image of a trampoline */ +#define FILLERS_MAX 128 + +/* Attach programs to bpf_fentry_test10 until the image of its trampoline is full */ +static struct tracing_multi_rollback **fillers_load_and_link (void ) {struct tracing_multi_rollback **skels, *skel;int i, err; - skels =calloc (max +1 ,sizeof (*skels)); + skels =calloc (FILLERS_MAX ,sizeof (*skels));if (!ASSERT_OK_PTR (skels,"calloc" ))return NULL ; -for (i =0 ; i < max; i++) { +for (i =0 ; i <FILLERS_MAX ; i++) { skel = skels[i] =tracing_multi_rollback__open ();if (!ASSERT_OK_PTR (skels[i],"tracing_multi_rollback__open" )) goto cleanup;
@@ -1005,14 +1009,18 @@ static struct tracing_multi_rollback **fillers_load_and_link(int max) goto cleanup; skel->links.filler =bpf_program__attach_trace (skel->progs.filler); -if (!ASSERT_OK_PTR (skels[i]->links.filler,"bpf_program__attach_trace" )) - goto cleanup; + err =libbpf_get_error (skel->links.filler); +if (err) +break ; } +if (!ASSERT_EQ (err, -E2BIG ,"bpf_program__attach_trace" )) + goto cleanup; +return skels; cleanup: -fillers_cleanup (skels, i +1 ); +fillers_cleanup (skels,FILLERS_MAX );return NULL ; }
@@ -1023,14 +1031,9 @@ static void test_rollback_unlink(void)struct tracing_multi_rollback **fillers;size_t cnt =FUNCS_CNT ;__u32 *ids =NULL ; -int err, max; - - max =get_bpf_max_tramp_links (); -if (!ASSERT_GE (max,1 ,"bpf_max_tramp_links" )) -return ; +int err; -/* Attach maximum allowed programs to bpf_fentry_test10 */ - fillers =fillers_load_and_link (max); + fillers =fillers_load_and_link ();if (!ASSERT_OK_PTR (fillers,"fillers_load_and_link" ))return ;
@@ -1047,8 +1050,8 @@ static void test_rollback_unlink(void)/* * Attach tracing_multi link on bpf_fentry_test1-10, which will -* fail on bpf_fentry_test10 function, because it already has -* maximum allowed programs attached. +* fail on bpf_fentry_test10 function, because the image of its +* trampoline is full. * * The rollback needs to unlink already link-ed trampolines and * put all of them.
@@ -1079,7 +1082,7 @@ static void test_rollback_unlink(void)tracing_multi_rollback_run (skel); cleanup: -fillers_cleanup (fillers, max); +fillers_cleanup (fillers,FILLERS_MAX );tracing_multi_rollback__destroy (extra);tracing_multi_rollback__destroy (skel);free (ids);
tools/testing/selftests/bpf/prog_tests/trampoline_count.c deleted +0 -89
89 lines not on this page · raw message
tools/testing/selftests/bpf/progs/test_trampoline_count.c deleted +0 -24
@@ -1,24 +0,0 @@ -// SPDX-License-Identifier: GPL-2.0 -#include <linux/bpf.h> -#include <bpf/bpf_helpers.h> -#include <bpf/bpf_tracing.h> - -SEC("fentry/bpf_testmod_trampoline_count_test") -int BPF_PROG (fentry_test ) -{ -return 0 ; -} - -SEC("fmod_ret/bpf_testmod_trampoline_count_test") -int BPF_PROG (fmod_ret_test ,int ret) -{ -return 0 ; -} - -SEC("fexit/bpf_testmod_trampoline_count_test") -int BPF_PROG (fexit_test ,int ret) -{ -return 0 ; -} - -char _license[] SEC("license") = "GPL";
tools/testing/selftests/bpf/test_kmods/bpf_testmod.c +0 -18
@@ -767,11 +767,6 @@ noinline void bpf_testmod_stacktrace_test_1(void)int bpf_testmod_fentry_ok; -noinline int bpf_testmod_trampoline_count_test(void) -{ -return 0 ; -} - noinline ssize_t bpf_testmod_test_read(struct file *file, struct kobject *kobj, const struct bin_attribute *bin_attr,
@@ -861,8 +856,6 @@ bpf_testmod_test_read(struct file *file, struct kobject *kobj, 21, 22, 23, 24, 25, 26) != 231) goto out; -bpf_testmod_trampoline_count_test (); -bpf_testmod_stacktrace_test_1 (); bpf_testmod_fentry_ok =1 ;
@@ -2609,16 +2602,6 @@ struct bpf_struct_ops testmod_multi_st_ops = {extern int bpf_fentry_test1 (int a); -BTF_KFUNCS_START (bpf_testmod_trampoline_count_ids) -BTF_ID_FLAGS (func ,bpf_testmod_trampoline_count_test ) -BTF_KFUNCS_END (bpf_testmod_trampoline_count_ids) - -static const struct -btf_kfunc_id_set bpf_testmod_trampoline_count_fmodret_set = { - .owner =THIS_MODULE , - .set = &bpf_testmod_trampoline_count_ids, -}; -static int bpf_testmod_init (void ) {const struct btf_id_dtor_kfunc bpf_testmod_dtors[] = {
@@ -2635,7 +2618,6 @@ static int bpf_testmod_init(void) ret = ret ?:register_btf_kfunc_id_set (BPF_PROG_TYPE_TRACING , &bpf_testmod_kfunc_set); ret = ret ?:register_btf_kfunc_id_set (BPF_PROG_TYPE_SYSCALL , &bpf_testmod_kfunc_set); ret = ret ?:register_btf_kfunc_id_set (BPF_PROG_TYPE_STRUCT_OPS , &bpf_testmod_kfunc_set); - ret = ret ?:register_btf_fmodret_id_set (&bpf_testmod_trampoline_count_fmodret_set); ret = ret ?:register_bpf_struct_ops (&bpf_bpf_testmod_ops, bpf_testmod_ops); ret = ret ?:register_bpf_struct_ops (&bpf_testmod_ops2, bpf_testmod_ops2); ret = ret ?:register_bpf_struct_ops (&bpf_testmod_ops3, bpf_testmod_ops3);
tools/testing/selftests/bpf/test_progs.c +0 -37
@@ -1269,43 +1269,6 @@ int write_sysctl(const char *sysctl, const char *value)return 0 ; } -int get_bpf_max_tramp_links_from (struct btf *btf) -{ -const struct btf_enum *e; -const struct btf_type *t; -__u32 i, type_cnt; -const char *name; -__u32 j, vlen; - -for (i =1 , type_cnt =btf__type_cnt (btf); i < type_cnt; i++) { - t =btf__type_by_id (btf, i); -if (!t || !btf_is_enum (t) || t->name_off) -continue ; - e =btf_enum (t); -for (j =0 , vlen =btf_vlen (t); j < vlen; j++, e++) { - name =btf__str_by_offset (btf, e->name_off); -if (name && !strcmp (name,"BPF_MAX_TRAMP_LINKS" )) -return e->val; - } - } - -return -1 ; -} - -int get_bpf_max_tramp_links (void ) -{ -struct btf *vmlinux_btf; -int ret; - - vmlinux_btf =btf__load_vmlinux_btf (); -if (!ASSERT_OK_PTR (vmlinux_btf,"vmlinux btf" )) -return -1 ; - ret =get_bpf_max_tramp_links_from (vmlinux_btf); -btf__free (vmlinux_btf); - -return ret; -} -static void dump_crash_log (void ) {fflush (stdout);
tools/testing/selftests/bpf/test_progs.h +0 -2
@@ -501,8 +501,6 @@ int compare_stack_ips(int smap_fd, int amap_fd, int stack_trace_len);int trigger_module_test_read (int read_sz);int trigger_module_test_write (int write_sz);int write_sysctl (const char *sysctl,const char *value); -int get_bpf_max_tramp_links_from (struct btf *btf); -int get_bpf_max_tramp_links (void );struct netns_obj ;struct netns_obj *netns_new (const char *name,bool open);
Signature · 1 line
2.55.0
PATCH 3/5 bpf: Remove BPF_MAX_TRAMP_LINKS
In reply to Alexei StarovoitovFrom: Alexei Starovoitov <[email protected]>
A trampoline takes the programs that fit into its image. The number BPF_MAX_TRAMP_LINKS was picked for each architecture so that this many fit, but the code for a program has no fixed size. It depends on the kind of the program, on its cookie, on the stack frame, on call depth tracking. So the number promises nothing, and it refuses programs that would fit:
limit fit x86-64, fentry or fexit 36 57 x86-64, fmod_ret, call depth tracking 36 35 arm64, fmod_ret 37 35
bpf_trampoline_update() asks the JIT for the size of the image and returns -E2BIG if it is too large. That is the limit that holds. Remove the other one.
Three things relied on the number:
- nodes[] in struct bpf_tramp_nodes. It is a pointer now. bpf_trampoline_get_progs() allocates the arrays behind the headers.
- skip[] in the s390 JIT, a label for each program that the second pass read. Patch the branch when the label is known, as invoke_bpf_prog() does for its other forward branch.
- Kernels with pages of 16 or 64 KB. An image may fill a page, so only the number kept it small there. bpf_session_cookie() has 8 bits for the index of a cookie, and the JITs have offsets into the stack frame that grows with the cookies. Limit the image to 4 KB on each kernel. These kernels now take what a kernel with 4 KB pages takes. That can be a few programs less than the number, for example 35 instead of 37 fmod_ret programs on arm64.
--- arch/s390/net/bpf_jit_comp.c | 18 ++++-------------- include/linux/bpf.h | 21 +++++++-------------- kernel/bpf/bpf_struct_ops.c | 2 +- kernel/bpf/trampoline.c | 25 ++++++++++++------------- 4 files changed, 24 insertions(+), 42 deletions(-)
arch/s390/net/bpf_jit_comp.c +4 -14
@@ -2551,8 +2551,6 @@ struct bpf_tramp_jit {int r14_off;/* Offset of saved %r14, has to be at the * bottom */ int do_fexit;/* do_fexit: label */ -int skip[BPF_MAX_TRAMP_LINKS ];/* skip: labels after each prog */ -int nr_progs; };static void load_imm64 (struct bpf_jit *jit,int dst_reg,u64 val)
@@ -2580,18 +2578,15 @@ static int invoke_bpf_prog(struct bpf_tramp_jit *tjit, offsetof(struct bpf_tramp_run_ctx , bpf_cookie);struct bpf_prog *p = node->link->prog;void *skip = jit->prg_buf + jit->prg; -int idx = tjit->nr_progs++; +int nop = jit->prg;int patch; -if (idx >=ARRAY_SIZE (tjit->skip)) -return -E2BIG ; -/* * nop, patched to skip this prog when it is detached */ /* brcl 0,skip */ -EMIT6_PCREL_RILC (0xc0040000 ,0 , tjit->skip[idx]); +EMIT6_PCREL_RILC (0xc0040000 ,0 ,0 );/* * run_ctx.cookie = node->cookie;
@@ -2653,7 +2648,8 @@ static int invoke_bpf_prog(struct bpf_tramp_jit *tjit,EMIT6_PCREL_RILB_PTR (0xc0050000 ,REG_14 ,bpf_trampoline_exit (p));/* skip: */ - tjit->skip[idx] = jit->prg; +if (jit->prg_buf) + *(u32 *)&jit->prg_buf[nop +2 ] = (jit->prg - nop) >>1 ;bpf_tramp_image_add_skip (im, p, skip, jit->prg_buf + jit->prg);return 0 ;
@@ -2717,8 +2713,6 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im,u64 func_meta;int i, j; -/* The skip labels are taken from the previous pass. */ - tjit->nr_progs =0 ;if (im) im->nr_skips =0 ;
@@ -3043,10 +3037,6 @@ int arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *image,if (ret <0 )return ret;if (tjit.common.prg > (char *)image_end - (char *)image) -/* -* Use the same error code as for exceeding -* BPF_MAX_TRAMP_LINKS. -*/ return -E2BIG ; tjit.common.prg =0 ;
include/linux/bpf.h +7 -14
@@ -24,6 +24,7 @@#include <linux/kallsyms.h> #include <linux/capability.h> #include <linux/sched/mm.h> +#include <linux/sizes.h> #include <linux/slab.h> #include <linux/percpu-refcount.h> #include <linux/stddef.h>
@@ -1343,26 +1344,18 @@ static inline u32 btf_func_model_arg_slots(const struct btf_func_model *m, u32 a*/ #define BPF_TRAMP_F_INDIRECT BIT(8) -/* Each call __bpf_prog_enter + call bpf_func + call __bpf_prog_exit is ~50 -* bytes on x86. The trampoline image has to fit in PAGE_SIZE. +/* +* The image of a trampoline is not larger, also where pages are. That limits +* the number of programs: the index of a session cookie fits in its 8 bits, +* and the offsets into the stack frame fit in the instructions of each JIT. */ -enum { -#if defined(__s390x__) || defined(__powerpc64__) -BPF_MAX_TRAMP_LINKS =27 , -#elif defined(__x86_64__) -BPF_MAX_TRAMP_LINKS =36 , -#elif defined(__aarch64__) -BPF_MAX_TRAMP_LINKS =37 , -#else -BPF_MAX_TRAMP_LINKS =38 , -#endif -}; +#define BPF_TRAMP_IMAGE_MAX_SIZE SZ_4K#define BPF_TRAMP_COOKIE_INDEX_SHIFT 8#define BPF_TRAMP_IS_RETURN_SHIFT 63struct bpf_tramp_nodes { -struct bpf_tramp_node *nodes[BPF_MAX_TRAMP_LINKS ]; +struct bpf_tramp_node **nodes;int nr_nodes; };
kernel/bpf/bpf_struct_ops.c +1 -1
@@ -646,7 +646,7 @@ int bpf_struct_ops_prepare_trampoline(struct bpf_tramp_nodes *tnodes,void *image = *_image;int size; - tnodes[BPF_TRAMP_FENTRY ].nodes[0 ] = node; + tnodes[BPF_TRAMP_FENTRY ].nodes = &node; tnodes[BPF_TRAMP_FENTRY ].nr_nodes =1 ;if (model->ret_size >0 )
kernel/bpf/trampoline.c +12 -13
@@ -506,14 +506,18 @@ bpf_trampoline_get_progs(const struct bpf_trampoline *tr, int *total, bool *ip_aint kind; *total =0 ; - tnodes =kzalloc_objs (*tnodes,BPF_TRAMP_MAX ); +for (kind =0 ; kind <BPF_TRAMP_MAX ; kind++) + *total += tr->progs_cnt[kind]; + +/* The arrays of nodes are behind the BPF_TRAMP_MAX headers */ + tnodes =kzalloc (BPF_TRAMP_MAX *sizeof (*tnodes) + *total *sizeof (*nodes),GFP_KERNEL );if (!tnodes)return ERR_PTR (-ENOMEM ); + nodes = (void *)&tnodes[BPF_TRAMP_MAX ];for (kind =0 ; kind <BPF_TRAMP_MAX ; kind++) { tnodes[kind].nr_nodes = tr->progs_cnt[kind]; - *total += tr->progs_cnt[kind]; - nodes = tnodes[kind].nodes; + tnodes[kind].nodes = nodes;hlist_for_each_entry (node, &tr->progs_hlist[kind], tramp_hlist) { *ip_arg |= node->link->prog->call_get_func_ip;
@@ -768,7 +772,7 @@ static int bpf_trampoline_update(struct bpf_trampoline *tr, bool lock_direct_mut goto out; } -if (size >PAGE_SIZE ) { +if (size >BPF_TRAMP_IMAGE_MAX_SIZE ) { err = -E2BIG ; goto out; }
@@ -889,22 +893,17 @@ static struct bpf_tramp_node *fsession_exit(struct bpf_tramp_node *node) }static int bpf_trampoline_add_prog (struct bpf_trampoline *tr, -struct bpf_tramp_node *node, -int cnt) +struct bpf_tramp_node *node) {enum bpf_tramp_prog_type kind;struct bpf_tramp_node *node_existing, *fexit;struct hlist_head *prog_list; kind =bpf_attach_type_to_tramp (node->link->prog); -if (kind ==BPF_TRAMP_FSESSION ) { +if (kind ==BPF_TRAMP_FSESSION ) prog_list = &tr->progs_hlist[BPF_TRAMP_FENTRY ]; - cnt++; - }else { +else prog_list = &tr->progs_hlist[kind]; - } -if (cnt >=BPF_MAX_TRAMP_LINKS ) -return -E2BIG ;if (!hlist_unhashed (&node->tramp_hlist))/* prog already linked */ return -EBUSY ;
@@ -1031,7 +1030,7 @@ static int __bpf_trampoline_link_prog(struct bpf_tramp_node *node, tr->extension_prog = node->link->prog;return 0 ; } - err =bpf_trampoline_add_prog (tr, node, cnt); + err =bpf_trampoline_add_prog (tr, node);if (err)return err; err =bpf_trampoline_update (tr, true/* lock_direct_mutex */ , ops, data);
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH bpf-next 3/5] bpf: Remove BPF_MAX_TRAMP_LINKS
Thank you for your contribution! Sashiko AI review found no new issues in this patch, but noticed 1 related pre-existing issue it does not introduce: - [Critical] bpf: missing list rollback on error…
Thank you for your contribution! Sashiko AI review found no new issues in this patch, but noticed 1 related pre-existing issue it does not introduce:
- [Critical] bpf: missing list rollback on error in bpf_trampoline_add_prog() [1]
--
commit 6cc47d6522f82133a841a876d884e04a65d2d4b1
Author: Alexei Starovoitov <[email protected]>
bpf: Remove BPF_MAX_TRAMP_LINKS
This commit removes the fixed BPF_MAX_TRAMP_LINKS program count limit on BPF trampolines, relying instead on a 4 KB image size limit (BPF_TRAMP_IMAGE_MAX_SIZE), dynamically allocating the bpf_tramp_nodes arrays, and patching s390 skip branches directly.
Quoted 4 lines
kernel/bpf/trampoline.c +0
-0
[ ... ]
Quoted 16 lines
kernel/bpf/trampoline.c +2
-3@@ -889,22 +893,17 @@ static struct bpf_tramp_node *fsession_exit(struct bpf_tramp_node *node) }static int bpf_trampoline_add_prog (struct bpf_trampoline *tr, -struct bpf_tramp_node *node, -int cnt) +struct bpf_tramp_node *node) {enum bpf_tramp_prog_type kind;struct bpf_tramp_node *node_existing, *fexit;struct hlist_head *prog_list; kind =bpf_attach_type_to_tramp (node->link->prog); -if (kind ==BPF_TRAMP_FSESSION ) { +if (kind ==BPF_TRAMP_FSESSION ) prog_list = &tr->progs_hlist[BPF_TRAMP_FENTRY ];
kernel/bpf/trampoline.c static struct bpf_tramp_node *fsession_exit(struct bpf_tramp_node *node) · line 909
Quoted 10 lines
kernel/bpf/trampoline.c +1
-5- cnt++; - }else { +else prog_list = &tr->progs_hlist[kind]; - } -if (cnt >=BPF_MAX_TRAMP_LINKS ) -return -E2BIG ;if (!hlist_unhashed (&node->tramp_hlist))/* prog already linked */ return -EBUSY ;
[Severity: Critical]
This problem wasn't introduced by this patch, but can bpf_trampoline_add_prog() leave a dangling node on prog_list when fsession_exit() fails [1]?
Later in this function, node->tramp_hlist is linked into prog_list and tr->progs_cnt[BPF_TRAMP_FENTRY] is incremented before fsession_exit() is checked:
kernel/bpf/trampoline.c:bpf_trampoline_add_prog() {
...
hlist_add_head(&node->tramp_hlist, prog_list);
node->link->prog->aux->tramp_linked = true;
if (kind == BPF_TRAMP_FSESSION) {
tr->progs_cnt[BPF_TRAMP_FENTRY]++;
fexit = fsession_exit(node);
if (WARN_ON_ONCE(!fexit))
return -EINVAL;
...
}If a BPF_TRAMP_FSESSION program is attached with a link type other than BPF_LINK_TYPE_TRACING or BPF_LINK_TYPE_TRACING_MULTI, fsession_exit() returns NULL and bpf_trampoline_add_prog() returns -EINVAL without unlinking node->tramp_hlist, decrementing tr->progs_cnt[BPF_TRAMP_FENTRY], or clearing tramp_linked. Because callers free node on error, subsequent trampoline accesses will hit a use-after-free on prog_list.
[1] https://sashiko.dev/#/bug/linux-6367ff56-4b7b-447b-bfcf-d5da9ee421e9
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
Re: [PATCH bpf-next 3/5] bpf: Remove BPF_MAX_TRAMP_LINKS
Quoted 7 lines · On Wed, Nov 18, 2026 at 05:39 PM [email protected]…
This problem wasn't introduced by this patch, but can
bpf_trampoline_add_prog()leave a dangling node onprog_listwhenfsession_exit()fails [1]?If a
BPF_TRAMP_FSESSIONprogram is attached with a link type other thanBPF_LINK_TYPE_TRACINGorBPF_LINK_TYPE_TRACING_MULTI,fsession_exit()returns NULL andbpf_trampoline_add_prog()returns-EINVALwithout
Not a bug. There is no such link. bpf_tracing_prog_attach() always inits BPF_LINK_TYPE_TRACING. bpf_tracing_multi_attach() always inits BPF_LINK_TYPE_TRACING_MULTI. The only other caller is the cgroup shim and its prog is BPF_LSM_MAC.
[email protected]
AI reviewRe: [PATCH bpf-next 3/5] bpf: Remove BPF_MAX_TRAMP_LINKS
In reply to Alexei StarovoitovTL;DR: Nit only: the new comment above BPF_TRAMP_IMAGE_MAX_SIZE in bpf.h is garbled and doesn't say what the 4 KB cap is for; a rewording is suggested. Not a bug.
Quoted 1 line
bpf: Remove
BPF_MAX_TRAMP_LINKS
TL;DR: Nit only: the new comment above BPF_TRAMP_IMAGE_MAX_SIZE in bpf.h is garbled and doesn't say what the 4 KB cap is for; a rewording is suggested. Not a bug.
Quoted 3 lines
include/linux/bpf.h +0
-0
[ ... ]
Quoted 13 lines
include/linux/bpf.h +4
-4@@ -1343,26 +1344,18 @@ static inline u32 btf_func_model_arg_slots(const struct btf_func_model *m, u32 a*/ #define BPF_TRAMP_F_INDIRECT BIT(8) -/* Each call __bpf_prog_enter + call bpf_func + call __bpf_prog_exit is ~50 -* bytes on x86. The trampoline image has to fit in PAGE_SIZE. +/* +* The image of a trampoline is not larger, also where pages are. That limits +* the number of programs: the index of a session cookie fits in its 8 bits, +* and the offsets into the stack frame fit in the instructions of each JIT. */ -enum { -#if defined(__s390x__) || defined(__powerpc64__)
include/linux/bpf.h static inline u32 btf_func_model_arg_slots(const struct btf_func_model *m, u32 a · line 1352
Quoted 10 lines
include/linux/bpf.h +1
-9- BPF_MAX_TRAMP_LINKS = 27, -#elif defined(__x86_64__) - BPF_MAX_TRAMP_LINKS = 36, -#elif defined(__aarch64__) - BPF_MAX_TRAMP_LINKS = 37, -#else - BPF_MAX_TRAMP_LINKS = 38, -#endif -}; +#define BPF_TRAMP_IMAGE_MAX_SIZE SZ_4K
this isn't a bug, but is the first sentence of this comment meant to read "The image of a trampoline is not larger, also where pages are."?
It doesn't say what the image is not larger than, and "also where pages are" leaves out the word "larger". The meaning only becomes clear from the commit message: the image is capped at 4 KB even on kernels with 16 KB or 64 KB pages.
Since this comment is the only place in the code explaining why the cap exists, could it be reworded along these lines?
A trampoline image is never larger than this, even on kernels with larger pages. That bounds the number of programs, so that the index of a session cookie fits in its 8 bits and the stack offsets fit in each JIT's instructions.
---
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/44438671971
PATCH 4/5 bpf, x86, arm64: Fit the return value of a prog to the return type
In reply to Alexei StarovoitovFrom: Alexei Starovoitov <[email protected]>
A program returns 64 bits. The x86-64 and arm64 trampolines hand them to a caller that was compiled for a function that returns int or bool.
An fmod_ret program skips the traced function if the 64 bits are not 0. The caller of a function that returns int reads the low 32 bits only. So the trampoline can skip the function on a value that the caller reads as 0:
program trampoline caller r0 = 1 << 32 not 0: skip eax == 0: success
That is no fault of the program. Compilers leave the high half of r0 open for int: "return x >> 8;" is a 64-bit shift. BPF LSM programs go through the same code, and the verifier checks only the low 32 bits of what they return.
A compiler may also assume that a function which returns bool leaves 0 or 1 in the low byte. This is what clang makes of "return f() ? 5 : 7;":
x86-64 xor al, 1; movzx eax, al; lea eax, [rax * 2 + 5] arm64 tst w0, #1; csel ..
If the byte is 2, the first returns 11 and the second takes it for false. An fmod_ret program on a function like should_fail_alloc_page() and a struct_ops program behind a member like sched_ext_ops::yield() can return any number.
Before the trampoline stores the value, extend its low ret_size bytes to 64 bits, with or without the sign, as the return type says. The s390 trampoline does that already. Make a bool 1 if its low byte is not 0. struct btf_func_model does not tell bool from u8, hence BTF_FMODEL_BOOL_RET. The trampoline tests, fexit programs read and the caller gets the same value.
An fexit program still reads -EINVAL if the fmod_ret program returns it, and now reads -EINVAL after "w0 = -22" too. Nothing but bool changes for the callers of struct_ops programs: they read the low bytes only.
The price is one instruction behind the call of such a program, three for bool. Fewer fmod_ret programs fit into an image: 33 instead of 35 on arm64, 31 for bool. The trampolines of the other architectures are not changed.
--- arch/arm64/net/bpf_jit_comp.c | 39 ++++++++++++++++++++++++++++++----- arch/x86/net/bpf_jit_comp.c | 33 ++++++++++++++++++++++++++++- include/linux/bpf.h | 3 +++ kernel/bpf/btf.c | 2 ++ kernel/bpf/verifier.c | 12 +++++------ 5 files changed, 76 insertions(+), 13 deletions(-)
arch/arm64/net/bpf_jit_comp.c +34 -5
@@ -2980,7 +2980,33 @@ bool bpf_jit_supports_large_stack(void)return true; } +/* +* A program returns 64 bits. The caller of the traced function, or of the +* struct_ops member, reads only the low ret_size bytes of x0. Extend them to +* 64 bits as the return type says. That is the value which the trampoline +* tests, which fexit programs read and which the caller gets. +*/ +static void emit_narrow_ret (struct jit_ctx *ctx,const struct btf_func_model *m) +{ +int msb = m->ret_size *8 -1 ; + +if (!m->ret_size || m->ret_size >=sizeof (u64)) +return ; + +if (m->ret_flags &BTF_FMODEL_SIGNED_ARG ) +emit (A64_SBFM (1 ,A64_R (0 ),A64_R (0 ),0 , msb), ctx); +else +emit (A64_UBFM (1 ,A64_R (0 ),A64_R (0 ),0 , msb), ctx); + +if (m->ret_flags &BTF_FMODEL_BOOL_RET ) { +/* The caller may assume that a bool is 0 or 1 */ +emit (A64_CBZ (1 ,A64_R (0 ),2 ), ctx); +emit (A64_MOVZ (1 ,A64_R (0 ),1 ,0 ), ctx); + } +} +static void invoke_bpf_prog (struct jit_ctx *ctx,struct bpf_tramp_image *im, +const struct btf_func_model *m,struct bpf_tramp_node *node,int bargs_off,int retval_off,int run_ctx_off,bool save_ret) {
@@ -3034,8 +3060,10 @@ static void invoke_bpf_prog(struct jit_ctx *ctx, struct bpf_tramp_image *im,emit_call ((const u64 )p->bpf_func, ctx); -if (save_ret) +if (save_ret) { +emit_narrow_ret (ctx, m);emit (A64_STR64I (A64_R (0 ),A64_SP , retval_off), ctx); + }if (ctx->image) {int offset = &ctx->image[ctx->idx] - branch;
@@ -3055,6 +3083,7 @@ static void invoke_bpf_prog(struct jit_ctx *ctx, struct bpf_tramp_image *im, }static void invoke_bpf_mod_ret (struct jit_ctx *ctx,struct bpf_tramp_image *im, +const struct btf_func_model *m,struct bpf_tramp_nodes *tn,int bargs_off,int retval_off,int run_ctx_off,__le32 **branches) {
@@ -3065,7 +3094,7 @@ static void invoke_bpf_mod_ret(struct jit_ctx *ctx, struct bpf_tramp_image *im,*/ emit (A64_STR64I (A64_ZR ,A64_SP , retval_off), ctx);for (i =0 ; i < tn->nr_nodes; i++) { -invoke_bpf_prog (ctx, im, tn->nodes[i], bargs_off, retval_off, +invoke_bpf_prog (ctx, im, m, tn->nodes[i], bargs_off, retval_off, run_ctx_off, true);/* if (*(u64 *)(sp + retval_off) != 0) * goto do_fexit;
@@ -3459,7 +3488,7 @@ static int prepare_trampoline(struct jit_ctx *ctx, struct bpf_tramp_image *im,store_func_meta (ctx, meta, func_meta_off); cookie_bargs_off--; } -invoke_bpf_prog (ctx, im, fentry->nodes[i], bargs_off, +invoke_bpf_prog (ctx, im, m, fentry->nodes[i], bargs_off, retval_off, run_ctx_off, flags &BPF_TRAMP_F_RET_FENTRY_RET ); }
@@ -3470,7 +3499,7 @@ static int prepare_trampoline(struct jit_ctx *ctx, struct bpf_tramp_image *im,if (!branches)return -ENOMEM ; -invoke_bpf_mod_ret (ctx, im, fmod_ret, bargs_off, retval_off, +invoke_bpf_mod_ret (ctx, im, m, fmod_ret, bargs_off, retval_off, run_ctx_off, branches); }
@@ -3504,7 +3533,7 @@ static int prepare_trampoline(struct jit_ctx *ctx, struct bpf_tramp_image *im,store_func_meta (ctx, meta, func_meta_off); cookie_bargs_off--; } -invoke_bpf_prog (ctx, im, fexit->nodes[i], bargs_off, retval_off, +invoke_bpf_prog (ctx, im, m, fexit->nodes[i], bargs_off, retval_off, run_ctx_off, false); }
arch/x86/net/bpf_jit_comp.c +32 -1
@@ -4004,6 +4004,35 @@ static void restore_regs(u8 **prog, const u8 *pos_of_slot, int nr_slots, } } +/* +* A program returns 64 bits. The caller of the traced function, or of the +* struct_ops member, reads only the low ret_size bytes of rax. Extend them to +* 64 bits as the return type says. That is the value which the trampoline +* tests, which fexit programs read and which the caller gets. +*/ +static void emit_narrow_ret (const struct btf_func_model *m,u8 **pprog) +{ +u8 *prog = *pprog; + +if (m->ret_flags &BTF_FMODEL_BOOL_RET ) { +/* The caller may assume that a bool is 0 or 1 */ +EMIT2 (0x84 ,0xC0 );/* test al, al */ +EMIT3 (0x0F ,0x95 ,0xC0 );/* setne al */ + } + +/* movsx rax, al | ax | eax. Nothing for 8 bytes */ +if (m->ret_flags &BTF_FMODEL_SIGNED_ARG ) +emit_movsx_reg (&prog, m->ret_size *8 , true,BPF_REG_0 ,BPF_REG_0 ); +else if (m->ret_size ==1 ) +EMIT3 (0x0F ,0xB6 ,0xC0 );/* movzx eax, al */ +else if (m->ret_size ==2 ) +EMIT3 (0x0F ,0xB7 ,0xC0 );/* movzx eax, ax */ +else if (m->ret_size ==4 ) +EMIT2 (0x89 ,0xC0 );/* mov eax, eax */ + + *pprog = prog; +} + static int invoke_bpf_prog(const struct btf_func_model *m, u8 **pprog, struct bpf_tramp_image *im, struct bpf_tramp_node *node, int stack_size,
@@ -4075,8 +4104,10 @@ static int invoke_bpf_prog(const struct btf_func_model *m, u8 **pprog,* BPF_TRAMP_FENTRY trampoline may need to return the return * value of BPF_PROG_TYPE_STRUCT_OPS prog. */ -if (save_ret) +if (save_ret) { +emit_narrow_ret (m, &prog);emit_stx (&prog,BPF_DW ,BPF_REG_FP ,BPF_REG_0 ,-8 ); + }/* replace 2 nops with JE insn, since jmp target is known */ emit_code (jmp_insn,X86_JE + ((prog - jmp_insn -2 ) <<8 ),2 );
include/linux/bpf.h +3 -0
@@ -1285,6 +1285,9 @@ struct bpf_prog_offload {/* The argument is nullable. */ #define BTF_FMODEL_NULLABLE_ARG BIT(3) +/* The return value is a bool. */ +#define BTF_FMODEL_BOOL_RET BIT(4) +struct btf_func_model {u8 ret_size;u8 ret_flags;
kernel/bpf/btf.c +2 -0
@@ -8385,6 +8385,8 @@ int btf_distill_func_proto(struct bpf_verifier_log *log, } m->ret_size = ret; m->ret_flags =__get_type_fmodel_flags (t); +if (ret ==1 &&btf_type_is_int (t) && (btf_int_encoding (t) &BTF_INT_BOOL )) + m->ret_flags |=BTF_FMODEL_BOOL_RET ;for (i =0 ; i < nargs; i++) {if (i == nargs -1 && args[i].type ==0 ) {
kernel/bpf/verifier.c +5 -7
@@ -19983,14 +19983,12 @@ static int check_fmodret_retval(struct bpf_verifier_env *env, int regno, }/* -* "w0 = -22" zero-extends, the caller of an int function sees -22. The -* trampoline tests all 64 bits: 1 << 32 would skip the function, and the -* caller would see 0. So the low 32 bits decide only if the high ones are 0. +* "w0 = -22" zero-extends. The trampoline sign-extends the low 32 bits of +* the value for an int function, so its caller sees -22. The low 32 bits +* decide if the high ones are 0. Else the 64 bits have to be in the list. * -* That is right where the ABI leaves the high 32 bits of an int in the -* return value register open: x86-64, arm64. s390x, riscv64, ppc64 and -* loongarch want it sign-extended: look here before one of them is added -* to CONFIG_BPF_FMODRET_SCAN. +* The trampolines of x86-64, arm64 and s390x do that: look here before +* another architecture is added to CONFIG_BPF_FMODRET_SCAN. */ t =btf_type_skip_modifiers (aux->attach_btf, aux->attach_func_proto->type,NULL );if (t->size ==4 &®_umax (reg) <=U32_MAX ) {
Signature · 1 line
2.55.0
PATCH 5/5 selftests/bpf: Test the width of the value of an fmod_ret program
In reply to Alexei StarovoitovFrom: Alexei Starovoitov <[email protected]>
Two fmod_ret programs return the same 64-bit value, one for a function that returns int, one for a function that returns bool. The test looks at what the trampoline makes of it:
value int function bool function 1 << 32 called called 0x100 skipped, 0x100 called 2 skipped, 2 skipped, 1 -EINVAL skipped, -EINVAL skipped, 1 (u32)-EINVAL skipped, -EINVAL skipped, 1
fexit programs read the value with bpf_get_func_ret(). The side effect of bpf_modify_return_test() tells whether it was called. bpf_testmod gets the bool function, which returns false.
The test runs on x86-64 and arm64. Also update the comment of a test that described the trampoline.
--- .../selftests/bpf/prog_tests/modify_return.c | 51 +++++++++++++++++++ .../selftests/bpf/progs/fmodret_retval.c | 2 +- .../selftests/bpf/progs/modify_return_width.c | 39 ++++++++++++++ .../selftests/bpf/test_kmods/bpf_testmod.c | 9 ++++ 4 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/bpf/progs/modify_return_width.c
tools/testing/selftests/bpf/prog_tests/modify_return.c +51 -0
@@ -7,6 +7,7 @@#include <test_progs.h> #include <unistd.h> #include "modify_return.skel.h" +#include "modify_return_width.skel.h" #define LOWER (x) ((x) & 0xffff)#define UPPER (x) ((x) >> 16)
@@ -66,6 +67,56 @@ void test_modify_return(void) -EINVAL * 2 /* want_ret */); } +/* +* The trampoline fits what an fmod_ret program returns to the return type of +* the function: int for bpf_modify_return_test(), bool for the one of bpf_testmod. +* Serial: get_func_args_test modifies the value of the first one for each task. +*/ +void serial_test_modify_return_width (void ) +{ +#if defined(__x86_64__) || defined(__aarch64__) +static const struct { +__u64 value;/* The fmod_ret programs return it */ +bool int_called;/* Is bpf_modify_return_test() called? */ +__u64 int_ret, bool_ret;/* The fexit programs read it */ + } tests[] = { + {1ULL <<32 , true,4 ,0 }, + {0x100 , false,0x100 ,0 }, + {2 , false,2 ,1 }, + { -EINVAL , false, -EINVAL ,1 }, + { (__u32)-EINVAL , false, -EINVAL ,1 }, + }; +LIBBPF_OPTS (bpf_test_run_opts, topts); +struct modify_return_width *skel; +int i; + + skel =modify_return_width__open_and_load (); +if (!ASSERT_OK_PTR (skel,"open_and_load" )) +return ; + skel->bss->pid =getpid (); +if (!ASSERT_OK (modify_return_width__attach (skel),"attach" )) + goto out; + +for (i =0 ; i <ARRAY_SIZE (tests); i++) { + skel->bss->value = tests[i].value; + skel->bss->int_ret =-1 ; + skel->bss->bool_ret =-1 ; +ASSERT_OK (bpf_prog_test_run_opts (bpf_program__fd (skel->progs.mod_int), &topts), +"test_run" ); +ASSERT_OK (trigger_module_test_read (1 ),"trigger_read" ); + +/* bpf_modify_return_test2() has the other side effect */ +ASSERT_EQ (UPPER (topts.retval),1 + tests[i].int_called,"int_called" ); +ASSERT_EQ (skel->bss->int_ret, tests[i].int_ret,"int_ret" ); +ASSERT_EQ (skel->bss->bool_ret, tests[i].bool_ret,"bool_ret" ); + } +out: +modify_return_width__destroy (skel); +#else +test__skip (); +#endif +} +void test_modify_return_graph (void ) {char instance[MAX_INSTANCE_PATH_LEN ];
tools/testing/selftests/bpf/progs/fmodret_retval.c +1 -1
@@ -85,7 +85,7 @@ __naked void negated_bool(void) ::: __clobber_all); } -/* Not 0, so the function is not called, but the caller would see 0 */ +/* 0 for the trampoline and for the caller, but not one of the 64-bit values of the list */ SEC("fmod_ret/bpf_testmod_retval_called") __failure __msg("R0 should be 0 or one of -27,-22") __naked void high_bits(void)
tools/testing/selftests/bpf/progs/modify_return_width.c new file +39 -0
@@ -0,0 +1,39 @@ +// SPDX-License-Identifier: GPL-2.0 +/* See serial_test_modify_return_width() */ +#include <vmlinux.h> +#include <bpf/bpf_helpers.h> +#include <bpf/bpf_tracing.h> + +char _license[] SEC("license") = "GPL"; + +int pid; +__u64 value;/* The fmod_ret programs return it */ +__u64 int_ret, bool_ret;/* The fexit programs read it */ + +SEC("fmod_ret/bpf_modify_return_test") +long BPF_PROG (mod_int ,int a,int *b,int ret) +{ +return bpf_get_current_pid_tgid () >>32 == pid ? value :0 ; +} + +SEC("fexit/bpf_modify_return_test") +int BPF_PROG (exit_int ,int a,int *b,int ret) +{ +if (bpf_get_current_pid_tgid () >>32 == pid) +bpf_get_func_ret (ctx, &int_ret); +return 0 ; +} + +SEC("fmod_ret/bpf_testmod_modify_return_bool") +long BPF_PROG (mod_bool ,bool ret) +{ +return bpf_get_current_pid_tgid () >>32 == pid ? value :0 ; +} + +SEC("fexit/bpf_testmod_modify_return_bool") +int BPF_PROG (exit_bool ,bool ret) +{ +if (bpf_get_current_pid_tgid () >>32 == pid) +bpf_get_func_ret (ctx, &bool_ret); +return 0 ; +}
tools/testing/selftests/bpf/test_kmods/bpf_testmod.c +9 -0
@@ -767,6 +767,13 @@ noinline void bpf_testmod_stacktrace_test_1(void)int bpf_testmod_fentry_ok; +noinline bool bpf_testmod_modify_return_bool(void) +{ + asmvolatile ("" ); +return false; +} +ALLOW_ERROR_INJECTION (bpf_testmod_modify_return_bool, TRUE); + noinline ssize_t bpf_testmod_test_read(struct file *file, struct kobject *kobj, const struct bin_attribute *bin_attr,
@@ -856,6 +863,8 @@ bpf_testmod_test_read(struct file *file, struct kobject *kobj, 21, 22, 23, 24, 25, 26) != 231) goto out; + (void )bpf_testmod_modify_return_bool (); +bpf_testmod_stacktrace_test_1 (); bpf_testmod_fentry_ok =1 ;
Signature · 1 line
2.55.0
This series was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:
Hello:
This series was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:
Quoted 5 lines · On Thu, 19 Nov 2026 01:29:04 +0000 you wrote:
From: Alexei Starovoitov <[email protected]>
Two things that the x86-64 and arm64 trampolines get wrong, and a limit that does not hold what it promises.
Quoted 7 lines
Patches 1 - 3, the size of the image.
BPF_MAX_TRAMP_LINKSprograms are meant to fit in a page. They may not: on x86-64 with call depth tracking the pass that finds the size warns or writes past its scratch page. Let that pass count without an image, as the other JITs do. Then the size is the limit on each architecture, andBPF_MAX_TRAMP_LINKScan go.[...]
Here is the summary with links:
- [bpf-next,1/5] bpf, x86: Count the size of a trampoline, do not emit it
https://git.kernel.org/bpf/bpf-next/c/71531b583c94 - [bpf-next,2/5] selftests/bpf: Don't use BPF_MAX_TRAMP_LINKS https://git.kernel.org/bpf/bpf-next/c/9e22f382de87 - [bpf-next,3/5] bpf: Remove BPF_MAX_TRAMP_LINKS https://git.kernel.org/bpf/bpf-next/c/cfe2171b0bd9 - [bpf-next,4/5] bpf, x86, arm64: Fit the return value of a prog to the return type https://git.kernel.org/bpf/bpf-next/c/664fde3a58f0 - [bpf-next,5/5] selftests/bpf: Test the width of the value of an fmod_ret program https://git.kernel.org/bpf/bpf-next/c/7af0b756a8ef
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html