From patchwork Tue May 1 18:56:03 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Salil Mehta X-Patchwork-Id: 134797 Delivered-To: patch@linaro.org Received: by 10.46.151.6 with SMTP id r6csp5281693lji; Tue, 1 May 2018 12:00:24 -0700 (PDT) X-Google-Smtp-Source: AB8JxZpQTaWKOdUs6Py3alKN06+csMN4gZd+aF3A0AzmLTMHEozdRTRVXFcVPeaRT7SRbW4fkm16 X-Received: by 2002:a17:902:8a87:: with SMTP id p7-v6mr2149566plo.278.1525201224096; Tue, 01 May 2018 12:00:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1525201224; cv=none; d=google.com; s=arc-20160816; b=cEMN/CDHdEtCYZvXsrxdWLq9ye/MFsw0SKI4gubkZsmIc6aMWkHbaRSOdRcU3eBzCF iucQ+6/P6PlWZWW0d2twss2dwBS2sSoYyY+vLB/jBG5OQRGSPvBwQrhQT9KMPSiPsTEQ a7Lesn9D9X9HK0re7g3l71ltW9sGYjo6MhOaaBKsrFtwWtI7Kgb0opIPmNDsCMeTI1sV PgMyUDFoA5CieTf+EsoCpcHYHI8CZBXFv4GyC0Azq57WpB3qhcT5SLoeVEbwQ5ucUNXQ uyxWr1zlqY85DD45tz9dBwvhdPRg/ulKR3lpTgBSx+IkiNL2TDHZIcNZ7k2gpeoHYhYP eHZw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:arc-authentication-results; bh=FJNP2jW7vJBPKVjmS1xl6bnPWM3PRVprqjq2XmC9JzU=; b=SE/+rbg42VJthlVFEC1Yj1h5Ic0eCGGj/ghnMK/NkzVQNqpSMQJ8yAAb2xqPHv+iYD jSyB5RQ13IyP03zGaw8CXTl/WEKsBpL+04aA5Fz1lHeucK9083ceFhpuy1oh+k/zoP1b SRZASRFgck2ltoxyR0nRS3xxrjG2/3LuvNzBI+HMDITwi2zLyJi/g0mcf/w/QWd5oPGL rQHIOV0Vn7DPVxuYFJmmqFiWO+A11RI/8k2UJpJkkAvvq3FxxHP4jbkJGS1o3zOsKuf5 gcWzQBCTKBPgTNoppO/xrmUlvkpLo/Ai9G6J8pnN8TCXljocaVMiNZC3bxggFMLfVMiS 5i3Q== ARC-Authentication-Results: i=1; mx.google.com; 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 c8si10177233pfj.138.2018.05.01.12.00.23; Tue, 01 May 2018 12:00:24 -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; 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 S1756646AbeEAS6I (ORCPT + 29 others); Tue, 1 May 2018 14:58:08 -0400 Received: from szxga06-in.huawei.com ([45.249.212.32]:54948 "EHLO huawei.com" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1756398AbeEAS6F (ORCPT ); Tue, 1 May 2018 14:58:05 -0400 Received: from DGGEMS402-HUB.china.huawei.com (unknown [172.30.72.59]) by Forcepoint Email with ESMTP id C93FCE11240; Wed, 2 May 2018 02:58:02 +0800 (CST) Received: from S00293818-DELL1.china.huawei.com (10.202.226.46) by DGGEMS402-HUB.china.huawei.com (10.3.19.202) with Microsoft SMTP Server id 14.3.361.1; Wed, 2 May 2018 02:57:53 +0800 From: Salil Mehta To: CC: , , , , , , , Huazhong Tan Subject: [PATCH net-next 7/9] net: hns3: fix a dead loop in hclge_cmd_csq_clean Date: Tue, 1 May 2018 19:56:03 +0100 Message-ID: <20180501185605.9584-8-salil.mehta@huawei.com> X-Mailer: git-send-email 2.8.3 In-Reply-To: <20180501185605.9584-1-salil.mehta@huawei.com> References: <20180501185605.9584-1-salil.mehta@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.202.226.46] X-CFilter-Loop: Reflected Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Huazhong Tan If head has invlid value then a dead loop can be triggered in hclge_cmd_csq_clean. This patch adds sanity check for this case. Fixes: 68c0a5c70614 ("net: hns3: Add HNS3 IMP(Integrated Mgmt Proc) Cmd Interface Support") Signed-off-by: Huazhong Tan Signed-off-by: Peng Li Signed-off-by: Salil Mehta --- .../net/ethernet/hisilicon/hns3/hns3pf/hclge_cmd.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) -- 2.7.4 diff --git a/drivers/net/ethernet/hisilicon/hns3/hns3pf/hclge_cmd.c b/drivers/net/ethernet/hisilicon/hns3/hns3pf/hclge_cmd.c index ff13d18..fab7068 100644 --- a/drivers/net/ethernet/hisilicon/hns3/hns3pf/hclge_cmd.c +++ b/drivers/net/ethernet/hisilicon/hns3/hns3pf/hclge_cmd.c @@ -31,6 +31,17 @@ static int hclge_ring_space(struct hclge_cmq_ring *ring) return ring->desc_num - used - 1; } +static int is_valid_csq_clean_head(struct hclge_cmq_ring *ring, int h) +{ + int u = ring->next_to_use; + int c = ring->next_to_clean; + + if (unlikely(h >= ring->desc_num)) + return 0; + + return u > c ? (h > c && h <= u) : (h > c || h <= u); +} + static int hclge_alloc_cmd_desc(struct hclge_cmq_ring *ring) { int size = ring->desc_num * sizeof(struct hclge_desc); @@ -141,6 +152,7 @@ static void hclge_cmd_init_regs(struct hclge_hw *hw) static int hclge_cmd_csq_clean(struct hclge_hw *hw) { + struct hclge_dev *hdev = (struct hclge_dev *)hw->back; struct hclge_cmq_ring *csq = &hw->cmq.csq; u16 ntc = csq->next_to_clean; struct hclge_desc *desc; @@ -149,6 +161,13 @@ static int hclge_cmd_csq_clean(struct hclge_hw *hw) desc = &csq->desc[ntc]; head = hclge_read_dev(hw, HCLGE_NIC_CSQ_HEAD_REG); + rmb(); /* Make sure head is ready before touch any data */ + + if (!is_valid_csq_clean_head(csq, head)) { + dev_warn(&hdev->pdev->dev, "wrong head (%d, %d-%d)\n", head, + csq->next_to_use, csq->next_to_clean); + return 0; + } while (head != ntc) { memset(desc, 0, sizeof(*desc));