Skip to contentSkip to conversation

selftests/bpf: Add missing trampoline program type to trampoline_count test

PATCH Superseded Yuntao Wang 4 participants 5 messages May 15, 2022 – May 20, 2022

Subsystems (6)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 selftests/bpf: Add missing trampoline program type to trampoline_count test

Currently the trampoline_count test doesn't include any fmod_ret bpf programs, fix it to make the test cover all possible trampoline program types.

Since fmod_ret bpf programs can't be attached to __set_task_comm function, as it's neither whitelisted for error injection nor a security hook, change it to bpf_modify_return_test.

This patch also does some other cleanups such as removing duplicate code, dropping inconsistent comments, etc.

---
 include/linux/bpf.h                           |   2 +-
 .../bpf/prog_tests/trampoline_count.c         | 121 ++++++------------
 .../bpf/progs/test_trampoline_count.c         |  16 ++-
 3 files changed, 47 insertions(+), 92 deletions(-)
include/linux/bpf.h +1 -1
index c107392b0ba7..cb01f247e05f 100644
@@ -724,7 +724,7 @@ struct btf_func_model {
 #define BPF_TRAMP_F_RET_FENTRY_RET	BIT(4)
 
 /* Each call __bpf_prog_enter + call bpf_func + call __bpf_prog_exit is ~50
- * bytes on x86.  Pick a number to fit into BPF_IMAGE_SIZE / 2
+ * bytes on x86.
  */
 #define BPF_MAX_TRAMP_LINKS 38
 
tools/testing/selftests/bpf/prog_tests/trampoline_count.c +37 -84

163 lines not on this page · raw message

tools/testing/selftests/bpf/progs/test_trampoline_count.c +9 -7
index f030e469d05b..7765720da7d5 100644
@@ -1,20 +1,22 @@
 // SPDX-License-Identifier: GPL-2.0
-#include <stdbool.h>
-#include <stddef.h>
 #include <linux/bpf.h>
 #include <bpf/bpf_helpers.h>
 #include <bpf/bpf_tracing.h>
 
-struct task_struct;
+SEC("fentry/bpf_modify_return_test")
+int BPF_PROG(fentry_test, int a, int *b)
+{
+	return 0;
+}
 
-SEC("fentry/__set_task_comm")
-int BPF_PROG(prog1, struct task_struct *tsk, const char *buf, bool exec)
+SEC("fmod_ret/bpf_modify_return_test")
+int BPF_PROG(fmod_ret_test, int a, int *b, int ret)
 {
 	return 0;
 }
 
-SEC("fexit/__set_task_comm")
-int BPF_PROG(prog2, struct task_struct *tsk, const char *buf, bool exec)
+SEC("fexit/bpf_modify_return_test")
+int BPF_PROG(fexit_test, int a, int *b, int ret)
 {
 	return 0;
 }
Signature · 1 line
2.36.1
Acked-by on v1 1/1
#1 lore raw
Quoted 4 lines · Yuntao Wang

Currently the trampoline_count test doesn't include any fmod_ret bpf programs, fix it to make the test cover all possible trampoline program types.

Quoted 8 lines

Since fmod_ret bpf programs can't be attached to __set_task_comm function, as it's neither whitelisted for error injection nor a security hook, change it to bpf_modify_return_test.

This patch also does some other cleanups such as removing duplicate code, dropping inconsistent comments, etc.

#2 lore raw
In reply to Yuntao Wang
Quoted 19 lines · Yuntao Wang

Currently the trampoline_count test doesn't include any fmod_ret bpf programs, fix it to make the test cover all possible trampoline program types.

Since fmod_ret bpf programs can't be attached to __set_task_comm function, as it's neither whitelisted for error injection nor a security hook, change it to bpf_modify_return_test.

This patch also does some other cleanups such as removing duplicate code, dropping inconsistent comments, etc.

---
 include/linux/bpf.h                           |   2 +-
 .../bpf/prog_tests/trampoline_count.c         | 121 ++++++------------
 .../bpf/progs/test_trampoline_count.c         |  16 ++-
 3 files changed, 47 insertions(+), 92 deletions(-)

[...]

tools/testing/selftests/bpf/prog_tests/trampoline_count.c line 113

Quoted 10 lines
        /* with E2BIG error */
tools/testing/selftests/bpf/prog_tests/trampoline_count.c +1 -5
-       ASSERT_EQ(err, -E2BIG, "proper error check");
+       ASSERT_EQ(libbpf_get_error(link), -E2BIG, "E2BIG");
        ASSERT_EQ(link, NULL, "ptr_is_null");
 
-       /* and finaly execute the probe */
-       if (CHECK_FAIL(prctl(PR_GET_NAME, comm, 0L, 0L, 0L)))
-               goto cleanup_extra;
-       CHECK_FAIL(test_task_rename());

we stopped testing that kernel function actually can be called properly, why don't you do bpf_prog_test_run() here to trigger bpf_modify_return_test in kernel?

Quoted 14 lines
tools/testing/selftests/bpf/prog_tests/trampoline_count.c +1 -8
-       CHECK_FAIL(prctl(PR_SET_NAME, comm, 0L, 0L, 0L));
-
-cleanup_extra:
-       bpf_object__close(obj);
 cleanup:
-       if (i >= MAX_TRAMP_PROGS)
-               i = MAX_TRAMP_PROGS - 1;
        for (; i >= 0; i--) {
-               bpf_link__destroy(inst[i].link_fentry);
-               bpf_link__destroy(inst[i].link_fexit);
+               bpf_link__destroy(inst[i].link);
                bpf_object__close(inst[i].obj);
        }
 }

