diff mbox

malloc: remove atfork hooks

Message ID 1453767942-19369-60-git-send-email-joern@purestorage.com
State New
Headers show

Commit Message

Jörn Engel Jan. 26, 2016, 12:25 a.m. UTC
They are no longer necessary or even checked.  More importantly, this
also no longer sets the thread-local arena-pointer to -1, which
triggered a segfault.

JIRA: PURE-42344
https://codereviews.purestorage.com/r/23706/
---
 tpc/malloc2.13/arena.h  | 129 +++++-------------------------------------------
 tpc/malloc2.13/hooks.h  |  48 ------------------
 tpc/malloc2.13/malloc.c |  13 -----
 3 files changed, 12 insertions(+), 178 deletions(-)
diff mbox

Patch

diff --git a/tpc/malloc2.13/arena.h b/tpc/malloc2.13/arena.h
index 587bad51dd63..2205c52da8f1 100644
--- a/tpc/malloc2.13/arena.h
+++ b/tpc/malloc2.13/arena.h
@@ -105,83 +105,9 @@  static int __malloc_initialized = -1;
 
 /* atfork support.  */
 
-static __malloc_ptr_t (*save_malloc_hook) (size_t __size,
-					   __const __malloc_ptr_t);
-static __malloc_ptr_t (*save_memalign_hook) (size_t __align, size_t __size,
-					     __const __malloc_ptr_t);
-static void           (*save_free_hook) (__malloc_ptr_t __ptr,
-					 __const __malloc_ptr_t);
-static Void_t*        save_arena;
-
-#ifdef ATFORK_MEM
-ATFORK_MEM;
-#endif
-
-/* Magic value for the thread-specific arena pointer when
-   malloc_atfork() is in use.  */
-
-#define ATFORK_ARENA_PTR ((Void_t*)-1)
-
-/* The following hooks are used while the `atfork' handling mechanism
-   is active. */
-
-static Void_t*
-malloc_atfork(size_t sz, const Void_t *caller)
-{
-  Void_t *vptr = NULL;
-  Void_t *victim;
-
-  tsd_getspecific(arena_key, vptr);
-  if(vptr == ATFORK_ARENA_PTR) {
-    /* We are the only thread that may allocate at all.  */
-    if(save_malloc_hook != malloc_check) {
-      return _int_malloc(&main_arena, sz);
-    } else {
-      if(top_check()<0)
-	return 0;
-      victim = _int_malloc(&main_arena, sz+1);
-      return mem2mem_check(victim, sz);
-    }
-  } else {
-    /* Suspend the thread until the `atfork' handlers have completed.
-       By that time, the hooks will have been reset as well, so that
-       mALLOc() can be used again. */
-    (void)mutex_lock(&list_lock);
-    (void)mutex_unlock(&list_lock);
-    return public_mALLOc(sz);
-  }
-}
-
-static void
-free_atfork(Void_t* mem, const Void_t *caller)
-{
-  Void_t *vptr = NULL;
-  struct malloc_state * ar_ptr;
-  mchunkptr p;                          /* chunk corresponding to mem */
-
-  if (mem == 0)                              /* free(0) has no effect */
-    return;
-
-  p = mem2chunk(mem);         /* do not bother to replicate free_check here */
-
-  if (chunk_is_mmapped(p))                       /* release mmapped memory. */
-  {
-    munmap_chunk(p);
-    return;
-  }
-
-  ar_ptr = arena_for_chunk(p);
-  tsd_getspecific(arena_key, vptr);
-  if(vptr != ATFORK_ARENA_PTR)
-    (void)mutex_lock(&ar_ptr->mutex);
-  _int_free(ar_ptr, p);
-  if(vptr != ATFORK_ARENA_PTR)
-    (void)mutex_unlock(&ar_ptr->mutex);
-}
-
-
 /* Counter for number of times the list is locked by the same thread.  */
 static unsigned int atfork_recursive_cntr;
