Skip to contentSkip to conversation

selftests/bpf: fix compiling loop{1,2,3}.c on s390

PATCH Superseded Ilya Leoshkevich 3 participants 6 messages Jul 2, 2019 – Jul 3, 2019

Subsystems (5)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 selftests/bpf: fix compiling loop{1,2,3}.c on s390

Use PT_REGS_RC(ctx) instead of ctx->rax, which is not present on s390.

Pass -D__TARGET_ARCH_$(ARCH) to selftests in order to choose a proper PT_REGS_RC variant.

Fix s930 -> s390 typo.

On s390, provide the forward declaration of struct pt_regs and cast it to user_pt_regs in PT_REGS_* macros. This is necessary, because instead of the full struct pt_regs, s390 exposes only its first field user_pt_regs to userspace, and bpf_helpers.h is used with both userspace (in selftests) and kernel (in samples) headers.

On x86, provide userspace versions of PT_REGS_* macros. Unlike s390, x86 provides struct pt_regs to both userspace and kernel, however, with different field names.

---
 tools/testing/selftests/bpf/Makefile      |  4 +-
 tools/testing/selftests/bpf/bpf_helpers.h | 46 +++++++++++++++--------
 tools/testing/selftests/bpf/progs/loop1.c |  2 +-
 tools/testing/selftests/bpf/progs/loop2.c |  2 +-
 tools/testing/selftests/bpf/progs/loop3.c |  2 +-
 5 files changed, 37 insertions(+), 19 deletions(-)
tools/testing/selftests/bpf/Makefile +3 -1
index d60fee59fbd1..599b320bef65 100644
@@ -1,5 +1,6 @@
 # SPDX-License-Identifier: GPL-2.0
 include ../../../../scripts/Kbuild.include
+include ../../../scripts/Makefile.arch
 
 LIBDIR := ../../../lib
 BPFDIR := $(LIBDIR)/bpf
