Integrated: JDK-8310233: Fix THP detection on Linux

Thomas Stuefe stuefe at openjdk.org
Mon Jul 17 04:59:14 UTC 2023


On Fri, 30 Jun 2023 16:26:43 GMT, Thomas Stuefe <stuefe at openjdk.org> wrote:

> Today, if we use UseTransparentHugePages, we assume that the *static* hugepage detection we do is valid for THPs:
> - that THPs use the page size (in hotspot used as "default large page size") found in `/proc/memlimit` "Hugepagesize")
> - that THPs are enabled if that page size is >0.
> 
> Both assumptions are incorrect:
> 
> - whether THPs are enabled should be checked at `/sys/kernel/mm/transparent_hugepage/enabled`, which is a tri-state value ("always", "madvise", "never"). THPs are available for the first two states.
> - The page size employed by `khugepaged` is set in `/sys/kernel/mm/transparent_hugepage/hpage_pmd_size`. It can differ from the default page size used for static hugepages. For example, we could configure a system such that it uses 1G static hugepages, but the THP page size would still be 2M.
> 
> ------
> 
> About the patch:
> 
> This is a limited, minimally invasive patch to fix THP detection. The patch aims to be easy to downport. There is more work to do, which I will do in subsequent RFEs.
> 
> Functionally, for *static* (non-THP) pages nothing changes. THP-mode now correctly detects THP support in the OS, and uses the correct page size (see examples below).
> 
> -------------
> 
> Example 1: System has THPs disabled, but static hugepages (1g, 2m) configured:
> 
> 
> thomas at starfish $ cat /sys/kernel/mm/transparent_hugepage/enabled
> always madvise [never]
> thomas at starfish $ cat /proc/meminfo | grep Hugepage
> Hugepagesize:    1048576 kB
> 
> 
> Without patch, we incorrectly assume THPs are enabled, and that THP page size is 1G (!), which we then proceed and use as heap page size, causing the heap size to be rounded up from 512m -> 1G. But - even though it is printed as "1G page backed" in log output - in reality it will still 4K-page-backed: the `madvise(2)` we use to set the THP page size will be ignored by the system, since THPs are disabled.
> 
> 
> thomas at starfish $ ./images/jdk/bin/java -Xmx512m -XX:+UseLargePages -XX:+UseTransparentHugePages -Xlog:pagesize -version
> [0.001s][info][pagesize] Using the default large page size: 1G
> [0.001s][info][pagesize] Usable page sizes: 4k, 2M, 1G
> ...
> [0.016s][info][pagesize] Heap:  min=1G max=1G base=0x00000000c0000000 size=1G page_size=1G
> 
> 
> With patch, we correctly refuse to use large pages (and we log more info):
> 
> 
> thomas at starfish $ ./images/jdk/bin/java -Xmx512m -XX:+UseLargePages -XX:+UseTransparentHugePages -Xlog:pagesize -version
> [0.001s][info][pagesize] Static hugepage support: 2M, 1G (default)
> [0.001s][info][pagesize]   default pagesize: 1G
> [0...

This pull request has now been integrated.

Changeset: 37ca9024
Author:    Thomas Stuefe <stuefe at openjdk.org>
URL:       https://git.openjdk.org/jdk/commit/37ca9024ef59d99cae0bd7e25b2e6d3c1e085f97
Stats:     815 lines in 7 files changed: 684 ins; 99 del; 32 mod

8310233: Fix THP detection on Linux

Reviewed-by: jsjolen, dholmes

-------------

PR: https://git.openjdk.org/jdk/pull/14739


More information about the hotspot-dev mailing list