linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v3 0/2] libbpf: fix reuse of DEVMAP
@ 2025-08-14 18:01 Yureka Lilian
  2025-08-14 18:01 ` [PATCH v3 1/2] " Yureka Lilian
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Yureka Lilian @ 2025-08-14 18:01 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

changes in v3:

- instead of setting BPF_F_RDONLY_PROG on both sides, just
  clear BPF_F_RDONLY_PROG in map_info.map_flags as suggested
  by Andrii Nakryiko

- in the test, use ASSERT_* instead of CHECK
- shorten the test by using open_and_load from the skel
- in the test, drop NULL check before unloading/destroying bpf objs

- start the commit messages with "libbpf" and "selftests/bpf"
  respectively instead of just "bpf"

changes in v2:

- preserve compatibility with older kernels
- add a basic selftest covering the re-use of DEVMAP maps

Yureka Lilian (2):
  libbpf: fix reuse of DEVMAP
  selftests/bpf: add test for DEVMAP reuse

 tools/lib/bpf/libbpf.c                        | 11 ++++
 .../bpf/prog_tests/pinning_devmap_reuse.c     | 50 +++++++++++++++++++
 .../selftests/bpf/progs/test_pinning_devmap.c | 20 ++++++++
 3 files changed, 81 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/pinning_devmap_reuse.c
 create mode 100644 tools/testing/selftests/bpf/progs/test_pinning_devmap.c

-- 
2.50.1


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

* [PATCH v3 1/2] libbpf: fix reuse of DEVMAP
  2025-08-14 18:01 [PATCH v3 0/2] libbpf: fix reuse of DEVMAP Yureka Lilian
@ 2025-08-14 18:01 ` Yureka Lilian
  2025-08-14 18:01 ` [PATCH v3 2/2] selftests/bpf: add test for DEVMAP reuse Yureka Lilian
  2025-08-16  0:00 ` [PATCH v3 0/2] libbpf: fix reuse of DEVMAP patchwork-bot+netdevbpf
  2 siblings, 0 replies; 5+ messages in thread
From: Yureka Lilian @ 2025-08-14 18:01 UTC (permalink / raw)
  To: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
	Martin KaFai Lau, Eduard Zingerman, 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,
but BPF_F_RDONLY_PROG being set on a map during creation is invalid.

