Skip to contentSkip to conversation

s390/bpf: Remove the orig_call NULL check

PATCH Applied Ilya Leoshkevich 3 participants 7 messages May 12, 2025 – May 15, 2025

Subsystems (3)
lore
Share & tools
lore

Ilya Leoshkevich

author maintainer
#0 lore raw

cover s390/bpf: Remove the orig_call NULL check

Hi,

I've been looking at fixing the tailcall_bpf2bpf_hierarchy failures on s390. One of the challenges is that when a BPF trampoline calls a BPF prog A, the prologue of A sets the tail call count to 0. Therefore it would be useful to know whether the trampoline is attached to some other BPF prog B, in which case A should be called using an offset equal to tail_call_start, bypassing the tail call count initialization.

The trampoline attachment point is passed to trampoline functions via the orig_call variable. Unfortunately in the case of calculating the size of a struct_ops trampoline it's NULL, and I could not think of a good reason to have it this way. This series makes it always non-NULL.

Best regards,
Ilya

Ilya Leoshkevich (2):
bpf: Pass the same orig_call value to trampoline functions s390/bpf: Remove the orig_call NULL check

 arch/s390/net/bpf_jit_comp.c | 5 ++---
 kernel/bpf/bpf_struct_ops.c  | 2 +-
 2 files changed, 3 insertions(+), 4 deletions(-)
Signature · 1 line
2.49.0
author maintainer
#1 lore raw

PATCH 1/2 bpf: Pass the same orig_call value to trampoline functions

There is currently some confusion in the s390x JIT regarding whether orig_call can be NULL and what that means. Originally the NULL value was used to distinguish the struct_ops case, but this was superseded by BPF_TRAMP_F_INDIRECT (see commit 0c970ed2f87c ("s390/bpf: Fix indirect trampoline generation").

The remaining reason to have this check is that NULL can actually be passed to the arch_bpf_trampoline_size() call - but not to the respective arch_prepare_bpf_trampoline()! call - by bpf_struct_ops_prepare_trampoline().

Remove this asymmetry by passing stub_func to both functions, so that JITs may rely on orig_call never being NULL.

---
 kernel/bpf/bpf_struct_ops.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/bpf/bpf_struct_ops.c +1 -1
index db13ee70d94d..96113633e391 100644
@@ -601,7 +601,7 @@ int bpf_struct_ops_prepare_trampoline(struct bpf_tramp_links *tlinks,
 	if (model->ret_size > 0)
 		flags |= BPF_TRAMP_F_RET_FENTRY_RET;
 
-	size = arch_bpf_trampoline_size(model, flags, tlinks, NULL);
+	size = arch_bpf_trampoline_size(model, flags, tlinks, stub_func);
 	if (size <= 0)
 		return size ? : -EFAULT;
 
Signature · 1 line
2.49.0
#2 lore raw

Re: [PATCH bpf-next 1/2] bpf: Pass the same orig_call value to trampoline functions

Quoted 24 lines · Ilya Leoshkevich

There is currently some confusion in the s390x JIT regarding whether orig_call can be NULL and what that means. Originally the NULL value was used to distinguish the struct_ops case, but this was superseded by BPF_TRAMP_F_INDIRECT (see commit 0c970ed2f87c ("s390/bpf: Fix indirect trampoline generation").

The remaining reason to have this check is that NULL can actually be passed to the arch_bpf_trampoline_size() call - but not to the respective arch_prepare_bpf_trampoline()! call - by bpf_struct_ops_prepare_trampoline().

Remove this asymmetry by passing stub_func to both functions, so that JITs may rely on orig_call never being NULL.

---
  kernel/bpf/bpf_struct_ops.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
kernel/bpf/bpf_struct_ops.c +0 -0
index db13ee70d94d..96113633e391 100644

kernel/bpf/bpf_struct_ops.c int bpf_struct_ops_prepare_trampoline(struct bpf_tramp_links *tlinks, · line 604

Quoted 5 lines
kernel/bpf/bpf_struct_ops.c +1 -1
@@ -601,7 +601,7 @@ int bpf_struct_ops_prepare_trampoline(struct bpf_tramp_links *tlinks,
  	if (model->ret_size > 0)
  		flags |= BPF_TRAMP_F_RET_FENTRY_RET;
 
-	size = arch_bpf_trampoline_size(model, flags, tlinks, NULL);
+	size = arch_bpf_trampoline_size(model, flags, tlinks, stub_func);

The change looks ok but not sure why it is needed.

I can see why stub_func is needed to generate the final image in arch_prepare_bpf_trampoline() in x86. The "arch_bpf_trampoline_size()" here is generating a temporary image, so NULL or not doesn't seem to matter.

Does the s390 jit need to use the actual stub_func address somewhere in the temporary and/or final image?

author maintainer
#3 lore raw

Re: [PATCH bpf-next 1/2] bpf: Pass the same orig_call value to trampoline functions

Quoted 43 lines · Martin KaFai Lau
Quoted 38 lines · Ilya Leoshkevich

There is currently some confusion in the s390x JIT regarding whether
orig_call can be NULL and what that means. Originally the NULL value
was used to distinguish the struct_ops case, but this was superseded by
BPF_TRAMP_F_INDIRECT (see commit 0c970ed2f87c ("s390/bpf: Fix indirect
trampoline generation").

The remaining reason to have this check is that NULL can actually be
passed to the arch_bpf_trampoline_size() call - but not to the respective arch_prepare_bpf_trampoline()! call - by bpf_struct_ops_prepare_trampoline().

Remove this asymmetry by passing stub_func to both functions, so that
JITs may rely on orig_call never being NULL.

---
  kernel/bpf/bpf_struct_ops.c | 2 +-

  1 file changed, 1 insertion(+), 1 deletion(-)
kernel/bpf/bpf_struct_ops.c +0 -0
index db13ee70d94d..96113633e391 100644
@@ -601,7 +601,7 @@ int bpf_struct_ops_prepare_trampoline(struct
 bpf_tramp_links *tlinks,
   	if (model->ret_size > 0)
   		flags |= BPF_TRAMP_F_RET_FENTRY_RET;

kernel/bpf/bpf_struct_ops.c +0 -1
-	size = arch_bpf_trampoline_size(model, flags, tlinks,

NULL);

kernel/bpf/bpf_struct_ops.c +1 -0
+	size = arch_bpf_trampoline_size(model, flags, tlinks,
Quoted 1 line

stub_func);

The change looks ok but not sure why it is needed.

Quoted 8 lines

I can see why stub_func is needed to generate the final image in arch_prepare_bpf_trampoline() in x86. The "arch_bpf_trampoline_size()" here is
generating a temporary image, so NULL or not doesn't seem to matter.

Does the s390 jit need to use the actual stub_func address somewhere in the
temporary and/or final image?

Not right now, however, in the future I would like to check whether orig_call points to a BPF prog. I have explained the rationale behind this in the series description.

Purely practical issues aside, currently it's unclear what orig_func == NULL means. It had meaning in the past, but not anymore. I think it would be good to remove this uncertainty and state that today orig_func is never NULL.

Furthermore, a hypothetical JIT may produce different instruction sequences for loading certain constants. Suppose we wanted to load the value of orig_call into a register. Then in the NULL case a JIT could generate:

xgr %r1,%r1

and in the non-NULL case:

llihf %r1,<high>
oilf %r1,<low>

As you mentioned, arch_bpf_trampoline_size() generates a temporary image, and in this case it would have a different size. So this asymmetry can be a potential footgun.

reviewer Acked-by
#4 lore raw

Re: [PATCH bpf-next 1/2] bpf: Pass the same orig_call value to trampoline functions

Quoted 53 lines · Ilya Leoshkevich
Quoted 43 lines · Martin KaFai Lau
Quoted 31 lines · Ilya Leoshkevich

There is currently some confusion in the s390x JIT regarding whether
orig_call can be NULL and what that means. Originally the NULL value
was used to distinguish the struct_ops case, but this was superseded by
BPF_TRAMP_F_INDIRECT (see commit 0c970ed2f87c ("s390/bpf: Fix indirect
trampoline generation").

The remaining reason to have this check is that NULL can actually be
passed to the arch_bpf_trampoline_size() call - but not to the respective arch_prepare_bpf_trampoline()! call - by bpf_struct_ops_prepare_trampoline().

Remove this asymmetry by passing stub_func to both functions, so that
JITs may rely on orig_call never being NULL.

---
   kernel/bpf/bpf_struct_ops.c | 2 +-
   1 file changed, 1 insertion(+), 1 deletion(-)
kernel/bpf/bpf_struct_ops.c +0 -0
index db13ee70d94d..96113633e391 100644

kernel/bpf/bpf_struct_ops.c int bpf_struct_ops_prepare_trampoline(struct · line 607

Quoted 8 lines
kernel/bpf/bpf_struct_ops.c +1 -1
@@ -601,7 +601,7 @@ int bpf_struct_ops_prepare_trampoline(struct
 bpf_tramp_links *tlinks,
   	if (model->ret_size > 0)
   		flags |= BPF_TRAMP_F_RET_FENTRY_RET;
 
-	size = arch_bpf_trampoline_size(model, flags, tlinks,
 NULL);
+	size = arch_bpf_trampoline_size(model, flags, tlinks,
 stub_func);

The change looks ok but not sure why it is needed.

Quoted 8 lines

I can see why stub_func is needed to generate the final image in arch_prepare_bpf_trampoline() in x86. The "arch_bpf_trampoline_size()" here is
generating a temporary image, so NULL or not doesn't seem to matter.

Does the s390 jit need to use the actual stub_func address somewhere in the
temporary and/or final image?

Quoted 8 lines

Not right now, however, in the future I would like to check whether orig_call points to a BPF prog. I have explained the rationale behind this in the series description.

Purely practical issues aside, currently it's unclear what orig_func == NULL means. It had meaning in the past, but not anymore. I think it would be good to remove this uncertainty and state that today orig_func is never NULL.

For the bpf_struct_ops trampoline image, there is no need to invoke the orig_call, so it had been NULL for both temporary and final image cases until the recent cfi fix in x86.

I was asking because it feels like the jit might be doing something incorrect (or at least unnecessary) for the bpf_struct_ops if it needs to use the orig_call address. I don't know much of the s390 jit though.

The change looks fine from the bpf_struct_ops pov. Always pass "stub_func" is more consistent also, so

I don't know enough about the tailcall_bpf2bpf_hierarchy as stated in the commit message, so I will defer to others to comment.

Quoted 15 lines

Furthermore, a hypothetical JIT may produce different instruction sequences for loading certain constants. Suppose we wanted to load the value of orig_call into a register. Then in the NULL case a JIT could generate:

   xgr %r1,%r1

and in the non-NULL case:

   llihf %r1,<high>
   oilf %r1,<low>

As you mentioned, arch_bpf_trampoline_size() generates a temporary image, and in this case it would have a different size. So this asymmetry can be a potential footgun.

author maintainer
#5 lore raw

PATCH 2/2 s390/bpf: Remove the orig_call NULL check

In reply to Ilya Leoshkevich

Now that orig_call can never be NULL, remove the respective check.

---
 arch/s390/net/bpf_jit_comp.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
arch/s390/net/bpf_jit_comp.c +2 -3
index 0776dfde2dba..4618f706067d 100644
@@ -2585,9 +2585,8 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im,
 	if (nr_stack_args > MAX_NR_STACK_ARGS)
 		return -ENOTSUPP;
 
-	/* Return to %r14, since func_addr and %r0 are not available. */
-	if ((!func_addr && !(flags & BPF_TRAMP_F_ORIG_STACK)) ||
-	    (flags & BPF_TRAMP_F_INDIRECT))
+	/* Return to %r14 in the struct_ops case. */
+	if (flags & BPF_TRAMP_F_INDIRECT)
 		flags |= BPF_TRAMP_F_SKIP_FRAME;
 
 	/*
Signature · 1 line
2.49.0
#6 lore raw
In reply to Ilya Leoshkevich
This series was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:

Hello:

This series was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:

Quoted 10 lines · On Mon, 12 May 2025 22:57:29 +0200 you wrote:

Hi,

I've been looking at fixing the tailcall_bpf2bpf_hierarchy failures on s390. One of the challenges is that when a BPF trampoline calls a BPF prog A, the prologue of A sets the tail call count to 0. Therefore it would be useful to know whether the trampoline is attached to some other BPF prog B, in which case A should be called using an offset equal to tail_call_start, bypassing the tail call count initialization.

[...]

Here is the summary with links:
- [bpf-next,1/2] bpf: Pass the same orig_call value to trampoline functions

    https://git.kernel.org/bpf/bpf-next/c/94bde253d3ae
  - [bpf-next,2/2] s390/bpf: Remove the orig_call NULL check
    https://git.kernel.org/bpf/bpf-next/c/8e57cf09c84c

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html