Skip to content

v3: advise MADV_HUGEPAGE independently of allow_large - #1504

Closed
Matthew Charles Vladislav Busel (Mattbusel) wants to merge 1 commit into
microsoft:dev3from
Mattbusel:fix/thp-advise-without-allow-large
Closed

Matthew Charles Vladislav Busel (Mattbusel) wants to merge 1 commit into
microsoft:dev3from
Mattbusel:fix/thp-advise-without-allow-large

Conversation

@Mattbusel

Copy link
Copy Markdown

Fixes #1502.

Port of #1328 to v3. Since the aligned page meta data change (82fd55f, 67b324e), mi_arenas_page_alloc_from_os allocates with allow_large=false because it commits lazily (_mi_prim_alloc asserts commit || !allow_large). unix_mmap only advised MADV_HUGEPAGE when allow_large was set, so on Linux with the default THP setting (madvise) huge objects no longer get transparent huge pages.

This drops allow_large from the THP advise condition in src/prim/unix/prim.c, as #1328 did for v2. allow_large still gates MAP_HUGETLB. MIMALLOC_ALLOW_THP=0 still disables the advise.

Measured

WSL2 Ubuntu 24.04, kernel 6.18, THP [madvise], static Release builds of dev3 (0bba5cc) and this branch. The test does mi_malloc(n), memset over it, then reads the mapping's VmFlags and AnonHugePages from /proc/self/smaps and the minor fault count from getrusage:

object dev3 faults dev3 AnonHugePages this PR faults this PR AnonHugePages
64 MiB 38 69632 kB 38 69632 kB
256 MiB 65542 0 kB (no hg) 644 260096 kB (hg)
1024 MiB 262150 0 kB (no hg) 29134 933888 kB (hg)

The 64 MiB object comes from an arena and was already advised. With MIMALLOC_ALLOW_THP=0 the 256 MiB case is back to 65542 faults and no hg.

ctest passes (16/16) in Release and Debug on the same machine.

Written with help from Claude (Assisted-by trailer on the commit).

Port of microsoft#1328 to v3. The aligned page meta data change made the OS
fallback in mi_arenas_page_alloc_from_os allocate with allow_large=false
(it commits lazily), and unix_mmap only advised MADV_HUGEPAGE when
allow_large was set. With the distro default THP setting (madvise),
huge objects therefore stopped getting transparent huge pages.

allow_large still gates MAP_HUGETLB. The THP advise now depends only on
mi_option_allow_thp and _mi_os_canuse_large_page, as in v2.

Fixes microsoft#1502.

Assisted-by: Claude:claude-opus-5-5
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

@daanx

Copy link
Copy Markdown
Collaborator

Thanks for the PR! However, I already fixed this in

  • e2a1f17 fix THP usage regression in v3
  • aac4f5f Fix THP advice eligibility for OS allocations
    Hope that works for you.

@Mattbusel

Copy link
Copy Markdown
Author

Works for me, thanks Daan. Glad it's fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants