LinuxPPC-Dev Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Lukas Stockner <lukas@lukasstockner.de>
To: Hari Bathini <hbathini@linux.ibm.com>,
	Christophe Leroy <chleroy@kernel.org>,
	bpf@vger.kernel.org, linuxppc-dev@lists.ozlabs.org
Cc: Naveen N Rao <naveen@kernel.org>,
	Madhavan Srinivasan <maddy@linux.ibm.com>,
	Michael Ellerman <mpe@ellerman.id.au>,
	Alexei Starovoitov <ast@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	Andrii Nakryiko <andrii@kernel.org>,
	Lukas Stockner <lukas@lukasstockner.de>
Subject: [PATCH 3/4] powerpc32/bpf: mark all argument registers seen in function calls
Date: Sun, 22 Nov 2026 20:42:04 +0100	[thread overview]
Message-ID: <20261122194205.218543-4-lukas@lukasstockner.de> (raw)
In-Reply-To: <20261122194205.218543-1-lukas@lukasstockner.de>

The JIT code tracks which registers are used and remaps based on that,
but currently does not account for the fact that function calls implicitly
use registers because the called function will treat them as arguments.

Therefore, if a function merely passes through one of its arguments
to another function call, it will incorrectly be considered unused and may
be overwritten with another register's contents.

In practice, this is unlikely to affect main programs as they only get
one argument (the context, which they are very likely to touch somehow
in addition to passing it on), so they'll have to touch other registers
if they want to pass them as arguments. Subprograms, however, are much
likelier to hit this.

Similarly, tail calls pass R1 to the callee, so mark it used as well.

Fixes: 51c66ad849a7 ("powerpc/bpf: Implement extended BPF on PPC32")
Fixes: a8936569a07b ("powerpc/bpf: Always reallocate BPF_REG_5, BPF_REG_AX and TMP_REG when possible")
Signed-off-by: Lukas Stockner <lukas@lukasstockner.de>
---
 arch/powerpc/net/bpf_jit_comp32.c | 17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)

diff --git a/arch/powerpc/net/bpf_jit_comp32.c b/arch/powerpc/net/bpf_jit_comp32.c
index 9f55dd4bdfd6..7414e350d2d4 100644
--- a/arch/powerpc/net/bpf_jit_comp32.c
+++ b/arch/powerpc/net/bpf_jit_comp32.c
@@ -339,7 +339,7 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, u32 *fimage, struct code
 {
 	const struct bpf_insn *insn = fp->insnsi;
 	int flen = fp->len;
-	int i, ret;
+	int i, ret, max_arg;
 
 	/* Start of epilogue code - will only be valid 2nd pass onwards */
 	u32 exit_addr = addrs[flen];
@@ -1183,6 +1183,18 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, u32 *fimage, struct code
 			if (ret < 0)
 				return ret;
 
+			/*
+			 * We don't know which arguments the called function will use,
+			 * so if this is a subprogram, assume the worst case where it
+			 * uses all of them. Regular programs only get one argument,
+			 * so any others would have been explicitly set.
+			 */
+			max_arg = ctx->is_subprog ? BPF_REG_5 : BPF_REG_1;
+			for (int reg = BPF_REG_1; reg <= max_arg; ++reg) {
+				bpf_set_seen_register(ctx, bpf_to_ppc(reg) - 1);
+				bpf_set_seen_register(ctx, bpf_to_ppc(reg));
+			}
+
 			if (bpf_is_seen_register(ctx, bpf_to_ppc(BPF_REG_5))) {
 				EMIT(PPC_RAW_STW(bpf_to_ppc(BPF_REG_5) - 1, _R1, 8));
 				EMIT(PPC_RAW_STW(bpf_to_ppc(BPF_REG_5), _R1, 12));
@@ -1412,6 +1424,9 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, u32 *fimage, struct code
 		 * Tail call
 		 */
 		case BPF_JMP | BPF_TAIL_CALL:
+			bpf_set_seen_register(ctx, bpf_to_ppc(BPF_REG_1) - 1);
+			bpf_set_seen_register(ctx, bpf_to_ppc(BPF_REG_1));
+
 			ctx->seen |= SEEN_TAILCALL;
 			ret = bpf_jit_emit_tail_call(image, ctx, addrs[i + 1]);
 			if (ret < 0)
-- 
2.56.0



  parent reply	other threads:[~2026-11-22 22:10 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-22 19:42 [PATCH 0/4] powerpc32/bpf: fix BPF-to-BPF calls and instruction folding Lukas Stockner
2026-11-22 19:42 ` [PATCH 1/4] powerpc32/bpf: fix argument passing for subprograms Lukas Stockner
2026-11-23  7:46   ` Christophe Leroy (CS GROUP)
2026-11-22 19:42 ` [PATCH 2/4] powerpc32/bpf: fetch fifth argument in BPF subprograms Lukas Stockner
2026-11-22 19:42 ` Lukas Stockner [this message]
2026-11-22 19:42 ` [PATCH 4/4] powerpc32/bpf: don't fold MOV into instructions that use dst_reg Lukas Stockner
2026-11-22 20:14   ` bot+bpf-ci
2026-11-28 18:32   ` Christophe Leroy (CS GROUP)

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261122194205.218543-4-lukas@lukasstockner.de \
    --to=lukas@lukasstockner.de \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=chleroy@kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=hbathini@linux.ibm.com \
    --cc=linuxppc-dev@lists.ozlabs.org \
    --cc=maddy@linux.ibm.com \
    --cc=mpe@ellerman.id.au \
    --cc=naveen@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox