tracing: perf_call_bpf: use struct trace_entry in struct syscall_tp_t
PATCH 1/1 tracing: perf_call_bpf: use struct trace_entry in struct syscall_tp_t
bpf tracepoint program uses struct trace_event_raw_sys_enter as argument where trace_entry is the first field. Use the same instead of unsigned long long since if it's amended (for example by RT patch) it accesses data with wrong offset.
--- kernel/trace/trace_syscalls.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)
kernel/trace/trace_syscalls.c +8 -2
@@ -555,12 +555,15 @@ static int perf_call_bpf_enter(struct trace_event_call *call, struct pt_regs *re struct syscall_trace_enter *rec) {struct syscall_tp_t { -unsigned long long regs; +struct trace_entry ent;unsigned long syscall_nr;unsigned long args[SYSCALL_DEFINE_MAXARGS ]; } param;int i; +BUILD_BUG_ON (sizeof (param.ent) <sizeof (void *)); + +/* __bpf_prog_run() requires *regs as the first parameter */ *(struct pt_regs **)¶m = regs; param.syscall_nr = rec->nr;for (i =0 ; i < sys_data->nb_args; i++)
@@ -657,11 +660,14 @@ static int perf_call_bpf_exit(struct trace_event_call *call, struct pt_regs *reg struct syscall_trace_exit *rec) {struct syscall_tp_t { -unsigned long long regs; +struct trace_entry ent;unsigned long syscall_nr;unsigned long ret; } param; +BUILD_BUG_ON (sizeof (param.ent) <sizeof (void *)); + +/* __bpf_prog_run() requires *regs as the first parameter */ *(struct pt_regs **)¶m = regs; param.syscall_nr = rec->nr; param.ret = rec->ret;
Signature · 1 line
2.41.0
Yonghong Song
reviewerQuoted 4 lines · Yauheni Kaliuta
bpf tracepoint program uses struct
trace_event_raw_sys_enteras argument wheretrace_entryis the first field. Use the same instead of unsigned long long since if it's amended (for example by RT patch) it accesses data with wrong offset.
Is this 'amended by RT patch' a real thing?
Quoted 11 lines
Signed-off-by Yauheni Kaliuta <[email protected]>--- kernel/trace/trace_syscalls.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)kernel/trace/trace_syscalls.c +0
-0
kernel/trace/trace_syscalls.c static int perf_call_bpf_enter(struct trace_event_call *call, struct pt_regs *re · line 561
Quoted 8 lines
kernel/trace/trace_syscalls.c +1
-1@@ -555,12 +555,15 @@ static int perf_call_bpf_enter(struct trace_event_call *call, struct pt_regs *re struct syscall_trace_enter *rec) {struct syscall_tp_t { -unsigned long long regs; +struct trace_entry ent;unsigned long syscall_nr;unsigned long args[SYSCALL_DEFINE_MAXARGS ]; } param;
I suspect we may have issues for 32bit kernel. In 32bit kernel, with the change, the alignment for param could be 4. That means, the 'ctx' pointer may have an alignment 4 for bpf program, if user tries to do ctx->regs, which will be a mis-aligned access and it may not work for all architectures.
Quoted 9 lines
kernel/trace/trace_syscalls.c +3
-0int i; +BUILD_BUG_ON (sizeof (param.ent) <sizeof (void *)); + +/* __bpf_prog_run() requires *regs as the first parameter */ *(struct pt_regs **)¶m = regs; param.syscall_nr = rec->nr;for (i =0 ; i < sys_data->nb_args; i++)
kernel/trace/trace_syscalls.c static int perf_call_bpf_exit(struct trace_event_call *call, struct pt_regs *reg · line 668
Quoted 10 lines
kernel/trace/trace_syscalls.c +2
-1@@ -657,11 +660,14 @@ static int perf_call_bpf_exit(struct trace_event_call *call, struct pt_regs *reg struct syscall_trace_exit *rec) {struct syscall_tp_t { -unsigned long long regs; +struct trace_entry ent;unsigned long syscall_nr;unsigned long ret; } param; +BUILD_BUG_ON (sizeof (param.ent) <sizeof (void *));
You already have BUILD_BUG_ON in perf_call_enter. There is no need to have another one here.
Quoted 5 lines
kernel/trace/trace_syscalls.c +2
-0+ +/* __bpf_prog_run() requires *regs as the first parameter */ *(struct pt_regs **)¶m = regs; param.syscall_nr = rec->nr; param.ret = rec->ret;
Hi, Yonghong!
Quoted 9 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line · Yonghong Song
Quoted 4 lines · Yauheni Kaliuta
bpf tracepoint program uses struct
trace_event_raw_sys_enteras argument wheretrace_entryis the first field. Use the same instead of unsigned long long since if it's amended (for example by RT patch) it accesses data with wrong offset.Is this 'amended by RT patch' a real thing?
Yes for me.
Quoted 17 lines
Quoted 17 lines
Signed-off-by Yauheni Kaliuta <[email protected]>---
kernel/trace/trace_syscalls.c| 10 ++++++++--1 file changed, 8 insertions(+), 2 deletions(-)kernel/trace/trace_syscalls.c +1
-1@@ -555,12 +555,15 @@ static int perf_call_bpf_enter(struct trace_event_call *call, struct pt_regs *re struct syscall_trace_enter *rec) {struct syscall_tp_t { -unsigned long long regs; +struct trace_entry ent;unsigned long syscall_nr;unsigned long args[SYSCALL_DEFINE_MAXARGS ];
Quoted 8 lines
kernel/trace/trace_syscalls.c static int perf_call_bpf_enter(struct trace_event_call *call, struct pt_regs *re · line 561
Quoted 1 line
kernel/trace/trace_syscalls.c +0
-0} param;I suspect we may have issues for 32bit kernel. In 32bit kernel, with the change, the alignment for param could be 4. That means, the 'ctx' pointer may have an alignment 4 for bpf program, if user tries to do
ctx->regs, which will be a mis-aligned access and it may not work for all architectures.
well, will __aligned(8) save the world?
Quoted 12 lines
Quoted 12 lines
kernel/trace/trace_syscalls.c +3
-1int i; +BUILD_BUG_ON (sizeof (param.ent) <sizeof (void *)); + +/* __bpf_prog_run() requires *regs as the first parameter */ *(struct pt_regs **)¶m = regs; param.syscall_nr = rec->nr;for (i =0 ; i < sys_data->nb_args; i++)@@ -657,11 +660,14 @@ static int perf_call_bpf_exit(struct trace_event_call *call, struct pt_regs *reg struct syscall_trace_exit *rec) { struct syscall_tp_t { -unsigned long long regs;
Quoted 8 lines
kernel/trace/trace_syscalls.c static int perf_call_bpf_exit(struct trace_event_call *call, struct pt_regs *reg · line 668
Quoted 5 lines
kernel/trace/trace_syscalls.c +2
-0+struct trace_entry ent;unsigned long syscall_nr;unsigned long ret; } param; +BUILD_BUG_ON (sizeof (param.ent) <sizeof (void *));You already have
BUILD_BUG_ONinperf_call_enter. There is no need to have another one here.
Oh yes, thanks :)
Quoted 5 lines
Quoted 5 lines
kernel/trace/trace_syscalls.c +2
-0+ +/* __bpf_prog_run() requires *regs as the first parameter */ *(struct pt_regs **)¶m = regs; param.syscall_nr = rec->nr; param.ret = rec->ret;
Signature · 2 lines
WBR, Yauheni Kaliuta
PATCH v2 1/1 tracing: perf_call_bpf: use struct trace_entry in struct syscall_tp_t
In reply to Yonghong Songbpf tracepoint program uses struct trace_event_raw_sys_enter as argument where trace_entry is the first field. Use the same instead of unsigned long long since if it's amended (for example by RT patch) it accesses data with wrong offset.
---
v2:
- remove extra BUILD_BUG_ON
- add structure alignement
--- kernel/trace/trace_syscalls.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-)
kernel/trace/trace_syscalls.c +8 -4
@@ -555,12 +555,15 @@ static int perf_call_bpf_enter(struct trace_event_call *call, struct pt_regs *re struct syscall_trace_enter *rec) {struct syscall_tp_t { -unsigned long long regs; +struct trace_entry ent;unsigned long syscall_nr;unsigned long args[SYSCALL_DEFINE_MAXARGS ]; - } param; + }__aligned(8) param;int i; +BUILD_BUG_ON (sizeof (param.ent) <sizeof (void *)); + +/* __bpf_prog_run() requires *regs as the first parameter */ *(struct pt_regs **)¶m = regs; param.syscall_nr = rec->nr;for (i =0 ; i < sys_data->nb_args; i++)
@@ -657,11 +660,12 @@ static int perf_call_bpf_exit(struct trace_event_call *call, struct pt_regs *reg struct syscall_trace_exit *rec) {struct syscall_tp_t { -unsigned long long regs; +struct trace_entry ent;unsigned long syscall_nr;unsigned long ret; - } param; + }__aligned(8) param; +/* __bpf_prog_run() requires *regs as the first parameter */ *(struct pt_regs **)¶m = regs; param.syscall_nr = rec->nr; param.ret = rec->ret;
Signature · 1 line
2.41.0
Yonghong Song
reviewerQuoted 22 lines · Yauheni Kaliuta
bpf tracepoint program uses struct
trace_event_raw_sys_enteras argument wheretrace_entryis the first field. Use the same instead of unsigned long long since if it's amended (for example by RT patch) it accesses data with wrong offset.Signed-off-by Yauheni Kaliuta <[email protected]>---
v2:
- remove extraBUILD_BUG_ON
- add structure alignement--- kernel/trace/trace_syscalls.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-)kernel/trace/trace_syscalls.c +0
-0@@ -555,12 +555,15 @@ static int perf_call_bpf_enter(struct trace_event_call *call, struct pt_regs *re struct syscall_trace_enter *rec) {
kernel/trace/trace_syscalls.c static int perf_call_bpf_enter(struct trace_event_call *call, struct pt_regs *re · line 564
Quoted 10 lines
kernel/trace/trace_syscalls.c +3
-2struct syscall_tp_t { -unsigned long long regs; +struct trace_entry ent;unsigned long syscall_nr;unsigned long args[SYSCALL_DEFINE_MAXARGS ]; - } param; + }__aligned(8) param;int i; +BUILD_BUG_ON (sizeof (param.ent) <sizeof (void *));
Considering we used 'unsigned long long regs' before, should the above BUILD_BUG_ON should be
BUILD_BUG_ON(sizeof(param.ent) < sizeof(long long));
?
kernel/trace/trace_syscalls.c static int perf_call_bpf_enter(struct trace_event_call *call, struct pt_regs *re · line 566
Quoted 2 lines
kernel/trace/trace_syscalls.c +2
-0+ +/* __bpf_prog_run() requires *regs as the first parameter */
This comment is not correct.
static __always_inline u32 __bpf_prog_run(const struct bpf_prog *prog,
const void *ctx, bpf_dispatcher_fn dfunc)
{
...
}The first parameter is 'prog'.
Also there is no __bpf_prog_run() referenced in this function so this comment may confuse readers. So I suggest removing this comment. The same for perf_call_bpf_exit() below.
Quoted 18 lines
kernel/trace/trace_syscalls.c +3
-2*(struct pt_regs **)¶m = regs; param.syscall_nr = rec->nr;for (i =0 ; i < sys_data->nb_args; i++)@@ -657,11 +660,12 @@ static int perf_call_bpf_exit(struct trace_event_call *call, struct pt_regs *reg struct syscall_trace_exit *rec) {struct syscall_tp_t { -unsigned long long regs; +struct trace_entry ent;unsigned long syscall_nr;unsigned long ret; - } param; + }__aligned(8) param; +/* __bpf_prog_run() requires *regs as the first parameter */ *(struct pt_regs **)¶m = regs; param.syscall_nr = rec->nr; param.ret = rec->ret;
Hi, Yonghong!
Quoted 35 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line · Yonghong Song
Quoted 19 lines · Yauheni Kaliuta
bpf tracepoint program uses struct
trace_event_raw_sys_enteras argument wheretrace_entryis the first field. Use the same instead of unsigned long long since if it's amended (for example by RT patch) it accesses data with wrong offset.Signed-off-by Yauheni Kaliuta <[email protected]>---
v2:
- remove extraBUILD_BUG_ON
- add structure alignement
---kernel/trace/trace_syscalls.c| 12 ++++++++----1 file changed, 8 insertions(+), 4 deletions(-)kernel/trace/trace_syscalls.c +0
-0@@ -555,12 +555,15 @@ static int perf_call_bpf_enter(struct trace_event_call *call, struct pt_regs *re struct syscall_trace_enter *rec)kernel/trace/trace_syscalls.c static int perf_call_bpf_enter(struct trace_event_call *call, struct pt_regs *re · line 563
Quoted 10 lines
kernel/trace/trace_syscalls.c +3
-2{struct syscall_tp_t { -unsigned long long regs; +struct trace_entry ent;unsigned long syscall_nr;unsigned long args[SYSCALL_DEFINE_MAXARGS ]; - } param; + }__aligned(8) param;int i; +BUILD_BUG_ON (sizeof (param.ent) <sizeof (void *));Considering we used 'unsigned long long regs' before, should the above
BUILD_BUG_ONshould be
Quoted 2 lines
BUILD_BUG_ON(sizeof(param.ent) < sizeof(long long));?
Since the pointer's value is assigned I agree with Alexei (in the thread [1]) to use void *.
Quoted 12 lines
kernel/trace/trace_syscalls.c static int perf_call_bpf_enter(struct trace_event_call *call, struct pt_regs *re · line 566
Quoted 2 lines
kernel/trace/trace_syscalls.c +2
-0+ +/* __bpf_prog_run() requires *regs as the first parameter */ This comment is not correct.
static
__always_inlineu32 __bpf_prog_run(const structbpf_prog*prog,
const void *ctx,bpf_dispatcher_fndfunc){ ... }
Quoted 5 lines
The first parameter is 'prog'.
Also there is no
__bpf_prog_run()referenced in this function so this comment may confuse readers. So I suggest removing this comment. The same forperf_call_bpf_exit()below.
Again, in [1] we agreed that it's better to have the comment since it's even more confusing.
Could you help to formulate it?
"__bpf_prog_run() requires *regs as the first argument for bpf prog" or something?
But yes, I can remove it of course.
Quoted 9 lines
Quoted 9 lines
kernel/trace/trace_syscalls.c +1
-1*(struct pt_regs **)¶m = regs; param.syscall_nr = rec->nr;for (i =0 ; i < sys_data->nb_args; i++)@@ -657,11 +660,12 @@ static int perf_call_bpf_exit(struct trace_event_call *call, struct pt_regs *reg struct syscall_trace_exit *rec) {struct syscall_tp_t { -unsigned long long regs; +struct trace_entry ent;
Quoted 8 lines
kernel/trace/trace_syscalls.c static int perf_call_bpf_exit(struct trace_event_call *call, struct pt_regs *reg · line 671
Quoted 8 lines
kernel/trace/trace_syscalls.c +2
-1unsigned long syscall_nr;unsigned long ret; - } param; + }__aligned(8) param; +/* __bpf_prog_run() requires *regs as the first parameter */ *(struct pt_regs **)¶m = regs; param.syscall_nr = rec->nr; param.ret = rec->ret;
[1] https://lore.kernel.org/bpf/[email protected]/T/#u
Signature · 2 lines
WBR, Yauheni Kaliuta
Yonghong Song
reviewerQuoted 40 lines · Yauheni Kaliuta
Hi, Yonghong!
Quoted 35 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line · Yonghong Song
Quoted 19 lines · Yauheni Kaliuta
bpf tracepoint program uses struct
trace_event_raw_sys_enteras argument wheretrace_entryis the first field. Use the same instead of unsigned long long since if it's amended (for example by RT patch) it accesses data with wrong offset.Signed-off-by Yauheni Kaliuta <[email protected]>---
v2:
- remove extraBUILD_BUG_ON
- add structure alignement
---kernel/trace/trace_syscalls.c| 12 ++++++++----1 file changed, 8 insertions(+), 4 deletions(-)kernel/trace/trace_syscalls.c +0
-0@@ -555,12 +555,15 @@ static int perf_call_bpf_enter(struct trace_event_call *call, struct pt_regs *re struct syscall_trace_enter *rec)kernel/trace/trace_syscalls.c static int perf_call_bpf_enter(struct trace_event_call *call, struct pt_regs *re · line 563
Quoted 10 lines
kernel/trace/trace_syscalls.c +3
-2{struct syscall_tp_t { -unsigned long long regs; +struct trace_entry ent;unsigned long syscall_nr;unsigned long args[SYSCALL_DEFINE_MAXARGS ]; - } param; + }__aligned(8) param;int i; +BUILD_BUG_ON (sizeof (param.ent) <sizeof (void *));Considering we used 'unsigned long long regs' before, should the above
BUILD_BUG_ONshould beQuoted 2 lines
BUILD_BUG_ON(sizeof(param.ent) < sizeof(long long));?
Quoted 2 lines
Since the pointer's value is assigned I agree with Alexei (in the thread [1]) to use void *.
Okay, let us compare to sizeof(void *) then.
Quoted 22 lines
Quoted 13 lines
kernel/trace/trace_syscalls.c static int perf_call_bpf_enter(struct trace_event_call *call, struct pt_regs *re · line 566
Quoted 3 lines
kernel/trace/trace_syscalls.c +2
-0+ +/* __bpf_prog_run() requires *regs as the first parameter */ This comment is not correct.
static
__always_inlineu32 __bpf_prog_run(const structbpf_prog*prog,
const void *ctx,bpf_dispatcher_fndfunc){ ... }Quoted 5 lines
The first parameter is 'prog'.
Also there is no
__bpf_prog_run()referenced in this function so this comment may confuse readers. So I suggest removing this comment. The same forperf_call_bpf_exit()below.Again, in [1] we agreed that it's better to have the comment since it's even more confusing.
Quoted 6 lines
Could you help to formulate it?
"
__bpf_prog_run()requires *regs as the first argument for bpf prog" or something?But yes, I can remove it of course.
You could have a comment like below:
/* bpf prog requires 'regs' to be the first member in the ctx (a.k.a.
¶m) */
Quoted 22 lines
Quoted 10 lines
Quoted 10 lines
kernel/trace/trace_syscalls.c +1
-1*(struct pt_regs **)¶m = regs; param.syscall_nr = rec->nr;for (i =0 ; i < sys_data->nb_args; i++)@@ -657,11 +660,12 @@ static int perf_call_bpf_exit(struct trace_event_call *call, struct pt_regs *reg struct syscall_trace_exit *rec) {struct syscall_tp_t { -unsigned long long regs; +struct trace_entry ent;Quoted 8 lines
kernel/trace/trace_syscalls.c static int perf_call_bpf_exit(struct trace_event_call *call, struct pt_regs *reg · line 671
Quoted 8 lines
kernel/trace/trace_syscalls.c +2
-1unsigned long syscall_nr;unsigned long ret; - } param; + }__aligned(8) param; +/* __bpf_prog_run() requires *regs as the first parameter */ *(struct pt_regs **)¶m = regs; param.syscall_nr = rec->nr; param.ret = rec->ret;
Hi, Yonghong!
Quoted 24 lines
Quoted 17 lines
Quoted 7 lines
kernel/trace/trace_syscalls.c static int perf_call_bpf_enter(struct trace_event_call *call, struct pt_regs *re · line 566
Quoted 5 lines
Quoted 1 line · Yonghong Song
kernel/trace/trace_syscalls.c +2
-0+ +/* __bpf_prog_run() requires *regs as the first parameter */ This comment is not correct.
static__always_inlineu32 __bpf_prog_run(const structbpf_prog*prog,
Quoted 9 lines
const void *ctx,
bpf_dispatcher_fndfunc){ ... }The first parameter is 'prog'.
Also there is no__bpf_prog_run()referenced in this function so this comment may confuse readers. So I suggest removing this comment. The same forperf_call_bpf_exit()below.Quoted 6 lines
Again, in [1] we agreed that it's better to have the comment since it's even more confusing.
Could you help to formulate it?
"__bpf_prog_run()requires *regs as the first argument for bpf prog" or something?
But yes, I can remove it of course.
Quoted 3 lines
You could have a comment like below:
/* bpf prog requires 'regs' to be the first member in the ctx (a.k.a. ¶m) */
Thanks!
Signature · 2 lines
WBR, Yauheni Kaliuta
PATCH v3 1/1 tracing: perf_call_bpf: use struct trace_entry in struct syscall_tp_t
In reply to Yauheni Kaliutabpf tracepoint program uses struct trace_event_raw_sys_enter as argument where trace_entry is the first field. Use the same instead of unsigned long long since if it's amended (for example by RT patch) it accesses data with wrong offset.
---
v3:
- Fixed comment
v2:
- remove extra BUILD_BUG_ON
- add structure alignement
--- kernel/trace/trace_syscalls.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-)
kernel/trace/trace_syscalls.c +8 -4
@@ -555,12 +555,15 @@ static int perf_call_bpf_enter(struct trace_event_call *call, struct pt_regs *re struct syscall_trace_enter *rec) {struct syscall_tp_t { -unsigned long long regs; +struct trace_entry ent;unsigned long syscall_nr;unsigned long args[SYSCALL_DEFINE_MAXARGS ]; - } param; + }__aligned(8) param;int i; +BUILD_BUG_ON (sizeof (param.ent) <sizeof (void *)); + +/* bpf prog requires 'regs' to be the first member in the ctx (a.k.a. ¶m) */ *(struct pt_regs **)¶m = regs; param.syscall_nr = rec->nr;for (i =0 ; i < sys_data->nb_args; i++)
@@ -657,11 +660,12 @@ static int perf_call_bpf_exit(struct trace_event_call *call, struct pt_regs *reg struct syscall_trace_exit *rec) {struct syscall_tp_t { -unsigned long long regs; +struct trace_entry ent;unsigned long syscall_nr;unsigned long ret; - } param; + }__aligned(8) param; +/* bpf prog requires 'regs' to be the first member in the ctx (a.k.a. ¶m) */ *(struct pt_regs **)¶m = regs; param.syscall_nr = rec->nr; param.ret = rec->ret;
Signature · 1 line
2.41.0
Quoted 6 lines · Yauheni Kaliuta
bpf tracepoint program uses struct
trace_event_raw_sys_enteras argument wheretrace_entryis the first field. Use the same instead of unsigned long long since if it's amended (for example by RT patch) it accesses data with wrong offset.Signed-off-by Yauheni Kaliuta <[email protected]>
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 8 lines · On Tue, 1 Aug 2023 10:52:22 +0300 you wrote:
bpf tracepoint program uses struct
trace_event_raw_sys_enteras argument wheretrace_entryis the first field. Use the same instead of unsigned long long since if it's amended (for example by RT patch) it accesses data with wrong offset.Signed-off-by Yauheni Kaliuta <[email protected]>[...]
Here is the summary with links:
- [bpf-next,v3] tracing: perf_call_bpf: use struct trace_entry in struct syscall_tp_t
https://git.kernel.org/bpf/bpf-next/c/d3c4db86c711
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html