mbox series

[v2,0/7] Add a tunable to decorate anonymous memory maps

Message ID 20231028195559.390407-1-adhemerval.zanella@linaro.org
Headers show
Series Add a tunable to decorate anonymous memory maps | expand

Message

Adhemerval Zanella Netto Oct. 28, 2023, 7:55 p.m. UTC
Linux 4.5 removed thread stack annotations due to the complexity of
computing them, and Linux 5.17 added support to naming anonymous virtual
memory areas through the prctl syscall [1].

Android and other projects like sanitizers uses this feature to improve
debugability and a way to user understand better what kind of memory
runtime allocates.

The patchset adds annotations for the pthread stack, on both glibc
created and user-provided, on loader minimal malloc and on malloc arenas
and default allocation. For instance, on x86_64 the resulting mapping of
the newly tst-decorate-maps shows:

5649d25bd000-5649d25bf000 r--p 00000000 103:05 40141614                  <buildir>/elf/tst-decorate-maps
5649d25bf000-5649d25c3000 r-xp 00002000 103:05 40141614                  <buildir>/elf/tst-decorate-maps
5649d25c3000-5649d25c5000 r--p 00006000 103:05 40141614                  <buildir>/elf/tst-decorate-maps
5649d25c5000-5649d25c6000 r--p 00007000 103:05 40141614                  <buildir>/elf/tst-decorate-maps
5649d25c6000-5649d25c7000 rw-p 00008000 103:05 40141614                  <buildir>/elf/tst-decorate-maps
5649d36c4000-5649d36e5000 rw-p 00000000 00:00 0                          [heap]
7fc76c000000-7fc76c021000 rw-p 00000000 00:00 0                          [anon: glibc: malloc arena]
7fc76c021000-7fc770000000 ---p 00000000 00:00 0
7fc774000000-7fc774021000 rw-p 00000000 00:00 0                          [anon: glibc: malloc arena]
7fc774021000-7fc778000000 ---p 00000000 00:00 0
7fc77c000000-7fc77c021000 rw-p 00000000 00:00 0                          [anon: glibc: malloc arena]
7fc77c021000-7fc780000000 ---p 00000000 00:00 0
7fc784000000-7fc784021000 rw-p 00000000 00:00 0                          [anon: glibc: malloc arena]
7fc784021000-7fc788000000 ---p 00000000 00:00 0
7fc788000000-7fc788021000 rw-p 00000000 00:00 0                          [anon: glibc: malloc arena]
7fc788021000-7fc78c000000 ---p 00000000 00:00 0
7fc78c000000-7fc78c021000 rw-p 00000000 00:00 0                          [anon: glibc: malloc arena]
7fc78c021000-7fc790000000 ---p 00000000 00:00 0
7fc790000000-7fc790021000 rw-p 00000000 00:00 0                          [anon: glibc: malloc arena]
7fc790021000-7fc794000000 ---p 00000000 00:00 0
7fc796800000-7fc797000000 rw-p 00000000 00:00 0                          [anon: glibc: pthread stack: 2188506]
7fc797000000-7fc797800000 rw-p 00000000 00:00 0                          [anon: glibc: pthread stack: 2188505]
7fc797800000-7fc798000000 rw-p 00000000 00:00 0                          [anon: glibc: pthread stack: 2188504]
7fc798000000-7fc798800000 rw-p 00000000 00:00 0                          [anon: glibc: pthread stack: 2188503]
7fc798800000-7fc799000000 rw-p 00000000 00:00 0                          [anon: glibc: pthread stack: 2188502]
7fc799000000-7fc799800000 rw-p 00000000 00:00 0                          [anon: glibc: pthread stack: 2188501]
7fc799800000-7fc79a000000 rw-p 00000000 00:00 0                          [anon: glibc: pthread stack: 2188500]
7fc79a000000-7fc79a800000 rw-p 00000000 00:00 0                          [anon: glibc: pthread stack: 2188499]
7fc79a800000-7fc79a826000 r--p 00000000 103:05 38343761                  <buildir>/libc.so
7fc79a826000-7fc79a99a000 r-xp 00026000 103:05 38343761                  <buildir>/libc.so
7fc79a99a000-7fc79a9f1000 r--p 0019a000 103:05 38343761                  <buildir>/libc.so
7fc79a9f1000-7fc79a9f5000 r--p 001f0000 103:05 38343761                  <buildir>/libc.so
7fc79a9f5000-7fc79a9f7000 rw-p 001f4000 103:05 38343761                  <buildir>/libc.so
7fc79a9f7000-7fc79aa04000 rw-p 00000000 00:00 0
7fc79aabb000-7fc79aafb000 rw-p 00000000 00:00 0                          [anon: glibc: pthread user stack: 2188508]
7fc79aafb000-7fc79ab3b000 rw-p 00000000 00:00 0                          [anon: glibc: pthread user stack: 2188507]
7fc79ab3b000-7fc79ab7c000 rw-p 00000000 00:00 0                          [anon: glibc: malloc]
7fc79ab7c000-7fc79ab7d000 rw-s 00000000 00:01 19458                      /dev/zero (deleted)
7fc79ab7d000-7fc79ab82000 rw-p 00000000 00:00 0                          [anon: glibc: loader malloc]
7fc79ab82000-7fc79ab83000 r--p 00000000 103:05 40141609                  <buildir>/elf/ld.so
7fc79ab83000-7fc79aba9000 r-xp 00001000 103:05 40141609                  <buildir>/elf/ld.so
7fc79aba9000-7fc79abb3000 r--p 00027000 103:05 40141609                  <buildir>/elf/ld.so
7fc79abb3000-7fc79abb5000 r--p 00031000 103:05 40141609                  <buildir>/elf/ld.so
7fc79abb5000-7fc79abb7000 rw-p 00033000 103:05 40141609                  <buildir>/elf/ld.so
7ffdb2f1d000-7ffdb2f3f000 rw-p 00000000 00:00 0                          [stack]
7ffdb2f7a000-7ffdb2f7e000 r--p 00000000 00:00 0                          [vvar]
7ffdb2f7e000-7ffdb2f80000 r-xp 00000000 00:00 0                          [vdso]
ffffffffff600000-ffffffffff601000 --xp 00000000 00:00 0                  [vsyscall]

