Source-Changes-HG archive

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

[src/trunk]: src/lib/libc/stdlib Hook up proper fork lock handling for malloc:



details:   https://anonhg.NetBSD.org/src/rev/1e76dc51ec90
branches:  trunk
changeset: 1010122:1e76dc51ec90
user:      joerg <joerg%NetBSD.org@localhost>
date:      Fri May 15 14:37:21 2020 +0000

description:
Hook up proper fork lock handling for malloc:
- lock all relevant mutexes just before fork
- unlock all mutexes just after fork in the parent
- full reinit non-spinlocks in the child
This is not using the normal pthread_atfork interface to ensure order of
operation, malloc is used as implementation detail too often.

diffstat:

 external/bsd/jemalloc/dist/src/jemalloc.c |  18 ++++++++++++++----
 lib/libc/gen/pthread_atfork.c             |   8 ++++++--
 lib/libc/include/extern.h                 |   3 ++-
 lib/libc/stdlib/jemalloc.c                |  24 ++++++++++++++++++++++--
 lib/libc/stdlib/malloc.c                  |  11 +++++++++--
 5 files changed, 53 insertions(+), 11 deletions(-)

diffs (195 lines):

diff -r bef0894ba260 -r 1e76dc51ec90 external/bsd/jemalloc/dist/src/jemalloc.c
--- a/external/bsd/jemalloc/dist/src/jemalloc.c Fri May 15 14:34:41 2020 +0000
+++ b/external/bsd/jemalloc/dist/src/jemalloc.c Fri May 15 14:37:21 2020 +0000
@@ -1,5 +1,9 @@
 #include <sys/cdefs.h>
 
+#ifdef __NetBSD__
+#include "extern.h"
+#endif
+
 #define JEMALLOC_C_
 #include "jemalloc/internal/jemalloc_preamble.h"
 #include "jemalloc/internal/jemalloc_internal_includes.h"
@@ -1383,7 +1387,7 @@
 
 #if (defined(JEMALLOC_HAVE_PTHREAD_ATFORK) && !defined(JEMALLOC_MUTEX_INIT_CB) \
     && !defined(JEMALLOC_ZONE) && !defined(_WIN32) && \
-    !defined(__native_client__))
+    !defined(__native_client__) && !defined(__NetBSD__))
        /* LinuxThreads' pthread_atfork() allocates. */
        if (pthread_atfork(jemalloc_prefork, jemalloc_postfork_parent,
            jemalloc_postfork_child) != 0) {
@@ -3221,7 +3225,7 @@
 }
 #endif
 
-#ifndef JEMALLOC_MUTEX_INIT_CB
+#if !defined(JEMALLOC_MUTEX_INIT_CB) && !defined(__NetBSD__)
 void
 jemalloc_prefork(void)
 #else
@@ -3294,7 +3298,7 @@
        prof_prefork1(tsd_tsdn(tsd));
 }
 
-#ifndef JEMALLOC_MUTEX_INIT_CB
+#if !defined(JEMALLOC_MUTEX_INIT_CB) && !defined(__NetBSD__)
 void
 jemalloc_postfork_parent(void)
 #else
@@ -3332,8 +3336,14 @@
        ctl_postfork_parent(tsd_tsdn(tsd));
 }
 
+#if !defined(__NetBSD__)
 void
