From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pf1-f174.google.com (mail-pf1-f174.google.com [209.85.210.174]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EC03A3C1412 for ; Mon, 11 May 2026 22:17:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.174 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778537839; cv=none; b=NE3c3hX+a7Z3CKXUdkqvaWhSM2gosmjzDHfemcQaazbkuuKSpNRu3qJ5bBnFvwMwasEL7Ho4Uu39BBMijsfa35NqiOiqRxn+qvMmjkRpKgR6iDMFsB7WqzTELjXbzSziGIQ8nL+nSr9rVRNR6+TKTOfLFqnXuReKHxr2wvdTe/M= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778537839; c=relaxed/simple; bh=JoVYuvHzO7OGK7CJlqRXcfoQ6RTGzrXDxxCk2RJK94s=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=a6lREGJg8pNtJvgAcBBpEnuDPONb+flZqC7I3P+VaFpPdbrIVGoZOvnCEVjU3oUY272xhLW+rqvLO5vRCriXpKw1kXK+vbdz2OEgF7/k4xauzx+ZPVaFlvgPTw5jLlKjX5CPOyoFuqiBJGZDqqnp/iGiKLxe9xefUyMzr4gaZk0= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=MmntwRi3; arc=none smtp.client-ip=209.85.210.174 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="MmntwRi3" Received: by mail-pf1-f174.google.com with SMTP id d2e1a72fcca58-8353c9f24d2so2525225b3a.3 for ; Mon, 11 May 2026 15:17:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1778537837; x=1779142637; 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; bh=WHGA0pnULoE9zOAnQoCX1h4jacBv900xuE41+ziaiRY=; b=MmntwRi3mDSwL7LOjCPKjZKQOgqHm3j2E6wL1bv7+1k/Cp76JkSV9DHaD2Q+pw6BRe YjTIXt+BvtiArKM4pwbLTWlVlVyxgijTdSCbhZqSf6tV2ff1hhCuCCue5BV5B2QEFYbE krUA9Lnot2tnCdR/LoVdUHRPt9xmYaIfFkjc8j6DG0Rf7NCSGj+L2dgxNUq9FIXVbCr4 dmGjXIl5lGchJvB65ZCMF4pnwmYKlSw9M+2oiw9FKiDwiWADmcnO6bvEzxmRi/LrCYx9 U3Af3q6CWBIyzEz1vtTu7aNQOpitolFuOe+szQVEA+NyELP498KknlT9dxxBEB5mCpXf PYSQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1778537837; x=1779142637; 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; bh=WHGA0pnULoE9zOAnQoCX1h4jacBv900xuE41+ziaiRY=; b=PV3Yov89vDLYM2wSfjRZyLdAHm0ufJ3QcwI/yeSHxdiMC6i5MkUnJMRoMHbWr51iTH zIYWlg7NRlB8Aw/ZP7GgLIb/0QL36YN1o3SQKptZuUv4yZ12cnX5o5ETfQX5r7kpPfHw bwOif5/bAbrUv+srMu2HNYEePlxZNFJooMr1x6/EFbSNFyE80J8uhJlCd5w4R5w1vRRu n/Te8xV7Hr1bN+xyWZ4V/ge21t0VW7Swm6rZRF/ov+LvyFcuVXUB4WXaQc/9V+JZWqwc FHtUdwvGe26YS/0AxQHUCzOhqxf3uFVPdnUx/D0QnWWLrAXh+BzKl+BvmegEGRNr3SuP WEjw== X-Forwarded-Encrypted: i=1; AFNElJ+YfKTFqxSnzjUrSUHUY5CL2TKqHoxGVNSjfo0PL0LEZr0AYNtQZaCzMn6CHLo0aL8/L3tGhPLr97uqEQ4=@vger.kernel.org X-Gm-Message-State: AOJu0YzWxlefS03yQB6WiXLLZUwXbofQUvviHYVGoAGVP5dW6+yZ01q9 BxoKmevYIoii0TorYrGaR0Kf8MhNwsDLCQlGPL+9qX1xfXKLvPiM6m2L X-Gm-Gg: Acq92OHwaYrz1DO1Tw6z4i101z4IrFkPuCR2D2Y5ey89jLzLrm76qhXWc1L7PkDMYbT cak2ZVNrYTtyF0iXAufQnVncwQ9+0FH3BvIXolPHkQSDRNeDhAYTXXt73Qn84jTQrFaODr0pFpE KYHVRMFkAxVlS6TjyIXg8WI2h2zPh68aplz33353n0vjNolDqVN6rhU7tzZ1yIbZWKhcFuUrDe3 aHE5Xdn7aHu5GgRIa9t2co02+GODWd/OlFqSnS83uQJoEVo4bMucrvjkZ8sfsuNsWjjRn4Ct+JD 3Ges8atiGmHyueiFY7r3Ixx80iMn+CiI4KZdUQxSoO+tSqyxAJfRJJhLPHk7Jtt7M9M8Ko5r8IW LN8GaAdyBxPoO8CiAKT1VEHowz4RQAVi6kaed5Rz39NjpjoM0qEzIvXNNED0d5fpCZIPaRgVZmV DQN/IDhAUbvSF9JNmULNghsF/7FhJOuvSG4LROFFTb47f512eSk6VdIABpJ7sQKWnorNAxcNhYA RaTi7ZQwYSufuLND7FcXBWG0syF4xxxIA== X-Received: by 2002:a05:6a00:2354:b0:837:80a:5ab4 with SMTP id d2e1a72fcca58-83eebf9e29fmr296684b3a.44.1778537837238; Mon, 11 May 2026 15:17:17 -0700 (PDT) Received: from visitorckw-work01.c.googlers.com.com (32.237.80.34.bc.googleusercontent.com. [34.80.237.32]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-83965a3e3ecsm21943329b3a.19.2026.05.11.15.17.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 May 2026 15:17:16 -0700 (PDT) From: Kuan-Wei Chiu To: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, eddyz87@gmail.com, memxor@gmail.com, luke.r.nels@gmail.com, xi.wang@gmail.com, pjw@kernel.org, palmer@dabbelt.com, aou@eecs.berkeley.edu Cc: martin.lau@linux.dev, song@kernel.org, yonghong.song@linux.dev, jolsa@kernel.org, alex@ghiti.fr, jserv@ccns.ncku.edu.tw, eleanor15x@gmail.com, marscheng@google.com, bpf@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, Kuan-Wei Chiu Subject: [PATCH bpf-next v2 2/3] riscv, bpf: Fix support for BPF_MOVSX in RV32 JIT Date: Mon, 11 May 2026 22:16:47 +0000 Message-ID: <20260511221648.3251464-3-visitorckw@gmail.com> X-Mailer: git-send-email 2.54.0.563.g4f69b47b94-goog In-Reply-To: <20260511221648.3251464-1-visitorckw@gmail.com> References: <20260511221648.3251464-1-visitorckw@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit The current rv32 bpf jit compiler incorrectly treats BPF_MOVSX as a standard zero-extended move operation. The bpf instruction set allows sign-extension moves by reusing the BPF_MOV opcode with the instruction offset set to 8, 16, or 32. Update the bpf_jit_emit_insn() function to check the offset field for both ALU and ALU64 MOV operations. If the offset is non-zero, emit the correct slli and srai instructions to perform the sign extension. Before this patch: [ 19.549705] test_bpf: #82 ALU_MOVSX | BPF_B jited:1 ret 2 != 1 (0x2 != 0x1)FAIL (1 times) [ 19.551354] test_bpf: #83 ALU_MOVSX | BPF_H jited:1 ret 2 != 1 (0x2 != 0x1)FAIL (1 times) [ 19.552576] test_bpf: #84 ALU64_MOVSX | BPF_B jited:1 ret 2 != 1 (0x2 != 0x1)FAIL (1 times) [ 19.553542] test_bpf: #85 ALU64_MOVSX | BPF_H jited:1 ret 2 != 1 (0x2 != 0x1)FAIL (1 times) [ 19.554807] test_bpf: #86 ALU64_MOVSX | BPF_W jited:1 ret 2 != 1 (0x2 != 0x1)FAIL (1 times) After this patch: [ 17.931172] test_bpf: #82 ALU_MOVSX | BPF_B jited:1 125 PASS [ 17.932198] test_bpf: #83 ALU_MOVSX | BPF_H jited:1 124 PASS [ 17.933039] test_bpf: #84 ALU64_MOVSX | BPF_B jited:1 124 PASS [ 17.933918] test_bpf: #85 ALU64_MOVSX | BPF_H jited:1 124 PASS [ 17.934751] test_bpf: #86 ALU64_MOVSX | BPF_W jited:1 122 PASS Fixes: 8100928c8814 ("bpf: Support new sign-extension mov insns") Signed-off-by: Kuan-Wei Chiu --- arch/riscv/net/bpf_jit_comp32.c | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/arch/riscv/net/bpf_jit_comp32.c b/arch/riscv/net/bpf_jit_comp32.c index 7396899ea276..f8509950fed4 100644 --- a/arch/riscv/net/bpf_jit_comp32.c +++ b/arch/riscv/net/bpf_jit_comp32.c @@ -974,6 +974,24 @@ int bpf_jit_emit_insn(const struct bpf_insn *insn, struct rv_jit_context *ctx, switch (code) { case BPF_ALU64 | BPF_MOV | BPF_X: + if (insn->off != 0) { + const s8 *rd = bpf_get_reg64(dst, tmp1, ctx); + const s8 *rs = bpf_get_reg64(src, tmp2, ctx); + + if (insn->off == 8) { + emit(rv_slli(lo(rd), lo(rs), 24), ctx); + emit(rv_srai(lo(rd), lo(rd), 24), ctx); + } else if (insn->off == 16) { + emit(rv_slli(lo(rd), lo(rs), 16), ctx); + emit(rv_srai(lo(rd), lo(rd), 16), ctx); + } else { + emit(rv_addi(lo(rd), lo(rs), 0), ctx); + } + emit(rv_srai(hi(rd), lo(rd), 31), ctx); + bpf_put_reg64(dst, rd, ctx); + break; + } + fallthrough; case BPF_ALU64 | BPF_ADD | BPF_X: case BPF_ALU64 | BPF_ADD | BPF_K: @@ -1024,6 +1042,20 @@ int bpf_jit_emit_insn(const struct bpf_insn *insn, struct rv_jit_context *ctx, emit_zext64(dst, ctx); break; } + if (insn->off != 0) { + const s8 *rd = bpf_get_reg32(dst, tmp1, ctx); + const s8 *rs = bpf_get_reg32(src, tmp2, ctx); + + if (insn->off == 8) { + emit(rv_slli(lo(rd), lo(rs), 24), ctx); + emit(rv_srai(lo(rd), lo(rd), 24), ctx); + } else if (insn->off == 16) { + emit(rv_slli(lo(rd), lo(rs), 16), ctx); + emit(rv_srai(lo(rd), lo(rd), 16), ctx); + } + bpf_put_reg32(dst, rd, ctx); + break; + } fallthrough; case BPF_ALU | BPF_ADD | BPF_X: -- 2.54.0.563.g4f69b47b94-goog