fa5b62
This patch is a RHEL-8.7 backport of the following upstream commit:
fa5b62
fa5b62
commit 52a103e237329b9f88a28513fe7506ffc3bd8ced
fa5b62
Author: Arjun Shankar <arjun@redhat.com>
fa5b62
Date:   Tue May 24 17:57:36 2022 +0200
fa5b62
fa5b62
    Fix deadlock when pthread_atfork handler calls pthread_atfork or dlclose
fa5b62
    
fa5b62
    In multi-threaded programs, registering via pthread_atfork,
fa5b62
    de-registering implicitly via dlclose, or running pthread_atfork
fa5b62
    handlers during fork was protected by an internal lock.  This meant
fa5b62
    that a pthread_atfork handler attempting to register another handler or
fa5b62
    dlclose a dynamically loaded library would lead to a deadlock.
fa5b62
    
fa5b62
    This commit fixes the deadlock in the following way:
fa5b62
    
fa5b62
    During the execution of handlers at fork time, the atfork lock is
fa5b62
    released prior to the execution of each handler and taken again upon its
fa5b62
    return.  Any handler registrations or de-registrations that occurred
fa5b62
    during the execution of the handler are accounted for before proceeding
fa5b62
    with further handler execution.
fa5b62
    
fa5b62
    If a handler that hasn't been executed yet gets de-registered by another
fa5b62
    handler during fork, it will not be executed.   If a handler gets
fa5b62
    registered by another handler during fork, it will not be executed
fa5b62
    during that particular fork.
fa5b62
    
fa5b62
    The possibility that handlers may now be registered or deregistered
fa5b62
    during handler execution means that identifying the next handler to be
fa5b62
    run after a given handler may register/de-register others requires some
fa5b62
    bookkeeping.  The fork_handler struct has an additional field, 'id',
fa5b62
    which is assigned sequentially during registration.  Thus, handlers are
fa5b62
    executed in ascending order of 'id' during 'prepare', and descending
fa5b62
    order of 'id' during parent/child handler execution after the fork.
fa5b62
    
fa5b62
    Two tests are included:
fa5b62
    
fa5b62
    * tst-atfork3: Adhemerval Zanella <adhemerval.zanella@linaro.org>
fa5b62
      This test exercises calling dlclose from prepare, parent, and child
fa5b62
      handlers.
fa5b62
    
fa5b62
    * tst-atfork4: This test exercises calling pthread_atfork and dlclose
fa5b62
      from the prepare handler.
fa5b62
    
