patches for DPDK stable branches
 help / color / mirror / Atom feed
From: Luc Pelletier <lucp.at.work@gmail.com>
To: olivier.matz@6wind.com, jianfeng.tan@intel.com
Cc: dev@dpdk.org, Luc Pelletier <lucp.at.work@gmail.com>, stable@dpdk.org
Subject: [dpdk-stable] [PATCH] eal: fix possible UB on creation of ctrl thread
Date: Wed, 24 Mar 2021 09:04:22 -0400	[thread overview]
Message-ID: <20210324130422.92357-1-lucp.at.work@gmail.com> (raw)

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


             reply	other threads:[~2021-03-24 13:05 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-03-24 13:04 Luc Pelletier [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210324130422.92357-1-lucp.at.work@gmail.com \
    --to=lucp.at.work@gmail.com \
    --cc=dev@dpdk.org \
    --cc=jianfeng.tan@intel.com \
    --cc=olivier.matz@6wind.com \
    --cc=stable@dpdk.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).