[...]

#3 lore raw

PATCH v2 1/1 selftests/bpf: Add missing trampoline program type to trampoline_count test

Currently the trampoline_count test doesn't include any fmod_ret bpf programs, fix it to make the test cover all possible trampoline program types.

Since fmod_ret bpf programs can't be attached to __set_task_comm function, as it's neither whitelisted for error injection nor a security hook, change it to bpf_modify_return_test.

This patch also does some other cleanups such as removing duplicate code, dropping inconsistent comments, etc.

---
v1->v2: call bpf_prog_test_run() to trigger bpf_modify_return_test in kernel

 include/linux/bpf.h                           |   2 +-
 .../bpf/prog_tests/trampoline_count.c         | 134 +++++++-----------
 .../bpf/progs/test_trampoline_count.c         |  16 ++-
 3 files changed, 61 insertions(+), 91 deletions(-)
include/linux/bpf.h +1 -1
index c107392b0ba7..cb01f247e05f 100644
@@ -724,7 +724,7 @@ struct btf_func_model {
 #define BPF_TRAMP_F_RET_FENTRY_RET	BIT(4)
 
 /* Each call __bpf_prog_enter + call bpf_func + call __bpf_prog_exit is ~50
- * bytes on x86.  Pick a number to fit into BPF_IMAGE_SIZE / 2
+ * bytes on x86.
  */
 #define BPF_MAX_TRAMP_LINKS 38
 
tools/testing/selftests/bpf/prog_tests/trampoline_count.c +51 -83

177 lines not on this page · raw message

tools/testing/selftests/bpf/progs/test_trampoline_count.c +9 -7
index f030e469d05b..7765720da7d5 100644
@@ -1,20 +1,22 @@
 // SPDX-License-Identifier: GPL-2.0
-#include <stdbool.h>
-#include <stddef.h>
 #include <linux/bpf.h>
 #include <bpf/bpf_helpers.h>
 #include <bpf/bpf_tracing.h>
 
-struct task_struct;
+SEC("fentry/bpf_modify_return_test")
+int BPF_PROG(fentry_test, int a, int *b)
+{
+	return 0;
+}
 
-SEC("fentry/__set_task_comm")
-int BPF_PROG(prog1, struct task_struct *tsk, const char *buf, bool exec)
+SEC("fmod_ret/bpf_modify_return_test")
+int BPF_PROG(fmod_ret_test, int a, int *b, int ret)
 {
 	return 0;
 }
 
-SEC("fexit/__set_task_comm")
-int BPF_PROG(prog2, struct task_struct *tsk, const char *buf, bool exec)
+SEC("fexit/bpf_modify_return_test")
+int BPF_PROG(fexit_test, int a, int *b, int ret)
 {
 	return 0;
 }
Signature · 1 line
2.36.1
#4 lore raw
This patch was applied to bpf/bpf-next.git (master) by Andrii Nakryiko <[email protected]>:

Hello:

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

Quoted 9 lines · On Thu, 19 May 2022 23:06:10 +0800 you wrote:

Currently the trampoline_count test doesn't include any fmod_ret bpf programs, fix it to make the test cover all possible trampoline program types.

Since fmod_ret bpf programs can't be attached to __set_task_comm function, as it's neither whitelisted for error injection nor a security hook, change it to bpf_modify_return_test.

[...]

Here is the summary with links:
- [bpf-next,v2] selftests/bpf: Add missing trampoline program type to trampoline_count test

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

You are awesome, thank you!

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