Skip to content

Commit fdb45ac

Browse files
committed
runtime: move mem profile sampling into m-acquired section
It was not safe to do mcache profiling updates outside the critical section, but we got lucky because the runtime was not preemptible. Adding chunked memory clearing (CL 270943) created preemption opportunities, which led to corruption of runtime data structures. Fixes #47304. Fixes #47302. Change-Id: I461615470d62328a83ccbac537fbdc6dcde81c85 Reviewed-on: https://go-review.googlesource.com/c/go/+/336449 Trust: David Chase <[email protected]> Run-TryBot: David Chase <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Austin Clements <[email protected]> Reviewed-by: Keith Randall <[email protected]>
1 parent 3e48c03 commit fdb45ac

File tree

1 file changed

+9
-11
lines changed

1 file changed

+9
-11
lines changed

src/runtime/malloc.go

+9-11
Original file line numberDiff line numberDiff line change
@@ -1135,13 +1135,21 @@ func mallocgc(size uintptr, typ *_type, needzero bool) unsafe.Pointer {
11351135
msanmalloc(x, size)
11361136
}
11371137

1138+
if rate := MemProfileRate; rate > 0 {
1139+
// Note cache c only valid while m acquired; see #47302
1140+
if rate != 1 && size < c.nextSample {
1141+
c.nextSample -= size
1142+
} else {
1143+
profilealloc(mp, x, size)
1144+
}
1145+
}
11381146
mp.mallocing = 0
11391147
releasem(mp)
11401148

11411149
// Pointerfree data can be zeroed late in a context where preemption can occur.
11421150
// x will keep the memory alive.
11431151
if !isZeroed && needzero {
1144-
memclrNoHeapPointersChunked(size, x)
1152+
memclrNoHeapPointersChunked(size, x) // This is a possible preemption point: see #47302
11451153
}
11461154

11471155
if debug.malloc {
@@ -1155,16 +1163,6 @@ func mallocgc(size uintptr, typ *_type, needzero bool) unsafe.Pointer {
11551163
}
11561164
}
11571165

1158-
if rate := MemProfileRate; rate > 0 {
1159-
if rate != 1 && size < c.nextSample {
1160-
c.nextSample -= size
1161-
} else {
1162-
mp := acquirem()
1163-
profilealloc(mp, x, size)
1164-
releasem(mp)
1165-
}
1166-
}
1167-
11681166
if assistG != nil {
11691167
// Account for internal fragmentation in the assist
11701168
// debt now that we know it.

0 commit comments

Comments
 (0)