Skip to content

When mi_os_prim_alloc_aligned falls back on overallocation, it drops allow_large #1290

Closed

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:

  1. For sizes bigger than 1GiB we don't pass aligned address hint to the OS.
  2. At the same time, we require 4MiB alignment for our segments, so arenas use 4MiB alignment for growth.
  3. When we mmap 2MiB hugepages, Linux can return an address that is aligned to 2MiB but not to 4MiB, especially without address hint.
  4. 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 pass allow_large as false.
  5. I believe in 1.9.10 we could just pass allow_large && _mi_os_canuse_large_page(size, alignment) instead of false in this branch, as mi_os_prim_alloc seems to bump alignment from 1 to OS page size if needed.
  6. I'm not sure why I can't reproduce this with a single-threaded program.

Activity

  1. eyupcanakman commented on May 10, 2026

    @eyupcanakman

    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-code allow_large=false.

  2. daanx commented on Jul 14, 2026

    @daanx
    Collaborator

    Ah strange. It looks like the alignment will be set correctly to 4MiB and thus at some point in unix_mmap_prim_aligned we 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?

  3. moskupols commented on Jul 14, 2026

    @moskupols
    Author

    As far as I can see unix_mmap_prim_aligned calls _mi_os_get_aligned_hint to get aligned hint for mmap, and that function has this case on line src/os.c:136:

      if (size > 1*MI_GiB) return NULL;  // guarantee the chance of fixed valid address is at most 1/(MI_HINT_AREA / 1<<30) = 1/4096.
    

    I also tried linking original example with mimalloc 1.9.10 built with this line added to unix_mmap_prim_aligned after the call to _mi_os_get_aligned_hint:

        fprintf(stderr, "[mi-hint] mmap request: hint=%p size=0x%zx try_alignment=0x%zx large=%d\n", hint,
                size, try_alignment, (int)((flags & MAP_HUGETLB) != 0));
    

    It prints this while size is 1 GiB:

    [mi-hint] mmap request: hint=0x5b07ec00000 size=0x40000000 try_alignment=0x400000 large=0
    

    and this for sizes above 1 GiB:

    [mi-hint] mmap request: hint=(nil) size=0x80000000 try_alignment=0x400000 large=0
    

    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:

    #define _GNU_SOURCE
    #include <linux/mman.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/mman.h>
    
    #define NPAGES 10
    #define HPSIZE (2UL * 1024 * 1024)
    
    #define SHOW(action)                                    \
      do {                                                  \
        printf("<- %s\n", action);                          \
        fflush(stdout);                                     \
        if (system("grep HugePages_Free /proc/meminfo")) {} \
      } while (0)
    
    int main(void) {
      SHOW("baseline");
    
      void* base = mmap(NULL, NPAGES * HPSIZE, PROT_READ | PROT_WRITE,
                        MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB | MAP_HUGE_2MB, -1, 0);
      if (base == MAP_FAILED) {
        perror("mmap");
        return 1;
      }
      SHOW("mmap 10 x 2MiB as one region (reserved, not yet faulted)");
    
      for (size_t i = 0; i < NPAGES; i++) ((volatile char*)base)[i * HPSIZE] = 1;
      SHOW("faulted in all 10 pages");
    
      for (size_t i = 0; i < NPAGES; i++) {
        int ret = munmap((char*)base + i * HPSIZE, HPSIZE);
        printf("munmap for page %d/%d returned %d ", (int)(i + 1), NPAGES, ret);
        SHOW("");
      }
      return 0;
    }
    

    It prints this, which confirms my undestanding I think:

    <- baseline
    HugePages_Free:      504
    <- mmap 10 x 2MiB as one region (reserved, not yet faulted)
    HugePages_Free:      504
    <- faulted in all 10 pages
    HugePages_Free:      494
    munmap for page 1/10 returned 0 <- 
    HugePages_Free:      495
    munmap for page 2/10 returned 0 <- 
    HugePages_Free:      496
    munmap for page 3/10 returned 0 <- 
    HugePages_Free:      497
    munmap for page 4/10 returned 0 <- 
    HugePages_Free:      498
    munmap for page 5/10 returned 0 <- 
    HugePages_Free:      499
    munmap for page 6/10 returned 0 <- 
    HugePages_Free:      500
    munmap for page 7/10 returned 0 <- 
    HugePages_Free:      501
    munmap for page 8/10 returned 0 <- 
    HugePages_Free:      502
    munmap for page 9/10 returned 0 <- 
    HugePages_Free:      503
    munmap for page 10/10 returned 0 <- 
    HugePages_Free:      504
    
  4. daanx commented on Jul 14, 2026

    @daanx
    Collaborator

    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...

  5. Kiptoo-Deus commented on Oct 6, 2026

    @Kiptoo-Deus

    This looks resolved by 82c1688 ("limit size for hint address only in secure mode, issue #1290"): in dev and dev2, _mi_os_get_aligned_hint now only applies the 1 GiB limit when MI_SECURE>0, and dev3 already 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 drop allow_large, since it unmaps around the aligned part) except when the hint itself fails. Maybe this can be closed?

  6. daanx commented on Oct 6, 2026

    @daanx
    Collaborator

    Great! Good to hear it works now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions