Skip to contentSkip to conversation

bpf: introduce helper for populating bpf_cpumask

PATCH Superseded Emil Tsalapatis 2 participants 9 messages Feb 28, 2025 – Mar 4, 2025

Subsystems (3)

v1 of 7 Superseded Latest is v7 →

lore
Share & tools
lore

Emil Tsalapatis

author reviewer
#0 lore raw

cover bpf: introduce helper for populating bpf_cpumask

Some BPF programs like scx schedulers have their own internal CPU mask types, mask types, which they must transform into struct bpf_cpumask instances before passing them to scheduling-related kfuncs. There is currently no way to efficiently populate the bitfield of a bpf_cpumask from BPF memory, and programs must use multiple bpf_cpumask_[set, clear] calls to do so. Introduce a kfunc helper to populate the bitfield of a bpf_cpumask from valid BPF memory with a single call.

Emil Tsalapatis (2):
bpf: add kfunc for populating cpumask bits
selftests: bpf: add bpf_cpumask_fill selftests

 kernel/bpf/cpumask.c                          | 21 +++++
 .../selftests/bpf/prog_tests/verifier.c       |  2 +
 .../selftests/bpf/progs/cpumask_success.c     | 23 ++++++
 .../selftests/bpf/progs/verifier_cpumask.c    | 77 +++++++++++++++++++
 4 files changed, 123 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/progs/verifier_cpumask.c
Signature · 1 line
2.47.1
author reviewer
#1 lore raw

PATCH 1/2 bpf: add kfunc for populating cpumask bits

Add a helper kfunc that sets the bitmap of a bpf_cpumask from BPF memory.

---
 kernel/bpf/cpumask.c | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
kernel/bpf/cpumask.c +21 -0
index cfa1c18e3a48..a13839b3595f 100644
@@ -420,6 +420,26 @@ __bpf_kfunc u32 bpf_cpumask_weight(const struct cpumask *cpumask)
 	return cpumask_weight(cpumask);
 }
 
+/**
+ * bpf_cpumask_fill() - Populate the CPU mask from the contents of
+ * a BPF memory region.
+ *
+ * @cpumask: The cpumask being populated.
+ * @src: The BPF memory holding the bit pattern.
+ * @src__sz: Length of the BPF memory region in bytes.
+ *
+ */
+__bpf_kfunc int bpf_cpumask_fill(struct cpumask *cpumask, void *src, size_t src__sz)
+{
+	/* The memory region must be large enough to populate the entire CPU mask. */
+	if (src__sz < BITS_TO_BYTES(nr_cpu_ids))
+		return -EACCES;
+
+	bitmap_copy(cpumask_bits(cpumask), src, nr_cpu_ids);
+
+	return 0;
+}
+
 __bpf_kfunc_end_defs();
 
 BTF_KFUNCS_START(cpumask_kfunc_btf_ids)
@@ -448,6 +468,7 @@ BTF_ID_FLAGS(func, bpf_cpumask_copy, KF_RCU)
 BTF_ID_FLAGS(func, bpf_cpumask_any_distribute, KF_RCU)
 BTF_ID_FLAGS(func, bpf_cpumask_any_and_distribute, KF_RCU)
 BTF_ID_FLAGS(func, bpf_cpumask_weight, KF_RCU)
+BTF_ID_FLAGS(func, bpf_cpumask_fill, KF_RCU)
 BTF_KFUNCS_END(cpumask_kfunc_btf_ids)
 
 static const struct btf_kfunc_id_set cpumask_kfunc_set = {
Signature · 1 line
2.47.1
#2 lore raw

Re: [PATCH 1/2] bpf: add kfunc for populating cpumask bits

Hi,

Quoted 22 lines · Emil Tsalapatis

Add a helper kfunc that sets the bitmap of a bpf_cpumask from BPF memory.

---
 kernel/bpf/cpumask.c | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
kernel/bpf/cpumask.c +6 -0
index cfa1c18e3a48..a13839b3595f 100644
@@ -420,6 +420,26 @@ __bpf_kfunc u32 bpf_cpumask_weight(const struct cpumask *cpumask)
 	return cpumask_weight(cpumask);
 }
 
