]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
selftests: bpf: Add tests for unbalanced rcu_read_lock
authorPuranjay Mohan <puranjay@kernel.org>
Mon, 17 Nov 2025 20:04:10 +0000 (20:04 +0000)
committerAlexei Starovoitov <ast@kernel.org>
Sat, 22 Nov 2025 02:34:59 +0000 (18:34 -0800)
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 <puranjay@kernel.org>
Acked-by: Eduard Zingerman <eddyz87@gmail.com>
Link: https://lore.kernel.org/r/20251117200411.25563-3-puranjay@kernel.org
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
tools/testing/selftests/bpf/prog_tests/rcu_read_lock.c
tools/testing/selftests/bpf/progs/rcu_read_lock.c

index 451a5d9ff4cb6fbba2ce87af7eb4b50865a958cf..246eb259c08a18c19dfc29c45b50877137b28b24 100644 (file)
@@ -79,6 +79,8 @@ static const char * const inproper_region_tests[] = {
        "non_sleepable_rcu_mismatch",
        "inproper_sleepable_helper",
        "inproper_sleepable_kfunc",
+       "nested_rcu_region_unbalanced_1",
+       "nested_rcu_region_unbalanced_2",
        "rcu_read_lock_global_subprog_lock",
        "rcu_read_lock_global_subprog_unlock",
        "rcu_read_lock_sleepable_helper_global_subprog",
index 3a868a1993492674c4cb72bd0a516827e7eb8871..d70c28824bbe3337dc74e16f5f83f1c72d4092d9 100644 (file)
@@ -278,6 +278,46 @@ out:
        return 0;
 }
 
+SEC("?fentry.s/" SYS_PREFIX "sys_nanosleep")
+int nested_rcu_region_unbalanced_1(void *ctx)
+{
+       struct task_struct *task, *real_parent;
+
+       /* nested rcu read lock regions */
+       task = bpf_get_current_task_btf();
+       bpf_rcu_read_lock();
+       bpf_rcu_read_lock();
+       real_parent = task->real_parent;
+       if (!real_parent)
+               goto out;
+       (void)bpf_task_storage_get(&map_a, real_parent, 0, 0);
+out:
+       bpf_rcu_read_unlock();
+       bpf_rcu_read_unlock();
+       bpf_rcu_read_unlock();
+       return 0;
+}
+
+SEC("?fentry.s/" SYS_PREFIX "sys_nanosleep")
+int nested_rcu_region_unbalanced_2(void *ctx)
+{
+       struct task_struct *task, *real_parent;
+
+       /* nested rcu read lock regions */
+       task = bpf_get_current_task_btf();
+       bpf_rcu_read_lock();
+       bpf_rcu_read_lock();
+       bpf_rcu_read_lock();
+       real_parent = task->real_parent;
+       if (!real_parent)
+               goto out;
+       (void)bpf_task_storage_get(&map_a, real_parent, 0, 0);
+out:
+       bpf_rcu_read_unlock();
+       bpf_rcu_read_unlock();
+       return 0;
+}
+
 SEC("?fentry.s/" SYS_PREFIX "sys_getpgid")
 int task_trusted_non_rcuptr(void *ctx)
 {