mirror of
https://github.com/netdata/libbpf.git
synced 2026-04-04 23:59:07 +08:00
libbpf: Fix race when pinning maps in parallel
When loading in parallel multiple programs which use the same to-be
pinned map, it is possible that two instances of the loader will call
bpf_object__create_maps() at the same time. If the map doesn't exist
when both instances call bpf_object__reuse_map(), then one of the
instances will fail with EEXIST when calling bpf_map__pin().
Fix the race by retrying reusing a map if bpf_map__pin() returns
EEXIST. The fix is similar to the one in iproute2: e4c4685fd6e4 ("bpf:
Fix race condition with map pinning").
Before retrying the pinning, we don't do any special cleaning of an
internal map state. The closer code inspection revealed that it's not
required:
- bpf_object__create_map(): map->inner_map is destroyed after a
successful call, map->fd is closed if pinning fails.
- bpf_object__populate_internal_map(): created map elements is
destroyed upon close(map->fd).
- init_map_slots(): slots are freed after their initialization.
Signed-off-by: Martynas Pumputis <m@lambda.lt>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20210726152001.34845-1-m@lambda.lt
This commit is contained in:
committed by
Andrii Nakryiko
parent
7c25b1d569
commit
3a0fc666ef
15
src/libbpf.c
15
src/libbpf.c
@@ -4657,10 +4657,13 @@ bpf_object__create_maps(struct bpf_object *obj)
|
|||||||
char *cp, errmsg[STRERR_BUFSIZE];
|
char *cp, errmsg[STRERR_BUFSIZE];
|
||||||
unsigned int i, j;
|
unsigned int i, j;
|
||||||
int err;
|
int err;
|
||||||
|
bool retried;
|
||||||
|
|
||||||
for (i = 0; i < obj->nr_maps; i++) {
|
for (i = 0; i < obj->nr_maps; i++) {
|
||||||
map = &obj->maps[i];
|
map = &obj->maps[i];
|
||||||
|
|
||||||
|
retried = false;
|
||||||
|
retry:
|
||||||
if (map->pin_path) {
|
if (map->pin_path) {
|
||||||
err = bpf_object__reuse_map(map);
|
err = bpf_object__reuse_map(map);
|
||||||
if (err) {
|
if (err) {
|
||||||
@@ -4668,6 +4671,12 @@ bpf_object__create_maps(struct bpf_object *obj)
|
|||||||
map->name);
|
map->name);
|
||||||
goto err_out;
|
goto err_out;
|
||||||
}
|
}
|
||||||
|
if (retried && map->fd < 0) {
|
||||||
|
pr_warn("map '%s': cannot find pinned map\n",
|
||||||
|
map->name);
|
||||||
|
err = -ENOENT;
|
||||||
|
goto err_out;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (map->fd >= 0) {
|
if (map->fd >= 0) {
|
||||||
@@ -4701,9 +4710,13 @@ bpf_object__create_maps(struct bpf_object *obj)
|
|||||||
if (map->pin_path && !map->pinned) {
|
if (map->pin_path && !map->pinned) {
|
||||||
err = bpf_map__pin(map, NULL);
|
err = bpf_map__pin(map, NULL);
|
||||||
if (err) {
|
if (err) {
|
||||||
|
zclose(map->fd);
|
||||||
|
if (!retried && err == -EEXIST) {
|
||||||
|
retried = true;
|
||||||
|
goto retry;
|
||||||
|
}
|
||||||
pr_warn("map '%s': failed to auto-pin at '%s': %d\n",
|
pr_warn("map '%s': failed to auto-pin at '%s': %d\n",
|
||||||
map->name, map->pin_path, err);
|
map->name, map->pin_path, err);
|
||||||
zclose(map->fd);
|
|
||||||
goto err_out;
|
goto err_out;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user