Skip to contentSkip to conversation

selftests/bpf: fix task_local_storage/exit_creds rcu usage

PATCH Superseded Delyan Kratunov 3 participants 4 messages Oct 19, 2022 – Oct 20, 2022

Subsystems (3)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 selftests/bpf: fix task_local_storage/exit_creds rcu usage

BPF CI has revealed flakiness in the task_local_storage/exit_creds test. The failure point in CI [1] is that null_ptr_count is equal to 0, which indicates that the program hasn't run yet. This points to the kern_sync_rcu (sys_membarrier -> synchronize_rcu underneath) not waiting sufficiently.

Indeed, synchronize_rcu only waits for read-side sections that started before the call. If the program execution starts *during* the synchronize_rcu invocation (due to, say, preemption), the test won't wait long enough.

As a speculative fix, make the synchornize_rcu calls in a loop until an explicit run counter has gone up.

[1]: https://github.com/kernel-patches/bpf/actions/runs/3268263235/jobs/5374940791

---
v1 -> v2:
Explicit loop counter and MAX_SYNC_RCU_CALLS guard.

 .../bpf/prog_tests/task_local_storage.c        | 18 +++++++++++++++---
 .../bpf/progs/task_local_storage_exit_creds.c  |  3 +++
 2 files changed, 18 insertions(+), 3 deletions(-)
tools/testing/selftests/bpf/prog_tests/task_local_storage.c +15 -3
index 035c263aab1b..99a42a2b6e14 100644
@@ -39,7 +39,8 @@ static void test_sys_enter_exit(void)
 static void test_exit_creds(void)
 {
 	struct task_local_storage_exit_creds *skel;
-	int err;
+	int err, run_count, sync_rcu_calls = 0;
+	const int MAX_SYNC_RCU_CALLS = 1000;
 
 	skel = task_local_storage_exit_creds__open_and_load();
 	if (!ASSERT_OK_PTR(skel, "skel_open_and_load"))
@@ -53,8 +54,19 @@ static void test_exit_creds(void)
 	if (CHECK_FAIL(system("ls > /dev/null")))
 		goto out;
 
-	/* sync rcu to make sure exit_creds() is called for "ls" */
-	kern_sync_rcu();
+	/* kern_sync_rcu is not enough on its own as the read section we want
+	 * to wait for may start after we enter synchronize_rcu, so our call
+	 * won't wait for the section to finish. Loop on the run counter
+	 * as well to ensure the program has run.
+	 */
+	do {
+		kern_sync_rcu();
+		run_count = __atomic_load_n(&skel->bss->run_count, __ATOMIC_SEQ_CST);
+	} while (run_count == 0 && ++sync_rcu_calls < MAX_SYNC_RCU_CALLS);
+
+	ASSERT_NEQ(sync_rcu_calls, MAX_SYNC_RCU_CALLS,
+		   "sync_rcu count too high");
+	ASSERT_NEQ(run_count, 0, "run_count");
 	ASSERT_EQ(skel->bss->valid_ptr_count, 0, "valid_ptr_count");
 	ASSERT_NEQ(skel->bss->null_ptr_count, 0, "null_ptr_count");
 out:
tools/testing/selftests/bpf/progs/task_local_storage_exit_creds.c +3 -0
index 81758c0aef99..41d88ed222ff 100644
@@ -14,6 +14,7 @@ struct {
 	__type(value, __u64);
 } task_storage SEC(".maps");
 
+int run_count = 0;
 int valid_ptr_count = 0;
 int null_ptr_count = 0;
 
@@ -28,5 +29,7 @@ int BPF_PROG(trace_exit_creds, struct task_struct *task)
 		__sync_fetch_and_add(&valid_ptr_count, 1);
 	else
 		__sync_fetch_and_add(&null_ptr_count, 1);
+
+	__sync_fetch_and_add(&run_count, 1);
 	return 0;
 }
Signature · 1 line
2.37.3
Acked-by on v2 1/1
#1 lore raw
Quoted 45 lines · Delyan Kratunov

BPF CI has revealed flakiness in the task_local_storage/exit_creds test. The failure point in CI [1] is that null_ptr_count is equal to 0, which indicates that the program hasn't run yet. This points to the kern_sync_rcu (sys_membarrier -> synchronize_rcu underneath) not waiting sufficiently.

Indeed, synchronize_rcu only waits for read-side sections that started before the call. If the program execution starts *during* the synchronize_rcu invocation (due to, say, preemption), the test won't wait long enough.

As a speculative fix, make the synchornize_rcu calls in a loop until an explicit run counter has gone up.

   [1]:  

https://github.com/kernel-patches/bpf/actions/runs/3268263235/jobs/5374940791

