From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <stable-bounces@dpdk.org>
Received: from dpdk.org (dpdk.org [92.243.14.124])
	by inbox.dpdk.org (Postfix) with ESMTP id 48C3DA0093
	for <public@inbox.dpdk.org>; Tue, 19 May 2020 15:11:37 +0200 (CEST)
Received: from [92.243.14.124] (localhost [127.0.0.1])
	by dpdk.org (Postfix) with ESMTP id 402D01D713;
	Tue, 19 May 2020 15:11:37 +0200 (CEST)
Received: from mail-wm1-f66.google.com (mail-wm1-f66.google.com
 [209.85.128.66]) by dpdk.org (Postfix) with ESMTP id C03AD1D70C
 for <stable@dpdk.org>; Tue, 19 May 2020 15:11:35 +0200 (CEST)
Received: by mail-wm1-f66.google.com with SMTP id m12so3029515wmc.0
 for <stable@dpdk.org>; Tue, 19 May 2020 06:11:35 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025;
 h=from:to:cc:subject:date:message-id:in-reply-to:references
 :mime-version:content-transfer-encoding;
 bh=/F6Lg8qpJG8J1ndO3ut2oky5SVNG/+XOXebRd6j3Xqc=;
 b=Xg5yJ17lOI4IVmoKz/WneVPmDJ0l4ZxvGwHTtqXn22K8kmZwsX/8PzS2WQtBYNWW7n
 ncJ6UIrQLP6oDrEjPA+nR3kslQmLqXBerVvyXbKNJ6seORQdOkQayj0zHuNgc8QSb4Vn
 XMFhB7IB/p/bAmI+4l558naUJUt6ojxE0PRwz/JNobGICFMQoFRZzPAfqFCS1cgRZov+
 k/CdYbaPsLbPqpivnWXh9xdYYMkcVmWrcUmquTlGMdmaigsyj/FFWaJ0F2wgxXXTQ71i
 OEKWJU5E/wEcYM1oAO/R1pBkT6KOLD4YU9Ak1FBWpIM32ZggVB8zxsEUikp6n05sRxog
 s8vw==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=1e100.net; s=20161025;
 h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to
 :references:mime-version:content-transfer-encoding;
 bh=/F6Lg8qpJG8J1ndO3ut2oky5SVNG/+XOXebRd6j3Xqc=;
 b=mPIMxlwpxEM61ozO4ta6Xd6enzC4RLsCO4N3F7xHPUx2KDT3q8ylmz+yxgjp2UXTOv
 Odfkeyn8SF9unqOpAFrU+NzY/1SJkSsElaJoEiBOCC3XVU9AAmfy061EF6WQpghpUotk
 /3PN8FQZqf7AQ4l1fj0OdVyIBI8DikupaGKF0LEJ/W8I75MpeJ8DRLNtLhjcF4SRSClw
 W1jfrVCVWhi4HWGTtRVJpNQaMA21M+zzkX/EJ+Ne6Gv4Tlktu9ISpKw2dONH826WeKY4
 /8zdBknYAVvtZ1/7aHin2PAxzRtxKMWae/4yDWU+QmZMD4HhO5yVpZkmVsceZkWcltTt
 Cc2g==
X-Gm-Message-State: AOAM5319tBR46vGs1sHoMelTLcFo/JFihJOSFxg5TT7whCjSf6auqn33
 wi1YTFVRCJA2vsSJKSNQhBM=
X-Google-Smtp-Source: ABdhPJzCJ4+TjCPQKtQ1qtPDpIV8mKMQKNQPy7a3TXojTHz9JF+AOultg2QkR6exH/q7wiMHsQyK6w==
X-Received: by 2002:a1c:6506:: with SMTP id z6mr5710341wmb.104.1589893895475; 
 Tue, 19 May 2020 06:11:35 -0700 (PDT)
Received: from localhost ([88.98.246.218])
 by smtp.gmail.com with ESMTPSA id w18sm21114313wro.33.2020.05.19.06.11.34
 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
 Tue, 19 May 2020 06:11:34 -0700 (PDT)
From: luca.boccassi@gmail.com
To: Tao Zhu <taox.zhu@intel.com>
Cc: Konstantin Ananyev <konstantin.ananyev@intel.com>,
 Xiaolong Ye <xiaolong.ye@intel.com>, dpdk stable <stable@dpdk.org>
Date: Tue, 19 May 2020 14:04:04 +0100
Message-Id: <20200519130549.112823-109-luca.boccassi@gmail.com>
X-Mailer: git-send-email 2.20.1
In-Reply-To: <20200519130549.112823-1-luca.boccassi@gmail.com>
References: <20200519125804.104349-1-luca.boccassi@gmail.com>
 <20200519130549.112823-1-luca.boccassi@gmail.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Subject: [dpdk-stable] patch 'net/ixgbe: fix resource leak after thread
	exits normally' has been queued to stable release 19.11.3
