| 1 |
// Copyright 2016 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
// The gccgo version of mem_*.go. |
| 6 |
|
| 7 |
package runtime |
| 8 |
|
| 9 |
import ( |
| 10 |
"unsafe" |
| 11 |
) |
| 12 |
|
| 13 |
// Functions called by C code. |
| 14 |
//go:linkname sysAlloc |
| 15 |
//go:linkname sysFree |
| 16 |
|
| 17 |
//extern __go_mmap |
| 18 |
func sysMmap(addr unsafe.Pointer, n uintptr, prot, flags, fd int32, off uintptr) unsafe.Pointer |
| 19 |
|
| 20 |
//extern munmap |
| 21 |
func munmap(addr unsafe.Pointer, length uintptr) int32 |
| 22 |
|
| 23 |
//extern madvise |
| 24 |
func madvise(addr unsafe.Pointer, n uintptr, flags int32) int32 |
| 25 |
|
| 26 |
var mmapFD = int32(-1) |
| 27 |
|
| 28 |
var devZero = []byte("/dev/zero\x00") |
| 29 |
|
| 30 |
func init() { |
| 31 |
if _MAP_ANON == 0 { |
| 32 |
mmapFD = open(&devZero[0], 0 /* O_RDONLY */, 0) |
| 33 |
if mmapFD < 0 { |
| 34 |
println("open /dev/zero: errno=", errno()) |
| 35 |
exit(2) |
| 36 |
} |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
func mmap(addr unsafe.Pointer, n uintptr, prot, flags, fd int32, off uintptr) (unsafe.Pointer, int) { |
| 41 |
p := sysMmap(addr, n, prot, flags, fd, off) |
| 42 |
if uintptr(p) == _MAP_FAILED { |
| 43 |
return nil, errno() |
| 44 |
} |
| 45 |
return p, 0 |
| 46 |
} |
| 47 |
|
| 48 |
// Don't split the stack as this method may be invoked without a valid G, which |
| 49 |
// prevents us from allocating more stack. |
| 50 |
// |
| 51 |
//go:nosplit |
| 52 |
func sysAlloc(n uintptr, sysStat *sysMemStat) unsafe.Pointer { |
| 53 |
p, err := mmap(nil, n, _PROT_READ|_PROT_WRITE, _MAP_ANON|_MAP_PRIVATE, mmapFD, 0) |
| 54 |
if err != 0 { |
| 55 |
if err == _EACCES { |
| 56 |
print("runtime: mmap: access denied\n") |
| 57 |
exit(2) |
| 58 |
} |
| 59 |
if err == _EAGAIN { |
| 60 |
print("runtime: mmap: too much locked memory (check 'ulimit -l').\n") |
| 61 |
exit(2) |
| 62 |
} |
| 63 |
return nil |
| 64 |
} |
| 65 |
sysStat.add(int64(n)) |
| 66 |
return p |
| 67 |
} |
| 68 |
|
| 69 |
func sysUnused(v unsafe.Pointer, n uintptr) { |
| 70 |
// By default, Linux's "transparent huge page" support will |
| 71 |
// merge pages into a huge page if there's even a single |
| 72 |
// present regular page, undoing the effects of the DONTNEED |
| 73 |
// below. On amd64, that means khugepaged can turn a single |
| 74 |
// 4KB page to 2MB, bloating the process's RSS by as much as |
| 75 |
// 512X. (See issue #8832 and Linux kernel bug |
| 76 |
// https://bugzilla.kernel.org/show_bug.cgi?id=93111) |
| 77 |
// |
| 78 |
// To work around this, we explicitly disable transparent huge |
| 79 |
// pages when we release pages of the heap. However, we have |
| 80 |
// to do this carefully because changing this flag tends to |
| 81 |
// split the VMA (memory mapping) containing v in to three |
| 82 |
// VMAs in order to track the different values of the |
| 83 |
// MADV_NOHUGEPAGE flag in the different regions. There's a |
| 84 |
// default limit of 65530 VMAs per address space (sysctl |
| 85 |
// vm.max_map_count), so we must be careful not to create too |
| 86 |
// many VMAs (see issue #12233). |
| 87 |
// |
| 88 |
// Since huge pages are huge, there's little use in adjusting |
| 89 |
// the MADV_NOHUGEPAGE flag on a fine granularity, so we avoid |
| 90 |
// exploding the number of VMAs by only adjusting the |
| 91 |
// MADV_NOHUGEPAGE flag on a large granularity. This still |
| 92 |
// gets most of the benefit of huge pages while keeping the |
| 93 |
// number of VMAs under control. With hugePageSize = 2MB, even |
| 94 |
// a pessimal heap can reach 128GB before running out of VMAs. |
| 95 |
if physHugePageSize != 0 && _MADV_NOHUGEPAGE != 0 { |
| 96 |
// If it's a large allocation, we want to leave huge |
| 97 |
// pages enabled. Hence, we only adjust the huge page |
| 98 |
// flag on the huge pages containing v and v+n-1, and |
| 99 |
// only if those aren't aligned. |
| 100 |
var head, tail uintptr |
| 101 |
if uintptr(v)%physHugePageSize != 0 { |
| 102 |
// Compute huge page containing v. |
| 103 |
head = uintptr(v) &^ (physHugePageSize - 1) |
| 104 |
} |
| 105 |
if (uintptr(v)+n)%physHugePageSize != 0 { |
| 106 |
// Compute huge page containing v+n-1. |
| 107 |
tail = (uintptr(v) + n - 1) &^ (physHugePageSize - 1) |
| 108 |
} |
| 109 |
|
| 110 |
// Note that madvise will return EINVAL if the flag is |
| 111 |
// already set, which is quite likely. We ignore |
| 112 |
// errors. |
| 113 |
if head != 0 && head+physHugePageSize == tail { |
| 114 |
// head and tail are different but adjacent, |
| 115 |
// so do this in one call. |
| 116 |
madvise(unsafe.Pointer(head), 2*physHugePageSize, _MADV_NOHUGEPAGE) |
| 117 |
} else { |
| 118 |
// Advise the huge pages containing v and v+n-1. |
| 119 |
if head != 0 { |
| 120 |
madvise(unsafe.Pointer(head), physHugePageSize, _MADV_NOHUGEPAGE) |
| 121 |
} |
| 122 |
if tail != 0 && tail != head { |
| 123 |
madvise(unsafe.Pointer(tail), physHugePageSize, _MADV_NOHUGEPAGE) |
| 124 |
} |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
if uintptr(v)&(physPageSize-1) != 0 || n&(physPageSize-1) != 0 { |
| 129 |
// madvise will round this to any physical page |
| 130 |
// *covered* by this range, so an unaligned madvise |
| 131 |
// will release more memory than intended. |
| 132 |
throw("unaligned sysUnused") |
| 133 |
} |
| 134 |
|
| 135 |
if _MADV_DONTNEED != 0 { |
| 136 |
madvise(v, n, _MADV_DONTNEED) |
| 137 |
} else if _MADV_FREE != 0 { |
| 138 |
madvise(v, n, _MADV_FREE) |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
func sysUsed(v unsafe.Pointer, n uintptr) { |
| 143 |
// Partially undo the NOHUGEPAGE marks from sysUnused |
| 144 |
// for whole huge pages between v and v+n. This may |
| 145 |
// leave huge pages off at the end points v and v+n |
| 146 |
// even though allocations may cover these entire huge |
| 147 |
// pages. We could detect this and undo NOHUGEPAGE on |
| 148 |
// the end points as well, but it's probably not worth |
| 149 |
// the cost because when neighboring allocations are |
| 150 |
// freed sysUnused will just set NOHUGEPAGE again. |
| 151 |
sysHugePage(v, n) |
| 152 |
} |
| 153 |
|
| 154 |
func sysHugePage(v unsafe.Pointer, n uintptr) { |
| 155 |
if physHugePageSize != 0 && _MADV_HUGEPAGE != 0 { |
| 156 |
// Round v up to a huge page boundary. |
| 157 |
beg := (uintptr(v) + (physHugePageSize - 1)) &^ (physHugePageSize - 1) |
| 158 |
// Round v+n down to a huge page boundary. |
| 159 |
end := (uintptr(v) + n) &^ (physHugePageSize - 1) |
| 160 |
|
| 161 |
if beg < end { |
| 162 |
madvise(unsafe.Pointer(beg), end-beg, _MADV_HUGEPAGE) |
| 163 |
} |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
// Don't split the stack as this function may be invoked without a valid G, |
| 168 |
// which prevents us from allocating more stack. |
| 169 |
// |
| 170 |
//go:nosplit |
| 171 |
func sysFree(v unsafe.Pointer, n uintptr, sysStat *sysMemStat) { |
| 172 |
sysStat.add(-int64(n)) |
| 173 |
munmap(v, n) |
| 174 |
} |
| 175 |
|
| 176 |
func sysFault(v unsafe.Pointer, n uintptr) { |
| 177 |
mmap(v, n, _PROT_NONE, _MAP_ANON|_MAP_PRIVATE|_MAP_FIXED, mmapFD, 0) |
| 178 |
} |
| 179 |
|
| 180 |
func sysReserve(v unsafe.Pointer, n uintptr) unsafe.Pointer { |
| 181 |
p, err := mmap(v, n, _PROT_NONE, _MAP_ANON|_MAP_PRIVATE, mmapFD, 0) |
| 182 |
if err != 0 { |
| 183 |
return nil |
| 184 |
} |
| 185 |
return p |
| 186 |
} |
| 187 |
|
| 188 |
func sysMap(v unsafe.Pointer, n uintptr, sysStat *sysMemStat) { |
| 189 |
sysStat.add(int64(n)) |
| 190 |
|
| 191 |
if GOOS == "aix" { |
| 192 |
// AIX does not allow mapping a range that is already mapped. |
| 193 |
// So always unmap first even if it is already unmapped. |
| 194 |
munmap(v, n) |
| 195 |
} |
| 196 |
p, err := mmap(v, n, _PROT_READ|_PROT_WRITE, _MAP_ANON|_MAP_FIXED|_MAP_PRIVATE, mmapFD, 0) |
| 197 |
if err == _ENOMEM { |
| 198 |
throw("runtime: out of memory") |
| 199 |
} |
| 200 |
if p != v || err != 0 { |
| 201 |
throw("runtime: cannot map pages in arena address space") |
| 202 |
} |
| 203 |
} |