fa5b62
    [BZ #24595, BZ #27054]
fa5b62
    
fa5b62
    Co-authored-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
fa5b62
    Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>
fa5b62
fa5b62
diff --git a/nptl/Makefile b/nptl/Makefile
fa5b62
index 70a3be23ecfcd9c9..76c914e23e8873f2 100644
fa5b62
--- a/nptl/Makefile
fa5b62
+++ b/nptl/Makefile
fa5b62
@@ -382,8 +382,17 @@ tests += tst-cancelx2 tst-cancelx3 tst-cancelx4 tst-cancelx5 \
fa5b62
 	 tst-cancelx16 tst-cancelx17 tst-cancelx18 tst-cancelx20 tst-cancelx21 \
fa5b62
 	 tst-cleanupx0 tst-cleanupx1 tst-cleanupx2 tst-cleanupx3 tst-cleanupx4
fa5b62
 ifeq ($(build-shared),yes)
fa5b62
-tests += tst-atfork2 tst-tls4 tst-_res1 tst-fini1 tst-compat-forwarder \
fa5b62
-	 tst-audit-threads
fa5b62
+tests += \
fa5b62
+  tst-atfork2 \
fa5b62
+  tst-tls4 \
fa5b62
+  tst-_res1 \
fa5b62
+  tst-fini1 \
fa5b62
+  tst-compat-forwarder \
fa5b62
+  tst-audit-threads \
fa5b62
+  tst-atfork3 \
fa5b62
+  tst-atfork4 \
fa5b62
+# tests
fa5b62
+
fa5b62
 tests-internal += tst-tls3 tst-tls3-malloc tst-tls5 tst-stackguard1
fa5b62
 tests-nolibpthread += tst-fini1
fa5b62
 ifeq ($(have-z-execstack),yes)
fa5b62
@@ -391,18 +400,39 @@ tests += tst-execstack
fa5b62
 endif
fa5b62
 endif
fa5b62
 
fa5b62
-modules-names = tst-atfork2mod tst-tls3mod tst-tls4moda tst-tls4modb \
fa5b62
-		tst-tls5mod tst-tls5moda tst-tls5modb tst-tls5modc \
fa5b62
-		tst-tls5modd tst-tls5mode tst-tls5modf tst-stack4mod \
fa5b62
-		tst-_res1mod1 tst-_res1mod2 tst-execstack-mod tst-fini1mod \
fa5b62
-		tst-join7mod tst-compat-forwarder-mod tst-audit-threads-mod1 \
fa5b62
-		tst-audit-threads-mod2
fa5b62
+modules-names = \
fa5b62
+  tst-atfork2mod \
fa5b62
+  tst-tls3mod \
fa5b62
+  tst-tls4moda \
fa5b62
+  tst-tls4modb \
fa5b62
+  tst-tls5mod \
fa5b62
+  tst-tls5moda \
fa5b62
+  tst-tls5modb \
fa5b62
+  tst-tls5modc \
fa5b62
+  tst-tls5modd \
fa5b62
+  tst-tls5mode \
fa5b62
+  tst-tls5modf \
fa5b62
+  tst-stack4mod \
fa5b62
+  tst-_res1mod1 \
fa5b62
+  tst-_res1mod2 \
fa5b62
+  tst-execstack-mod \
fa5b62
+  tst-fini1mod \
fa5b62
+  tst-join7mod \
fa5b62
+  tst-compat-forwarder-mod \
fa5b62
+  tst-audit-threads-mod1 \
fa5b62
+  tst-audit-threads-mod2 \
fa5b62
+  tst-atfork3mod \
fa5b62
+  tst-atfork4mod \
fa5b62
+# module-names
fa5b62
+
fa5b62
 extra-test-objs += $(addsuffix .os,$(strip $(modules-names))) \
fa5b62
 		   tst-cleanup4aux.o tst-cleanupx4aux.o
fa5b62
 test-extras += tst-cleanup4aux tst-cleanupx4aux
fa5b62
 test-modules = $(addprefix $(objpfx),$(addsuffix .so,$(modules-names)))
fa5b62
 
fa5b62
 tst-atfork2mod.so-no-z-defs = yes
fa5b62
+tst-atfork3mod.so-no-z-defs = yes
fa5b62
+tst-atfork4mod.so-no-z-defs = yes
fa5b62
 tst-tls3mod.so-no-z-defs = yes
fa5b62
 tst-tls5mod.so-no-z-defs = yes
fa5b62
 tst-tls5moda.so-no-z-defs = yes
fa5b62
@@ -541,6 +571,14 @@ LDFLAGS-tst-atfork2 = -rdynamic
fa5b62
 tst-atfork2-ENV = MALLOC_TRACE=$(objpfx)tst-atfork2.mtrace
fa5b62
 $(objpfx)tst-atfork2mod.so: $(shared-thread-library)
fa5b62
 
fa5b62
+$(objpfx)tst-atfork3: $(libdl) $(shared-thread-library)
fa5b62
+LDFLAGS-tst-atfork3 = -rdynamic
fa5b62
+$(objpfx)tst-atfork3mod.so: $(shared-thread-library)
fa5b62
+
fa5b62
+$(objpfx)tst-atfork4: $(libdl) $(shared-thread-library)
fa5b62
+LDFLAGS-tst-atfork4 = -rdynamic
fa5b62
+$(objpfx)tst-atfork4mod.so: $(shared-thread-library)
fa5b62
+
fa5b62
 tst-stack3-ENV = MALLOC_TRACE=$(objpfx)tst-stack3.mtrace
fa5b62
 $(objpfx)tst-stack3-mem.out: $(objpfx)tst-stack3.out
fa5b62
 	$(common-objpfx)malloc/mtrace $(objpfx)tst-stack3.mtrace > $@; \
fa5b62
@@ -640,6 +678,8 @@ $(objpfx)../libc.so: $(common-objpfx)libc.so ;
fa5b62
 $(addprefix $(objpfx),$(tests-static) $(xtests-static)): $(objpfx)libpthread.a
fa5b62
 
fa5b62
 $(objpfx)tst-atfork2.out: $(objpfx)tst-atfork2mod.so
fa5b62
+$(objpfx)tst-atfork3.out: $(objpfx)tst-atfork3mod.so
fa5b62
+$(objpfx)tst-atfork4.out: $(objpfx)tst-atfork4mod.so
fa5b62
 else
fa5b62
 $(addprefix $(objpfx),$(tests) $(test-srcs)): $(objpfx)libpthread.a
fa5b62
 endif
fa5b62
diff --git a/nptl/register-atfork.c b/nptl/register-atfork.c
fa5b62
index 9edb7d4bbb49fbed..4c1e20ae8cab005f 100644
fa5b62
--- a/nptl/register-atfork.c
fa5b62
+++ b/nptl/register-atfork.c
fa5b62
@@ -21,6 +21,8 @@
fa5b62
 #include <string.h>
fa5b62
 #include <fork.h>
fa5b62
 #include <atomic.h>
fa5b62
+#include <intprops.h>
fa5b62
+#include <stdio.h>
fa5b62
 
fa5b62
 #define DYNARRAY_ELEMENT           struct fork_handler
fa5b62
 #define DYNARRAY_STRUCT            fork_handler_list
fa5b62
@@ -29,7 +31,7 @@
fa5b62
 #include <malloc/dynarray-skeleton.c>
fa5b62
 
fa5b62
 static struct fork_handler_list fork_handlers;
fa5b62
-static bool fork_handler_init = false;
fa5b62
+static uint64_t fork_handler_counter;
fa5b62
 
fa5b62
 static int atfork_lock = LLL_LOCK_INITIALIZER;
fa5b62
 
fa5b62
@@ -39,11 +41,8 @@ __register_atfork (void (*prepare) (void), void (*parent) (void),
fa5b62
 {
fa5b62
   lll_lock (atfork_lock, LLL_PRIVATE);
fa5b62
 
fa5b62
-  if (!fork_handler_init)
fa5b62
-    {
fa5b62
-      fork_handler_list_init (&fork_handlers);
fa5b62
-      fork_handler_init = true;
fa5b62
-    }
fa5b62
+  if (fork_handler_counter == 0)
fa5b62
+    fork_handler_list_init (&fork_handlers);
fa5b62
 
fa5b62
   struct fork_handler *newp = fork_handler_list_emplace (&fork_handlers);
fa5b62
   if (newp != NULL)
fa5b62
@@ -52,6 +51,13 @@ __register_atfork (void (*prepare) (void), void (*parent) (void),
fa5b62
       newp->parent_handler = parent;
fa5b62
       newp->child_handler = child;
fa5b62
       newp->dso_handle = dso_handle;
fa5b62
+
fa5b62
+      /* IDs assigned to handlers start at 1 and increment with handler
fa5b62
+         registration.  Un-registering a handlers discards the corresponding
fa5b62
+         ID.  It is not reused in future registrations.  */
fa5b62
+      if (INT_ADD_OVERFLOW (fork_handler_counter, 1))
fa5b62
+        __libc_fatal ("fork handler counter overflow");
fa5b62
+      newp->id = ++fork_handler_counter;
fa5b62
     }
fa5b62
 
fa5b62
   /* Release the lock.  */
fa5b62
@@ -106,37 +112,111 @@ __unregister_atfork (void *dso_handle)
fa5b62
   lll_unlock (atfork_lock, LLL_PRIVATE);
fa5b62
 }
fa5b62
 
fa5b62
-void
fa5b62
-__run_fork_handlers (enum __run_fork_handler_type who, _Bool do_locking)
fa5b62
+uint64_t
fa5b62
+__run_prefork_handlers (_Bool do_locking)
fa5b62
 {
fa5b62
-  struct fork_handler *runp;
fa5b62
+  uint64_t lastrun;
fa5b62
 
fa5b62
-  if (who == atfork_run_prepare)
fa5b62
+  if (do_locking)
fa5b62
+    lll_lock (atfork_lock, LLL_PRIVATE);
fa5b62
+
fa5b62
+  /* We run prepare handlers from last to first.  After fork, only
fa5b62
+     handlers up to the last handler found here (pre-fork) will be run.
fa5b62
+     Handlers registered during __run_prefork_handlers or
fa5b62
+     __run_postfork_handlers will be positioned after this last handler, and
fa5b62
+     since their prepare handlers won't be run now, their parent/child
fa5b62
+     handlers should also be ignored.  */
fa5b62
+  lastrun = fork_handler_counter;
fa5b62
+
fa5b62
+  size_t sl = fork_handler_list_size (&fork_handlers);
fa5b62
+  for (size_t i = sl; i > 0;)
fa5b62
     {
fa5b62
-      if (do_locking)
fa5b62
-	lll_lock (atfork_lock, LLL_PRIVATE);
fa5b62
-      size_t sl = fork_handler_list_size (&fork_handlers);
fa5b62
-      for (size_t i = sl; i > 0; i--)
fa5b62
-	{
fa5b62
-	  runp = fork_handler_list_at (&fork_handlers, i - 1);
fa5b62
-	  if (runp->prepare_handler != NULL)
fa5b62
-	    runp->prepare_handler ();
fa5b62
-	}
fa5b62
+      struct fork_handler *runp
fa5b62
+        = fork_handler_list_at (&fork_handlers, i - 1);
fa5b62
+
fa5b62
+      uint64_t id = runp->id;
fa5b62
+
fa5b62
+      if (runp->prepare_handler != NULL)
fa5b62
+        {
fa5b62
+          if (do_locking)
fa5b62
+            lll_unlock (atfork_lock, LLL_PRIVATE);
fa5b62
+
fa5b62
+          runp->prepare_handler ();
fa5b62
+
fa5b62
+          if (do_locking)
fa5b62
+            lll_lock (atfork_lock, LLL_PRIVATE);
fa5b62
+        }
fa5b62
+
fa5b62
+      /* We unlocked, ran the handler, and locked again.  In the
fa5b62
+         meanwhile, one or more deregistrations could have occurred leading
fa5b62
+         to the current (just run) handler being moved up the list or even
fa5b62
+         removed from the list itself.  Since handler IDs are guaranteed to
fa5b62
+         to be in increasing order, the next handler has to have:  */
fa5b62
+
fa5b62
+      /* A. An earlier position than the current one has.  */
fa5b62
+      i--;
fa5b62
+
fa5b62
+      /* B. A lower ID than the current one does.  The code below skips
fa5b62
+         any newly added handlers with higher IDs.  */
fa5b62
+      while (i > 0
fa5b62
+             && fork_handler_list_at (&fork_handlers, i - 1)->id >= id)
fa5b62
+        i--;
fa5b62
     }
fa5b62
-  else
fa5b62
+
fa5b62
+  return lastrun;
fa5b62
+}
fa5b62
+
fa5b62
+void
fa5b62
+__run_postfork_handlers (enum __run_fork_handler_type who, _Bool do_locking,
fa5b62
+                         uint64_t lastrun)
fa5b62
+{
fa5b62
+  size_t sl = fork_handler_list_size (&fork_handlers);
fa5b62
+  for (size_t i = 0; i < sl;)
fa5b62
     {
fa5b62
-      size_t sl = fork_handler_list_size (&fork_handlers);
fa5b62
-      for (size_t i = 0; i < sl; i++)
fa5b62
-	{
fa5b62
-	  runp = fork_handler_list_at (&fork_handlers, i);
fa5b62
-	  if (who == atfork_run_child && runp->child_handler)
fa5b62
-	    runp->child_handler ();
fa5b62
-	  else if (who == atfork_run_parent && runp->parent_handler)
fa5b62
-	    runp->parent_handler ();
fa5b62
-	}
fa5b62
+      struct fork_handler *runp = fork_handler_list_at (&fork_handlers, i);
fa5b62
+      uint64_t id = runp->id;
fa5b62
+
fa5b62
+      /* prepare handlers were not run for handlers with ID > LASTRUN.
fa5b62
+         Thus, parent/child handlers will also not be run.  */
fa5b62
+      if (id > lastrun)
fa5b62
+        break;
fa5b62
+
fa5b62
       if (do_locking)
fa5b62
-	lll_unlock (atfork_lock, LLL_PRIVATE);
fa5b62
+        lll_unlock (atfork_lock, LLL_PRIVATE);
fa5b62
+
fa5b62
+      if (who == atfork_run_child && runp->child_handler)
fa5b62
+        runp->child_handler ();
fa5b62
+      else if (who == atfork_run_parent && runp->parent_handler)
fa5b62
+        runp->parent_handler ();
fa5b62
+
fa5b62
+      if (do_locking)
fa5b62
+        lll_lock (atfork_lock, LLL_PRIVATE);
fa5b62
+
fa5b62
+      /* We unlocked, ran the handler, and locked again.  In the meanwhile,
fa5b62
+         one or more [de]registrations could have occurred.  Due to this,
fa5b62
+         the list size must be updated.  */
fa5b62
+      sl = fork_handler_list_size (&fork_handlers);
fa5b62
+
fa5b62
+      /* The just-run handler could also have moved up the list. */
fa5b62
+
fa5b62
+      if (sl > i && fork_handler_list_at (&fork_handlers, i)->id == id)
fa5b62
+        /* The position of the recently run handler hasn't changed.  The
fa5b62
+           next handler to be run is an easy increment away.  */
fa5b62
+        i++;
fa5b62
+      else
fa5b62
+        {
fa5b62
+          /* The next handler to be run is the first handler in the list
fa5b62
+             to have an ID higher than the current one.  */
fa5b62
+          for (i = 0; i < sl; i++)
fa5b62
+            {
fa5b62
+              if (fork_handler_list_at (&fork_handlers, i)->id > id)
fa5b62
+                break;
fa5b62
+            }
fa5b62
+        }
fa5b62
     }
fa5b62
+
fa5b62
+  if (do_locking)
fa5b62
+    lll_unlock (atfork_lock, LLL_PRIVATE);
fa5b62
 }
fa5b62
 
fa5b62
 
fa5b62
diff --git a/nptl/tst-atfork3.c b/nptl/tst-atfork3.c
fa5b62
new file mode 100644
fa5b62
index 0000000000000000..bb2250e432ab79ad
fa5b62
--- /dev/null
fa5b62
+++ b/nptl/tst-atfork3.c
fa5b62
@@ -0,0 +1,118 @@
fa5b62
+/* Check if pthread_atfork handler can call dlclose (BZ#24595).
fa5b62
+   Copyright (C) 2022 Free Software Foundation, Inc.
fa5b62
+   This file is part of the GNU C Library.
fa5b62
+
fa5b62
+   The GNU C Library is free software; you can redistribute it and/or
fa5b62
+   modify it under the terms of the GNU Lesser General Public
fa5b62
+   License as published by the Free Software Foundation; either
fa5b62
+   version 2.1 of the License, or (at your option) any later version.
fa5b62
+
fa5b62
+   The GNU C Library is distributed in the hope that it will be useful,
fa5b62
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
fa5b62
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
fa5b62
+   Lesser General Public License for more details.
fa5b62
+
fa5b62
+   You should have received a copy of the GNU Lesser General Public
fa5b62
+   License along with the GNU C Library; if not, see
fa5b62
+   <http://www.gnu.org/licenses/>.  */
fa5b62
+
fa5b62
+#include <stdio.h>
fa5b62
+#include <pthread.h>
fa5b62
+#include <unistd.h>
fa5b62
+#include <stdlib.h>
fa5b62
+#include <stdbool.h>
fa5b62
+
fa5b62
+#include <support/check.h>
fa5b62
+#include <support/xthread.h>
fa5b62
+#include <support/capture_subprocess.h>
fa5b62
+#include <support/xdlfcn.h>
fa5b62
+
fa5b62
+/* Check if pthread_atfork handlers do not deadlock when calling a function
fa5b62
+   that might alter the internal fork handle list, such as dlclose.
fa5b62
+
fa5b62
+   The test registers a callback set with pthread_atfork(), dlopen() a shared
fa5b62
+   library (nptl/tst-atfork3mod.c), calls an exported symbol from the library
fa5b62
+   (which in turn also registers atfork handlers), and calls fork to trigger
fa5b62
+   the callbacks.  */
fa5b62
+
fa5b62
+static void *handler;
fa5b62
+static bool run_dlclose_prepare;
fa5b62
+static bool run_dlclose_parent;
fa5b62
+static bool run_dlclose_child;
fa5b62
+
fa5b62
+static void
fa5b62
+prepare (void)
fa5b62
+{
fa5b62
+  if (run_dlclose_prepare)
fa5b62
+    xdlclose (handler);
fa5b62
+}
fa5b62
+
fa5b62
+static void
fa5b62
+parent (void)
fa5b62
+{
fa5b62
+  if (run_dlclose_parent)
fa5b62
+    xdlclose (handler);
fa5b62
+}
fa5b62
+
fa5b62
+static void
fa5b62
+child (void)
fa5b62
+{
fa5b62
+  if (run_dlclose_child)
fa5b62
+    xdlclose (handler);
fa5b62
+}
fa5b62
+
fa5b62
+static void
fa5b62
+proc_func (void *closure)
fa5b62
+{
fa5b62
+}
fa5b62
+
fa5b62
+static void
fa5b62
+do_test_generic (bool dlclose_prepare, bool dlclose_parent, bool dlclose_child)
fa5b62
+{
fa5b62
+  run_dlclose_prepare = dlclose_prepare;
fa5b62
+  run_dlclose_parent = dlclose_parent;
fa5b62
+  run_dlclose_child = dlclose_child;
fa5b62
+
fa5b62
+  handler = xdlopen ("tst-atfork3mod.so", RTLD_NOW);
fa5b62
+
fa5b62
+  int (*atfork3mod_func)(void);
fa5b62
+  atfork3mod_func = xdlsym (handler, "atfork3mod_func");
fa5b62
+
fa5b62
+  atfork3mod_func ();
fa5b62
+
fa5b62
+  struct support_capture_subprocess proc
fa5b62
+    = support_capture_subprocess (proc_func, NULL);
fa5b62
+  support_capture_subprocess_check (&proc, "tst-atfork3", 0, sc_allow_none);
fa5b62
+
fa5b62
+  handler = atfork3mod_func = NULL;
fa5b62
+
fa5b62
+  support_capture_subprocess_free (&proc;;
fa5b62
+}
fa5b62
+
fa5b62
+static void *
fa5b62
+thread_func (void *closure)
fa5b62
+{
fa5b62
+  return NULL;
fa5b62
+}
fa5b62
+
fa5b62
+static int
fa5b62
+do_test (void)
fa5b62
+{
fa5b62
+  {
fa5b62
+    /* Make the process acts as multithread.  */
fa5b62
+    pthread_attr_t attr;
fa5b62
+    xpthread_attr_init (&attr);
fa5b62
+    xpthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
fa5b62
+    xpthread_create (&attr, thread_func, NULL);
fa5b62
+  }
fa5b62
+
fa5b62
+  TEST_COMPARE (pthread_atfork (prepare, parent, child), 0);
fa5b62
+
fa5b62
+  do_test_generic (true  /* prepare */, false /* parent */, false /* child */);
fa5b62
+  do_test_generic (false /* prepare */, true  /* parent */, false /* child */);
fa5b62
+  do_test_generic (false /* prepare */, false /* parent */, true  /* child */);
fa5b62
+
fa5b62
+  return 0;
fa5b62
+}
fa5b62
+
fa5b62
+#include <support/test-driver.c>
fa5b62
diff --git a/nptl/tst-atfork3mod.c b/nptl/tst-atfork3mod.c
fa5b62
new file mode 100644
fa5b62
index 0000000000000000..6d0658cb9efdecbc
fa5b62
--- /dev/null
fa5b62
+++ b/nptl/tst-atfork3mod.c
fa5b62
@@ -0,0 +1,44 @@
fa5b62
+/* Copyright (C) 2022 Free Software Foundation, Inc.
fa5b62
+   This file is part of the GNU C Library.
fa5b62
+
fa5b62
+   The GNU C Library is free software; you can redistribute it and/or
fa5b62
+   modify it under the terms of the GNU Lesser General Public
fa5b62
+   License as published by the Free Software Foundation; either
fa5b62
+   version 2.1 of the License, or (at your option) any later version.
fa5b62
+
fa5b62
+   The GNU C Library is distributed in the hope that it will be useful,
fa5b62
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
fa5b62
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
fa5b62
+   Lesser General Public License for more details.
fa5b62
+
fa5b62
+   You should have received a copy of the GNU Lesser General Public
fa5b62
+   License along with the GNU C Library; if not, see
fa5b62
+   <http://www.gnu.org/licenses/>.  */
fa5b62
+
fa5b62
+#include <unistd.h>
fa5b62
+#include <stdlib.h>
fa5b62
+#include <pthread.h>
fa5b62
+
fa5b62
+#include <support/check.h>
fa5b62
+
fa5b62
+static void
fa5b62
+mod_prepare (void)
fa5b62
+{
fa5b62
+}
fa5b62
+
fa5b62
+static void
fa5b62
+mod_parent (void)
fa5b62
+{
fa5b62
+}
fa5b62
+
fa5b62
+static void
fa5b62
+mod_child (void)
fa5b62
+{
fa5b62
+}
fa5b62
+
fa5b62
+int atfork3mod_func (void)
fa5b62
+{
fa5b62
+  TEST_COMPARE (pthread_atfork (mod_prepare, mod_parent, mod_child), 0);
fa5b62
+
fa5b62
+  return 0;
fa5b62
+}
fa5b62
diff --git a/nptl/tst-atfork4.c b/nptl/tst-atfork4.c
fa5b62
new file mode 100644
fa5b62
index 0000000000000000..52dc87e73b846ab9
fa5b62
--- /dev/null
fa5b62
+++ b/nptl/tst-atfork4.c
fa5b62
@@ -0,0 +1,128 @@
fa5b62
+/* pthread_atfork supports handlers that call pthread_atfork or dlclose.
fa5b62
+   Copyright (C) 2022 Free Software Foundation, Inc.
fa5b62
+   This file is part of the GNU C Library.
fa5b62
+
fa5b62
+   The GNU C Library is free software; you can redistribute it and/or
fa5b62
+   modify it under the terms of the GNU Lesser General Public
fa5b62
+   License as published by the Free Software Foundation; either
fa5b62
+   version 2.1 of the License, or (at your option) any later version.
fa5b62
+
fa5b62
+   The GNU C Library is distributed in the hope that it will be useful,
fa5b62
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
fa5b62
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
fa5b62
+   Lesser General Public License for more details.
fa5b62
+
fa5b62
+   You should have received a copy of the GNU Lesser General Public
fa5b62
+   License along with the GNU C Library; if not, see
fa5b62
+   <https://www.gnu.org/licenses/>.  */
fa5b62
+
fa5b62
+#include <support/xdlfcn.h>
fa5b62
+#include <stdio.h>
fa5b62
+#include <support/xthread.h>
fa5b62
+#include <sys/types.h>
fa5b62
+#include <sys/wait.h>
fa5b62
+#include <support/xunistd.h>
fa5b62
+#include <support/check.h>
fa5b62
+#include <stdlib.h>
fa5b62
+
fa5b62
+static void *
fa5b62
+thread_func (void *x)
fa5b62
+{
fa5b62
+  return NULL;
fa5b62
+}
fa5b62
+
fa5b62
+static unsigned int second_atfork_handler_runcount = 0;
fa5b62
+
fa5b62
+static void
fa5b62
+second_atfork_handler (void)
fa5b62
+{
fa5b62
+  second_atfork_handler_runcount++;
fa5b62
+}
fa5b62
+
fa5b62
+static void *h = NULL;
fa5b62
+
fa5b62
+static unsigned int atfork_handler_runcount = 0;
fa5b62
+
fa5b62
+static void
fa5b62
+prepare (void)
fa5b62
+{
fa5b62
+  /* These atfork handlers are registered while atfork handlers are being
fa5b62
+     executed and thus will not be executed during the corresponding
fa5b62
+     fork.  */
fa5b62
+  TEST_VERIFY_EXIT (pthread_atfork (second_atfork_handler,
fa5b62
+                                    second_atfork_handler,
fa5b62
+                                    second_atfork_handler) == 0);
fa5b62
+
fa5b62
+  /* This will de-register the atfork handlers registered by the dlopen'd
fa5b62
+     library and so they will not be executed.  */
fa5b62
+  if (h != NULL)
fa5b62
+    {
fa5b62
+      xdlclose (h);
fa5b62
+      h = NULL;
fa5b62
+    }
fa5b62
+
fa5b62
+  atfork_handler_runcount++;
fa5b62
+}
fa5b62
+
fa5b62
+static void
fa5b62
+after (void)
fa5b62
+{
fa5b62
+  atfork_handler_runcount++;
fa5b62
+}
fa5b62
+
fa5b62
+static int
fa5b62
+do_test (void)
fa5b62
+{
fa5b62
+  /* Make sure __libc_single_threaded is 0.  */
fa5b62
+  pthread_attr_t attr;
fa5b62
+  xpthread_attr_init (&attr);
fa5b62
+  xpthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
fa5b62
+  xpthread_create (&attr, thread_func, NULL);
fa5b62
+
fa5b62
+  void (*reg_atfork_handlers) (void);
fa5b62
+
fa5b62
+  h = xdlopen ("tst-atfork4mod.so", RTLD_LAZY);
fa5b62
+
fa5b62
+  reg_atfork_handlers = xdlsym (h, "reg_atfork_handlers");
fa5b62
+
fa5b62
+  reg_atfork_handlers ();
fa5b62
+
fa5b62
+  /* We register our atfork handlers *after* loading the module so that our
fa5b62
+     prepare handler is called first at fork, where we then dlclose the
fa5b62
+     module before its prepare handler has a chance to be called.  */
fa5b62
+  TEST_VERIFY_EXIT (pthread_atfork (prepare, after, after) == 0);
fa5b62
+
fa5b62
+  pid_t pid = xfork ();
fa5b62
+
fa5b62
+  /* Both the parent and the child processes should observe this.  */
fa5b62
+  TEST_VERIFY_EXIT (atfork_handler_runcount == 2);
fa5b62
+  TEST_VERIFY_EXIT (second_atfork_handler_runcount == 0);
fa5b62
+
fa5b62
+  if (pid > 0)
fa5b62
+    {
fa5b62
+      int childstat;
fa5b62
+
fa5b62
+      xwaitpid (-1, &childstat, 0);
fa5b62
+      TEST_VERIFY_EXIT (WIFEXITED (childstat)
fa5b62
+                        && WEXITSTATUS (childstat) == 0);
fa5b62
+
fa5b62
+      /* This time, the second set of atfork handlers should also be called
fa5b62
+         since the handlers are already in place before fork is called.  */
fa5b62
+
fa5b62
+      pid = xfork ();
fa5b62
+
fa5b62
+      TEST_VERIFY_EXIT (atfork_handler_runcount == 4);
fa5b62
+      TEST_VERIFY_EXIT (second_atfork_handler_runcount == 2);
fa5b62
+
fa5b62
+      if (pid > 0)
fa5b62
+        {
fa5b62
+          xwaitpid (-1, &childstat, 0);
fa5b62
+          TEST_VERIFY_EXIT (WIFEXITED (childstat)
fa5b62
+                            && WEXITSTATUS (childstat) == 0);
fa5b62
+        }
fa5b62
+    }
fa5b62
+
fa5b62
+  return 0;
fa5b62
+}
fa5b62
+
fa5b62
+#include <support/test-driver.c>
fa5b62
diff --git a/nptl/tst-atfork4mod.c b/nptl/tst-atfork4mod.c
fa5b62
new file mode 100644
fa5b62
index 0000000000000000..e111efeb185916e0
fa5b62
--- /dev/null
fa5b62
+++ b/nptl/tst-atfork4mod.c
fa5b62
@@ -0,0 +1,48 @@
fa5b62
+/* pthread_atfork supports handlers that call pthread_atfork or dlclose.
fa5b62
+   Copyright (C) 2022 Free Software Foundation, Inc.
fa5b62
+   This file is part of the GNU C Library.
fa5b62
+
fa5b62
+   The GNU C Library is free software; you can redistribute it and/or
fa5b62
+   modify it under the terms of the GNU Lesser General Public
fa5b62
+   License as published by the Free Software Foundation; either
fa5b62
+   version 2.1 of the License, or (at your option) any later version.
fa5b62
+
fa5b62
+   The GNU C Library is distributed in the hope that it will be useful,
fa5b62
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
fa5b62
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
fa5b62
+   Lesser General Public License for more details.
fa5b62
+
fa5b62
+   You should have received a copy of the GNU Lesser General Public
fa5b62
+   License along with the GNU C Library; if not, see
fa5b62
+   <https://www.gnu.org/licenses/>.  */
fa5b62
+
fa5b62
+#include <pthread.h>
fa5b62
+#include <stdlib.h>
fa5b62
+
fa5b62
+/* This dynamically loaded library simply registers its atfork handlers when
fa5b62
+   asked to.  The atfork handlers should never be executed because the
fa5b62
+   library is unloaded before fork is called by the test program.  */
fa5b62
+
fa5b62
+static void
fa5b62
+prepare (void)
fa5b62
+{
fa5b62
+  abort ();
fa5b62
+}
fa5b62
+
fa5b62
+static void
fa5b62
+parent (void)
fa5b62
+{
fa5b62
+  abort ();
fa5b62
+}
fa5b62
+
fa5b62
+static void
fa5b62
+child (void)
fa5b62
+{
fa5b62
+  abort ();
fa5b62
+}
fa5b62
+
fa5b62
+void
fa5b62
+reg_atfork_handlers (void)
fa5b62
+{
fa5b62
+  pthread_atfork (prepare, parent, child);
fa5b62
+}
fa5b62
diff --git a/sysdeps/nptl/fork.c b/sysdeps/nptl/fork.c
fa5b62
index b4d20fa652f4ba3b..1324b813136764fc 100644
fa5b62
--- a/sysdeps/nptl/fork.c
fa5b62
+++ b/sysdeps/nptl/fork.c
fa5b62
@@ -54,8 +54,9 @@ __libc_fork (void)
fa5b62
      signal handlers.  POSIX requires that fork is async-signal-safe,
fa5b62
      but our current fork implementation is not.  */
fa5b62
   bool multiple_threads = THREAD_GETMEM (THREAD_SELF, header.multiple_threads);
fa5b62
+  uint64_t lastrun;
fa5b62
 
fa5b62
-  __run_fork_handlers (atfork_run_prepare, multiple_threads);
fa5b62
+  lastrun = __run_prefork_handlers (multiple_threads);
fa5b62
 
fa5b62
   /* If we are not running multiple threads, we do not have to
fa5b62
      preserve lock state.  If fork runs from a signal handler, only
fa5b62
@@ -129,7 +130,7 @@ __libc_fork (void)
fa5b62
       __rtld_lock_initialize (GL(dl_load_tls_lock));
fa5b62
 
fa5b62
       /* Run the handlers registered for the child.  */
fa5b62
-      __run_fork_handlers (atfork_run_child, multiple_threads);
fa5b62
+      __run_postfork_handlers (atfork_run_child, multiple_threads, lastrun);
fa5b62
     }
fa5b62
   else
fa5b62
     {
fa5b62
@@ -144,7 +145,7 @@ __libc_fork (void)
fa5b62
 	}
fa5b62
 
fa5b62
       /* Run the handlers registered for the parent.  */
fa5b62
-      __run_fork_handlers (atfork_run_parent, multiple_threads);
fa5b62
+      __run_postfork_handlers (atfork_run_parent, multiple_threads, lastrun);
fa5b62
     }
fa5b62
 
fa5b62
   return pid;
fa5b62
diff --git a/sysdeps/nptl/fork.h b/sysdeps/nptl/fork.h
fa5b62
index bef2b7a8a6af8635..222c4f618970a455 100644
fa5b62
--- a/sysdeps/nptl/fork.h
fa5b62
+++ b/sysdeps/nptl/fork.h
fa5b62
@@ -31,6 +31,7 @@ struct fork_handler
fa5b62
   void (*parent_handler) (void);
fa5b62
   void (*child_handler) (void);
fa5b62
   void *dso_handle;
fa5b62
+  uint64_t id;
fa5b62
 };
