Received: by 2002:ac0:bc90:0:0:0:0:0 with SMTP id a16csp5584877img; Wed, 27 Mar 2019 11:09:48 -0700 (PDT) X-Google-Smtp-Source: APXvYqwI5za8VRlJqCcBKs+SCX061hQyEsVYydboBCF0nScPd0AUEEj4X62pkoMRn0rhSB621ZpA X-Received: by 2002:a62:ab14:: with SMTP id p20mr37722839pff.23.1553710188469; Wed, 27 Mar 2019 11:09:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1553710188; cv=none; d=google.com; s=arc-20160816; b=AKLrSBn5IIxm9CJVltsrloOT++oZNn+wOHLfc8CBDDLjGJIWfL1Kfgsjxjb/3fIB5D yPQAradU0KDreSFrQWqdG0s9deU1QFq/q7fuWtKuErKaxnSWmSl4DCvy8vgZdHcttLTk dZZ0DnsmNT+U54hkMktDPLTCL3xa26onb/BEeGgYWSqi/mnd1YnnC9sBY3nou3X05d1d cvnUOuUBQ5X2tHyyy9fBO/zSXfh7MJB3ji9F3dYPoqiX3lCk1XnP1ron58caw7exvKkF uCVVsYLLiHo9YcMo1xz2VpmY6NcJrO5f1onFo6ToabWxH40Kj4+TvpSmm9CwqNKF07V2 7tew== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=AQ8+upkyt9av1q1MkcXyYQHji7RCNxamqSLNs9url2c=; b=aTXKuonYi6MY6DoM7iD9snL96hyP6kRg5VsY8nyVlOfjyL7vVgiXN8yptM5yJVr9+A hRsZSzkH+IbAWPXkQc8FK2VhvTF6RmsB+656MMCDQ/IvIJwOqJAJd0/G1gA8C0OyQXNN cKsXTLuVdNGPY/xdUjsDoFsaEcCaP7fBuOGwBAUv8mj05iUx2SzOaeju+h2g43WOiNxk fDog7g/H6EZjv7G+1qfDZ3DfDY1YC4LrRA7S0o6yn59xZh9yZ5OAG1uchfaHE8c4qGYX 8NBYCwlOwdTWIKbOamuyBZP4MsV9RCPrLyVDTG2xGmQ0UB5QRjsweiJdbc1vvxFetpRB sP0Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=QSLgfmjP; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id m2si10977339pll.44.2019.03.27.11.09.32; Wed, 27 Mar 2019 11:09:48 -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=QSLgfmjP; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2388310AbfC0SIM (ORCPT + 99 others); Wed, 27 Mar 2019 14:08:12 -0400 Received: from mail.kernel.org ([198.145.29.99]:50104 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2387496AbfC0SIK (ORCPT ); Wed, 27 Mar 2019 14:08:10 -0400 Received: from sasha-vm.mshome.net (c-73-47-72-35.hsd1.nh.comcast.net [73.47.72.35]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 6021F217F9; Wed, 27 Mar 2019 18:08:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1553710089; bh=Mz3LRQ9HvV8YCCPHLZPUu5Dq2PDMcYBbKYO9oKBC54w=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=QSLgfmjPAewcRAgf3OzHU+iaAH1zM5/5s7p97IetKhw74XFfUiSaAtuGrj0/lKD7G i9USb5pJErah2Ygb4O+W/47H+s4wTl2AYgAaw4QMsLkXVmRgOuV/yPOOj/etQ/qqVX PsxZwpcv37yw5qs6EtR/6dO9VAD69LcW0twVV4PY= From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Jiong Wang , Alexei Starovoitov , Sasha Levin , netdev@vger.kernel.org, bpf@vger.kernel.org, oss-drivers@netronome.com Subject: [PATCH AUTOSEL 5.0 192/262] nfp: bpf: correct the behavior for shifts by zero Date: Wed, 27 Mar 2019 14:00:47 -0400 Message-Id: <20190327180158.10245-192-sashal@kernel.org> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20190327180158.10245-1-sashal@kernel.org> References: <20190327180158.10245-1-sashal@kernel.org> MIME-Version: 1.0 X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Jiong Wang [ Upstream commit db0a4b3b6b83a081a9ec309cc8178e5c9b4706a5 ] Shifts by zero do nothing, and should be treated as nops. Even though compiler is not supposed to generate such instructions and manual written assembly is unlikely to have them, but they are legal instructions and have defined behavior. This patch correct existing shifts code-gen to make sure they do nothing when shift amount is zero except when the instruction is ALU32 for which high bits need to be cleared. For shift amount bigger than type size, already, NFP JIT back-end errors out for immediate shift and only low 5 bits will be taken into account for indirect shift which is the same as x86. Reviewed-by: Jakub Kicinski Signed-off-by: Jiong Wang Signed-off-by: Alexei Starovoitov Signed-off-by: Sasha Levin --- drivers/net/ethernet/netronome/nfp/bpf/jit.c | 30 +++++++++++++------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/drivers/net/ethernet/netronome/nfp/bpf/jit.c b/drivers/net/ethernet/netronome/nfp/bpf/jit.c index 0a868c829b90..b71ecde7e1d2 100644 --- a/drivers/net/ethernet/netronome/nfp/bpf/jit.c +++ b/drivers/net/ethernet/netronome/nfp/bpf/jit.c @@ -1958,6 +1958,9 @@ static int neg_reg64(struct nfp_prog *nfp_prog, struct nfp_insn_meta *meta) */ static int __shl_imm64(struct nfp_prog *nfp_prog, u8 dst, u8 shift_amt) { + if (!shift_amt) + return 0; + if (shift_amt < 32) { emit_shf(nfp_prog, reg_both(dst + 1), reg_a(dst + 1), SHF_OP_NONE, reg_b(dst), SHF_SC_R_DSHF, @@ -2070,6 +2073,9 @@ static int shl_reg64(struct nfp_prog *nfp_prog, struct nfp_insn_meta *meta) */ static int __shr_imm64(struct nfp_prog *nfp_prog, u8 dst, u8 shift_amt) { + if (!shift_amt) + return 0; + if (shift_amt < 32) { emit_shf(nfp_prog, reg_both(dst), reg_a(dst + 1), SHF_OP_NONE, reg_b(dst), SHF_SC_R_DSHF, shift_amt); @@ -2171,6 +2177,9 @@ static int shr_reg64(struct nfp_prog *nfp_prog, struct nfp_insn_meta *meta) */ static int __ashr_imm64(struct nfp_prog *nfp_prog, u8 dst, u8 shift_amt) { + if (!shift_amt) + return 0; + if (shift_amt < 32) { emit_shf(nfp_prog, reg_both(dst), reg_a(dst + 1), SHF_OP_NONE, reg_b(dst), SHF_SC_R_DSHF, shift_amt); @@ -2379,10 +2388,13 @@ static int neg_reg(struct nfp_prog *nfp_prog, struct nfp_insn_meta *meta) static int __ashr_imm(struct nfp_prog *nfp_prog, u8 dst, u8 shift_amt) { - /* Set signedness bit (MSB of result). */ - emit_alu(nfp_prog, reg_none(), reg_a(dst), ALU_OP_OR, reg_imm(0)); - emit_shf(nfp_prog, reg_both(dst), reg_none(), SHF_OP_ASHR, reg_b(dst), - SHF_SC_R_SHF, shift_amt); + if (shift_amt) { + /* Set signedness bit (MSB of result). */ + emit_alu(nfp_prog, reg_none(), reg_a(dst), ALU_OP_OR, + reg_imm(0)); + emit_shf(nfp_prog, reg_both(dst), reg_none(), SHF_OP_ASHR, + reg_b(dst), SHF_SC_R_SHF, shift_amt); + } wrp_immed(nfp_prog, reg_both(dst + 1), 0); return 0; @@ -2424,12 +2436,10 @@ static int shl_imm(struct nfp_prog *nfp_prog, struct nfp_insn_meta *meta) { const struct bpf_insn *insn = &meta->insn; - if (!insn->imm) - return 1; /* TODO: zero shift means indirect */ - - emit_shf(nfp_prog, reg_both(insn->dst_reg * 2), - reg_none(), SHF_OP_NONE, reg_b(insn->dst_reg * 2), - SHF_SC_L_SHF, insn->imm); + if (insn->imm) + emit_shf(nfp_prog, reg_both(insn->dst_reg * 2), + reg_none(), SHF_OP_NONE, reg_b(insn->dst_reg * 2), + SHF_SC_L_SHF, insn->imm); wrp_immed(nfp_prog, reg_both(insn->dst_reg * 2 + 1), 0); return 0; -- 2.19.1