Skip to content

Commit a4fbfdd

Browse files
stintelborkmann
authored andcommitted
libbpf: Fix BPF_MAP_TYPE_PERF_EVENT_ARRAY auto-pinning
When a BPF map of type BPF_MAP_TYPE_PERF_EVENT_ARRAY doesn't have the max_entries parameter set, the map will be created with max_entries set to the number of available CPUs. When we try to reuse such a pinned map, map_is_reuse_compat will return false, as max_entries in the map definition differs from max_entries of the existing map, causing the following error: libbpf: couldn't reuse pinned map at '/sys/fs/bpf/m_logging': parameter mismatch Fix this by overwriting max_entries in the map definition. For this to work, we need to do this in bpf_object__create_maps, before calling bpf_object__reuse_map. Fixes: 57a00f4 ("libbpf: Add auto-pinning of maps when loading BPF objects") Signed-off-by: Stijn Tintel <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]> Acked-by: Song Liu <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 07609c1 commit a4fbfdd

File tree

1 file changed

+25
-19
lines changed

1 file changed

+25
-19
lines changed

tools/lib/bpf/libbpf.c

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4859,7 +4859,6 @@ static int bpf_object__create_map(struct bpf_object *obj, struct bpf_map *map, b
48594859
LIBBPF_OPTS(bpf_map_create_opts, create_attr);
48604860
struct bpf_map_def *def = &map->def;
48614861
const char *map_name = NULL;
4862-
__u32 max_entries;
48634862
int err = 0;
48644863

48654864
if (kernel_supports(obj, FEAT_PROG_NAME))
@@ -4869,21 +4868,6 @@ static int bpf_object__create_map(struct bpf_object *obj, struct bpf_map *map, b
48694868
create_attr.numa_node = map->numa_node;
48704869
create_attr.map_extra = map->map_extra;
48714870

4872-
if (def->type == BPF_MAP_TYPE_PERF_EVENT_ARRAY && !def->max_entries) {
4873-
int nr_cpus;
4874-
4875-
nr_cpus = libbpf_num_possible_cpus();
4876-
if (nr_cpus < 0) {
4877-
pr_warn("map '%s': failed to determine number of system CPUs: %d\n",
4878-
map->name, nr_cpus);
4879-
return nr_cpus;
4880-
}
4881-
pr_debug("map '%s': setting size to %d\n", map->name, nr_cpus);
4882-
max_entries = nr_cpus;
4883-
} else {
4884-
max_entries = def->max_entries;
4885-
}
4886-
48874871
if (bpf_map__is_struct_ops(map))
48884872
create_attr.btf_vmlinux_value_type_id = map->btf_vmlinux_value_type_id;
48894873

@@ -4933,7 +4917,7 @@ static int bpf_object__create_map(struct bpf_object *obj, struct bpf_map *map, b
49334917

49344918
if (obj->gen_loader) {
49354919
bpf_gen__map_create(obj->gen_loader, def->type, map_name,
4936-
def->key_size, def->value_size, max_entries,
4920+
def->key_size, def->value_size, def->max_entries,
49374921
&create_attr, is_inner ? -1 : map - obj->maps);
49384922
/* Pretend to have valid FD to pass various fd >= 0 checks.
49394923
* This fd == 0 will not be used with any syscall and will be reset to -1 eventually.
@@ -4942,7 +4926,7 @@ static int bpf_object__create_map(struct bpf_object *obj, struct bpf_map *map, b
49424926
} else {
49434927
map->fd = bpf_map_create(def->type, map_name,
49444928
def->key_size, def->value_size,
4945-
max_entries, &create_attr);
4929+
def->max_entries, &create_attr);
49464930
}
49474931
if (map->fd < 0 && (create_attr.btf_key_type_id ||
49484932
create_attr.btf_value_type_id)) {
@@ -4959,7 +4943,7 @@ static int bpf_object__create_map(struct bpf_object *obj, struct bpf_map *map, b
49594943
map->btf_value_type_id = 0;
49604944
map->fd = bpf_map_create(def->type, map_name,
49614945
def->key_size, def->value_size,
4962-
max_entries, &create_attr);
4946+
def->max_entries, &create_attr);
49634947
}
49644948

49654949
err = map->fd < 0 ? -errno : 0;
@@ -5063,6 +5047,24 @@ static int bpf_object_init_prog_arrays(struct bpf_object *obj)
50635047
return 0;
50645048
}
50655049

5050+
static int map_set_def_max_entries(struct bpf_map *map)
5051+
{
5052+
if (map->def.type == BPF_MAP_TYPE_PERF_EVENT_ARRAY && !map->def.max_entries) {
5053+
int nr_cpus;
5054+
5055+
nr_cpus = libbpf_num_possible_cpus();
5056+
if (nr_cpus < 0) {
5057+
pr_warn("map '%s': failed to determine number of system CPUs: %d\n",
5058+
map->name, nr_cpus);
5059+
return nr_cpus;
5060+
}
5061+
pr_debug("map '%s': setting size to %d\n", map->name, nr_cpus);
5062+
map->def.max_entries = nr_cpus;
5063+
}
5064+
5065+
return 0;
5066+
}
5067+
50665068
static int
50675069
bpf_object__create_maps(struct bpf_object *obj)
50685070
{
@@ -5095,6 +5097,10 @@ bpf_object__create_maps(struct bpf_object *obj)
50955097
continue;
50965098
}
50975099

5100+
err = map_set_def_max_entries(map);
5101+
if (err)
5102+
goto err_out;
5103+
50985104
retried = false;
50995105
retry:
51005106
if (map->pin_path) {

0 commit comments

Comments
 (0)