allocatestack.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318
  1. /* Copyright (C) 2002-2019 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
  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. <http://www.gnu.org/licenses/>. */
  15. #include <assert.h>
  16. #include <errno.h>
  17. #include <signal.h>
  18. #include <stdint.h>
  19. #include <string.h>
  20. #include <unistd.h>
  21. #include <sys/mman.h>
  22. #include <sys/param.h>
  23. #include <dl-sysdep.h>
  24. #include <dl-tls.h>
  25. #include <tls.h>
  26. #include <list.h>
  27. #include <lowlevellock.h>
  28. #include <futex-internal.h>
  29. #include <kernel-features.h>
  30. #include <stack-aliasing.h>
  31. #ifndef NEED_SEPARATE_REGISTER_STACK
  32. /* Most architectures have exactly one stack pointer. Some have more. */
  33. # define STACK_VARIABLES void *stackaddr = NULL
  34. /* How to pass the values to the 'create_thread' function. */
  35. # define STACK_VARIABLES_ARGS stackaddr
  36. /* How to declare function which gets there parameters. */
  37. # define STACK_VARIABLES_PARMS void *stackaddr
  38. /* How to declare allocate_stack. */
  39. # define ALLOCATE_STACK_PARMS void **stack
  40. /* This is how the function is called. We do it this way to allow
  41. other variants of the function to have more parameters. */
  42. # define ALLOCATE_STACK(attr, pd) allocate_stack (attr, pd, &stackaddr)
  43. #else
  44. /* We need two stacks. The kernel will place them but we have to tell
  45. the kernel about the size of the reserved address space. */
  46. # define STACK_VARIABLES void *stackaddr = NULL; size_t stacksize = 0
  47. /* How to pass the values to the 'create_thread' function. */
  48. # define STACK_VARIABLES_ARGS stackaddr, stacksize
  49. /* How to declare function which gets there parameters. */
  50. # define STACK_VARIABLES_PARMS void *stackaddr, size_t stacksize
  51. /* How to declare allocate_stack. */
  52. # define ALLOCATE_STACK_PARMS void **stack, size_t *stacksize
  53. /* This is how the function is called. We do it this way to allow
  54. other variants of the function to have more parameters. */
  55. # define ALLOCATE_STACK(attr, pd) \
  56. allocate_stack (attr, pd, &stackaddr, &stacksize)
  57. #endif
  58. /* Default alignment of stack. */
  59. #ifndef STACK_ALIGN
  60. # define STACK_ALIGN __alignof__ (long double)
  61. #endif
  62. /* Default value for minimal stack size after allocating thread
  63. descriptor and guard. */
  64. #ifndef MINIMAL_REST_STACK
  65. # define MINIMAL_REST_STACK 4096
  66. #endif
  67. /* Newer kernels have the MAP_STACK flag to indicate a mapping is used for
  68. a stack. Use it when possible. */
  69. #ifndef MAP_STACK
  70. # define MAP_STACK 0
  71. #endif
  72. /* This yields the pointer that TLS support code calls the thread pointer. */
  73. #if TLS_TCB_AT_TP
  74. # define TLS_TPADJ(pd) (pd)
  75. #elif TLS_DTV_AT_TP
  76. # define TLS_TPADJ(pd) ((struct pthread *)((char *) (pd) + TLS_PRE_TCB_SIZE))
  77. #endif
  78. /* Cache handling for not-yet free stacks. */
  79. /* Maximum size in kB of cache. */
  80. static size_t stack_cache_maxsize = 40 * 1024 * 1024; /* 40MiBi by default. */
  81. static size_t stack_cache_actsize;
  82. /* Mutex protecting this variable. */
  83. static int stack_cache_lock = LLL_LOCK_INITIALIZER;
  84. /* List of queued stack frames. */
  85. static LIST_HEAD (stack_cache);
  86. /* List of the stacks in use. */
  87. static LIST_HEAD (stack_used);
  88. /* We need to record what list operations we are going to do so that,
  89. in case of an asynchronous interruption due to a fork() call, we
  90. can correct for the work. */
  91. static uintptr_t in_flight_stack;
  92. /* List of the threads with user provided stacks in use. No need to
  93. initialize this, since it's done in __pthread_initialize_minimal. */
  94. list_t __stack_user __attribute__ ((nocommon));
  95. hidden_data_def (__stack_user)
  96. /* Check whether the stack is still used or not. */
  97. #define FREE_P(descr) ((descr)->tid <= 0)
  98. static void
  99. stack_list_del (list_t *elem)
  100. {
  101. in_flight_stack = (uintptr_t) elem;
  102. atomic_write_barrier ();
  103. list_del (elem);
  104. atomic_write_barrier ();
  105. in_flight_stack = 0;
  106. }
  107. static void
  108. stack_list_add (list_t *elem, list_t *list)
  109. {
  110. in_flight_stack = (uintptr_t) elem | 1;
  111. atomic_write_barrier ();
  112. list_add (elem, list);
  113. atomic_write_barrier ();
  114. in_flight_stack = 0;
  115. }
  116. /* We create a double linked list of all cache entries. Double linked
  117. because this allows removing entries from the end. */
  118. /* Get a stack frame from the cache. We have to match by size since
  119. some blocks might be too small or far too large. */
  120. static struct pthread *
  121. get_cached_stack (size_t *sizep, void **memp)
  122. {
  123. size_t size = *sizep;
  124. struct pthread *result = NULL;
  125. list_t *entry;
  126. lll_lock (stack_cache_lock, LLL_PRIVATE);
  127. /* Search the cache for a matching entry. We search for the
  128. smallest stack which has at least the required size. Note that
  129. in normal situations the size of all allocated stacks is the
  130. same. As the very least there are only a few different sizes.
  131. Therefore this loop will exit early most of the time with an
  132. exact match. */
  133. list_for_each (entry, &stack_cache)
  134. {
  135. struct pthread *curr;
  136. curr = list_entry (entry, struct pthread, list);
  137. if (FREE_P (curr) && curr->stackblock_size >= size)
  138. {
  139. if (curr->stackblock_size == size)
  140. {
  141. result = curr;
  142. break;
  143. }
  144. if (result == NULL
  145. || result->stackblock_size > curr->stackblock_size)
  146. result = curr;
  147. }
  148. }
  149. if (__builtin_expect (result == NULL, 0)
  150. /* Make sure the size difference is not too excessive. In that
  151. case we do not use the block. */
  152. || __builtin_expect (result->stackblock_size > 4 * size, 0))
  153. {
  154. /* Release the lock. */
  155. lll_unlock (stack_cache_lock, LLL_PRIVATE);
  156. return NULL;
  157. }
  158. /* Don't allow setxid until cloned. */
  159. result->setxid_futex = -1;
  160. /* Dequeue the entry. */
  161. stack_list_del (&result->list);
  162. /* And add to the list of stacks in use. */
  163. stack_list_add (&result->list, &stack_used);
  164. /* And decrease the cache size. */
  165. stack_cache_actsize -= result->stackblock_size;
  166. /* Release the lock early. */
  167. lll_unlock (stack_cache_lock, LLL_PRIVATE);
  168. /* Report size and location of the stack to the caller. */
  169. *sizep = result->stackblock_size;
  170. *memp = result->stackblock;
  171. /* Cancellation handling is back to the default. */
  172. result->cancelhandling = 0;
  173. result->cleanup = NULL;
  174. /* No pending event. */
  175. result->nextevent = NULL;
  176. /* Clear the DTV. */
  177. dtv_t *dtv = GET_DTV (TLS_TPADJ (result));
  178. for (size_t cnt = 0; cnt < dtv[-1].counter; ++cnt)
  179. free (dtv[1 + cnt].pointer.to_free);
  180. memset (dtv, '\0', (dtv[-1].counter + 1) * sizeof (dtv_t));
  181. /* Re-initialize the TLS. */
  182. _dl_allocate_tls_init (TLS_TPADJ (result));
  183. return result;
  184. }
  185. /* Free stacks until cache size is lower than LIMIT. */
  186. static void
  187. free_stacks (size_t limit)
  188. {
  189. /* We reduce the size of the cache. Remove the last entries until
  190. the size is below the limit. */
  191. list_t *entry;
  192. list_t *prev;
  193. /* Search from the end of the list. */
  194. list_for_each_prev_safe (entry, prev, &stack_cache)
  195. {
  196. struct pthread *curr;
  197. curr = list_entry (entry, struct pthread, list);
  198. if (FREE_P (curr))
  199. {
  200. /* Unlink the block. */
  201. stack_list_del (entry);
  202. /* Account for the freed memory. */
  203. stack_cache_actsize -= curr->stackblock_size;
  204. /* Free the memory associated with the ELF TLS. */
  205. _dl_deallocate_tls (TLS_TPADJ (curr), false);
  206. /* Remove this block. This should never fail. If it does
  207. something is really wrong. */
  208. if (__munmap (curr->stackblock, curr->stackblock_size) != 0)
  209. abort ();
  210. /* Maybe we have freed enough. */
  211. if (stack_cache_actsize <= limit)
  212. break;
  213. }
  214. }
  215. }
  216. /* Free all the stacks on cleanup. */
  217. void
  218. __nptl_stacks_freeres (void)
  219. {
  220. free_stacks (0);
  221. }
  222. /* Add a stack frame which is not used anymore to the stack. Must be
  223. called with the cache lock held. */
  224. static inline void
  225. __attribute ((always_inline))
  226. queue_stack (struct pthread *stack)
  227. {
  228. /* We unconditionally add the stack to the list. The memory may
  229. still be in use but it will not be reused until the kernel marks
  230. the stack as not used anymore. */
  231. stack_list_add (&stack->list, &stack_cache);
  232. stack_cache_actsize += stack->stackblock_size;
  233. if (__glibc_unlikely (stack_cache_actsize > stack_cache_maxsize))
  234. free_stacks (stack_cache_maxsize);
  235. }
  236. static int
  237. change_stack_perm (struct pthread *pd
  238. #ifdef NEED_SEPARATE_REGISTER_STACK
  239. , size_t pagemask
  240. #endif
  241. )
  242. {
  243. #ifdef NEED_SEPARATE_REGISTER_STACK
  244. void *stack = (pd->stackblock
  245. + (((((pd->stackblock_size - pd->guardsize) / 2)
  246. & pagemask) + pd->guardsize) & pagemask));
  247. size_t len = pd->stackblock + pd->stackblock_size - stack;
  248. #elif _STACK_GROWS_DOWN
  249. void *stack = pd->stackblock + pd->guardsize;
  250. size_t len = pd->stackblock_size - pd->guardsize;
  251. #elif _STACK_GROWS_UP
  252. void *stack = pd->stackblock;
  253. size_t len = (uintptr_t) pd - pd->guardsize - (uintptr_t) pd->stackblock;
  254. #else
  255. # error "Define either _STACK_GROWS_DOWN or _STACK_GROWS_UP"
  256. #endif
  257. if (__mprotect (stack, len, PROT_READ | PROT_WRITE | PROT_EXEC) != 0)
  258. return errno;
  259. return 0;
  260. }
  261. /* Return the guard page position on allocated stack. */
  262. static inline char *
  263. __attribute ((always_inline))
  264. guard_position (void *mem, size_t size, size_t guardsize, struct pthread *pd,
  265. size_t pagesize_m1)
  266. {
  267. #ifdef NEED_SEPARATE_REGISTER_STACK
  268. return mem + (((size - guardsize) / 2) & ~pagesize_m1);
  269. #elif _STACK_GROWS_DOWN
  270. return mem;
  271. #elif _STACK_GROWS_UP
  272. return (char *) (((uintptr_t) pd - guardsize) & ~pagesize_m1);
  273. #endif
  274. }
  275. /* Based on stack allocated with PROT_NONE, setup the required portions with
  276. 'prot' flags based on the guard page position. */
  277. static inline int
  278. setup_stack_prot (char *mem, size_t size, char *guard, size_t guardsize,
  279. const int prot)
  280. {
  281. char *guardend = guard + guardsize;
  282. #if _STACK_GROWS_DOWN && !defined(NEED_SEPARATE_REGISTER_STACK)
  283. /* As defined at guard_position, for architectures with downward stack
  284. the guard page is always at start of the allocated area. */
  285. if (__mprotect (guardend, size - guardsize, prot) != 0)
  286. return errno;
  287. #else
  288. size_t mprots1 = (uintptr_t) guard - (uintptr_t) mem;
  289. if (__mprotect (mem, mprots1, prot) != 0)
  290. return errno;
  291. size_t mprots2 = ((uintptr_t) mem + size) - (uintptr_t) guardend;
  292. if (__mprotect (guardend, mprots2, prot) != 0)
  293. return errno;
  294. #endif
  295. return 0;
  296. }
  297. /* Mark the memory of the stack as usable to the kernel. It frees everything
  298. except for the space used for the TCB itself. */
  299. static inline void
  300. __always_inline
  301. advise_stack_range (void *mem, size_t size, uintptr_t pd, size_t guardsize)
  302. {
  303. uintptr_t sp = (uintptr_t) CURRENT_STACK_FRAME;
  304. size_t pagesize_m1 = __getpagesize () - 1;
  305. #if _STACK_GROWS_DOWN && !defined(NEED_SEPARATE_REGISTER_STACK)
  306. size_t freesize = (sp - (uintptr_t) mem) & ~pagesize_m1;
  307. assert (freesize < size);
  308. if (freesize > PTHREAD_STACK_MIN)
  309. __madvise (mem, freesize - PTHREAD_STACK_MIN, MADV_DONTNEED);
  310. #else
  311. /* Page aligned start of memory to free (higher than or equal
  312. to current sp plus the minimum stack size). */
  313. uintptr_t freeblock = (sp + PTHREAD_STACK_MIN + pagesize_m1) & ~pagesize_m1;
  314. uintptr_t free_end = (pd - guardsize) & ~pagesize_m1;
  315. if (free_end > freeblock)
  316. {
  317. size_t freesize = free_end - freeblock;
  318. assert (freesize < size);
  319. __madvise ((void*) freeblock, freesize, MADV_DONTNEED);
  320. }
  321. #endif
  322. }
  323. /* Returns a usable stack for a new thread either by allocating a
  324. new stack or reusing a cached stack of sufficient size.
  325. ATTR must be non-NULL and point to a valid pthread_attr.
  326. PDP must be non-NULL. */
  327. static int
  328. allocate_stack (const struct pthread_attr *attr, struct pthread **pdp,
  329. ALLOCATE_STACK_PARMS)
  330. {
  331. struct pthread *pd;
  332. size_t size;
  333. size_t pagesize_m1 = __getpagesize () - 1;
  334. assert (powerof2 (pagesize_m1 + 1));
  335. assert (TCB_ALIGNMENT >= STACK_ALIGN);
  336. /* Get the stack size from the attribute if it is set. Otherwise we
  337. use the default we determined at start time. */
  338. if (attr->stacksize != 0)
  339. size = attr->stacksize;
  340. else
  341. {
  342. lll_lock (__default_pthread_attr_lock, LLL_PRIVATE);
  343. size = __default_pthread_attr.stacksize;
  344. lll_unlock (__default_pthread_attr_lock, LLL_PRIVATE);
  345. }
  346. /* Get memory for the stack. */
  347. if (__glibc_unlikely (attr->flags & ATTR_FLAG_STACKADDR))
  348. {
  349. uintptr_t adj;
  350. char *stackaddr = (char *) attr->stackaddr;
  351. /* Assume the same layout as the _STACK_GROWS_DOWN case, with struct
  352. pthread at the top of the stack block. Later we adjust the guard
  353. location and stack address to match the _STACK_GROWS_UP case. */
  354. if (_STACK_GROWS_UP)
  355. stackaddr += attr->stacksize;
  356. /* If the user also specified the size of the stack make sure it
  357. is large enough. */
  358. if (attr->stacksize != 0
  359. && attr->stacksize < (__static_tls_size + MINIMAL_REST_STACK))
  360. return EINVAL;
  361. /* Adjust stack size for alignment of the TLS block. */
  362. #if TLS_TCB_AT_TP
  363. adj = ((uintptr_t) stackaddr - TLS_TCB_SIZE)
  364. & __static_tls_align_m1;
  365. assert (size > adj + TLS_TCB_SIZE);
  366. #elif TLS_DTV_AT_TP
  367. adj = ((uintptr_t) stackaddr - __static_tls_size)
  368. & __static_tls_align_m1;
  369. assert (size > adj);
  370. #endif
  371. /* The user provided some memory. Let's hope it matches the
  372. size... We do not allocate guard pages if the user provided
  373. the stack. It is the user's responsibility to do this if it
  374. is wanted. */
  375. #if TLS_TCB_AT_TP
  376. pd = (struct pthread *) ((uintptr_t) stackaddr
  377. - TLS_TCB_SIZE - adj);
  378. #elif TLS_DTV_AT_TP
  379. pd = (struct pthread *) (((uintptr_t) stackaddr
  380. - __static_tls_size - adj)
  381. - TLS_PRE_TCB_SIZE);
  382. #endif
  383. /* The user provided stack memory needs to be cleared. */
  384. memset (pd, '\0', sizeof (struct pthread));
  385. /* The first TSD block is included in the TCB. */
  386. pd->specific[0] = pd->specific_1stblock;
  387. /* Remember the stack-related values. */
  388. pd->stackblock = (char *) stackaddr - size;
  389. pd->stackblock_size = size;
  390. /* This is a user-provided stack. It will not be queued in the
  391. stack cache nor will the memory (except the TLS memory) be freed. */
  392. pd->user_stack = true;
  393. /* This is at least the second thread. */
  394. pd->header.multiple_threads = 1;
  395. #ifndef TLS_MULTIPLE_THREADS_IN_TCB
  396. __pthread_multiple_threads = *__libc_multiple_threads_ptr = 1;
  397. #endif
  398. #ifdef NEED_DL_SYSINFO
  399. SETUP_THREAD_SYSINFO (pd);
  400. #endif
  401. /* Don't allow setxid until cloned. */
  402. pd->setxid_futex = -1;
  403. /* Allocate the DTV for this thread. */
  404. if (_dl_allocate_tls (TLS_TPADJ (pd)) == NULL)
  405. {
  406. /* Something went wrong. */
  407. assert (errno == ENOMEM);
  408. return errno;
  409. }
  410. /* Prepare to modify global data. */
  411. lll_lock (stack_cache_lock, LLL_PRIVATE);
  412. /* And add to the list of stacks in use. */
  413. list_add (&pd->list, &__stack_user);
  414. lll_unlock (stack_cache_lock, LLL_PRIVATE);
  415. }
  416. else
  417. {
  418. /* Allocate some anonymous memory. If possible use the cache. */
  419. size_t guardsize;
  420. size_t reqsize;
  421. void *mem;
  422. const int prot = (PROT_READ | PROT_WRITE
  423. | ((GL(dl_stack_flags) & PF_X) ? PROT_EXEC : 0));
  424. /* Adjust the stack size for alignment. */
  425. size &= ~__static_tls_align_m1;
  426. assert (size != 0);
  427. /* Make sure the size of the stack is enough for the guard and
  428. eventually the thread descriptor. */
  429. guardsize = (attr->guardsize + pagesize_m1) & ~pagesize_m1;
  430. if (guardsize < attr->guardsize || size + guardsize < guardsize)
  431. /* Arithmetic overflow. */
  432. return EINVAL;
  433. size += guardsize;
  434. if (__builtin_expect (size < ((guardsize + __static_tls_size
  435. + MINIMAL_REST_STACK + pagesize_m1)
  436. & ~pagesize_m1),
  437. 0))
  438. /* The stack is too small (or the guard too large). */
  439. return EINVAL;
  440. /* Try to get a stack from the cache. */
  441. reqsize = size;
  442. pd = get_cached_stack (&size, &mem);
  443. if (pd == NULL)
  444. {
  445. /* To avoid aliasing effects on a larger scale than pages we
  446. adjust the allocated stack size if necessary. This way
  447. allocations directly following each other will not have
  448. aliasing problems. */
  449. #if MULTI_PAGE_ALIASING != 0
  450. if ((size % MULTI_PAGE_ALIASING) == 0)
  451. size += pagesize_m1 + 1;
  452. #endif
  453. /* If a guard page is required, avoid committing memory by first
  454. allocate with PROT_NONE and then reserve with required permission
  455. excluding the guard page. */
  456. mem = __mmap (NULL, size, (guardsize == 0) ? prot : PROT_NONE,
  457. MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK, -1, 0);
  458. if (__glibc_unlikely (mem == MAP_FAILED))
  459. return errno;
  460. /* SIZE is guaranteed to be greater than zero.
  461. So we can never get a null pointer back from mmap. */
  462. assert (mem != NULL);
  463. /* Place the thread descriptor at the end of the stack. */
  464. #if TLS_TCB_AT_TP
  465. pd = (struct pthread *) ((char *) mem + size) - 1;
  466. #elif TLS_DTV_AT_TP
  467. pd = (struct pthread *) ((((uintptr_t) mem + size
  468. - __static_tls_size)
  469. & ~__static_tls_align_m1)
  470. - TLS_PRE_TCB_SIZE);
  471. #endif
  472. /* Now mprotect the required region excluding the guard area. */
  473. if (__glibc_likely (guardsize > 0))
  474. {
  475. char *guard = guard_position (mem, size, guardsize, pd,
  476. pagesize_m1);
  477. if (setup_stack_prot (mem, size, guard, guardsize, prot) != 0)
  478. {
  479. __munmap (mem, size);
  480. return errno;
  481. }
  482. }
  483. /* Remember the stack-related values. */
  484. pd->stackblock = mem;
  485. pd->stackblock_size = size;
  486. /* Update guardsize for newly allocated guardsize to avoid
  487. an mprotect in guard resize below. */
  488. pd->guardsize = guardsize;
  489. /* We allocated the first block thread-specific data array.
  490. This address will not change for the lifetime of this
  491. descriptor. */
  492. pd->specific[0] = pd->specific_1stblock;
  493. /* This is at least the second thread. */
  494. pd->header.multiple_threads = 1;
  495. #ifndef TLS_MULTIPLE_THREADS_IN_TCB
  496. __pthread_multiple_threads = *__libc_multiple_threads_ptr = 1;
  497. #endif
  498. #ifdef NEED_DL_SYSINFO
  499. SETUP_THREAD_SYSINFO (pd);
  500. #endif
  501. /* Don't allow setxid until cloned. */
  502. pd->setxid_futex = -1;
  503. /* Allocate the DTV for this thread. */
  504. if (_dl_allocate_tls (TLS_TPADJ (pd)) == NULL)
  505. {
  506. /* Something went wrong. */
  507. assert (errno == ENOMEM);
  508. /* Free the stack memory we just allocated. */
  509. (void) __munmap (mem, size);
  510. return errno;
  511. }
  512. /* Prepare to modify global data. */
  513. lll_lock (stack_cache_lock, LLL_PRIVATE);
  514. /* And add to the list of stacks in use. */
  515. stack_list_add (&pd->list, &stack_used);
  516. lll_unlock (stack_cache_lock, LLL_PRIVATE);
  517. /* There might have been a race. Another thread might have
  518. caused the stacks to get exec permission while this new
  519. stack was prepared. Detect if this was possible and
  520. change the permission if necessary. */
  521. if (__builtin_expect ((GL(dl_stack_flags) & PF_X) != 0
  522. && (prot & PROT_EXEC) == 0, 0))
  523. {
  524. int err = change_stack_perm (pd
  525. #ifdef NEED_SEPARATE_REGISTER_STACK
  526. , ~pagesize_m1
  527. #endif
  528. );
  529. if (err != 0)
  530. {
  531. /* Free the stack memory we just allocated. */
  532. (void) __munmap (mem, size);
  533. return err;
  534. }
  535. }
  536. /* Note that all of the stack and the thread descriptor is
  537. zeroed. This means we do not have to initialize fields
  538. with initial value zero. This is specifically true for
  539. the 'tid' field which is always set back to zero once the
  540. stack is not used anymore and for the 'guardsize' field
  541. which will be read next. */
  542. }
  543. /* Create or resize the guard area if necessary. */
  544. if (__glibc_unlikely (guardsize > pd->guardsize))
  545. {
  546. char *guard = guard_position (mem, size, guardsize, pd,
  547. pagesize_m1);
  548. if (__mprotect (guard, guardsize, PROT_NONE) != 0)
  549. {
  550. mprot_error:
  551. lll_lock (stack_cache_lock, LLL_PRIVATE);
  552. /* Remove the thread from the list. */
  553. stack_list_del (&pd->list);
  554. lll_unlock (stack_cache_lock, LLL_PRIVATE);
  555. /* Get rid of the TLS block we allocated. */
  556. _dl_deallocate_tls (TLS_TPADJ (pd), false);
  557. /* Free the stack memory regardless of whether the size
  558. of the cache is over the limit or not. If this piece
  559. of memory caused problems we better do not use it
  560. anymore. Uh, and we ignore possible errors. There
  561. is nothing we could do. */
  562. (void) __munmap (mem, size);
  563. return errno;
  564. }
  565. pd->guardsize = guardsize;
  566. }
  567. else if (__builtin_expect (pd->guardsize - guardsize > size - reqsize,
  568. 0))
  569. {
  570. /* The old guard area is too large. */
  571. #ifdef NEED_SEPARATE_REGISTER_STACK
  572. char *guard = mem + (((size - guardsize) / 2) & ~pagesize_m1);
  573. char *oldguard = mem + (((size - pd->guardsize) / 2) & ~pagesize_m1);
  574. if (oldguard < guard
  575. && __mprotect (oldguard, guard - oldguard, prot) != 0)
  576. goto mprot_error;
  577. if (__mprotect (guard + guardsize,
  578. oldguard + pd->guardsize - guard - guardsize,
  579. prot) != 0)
  580. goto mprot_error;
  581. #elif _STACK_GROWS_DOWN
  582. if (__mprotect ((char *) mem + guardsize, pd->guardsize - guardsize,
  583. prot) != 0)
  584. goto mprot_error;
  585. #elif _STACK_GROWS_UP
  586. char *new_guard = (char *)(((uintptr_t) pd - guardsize)
  587. & ~pagesize_m1);
  588. char *old_guard = (char *)(((uintptr_t) pd - pd->guardsize)
  589. & ~pagesize_m1);
  590. /* The guard size difference might be > 0, but once rounded
  591. to the nearest page the size difference might be zero. */
  592. if (new_guard > old_guard
  593. && __mprotect (old_guard, new_guard - old_guard, prot) != 0)
  594. goto mprot_error;
  595. #endif
  596. pd->guardsize = guardsize;
  597. }
  598. /* The pthread_getattr_np() calls need to get passed the size
  599. requested in the attribute, regardless of how large the
  600. actually used guardsize is. */
  601. pd->reported_guardsize = guardsize;
  602. }
  603. /* Initialize the lock. We have to do this unconditionally since the
  604. stillborn thread could be canceled while the lock is taken. */
  605. pd->lock = LLL_LOCK_INITIALIZER;
  606. /* The robust mutex lists also need to be initialized
  607. unconditionally because the cleanup for the previous stack owner
  608. might have happened in the kernel. */
  609. pd->robust_head.futex_offset = (offsetof (pthread_mutex_t, __data.__lock)
  610. - offsetof (pthread_mutex_t,
  611. __data.__list.__next));
  612. pd->robust_head.list_op_pending = NULL;
  613. #if __PTHREAD_MUTEX_HAVE_PREV
  614. pd->robust_prev = &pd->robust_head;
  615. #endif
  616. pd->robust_head.list = &pd->robust_head;
  617. /* We place the thread descriptor at the end of the stack. */
  618. *pdp = pd;
  619. #if _STACK_GROWS_DOWN
  620. void *stacktop;
  621. # if TLS_TCB_AT_TP
  622. /* The stack begins before the TCB and the static TLS block. */
  623. stacktop = ((char *) (pd + 1) - __static_tls_size);
  624. # elif TLS_DTV_AT_TP
  625. stacktop = (char *) (pd - 1);
  626. # endif
  627. # ifdef NEED_SEPARATE_REGISTER_STACK
  628. *stack = pd->stackblock;
  629. *stacksize = stacktop - *stack;
  630. # else
  631. *stack = stacktop;
  632. # endif
  633. #else
  634. *stack = pd->stackblock;
  635. #endif
  636. return 0;
  637. }
  638. void
  639. __deallocate_stack (struct pthread *pd)
  640. {
  641. lll_lock (stack_cache_lock, LLL_PRIVATE);
  642. /* Remove the thread from the list of threads with user defined
  643. stacks. */
  644. stack_list_del (&pd->list);
  645. /* Not much to do. Just free the mmap()ed memory. Note that we do
  646. not reset the 'used' flag in the 'tid' field. This is done by
  647. the kernel. If no thread has been created yet this field is
  648. still zero. */
  649. if (__glibc_likely (! pd->user_stack))
  650. (void) queue_stack (pd);
  651. else
  652. /* Free the memory associated with the ELF TLS. */
  653. _dl_deallocate_tls (TLS_TPADJ (pd), false);
  654. lll_unlock (stack_cache_lock, LLL_PRIVATE);
  655. }
  656. int
  657. __make_stacks_executable (void **stack_endp)
  658. {
  659. /* First the main thread's stack. */
  660. int err = _dl_make_stack_executable (stack_endp);
  661. if (err != 0)
  662. return err;
  663. #ifdef NEED_SEPARATE_REGISTER_STACK
  664. const size_t pagemask = ~(__getpagesize () - 1);
  665. #endif
  666. lll_lock (stack_cache_lock, LLL_PRIVATE);
  667. list_t *runp;
  668. list_for_each (runp, &stack_used)
  669. {
  670. err = change_stack_perm (list_entry (runp, struct pthread, list)
  671. #ifdef NEED_SEPARATE_REGISTER_STACK
  672. , pagemask
  673. #endif
  674. );
  675. if (err != 0)
  676. break;
  677. }
  678. /* Also change the permission for the currently unused stacks. This
  679. might be wasted time but better spend it here than adding a check
  680. in the fast path. */
  681. if (err == 0)
  682. list_for_each (runp, &stack_cache)
  683. {
  684. err = change_stack_perm (list_entry (runp, struct pthread, list)
  685. #ifdef NEED_SEPARATE_REGISTER_STACK
  686. , pagemask
  687. #endif
  688. );
  689. if (err != 0)
  690. break;
  691. }
  692. lll_unlock (stack_cache_lock, LLL_PRIVATE);
  693. return err;
  694. }
  695. /* In case of a fork() call the memory allocation in the child will be
  696. the same but only one thread is running. All stacks except that of
  697. the one running thread are not used anymore. We have to recycle
  698. them. */
  699. void
  700. __reclaim_stacks (void)
  701. {
  702. struct pthread *self = (struct pthread *) THREAD_SELF;
  703. /* No locking necessary. The caller is the only stack in use. But
  704. we have to be aware that we might have interrupted a list
  705. operation. */
  706. if (in_flight_stack != 0)
  707. {
  708. bool add_p = in_flight_stack & 1;
  709. list_t *elem = (list_t *) (in_flight_stack & ~(uintptr_t) 1);
  710. if (add_p)
  711. {
  712. /* We always add at the beginning of the list. So in this case we
  713. only need to check the beginning of these lists to see if the
  714. pointers at the head of the list are inconsistent. */
  715. list_t *l = NULL;
  716. if (stack_used.next->prev != &stack_used)
  717. l = &stack_used;
  718. else if (stack_cache.next->prev != &stack_cache)
  719. l = &stack_cache;
  720. if (l != NULL)
  721. {
  722. assert (l->next->prev == elem);
  723. elem->next = l->next;
  724. elem->prev = l;
  725. l->next = elem;
  726. }
  727. }
  728. else
  729. {
  730. /* We can simply always replay the delete operation. */
  731. elem->next->prev = elem->prev;
  732. elem->prev->next = elem->next;
  733. }
  734. }
  735. /* Mark all stacks except the still running one as free. */
  736. list_t *runp;
  737. list_for_each (runp, &stack_used)
  738. {
  739. struct pthread *curp = list_entry (runp, struct pthread, list);
  740. if (curp != self)
  741. {
  742. /* This marks the stack as free. */
  743. curp->tid = 0;
  744. /* Account for the size of the stack. */
  745. stack_cache_actsize += curp->stackblock_size;
  746. if (curp->specific_used)
  747. {
  748. /* Clear the thread-specific data. */
  749. memset (curp->specific_1stblock, '\0',
  750. sizeof (curp->specific_1stblock));
  751. curp->specific_used = false;
  752. for (size_t cnt = 1; cnt < PTHREAD_KEY_1STLEVEL_SIZE; ++cnt)
  753. if (curp->specific[cnt] != NULL)
  754. {
  755. memset (curp->specific[cnt], '\0',
  756. sizeof (curp->specific_1stblock));
  757. /* We have allocated the block which we do not
  758. free here so re-set the bit. */
  759. curp->specific_used = true;
  760. }
  761. }
  762. }
  763. }
  764. /* Add the stack of all running threads to the cache. */
  765. list_splice (&stack_used, &stack_cache);
  766. /* Remove the entry for the current thread to from the cache list
  767. and add it to the list of running threads. Which of the two
  768. lists is decided by the user_stack flag. */
  769. stack_list_del (&self->list);
  770. /* Re-initialize the lists for all the threads. */
  771. INIT_LIST_HEAD (&stack_used);
  772. INIT_LIST_HEAD (&__stack_user);
  773. if (__glibc_unlikely (THREAD_GETMEM (self, user_stack)))
  774. list_add (&self->list, &__stack_user);
  775. else
  776. list_add (&self->list, &stack_used);
  777. /* There is one thread running. */
  778. __nptl_nthreads = 1;
  779. in_flight_stack = 0;
  780. /* Initialize locks. */
  781. stack_cache_lock = LLL_LOCK_INITIALIZER;
  782. __default_pthread_attr_lock = LLL_LOCK_INITIALIZER;
  783. }
  784. #if HP_TIMING_AVAIL
  785. # undef __find_thread_by_id
  786. /* Find a thread given the thread ID. */
  787. attribute_hidden
  788. struct pthread *
  789. __find_thread_by_id (pid_t tid)
  790. {
  791. struct pthread *result = NULL;
  792. lll_lock (stack_cache_lock, LLL_PRIVATE);
  793. /* Iterate over the list with system-allocated threads first. */
  794. list_t *runp;
  795. list_for_each (runp, &stack_used)
  796. {
  797. struct pthread *curp;
  798. curp = list_entry (runp, struct pthread, list);
  799. if (curp->tid == tid)
  800. {
  801. result = curp;
  802. goto out;
  803. }
  804. }
  805. /* Now the list with threads using user-allocated stacks. */
  806. list_for_each (runp, &__stack_user)
  807. {
  808. struct pthread *curp;
  809. curp = list_entry (runp, struct pthread, list);
  810. if (curp->tid == tid)
  811. {
  812. result = curp;
  813. goto out;
  814. }
  815. }
  816. out:
  817. lll_unlock (stack_cache_lock, LLL_PRIVATE);
  818. return result;
  819. }
  820. #endif
  821. #ifdef SIGSETXID
  822. static void
  823. setxid_mark_thread (struct xid_command *cmdp, struct pthread *t)
  824. {
  825. int ch;
  826. /* Wait until this thread is cloned. */
  827. if (t->setxid_futex == -1
  828. && ! atomic_compare_and_exchange_bool_acq (&t->setxid_futex, -2, -1))
  829. do
  830. futex_wait_simple (&t->setxid_futex, -2, FUTEX_PRIVATE);
  831. while (t->setxid_futex == -2);
  832. /* Don't let the thread exit before the setxid handler runs. */
  833. t->setxid_futex = 0;
  834. do
  835. {
  836. ch = t->cancelhandling;
  837. /* If the thread is exiting right now, ignore it. */
  838. if ((ch & EXITING_BITMASK) != 0)
  839. {
  840. /* Release the futex if there is no other setxid in
  841. progress. */
  842. if ((ch & SETXID_BITMASK) == 0)
  843. {
  844. t->setxid_futex = 1;
  845. futex_wake (&t->setxid_futex, 1, FUTEX_PRIVATE);
  846. }
  847. return;
  848. }
  849. }
  850. while (atomic_compare_and_exchange_bool_acq (&t->cancelhandling,
  851. ch | SETXID_BITMASK, ch));
  852. }
  853. static void
  854. setxid_unmark_thread (struct xid_command *cmdp, struct pthread *t)
  855. {
  856. int ch;
  857. do
  858. {
  859. ch = t->cancelhandling;
  860. if ((ch & SETXID_BITMASK) == 0)
  861. return;
  862. }
  863. while (atomic_compare_and_exchange_bool_acq (&t->cancelhandling,
  864. ch & ~SETXID_BITMASK, ch));
  865. /* Release the futex just in case. */
  866. t->setxid_futex = 1;
  867. futex_wake (&t->setxid_futex, 1, FUTEX_PRIVATE);
  868. }
  869. static int
  870. setxid_signal_thread (struct xid_command *cmdp, struct pthread *t)
  871. {
  872. if ((t->cancelhandling & SETXID_BITMASK) == 0)
  873. return 0;
  874. int val;
  875. pid_t pid = __getpid ();
  876. INTERNAL_SYSCALL_DECL (err);
  877. val = INTERNAL_SYSCALL_CALL (tgkill, err, pid, t->tid, SIGSETXID);
  878. /* If this failed, it must have had not started yet or else exited. */
  879. if (!INTERNAL_SYSCALL_ERROR_P (val, err))
  880. {
  881. atomic_increment (&cmdp->cntr);
  882. return 1;
  883. }
  884. else
  885. return 0;
  886. }
  887. /* Check for consistency across set*id system call results. The abort
  888. should not happen as long as all privileges changes happen through
  889. the glibc wrappers. ERROR must be 0 (no error) or an errno
  890. code. */
  891. void
  892. attribute_hidden
  893. __nptl_setxid_error (struct xid_command *cmdp, int error)
  894. {
  895. do
  896. {
  897. int olderror = cmdp->error;
  898. if (olderror == error)
  899. break;
  900. if (olderror != -1)
  901. {
  902. /* Mismatch between current and previous results. Save the
  903. error value to memory so that is not clobbered by the
  904. abort function and preserved in coredumps. */
  905. volatile int xid_err __attribute__((unused)) = error;
  906. abort ();
  907. }
  908. }
  909. while (atomic_compare_and_exchange_bool_acq (&cmdp->error, error, -1));
  910. }
  911. int
  912. attribute_hidden
  913. __nptl_setxid (struct xid_command *cmdp)
  914. {
  915. int signalled;
  916. int result;
  917. lll_lock (stack_cache_lock, LLL_PRIVATE);
  918. __xidcmd = cmdp;
  919. cmdp->cntr = 0;
  920. cmdp->error = -1;
  921. struct pthread *self = THREAD_SELF;
  922. /* Iterate over the list with system-allocated threads first. */
  923. list_t *runp;
  924. list_for_each (runp, &stack_used)
  925. {
  926. struct pthread *t = list_entry (runp, struct pthread, list);
  927. if (t == self)
  928. continue;
  929. setxid_mark_thread (cmdp, t);
  930. }
  931. /* Now the list with threads using user-allocated stacks. */
  932. list_for_each (runp, &__stack_user)
  933. {
  934. struct pthread *t = list_entry (runp, struct pthread, list);
  935. if (t == self)
  936. continue;
  937. setxid_mark_thread (cmdp, t);
  938. }
  939. /* Iterate until we don't succeed in signalling anyone. That means
  940. we have gotten all running threads, and their children will be
  941. automatically correct once started. */
  942. do
  943. {
  944. signalled = 0;
  945. list_for_each (runp, &stack_used)
  946. {
  947. struct pthread *t = list_entry (runp, struct pthread, list);
  948. if (t == self)
  949. continue;
  950. signalled += setxid_signal_thread (cmdp, t);
  951. }
  952. list_for_each (runp, &__stack_user)
  953. {
  954. struct pthread *t = list_entry (runp, struct pthread, list);
  955. if (t == self)
  956. continue;
  957. signalled += setxid_signal_thread (cmdp, t);
  958. }
  959. int cur = cmdp->cntr;
  960. while (cur != 0)
  961. {
  962. futex_wait_simple ((unsigned int *) &cmdp->cntr, cur,
  963. FUTEX_PRIVATE);
  964. cur = cmdp->cntr;
  965. }
  966. }
  967. while (signalled != 0);
  968. /* Clean up flags, so that no thread blocks during exit waiting
  969. for a signal which will never come. */
  970. list_for_each (runp, &stack_used)
  971. {
  972. struct pthread *t = list_entry (runp, struct pthread, list);
  973. if (t == self)
  974. continue;
  975. setxid_unmark_thread (cmdp, t);
  976. }
  977. list_for_each (runp, &__stack_user)
  978. {
  979. struct pthread *t = list_entry (runp, struct pthread, list);
  980. if (t == self)
  981. continue;
  982. setxid_unmark_thread (cmdp, t);
  983. }
  984. /* This must be last, otherwise the current thread might not have
  985. permissions to send SIGSETXID syscall to the other threads. */
  986. INTERNAL_SYSCALL_DECL (err);
  987. result = INTERNAL_SYSCALL_NCS (cmdp->syscall_no, err, 3,
  988. cmdp->id[0], cmdp->id[1], cmdp->id[2]);
  989. int error = 0;
  990. if (__glibc_unlikely (INTERNAL_SYSCALL_ERROR_P (result, err)))
  991. {
  992. error = INTERNAL_SYSCALL_ERRNO (result, err);
  993. __set_errno (error);
  994. result = -1;
  995. }
  996. __nptl_setxid_error (cmdp, error);
  997. lll_unlock (stack_cache_lock, LLL_PRIVATE);
  998. return result;
  999. }
  1000. #endif /* SIGSETXID. */
  1001. static inline void __attribute__((always_inline))
  1002. init_one_static_tls (struct pthread *curp, struct link_map *map)
  1003. {
  1004. # if TLS_TCB_AT_TP
  1005. void *dest = (char *) curp - map->l_tls_offset;
  1006. # elif TLS_DTV_AT_TP
  1007. void *dest = (char *) curp + map->l_tls_offset + TLS_PRE_TCB_SIZE;
  1008. # else
  1009. # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
  1010. # endif
  1011. /* Initialize the memory. */
  1012. memset (__mempcpy (dest, map->l_tls_initimage, map->l_tls_initimage_size),
  1013. '\0', map->l_tls_blocksize - map->l_tls_initimage_size);
  1014. }
  1015. void
  1016. attribute_hidden
  1017. __pthread_init_static_tls (struct link_map *map)
  1018. {
  1019. lll_lock (stack_cache_lock, LLL_PRIVATE);
  1020. /* Iterate over the list with system-allocated threads first. */
  1021. list_t *runp;
  1022. list_for_each (runp, &stack_used)
  1023. init_one_static_tls (list_entry (runp, struct pthread, list), map);
  1024. /* Now the list with threads using user-allocated stacks. */
  1025. list_for_each (runp, &__stack_user)
  1026. init_one_static_tls (list_entry (runp, struct pthread, list), map);
  1027. lll_unlock (stack_cache_lock, LLL_PRIVATE);
  1028. }
  1029. void
  1030. attribute_hidden
  1031. __wait_lookup_done (void)
  1032. {
  1033. lll_lock (stack_cache_lock, LLL_PRIVATE);
  1034. struct pthread *self = THREAD_SELF;
  1035. /* Iterate over the list with system-allocated threads first. */
  1036. list_t *runp;
  1037. list_for_each (runp, &stack_used)
  1038. {
  1039. struct pthread *t = list_entry (runp, struct pthread, list);
  1040. if (t == self || t->header.gscope_flag == THREAD_GSCOPE_FLAG_UNUSED)
  1041. continue;
  1042. int *const gscope_flagp = &t->header.gscope_flag;
  1043. /* We have to wait until this thread is done with the global
  1044. scope. First tell the thread that we are waiting and
  1045. possibly have to be woken. */
  1046. if (atomic_compare_and_exchange_bool_acq (gscope_flagp,
  1047. THREAD_GSCOPE_FLAG_WAIT,
  1048. THREAD_GSCOPE_FLAG_USED))
  1049. continue;
  1050. do
  1051. futex_wait_simple ((unsigned int *) gscope_flagp,
  1052. THREAD_GSCOPE_FLAG_WAIT, FUTEX_PRIVATE);
  1053. while (*gscope_flagp == THREAD_GSCOPE_FLAG_WAIT);
  1054. }
  1055. /* Now the list with threads using user-allocated stacks. */
  1056. list_for_each (runp, &__stack_user)
  1057. {
  1058. struct pthread *t = list_entry (runp, struct pthread, list);
  1059. if (t == self || t->header.gscope_flag == THREAD_GSCOPE_FLAG_UNUSED)
  1060. continue;
  1061. int *const gscope_flagp = &t->header.gscope_flag;
  1062. /* We have to wait until this thread is done with the global
  1063. scope. First tell the thread that we are waiting and
  1064. possibly have to be woken. */
  1065. if (atomic_compare_and_exchange_bool_acq (gscope_flagp,
  1066. THREAD_GSCOPE_FLAG_WAIT,
  1067. THREAD_GSCOPE_FLAG_USED))
  1068. continue;
  1069. do
  1070. futex_wait_simple ((unsigned int *) gscope_flagp,
  1071. THREAD_GSCOPE_FLAG_WAIT, FUTEX_PRIVATE);
  1072. while (*gscope_flagp == THREAD_GSCOPE_FLAG_WAIT);
  1073. }
  1074. lll_unlock (stack_cache_lock, LLL_PRIVATE);
  1075. }