[PATCH] malloc: Support THP in arenas

Wilco Dijkstra Wilco.Dijkstra@arm.com
Wed Jul 30 15:09:37 GMT 2025


Arenas support huge pages but not transparent huge pages.  Add this by
also checking mp_.thp_pagesize when creating a new arena, and use madvise.

Passes regress, OK for commit?

---

diff --git a/malloc/arena.c b/malloc/arena.c
index 90c526f23ba1b2d83a3479245179d1d93b09302d..91a4f36232094ec4f842c7cb661d7b61347320e2 100644
--- a/malloc/arena.c
+++ b/malloc/arena.c
@@ -444,10 +444,13 @@ alloc_new_heap  (size_t size, size_t top_pad, size_t pagesize,
 static heap_info *
 new_heap (size_t size, size_t top_pad)
 {
-  if (mp_.hp_pagesize != 0 && mp_.hp_pagesize <= heap_max_size ())
+  bool use_hugepage = mp_.hp_pagesize != 0;
+  size_t pagesize = use_hugepage ? mp_.hp_pagesize : mp_.thp_pagesize;
+
+  if (pagesize != 0 && pagesize <= heap_max_size ())
     {
-      heap_info *h = alloc_new_heap (size, top_pad, mp_.hp_pagesize,
-				     mp_.hp_flags);
+      heap_info *h = alloc_new_heap (size, top_pad, pagesize,
+				     use_hugepage ? mp_.hp_flags : 0);
       if (h != NULL)
 	return h;
     }
@@ -479,6 +482,8 @@ grow_heap (heap_info *h, long diff)
       h->mprotect_size = new_size;
     }
 
+  madvise_thp (h, new_size);
+
   h->size = new_size;
   LIBC_PROBE (memory_heap_more, 2, h, h->size);
   return 0;




More information about the Libc-alpha mailing list