bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/2] bpf: introduce helper for populating bpf_cpumask
@ 2025-02-28  0:33 Emil Tsalapatis
  2025-02-28  0:33 ` [PATCH 1/2] bpf: add kfunc for populating cpumask bits Emil Tsalapatis
  2025-02-28  0:33 ` [PATCH 2/2] selftests: bpf: add bpf_cpumask_fill selftests Emil Tsalapatis
  0 siblings, 2 replies; 9+ messages in thread
From: Emil Tsalapatis @ 2025-02-28  0:33 UTC (permalink / raw)
  To: bpf
  Cc: ast, daniel, andrii, martin.lau, eddyz87, yonghong.song,
	Emil Tsalapatis

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.

Signed-off-by: Emil Tsalapatis (Meta) <emil@etsalapatis.com>

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

-- 
2.47.1


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PATCH 1/2] bpf: add kfunc for populating cpumask bits
  2025-02-28  0:33 [PATCH 0/2] bpf: introduce helper for populating bpf_cpumask Emil Tsalapatis
@ 2025-02-28  0:33 ` Emil Tsalapatis
  2025-03-01  0:56   ` Hou Tao
  2025-02-28  0:33 ` [PATCH 2/2] selftests: bpf: add bpf_cpumask_fill selftests Emil Tsalapatis
  1 sibling, 1 reply; 9+ messages in thread
From: Emil Tsalapatis @ 2025-02-28  0:33 UTC (permalink / raw)
  To: bpf
  Cc: ast, daniel, andrii, martin.lau, eddyz87, yonghong.song,
	Emil Tsalapatis

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

Signed-off-by: Emil Tsalapatis (Meta) <emil@etsalapatis.com>
---
 kernel/bpf/cpumask.c | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)

diff --git a/kernel/bpf/cpumask.c b/kernel/bpf/cpumask.c
index cfa1c18e3a48..a13839b3595f 100644
--- a/kernel/bpf/cpumask.c
+++ b/kernel/bpf/cpumask.c
@@ -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 = {
-- 
2.47.1


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [PATCH 2/2] selftests: bpf: add bpf_cpumask_fill selftests
  2025-02-28  0:33 [PATCH 0/2] bpf: introduce helper for populating bpf_cpumask Emil Tsalapatis
  2025-02-28  0:33 ` [PATCH 1/2] bpf: add kfunc for populating cpumask bits Emil Tsalapatis
@ 2025-02-28  0:33 ` Emil Tsalapatis
  2025-03-01  1:15   ` Hou Tao
  1 sibling, 1 reply; 9+ messages in thread
From: Emil Tsalapatis @ 2025-02-28  0:33 UTC (permalink / raw)
  To: bpf
  Cc: ast, daniel, andrii, martin.lau, eddyz87, yonghong.song,
	Emil Tsalapatis

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

Signed-off-by: Emil Tsalapatis (Meta) <emil@etsalapatis.com>
---
 .../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

