Skip to contentSkip to conversation

s390/bpf: Add s390 JIT support for timed may_goto

PATCH Superseded Ilya Leoshkevich 1 participant 6 messages Aug 21, 2025

Subsystems (5)
Kselftest BPF s390 BPF selftests +1 more subsystems

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Ilya Leoshkevich

author maintainer
#0 lore raw

cover s390/bpf: Add s390 JIT support for timed may_goto

Hi,

This series adds timed may_goto implementation to the s390x JIT. Patch 1 is the implementation, patches 2-5 are the associated test changes.

Best regards,
Ilya

Ilya Leoshkevich (5):
s390/bpf: Add s390 JIT support for timed may_goto selftests/bpf: Add a missing newline to the "bad arch spec" message selftests/bpf: Add __arch_s390x macro
selftests/bpf: Enable timed may_goto verifier tests on s390x selftests/bpf: Remove may_goto tests from DENYLIST.s390x

 arch/s390/net/Makefile                        |  2 +-
 arch/s390/net/bpf_jit_comp.c                  | 25 +++++++++--
 arch/s390/net/bpf_timed_may_goto.S            | 45 +++++++++++++++++++
 tools/testing/selftests/bpf/DENYLIST.s390x    |  1 -
 .../testing/selftests/bpf/prog_tests/stream.c |  2 +-
 tools/testing/selftests/bpf/progs/bpf_misc.h  |  1 +
 .../selftests/bpf/progs/verifier_may_goto_1.c |  8 +++-
 tools/testing/selftests/bpf/test_loader.c     |  7 ++-
 8 files changed, 81 insertions(+), 10 deletions(-)
 create mode 100644 arch/s390/net/bpf_timed_may_goto.S
Signature · 1 line
2.50.1
author maintainer
#1 lore raw

PATCH 1/5 s390/bpf: Add s390 JIT support for timed may_goto

The verifier provides an architecture-independent implementation of the may_goto instruction, which is currently used on s390x, but it has a
downside: there is no way to prevent progs using it from running for a very long time.

The solution to this problem is an alternative timed implementation, which requires architecture-specific bits. Its availability is signaled to the verifier by bpf_jit_supports_timed_may_goto() returning true.

The verifier then emits a call to arch_bpf_timed_may_goto() using a non-standard calling convention. This function must act as a trampoline for bpf_check_timed_may_goto().

Implement bpf_jit_supports_timed_may_goto(), account for the special calling convention in the BPF_CALL implementation, and implement arch_bpf_timed_may_goto().

---
 arch/s390/net/Makefile             |  2 +-
 arch/s390/net/bpf_jit_comp.c       | 25 ++++++++++++++---
 arch/s390/net/bpf_timed_may_goto.S | 45 ++++++++++++++++++++++++++++++
 3 files changed, 67 insertions(+), 5 deletions(-)
 create mode 100644 arch/s390/net/bpf_timed_may_goto.S
arch/s390/net/Makefile +1 -1
index 8cab6deb0403..9275cf63192a 100644
@@ -2,5 +2,5 @@
 #
 # Arch-specific network modules
 #
-obj-$(CONFIG_BPF_JIT) += bpf_jit_comp.o
+obj-$(CONFIG_BPF_JIT) += bpf_jit_comp.o bpf_timed_may_goto.o
 obj-$(CONFIG_HAVE_PNETID) += pnet.o
arch/s390/net/bpf_jit_comp.c +21 -4
index fd45f03a213c..8b57d8532f36 100644
@@ -1806,10 +1806,22 @@ static noinline int bpf_jit_insn(struct bpf_jit *jit, struct bpf_prog *fp,
 			}
 		}
 
