* [PATCH bpf-next v5 01/10] selftests/bpf: Fix an endianness issue in bpf_syscall_macro test
2022-02-09 2:17 [PATCH bpf-next v5 00/10] Fix accessing syscall arguments Ilya Leoshkevich
@ 2022-02-09 2:17 ` Ilya Leoshkevich
2022-02-09 2:17 ` [PATCH bpf-next v5 02/10] libbpf: Add PT_REGS_SYSCALL_REGS macro Ilya Leoshkevich
` (10 subsequent siblings)
11 siblings, 0 replies; 16+ messages in thread
From: Ilya Leoshkevich @ 2022-02-09 2:17 UTC (permalink / raw)
To: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
Heiko Carstens, Vasily Gorbik, Christian Borntraeger,
Alexander Gordeev
Cc: bpf, Ilya Leoshkevich
bpf_syscall_macro reads a long argument into an int variable, which
produces a wrong value on big-endian systems. Fix by reading the
argument into an intermediate long variable first.
Fixes: 77fc0330dfe5 ("selftests/bpf: Add a test to confirm PT_REGS_PARM4_SYSCALL")
Signed-off-by: Ilya Leoshkevich <iii@linux.ibm.com>
---
tools/testing/selftests/bpf/progs/bpf_syscall_macro.c | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)
diff --git a/tools/testing/selftests/bpf/progs/bpf_syscall_macro.c b/tools/testing/selftests/bpf/progs/bpf_syscall_macro.c
index c8e60220cda8..f5c6ef2ff6d1 100644
--- a/tools/testing/selftests/bpf/progs/bpf_syscall_macro.c
+++ b/tools/testing/selftests/bpf/progs/bpf_syscall_macro.c
@@ -28,6 +28,7 @@ int BPF_KPROBE(handle_sys_prctl)
{
struct pt_regs *real_regs;
pid_t pid = bpf_get_current_pid_tgid() >> 32;
+ unsigned long tmp;
if (pid != filter_pid)
return 0;
@@ -35,7 +36,9 @@ int BPF_KPROBE(handle_sys_prctl)
real_regs = (struct pt_regs *)PT_REGS_PARM1(ctx);
/* test for PT_REGS_PARM */
- bpf_probe_read_kernel(&arg1, sizeof(arg1), &PT_REGS_PARM1_SYSCALL(real_regs));
+
+ bpf_probe_read_kernel(&tmp, sizeof(tmp), &PT_REGS_PARM1_SYSCALL(real_regs));
+ arg1 = tmp;
bpf_probe_read_kernel(&arg2, sizeof(arg2), &PT_REGS_PARM2_SYSCALL(real_regs));
bpf_probe_read_kernel(&arg3, sizeof(arg3), &PT_REGS_PARM3_SYSCALL(real_regs));
bpf_probe_read_kernel(&arg4_cx, sizeof(arg4_cx), &PT_REGS_PARM4(real_regs));
--
2.34.1
^ permalink raw reply related [flat|nested] 16+ messages in thread
* [PATCH bpf-next v5 02/10] libbpf: Add PT_REGS_SYSCALL_REGS macro
2022-02-09 2:17 [PATCH bpf-next v5 00/10] Fix accessing syscall arguments Ilya Leoshkevich
2022-02-09 2:17 ` [PATCH bpf-next v5 01/10] selftests/bpf: Fix an endianness issue in bpf_syscall_macro test Ilya Leoshkevich
@ 2022-02-09 2:17 ` Ilya Leoshkevich
2022-02-09 2:17 ` [PATCH bpf-next v5 03/10] selftests/bpf: Use PT_REGS_SYSCALL_REGS in bpf_syscall_macro Ilya Leoshkevich
` (9 subsequent siblings)
11 siblings, 0 replies; 16+ messages in thread
From: Ilya Leoshkevich @ 2022-02-09 2:17 UTC (permalink / raw)
To: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
Heiko Carstens, Vasily Gorbik, Christian Borntraeger,
Alexander Gordeev
Cc: bpf, Ilya Leoshkevich
Architectures that select ARCH_HAS_SYSCALL_WRAPPER pass a pointer to
struct pt_regs to syscall handlers, others unpack it into individual
function parameters. Introduce a macro to describe what a particular
arch does.
Signed-off-by: Ilya Leoshkevich <iii@linux.ibm.com>
---
tools/lib/bpf/bpf_tracing.h | 10 ++++++++++
1 file changed, 10 insertions(+)
diff --git a/tools/lib/bpf/bpf_tracing.h b/tools/lib/bpf/bpf_tracing.h
index 032ba809f3e5..a5e92656bfba 100644
--- a/tools/lib/bpf/bpf_tracing.h
+++ b/tools/lib/bpf/bpf_tracing.h
@@ -326,6 +326,16 @@ struct pt_regs;
#endif /* defined(bpf_target_defined) */
+/*
+ * When invoked from a syscall handler kprobe, returns a pointer to a
+ * struct pt_regs containing syscall arguments and suitable for passing to
+ * PT_REGS_PARMn_SYSCALL() and PT_REGS_PARMn_CORE_SYSCALL().
+ */
+#ifndef PT_REGS_SYSCALL_REGS
+/* By default, assume that the arch selects ARCH_HAS_SYSCALL_WRAPPER. */
+#define PT_REGS_SYSCALL_REGS(ctx) ((struct pt_regs *)PT_REGS_PARM1(ctx))
+#endif
+
#ifndef ___bpf_concat
#define ___bpf_concat(a, b) a ## b
#endif
--
2.34.1
^ permalink raw reply related [flat|nested] 16+ messages in thread
* [PATCH bpf-next v5 03/10] selftests/bpf: Use PT_REGS_SYSCALL_REGS in bpf_syscall_macro
2022-02-09 2:17 [PATCH bpf-next v5 00/10] Fix accessing syscall arguments Ilya Leoshkevich
2022-02-09 2:17 ` [PATCH bpf-next v5 01/10] selftests/bpf: Fix an endianness issue in bpf_syscall_macro test Ilya Leoshkevich
2022-02-09 2:17 ` [PATCH bpf-next v5 02/10] libbpf: Add PT_REGS_SYSCALL_REGS macro Ilya Leoshkevich
@ 2022-02-09 2:17 ` Ilya Leoshkevich
2022-02-09 2:17 ` [PATCH bpf-next v5 04/10] libbpf: Fix accessing syscall arguments on powerpc Ilya Leoshkevich
` (8 subsequent siblings)
11 siblings, 0 replies; 16+ messages in thread
From: Ilya Leoshkevich @ 2022-02-09 2:17 UTC (permalink / raw)
To: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
Heiko Carstens, Vasily Gorbik, Christian Borntraeger,
Alexander Gordeev
Cc: bpf, Ilya Leoshkevich
Ensure that PT_REGS_SYSCALL_REGS works correctly.
Signed-off-by: Ilya Leoshkevich <iii@linux.ibm.com>
---
tools/testing/selftests/bpf/progs/bpf_syscall_macro.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/tools/testing/selftests/bpf/progs/bpf_syscall_macro.c b/tools/testing/selftests/bpf/progs/bpf_syscall_macro.c
index f5c6ef2ff6d1..e7c622cb6a39 100644
--- a/tools/testing/selftests/bpf/progs/bpf_syscall_macro.c
+++ b/tools/testing/selftests/bpf/progs/bpf_syscall_macro.c
@@ -33,7 +33,7 @@ int BPF_KPROBE(handle_sys_prctl)
if (pid != filter_pid)
return 0;
- real_regs = (struct pt_regs *)PT_REGS_PARM1(ctx);
+ real_regs = PT_REGS_SYSCALL_REGS(ctx);
/* test for PT_REGS_PARM */
--
2.34.1
^ permalink raw reply related [flat|nested] 16+ messages in thread
* [PATCH bpf-next v5 04/10] libbpf: Fix accessing syscall arguments on powerpc
2022-02-09 2:17 [PATCH bpf-next v5 00/10] Fix accessing syscall arguments Ilya Leoshkevich
` (2 preceding siblings ...)
2022-02-09 2:17 ` [PATCH bpf-next v5 03/10] selftests/bpf: Use PT_REGS_SYSCALL_REGS in bpf_syscall_macro Ilya Leoshkevich
@ 2022-02-09 2:17 ` Ilya Leoshkevich
2022-02-09 2:17 ` [PATCH bpf-next v5 05/10] libbpf: Fix riscv register names Ilya Leoshkevich
` (7 subsequent siblings)
11 siblings, 0 replies; 16+ messages in thread
From: Ilya Leoshkevich @ 2022-02-09 2:17 UTC (permalink / raw)
To: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
Heiko Carstens, Vasily Gorbik, Christian Borntraeger,
Alexander Gordeev
Cc: bpf, Ilya Leoshkevich, Naveen N . Rao
powerpc does not select ARCH_HAS_SYSCALL_WRAPPER, so its syscall
handlers take "unpacked" syscall arguments. Indicate this to libbpf
using PT_REGS_SYSCALL_REGS macro.
Reported-by: Heiko Carstens <hca@linux.ibm.com>
Tested-by: Naveen N. Rao <naveen.n.rao@linux.vnet.ibm.com>
Signed-off-by: Ilya Leoshkevich <iii@linux.ibm.com>
---
tools/lib/bpf/bpf_tracing.h | 2 ++
1 file changed, 2 insertions(+)
diff --git a/tools/lib/bpf/bpf_tracing.h b/tools/lib/bpf/bpf_tracing.h
index a5e92656bfba..20bc63770c9f 100644
--- a/tools/lib/bpf/bpf_tracing.h
+++ b/tools/lib/bpf/bpf_tracing.h
@@ -180,6 +180,8 @@
#define __PT_RC_REG gpr[3]
#define __PT_SP_REG sp
#define __PT_IP_REG nip
+/* powerpc does not select ARCH_HAS_SYSCALL_WRAPPER. */
+#define PT_REGS_SYSCALL_REGS(ctx) ctx
#elif defined(bpf_target_sparc)
--
2.34.1
^ permalink raw reply related [flat|nested] 16+ messages in thread
* [PATCH bpf-next v5 05/10] libbpf: Fix riscv register names
2022-02-09 2:17 [PATCH bpf-next v5 00/10] Fix accessing syscall arguments Ilya Leoshkevich
` (3 preceding siblings ...)
2022-02-09 2:17 ` [PATCH bpf-next v5 04/10] libbpf: Fix accessing syscall arguments on powerpc Ilya Leoshkevich
@ 2022-02-09 2:17 ` Ilya Leoshkevich
2022-02-09 2:17 ` [PATCH bpf-next v5 06/10] libbpf: Fix accessing syscall arguments on riscv Ilya Leoshkevich
` (6 subsequent siblings)
11 siblings, 0 replies; 16+ messages in thread
From: Ilya Leoshkevich @ 2022-02-09 2:17 UTC (permalink / raw)
To: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
Heiko Carstens, Vasily Gorbik, Christian Borntraeger,
Alexander Gordeev
Cc: bpf, Ilya Leoshkevich
riscv registers are accessed via struct user_regs_struct, not struct
pt_regs. The program counter member in this struct is called pc, not
epc. The frame pointer is called s0, not fp.
Fixes: 3cc31d794097 ("libbpf: Normalize PT_REGS_xxx() macro definitions")
Signed-off-by: Ilya Leoshkevich <iii@linux.ibm.com>
---
tools/lib/bpf/bpf_tracing.h | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/tools/lib/bpf/bpf_tracing.h b/tools/lib/bpf/bpf_tracing.h
index 20bc63770c9f..03e501ac8f60 100644
--- a/tools/lib/bpf/bpf_tracing.h
+++ b/tools/lib/bpf/bpf_tracing.h
@@ -210,10 +210,10 @@
#define __PT_PARM4_REG a3
#define __PT_PARM5_REG a4
#define __PT_RET_REG ra
-#define __PT_FP_REG fp
+#define __PT_FP_REG s0
#define __PT_RC_REG a5
#define __PT_SP_REG sp
-#define __PT_IP_REG epc
+#define __PT_IP_REG pc
#endif
--
2.34.1
^ permalink raw reply related [flat|nested] 16+ messages in thread
* [PATCH bpf-next v5 06/10] libbpf: Fix accessing syscall arguments on riscv
2022-02-09 2:17 [PATCH bpf-next v5 00/10] Fix accessing syscall arguments Ilya Leoshkevich
` (4 preceding siblings ...)
2022-02-09 2:17 ` [PATCH bpf-next v5 05/10] libbpf: Fix riscv register names Ilya Leoshkevich
@ 2022-02-09 2:17 ` Ilya Leoshkevich
2022-02-09 2:17 ` [PATCH bpf-next v5 07/10] selftests/bpf: Skip test_bpf_syscall_macro:syscall_arg1 on arm64 and s390 Ilya Leoshkevich
` (5 subsequent siblings)
11 siblings, 0 replies; 16+ messages in thread
From: Ilya Leoshkevich @ 2022-02-09 2:17 UTC (permalink / raw)
To: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
Heiko Carstens, Vasily Gorbik, Christian Borntraeger,
Alexander Gordeev
Cc: bpf, Ilya Leoshkevich
riscv does not select ARCH_HAS_SYSCALL_WRAPPER, so its syscall
handlers take "unpacked" syscall arguments. Indicate this to libbpf
using PT_REGS_SYSCALL_REGS macro.
Reported-by: Heiko Carstens <hca@linux.ibm.com>
Signed-off-by: Ilya Leoshkevich <iii@linux.ibm.com>
---
tools/lib/bpf/bpf_tracing.h | 2 ++
1 file changed, 2 insertions(+)
diff --git a/tools/lib/bpf/bpf_tracing.h b/tools/lib/bpf/bpf_tracing.h
index 03e501ac8f60..41a015ee6bfb 100644
--- a/tools/lib/bpf/bpf_tracing.h
+++ b/tools/lib/bpf/bpf_tracing.h
@@ -214,6 +214,8 @@
#define __PT_RC_REG a5
#define __PT_SP_REG sp
#define __PT_IP_REG pc
+/* riscv does not select ARCH_HAS_SYSCALL_WRAPPER. */
+#define PT_REGS_SYSCALL_REGS(ctx) ctx
#endif
--
2.34.1
^ permalink raw reply related [flat|nested] 16+ messages in thread
* [PATCH bpf-next v5 07/10] selftests/bpf: Skip test_bpf_syscall_macro:syscall_arg1 on arm64 and s390
2022-02-09 2:17 [PATCH bpf-next v5 00/10] Fix accessing syscall arguments Ilya Leoshkevich
` (5 preceding siblings ...)
2022-02-09 2:17 ` [PATCH bpf-next v5 06/10] libbpf: Fix accessing syscall arguments on riscv Ilya Leoshkevich
@ 2022-02-09 2:17 ` Ilya Leoshkevich
2022-02-09 2:17 ` [PATCH bpf-next v5 08/10] libbpf: Allow overriding PT_REGS_PARM1{_CORE}_SYSCALL Ilya Leoshkevich
` (4 subsequent siblings)
11 siblings, 0 replies; 16+ messages in thread
From: Ilya Leoshkevich @ 2022-02-09 2:17 UTC (permalink / raw)
To: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
Heiko Carstens, Vasily Gorbik, Christian Borntraeger,
Alexander Gordeev
Cc: bpf, Ilya Leoshkevich
These architectures can provide access to the first syscall argument
only through PT_REGS_PARM1_CORE_SYSCALL().
Signed-off-by: Ilya Leoshkevich <iii@linux.ibm.com>
---
.../testing/selftests/bpf/prog_tests/test_bpf_syscall_macro.c | 4 ++++
tools/testing/selftests/bpf/progs/bpf_syscall_macro.c | 4 +++-
2 files changed, 7 insertions(+), 1 deletion(-)
diff --git a/tools/testing/selftests/bpf/prog_tests/test_bpf_syscall_macro.c b/tools/testing/selftests/bpf/prog_tests/test_bpf_syscall_macro.c
index f5f4c8adf539..8bc58bda500d 100644
--- a/tools/testing/selftests/bpf/prog_tests/test_bpf_syscall_macro.c
+++ b/tools/testing/selftests/bpf/prog_tests/test_bpf_syscall_macro.c
@@ -33,7 +33,11 @@ void test_bpf_syscall_macro(void)
/* check whether args of syscall are copied correctly */
prctl(exp_arg1, exp_arg2, exp_arg3, exp_arg4, exp_arg5);
+#if defined(__aarch64__) || defined(__s390__)
+ ASSERT_NEQ(skel->bss->arg1, exp_arg1, "syscall_arg1");
+#else
ASSERT_EQ(skel->bss->arg1, exp_arg1, "syscall_arg1");
+#endif
ASSERT_EQ(skel->bss->arg2, exp_arg2, "syscall_arg2");
ASSERT_EQ(skel->bss->arg3, exp_arg3, "syscall_arg3");
/* it cannot copy arg4 when uses PT_REGS_PARM4 on x86_64 */
diff --git a/tools/testing/selftests/bpf/progs/bpf_syscall_macro.c b/tools/testing/selftests/bpf/progs/bpf_syscall_macro.c
index e7c622cb6a39..496e54d0ac22 100644
--- a/tools/testing/selftests/bpf/progs/bpf_syscall_macro.c
+++ b/tools/testing/selftests/bpf/progs/bpf_syscall_macro.c
@@ -28,7 +28,7 @@ int BPF_KPROBE(handle_sys_prctl)
{
struct pt_regs *real_regs;
pid_t pid = bpf_get_current_pid_tgid() >> 32;
- unsigned long tmp;
+ unsigned long tmp = 0;
if (pid != filter_pid)
return 0;
@@ -37,7 +37,9 @@ int BPF_KPROBE(handle_sys_prctl)
/* test for PT_REGS_PARM */
+#if !defined(bpf_target_arm64) && !defined(bpf_target_s390)
bpf_probe_read_kernel(&tmp, sizeof(tmp), &PT_REGS_PARM1_SYSCALL(real_regs));
+#endif
arg1 = tmp;
bpf_probe_read_kernel(&arg2, sizeof(arg2), &PT_REGS_PARM2_SYSCALL(real_regs));
bpf_probe_read_kernel(&arg3, sizeof(arg3), &PT_REGS_PARM3_SYSCALL(real_regs));
--
2.34.1
^ permalink raw reply related [flat|nested] 16+ messages in thread
* [PATCH bpf-next v5 08/10] libbpf: Allow overriding PT_REGS_PARM1{_CORE}_SYSCALL
2022-02-09 2:17 [PATCH bpf-next v5 00/10] Fix accessing syscall arguments Ilya Leoshkevich
` (6 preceding siblings ...)
2022-02-09 2:17 ` [PATCH bpf-next v5 07/10] selftests/bpf: Skip test_bpf_syscall_macro:syscall_arg1 on arm64 and s390 Ilya Leoshkevich
@ 2022-02-09 2:17 ` Ilya Leoshkevich
2022-02-09 5:39 ` Andrii Nakryiko
2022-02-09 2:17 ` [PATCH bpf-next v5 09/10] libbpf: Fix accessing the first syscall argument on arm64 Ilya Leoshkevich
` (3 subsequent siblings)
11 siblings, 1 reply; 16+ messages in thread
From: Ilya Leoshkevich @ 2022-02-09 2:17 UTC (permalink / raw)
To: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
Heiko Carstens, Vasily Gorbik, Christian Borntraeger,
Alexander Gordeev
Cc: bpf, Ilya Leoshkevich
arm64 and s390 need a special way to access the first syscall argument.
Signed-off-by: Ilya Leoshkevich <iii@linux.ibm.com>
---
tools/lib/bpf/bpf_tracing.h | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/tools/lib/bpf/bpf_tracing.h b/tools/lib/bpf/bpf_tracing.h
index 41a015ee6bfb..f364f1f4710e 100644
--- a/tools/lib/bpf/bpf_tracing.h
+++ b/tools/lib/bpf/bpf_tracing.h
@@ -269,7 +269,9 @@ struct pt_regs;
#endif
+#ifndef PT_REGS_PARM1_SYSCALL
#define PT_REGS_PARM1_SYSCALL(x) PT_REGS_PARM1(x)
+#endif
#define PT_REGS_PARM2_SYSCALL(x) PT_REGS_PARM2(x)
#define PT_REGS_PARM3_SYSCALL(x) PT_REGS_PARM3(x)
#ifdef __PT_PARM4_REG_SYSCALL
@@ -279,7 +281,9 @@ struct pt_regs;
#endif
#define PT_REGS_PARM5_SYSCALL(x) PT_REGS_PARM5(x)
+#ifndef PT_REGS_PARM1_CORE_SYSCALL
#define PT_REGS_PARM1_CORE_SYSCALL(x) PT_REGS_PARM1_CORE(x)
+#endif
#define PT_REGS_PARM2_CORE_SYSCALL(x) PT_REGS_PARM2_CORE(x)
#define PT_REGS_PARM3_CORE_SYSCALL(x) PT_REGS_PARM3_CORE(x)
#ifdef __PT_PARM4_REG_SYSCALL
--
2.34.1
^ permalink raw reply related [flat|nested] 16+ messages in thread
* Re: [PATCH bpf-next v5 08/10] libbpf: Allow overriding PT_REGS_PARM1{_CORE}_SYSCALL
2022-02-09 2:17 ` [PATCH bpf-next v5 08/10] libbpf: Allow overriding PT_REGS_PARM1{_CORE}_SYSCALL Ilya Leoshkevich
@ 2022-02-09 5:39 ` Andrii Nakryiko
0 siblings, 0 replies; 16+ messages in thread
From: Andrii Nakryiko @ 2022-02-09 5:39 UTC (permalink / raw)
To: Ilya Leoshkevich
Cc: Alexei Starovoitov, Daniel Borkmann, Heiko Carstens,
Vasily Gorbik, Christian Borntraeger, Alexander Gordeev, bpf
On Tue, Feb 8, 2022 at 6:18 PM Ilya Leoshkevich <iii@linux.ibm.com> wrote:
>
> arm64 and s390 need a special way to access the first syscall argument.
>
> Signed-off-by: Ilya Leoshkevich <iii@linux.ibm.com>
> ---
> tools/lib/bpf/bpf_tracing.h | 4 ++++
> 1 file changed, 4 insertions(+)
>
> diff --git a/tools/lib/bpf/bpf_tracing.h b/tools/lib/bpf/bpf_tracing.h
> index 41a015ee6bfb..f364f1f4710e 100644
> --- a/tools/lib/bpf/bpf_tracing.h
> +++ b/tools/lib/bpf/bpf_tracing.h
> @@ -269,7 +269,9 @@ struct pt_regs;
>
> #endif
>
> +#ifndef PT_REGS_PARM1_SYSCALL
> #define PT_REGS_PARM1_SYSCALL(x) PT_REGS_PARM1(x)
> +#endif
> #define PT_REGS_PARM2_SYSCALL(x) PT_REGS_PARM2(x)
> #define PT_REGS_PARM3_SYSCALL(x) PT_REGS_PARM3(x)
> #ifdef __PT_PARM4_REG_SYSCALL
> @@ -279,7 +281,9 @@ struct pt_regs;
> #endif
> #define PT_REGS_PARM5_SYSCALL(x) PT_REGS_PARM5(x)
>
> +#ifndef PT_REGS_PARM1_CORE_SYSCALL
> #define PT_REGS_PARM1_CORE_SYSCALL(x) PT_REGS_PARM1_CORE(x)
> +#endif
> #define PT_REGS_PARM2_CORE_SYSCALL(x) PT_REGS_PARM2_CORE(x)
> #define PT_REGS_PARM3_CORE_SYSCALL(x) PT_REGS_PARM3_CORE(x)
> #ifdef __PT_PARM4_REG_SYSCALL
I've changed PARM4 handling to be the same as for PARM1 for
consistency (and flexibility, if we ever need as much)
> --
> 2.34.1
>
^ permalink raw reply [flat|nested] 16+ messages in thread
* [PATCH bpf-next v5 09/10] libbpf: Fix accessing the first syscall argument on arm64
2022-02-09 2:17 [PATCH bpf-next v5 00/10] Fix accessing syscall arguments Ilya Leoshkevich
` (7 preceding siblings ...)
2022-02-09 2:17 ` [PATCH bpf-next v5 08/10] libbpf: Allow overriding PT_REGS_PARM1{_CORE}_SYSCALL Ilya Leoshkevich
@ 2022-02-09 2:17 ` Ilya Leoshkevich
2022-02-09 5:39 ` Andrii Nakryiko
2022-02-09 2:17 ` [PATCH bpf-next v5 10/10] libbpf: Fix accessing the first syscall argument on s390 Ilya Leoshkevich
` (2 subsequent siblings)
11 siblings, 1 reply; 16+ messages in thread
From: Ilya Leoshkevich @ 2022-02-09 2:17 UTC (permalink / raw)
To: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
Heiko Carstens, Vasily Gorbik, Christian Borntraeger,
Alexander Gordeev
Cc: bpf, Ilya Leoshkevich
On arm64, the first syscall argument should be accessed via orig_x0
(see arch/arm64/include/asm/syscall.h). Currently regs[0] is used
instead, leading to bpf_syscall_macro test failure.
orig_x0 cannot be added to struct user_pt_regs, since its layout is a
part of the ABI. Therefore provide access to it only through
PT_REGS_PARM1_CORE_SYSCALL() by using a struct pt_regs flavor.
Reported-by: Heiko Carstens <hca@linux.ibm.com>
Signed-off-by: Ilya Leoshkevich <iii@linux.ibm.com>
---
tools/lib/bpf/bpf_tracing.h | 10 ++++++++++
1 file changed, 10 insertions(+)
diff --git a/tools/lib/bpf/bpf_tracing.h b/tools/lib/bpf/bpf_tracing.h
index f364f1f4710e..928f85f7961c 100644
--- a/tools/lib/bpf/bpf_tracing.h
+++ b/tools/lib/bpf/bpf_tracing.h
@@ -142,8 +142,18 @@
#elif defined(bpf_target_arm64)
+struct pt_regs___arm64 {
+ unsigned long orig_x0;
+} __attribute__((preserve_access_index));
+
/* arm64 provides struct user_pt_regs instead of struct pt_regs to userspace */
#define __PT_REGS_CAST(x) ((const struct user_pt_regs *)(x))
+#define PT_REGS_PARM1_SYSCALL(x) ({ \
+ _Pragma("GCC error \"PT_REGS_PARM1_SYSCALL() is not supported on arm64, use PT_REGS_PARM1_CORE_SYSCALL() instead\""); \
+ 0l; \
+})
+#define PT_REGS_PARM1_CORE_SYSCALL(x) \
+ BPF_CORE_READ((const struct pt_regs___arm64 *)(x), orig_x0)
#define __PT_PARM1_REG regs[0]
#define __PT_PARM2_REG regs[1]
#define __PT_PARM3_REG regs[2]
--
2.34.1
^ permalink raw reply related [flat|nested] 16+ messages in thread
* Re: [PATCH bpf-next v5 09/10] libbpf: Fix accessing the first syscall argument on arm64
2022-02-09 2:17 ` [PATCH bpf-next v5 09/10] libbpf: Fix accessing the first syscall argument on arm64 Ilya Leoshkevich
@ 2022-02-09 5:39 ` Andrii Nakryiko
0 siblings, 0 replies; 16+ messages in thread
From: Andrii Nakryiko @ 2022-02-09 5:39 UTC (permalink / raw)
To: Ilya Leoshkevich
Cc: Alexei Starovoitov, Daniel Borkmann, Heiko Carstens,
Vasily Gorbik, Christian Borntraeger, Alexander Gordeev, bpf
On Tue, Feb 8, 2022 at 6:18 PM Ilya Leoshkevich <iii@linux.ibm.com> wrote:
>
> On arm64, the first syscall argument should be accessed via orig_x0
> (see arch/arm64/include/asm/syscall.h). Currently regs[0] is used
> instead, leading to bpf_syscall_macro test failure.
>
> orig_x0 cannot be added to struct user_pt_regs, since its layout is a
> part of the ABI. Therefore provide access to it only through
> PT_REGS_PARM1_CORE_SYSCALL() by using a struct pt_regs flavor.
>
> Reported-by: Heiko Carstens <hca@linux.ibm.com>
> Signed-off-by: Ilya Leoshkevich <iii@linux.ibm.com>
> ---
> tools/lib/bpf/bpf_tracing.h | 10 ++++++++++
> 1 file changed, 10 insertions(+)
>
> diff --git a/tools/lib/bpf/bpf_tracing.h b/tools/lib/bpf/bpf_tracing.h
> index f364f1f4710e..928f85f7961c 100644
> --- a/tools/lib/bpf/bpf_tracing.h
> +++ b/tools/lib/bpf/bpf_tracing.h
> @@ -142,8 +142,18 @@
>
> #elif defined(bpf_target_arm64)
>
> +struct pt_regs___arm64 {
> + unsigned long orig_x0;
> +} __attribute__((preserve_access_index));
> +
I just realized that this will probably break anyone who's using old
Clang to compile a non-CORE BPF program because preserve_access_index
attribute will be unknown.
But we don't have to use __attribute__((preserve_access_index)) here,
because we use BPF_CORE_READ() in those macro, which will make
accesses CO-RE-relocatable anyways. So I dropped
__attribute__((preserve_access_index)) for better backwards
compatibility.
> /* arm64 provides struct user_pt_regs instead of struct pt_regs to userspace */
> #define __PT_REGS_CAST(x) ((const struct user_pt_regs *)(x))
> +#define PT_REGS_PARM1_SYSCALL(x) ({ \
> + _Pragma("GCC error \"PT_REGS_PARM1_SYSCALL() is not supported on arm64, use PT_REGS_PARM1_CORE_SYSCALL() instead\""); \
> + 0l; \
> +})
I shortened message to just "use PT_REGS_PARM1_CORE_SYSCALL() instead"
and made it into a single-liner
> +#define PT_REGS_PARM1_CORE_SYSCALL(x) \
> + BPF_CORE_READ((const struct pt_regs___arm64 *)(x), orig_x0)
also made this into a single-liner
> #define __PT_PARM1_REG regs[0]
> #define __PT_PARM2_REG regs[1]
> #define __PT_PARM3_REG regs[2]
> --
> 2.34.1
>
^ permalink raw reply [flat|nested] 16+ messages in thread
* [PATCH bpf-next v5 10/10] libbpf: Fix accessing the first syscall argument on s390
2022-02-09 2:17 [PATCH bpf-next v5 00/10] Fix accessing syscall arguments Ilya Leoshkevich
` (8 preceding siblings ...)
2022-02-09 2:17 ` [PATCH bpf-next v5 09/10] libbpf: Fix accessing the first syscall argument on arm64 Ilya Leoshkevich
@ 2022-02-09 2:17 ` Ilya Leoshkevich
2022-02-09 5:39 ` Andrii Nakryiko
2022-02-09 5:39 ` [PATCH bpf-next v5 00/10] Fix accessing syscall arguments Andrii Nakryiko
2022-02-09 5:40 ` patchwork-bot+netdevbpf
11 siblings, 1 reply; 16+ messages in thread
From: Ilya Leoshkevich @ 2022-02-09 2:17 UTC (permalink / raw)
To: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
Heiko Carstens, Vasily Gorbik, Christian Borntraeger,
Alexander Gordeev
Cc: bpf, Ilya Leoshkevich, Andrii Nakryiko
On s390, the first syscall argument should be accessed via orig_gpr2
(see arch/s390/include/asm/syscall.h). Currently gpr[2] is used
instead, leading to bpf_syscall_macro test failure.
orig_gpr2 cannot be added to user_pt_regs, since its layout is a part
of the ABI. Therefore provide access to it only through
PT_REGS_PARM1_CORE_SYSCALL() by using a struct pt_regs flavor.
Reported-by: Andrii Nakryiko <andrii@kernel.org>
Signed-off-by: Ilya Leoshkevich <iii@linux.ibm.com>
---
tools/lib/bpf/bpf_tracing.h | 10 ++++++++++
1 file changed, 10 insertions(+)
diff --git a/tools/lib/bpf/bpf_tracing.h b/tools/lib/bpf/bpf_tracing.h
index 928f85f7961c..0e0414801457 100644
--- a/tools/lib/bpf/bpf_tracing.h
+++ b/tools/lib/bpf/bpf_tracing.h
@@ -114,9 +114,19 @@
#elif defined(bpf_target_s390)
+struct pt_regs___s390 {
+ unsigned long orig_gpr2;
+} __attribute__((preserve_access_index));
+
/* s390 provides user_pt_regs instead of struct pt_regs to userspace */
#define __PT_REGS_CAST(x) ((const user_pt_regs *)(x))
#define __PT_PARM1_REG gprs[2]
+#define PT_REGS_PARM1_SYSCALL(x) ({ \
+ _Pragma("GCC error \"PT_REGS_PARM1_SYSCALL() is not supported on s390, use PT_REGS_PARM1_CORE_SYSCALL() instead\""); \
+ 0l; \
+})
+#define PT_REGS_PARM1_CORE_SYSCALL(x) \
+ BPF_CORE_READ((const struct pt_regs___s390 *)(x), orig_gpr2)
#define __PT_PARM2_REG gprs[3]
#define __PT_PARM3_REG gprs[4]
#define __PT_PARM4_REG gprs[5]
--
2.34.1
^ permalink raw reply related [flat|nested] 16+ messages in thread
* Re: [PATCH bpf-next v5 10/10] libbpf: Fix accessing the first syscall argument on s390
2022-02-09 2:17 ` [PATCH bpf-next v5 10/10] libbpf: Fix accessing the first syscall argument on s390 Ilya Leoshkevich
@ 2022-02-09 5:39 ` Andrii Nakryiko
0 siblings, 0 replies; 16+ messages in thread
From: Andrii Nakryiko @ 2022-02-09 5:39 UTC (permalink / raw)
To: Ilya Leoshkevich
Cc: Alexei Starovoitov, Daniel Borkmann, Heiko Carstens,
Vasily Gorbik, Christian Borntraeger, Alexander Gordeev, bpf,
Andrii Nakryiko
On Tue, Feb 8, 2022 at 6:18 PM Ilya Leoshkevich <iii@linux.ibm.com> wrote:
>
> On s390, the first syscall argument should be accessed via orig_gpr2
> (see arch/s390/include/asm/syscall.h). Currently gpr[2] is used
> instead, leading to bpf_syscall_macro test failure.
>
> orig_gpr2 cannot be added to user_pt_regs, since its layout is a part
> of the ABI. Therefore provide access to it only through
> PT_REGS_PARM1_CORE_SYSCALL() by using a struct pt_regs flavor.
>
> Reported-by: Andrii Nakryiko <andrii@kernel.org>
> Signed-off-by: Ilya Leoshkevich <iii@linux.ibm.com>
> ---
> tools/lib/bpf/bpf_tracing.h | 10 ++++++++++
> 1 file changed, 10 insertions(+)
>
> diff --git a/tools/lib/bpf/bpf_tracing.h b/tools/lib/bpf/bpf_tracing.h
> index 928f85f7961c..0e0414801457 100644
> --- a/tools/lib/bpf/bpf_tracing.h
> +++ b/tools/lib/bpf/bpf_tracing.h
> @@ -114,9 +114,19 @@
>
> #elif defined(bpf_target_s390)
>
> +struct pt_regs___s390 {
> + unsigned long orig_gpr2;
> +} __attribute__((preserve_access_index));
> +
> /* s390 provides user_pt_regs instead of struct pt_regs to userspace */
> #define __PT_REGS_CAST(x) ((const user_pt_regs *)(x))
> #define __PT_PARM1_REG gprs[2]
> +#define PT_REGS_PARM1_SYSCALL(x) ({ \
> + _Pragma("GCC error \"PT_REGS_PARM1_SYSCALL() is not supported on s390, use PT_REGS_PARM1_CORE_SYSCALL() instead\""); \
> + 0l; \
> +})
> +#define PT_REGS_PARM1_CORE_SYSCALL(x) \
> + BPF_CORE_READ((const struct pt_regs___s390 *)(x), orig_gpr2)
same manipulations as on previous patch
> #define __PT_PARM2_REG gprs[3]
> #define __PT_PARM3_REG gprs[4]
> #define __PT_PARM4_REG gprs[5]
> --
> 2.34.1
>
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [PATCH bpf-next v5 00/10] Fix accessing syscall arguments
2022-02-09 2:17 [PATCH bpf-next v5 00/10] Fix accessing syscall arguments Ilya Leoshkevich
` (9 preceding siblings ...)
2022-02-09 2:17 ` [PATCH bpf-next v5 10/10] libbpf: Fix accessing the first syscall argument on s390 Ilya Leoshkevich
@ 2022-02-09 5:39 ` Andrii Nakryiko
2022-02-09 5:40 ` patchwork-bot+netdevbpf
11 siblings, 0 replies; 16+ messages in thread
From: Andrii Nakryiko @ 2022-02-09 5:39 UTC (permalink / raw)
To: Ilya Leoshkevich
Cc: Alexei Starovoitov, Daniel Borkmann, Heiko Carstens,
Vasily Gorbik, Christian Borntraeger, Alexander Gordeev, bpf
On Tue, Feb 8, 2022 at 6:17 PM Ilya Leoshkevich <iii@linux.ibm.com> wrote:
>
> libbpf now has macros to access syscall arguments in an
> architecture-agnostic manner, but unfortunately they have a number of
> issues on non-Intel arches, which this series aims to fix.
>
> v1: https://lore.kernel.org/bpf/20220201234200.1836443-1-iii@linux.ibm.com/
> v1 -> v2:
> * Put orig_gpr2 in place of args[1] on s390 (Vasily).
> * Fix arm64, powerpc and riscv (Heiko).
>
> v2: https://lore.kernel.org/bpf/20220204041955.1958263-1-iii@linux.ibm.com/
> v2 -> v3:
> * Undo args[1] change (Andrii).
> * Rename PT_REGS_SYSCALL to PT_REGS_SYSCALL_REGS (Andrii).
> * Split the riscv patch (Andrii).
>
> v3: https://lore.kernel.org/bpf/20220204145018.1983773-1-iii@linux.ibm.com/
> v3 -> v4:
> * Undo arm64's and s390's user_pt_regs changes.
> * Use struct pt_regs when vmlinux.h is available (Andrii).
> * Use offsetofend for accessing orig_gpr2 and orig_x0 (Andrii).
> * Move libbpf's copy of offsetofend to a new header.
> * Fix riscv's __PT_FP_REG.
> * Use PT_REGS_SYSCALL_REGS in test_probe_user.c.
> * Test bpf_syscall_macro with userspace headers.
> * Use Naveen's suggestions and code in patches 5 and 6.
> * Add warnings to arm64's and s390's ptrace.h (Andrii).
>
> v4: https://lore.kernel.org/bpf/20220208051635.2160304-1-iii@linux.ibm.com/
> v4 -> v5:
> * Go back to v3.
> * Do not touch arch headers.
> * Use CO-RE struct flavors to access orig_x0 and orig_gpr2.
> * Fail compilation if non-CO-RE macros are used to access the first
> syscall parameter on arm64 and s390.
> * Fix accessing frame pointer on riscv.
>
> Ilya Leoshkevich (10):
> selftests/bpf: Fix an endianness issue in bpf_syscall_macro test
> libbpf: Add PT_REGS_SYSCALL_REGS macro
> selftests/bpf: Use PT_REGS_SYSCALL_REGS in bpf_syscall_macro
> libbpf: Fix accessing syscall arguments on powerpc
> libbpf: Fix riscv register names
> libbpf: Fix accessing syscall arguments on riscv
> selftests/bpf: Skip test_bpf_syscall_macro:syscall_arg1 on arm64 and
> s390
> libbpf: Allow overriding PT_REGS_PARM1{_CORE}_SYSCALL
> libbpf: Fix accessing the first syscall argument on arm64
> libbpf: Fix accessing the first syscall argument on s390
>
Applied to bpf-next with few adjustments in patches 8-10, thanks. I'll
sync all this to Github shortly to run it through s390x CI tests as
well.
> tools/lib/bpf/bpf_tracing.h | 42 ++++++++++++++++++-
> .../bpf/prog_tests/test_bpf_syscall_macro.c | 4 ++
> .../selftests/bpf/progs/bpf_syscall_macro.c | 9 +++-
> 3 files changed, 51 insertions(+), 4 deletions(-)
>
> --
> 2.34.1
>
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [PATCH bpf-next v5 00/10] Fix accessing syscall arguments
2022-02-09 2:17 [PATCH bpf-next v5 00/10] Fix accessing syscall arguments Ilya Leoshkevich
` (10 preceding siblings ...)
2022-02-09 5:39 ` [PATCH bpf-next v5 00/10] Fix accessing syscall arguments Andrii Nakryiko
@ 2022-02-09 5:40 ` patchwork-bot+netdevbpf
11 siblings, 0 replies; 16+ messages in thread
From: patchwork-bot+netdevbpf @ 2022-02-09 5:40 UTC (permalink / raw)
To: Ilya Leoshkevich
Cc: ast, daniel, andrii.nakryiko, hca, gor, borntraeger, agordeev, bpf
Hello:
This series was applied to bpf/bpf-next.git (master)
by Andrii Nakryiko <andrii@kernel.org>:
On Wed, 9 Feb 2022 03:17:35 +0100 you wrote:
> libbpf now has macros to access syscall arguments in an
> architecture-agnostic manner, but unfortunately they have a number of
> issues on non-Intel arches, which this series aims to fix.
>
> v1: https://lore.kernel.org/bpf/20220201234200.1836443-1-iii@linux.ibm.com/
> v1 -> v2:
> * Put orig_gpr2 in place of args[1] on s390 (Vasily).
> * Fix arm64, powerpc and riscv (Heiko).
>
> [...]
Here is the summary with links:
- [bpf-next,v5,01/10] selftests/bpf: Fix an endianness issue in bpf_syscall_macro test
https://git.kernel.org/bpf/bpf-next/c/4fc49b51ab9d
- [bpf-next,v5,02/10] libbpf: Add PT_REGS_SYSCALL_REGS macro
https://git.kernel.org/bpf/bpf-next/c/c5a1ffa0da76
- [bpf-next,v5,03/10] selftests/bpf: Use PT_REGS_SYSCALL_REGS in bpf_syscall_macro
https://git.kernel.org/bpf/bpf-next/c/3f928cab927c
- [bpf-next,v5,04/10] libbpf: Fix accessing syscall arguments on powerpc
https://git.kernel.org/bpf/bpf-next/c/f07f1503469b
- [bpf-next,v5,05/10] libbpf: Fix riscv register names
https://git.kernel.org/bpf/bpf-next/c/5c101153bfd6
- [bpf-next,v5,06/10] libbpf: Fix accessing syscall arguments on riscv
https://git.kernel.org/bpf/bpf-next/c/cf0b5b276923
- [bpf-next,v5,07/10] selftests/bpf: Skip test_bpf_syscall_macro:syscall_arg1 on arm64 and s390
https://git.kernel.org/bpf/bpf-next/c/9e45a377f29b
- [bpf-next,v5,08/10] libbpf: Allow overriding PT_REGS_PARM1{_CORE}_SYSCALL
https://git.kernel.org/bpf/bpf-next/c/60d16c5ccb81
- [bpf-next,v5,09/10] libbpf: Fix accessing the first syscall argument on arm64
https://git.kernel.org/bpf/bpf-next/c/fbca4a2f6497
- [bpf-next,v5,10/10] libbpf: Fix accessing the first syscall argument on s390
https://git.kernel.org/bpf/bpf-next/c/1f22a6f9f9a0
You are awesome, thank you!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html
^ permalink raw reply [flat|nested] 16+ messages in thread