+/**
+ * bpf_cpumask_fill() - Populate the CPU mask from the contents of
+ * a BPF memory region.
+ *
+ * @cpumask: The cpumask being populated.
+ * @src: The BPF memory holding the bit pattern.

kernel/bpf/cpumask.c __bpf_kfunc u32 bpf_cpumask_weight(const struct cpumask *cpumask) · line 438

Quoted 10 lines
kernel/bpf/cpumask.c +10 -0
+ * @src__sz: Length of the BPF memory region in bytes.
+ *
+ */
+__bpf_kfunc int bpf_cpumask_fill(struct cpumask *cpumask, void *src, size_t src__sz)
+{
+	/* The memory region must be large enough to populate the entire CPU mask. */
+	if (src__sz < BITS_TO_BYTES(nr_cpu_ids))
+		return -EACCES;
+
+	bitmap_copy(cpumask_bits(cpumask), src, nr_cpu_ids);

Should we use src__sz < bitmap_size(nr_cpu_ids) instead ? Because in bitmap_copy(), it assumes the size of src should be bitmap_size(nr_cpu_ids).

Quoted 15 lines
kernel/bpf/cpumask.c +5 -0
+
+	return 0;
+}
+
 __bpf_kfunc_end_defs();
 
 BTF_KFUNCS_START(cpumask_kfunc_btf_ids)
@@ -448,6 +468,7 @@ BTF_ID_FLAGS(func, bpf_cpumask_copy, KF_RCU)
 BTF_ID_FLAGS(func, bpf_cpumask_any_distribute, KF_RCU)
 BTF_ID_FLAGS(func, bpf_cpumask_any_and_distribute, KF_RCU)
 BTF_ID_FLAGS(func, bpf_cpumask_weight, KF_RCU)
+BTF_ID_FLAGS(func, bpf_cpumask_fill, KF_RCU)
 BTF_KFUNCS_END(cpumask_kfunc_btf_ids)
 
 static const struct btf_kfunc_id_set cpumask_kfunc_set = {
author reviewer
#3 lore raw

Re: [PATCH 1/2] bpf: add kfunc for populating cpumask bits

Hi,

Quoted 31 lines · Hou Tao

Hi,

Quoted 27 lines · Emil Tsalapatis

Add a helper kfunc that sets the bitmap of a bpf_cpumask from BPF memory.

---
 kernel/bpf/cpumask.c | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
kernel/bpf/cpumask.c +11 -0
index cfa1c18e3a48..a13839b3595f 100644
@@ -420,6 +420,26 @@ __bpf_kfunc u32 bpf_cpumask_weight(const struct cpumask *cpumask)
      return cpumask_weight(cpumask);
 }
 
+/**
+ * bpf_cpumask_fill() - Populate the CPU mask from the contents of
+ * a BPF memory region.
+ *
+ * @cpumask: The cpumask being populated.
+ * @src: The BPF memory holding the bit pattern.
+ * @src__sz: Length of the BPF memory region in bytes.
+ *
+ */
+__bpf_kfunc int bpf_cpumask_fill(struct cpumask *cpumask, void *src, size_t src__sz)
+{
Quoted 8 lines

kernel/bpf/cpumask.c __bpf_kfunc u32 bpf_cpumask_weight(const struct cpumask *cpumask) · line 438

Quoted 5 lines
kernel/bpf/cpumask.c +5 -0
+     /* The memory region must be large enough to populate the entire CPU mask. */
+     if (src__sz < BITS_TO_BYTES(nr_cpu_ids))
+             return -EACCES;
+
+     bitmap_copy(cpumask_bits(cpumask), src, nr_cpu_ids);

Should we use src__sz < bitmap_size(nr_cpu_ids) instead ? Because in bitmap_copy(), it assumes the size of src should be bitmap_size(nr_cpu_ids).

This is a great catch, thank you. Comparing with BITS_TO_BYTES(nr_cpu_ids) allows byte-aligned
masks through, even though bitmap_copy assumes all masks are long-aligned.

Quoted 16 lines
Quoted 15 lines
kernel/bpf/cpumask.c +5 -0
+
+     return 0;
+}
+
 __bpf_kfunc_end_defs();
 
 BTF_KFUNCS_START(cpumask_kfunc_btf_ids)