---
v1 -> v2:
Explicit loop counter and MAX_SYNC_RCU_CALLS guard.

  .../bpf/prog_tests/task_local_storage.c        | 18 +++++++++++++++---
  .../bpf/progs/task_local_storage_exit_creds.c  |  3 +++
  2 files changed, 18 insertions(+), 3 deletions(-)
tools/testing/selftests/bpf/prog_tests/task_local_storage.c +2 -2
index 035c263aab1b..99a42a2b6e14 100644
@@ -39,7 +39,8 @@ static void test_sys_enter_exit(void)
  static void test_exit_creds(void)
  {
  	struct task_local_storage_exit_creds *skel;
-	int err;
+	int err, run_count, sync_rcu_calls = 0;
+	const int MAX_SYNC_RCU_CALLS = 1000;
 
  	skel = task_local_storage_exit_creds__open_and_load();
  	if (!ASSERT_OK_PTR(skel, "skel_open_and_load"))
@@ -53,8 +54,19 @@ static void test_exit_creds(void)
  	if (CHECK_FAIL(system("ls > /dev/null")))
  		goto out;
 
-	/* sync rcu to make sure exit_creds() is called for "ls" */

tools/testing/selftests/bpf/prog_tests/task_local_storage.c static void test_exit_creds(void) · line 65

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/task_local_storage.c +9 -1
-	kern_sync_rcu();
+	/* kern_sync_rcu is not enough on its own as the read section we want
+	 * to wait for may start after we enter synchronize_rcu, so our call
+	 * won't wait for the section to finish. Loop on the run counter
+	 * as well to ensure the program has run.
+	 */
+	do {
+		kern_sync_rcu();
+		run_count = __atomic_load_n(&skel->bss->run_count, __ATOMIC_SEQ_CST);
+	} while (run_count == 0 && ++sync_rcu_calls < MAX_SYNC_RCU_CALLS);

Might have been easier to do the following instead?

int sync_rcu_calls = 1000;
do {
} while (run_count == 0 && --sync_rcu_calls);

Quoted 29 lines
tools/testing/selftests/bpf/prog_tests/task_local_storage.c +4 -0
+
+	ASSERT_NEQ(sync_rcu_calls, MAX_SYNC_RCU_CALLS,
+		   "sync_rcu count too high");
+	ASSERT_NEQ(run_count, 0, "run_count");
  	ASSERT_EQ(skel->bss->valid_ptr_count, 0, "valid_ptr_count");
  	ASSERT_NEQ(skel->bss->null_ptr_count, 0, "null_ptr_count");
  out:
tools/testing/selftests/bpf/progs/task_local_storage_exit_creds.c +3 -1
index 81758c0aef99..41d88ed222ff 100644
@@ -14,6 +14,7 @@ struct {
  	__type(value, __u64);
  } task_storage SEC(".maps");
 
+int run_count = 0;
  int valid_ptr_count = 0;
  int null_ptr_count = 0;
 
@@ -28,5 +29,7 @@ int BPF_PROG(trace_exit_creds, struct task_struct *task)
  		__sync_fetch_and_add(&valid_ptr_count, 1);
  	else
  		__sync_fetch_and_add(&null_ptr_count, 1);
+
+	__sync_fetch_and_add(&run_count, 1);
  	return 0;
  }
--

2.37.3

#2 lore raw
Quoted 59 lines · On Wed, Oct 19, 2022 at 12:57 PM <[email protected]> wrote:
Quoted 46 lines · Delyan Kratunov

BPF CI has revealed flakiness in the task_local_storage/exit_creds test. The failure point in CI [1] is that null_ptr_count is equal to 0, which indicates that the program hasn't run yet. This points to the kern_sync_rcu (sys_membarrier -> synchronize_rcu underneath) not waiting sufficiently.

Indeed, synchronize_rcu only waits for read-side sections that started before the call. If the program execution starts *during* the synchronize_rcu invocation (due to, say, preemption), the test won't wait long enough.

As a speculative fix, make the synchornize_rcu calls in a loop until an explicit run counter has gone up.

   [1]:

https://github.com/kernel-patches/bpf/actions/runs/3268263235/jobs/5374940791

---
v1 -> v2:
Explicit loop counter and MAX_SYNC_RCU_CALLS guard.

  .../bpf/prog_tests/task_local_storage.c        | 18 +++++++++++++++---
  .../bpf/progs/task_local_storage_exit_creds.c  |  3 +++
  2 files changed, 18 insertions(+), 3 deletions(-)