Thus, ignore the BPF_F_RDONLY_PROG flag in the flags returned from
get_map_info when checking for compatibility with an existing DEVMAP.

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 | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index d41ee26b9..affcbb231 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -5088,6 +5088,17 @@ 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 in the flags
+	 * returned from get_map_info 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;
+	}
+
 	return (map_info.type == map->def.type &&
 		map_info.key_size == map->def.key_size &&
 		map_info.value_size == map->def.value_size &&
-- 
2.50.1


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

* [PATCH v3 2/2] selftests/bpf: add test for DEVMAP reuse
  2025-08-14 18:01 [PATCH v3 0/2] libbpf: fix reuse of DEVMAP Yureka Lilian
  2025-08-14 18:01 ` [PATCH v3 1/2] " Yureka Lilian
@ 2025-08-14 18:01 ` Yureka Lilian
  2025-08-14 18:08   ` Eduard Zingerman
  2025-08-16  0:00 ` [PATCH v3 0/2] libbpf: fix reuse of DEVMAP patchwork-bot+netdevbpf
  2 siblings, 1 reply; 5+ messages in thread
From: Yureka Lilian @ 2025-08-14 18:01 UTC (permalink / raw)
  To: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
	Martin KaFai Lau, Eduard Zingerman, Song Liu, Yonghong Song,
	John Fastabend, KP Singh, Stanislav Fomichev, Hao Luo, Jiri Olsa
  Cc: Yureka Lilian, bpf, linux-kernel

The test covers basic re-use of a pinned DEVMAP map,
with both matching and mismatching parameters.

Signed-off-by: Yureka Lilian <yuka@yuka.dev>
---
 .../bpf/prog_tests/pinning_devmap_reuse.c     | 50 +++++++++++++++++++
 .../selftests/bpf/progs/test_pinning_devmap.c | 20 ++++++++
 2 files changed, 70 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/pinning_devmap_reuse.c
 create mode 100644 tools/testing/selftests/bpf/progs/test_pinning_devmap.c

diff --git a/tools/testing/selftests/bpf/prog_tests/pinning_devmap_reuse.c b/tools/testing/selftests/bpf/prog_tests/pinning_devmap_reuse.c
new file mode 100644
index 000000000..9ae49b587
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/pinning_devmap_reuse.c
@@ -0,0 +1,50 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include <test_progs.h>
+
+
+#include "test_pinning_devmap.skel.h"
+
+void test_pinning_devmap_reuse(void)
+{
+	const char *pinpath1 = "/sys/fs/bpf/pinmap1";
+	const char *pinpath2 = "/sys/fs/bpf/pinmap2";
+	struct test_pinning_devmap *skel1 = NULL, *skel2 = NULL;
+	int err;
+	DECLARE_LIBBPF_OPTS(bpf_object_open_opts, opts);
+
+	/* load the object a first time */
+	skel1 = test_pinning_devmap__open_and_load();
+	if (!ASSERT_OK_PTR(skel1, "skel_load1"))
+		goto out;
+
+	/* load the object a second time, re-using the pinned map */
+	skel2 = test_pinning_devmap__open_and_load();
+	if (!ASSERT_OK_PTR(skel2, "skel_load2"))
+		goto out;
+
+	/* we can close the reference safely without
+	 * the map's refcount falling to 0
+	 */
+	test_pinning_devmap__destroy(skel1);
+	skel1 = NULL;
+
+	/* now, swap the pins */
+	err = renameat2(0, pinpath1, 0, pinpath2, RENAME_EXCHANGE);
+	if (!ASSERT_OK(err, "swap pins"))
+		goto out;
+
+	/* load the object again, this time the re-use should fail */
+	skel1 = test_pinning_devmap__open_and_load();
+	if (!ASSERT_ERR_PTR(skel1, "skel_load3"))
+		goto out;
+
+out:
+	unlink(pinpath1);
+	unlink(pinpath2);
+	test_pinning_devmap__destroy(skel1);
+	test_pinning_devmap__destroy(skel2);
+}
diff --git a/tools/testing/selftests/bpf/progs/test_pinning_devmap.c b/tools/testing/selftests/bpf/progs/test_pinning_devmap.c
new file mode 100644
index 000000000..c855f8f87
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/test_pinning_devmap.c
@@ -0,0 +1,20 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <linux/bpf.h>
+#include <bpf/bpf_helpers.h>
+
+struct {
+	__uint(type, BPF_MAP_TYPE_DEVMAP);
+	__uint(max_entries, 1);
+	__type(key, __u32);
+	__type(value, __u32);
+	__uint(pinning, LIBBPF_PIN_BY_NAME);
+} pinmap1 SEC(".maps");
+
+struct {
+	__uint(type, BPF_MAP_TYPE_DEVMAP);
+	__uint(max_entries, 2);
+	__type(key, __u32);
+	__type(value, __u32);
+	__uint(pinning, LIBBPF_PIN_BY_NAME);
+} pinmap2 SEC(".maps");
-- 
2.50.1


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

* Re: [PATCH v3 2/2] selftests/bpf: add test for DEVMAP reuse
  2025-08-14 18:01 ` [PATCH v3 2/2] selftests/bpf: add test for DEVMAP reuse Yureka Lilian
@ 2025-08-14 18:08   ` Eduard Zingerman
  0 siblings, 0 replies; 5+ messages in thread
From: Eduard Zingerman @ 2025-08-14 18:08 UTC (permalink / raw)
  To: Yureka Lilian, Alexei Starovoitov, Daniel Borkmann,
	Andrii Nakryiko, Martin KaFai Lau, Song Liu, Yonghong Song,
	John Fastabend, KP Singh, Stanislav Fomichev, Hao Luo, Jiri Olsa
  Cc: bpf, linux-kernel

On Thu, 2025-08-14 at 20:01 +0200, Yureka Lilian wrote:
> The test covers basic re-use of a pinned DEVMAP map,
> with both matching and mismatching parameters.
> 
> Signed-off-by: Yureka Lilian <yuka@yuka.dev>
> ---

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

[...]

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

* Re: [PATCH v3 0/2] libbpf: fix reuse of DEVMAP
  2025-08-14 18:01 [PATCH v3 0/2] libbpf: fix reuse of DEVMAP Yureka Lilian
  2025-08-14 18:01 ` [PATCH v3 1/2] " Yureka Lilian
  2025-08-14 18:01 ` [PATCH v3 2/2] selftests/bpf: add test for DEVMAP reuse Yureka Lilian
@ 2025-08-16  0:00 ` patchwork-bot+netdevbpf
  2 siblings, 0 replies; 5+ messages in thread
From: patchwork-bot+netdevbpf @ 2025-08-16  0:00 UTC (permalink / raw)
  To: Yureka Lilian
  Cc: andrii, eddyz87, ast, daniel, martin.lau, song, yonghong.song,
	john.fastabend, kpsingh, sdf, haoluo, jolsa, bpf, linux-kernel

Hello:

This series was applied to bpf/bpf-next.git (master)
by Andrii Nakryiko <andrii@kernel.org>:

On Thu, 14 Aug 2025 20:01:11 +0200 you wrote:
> changes in v3:
> 
> - instead of setting BPF_F_RDONLY_PROG on both sides, just
>   clear BPF_F_RDONLY_PROG in map_info.map_flags as suggested
>   by Andrii Nakryiko
> 
> - in the test, use ASSERT_* instead of CHECK
> - shorten the test by using open_and_load from the skel
> - in the test, drop NULL check before unloading/destroying bpf objs
> 
> [...]

Here is the summary with links:
  - [v3,1/2] libbpf: fix reuse of DEVMAP
    (no matching commit)
  - [v3,2/2] selftests/bpf: add test for DEVMAP reuse
    https://git.kernel.org/bpf/bpf-next/c/7f8fa9d370c1

You are awesome, thank you!
-- 
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html



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

end of thread, other threads:[~2025-08-15 23:59 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-08-14 18:01 [PATCH v3 0/2] libbpf: fix reuse of DEVMAP Yureka Lilian
2025-08-14 18:01 ` [PATCH v3 1/2] " Yureka Lilian
2025-08-14 18:01 ` [PATCH v3 2/2] selftests/bpf: add test for DEVMAP reuse Yureka Lilian
2025-08-14 18:08   ` Eduard Zingerman
2025-08-16  0:00 ` [PATCH v3 0/2] libbpf: fix reuse of DEVMAP patchwork-bot+netdevbpf

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).