Skip to content

Commit b655fc1

Browse files
Joel Fernandesdavem330
authored andcommitted
samples/bpf: Fix pt_regs issues when cross-compiling
BPF samples fail to build when cross-compiling for ARM64 because of incorrect pt_regs param selection. This is because clang defines __x86_64__ and bpf_headers thinks we're building for x86. Since clang is building for the BPF target, it shouldn't make assumptions about what target the BPF program is going to run on. To fix this, lets pass ARCH so the header knows which target the BPF program is being compiled for and can use the correct pt_regs code. Acked-by: Alexei Starovoitov <ast@kernel.org> Signed-off-by: Joel Fernandes <joelaf@google.com> Acked-by: Daniel Borkmann <daniel@iogearbox.net> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 876e88e commit b655fc1

File tree

2 files changed

+50
-8
lines changed

2 files changed

+50
-8
lines changed

samples/bpf/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ $(obj)/%.o: $(src)/%.c
230230
$(CLANG) $(NOSTDINC_FLAGS) $(LINUXINCLUDE) $(EXTRA_CFLAGS) -I$(obj) \
231231
-I$(srctree)/tools/testing/selftests/bpf/ \
232232
-D__KERNEL__ -D__ASM_SYSREG_H -Wno-unused-value -Wno-pointer-sign \
233-
-Wno-compare-distinct-pointer-types \
233+
-D__TARGET_ARCH_$(ARCH) -Wno-compare-distinct-pointer-types \
234234
-Wno-gnu-variable-sized-type-not-at-end \
235235
-Wno-address-of-packed-member -Wno-tautological-compare \
236236
-Wno-unknown-warning-option \

tools/testing/selftests/bpf/bpf_helpers.h

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,47 @@ static int (*bpf_skb_under_cgroup)(void *ctx, void *map, int index) =
109109
static int (*bpf_skb_change_head)(void *, int len, int flags) =
110110
(void *) BPF_FUNC_skb_change_head;
111111

112+
/* Scan the ARCH passed in from ARCH env variable (see Makefile) */
113+
#if defined(__TARGET_ARCH_x86)
114+
#define bpf_target_x86
115+
#define bpf_target_defined
116+
#elif defined(__TARGET_ARCH_s930x)
117+
#define bpf_target_s930x
118+
#define bpf_target_defined
119+
#elif defined(__TARGET_ARCH_arm64)
120+
#define bpf_target_arm64
121+
#define bpf_target_defined
122+
#elif defined(__TARGET_ARCH_mips)
123+
#define bpf_target_mips
124+
#define bpf_target_defined
125+
#elif defined(__TARGET_ARCH_powerpc)
126+
#define bpf_target_powerpc
127+
#define bpf_target_defined
128+
#elif defined(__TARGET_ARCH_sparc)
129+
#define bpf_target_sparc
130+
#define bpf_target_defined
131+
#else
132+
#undef bpf_target_defined
133+
#endif
134+
135+
/* Fall back to what the compiler says */
136+
#ifndef bpf_target_defined
112137
#if defined(__x86_64__)
138+
#define bpf_target_x86
139+
#elif defined(__s390x__)
140+
#define bpf_target_s930x
141+
#elif defined(__aarch64__)
142+
#define bpf_target_arm64
143+
#elif defined(__mips__)
144+
#define bpf_target_mips
145+
#elif defined(__powerpc__)
146+
#define bpf_target_powerpc
147+
#elif defined(__sparc__)
148+
#define bpf_target_sparc
149+
#endif
150+
#endif
151+
152+
#if defined(bpf_target_x86)
113153

114154
#define PT_REGS_PARM1(x) ((x)->di)
115155
#define PT_REGS_PARM2(x) ((x)->si)
@@ -122,7 +162,7 @@ static int (*bpf_skb_change_head)(void *, int len, int flags) =
122162
#define PT_REGS_SP(x) ((x)->sp)
123163
#define PT_REGS_IP(x) ((x)->ip)
124164

125-
#elif defined(__s390x__)
165+
#elif defined(bpf_target_s390x)
126166

127167
#define PT_REGS_PARM1(x) ((x)->gprs[2])
128168
#define PT_REGS_PARM2(x) ((x)->gprs[3])
@@ -135,7 +175,7 @@ static int (*bpf_skb_change_head)(void *, int len, int flags) =
135175
#define PT_REGS_SP(x) ((x)->gprs[15])
136176
#define PT_REGS_IP(x) ((x)->psw.addr)
137177

138-
#elif defined(__aarch64__)
178+
#elif defined(bpf_target_arm64)
139179

140180
#define PT_REGS_PARM1(x) ((x)->regs[0])
141181
#define PT_REGS_PARM2(x) ((x)->regs[1])
@@ -148,7 +188,7 @@ static int (*bpf_skb_change_head)(void *, int len, int flags) =
148188
#define PT_REGS_SP(x) ((x)->sp)
149189
#define PT_REGS_IP(x) ((x)->pc)
150190

151-
#elif defined(__mips__)
191+
#elif defined(bpf_target_mips)
152192

153193
#define PT_REGS_PARM1(x) ((x)->regs[4])
154194
#define PT_REGS_PARM2(x) ((x)->regs[5])
@@ -161,7 +201,7 @@ static int (*bpf_skb_change_head)(void *, int len, int flags) =
161201
#define PT_REGS_SP(x) ((x)->regs[29])
162202
#define PT_REGS_IP(x) ((x)->cp0_epc)
163203

164-
#elif defined(__powerpc__)
204+
#elif defined(bpf_target_powerpc)
165205

166206
#define PT_REGS_PARM1(x) ((x)->gpr[3])
167207
#define PT_REGS_PARM2(x) ((x)->gpr[4])
@@ -172,7 +212,7 @@ static int (*bpf_skb_change_head)(void *, int len, int flags) =
172212
#define PT_REGS_SP(x) ((x)->sp)
173213
#define PT_REGS_IP(x) ((x)->nip)
174214

175-
#elif defined(__sparc__)
215+
#elif defined(bpf_target_sparc)
176216

177217
#define PT_REGS_PARM1(x) ((x)->u_regs[UREG_I0])
178218
#define PT_REGS_PARM2(x) ((x)->u_regs[UREG_I1])
@@ -182,6 +222,8 @@ static int (*bpf_skb_change_head)(void *, int len, int flags) =
182222
#define PT_REGS_RET(x) ((x)->u_regs[UREG_I7])
183223
#define PT_REGS_RC(x) ((x)->u_regs[UREG_I0])
184224
#define PT_REGS_SP(x) ((x)->u_regs[UREG_FP])
225+
226+
/* Should this also be a bpf_target check for the sparc case? */
185227
#if defined(__arch64__)
186228
#define PT_REGS_IP(x) ((x)->tpc)
187229
#else
@@ -190,10 +232,10 @@ static int (*bpf_skb_change_head)(void *, int len, int flags) =
190232

191233
#endif
192234

193-
#ifdef __powerpc__
235+
#ifdef bpf_target_powerpc
194236
#define BPF_KPROBE_READ_RET_IP(ip, ctx) ({ (ip) = (ctx)->link; })
195237
#define BPF_KRETPROBE_READ_RET_IP BPF_KPROBE_READ_RET_IP
196-
#elif defined(__sparc__)
238+
#elif bpf_target_sparc
197239
#define BPF_KPROBE_READ_RET_IP(ip, ctx) ({ (ip) = PT_REGS_RET(ctx); })
198240
#define BPF_KRETPROBE_READ_RET_IP BPF_KPROBE_READ_RET_IP
199241
#else

0 commit comments

Comments
 (0)