Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1725725

[PATCH RFC v3 3/4] samples/bpf: Fix pt_regs issues when cross-compiling

Path csiph.com!aioe.org!bofh.it!news.nic.it!robomod
From Joel Fernandes <joelaf@google.com>
Newsgroups linux.kernel
Subject [PATCH RFC v3 3/4] samples/bpf: Fix pt_regs issues when cross-compiling
Date Sun, 03 Sep 2017 20:30:02 +0200
Message-ID <ulHOG-3lK-15@gated-at.bofh.it> (permalink)
References <ulHOF-3lK-5@gated-at.bofh.it>
X-Original-To linux-kernel@vger.kernel.org, netdev@vger.kernel.org
Dkim-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=eHfjwX5w/EXC/FwMFQ4dwJii41udQ+7ZPd1paVvhvzg=; b=lEW9UKcSkrvCU9pmRq24A6JWR814XYaqXrYGv5PNdSxMQ27gF+6QRkCAwKnmfI/ceR 86UfumldwvJ31yyolU37mA0wUPNjOd5PGL8S3jAXsja2MkyMwcTl0gN6ZckDOxJNE8Vm HJ4ozeMe2Ly1w1L2F1tfQxhe8hy+ieNIpJT7pOuS019ojSangulFX/FQf0I9VCHdaLNp TEMHWYyXYIVfwgeAe/nbEhwI6/Io5hdeXcHFUoE8dcWR21OQ+SvuZOLM4Zw7cjeGnNpF 3sdfyv1t2nVaBy6+/+YCsbgRHpswFGM/V8903uy4bcsbQrGAHlI/RdHWLM7hU5N/CQc9 OfSg==
X-Google-Dkim-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=eHfjwX5w/EXC/FwMFQ4dwJii41udQ+7ZPd1paVvhvzg=; b=m1L2DBkxaZ/Qk3x/9yEweSSDob9ijiO1OqQhvUjfCAr6rFfcKJXNyLQ0U1+HPXgrTf Teb/XedhZWrvYNEa/prCGmUYfiMzTvTLJmw5/7SS46es1543KNYZy+VZFs9LVglQJiaL Dh0eXKs2fxrZpSJvVjgQdR+PNL/9UofjH0HBJqqeOJJhEvx+dEqP4kT/sJg0d0QbK9hc trNOc2P/zrfIaRCsNb8NdRFms2krM70Kx3hid1M0qi6qRCVTp4KtGpPfMlvVniNto9b/ 84BjZYgmiarMgIEea2ctqMWfU+wyOTPJyF89XK7Ad7kTiTKIp11ayJ32JQWqbkfIHg+J pWhA==
X-Gm-Message-State AHPjjUir4Bw+Tyf04dA8Cjln+3RXSQ6zisI5q0RHQRp2xrK1S55x7qg6 G2QOreJzeCF5o+pOR+r8CA==
X-Google-SMTP-Source ADKCNb4ywmauyzZqCOs0Wj5w1pzqjInBzOlx4aV2kUh3P/+SdaZWVgyyE9acBDFnE6m/z3OU0rq7EA==
X-Received by 10.98.194.193 with SMTP id w62mr1579946pfk.246.1504463167492; Sun, 03 Sep 2017 11:26:07 -0700 (PDT)
X-Mailer git-send-email 2.14.1.581.gf28d330327-goog
Sender robomod@news.nic.it
List-ID <linux-kernel.vger.kernel.org>
X-Mailing-List linux-kernel@vger.kernel.org
Approved robomod@news.nic.it
Lines 150
Organization linux.* mail to news gateway
X-Original-Cc kernel-team@android.com, Juri Lelli <juri.lelli@arm.com>, Alison Chaiken <alison@she-devel.com>, Chenbo Feng <fengc@google.com>, Joel Fernandes <joelaf@google.com>, Alexei Starovoitov <ast@kernel.org>, Daniel Borkmann <daniel@iogearbox.net>, David Miller <davem@davemloft.net>
X-Original-Date Sun, 3 Sep 2017 11:25:48 -0700
X-Original-Message-ID <20170903182549.30866-3-joelaf@google.com>
X-Original-References <20170903182549.30866-1-joelaf@google.com>
X-Original-Sender linux-kernel-owner@vger.kernel.org
Xref csiph.com linux.kernel:1725725

