Skip to content

Commit cba41bb

Browse files
GunshipPenguinMartin KaFai Lau
authored andcommitted
bpf: Fix elem_size not being set for inner maps
Commit d937bc3 ("bpf: make uniform use of array->elem_size everywhere in arraymap.c") changed array_map_gen_lookup to use array->elem_size instead of round_up(map->value_size, 8) as the element size when generating code to access a value in an array map. array->elem_size, however, is not set by bpf_map_meta_alloc when initializing an BPF_MAP_TYPE_ARRAY_OF_MAPS or BPF_MAP_TYPE_HASH_OF_MAPS. This results in array_map_gen_lookup incorrectly outputting code that always accesses index 0 in the array (as the index will be calculated via a multiplication with the element size, which is incorrectly set to 0). Set elem_size on the bpf_array object when allocating an array or hash of maps to fix this. Fixes: d937bc3 ("bpf: make uniform use of array->elem_size everywhere in arraymap.c") Signed-off-by: Rhys Rustad-Elliott <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Martin KaFai Lau <[email protected]>
1 parent b0fd185 commit cba41bb

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

kernel/bpf/map_in_map.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,13 @@ struct bpf_map *bpf_map_meta_alloc(int inner_map_ufd)
6969
/* Misc members not needed in bpf_map_meta_equal() check. */
7070
inner_map_meta->ops = inner_map->ops;
7171
if (inner_map->ops == &array_map_ops) {
72+
struct bpf_array *inner_array_meta =
73+
container_of(inner_map_meta, struct bpf_array, map);
74+
struct bpf_array *inner_array = container_of(inner_map, struct bpf_array, map);
75+
76+
inner_array_meta->index_mask = inner_array->index_mask;
77+
inner_array_meta->elem_size = inner_array->elem_size;
7278
inner_map_meta->bypass_spec_v1 = inner_map->bypass_spec_v1;
73-
container_of(inner_map_meta, struct bpf_array, map)->index_mask =
74-
container_of(inner_map, struct bpf_array, map)->index_mask;
7579
}
7680

7781
fdput(f);

0 commit comments

Comments
 (0)