tools/testing/selftests/bpf/prog_tests/task_local_storage.c +2 -2
index 035c263aab1b..99a42a2b6e14 100644
@@ -39,7 +39,8 @@ static void test_sys_enter_exit(void)
  static void test_exit_creds(void)
  {
      struct task_local_storage_exit_creds *skel;
-     int err;
+     int err, run_count, sync_rcu_calls = 0;
+     const int MAX_SYNC_RCU_CALLS = 1000;
 
      skel = task_local_storage_exit_creds__open_and_load();
      if (!ASSERT_OK_PTR(skel, "skel_open_and_load"))
@@ -53,8 +54,19 @@ static void test_exit_creds(void)
      if (CHECK_FAIL(system("ls > /dev/null")))
              goto out;
 
-     /* sync rcu to make sure exit_creds() is called for "ls" */

tools/testing/selftests/bpf/prog_tests/task_local_storage.c static void test_exit_creds(void) · line 65

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/task_local_storage.c +9 -1
-     kern_sync_rcu();
+     /* kern_sync_rcu is not enough on its own as the read section we want
+      * to wait for may start after we enter synchronize_rcu, so our call
+      * won't wait for the section to finish. Loop on the run counter
+      * as well to ensure the program has run.
+      */
+     do {
+             kern_sync_rcu();
+             run_count = __atomic_load_n(&skel->bss->run_count, __ATOMIC_SEQ_CST);
+     } while (run_count == 0 && ++sync_rcu_calls < MAX_SYNC_RCU_CALLS);
Quoted 7 lines

Might have been easier to do the following instead?

int sync_rcu_calls = 1000;
do {
} while (run_count == 0 && --sync_rcu_calls);

I think it's a preference of the author.
Both are fine. imo.

I was about to apply, but then noticed Delyan's author line and SOB are different. @meta vs @fb :(
Delyan, please fix.

Fixing SOB is not something maintainers can do while applying.

#3 lore raw
Quoted 65 lines · Alexei Starovoitov
Quoted 63 lines · On Wed, Oct 19, 2022 at 12:57 PM <[email protected]> wrote:
Quoted 46 lines · Delyan Kratunov

BPF CI has revealed flakiness in the task_local_storage/exit_creds test. The failure point in CI [1] is that null_ptr_count is equal to 0, which indicates that the program hasn't run yet. This points to the kern_sync_rcu (sys_membarrier -> synchronize_rcu underneath) not waiting sufficiently.

Indeed, synchronize_rcu only waits for read-side sections that started before the call. If the program execution starts *during* the synchronize_rcu invocation (due to, say, preemption), the test won't wait long enough.

As a speculative fix, make the synchornize_rcu calls in a loop until an explicit run counter has gone up.

   [1]:

https://github.com/kernel-patches/bpf/actions/runs/3268263235/jobs/5374940791

---
v1 -> v2:
Explicit loop counter and MAX_SYNC_RCU_CALLS guard.

  .../bpf/prog_tests/task_local_storage.c        | 18 +++++++++++++++---
  .../bpf/progs/task_local_storage_exit_creds.c  |  3 +++
  2 files changed, 18 insertions(+), 3 deletions(-)
tools/testing/selftests/bpf/prog_tests/task_local_storage.c +2 -2
index 035c263aab1b..99a42a2b6e14 100644
@@ -39,7 +39,8 @@ static void test_sys_enter_exit(void)
  static void test_exit_creds(void)
  {
      struct task_local_storage_exit_creds *skel;
-     int err;
+     int err, run_count, sync_rcu_calls = 0;
+     const int MAX_SYNC_RCU_CALLS = 1000;
 
      skel = task_local_storage_exit_creds__open_and_load();
      if (!ASSERT_OK_PTR(skel, "skel_open_and_load"))
@@ -53,8 +54,19 @@ static void test_exit_creds(void)
      if (CHECK_FAIL(system("ls > /dev/null")))
              goto out;
 
-     /* sync rcu to make sure exit_creds() is called for "ls" */

tools/testing/selftests/bpf/prog_tests/task_local_storage.c static void test_exit_creds(void) · line 65

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/task_local_storage.c +9 -1
-     kern_sync_rcu();
+     /* kern_sync_rcu is not enough on its own as the read section we want
+      * to wait for may start after we enter synchronize_rcu, so our call
+      * won't wait for the section to finish. Loop on the run counter
+      * as well to ensure the program has run.
+      */
+     do {
+             kern_sync_rcu();
+             run_count = __atomic_load_n(&skel->bss->run_count, __ATOMIC_SEQ_CST);
+     } while (run_count == 0 && ++sync_rcu_calls < MAX_SYNC_RCU_CALLS);

Might have been easier to do the following instead?

Quoted 7 lines
Quoted 3 lines

int sync_rcu_calls = 1000;
do {
} while (run_count == 0 && --sync_rcu_calls);

I think it's a preference of the author.
Both are fine. imo.

Agreed, that's why I acked it, shouldn't really matter.

Quoted 5 lines

I was about to apply, but then noticed Delyan's author line and SOB are different. @meta vs @fb :(
Delyan, please fix.

Fixing SOB is not something maintainers can do while applying.