support: Free gdb_script_name
[glibc.git] / nptl / nptl-stack.c
blob7853c105be30111a3f6b007ec4966836e3a5c742
1 /* Stack cache management for NPTL.
2 Copyright (C) 2002-2021 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <https://www.gnu.org/licenses/>. */
20 #include <nptl-stack.h>
21 #include <ldsodefs.h>
23 /* Maximum size in kB of cache. 40MiBi by default. */
24 static const size_t stack_cache_maxsize = 40 * 1024 * 1024;
26 void
27 __nptl_stack_list_del (list_t *elem)
29 GL (dl_in_flight_stack) = (uintptr_t) elem;
31 atomic_write_barrier ();
33 list_del (elem);
35 atomic_write_barrier ();
37 GL (dl_in_flight_stack) = 0;
39 libc_hidden_def (__nptl_stack_list_del)
41 void
42 __nptl_stack_list_add (list_t *elem, list_t *list)
44 GL (dl_in_flight_stack) = (uintptr_t) elem | 1;
46 atomic_write_barrier ();
48 list_add (elem, list);
50 atomic_write_barrier ();
52 GL (dl_in_flight_stack) = 0;
54 libc_hidden_def (__nptl_stack_list_add)
56 void
57 __nptl_free_stacks (size_t limit)
59 /* We reduce the size of the cache. Remove the last entries until
60 the size is below the limit. */
61 list_t *entry;
62 list_t *prev;
64 /* Search from the end of the list. */
65 list_for_each_prev_safe (entry, prev, &GL (dl_stack_cache))
67 struct pthread *curr;
69 curr = list_entry (entry, struct pthread, list);
70 if (__nptl_stack_in_use (curr))
72 /* Unlink the block. */
73 __nptl_stack_list_del (entry);
75 /* Account for the freed memory. */
76 GL (dl_stack_cache_actsize) -= curr->stackblock_size;
78 /* Free the memory associated with the ELF TLS. */
79 _dl_deallocate_tls (TLS_TPADJ (curr), false);
81 /* Remove this block. This should never fail. If it does
82 something is really wrong. */
83 if (__munmap (curr->stackblock, curr->stackblock_size) != 0)
84 abort ();
86 /* Maybe we have freed enough. */
87 if (GL (dl_stack_cache_actsize) <= limit)
88 break;
93 /* Add a stack frame which is not used anymore to the stack. Must be
94 called with the cache lock held. */
95 static inline void
96 __attribute ((always_inline))
97 queue_stack (struct pthread *stack)
99 /* We unconditionally add the stack to the list. The memory may
100 still be in use but it will not be reused until the kernel marks
101 the stack as not used anymore. */
102 __nptl_stack_list_add (&stack->list, &GL (dl_stack_cache));
104 GL (dl_stack_cache_actsize) += stack->stackblock_size;
105 if (__glibc_unlikely (GL (dl_stack_cache_actsize) > stack_cache_maxsize))
106 __nptl_free_stacks (stack_cache_maxsize);
109 void
110 __nptl_deallocate_stack (struct pthread *pd)
112 lll_lock (GL (dl_stack_cache_lock), LLL_PRIVATE);
114 /* Remove the thread from the list of threads with user defined
115 stacks. */
116 __nptl_stack_list_del (&pd->list);
118 /* Not much to do. Just free the mmap()ed memory. Note that we do
119 not reset the 'used' flag in the 'tid' field. This is done by
120 the kernel. If no thread has been created yet this field is
121 still zero. */
122 if (__glibc_likely (! pd->user_stack))
123 (void) queue_stack (pd);
124 else
125 /* Free the memory associated with the ELF TLS. */
126 _dl_deallocate_tls (TLS_TPADJ (pd), false);
128 lll_unlock (GL (dl_stack_cache_lock), LLL_PRIVATE);
130 libc_hidden_def (__nptl_deallocate_stack)