diff mbox series

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

Message ID 20250228003321.1409285-3-emil@etsalapatis.com (mailing list archive)
State Superseded
Delegated to: BPF
Headers show
Series bpf: introduce helper for populating bpf_cpumask | expand

Checks

Context Check Description
bpf/vmtest-bpf-next-PR success PR summary
bpf/vmtest-bpf-next-VM_Test-49 success Logs for x86_64-llvm-18 / test (test_verifier, false, 360) / test_verifier on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-3 success Logs for Validate matrix.py
bpf/vmtest-bpf-next-VM_Test-1 success Logs for ShellCheck
bpf/vmtest-bpf-next-VM_Test-14 success Logs for s390x-gcc / build / build for s390x with gcc
bpf/vmtest-bpf-next-VM_Test-21 success Logs for set-matrix
bpf/vmtest-bpf-next-VM_Test-19 success Logs for s390x-gcc / veristat-kernel
bpf/vmtest-bpf-next-VM_Test-18 success Logs for s390x-gcc / test (test_verifier, false, 360) / test_verifier on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-10 success Logs for aarch64-gcc / test (test_verifier, false, 360) / test_verifier on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-15 success Logs for s390x-gcc / build-release
bpf/vmtest-bpf-next-VM_Test-24 success Logs for x86_64-gcc / build-release
bpf/vmtest-bpf-next-VM_Test-23 success Logs for x86_64-gcc / build / build for x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-20 success Logs for s390x-gcc / veristat-meta
bpf/vmtest-bpf-next-VM_Test-4 success Logs for aarch64-gcc / GCC BPF
bpf/vmtest-bpf-next-VM_Test-2 success Logs for Unittests
bpf/vmtest-bpf-next-VM_Test-11 success Logs for aarch64-gcc / veristat-kernel
bpf/vmtest-bpf-next-VM_Test-5 success Logs for aarch64-gcc / build / build for aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-12 success Logs for aarch64-gcc / veristat-meta
bpf/vmtest-bpf-next-VM_Test-6 success Logs for aarch64-gcc / build-release
bpf/vmtest-bpf-next-VM_Test-0 success Logs for Lint
bpf/vmtest-bpf-next-VM_Test-13 success Logs for s390x-gcc / GCC BPF
bpf/vmtest-bpf-next-VM_Test-40 success Logs for x86_64-llvm-17 / veristat-kernel
bpf/vmtest-bpf-next-VM_Test-41 success Logs for x86_64-llvm-17 / veristat-meta
bpf/vmtest-bpf-next-VM_Test-34 success Logs for x86_64-llvm-17 / build / build for x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-35 success Logs for x86_64-llvm-17 / build-release / build for x86_64 with llvm-17-O2
bpf/vmtest-bpf-next-VM_Test-39 success Logs for x86_64-llvm-17 / test (test_verifier, false, 360) / test_verifier on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-44 success Logs for x86_64-llvm-18 / build-release / build for x86_64 with llvm-18-O2
bpf/vmtest-bpf-next-VM_Test-43 success Logs for x86_64-llvm-18 / build / build for x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-30 success Logs for x86_64-gcc / test (test_verifier, false, 360) / test_verifier on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-51 success Logs for x86_64-llvm-18 / veristat-meta
bpf/vmtest-bpf-next-VM_Test-50 success Logs for x86_64-llvm-18 / veristat-kernel
bpf/vmtest-bpf-next-VM_Test-22 success Logs for x86_64-gcc / GCC BPF / GCC BPF
bpf/vmtest-bpf-next-VM_Test-31 success Logs for x86_64-gcc / veristat-kernel / x86_64-gcc veristat_kernel
bpf/vmtest-bpf-next-VM_Test-7 success Logs for aarch64-gcc / test (test_maps, false, 360) / test_maps on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-45 success Logs for x86_64-llvm-18 / test (test_maps, false, 360) / test_maps on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-47 success Logs for x86_64-llvm-18 / test (test_progs_cpuv4, false, 360) / test_progs_cpuv4 on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-26 success Logs for x86_64-gcc / test (test_progs, false, 360) / test_progs on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-37 success Logs for x86_64-llvm-17 / test (test_progs, false, 360) / test_progs on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-48 success Logs for x86_64-llvm-18 / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-36 success Logs for x86_64-llvm-17 / test (test_maps, false, 360) / test_maps on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-28 success Logs for x86_64-gcc / test (test_progs_no_alu32_parallel, true, 30) / test_progs_no_alu32_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-46 success Logs for x86_64-llvm-18 / test (test_progs, false, 360) / test_progs on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-33 success Logs for x86_64-llvm-17 / GCC BPF / GCC BPF
bpf/vmtest-bpf-next-VM_Test-27 success Logs for x86_64-gcc / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-42 success Logs for x86_64-llvm-18 / GCC BPF / GCC BPF
bpf/vmtest-bpf-next-VM_Test-32 success Logs for x86_64-gcc / veristat-meta / x86_64-gcc veristat_meta
bpf/vmtest-bpf-next-VM_Test-38 success Logs for x86_64-llvm-17 / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-25 success Logs for x86_64-gcc / test (test_maps, false, 360) / test_maps on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-29 success Logs for x86_64-gcc / test (test_progs_parallel, true, 30) / test_progs_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-9 success Logs for aarch64-gcc / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-8 success Logs for aarch64-gcc / test (test_progs, false, 360) / test_progs on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-16 success Logs for s390x-gcc / test (test_progs, false, 360) / test_progs on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-17 success Logs for s390x-gcc / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on s390x with gcc
netdev/series_format success Posting correctly formatted
netdev/tree_selection success Guessed tree name to be net-next
netdev/ynl success Generated files up to date; no warnings/errors; no diff in generated;
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 0 this patch: 0
netdev/build_tools success Errors and warnings before: 26 (+1) this patch: 26 (+1)
netdev/cc_maintainers warning 12 maintainers not CCed: houtao1@huawei.com mykolal@fb.com song@kernel.org kpsingh@kernel.org shuah@kernel.org john.fastabend@gmail.com jolsa@kernel.org thinker.li@gmail.com haoluo@google.com linux-kselftest@vger.kernel.org sdf@fomichev.me martin.lau@linux.dev
netdev/build_clang success Errors and warnings before: 1 this patch: 1
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 8 this patch: 8
netdev/checkpatch warning CHECK: From:/Signed-off-by: email comments mismatch: 'From: Emil Tsalapatis <emil@etsalapatis.com>' != 'Signed-off-by: Emil Tsalapatis (Meta) <emil@etsalapatis.com>' WARNING: added, moved or deleted file(s), does MAINTAINERS need updating? WARNING: line length of 83 exceeds 80 columns WARNING: line length of 86 exceeds 80 columns WARNING: line length of 87 exceeds 80 columns WARNING: line length of 89 exceeds 80 columns
netdev/build_clang_rust success No Rust files in patch. Skipping build
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0

Commit Message

Emil Tsalapatis Feb. 28, 2025, 12:33 a.m. UTC
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

Comments

Hou Tao March 1, 2025, 1:15 a.m. UTC | #1
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";
Emil Tsalapatis March 4, 2025, 4:05 a.m. UTC | #2
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";
>
diff mbox series

Patch

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