Skip to contentSkip to conversation

bpf: fix reuse of DEVMAP

PATCH New Yureka Lilian 3 participants 4 messages Aug 11, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 bpf: fix reuse of DEVMAP

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, it BPF_F_RDONLY_PROG being set on a map being created is invalid.

Thus, match the BPF_F_RDONLY_PROG flag being set on the new map when checking for compatibility with an existing DEVMAP

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

---
 tools/lib/bpf/libbpf.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
tools/lib/bpf/libbpf.c +11 -1
index fb4d92c5c..a554d7fff 100644
@@ -5081,6 +5081,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);
@@ -5093,11 +5094,20 @@ 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, match the BPF_F_RDONLY_PROG flag being set on the new
+	 * map 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_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);
 }
 
Signature · 1 line
2.50.1
#1 lore raw

PATCH 1/1 bpf: fix reuse of DEVMAP

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, it BPF_F_RDONLY_PROG being set on a map being created is invalid.

Thus, match the BPF_F_RDONLY_PROG flag being set on the new map when checking for compatibility with an existing DEVMAP

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

---
 tools/lib/bpf/libbpf.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
tools/lib/bpf/libbpf.c +11 -1
index fb4d92c5c..a554d7fff 100644
@@ -5081,6 +5081,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);
@@ -5093,11 +5094,20 @@ 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, match the BPF_F_RDONLY_PROG flag being set on the new
+	 * map 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_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);
 }
 
Signature · 1 line
2.50.1
maintainer
#2 lore raw
Quoted 7 lines · Yureka Lilian

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, it BPF_F_RDONLY_PROG being set on a map being created is invalid.

Thus, match the BPF_F_RDONLY_PROG flag being set on the new map when checking for compatibility with an existing DEVMAP

Quoted 6 lines

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

---

The change makes sense to me, could you please add a selftest?

[...]

#3 lore raw
In reply to Yureka Lilian
Quoted 4 lines · Yureka Lilian

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, it BPF_F_RDONLY_PROG being set on a map being created is invalid.

Quoted 8 lines

Thus, match the BPF_F_RDONLY_PROG flag being set on the new map when checking for compatibility with an existing DEVMAP

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

This should be a fix for this commit?

Fixes 0cdbb4b09a06 ("devmap: Allow map lookups from eBPF")
Quoted 18 lines
---
  tools/lib/bpf/libbpf.c | 12 +++++++++++-
  1 file changed, 11 insertions(+), 1 deletion(-)
tools/lib/bpf/libbpf.c +1 -0
index fb4d92c5c..a554d7fff 100644
@@ -5081,6 +5081,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);
@@ -5093,11 +5094,20 @@ static bool map_is_reuse_compat(const struct bpf_map *map, int map_fd)
  		return false;

tools/lib/bpf/libbpf.c static bool map_is_reuse_compat(const struct bpf_map *map, int map_fd) · line 5104

Quoted 10 lines
tools/lib/bpf/libbpf.c +8 -0
  	}
 
+	/* 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, match the BPF_F_RDONLY_PROG flag being set on the new
+	 * map 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_flags_for_check |= BPF_F_RDONLY_PROG;

Does it break older kernels that do not set the RDONLY_PROG bit implicitly by itself?

Quoted 10 lines
tools/lib/bpf/libbpf.c +2 -1
+
  	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);
  }