d8307d
commit 669ff911e2571f74a2668493e326ac9a505776bd
d8307d
Author: Florian Weimer <fweimer@redhat.com>
d8307d
Date:   Fri Feb 8 12:46:19 2019 +0100
d8307d
d8307d
    nptl: Avoid fork handler lock for async-signal-safe fork [BZ #24161]
d8307d
    
d8307d
    Commit 27761a1042daf01987e7d79636d0c41511c6df3c ("Refactor atfork
d8307d
    handlers") introduced a lock, atfork_lock, around fork handler list
d8307d
    accesses.  It turns out that this lock occasionally results in
d8307d
    self-deadlocks in malloc/tst-mallocfork2:
d8307d
    
d8307d
    (gdb) bt
d8307d
    #0  __lll_lock_wait_private ()
d8307d
        at ../sysdeps/unix/sysv/linux/x86_64/lowlevellock.S:63
d8307d
    #1  0x00007f160c6f927a in __run_fork_handlers (who=(unknown: 209394016),
d8307d
        who@entry=atfork_run_prepare) at register-atfork.c:116
d8307d
    #2  0x00007f160c6b7897 in __libc_fork () at ../sysdeps/nptl/fork.c:58
d8307d
    #3  0x00000000004027d6 in sigusr1_handler (signo=<optimized out>)
d8307d
        at tst-mallocfork2.c:80
d8307d
    #4  sigusr1_handler (signo=<optimized out>) at tst-mallocfork2.c:64
d8307d
    #5  <signal handler called>
d8307d
    #6  0x00007f160c6f92e4 in __run_fork_handlers (who=who@entry=atfork_run_parent)
d8307d
        at register-atfork.c:136
d8307d
    #7  0x00007f160c6b79a2 in __libc_fork () at ../sysdeps/nptl/fork.c:152
d8307d
    #8  0x0000000000402567 in do_test () at tst-mallocfork2.c:156
d8307d
    #9  0x0000000000402dd2 in support_test_main (argc=1, argv=0x7ffc81ef1ab0,
d8307d
        config=config@entry=0x7ffc81ef1970) at support_test_main.c:350
d8307d
    #10 0x0000000000402362 in main (argc=<optimized out>, argv=<optimized out>)
d8307d
        at ../support/test-driver.c:168
d8307d
    
d8307d
    If no locking happens in the single-threaded case (where fork is
d8307d
    expected to be async-signal-safe), this deadlock is avoided.
d8307d
    (pthread_atfork is not required to be async-signal-safe, so a fork
d8307d
    call from a signal handler interrupting pthread_atfork is not
d8307d
    a problem.)
d8307d
d8307d
diff --git a/nptl/register-atfork.c b/nptl/register-atfork.c
d8307d
index bc797b7..80a1bec 100644
d8307d
--- a/nptl/register-atfork.c
d8307d
+++ b/nptl/register-atfork.c
d8307d
@@ -107,13 +107,14 @@ __unregister_atfork (void *dso_handle)
d8307d
 }
d8307d
 
d8307d
 void
d8307d
-__run_fork_handlers (enum __run_fork_handler_type who)
d8307d
+__run_fork_handlers (enum __run_fork_handler_type who, _Bool do_locking)
d8307d
 {
d8307d
   struct fork_handler *runp;
d8307d
 
d8307d
   if (who == atfork_run_prepare)
d8307d
     {
d8307d
-      lll_lock (atfork_lock, LLL_PRIVATE);
d8307d
+      if (do_locking)
d8307d
+	lll_lock (atfork_lock, LLL_PRIVATE);
d8307d
       size_t sl = fork_handler_list_size (&fork_handlers);
d8307d
       for (size_t i = sl; i > 0; i--)
d8307d
 	{
d8307d
@@ -133,7 +134,8 @@ __run_fork_handlers (enum __run_fork_handler_type who)
d8307d
 	  else if (who == atfork_run_parent && runp->parent_handler)
d8307d
 	    runp->parent_handler ();
d8307d
 	}
d8307d
-      lll_unlock (atfork_lock, LLL_PRIVATE);
d8307d
+      if (do_locking)
d8307d
+	lll_unlock (atfork_lock, LLL_PRIVATE);
d8307d
     }
d8307d
 }
d8307d
 
d8307d
diff --git a/sysdeps/nptl/fork.c b/sysdeps/nptl/fork.c
d8307d
index bd68f18..14b69a6 100644
d8307d
--- a/sysdeps/nptl/fork.c
d8307d
+++ b/sysdeps/nptl/fork.c
d8307d
@@ -55,7 +55,7 @@ __libc_fork (void)
d8307d
      but our current fork implementation is not.  */
d8307d
   bool multiple_threads = THREAD_GETMEM (THREAD_SELF, header.multiple_threads);
d8307d
 
d8307d
-  __run_fork_handlers (atfork_run_prepare);
d8307d
+  __run_fork_handlers (atfork_run_prepare, multiple_threads);
d8307d
 
d8307d
   /* If we are not running multiple threads, we do not have to
d8307d
      preserve lock state.  If fork runs from a signal handler, only
d8307d
@@ -134,7 +134,7 @@ __libc_fork (void)
d8307d
       __rtld_lock_initialize (GL(dl_load_lock));
d8307d
 
d8307d
       /* Run the handlers registered for the child.  */
d8307d
-      __run_fork_handlers (atfork_run_child);
d8307d
+      __run_fork_handlers (atfork_run_child, multiple_threads);
d8307d
     }
d8307d
   else
d8307d
     {
d8307d
@@ -149,7 +149,7 @@ __libc_fork (void)
d8307d
 	}
d8307d
 
d8307d
       /* Run the handlers registered for the parent.  */
d8307d
-      __run_fork_handlers (atfork_run_parent);
d8307d
+      __run_fork_handlers (atfork_run_parent, multiple_threads);
d8307d
     }
d8307d
 
d8307d
   return pid;
d8307d
diff --git a/sysdeps/nptl/fork.h b/sysdeps/nptl/fork.h
d8307d
index a1c3b26..99ed760 100644
d8307d
--- a/sysdeps/nptl/fork.h
d8307d
+++ b/sysdeps/nptl/fork.h
d8307d
@@ -52,9 +52,11 @@ enum __run_fork_handler_type
d8307d
    - atfork_run_child: run all the CHILD_HANDLER and unlocks the internal
d8307d
 		       lock.
d8307d
    - atfork_run_parent: run all the PARENT_HANDLER and unlocks the internal
d8307d
-			lock.  */
d8307d
-extern void __run_fork_handlers (enum __run_fork_handler_type who)
d8307d
-  attribute_hidden;
d8307d
+			lock.
d8307d
+
d8307d
+   Perform locking only if DO_LOCKING.  */
d8307d
+extern void __run_fork_handlers (enum __run_fork_handler_type who,
d8307d
+				 _Bool do_locking) attribute_hidden;
d8307d
 
d8307d
 /* C library side function to register new fork handlers.  */
d8307d
 extern int __register_atfork (void (*__prepare) (void),