Skip to contentSkip to conversation

selftests/bpf: Fix flakiness of task_local_storage/sys_enter_exit

PATCH Applied Ihor Solodrai 4 participants 5 messages Feb 24

Subsystems (3)
lore
Share & tools
lore

Ihor Solodrai

author reviewer
#0 lore raw

PATCH v2 1/1 selftests/bpf: Fix flakiness of task_local_storage/sys_enter_exit

The test_sys_enter_exit test was setting target_pid before attaching the BPF programs, which causes syscalls made during the attach phase to be counted. This is flaky because, apparently, there is no guarantee that both on_enter and on_exit will trigger during the attachment.

Move the target_pid assignment to after task_local_storage__attach() so that only explicit sys_gettid() calls are counted.

---

v1->v2: reset skel->bss->target_pid to 0 before asserts (Amery)
v1: https://lore.kernel.org/bpf/[email protected]/

---
 .../bpf/prog_tests/task_local_storage.c          | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)
tools/testing/selftests/bpf/prog_tests/task_local_storage.c +11 -5
index 7bee33797c71..1b26c12f255a 100644
@@ -25,24 +25,30 @@
 static void test_sys_enter_exit(void)
 {
 	struct task_local_storage *skel;
+	pid_t pid = sys_gettid();
 	int err;
 
 	skel = task_local_storage__open_and_load();
 	if (!ASSERT_OK_PTR(skel, "skel_open_and_load"))
 		return;
 
-	skel->bss->target_pid = sys_gettid();
-
 	err = task_local_storage__attach(skel);
 	if (!ASSERT_OK(err, "skel_attach"))
 		goto out;
 
+	/* Set target_pid after attach so that syscalls made during
+	 * attach are not counted.
+	 */
+	skel->bss->target_pid = pid;
+
 	sys_gettid();
 	sys_gettid();
 
-	/* 3x syscalls: 1x attach and 2x gettid */
-	ASSERT_EQ(skel->bss->enter_cnt, 3, "enter_cnt");
-	ASSERT_EQ(skel->bss->exit_cnt, 3, "exit_cnt");
+	skel->bss->target_pid = 0;
+
+	/* 2x gettid syscalls */
+	ASSERT_EQ(skel->bss->enter_cnt, 2, "enter_cnt");
+	ASSERT_EQ(skel->bss->exit_cnt, 2, "exit_cnt");
 	ASSERT_EQ(skel->bss->mismatch_cnt, 0, "mismatch_cnt");
 out:
 	task_local_storage__destroy(skel);
Signature · 1 line
2.53.0
reviewer Reviewed-by on v2 1/1
#1 lore raw
Quoted 6 lines · Ihor Solodrai

The test_sys_enter_exit test was setting target_pid before attaching the BPF programs, which causes syscalls made during the attach phase to be counted. This is flaky because, apparently, there is no guarantee that both on_enter and on_exit will trigger during the attachment.

Quoted 7 lines

Move the target_pid assignment to after task_local_storage__attach() so that only explicit sys_gettid() calls are counted.

Quoted 49 lines

---

v1->v2: reset skel->bss->target_pid to 0 before asserts (Amery)
v1: https://lore.kernel.org/bpf/[email protected]/

---
 .../bpf/prog_tests/task_local_storage.c          | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)
