| 1 | /* Malloc implementation for multiple threads without lock contention. |
| 2 | Copyright (C) 2001-2017 Free Software Foundation, Inc. |
| 3 | This file is part of the GNU C Library. |
| 4 | Contributed by Wolfram Gloger <wg@malloc.de>, 2001. |
| 5 | |
| 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 License as |
| 8 | published by the Free Software Foundation; either version 2.1 of the |
| 9 | License, or (at your option) any later version. |
| 10 | |
| 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. |
| 15 | |
| 16 | You should have received a copy of the GNU Lesser General Public |
| 17 | License along with the GNU C Library; see the file COPYING.LIB. If |
| 18 | not, see <http://www.gnu.org/licenses/>. */ |
| 19 | |
| 20 | #include <stdbool.h> |
| 21 | |
| 22 | #if HAVE_TUNABLES |
| 23 | # define TUNABLE_NAMESPACE malloc |
| 24 | #endif |
| 25 | #include <elf/dl-tunables.h> |
| 26 | |
| 27 | /* Compile-time constants. */ |
| 28 | |
| 29 | #define HEAP_MIN_SIZE (32 * 1024) |
| 30 | #ifndef HEAP_MAX_SIZE |
| 31 | # ifdef DEFAULT_MMAP_THRESHOLD_MAX |
| 32 | # define HEAP_MAX_SIZE (2 * DEFAULT_MMAP_THRESHOLD_MAX) |
| 33 | # else |
| 34 | # define HEAP_MAX_SIZE (1024 * 1024) /* must be a power of two */ |
| 35 | # endif |
| 36 | #endif |
| 37 | |
| 38 | /* HEAP_MIN_SIZE and HEAP_MAX_SIZE limit the size of mmap()ed heaps |
| 39 | that are dynamically created for multi-threaded programs. The |
| 40 | maximum size must be a power of two, for fast determination of |
| 41 | which heap belongs to a chunk. It should be much larger than the |
| 42 | mmap threshold, so that requests with a size just below that |
| 43 | threshold can be fulfilled without creating too many heaps. */ |
| 44 | |
| 45 | /***************************************************************************/ |
| 46 | |
| 47 | #define top(ar_ptr) ((ar_ptr)->top) |
| 48 | |
| 49 | /* A heap is a single contiguous memory region holding (coalesceable) |
| 50 | malloc_chunks. It is allocated with mmap() and always starts at an |
| 51 | address aligned to HEAP_MAX_SIZE. */ |
| 52 | |
| 53 | typedef struct _heap_info |
| 54 | { |
| 55 | mstate ar_ptr; /* Arena for this heap. */ |
| 56 | struct _heap_info *prev; /* Previous heap. */ |
| 57 | size_t size; /* Current size in bytes. */ |
| 58 | size_t mprotect_size; /* Size in bytes that has been mprotected |
| 59 | PROT_READ|PROT_WRITE. */ |
| 60 | /* Make sure the following data is properly aligned, particularly |
| 61 | that sizeof (heap_info) + 2 * SIZE_SZ is a multiple of |
| 62 | MALLOC_ALIGNMENT. */ |
| 63 | char pad[-6 * SIZE_SZ & MALLOC_ALIGN_MASK]; |
| 64 | } heap_info; |
| 65 | |
| 66 | /* Get a compile-time error if the heap_info padding is not correct |
| 67 | to make alignment work as expected in sYSMALLOc. */ |
| 68 | extern int sanity_check_heap_info_alignment[(sizeof (heap_info) |
| 69 | + 2 * SIZE_SZ) % MALLOC_ALIGNMENT |
| 70 | ? -1 : 1]; |
| 71 | |
| 72 | /* Thread specific data. */ |
| 73 | |
| 74 | static __thread mstate thread_arena attribute_tls_model_ie; |
| 75 | |
| 76 | /* Arena free list. free_list_lock synchronizes access to the |
| 77 | free_list variable below, and the next_free and attached_threads |
| 78 | members of struct malloc_state objects. No other locks must be |
| 79 | acquired after free_list_lock has been acquired. */ |
| 80 | |
| 81 | __libc_lock_define_initialized (static, free_list_lock); |
| 82 | static size_t narenas = 1; |
| 83 | static mstate free_list; |
| 84 | |
| 85 | /* list_lock prevents concurrent writes to the next member of struct |
| 86 | malloc_state objects. |
| 87 | |
| 88 | Read access to the next member is supposed to synchronize with the |
| 89 | atomic_write_barrier and the write to the next member in |
| 90 | _int_new_arena. This suffers from data races; see the FIXME |
| 91 | comments in _int_new_arena and reused_arena. |
| 92 | |
| 93 | list_lock also prevents concurrent forks. At the time list_lock is |
| 94 | acquired, no arena lock must have been acquired, but it is |
| 95 | permitted to acquire arena locks subsequently, while list_lock is |
| 96 | acquired. */ |
| 97 | __libc_lock_define_initialized (static, list_lock); |
| 98 | |
| 99 | /* Already initialized? */ |
| 100 | int __malloc_initialized = -1; |
| 101 | |
| 102 | /**************************************************************************/ |
| 103 | |
| 104 | |
| 105 | /* arena_get() acquires an arena and locks the corresponding mutex. |
| 106 | First, try the one last locked successfully by this thread. (This |
| 107 | is the common case and handled with a macro for speed.) Then, loop |
| 108 | once over the circularly linked list of arenas. If no arena is |
| 109 | readily available, create a new one. In this latter case, `size' |
| 110 | is just a hint as to how much memory will be required immediately |
| 111 | in the new arena. */ |
| 112 | |
| 113 | #define arena_get(ptr, size) do { \ |
| 114 | ptr = thread_arena; \ |
| 115 | arena_lock (ptr, size); \ |
| 116 | } while (0) |
| 117 | |
| 118 | #define arena_lock(ptr, size) do { \ |
| 119 | if (ptr && !arena_is_corrupt (ptr)) \ |
| 120 | __libc_lock_lock (ptr->mutex); \ |
| 121 | else \ |
| 122 | ptr = arena_get2 ((size), NULL); \ |
| 123 | } while (0) |
| 124 | |
| 125 | /* find the heap and corresponding arena for a given ptr */ |
| 126 | |
| 127 | #define heap_for_ptr(ptr) \ |
| 128 | ((heap_info *) ((unsigned long) (ptr) & ~(HEAP_MAX_SIZE - 1))) |
| 129 | #define arena_for_chunk(ptr) \ |
| 130 | (chunk_main_arena (ptr) ? &main_arena : heap_for_ptr (ptr)->ar_ptr) |
| 131 | |
| 132 | |
| 133 | /**************************************************************************/ |
| 134 | |
| 135 | /* atfork support. */ |
| 136 | |
| 137 | /* The following three functions are called around fork from a |
| 138 | multi-threaded process. We do not use the general fork handler |
| 139 | mechanism to make sure that our handlers are the last ones being |
| 140 | called, so that other fork handlers can use the malloc |
| 141 | subsystem. */ |
| 142 | |
| 143 | void |
| 144 | internal_function |
| 145 | __malloc_fork_lock_parent (void) |
| 146 | { |
| 147 | if (__malloc_initialized < 1) |
| 148 | return; |
| 149 | |
| 150 | /* We do not acquire free_list_lock here because we completely |
| 151 | reconstruct free_list in __malloc_fork_unlock_child. */ |
| 152 | |
| 153 | __libc_lock_lock (list_lock); |
| 154 | |
| 155 | for (mstate ar_ptr = &main_arena;; ) |
| 156 | { |
| 157 | __libc_lock_lock (ar_ptr->mutex); |
| 158 | ar_ptr = ar_ptr->next; |
| 159 | if (ar_ptr == &main_arena) |
| 160 | break; |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | void |
| 165 | internal_function |
| 166 | __malloc_fork_unlock_parent (void) |
| 167 | { |
| 168 | if (__malloc_initialized < 1) |
| 169 | return; |
| 170 | |
| 171 | for (mstate ar_ptr = &main_arena;; ) |
| 172 | { |
| 173 | __libc_lock_unlock (ar_ptr->mutex); |
| 174 | ar_ptr = ar_ptr->next; |
| 175 | if (ar_ptr == &main_arena) |
| 176 | break; |
| 177 | } |
| 178 | __libc_lock_unlock (list_lock); |
| 179 | } |
| 180 | |
| 181 | void |
| 182 | internal_function |
| 183 | __malloc_fork_unlock_child (void) |
| 184 | { |
| 185 | if (__malloc_initialized < 1) |
| 186 | return; |
| 187 | |
| 188 | /* Push all arenas to the free list, except thread_arena, which is |
| 189 | attached to the current thread. */ |
| 190 | __libc_lock_init (free_list_lock); |
| 191 | if (thread_arena != NULL) |
| 192 | thread_arena->attached_threads = 1; |
| 193 | free_list = NULL; |
| 194 | for (mstate ar_ptr = &main_arena;; ) |
| 195 | { |
| 196 | __libc_lock_init (ar_ptr->mutex); |
| 197 | if (ar_ptr != thread_arena) |
| 198 | { |
| 199 | /* This arena is no longer attached to any thread. */ |
| 200 | ar_ptr->attached_threads = 0; |
| 201 | ar_ptr->next_free = free_list; |
| 202 | free_list = ar_ptr; |
| 203 | } |
| 204 | ar_ptr = ar_ptr->next; |
| 205 | if (ar_ptr == &main_arena) |
| 206 | break; |
| 207 | } |
| 208 | |
| 209 | __libc_lock_init (list_lock); |
| 210 | } |
| 211 | |
| 212 | #if HAVE_TUNABLES |
| 213 | static inline int do_set_mallopt_check (int32_t value); |
| 214 | void |
| 215 | DL_TUNABLE_CALLBACK (set_mallopt_check) (void *valp) |
| 216 | { |
| 217 | int32_t value = *(int32_t *) valp; |
| 218 | do_set_mallopt_check (value); |
| 219 | if (check_action != 0) |
| 220 | __malloc_check_init (); |
| 221 | } |
| 222 | |
| 223 | # define DL_TUNABLE_CALLBACK_FNDECL(__name, __type) \ |
| 224 | static inline int do_ ## __name (__type value); \ |
| 225 | void \ |
| 226 | DL_TUNABLE_CALLBACK (__name) (void *valp) \ |
| 227 | { \ |
| 228 | __type value = *(__type *) valp; \ |
| 229 | do_ ## __name (value); \ |
| 230 | } |
| 231 | |
| 232 | DL_TUNABLE_CALLBACK_FNDECL (set_mmap_threshold, size_t) |
| 233 | DL_TUNABLE_CALLBACK_FNDECL (set_mmaps_max, int32_t) |
| 234 | DL_TUNABLE_CALLBACK_FNDECL (set_top_pad, size_t) |
| 235 | DL_TUNABLE_CALLBACK_FNDECL (set_perturb_byte, int32_t) |
| 236 | DL_TUNABLE_CALLBACK_FNDECL (set_trim_threshold, size_t) |
| 237 | DL_TUNABLE_CALLBACK_FNDECL (set_arena_max, size_t) |
| 238 | DL_TUNABLE_CALLBACK_FNDECL (set_arena_test, size_t) |
| 239 | #else |
| 240 | /* Initialization routine. */ |
| 241 | #include <string.h> |
| 242 | extern char **_environ; |
| 243 | |
| 244 | static char * |
| 245 | internal_function |
| 246 | next_env_entry (char ***position) |
| 247 | { |
| 248 | char **current = *position; |
| 249 | char *result = NULL; |
| 250 | |
| 251 | while (*current != NULL) |
| 252 | { |
| 253 | if (__builtin_expect ((*current)[0] == 'M', 0) |
| 254 | && (*current)[1] == 'A' |
| 255 | && (*current)[2] == 'L' |
| 256 | && (*current)[3] == 'L' |
| 257 | && (*current)[4] == 'O' |
| 258 | && (*current)[5] == 'C' |
| 259 | && (*current)[6] == '_') |
| 260 | { |
| 261 | result = &(*current)[7]; |
| 262 | |
| 263 | /* Save current position for next visit. */ |
| 264 | *position = ++current; |
| 265 | |
| 266 | break; |
| 267 | } |
| 268 | |
| 269 | ++current; |
| 270 | } |
| 271 | |
| 272 | return result; |
| 273 | } |
| 274 | #endif |
| 275 | |
| 276 | |
| 277 | #ifdef SHARED |
| 278 | static void * |
| 279 | __failing_morecore (ptrdiff_t d) |
| 280 | { |
| 281 | return (void *) MORECORE_FAILURE; |
| 282 | } |
| 283 | |
| 284 | extern struct dl_open_hook *_dl_open_hook; |
| 285 | libc_hidden_proto (_dl_open_hook); |
| 286 | #endif |
| 287 | |
| 288 | static void |
| 289 | ptmalloc_init (void) |
| 290 | { |
| 291 | if (__malloc_initialized >= 0) |
| 292 | return; |
| 293 | |
| 294 | __malloc_initialized = 0; |
| 295 | |
| 296 | #ifdef SHARED |
| 297 | /* In case this libc copy is in a non-default namespace, never use brk. |
| 298 | Likewise if dlopened from statically linked program. */ |
| 299 | Dl_info di; |
| 300 | struct link_map *l; |
| 301 | |
| 302 | if (_dl_open_hook != NULL |
| 303 | || (_dl_addr (ptmalloc_init, &di, &l, NULL) != 0 |
| 304 | && l->l_ns != LM_ID_BASE)) |
| 305 | __morecore = __failing_morecore; |
| 306 | #endif |
| 307 | |
| 308 | thread_arena = &main_arena; |
| 309 | |
| 310 | #if HAVE_TUNABLES |
| 311 | /* Ensure initialization/consolidation and do it under a lock so that a |
| 312 | thread attempting to use the arena in parallel waits on us till we |
| 313 | finish. */ |
| 314 | __libc_lock_lock (main_arena.mutex); |
| 315 | malloc_consolidate (&main_arena); |
| 316 | |
| 317 | TUNABLE_SET_VAL_WITH_CALLBACK (check, NULL, set_mallopt_check); |
| 318 | TUNABLE_SET_VAL_WITH_CALLBACK (top_pad, NULL, set_top_pad); |
| 319 | TUNABLE_SET_VAL_WITH_CALLBACK (perturb, NULL, set_perturb_byte); |
| 320 | TUNABLE_SET_VAL_WITH_CALLBACK (mmap_threshold, NULL, set_mmap_threshold); |
| 321 | TUNABLE_SET_VAL_WITH_CALLBACK (trim_threshold, NULL, set_trim_threshold); |
| 322 | TUNABLE_SET_VAL_WITH_CALLBACK (mmap_max, NULL, set_mmaps_max); |
| 323 | TUNABLE_SET_VAL_WITH_CALLBACK (arena_max, NULL, set_arena_max); |
| 324 | TUNABLE_SET_VAL_WITH_CALLBACK (arena_test, NULL, set_arena_test); |
| 325 | __libc_lock_unlock (main_arena.mutex); |
| 326 | #else |
| 327 | const char *s = NULL; |
| 328 | if (__glibc_likely (_environ != NULL)) |
| 329 | { |
| 330 | char **runp = _environ; |
| 331 | char *envline; |
| 332 | |
| 333 | while (__builtin_expect ((envline = next_env_entry (&runp)) != NULL, |
| 334 | 0)) |
| 335 | { |
| 336 | size_t len = strcspn (envline, "=" ); |
| 337 | |
| 338 | if (envline[len] != '=') |
| 339 | /* This is a "MALLOC_" variable at the end of the string |
| 340 | without a '=' character. Ignore it since otherwise we |
| 341 | will access invalid memory below. */ |
| 342 | continue; |
| 343 | |
| 344 | switch (len) |
| 345 | { |
| 346 | case 6: |
| 347 | if (memcmp (envline, "CHECK_" , 6) == 0) |
| 348 | s = &envline[7]; |
| 349 | break; |
| 350 | case 8: |
| 351 | if (!__builtin_expect (__libc_enable_secure, 0)) |
| 352 | { |
| 353 | if (memcmp (envline, "TOP_PAD_" , 8) == 0) |
| 354 | __libc_mallopt (M_TOP_PAD, atoi (&envline[9])); |
| 355 | else if (memcmp (envline, "PERTURB_" , 8) == 0) |
| 356 | __libc_mallopt (M_PERTURB, atoi (&envline[9])); |
| 357 | } |
| 358 | break; |
| 359 | case 9: |
| 360 | if (!__builtin_expect (__libc_enable_secure, 0)) |
| 361 | { |
| 362 | if (memcmp (envline, "MMAP_MAX_" , 9) == 0) |
| 363 | __libc_mallopt (M_MMAP_MAX, atoi (&envline[10])); |
| 364 | else if (memcmp (envline, "ARENA_MAX" , 9) == 0) |
| 365 | __libc_mallopt (M_ARENA_MAX, atoi (&envline[10])); |
| 366 | } |
| 367 | break; |
| 368 | case 10: |
| 369 | if (!__builtin_expect (__libc_enable_secure, 0)) |
| 370 | { |
| 371 | if (memcmp (envline, "ARENA_TEST" , 10) == 0) |
| 372 | __libc_mallopt (M_ARENA_TEST, atoi (&envline[11])); |
| 373 | } |
| 374 | break; |
| 375 | case 15: |
| 376 | if (!__builtin_expect (__libc_enable_secure, 0)) |
| 377 | { |
| 378 | if (memcmp (envline, "TRIM_THRESHOLD_" , 15) == 0) |
| 379 | __libc_mallopt (M_TRIM_THRESHOLD, atoi (&envline[16])); |
| 380 | else if (memcmp (envline, "MMAP_THRESHOLD_" , 15) == 0) |
| 381 | __libc_mallopt (M_MMAP_THRESHOLD, atoi (&envline[16])); |
| 382 | } |
| 383 | break; |
| 384 | default: |
| 385 | break; |
| 386 | } |
| 387 | } |
| 388 | } |
| 389 | if (s && s[0]) |
| 390 | { |
| 391 | __libc_mallopt (M_CHECK_ACTION, (int) (s[0] - '0')); |
| 392 | if (check_action != 0) |
| 393 | __malloc_check_init (); |
| 394 | } |
| 395 | #endif |
| 396 | |
| 397 | #if HAVE_MALLOC_INIT_HOOK |
| 398 | void (*hook) (void) = atomic_forced_read (__malloc_initialize_hook); |
| 399 | if (hook != NULL) |
| 400 | (*hook)(); |
| 401 | #endif |
| 402 | __malloc_initialized = 1; |
| 403 | } |
| 404 | |
| 405 | /* Managing heaps and arenas (for concurrent threads) */ |
| 406 | |
| 407 | #if MALLOC_DEBUG > 1 |
| 408 | |
| 409 | /* Print the complete contents of a single heap to stderr. */ |
| 410 | |
| 411 | static void |
| 412 | dump_heap (heap_info *heap) |
| 413 | { |
| 414 | char *ptr; |
| 415 | mchunkptr p; |
| 416 | |
| 417 | fprintf (stderr, "Heap %p, size %10lx:\n" , heap, (long) heap->size); |
| 418 | ptr = (heap->ar_ptr != (mstate) (heap + 1)) ? |
| 419 | (char *) (heap + 1) : (char *) (heap + 1) + sizeof (struct malloc_state); |
| 420 | p = (mchunkptr) (((unsigned long) ptr + MALLOC_ALIGN_MASK) & |
| 421 | ~MALLOC_ALIGN_MASK); |
| 422 | for (;; ) |
| 423 | { |
| 424 | fprintf (stderr, "chunk %p size %10lx" , p, (long) p->size); |
| 425 | if (p == top (heap->ar_ptr)) |
| 426 | { |
| 427 | fprintf (stderr, " (top)\n" ); |
| 428 | break; |
| 429 | } |
| 430 | else if (p->size == (0 | PREV_INUSE)) |
| 431 | { |
| 432 | fprintf (stderr, " (fence)\n" ); |
| 433 | break; |
| 434 | } |
| 435 | fprintf (stderr, "\n" ); |
| 436 | p = next_chunk (p); |
| 437 | } |
| 438 | } |
| 439 | #endif /* MALLOC_DEBUG > 1 */ |
| 440 | |
| 441 | /* If consecutive mmap (0, HEAP_MAX_SIZE << 1, ...) calls return decreasing |
| 442 | addresses as opposed to increasing, new_heap would badly fragment the |
| 443 | address space. In that case remember the second HEAP_MAX_SIZE part |
| 444 | aligned to HEAP_MAX_SIZE from last mmap (0, HEAP_MAX_SIZE << 1, ...) |
| 445 | call (if it is already aligned) and try to reuse it next time. We need |
| 446 | no locking for it, as kernel ensures the atomicity for us - worst case |
| 447 | we'll call mmap (addr, HEAP_MAX_SIZE, ...) for some value of addr in |
| 448 | multiple threads, but only one will succeed. */ |
| 449 | static char *aligned_heap_area; |
| 450 | |
| 451 | /* Create a new heap. size is automatically rounded up to a multiple |
| 452 | of the page size. */ |
| 453 | |
| 454 | static heap_info * |
| 455 | internal_function |
| 456 | new_heap (size_t size, size_t top_pad) |
| 457 | { |
| 458 | size_t pagesize = GLRO (dl_pagesize); |
| 459 | char *p1, *p2; |
| 460 | unsigned long ul; |
| 461 | heap_info *h; |
| 462 | |
| 463 | if (size + top_pad < HEAP_MIN_SIZE) |
| 464 | size = HEAP_MIN_SIZE; |
| 465 | else if (size + top_pad <= HEAP_MAX_SIZE) |
| 466 | size += top_pad; |
| 467 | else if (size > HEAP_MAX_SIZE) |
| 468 | return 0; |
| 469 | else |
| 470 | size = HEAP_MAX_SIZE; |
| 471 | size = ALIGN_UP (size, pagesize); |
| 472 | |
| 473 | /* A memory region aligned to a multiple of HEAP_MAX_SIZE is needed. |
| 474 | No swap space needs to be reserved for the following large |
| 475 | mapping (on Linux, this is the case for all non-writable mappings |
| 476 | anyway). */ |
| 477 | p2 = MAP_FAILED; |
| 478 | if (aligned_heap_area) |
| 479 | { |
| 480 | p2 = (char *) MMAP (aligned_heap_area, HEAP_MAX_SIZE, PROT_NONE, |
| 481 | MAP_NORESERVE); |
| 482 | aligned_heap_area = NULL; |
| 483 | if (p2 != MAP_FAILED && ((unsigned long) p2 & (HEAP_MAX_SIZE - 1))) |
| 484 | { |
| 485 | __munmap (p2, HEAP_MAX_SIZE); |
| 486 | p2 = MAP_FAILED; |
| 487 | } |
| 488 | } |
| 489 | if (p2 == MAP_FAILED) |
| 490 | { |
| 491 | p1 = (char *) MMAP (0, HEAP_MAX_SIZE << 1, PROT_NONE, MAP_NORESERVE); |
| 492 | if (p1 != MAP_FAILED) |
| 493 | { |
| 494 | p2 = (char *) (((unsigned long) p1 + (HEAP_MAX_SIZE - 1)) |
| 495 | & ~(HEAP_MAX_SIZE - 1)); |
| 496 | ul = p2 - p1; |
| 497 | if (ul) |
| 498 | __munmap (p1, ul); |
| 499 | else |
| 500 | aligned_heap_area = p2 + HEAP_MAX_SIZE; |
| 501 | __munmap (p2 + HEAP_MAX_SIZE, HEAP_MAX_SIZE - ul); |
| 502 | } |
| 503 | else |
| 504 | { |
| 505 | /* Try to take the chance that an allocation of only HEAP_MAX_SIZE |
| 506 | is already aligned. */ |
| 507 | p2 = (char *) MMAP (0, HEAP_MAX_SIZE, PROT_NONE, MAP_NORESERVE); |
| 508 | if (p2 == MAP_FAILED) |
| 509 | return 0; |
| 510 | |
| 511 | if ((unsigned long) p2 & (HEAP_MAX_SIZE - 1)) |
| 512 | { |
| 513 | __munmap (p2, HEAP_MAX_SIZE); |
| 514 | return 0; |
| 515 | } |
| 516 | } |
| 517 | } |
| 518 | if (__mprotect (p2, size, PROT_READ | PROT_WRITE) != 0) |
| 519 | { |
| 520 | __munmap (p2, HEAP_MAX_SIZE); |
| 521 | return 0; |
| 522 | } |
| 523 | h = (heap_info *) p2; |
| 524 | h->size = size; |
| 525 | h->mprotect_size = size; |
| 526 | LIBC_PROBE (memory_heap_new, 2, h, h->size); |
| 527 | return h; |
| 528 | } |
| 529 | |
| 530 | /* Grow a heap. size is automatically rounded up to a |
| 531 | multiple of the page size. */ |
| 532 | |
| 533 | static int |
| 534 | grow_heap (heap_info *h, long diff) |
| 535 | { |
| 536 | size_t pagesize = GLRO (dl_pagesize); |
| 537 | long new_size; |
| 538 | |
| 539 | diff = ALIGN_UP (diff, pagesize); |
| 540 | new_size = (long) h->size + diff; |
| 541 | if ((unsigned long) new_size > (unsigned long) HEAP_MAX_SIZE) |
| 542 | return -1; |
| 543 | |
| 544 | if ((unsigned long) new_size > h->mprotect_size) |
| 545 | { |
| 546 | if (__mprotect ((char *) h + h->mprotect_size, |
| 547 | (unsigned long) new_size - h->mprotect_size, |
| 548 | PROT_READ | PROT_WRITE) != 0) |
| 549 | return -2; |
| 550 | |
| 551 | h->mprotect_size = new_size; |
| 552 | } |
| 553 | |
| 554 | h->size = new_size; |
| 555 | LIBC_PROBE (memory_heap_more, 2, h, h->size); |
| 556 | return 0; |
| 557 | } |
| 558 | |
| 559 | /* Shrink a heap. */ |
| 560 | |
| 561 | static int |
| 562 | shrink_heap (heap_info *h, long diff) |
| 563 | { |
| 564 | long new_size; |
| 565 | |
| 566 | new_size = (long) h->size - diff; |
| 567 | if (new_size < (long) sizeof (*h)) |
| 568 | return -1; |
| 569 | |
| 570 | /* Try to re-map the extra heap space freshly to save memory, and make it |
| 571 | inaccessible. See malloc-sysdep.h to know when this is true. */ |
| 572 | if (__glibc_unlikely (check_may_shrink_heap ())) |
| 573 | { |
| 574 | if ((char *) MMAP ((char *) h + new_size, diff, PROT_NONE, |
| 575 | MAP_FIXED) == (char *) MAP_FAILED) |
| 576 | return -2; |
| 577 | |
| 578 | h->mprotect_size = new_size; |
| 579 | } |
| 580 | else |
| 581 | __madvise ((char *) h + new_size, diff, MADV_DONTNEED); |
| 582 | /*fprintf(stderr, "shrink %p %08lx\n", h, new_size);*/ |
| 583 | |
| 584 | h->size = new_size; |
| 585 | LIBC_PROBE (memory_heap_less, 2, h, h->size); |
| 586 | return 0; |
| 587 | } |
| 588 | |
| 589 | /* Delete a heap. */ |
| 590 | |
| 591 | #define delete_heap(heap) \ |
| 592 | do { \ |
| 593 | if ((char *) (heap) + HEAP_MAX_SIZE == aligned_heap_area) \ |
| 594 | aligned_heap_area = NULL; \ |
| 595 | __munmap ((char *) (heap), HEAP_MAX_SIZE); \ |
| 596 | } while (0) |
| 597 | |
| 598 | static int |
| 599 | internal_function |
| 600 | heap_trim (heap_info *heap, size_t pad) |
| 601 | { |
| 602 | mstate ar_ptr = heap->ar_ptr; |
| 603 | unsigned long pagesz = GLRO (dl_pagesize); |
| 604 | mchunkptr top_chunk = top (ar_ptr), p, bck, fwd; |
| 605 | heap_info *prev_heap; |
| 606 | long new_size, top_size, top_area, , prev_size, misalign; |
| 607 | |
| 608 | /* Can this heap go away completely? */ |
| 609 | while (top_chunk == chunk_at_offset (heap, sizeof (*heap))) |
| 610 | { |
| 611 | prev_heap = heap->prev; |
| 612 | prev_size = prev_heap->size - (MINSIZE - 2 * SIZE_SZ); |
| 613 | p = chunk_at_offset (prev_heap, prev_size); |
| 614 | /* fencepost must be properly aligned. */ |
| 615 | misalign = ((long) p) & MALLOC_ALIGN_MASK; |
| 616 | p = chunk_at_offset (prev_heap, prev_size - misalign); |
| 617 | assert (chunksize_nomask (p) == (0 | PREV_INUSE)); /* must be fencepost */ |
| 618 | p = prev_chunk (p); |
| 619 | new_size = chunksize (p) + (MINSIZE - 2 * SIZE_SZ) + misalign; |
| 620 | assert (new_size > 0 && new_size < (long) (2 * MINSIZE)); |
| 621 | if (!prev_inuse (p)) |
| 622 | new_size += prev_size (p); |
| 623 | assert (new_size > 0 && new_size < HEAP_MAX_SIZE); |
| 624 | if (new_size + (HEAP_MAX_SIZE - prev_heap->size) < pad + MINSIZE + pagesz) |
| 625 | break; |
| 626 | ar_ptr->system_mem -= heap->size; |
| 627 | LIBC_PROBE (memory_heap_free, 2, heap, heap->size); |
| 628 | delete_heap (heap); |
| 629 | heap = prev_heap; |
| 630 | if (!prev_inuse (p)) /* consolidate backward */ |
| 631 | { |
| 632 | p = prev_chunk (p); |
| 633 | unlink (ar_ptr, p, bck, fwd); |
| 634 | } |
| 635 | assert (((unsigned long) ((char *) p + new_size) & (pagesz - 1)) == 0); |
| 636 | assert (((char *) p + new_size) == ((char *) heap + heap->size)); |
| 637 | top (ar_ptr) = top_chunk = p; |
| 638 | set_head (top_chunk, new_size | PREV_INUSE); |
| 639 | /*check_chunk(ar_ptr, top_chunk);*/ |
| 640 | } |
| 641 | |
| 642 | /* Uses similar logic for per-thread arenas as the main arena with systrim |
| 643 | and _int_free by preserving the top pad and rounding down to the nearest |
| 644 | page. */ |
| 645 | top_size = chunksize (top_chunk); |
| 646 | if ((unsigned long)(top_size) < |
| 647 | (unsigned long)(mp_.trim_threshold)) |
| 648 | return 0; |
| 649 | |
| 650 | top_area = top_size - MINSIZE - 1; |
| 651 | if (top_area < 0 || (size_t) top_area <= pad) |
| 652 | return 0; |
| 653 | |
| 654 | /* Release in pagesize units and round down to the nearest page. */ |
| 655 | extra = ALIGN_DOWN(top_area - pad, pagesz); |
| 656 | if (extra == 0) |
| 657 | return 0; |
| 658 | |
| 659 | /* Try to shrink. */ |
| 660 | if (shrink_heap (heap, extra) != 0) |
| 661 | return 0; |
| 662 | |
| 663 | ar_ptr->system_mem -= extra; |
| 664 | |
| 665 | /* Success. Adjust top accordingly. */ |
| 666 | set_head (top_chunk, (top_size - extra) | PREV_INUSE); |
| 667 | /*check_chunk(ar_ptr, top_chunk);*/ |
| 668 | return 1; |
| 669 | } |
| 670 | |
| 671 | /* Create a new arena with initial size "size". */ |
| 672 | |
| 673 | /* If REPLACED_ARENA is not NULL, detach it from this thread. Must be |
| 674 | called while free_list_lock is held. */ |
| 675 | static void |
| 676 | detach_arena (mstate replaced_arena) |
| 677 | { |
| 678 | if (replaced_arena != NULL) |
| 679 | { |
| 680 | assert (replaced_arena->attached_threads > 0); |
| 681 | /* The current implementation only detaches from main_arena in |
| 682 | case of allocation failure. This means that it is likely not |
| 683 | beneficial to put the arena on free_list even if the |
| 684 | reference count reaches zero. */ |
| 685 | --replaced_arena->attached_threads; |
| 686 | } |
| 687 | } |
| 688 | |
| 689 | static mstate |
| 690 | _int_new_arena (size_t size) |
| 691 | { |
| 692 | mstate a; |
| 693 | heap_info *h; |
| 694 | char *ptr; |
| 695 | unsigned long misalign; |
| 696 | |
| 697 | h = new_heap (size + (sizeof (*h) + sizeof (*a) + MALLOC_ALIGNMENT), |
| 698 | mp_.top_pad); |
| 699 | if (!h) |
| 700 | { |
| 701 | /* Maybe size is too large to fit in a single heap. So, just try |
| 702 | to create a minimally-sized arena and let _int_malloc() attempt |
| 703 | to deal with the large request via mmap_chunk(). */ |
| 704 | h = new_heap (sizeof (*h) + sizeof (*a) + MALLOC_ALIGNMENT, mp_.top_pad); |
| 705 | if (!h) |
| 706 | return 0; |
| 707 | } |
| 708 | a = h->ar_ptr = (mstate) (h + 1); |
| 709 | malloc_init_state (a); |
| 710 | a->attached_threads = 1; |
| 711 | /*a->next = NULL;*/ |
| 712 | a->system_mem = a->max_system_mem = h->size; |
| 713 | |
| 714 | /* Set up the top chunk, with proper alignment. */ |
| 715 | ptr = (char *) (a + 1); |
| 716 | misalign = (unsigned long) chunk2mem (ptr) & MALLOC_ALIGN_MASK; |
| 717 | if (misalign > 0) |
| 718 | ptr += MALLOC_ALIGNMENT - misalign; |
| 719 | top (a) = (mchunkptr) ptr; |
| 720 | set_head (top (a), (((char *) h + h->size) - ptr) | PREV_INUSE); |
| 721 | |
| 722 | LIBC_PROBE (memory_arena_new, 2, a, size); |
| 723 | mstate replaced_arena = thread_arena; |
| 724 | thread_arena = a; |
| 725 | __libc_lock_init (a->mutex); |
| 726 | |
| 727 | __libc_lock_lock (list_lock); |
| 728 | |
| 729 | /* Add the new arena to the global list. */ |
| 730 | a->next = main_arena.next; |
| 731 | /* FIXME: The barrier is an attempt to synchronize with read access |
| 732 | in reused_arena, which does not acquire list_lock while |
| 733 | traversing the list. */ |
| 734 | atomic_write_barrier (); |
| 735 | main_arena.next = a; |
| 736 | |
| 737 | __libc_lock_unlock (list_lock); |
| 738 | |
| 739 | __libc_lock_lock (free_list_lock); |
| 740 | detach_arena (replaced_arena); |
| 741 | __libc_lock_unlock (free_list_lock); |
| 742 | |
| 743 | /* Lock this arena. NB: Another thread may have been attached to |
| 744 | this arena because the arena is now accessible from the |
| 745 | main_arena.next list and could have been picked by reused_arena. |
| 746 | This can only happen for the last arena created (before the arena |
| 747 | limit is reached). At this point, some arena has to be attached |
| 748 | to two threads. We could acquire the arena lock before list_lock |
| 749 | to make it less likely that reused_arena picks this new arena, |
| 750 | but this could result in a deadlock with |
| 751 | __malloc_fork_lock_parent. */ |
| 752 | |
| 753 | __libc_lock_lock (a->mutex); |
| 754 | |
| 755 | return a; |
| 756 | } |
| 757 | |
| 758 | |
| 759 | /* Remove an arena from free_list. */ |
| 760 | static mstate |
| 761 | get_free_list (void) |
| 762 | { |
| 763 | mstate replaced_arena = thread_arena; |
| 764 | mstate result = free_list; |
| 765 | if (result != NULL) |
| 766 | { |
| 767 | __libc_lock_lock (free_list_lock); |
| 768 | result = free_list; |
| 769 | if (result != NULL) |
| 770 | { |
| 771 | free_list = result->next_free; |
| 772 | |
| 773 | /* The arena will be attached to this thread. */ |
| 774 | assert (result->attached_threads == 0); |
| 775 | result->attached_threads = 1; |
| 776 | |
| 777 | detach_arena (replaced_arena); |
| 778 | } |
| 779 | __libc_lock_unlock (free_list_lock); |
| 780 | |
| 781 | if (result != NULL) |
| 782 | { |
| 783 | LIBC_PROBE (memory_arena_reuse_free_list, 1, result); |
| 784 | __libc_lock_lock (result->mutex); |
| 785 | thread_arena = result; |
| 786 | } |
| 787 | } |
| 788 | |
| 789 | return result; |
| 790 | } |
| 791 | |
| 792 | /* Remove the arena from the free list (if it is present). |
| 793 | free_list_lock must have been acquired by the caller. */ |
| 794 | static void |
| 795 | remove_from_free_list (mstate arena) |
| 796 | { |
| 797 | mstate *previous = &free_list; |
| 798 | for (mstate p = free_list; p != NULL; p = p->next_free) |
| 799 | { |
| 800 | assert (p->attached_threads == 0); |
| 801 | if (p == arena) |
| 802 | { |
| 803 | /* Remove the requested arena from the list. */ |
| 804 | *previous = p->next_free; |
| 805 | break; |
| 806 | } |
| 807 | else |
| 808 | previous = &p->next_free; |
| 809 | } |
| 810 | } |
| 811 | |
| 812 | /* Lock and return an arena that can be reused for memory allocation. |
| 813 | Avoid AVOID_ARENA as we have already failed to allocate memory in |
| 814 | it and it is currently locked. */ |
| 815 | static mstate |
| 816 | reused_arena (mstate avoid_arena) |
| 817 | { |
| 818 | mstate result; |
| 819 | /* FIXME: Access to next_to_use suffers from data races. */ |
| 820 | static mstate next_to_use; |
| 821 | if (next_to_use == NULL) |
| 822 | next_to_use = &main_arena; |
| 823 | |
| 824 | /* Iterate over all arenas (including those linked from |
| 825 | free_list). */ |
| 826 | result = next_to_use; |
| 827 | do |
| 828 | { |
| 829 | if (!arena_is_corrupt (result) && !__libc_lock_trylock (result->mutex)) |
| 830 | goto out; |
| 831 | |
| 832 | /* FIXME: This is a data race, see _int_new_arena. */ |
| 833 | result = result->next; |
| 834 | } |
| 835 | while (result != next_to_use); |
| 836 | |
| 837 | /* Avoid AVOID_ARENA as we have already failed to allocate memory |
| 838 | in that arena and it is currently locked. */ |
| 839 | if (result == avoid_arena) |
| 840 | result = result->next; |
| 841 | |
| 842 | /* Make sure that the arena we get is not corrupted. */ |
| 843 | mstate begin = result; |
| 844 | while (arena_is_corrupt (result) || result == avoid_arena) |
| 845 | { |
| 846 | result = result->next; |
| 847 | if (result == begin) |
| 848 | /* We looped around the arena list. We could not find any |
| 849 | arena that was either not corrupted or not the one we |
| 850 | wanted to avoid. */ |
| 851 | return NULL; |
| 852 | } |
| 853 | |
| 854 | /* No arena available without contention. Wait for the next in line. */ |
| 855 | LIBC_PROBE (memory_arena_reuse_wait, 3, &result->mutex, result, avoid_arena); |
| 856 | __libc_lock_lock (result->mutex); |
| 857 | |
| 858 | out: |
| 859 | /* Attach the arena to the current thread. */ |
| 860 | { |
| 861 | /* Update the arena thread attachment counters. */ |
| 862 | mstate replaced_arena = thread_arena; |
| 863 | __libc_lock_lock (free_list_lock); |
| 864 | detach_arena (replaced_arena); |
| 865 | |
| 866 | /* We may have picked up an arena on the free list. We need to |
| 867 | preserve the invariant that no arena on the free list has a |
| 868 | positive attached_threads counter (otherwise, |
| 869 | arena_thread_freeres cannot use the counter to determine if the |
| 870 | arena needs to be put on the free list). We unconditionally |
| 871 | remove the selected arena from the free list. The caller of |
| 872 | reused_arena checked the free list and observed it to be empty, |
| 873 | so the list is very short. */ |
| 874 | remove_from_free_list (result); |
| 875 | |
| 876 | ++result->attached_threads; |
| 877 | |
| 878 | __libc_lock_unlock (free_list_lock); |
| 879 | } |
| 880 | |
| 881 | LIBC_PROBE (memory_arena_reuse, 2, result, avoid_arena); |
| 882 | thread_arena = result; |
| 883 | next_to_use = result->next; |
| 884 | |
| 885 | return result; |
| 886 | } |
| 887 | |
| 888 | static mstate |
| 889 | internal_function |
| 890 | arena_get2 (size_t size, mstate avoid_arena) |
| 891 | { |
| 892 | mstate a; |
| 893 | |
| 894 | static size_t narenas_limit; |
| 895 | |
| 896 | a = get_free_list (); |
| 897 | if (a == NULL) |
| 898 | { |
| 899 | /* Nothing immediately available, so generate a new arena. */ |
| 900 | if (narenas_limit == 0) |
| 901 | { |
| 902 | if (mp_.arena_max != 0) |
| 903 | narenas_limit = mp_.arena_max; |
| 904 | else if (narenas > mp_.arena_test) |
| 905 | { |
| 906 | int n = __get_nprocs (); |
| 907 | |
| 908 | if (n >= 1) |
| 909 | narenas_limit = NARENAS_FROM_NCORES (n); |
| 910 | else |
| 911 | /* We have no information about the system. Assume two |
| 912 | cores. */ |
| 913 | narenas_limit = NARENAS_FROM_NCORES (2); |
| 914 | } |
| 915 | } |
| 916 | repeat:; |
| 917 | size_t n = narenas; |
| 918 | /* NB: the following depends on the fact that (size_t)0 - 1 is a |
| 919 | very large number and that the underflow is OK. If arena_max |
| 920 | is set the value of arena_test is irrelevant. If arena_test |
| 921 | is set but narenas is not yet larger or equal to arena_test |
| 922 | narenas_limit is 0. There is no possibility for narenas to |
| 923 | be too big for the test to always fail since there is not |
| 924 | enough address space to create that many arenas. */ |
| 925 | if (__glibc_unlikely (n <= narenas_limit - 1)) |
| 926 | { |
| 927 | if (catomic_compare_and_exchange_bool_acq (&narenas, n + 1, n)) |
| 928 | goto repeat; |
| 929 | a = _int_new_arena (size); |
| 930 | if (__glibc_unlikely (a == NULL)) |
| 931 | catomic_decrement (&narenas); |
| 932 | } |
| 933 | else |
| 934 | a = reused_arena (avoid_arena); |
| 935 | } |
| 936 | return a; |
| 937 | } |
| 938 | |
| 939 | /* If we don't have the main arena, then maybe the failure is due to running |
| 940 | out of mmapped areas, so we can try allocating on the main arena. |
| 941 | Otherwise, it is likely that sbrk() has failed and there is still a chance |
| 942 | to mmap(), so try one of the other arenas. */ |
| 943 | static mstate |
| 944 | arena_get_retry (mstate ar_ptr, size_t bytes) |
| 945 | { |
| 946 | LIBC_PROBE (memory_arena_retry, 2, bytes, ar_ptr); |
| 947 | if (ar_ptr != &main_arena) |
| 948 | { |
| 949 | __libc_lock_unlock (ar_ptr->mutex); |
| 950 | /* Don't touch the main arena if it is corrupt. */ |
| 951 | if (arena_is_corrupt (&main_arena)) |
| 952 | return NULL; |
| 953 | |
| 954 | ar_ptr = &main_arena; |
| 955 | __libc_lock_lock (ar_ptr->mutex); |
| 956 | } |
| 957 | else |
| 958 | { |
| 959 | __libc_lock_unlock (ar_ptr->mutex); |
| 960 | ar_ptr = arena_get2 (bytes, ar_ptr); |
| 961 | } |
| 962 | |
| 963 | return ar_ptr; |
| 964 | } |
| 965 | |
| 966 | static void __attribute__ ((section ("__libc_thread_freeres_fn" ))) |
| 967 | arena_thread_freeres (void) |
| 968 | { |
| 969 | mstate a = thread_arena; |
| 970 | thread_arena = NULL; |
| 971 | |
| 972 | if (a != NULL) |
| 973 | { |
| 974 | __libc_lock_lock (free_list_lock); |
| 975 | /* If this was the last attached thread for this arena, put the |
| 976 | arena on the free list. */ |
| 977 | assert (a->attached_threads > 0); |
| 978 | if (--a->attached_threads == 0) |
| 979 | { |
| 980 | a->next_free = free_list; |
| 981 | free_list = a; |
| 982 | } |
| 983 | __libc_lock_unlock (free_list_lock); |
| 984 | } |
| 985 | } |
| 986 | text_set_element (__libc_thread_subfreeres, arena_thread_freeres); |
| 987 | |
| 988 | /* |
| 989 | * Local variables: |
| 990 | * c-basic-offset: 2 |
| 991 | * End: |
| 992 | */ |
| 993 | |