From nobody Sat Jun 13 00:35:33 2026 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 620982836A0 for ; Mon, 11 May 2026 22:17:10 +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=1778537831; cv=none; b=Flhue2tjdzMrqVmegj9S7EDB3Wmlie9ovzvTsnnHi3WXNvF2flEW/CfHDdS+kPCpZSZEd4lZWJ3d2JOA4AcklGYp7P0e6W4jAwlz0wUfxCV+1A4guJezsPg0h0aUbJ66JAXxC02EW2SG3fuVMUDlUFQMk/hr9rPFUyNdTwzuyDo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778537831; c=relaxed/simple; bh=hxgxXXX9KNU29N2vt4j9TZfSsWP0fn+WmP2W06CD2sE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=J9hSOfsOoHLdGhV0+b9OixAt2vCSzBc37SXmf525Me/b9Sj2u8eKow07FIY/pE5d9oo21bC7M2YPIG3y2KRRnsyp7XhGosLtE8BpKJKWjZZ1wNn9uc6GNdJVwUb9UWVCX4ugRkkDWIZ/ztuw7YdVy/krZgcpsGz5vynk15NfEGw= 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=aQ8Ff5Vs; 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="aQ8Ff5Vs" Received: by mail-pf1-f174.google.com with SMTP id d2e1a72fcca58-8353fd1cb5fso2315511b3a.0 for ; Mon, 11 May 2026 15:17:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1778537830; x=1779142630; 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=nzbHlwFEvu/VdMCQ/9sPrwxhCrnSDlirFVz5WBJAiSo=; b=aQ8Ff5Vso1WY9UfT2ft6zG8J6KhpYLJfL1O8zvcl62gW9P23mFNozJ4zAAIDqWhH9i 3Wn6UyK+SIqdFAg54NW+Fj+Zd0d7ldNS39ryZCwQ0zRoUIYtXsg/3v6LcXZCtvcbWZZb wbBNJcChQO/iyq00swTwI8vAz+d3q4vkz6BFpJa8n5KY8Q6ikXW5LZXrmQPBJLrJivKi ot7fXOlHKGNnXNVdZzLLeA+eHorElypW/v2Y8lBVJp+EV/p+2uwWp9gQhYYy1Zjahzhj ipu75qSg1rqC7By+b2Wgdng1NhHR8BnnFkB1924p5/+dQnLKD25Se4N5fe75shfNMLAR I4aA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1778537830; x=1779142630; 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=nzbHlwFEvu/VdMCQ/9sPrwxhCrnSDlirFVz5WBJAiSo=; b=FcwO9a329Z0WB+PhOv+WWaIsm+3EOP4FVxhdCawPs+ffzwbiCoV2zgK0YDpFX/pRBF aKYoTuWoSoKkNkBx8cfZMOI3FlbusuYzLFt1dekhuvy78PQ1FVaeQodfT/Nywzv9lwmR NulP7/RzUgBaT6emsGyl3pkGziRkILTfeSB9K0IlJRzsFW9hLgUzqrFcADLN8AGLf+m5 kWE+1ibRosg8T33NKxJAokAEhi/SPVm7PlXk/IlABELtiyEXbpRKDORHbvkptJFliwmC wa1ptxIY6C6PSmCxslZjfZSpYf5Ku7NaZ7aelnwaFqOyt/ljh5VYfTvJ8+vFm93Kizjx bOcQ== X-Forwarded-Encrypted: i=1; AFNElJ9qOXiTCZUR58jIisvemfBAPCxZUs1qo0pZ3b2CNCPtpQXLUa9fJuaJl567El9sDPbPysHUX8e95Tb9tB4=@vger.kernel.org X-Gm-Message-State: AOJu0YxKmjzWj0NNj4KkBam4mYmKihebvLA7NWjjnS/+odiSapxZsW7b 6jmOeq1z7/g3ZaNOU3jlQZgMCDroPo/EGRzQ180GGpMhYxWoS9dI8P0m X-Gm-Gg: Acq92OERJfoLLx64fenF++kYJJxL55/4MT9NJfO/EUko5WH9yWDPvnyVFc0lVyMKiP8 rBncjJEjI7jZ9x1ZjUbBL7SaSc30h/xLVfOrkHLL8P04tKODOQcdvV103JL4LdykdZr/PZnnfNG JQIOI+B0VV7lfNo1u65AkLq0rAJRTRpKCYqApHjdQg15WS0Qz7qw0qhyJ2BthCrFNttmtVBxJe9 mBGPkltqiZWt/ZBJUJbmCKrBBfXaxfhiru35zfzEpRrEdZ76ECNVUfR8OZlZmzNxXTIdg/mjGR1 EzTIyEgS1vLg2ufMOvM9hT31Nfbz0sAztrxEwpbq2bp2MHvxqnYHsxd5DV9clGJ2K+kcBxftuKF z/TJlJ67OjlaSmamZD6nM3i102uC/JZ96HWntviwpMPhxkB5D/kymQXLFtZxTdz1gIUBS3Ysw4/ vhtFIg65EMt76Dc21NRBM1F1a+T4TuWHfKtlk76dXa88gwQ4cuk5PSgr8LNhMGaPqHRi3GAdW94 fEkoQP1gMTUtgCtwlSh0CB+1yX8aqe+Vg== X-Received: by 2002:a05:6a00:f0b:b0:82f:456c:29f1 with SMTP id d2e1a72fcca58-83ee83251bemr1014107b3a.12.1778537829642; Mon, 11 May 2026 15:17:09 -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.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 May 2026 15:17:09 -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 1/3] riscv, bpf: Fix support for BPF_SDIV and BPF_SMOD in RV32 JIT Date: Mon, 11 May 2026 22:16:46 +0000 Message-ID: <20260511221648.3251464-2-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: quoted-printable Content-Type: text/plain; charset="utf-8" The current rv32 bpf jit compiler incorrectly treats BPF_SDIV and BPF_SMOD as unsigned operations. The BPF instruction set allows signed division and modulo by reusing the BPF_DIV and BPF_MOD opcodes with the instruction offset set to 1. Update the emit_alu_r32() function to accept an 'is_sdiv' variable and emit the correct div and rem instructions when the offset is 1. Before this patch: [ 44.161771] test_bpf: #165 ALU_SDIV_X: -6 / 2 =3D -3 jited:1 ret 2147483= 645 !=3D -3 (0x7ffffffd !=3D 0xfffffffd)FAIL (1 times) [ 44.167385] test_bpf: #166 ALU_SDIV_K: -6 / 2 =3D -3 jited:1 ret 2147483= 645 !=3D -3 (0x7ffffffd !=3D 0xfffffffd)FAIL (1 times) [ 44.171053] test_bpf: #169 ALU_SMOD_X: -7 % 2 =3D -1 jited:1 ret 1 !=3D = -1 (0x1 !=3D 0xffffffff)FAIL (1 times) [ 44.172081] test_bpf: #170 ALU_SMOD_K: -7 % 2 =3D -1 jited:1 ret 1 !=3D = -1 (0x1 !=3D 0xffffffff)FAIL (1 times) After this patch: [ 16.002192] test_bpf: #165 ALU_SDIV_X: -6 / 2 =3D -3 jited:1 95 PASS [ 16.002983] test_bpf: #166 ALU_SDIV_K: -6 / 2 =3D -3 jited:1 1059 PASS [ 16.017167] test_bpf: #169 ALU_SMOD_X: -7 % 2 =3D -1 jited:1 136 PASS [ 16.023002] test_bpf: #170 ALU_SMOD_K: -7 % 2 =3D -1 jited:1 109 PASS Fixes: ec0e2da95f72 ("bpf: Support new signed div/mod instructions.") Signed-off-by: Kuan-Wei Chiu --- arch/riscv/net/bpf_jit_comp32.c | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/arch/riscv/net/bpf_jit_comp32.c b/arch/riscv/net/bpf_jit_comp3= 2.c index 592dd86fbf81..7396899ea276 100644 --- a/arch/riscv/net/bpf_jit_comp32.c +++ b/arch/riscv/net/bpf_jit_comp32.c @@ -509,7 +509,7 @@ static void emit_alu_r64(const s8 *dst, const s8 *src, } =20 static void emit_alu_r32(const s8 *dst, const s8 *src, - struct rv_jit_context *ctx, const u8 op) + struct rv_jit_context *ctx, const u8 op, bool is_sdiv) { const s8 *tmp1 =3D bpf2rv32[TMP_REG_1]; const s8 *tmp2 =3D bpf2rv32[TMP_REG_2]; @@ -539,10 +539,16 @@ static void emit_alu_r32(const s8 *dst, const s8 *src, emit(rv_mul(lo(rd), lo(rd), lo(rs)), ctx); break; case BPF_DIV: - emit(rv_divu(lo(rd), lo(rd), lo(rs)), ctx); + if (is_sdiv) + emit(rv_div(lo(rd), lo(rd), lo(rs)), ctx); + else + emit(rv_divu(lo(rd), lo(rd), lo(rs)), ctx); break; case BPF_MOD: - emit(rv_remu(lo(rd), lo(rd), lo(rs)), ctx); + if (is_sdiv) + emit(rv_rem(lo(rd), lo(rd), lo(rs)), ctx); + else + emit(rv_remu(lo(rd), lo(rd), lo(rs)), ctx); break; case BPF_LSH: emit(rv_sll(lo(rd), lo(rd), lo(rs)), ctx); @@ -959,6 +965,7 @@ int bpf_jit_emit_insn(const struct bpf_insn *insn, stru= ct rv_jit_context *ctx, u8 code =3D insn->code; s16 off =3D insn->off; s32 imm =3D insn->imm; + bool is_sdiv =3D false; =20 const s8 *dst =3D bpf2rv32[insn->dst_reg]; const s8 *src =3D bpf2rv32[insn->src_reg]; @@ -1041,7 +1048,9 @@ int bpf_jit_emit_insn(const struct bpf_insn *insn, st= ruct rv_jit_context *ctx, emit_imm32(tmp2, imm, ctx); src =3D tmp2; } - emit_alu_r32(dst, src, ctx, BPF_OP(code)); + if ((BPF_OP(code) =3D=3D BPF_DIV || BPF_OP(code) =3D=3D BPF_MOD) && insn= ->off =3D=3D 1) + is_sdiv =3D true; + emit_alu_r32(dst, src, ctx, BPF_OP(code), is_sdiv); break; =20 case BPF_ALU | BPF_MOV | BPF_K: @@ -1065,7 +1074,7 @@ int bpf_jit_emit_insn(const struct bpf_insn *insn, st= ruct rv_jit_context *ctx, * src is ignored---choose tmp2 as a dummy register since it * is not on the stack. */ - emit_alu_r32(dst, tmp2, ctx, BPF_OP(code)); + emit_alu_r32(dst, tmp2, ctx, BPF_OP(code), false); break; =20 case BPF_ALU | BPF_END | BPF_FROM_LE: --=20 2.54.0.563.g4f69b47b94-goog From nobody Sat Jun 13 00:35:33 2026 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: quoted-printable Content-Type: text/plain; charset="utf-8" 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 !=3D 1 (0x2 != =3D 0x1)FAIL (1 times) [ 19.551354] test_bpf: #83 ALU_MOVSX | BPF_H jited:1 ret 2 !=3D 1 (0x2 != =3D 0x1)FAIL (1 times) [ 19.552576] test_bpf: #84 ALU64_MOVSX | BPF_B jited:1 ret 2 !=3D 1 (0x2 = !=3D 0x1)FAIL (1 times) [ 19.553542] test_bpf: #85 ALU64_MOVSX | BPF_H jited:1 ret 2 !=3D 1 (0x2 = !=3D 0x1)FAIL (1 times) [ 19.554807] test_bpf: #86 ALU64_MOVSX | BPF_W jited:1 ret 2 !=3D 1 (0x2 = !=3D 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_comp3= 2.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, str= uct rv_jit_context *ctx, =20 switch (code) { case BPF_ALU64 | BPF_MOV | BPF_X: + if (insn->off !=3D 0) { + const s8 *rd =3D bpf_get_reg64(dst, tmp1, ctx); + const s8 *rs =3D bpf_get_reg64(src, tmp2, ctx); + + if (insn->off =3D=3D 8) { + emit(rv_slli(lo(rd), lo(rs), 24), ctx); + emit(rv_srai(lo(rd), lo(rd), 24), ctx); + } else if (insn->off =3D=3D 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; =20 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, s= truct rv_jit_context *ctx, emit_zext64(dst, ctx); break; } + if (insn->off !=3D 0) { + const s8 *rd =3D bpf_get_reg32(dst, tmp1, ctx); + const s8 *rs =3D bpf_get_reg32(src, tmp2, ctx); + + if (insn->off =3D=3D 8) { + emit(rv_slli(lo(rd), lo(rs), 24), ctx); + emit(rv_srai(lo(rd), lo(rd), 24), ctx); + } else if (insn->off =3D=3D 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; =20 case BPF_ALU | BPF_ADD | BPF_X: --=20 2.54.0.563.g4f69b47b94-goog From nobody Sat Jun 13 00:35:33 2026 Received: from mail-pg1-f182.google.com (mail-pg1-f182.google.com [209.85.215.182]) (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 6F4563C196B for ; Mon, 11 May 2026 22:17:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778537846; cv=none; b=GpM00z2RVfcgByuubXlINnqXH5QcVFtltjlTC4KF3Jaw3BbAxEUI7mfr1uPxRd37ReBtskNYgtgr3FLDhbv/UeMg9V2GCTuL3EYZko7Lp6mvLJHAH4sRMRML2MAOCUm2x2+Rqu/AnP21rNdF2LymepMGG7Cwg2qoEpovyEB0/H8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778537846; c=relaxed/simple; bh=GV1SY4c2fzlzo6fX5tbPqwpSIJrRqDyNam3nkfGtQJQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=SPr0/dZZKLw5x2EBSE2oVB4max4WHWtHQtdPP22oNGvxwKw9WCNZnuwPFLrbatMg6r77+FxtrEZBKw5qdnW0oMUt+sspTMhxyGzbGv5LCc4JRC7lv6hgobhYvk0f6FCiJ3dtuiCHH86x8wfJs1Z3S1k6V4+axh1kPrzez2Sdp7Q= 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=IlxWJxJx; arc=none smtp.client-ip=209.85.215.182 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="IlxWJxJx" Received: by mail-pg1-f182.google.com with SMTP id 41be03b00d2f7-c736261ee8dso2213939a12.1 for ; Mon, 11 May 2026 15:17:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1778537845; x=1779142645; 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=n7mbGY8zUp4Stmyf9OXEpNnChS/Bjn8U2B0Qfpbuf84=; b=IlxWJxJxd4N6MP7xRRCJLG65H9J/UXB9Zpec4B9LdiEAj/39l4a1kiglCXkJ0h0okJ 3wKBAvDaaBHXNZKO4XjLpAU4tmgzErWZ74fQwoG0ju338Z5eJlhRkNrWf5q6izIrSHZD AZyLT49KFLLmTHiUp3QKkKUX/4qM2mC+8P8CTQTNJ/8U07OfAYF0K/It92ufH68esErf OO8bXoLA4qorJ6o2+V1Zs+cDnZgEsA6ShqZk9wdcbRwE4iMzrAQVb+bKecFZXCdUxsA2 xmbJaktI5gC7Yz3IPp+6Pqg6eu0dSSvbz5BwaR4qhFvTGlmoCPlnpgz8PKwFA6lDK28o YT2Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1778537845; x=1779142645; 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=n7mbGY8zUp4Stmyf9OXEpNnChS/Bjn8U2B0Qfpbuf84=; b=iut50tHK0CXfwnasQptENwI83dTvk06rXLd8yHLQnn3YwlAnzyOqAk7bEHua8BRq+k +CUwGkV2rt9eMnV0qBHy0ZZ6TtDCk9TPX3YRtwXLF7UmwGREHKj3MIl/MQPAEC66fFtA Q6OBESIPZnea3Wr4PRADno4HOeQAVTADHYP/0CHM7x87Xr39yqsQI0ivfQZJUqT+HnCO /7eJlQrqChrSbCooFwd2ZXlWLTRJHa9CbGeQ4+hcr2tKWP1i1Z3dh2sPII28Ghs06BIL 8Z8Vasl7dXQMVmZ1F+TeVNoAPG2xJRaTrV8KjAsQ3YHK0EVymSyY0ooDMzuc9DeUMRpc 96Vg== X-Forwarded-Encrypted: i=1; AFNElJ+FprnAaac5KR2ssKYmVKRlpF322pumXtt8huddwdgW15Bv9HFCbHqiCf3fhsbE9ysSLBv9ZuERlFoZV9g=@vger.kernel.org X-Gm-Message-State: AOJu0Yw7Y+d+As2w2PKd6PAKA1Qxu6Gm9HLRg06wGNNltFdpSmojZZF9 SPAEeZ7VV6TgzsuTcINANd/FsOgOpg+AVbY+E57gQFuRUZIEW5q1zj6X X-Gm-Gg: Acq92OF4t0rRuKMeHk6s67OZkEx3v8bP0W9Ibu72mKBXnhKQYOd+V4qFNpKYspnckgo vRHBVMKi3sbId369u6lSBd58Yq96aG/QSx+CyvdB+lYQQ/D746l+lZlXICYHt/eyzvxH2sXZlqY Z4y/O3GRK40Ue1Wc9VyDFr1R3n7mQsuhJdOzLhVXj0zVnN72M069SPsEaN4BYESWcQUIHjp1LOU OwtNBBqDMZHneNHcwGm9Z4HuKBe1lltU4yxj2dhrIJ2NIJE0kgzYi5K1CBFRB9oLGuiP0AH7hkq fEWIxSmWYHhgvvGfFhaamkzTYfjfkP+Ea0Jb23F63jsw92Anjix2xUqeIn6zsV/I4Z6K8wCVw40 LHe/MpGuAFobQldZR32cCsxcq+5gx6+ZLMNbwYhQlgZmyb0mTbq7oaLrqvmgHM6jARUBrRFzdGb r8/thpafIy2ld8I4oJG3bF6rW6xaRWSpICQx/wYn5b7g6+BfwaOm3i9oc7a6KoEayN+dJJ0xf1K 4+TqD4tCvN+z7QPz8lBZDZaHtO+MuGa9A== X-Received: by 2002:a05:6a20:6a09:b0:3a2:f14a:4290 with SMTP id adf61e73a8af0-3aa5aa7a02dmr27720750637.38.1778537844766; Mon, 11 May 2026 15:17:24 -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.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 May 2026 15:17:24 -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 3/3] riscv, bpf: Add 32 bit atomic operations to RV32 JIT Date: Mon, 11 May 2026 22:16:48 +0000 Message-ID: <20260511221648.3251464-4-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: quoted-printable Content-Type: text/plain; charset="utf-8" The RV32 BPF JIT compiler currently only supports the BPF_ADD atomic operation. Other 32 bit atomic operations (and, or, xor, xchg) and their BPF_FETCH variants are not supported and gracefully fall back to the interpreter. Since the RISC-V A extension is required for Linux on RV32, we can natively support these 32-bit BPF atomic operations by mapping them directly to the corresponding RISC-V amo*.w instructions. Implement BPF_ADD, BPF_AND, BPF_OR, BPF_XOR, and BPF_XCHG with and without BPF_FETCH. BPF_CMPXCHG requires a more complex lr.w/sc.w loop and is left to fall back to the interpreter. Before this patch: [ 138.862161] test_bpf: Summary: 1054 PASSED, 0 FAILED, [843/1042 JIT'ed] After this patch: [ 157.024124] test_bpf: Summary: 1054 PASSED, 0 FAILED, [902/1042 JIT'ed] Signed-off-by: Kuan-Wei Chiu --- arch/riscv/net/bpf_jit_comp32.c | 50 +++++++++++++++++++++++++-------- 1 file changed, 39 insertions(+), 11 deletions(-) diff --git a/arch/riscv/net/bpf_jit_comp32.c b/arch/riscv/net/bpf_jit_comp3= 2.c index f8509950fed4..7fd726a09b26 100644 --- a/arch/riscv/net/bpf_jit_comp32.c +++ b/arch/riscv/net/bpf_jit_comp32.c @@ -877,7 +877,7 @@ static int emit_load_r64(const s8 *dst, const s8 *src, = s16 off, =20 static int emit_store_r64(const s8 *dst, const s8 *src, s16 off, struct rv_jit_context *ctx, const u8 size, - const u8 mode) + const u8 mode, s32 imm) { const s8 *tmp1 =3D bpf2rv32[TMP_REG_1]; const s8 *tmp2 =3D bpf2rv32[TMP_REG_2]; @@ -902,11 +902,43 @@ static int emit_store_r64(const s8 *dst, const s8 *sr= c, s16 off, case BPF_MEM: emit(rv_sw(RV_REG_T0, 0, lo(rs)), ctx); break; - case BPF_ATOMIC: /* Only BPF_ADD supported */ - emit(rv_amoadd_w(RV_REG_ZERO, lo(rs), RV_REG_T0, 0, 0), - ctx); + case BPF_ATOMIC: + { + bool is_fetch =3D (imm & BPF_FETCH) || (imm =3D=3D BPF_XCHG); + s8 fetch_reg =3D is_fetch ? lo(rs) : RV_REG_ZERO; + int aq =3D is_fetch ? 1 : 0; + int rl =3D is_fetch ? 1 : 0; + + switch (imm) { + case BPF_ADD: + case BPF_ADD | BPF_FETCH: + emit(rv_amoadd_w(fetch_reg, lo(rs), RV_REG_T0, aq, rl), ctx); + break; + case BPF_AND: + case BPF_AND | BPF_FETCH: + emit(rv_amoand_w(fetch_reg, lo(rs), RV_REG_T0, aq, rl), ctx); + break; + case BPF_OR: + case BPF_OR | BPF_FETCH: + emit(rv_amoor_w(fetch_reg, lo(rs), RV_REG_T0, aq, rl), ctx); + break; + case BPF_XOR: + case BPF_XOR | BPF_FETCH: + emit(rv_amoxor_w(fetch_reg, lo(rs), RV_REG_T0, aq, rl), ctx); + break; + case BPF_XCHG: + emit(rv_amoswap_w(fetch_reg, lo(rs), RV_REG_T0, aq, rl), ctx); + break; + default: + return -1; + } + if (is_fetch) { + emit(rv_addi(hi(rs), RV_REG_ZERO, 0), ctx); + bpf_put_reg64(src, rs, ctx); + } break; } + } break; case BPF_DW: emit(rv_sw(RV_REG_T0, 0, lo(rs)), ctx); @@ -1308,20 +1340,16 @@ int bpf_jit_emit_insn(const struct bpf_insn *insn, = struct rv_jit_context *ctx, } =20 if (emit_store_r64(dst, src, off, ctx, BPF_SIZE(code), - BPF_MODE(code))) + BPF_MODE(code), 0)) return -1; break; =20 case BPF_STX | BPF_ATOMIC | BPF_W: - if (insn->imm !=3D BPF_ADD) { - pr_info_once( - "bpf-jit: not supported: atomic operation %02x ***\n", - insn->imm); + if (insn->imm =3D=3D BPF_CMPXCHG) return -EFAULT; - } =20 if (emit_store_r64(dst, src, off, ctx, BPF_SIZE(code), - BPF_MODE(code))) + BPF_MODE(code), insn->imm)) return -1; break; =20 --=20 2.54.0.563.g4f69b47b94-goog