From 22664f33b7389f1b3df409a831c83213cfbbe6d3 Mon Sep 17 00:00:00 2001 From: Cherry Mui Date: Mon, 21 Oct 2024 17:45:01 -0400 Subject: runtime: reserve fewer memory for aligned reservation on sbrk systems Sometimes the runtime needs to reserve some memory with a large alignment, which the OS usually won't directly satisfy. So, it asks size+align bytes instead, and frees the unaligned portions. On sbrk systems, this doesn't work that well, as freeing the tail portion doesn't really free the memory to the OS. Instead, we could simply round the current break up, then reserve the given size, without wasting the tail portion. Also, don't create heap arena hints on sbrk systems. We can only grow the break sequentially, and reserving specific addresses would not succeed anyway. For #69018. Change-Id: Iadc2c54d62b00ad7befa5bbf71146523483a8c47 Reviewed-on: https://go-review.googlesource.com/c/go/+/621715 Reviewed-by: Michael Knyszek LUCI-TryBot-Result: Go LUCI Reviewed-by: Michael Pratt --- src/runtime/malloc.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) (limited to 'src/runtime/malloc.go') diff --git a/src/runtime/malloc.go b/src/runtime/malloc.go index 74decd54c4..0700d0d1cd 100644 --- a/src/runtime/malloc.go +++ b/src/runtime/malloc.go @@ -470,7 +470,10 @@ func mallocinit() { lockInit(&globalAlloc.mutex, lockRankGlobalAlloc) // Create initial arena growth hints. - if goarch.PtrSize == 8 { + if isSbrkPlatform { + // Don't generate hints on sbrk platforms. We can + // only grow the break sequentially. + } else if goarch.PtrSize == 8 { // On a 64-bit machine, we pick the following hints // because: // @@ -828,6 +831,12 @@ mapped: // aligned to align bytes. It may reserve either n or n+align bytes, // so it returns the size that was reserved. func sysReserveAligned(v unsafe.Pointer, size, align uintptr) (unsafe.Pointer, uintptr) { + if isSbrkPlatform { + if v != nil { + throw("unexpected heap arena hint on sbrk platform") + } + return sysReserveAlignedSbrk(size, align) + } // Since the alignment is rather large in uses of this // function, we're not likely to get it by chance, so we ask // for a larger region and remove the parts we don't need. -- cgit v1.3-5-g45d5