-jemalloc_postfork_child(void) {
+jemalloc_postfork_child(void)
+#else
+JEMALLOC_EXPORT void
+_malloc_postfork_child(void)
+#endif
+{
        tsd_t *tsd;
        unsigned i, narenas;
 
diff -r bef0894ba260 -r 1e76dc51ec90 lib/libc/gen/pthread_atfork.c
--- a/lib/libc/gen/pthread_atfork.c     Fri May 15 14:34:41 2020 +0000
+++ b/lib/libc/gen/pthread_atfork.c     Fri May 15 14:37:21 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: pthread_atfork.c,v 1.14 2020/04/19 01:06:15 joerg Exp $        */
+/*     $NetBSD: pthread_atfork.c,v 1.15 2020/05/15 14:37:21 joerg Exp $        */
 
 /*-
  * Copyright (c) 2002 The NetBSD Foundation, Inc.
@@ -31,7 +31,7 @@
 
 #include <sys/cdefs.h>
 #if defined(LIBC_SCCS) && !defined(lint)
-__RCSID("$NetBSD: pthread_atfork.c,v 1.14 2020/04/19 01:06:15 joerg Exp $");
+__RCSID("$NetBSD: pthread_atfork.c,v 1.15 2020/05/15 14:37:21 joerg Exp $");
 #endif /* LIBC_SCCS and not lint */
 
 #include "namespace.h"
@@ -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 bef0894ba260 -r 1e76dc51ec90 lib/libc/include/extern.h
--- a/lib/libc/include/extern.h Fri May 15 14:34:41 2020 +0000
+++ b/lib/libc/include/extern.h Fri May 15 14:37:21 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: extern.h,v 1.25 2017/01/12 00:43:55 christos Exp $     */
+/*     $NetBSD: extern.h,v 1.26 2020/05/15 14:37:21 joerg Exp $        */
 
 /*
  * Copyright (c) 1997 Christos Zoulas.  All rights reserved.
@@ -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 bef0894ba260 -r 1e76dc51ec90 lib/libc/stdlib/jemalloc.c
--- a/lib/libc/stdlib/jemalloc.c        Fri May 15 14:34:41 2020 +0000
+++ b/lib/libc/stdlib/jemalloc.c        Fri May 15 14:37:21 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: jemalloc.c,v 1.52 2020/05/03 07:32:54 mrg Exp $        */
+/*     $NetBSD: jemalloc.c,v 1.53 2020/05/15 14:37:21 joerg Exp $      */
 
 /*-
  * Copyright (C) 2006,2007 Jason Evans <jasone%FreeBSD.org@localhost>.
@@ -117,7 +117,7 @@
 
 #include <sys/cdefs.h>
 /* __FBSDID("$FreeBSD: src/lib/libc/stdlib/malloc.c,v 1.147 2007/06/15 22:00:16 jasone Exp $"); */ 
-__RCSID("$NetBSD: jemalloc.c,v 1.52 2020/05/03 07:32:54 mrg Exp $");
+__RCSID("$NetBSD: jemalloc.c,v 1.53 2020/05/15 14:37:21 joerg Exp $");
 
 #ifdef __FreeBSD__
 #include "libc_private.h"
@@ -4027,6 +4027,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 bef0894ba260 -r 1e76dc51ec90 lib/libc/stdlib/malloc.c
--- a/lib/libc/stdlib/malloc.c  Fri May 15 14:34:41 2020 +0000
+++ b/lib/libc/stdlib/malloc.c  Fri May 15 14:37:21 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: malloc.c,v 1.59 2017/01/13 04:18:54 christos Exp $     */
+/*     $NetBSD: malloc.c,v 1.60 2020/05/15 14:37:21 joerg Exp $        */
 
 /*
  * ----------------------------------------------------------------------------
@@ -93,7 +93,7 @@
 # include <sys/cdefs.h>
 # include "extern.h"
 # if defined(LIBC_SCCS) && !defined(lint)
-__RCSID("$NetBSD: malloc.c,v 1.59 2017/01/13 04:18:54 christos Exp $");
+__RCSID("$NetBSD: malloc.c,v 1.60 2020/05/15 14:37:21 joerg Exp $");
 # endif /* LIBC_SCCS and not lint */
 # include <reentrant.h>
 # ifdef _REENTRANT
@@ -1296,3 +1296,10 @@
 
        _MALLOC_UNLOCK();
 }
+
+void
+_malloc_postfork_child(void)
+{
+
+       _MALLOC_UNLOCK();
+}



Home | Main Index | Thread Index | Old Index