fork.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /* System specific fork hooks. Linux version.
  2. Copyright (C) 2021-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #ifndef _FORK_H
  16. #define _FORK_H
  17. #include <assert.h>
  18. #include <kernel-posix-timers.h>
  19. #include <ldsodefs.h>
  20. #include <list.h>
  21. #include <mqueue.h>
  22. #include <pthreadP.h>
  23. #include <sysdep.h>
  24. #include <getrandom-internal.h>
  25. static inline void
  26. fork_system_setup (void)
  27. {
  28. /* See __pthread_once. */
  29. __fork_generation += __PTHREAD_ONCE_FORK_GEN_INCR;
  30. }
  31. static void
  32. fork_system_setup_after_fork (void)
  33. {
  34. /* There is one thread running. */
  35. __nptl_nthreads = 1;
  36. /* Initialize thread library locks. */
  37. GL (dl_stack_cache_lock) = LLL_LOCK_INITIALIZER;
  38. __default_pthread_attr_lock = LLL_LOCK_INITIALIZER;
  39. call_function_static_weak (__mq_notify_fork_subprocess);
  40. call_function_static_weak (__timer_fork_subprocess);
  41. call_function_static_weak (__getrandom_fork_subprocess);
  42. }
  43. /* In case of a fork() call the memory allocation in the child will be
  44. the same but only one thread is running. All stacks except that of
  45. the one running thread are not used anymore. We have to recycle
  46. them. */
  47. static void
  48. reclaim_stacks (void)
  49. {
  50. struct pthread *self = (struct pthread *) THREAD_SELF;
  51. /* No locking necessary. The caller is the only stack in use. But
  52. we have to be aware that we might have interrupted a list
  53. operation. */
  54. if (GL (dl_in_flight_stack) != 0)
  55. {
  56. bool add_p = GL (dl_in_flight_stack) & 1;
  57. list_t *elem = (list_t *) (GL (dl_in_flight_stack) & ~(uintptr_t) 1);
  58. if (add_p)
  59. {
  60. /* We always add at the beginning of the list. So in this case we
  61. only need to check the beginning of these lists to see if the
  62. pointers at the head of the list are inconsistent. */
  63. list_t *l = NULL;
  64. if (GL (dl_stack_used).next->prev != &GL (dl_stack_used))
  65. l = &GL (dl_stack_used);
  66. else if (GL (dl_stack_cache).next->prev != &GL (dl_stack_cache))
  67. l = &GL (dl_stack_cache);
  68. if (l != NULL)
  69. {
  70. assert (l->next->prev == elem);
  71. elem->next = l->next;
  72. elem->prev = l;
  73. l->next = elem;
  74. }
  75. }
  76. else
  77. {
  78. /* We can simply always replay the delete operation. */
  79. elem->next->prev = elem->prev;
  80. elem->prev->next = elem->next;
  81. }
  82. GL (dl_in_flight_stack) = 0;
  83. }
  84. /* Mark all stacks except the still running one as free. */
  85. list_t *runp;
  86. list_for_each (runp, &GL (dl_stack_used))
  87. {
  88. struct pthread *curp = list_entry (runp, struct pthread, list);
  89. if (curp != self)
  90. {
  91. /* This marks the stack as free. */
  92. curp->tid = 0;
  93. /* Account for the size of the stack. */
  94. GL (dl_stack_cache_actsize) += curp->stackblock_size;
  95. if (curp->specific_used)
  96. {
  97. /* Clear the thread-specific data. */
  98. memset (curp->specific_1stblock, '\0',
  99. sizeof (curp->specific_1stblock));
  100. curp->specific_used = false;
  101. for (size_t cnt = 1; cnt < PTHREAD_KEY_1STLEVEL_SIZE; ++cnt)
  102. if (curp->specific[cnt] != NULL)
  103. {
  104. memset (curp->specific[cnt], '\0',
  105. sizeof (curp->specific_1stblock));
  106. /* We have allocated the block which we do not
  107. free here so re-set the bit. */
  108. curp->specific_used = true;
  109. }
  110. }
  111. call_function_static_weak (__getrandom_reset_state, curp);
  112. }
  113. }
  114. /* Also reset stale getrandom states for user stack threads. */
  115. list_for_each (runp, &GL (dl_stack_user))
  116. {
  117. struct pthread *curp = list_entry (runp, struct pthread, list);
  118. if (curp != self)
  119. call_function_static_weak (__getrandom_reset_state, curp);
  120. }
  121. /* Add the stack of all running threads to the cache. */
  122. list_splice (&GL (dl_stack_used), &GL (dl_stack_cache));
  123. /* Remove the entry for the current thread to from the cache list
  124. and add it to the list of running threads. Which of the two
  125. lists is decided by the user_stack flag. */
  126. list_del (&self->list);
  127. /* Re-initialize the lists for all the threads. */
  128. INIT_LIST_HEAD (&GL (dl_stack_used));
  129. INIT_LIST_HEAD (&GL (dl_stack_user));
  130. if (__glibc_unlikely (THREAD_GETMEM (self, stack_mode)
  131. == ALLOCATE_GUARD_USER))
  132. list_add (&self->list, &GL (dl_stack_user));
  133. else
  134. list_add (&self->list, &GL (dl_stack_used));
  135. }
  136. #endif