Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src-draft/trunk]: src/lib/libc Explicitly do malloc locking for fork. Full r...



details:   https://anonhg.NetBSD.org/src-all/rev/afd1a8284a81
branches:  trunk
changeset: 931684:afd1a8284a81
user:      Joerg Sonnenberger <joerg%bec.de@localhost>
date:      Fri Apr 17 18:26:28 2020 +0200

description:
Explicitly do malloc locking for fork. Full reinit the malloc lock in
post-fork for (old) jemalloc

diffstat:

 lib/libc/gen/pthread_atfork.c |   4 ++++
 lib/libc/include/extern.h     |   1 +
 lib/libc/stdlib/jemalloc.c    |  20 ++++++++++++++++++++
 lib/libc/stdlib/malloc.c      |   7 +++++++
 4 files changed, 32 insertions(+), 0 deletions(-)

diffs (88 lines):

diff -r 062654850045 -r afd1a8284a81 lib/libc/gen/pthread_atfork.c
--- a/lib/libc/gen/pthread_atfork.c     Thu Feb 20 15:17:19 2020 +0100
+++ b/lib/libc/gen/pthread_atfork.c     Fri Apr 17 18:26:28 2020 +0200
@@ -40,6 +40,7 @@
 #include <stdlib.h>
 #include <unistd.h>
 #include <sys/queue.h>
+#include "extern.h"
 #include "reentrant.h"
 
 #ifdef __weak_alias
@@ -163,6 +164,7 @@
        mutex_lock(&atfork_lock);
        SIMPLEQ_FOREACH(iter, &prepareq, next)
                (*iter->fn)();
+       _malloc_prefork();
 
        ret = __locked_fork(&errno);
 
@@ -171,11 +173,13 @@
                 * We are the parent. It doesn't matter here whether
                 * the fork call succeeded or failed.
                 */
+               _malloc_postfork();
                SIMPLEQ_FOREACH(iter, &parentq, next)
                        (*iter->fn)();
                mutex_unlock(&atfork_lock);
        } else {
                /* We are the child */
+               _malloc_postfork_child();
                SIMPLEQ_FOREACH(iter, &childq, next)
                        (*iter->fn)();
                /*
diff -r 062654850045 -r afd1a8284a81 lib/libc/include/extern.h
--- a/lib/libc/include/extern.h Thu Feb 20 15:17:19 2020 +0100
+++ b/lib/libc/include/extern.h Fri Apr 17 18:26:28 2020 +0200
@@ -69,6 +69,7 @@
 
 void   _malloc_prefork(void);
 void   _malloc_postfork(void);
+void   _malloc_postfork_child(void);
 
 int    _sys_setcontext(const ucontext_t *);
 
diff -r 062654850045 -r afd1a8284a81 lib/libc/stdlib/jemalloc.c
--- a/lib/libc/stdlib/jemalloc.c        Thu Feb 20 15:17:19 2020 +0100
+++ b/lib/libc/stdlib/jemalloc.c        Fri Apr 17 18:26:28 2020 +0200
@@ -4024,6 +4024,26 @@
        malloc_mutex_unlock(&init_lock);
 }
 
+void
+_malloc_postfork_child(void)
+{
+       unsigned i;
+
+       /* Release all mutexes, now that fork() has completed. */
+#ifdef USE_BRK
+       malloc_mutex_init(&brk_mtx);
+#endif
+       malloc_mutex_init(&base_mtx);
+       malloc_mutex_init(&chunks_mtx);
+
+       for (i = narenas; i-- > 0; ) {
+               if (arenas[i] != NULL)
+                       malloc_mutex_init(&arenas[i]->mtx);
+       }
+       malloc_mutex_init(&arenas_mtx);
+       malloc_mutex_init(&init_lock);
+}
+
 /*
  * End library-private functions.
  */
diff -r 062654850045 -r afd1a8284a81 lib/libc/stdlib/malloc.c
--- a/lib/libc/stdlib/malloc.c  Thu Feb 20 15:17:19 2020 +0100
+++ b/lib/libc/stdlib/malloc.c  Fri Apr 17 18:26:28 2020 +0200
@@ -1296,3 +1296,10 @@
 
        _MALLOC_UNLOCK();
 }
+
+void
+_malloc_postfork_child(void)
+{
+
+       _MALLOC_UNLOCK();
+}



Home | Main Index | Thread Index | Old Index