Skip to contentSkip to conversation

selftests/bpf: restrict ns_current_pid_tgid updates to target task

PATCH In review Sun Jian 2 participants 2 messages Mar 16

Subsystems (3)
lore
Share & tools
lore

Sun Jian

author
#0 lore raw

PATCH 1/1 selftests/bpf: restrict ns_current_pid_tgid updates to target task

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

---
 .../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(-)
tools/testing/selftests/bpf/prog_tests/ns_current_pid_tgid.c +3 -2
index 99c953f2be21..e3938bb48151 100644
@@ -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);
tools/testing/selftests/bpf/progs/test_ns_current_pid_tgid.c +7 -0
index 386315afad65..d02c182a7100 100644
@@ -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

Signature · 1 line
2.43.0
reviewer Reviewed-by on v1 1/1
#1 lore raw
Quoted 13 lines · Sun Jian

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.

Quoted 5 lines

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

Quoted 55 lines
---
 .../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(-)
tools/testing/selftests/bpf/prog_tests/ns_current_pid_tgid.c +3 -2
index 99c953f2be21..e3938bb48151 100644
@@ -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);
tools/testing/selftests/bpf/progs/test_ns_current_pid_tgid.c +7 -0
index 386315afad65..d02c182a7100 100644
@@ -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