Skip to content

Commit 3ac4dbe

Browse files
anakryikoborkmann
authored andcommitted
selftests/bpf: Split off tracing-only helpers into bpf_tracing.h
Split-off PT_REGS-related helpers into bpf_tracing.h header. Adjust selftests and samples to include it where necessary. Signed-off-by: Andrii Nakryiko <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]> Acked-by: John Fastabend <[email protected]> Acked-by: Song Liu <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 694731e commit 3ac4dbe

18 files changed

+211
-190
lines changed

samples/bpf/map_perf_test_kern.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <uapi/linux/bpf.h>
1111
#include "bpf_helpers.h"
1212
#include "bpf_legacy.h"
13+
#include "bpf_tracing.h"
1314

1415
#define MAX_ENTRIES 1000
1516
#define MAX_NR_CPUS 1024

samples/bpf/offwaketime_kern.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*/
77
#include <uapi/linux/bpf.h>
88
#include "bpf_helpers.h"
9+
#include "bpf_tracing.h"
910
#include <uapi/linux/ptrace.h>
1011
#include <uapi/linux/perf_event.h>
1112
#include <linux/version.h>

samples/bpf/sampleip_kern.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <uapi/linux/bpf.h>
1010
#include <uapi/linux/bpf_perf_event.h>
1111
#include "bpf_helpers.h"
12+
#include "bpf_tracing.h"
1213

1314
#define MAX_IPS 8192
1415

samples/bpf/spintest_kern.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <uapi/linux/bpf.h>
1111
#include <uapi/linux/perf_event.h>
1212
#include "bpf_helpers.h"
13+
#include "bpf_tracing.h"
1314

