Skip to contentSkip to conversation

selftests/bpf: Fix flaky refcount asserts in map_kptr_race

PATCH In review Maxim Khmelevskii 3 participants 8 messages Jul 23 – Oct 21

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 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. wait_for_refs prevents flaky failures when other tests have contaminated the refcount.

Fixed tests:
- test_htab_leak
- test_percpu_htab_leak
- test_sk_ls_leak

---
 .../testing/selftests/bpf/prog_tests/map_kptr_race.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
tools/testing/selftests/bpf/prog_tests/map_kptr_race.c +12 -0
index 506ed55e8528..5a543b3465a8 100644
@@ -28,6 +28,15 @@ 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)
+{
+	for (int i = 0; i < 500; i++) {
+		if (read_refs(skel) == 2)
+			return;
+		usleep(10 * 1000);
+	}
+}
+
 static void test_htab_leak(void)
 {
 	LIBBPF_OPTS(bpf_test_run_opts, opts,
@@ -73,6 +82,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:
@@ -134,6 +144,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:
@@ -195,6 +206,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:
Signature · 1 line
2.54.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] The polling loop in `wait_for_refs` continues retrying after a fatal error in `read_refs`…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] The polling loop in wait_for_refs continues retrying after a fatal error in read_refs, causing unnecessary delays and test log spam.
- [Medium] A Time-Of-Check to Time-Of-Use (TOCTOU) race exists between wait_for_refs() and the subsequent ASSERT_EQ(), which can still cause flaky test failures. --

--- Patch [1]: [PATCH bpf-next] selftests/bpf: Fix flaky refcount asserts in map_kptr_race --- commit a9dbb8ca45d040e8f920633939d7db5945ecaca2
Author: Maxim Khmelevskii <[email protected]>

selftests/bpf: Fix flaky refcount asserts in map_kptr_race

bpf_testmod shares objects across tests, including the prog_test_struct. Refcounter release is deferred behind an RCU grace period, so it may still be pending. This patch adds wait_for_refs to prevent flaky failures when other tests have contaminated the refcount.

Quoted 7 lines
tools/testing/selftests/bpf/prog_tests/map_kptr_race.c +0 -0
index 506ed55e85289..5a543b3465a80 100644
@@ -28,6 +28,15 @@ static int read_refs(struct map_kptr_race *skel)
 	return skel->bss->num_of_refs;
 }

tools/testing/selftests/bpf/prog_tests/map_kptr_race.c static int read_refs(struct map_kptr_race *skel) · line 39

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/map_kptr_race.c +9 -0
 
+static void wait_for_refs(struct map_kptr_race *skel)
+{
+	for (int i = 0; i < 500; i++) {
+		if (read_refs(skel) == 2)
+			return;
+		usleep(10 * 1000);
+	}
+}
+

[Severity: Medium]
Since read_refs() uses ASSERT_OK() which logs an error message on failure and returns -1, does this loop introduce a regression by failing to handle fatal errors correctly?

If read_refs() returns a negative error code (for example, if bpf_prog_test_run_opts fails), it appears this loop will ignore it and retry 500 times.

This would result in 500 redundant assertion failures in the test output and an unnecessary 5-second stall before the test finally fails.

Could we add a check to break early if read_refs() returns a value < 0?

[ ... ]

tools/testing/selftests/bpf/prog_tests/map_kptr_race.c static void test_htab_leak(void) · line 87

Quoted 7 lines
tools/testing/selftests/bpf/prog_tests/map_kptr_race.c +1 -0
@@ -73,6 +82,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");
 

[Severity: Medium]
Is there a potential time-of-check to time-of-use regression here that could still result in flaky tests?

wait_for_refs() waits for the condition and returns, but then the shared state is read again via ASSERT_EQ(read_refs(watcher), 2).

Because tests run in parallel and share the bpf_testmod kernel module, another test could increment the refcount in the unprotected window between wait_for_refs() returning and read_refs() executing inside the assertion. This same pattern also appears in test_percpu_htab_leak() and test_sk_ls_leak().

Could wait_for_refs() return the final refcount value or boolean success status so the double-read can be avoided?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#2 lore raw
In reply to Maxim Khmelevskii
Quoted 11 lines · Maxim Khmelevskii

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. wait_for_refs prevents flaky failures when other tests have contaminated the refcount.

