BPF List
 help / color / mirror / Atom feed
* [PATCH bpf-next v2] selftests/bpf: Fix flaky refcount asserts in map_kptr_race
@ 2026-12-04 15:31 Maxim Khmelevskii
  2026-12-04 15:50 ` sashiko-bot
  2026-12-04 18:31 ` Alexei Starovoitov
  0 siblings, 2 replies; 3+ messages in thread
From: Maxim Khmelevskii @ 2026-12-04 15:31 UTC (permalink / raw)
  To: Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann
  Cc: bpf, Ilya Leoshkevich, Heiko Carstens, Vasily Gorbik,
	Alexander Gordeev

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


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

* Re: [PATCH bpf-next v2] selftests/bpf: Fix flaky refcount asserts in map_kptr_race
  2026-12-04 15:31 [PATCH bpf-next v2] selftests/bpf: Fix flaky refcount asserts in map_kptr_race Maxim Khmelevskii
@ 2026-12-04 15:50 ` sashiko-bot
  2026-12-04 18:31 ` Alexei Starovoitov
  1 sibling, 0 replies; 3+ messages in thread
From: sashiko-bot @ 2026-12-04 15:50 UTC (permalink / raw)
  To: Maxim Khmelevskii; +Cc: bpf

Thank you for your contribution! Sashiko AI review found 5 potential
issues to consider:
- [Low] bpf: non-conforming multi-line comment style in wait_for_refs()
- [Low] bpf: missing Fixes trailer referencing commit 2939d7b3b0e5
- [Low] bpf: unnecessary fexit program in wait_for_refs() vs polling
  read_refs()
- [Low] bpf: ignored read_refs() error return stalls wait_for_refs() for
  5 seconds
- [Low] bpf: lost wakeup in wait_for_refs() from fexit recursion
  suppression
--

commit 671d94475461ae9179f227fbd592ac3b5f77db64
Author: Maxim Khmelevskii <max@linux.ibm.com>

> selftests/bpf: Fix flaky refcount asserts in map_kptr_race
>
> 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>

[Severity: Low]
This fixes flaky refcount assertions in map_kptr_race.  Should it include:

  Fixes: 2939d7b3b0e5 ("selftests/bpf: Add tests for special fields races")

> 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 506ed55e85289..e6dd71c80a739 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.
> +	 */

[Severity: Low]
This isn't a bug, but BPF code uses the modern kernel multi-line comment
style with the opening /* on its own line.

> +	read_refs(skel);
> +	while (!READ_ONCE(skel->bss->refs_settled) &&
> +	       get_time_ns() - start < timeout) {
> +		usleep(1000);
> +	}
> +}

[Severity: Low]
Should wait_for_refs() return early if read_refs(skel) fails?

read_refs() returns -1 when bpf_prog_test_run_opts() fails or count_ref()
returns non-zero, without running bpf_kfunc_call_test_release().  Ignoring
that return value lets the loop sleep for the full 5-second timeout before
returning to the caller.

[ ... ]

> diff --git a/tools/testing/selftests/bpf/progs/map_kptr_race.c b/tools/testing/selftests/bpf/progs/map_kptr_race.c
> index f6f136cd8f609..fafe4dc88b395 100644
> --- a/tools/testing/selftests/bpf/progs/map_kptr_race.c
> +++ b/tools/testing/selftests/bpf/progs/map_kptr_race.c

[ ... ]

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

[Severity: Low]
Can wait_for_refs() miss the final 2 -> 1 transition and stall for the
full 5-second timeout if an RCU softirq nests inside kfunc_test_release()?

read_refs() invokes count_ref() via bpf_prog_run_pin_on_cpu(), which
leaves softirqs enabled.  If one deferred release is still pending when
kfunc_test_release() runs, a nested RCU softirq on the same CPU can drop
the last reference while kfunc_test_release() is still on current->bpf_ctx:

read_refs() -> count_ref()
  bpf_kfunc_call_test_release()      (refs: 3 -> 2)
  __bpf_prog_enter_recur()           (sets current->bpf_ctx)
  kfunc_test_release()               (reads refs == 2)
  <softirq>
    ... -> bpf_obj_free_fields()
      bpf_kfunc_call_test_release()  (refs: 2 -> 1)
      __bpf_prog_enter_recur()       <-- skipped, prog already in bpf_ctx
  bpf_prog_exit_run_ctx()            (refs_settled remains 0)

Because wait_for_refs() calls read_refs() only once before the loop,
refs_settled stays 0 until the 5-second timeout expires.

[Severity: Low]
Is the kfunc_test_release fexit program needed here instead of polling
read_refs(skel) directly in wait_for_refs()?

Because test_sk_ls_leak() calls map_kptr_race__attach(skel), adding an
auto-attachable fexit program also attaches it to skel in addition to the
three manual attaches on watcher.  Polling read_refs(skel) in the existing
timeout loop until it returns 2 or < 0 (similar to wait_for_map_release()
in tools/testing/selftests/bpf/prog_tests/map_kptr.c) would avoid the
extra fexit program, refs_settled flag, and attachment boilerplate, and
also avoids the lost-notification race above.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261204153138.31801-2-max@linux.ibm.com?part=1

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

* Re: [PATCH bpf-next v2] selftests/bpf: Fix flaky refcount asserts in map_kptr_race
  2026-12-04 15:31 [PATCH bpf-next v2] selftests/bpf: Fix flaky refcount asserts in map_kptr_race Maxim Khmelevskii
  2026-12-04 15:50 ` sashiko-bot
@ 2026-12-04 18:31 ` Alexei Starovoitov
  1 sibling, 0 replies; 3+ messages in thread
From: Alexei Starovoitov @ 2026-12-04 18:31 UTC (permalink / raw)
  To: Maxim Khmelevskii, Andrii Nakryiko, Daniel Borkmann
  Cc: bpf, Ilya Leoshkevich, Heiko Carstens, Vasily Gorbik,
	Alexander Gordeev

On Fri, Dec 04, 2026 at 07:31 AM Maxim Khmelevskii <max@linux.ibm.com> wrote:
> 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.

It should be fixed already. See
commit 2ad395a38471 ("selftests/bpf: Wait for the last maps of map_kptr
to be freed")

pw-bot: cr

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

end of thread, other threads:[~2026-12-04 18:31 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-04 15:31 [PATCH bpf-next v2] selftests/bpf: Fix flaky refcount asserts in map_kptr_race Maxim Khmelevskii
2026-12-04 15:50 ` sashiko-bot
2026-12-04 18:31 ` Alexei Starovoitov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox