Skip to content

Commit 63ae8eb

Browse files
davemarchevskyborkmann
authored andcommitted
selftests/bpf: Add CO-RE relocs kfunc flavors tests
This patch adds selftests that exercise kfunc flavor relocation functionality added in the previous patch. The actual kfunc defined in kernel/bpf/helpers.c is: struct task_struct *bpf_task_acquire(struct task_struct *p) The following relocation behaviors are checked: struct task_struct *bpf_task_acquire___one(struct task_struct *name) * Should succeed despite differing param name struct task_struct *bpf_task_acquire___two(struct task_struct *p, void *ctx) * Should fail because there is no two-param bpf_task_acquire struct task_struct *bpf_task_acquire___three(void *ctx) * Should fail because, despite vmlinux's bpf_task_acquire having one param, the types don't match Signed-off-by: Dave Marchevsky <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]> Acked-by: David Vernet <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 5964a22 commit 63ae8eb

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

tools/testing/selftests/bpf/prog_tests/task_kfunc.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ static const char * const success_tests[] = {
7979
"test_task_from_pid_current",
8080
"test_task_from_pid_invalid",
8181
"task_kfunc_acquire_trusted_walked",
82+
"test_task_kfunc_flavor_relo",
83+
"test_task_kfunc_flavor_relo_not_found",
8284
};
8385

8486
void test_task_kfunc(void)

tools/testing/selftests/bpf/progs/task_kfunc_success.c

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ int err, pid;
1818
*/
1919

2020
struct task_struct *bpf_task_acquire(struct task_struct *p) __ksym __weak;
21+
22+
struct task_struct *bpf_task_acquire___one(struct task_struct *task) __ksym __weak;
23+
/* The two-param bpf_task_acquire doesn't exist */
24+
struct task_struct *bpf_task_acquire___two(struct task_struct *p, void *ctx) __ksym __weak;
25+
/* Incorrect type for first param */
26+
struct task_struct *bpf_task_acquire___three(void *ctx) __ksym __weak;
27+
2128
void invalid_kfunc(void) __ksym __weak;
2229
void bpf_testmod_test_mod_kfunc(int i) __ksym __weak;
2330

@@ -55,6 +62,50 @@ static int test_acquire_release(struct task_struct *task)
5562
return 0;
5663
}
5764

65+
SEC("tp_btf/task_newtask")
66+
int BPF_PROG(test_task_kfunc_flavor_relo, struct task_struct *task, u64 clone_flags)
67+
{
68+
struct task_struct *acquired = NULL;
69+
int fake_ctx = 42;
70+
71+
if (bpf_ksym_exists(bpf_task_acquire___one)) {
72+
acquired = bpf_task_acquire___one(task);
73+
} else if (bpf_ksym_exists(bpf_task_acquire___two)) {
74+
/* Here, bpf_object__resolve_ksym_func_btf_id's find_ksym_btf_id
75+
* call will find vmlinux's bpf_task_acquire, but subsequent
76+
* bpf_core_types_are_compat will fail
77+
*/
78+
acquired = bpf_task_acquire___two(task, &fake_ctx);
79+
err = 3;
80+
return 0;
81+
} else if (bpf_ksym_exists(bpf_task_acquire___three)) {
82+
/* bpf_core_types_are_compat will fail similarly to above case */
83+
acquired = bpf_task_acquire___three(&fake_ctx);
84+
err = 4;
85+
return 0;
86+
}
87+
88+
if (acquired)
89+
bpf_task_release(acquired);
90+
else
91+
err = 5;
92+
return 0;
93+
}
94+
95+
SEC("tp_btf/task_newtask")
96+
int BPF_PROG(test_task_kfunc_flavor_relo_not_found, struct task_struct *task, u64 clone_flags)
97+
{
98+
/* Neither symbol should successfully resolve.
99+
* Success or failure of one ___flavor should not affect others
100+
*/
101+
if (bpf_ksym_exists(bpf_task_acquire___two))
102+
err = 1;
103+
else if (bpf_ksym_exists(bpf_task_acquire___three))
104+
err = 2;
105+
106+
return 0;
107+
}
108+
58109
SEC("tp_btf/task_newtask")
59110
int BPF_PROG(test_task_acquire_release_argument, struct task_struct *task, u64 clone_flags)
60111
{

0 commit comments

Comments
 (0)