bpf: Update max_entries for array maps
PATCH 1/1 bpf: Update max_entries for array maps
To improve memory handling and alignment max_entries is rounded up before using its value to allocate memory.
This can lead to a situation where more memory is allocated than usable if max_entries is no adjusted accordingly. So this change updates max_entries in order to make the allocated memory available.
--- kernel/bpf/arraymap.c | 1 + 1 file changed, 1 insertion(+)
kernel/bpf/arraymap.c +1 -0
@@ -145,6 +145,7 @@ static struct bpf_map *array_map_alloc(union bpf_attr *attr)/* copy mandatory map attributes */ bpf_map_init_from_attr (&array->map, attr); array->elem_size = elem_size; + array->map.max_entries = max_entries;if (percpu &&bpf_array_alloc_percpu (array)) {bpf_map_area_free (array);
Signature · 1 line
2.37.3
Hi,
Quoted 16 lines · Florian Lehner
To improve memory handling and alignment
max_entriesis rounded up before using its value to allocate memory.
This can lead to a situation where more memory is allocated than usable ifmax_entriesis no adjusted accordingly. So this change updatesmax_entriesin order to make the allocated memory available.Signed-off-by Florian Lehner <[email protected]>--- kernel/bpf/arraymap.c | 1 + 1 file changed, 1 insertion(+)kernel/bpf/arraymap.c +0
-0
kernel/bpf/arraymap.c static struct bpf_map *array_map_alloc(union bpf_attr *attr) · line 151
Quoted 7 lines
kernel/bpf/arraymap.c +1
-0@@ -145,6 +145,7 @@ static struct bpf_map *array_map_alloc(union bpf_attr *attr)/* copy mandatory map attributes */ bpf_map_init_from_attr (&array->map, attr); array->elem_size = elem_size; + array->map.max_entries = max_entries;if (percpu &&bpf_array_alloc_percpu (array)) {bpf_map_area_free (array);
The override of max_entries is unnecessary and is also wrong. bpf_array_alloc_percpu() will use array->map.max_entries to allocate per-cpu value, and if using the rounded-up max_entries, there will be memory waste because the extra allocated per-cpu values should not be accessible to bpf program or user-space program.