public inbox for bpf@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] selftests/bpf: modify_return: isolate fmod_ret hooks by pid
@ 2026-03-12 10:42 Sun Jian
  2026-03-12 12:24 ` Jiri Olsa
  2026-03-13  3:45 ` [PATCH v2] " Sun Jian
  0 siblings, 2 replies; 4+ messages in thread
From: Sun Jian @ 2026-03-12 10:42 UTC (permalink / raw)
  To: bpf, linux-kselftest, andrii, shuah
  Cc: ast, daniel, eddyz87, martin.lau, Sun Jian

modify_return's fmod_ret programs can override bpf_modify_return_test()'s
return value, which conflicts with get_func_ip_test when selftests run in
parallel.

Store current tgid in BSS and make modify_return hooks act only for that
tgid. For other tasks, fentry/fexit become no-ops and fmod_ret returns the
original ret.

Drop the serial-only restriction and remove the TODO comment.

Tested:
  sudo ./test_progs -t modify_return
  sudo ./test_progs -t get_func_ip_test
  sudo ./test_progs -j$(nproc) -t modify_return
  sudo ./test_progs -j$(nproc) -t get_func_ip_test

Signed-off-by: Sun Jian <sun.jian.kdev@gmail.com>
---
 .../selftests/bpf/prog_tests/modify_return.c  |  8 ++++---
 .../selftests/bpf/progs/modify_return.c       | 23 +++++++++++++++++++
 2 files changed, 28 insertions(+), 3 deletions(-)

diff --git a/tools/testing/selftests/bpf/prog_tests/modify_return.c b/tools/testing/selftests/bpf/prog_tests/modify_return.c
index a70c99c2f8c8..4661d77ebdfc 100644
--- a/tools/testing/selftests/bpf/prog_tests/modify_return.c
+++ b/tools/testing/selftests/bpf/prog_tests/modify_return.c
@@ -5,6 +5,7 @@
  */
 
 #include <test_progs.h>
+#include <unistd.h>
 #include "modify_return.skel.h"
 
 #define LOWER(x) ((x) & 0xffff)
@@ -23,11 +24,13 @@ static void run_test(__u32 input_retval, __u16 want_side_effect, __s16 want_ret)
 	if (!ASSERT_OK_PTR(skel, "skel_load"))
 		goto cleanup;
 
+	skel->bss->input_retval = input_retval;
+	skel->bss->test_pid = getpid();
+
 	err = modify_return__attach(skel);
 	if (!ASSERT_OK(err, "modify_return__attach failed"))
 		goto cleanup;
 
-	skel->bss->input_retval = input_retval;
 	prog_fd = bpf_program__fd(skel->progs.fmod_ret_test);
 	err = bpf_prog_test_run_opts(prog_fd, &topts);
 	ASSERT_OK(err, "test_run");
@@ -49,8 +52,7 @@ static void run_test(__u32 input_retval, __u16 want_side_effect, __s16 want_ret)
 	modify_return__destroy(skel);
 }
 
