[PATCH] selftests/bpf: restrict ns_current_pid_tgid updates to target task

Sun Jian posted 1 patch 3 weeks ago
.../testing/selftests/bpf/prog_tests/ns_current_pid_tgid.c | 5 +++--
.../testing/selftests/bpf/progs/test_ns_current_pid_tgid.c | 7 +++++++
2 files changed, 10 insertions(+), 2 deletions(-)
[PATCH] selftests/bpf: restrict ns_current_pid_tgid updates to target task
Posted by Sun Jian 3 weeks ago
ns_current_pid_tgid uses a shared syscall tracepoint for
current_pid_tgid tracepoint subtests. When selftests run in parallel,
unrelated tasks can trigger the same hook and overwrite BSS results,
causing false failures.

Store the expected pid/tgid in BSS and update user_pid/user_tgid only
when bpf_get_ns_current_pid_tgid() returns values matching the target
task.

This prevents unrelated tasks from interfering with the test, allows
dropping the serial-only restriction, and removes the obsolete TODO
comment.

Tested:
  ./test_progs -t current_pid_tgid
  ./test_progs -j$(nproc) -t current_pid_tgid

Signed-off-by: Sun Jian <sun.jian.kdev@gmail.com>
---
 .../testing/selftests/bpf/prog_tests/ns_current_pid_tgid.c | 5 +++--
 .../testing/selftests/bpf/progs/test_ns_current_pid_tgid.c | 7 +++++++
 2 files changed, 10 insertions(+), 2 deletions(-)

diff --git a/tools/testing/selftests/bpf/prog_tests/ns_current_pid_tgid.c b/tools/testing/selftests/bpf/prog_tests/ns_current_pid_tgid.c
index 99c953f2be21..e3938bb48151 100644
--- a/tools/testing/selftests/bpf/prog_tests/ns_current_pid_tgid.c
+++ b/tools/testing/selftests/bpf/prog_tests/ns_current_pid_tgid.c
@@ -34,6 +34,8 @@ static int get_pid_tgid(pid_t *pid, pid_t *tgid,
 	bss->ino = st.st_ino;
 	bss->user_pid = 0;
 	bss->user_tgid = 0;
+	bss->expected_pid = *pid;
+	bss->expected_tgid = *tgid;
 	return 0;
 }
 
@@ -200,8 +202,7 @@ static void test_ns_current_pid_tgid_new_ns(int (*fn)(void *), void *arg)
 		return;
 }
 
