Skip to contentSkip to conversation

get rid of pthread_cancel

RFC In review Stephen Hemminger 4 participants 11 messages Sep 24, 2025 – Sep 28, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover get rid of pthread_cancel

The use of pthread_cancel in DPDK is problematic since cancel is implemented as signal in Linux and TerminateThread in Windows. Using pthread_cancel also exposes the internals of rte_thread_t to drivers which limits potential ABI changes.

This patchset shows how the same effect can be had in most places by using either existing flag variables or the semantics of sockets/pipes used to communicate with the thread.

This RFC because it doesn't cover all uses of pthread_cancel, in the final version pthread_cancel will be gone and flagged as error in checkpatch; and the patches hit multiple drivers which require special hardware.

Stephen Hemminger (6):
eal: avoid using pthread_cancel
eventdev: avoid use of pthread_cancel
raw/ifpga: avoid use of pthread_cancel
dma/skeleton: avoid use of pthread_cancel
intel/ipn3ke: avoid use of pthread_cancel
intel/iavf: remove use of pthread_cancel

 drivers/dma/skeleton/skeleton_dmadev.c        |  5 +---
 drivers/net/intel/iavf/iavf_vchnl.c           |  8 +++----
 drivers/net/intel/ipn3ke/ipn3ke_representor.c |  8 ++-----
 drivers/raw/ifpga/ifpga_rawdev.c              |  8 +------
 lib/eal/common/eal_common_proc.c              | 24 +++++--------------
 lib/eventdev/rte_event_eth_rx_adapter.c       | 21 ++++++++--------
 6 files changed, 25 insertions(+), 49 deletions(-)
Signature · 1 line
2.47.3
#1 lore raw

RFC 1/6 eal: avoid using pthread_cancel

The multi-process handling thread can be managed more safely by closing the underlying socket rather than pthread_cancel. Closing the socket in the main thread will cause the read() in the MP handler to unblock.

The use of atomic for updating the socket fd is not needed. The fd for the socket is set before the thread is created and not modified again until handler has exited.

---
 lib/eal/common/eal_common_proc.c | 24 ++++++------------------
 1 file changed, 6 insertions(+), 18 deletions(-)
lib/eal/common/eal_common_proc.c +6 -18
index 0dea787e38..8e60e815ed 100644
@@ -34,7 +34,7 @@
 #include "eal_filesystem.h"
 #include "eal_internal_cfg.h"
 
-static RTE_ATOMIC(int) mp_fd = -1;
+static int mp_fd = -1;
 static rte_thread_t mp_handle_tid;
 static char mp_filter[PATH_MAX];   /* Filter for secondary process sockets */
 static char mp_dir_path[PATH_MAX]; /* The directory path for all mp sockets */
@@ -406,17 +406,9 @@ mp_handle(void *arg __rte_unused)
 {
 	struct mp_msg_internal msg;
 	struct sockaddr_un sa;
-	int fd;
-
-	while ((fd = rte_atomic_load_explicit(&mp_fd, rte_memory_order_relaxed)) >= 0) {
-		int ret;
-
-		ret = read_msg(fd, &msg, &sa);
-		if (ret <= 0)
-			break;
 
+	while (read_msg(mp_fd, &msg, &sa) > 0)
 		process_msg(&msg, &sa);
-	}
 
 	return 0;
 }
@@ -656,7 +648,7 @@ rte_mp_channel_init(void)
 		EAL_LOG(ERR, "failed to create mp thread: %s",
 			strerror(errno));
 		close(dir_fd);
-		close(rte_atomic_exchange_explicit(&mp_fd, -1, rte_memory_order_relaxed));
+		close(mp_fd);
 		return -1;
 	}
 
