
Ben Gamari pushed to branch wip/T26151 at Glasgow Haskell Compiler / GHC Commits: 1028f02f by Ben Gamari at 2025-07-01T12:47:27-04:00 rts/posix: Hold on to low reservations when reserving heap Previously when the OS gave us an address space reservation in low memory we would immediately release it and try again. However, on some platforms this meant that we would get the same allocation again in the next iteration (since mmap's `hint` argument is just that, a hint). Instead we now hold on to low reservations until we have found a suitable heap reservation. Fixes #26151. - - - - - 1 changed file: - rts/posix/OSMem.c Changes: ===================================== rts/posix/OSMem.c ===================================== @@ -585,6 +585,10 @@ void *osReserveHeapMemory(void *startAddressPtr, W_ *len) } #endif + const int MAX_ATTEMPTS = 8; + void *bad_allocs[MAX_ATTEMPTS] = {}; + size_t bad_alloc_lens[MAX_ATTEMPTS] = {}; + attempt = 0; while (attempt < MAX_ATTEMPTS) { *len &= ~MBLOCK_MASK; @@ -611,18 +615,31 @@ void *osReserveHeapMemory(void *startAddressPtr, W_ *len) } else if ((W_)at >= minimumAddress) { // Success! We were given a block of memory starting above the 8 GB // mark, which is what we were looking for. - break; } else { // We got addressing space but it wasn't above the 8GB mark. - // Try again. - if (munmap(at, *len) < 0) { - sysErrorBelch("unable to release reserved heap"); + // Free any portion *above* 8GB and hang on to the rest to increase + // the likelihood that we get a suitable allocation next iteration. + uintptr_t end = (W_) at + *len; + bad_allocs[attempt] = at; + if (end > minimumAddress) { + if (munmap((void *) minimumAddress, end - minimumAddress) < 0) { + sysErrorBelch("unable to release high portion of low memory reservation"); + } + bad_alloc_lens[attempt] = minimumAddress - (W_) at; + } else { + bad_alloc_lens[attempt] = *len; } } attempt++; } + for (int i=0; i < MAX_ATTEMPTS; i++) { + if (bad_allocs[i] != NULL && munmap(bad_allocs[i], bad_alloc_lens[i]) < 0) { + sysErrorBelch("unable to release reserved heap"); + } + } + if (at == NULL) { sysErrorBelch("failed to reserve heap memory"); } View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/1028f02fb6bff3af71cb7863f93bb3d2... -- View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/1028f02fb6bff3af71cb7863f93bb3d2... You're receiving this email because of your account on gitlab.haskell.org.