mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Yureka Lilian <yuka@yuka.dev>
To: Andrii Nakryiko <andrii@kernel.org>,
	Eduard Zingerman <eddyz87@gmail.com>,
	Alexei Starovoitov <ast@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	Martin KaFai Lau <martin.lau@linux.dev>,
	Song Liu <song@kernel.org>,
	Yonghong Song <yonghong.song@linux.dev>,
	John Fastabend <john.fastabend@gmail.com>,
	KP Singh <kpsingh@kernel.org>,
	Stanislav Fomichev <sdf@fomichev.me>, Hao Luo <haoluo@google.com>,
	Jiri Olsa <jolsa@kernel.org>
Cc: Yureka Lilian <yuka@yuka.dev>,
	bpf@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH v2 2/2] bpf: add test for DEVMAP reuse
Date: Wed, 13 Aug 2025 22:09:11 +0200	[thread overview]
Message-ID: <20250813200912.3523279-3-yuka@yuka.dev> (raw)
In-Reply-To: <20250813200912.3523279-1-yuka@yuka.dev>

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     | 68 +++++++++++++++++++
 .../selftests/bpf/progs/test_pinning_devmap.c | 20 ++++++
 2 files changed, 88 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..06befb03b
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/pinning_devmap_reuse.c
@@ -0,0 +1,68 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include <test_progs.h>
+
+void test_pinning_devmap_reuse(void)
+{
+	const char *pinpath1 = "/sys/fs/bpf/pinmap1";
+	const char *pinpath2 = "/sys/fs/bpf/pinmap2";
+	const char *file = "./test_pinning_devmap.bpf.o";
+	struct bpf_object *obj1 = NULL, *obj2 = NULL;
+	int err;
+	__u32 duration = 0;
+	DECLARE_LIBBPF_OPTS(bpf_object_open_opts, opts);
+
+	/* load the object a first time */
+	obj1 = bpf_object__open_file(file, NULL);
+	err = libbpf_get_error(obj1);
+	if (CHECK(err, "first open", "err %d\n", err)) {
+		obj1 = NULL;
+		goto out;
+	}
+	err = bpf_object__load(obj1);
+	if (CHECK(err, "first load", "err %d\n", err))
+		goto out;
+
+	/* load the object a second time, re-using the pinned map */
+	obj2 = bpf_object__open_file(file, NULL);
+	if (CHECK(err, "second open", "err %d\n", err)) {
+		obj2 = NULL;
+		goto out;
+	}
+	err = bpf_object__load(obj2);
+	if (CHECK(err, "second load", "err %d\n", err))
+		goto out;
+
+	/* we can close the reference safely without
+	 * the map's refcount falling to 0
+	 */
+	bpf_object__close(obj1);
+	obj1 = NULL;
+
+	/* now, swap the pins */
+	err = renameat2(0, pinpath1, 0, pinpath2, RENAME_EXCHANGE);
+	if (CHECK(err, "swap pins", "err %d\n", err))
+		goto out;
+
+	/* load the object again, this time the re-use should fail */
+	obj1 = bpf_object__open_file(file, NULL);
+	err = libbpf_get_error(obj1);
+	if (CHECK(err, "third open", "err %d\n", err)) {
+		obj1 = NULL;
+		goto out;
+	}
+	err = bpf_object__load(obj1);
+	if (CHECK(err != -EINVAL, "param mismatch load", "err %d\n", err))
+		goto out;
+
+out:
+	unlink(pinpath1);
+	unlink(pinpath2);
+	if (obj1)
+		bpf_object__close(obj1);
+	if (obj2)
+		bpf_object__close(obj2);
+}
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


  parent reply	other threads:[~2025-08-13 20:09 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-08-13 20:09 [PATCH v2 0/2] bpf: fix reuse of DEVMAP Yureka Lilian
2025-08-13 20:09 ` [PATCH v2 1/2] " Yureka Lilian
2025-08-13 21:59   ` Eduard Zingerman
2025-08-13 22:49   ` Andrii Nakryiko
2025-08-13 20:09 ` Yureka Lilian [this message]
2025-08-13 21:54   ` [PATCH v2 2/2] bpf: add test for DEVMAP reuse Eduard Zingerman
2025-08-13 22:01 ` [PATCH v2 0/2] bpf: fix reuse of DEVMAP Eduard Zingerman

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250813200912.3523279-3-yuka@yuka.dev \
    --to=yuka@yuka.dev \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=eddyz87@gmail.com \
    --cc=haoluo@google.com \
    --cc=john.fastabend@gmail.com \
    --cc=jolsa@kernel.org \
    --cc=kpsingh@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=martin.lau@linux.dev \
    --cc=sdf@fomichev.me \
    --cc=song@kernel.org \
    --cc=yonghong.song@linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®