Skip to contentSkip to conversation

Fixes for Exceptions

PATCH Superseded Kumar Kartikeya Dwivedi 2 participants 6 messages Sep 18, 2023

Subsystems (5)
x86 Kselftest BPF BPF selftests +1 more subsystems

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Kumar Kartikeya Dwivedi

author maintainer
#0 lore raw

cover Fixes for Exceptions

Set of fixes for bugs reported for the exceptions series.

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
author maintainer
#1 lore raw

PATCH 1/3 selftests/bpf: Print log buffer for exceptions test only on failure

Alexei 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.

Fixes d2a93715bfb0 ("selftests/bpf: Add tests for BPF exceptions")
---
 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
index 5663e427dc00..516f4a13013c 100644
@@ -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
author maintainer
#2 lore raw

PATCH 2/3 bpf: Fix bpf_throw warning on 32-bit arch

In reply to 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.

Fixes ec5290a178b7 ("bpf: Prevent KASAN false positive with bpf_throw")
---
 kernel/bpf/helpers.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/bpf/helpers.c +1 -1
index 7ff2a42f1996..dd1c69ee3375 100644
@@ -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
author maintainer
#3 lore raw

PATCH 3/3 bpf: Disable exceptions when CONFIG_UNWINDER_FRAME_POINTER=y

In reply to Kumar Kartikeya Dwivedi

The 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.

Fixes fd5d27b70188 ("arch/x86: Implement arch_bpf_stack_walk")
---
 arch/x86/net/bpf_jit_comp.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
arch/x86/net/bpf_jit_comp.c +4 -5
index 84005f2114e0..8c10d9abc239 100644
@@ -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
#4 lore raw

Re: [PATCH bpf-next v1 3/3] bpf: Disable exceptions when CONFIG_UNWINDER_FRAME_POINTER=y

Quoted 9 lines · Kumar Kartikeya Dwivedi

The 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.

Fixes fd5d27b70188 ("arch/x86: Implement arch_bpf_stack_walk")

Very weird that only this patch reached patchwork. Could you resend and trim cc list significantly?

author maintainer
#5 lore raw

Re: [PATCH bpf-next v1 3/3] bpf: Disable exceptions when CONFIG_UNWINDER_FRAME_POINTER=y

Quoted 9 lines · Alexei Starovoitov
Quoted 6 lines · Kumar Kartikeya Dwivedi

The 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.

Quoted 6 lines
Quoted 3 lines
Fixes fd5d27b70188 ("arch/x86: Implement arch_bpf_stack_walk")

Very weird that only this patch reached patchwork. Could you resend and trim cc list significantly?

I have resent it with just BPF and you in To:.
But I think it's not with this patch, a lot of emails are not appearing on the list.