Source file src/runtime/mheap.go
1 // Copyright 2009 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 // Page heap. 6 // 7 // See malloc.go for overview. 8 9 package runtime 10 11 import ( 12 "internal/cpu" 13 "internal/goarch" 14 "internal/runtime/atomic" 15 "runtime/internal/sys" 16 "unsafe" 17 ) 18 19 const ( 20 // minPhysPageSize is a lower-bound on the physical page size. The 21 // true physical page size may be larger than this. In contrast, 22 // sys.PhysPageSize is an upper-bound on the physical page size. 23 minPhysPageSize = 4096 24 25 // maxPhysPageSize is the maximum page size the runtime supports. 26 maxPhysPageSize = 512 << 10 27 28 // maxPhysHugePageSize sets an upper-bound on the maximum huge page size 29 // that the runtime supports. 30 maxPhysHugePageSize = pallocChunkBytes 31 32 // pagesPerReclaimerChunk indicates how many pages to scan from the 33 // pageInUse bitmap at a time. Used by the page reclaimer. 34 // 35 // Higher values reduce contention on scanning indexes (such as 36 // h.reclaimIndex), but increase the minimum latency of the 37 // operation. 38 // 39 // The time required to scan this many pages can vary a lot depending 40 // on how many spans are actually freed. Experimentally, it can 41 // scan for pages at ~300 GB/ms on a 2.6GHz Core i7, but can only 42 // free spans at ~32 MB/ms. Using 512 pages bounds this at 43 // roughly 100µs. 44 // 45 // Must be a multiple of the pageInUse bitmap element size and 46 // must also evenly divide pagesPerArena. 47 pagesPerReclaimerChunk = 512 48 49 // physPageAlignedStacks indicates whether stack allocations must be 50 // physical page aligned. This is a requirement for MAP_STACK on 51 // OpenBSD. 52 physPageAlignedStacks = GOOS == "openbsd" 53 ) 54 55 // Main malloc heap. 56 // The heap itself is the "free" and "scav" treaps, 57 // but all the other global data is here too. 58 // 59 // mheap must not be heap-allocated because it contains mSpanLists, 60 // which must not be heap-allocated. 61 type mheap struct { 62 _ sys.NotInHeap 63 64 // lock must only be acquired on the system stack, otherwise a g 65 // could self-deadlock if its stack grows with the lock held. 66 lock mutex 67 68 pages pageAlloc // page allocation data structure 69 70 sweepgen uint32 // sweep generation, see comment in mspan; written during STW 71 72 // allspans is a slice of all mspans ever created. Each mspan 73 // appears exactly once. 74 // 75 // The memory for allspans is manually managed and can be 76 // reallocated and move as the heap grows. 77 // 78 // In general, allspans is protected by mheap_.lock, which 79 // prevents concurrent access as well as freeing the backing 80 // store. Accesses during STW might not hold the lock, but 81 // must ensure that allocation cannot happen around the 82 // access (since that may free the backing store). 83 allspans []*mspan // all spans out there 84 85 // Proportional sweep 86 // 87 // These parameters represent a linear function from gcController.heapLive 88 // to page sweep count. The proportional sweep system works to 89 // stay in the black by keeping the current page sweep count 90 // above this line at the current gcController.heapLive. 91 // 92 // The line has slope sweepPagesPerByte and passes through a 93 // basis point at (sweepHeapLiveBasis, pagesSweptBasis). At 94 // any given time, the system is at (gcController.heapLive, 95 // pagesSwept) in this space. 96 // 97 // It is important that the line pass through a point we 98 // control rather than simply starting at a 0,0 origin 99 // because that lets us adjust sweep pacing at any time while 100 // accounting for current progress. If we could only adjust 101 // the slope, it would create a discontinuity in debt if any 102 // progress has already been made. 103 pagesInUse atomic.Uintptr // pages of spans in stats mSpanInUse 104 pagesSwept atomic.Uint64 // pages swept this cycle 105 pagesSweptBasis atomic.Uint64 // pagesSwept to use as the origin of the sweep ratio 106 sweepHeapLiveBasis uint64 // value of gcController.heapLive to use as the origin of sweep ratio; written with lock, read without 107 sweepPagesPerByte float64 // proportional sweep ratio; written with lock, read without 108 109 // Page reclaimer state 110 111 // reclaimIndex is the page index in allArenas of next page to 112 // reclaim. Specifically, it refers to page (i % 113 // pagesPerArena) of arena allArenas[i / pagesPerArena]. 114 // 115 // If this is >= 1<<63, the page reclaimer is done scanning 116 // the page marks. 117 reclaimIndex atomic.Uint64 118 119 // reclaimCredit is spare credit for extra pages swept. Since 120 // the page reclaimer works in large chunks, it may reclaim 121 // more than requested. Any spare pages released go to this 122 // credit pool. 123 reclaimCredit atomic.Uintptr 124 125 _ cpu.CacheLinePad // prevents false-sharing between arenas and preceding variables 126 127 // arenas is the heap arena map. It points to the metadata for 128 // the heap for every arena frame of the entire usable virtual 129 // address space. 130 // 131 // Use arenaIndex to compute indexes into this array. 132 // 133 // For regions of the address space that are not backed by the 134 // Go heap, the arena map contains nil. 135 // 136 // Modifications are protected by mheap_.lock. Reads can be 137 // performed without locking; however, a given entry can 138 // transition from nil to non-nil at any time when the lock 139 // isn't held. (Entries never transitions back to nil.) 140 // 141 // In general, this is a two-level mapping consisting of an L1 142 // map and possibly many L2 maps. This saves space when there 143 // are a huge number of arena frames. However, on many 144 // platforms (even 64-bit), arenaL1Bits is 0, making this 145 // effectively a single-level map. In this case, arenas[0] 146 // will never be nil. 147 arenas [1 << arenaL1Bits]*[1 << arenaL2Bits]*heapArena 148 149 // arenasHugePages indicates whether arenas' L2 entries are eligible 150 // to be backed by huge pages. 151 arenasHugePages bool 152 153 // heapArenaAlloc is pre-reserved space for allocating heapArena 154 // objects. This is only used on 32-bit, where we pre-reserve 155 // this space to avoid interleaving it with the heap itself. 156 heapArenaAlloc linearAlloc 157 158 // arenaHints is a list of addresses at which to attempt to 159 // add more heap arenas. This is initially populated with a 160 // set of general hint addresses, and grown with the bounds of 161 // actual heap arena ranges. 162 arenaHints *arenaHint 163 164 // arena is a pre-reserved space for allocating heap arenas 165 // (the actual arenas). This is only used on 32-bit. 166 arena linearAlloc 167 168 // allArenas is the arenaIndex of every mapped arena. This can 169 // be used to iterate through the address space. 170 // 171 // Access is protected by mheap_.lock. However, since this is 172 // append-only and old backing arrays are never freed, it is 173 // safe to acquire mheap_.lock, copy the slice header, and 174 // then release mheap_.lock. 175 allArenas []arenaIdx 176 177 // sweepArenas is a snapshot of allArenas taken at the 178 // beginning of the sweep cycle. This can be read safely by 179 // simply blocking GC (by disabling preemption). 180 sweepArenas []arenaIdx 181 182 // markArenas is a snapshot of allArenas taken at the beginning 183 // of the mark cycle. Because allArenas is append-only, neither 184 // this slice nor its contents will change during the mark, so 185 // it can be read safely. 186 markArenas []arenaIdx 187 188 // curArena is the arena that the heap is currently growing 189 // into. This should always be physPageSize-aligned. 190 curArena struct { 191 base, end uintptr 192 } 193 194 // central free lists for small size classes. 195 // the padding makes sure that the mcentrals are 196 // spaced CacheLinePadSize bytes apart, so that each mcentral.lock 197 // gets its own cache line. 198 // central is indexed by spanClass. 199 central [numSpanClasses]struct { 200 mcentral mcentral 201 pad [(cpu.CacheLinePadSize - unsafe.Sizeof(mcentral{})%cpu.CacheLinePadSize) % cpu.CacheLinePadSize]byte 202 } 203 204 spanalloc fixalloc // allocator for span* 205 cachealloc fixalloc // allocator for mcache* 206 specialfinalizeralloc fixalloc // allocator for specialfinalizer* 207 specialprofilealloc fixalloc // allocator for specialprofile* 208 specialReachableAlloc fixalloc // allocator for specialReachable 209 specialPinCounterAlloc fixalloc // allocator for specialPinCounter 210 specialWeakHandleAlloc fixalloc // allocator for specialWeakHandle 211 speciallock mutex // lock for special record allocators. 212 arenaHintAlloc fixalloc // allocator for arenaHints 213 214 // User arena state. 215 // 216 // Protected by mheap_.lock. 217 userArena struct { 218 // arenaHints is a list of addresses at which to attempt to 219 // add more heap arenas for user arena chunks. This is initially 220 // populated with a set of general hint addresses, and grown with 221 // the bounds of actual heap arena ranges. 222 arenaHints *arenaHint 223 224 // quarantineList is a list of user arena spans that have been set to fault, but 225 // are waiting for all pointers into them to go away. Sweeping handles 226 // identifying when this is true, and moves the span to the ready list. 227 quarantineList mSpanList 228 229 // readyList is a list of empty user arena spans that are ready for reuse. 230 readyList mSpanList 231 } 232 233 unused *specialfinalizer // never set, just here to force the specialfinalizer type into DWARF 234 } 235 236 var mheap_ mheap 237 238 // A heapArena stores metadata for a heap arena. heapArenas are stored 239 // outside of the Go heap and accessed via the mheap_.arenas index. 240 type heapArena struct { 241 _ sys.NotInHeap 242 243 // spans maps from virtual address page ID within this arena to *mspan. 244 // For allocated spans, their pages map to the span itself. 245 // For free spans, only the lowest and highest pages map to the span itself. 246 // Internal pages map to an arbitrary span. 247 // For pages that have never been allocated, spans entries are nil. 248 // 249 // Modifications are protected by mheap.lock. Reads can be 250 // performed without locking, but ONLY from indexes that are 251 // known to contain in-use or stack spans. This means there 252 // must not be a safe-point between establishing that an 253 // address is live and looking it up in the spans array. 254 spans [pagesPerArena]*mspan 255 256 // pageInUse is a bitmap that indicates which spans are in 257 // state mSpanInUse. This bitmap is indexed by page number, 258 // but only the bit corresponding to the first page in each 259 // span is used. 260 // 261 // Reads and writes are atomic. 262 pageInUse [pagesPerArena / 8]uint8 263 264 // pageMarks is a bitmap that indicates which spans have any 265 // marked objects on them. Like pageInUse, only the bit 266 // corresponding to the first page in each span is used. 267 // 268 // Writes are done atomically during marking. Reads are 269 // non-atomic and lock-free since they only occur during 270 // sweeping (and hence never race with writes). 271 // 272 // This is used to quickly find whole spans that can be freed. 273 // 274 // TODO(austin): It would be nice if this was uint64 for 275 // faster scanning, but we don't have 64-bit atomic bit 276 // operations. 277 pageMarks [pagesPerArena / 8]uint8 278 279 // pageSpecials is a bitmap that indicates which spans have 280 // specials (finalizers or other). Like pageInUse, only the bit 281 // corresponding to the first page in each span is used. 282 // 283 // Writes are done atomically whenever a special is added to 284 // a span and whenever the last special is removed from a span. 285 // Reads are done atomically to find spans containing specials 286 // during marking. 287 pageSpecials [pagesPerArena / 8]uint8 288 289 // checkmarks stores the debug.gccheckmark state. It is only 290 // used if debug.gccheckmark > 0. 291 checkmarks *checkmarksMap 292 293 // zeroedBase marks the first byte of the first page in this 294 // arena which hasn't been used yet and is therefore already 295 // zero. zeroedBase is relative to the arena base. 296 // Increases monotonically until it hits heapArenaBytes. 297 // 298 // This field is sufficient to determine if an allocation 299 // needs to be zeroed because the page allocator follows an 300 // address-ordered first-fit policy. 301 // 302 // Read atomically and written with an atomic CAS. 303 zeroedBase uintptr 304 } 305 306 // arenaHint is a hint for where to grow the heap arenas. See 307 // mheap_.arenaHints. 308 type arenaHint struct { 309 _ sys.NotInHeap 310 addr uintptr 311 down bool 312 next *arenaHint 313 } 314 315 // An mspan is a run of pages. 316 // 317 // When a mspan is in the heap free treap, state == mSpanFree 318 // and heapmap(s->start) == span, heapmap(s->start+s->npages-1) == span. 319 // If the mspan is in the heap scav treap, then in addition to the 320 // above scavenged == true. scavenged == false in all other cases. 321 // 322 // When a mspan is allocated, state == mSpanInUse or mSpanManual 323 // and heapmap(i) == span for all s->start <= i < s->start+s->npages. 324 325 // Every mspan is in one doubly-linked list, either in the mheap's 326 // busy list or one of the mcentral's span lists. 327 328 // An mspan representing actual memory has state mSpanInUse, 329 // mSpanManual, or mSpanFree. Transitions between these states are 330 // constrained as follows: 331 // 332 // - A span may transition from free to in-use or manual during any GC 333 // phase. 334 // 335 // - During sweeping (gcphase == _GCoff), a span may transition from 336 // in-use to free (as a result of sweeping) or manual to free (as a 337 // result of stacks being freed). 338 // 339 // - During GC (gcphase != _GCoff), a span *must not* transition from 340 // manual or in-use to free. Because concurrent GC may read a pointer 341 // and then look up its span, the span state must be monotonic. 342 // 343 // Setting mspan.state to mSpanInUse or mSpanManual must be done 344 // atomically and only after all other span fields are valid. 345 // Likewise, if inspecting a span is contingent on it being 346 // mSpanInUse, the state should be loaded atomically and checked 347 // before depending on other fields. This allows the garbage collector 348 // to safely deal with potentially invalid pointers, since resolving 349 // such pointers may race with a span being allocated. 350 type mSpanState uint8 351 352 const ( 353 mSpanDead mSpanState = iota 354 mSpanInUse // allocated for garbage collected heap 355 mSpanManual // allocated for manual management (e.g., stack allocator) 356 ) 357 358 // mSpanStateNames are the names of the span states, indexed by 359 // mSpanState. 360 var mSpanStateNames = []string{ 361 "mSpanDead", 362 "mSpanInUse", 363 "mSpanManual", 364 } 365 366 // mSpanStateBox holds an atomic.Uint8 to provide atomic operations on 367 // an mSpanState. This is a separate type to disallow accidental comparison 368 // or assignment with mSpanState. 369 type mSpanStateBox struct { 370 s atomic.Uint8 371 } 372 373 // It is nosplit to match get, below. 374 375 //go:nosplit 376 func (b *mSpanStateBox) set(s mSpanState) { 377 b.s.Store(uint8(s)) 378 } 379 380 // It is nosplit because it's called indirectly by typedmemclr, 381 // which must not be preempted. 382 383 //go:nosplit 384 func (b *mSpanStateBox) get() mSpanState { 385 return mSpanState(b.s.Load()) 386 } 387 388 // mSpanList heads a linked list of spans. 389 type mSpanList struct { 390 _ sys.NotInHeap 391 first *mspan // first span in list, or nil if none 392 last *mspan // last span in list, or nil if none 393 } 394 395 type mspan struct { 396 _ sys.NotInHeap 397 next *mspan // next span in list, or nil if none 398 prev *mspan // previous span in list, or nil if none 399 list *mSpanList // For debugging. 400 401 startAddr uintptr // address of first byte of span aka s.base() 402 npages uintptr // number of pages in span 403 404 manualFreeList gclinkptr // list of free objects in mSpanManual spans 405 406 // freeindex is the slot index between 0 and nelems at which to begin scanning 407 // for the next free object in this span. 408 // Each allocation scans allocBits starting at freeindex until it encounters a 0 409 // indicating a free object. freeindex is then adjusted so that subsequent scans begin 410 // just past the newly discovered free object. 411 // 412 // If freeindex == nelem, this span has no free objects. 413 // 414 // allocBits is a bitmap of objects in this span. 415 // If n >= freeindex and allocBits[n/8] & (1<<(n%8)) is 0 416 // then object n is free; 417 // otherwise, object n is allocated. Bits starting at nelem are 418 // undefined and should never be referenced. 419 // 420 // Object n starts at address n*elemsize + (start << pageShift). 421 freeindex uint16 422 // TODO: Look up nelems from sizeclass and remove this field if it 423 // helps performance. 424 nelems uint16 // number of object in the span. 425 // freeIndexForScan is like freeindex, except that freeindex is 426 // used by the allocator whereas freeIndexForScan is used by the 427 // GC scanner. They are two fields so that the GC sees the object 428 // is allocated only when the object and the heap bits are 429 // initialized (see also the assignment of freeIndexForScan in 430 // mallocgc, and issue 54596). 431 freeIndexForScan uint16 432 433 // Cache of the allocBits at freeindex. allocCache is shifted 434 // such that the lowest bit corresponds to the bit freeindex. 435 // allocCache holds the complement of allocBits, thus allowing 436 // ctz (count trailing zero) to use it directly. 437 // allocCache may contain bits beyond s.nelems; the caller must ignore 438 // these. 439 allocCache uint64 440 441 // allocBits and gcmarkBits hold pointers to a span's mark and 442 // allocation bits. The pointers are 8 byte aligned. 443 // There are three arenas where this data is held. 444 // free: Dirty arenas that are no longer accessed 445 // and can be reused. 446 // next: Holds information to be used in the next GC cycle. 447 // current: Information being used during this GC cycle. 448 // previous: Information being used during the last GC cycle. 449 // A new GC cycle starts with the call to finishsweep_m. 450 // finishsweep_m moves the previous arena to the free arena, 451 // the current arena to the previous arena, and 452 // the next arena to the current arena. 453 // The next arena is populated as the spans request 454 // memory to hold gcmarkBits for the next GC cycle as well 455 // as allocBits for newly allocated spans. 456 // 457 // The pointer arithmetic is done "by hand" instead of using 458 // arrays to avoid bounds checks along critical performance 459 // paths. 460 // The sweep will free the old allocBits and set allocBits to the 461 // gcmarkBits. The gcmarkBits are replaced with a fresh zeroed 462 // out memory. 463 allocBits *gcBits 464 gcmarkBits *gcBits 465 pinnerBits *gcBits // bitmap for pinned objects; accessed atomically 466 467 // sweep generation: 468 // if sweepgen == h->sweepgen - 2, the span needs sweeping 469 // if sweepgen == h->sweepgen - 1, the span is currently being swept 470 // if sweepgen == h->sweepgen, the span is swept and ready to use 471 // if sweepgen == h->sweepgen + 1, the span was cached before sweep began and is still cached, and needs sweeping 472 // if sweepgen == h->sweepgen + 3, the span was swept and then cached and is still cached 473 // h->sweepgen is incremented by 2 after every GC 474 475 sweepgen uint32 476 divMul uint32 // for divide by elemsize 477 allocCount uint16 // number of allocated objects 478 spanclass spanClass // size class and noscan (uint8) 479 state mSpanStateBox // mSpanInUse etc; accessed atomically (get/set methods) 480 needzero uint8 // needs to be zeroed before allocation 481 isUserArenaChunk bool // whether or not this span represents a user arena 482 allocCountBeforeCache uint16 // a copy of allocCount that is stored just before this span is cached 483 elemsize uintptr // computed from sizeclass or from npages 484 limit uintptr // end of data in span 485 speciallock mutex // guards specials list and changes to pinnerBits 486 specials *special // linked list of special records sorted by offset. 487 userArenaChunkFree addrRange // interval for managing chunk allocation 488 largeType *_type // malloc header for large objects. 489 } 490 491 func (s *mspan) base() uintptr { 492 return s.startAddr 493 } 494 495 func (s *mspan) layout() (size, n, total uintptr) { 496 total = s.npages << _PageShift 497 size = s.elemsize 498 if size > 0 { 499 n = total / size 500 } 501 return 502 } 503 504 // recordspan adds a newly allocated span to h.allspans. 505 // 506 // This only happens the first time a span is allocated from 507 // mheap.spanalloc (it is not called when a span is reused). 508 // 509 // Write barriers are disallowed here because it can be called from 510 // gcWork when allocating new workbufs. However, because it's an 511 // indirect call from the fixalloc initializer, the compiler can't see 512 // this. 513 // 514 // The heap lock must be held. 515 // 516 //go:nowritebarrierrec 517 func recordspan(vh unsafe.Pointer, p unsafe.Pointer) { 518 h := (*mheap)(vh) 519 s := (*mspan)(p) 520 521 assertLockHeld(&h.lock) 522 523 if len(h.allspans) >= cap(h.allspans) { 524 n := 64 * 1024 / goarch.PtrSize 525 if n < cap(h.allspans)*3/2 { 526 n = cap(h.allspans) * 3 / 2 527 } 528 var new []*mspan 529 sp := (*slice)(unsafe.Pointer(&new)) 530 sp.array = sysAlloc(uintptr(n)*goarch.PtrSize, &memstats.other_sys) 531 if sp.array == nil { 532 throw("runtime: cannot allocate memory") 533 } 534 sp.len = len(h.allspans) 535 sp.cap = n 536 if len(h.allspans) > 0 { 537 copy(new, h.allspans) 538 } 539 oldAllspans := h.allspans 540 *(*notInHeapSlice)(unsafe.Pointer(&h.allspans)) = *(*notInHeapSlice)(unsafe.Pointer(&new)) 541 if len(oldAllspans) != 0 { 542 sysFree(unsafe.Pointer(&oldAllspans[0]), uintptr(cap(oldAllspans))*unsafe.Sizeof(oldAllspans[0]), &memstats.other_sys) 543 } 544 } 545 h.allspans = h.allspans[:len(h.allspans)+1] 546 h.allspans[len(h.allspans)-1] = s 547 } 548 549 // A spanClass represents the size class and noscan-ness of a span. 550 // 551 // Each size class has a noscan spanClass and a scan spanClass. The 552 // noscan spanClass contains only noscan objects, which do not contain 553 // pointers and thus do not need to be scanned by the garbage 554 // collector. 555 type spanClass uint8 556 557 const ( 558 numSpanClasses = _NumSizeClasses << 1 559 tinySpanClass = spanClass(tinySizeClass<<1 | 1) 560 ) 561 562 func makeSpanClass(sizeclass uint8, noscan bool) spanClass { 563 return spanClass(sizeclass<<1) | spanClass(bool2int(noscan)) 564 } 565 566 //go:nosplit 567 func (sc spanClass) sizeclass() int8 { 568 return int8(sc >> 1) 569 } 570 571 //go:nosplit 572 func (sc spanClass) noscan() bool { 573 return sc&1 != 0 574 } 575 576 // arenaIndex returns the index into mheap_.arenas of the arena 577 // containing metadata for p. This index combines of an index into the 578 // L1 map and an index into the L2 map and should be used as 579 // mheap_.arenas[ai.l1()][ai.l2()]. 580 // 581 // If p is outside the range of valid heap addresses, either l1() or 582 // l2() will be out of bounds. 583 // 584 // It is nosplit because it's called by spanOf and several other 585 // nosplit functions. 586 // 587 //go:nosplit 588 func arenaIndex(p uintptr) arenaIdx { 589 return arenaIdx((p - arenaBaseOffset) / heapArenaBytes) 590 } 591 592 // arenaBase returns the low address of the region covered by heap 593 // arena i. 594 func arenaBase(i arenaIdx) uintptr { 595 return uintptr(i)*heapArenaBytes + arenaBaseOffset 596 } 597 598 type arenaIdx uint 599 600 // l1 returns the "l1" portion of an arenaIdx. 601 // 602 // Marked nosplit because it's called by spanOf and other nosplit 603 // functions. 604 // 605 //go:nosplit 606 func (i arenaIdx) l1() uint { 607 if arenaL1Bits == 0 { 608 // Let the compiler optimize this away if there's no 609 // L1 map. 610 return 0 611 } else { 612 return uint(i) >> arenaL1Shift 613 } 614 } 615 616 // l2 returns the "l2" portion of an arenaIdx. 617 // 618 // Marked nosplit because it's called by spanOf and other nosplit funcs. 619 // functions. 620 // 621 //go:nosplit 622 func (i arenaIdx) l2() uint { 623 if arenaL1Bits == 0 { 624 return uint(i) 625 } else { 626 return uint(i) & (1<<arenaL2Bits - 1) 627 } 628 } 629 630 // inheap reports whether b is a pointer into a (potentially dead) heap object. 631 // It returns false for pointers into mSpanManual spans. 632 // Non-preemptible because it is used by write barriers. 633 // 634 //go:nowritebarrier 635 //go:nosplit 636 func inheap(b uintptr) bool { 637 return spanOfHeap(b) != nil 638 } 639 640 // inHeapOrStack is a variant of inheap that returns true for pointers 641 // into any allocated heap span. 642 // 643 //go:nowritebarrier 644 //go:nosplit 645 func inHeapOrStack(b uintptr) bool { 646 s := spanOf(b) 647 if s == nil || b < s.base() { 648 return false 649 } 650 switch s.state.get() { 651 case mSpanInUse, mSpanManual: 652 return b < s.limit 653 default: 654 return false 655 } 656 } 657 658 // spanOf returns the span of p. If p does not point into the heap 659 // arena or no span has ever contained p, spanOf returns nil. 660 // 661 // If p does not point to allocated memory, this may return a non-nil 662 // span that does *not* contain p. If this is a possibility, the 663 // caller should either call spanOfHeap or check the span bounds 664 // explicitly. 665 // 666 // Must be nosplit because it has callers that are nosplit. 667 // 668 //go:nosplit 669 func spanOf(p uintptr) *mspan { 670 // This function looks big, but we use a lot of constant 671 // folding around arenaL1Bits to get it under the inlining 672 // budget. Also, many of the checks here are safety checks 673 // that Go needs to do anyway, so the generated code is quite 674 // short. 675 ri := arenaIndex(p) 676 if arenaL1Bits == 0 { 677 // If there's no L1, then ri.l1() can't be out of bounds but ri.l2() can. 678 if ri.l2() >= uint(len(mheap_.arenas[0])) { 679 return nil 680 } 681 } else { 682 // If there's an L1, then ri.l1() can be out of bounds but ri.l2() can't. 683 if ri.l1() >= uint(len(mheap_.arenas)) { 684 return nil 685 } 686 } 687 l2 := mheap_.arenas[ri.l1()] 688 if arenaL1Bits != 0 && l2 == nil { // Should never happen if there's no L1. 689 return nil 690 } 691 ha := l2[ri.l2()] 692 if ha == nil { 693 return nil 694 } 695 return ha.spans[(p/pageSize)%pagesPerArena] 696 } 697 698 // spanOfUnchecked is equivalent to spanOf, but the caller must ensure 699 // that p points into an allocated heap arena. 700 // 701 // Must be nosplit because it has callers that are nosplit. 702 // 703 //go:nosplit 704 func spanOfUnchecked(p uintptr) *mspan { 705 ai := arenaIndex(p) 706 return mheap_.arenas[ai.l1()][ai.l2()].spans[(p/pageSize)%pagesPerArena] 707 } 708 709 // spanOfHeap is like spanOf, but returns nil if p does not point to a 710 // heap object. 711 // 712 // Must be nosplit because it has callers that are nosplit. 713 // 714 //go:nosplit 715 func spanOfHeap(p uintptr) *mspan { 716 s := spanOf(p) 717 // s is nil if it's never been allocated. Otherwise, we check 718 // its state first because we don't trust this pointer, so we 719 // have to synchronize with span initialization. Then, it's 720 // still possible we picked up a stale span pointer, so we 721 // have to check the span's bounds. 722 if s == nil || s.state.get() != mSpanInUse || p < s.base() || p >= s.limit { 723 return nil 724 } 725 return s 726 } 727 728 // pageIndexOf returns the arena, page index, and page mask for pointer p. 729 // The caller must ensure p is in the heap. 730 func pageIndexOf(p uintptr) (arena *heapArena, pageIdx uintptr, pageMask uint8) { 731 ai := arenaIndex(p) 732 arena = mheap_.arenas[ai.l1()][ai.l2()] 733 pageIdx = ((p / pageSize) / 8) % uintptr(len(arena.pageInUse)) 734 pageMask = byte(1 << ((p / pageSize) % 8)) 735 return 736 } 737 738 // Initialize the heap. 739 func (h *mheap) init() { 740 lockInit(&h.lock, lockRankMheap) 741 lockInit(&h.speciallock, lockRankMheapSpecial) 742 743 h.spanalloc.init(unsafe.Sizeof(mspan{}), recordspan, unsafe.Pointer(h), &memstats.mspan_sys) 744 h.cachealloc.init(unsafe.Sizeof(mcache{}), nil, nil, &memstats.mcache_sys) 745 h.specialfinalizeralloc.init(unsafe.Sizeof(specialfinalizer{}), nil, nil, &memstats.other_sys) 746 h.specialprofilealloc.init(unsafe.Sizeof(specialprofile{}), nil, nil, &memstats.other_sys) 747 h.specialReachableAlloc.init(unsafe.Sizeof(specialReachable{}), nil, nil, &memstats.other_sys) 748 h.specialPinCounterAlloc.init(unsafe.Sizeof(specialPinCounter{}), nil, nil, &memstats.other_sys) 749 h.specialWeakHandleAlloc.init(unsafe.Sizeof(specialWeakHandle{}), nil, nil, &memstats.gcMiscSys) 750 h.arenaHintAlloc.init(unsafe.Sizeof(arenaHint{}), nil, nil, &memstats.other_sys) 751 752 // Don't zero mspan allocations. Background sweeping can 753 // inspect a span concurrently with allocating it, so it's 754 // important that the span's sweepgen survive across freeing 755 // and re-allocating a span to prevent background sweeping 756 // from improperly cas'ing it from 0. 757 // 758 // This is safe because mspan contains no heap pointers. 759 h.spanalloc.zero = false 760 761 // h->mapcache needs no init 762 763 for i := range h.central { 764 h.central[i].mcentral.init(spanClass(i)) 765 } 766 767 h.pages.init(&h.lock, &memstats.gcMiscSys, false) 768 } 769 770 // reclaim sweeps and reclaims at least npage pages into the heap. 771 // It is called before allocating npage pages to keep growth in check. 772 // 773 // reclaim implements the page-reclaimer half of the sweeper. 774 // 775 // h.lock must NOT be held. 776 func (h *mheap) reclaim(npage uintptr) { 777 // TODO(austin): Half of the time spent freeing spans is in 778 // locking/unlocking the heap (even with low contention). We 779 // could make the slow path here several times faster by 780 // batching heap frees. 781 782 // Bail early if there's no more reclaim work. 783 if h.reclaimIndex.Load() >= 1<<63 { 784 return 785 } 786 787 // Disable preemption so the GC can't start while we're 788 // sweeping, so we can read h.sweepArenas, and so 789 // traceGCSweepStart/Done pair on the P. 790 mp := acquirem() 791 792 trace := traceAcquire() 793 if trace.ok() { 794 trace.GCSweepStart() 795 traceRelease(trace) 796 } 797 798 arenas := h.sweepArenas 799 locked := false 800 for npage > 0 { 801 // Pull from accumulated credit first. 802 if credit := h.reclaimCredit.Load(); credit > 0 { 803 take := credit 804 if take > npage { 805 // Take only what we need. 806 take = npage 807 } 808 if h.reclaimCredit.CompareAndSwap(credit, credit-take) { 809 npage -= take 810 } 811 continue 812 } 813 814 // Claim a chunk of work. 815 idx := uintptr(h.reclaimIndex.Add(pagesPerReclaimerChunk) - pagesPerReclaimerChunk) 816 if idx/pagesPerArena >= uintptr(len(arenas)) { 817 // Page reclaiming is done. 818 h.reclaimIndex.Store(1 << 63) 819 break 820 } 821 822 if !locked { 823 // Lock the heap for reclaimChunk. 824 lock(&h.lock) 825 locked = true 826 } 827 828 // Scan this chunk. 829 nfound := h.reclaimChunk(arenas, idx, pagesPerReclaimerChunk) 830 if nfound <= npage { 831 npage -= nfound 832 } else { 833 // Put spare pages toward global credit. 834 h.reclaimCredit.Add(nfound - npage) 835 npage = 0 836 } 837 } 838 if locked { 839 unlock(&h.lock) 840 } 841 842 trace = traceAcquire() 843 if trace.ok() { 844 trace.GCSweepDone() 845 traceRelease(trace) 846 } 847 releasem(mp) 848 } 849 850 // reclaimChunk sweeps unmarked spans that start at page indexes [pageIdx, pageIdx+n). 851 // It returns the number of pages returned to the heap. 852 // 853 // h.lock must be held and the caller must be non-preemptible. Note: h.lock may be 854 // temporarily unlocked and re-locked in order to do sweeping or if tracing is 855 // enabled. 856 func (h *mheap) reclaimChunk(arenas []arenaIdx, pageIdx, n uintptr) uintptr { 857 // The heap lock must be held because this accesses the 858 // heapArena.spans arrays using potentially non-live pointers. 859 // In particular, if a span were freed and merged concurrently 860 // with this probing heapArena.spans, it would be possible to 861 // observe arbitrary, stale span pointers. 862 assertLockHeld(&h.lock) 863 864 n0 := n 865 var nFreed uintptr 866 sl := sweep.active.begin() 867 if !sl.valid { 868 return 0 869 } 870 for n > 0 { 871 ai := arenas[pageIdx/pagesPerArena] 872 ha := h.arenas[ai.l1()][ai.l2()] 873 874 // Get a chunk of the bitmap to work on. 875 arenaPage := uint(pageIdx % pagesPerArena) 876 inUse := ha.pageInUse[arenaPage/8:] 877 marked := ha.pageMarks[arenaPage/8:] 878 if uintptr(len(inUse)) > n/8 { 879 inUse = inUse[:n/8] 880 marked = marked[:n/8] 881 } 882 883 // Scan this bitmap chunk for spans that are in-use 884 // but have no marked objects on them. 885 for i := range inUse { 886 inUseUnmarked := atomic.Load8(&inUse[i]) &^ marked[i] 887 if inUseUnmarked == 0 { 888 continue 889 } 890 891 for j := uint(0); j < 8; j++ { 892 if inUseUnmarked&(1<<j) != 0 { 893 s := ha.spans[arenaPage+uint(i)*8+j] 894 if s, ok := sl.tryAcquire(s); ok { 895 npages := s.npages 896 unlock(&h.lock) 897 if s.sweep(false) { 898 nFreed += npages 899 } 900 lock(&h.lock) 901 // Reload inUse. It's possible nearby 902 // spans were freed when we dropped the 903 // lock and we don't want to get stale 904 // pointers from the spans array. 905 inUseUnmarked = atomic.Load8(&inUse[i]) &^ marked[i] 906 } 907 } 908 } 909 } 910 911 // Advance. 912 pageIdx += uintptr(len(inUse) * 8) 913 n -= uintptr(len(inUse) * 8) 914 } 915 sweep.active.end(sl) 916 trace := traceAcquire() 917 if trace.ok() { 918 unlock(&h.lock) 919 // Account for pages scanned but not reclaimed. 920 trace.GCSweepSpan((n0 - nFreed) * pageSize) 921 traceRelease(trace) 922 lock(&h.lock) 923 } 924 925 assertLockHeld(&h.lock) // Must be locked on return. 926 return nFreed 927 } 928 929 // spanAllocType represents the type of allocation to make, or 930 // the type of allocation to be freed. 931 type spanAllocType uint8 932 933 const ( 934 spanAllocHeap spanAllocType = iota // heap span 935 spanAllocStack // stack span 936 spanAllocPtrScalarBits // unrolled GC prog bitmap span 937 spanAllocWorkBuf // work buf span 938 ) 939 940 // manual returns true if the span allocation is manually managed. 941 func (s spanAllocType) manual() bool { 942 return s != spanAllocHeap 943 } 944 945 // alloc allocates a new span of npage pages from the GC'd heap. 946 // 947 // spanclass indicates the span's size class and scannability. 948 // 949 // Returns a span that has been fully initialized. span.needzero indicates 950 // whether the span has been zeroed. Note that it may not be. 951 func (h *mheap) alloc(npages uintptr, spanclass spanClass) *mspan { 952 // Don't do any operations that lock the heap on the G stack. 953 // It might trigger stack growth, and the stack growth code needs 954 // to be able to allocate heap. 955 var s *mspan 956 systemstack(func() { 957 // To prevent excessive heap growth, before allocating n pages 958 // we need to sweep and reclaim at least n pages. 959 if !isSweepDone() { 960 h.reclaim(npages) 961 } 962 s = h.allocSpan(npages, spanAllocHeap, spanclass) 963 }) 964 return s 965 } 966 967 // allocManual allocates a manually-managed span of npage pages. 968 // allocManual returns nil if allocation fails. 969 // 970 // allocManual adds the bytes used to *stat, which should be a 971 // memstats in-use field. Unlike allocations in the GC'd heap, the 972 // allocation does *not* count toward heapInUse. 973 // 974 // The memory backing the returned span may not be zeroed if 975 // span.needzero is set. 976 // 977 // allocManual must be called on the system stack because it may 978 // acquire the heap lock via allocSpan. See mheap for details. 979 // 980 // If new code is written to call allocManual, do NOT use an 981 // existing spanAllocType value and instead declare a new one. 982 // 983 //go:systemstack 984 func (h *mheap) allocManual(npages uintptr, typ spanAllocType) *mspan { 985 if !typ.manual() { 986 throw("manual span allocation called with non-manually-managed type") 987 } 988 return h.allocSpan(npages, typ, 0) 989 } 990 991 // setSpans modifies the span map so [spanOf(base), spanOf(base+npage*pageSize)) 992 // is s. 993 func (h *mheap) setSpans(base, npage uintptr, s *mspan) { 994 p := base / pageSize 995 ai := arenaIndex(base) 996 ha := h.arenas[ai.l1()][ai.l2()] 997 for n := uintptr(0); n < npage; n++ { 998 i := (p + n) % pagesPerArena 999 if i == 0 { 1000 ai = arenaIndex(base + n*pageSize) 1001 ha = h.arenas[ai.l1()][ai.l2()] 1002 } 1003 ha.spans[i] = s 1004 } 1005 } 1006 1007 // allocNeedsZero checks if the region of address space [base, base+npage*pageSize), 1008 // assumed to be allocated, needs to be zeroed, updating heap arena metadata for 1009 // future allocations. 1010 // 1011 // This must be called each time pages are allocated from the heap, even if the page 1012 // allocator can otherwise prove the memory it's allocating is already zero because 1013 // they're fresh from the operating system. It updates heapArena metadata that is 1014 // critical for future page allocations. 1015 // 1016 // There are no locking constraints on this method. 1017 func (h *mheap) allocNeedsZero(base, npage uintptr) (needZero bool) { 1018 for npage > 0 { 1019 ai := arenaIndex(base) 1020 ha := h.arenas[ai.l1()][ai.l2()] 1021 1022 zeroedBase := atomic.Loaduintptr(&ha.zeroedBase) 1023 arenaBase := base % heapArenaBytes 1024 if arenaBase < zeroedBase { 1025 // We extended into the non-zeroed part of the 1026 // arena, so this region needs to be zeroed before use. 1027 // 1028 // zeroedBase is monotonically increasing, so if we see this now then 1029 // we can be sure we need to zero this memory region. 1030 // 1031 // We still need to update zeroedBase for this arena, and 1032 // potentially more arenas. 1033 needZero = true 1034 } 1035 // We may observe arenaBase > zeroedBase if we're racing with one or more 1036 // allocations which are acquiring memory directly before us in the address 1037 // space. But, because we know no one else is acquiring *this* memory, it's 1038 // still safe to not zero. 1039 1040 // Compute how far into the arena we extend into, capped 1041 // at heapArenaBytes. 1042 arenaLimit := arenaBase + npage*pageSize 1043 if arenaLimit > heapArenaBytes { 1044 arenaLimit = heapArenaBytes 1045 } 1046 // Increase ha.zeroedBase so it's >= arenaLimit. 1047 // We may be racing with other updates. 1048 for arenaLimit > zeroedBase { 1049 if atomic.Casuintptr(&ha.zeroedBase, zeroedBase, arenaLimit) { 1050 break 1051 } 1052 zeroedBase = atomic.Loaduintptr(&ha.zeroedBase) 1053 // Double check basic conditions of zeroedBase. 1054 if zeroedBase <= arenaLimit && zeroedBase > arenaBase { 1055 // The zeroedBase moved into the space we were trying to 1056 // claim. That's very bad, and indicates someone allocated 1057 // the same region we did. 1058 throw("potentially overlapping in-use allocations detected") 1059 } 1060 } 1061 1062 // Move base forward and subtract from npage to move into 1063 // the next arena, or finish. 1064 base += arenaLimit - arenaBase 1065 npage -= (arenaLimit - arenaBase) / pageSize 1066 } 1067 return 1068 } 1069 1070 // tryAllocMSpan attempts to allocate an mspan object from 1071 // the P-local cache, but may fail. 1072 // 1073 // h.lock need not be held. 1074 // 1075 // This caller must ensure that its P won't change underneath 1076 // it during this function. Currently to ensure that we enforce 1077 // that the function is run on the system stack, because that's 1078 // the only place it is used now. In the future, this requirement 1079 // may be relaxed if its use is necessary elsewhere. 1080 // 1081 //go:systemstack 1082 func (h *mheap) tryAllocMSpan() *mspan { 1083 pp := getg().m.p.ptr() 1084 // If we don't have a p or the cache is empty, we can't do 1085 // anything here. 1086 if pp == nil || pp.mspancache.len == 0 { 1087 return nil 1088 } 1089 // Pull off the last entry in the cache. 1090 s := pp.mspancache.buf[pp.mspancache.len-1] 1091 pp.mspancache.len-- 1092 return s 1093 } 1094 1095 // allocMSpanLocked allocates an mspan object. 1096 // 1097 // h.lock must be held. 1098 // 1099 // allocMSpanLocked must be called on the system stack because 1100 // its caller holds the heap lock. See mheap for details. 1101 // Running on the system stack also ensures that we won't 1102 // switch Ps during this function. See tryAllocMSpan for details. 1103 // 1104 //go:systemstack 1105 func (h *mheap) allocMSpanLocked() *mspan { 1106 assertLockHeld(&h.lock) 1107 1108 pp := getg().m.p.ptr() 1109 if pp == nil { 1110 // We don't have a p so just do the normal thing. 1111 return (*mspan)(h.spanalloc.alloc()) 1112 } 1113 // Refill the cache if necessary. 1114 if pp.mspancache.len == 0 { 1115 const refillCount = len(pp.mspancache.buf) / 2 1116 for i := 0; i < refillCount; i++ { 1117 pp.mspancache.buf[i] = (*mspan)(h.spanalloc.alloc()) 1118 } 1119 pp.mspancache.len = refillCount 1120 } 1121 // Pull off the last entry in the cache. 1122 s := pp.mspancache.buf[pp.mspancache.len-1] 1123 pp.mspancache.len-- 1124 return s 1125 } 1126 1127 // freeMSpanLocked free an mspan object. 1128 // 1129 // h.lock must be held. 1130 // 1131 // freeMSpanLocked must be called on the system stack because 1132 // its caller holds the heap lock. See mheap for details. 1133 // Running on the system stack also ensures that we won't 1134 // switch Ps during this function. See tryAllocMSpan for details. 1135 // 1136 //go:systemstack 1137 func (h *mheap) freeMSpanLocked(s *mspan) { 1138 assertLockHeld(&h.lock) 1139 1140 pp := getg().m.p.ptr() 1141 // First try to free the mspan directly to the cache. 1142 if pp != nil && pp.mspancache.len < len(pp.mspancache.buf) { 1143 pp.mspancache.buf[pp.mspancache.len] = s 1144 pp.mspancache.len++ 1145 return 1146 } 1147 // Failing that (or if we don't have a p), just free it to 1148 // the heap. 1149 h.spanalloc.free(unsafe.Pointer(s)) 1150 } 1151 1152 // allocSpan allocates an mspan which owns npages worth of memory. 1153 // 1154 // If typ.manual() == false, allocSpan allocates a heap span of class spanclass 1155 // and updates heap accounting. If manual == true, allocSpan allocates a 1156 // manually-managed span (spanclass is ignored), and the caller is 1157 // responsible for any accounting related to its use of the span. Either 1158 // way, allocSpan will atomically add the bytes in the newly allocated 1159 // span to *sysStat. 1160 // 1161 // The returned span is fully initialized. 1162 // 1163 // h.lock must not be held. 1164 // 1165 // allocSpan must be called on the system stack both because it acquires 1166 // the heap lock and because it must block GC transitions. 1167 // 1168 //go:systemstack 1169 func (h *mheap) allocSpan(npages uintptr, typ spanAllocType, spanclass spanClass) (s *mspan) { 1170 // Function-global state. 1171 gp := getg() 1172 base, scav := uintptr(0), uintptr(0) 1173 growth := uintptr(0) 1174 1175 // On some platforms we need to provide physical page aligned stack 1176 // allocations. Where the page size is less than the physical page 1177 // size, we already manage to do this by default. 1178 needPhysPageAlign := physPageAlignedStacks && typ == spanAllocStack && pageSize < physPageSize 1179 1180 // If the allocation is small enough, try the page cache! 1181 // The page cache does not support aligned allocations, so we cannot use 1182 // it if we need to provide a physical page aligned stack allocation. 1183 pp := gp.m.p.ptr() 1184 if !needPhysPageAlign && pp != nil && npages < pageCachePages/4 { 1185 c := &pp.pcache 1186 1187 // If the cache is empty, refill it. 1188 if c.empty() { 1189 lock(&h.lock) 1190 *c = h.pages.allocToCache() 1191 unlock(&h.lock) 1192 } 1193 1194 // Try to allocate from the cache. 1195 base, scav = c.alloc(npages) 1196 if base != 0 { 1197 s = h.tryAllocMSpan() 1198 if s != nil { 1199 goto HaveSpan 1200 } 1201 // We have a base but no mspan, so we need 1202 // to lock the heap. 1203 } 1204 } 1205 1206 // For one reason or another, we couldn't get the 1207 // whole job done without the heap lock. 1208 lock(&h.lock) 1209 1210 if needPhysPageAlign { 1211 // Overallocate by a physical page to allow for later alignment. 1212 extraPages := physPageSize / pageSize 1213 1214 // Find a big enough region first, but then only allocate the 1215 // aligned portion. We can't just allocate and then free the 1216 // edges because we need to account for scavenged memory, and 1217 // that's difficult with alloc. 1218 // 1219 // Note that we skip updates to searchAddr here. It's OK if 1220 // it's stale and higher than normal; it'll operate correctly, 1221 // just come with a performance cost. 1222 base, _ = h.pages.find(npages + extraPages) 1223 if base == 0 { 1224 var ok bool 1225 growth, ok = h.grow(npages + extraPages) 1226 if !ok { 1227 unlock(&h.lock) 1228 return nil 1229 } 1230 base, _ = h.pages.find(npages + extraPages) 1231 if base == 0 { 1232 throw("grew heap, but no adequate free space found") 1233 } 1234 } 1235 base = alignUp(base, physPageSize) 1236 scav = h.pages.allocRange(base, npages) 1237 } 1238 1239 if base == 0 { 1240 // Try to acquire a base address. 1241 base, scav = h.pages.alloc(npages) 1242 if base == 0 { 1243 var ok bool 1244 growth, ok = h.grow(npages) 1245 if !ok { 1246 unlock(&h.lock) 1247 return nil 1248 } 1249 base, scav = h.pages.alloc(npages) 1250 if base == 0 { 1251 throw("grew heap, but no adequate free space found") 1252 } 1253 } 1254 } 1255 if s == nil { 1256 // We failed to get an mspan earlier, so grab 1257 // one now that we have the heap lock. 1258 s = h.allocMSpanLocked() 1259 } 1260 unlock(&h.lock) 1261 1262 HaveSpan: 1263 // Decide if we need to scavenge in response to what we just allocated. 1264 // Specifically, we track the maximum amount of memory to scavenge of all 1265 // the alternatives below, assuming that the maximum satisfies *all* 1266 // conditions we check (e.g. if we need to scavenge X to satisfy the 1267 // memory limit and Y to satisfy heap-growth scavenging, and Y > X, then 1268 // it's fine to pick Y, because the memory limit is still satisfied). 1269 // 1270 // It's fine to do this after allocating because we expect any scavenged 1271 // pages not to get touched until we return. Simultaneously, it's important 1272 // to do this before calling sysUsed because that may commit address space. 1273 bytesToScavenge := uintptr(0) 1274 forceScavenge := false 1275 if limit := gcController.memoryLimit.Load(); !gcCPULimiter.limiting() { 1276 // Assist with scavenging to maintain the memory limit by the amount 1277 // that we expect to page in. 1278 inuse := gcController.mappedReady.Load() 1279 // Be careful about overflow, especially with uintptrs. Even on 32-bit platforms 1280 // someone can set a really big memory limit that isn't maxInt64. 1281 if uint64(scav)+inuse > uint64(limit) { 1282 bytesToScavenge = uintptr(uint64(scav) + inuse - uint64(limit)) 1283 forceScavenge = true 1284 } 1285 } 1286 if goal := scavenge.gcPercentGoal.Load(); goal != ^uint64(0) && growth > 0 { 1287 // We just caused a heap growth, so scavenge down what will soon be used. 1288 // By scavenging inline we deal with the failure to allocate out of 1289 // memory fragments by scavenging the memory fragments that are least 1290 // likely to be re-used. 1291 // 1292 // Only bother with this because we're not using a memory limit. We don't 1293 // care about heap growths as long as we're under the memory limit, and the 1294 // previous check for scaving already handles that. 1295 if retained := heapRetained(); retained+uint64(growth) > goal { 1296 // The scavenging algorithm requires the heap lock to be dropped so it 1297 // can acquire it only sparingly. This is a potentially expensive operation 1298 // so it frees up other goroutines to allocate in the meanwhile. In fact, 1299 // they can make use of the growth we just created. 1300 todo := growth 1301 if overage := uintptr(retained + uint64(growth) - goal); todo > overage { 1302 todo = overage 1303 } 1304 if todo > bytesToScavenge { 1305 bytesToScavenge = todo 1306 } 1307 } 1308 } 1309 // There are a few very limited circumstances where we won't have a P here. 1310 // It's OK to simply skip scavenging in these cases. Something else will notice 1311 // and pick up the tab. 1312 var now int64 1313 if pp != nil && bytesToScavenge > 0 { 1314 // Measure how long we spent scavenging and add that measurement to the assist 1315 // time so we can track it for the GC CPU limiter. 1316 // 1317 // Limiter event tracking might be disabled if we end up here 1318 // while on a mark worker. 1319 start := nanotime() 1320 track := pp.limiterEvent.start(limiterEventScavengeAssist, start) 1321 1322 // Scavenge, but back out if the limiter turns on. 1323 released := h.pages.scavenge(bytesToScavenge, func() bool { 1324 return gcCPULimiter.limiting() 1325 }, forceScavenge) 1326 1327 mheap_.pages.scav.releasedEager.Add(released) 1328 1329 // Finish up accounting. 1330 now = nanotime() 1331 if track { 1332 pp.limiterEvent.stop(limiterEventScavengeAssist, now) 1333 } 1334 scavenge.assistTime.Add(now - start) 1335 } 1336 1337 // Initialize the span. 1338 h.initSpan(s, typ, spanclass, base, npages) 1339 1340 // Commit and account for any scavenged memory that the span now owns. 1341 nbytes := npages * pageSize 1342 if scav != 0 { 1343 // sysUsed all the pages that are actually available 1344 // in the span since some of them might be scavenged. 1345 sysUsed(unsafe.Pointer(base), nbytes, scav) 1346 gcController.heapReleased.add(-int64(scav)) 1347 } 1348 // Update stats. 1349 gcController.heapFree.add(-int64(nbytes - scav)) 1350 if typ == spanAllocHeap { 1351 gcController.heapInUse.add(int64(nbytes)) 1352 } 1353 // Update consistent stats. 1354 stats := memstats.heapStats.acquire() 1355 atomic.Xaddint64(&stats.committed, int64(scav)) 1356 atomic.Xaddint64(&stats.released, -int64(scav)) 1357 switch typ { 1358 case spanAllocHeap: 1359 atomic.Xaddint64(&stats.inHeap, int64(nbytes)) 1360 case spanAllocStack: 1361 atomic.Xaddint64(&stats.inStacks, int64(nbytes)) 1362 case spanAllocPtrScalarBits: 1363 atomic.Xaddint64(&stats.inPtrScalarBits, int64(nbytes)) 1364 case spanAllocWorkBuf: 1365 atomic.Xaddint64(&stats.inWorkBufs, int64(nbytes)) 1366 } 1367 memstats.heapStats.release() 1368 1369 pageTraceAlloc(pp, now, base, npages) 1370 return s 1371 } 1372 1373 // initSpan initializes a blank span s which will represent the range 1374 // [base, base+npages*pageSize). typ is the type of span being allocated. 1375 func (h *mheap) initSpan(s *mspan, typ spanAllocType, spanclass spanClass, base, npages uintptr) { 1376 // At this point, both s != nil and base != 0, and the heap 1377 // lock is no longer held. Initialize the span. 1378 s.init(base, npages) 1379 if h.allocNeedsZero(base, npages) { 1380 s.needzero = 1 1381 } 1382 nbytes := npages * pageSize 1383 if typ.manual() { 1384 s.manualFreeList = 0 1385 s.nelems = 0 1386 s.limit = s.base() + s.npages*pageSize 1387 s.state.set(mSpanManual) 1388 } else { 1389 // We must set span properties before the span is published anywhere 1390 // since we're not holding the heap lock. 1391 s.spanclass = spanclass 1392 if sizeclass := spanclass.sizeclass(); sizeclass == 0 { 1393 s.elemsize = nbytes 1394 s.nelems = 1 1395 s.divMul = 0 1396 } else { 1397 s.elemsize = uintptr(class_to_size[sizeclass]) 1398 if !s.spanclass.noscan() && heapBitsInSpan(s.elemsize) { 1399 // Reserve space for the pointer/scan bitmap at the end. 1400 s.nelems = uint16((nbytes - (nbytes / goarch.PtrSize / 8)) / s.elemsize) 1401 } else { 1402 s.nelems = uint16(nbytes / s.elemsize) 1403 } 1404 s.divMul = class_to_divmagic[sizeclass] 1405 } 1406 1407 // Initialize mark and allocation structures. 1408 s.freeindex = 0 1409 s.freeIndexForScan = 0 1410 s.allocCache = ^uint64(0) // all 1s indicating all free. 1411 s.gcmarkBits = newMarkBits(uintptr(s.nelems)) 1412 s.allocBits = newAllocBits(uintptr(s.nelems)) 1413 1414 // It's safe to access h.sweepgen without the heap lock because it's 1415 // only ever updated with the world stopped and we run on the 1416 // systemstack which blocks a STW transition. 1417 atomic.Store(&s.sweepgen, h.sweepgen) 1418 1419 // Now that the span is filled in, set its state. This 1420 // is a publication barrier for the other fields in 1421 // the span. While valid pointers into this span 1422 // should never be visible until the span is returned, 1423 // if the garbage collector finds an invalid pointer, 1424 // access to the span may race with initialization of 1425 // the span. We resolve this race by atomically 1426 // setting the state after the span is fully 1427 // initialized, and atomically checking the state in 1428 // any situation where a pointer is suspect. 1429 s.state.set(mSpanInUse) 1430 } 1431 1432 // Publish the span in various locations. 1433 1434 // This is safe to call without the lock held because the slots 1435 // related to this span will only ever be read or modified by 1436 // this thread until pointers into the span are published (and 1437 // we execute a publication barrier at the end of this function 1438 // before that happens) or pageInUse is updated. 1439 h.setSpans(s.base(), npages, s) 1440 1441 if !typ.manual() { 1442 // Mark in-use span in arena page bitmap. 1443 // 1444 // This publishes the span to the page sweeper, so 1445 // it's imperative that the span be completely initialized 1446 // prior to this line. 1447 arena, pageIdx, pageMask := pageIndexOf(s.base()) 1448 atomic.Or8(&arena.pageInUse[pageIdx], pageMask) 1449 1450 // Update related page sweeper stats. 1451 h.pagesInUse.Add(npages) 1452 } 1453 1454 // Make sure the newly allocated span will be observed 1455 // by the GC before pointers into the span are published. 1456 publicationBarrier() 1457 } 1458 1459 // Try to add at least npage pages of memory to the heap, 1460 // returning how much the heap grew by and whether it worked. 1461 // 1462 // h.lock must be held. 1463 func (h *mheap) grow(npage uintptr) (uintptr, bool) { 1464 assertLockHeld(&h.lock) 1465 1466 // We must grow the heap in whole palloc chunks. 1467 // We call sysMap below but note that because we 1468 // round up to pallocChunkPages which is on the order 1469 // of MiB (generally >= to the huge page size) we 1470 // won't be calling it too much. 1471 ask := alignUp(npage, pallocChunkPages) * pageSize 1472 1473 totalGrowth := uintptr(0) 1474 // This may overflow because ask could be very large 1475 // and is otherwise unrelated to h.curArena.base. 1476 end := h.curArena.base + ask 1477 nBase := alignUp(end, physPageSize) 1478 if nBase > h.curArena.end || /* overflow */ end < h.curArena.base { 1479 // Not enough room in the current arena. Allocate more 1480 // arena space. This may not be contiguous with the 1481 // current arena, so we have to request the full ask. 1482 av, asize := h.sysAlloc(ask, &h.arenaHints, true) 1483 if av == nil { 1484 inUse := gcController.heapFree.load() + gcController.heapReleased.load() + gcController.heapInUse.load() 1485 print("runtime: out of memory: cannot allocate ", ask, "-byte block (", inUse, " in use)\n") 1486 return 0, false 1487 } 1488 1489 if uintptr(av) == h.curArena.end { 1490 // The new space is contiguous with the old 1491 // space, so just extend the current space. 1492 h.curArena.end = uintptr(av) + asize 1493 } else { 1494 // The new space is discontiguous. Track what 1495 // remains of the current space and switch to 1496 // the new space. This should be rare. 1497 if size := h.curArena.end - h.curArena.base; size != 0 { 1498 // Transition this space from Reserved to Prepared and mark it 1499 // as released since we'll be able to start using it after updating 1500 // the page allocator and releasing the lock at any time. 1501 sysMap(unsafe.Pointer(h.curArena.base), size, &gcController.heapReleased) 1502 // Update stats. 1503 stats := memstats.heapStats.acquire() 1504 atomic.Xaddint64(&stats.released, int64(size)) 1505 memstats.heapStats.release() 1506 // Update the page allocator's structures to make this 1507 // space ready for allocation. 1508 h.pages.grow(h.curArena.base, size) 1509 totalGrowth += size 1510 } 1511 // Switch to the new space. 1512 h.curArena.base = uintptr(av) 1513 h.curArena.end = uintptr(av) + asize 1514 } 1515 1516 // Recalculate nBase. 1517 // We know this won't overflow, because sysAlloc returned 1518 // a valid region starting at h.curArena.base which is at 1519 // least ask bytes in size. 1520 nBase = alignUp(h.curArena.base+ask, physPageSize) 1521 } 1522 1523 // Grow into the current arena. 1524 v := h.curArena.base 1525 h.curArena.base = nBase 1526 1527 // Transition the space we're going to use from Reserved to Prepared. 1528 // 1529 // The allocation is always aligned to the heap arena 1530 // size which is always > physPageSize, so its safe to 1531 // just add directly to heapReleased. 1532 sysMap(unsafe.Pointer(v), nBase-v, &gcController.heapReleased) 1533 1534 // The memory just allocated counts as both released 1535 // and idle, even though it's not yet backed by spans. 1536 stats := memstats.heapStats.acquire() 1537 atomic.Xaddint64(&stats.released, int64(nBase-v)) 1538 memstats.heapStats.release() 1539 1540 // Update the page allocator's structures to make this 1541 // space ready for allocation. 1542 h.pages.grow(v, nBase-v) 1543 totalGrowth += nBase - v 1544 return totalGrowth, true 1545 } 1546 1547 // Free the span back into the heap. 1548 func (h *mheap) freeSpan(s *mspan) { 1549 systemstack(func() { 1550 pageTraceFree(getg().m.p.ptr(), 0, s.base(), s.npages) 1551 1552 lock(&h.lock) 1553 if msanenabled { 1554 // Tell msan that this entire span is no longer in use. 1555 base := unsafe.Pointer(s.base()) 1556 bytes := s.npages << _PageShift 1557 msanfree(base, bytes) 1558 } 1559 if asanenabled { 1560 // Tell asan that this entire span is no longer in use. 1561 base := unsafe.Pointer(s.base()) 1562 bytes := s.npages << _PageShift 1563 asanpoison(base, bytes) 1564 } 1565 h.freeSpanLocked(s, spanAllocHeap) 1566 unlock(&h.lock) 1567 }) 1568 } 1569 1570 // freeManual frees a manually-managed span returned by allocManual. 1571 // typ must be the same as the spanAllocType passed to the allocManual that 1572 // allocated s. 1573 // 1574 // This must only be called when gcphase == _GCoff. See mSpanState for 1575 // an explanation. 1576 // 1577 // freeManual must be called on the system stack because it acquires 1578 // the heap lock. See mheap for details. 1579 // 1580 //go:systemstack 1581 func (h *mheap) freeManual(s *mspan, typ spanAllocType) { 1582 pageTraceFree(getg().m.p.ptr(), 0, s.base(), s.npages) 1583 1584 s.needzero = 1 1585 lock(&h.lock) 1586 h.freeSpanLocked(s, typ) 1587 unlock(&h.lock) 1588 } 1589 1590 func (h *mheap) freeSpanLocked(s *mspan, typ spanAllocType) { 1591 assertLockHeld(&h.lock) 1592 1593 switch s.state.get() { 1594 case mSpanManual: 1595 if s.allocCount != 0 { 1596 throw("mheap.freeSpanLocked - invalid stack free") 1597 } 1598 case mSpanInUse: 1599 if s.isUserArenaChunk { 1600 throw("mheap.freeSpanLocked - invalid free of user arena chunk") 1601 } 1602 if s.allocCount != 0 || s.sweepgen != h.sweepgen { 1603 print("mheap.freeSpanLocked - span ", s, " ptr ", hex(s.base()), " allocCount ", s.allocCount, " sweepgen ", s.sweepgen, "/", h.sweepgen, "\n") 1604 throw("mheap.freeSpanLocked - invalid free") 1605 } 1606 h.pagesInUse.Add(-s.npages) 1607 1608 // Clear in-use bit in arena page bitmap. 1609 arena, pageIdx, pageMask := pageIndexOf(s.base()) 1610 atomic.And8(&arena.pageInUse[pageIdx], ^pageMask) 1611 default: 1612 throw("mheap.freeSpanLocked - invalid span state") 1613 } 1614 1615 // Update stats. 1616 // 1617 // Mirrors the code in allocSpan. 1618 nbytes := s.npages * pageSize 1619 gcController.heapFree.add(int64(nbytes)) 1620 if typ == spanAllocHeap { 1621 gcController.heapInUse.add(-int64(nbytes)) 1622 } 1623 // Update consistent stats. 1624 stats := memstats.heapStats.acquire() 1625 switch typ { 1626 case spanAllocHeap: 1627 atomic.Xaddint64(&stats.inHeap, -int64(nbytes)) 1628 case spanAllocStack: 1629 atomic.Xaddint64(&stats.inStacks, -int64(nbytes)) 1630 case spanAllocPtrScalarBits: 1631 atomic.Xaddint64(&stats.inPtrScalarBits, -int64(nbytes)) 1632 case spanAllocWorkBuf: 1633 atomic.Xaddint64(&stats.inWorkBufs, -int64(nbytes)) 1634 } 1635 memstats.heapStats.release() 1636 1637 // Mark the space as free. 1638 h.pages.free(s.base(), s.npages) 1639 1640 // Free the span structure. We no longer have a use for it. 1641 s.state.set(mSpanDead) 1642 h.freeMSpanLocked(s) 1643 } 1644 1645 // scavengeAll acquires the heap lock (blocking any additional 1646 // manipulation of the page allocator) and iterates over the whole 1647 // heap, scavenging every free page available. 1648 // 1649 // Must run on the system stack because it acquires the heap lock. 1650 // 1651 //go:systemstack 1652 func (h *mheap) scavengeAll() { 1653 // Disallow malloc or panic while holding the heap lock. We do 1654 // this here because this is a non-mallocgc entry-point to 1655 // the mheap API. 1656 gp := getg() 1657 gp.m.mallocing++ 1658 1659 // Force scavenge everything. 1660 released := h.pages.scavenge(^uintptr(0), nil, true) 1661 1662 gp.m.mallocing-- 1663 1664 if debug.scavtrace > 0 { 1665 printScavTrace(0, released, true) 1666 } 1667 } 1668 1669 //go:linkname runtime_debug_freeOSMemory runtime/debug.freeOSMemory 1670 func runtime_debug_freeOSMemory() { 1671 GC() 1672 systemstack(func() { mheap_.scavengeAll() }) 1673 } 1674 1675 // Initialize a new span with the given start and npages. 1676 func (span *mspan) init(base uintptr, npages uintptr) { 1677 // span is *not* zeroed. 1678 span.next = nil 1679 span.prev = nil 1680 span.list = nil 1681 span.startAddr = base 1682 span.npages = npages 1683 span.allocCount = 0 1684 span.spanclass = 0 1685 span.elemsize = 0 1686 span.speciallock.key = 0 1687 span.specials = nil 1688 span.needzero = 0 1689 span.freeindex = 0 1690 span.freeIndexForScan = 0 1691 span.allocBits = nil 1692 span.gcmarkBits = nil 1693 span.pinnerBits = nil 1694 span.state.set(mSpanDead) 1695 lockInit(&span.speciallock, lockRankMspanSpecial) 1696 } 1697 1698 func (span *mspan) inList() bool { 1699 return span.list != nil 1700 } 1701 1702 // Initialize an empty doubly-linked list. 1703 func (list *mSpanList) init() { 1704 list.first = nil 1705 list.last = nil 1706 } 1707 1708 func (list *mSpanList) remove(span *mspan) { 1709 if span.list != list { 1710 print("runtime: failed mSpanList.remove span.npages=", span.npages, 1711 " span=", span, " prev=", span.prev, " span.list=", span.list, " list=", list, "\n") 1712 throw("mSpanList.remove") 1713 } 1714 if list.first == span { 1715 list.first = span.next 1716 } else { 1717 span.prev.next = span.next 1718 } 1719 if list.last == span { 1720 list.last = span.prev 1721 } else { 1722 span.next.prev = span.prev 1723 } 1724 span.next = nil 1725 span.prev = nil 1726 span.list = nil 1727 } 1728 1729 func (list *mSpanList) isEmpty() bool { 1730 return list.first == nil 1731 } 1732 1733 func (list *mSpanList) insert(span *mspan) { 1734 if span.next != nil || span.prev != nil || span.list != nil { 1735 println("runtime: failed mSpanList.insert", span, span.next, span.prev, span.list) 1736 throw("mSpanList.insert") 1737 } 1738 span.next = list.first 1739 if list.first != nil { 1740 // The list contains at least one span; link it in. 1741 // The last span in the list doesn't change. 1742 list.first.prev = span 1743 } else { 1744 // The list contains no spans, so this is also the last span. 1745 list.last = span 1746 } 1747 list.first = span 1748 span.list = list 1749 } 1750 1751 func (list *mSpanList) insertBack(span *mspan) { 1752 if span.next != nil || span.prev != nil || span.list != nil { 1753 println("runtime: failed mSpanList.insertBack", span, span.next, span.prev, span.list) 1754 throw("mSpanList.insertBack") 1755 } 1756 span.prev = list.last 1757 if list.last != nil { 1758 // The list contains at least one span. 1759 list.last.next = span 1760 } else { 1761 // The list contains no spans, so this is also the first span. 1762 list.first = span 1763 } 1764 list.last = span 1765 span.list = list 1766 } 1767 1768 // takeAll removes all spans from other and inserts them at the front 1769 // of list. 1770 func (list *mSpanList) takeAll(other *mSpanList) { 1771 if other.isEmpty() { 1772 return 1773 } 1774 1775 // Reparent everything in other to list. 1776 for s := other.first; s != nil; s = s.next { 1777 s.list = list 1778 } 1779 1780 // Concatenate the lists. 1781 if list.isEmpty() { 1782 *list = *other 1783 } else { 1784 // Neither list is empty. Put other before list. 1785 other.last.next = list.first 1786 list.first.prev = other.last 1787 list.first = other.first 1788 } 1789 1790 other.first, other.last = nil, nil 1791 } 1792 1793 const ( 1794 // _KindSpecialFinalizer is for tracking finalizers. 1795 _KindSpecialFinalizer = 1 1796 // _KindSpecialWeakHandle is used for creating weak pointers. 1797 _KindSpecialWeakHandle = 2 1798 // _KindSpecialProfile is for memory profiling. 1799 _KindSpecialProfile = 3 1800 // _KindSpecialReachable is a special used for tracking 1801 // reachability during testing. 1802 _KindSpecialReachable = 4 1803 // _KindSpecialPinCounter is a special used for objects that are pinned 1804 // multiple times 1805 _KindSpecialPinCounter = 5 1806 ) 1807 1808 type special struct { 1809 _ sys.NotInHeap 1810 next *special // linked list in span 1811 offset uint16 // span offset of object 1812 kind byte // kind of special 1813 } 1814 1815 // spanHasSpecials marks a span as having specials in the arena bitmap. 1816 func spanHasSpecials(s *mspan) { 1817 arenaPage := (s.base() / pageSize) % pagesPerArena 1818 ai := arenaIndex(s.base()) 1819 ha := mheap_.arenas[ai.l1()][ai.l2()] 1820 atomic.Or8(&ha.pageSpecials[arenaPage/8], uint8(1)<<(arenaPage%8)) 1821 } 1822 1823 // spanHasNoSpecials marks a span as having no specials in the arena bitmap. 1824 func spanHasNoSpecials(s *mspan) { 1825 arenaPage := (s.base() / pageSize) % pagesPerArena 1826 ai := arenaIndex(s.base()) 1827 ha := mheap_.arenas[ai.l1()][ai.l2()] 1828 atomic.And8(&ha.pageSpecials[arenaPage/8], ^(uint8(1) << (arenaPage % 8))) 1829 } 1830 1831 // Adds the special record s to the list of special records for 1832 // the object p. All fields of s should be filled in except for 1833 // offset & next, which this routine will fill in. 1834 // Returns true if the special was successfully added, false otherwise. 1835 // (The add will fail only if a record with the same p and s->kind 1836 // already exists.) 1837 func addspecial(p unsafe.Pointer, s *special) bool { 1838 span := spanOfHeap(uintptr(p)) 1839 if span == nil { 1840 throw("addspecial on invalid pointer") 1841 } 1842 1843 // Ensure that the span is swept. 1844 // Sweeping accesses the specials list w/o locks, so we have 1845 // to synchronize with it. And it's just much safer. 1846 mp := acquirem() 1847 span.ensureSwept() 1848 1849 offset := uintptr(p) - span.base() 1850 kind := s.kind 1851 1852 lock(&span.speciallock) 1853 1854 // Find splice point, check for existing record. 1855 iter, exists := span.specialFindSplicePoint(offset, kind) 1856 if !exists { 1857 // Splice in record, fill in offset. 1858 s.offset = uint16(offset) 1859 s.next = *iter 1860 *iter = s 1861 spanHasSpecials(span) 1862 } 1863 1864 unlock(&span.speciallock) 1865 releasem(mp) 1866 return !exists // already exists 1867 } 1868 1869 // Removes the Special record of the given kind for the object p. 1870 // Returns the record if the record existed, nil otherwise. 1871 // The caller must FixAlloc_Free the result. 1872 func removespecial(p unsafe.Pointer, kind uint8) *special { 1873 span := spanOfHeap(uintptr(p)) 1874 if span == nil { 1875 throw("removespecial on invalid pointer") 1876 } 1877 1878 // Ensure that the span is swept. 1879 // Sweeping accesses the specials list w/o locks, so we have 1880 // to synchronize with it. And it's just much safer. 1881 mp := acquirem() 1882 span.ensureSwept() 1883 1884 offset := uintptr(p) - span.base() 1885 1886 var result *special 1887 lock(&span.speciallock) 1888 1889 iter, exists := span.specialFindSplicePoint(offset, kind) 1890 if exists { 1891 s := *iter 1892 *iter = s.next 1893 result = s 1894 } 1895 if span.specials == nil { 1896 spanHasNoSpecials(span) 1897 } 1898 unlock(&span.speciallock) 1899 releasem(mp) 1900 return result 1901 } 1902 1903 // Find a splice point in the sorted list and check for an already existing 1904 // record. Returns a pointer to the next-reference in the list predecessor. 1905 // Returns true, if the referenced item is an exact match. 1906 func (span *mspan) specialFindSplicePoint(offset uintptr, kind byte) (**special, bool) { 1907 // Find splice point, check for existing record. 1908 iter := &span.specials 1909 found := false 1910 for { 1911 s := *iter 1912 if s == nil { 1913 break 1914 } 1915 if offset == uintptr(s.offset) && kind == s.kind { 1916 found = true 1917 break 1918 } 1919 if offset < uintptr(s.offset) || (offset == uintptr(s.offset) && kind < s.kind) { 1920 break 1921 } 1922 iter = &s.next 1923 } 1924 return iter, found 1925 } 1926 1927 // The described object has a finalizer set for it. 1928 // 1929 // specialfinalizer is allocated from non-GC'd memory, so any heap 1930 // pointers must be specially handled. 1931 type specialfinalizer struct { 1932 _ sys.NotInHeap 1933 special special 1934 fn *funcval // May be a heap pointer. 1935 nret uintptr 1936 fint *_type // May be a heap pointer, but always live. 1937 ot *ptrtype // May be a heap pointer, but always live. 1938 } 1939 1940 // Adds a finalizer to the object p. Returns true if it succeeded. 1941 func addfinalizer(p unsafe.Pointer, f *funcval, nret uintptr, fint *_type, ot *ptrtype) bool { 1942 lock(&mheap_.speciallock) 1943 s := (*specialfinalizer)(mheap_.specialfinalizeralloc.alloc()) 1944 unlock(&mheap_.speciallock) 1945 s.special.kind = _KindSpecialFinalizer 1946 s.fn = f 1947 s.nret = nret 1948 s.fint = fint 1949 s.ot = ot 1950 if addspecial(p, &s.special) { 1951 // This is responsible for maintaining the same 1952 // GC-related invariants as markrootSpans in any 1953 // situation where it's possible that markrootSpans 1954 // has already run but mark termination hasn't yet. 1955 if gcphase != _GCoff { 1956 base, span, _ := findObject(uintptr(p), 0, 0) 1957 mp := acquirem() 1958 gcw := &mp.p.ptr().gcw 1959 // Mark everything reachable from the object 1960 // so it's retained for the finalizer. 1961 if !span.spanclass.noscan() { 1962 scanobject(base, gcw) 1963 } 1964 // Mark the finalizer itself, since the 1965 // special isn't part of the GC'd heap. 1966 scanblock(uintptr(unsafe.Pointer(&s.fn)), goarch.PtrSize, &oneptrmask[0], gcw, nil) 1967 releasem(mp) 1968 } 1969 return true 1970 } 1971 1972 // There was an old finalizer 1973 lock(&mheap_.speciallock) 1974 mheap_.specialfinalizeralloc.free(unsafe.Pointer(s)) 1975 unlock(&mheap_.speciallock) 1976 return false 1977 } 1978 1979 // Removes the finalizer (if any) from the object p. 1980 func removefinalizer(p unsafe.Pointer) { 1981 s := (*specialfinalizer)(unsafe.Pointer(removespecial(p, _KindSpecialFinalizer))) 1982 if s == nil { 1983 return // there wasn't a finalizer to remove 1984 } 1985 lock(&mheap_.speciallock) 1986 mheap_.specialfinalizeralloc.free(unsafe.Pointer(s)) 1987 unlock(&mheap_.speciallock) 1988 } 1989 1990 // The described object has a weak pointer. 1991 // 1992 // Weak pointers in the GC have the following invariants: 1993 // 1994 // - Strong-to-weak conversions must ensure the strong pointer 1995 // remains live until the weak handle is installed. This ensures 1996 // that creating a weak pointer cannot fail. 1997 // 1998 // - Weak-to-strong conversions require the weakly-referenced 1999 // object to be swept before the conversion may proceed. This 2000 // ensures that weak-to-strong conversions cannot resurrect 2001 // dead objects by sweeping them before that happens. 2002 // 2003 // - Weak handles are unique and canonical for each byte offset into 2004 // an object that a strong pointer may point to, until an object 2005 // becomes unreachable. 2006 // 2007 // - Weak handles contain nil as soon as an object becomes unreachable 2008 // the first time, before a finalizer makes it reachable again. New 2009 // weak handles created after resurrection are newly unique. 2010 // 2011 // specialWeakHandle is allocated from non-GC'd memory, so any heap 2012 // pointers must be specially handled. 2013 type specialWeakHandle struct { 2014 _ sys.NotInHeap 2015 special special 2016 // handle is a reference to the actual weak pointer. 2017 // It is always heap-allocated and must be explicitly kept 2018 // live so long as this special exists. 2019 handle *atomic.Uintptr 2020 } 2021 2022 //go:linkname internal_weak_runtime_registerWeakPointer internal/weak.runtime_registerWeakPointer 2023 func internal_weak_runtime_registerWeakPointer(p unsafe.Pointer) unsafe.Pointer { 2024 return unsafe.Pointer(getOrAddWeakHandle(unsafe.Pointer(p))) 2025 } 2026 2027 //go:linkname internal_weak_runtime_makeStrongFromWeak internal/weak.runtime_makeStrongFromWeak 2028 func internal_weak_runtime_makeStrongFromWeak(u unsafe.Pointer) unsafe.Pointer { 2029 handle := (*atomic.Uintptr)(u) 2030 2031 // Prevent preemption. We want to make sure that another GC cycle can't start. 2032 mp := acquirem() 2033 p := handle.Load() 2034 if p == 0 { 2035 releasem(mp) 2036 return nil 2037 } 2038 // Be careful. p may or may not refer to valid memory anymore, as it could've been 2039 // swept and released already. It's always safe to ensure a span is swept, though, 2040 // even if it's just some random span. 2041 span := spanOfHeap(p) 2042 if span == nil { 2043 // The span probably got swept and released. 2044 releasem(mp) 2045 return nil 2046 } 2047 // Ensure the span is swept. 2048 span.ensureSwept() 2049 2050 // Now we can trust whatever we get from handle, so make a strong pointer. 2051 // 2052 // Even if we just swept some random span that doesn't contain this object, because 2053 // this object is long dead and its memory has since been reused, we'll just observe nil. 2054 ptr := unsafe.Pointer(handle.Load()) 2055 releasem(mp) 2056 return ptr 2057 } 2058 2059 // Retrieves or creates a weak pointer handle for the object p. 2060 func getOrAddWeakHandle(p unsafe.Pointer) *atomic.Uintptr { 2061 // First try to retrieve without allocating. 2062 if handle := getWeakHandle(p); handle != nil { 2063 return handle 2064 } 2065 2066 lock(&mheap_.speciallock) 2067 s := (*specialWeakHandle)(mheap_.specialWeakHandleAlloc.alloc()) 2068 unlock(&mheap_.speciallock) 2069 2070 handle := new(atomic.Uintptr) 2071 s.special.kind = _KindSpecialWeakHandle 2072 s.handle = handle 2073 handle.Store(uintptr(p)) 2074 if addspecial(p, &s.special) { 2075 // This is responsible for maintaining the same 2076 // GC-related invariants as markrootSpans in any 2077 // situation where it's possible that markrootSpans 2078 // has already run but mark termination hasn't yet. 2079 if gcphase != _GCoff { 2080 mp := acquirem() 2081 gcw := &mp.p.ptr().gcw 2082 // Mark the weak handle itself, since the 2083 // special isn't part of the GC'd heap. 2084 scanblock(uintptr(unsafe.Pointer(&s.handle)), goarch.PtrSize, &oneptrmask[0], gcw, nil) 2085 releasem(mp) 2086 } 2087 return s.handle 2088 } 2089 2090 // There was an existing handle. Free the special 2091 // and try again. We must succeed because we're explicitly 2092 // keeping p live until the end of this function. Either 2093 // we, or someone else, must have succeeded, because we can 2094 // only fail in the event of a race, and p will still be 2095 // be valid no matter how much time we spend here. 2096 lock(&mheap_.speciallock) 2097 mheap_.specialWeakHandleAlloc.free(unsafe.Pointer(s)) 2098 unlock(&mheap_.speciallock) 2099 2100 handle = getWeakHandle(p) 2101 if handle == nil { 2102 throw("failed to get or create weak handle") 2103 } 2104 2105 // Keep p alive for the duration of the function to ensure 2106 // that it cannot die while we're trying to this. 2107 KeepAlive(p) 2108 return handle 2109 } 2110 2111 func getWeakHandle(p unsafe.Pointer) *atomic.Uintptr { 2112 span := spanOfHeap(uintptr(p)) 2113 if span == nil { 2114 throw("getWeakHandle on invalid pointer") 2115 } 2116 2117 // Ensure that the span is swept. 2118 // Sweeping accesses the specials list w/o locks, so we have 2119 // to synchronize with it. And it's just much safer. 2120 mp := acquirem() 2121 span.ensureSwept() 2122 2123 offset := uintptr(p) - span.base() 2124 2125 lock(&span.speciallock) 2126 2127 // Find the existing record and return the handle if one exists. 2128 var handle *atomic.Uintptr 2129 iter, exists := span.specialFindSplicePoint(offset, _KindSpecialWeakHandle) 2130 if exists { 2131 handle = ((*specialWeakHandle)(unsafe.Pointer(*iter))).handle 2132 } 2133 unlock(&span.speciallock) 2134 releasem(mp) 2135 2136 return handle 2137 } 2138 2139 // The described object is being heap profiled. 2140 type specialprofile struct { 2141 _ sys.NotInHeap 2142 special special 2143 b *bucket 2144 } 2145 2146 // Set the heap profile bucket associated with addr to b. 2147 func setprofilebucket(p unsafe.Pointer, b *bucket) { 2148 lock(&mheap_.speciallock) 2149 s := (*specialprofile)(mheap_.specialprofilealloc.alloc()) 2150 unlock(&mheap_.speciallock) 2151 s.special.kind = _KindSpecialProfile 2152 s.b = b 2153 if !addspecial(p, &s.special) { 2154 throw("setprofilebucket: profile already set") 2155 } 2156 } 2157 2158 // specialReachable tracks whether an object is reachable on the next 2159 // GC cycle. This is used by testing. 2160 type specialReachable struct { 2161 special special 2162 done bool 2163 reachable bool 2164 } 2165 2166 // specialPinCounter tracks whether an object is pinned multiple times. 2167 type specialPinCounter struct { 2168 special special 2169 counter uintptr 2170 } 2171 2172 // specialsIter helps iterate over specials lists. 2173 type specialsIter struct { 2174 pprev **special 2175 s *special 2176 } 2177 2178 func newSpecialsIter(span *mspan) specialsIter { 2179 return specialsIter{&span.specials, span.specials} 2180 } 2181 2182 func (i *specialsIter) valid() bool { 2183 return i.s != nil 2184 } 2185 2186 func (i *specialsIter) next() { 2187 i.pprev = &i.s.next 2188 i.s = *i.pprev 2189 } 2190 2191 // unlinkAndNext removes the current special from the list and moves 2192 // the iterator to the next special. It returns the unlinked special. 2193 func (i *specialsIter) unlinkAndNext() *special { 2194 cur := i.s 2195 i.s = cur.next 2196 *i.pprev = i.s 2197 return cur 2198 } 2199 2200 // freeSpecial performs any cleanup on special s and deallocates it. 2201 // s must already be unlinked from the specials list. 2202 func freeSpecial(s *special, p unsafe.Pointer, size uintptr) { 2203 switch s.kind { 2204 case _KindSpecialFinalizer: 2205 sf := (*specialfinalizer)(unsafe.Pointer(s)) 2206 queuefinalizer(p, sf.fn, sf.nret, sf.fint, sf.ot) 2207 lock(&mheap_.speciallock) 2208 mheap_.specialfinalizeralloc.free(unsafe.Pointer(sf)) 2209 unlock(&mheap_.speciallock) 2210 case _KindSpecialWeakHandle: 2211 sw := (*specialWeakHandle)(unsafe.Pointer(s)) 2212 sw.handle.Store(0) 2213 lock(&mheap_.speciallock) 2214 mheap_.specialWeakHandleAlloc.free(unsafe.Pointer(s)) 2215 unlock(&mheap_.speciallock) 2216 case _KindSpecialProfile: 2217 sp := (*specialprofile)(unsafe.Pointer(s)) 2218 mProf_Free(sp.b, size) 2219 lock(&mheap_.speciallock) 2220 mheap_.specialprofilealloc.free(unsafe.Pointer(sp)) 2221 unlock(&mheap_.speciallock) 2222 case _KindSpecialReachable: 2223 sp := (*specialReachable)(unsafe.Pointer(s)) 2224 sp.done = true 2225 // The creator frees these. 2226 case _KindSpecialPinCounter: 2227 lock(&mheap_.speciallock) 2228 mheap_.specialPinCounterAlloc.free(unsafe.Pointer(s)) 2229 unlock(&mheap_.speciallock) 2230 default: 2231 throw("bad special kind") 2232 panic("not reached") 2233 } 2234 } 2235 2236 // gcBits is an alloc/mark bitmap. This is always used as gcBits.x. 2237 type gcBits struct { 2238 _ sys.NotInHeap 2239 x uint8 2240 } 2241 2242 // bytep returns a pointer to the n'th byte of b. 2243 func (b *gcBits) bytep(n uintptr) *uint8 { 2244 return addb(&b.x, n) 2245 } 2246 2247 // bitp returns a pointer to the byte containing bit n and a mask for 2248 // selecting that bit from *bytep. 2249 func (b *gcBits) bitp(n uintptr) (bytep *uint8, mask uint8) { 2250 return b.bytep(n / 8), 1 << (n % 8) 2251 } 2252 2253 const gcBitsChunkBytes = uintptr(64 << 10) 2254 const gcBitsHeaderBytes = unsafe.Sizeof(gcBitsHeader{}) 2255 2256 type gcBitsHeader struct { 2257 free uintptr // free is the index into bits of the next free byte. 2258 next uintptr // *gcBits triggers recursive type bug. (issue 14620) 2259 } 2260 2261 type gcBitsArena struct { 2262 _ sys.NotInHeap 2263 // gcBitsHeader // side step recursive type bug (issue 14620) by including fields by hand. 2264 free uintptr // free is the index into bits of the next free byte; read/write atomically 2265 next *gcBitsArena 2266 bits [gcBitsChunkBytes - gcBitsHeaderBytes]gcBits 2267 } 2268 2269 var gcBitsArenas struct { 2270 lock mutex 2271 free *gcBitsArena 2272 next *gcBitsArena // Read atomically. Write atomically under lock. 2273 current *gcBitsArena 2274 previous *gcBitsArena 2275 } 2276 2277 // tryAlloc allocates from b or returns nil if b does not have enough room. 2278 // This is safe to call concurrently. 2279 func (b *gcBitsArena) tryAlloc(bytes uintptr) *gcBits { 2280 if b == nil || atomic.Loaduintptr(&b.free)+bytes > uintptr(len(b.bits)) { 2281 return nil 2282 } 2283 // Try to allocate from this block. 2284 end := atomic.Xadduintptr(&b.free, bytes) 2285 if end > uintptr(len(b.bits)) { 2286 return nil 2287 } 2288 // There was enough room. 2289 start := end - bytes 2290 return &b.bits[start] 2291 } 2292 2293 // newMarkBits returns a pointer to 8 byte aligned bytes 2294 // to be used for a span's mark bits. 2295 func newMarkBits(nelems uintptr) *gcBits { 2296 blocksNeeded := (nelems + 63) / 64 2297 bytesNeeded := blocksNeeded * 8 2298 2299 // Try directly allocating from the current head arena. 2300 head := (*gcBitsArena)(atomic.Loadp(unsafe.Pointer(&gcBitsArenas.next))) 2301 if p := head.tryAlloc(bytesNeeded); p != nil { 2302 return p 2303 } 2304 2305 // There's not enough room in the head arena. We may need to 2306 // allocate a new arena. 2307 lock(&gcBitsArenas.lock) 2308 // Try the head arena again, since it may have changed. Now 2309 // that we hold the lock, the list head can't change, but its 2310 // free position still can. 2311 if p := gcBitsArenas.next.tryAlloc(bytesNeeded); p != nil { 2312 unlock(&gcBitsArenas.lock) 2313 return p 2314 } 2315 2316 // Allocate a new arena. This may temporarily drop the lock. 2317 fresh := newArenaMayUnlock() 2318 // If newArenaMayUnlock dropped the lock, another thread may 2319 // have put a fresh arena on the "next" list. Try allocating 2320 // from next again. 2321 if p := gcBitsArenas.next.tryAlloc(bytesNeeded); p != nil { 2322 // Put fresh back on the free list. 2323 // TODO: Mark it "already zeroed" 2324 fresh.next = gcBitsArenas.free 2325 gcBitsArenas.free = fresh 2326 unlock(&gcBitsArenas.lock) 2327 return p 2328 } 2329 2330 // Allocate from the fresh arena. We haven't linked it in yet, so 2331 // this cannot race and is guaranteed to succeed. 2332 p := fresh.tryAlloc(bytesNeeded) 2333 if p == nil { 2334 throw("markBits overflow") 2335 } 2336 2337 // Add the fresh arena to the "next" list. 2338 fresh.next = gcBitsArenas.next 2339 atomic.StorepNoWB(unsafe.Pointer(&gcBitsArenas.next), unsafe.Pointer(fresh)) 2340 2341 unlock(&gcBitsArenas.lock) 2342 return p 2343 } 2344 2345 // newAllocBits returns a pointer to 8 byte aligned bytes 2346 // to be used for this span's alloc bits. 2347 // newAllocBits is used to provide newly initialized spans 2348 // allocation bits. For spans not being initialized the 2349 // mark bits are repurposed as allocation bits when 2350 // the span is swept. 2351 func newAllocBits(nelems uintptr) *gcBits { 2352 return newMarkBits(nelems) 2353 } 2354 2355 // nextMarkBitArenaEpoch establishes a new epoch for the arenas 2356 // holding the mark bits. The arenas are named relative to the 2357 // current GC cycle which is demarcated by the call to finishweep_m. 2358 // 2359 // All current spans have been swept. 2360 // During that sweep each span allocated room for its gcmarkBits in 2361 // gcBitsArenas.next block. gcBitsArenas.next becomes the gcBitsArenas.current 2362 // where the GC will mark objects and after each span is swept these bits 2363 // will be used to allocate objects. 2364 // gcBitsArenas.current becomes gcBitsArenas.previous where the span's 2365 // gcAllocBits live until all the spans have been swept during this GC cycle. 2366 // The span's sweep extinguishes all the references to gcBitsArenas.previous 2367 // by pointing gcAllocBits into the gcBitsArenas.current. 2368 // The gcBitsArenas.previous is released to the gcBitsArenas.free list. 2369 func nextMarkBitArenaEpoch() { 2370 lock(&gcBitsArenas.lock) 2371 if gcBitsArenas.previous != nil { 2372 if gcBitsArenas.free == nil { 2373 gcBitsArenas.free = gcBitsArenas.previous 2374 } else { 2375 // Find end of previous arenas. 2376 last := gcBitsArenas.previous 2377 for last = gcBitsArenas.previous; last.next != nil; last = last.next { 2378 } 2379 last.next = gcBitsArenas.free 2380 gcBitsArenas.free = gcBitsArenas.previous 2381 } 2382 } 2383 gcBitsArenas.previous = gcBitsArenas.current 2384 gcBitsArenas.current = gcBitsArenas.next 2385 atomic.StorepNoWB(unsafe.Pointer(&gcBitsArenas.next), nil) // newMarkBits calls newArena when needed 2386 unlock(&gcBitsArenas.lock) 2387 } 2388 2389 // newArenaMayUnlock allocates and zeroes a gcBits arena. 2390 // The caller must hold gcBitsArena.lock. This may temporarily release it. 2391 func newArenaMayUnlock() *gcBitsArena { 2392 var result *gcBitsArena 2393 if gcBitsArenas.free == nil { 2394 unlock(&gcBitsArenas.lock) 2395 result = (*gcBitsArena)(sysAlloc(gcBitsChunkBytes, &memstats.gcMiscSys)) 2396 if result == nil { 2397 throw("runtime: cannot allocate memory") 2398 } 2399 lock(&gcBitsArenas.lock) 2400 } else { 2401 result = gcBitsArenas.free 2402 gcBitsArenas.free = gcBitsArenas.free.next 2403 memclrNoHeapPointers(unsafe.Pointer(result), gcBitsChunkBytes) 2404 } 2405 result.next = nil 2406 // If result.bits is not 8 byte aligned adjust index so 2407 // that &result.bits[result.free] is 8 byte aligned. 2408 if unsafe.Offsetof(gcBitsArena{}.bits)&7 == 0 { 2409 result.free = 0 2410 } else { 2411 result.free = 8 - (uintptr(unsafe.Pointer(&result.bits[0])) & 7) 2412 } 2413 return result 2414 } 2415