public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Siddhesh Poyarekar <siddhesh@sourceware.org>
To: libc-alpha@sourceware.org
Subject: [PATCH] malloc: Ensure that ptmalloc_init runs only once
Date: Thu, 17 Jun 2021 16:02:17 +0530	[thread overview]
Message-ID: <20210617103217.2633690-1-siddhesh@sourceware.org> (raw)

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


             reply	other threads:[~2021-06-17 10:32 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-06-17 10:32 Siddhesh Poyarekar [this message]
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

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=20210617103217.2633690-1-siddhesh@sourceware.org \
    --to=siddhesh@sourceware.org \
    --cc=libc-alpha@sourceware.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 read-only IMAP folder(s) and NNTP newsgroup(s).