1415
struct bpf_map_def SEC("maps") my_map = {
1516
.type = BPF_MAP_TYPE_HASH,

samples/bpf/test_map_in_map_kern.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <uapi/linux/in6.h>
1313
#include "bpf_helpers.h"
1414
#include "bpf_legacy.h"
15+
#include "bpf_tracing.h"
1516

1617
#define MAX_NR_PORTS 65536
1718

samples/bpf/test_overhead_kprobe_kern.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <linux/ptrace.h>
99
#include <uapi/linux/bpf.h>
1010
#include "bpf_helpers.h"
11+
#include "bpf_tracing.h"
1112

1213
#define _(P) ({typeof(P) val = 0; bpf_probe_read(&val, sizeof(val), &P); val;})
1314

samples/bpf/test_probe_write_user_kern.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <uapi/linux/bpf.h>
1010
#include <linux/version.h>
1111
#include "bpf_helpers.h"
12+
#include "bpf_tracing.h"
1213

1314
struct bpf_map_def SEC("maps") dnat_map = {
1415
.type = BPF_MAP_TYPE_HASH,

samples/bpf/trace_event_kern.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <uapi/linux/bpf_perf_event.h>
1111
#include <uapi/linux/perf_event.h>
1212
#include "bpf_helpers.h"
13+
#include "bpf_tracing.h"
1314

1415
struct key_t {
1516
char comm[TASK_COMM_LEN];

samples/bpf/tracex1_kern.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <uapi/linux/bpf.h>
1010
#include <linux/version.h>
1111
#include "bpf_helpers.h"
12+
#include "bpf_tracing.h"
1213

1314
#define _(P) ({typeof(P) val = 0; bpf_probe_read(&val, sizeof(val), &P); val;})
1415

samples/bpf/tracex2_kern.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <linux/version.h>
1010
#include <uapi/linux/bpf.h>
1111
#include "bpf_helpers.h"
12+
#include "bpf_tracing.h"
1213

1314
struct bpf_map_def SEC("maps") my_map = {
1415
.type = BPF_MAP_TYPE_HASH,

samples/bpf/tracex3_kern.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <linux/version.h>
1010
#include <uapi/linux/bpf.h>
1111
#include "bpf_helpers.h"
12+
#include "bpf_tracing.h"
1213

1314
struct bpf_map_def SEC("maps") my_map = {
1415
.type = BPF_MAP_TYPE_HASH,

samples/bpf/tracex4_kern.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <linux/version.h>
99
#include <uapi/linux/bpf.h>
1010
#include "bpf_helpers.h"
11+
#include "bpf_tracing.h"
1112

1213
struct pair {
1314
u64 val;

samples/bpf/tracex5_kern.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <uapi/linux/unistd.h>
1212
#include "syscall_nrs.h"
1313
#include "bpf_helpers.h"
14+
#include "bpf_tracing.h"
1415

1516
#define PROG(F) SEC("kprobe/"__stringify(F)) int bpf_func_##F
1617

tools/testing/selftests/bpf/bpf_helpers.h

Lines changed: 0 additions & 190 deletions
Original file line numberDiff line numberDiff line change
@@ -32,196 +32,6 @@ struct bpf_map_def {
3232
unsigned int map_flags;
3333
};
3434

35-
/* Scan the ARCH passed in from ARCH env variable (see Makefile) */
36-
#if defined(__TARGET_ARCH_x86)
37-
#define bpf_target_x86
38-
#define bpf_target_defined
39-
#elif defined(__TARGET_ARCH_s390)
40-
#define bpf_target_s390
41-
#define bpf_target_defined
42-
#elif defined(__TARGET_ARCH_arm)
43-
#define bpf_target_arm
44-
#define bpf_target_defined
45-
#elif defined(__TARGET_ARCH_arm64)
46-
#define bpf_target_arm64
47-
#define bpf_target_defined
48-
#elif defined(__TARGET_ARCH_mips)
49-
#define bpf_target_mips
50-
#define bpf_target_defined
51-
#elif defined(__TARGET_ARCH_powerpc)
52-
#define bpf_target_powerpc
53-
#define bpf_target_defined
54-
#elif defined(__TARGET_ARCH_sparc)
55-
#define bpf_target_sparc
56-
#define bpf_target_defined
57-
#else
58-
#undef bpf_target_defined
59-
#endif
60-
61-
/* Fall back to what the compiler says */
62-
#ifndef bpf_target_defined
63-
#if defined(__x86_64__)
64-
#define bpf_target_x86
65-
#elif defined(__s390__)
66-
#define bpf_target_s390
67-
#elif defined(__arm__)
68-
#define bpf_target_arm
69-
#elif defined(__aarch64__)
70-
#define bpf_target_arm64
71-
#elif defined(__mips__)
72-
#define bpf_target_mips
73-
#elif defined(__powerpc__)
74-
#define bpf_target_powerpc
75-
#elif defined(__sparc__)
76-
#define bpf_target_sparc
77-
#endif
78-
#endif
79-
80-
#if defined(bpf_target_x86)
81-
82-
#ifdef __KERNEL__
83-
#define PT_REGS_PARM1(x) ((x)->di)
84-
#define PT_REGS_PARM2(x) ((x)->si)
85-
#define PT_REGS_PARM3(x) ((x)->dx)
86-
#define PT_REGS_PARM4(x) ((x)->cx)
87-
#define PT_REGS_PARM5(x) ((x)->r8)
88-
#define PT_REGS_RET(x) ((x)->sp)
89-
#define PT_REGS_FP(x) ((x)->bp)
90-
#define PT_REGS_RC(x) ((x)->ax)
91-
#define PT_REGS_SP(x) ((x)->sp)
92-
#define PT_REGS_IP(x) ((x)->ip)
93-
#else
94-
#ifdef __i386__
95-
/* i386 kernel is built with -mregparm=3 */
96-
#define PT_REGS_PARM1(x) ((x)->eax)
97-
#define PT_REGS_PARM2(x) ((x)->edx)
98-
#define PT_REGS_PARM3(x) ((x)->ecx)
99-
#define PT_REGS_PARM4(x) 0
100-
#define PT_REGS_PARM5(x) 0
101-
#define PT_REGS_RET(x) ((x)->esp)
102-
#define PT_REGS_FP(x) ((x)->ebp)
103-
#define PT_REGS_RC(x) ((x)->eax)
104-
#define PT_REGS_SP(x) ((x)->esp)
105-
#define PT_REGS_IP(x) ((x)->eip)
106-
#else
107-
#define PT_REGS_PARM1(x) ((x)->rdi)
108-
#define PT_REGS_PARM2(x) ((x)->rsi)
109-
#define PT_REGS_PARM3(x) ((x)->rdx)
110-
#define PT_REGS_PARM4(x) ((x)->rcx)
111-
#define PT_REGS_PARM5(x) ((x)->r8)
112-
#define PT_REGS_RET(x) ((x)->rsp)
113-
#define PT_REGS_FP(x) ((x)->rbp)
114-
#define PT_REGS_RC(x) ((x)->rax)
115-
#define PT_REGS_SP(x) ((x)->rsp)
116-
#define PT_REGS_IP(x) ((x)->rip)
117-
#endif
118-
#endif
119-
120-
#elif defined(bpf_target_s390)
121-
122-
/* s390 provides user_pt_regs instead of struct pt_regs to userspace */
123-
struct pt_regs;
124-
#define PT_REGS_S390 const volatile user_pt_regs
125-
#define PT_REGS_PARM1(x) (((PT_REGS_S390 *)(x))->gprs[2])
126-
#define PT_REGS_PARM2(x) (((PT_REGS_S390 *)(x))->gprs[3])
127-
#define PT_REGS_PARM3(x) (((PT_REGS_S390 *)(x))->gprs[4])
128-
#define PT_REGS_PARM4(x) (((PT_REGS_S390 *)(x))->gprs[5])
129-
#define PT_REGS_PARM5(x) (((PT_REGS_S390 *)(x))->gprs[6])
130-
#define PT_REGS_RET(x) (((PT_REGS_S390 *)(x))->gprs[14])
131-
/* Works only with CONFIG_FRAME_POINTER */
132-
#define PT_REGS_FP(x) (((PT_REGS_S390 *)(x))->gprs[11])
133-
#define PT_REGS_RC(x) (((PT_REGS_S390 *)(x))->gprs[2])
134-
#define PT_REGS_SP(x) (((PT_REGS_S390 *)(x))->gprs[15])
135-
#define PT_REGS_IP(x) (((PT_REGS_S390 *)(x))->psw.addr)
136-
137-
#elif defined(bpf_target_arm)
138-
139-
#define PT_REGS_PARM1(x) ((x)->uregs[0])
140-
#define PT_REGS_PARM2(x) ((x)->uregs[1])
141-
#define PT_REGS_PARM3(x) ((x)->uregs[2])
142-
#define PT_REGS_PARM4(x) ((x)->uregs[3])
143-
#define PT_REGS_PARM5(x) ((x)->uregs[4])
144-
#define PT_REGS_RET(x) ((x)->uregs[14])
145-
#define PT_REGS_FP(x) ((x)->uregs[11]) /* Works only with CONFIG_FRAME_POINTER */
146-
#define PT_REGS_RC(x) ((x)->uregs[0])
147-
#define PT_REGS_SP(x) ((x)->uregs[13])
148-
#define PT_REGS_IP(x) ((x)->uregs[12])
149-
150-
#elif defined(bpf_target_arm64)
151-
152-
/* arm64 provides struct user_pt_regs instead of struct pt_regs to userspace */
153-
struct pt_regs;
154-
#define PT_REGS_ARM64 const volatile struct user_pt_regs
155-
#define PT_REGS_PARM1(x) (((PT_REGS_ARM64 *)(x))->regs[0])
156-
#define PT_REGS_PARM2(x) (((PT_REGS_ARM64 *)(x))->regs[1])
157-
#define PT_REGS_PARM3(x) (((PT_REGS_ARM64 *)(x))->regs[2])
158-
#define PT_REGS_PARM4(x) (((PT_REGS_ARM64 *)(x))->regs[3])
159-
#define PT_REGS_PARM5(x) (((PT_REGS_ARM64 *)(x))->regs[4])
160-
#define PT_REGS_RET(x) (((PT_REGS_ARM64 *)(x))->regs[30])
161-
/* Works only with CONFIG_FRAME_POINTER */
162-
#define PT_REGS_FP(x) (((PT_REGS_ARM64 *)(x))->regs[29])
163-
#define PT_REGS_RC(x) (((PT_REGS_ARM64 *)(x))->regs[0])
164-
#define PT_REGS_SP(x) (((PT_REGS_ARM64 *)(x))->sp)
165-
#define PT_REGS_IP(x) (((PT_REGS_ARM64 *)(x))->pc)
166-
167-
#elif defined(bpf_target_mips)
168-
169-
#define PT_REGS_PARM1(x) ((x)->regs[4])
170-
#define PT_REGS_PARM2(x) ((x)->regs[5])
171-
#define PT_REGS_PARM3(x) ((x)->regs[6])
172-
#define PT_REGS_PARM4(x) ((x)->regs[7])
173-
#define PT_REGS_PARM5(x) ((x)->regs[8])
174-
#define PT_REGS_RET(x) ((x)->regs[31])
175-
#define PT_REGS_FP(x) ((x)->regs[30]) /* Works only with CONFIG_FRAME_POINTER */
176-
#define PT_REGS_RC(x) ((x)->regs[1])
177-
#define PT_REGS_SP(x) ((x)->regs[29])
178-
#define PT_REGS_IP(x) ((x)->cp0_epc)
179-
180-
#elif defined(bpf_target_powerpc)
181-
182-
#define PT_REGS_PARM1(x) ((x)->gpr[3])
183-
#define PT_REGS_PARM2(x) ((x)->gpr[4])
184-
#define PT_REGS_PARM3(x) ((x)->gpr[5])
185-
#define PT_REGS_PARM4(x) ((x)->gpr[6])
186-
#define PT_REGS_PARM5(x) ((x)->gpr[7])
187-
#define PT_REGS_RC(x) ((x)->gpr[3])
188-
#define PT_REGS_SP(x) ((x)->sp)
189-
#define PT_REGS_IP(x) ((x)->nip)
190-
191-
#elif defined(bpf_target_sparc)
192-
193-
#define PT_REGS_PARM1(x) ((x)->u_regs[UREG_I0])
194-
#define PT_REGS_PARM2(x) ((x)->u_regs[UREG_I1])
195-
#define PT_REGS_PARM3(x) ((x)->u_regs[UREG_I2])
196-
#define PT_REGS_PARM4(x) ((x)->u_regs[UREG_I3])
197-
#define PT_REGS_PARM5(x) ((x)->u_regs[UREG_I4])
198-
#define PT_REGS_RET(x) ((x)->u_regs[UREG_I7])
199-
#define PT_REGS_RC(x) ((x)->u_regs[UREG_I0])
200-
#define PT_REGS_SP(x) ((x)->u_regs[UREG_FP])
201-
202-
/* Should this also be a bpf_target check for the sparc case? */
203-
#if defined(__arch64__)
204-
#define PT_REGS_IP(x) ((x)->tpc)
205-
#else
206-
#define PT_REGS_IP(x) ((x)->pc)
207-
#endif
208-
209-
#endif
210-
211-
#if defined(bpf_target_powerpc)
212-
#define BPF_KPROBE_READ_RET_IP(ip, ctx) ({ (ip) = (ctx)->link; })
213-
#define BPF_KRETPROBE_READ_RET_IP BPF_KPROBE_READ_RET_IP
214-
#elif defined(bpf_target_sparc)
215-
#define BPF_KPROBE_READ_RET_IP(ip, ctx) ({ (ip) = PT_REGS_RET(ctx); })
216-
#define BPF_KRETPROBE_READ_RET_IP BPF_KPROBE_READ_RET_IP
217-
#else
218-
#define BPF_KPROBE_READ_RET_IP(ip, ctx) ({ \
219-
bpf_probe_read(&(ip), sizeof(ip), (void *)PT_REGS_RET(ctx)); })
220-
#define BPF_KRETPROBE_READ_RET_IP(ip, ctx) ({ \
221-
bpf_probe_read(&(ip), sizeof(ip), \
222-
(void *)(PT_REGS_FP(ctx) + sizeof(ip))); })
223-
#endif
224-
22535
/*
22636
* bpf_core_read() abstracts away bpf_probe_read() call and captures offset
22737
* relocation for source address using __builtin_preserve_access_index()

0 commit comments

Comments
 (0)