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