X-BeenThere: stable@dpdk.org
X-Mailman-Version: 2.1.15
Precedence: list
List-Id: patches for DPDK stable branches <stable.dpdk.org>
List-Unsubscribe: <https://mails.dpdk.org/options/stable>,
 <mailto:stable-request@dpdk.org?subject=unsubscribe>
List-Archive: <http://mails.dpdk.org/archives/stable/>
List-Post: <mailto:stable@dpdk.org>
List-Help: <mailto:stable-request@dpdk.org?subject=help>
List-Subscribe: <https://mails.dpdk.org/listinfo/stable>,
 <mailto:stable-request@dpdk.org?subject=subscribe>
Errors-To: stable-bounces@dpdk.org
Sender: "stable" <stable-bounces@dpdk.org>

Hi,

FYI, your patch has been queued to stable release 19.11.3

Note it hasn't been pushed to http://dpdk.org/browse/dpdk-stable yet.
It will be pushed if I get no objections before 05/21/20. So please
shout if anyone has objections.

Also note that after the patch there's a diff of the upstream commit vs the
patch applied to the branch. This will indicate if there was any rebasing
needed to apply to the stable branch. If there were code changes for rebasing
(ie: not only metadata diffs), please double check that the rebase was
correctly done.

Thanks.

Luca Boccassi

---
>From 9a673fc54474800604960850eacf78e0ca35b286 Mon Sep 17 00:00:00 2001
From: Tao Zhu <taox.zhu@intel.com>
Date: Wed, 22 Apr 2020 12:37:36 +0000
Subject: [PATCH] net/ixgbe: fix resource leak after thread exits normally

[ upstream commit d68ab7a9f9aeb486bd17d5fa42bd5cc9f179693f ]

When the thread exits normally, pthread_join() is not called, which can
result in a resource leak. Therefore, the thread is set to separation
mode using function pthread_detach(), so that no program call
pthread_join() is required to recycle, and when the thread exits,
the system automatically reclaims resources.

Wait for the thread to finish with timeout argument(0 means that it will
not return until link complete), wait until the thread finishes before
returning. Normally, the thread will finish in a shorter time, and give
a warning message if it hasn't finished in a longer time.

Fixes: 819d0d1d57f1 ("net/ixgbe: fix blocking system events")

Signed-off-by: Tao Zhu <taox.zhu@intel.com>
Acked-by: Konstantin Ananyev <konstantin.ananyev@intel.com>
Reviewed-by: Xiaolong Ye <xiaolong.ye@intel.com>
---
 drivers/net/ixgbe/ixgbe_ethdev.c | 58 +++++++++++++++-----------------
 1 file changed, 28 insertions(+), 30 deletions(-)

diff --git a/drivers/net/ixgbe/ixgbe_ethdev.c b/drivers/net/ixgbe/ixgbe_ethdev.c
index 2cedd1a273..89d0e1c66a 100644
--- a/drivers/net/ixgbe/ixgbe_ethdev.c
+++ b/drivers/net/ixgbe/ixgbe_ethdev.c
@@ -230,7 +230,8 @@ static int ixgbe_dev_interrupt_action(struct rte_eth_dev *dev);
 static void ixgbe_dev_interrupt_handler(void *param);
 static void ixgbe_dev_interrupt_delayed_handler(void *param);
 static void *ixgbe_dev_setup_link_thread_handler(void *param);
-static void ixgbe_dev_cancel_link_thread(struct rte_eth_dev *dev);
+static int ixgbe_dev_wait_setup_link_complete(struct rte_eth_dev *dev,
+					      uint32_t timeout_ms);
 
 static int ixgbe_add_rar(struct rte_eth_dev *dev,
 			struct rte_ether_addr *mac_addr,
@@ -2598,7 +2599,7 @@ ixgbe_dev_start(struct rte_eth_dev *dev)
 	PMD_INIT_FUNC_TRACE();
 
 	/* Stop the link setup handler before resetting the HW. */
-	ixgbe_dev_cancel_link_thread(dev);
+	ixgbe_dev_wait_setup_link_complete(dev, 0);
 
 	/* disable uio/vfio intr/eventfd mapping */
 	rte_intr_disable(intr_handle);
@@ -2885,7 +2886,7 @@ ixgbe_dev_stop(struct rte_eth_dev *dev)
 
 	PMD_INIT_FUNC_TRACE();
 
-	ixgbe_dev_cancel_link_thread(dev);
+	ixgbe_dev_wait_setup_link_complete(dev, 0);
 
 	/* disable interrupts */
 	ixgbe_disable_intr(hw);
@@ -4140,36 +4141,32 @@ out:
 	return ret_val;
 }
 
