Skip to content

Commit 8b788d6

Browse files
kkdwivediAlexei Starovoitov
authored andcommitted
selftests/bpf: Add tests for KF_RCU_PROTECTED
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]> Acked-by: Eduard Zingerman <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent 1512231 commit 8b788d6

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

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

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,3 +123,49 @@ 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+
}
149+
150+
SEC("?fentry.s/" SYS_PREFIX "sys_getpgid")
151+
__failure __msg("kernel func bpf_kfunc_ret_rcu_test_nostruct requires RCU critical section protection")
152+
int iter_ret_rcu_test_protected_nostruct(const void *ctx)
153+
{
154+
void *p;
155+
156+
p = bpf_kfunc_ret_rcu_test_nostruct(4);
157+
return *(int *)p;
158+
}
159+
160+
SEC("?fentry.s/" SYS_PREFIX "sys_getpgid")
161+
__failure __msg("R1 type=rdonly_rcu_mem_or_null expected=")
162+
int iter_ret_rcu_test_type_nostruct(const void *ctx)
163+
{
164+
void *p;
165+
166+
bpf_rcu_read_lock();
167+
p = bpf_kfunc_ret_rcu_test_nostruct(4);
168+
bpf_this_cpu_ptr(p);
169+
bpf_rcu_read_unlock();
170+
return 0;
171+
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,16 @@ __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+
226+
__bpf_kfunc int *bpf_kfunc_ret_rcu_test_nostruct(int rdonly_buf_size)
227+
{
228+
return NULL;
229+
}
230+
221231
__bpf_kfunc struct bpf_testmod_ctx *
222232
bpf_testmod_ctx_create(int *err)
223233
{
@@ -623,6 +633,8 @@ BTF_ID_FLAGS(func, bpf_kfunc_trusted_vma_test, KF_TRUSTED_ARGS)
623633
BTF_ID_FLAGS(func, bpf_kfunc_trusted_task_test, KF_TRUSTED_ARGS)
624634
BTF_ID_FLAGS(func, bpf_kfunc_trusted_num_test, KF_TRUSTED_ARGS)
625635
BTF_ID_FLAGS(func, bpf_kfunc_rcu_task_test, KF_RCU)
636+
BTF_ID_FLAGS(func, bpf_kfunc_ret_rcu_test, KF_RET_NULL | KF_RCU_PROTECTED)
637+
BTF_ID_FLAGS(func, bpf_kfunc_ret_rcu_test_nostruct, KF_RET_NULL | KF_RCU_PROTECTED)
626638
BTF_ID_FLAGS(func, bpf_testmod_ctx_create, KF_ACQUIRE | KF_RET_NULL)
627639
BTF_ID_FLAGS(func, bpf_testmod_ctx_release, KF_RELEASE)
628640
BTF_ID_FLAGS(func, bpf_testmod_ops3_call_test_1)

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,8 @@ 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;
162+
int *bpf_kfunc_ret_rcu_test_nostruct(int rdonly_buf_size) __ksym;
161163

162164
int bpf_kfunc_multi_st_ops_test_1(struct st_ops_args *args, u32 id) __ksym;
163165

0 commit comments

Comments
 (0)