@@ -670,15 +662,11 @@ rte_mp_channel_init(void)
 void
 rte_mp_channel_cleanup(void)
 {
-	int fd;
-
-	fd = rte_atomic_exchange_explicit(&mp_fd, -1, rte_memory_order_relaxed);
-	if (fd < 0)
-		return;
+	/* shutdown() will cause recvmsg to unblock */
+	shutdown(mp_fd, SHUT_RDWR);
 
-	pthread_cancel((pthread_t)mp_handle_tid.opaque_id);
 	rte_thread_join(mp_handle_tid, NULL);
-	close_socket_fd(fd);
+	close_socket_fd(mp_fd);
 }
 
 /**
Signature · 1 line
2.47.3
#2 lore raw

RFC 2/6 eventdev: avoid use of pthread_cancel

In reply to Stephen Hemminger

To control the rx event thread is simpler and safer to just close the file descriptor rather than using pthread_cancel().

---
 lib/eventdev/rte_event_eth_rx_adapter.c | 21 +++++++++++----------
 1 file changed, 11 insertions(+), 10 deletions(-)
lib/eventdev/rte_event_eth_rx_adapter.c +11 -10
index 994f256322..95ceb35c5a 100644
@@ -1173,9 +1173,11 @@ rxa_intr_thread(void *arg)
 	while (1) {
 		n = rte_epoll_wait(rx_adapter->epd, epoll_events,
 				RTE_EVENT_ETH_INTR_RING_SIZE, -1);
-		if (unlikely(n < 0))
-			RTE_EDEV_LOG_ERR("rte_epoll_wait returned error %d",
-					n);
+		if (unlikely(n < 0)) {
+			RTE_EDEV_LOG_ERR("rte_epoll_wait returned error %d", n);
+			break;
+		}
+
 		for (i = 0; i < n; i++) {
 			rxa_intr_ring_enqueue(rx_adapter,
 					epoll_events[i].epdata.data);
@@ -1643,10 +1645,12 @@ rxa_destroy_intr_thread(struct event_eth_rx_adapter *rx_adapter)
 {
 	int err;
 
-	err = pthread_cancel((pthread_t)rx_adapter->rx_intr_thread.opaque_id);
-	if (err)
-		RTE_EDEV_LOG_ERR("Can't cancel interrupt thread err = %d",
-				err);
+	/*
+	 * close the epoll fd used in the interrupt thread
+	 * this will unblock the rte_epoll_wait().
+	 */
+	close(rx_adapter->epd);
+	rx_adapter->epd = INIT_FD;
 
 	err = rte_thread_join(rx_adapter->rx_intr_thread, NULL);
 	if (err)
@@ -1671,9 +1675,6 @@ rxa_free_intr_resources(struct event_eth_rx_adapter *rx_adapter)
 	if (ret)
 		return ret;
 
-	close(rx_adapter->epd);
-	rx_adapter->epd = INIT_FD;
-
 	return ret;
 }
 
Signature · 1 line
2.47.3
#3 lore raw

RFC 3/6 raw/ifpga: avoid use of pthread_cancel

In reply to Stephen Hemminger

Use refcnt to do controlled shutdown of control thread rather than using pthread_cancel().

---
 drivers/raw/ifpga/ifpga_rawdev.c | 8 +-------
 1 file changed, 1 insertion(+), 7 deletions(-)
drivers/raw/ifpga/ifpga_rawdev.c +1 -7
index 5b9b596435..d61927f193 100644
@@ -573,13 +573,7 @@ ifpga_monitor_stop_func(struct ifpga_rawdev *dev)
 
 	dev->poll_enabled = 0;
 
