From: Lance Yang Date: Tue, 16 May 2023 08:20:42 +0000 (+0000) Subject: runtime: fall back on mmap if madvise is unsupported X-Git-Tag: go1.21rc1~355 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=5ffdc1f15c66f4f31124a43c4bd9de94b9131e15;p=gostls13.git runtime: fall back on mmap if madvise is unsupported Since Linux 3.18, support for madvise is optional, depending on the setting of the CONFIG_ADVISE_SYSCALLS configuration option. The Go runtime currently assumes in several places that we do not unmap heap memory; that needs to remain true. So, if madvise is unsupported, we cannot fall back on munmap. AFAIK, the only way to free the pages is to remap the memory region. For the x86, the system call mmap() is implemented by sys_mmap2() which calls do_mmap2() directly with the same parameters. The main call trace for mmap(v, n, PROT_READ|PROT_WRITE, MAP_ANON|MAP_FIXED|MAP_PRIVATE, -1, 0) is as follows: ``` do_mmap2() \- do_mmap_pgoff() \- get_unmapped_area() \- find_vma_prepare() // If a VMA was found and it is part of the new mmaping, remove // the old mapping as the new one will cover both. // Unmap all the pages in the region to be unmapped. \- do_munmap() // Allocate a VMA from the slab allocator. \- kmem_cache_alloc() // Link in the new vm_area_struct. \- vma_link() ``` So, it's safe to fall back on mmap(). See D.2 https://www.kernel.org/doc/gorman/html/understand/understand021.html Change-Id: Ia2b4234bc0bf8a4631a9926364598854618fe270 GitHub-Last-Rev: 179f04715442b44cd4b7bf3e6cae3dd9092128e7 GitHub-Pull-Request: golang/go#60218 Reviewed-on: https://go-review.googlesource.com/c/go/+/495081 Reviewed-by: David Chase Reviewed-by: Michael Knyszek Run-TryBot: Michael Knyszek TryBot-Result: Gopher Robot --- diff --git a/src/runtime/mem_linux.go b/src/runtime/mem_linux.go index b456f7f2ff..bdfab13fed 100644 --- a/src/runtime/mem_linux.go +++ b/src/runtime/mem_linux.go @@ -36,6 +36,8 @@ func sysAllocOS(n uintptr) unsafe.Pointer { var adviseUnused = uint32(_MADV_FREE) +const madviseUnsupported = 0 + func sysUnusedOS(v unsafe.Pointer, n uintptr) { if uintptr(v)&(physPageSize-1) != 0 || n&(physPageSize-1) != 0 { // madvise will round this to any physical page @@ -44,17 +46,31 @@ func sysUnusedOS(v unsafe.Pointer, n uintptr) { throw("unaligned sysUnused") } - var advise uint32 - if debug.madvdontneed != 0 { + advise := atomic.Load(&adviseUnused) + if debug.madvdontneed != 0 && advise != madviseUnsupported { advise = _MADV_DONTNEED - } else { - advise = atomic.Load(&adviseUnused) } - if errno := madvise(v, n, int32(advise)); advise == _MADV_FREE && errno != 0 { - // MADV_FREE was added in Linux 4.5. Fall back to MADV_DONTNEED if it is - // not supported. + switch advise { + case _MADV_FREE: + if madvise(v, n, _MADV_FREE) == 0 { + break + } atomic.Store(&adviseUnused, _MADV_DONTNEED) - madvise(v, n, _MADV_DONTNEED) + fallthrough + case _MADV_DONTNEED: + // MADV_FREE was added in Linux 4.5. Fall back on MADV_DONTNEED if it's + // not supported. + if madvise(v, n, _MADV_DONTNEED) == 0 { + break + } + atomic.Store(&adviseUnused, madviseUnsupported) + fallthrough + case madviseUnsupported: + // Since Linux 3.18, support for madvise is optional. + // Fall back on mmap if it's not supported. + // _MAP_ANON|_MAP_FIXED|_MAP_PRIVATE will unmap all the + // pages in the old mapping, and remap the memory region. + mmap(v, n, _PROT_READ|_PROT_WRITE, _MAP_ANON|_MAP_FIXED|_MAP_PRIVATE, -1, 0) } if debug.harddecommit > 0 {