DPDK-dev Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Stephen Hemminger <stephen@networkplumber.org>
To: dev@dpdk.org
Cc: Stephen Hemminger <stephen@networkplumber.org>,
	stable@dpdk.org, Dmitry Kozlyuk <dmitry.kozliuk@gmail.com>
Subject: [PATCH v3 1/7] eal: add helper to initialize process-shared mutex
Date: Wed, 29 Apr 2026 11:46:38 -0700	[thread overview]
Message-ID: <20260429184739.706250-2-stephen@networkplumber.org> (raw)
In-Reply-To: <20260429184739.706250-1-stephen@networkplumber.org>

Drivers and libraries that place a pthread_mutex_t in shared memory
to coordinate primary and secondary DPDK processes must initialize
the mutex with PTHREAD_PROCESS_SHARED, otherwise behaviour is
undefined. Add an internal helper so this is done in one place rather
than copied into every driver.

Errors from the attribute calls are ignored: on platforms that do not
support cross-process mutex sharing the mutex still works within a
single process.

Bugzilla ID: 662
Cc: stable@dpdk.org

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 lib/eal/include/rte_thread.h | 15 +++++++++++++++
 lib/eal/unix/rte_thread.c    | 12 ++++++++++++
 lib/eal/windows/rte_thread.c | 10 ++++++++++
 3 files changed, 37 insertions(+)

diff --git a/lib/eal/include/rte_thread.h b/lib/eal/include/rte_thread.h
index 8da9d4d3fb..27d5b9c594 100644
--- a/lib/eal/include/rte_thread.h
+++ b/lib/eal/include/rte_thread.h
@@ -3,6 +3,7 @@
  * Copyright (C) 2022 Microsoft Corporation
  */
 
+#include <pthread.h>
 #include <stdint.h>
 
 #include <rte_os.h>
@@ -463,6 +464,20 @@ int rte_thread_value_set(rte_thread_key key, const void *value);
  */
 void *rte_thread_value_get(rte_thread_key key);
 
+/**
+ * Initialize a pthread mutex for use in shared memory accessible by
+ * multiple DPDK processes.
+ *
+ * Sets PTHREAD_PROCESS_SHARED so the mutex can synchronize threads
+ * across DPDK primary and secondary processes when the mutex resides
+ * in shared memory (e.g. hugepage memory).
+ *
+ * @param mutex
+ *   The mutex to initialize.
+ */
+__rte_internal
+void rte_thread_mutex_init_shared(pthread_mutex_t *mutex);
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/lib/eal/unix/rte_thread.c b/lib/eal/unix/rte_thread.c
index 950c0848ba..f3a6bd2ad3 100644
--- a/lib/eal/unix/rte_thread.c
+++ b/lib/eal/unix/rte_thread.c
@@ -419,3 +419,15 @@ rte_thread_get_affinity_by_id(rte_thread_t thread_id,
 	return pthread_getaffinity_np((pthread_t)thread_id.opaque_id,
 		sizeof(*cpuset), cpuset);
 }
+
+RTE_EXPORT_INTERNAL_SYMBOL(rte_thread_mutex_init_shared)
+void
+rte_thread_mutex_init_shared(pthread_mutex_t *mutex)
+{
+	pthread_mutexattr_t attr;
+
+	pthread_mutexattr_init(&attr);
+	pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
+	pthread_mutex_init(mutex, &attr);
+	pthread_mutexattr_destroy(&attr);
+}
diff --git a/lib/eal/windows/rte_thread.c b/lib/eal/windows/rte_thread.c
index 85e5a57346..19bb9ed61d 100644
--- a/lib/eal/windows/rte_thread.c
+++ b/lib/eal/windows/rte_thread.c
@@ -621,3 +621,13 @@ rte_thread_get_affinity_by_id(rte_thread_t thread_id,
 	}
 	return ret;
 }
