BPF List
 help / color / mirror / Atom feed
From: Maxim Khmelevskii <max@linux.ibm.com>
To: Alexei Starovoitov <ast@kernel.org>,
	Andrii Nakryiko <andrii@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>
Cc: bpf@vger.kernel.org, Ilya Leoshkevich <iii@linux.ibm.com>,
	Heiko Carstens <hca@linux.ibm.com>,
	Vasily Gorbik <gor@linux.ibm.com>,
	Alexander Gordeev <agordeev@linux.ibm.com>
Subject: [PATCH bpf-next v2] selftests/bpf: Fix flaky refcount asserts in map_kptr_race
Date: Fri,  4 Dec 2026 16:31:30 +0100	[thread overview]
Message-ID: <20261204153138.31801-2-max@linux.ibm.com> (raw)

bpf_testmod shares objects across tests; prog_test_struct that holds
cnt.refs.counter is one of them. Refcounter release is deferred behind
an RCU grace period, so it may still be pending when this test asserts.
Add a new test prog which attaches to bpf_kfunc_call_test_release and
checks the reference counter. If the ref counter is 1, it indicates
that all references were released. Use the new prog to wait until the
references are released.

Fixed tests:
 - test_htab_leak
 - test_percpu_htab_leak
 - test_sk_ls_leak

Reported-by: Ilya Leoshkevich <iii@linux.ibm.com>
Reviewed-by: Ilya Leoshkevich <iii@linux.ibm.com>
Signed-off-by: Maxim Khmelevskii <max@linux.ibm.com>
---
 .../selftests/bpf/prog_tests/map_kptr_race.c  | 28 +++++++++++++++++++
 .../selftests/bpf/progs/map_kptr_race.c       |  9 ++++++
 2 files changed, 37 insertions(+)

diff --git a/tools/testing/selftests/bpf/prog_tests/map_kptr_race.c b/tools/testing/selftests/bpf/prog_tests/map_kptr_race.c
index 506ed55e8528..e6dd71c80a73 100644
--- a/tools/testing/selftests/bpf/prog_tests/map_kptr_race.c
+++ b/tools/testing/selftests/bpf/prog_tests/map_kptr_race.c
@@ -28,6 +28,22 @@ static int read_refs(struct map_kptr_race *skel)
 	return skel->bss->num_of_refs;
 }
 
+static void wait_for_refs(struct map_kptr_race *skel)
+{
+	__u64 timeout = 5ULL * 1000 * 1000 * 1000;
+	__u64 start = get_time_ns();
+
+	/* At least one bpf_kfunc_call_test_release call is needed after
+	 * kfunc_test_release is attached, otherwise refs_settled
+	 * might never be set.
+	 */
+	read_refs(skel);
+	while (!READ_ONCE(skel->bss->refs_settled) &&
+	       get_time_ns() - start < timeout) {
+		usleep(1000);
+	}
+}
+
 static void test_htab_leak(void)
 {
 	LIBBPF_OPTS(bpf_test_run_opts, opts,
@@ -63,6 +79,9 @@ static void test_htab_leak(void)
 	watcher->links.htab_map_free = bpf_program__attach(watcher->progs.htab_map_free);
 	if (!ASSERT_OK_PTR(watcher->links.htab_map_free, "attach fexit"))
 		goto out_watcher;
+	watcher->links.kfunc_test_release = bpf_program__attach(watcher->progs.kfunc_test_release);
+	if (!ASSERT_OK_PTR(watcher->links.kfunc_test_release, "attach kfunc_test_release"))
+		goto out_watcher;
 
 	map_kptr_race__destroy(skel);
 	skel = NULL;
@@ -73,6 +92,7 @@ static void test_htab_leak(void)
 		sched_yield();
 
 	ASSERT_EQ(watcher->bss->map_freed, 1, "map_freed");
+	wait_for_refs(watcher);
 	ASSERT_EQ(read_refs(watcher), 2, "htab refcount");
 
 out_watcher:
@@ -124,6 +144,9 @@ static void test_percpu_htab_leak(void)
 	watcher->links.htab_map_free = bpf_program__attach(watcher->progs.htab_map_free);
 	if (!ASSERT_OK_PTR(watcher->links.htab_map_free, "attach fexit"))
 		goto out_watcher;
+	watcher->links.kfunc_test_release = bpf_program__attach(watcher->progs.kfunc_test_release);
+	if (!ASSERT_OK_PTR(watcher->links.kfunc_test_release, "attach kfunc_test_release"))
+		goto out_watcher;
 
 	map_kptr_race__destroy(skel);
 	skel = NULL;
@@ -134,6 +157,7 @@ static void test_percpu_htab_leak(void)
 		sched_yield();
 
 	ASSERT_EQ(watcher->bss->map_freed, 1, "map_freed");
+	wait_for_refs(watcher);
 	ASSERT_EQ(read_refs(watcher), 2, "percpu_htab refcount");
 
 out_watcher:
@@ -185,6 +209,9 @@ static void test_sk_ls_leak(void)
 	watcher->links.sk_map_free = bpf_program__attach(watcher->progs.sk_map_free);
 	if (!ASSERT_OK_PTR(watcher->links.sk_map_free, "attach fexit"))
 		goto out_watcher;
+	watcher->links.kfunc_test_release = bpf_program__attach(watcher->progs.kfunc_test_release);
+	if (!ASSERT_OK_PTR(watcher->links.kfunc_test_release, "attach kfunc_test_release"))
+		goto out_watcher;
 
 	map_kptr_race__destroy(skel);
 	skel = NULL;
@@ -195,6 +222,7 @@ static void test_sk_ls_leak(void)
 		sched_yield();
 
 	ASSERT_EQ(watcher->bss->map_freed, 1, "map_freed");
+	wait_for_refs(watcher);
 	ASSERT_EQ(read_refs(watcher), 2, "sk_ls refcount");
 
 out_watcher:
diff --git a/tools/testing/selftests/bpf/progs/map_kptr_race.c b/tools/testing/selftests/bpf/progs/map_kptr_race.c
index f6f136cd8f60..fafe4dc88b39 100644
--- a/tools/testing/selftests/bpf/progs/map_kptr_race.c
+++ b/tools/testing/selftests/bpf/progs/map_kptr_race.c
@@ -37,6 +37,7 @@ int sk_ls_leak_done;
 int target_map_id;
 int map_freed;
 const volatile int nr_cpus;
+int refs_settled;
 
 SEC("tc")
 int test_htab_leak(struct __sk_buff *skb)
@@ -178,6 +179,14 @@ int BPF_PROG(sk_map_free, struct bpf_map *map)
 	return 0;
 }
 
+SEC("fexit/bpf_kfunc_call_test_release")
+int BPF_PROG(kfunc_test_release, struct prog_test_ref_kfunc *p)
+{
+	if (p->cnt.refs.counter == 1)
+		refs_settled = 1;
+	return 0;
+}
+
 SEC("syscall")
 int count_ref(void *ctx)
 {
-- 
2.56.1


             reply	other threads:[~2026-12-04 15:32 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-04 15:31 Maxim Khmelevskii [this message]
2026-12-04 15:50 ` [PATCH bpf-next v2] selftests/bpf: Fix flaky refcount asserts in map_kptr_race sashiko-bot
2026-12-04 18:31 ` Alexei Starovoitov

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=20261204153138.31801-2-max@linux.ibm.com \
    --to=max@linux.ibm.com \
    --cc=agordeev@linux.ibm.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=gor@linux.ibm.com \
    --cc=hca@linux.ibm.com \
    --cc=iii@linux.ibm.com \
    /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