-/* TODO: use a different tracepoint */
-void serial_test_current_pid_tgid(void)
+void test_current_pid_tgid(void)
 {
 	if (test__start_subtest("root_ns_tp"))
 		test_current_pid_tgid_tp(NULL);
diff --git a/tools/testing/selftests/bpf/progs/test_ns_current_pid_tgid.c b/tools/testing/selftests/bpf/progs/test_ns_current_pid_tgid.c
index 386315afad65..d02c182a7100 100644
--- a/tools/testing/selftests/bpf/progs/test_ns_current_pid_tgid.c
+++ b/tools/testing/selftests/bpf/progs/test_ns_current_pid_tgid.c
@@ -14,6 +14,8 @@ struct {
 
 __u64 user_pid = 0;
 __u64 user_tgid = 0;
+__u64 expected_pid = 0;
+__u64 expected_tgid = 0;
 __u64 dev = 0;
 __u64 ino = 0;
 
@@ -24,6 +26,11 @@ static void get_pid_tgid(void)
 	if (bpf_get_ns_current_pid_tgid(dev, ino, &nsdata, sizeof(struct bpf_pidns_info)))
 		return;
 
+	if (expected_pid && nsdata.pid != expected_pid)
+		return;
+	if (expected_tgid && nsdata.tgid != expected_tgid)
+		return;
+
 	user_pid = nsdata.pid;
 	user_tgid = nsdata.tgid;
 }

base-commit: f338e77383789c0cae23ca3d48adcc5e9e137e3c
-- 
2.43.0
Re: [PATCH] selftests/bpf: restrict ns_current_pid_tgid updates to target task
Posted by Emil Tsalapatis 3 weeks ago
On Mon Mar 16, 2026 at 3:34 AM EDT, Sun Jian wrote:
> ns_current_pid_tgid uses a shared syscall tracepoint for
> current_pid_tgid tracepoint subtests. When selftests run in parallel,
> unrelated tasks can trigger the same hook and overwrite BSS results,
> causing false failures.
>
> Store the expected pid/tgid in BSS and update user_pid/user_tgid only
> when bpf_get_ns_current_pid_tgid() returns values matching the target
> task.
>
> This prevents unrelated tasks from interfering with the test, allows
> dropping the serial-only restriction, and removes the obsolete TODO
> comment.
>
> Tested:
>   ./test_progs -t current_pid_tgid
>   ./test_progs -j$(nproc) -t current_pid_tgid
>
> Signed-off-by: Sun Jian <sun.jian.kdev@gmail.com>

Reviewed-by: Emil Tsalapatis <emil@etsalapatis.com>

> ---
>  .../testing/selftests/bpf/prog_tests/ns_current_pid_tgid.c | 5 +++--
>  .../testing/selftests/bpf/progs/test_ns_current_pid_tgid.c | 7 +++++++
>  2 files changed, 10 insertions(+), 2 deletions(-)
>
> diff --git a/tools/testing/selftests/bpf/prog_tests/ns_current_pid_tgid.c b/tools/testing/selftests/bpf/prog_tests/ns_current_pid_tgid.c
> index 99c953f2be21..e3938bb48151 100644
> --- a/tools/testing/selftests/bpf/prog_tests/ns_current_pid_tgid.c
> +++ b/tools/testing/selftests/bpf/prog_tests/ns_current_pid_tgid.c
> @@ -34,6 +34,8 @@ static int get_pid_tgid(pid_t *pid, pid_t *tgid,
>  	bss->ino = st.st_ino;
>  	bss->user_pid = 0;
>  	bss->user_tgid = 0;
> +	bss->expected_pid = *pid;
> +	bss->expected_tgid = *tgid;
>  	return 0;
>  }
>  
> @@ -200,8 +202,7 @@ static void test_ns_current_pid_tgid_new_ns(int (*fn)(void *), void *arg)
>  		return;
>  }
>  
> -/* TODO: use a different tracepoint */
> -void serial_test_current_pid_tgid(void)
> +void test_current_pid_tgid(void)
>  {
>  	if (test__start_subtest("root_ns_tp"))
>  		test_current_pid_tgid_tp(NULL);
> diff --git a/tools/testing/selftests/bpf/progs/test_ns_current_pid_tgid.c b/tools/testing/selftests/bpf/progs/test_ns_current_pid_tgid.c
> index 386315afad65..d02c182a7100 100644
> --- a/tools/testing/selftests/bpf/progs/test_ns_current_pid_tgid.c
> +++ b/tools/testing/selftests/bpf/progs/test_ns_current_pid_tgid.c
> @@ -14,6 +14,8 @@ struct {
>  
>  __u64 user_pid = 0;
>  __u64 user_tgid = 0;
> +__u64 expected_pid = 0;
> +__u64 expected_tgid = 0;
>  __u64 dev = 0;
>  __u64 ino = 0;
>  
> @@ -24,6 +26,11 @@ static void get_pid_tgid(void)
>  	if (bpf_get_ns_current_pid_tgid(dev, ino, &nsdata, sizeof(struct bpf_pidns_info)))
>  		return;
>  
> +	if (expected_pid && nsdata.pid != expected_pid)
> +		return;
> +	if (expected_tgid && nsdata.tgid != expected_tgid)
> +		return;
> +
>  	user_pid = nsdata.pid;
>  	user_tgid = nsdata.tgid;
>  }
>
> base-commit: f338e77383789c0cae23ca3d48adcc5e9e137e3c