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.
This commit is contained in:
joerg 2020-05-15 14:37:21 +00:00
parent 6389ebde8b
commit 8409cf4a20
5 changed files with 53 additions and 11 deletions

View File

@ -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 @@ malloc_init_hard_recursible(void) {
#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 @@ jemalloc_constructor(void) {
}
#endif
#ifndef JEMALLOC_MUTEX_INIT_CB
#if !defined(JEMALLOC_MUTEX_INIT_CB) && !defined(__NetBSD__)
void
jemalloc_prefork(void)
#else
@ -3294,7 +3298,7 @@ _malloc_prefork(void)
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 @@ _malloc_postfork(void)
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;

View File

@ -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 @@ __RCSID("$NetBSD: pthread_atfork.c,v 1.14 2020/04/19 01:06:15 joerg Exp $");
#include <stdlib.h>
#include <unistd.h>
#include <sys/queue.h>
#include "extern.h"
#include "reentrant.h"
#ifdef __weak_alias
@ -163,6 +164,7 @@ fork(void)
mutex_lock(&atfork_lock);
SIMPLEQ_FOREACH(iter, &prepareq, next)
(*iter->fn)();
_malloc_prefork();
ret = __locked_fork(&errno);
@ -171,11 +173,13 @@ fork(void)
* 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)();
/*

View File

@ -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 vsyslogp_ss(int, struct syslog_data *, const char *, const char *,
void _malloc_prefork(void);
void _malloc_postfork(void);
void _malloc_postfork_child(void);
int _sys_setcontext(const ucontext_t *);

View File

@ -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>.
@ -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_postfork(void)
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.
*/

View File

@ -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 @@ int utrace(const char *, void *, size_t);
# 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_postfork(void)
_MALLOC_UNLOCK();
}
void
_malloc_postfork_child(void)
{
_MALLOC_UNLOCK();
}