@@ -448,6 +468,7 @@ BTF_ID_FLAGS(func, bpf_cpumask_copy, KF_RCU)
 BTF_ID_FLAGS(func, bpf_cpumask_any_distribute, KF_RCU)
 BTF_ID_FLAGS(func, bpf_cpumask_any_and_distribute, KF_RCU)
 BTF_ID_FLAGS(func, bpf_cpumask_weight, KF_RCU)
+BTF_ID_FLAGS(func, bpf_cpumask_fill, KF_RCU)
 BTF_KFUNCS_END(cpumask_kfunc_btf_ids)
 
 static const struct btf_kfunc_id_set cpumask_kfunc_set = {
#4 lore raw

Re: [PATCH 1/2] bpf: add kfunc for populating cpumask bits

Hi,

Quoted 40 lines · Emil Tsalapatis

Hi,

Quoted 29 lines · Hou Tao

Hi,

Quoted 26 lines · Emil Tsalapatis

Add a helper kfunc that sets the bitmap of a bpf_cpumask from BPF memory.

---
 kernel/bpf/cpumask.c | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
kernel/bpf/cpumask.c +10 -0
index cfa1c18e3a48..a13839b3595f 100644
@@ -420,6 +420,26 @@ __bpf_kfunc u32 bpf_cpumask_weight(const struct cpumask *cpumask)
      return cpumask_weight(cpumask);
 }
 
+/**
+ * bpf_cpumask_fill() - Populate the CPU mask from the contents of
+ * a BPF memory region.
+ *
+ * @cpumask: The cpumask being populated.
+ * @src: The BPF memory holding the bit pattern.
+ * @src__sz: Length of the BPF memory region in bytes.
+ *
+ */
+__bpf_kfunc int bpf_cpumask_fill(struct cpumask *cpumask, void *src, size_t src__sz)
Quoted 8 lines

kernel/bpf/cpumask.c __bpf_kfunc u32 bpf_cpumask_weight(const struct cpumask *cpumask) · line 438

Quoted 6 lines
kernel/bpf/cpumask.c +6 -0
+{
+     /* The memory region must be large enough to populate the entire CPU mask. */
+     if (src__sz < BITS_TO_BYTES(nr_cpu_ids))
+             return -EACCES;
+
+     bitmap_copy(cpumask_bits(cpumask), src, nr_cpu_ids);

Should we use src__sz < bitmap_size(nr_cpu_ids) instead ? Because in bitmap_copy(), it assumes the size of src should be bitmap_size(nr_cpu_ids).

Quoted 3 lines

This is a great catch, thank you. Comparing with BITS_TO_BYTES(nr_cpu_ids) allows byte-aligned
masks through, even though bitmap_copy assumes all masks are long-aligned.

Er, the long-aligned assumption raises another problem. Do we need to make the src pointer be long-aligned because bitmap_copy() may use "*dst = *src" to dereference the src pointer ? Or would it be better to use memcpy() to copy the cpumask directly ?

Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
kernel/bpf/cpumask.c +5 -0
+
+     return 0;
+}
+
 __bpf_kfunc_end_defs();
 
 BTF_KFUNCS_START(cpumask_kfunc_btf_ids)
@@ -448,6 +468,7 @@ BTF_ID_FLAGS(func, bpf_cpumask_copy, KF_RCU)
 BTF_ID_FLAGS(func, bpf_cpumask_any_distribute, KF_RCU)
 BTF_ID_FLAGS(func, bpf_cpumask_any_and_distribute, KF_RCU)
 BTF_ID_FLAGS(func, bpf_cpumask_weight, KF_RCU)