Fixed tests:
- test_htab_leak
- test_percpu_htab_leak
- test_sk_ls_leak

Quoted 4 lines

---

Wouldn't this still be flaky? Could we order these tests and wait for a RCU gp to pass before finishing each one? We have kern_sync_rcu(). Either way, I don't think the current approach is the right way forward.

pw-bot: cr

Quoted 47 lines
 .../testing/selftests/bpf/prog_tests/map_kptr_race.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
tools/testing/selftests/bpf/prog_tests/map_kptr_race.c +12 -0
index 506ed55e8528..5a543b3465a8 100644
@@ -28,6 +28,15 @@ 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)
+{
+	for (int i = 0; i < 500; i++) {
+		if (read_refs(skel) == 2)
+			return;
+		usleep(10 * 1000);
+	}
+}
+
 static void test_htab_leak(void)
 {
 	LIBBPF_OPTS(bpf_test_run_opts, opts,
@@ -73,6 +82,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:
@@ -134,6 +144,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:
@@ -195,6 +206,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:
#3 lore raw
Quoted 64 lines · Kumar Kartikeya Dwivedi
Quoted 11 lines · Maxim Khmelevskii

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. wait_for_refs prevents flaky failures when other tests have contaminated the refcount.

Fixed tests:
- test_htab_leak
- test_percpu_htab_leak
- test_sk_ls_leak

Quoted 4 lines

---

Wouldn't this still be flaky? Could we order these tests and wait for a RCU gp to pass before finishing each one? We have kern_sync_rcu(). Either way, I don't think the current approach is the right way forward.

pw-bot: cr

Quoted 42 lines
  .../testing/selftests/bpf/prog_tests/map_kptr_race.c | 12 ++++++++++++
  1 file changed, 12 insertions(+)
tools/testing/selftests/bpf/prog_tests/map_kptr_race.c +11 -0
index 506ed55e8528..5a543b3465a8 100644
@@ -28,6 +28,15 @@ 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)
+{
+	for (int i = 0; i < 500; i++) {
+		if (read_refs(skel) == 2)
+			return;
+		usleep(10 * 1000);
+	}
+}
+
  static void test_htab_leak(void)
  {
  	LIBBPF_OPTS(bpf_test_run_opts, opts,
@@ -73,6 +82,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:
@@ -134,6 +144,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:
@@ -195,6 +206,7 @@ static void test_sk_ls_leak(void)
  		sched_yield();
 
Quoted 7 lines

tools/testing/selftests/bpf/prog_tests/map_kptr_race.c static void test_sk_ls_leak(void) · line 212

Quoted 5 lines
tools/testing/selftests/bpf/prog_tests/map_kptr_race.c +1 -0
  	ASSERT_EQ(watcher->bss->map_freed, 1, "map_freed");
+	wait_for_refs(watcher);
  	ASSERT_EQ(read_refs(watcher), 2, "sk_ls refcount");
 
  out_watcher:

Wouldn't this still be flaky?

Yes, but it seems like the best solution to me.

Quoted 1 line

Could we order these tests and wait for a RCU gp

to pass before finishing each one?

I don't think so, because an RCU gp does not guarantee that htab dtor was called.

Let me try to explain these points and please correct me if they are wrong.

That is how htab frees memory:

const struct bpf_map_ops htab_map_ops = {
...
    .map_free = htab_map_free,
...

static void htab_map_free(struct bpf_map *map) |-> void bpf_mem_alloc_destroy(struct bpf_mem_alloc *ma) wait for bpf_mem_refill

static void bpf_mem_refill(struct irq_work *work)
|-> static void free_bulk(struct bpf_mem_cache *c)
    |-> static void do_call_rcu_ttrace(struct bpf_mem_cache *c)
        |-> static inline void call_rcu_tasks_trace(struct rcu_head *rhp, rcu_callback_t func) register callback __free_rcu

static void __free_rcu(struct rcu_head *head)
|-> static int free_all(struct bpf_mem_cache *c, struct llist_node *llnode, bool percpu) here the htab destructor is called

If this is correct, htab free happens after RCU gp and task RCU gp, which makes the option with waiting for the RCU gp after each test pointless. What kind of guarantees kern_sync_rcu gives right now?

Polling on refs seems better because it improves chances for the test to pass without introducing major changes and it works with any RCU flavor, also the same hack is used already in bpf_testmod_exit.

Considering all of this, what do you think is the way forward?

#4 lore raw
Quoted 104 lines · Maxim Khmelevskii
Quoted 64 lines · Kumar Kartikeya Dwivedi
Quoted 11 lines · Maxim Khmelevskii

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. wait_for_refs prevents flaky failures when other tests have contaminated the refcount.

Fixed tests:
  - test_htab_leak
  - test_percpu_htab_leak
  - test_sk_ls_leak

Quoted 4 lines

---

Wouldn't this still be flaky? Could we order these tests and wait for a RCU gp to pass before finishing each one? We have kern_sync_rcu(). Either way, I don't think the current approach is the right way forward.

pw-bot: cr

Quoted 42 lines

.../testing/selftests/bpf/prog_tests/map_kptr_race.c | 12 ++++++++++++

  1 file changed, 12 insertions(+)
tools/testing/selftests/bpf/prog_tests/map_kptr_race.c +10 -0
index 506ed55e8528..5a543b3465a8 100644
@@ -28,6 +28,15 @@ 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)
+{
+    for (int i = 0; i < 500; i++) {
+        if (read_refs(skel) == 2)
+            return;
+        usleep(10 * 1000);
+    }
+}
+
   static void test_htab_leak(void)
   {
       LIBBPF_OPTS(bpf_test_run_opts, opts,
@@ -73,6 +82,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:

tools/testing/selftests/bpf/prog_tests/map_kptr_race.c +1 -0
@@ -134,6 +144,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:

tools/testing/selftests/bpf/prog_tests/map_kptr_race.c +0 -0
@@ -195,6 +206,7 @@ static void test_sk_ls_leak(void)
           sched_yield();
 
Quoted 7 lines
Quoted 5 lines
tools/testing/selftests/bpf/prog_tests/map_kptr_race.c +1 -0
       ASSERT_EQ(watcher->bss->map_freed, 1, "map_freed");
+    wait_for_refs(watcher);
       ASSERT_EQ(read_refs(watcher), 2, "sk_ls refcount");
 

  out_watcher:

Wouldn't this still be flaky?

Yes, but it seems like the best solution to me.

Quoted 1 line

Could we order these tests and wait for a RCU gp

to pass before finishing each one?

I don't think so, because an RCU gp does not guarantee that htab dtor was called.

Let me try to explain these points and please correct me if they are wrong.

That is how htab frees memory:

const struct bpf_map_ops htab_map_ops = {
...
    .map_free = htab_map_free,
...

static void htab_map_free(struct bpf_map *map) |-> void bpf_mem_alloc_destroy(struct bpf_mem_alloc *ma) wait for bpf_mem_refill

static void bpf_mem_refill(struct irq_work *work)
|-> static void free_bulk(struct bpf_mem_cache *c)
    |-> static void do_call_rcu_ttrace(struct bpf_mem_cache *c)
        |-> static inline void call_rcu_tasks_trace(struct rcu_head *rhp, rcu_callback_t func) register callback __free_rcu

static void __free_rcu(struct rcu_head *head)
|-> static int free_all(struct bpf_mem_cache *c, struct llist_node *llnode, bool percpu) here the htab destructor is called

Quoted 6 lines

If this is correct, htab free happens after RCU gp and task RCU gp, which makes the option with waiting for the RCU gp after each test pointless. What kind of guarantees kern_sync_rcu gives right now?

Polling on refs seems better because it improves chances for the test to pass without introducing major changes and it works with any RCU flavor, also the same hack is used already in bpf_testmod_exit.

Considering all of this, what do you think is the way forward?

Hi, I know the solution isn't perfect and the issue most likely is low priority, but still would be nice to improve the test. Please hit me up whenever you have any comments regarding it.

#5 lore raw
Quoted 122 lines · Maxim Khmelevskii
Quoted 120 lines · Maxim Khmelevskii
Quoted 68 lines · Kumar Kartikeya Dwivedi
Quoted 12 lines · Maxim Khmelevskii

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. wait_for_refs prevents flaky failures when other tests have contaminated
the refcount.

Fixed tests:
- test_htab_leak
- test_percpu_htab_leak
- test_sk_ls_leak

Quoted 4 lines

---

Wouldn't this still be flaky? Could we order these tests and wait for a RCU gp
to pass before finishing each one? We have kern_sync_rcu(). Either way, I don't
think the current approach is the right way forward.

pw-bot: cr

Quoted 43 lines

.../testing/selftests/bpf/prog_tests/map_kptr_race.c | 12 ++++++++++++

  1 file changed, 12 insertions(+)
tools/testing/selftests/bpf/prog_tests/map_kptr_race.c +11 -0
index 506ed55e8528..5a543b3465a8 100644
@@ -28,6 +28,15 @@ 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)
+{
+    for (int i = 0; i < 500; i++) {
+        if (read_refs(skel) == 2)
+            return;
+        usleep(10 * 1000);
+    }
+}
+
  static void test_htab_leak(void)
  {
      LIBBPF_OPTS(bpf_test_run_opts, opts,
@@ -73,6 +82,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:
@@ -134,6 +144,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:
@@ -195,6 +206,7 @@ static void test_sk_ls_leak(void)
          sched_yield();
 
Quoted 7 lines

tools/testing/selftests/bpf/prog_tests/map_kptr_race.c static void test_sk_ls_leak(void) · line 212

Quoted 5 lines
tools/testing/selftests/bpf/prog_tests/map_kptr_race.c +1 -0
      ASSERT_EQ(watcher->bss->map_freed, 1, "map_freed");
+    wait_for_refs(watcher);
      ASSERT_EQ(read_refs(watcher), 2, "sk_ls refcount");
 
  out_watcher:

Wouldn't this still be flaky?

Yes, but it seems like the best solution to me.

Quoted 1 line

Could we order these tests and wait for a RCU gp

to pass before finishing each one?

I don't think so, because an RCU gp does not guarantee that htab dtor was called.

Let me try to explain these points and please correct me if they are wrong.

That is how htab frees memory:

const struct bpf_map_ops htab_map_ops = {
...
.map_free = htab_map_free,
...

static void htab_map_free(struct bpf_map *map) |-> void bpf_mem_alloc_destroy(struct bpf_mem_alloc *ma)
wait for bpf_mem_refill

static void bpf_mem_refill(struct irq_work *work) |-> static void free_bulk(struct bpf_mem_cache *c)

    |-> static void do_call_rcu_ttrace(struct bpf_mem_cache *c)
        |-> static inline void call_rcu_tasks_trace(struct rcu_head

*rhp, rcu_callback_t func)
register callback __free_rcu

static void __free_rcu(struct rcu_head *head)
|-> static int free_all(struct bpf_mem_cache *c, struct llist_node *llnode, bool percpu)
here the htab destructor is called

If this is correct, htab free happens after RCU gp and task RCU gp, which makes the option with waiting for the RCU gp after each test pointless. What kind of guarantees kern_sync_rcu gives right now?

Polling on refs seems better because it improves chances for the test to pass without introducing major changes and it works with any RCU flavor, also the same hack is used already in bpf_testmod_exit.

Quoted 6 lines
Quoted 2 lines

Considering all of this, what do you think is the way forward?

Hi, I know the solution isn't perfect and the issue most likely is low priority, but still would be nice to improve the test. Please hit me up whenever you have any comments regarding it.

I still feel this is not the right solution. If RCU gp isn't enough, trace when the dtor is called and set a bit that user space can wait upon, and we can spin with yields until that bit is set. If other tests modifying the refcount is problematic to wait properly then we can mark this one as serial, but I think that can be avoided.

#6 lore raw
Quoted 126 lines · Kumar Kartikeya Dwivedi
Quoted 119 lines · Maxim Khmelevskii
Quoted 118 lines · Maxim Khmelevskii
Quoted 68 lines · Kumar Kartikeya Dwivedi
Quoted 12 lines · Maxim Khmelevskii

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. wait_for_refs prevents flaky failures when other tests have contaminated
the refcount.

Fixed tests:
- test_htab_leak
- test_percpu_htab_leak
- test_sk_ls_leak

Quoted 4 lines

---

Wouldn't this still be flaky? Could we order these tests and wait for a RCU gp
to pass before finishing each one? We have kern_sync_rcu(). Either way, I don't
think the current approach is the right way forward.

pw-bot: cr

Quoted 43 lines

.../testing/selftests/bpf/prog_tests/map_kptr_race.c | 12 ++++++++++++

   1 file changed, 12 insertions(+)
tools/testing/selftests/bpf/prog_tests/map_kptr_race.c +11 -0
index 506ed55e8528..5a543b3465a8 100644
@@ -28,6 +28,15 @@ 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)
+{
+    for (int i = 0; i < 500; i++) {
+        if (read_refs(skel) == 2)
+            return;
+        usleep(10 * 1000);
+    }
+}
+
   static void test_htab_leak(void)
   {
       LIBBPF_OPTS(bpf_test_run_opts, opts,
@@ -73,6 +82,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:
@@ -134,6 +144,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:
@@ -195,6 +206,7 @@ static void test_sk_ls_leak(void)
           sched_yield();
 
Quoted 6 lines

tools/testing/selftests/bpf/prog_tests/map_kptr_race.c static void test_sk_ls_leak(void) · line 212

Quoted 5 lines
tools/testing/selftests/bpf/prog_tests/map_kptr_race.c +1 -0
       ASSERT_EQ(watcher->bss->map_freed, 1, "map_freed");
+    wait_for_refs(watcher);
       ASSERT_EQ(read_refs(watcher), 2, "sk_ls refcount");
 
   out_watcher:

Wouldn't this still be flaky?

Yes, but it seems like the best solution to me.

Quoted 1 line

Could we order these tests and wait for a RCU gp

to pass before finishing each one?

I don't think so, because an RCU gp does not guarantee that htab dtor was called.

Let me try to explain these points and please correct me if they are wrong.

That is how htab frees memory:

const struct bpf_map_ops htab_map_ops = {
...
.map_free = htab_map_free,
...

static void htab_map_free(struct bpf_map *map) |-> void bpf_mem_alloc_destroy(struct bpf_mem_alloc *ma)
wait for bpf_mem_refill

static void bpf_mem_refill(struct irq_work *work) |-> static void free_bulk(struct bpf_mem_cache *c)

     |-> static void do_call_rcu_ttrace(struct bpf_mem_cache *c)
         |-> static inline void call_rcu_tasks_trace(struct rcu_head

*rhp, rcu_callback_t func)
register callback __free_rcu

static void __free_rcu(struct rcu_head *head)
|-> static int free_all(struct bpf_mem_cache *c, struct llist_node *llnode, bool percpu)
here the htab destructor is called

If this is correct, htab free happens after RCU gp and task RCU gp, which makes the option with waiting for the RCU gp after each test pointless. What kind of guarantees kern_sync_rcu gives right now?

Polling on refs seems better because it improves chances for the test to pass without introducing major changes and it works with any RCU flavor, also the same hack is used already in bpf_testmod_exit.

Quoted 6 lines
Quoted 2 lines

Considering all of this, what do you think is the way forward?

Hi, I know the solution isn't perfect and the issue most likely is low priority, but still would be nice to improve the test. Please hit me up whenever you have any comments regarding it.

Quoted 5 lines

I still feel this is not the right solution. If RCU gp isn't enough, trace when the dtor is called and set a bit that user space can wait upon, and we can spin with yields until that bit is set. If other tests modifying the refcount is problematic to wait properly then we can mark this one as serial, but I think that can be avoided.

What would you say on making a new prog to track bpf_kfunc_call_test_release ?

SEC("fexit/bpf_kfunc_call_test_release")
int BPF_PROG(test_release, struct prog_test_ref_kfunc *p) {
    /* refs_settled is a global var */
    if (p->cnt.refs.counter == 1)
        refs_settled = 1;
    return 0;
}

Then it's possible to poll on refs_settled in wait_for_refs, like this:

    while (!READ_ONCE(skel->bss->refs_settled))
        sched_yield();

Would that be a better solution? If you want, I could send the v2, so you have all the details.

#7 lore raw
Quoted 146 lines · Maxim Khmelevskii
Quoted 126 lines · Kumar Kartikeya Dwivedi
Quoted 119 lines · Maxim Khmelevskii
Quoted 118 lines · Maxim Khmelevskii
Quoted 68 lines · Kumar Kartikeya Dwivedi
Quoted 12 lines · Maxim Khmelevskii

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. wait_for_refs prevents flaky failures when other tests have contaminated
the refcount.

Fixed tests:
- test_htab_leak
- test_percpu_htab_leak
- test_sk_ls_leak

Quoted 4 lines

---

Wouldn't this still be flaky? Could we order these tests and wait for a RCU gp
to pass before finishing each one? We have kern_sync_rcu(). Either way, I don't
think the current approach is the right way forward.

pw-bot: cr

Quoted 43 lines

.../testing/selftests/bpf/prog_tests/map_kptr_race.c | 12 ++++++++++++

   1 file changed, 12 insertions(+)
tools/testing/selftests/bpf/prog_tests/map_kptr_race.c +11 -0
index 506ed55e8528..5a543b3465a8 100644
@@ -28,6 +28,15 @@ 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)
+{
+    for (int i = 0; i < 500; i++) {
+        if (read_refs(skel) == 2)
+            return;
+        usleep(10 * 1000);
+    }
+}
+
   static void test_htab_leak(void)
   {
       LIBBPF_OPTS(bpf_test_run_opts, opts,
@@ -73,6 +82,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:
@@ -134,6 +144,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:
@@ -195,6 +206,7 @@ static void test_sk_ls_leak(void)
           sched_yield();
 
Quoted 6 lines

tools/testing/selftests/bpf/prog_tests/map_kptr_race.c static void test_sk_ls_leak(void) · line 212

Quoted 5 lines
tools/testing/selftests/bpf/prog_tests/map_kptr_race.c +1 -0
       ASSERT_EQ(watcher->bss->map_freed, 1, "map_freed");
+    wait_for_refs(watcher);
       ASSERT_EQ(read_refs(watcher), 2, "sk_ls refcount");
 
   out_watcher:

Wouldn't this still be flaky?

Yes, but it seems like the best solution to me.

Quoted 1 line

Could we order these tests and wait for a RCU gp

to pass before finishing each one?

I don't think so, because an RCU gp does not guarantee that htab dtor was called.

Let me try to explain these points and please correct me if they are wrong.

That is how htab frees memory:

const struct bpf_map_ops htab_map_ops = {
...
.map_free = htab_map_free,
...

static void htab_map_free(struct bpf_map *map) |-> void bpf_mem_alloc_destroy(struct bpf_mem_alloc *ma)
wait for bpf_mem_refill

static void bpf_mem_refill(struct irq_work *work) |-> static void free_bulk(struct bpf_mem_cache *c)

     |-> static void do_call_rcu_ttrace(struct bpf_mem_cache *c)
         |-> static inline void call_rcu_tasks_trace(struct rcu_head

*rhp, rcu_callback_t func)
register callback __free_rcu

static void __free_rcu(struct rcu_head *head)
|-> static int free_all(struct bpf_mem_cache *c, struct llist_node *llnode, bool percpu)
here the htab destructor is called

If this is correct, htab free happens after RCU gp and task RCU gp, which makes the option with waiting for the RCU gp after each test pointless. What kind of guarantees kern_sync_rcu gives right now?

Polling on refs seems better because it improves chances for the test to pass without introducing major changes and it works with any RCU flavor, also the same hack is used already in bpf_testmod_exit.

Quoted 6 lines
Quoted 2 lines

Considering all of this, what do you think is the way forward?

Hi, I know the solution isn't perfect and the issue most likely is low priority, but still would be nice to improve the test. Please hit me up whenever you have any comments regarding it.

Quoted 5 lines

I still feel this is not the right solution. If RCU gp isn't enough, trace when the dtor is called and set a bit that user space can wait upon, and we can spin with yields until that bit is set. If other tests modifying the refcount is problematic to wait properly then we can mark this one as serial, but I think that can be avoided.

What would you say on making a new prog to track
bpf_kfunc_call_test_release ?

SEC("fexit/bpf_kfunc_call_test_release")
int BPF_PROG(test_release, struct prog_test_ref_kfunc *p)

{
     /* refs_settled is a global var */
     if (p->cnt.refs.counter == 1)
         refs_settled = 1;
     return 0;
}
Quoted 8 lines

Then it's possible to poll on refs_settled in wait_for_refs, like this:

     while (!READ_ONCE(skel->bss->refs_settled))
         sched_yield();

Would that be a better solution? If you want, I could send the v2, so you have all the details.

Yes, makes much more sense. Please share a v2 and I'll take a closer look.