Commit ec6d5f47 authored by Toke Høiland-Jørgensen's avatar Toke Høiland-Jørgensen Committed by Alexei Starovoitov

libbpf: Unpin auto-pinned maps if loading fails

Since the automatic map-pinning happens during load, it will leave pinned
maps around if the load fails at a later stage. Fix this by unpinning any
pinned maps on cleanup. To avoid unpinning pinned maps that were reused
rather than newly pinned, add a new boolean property on struct bpf_map to
keep track of whether that map was reused or not; and only unpin those maps
that were not reused.

Fixes: 57a00f41 ("libbpf: Add auto-pinning of maps when loading BPF objects")
Signed-off-by: default avatarToke Høiland-Jørgensen <toke@redhat.com>
Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
Acked-by: default avatarAndrii Nakryiko <andriin@fb.com>
Acked-by: default avatarDavid S. Miller <davem@davemloft.net>
Acked-by: default avatarSong Liu <songliubraving@fb.com>
Link: https://lore.kernel.org/bpf/157333184731.88376.9992935027056165873.stgit@toke.dk
parent 451d1dc8
...@@ -229,6 +229,7 @@ struct bpf_map { ...@@ -229,6 +229,7 @@ struct bpf_map {
enum libbpf_map_type libbpf_type; enum libbpf_map_type libbpf_type;
char *pin_path; char *pin_path;
bool pinned; bool pinned;
bool reused;
}; };
struct bpf_secdata { struct bpf_secdata {
...@@ -1995,6 +1996,7 @@ int bpf_map__reuse_fd(struct bpf_map *map, int fd) ...@@ -1995,6 +1996,7 @@ int bpf_map__reuse_fd(struct bpf_map *map, int fd)
map->def.map_flags = info.map_flags; map->def.map_flags = info.map_flags;
map->btf_key_type_id = info.btf_key_type_id; map->btf_key_type_id = info.btf_key_type_id;
map->btf_value_type_id = info.btf_value_type_id; map->btf_value_type_id = info.btf_value_type_id;
map->reused = true;
return 0; return 0;
...@@ -4026,7 +4028,7 @@ int bpf_object__unload(struct bpf_object *obj) ...@@ -4026,7 +4028,7 @@ int bpf_object__unload(struct bpf_object *obj)
int bpf_object__load_xattr(struct bpf_object_load_attr *attr) int bpf_object__load_xattr(struct bpf_object_load_attr *attr)
{ {
struct bpf_object *obj; struct bpf_object *obj;
int err; int err, i;
if (!attr) if (!attr)
return -EINVAL; return -EINVAL;
...@@ -4047,6 +4049,11 @@ int bpf_object__load_xattr(struct bpf_object_load_attr *attr) ...@@ -4047,6 +4049,11 @@ int bpf_object__load_xattr(struct bpf_object_load_attr *attr)
return 0; return 0;
out: out:
/* unpin any maps that were auto-pinned during load */
for (i = 0; i < obj->nr_maps; i++)
if (obj->maps[i].pinned && !obj->maps[i].reused)
bpf_map__unpin(&obj->maps[i], NULL);
bpf_object__unload(obj); bpf_object__unload(obj);
pr_warn("failed to load object '%s'\n", obj->path); pr_warn("failed to load object '%s'\n", obj->path);
return err; return err;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment