Skip to contentSkip to conversation

bpf: Clear invalid cpumask bits after population

PATCH New Avi Weiss 1 participant 3 messages Nov 15

Subsystems (3)
lore
Share & tools
lore

Avi Weiss

author
#0 lore raw

cover bpf: Clear invalid cpumask bits after population

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

Signature · 1 line
2.43.0
author
#1 lore raw

PATCH 1/2 bpf: Clear invalid tail bits when populating a cpumask

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")
---
 kernel/bpf/cpumask.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/bpf/cpumask.c +1 -1
index 1336a4efa755..203f93077a0c 100644
@@ -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;
 }
Signature · 1 line
2.43.0
author
#2 lore raw

PATCH 2/2 selftests/bpf: Check cpumask weight after population

In reply to 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.

---
 tools/testing/selftests/bpf/progs/cpumask_success.c | 3 +++
 1 file changed, 3 insertions(+)
tools/testing/selftests/bpf/progs/cpumask_success.c +3 -0
index ddaba4318c57..09c0ee35d636 100644
@@ -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);
 
Signature · 1 line
2.43.0