]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
elf: Add glibc.mem.decorate_maps tunable
authorAdhemerval Zanella <adhemerval.zanella@linaro.org>
Wed, 1 Nov 2023 12:56:11 +0000 (09:56 -0300)
committerAdhemerval Zanella <adhemerval.zanella@linaro.org>
Tue, 7 Nov 2023 13:27:57 +0000 (10:27 -0300)
The PR_SET_VMA_ANON_NAME support is only enabled through a configurable
kernel switch, mainly because assigning a name to a
anonymous virtual memory area might prevent that area from being
merged with adjacent virtual memory areas.

For instance, with the following code:

   void *p1 = mmap (NULL,
                    1024 * 4096,
                    PROT_READ | PROT_WRITE,
                    MAP_PRIVATE | MAP_ANONYMOUS,
                    -1,
                    0);

   void *p2 = mmap (p1 + (1024 * 4096),
                    1024 * 4096,
                    PROT_READ | PROT_WRITE,
                    MAP_PRIVATE | MAP_ANONYMOUS,
                    -1,
                    0);

The kernel will potentially merge both mappings resulting in only one
segment of size 0x800000.  If the segment is names with
PR_SET_VMA_ANON_NAME with different names, it results in two mappings.

Although this will unlikely be an issue for pthread stacks and malloc
arenas (since for pthread stacks the guard page will result in
a PROT_NONE segment, similar to the alignment requirement for the arena
block), it still might prevent the mmap memory allocated for detail
malloc.

There is also another potential scalability issue, where the prctl
requires
to take the mmap global lock which is still not fully fixed in Linux
[1] (for pthread stacks and arenas, it is mitigated by the stack
cached and the arena reuse).

So this patch disables anonymous mapping annotations as default and
add a new tunable, glibc.mem.decorate_maps, can be used to enable
it.

[1] https://lwn.net/Articles/906852/

Checked on x86_64-linux-gnu and aarch64-linux-gnu.
Reviewed-by: DJ Delorie <dj@redhat.com>
NEWS
elf/Makefile
elf/dl-tunables.list
manual/tunables.texi
sysdeps/unix/sysv/linux/setvmaname.c

diff --git a/NEWS b/NEWS
index 4580fe381d63bd709bdb8b4055b1d40b902e029e..139cfef1b03193ddadb19c61119c092ad4e24e6b 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -38,6 +38,11 @@ Major new features:
   and the wfN format length modifiers for arguments pointing to types
   int_fastN_t or uint_fastN_t, as specified in draft ISO C2X.
 
+* A new tunable, glibc.mem.decorate_maps, can be used to add additional
+  information on underlying memory allocated by the glibc (for instance,
+  on thread stack created by pthread_create or memory allocated by
+  malloc).
+
 Deprecated and removed features, and other changes affecting compatibility:
 
 * The ldconfig program now skips file names containing ';' or ending in
index 328dbe82de08b0695fe18f23de74591ce2b96791..f9bd86a05a7e70e37c71fcc72e42764662470702 100644 (file)
@@ -2985,5 +2985,5 @@ $(objpfx)tst-dlclose-lazy.out: \
 $(objpfx)tst-decorate-maps: $(shared-thread-library)
 
 tst-decorate-maps-ENV = \
-  GLIBC_TUNABLES=glibc.malloc.arena_max=8:glibc.malloc.mmap_threshold=1024
+  GLIBC_TUNABLES=glibc.malloc.arena_max=8:glibc.malloc.mmap_threshold=1024:glibc.mem.decorate_maps=1
 tst-decorate-maps-ARGS = 8
index 695ba7192e5f3abeab41801fe5da8cc0f86822bc..888d2ede0420fbaf30adf1229e86ad92183e7d30 100644 (file)
@@ -160,6 +160,11 @@ glibc {
       maxval: 255
       security_level: SXID_IGNORE
     }
+    decorate_maps {
+      type: INT_32
+      minval: 0
+      maxval: 1
+    }
   }
 
   rtld {
index 776fd93fd99741ad4ee99e6553e819538c851e29..c28360adcdc06b19e83a430cacedf2bc6a331e6a 100644 (file)
@@ -653,6 +653,23 @@ support in the kernel if this tunable has any non-zero value.
 The default value is @samp{0}, which disables all memory tagging.
 @end deftp
 
+@deftp Tunable glibc.mem.decorate_maps
+If the kernel supports naming anonymous virtual memory areas (since
+Linux version 5.17, although not always enabled by some kernel
+configurations), this tunable can be used to control whether
+@theglibc{} decorates the underlying memory obtained from operating
+system with a string describing its usage (for instance, on the thread
+stack created by @code{ptthread_create} or memory allocated by
+@code{malloc}).
+
+The process mappings can be obtained by reading the @code{/proc/<pid>maps}
+(with @code{pid} being either the @dfn{process ID} or @code{self} for the
+process own mapping).
+
+This tunable takes a value of 0 and 1, where 1 enables the feature.
+The default value is @samp{0}, which disables the decoration.
+@end deftp
+
 @node gmon Tunables
 @section gmon Tunables
 @cindex gmon tunables
index 9960ab5917bc45aa3bf87054ad512e77a0215d20..cd6d571772758c4f1140aab8a3b2bc9246b6435f 100644 (file)
@@ -20,6 +20,7 @@
 #include <setvmaname.h>
 #include <sys/prctl.h>
 #include <sysdep.h>
+#include <elf/dl-tunables.h>
 
 /* If PR_SET_VMA_ANON_NAME is not supported by the kernel, prctl returns
    EINVAL.  However, it also returns the same error for invalid argument.
@@ -34,11 +35,15 @@ __set_vma_name (void *start, size_t len, const char *name)
   if (atomic_load_relaxed (&prctl_supported) == 0)
     return;
 
-  int r = INTERNAL_SYSCALL_CALL (prctl, PR_SET_VMA, PR_SET_VMA_ANON_NAME,
-                                start, len, name);
-  if (r == 0 || r != -EINVAL)
-    return;
-
+  /* Set the prctl as not supported to avoid checking the tunable on every
+     call.  */
+  if (TUNABLE_GET (glibc, mem, decorate_maps, int32_t, NULL) != 0)
+    {
+      int r = INTERNAL_SYSCALL_CALL (prctl, PR_SET_VMA, PR_SET_VMA_ANON_NAME,
+                                    start, len, name);
+      if (r == 0 || r != -EINVAL)
+       return;
+    }
   atomic_store_relaxed (&prctl_supported, 0);
   return;
 }