|
|
bca718 |
Upstream commits:
|
|
|
bca718 |
|
|
|
bca718 |
commit a62719ba90e2fa1728890ae7dc8df9e32a622e7b
|
|
|
bca718 |
Author: Florian Weimer <fweimer@redhat.com>
|
|
|
bca718 |
Date: Wed Oct 28 19:32:46 2015 +0100
|
|
|
bca718 |
|
|
|
bca718 |
malloc: Prevent arena free_list from turning cyclic [BZ #19048]
|
|
|
bca718 |
|
|
|
bca718 |
commit 3da825ce483903e3a881a016113b3e59fd4041de
|
|
|
bca718 |
Author: Florian Weimer <fweimer@redhat.com>
|
|
|
bca718 |
Date: Wed Dec 16 12:39:48 2015 +0100
|
|
|
bca718 |
|
|
|
bca718 |
malloc: Fix attached thread reference count handling [BZ #19243]
|
|
|
bca718 |
|
|
|
bca718 |
commit 90c400bd4904b0240a148f0b357a5cbc36179239
|
|
|
bca718 |
Author: Florian Weimer <fweimer@redhat.com>
|
|
|
bca718 |
Date: Mon Dec 21 16:42:46 2015 +0100
|
|
|
bca718 |
|
|
|
bca718 |
malloc: Fix list_lock/arena lock deadlock [BZ #19182]
|
|
|
bca718 |
|
|
|
bca718 |
commit 7962541a32eff5597bc4207e781cfac8d1bb0d87
|
|
|
bca718 |
Author: Florian Weimer <fweimer@redhat.com>
|
|
|
bca718 |
Date: Wed Dec 23 17:23:33 2015 +0100
|
|
|
bca718 |
|
|
|
bca718 |
malloc: Update comment for list_lock
|
|
|
bca718 |
|
|
|
bca718 |
commit 2a38688932243b5b16fb12d84c7ac1138ce50363
|
|
|
bca718 |
Author: Florian Weimer <fweimer@redhat.com>
|
|
|
bca718 |
Date: Fri Feb 19 14:11:32 2016 +0100
|
|
|
bca718 |
|
|
|
bca718 |
tst-malloc-thread-exit: Use fewer system resources
|
|
|
bca718 |
|
|
|
bca718 |
Also included is the following change, which has not yet been
|
|
|
bca718 |
committed upstream:
|
|
|
bca718 |
|
|
|
bca718 |
malloc: Preserve arena free list/thread count invariant [BZ #20370]
|
|
|
bca718 |
|
|
|
bca718 |
It is necessary to preserve the invariant that if an arena is
|
|
|
bca718 |
on the free list, it has thread attach count zero. Otherwise,
|
|
|
bca718 |
when arena_thread_freeres sees the zero attach count, it will
|
|
|
bca718 |
add it, and without the invariant, an arena could get pushed
|
|
|
bca718 |
to the list twice, resulting in a cycle.
|
|
|
bca718 |
|
|
|
bca718 |
One possible execution trace looks like this:
|
|
|
bca718 |
|
|
|
bca718 |
Thread 1 examines free list and observes it as empty.
|
|
|
bca718 |
Thread 2 exits and adds its arena to the free list,
|
|
|
bca718 |
with attached_threads == 0).
|
|
|
bca718 |
Thread 1 selects this arena in reused_arena (not from the free list).
|
|
|
bca718 |
Thread 1 increments attached_threads and attaches itself.
|
|
|
bca718 |
(The arena remains on the free list.)
|
|
|
bca718 |
Thread 1 exits, decrements attached_threads,
|
|
|
bca718 |
and adds the arena to the free list.
|
|
|
bca718 |
|
|
|
bca718 |
The final step creates a cycle in the usual way (by overwriting the
|
|
|
bca718 |
next_free member with the former list head, while there is another
|
|
|
bca718 |
list item pointing to the arena structure).
|
|
|
bca718 |
|
|
|
bca718 |
tst-malloc-thread-exit exhibits this issue, but it was only visible
|
|
|
bca718 |
with a debugger because the incorrect fix in bug 19243 removed
|
|
|
bca718 |
the assert from get_free_list.
|
|
|
bca718 |
|
|
|
bca718 |
|
|
|
bca718 |
Index: b/malloc/arena.c
|
|
|
bca718 |
===================================================================
|
|
|
bca718 |
--- a/malloc/arena.c
|
|
|
bca718 |
+++ b/malloc/arena.c
|
|
|
bca718 |
@@ -77,10 +77,30 @@ extern int sanity_check_heap_info_alignm
|
|
|
bca718 |
/* Thread specific data */
|
|
|
bca718 |
|
|
|
bca718 |
static tsd_key_t arena_key;
|
|
|
bca718 |
-static mutex_t list_lock = MUTEX_INITIALIZER;
|
|
|
bca718 |
+
|
|
|
bca718 |
+/* Arena free list. free_list_lock synchronizes access to the
|
|
|
bca718 |
+ free_list variable below, and the next_free and attached_threads
|
|
|
bca718 |
+ members of struct malloc_state objects. No other locks must be
|
|
|
bca718 |
+ acquired after free_list_lock has been acquired. */
|
|
|
bca718 |
+
|
|
|
bca718 |
+static mutex_t free_list_lock = MUTEX_INITIALIZER;
|
|
|
bca718 |
static size_t narenas = 1;
|
|
|
bca718 |
static mstate free_list;
|
|
|
bca718 |
|
|
|
bca718 |
+/* list_lock prevents concurrent writes to the next member of struct
|
|
|
bca718 |
+ malloc_state objects.
|
|
|
bca718 |
+
|
|
|
bca718 |
+ Read access to the next member is supposed to synchronize with the
|
|
|
bca718 |
+ atomic_write_barrier and the write to the next member in
|
|
|
bca718 |
+ _int_new_arena. This suffers from data races; see the FIXME
|
|
|
bca718 |
+ comments in _int_new_arena and reused_arena.
|
|
|
bca718 |
+
|
|
|
bca718 |
+ list_lock also prevents concurrent forks. At the time list_lock is
|
|
|
bca718 |
+ acquired, no arena lock must have been acquired, but it is
|
|
|
bca718 |
+ permitted to acquire arena locks subsequently, while list_lock is
|
|
|
bca718 |
+ acquired. */
|
|
|
bca718 |
+static mutex_t list_lock = MUTEX_INITIALIZER;
|
|
|
bca718 |
+
|
|
|
bca718 |
#if THREAD_STATS
|
|
|
bca718 |
static int stat_n_heaps;
|
|
|
bca718 |
#define THREAD_STAT(x) x
|
|
|
bca718 |
@@ -221,6 +241,10 @@ ptmalloc_lock_all (void)
|
|
|
bca718 |
|
|
|
bca718 |
if(__malloc_initialized < 1)
|
|
|
bca718 |
return;
|
|
|
bca718 |
+
|
|
|
bca718 |
+ /* We do not acquire free_list_lock here because we completely
|
|
|
bca718 |
+ reconstruct free_list in ptmalloc_unlock_all2. */
|
|
|
bca718 |
+
|
|
|
bca718 |
if (mutex_trylock(&list_lock))
|
|
|
bca718 |
{
|
|
|
bca718 |
void *my_arena;
|
|
|
bca718 |
@@ -242,7 +266,10 @@ ptmalloc_lock_all (void)
|
|
|
bca718 |
save_free_hook = __free_hook;
|
|
|
bca718 |
__malloc_hook = malloc_atfork;
|
|
|
bca718 |
__free_hook = free_atfork;
|
|
|
bca718 |
- /* Only the current thread may perform malloc/free calls now. */
|
|
|
bca718 |
+ /* Only the current thread may perform malloc/free calls now.
|
|
|
bca718 |
+ save_arena will be reattached to the current thread, in
|
|
|
bca718 |
+ ptmalloc_lock_all, so save_arena->attached_threads is not
|
|
|
bca718 |
+ updated. */
|
|
|
bca718 |
tsd_getspecific(arena_key, save_arena);
|
|
|
bca718 |
tsd_setspecific(arena_key, ATFORK_ARENA_PTR);
|
|
|
bca718 |
out:
|
|
|
bca718 |
@@ -258,6 +285,9 @@ ptmalloc_unlock_all (void)
|
|
|
bca718 |
return;
|
|
|
bca718 |
if (--atfork_recursive_cntr != 0)
|
|
|
bca718 |
return;
|
|
|
bca718 |
+ /* Replace ATFORK_ARENA_PTR with save_arena.
|
|
|
bca718 |
+ save_arena->attached_threads was not changed in ptmalloc_lock_all
|
|
|
bca718 |
+ and is still correct. */
|
|
|
bca718 |
tsd_setspecific(arena_key, save_arena);
|
|
|
bca718 |
__malloc_hook = save_malloc_hook;
|
|
|
bca718 |
__free_hook = save_free_hook;
|
|
|
bca718 |
@@ -286,16 +316,24 @@ ptmalloc_unlock_all2 (void)
|
|
|
bca718 |
tsd_setspecific(arena_key, save_arena);
|
|
|
bca718 |
__malloc_hook = save_malloc_hook;
|
|
|
bca718 |
__free_hook = save_free_hook;
|
|
|
bca718 |
+ /* Push all arenas to the free list, except save_arena, which is
|
|
|
bca718 |
+ attached to the current thread. */
|
|
|
bca718 |
+ mutex_init (&free_list_lock);
|
|
|
bca718 |
+ if (save_arena != NULL)
|
|
|
bca718 |
+ ((mstate) save_arena)->attached_threads = 1;
|
|
|
bca718 |
free_list = NULL;
|
|
|
bca718 |
for(ar_ptr = &main_arena;;) {
|
|
|
bca718 |
mutex_init(&ar_ptr->mutex);
|
|
|
bca718 |
if (ar_ptr != save_arena) {
|
|
|
bca718 |
+ /* This arena is no longer attached to any thread. */
|
|
|
bca718 |
+ ar_ptr->attached_threads = 0;
|
|
|
bca718 |
ar_ptr->next_free = free_list;
|
|
|
bca718 |
free_list = ar_ptr;
|
|
|
bca718 |
}
|
|
|
bca718 |
ar_ptr = ar_ptr->next;
|
|
|
bca718 |
if(ar_ptr == &main_arena) break;
|
|
|
bca718 |
}
|
|
|
bca718 |
+
|
|
|
bca718 |
mutex_init(&list_lock);
|
|
|
bca718 |
atfork_recursive_cntr = 0;
|
|
|
bca718 |
}
|
|
|
bca718 |
@@ -692,8 +730,25 @@ heap_trim(heap_info *heap, size_t pad)
|
|
|
bca718 |
return 1;
|
|
|
bca718 |
}
|
|
|
bca718 |
|
|
|
bca718 |
-/* Create a new arena with initial size "size". */
|
|
|
bca718 |
|
|
|
bca718 |
+/* If REPLACED_ARENA is not NULL, detach it from this thread. Must be
|
|
|
bca718 |
+ called while free_list_lock is held. */
|
|
|
bca718 |
+static void
|
|
|
bca718 |
+detach_arena (mstate replaced_arena)
|
|
|
bca718 |
+{
|
|
|
bca718 |
+ if (replaced_arena != NULL)
|
|
|
bca718 |
+ {
|
|
|
bca718 |
+ assert (replaced_arena->attached_threads > 0);
|
|
|
bca718 |
+ /* The current implementation only detaches from main_arena in
|
|
|
bca718 |
+ case of allocation failure. This means that it is likely not
|
|
|
bca718 |
+ beneficial to put the arena on free_list even if the
|
|
|
bca718 |
+ reference count reaches zero. */
|
|
|
bca718 |
+ --replaced_arena->attached_threads;
|
|
|
bca718 |
+ }
|
|
|
bca718 |
+}
|
|
|
bca718 |
+
|
|
|
bca718 |
+
|
|
|
bca718 |
+/* Create a new arena with initial size "size". */
|
|
|
bca718 |
static mstate
|
|
|
bca718 |
_int_new_arena(size_t size)
|
|
|
bca718 |
{
|
|
|
bca718 |
@@ -714,6 +769,7 @@ _int_new_arena(size_t size)
|
|
|
bca718 |
}
|
|
|
bca718 |
a = h->ar_ptr = (mstate)(h+1);
|
|
|
bca718 |
malloc_init_state(a);
|
|
|
bca718 |
+ a->attached_threads = 1;
|
|
|
bca718 |
/*a->next = NULL;*/
|
|
|
bca718 |
a->system_mem = a->max_system_mem = h->size;
|
|
|
bca718 |
arena_mem += h->size;
|
|
|
bca718 |
@@ -727,36 +783,68 @@ _int_new_arena(size_t size)
|
|
|
bca718 |
set_head(top(a), (((char*)h + h->size) - ptr) | PREV_INUSE);
|
|
|
bca718 |
|
|
|
bca718 |
LIBC_PROBE (memory_arena_new, 2, a, size);
|
|
|
bca718 |
+ mstate replaced_arena;
|
|
|
bca718 |
+ {
|
|
|
bca718 |
+ void *vptr = NULL;
|
|
|
bca718 |
+ replaced_arena = tsd_getspecific (arena_key, vptr);
|
|
|
bca718 |
+ }
|
|
|
bca718 |
tsd_setspecific(arena_key, (void *)a);
|
|
|
bca718 |
mutex_init(&a->mutex);
|
|
|
bca718 |
- (void)mutex_lock(&a->mutex);
|
|
|
bca718 |
|
|
|
bca718 |
(void)mutex_lock(&list_lock);
|
|
|
bca718 |
|
|
|
bca718 |
/* Add the new arena to the global list. */
|
|
|
bca718 |
a->next = main_arena.next;
|
|
|
bca718 |
+ /* FIXME: The barrier is an attempt to synchronize with read access
|
|
|
bca718 |
+ in reused_arena, which does not acquire list_lock while
|
|
|
bca718 |
+ traversing the list. */
|
|
|
bca718 |
atomic_write_barrier ();
|
|
|
bca718 |
main_arena.next = a;
|
|
|
bca718 |
|
|
|
bca718 |
(void)mutex_unlock(&list_lock);
|
|
|
bca718 |
|
|
|
bca718 |
+ (void) mutex_lock (&free_list_lock);
|
|
|
bca718 |
+ detach_arena (replaced_arena);
|
|
|
bca718 |
+ (void) mutex_unlock (&free_list_lock);
|
|
|
bca718 |
+
|
|
|
bca718 |
+ /* Lock this arena. NB: Another thread may have been attached to
|
|
|
bca718 |
+ this arena because the arena is now accessible from the
|
|
|
bca718 |
+ main_arena.next list and could have been picked by reused_arena.
|
|
|
bca718 |
+ This can only happen for the last arena created (before the arena
|
|
|
bca718 |
+ limit is reached). At this point, some arena has to be attached
|
|
|
bca718 |
+ to two threads. We could acquire the arena lock before list_lock
|
|
|
bca718 |
+ to make it less likely that reused_arena picks this new arena,
|
|
|
bca718 |
+ but this could result in a deadlock with ptmalloc_lock_all. */
|
|
|
bca718 |
+
|
|
|
bca718 |
+ (void) mutex_lock (&a->mutex);
|
|
|
bca718 |
+
|
|
|
bca718 |
THREAD_STAT(++(a->stat_lock_loop));
|
|
|
bca718 |
|
|
|
bca718 |
return a;
|
|
|
bca718 |
}
|
|
|
bca718 |
|
|
|
bca718 |
-
|
|
|
bca718 |
+/* Remove an arena from free_list. */
|
|
|
bca718 |
static mstate
|
|
|
bca718 |
get_free_list (void)
|
|
|
bca718 |
{
|
|
|
bca718 |
+ void *vptr = NULL;
|
|
|
bca718 |
+ mstate replaced_arena = tsd_getspecific (arena_key, vptr);
|
|
|
bca718 |
mstate result = free_list;
|
|
|
bca718 |
if (result != NULL)
|
|
|
bca718 |
{
|
|
|
bca718 |
- (void)mutex_lock(&list_lock);
|
|
|
bca718 |
+ (void)mutex_lock(&free_list_lock);
|
|
|
bca718 |
result = free_list;
|
|
|
bca718 |
if (result != NULL)
|
|
|
bca718 |
- free_list = result->next_free;
|
|
|
bca718 |
- (void)mutex_unlock(&list_lock);
|
|
|
bca718 |
+ {
|
|
|
bca718 |
+ free_list = result->next_free;
|
|
|
bca718 |
+
|
|
|
bca718 |
+ /* The arena will be attached to this thread. */
|
|
|
bca718 |
+ assert (result->attached_threads == 0);
|
|
|
bca718 |
+ result->attached_threads = 1;
|
|
|
bca718 |
+
|
|
|
bca718 |
+ detach_arena (replaced_arena);
|
|
|
bca718 |
+ }
|
|
|
bca718 |
+ (void)mutex_unlock(&free_list_lock);
|
|
|
bca718 |
|
|
|
bca718 |
if (result != NULL)
|
|
|
bca718 |
{
|
|
|
bca718 |
@@ -770,6 +858,26 @@ get_free_list (void)
|
|
|
bca718 |
return result;
|
|
|
bca718 |
}
|
|
|
bca718 |
|
|
|
bca718 |
+/* Remove the arena from the free list (if it is present).
|
|
|
bca718 |
+ free_list_lock must have been acquired by the caller. */
|
|
|
bca718 |
+static void
|
|
|
bca718 |
+remove_from_free_list (mstate arena)
|
|
|
bca718 |
+{
|
|
|
bca718 |
+ mstate *previous = &free_list;
|
|
|
bca718 |
+ for (mstate p = free_list; p != NULL; p = p->next_free)
|
|
|
bca718 |
+ {
|
|
|
bca718 |
+ assert (p->attached_threads == 0);
|
|
|
bca718 |
+ if (p == arena)
|
|
|
bca718 |
+ {
|
|
|
bca718 |
+ /* Remove the requested arena from the list. */
|
|
|
bca718 |
+ *previous = p->next_free;
|
|
|
bca718 |
+ break;
|
|
|
bca718 |
+ }
|
|
|
bca718 |
+ else
|
|
|
bca718 |
+ previous = &p->next_free;
|
|
|
bca718 |
+ }
|
|
|
bca718 |
+}
|
|
|
bca718 |
+
|
|
|
bca718 |
/* Lock and return an arena that can be reused for memory allocation.
|
|
|
bca718 |
Avoid AVOID_ARENA as we have already failed to allocate memory in
|
|
|
bca718 |
it and it is currently locked. */
|
|
|
bca718 |
@@ -777,16 +885,20 @@ static mstate
|
|
|
bca718 |
reused_arena (mstate avoid_arena)
|
|
|
bca718 |
{
|
|
|
bca718 |
mstate result;
|
|
|
bca718 |
+ /* FIXME: Access to next_to_use suffers from data races. */
|
|
|
bca718 |
static mstate next_to_use;
|
|
|
bca718 |
if (next_to_use == NULL)
|
|
|
bca718 |
next_to_use = &main_arena;
|
|
|
bca718 |
|
|
|
bca718 |
+ /* Iterate over all arenas (including those linked from
|
|
|
bca718 |
+ free_list). */
|
|
|
bca718 |
result = next_to_use;
|
|
|
bca718 |
do
|
|
|
bca718 |
{
|
|
|
bca718 |
if (!arena_is_corrupt (result) && !mutex_trylock(&result->mutex))
|
|
|
bca718 |
goto out;
|
|
|
bca718 |
|
|
|
bca718 |
+ /* FIXME: This is a data race, see _int_new_arena. */
|
|
|
bca718 |
result = result->next;
|
|
|
bca718 |
}
|
|
|
bca718 |
while (result != next_to_use);
|
|
|
bca718 |
@@ -815,6 +927,27 @@ reused_arena (mstate avoid_arena)
|
|
|
bca718 |
(void)mutex_lock(&result->mutex);
|
|
|
bca718 |
|
|
|
bca718 |
out:
|
|
|
bca718 |
+ /* Attach the arena to the current thread. */
|
|
|
bca718 |
+ {
|
|
|
bca718 |
+ /* Update the arena thread attachment counters. */
|
|
|
bca718 |
+ void *vptr = NULL;
|
|
|
bca718 |
+ mstate replaced_arena = tsd_getspecific (arena_key, vptr);
|
|
|
bca718 |
+ (void) mutex_lock (&free_list_lock);
|
|
|
bca718 |
+ detach_arena (replaced_arena);
|
|
|
bca718 |
+
|
|
|
bca718 |
+ /* We may have picked up an arena on the free list. We need to
|
|
|
bca718 |
+ preserve the invariant that no arena on the free list has a
|
|
|
bca718 |
+ positive attached_threads counter (otherwise,
|
|
|
bca718 |
+ arena_thread_freeres cannot use the counter to determine if the
|
|
|
bca718 |
+ arena needs to be put on the free list). We unconditionally
|
|
|
bca718 |
+ remove the selected arena from the free list. The caller of
|
|
|
bca718 |
+ reused_arena checked the free list and observed it to be empty,
|
|
|
bca718 |
+ so the list is very short. */
|
|
|
bca718 |
+ remove_from_free_list (result);
|
|
|
bca718 |
+
|
|
|
bca718 |
+ ++result->attached_threads;
|
|
|
bca718 |
+ (void) mutex_unlock (&free_list_lock);
|
|
|
bca718 |
+ }
|
|
|
bca718 |
LIBC_PROBE (memory_arena_reuse, 2, result, avoid_arena);
|
|
|
bca718 |
tsd_setspecific(arena_key, (void *)result);
|
|
|
bca718 |
THREAD_STAT(++(result->stat_lock_loop));
|
|
|
bca718 |
@@ -905,10 +1038,16 @@ arena_thread_freeres (void)
|
|
|
bca718 |
|
|
|
bca718 |
if (a != NULL)
|
|
|
bca718 |
{
|
|
|
bca718 |
- (void)mutex_lock(&list_lock);
|
|
|
bca718 |
- a->next_free = free_list;
|
|
|
bca718 |
- free_list = a;
|
|
|
bca718 |
- (void)mutex_unlock(&list_lock);
|
|
|
bca718 |
+ (void)mutex_lock(&free_list_lock);
|
|
|
bca718 |
+ /* If this was the last attached thread for this arena, put the
|
|
|
bca718 |
+ arena on the free list. */
|
|
|
bca718 |
+ assert (a->attached_threads > 0);
|
|
|
bca718 |
+ if (--a->attached_threads == 0)
|
|
|
bca718 |
+ {
|
|
|
bca718 |
+ a->next_free = free_list;
|
|
|
bca718 |
+ free_list = a;
|
|
|
bca718 |
+ }
|
|
|
bca718 |
+ (void)mutex_unlock(&free_list_lock);
|
|
|
bca718 |
}
|
|
|
bca718 |
}
|
|
|
bca718 |
text_set_element (__libc_thread_subfreeres, arena_thread_freeres);
|
|
|
bca718 |
Index: b/malloc/malloc.c
|
|
|
bca718 |
===================================================================
|
|
|
bca718 |
--- a/malloc/malloc.c
|
|
|
bca718 |
+++ b/malloc/malloc.c
|
|
|
bca718 |
@@ -1727,8 +1727,13 @@ struct malloc_state {
|
|
|
bca718 |
/* Linked list */
|
|
|
bca718 |
struct malloc_state *next;
|
|
|
bca718 |
|
|
|
bca718 |
- /* Linked list for free arenas. */
|
|
|
bca718 |
+ /* Linked list for free arenas. Access to this field is serialized
|
|
|
bca718 |
+ by free_list_lock in arena.c. */
|
|
|
bca718 |
struct malloc_state *next_free;
|
|
|
bca718 |
+ /* Number of threads attached to this arena. 0 if the arena is on
|
|
|
bca718 |
+ the free list. Access to this field is serialized by
|
|
|
bca718 |
+ free_list_lock in arena.c. */
|
|
|
bca718 |
+ INTERNAL_SIZE_T attached_threads;
|
|
|
bca718 |
|
|
|
bca718 |
/* Memory allocated from the system in this arena. */
|
|
|
bca718 |
INTERNAL_SIZE_T system_mem;
|
|
|
bca718 |
@@ -1772,7 +1777,8 @@ struct malloc_par {
|
|
|
bca718 |
static struct malloc_state main_arena =
|
|
|
bca718 |
{
|
|
|
bca718 |
.mutex = MUTEX_INITIALIZER,
|
|
|
bca718 |
- .next = &main_arena
|
|
|
bca718 |
+ .next = &main_arena,
|
|
|
bca718 |
+ .attached_threads = 1,
|
|
|
bca718 |
};
|
|
|
bca718 |
|
|
|
bca718 |
/* There is only one instance of the malloc parameters. */
|
|
|
bca718 |
Index: b/malloc/Makefile
|
|
|
bca718 |
===================================================================
|
|
|
bca718 |
--- a/malloc/Makefile
|
|
|
bca718 |
+++ b/malloc/Makefile
|
|
|
bca718 |
@@ -20,13 +20,14 @@
|
|
|
bca718 |
#
|
|
|
bca718 |
subdir := malloc
|
|
|
bca718 |
|
|
|
bca718 |
-all:
|
|
|
bca718 |
+include ../Makeconfig
|
|
|
bca718 |
|
|
|
bca718 |
dist-headers := malloc.h
|
|
|
bca718 |
headers := $(dist-headers) obstack.h mcheck.h
|
|
|
bca718 |
tests := mallocbug tst-malloc tst-valloc tst-calloc tst-obstack \
|
|
|
bca718 |
tst-mallocstate tst-mcheck tst-mallocfork tst-trim1 \
|
|
|
bca718 |
- tst-malloc-usable tst-malloc-backtrace
|
|
|
bca718 |
+ tst-malloc-usable \
|
|
|
bca718 |
+ tst-malloc-backtrace tst-malloc-thread-exit
|
|
|
bca718 |
test-srcs = tst-mtrace
|
|
|
bca718 |
|
|
|
bca718 |
routines = malloc morecore mcheck mtrace obstack
|
|
|
bca718 |
@@ -43,6 +44,8 @@ libmemusage-inhibit-o = $(filter-out .os
|
|
|
bca718 |
|
|
|
bca718 |
$(objpfx)tst-malloc-backtrace: $(common-objpfx)nptl/libpthread.so \
|
|
|
bca718 |
$(common-objpfx)nptl/libpthread_nonshared.a
|
|
|
bca718 |
+$(objpfx)tst-malloc-thread-exit: $(common-objpfx)nptl/libpthread.so \
|
|
|
bca718 |
+ $(common-objpfx)nptl/libpthread_nonshared.a
|
|
|
bca718 |
|
|
|
bca718 |
# These should be removed by `make clean'.
|
|
|
bca718 |
extra-objs = mcheck-init.o libmcheck.a
|
|
|
bca718 |
@@ -50,8 +53,6 @@ extra-objs = mcheck-init.o libmcheck.a
|
|
|
bca718 |
# Include the cleanup handler.
|
|
|
bca718 |
aux := set-freeres thread-freeres
|
|
|
bca718 |
|
|
|
bca718 |
-include ../Makeconfig
|
|
|
bca718 |
-
|
|
|
bca718 |
CPPFLAGS-memusagestat = -DNOT_IN_libc
|
|
|
bca718 |
|
|
|
bca718 |
# The Perl script to analyze the output of the mtrace functions.
|
|
|
bca718 |
Index: b/malloc/tst-malloc-thread-exit.c
|
|
|
bca718 |
===================================================================
|
|
|
bca718 |
--- /dev/null
|
|
|
bca718 |
+++ b/malloc/tst-malloc-thread-exit.c
|
|
|
bca718 |
@@ -0,0 +1,218 @@
|
|
|
bca718 |
+/* Test malloc with concurrent thread termination.
|
|
|
bca718 |
+ Copyright (C) 2015-2016 Free Software Foundation, Inc.
|
|
|
bca718 |
+ This file is part of the GNU C Library.
|
|
|
bca718 |
+
|
|
|
bca718 |
+ The GNU C Library is free software; you can redistribute it and/or
|
|
|
bca718 |
+ modify it under the terms of the GNU Lesser General Public
|
|
|
bca718 |
+ License as published by the Free Software Foundation; either
|
|
|
bca718 |
+ version 2.1 of the License, or (at your option) any later version.
|
|
|
bca718 |
+
|
|
|
bca718 |
+ The GNU C Library is distributed in the hope that it will be useful,
|
|
|
bca718 |
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
bca718 |
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
bca718 |
+ Lesser General Public License for more details.
|
|
|
bca718 |
+
|
|
|
bca718 |
+ You should have received a copy of the GNU Lesser General Public
|
|
|
bca718 |
+ License along with the GNU C Library; if not, see
|
|
|
bca718 |
+ <http://www.gnu.org/licenses/>. */
|
|
|
bca718 |
+
|
|
|
bca718 |
+/* This thread spawns a number of outer threads, equal to the arena
|
|
|
bca718 |
+ limit. The outer threads run a loop which start and join two
|
|
|
bca718 |
+ different kinds of threads: the first kind allocates (attaching an
|
|
|
bca718 |
+ arena to the thread; malloc_first_thread) and waits, the second
|
|
|
bca718 |
+ kind waits and allocates (wait_first_threads). Both kinds of
|
|
|
bca718 |
+ threads exit immediately after waiting. The hope is that this will
|
|
|
bca718 |
+ exhibit races in thread termination and arena management,
|
|
|
bca718 |
+ particularly related to the arena free list. */
|
|
|
bca718 |
+
|
|
|
bca718 |
+#include <errno.h>
|
|
|
bca718 |
+#include <malloc.h>
|
|
|
bca718 |
+#include <pthread.h>
|
|
|
bca718 |
+#include <stdbool.h>
|
|
|
bca718 |
+#include <stdio.h>
|
|
|
bca718 |
+#include <stdlib.h>
|
|
|
bca718 |
+#include <unistd.h>
|
|
|
bca718 |
+
|
|
|
bca718 |
+static int do_test (void);
|
|
|
bca718 |
+
|
|
|
bca718 |
+#define TEST_FUNCTION do_test ()
|
|
|
bca718 |
+#include "../test-skeleton.c"
|
|
|
bca718 |
+
|
|
|
bca718 |
+static bool termination_requested;
|
|
|
bca718 |
+static int inner_thread_count = 4;
|
|
|
bca718 |
+static size_t malloc_size = 32;
|
|
|
bca718 |
+
|
|
|
bca718 |
+static void
|
|
|
bca718 |
+__attribute__ ((noinline, noclone))
|
|
|
bca718 |
+unoptimized_free (void *ptr)
|
|
|
bca718 |
+{
|
|
|
bca718 |
+ free (ptr);
|
|
|
bca718 |
+}
|
|
|
bca718 |
+
|
|
|
bca718 |
+static void *
|
|
|
bca718 |
+malloc_first_thread (void * closure)
|
|
|
bca718 |
+{
|
|
|
bca718 |
+ pthread_barrier_t *barrier = closure;
|
|
|
bca718 |
+ void *ptr = malloc (malloc_size);
|
|
|
bca718 |
+ if (ptr == NULL)
|
|
|
bca718 |
+ {
|
|
|
bca718 |
+ printf ("error: malloc: %m\n");
|
|
|
bca718 |
+ abort ();
|
|
|
bca718 |
+ }
|
|
|
bca718 |
+ int ret = pthread_barrier_wait (barrier);
|
|
|
bca718 |
+ if (ret != 0 && ret != PTHREAD_BARRIER_SERIAL_THREAD)
|
|
|
bca718 |
+ {
|
|
|
bca718 |
+ errno = ret;
|
|
|
bca718 |
+ printf ("error: pthread_barrier_wait: %m\n");
|
|
|
bca718 |
+ abort ();
|
|
|
bca718 |
+ }
|
|
|
bca718 |
+ unoptimized_free (ptr);
|
|
|
bca718 |
+ return NULL;
|
|
|
bca718 |
+}
|
|
|
bca718 |
+
|
|
|
bca718 |
+static void *
|
|
|
bca718 |
+wait_first_thread (void * closure)
|
|
|
bca718 |
+{
|
|
|
bca718 |
+ pthread_barrier_t *barrier = closure;
|
|
|
bca718 |
+ int ret = pthread_barrier_wait (barrier);
|
|
|
bca718 |
+ if (ret != 0 && ret != PTHREAD_BARRIER_SERIAL_THREAD)
|
|
|
bca718 |
+ {
|
|
|
bca718 |
+ errno = ret;
|
|
|
bca718 |
+ printf ("error: pthread_barrier_wait: %m\n");
|
|
|
bca718 |
+ abort ();
|
|
|
bca718 |
+ }
|
|
|
bca718 |
+ void *ptr = malloc (malloc_size);
|
|
|
bca718 |
+ if (ptr == NULL)
|
|
|
bca718 |
+ {
|
|
|
bca718 |
+ printf ("error: malloc: %m\n");
|
|
|
bca718 |
+ abort ();
|
|
|
bca718 |
+ }
|
|
|
bca718 |
+ unoptimized_free (ptr);
|
|
|
bca718 |
+ return NULL;
|
|
|
bca718 |
+}
|
|
|
bca718 |
+
|
|
|
bca718 |
+static void *
|
|
|
bca718 |
+outer_thread (void *closure)
|
|
|
bca718 |
+{
|
|
|
bca718 |
+ pthread_t *threads = calloc (sizeof (*threads), inner_thread_count);
|
|
|
bca718 |
+ if (threads == NULL)
|
|
|
bca718 |
+ {
|
|
|
bca718 |
+ printf ("error: calloc: %m\n");
|
|
|
bca718 |
+ abort ();
|
|
|
bca718 |
+ }
|
|
|
bca718 |
+
|
|
|
bca718 |
+ while (!__atomic_load_n (&termination_requested, __ATOMIC_RELAXED))
|
|
|
bca718 |
+ {
|
|
|
bca718 |
+ pthread_barrier_t barrier;
|
|
|
bca718 |
+ int ret = pthread_barrier_init (&barrier, NULL, inner_thread_count + 1);
|
|
|
bca718 |
+ if (ret != 0)
|
|
|
bca718 |
+ {
|
|
|
bca718 |
+ errno = ret;
|
|
|
bca718 |
+ printf ("pthread_barrier_init: %m\n");
|
|
|
bca718 |
+ abort ();
|
|
|
bca718 |
+ }
|
|
|
bca718 |
+ for (int i = 0; i < inner_thread_count; ++i)
|
|
|
bca718 |
+ {
|
|
|
bca718 |
+ void *(*func) (void *);
|
|
|
bca718 |
+ if ((i % 2) == 0)
|
|
|
bca718 |
+ func = malloc_first_thread;
|
|
|
bca718 |
+ else
|
|
|
bca718 |
+ func = wait_first_thread;
|
|
|
bca718 |
+ ret = pthread_create (threads + i, NULL, func, &barrier);
|
|
|
bca718 |
+ if (ret != 0)
|
|
|
bca718 |
+ {
|
|
|
bca718 |
+ errno = ret;
|
|
|
bca718 |
+ printf ("error: pthread_create: %m\n");
|
|
|
bca718 |
+ abort ();
|
|
|
bca718 |
+ }
|
|
|
bca718 |
+ }
|
|
|
bca718 |
+ ret = pthread_barrier_wait (&barrier);
|
|
|
bca718 |
+ if (ret != 0 && ret != PTHREAD_BARRIER_SERIAL_THREAD)
|
|
|
bca718 |
+ {
|
|
|
bca718 |
+ errno = ret;
|
|
|
bca718 |
+ printf ("pthread_wait: %m\n");
|
|
|
bca718 |
+ abort ();
|
|
|
bca718 |
+ }
|
|
|
bca718 |
+ for (int i = 0; i < inner_thread_count; ++i)
|
|
|
bca718 |
+ {
|
|
|
bca718 |
+ ret = pthread_join (threads[i], NULL);
|
|
|
bca718 |
+ if (ret != 0)
|
|
|
bca718 |
+ {
|
|
|
bca718 |
+ ret = errno;
|
|
|
bca718 |
+ printf ("error: pthread_join: %m\n");
|
|
|
bca718 |
+ abort ();
|
|
|
bca718 |
+ }
|
|
|
bca718 |
+ }
|
|
|
bca718 |
+ ret = pthread_barrier_destroy (&barrier);
|
|
|
bca718 |
+ if (ret != 0)
|
|
|
bca718 |
+ {
|
|
|
bca718 |
+ ret = errno;
|
|
|
bca718 |
+ printf ("pthread_barrier_destroy: %m\n");
|
|
|
bca718 |
+ abort ();
|
|
|
bca718 |
+ }
|
|
|
bca718 |
+ }
|
|
|
bca718 |
+
|
|
|
bca718 |
+ free (threads);
|
|
|
bca718 |
+
|
|
|
bca718 |
+ return NULL;
|
|
|
bca718 |
+}
|
|
|
bca718 |
+
|
|
|
bca718 |
+static int
|
|
|
bca718 |
+do_test (void)
|
|
|
bca718 |
+{
|
|
|
bca718 |
+ /* The number of threads should be smaller than the number of
|
|
|
bca718 |
+ arenas, so that there will be some free arenas to add to the
|
|
|
bca718 |
+ arena free list. */
|
|
|
bca718 |
+ enum { outer_thread_count = 2 };
|
|
|
bca718 |
+ if (mallopt (M_ARENA_MAX, 8) == 0)
|
|
|
bca718 |
+ {
|
|
|
bca718 |
+ printf ("error: mallopt (M_ARENA_MAX) failed\n");
|
|
|
bca718 |
+ return 1;
|
|
|
bca718 |
+ }
|
|
|
bca718 |
+
|
|
|
bca718 |
+ /* Leave some room for shutting down all threads gracefully. */
|
|
|
bca718 |
+ int timeout = 3;
|
|
|
bca718 |
+ if (timeout > TIMEOUT)
|
|
|
bca718 |
+ timeout = TIMEOUT - 1;
|
|
|
bca718 |
+
|
|
|
bca718 |
+ pthread_t *threads = calloc (sizeof (*threads), outer_thread_count);
|
|
|
bca718 |
+ if (threads == NULL)
|
|
|
bca718 |
+ {
|
|
|
bca718 |
+ printf ("error: calloc: %m\n");
|
|
|
bca718 |
+ abort ();
|
|
|
bca718 |
+ }
|
|
|
bca718 |
+
|
|
|
bca718 |
+ for (long i = 0; i < outer_thread_count; ++i)
|
|
|
bca718 |
+ {
|
|
|
bca718 |
+ int ret = pthread_create (threads + i, NULL, outer_thread, NULL);
|
|
|
bca718 |
+ if (ret != 0)
|
|
|
bca718 |
+ {
|
|
|
bca718 |
+ errno = ret;
|
|
|
bca718 |
+ printf ("error: pthread_create: %m\n");
|
|
|
bca718 |
+ abort ();
|
|
|
bca718 |
+ }
|
|
|
bca718 |
+ }
|
|
|
bca718 |
+
|
|
|
bca718 |
+ struct timespec ts = {timeout, 0};
|
|
|
bca718 |
+ if (nanosleep (&ts, NULL))
|
|
|
bca718 |
+ {
|
|
|
bca718 |
+ printf ("error: error: nanosleep: %m\n");
|
|
|
bca718 |
+ abort ();
|
|
|
bca718 |
+ }
|
|
|
bca718 |
+
|
|
|
bca718 |
+ __atomic_store_n (&termination_requested, true, __ATOMIC_RELAXED);
|
|
|
bca718 |
+
|
|
|
bca718 |
+ for (long i = 0; i < outer_thread_count; ++i)
|
|
|
bca718 |
+ {
|
|
|
bca718 |
+ int ret = pthread_join (threads[i], NULL);
|
|
|
bca718 |
+ if (ret != 0)
|
|
|
bca718 |
+ {
|
|
|
bca718 |
+ errno = ret;
|
|
|
bca718 |
+ printf ("error: pthread_join: %m\n");
|
|
|
bca718 |
+ abort ();
|
|
|
bca718 |
+ }
|
|
|
bca718 |
+ }
|
|
|
bca718 |
+ free (threads);
|
|
|
bca718 |
+
|
|
|
bca718 |
+ return 0;
|
|
|
bca718 |
+}
|