5f7b84
commit cb89ba9c72f66327f5d66034681eb1d46eedf96f
5f7b84
Author: DJ Delorie <dj@redhat.com>
5f7b84
Date:   Thu Aug 8 19:09:43 2019 -0400
5f7b84
5f7b84
    Add glibc.malloc.mxfast tunable
5f7b84
    
5f7b84
    * elf/dl-tunables.list: Add glibc.malloc.mxfast.
5f7b84
    * manual/tunables.texi: Document it.
5f7b84
    * malloc/malloc.c (do_set_mxfast): New.
5f7b84
    (__libc_mallopt): Call it.
5f7b84
    * malloc/arena.c: Add mxfast tunable.
5f7b84
    * malloc/tst-mxfast.c: New.
5f7b84
    * malloc/Makefile: Add it.
5f7b84
    
5f7b84
    Reviewed-by: Carlos O'Donell <carlos@redhat.com>
5f7b84
    (cherry picked from commit c48d92b430c480de06762f80c104922239416826)
5f7b84
5f7b84
diff --git a/elf/dl-tunables.list b/elf/dl-tunables.list
5f7b84
index 1f8ecb8437a0460f..1ff6fcb6f24f93a8 100644
5f7b84
--- a/elf/dl-tunables.list
5f7b84
+++ b/elf/dl-tunables.list
5f7b84
@@ -85,6 +85,11 @@ glibc {
5f7b84
     tcache_unsorted_limit {
5f7b84
       type: SIZE_T
5f7b84
     }
5f7b84
+    mxfast {
5f7b84
+      type: SIZE_T
5f7b84
+      minval: 0
5f7b84
+      security_level: SXID_IGNORE
5f7b84
+    }
5f7b84
   }
