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