bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] bpf: fix reuse of DEVMAP
@ 2025-08-11  9:10 Yureka Lilian
  2025-08-11  9:39 ` Yureka Lilian
  0 siblings, 1 reply; 4+ messages in thread
From: Yureka Lilian @ 2025-08-11  9:10 UTC (permalink / raw)
  To: bpf; +Cc: 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

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

Signed-off-by: Yureka Lilian <yuka@yuka.dev>
---
 tools/lib/bpf/libbpf.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index fb4d92c5c..a554d7fff 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -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);
 }
 
-- 
2.50.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH] bpf: fix reuse of DEVMAP
  2025-08-11  9:10 [PATCH] bpf: fix reuse of DEVMAP Yureka Lilian
@ 2025-08-11  9:39 ` Yureka Lilian
  2025-08-11 17:29   ` Eduard Zingerman
  2025-08-11 19:38   ` Martin KaFai Lau
  0 siblings, 2 replies; 4+ messages in thread
From: Yureka Lilian @ 2025-08-11  9:39 UTC (permalink / raw)
  To: Andrii Nakryiko, Eduard Zingerman, Alexei Starovoitov,
	Daniel Borkmann, Martin KaFai Lau, Song Liu, Yonghong Song,
	John Fastabend, KP Singh, Stanislav Fomichev, Hao Luo, Jiri Olsa
  Cc: Yureka Lilian, bpf, linux-kernel

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

Signed-off-by: Yureka Lilian <yuka@yuka.dev>
---
 tools/lib/bpf/libbpf.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index fb4d92c5c..a554d7fff 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -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);
 }
 
-- 
2.50.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] bpf: fix reuse of DEVMAP
  2025-08-11  9:39 ` Yureka Lilian
@ 2025-08-11 17:29   ` Eduard Zingerman
  2025-08-11 19:38   ` Martin KaFai Lau
  1 sibling, 0 replies; 4+ messages in thread
From: Eduard Zingerman @ 2025-08-11 17:29 UTC (permalink / raw)
  To: Yureka Lilian, Andrii Nakryiko, Alexei Starovoitov,
	Daniel Borkmann, Martin KaFai Lau, Song Liu, Yonghong Song,
	John Fastabend, KP Singh, Stanislav Fomichev, Hao Luo, Jiri Olsa
  Cc: bpf, linux-kernel

On Mon, 2025-08-11 at 11:39 +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,
> 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
> 
> Signed-off-by: Yureka Lilian <yuka@yuka.dev>
> ---

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

[...]

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] bpf: fix reuse of DEVMAP
  2025-08-11  9:39 ` Yureka Lilian
  2025-08-11 17:29   ` Eduard Zingerman
@ 2025-08-11 19:38   ` Martin KaFai Lau
  1 sibling, 0 replies; 4+ messages in thread
From: Martin KaFai Lau @ 2025-08-11 19:38 UTC (permalink / raw)
  To: Yureka Lilian
  Cc: Andrii Nakryiko, Eduard Zingerman, Alexei Starovoitov,
	Daniel Borkmann, Song Liu, Yonghong Song, John Fastabend,
	KP Singh, Stanislav Fomichev, Hao Luo, Jiri Olsa,
	Toke Høiland-Jørgensen, bpf, linux-kernel

On 8/11/25 2:39 AM, 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,
> 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
> 
> Signed-off-by: Yureka Lilian <yuka@yuka.dev>

This should be a fix for this commit?

Fixes: 0cdbb4b09a06 ("devmap: Allow map lookups from eBPF")

> ---
>   tools/lib/bpf/libbpf.c | 12 +++++++++++-
>   1 file changed, 11 insertions(+), 1 deletion(-)
> 
> diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
> index fb4d92c5c..a554d7fff 100644
> --- a/tools/lib/bpf/libbpf.c
> +++ b/tools/lib/bpf/libbpf.c
> @@ -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;

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

> +
>   	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);
>   }
>   


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2025-08-11 19:38 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-08-11  9:10 [PATCH] bpf: fix reuse of DEVMAP Yureka Lilian
2025-08-11  9:39 ` Yureka Lilian
2025-08-11 17:29   ` Eduard Zingerman
2025-08-11 19:38   ` Martin KaFai Lau

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).