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
7package runtime
8
9import (
10	"runtime/internal/atomic"
11	"runtime/internal/sys"
12	"unsafe"
13)
14
15const (
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	// rootBlockSpans is the number of spans to scan per span
25	// root.
26	rootBlockSpans = 8 * 1024 // 64MB worth of spans
27
28	// maxObletBytes is the maximum bytes of an object to scan at
29	// once. Larger objects will be split up into "oblets" of at
30	// most this size. Since we can scan 1–2 MB/ms, 128 KB bounds
31	// scan preemption at ~100 µs.
32	//
33	// This must be > _MaxSmallSize so that the object base is the
34	// span base.
35	maxObletBytes = 128 << 10
36
37	// drainCheckThreshold specifies how many units of work to do
38	// between self-preemption checks in gcDrain. Assuming a scan
39	// rate of 1 MB/ms, this is ~100 µs. Lower values have higher
40	// overhead in the scan loop (the scheduler check may perform
41	// a syscall, so its overhead is nontrivial). Higher values
42	// make the system less responsive to incoming work.
43	drainCheckThreshold = 100000
44)
45
46// gcMarkRootPrepare queues root scanning jobs (stacks, globals, and
47// some miscellany) and initializes scanning-related state.
48//
49// The world must be stopped.
50//
51//go:nowritebarrier
52func gcMarkRootPrepare() {
53	work.nFlushCacheRoots = 0
54
55	work.nDataRoots = 0
56
57	// Only scan globals once per cycle; preferably concurrently.
58	roots := gcRoots
59	for roots != nil {
60		work.nDataRoots++
61		roots = roots.next
62	}
63
64	// Scan span roots for finalizer specials.
65	//
66	// We depend on addfinalizer to mark objects that get
67	// finalizers after root marking.
68	//
69	// We're only interested in scanning the in-use spans,
70	// which will all be swept at this point. More spans
71	// may be added to this list during concurrent GC, but
72	// we only care about spans that were allocated before
73	// this mark phase.
74	work.nSpanRoots = mheap_.sweepSpans[mheap_.sweepgen/2%2].numBlocks()
75
76	// Scan stacks.
77	//
78	// Gs may be created after this point, but it's okay that we
79	// ignore them because they begin life without any roots, so
80	// there's nothing to scan, and any roots they create during
81	// the concurrent phase will be scanned during mark
82	// termination.
83	work.nStackRoots = int(atomic.Loaduintptr(&allglen))
84
85	work.markrootNext = 0
86	work.markrootJobs = uint32(fixedRootCount + work.nFlushCacheRoots + work.nDataRoots + work.nSpanRoots + work.nStackRoots)
87}
88
89// gcMarkRootCheck checks that all roots have been scanned. It is
90// purely for debugging.
91func gcMarkRootCheck() {
92	if work.markrootNext < work.markrootJobs {
93		print(work.markrootNext, " of ", work.markrootJobs, " markroot jobs done\n")
94		throw("left over markroot jobs")
95	}
96
97	lock(&allglock)
98	// Check that stacks have been scanned.
99	var gp *g
100	for i := 0; i < work.nStackRoots; i++ {
101		gp = allgs[i]
102		if !gp.gcscandone {
103			goto fail
104		}
105	}
106	unlock(&allglock)
107	return
108
109fail:
110	println("gp", gp, "goid", gp.goid,
111		"status", readgstatus(gp),
112		"gcscandone", gp.gcscandone)
113	unlock(&allglock) // Avoid self-deadlock with traceback.
114	throw("scan missed a g")
115}
116
117// ptrmask for an allocation containing a single pointer.
118var oneptrmask = [...]uint8{1}
119
120// markroot scans the i'th root.
121//
122// Preemption must be disabled (because this uses a gcWork).
123//
124// nowritebarrier is only advisory here.
125//
126//go:nowritebarrier
127func markroot(gcw *gcWork, i uint32) {
128	// TODO(austin): This is a bit ridiculous. Compute and store
129	// the bases in gcMarkRootPrepare instead of the counts.
130	baseFlushCache := uint32(fixedRootCount)
131	baseData := baseFlushCache + uint32(work.nFlushCacheRoots)
132	baseSpans := baseData + uint32(work.nDataRoots)
133	baseStacks := baseSpans + uint32(work.nSpanRoots)
134	end := baseStacks + uint32(work.nStackRoots)
135
136	// Note: if you add a case here, please also update heapdump.go:dumproots.
137	switch {
138	case baseFlushCache <= i && i < baseData:
139		flushmcache(int(i - baseFlushCache))
140
141	case baseData <= i && i < baseSpans:
142		roots := gcRoots
143		c := baseData
144		for roots != nil {
145			if i == c {
146				markrootBlock(roots, gcw)
147				break
148			}
149			roots = roots.next
150			c++
151		}
152
153	case i == fixedRootFinalizers:
154		for fb := allfin; fb != nil; fb = fb.alllink {
155			cnt := uintptr(atomic.Load(&fb.cnt))
156			scanblock(uintptr(unsafe.Pointer(&fb.fin[0])), cnt*unsafe.Sizeof(fb.fin[0]), &finptrmask[0], gcw)
157		}
158
159	case i == fixedRootFreeGStacks:
160		// FIXME: We don't do this for gccgo.
161
162	case baseSpans <= i && i < baseStacks:
163		// mark mspan.specials
164		markrootSpans(gcw, int(i-baseSpans))
165
166	default:
167		// the rest is scanning goroutine stacks
168		var gp *g
169		if baseStacks <= i && i < end {
170			gp = allgs[i-baseStacks]
171		} else {
172			throw("markroot: bad index")
173		}
174
175		// remember when we've first observed the G blocked
176		// needed only to output in traceback
177		status := readgstatus(gp) // We are not in a scan state
178		if (status == _Gwaiting || status == _Gsyscall) && gp.waitsince == 0 {
179			gp.waitsince = work.tstart
180		}
181
182		// scanstack must be done on the system stack in case
183		// we're trying to scan our own stack.
184		systemstack(func() {
185			// If this is a self-scan, put the user G in
186			// _Gwaiting to prevent self-deadlock. It may
187			// already be in _Gwaiting if this is a mark
188			// worker or we're in mark termination.
189			userG := getg().m.curg
190			selfScan := gp == userG && readgstatus(userG) == _Grunning
191			if selfScan {
192				casgstatus(userG, _Grunning, _Gwaiting)
193				userG.waitreason = waitReasonGarbageCollectionScan
194			}
195
196			// TODO: suspendG blocks (and spins) until gp
197			// stops, which may take a while for
198			// running goroutines. Consider doing this in
199			// two phases where the first is non-blocking:
200			// we scan the stacks we can and ask running
201			// goroutines to scan themselves; and the
202			// second blocks.
203			stopped := suspendG(gp)
204			if stopped.dead {
205				gp.gcscandone = true
206				return
207			}
208			if gp.gcscandone {
209				throw("g already scanned")
210			}
211			scanstack(gp, gcw)
212			gp.gcscandone = true
213			resumeG(stopped)
214
215			if selfScan {
216				casgstatus(userG, _Gwaiting, _Grunning)
217			}
218		})
219	}
220}
221
222// markrootBlock scans one element of the list of GC roots.
223//
224//go:nowritebarrier
225func markrootBlock(roots *gcRootList, gcw *gcWork) {
226	for i := 0; i < roots.count; i++ {
227		r := &roots.roots[i]
228		scanblock(uintptr(r.decl), r.ptrdata, r.gcdata, gcw)
229	}
230}
231
232// markrootSpans marks roots for one shard of work.spans.
233//
234//go:nowritebarrier
235func markrootSpans(gcw *gcWork, shard int) {
236	// Objects with finalizers have two GC-related invariants:
237	//
238	// 1) Everything reachable from the object must be marked.
239	// This ensures that when we pass the object to its finalizer,
240	// everything the finalizer can reach will be retained.
241	//
242	// 2) Finalizer specials (which are not in the garbage
243	// collected heap) are roots. In practice, this means the fn
244	// field must be scanned.
245	//
246	// TODO(austin): There are several ideas for making this more
247	// efficient in issue #11485.
248
249	sg := mheap_.sweepgen
250	spans := mheap_.sweepSpans[mheap_.sweepgen/2%2].block(shard)
251	// Note that work.spans may not include spans that were
252	// allocated between entering the scan phase and now. We may
253	// also race with spans being added into sweepSpans when they're
254	// just created, and as a result we may see nil pointers in the
255	// spans slice. This is okay because any objects with finalizers
256	// in those spans must have been allocated and given finalizers
257	// after we entered the scan phase, so addfinalizer will have
258	// ensured the above invariants for them.
259	for i := 0; i < len(spans); i++ {
260		// sweepBuf.block requires that we read pointers from the block atomically.
261		// It also requires that we ignore nil pointers.
262		s := (*mspan)(atomic.Loadp(unsafe.Pointer(&spans[i])))
263
264		// This is racing with spans being initialized, so
265		// check the state carefully.
266		if s == nil || s.state.get() != mSpanInUse {
267			continue
268		}
269		// Check that this span was swept (it may be cached or uncached).
270		if !useCheckmark && !(s.sweepgen == sg || s.sweepgen == sg+3) {
271			// sweepgen was updated (+2) during non-checkmark GC pass
272			print("sweep ", s.sweepgen, " ", sg, "\n")
273			throw("gc: unswept span")
274		}
275
276		// Speculatively check if there are any specials
277		// without acquiring the span lock. This may race with
278		// adding the first special to a span, but in that
279		// case addfinalizer will observe that the GC is
280		// active (which is globally synchronized) and ensure
281		// the above invariants. We may also ensure the
282		// invariants, but it's okay to scan an object twice.
283		if s.specials == nil {
284			continue
285		}
286
287		// Lock the specials to prevent a special from being
288		// removed from the list while we're traversing it.
289		lock(&s.speciallock)
290
291		for sp := s.specials; sp != nil; sp = sp.next {
292			if sp.kind != _KindSpecialFinalizer {
293				continue
294			}
295			// don't mark finalized object, but scan it so we
296			// retain everything it points to.
297			spf := (*specialfinalizer)(unsafe.Pointer(sp))
298			// A finalizer can be set for an inner byte of an object, find object beginning.
299			p := s.base() + uintptr(spf.special.offset)/s.elemsize*s.elemsize
300
301			// Mark everything that can be reached from
302			// the object (but *not* the object itself or
303			// we'll never collect it).
304			scanobject(p, gcw)
305
306			// The special itself is a root.
307			scanblock(uintptr(unsafe.Pointer(&spf.fn)), sys.PtrSize, &oneptrmask[0], gcw)
308		}
309
310		unlock(&s.speciallock)
311	}
312}
313
314// gcAssistAlloc performs GC work to make gp's assist debt positive.
315// gp must be the calling user gorountine.
316//
317// This must be called with preemption enabled.
318func gcAssistAlloc(gp *g) {
319	// Don't assist in non-preemptible contexts. These are
320	// generally fragile and won't allow the assist to block.
321	if getg() == gp.m.g0 {
322		return
323	}
324	if mp := getg().m; mp.locks > 0 || mp.preemptoff != "" {
325		return
326	}
327
328	traced := false
329retry:
330	// Compute the amount of scan work we need to do to make the
331	// balance positive. When the required amount of work is low,
332	// we over-assist to build up credit for future allocations
333	// and amortize the cost of assisting.
334	debtBytes := -gp.gcAssistBytes
335	scanWork := int64(gcController.assistWorkPerByte * float64(debtBytes))
336	if scanWork < gcOverAssistWork {
337		scanWork = gcOverAssistWork
338		debtBytes = int64(gcController.assistBytesPerWork * float64(scanWork))
339	}
340
341	// Steal as much credit as we can from the background GC's
342	// scan credit. This is racy and may drop the background
343	// credit below 0 if two mutators steal at the same time. This
344	// will just cause steals to fail until credit is accumulated
345	// again, so in the long run it doesn't really matter, but we
346	// do have to handle the negative credit case.
347	bgScanCredit := atomic.Loadint64(&gcController.bgScanCredit)
348	stolen := int64(0)
349	if bgScanCredit > 0 {
350		if bgScanCredit < scanWork {
351			stolen = bgScanCredit
352			gp.gcAssistBytes += 1 + int64(gcController.assistBytesPerWork*float64(stolen))
353		} else {
354			stolen = scanWork
355			gp.gcAssistBytes += debtBytes
356		}
357		atomic.Xaddint64(&gcController.bgScanCredit, -stolen)
358
359		scanWork -= stolen
360
361		if scanWork == 0 {
362			// We were able to steal all of the credit we
363			// needed.
364			if traced {
365				traceGCMarkAssistDone()
366			}
367			return
368		}
369	}
370
371	if trace.enabled && !traced {
372		traced = true
373		traceGCMarkAssistStart()
374	}
375
376	// Perform assist work
377	systemstack(func() {
378		gcAssistAlloc1(gp, scanWork)
379		// The user stack may have moved, so this can't touch
380		// anything on it until it returns from systemstack.
381	})
382
383	completed := gp.param != nil
384	gp.param = nil
385	if completed {
386		gcMarkDone()
387	}
388
389	if gp.gcAssistBytes < 0 {
390		// We were unable steal enough credit or perform
391		// enough work to pay off the assist debt. We need to
392		// do one of these before letting the mutator allocate
393		// more to prevent over-allocation.
394		//
395		// If this is because we were preempted, reschedule
396		// and try some more.
397		if gp.preempt {
398			Gosched()
399			goto retry
400		}
401
402		// Add this G to an assist queue and park. When the GC
403		// has more background credit, it will satisfy queued
404		// assists before flushing to the global credit pool.
405		//
406		// Note that this does *not* get woken up when more
407		// work is added to the work list. The theory is that
408		// there wasn't enough work to do anyway, so we might
409		// as well let background marking take care of the
410		// work that is available.
411		if !gcParkAssist() {
412			goto retry
413		}
414
415		// At this point either background GC has satisfied
416		// this G's assist debt, or the GC cycle is over.
417	}
418	if traced {
419		traceGCMarkAssistDone()
420	}
421}
422
423// gcAssistAlloc1 is the part of gcAssistAlloc that runs on the system
424// stack. This is a separate function to make it easier to see that
425// we're not capturing anything from the user stack, since the user
426// stack may move while we're in this function.
427//
428// gcAssistAlloc1 indicates whether this assist completed the mark
429// phase by setting gp.param to non-nil. This can't be communicated on
430// the stack since it may move.
431//
432//go:systemstack
433func gcAssistAlloc1(gp *g, scanWork int64) {
434	// Clear the flag indicating that this assist completed the
435	// mark phase.
436	gp.param = nil
437
438	if atomic.Load(&gcBlackenEnabled) == 0 {
439		// The gcBlackenEnabled check in malloc races with the
440		// store that clears it but an atomic check in every malloc
441		// would be a performance hit.
442		// Instead we recheck it here on the non-preemptable system
443		// stack to determine if we should perform an assist.
444
445		// GC is done, so ignore any remaining debt.
446		gp.gcAssistBytes = 0
447		return
448	}
449	// Track time spent in this assist. Since we're on the
450	// system stack, this is non-preemptible, so we can
451	// just measure start and end time.
452	startTime := nanotime()
453
454	decnwait := atomic.Xadd(&work.nwait, -1)
455	if decnwait == work.nproc {
456		println("runtime: work.nwait =", decnwait, "work.nproc=", work.nproc)
457		throw("nwait > work.nprocs")
458	}
459
460	// gcDrainN requires the caller to be preemptible.
461	casgstatus(gp, _Grunning, _Gwaiting)
462	gp.waitreason = waitReasonGCAssistMarking
463
464	// drain own cached work first in the hopes that it
465	// will be more cache friendly.
466	gcw := &getg().m.p.ptr().gcw
467	workDone := gcDrainN(gcw, scanWork)
468
469	casgstatus(gp, _Gwaiting, _Grunning)
470
471	// Record that we did this much scan work.
472	//
473	// Back out the number of bytes of assist credit that
474	// this scan work counts for. The "1+" is a poor man's
475	// round-up, to ensure this adds credit even if
476	// assistBytesPerWork is very low.
477	gp.gcAssistBytes += 1 + int64(gcController.assistBytesPerWork*float64(workDone))
478
479	// If this is the last worker and we ran out of work,
480	// signal a completion point.
481	incnwait := atomic.Xadd(&work.nwait, +1)
482	if incnwait > work.nproc {
483		println("runtime: work.nwait=", incnwait,
484			"work.nproc=", work.nproc)
485		throw("work.nwait > work.nproc")
486	}
487
488	if incnwait == work.nproc && !gcMarkWorkAvailable(nil) {
489		// This has reached a background completion point. Set
490		// gp.param to a non-nil value to indicate this. It
491		// doesn't matter what we set it to (it just has to be
492		// a valid pointer).
493		gp.param = unsafe.Pointer(gp)
494	}
495	duration := nanotime() - startTime
496	_p_ := gp.m.p.ptr()
497	_p_.gcAssistTime += duration
498	if _p_.gcAssistTime > gcAssistTimeSlack {
499		atomic.Xaddint64(&gcController.assistTime, _p_.gcAssistTime)
500		_p_.gcAssistTime = 0
501	}
502}
503
504// gcWakeAllAssists wakes all currently blocked assists. This is used
505// at the end of a GC cycle. gcBlackenEnabled must be false to prevent
506// new assists from going to sleep after this point.
507func gcWakeAllAssists() {
508	lock(&work.assistQueue.lock)
509	list := work.assistQueue.q.popList()
510	injectglist(&list)
511	unlock(&work.assistQueue.lock)
512}
513
514// gcParkAssist puts the current goroutine on the assist queue and parks.
515//
516// gcParkAssist reports whether the assist is now satisfied. If it
517// returns false, the caller must retry the assist.
518//
519//go:nowritebarrier
520func gcParkAssist() bool {
521	lock(&work.assistQueue.lock)
522	// If the GC cycle finished while we were getting the lock,
523	// exit the assist. The cycle can't finish while we hold the
524	// lock.
525	if atomic.Load(&gcBlackenEnabled) == 0 {
526		unlock(&work.assistQueue.lock)
527		return true
528	}
529
530	gp := getg()
531	oldList := work.assistQueue.q
532	work.assistQueue.q.pushBack(gp)
533
534	// Recheck for background credit now that this G is in
535	// the queue, but can still back out. This avoids a
536	// race in case background marking has flushed more
537	// credit since we checked above.
538	if atomic.Loadint64(&gcController.bgScanCredit) > 0 {
539		work.assistQueue.q = oldList
540		if oldList.tail != 0 {
541			oldList.tail.ptr().schedlink.set(nil)
542		}
543		unlock(&work.assistQueue.lock)
544		return false
545	}
546	// Park.
547	goparkunlock(&work.assistQueue.lock, waitReasonGCAssistWait, traceEvGoBlockGC, 2)
548	return true
549}
550
551// gcFlushBgCredit flushes scanWork units of background scan work
552// credit. This first satisfies blocked assists on the
553// work.assistQueue and then flushes any remaining credit to
554// gcController.bgScanCredit.
555//
556// Write barriers are disallowed because this is used by gcDrain after
557// it has ensured that all work is drained and this must preserve that
558// condition.
559//
560//go:nowritebarrierrec
561func gcFlushBgCredit(scanWork int64) {
562	if work.assistQueue.q.empty() {
563		// Fast path; there are no blocked assists. There's a
564		// small window here where an assist may add itself to
565		// the blocked queue and park. If that happens, we'll
566		// just get it on the next flush.
567		atomic.Xaddint64(&gcController.bgScanCredit, scanWork)
568		return
569	}
570
571	scanBytes := int64(float64(scanWork) * gcController.assistBytesPerWork)
572
573	lock(&work.assistQueue.lock)
574	for !work.assistQueue.q.empty() && scanBytes > 0 {
575		gp := work.assistQueue.q.pop()
576		// Note that gp.gcAssistBytes is negative because gp
577		// is in debt. Think carefully about the signs below.
578		if scanBytes+gp.gcAssistBytes >= 0 {
579			// Satisfy this entire assist debt.
580			scanBytes += gp.gcAssistBytes
581			gp.gcAssistBytes = 0
582			// It's important that we *not* put gp in
583			// runnext. Otherwise, it's possible for user
584			// code to exploit the GC worker's high
585			// scheduler priority to get itself always run
586			// before other goroutines and always in the
587			// fresh quantum started by GC.
588			ready(gp, 0, false)
589		} else {
590			// Partially satisfy this assist.
591			gp.gcAssistBytes += scanBytes
592			scanBytes = 0
593			// As a heuristic, we move this assist to the
594			// back of the queue so that large assists
595			// can't clog up the assist queue and
596			// substantially delay small assists.
597			work.assistQueue.q.pushBack(gp)
598			break
599		}
600	}
601
602	if scanBytes > 0 {
603		// Convert from scan bytes back to work.
604		scanWork = int64(float64(scanBytes) * gcController.assistWorkPerByte)
605		atomic.Xaddint64(&gcController.bgScanCredit, scanWork)
606	}
607	unlock(&work.assistQueue.lock)
608}
609
610// We use a C function to find the stack.
611// Returns whether we succesfully scanned the stack.
612func doscanstack(*g, *gcWork) bool
613
614func doscanstackswitch(*g, *g)
615
616// scanstack scans gp's stack, greying all pointers found on the stack.
617//
618// scanstack will also shrink the stack if it is safe to do so. If it
619// is not, it schedules a stack shrink for the next synchronous safe
620// point.
621//
622// scanstack is marked go:systemstack because it must not be preempted
623// while using a workbuf.
624//
625//go:nowritebarrier
626//go:systemstack
627func scanstack(gp *g, gcw *gcWork) {
628	if readgstatus(gp)&_Gscan == 0 {
629		print("runtime:scanstack: gp=", gp, ", goid=", gp.goid, ", gp->atomicstatus=", hex(readgstatus(gp)), "\n")
630		throw("scanstack - bad status")
631	}
632
633	switch readgstatus(gp) &^ _Gscan {
634	default:
635		print("runtime: gp=", gp, ", goid=", gp.goid, ", gp->atomicstatus=", readgstatus(gp), "\n")
636		throw("mark - bad status")
637	case _Gdead:
638		return
639	case _Grunning:
640		print("runtime: gp=", gp, ", goid=", gp.goid, ", gp->atomicstatus=", readgstatus(gp), "\n")
641		throw("scanstack: goroutine not stopped")
642	case _Grunnable, _Gsyscall, _Gwaiting:
643		// ok
644	}
645
646	// Scan the stack.
647	if usestackmaps {
648		g := getg()
649		if g == gp {
650			// Scan its own stack.
651			doscanstack(gp, gcw)
652		} else if gp.entry != nil {
653			// This is a newly created g that hasn't run. No stack to scan.
654		} else if readgstatus(gp)&^_Gscan == _Gsyscall {
655			scanSyscallStack(gp, gcw)
656		} else {
657			// Scanning another g's stack. We need to switch to that g
658			// to unwind its stack. And switch back after scan.
659			scanstackswitch(gp, gcw)
660		}
661	} else {
662		doscanstack(gp, gcw)
663
664		// Conservatively scan the saved register values.
665		scanstackblock(uintptr(unsafe.Pointer(&gp.gcregs)), unsafe.Sizeof(gp.gcregs), gcw)
666		scanstackblock(uintptr(unsafe.Pointer(&gp.context)), unsafe.Sizeof(gp.context), gcw)
667	}
668
669	// Note: in the gc runtime scanstack also scans defer records.
670	// This is necessary as it uses stack objects (a.k.a. stack tracing).
671	// We don't (yet) do stack objects, and regular stack/heap scan
672	// will take care of defer records just fine.
673}
674
675// scanstackswitch scans gp's stack by switching (gogo) to gp and
676// letting it scan its own stack, and switching back upon finish.
677//
678//go:nowritebarrier
679func scanstackswitch(gp *g, gcw *gcWork) {
680	g := getg()
681
682	// We are on the system stack which prevents preemption. But
683	// we are going to switch to g stack. Lock m to block preemption.
684	mp := acquirem()
685
686	// The doscanstackswitch function will modify the current g's
687	// context. Preserve it.
688	// The stack scan code may call systemstack, which will modify
689	// gp's context. Preserve it as well so we can resume gp.
690	context := g.context
691	stackcontext := g.stackcontext
692	context2 := gp.context
693	stackcontext2 := gp.stackcontext
694
695	gp.scangcw = uintptr(unsafe.Pointer(gcw))
696	gp.scang = uintptr(unsafe.Pointer(g))
697	doscanstackswitch(g, gp)
698
699	// Restore the contexts.
700	g.context = context
701	g.stackcontext = stackcontext
702	gp.context = context2
703	gp.stackcontext = stackcontext2
704	gp.scangcw = 0
705	// gp.scang is already cleared in C code.
706
707	releasem(mp)
708}
709
710// scanSyscallStack scans the stack of a goroutine blocked in a
711// syscall by waking it up and asking it to scan its own stack.
712func scanSyscallStack(gp *g, gcw *gcWork) {
713	if gp.scanningself {
714		// We've suspended the goroutine by setting the _Gscan bit,
715		// so this shouldn't be possible.
716		throw("scanSyscallStack: scanningself")
717	}
718	if gp.gcscandone {
719		// We've suspended the goroutine by setting the _Gscan bit,
720		// so this shouldn't be possible.
721
722		throw("scanSyscallStack: gcscandone")
723	}
724
725	gp.gcScannedSyscallStack = false
726	for {
727		mp := gp.m
728		noteclear(&mp.scannote)
729		gp.scangcw = uintptr(unsafe.Pointer(gcw))
730		tgkill(getpid(), _pid_t(mp.procid), _SIGURG)
731		// Wait for gp to scan its own stack.
732		notesleep(&mp.scannote)
733		if gp.gcScannedSyscallStack {
734			return
735		}
736
737		// The signal was delivered at a bad time.  Try again.
738		osyield()
739	}
740}
741
742type gcDrainFlags int
743
744const (
745	gcDrainUntilPreempt gcDrainFlags = 1 << iota
746	gcDrainFlushBgCredit
747	gcDrainIdle
748	gcDrainFractional
749)
750
751// gcDrain scans roots and objects in work buffers, blackening grey
752// objects until it is unable to get more work. It may return before
753// GC is done; it's the caller's responsibility to balance work from
754// other Ps.
755//
756// If flags&gcDrainUntilPreempt != 0, gcDrain returns when g.preempt
757// is set.
758//
759// If flags&gcDrainIdle != 0, gcDrain returns when there is other work
760// to do.
761//
762// If flags&gcDrainFractional != 0, gcDrain self-preempts when
763// pollFractionalWorkerExit() returns true. This implies
764// gcDrainNoBlock.
765//
766// If flags&gcDrainFlushBgCredit != 0, gcDrain flushes scan work
767// credit to gcController.bgScanCredit every gcCreditSlack units of
768// scan work.
769//
770//go:nowritebarrier
771func gcDrain(gcw *gcWork, flags gcDrainFlags) {
772	if !writeBarrier.needed {
773		throw("gcDrain phase incorrect")
774	}
775
776	gp := getg().m.curg
777	preemptible := flags&gcDrainUntilPreempt != 0
778	flushBgCredit := flags&gcDrainFlushBgCredit != 0
779	idle := flags&gcDrainIdle != 0
780
781	initScanWork := gcw.scanWork
782
783	// checkWork is the scan work before performing the next
784	// self-preempt check.
785	checkWork := int64(1<<63 - 1)
786	var check func() bool
787	if flags&(gcDrainIdle|gcDrainFractional) != 0 {
788		checkWork = initScanWork + drainCheckThreshold
789		if idle {
790			check = pollWork
791		} else if flags&gcDrainFractional != 0 {
792			check = pollFractionalWorkerExit
793		}
794	}
795
796	// Drain root marking jobs.
797	if work.markrootNext < work.markrootJobs {
798		for !(preemptible && gp.preempt) {
799			job := atomic.Xadd(&work.markrootNext, +1) - 1
800			if job >= work.markrootJobs {
801				break
802			}
803			markroot(gcw, job)
804			if check != nil && check() {
805				goto done
806			}
807		}
808	}
809
810	// Drain heap marking jobs.
811	for !(preemptible && gp.preempt) {
812		// Try to keep work available on the global queue. We used to
813		// check if there were waiting workers, but it's better to
814		// just keep work available than to make workers wait. In the
815		// worst case, we'll do O(log(_WorkbufSize)) unnecessary
816		// balances.
817		if work.full == 0 {
818			gcw.balance()
819		}
820
821		b := gcw.tryGetFast()
822		if b == 0 {
823			b = gcw.tryGet()
824			if b == 0 {
825				// Flush the write barrier
826				// buffer; this may create
827				// more work.
828				wbBufFlush(nil, 0)
829				b = gcw.tryGet()
830			}
831		}
832		if b == 0 {
833			// Unable to get work.
834			break
835		}
836		scanobject(b, gcw)
837
838		// Flush background scan work credit to the global
839		// account if we've accumulated enough locally so
840		// mutator assists can draw on it.
841		if gcw.scanWork >= gcCreditSlack {
842			atomic.Xaddint64(&gcController.scanWork, gcw.scanWork)
843			if flushBgCredit {
844				gcFlushBgCredit(gcw.scanWork - initScanWork)
845				initScanWork = 0
846			}
847			checkWork -= gcw.scanWork
848			gcw.scanWork = 0
849
850			if checkWork <= 0 {
851				checkWork += drainCheckThreshold
852				if check != nil && check() {
853					break
854				}
855			}
856		}
857	}
858
859done:
860	// Flush remaining scan work credit.
861	if gcw.scanWork > 0 {
862		atomic.Xaddint64(&gcController.scanWork, gcw.scanWork)
863		if flushBgCredit {
864			gcFlushBgCredit(gcw.scanWork - initScanWork)
865		}
866		gcw.scanWork = 0
867	}
868}
869
870// gcDrainN blackens grey objects until it has performed roughly
871// scanWork units of scan work or the G is preempted. This is
872// best-effort, so it may perform less work if it fails to get a work
873// buffer. Otherwise, it will perform at least n units of work, but
874// may perform more because scanning is always done in whole object
875// increments. It returns the amount of scan work performed.
876//
877// The caller goroutine must be in a preemptible state (e.g.,
878// _Gwaiting) to prevent deadlocks during stack scanning. As a
879// consequence, this must be called on the system stack.
880//
881//go:nowritebarrier
882//go:systemstack
883func gcDrainN(gcw *gcWork, scanWork int64) int64 {
884	if !writeBarrier.needed {
885		throw("gcDrainN phase incorrect")
886	}
887
888	// There may already be scan work on the gcw, which we don't
889	// want to claim was done by this call.
890	workFlushed := -gcw.scanWork
891
892	gp := getg().m.curg
893	for !gp.preempt && workFlushed+gcw.scanWork < scanWork {
894		// See gcDrain comment.
895		if work.full == 0 {
896			gcw.balance()
897		}
898
899		// This might be a good place to add prefetch code...
900		// if(wbuf.nobj > 4) {
901		//         PREFETCH(wbuf->obj[wbuf.nobj - 3];
902		//  }
903		//
904		b := gcw.tryGetFast()
905		if b == 0 {
906			b = gcw.tryGet()
907			if b == 0 {
908				// Flush the write barrier buffer;
909				// this may create more work.
910				wbBufFlush(nil, 0)
911				b = gcw.tryGet()
912			}
913		}
914
915		if b == 0 {
916			// Try to do a root job.
917			//
918			// TODO: Assists should get credit for this
919			// work.
920			if work.markrootNext < work.markrootJobs {
921				job := atomic.Xadd(&work.markrootNext, +1) - 1
922				if job < work.markrootJobs {
923					markroot(gcw, job)
924					continue
925				}
926			}
927			// No heap or root jobs.
928			break
929		}
930		scanobject(b, gcw)
931
932		// Flush background scan work credit.
933		if gcw.scanWork >= gcCreditSlack {
934			atomic.Xaddint64(&gcController.scanWork, gcw.scanWork)
935			workFlushed += gcw.scanWork
936			gcw.scanWork = 0
937		}
938	}
939
940	// Unlike gcDrain, there's no need to flush remaining work
941	// here because this never flushes to bgScanCredit and
942	// gcw.dispose will flush any remaining work to scanWork.
943
944	return workFlushed + gcw.scanWork
945}
946
947// scanblock scans b as scanobject would, but using an explicit
948// pointer bitmap instead of the heap bitmap.
949//
950// This is used to scan non-heap roots, so it does not update
951// gcw.bytesMarked or gcw.scanWork.
952//
953//go:nowritebarrier
954func scanblock(b0, n0 uintptr, ptrmask *uint8, gcw *gcWork) {
955	// Use local copies of original parameters, so that a stack trace
956	// due to one of the throws below shows the original block
957	// base and extent.
958	b := b0
959	n := n0
960
961	for i := uintptr(0); i < n; {
962		// Find bits for the next word.
963		bits := uint32(*addb(ptrmask, i/(sys.PtrSize*8)))
964		if bits == 0 {
965			i += sys.PtrSize * 8
966			continue
967		}
968		for j := 0; j < 8 && i < n; j++ {
969			if bits&1 != 0 {
970				// Same work as in scanobject; see comments there.
971				p := *(*uintptr)(unsafe.Pointer(b + i))
972				if p != 0 {
973					if obj, span, objIndex := findObject(p, b, i, false); obj != 0 {
974						greyobject(obj, b, i, span, gcw, objIndex, false)
975					}
976				}
977			}
978			bits >>= 1
979			i += sys.PtrSize
980		}
981	}
982}
983
984// scanobject scans the object starting at b, adding pointers to gcw.
985// b must point to the beginning of a heap object or an oblet.
986// scanobject consults the GC bitmap for the pointer mask and the
987// spans for the size of the object.
988//
989//go:nowritebarrier
990func scanobject(b uintptr, gcw *gcWork) {
991	// Find the bits for b and the size of the object at b.
992	//
993	// b is either the beginning of an object, in which case this
994	// is the size of the object to scan, or it points to an
995	// oblet, in which case we compute the size to scan below.
996	hbits := heapBitsForAddr(b)
997	s := spanOfUnchecked(b)
998	n := s.elemsize
999	if n == 0 {
1000		throw("scanobject n == 0")
1001	}
1002
1003	if n > maxObletBytes {
1004		// Large object. Break into oblets for better
1005		// parallelism and lower latency.
1006		if b == s.base() {
1007			// It's possible this is a noscan object (not
1008			// from greyobject, but from other code
1009			// paths), in which case we must *not* enqueue
1010			// oblets since their bitmaps will be
1011			// uninitialized.
1012			if s.spanclass.noscan() {
1013				// Bypass the whole scan.
1014				gcw.bytesMarked += uint64(n)
1015				return
1016			}
1017
1018			// Enqueue the other oblets to scan later.
1019			// Some oblets may be in b's scalar tail, but
1020			// these will be marked as "no more pointers",
1021			// so we'll drop out immediately when we go to
1022			// scan those.
1023			for oblet := b + maxObletBytes; oblet < s.base()+s.elemsize; oblet += maxObletBytes {
1024				if !gcw.putFast(oblet) {
1025					gcw.put(oblet)
1026				}
1027			}
1028		}
1029
1030		// Compute the size of the oblet. Since this object
1031		// must be a large object, s.base() is the beginning
1032		// of the object.
1033		n = s.base() + s.elemsize - b
1034		if n > maxObletBytes {
1035			n = maxObletBytes
1036		}
1037	}
1038
1039	var i uintptr
1040	for i = 0; i < n; i += sys.PtrSize {
1041		// Find bits for this word.
1042		if i != 0 {
1043			// Avoid needless hbits.next() on last iteration.
1044			hbits = hbits.next()
1045		}
1046		// Load bits once. See CL 22712 and issue 16973 for discussion.
1047		bits := hbits.bits()
1048		// During checkmarking, 1-word objects store the checkmark
1049		// in the type bit for the one word. The only one-word objects
1050		// are pointers, or else they'd be merged with other non-pointer
1051		// data into larger allocations.
1052		if i != 1*sys.PtrSize && bits&bitScan == 0 {
1053			break // no more pointers in this object
1054		}
1055		if bits&bitPointer == 0 {
1056			continue // not a pointer
1057		}
1058
1059		// Work here is duplicated in scanblock and above.
1060		// If you make changes here, make changes there too.
1061		obj := *(*uintptr)(unsafe.Pointer(b + i))
1062
1063		// At this point we have extracted the next potential pointer.
1064		// Quickly filter out nil and pointers back to the current object.
1065		if obj != 0 && obj-b >= n {
1066			// Test if obj points into the Go heap and, if so,
1067			// mark the object.
1068			//
1069			// Note that it's possible for findObject to
1070			// fail if obj points to a just-allocated heap
1071			// object because of a race with growing the
1072			// heap. In this case, we know the object was
1073			// just allocated and hence will be marked by
1074			// allocation itself.
1075			if obj, span, objIndex := findObject(obj, b, i, false); obj != 0 {
1076				greyobject(obj, b, i, span, gcw, objIndex, false)
1077			}
1078		}
1079	}
1080	gcw.bytesMarked += uint64(n)
1081	gcw.scanWork += int64(i)
1082}
1083
1084//go:linkname scanstackblock
1085
1086// scanstackblock is called by the stack scanning code in C to
1087// actually find and mark pointers in the stack block. This is like
1088// scanblock, but we scan the stack conservatively, so there is no
1089// bitmask of pointers.
1090func scanstackblock(b, n uintptr, gcw *gcWork) {
1091	if usestackmaps {
1092		throw("scanstackblock: conservative scan but stack map is used")
1093	}
1094
1095	for i := uintptr(0); i < n; i += sys.PtrSize {
1096		// Same work as in scanobject; see comments there.
1097		obj := *(*uintptr)(unsafe.Pointer(b + i))
1098		if obj, span, objIndex := findObject(obj, b, i, true); obj != 0 {
1099			greyobject(obj, b, i, span, gcw, objIndex, true)
1100		}
1101	}
1102}
1103
1104// scanstackblockwithmap is like scanstackblock, but with an explicit
1105// pointer bitmap. This is used only when precise stack scan is enabled.
1106//go:linkname scanstackblockwithmap
1107//go:nowritebarrier
1108func scanstackblockwithmap(pc, b0, n0 uintptr, ptrmask *uint8, gcw *gcWork) {
1109	// Use local copies of original parameters, so that a stack trace
1110	// due to one of the throws below shows the original block
1111	// base and extent.
1112	b := b0
1113	n := n0
1114
1115	for i := uintptr(0); i < n; {
1116		// Find bits for the next word.
1117		bits := uint32(*addb(ptrmask, i/(sys.PtrSize*8)))
1118		if bits == 0 {
1119			i += sys.PtrSize * 8
1120			continue
1121		}
1122		for j := 0; j < 8 && i < n; j++ {
1123			if bits&1 != 0 {
1124				// Same work as in scanobject; see comments there.
1125				obj := *(*uintptr)(unsafe.Pointer(b + i))
1126				if obj != 0 {
1127					o, span, objIndex := findObject(obj, b, i, false)
1128					if obj < minPhysPageSize ||
1129						span != nil && span.state.get() != mSpanManual &&
1130							(obj < span.base() || obj >= span.limit || span.state.get() != mSpanInUse) {
1131						print("runtime: found in object at *(", hex(b), "+", hex(i), ") = ", hex(obj), ", pc=", hex(pc), "\n")
1132						name, file, line, _ := funcfileline(pc, -1, false)
1133						print(name, "\n", file, ":", line, "\n")
1134						//gcDumpObject("object", b, i)
1135						throw("found bad pointer in Go stack (incorrect use of unsafe or cgo?)")
1136					}
1137					if o != 0 {
1138						greyobject(o, b, i, span, gcw, objIndex, false)
1139					}
1140				}
1141			}
1142			bits >>= 1
1143			i += sys.PtrSize
1144		}
1145	}
1146}
1147
1148// Shade the object if it isn't already.
1149// The object is not nil and known to be in the heap.
1150// Preemption must be disabled.
1151//go:nowritebarrier
1152func shade(b uintptr) {
1153	if obj, span, objIndex := findObject(b, 0, 0, !usestackmaps); obj != 0 {
1154		gcw := &getg().m.p.ptr().gcw
1155		greyobject(obj, 0, 0, span, gcw, objIndex, !usestackmaps)
1156	}
1157}
1158
1159// obj is the start of an object with mark mbits.
1160// If it isn't already marked, mark it and enqueue into gcw.
1161// base and off are for debugging only and could be removed.
1162//
1163// See also wbBufFlush1, which partially duplicates this logic.
1164//
1165//go:nowritebarrierrec
1166func greyobject(obj, base, off uintptr, span *mspan, gcw *gcWork, objIndex uintptr, forStack bool) {
1167	// obj should be start of allocation, and so must be at least pointer-aligned.
1168	if obj&(sys.PtrSize-1) != 0 {
1169		throw("greyobject: obj not pointer-aligned")
1170	}
1171	mbits := span.markBitsForIndex(objIndex)
1172
1173	if useCheckmark {
1174		if !mbits.isMarked() {
1175			// Stack scanning is conservative, so we can
1176			// see a reference to an object not previously
1177			// found. Assume the object was correctly not
1178			// marked and ignore the pointer.
1179			if forStack {
1180				return
1181			}
1182			printlock()
1183			print("runtime:greyobject: checkmarks finds unexpected unmarked object obj=", hex(obj), "\n")
1184			print("runtime: found obj at *(", hex(base), "+", hex(off), ")\n")
1185
1186			// Dump the source (base) object
1187			gcDumpObject("base", base, off)
1188
1189			// Dump the object
1190			gcDumpObject("obj", obj, ^uintptr(0))
1191
1192			getg().m.traceback = 2
1193			throw("checkmark found unmarked object")
1194		}
1195		hbits := heapBitsForAddr(obj)
1196		if hbits.isCheckmarked(span.elemsize) {
1197			return
1198		}
1199		hbits.setCheckmarked(span.elemsize)
1200		if !hbits.isCheckmarked(span.elemsize) {
1201			throw("setCheckmarked and isCheckmarked disagree")
1202		}
1203	} else {
1204		// Stack scanning is conservative, so we can see a
1205		// pointer to a free object. Assume the object was
1206		// correctly freed and we must ignore the pointer.
1207		if forStack && span.isFree(objIndex) {
1208			return
1209		}
1210
1211		if debug.gccheckmark > 0 && span.isFree(objIndex) {
1212			print("runtime: marking free object ", hex(obj), " found at *(", hex(base), "+", hex(off), ")\n")
1213			gcDumpObject("base", base, off)
1214			gcDumpObject("obj", obj, ^uintptr(0))
1215			getg().m.traceback = 2
1216			throw("marking free object")
1217		}
1218
1219		// If marked we have nothing to do.
1220		if mbits.isMarked() {
1221			return
1222		}
1223		mbits.setMarked()
1224
1225		// Mark span.
1226		arena, pageIdx, pageMask := pageIndexOf(span.base())
1227		if arena.pageMarks[pageIdx]&pageMask == 0 {
1228			atomic.Or8(&arena.pageMarks[pageIdx], pageMask)
1229		}
1230
1231		// If this is a noscan object, fast-track it to black
1232		// instead of greying it.
1233		if span.spanclass.noscan() {
1234			gcw.bytesMarked += uint64(span.elemsize)
1235			return
1236		}
1237	}
1238
1239	// Queue the obj for scanning. The PREFETCH(obj) logic has been removed but
1240	// seems like a nice optimization that can be added back in.
1241	// There needs to be time between the PREFETCH and the use.
1242	// Previously we put the obj in an 8 element buffer that is drained at a rate
1243	// to give the PREFETCH time to do its work.
1244	// Use of PREFETCHNTA might be more appropriate than PREFETCH
1245	if !gcw.putFast(obj) {
1246		gcw.put(obj)
1247	}
1248}
1249
1250// gcDumpObject dumps the contents of obj for debugging and marks the
1251// field at byte offset off in obj.
1252func gcDumpObject(label string, obj, off uintptr) {
1253	s := spanOf(obj)
1254	print(label, "=", hex(obj))
1255	if s == nil {
1256		print(" s=nil\n")
1257		return
1258	}
1259	print(" s.base()=", hex(s.base()), " s.limit=", hex(s.limit), " s.spanclass=", s.spanclass, " s.elemsize=", s.elemsize, " s.state=")
1260	if state := s.state.get(); 0 <= state && int(state) < len(mSpanStateNames) {
1261		print(mSpanStateNames[state], "\n")
1262	} else {
1263		print("unknown(", state, ")\n")
1264	}
1265
1266	skipped := false
1267	size := s.elemsize
1268	if s.state.get() == mSpanManual && size == 0 {
1269		// We're printing something from a stack frame. We
1270		// don't know how big it is, so just show up to an
1271		// including off.
1272		size = off + sys.PtrSize
1273	}
1274	for i := uintptr(0); i < size; i += sys.PtrSize {
1275		// For big objects, just print the beginning (because
1276		// that usually hints at the object's type) and the
1277		// fields around off.
1278		if !(i < 128*sys.PtrSize || off-16*sys.PtrSize < i && i < off+16*sys.PtrSize) {
1279			skipped = true
1280			continue
1281		}
1282		if skipped {
1283			print(" ...\n")
1284			skipped = false
1285		}
1286		print(" *(", label, "+", i, ") = ", hex(*(*uintptr)(unsafe.Pointer(obj + i))))
1287		if i == off {
1288			print(" <==")
1289		}
1290		print("\n")
1291	}
1292	if skipped {
1293		print(" ...\n")
1294	}
1295}
1296
1297// gcmarknewobject marks a newly allocated object black. obj must
1298// not contain any non-nil pointers.
1299//
1300// This is nosplit so it can manipulate a gcWork without preemption.
1301//
1302//go:nowritebarrier
1303//go:nosplit
1304func gcmarknewobject(obj, size, scanSize uintptr) {
1305	if useCheckmark { // The world should be stopped so this should not happen.
1306		throw("gcmarknewobject called while doing checkmark")
1307	}
1308	markBitsForAddr(obj).setMarked()
1309	gcw := &getg().m.p.ptr().gcw
1310	gcw.bytesMarked += uint64(size)
1311	gcw.scanWork += int64(scanSize)
1312}
1313
1314// gcMarkTinyAllocs greys all active tiny alloc blocks.
1315//
1316// The world must be stopped.
1317func gcMarkTinyAllocs() {
1318	for _, p := range allp {
1319		c := p.mcache
1320		if c == nil || c.tiny == 0 {
1321			continue
1322		}
1323		_, span, objIndex := findObject(c.tiny, 0, 0, false)
1324		gcw := &p.gcw
1325		greyobject(c.tiny, 0, 0, span, gcw, objIndex, false)
1326	}
1327}
1328
1329// Checkmarking
1330
1331// To help debug the concurrent GC we remark with the world
1332// stopped ensuring that any object encountered has their normal
1333// mark bit set. To do this we use an orthogonal bit
1334// pattern to indicate the object is marked. The following pattern
1335// uses the upper two bits in the object's boundary nibble.
1336// 01: scalar  not marked
1337// 10: pointer not marked
1338// 11: pointer     marked
1339// 00: scalar      marked
1340// Xoring with 01 will flip the pattern from marked to unmarked and vica versa.
1341// The higher bit is 1 for pointers and 0 for scalars, whether the object
1342// is marked or not.
1343// The first nibble no longer holds the typeDead pattern indicating that the
1344// there are no more pointers in the object. This information is held
1345// in the second nibble.
1346
1347// If useCheckmark is true, marking of an object uses the
1348// checkmark bits (encoding above) instead of the standard
1349// mark bits.
1350var useCheckmark = false
1351
1352//go:nowritebarrier
1353func initCheckmarks() {
1354	useCheckmark = true
1355	for _, s := range mheap_.allspans {
1356		if s.state.get() == mSpanInUse {
1357			heapBitsForAddr(s.base()).initCheckmarkSpan(s.layout())
1358		}
1359	}
1360}
1361
1362func clearCheckmarks() {
1363	useCheckmark = false
1364	for _, s := range mheap_.allspans {
1365		if s.state.get() == mSpanInUse {
1366			heapBitsForAddr(s.base()).clearCheckmarkSpan(s.layout())
1367		}
1368	}
1369}
1370