2014-11-11 15:05:02 -07:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package runtime
|
|
|
|
|
|
|
|
import "unsafe"
|
|
|
|
|
2015-02-19 11:38:46 -07:00
|
|
|
// Per-thread (in Go, per-P) cache for small objects.
|
|
|
|
// No locking needed because it is per-thread (per-P).
|
runtime: clear tiny alloc cache in mark term, not sweep term
The tiny alloc cache is maintained in a pointer from non-GC'd memory
(mcache) to heap memory and hence must be handled carefully.
Currently we clear the tiny alloc cache during sweep termination and,
if it is assigned to a non-nil value during concurrent marking, we
depend on a write barrier to keep the new value alive. However, while
the compiler currently always generates this write barrier, we're
treading on thin ice because write barriers may not happen for writes
to non-heap memory (e.g., typedmemmove). Without this lucky write
barrier, the GC may free a current tiny block while it's still
reachable by the tiny allocator, leading to later memory corruption.
Change this code so that, rather than depending on the write barrier,
we simply clear the tiny cache during mark termination when we're
clearing all of the other mcaches. If the current tiny block is
reachable from regular pointers, it will be retained; if it isn't
reachable from regular pointers, it may be freed, but that's okay
because there won't be any pointers in non-GC'd memory to it.
Change-Id: I8230980d8612c35c2997b9705641a1f9f865f879
Reviewed-on: https://go-review.googlesource.com/16962
Run-TryBot: Austin Clements <austin@google.com>
Reviewed-by: Russ Cox <rsc@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2015-11-16 13:20:59 -07:00
|
|
|
//
|
|
|
|
// mcaches are allocated from non-GC'd memory, so any heap pointers
|
|
|
|
// must be specially handled.
|
2015-02-19 11:38:46 -07:00
|
|
|
type mcache struct {
|
|
|
|
// The following members are accessed on every malloc,
|
|
|
|
// so they are grouped here for better caching.
|
2015-04-13 15:06:22 -06:00
|
|
|
next_sample int32 // trigger heap sample after allocating this many bytes
|
|
|
|
local_cachealloc uintptr // bytes allocated from cache since last lock of heap
|
2015-05-04 14:10:49 -06:00
|
|
|
local_scan uintptr // bytes of scannable heap allocated
|
runtime: clear tiny alloc cache in mark term, not sweep term
The tiny alloc cache is maintained in a pointer from non-GC'd memory
(mcache) to heap memory and hence must be handled carefully.
Currently we clear the tiny alloc cache during sweep termination and,
if it is assigned to a non-nil value during concurrent marking, we
depend on a write barrier to keep the new value alive. However, while
the compiler currently always generates this write barrier, we're
treading on thin ice because write barriers may not happen for writes
to non-heap memory (e.g., typedmemmove). Without this lucky write
barrier, the GC may free a current tiny block while it's still
reachable by the tiny allocator, leading to later memory corruption.
Change this code so that, rather than depending on the write barrier,
we simply clear the tiny cache during mark termination when we're
clearing all of the other mcaches. If the current tiny block is
reachable from regular pointers, it will be retained; if it isn't
reachable from regular pointers, it may be freed, but that's okay
because there won't be any pointers in non-GC'd memory to it.
Change-Id: I8230980d8612c35c2997b9705641a1f9f865f879
Reviewed-on: https://go-review.googlesource.com/16962
Run-TryBot: Austin Clements <austin@google.com>
Reviewed-by: Russ Cox <rsc@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2015-11-16 13:20:59 -07:00
|
|
|
|
2015-02-19 11:38:46 -07:00
|
|
|
// Allocator cache for tiny objects w/o pointers.
|
|
|
|
// See "Tiny allocator" comment in malloc.go.
|
runtime: clear tiny alloc cache in mark term, not sweep term
The tiny alloc cache is maintained in a pointer from non-GC'd memory
(mcache) to heap memory and hence must be handled carefully.
Currently we clear the tiny alloc cache during sweep termination and,
if it is assigned to a non-nil value during concurrent marking, we
depend on a write barrier to keep the new value alive. However, while
the compiler currently always generates this write barrier, we're
treading on thin ice because write barriers may not happen for writes
to non-heap memory (e.g., typedmemmove). Without this lucky write
barrier, the GC may free a current tiny block while it's still
reachable by the tiny allocator, leading to later memory corruption.
Change this code so that, rather than depending on the write barrier,
we simply clear the tiny cache during mark termination when we're
clearing all of the other mcaches. If the current tiny block is
reachable from regular pointers, it will be retained; if it isn't
reachable from regular pointers, it may be freed, but that's okay
because there won't be any pointers in non-GC'd memory to it.
Change-Id: I8230980d8612c35c2997b9705641a1f9f865f879
Reviewed-on: https://go-review.googlesource.com/16962
Run-TryBot: Austin Clements <austin@google.com>
Reviewed-by: Russ Cox <rsc@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2015-11-16 13:20:59 -07:00
|
|
|
|
|
|
|
// tiny points to the beginning of the current tiny block, or
|
|
|
|
// nil if there is no current tiny block.
|
|
|
|
//
|
|
|
|
// tiny is a heap pointer. Since mcache is in non-GC'd memory,
|
|
|
|
// we handle it by clearing it in releaseAll during mark
|
|
|
|
// termination.
|
2015-02-19 11:38:46 -07:00
|
|
|
tiny unsafe.Pointer
|
|
|
|
tinyoffset uintptr
|
|
|
|
local_tinyallocs uintptr // number of tiny allocs not counted in other stats
|
|
|
|
|
|
|
|
// The rest is not accessed on every malloc.
|
|
|
|
alloc [_NumSizeClasses]*mspan // spans to allocate from
|
|
|
|
|
|
|
|
stackcache [_NumStackOrders]stackfreelist
|
|
|
|
|
|
|
|
// Local allocator stats, flushed during GC.
|
|
|
|
local_nlookup uintptr // number of pointer lookups
|
|
|
|
local_largefree uintptr // bytes freed for large objects (>maxsmallsize)
|
|
|
|
local_nlargefree uintptr // number of frees for large objects (>maxsmallsize)
|
|
|
|
local_nsmallfree [_NumSizeClasses]uintptr // number of frees for small objects (<=maxsmallsize)
|
|
|
|
}
|
|
|
|
|
|
|
|
// A gclink is a node in a linked list of blocks, like mlink,
|
|
|
|
// but it is opaque to the garbage collector.
|
|
|
|
// The GC does not trace the pointers during collection,
|
|
|
|
// and the compiler does not emit write barriers for assignments
|
|
|
|
// of gclinkptr values. Code should store references to gclinks
|
|
|
|
// as gclinkptr, not as *gclink.
|
|
|
|
type gclink struct {
|
|
|
|
next gclinkptr
|
|
|
|
}
|
|
|
|
|
|
|
|
// A gclinkptr is a pointer to a gclink, but it is opaque
|
|
|
|
// to the garbage collector.
|
|
|
|
type gclinkptr uintptr
|
|
|
|
|
|
|
|
// ptr returns the *gclink form of p.
|
|
|
|
// The result should be used for accessing fields, not stored
|
|
|
|
// in other data structures.
|
|
|
|
func (p gclinkptr) ptr() *gclink {
|
|
|
|
return (*gclink)(unsafe.Pointer(p))
|
|
|
|
}
|
|
|
|
|
|
|
|
type stackfreelist struct {
|
|
|
|
list gclinkptr // linked list of free stacks
|
|
|
|
size uintptr // total size of stacks in list
|
|
|
|
}
|
|
|
|
|
2014-11-11 15:05:02 -07:00
|
|
|
// dummy MSpan that contains no free objects.
|
|
|
|
var emptymspan mspan
|
|
|
|
|
|
|
|
func allocmcache() *mcache {
|
|
|
|
lock(&mheap_.lock)
|
2015-11-11 17:13:51 -07:00
|
|
|
c := (*mcache)(mheap_.cachealloc.alloc())
|
2014-11-11 15:05:02 -07:00
|
|
|
unlock(&mheap_.lock)
|
|
|
|
memclr(unsafe.Pointer(c), unsafe.Sizeof(*c))
|
|
|
|
for i := 0; i < _NumSizeClasses; i++ {
|
|
|
|
c.alloc[i] = &emptymspan
|
|
|
|
}
|
2015-09-14 15:03:45 -06:00
|
|
|
c.next_sample = nextSample()
|
2014-11-11 15:05:02 -07:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
func freemcache(c *mcache) {
|
[dev.cc] runtime: delete scalararg, ptrarg; rename onM to systemstack
Scalararg and ptrarg are not "signal safe".
Go code filling them out can be interrupted by a signal,
and then the signal handler runs, and if it also ends up
in Go code that uses scalararg or ptrarg, now the old
values have been smashed.
For the pieces of code that do need to run in a signal handler,
we introduced onM_signalok, which is really just onM
except that the _signalok is meant to convey that the caller
asserts that scalarg and ptrarg will be restored to their old
values after the call (instead of the usual behavior, zeroing them).
Scalararg and ptrarg are also untyped and therefore error-prone.
Go code can always pass a closure instead of using scalararg
and ptrarg; they were only really necessary for C code.
And there's no more C code.
For all these reasons, delete scalararg and ptrarg, converting
the few remaining references to use closures.
Once those are gone, there is no need for a distinction between
onM and onM_signalok, so replace both with a single function
equivalent to the current onM_signalok (that is, it can be called
on any of the curg, g0, and gsignal stacks).
The name onM and the phrase 'm stack' are misnomers,
because on most system an M has two system stacks:
the main thread stack and the signal handling stack.
Correct the misnomer by naming the replacement function systemstack.
Fix a few references to "M stack" in code.
The main motivation for this change is to eliminate scalararg/ptrarg.
Rick and I have already seen them cause problems because
the calling sequence m.ptrarg[0] = p is a heap pointer assignment,
so it gets a write barrier. The write barrier also uses onM, so it has
all the same problems as if it were being invoked by a signal handler.
We worked around this by saving and restoring the old values
and by calling onM_signalok, but there's no point in keeping this nice
home for bugs around any longer.
This CL also changes funcline to return the file name as a result
instead of filling in a passed-in *string. (The *string signature is
left over from when the code was written in and called from C.)
That's arguably an unrelated change, except that once I had done
the ptrarg/scalararg/onM cleanup I started getting false positives
about the *string argument escaping (not allowed in package runtime).
The compiler is wrong, but the easiest fix is to write the code like
Go code instead of like C code. I am a bit worried that the compiler
is wrong because of some use of uninitialized memory in the escape
analysis. If that's the reason, it will go away when we convert the
compiler to Go. (And if not, we'll debug it the next time.)
LGTM=khr
R=r, khr
CC=austin, golang-codereviews, iant, rlh
https://golang.org/cl/174950043
2014-11-12 12:54:31 -07:00
|
|
|
systemstack(func() {
|
2015-11-11 17:13:51 -07:00
|
|
|
c.releaseAll()
|
2014-11-11 15:05:02 -07:00
|
|
|
stackcache_clear(c)
|
2014-11-15 06:00:38 -07:00
|
|
|
|
|
|
|
// NOTE(rsc,rlh): If gcworkbuffree comes back, we need to coordinate
|
|
|
|
// with the stealing of gcworkbufs during garbage collection to avoid
|
|
|
|
// a race where the workbuf is double-freed.
|
|
|
|
// gcworkbuffree(c.gcworkbuf)
|
|
|
|
|
2014-11-11 15:05:02 -07:00
|
|
|
lock(&mheap_.lock)
|
|
|
|
purgecachedstats(c)
|
2015-11-11 17:13:51 -07:00
|
|
|
mheap_.cachealloc.free(unsafe.Pointer(c))
|
2014-11-11 15:05:02 -07:00
|
|
|
unlock(&mheap_.lock)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Gets a span that has a free object in it and assigns it
|
|
|
|
// to be the cached span for the given sizeclass. Returns this span.
|
2015-11-11 17:13:51 -07:00
|
|
|
func (c *mcache) refill(sizeclass int32) *mspan {
|
2014-11-11 15:05:02 -07:00
|
|
|
_g_ := getg()
|
|
|
|
|
|
|
|
_g_.m.locks++
|
|
|
|
// Return the current cached span to the central lists.
|
|
|
|
s := c.alloc[sizeclass]
|
2014-11-20 10:08:13 -07:00
|
|
|
if s.freelist.ptr() != nil {
|
2014-12-27 21:58:00 -07:00
|
|
|
throw("refill on a nonempty span")
|
2014-11-11 15:05:02 -07:00
|
|
|
}
|
|
|
|
if s != &emptymspan {
|
|
|
|
s.incache = false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get a new cached span from the central lists.
|
2015-11-11 17:13:51 -07:00
|
|
|
s = mheap_.central[sizeclass].mcentral.cacheSpan()
|
2014-11-11 15:05:02 -07:00
|
|
|
if s == nil {
|
2014-12-27 21:58:00 -07:00
|
|
|
throw("out of memory")
|
2014-11-11 15:05:02 -07:00
|
|
|
}
|
2014-11-20 10:08:13 -07:00
|
|
|
if s.freelist.ptr() == nil {
|
2014-11-11 15:05:02 -07:00
|
|
|
println(s.ref, (s.npages<<_PageShift)/s.elemsize)
|
2014-12-27 21:58:00 -07:00
|
|
|
throw("empty span")
|
2014-11-11 15:05:02 -07:00
|
|
|
}
|
|
|
|
c.alloc[sizeclass] = s
|
|
|
|
_g_.m.locks--
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2015-11-11 17:13:51 -07:00
|
|
|
func (c *mcache) releaseAll() {
|
2014-11-11 15:05:02 -07:00
|
|
|
for i := 0; i < _NumSizeClasses; i++ {
|
|
|
|
s := c.alloc[i]
|
|
|
|
if s != &emptymspan {
|
2015-11-11 17:13:51 -07:00
|
|
|
mheap_.central[i].mcentral.uncacheSpan(s)
|
2014-11-11 15:05:02 -07:00
|
|
|
c.alloc[i] = &emptymspan
|
|
|
|
}
|
|
|
|
}
|
runtime: clear tiny alloc cache in mark term, not sweep term
The tiny alloc cache is maintained in a pointer from non-GC'd memory
(mcache) to heap memory and hence must be handled carefully.
Currently we clear the tiny alloc cache during sweep termination and,
if it is assigned to a non-nil value during concurrent marking, we
depend on a write barrier to keep the new value alive. However, while
the compiler currently always generates this write barrier, we're
treading on thin ice because write barriers may not happen for writes
to non-heap memory (e.g., typedmemmove). Without this lucky write
barrier, the GC may free a current tiny block while it's still
reachable by the tiny allocator, leading to later memory corruption.
Change this code so that, rather than depending on the write barrier,
we simply clear the tiny cache during mark termination when we're
clearing all of the other mcaches. If the current tiny block is
reachable from regular pointers, it will be retained; if it isn't
reachable from regular pointers, it may be freed, but that's okay
because there won't be any pointers in non-GC'd memory to it.
Change-Id: I8230980d8612c35c2997b9705641a1f9f865f879
Reviewed-on: https://go-review.googlesource.com/16962
Run-TryBot: Austin Clements <austin@google.com>
Reviewed-by: Russ Cox <rsc@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2015-11-16 13:20:59 -07:00
|
|
|
// Clear tinyalloc pool.
|
|
|
|
c.tiny = nil
|
|
|
|
c.tinyoffset = 0
|
2014-11-11 15:05:02 -07:00
|
|
|
}
|