Skip to contentSkip to conversation

selftests/bpf: Fix build of task_pt_regs tests for arm64

PATCH Superseded Jean-Philippe Brucker 4 participants 5 messages Sep 2, 2021 – Sep 3, 2021

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 selftests/bpf: Fix build of task_pt_regs tests for arm64

struct pt_regs is not exported to userspace on all archs. arm64 and s390 export "user_pt_regs" instead, which causes build failure at the moment:

progs/test_task_pt_regs.c:8:16: error: variable has incomplete type 'struct pt_regs' struct pt_regs current_regs = {};

Use the multi-arch macros defined by tools/lib/bpf/bpf_tracing.h to copy the pt_regs into a locally-defined struct.

Copying the user_pt_regs struct on arm64 wouldn't work because the struct is too large and the compiler complains about using too much stack.

Fixes 576d47bb1a92 ("bpf: selftests: Add bpf_task_pt_regs() selftest")
---
 .../selftests/bpf/bpf_pt_regs_helpers.h       | 30 +++++++++++++++++++
 .../selftests/bpf/prog_tests/task_pt_regs.c   |  1 +
 .../selftests/bpf/progs/test_task_pt_regs.c   | 10 ++++---
 3 files changed, 37 insertions(+), 4 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/bpf_pt_regs_helpers.h
tools/testing/selftests/bpf/bpf_pt_regs_helpers.h new file +30 -0
new file mode 100644
index 000000000000..7531f4824ead
@@ -0,0 +1,30 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef __BPF_PT_REGS_HELPERS
+#define __BPF_PT_REGS_HELPERS
+
+#include <bpf/bpf_tracing.h>
+
+struct bpf_pt_regs {
+	unsigned long long parm[5];
+	unsigned long long ret;
+	unsigned long long fp;
+	unsigned long long rc;
+	unsigned long long sp;
+	unsigned long long ip;
+};
+
+static inline void bpf_copy_pt_regs(struct bpf_pt_regs *dest, struct pt_regs *src)
+{
+	dest->parm[0]	= PT_REGS_PARM1(src);
+	dest->parm[1]	= PT_REGS_PARM2(src);
+	dest->parm[2]	= PT_REGS_PARM3(src);
+	dest->parm[3]	= PT_REGS_PARM4(src);
+	dest->parm[4]	= PT_REGS_PARM5(src);
+	dest->ret	= PT_REGS_RET(src);
+	dest->fp	= PT_REGS_FP(src);
+	dest->rc	= PT_REGS_RC(src);
+	dest->sp	= PT_REGS_SP(src);
+	dest->ip	= PT_REGS_IP(src);
+}
+
+#endif /* __BPF_PT_REGS_HELPERS */
tools/testing/selftests/bpf/prog_tests/task_pt_regs.c +1 -0
index 53f0e0fa1a53..196453b75937 100644
@@ -2,6 +2,7 @@
 #define _GNU_SOURCE
 #include <test_progs.h>
 #include <linux/ptrace.h>
+#include "bpf_pt_regs_helpers.h"
 #include "test_task_pt_regs.skel.h"
 
 void test_task_pt_regs(void)
tools/testing/selftests/bpf/progs/test_task_pt_regs.c +6 -4
index 6c059f1cfa1b..348da3509093 100644
@@ -5,8 +5,10 @@
 #include <bpf/bpf_helpers.h>
 #include <bpf/bpf_tracing.h>
 
-struct pt_regs current_regs = {};
-struct pt_regs ctx_regs = {};
+#include "bpf_pt_regs_helpers.h"
+
+struct bpf_pt_regs current_regs = {};
+struct bpf_pt_regs ctx_regs = {};
 int uprobe_res = 0;
 
 SEC("uprobe/trigger_func")
@@ -17,8 +19,8 @@ int handle_uprobe(struct pt_regs *ctx)
 
 	current = bpf_get_current_task_btf();
 	regs = (struct pt_regs *) bpf_task_pt_regs(current);
-	__builtin_memcpy(&current_regs, regs, sizeof(*regs));
-	__builtin_memcpy(&ctx_regs, ctx, sizeof(*ctx));
+	bpf_copy_pt_regs(&current_regs, regs);
+	bpf_copy_pt_regs(&ctx_regs, ctx);
 
 	/* Prove that uprobe was run */
 	uprobe_res = 1;
Signature · 1 line
2.33.0
Acked-by on v1 1/1
#1 lore raw
Quoted 13 lines · Jean-Philippe Brucker