diff --git a/tools/testing/selftests/bpf/prog_tests/verifier.c b/tools/testing/selftests/bpf/prog_tests/verifier.c
index 8a0e1ff8a2dc..4dd95e93bd7e 100644
--- a/tools/testing/selftests/bpf/prog_tests/verifier.c
+++ b/tools/testing/selftests/bpf/prog_tests/verifier.c
@@ -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); }
diff --git a/tools/testing/selftests/bpf/progs/cpumask_success.c b/tools/testing/selftests/bpf/progs/cpumask_success.c
index 80ee469b0b60..f252aa2f3090 100644
--- a/tools/testing/selftests/bpf/progs/cpumask_success.c
+++ b/tools/testing/selftests/bpf/progs/cpumask_success.c
@@ -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;
+}
+
diff --git a/tools/testing/selftests/bpf/progs/verifier_cpumask.c b/tools/testing/selftests/bpf/progs/verifier_cpumask.c
new file mode 100644
index 000000000000..bb84dd36beac
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/verifier_cpumask.c
@@ -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;
+	}
+
+	ret = bpf_cpumask_fill((struct cpumask *)mask, bits, CPUMASK_TEST_MASKLEN);
+	if (!ret)
+		err = 2;
+
+	if (mask)
+		bpf_cpumask_release(mask);
+
+	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";
-- 
2.47.1


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* Re: [PATCH 1/2] bpf: add kfunc for populating cpumask bits
  2025-02-28  0:33 ` [PATCH 1/2] bpf: add kfunc for populating cpumask bits Emil Tsalapatis
@ 2025-03-01  0:56   ` Hou Tao
  2025-03-04  3:18     ` Emil Tsalapatis
  0 siblings, 1 reply; 9+ messages in thread
From: Hou Tao @ 2025-03-01  0:56 UTC (permalink / raw)
  To: Emil Tsalapatis, bpf
  Cc: ast, daniel, andrii, martin.lau, eddyz87, yonghong.song

Hi,

On 2/28/2025 8:33 AM, Emil Tsalapatis wrote:
> Add a helper kfunc that sets the bitmap of a bpf_cpumask from BPF
> memory.
>
> Signed-off-by: Emil Tsalapatis (Meta) <emil@etsalapatis.com>
> ---
>  kernel/bpf/cpumask.c | 21 +++++++++++++++++++++
>  1 file changed, 21 insertions(+)
>
> diff --git a/kernel/bpf/cpumask.c b/kernel/bpf/cpumask.c
> index cfa1c18e3a48..a13839b3595f 100644
> --- a/kernel/bpf/cpumask.c
> +++ b/kernel/bpf/cpumask.c
> @@ -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);

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).
> +
> +	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 = {


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH 2/2] selftests: bpf: add bpf_cpumask_fill selftests
  2025-02-28  0:33 ` [PATCH 2/2] selftests: bpf: add bpf_cpumask_fill selftests Emil Tsalapatis
@ 2025-03-01  1:15   ` Hou Tao
  2025-03-04  4:05     ` Emil Tsalapatis
  0 siblings, 1 reply; 9+ messages in thread
From: Hou Tao @ 2025-03-01  1:15 UTC (permalink / raw)
  To: Emil Tsalapatis, bpf
  Cc: ast, daniel, andrii, martin.lau, eddyz87, yonghong.song

Hi,

On 2/28/2025 8:33 AM, Emil Tsalapatis wrote:
> Add selftests for the bpf_cpumask_fill helper that sets a bpf_cpumask to
> a bit pattern provided by a BPF program.
>
> Signed-off-by: Emil Tsalapatis (Meta) <emil@etsalapatis.com>
> ---
>  .../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
>
> diff --git a/tools/testing/selftests/bpf/prog_tests/verifier.c b/tools/testing/selftests/bpf/prog_tests/verifier.c
> index 8a0e1ff8a2dc..4dd95e93bd7e 100644
> --- a/tools/testing/selftests/bpf/prog_tests/verifier.c
> +++ b/tools/testing/selftests/bpf/prog_tests/verifier.c
> @@ -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); }

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 ?
>  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); }
> diff --git a/tools/testing/selftests/bpf/progs/cpumask_success.c b/tools/testing/selftests/bpf/progs/cpumask_success.c
> index 80ee469b0b60..f252aa2f3090 100644
> --- a/tools/testing/selftests/bpf/progs/cpumask_success.c
> +++ b/tools/testing/selftests/bpf/progs/cpumask_success.c
> @@ -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;

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.
> +
> +	bpf_cpumask_release(local);
> +
> +	return 0;
> +}
> +
> diff --git a/tools/testing/selftests/bpf/progs/verifier_cpumask.c b/tools/testing/selftests/bpf/progs/verifier_cpumask.c
> new file mode 100644
> index 000000000000..bb84dd36beac
> --- /dev/null
> +++ b/tools/testing/selftests/bpf/progs/verifier_cpumask.c
> @@ -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;
> +	}
> +
> +	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.
> +
> +	if (mask)
> +		bpf_cpumask_release(mask);

