bpf: Reduce smap->elem_size
Martin KaFai Lau
reviewerPATCH 1/1 bpf: Reduce smap->elem_size
From: Martin KaFai Lau <[email protected]>
'struct bpf_local_storage_elem' has a 56 bytes padding at the end which can be used for attr->value_size. The current smap->elem_size calculation is unnecessarily inflated by 56 bytes.
The patch is to fix it by calculating the smap->elem_size with offsetof().
--- 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 = offsetof(struct bpf_local_storage_elem , sdata) + + offsetof(struct bpf_local_storage_data, data[attr->value_size]);return smap; }
Signature · 1 line
2.30.2
Quoted 4 lines · Martin KaFai Lau
From: Martin KaFai Lau <[email protected]>
'struct
bpf_local_storage_elem' has a 56 bytes padding at the end which can be used forattr->value_size. The currentsmap->elem_size
'can be' => 'will be'?
Quoted 6 lines
calculation is unnecessarily inflated by 56 bytes.
The patch is to fix it by calculating the
smap->elem_sizewithoffsetof().Signed-off-by Martin KaFai Lau <[email protected]>
Quoted 19 lines
--- 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 = offsetof(struct bpf_local_storage_elem , sdata) + + offsetof(struct bpf_local_storage_data, data[attr->value_size]);return smap; }
A message that is not in the archive (sent off-list or not yet received): [email protected]
A message that is not in the archive (sent off-list or not yet received): CAEf4BzbJGpkhyio9+S1U=bnYycaknw0SNada6orzNV_+VfPwGw@mail.gmail.com
Martin KaFai Lau
reviewer[ Cc: the bpf list back. I dropped it by mistake in my last reply. ]
Quoted 24 lines · Andrii Nakryiko
Quoted 12 lines · Martin KaFai Lau
Quoted 9 lines · Yonghong Song
Quoted 4 lines · Martin KaFai Lau
From: Martin KaFai Lau <[email protected]>
'struct
bpf_local_storage_elem' has a 56 bytes padding at the end which can be used forattr->value_size. The currentsmap->elem_size'can be' => 'will be'?
Quoted 7 lines
I used 'can be' to describe the current situation that the padding is not used for the map's value. I may have used the wrong tense?
I can rephrase it to something like,
'struct
bpf_local_storage_elem' has a 56 bytes padding at the end which is currently not used for theattr->value_size.I actually found the use of
attr->value_sizeto mean "value content" more confusing than can vs will be.
Quoted 8 lines
As a suggestion something like the below?
'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. Useoffsetof()instead to calculate more exact memory use.
SGTM.
Quoted 7 lines
btw, I think you can calculate the same arguably a bit more straightforwardly as:
smap->elem_size=offsetofend(struct bpf_local_storage_elem, sdata)+attr->value_size;
Sure. will change.
Quoted 5 lines
right?
but TIL that
offsetof(struct bpf_local_storage_data, data[attr->value_size])does the right thing
Yeah, I think I have seen other places using it also. I found it more intuitive to read with array[size] to tell there is a flexible array at the end. I am not super attached to it and will change to the way above.
Quoted 50 lines · Martin KaFai Lau
[ Cc: the bpf list back. I dropped it by mistake in my last reply. ]
Quoted 24 lines · Andrii Nakryiko
Quoted 12 lines · Martin KaFai Lau
Quoted 9 lines · Yonghong Song
Quoted 4 lines · Martin KaFai Lau
From: Martin KaFai Lau <[email protected]>
'struct
bpf_local_storage_elem' has a 56 bytes padding at the end which can be used forattr->value_size. The currentsmap->elem_size'can be' => 'will be'?
Quoted 7 lines
I used 'can be' to describe the current situation that the padding is not used for the map's value. I may have used the wrong tense?
I can rephrase it to something like,
'struct
bpf_local_storage_elem' has a 56 bytes padding at the end which is currently not used for theattr->value_size.I actually found the use of
attr->value_sizeto mean "value content" more confusing than can vs will be.Quoted 8 lines
As a suggestion something like the below?
'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. Useoffsetof()instead to calculate more exact memory use.SGTM.
Quoted 7 lines
btw, I think you can calculate the same arguably a bit more straightforwardly as:
smap->elem_size=offsetofend(struct bpf_local_storage_elem, sdata)+attr->value_size;Sure. will change.
Quoted 1 line
Quoted 9 lines
Quoted 4 lines
right?
but TIL that
offsetof(struct bpf_local_storage_data, data[attr->value_size])does the right thingYeah, I think I have seen other places using it also. I found it more intuitive to read with array[size] to tell there is a flexible array at the end. I am not super attached to it and will change to the way above.
I don't care either, was just surprised. But 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