The PR_SET_VMA_ANON_NAME support is currently 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. There is also a potential
scalability issue, where the prctl requires take the mmap global lock
and it still not fully fixed in Linux [1] (for pthread stacks and
arenas, it is mitigated by the stack cached and the arena reuse).

So the decoration is only enable through a new tunable,
glibc.mem.decorate_maps.

[1] https://github.com/torvalds/linux/commit/65376df582174ffcec9e6471bf5b0dd79ba05e4a
[1] https://github.com/torvalds/linux/commit/9a10064f5625d5572c3626c1516e0bebc6c9fe9b

Changes from v1:
* Fixed arm thumb build.
* Added decoration on assert and __libc_fatal.

Adhemerval Zanella (7):
  linux: Add PR_SET_VMA_ANON_NAME support
  support: Add support_set_vma_name
  nptl: Decorate thread stack on pthread_create
  malloc: Decorate malloc maps
  assert: Decorate error message buffer
  linux: Decorate __libc_fatal error buffer
  elf: Add glibc.mem.decorate_maps tunable

 NEWS                                 |   5 +
 assert/assert.c                      |   2 +
 elf/Makefile                         |   7 +
 elf/dl-minimal-malloc.c              |   2 +
 elf/dl-tunables.list                 |   5 +
 elf/tst-decorate-maps.c              | 197 +++++++++++++++++++++++++++
 include/sys/prctl.h                  |   5 +
 malloc/arena.c                       |   4 +
 malloc/malloc.c                      |   5 +
 manual/tunables.texi                 |  12 ++
 nptl/Makefile                        |   4 +
 nptl/allocatestack.c                 |  40 ++++++
 nptl/pthread_create.c                |   6 +
 support/Makefile                     |   1 +
 support/support.h                    |   4 +
 support/support_set_vma_name.c       |  49 +++++++
 sysdeps/generic/setvmaname.h         |  27 ++++
 sysdeps/posix/libc_fatal.c           |   3 +
 sysdeps/unix/sysv/linux/Makefile     |   1 +
 sysdeps/unix/sysv/linux/setvmaname.c |  48 +++++++
 sysdeps/unix/sysv/linux/setvmaname.h |  36 +++++
 21 files changed, 463 insertions(+)
 create mode 100644 elf/tst-decorate-maps.c
 create mode 100644 support/support_set_vma_name.c
 create mode 100644 sysdeps/generic/setvmaname.h
 create mode 100644 sysdeps/unix/sysv/linux/setvmaname.c
 create mode 100644 sysdeps/unix/sysv/linux/setvmaname.h