Skip to content

Commit b7eca1c

Browse files
mknyszekdmitshur
authored andcommitted
[release-branch.go1.14] runtime: make the scavenger's pacing logic more defensive
This change adds two bits of logic to the scavenger's pacing. Firstly, it checks to make sure we scavenged at least one physical page, if we released a non-zero amount of memory. If we try to release less than one physical page, most systems will release the whole page, which could lead to memory corruption down the road, and this is a signal we're in this situation. Secondly, the scavenger's pacing logic now checks to see if the time a scavenging operation takes is measured to be exactly zero or negative. The exact zero case can happen if time update granularity is too large to effectively capture the time the scavenging operation took, like on Windows where the OS timer frequency is generally 1ms. The negative case should not happen, but we're being defensive (against kernel bugs, bugs in the runtime, etc.). If either of these cases happen, we fall back to Go 1.13 behavior: assume the scavenge operation took around 10µs per physical page. We ignore huge pages in this case because we're in unknown territory, so we choose to be conservative about pacing (huge pages could only increase the rate of scavenging). Currently, the scavenger is broken on Windows because the granularity of time measurement is around 1 ms, which is too coarse to measure how fast we're scavenging, so we often end up with a scavenging time of zero, followed by NaNs and garbage values in the pacing logic, which usually leads to the scavenger sleeping forever. For #38617. Fixes #38856. Change-Id: Iaaa2a4cbb21338e1258d010f7362ed58b7db1af7 Reviewed-on: https://go-review.googlesource.com/c/go/+/229997 Run-TryBot: Michael Knyszek <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: David Chase <[email protected]> Reviewed-by: Austin Clements <[email protected]> (cherry picked from commit c791537) Reviewed-on: https://go-review.googlesource.com/c/go/+/232743
1 parent 6143ce3 commit b7eca1c

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

src/runtime/mgcscavenge.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,28 @@ func bgscavenge(c chan int) {
286286
continue
287287
}
288288

289+
if released < physPageSize {
290+
// If this happens, it means that we may have attempted to release part
291+
// of a physical page, but the likely effect of that is that it released
292+
// the whole physical page, some of which may have still been in-use.
293+
// This could lead to memory corruption. Throw.
294+
throw("released less than one physical page of memory")
295+
}
296+
297+
// On some platforms we may see crit as zero if the time it takes to scavenge
298+
// memory is less than the minimum granularity of its clock (e.g. Windows).
299+
// In this case, just assume scavenging takes 10 µs per regular physical page
300+
// (determined empirically), and conservatively ignore the impact of huge pages
301+
// on timing.
302+
//
303+
// We shouldn't ever see a crit value less than zero unless there's a bug of
304+
// some kind, either on our side or in the platform we're running on, but be
305+
// defensive in that case as well.
306+
const approxCritNSPerPhysicalPage = 10e3
307+
if crit <= 0 {
308+
crit = approxCritNSPerPhysicalPage * float64(released/physPageSize)
309+
}
310+
289311
// Multiply the critical time by 1 + the ratio of the costs of using
290312
// scavenged memory vs. scavenging memory. This forces us to pay down
291313
// the cost of reusing this memory eagerly by sleeping for a longer period

0 commit comments

Comments
 (0)