patches for DPDK stable branches
 help / color / mirror / Atom feed
* [dpdk-stable] [PATCH] eal: fix possible UB on creation of ctrl thread
@ 2021-03-24 13:04 Luc Pelletier
  2021-03-25 11:27 ` [dpdk-stable] [PATCH v2] eal: fix race in ctrl thread creation Olivier Matz
  2021-04-06 16:15 ` [dpdk-stable] [PATCH v3] " Luc Pelletier
  0 siblings, 2 replies; 24+ messages in thread
From: Luc Pelletier @ 2021-03-24 13:04 UTC (permalink / raw)
  To: olivier.matz, jianfeng.tan; +Cc: dev, Luc Pelletier, stable

The creation of control threads uses a pthread barrier for
synchronization. This patch fixes a race condition where the pthread
barrier could get destroyed while one of the threads has not yet
returned from the pthread_barrier_wait function, which could result in
undefined behaviour.

Fixes: 3a0d465d4c53 ("eal: fix use-after-free on control thread creation")
Cc: jianfeng.tan@intel.com
Cc: stable@dpdk.org

Signed-off-by: Luc Pelletier <lucp.at.work@gmail.com>
---
 lib/librte_eal/common/eal_common_thread.c | 20 +++++++++++++++++---
 1 file changed, 17 insertions(+), 3 deletions(-)

diff --git a/lib/librte_eal/common/eal_common_thread.c b/lib/librte_eal/common/eal_common_thread.c
index 73a055902..bd9a9fdbc 100644
--- a/lib/librte_eal/common/eal_common_thread.c
+++ b/lib/librte_eal/common/eal_common_thread.c
@@ -170,6 +170,7 @@ struct rte_thread_ctrl_params {
 	void *(*start_routine)(void *);
 	void *arg;
 	pthread_barrier_t configured;
+	bool barrier_in_use;
 };
 
 static void *ctrl_thread_init(void *arg)
@@ -186,9 +187,13 @@ static void *ctrl_thread_init(void *arg)
 
 	ret = pthread_barrier_wait(&params->configured);
 	if (ret == PTHREAD_BARRIER_SERIAL_THREAD) {
+		while (__atomic_load_n(&params->barrier_in_use,
+				       __ATOMIC_ACQUIRE))
+			sched_yield();
 		pthread_barrier_destroy(&params->configured);
 		free(params);
-	}
+	} else
+		__atomic_store_n(&params->barrier_in_use, 0, __ATOMIC_RELEASE);
 
 	return start_routine(routine_arg);
 }
@@ -210,6 +215,7 @@ rte_ctrl_thread_create(pthread_t *thread, const char *name,
 
 	params->start_routine = start_routine;
 	params->arg = arg;
+	params->barrier_in_use = 1;
 
 	pthread_barrier_init(&params->configured, NULL, 2);
 
@@ -232,18 +238,26 @@ rte_ctrl_thread_create(pthread_t *thread, const char *name,
 
 	ret = pthread_barrier_wait(&params->configured);
 	if (ret == PTHREAD_BARRIER_SERIAL_THREAD) {
+		while (__atomic_load_n(&params->barrier_in_use,
+				       __ATOMIC_ACQUIRE))
+			sched_yield();
 		pthread_barrier_destroy(&params->configured);
 		free(params);
-	}
+	} else
+		__atomic_store_n(&params->barrier_in_use, 0, __ATOMIC_RELEASE);
 
 	return 0;
 
 fail:
 	if (PTHREAD_BARRIER_SERIAL_THREAD ==
 	    pthread_barrier_wait(&params->configured)) {
+		while (__atomic_load_n(&params->barrier_in_use,
+				       __ATOMIC_ACQUIRE))
+			sched_yield();
 		pthread_barrier_destroy(&params->configured);
 		free(params);
-	}
+	} else
+		__atomic_store_n(&params->barrier_in_use, 0, __ATOMIC_RELEASE);
 	pthread_cancel(*thread);
 	pthread_join(*thread, NULL);
 	return -ret;
-- 
2.25.1


^ permalink raw reply	[flat|nested] 24+ messages in thread

end of thread, other threads:[~2021-04-09 14:34 UTC | newest]

Thread overview: 24+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-03-24 13:04 [dpdk-stable] [PATCH] eal: fix possible UB on creation of ctrl thread Luc Pelletier
2021-03-25 11:27 ` [dpdk-stable] [PATCH v2] eal: fix race in ctrl thread creation Olivier Matz
2021-03-25 14:42   ` Luc Pelletier
2021-04-02  4:34   ` [dpdk-stable] [dpdk-dev] " Honnappa Nagarahalli
2021-04-06 16:15 ` [dpdk-stable] [PATCH v3] " Luc Pelletier
2021-04-06 21:10   ` Honnappa Nagarahalli
2021-04-07 12:35     ` [dpdk-stable] [PATCH v4] " Luc Pelletier
2021-04-07 12:53       ` [dpdk-stable] [PATCH v5] " Luc Pelletier
2021-04-07 13:22         ` Luc Pelletier
2021-04-07 13:31         ` Olivier Matz
2021-04-07 14:42           ` [dpdk-stable] [PATCH v6] " Luc Pelletier
2021-04-07 14:57             ` Olivier Matz
2021-04-07 15:29               ` [dpdk-stable] [PATCH v7] " Luc Pelletier
2021-04-07 17:15                 ` Honnappa Nagarahalli
2021-04-07 15:15           ` [dpdk-stable] [PATCH v5] " Honnappa Nagarahalli
2021-04-07 20:16             ` [dpdk-stable] [PATCH 1/2] " Luc Pelletier
2021-04-08 14:17               ` Olivier Matz
2021-04-08 17:06               ` Honnappa Nagarahalli
2021-04-07 20:16             ` [dpdk-stable] [PATCH 2/2] eal: fix hang in ctrl thread creation error logic Luc Pelletier
2021-04-08 14:20               ` Olivier Matz
2021-04-08 18:01                 ` Luc Pelletier
2021-04-09  8:13                   ` [dpdk-stable] [dpdk-dev] " David Marchand
2021-04-08 17:07               ` [dpdk-stable] " Honnappa Nagarahalli
2021-04-09 14:34               ` David Marchand

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).