bpf: Reduce smap->elem_size
Martin KaFai Lau
reviewerPATCH v2 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 offsetofend() instead to calculate more exact memory use.
---
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
@@ -580,8 +580,8 @@ static struct bpf_local_storage_map *__bpf_local_storage_map_alloc(union bpf_attraw_spin_lock_init (&smap->buckets[i].lock); } - smap->elem_size = -sizeof (struct bpf_local_storage_elem ) + attr->value_size; + smap->elem_size = offsetofend(struct bpf_local_storage_elem, sdata) + + attr->value_size;return smap; }
Signature · 1 line
2.30.2
Quoted 24 lines · Martin KaFai Lau
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 usesizeof()to calculate the total size, we overinflate it by 56 bytes. Useoffsetofend()instead to calculate more exact memory use.Acked-by Yonghong Song <[email protected]>Signed-off-by Martin KaFai Lau <[email protected]>---
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 +0
-0
kernel/bpf/bpf_local_storage.c static struct bpf_local_storage_map *__bpf_local_storage_map_alloc(union bpf_att · line 584
Quoted 7 lines
kernel/bpf/bpf_local_storage.c +2
-2@@ -580,8 +580,8 @@ static struct bpf_local_storage_map *__bpf_local_storage_map_alloc(union bpf_attraw_spin_lock_init (&smap->buckets[i].lock); } - smap->elem_size = -sizeof (struct bpf_local_storage_elem ) + attr->value_size; + smap->elem_size = offsetofend(struct bpf_local_storage_elem, sdata) + + attr->value_size;
Heh, we raced down to a minute. Copy/pasting my latest reply from original thread.
it just occurred to me
that your change can be written equivalently (but now I do think it's cleaner) as:
smap->elem_size = offsetof(struct bpf_local_storage_elem, sdata.data[attr->value_size]);
sdata is embedded struct, no pointer dereference, so single offsetof() should be enough to peer inside it
Whichever you prefer, both versions work for me:
Quoted 6 lines
kernel/bpf/bpf_local_storage.c +0
-1return smap; } --2.30.2
Martin KaFai Lau
reviewerQuoted 28 lines · Andrii Nakryiko
Quoted 27 lines · Martin KaFai Lau
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 usesizeof()to calculate the total size, we overinflate it by 56 bytes. Useoffsetofend()instead to calculate more exact memory use.Acked-by Yonghong Song <[email protected]>Signed-off-by Martin KaFai Lau <[email protected]>---
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 +0
-0@@ -580,8 +580,8 @@ static struct bpf_local_storage_map *__bpf_local_storage_map_alloc(union bpf_attraw_spin_lock_init (&smap->buckets[i].lock); }
Quoted 7 lines
kernel/bpf/bpf_local_storage.c static struct bpf_local_storage_map *__bpf_local_storage_map_alloc(union bpf_att · line 584
Quoted 4 lines
kernel/bpf/bpf_local_storage.c +2
-2- smap->elem_size = -sizeof (struct bpf_local_storage_elem ) + attr->value_size; + smap->elem_size = offsetofend(struct bpf_local_storage_elem, sdata) + + attr->value_size;Heh, we raced down to a minute. Copy/pasting my latest reply from original thread.
lol. email is more parallel than most people thought :)
Quoted 7 lines
it just occurred to me
that your change can be written equivalently (but now I do think it's cleaner) as:
smap->elem_size=offsetof(struct bpf_local_storage_elem, sdata.data[attr->value_size]);
Sure. will post v3.
Quoted 9 lines
sdata is embedded struct, no pointer dereference, so single
offsetof()should be enough to peer inside itWhichever you prefer, both versions work for me:
Acked-by Andrii Nakryiko <[email protected]>
Thanks for the review.