selftests/bpf: use BPF_KSYSCALL and SEC("ksyscall") in selftests
authorAndrii Nakryiko <andrii@kernel.org>
Thu, 14 Jul 2022 07:07:55 +0000 (00:07 -0700)
committerAlexei Starovoitov <ast@kernel.org>
Tue, 19 Jul 2022 16:33:18 +0000 (09:33 -0700)
Convert few selftest that used plain SEC("kprobe") with arch-specific
syscall wrapper prefix to ksyscall/kretsyscall and corresponding
BPF_KSYSCALL macro. test_probe_user.c is especially benefiting from this
simplification.

Tested-by: Alan Maguire <alan.maguire@oracle.com>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/r/20220714070755.3235561-6-andrii@kernel.org
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
tools/testing/selftests/bpf/progs/bpf_syscall_macro.c
tools/testing/selftests/bpf/progs/test_attach_probe.c
tools/testing/selftests/bpf/progs/test_probe_user.c

index 05838ed..e1e1189 100644 (file)
@@ -64,9 +64,9 @@ int BPF_KPROBE(handle_sys_prctl)
        return 0;
 }
 
-SEC("kprobe/" SYS_PREFIX "sys_prctl")
-int BPF_KPROBE_SYSCALL(prctl_enter, int option, unsigned long arg2,
-                      unsigned long arg3, unsigned long arg4, unsigned long arg5)
+SEC("ksyscall/prctl")
+int BPF_KSYSCALL(prctl_enter, int option, unsigned long arg2,
+                unsigned long arg3, unsigned long arg4, unsigned long arg5)
 {
        pid_t pid = bpf_get_current_pid_tgid() >> 32;
 
index f1c88ad..a1e45fe 100644 (file)
@@ -1,11 +1,10 @@
 // SPDX-License-Identifier: GPL-2.0
 // Copyright (c) 2017 Facebook
 
-#include <linux/ptrace.h>
-#include <linux/bpf.h>
+#include "vmlinux.h"
 #include <bpf/bpf_helpers.h>
 #include <bpf/bpf_tracing.h>
-#include <stdbool.h>
+#include <bpf/bpf_core_read.h>
 #include "bpf_misc.h"
 
 int kprobe_res = 0;
@@ -31,8 +30,8 @@ int handle_kprobe(struct pt_regs *ctx)
        return 0;
 }
 
-SEC("kprobe/" SYS_PREFIX "sys_nanosleep")
-int BPF_KPROBE(handle_kprobe_auto)
+SEC("ksyscall/nanosleep")
+int BPF_KSYSCALL(handle_kprobe_auto, struct __kernel_timespec *req, struct __kernel_timespec *rem)
 {
        kprobe2_res = 11;
        return 0;
@@ -56,11 +55,11 @@ int handle_kretprobe(struct pt_regs *ctx)
        return 0;
 }
 
-SEC("kretprobe/" SYS_PREFIX "sys_nanosleep")
-int BPF_KRETPROBE(handle_kretprobe_auto)
+SEC("kretsyscall/nanosleep")
+int BPF_KRETPROBE(handle_kretprobe_auto, int ret)
 {
        kretprobe2_res = 22;
-       return 0;
+       return ret;
 }
 
 SEC("uprobe")
index 702578a..8e14950 100644 (file)
@@ -1,35 +1,20 @@
 // SPDX-License-Identifier: GPL-2.0
-
-#include <linux/ptrace.h>
-#include <linux/bpf.h>
-
-#include <netinet/in.h>
-
+#include "vmlinux.h"
 #include <bpf/bpf_helpers.h>
 #include <bpf/bpf_tracing.h>
+#include <bpf/bpf_core_read.h>
 #include "bpf_misc.h"
 
 static struct sockaddr_in old;
 
-SEC("kprobe/" SYS_PREFIX "sys_connect")
-int BPF_KPROBE(handle_sys_connect)
+SEC("ksyscall/connect")
+int BPF_KSYSCALL(handle_sys_connect, int fd, struct sockaddr_in *uservaddr, int addrlen)
 {
-#if SYSCALL_WRAPPER == 1
-       struct pt_regs *real_regs;
-#endif
        struct sockaddr_in new;
-       void *ptr;
-
-#if SYSCALL_WRAPPER == 0
-       ptr = (void *)PT_REGS_PARM2(ctx);
-#else
-       real_regs = (struct pt_regs *)PT_REGS_PARM1(ctx);
-       bpf_probe_read_kernel(&ptr, sizeof(ptr), &PT_REGS_PARM2(real_regs));
-#endif
 
-       bpf_probe_read_user(&old, sizeof(old), ptr);
+       bpf_probe_read_user(&old, sizeof(old), uservaddr);
        __builtin_memset(&new, 0xab, sizeof(new));
-       bpf_probe_write_user(ptr, &new, sizeof(new));
+       bpf_probe_write_user(uservaddr, &new, sizeof(new));
 
        return 0;
 }