+BTF_ID_FLAGS(func, bpf_cpumask_fill, KF_RCU)
 BTF_KFUNCS_END(cpumask_kfunc_btf_ids)
 
 static const struct btf_kfunc_id_set cpumask_kfunc_set = {
author reviewer
#5 lore raw

Re: [PATCH 1/2] bpf: add kfunc for populating cpumask bits

Hi,

Quoted 44 lines · Hou Tao

Hi,

Quoted 40 lines · Emil Tsalapatis

Hi,

Quoted 37 lines · Hou Tao

Hi,

Quoted 22 lines · Emil Tsalapatis

Add a helper kfunc that sets the bitmap of a bpf_cpumask from BPF memory.

---
 kernel/bpf/cpumask.c | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
kernel/bpf/cpumask.c +6 -0
index cfa1c18e3a48..a13839b3595f 100644
@@ -420,6 +420,26 @@ __bpf_kfunc u32 bpf_cpumask_weight(const struct cpumask *cpumask)
      return cpumask_weight(cpumask);
 }
 
+/**
+ * bpf_cpumask_fill() - Populate the CPU mask from the contents of
+ * a BPF memory region.
+ *
+ * @cpumask: The cpumask being populated.
+ * @src: The BPF memory holding the bit pattern.

kernel/bpf/cpumask.c __bpf_kfunc u32 bpf_cpumask_weight(const struct cpumask *cpumask) · line 438

Quoted 10 lines
kernel/bpf/cpumask.c +10 -0
+ * @src__sz: Length of the BPF memory region in bytes.
+ *
+ */
+__bpf_kfunc int bpf_cpumask_fill(struct cpumask *cpumask, void *src, size_t src__sz)
+{
+     /* The memory region must be large enough to populate the entire CPU mask. */
+     if (src__sz < BITS_TO_BYTES(nr_cpu_ids))
+             return -EACCES;
+
+     bitmap_copy(cpumask_bits(cpumask), src, nr_cpu_ids);

Should we use src__sz < bitmap_size(nr_cpu_ids) instead ? Because in bitmap_copy(), it assumes the size of src should be bitmap_size(nr_cpu_ids).

Quoted 8 lines
Quoted 3 lines

This is a great catch, thank you. Comparing with BITS_TO_BYTES(nr_cpu_ids) allows byte-aligned
masks through, even though bitmap_copy assumes all masks are long-aligned.

Er, the long-aligned assumption raises another problem. Do we need to make the src pointer be long-aligned because bitmap_copy() may use "*dst = *src" to dereference the src pointer ? Or would it be better to use memcpy() to copy the cpumask directly ?

I would be fine with either, IMO the former is preferable. We are rounding up the
size of the BPF-side CPU mask to the nearest long anyway, so it makes sense for the
memory region to be long-aligned. The alternative would make the copy slightly slower
on machines with nr_cpu_ids <= 64, though at least for sched_ext this function should
be rare enough that the performance impact is be minimal.

If that makes sense, I will add an alignment check and an associated selftest.

Quoted 16 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
kernel/bpf/cpumask.c +5 -0
+
+     return 0;
+}
+
 __bpf_kfunc_end_defs();
 
 BTF_KFUNCS_START(cpumask_kfunc_btf_ids)
@@ -448,6 +468,7 @@ BTF_ID_FLAGS(func, bpf_cpumask_copy, KF_RCU)
 BTF_ID_FLAGS(func, bpf_cpumask_any_distribute, KF_RCU)
 BTF_ID_FLAGS(func, bpf_cpumask_any_and_distribute, KF_RCU)
 BTF_ID_FLAGS(func, bpf_cpumask_weight, KF_RCU)