Show key headers only | View raw


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.

CC: Alexei Starovoitov <ast@kernel.org>
CC: Daniel Borkmann <daniel@iogearbox.net>
Cc: David Miller <davem@davemloft.net>
Signed-off-by: Joel Fernandes <joelaf@google.com>
---
 samples/bpf/Makefile                      |  2 +-
 tools/testing/selftests/bpf/bpf_helpers.h | 56 +++++++++++++++++++++++++++----
 2 files changed, 50 insertions(+), 8 deletions(-)

diff --git a/samples/bpf/Makefile b/samples/bpf/Makefile
index 13f74b67ca44..ebc2ad69b62c 100644
--- a/samples/bpf/Makefile
+++ b/samples/bpf/Makefile
@@ -230,7 +230,7 @@ $(obj)/%.o: $(src)/%.c
 	$(CLANG) $(NOSTDINC_FLAGS) $(LINUXINCLUDE) $(EXTRA_CFLAGS) -I$(obj) \
 		-I$(srctree)/tools/testing/selftests/bpf/ \
 		-D__KERNEL__ -D__ASM_SYSREG_H -Wno-unused-value -Wno-pointer-sign \
-		-Wno-compare-distinct-pointer-types \
+		-D__TARGET_ARCH_$(ARCH) -Wno-compare-distinct-pointer-types \
 		-Wno-gnu-variable-sized-type-not-at-end \
 		-Wno-address-of-packed-member -Wno-tautological-compare \
 		-Wno-unknown-warning-option \
diff --git a/tools/testing/selftests/bpf/bpf_helpers.h b/tools/testing/selftests/bpf/bpf_helpers.h
index 36fb9161b34a..4875395b0b52 100644
--- a/tools/testing/selftests/bpf/bpf_helpers.h
+++ b/tools/testing/selftests/bpf/bpf_helpers.h
@@ -109,7 +109,47 @@ static int (*bpf_skb_under_cgroup)(void *ctx, void *map, int index) =
 static int (*bpf_skb_change_head)(void *, int len, int flags) =
 	(void *) BPF_FUNC_skb_change_head;
 
+/* Scan the ARCH passed in from ARCH env variable (see Makefile) */
+#if defined(__TARGET_ARCH_x86)
+	#define bpf_target_x86
+	#define bpf_target_defined
+#elif defined(__TARGET_ARCH_s930x)
+	#define bpf_target_s930x
+	#define bpf_target_defined
+#elif defined(__TARGET_ARCH_arm64)
+	#define bpf_target_arm64
+	#define bpf_target_defined
+#elif defined(__TARGET_ARCH_mips)
+	#define bpf_target_mips
+	#define bpf_target_defined
+#elif defined(__TARGET_ARCH_powerpc)
+	#define bpf_target_powerpc
+	#define bpf_target_defined
+#elif defined(__TARGET_ARCH_sparc)
+	#define bpf_target_sparc
+	#define bpf_target_defined
+#else
+	#undef bpf_target_defined
+#endif
+
+/* Fall back to what the compiler says */
+#ifndef bpf_target_defined
 #if defined(__x86_64__)
+	#define bpf_target_x86
+#elif defined(__s390x__)
+	#define bpf_target_s930x
+#elif defined(__aarch64__)
+	#define bpf_target_arm64
+#elif defined(__mips__)
+	#define bpf_target_mips
+#elif defined(__powerpc__)
+	#define bpf_target_powerpc
+#elif defined(__sparc__)
+	#define bpf_target_sparc
+#endif
+#endif
+
+#if defined(bpf_target_x86)
 
 #define PT_REGS_PARM1(x) ((x)->di)
 #define PT_REGS_PARM2(x) ((x)->si)
