mbox series

[v5,0/7] malloc: Improve Huge Page support

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

Message

Adhemerval Zanella Dec. 14, 2021, 6: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.

  - >2: 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.

---
v5: Rebased on current master, fixed some typos and comment, and changed
    the return type of __malloc_default_thp_pagesize (to match the
    kernel).
v4: Fixed the area shrink logic, removed malloc/tst-free-errno* from
    hugetlb2 tests set, added huge page support on main arena.
v3: Only use one tunable, 'glibc.malloc.hugetlb', disabled madvise if THP
    is set to always, added support to arenas.
v2: Renamed thp_pagesize to thp_madvise and make it a boolean state,
    added MAP_HUGETLB support for mmap, removed system specific hooks for
    THP huge page size in favor of Linux generic implementation, initial
    program segments need to be page aligned for the first madvise call.

Adhemerval Zanella (7):
  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
  malloc: Move MORECORE fallback mmap to sysmalloc_mmap_fallback
  malloc: Enable huge page support on main arena

 NEWS                                       |   7 +
 Rules                                      |  36 +++
 elf/dl-tunables.list                       |   4 +
 elf/tst-rtld-list-tunables.exp             |   1 +
 include/libc-pointer-arith.h               |   8 +
 malloc/Makefile                            |  23 ++
 malloc/arena.c                             | 143 ++++++---
 malloc/malloc-internal.h                   |   1 +
 malloc/malloc.c                            | 354 ++++++++++++++-------
 malloc/morecore.c                          |   4 -
 manual/tunables.texi                       |  17 +
 sysdeps/generic/Makefile                   |   8 +
 sysdeps/generic/malloc-hugepages.c         |  39 +++
 sysdeps/generic/malloc-hugepages.h         |  44 +++
 sysdeps/unix/sysv/linux/malloc-hugepages.c | 201 ++++++++++++
 15 files changed, 734 insertions(+), 156 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