1 /* Copyright (C) 2002-2007, 2009 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
26 #include <sys/param.h>
28 #include <lowlevellock.h>
30 #include <bits/kernel-features.h>
33 #ifndef NEED_SEPARATE_REGISTER_STACK
35 /* Most architectures have exactly one stack pointer. Some have more. */
36 # define STACK_VARIABLES void *stackaddr = NULL
38 /* How to pass the values to the 'create_thread' function. */
39 # define STACK_VARIABLES_ARGS stackaddr
41 /* How to declare function which gets there parameters. */
42 # define STACK_VARIABLES_PARMS void *stackaddr
44 /* How to declare allocate_stack. */
45 # define ALLOCATE_STACK_PARMS void **stack
47 /* This is how the function is called. We do it this way to allow
48 other variants of the function to have more parameters. */
49 # define ALLOCATE_STACK(attr, pd) allocate_stack (attr, pd, &stackaddr)
53 /* We need two stacks. The kernel will place them but we have to tell
54 the kernel about the size of the reserved address space. */
55 # define STACK_VARIABLES void *stackaddr = NULL; size_t stacksize = 0
57 /* How to pass the values to the 'create_thread' function. */
58 # define STACK_VARIABLES_ARGS stackaddr, stacksize
60 /* How to declare function which gets there parameters. */
61 # define STACK_VARIABLES_PARMS void *stackaddr, size_t stacksize
63 /* How to declare allocate_stack. */
64 # define ALLOCATE_STACK_PARMS void **stack, size_t *stacksize
66 /* This is how the function is called. We do it this way to allow
67 other variants of the function to have more parameters. */
68 # define ALLOCATE_STACK(attr, pd) \
69 allocate_stack (attr, pd, &stackaddr, &stacksize)
74 /* Default alignment of stack. */
76 # define STACK_ALIGN __alignof__ (long double)
79 /* Default value for minimal stack size after allocating thread
80 descriptor and guard. */
81 #ifndef MINIMAL_REST_STACK
82 # define MINIMAL_REST_STACK 4096
86 /* Newer kernels have the MAP_STACK flag to indicate a mapping is used for
87 a stack. Use it when possible. */
92 /* This yields the pointer that TLS support code calls the thread pointer. */
93 #if defined(TLS_TCB_AT_TP)
94 # define TLS_TPADJ(pd) (pd)
95 #elif defined(TLS_DTV_AT_TP)
96 # define TLS_TPADJ(pd) ((struct pthread *)((char *) (pd) + TLS_PRE_TCB_SIZE))
99 /* Cache handling for not-yet free stacks. */
102 Maximum size in kB of cache. GNU libc default is 40MiB
103 embedded systems don't have enough ram for big dirty stack caches,
104 reduce it to 16MiB. 4 does not work, f.e. tst-kill4 segfaults.
106 static size_t stack_cache_maxsize
= 16 * 1024 * 1024;
107 static size_t stack_cache_actsize
;
109 /* Mutex protecting this variable. */
110 static int stack_cache_lock
= LLL_LOCK_INITIALIZER
;
112 /* List of queued stack frames. */
113 static LIST_HEAD (stack_cache
);
115 /* List of the stacks in use. */
116 static LIST_HEAD (stack_used
);
118 /* We need to record what list operations we are going to do so that,
119 in case of an asynchronous interruption due to a fork() call, we
120 can correct for the work. */
121 static uintptr_t in_flight_stack
;
123 /* List of the threads with user provided stacks in use. No need to
124 initialize this, since it's done in __pthread_initialize_minimal. */
125 list_t __stack_user
__attribute__ ((nocommon
));
126 hidden_data_def (__stack_user
)
128 #if defined COLORING_INCREMENT && COLORING_INCREMENT != 0
129 /* Number of threads created. */
130 static unsigned int nptl_ncreated
;
134 /* Check whether the stack is still used or not. */
135 #define FREE_P(descr) ((descr)->tid <= 0)
139 stack_list_del (list_t
*elem
)
141 in_flight_stack
= (uintptr_t) elem
;
143 atomic_write_barrier ();
147 atomic_write_barrier ();
154 stack_list_add (list_t
*elem
, list_t
*list
)
156 in_flight_stack
= (uintptr_t) elem
| 1;
158 atomic_write_barrier ();
160 list_add (elem
, list
);
162 atomic_write_barrier ();
168 /* We create a double linked list of all cache entries. Double linked
169 because this allows removing entries from the end. */
172 /* Get a stack frame from the cache. We have to match by size since
173 some blocks might be too small or far too large. */
174 static struct pthread
*
175 get_cached_stack (size_t *sizep
, void **memp
)
177 size_t size
= *sizep
;
178 struct pthread
*result
= NULL
;
181 lll_lock (stack_cache_lock
, LLL_PRIVATE
);
183 /* Search the cache for a matching entry. We search for the
184 smallest stack which has at least the required size. Note that
185 in normal situations the size of all allocated stacks is the
186 same. As the very least there are only a few different sizes.
187 Therefore this loop will exit early most of the time with an
189 list_for_each (entry
, &stack_cache
)
191 struct pthread
*curr
;
193 curr
= list_entry (entry
, struct pthread
, list
);
194 if (FREE_P (curr
) && curr
->stackblock_size
>= size
)
196 if (curr
->stackblock_size
== size
)
203 || result
->stackblock_size
> curr
->stackblock_size
)
208 if (__builtin_expect (result
== NULL
, 0)
209 /* Make sure the size difference is not too excessive. In that
210 case we do not use the block. */
211 || __builtin_expect (result
->stackblock_size
> 4 * size
, 0))
213 /* Release the lock. */
214 lll_unlock (stack_cache_lock
, LLL_PRIVATE
);
219 /* Dequeue the entry. */
220 stack_list_del (&result
->list
);
222 /* And add to the list of stacks in use. */
223 stack_list_add (&result
->list
, &stack_used
);
225 /* And decrease the cache size. */
226 stack_cache_actsize
-= result
->stackblock_size
;
228 /* Release the lock early. */
229 lll_unlock (stack_cache_lock
, LLL_PRIVATE
);
231 /* Report size and location of the stack to the caller. */
232 *sizep
= result
->stackblock_size
;
233 *memp
= result
->stackblock
;
235 /* Cancellation handling is back to the default. */
236 result
->cancelhandling
= 0;
237 result
->cleanup
= NULL
;
239 /* No pending event. */
240 result
->nextevent
= NULL
;
243 dtv_t
*dtv
= GET_DTV (TLS_TPADJ (result
));
244 memset (dtv
, '\0', (dtv
[-1].counter
+ 1) * sizeof (dtv_t
));
246 /* Re-initialize the TLS. */
247 _dl_allocate_tls_init (TLS_TPADJ (result
));
253 /* Free stacks until cache size is lower than LIMIT. */
255 __free_stacks (size_t limit
)
257 /* We reduce the size of the cache. Remove the last entries until
258 the size is below the limit. */
262 /* Search from the end of the list. */
263 list_for_each_prev_safe (entry
, prev
, &stack_cache
)
265 struct pthread
*curr
;
267 curr
= list_entry (entry
, struct pthread
, list
);
270 /* Unlink the block. */
271 stack_list_del (entry
);
273 /* Account for the freed memory. */
274 stack_cache_actsize
-= curr
->stackblock_size
;
276 /* Free the memory associated with the ELF TLS. */
277 _dl_deallocate_tls (TLS_TPADJ (curr
), false);
279 /* Remove this block. This should never fail. If it does
280 something is really wrong. */
281 if (munmap (curr
->stackblock
, curr
->stackblock_size
) != 0)
284 /* Maybe we have freed enough. */
285 if (stack_cache_actsize
<= limit
)
292 /* Add a stack frame which is not used anymore to the stack. Must be
293 called with the cache lock held. */
295 __attribute ((always_inline
))
296 queue_stack (struct pthread
*stack
)
298 /* We unconditionally add the stack to the list. The memory may
299 still be in use but it will not be reused until the kernel marks
300 the stack as not used anymore. */
301 stack_list_add (&stack
->list
, &stack_cache
);
303 stack_cache_actsize
+= stack
->stackblock_size
;
304 if (__builtin_expect (stack_cache_actsize
> stack_cache_maxsize
, 0))
305 __free_stacks (stack_cache_maxsize
);
311 change_stack_perm (struct pthread
*pd
312 #ifdef NEED_SEPARATE_REGISTER_STACK
317 #ifdef NEED_SEPARATE_REGISTER_STACK
318 void *stack
= (pd
->stackblock
319 + (((((pd
->stackblock_size
- pd
->guardsize
) / 2)
320 & pagemask
) + pd
->guardsize
) & pagemask
));
321 size_t len
= pd
->stackblock
+ pd
->stackblock_size
- stack
;
322 #elif defined _STACK_GROWS_DOWN
323 void *stack
= pd
->stackblock
+ pd
->guardsize
;
324 size_t len
= pd
->stackblock_size
- pd
->guardsize
;
325 #elif defined _STACK_GROWS_UP
326 void *stack
= pd
->stackblock
;
327 size_t len
= (uintptr_t) pd
- pd
->guardsize
- (uintptr_t) pd
->stackblock
;
329 # error "Define either _STACK_GROWS_DOWN or _STACK_GROWS_UP"
331 if (mprotect (stack
, len
, PROT_READ
| PROT_WRITE
| PROT_EXEC
) != 0)
339 allocate_stack (const struct pthread_attr
*attr
, struct pthread
**pdp
,
340 ALLOCATE_STACK_PARMS
)
344 size_t pagesize_m1
= __getpagesize () - 1;
347 assert (attr
!= NULL
);
348 assert (powerof2 (pagesize_m1
+ 1));
349 assert (TCB_ALIGNMENT
>= STACK_ALIGN
);
351 /* Get the stack size from the attribute if it is set. Otherwise we
352 use the default we determined at start time. */
353 size
= attr
->stacksize
?: __default_stacksize
;
355 /* Get memory for the stack. */
356 if (__builtin_expect (attr
->flags
& ATTR_FLAG_STACKADDR
, 0))
360 /* If the user also specified the size of the stack make sure it
362 if (attr
->stacksize
!= 0
363 && attr
->stacksize
< (__static_tls_size
+ MINIMAL_REST_STACK
))
366 /* Adjust stack size for alignment of the TLS block. */
367 #if defined(TLS_TCB_AT_TP)
368 adj
= ((uintptr_t) attr
->stackaddr
- TLS_TCB_SIZE
)
369 & __static_tls_align_m1
;
370 assert (size
> adj
+ TLS_TCB_SIZE
);
371 #elif defined(TLS_DTV_AT_TP)
372 adj
= ((uintptr_t) attr
->stackaddr
- __static_tls_size
)
373 & __static_tls_align_m1
;
377 /* The user provided some memory. Let's hope it matches the
378 size... We do not allocate guard pages if the user provided
379 the stack. It is the user's responsibility to do this if it
381 #if defined(TLS_TCB_AT_TP)
382 pd
= (struct pthread
*) ((uintptr_t) attr
->stackaddr
383 - TLS_TCB_SIZE
- adj
);
384 #elif defined(TLS_DTV_AT_TP)
385 pd
= (struct pthread
*) (((uintptr_t) attr
->stackaddr
386 - __static_tls_size
- adj
)
390 /* The user provided stack memory needs to be cleared. */
391 memset (pd
, '\0', sizeof (struct pthread
));
393 /* The first TSD block is included in the TCB. */
394 pd
->specific
[0] = pd
->specific_1stblock
;
396 /* Remember the stack-related values. */
397 pd
->stackblock
= (char *) attr
->stackaddr
- size
;
398 pd
->stackblock_size
= size
;
400 /* This is a user-provided stack. It will not be queued in the
401 stack cache nor will the memory (except the TLS memory) be freed. */
402 pd
->user_stack
= true;
404 /* This is at least the second thread. */
405 pd
->header
.multiple_threads
= 1;
406 #ifndef TLS_MULTIPLE_THREADS_IN_TCB
407 __pthread_multiple_threads
= *__libc_multiple_threads_ptr
= 1;
410 #ifndef __ASSUME_PRIVATE_FUTEX
411 /* The thread must know when private futexes are supported. */
412 pd
->header
.private_futex
= THREAD_GETMEM (THREAD_SELF
,
413 header
.private_futex
);
416 #ifdef NEED_DL_SYSINFO
417 /* Copy the sysinfo value from the parent. */
418 THREAD_SYSINFO(pd
) = THREAD_SELF_SYSINFO
;
421 /* Allocate the DTV for this thread. */
422 if (_dl_allocate_tls (TLS_TPADJ (pd
)) == NULL
)
424 /* Something went wrong. */
425 assert (errno
== ENOMEM
);
430 /* Prepare to modify global data. */
431 lll_lock (stack_cache_lock
, LLL_PRIVATE
);
433 /* And add to the list of stacks in use. */
434 list_add (&pd
->list
, &__stack_user
);
436 lll_unlock (stack_cache_lock
, LLL_PRIVATE
);
440 /* Allocate some anonymous memory. If possible use the cache. */
444 const int prot
= (PROT_READ
| PROT_WRITE
);
446 #if defined COLORING_INCREMENT && COLORING_INCREMENT != 0
447 /* Add one more page for stack coloring. Don't do it for stacks
448 with 16 times pagesize or larger. This might just cause
449 unnecessary misalignment. */
450 if (size
<= 16 * pagesize_m1
)
451 size
+= pagesize_m1
+ 1;
454 /* Adjust the stack size for alignment. */
455 size
&= ~__static_tls_align_m1
;
458 /* Make sure the size of the stack is enough for the guard and
459 eventually the thread descriptor. */
460 guardsize
= (attr
->guardsize
+ pagesize_m1
) & ~pagesize_m1
;
461 if (__builtin_expect (size
< ((guardsize
+ __static_tls_size
462 + MINIMAL_REST_STACK
+ pagesize_m1
)
465 /* The stack is too small (or the guard too large). */
468 /* Try to get a stack from the cache. */
470 pd
= get_cached_stack (&size
, &mem
);
473 /* To avoid aliasing effects on a larger scale than pages we
474 adjust the allocated stack size if necessary. This way
475 allocations directly following each other will not have
476 aliasing problems. */
477 #if defined MULTI_PAGE_ALIASING && MULTI_PAGE_ALIASING != 0
478 if ((size
% MULTI_PAGE_ALIASING
) == 0)
479 size
+= pagesize_m1
+ 1;
482 mem
= mmap (NULL
, size
, prot
,
483 MAP_PRIVATE
| MAP_ANONYMOUS
| MAP_STACK
, -1, 0);
485 if (__builtin_expect (mem
== MAP_FAILED
, 0))
488 __set_errno (EAGAIN
);
493 /* SIZE is guaranteed to be greater than zero.
494 So we can never get a null pointer back from mmap. */
495 assert (mem
!= NULL
);
497 #if defined COLORING_INCREMENT && COLORING_INCREMENT != 0
498 /* Atomically increment NCREATED. */
499 unsigned int ncreated
= atomic_increment_val (&nptl_ncreated
);
501 /* We chose the offset for coloring by incrementing it for
502 every new thread by a fixed amount. The offset used
503 module the page size. Even if coloring would be better
504 relative to higher alignment values it makes no sense to
505 do it since the mmap() interface does not allow us to
506 specify any alignment for the returned memory block. */
507 size_t coloring
= (ncreated
* COLORING_INCREMENT
) & pagesize_m1
;
509 /* Make sure the coloring offsets does not disturb the alignment
510 of the TCB and static TLS block. */
511 if (__builtin_expect ((coloring
& __static_tls_align_m1
) != 0, 0))
512 coloring
= (((coloring
+ __static_tls_align_m1
)
513 & ~(__static_tls_align_m1
))
516 /* Unless specified we do not make any adjustments. */
520 /* Place the thread descriptor at the end of the stack. */
521 #if defined(TLS_TCB_AT_TP)
522 pd
= (struct pthread
*) ((char *) mem
+ size
- coloring
) - 1;
523 #elif defined(TLS_DTV_AT_TP)
524 pd
= (struct pthread
*) ((((uintptr_t) mem
+ size
- coloring
526 & ~__static_tls_align_m1
)
530 /* Remember the stack-related values. */
531 pd
->stackblock
= mem
;
532 pd
->stackblock_size
= size
;
534 /* We allocated the first block thread-specific data array.
535 This address will not change for the lifetime of this
537 pd
->specific
[0] = pd
->specific_1stblock
;
539 /* This is at least the second thread. */
540 pd
->header
.multiple_threads
= 1;
541 #ifndef TLS_MULTIPLE_THREADS_IN_TCB
542 __pthread_multiple_threads
= *__libc_multiple_threads_ptr
= 1;
545 #ifndef __ASSUME_PRIVATE_FUTEX
546 /* The thread must know when private futexes are supported. */
547 pd
->header
.private_futex
= THREAD_GETMEM (THREAD_SELF
,
548 header
.private_futex
);
551 #ifdef NEED_DL_SYSINFO
552 /* Copy the sysinfo value from the parent. */
553 THREAD_SYSINFO(pd
) = THREAD_SELF_SYSINFO
;
556 /* Allocate the DTV for this thread. */
557 if (_dl_allocate_tls (TLS_TPADJ (pd
)) == NULL
)
559 /* Something went wrong. */
560 assert (errno
== ENOMEM
);
562 /* Free the stack memory we just allocated. */
563 (void) munmap (mem
, size
);
569 /* Prepare to modify global data. */
570 lll_lock (stack_cache_lock
, LLL_PRIVATE
);
572 /* And add to the list of stacks in use. */
573 stack_list_add (&pd
->list
, &stack_used
);
575 lll_unlock (stack_cache_lock
, LLL_PRIVATE
);
578 /* Note that all of the stack and the thread descriptor is
579 zeroed. This means we do not have to initialize fields
580 with initial value zero. This is specifically true for
581 the 'tid' field which is always set back to zero once the
582 stack is not used anymore and for the 'guardsize' field
583 which will be read next. */
586 /* Create or resize the guard area if necessary. */
587 if (__builtin_expect (guardsize
> pd
->guardsize
, 0))
589 #ifdef NEED_SEPARATE_REGISTER_STACK
590 char *guard
= mem
+ (((size
- guardsize
) / 2) & ~pagesize_m1
);
591 #elif defined _STACK_GROWS_DOWN
593 #elif defined _STACK_GROWS_UP
594 char *guard
= (char *) (((uintptr_t) pd
- guardsize
) & ~pagesize_m1
);
596 if (mprotect (guard
, guardsize
, PROT_NONE
) != 0)
602 lll_lock (stack_cache_lock
, LLL_PRIVATE
);
604 /* Remove the thread from the list. */
605 stack_list_del (&pd
->list
);
607 lll_unlock (stack_cache_lock
, LLL_PRIVATE
);
609 /* Get rid of the TLS block we allocated. */
610 _dl_deallocate_tls (TLS_TPADJ (pd
), false);
612 /* Free the stack memory regardless of whether the size
613 of the cache is over the limit or not. If this piece
614 of memory caused problems we better do not use it
615 anymore. Uh, and we ignore possible errors. There
616 is nothing we could do. */
617 (void) munmap (mem
, size
);
622 pd
->guardsize
= guardsize
;
624 else if (__builtin_expect (pd
->guardsize
- guardsize
> size
- reqsize
,
627 /* The old guard area is too large. */
629 #ifdef NEED_SEPARATE_REGISTER_STACK
630 char *guard
= mem
+ (((size
- guardsize
) / 2) & ~pagesize_m1
);
631 char *oldguard
= mem
+ (((size
- pd
->guardsize
) / 2) & ~pagesize_m1
);
634 && mprotect (oldguard
, guard
- oldguard
, prot
) != 0)
637 if (mprotect (guard
+ guardsize
,
638 oldguard
+ pd
->guardsize
- guard
- guardsize
,
641 #elif defined _STACK_GROWS_DOWN
642 if (mprotect ((char *) mem
+ guardsize
, pd
->guardsize
- guardsize
,
645 #elif defined _STACK_GROWS_UP
646 if (mprotect ((char *) (((uintptr_t) pd
- pd
->guardsize
) & ~pagesize_m1
),
647 pd
->guardsize
- guardsize
, prot
) != 0)
651 pd
->guardsize
= guardsize
;
653 /* The pthread_getattr_np() calls need to get passed the size
654 requested in the attribute, regardless of how large the
655 actually used guardsize is. */
656 pd
->reported_guardsize
= guardsize
;
659 /* Initialize the lock. We have to do this unconditionally since the
660 stillborn thread could be canceled while the lock is taken. */
661 pd
->lock
= LLL_LOCK_INITIALIZER
;
663 /* The robust mutex lists also need to be initialized
664 unconditionally because the cleanup for the previous stack owner
665 might have happened in the kernel. */
666 pd
->robust_head
.futex_offset
= (offsetof (pthread_mutex_t
, __data
.__lock
)
667 - offsetof (pthread_mutex_t
,
668 __data
.__list
.__next
));
669 pd
->robust_head
.list_op_pending
= NULL
;
670 #ifdef __PTHREAD_MUTEX_HAVE_PREV
671 pd
->robust_prev
= &pd
->robust_head
;
673 pd
->robust_head
.list
= &pd
->robust_head
;
675 /* We place the thread descriptor at the end of the stack. */
678 #if defined(TLS_TCB_AT_TP)
679 /* The stack begins before the TCB and the static TLS block. */
680 stacktop
= ((char *) (pd
+ 1) - __static_tls_size
);
681 #elif defined(TLS_DTV_AT_TP)
682 stacktop
= (char *) (pd
- 1);
685 #ifdef NEED_SEPARATE_REGISTER_STACK
686 *stack
= pd
->stackblock
;
687 *stacksize
= stacktop
- *stack
;
688 #elif defined _STACK_GROWS_DOWN
690 #elif defined _STACK_GROWS_UP
691 *stack
= pd
->stackblock
;
701 __deallocate_stack (struct pthread
*pd
)
703 lll_lock (stack_cache_lock
, LLL_PRIVATE
);
705 /* Remove the thread from the list of threads with user defined
707 stack_list_del (&pd
->list
);
709 /* Not much to do. Just free the mmap()ed memory. Note that we do
710 not reset the 'used' flag in the 'tid' field. This is done by
711 the kernel. If no thread has been created yet this field is
713 if (__builtin_expect (! pd
->user_stack
, 1))
714 (void) queue_stack (pd
);
716 /* Free the memory associated with the ELF TLS. */
717 _dl_deallocate_tls (TLS_TPADJ (pd
), false);
719 lll_unlock (stack_cache_lock
, LLL_PRIVATE
);
725 __make_stacks_executable (void **stack_endp
)
727 /* First the main thread's stack. */
732 #ifdef NEED_SEPARATE_REGISTER_STACK
733 const size_t pagemask
= ~(__getpagesize () - 1);
736 lll_lock (stack_cache_lock
, LLL_PRIVATE
);
739 list_for_each (runp
, &stack_used
)
741 err
= change_stack_perm (list_entry (runp
, struct pthread
, list
)
742 #ifdef NEED_SEPARATE_REGISTER_STACK
750 /* Also change the permission for the currently unused stacks. This
751 might be wasted time but better spend it here than adding a check
754 list_for_each (runp
, &stack_cache
)
756 err
= change_stack_perm (list_entry (runp
, struct pthread
, list
)
757 #ifdef NEED_SEPARATE_REGISTER_STACK
765 lll_unlock (stack_cache_lock
, LLL_PRIVATE
);
771 /* In case of a fork() call the memory allocation in the child will be
772 the same but only one thread is running. All stacks except that of
773 the one running thread are not used anymore. We have to recycle
776 __reclaim_stacks (void)
778 struct pthread
*self
= (struct pthread
*) THREAD_SELF
;
780 /* No locking necessary. The caller is the only stack in use. But
781 we have to be aware that we might have interrupted a list
784 if (in_flight_stack
!= 0)
786 bool add_p
= in_flight_stack
& 1;
787 list_t
*elem
= (list_t
*)(uintptr_t)(in_flight_stack
& ~UINTMAX_C (1));
791 /* We always add at the beginning of the list. So in this
792 case we only need to check the beginning of these lists. */
793 int check_list (list_t
*l
)
795 if (l
->next
->prev
!= l
)
797 assert (l
->next
->prev
== elem
);
799 elem
->next
= l
->next
;
809 if (check_list (&stack_used
) == 0)
810 (void) check_list (&stack_cache
);
814 /* We can simply always replay the delete operation. */
815 elem
->next
->prev
= elem
->prev
;
816 elem
->prev
->next
= elem
->next
;
820 /* Mark all stacks except the still running one as free. */
822 list_for_each (runp
, &stack_used
)
824 struct pthread
*curp
= list_entry (runp
, struct pthread
, list
);
827 /* This marks the stack as free. */
830 /* Account for the size of the stack. */
831 stack_cache_actsize
+= curp
->stackblock_size
;
833 if (curp
->specific_used
)
835 /* Clear the thread-specific data. */
836 memset (curp
->specific_1stblock
, '\0',
837 sizeof (curp
->specific_1stblock
));
839 curp
->specific_used
= false;
842 for (cnt
= 1; cnt
< PTHREAD_KEY_1STLEVEL_SIZE
; ++cnt
)
843 if (curp
->specific
[cnt
] != NULL
)
845 memset (curp
->specific
[cnt
], '\0',
846 sizeof (curp
->specific_1stblock
));
848 /* We have allocated the block which we do not
849 free here so re-set the bit. */
850 curp
->specific_used
= true;
856 /* Add the stack of all running threads to the cache. */
857 list_splice (&stack_used
, &stack_cache
);
859 /* Remove the entry for the current thread to from the cache list
860 and add it to the list of running threads. Which of the two
861 lists is decided by the user_stack flag. */
862 stack_list_del (&self
->list
);
864 /* Re-initialize the lists for all the threads. */
865 INIT_LIST_HEAD (&stack_used
);
866 INIT_LIST_HEAD (&__stack_user
);
868 if (__builtin_expect (THREAD_GETMEM (self
, user_stack
), 0))
869 list_add (&self
->list
, &__stack_user
);
871 list_add (&self
->list
, &stack_used
);
873 /* There is one thread running. */
878 /* Initialize the lock. */
879 stack_cache_lock
= LLL_LOCK_INITIALIZER
;
885 setxid_mark_thread (struct xid_command
*cmdp
, struct pthread
*t
)
889 /* Don't let the thread exit before the setxid handler runs. */
894 ch
= t
->cancelhandling
;
896 /* If the thread is exiting right now, ignore it. */
897 if ((ch
& EXITING_BITMASK
) != 0)
900 while (atomic_compare_and_exchange_bool_acq (&t
->cancelhandling
,
901 ch
| SETXID_BITMASK
, ch
));
907 setxid_unmark_thread (struct xid_command
*cmdp
, struct pthread
*t
)
913 ch
= t
->cancelhandling
;
914 if ((ch
& SETXID_BITMASK
) == 0)
917 while (atomic_compare_and_exchange_bool_acq (&t
->cancelhandling
,
918 ch
& ~SETXID_BITMASK
, ch
));
920 /* Release the futex just in case. */
922 lll_futex_wake (&t
->setxid_futex
, 1, LLL_PRIVATE
);
928 setxid_signal_thread (struct xid_command
*cmdp
, struct pthread
*t
)
930 if ((t
->cancelhandling
& SETXID_BITMASK
) == 0)
934 pid_t pid
= getpid ();
935 INTERNAL_SYSCALL_DECL (err
);
936 val
= INTERNAL_SYSCALL (tgkill
, err
, 3, pid
, t
->tid
, SIGSETXID
);
938 /* If this failed, it must have had not started yet or else exited. */
939 if (!INTERNAL_SYSCALL_ERROR_P (val
, err
))
941 atomic_increment (&cmdp
->cntr
);
951 __nptl_setxid (struct xid_command
*cmdp
)
955 lll_lock (stack_cache_lock
, LLL_PRIVATE
);
960 struct pthread
*self
= THREAD_SELF
;
962 /* Iterate over the list with system-allocated threads first. */
964 list_for_each (runp
, &stack_used
)
966 struct pthread
*t
= list_entry (runp
, struct pthread
, list
);
970 setxid_mark_thread (cmdp
, t
);
973 /* Now the list with threads using user-allocated stacks. */
974 list_for_each (runp
, &__stack_user
)
976 struct pthread
*t
= list_entry (runp
, struct pthread
, list
);
980 setxid_mark_thread (cmdp
, t
);
983 /* Iterate until we don't succeed in signalling anyone. That means
984 we have gotten all running threads, and their children will be
985 automatically correct once started. */
990 list_for_each (runp
, &stack_used
)
992 struct pthread
*t
= list_entry (runp
, struct pthread
, list
);
996 signalled
+= setxid_signal_thread (cmdp
, t
);
999 list_for_each (runp
, &__stack_user
)
1001 struct pthread
*t
= list_entry (runp
, struct pthread
, list
);
1005 signalled
+= setxid_signal_thread (cmdp
, t
);
1008 int cur
= cmdp
->cntr
;
1011 lll_futex_wait (&cmdp
->cntr
, cur
, LLL_PRIVATE
);
1015 while (signalled
!= 0);
1017 /* Clean up flags, so that no thread blocks during exit waiting
1018 for a signal which will never come. */
1019 list_for_each (runp
, &stack_used
)
1021 struct pthread
*t
= list_entry (runp
, struct pthread
, list
);
1025 setxid_unmark_thread (cmdp
, t
);
1028 list_for_each (runp
, &__stack_user
)
1030 struct pthread
*t
= list_entry (runp
, struct pthread
, list
);
1034 setxid_unmark_thread (cmdp
, t
);
1037 /* This must be last, otherwise the current thread might not have
1038 permissions to send SIGSETXID syscall to the other threads. */
1039 INTERNAL_SYSCALL_DECL (err
);
1040 result
= INTERNAL_SYSCALL_NCS (cmdp
->syscall_no
, err
, 3,
1041 cmdp
->id
[0], cmdp
->id
[1], cmdp
->id
[2]);
1042 if (INTERNAL_SYSCALL_ERROR_P (result
, err
))
1044 __set_errno (INTERNAL_SYSCALL_ERRNO (result
, err
));
1048 lll_unlock (stack_cache_lock
, LLL_PRIVATE
);
1052 static inline void __attribute__((always_inline
))
1053 init_one_static_tls (struct pthread
*curp
, struct link_map
*map
)
1055 dtv_t
*dtv
= GET_DTV (TLS_TPADJ (curp
));
1056 # if defined(TLS_TCB_AT_TP)
1057 void *dest
= (char *) curp
- map
->l_tls_offset
;
1058 # elif defined(TLS_DTV_AT_TP)
1059 void *dest
= (char *) curp
+ map
->l_tls_offset
+ TLS_PRE_TCB_SIZE
;
1061 # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
1064 /* Fill in the DTV slot so that a later LD/GD access will find it. */
1065 dtv
[map
->l_tls_modid
].pointer
.val
= dest
;
1066 dtv
[map
->l_tls_modid
].pointer
.is_static
= true;
1068 /* Initialize the memory. */
1069 memset (mempcpy (dest
, map
->l_tls_initimage
, map
->l_tls_initimage_size
),
1070 '\0', map
->l_tls_blocksize
- map
->l_tls_initimage_size
);
1075 __pthread_init_static_tls (struct link_map
*map
)
1077 lll_lock (stack_cache_lock
, LLL_PRIVATE
);
1079 /* Iterate over the list with system-allocated threads first. */
1081 list_for_each (runp
, &stack_used
)
1082 init_one_static_tls (list_entry (runp
, struct pthread
, list
), map
);
1084 /* Now the list with threads using user-allocated stacks. */
1085 list_for_each (runp
, &__stack_user
)
1086 init_one_static_tls (list_entry (runp
, struct pthread
, list
), map
);
1088 lll_unlock (stack_cache_lock
, LLL_PRIVATE
);
1094 __wait_lookup_done (void)
1096 lll_lock (stack_cache_lock
, LLL_PRIVATE
);
1098 struct pthread
*self
= THREAD_SELF
;
1100 /* Iterate over the list with system-allocated threads first. */
1102 list_for_each (runp
, &stack_used
)
1104 struct pthread
*t
= list_entry (runp
, struct pthread
, list
);
1105 if (t
== self
|| t
->header
.gscope_flag
== THREAD_GSCOPE_FLAG_UNUSED
)
1108 int *const gscope_flagp
= &t
->header
.gscope_flag
;
1110 /* We have to wait until this thread is done with the global
1111 scope. First tell the thread that we are waiting and
1112 possibly have to be woken. */
1113 if (atomic_compare_and_exchange_bool_acq (gscope_flagp
,
1114 THREAD_GSCOPE_FLAG_WAIT
,
1115 THREAD_GSCOPE_FLAG_USED
))
1119 lll_futex_wait (gscope_flagp
, THREAD_GSCOPE_FLAG_WAIT
, LLL_PRIVATE
);
1120 while (*gscope_flagp
== THREAD_GSCOPE_FLAG_WAIT
);
1123 /* Now the list with threads using user-allocated stacks. */
1124 list_for_each (runp
, &__stack_user
)
1126 struct pthread
*t
= list_entry (runp
, struct pthread
, list
);
1127 if (t
== self
|| t
->header
.gscope_flag
== THREAD_GSCOPE_FLAG_UNUSED
)
1130 int *const gscope_flagp
= &t
->header
.gscope_flag
;
1132 /* We have to wait until this thread is done with the global
1133 scope. First tell the thread that we are waiting and
1134 possibly have to be woken. */
1135 if (atomic_compare_and_exchange_bool_acq (gscope_flagp
,
1136 THREAD_GSCOPE_FLAG_WAIT
,
1137 THREAD_GSCOPE_FLAG_USED
))
1141 lll_futex_wait (gscope_flagp
, THREAD_GSCOPE_FLAG_WAIT
, LLL_PRIVATE
);
1142 while (*gscope_flagp
== THREAD_GSCOPE_FLAG_WAIT
);
1145 lll_unlock (stack_cache_lock
, LLL_PRIVATE
);