1 /* Copyright (C) 2002, 2003 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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
26 #include <sys/param.h>
27 #include <dl-sysdep.h>
32 #ifndef NEED_SEPARATE_REGISTER_STACK
34 /* Most architectures have exactly one stack pointer. Some have more. */
35 #define STACK_VARIABLES void *stackaddr
37 /* How to pass the values to the 'create_thread' function. */
38 #define STACK_VARIABLES_ARGS stackaddr
40 /* How to declare function which gets there parameters. */
41 #define STACK_VARIABLES_PARMS void *stackaddr
43 /* How to declare allocate_stack. */
44 #define ALLOCATE_STACK_PARMS void **stack
46 /* This is how the function is called. We do it this way to allow
47 other variants of the function to have more parameters. */
48 #define ALLOCATE_STACK(attr, pd) allocate_stack (attr, pd, &stackaddr)
52 #define STACK_VARIABLES void *stackaddr; size_t stacksize
53 #define STACK_VARIABLES_ARGS stackaddr, stacksize
54 #define STACK_VARIABLES_PARMS void *stackaddr, size_t stacksize
55 #define ALLOCATE_STACK_PARMS void **stack, size_t *stacksize
56 #define ALLOCATE_STACK(attr, pd) \
57 allocate_stack (attr, pd, &stackaddr, &stacksize)
62 /* Default alignment of stack. */
64 # define STACK_ALIGN __alignof__ (long double)
67 /* Default value for minimal stack size after allocating thread
68 descriptor and guard. */
69 #ifndef MINIMAL_REST_STACK
70 # define MINIMAL_REST_STACK 4096
74 /* Let the architecture add some flags to the mmap() call used to
76 #ifndef ARCH_MAP_FLAGS
77 # define ARCH_MAP_FLAGS 0
80 /* This yields the pointer that TLS support code calls the thread pointer. */
82 # define TLS_TPADJ(pd) (pd)
84 # define TLS_TPADJ(pd) ((struct pthread *)((char *) (pd) + TLS_PRE_TCB_SIZE))
87 /* Cache handling for not-yet free stacks. */
89 /* Maximum size in kB of cache. */
90 static size_t stack_cache_maxsize = 40 * 1024 * 1024; /* 40MiBi by default. */
91 static size_t stack_cache_actsize;
93 /* Mutex protecting this variable. */
94 static lll_lock_t stack_cache_lock = LLL_LOCK_INITIALIZER;
96 /* List of queued stack frames. */
97 static LIST_HEAD (stack_cache);
99 /* List of the stacks in use. */
100 static LIST_HEAD (stack_used);
102 /* List of the threads with user provided stacks in use. No need to
103 initialize this, since it's done in __pthread_initialize_minimal. */
104 list_t __stack_user __attribute__ ((nocommon));
105 hidden_data_def (__stack_user)
107 #if COLORING_INCREMENT != 0
108 /* Number of threads created. */
109 static unsigned int nptl_ncreated;
113 /* Check whether the stack is still used or not. */
114 #define FREE_P(descr) ((descr)->tid == 0)
117 /* We create a double linked list of all cache entries. Double linked
118 because this allows removing entries from the end. */
121 /* Get a stack frame from the cache. We have to match by size since
122 some blocks might be too small or far too large. */
123 static struct pthread *
124 get_cached_stack (size_t *sizep, void **memp)
126 size_t size = *sizep;
127 struct pthread *result = NULL;
130 lll_lock (stack_cache_lock);
132 /* Search the cache for a matching entry. We search for the
133 smallest stack which has at least the required size. Note that
134 in normal situations the size of all allocated stacks is the
135 same. As the very least there are only a few different sizes.
136 Therefore this loop will exit early most of the time with an
138 list_for_each (entry, &stack_cache)
140 struct pthread *curr;
142 curr = list_entry (entry, struct pthread, list);
143 if (FREE_P (curr) && curr->stackblock_size >= size)
145 if (curr->stackblock_size == size)
152 || result->stackblock_size > curr->stackblock_size)
157 if (__builtin_expect (result == NULL, 0)
158 /* Make sure the size difference is not too excessive. In that
159 case we do not use the block. */
160 || __builtin_expect (result->stackblock_size > 4 * size, 0))
162 /* Release the lock. */
163 lll_unlock (stack_cache_lock);
168 /* Dequeue the entry. */
169 list_del (&result->list);
171 /* And add to the list of stacks in use. */
172 list_add (&result->list, &stack_used);
174 /* And decrease the cache size. */
175 stack_cache_actsize -= result->stackblock_size;
177 /* Release the lock early. */
178 lll_unlock (stack_cache_lock);
180 /* Report size and location of the stack to the caller. */
181 *sizep = result->stackblock_size;
182 *memp = result->stackblock;
184 /* Cancellation handling is back to the default. */
185 result->cancelhandling = 0;
186 result->cleanup = NULL;
188 /* No pending event. */
189 result->nextevent = NULL;
192 dtv_t *dtv = GET_DTV (TLS_TPADJ (result));
193 memset (dtv, '\0', (dtv[-1].counter + 1) * sizeof (dtv_t));
195 /* Re-initialize the TLS. */
196 _dl_allocate_tls_init (TLS_TPADJ (result));
202 /* Add a stack frame which is not used anymore to the stack. Must be
203 called with the cache lock held. */
205 queue_stack (struct pthread *stack)
207 /* We unconditionally add the stack to the list. The memory may
208 still be in use but it will not be reused until the kernel marks
209 the stack as not used anymore. */
210 list_add (&stack->list, &stack_cache);
212 stack_cache_actsize += stack->stackblock_size;
213 if (__builtin_expect (stack_cache_actsize > stack_cache_maxsize, 0))
215 /* We reduce the size of the cache. Remove the last entries
216 until the size is below the limit. */
220 /* Search from the end of the list. */
221 list_for_each_prev_safe (entry, prev, &stack_cache)
223 struct pthread *curr;
225 curr = list_entry (entry, struct pthread, list);
228 /* Unlink the block. */
231 /* Account for the freed memory. */
232 stack_cache_actsize -= curr->stackblock_size;
234 /* Free the memory associated with the ELF TLS. */
235 _dl_deallocate_tls (TLS_TPADJ (curr), false);
237 /* Remove this block. This should never fail. If it
238 does something is really wrong. */
239 if (munmap (curr->stackblock, curr->stackblock_size) != 0)
242 /* Maybe we have freed enough. */
243 if (stack_cache_actsize <= stack_cache_maxsize)
253 allocate_stack (const struct pthread_attr *attr, struct pthread **pdp,
254 ALLOCATE_STACK_PARMS)
258 size_t pagesize_m1 = __getpagesize () - 1;
261 assert (attr != NULL);
262 assert (powerof2 (pagesize_m1 + 1));
263 assert (TCB_ALIGNMENT >= STACK_ALIGN);
265 /* Get the stack size from the attribute if it is set. Otherwise we
266 use the default we determined at start time. */
267 size = attr->stacksize ?: __default_stacksize;
269 /* Get memory for the stack. */
270 if (__builtin_expect (attr->flags & ATTR_FLAG_STACKADDR, 0))
274 /* If the user also specified the size of the stack make sure it
276 if (attr->stacksize != 0
277 && attr->stacksize < (__static_tls_size + MINIMAL_REST_STACK))
280 /* Adjust stack size for alignment of the TLS block. */
282 adj = ((uintptr_t) attr->stackaddr - TLS_TCB_SIZE)
283 & __static_tls_align_m1;
284 assert (size > adj + TLS_TCB_SIZE);
286 adj = ((uintptr_t) attr->stackaddr - __static_tls_size)
287 & __static_tls_align_m1;
291 /* The user provided some memory. Let's hope it matches the
292 size... We do not allocate guard pages if the user provided
293 the stack. It is the user's responsibility to do this if it
296 pd = (struct pthread *) ((uintptr_t) attr->stackaddr
297 - TLS_TCB_SIZE - adj);
299 pd = (struct pthread *) (((uintptr_t) attr->stackaddr
300 - __static_tls_size - adj)
304 /* The user provided stack memory needs to be cleared. */
305 memset (pd, '\0', sizeof (struct pthread));
307 /* The first TSD block is included in the TCB. */
308 pd->specific[0] = pd->specific_1stblock;
310 #if LLL_LOCK_INITIALIZER != 0
311 /* Initialize the lock. */
312 pd->lock = LLL_LOCK_INITIALIZER;
315 /* Remember the stack-related values. */
316 pd->stackblock = (char *) attr->stackaddr - size;
317 pd->stackblock_size = size;
319 /* This is a user-provided stack. It will not be queued in the
320 stack cache nor will the memory (except the TLS memory) be freed. */
321 pd->user_stack = true;
323 #ifdef TLS_MULTIPLE_THREADS_IN_TCB
324 /* This is at least the second thread. */
325 pd->header.multiple_threads = 1;
327 __pthread_multiple_threads = *__libc_multiple_threads_ptr = 1;
330 #ifdef NEED_DL_SYSINFO
331 /* Copy the sysinfo value from the parent. */
332 pd->header.sysinfo = THREAD_GETMEM (THREAD_SELF, header.sysinfo);
335 /* Allocate the DTV for this thread. */
336 if (_dl_allocate_tls (TLS_TPADJ (pd)) == NULL)
337 /* Something went wrong. */
341 /* Prepare to modify global data. */
342 lll_lock (stack_cache_lock);
344 /* And add to the list of stacks in use. */
345 list_add (&pd->list, &__stack_user);
347 lll_unlock (stack_cache_lock);
351 /* Allocate some anonymous memory. If possible use the
357 #if COLORING_INCREMENT != 0
358 /* Add one more page for stack coloring. Don't do it for stacks
359 with 16 times pagesize or larger. This might just cause
360 unnecessary misalignment. */
361 if (size <= 16 * pagesize_m1)
362 size += pagesize_m1 + 1;
365 /* Adjust the stack size for alignment. */
366 size &= ~__static_tls_align_m1;
369 /* Make sure the size of the stack is enough for the guard and
370 eventually the thread descriptor. */
371 guardsize = (attr->guardsize + pagesize_m1) & ~pagesize_m1;
372 if (__builtin_expect (size < (guardsize + __static_tls_size
373 + MINIMAL_REST_STACK + pagesize_m1 + 1),
375 /* The stack is too small (or the guard too large). */
378 /* Try to get a stack from the cache. */
380 pd = get_cached_stack (&size, &mem);
383 /* To avoid aliasing effects on a larger scale then pages we
384 adjust the allocated stack size if necessary. This way
385 allocations directly following each other will not have
386 aliasing problems. */
387 #if MULTI_PAGE_ALIASING != 0
388 if ((size % MULTI_PAGE_ALIASING) == 0)
389 size += pagesize_m1 + 1;
392 mem = mmap (NULL, size, PROT_READ | PROT_WRITE | PROT_EXEC,
393 MAP_PRIVATE | MAP_ANONYMOUS | ARCH_MAP_FLAGS, -1, 0);
395 if (__builtin_expect (mem == MAP_FAILED, 0))
397 #ifdef ARCH_RETRY_MMAP
398 mem = ARCH_RETRY_MMAP (size);
399 if (__builtin_expect (mem == MAP_FAILED, 0))
404 /* SIZE is guaranteed to be greater than zero.
405 So we can never get a null pointer back from mmap. */
406 assert (mem != NULL);
408 #if COLORING_INCREMENT != 0
409 /* Atomically increment NCREATED. */
410 unsigned int ncreated = (atomic_exchange_and_add (&nptl_ncreated, 1)
413 /* We chose the offset for coloring by incrementing it for
414 every new thread by a fixed amount. The offset used
415 module the page size. Even if coloring would be better
416 relative to higher alignment values it makes no sense to
417 do it since the mmap() interface does not allow us to
418 specify any alignment for the returned memory block. */
419 size_t coloring = (ncreated * COLORING_INCREMENT) & pagesize_m1;
421 /* Make sure the coloring offsets does not disturb the alignment
422 of the TCB and static TLS block. */
423 if (__builtin_expect ((coloring & __static_tls_align_m1) != 0, 0))
424 coloring = (((coloring + __static_tls_align_m1)
425 & ~(__static_tls_align_m1))
428 /* Unless specified we do not make any adjustments. */
432 /* Place the thread descriptor at the end of the stack. */
434 pd = (struct pthread *) ((char *) mem + size - coloring) - 1;
436 pd = (struct pthread *) ((((uintptr_t) mem + size - coloring
438 & ~__static_tls_align_m1)
442 /* Remember the stack-related values. */
443 pd->stackblock = mem;
444 pd->stackblock_size = size;
446 /* We allocated the first block thread-specific data array.
447 This address will not change for the lifetime of this
449 pd->specific[0] = pd->specific_1stblock;
451 #if LLL_LOCK_INITIALIZER != 0
452 /* Initialize the lock. */
453 pd->lock = LLL_LOCK_INITIALIZER;
456 #ifdef TLS_MULTIPLE_THREADS_IN_TCB
457 /* This is at least the second thread. */
458 pd->header.multiple_threads = 1;
460 __pthread_multiple_threads = *__libc_multiple_threads_ptr = 1;
463 #ifdef NEED_DL_SYSINFO
464 /* Copy the sysinfo value from the parent. */
465 pd->header.sysinfo = THREAD_GETMEM (THREAD_SELF, header.sysinfo);
468 /* Allocate the DTV for this thread. */
469 if (_dl_allocate_tls (TLS_TPADJ (pd)) == NULL)
471 /* Something went wrong. */
474 /* Free the stack memory we just allocated. */
475 (void) munmap (mem, size);
481 /* Prepare to modify global data. */
482 lll_lock (stack_cache_lock);
484 /* And add to the list of stacks in use. */
485 list_add (&pd->list, &stack_used);
487 lll_unlock (stack_cache_lock);
490 /* Note that all of the stack and the thread descriptor is
491 zeroed. This means we do not have to initialize fields
492 with initial value zero. This is specifically true for
493 the 'tid' field which is always set back to zero once the
494 stack is not used anymore and for the 'guardsize' field
495 which will be read next. */
498 /* Create or resize the guard area if necessary. */
499 if (__builtin_expect (guardsize > pd->guardsize, 0))
501 #ifdef NEED_SEPARATE_REGISTER_STACK
502 char *guard = mem + (((size - guardsize) / 2) & ~pagesize_m1);
506 if (mprotect (guard, guardsize, PROT_NONE) != 0)
512 lll_lock (stack_cache_lock);
514 /* Remove the thread from the list. */
515 list_del (&pd->list);
517 lll_unlock (stack_cache_lock);
519 /* Get rid of the TLS block we allocated. */
520 _dl_deallocate_tls (TLS_TPADJ (pd), false);
522 /* Free the stack memory regardless of whether the size
523 of the cache is over the limit or not. If this piece
524 of memory caused problems we better do not use it
525 anymore. Uh, and we ignore possible errors. There
526 is nothing we could do. */
527 (void) munmap (mem, size);
532 pd->guardsize = guardsize;
534 else if (__builtin_expect (pd->guardsize - guardsize > size - reqsize,
537 /* The old guard area is too large. */
539 #ifdef NEED_SEPARATE_REGISTER_STACK
540 char *guard = mem + (((size - guardsize) / 2) & ~pagesize_m1);
541 char *oldguard = mem + (((size - pd->guardsize) / 2) & ~pagesize_m1);
544 && mprotect (oldguard, guard - oldguard,
545 PROT_READ | PROT_WRITE | PROT_EXEC) != 0)
548 if (mprotect (guard + guardsize,
549 oldguard + pd->guardsize - guard - guardsize,
550 PROT_READ | PROT_WRITE | PROT_EXEC) != 0)
553 if (mprotect ((char *) mem + guardsize, pd->guardsize - guardsize,
554 PROT_READ | PROT_WRITE | PROT_EXEC) != 0)
558 pd->guardsize = guardsize;
562 /* We place the thread descriptor at the end of the stack. */
566 /* The stack begins before the TCB and the static TLS block. */
567 stacktop = ((char *) (pd + 1) - __static_tls_size);
569 stacktop = (char *) (pd - 1);
572 #ifdef NEED_SEPARATE_REGISTER_STACK
573 *stack = pd->stackblock;
574 *stacksize = stacktop - *stack;
585 __deallocate_stack (struct pthread *pd)
587 lll_lock (stack_cache_lock);
589 /* Remove the thread from the list of threads with user defined
591 list_del (&pd->list);
593 /* Not much to do. Just free the mmap()ed memory. Note that we do
594 not reset the 'used' flag in the 'tid' field. This is done by
595 the kernel. If no thread has been created yet this field is
597 if (__builtin_expect (! pd->user_stack, 1))
598 (void) queue_stack (pd);
600 /* Free the memory associated with the ELF TLS. */
601 _dl_deallocate_tls (TLS_TPADJ (pd), false);
603 lll_unlock (stack_cache_lock);
607 /* In case of a fork() call the memory allocation in the child will be
608 the same but only one thread is running. All stacks except that of
609 the one running thread are not used anymore. We have to recycle
612 __reclaim_stacks (void)
614 struct pthread *self = (struct pthread *) THREAD_SELF;
616 /* No locking necessary. The caller is the only stack in use. */
618 /* Mark all stacks except the still running one as free. */
620 list_for_each (runp, &stack_used)
622 struct pthread *curp;
624 curp = list_entry (runp, struct pthread, list);
627 /* This marks the stack as free. */
630 /* Account for the size of the stack. */
631 stack_cache_actsize += curp->stackblock_size;
635 /* Add the stack of all running threads to the cache. */
636 list_splice (&stack_used, &stack_cache);
638 /* Remove the entry for the current thread to from the cache list
639 and add it to the list of running threads. Which of the two
640 lists is decided by the user_stack flag. */
641 list_del (&self->list);
643 /* Re-initialize the lists for all the threads. */
644 INIT_LIST_HEAD (&stack_used);
645 INIT_LIST_HEAD (&__stack_user);
647 if (__builtin_expect (THREAD_GETMEM (self, user_stack), 0))
648 list_add (&self->list, &__stack_user);
650 list_add (&self->list, &stack_used);
652 /* There is one thread running. */
655 /* Initialize the lock. */
656 stack_cache_lock = LLL_LOCK_INITIALIZER;