fa5b62
 
fa5b62
 /* Function to call to unregister fork handlers.  */
fa5b62
@@ -44,19 +45,18 @@ enum __run_fork_handler_type
fa5b62
   atfork_run_parent
fa5b62
 };
fa5b62
 
fa5b62
-/* Run the atfork handlers and lock/unlock the internal lock depending
fa5b62
-   of the WHO argument:
fa5b62
+/* Run the atfork prepare handlers in the reverse order of registration and
fa5b62
+   return the ID of the last registered handler.  If DO_LOCKING is true, the
fa5b62
+   internal lock is held locked upon return.  */
fa5b62
+extern uint64_t __run_prefork_handlers (_Bool do_locking) attribute_hidden;
fa5b62
 
fa5b62
-   - atfork_run_prepare: run all the PREPARE_HANDLER in reverse order of
fa5b62
-			 insertion and locks the internal lock.
fa5b62
-   - atfork_run_child: run all the CHILD_HANDLER and unlocks the internal
fa5b62
-		       lock.
fa5b62
-   - atfork_run_parent: run all the PARENT_HANDLER and unlocks the internal
fa5b62
-			lock.
fa5b62
-
fa5b62
-   Perform locking only if DO_LOCKING.  */
fa5b62
-extern void __run_fork_handlers (enum __run_fork_handler_type who,
fa5b62
-				 _Bool do_locking) attribute_hidden;
fa5b62
+/* Given a handler type (parent or child), run all the atfork handlers in
fa5b62
+   the order of registration up to and including the handler with id equal
fa5b62
+   to LASTRUN.  If DO_LOCKING is true, the internal lock is unlocked prior
fa5b62
+   to return.  */
fa5b62
+extern void __run_postfork_handlers (enum __run_fork_handler_type who,
fa5b62
+                                     _Bool do_locking,
fa5b62
+                                     uint64_t lastrun) attribute_hidden;
fa5b62
 
fa5b62
 /* C library side function to register new fork handlers.  */
fa5b62
 extern int __register_atfork (void (*__prepare) (void),