public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] malloc: Ensure that ptmalloc_init runs only once
@ 2021-06-17 10:32 Siddhesh Poyarekar
  2021-06-17 14:31 ` Florian Weimer
  0 siblings, 1 reply; 9+ messages in thread
From: Siddhesh Poyarekar @ 2021-06-17 10:32 UTC (permalink / raw)
  To: libc-alpha

It is possible that multiple threads simultaneously enter
ptmalloc_init and succeed the < 0 check.  Make the comparison and
setting of __malloc_initialized atomic so that only one of them goes
through.  Additionally, if a thread sees that another thread is
running the initialization (i.e. __malloc_initialized == 0) then wait
till it is done.
---
 malloc/arena.c  | 12 +++++++++---
 malloc/malloc.c | 14 +++++++-------
 2 files changed, 16 insertions(+), 10 deletions(-)

diff --git a/malloc/arena.c b/malloc/arena.c
index 7eb110445e..3cd4391f25 100644
--- a/malloc/arena.c
+++ b/malloc/arena.c
@@ -290,10 +290,16 @@ libc_hidden_proto (_dl_open_hook);
 static void
 ptmalloc_init (void)
 {
-  if (__malloc_initialized >= 0)
-    return;
+  int oldval = catomic_compare_and_exchange_val_acq (&__malloc_initialized,
+						     -1, 0);
 
-  __malloc_initialized = 0;
+  if (oldval == 1)
+    return;
+  else if (oldval == 0)
+    {
+      while (__malloc_initialized != 1);
+      return;
+    }
 
 #ifdef USE_MTAG
   if ((TUNABLE_GET_FULL (glibc, mem, tagging, int32_t, NULL) & 1) != 0)
diff --git a/malloc/malloc.c b/malloc/malloc.c
index 0e2e1747e0..cc3d1f41e8 100644
--- a/malloc/malloc.c
+++ b/malloc/malloc.c
@@ -3562,7 +3562,7 @@ libc_hidden_def (__libc_memalign)
 void *
 __libc_valloc (size_t bytes)
 {
-  if (__malloc_initialized < 0)
+  if (__malloc_initialized <= 0)
     ptmalloc_init ();
 
   void *address = RETURN_ADDRESS (0);
@@ -3573,7 +3573,7 @@ __libc_valloc (size_t bytes)
 void *
 __libc_pvalloc (size_t bytes)
 {
-  if (__malloc_initialized < 0)
+  if (__malloc_initialized <= 0)
     ptmalloc_init ();
 
   void *address = RETURN_ADDRESS (0);
@@ -5077,7 +5077,7 @@ __malloc_trim (size_t s)
 {
   int result = 0;
 
-  if (__malloc_initialized < 0)
+  if (__malloc_initialized <= 0)
     ptmalloc_init ();
 
   mstate ar_ptr = &main_arena;
@@ -5212,7 +5212,7 @@ __libc_mallinfo2 (void)
   struct mallinfo2 m;
   mstate ar_ptr;
 
-  if (__malloc_initialized < 0)
+  if (__malloc_initialized <= 0)
     ptmalloc_init ();
 
   memset (&m, 0, sizeof (m));
@@ -5263,7 +5263,7 @@ __malloc_stats (void)
   mstate ar_ptr;
   unsigned int in_use_b = mp_.mmapped_mem, system_b = in_use_b;
 
-  if (__malloc_initialized < 0)
+  if (__malloc_initialized <= 0)
     ptmalloc_init ();
   _IO_flockfile (stderr);
   int old_flags2 = stderr->_flags2;
@@ -5432,7 +5432,7 @@ __libc_mallopt (int param_number, int value)
   mstate av = &main_arena;
   int res = 1;
 
-  if (__malloc_initialized < 0)
+  if (__malloc_initialized <= 0)
     ptmalloc_init ();
   __libc_lock_lock (av->mutex);
 
@@ -5691,7 +5691,7 @@ __malloc_info (int options, FILE *fp)
 
 
 
-  if (__malloc_initialized < 0)
+  if (__malloc_initialized <= 0)
     ptmalloc_init ();
 
   fputs ("<malloc version=\"1\">\n", fp);
-- 
2.31.1


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

end of thread, other threads:[~2021-06-18  6:36 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-06-17 10:32 [PATCH] malloc: Ensure that ptmalloc_init runs only once Siddhesh Poyarekar
2021-06-17 14:31 ` Florian Weimer
2021-06-18  2:31   ` Siddhesh Poyarekar
2021-06-18  5:45     ` Florian Weimer
2021-06-18  5:51       ` Siddhesh Poyarekar
2021-06-18  5:55         ` Florian Weimer
2021-06-18  6:28           ` Siddhesh Poyarekar
2021-06-18  6:32             ` Siddhesh Poyarekar
2021-06-18  6:36             ` Florian Weimer

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 read-only IMAP folder(s) and NNTP newsgroup(s).