From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <dev-bounces@dpdk.org>
Received: from dpdk.org (dpdk.org [92.243.14.124])
	by inbox.dpdk.org (Postfix) with ESMTP id 0545DA0542;
	Thu, 13 Feb 2020 15:33:31 +0100 (CET)
Received: from [92.243.14.124] (localhost [127.0.0.1])
	by dpdk.org (Postfix) with ESMTP id EFFD01C00E;
	Thu, 13 Feb 2020 15:33:30 +0100 (CET)
Received: from wout3-smtp.messagingengine.com (wout3-smtp.messagingengine.com
 [64.147.123.19]) by dpdk.org (Postfix) with ESMTP id 7B3991C002
 for <dev@dpdk.org>; Thu, 13 Feb 2020 15:33:29 +0100 (CET)
Received: from compute1.internal (compute1.nyi.internal [10.202.2.41])
 by mailout.west.internal (Postfix) with ESMTP id 4E117921;
 Thu, 13 Feb 2020 09:33:26 -0500 (EST)
Received: from mailfrontend2 ([10.202.2.163])
 by compute1.internal (MEProxy); Thu, 13 Feb 2020 09:33:26 -0500
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=monjalon.net; h=
 from:to:cc:subject:date:message-id:mime-version
 :content-transfer-encoding; s=mesmtp; bh=/42L7hTyQr4tuFEKfWw7C+K
 jhCAa/ZPjEBg3XI54fSg=; b=SW+ab3QFaFdwDOy2zukyHCQRlfnBeM/+CSVuH8D
 9YqJXc5vbV3B3gfKlWJfB8E++uMJdI1ivWXoC3OXOBfLwhwimUbMeQ4FTzEoDrNC
 twJ4tzMTpWEkzi+8VxmEvwTkNG5eT3xAhDlpLt/CpfXROXNMxYj/OUSqTn88kJMb
 KAQ0=
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=
 messagingengine.com; h=cc:content-transfer-encoding:date:from
 :message-id:mime-version:subject:to:x-me-proxy:x-me-proxy
 :x-me-sender:x-me-sender:x-sasl-enc; s=fm2; bh=/42L7hTyQr4tuFEKf
 Ww7C+KjhCAa/ZPjEBg3XI54fSg=; b=XRn1GMlVFul+uRfTUIaiHOXFaAQECJwPG
 x3ASdJO4He/G/FYYQ7DxgLlliJWjtbVli9mBoqyQhnBsBsCC+x0Hfr/og/wKTeJK
 QfW/DCl9n0EqaICb2XWCpCyTvszgEQ+KAh0lySVrnsWP0BWFhrI0E9EIMS7BfpiO
 NOgnttiQg0w/mLDgfKUc5ABGcfVu9UBx6uYClhEl7Sbm8jF43+BS9+JUzJBjs9lM
 hPFiVrdOpKFVxCR3sr3NcNfREkX2kCyL0vPL6ZccgKWeC0hHsQFdQrL5bnuR99W/
 FUvlYHVSL9EE/gS23ZzrIw1/g5pMygksrdNzkFAafm/2TXu3hMIEg==
X-ME-Sender: <xms:NV5FXsR5iu_yUF5WPetgYFbVX3_zVJJwbx4UP8-k16H27gLyOVHH7w>
X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedugedrieekgdeiiecutefuodetggdotefrodftvf
 curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu
 uegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenuc
 fjughrpefhvffufffkofgggfestdekredtredttdenucfhrhhomhepvfhhohhmrghsucfo
 ohhnjhgrlhhonhcuoehthhhomhgrshesmhhonhhjrghlohhnrdhnvghtqeenucfkphepje
 ejrddufeegrddvtdefrddukeegnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghm
 pehmrghilhhfrhhomhepthhhohhmrghssehmohhnjhgrlhhonhdrnhgvth
X-ME-Proxy: <xmx:NV5FXqsdczpZw9sSTaaLJoyX7z77TO7BtxjE8joIsoiqO6XmIB6YRg>
 <xmx:NV5FXhuP2-lGU61jO1_tWTMxDByYo5B9jZwvwdA2pgl22xiFjZFITw>
 <xmx:NV5FXleCg9hItr7mGjMSWUM1k7oJ7L1OYaYlc6lRSygpOCnym-ADBA>
 <xmx:NV5FXiHioxliksZiaJ-YyQRgsn0Hl71iud3yjrRfV1CUX6T0CUgIig>
Received: from xps.monjalon.net (184.203.134.77.rev.sfr.net [77.134.203.184])
 by mail.messagingengine.com (Postfix) with ESMTPA id 891403060A08;
 Thu, 13 Feb 2020 09:33:24 -0500 (EST)
From: Thomas Monjalon <thomas@monjalon.net>
To: dev@dpdk.org
Cc: Ferruh Yigit <ferruh.yigit@intel.com>,
 Andrew Rybchenko <arybchenko@solarflare.com>
Date: Thu, 13 Feb 2020 15:33:12 +0100
Message-Id: <20200213143312.1021487-1-thomas@monjalon.net>
X-Mailer: git-send-email 2.25.0
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Subject: [dpdk-dev] [PATCH 20.05] ethdev: clear struct on releasing port
X-BeenThere: dev@dpdk.org
X-Mailman-Version: 2.1.15
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>

The function rte_eth_dev_release_port() is called by drivers
when closing or removing a device.
Its main usage should be via rte_eth_dev_close() by up-to-date
drivers which are compliant with RTE_ETH_DEV_CLOSE_REMOVE flag.

When a port is released, the data (rte_eth_dev_data) are cleared,
but the pointers in rte_eth_dev were not cleared.
It may cause issues with code paths trying to use dangling pointers
(e.g. the .device pointer which may reference a removed rte_device).
Everything is now cleared to 0 when releasing.
The state is explicitly set to RTE_ETH_DEV_UNUSED which is 0 anyway.

Using this patch may reveal bugs in some code paths:
	- device pointer must be saved before closing a port if needed
	- drivers must close ports on device remove for consistent cleanup

Signed-off-by: Thomas Monjalon <thomas@monjalon.net>
---
 lib/librte_ethdev/rte_ethdev.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/lib/librte_ethdev/rte_ethdev.c b/lib/librte_ethdev/rte_ethdev.c
index 774c721b34..2a43a9abe9 100644
--- a/lib/librte_ethdev/rte_ethdev.c
+++ b/lib/librte_ethdev/rte_ethdev.c
@@ -553,8 +553,6 @@ rte_eth_dev_release_port(struct rte_eth_dev *eth_dev)
 
 	rte_spinlock_lock(&rte_eth_dev_shared_data->ownership_lock);
 
-	eth_dev->state = RTE_ETH_DEV_UNUSED;
-
 	if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
 		rte_free(eth_dev->data->rx_queues);
 		rte_free(eth_dev->data->tx_queues);
@@ -563,6 +561,8 @@ rte_eth_dev_release_port(struct rte_eth_dev *eth_dev)
 		rte_free(eth_dev->data->dev_private);
 		memset(eth_dev->data, 0, sizeof(struct rte_eth_dev_data));
 	}
+	memset(eth_dev, 0, sizeof(struct rte_eth_dev));
+	eth_dev->state = RTE_ETH_DEV_UNUSED;
 
 	rte_spinlock_unlock(&rte_eth_dev_shared_data->ownership_lock);
 
-- 
2.25.0