The "if (mask)" check is unnecessary.
> +
> +	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";


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH 1/2] bpf: add kfunc for populating cpumask bits
  2025-03-01  0:56   ` Hou Tao
@ 2025-03-04  3:18     ` Emil Tsalapatis
  2025-03-04 14:04       ` Hou Tao
  0 siblings, 1 reply; 9+ messages in thread
From: Emil Tsalapatis @ 2025-03-04  3:18 UTC (permalink / raw)
  To: Hou Tao; +Cc: bpf, ast, daniel, andrii, martin.lau, eddyz87, yonghong.song

Hi,

On Fri, Feb 28, 2025 at 7:56 PM Hou Tao <houtao@huaweicloud.com> wrote:
>
> Hi,
>
> On 2/28/2025 8:33 AM, Emil Tsalapatis wrote:
> > Add a helper kfunc that sets the bitmap of a bpf_cpumask from BPF
> > memory.
> >
> > Signed-off-by: Emil Tsalapatis (Meta) <emil@etsalapatis.com>
> > ---
> >  kernel/bpf/cpumask.c | 21 +++++++++++++++++++++
> >  1 file changed, 21 insertions(+)
> >
> > diff --git a/kernel/bpf/cpumask.c b/kernel/bpf/cpumask.c
> > index cfa1c18e3a48..a13839b3595f 100644
> > --- a/kernel/bpf/cpumask.c
> > +++ b/kernel/bpf/cpumask.c
> > @@ -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);
>
> 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.

> > +
> > +     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 = {
>

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH 2/2] selftests: bpf: add bpf_cpumask_fill selftests
  2025-03-01  1:15   ` Hou Tao
@ 2025-03-04  4:05     ` Emil Tsalapatis
  0 siblings, 0 replies; 9+ messages in thread
From: Emil Tsalapatis @ 2025-03-04  4:05 UTC (permalink / raw)
  To: Hou Tao; +Cc: bpf, ast, daniel, andrii, martin.lau, eddyz87, yonghong.song

Hi,


On Fri, Feb 28, 2025 at 8:16 PM Hou Tao <houtao@huaweicloud.com> wrote:
>
> Hi,
>
> On 2/28/2025 8:33 AM, Emil Tsalapatis wrote:
> > Add selftests for the bpf_cpumask_fill helper that sets a bpf_cpumask to
> > a bit pattern provided by a BPF program.
> >
> > Signed-off-by: Emil Tsalapatis (Meta) <emil@etsalapatis.com>
> > ---
> >  .../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
> >
> > diff --git a/tools/testing/selftests/bpf/prog_tests/verifier.c b/tools/testing/selftests/bpf/prog_tests/verifier.c
> > index 8a0e1ff8a2dc..4dd95e93bd7e 100644
> > --- a/tools/testing/selftests/bpf/prog_tests/verifier.c
> > +++ b/tools/testing/selftests/bpf/prog_tests/verifier.c
> > @@ -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); }
>
> 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.

> >  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); }
> > diff --git a/tools/testing/selftests/bpf/progs/cpumask_success.c b/tools/testing/selftests/bpf/progs/cpumask_success.c
> > index 80ee469b0b60..f252aa2f3090 100644
> > --- a/tools/testing/selftests/bpf/progs/cpumask_success.c
> > +++ b/tools/testing/selftests/bpf/progs/cpumask_success.c
> > @@ -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;
>
> 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.

> > +
> > +     bpf_cpumask_release(local);
> > +
> > +     return 0;
> > +}
> > +
> > diff --git a/tools/testing/selftests/bpf/progs/verifier_cpumask.c b/tools/testing/selftests/bpf/progs/verifier_cpumask.c
> > new file mode 100644
> > index 000000000000..bb84dd36beac
> > --- /dev/null
> > +++ b/tools/testing/selftests/bpf/progs/verifier_cpumask.c
> > @@ -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;
> > +     }
> > +
> > +     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.

> > +
> > +     if (mask)
> > +             bpf_cpumask_release(mask);
>
> The "if (mask)" check is unnecessary.

Ditto.


> > +
> > +     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";
>

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH 1/2] bpf: add kfunc for populating cpumask bits
  2025-03-04  3:18     ` Emil Tsalapatis
