Skip to contentSkip to conversation

bpf: Update max_entries for array maps

PATCH In review Florian Lehner 2 participants 2 messages Oct 25, 2022 – Oct 26, 2022 BPF

lore
Share & tools
lore
#0 lore raw

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
index 832b2659e96e..9411fa255ccc 100644
@@ -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
#1 lore raw

Hi,

Quoted 16 lines · Florian Lehner

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 +0 -0
index 832b2659e96e..9411fa255ccc 100644

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.