Skip to content

Commit 09156cf

Browse files
brbgregkh
authored andcommitted
libbpf: Fix reuse of pinned map on older kernel
[ Upstream commit 97eb313 ] 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 Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent f94ca31 commit 09156cf

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
@@ -3844,6 +3844,42 @@ static int bpf_map_find_btf_info(struct bpf_object *obj, struct bpf_map *map)
38443844
return 0;
38453845
}
38463846

3847+
static int bpf_get_map_info_from_fdinfo(int fd, struct bpf_map_info *info)
3848+
{
3849+
char file[PATH_MAX], buff[4096];
3850+
FILE *fp;
3851+
__u32 val;
3852+
int err;
3853+
3854+
snprintf(file, sizeof(file), "/proc/%d/fdinfo/%d", getpid(), fd);
3855+
memset(info, 0, sizeof(*info));
3856+
3857+
fp = fopen(file, "r");
3858+
if (!fp) {
3859+
err = -errno;
3860+
pr_warn("failed to open %s: %d. No procfs support?\n", file,
3861+
err);
3862+
return err;
3863+
}
3864+
3865+
while (fgets(buff, sizeof(buff), fp)) {
3866+
if (sscanf(buff, "map_type:\t%u", &val) == 1)
3867+
info->type = val;
3868+
else if (sscanf(buff, "key_size:\t%u", &val) == 1)
3869+
info->key_size = val;
3870+
else if (sscanf(buff, "value_size:\t%u", &val) == 1)
3871+
info->value_size = val;
3872+
else if (sscanf(buff, "max_entries:\t%u", &val) == 1)
3873+
info->max_entries = val;
3874+
else if (sscanf(buff, "map_flags:\t%i", &val) == 1)
3875+
info->map_flags = val;
3876+
}
3877+
3878+
fclose(fp);
3879+
3880+
return 0;
3881+
}
3882+
38473883
int bpf_map__reuse_fd(struct bpf_map *map, int fd)
38483884
{
38493885
struct bpf_map_info info = {};
@@ -3852,6 +3888,8 @@ int bpf_map__reuse_fd(struct bpf_map *map, int fd)
38523888
char *new_name;
38533889

38543890
err = bpf_obj_get_info_by_fd(fd, &info, &len);
3891+
if (err && errno == EINVAL)
3892+
err = bpf_get_map_info_from_fdinfo(fd, &info);
38553893
if (err)
38563894
return err;
38573895

@@ -4318,12 +4356,16 @@ static bool map_is_reuse_compat(const struct bpf_map *map, int map_fd)
43184356
struct bpf_map_info map_info = {};
43194357
char msg[STRERR_BUFSIZE];
43204358
__u32 map_info_len;
4359+
int err;
43214360

43224361
map_info_len = sizeof(map_info);
43234362

4324-
if (bpf_obj_get_info_by_fd(map_fd, &map_info, &map_info_len)) {
4325-
pr_warn("failed to get map info for map FD %d: %s\n",
4326-
map_fd, libbpf_strerror_r(errno, msg, sizeof(msg)));
4363+
err = bpf_obj_get_info_by_fd(map_fd, &map_info, &map_info_len);
4364+
if (err && errno == EINVAL)
4365+
err = bpf_get_map_info_from_fdinfo(map_fd, &map_info);
4366+
if (err) {
4367+
pr_warn("failed to get map info for map FD %d: %s\n", map_fd,
4368+
libbpf_strerror_r(errno, msg, sizeof(msg)));
43274369
return false;
43284370
}
43294371

0 commit comments

Comments
 (0)