-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
selftests/bpf: Add struct_ops prog private stack tests
Add three tests for struct_ops using private stack. ./test_progs -t struct_ops_private_stack #333/1 struct_ops_private_stack/private_stack:OK #333/2 struct_ops_private_stack/private_stack_fail:OK #333/3 struct_ops_private_stack/private_stack_recur:OK #333 struct_ops_private_stack:OK The following is a snippet of a struct_ops check_member() implementation: u32 moff = __btf_member_bit_offset(t, member) / 8; switch (moff) { case offsetof(struct bpf_testmod_ops3, test_1): prog->aux->use_priv_stack = true; prog->aux->recursion_detected = test_1_recursion_detected; fallthrough; default: break; } return 0; The first test is with nested two different callback functions where the first prog has more than 512 byte stack size (including subprogs) with private stack enabled. The second test is a negative test where the second prog has more than 512 byte stack size without private stack enabled. The third test is the same callback function recursing itself. At run time, the jit trampoline recursion check kicks in to prevent the recursion. The recursion_detected() callback function is implemented by the bpf_testmod, the following message in dmesg bpf_testmod: oh no, recursing into test_1, recursion_misses 1 demonstrates the callback function is indeed triggered when recursion miss happens. Signed-off-by: Yonghong Song <[email protected]>
- Loading branch information
Yonghong Song
authored and
Kernel Patches Daemon
committed
Nov 4, 2024
1 parent
ab0cb45
commit a4eca30
Showing
6 changed files
with
389 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
tools/testing/selftests/bpf/prog_tests/struct_ops_private_stack.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
// SPDX-License-Identifier: GPL-2.0 | ||
|
||
#include <test_progs.h> | ||
#include "struct_ops_private_stack.skel.h" | ||
#include "struct_ops_private_stack_fail.skel.h" | ||
#include "struct_ops_private_stack_recur.skel.h" | ||
|
||
static void test_private_stack(void) | ||
{ | ||
struct struct_ops_private_stack *skel; | ||
struct bpf_link *link; | ||
int err; | ||
|
||
skel = struct_ops_private_stack__open(); | ||
if (!ASSERT_OK_PTR(skel, "struct_ops_private_stack__open")) | ||
return; | ||
|
||
if (skel->data->skip) { | ||
test__skip(); | ||
goto cleanup; | ||
} | ||
|
||
err = struct_ops_private_stack__load(skel); | ||
if (!ASSERT_OK(err, "struct_ops_private_stack__load")) | ||
goto cleanup; | ||
|
||
link = bpf_map__attach_struct_ops(skel->maps.testmod_1); | ||
if (!ASSERT_OK_PTR(link, "attach_struct_ops")) | ||
goto cleanup; | ||
|
||
ASSERT_OK(trigger_module_test_read(256), "trigger_read"); | ||
|
||
ASSERT_EQ(skel->bss->val_i, 3, "val_i"); | ||
ASSERT_EQ(skel->bss->val_j, 8, "val_j"); | ||
|
||
bpf_link__destroy(link); | ||
|
||
cleanup: | ||
struct_ops_private_stack__destroy(skel); | ||
} | ||
|
||
static void test_private_stack_fail(void) | ||
{ | ||
struct struct_ops_private_stack_fail *skel; | ||
int err; | ||
|
||
skel = struct_ops_private_stack_fail__open(); | ||
if (!ASSERT_OK_PTR(skel, "struct_ops_private_stack_fail__open")) | ||
return; | ||
|
||
if (skel->data->skip) { | ||
test__skip(); | ||
goto cleanup; | ||
} | ||
|
||
err = struct_ops_private_stack_fail__load(skel); | ||
if (!ASSERT_ERR(err, "struct_ops_private_stack_fail__load")) | ||
goto cleanup; | ||
return; | ||
|
||
cleanup: | ||
struct_ops_private_stack_fail__destroy(skel); | ||
} | ||
|
||
static void test_private_stack_recur(void) | ||
{ | ||
struct struct_ops_private_stack_recur *skel; | ||
struct bpf_link *link; | ||
int err; | ||
|
||
skel = struct_ops_private_stack_recur__open(); | ||
if (!ASSERT_OK_PTR(skel, "struct_ops_private_stack_recur__open")) | ||
return; | ||
|
||
if (skel->data->skip) { | ||
test__skip(); | ||
goto cleanup; | ||
} | ||
|
||
err = struct_ops_private_stack_recur__load(skel); | ||
if (!ASSERT_OK(err, "struct_ops_private_stack_recur__load")) | ||
goto cleanup; | ||
|
||
link = bpf_map__attach_struct_ops(skel->maps.testmod_1); | ||
if (!ASSERT_OK_PTR(link, "attach_struct_ops")) | ||
goto cleanup; | ||
|
||
ASSERT_OK(trigger_module_test_read(256), "trigger_read"); | ||
|
||
ASSERT_EQ(skel->bss->val_j, 3, "val_j"); | ||
|
||
bpf_link__destroy(link); | ||
|
||
cleanup: | ||
struct_ops_private_stack_recur__destroy(skel); | ||
} | ||
|
||
void test_struct_ops_private_stack(void) | ||
{ | ||
if (test__start_subtest("private_stack")) | ||
test_private_stack(); | ||
if (test__start_subtest("private_stack_fail")) | ||
test_private_stack_fail(); | ||
if (test__start_subtest("private_stack_recur")) | ||
test_private_stack_recur(); | ||
} |
62 changes: 62 additions & 0 deletions
62
tools/testing/selftests/bpf/progs/struct_ops_private_stack.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// SPDX-License-Identifier: GPL-2.0 | ||
|
||
#include <vmlinux.h> | ||
#include <bpf/bpf_helpers.h> | ||
#include <bpf/bpf_tracing.h> | ||
#include "../bpf_testmod/bpf_testmod.h" | ||
|
||
char _license[] SEC("license") = "GPL"; | ||
|
||
#if defined(__TARGET_ARCH_x86) | ||
bool skip __attribute((__section__(".data"))) = false; | ||
#else | ||
bool skip = true; | ||
#endif | ||
|
||
void bpf_testmod_ops3_call_test_2(void) __ksym; | ||
|
||
int val_i, val_j; | ||
|
||
__noinline static int subprog2(int *a, int *b) | ||
{ | ||
return val_i + a[10] + b[20]; | ||
} | ||
|
||
__noinline static int subprog1(int *a) | ||
{ | ||
/* stack size 200 bytes */ | ||
int b[50] = {}; | ||
|
||
b[20] = 2; | ||
return subprog2(a, b); | ||
} | ||
|
||
|
||
SEC("struct_ops") | ||
int BPF_PROG(test_1) | ||
{ | ||
/* stack size 400 bytes */ | ||
int a[100] = {}; | ||
|
||
a[10] = 1; | ||
val_i = subprog1(a); | ||
bpf_testmod_ops3_call_test_2(); | ||
return 0; | ||
} | ||
|
||
SEC("struct_ops") | ||
int BPF_PROG(test_2) | ||
{ | ||
/* stack size 200 bytes */ | ||
int a[50] = {}; | ||
|
||
a[10] = 3; | ||
val_j = subprog1(a); | ||
return 0; | ||
} | ||
|
||
SEC(".struct_ops") | ||
struct bpf_testmod_ops3 testmod_1 = { | ||
.test_1 = (void *)test_1, | ||
.test_2 = (void *)test_2, | ||
}; |
62 changes: 62 additions & 0 deletions
62
tools/testing/selftests/bpf/progs/struct_ops_private_stack_fail.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// SPDX-License-Identifier: GPL-2.0 | ||
|
||
#include <vmlinux.h> | ||
#include <bpf/bpf_helpers.h> | ||
#include <bpf/bpf_tracing.h> | ||
#include "../bpf_testmod/bpf_testmod.h" | ||
|
||
char _license[] SEC("license") = "GPL"; | ||
|
||
#if defined(__TARGET_ARCH_x86) | ||
bool skip __attribute((__section__(".data"))) = false; | ||
#else | ||
bool skip = true; | ||
#endif | ||
|
||
void bpf_testmod_ops3_call_test_2(void) __ksym; | ||
|
||
int val_i, val_j; | ||
|
||
__noinline static int subprog2(int *a, int *b) | ||
{ | ||
return val_i + a[10] + b[20]; | ||
} | ||
|
||
__noinline static int subprog1(int *a) | ||
{ | ||
/* stack size 200 bytes */ | ||
int b[50] = {}; | ||
|
||
b[20] = 2; | ||
return subprog2(a, b); | ||
} | ||
|
||
|
||
SEC("struct_ops") | ||
int BPF_PROG(test_1) | ||
{ | ||
/* stack size 100 bytes */ | ||
int a[25] = {}; | ||
|
||
a[10] = 1; | ||
val_i = subprog1(a); | ||
bpf_testmod_ops3_call_test_2(); | ||
return 0; | ||
} | ||
|
||
SEC("struct_ops") | ||
int BPF_PROG(test_2) | ||
{ | ||
/* stack size 400 bytes */ | ||
int a[100] = {}; | ||
|
||
a[10] = 3; | ||
val_j = subprog1(a); | ||
return 0; | ||
} | ||
|
||
SEC(".struct_ops") | ||
struct bpf_testmod_ops3 testmod_1 = { | ||
.test_1 = (void *)test_1, | ||
.test_2 = (void *)test_2, | ||
}; |
Oops, something went wrong.