+BTF_ID_FLAGS(func, bpf_cpumask_fill, KF_RCU)
 BTF_KFUNCS_END(cpumask_kfunc_btf_ids)
 
 static const struct btf_kfunc_id_set cpumask_kfunc_set = {
author reviewer
#6 lore raw

PATCH 2/2 selftests: bpf: add bpf_cpumask_fill selftests

In reply to Emil Tsalapatis

Add selftests for the bpf_cpumask_fill helper that sets a bpf_cpumask to a bit pattern provided by a BPF program.

---
 .../selftests/bpf/prog_tests/verifier.c       |  2 +
 .../selftests/bpf/progs/cpumask_success.c     | 23 ++++++
 .../selftests/bpf/progs/verifier_cpumask.c    | 77 +++++++++++++++++++
 3 files changed, 102 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/progs/verifier_cpumask.c
tools/testing/selftests/bpf/prog_tests/verifier.c +2 -0
index 8a0e1ff8a2dc..4dd95e93bd7e 100644
@@ -23,6 +23,7 @@
 #include "verifier_cgroup_storage.skel.h"
 #include "verifier_const.skel.h"
 #include "verifier_const_or.skel.h"
+#include "verifier_cpumask.skel.h"
 #include "verifier_ctx.skel.h"
 #include "verifier_ctx_sk_msg.skel.h"
 #include "verifier_d_path.skel.h"
@@ -155,6 +156,7 @@ void test_verifier_cgroup_skb(void)           { RUN(verifier_cgroup_skb); }
 void test_verifier_cgroup_storage(void)       { RUN(verifier_cgroup_storage); }
 void test_verifier_const(void)                { RUN(verifier_const); }
 void test_verifier_const_or(void)             { RUN(verifier_const_or); }
+void test_verifier_cpumask(void)              { RUN(verifier_cpumask); }
 void test_verifier_ctx(void)                  { RUN(verifier_ctx); }
 void test_verifier_ctx_sk_msg(void)           { RUN(verifier_ctx_sk_msg); }
 void test_verifier_d_path(void)               { RUN(verifier_d_path); }
tools/testing/selftests/bpf/progs/cpumask_success.c +23 -0
index 80ee469b0b60..f252aa2f3090 100644
@@ -770,3 +770,26 @@ int BPF_PROG(test_refcount_null_tracking, struct task_struct *task, u64 clone_fl
 		bpf_cpumask_release(mask2);
 	return 0;
 }
+
+SEC("syscall")
+__success
+int BPF_PROG(test_fill_reject_small_mask)
+{
+	struct bpf_cpumask *local;
+	u8 toofewbits;
+	int ret;
+
+	local = create_cpumask();
+	if (!local)
+		return 0;
+
+	/* The kfunc should prevent this operation */
+	ret = bpf_cpumask_fill((struct cpumask *)local, &toofewbits, sizeof(toofewbits));
+	if (ret != -EACCES)
+		err = 2;
+
+	bpf_cpumask_release(local);
+
+	return 0;
+}
+
tools/testing/selftests/bpf/progs/verifier_cpumask.c new file +77 -0

77 lines not on this page · raw message

Signature · 1 line
2.47.1
#7 lore raw

Re: [PATCH 2/2] selftests: bpf: add bpf_cpumask_fill selftests

Hi,

Quoted 24 lines · Emil Tsalapatis

Add selftests for the bpf_cpumask_fill helper that sets a bpf_cpumask to a bit pattern provided by a BPF program.

---
 .../selftests/bpf/prog_tests/verifier.c       |  2 +
 .../selftests/bpf/progs/cpumask_success.c     | 23 ++++++
 .../selftests/bpf/progs/verifier_cpumask.c    | 77 +++++++++++++++++++
 3 files changed, 102 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/progs/verifier_cpumask.c
tools/testing/selftests/bpf/prog_tests/verifier.c +1 -0
index 8a0e1ff8a2dc..4dd95e93bd7e 100644
@@ -23,6 +23,7 @@
 #include "verifier_cgroup_storage.skel.h"
 #include "verifier_const.skel.h"
 #include "verifier_const_or.skel.h"
+#include "verifier_cpumask.skel.h"
 #include "verifier_ctx.skel.h"
 #include "verifier_ctx_sk_msg.skel.h"
 #include "verifier_d_path.skel.h"

tools/testing/selftests/bpf/prog_tests/verifier.c void test_verifier_cgroup_skb(void) { RUN(verifier_cgroup_skb); } · line 159

Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/verifier.c +1 -0
@@ -155,6 +156,7 @@ void test_verifier_cgroup_skb(void)           { RUN(verifier_cgroup_skb); }
 void test_verifier_cgroup_storage(void)       { RUN(verifier_cgroup_storage); }
 void test_verifier_const(void)                { RUN(verifier_const); }
 void test_verifier_const_or(void)             { RUN(verifier_const_or); }
+void test_verifier_cpumask(void)              { RUN(verifier_cpumask); }

Why is a new file necessary ? Is it more reasonable to add these success and failure test cases in cpumask_success.c and cpumask_failure.c ?

Quoted 18 lines
tools/testing/selftests/bpf/prog_tests/verifier.c +0 -0
 void test_verifier_ctx(void)                  { RUN(verifier_ctx); }
 void test_verifier_ctx_sk_msg(void)           { RUN(verifier_ctx_sk_msg); }
 void test_verifier_d_path(void)               { RUN(verifier_d_path); }
tools/testing/selftests/bpf/progs/cpumask_success.c +7 -0
index 80ee469b0b60..f252aa2f3090 100644
@@ -770,3 +770,26 @@ int BPF_PROG(test_refcount_null_tracking, struct task_struct *task, u64 clone_fl
 		bpf_cpumask_release(mask2);
 	return 0;
 }
+
+SEC("syscall")
+__success
+int BPF_PROG(test_fill_reject_small_mask)
+{
+	struct bpf_cpumask *local;
+	u8 toofewbits;

tools/testing/selftests/bpf/progs/cpumask_success.c int BPF_PROG(test_refcount_null_tracking, struct task_struct *task, u64 clone_fl · line 789

Quoted 10 lines
tools/testing/selftests/bpf/progs/cpumask_success.c +10 -0
+	int ret;
+
+	local = create_cpumask();
+	if (!local)
+		return 0;
+
+	/* The kfunc should prevent this operation */
+	ret = bpf_cpumask_fill((struct cpumask *)local, &toofewbits, sizeof(toofewbits));
+	if (ret != -EACCES)
+		err = 2;

The check may not be true when running local with a smaller NR_CPUS. It will be more reasonable to adjust the size according to the value of nr_cpu_ids.

Quoted 32 lines
tools/testing/selftests/bpf/progs/cpumask_success.c +6 -0
+
+	bpf_cpumask_release(local);
+
+	return 0;
+}
+
tools/testing/selftests/bpf/progs/verifier_cpumask.c new file +20 -0
new file mode 100644
index 000000000000..bb84dd36beac
@@ -0,0 +1,77 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2025 Meta Platforms, Inc. and affiliates. */
+
+#include <vmlinux.h>
+#include <bpf/bpf_tracing.h>
+#include <bpf/bpf_helpers.h>
+#include "bpf_misc.h"
+
+#include "cpumask_common.h"
+
+#define CPUMASK_TEST_MASKLEN (8 * sizeof(u64))
+
+u64 bits[CPUMASK_TEST_MASKLEN];
+
+SEC("syscall")
+__success
+int BPF_PROG(test_cpumask_fill)
+{
+	struct bpf_cpumask *mask;
+	int ret;

tools/testing/selftests/bpf/progs/verifier_cpumask.c line 30

Quoted 10 lines
tools/testing/selftests/bpf/progs/verifier_cpumask.c new file +10 -0
+
+	mask = bpf_cpumask_create();
+	if (!mask) {
+		err = 1;
+		return 0;
+	}
+
+	ret = bpf_cpumask_fill((struct cpumask *)mask, bits, CPUMASK_TEST_MASKLEN);
+	if (!ret)
+		err = 2;

It would be better to also test the cpu bits in the cpumask after bpf_cpumask_fill() is expected.

tools/testing/selftests/bpf/progs/verifier_cpumask.c line 33

Quoted 3 lines
tools/testing/selftests/bpf/progs/verifier_cpumask.c new file +3 -0
+
+	if (mask)
+		bpf_cpumask_release(mask);

The "if (mask)" check is unnecessary.

Quoted 44 lines
tools/testing/selftests/bpf/progs/verifier_cpumask.c new file +44 -0
+
+	return 0;
+}
+
+SEC("syscall")
+__description("bpf_cpumask_fill: invalid cpumask target")
+__failure __msg("type=scalar expected=fp")
+int BPF_PROG(test_cpumask_fill_cpumask_invalid)
+{
+	struct bpf_cpumask *invalid = (struct bpf_cpumask *)0x123456;
+	int ret;
+
+	ret = bpf_cpumask_fill((struct cpumask *)invalid, bits, CPUMASK_TEST_MASKLEN);
+	if (!ret)
+		err = 2;
+
+	return 0;
+}
+
+SEC("syscall")
+__description("bpf_cpumask_fill: invalid cpumask source")
+__failure __msg("leads to invalid memory access")
+int BPF_PROG(test_cpumask_fill_bpf_invalid)
+{
+	void *garbage = (void *)0x123456;
+	struct bpf_cpumask *local;
+	int ret;
+
+	local = create_cpumask();
+	if (!local) {
+		err = 1;
+		return 0;
+	}
+
+	ret = bpf_cpumask_fill((struct cpumask *)local, garbage, CPUMASK_TEST_MASKLEN);
+	if (!ret)
+		err = 2;
+
+	bpf_cpumask_release(local);
+
+	return 0;
+}
+
+char _license[] SEC("license") = "GPL";
author reviewer
#8 lore raw

Re: [PATCH 2/2] selftests: bpf: add bpf_cpumask_fill selftests

Hi,

Quoted 27 lines · Hou Tao

Hi,

Quoted 23 lines · Emil Tsalapatis

Add selftests for the bpf_cpumask_fill helper that sets a bpf_cpumask to a bit pattern provided by a BPF program.

---
 .../selftests/bpf/prog_tests/verifier.c       |  2 +
 .../selftests/bpf/progs/cpumask_success.c     | 23 ++++++
 .../selftests/bpf/progs/verifier_cpumask.c    | 77 +++++++++++++++++++
 3 files changed, 102 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/progs/verifier_cpumask.c
tools/testing/selftests/bpf/prog_tests/verifier.c +1 -0
index 8a0e1ff8a2dc..4dd95e93bd7e 100644
@@ -23,6 +23,7 @@
 #include "verifier_cgroup_storage.skel.h"
 #include "verifier_const.skel.h"
 #include "verifier_const_or.skel.h"
+#include "verifier_cpumask.skel.h"
 #include "verifier_ctx.skel.h"
 #include "verifier_ctx_sk_msg.skel.h"
 #include "verifier_d_path.skel.h"
Quoted 8 lines

tools/testing/selftests/bpf/prog_tests/verifier.c void test_verifier_cgroup_skb(void) { RUN(verifier_cgroup_skb); } · line 159

Quoted 5 lines
tools/testing/selftests/bpf/prog_tests/verifier.c +1 -0
@@ -155,6 +156,7 @@ void test_verifier_cgroup_skb(void)           { RUN(verifier_cgroup_skb); }
 void test_verifier_cgroup_storage(void)       { RUN(verifier_cgroup_storage); }
 void test_verifier_const(void)                { RUN(verifier_const); }
 void test_verifier_const_or(void)             { RUN(verifier_const_or); }
+void test_verifier_cpumask(void)              { RUN(verifier_cpumask); }

Why is a new file necessary ? Is it more reasonable to add these success and failure test cases in cpumask_success.c and cpumask_failure.c ?

Sounds good, I will roll the new tests into the existing files.

Quoted 24 lines
Quoted 24 lines
tools/testing/selftests/bpf/prog_tests/verifier.c +0 -0
 void test_verifier_ctx(void)                  { RUN(verifier_ctx); }
 void test_verifier_ctx_sk_msg(void)           { RUN(verifier_ctx_sk_msg); }
 void test_verifier_d_path(void)               { RUN(verifier_d_path); }
tools/testing/selftests/bpf/progs/cpumask_success.c +13 -0
index 80ee469b0b60..f252aa2f3090 100644
@@ -770,3 +770,26 @@ int BPF_PROG(test_refcount_null_tracking, struct task_struct *task, u64 clone_fl
              bpf_cpumask_release(mask2);
      return 0;
 }
+
+SEC("syscall")
+__success
+int BPF_PROG(test_fill_reject_small_mask)
+{
+     struct bpf_cpumask *local;
+     u8 toofewbits;
+     int ret;
+
+     local = create_cpumask();
+     if (!local)
+             return 0;
+
Quoted 8 lines

tools/testing/selftests/bpf/progs/cpumask_success.c int BPF_PROG(test_refcount_null_tracking, struct task_struct *task, u64 clone_fl · line 789

Quoted 4 lines
tools/testing/selftests/bpf/progs/cpumask_success.c +4 -0
+     /* The kfunc should prevent this operation */
+     ret = bpf_cpumask_fill((struct cpumask *)local, &toofewbits, sizeof(toofewbits));
+     if (ret != -EACCES)
+             err = 2;

The check may not be true when running local with a smaller NR_CPUS. It will be more reasonable to adjust the size according to the value of nr_cpu_ids.

Now that the size check rounds the size up to the nearest sizeof(long) bytes, passing a
mask of size 1 is guaranteed to fail.

Quoted 37 lines
Quoted 37 lines
tools/testing/selftests/bpf/progs/cpumask_success.c +6 -0
+
+     bpf_cpumask_release(local);
+
+     return 0;
+}
+
tools/testing/selftests/bpf/progs/verifier_cpumask.c new file +25 -0
new file mode 100644
index 000000000000..bb84dd36beac
@@ -0,0 +1,77 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2025 Meta Platforms, Inc. and affiliates. */
+
+#include <vmlinux.h>
+#include <bpf/bpf_tracing.h>
+#include <bpf/bpf_helpers.h>
+#include "bpf_misc.h"
+
+#include "cpumask_common.h"
+
+#define CPUMASK_TEST_MASKLEN (8 * sizeof(u64))
+
+u64 bits[CPUMASK_TEST_MASKLEN];
+
+SEC("syscall")
+__success
+int BPF_PROG(test_cpumask_fill)
+{
+     struct bpf_cpumask *mask;
+     int ret;
+
+     mask = bpf_cpumask_create();
+     if (!mask) {
+             err = 1;
+             return 0;
Quoted 8 lines

tools/testing/selftests/bpf/progs/verifier_cpumask.c line 30

Quoted 5 lines
tools/testing/selftests/bpf/progs/verifier_cpumask.c new file +5 -0
+     }
+
+     ret = bpf_cpumask_fill((struct cpumask *)mask, bits, CPUMASK_TEST_MASKLEN);
+     if (!ret)
+             err = 2;

It would be better to also test the cpu bits in the cpumask after bpf_cpumask_fill() is expected.

Sounds good, will address it.

Quoted 5 lines

tools/testing/selftests/bpf/progs/verifier_cpumask.c line 33

Quoted 3 lines
tools/testing/selftests/bpf/progs/verifier_cpumask.c new file +3 -0
+
+     if (mask)
+             bpf_cpumask_release(mask);

The "if (mask)" check is unnecessary.

Ditto.

Quoted 45 lines
Quoted 44 lines
tools/testing/selftests/bpf/progs/verifier_cpumask.c new file +44 -0
+
+     return 0;
+}
+
+SEC("syscall")
+__description("bpf_cpumask_fill: invalid cpumask target")
+__failure __msg("type=scalar expected=fp")
+int BPF_PROG(test_cpumask_fill_cpumask_invalid)
+{
+     struct bpf_cpumask *invalid = (struct bpf_cpumask *)0x123456;
+     int ret;
+
+     ret = bpf_cpumask_fill((struct cpumask *)invalid, bits, CPUMASK_TEST_MASKLEN);
+     if (!ret)
+             err = 2;
+
+     return 0;
+}
+
+SEC("syscall")
+__description("bpf_cpumask_fill: invalid cpumask source")
+__failure __msg("leads to invalid memory access")
+int BPF_PROG(test_cpumask_fill_bpf_invalid)
+{
+     void *garbage = (void *)0x123456;
+     struct bpf_cpumask *local;
+     int ret;
+
+     local = create_cpumask();
+     if (!local) {
+             err = 1;
+             return 0;
+     }
+
+     ret = bpf_cpumask_fill((struct cpumask *)local, garbage, CPUMASK_TEST_MASKLEN);
+     if (!ret)
+             err = 2;
+
+     bpf_cpumask_release(local);
+
+     return 0;
+}
+
+char _license[] SEC("license") = "GPL";