summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBen Gamari <ben@smart-cactus.org>2022-10-20 22:42:14 +0000
committerMarge Bot <ben+marge-bot@smart-cactus.org>2023-03-08 15:02:30 -0500
commit92227b6022b35d87f6366c75e09ed495b7c3603e (patch)
treeaba882da3bffbc4f39800a19398f4b8dcc4e4b72
parentc4e6bfc801a79b73e94d363db1d3e65076e17981 (diff)
downloadhaskell-92227b6022b35d87f6366c75e09ed495b7c3603e.tar.gz
nonmoving: Fix handling of weak pointers
This fixes an interaction between aging and weak pointer handling which prevented the finalization of some weak pointers. In particular, weak pointers could have their keys incorrectly marked by the preparatory collector, preventing their finalization by the subsequent concurrent collection. While in the area, we also significantly improve the assertions regarding weak pointers. Fixes #22327.
-rw-r--r--rts/RtsStartup.c1
-rw-r--r--rts/sm/MarkWeak.c78
-rw-r--r--rts/sm/NonMoving.c116
-rw-r--r--rts/sm/NonMoving.h1
-rw-r--r--rts/sm/NonMovingMark.c44
-rw-r--r--rts/sm/NonMovingMark.h1
6 files changed, 162 insertions, 79 deletions
diff --git a/rts/RtsStartup.c b/rts/RtsStartup.c
index 83f43bf619..5cb94c71e8 100644
--- a/rts/RtsStartup.c
+++ b/rts/RtsStartup.c
@@ -488,6 +488,7 @@ hs_exit_(bool wait_foreign)
for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
runAllCFinalizers(generations[g].weak_ptr_list);
}
+ runAllCFinalizers(nonmoving_weak_ptr_list);
#if defined(RTS_USER_SIGNALS)
if (RtsFlags.MiscFlags.install_signal_handlers) {
diff --git a/rts/sm/MarkWeak.c b/rts/sm/MarkWeak.c
index 99383ebd42..0cac3e7769 100644
--- a/rts/sm/MarkWeak.c
+++ b/rts/sm/MarkWeak.c
@@ -50,7 +50,7 @@
- weak_stage == WeakPtrs
- We process all the weak pointers whos keys are alive (evacuate
+ We process all the weak pointers whose keys are alive (evacuate
their values and finalizers), and repeat until we can find no new
live keys. If no live keys are found in this pass, then we
evacuate the finalizers of all the dead weak pointers in order to
@@ -82,12 +82,46 @@ static bool tidyWeakList (generation *gen);
static bool resurrectUnreachableThreads (generation *gen, StgTSO **resurrected_threads);
static void tidyThreadList (generation *gen);
+/*
+ * Note [Weak pointer processing and the non-moving GC]
+ * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ * When using the non-moving GC we defer weak pointer processing
+ * until the concurrent marking phase as weaks in the non-moving heap may be
+ * keyed on objects living in the non-moving generation. To accomplish this
+ * initWeakForGC keeps all weak pointers on oldest_gen->weak_ptr_list, where
+ * nonmovingCollect will find them. From there they will be moved to
+ * nonmoving_old_weak_ptr_list. During the mark loop we will move weaks with
+ * reachable keys to nonmoving_weak_ptr_list. At the end of concurrent marking
+ * we tidy the weak list (in nonmovingTidyWeakList) and perform another set of
+ * marking as necessary, just as is done in tidyWeakList.
+ *
+ * Note that this treatment takes advantage of the fact that we usually need
+ * not worry about Weak#s living in the non-moving heap but being keyed on an
+ * object in the moving heap since the Weak# must be strictly older than the
+ * key. Such objects would otherwise pose a problem since the non-moving
+ * collector would be unable to safely determine the liveness of the key.
+ * In the rare case that we *do* see such a key (e.g. in the case of a
+ * pinned ByteArray# living in a partially-filled accumulator block)
+ * the nonmoving collector assumes that it is live.
+ *
+ */
+
+/*
+ * Prepare the weak object lists for GC. Specifically, reset weak_stage
+ * and move all generations' `weak_ptr_list`s to `old_weak_ptr_list`.
+ * Weaks with live keys will later be moved back to `weak_ptr_list` by
+ * `tidyWeakList`.
+ */
void
initWeakForGC(void)
{
- uint32_t g;
+ uint32_t oldest = N;
+ if (RtsFlags.GcFlags.useNonmoving && N == oldest_gen->no) {
+ // See Note [Weak pointer processing and the non-moving GC].
+ oldest = oldest_gen->no - 1;
+ }
- for (g = 0; g <= N; g++) {
+ for (uint32_t g = 0; g <= oldest; g++) {
generation *gen = &generations[g];
gen->old_weak_ptr_list = gen->weak_ptr_list;
gen->weak_ptr_list = NULL;
@@ -96,6 +130,14 @@ initWeakForGC(void)
weak_stage = WeakThreads;
}
+/*
+ * Walk the weak pointer lists after having finished a round of scavenging,
+ * tidying the weak (and possibly thread) lists (depending upon the current
+ * weak_stage).
+ *
+ * Returns true if new live weak pointers were found, implying that another
+ * round of scavenging is necessary.
+ */
bool
traverseWeakPtrList(StgWeak **dead_weak_ptr_list, StgTSO **resurrected_threads)
{
@@ -182,6 +224,11 @@ traverseWeakPtrList(StgWeak **dead_weak_ptr_list, StgTSO **resurrected_threads)
}
}
+/*
+ * Deal with weak pointers with unreachable keys after GC has concluded.
+ * This means marking the finalizer (and possibly value) in preparation for
+ * later finalization.
+ */
static void collectDeadWeakPtrs (generation *gen, StgWeak **dead_weak_ptr_list)
{
StgWeak *w, *next_w;
@@ -198,6 +245,10 @@ static void collectDeadWeakPtrs (generation *gen, StgWeak **dead_weak_ptr_list)
}
}
+/*
+ * Deal with threads left on the old_threads list after GC has concluded,
+ * moving them onto the resurrected_threads list where appropriate.
+ */
static bool resurrectUnreachableThreads (generation *gen, StgTSO **resurrected_threads)
{
StgTSO *t, *tmp, *next;
@@ -233,8 +284,21 @@ static bool resurrectUnreachableThreads (generation *gen, StgTSO **resurrected_t
return flag;
}
+/*
+ * Walk over the `old_weak_ptr_list` of the given generation and:
+ *
+ * - remove any DEAD_WEAKs
+ * - move any weaks with reachable keys to the `weak_ptr_list` of the
+ * appropriate to-space and mark the weak's value and finalizer.
+ */
static bool tidyWeakList(generation *gen)
{
+ if (RtsFlags.GcFlags.useNonmoving && gen == oldest_gen) {
+ // See Note [Weak pointer processing and the non-moving GC].
+ ASSERT(gen->old_weak_ptr_list == NULL);
+ return false;
+ }
+
StgWeak *w, **last_w, *next_w;
const StgInfoTable *info;
StgClosure *new;
@@ -322,6 +386,10 @@ static bool tidyWeakList(generation *gen)
return flag;
}
+/*
+ * Walk over the `old_threads` list of the given generation and move any
+ * reachable threads onto the `threads` list.
+ */
static void tidyThreadList (generation *gen)
{
StgTSO *t, *tmp, *next, **prev;
@@ -381,6 +449,10 @@ static void checkWeakPtrSanity(StgWeak *hd, StgWeak *tl)
}
#endif
+/*
+ * Traverse the capabilities' local new-weak-pointer lists at the beginning of
+ * GC and move them to the nursery's weak_ptr_list.
+ */
void collectFreshWeakPtrs()
{
uint32_t i;
diff --git a/rts/sm/NonMoving.c b/rts/sm/NonMoving.c
index 84dc82c1bb..697cdea6da 100644
--- a/rts/sm/NonMoving.c
+++ b/rts/sm/NonMoving.c
@@ -25,7 +25,7 @@
#include "NonMovingSweep.h"
#include "NonMovingCensus.h"
#include "StablePtr.h" // markStablePtrTable
-#include "Weak.h" // dead_weak_ptr_list
+#include "Weak.h" // scheduleFinalizers
struct NonmovingHeap nonmovingHeap;
@@ -247,6 +247,9 @@ Mutex concurrent_coll_finished_lock;
* how we use the DIRTY flags associated with MUT_VARs and TVARs to improve
* barrier efficiency.
*
+ * - Note [Weak pointer processing and the non-moving GC] (MarkWeak.c) describes
+ * how weak pointers are handled when the non-moving GC is in use.
+ *
* [ueno 2016]:
* Katsuhiro Ueno and Atsushi Ohori. 2016. A fully concurrent garbage
* collector for functional programs on multicore processors. SIGPLAN Not. 51,
@@ -285,8 +288,8 @@ Mutex concurrent_coll_finished_lock;
* was (unsurprisingly) also found to result in significant amounts of
* unnecessary copying.
*
- * Consequently, we now allow aging. Aging allows the preparatory GC leading up
- * to a major collection to evacuate some objects into the young generation.
+ * Consequently, we now allow "aging", allows the preparatory GC leading up
+ * to a major collection to evacuate objects into the young generation.
* However, this introduces the following tricky case that might arise after
* we have finished the preparatory GC:
*
@@ -295,6 +298,7 @@ Mutex concurrent_coll_finished_lock;
* ┆
* B ←────────────── A ←─────────────── root
* │ ┆ ↖─────────────── gen1 mut_list
+ * │ ┆
* ╰───────────────→ C
* ┆
*
@@ -335,8 +339,9 @@ Mutex concurrent_coll_finished_lock;
* The implementation details of this are described in Note [Non-moving GC:
* Marking evacuated objects] in Evac.c.
*
- * Note [Deadlock detection under nonmoving collector]
- * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ *
+ * Note [Deadlock detection under the non-moving collector]
+ * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* In GHC the garbage collector is responsible for identifying deadlocked
* programs. Providing for this responsibility is slightly tricky in the
* non-moving collector due to the existence of aging. In particular, the
@@ -895,43 +900,6 @@ static void nonmovingPrepareMark(void)
#endif
}
-// Mark weak pointers in the non-moving heap. They'll either end up in
-// dead_weak_ptr_list or stay in weak_ptr_list. Either way they need to be kept
-// during sweep. See `MarkWeak.c:markWeakPtrList` for the moving heap variant
-// of this.
-static void nonmovingMarkWeakPtrList(MarkQueue *mark_queue, StgWeak *dead_weak_ptr_list)
-{
- for (StgWeak *w = oldest_gen->weak_ptr_list; w; w = w->link) {
- markQueuePushClosureGC(mark_queue, (StgClosure*)w);
- // Do not mark finalizers and values here, those fields will be marked
- // in `nonmovingMarkDeadWeaks` (for dead weaks) or
- // `nonmovingTidyWeaks` (for live weaks)
- }
-
- // We need to mark dead_weak_ptr_list too. This is subtle:
- //
- // - By the beginning of this GC we evacuated all weaks to the non-moving
- // heap (in `markWeakPtrList`)
- //
- // - During the scavenging of the moving heap we discovered that some of
- // those weaks are dead and moved them to `dead_weak_ptr_list`. Note that
- // because of the fact above _all weaks_ are in the non-moving heap at
- // this point.
- //
- // - So, to be able to traverse `dead_weak_ptr_list` and run finalizers we
- // need to mark it.
- for (StgWeak *w = dead_weak_ptr_list; w; w = w->link) {
- markQueuePushClosureGC(mark_queue, (StgClosure*)w);
-
- // Mark the value and finalizer since they will be needed regardless of
- // whether we find the weak is live.
- if (w->cfinalizers != &stg_NO_FINALIZER_closure) {
- markQueuePushClosureGC(mark_queue, w->value);
- }
- markQueuePushClosureGC(mark_queue, w->finalizer);
- }
-}
-
void nonmovingCollect(StgWeak **dead_weaks, StgTSO **resurrected_threads)
{
#if defined(THREADED_RTS)
@@ -964,9 +932,16 @@ void nonmovingCollect(StgWeak **dead_weaks, StgTSO **resurrected_threads)
markCapability((evac_fn)markQueueAddRoot, mark_queue,
getCapability(n), true/*don't mark sparks*/);
}
- nonmovingMarkWeakPtrList(mark_queue, *dead_weaks);
markStablePtrTable((evac_fn)markQueueAddRoot, mark_queue);
+ // The dead weak pointer list shouldn't contain any weaks in the
+ // nonmoving heap
+#if defined(DEBUG)
+ for (StgWeak *w = *dead_weaks; w; w = w->link) {
+ ASSERT(Bdescr((StgPtr) w)->gen != oldest_gen);
+ }
+#endif
+
// Mark threads resurrected during moving heap scavenging
for (StgTSO *tso = *resurrected_threads; tso != END_TSO_QUEUE; tso = tso->global_link) {
markQueuePushClosureGC(mark_queue, (StgClosure*)tso);
@@ -992,8 +967,23 @@ void nonmovingCollect(StgWeak **dead_weaks, StgTSO **resurrected_threads)
// alive).
ASSERT(oldest_gen->old_weak_ptr_list == NULL);
ASSERT(nonmoving_old_weak_ptr_list == NULL);
- nonmoving_old_weak_ptr_list = oldest_gen->weak_ptr_list;
- oldest_gen->weak_ptr_list = NULL;
+ {
+ // Move both oldest_gen->weak_ptr_list and nonmoving_weak_ptr_list to
+ // nonmoving_old_weak_ptr_list
+ StgWeak **weaks = &oldest_gen->weak_ptr_list;
+ uint32_t n = 0;
+ while (*weaks) {
+ weaks = &(*weaks)->link;
+ n++;
+ }
+ debugTrace(DEBUG_nonmoving_gc, "%d new nonmoving weaks", n);
+ *weaks = nonmoving_weak_ptr_list;
+ nonmoving_old_weak_ptr_list = oldest_gen->weak_ptr_list;
+ nonmoving_weak_ptr_list = NULL;
+ oldest_gen->weak_ptr_list = NULL;
+ // At this point all weaks in the nonmoving generation are on
+ // nonmoving_old_weak_ptr_list
+ }
trace(TRACE_nonmoving_gc, "Finished nonmoving GC preparation");
// We are now safe to start concurrent marking
@@ -1057,7 +1047,6 @@ static void* nonmovingConcurrentMark(void *data)
return NULL;
}
-// TODO: Not sure where to put this function.
// Append w2 to the end of w1.
static void appendWeakList( StgWeak **w1, StgWeak *w2 )
{
@@ -1098,6 +1087,9 @@ static void nonmovingMark_(MarkQueue *mark_queue, StgWeak **dead_weaks, StgTSO *
}
}
+ // Mark Weak#s
+ nonmovingMarkWeakPtrList(mark_queue);
+
// Do concurrent marking; most of the heap will get marked here.
nonmovingMarkThreadsWeaks(mark_queue);
@@ -1108,21 +1100,13 @@ static void nonmovingMark_(MarkQueue *mark_queue, StgWeak **dead_weaks, StgTSO *
if (getSchedState() > SCHED_RUNNING) {
// Note that we break our invariants here and leave segments in
// nonmovingHeap.sweep_list, don't free nonmoving_large_objects etc.
- // However because we won't be running mark-sweep in the final GC this
+ // However because we won't be running sweep in the final GC this
// is OK.
-
- // This is a RTS shutdown so we need to move our copy (snapshot) of
- // weaks (nonmoving_old_weak_ptr_list and nonmoving_weak_ptr_list) to
- // oldest_gen->threads to be able to run C finalizers in hs_exit_. Note
- // that there may be more weaks added to oldest_gen->threads since we
- // started mark, so we need to append our list to the tail of
- // oldest_gen->threads.
- appendWeakList(&nonmoving_old_weak_ptr_list, nonmoving_weak_ptr_list);
- appendWeakList(&oldest_gen->weak_ptr_list, nonmoving_old_weak_ptr_list);
- // These lists won't be used again so this is not necessary, but still
- nonmoving_old_weak_ptr_list = NULL;
- nonmoving_weak_ptr_list = NULL;
-
+ //
+ // However, we must move any weak pointers remaining on
+ // nonmoving_old_weak_ptr_list back to nonmoving_weak_ptr_list
+ // such that their C finalizers can be run by hs_exit_.
+ appendWeakList(&nonmoving_weak_ptr_list, nonmoving_old_weak_ptr_list);
goto finish;
}
@@ -1194,15 +1178,9 @@ static void nonmovingMark_(MarkQueue *mark_queue, StgWeak **dead_weaks, StgTSO *
nonmoving_old_threads = END_TSO_QUEUE;
}
- {
- StgWeak **weaks = &oldest_gen->weak_ptr_list;
- while (*weaks) {
- weaks = &(*weaks)->link;
- }
- *weaks = nonmoving_weak_ptr_list;
- nonmoving_weak_ptr_list = NULL;
- nonmoving_old_weak_ptr_list = NULL;
- }
+ // At this point point any weak that remains on nonmoving_old_weak_ptr_list
+ // has a dead key.
+ nonmoving_old_weak_ptr_list = NULL;
// Prune spark lists
// See Note [Spark management under the nonmoving collector].
diff --git a/rts/sm/NonMoving.h b/rts/sm/NonMoving.h
index fcbf6ca89b..45ece3aa6a 100644
--- a/rts/sm/NonMoving.h
+++ b/rts/sm/NonMoving.h
@@ -298,7 +298,6 @@ INLINE_HEADER void nonmovingSetClosureMark(StgPtr p)
nonmovingSetMark(nonmovingGetSegment(p), nonmovingGetBlockIdx(p));
}
-// TODO: Audit the uses of these
/* Was the given closure marked this major GC cycle? */
INLINE_HEADER bool nonmovingClosureMarkedThisCycle(StgPtr p)
{
diff --git a/rts/sm/NonMovingMark.c b/rts/sm/NonMovingMark.c
index e4c1c57d36..f91bcbd58d 100644
--- a/rts/sm/NonMovingMark.c
+++ b/rts/sm/NonMovingMark.c
@@ -37,6 +37,7 @@ static void trace_PAP_payload (MarkQueue *queue,
StgClosure *fun,
StgClosure **payload,
StgWord size);
+static bool is_nonmoving_weak(StgWeak *weak);
// How many Array# entries to add to the mark queue at once?
#define MARK_ARRAY_CHUNK_LENGTH 128
@@ -1525,10 +1526,12 @@ mark_closure (MarkQueue *queue, const StgClosure *p0, StgClosure **origin)
break;
}
+ case WEAK:
+ ASSERT(is_nonmoving_weak((StgWeak*) p));
+ // fallthrough
gen_obj:
case CONSTR:
case CONSTR_NOCAF:
- case WEAK:
case PRIM:
{
for (StgWord i = 0; i < info->layout.payload.ptrs; i++) {
@@ -1897,6 +1900,28 @@ static bool nonmovingIsNowAlive (StgClosure *p)
}
}
+// Mark all Weak#s on nonmoving_old_weak_ptr_list.
+void nonmovingMarkWeakPtrList (struct MarkQueue_ *queue)
+{
+ ASSERT(nonmoving_weak_ptr_list == NULL);
+ for (StgWeak *w = nonmoving_old_weak_ptr_list; w != NULL; w = w->link) {
+ mark_closure(queue, (StgClosure *) w, NULL);
+ }
+}
+
+// Determine whether a weak pointer object is on one of the nonmoving
+// collector's weak pointer lists. Used for sanity checking.
+static bool is_nonmoving_weak(StgWeak *weak)
+{
+ for (StgWeak *w = nonmoving_old_weak_ptr_list; w != NULL; w = w->link) {
+ if (w == weak) return true;
+ }
+ for (StgWeak *w = nonmoving_weak_ptr_list; w != NULL; w = w->link) {
+ if (w == weak) return true;
+ }
+ return false;
+}
+
// Non-moving heap variant of `tidyWeakList`
bool nonmovingTidyWeaks (struct MarkQueue_ *queue)
{
@@ -1905,6 +1930,9 @@ bool nonmovingTidyWeaks (struct MarkQueue_ *queue)
StgWeak **last_w = &nonmoving_old_weak_ptr_list;
StgWeak *next_w;
for (StgWeak *w = nonmoving_old_weak_ptr_list; w != NULL; w = next_w) {
+ // This should have been marked by nonmovingMarkWeaks
+ ASSERT(nonmovingIsNowAlive((StgClosure *) w));
+
if (w->header.info == &stg_DEAD_WEAK_info) {
// finalizeWeak# was called on the weak
next_w = w->link;
@@ -1915,7 +1943,10 @@ bool nonmovingTidyWeaks (struct MarkQueue_ *queue)
// Otherwise it's a live weak
ASSERT(w->header.info == &stg_WEAK_info);
- if (nonmovingIsNowAlive(w->key)) {
+ // See Note [Weak pointer processing and the non-moving GC] in
+ // MarkWeak.c
+ bool key_in_nonmoving = Bdescr((StgPtr) w->key)->flags & BF_NONMOVING;
+ if (!key_in_nonmoving || nonmovingIsNowAlive(w->key)) {
nonmovingMarkLiveWeak(queue, w);
did_work = true;
@@ -1923,7 +1954,7 @@ bool nonmovingTidyWeaks (struct MarkQueue_ *queue)
*last_w = w->link;
next_w = w->link;
- // and put it on the weak ptr list
+ // and put it on nonmoving_weak_ptr_list
w->link = nonmoving_weak_ptr_list;
nonmoving_weak_ptr_list = w;
} else {
@@ -1945,7 +1976,8 @@ void nonmovingMarkDeadWeak (struct MarkQueue_ *queue, StgWeak *w)
void nonmovingMarkLiveWeak (struct MarkQueue_ *queue, StgWeak *w)
{
- ASSERT(nonmovingClosureMarkedThisCycle((P_)w));
+ ASSERT(nonmovingIsNowAlive((StgClosure *) w));
+ ASSERT(nonmovingIsNowAlive((StgClosure *) w->key));
markQueuePushClosure_(queue, w->value);
markQueuePushClosure_(queue, w->finalizer);
markQueuePushClosure_(queue, w->cfinalizers);
@@ -1959,9 +1991,9 @@ void nonmovingMarkDeadWeaks (struct MarkQueue_ *queue, StgWeak **dead_weaks)
{
StgWeak *next_w;
for (StgWeak *w = nonmoving_old_weak_ptr_list; w; w = next_w) {
- ASSERT(!nonmovingClosureMarkedThisCycle((P_)(w->key)));
+ ASSERT(!nonmovingIsNowAlive(w->key));
nonmovingMarkDeadWeak(queue, w);
- next_w = w ->link;
+ next_w = w->link;
w->link = *dead_weaks;
*dead_weaks = w;
}
diff --git a/rts/sm/NonMovingMark.h b/rts/sm/NonMovingMark.h
index 04859215b1..7d4fd3ccb4 100644
--- a/rts/sm/NonMovingMark.h
+++ b/rts/sm/NonMovingMark.h
@@ -157,6 +157,7 @@ void initMarkQueue(MarkQueue *queue);
void freeMarkQueue(MarkQueue *queue);
void nonmovingMark(struct MarkQueue_ *STG_RESTRICT queue);
+void nonmovingMarkWeakPtrList(struct MarkQueue_ *queue);
bool nonmovingTidyWeaks(struct MarkQueue_ *queue);
void nonmovingTidyThreads(void);
void nonmovingMarkDeadWeaks(struct MarkQueue_ *queue, StgWeak **dead_weak_ptr_list);