Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp836520ybi; Fri, 12 Jul 2019 05:28:05 -0700 (PDT) X-Google-Smtp-Source: APXvYqzpANUxorgw9GnaEwKmtp2Aon7S5k16meAJmNgPeDDPBejatmznE1Y6fh/FfWzjSppandUC X-Received: by 2002:a65:4347:: with SMTP id k7mr10679277pgq.253.1562934485274; Fri, 12 Jul 2019 05:28:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1562934485; cv=none; d=google.com; s=arc-20160816; b=LQsm6ES/MSeY929hLsdMR/LSckK6nm15SBOUQvZSZxAZ2whhcH0oxKzcd+31A0utCG TrmWnbI6WuE9fZQAsorTT2lMd88J4qO5MBjEfmEpEVJElWDhHWmx25+MGYec4E9hrZu8 zlRPyAHilmAWAA+54/N+ZKUI+mLA/xKVaxfGZ2ogl72W15gY6dhtkzMYJnc5wkhA17M6 CkEEPf07gL+znut3oXqCpmalSKM65WaRT2XBpgRsliZyzO9ffm8znS2kJTlIHzBQEIIu t393ul7n0VLUVn7WlTaa3I+hUwoW/PCmmOq/gvhATKKHz4v0YXl4F4H1W82ysSOx4yfg Uj8w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=Hn2b8IaTF8FkSPBL92fmv7rDE/Pe+y1PVHrIl+jM6wk=; b=RjPOe6/eI9kNB/3Z2Ohn7U5cPO47nMHZGPDjXshGm0pXISbACJJxWfU1bbpd7N7s0f R1Iw2/1OXrv5DSSc4c5Gd1uWWx6ZMQoKIIpIaIg0hgDxLPoM2AL4DrNXISPJUmc85rQK U0rE9IdIbaLObPfDtMe0eWwDxS786vZng28lgR5qCL9w2lpmIpIrIDWmf3dckYle5La1 YodQDMDfZmo7QC7WHWgHV8Va8UCRhls5nHNBI4jPq0r7RBepBGDfpKfVq/QyODEnukTa DQxrdK7AM2QiX58PRXDglFhY9ndUFRRpzHzYar9UCxOtHFUIELjyO2LS2tRKuAzRYEEM 4xPQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=IzaOE7wd; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id h18si7577517pjt.9.2019.07.12.05.27.49; Fri, 12 Jul 2019 05:28:05 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=IzaOE7wd; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728347AbfGLMZu (ORCPT + 99 others); Fri, 12 Jul 2019 08:25:50 -0400 Received: from mail.kernel.org ([198.145.29.99]:36014 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727391AbfGLMZm (ORCPT ); Fri, 12 Jul 2019 08:25:42 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id EBCE7216C4; Fri, 12 Jul 2019 12:25:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1562934341; bh=QgeCaSs0WFCBBb9xg0R1fRjrrhR04XELJAOQkKcP1bc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=IzaOE7wd8qMtNtHqG8ieYPuRB9wH0ymeuHM59RrDz++WgbXtZHIEozUAd+yvm6wk8 IzdsvgMkeqCsnnH5Dcpq6S7lFIjFvPTqhhj+DoIm0Hu0tDiV6X9QbA7ayeJ8LaQbue wWfU+Zz9YOySf6kpE4GcPIANb6O+jEw6np9LVfsI= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Xi Wang , Luke Nelson , Song Liu , =?UTF-8?q?Bj=C3=B6rn=20T=C3=B6pel?= , Palmer Dabbelt , Alexei Starovoitov , Sasha Levin Subject: [PATCH 5.1 023/138] bpf, riscv: clear high 32 bits for ALU32 add/sub/neg/lsh/rsh/arsh Date: Fri, 12 Jul 2019 14:18:07 +0200 Message-Id: <20190712121629.597450597@linuxfoundation.org> X-Mailer: git-send-email 2.22.0 In-Reply-To: <20190712121628.731888964@linuxfoundation.org> References: <20190712121628.731888964@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org [ Upstream commit 1e692f09e091bf5c8b38384f297d6dae5dbf0f12 ] In BPF, 32-bit ALU operations should zero-extend their results into the 64-bit registers. The current BPF JIT on RISC-V emits incorrect instructions that perform sign extension only (e.g., addw, subw) on 32-bit add, sub, lsh, rsh, arsh, and neg. This behavior diverges from the interpreter and JITs for other architectures. This patch fixes the bugs by performing zero extension on the destination register of 32-bit ALU operations. Fixes: 2353ecc6f91f ("bpf, riscv: add BPF JIT for RV64G") Cc: Xi Wang Signed-off-by: Luke Nelson Acked-by: Song Liu Acked-by: Björn Töpel Reviewed-by: Palmer Dabbelt Signed-off-by: Alexei Starovoitov Signed-off-by: Sasha Levin --- arch/riscv/net/bpf_jit_comp.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/arch/riscv/net/bpf_jit_comp.c b/arch/riscv/net/bpf_jit_comp.c index e5c8d675bd6e..426d5c33ea90 100644 --- a/arch/riscv/net/bpf_jit_comp.c +++ b/arch/riscv/net/bpf_jit_comp.c @@ -751,10 +751,14 @@ static int emit_insn(const struct bpf_insn *insn, struct rv_jit_context *ctx, case BPF_ALU | BPF_ADD | BPF_X: case BPF_ALU64 | BPF_ADD | BPF_X: emit(is64 ? rv_add(rd, rd, rs) : rv_addw(rd, rd, rs), ctx); + if (!is64) + emit_zext_32(rd, ctx); break; case BPF_ALU | BPF_SUB | BPF_X: case BPF_ALU64 | BPF_SUB | BPF_X: emit(is64 ? rv_sub(rd, rd, rs) : rv_subw(rd, rd, rs), ctx); + if (!is64) + emit_zext_32(rd, ctx); break; case BPF_ALU | BPF_AND | BPF_X: case BPF_ALU64 | BPF_AND | BPF_X: @@ -795,14 +799,20 @@ static int emit_insn(const struct bpf_insn *insn, struct rv_jit_context *ctx, case BPF_ALU | BPF_LSH | BPF_X: case BPF_ALU64 | BPF_LSH | BPF_X: emit(is64 ? rv_sll(rd, rd, rs) : rv_sllw(rd, rd, rs), ctx); + if (!is64) + emit_zext_32(rd, ctx); break; case BPF_ALU | BPF_RSH | BPF_X: case BPF_ALU64 | BPF_RSH | BPF_X: emit(is64 ? rv_srl(rd, rd, rs) : rv_srlw(rd, rd, rs), ctx); + if (!is64) + emit_zext_32(rd, ctx); break; case BPF_ALU | BPF_ARSH | BPF_X: case BPF_ALU64 | BPF_ARSH | BPF_X: emit(is64 ? rv_sra(rd, rd, rs) : rv_sraw(rd, rd, rs), ctx); + if (!is64) + emit_zext_32(rd, ctx); break; /* dst = -dst */ @@ -810,6 +820,8 @@ static int emit_insn(const struct bpf_insn *insn, struct rv_jit_context *ctx, case BPF_ALU64 | BPF_NEG: emit(is64 ? rv_sub(rd, RV_REG_ZERO, rd) : rv_subw(rd, RV_REG_ZERO, rd), ctx); + if (!is64) + emit_zext_32(rd, ctx); break; /* dst = BSWAP##imm(dst) */ @@ -964,14 +976,20 @@ static int emit_insn(const struct bpf_insn *insn, struct rv_jit_context *ctx, case BPF_ALU | BPF_LSH | BPF_K: case BPF_ALU64 | BPF_LSH | BPF_K: emit(is64 ? rv_slli(rd, rd, imm) : rv_slliw(rd, rd, imm), ctx); + if (!is64) + emit_zext_32(rd, ctx); break; case BPF_ALU | BPF_RSH | BPF_K: case BPF_ALU64 | BPF_RSH | BPF_K: emit(is64 ? rv_srli(rd, rd, imm) : rv_srliw(rd, rd, imm), ctx); + if (!is64) + emit_zext_32(rd, ctx); break; case BPF_ALU | BPF_ARSH | BPF_K: case BPF_ALU64 | BPF_ARSH | BPF_K: emit(is64 ? rv_srai(rd, rd, imm) : rv_sraiw(rd, rd, imm), ctx); + if (!is64) + emit_zext_32(rd, ctx); break; /* JUMP off */ -- 2.20.1