struct pt_regs is not exported to userspace on all archs. arm64 and s390 export "user_pt_regs" instead, which causes build failure at the moment:

progs/test_task_pt_regs.c:8:16: error: variable has incomplete type 'struct pt_regs' struct pt_regs current_regs = {};

Use the multi-arch macros defined by tools/lib/bpf/bpf_tracing.h to copy the pt_regs into a locally-defined struct.

Copying the user_pt_regs struct on arm64 wouldn't work because the struct is too large and the compiler complains about using too much stack.

Quoted 8 lines
Fixes 576d47bb1a92 ("bpf: selftests: Add bpf_task_pt_regs() selftest")
---
 .../selftests/bpf/bpf_pt_regs_helpers.h       | 30 +++++++++++++++++++
 .../selftests/bpf/prog_tests/task_pt_regs.c   |  1 +
 .../selftests/bpf/progs/test_task_pt_regs.c   | 10 ++++---
 3 files changed, 37 insertions(+), 4 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/bpf_pt_regs_helpers.h

[...]

#2 lore raw
In reply to Jean-Philippe Brucker
Quoted 6 lines · Jean-Philippe Brucker

struct pt_regs is not exported to userspace on all archs. arm64 and s390 export "user_pt_regs" instead, which causes build failure at the moment:

progs/test_task_pt_regs.c:8:16: error: variable has incomplete type 'struct pt_regs' struct pt_regs current_regs = {};

Right, which is 'bpf_user_pt_regs_t'.
It's defined for all archs and arm64/s390/ppc/risv define it differently from pt_regs.

Quoted 7 lines

Use the multi-arch macros defined by tools/lib/bpf/bpf_tracing.h to copy the pt_regs into a locally-defined struct.

Copying the user_pt_regs struct on arm64 wouldn't work because the struct is too large and the compiler complains about using too much stack.

That's a different issue.
I think the cleaner fix would be to make the test use bpf_user_pt_regs_t instead.

Quoted 88 lines
Fixes 576d47bb1a92 ("bpf: selftests: Add bpf_task_pt_regs() selftest")
---
 .../selftests/bpf/bpf_pt_regs_helpers.h       | 30 +++++++++++++++++++
 .../selftests/bpf/prog_tests/task_pt_regs.c   |  1 +
 .../selftests/bpf/progs/test_task_pt_regs.c   | 10 ++++---
 3 files changed, 37 insertions(+), 4 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/bpf_pt_regs_helpers.h
tools/testing/selftests/bpf/bpf_pt_regs_helpers.h new file +30 -0
new file mode 100644
index 000000000000..7531f4824ead
@@ -0,0 +1,30 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef __BPF_PT_REGS_HELPERS
+#define __BPF_PT_REGS_HELPERS
+
+#include <bpf/bpf_tracing.h>
+
+struct bpf_pt_regs {
+       unsigned long long parm[5];
+       unsigned long long ret;
+       unsigned long long fp;
+       unsigned long long rc;
+       unsigned long long sp;
+       unsigned long long ip;
+};
+
+static inline void bpf_copy_pt_regs(struct bpf_pt_regs *dest, struct pt_regs *src)
+{
+       dest->parm[0]   = PT_REGS_PARM1(src);
+       dest->parm[1]   = PT_REGS_PARM2(src);
+       dest->parm[2]   = PT_REGS_PARM3(src);
+       dest->parm[3]   = PT_REGS_PARM4(src);
+       dest->parm[4]   = PT_REGS_PARM5(src);
+       dest->ret       = PT_REGS_RET(src);
+       dest->fp        = PT_REGS_FP(src);
+       dest->rc        = PT_REGS_RC(src);
+       dest->sp        = PT_REGS_SP(src);
+       dest->ip        = PT_REGS_IP(src);
+}
+
+#endif /* __BPF_PT_REGS_HELPERS */
tools/testing/selftests/bpf/prog_tests/task_pt_regs.c +1 -0
index 53f0e0fa1a53..196453b75937 100644
@@ -2,6 +2,7 @@
 #define _GNU_SOURCE
 #include <test_progs.h>
 #include <linux/ptrace.h>
+#include "bpf_pt_regs_helpers.h"
 #include "test_task_pt_regs.skel.h"
 
 void test_task_pt_regs(void)
