Blame SOURCES/glibc-rh1065574-6.patch

147e83
commit 7a9368a1174cb15b9f1d6342e0e10dd90dae238d
147e83
Author: Florian Weimer <fweimer@redhat.com>
147e83
Date:   Wed Nov 15 11:39:01 2017 +0100
147e83
147e83
    malloc: Add missing arena lock in malloc_info [BZ #22408]
147e83
    
147e83
    Obtain the size information while the arena lock is acquired, and only
147e83
    print it later.
147e83
147e83
Conflicts:
147e83
	malloc/Makefile
147e83
	  (Differences in available tests.)
147e83
147e83
diff --git a/malloc/Makefile b/malloc/Makefile
147e83
index 992cec6b03115a76..6f216f423293dc6c 100644
147e83
--- a/malloc/Makefile
147e83
+++ b/malloc/Makefile
147e83
@@ -31,6 +31,7 @@ tests := mallocbug tst-malloc tst-valloc tst-calloc tst-obstack \
147e83
 	 tst-malloc-thread-fail tst-malloc-fork-deadlock \
147e83
 	 tst-interpose-nothread \
147e83
 	 tst-interpose-thread \
147e83
+	 tst-malloc_info \
147e83
 	 tst-interpose-static-nothread \
147e83
 	 tst-interpose-static-thread \
147e83
 	 tst-scratch_buffer \
147e83
@@ -214,3 +215,5 @@ $(objpfx)tst-dynarray-mem: $(objpfx)tst-dynarray.out
147e83
 tst-dynarray-fail-ENV = MALLOC_TRACE=$(objpfx)tst-dynarray-fail.mtrace
147e83
 $(objpfx)tst-dynarray-fail-mem: $(objpfx)tst-dynarray-fail.out
147e83
 	$(common-objpfx)malloc/mtrace $(objpfx)tst-dynarray-fail.mtrace > $@
147e83
+
147e83
+$(objpfx)tst-malloc_info: $(shared-thread-library)
147e83
diff --git a/malloc/malloc.c b/malloc/malloc.c
147e83
index d2a5e251da4f1191..035f2167be7019d8 100644
147e83
--- a/malloc/malloc.c
147e83
+++ b/malloc/malloc.c
147e83
@@ -5108,6 +5108,15 @@ __malloc_info (int options, FILE *fp)
147e83
 	  avail += sizes[NFASTBINS - 1 + i].total;
147e83
 	}
147e83
 
147e83
+      size_t heap_size = 0;
147e83
+      size_t heap_mprotect_size = 0;
147e83
+      if (ar_ptr != &main_arena)
147e83
+	{
147e83
+	  heap_info *heap = heap_for_ptr (top (ar_ptr));
147e83
+	  heap_size = heap->size;
147e83
+	  heap_mprotect_size = heap->mprotect_size;
147e83
+	}
147e83
+
147e83
       __libc_lock_unlock (ar_ptr->mutex);
147e83
 
147e83
       total_nfastblocks += nfastblocks;
147e83
@@ -5141,13 +5150,12 @@ __malloc_info (int options, FILE *fp)
147e83
 
147e83
       if (ar_ptr != &main_arena)
147e83
 	{
147e83
-	  heap_info *heap = heap_for_ptr (top (ar_ptr));
147e83
 	  fprintf (fp,
147e83
 		   "<aspace type=\"total\" size=\"%zu\"/>\n"
147e83
 		   "<aspace type=\"mprotect\" size=\"%zu\"/>\n",
147e83
-		   heap->size, heap->mprotect_size);
147e83
-	  total_aspace += heap->size;
147e83
-	  total_aspace_mprotect += heap->mprotect_size;
147e83
+		   heap_size, heap_mprotect_size);
147e83
+	  total_aspace += heap_size;
147e83
+	  total_aspace_mprotect += heap_mprotect_size;
147e83
 	}
147e83
       else
147e83
 	{
147e83
diff --git a/malloc/tst-malloc_info.c b/malloc/tst-malloc_info.c
147e83
new file mode 100644
147e83
index 0000000000000000..a25b8cbeae78e710
147e83
--- /dev/null
147e83
+++ b/malloc/tst-malloc_info.c
147e83
@@ -0,0 +1,101 @@
147e83
+/* Smoke test for malloc_info.
147e83
+   Copyright (C) 2017 Free Software Foundation, Inc.
147e83
+   This file is part of the GNU C Library.
147e83
+
147e83
+   The GNU C Library is free software; you can redistribute it and/or
147e83
+   modify it under the terms of the GNU Lesser General Public
147e83
+   License as published by the Free Software Foundation; either
147e83
+   version 2.1 of the License, or (at your option) any later version.
147e83
+
147e83
+   The GNU C Library is distributed in the hope that it will be useful,
147e83
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
147e83
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
147e83
+   Lesser General Public License for more details.
147e83
+
147e83
+   You should have received a copy of the GNU Lesser General Public
147e83
+   License along with the GNU C Library; if not, see
147e83
+   <http://www.gnu.org/licenses/>.  */
147e83
+
147e83
+/* The purpose of this test is to provide a quick way to run
147e83
+   malloc_info in a multi-threaded process.  */
147e83
+
147e83
+#include <array_length.h>
147e83
+#include <malloc.h>
147e83
+#include <stdlib.h>
147e83
+#include <support/support.h>
147e83
+#include <support/xthread.h>
147e83
+
147e83
+/* This barrier is used to have the main thread wait until the helper
147e83
+   threads have performed their allocations.  */
147e83
+static pthread_barrier_t barrier;
147e83
+
147e83
+enum
147e83
+  {
147e83
+    /* Number of threads performing allocations.  */
147e83
+    thread_count  = 4,
147e83
+
147e83
+    /* Amount of memory allocation per thread.  This should be large
147e83
+       enough to cause the allocation of multiple heaps per arena.  */
147e83
+    per_thread_allocations
147e83
+      = sizeof (void *) == 4 ? 16 * 1024 * 1024 : 128 * 1024 * 1024,
147e83
+  };
147e83
+
147e83
+static void *
147e83
+allocation_thread_function (void *closure)
147e83
+{
147e83
+  struct list
147e83
+  {
147e83
+    struct list *next;
147e83
+    long dummy[4];
147e83
+  };
147e83
+
147e83
+  struct list *head = NULL;
147e83
+  size_t allocated = 0;
147e83
+  while (allocated < per_thread_allocations)
147e83
+    {
147e83
+      struct list *new_head = xmalloc (sizeof (*new_head));
147e83
+      allocated += sizeof (*new_head);
147e83
+      new_head->next = head;
147e83
+      head = new_head;
147e83
+    }
147e83
+
147e83
+  xpthread_barrier_wait (&barrier);
147e83
+
147e83
+  /* Main thread prints first statistics here.  */
147e83
+
147e83
+  xpthread_barrier_wait (&barrier);
147e83
+
147e83
+  while (head != NULL)
147e83
+    {
147e83
+      struct list *next_head = head->next;
147e83
+      free (head);
147e83
+      head = next_head;
147e83
+    }
147e83
+
147e83
+  return NULL;
147e83
+}
147e83
+
147e83
+static int
147e83
+do_test (void)
147e83
+{
147e83
+  xpthread_barrier_init (&barrier, NULL, thread_count + 1);
147e83
+
147e83
+  pthread_t threads[thread_count];
147e83
+  for (size_t i = 0; i < array_length (threads); ++i)
147e83
+    threads[i] = xpthread_create (NULL, allocation_thread_function, NULL);
147e83
+
147e83
+  xpthread_barrier_wait (&barrier);
147e83
+  puts ("info: After allocation:");
147e83
+  malloc_info (0, stdout);
147e83
+
147e83
+  xpthread_barrier_wait (&barrier);
147e83
+  for (size_t i = 0; i < array_length (threads); ++i)
147e83
+    xpthread_join (threads[i]);
147e83
+
147e83
+  puts ("\ninfo: After deallocation:");
147e83
+  malloc_info (0, stdout);
147e83
+
147e83
+  return 0;
147e83
+}
147e83
+
147e83
+#include <support/test-driver.c>