[PATCH bpf-next 4/6] selftests/bpf: Allow one single recursion in fentry recursion test

From: Yafang Shao
Date: Mon Apr 17 2023 - 11:48:26 EST


This is a prepation for replacing prog->active with
test_recursion_{acquire,release}, in which one single recursion in the
process context is allowed. The behavior will be as follows,

SEC("fentry/htab_map_delete_elem")
pass2++; <<<< Turn out to be 1 after this operation.
bpf_map_delete_elem(&hash2, &key);
SEC("fentry/htab_map_delete_elem") <<<< not recursion
pass2++; <<<< Turn out to be 2 after this operation.
bpf_map_delete_elem(&hash2, &key);
SEC("fentry/htab_map_delete_elem") <<<< RECURSION

Hence we need to change the selftest to allow it. To be
backward-compatibility, we allow both the old value and the new value
to be expected, so a new helper ASSERT_IN_ARRAY() is introduced.

Signed-off-by: Yafang Shao <laoar.shao@xxxxxxxxx>
---
tools/testing/selftests/bpf/prog_tests/recursion.c | 7 +++++--
tools/testing/selftests/bpf/test_progs.h | 19 +++++++++++++++++++
2 files changed, 24 insertions(+), 2 deletions(-)

diff --git a/tools/testing/selftests/bpf/prog_tests/recursion.c b/tools/testing/selftests/bpf/prog_tests/recursion.c
index 23552d3..dfbed2e 100644
--- a/tools/testing/selftests/bpf/prog_tests/recursion.c
+++ b/tools/testing/selftests/bpf/prog_tests/recursion.c
@@ -8,6 +8,7 @@ void test_recursion(void)
struct bpf_prog_info prog_info = {};
__u32 prog_info_len = sizeof(prog_info);
struct recursion *skel;
+ int expected[2];
int key = 0;
int err;

@@ -27,9 +28,11 @@ void test_recursion(void)

ASSERT_EQ(skel->bss->pass2, 0, "pass2 == 0");
bpf_map_delete_elem(bpf_map__fd(skel->maps.hash2), &key);
- ASSERT_EQ(skel->bss->pass2, 1, "pass2 == 1");
+ expected[1] = 2;
+ ASSERT_IN_ARRAY(skel->bss->pass2, expected, "pass2 in [0 2]");
bpf_map_delete_elem(bpf_map__fd(skel->maps.hash2), &key);
- ASSERT_EQ(skel->bss->pass2, 2, "pass2 == 2");
+ expected[1] = 4;
+ ASSERT_IN_ARRAY(skel->bss->pass2, expected, "pass2 in [0 4]");

err = bpf_prog_get_info_by_fd(bpf_program__fd(skel->progs.on_delete),
&prog_info, &prog_info_len);
diff --git a/tools/testing/selftests/bpf/test_progs.h b/tools/testing/selftests/bpf/test_progs.h
index 10ba432..79e96cc 100644
--- a/tools/testing/selftests/bpf/test_progs.h
+++ b/tools/testing/selftests/bpf/test_progs.h
@@ -245,6 +245,25 @@ struct msg {
___ok; \
})

+#define ASSERT_IN_ARRAY(actual, expected, name) ({ \
+ static int duration; \
+ typeof(actual) ___act = (actual); \
+ typeof((expected)[0]) * ___exp = (expected); \
+ bool ___ok = false; \
+ int i; \
+ \
+ for (i = 0; i < ARRAY_SIZE(expected); i++) { \
+ if (___act == ___exp[i]) { \
+ ___ok = true; \
+ break; \
+ } \
+ } \
+ CHECK(!___ok, (name), \
+ "unexpected %s: actual %lld not in array\n", \
+ (name), (long long)(___act)); \
+ ___ok; \
+})
+
#define ASSERT_NEQ(actual, expected, name) ({ \
static int duration = 0; \
typeof(actual) ___act = (actual); \
--
1.8.3.1