-/* TODO: conflict with get_func_ip_test */
-void serial_test_modify_return(void)
+void test_modify_return(void)
 {
 	run_test(0 /* input_retval */,
 		 2 /* want_side_effect */,
diff --git a/tools/testing/selftests/bpf/progs/modify_return.c b/tools/testing/selftests/bpf/progs/modify_return.c
index 3376d4849f58..5ff18ddb3050 100644
--- a/tools/testing/selftests/bpf/progs/modify_return.c
+++ b/tools/testing/selftests/bpf/progs/modify_return.c
@@ -7,16 +7,29 @@
 #include <linux/bpf.h>
 #include <bpf/bpf_helpers.h>
 #include <bpf/bpf_tracing.h>
+#include <stdbool.h>
 
 char _license[] SEC("license") = "GPL";
 
 static int sequence = 0;
 __s32 input_retval = 0;
+__u32 test_pid = 0;
+
+static __always_inline bool match_pid(void)
+{
+	__u64 pid_tgid = bpf_get_current_pid_tgid();
+
+	if (!test_pid)
+		return false;
+	return (__u32)(pid_tgid >> 32) == test_pid;
+}
 
 __u64 fentry_result = 0;
 SEC("fentry/bpf_modify_return_test")
 int BPF_PROG(fentry_test, int a, __u64 b)
 {
+	if (!match_pid())
+		return 0;
 	sequence++;
 	fentry_result = (sequence == 1);
 	return 0;
@@ -26,6 +39,8 @@ __u64 fmod_ret_result = 0;
 SEC("fmod_ret/bpf_modify_return_test")
 int BPF_PROG(fmod_ret_test, int a, int *b, int ret)
 {
+	if (!match_pid())
+		return ret;
 	sequence++;
 	/* This is the first fmod_ret program, the ret passed should be 0 */
 	fmod_ret_result = (sequence == 2 && ret == 0);
@@ -36,6 +51,8 @@ __u64 fexit_result = 0;
 SEC("fexit/bpf_modify_return_test")
 int BPF_PROG(fexit_test, int a, __u64 b, int ret)
 {
+	if (!match_pid())
+		return 0;
 	sequence++;
 	/* If the input_reval is non-zero a successful modification should have
 	 * occurred.
@@ -55,6 +72,8 @@ SEC("fentry/bpf_modify_return_test2")
 int BPF_PROG(fentry_test2, int a, int *b, short c, int d, void *e, char f,
 	     int g)
 {
+	if (!match_pid())
+		return 0;
 	sequence2++;
 	fentry_result2 = (sequence2 == 1);
 	return 0;
@@ -65,6 +84,8 @@ SEC("fmod_ret/bpf_modify_return_test2")
 int BPF_PROG(fmod_ret_test2, int a, int *b, short c, int d, void *e, char f,
 	     int g, int ret)
 {
+	if (!match_pid())
+		return ret;
 	sequence2++;
 	/* This is the first fmod_ret program, the ret passed should be 0 */
 	fmod_ret_result2 = (sequence2 == 2 && ret == 0);
@@ -76,6 +97,8 @@ SEC("fexit/bpf_modify_return_test2")
 int BPF_PROG(fexit_test2, int a, int *b, short c, int d, void *e, char f,
 	     int g, int ret)
 {
+	if (!match_pid())
+		return 0;
 	sequence2++;
 	/* If the input_reval is non-zero a successful modification should have
 	 * occurred.

base-commit: 80234b5ab240f52fa45d201e899e207b9265ef91
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] selftests/bpf: modify_return: isolate fmod_ret hooks by pid
  2026-03-12 10:42 [PATCH] selftests/bpf: modify_return: isolate fmod_ret hooks by pid Sun Jian
@ 2026-03-12 12:24 ` Jiri Olsa
  2026-03-13  3:45 ` [PATCH v2] " Sun Jian
  1 sibling, 0 replies; 4+ messages in thread
From: Jiri Olsa @ 2026-03-12 12:24 UTC (permalink / raw)
  To: Sun Jian
  Cc: bpf, linux-kselftest, andrii, shuah, ast, daniel, eddyz87,
	martin.lau

On Thu, Mar 12, 2026 at 06:42:19PM +0800, Sun Jian wrote:

SNIP

> diff --git a/tools/testing/selftests/bpf/progs/modify_return.c b/tools/testing/selftests/bpf/progs/modify_return.c
> index 3376d4849f58..5ff18ddb3050 100644
> --- a/tools/testing/selftests/bpf/progs/modify_return.c
> +++ b/tools/testing/selftests/bpf/progs/modify_return.c
> @@ -7,16 +7,29 @@
>  #include <linux/bpf.h>
>  #include <bpf/bpf_helpers.h>
>  #include <bpf/bpf_tracing.h>
> +#include <stdbool.h>
>  
>  char _license[] SEC("license") = "GPL";
>  
>  static int sequence = 0;
>  __s32 input_retval = 0;
> +__u32 test_pid = 0;
> +
> +static __always_inline bool match_pid(void)
> +{
> +	__u64 pid_tgid = bpf_get_current_pid_tgid();
> +
> +	if (!test_pid)
> +		return false;
> +	return (__u32)(pid_tgid >> 32) == test_pid;
> +}
>  
>  __u64 fentry_result = 0;
>  SEC("fentry/bpf_modify_return_test")
>  int BPF_PROG(fentry_test, int a, __u64 b)
>  {
> +	if (!match_pid())
> +		return 0;

I think just simple pid check like we do in other places
should be enough:

	if (bpf_get_current_pid_tgid() >> 32 != test_pid)
		return 0;

jirka


>  	sequence++;
>  	fentry_result = (sequence == 1);
>  	return 0;
> @@ -26,6 +39,8 @@ __u64 fmod_ret_result = 0;
>  SEC("fmod_ret/bpf_modify_return_test")
>  int BPF_PROG(fmod_ret_test, int a, int *b, int ret)
>  {
> +	if (!match_pid())
> +		return ret;
>  	sequence++;
>  	/* This is the first fmod_ret program, the ret passed should be 0 */
>  	fmod_ret_result = (sequence == 2 && ret == 0);
> @@ -36,6 +51,8 @@ __u64 fexit_result = 0;
>  SEC("fexit/bpf_modify_return_test")
>  int BPF_PROG(fexit_test, int a, __u64 b, int ret)
>  {
> +	if (!match_pid())
> +		return 0;
>  	sequence++;
>  	/* If the input_reval is non-zero a successful modification should have
>  	 * occurred.
> @@ -55,6 +72,8 @@ SEC("fentry/bpf_modify_return_test2")
>  int BPF_PROG(fentry_test2, int a, int *b, short c, int d, void *e, char f,
>  	     int g)
>  {
> +	if (!match_pid())
> +		return 0;
>  	sequence2++;
>  	fentry_result2 = (sequence2 == 1);
>  	return 0;
> @@ -65,6 +84,8 @@ SEC("fmod_ret/bpf_modify_return_test2")
>  int BPF_PROG(fmod_ret_test2, int a, int *b, short c, int d, void *e, char f,
>  	     int g, int ret)
>  {
> +	if (!match_pid())
> +		return ret;
>  	sequence2++;
>  	/* This is the first fmod_ret program, the ret passed should be 0 */
>  	fmod_ret_result2 = (sequence2 == 2 && ret == 0);
> @@ -76,6 +97,8 @@ SEC("fexit/bpf_modify_return_test2")
>  int BPF_PROG(fexit_test2, int a, int *b, short c, int d, void *e, char f,
>  	     int g, int ret)
>  {
> +	if (!match_pid())
> +		return 0;
>  	sequence2++;
>  	/* If the input_reval is non-zero a successful modification should have
>  	 * occurred.
> 
> base-commit: 80234b5ab240f52fa45d201e899e207b9265ef91
> -- 
> 2.43.0
> 
> 

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH v2] selftests/bpf: modify_return: isolate fmod_ret hooks by pid
  2026-03-12 10:42 [PATCH] selftests/bpf: modify_return: isolate fmod_ret hooks by pid Sun Jian
  2026-03-12 12:24 ` Jiri Olsa
@ 2026-03-13  3:45 ` Sun Jian
  2026-03-19  0:44   ` patchwork-bot+netdevbpf
  1 sibling, 1 reply; 4+ messages in thread
From: Sun Jian @ 2026-03-13  3:45 UTC (permalink / raw)
  To: bpf, linux-kselftest, andrii, shuah
  Cc: ast, daniel, eddyz87, martin.lau, Sun Jian

modify_return's fmod_ret programs can override bpf_modify_return_test()'s
return value, which conflicts with get_func_ip_test when selftests run in
parallel.

Store current tgid in BSS and make modify_return hooks act only for that
tgid. For other tasks, fentry/fexit become no-ops and fmod_ret returns the
original ret.

Drop the serial-only restriction and remove the TODO comment.

Tested:
  sudo ./test_progs -t modify_return
  sudo ./test_progs -t get_func_ip_test
  sudo ./test_progs -j$(nproc) -t modify_return
  sudo ./test_progs -j$(nproc) -t get_func_ip_test

Signed-off-by: Sun Jian <sun.jian.kdev@gmail.com>
---

v2:
- inline pid checks per suggestion from Jiri Olsa <jolsa@kernel.org>

 .../selftests/bpf/prog_tests/modify_return.c        |  8 +++++---
 tools/testing/selftests/bpf/progs/modify_return.c   | 13 +++++++++++++
 2 files changed, 18 insertions(+), 3 deletions(-)

diff --git a/tools/testing/selftests/bpf/prog_tests/modify_return.c b/tools/testing/selftests/bpf/prog_tests/modify_return.c
index a70c99c2f8c8..4661d77ebdfc 100644
--- a/tools/testing/selftests/bpf/prog_tests/modify_return.c
+++ b/tools/testing/selftests/bpf/prog_tests/modify_return.c
@@ -5,6 +5,7 @@
  */
 
 #include <test_progs.h>
+#include <unistd.h>
 #include "modify_return.skel.h"
 
 #define LOWER(x) ((x) & 0xffff)
@@ -23,11 +24,13 @@ static void run_test(__u32 input_retval, __u16 want_side_effect, __s16 want_ret)
 	if (!ASSERT_OK_PTR(skel, "skel_load"))
 		goto cleanup;
 
+	skel->bss->input_retval = input_retval;
+	skel->bss->test_pid = getpid();
+
 	err = modify_return__attach(skel);
 	if (!ASSERT_OK(err, "modify_return__attach failed"))
 		goto cleanup;
 
-	skel->bss->input_retval = input_retval;
 	prog_fd = bpf_program__fd(skel->progs.fmod_ret_test);
 	err = bpf_prog_test_run_opts(prog_fd, &topts);
 	ASSERT_OK(err, "test_run");
@@ -49,8 +52,7 @@ static void run_test(__u32 input_retval, __u16 want_side_effect, __s16 want_ret)
 	modify_return__destroy(skel);
 }
 
-/* TODO: conflict with get_func_ip_test */
-void serial_test_modify_return(void)
+void test_modify_return(void)
 {
 	run_test(0 /* input_retval */,
 		 2 /* want_side_effect */,
diff --git a/tools/testing/selftests/bpf/progs/modify_return.c b/tools/testing/selftests/bpf/progs/modify_return.c
index 3376d4849f58..68fabd2efe8d 100644
--- a/tools/testing/selftests/bpf/progs/modify_return.c
+++ b/tools/testing/selftests/bpf/progs/modify_return.c
@@ -12,11 +12,14 @@ char _license[] SEC("license") = "GPL";
 
 static int sequence = 0;
 __s32 input_retval = 0;
+__u32 test_pid = 0;
 
 __u64 fentry_result = 0;
 SEC("fentry/bpf_modify_return_test")
 int BPF_PROG(fentry_test, int a, __u64 b)
 {
+	if (bpf_get_current_pid_tgid() >> 32 != test_pid)
+		return 0;
 	sequence++;
 	fentry_result = (sequence == 1);
 	return 0;
@@ -26,6 +29,8 @@ __u64 fmod_ret_result = 0;
 SEC("fmod_ret/bpf_modify_return_test")
 int BPF_PROG(fmod_ret_test, int a, int *b, int ret)
 {
+	if (bpf_get_current_pid_tgid() >> 32 != test_pid)
+		return ret;
 	sequence++;
 	/* This is the first fmod_ret program, the ret passed should be 0 */
 	fmod_ret_result = (sequence == 2 && ret == 0);
@@ -36,6 +41,8 @@ __u64 fexit_result = 0;
 SEC("fexit/bpf_modify_return_test")
 int BPF_PROG(fexit_test, int a, __u64 b, int ret)
 {
+	if (bpf_get_current_pid_tgid() >> 32 != test_pid)
+		return 0;
 	sequence++;
 	/* If the input_reval is non-zero a successful modification should have
 	 * occurred.
@@ -55,6 +62,8 @@ SEC("fentry/bpf_modify_return_test2")
 int BPF_PROG(fentry_test2, int a, int *b, short c, int d, void *e, char f,
 	     int g)
 {
+	if (bpf_get_current_pid_tgid() >> 32 != test_pid)
+		return 0;
 	sequence2++;
 	fentry_result2 = (sequence2 == 1);
 	return 0;
@@ -65,6 +74,8 @@ SEC("fmod_ret/bpf_modify_return_test2")
 int BPF_PROG(fmod_ret_test2, int a, int *b, short c, int d, void *e, char f,
 	     int g, int ret)
 {
+	if (bpf_get_current_pid_tgid() >> 32 != test_pid)
+		return ret;
 	sequence2++;
 	/* This is the first fmod_ret program, the ret passed should be 0 */
 	fmod_ret_result2 = (sequence2 == 2 && ret == 0);
@@ -76,6 +87,8 @@ SEC("fexit/bpf_modify_return_test2")
 int BPF_PROG(fexit_test2, int a, int *b, short c, int d, void *e, char f,
 	     int g, int ret)
 {
+	if (bpf_get_current_pid_tgid() >> 32 != test_pid)
+		return 0;
 	sequence2++;
 	/* If the input_reval is non-zero a successful modification should have
 	 * occurred.

base-commit: 80234b5ab240f52fa45d201e899e207b9265ef91
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH v2] selftests/bpf: modify_return: isolate fmod_ret hooks by pid
  2026-03-13  3:45 ` [PATCH v2] " Sun Jian
@ 2026-03-19  0:44   ` patchwork-bot+netdevbpf
  0 siblings, 0 replies; 4+ messages in thread
From: patchwork-bot+netdevbpf @ 2026-03-19  0:44 UTC (permalink / raw)
  To: Sun Jian
  Cc: bpf, linux-kselftest, andrii, shuah, ast, daniel, eddyz87,
	martin.lau

Hello:

This patch was applied to bpf/bpf-next.git (master)
by Alexei Starovoitov <ast@kernel.org>:

On Fri, 13 Mar 2026 11:45:40 +0800 you wrote:
> modify_return's fmod_ret programs can override bpf_modify_return_test()'s
> return value, which conflicts with get_func_ip_test when selftests run in
> parallel.
> 
> Store current tgid in BSS and make modify_return hooks act only for that
> tgid. For other tasks, fentry/fexit become no-ops and fmod_ret returns the
> original ret.
> 
> [...]

Here is the summary with links:
  - [v2] selftests/bpf: modify_return: isolate fmod_ret hooks by pid
    https://git.kernel.org/bpf/bpf-next/c/4a4fedb8a523

You are awesome, thank you!
-- 
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html



^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-03-19  0:44 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-03-12 10:42 [PATCH] selftests/bpf: modify_return: isolate fmod_ret hooks by pid Sun Jian
2026-03-12 12:24 ` Jiri Olsa
2026-03-13  3:45 ` [PATCH v2] " Sun Jian
2026-03-19  0:44   ` patchwork-bot+netdevbpf

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox