Skip to contentSkip to conversation

selftests/bpf: make multi-uprobe tests work in RELEASE=1 mode

PATCH Superseded Andrii Nakryiko 3 participants 3 messages Mar 29, 2024

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Andrii Nakryiko

author maintainer
#0 lore raw

PATCH 1/1 selftests/bpf: make multi-uprobe tests work in RELEASE=1 mode

When BPF selftests are built in RELEASE=1 mode with -O2 optimization level, uprobe_multi binary, called from multi-uprobe tests is optimized to the point that all the thousands of target uprobe_multi_func_XXX functions are eliminated, breaking tests.

So ensure they are preserved through noinline + asm volatile trick.

But, actually, compiling uprobe_multi binary with -O2 takes a really long time, and is quite useless (it's not a benchmark). So in addition to ensuring that uprobe_multi_func_XXX functions are preserved, opt-out of -O2 explicitly in Makefile and stick to -O0. This saves a lot of compilation time.

---
 tools/testing/selftests/bpf/Makefile       | 2 +-
 tools/testing/selftests/bpf/uprobe_multi.c | 4 +++-
 2 files changed, 4 insertions(+), 2 deletions(-)
tools/testing/selftests/bpf/Makefile +1 -1
index 3b9eb40d6343..b0ac3dd80acf 100644
@@ -759,7 +759,7 @@ $(OUTPUT)/veristat: $(OUTPUT)/veristat.o
 
 $(OUTPUT)/uprobe_multi: uprobe_multi.c
 	$(call msg,BINARY,,$@)
-	$(Q)$(CC) $(CFLAGS) $(LDFLAGS) $^ $(LDLIBS) -o $@
+	$(Q)$(CC) $(CFLAGS) -O0 $(LDFLAGS) $^ $(LDLIBS) -o $@
 
 EXTRA_CLEAN := $(SCRATCH_DIR) $(HOST_SCRATCH_DIR)			\
 	prog_tests/tests.h map_tests/tests.h verifier/tests.h		\
tools/testing/selftests/bpf/uprobe_multi.c +3 -1
index a61ceab60b68..e38228563766 100644
@@ -9,7 +9,9 @@
 
 #define NAME(name, idx) PASTE(name, idx)
 
-#define DEF(name, idx)  int NAME(name, idx)(void) { return 0; }
+#define DEF(name, idx) \
+	int __attribute__((noinline)) \
+	NAME(name, idx)(void) { asm volatile (""); return 0; }
 #define CALL(name, idx) NAME(name, idx)();
 
 #define F(body, name, idx) body(name, idx)
Signature · 1 line
2.43.0
#1 lore raw
Quoted 38 lines · Andrii Nakryiko

When BPF selftests are built in RELEASE=1 mode with -O2 optimization level, uprobe_multi binary, called from multi-uprobe tests is optimized to the point that all the thousands of target uprobe_multi_func_XXX functions are eliminated, breaking tests.

So ensure they are preserved through noinline + asm volatile trick.

But, actually, compiling uprobe_multi binary with -O2 takes a really long time, and is quite useless (it's not a benchmark). So in addition to ensuring that uprobe_multi_func_XXX functions are preserved, opt-out of -O2 explicitly in Makefile and stick to -O0. This saves a lot of compilation time.

---
 tools/testing/selftests/bpf/Makefile       | 2 +-
 tools/testing/selftests/bpf/uprobe_multi.c | 4 +++-
 2 files changed, 4 insertions(+), 2 deletions(-)
tools/testing/selftests/bpf/Makefile +1 -1
index 3b9eb40d6343..b0ac3dd80acf 100644
@@ -759,7 +759,7 @@ $(OUTPUT)/veristat: $(OUTPUT)/veristat.o
 
 $(OUTPUT)/uprobe_multi: uprobe_multi.c
        $(call msg,BINARY,,$@)
-       $(Q)$(CC) $(CFLAGS) $(LDFLAGS) $^ $(LDLIBS) -o $@
+       $(Q)$(CC) $(CFLAGS) -O0 $(LDFLAGS) $^ $(LDLIBS) -o $@
 
 EXTRA_CLEAN := $(SCRATCH_DIR) $(HOST_SCRATCH_DIR)                      \
        prog_tests/tests.h map_tests/tests.h verifier/tests.h           \
tools/testing/selftests/bpf/uprobe_multi.c +0 -0
index a61ceab60b68..e38228563766 100644

tools/testing/selftests/bpf/uprobe_multi.c line 14

Quoted 7 lines
tools/testing/selftests/bpf/uprobe_multi.c +3 -1
@@ -9,7 +9,9 @@
 
 #define NAME(name, idx) PASTE(name, idx)
 
-#define DEF(name, idx)  int NAME(name, idx)(void) { return 0; }
+#define DEF(name, idx) \
+       int __attribute__((noinline)) \
+       NAME(name, idx)(void) { asm volatile (""); return 0; }

I prefer to use __weak to avoid inlining and compiler optimizations. It's less work for compiler as well,
since noinline doesn't tell compiler to stop analyzing the body of the function, while __weak does have such an effect. And asm won't be necessary as well.

#2 lore raw
Quoted 43 lines · Alexei Starovoitov
Quoted 41 lines · Andrii Nakryiko

When BPF selftests are built in RELEASE=1 mode with -O2 optimization level, uprobe_multi binary, called from multi-uprobe tests is optimized to the point that all the thousands of target uprobe_multi_func_XXX functions are eliminated, breaking tests.

So ensure they are preserved through noinline + asm volatile trick.

But, actually, compiling uprobe_multi binary with -O2 takes a really long time, and is quite useless (it's not a benchmark). So in addition to ensuring that uprobe_multi_func_XXX functions are preserved, opt-out of -O2 explicitly in Makefile and stick to -O0. This saves a lot of compilation time.

---
 tools/testing/selftests/bpf/Makefile       | 2 +-
 tools/testing/selftests/bpf/uprobe_multi.c | 4 +++-
 2 files changed, 4 insertions(+), 2 deletions(-)
tools/testing/selftests/bpf/Makefile +1 -1
index 3b9eb40d6343..b0ac3dd80acf 100644
@@ -759,7 +759,7 @@ $(OUTPUT)/veristat: $(OUTPUT)/veristat.o
 
 $(OUTPUT)/uprobe_multi: uprobe_multi.c
        $(call msg,BINARY,,$@)
-       $(Q)$(CC) $(CFLAGS) $(LDFLAGS) $^ $(LDLIBS) -o $@
+       $(Q)$(CC) $(CFLAGS) -O0 $(LDFLAGS) $^ $(LDLIBS) -o $@
 
 EXTRA_CLEAN := $(SCRATCH_DIR) $(HOST_SCRATCH_DIR)                      \
        prog_tests/tests.h map_tests/tests.h verifier/tests.h           \
tools/testing/selftests/bpf/uprobe_multi.c +0 -0
index a61ceab60b68..e38228563766 100644
@@ -9,7 +9,9 @@
 
 #define NAME(name, idx) PASTE(name, idx)
 
Quoted 10 lines

tools/testing/selftests/bpf/uprobe_multi.c line 14

Quoted 4 lines
tools/testing/selftests/bpf/uprobe_multi.c +3 -1
-#define DEF(name, idx)  int NAME(name, idx)(void) { return 0; }
+#define DEF(name, idx) \
+       int __attribute__((noinline)) \
+       NAME(name, idx)(void) { asm volatile (""); return 0; }

I prefer to use __weak to avoid inlining and compiler optimizations. It's less work for compiler as well,
since noinline doesn't tell compiler to stop analyzing the body of the function, while __weak does have such an effect. And asm won't be necessary as well.

makes sense, will do __weak, maybe it will make -O0 unnecessary as well