tools/testing/selftests/bpf/prog_tests/task_local_storage.c +11 -5
index 7bee33797c71..1b26c12f255a 100644
@@ -25,24 +25,30 @@
 static void test_sys_enter_exit(void)
 {
 	struct task_local_storage *skel;
+	pid_t pid = sys_gettid();
 	int err;
 
 	skel = task_local_storage__open_and_load();
 	if (!ASSERT_OK_PTR(skel, "skel_open_and_load"))
 		return;
 
-	skel->bss->target_pid = sys_gettid();
-
 	err = task_local_storage__attach(skel);
 	if (!ASSERT_OK(err, "skel_attach"))
 		goto out;
 
+	/* Set target_pid after attach so that syscalls made during
+	 * attach are not counted.
+	 */
+	skel->bss->target_pid = pid;
+
 	sys_gettid();
 	sys_gettid();
 
-	/* 3x syscalls: 1x attach and 2x gettid */
-	ASSERT_EQ(skel->bss->enter_cnt, 3, "enter_cnt");
-	ASSERT_EQ(skel->bss->exit_cnt, 3, "exit_cnt");
+	skel->bss->target_pid = 0;
+
+	/* 2x gettid syscalls */
+	ASSERT_EQ(skel->bss->enter_cnt, 2, "enter_cnt");
+	ASSERT_EQ(skel->bss->exit_cnt, 2, "exit_cnt");
 	ASSERT_EQ(skel->bss->mismatch_cnt, 0, "mismatch_cnt");
 out:
 	task_local_storage__destroy(skel);
#2 lore raw
In reply to Ihor Solodrai
Quoted 50 lines · Ihor Solodrai

The test_sys_enter_exit test was setting target_pid before attaching the BPF programs, which causes syscalls made during the attach phase to be counted. This is flaky because, apparently, there is no guarantee that both on_enter and on_exit will trigger during the attachment.

Move the target_pid assignment to after task_local_storage__attach() so that only explicit sys_gettid() calls are counted.

---

v1->v2: reset skel->bss->target_pid to 0 before asserts (Amery)
v1: https://lore.kernel.org/bpf/[email protected]/

---
 .../bpf/prog_tests/task_local_storage.c          | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)
tools/testing/selftests/bpf/prog_tests/task_local_storage.c +6 -2
index 7bee33797c71..1b26c12f255a 100644
@@ -25,24 +25,30 @@
 static void test_sys_enter_exit(void)
 {
        struct task_local_storage *skel;
+       pid_t pid = sys_gettid();
        int err;
 
        skel = task_local_storage__open_and_load();
        if (!ASSERT_OK_PTR(skel, "skel_open_and_load"))
                return;
 
-       skel->bss->target_pid = sys_gettid();
-
        err = task_local_storage__attach(skel);
        if (!ASSERT_OK(err, "skel_attach"))
                goto out;
 
+       /* Set target_pid after attach so that syscalls made during
+        * attach are not counted.
+        */
+       skel->bss->target_pid = pid;
+
        sys_gettid();

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

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/task_local_storage.c +5 -3
        sys_gettid();
 
-       /* 3x syscalls: 1x attach and 2x gettid */
-       ASSERT_EQ(skel->bss->enter_cnt, 3, "enter_cnt");
-       ASSERT_EQ(skel->bss->exit_cnt, 3, "exit_cnt");
+       skel->bss->target_pid = 0;
+
+       /* 2x gettid syscalls */
+       ASSERT_EQ(skel->bss->enter_cnt, 2, "enter_cnt");
+       ASSERT_EQ(skel->bss->exit_cnt, 2, "exit_cnt");

Does tools/testing/selftests/bpf/prog_tests/cgrp_local_storage.c also have the same flakiness issue and should also be updated in the same way?

Quoted 6 lines
tools/testing/selftests/bpf/prog_tests/task_local_storage.c +0 -1
        ASSERT_EQ(skel->bss->mismatch_cnt, 0, "mismatch_cnt");
 out:
        task_local_storage__destroy(skel);
--

2.53.0

author reviewer
#3 lore raw
Quoted 53 lines · Amery Hung
Quoted 52 lines · Ihor Solodrai

The test_sys_enter_exit test was setting target_pid before attaching the BPF programs, which causes syscalls made during the attach phase to be counted. This is flaky because, apparently, there is no guarantee that both on_enter and on_exit will trigger during the attachment.

Move the target_pid assignment to after task_local_storage__attach() so that only explicit sys_gettid() calls are counted.

---

v1->v2: reset skel->bss->target_pid to 0 before asserts (Amery)
v1: https://lore.kernel.org/bpf/[email protected]/

---
 .../bpf/prog_tests/task_local_storage.c          | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)
