Repository navigation
Description
Hi, I noticed that in my app some OS allocations sometimes fail to be mapped to 2MiB large pages and get mapped to 4KiB pages instead.
I've only been able to reproduce this with OS allocation sizes strictly larger than 1GiB (which by default start after first 8 1GiB allocations), and only in multi-threaded applications.
Here's a repro, I've checked it on 1.9.7 and on 1.9.10:
#define _GNU_SOURCE
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
enum {
BLOCK = 1ull << 20,
BLOCKS = 20 * 1024,
THREADS = 4,
BLOCKS_PER_THREAD = BLOCKS / THREADS,
};
static void* ptrs[THREADS][BLOCKS_PER_THREAD];
static void* worker(void* arg) {
long t = (long)arg;
for (int i = 0; i < BLOCKS_PER_THREAD; ++i) {
ptrs[t][i] = malloc(BLOCK);
if (!ptrs[t][i]) {
perror("malloc");
return (void*)1;
}
memset(ptrs[t][i], 0xab, BLOCK);
}
return NULL;
}
int main(void) {
pthread_t threads[THREADS];
for (long t = 0; t < THREADS; ++t) {
if (pthread_create(&threads[t], NULL, worker, (void*)t) != 0) {
perror("pthread_create");
return 1;
}
}
for (int t = 0; t < THREADS; ++t) {
void* res = NULL;
pthread_join(threads[t], &res);
if (res) return 1;
}
return 0;
}
If run like this on Linux 5.15 (this requires 20GiB in 2MiB hugepages to maximize chance of reproduction, but this requirement could probably be reduced):
$ gcc -O2 repro.c path/to/libmimalloc.a -pthread -o repro
$ echo 10240 | sudo tee /sys/kernel/mm/hugepages/hugepages-2048kB/nr_hugepages
$ MIMALLOC_ALLOW_LARGE_OS_PAGES=1 MIMALLOC_ARENA_RESERVE=2G MIMALLOC_VERBOSE=1 ./repro
In environment with enough hugepages I would expect all the allocations to be logged with (in large pages) suffix.
However, some allocations don't have this suffix and indeed kernel reports a few gigabytes of memory not in hugepages.
Before log about each allocation that is not in large pages there's a log of this form:
mimalloc: warning: thread 0x7FFC377E7700: unable to allocate aligned OS memory directly, fall back to over-allocation (size: 0x80000000 bytes, address: 0x7FFA47600000, alignment: 0x400000, commit: 1)
I believe what is happening in my case is that:
- For sizes bigger than 1GiB we don't pass aligned address hint to the OS.
- At the same time, we require 4MiB alignment for our segments, so arenas use 4MiB alignment for growth.
- When we mmap 2MiB hugepages, Linux can return an address that is aligned to 2MiB but not to 4MiB, especially without address hint.
- In this case on Linux we go the
overallocate...route and allocate alloc_size + alignment bytes, which would usually still be divisible by 2MiB which is the requirement for 2MiB hugepages. However, in this fallback branch we always passallow_largeas false. - I believe in 1.9.10 we could just pass
allow_large && _mi_os_canuse_large_page(size, alignment)instead offalsein this branch, asmi_os_prim_allocseems to bump alignment from 1 to OS page size if needed. - I'm not sure why I can't reproduce this with a single-threaded program.
Activity
eyupcanakman commentedon May 10, 2026
Daan (@daanx) happy to send a small PR for this if you haven't picked it up yet. Both branches in
mi_os_prim_alloc_aligned(src/os.c:286 and :306) hard-codeallow_large=false.daanx commentedon Jul 14, 2026
Ah strange. It looks like the
alignmentwill be set correctly to 4MiB and thus at some point inunix_mmap_prim_alignedwe will get an aligned hint address. That it still failed can just happen on Linux (either another address or unable to fragment or something). In the overallocation path though we cannot really allow large pages though as then we cannot free the range partially (as large pages are pinned in memory).A better solution might be to try v3 as that does not need 4MiB alignment anymore?
moskupols commentedon Jul 14, 2026
As far as I can see
unix_mmap_prim_alignedcalls_mi_os_get_aligned_hintto get aligned hint for mmap, and that function has this case on linesrc/os.c:136:I also tried linking original example with mimalloc 1.9.10 built with this line added to
unix_mmap_prim_alignedafter the call to_mi_os_get_aligned_hint:It prints this while size is 1 GiB:
and this for sizes above 1 GiB:
As for the allocation path and partial free issue, I'm not sure I follow. In my understanding hugepages are pinned in memory in the sense that they cannot be swapped out of physical memory once faulted until the page is unmapped. But the mapping is separate per OS page, so we can change it with OS page granularity. To check my understanding that we can in fact unmap pages individually after mapping them all in one mmap, I tried this program on linux 5.15:
It prints this, which confirms my undestanding I think:
daanx commentedon Jul 14, 2026
Thanks -- I forgot about the size limit in the aligned hints. This is actually 16 GiB in v3 and I think we can raise it to 16 GiB as well in v1/v2. Maybe even 64 GiB still gives a lot of randomness. Maybe we should not even do this unless MI_SECURE is defined. I'll fix this.
Ah, yes, you can unmap full huge pages -- but not partial ones. So it is risky for the fallback path to over-allocate and free parts as we cannot guarantee the returned pointer is actually huge page aligned. Well, now that I wrote that, I think it will (1) either be huge pages and huge page aligned (and if our required alignment is a multiple of huge page alignment we should be good), or (2) not huge page aligned but regular pages so we should be good at well. It still feels a bit risky though...
limit size for hint address only in secure mode, issue #1290
os: no address hint for OS allocations above 1 GiB
Kiptoo-Deus commentedon Oct 6, 2026
This looks resolved by 82c1688 ("limit size for hint address only in secure mode, issue #1290"): in
devanddev2,_mi_os_get_aligned_hintnow only applies the 1 GiB limit whenMI_SECURE>0, anddev3already allows up to 32 GiB. So allocations over 1 GiB get an aligned hint again and shouldn't hit the over-allocation fallback (which has to dropallow_large, since it unmaps around the aligned part) except when the hint itself fails. Maybe this can be closed?daanx commentedon Oct 6, 2026
Great! Good to hear it works now.