Paul Khuong provided a way to reliably allocate 2MiB aligned pages with mmap, without having do mess around too much to get the alignment: https://twitter.com/pkhuong/status/1462988088070791173.
mmap is a bit safer than aligned_alloc because you don’t need to madvise first, which is not guarantee to work, since aligned_alloc is not guarantee to allocate the page before you get to madvise. See https://twitter.com/trav_downs/status/1462929358155223043, from the same thread.
Once upon a time we had to disable transparent huge pages to prevent sporadic latency spikes. We believed the THP system was holding locks on the memory map for unacceptably long periods—or something like that. We never tracked down exactly what was happening. Does that still happen or has THP been fixed/improved?
Fascinating! What’s the use-case for this though?
Huge pages put less pressure on the CPU’s TLB when working with large datasets. (Specifically, one huge page takes a single TLB entry, while the equivalent normal pages take 512 TLB entries)
I always wanted to get 1GB huge pages working when I worked at a database company, but never found the time. I believe CPUs have 4 TLB entries for 1GB pages, equivalent to 1,048,576 4KB TLB entires.