Skip to content

Commit 97eb313

Browse files
brbanakryiko
authored andcommitted
libbpf: Fix reuse of pinned map on older kernel
When loading a BPF program with a pinned map, the loader checks whether the pinned map can be reused, i.e. their properties match. To derive such of the pinned map, the loader invokes BPF_OBJ_GET_INFO_BY_FD and then does the comparison. Unfortunately, on < 4.12 kernels the BPF_OBJ_GET_INFO_BY_FD is not available, so loading the program fails with the following error: libbpf: failed to get map info for map FD 5: Invalid argument libbpf: couldn't reuse pinned map at '/sys/fs/bpf/tc/globals/cilium_call_policy': parameter mismatch" libbpf: map 'cilium_call_policy': error reusing pinned map libbpf: map 'cilium_call_policy': failed to create: Invalid argument(-22) libbpf: failed to load object 'bpf_overlay.o' To fix this, fallback to derivation of the map properties via /proc/$PID/fdinfo/$MAP_FD if BPF_OBJ_GET_INFO_BY_FD fails with EINVAL, which can be used as an indicator that the kernel doesn't support the latter. Signed-off-by: Martynas Pumputis <m@lambda.lt> Signed-off-by: Andrii Nakryiko <andrii@kernel.org> Acked-by: John Fastabend <john.fastabend@gmail.com> Link: https://lore.kernel.org/bpf/20210712125552.58705-1-m@lambda.lt
1 parent eff9415 commit 97eb313

1 file changed

Lines changed: 45 additions & 3 deletions

File tree

‎tools/lib/bpf/libbpf.c‎

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3894,6 +3894,42 @@ static int bpf_map_find_btf_info(struct bpf_object *obj, struct bpf_map *map)
38943894
return 0;
38953895
}
38963896

3897+
static int bpf_get_map_info_from_fdinfo(int fd, struct bpf_map_info *info)
3898+
{
3899+
char file[PATH_MAX], buff[4096];
3900+
FILE *fp;
3901+
__u32 val;
3902+
int err;
3903+
3904+
snprintf(file, sizeof(file), "/proc/%d/fdinfo/%d", getpid(), fd);
3905+
memset(info, 0, sizeof(*info));
3906+
3907+
fp = fopen(file, "r");
3908+
if (!fp) {
3909+
err = -errno;
3910+
pr_warn("failed to open %s: %d. No procfs support?\n", file,
3911+
err);
3912+
return err;
3913+
}
3914+
3915+
while (fgets(buff, sizeof(buff), fp)) {
3916+
if (sscanf(buff, "map_type:\t%u", &val) == 1)
3917+
info->type = val;
3918+
else if (sscanf(buff, "key_size:\t%u", &val) == 1)
3919+
info->key_size = val;
3920+
else if (sscanf(buff, "value_size:\t%u", &val) == 1)
3921+
info->value_size = val;
3922+
else if (sscanf(buff, "max_entries:\t%u", &val) == 1)
3923+
info->max_entries = val;
3924+
else if (sscanf(buff, "map_flags:\t%i", &val) == 1)
3925+
info->map_flags = val;
3926+
}
3927+
3928+
fclose(fp);
3929+
3930+
return 0;
3931+
}
3932+
38973933
int bpf_map__reuse_fd(struct bpf_map *map, int fd)
38983934
{
38993935
struct bpf_map_info info = {};
@@ -3902,6 +3938,8 @@ int bpf_map__reuse_fd(struct bpf_map *map, int fd)
39023938
char *new_name;
39033939

39043940
err = bpf_obj_get_info_by_fd(fd, &info, &len);
3941+
if (err && errno == EINVAL)
3942+
err = bpf_get_map_info_from_fdinfo(fd, &info);
39053943
if (err)
39063944
return libbpf_err(err);
39073945

@@ -4381,12 +4419,16 @@ static bool map_is_reuse_compat(const struct bpf_map *map, int map_fd)
43814419
struct bpf_map_info map_info = {};
43824420
char msg[STRERR_BUFSIZE];
43834421
__u32 map_info_len;
4422+
int err;
43844423

43854424
map_info_len = sizeof(map_info);
43864425

4387-
if (bpf_obj_get_info_by_fd(map_fd, &map_info, &map_info_len)) {
4388-
pr_warn("failed to get map info for map FD %d: %s\n",
4389-
map_fd, libbpf_strerror_r(errno, msg, sizeof(msg)));
4426+
err = bpf_obj_get_info_by_fd(map_fd, &map_info, &map_info_len);
4427+
if (err && errno == EINVAL)
4428+
err = bpf_get_map_info_from_fdinfo(map_fd, &map_info);
4429+
if (err) {
4430+
pr_warn("failed to get map info for map FD %d: %s\n", map_fd,
4431+
libbpf_strerror_r(errno, msg, sizeof(msg)));
43904432
return false;
43914433
}
43924434

0 commit comments

Comments
 (0)