Skip to content

Commit be46084

Browse files
kkdwvdKernel Patches Daemon
authored andcommitted
selftests/bpf: Add tests for KF_RET_RCU
Add a couple of test cases to ensure RCU protection is kicked in automatically, and the return type is as expected. Signed-off-by: Kumar Kartikeya Dwivedi <[email protected]>
1 parent 636fd98 commit be46084

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,3 +123,26 @@ int iter_next_ptr_mem_not_trusted(const void *ctx)
123123
bpf_iter_num_destroy(&num_it);
124124
return 0;
125125
}
126+
127+
SEC("?fentry.s/" SYS_PREFIX "sys_getpgid")
128+
__failure __msg("kernel func bpf_kfunc_ret_rcu_test requires RCU critical section protection")
129+
int iter_ret_rcu_test_protected(const void *ctx)
130+
{
131+
struct task_struct *p;
132+
133+
p = bpf_kfunc_ret_rcu_test();
134+
return p->pid;
135+
}
136+
137+
SEC("?fentry.s/" SYS_PREFIX "sys_getpgid")
138+
__failure __msg("R1 type=rcu_ptr_or_null_ expected=")
139+
int iter_ret_rcu_test_type(const void *ctx)
140+
{
141+
struct task_struct *p;
142+
143+
bpf_rcu_read_lock();
144+
p = bpf_kfunc_ret_rcu_test();
145+
bpf_this_cpu_ptr(p);
146+
bpf_rcu_read_unlock();
147+
return 0;
148+
}

tools/testing/selftests/bpf/test_kmods/bpf_testmod.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,11 @@ __bpf_kfunc void bpf_kfunc_rcu_task_test(struct task_struct *ptr)
218218
{
219219
}
220220

221+
__bpf_kfunc struct task_struct *bpf_kfunc_ret_rcu_test(void)
222+
{
223+
return NULL;
224+
}
225+
221226
__bpf_kfunc struct bpf_testmod_ctx *
222227
bpf_testmod_ctx_create(int *err)
223228
{
@@ -623,6 +628,7 @@ BTF_ID_FLAGS(func, bpf_kfunc_trusted_vma_test, KF_TRUSTED_ARGS)
623628
BTF_ID_FLAGS(func, bpf_kfunc_trusted_task_test, KF_TRUSTED_ARGS)
624629
BTF_ID_FLAGS(func, bpf_kfunc_trusted_num_test, KF_TRUSTED_ARGS)
625630
BTF_ID_FLAGS(func, bpf_kfunc_rcu_task_test, KF_RCU)
631+
BTF_ID_FLAGS(func, bpf_kfunc_ret_rcu_test, KF_RET_NULL | KF_RET_RCU)
626632
BTF_ID_FLAGS(func, bpf_testmod_ctx_create, KF_ACQUIRE | KF_RET_NULL)
627633
BTF_ID_FLAGS(func, bpf_testmod_ctx_release, KF_RELEASE)
628634
BTF_ID_FLAGS(func, bpf_testmod_ops3_call_test_1)

tools/testing/selftests/bpf/test_kmods/bpf_testmod_kfunc.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ void bpf_kfunc_trusted_vma_test(struct vm_area_struct *ptr) __ksym;
158158
void bpf_kfunc_trusted_task_test(struct task_struct *ptr) __ksym;
159159
void bpf_kfunc_trusted_num_test(int *ptr) __ksym;
160160
void bpf_kfunc_rcu_task_test(struct task_struct *ptr) __ksym;
161+
struct task_struct *bpf_kfunc_ret_rcu_test(void) __ksym;
161162

162163
int bpf_kfunc_multi_st_ops_test_1(struct st_ops_args *args, u32 id) __ksym;
163164

0 commit comments

Comments
 (0)