Skip to content

[Support] Make dontNeedIfMmap actually release pages on Linux - #240071

Open
MaskRay wants to merge 1 commit into
llvm:mainfrom
MaskRay:pr/support-dontneed-linux
Open

MaskRay wants to merge 1 commit into
llvm:mainfrom
MaskRay:pr/support-dontneed-linux

Conversation

@MaskRay

@MaskRay MaskRay commented Nov 7, 2026

Copy link
Copy Markdown
Member

https://reviews.llvm.org/D116366 added dontNeedIfMmap for lld/ELF LTO
memory saving (used on read-only maps) https://reviews.llvm.org/D116367.
D119856 (b66b324) switched to posix_madvise to fix the Illumos
build, which made dontNeedIfMmap a no-op on Linux, both glibc and
musl.

(glibc and musl return 0 for POSIX_MADV_DONTNEED without calling
madvise, because POSIX requires posix_madvise not to change the
semantics of memory access, while Linux's MADV_DONTNEED discards data
(which is acceptable for the read-only map use case).

Other systems treat MADV_DONTNEED as a hint. On macOS 26, a 512 MiB
read-only file mapping stays resident after madvise or posix_madvise;
only the next access is slower.

Call madvise on Linux only, restoring the RSS reduction. Make
dontNeedIfMmap a no-op elsewhere.

https://reviews.llvm.org/D116366 added `dontNeedIfMmap` for lld/ELF LTO
memory saving (used on read-only maps) https://reviews.llvm.org/D116367.
D119856 (b66b324) switched to posix_madvise to fix the Illumos
build, which made `dontNeedIfMmap` a no-op on Linux, both glibc and
musl.

(glibc and musl return 0 for POSIX_MADV_DONTNEED without calling
`madvise`, because POSIX requires posix_madvise not to change the
semantics of memory access, while Linux's MADV_DONTNEED discards data
(which is acceptable for the read-only map use case).

Other systems treat MADV_DONTNEED as a hint. On macOS 26, a 512 MiB
read-only file mapping stays resident after madvise or posix_madvise;
only the next access is slower.

Call madvise on Linux only, restoring the RSS reduction. Make
dontNeedIfMmap a no-op elsewhere.
@llvmorg-github-actions

Copy link
Copy Markdown

@llvm/pr-subscribers-llvm-support

Author: Fangrui Song (MaskRay)

Changes

https://reviews.llvm.org/D116366 added dontNeedIfMmap for lld/ELF LTO
memory saving (used on read-only maps) https://reviews.llvm.org/D116367.
D119856 (b66b324) switched to posix_madvise to fix the Illumos
build, which made dontNeedIfMmap a no-op on Linux, both glibc and
musl.

(glibc and musl return 0 for POSIX_MADV_DONTNEED without calling
madvise, because POSIX requires posix_madvise not to change the
semantics of memory access, while Linux's MADV_DONTNEED discards data
(which is acceptable for the read-only map use case).

Other systems treat MADV_DONTNEED as a hint. On macOS 26, a 512 MiB
read-only file mapping stays resident after madvise or posix_madvise;
only the next access is slower.

Call madvise on Linux only, restoring the RSS reduction. Make
dontNeedIfMmap a no-op elsewhere.


Full diff: https://github.com/llvm/llvm-project/pull/240071.diff

2 Files Affected:

  • (modified) llvm/include/llvm/Support/MemoryBuffer.h (+1-1)
  • (modified) llvm/lib/Support/Unix/Path.inc (+4-5)
diff --git a/llvm/include/llvm/Support/MemoryBuffer.h b/llvm/include/llvm/Support/MemoryBuffer.h
index ea911681bd7760..2811eed86b7c01 100644
--- a/llvm/include/llvm/Support/MemoryBuffer.h
+++ b/llvm/include/llvm/Support/MemoryBuffer.h
@@ -69,7 +69,7 @@ class LLVM_ABI MemoryBuffer {
   /// For read-only MemoryBuffer_MMap, mark the buffer as unused in the near
   /// future and the kernel can free resources associated with it. Further
   /// access is supported but may be expensive. This calls
-  /// madvise(MADV_DONTNEED) on read-only file mappings on *NIX systems. This
+  /// madvise(MADV_DONTNEED) on read-only file mappings on Linux. This
   /// function should not be called on a writable buffer.
   virtual void dontNeedIfMmap() {}
 
diff --git a/llvm/lib/Support/Unix/Path.inc b/llvm/lib/Support/Unix/Path.inc
index 202f7338ca6fa5..5fee0a494f6fd3 100644
--- a/llvm/lib/Support/Unix/Path.inc
+++ b/llvm/lib/Support/Unix/Path.inc
@@ -924,11 +924,10 @@ void mapped_file_region::dontNeedImpl() {
   assert(Mode == mapped_file_region::readonly);
   if (!Mapping)
     return;
-#if defined(__MVS__) || defined(_AIX)
-    // If we don't have madvise, or it isn't beneficial, treat this as a no-op.
-#elif defined(POSIX_MADV_DONTNEED)
-  ::posix_madvise(Mapping, Size, POSIX_MADV_DONTNEED);
-#else
+#ifdef __linux__
+  // Linux's MADV_DONTNEED unmaps the pages and reduces RSS. Other systems
+  // treat it as a hint (macOS keeps the pages resident), and glibc and musl's
+  // posix_madvise ignore POSIX_MADV_DONTNEED.
   ::madvise(Mapping, Size, MADV_DONTNEED);
 #endif
 }

This branch has not been deployed

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant