Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit a99855d

Browse files
committedSep 21, 2017
Merge branch 'ebpf-samples-cross-compile'
Joel Fernandes says: ==================== Add cross-compilation support to eBPF samples These patches fix issues seen when cross-compiling eBPF samples on arm64. Compared to [1], I dropped the controversial inline-asm patch and exploring other options to fix it. However these patches are a step in the right direction and I look forward to getting them into -next and the merge window. Changes since v3: - just a repost with acks [1] https://lkml.org/lkml/2017/8/7/417 ==================== Signed-off-by: David S. Miller <davem@davemloft.net>
2 parents ae3e24f + 8bf2ac2 commit a99855d

File tree

5 files changed

+67
-10
lines changed

5 files changed

+67
-10
lines changed
 

‎samples/bpf/Makefile

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,11 @@ HOSTLOADLIBES_syscall_tp += -lelf
177177
LLC ?= llc
178178
CLANG ?= clang
179179

180+
# Detect that we're cross compiling and use the cross compiler
181+
ifdef CROSS_COMPILE
182+
HOSTCC = $(CROSS_COMPILE)gcc
183+
endif
184+
180185
# Trick to allow make to be run from this directory
181186
all:
182187
$(MAKE) -C ../../ $(CURDIR)/
@@ -225,7 +230,7 @@ $(obj)/%.o: $(src)/%.c
225230
$(CLANG) $(NOSTDINC_FLAGS) $(LINUXINCLUDE) $(EXTRA_CFLAGS) -I$(obj) \
226231
-I$(srctree)/tools/testing/selftests/bpf/ \
227232
-D__KERNEL__ -D__ASM_SYSREG_H -Wno-unused-value -Wno-pointer-sign \
228-
-Wno-compare-distinct-pointer-types \
233+
-D__TARGET_ARCH_$(ARCH) -Wno-compare-distinct-pointer-types \
229234
-Wno-gnu-variable-sized-type-not-at-end \
230235
-Wno-address-of-packed-member -Wno-tautological-compare \
231236
-Wno-unknown-warning-option \

‎samples/bpf/README.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,13 @@ It is also possible to point make to the newly compiled 'llc' or
6464
'clang' command via redefining LLC or CLANG on the make command line::
6565

6666
make samples/bpf/ LLC=~/git/llvm/build/bin/llc CLANG=~/git/llvm/build/bin/clang
67+
68+
Cross compiling samples
69+
-----------------------
70+
In order to cross-compile, say for arm64 targets, export CROSS_COMPILE and ARCH
71+
environment variables before calling make. This will direct make to build
72+
samples for the cross target.
73+
74+
export ARCH=arm64
75+
export CROSS_COMPILE="aarch64-linux-gnu-"
76+
make samples/bpf/ LLC=~/git/llvm/build/bin/llc CLANG=~/git/llvm/build/bin/clang

‎samples/bpf/map_perf_test_kern.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ int stress_hash_map_lookup(struct pt_regs *ctx)
266266
return 0;
267267
}
268268

269-
SEC("kprobe/sys_getpgrp")
269+
SEC("kprobe/sys_getppid")
270270
int stress_array_map_lookup(struct pt_regs *ctx)
271271
{
272272
u32 key = 1, i;

‎samples/bpf/map_perf_test_user.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ static void test_array_lookup(int cpu)
282282

283283
start_time = time_get_ns();
284284
for (i = 0; i < max_cnt; i++)
285-
syscall(__NR_getpgrp, 0);
285+
syscall(__NR_getppid, 0);
286286
printf("%d:array_lookup %lld lookups per sec\n",
287287
cpu, max_cnt * 1000000000ll * 64 / (time_get_ns() - start_time));
288288
}

‎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)
Failed to load comments.