mbox series

[v3,0/5] malloc: Improve Huge Page support

Message ID 20210823215713.3304523-1-adhemerval.zanella@linaro.org
Headers show
Series malloc: Improve Huge Page support | expand


Adhemerval Zanella Aug. 23, 2021, 9:57 p.m. UTC
Linux currently supports two ways to use Huge Pages: either by using
specific flags directly with the syscall (MAP_HUGETLB for mmap(), or
SHM_HUGETLB for shmget()), or by using Transparent Huge Pages (THP)
where the kernel will try to move allocated anonymous pages to Huge
Pages blocks transparent to application.

Also, THP current support three different modes [1]: 'never', 'madvise',
and 'always'.  The 'never' is self-explanatory and 'always' will enable
THP for all anonymous memory.  However, 'madvise' is still the default
for some systems and for such cases THP will be only used if the memory
range is explicity advertise by the program through a
madvise(MADV_HUGEPAGE) call.

This patchset adds a two new tunables to improve malloc() support with
Huge Page, 'glibc.malloc.hugetlb' with the supported values:

  - '0': default value, do not enable any huge page usage.

  - '1': instruct the system allocator to issue a madvise(MADV_HUGEPAGE)
    call after a mmap() one for sizes larger than the default huge page
    size and on sbrk() calls to extend the program data segment.

  - '2':  instruct the system allocator to round allocation to huge page
    sizes along with the required flags (MAP_HUGETLB for Linux).  If the
    memory allocation fails, the default system page size is used instead.

  - positive value: same as '2' but to use a specific huge page size.
    The value is checked against the supported one by the system.

The 'glibc.malloc.hugetlb=2' aims to replace the 'morecore' removed
callback from 2.34 for libhugetlbfs (where the library tries to leverage
the huge pages usage instead to provide a system allocator).  By
implementing the support directly on the mmap() code patch there is
no need to try emulate the morecore()/sbrk() semantic which simplifies
the code and make memory shrink logic more straighforward.

Changes from previous version:

  - Only use one tunable, 'glibc.malloc.hugetlb'.
  - Disable madvise if THP is set to always.
  - Add support to arenas.

Adhemerval Zanella (5):
  malloc: Add madvise support for Transparent Huge Pages
  malloc: Add THP/madvise support for sbrk
  malloc: Move mmap logic to its own function
  malloc: Add Huge Page support for mmap()
  malloc: Add huge page support to arenas

 NEWS                                       |  11 +-
 Rules                                      |  36 +++
 elf/dl-tunables.list                       |   4 +
 elf/tst-rtld-list-tunables.exp             |   1 +
 include/libc-pointer-arith.h               |  10 +
 malloc/Makefile                            |  18 ++
 malloc/arena.c                             | 124 +++++++---
 malloc/malloc-internal.h                   |   1 +
 malloc/malloc.c                            | 268 +++++++++++++++------
 manual/tunables.texi                       |  16 ++
 sysdeps/generic/Makefile                   |   8 +
 sysdeps/generic/malloc-hugepages.c         |  38 +++
 sysdeps/generic/malloc-hugepages.h         |  44 ++++
 sysdeps/unix/sysv/linux/malloc-hugepages.c | 202 ++++++++++++++++
 14 files changed, 660 insertions(+), 121 deletions(-)
 create mode 100644 sysdeps/generic/malloc-hugepages.c
 create mode 100644 sysdeps/generic/malloc-hugepages.h
 create mode 100644 sysdeps/unix/sysv/linux/malloc-hugepages.c