[PATCH v2 1/2] bpf: fix reuse of DEVMAP

Yureka Lilian posted 2 patches 1 month, 3 weeks ago
[PATCH v2 1/2] bpf: fix reuse of DEVMAP
Posted by Yureka Lilian 1 month, 3 weeks ago
Previously, re-using pinned DEVMAP maps would always fail, because
get_map_info on a DEVMAP always returns flags with BPF_F_RDONLY_PROG set,
but BPF_F_RDONLY_PROG being set on a map during creation is invalid.

Thus, ignore the BPF_F_RDONLY_PROG flag on both sides when checking for
compatibility with an existing DEVMAP.

Ignoring it on both sides ensures that it continues to work on older
kernels which don't set BPF_F_RDONLY_PROG on get_map_info.

The same problem is handled in a third-party ebpf library:
- https://github.com/cilium/ebpf/issues/925
- https://github.com/cilium/ebpf/pull/930

Fixes: 0cdbb4b09a06 ("devmap: Allow map lookups from eBPF")
Signed-off-by: Yureka Lilian <yuka@yuka.dev>
---
 tools/lib/bpf/libbpf.c | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)

diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index d41ee26b9..049b0c400 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -5076,6 +5076,7 @@ static bool map_is_reuse_compat(const struct bpf_map *map, int map_fd)
 {
 	struct bpf_map_info map_info;
 	__u32 map_info_len = sizeof(map_info);
+	__u32 map_flags_for_check = map->def.map_flags;
 	int err;
 
 	memset(&map_info, 0, map_info_len);
@@ -5088,11 +5089,22 @@ static bool map_is_reuse_compat(const struct bpf_map *map, int map_fd)
 		return false;
 	}
 
+	/* get_map_info on a DEVMAP will always return flags with
+	 * BPF_F_RDONLY_PROG set, but it will never be set on a map
+	 * being created.
+	 * Thus, ignore the BPF_F_RDONLY_PROG flag on both sides when
+	 * checking for compatibility with an existing DEVMAP.
+	 */
+	if (map->def.type == BPF_MAP_TYPE_DEVMAP || map->def.type == BPF_MAP_TYPE_DEVMAP_HASH) {
+		map_info.map_flags |= BPF_F_RDONLY_PROG;
+		map_flags_for_check |= BPF_F_RDONLY_PROG;
+	}
+
 	return (map_info.type == map->def.type &&
 		map_info.key_size == map->def.key_size &&
 		map_info.value_size == map->def.value_size &&
 		map_info.max_entries == map->def.max_entries &&
-		map_info.map_flags == map->def.map_flags &&
+		map_info.map_flags == map_flags_for_check &&
 		map_info.map_extra == map->map_extra);
 }
 
-- 
2.50.1
Re: [PATCH v2 1/2] bpf: fix reuse of DEVMAP
Posted by Andrii Nakryiko 1 month, 3 weeks ago
On Wed, Aug 13, 2025 at 1:09 PM Yureka Lilian <yuka@yuka.dev> wrote:
>
> Previously, re-using pinned DEVMAP maps would always fail, because
> get_map_info on a DEVMAP always returns flags with BPF_F_RDONLY_PROG set,
> but BPF_F_RDONLY_PROG being set on a map during creation is invalid.
>
> Thus, ignore the BPF_F_RDONLY_PROG flag on both sides when checking for
> compatibility with an existing DEVMAP.
>
> Ignoring it on both sides ensures that it continues to work on older
> kernels which don't set BPF_F_RDONLY_PROG on get_map_info.
>
> The same problem is handled in a third-party ebpf library:
> - https://github.com/cilium/ebpf/issues/925
> - https://github.com/cilium/ebpf/pull/930
>
> Fixes: 0cdbb4b09a06 ("devmap: Allow map lookups from eBPF")
> Signed-off-by: Yureka Lilian <yuka@yuka.dev>
> ---
>  tools/lib/bpf/libbpf.c | 14 +++++++++++++-
>  1 file changed, 13 insertions(+), 1 deletion(-)
>
> diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
> index d41ee26b9..049b0c400 100644
> --- a/tools/lib/bpf/libbpf.c
> +++ b/tools/lib/bpf/libbpf.c
> @@ -5076,6 +5076,7 @@ static bool map_is_reuse_compat(const struct bpf_map *map, int map_fd)
>  {
>         struct bpf_map_info map_info;
>         __u32 map_info_len = sizeof(map_info);
> +       __u32 map_flags_for_check = map->def.map_flags;
>         int err;
>
>         memset(&map_info, 0, map_info_len);
> @@ -5088,11 +5089,22 @@ static bool map_is_reuse_compat(const struct bpf_map *map, int map_fd)
>                 return false;
>         }
>
> +       /* get_map_info on a DEVMAP will always return flags with
> +        * BPF_F_RDONLY_PROG set, but it will never be set on a map
> +        * being created.
> +        * Thus, ignore the BPF_F_RDONLY_PROG flag on both sides when
> +        * checking for compatibility with an existing DEVMAP.
> +        */
> +       if (map->def.type == BPF_MAP_TYPE_DEVMAP || map->def.type == BPF_MAP_TYPE_DEVMAP_HASH) {
> +               map_info.map_flags |= BPF_F_RDONLY_PROG;
> +               map_flags_for_check |= BPF_F_RDONLY_PROG;
> +       }

can we instead clear BPF_F_RDONLY_PROG in map_info.map_flags? and then
keep using map->def.map_flags directly

pw-bot: cr


> +
>         return (map_info.type == map->def.type &&
>                 map_info.key_size == map->def.key_size &&
>                 map_info.value_size == map->def.value_size &&
>                 map_info.max_entries == map->def.max_entries &&
> -               map_info.map_flags == map->def.map_flags &&
> +               map_info.map_flags == map_flags_for_check &&
>                 map_info.map_extra == map->map_extra);
>  }
>
> --
> 2.50.1
>
Re: [PATCH v2 1/2] bpf: fix reuse of DEVMAP
Posted by Eduard Zingerman 1 month, 3 weeks ago
On Wed, 2025-08-13 at 22:09 +0200, Yureka Lilian wrote:
> Previously, re-using pinned DEVMAP maps would always fail, because
> get_map_info on a DEVMAP always returns flags with BPF_F_RDONLY_PROG set,
> but BPF_F_RDONLY_PROG being set on a map during creation is invalid.
> 
> Thus, ignore the BPF_F_RDONLY_PROG flag on both sides when checking for
> compatibility with an existing DEVMAP.
> 
> Ignoring it on both sides ensures that it continues to work on older
> kernels which don't set BPF_F_RDONLY_PROG on get_map_info.
> 
> The same problem is handled in a third-party ebpf library:
> - https://github.com/cilium/ebpf/issues/925
> - https://github.com/cilium/ebpf/pull/930
> 
> Fixes: 0cdbb4b09a06 ("devmap: Allow map lookups from eBPF")
> Signed-off-by: Yureka Lilian <yuka@yuka.dev>
> ---

Reviewed-by: Eduard Zingerman <eddyz87@gmail.com>

[...]