Skip to content

Commit d814ed6

Browse files
anakryikoAlexei Starovoitov
authored andcommitted
selftests/bpf: use BPF_KSYSCALL and SEC("ksyscall") in selftests
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>
1 parent 708ac5b commit d814ed6

File tree

3 files changed

+16
-32
lines changed

3 files changed

+16
-32
lines changed

tools/testing/selftests/bpf/progs/bpf_syscall_macro.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ int BPF_KPROBE(handle_sys_prctl)
6464
return 0;
6565
}
6666

67-
SEC("kprobe/" SYS_PREFIX "sys_prctl")
68-
int BPF_KPROBE_SYSCALL(prctl_enter, int option, unsigned long arg2,
69-
unsigned long arg3, unsigned long arg4, unsigned long arg5)
67+
SEC("ksyscall/prctl")
68+
int BPF_KSYSCALL(prctl_enter, int option, unsigned long arg2,
69+
unsigned long arg3, unsigned long arg4, unsigned long arg5)
7070
{
7171
pid_t pid = bpf_get_current_pid_tgid() >> 32;
7272

tools/testing/selftests/bpf/progs/test_attach_probe.c

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
// SPDX-License-Identifier: GPL-2.0
22
// Copyright (c) 2017 Facebook
33

4-
#include <linux/ptrace.h>
5-
#include <linux/bpf.h>
4+
#include "vmlinux.h"
65
#include <bpf/bpf_helpers.h>
76
#include <bpf/bpf_tracing.h>
8-
#include <stdbool.h>
7+
#include <bpf/bpf_core_read.h>
98
#include "bpf_misc.h"
109

1110
int kprobe_res = 0;
@@ -31,8 +30,8 @@ int handle_kprobe(struct pt_regs *ctx)
3130
return 0;
3231
}
3332

34-
SEC("kprobe/" SYS_PREFIX "sys_nanosleep")
35-
int BPF_KPROBE(handle_kprobe_auto)
33+
SEC("ksyscall/nanosleep")
34+
int BPF_KSYSCALL(handle_kprobe_auto, struct __kernel_timespec *req, struct __kernel_timespec *rem)
3635
{
3736
kprobe2_res = 11;
3837
return 0;
@@ -56,11 +55,11 @@ int handle_kretprobe(struct pt_regs *ctx)
5655
return 0;
5756
}
5857

59-
SEC("kretprobe/" SYS_PREFIX "sys_nanosleep")
60-
int BPF_KRETPROBE(handle_kretprobe_auto)
58+
SEC("kretsyscall/nanosleep")
59+
int BPF_KRETPROBE(handle_kretprobe_auto, int ret)
6160
{
6261
kretprobe2_res = 22;
63-
return 0;
62+
return ret;
6463
}
6564

6665
SEC("uprobe")

tools/testing/selftests/bpf/progs/test_probe_user.c

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,20 @@
11
// SPDX-License-Identifier: GPL-2.0
2-
3-
#include <linux/ptrace.h>
4-
#include <linux/bpf.h>
5-
6-
#include <netinet/in.h>
7-
2+
#include "vmlinux.h"
83
#include <bpf/bpf_helpers.h>
94
#include <bpf/bpf_tracing.h>
5+
#include <bpf/bpf_core_read.h>
106
#include "bpf_misc.h"
117

128
static struct sockaddr_in old;
139

14-
SEC("kprobe/" SYS_PREFIX "sys_connect")
15-
int BPF_KPROBE(handle_sys_connect)
10+
SEC("ksyscall/connect")
11+
int BPF_KSYSCALL(handle_sys_connect, int fd, struct sockaddr_in *uservaddr, int addrlen)
1612
{
17-
#if SYSCALL_WRAPPER == 1
18-
struct pt_regs *real_regs;
19-
#endif
2013
struct sockaddr_in new;
21-
void *ptr;
22-
23-
#if SYSCALL_WRAPPER == 0
24-
ptr = (void *)PT_REGS_PARM2(ctx);
25-
#else
26-
real_regs = (struct pt_regs *)PT_REGS_PARM1(ctx);
27-
bpf_probe_read_kernel(&ptr, sizeof(ptr), &PT_REGS_PARM2(real_regs));
28-
#endif
2914

30-
bpf_probe_read_user(&old, sizeof(old), ptr);
15+
bpf_probe_read_user(&old, sizeof(old), uservaddr);
3116
__builtin_memset(&new, 0xab, sizeof(new));
32-
bpf_probe_write_user(ptr, &new, sizeof(new));
17+
bpf_probe_write_user(uservaddr, &new, sizeof(new));
3318

3419
return 0;
3520
}

0 commit comments

Comments
 (0)