Skip to content

Commit eb814cf

Browse files
Delyan KratunovAlexei Starovoitov
authored andcommitted
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 Signed-off-by: Delyan Kratunov <delyank@meta.com> Link: https://lore.kernel.org/r/156d4ef82275a074e8da8f4cffbd01b0c1466493.camel@meta.com Signed-off-by: Alexei Starovoitov <ast@kernel.org>
1 parent 12f9682 commit eb814cf

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

tools/testing/selftests/bpf/prog_tests/task_local_storage.c

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ static void test_sys_enter_exit(void)
3939
static void test_exit_creds(void)
4040
{
4141
struct task_local_storage_exit_creds *skel;
42-
int err;
42+
int err, run_count, sync_rcu_calls = 0;
43+
const int MAX_SYNC_RCU_CALLS = 1000;
4344

4445
skel = task_local_storage_exit_creds__open_and_load();
4546
if (!ASSERT_OK_PTR(skel, "skel_open_and_load"))
@@ -53,8 +54,19 @@ static void test_exit_creds(void)
5354
if (CHECK_FAIL(system("ls > /dev/null")))
5455
goto out;
5556

56-
/* sync rcu to make sure exit_creds() is called for "ls" */
57-
kern_sync_rcu();
57+
/* kern_sync_rcu is not enough on its own as the read section we want
58+
* to wait for may start after we enter synchronize_rcu, so our call
59+
* won't wait for the section to finish. Loop on the run counter
60+
* as well to ensure the program has run.
61+
*/
62+
do {
63+
kern_sync_rcu();
64+
run_count = __atomic_load_n(&skel->bss->run_count, __ATOMIC_SEQ_CST);
65+
} while (run_count == 0 && ++sync_rcu_calls < MAX_SYNC_RCU_CALLS);
66+
67+
ASSERT_NEQ(sync_rcu_calls, MAX_SYNC_RCU_CALLS,
68+
"sync_rcu count too high");
69+
ASSERT_NEQ(run_count, 0, "run_count");
5870
ASSERT_EQ(skel->bss->valid_ptr_count, 0, "valid_ptr_count");
5971
ASSERT_NEQ(skel->bss->null_ptr_count, 0, "null_ptr_count");
6072
out:

tools/testing/selftests/bpf/progs/task_local_storage_exit_creds.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ struct {
1414
__type(value, __u64);
1515
} task_storage SEC(".maps");
1616

17+
int run_count = 0;
1718
int valid_ptr_count = 0;
1819
int null_ptr_count = 0;
1920

@@ -28,5 +29,7 @@ int BPF_PROG(trace_exit_creds, struct task_struct *task)
2829
__sync_fetch_and_add(&valid_ptr_count, 1);
2930
else
3031
__sync_fetch_and_add(&null_ptr_count, 1);
32+
33+
__sync_fetch_and_add(&run_count, 1);
3134
return 0;
3235
}

0 commit comments

Comments
 (0)