tools/testing/selftests/bpf/progs/test_task_pt_regs.c +6 -5
index 6c059f1cfa1b..348da3509093 100644
@@ -5,8 +5,10 @@
 #include <bpf/bpf_helpers.h>
 #include <bpf/bpf_tracing.h>
 
-struct pt_regs current_regs = {};
-struct pt_regs ctx_regs = {};
+#include "bpf_pt_regs_helpers.h"
+
+struct bpf_pt_regs current_regs = {};
+struct bpf_pt_regs ctx_regs = {};
 int uprobe_res = 0;
 
 SEC("uprobe/trigger_func")
@@ -17,8 +19,8 @@ int handle_uprobe(struct pt_regs *ctx)
 
        current = bpf_get_current_task_btf();
        regs = (struct pt_regs *) bpf_task_pt_regs(current);
-       __builtin_memcpy(&current_regs, regs, sizeof(*regs));
-       __builtin_memcpy(&ctx_regs, ctx, sizeof(*ctx));
+       bpf_copy_pt_regs(&current_regs, regs);
+       bpf_copy_pt_regs(&ctx_regs, ctx);
 
        /* Prove that uprobe was run */
        uprobe_res = 1;
--

2.33.0

#3 lore raw
Quoted 14 lines · Alexei Starovoitov
Quoted 6 lines · Jean-Philippe Brucker

struct pt_regs is not exported to userspace on all archs. arm64 and s390 export "user_pt_regs" instead, which causes build failure at the moment:

progs/test_task_pt_regs.c:8:16: error: variable has incomplete type 'struct pt_regs' struct pt_regs current_regs = {};

Right, which is 'bpf_user_pt_regs_t'.
It's defined for all archs and arm64/s390/ppc/risv define it differently from pt_regs.

Quoted 1 line
Quoted 8 lines
Quoted 6 lines

Use the multi-arch macros defined by tools/lib/bpf/bpf_tracing.h to copy the pt_regs into a locally-defined struct.

Copying the user_pt_regs struct on arm64 wouldn't work because the struct is too large and the compiler complains about using too much stack.

That's a different issue.

It does work when doing an implicit copy (current_regs = *regs) rather than using __builtin_memcpy(). Don't know why but I'll take it.

Quoted 2 lines

I think the cleaner fix would be to make the test use bpf_user_pt_regs_t instead.

Right, although that comes with another complication. We end up including tools/include/uapi/asm/bpf_perf_event.h which requires the compiler builtins "__aarch64__", "__s390__", etc. Those are not defined with "clang -target bpf" so I have to add them to the command line. I'll resend with your suggestion but this patch is simpler.

Thanks,
Jean

#4 lore raw
Quoted 28 lines · Jean-Philippe Brucker
Quoted 14 lines · Alexei Starovoitov
Quoted 6 lines · Jean-Philippe Brucker

struct pt_regs is not exported to userspace on all archs. arm64 and s390 export "user_pt_regs" instead, which causes build failure at the moment:

progs/test_task_pt_regs.c:8:16: error: variable has incomplete type 'struct pt_regs' struct pt_regs current_regs = {};

Right, which is 'bpf_user_pt_regs_t'.
It's defined for all archs and arm64/s390/ppc/risv define it differently from pt_regs.

Quoted 1 line
Quoted 8 lines
Quoted 6 lines

Use the multi-arch macros defined by tools/lib/bpf/bpf_tracing.h to copy the pt_regs into a locally-defined struct.

Copying the user_pt_regs struct on arm64 wouldn't work because the struct is too large and the compiler complains about using too much stack.

That's a different issue.

It does work when doing an implicit copy (current_regs = *regs) rather than using __builtin_memcpy(). Don't know why but I'll take it.

Quoted 9 lines
Quoted 2 lines

I think the cleaner fix would be to make the test use bpf_user_pt_regs_t instead.

Right, although that comes with another complication. We end up including tools/include/uapi/asm/bpf_perf_event.h which requires the compiler builtins "__aarch64__", "__s390__", etc. Those are not defined with "clang -target bpf" so I have to add them to the command line. I'll resend with your suggestion but this patch is simpler.

The test doesn't care about struct pt_regs type itself, it only cares to check that contents of captured pt_regs are the same.

We can use CO-RE to check whether user_pt_regs or pt_regs exists in the kernel. We can also use bpf_core_type_size() to know exactly how many bytes we want to capture. And then just use bpf_probe_read_kernel() as memcpy() equivalent to capture bytes. This should work on all architectures.

Quoted 2 lines

Thanks,
Jean