On Fri, Jul 8, 2022 at 5:12 AM anquan wu <leiqi1323@xxxxxxxxx> wrote: > > BPF map name was limited to BPF_OBJ_NAME_LEN from https://github.com/torvalds/linux/blob/master/include/uapi/linux/bpf.h#L1286. > > If a map name is defined as being longer than BPF_OBJ_NAME_LEN , it will be truncated to BPF_OBJ_NAME_LEN when a userspace program creates the map using bpf syscall . A pinned map also generates a path in the /sys. > > bpf(BPF_OBJ_GET, {pathname="/sys/fs/bpf/process_pinned_map", bpf_fd=0, file_flags=0}, 144) = -1 ENOENT (No such file or directory) > > bpf(BPF_MAP_CREATE, {map_type=BPF_MAP_TYPE_HASH, key_size=4, value_size=4, max_entries=1024, map_flags=0, inner_map_fd=0, map_name="process_pinned_", map_ifindex=0, btf_fd=3, btf_key_type_id=6, btf_value_type_id=10, btf_vmlinux_value_type_id=0}, 72) = 4 > > bpf(BPF_OBJ_PIN, {pathname="/sys/fs/bpf/process_pinned_map", bpf_fd=4, file_flags=0}, 144) = 0 > > If the previous program wanted to reuse the map,it can not get bpf_map by name, because the name of the program is only partially the same as the name which get from pinned path. > > I came up with a solution. > > If the name of pinned map are the same as the name of bpf object for the first (BPF_OBJ_NAME_LEN - 1), bpf map name still uses the name of bpf object. > > Signed-off-by: anquan.wu <leiqi96@xxxxxxxxxxx> > --- Please read how to properly format the patch. Start with [0] [0] https://kernelnewbies.org/FirstKernelPatch > tools/lib/bpf/libbpf.c | 6 +++++- > 1 file changed, 5 insertions(+), 1 deletion(-) > > diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c > index e89cc9c885b3..73c7f5093073 100644 > --- a/tools/lib/bpf/libbpf.c > +++ b/tools/lib/bpf/libbpf.c > @@ -4337,7 +4337,11 @@ int bpf_map__reuse_fd(struct bpf_map *map, int fd) > if (err) > return libbpf_err(err); > > - new_name = strdup(info.name); > + if (!strncmp(map->name, info.name, strlen(info.name))) { > + new_name = strdup(map->name); > + } else { > + new_name = strdup(info.name); > + } as for the approach... I think it would be a bit safer to improve the name truncation. Make sure that strlen(info.name) is exactly BPF_OBJ_NAME_LEN - 1, and only then if strncmp() matches use map->name. > if (!new_name) > return libbpf_err(-errno); > > --