5f7b84
   tune {
5f7b84
     hwcap_mask {
5f7b84
diff --git a/malloc/Makefile b/malloc/Makefile
5f7b84
index 228a1279a5960d8c..bf9a53cb7c5ebacb 100644
5f7b84
--- a/malloc/Makefile
5f7b84
+++ b/malloc/Makefile
5f7b84
@@ -39,6 +39,7 @@ tests := mallocbug tst-malloc tst-valloc tst-calloc tst-obstack \
5f7b84
 	 tst-malloc-too-large \
5f7b84
 	 tst-malloc-stats-cancellation \
5f7b84
 	 tst-tcfree1 tst-tcfree2 tst-tcfree3 \
5f7b84
+	 tst-mxfast \
5f7b84
 
5f7b84
 tests-static := \
5f7b84
 	 tst-interpose-static-nothread \
5f7b84
@@ -196,6 +197,8 @@ tst-malloc-usable-static-ENV = $(tst-malloc-usable-ENV)
5f7b84
 tst-malloc-usable-tunables-ENV = GLIBC_TUNABLES=glibc.malloc.check=3
5f7b84
 tst-malloc-usable-static-tunables-ENV = $(tst-malloc-usable-tunables-ENV)
5f7b84
 
5f7b84
+tst-mxfast-ENV = GLIBC_TUNABLES=glibc.malloc.tcache_count=0:glibc.malloc.mxfast=0
5f7b84
+
5f7b84
 ifeq ($(experimental-malloc),yes)
5f7b84
 CPPFLAGS-malloc.c += -DUSE_TCACHE=1
5f7b84
 else
5f7b84
diff --git a/malloc/arena.c b/malloc/arena.c
5f7b84
index ff8fd5d2a7e51ac8..f5c7ad4570ad6186 100644
5f7b84
--- a/malloc/arena.c
5f7b84
+++ b/malloc/arena.c
5f7b84
@@ -237,6 +237,7 @@ TUNABLE_CALLBACK_FNDECL (set_tcache_max, size_t)
5f7b84
 TUNABLE_CALLBACK_FNDECL (set_tcache_count, size_t)
5f7b84
 TUNABLE_CALLBACK_FNDECL (set_tcache_unsorted_limit, size_t)
5f7b84
 #endif
5f7b84
+TUNABLE_CALLBACK_FNDECL (set_mxfast, size_t)
5f7b84
 #else
5f7b84
 /* Initialization routine. */
5f7b84
 #include <string.h>
5f7b84
@@ -324,6 +325,7 @@ ptmalloc_init (void)
5f7b84
   TUNABLE_GET (tcache_unsorted_limit, size_t,
5f7b84
 	       TUNABLE_CALLBACK (set_tcache_unsorted_limit));
5f7b84
 # endif
5f7b84
+  TUNABLE_GET (mxfast, size_t, TUNABLE_CALLBACK (set_mxfast));
5f7b84
 #else
5f7b84
   const char *s = NULL;
5f7b84
   if (__glibc_likely (_environ != NULL))
5f7b84
diff --git a/malloc/malloc.c b/malloc/malloc.c
5f7b84
index fcf480acdaea1b86..9756ed0a0d28c5f6 100644
5f7b84
--- a/malloc/malloc.c
5f7b84
+++ b/malloc/malloc.c
5f7b84
@@ -5142,6 +5142,19 @@ do_set_tcache_unsorted_limit (size_t value)
5f7b84
 }
5f7b84
 #endif
5f7b84
 
5f7b84
+static inline int
5f7b84
+__always_inline
5f7b84
+do_set_mxfast (size_t value)
5f7b84
+{
5f7b84
+  if (value >= 0 && value <= MAX_FAST_SIZE)
5f7b84
+    {
5f7b84
+      LIBC_PROBE (memory_mallopt_mxfast, 2, value, get_max_fast ());
5f7b84
+      set_max_fast (value);
5f7b84
+      return 1;
5f7b84
+    }
5f7b84
+  return 0;
5f7b84
+}
5f7b84
+
5f7b84
 int
5f7b84
 __libc_mallopt (int param_number, int value)
5f7b84
 {
5f7b84
@@ -5161,13 +5174,7 @@ __libc_mallopt (int param_number, int value)
5f7b84
   switch (param_number)
5f7b84
     {
5f7b84
     case M_MXFAST:
5f7b84
-      if (value >= 0 && value <= MAX_FAST_SIZE)
5f7b84
-        {
5f7b84
-          LIBC_PROBE (memory_mallopt_mxfast, 2, value, get_max_fast ());
5f7b84
-          set_max_fast (value);
5f7b84
-        }
5f7b84
-      else
5f7b84
-        res = 0;
5f7b84
+      do_set_mxfast (value);
5f7b84
       break;
5f7b84
 
5f7b84
     case M_TRIM_THRESHOLD:
5f7b84
diff --git a/malloc/tst-mxfast.c b/malloc/tst-mxfast.c
5f7b84
new file mode 100644
5f7b84
index 0000000000000000..7a371d2f9d2f0005
5f7b84
--- /dev/null
5f7b84
+++ b/malloc/tst-mxfast.c
5f7b84
@@ -0,0 +1,50 @@
5f7b84
+/* Test that glibc.malloc.mxfast tunable works.
5f7b84
+   Copyright (C) 2018, 2019 Free Software Foundation, Inc.
5f7b84
+   This file is part of the GNU C Library.
5f7b84
+
5f7b84
+   The GNU C Library is free software; you can redistribute it and/or
5f7b84
+   modify it under the terms of the GNU Lesser General Public
5f7b84
+   License as published by the Free Software Foundation; either
5f7b84
+   version 2.1 of the License, or (at your option) any later version.
5f7b84
+
5f7b84
+   The GNU C Library is distributed in the hope that it will be useful,
5f7b84
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
5f7b84
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
5f7b84
+   Lesser General Public License for more details.
5f7b84
+
5f7b84
+   You should have received a copy of the GNU Lesser General Public
5f7b84
+   License along with the GNU C Library; if not, see
5f7b84
+   <http://www.gnu.org/licenses/>.  */
5f7b84
+
5f7b84
+/* This test verifies that setting the glibc.malloc.mxfast tunable to
5f7b84
+   zero results in free'd blocks being returned to the small bins, not
5f7b84
+   the fast bins.  */
5f7b84
+
5f7b84
+#include <malloc.h>
5f7b84
+#include <assert.h>
5f7b84
+
5f7b84
+int
5f7b84
+do_test(void)
5f7b84
+{
5f7b84
+  struct mallinfo m;
5f7b84
+  char * volatile p1;
5f7b84
+  char * volatile p2;
5f7b84
+
5f7b84
+  /* Arbitrary value; must be in default fastbin range.  */
5f7b84
+  p1 = malloc (3);
5f7b84
+  /* Something large so that p1 isn't a "top block" */
5f7b84
+  p2 = malloc (512);
5f7b84
+  free (p1);
5f7b84
+
5f7b84
+  m = mallinfo();
5f7b84
+
5f7b84
+  /* This will fail if there are any blocks in the fastbins.  */
5f7b84
+  assert (m.smblks == 0);
5f7b84
+
5f7b84
+  /* To keep gcc happy.  */
5f7b84
+  free (p2);
5f7b84
+
5f7b84
+  return 0;
5f7b84
+}
5f7b84
+
5f7b84
+#include <support/test-driver.c>
5f7b84
diff --git a/manual/tunables.texi b/manual/tunables.texi
5f7b84
index f6c49250e3889ddd..3dc6f9a44592c030 100644
5f7b84
--- a/manual/tunables.texi
5f7b84
+++ b/manual/tunables.texi
5f7b84
@@ -213,6 +213,18 @@ pre-fill the per-thread cache with.  The default, or when set to zero,
5f7b84
 is no limit.
5f7b84
 @end deftp
5f7b84
 
5f7b84
+@deftp Tunable glibc.malloc.mxfast
5f7b84
+One of the optimizations malloc uses is to maintain a series of ``fast
5f7b84
+bins'' that hold chunks up to a specific size.  The default and
5f7b84
+maximum size which may be held this way is 80 bytes on 32-bit systems
5f7b84
+or 160 bytes on 64-bit systems.  Applications which value size over
5f7b84
+speed may choose to reduce the size of requests which are serviced
5f7b84
+from fast bins with this tunable.  Note that the value specified
5f7b84
+includes malloc's internal overhead, which is normally the size of one
5f7b84
+pointer, so add 4 on 32-bit systems or 8 on 64-bit systems to the size
5f7b84
+passed to @code{malloc} for the largest bin size to enable.
5f7b84
+@end deftp
5f7b84
+
5f7b84
 @node Elision Tunables
5f7b84
 @section Elision Tunables
5f7b84
 @cindex elision tunables