diff mbox series

samples: bpf: fix broken behavior of tracex2 write_size count

Message ID 20221202162907.26721-1-danieltimlee@gmail.com (mailing list archive)
State Changes Requested
Delegated to: BPF
Headers show
Series samples: bpf: fix broken behavior of tracex2 write_size count | expand

Checks

Context Check Description
netdev/tree_selection success Not a local patch
bpf/vmtest-bpf-next-VM_Test-36 success Logs for test_verifier on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-21 success Logs for test_progs_no_alu32 on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-26 success Logs for test_progs_no_alu32_parallel on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-31 success Logs for test_progs_parallel on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-16 fail Logs for test_progs on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-11 success Logs for test_maps on s390x with gcc
bpf/vmtest-bpf-next-PR success PR summary
bpf/vmtest-bpf-next-VM_Test-6 success Logs for build for x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-2 success Logs for build for aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-3 success Logs for build for aarch64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-5 success Logs for build for x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-4 success Logs for build for s390x with gcc
bpf/vmtest-bpf-next-VM_Test-12 success Logs for test_maps on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-13 success Logs for test_maps on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-17 success Logs for test_progs on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-18 success Logs for test_progs on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-22 success Logs for test_progs_no_alu32 on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-23 success Logs for test_progs_no_alu32 on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-27 success Logs for test_progs_no_alu32_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-28 success Logs for test_progs_no_alu32_parallel on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-32 success Logs for test_progs_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-33 success Logs for test_progs_parallel on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-37 success Logs for test_verifier on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-38 success Logs for test_verifier on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-9 success Logs for test_maps on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-10 success Logs for test_maps on aarch64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-14 success Logs for test_progs on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-15 fail Logs for test_progs on aarch64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-19 success Logs for test_progs_no_alu32 on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-20 fail Logs for test_progs_no_alu32 on aarch64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-24 success Logs for test_progs_no_alu32_parallel on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-25 success Logs for test_progs_no_alu32_parallel on aarch64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-29 success Logs for test_progs_parallel on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-30 success Logs for test_progs_parallel on aarch64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-34 success Logs for test_verifier on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-35 success Logs for test_verifier on aarch64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-1 success Logs for ShellCheck
bpf/vmtest-bpf-next-VM_Test-7 success Logs for llvm-toolchain
bpf/vmtest-bpf-next-VM_Test-8 success Logs for set-matrix

Commit Message

Daniel T. Lee Dec. 2, 2022, 4:29 p.m. UTC
Currently, there is a problem with tracex2, as it doesn't print the
histogram properly and the results are misleading. (all results report
as 0)

The problem is caused by a change in arguments of the function to which
the kprobe connects. This tracex2 bpf program uses kprobe (attached
to __x64_sys_write) to figure out the size of the write system call. In
order to achieve this, the third argument 'count' must be intact.

The following is a prototype of the sys_write variant. (checked with
pfunct)

    ~/git/linux$ pfunct -P fs/read_write.o | grep sys_write
    ssize_t ksys_write(unsigned int fd, const char  * buf, size_t count);
    long int __x64_sys_write(const struct pt_regs  * regs);
    ... cross compile with s390x ...
    long int __s390_sys_write(struct pt_regs * regs);

Since the __x64_sys_write (or s390x also) doesn't have the proper
argument, changing the kprobe event to ksys_write will fix the problem.

Signed-off-by: Daniel T. Lee <danieltimlee@gmail.com>
---
 samples/bpf/tracex2_kern.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Comments

Andrii Nakryiko Dec. 3, 2022, 12:34 a.m. UTC | #1
On Fri, Dec 2, 2022 at 8:29 AM Daniel T. Lee <danieltimlee@gmail.com> wrote:
>
> Currently, there is a problem with tracex2, as it doesn't print the
> histogram properly and the results are misleading. (all results report
> as 0)
>
> The problem is caused by a change in arguments of the function to which
> the kprobe connects. This tracex2 bpf program uses kprobe (attached
> to __x64_sys_write) to figure out the size of the write system call. In
> order to achieve this, the third argument 'count' must be intact.
>
> The following is a prototype of the sys_write variant. (checked with
> pfunct)
>
>     ~/git/linux$ pfunct -P fs/read_write.o | grep sys_write
>     ssize_t ksys_write(unsigned int fd, const char  * buf, size_t count);
>     long int __x64_sys_write(const struct pt_regs  * regs);
>     ... cross compile with s390x ...
>     long int __s390_sys_write(struct pt_regs * regs);
>
> Since the __x64_sys_write (or s390x also) doesn't have the proper
> argument, changing the kprobe event to ksys_write will fix the problem.
>
> Signed-off-by: Daniel T. Lee <danieltimlee@gmail.com>
> ---
>  samples/bpf/tracex2_kern.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/samples/bpf/tracex2_kern.c b/samples/bpf/tracex2_kern.c
> index 93e0b7680b4f..fc65c589e87f 100644
> --- a/samples/bpf/tracex2_kern.c
> +++ b/samples/bpf/tracex2_kern.c
> @@ -78,7 +78,7 @@ struct {
>         __uint(max_entries, 1024);
>  } my_hist_map SEC(".maps");
>
> -SEC("kprobe/" SYSCALL(sys_write))
> +SEC("kprobe/ksys_write")
>  int bpf_prog3(struct pt_regs *ctx)
>  {
>         long write_size = PT_REGS_PARM3(ctx);


use

SEC("ksyscall/write")
int BPF_KSYSCALL(bpf_prog3, unsigned int fd, const char *buf, size_t count)

instead?

And maybe let's update other samples to use SEC("ksyscall") and
BPF_KSYSCALL() macro as well?


> --
> 2.34.1
>
Daniel T. Lee Dec. 3, 2022, 9:11 a.m. UTC | #2
On Sat, Dec 3, 2022 at 9:34 AM Andrii Nakryiko
<andrii.nakryiko@gmail.com> wrote:
>
> On Fri, Dec 2, 2022 at 8:29 AM Daniel T. Lee <danieltimlee@gmail.com> wrote:
> >
> > Currently, there is a problem with tracex2, as it doesn't print the
> > histogram properly and the results are misleading. (all results report
> > as 0)
> >
> > The problem is caused by a change in arguments of the function to which
> > the kprobe connects. This tracex2 bpf program uses kprobe (attached
> > to __x64_sys_write) to figure out the size of the write system call. In
> > order to achieve this, the third argument 'count' must be intact.
> >
> > The following is a prototype of the sys_write variant. (checked with
> > pfunct)
> >
> >     ~/git/linux$ pfunct -P fs/read_write.o | grep sys_write
> >     ssize_t ksys_write(unsigned int fd, const char  * buf, size_t count);
> >     long int __x64_sys_write(const struct pt_regs  * regs);
> >     ... cross compile with s390x ...
> >     long int __s390_sys_write(struct pt_regs * regs);
> >
> > Since the __x64_sys_write (or s390x also) doesn't have the proper
> > argument, changing the kprobe event to ksys_write will fix the problem.
> >
> > Signed-off-by: Daniel T. Lee <danieltimlee@gmail.com>
> > ---
> >  samples/bpf/tracex2_kern.c | 2 +-
> >  1 file changed, 1 insertion(+), 1 deletion(-)
> >
> > diff --git a/samples/bpf/tracex2_kern.c b/samples/bpf/tracex2_kern.c
> > index 93e0b7680b4f..fc65c589e87f 100644
> > --- a/samples/bpf/tracex2_kern.c
> > +++ b/samples/bpf/tracex2_kern.c
> > @@ -78,7 +78,7 @@ struct {
> >         __uint(max_entries, 1024);
> >  } my_hist_map SEC(".maps");
> >
> > -SEC("kprobe/" SYSCALL(sys_write))
> > +SEC("kprobe/ksys_write")
> >  int bpf_prog3(struct pt_regs *ctx)
> >  {
> >         long write_size = PT_REGS_PARM3(ctx);
>
>
> use
>
> SEC("ksyscall/write")
> int BPF_KSYSCALL(bpf_prog3, unsigned int fd, const char *buf, size_t count)
>
> instead?
>
> And maybe let's update other samples to use SEC("ksyscall") and
> BPF_KSYSCALL() macro as well?
>
>

Thanks for the review!

I'll check with the new BPF_KSYSCALL and try to fix others as well!


> > --
> > 2.34.1
> >
diff mbox series

Patch

diff --git a/samples/bpf/tracex2_kern.c b/samples/bpf/tracex2_kern.c
index 93e0b7680b4f..fc65c589e87f 100644
--- a/samples/bpf/tracex2_kern.c
+++ b/samples/bpf/tracex2_kern.c
@@ -78,7 +78,7 @@  struct {
 	__uint(max_entries, 1024);
 } my_hist_map SEC(".maps");
 
-SEC("kprobe/" SYSCALL(sys_write))
+SEC("kprobe/ksys_write")
 int bpf_prog3(struct pt_regs *ctx)
 {
 	long write_size = PT_REGS_PARM3(ctx);