[PATCH] elf: Align large load segments to PMD huge page size for THP

Wilco Dijkstra Wilco.Dijkstra@arm.com
Tue Feb 24 18:29:19 GMT 2026


Hi Wang,

+#ifdef __LP64__
+	  /* Dynamically calculate the PMD huge page size based on the normal
+	     page size (e.g., 2M for 4K pages, 32M for 16K pages). Aligning
+	     load segments that are large enough to the PMD size helps improve
+	     THP eligibility and reduces TLB pressure.
+	     We cap the huge page size at 32M to avoid over-aligning on systems
+	     with very large normal pages (like 64K pages with 512M huge pages). */
+	  size_t page_size = GLRO(dl_pagesize);
+	  size_t huge_page_size = page_size * (page_size / sizeof (void *));
+	  size_t huge_page_mask = huge_page_size - 1;
+	  if (huge_page_size <= (32 * 1024 * 1024)
+	      && ((c->mapstart | c->mapoff) & huge_page_mask) == 0
+	      && (c->mapend - c->mapstart) >= huge_page_size
+	      && p_align_max < huge_page_size)
+	    p_align_max = huge_page_size;
+#endif

So I think the idea of this is great, but like with malloc, it seems best to make
this opt-in via a header (maybe similar mechanism as malloc-hugepages.h)
and allow different sizes rather than calculate a single huge page size.
For example on AArch64 the best huge page size is 2MB irrespectively of the
base pagesize. On other targets, this calculation might not be valid or there
are no huge pages.

Also I don't see any madvise call in the elf/ directory. Is that needed as well
or is that automatic based on the alignment?

Cheers,
Wilco


More information about the Libc-alpha mailing list