From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <dev-bounces@dpdk.org>
Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124])
	by inbox.dpdk.org (Postfix) with ESMTP id 8372BA0C43;
	Thu, 21 Oct 2021 04:28:47 +0200 (CEST)
Received: from [217.70.189.124] (localhost [127.0.0.1])
	by mails.dpdk.org (Postfix) with ESMTP id 0B9AB40150;
	Thu, 21 Oct 2021 04:28:47 +0200 (CEST)
Received: from szxga08-in.huawei.com (szxga08-in.huawei.com [45.249.212.255])
 by mails.dpdk.org (Postfix) with ESMTP id DF09340142
 for <dev@dpdk.org>; Thu, 21 Oct 2021 04:28:45 +0200 (CEST)
Received: from dggemv711-chm.china.huawei.com (unknown [172.30.72.55])
 by szxga08-in.huawei.com (SkyGuard) with ESMTP id 4HZWZm4drZz1DHpJ;
 Thu, 21 Oct 2021 10:26:56 +0800 (CST)
Received: from dggema767-chm.china.huawei.com (10.1.198.209) by
 dggemv711-chm.china.huawei.com (10.1.198.66) with Microsoft SMTP Server
 (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256) id
 15.1.2308.15; Thu, 21 Oct 2021 10:28:40 +0800
Received: from localhost.localdomain (10.67.165.24) by
 dggema767-chm.china.huawei.com (10.1.198.209) with Microsoft SMTP Server
 (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id
 15.1.2308.15; Thu, 21 Oct 2021 10:28:40 +0800
From: Huisong Li <lihuisong@huawei.com>
To: <dev@dpdk.org>
CC: <thomas@monjalon.net>, <ferruh.yigit@intel.com>,
 <aman.deep.singh@intel.com>, <andrew.rybchenko@oktetlabs.ru>,
 <anatoly.burakov@intel.com>, <lihuisong@huawei.com>
Date: Thu, 21 Oct 2021 10:24:21 +0800
Message-ID: <20211021022421.9306-1-lihuisong@huawei.com>
X-Mailer: git-send-email 2.33.0
In-Reply-To: <1627908397-51565-1-git-send-email-lihuisong@huawei.com>
References: <1627908397-51565-1-git-send-email-lihuisong@huawei.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Content-Type: text/plain
X-Originating-IP: [10.67.165.24]
X-ClientProxiedBy: dggems705-chm.china.huawei.com (10.3.19.182) To
 dggema767-chm.china.huawei.com (10.1.198.209)
X-CFilter-Loop: Reflected
Subject: [dpdk-dev] [PATCH V4] ethdev: fix eth device released repeatedly
X-BeenThere: dev@dpdk.org
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: DPDK patches and discussions <dev.dpdk.org>
List-Unsubscribe: <https://mails.dpdk.org/options/dev>,
 <mailto:dev-request@dpdk.org?subject=unsubscribe>
List-Archive: <http://mails.dpdk.org/archives/dev/>
List-Post: <mailto:dev@dpdk.org>
List-Help: <mailto:dev-request@dpdk.org?subject=help>
List-Subscribe: <https://mails.dpdk.org/listinfo/dev>,
 <mailto:dev-request@dpdk.org?subject=subscribe>
Errors-To: dev-bounces@dpdk.org
Sender: "dev" <dev-bounces@dpdk.org>

In secondary process, rte_eth_dev_close() doesn't clear eth_dev->data.
If calling rte_dev_remove() after rte_eth_dev_close(),
in rte_eth_dev_pci_generic_remove() function, the released eth device still
can be found by its name in shared memory. As a result, the eth device will
be released repeatedly. The state of the eth device is modified to
RTE_ETH_DEV_UNUSED after rte_eth_dev_close(). So this state can be used to
avoid this problem.

Fixes: dcd5c8112bc3 ("ethdev: add PCI driver helpers")
Cc: stable@dpdk.org

Signed-off-by: Huisong Li <lihuisong@huawei.com>
---
v4:
 * add fixes line and stable tag.
v3:
 * fix the commit log description and the comment inside the code.
v2:                                                                  
  * fix the commit log description.   
v1:                                                                 
  * fix commit log and add a judgment for secondary process.
---
 lib/ethdev/ethdev_pci.h | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/lib/ethdev/ethdev_pci.h b/lib/ethdev/ethdev_pci.h
index 8edca82ce8..fcabae02fa 100644
--- a/lib/ethdev/ethdev_pci.h
+++ b/lib/ethdev/ethdev_pci.h
@@ -151,6 +151,16 @@ rte_eth_dev_pci_generic_remove(struct rte_pci_device *pci_dev,
 	if (!eth_dev)
 		return 0;
 
+	/*
+	 * In secondary process, a released eth device can be found by its name
+	 * in shared memory.
+	 * If the state of the eth device is RTE_ETH_DEV_UNUSED, it means the
+	 * eth device has been released.
+	 */
+	if (rte_eal_process_type() == RTE_PROC_SECONDARY &&
+	    eth_dev->state == RTE_ETH_DEV_UNUSED)
+		return 0;
+
 	if (dev_uninit) {
 		ret = dev_uninit(eth_dev);
 		if (ret)
-- 
2.33.0