@ 2025-03-04 14:04       ` Hou Tao
  2025-03-04 21:57         ` Emil Tsalapatis
  0 siblings, 1 reply; 9+ messages in thread
From: Hou Tao @ 2025-03-04 14:04 UTC (permalink / raw)
  To: Emil Tsalapatis
  Cc: bpf, ast, daniel, andrii, martin.lau, eddyz87, yonghong.song

Hi,

On 3/4/2025 11:18 AM, Emil Tsalapatis wrote:
> Hi,
>
> On Fri, Feb 28, 2025 at 7:56 PM Hou Tao <houtao@huaweicloud.com> wrote:
>> Hi,
>>
>> On 2/28/2025 8:33 AM, Emil Tsalapatis wrote:
>>> Add a helper kfunc that sets the bitmap of a bpf_cpumask from BPF
>>> memory.
>>>
>>> Signed-off-by: Emil Tsalapatis (Meta) <emil@etsalapatis.com>
>>> ---
>>>  kernel/bpf/cpumask.c | 21 +++++++++++++++++++++
>>>  1 file changed, 21 insertions(+)
>>>
>>> diff --git a/kernel/bpf/cpumask.c b/kernel/bpf/cpumask.c
>>> index cfa1c18e3a48..a13839b3595f 100644
>>> --- a/kernel/bpf/cpumask.c
>>> +++ b/kernel/bpf/cpumask.c
>>> @@ -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);
>> 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.

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 ?
>>> +
>>> +     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 = {


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH 1/2] bpf: add kfunc for populating cpumask bits
  2025-03-04 14:04       ` Hou Tao
@ 2025-03-04 21:57         ` Emil Tsalapatis
  0 siblings, 0 replies; 9+ messages in thread
From: Emil Tsalapatis @ 2025-03-04 21:57 UTC (permalink / raw)
  To: Hou Tao; +Cc: bpf, ast, daniel, andrii, martin.lau, eddyz87, yonghong.song

Hi,


On Tue, Mar 4, 2025 at 9:04 AM Hou Tao <houtao@huaweicloud.com> wrote:
>
> Hi,
>
> On 3/4/2025 11:18 AM, Emil Tsalapatis wrote:
> > Hi,
> >
> > On Fri, Feb 28, 2025 at 7:56 PM Hou Tao <houtao@huaweicloud.com> wrote:
> >> Hi,
> >>
> >> On 2/28/2025 8:33 AM, Emil Tsalapatis wrote:
> >>> Add a helper kfunc that sets the bitmap of a bpf_cpumask from BPF
> >>> memory.
> >>>
> >>> Signed-off-by: Emil Tsalapatis (Meta) <emil@etsalapatis.com>
> >>> ---
> >>>  kernel/bpf/cpumask.c | 21 +++++++++++++++++++++
> >>>  1 file changed, 21 insertions(+)
> >>>
> >>> diff --git a/kernel/bpf/cpumask.c b/kernel/bpf/cpumask.c
> >>> index cfa1c18e3a48..a13839b3595f 100644
> >>> --- a/kernel/bpf/cpumask.c
> >>> +++ b/kernel/bpf/cpumask.c
> >>> @@ -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);
> >> 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.
>
> 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.




> >>> +
> >>> +     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 = {
>

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2025-03-04 21:57 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-02-28  0:33 [PATCH 0/2] bpf: introduce helper for populating bpf_cpumask Emil Tsalapatis
2025-02-28  0:33 ` [PATCH 1/2] bpf: add kfunc for populating cpumask bits Emil Tsalapatis
2025-03-01  0:56   ` Hou Tao
2025-03-04  3:18     ` Emil Tsalapatis
2025-03-04 14:04       ` Hou Tao
2025-03-04 21:57         ` Emil Tsalapatis
2025-02-28  0:33 ` [PATCH 2/2] selftests: bpf: add bpf_cpumask_fill selftests Emil Tsalapatis
2025-03-01  1:15   ` Hou Tao
2025-03-04  4:05     ` Emil Tsalapatis

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).