Linux Kernel Selftest development
 help / color / mirror / Atom feed
* [PATCH bpf 0/2] bpf: Clear invalid cpumask bits after population
@ 2026-11-15  6:45 Avi Weiss
  2026-11-15  6:45 ` [PATCH bpf 1/2] bpf: Clear invalid tail bits when populating a cpumask Avi Weiss
  2026-11-15  6:45 ` [PATCH bpf 2/2] selftests/bpf: Check cpumask weight after population Avi Weiss
  0 siblings, 2 replies; 3+ messages in thread
From: Avi Weiss @ 2026-11-15  6:45 UTC (permalink / raw)
  To: bpf
  Cc: ast, daniel, andrii, eddyz87, memxor, shuah, emil, houtao1,
	linux-kselftest, Avi Weiss

bpf_cpumask_populate() copies the final bitmap word without clearing
bits beyond nr_cpu_ids. On configurations where cpumask operations
scan a whole word, those bits can inflate the reported mask weight.

Patch 1 clears the unused tail bits. Patch 2 checks the resulting
weight in the existing populate selftest.

Tested on 97d73c825122 (x86_64, CONFIG_NR_CPUS=64, 4 possible CPUs):
- Unpatched: cpumask/test_populate FAIL (err=5).
- Patched: cpumask/test_populate PASS.
- Patched: cpumask selftests 36/36 subtests PASS.

Not run: allmodconfig, allyesconfig, the full BPF selftest suite,
cross-architecture builds or a dedicated performance benchmark.
The changes have not been rebuilt on top of the target BPF fixes tip.

Avi Weiss (2):
  bpf: Clear invalid tail bits when populating a cpumask
  selftests/bpf: Check cpumask weight after population

 kernel/bpf/cpumask.c                                | 2 +-
 tools/testing/selftests/bpf/progs/cpumask_success.c | 3 +++
 2 files changed, 4 insertions(+), 1 deletion(-)


base-commit: 4b4eb7f44ee05754339d07e9df649809f45d14c5
-- 
2.43.0


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

* [PATCH bpf 1/2] bpf: Clear invalid tail bits when populating a cpumask
  2026-11-15  6:45 [PATCH bpf 0/2] bpf: Clear invalid cpumask bits after population Avi Weiss
@ 2026-11-15  6:45 ` Avi Weiss
  2026-11-15  6:45 ` [PATCH bpf 2/2] selftests/bpf: Check cpumask weight after population Avi Weiss
  1 sibling, 0 replies; 3+ messages in thread
From: Avi Weiss @ 2026-11-15  6:45 UTC (permalink / raw)
  To: bpf
  Cc: ast, daniel, andrii, eddyz87, memxor, shuah, emil, houtao1,
	linux-kselftest, Avi Weiss

bpf_cpumask_populate() uses bitmap_copy(), which copies the full last
word without clearing source bits above nr_cpu_ids. Those bits do not
represent valid CPUs, but cpumask operations that scan a whole word
can still observe them.

With CONFIG_NR_CPUS=64 and four possible CPUs, populating a bitmap
filled with 0xaa makes bpf_cpumask_weight() return 32 instead of 2.

Use bitmap_copy_clear_tail() to mask off the unused bits in the last
copied word.

Fixes: 950ad93df2fc ("bpf: add kfunc for populating cpumask bits")
Signed-off-by: Avi Weiss <thnkslprpt@gmail.com>
---
 kernel/bpf/cpumask.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/kernel/bpf/cpumask.c b/kernel/bpf/cpumask.c
index 1336a4efa755..203f93077a0c 100644
--- a/kernel/bpf/cpumask.c
+++ b/kernel/bpf/cpumask.c
@@ -467,7 +467,7 @@ __bpf_kfunc int bpf_cpumask_populate(struct bpf_cpumask *cpumask, void *src, siz
 		!IS_ALIGNED(source, sizeof(long)))
 		return -EINVAL;
 
-	bitmap_copy(cpumask_bits(&cpumask->cpumask), src, nr_cpu_ids);
+	bitmap_copy_clear_tail(cpumask_bits(&cpumask->cpumask), src, nr_cpu_ids);
 
 	return 0;
 }
-- 
2.43.0


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

* [PATCH bpf 2/2] selftests/bpf: Check cpumask weight after population
  2026-11-15  6:45 [PATCH bpf 0/2] bpf: Clear invalid cpumask bits after population Avi Weiss
  2026-11-15  6:45 ` [PATCH bpf 1/2] bpf: Clear invalid tail bits when populating a cpumask Avi Weiss
@ 2026-11-15  6:45 ` Avi Weiss
  1 sibling, 0 replies; 3+ messages in thread
From: Avi Weiss @ 2026-11-15  6:45 UTC (permalink / raw)
  To: bpf
  Cc: ast, daniel, andrii, eddyz87, memxor, shuah, emil, houtao1,
	linux-kselftest, Avi Weiss

The populate selftest checks individual valid CPU bits, but it does
not check whether bits beyond nr_cpu_ids affect cpumask operations
that scan the entire final word.

Verify the weight after populating an alternating-bit source to
catch leftover invalid bits.

Signed-off-by: Avi Weiss <thnkslprpt@gmail.com>
---
 tools/testing/selftests/bpf/progs/cpumask_success.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/tools/testing/selftests/bpf/progs/cpumask_success.c b/tools/testing/selftests/bpf/progs/cpumask_success.c
index ddaba4318c57..09c0ee35d636 100644
--- a/tools/testing/selftests/bpf/progs/cpumask_success.c
+++ b/tools/testing/selftests/bpf/progs/cpumask_success.c
@@ -887,6 +887,9 @@ int BPF_PROG(test_populate, struct task_struct *task, u64 clone_flags)
 		break;
 	}
 
+	if (!err && bpf_cpumask_weight((const struct cpumask *)mask) != nr_cpus / 2)
+		err = 5;
+
 out:
 	bpf_cpumask_release(mask);
 
-- 
2.43.0


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

end of thread, other threads:[~2026-11-15  6:45 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-15  6:45 [PATCH bpf 0/2] bpf: Clear invalid cpumask bits after population Avi Weiss
2026-11-15  6:45 ` [PATCH bpf 1/2] bpf: Clear invalid tail bits when populating a cpumask Avi Weiss
2026-11-15  6:45 ` [PATCH bpf 2/2] selftests/bpf: Check cpumask weight after population Avi Weiss

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox