Re: [PATCH bpf-next 07/10] bpf,x86: Implement JIT support for stack arguments

Amery Hung
2026-04-02T22:26:17Z

On Wed, Apr 1, 2026 at 6:28 PM Yonghong Song <yonghong.song@linux.dev> wrote:
>

[...]

> case BPF_JMP | BPF_CALL: {
> + int off, base_off, n_stack_args, kfunc_stack_args = 0, stack_args = 0;
> + u16 outgoing_stack_args = stack_arg_depth - incoming_stack_arg_depth;
> u8 *ip = image + addrs[i - 1];
>
> func = (u8 *) __bpf_call_base + imm32;
> @@ -2449,6 +2549,29 @@ st: if (is_imm8(insn->off))
> }
> if (!imm32)
> return -EINVAL;
> +
> + if (src_reg == BPF_PSEUDO_CALL && outgoing_stack_args > 0) {
> + n_stack_args = outgoing_stack_args / 8;
> + base_off = -(prog_stack_depth + incoming_stack_arg_depth);
> + ip += push_stack_args(&prog, base_off, n_stack_args, 1);
> + }
> +
> + if (src_reg != BPF_PSEUDO_CALL && insn->off > 0) {
> + kfunc_stack_args = insn->off;
> + stack_args = kfunc_stack_args > 1 ? kfunc_stack_args - 1 : 0;
> + base_off = -(prog_stack_depth + incoming_stack_arg_depth);
> + ip += push_stack_args(&prog, base_off, kfunc_stack_args, 2);
> +
> + /* mov r9, [rbp + base_off - 8] */
> + off = base_off - 8;
> + if (is_imm8(off)) {
> + EMIT4(0x4C, 0x8B, 0x4D, off);
> + ip += 4;
> + } else {
> + EMIT3_off32(0x4C, 0x8B, 0x8D, off);
> + ip += 7;
> + }
> + }

Do we need to make sure RSP is 16-byte aligned before passing arg
through stack per x86-64 ABI?

> if (priv_frame_ptr) {
> push_r9(&prog);
> ip += 2;
> @@ -2458,6 +2581,14 @@ st: if (is_imm8(insn->off))
> return -EINVAL;
> if (priv_frame_ptr)
> pop_r9(&prog);
> + if (stack_args > 0) {
> + /* add rsp, stack_args * 8 */
> + EMIT4(0x48, 0x83, 0xC4, stack_args * 8);
> + }
> + if (src_reg == BPF_PSEUDO_CALL && outgoing_stack_args > 0) {
> + /* add rsp, outgoing_stack_args */
> + EMIT4(0x48, 0x83, 0xC4, outgoing_stack_args);
> + }
> break;
> }
>
> --
> 2.52.0
>
>