]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
malloc: Do not call madvise if heap's oldsize >= THP size
authorDev Jain <dev.jain@arm.com>
Fri, 17 Oct 2025 14:18:43 +0000 (19:48 +0530)
committerAdhemerval Zanella <adhemerval.zanella@linaro.org>
Mon, 20 Oct 2025 14:33:54 +0000 (11:33 -0300)
Linux handles virtual memory in Virtual Memory Areas (VMAs). The
madvise(MADV_HUGEPAGE) call works on a VMA granularity, which sets the
VM_HUGEPAGE flag on the VMA. This flag is invariant of the mprotect()
syscall which is used in growing the secondary heaps. Therefore, we
need to call madvise() only when we are sure that VM_HUGEPAGE was not
previously set, which is only in the case when h->size < mp_.thp_pagesize.

Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
malloc/arena.c

index 4cd79d7244bf62d2a97af2b09241a7d23e73a82a..2551cb77494adced238258cebd4ba55f90defeaa 100644 (file)
@@ -482,7 +482,10 @@ grow_heap (heap_info *h, long diff)
       h->mprotect_size = new_size;
     }
 
-  madvise_thp (h, new_size);
+  /* mprotect preserves MADV_HUGEPAGE semantics - this means that if the old
+     region was marked with MADV_HUGEPAGE, the new region will retain that.  */
+  if (h->size < mp_.thp_pagesize)
+    madvise_thp (h, new_size);
 
   h->size = new_size;
   LIBC_PROBE (memory_heap_more, 2, h, h->size);