bpf: report arena faults to BPF streams
Puranjay Mohan
maintainerv7 cover bpf: report arena faults to BPF streams
Changes in v6->v7:
v6: https://lore.kernel.org/all/[email protected]/
- Added comments about the usage of arena_reg in x86 and arm64 jits. (Alexei)
- Used clear_lo32() for clearing the lower 32-bits of user_vm_start. (Alexei)
- Moved update of the old tests to use __stderr to a separate commit (Eduard)
- Used test__skip() in prog_tests/stream.c (Eduard)
- Start a sub-test for read / write
Changes in v5->v6:
v5: https://lore.kernel.org/all/[email protected]/
- Introduces __stderr and __stdout for easy testing of bpf streams (Eduard)
- Add more test cases for arena fault reporting (subprog and callback)
- Fix main_prog_aux usage and return main_prog from find_from_stack_cb (Kumar)
- Properly fix the build issue reported by kernel test robot
Changes in v4->v5:
v4: https://lore.kernel.org/all/[email protected]/
- Added patch 2 to introducing main_prog_aux for easier access to streams.
- Fixed bug in fault handlers when arena_reg == dst_reg
- Updated selftest to check test above edge case.
- Added comments about the usage of barrier_var() in code and commit message.
Changes in v3->v4:
v3: https://lore.kernel.org/all/[email protected]/
- Fixed a build issue when CONFIG_BPF_JIT=y and # CONFIG_BPF_SYSCALL is not set
Changes in v2->v3:
v2: https://lore.kernel.org/all/[email protected]/
- Improved the selftest to check the exact fault address
- Dropped BPF_NO_KFUNC_PROTOTYPES and bpf_arena_alloc/free_pages() usage
- Rebased on bpf-next/master
Changes in v1->v2:
v1: https://lore.kernel.org/all/[email protected]/
- Changed variable and mask names for consistency (Yonghong)
- Added Acked-by: Yonghong Song <[email protected]> on two patches
This set adds the support of reporting page faults inside arena to BPF stderr stream. The reported address is the one that a user would expect to see if they pass it to bpf_printk();
Here is an example output from the stderr stream and bpf_printk()
ERROR: Arena WRITE access at unmapped address 0xdeaddead0000
CPU: 9 UID: 0 PID: 502 Comm: test_progs Call trace: bpf_stream_stage_dump_stack+0xc0/0x150 bpf_prog_report_arena_violation+0x98/0xf0 ex_handler_bpf+0x5c/0x78 fixup_exception+0xf8/0x160 __do_kernel_fault+0x40/0x188 do_bad_area+0x70/0x88 do_translation_fault+0x54/0x98 do_mem_abort+0x4c/0xa8 el1_abort+0x44/0x70 el1h_64_sync_handler+0x50/0x108 el1h_64_sync+0x6c/0x70 bpf_prog_a64a9778d31b8e88_stream_arena_write_fault+0x84/0xc8 *(page) = 1; @ stream.c:100 bpf_prog_test_run_syscall+0x100/0x328 __sys_bpf+0x508/0xb98 __arm64_sys_bpf+0x2c/0x48 invoke_syscall+0x50/0x120 el0_svc_common.constprop.0+0x48/0xf8 do_el0_svc+0x28/0x40 el0_svc+0x48/0xf8 el0t_64_sync_handler+0xa0/0xe8 el0t_64_sync+0x198/0x1a0
Same address is printed by bpf_printk():
1389.078831: bpf_trace_printk: Read Address: 0xdeaddead0000
To make this possible, some extra metadata has to be passed to the bpf exception handler, so the bpf exception handling mechanism for both x86-64 and arm64 have been improved in this set.
The streams selftest has been updated to test this new feature.
Puranjay Mohan (6):
bpf: arm64: simplify exception table handling
bpf: core: introduce main_prog_aux for stream access
bpf: Report arena faults to BPF stderr
selftests: bpf: introduce __stderr and __stdout
selftests: bpf: use __stderr in stream error tests selftests/bpf: Add tests for arena fault reporting
arch/arm64/net/bpf_jit_comp.c | 85 +++++++--- arch/x86/net/bpf_jit_comp.c | 85 +++++++++- include/linux/bpf.h | 7 + kernel/bpf/arena.c | 30 ++++ kernel/bpf/core.c | 6 +- kernel/bpf/verifier.c | 1 + .../testing/selftests/bpf/prog_tests/stream.c | 131 ++++++--------- tools/testing/selftests/bpf/progs/bpf_misc.h | 10 ++ tools/testing/selftests/bpf/progs/stream.c | 158 ++++++++++++++++++ tools/testing/selftests/bpf/test_loader.c | 90 ++++++++++ 10 files changed, 491 insertions(+), 112 deletions(-)
Signature · 1 line
2.47.3
Puranjay Mohan
maintainerPATCH v7 1/6 bpf: arm64: simplify exception table handling
BPF loads with BPF_PROBE_MEM(SX) can load from unsafe pointers and the JIT adds an exception table entry for the JITed instruction which allows the exeption handler to set the destination register of the load to zero and continue execution from the next instruction.
As all arm64 instructions are AARCH64_INSN_SIZE size, the exception handler can just increment the pc by AARCH64_INSN_SIZE without needing the exact address of the instruction following the the faulting instruction.
Simplify the exception table usage in arm64 JIT by only saving the destination register in ex->fixup and drop everything related to the fixup_offset. The fault handler is modified to add AARCH64_INSN_SIZE to the pc.
--- arch/arm64/net/bpf_jit_comp.c | 25 +++---------------------- 1 file changed, 3 insertions(+), 22 deletions(-)
arch/arm64/net/bpf_jit_comp.c +3 -22
@@ -1066,19 +1066,18 @@ static void build_epilogue(struct jit_ctx *ctx, bool was_classic)emit (A64_RET (A64_LR ), ctx); } -#define BPF_FIXUP_OFFSET_MASK GENMASK(26, 0)#define BPF_FIXUP_REG_MASK GENMASK(31, 27)#define DONT_CLEAR 5/* Unused ARM64 register from BPF's POV */ bool ex_handler_bpf (const struct exception_table_entry *ex,struct pt_regs *regs) { -off_t offset =FIELD_GET (BPF_FIXUP_OFFSET_MASK , ex->fixup);int dst_reg =FIELD_GET (BPF_FIXUP_REG_MASK , ex->fixup);if (dst_reg !=DONT_CLEAR ) regs->regs[dst_reg] =0 ; - regs->pc = (unsigned long )&ex->fixup - offset; +/* Skip the faulting instruction */ + regs->pc +=AARCH64_INSN_SIZE ;return true; }
@@ -1088,7 +1087,6 @@ static int add_exception_handler(const struct bpf_insn *insn, int dst_reg) {off_t ins_offset; -off_t fixup_offset;unsigned long pc;struct exception_table_entry *ex;
@@ -1119,22 +1117,6 @@ static int add_exception_handler(const struct bpf_insn *insn,if (WARN_ON_ONCE (ins_offset >=0 || ins_offset <INT_MIN ))return -ERANGE ; -/* -* Since the extable follows the program, the fixup offset is always -* negative and limited to BPF_JIT_REGION_SIZE. Store a positive value -* to keep things simple, and put the destination register in the upper -* bits. We don't need to worry about buildtime or runtime sort -* modifying the upper bits because the table is already sorted, and -* isn't part of the main exception table. -* -* The fixup_offset is set to the next instruction from the instruction -* that may fault. The execution will jump to this after handling the -* fault. -*/ - fixup_offset = (long )&ex->fixup - (pc +AARCH64_INSN_SIZE ); -if (!FIELD_FIT (BPF_FIXUP_OFFSET_MASK , fixup_offset)) -return -ERANGE ; -/* * The offsets above have been calculated using the RO buffer but we * need to use the R/W buffer for writes.
@@ -1147,8 +1129,7 @@ static int add_exception_handler(const struct bpf_insn *insn,if (BPF_CLASS (insn->code) !=BPF_LDX ) dst_reg =DONT_CLEAR ; - ex->fixup =FIELD_PREP (BPF_FIXUP_OFFSET_MASK , fixup_offset) | -FIELD_PREP (BPF_FIXUP_REG_MASK , dst_reg); + ex->fixup =FIELD_PREP (BPF_FIXUP_REG_MASK , dst_reg); ex->type =EX_TYPE_BPF ;
Signature · 1 line
2.47.3
Puranjay Mohan
maintainerPATCH v7 2/6 bpf: core: introduce main_prog_aux for stream access
In reply to Puranjay MohanBPF streams are only valid for the main programs, to make it easier to access streams from subprogs, introduce main_prog_aux in struct bpf_prog_aux.
prog->aux->main_prog_aux = prog->aux, for main programs and prog->aux->main_prog_aux = main_prog->aux, for subprograms.
Make bpf_prog_find_from_stack() use the added main_prog_aux to return the mainprog when a subprog is found on the stack.
--- include/linux/bpf.h | 1 + kernel/bpf/core.c | 6 +++--- kernel/bpf/verifier.c | 1 + 3 files changed, 5 insertions(+), 3 deletions(-)
include/linux/bpf.h +1 -0
@@ -1633,6 +1633,7 @@ struct bpf_prog_aux {/* function name for valid attach_btf_id */ const char *attach_func_name;struct bpf_prog **func; +struct bpf_prog_aux *main_prog_aux;void *jit_data;/* JIT specific data. arch dependent */ struct bpf_jit_poke_descriptor *poke_tab;struct bpf_kfunc_desc_tab *kfunc_tab;
kernel/bpf/core.c +3 -3
@@ -120,6 +120,7 @@ struct bpf_prog *bpf_prog_alloc_no_stats(unsigned int size, gfp_t gfp_extra_flag fp->pages = size /PAGE_SIZE ; fp->aux = aux; + fp->aux->main_prog_aux = aux; fp->aux->prog = fp; fp->jit_requested =ebpf_jit_enabled (); fp->blinding_requested =bpf_jit_blinding_enabled (fp);
@@ -3292,9 +3293,8 @@ static bool find_from_stack_cb(void *cookie, u64 ip, u64 sp, u64 bp)rcu_read_unlock ();if (!prog)return true; -if (bpf_is_subprog (prog)) -return true; - ctxp->prog = prog; +/* Make sure we return the main prog if we found a subprog */ + ctxp->prog = prog->aux->main_prog_aux->prog;return false; }
kernel/bpf/verifier.c +1 -0
@@ -21597,6 +21597,7 @@ static int jit_subprogs(struct bpf_verifier_env *env) func[i]->aux->func_info_cnt = prog->aux->func_info_cnt; func[i]->aux->poke_tab = prog->aux->poke_tab; func[i]->aux->size_poke_tab = prog->aux->size_poke_tab; + func[i]->aux->main_prog_aux = prog->aux;for (j =0 ; j < prog->aux->size_poke_tab; j++) {struct bpf_jit_poke_descriptor *poke;
Signature · 1 line
2.47.3
Re: [PATCH bpf-next v7 2/6] bpf: core: introduce main_prog_aux for stream access
Quoted 4 lines · Puranjay Mohan
BPF streams are only valid for the main programs, to make it easier to access streams from subprogs, introduce
main_prog_auxin structbpf_prog_aux.
Quoted 8 lines
prog->aux->main_prog_aux=prog->aux, for main programs andprog->aux->main_prog_aux=main_prog->aux, for subprograms.Make
bpf_prog_find_from_stack()use the addedmain_prog_auxto return the mainprog when a subprog is found on the stack.Signed-off-by Puranjay Mohan <[email protected]>---
[...]
kernel/bpf/core.c struct bpf_prog *bpf_prog_alloc_no_stats(unsigned int size, gfp_t gfp_extra_flag · line 123
Quoted 5 lines
kernel/bpf/core.c +1
-0@@ -120,6 +120,7 @@ struct bpf_prog *bpf_prog_alloc_no_stats(unsigned int size, gfp_t gfp_extra_flag fp->pages = size /PAGE_SIZE ; fp->aux = aux; + fp->aux->main_prog_aux = aux;
The fact that this needs to be added both here and in jit_subprogs is unfortunate :(
[...]
Puranjay Mohan
maintainerPATCH v7 3/6 bpf: Report arena faults to BPF stderr
In reply to Puranjay MohanBegin reporting arena page faults and the faulting address to BPF program's stderr, this patch adds support in the arm64 and x86-64 JITs, support for other archs can be added later.
The fault handlers receive the 32 bit address in the arena region so the upper 32 bits of user_vm_start is added to it before printing the address. This is what the user would expect to see as this is what is printed by bpf_printk() is you pass it an address returned by bpf_arena_alloc_pages();
--- arch/arm64/net/bpf_jit_comp.c | 60 +++++++++++++++++++++++++ arch/x86/net/bpf_jit_comp.c | 85 ++++++++++++++++++++++++++++++++--- include/linux/bpf.h | 6 +++ kernel/bpf/arena.c | 30 +++++++++++++ 4 files changed, 176 insertions(+), 5 deletions(-)
arch/arm64/net/bpf_jit_comp.c +60 -0
@@ -1066,6 +1066,30 @@ static void build_epilogue(struct jit_ctx *ctx, bool was_classic)emit (A64_RET (A64_LR ), ctx); } +/* +* Metadata encoding for exception handling in JITed code. +* +* Format of `fixup` field in `struct exception_table_entry`: +* +* Bit layout of `fixup` (32-bit): +* +* +-----------+--------+-----------+-----------+----------+ +* | 31-27 | 26-22 | 21 | 20-16 | 15-0 | +* | | | | | | +* | FIXUP_REG | Unused | ARENA_ACC | ARENA_REG | OFFSET | +* +-----------+--------+-----------+-----------+----------+ +* +* - OFFSET (16 bits): Offset used to compute address for Load/Store instruction. +* - ARENA_REG (5 bits): Register that is used to calculate the address for load/store when +* accessing the arena region. +* - ARENA_ACCESS (1 bit): This bit is set when the faulting instruction accessed the arena region. +* - FIXUP_REG (5 bits): Destination register for the load instruction (cleared on fault) or set to +* DONT_CLEAR if it is a store instruction. +*/ + +#define BPF_FIXUP_OFFSET_MASK GENMASK(15, 0) +#define BPF_FIXUP_ARENA_REG_MASK GENMASK(20, 16) +#define BPF_ARENA_ACCESS BIT(21)#define BPF_FIXUP_REG_MASK GENMASK(31, 27)#define DONT_CLEAR 5/* Unused ARM64 register from BPF's POV */
@@ -1073,11 +1097,22 @@ bool ex_handler_bpf(const struct exception_table_entry *ex, struct pt_regs *regs) {int dst_reg =FIELD_GET (BPF_FIXUP_REG_MASK , ex->fixup); +s16 off =FIELD_GET (BPF_FIXUP_OFFSET_MASK , ex->fixup); +int arena_reg =FIELD_GET (BPF_FIXUP_ARENA_REG_MASK , ex->fixup); +bool is_arena = !!(ex->fixup &BPF_ARENA_ACCESS ); +bool is_write = (dst_reg ==DONT_CLEAR ); +unsigned long addr; + +if (is_arena) { + addr = regs->regs[arena_reg] + off; +bpf_prog_report_arena_violation (is_write, addr, regs->pc); + }if (dst_reg !=DONT_CLEAR ) regs->regs[dst_reg] =0 ;/* Skip the faulting instruction */ regs->pc +=AARCH64_INSN_SIZE ; +return true; }
@@ -1087,6 +1122,9 @@ static int add_exception_handler(const struct bpf_insn *insn, int dst_reg) {off_t ins_offset; +s16 off = insn->off; +bool is_arena; +int arena_reg;unsigned long pc;struct exception_table_entry *ex;
@@ -1100,6 +1138,9 @@ static int add_exception_handler(const struct bpf_insn *insn, BPF_MODE(insn->code) != BPF_PROBE_ATOMIC)return 0 ; + is_arena = (BPF_MODE (insn->code) ==BPF_PROBE_MEM32 ) || + (BPF_MODE (insn->code) ==BPF_PROBE_ATOMIC ); +if (!ctx->prog->aux->extable ||WARN_ON_ONCE (ctx->exentry_idx >= ctx->prog->aux->num_exentries))return -EINVAL ;
@@ -1131,6 +1172,25 @@ static int add_exception_handler(const struct bpf_insn *insn, ex->fixup =FIELD_PREP (BPF_FIXUP_REG_MASK , dst_reg); +if (is_arena) { + ex->fixup |=BPF_ARENA_ACCESS ; +/* +* insn->src_reg/dst_reg holds the address in the arena region with upper 32-bits +* being zero because of a preceding addr_space_cast(r<n>, 0x0, 0x1) instruction. +* This address is adjusted with the addition of arena_vm_start (see the +* implementation of BPF_PROBE_MEM32 and BPF_PROBE_ATOMIC) before being used for the +* memory access. Pass the reg holding the unmodified 32-bit address to +* ex_handler_bpf. +*/ +if (BPF_CLASS (insn->code) ==BPF_LDX ) + arena_reg = bpf2a64[insn->src_reg]; +else + arena_reg = bpf2a64[insn->dst_reg]; + + ex->fixup |=FIELD_PREP (BPF_FIXUP_OFFSET_MASK , off) | +FIELD_PREP (BPF_FIXUP_ARENA_REG_MASK , arena_reg); + } + ex->type =EX_TYPE_BPF ; ctx->exentry_idx++;
arch/x86/net/bpf_jit_comp.c +80 -5
123 lines not on this page · raw message
include/linux/bpf.h +6 -0
@@ -2881,6 +2881,7 @@ void bpf_dynptr_init(struct bpf_dynptr_kern *ptr, void *data, enum bpf_dynptr_type type, u32 offset, u32 size);void bpf_dynptr_set_null (struct bpf_dynptr_kern *ptr);void bpf_dynptr_set_rdonly (struct bpf_dynptr_kern *ptr); +void bpf_prog_report_arena_violation (bool write,unsigned long addr,unsigned long fault_ip);#else /* !CONFIG_BPF_SYSCALL */ static inline struct bpf_prog *bpf_prog_get (u32 ufd)
@@ -3168,6 +3169,11 @@ static inline void bpf_dynptr_set_null(struct bpf_dynptr_kern *ptr)static inline void bpf_dynptr_set_rdonly (struct bpf_dynptr_kern *ptr) { } + +static inline void bpf_prog_report_arena_violation (bool write,unsigned long addr, +unsigned long fault_ip) +{ +}#endif /* CONFIG_BPF_SYSCALL */ static __always_inline int
kernel/bpf/arena.c +30 -0
@@ -633,3 +633,33 @@ static int __init kfunc_init(void)return register_btf_kfunc_id_set (BPF_PROG_TYPE_UNSPEC , &common_kfunc_set); }late_initcall (kfunc_init); + +void bpf_prog_report_arena_violation (bool write,unsigned long addr,unsigned long fault_ip) +{ +struct bpf_stream_stage ss; +struct bpf_prog *prog; +u64 user_vm_start; + +/* +* The RCU read lock is held to safely traverse the latch tree, but we +* don't need its protection when accessing the prog, since it will not +* disappear while we are handling the fault. +*/ +rcu_read_lock (); + prog =bpf_prog_ksym_find (fault_ip); +rcu_read_unlock (); +if (!prog) +return ; + +/* Use main prog for stream access */ + prog = prog->aux->main_prog_aux->prog; + + user_vm_start =bpf_arena_get_user_vm_start (prog->aux->arena); + addr +=clear_lo32 (user_vm_start); + +bpf_stream_stage (ss, prog,BPF_STDERR , ({ +bpf_stream_printk (ss,"ERROR: Arena %s access at unmapped address 0x%lx\n" , + write ?"WRITE" :"READ" , addr); +bpf_stream_dump_stack (ss); + })); +}
Signature · 1 line
2.47.3
Puranjay Mohan
maintainerPATCH v7 4/6 selftests: bpf: introduce __stderr and __stdout
In reply to Puranjay MohanAdd __stderr and __stdout to validate the output of BPF streams for bpf selftests. Similar to __xlated, __jited, etc., __stderr/out can be used in the BPF progs to compare a string (regex supported) to the output in the bpf streams.
--- tools/testing/selftests/bpf/progs/bpf_misc.h | 10 +++ tools/testing/selftests/bpf/test_loader.c | 90 ++++++++++++++++++++ 2 files changed, 100 insertions(+)
tools/testing/selftests/bpf/progs/bpf_misc.h +10 -0
@@ -35,6 +35,12 @@* inside the brackets. * __msg_unpriv Same as __msg but for unprivileged mode. * +* __stderr Message expected to be found in bpf stderr stream. The +* same regex rules apply like __msg. +* __stderr_unpriv Same as __stderr but for unpriveleged mode. +* __stdout Same as __stderr but for stdout stream. +* __stdout_unpriv Same as __stdout but for unpriveleged mode. +* * __xlated Expect a line in a disassembly log after verifier applies rewrites. * Multiple __xlated attributes could be specified. * Regular expressions could be specified same way as in __msg.
@@ -140,6 +146,10 @@#define __caps_unpriv (caps) __attribute__((btf_decl_tag("comment:test_caps_unpriv=" EXPAND_QUOTE(caps))))#define __load_if_JITed () __attribute__((btf_decl_tag("comment:load_mode=jited")))#define __load_if_no_JITed () __attribute__((btf_decl_tag("comment:load_mode=no_jited"))) +#define __stderr (msg) __attribute__((btf_decl_tag("comment:test_expect_stderr=" XSTR(__COUNTER__) "=" msg))) +#define __stderr_unpriv (msg) __attribute__((btf_decl_tag("comment:test_expect_stderr_unpriv=" XSTR(__COUNTER__) "=" msg))) +#define __stdout (msg) __attribute__((btf_decl_tag("comment:test_expect_stdout=" XSTR(__COUNTER__) "=" msg))) +#define __stdout_unpriv (msg) __attribute__((btf_decl_tag("comment:test_expect_stdout_unpriv=" XSTR(__COUNTER__) "=" msg)))/* Define common capabilities tested using __caps_unpriv */ #define CAP_NET_ADMIN 12
tools/testing/selftests/bpf/test_loader.c +90 -0
144 lines not on this page · raw message
Signature · 1 line
2.47.3
Re: [PATCH bpf-next v7 4/6] selftests: bpf: introduce __stderr and __stdout
Quoted 7 lines · Puranjay Mohan
Add
__stderrand__stdoutto validate the output of BPF streams for bpf selftests. Similar to__xlated,__jited, etc., __stderr/out can be used in the BPF progs to compare a string (regex supported) to the output in the bpf streams.Signed-off-by Puranjay Mohan <[email protected]>---
[...]
Puranjay Mohan
maintainerPATCH v7 5/6 selftests: bpf: use __stderr in stream error tests
In reply to Puranjay MohanStart using __stderr directly in the bpf programs to test the reporting of may_goto timeout detection and spin_lock dead lock detection.
--- .../testing/selftests/bpf/prog_tests/stream.c | 82 ------------------- tools/testing/selftests/bpf/progs/stream.c | 17 ++++ 2 files changed, 17 insertions(+), 82 deletions(-)
tools/testing/selftests/bpf/prog_tests/stream.c +0 -82
@@ -2,7 +2,6 @@/* Copyright (c) 2025 Meta Platforms, Inc. and affiliates. */ #include <test_progs.h> #include <sys/mman.h> -#include <regex.h> #include "stream.skel.h" #include "stream_fail.skel.h"
@@ -18,87 +17,6 @@ void test_stream_success(void)return ; } -struct { -int prog_off; -const char *errstr; -} stream_error_arr[] = { - { - offsetof(struct stream, progs.stream_cond_break), -"ERROR: Timeout detected for may_goto instruction\n" -"CPU: [0-9]+ UID: 0 PID: [0-9]+ Comm: .*\n" -"Call trace:\n" -"([a-zA-Z_][a-zA-Z0-9_]*\\+0x[0-9a-fA-F]+/0x[0-9a-fA-F]+\n" -"|[ \t]+[^\n]+\n)*" , - }, - { - offsetof(struct stream, progs.stream_deadlock), -"ERROR: AA or ABBA deadlock detected for bpf_res_spin_lock\n" -"Attempted lock = (0x[0-9a-fA-F]+)\n" -"Total held locks = 1\n" -"Held lock\\[ 0\\] = \\1\n" // Lock address must match -"CPU: [0-9]+ UID: 0 PID: [0-9]+ Comm: .*\n" -"Call trace:\n" -"([a-zA-Z_][a-zA-Z0-9_]*\\+0x[0-9a-fA-F]+/0x[0-9a-fA-F]+\n" -"|[ \t]+[^\n]+\n)*" , - }, -}; - -static int match_regex (const char *pattern,const char *string) -{ -int err, rc; -regex_t re; - - err =regcomp (&re, pattern,REG_EXTENDED |REG_NEWLINE ); -if (err) -return -1 ; - rc =regexec (&re, string,0 ,NULL ,0 ); -regfree (&re); -return rc ==0 ?1 :0 ; -} - -void test_stream_errors (void ) -{ -LIBBPF_OPTS (bpf_test_run_opts, opts); -LIBBPF_OPTS (bpf_prog_stream_read_opts, ropts); -struct stream *skel; -int ret, prog_fd; -char buf[1024 ]; - - skel =stream__open_and_load (); -if (!ASSERT_OK_PTR (skel,"stream__open_and_load" )) -return ; - -for (int i =0 ; i <ARRAY_SIZE (stream_error_arr); i++) { -struct bpf_program **prog; - - prog = (struct bpf_program **)(((char *)skel) + stream_error_arr[i].prog_off); - prog_fd =bpf_program__fd (*prog); - ret =bpf_prog_test_run_opts (prog_fd, &opts); -ASSERT_OK (ret,"ret" ); -ASSERT_OK (opts.retval,"retval" ); - -#if !defined(__x86_64__) && !defined(__s390x__) && !defined(__aarch64__) -ASSERT_TRUE (1 ,"Timed may_goto unsupported, skip." ); -if (i ==0 ) { - ret =bpf_prog_stream_read (prog_fd,2 , buf,sizeof (buf), &ropts); -ASSERT_EQ (ret,0 ,"stream read" ); -continue ; - } -#endif - - ret =bpf_prog_stream_read (prog_fd,BPF_STREAM_STDERR , buf,sizeof (buf), &ropts); -ASSERT_GT (ret,0 ,"stream read" ); -ASSERT_LE (ret,1023 ,"len for buf" ); - buf[ret] ='\0' ; - - ret =match_regex (stream_error_arr[i].errstr, buf); -if (!ASSERT_TRUE (ret ==1 ,"regex match" )) -fprintf (stderr,"Output from stream:\n%s\n" , buf); - } - -stream__destroy (skel); -} -void test_stream_syscall (void ) {LIBBPF_OPTS (bpf_test_run_opts, opts);
tools/testing/selftests/bpf/progs/stream.c +17 -0
@@ -37,7 +37,15 @@ int stream_exhaust(void *ctx) } SEC("syscall") +__arch_x86_64 +__arch_arm64 +__arch_s390x __success __retval(0) +__stderr("ERROR: Timeout detected for may_goto instruction") +__stderr("CPU: {{[0-9]+}} UID: 0 PID: {{[0-9]+}} Comm: {{.*}}") +__stderr ("Call trace:\n" +"{{([a-zA-Z_][a-zA-Z0-9_]*\\+0x[0-9a-fA-F]+/0x[0-9a-fA-F]+\n" +"|[ \t]+[^\n]+\n)*}}")int stream_cond_break (void *ctx) {while (can_loop)
@@ -47,6 +55,15 @@ int stream_cond_break(void *ctx) SEC("syscall") __success __retval(0) +__stderr("ERROR: AA or ABBA deadlock detected for bpf_res_spin_lock") +__stderr("{{Attempted lock = (0x[0-9a-fA-F]+)\n" +"Total held locks = 1\n" +"Held lock\\[ 0\\] = \\1}}") +__stderr("...") +__stderr("CPU: {{[0-9]+}} UID: 0 PID: {{[0-9]+}} Comm: {{.*}}") +__stderr("Call trace:\n" +"{{([a-zA-Z_][a-zA-Z0-9_]*\\+0x[0-9a-fA-F]+/0x[0-9a-fA-F]+\n" +"|[ \t]+[^\n]+\n)*}}")int stream_deadlock (void *ctx) {struct bpf_res_spin_lock *lock, *nlock;
Signature · 1 line
2.47.3
Re: [PATCH bpf-next v7 5/6] selftests: bpf: use __stderr in stream error tests
Quoted 5 lines · Puranjay Mohan
Start using
__stderrdirectly in the bpf programs to test the reporting ofmay_gototimeout detection andspin_lockdead lock detection.Signed-off-by Puranjay Mohan <[email protected]>---
[...]
Puranjay Mohan
maintainerPATCH v7 6/6 selftests/bpf: Add tests for arena fault reporting
In reply to Puranjay MohanAdd selftests for testing the reporting of arena page faults through BPF streams. Two new bpf programs are added that read and write to an unmapped arena address and the fault reporting is verified in the userspace through streams.
The added bpf programs need to access the user_vm_start in struct bpf_arena, this is done by casting &arena to struct bpf_arena *, but barrier_var() is used on this ptr before accessing ptr->user_vm_start; to stop GCC from issuing an out-of-bound access due to the cast from smaller map struct to larger "struct bpf_arena"
--- .../testing/selftests/bpf/prog_tests/stream.c | 49 ++++++ tools/testing/selftests/bpf/progs/stream.c | 141 ++++++++++++++++++ 2 files changed, 190 insertions(+)
tools/testing/selftests/bpf/prog_tests/stream.c +49 -0
@@ -57,3 +57,52 @@ void test_stream_syscall(void)stream__destroy (skel); } + +static void test_address (struct bpf_program *prog,unsigned long *fault_addr_p) +{ +LIBBPF_OPTS (bpf_test_run_opts, opts); +LIBBPF_OPTS (bpf_prog_stream_read_opts, ropts); +int ret, prog_fd; +char fault_addr[64 ]; +char buf[1024 ]; + + prog_fd =bpf_program__fd (prog); + + ret =bpf_prog_test_run_opts (prog_fd, &opts); +ASSERT_OK (ret,"ret" ); +ASSERT_OK (opts.retval,"retval" ); + +sprintf (fault_addr,"0x%lx" , *fault_addr_p); + + ret =bpf_prog_stream_read (prog_fd,BPF_STREAM_STDERR , buf,sizeof (buf), &ropts); +ASSERT_GT (ret,0 ,"stream read" ); +ASSERT_LE (ret,1023 ,"len for buf" ); + buf[ret] ='\0' ; + +if (!ASSERT_HAS_SUBSTR (buf, fault_addr,"fault_addr" )) { +fprintf (stderr,"Output from stream:\n%s\n" , buf); +fprintf (stderr,"Fault Addr: %s\n" , fault_addr); + } +} + +void test_stream_arena_fault_address (void ) +{ +struct stream *skel; + +#if !defined(__x86_64__) && !defined(__aarch64__) +printf ("%s:SKIP: arena fault reporting not supported\n" , __func__); +test__skip (); +return ; +#endif + + skel =stream__open_and_load (); +if (!ASSERT_OK_PTR (skel,"stream__open_and_load" )) +return ; + +if (test__start_subtest ("read_fault" )) +test_address (skel->progs.stream_arena_read_fault, &skel->bss->fault_addr); +if (test__start_subtest ("write_fault" )) +test_address (skel->progs.stream_arena_write_fault, &skel->bss->fault_addr); + +stream__destroy (skel); +}
tools/testing/selftests/bpf/progs/stream.c +141 -0
161 lines not on this page · raw message
Signature · 1 line
2.47.3
Re: [PATCH bpf-next v7 6/6] selftests/bpf: Add tests for arena fault reporting
Quoted 5 lines · Puranjay Mohan
Add selftests for testing the reporting of arena page faults through BPF streams. Two new bpf programs are added that read and write to an unmapped arena address and the fault reporting is verified in the userspace through streams.
Quoted 8 lines
The added bpf programs need to access the
user_vm_startin structbpf_arena, this is done by casting &arena to structbpf_arena*, butbarrier_var()is used on this ptr before accessingptr->user_vm_start; to stop GCC from issuing an out-of-bound access due to the cast from smaller map struct to larger "structbpf_arena"Signed-off-by Puranjay Mohan <[email protected]>---
[...]
tools/testing/selftests/bpf/progs/stream.c int stream_syscall(void *ctx) · line 124
Quoted 9 lines
tools/testing/selftests/bpf/progs/stream.c +9
-0+SEC("syscall") +__arch_x86_64 +__arch_arm64 +__success __retval(0) +__stderr("ERROR: Arena WRITE access at unmapped address 0x{{.*}}") +__stderr("CPU: {{[0-9]+}} UID: 0 PID: {{[0-9]+}} Comm: {{.*}}") +__stderr ("Call trace:\n" +"{{([a-zA-Z_][a-zA-Z0-9_]*\\+0x[0-9a-fA-F]+/0x[0-9a-fA-F]+\n" +"|[ \t]+[^\n]+\n)*}}" )
Nit: here and in other tests, the regex is a bit hard to read.
How wrong would it be to write it down as follows:
__stderr("Call trace:")
__stderr("bpf_stream_stage_dump_stack+0x{{.*}}/0x{{.*}}")
?
(or at-least add a comment).[...]
Re: [PATCH bpf-next v7 6/6] selftests/bpf: Add tests for arena fault reporting
Quoted 32 lines · Eduard Zingerman
Quoted 5 lines · Puranjay Mohan
Add selftests for testing the reporting of arena page faults through BPF streams. Two new bpf programs are added that read and write to an unmapped arena address and the fault reporting is verified in the userspace through streams.
Quoted 8 lines
The added bpf programs need to access the
user_vm_startin structbpf_arena, this is done by casting &arena to structbpf_arena*, butbarrier_var()is used on this ptr before accessingptr->user_vm_start; to stop GCC from issuing an out-of-bound access due to the cast from smaller map struct to larger "structbpf_arena"Signed-off-by Puranjay Mohan <[email protected]>---
Acked-by Eduard Zingerman <[email protected]>[...]
tools/testing/selftests/bpf/progs/stream.c int stream_syscall(void *ctx) · line 124
Quoted 9 lines
tools/testing/selftests/bpf/progs/stream.c +9
-0+SEC("syscall") +__arch_x86_64 +__arch_arm64 +__success __retval(0) +__stderr("ERROR: Arena WRITE access at unmapped address 0x{{.*}}") +__stderr("CPU: {{[0-9]+}} UID: 0 PID: {{[0-9]+}} Comm: {{.*}}") +__stderr ("Call trace:\n" +"{{([a-zA-Z_][a-zA-Z0-9_]*\\+0x[0-9a-fA-F]+/0x[0-9a-fA-F]+\n" +"|[ \t]+[^\n]+\n)*}}" )Nit: here and in other tests, the regex is a bit hard to read.
Quoted 5 lines
How wrong would it be to write it down as follows:
__stderr("Call trace:")
__stderr("bpf_stream_stage_dump_stack+0x{{.*}}/0x{{.*}}")
?
(or at-least add a comment).Applied as-is.
Please send a follow up for this one.
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 9 lines · On Thu, 11 Sep 2025 14:57:59 +0000 you wrote:
Changes in
v6->v7:
v6: https://lore.kernel.org/all/[email protected]/
- Added comments about the usage ofarena_regin x86 and arm64 jits. (Alexei)
- Usedclear_lo32()for clearing the lower 32-bits ofuser_vm_start. (Alexei)
- Moved update of the old tests to use__stderrto a separate commit (Eduard)
- Usedtest__skip()inprog_tests/stream.c(Eduard)
- Start a sub-test for read / write[...]
Here is the summary with links:
- [bpf-next,v7,1/6] bpf: arm64: simplify exception table handling
https://git.kernel.org/bpf/bpf-next/c/0460484244e1 - [bpf-next,v7,2/6] bpf: core: introduce main_prog_aux for stream access https://git.kernel.org/bpf/bpf-next/c/70f23546d246 - [bpf-next,v7,3/6] bpf: Report arena faults to BPF stderr https://git.kernel.org/bpf/bpf-next/c/5c5240d02061 - [bpf-next,v7,4/6] selftests: bpf: introduce __stderr and __stdout https://git.kernel.org/bpf/bpf-next/c/744eeb2b27c2 - [bpf-next,v7,5/6] selftests: bpf: use __stderr in stream error tests https://git.kernel.org/bpf/bpf-next/c/edd03fcd7601 - [bpf-next,v7,6/6] selftests/bpf: Add tests for arena fault reporting https://git.kernel.org/bpf/bpf-next/c/86f2225065be
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html