libbpf: Fix accessing syscall arguments
v3 cover libbpf: Fix accessing syscall arguments
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/[email protected]/
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/[email protected]/
v2 -> v3:
* Undo args[1] change (Andrii).
* Rename PT_REGS_SYSCALL to PT_REGS_SYSCALL_REGS (Andrii).
* Split the riscv patch (Andrii).
+cc Naveen.
Ilya Leoshkevich (11):
arm64/bpf: Add orig_x0 to user_pt_regs
s390/bpf: Add orig_gpr2 to user_pt_regs selftests/bpf: Fix an endianness issue in bpf_syscall_macro test
libbpf: Add __PT_PARM1_REG_SYSCALL macro
libbpf: Add PT_REGS_SYSCALL_REGS macro
selftests/bpf: Use PT_REGS_SYSCALL_REGS in bpf_syscall_macro
libbpf: Fix accessing the first syscall argument on arm64
libbpf: Fix accessing syscall arguments on powerpc
libbpf: Fix accessing program counter on riscv
libbpf: Fix accessing syscall arguments on riscv
libbpf: Fix accessing the first syscall argument on s390
arch/arm64/include/asm/ptrace.h | 2 +- arch/arm64/include/uapi/asm/ptrace.h | 1 + arch/s390/include/asm/ptrace.h | 2 +- arch/s390/include/uapi/asm/ptrace.h | 1 + tools/lib/bpf/bpf_tracing.h | 23 ++++++++++++++++++- .../selftests/bpf/progs/bpf_syscall_macro.c | 7 ++++-- 6 files changed, 31 insertions(+), 5 deletions(-)
Signature · 1 line
2.34.1
PATCH v3 1/11 arm64/bpf: Add orig_x0 to user_pt_regs
orig_x0 is needed in order to access the first syscall argument from eBPF programs.
--- arch/arm64/include/asm/ptrace.h | 2 +- arch/arm64/include/uapi/asm/ptrace.h | 1 + 2 files changed, 2 insertions(+), 1 deletion(-)
arch/arm64/include/asm/ptrace.h +1 -1
@@ -183,9 +183,9 @@ struct pt_regs {u64 sp;u64 pc;u64 pstate; +u64 orig_x0; }; }; -u64 orig_x0;#ifdef __AARCH64EB__u32 unused2;s32 syscallno;
arch/arm64/include/uapi/asm/ptrace.h +1 -0
@@ -90,6 +90,7 @@ struct user_pt_regs {__u64 sp;__u64 pc;__u64 pstate; +__u64 orig_x0; };struct user_fpsimd_state {
Signature · 1 line
2.34.1
PATCH v3 2/11 s390/bpf: Add orig_gpr2 to user_pt_regs
In reply to Ilya Leoshkevichuser_pt_regs is used by eBPF in order to access userspace registers - see commit 466698e654e8 ("s390/bpf: correct broken uapi for BPF_PROG_TYPE_PERF_EVENT program type"). In order to access the first syscall argument from eBPF programs, we need to export orig_gpr2.
--- arch/s390/include/asm/ptrace.h | 2 +- arch/s390/include/uapi/asm/ptrace.h | 1 + 2 files changed, 2 insertions(+), 1 deletion(-)
arch/s390/include/asm/ptrace.h +1 -1
@@ -83,9 +83,9 @@ struct pt_regs {unsigned long args[1 ];psw_t psw;unsigned long gprs[NUM_GPRS ]; +unsigned long orig_gpr2; }; }; -unsigned long orig_gpr2; union { struct { unsigned int int_code;
arch/s390/include/uapi/asm/ptrace.h +1 -0
@@ -295,6 +295,7 @@ typedef struct {unsigned long args[1 ];psw_t psw;unsigned long gprs[NUM_GPRS ]; +unsigned long orig_gpr2; } user_pt_regs;/*
Signature · 1 line
2.34.1
Re: [PATCH bpf-next v3 02/11] s390/bpf: Add orig_gpr2 to user_pt_regs
Quoted 10 lines · Ilya Leoshkevich
user_pt_regsis used by eBPF in order to access userspace registers - see commit466698e654e8("s390/bpf: correct broken uapi forBPF_PROG_TYPE_PERF_EVENTprogram type"). In order to access the first syscall argument from eBPF programs, we need to exportorig_gpr2.Signed-off-by Ilya Leoshkevich <[email protected]>--- arch/s390/include/asm/ptrace.h | 2 +- arch/s390/include/uapi/asm/ptrace.h | 1 + 2 files changed, 2 insertions(+), 1 deletion(-)
Re: [PATCH bpf-next v3 02/11] s390/bpf: Add orig_gpr2 to user_pt_regs
In reply to Ilya LeoshkevichQuoted 10 lines · Ilya Leoshkevich
user_pt_regsis used by eBPF in order to access userspace registers - see commit466698e654e8("s390/bpf: correct broken uapi forBPF_PROG_TYPE_PERF_EVENTprogram type"). In order to access the first syscall argument from eBPF programs, we need to exportorig_gpr2.Signed-off-by Ilya Leoshkevich <[email protected]>--- arch/s390/include/asm/ptrace.h | 2 +- arch/s390/include/uapi/asm/ptrace.h | 1 + 2 files changed, 2 insertions(+), 1 deletion(-)
PATCH v3 3/11 selftests/bpf: Fix an endianness issue in bpf_syscall_macro test
In reply to Ilya Leoshkevichbpf_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.
--- tools/testing/selftests/bpf/progs/bpf_syscall_macro.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
tools/testing/selftests/bpf/progs/bpf_syscall_macro.c +4 -1
@@ -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));
Signature · 1 line
2.34.1
PATCH v3 4/11 libbpf: Add __PT_PARM1_REG_SYSCALL macro
In reply to Ilya LeoshkevichSome architectures have a special way to access the first syscall argument. There already exists __PT_PARM4_REG_SYSCALL for the fourth argument, so define a similar macro for the first one.
--- tools/lib/bpf/bpf_tracing.h | 8 ++++++++ 1 file changed, 8 insertions(+)
tools/lib/bpf/bpf_tracing.h +8 -0
@@ -265,7 +265,11 @@ struct pt_regs;#endif +#ifdef __PT_PARM1_REG_SYSCALL +#define PT_REGS_PARM1_SYSCALL (x) (__PT_REGS_CAST(x)->__PT_PARM1_REG_SYSCALL) +#else /* __PT_PARM1_REG_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
@@ -275,7 +279,11 @@ struct pt_regs;#endif #define PT_REGS_PARM5_SYSCALL (x) PT_REGS_PARM5(x) +#ifdef __PT_PARM1_REG_SYSCALL +#define PT_REGS_PARM1_CORE_SYSCALL (x) BPF_CORE_READ(__PT_REGS_CAST(x), __PT_PARM1_REG_SYSCALL) +#else /* __PT_PARM1_REG_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
Signature · 1 line
2.34.1
Re: [PATCH bpf-next v3 04/11] libbpf: Add __PT_PARM1_REG_SYSCALL macro
Quoted 27 lines · Ilya Leoshkevich
Some architectures have a special way to access the first syscall argument. There already exists
__PT_PARM4_REG_SYSCALLfor the fourth argument, so define a similar macro for the first one.Signed-off-by Ilya Leoshkevich <[email protected]>--- tools/lib/bpf/bpf_tracing.h | 8 ++++++++ 1 file changed, 8 insertions(+)tools/lib/bpf/bpf_tracing.h +4
-0@@ -265,7 +265,11 @@ struct pt_regs;#endif +#ifdef __PT_PARM1_REG_SYSCALL +#define PT_REGS_PARM1_SYSCALL (x) (__PT_REGS_CAST(x)->__PT_PARM1_REG_SYSCALL) +#else /* __PT_PARM1_REG_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@@ -275,7 +279,11 @@ struct pt_regs;#endif
tools/lib/bpf/bpf_tracing.h struct pt_regs; · line 289
Quoted 10 lines
tools/lib/bpf/bpf_tracing.h +4
-0#define PT_REGS_PARM5_SYSCALL (x) PT_REGS_PARM5(x) +#ifdef __PT_PARM1_REG_SYSCALL +#define PT_REGS_PARM1_CORE_SYSCALL (x) BPF_CORE_READ(__PT_REGS_CAST(x), __PT_PARM1_REG_SYSCALL) +#else /* __PT_PARM1_REG_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
When I was contemplating implementing this for powerpc, I came up with the below patch which looked cleaner to me, and also makes it easy for architectures to over-ride any other syscall parameter in future. Feel free to include this in your series if it makes sense.
- Naveen
--
libbpf: Generalize overriding syscall parameter access macros
Instead of conditionally overriding PT_REGS_PARM4_SYSCALL, provide default fallback for all _REG_SYSCALL macros so that architectures can simply override a specific syscall parameter macro.
---tools/lib/bpf/bpf_tracing.h | 40 ++++++++++++++++++++++---------------
1 file changed, 24 insertions(+), 16 deletions(-)
tools/lib/bpf/bpf_tracing.h +0 -0
@@ -265,25 +265,33 @@ struct pt_regs;
#endif
-#define PT_REGS_PARM1_SYSCALL(x) PT_REGS_PARM1(x)
-#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
-#define PT_REGS_PARM4_SYSCALL(x) (__PT_REGS_CAST(x)->__PT_PARM4_REG_SYSCALL) -#else /* __PT_PARM4_REG_SYSCALL */
-#define PT_REGS_PARM4_SYSCALL(x) PT_REGS_PARM4(x)
+#ifndef __PT_PARM1_REG_SYSCALL
+#define __PT_PARM1_REG_SYSCALL __PT_PARM1_REG
+#endif
+#ifndef __PT_PARM2_REG_SYSCALL
+#define __PT_PARM2_REG_SYSCALL __PT_PARM2_REG
+#endif
+#ifndef __PT_PARM3_REG_SYSCALL
+#define __PT_PARM3_REG_SYSCALL __PT_PARM3_REG
+#endif
+#ifndef __PT_PARM4_REG_SYSCALL
+#define __PT_PARM4_REG_SYSCALL __PT_PARM4_REG
#endif
-#define PT_REGS_PARM5_SYSCALL(x) PT_REGS_PARM5(x)
+#ifndef __PT_PARM5_REG_SYSCALL
+#define __PT_PARM5_REG_SYSCALL __PT_PARM5_REG
+#endif
+
+#define PT_REGS_PARM1_SYSCALL(x) (__PT_REGS_CAST(x)->__PT_PARM1_REG_SYSCALL) +#define PT_REGS_PARM2_SYSCALL(x) (__PT_REGS_CAST(x)->__PT_PARM2_REG_SYSCALL) +#define PT_REGS_PARM3_SYSCALL(x) (__PT_REGS_CAST(x)->__PT_PARM3_REG_SYSCALL) +#define PT_REGS_PARM4_SYSCALL(x) (__PT_REGS_CAST(x)->__PT_PARM4_REG_SYSCALL) +#define PT_REGS_PARM5_SYSCALL(x) (__PT_REGS_CAST(x)->__PT_PARM5_REG_SYSCALL)
-#define PT_REGS_PARM1_CORE_SYSCALL(x) PT_REGS_PARM1_CORE(x)
-#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
+#define PT_REGS_PARM1_CORE_SYSCALL(x) BPF_CORE_READ(__PT_REGS_CAST(x), __PT_PARM1_REG_SYSCALL) +#define PT_REGS_PARM2_CORE_SYSCALL(x) BPF_CORE_READ(__PT_REGS_CAST(x), __PT_PARM2_REG_SYSCALL) +#define PT_REGS_PARM3_CORE_SYSCALL(x) BPF_CORE_READ(__PT_REGS_CAST(x), __PT_PARM3_REG_SYSCALL) #define PT_REGS_PARM4_CORE_SYSCALL(x) BPF_CORE_READ(__PT_REGS_CAST(x), __PT_PARM4_REG_SYSCALL) -#else /* __PT_PARM4_REG_SYSCALL */
-#define PT_REGS_PARM4_CORE_SYSCALL(x) PT_REGS_PARM4_CORE(x)
-#endif
-#define PT_REGS_PARM5_CORE_SYSCALL(x) PT_REGS_PARM5_CORE(x)
+#define PT_REGS_PARM5_CORE_SYSCALL(x) BPF_CORE_READ(__PT_REGS_CAST(x), __PT_PARM5_REG_SYSCALL)
#else /* defined(bpf_target_defined) */
Signature · 1 line
2.34.1
PATCH v3 5/11 libbpf: Add PT_REGS_SYSCALL_REGS macro
In reply to Ilya LeoshkevichSome architectures 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, using passing pt_regs * as a default.
--- tools/lib/bpf/bpf_tracing.h | 9 +++++++++ 1 file changed, 9 insertions(+)
tools/lib/bpf/bpf_tracing.h +9 -0
@@ -334,6 +334,15 @@ 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 +#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
Signature · 1 line
2.34.1
Re: [PATCH bpf-next v3 05/11] libbpf: Add PT_REGS_SYSCALL_REGS macro
Quoted 2 lines · Ilya Leoshkevich
Some architectures pass a pointer to struct
pt_regsto syscall handlers, others unpack it into individual function parameters.
I think that is just dependent on ARCH_HAS_SYSCALL_WRAPPER, so only x86, arm64 and s390 pass pointers to pt_regs to syscall entry points.
Quoted 15 lines
Introduce a macro to describe what a particular arch does, using
passing pt_regs *as a default.Signed-off-by Ilya Leoshkevich <[email protected]>--- tools/lib/bpf/bpf_tracing.h | 9 +++++++++ 1 file changed, 9 insertions(+)tools/lib/bpf/bpf_tracing.h +0
-0@@ -334,6 +334,15 @@ struct pt_regs;#endif /* defined(bpf_target_defined) */
tools/lib/bpf/bpf_tracing.h struct pt_regs; · line 345
Quoted 10 lines
tools/lib/bpf/bpf_tracing.h +9
-0+/* +* 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 +#define PT_REGS_SYSCALL_REGS (ctx) ((struct pt_regs *)PT_REGS_PARM1(ctx)) +#endif +
I think that name is misleading if an architecture doesn't implement syscall wrappers, since you are simply getting access to the kprobe pt_regs, rather than the syscall pt_regs. This can perhaps be named PT_REGS_SYSCALL_UNWRAP() or such to make that clear.
Also, should this just be keyed off a simpler HAS_SYSCALL_WRAPPER or such, rather than the other way around?
tools/lib/bpf/bpf_tracing.h +0 -0
@@ -110,6 +110,8 @@
#endif /* __i386__ */
+#define HAS_SYSCALL_WRAPPER
+
#endif /* __KERNEL__ || __VMLINUX_H__ */
#elif defined(bpf_target_s390)
@@ -126,6 +128,7 @@
#define __PT_RC_REG gprs[2]
#define __PT_SP_REG gprs[15]
#define __PT_IP_REG psw.addr
+#define HAS_SYSCALL_WRAPPER
#elif defined(bpf_target_arm)
@@ -154,6 +157,7 @@
#define __PT_RC_REG regs[0]
#define __PT_SP_REG sp
#define __PT_IP_REG pc
+#define HAS_SYSCALL_WRAPPER
#elif defined(bpf_target_mips)
We can then simply do:
#ifdef HAS_SYSCALL_WRAPPER
#define PT_REGS_SYSCALL_UNWRAP(ctx) ((struct pt_regs *)PT_REGS_PARM1(ctx)) #else
#define PT_REGS_SYSCALL_unwRAP(ctx) ((struct pt_regs *)(ctx)) #endif
Taking this a bit further, it would be nice if we can fold in progs/bpf_misc.h into bpf_traching.h by also including SYS_PREFIX.
- Naveen
Re: [PATCH bpf-next v3 05/11] libbpf: Add PT_REGS_SYSCALL_REGS macro
Quoted 24 lines · Naveen N. Rao
Quoted 2 lines · Ilya Leoshkevich
Some architectures pass a pointer to struct
pt_regsto syscall handlers, others unpack it into individual function parameters.I think that is just dependent on
ARCH_HAS_SYSCALL_WRAPPER, so only x86, arm64 and s390 pass pointers topt_regsto syscall entry points.Quoted 16 lines
Introduce a macro to describe what a particular arch does, using
passing pt_regs *as a default.Signed-off-by Ilya Leoshkevich <[email protected]>--- tools/lib/bpf/bpf_tracing.h | 9 +++++++++ 1 file changed, 9 insertions(+)tools/lib/bpf/bpf_tracing.h +0
-0@@ -334,6 +334,15 @@ struct pt_regs;#endif /* defined(bpf_target_defined) */
Quoted 14 lines
tools/lib/bpf/bpf_tracing.h struct pt_regs; · line 345
Quoted 9 lines
tools/lib/bpf/bpf_tracing.h +9
-0+/* +* 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 +#define PT_REGS_SYSCALL_REGS (ctx) ((struct pt_regs *)PT_REGS_PARM1(ctx)) +#endif +I think that name is misleading if an architecture doesn't implement syscall wrappers, since you are simply getting access to the kprobe
pt_regs, rather than the syscallpt_regs. This can perhaps be namedPT_REGS_SYSCALL_UNWRAP()or such to make that clear.
UNWRAP implies that there is something to unwrap, always. In case of s390x, for example, there is nothing to unwrap. So I think PT_REGS_SYSCALL_REGS() makes more sense, it just fetches correct pt_regs to work with to get syscall input arguments (and it might be exactly the same pt_regs that are passed in).
I think in practice most users won't ever have to use this, as we'll add BPF_KPROBE_SYSCALL() macro, similar to BPF_KPROBE that we have now, but specific to syscall kprobe.
Quoted 3 lines
Also, should this just be keyed off a simpler
HAS_SYSCALL_WRAPPERor such, rather than the other way around?
I think the way Ilya did it is totally fine.
Quoted 40 lines
tools/lib/bpf/bpf_tracing.h +4
-0@@ -110,6 +110,8 @@#endif /* __i386__ */ +#define HAS_SYSCALL_WRAPPER +#endif /* __KERNEL__ || __VMLINUX_H__ */ #elif defined(bpf_target_s390)@@ -126,6 +128,7 @@#define __PT_RC_REG gprs[2]#define __PT_SP_REG gprs[15]#define __PT_IP_REG psw.addr +#define HAS_SYSCALL_WRAPPER #elif defined(bpf_target_arm)@@ -154,6 +157,7 @@#define __PT_RC_REG regs[0]#define __PT_SP_REG sp#define __PT_IP_REG pc +#define HAS_SYSCALL_WRAPPER #elif defined(bpf_target_mips)We can then simply do:
#ifdef
HAS_SYSCALL_WRAPPER
#definePT_REGS_SYSCALL_UNWRAP(ctx)((structpt_regs*)PT_REGS_PARM1(ctx)) #else
#definePT_REGS_SYSCALL_unwRAP(ctx)((structpt_regs*)(ctx)) #endif
Quoted 2 lines
Taking this a bit further, it would be nice if we can fold in
progs/bpf_misc.hintobpf_traching.hby also includingSYS_PREFIX.
As far as I know, SYS_PREFIX depends not just on architecture but also on kernel version (older versions of x86-64 kernels didn't need that prefix). For selftests, given they follow the latest version of kernel it's ok to always append SYS_PREFIX, but generally speaking for user BPF apps, they would need to be more careful and check whether they need SYS_PREFIX or not. So I don't want to add SYS_PREFIX to bpf_tracing.h because it's misleading.
Quoted 4 lines
- Naveen
Re: [PATCH bpf-next v3 05/11] libbpf: Add PT_REGS_SYSCALL_REGS macro
Quoted 47 lines · Andrii Nakryiko
Quoted 24 lines · Naveen N. Rao
Quoted 2 lines · Ilya Leoshkevich
Some architectures pass a pointer to struct
pt_regsto syscall handlers, others unpack it into individual function parameters.I think that is just dependent on
ARCH_HAS_SYSCALL_WRAPPER, so only x86, arm64 and s390 pass pointers topt_regsto syscall entry points.Quoted 16 lines
Introduce a macro to describe what a particular arch does, using
passing pt_regs *as a default.Signed-off-by Ilya Leoshkevich <[email protected]>--- tools/lib/bpf/bpf_tracing.h | 9 +++++++++ 1 file changed, 9 insertions(+)tools/lib/bpf/bpf_tracing.h +0
-0@@ -334,6 +334,15 @@ struct pt_regs;#endif /* defined(bpf_target_defined) */ Quoted 14 lines
tools/lib/bpf/bpf_tracing.h struct pt_regs; · line 345
Quoted 9 lines
tools/lib/bpf/bpf_tracing.h +9
-0+/* +* 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 +#define PT_REGS_SYSCALL_REGS (ctx) ((struct pt_regs *)PT_REGS_PARM1(ctx)) +#endif +I think that name is misleading if an architecture doesn't implement syscall wrappers, since you are simply getting access to the kprobe
pt_regs, rather than the syscallpt_regs. This can perhaps be namedPT_REGS_SYSCALL_UNWRAP()or such to make that clear.UNWRAP implies that there is something to unwrap, always. In case of s390x, for example, there is nothing to unwrap. So I think
PT_REGS_SYSCALL_REGS()makes more sense, it just fetches correctpt_regsto work with to get syscall input arguments (and it might be exactly the samept_regsthat are passed in).
Quoted 3 lines
I think in practice most users won't ever have to use this, as we'll add
BPF_KPROBE_SYSCALL()macro, similar toBPF_KPROBEthat we have now, but specific to syscall kprobe.
That will be very nice.
Quoted 47 lines
Quoted 4 lines
Also, should this just be keyed off a simpler
HAS_SYSCALL_WRAPPERor such, rather than the other way around?I think the way Ilya did it is totally fine.
Quoted 40 lines
tools/lib/bpf/bpf_tracing.h +4
-0@@ -110,6 +110,8 @@#endif /* __i386__ */ +#define HAS_SYSCALL_WRAPPER +#endif /* __KERNEL__ || __VMLINUX_H__ */ #elif defined(bpf_target_s390)@@ -126,6 +128,7 @@#define __PT_RC_REG gprs[2]#define __PT_SP_REG gprs[15]#define __PT_IP_REG psw.addr +#define HAS_SYSCALL_WRAPPER #elif defined(bpf_target_arm)@@ -154,6 +157,7 @@#define __PT_RC_REG regs[0]#define __PT_SP_REG sp#define __PT_IP_REG pc +#define HAS_SYSCALL_WRAPPER #elif defined(bpf_target_mips)We can then simply do:
#ifdef
HAS_SYSCALL_WRAPPER
#definePT_REGS_SYSCALL_UNWRAP(ctx)((structpt_regs*)PT_REGS_PARM1(ctx)) #else
#definePT_REGS_SYSCALL_unwRAP(ctx)((structpt_regs*)(ctx)) #endif
Quoted 10 lines
Quoted 2 lines
Taking this a bit further, it would be nice if we can fold in
progs/bpf_misc.hintobpf_traching.hby also includingSYS_PREFIX.As far as I know,
SYS_PREFIXdepends not just on architecture but also on kernel version (older versions of x86-64 kernels didn't need that prefix). For selftests, given they follow the latest version of kernel it's ok to always appendSYS_PREFIX, but generally speaking for user BPF apps, they would need to be more careful and check whether they needSYS_PREFIXor not. So I don't want to addSYS_PREFIXtobpf_tracing.hbecause it's misleading.
That makes sense, thanks.
- Naveen
PATCH v3 6/11 selftests/bpf: Use PT_REGS_SYSCALL_REGS in bpf_syscall_macro
In reply to Ilya LeoshkevichEnsure that PT_REGS_SYSCALL_REGS works correctly.
--- tools/testing/selftests/bpf/progs/bpf_syscall_macro.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/bpf/progs/bpf_syscall_macro.c +1 -1
@@ -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 */
Signature · 1 line
2.34.1
PATCH v3 7/11 libbpf: Fix accessing the first syscall argument on arm64
In reply to Ilya LeoshkevichOn 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.
--- tools/lib/bpf/bpf_tracing.h | 1 + 1 file changed, 1 insertion(+)
tools/lib/bpf/bpf_tracing.h +1 -0
@@ -145,6 +145,7 @@/* 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_PARM1_REG regs[0] +#define __PT_PARM1_REG_SYSCALL orig_x0#define __PT_PARM2_REG regs[1]#define __PT_PARM3_REG regs[2]#define __PT_PARM4_REG regs[3]
Signature · 1 line
2.34.1
PATCH v3 8/11 libbpf: Fix accessing syscall arguments on powerpc
In reply to Ilya Leoshkevichpowerpc's syscall handlers get "unpacked" arguments instead of a struct pt_regs pointer. Indicate this to libbpf using PT_REGS_SYSCALL_REGS macro.
--- tools/lib/bpf/bpf_tracing.h | 1 + 1 file changed, 1 insertion(+)
tools/lib/bpf/bpf_tracing.h +1 -0
@@ -181,6 +181,7 @@#define __PT_RC_REG gpr[3]#define __PT_SP_REG sp#define __PT_IP_REG nip +#define PT_REGS_SYSCALL_REGS (ctx) ctx#elif defined(bpf_target_sparc)
Signature · 1 line
2.34.1
Re: [PATCH bpf-next v3 08/11] libbpf: Fix accessing syscall arguments on powerpc
Quoted 9 lines · Ilya Leoshkevich
powerpc's syscall handlers get "unpacked" arguments instead of a struct
pt_regspointer. Indicate this to libbpf usingPT_REGS_SYSCALL_REGSmacro.Reported-by Heiko Carstens <[email protected]>Signed-off-by Ilya Leoshkevich <[email protected]>--- tools/lib/bpf/bpf_tracing.h | 1 + 1 file changed, 1 insertion(+)
Quoted 17 lines
tools/lib/bpf/bpf_tracing.h +1
-0@@ -181,6 +181,7 @@#define __PT_RC_REG gpr[3]#define __PT_SP_REG sp#define __PT_IP_REG nip +#define PT_REGS_SYSCALL_REGS (ctx) ctx#elif defined(bpf_target_sparc)Signature · 1 line
2.34.1
PATCH v3 9/11 libbpf: Fix accessing program counter on riscv
In reply to Ilya Leoshkevichriscv registers are accessed via struct user_regs_struct, not struct pt_regs. The program counter member in this struct is called pc, not epc.
--- tools/lib/bpf/bpf_tracing.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/lib/bpf/bpf_tracing.h +1 -1
@@ -213,7 +213,7 @@#define __PT_FP_REG fp#define __PT_RC_REG a5#define __PT_SP_REG sp -#define __PT_IP_REG epc +#define __PT_IP_REG pc#endif
Signature · 1 line
2.34.1
PATCH v3 10/11 libbpf: Fix accessing syscall arguments on riscv
In reply to Ilya Leoshkevichriscv's syscall handlers get "unpacked" arguments instead of a struct pt_regs pointer. Indicate this to libbpf using PT_REGS_SYSCALL_REGS macro.
--- tools/lib/bpf/bpf_tracing.h | 1 + 1 file changed, 1 insertion(+)
tools/lib/bpf/bpf_tracing.h +1 -0
@@ -214,6 +214,7 @@#define __PT_RC_REG a5#define __PT_SP_REG sp#define __PT_IP_REG pc +#define PT_REGS_SYSCALL_REGS (ctx) ctx#endif
Signature · 1 line
2.34.1
PATCH v3 11/11 libbpf: Fix accessing the first syscall argument on s390
In reply to Ilya LeoshkevichOn 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.
--- tools/lib/bpf/bpf_tracing.h | 1 + 1 file changed, 1 insertion(+)
tools/lib/bpf/bpf_tracing.h +1 -0
@@ -117,6 +117,7 @@/* 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_PARM1_REG_SYSCALL orig_gpr2#define __PT_PARM2_REG gprs[3]#define __PT_PARM3_REG gprs[4]#define __PT_PARM4_REG gprs[5]
Signature · 1 line
2.34.1
This series was applied to bpf/bpf-next.git (master) by Andrii Nakryiko <[email protected]>:
Hello:
This series was applied to bpf/bpf-next.git (master) by Andrii Nakryiko <[email protected]>:
Quoted 10 lines · On Fri, 4 Feb 2022 15:50:07 +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/[email protected]/
v1 -> v2:
* Putorig_gpr2in place of args[1] on s390 (Vasily).
* Fix arm64, powerpc and riscv (Heiko).[...]
Here is the summary with links:
- [bpf-next,v3,01/11] arm64/bpf: Add orig_x0 to user_pt_regs
https://git.kernel.org/bpf/bpf-next/c/d473f4062165 - [bpf-next,v3,02/11] s390/bpf: Add orig_gpr2 to user_pt_regs https://git.kernel.org/bpf/bpf-next/c/61f88e88f263 - [bpf-next,v3,03/11] selftests/bpf: Fix an endianness issue in bpf_syscall_macro test https://git.kernel.org/bpf/bpf-next/c/a936c141cbe4 - [bpf-next,v3,04/11] libbpf: Add __PT_PARM1_REG_SYSCALL macro https://git.kernel.org/bpf/bpf-next/c/3a9d84aafb8c - [bpf-next,v3,05/11] libbpf: Add PT_REGS_SYSCALL_REGS macro https://git.kernel.org/bpf/bpf-next/c/b62a862d42f5 - [bpf-next,v3,06/11] selftests/bpf: Use PT_REGS_SYSCALL_REGS in bpf_syscall_macro https://git.kernel.org/bpf/bpf-next/c/730809c15ac2 - [bpf-next,v3,07/11] libbpf: Fix accessing the first syscall argument on arm64 https://git.kernel.org/bpf/bpf-next/c/8b9b06ad4726 - [bpf-next,v3,08/11] libbpf: Fix accessing syscall arguments on powerpc https://git.kernel.org/bpf/bpf-next/c/f5af16d0ae28 - [bpf-next,v3,09/11] libbpf: Fix accessing program counter on riscv https://git.kernel.org/bpf/bpf-next/c/27870c91b5c7 - [bpf-next,v3,10/11] libbpf: Fix accessing syscall arguments on riscv https://git.kernel.org/bpf/bpf-next/c/5860b82236c6 - [bpf-next,v3,11/11] libbpf: Fix accessing the first syscall argument on s390 https://git.kernel.org/bpf/bpf-next/c/088d6aafd5bb
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html
Quoted 33 lines · On Sat, Feb 5, 2022 at 12:30 PM <[email protected]> wrote:
Hello:
This series was applied to bpf/bpf-next.git (master) by Andrii Nakryiko <[email protected]>:
Quoted 10 lines · On Fri, 4 Feb 2022 15:50:07 +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/[email protected]/
v1 -> v2:
* Putorig_gpr2in place of args[1] on s390 (Vasily).
* Fix arm64, powerpc and riscv (Heiko).[...]
Here is the summary with links:
- [bpf-next,v3,01/11] arm64/bpf: Addorig_x0touser_pt_regshttps://git.kernel.org/bpf/bpf-next/c/d473f4062165 - [bpf-next,v3,02/11] s390/bpf: Add orig_gpr2 to user_pt_regs https://git.kernel.org/bpf/bpf-next/c/61f88e88f263 - [bpf-next,v3,03/11] selftests/bpf: Fix an endianness issue in bpf_syscall_macro test https://git.kernel.org/bpf/bpf-next/c/a936c141cbe4 - [bpf-next,v3,04/11] libbpf: Add __PT_PARM1_REG_SYSCALL macro https://git.kernel.org/bpf/bpf-next/c/3a9d84aafb8c - [bpf-next,v3,05/11] libbpf: Add PT_REGS_SYSCALL_REGS macro https://git.kernel.org/bpf/bpf-next/c/b62a862d42f5 - [bpf-next,v3,06/11] selftests/bpf: Use PT_REGS_SYSCALL_REGS in bpf_syscall_macro https://git.kernel.org/bpf/bpf-next/c/730809c15ac2 - [bpf-next,v3,07/11] libbpf: Fix accessing the first syscall argument on arm64 https://git.kernel.org/bpf/bpf-next/c/8b9b06ad4726
Quoted 9 lines
- [bpf-next,v3,08/11] libbpf: Fix accessing syscall arguments on powerpc
https://git.kernel.org/bpf/bpf-next/c/f5af16d0ae28 - [bpf-next,v3,09/11] libbpf: Fix accessing program counter on riscv https://git.kernel.org/bpf/bpf-next/c/27870c91b5c7 - [bpf-next,v3,10/11] libbpf: Fix accessing syscall arguments on riscv https://git.kernel.org/bpf/bpf-next/c/5860b82236c6 - [bpf-next,v3,11/11] libbpf: Fix accessing the first syscall argument on s390 https://git.kernel.org/bpf/bpf-next/c/088d6aafd5bb
I've backed out this patch set from bpf-next for now while we are deciding the best way to deal with s390x and arm64 arches without breaking UAPIs.
Quoted 6 lines
You are awesome, thank you!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html