Skip to contentSkip to conversation

bpf: Build-time assert that cpumask offset is zero

PATCH Applied David Vernet 2 participants 2 messages Jan 28, 2023 BPF

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 bpf: Build-time assert that cpumask offset is zero

The first element of a struct bpf_cpumask is a cpumask_t. This is done to allow struct bpf_cpumask to be cast to a struct cpumask. If this element were ever moved to another field, any BPF program passing a struct bpf_cpumask * to a kfunc expecting a const struct cpumask * would immediately fail to load. Add a build-time assertion so this is assumption is captured and verified.

---
 kernel/bpf/cpumask.c | 3 +++
 1 file changed, 3 insertions(+)
kernel/bpf/cpumask.c +3 -0
index 25355a0a367a..6bbb67dfc998 100644
@@ -52,6 +52,9 @@ struct bpf_cpumask *bpf_cpumask_create(void)
 {
 	struct bpf_cpumask *cpumask;
 
+	/* cpumask must be the first element so struct bpf_cpumask be cast to struct cpumask. */
+	BUILD_BUG_ON(offsetof(struct bpf_cpumask, cpumask) != 0);
+
 	cpumask = bpf_mem_alloc(&bpf_cpumask_ma, sizeof(*cpumask));
 	if (!cpumask)
 		return NULL;
Signature · 1 line
2.39.0
#1 lore raw
This patch was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:

Hello:

This patch was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:

Quoted 8 lines · On Sat, 28 Jan 2023 08:15:37 -0600 you wrote:

The first element of a struct bpf_cpumask is a cpumask_t. This is done to allow struct bpf_cpumask to be cast to a struct cpumask. If this element were ever moved to another field, any BPF program passing a struct bpf_cpumask * to a kfunc expecting a const struct cpumask * would immediately fail to load. Add a build-time assertion so this is assumption is captured and verified.

[...]

Here is the summary with links:
- [bpf-next] bpf: Build-time assert that cpumask offset is zero

    https://git.kernel.org/bpf/bpf-next/c/cb4a21ea592f

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html