Skip to content

Commit c915215

Browse files
mknyszekgopherbot
authored andcommitted
runtime: add the disablethp GODEBUG setting
Go 1.21.1 and Go 1.22 have ceased working around an issue with Linux kernel defaults for transparent huge pages that can result in excessive memory overheads. (https://bugzilla.kernel.org/show_bug.cgi?id=93111) Many Linux distributions disable huge pages altogether these days, so this problem isn't quite as far-reaching as it used to be. Also, the problem only affects Go programs with very particular memory usage patterns. That being said, because the runtime used to actively deal with this problem (but with some unpredictable behavior), it's preventing users that don't have a lot of control over their execution environment from upgrading to Go beyond Go 1.20. This change adds a GODEBUG to smooth over the transition. The GODEBUG setting disables transparent huge pages for all heap memory on Linux, which is much more predictable than restoring the old behavior. Fixes golang#64332. Change-Id: I73b1894337f0f0b1a5a17b90da1221e118e0b145 Reviewed-on: https://go-review.googlesource.com/c/go/+/547475 Reviewed-by: Michael Pratt <[email protected]> Auto-Submit: Michael Knyszek <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent 3d2645f commit c915215

File tree

4 files changed

+30
-0
lines changed

4 files changed

+30
-0
lines changed

doc/godebug.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,19 @@ when the connection supports neither TLS 1.3 nor Extended Master Secret
165165
(implemented in Go 1.21). It can be reenabled with the [`tlsunsafeekm`
166166
setting](/pkg/crypto/tls/#ConnectionState.ExportKeyingMaterial).
167167

168+
Go 1.22 changed how the runtime interacts with transparent huge pages on Linux.
169+
In particular, a common default Linux kernel configuration can result in
170+
significant memory overheads, and Go 1.22 no longer works around this default.
171+
To work around this issue without adjusting kernel settings, transparent huge
172+
pages can be disabled for Go memory with the
173+
[`disablethp` setting](/pkg/runtime#hdr-Environment_Variable).
174+
This behavior was backported to Go 1.21.1, but the setting is only available
175+
starting with Go 1.21.6.
176+
This setting may be removed in a future release, and users impacted by this issue
177+
should adjust their Linux configuration according to the recommendations in the
178+
[GC guide](/doc/gc-guide#Linux_transparent_huge_pages), or switch to a Linux
179+
distribution that disables transparent huge pages altogether.
180+
168181
### Go 1.21
169182

170183
Go 1.21 made it a run-time error to call `panic` with a nil interface value,

src/runtime/extern.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,13 @@ It is a comma-separated list of name=val pairs setting these named variables:
5555
cgocheck mode can be enabled using GOEXPERIMENT (which
5656
requires a rebuild), see https://pkg.go.dev/internal/goexperiment for details.
5757
58+
disablethp: setting disablethp=1 on Linux disables transparent huge pages for the heap.
59+
It has no effect on other platforms. disablethp is meant for compatibility with versions
60+
of Go before 1.21, which stopped working around a Linux kernel default that can result
61+
in significant memory overuse. See https://go.dev/issue/64332. This setting will be
62+
removed in a future release, so operators should tweak their Linux configuration to suit
63+
their needs before then. See https://go.dev/doc/gc-guide#Linux_transparent_huge_pages.
64+
5865
dontfreezetheworld: by default, the start of a fatal panic or throw
5966
"freezes the world", preempting all threads to stop all running
6067
goroutines, which makes it possible to traceback all goroutines, and

src/runtime/mem_linux.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,4 +170,12 @@ func sysMapOS(v unsafe.Pointer, n uintptr) {
170170
print("runtime: mmap(", v, ", ", n, ") returned ", p, ", ", err, "\n")
171171
throw("runtime: cannot map pages in arena address space")
172172
}
173+
174+
// Disable huge pages if the GODEBUG for it is set.
175+
//
176+
// Note that there are a few sysHugePage calls that can override this, but
177+
// they're all for GC metadata.
178+
if debug.disablethp != 0 {
179+
sysNoHugePageOS(v, n)
180+
}
173181
}

src/runtime/runtime1.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,7 @@ type dbgVar struct {
309309
var debug struct {
310310
cgocheck int32
311311
clobberfree int32
312+
disablethp int32
312313
dontfreezetheworld int32
313314
efence int32
314315
gccheckmark int32
@@ -344,6 +345,7 @@ var dbgvars = []*dbgVar{
344345
{name: "allocfreetrace", value: &debug.allocfreetrace},
345346
{name: "clobberfree", value: &debug.clobberfree},
346347
{name: "cgocheck", value: &debug.cgocheck},
348+
{name: "disablethp", value: &debug.disablethp},
347349
{name: "dontfreezetheworld", value: &debug.dontfreezetheworld},
348350
{name: "efence", value: &debug.efence},
349351
{name: "gccheckmark", value: &debug.gccheckmark},

0 commit comments

Comments
 (0)