Skip to content

Commit f19c39d

Browse files
liuhangbinkernel-patches-bot
authored andcommitted
libbpf: check if pin_path was set even map fd exist
Say a user reuse map fd after creating a map manually and set the pin_path, then load the object via libbpf. In libbpf bpf_object__create_maps(), bpf_object__reuse_map() will return 0 if there is no pinned map in map->pin_path. Then after checking if map fd exist, we should also check if pin_path was set and do bpf_map__pin() instead of continue the loop. Fix it by creating map if fd not exist and continue checking pin_path after that. v2: keep if condition with existing order Suggested-by: Andrii Nakryiko <[email protected]> Signed-off-by: Hangbin Liu <[email protected]> Acked-by: Andrii Nakryiko <[email protected]>
1 parent f87d39d commit f19c39d

File tree

1 file changed

+18
-19
lines changed

1 file changed

+18
-19
lines changed

tools/lib/bpf/libbpf.c

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4256,29 +4256,28 @@ bpf_object__create_maps(struct bpf_object *obj)
42564256
if (map->fd >= 0) {
42574257
pr_debug("map '%s': skipping creation (preset fd=%d)\n",
42584258
map->name, map->fd);
4259-
continue;
4260-
}
4261-
4262-
err = bpf_object__create_map(obj, map);
4263-
if (err)
4264-
goto err_out;
4259+
} else {
4260+
err = bpf_object__create_map(obj, map);
4261+
if (err)
4262+
goto err_out;
42654263

4266-
pr_debug("map '%s': created successfully, fd=%d\n", map->name,
4267-
map->fd);
4264+
pr_debug("map '%s': created successfully, fd=%d\n",
4265+
map->name, map->fd);
42684266

4269-
if (bpf_map__is_internal(map)) {
4270-
err = bpf_object__populate_internal_map(obj, map);
4271-
if (err < 0) {
4272-
zclose(map->fd);
4273-
goto err_out;
4267+
if (bpf_map__is_internal(map)) {
4268+
err = bpf_object__populate_internal_map(obj, map);
4269+
if (err < 0) {
4270+
zclose(map->fd);
4271+
goto err_out;
4272+
}
42744273
}
4275-
}
42764274

4277-
if (map->init_slots_sz) {
4278-
err = init_map_slots(map);
4279-
if (err < 0) {
4280-
zclose(map->fd);
4281-
goto err_out;
4275+
if (map->init_slots_sz) {
4276+
err = init_map_slots(map);
4277+
if (err < 0) {
4278+
zclose(map->fd);
4279+
goto err_out;
4280+
}
42824281
}
42834282
}
42844283

0 commit comments

Comments
 (0)