diff options
author | Austin Clements <austin@google.com> | 2016-09-25 17:12:43 -0400 |
---|---|---|
committer | Austin Clements <austin@google.com> | 2016-10-28 18:20:23 +0000 |
commit | ae3bb4a537c84e43be2ed7b79e84450332aafe2d (patch) | |
tree | 3caa28c5dfb2b4a3265c854f12d3d0cbd43872a0 /src/runtime/mcache.go | |
parent | f4dcc9b29b052b99b97d0e445cda589f08bfc798 (diff) | |
download | go-ae3bb4a537c84e43be2ed7b79e84450332aafe2d.tar.gz go-ae3bb4a537c84e43be2ed7b79e84450332aafe2d.zip |
runtime: make fixalloc zero allocations on reuse
Currently fixalloc does not zero memory it reuses. This is dangerous
with the hybrid barrier if the type may contain heap pointers, since
it may cause us to observe a dead heap pointer on reuse. It's also
error-prone since it's the only allocator that doesn't zero on
allocation (mallocgc of course zeroes, but so do persistentalloc and
sysAlloc). It's also largely pointless: for mcache, the caller
immediately memclrs the allocation; and the two specials types are
tiny so there's no real cost to zeroing them.
Change fixalloc to zero allocations by default.
The only type we don't zero by default is mspan. This actually
requires that the spsn's sweepgen survive across freeing and
reallocating a span. If we were to zero it, the following race would
be possible:
1. The current sweepgen is 2. Span s is on the unswept list.
2. Direct sweeping sweeps span s, finds it's all free, and releases s
to the fixalloc.
3. Thread 1 allocates s from fixalloc. Suppose this zeros s, including
s.sweepgen.
4. Thread 1 calls s.init, which sets s.state to _MSpanDead.
5. On thread 2, background sweeping comes across span s in allspans
and cas's s.sweepgen from 0 (sg-2) to 1 (sg-1). Now it thinks it
owns it for sweeping. 6. Thread 1 continues initializing s.
Everything breaks.
I would like to fix this because it's obviously confusing, but it's a
subtle enough problem that I'm leaving it alone for now. The solution
may be to skip sweepgen 0, but then we have to think about wrap-around
much more carefully.
Updates #17503.
Change-Id: Ie08691feed3abbb06a31381b94beb0a2e36a0613
Reviewed-on: https://go-review.googlesource.com/31368
Reviewed-by: Keith Randall <khr@golang.org>
Reviewed-by: Rick Hudson <rlh@golang.org>
Diffstat (limited to 'src/runtime/mcache.go')
-rw-r--r-- | src/runtime/mcache.go | 1 |
1 files changed, 0 insertions, 1 deletions
diff --git a/src/runtime/mcache.go b/src/runtime/mcache.go index 38d5492df1..c483310cee 100644 --- a/src/runtime/mcache.go +++ b/src/runtime/mcache.go @@ -77,7 +77,6 @@ func allocmcache() *mcache { lock(&mheap_.lock) c := (*mcache)(mheap_.cachealloc.alloc()) unlock(&mheap_.lock) - memclr(unsafe.Pointer(c), unsafe.Sizeof(*c)) for i := 0; i < _NumSizeClasses; i++ { c.alloc[i] = &emptymspan } |