-		/* brasl %r14,func */
-		EMIT6_PCREL_RILB_PTR(0xc0050000, REG_14, (void *)func);
-		/* lgr %b0,%r2: load return value into %b0 */
-		EMIT4(0xb9040000, BPF_REG_0, REG_2);
+		if ((void *)func == arch_bpf_timed_may_goto) {
+			/*
+			 * arch_bpf_timed_may_goto() has a special ABI: the
+			 * parameters are in BPF_REG_AX and BPF_REG_10; the
+			 * return value is in BPF_REG_AX; and all GPRs except
+			 * REG_W0, REG_W1, and BPF_REG_AX are callee-saved.
+			 */
+
+			/* brasl %r0,func */
+			EMIT6_PCREL_RILB_PTR(0xc0050000, REG_0, (void *)func);
+		} else {
+			/* brasl %r14,func */
+			EMIT6_PCREL_RILB_PTR(0xc0050000, REG_14, (void *)func);
+			/* lgr %b0,%r2: load return value into %b0 */
+			EMIT4(0xb9040000, BPF_REG_0, REG_2);
+		}
 
 		/*
 		 * Copy the potentially updated tail call counter back.
@@ -2993,3 +3005,8 @@ void arch_bpf_stack_walk(bool (*consume_fn)(void *, u64, u64, u64),
 		prev_addr = addr;
 	}
 }
+
+bool bpf_jit_supports_timed_may_goto(void)
+{
+	return true;
+}
arch/s390/net/bpf_timed_may_goto.S new file +45 -0

45 lines not on this page · raw message

Signature · 1 line
2.50.1
author maintainer
#2 lore raw

PATCH 2/5 selftests/bpf: Add a missing newline to the "bad arch spec" message

In reply to Ilya Leoshkevich

Fix error messages like this one:

parse_test_spec:FAIL:569 bad arch spec: 's390x'process_subtest:FAIL:1153 Can't parse test spec for program 'may_goto_simple'

---
 tools/testing/selftests/bpf/test_loader.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/bpf/test_loader.c +1 -1
index 78423cf89e01..e1987d1959fd 100644
@@ -566,7 +566,7 @@ static int parse_test_spec(struct test_loader *tester,
 			} else if (strcmp(val, "RISCV64") == 0) {
 				arch = ARCH_RISCV64;
 			} else {
-				PRINT_FAIL("bad arch spec: '%s'", val);
+				PRINT_FAIL("bad arch spec: '%s'\n", val);
 				err = -EINVAL;
 				goto cleanup;
 			}
Signature · 1 line
2.50.1
author maintainer
#3 lore raw

PATCH 3/5 selftests/bpf: Add __arch_s390x macro

In reply to Ilya Leoshkevich

Make it possible to limit certain tests to s390x, just like it's already done for x86_64, arm64, and riscv64.

---
 tools/testing/selftests/bpf/progs/bpf_misc.h | 1 +
 tools/testing/selftests/bpf/test_loader.c    | 5 +++++
 2 files changed, 6 insertions(+)
tools/testing/selftests/bpf/progs/bpf_misc.h +1 -0
index c1cfd297aabf..72c2d72a245e 100644
@@ -136,6 +136,7 @@
 #define __arch_x86_64		__arch("X86_64")
 #define __arch_arm64		__arch("ARM64")
 #define __arch_riscv64		__arch("RISCV64")
+#define __arch_s390x		__arch("s390x")
 #define __caps_unpriv(caps)	__attribute__((btf_decl_tag("comment:test_caps_unpriv=" EXPAND_QUOTE(caps))))
 #define __load_if_JITed()	__attribute__((btf_decl_tag("comment:load_mode=jited")))
 #define __load_if_no_JITed()	__attribute__((btf_decl_tag("comment:load_mode=no_jited")))
tools/testing/selftests/bpf/test_loader.c +5 -0
index e1987d1959fd..a9388ac88358 100644
@@ -374,6 +374,7 @@ enum arch {
 	ARCH_X86_64	= 0x2,
 	ARCH_ARM64	= 0x4,
 	ARCH_RISCV64	= 0x8,
+	ARCH_S390X	= 0x10,
 };
 
 static int get_current_arch(void)
@@ -384,6 +385,8 @@ static int get_current_arch(void)
 	return ARCH_ARM64;
 #elif defined(__riscv) && __riscv_xlen == 64
 	return ARCH_RISCV64;
+#elif defined(__s390x__)
+	return ARCH_S390X;
 #endif
 	return ARCH_UNKNOWN;
 }
@@ -565,6 +568,8 @@ static int parse_test_spec(struct test_loader *tester,
 				arch = ARCH_ARM64;
 			} else if (strcmp(val, "RISCV64") == 0) {
 				arch = ARCH_RISCV64;
+			} else if (strcmp(val, "s390x") == 0) {
+				arch = ARCH_S390X;
 			} else {
 				PRINT_FAIL("bad arch spec: '%s'\n", val);
 				err = -EINVAL;
Signature · 1 line
2.50.1
author maintainer
#4 lore raw

PATCH 4/5 selftests/bpf: Enable timed may_goto verifier tests on s390x

In reply to Ilya Leoshkevich

Now that the timed may_goto implementation is available on s390x, enable the respective verifier tests.

---
 tools/testing/selftests/bpf/prog_tests/stream.c         | 2 +-
 tools/testing/selftests/bpf/progs/verifier_may_goto_1.c | 8 ++++++--
 2 files changed, 7 insertions(+), 3 deletions(-)
tools/testing/selftests/bpf/prog_tests/stream.c +1 -1
index d9f0185dca61..d89d04832011 100644
@@ -77,7 +77,7 @@ void test_stream_errors(void)
 		ASSERT_OK(ret, "ret");
 		ASSERT_OK(opts.retval, "retval");
 
-#if !defined(__x86_64__)
+#if !defined(__x86_64__) || !defined(__s390x__)
 		ASSERT_TRUE(1, "Timed may_goto unsupported, skip.");
 		if (i == 0) {
 			ret = bpf_prog_stream_read(prog_fd, 2, buf, sizeof(buf), &ropts);
tools/testing/selftests/bpf/progs/verifier_may_goto_1.c +6 -2
index 3966d827f288..cc1063863569 100644
@@ -9,6 +9,7 @@
 SEC("raw_tp")
 __description("may_goto 0")
 __arch_x86_64
+__arch_s390x
 __xlated("0: r0 = 1")
 __xlated("1: exit")
 __success
@@ -27,6 +28,7 @@ __naked void may_goto_simple(void)
 SEC("raw_tp")
 __description("batch 2 of may_goto 0")
 __arch_x86_64
+__arch_s390x
 __xlated("0: r0 = 1")
 __xlated("1: exit")
 __success
@@ -47,6 +49,7 @@ __naked void may_goto_batch_0(void)
 SEC("raw_tp")
 __description("may_goto batch with offsets 2/1/0")
 __arch_x86_64
+__arch_s390x
 __xlated("0: r0 = 1")
 __xlated("1: exit")
 __success
@@ -69,8 +72,9 @@ __naked void may_goto_batch_1(void)
 }
 
 SEC("raw_tp")
-__description("may_goto batch with offsets 2/0 - x86_64")
+__description("may_goto batch with offsets 2/0 - x86_64 and s390x")
 __arch_x86_64
+__arch_s390x
 __xlated("0: *(u64 *)(r10 -16) = 65535")
 __xlated("1: *(u64 *)(r10 -8) = 0")
 __xlated("2: r11 = *(u64 *)(r10 -16)")
@@ -84,7 +88,7 @@ __xlated("9: r0 = 1")
 __xlated("10: r0 = 2")
 __xlated("11: exit")
 __success
-__naked void may_goto_batch_2_x86_64(void)
+__naked void may_goto_batch_2_x86_64_s390x(void)
 {
 	asm volatile (
 	".8byte %[may_goto1];"
Signature · 1 line
2.50.1
author maintainer
#5 lore raw

PATCH 5/5 selftests/bpf: Remove may_goto tests from DENYLIST.s390x

In reply to Ilya Leoshkevich

The may_goto instruction is now fully supported on s390x, including the timed implementation, so remove the respective test from the denylist.

---
 tools/testing/selftests/bpf/DENYLIST.s390x | 1 -
 1 file changed, 1 deletion(-)
tools/testing/selftests/bpf/DENYLIST.s390x +0 -1
index 3ebd77206f98..a17baf8c6fd7 100644
@@ -2,4 +2,3 @@
 # Alphabetical order
 get_stack_raw_tp                         # user_stack corrupted user stack                                             (no backchain userspace)
 stacktrace_build_id                      # compare_map_keys stackid_hmap vs. stackmap err -2 errno 2                   (?)
-verifier_iterating_callbacks
Signature · 1 line
2.50.1