@@ -122,7 +162,7 @@ static int (*bpf_skb_change_head)(void *, int len, int flags) =
 #define PT_REGS_SP(x) ((x)->sp)
 #define PT_REGS_IP(x) ((x)->ip)
 
-#elif defined(__s390x__)
+#elif defined(bpf_target_s390x)
 
 #define PT_REGS_PARM1(x) ((x)->gprs[2])
 #define PT_REGS_PARM2(x) ((x)->gprs[3])
@@ -135,7 +175,7 @@ static int (*bpf_skb_change_head)(void *, int len, int flags) =
 #define PT_REGS_SP(x) ((x)->gprs[15])
 #define PT_REGS_IP(x) ((x)->psw.addr)
 
-#elif defined(__aarch64__)
+#elif defined(bpf_target_arm64)
 
 #define PT_REGS_PARM1(x) ((x)->regs[0])
 #define PT_REGS_PARM2(x) ((x)->regs[1])
@@ -148,7 +188,7 @@ static int (*bpf_skb_change_head)(void *, int len, int flags) =
 #define PT_REGS_SP(x) ((x)->sp)
 #define PT_REGS_IP(x) ((x)->pc)
 
-#elif defined(__mips__)
+#elif defined(bpf_target_mips)
 
 #define PT_REGS_PARM1(x) ((x)->regs[4])
 #define PT_REGS_PARM2(x) ((x)->regs[5])
@@ -161,7 +201,7 @@ static int (*bpf_skb_change_head)(void *, int len, int flags) =
 #define PT_REGS_SP(x) ((x)->regs[29])
 #define PT_REGS_IP(x) ((x)->cp0_epc)
 
-#elif defined(__powerpc__)
+#elif defined(bpf_target_powerpc)
 
 #define PT_REGS_PARM1(x) ((x)->gpr[3])
 #define PT_REGS_PARM2(x) ((x)->gpr[4])
@@ -172,7 +212,7 @@ static int (*bpf_skb_change_head)(void *, int len, int flags) =
 #define PT_REGS_SP(x) ((x)->sp)
 #define PT_REGS_IP(x) ((x)->nip)
 
-#elif defined(__sparc__)
+#elif defined(bpf_target_sparc)
 
 #define PT_REGS_PARM1(x) ((x)->u_regs[UREG_I0])
 #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) =
 #define PT_REGS_RET(x) ((x)->u_regs[UREG_I7])
 #define PT_REGS_RC(x) ((x)->u_regs[UREG_I0])
 #define PT_REGS_SP(x) ((x)->u_regs[UREG_FP])
+
+/* Should this also be a bpf_target check for the sparc case? */
 #if defined(__arch64__)
 #define PT_REGS_IP(x) ((x)->tpc)
 #else
@@ -190,10 +232,10 @@ static int (*bpf_skb_change_head)(void *, int len, int flags) =
 
 #endif
 
-#ifdef __powerpc__
+#ifdef bpf_target_powerpc
 #define BPF_KPROBE_READ_RET_IP(ip, ctx)		({ (ip) = (ctx)->link; })
 #define BPF_KRETPROBE_READ_RET_IP		BPF_KPROBE_READ_RET_IP
-#elif defined(__sparc__)
+#elif bpf_target_sparc
 #define BPF_KPROBE_READ_RET_IP(ip, ctx)		({ (ip) = PT_REGS_RET(ctx); })
 #define BPF_KRETPROBE_READ_RET_IP		BPF_KPROBE_READ_RET_IP
 #else
-- 
2.14.1.581.gf28d330327-goog

Back to linux.kernel | Previous | Next | Find similar | Unroll thread


Thread

[PATCH RFC v3 3/4] samples/bpf: Fix pt_regs issues when cross-compiling Joel Fernandes <joelaf@google.com> - 2017-09-03 20:30 +0200

csiph-web