This patch tests bpf graphs storing kernel objects. Signed-off-by: Amery Hung <amery.hung@xxxxxxxxxxxxx> --- .../selftests/bpf/bpf_testmod/bpf_testmod.c | 14 +++++++++ .../selftests/bpf/bpf_testmod/bpf_testmod.h | 5 ++++ .../selftests/bpf/prog_tests/linked_list.c | 6 ++-- .../testing/selftests/bpf/progs/linked_list.c | 15 ++++++++++ .../testing/selftests/bpf/progs/linked_list.h | 8 +++++ .../selftests/bpf/progs/linked_list_fail.c | 29 +++++++++++++++++++ 6 files changed, 75 insertions(+), 2 deletions(-) diff --git a/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c b/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c index 097a8d1c2ef8..90dda6335c04 100644 --- a/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c +++ b/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c @@ -494,6 +494,18 @@ __bpf_kfunc static u32 bpf_kfunc_call_test_static_unused_arg(u32 arg, u32 unused return arg; } +__bpf_kfunc static struct bpf_testmod_linked_list_obj * +bpf_kfunc_call_test_acq_linked_list_obj(void) +{ + return kzalloc(sizeof(struct bpf_testmod_linked_list_obj), GFP_ATOMIC); +} + +__bpf_kfunc static void +bpf_kfunc_call_test_rel_linked_list_obj(struct bpf_testmod_linked_list_obj *obj) +{ + kvfree(obj); +} + BTF_KFUNCS_START(bpf_testmod_check_kfunc_ids) BTF_ID_FLAGS(func, bpf_testmod_test_mod_kfunc) BTF_ID_FLAGS(func, bpf_kfunc_call_test1) @@ -520,6 +532,8 @@ BTF_ID_FLAGS(func, bpf_kfunc_call_test_ref, KF_TRUSTED_ARGS | KF_RCU) BTF_ID_FLAGS(func, bpf_kfunc_call_test_destructive, KF_DESTRUCTIVE) BTF_ID_FLAGS(func, bpf_kfunc_call_test_static_unused_arg) BTF_ID_FLAGS(func, bpf_kfunc_call_test_offset) +BTF_ID_FLAGS(func, bpf_kfunc_call_test_acq_linked_list_obj, KF_ACQUIRE | KF_RET_NULL) +BTF_ID_FLAGS(func, bpf_kfunc_call_test_rel_linked_list_obj, KF_RELEASE) BTF_KFUNCS_END(bpf_testmod_check_kfunc_ids) static int bpf_testmod_ops_init(struct btf *btf) diff --git a/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.h b/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.h index 6d24e1307b64..77c36fc016e3 100644 --- a/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.h +++ b/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.h @@ -99,4 +99,9 @@ struct bpf_testmod_ops2 { int (*test_1)(void); }; +struct bpf_testmod_linked_list_obj { + int val; + struct bpf_list_node node; +}; + #endif /* _BPF_TESTMOD_H */ diff --git a/tools/testing/selftests/bpf/prog_tests/linked_list.c b/tools/testing/selftests/bpf/prog_tests/linked_list.c index 2fb89de63bd2..813c2e9a2346 100644 --- a/tools/testing/selftests/bpf/prog_tests/linked_list.c +++ b/tools/testing/selftests/bpf/prog_tests/linked_list.c @@ -80,8 +80,8 @@ static struct { { "direct_write_node", "direct access to bpf_list_node is disallowed" }, { "use_after_unlock_push_front", "invalid mem access 'scalar'" }, { "use_after_unlock_push_back", "invalid mem access 'scalar'" }, - { "double_push_front", "arg#1 expected pointer to allocated object" }, - { "double_push_back", "arg#1 expected pointer to allocated object" }, + { "double_push_front", "arg#1 expected pointer to allocated object or trusted pointer" }, + { "double_push_back", "arg#1 expected pointer to allocated object or trusted pointer" }, { "no_node_value_type", "bpf_list_node not found at offset=0" }, { "incorrect_value_type", "operation on bpf_list_head expects arg#1 bpf_list_node at offset=48 in struct foo, " @@ -96,6 +96,8 @@ static struct { { "incorrect_head_off2", "bpf_list_head not found at offset=1" }, { "pop_front_off", "off 48 doesn't point to 'struct bpf_spin_lock' that is at 40" }, { "pop_back_off", "off 48 doesn't point to 'struct bpf_spin_lock' that is at 40" }, + { "direct_write_node_kernel", "" }, + { "push_local_node_to_kptr_list", "operation on bpf_list_head expects arg#1 bpf_list_node at offset=8 in struct bpf_testmod_linked_list_obj, but arg is at offset=8 in struct bpf_testmod_linked_list_obj" }, }; static void test_linked_list_fail_prog(const char *prog_name, const char *err_msg) diff --git a/tools/testing/selftests/bpf/progs/linked_list.c b/tools/testing/selftests/bpf/progs/linked_list.c index 26205ca80679..148ec67feaf7 100644 --- a/tools/testing/selftests/bpf/progs/linked_list.c +++ b/tools/testing/selftests/bpf/progs/linked_list.c @@ -378,4 +378,19 @@ int global_list_in_list(void *ctx) return test_list_in_list(&glock, &ghead); } +SEC("tc") +int push_to_kptr_list(void *ctx) +{ + struct bpf_testmod_linked_list_obj *f; + + f = bpf_kfunc_call_test_acq_linked_list_obj(); + if (!f) + return 0; + + bpf_spin_lock(&glock3); + bpf_list_push_back(&ghead3, &f->node); + bpf_spin_unlock(&glock3); + return 0; +} + char _license[] SEC("license") = "GPL"; diff --git a/tools/testing/selftests/bpf/progs/linked_list.h b/tools/testing/selftests/bpf/progs/linked_list.h index c0f3609a7ffa..14bd92cfdb6f 100644 --- a/tools/testing/selftests/bpf/progs/linked_list.h +++ b/tools/testing/selftests/bpf/progs/linked_list.h @@ -5,6 +5,7 @@ #include <vmlinux.h> #include <bpf/bpf_helpers.h> #include "bpf_experimental.h" +#include "../bpf_testmod/bpf_testmod.h" struct bar { struct bpf_list_node node; @@ -52,5 +53,12 @@ struct { private(A) struct bpf_spin_lock glock; private(A) struct bpf_list_head ghead __contains(foo, node2); private(B) struct bpf_spin_lock glock2; +private(C) struct bpf_spin_lock glock3; +private(C) struct bpf_list_head ghead3 __contains_kptr(bpf_testmod_linked_list_obj, node); + +struct bpf_testmod_linked_list_obj *bpf_kfunc_call_test_acq_linked_list_obj(void) __ksym; +void bpf_kfunc_call_test_rel_linked_list_obj(struct bpf_testmod_linked_list_obj *obj) __ksym; +struct bpf_testmod_rb_tree_obj *bpf_kfunc_call_test_acq_rb_tree_obj(void) __ksym; +void bpf_kfunc_call_test_rel_rb_tree_obj(struct bpf_testmod_rb_tree_obj *obj) __ksym; #endif diff --git a/tools/testing/selftests/bpf/progs/linked_list_fail.c b/tools/testing/selftests/bpf/progs/linked_list_fail.c index 6438982b928b..5f8063ecc448 100644 --- a/tools/testing/selftests/bpf/progs/linked_list_fail.c +++ b/tools/testing/selftests/bpf/progs/linked_list_fail.c @@ -609,4 +609,33 @@ int pop_back_off(void *ctx) return pop_ptr_off((void *)bpf_list_pop_back); } +SEC("?tc") +int direct_write_node_kernel(void *ctx) +{ + struct bpf_testmod_linked_list_obj *f; + + f = bpf_kfunc_call_test_acq_linked_list_obj(); + if (!f) + return 0; + + *(__u64 *)&f->node = 0; + bpf_kfunc_call_test_rel_linked_list_obj(f); + return 0; +} + +SEC("?tc") +int push_local_node_to_kptr_list(void *ctx) +{ + struct bpf_testmod_linked_list_obj *f; + + f = bpf_obj_new(typeof(*f)); + if (!f) + return 0; + + bpf_spin_lock(&glock3); + bpf_list_push_back(&ghead3, &f->node); + bpf_spin_unlock(&glock3); + return 0; +} + char _license[] SEC("license") = "GPL"; -- 2.20.1