Fixes for Exceptions
Subsystems (5)
Kumar Kartikeya Dwivedi
maintainerv2 cover Fixes for Exceptions
Set of fixes for bugs reported for the exceptions series.
Changelog:
----------
v1 -> v2
v1: https://lore.kernel.org/bpf/[email protected]
* Resend with trimmed Cc due to patchwork problem (Alexei)
Kumar Kartikeya Dwivedi (3):
selftests/bpf: Print log buffer for exceptions test only on failure
bpf: Fix bpf_throw warning on 32-bit arch
bpf: Disable exceptions when CONFIG_UNWINDER_FRAME_POINTER=y
arch/x86/net/bpf_jit_comp.c | 9 ++++----- kernel/bpf/helpers.c | 2 +- tools/testing/selftests/bpf/prog_tests/exceptions.c | 5 +++-- 3 files changed, 8 insertions(+), 8 deletions(-)
base-commit: ec6f1b4db95b7eedb3fe85f4f14e08fa0e9281c3
Signature · 1 line
2.41.0
Kumar Kartikeya Dwivedi
maintainerPATCH v2 2/3 bpf: Fix bpf_throw warning on 32-bit arch
On 32-bit architectures, the pointer width is 32-bit, while we try to cast from a u64 down to it, the compiler complains on mismatch in integer size. Fix this by first casting to long which should match the pointer width on targets supported by Linux.
--- kernel/bpf/helpers.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/bpf/helpers.c +1 -1
@@ -2488,7 +2488,7 @@ __bpf_kfunc void bpf_throw(u64 cookie)* deeper stack depths than ctx.sp as we do not return from bpf_throw, * which skips compiler generated instrumentation to do the same. */ -kasan_unpoison_task_stack_below ((void *)ctx.sp); +kasan_unpoison_task_stack_below ((void *)(long )ctx.sp); ctx.aux->bpf_exception_cb (cookie, ctx.sp, ctx.bp);WARN (1 ,"A call to BPF exception callback should never return\n" ); }
Signature · 1 line
2.41.0
Re: [PATCH bpf-next v2 3/3] bpf: Disable exceptions when CONFIG_UNWINDER_FRAME_POINTER=y
In reply to Kumar Kartikeya DwivediQuoted 10 lines · Kumar Kartikeya Dwivedi
The build with
CONFIG_UNWINDER_FRAME_POINTER=yis broken for current exceptions feature as it assumes ORC unwinder specific fields in theunwind_state. Disable exceptions whenframe_pointerunwinder is enabled for now.Fixes fd5d27b70188 ("arch/x86: Implement arch_bpf_stack_walk")Reported-by Eric Dumazet <[email protected]>Signed-off-by Kumar Kartikeya Dwivedi <[email protected]>---
SGTM, feel free adding
Kumar Kartikeya Dwivedi
maintainerPATCH v2 3/3 bpf: Disable exceptions when CONFIG_UNWINDER_FRAME_POINTER=y
In reply to Kumar Kartikeya DwivediThe build with CONFIG_UNWINDER_FRAME_POINTER=y is broken for current exceptions feature as it assumes ORC unwinder specific fields in the unwind_state. Disable exceptions when frame_pointer unwinder is enabled for now.
--- arch/x86/net/bpf_jit_comp.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-)
arch/x86/net/bpf_jit_comp.c +4 -5
@@ -3003,16 +3003,15 @@ void bpf_jit_free(struct bpf_prog *prog)bool bpf_jit_supports_exceptions (void ) {/* We unwind through both kernel frames (starting from within bpf_throw -* call) and BPF frames. Therefore we require one of ORC or FP unwinder -* to be enabled to walk kernel frames and reach BPF frames in the stack -* trace. +* call) and BPF frames. Therefore we require ORC unwinder to be enabled +* to walk kernel frames and reach BPF frames in the stack trace. */ -return IS_ENABLED (CONFIG_UNWINDER_ORC ) ||IS_ENABLED (CONFIG_UNWINDER_FRAME_POINTER ); +return IS_ENABLED (CONFIG_UNWINDER_ORC ); }void arch_bpf_stack_walk (bool (*consume_fn)(void *cookie,u64 ip,u64 sp,u64 bp),void *cookie) { -#if defined(CONFIG_UNWINDER_ORC ) || defined(CONFIG_UNWINDER_FRAME_POINTER ) +#if defined(CONFIG_UNWINDER_ORC )struct unwind_state state;unsigned long addr;
Signature · 1 line
2.41.0
Kumar Kartikeya Dwivedi
maintainerPATCH v2 1/3 selftests/bpf: Print log buffer for exceptions test only on failure
In reply to Kumar Kartikeya DwivediAlexei reported seeing log messages for some test cases even though we just wanted to match the error string from the verifier. Move the printing of the log buffer to a guarded condition so that we only print it when we fail to match on the expected string in the log buffer, preventing unneeded output when running the test.
--- tools/testing/selftests/bpf/prog_tests/exceptions.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
tools/testing/selftests/bpf/prog_tests/exceptions.c +3 -2
@@ -103,9 +103,10 @@ static void test_exceptions_success(void) goto done; \ } \if (load_ret !=0 ) { \ -printf ("%s\n" , log_buf); \ -if (!ASSERT_OK_PTR (strstr (log_buf, msg),"strstr" )) \ +if (!ASSERT_OK_PTR (strstr (log_buf, msg),"strstr" )) { \ +printf ("%s\n" , log_buf); \ goto done; \ + } \ } \if (!load_ret && attach_err) { \if (!ASSERT_ERR_PTR (link =bpf_program__attach (prog),"attach err" )) \
Signature · 1 line
2.41.0
Re: [PATCH bpf-next v2 2/3] bpf: Fix bpf_throw warning on 32-bit arch
In reply to Kumar Kartikeya DwivediHi Kumar,
Quoted 4 lines · Kumar Kartikeya Dwivedi
On 32-bit architectures, the pointer width is 32-bit, while we try to cast from a u64 down to it, the compiler complains on mismatch in integer size. Fix this by first casting to long which should match the pointer width on targets supported by Linux.
Thank you for the patch, it fixes the issue on our side!
(Not sure you need a tested by tag but just in case: )
Quoted 12 lines
Fixes ec5290a178b7 ("bpf: Prevent KASAN false positive with bpf_throw")Reported-by Matthieu Baerts <[email protected]>Signed-off-by Kumar Kartikeya Dwivedi <[email protected]>--- kernel/bpf/helpers.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)kernel/bpf/helpers.c +0
-0
kernel/bpf/helpers.c __bpf_kfunc void bpf_throw(u64 cookie) · line 2491
Quoted 5 lines
kernel/bpf/helpers.c +1
-1@@ -2488,7 +2488,7 @@ __bpf_kfunc void bpf_throw(u64 cookie)* deeper stack depths than ctx.sp as we do not return from bpf_throw, * which skips compiler generated instrumentation to do the same. */ -kasan_unpoison_task_stack_below ((void *)ctx.sp); +kasan_unpoison_task_stack_below ((void *)(long )ctx.sp);
I never know what's the recommended way to fix such issues: casting it to 'long' or 'unsigned long'? But it looks like both are used in the kernel and 'long' is more often used than the other one so all good I suppose.
kernel/bpf/helpers.c __bpf_kfunc void bpf_throw(u64 cookie) · line 2494
Quoted 3 lines
kernel/bpf/helpers.c +0
-0ctx.aux->bpf_exception_cb (cookie, ctx.sp, ctx.bp);WARN (1 ,"A call to BPF exception callback should never return\n" ); }
Cheers,
Matt
Signature · 2 lines
Tessares | Belgium | Hybrid Access Solutions www.tessares.net
Re: [PATCH bpf-next v2 2/3] bpf: Fix bpf_throw warning on 32-bit arch
Quoted 21 lines · Matthieu Baerts
Hi Kumar,
Quoted 4 lines · Kumar Kartikeya Dwivedi
On 32-bit architectures, the pointer width is 32-bit, while we try to cast from a u64 down to it, the compiler complains on mismatch in integer size. Fix this by first casting to long which should match the pointer width on targets supported by Linux.
Thank you for the patch, it fixes the issue on our side!
(Not sure you need a tested by tag but just in case: )
Tested-by Matthieu Baerts <[email protected]>Quoted 7 lines
Fixes ec5290a178b7 ("bpf: Prevent KASAN false positive with bpf_throw")Reported-by Matthieu Baerts <[email protected]>Signed-off-by Kumar Kartikeya Dwivedi <[email protected]>--- kernel/bpf/helpers.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 14 lines
kernel/bpf/helpers.c __bpf_kfunc void bpf_throw(u64 cookie) · line 2491
Quoted 10 lines
kernel/bpf/helpers.c +1
-1@@ -2488,7 +2488,7 @@ __bpf_kfunc void bpf_throw(u64 cookie)* deeper stack depths than ctx.sp as we do not return from bpf_throw, * which skips compiler generated instrumentation to do the same. */ -kasan_unpoison_task_stack_below ((void *)ctx.sp); +kasan_unpoison_task_stack_below ((void *)(long )ctx.sp);I never know what's the recommended way to fix such issues: casting it to 'long' or 'unsigned long'? But it looks like both are used in the kernel and 'long' is more often used than the other one so all good I suppose.
Shouldn't we have a macro to do this kind of casting if there is not? Without any comment, it is difficult to know why this extra casting is here.
Quoted 7 lines
kernel/bpf/helpers.c __bpf_kfunc void bpf_throw(u64 cookie) · line 2494
Quoted 4 lines
kernel/bpf/helpers.c +0
-0ctx.aux->bpf_exception_cb (cookie, ctx.sp, ctx.bp);WARN (1 ,"A call to BPF exception callback should never return\n" ); }Cheers,
Matt
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 8 lines · On Mon, 18 Sep 2023 17:52:30 +0200 you wrote:
Set of fixes for bugs reported for the exceptions series.
Changelog:
----------
v1 -> v2
v1: https://lore.kernel.org/bpf/[email protected][...]
Here is the summary with links:
- [bpf-next,v2,1/3] selftests/bpf: Print log buffer for exceptions test only on failure
https://git.kernel.org/bpf/bpf-next/c/c425a3501f24 - [bpf-next,v2,2/3] bpf: Fix bpf_throw warning on 32-bit arch https://git.kernel.org/bpf/bpf-next/c/00b7e8f4c02d - [bpf-next,v2,3/3] bpf: Disable exceptions when CONFIG_UNWINDER_FRAME_POINTER=y https://git.kernel.org/bpf/bpf-next/c/43c6e890472e
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html