Skip to contentSkip to conversation

net/ixgbe: fix resource leak after thread exits normally

PATCH Superseded [email protected] 3 participants 13 messages Apr 10, 2020 – Apr 22, 2020 1 message not in the archive Network drivers

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 net/ixgbe: fix resource leak after thread exits normally

From: "Zhu, Tao" <[email protected]>

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.

Fixes 819d0d1d57f1 ("net/ixgbe: fix blocking system events")
---
 drivers/net/ixgbe/ixgbe_ethdev.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/net/ixgbe/ixgbe_ethdev.c +1 -2
index 2c57976..f141ae4 100644
@@ -4165,11 +4165,9 @@ static int ixgbevf_dev_xstats_get_names(__rte_unused struct rte_eth_dev *dev,
 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!");
 	}
@@ -4186,6 +4184,7 @@ static int ixgbevf_dev_xstats_get_names(__rte_unused struct rte_eth_dev *dev,
 	u32 speed;
 	bool autoneg = false;
 
+	pthread_detach(pthread_self());
 	speed = hw->phy.autoneg_advertised;
 	if (!speed)
 		ixgbe_get_link_capabilities(hw, &speed, &autoneg);
Signature · 1 line
1.8.3.1
#1 lore raw

PATCH v2 1/1 net/ixgbe: fix resource leak after thread exits normally

From: Zhu Tao <[email protected]>

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.

Fixes 819d0d1d57f1 ("net/ixgbe: fix blocking system events")
---
 drivers/net/ixgbe/ixgbe_ethdev.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

v2 changes:
commit log.

drivers/net/ixgbe/ixgbe_ethdev.c +1 -2
index 2c57976..f141ae4 100644
@@ -4165,11 +4165,9 @@ static int ixgbevf_dev_xstats_get_names(__rte_unused struct rte_eth_dev *dev,
 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!");
 	}
@@ -4186,6 +4184,7 @@ static int ixgbevf_dev_xstats_get_names(__rte_unused struct rte_eth_dev *dev,
 	u32 speed;
 	bool autoneg = false;
 
+	pthread_detach(pthread_self());
 	speed = hw->phy.autoneg_advertised;
 	if (!speed)
 		ixgbe_get_link_capabilities(hw, &speed, &autoneg);
Signature · 1 line
1.8.3.1
#2 lore raw

Hi Tao,

Quoted 24 lines

From: Zhu Tao <[email protected]>

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.

Fixes 819d0d1d57f1 ("net/ixgbe: fix blocking system events")
---
 drivers/net/ixgbe/ixgbe_ethdev.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

v2 changes:
commit log.

drivers/net/ixgbe/ixgbe_ethdev.c +0 -0
index 2c57976..f141ae4 100644

drivers/net/ixgbe/ixgbe_ethdev.c static int ixgbevf_dev_xstats_get_names(__rte_unused struct rte_eth_dev *dev, · line 4172

Quoted 8 lines
drivers/net/ixgbe/ixgbe_ethdev.c +0 -2
@@ -4165,11 +4165,9 @@ static int ixgbevf_dev_xstats_get_names(__rte_unused struct rte_eth_dev *dev,
 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);

As you already waiting for link thread termination in ixgbe_dev_wait_setup_link_complete(), do you still need pthread_cancel() here?

Quoted 13 lines
drivers/net/ixgbe/ixgbe_ethdev.c +1 -1
 		rte_atomic32_clear(&ad->link_thread_running);
 		PMD_DRV_LOG(ERR, "Link thread not complete, cancel it!");
 	}
@@ -4186,6 +4184,7 @@ static int ixgbevf_dev_xstats_get_names(__rte_unused struct rte_eth_dev *dev,
 	u32 speed;
 	bool autoneg = false;
 
+	pthread_detach(pthread_self());
 	speed = hw->phy.autoneg_advertised;
 	if (!speed)
 		ixgbe_get_link_capabilities(hw, &speed, &autoneg);
--

1.8.3.1

A message that is not in the archive (sent off-list or not yet received): 60652C6914E08D41B9AA1580751B3CA9015ED8D2@SHSMSX103.ccr.corp.intel.com

#4 lore raw

Hi Tao,

Quoted 2 lines

Because ixgbe_dev_wait_setup_link_complete() doesn't guarantee that the thread will always complete properly. So after the wait timeout, need to call pthread_cancel() to cancel the thread.

But pthread_cancel() also doesn't guarantee immediate thread termination. You'll have to wait for link_thread_running again to make sure. Another thing - in theory if link_thread was already terminated, then its TID can be reused by other thread and we will terminate different thread (though chances are quite small).

Quoted 86 lines

Definition of function ixgbe_dev_wait_setup_link_complete() as below:

/* return 1: setup complete, return 0: setup not complete, and wait timeout*/ static int ixgbe_dev_wait_setup_link_complete(struct rte_eth_dev *dev) { #define DELAY_INTERVAL 100 /* 100ms */

#define MAX_TIMEOUT    90 /* 9s (90 * 100ms) in total */
        struct ixgbe_adapter *ad = dev->data->dev_private;
        int timeout = MAX_TIMEOUT;

        while (rte_atomic32_read(&ad->link_thread_running) && timeout) {
                msec_delay(DELAY_INTERVAL);
                timeout--;
        }


        return !!timeout;
}

BR,
Zhu, Tao

Quoted 65 lines

-----Original Message-----
From: Ananyev, Konstantin
Sent: Friday, April 10, 2020 8:10 PM
To: Zhu, TaoX <[email protected]>; Lu, Wenzhuo <[email protected]>; Ye, Xiaolong <[email protected]>
Cc: [email protected]; [email protected]; [email protected]
Subject: RE: [PATCH v2] net/ixgbe: fix resource leak after thread exits normally

Hi Tao,

Quoted 12 lines

From: Zhu Tao <[email protected]>

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.

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

Review

Quoted 8 lines
---
 drivers/net/ixgbe/ixgbe_ethdev.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

v2 changes:
commit log.

(quoted diff) +0 -0

b/drivers/net/ixgbe/ixgbe_ethdev.c

drivers/net/ixgbe/ixgbe_ethdev.c static int

Quoted 4 lines

index 2c57976..f141ae4 100644

drivers/net/ixgbe/ixgbe_ethdev.c +0 -0
@@ -4165,11 +4165,9 @@ static int

ixgbevf_dev_xstats_get_names(__rte_unused struct rte_eth_dev *dev,

drivers/net/ixgbe/ixgbe_ethdev.c static int ixgbevf_dev_xstats_get_names(__rte_unused struct rte_eth_dev *dev, · line 4172

Quoted 8 lines
drivers/net/ixgbe/ixgbe_ethdev.c +0 -2
 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);

As you already waiting for link thread termination in ixgbe_dev_wait_setup_link_complete(), do you still need pthread_cancel() here?

drivers/net/ixgbe/ixgbe_ethdev.c static int

Quoted 4 lines
drivers/net/ixgbe/ixgbe_ethdev.c +0 -0
 rte_atomic32_clear(&ad->link_thread_running);
 PMD_DRV_LOG(ERR, "Link thread not complete, cancel it!");
 }
@@ -4186,6 +4184,7 @@ static int

ixgbevf_dev_xstats_get_names(__rte_unused struct rte_eth_dev *dev,

Quoted 9 lines
drivers/net/ixgbe/ixgbe_ethdev.c +1 -1
 u32 speed;
 bool autoneg = false;
 
+pthread_detach(pthread_self());
 speed = hw->phy.autoneg_advertised;
 if (!speed)
 ixgbe_get_link_capabilities(hw, &speed, &autoneg);
--

1.8.3.1

#5 lore raw
Quoted 4 lines

Hi Tao,

Quoted 13 lines
Quoted 2 lines

Because ixgbe_dev_wait_setup_link_complete() doesn't guarantee that the thread will always complete properly. So after the wait timeout, need to call pthread_cancel() to cancel the thread.

But pthread_cancel() also doesn't guarantee immediate thread termination. You'll have to wait for link_thread_running again to make sure. Another thing - in theory if link_thread was already terminated, then its TID can be reused by other thread and we will terminate different thread (though chances are quite small).

Quoted 4 lines

Definition of function ixgbe_dev_wait_setup_link_complete() as below:

/* return 1: setup complete, return 0: setup not complete, and wait timeout*/ static int ixgbe_dev_wait_setup_link_complete(struct rte_eth_dev *dev) { #define DELAY_INTERVAL 100 /* 100ms */

As another thought - instead of unconditionally defining timeout inside the function itself, can we have it a s function parameter? Can caller can decide for how long he is ok to wait.

Quoted 82 lines
Quoted 82 lines
#define MAX_TIMEOUT    90 /* 9s (90 * 100ms) in total */
        struct ixgbe_adapter *ad = dev->data->dev_private;
        int timeout = MAX_TIMEOUT;

        while (rte_atomic32_read(&ad->link_thread_running) && timeout) {
                msec_delay(DELAY_INTERVAL);
                timeout--;
        }


        return !!timeout;
}

BR,
Zhu, Tao

Quoted 65 lines

-----Original Message-----
From: Ananyev, Konstantin
Sent: Friday, April 10, 2020 8:10 PM
To: Zhu, TaoX <[email protected]>; Lu, Wenzhuo <[email protected]>; Ye, Xiaolong <[email protected]>
Cc: [email protected]; [email protected]; [email protected]
Subject: RE: [PATCH v2] net/ixgbe: fix resource leak after thread exits normally

Hi Tao,

Quoted 12 lines

From: Zhu Tao <[email protected]>

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.

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

Review

Quoted 8 lines
---
 drivers/net/ixgbe/ixgbe_ethdev.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

v2 changes:
commit log.

(quoted diff) +0 -0

b/drivers/net/ixgbe/ixgbe_ethdev.c

drivers/net/ixgbe/ixgbe_ethdev.c static int

Quoted 4 lines

index 2c57976..f141ae4 100644

drivers/net/ixgbe/ixgbe_ethdev.c +0 -0
@@ -4165,11 +4165,9 @@ static int

ixgbevf_dev_xstats_get_names(__rte_unused struct rte_eth_dev *dev,

drivers/net/ixgbe/ixgbe_ethdev.c static int ixgbevf_dev_xstats_get_names(__rte_unused struct rte_eth_dev *dev, · line 4172

Quoted 8 lines
drivers/net/ixgbe/ixgbe_ethdev.c +0 -2
 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);

As you already waiting for link thread termination in ixgbe_dev_wait_setup_link_complete(), do you still need pthread_cancel() here?

drivers/net/ixgbe/ixgbe_ethdev.c static int

Quoted 4 lines
drivers/net/ixgbe/ixgbe_ethdev.c +0 -0
 rte_atomic32_clear(&ad->link_thread_running);
 PMD_DRV_LOG(ERR, "Link thread not complete, cancel it!");
 }
@@ -4186,6 +4184,7 @@ static int

ixgbevf_dev_xstats_get_names(__rte_unused struct rte_eth_dev *dev,

Quoted 9 lines
drivers/net/ixgbe/ixgbe_ethdev.c +1 -1
 u32 speed;
 bool autoneg = false;
 
+pthread_detach(pthread_self());
 speed = hw->phy.autoneg_advertised;
 if (!speed)
 ixgbe_get_link_capabilities(hw, &speed, &autoneg);
--

1.8.3.1

#6 lore raw

PATCH v3 1/1 net/ixgbe: fix resource leak after thread exits normally

In reply to [email protected]

From: Zhu Tao <[email protected]>

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 without setting the timeout, 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")
---
 drivers/net/ixgbe/ixgbe_ethdev.c | 46 +++++++++++++++-------------------------
 1 file changed, 17 insertions(+), 29 deletions(-)

v3 changes:
1. Wait for the thread to finish without setting the timeout, and the corresponding function name has also been modified.
2. Commit log.

v2 changes:
Commit log.

drivers/net/ixgbe/ixgbe_ethdev.c +17 -29
index 2c57976..dac20a3 100644
@@ -230,7 +230,7 @@ static int ixgbe_dev_rss_reta_query(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 void ixgbe_dev_wait_setup_link_complete(struct rte_eth_dev *dev);
 
 static int ixgbe_add_rar(struct rte_eth_dev *dev,
 			struct rte_ether_addr *mac_addr,
@@ -2601,7 +2601,7 @@ static int eth_ixgbevf_pci_remove(struct rte_pci_device *pci_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);
 
 	/* disable uio/vfio intr/eventfd mapping */
 	rte_intr_disable(intr_handle);
@@ -2888,7 +2888,7 @@ static int eth_ixgbevf_pci_remove(struct rte_pci_device *pci_dev)
 
 	PMD_INIT_FUNC_TRACE();
 
-	ixgbe_dev_cancel_link_thread(dev);
+	ixgbe_dev_wait_setup_link_complete(dev);
 
 	/* disable interrupts */
 	ixgbe_disable_intr(hw);
@@ -4143,35 +4143,22 @@ static int ixgbevf_dev_xstats_get_names(__rte_unused struct rte_eth_dev *dev,
 	return ret_val;
 }
 
-/* return 1: setup complete, return 0: setup not complete, and wait timeout*/
-static int
+static void
 ixgbe_dev_wait_setup_link_complete(struct rte_eth_dev *dev)
 {
-#define DELAY_INTERVAL 100 /* 100ms */
-#define MAX_TIMEOUT    90 /* 9s (90 * 100ms) in total */
+#define DELAY_INTERVAL 10 /* 10ms */
+#define WARNING_TIMEOUT    900 /* 9s (900 * 10ms) in total */
 	struct ixgbe_adapter *ad = dev->data->dev_private;
-	int timeout = MAX_TIMEOUT;
+	int timeout = WARNING_TIMEOUT;
 
-	while (rte_atomic32_read(&ad->link_thread_running) && timeout) {
+	while (rte_atomic32_read(&ad->link_thread_running)) {
 		msec_delay(DELAY_INTERVAL);
 		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) {
+			timeout = WARNING_TIMEOUT;
+			PMD_DRV_LOG(ERR, "IXGBE link thread not complete too long time!");
+		}
 	}
 }
 
@@ -4186,6 +4173,7 @@ static int ixgbevf_dev_xstats_get_names(__rte_unused struct rte_eth_dev *dev,
 	u32 speed;
 	bool autoneg = false;
 
+	pthread_detach(pthread_self());
 	speed = hw->phy.autoneg_advertised;
 	if (!speed)
 		ixgbe_get_link_capabilities(hw, &speed, &autoneg);
@@ -4282,8 +4270,8 @@ static int ixgbevf_dev_xstats_get_names(__rte_unused 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);
+			if (rte_atomic32_test_and_set(&ad->link_thread_running)) {
 				if (rte_ctrl_thread_create(&ad->link_thread_tid,
 					"ixgbe-link-handler",
 					NULL,
@@ -5323,7 +5311,7 @@ static int ixgbevf_dev_xstats_get_names(__rte_unused 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);
 
 	err = hw->mac.ops.reset_hw(hw);
 	if (err) {
@@ -5421,7 +5409,7 @@ static int ixgbevf_dev_xstats_get_names(__rte_unused struct rte_eth_dev *dev,
 
 	PMD_INIT_FUNC_TRACE();
 
-	ixgbe_dev_cancel_link_thread(dev);
+	ixgbe_dev_wait_setup_link_complete(dev);
 
 	ixgbevf_intr_disable(dev);
 
Signature · 1 line
1.8.3.1
#7 lore raw

PATCH v4 1/1 net/ixgbe: fix resource leak after thread exits normally

In reply to [email protected]

From: Zhu Tao <[email protected]>

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 without setting the timeout, 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")
---
 drivers/net/ixgbe/ixgbe_ethdev.c | 46 +++++++++++++++-------------------------
 1 file changed, 17 insertions(+), 29 deletions(-)

v4 changes:
Format codes.

v3 changes:
1. Wait for the thread to finish without setting the timeout, and the corresponding function name has also been modified.
2. Commit log.

v2 changes:
Commit log.

drivers/net/ixgbe/ixgbe_ethdev.c +17 -29
index 2c57976..9ad93c5 100644
@@ -230,7 +230,7 @@ static int ixgbe_dev_rss_reta_query(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 void ixgbe_dev_wait_setup_link_complete(struct rte_eth_dev *dev);
 
 static int ixgbe_add_rar(struct rte_eth_dev *dev,
 			struct rte_ether_addr *mac_addr,
@@ -2601,7 +2601,7 @@ static int eth_ixgbevf_pci_remove(struct rte_pci_device *pci_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);
 
 	/* disable uio/vfio intr/eventfd mapping */
 	rte_intr_disable(intr_handle);
@@ -2888,7 +2888,7 @@ static int eth_ixgbevf_pci_remove(struct rte_pci_device *pci_dev)
 
 	PMD_INIT_FUNC_TRACE();
 
-	ixgbe_dev_cancel_link_thread(dev);
+	ixgbe_dev_wait_setup_link_complete(dev);
 
 	/* disable interrupts */
 	ixgbe_disable_intr(hw);
@@ -4143,35 +4143,22 @@ static int ixgbevf_dev_xstats_get_names(__rte_unused struct rte_eth_dev *dev,
 	return ret_val;
 }
 
-/* return 1: setup complete, return 0: setup not complete, and wait timeout*/
-static int
+static void
 ixgbe_dev_wait_setup_link_complete(struct rte_eth_dev *dev)
 {
-#define DELAY_INTERVAL 100 /* 100ms */
-#define MAX_TIMEOUT    90 /* 9s (90 * 100ms) in total */
+#define DELAY_INTERVAL 10 /* 10ms */
+#define WARNING_TIMEOUT    900 /* 9s (900 * 10ms) in total */
 	struct ixgbe_adapter *ad = dev->data->dev_private;
-	int timeout = MAX_TIMEOUT;
+	int timeout = WARNING_TIMEOUT;
 
-	while (rte_atomic32_read(&ad->link_thread_running) && timeout) {
+	while (rte_atomic32_read(&ad->link_thread_running)) {
 		msec_delay(DELAY_INTERVAL);
 		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) {
+			timeout = WARNING_TIMEOUT;
+			PMD_DRV_LOG(ERR, "IXGBE link thread not complete too long time!");
+		}
 	}
 }
 
@@ -4186,6 +4173,7 @@ static int ixgbevf_dev_xstats_get_names(__rte_unused struct rte_eth_dev *dev,
 	u32 speed;
 	bool autoneg = false;
 
+	pthread_detach(pthread_self());
 	speed = hw->phy.autoneg_advertised;
 	if (!speed)
 		ixgbe_get_link_capabilities(hw, &speed, &autoneg);
@@ -4282,8 +4270,8 @@ static int ixgbevf_dev_xstats_get_names(__rte_unused 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);
+			if (rte_atomic32_test_and_set(&ad->link_thread_running)) {
 				if (rte_ctrl_thread_create(&ad->link_thread_tid,
 					"ixgbe-link-handler",
 					NULL,
@@ -5323,7 +5311,7 @@ static int ixgbevf_dev_xstats_get_names(__rte_unused 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);
 
 	err = hw->mac.ops.reset_hw(hw);
 	if (err) {
@@ -5421,7 +5409,7 @@ static int ixgbevf_dev_xstats_get_names(__rte_unused struct rte_eth_dev *dev,
 
 	PMD_INIT_FUNC_TRACE();
 
-	ixgbe_dev_cancel_link_thread(dev);
+	ixgbe_dev_wait_setup_link_complete(dev);
 
 	ixgbevf_intr_disable(dev);
 
Signature · 1 line
1.8.3.1
#8 lore raw
Quoted 93 lines · On 04/15, [email protected] wrote:

From: Zhu Tao <[email protected]>

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 without setting the timeout, 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")

---
drivers/net/ixgbe/ixgbe_ethdev.c | 46 +++++++++++++++-------------------------

1 file changed, 17 insertions(+), 29 deletions(-)

v4 changes:
Format codes.

v3 changes:
1. Wait for the thread to finish without setting the timeout, and the corresponding function name has also been modified.
2. Commit log.

v2 changes:
Commit log.

drivers/net/ixgbe/ixgbe_ethdev.c +8 -18
index 2c57976..9ad93c5 100644
@@ -230,7 +230,7 @@ static int ixgbe_dev_rss_reta_query(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 void ixgbe_dev_wait_setup_link_complete(struct rte_eth_dev *dev);
 
 static int ixgbe_add_rar(struct rte_eth_dev *dev,
 			struct rte_ether_addr *mac_addr,
@@ -2601,7 +2601,7 @@ static int eth_ixgbevf_pci_remove(struct rte_pci_device *pci_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);
 
 	/* disable uio/vfio intr/eventfd mapping */
 	rte_intr_disable(intr_handle);
@@ -2888,7 +2888,7 @@ static int eth_ixgbevf_pci_remove(struct rte_pci_device *pci_dev)
 
 	PMD_INIT_FUNC_TRACE();
 
-	ixgbe_dev_cancel_link_thread(dev);
+	ixgbe_dev_wait_setup_link_complete(dev);
 
 	/* disable interrupts */
 	ixgbe_disable_intr(hw);
@@ -4143,35 +4143,22 @@ static int ixgbevf_dev_xstats_get_names(__rte_unused struct rte_eth_dev *dev,
 	return ret_val;
 }
 
-/* return 1: setup complete, return 0: setup not complete, and wait timeout*/
-static int
+static void
 ixgbe_dev_wait_setup_link_complete(struct rte_eth_dev *dev)
 {
-#define DELAY_INTERVAL 100 /* 100ms */
-#define MAX_TIMEOUT    90 /* 9s (90 * 100ms) in total */
+#define DELAY_INTERVAL 10 /* 10ms */
+#define WARNING_TIMEOUT    900 /* 9s (900 * 10ms) in total */
 	struct ixgbe_adapter *ad = dev->data->dev_private;
-	int timeout = MAX_TIMEOUT;
+	int timeout = WARNING_TIMEOUT;
 
-	while (rte_atomic32_read(&ad->link_thread_running) && timeout) {
+	while (rte_atomic32_read(&ad->link_thread_running)) {
 		msec_delay(DELAY_INTERVAL);
 		timeout--;
-	}
-
-
-	return !!timeout;
-}
 
-static void
-ixgbe_dev_cancel_link_thread(struct rte_eth_dev *dev)
-{
-	struct ixgbe_adapter *ad = dev->data->dev_private;

drivers/net/ixgbe/ixgbe_ethdev.c static int ixgbevf_dev_xstats_get_names(__rte_unused struct rte_eth_dev *dev, · line 4160

Quoted 10 lines
drivers/net/ixgbe/ixgbe_ethdev.c +3 -7
-	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) {
+			timeout = WARNING_TIMEOUT;
+			PMD_DRV_LOG(ERR, "IXGBE link thread not complete too long time!");

In theory, is there any possibility that the while loop will never end? And as Konstantin suggested, I think it makes sense to provide a parameter for caller to decide how long they want to wait.

Thanks,
Xiaolong

Quoted 4 lines
drivers/net/ixgbe/ixgbe_ethdev.c +1 -0
+		}
 	}
 }
 
#9 lore raw

PATCH v5 1/1 net/ixgbe: fix resource leak after thread exits normally

In reply to [email protected]

From: Zhu Tao <[email protected]>

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")
---
 drivers/net/ixgbe/ixgbe_ethdev.c | 57 +++++++++++++++++++---------------------
 1 file changed, 27 insertions(+), 30 deletions(-)

V5 changes:
Modify the definition of function ixgbe_dev_wait_setup_link_complete(),
so that the caller can choose whether to block, etc.

v4 changes:
Format codes.

v3 changes:
1. Wait for the thread to finish without setting the timeout, and the corresponding function name has also been modified.
2. Commit log.

v2 changes:
Commit log.

drivers/net/ixgbe/ixgbe_ethdev.c +27 -30
index 2c57976..92788ee 100644
@@ -230,7 +230,7 @@ static int ixgbe_dev_rss_reta_query(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,
@@ -2601,7 +2601,7 @@ static int eth_ixgbevf_pci_remove(struct rte_pci_device *pci_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);
@@ -2888,7 +2888,7 @@ static int eth_ixgbevf_pci_remove(struct rte_pci_device *pci_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);
@@ -4143,36 +4143,32 @@ static int ixgbevf_dev_xstats_get_names(__rte_unused struct rte_eth_dev *dev,
 	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;
+	int timeout = timeout_ms ? WARNING_TIMEOUT : timeout_ms;
 
-	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 *
@@ -4186,6 +4182,7 @@ static int ixgbevf_dev_xstats_get_names(__rte_unused struct rte_eth_dev *dev,
 	u32 speed;
 	bool autoneg = false;
 
+	pthread_detach(pthread_self());
 	speed = hw->phy.autoneg_advertised;
 	if (!speed)
 		ixgbe_get_link_capabilities(hw, &speed, &autoneg);
@@ -4282,8 +4279,8 @@ static int ixgbevf_dev_xstats_get_names(__rte_unused 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,
@@ -5323,7 +5320,7 @@ static int ixgbevf_dev_xstats_get_names(__rte_unused 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) {
@@ -5421,7 +5418,7 @@ static int ixgbevf_dev_xstats_get_names(__rte_unused 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);
 
Signature · 1 line
1.8.3.1
Acked-by on v5 1/1
#10 lore raw
Quoted 13 lines

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.

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

---

Quoted 2 lines

--
1.8.3.1

#11 lore raw
In reply to [email protected]
Quoted 84 lines · On 04/21, [email protected] wrote:

From: Zhu Tao <[email protected]>

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")

---
drivers/net/ixgbe/ixgbe_ethdev.c | 57 +++++++++++++++++++---------------------

1 file changed, 27 insertions(+), 30 deletions(-)

V5 changes:
Modify the definition of function ixgbe_dev_wait_setup_link_complete(),
so that the caller can choose whether to block, etc.

v4 changes:
Format codes.

v3 changes:
1. Wait for the thread to finish without setting the timeout, and the corresponding function name has also been modified.
2. Commit log.

v2 changes:
Commit log.

drivers/net/ixgbe/ixgbe_ethdev.c +7 -4
index 2c57976..92788ee 100644
@@ -230,7 +230,7 @@ static int ixgbe_dev_rss_reta_query(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,
@@ -2601,7 +2601,7 @@ static int eth_ixgbevf_pci_remove(struct rte_pci_device *pci_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);
@@ -2888,7 +2888,7 @@ static int eth_ixgbevf_pci_remove(struct rte_pci_device *pci_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);
@@ -4143,36 +4143,32 @@ static int ixgbevf_dev_xstats_get_names(__rte_unused struct rte_eth_dev *dev,
 	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

drivers/net/ixgbe/ixgbe_ethdev.c +1 -1
-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)

{

drivers/net/ixgbe/ixgbe_ethdev.c +1 -2
-#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;

drivers/net/ixgbe/ixgbe_ethdev.c static int ixgbevf_dev_xstats_get_names(__rte_unused struct rte_eth_dev *dev, · line 4155

Quoted 2 lines
drivers/net/ixgbe/ixgbe_ethdev.c +1 -1
-	int timeout = MAX_TIMEOUT;
+	int timeout = timeout_ms ? WARNING_TIMEOUT : timeout_ms;

Sees timeout_ms is 0, set timeout as 0? I guess what you intended to do is:

	int timeout = timeout_ms ? timeout_ms : WARNING_TIMEOUT;

Thanks,
Xiaolong

Quoted 77 lines
drivers/net/ixgbe/ixgbe_ethdev.c +2 -2
-	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--;
drivers/net/ixgbe/ixgbe_ethdev.c +8 -16
-	}
-
-
-	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!");
+		}
	}
drivers/net/ixgbe/ixgbe_ethdev.c +2 -0
+
+	return 1;

}

static void *

drivers/net/ixgbe/ixgbe_ethdev.c +5 -4
@@ -4186,6 +4182,7 @@ static int ixgbevf_dev_xstats_get_names(__rte_unused struct rte_eth_dev *dev,
 	u32 speed;
 	bool autoneg = false;
 
+	pthread_detach(pthread_self());
 	speed = hw->phy.autoneg_advertised;
 	if (!speed)
 		ixgbe_get_link_capabilities(hw, &speed, &autoneg);
@@ -4282,8 +4279,8 @@ static int ixgbevf_dev_xstats_get_names(__rte_unused 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,
@@ -5323,7 +5320,7 @@ static int ixgbevf_dev_xstats_get_names(__rte_unused 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) {
@@ -5421,7 +5418,7 @@ static int ixgbevf_dev_xstats_get_names(__rte_unused 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);
 
Signature · 1 line
1.8.3.1
#12 lore raw

PATCH v6 1/1 net/ixgbe: fix resource leak after thread exits normally

In reply to [email protected]

From: Zhu Tao <[email protected]>

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")
---
 drivers/net/ixgbe/ixgbe_ethdev.c | 57 +++++++++++++++++++---------------------
 1 file changed, 27 insertions(+), 30 deletions(-)

V6 changes:
Change code segment
'int timeout = timeout_ms ? WARNING_TIMEOUT : timeout_ms;'
to
'uint32_t timeout = timeout_ms ? timeout_ms : WARNING_TIMEOUT;'.

V5 changes:
Modify the definition of function ixgbe_dev_wait_setup_link_complete(),
so that the caller can choose whether to block, etc.

v4 changes:
Format codes.

v3 changes:
1. Wait for the thread to finish without setting the timeout, and the corresponding function name has also been modified.
2. Commit log.

v2 changes:
Commit log.

drivers/net/ixgbe/ixgbe_ethdev.c +27 -30
index 2c57976..a3ae4d7 100644
@@ -230,7 +230,7 @@ static int ixgbe_dev_rss_reta_query(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,
@@ -2601,7 +2601,7 @@ static int eth_ixgbevf_pci_remove(struct rte_pci_device *pci_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);
@@ -2888,7 +2888,7 @@ static int eth_ixgbevf_pci_remove(struct rte_pci_device *pci_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);
@@ -4143,36 +4143,32 @@ static int ixgbevf_dev_xstats_get_names(__rte_unused struct rte_eth_dev *dev,
 	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 *
@@ -4186,6 +4182,7 @@ static int ixgbevf_dev_xstats_get_names(__rte_unused struct rte_eth_dev *dev,
 	u32 speed;
 	bool autoneg = false;
 
+	pthread_detach(pthread_self());
 	speed = hw->phy.autoneg_advertised;
 	if (!speed)
 		ixgbe_get_link_capabilities(hw, &speed, &autoneg);
@@ -4282,8 +4279,8 @@ static int ixgbevf_dev_xstats_get_names(__rte_unused 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,
@@ -5323,7 +5320,7 @@ static int ixgbevf_dev_xstats_get_names(__rte_unused 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) {
@@ -5421,7 +5418,7 @@ static int ixgbevf_dev_xstats_get_names(__rte_unused 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);
 
Signature · 1 line
1.8.3.1
Reviewed-by on v6 1/1 Applied
#13 lore raw
Quoted 170 lines · On 04/22, [email protected] wrote:

From: Zhu Tao <[email protected]>

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")

---
drivers/net/ixgbe/ixgbe_ethdev.c | 57 +++++++++++++++++++---------------------

1 file changed, 27 insertions(+), 30 deletions(-)

V6 changes:
Change code segment
'int timeout = timeout_ms ? WARNING_TIMEOUT : timeout_ms;'
to
'uint32_t timeout = timeout_ms ? timeout_ms : WARNING_TIMEOUT;'.

V5 changes:
Modify the definition of function ixgbe_dev_wait_setup_link_complete(),
so that the caller can choose whether to block, etc.

v4 changes:
Format codes.

v3 changes:
1. Wait for the thread to finish without setting the timeout, and the corresponding function name has also been modified.
2. Commit log.

v2 changes:
Commit log.

drivers/net/ixgbe/ixgbe_ethdev.c +7 -4
index 2c57976..a3ae4d7 100644
@@ -230,7 +230,7 @@ static int ixgbe_dev_rss_reta_query(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,
@@ -2601,7 +2601,7 @@ static int eth_ixgbevf_pci_remove(struct rte_pci_device *pci_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);
@@ -2888,7 +2888,7 @@ static int eth_ixgbevf_pci_remove(struct rte_pci_device *pci_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);
@@ -4143,36 +4143,32 @@ static int ixgbevf_dev_xstats_get_names(__rte_unused struct rte_eth_dev *dev,
 	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

drivers/net/ixgbe/ixgbe_ethdev.c +1 -1
-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)

{

drivers/net/ixgbe/ixgbe_ethdev.c +1 -2
-#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;
drivers/net/ixgbe/ixgbe_ethdev.c +3 -3
-	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--;
drivers/net/ixgbe/ixgbe_ethdev.c +8 -16
-	}
-
-
-	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!");
+		}
	}
drivers/net/ixgbe/ixgbe_ethdev.c +2 -0
+
+	return 1;

}

static void *

drivers/net/ixgbe/ixgbe_ethdev.c +5 -4
@@ -4186,6 +4182,7 @@ static int ixgbevf_dev_xstats_get_names(__rte_unused struct rte_eth_dev *dev,
 	u32 speed;
 	bool autoneg = false;
 
+	pthread_detach(pthread_self());
 	speed = hw->phy.autoneg_advertised;
 	if (!speed)
 		ixgbe_get_link_capabilities(hw, &speed, &autoneg);
@@ -4282,8 +4279,8 @@ static int ixgbevf_dev_xstats_get_names(__rte_unused 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,
@@ -5323,7 +5320,7 @@ static int ixgbevf_dev_xstats_get_names(__rte_unused 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) {
@@ -5421,7 +5418,7 @@ static int ixgbevf_dev_xstats_get_names(__rte_unused 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);
 
Signature · 1 line
1.8.3.1

Applied to dpdk-next-net-intel, Thanks.