-/* return 1: setup complete, return 0: setup not complete, and wait timeout*/
+/*
+ * If @timeout_ms was 0, it means that it will not return until link complete.
+ * It returns 1 on complete, return 0 on timeout.
+ */
 static int
-ixgbe_dev_wait_setup_link_complete(struct rte_eth_dev *dev)
+ixgbe_dev_wait_setup_link_complete(struct rte_eth_dev *dev, uint32_t timeout_ms)
 {
-#define DELAY_INTERVAL 100 /* 100ms */
-#define MAX_TIMEOUT    90 /* 9s (90 * 100ms) in total */
+#define WARNING_TIMEOUT    9000 /* 9s  in total */
 	struct ixgbe_adapter *ad = dev->data->dev_private;
-	int timeout = MAX_TIMEOUT;
+	uint32_t timeout = timeout_ms ? timeout_ms : WARNING_TIMEOUT;
 
-	while (rte_atomic32_read(&ad->link_thread_running) && timeout) {
-		msec_delay(DELAY_INTERVAL);
+	while (rte_atomic32_read(&ad->link_thread_running)) {
+		msec_delay(1);
 		timeout--;
-	}
-
-
-	return !!timeout;
-}
 
-static void
-ixgbe_dev_cancel_link_thread(struct rte_eth_dev *dev)
-{
-	struct ixgbe_adapter *ad = dev->data->dev_private;
-	void *retval;
-
-	if (!ixgbe_dev_wait_setup_link_complete(dev)) {
-		pthread_cancel(ad->link_thread_tid);
-		pthread_join(ad->link_thread_tid, &retval);
-		rte_atomic32_clear(&ad->link_thread_running);
-		PMD_DRV_LOG(ERR, "Link thread not complete, cancel it!");
+		if (timeout_ms) {
+			if (!timeout)
+				return 0;
+		} else if (!timeout) {
+			/* It will not return until link complete */
+			timeout = WARNING_TIMEOUT;
+			PMD_DRV_LOG(ERR, "IXGBE link thread not complete too long time!");
+		}
 	}
+
+	return 1;
 }
 
 static void *
@@ -4183,6 +4180,7 @@ ixgbe_dev_setup_link_thread_handler(void *param)
 	u32 speed;
 	bool autoneg = false;
 
+	pthread_detach(pthread_self());
 	speed = hw->phy.autoneg_advertised;
 	if (!speed)
 		ixgbe_get_link_capabilities(hw, &speed, &autoneg);
@@ -4278,8 +4276,8 @@ ixgbe_dev_link_update_share(struct rte_eth_dev *dev,
 	if (link_up == 0) {
 		if (ixgbe_get_media_type(hw) == ixgbe_media_type_fiber) {
 			intr->flags |= IXGBE_FLAG_NEED_LINK_CONFIG;
-			if (ixgbe_dev_wait_setup_link_complete(dev) &&
-			    rte_atomic32_test_and_set(&ad->link_thread_running)) {
+			ixgbe_dev_wait_setup_link_complete(dev, 0);
+			if (rte_atomic32_test_and_set(&ad->link_thread_running)) {
 				if (rte_ctrl_thread_create(&ad->link_thread_tid,
 					"ixgbe-link-handler",
 					NULL,
@@ -5319,7 +5317,7 @@ ixgbevf_dev_start(struct rte_eth_dev *dev)
 	PMD_INIT_FUNC_TRACE();
 
 	/* Stop the link setup handler before resetting the HW. */
-	ixgbe_dev_cancel_link_thread(dev);
+	ixgbe_dev_wait_setup_link_complete(dev, 0);
 
 	err = hw->mac.ops.reset_hw(hw);
 	if (err) {
@@ -5417,7 +5415,7 @@ ixgbevf_dev_stop(struct rte_eth_dev *dev)
 
 	PMD_INIT_FUNC_TRACE();
 
-	ixgbe_dev_cancel_link_thread(dev);
+	ixgbe_dev_wait_setup_link_complete(dev, 0);
 
 	ixgbevf_intr_disable(dev);
 
-- 
2.20.1

---
  Diff of the applied patch vs upstream commit (please double-check if non-empty:
---
--- -	2020-05-19 14:04:48.920788348 +0100
+++ 0109-net-ixgbe-fix-resource-leak-after-thread-exits-norma.patch	2020-05-19 14:04:44.360650983 +0100
@@ -1,8 +1,10 @@
-From d68ab7a9f9aeb486bd17d5fa42bd5cc9f179693f Mon Sep 17 00:00:00 2001
+From 9a673fc54474800604960850eacf78e0ca35b286 Mon Sep 17 00:00:00 2001
 From: Tao Zhu <taox.zhu@intel.com>
 Date: Wed, 22 Apr 2020 12:37:36 +0000
 Subject: [PATCH] net/ixgbe: fix resource leak after thread exits normally
 
+[ upstream commit d68ab7a9f9aeb486bd17d5fa42bd5cc9f179693f ]
+
 When the thread exits normally, pthread_join() is not called, which can
 result in a resource leak. Therefore, the thread is set to separation
 mode using function pthread_detach(), so that no program call
@@ -15,7 +17,6 @@
 a warning message if it hasn't finished in a longer time.
 
 Fixes: 819d0d1d57f1 ("net/ixgbe: fix blocking system events")
-Cc: stable@dpdk.org
 
 Signed-off-by: Tao Zhu <taox.zhu@intel.com>
 Acked-by: Konstantin Ananyev <konstantin.ananyev@intel.com>
@@ -25,7 +26,7 @@
  1 file changed, 28 insertions(+), 30 deletions(-)
 
 diff --git a/drivers/net/ixgbe/ixgbe_ethdev.c b/drivers/net/ixgbe/ixgbe_ethdev.c
-index 2a8543fd73..d26cf0ab9e 100644
+index 2cedd1a273..89d0e1c66a 100644
 --- a/drivers/net/ixgbe/ixgbe_ethdev.c
 +++ b/drivers/net/ixgbe/ixgbe_ethdev.c
 @@ -230,7 +230,8 @@ static int ixgbe_dev_interrupt_action(struct rte_eth_dev *dev);
@@ -38,7 +39,7 @@
  
  static int ixgbe_add_rar(struct rte_eth_dev *dev,
  			struct rte_ether_addr *mac_addr,
-@@ -2601,7 +2602,7 @@ ixgbe_dev_start(struct rte_eth_dev *dev)
+@@ -2598,7 +2599,7 @@ ixgbe_dev_start(struct rte_eth_dev *dev)
  	PMD_INIT_FUNC_TRACE();
  
  	/* Stop the link setup handler before resetting the HW. */
@@ -47,7 +48,7 @@
  
  	/* disable uio/vfio intr/eventfd mapping */
  	rte_intr_disable(intr_handle);
-@@ -2888,7 +2889,7 @@ ixgbe_dev_stop(struct rte_eth_dev *dev)
+@@ -2885,7 +2886,7 @@ ixgbe_dev_stop(struct rte_eth_dev *dev)
  
  	PMD_INIT_FUNC_TRACE();
  
@@ -56,7 +57,7 @@
  
  	/* disable interrupts */
  	ixgbe_disable_intr(hw);
-@@ -4143,36 +4144,32 @@ out:
+@@ -4140,36 +4141,32 @@ out:
  	return ret_val;
  }
  
@@ -112,7 +113,7 @@
  }
  
  static void *
-@@ -4186,6 +4183,7 @@ ixgbe_dev_setup_link_thread_handler(void *param)
+@@ -4183,6 +4180,7 @@ ixgbe_dev_setup_link_thread_handler(void *param)
  	u32 speed;
  	bool autoneg = false;
  
@@ -120,7 +121,7 @@
  	speed = hw->phy.autoneg_advertised;
  	if (!speed)
  		ixgbe_get_link_capabilities(hw, &speed, &autoneg);
-@@ -4282,8 +4280,8 @@ ixgbe_dev_link_update_share(struct rte_eth_dev *dev,
+@@ -4278,8 +4276,8 @@ ixgbe_dev_link_update_share(struct rte_eth_dev *dev,
  	if (link_up == 0) {
  		if (ixgbe_get_media_type(hw) == ixgbe_media_type_fiber) {
  			intr->flags |= IXGBE_FLAG_NEED_LINK_CONFIG;
@@ -131,7 +132,7 @@
  				if (rte_ctrl_thread_create(&ad->link_thread_tid,
  					"ixgbe-link-handler",
  					NULL,
-@@ -5323,7 +5321,7 @@ ixgbevf_dev_start(struct rte_eth_dev *dev)
+@@ -5319,7 +5317,7 @@ ixgbevf_dev_start(struct rte_eth_dev *dev)
  	PMD_INIT_FUNC_TRACE();
  
  	/* Stop the link setup handler before resetting the HW. */
@@ -140,7 +141,7 @@
  
  	err = hw->mac.ops.reset_hw(hw);
  	if (err) {
-@@ -5421,7 +5419,7 @@ ixgbevf_dev_stop(struct rte_eth_dev *dev)
+@@ -5417,7 +5415,7 @@ ixgbevf_dev_stop(struct rte_eth_dev *dev)
  
  	PMD_INIT_FUNC_TRACE();