+
+/* Note: Windows does not have PTHREAD_PROCESS_SHARED
+ * and DPDK on Windows only supports single process model.
+ */
+RTE_EXPORT_INTERNAL_SYMBOL(rte_thread_mutex_init_shared)
+void
+rte_thread_mutex_init_shared(pthread_mutex_t *mutex)
+{
+	pthread_mutex_init(mutex, NULL);
+}
-- 
2.53.0


  reply	other threads:[~2026-04-29 18:47 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-04-13 17:14 [PATCH 0/6] fix pthread mutexes for multi-process Stephen Hemminger
2026-04-13 17:14 ` [PATCH 1/6] ethdev: fix flow_ops_mutex " Stephen Hemminger
2026-04-13 17:14 ` [PATCH 2/6] net/failsafe: fix hotplug_mutex " Stephen Hemminger
2026-04-13 17:14 ` [PATCH 3/6] net/atlantic: fix mbox_mutex " Stephen Hemminger
2026-04-13 17:14 ` [PATCH 4/6] net/axgbe: fix mutexes " Stephen Hemminger
2026-04-13 17:14 ` [PATCH 5/6] net/bnxt: " Stephen Hemminger
2026-04-14 18:51   ` Kishore Padmanabha
2026-04-13 17:14 ` [PATCH 6/6] net/hinic: " Stephen Hemminger
2026-04-14 14:39 ` [PATCH v2 0/6] fix process shared pthread mutexes Stephen Hemminger
2026-04-14 14:39   ` [PATCH v2 1/6] ethdev: fix flow_ops_mutex for multi-process Stephen Hemminger
2026-04-14 14:39   ` [PATCH v2 2/6] net/failsafe: fix hotplug_mutex " Stephen Hemminger
2026-04-14 14:39   ` [PATCH v2 3/6] net/atlantic: fix mbox_mutex " Stephen Hemminger
2026-04-14 14:39   ` [PATCH v2 4/6] net/axgbe: fix mutexes " Stephen Hemminger
2026-04-14 14:39   ` [PATCH v2 5/6] net/bnxt: " Stephen Hemminger
2026-04-14 14:39   ` [PATCH v2 6/6] net/hinic: " Stephen Hemminger
2026-04-23 15:05   ` [PATCH v2 0/6] fix process shared pthread mutexes Stephen Hemminger
2026-04-23 17:29   ` Konstantin Ananyev
2026-05-08 16:39   ` Stephen Hemminger
2026-04-15 14:51 ` [PATCH v3 0/2] af_packet: cleanup and test Stephen Hemminger
2026-04-15 14:51   ` [PATCH v3 1/2] net/af_packet: fix indentation Stephen Hemminger
2026-04-15 14:51   ` [PATCH v3 2/2] test: add test for af_packet Stephen Hemminger
2026-04-16 17:57 ` [PATCH v4 0/2] af_packet: cleanup and test Stephen Hemminger
2026-04-16 17:57   ` [PATCH v4 1/2] net/af_packet: fix indentation Stephen Hemminger
2026-04-16 17:57   ` [PATCH v4 2/2] test: add test for af_packet Stephen Hemminger
2026-04-29 18:46 ` [PATCH v3 0/7] fix use of pthread mutex between processes Stephen Hemminger
2026-04-29 18:46   ` Stephen Hemminger [this message]
2026-04-29 18:46   ` [PATCH v3 2/7] ethdev: fix flow_ops_mutex for multi-process Stephen Hemminger
2026-04-29 18:46   ` [PATCH v3 3/7] net/failsafe: fix hotplug_mutex " Stephen Hemminger
2026-04-29 18:46   ` [PATCH v3 4/7] net/atlantic: fix mbox_mutex " Stephen Hemminger
2026-04-29 18:46   ` [PATCH v3 5/7] net/axgbe: fix mutexes " Stephen Hemminger
2026-04-29 18:46   ` [PATCH v3 6/7] net/bnxt: " Stephen Hemminger
2026-04-29 18:46   ` [PATCH v3 7/7] net/hinic: " Stephen Hemminger
2026-04-30 17:36   ` [PATCH v3 0/7] fix use of pthread mutex between processes Stephen Hemminger

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=20260429184739.706250-2-stephen@networkplumber.org \
    --to=stephen@networkplumber.org \
    --cc=dev@dpdk.org \
    --cc=dmitry.kozliuk@gmail.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