Skip to contentSkip to conversation

bpf: Reduce smap->elem_size

PATCH Applied Martin KaFai Lau 2 participants 2 messages Dec 21, 2022 BPF

lore
Share & tools
lore

Martin KaFai Lau

author reviewer
#0 lore raw

PATCH v3 1/1 bpf: Reduce smap->elem_size

From: Martin KaFai Lau <[email protected]>

'struct bpf_local_storage_elem' has an unused 56 byte padding at the end due to struct's cache-line alignment requirement. This padding space is overlapped by storage value contents, so if we use sizeof() to calculate the total size, we overinflate it by 56 bytes. Use offsetof() instead to calculate more exact memory use.

---
v3:
Use one offsetof and save a '+' operator (Andrii)

v2:
Rephrase the commit message (Andrii and Yonghong) Use offsetofend instead of offsetof (Andrii)

 kernel/bpf/bpf_local_storage.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
kernel/bpf/bpf_local_storage.c +2 -2
index b39a46e8fb08..373c3c2c75bc 100644
@@ -580,8 +580,8 @@ static struct bpf_local_storage_map *__bpf_local_storage_map_alloc(union bpf_att
 		raw_spin_lock_init(&smap->buckets[i].lock);
 	}
 
-	smap->elem_size =
-		sizeof(struct bpf_local_storage_elem) + attr->value_size;
+	smap->elem_size = offsetof(struct bpf_local_storage_elem,
+				   sdata.data[attr->value_size]);
 
 	return smap;
 }
Signature · 1 line
2.30.2
#1 lore raw
This patch was applied to bpf/bpf-next.git (master) by Daniel Borkmann <[email protected]>:

Hello:

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

Quoted 9 lines · On Tue, 20 Dec 2022 17:30:36 -0800 you wrote:

From: Martin KaFai Lau <[email protected]>

'struct bpf_local_storage_elem' has an unused 56 byte padding at the end due to struct's cache-line alignment requirement. This padding space is overlapped by storage value contents, so if we use sizeof() to calculate the total size, we overinflate it by 56 bytes. Use offsetof() instead to calculate more exact memory use.

[...]

Here is the summary with links:
- [v3,bpf-next] bpf: Reduce smap->elem_size

    https://git.kernel.org/bpf/bpf-next/c/552d42a356eb

You are awesome, thank you!

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