@@ -138,7 +139,8 @@ CLANG_SYS_INCLUDES := $(shell $(CLANG) -v -E - </dev/null 2>&1 \
 
 CLANG_FLAGS = -I. -I./include/uapi -I../../../include/uapi \
 	      $(CLANG_SYS_INCLUDES) \
-	      -Wno-compare-distinct-pointer-types
+	      -Wno-compare-distinct-pointer-types \
+	      -D__TARGET_ARCH_$(ARCH)
 
 $(OUTPUT)/test_l4lb_noinline.o: CLANG_FLAGS += -fno-inline
 $(OUTPUT)/test_xdp_noinline.o: CLANG_FLAGS += -fno-inline
tools/testing/selftests/bpf/bpf_helpers.h +31 -15

72 lines not on this page · raw message

tools/testing/selftests/bpf/progs/loop1.c +1 -1
index dea395af9ea9..7cdb7f878310 100644
@@ -18,7 +18,7 @@ int nested_loops(volatile struct pt_regs* ctx)
 	for (j = 0; j < 300; j++)
 		for (i = 0; i < j; i++) {
 			if (j & 1)
-				m = ctx->rax;
+				m = PT_REGS_RC(ctx);
 			else
 				m = j;
 			sum += i * m;
tools/testing/selftests/bpf/progs/loop2.c +1 -1
index 0637bd8e8bcf..9b2f808a2863 100644
@@ -16,7 +16,7 @@ int while_true(volatile struct pt_regs* ctx)
 	int i = 0;
 
 	while (true) {
-		if (ctx->rax & 1)
+		if (PT_REGS_RC(ctx) & 1)
 			i += 3;
 		else
 			i += 7;
tools/testing/selftests/bpf/progs/loop3.c +1 -1
index 30a0f6cba080..d727657d51e2 100644
@@ -16,7 +16,7 @@ int while_true(volatile struct pt_regs* ctx)
 	__u64 i = 0, sum = 0;
 	do {
 		i++;
-		sum += ctx->rax;
+		sum += PT_REGS_RC(ctx);
 	} while (i < 0x100000000ULL);
 	return sum;
 }
Signature · 1 line
2.21.0
#1 lore raw
Quoted 64 lines · Ilya Leoshkevich

Use PT_REGS_RC(ctx) instead of ctx->rax, which is not present on s390.

Pass -D__TARGET_ARCH_$(ARCH) to selftests in order to choose a proper PT_REGS_RC variant.

Fix s930 -> s390 typo.

On s390, provide the forward declaration of struct pt_regs and cast it to user_pt_regs in PT_REGS_* macros. This is necessary, because instead of the full struct pt_regs, s390 exposes only its first field user_pt_regs to userspace, and bpf_helpers.h is used with both userspace (in selftests) and kernel (in samples) headers.

On x86, provide userspace versions of PT_REGS_* macros. Unlike s390, x86 provides struct pt_regs to both userspace and kernel, however, with different field names.

---
 tools/testing/selftests/bpf/Makefile      |  4 +-
 tools/testing/selftests/bpf/bpf_helpers.h | 46 +++++++++++++++--------
 tools/testing/selftests/bpf/progs/loop1.c |  2 +-
 tools/testing/selftests/bpf/progs/loop2.c |  2 +-
 tools/testing/selftests/bpf/progs/loop3.c |  2 +-
 5 files changed, 37 insertions(+), 19 deletions(-)
tools/testing/selftests/bpf/Makefile +3 -1
index d60fee59fbd1..599b320bef65 100644
@@ -1,5 +1,6 @@
 # SPDX-License-Identifier: GPL-2.0
 include ../../../../scripts/Kbuild.include
+include ../../../scripts/Makefile.arch
 
 LIBDIR := ../../../lib
 BPFDIR := $(LIBDIR)/bpf
@@ -138,7 +139,8 @@ CLANG_SYS_INCLUDES := $(shell $(CLANG) -v -E - </dev/null 2>&1 \
 
 CLANG_FLAGS = -I. -I./include/uapi -I../../../include/uapi \
              $(CLANG_SYS_INCLUDES) \
-             -Wno-compare-distinct-pointer-types
+             -Wno-compare-distinct-pointer-types \
+             -D__TARGET_ARCH_$(ARCH)
 
 $(OUTPUT)/test_l4lb_noinline.o: CLANG_FLAGS += -fno-inline
 $(OUTPUT)/test_xdp_noinline.o: CLANG_FLAGS += -fno-inline
tools/testing/selftests/bpf/bpf_helpers.h +2 -2
index 1a5b1accf091..faf86d83301a 100644
@@ -312,8 +312,8 @@ static int (*bpf_skb_adjust_room)(void *ctx, __s32 len_diff, __u32 mode,
 #if defined(__TARGET_ARCH_x86)
        #define bpf_target_x86
        #define bpf_target_defined
-#elif defined(__TARGET_ARCH_s930x)
-       #define bpf_target_s930x
+#elif defined(__TARGET_ARCH_s390)
+       #define bpf_target_s390
        #define bpf_target_defined
 #elif defined(__TARGET_ARCH_arm)
        #define bpf_target_arm

tools/testing/selftests/bpf/bpf_helpers.h static int (*bpf_skb_adjust_room)(void *ctx, __s32 len_diff, __u32 mode, · line 342

Quoted 5 lines
tools/testing/selftests/bpf/bpf_helpers.h +0 -2
@@ -338,8 +338,8 @@ static int (*bpf_skb_adjust_room)(void *ctx, __s32 len_diff, __u32 mode,
 #ifndef bpf_target_defined
 #if defined(__x86_64__)
        #define bpf_target_x86
-#elif defined(__s390x__)
-       #define bpf_target_s930x

I see in some other places (e.g., bcc) where
macro __s390x__ is also used to indicate a s390 architecture. Could you explain the difference between __s390__ and __s390x__?

tools/testing/selftests/bpf/bpf_helpers.h static int (*bpf_skb_adjust_room)(void *ctx, __s32 len_diff, __u32 mode, · line 358

Quoted 10 lines
tools/testing/selftests/bpf/bpf_helpers.h +3 -0
+#elif defined(__s390__)
+       #define bpf_target_s390
 #elif defined(__arm__)
        #define bpf_target_arm
 #elif defined(__aarch64__)
@@ -355,6 +355,7 @@ static int (*bpf_skb_adjust_room)(void *ctx, __s32 len_diff, __u32 mode,
 
 #if defined(bpf_target_x86)
 
+#ifdef __KERNEL__

In samples/bpf/, __KERNEL__ is defined at clang options and in selftests/bpf/, the __KERNEL__ is not defined.

I checked x86 pt_regs definition with and without __KERNEL__. They are identical except some register name difference. I am wondering whether we can unify into all without __KERNEL__. Is __KERNEL__ really needed?

Quoted 36 lines
tools/testing/selftests/bpf/bpf_helpers.h +16 -11
 #define PT_REGS_PARM1(x) ((x)->di)
 #define PT_REGS_PARM2(x) ((x)->si)
 #define PT_REGS_PARM3(x) ((x)->dx)
@@ -365,19 +366,34 @@ static int (*bpf_skb_adjust_room)(void *ctx, __s32 len_diff, __u32 mode,
 #define PT_REGS_RC(x) ((x)->ax)
 #define PT_REGS_SP(x) ((x)->sp)
 #define PT_REGS_IP(x) ((x)->ip)
+#else
+#define PT_REGS_PARM1(x) ((x)->rdi)
+#define PT_REGS_PARM2(x) ((x)->rsi)
+#define PT_REGS_PARM3(x) ((x)->rdx)
+#define PT_REGS_PARM4(x) ((x)->rcx)
+#define PT_REGS_PARM5(x) ((x)->r8)
+#define PT_REGS_RET(x) ((x)->rsp)
+#define PT_REGS_FP(x) ((x)->rbp)
+#define PT_REGS_RC(x) ((x)->rax)
+#define PT_REGS_SP(x) ((x)->rsp)
+#define PT_REGS_IP(x) ((x)->rip)
+#endif
 
-#elif defined(bpf_target_s390x)
+#elif defined(bpf_target_s390)
 
-#define PT_REGS_PARM1(x) ((x)->gprs[2])
-#define PT_REGS_PARM2(x) ((x)->gprs[3])
-#define PT_REGS_PARM3(x) ((x)->gprs[4])
-#define PT_REGS_PARM4(x) ((x)->gprs[5])
-#define PT_REGS_PARM5(x) ((x)->gprs[6])
-#define PT_REGS_RET(x) ((x)->gprs[14])
-#define PT_REGS_FP(x) ((x)->gprs[11]) /* Works only with CONFIG_FRAME_POINTER */
-#define PT_REGS_RC(x) ((x)->gprs[2])
-#define PT_REGS_SP(x) ((x)->gprs[15])
-#define PT_REGS_IP(x) ((x)->psw.addr)
+/* s390 provides user_pt_regs instead of struct pt_regs to userspace */
+struct pt_regs;
+#define PT_REGS_PARM1(x) (((const volatile user_pt_regs *)(x))->gprs[2])

tools/testing/selftests/bpf/bpf_helpers.h static int (*bpf_skb_adjust_room)(void *ctx, __s32 len_diff, __u32 mode, · line 396

Quoted 10 lines
tools/testing/selftests/bpf/bpf_helpers.h +10 -0
+#define PT_REGS_PARM2(x) (((const volatile user_pt_regs *)(x))->gprs[3])
+#define PT_REGS_PARM3(x) (((const volatile user_pt_regs *)(x))->gprs[4])
+#define PT_REGS_PARM4(x) (((const volatile user_pt_regs *)(x))->gprs[5])
+#define PT_REGS_PARM5(x) (((const volatile user_pt_regs *)(x))->gprs[6])
+#define PT_REGS_RET(x) (((const volatile user_pt_regs *)(x))->gprs[14])
+/* Works only with CONFIG_FRAME_POINTER */
+#define PT_REGS_FP(x) (((const volatile user_pt_regs *)(x))->gprs[11])
+#define PT_REGS_RC(x) (((const volatile user_pt_regs *)(x))->gprs[2])
+#define PT_REGS_SP(x) (((const volatile user_pt_regs *)(x))->gprs[15])
+#define PT_REGS_IP(x) (((const volatile user_pt_regs *)(x))->psw.addr)

Is user_pt_regs a recent change or has been there for quite some time? I am asking since bcc did not use user_pt_regs yet.

Quoted 45 lines
tools/testing/selftests/bpf/bpf_helpers.h +0 -0
 #elif defined(bpf_target_arm)
 
tools/testing/selftests/bpf/progs/loop1.c +1 -1
index dea395af9ea9..7cdb7f878310 100644
@@ -18,7 +18,7 @@ int nested_loops(volatile struct pt_regs* ctx)
        for (j = 0; j < 300; j++)
                for (i = 0; i < j; i++) {
                        if (j & 1)
-                               m = ctx->rax;
+                               m = PT_REGS_RC(ctx);
                        else
                                m = j;
                        sum += i * m;
tools/testing/selftests/bpf/progs/loop2.c +1 -1
index 0637bd8e8bcf..9b2f808a2863 100644
@@ -16,7 +16,7 @@ int while_true(volatile struct pt_regs* ctx)
        int i = 0;
 
        while (true) {
-               if (ctx->rax & 1)
+               if (PT_REGS_RC(ctx) & 1)
                        i += 3;
                else
                        i += 7;
tools/testing/selftests/bpf/progs/loop3.c +1 -2
index 30a0f6cba080..d727657d51e2 100644
@@ -16,7 +16,7 @@ int while_true(volatile struct pt_regs* ctx)
        __u64 i = 0, sum = 0;
        do {
                i++;
-               sum += ctx->rax;
+               sum += PT_REGS_RC(ctx);
        } while (i < 0x100000000ULL);
        return sum;
 }
--

2.21.0

#2 lore raw
Quoted 11 lines

Am 02.07.2019 um 18:42 schrieb Y Song <[email protected]>:

tools/testing/selftests/bpf/bpf_helpers.h static int (*bpf_skb_adjust_room)(void *ctx, __s32 len_diff, __u32 mode, · line 342

Quoted 3 lines · Ilya Leoshkevich
tools/testing/selftests/bpf/bpf_helpers.h +0 -2
-#elif defined(__s390x__)
-       #define bpf_target_s930x

I see in some other places (e.g., bcc) where
macro __s390x__ is also used to indicate a s390 architecture. Could you explain the difference between __s390__ and __s390x__?

__s390__ is defined for 32-bit and 64-bit variants, __s390x__ is defined for 64-bit variant only.

Quoted 11 lines
Quoted 3 lines

#if defined(bpf_target_x86)

+#ifdef __KERNEL__

In samples/bpf/, __KERNEL__ is defined at clang options and in selftests/bpf/, the __KERNEL__ is not defined.

I checked x86 pt_regs definition with and without __KERNEL__. They are identical except some register name difference. I am wondering whether we can unify into all without __KERNEL__. Is __KERNEL__ really needed?

Right now removing it causes the build to fail, but the errors look fixable. However, I wonder whether there is a plan regarding this:
should eBPF programs be built with user headers, kernel headers, or both? Status quo appears to be "both", so I’ve decided to stick with that in this patch.

Quoted 6 lines

tools/testing/selftests/bpf/bpf_helpers.h static int (*bpf_skb_adjust_room)(void *ctx, __s32 len_diff, __u32 mode, · line 386

Quoted 3 lines
tools/testing/selftests/bpf/bpf_helpers.h +3 -0
+/* s390 provides user_pt_regs instead of struct pt_regs to userspace */
+struct pt_regs;
+#define PT_REGS_PARM1(x) (((const volatile user_pt_regs *)(x))->gprs[2])

Is user_pt_regs a recent change or has been there for quite some time? I am asking since bcc did not use user_pt_regs yet.

It was added in late 2017 in commit 466698e654e8 ("s390/bpf: correct broken uapi for BPF_PROG_TYPE_PERF_EVENT program type“).

#3 lore raw
Quoted 8 lines · Ilya Leoshkevich
Quoted 8 lines

Am 02.07.2019 um 18:42 schrieb Y Song <[email protected]>:

Quoted 3 lines · Ilya Leoshkevich
tools/testing/selftests/bpf/bpf_helpers.h +0 -2
-#elif defined(__s390x__)
-       #define bpf_target_s930x
Quoted 7 lines
Quoted 4 lines

I see in some other places (e.g., bcc) where
macro __s390x__ is also used to indicate a s390 architecture. Could you explain the difference between __s390__ and __s390x__?

__s390__ is defined for 32-bit and 64-bit variants, __s390x__ is defined for 64-bit variant only.

Thanks.

Quoted 13 lines
Quoted 5 lines
Quoted 4 lines

#if defined(bpf_target_x86)

+#ifdef __KERNEL__

Quoted 7 lines

In samples/bpf/, __KERNEL__ is defined at clang options and in selftests/bpf/, the __KERNEL__ is not defined.

I checked x86 pt_regs definition with and without __KERNEL__. They are identical except some register name difference. I am wondering whether we can unify into all without __KERNEL__. Is __KERNEL__ really needed?

Quoted 5 lines

Right now removing it causes the build to fail, but the errors look fixable. However, I wonder whether there is a plan regarding this:
should eBPF programs be built with user headers, kernel headers, or both? Status quo appears to be "both", so I’ve decided to stick with that in this patch.

Your patch is okay in the sense it maintains the current behavor. I think it is okay since user level and kernel pt_regs layout are the same except certain names are different.

Quoted 10 lines
Quoted 7 lines

tools/testing/selftests/bpf/bpf_helpers.h static int (*bpf_skb_adjust_room)(void *ctx, __s32 len_diff, __u32 mode, · line 386

Quoted 4 lines
tools/testing/selftests/bpf/bpf_helpers.h +3 -0
+/* s390 provides user_pt_regs instead of struct pt_regs to userspace */
+struct pt_regs;
+#define PT_REGS_PARM1(x) (((const volatile user_pt_regs *)(x))->gprs[2])

Is user_pt_regs a recent change or has been there for quite some time? I am asking since bcc did not use user_pt_regs yet.

It was added in late 2017 in commit 466698e654e8 ("s390/bpf: correct broken uapi for BPF_PROG_TYPE_PERF_EVENT program type“).

Thanks.

Acked-by Yonghong Song on v1 1/1
#4 lore raw
In reply to Ilya Leoshkevich
Quoted 14 lines · Ilya Leoshkevich

Use PT_REGS_RC(ctx) instead of ctx->rax, which is not present on s390.

Pass -D__TARGET_ARCH_$(ARCH) to selftests in order to choose a proper PT_REGS_RC variant.

Fix s930 -> s390 typo.

On s390, provide the forward declaration of struct pt_regs and cast it to user_pt_regs in PT_REGS_* macros. This is necessary, because instead of the full struct pt_regs, s390 exposes only its first field user_pt_regs to userspace, and bpf_helpers.h is used with both userspace (in selftests) and kernel (in samples) headers.

Quoted 5 lines

On x86, provide userspace versions of PT_REGS_* macros. Unlike s390, x86 provides struct pt_regs to both userspace and kernel, however, with different field names.

Quoted 151 lines
---
 tools/testing/selftests/bpf/Makefile      |  4 +-
 tools/testing/selftests/bpf/bpf_helpers.h | 46 +++++++++++++++--------
 tools/testing/selftests/bpf/progs/loop1.c |  2 +-
 tools/testing/selftests/bpf/progs/loop2.c |  2 +-
 tools/testing/selftests/bpf/progs/loop3.c |  2 +-
 5 files changed, 37 insertions(+), 19 deletions(-)
tools/testing/selftests/bpf/Makefile +3 -1
index d60fee59fbd1..599b320bef65 100644
@@ -1,5 +1,6 @@
 # SPDX-License-Identifier: GPL-2.0
 include ../../../../scripts/Kbuild.include
+include ../../../scripts/Makefile.arch
 
 LIBDIR := ../../../lib
 BPFDIR := $(LIBDIR)/bpf
@@ -138,7 +139,8 @@ CLANG_SYS_INCLUDES := $(shell $(CLANG) -v -E - </dev/null 2>&1 \
 
 CLANG_FLAGS = -I. -I./include/uapi -I../../../include/uapi \
              $(CLANG_SYS_INCLUDES) \
-             -Wno-compare-distinct-pointer-types
+             -Wno-compare-distinct-pointer-types \
+             -D__TARGET_ARCH_$(ARCH)
 
 $(OUTPUT)/test_l4lb_noinline.o: CLANG_FLAGS += -fno-inline
 $(OUTPUT)/test_xdp_noinline.o: CLANG_FLAGS += -fno-inline
tools/testing/selftests/bpf/bpf_helpers.h +31 -15
index 1a5b1accf091..faf86d83301a 100644
@@ -312,8 +312,8 @@ static int (*bpf_skb_adjust_room)(void *ctx, __s32 len_diff, __u32 mode,
 #if defined(__TARGET_ARCH_x86)
        #define bpf_target_x86
        #define bpf_target_defined
-#elif defined(__TARGET_ARCH_s930x)
-       #define bpf_target_s930x
+#elif defined(__TARGET_ARCH_s390)
+       #define bpf_target_s390
        #define bpf_target_defined
 #elif defined(__TARGET_ARCH_arm)
        #define bpf_target_arm
@@ -338,8 +338,8 @@ static int (*bpf_skb_adjust_room)(void *ctx, __s32 len_diff, __u32 mode,
 #ifndef bpf_target_defined
 #if defined(__x86_64__)
        #define bpf_target_x86
-#elif defined(__s390x__)
-       #define bpf_target_s930x
+#elif defined(__s390__)
+       #define bpf_target_s390
 #elif defined(__arm__)
        #define bpf_target_arm
 #elif defined(__aarch64__)
@@ -355,6 +355,7 @@ static int (*bpf_skb_adjust_room)(void *ctx, __s32 len_diff, __u32 mode,
 
 #if defined(bpf_target_x86)
 
+#ifdef __KERNEL__
 #define PT_REGS_PARM1(x) ((x)->di)
 #define PT_REGS_PARM2(x) ((x)->si)
 #define PT_REGS_PARM3(x) ((x)->dx)
@@ -365,19 +366,34 @@ static int (*bpf_skb_adjust_room)(void *ctx, __s32 len_diff, __u32 mode,
 #define PT_REGS_RC(x) ((x)->ax)
 #define PT_REGS_SP(x) ((x)->sp)
 #define PT_REGS_IP(x) ((x)->ip)
+#else
+#define PT_REGS_PARM1(x) ((x)->rdi)
+#define PT_REGS_PARM2(x) ((x)->rsi)
+#define PT_REGS_PARM3(x) ((x)->rdx)
+#define PT_REGS_PARM4(x) ((x)->rcx)
+#define PT_REGS_PARM5(x) ((x)->r8)
+#define PT_REGS_RET(x) ((x)->rsp)
+#define PT_REGS_FP(x) ((x)->rbp)
+#define PT_REGS_RC(x) ((x)->rax)
+#define PT_REGS_SP(x) ((x)->rsp)
+#define PT_REGS_IP(x) ((x)->rip)
+#endif
 
-#elif defined(bpf_target_s390x)
+#elif defined(bpf_target_s390)
 
-#define PT_REGS_PARM1(x) ((x)->gprs[2])
-#define PT_REGS_PARM2(x) ((x)->gprs[3])
-#define PT_REGS_PARM3(x) ((x)->gprs[4])
-#define PT_REGS_PARM4(x) ((x)->gprs[5])
-#define PT_REGS_PARM5(x) ((x)->gprs[6])
-#define PT_REGS_RET(x) ((x)->gprs[14])
-#define PT_REGS_FP(x) ((x)->gprs[11]) /* Works only with CONFIG_FRAME_POINTER */
-#define PT_REGS_RC(x) ((x)->gprs[2])
-#define PT_REGS_SP(x) ((x)->gprs[15])
-#define PT_REGS_IP(x) ((x)->psw.addr)
+/* s390 provides user_pt_regs instead of struct pt_regs to userspace */
+struct pt_regs;
+#define PT_REGS_PARM1(x) (((const volatile user_pt_regs *)(x))->gprs[2])
+#define PT_REGS_PARM2(x) (((const volatile user_pt_regs *)(x))->gprs[3])
+#define PT_REGS_PARM3(x) (((const volatile user_pt_regs *)(x))->gprs[4])
+#define PT_REGS_PARM4(x) (((const volatile user_pt_regs *)(x))->gprs[5])
+#define PT_REGS_PARM5(x) (((const volatile user_pt_regs *)(x))->gprs[6])
+#define PT_REGS_RET(x) (((const volatile user_pt_regs *)(x))->gprs[14])
+/* Works only with CONFIG_FRAME_POINTER */
+#define PT_REGS_FP(x) (((const volatile user_pt_regs *)(x))->gprs[11])
+#define PT_REGS_RC(x) (((const volatile user_pt_regs *)(x))->gprs[2])
+#define PT_REGS_SP(x) (((const volatile user_pt_regs *)(x))->gprs[15])
+#define PT_REGS_IP(x) (((const volatile user_pt_regs *)(x))->psw.addr)
 
 #elif defined(bpf_target_arm)
 
tools/testing/selftests/bpf/progs/loop1.c +1 -1
index dea395af9ea9..7cdb7f878310 100644
@@ -18,7 +18,7 @@ int nested_loops(volatile struct pt_regs* ctx)
        for (j = 0; j < 300; j++)
                for (i = 0; i < j; i++) {
                        if (j & 1)
-                               m = ctx->rax;
+                               m = PT_REGS_RC(ctx);
                        else
                                m = j;
                        sum += i * m;
tools/testing/selftests/bpf/progs/loop2.c +1 -1
index 0637bd8e8bcf..9b2f808a2863 100644
@@ -16,7 +16,7 @@ int while_true(volatile struct pt_regs* ctx)
        int i = 0;
 
        while (true) {
-               if (ctx->rax & 1)
+               if (PT_REGS_RC(ctx) & 1)
                        i += 3;
                else
                        i += 7;
tools/testing/selftests/bpf/progs/loop3.c +1 -2
index 30a0f6cba080..d727657d51e2 100644
@@ -16,7 +16,7 @@ int while_true(volatile struct pt_regs* ctx)
        __u64 i = 0, sum = 0;
        do {
                i++;
-               sum += ctx->rax;
+               sum += PT_REGS_RC(ctx);
        } while (i < 0x100000000ULL);
        return sum;
 }
--

2.21.0

maintainer
#5 lore raw
In reply to Ilya Leoshkevich
Quoted 13 lines · Ilya Leoshkevich

Use PT_REGS_RC(ctx) instead of ctx->rax, which is not present on s390.

Pass -D__TARGET_ARCH_$(ARCH) to selftests in order to choose a proper PT_REGS_RC variant.

Fix s930 -> s390 typo.

On s390, provide the forward declaration of struct pt_regs and cast it to user_pt_regs in PT_REGS_* macros. This is necessary, because instead of the full struct pt_regs, s390 exposes only its first field user_pt_regs to userspace, and bpf_helpers.h is used with both userspace (in selftests) and kernel (in samples) headers.

Quoted 5 lines

On x86, provide userspace versions of PT_REGS_* macros. Unlike s390, x86 provides struct pt_regs to both userspace and kernel, however, with different field names.

This doesn't apply cleanly to bpf-next, please rebase. I also think this should be ideally split into multiple patches, seems like 4 different issues which you are addressing in this single patch.

Thanks,
Daniel