selftests/bpf: Add test for bpf_cpumask_weight() kfunc
authorDavid Vernet <void@manifault.com>
Thu, 7 Dec 2023 21:08:43 +0000 (15:08 -0600)
committerAlexei Starovoitov <ast@kernel.org>
Sun, 10 Dec 2023 05:37:33 +0000 (21:37 -0800)
The new bpf_cpumask_weight() kfunc can be used to count the number of
bits that are set in a struct cpumask* kptr. Let's add a selftest to
verify its behavior.

Signed-off-by: David Vernet <void@manifault.com>
Acked-by: Yonghong Song <yonghong.song@linux.dev>
Link: https://lore.kernel.org/r/20231207210843.168466-3-void@manifault.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
tools/testing/selftests/bpf/prog_tests/cpumask.c
tools/testing/selftests/bpf/progs/cpumask_common.h
tools/testing/selftests/bpf/progs/cpumask_success.c

index 756ea8b590b6fe02317aa96c99687e1eef3da45a..c2e886399e3cfb0e8d3fe4b5b737820215e14d55 100644 (file)
@@ -18,6 +18,7 @@ static const char * const cpumask_success_testcases[] = {
        "test_insert_leave",
        "test_insert_remove_release",
        "test_global_mask_rcu",
+       "test_cpumask_weight",
 };
 
 static void verify_success(const char *prog_name)
index b15c588ace15dfddf4c9f28e85c624a11df37b86..0cd4aebb97cf59cf6984c0286270554268a4c54d 100644 (file)
@@ -54,6 +54,7 @@ bool bpf_cpumask_full(const struct cpumask *cpumask) __ksym;
 void bpf_cpumask_copy(struct bpf_cpumask *dst, const struct cpumask *src) __ksym;
 u32 bpf_cpumask_any_distribute(const struct cpumask *src) __ksym;
 u32 bpf_cpumask_any_and_distribute(const struct cpumask *src1, const struct cpumask *src2) __ksym;
+u32 bpf_cpumask_weight(const struct cpumask *cpumask) __ksym;
 
 void bpf_rcu_read_lock(void) __ksym;
 void bpf_rcu_read_unlock(void) __ksym;
index 674a63424dee3ebdf2f322baeea037f834657cb1..fc3666edf4561a2e681b72ea570558985f6b6515 100644 (file)
@@ -460,6 +460,49 @@ int BPF_PROG(test_global_mask_rcu, struct task_struct *task, u64 clone_flags)
        return 0;
 }
 
+SEC("tp_btf/task_newtask")
+int BPF_PROG(test_cpumask_weight, struct task_struct *task, u64 clone_flags)
+{
+       struct bpf_cpumask *local;
+
+       if (!is_test_task())
+               return 0;
+
+       local = create_cpumask();
+       if (!local)
+               return 0;
+
+       if (bpf_cpumask_weight(cast(local)) != 0) {
+               err = 3;
+               goto out;
+       }
+
+       bpf_cpumask_set_cpu(0, local);
+       if (bpf_cpumask_weight(cast(local)) != 1) {
+               err = 4;
+               goto out;
+       }
+
+       /*
+        * Make sure that adding additional CPUs changes the weight. Test to
+        * see whether the CPU was set to account for running on UP machines.
+        */
+       bpf_cpumask_set_cpu(1, local);
+       if (bpf_cpumask_test_cpu(1, cast(local)) && bpf_cpumask_weight(cast(local)) != 2) {
+               err = 5;
+               goto out;
+       }
+
+       bpf_cpumask_clear(local);
+       if (bpf_cpumask_weight(cast(local)) != 0) {
+               err = 6;
+               goto out;
+       }
+out:
+       bpf_cpumask_release(local);
+       return 0;
+}
+
 SEC("tp_btf/task_newtask")
 __success
 int BPF_PROG(test_refcount_null_tracking, struct task_struct *task, u64 clone_flags)