Skip to content

Commit 10bf1c8

Browse files
puranjaymohanKernel Patches Daemon
authored andcommitted
selftests: bpf: Add tests for unbalanced rcu_read_lock
As verifier now supports nested rcu critical sections, add new test cases to make sure unbalanced usage of rcu_read_lock()/unlock() is rejected. Signed-off-by: Puranjay Mohan <[email protected]>
1 parent 8f524c9 commit 10bf1c8

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ static const char * const inproper_region_tests[] = {
7979
"non_sleepable_rcu_mismatch",
8080
"inproper_sleepable_helper",
8181
"inproper_sleepable_kfunc",
82+
"nested_rcu_region_unbalanced_1",
83+
"nested_rcu_region_unbalanced_2",
8284
"rcu_read_lock_global_subprog_lock",
8385
"rcu_read_lock_global_subprog_unlock",
8486
"rcu_read_lock_sleepable_helper_global_subprog",

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,46 @@ int nested_rcu_region(void *ctx)
278278
return 0;
279279
}
280280

281+
SEC("?fentry.s/" SYS_PREFIX "sys_nanosleep")
282+
int nested_rcu_region_unbalanced_1(void *ctx)
283+
{
284+
struct task_struct *task, *real_parent;
285+
286+
/* nested rcu read lock regions */
287+
task = bpf_get_current_task_btf();
288+
bpf_rcu_read_lock();
289+
bpf_rcu_read_lock();
290+
real_parent = task->real_parent;
291+
if (!real_parent)
292+
goto out;
293+
(void)bpf_task_storage_get(&map_a, real_parent, 0, 0);
294+
out:
295+
bpf_rcu_read_unlock();
296+
bpf_rcu_read_unlock();
297+
bpf_rcu_read_unlock();
298+
return 0;
299+
}
300+
301+
SEC("?fentry.s/" SYS_PREFIX "sys_nanosleep")
302+
int nested_rcu_region_unbalanced_2(void *ctx)
303+
{
304+
struct task_struct *task, *real_parent;
305+
306+
/* nested rcu read lock regions */
307+
task = bpf_get_current_task_btf();
308+
bpf_rcu_read_lock();
309+
bpf_rcu_read_lock();
310+
bpf_rcu_read_lock();
311+
real_parent = task->real_parent;
312+
if (!real_parent)
313+
goto out;
314+
(void)bpf_task_storage_get(&map_a, real_parent, 0, 0);
315+
out:
316+
bpf_rcu_read_unlock();
317+
bpf_rcu_read_unlock();
318+
return 0;
319+
}
320+
281321
SEC("?fentry.s/" SYS_PREFIX "sys_getpgid")
282322
int task_trusted_non_rcuptr(void *ctx)
283323
{

0 commit comments

Comments
 (0)