Debian Codegraph @

libgo/go/runtime/mbarrier.go

raw
1 // Copyright 2015 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 // Garbage collector: write barriers.
6 //
7 // For the concurrent garbage collector, the Go compiler implements
8 // updates to pointer-valued fields that may be in heap objects by
9 // emitting calls to write barriers. The main write barrier for
10 // individual pointer writes is gcWriteBarrier and is implemented in
11 // assembly. This file contains write barrier entry points for bulk
12 // operations. See also mwbbuf.go.
13
14 package runtime
15
16 import (
17 "internal/abi"
18 "internal/goarch"
19 "unsafe"
20 )
21
22 // For gccgo, use go:linkname to export compiler-called functions.
23 //
24 //go:linkname typedmemmove
25 //go:linkname typedslicecopy
26 //go:linkname memclrHasPointers
27
28 // Go uses a hybrid barrier that combines a Yuasa-style deletion
29 // barrier—which shades the object whose reference is being
30 // overwritten—with Dijkstra insertion barrier—which shades the object
31 // whose reference is being written. The insertion part of the barrier
32 // is necessary while the calling goroutine's stack is grey. In
33 // pseudocode, the barrier is:
34 //
35 // writePointer(slot, ptr):
36 // shade(*slot)
37 // if current stack is grey:
38 // shade(ptr)
39 // *slot = ptr
40 //
41 // slot is the destination in Go code.
42 // ptr is the value that goes into the slot in Go code.
43 //
44 // Shade indicates that it has seen a white pointer by adding the referent
45 // to wbuf as well as marking it.
46 //
47 // The two shades and the condition work together to prevent a mutator
48 // from hiding an object from the garbage collector:
49 //
50 // 1. shade(*slot) prevents a mutator from hiding an object by moving
51 // the sole pointer to it from the heap to its stack. If it attempts
52 // to unlink an object from the heap, this will shade it.
53 //
54 // 2. shade(ptr) prevents a mutator from hiding an object by moving
55 // the sole pointer to it from its stack into a black object in the
56 // heap. If it attempts to install the pointer into a black object,
57 // this will shade it.
58 //
59 // 3. Once a goroutine's stack is black, the shade(ptr) becomes
60 // unnecessary. shade(ptr) prevents hiding an object by moving it from
61 // the stack to the heap, but this requires first having a pointer
62 // hidden on the stack. Immediately after a stack is scanned, it only
63 // points to shaded objects, so it's not hiding anything, and the
64 // shade(*slot) prevents it from hiding any other pointers on its
65 // stack.
66 //
67 // For a detailed description of this barrier and proof of
68 // correctness, see https://github.com/golang/proposal/blob/master/design/17503-eliminate-rescan.md
69 //
70 //
71 //
72 // Dealing with memory ordering:
73 //
74 // Both the Yuasa and Dijkstra barriers can be made conditional on the
75 // color of the object containing the slot. We chose not to make these
76 // conditional because the cost of ensuring that the object holding
77 // the slot doesn't concurrently change color without the mutator
78 // noticing seems prohibitive.
79 //
80 // Consider the following example where the mutator writes into
81 // a slot and then loads the slot's mark bit while the GC thread
82 // writes to the slot's mark bit and then as part of scanning reads
83 // the slot.
84 //
85 // Initially both [slot] and [slotmark] are 0 (nil)
86 // Mutator thread GC thread
87 // st [slot], ptr st [slotmark], 1
88 //
89 // ld r1, [slotmark] ld r2, [slot]
90 //
91 // Without an expensive memory barrier between the st and the ld, the final
92 // result on most HW (including 386/amd64) can be r1==r2==0. This is a classic
93 // example of what can happen when loads are allowed to be reordered with older
94 // stores (avoiding such reorderings lies at the heart of the classic
95 // Peterson/Dekker algorithms for mutual exclusion). Rather than require memory
96 // barriers, which will slow down both the mutator and the GC, we always grey
97 // the ptr object regardless of the slot's color.
98 //
99 // Another place where we intentionally omit memory barriers is when
100 // accessing mheap_.arena_used to check if a pointer points into the
101 // heap. On relaxed memory machines, it's possible for a mutator to
102 // extend the size of the heap by updating arena_used, allocate an
103 // object from this new region, and publish a pointer to that object,
104 // but for tracing running on another processor to observe the pointer
105 // but use the old value of arena_used. In this case, tracing will not
106 // mark the object, even though it's reachable. However, the mutator
107 // is guaranteed to execute a write barrier when it publishes the
108 // pointer, so it will take care of marking the object. A general
109 // consequence of this is that the garbage collector may cache the
110 // value of mheap_.arena_used. (See issue #9984.)
111 //
112 //
113 // Stack writes:
114 //
115 // The compiler omits write barriers for writes to the current frame,
116 // but if a stack pointer has been passed down the call stack, the
117 // compiler will generate a write barrier for writes through that
118 // pointer (because it doesn't know it's not a heap pointer).
119 //
120 // One might be tempted to ignore the write barrier if slot points
121 // into to the stack. Don't do it! Mark termination only re-scans
122 // frames that have potentially been active since the concurrent scan,
123 // so it depends on write barriers to track changes to pointers in
124 // stack frames that have not been active.
125 //
126 //
127 // Global writes:
128 //
129 // The Go garbage collector requires write barriers when heap pointers
130 // are stored in globals. Many garbage collectors ignore writes to
131 // globals and instead pick up global -> heap pointers during
132 // termination. This increases pause time, so we instead rely on write
133 // barriers for writes to globals so that we don't have to rescan
134 // global during mark termination.
135 //
136 //
137 // Publication ordering:
138 //
139 // The write barrier is *pre-publication*, meaning that the write
140 // barrier happens prior to the *slot = ptr write that may make ptr
141 // reachable by some goroutine that currently cannot reach it.
142 //
143 //
144 // Signal handler pointer writes:
145 //
146 // In general, the signal handler cannot safely invoke the write
147 // barrier because it may run without a P or even during the write
148 // barrier.
149 //
150 // There is exactly one exception: profbuf.go omits a barrier during
151 // signal handler profile logging. That's safe only because of the
152 // deletion barrier. See profbuf.go for a detailed argument. If we
153 // remove the deletion barrier, we'll have to work out a new way to
154 // handle the profile logging.
155
156 // typedmemmove copies a value of type t to dst from src.
157 // Must be nosplit, see #16026.
158 //
159 // TODO: Perfect for go:nosplitrec since we can't have a safe point
160 // anywhere in the bulk barrier or memmove.
161 //
162 //go:nosplit
163 func typedmemmove(typ *_type, dst, src unsafe.Pointer) {
164 if dst == src {
165 return
166 }
167 if writeBarrier.needed && typ.ptrdata != 0 {
168 bulkBarrierPreWrite(uintptr(dst), uintptr(src), typ.ptrdata)
169 }
170 // There's a race here: if some other goroutine can write to
171 // src, it may change some pointer in src after we've
172 // performed the write barrier but before we perform the
173 // memory copy. This safe because the write performed by that
174 // other goroutine must also be accompanied by a write
175 // barrier, so at worst we've unnecessarily greyed the old
176 // pointer that was in src.
177 memmove(dst, src, typ.size)
178 if writeBarrier.cgo {
179 cgoCheckMemmove(typ, dst, src, 0, typ.size)
180 }
181 }
182
183 //go:linkname reflect_typedmemmove reflect.typedmemmove
184 func reflect_typedmemmove(typ *_type, dst, src unsafe.Pointer) {
185 if raceenabled {
186 raceWriteObjectPC(typ, dst, getcallerpc(), abi.FuncPCABIInternal(reflect_typedmemmove))
187 raceReadObjectPC(typ, src, getcallerpc(), abi.FuncPCABIInternal(reflect_typedmemmove))
188 }
189 if msanenabled {
190 msanwrite(dst, typ.size)
191 msanread(src, typ.size)
192 }
193 if asanenabled {
194 asanwrite(dst, typ.size)
195 asanread(src, typ.size)
196 }
197 typedmemmove(typ, dst, src)
198 }
199
200 //go:linkname reflectlite_typedmemmove internal_1reflectlite.typedmemmove
201 func reflectlite_typedmemmove(typ *_type, dst, src unsafe.Pointer) {
202 reflect_typedmemmove(typ, dst, src)
203 }
204
205 // typedmemmovepartial is like typedmemmove but assumes that
206 // dst and src point off bytes into the value and only copies size bytes.
207 // off must be a multiple of goarch.PtrSize.
208 //go:linkname reflect_typedmemmovepartial reflect.typedmemmovepartial
209 func reflect_typedmemmovepartial(typ *_type, dst, src unsafe.Pointer, off, size uintptr) {
210 if writeBarrier.needed && typ.ptrdata > off && size >= goarch.PtrSize {
211 if off&(goarch.PtrSize-1) != 0 {
212 panic("reflect: internal error: misaligned offset")
213 }
214 pwsize := alignDown(size, goarch.PtrSize)
215 if poff := typ.ptrdata - off; pwsize > poff {
216 pwsize = poff
217 }
218 bulkBarrierPreWrite(uintptr(dst), uintptr(src), pwsize)
219 }
220
221 memmove(dst, src, size)
222 if writeBarrier.cgo {
223 cgoCheckMemmove(typ, dst, src, off, size)
224 }
225 }
226
227 //go:nosplit
228 func typedslicecopy(typ *_type, dstPtr unsafe.Pointer, dstLen int, srcPtr unsafe.Pointer, srcLen int) int {
229 n := dstLen
230 if n > srcLen {
231 n = srcLen
232 }
233 if n == 0 {
234 return 0
235 }
236
237 // The compiler emits calls to typedslicecopy before
238 // instrumentation runs, so unlike the other copying and
239 // assignment operations, it's not instrumented in the calling
240 // code and needs its own instrumentation.
241 if raceenabled {
242 callerpc := getcallerpc()
243 pc := abi.FuncPCABIInternal(slicecopy)
244 racewriterangepc(dstPtr, uintptr(n)*typ.size, callerpc, pc)
245 racereadrangepc(srcPtr, uintptr(n)*typ.size, callerpc, pc)
246 }
247 if msanenabled {
248 msanwrite(dstPtr, uintptr(n)*typ.size)
249 msanread(srcPtr, uintptr(n)*typ.size)
250 }
251 if asanenabled {
252 asanwrite(dstPtr, uintptr(n)*typ.size)
253 asanread(srcPtr, uintptr(n)*typ.size)
254 }
255
256 if writeBarrier.cgo {
257 cgoCheckSliceCopy(typ, dstPtr, srcPtr, n)
258 }
259
260 if dstPtr == srcPtr {
261 return n
262 }
263
264 // Note: No point in checking typ.ptrdata here:
265 // compiler only emits calls to typedslicecopy for types with pointers,
266 // and growslice and reflect_typedslicecopy check for pointers
267 // before calling typedslicecopy.
268 size := uintptr(n) * typ.size
269 if writeBarrier.needed {
270 pwsize := size - typ.size + typ.ptrdata
271 bulkBarrierPreWrite(uintptr(dstPtr), uintptr(srcPtr), pwsize)
272 }
273 // See typedmemmove for a discussion of the race between the
274 // barrier and memmove.
275 memmove(dstPtr, srcPtr, size)
276 return n
277 }
278
279 //go:linkname reflect_typedslicecopy reflect.typedslicecopy
280 func reflect_typedslicecopy(elemType *_type, dst, src slice) int {
281 if elemType.ptrdata == 0 {
282 return slicecopy(dst.array, dst.len, src.array, src.len, elemType.size)
283 }
284 return typedslicecopy(elemType, dst.array, dst.len, src.array, src.len)
285 }
286
287 // typedmemclr clears the typed memory at ptr with type typ. The
288 // memory at ptr must already be initialized (and hence in type-safe
289 // state). If the memory is being initialized for the first time, see
290 // memclrNoHeapPointers.
291 //
292 // If the caller knows that typ has pointers, it can alternatively
293 // call memclrHasPointers.
294 //
295 //go:nosplit
296 func typedmemclr(typ *_type, ptr unsafe.Pointer) {
297 if writeBarrier.needed && typ.ptrdata != 0 {
298 bulkBarrierPreWrite(uintptr(ptr), 0, typ.ptrdata)
299 }
300 memclrNoHeapPointers(ptr, typ.size)
301 }
302
303 //go:linkname reflect_typedmemclr reflect.typedmemclr
304 func reflect_typedmemclr(typ *_type, ptr unsafe.Pointer) {
305 typedmemclr(typ, ptr)
306 }
307
308 //go:linkname reflect_typedmemclrpartial reflect.typedmemclrpartial
309 func reflect_typedmemclrpartial(typ *_type, ptr unsafe.Pointer, off, size uintptr) {
310 if writeBarrier.needed && typ.ptrdata != 0 {
311 bulkBarrierPreWrite(uintptr(ptr), 0, size)
312 }
313 memclrNoHeapPointers(ptr, size)
314 }
315
316 // memclrHasPointers clears n bytes of typed memory starting at ptr.
317 // The caller must ensure that the type of the object at ptr has
318 // pointers, usually by checking typ.ptrdata. However, ptr
319 // does not have to point to the start of the allocation.
320 //
321 //go:nosplit
322 func memclrHasPointers(ptr unsafe.Pointer, n uintptr) {
323 bulkBarrierPreWrite(uintptr(ptr), 0, n)
324 memclrNoHeapPointers(ptr, n)
325 }