]> Cypherpunks.ru repositories - gostls13.git/blob - src/runtime/mgcmark.go
runtime: replace all callback uses of gentraceback with unwinder
[gostls13.git] / src / runtime / mgcmark.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 // Garbage collector: marking and scanning
6
7 package runtime
8
9 import (
10         "internal/goarch"
11         "runtime/internal/atomic"
12         "runtime/internal/sys"
13         "unsafe"
14 )
15
16 const (
17         fixedRootFinalizers = iota
18         fixedRootFreeGStacks
19         fixedRootCount
20
21         // rootBlockBytes is the number of bytes to scan per data or
22         // BSS root.
23         rootBlockBytes = 256 << 10
24
25         // maxObletBytes is the maximum bytes of an object to scan at
26         // once. Larger objects will be split up into "oblets" of at
27         // most this size. Since we can scan 1–2 MB/ms, 128 KB bounds
28         // scan preemption at ~100 µs.
29         //
30         // This must be > _MaxSmallSize so that the object base is the
31         // span base.
32         maxObletBytes = 128 << 10
33
34         // drainCheckThreshold specifies how many units of work to do
35         // between self-preemption checks in gcDrain. Assuming a scan
36         // rate of 1 MB/ms, this is ~100 µs. Lower values have higher
37         // overhead in the scan loop (the scheduler check may perform
38         // a syscall, so its overhead is nontrivial). Higher values
39         // make the system less responsive to incoming work.
40         drainCheckThreshold = 100000
41
42         // pagesPerSpanRoot indicates how many pages to scan from a span root
43         // at a time. Used by special root marking.
44         //
45         // Higher values improve throughput by increasing locality, but
46         // increase the minimum latency of a marking operation.
47         //
48         // Must be a multiple of the pageInUse bitmap element size and
49         // must also evenly divide pagesPerArena.
50         pagesPerSpanRoot = 512
51 )
52
53 // gcMarkRootPrepare queues root scanning jobs (stacks, globals, and
54 // some miscellany) and initializes scanning-related state.
55 //
56 // The world must be stopped.
57 func gcMarkRootPrepare() {
58         assertWorldStopped()
59
60         // Compute how many data and BSS root blocks there are.
61         nBlocks := func(bytes uintptr) int {
62                 return int(divRoundUp(bytes, rootBlockBytes))
63         }
64
65         work.nDataRoots = 0
66         work.nBSSRoots = 0
67
68         // Scan globals.
69         for _, datap := range activeModules() {
70                 nDataRoots := nBlocks(datap.edata - datap.data)
71                 if nDataRoots > work.nDataRoots {
72                         work.nDataRoots = nDataRoots
73                 }
74         }
75
76         for _, datap := range activeModules() {
77                 nBSSRoots := nBlocks(datap.ebss - datap.bss)
78                 if nBSSRoots > work.nBSSRoots {
79                         work.nBSSRoots = nBSSRoots
80                 }
81         }
82
83         // Scan span roots for finalizer specials.
84         //
85         // We depend on addfinalizer to mark objects that get
86         // finalizers after root marking.
87         //
88         // We're going to scan the whole heap (that was available at the time the
89         // mark phase started, i.e. markArenas) for in-use spans which have specials.
90         //
91         // Break up the work into arenas, and further into chunks.
92         //
93         // Snapshot allArenas as markArenas. This snapshot is safe because allArenas
94         // is append-only.
95         mheap_.markArenas = mheap_.allArenas[:len(mheap_.allArenas):len(mheap_.allArenas)]
96         work.nSpanRoots = len(mheap_.markArenas) * (pagesPerArena / pagesPerSpanRoot)
97
98         // Scan stacks.
99         //
100         // Gs may be created after this point, but it's okay that we
101         // ignore them because they begin life without any roots, so
102         // there's nothing to scan, and any roots they create during
103         // the concurrent phase will be caught by the write barrier.
104         work.stackRoots = allGsSnapshot()
105         work.nStackRoots = len(work.stackRoots)
106
107         work.markrootNext = 0
108         work.markrootJobs = uint32(fixedRootCount + work.nDataRoots + work.nBSSRoots + work.nSpanRoots + work.nStackRoots)
109
110         // Calculate base indexes of each root type
111         work.baseData = uint32(fixedRootCount)
112         work.baseBSS = work.baseData + uint32(work.nDataRoots)
113         work.baseSpans = work.baseBSS + uint32(work.nBSSRoots)
114         work.baseStacks = work.baseSpans + uint32(work.nSpanRoots)
115         work.baseEnd = work.baseStacks + uint32(work.nStackRoots)
116 }
117
118 // gcMarkRootCheck checks that all roots have been scanned. It is
119 // purely for debugging.
120 func gcMarkRootCheck() {
121         if work.markrootNext < work.markrootJobs {
122                 print(work.markrootNext, " of ", work.markrootJobs, " markroot jobs done\n")
123                 throw("left over markroot jobs")
124         }
125
126         // Check that stacks have been scanned.
127         //
128         // We only check the first nStackRoots Gs that we should have scanned.
129         // Since we don't care about newer Gs (see comment in
130         // gcMarkRootPrepare), no locking is required.
131         i := 0
132         forEachGRace(func(gp *g) {
133                 if i >= work.nStackRoots {
134                         return
135                 }
136
137                 if !gp.gcscandone {
138                         println("gp", gp, "goid", gp.goid,
139                                 "status", readgstatus(gp),
140                                 "gcscandone", gp.gcscandone)
141                         throw("scan missed a g")
142                 }
143
144                 i++
145         })
146 }
147
148 // ptrmask for an allocation containing a single pointer.
149 var oneptrmask = [...]uint8{1}
150
151 // markroot scans the i'th root.
152 //
153 // Preemption must be disabled (because this uses a gcWork).
154 //
155 // Returns the amount of GC work credit produced by the operation.
156 // If flushBgCredit is true, then that credit is also flushed
157 // to the background credit pool.
158 //
159 // nowritebarrier is only advisory here.
160 //
161 //go:nowritebarrier
162 func markroot(gcw *gcWork, i uint32, flushBgCredit bool) int64 {
163         // Note: if you add a case here, please also update heapdump.go:dumproots.
164         var workDone int64
165         var workCounter *atomic.Int64
166         switch {
167         case work.baseData <= i && i < work.baseBSS:
168                 workCounter = &gcController.globalsScanWork
169                 for _, datap := range activeModules() {
170                         workDone += markrootBlock(datap.data, datap.edata-datap.data, datap.gcdatamask.bytedata, gcw, int(i-work.baseData))
171                 }
172
173         case work.baseBSS <= i && i < work.baseSpans:
174                 workCounter = &gcController.globalsScanWork
175                 for _, datap := range activeModules() {
176                         workDone += markrootBlock(datap.bss, datap.ebss-datap.bss, datap.gcbssmask.bytedata, gcw, int(i-work.baseBSS))
177                 }
178
179         case i == fixedRootFinalizers:
180                 for fb := allfin; fb != nil; fb = fb.alllink {
181                         cnt := uintptr(atomic.Load(&fb.cnt))
182                         scanblock(uintptr(unsafe.Pointer(&fb.fin[0])), cnt*unsafe.Sizeof(fb.fin[0]), &finptrmask[0], gcw, nil)
183                 }
184
185         case i == fixedRootFreeGStacks:
186                 // Switch to the system stack so we can call
187                 // stackfree.
188                 systemstack(markrootFreeGStacks)
189
190         case work.baseSpans <= i && i < work.baseStacks:
191                 // mark mspan.specials
192                 markrootSpans(gcw, int(i-work.baseSpans))
193
194         default:
195                 // the rest is scanning goroutine stacks
196                 workCounter = &gcController.stackScanWork
197                 if i < work.baseStacks || work.baseEnd <= i {
198                         printlock()
199                         print("runtime: markroot index ", i, " not in stack roots range [", work.baseStacks, ", ", work.baseEnd, ")\n")
200                         throw("markroot: bad index")
201                 }
202                 gp := work.stackRoots[i-work.baseStacks]
203
204                 // remember when we've first observed the G blocked
205                 // needed only to output in traceback
206                 status := readgstatus(gp) // We are not in a scan state
207                 if (status == _Gwaiting || status == _Gsyscall) && gp.waitsince == 0 {
208                         gp.waitsince = work.tstart
209                 }
210
211                 // scanstack must be done on the system stack in case
212                 // we're trying to scan our own stack.
213                 systemstack(func() {
214                         // If this is a self-scan, put the user G in
215                         // _Gwaiting to prevent self-deadlock. It may
216                         // already be in _Gwaiting if this is a mark
217                         // worker or we're in mark termination.
218                         userG := getg().m.curg
219                         selfScan := gp == userG && readgstatus(userG) == _Grunning
220                         if selfScan {
221                                 casGToWaiting(userG, _Grunning, waitReasonGarbageCollectionScan)
222                         }
223
224                         // TODO: suspendG blocks (and spins) until gp
225                         // stops, which may take a while for
226                         // running goroutines. Consider doing this in
227                         // two phases where the first is non-blocking:
228                         // we scan the stacks we can and ask running
229                         // goroutines to scan themselves; and the
230                         // second blocks.
231                         stopped := suspendG(gp)
232                         if stopped.dead {
233                                 gp.gcscandone = true
234                                 return
235                         }
236                         if gp.gcscandone {
237                                 throw("g already scanned")
238                         }
239                         workDone += scanstack(gp, gcw)
240                         gp.gcscandone = true
241                         resumeG(stopped)
242
243                         if selfScan {
244                                 casgstatus(userG, _Gwaiting, _Grunning)
245                         }
246                 })
247         }
248         if workCounter != nil && workDone != 0 {
249                 workCounter.Add(workDone)
250                 if flushBgCredit {
251                         gcFlushBgCredit(workDone)
252                 }
253         }
254         return workDone
255 }
256
257 // markrootBlock scans the shard'th shard of the block of memory [b0,
258 // b0+n0), with the given pointer mask.
259 //
260 // Returns the amount of work done.
261 //
262 //go:nowritebarrier
263 func markrootBlock(b0, n0 uintptr, ptrmask0 *uint8, gcw *gcWork, shard int) int64 {
264         if rootBlockBytes%(8*goarch.PtrSize) != 0 {
265                 // This is necessary to pick byte offsets in ptrmask0.
266                 throw("rootBlockBytes must be a multiple of 8*ptrSize")
267         }
268
269         // Note that if b0 is toward the end of the address space,
270         // then b0 + rootBlockBytes might wrap around.
271         // These tests are written to avoid any possible overflow.
272         off := uintptr(shard) * rootBlockBytes
273         if off >= n0 {
274                 return 0
275         }
276         b := b0 + off
277         ptrmask := (*uint8)(add(unsafe.Pointer(ptrmask0), uintptr(shard)*(rootBlockBytes/(8*goarch.PtrSize))))
278         n := uintptr(rootBlockBytes)
279         if off+n > n0 {
280                 n = n0 - off
281         }
282
283         // Scan this shard.
284         scanblock(b, n, ptrmask, gcw, nil)
285         return int64(n)
286 }
287
288 // markrootFreeGStacks frees stacks of dead Gs.
289 //
290 // This does not free stacks of dead Gs cached on Ps, but having a few
291 // cached stacks around isn't a problem.
292 func markrootFreeGStacks() {
293         // Take list of dead Gs with stacks.
294         lock(&sched.gFree.lock)
295         list := sched.gFree.stack
296         sched.gFree.stack = gList{}
297         unlock(&sched.gFree.lock)
298         if list.empty() {
299                 return
300         }
301
302         // Free stacks.
303         q := gQueue{list.head, list.head}
304         for gp := list.head.ptr(); gp != nil; gp = gp.schedlink.ptr() {
305                 stackfree(gp.stack)
306                 gp.stack.lo = 0
307                 gp.stack.hi = 0
308                 // Manipulate the queue directly since the Gs are
309                 // already all linked the right way.
310                 q.tail.set(gp)
311         }
312
313         // Put Gs back on the free list.
314         lock(&sched.gFree.lock)
315         sched.gFree.noStack.pushAll(q)
316         unlock(&sched.gFree.lock)
317 }
318
319 // markrootSpans marks roots for one shard of markArenas.
320 //
321 //go:nowritebarrier
322 func markrootSpans(gcw *gcWork, shard int) {
323         // Objects with finalizers have two GC-related invariants:
324         //
325         // 1) Everything reachable from the object must be marked.
326         // This ensures that when we pass the object to its finalizer,
327         // everything the finalizer can reach will be retained.
328         //
329         // 2) Finalizer specials (which are not in the garbage
330         // collected heap) are roots. In practice, this means the fn
331         // field must be scanned.
332         sg := mheap_.sweepgen
333
334         // Find the arena and page index into that arena for this shard.
335         ai := mheap_.markArenas[shard/(pagesPerArena/pagesPerSpanRoot)]
336         ha := mheap_.arenas[ai.l1()][ai.l2()]
337         arenaPage := uint(uintptr(shard) * pagesPerSpanRoot % pagesPerArena)
338
339         // Construct slice of bitmap which we'll iterate over.
340         specialsbits := ha.pageSpecials[arenaPage/8:]
341         specialsbits = specialsbits[:pagesPerSpanRoot/8]
342         for i := range specialsbits {
343                 // Find set bits, which correspond to spans with specials.
344                 specials := atomic.Load8(&specialsbits[i])
345                 if specials == 0 {
346                         continue
347                 }
348                 for j := uint(0); j < 8; j++ {
349                         if specials&(1<<j) == 0 {
350                                 continue
351                         }
352                         // Find the span for this bit.
353                         //
354                         // This value is guaranteed to be non-nil because having
355                         // specials implies that the span is in-use, and since we're
356                         // currently marking we can be sure that we don't have to worry
357                         // about the span being freed and re-used.
358                         s := ha.spans[arenaPage+uint(i)*8+j]
359
360                         // The state must be mSpanInUse if the specials bit is set, so
361                         // sanity check that.
362                         if state := s.state.get(); state != mSpanInUse {
363                                 print("s.state = ", state, "\n")
364                                 throw("non in-use span found with specials bit set")
365                         }
366                         // Check that this span was swept (it may be cached or uncached).
367                         if !useCheckmark && !(s.sweepgen == sg || s.sweepgen == sg+3) {
368                                 // sweepgen was updated (+2) during non-checkmark GC pass
369                                 print("sweep ", s.sweepgen, " ", sg, "\n")
370                                 throw("gc: unswept span")
371                         }
372
373                         // Lock the specials to prevent a special from being
374                         // removed from the list while we're traversing it.
375                         lock(&s.speciallock)
376                         for sp := s.specials; sp != nil; sp = sp.next {
377                                 if sp.kind != _KindSpecialFinalizer {
378                                         continue
379                                 }
380                                 // don't mark finalized object, but scan it so we
381                                 // retain everything it points to.
382                                 spf := (*specialfinalizer)(unsafe.Pointer(sp))
383                                 // A finalizer can be set for an inner byte of an object, find object beginning.
384                                 p := s.base() + uintptr(spf.special.offset)/s.elemsize*s.elemsize
385
386                                 // Mark everything that can be reached from
387                                 // the object (but *not* the object itself or
388                                 // we'll never collect it).
389                                 if !s.spanclass.noscan() {
390                                         scanobject(p, gcw)
391                                 }
392
393                                 // The special itself is a root.
394                                 scanblock(uintptr(unsafe.Pointer(&spf.fn)), goarch.PtrSize, &oneptrmask[0], gcw, nil)
395                         }
396                         unlock(&s.speciallock)
397                 }
398         }
399 }
400
401 // gcAssistAlloc performs GC work to make gp's assist debt positive.
402 // gp must be the calling user goroutine.
403 //
404 // This must be called with preemption enabled.
405 func gcAssistAlloc(gp *g) {
406         // Don't assist in non-preemptible contexts. These are
407         // generally fragile and won't allow the assist to block.
408         if getg() == gp.m.g0 {
409                 return
410         }
411         if mp := getg().m; mp.locks > 0 || mp.preemptoff != "" {
412                 return
413         }
414
415         traced := false
416 retry:
417         if gcCPULimiter.limiting() {
418                 // If the CPU limiter is enabled, intentionally don't
419                 // assist to reduce the amount of CPU time spent in the GC.
420                 if traced {
421                         traceGCMarkAssistDone()
422                 }
423                 return
424         }
425         // Compute the amount of scan work we need to do to make the
426         // balance positive. When the required amount of work is low,
427         // we over-assist to build up credit for future allocations
428         // and amortize the cost of assisting.
429         assistWorkPerByte := gcController.assistWorkPerByte.Load()
430         assistBytesPerWork := gcController.assistBytesPerWork.Load()
431         debtBytes := -gp.gcAssistBytes
432         scanWork := int64(assistWorkPerByte * float64(debtBytes))
433         if scanWork < gcOverAssistWork {
434                 scanWork = gcOverAssistWork
435                 debtBytes = int64(assistBytesPerWork * float64(scanWork))
436         }
437
438         // Steal as much credit as we can from the background GC's
439         // scan credit. This is racy and may drop the background
440         // credit below 0 if two mutators steal at the same time. This
441         // will just cause steals to fail until credit is accumulated
442         // again, so in the long run it doesn't really matter, but we
443         // do have to handle the negative credit case.
444         bgScanCredit := gcController.bgScanCredit.Load()
445         stolen := int64(0)
446         if bgScanCredit > 0 {
447                 if bgScanCredit < scanWork {
448                         stolen = bgScanCredit
449                         gp.gcAssistBytes += 1 + int64(assistBytesPerWork*float64(stolen))
450                 } else {
451                         stolen = scanWork
452                         gp.gcAssistBytes += debtBytes
453                 }
454                 gcController.bgScanCredit.Add(-stolen)
455
456                 scanWork -= stolen
457
458                 if scanWork == 0 {
459                         // We were able to steal all of the credit we
460                         // needed.
461                         if traced {
462                                 traceGCMarkAssistDone()
463                         }
464                         return
465                 }
466         }
467
468         if trace.enabled && !traced {
469                 traced = true
470                 traceGCMarkAssistStart()
471         }
472
473         // Perform assist work
474         systemstack(func() {
475                 gcAssistAlloc1(gp, scanWork)
476                 // The user stack may have moved, so this can't touch
477                 // anything on it until it returns from systemstack.
478         })
479
480         completed := gp.param != nil
481         gp.param = nil
482         if completed {
483                 gcMarkDone()
484         }
485
486         if gp.gcAssistBytes < 0 {
487                 // We were unable steal enough credit or perform
488                 // enough work to pay off the assist debt. We need to
489                 // do one of these before letting the mutator allocate
490                 // more to prevent over-allocation.
491                 //
492                 // If this is because we were preempted, reschedule
493                 // and try some more.
494                 if gp.preempt {
495                         Gosched()
496                         goto retry
497                 }
498
499                 // Add this G to an assist queue and park. When the GC
500                 // has more background credit, it will satisfy queued
501                 // assists before flushing to the global credit pool.
502                 //
503                 // Note that this does *not* get woken up when more
504                 // work is added to the work list. The theory is that
505                 // there wasn't enough work to do anyway, so we might
506                 // as well let background marking take care of the
507                 // work that is available.
508                 if !gcParkAssist() {
509                         goto retry
510                 }
511
512                 // At this point either background GC has satisfied
513                 // this G's assist debt, or the GC cycle is over.
514         }
515         if traced {
516                 traceGCMarkAssistDone()
517         }
518 }
519
520 // gcAssistAlloc1 is the part of gcAssistAlloc that runs on the system
521 // stack. This is a separate function to make it easier to see that
522 // we're not capturing anything from the user stack, since the user
523 // stack may move while we're in this function.
524 //
525 // gcAssistAlloc1 indicates whether this assist completed the mark
526 // phase by setting gp.param to non-nil. This can't be communicated on
527 // the stack since it may move.
528 //
529 //go:systemstack
530 func gcAssistAlloc1(gp *g, scanWork int64) {
531         // Clear the flag indicating that this assist completed the
532         // mark phase.
533         gp.param = nil
534
535         if atomic.Load(&gcBlackenEnabled) == 0 {
536                 // The gcBlackenEnabled check in malloc races with the
537                 // store that clears it but an atomic check in every malloc
538                 // would be a performance hit.
539                 // Instead we recheck it here on the non-preemptable system
540                 // stack to determine if we should perform an assist.
541
542                 // GC is done, so ignore any remaining debt.
543                 gp.gcAssistBytes = 0
544                 return
545         }
546         // Track time spent in this assist. Since we're on the
547         // system stack, this is non-preemptible, so we can
548         // just measure start and end time.
549         //
550         // Limiter event tracking might be disabled if we end up here
551         // while on a mark worker.
552         startTime := nanotime()
553         trackLimiterEvent := gp.m.p.ptr().limiterEvent.start(limiterEventMarkAssist, startTime)
554
555         decnwait := atomic.Xadd(&work.nwait, -1)
556         if decnwait == work.nproc {
557                 println("runtime: work.nwait =", decnwait, "work.nproc=", work.nproc)
558                 throw("nwait > work.nprocs")
559         }
560
561         // gcDrainN requires the caller to be preemptible.
562         casGToWaiting(gp, _Grunning, waitReasonGCAssistMarking)
563
564         // drain own cached work first in the hopes that it
565         // will be more cache friendly.
566         gcw := &getg().m.p.ptr().gcw
567         workDone := gcDrainN(gcw, scanWork)
568
569         casgstatus(gp, _Gwaiting, _Grunning)
570
571         // Record that we did this much scan work.
572         //
573         // Back out the number of bytes of assist credit that
574         // this scan work counts for. The "1+" is a poor man's
575         // round-up, to ensure this adds credit even if
576         // assistBytesPerWork is very low.
577         assistBytesPerWork := gcController.assistBytesPerWork.Load()
578         gp.gcAssistBytes += 1 + int64(assistBytesPerWork*float64(workDone))
579
580         // If this is the last worker and we ran out of work,
581         // signal a completion point.
582         incnwait := atomic.Xadd(&work.nwait, +1)
583         if incnwait > work.nproc {
584                 println("runtime: work.nwait=", incnwait,
585                         "work.nproc=", work.nproc)
586                 throw("work.nwait > work.nproc")
587         }
588
589         if incnwait == work.nproc && !gcMarkWorkAvailable(nil) {
590                 // This has reached a background completion point. Set
591                 // gp.param to a non-nil value to indicate this. It
592                 // doesn't matter what we set it to (it just has to be
593                 // a valid pointer).
594                 gp.param = unsafe.Pointer(gp)
595         }
596         now := nanotime()
597         duration := now - startTime
598         pp := gp.m.p.ptr()
599         pp.gcAssistTime += duration
600         if trackLimiterEvent {
601                 pp.limiterEvent.stop(limiterEventMarkAssist, now)
602         }
603         if pp.gcAssistTime > gcAssistTimeSlack {
604                 gcController.assistTime.Add(pp.gcAssistTime)
605                 gcCPULimiter.update(now)
606                 pp.gcAssistTime = 0
607         }
608 }
609
610 // gcWakeAllAssists wakes all currently blocked assists. This is used
611 // at the end of a GC cycle. gcBlackenEnabled must be false to prevent
612 // new assists from going to sleep after this point.
613 func gcWakeAllAssists() {
614         lock(&work.assistQueue.lock)
615         list := work.assistQueue.q.popList()
616         injectglist(&list)
617         unlock(&work.assistQueue.lock)
618 }
619
620 // gcParkAssist puts the current goroutine on the assist queue and parks.
621 //
622 // gcParkAssist reports whether the assist is now satisfied. If it
623 // returns false, the caller must retry the assist.
624 func gcParkAssist() bool {
625         lock(&work.assistQueue.lock)
626         // If the GC cycle finished while we were getting the lock,
627         // exit the assist. The cycle can't finish while we hold the
628         // lock.
629         if atomic.Load(&gcBlackenEnabled) == 0 {
630                 unlock(&work.assistQueue.lock)
631                 return true
632         }
633
634         gp := getg()
635         oldList := work.assistQueue.q
636         work.assistQueue.q.pushBack(gp)
637
638         // Recheck for background credit now that this G is in
639         // the queue, but can still back out. This avoids a
640         // race in case background marking has flushed more
641         // credit since we checked above.
642         if gcController.bgScanCredit.Load() > 0 {
643                 work.assistQueue.q = oldList
644                 if oldList.tail != 0 {
645                         oldList.tail.ptr().schedlink.set(nil)
646                 }
647                 unlock(&work.assistQueue.lock)
648                 return false
649         }
650         // Park.
651         goparkunlock(&work.assistQueue.lock, waitReasonGCAssistWait, traceEvGoBlockGC, 2)
652         return true
653 }
654
655 // gcFlushBgCredit flushes scanWork units of background scan work
656 // credit. This first satisfies blocked assists on the
657 // work.assistQueue and then flushes any remaining credit to
658 // gcController.bgScanCredit.
659 //
660 // Write barriers are disallowed because this is used by gcDrain after
661 // it has ensured that all work is drained and this must preserve that
662 // condition.
663 //
664 //go:nowritebarrierrec
665 func gcFlushBgCredit(scanWork int64) {
666         if work.assistQueue.q.empty() {
667                 // Fast path; there are no blocked assists. There's a
668                 // small window here where an assist may add itself to
669                 // the blocked queue and park. If that happens, we'll
670                 // just get it on the next flush.
671                 gcController.bgScanCredit.Add(scanWork)
672                 return
673         }
674
675         assistBytesPerWork := gcController.assistBytesPerWork.Load()
676         scanBytes := int64(float64(scanWork) * assistBytesPerWork)
677
678         lock(&work.assistQueue.lock)
679         for !work.assistQueue.q.empty() && scanBytes > 0 {
680                 gp := work.assistQueue.q.pop()
681                 // Note that gp.gcAssistBytes is negative because gp
682                 // is in debt. Think carefully about the signs below.
683                 if scanBytes+gp.gcAssistBytes >= 0 {
684                         // Satisfy this entire assist debt.
685                         scanBytes += gp.gcAssistBytes
686                         gp.gcAssistBytes = 0
687                         // It's important that we *not* put gp in
688                         // runnext. Otherwise, it's possible for user
689                         // code to exploit the GC worker's high
690                         // scheduler priority to get itself always run
691                         // before other goroutines and always in the
692                         // fresh quantum started by GC.
693                         ready(gp, 0, false)
694                 } else {
695                         // Partially satisfy this assist.
696                         gp.gcAssistBytes += scanBytes
697                         scanBytes = 0
698                         // As a heuristic, we move this assist to the
699                         // back of the queue so that large assists
700                         // can't clog up the assist queue and
701                         // substantially delay small assists.
702                         work.assistQueue.q.pushBack(gp)
703                         break
704                 }
705         }
706
707         if scanBytes > 0 {
708                 // Convert from scan bytes back to work.
709                 assistWorkPerByte := gcController.assistWorkPerByte.Load()
710                 scanWork = int64(float64(scanBytes) * assistWorkPerByte)
711                 gcController.bgScanCredit.Add(scanWork)
712         }
713         unlock(&work.assistQueue.lock)
714 }
715
716 // scanstack scans gp's stack, greying all pointers found on the stack.
717 //
718 // Returns the amount of scan work performed, but doesn't update
719 // gcController.stackScanWork or flush any credit. Any background credit produced
720 // by this function should be flushed by its caller. scanstack itself can't
721 // safely flush because it may result in trying to wake up a goroutine that
722 // was just scanned, resulting in a self-deadlock.
723 //
724 // scanstack will also shrink the stack if it is safe to do so. If it
725 // is not, it schedules a stack shrink for the next synchronous safe
726 // point.
727 //
728 // scanstack is marked go:systemstack because it must not be preempted
729 // while using a workbuf.
730 //
731 //go:nowritebarrier
732 //go:systemstack
733 func scanstack(gp *g, gcw *gcWork) int64 {
734         if readgstatus(gp)&_Gscan == 0 {
735                 print("runtime:scanstack: gp=", gp, ", goid=", gp.goid, ", gp->atomicstatus=", hex(readgstatus(gp)), "\n")
736                 throw("scanstack - bad status")
737         }
738
739         switch readgstatus(gp) &^ _Gscan {
740         default:
741                 print("runtime: gp=", gp, ", goid=", gp.goid, ", gp->atomicstatus=", readgstatus(gp), "\n")
742                 throw("mark - bad status")
743         case _Gdead:
744                 return 0
745         case _Grunning:
746                 print("runtime: gp=", gp, ", goid=", gp.goid, ", gp->atomicstatus=", readgstatus(gp), "\n")
747                 throw("scanstack: goroutine not stopped")
748         case _Grunnable, _Gsyscall, _Gwaiting:
749                 // ok
750         }
751
752         if gp == getg() {
753                 throw("can't scan our own stack")
754         }
755
756         // scannedSize is the amount of work we'll be reporting.
757         //
758         // It is less than the allocated size (which is hi-lo).
759         var sp uintptr
760         if gp.syscallsp != 0 {
761                 sp = gp.syscallsp // If in a system call this is the stack pointer (gp.sched.sp can be 0 in this case on Windows).
762         } else {
763                 sp = gp.sched.sp
764         }
765         scannedSize := gp.stack.hi - sp
766
767         // Keep statistics for initial stack size calculation.
768         // Note that this accumulates the scanned size, not the allocated size.
769         p := getg().m.p.ptr()
770         p.scannedStackSize += uint64(scannedSize)
771         p.scannedStacks++
772
773         if isShrinkStackSafe(gp) {
774                 // Shrink the stack if not much of it is being used.
775                 shrinkstack(gp)
776         } else {
777                 // Otherwise, shrink the stack at the next sync safe point.
778                 gp.preemptShrink = true
779         }
780
781         var state stackScanState
782         state.stack = gp.stack
783
784         if stackTraceDebug {
785                 println("stack trace goroutine", gp.goid)
786         }
787
788         if debugScanConservative && gp.asyncSafePoint {
789                 print("scanning async preempted goroutine ", gp.goid, " stack [", hex(gp.stack.lo), ",", hex(gp.stack.hi), ")\n")
790         }
791
792         // Scan the saved context register. This is effectively a live
793         // register that gets moved back and forth between the
794         // register and sched.ctxt without a write barrier.
795         if gp.sched.ctxt != nil {
796                 scanblock(uintptr(unsafe.Pointer(&gp.sched.ctxt)), goarch.PtrSize, &oneptrmask[0], gcw, &state)
797         }
798
799         // Scan the stack. Accumulate a list of stack objects.
800         var u unwinder
801         for u.init(gp, 0); u.valid(); u.next() {
802                 scanframeworker(&u.frame, &state, gcw)
803         }
804
805         // Find additional pointers that point into the stack from the heap.
806         // Currently this includes defers and panics. See also function copystack.
807
808         // Find and trace other pointers in defer records.
809         for d := gp._defer; d != nil; d = d.link {
810                 if d.fn != nil {
811                         // Scan the func value, which could be a stack allocated closure.
812                         // See issue 30453.
813                         scanblock(uintptr(unsafe.Pointer(&d.fn)), goarch.PtrSize, &oneptrmask[0], gcw, &state)
814                 }
815                 if d.link != nil {
816                         // The link field of a stack-allocated defer record might point
817                         // to a heap-allocated defer record. Keep that heap record live.
818                         scanblock(uintptr(unsafe.Pointer(&d.link)), goarch.PtrSize, &oneptrmask[0], gcw, &state)
819                 }
820                 // Retain defers records themselves.
821                 // Defer records might not be reachable from the G through regular heap
822                 // tracing because the defer linked list might weave between the stack and the heap.
823                 if d.heap {
824                         scanblock(uintptr(unsafe.Pointer(&d)), goarch.PtrSize, &oneptrmask[0], gcw, &state)
825                 }
826         }
827         if gp._panic != nil {
828                 // Panics are always stack allocated.
829                 state.putPtr(uintptr(unsafe.Pointer(gp._panic)), false)
830         }
831
832         // Find and scan all reachable stack objects.
833         //
834         // The state's pointer queue prioritizes precise pointers over
835         // conservative pointers so that we'll prefer scanning stack
836         // objects precisely.
837         state.buildIndex()
838         for {
839                 p, conservative := state.getPtr()
840                 if p == 0 {
841                         break
842                 }
843                 obj := state.findObject(p)
844                 if obj == nil {
845                         continue
846                 }
847                 r := obj.r
848                 if r == nil {
849                         // We've already scanned this object.
850                         continue
851                 }
852                 obj.setRecord(nil) // Don't scan it again.
853                 if stackTraceDebug {
854                         printlock()
855                         print("  live stkobj at", hex(state.stack.lo+uintptr(obj.off)), "of size", obj.size)
856                         if conservative {
857                                 print(" (conservative)")
858                         }
859                         println()
860                         printunlock()
861                 }
862                 gcdata := r.gcdata()
863                 var s *mspan
864                 if r.useGCProg() {
865                         // This path is pretty unlikely, an object large enough
866                         // to have a GC program allocated on the stack.
867                         // We need some space to unpack the program into a straight
868                         // bitmask, which we allocate/free here.
869                         // TODO: it would be nice if there were a way to run a GC
870                         // program without having to store all its bits. We'd have
871                         // to change from a Lempel-Ziv style program to something else.
872                         // Or we can forbid putting objects on stacks if they require
873                         // a gc program (see issue 27447).
874                         s = materializeGCProg(r.ptrdata(), gcdata)
875                         gcdata = (*byte)(unsafe.Pointer(s.startAddr))
876                 }
877
878                 b := state.stack.lo + uintptr(obj.off)
879                 if conservative {
880                         scanConservative(b, r.ptrdata(), gcdata, gcw, &state)
881                 } else {
882                         scanblock(b, r.ptrdata(), gcdata, gcw, &state)
883                 }
884
885                 if s != nil {
886                         dematerializeGCProg(s)
887                 }
888         }
889
890         // Deallocate object buffers.
891         // (Pointer buffers were all deallocated in the loop above.)
892         for state.head != nil {
893                 x := state.head
894                 state.head = x.next
895                 if stackTraceDebug {
896                         for i := 0; i < x.nobj; i++ {
897                                 obj := &x.obj[i]
898                                 if obj.r == nil { // reachable
899                                         continue
900                                 }
901                                 println("  dead stkobj at", hex(gp.stack.lo+uintptr(obj.off)), "of size", obj.r.size)
902                                 // Note: not necessarily really dead - only reachable-from-ptr dead.
903                         }
904                 }
905                 x.nobj = 0
906                 putempty((*workbuf)(unsafe.Pointer(x)))
907         }
908         if state.buf != nil || state.cbuf != nil || state.freeBuf != nil {
909                 throw("remaining pointer buffers")
910         }
911         return int64(scannedSize)
912 }
913
914 // Scan a stack frame: local variables and function arguments/results.
915 //
916 //go:nowritebarrier
917 func scanframeworker(frame *stkframe, state *stackScanState, gcw *gcWork) {
918         if _DebugGC > 1 && frame.continpc != 0 {
919                 print("scanframe ", funcname(frame.fn), "\n")
920         }
921
922         isAsyncPreempt := frame.fn.valid() && frame.fn.funcID == funcID_asyncPreempt
923         isDebugCall := frame.fn.valid() && frame.fn.funcID == funcID_debugCallV2
924         if state.conservative || isAsyncPreempt || isDebugCall {
925                 if debugScanConservative {
926                         println("conservatively scanning function", funcname(frame.fn), "at PC", hex(frame.continpc))
927                 }
928
929                 // Conservatively scan the frame. Unlike the precise
930                 // case, this includes the outgoing argument space
931                 // since we may have stopped while this function was
932                 // setting up a call.
933                 //
934                 // TODO: We could narrow this down if the compiler
935                 // produced a single map per function of stack slots
936                 // and registers that ever contain a pointer.
937                 if frame.varp != 0 {
938                         size := frame.varp - frame.sp
939                         if size > 0 {
940                                 scanConservative(frame.sp, size, nil, gcw, state)
941                         }
942                 }
943
944                 // Scan arguments to this frame.
945                 if n := frame.argBytes(); n != 0 {
946                         // TODO: We could pass the entry argument map
947                         // to narrow this down further.
948                         scanConservative(frame.argp, n, nil, gcw, state)
949                 }
950
951                 if isAsyncPreempt || isDebugCall {
952                         // This function's frame contained the
953                         // registers for the asynchronously stopped
954                         // parent frame. Scan the parent
955                         // conservatively.
956                         state.conservative = true
957                 } else {
958                         // We only wanted to scan those two frames
959                         // conservatively. Clear the flag for future
960                         // frames.
961                         state.conservative = false
962                 }
963                 return
964         }
965
966         locals, args, objs := frame.getStackMap(&state.cache, false)
967
968         // Scan local variables if stack frame has been allocated.
969         if locals.n > 0 {
970                 size := uintptr(locals.n) * goarch.PtrSize
971                 scanblock(frame.varp-size, size, locals.bytedata, gcw, state)
972         }
973
974         // Scan arguments.
975         if args.n > 0 {
976                 scanblock(frame.argp, uintptr(args.n)*goarch.PtrSize, args.bytedata, gcw, state)
977         }
978
979         // Add all stack objects to the stack object list.
980         if frame.varp != 0 {
981                 // varp is 0 for defers, where there are no locals.
982                 // In that case, there can't be a pointer to its args, either.
983                 // (And all args would be scanned above anyway.)
984                 for i := range objs {
985                         obj := &objs[i]
986                         off := obj.off
987                         base := frame.varp // locals base pointer
988                         if off >= 0 {
989                                 base = frame.argp // arguments and return values base pointer
990                         }
991                         ptr := base + uintptr(off)
992                         if ptr < frame.sp {
993                                 // object hasn't been allocated in the frame yet.
994                                 continue
995                         }
996                         if stackTraceDebug {
997                                 println("stkobj at", hex(ptr), "of size", obj.size)
998                         }
999                         state.addObject(ptr, obj)
1000                 }
1001         }
1002 }
1003
1004 type gcDrainFlags int
1005
1006 const (
1007         gcDrainUntilPreempt gcDrainFlags = 1 << iota
1008         gcDrainFlushBgCredit
1009         gcDrainIdle
1010         gcDrainFractional
1011 )
1012
1013 // gcDrain scans roots and objects in work buffers, blackening grey
1014 // objects until it is unable to get more work. It may return before
1015 // GC is done; it's the caller's responsibility to balance work from
1016 // other Ps.
1017 //
1018 // If flags&gcDrainUntilPreempt != 0, gcDrain returns when g.preempt
1019 // is set.
1020 //
1021 // If flags&gcDrainIdle != 0, gcDrain returns when there is other work
1022 // to do.
1023 //
1024 // If flags&gcDrainFractional != 0, gcDrain self-preempts when
1025 // pollFractionalWorkerExit() returns true. This implies
1026 // gcDrainNoBlock.
1027 //
1028 // If flags&gcDrainFlushBgCredit != 0, gcDrain flushes scan work
1029 // credit to gcController.bgScanCredit every gcCreditSlack units of
1030 // scan work.
1031 //
1032 // gcDrain will always return if there is a pending STW.
1033 //
1034 //go:nowritebarrier
1035 func gcDrain(gcw *gcWork, flags gcDrainFlags) {
1036         if !writeBarrier.needed {
1037                 throw("gcDrain phase incorrect")
1038         }
1039
1040         gp := getg().m.curg
1041         preemptible := flags&gcDrainUntilPreempt != 0
1042         flushBgCredit := flags&gcDrainFlushBgCredit != 0
1043         idle := flags&gcDrainIdle != 0
1044
1045         initScanWork := gcw.heapScanWork
1046
1047         // checkWork is the scan work before performing the next
1048         // self-preempt check.
1049         checkWork := int64(1<<63 - 1)
1050         var check func() bool
1051         if flags&(gcDrainIdle|gcDrainFractional) != 0 {
1052                 checkWork = initScanWork + drainCheckThreshold
1053                 if idle {
1054                         check = pollWork
1055                 } else if flags&gcDrainFractional != 0 {
1056                         check = pollFractionalWorkerExit
1057                 }
1058         }
1059
1060         // Drain root marking jobs.
1061         if work.markrootNext < work.markrootJobs {
1062                 // Stop if we're preemptible or if someone wants to STW.
1063                 for !(gp.preempt && (preemptible || sched.gcwaiting.Load())) {
1064                         job := atomic.Xadd(&work.markrootNext, +1) - 1
1065                         if job >= work.markrootJobs {
1066                                 break
1067                         }
1068                         markroot(gcw, job, flushBgCredit)
1069                         if check != nil && check() {
1070                                 goto done
1071                         }
1072                 }
1073         }
1074
1075         // Drain heap marking jobs.
1076         // Stop if we're preemptible or if someone wants to STW.
1077         for !(gp.preempt && (preemptible || sched.gcwaiting.Load())) {
1078                 // Try to keep work available on the global queue. We used to
1079                 // check if there were waiting workers, but it's better to
1080                 // just keep work available than to make workers wait. In the
1081                 // worst case, we'll do O(log(_WorkbufSize)) unnecessary
1082                 // balances.
1083                 if work.full == 0 {
1084                         gcw.balance()
1085                 }
1086
1087                 b := gcw.tryGetFast()
1088                 if b == 0 {
1089                         b = gcw.tryGet()
1090                         if b == 0 {
1091                                 // Flush the write barrier
1092                                 // buffer; this may create
1093                                 // more work.
1094                                 wbBufFlush()
1095                                 b = gcw.tryGet()
1096                         }
1097                 }
1098                 if b == 0 {
1099                         // Unable to get work.
1100                         break
1101                 }
1102                 scanobject(b, gcw)
1103
1104                 // Flush background scan work credit to the global
1105                 // account if we've accumulated enough locally so
1106                 // mutator assists can draw on it.
1107                 if gcw.heapScanWork >= gcCreditSlack {
1108                         gcController.heapScanWork.Add(gcw.heapScanWork)
1109                         if flushBgCredit {
1110                                 gcFlushBgCredit(gcw.heapScanWork - initScanWork)
1111                                 initScanWork = 0
1112                         }
1113                         checkWork -= gcw.heapScanWork
1114                         gcw.heapScanWork = 0
1115
1116                         if checkWork <= 0 {
1117                                 checkWork += drainCheckThreshold
1118                                 if check != nil && check() {
1119                                         break
1120                                 }
1121                         }
1122                 }
1123         }
1124
1125 done:
1126         // Flush remaining scan work credit.
1127         if gcw.heapScanWork > 0 {
1128                 gcController.heapScanWork.Add(gcw.heapScanWork)
1129                 if flushBgCredit {
1130                         gcFlushBgCredit(gcw.heapScanWork - initScanWork)
1131                 }
1132                 gcw.heapScanWork = 0
1133         }
1134 }
1135
1136 // gcDrainN blackens grey objects until it has performed roughly
1137 // scanWork units of scan work or the G is preempted. This is
1138 // best-effort, so it may perform less work if it fails to get a work
1139 // buffer. Otherwise, it will perform at least n units of work, but
1140 // may perform more because scanning is always done in whole object
1141 // increments. It returns the amount of scan work performed.
1142 //
1143 // The caller goroutine must be in a preemptible state (e.g.,
1144 // _Gwaiting) to prevent deadlocks during stack scanning. As a
1145 // consequence, this must be called on the system stack.
1146 //
1147 //go:nowritebarrier
1148 //go:systemstack
1149 func gcDrainN(gcw *gcWork, scanWork int64) int64 {
1150         if !writeBarrier.needed {
1151                 throw("gcDrainN phase incorrect")
1152         }
1153
1154         // There may already be scan work on the gcw, which we don't
1155         // want to claim was done by this call.
1156         workFlushed := -gcw.heapScanWork
1157
1158         // In addition to backing out because of a preemption, back out
1159         // if the GC CPU limiter is enabled.
1160         gp := getg().m.curg
1161         for !gp.preempt && !gcCPULimiter.limiting() && workFlushed+gcw.heapScanWork < scanWork {
1162                 // See gcDrain comment.
1163                 if work.full == 0 {
1164                         gcw.balance()
1165                 }
1166
1167                 b := gcw.tryGetFast()
1168                 if b == 0 {
1169                         b = gcw.tryGet()
1170                         if b == 0 {
1171                                 // Flush the write barrier buffer;
1172                                 // this may create more work.
1173                                 wbBufFlush()
1174                                 b = gcw.tryGet()
1175                         }
1176                 }
1177
1178                 if b == 0 {
1179                         // Try to do a root job.
1180                         if work.markrootNext < work.markrootJobs {
1181                                 job := atomic.Xadd(&work.markrootNext, +1) - 1
1182                                 if job < work.markrootJobs {
1183                                         workFlushed += markroot(gcw, job, false)
1184                                         continue
1185                                 }
1186                         }
1187                         // No heap or root jobs.
1188                         break
1189                 }
1190
1191                 scanobject(b, gcw)
1192
1193                 // Flush background scan work credit.
1194                 if gcw.heapScanWork >= gcCreditSlack {
1195                         gcController.heapScanWork.Add(gcw.heapScanWork)
1196                         workFlushed += gcw.heapScanWork
1197                         gcw.heapScanWork = 0
1198                 }
1199         }
1200
1201         // Unlike gcDrain, there's no need to flush remaining work
1202         // here because this never flushes to bgScanCredit and
1203         // gcw.dispose will flush any remaining work to scanWork.
1204
1205         return workFlushed + gcw.heapScanWork
1206 }
1207
1208 // scanblock scans b as scanobject would, but using an explicit
1209 // pointer bitmap instead of the heap bitmap.
1210 //
1211 // This is used to scan non-heap roots, so it does not update
1212 // gcw.bytesMarked or gcw.heapScanWork.
1213 //
1214 // If stk != nil, possible stack pointers are also reported to stk.putPtr.
1215 //
1216 //go:nowritebarrier
1217 func scanblock(b0, n0 uintptr, ptrmask *uint8, gcw *gcWork, stk *stackScanState) {
1218         // Use local copies of original parameters, so that a stack trace
1219         // due to one of the throws below shows the original block
1220         // base and extent.
1221         b := b0
1222         n := n0
1223
1224         for i := uintptr(0); i < n; {
1225                 // Find bits for the next word.
1226                 bits := uint32(*addb(ptrmask, i/(goarch.PtrSize*8)))
1227                 if bits == 0 {
1228                         i += goarch.PtrSize * 8
1229                         continue
1230                 }
1231                 for j := 0; j < 8 && i < n; j++ {
1232                         if bits&1 != 0 {
1233                                 // Same work as in scanobject; see comments there.
1234                                 p := *(*uintptr)(unsafe.Pointer(b + i))
1235                                 if p != 0 {
1236                                         if obj, span, objIndex := findObject(p, b, i); obj != 0 {
1237                                                 greyobject(obj, b, i, span, gcw, objIndex)
1238                                         } else if stk != nil && p >= stk.stack.lo && p < stk.stack.hi {
1239                                                 stk.putPtr(p, false)
1240                                         }
1241                                 }
1242                         }
1243                         bits >>= 1
1244                         i += goarch.PtrSize
1245                 }
1246         }
1247 }
1248
1249 // scanobject scans the object starting at b, adding pointers to gcw.
1250 // b must point to the beginning of a heap object or an oblet.
1251 // scanobject consults the GC bitmap for the pointer mask and the
1252 // spans for the size of the object.
1253 //
1254 //go:nowritebarrier
1255 func scanobject(b uintptr, gcw *gcWork) {
1256         // Prefetch object before we scan it.
1257         //
1258         // This will overlap fetching the beginning of the object with initial
1259         // setup before we start scanning the object.
1260         sys.Prefetch(b)
1261
1262         // Find the bits for b and the size of the object at b.
1263         //
1264         // b is either the beginning of an object, in which case this
1265         // is the size of the object to scan, or it points to an
1266         // oblet, in which case we compute the size to scan below.
1267         s := spanOfUnchecked(b)
1268         n := s.elemsize
1269         if n == 0 {
1270                 throw("scanobject n == 0")
1271         }
1272         if s.spanclass.noscan() {
1273                 // Correctness-wise this is ok, but it's inefficient
1274                 // if noscan objects reach here.
1275                 throw("scanobject of a noscan object")
1276         }
1277
1278         if n > maxObletBytes {
1279                 // Large object. Break into oblets for better
1280                 // parallelism and lower latency.
1281                 if b == s.base() {
1282                         // Enqueue the other oblets to scan later.
1283                         // Some oblets may be in b's scalar tail, but
1284                         // these will be marked as "no more pointers",
1285                         // so we'll drop out immediately when we go to
1286                         // scan those.
1287                         for oblet := b + maxObletBytes; oblet < s.base()+s.elemsize; oblet += maxObletBytes {
1288                                 if !gcw.putFast(oblet) {
1289                                         gcw.put(oblet)
1290                                 }
1291                         }
1292                 }
1293
1294                 // Compute the size of the oblet. Since this object
1295                 // must be a large object, s.base() is the beginning
1296                 // of the object.
1297                 n = s.base() + s.elemsize - b
1298                 if n > maxObletBytes {
1299                         n = maxObletBytes
1300                 }
1301         }
1302
1303         hbits := heapBitsForAddr(b, n)
1304         var scanSize uintptr
1305         for {
1306                 var addr uintptr
1307                 if hbits, addr = hbits.nextFast(); addr == 0 {
1308                         if hbits, addr = hbits.next(); addr == 0 {
1309                                 break
1310                         }
1311                 }
1312
1313                 // Keep track of farthest pointer we found, so we can
1314                 // update heapScanWork. TODO: is there a better metric,
1315                 // now that we can skip scalar portions pretty efficiently?
1316                 scanSize = addr - b + goarch.PtrSize
1317
1318                 // Work here is duplicated in scanblock and above.
1319                 // If you make changes here, make changes there too.
1320                 obj := *(*uintptr)(unsafe.Pointer(addr))
1321
1322                 // At this point we have extracted the next potential pointer.
1323                 // Quickly filter out nil and pointers back to the current object.
1324                 if obj != 0 && obj-b >= n {
1325                         // Test if obj points into the Go heap and, if so,
1326                         // mark the object.
1327                         //
1328                         // Note that it's possible for findObject to
1329                         // fail if obj points to a just-allocated heap
1330                         // object because of a race with growing the
1331                         // heap. In this case, we know the object was
1332                         // just allocated and hence will be marked by
1333                         // allocation itself.
1334                         if obj, span, objIndex := findObject(obj, b, addr-b); obj != 0 {
1335                                 greyobject(obj, b, addr-b, span, gcw, objIndex)
1336                         }
1337                 }
1338         }
1339         gcw.bytesMarked += uint64(n)
1340         gcw.heapScanWork += int64(scanSize)
1341 }
1342
1343 // scanConservative scans block [b, b+n) conservatively, treating any
1344 // pointer-like value in the block as a pointer.
1345 //
1346 // If ptrmask != nil, only words that are marked in ptrmask are
1347 // considered as potential pointers.
1348 //
1349 // If state != nil, it's assumed that [b, b+n) is a block in the stack
1350 // and may contain pointers to stack objects.
1351 func scanConservative(b, n uintptr, ptrmask *uint8, gcw *gcWork, state *stackScanState) {
1352         if debugScanConservative {
1353                 printlock()
1354                 print("conservatively scanning [", hex(b), ",", hex(b+n), ")\n")
1355                 hexdumpWords(b, b+n, func(p uintptr) byte {
1356                         if ptrmask != nil {
1357                                 word := (p - b) / goarch.PtrSize
1358                                 bits := *addb(ptrmask, word/8)
1359                                 if (bits>>(word%8))&1 == 0 {
1360                                         return '$'
1361                                 }
1362                         }
1363
1364                         val := *(*uintptr)(unsafe.Pointer(p))
1365                         if state != nil && state.stack.lo <= val && val < state.stack.hi {
1366                                 return '@'
1367                         }
1368
1369                         span := spanOfHeap(val)
1370                         if span == nil {
1371                                 return ' '
1372                         }
1373                         idx := span.objIndex(val)
1374                         if span.isFree(idx) {
1375                                 return ' '
1376                         }
1377                         return '*'
1378                 })
1379                 printunlock()
1380         }
1381
1382         for i := uintptr(0); i < n; i += goarch.PtrSize {
1383                 if ptrmask != nil {
1384                         word := i / goarch.PtrSize
1385                         bits := *addb(ptrmask, word/8)
1386                         if bits == 0 {
1387                                 // Skip 8 words (the loop increment will do the 8th)
1388                                 //
1389                                 // This must be the first time we've
1390                                 // seen this word of ptrmask, so i
1391                                 // must be 8-word-aligned, but check
1392                                 // our reasoning just in case.
1393                                 if i%(goarch.PtrSize*8) != 0 {
1394                                         throw("misaligned mask")
1395                                 }
1396                                 i += goarch.PtrSize*8 - goarch.PtrSize
1397                                 continue
1398                         }
1399                         if (bits>>(word%8))&1 == 0 {
1400                                 continue
1401                         }
1402                 }
1403
1404                 val := *(*uintptr)(unsafe.Pointer(b + i))
1405
1406                 // Check if val points into the stack.
1407                 if state != nil && state.stack.lo <= val && val < state.stack.hi {
1408                         // val may point to a stack object. This
1409                         // object may be dead from last cycle and
1410                         // hence may contain pointers to unallocated
1411                         // objects, but unlike heap objects we can't
1412                         // tell if it's already dead. Hence, if all
1413                         // pointers to this object are from
1414                         // conservative scanning, we have to scan it
1415                         // defensively, too.
1416                         state.putPtr(val, true)
1417                         continue
1418                 }
1419
1420                 // Check if val points to a heap span.
1421                 span := spanOfHeap(val)
1422                 if span == nil {
1423                         continue
1424                 }
1425
1426                 // Check if val points to an allocated object.
1427                 idx := span.objIndex(val)
1428                 if span.isFree(idx) {
1429                         continue
1430                 }
1431
1432                 // val points to an allocated object. Mark it.
1433                 obj := span.base() + idx*span.elemsize
1434                 greyobject(obj, b, i, span, gcw, idx)
1435         }
1436 }
1437
1438 // Shade the object if it isn't already.
1439 // The object is not nil and known to be in the heap.
1440 // Preemption must be disabled.
1441 //
1442 //go:nowritebarrier
1443 func shade(b uintptr) {
1444         if obj, span, objIndex := findObject(b, 0, 0); obj != 0 {
1445                 gcw := &getg().m.p.ptr().gcw
1446                 greyobject(obj, 0, 0, span, gcw, objIndex)
1447         }
1448 }
1449
1450 // obj is the start of an object with mark mbits.
1451 // If it isn't already marked, mark it and enqueue into gcw.
1452 // base and off are for debugging only and could be removed.
1453 //
1454 // See also wbBufFlush1, which partially duplicates this logic.
1455 //
1456 //go:nowritebarrierrec
1457 func greyobject(obj, base, off uintptr, span *mspan, gcw *gcWork, objIndex uintptr) {
1458         // obj should be start of allocation, and so must be at least pointer-aligned.
1459         if obj&(goarch.PtrSize-1) != 0 {
1460                 throw("greyobject: obj not pointer-aligned")
1461         }
1462         mbits := span.markBitsForIndex(objIndex)
1463
1464         if useCheckmark {
1465                 if setCheckmark(obj, base, off, mbits) {
1466                         // Already marked.
1467                         return
1468                 }
1469         } else {
1470                 if debug.gccheckmark > 0 && span.isFree(objIndex) {
1471                         print("runtime: marking free object ", hex(obj), " found at *(", hex(base), "+", hex(off), ")\n")
1472                         gcDumpObject("base", base, off)
1473                         gcDumpObject("obj", obj, ^uintptr(0))
1474                         getg().m.traceback = 2
1475                         throw("marking free object")
1476                 }
1477
1478                 // If marked we have nothing to do.
1479                 if mbits.isMarked() {
1480                         return
1481                 }
1482                 mbits.setMarked()
1483
1484                 // Mark span.
1485                 arena, pageIdx, pageMask := pageIndexOf(span.base())
1486                 if arena.pageMarks[pageIdx]&pageMask == 0 {
1487                         atomic.Or8(&arena.pageMarks[pageIdx], pageMask)
1488                 }
1489
1490                 // If this is a noscan object, fast-track it to black
1491                 // instead of greying it.
1492                 if span.spanclass.noscan() {
1493                         gcw.bytesMarked += uint64(span.elemsize)
1494                         return
1495                 }
1496         }
1497
1498         // We're adding obj to P's local workbuf, so it's likely
1499         // this object will be processed soon by the same P.
1500         // Even if the workbuf gets flushed, there will likely still be
1501         // some benefit on platforms with inclusive shared caches.
1502         sys.Prefetch(obj)
1503         // Queue the obj for scanning.
1504         if !gcw.putFast(obj) {
1505                 gcw.put(obj)
1506         }
1507 }
1508
1509 // gcDumpObject dumps the contents of obj for debugging and marks the
1510 // field at byte offset off in obj.
1511 func gcDumpObject(label string, obj, off uintptr) {
1512         s := spanOf(obj)
1513         print(label, "=", hex(obj))
1514         if s == nil {
1515                 print(" s=nil\n")
1516                 return
1517         }
1518         print(" s.base()=", hex(s.base()), " s.limit=", hex(s.limit), " s.spanclass=", s.spanclass, " s.elemsize=", s.elemsize, " s.state=")
1519         if state := s.state.get(); 0 <= state && int(state) < len(mSpanStateNames) {
1520                 print(mSpanStateNames[state], "\n")
1521         } else {
1522                 print("unknown(", state, ")\n")
1523         }
1524
1525         skipped := false
1526         size := s.elemsize
1527         if s.state.get() == mSpanManual && size == 0 {
1528                 // We're printing something from a stack frame. We
1529                 // don't know how big it is, so just show up to an
1530                 // including off.
1531                 size = off + goarch.PtrSize
1532         }
1533         for i := uintptr(0); i < size; i += goarch.PtrSize {
1534                 // For big objects, just print the beginning (because
1535                 // that usually hints at the object's type) and the
1536                 // fields around off.
1537                 if !(i < 128*goarch.PtrSize || off-16*goarch.PtrSize < i && i < off+16*goarch.PtrSize) {
1538                         skipped = true
1539                         continue
1540                 }
1541                 if skipped {
1542                         print(" ...\n")
1543                         skipped = false
1544                 }
1545                 print(" *(", label, "+", i, ") = ", hex(*(*uintptr)(unsafe.Pointer(obj + i))))
1546                 if i == off {
1547                         print(" <==")
1548                 }
1549                 print("\n")
1550         }
1551         if skipped {
1552                 print(" ...\n")
1553         }
1554 }
1555
1556 // gcmarknewobject marks a newly allocated object black. obj must
1557 // not contain any non-nil pointers.
1558 //
1559 // This is nosplit so it can manipulate a gcWork without preemption.
1560 //
1561 //go:nowritebarrier
1562 //go:nosplit
1563 func gcmarknewobject(span *mspan, obj, size uintptr) {
1564         if useCheckmark { // The world should be stopped so this should not happen.
1565                 throw("gcmarknewobject called while doing checkmark")
1566         }
1567
1568         // Mark object.
1569         objIndex := span.objIndex(obj)
1570         span.markBitsForIndex(objIndex).setMarked()
1571
1572         // Mark span.
1573         arena, pageIdx, pageMask := pageIndexOf(span.base())
1574         if arena.pageMarks[pageIdx]&pageMask == 0 {
1575                 atomic.Or8(&arena.pageMarks[pageIdx], pageMask)
1576         }
1577
1578         gcw := &getg().m.p.ptr().gcw
1579         gcw.bytesMarked += uint64(size)
1580 }
1581
1582 // gcMarkTinyAllocs greys all active tiny alloc blocks.
1583 //
1584 // The world must be stopped.
1585 func gcMarkTinyAllocs() {
1586         assertWorldStopped()
1587
1588         for _, p := range allp {
1589                 c := p.mcache
1590                 if c == nil || c.tiny == 0 {
1591                         continue
1592                 }
1593                 _, span, objIndex := findObject(c.tiny, 0, 0)
1594                 gcw := &p.gcw
1595                 greyobject(c.tiny, 0, 0, span, gcw, objIndex)
1596         }
1597 }