tools/testing/selftests/bpf/prog_tests/task_local_storage.c +6 -2
index 7bee33797c71..1b26c12f255a 100644
@@ -25,24 +25,30 @@
 static void test_sys_enter_exit(void)
 {
        struct task_local_storage *skel;
+       pid_t pid = sys_gettid();
        int err;
 
        skel = task_local_storage__open_and_load();
        if (!ASSERT_OK_PTR(skel, "skel_open_and_load"))
                return;
 
-       skel->bss->target_pid = sys_gettid();
-
        err = task_local_storage__attach(skel);
        if (!ASSERT_OK(err, "skel_attach"))
                goto out;
 
+       /* Set target_pid after attach so that syscalls made during
+        * attach are not counted.
+        */
+       skel->bss->target_pid = pid;
+
        sys_gettid();
        sys_gettid();
 
Quoted 12 lines

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

Quoted 8 lines
tools/testing/selftests/bpf/prog_tests/task_local_storage.c +5 -3
-       /* 3x syscalls: 1x attach and 2x gettid */
-       ASSERT_EQ(skel->bss->enter_cnt, 3, "enter_cnt");
-       ASSERT_EQ(skel->bss->exit_cnt, 3, "exit_cnt");
+       skel->bss->target_pid = 0;
+
+       /* 2x gettid syscalls */
+       ASSERT_EQ(skel->bss->enter_cnt, 2, "enter_cnt");
+       ASSERT_EQ(skel->bss->exit_cnt, 2, "exit_cnt");

Does tools/testing/selftests/bpf/prog_tests/cgrp_local_storage.c also have the same flakiness issue and should also be updated in the same way?

I haven't seen similar failures on CI, but that may just mean we were lucky.

Do you know of other tests that may have the same issue?

I checked these, but only task_local_storage and cgrp_local_storage assert counts:

$ grep -r 'skel->bss->target_pid = .*gettid' tools/testing/selftests/bpf/
tools/testing/selftests/bpf/prog_tests/cgroup_xattr.c:  skel->bss->target_pid = sys_gettid();
tools/testing/selftests/bpf/prog_tests/rcu_read_lock.c: skel->bss->target_pid = sys_gettid();
tools/testing/selftests/bpf/prog_tests/rcu_read_lock.c: skel->bss->target_pid = sys_gettid();
tools/testing/selftests/bpf/prog_tests/task_local_storage.c:    skel->bss->target_pid = sys_gettid();
tools/testing/selftests/bpf/prog_tests/cgrp_local_storage.c:    skel->bss->target_pid = sys_gettid();
tools/testing/selftests/bpf/prog_tests/cgrp_local_storage.c:    skel->bss->target_pid = sys_gettid();
Quoted 7 lines
Quoted 7 lines
tools/testing/selftests/bpf/prog_tests/task_local_storage.c +0 -1
        ASSERT_EQ(skel->bss->mismatch_cnt, 0, "mismatch_cnt");
 out:
        task_local_storage__destroy(skel);
--

2.53.0

#4 lore raw
In reply to Ihor Solodrai
This patch was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:

Hello:

This patch was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:

Quoted 10 lines · On Tue, 24 Feb 2026 13:12:02 -0800 you wrote:

The test_sys_enter_exit test was setting target_pid before attaching the BPF programs, which causes syscalls made during the attach phase to be counted. This is flaky because, apparently, there is no guarantee that both on_enter and on_exit will trigger during the attachment.

Move the target_pid assignment to after task_local_storage__attach() so that only explicit sys_gettid() calls are counted.

[...]

Here is the summary with links:
- [bpf-next,v2] selftests/bpf: Fix flakiness of task_local_storage/sys_enter_exit

    https://git.kernel.org/bpf/bpf-next/c/c89b50cc6b9f

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html