Andreas Klebinger pushed to branch wip/andreask/9.10.4-batch1 at Glasgow Haskell Compiler / GHC Commits: f6a2555b by Ben Gamari at 2026-10-23T09:38:10+02:00 rts/posix: Enforce iteration limit on heap reservation logic Previously we could loop indefinitely when attempting to get an address space reservation for our heap. Limit the logic to 8 iterations to ensure we instead issue a reasonable error message. Addresses #26151. (cherry picked from commit a48db5706800ed2691d219323a6fdb5d7a73c638) - - - - - e81d64e1 by Ben Gamari at 2026-10-23T09:38:25+02: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. (cherry picked from commit 3ab77586e6daae5c7de481d2b9532bf499b587ff) - - - - - 44b8c8f0 by Ben Gamari at 2026-10-23T09:38:37+02:00 rts: Eliminate uses of implicit constant arrays Folding of `const`-sized variable-length arrays to a constant-length array is a gnu extension which clang complains about. Closes #26502. (cherry picked from commit 064107a57b90a2981049e3158b5904f8f3fb506f) - - - - - 2 changed files: - rts/Printer.c - rts/posix/OSMem.c Changes: ===================================== rts/Printer.c ===================================== @@ -994,8 +994,8 @@ findPtr(P_ p, int follow) { uint32_t g, n; bdescr *bd; - const int arr_size = 1024; - StgPtr arr[arr_size]; +#define ARR_SIZE 1024 + StgPtr arr[ARR_SIZE]; int i = 0; searched = 0; @@ -1005,24 +1005,24 @@ findPtr(P_ p, int follow) // just before a block is used. for (n = 0; n < getNumCapabilities(); n++) { bd = nurseries[i].blocks; - i = findPtrBlocks(p,bd,arr,arr_size,i); - if (i >= arr_size) return; + i = findPtrBlocks(p,bd,arr,ARR_SIZE,i); + if (i >= ARR_SIZE) return; } #endif for (g = 0; g < RtsFlags.GcFlags.generations; g++) { bd = generations[g].blocks; - i = findPtrBlocks(p,bd,arr,arr_size,i); + i = findPtrBlocks(p,bd,arr,ARR_SIZE,i); bd = generations[g].large_objects; - i = findPtrBlocks(p,bd,arr,arr_size,i); - if (i >= arr_size) return; + i = findPtrBlocks(p,bd,arr,ARR_SIZE,i); + if (i >= ARR_SIZE) return; for (n = 0; n < getNumCapabilities(); n++) { i = findPtrBlocks(p, gc_threads[n]->gens[g].part_list, - arr, arr_size, i); + arr, ARR_SIZE, i); i = findPtrBlocks(p, gc_threads[n]->gens[g].todo_bd, - arr, arr_size, i); + arr, ARR_SIZE, i); } - if (i >= arr_size) return; + if (i >= ARR_SIZE) return; } if (follow && i == 1) { debugBelch("-->\n"); ===================================== rts/posix/OSMem.c ===================================== @@ -585,8 +585,14 @@ void *osReserveHeapMemory(void *startAddressPtr, W_ *len) } #endif +#define MAX_ATTEMPTS 256 + void *bad_allocs[MAX_ATTEMPTS]; + size_t bad_alloc_lens[MAX_ATTEMPTS]; + memset(bad_allocs, 0, sizeof(void*) * MAX_ATTEMPTS); + memset(bad_alloc_lens, 0, sizeof(size_t) * MAX_ATTEMPTS); + attempt = 0; - while (1) { + while (attempt < MAX_ATTEMPTS) { *len &= ~MBLOCK_MASK; if (*len < MBLOCK_SIZE) { @@ -611,18 +617,35 @@ 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"); + } + return at; } View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/93a33fc3f15c421f248ae36bc3e67bb... -- View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/93a33fc3f15c421f248ae36bc3e67bb... You're receiving this email because of your account on gitlab.haskell.org. Manage all notifications: https://gitlab.haskell.org/-/profile/notifications | Help: https://gitlab.haskell.org/help