+static unsigned int atfork_recursive_thread;
 
 /* The following two functions are registered via thread_atfork() to
    make sure that the mutexes remain in a consistent state in the
@@ -189,6 +115,12 @@  static unsigned int atfork_recursive_cntr;
    temporarily, because the `atfork' handler mechanism may use
    malloc/free internally (e.g. in LinuxThreads). */
 
+#include <sys/syscall.h>
+static inline pid_t gettid(void)
+{
+	return syscall(SYS_gettid);
+}
+
 static void
 ptmalloc_lock_all (void)
 {
@@ -198,9 +130,7 @@  ptmalloc_lock_all (void)
     return;
   if (mutex_trylock(&list_lock))
     {
-      Void_t *my_arena;
-      tsd_getspecific(arena_key, my_arena);
-      if (my_arena == ATFORK_ARENA_PTR)
+      if (atfork_recursive_thread == gettid())
 	/* This is the same thread which already locks the global list.
 	   Just bump the counter.  */
 	goto out;
@@ -213,13 +143,7 @@  ptmalloc_lock_all (void)
     ar_ptr = ar_ptr->next;
     if(ar_ptr == &main_arena) break;
   }
-  save_malloc_hook = dlmalloc_hook;
-  save_free_hook = dlfree_hook;
-  dlmalloc_hook = malloc_atfork;
-  dlfree_hook = free_atfork;
-  /* Only the current thread may perform malloc/free calls now. */
-  tsd_getspecific(arena_key, save_arena);
-  tsd_setspecific(arena_key, ATFORK_ARENA_PTR);
+  atfork_recursive_thread = gettid();
  out:
   ++atfork_recursive_cntr;
 }
@@ -233,9 +157,7 @@  ptmalloc_unlock_all (void)
     return;
   if (--atfork_recursive_cntr != 0)
     return;
-  tsd_setspecific(arena_key, save_arena);
-  dlmalloc_hook = save_malloc_hook;
-  dlfree_hook = save_free_hook;
+  atfork_recursive_thread = 0;
   for(ar_ptr = &main_arena;;) {
     (void)mutex_unlock(&ar_ptr->mutex);
     ar_ptr = ar_ptr->next;
@@ -259,7 +181,7 @@  ptmalloc_unlock_all2 (void)
   if(__malloc_initialized < 1)
     return;
 #if defined MALLOC_HOOKS
-  tsd_setspecific(arena_key, save_arena);
+  atfork_recursive_thread = 0;
   dlmalloc_hook = save_malloc_hook;
   dlfree_hook = save_free_hook;
 #endif
@@ -302,7 +224,6 @@  static int num_nodes = 0;
 #include <sys/types.h>
 #include <dirent.h>
 #include <string.h>
-#include <sys/syscall.h>
 
 /*
  * Wouldn't it be nice to get this with a single syscall instead?
@@ -329,11 +250,6 @@  static int numa_node_count(void)
 	return ret;
 }
 
-static inline pid_t gettid(void)
-{
-	return syscall(SYS_gettid);
-}
-
 static void ptmalloc_init(void)
 {
 	const char *s;
@@ -355,19 +271,6 @@  static void ptmalloc_init(void)
 	}
 	ptmalloc_init_minimal();
 
-#ifndef NO_THREADS
-#ifndef NO_STARTER
-	/* With some threads implementations, creating thread-specific data
-	   or initializing a mutex may call malloc() itself.  Provide a
-	   simple starter version (realloc() won't work). */
-	save_malloc_hook = dlmalloc_hook;
-	save_memalign_hook = dlmemalign_hook;
-	save_free_hook = dlfree_hook;
-	dlmalloc_hook = malloc_starter;
-	dlmemalign_hook = memalign_starter;
-	dlfree_hook = free_starter;
-#endif				/* !defined NO_STARTER */
-#endif				/* !defined NO_THREADS */
 	mutex_init(&main_arena.mutex);
 	main_arena.next = &main_arena;
 	main_arena.local_next = &main_arena;
@@ -391,15 +294,7 @@  static void ptmalloc_init(void)
 	tsd_key_create(&arena_key, NULL);
 	tsd_setspecific(arena_key, (Void_t *) & main_arena);
 	thread_atfork(ptmalloc_lock_all, ptmalloc_unlock_all, ptmalloc_unlock_all2);
-#ifndef NO_THREADS
-#ifndef NO_STARTER
-	dlmalloc_hook = save_malloc_hook;
-	dlmemalign_hook = save_memalign_hook;
-	dlfree_hook = save_free_hook;
-#else
-#undef NO_STARTER
-#endif
-#endif
+
 	if (!secure) {
 		if ((s = getenv("MALLOC_TRIM_THRESHOLD_")))
 			mALLOPt(M_TRIM_THRESHOLD, atoi(s));
diff --git a/tpc/malloc2.13/hooks.h b/tpc/malloc2.13/hooks.h
index afc8eeb93a8b..f192080a71bf 100644
--- a/tpc/malloc2.13/hooks.h
+++ b/tpc/malloc2.13/hooks.h
@@ -361,54 +361,6 @@  memalign_check(size_t alignment, size_t bytes, const Void_t *caller)
   return mem2mem_check(mem, bytes);
 }
 
-#ifndef NO_THREADS
-
-
-# ifdef NO_STARTER
-#  undef NO_STARTER
-# else
-
-/* The following hooks are used when the global initialization in
-   ptmalloc_init() hasn't completed yet. */
-
-static Void_t*
-malloc_starter(size_t sz, const Void_t *caller)
-{
-  Void_t* victim;
-
-  victim = _int_malloc(&main_arena, sz);
-
-  return victim ? BOUNDED_N(victim, sz) : 0;
-}
-
-static Void_t*
-memalign_starter(size_t align, size_t sz, const Void_t *caller)
-{
-  Void_t* victim;
-
-  victim = _int_memalign(&main_arena, align, sz);
-
-  return victim ? BOUNDED_N(victim, sz) : 0;
-}
-
-static void
-free_starter(Void_t* mem, const Void_t *caller)
-{
-  mchunkptr p;
-
-  if(!mem) return;
-  p = mem2chunk(mem);
-  if (chunk_is_mmapped(p)) {
-    munmap_chunk(p);
-    return;
-  }
-  _int_free(&main_arena, p);
-}
-
-# endif	/* !defiend NO_STARTER */
-#endif /* NO_THREADS */
-
-
 /* Get/set state: malloc_get_state() records the current state of all
    malloc variables (_except_ for the actual heap contents and `hook'
    function pointers) in a system dependent, opaque data structure.
diff --git a/tpc/malloc2.13/malloc.c b/tpc/malloc2.13/malloc.c
index 2e3067344ad2..f56321444b76 100644
--- a/tpc/malloc2.13/malloc.c
+++ b/tpc/malloc2.13/malloc.c
@@ -1392,19 +1392,6 @@  static Void_t*   realloc_check(Void_t* oldmem, size_t bytes,
 			       const Void_t *caller);
 static Void_t*   memalign_check(size_t alignment, size_t bytes,
 				const Void_t *caller);
-#ifndef NO_THREADS
-# ifdef NO_STARTER
-#  undef NO_STARTER
-# else
-static Void_t*   malloc_starter(size_t sz, const Void_t *caller);
-static Void_t*   memalign_starter(size_t aln, size_t sz, const Void_t *caller);
-static void      free_starter(Void_t* mem, const Void_t *caller);
-# endif
-static Void_t*   malloc_atfork(size_t sz, const Void_t *caller);
-static void      free_atfork(Void_t* mem, const Void_t *caller);
-#endif
-
-