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. If this VMA or a portion of it is mremapped
to a different location, Linux will create a new VMA, which will have
the same flags as the old one. This implies that the VM_HUGEPAGE flag
will be retained. Therefore, if we can guarantee that the old VMA was
marked with VM_HUGEPAGE, then there is no need to call madvise_thp() in
mremap_chunk().
The old chunk comes from a heap or non-heap allocation, both of which
have already been enlightened for THP. This implies that, if THP is on,
and the size of the old chunk is greater than or equal to thp_pagesize,
the VMA to which this chunk belongs to, has the VM_HUGEPAGE flag set.
Hence in this case we can avoid invoking the madvise() syscall.
Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
if (cp == MAP_FAILED)
return NULL;
- madvise_thp (cp, new_size);
+ /* mremap preserves the region's flags - this means that if the old chunk
+ was marked with MADV_HUGEPAGE, the new chunk will retain that. */
+ if (total_size < mp_.thp_pagesize)
+ madvise_thp (cp, new_size);
p = mmap_set_chunk ((uintptr_t) cp, new_size, offset, is_hp);