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