-	if (!(rte_atomic_fetch_sub_explicit(&ifpga_monitor_refcnt, 1,
-			rte_memory_order_relaxed) - 1) &&
-		ifpga_monitor_start_thread.opaque_id != 0) {
-		ret = pthread_cancel((pthread_t)ifpga_monitor_start_thread.opaque_id);
-		if (ret)
-			IFPGA_RAWDEV_PMD_ERR("Can't cancel the thread");
-
+	if (rte_atomic_fetch_sub_explicit(&ifpga_monitor_refcnt, 1, rte_memory_order_relaxed) == 1) {
 		ret = rte_thread_join(ifpga_monitor_start_thread, NULL);
 		if (ret)
 			IFPGA_RAWDEV_PMD_ERR("Can't join the thread");
Signature · 1 line
2.47.3
Reviewed-by on v1 3/6
#4 lore raw

Re: [RFC 3/6] raw/ifpga: avoid use of pthread_cancel

Hi,

________________________________
From: Stephen Hemminger <[email protected]>
Sent: Thursday, September 25, 2025 12:51 AM
To: [email protected] <[email protected]>
Cc: Stephen Hemminger <[email protected]>; Xu, Rosen <[email protected]>
Subject: [RFC 3/6] raw/ifpga: avoid use of pthread_cancel

[CAUTION: This email is from outside your organization. Unless you trust the sender, do not click on links or open attachments as it may be a fraudulent email attempting to steal your information and/or compromise your computer.]

Use refcnt to do controlled shutdown of control thread rather than using pthread_cancel().

---
 drivers/raw/ifpga/ifpga_rawdev.c | 8 +-------
 1 file changed, 1 insertion(+), 7 deletions(-)
drivers/raw/ifpga/ifpga_rawdev.c +1 -7
index 5b9b596435..d61927f193 100644
@@ -573,13 +573,7 @@ ifpga_monitor_stop_func(struct ifpga_rawdev *dev)
 
        dev->poll_enabled = 0;
 
-       if (!(rte_atomic_fetch_sub_explicit(&ifpga_monitor_refcnt, 1,
-                       rte_memory_order_relaxed) - 1) &&
-               ifpga_monitor_start_thread.opaque_id != 0) {
-               ret = pthread_cancel((pthread_t)ifpga_monitor_start_thread.opaque_id);
-               if (ret)
-                       IFPGA_RAWDEV_PMD_ERR("Can't cancel the thread");
-
+       if (rte_atomic_fetch_sub_explicit(&ifpga_monitor_refcnt, 1, rte_memory_order_relaxed) == 1) {
                ret = rte_thread_join(ifpga_monitor_start_thread, NULL);
                if (ret)
                        IFPGA_RAWDEV_PMD_ERR("Can't join the thread");

--
2.47.3

#5 lore raw

RFC 4/6 dma/skeleton: avoid use of pthread_cancel

In reply to Stephen Hemminger

Can use exit_flag to do controlled shutdown of the control thread.

---
 drivers/dma/skeleton/skeleton_dmadev.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
drivers/dma/skeleton/skeleton_dmadev.c +1 -4
index e287802eb9..62a25f9d34 100644
@@ -5,8 +5,6 @@
 #include <inttypes.h>
 #include <stdlib.h>
 
-#include <pthread.h>
-
 #include <bus_vdev_driver.h>
 #include <rte_cycles.h>
 #include <rte_eal.h>
@@ -215,10 +213,9 @@ skeldma_stop(struct rte_dma_dev *dev)
 {
 	struct skeldma_hw *hw = dev->data->dev_private;
 
+	/* setting exit flag causes cpuwork_thread to exit polling loop */
 	hw->exit_flag = true;
-	rte_delay_ms(1);
 
-	(void)pthread_cancel((pthread_t)hw->thread.opaque_id);
 	rte_thread_join(hw->thread, NULL);
 
 	return 0;
Signature · 1 line
2.47.3
#7 lore raw

RFC 5/6 intel/ipn3ke: avoid use of pthread_cancel

In reply to Stephen Hemminger

Can use the existing reference count (scan_num) to control the thread without using pthread_cancel.

---
 drivers/net/intel/ipn3ke/ipn3ke_representor.c | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)
drivers/net/intel/ipn3ke/ipn3ke_representor.c +2 -6
index feb57420c3..4e831c6924 100644
@@ -26,7 +26,7 @@
 #include "ipn3ke_logs.h"
 #include "ipn3ke_ethdev.h"
 
-static int ipn3ke_rpst_scan_num;
+static volatile int ipn3ke_rpst_scan_num;
 static rte_thread_t ipn3ke_rpst_scan_thread;
 
 /** Double linked list of representor port. */
@@ -2578,7 +2578,7 @@ ipn3ke_rpst_scan_handle_request(__rte_unused void *param)
 #define MS 1000
 #define SCAN_NUM 32
 
-	for (;;) {
+	while (ipn3ke_rpst_scan_num > 0) {
 		num = 0;
 		TAILQ_FOREACH(rpst, &ipn3ke_rpst_list, next) {
 			if (rpst->i40e_pf_eth &&
@@ -2612,10 +2612,6 @@ ipn3ke_rpst_scan_check(void)
 			return -1;
 		}
 	} else if (ipn3ke_rpst_scan_num == 0) {
-		ret = pthread_cancel((pthread_t)ipn3ke_rpst_scan_thread.opaque_id);
-		if (ret)
-			IPN3KE_AFU_PMD_ERR("Can't cancel the thread");
-
 		ret = rte_thread_join(ipn3ke_rpst_scan_thread, NULL);
 		if (ret)
 			IPN3KE_AFU_PMD_ERR("Can't join the thread");
Signature · 1 line
2.47.3
Reviewed-by on v1 5/6
#8 lore raw

Re: [RFC 5/6] intel/ipn3ke: avoid use of pthread_cancel

Hi,

________________________________
From: Stephen Hemminger <[email protected]>
Sent: Thursday, September 25, 2025 12:51 AM
To: [email protected] <[email protected]>
Cc: Stephen Hemminger <[email protected]>; Xu, Rosen <[email protected]>
Subject: [RFC 5/6] intel/ipn3ke: avoid use of pthread_cancel

[CAUTION: This email is from outside your organization. Unless you trust the sender, do not click on links or open attachments as it may be a fraudulent email attempting to steal your information and/or compromise your computer.]

Can use the existing reference count (scan_num) to control the thread without using pthread_cancel.

---
 drivers/net/intel/ipn3ke/ipn3ke_representor.c | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)
drivers/net/intel/ipn3ke/ipn3ke_representor.c +2 -6
index feb57420c3..4e831c6924 100644
@@ -26,7 +26,7 @@
 #include "ipn3ke_logs.h"
 #include "ipn3ke_ethdev.h"
 
-static int ipn3ke_rpst_scan_num;
+static volatile int ipn3ke_rpst_scan_num;
 static rte_thread_t ipn3ke_rpst_scan_thread;
 
 /** Double linked list of representor port. */
@@ -2578,7 +2578,7 @@ ipn3ke_rpst_scan_handle_request(__rte_unused void *param)
 #define MS 1000
 #define SCAN_NUM 32
 
-       for (;;) {
+       while (ipn3ke_rpst_scan_num > 0) {
                num = 0;
                TAILQ_FOREACH(rpst, &ipn3ke_rpst_list, next) {
                        if (rpst->i40e_pf_eth &&
@@ -2612,10 +2612,6 @@ ipn3ke_rpst_scan_check(void)
                        return -1;
                }
        } else if (ipn3ke_rpst_scan_num == 0) {
-               ret = pthread_cancel((pthread_t)ipn3ke_rpst_scan_thread.opaque_id);
-               if (ret)
-                       IPN3KE_AFU_PMD_ERR("Can't cancel the thread");
-
                ret = rte_thread_join(ipn3ke_rpst_scan_thread, NULL);
                if (ret)
                        IPN3KE_AFU_PMD_ERR("Can't join the thread");

--
2.47.3

#9 lore raw

RFC 6/6 intel/iavf: remove use of pthread_cancel

In reply to Stephen Hemminger

The iavf driver uses a pipe to communicate with control thread. By closing the write side of the pipe, the main thread can tell the control thread to exit without use of pthread_cancel.

---
 drivers/net/intel/iavf/iavf_vchnl.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/net/intel/iavf/iavf_vchnl.c +4 -4
index b1b7a5bf94..781bae11be 100644
@@ -161,14 +161,14 @@ iavf_dev_event_handler_fini(void)
 	if (rte_atomic_fetch_sub_explicit(&handler->ndev, 1, rte_memory_order_relaxed) - 1 != 0)
 		return;
 
-	int unused = pthread_cancel((pthread_t)handler->tid.opaque_id);
-	RTE_SET_USED(unused);
-	close(handler->fd[0]);
+	/* closing the write side of the pipe will cause read() to return 0 in thread */
 	close(handler->fd[1]);
-	handler->fd[0] = -1;
 	handler->fd[1] = -1;
 
 	rte_thread_join(handler->tid, NULL);
+	close(handler->fd[0]);
+	handler->fd[0] = -1;
+
 	pthread_mutex_destroy(&handler->lock);
 
 	struct iavf_event_element *pos, *save_next;
Signature · 1 line
2.47.3
Acked-by on v1 6/6
#10 lore raw

Re: [RFC 6/6] intel/iavf: remove use of pthread_cancel

Quoted 6 lines · Stephen Hemminger

The iavf driver uses a pipe to communicate with control thread. By closing the write side of the pipe, the main thread can tell the control thread to exit without use of pthread_cancel.

---

Quoted 29 lines
 drivers/net/intel/iavf/iavf_vchnl.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/net/intel/iavf/iavf_vchnl.c +4 -4
index b1b7a5bf94..781bae11be 100644
@@ -161,14 +161,14 @@ iavf_dev_event_handler_fini(void)
 	if (rte_atomic_fetch_sub_explicit(&handler->ndev, 1, rte_memory_order_relaxed) - 1 != 0)
 		return;
 
-	int unused = pthread_cancel((pthread_t)handler->tid.opaque_id);
-	RTE_SET_USED(unused);
-	close(handler->fd[0]);
+	/* closing the write side of the pipe will cause read() to return 0 in thread */
 	close(handler->fd[1]);
-	handler->fd[0] = -1;
 	handler->fd[1] = -1;
 
 	rte_thread_join(handler->tid, NULL);
+	close(handler->fd[0]);
+	handler->fd[0] = -1;
+
 	pthread_mutex_destroy(&handler->lock);
 
 	struct iavf_event_element *pos, *save_next;
Signature · 1 line
2.47.3