Received: by 10.192.165.148 with SMTP id m20csp5271896imm; Tue, 1 May 2018 12:01:19 -0700 (PDT) X-Google-Smtp-Source: AB8JxZrrO0BzMhduzaHrI8cBgI5swO5o/Lvvn8ExMh/ElrnqgL2x4s92Ogok6CulLmQMYALTRmtY X-Received: by 2002:a65:6645:: with SMTP id z5-v6mr9930427pgv.43.1525201279826; Tue, 01 May 2018 12:01:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1525201279; cv=none; d=google.com; s=arc-20160816; b=IfmGROk2RIad/9FMSZ18diBehpXNiyJ9MxNHlwnTQEN0RPbhUTXER11rX9KsQ55Ba6 x7Pow9vlULiV6Wl7x/QXoBbgslQJpBH0gps3mAt+WkAz8FOjtoT5OOpTByHITaKCrDtS Vgoso2AndSXjkgIr0BbJiKjo258I6zDWoPSRg/1N4FsqIRMA01LpPQrhw4YLR1hOkbHc IMpdv2MpPuW6LB3UX/XvwQiB4xVXH/ncIV3SXuSXjzZVvS2zjQCtzaOQUxkKuk8G3tEc cw63yp5NP+de2UrEs3zaVZ5BpJ44O1E58UmtJgO8PyL9GXwVToBo8mb37cNqqdwAZkwE D2jQ== 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 w4-v6si8889051plp.130.2018.05.01.12.01.04; Tue, 01 May 2018 12:01:19 -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 S1756639AbeEAS6I (ORCPT + 99 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 Content-Type: text/plain 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(+) 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)); -- 2.7.4