Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.128.41 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796144003; b=4seiZVHuVBvF8LYRBVZ80P3Yo41JZHIL/vEGOA3k4Bs0O/cDpo93UByRID19ix0kGSH8 8OSLPTSLV0P7W/7oSmyO6FIhwqtjWX8GpcwP7a7VlHoy0MFGEWmAPf8xLVS5sVZUPKXql kyle1QeHUmKgIfZFMGuf/Ld2KQ4NvGMJQlGrXuLve4PUTQ9pbB3XhA9adfuIejPfP7fp3 YCHHjTOV9YZ0AkgJn8XlvV3p/JlWt7fqsVN0Bvh0CzVoWCJMLEWMj8pVq4OwHuL5qeTev J9H5MjESOaNuzXfs6yj/NxxBDgSeGL314OU2hiWlAC5F3GTw3edNhMywSedBBUybowA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796144003; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-ID:X-Mailer:In-Reply-To:References: MIME-Version:Content-Transfer-Encoding; bh=zrqSTPUROAnUHv+YEvsHuBuJbjUdkEiMhSkqWKMgPNY=; b=TPHTw3XYRKXv8EXQ43Z0OugyeXDBfMUuHULxFj+2iEznoinaeByDdnEsTeRiPZLGwTE+ T4hTdjuNqqrv8nw7isLtP7sVgMsYAXixO7ABPy2k+6VsB56HkHIC+4sqA4trWDnQqGHA8 9w1Kj6tfupyiDgQX2B8w7Dc+KH/xQ6sQfUnU0V9m/T5L0N5fY4A25sDPOwRl8JoEex2qf 0nwnmMh1XJOMXIH5nEPxq6KcvbGadXcHwVEmxLZc209lME0dCmfgCcCpa1gdXqGXydbhs 3h8wCVMkJ9J48x8A4l39X2iD5fDoR69sqpLqLbfo3jLLDQREQ76Zu1p8lHj1qpEsWdw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none header.from=lukasstockner.de; spf=pass smtp.mailfrom=lukasstockner.de; dkim=pass header.d=lukasstockner-de.20260707.gappssmtp.com header.i=@lukasstockner-de.20260707.gappssmtp.com header.a=rsa-sha256 header.s=20260707 header.b=JG+5JOAt; arc=none smtp.remote-ip=209.85.128.41 Received: from mail-wm1-f41.google.com (mail-wm1-f41.google.com [209.85.128.41]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BBB95606A681 for ; Tue, 1 Dec 2026 16:53:22 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org BBB95606A681 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=lukasstockner.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=lukasstockner.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=lukasstockner-de.20260707.gappssmtp.com header.i=@lukasstockner-de.20260707.gappssmtp.com header.a=rsa-sha256 header.s=20260707 header.b=JG+5JOAt Received: by mail-wm1-f41.google.com with SMTP id 5b1f17b1804b1-4bb9d8e8e90so89784225e9.3 for ; Tue, 01 Dec 2026 08:53:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=lukasstockner-de.20260707.gappssmtp.com; s=20260707; t=1796144001; x=1796748801; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=zrqSTPUROAnUHv+YEvsHuBuJbjUdkEiMhSkqWKMgPNY=; b=JG+5JOAtpYhpSMuvCFWFyntZlumJ5VOsisXJxgQnztWmOBiXNGtuvdNZJLgxcZ9KMu RtXxCKJXS8mbpcSZ6V9VkP7MkSXb7Y52nuYQ4nDiTnpEG7hZhqXUWetB/q7vn+JoCVOw +ez2zW8RYLOSO4EhA1V+Qu0bMhc3OcvCH5Pivhym1zgEFATv0BGvtCcWn1C/GnkFyQi1 EyR46x4dUbAhIFbS7bfOqx3GSwLo1Oh5cB0/OYppCFIPz9HLm6KzxmeOR3Q0hJAdswxF F4zvtt9pYQpPR1fx85XQKZkjHeh/kcANGJFU6bUBkyHJzpdKA0kaAK0xBaIJHx2efpkk HcfA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796144001; x=1796748801; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=zrqSTPUROAnUHv+YEvsHuBuJbjUdkEiMhSkqWKMgPNY=; b=1TStntKMj66eWdIzsrAREjXyqIHN8cJmU2FROw3Sq0aXbjG39lbfWWmlbdBt5q1IrX Yj1QnihU28w5agT18ceVEgT48a/DNphTqYOJAoIw1huu+veVF0L6ka1u9I82v8BGKp6Y twMRG6bJoYRQ+o5EoAnp7XvyUB0UHwohwGVia4sOZAIA3bOLQfu00gKF2DwXu6gxBBro GU8h8LHdJJzOSeyQdGwr9LdFor+KPWnugC250AibAJ1M49x1iwNmlWwVu/hFFNC5Ss1e yB838OCMyhkOf3000eQHLz3sC0yKEPuyUcdLsdaPAjx5m2IUrFJe97EmNRLXaIr8yYkl bveA== X-Forwarded-Encrypted: i=1; AL+hErFSU+grZ0XlaYxNobjbj+umfX8wB0GgAU2lcYlpMJlLRr9pO5EEgUYyFS8Pb8uUDngWZOU=@vger.kernel.org X-Gm-Message-State: APuU4Z/A9O2K+ujG/adhtUCMTit6JdHxne66KOOUGNywJi3c406rqtQT F8Y6UfeMW587XmL8zWXlCLTl4Wv+ngjO8oabpionHL8ja5FlZzKhUw5QpEEKkxjcBwE= X-Gm-Gg: AZFdTZdeKDG42a3E3csHsfbCwu/a+gn8y2yPCEiS4q7/fqm8G4NcvKu+bKKyq3If4J8 2PkBSm3hpZc808EJoco/rIdohy39ULqqfjIBhfc6j7ROH2ha4FZYxOznsyEmY1rOlHyYcfz9aOw vr4Ap00yXzuqxUqOk+LKJ4N2nezm5CPp52gSsQF88JhtnZhjn33WZsAZCoFr3IhXQT95RL+3wFF z71wmDCSRmDsFFOzF/U/PyRbdcATWmG/5xs0lHJ7izLD/T8jKC2im3zaBjnPoCZi68UtEIYsJcO T3iph9VWE6vKiv9P9LIgzo0L8e+8oTi9+zCeEcSf8VidEeuarrFD1L1Xl6uquqURq2CTSdq+JMa qzJGEESI6CbR2laelcgYoAXw9jRYo/zaWvWa3sl+xw4wVL+0eKWwzlkCHKowPLVavTb7938DNG7 GVEFnnBm5MVyU3AIebZoJSXKOXy7sz2JzOP10Fu0OPdx7gqRgi2NgW8C7L1VeFYkWoTenG2qFSx x1pI2S7Qi6pcoLFVhG+BpW9WXTWTBU6qtcBEVYpA1lMTmlRL+SsUXtkhRI0WR2h7c44Wwto7rSQ ksO2YNjz7/edK8m1FZGNLmtyudTUARRap/aue5WBg5CYaPEbb69R2STEbmwWNUShT46dXCCb8Sq 9JCxhJS2qEAJQgRWSMCUX X-Received: by 2002:a05:600c:3ba3:b0:4bb:6118:4c9a with SMTP id 5b1f17b1804b1-4bb88819940mr878921315e9.30.1796144001115; Tue, 01 Dec 2026 08:53:21 -0800 (PST) Received: from lukas-laptop (p200300feff05c900f3466834a17f39d0.dip0.t-ipconnect.de. [2003:fe:ff05:c900:f346:6834:a17f:39d0]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-4ab65cb42absm71207855f8f.1.2026.12.01.08.53.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Dec 2026 08:53:20 -0800 (PST) From: Lukas Stockner To: Hari Bathini , Christophe Leroy , Naveen N Rao , bpf@vger.kernel.org, linuxppc-dev@lists.ozlabs.org Cc: Madhavan Srinivasan , Michael Ellerman , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Lukas Stockner Subject: [PATCH v2 7/7] powerpc32/bpf: don't fold MOV into jump targets Date: Tue, 1 Dec 2026 17:53:13 +0100 Message-ID: <20261201165313.413960-8-lukas@lukasstockner.de> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261201165313.413960-1-lukas@lukasstockner.de> References: <20261201165313.413960-1-lukas@lukasstockner.de> X-Mailing-List: bpf@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit If a BPF program jumps to an ALU instruction that was combined with a preceding MOV, it will incorrectly execute both the MOV and the ALU operation. Therefore, avoid 3-op folding if the second instruction is a jump target. Fixes: 19daf0aef84f ("powerpc/bpf/32: perform three operands ALU operations") Signed-off-by: Lukas Stockner --- arch/powerpc/net/bpf_jit.h | 4 ++-- arch/powerpc/net/bpf_jit_comp.c | 8 ++++---- arch/powerpc/net/bpf_jit_comp32.c | 6 +++--- arch/powerpc/net/bpf_jit_comp64.c | 5 +++-- include/linux/bpf.h | 2 ++ kernel/bpf/core.c | 9 +++++++++ 6 files changed, 23 insertions(+), 11 deletions(-) diff --git a/arch/powerpc/net/bpf_jit.h b/arch/powerpc/net/bpf_jit.h index 4da8bde92e1e..fea87cc88b85 100644 --- a/arch/powerpc/net/bpf_jit.h +++ b/arch/powerpc/net/bpf_jit.h @@ -224,8 +224,8 @@ static inline void bpf_clear_seen_register(struct codegen_context *ctx, int i) void bpf_jit_init_reg_mapping(struct codegen_context *ctx); int bpf_jit_emit_func_call_rel(u32 *image, u32 *fimage, struct codegen_context *ctx, u64 func); -int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, u32 *fimage, struct codegen_context *ctx, - u32 *addrs, int pass, bool extra_pass); +int bpf_jit_build_body(struct bpf_verifier_env *env, struct bpf_prog *fp, u32 *image, u32 *fimage, + struct codegen_context *ctx, u32 *addrs, int pass, bool extra_pass); void bpf_jit_build_prologue(u32 *image, struct codegen_context *ctx); void bpf_jit_build_epilogue(u32 *image, u32 *fimage, struct codegen_context *ctx); void bpf_jit_build_fentry_stubs(u32 *image, u32 *fimage, struct codegen_context *ctx); diff --git a/arch/powerpc/net/bpf_jit_comp.c b/arch/powerpc/net/bpf_jit_comp.c index ce2966c52ac5..01151a78f2b2 100644 --- a/arch/powerpc/net/bpf_jit_comp.c +++ b/arch/powerpc/net/bpf_jit_comp.c @@ -289,7 +289,7 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_verifier_env *env, struct bpf_pr } /* Scouting faux-generate pass 0 */ - if (bpf_jit_build_body(fp, NULL, NULL, &cgctx, addrs, 0, false)) + if (bpf_jit_build_body(env, fp, NULL, NULL, &cgctx, addrs, 0, false)) /* We hit something illegal or unsupported. */ goto out_err; @@ -303,7 +303,7 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_verifier_env *env, struct bpf_pr if (cgctx.seen & SEEN_TAILCALL || !is_offset_in_branch_range((long)cgctx.idx * 4)) { cgctx.idx = 0; cgctx.alt_exit_addr = 0; - if (bpf_jit_build_body(fp, NULL, NULL, &cgctx, addrs, 0, false)) + if (bpf_jit_build_body(env, fp, NULL, NULL, &cgctx, addrs, 0, false)) goto out_err; } @@ -344,8 +344,8 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_verifier_env *env, struct bpf_pr cgctx.exentry_idx = 0; cgctx.alt_exit_addr = 0; bpf_jit_build_prologue(code_base, &cgctx); - if (bpf_jit_build_body(fp, code_base, fcode_base, &cgctx, addrs, pass, - extra_pass)) { + if (bpf_jit_build_body(env, fp, code_base, fcode_base, &cgctx, addrs, + pass, extra_pass)) { bpf_arch_text_copy(&fhdr->size, &hdr->size, sizeof(hdr->size)); bpf_jit_binary_pack_free(fhdr, hdr); goto out_err; diff --git a/arch/powerpc/net/bpf_jit_comp32.c b/arch/powerpc/net/bpf_jit_comp32.c index 83457b21d0a8..ec2d5ce2f70a 100644 --- a/arch/powerpc/net/bpf_jit_comp32.c +++ b/arch/powerpc/net/bpf_jit_comp32.c @@ -334,8 +334,8 @@ static int bpf_jit_emit_tail_call(u32 *image, struct codegen_context *ctx, u32 o } /* Assemble the body code between the prologue & epilogue */ -int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, u32 *fimage, struct codegen_context *ctx, - u32 *addrs, int pass, bool extra_pass) +int bpf_jit_build_body(struct bpf_verifier_env *env, struct bpf_prog *fp, u32 *image, u32 *fimage, + struct codegen_context *ctx, u32 *addrs, int pass, bool extra_pass) { const struct bpf_insn *insn = fp->insnsi; int flen = fp->len; @@ -368,7 +368,7 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, u32 *fimage, struct code (BPF_CLASS(prevcode) == BPF_ALU64 || BPF_CLASS(prevcode) == BPF_ALU) && BPF_OP(prevcode) == BPF_MOV && BPF_SRC(prevcode) == BPF_X && insn[i - 1].dst_reg == insn[i].dst_reg && insn[i - 1].imm != 1 && - insn[i - 1].off == 0) { + insn[i - 1].off == 0 && !bpf_insn_is_jump_target(env, fp, i)) { src2_reg = bpf_to_ppc(insn[i - 1].src_reg); src2_reg_h = src2_reg - 1; if (BPF_SRC(code) == BPF_X && insn[i].src_reg == insn[i - 1].dst_reg) { diff --git a/arch/powerpc/net/bpf_jit_comp64.c b/arch/powerpc/net/bpf_jit_comp64.c index c1e86fe0bd79..d7edb2723f42 100644 --- a/arch/powerpc/net/bpf_jit_comp64.c +++ b/arch/powerpc/net/bpf_jit_comp64.c @@ -964,8 +964,9 @@ static int emit_atomic_ld_st(const struct bpf_insn insn, struct codegen_context } /* Assemble the body code between the prologue & epilogue */ -int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, u32 *fimage, struct codegen_context *ctx, - u32 *addrs, int pass, bool extra_pass) +int bpf_jit_build_body(struct bpf_verifier_env *env __always_unused, struct bpf_prog *fp, + u32 *image, u32 *fimage, struct codegen_context *ctx, u32 *addrs, int pass, + bool extra_pass) { enum stf_barrier_type stf_barrier = stf_barrier_type_get(); bool sync_emitted, ori31_emitted; diff --git a/include/linux/bpf.h b/include/linux/bpf.h index 1dd9db16f324..c1a7a0be3a9c 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -1697,6 +1697,8 @@ void bpf_jit_uncharge_modmem(u32 size); bool bpf_prog_has_trampoline(const struct bpf_prog *prog); bool bpf_insn_is_indirect_target(const struct bpf_verifier_env *env, const struct bpf_prog *prog, int insn_idx); +bool bpf_insn_is_jump_target(const struct bpf_verifier_env *env, const struct bpf_prog *prog, + int insn_idx); u16 bpf_out_stack_arg_cnt(const struct bpf_verifier_env *env, const struct bpf_prog *prog); #else static inline int bpf_trampoline_link_prog(struct bpf_tramp_node *node, diff --git a/kernel/bpf/core.c b/kernel/bpf/core.c index 0bfd23a41401..27c200f43dd5 100644 --- a/kernel/bpf/core.c +++ b/kernel/bpf/core.c @@ -1662,6 +1662,15 @@ bool bpf_insn_is_indirect_target(const struct bpf_verifier_env *env, const struc return env->insn_aux_data[insn_idx].indirect_target; } +bool bpf_insn_is_jump_target(const struct bpf_verifier_env *env, const struct bpf_prog *prog, + int insn_idx) +{ + if (!env) + return false; + insn_idx += prog->aux->subprog_start; + return env->insn_aux_data[insn_idx].jump_target; +} + u16 bpf_out_stack_arg_cnt(const struct bpf_verifier_env *env, const struct bpf_prog *prog) { const struct bpf_subprog_info *sub; -- 2.56.0