git: 935509ba185d - stable/13 - reap_kill_subtree_once(): handle proctree_lock unlock in reap_kill_proc()

From: Konstantin Belousov <kib_at_FreeBSD.org>
Date: Fri, 24 Jun 2022 19:37:07 UTC
The branch stable/13 has been updated by kib:

URL: https://cgit.FreeBSD.org/src/commit/?id=935509ba185d9c2781fad2bc6bb8ec77a2534a78

commit 935509ba185d9c2781fad2bc6bb8ec77a2534a78
Author:     Konstantin Belousov <kib@FreeBSD.org>
AuthorDate: 2022-05-09 21:41:23 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2022-06-24 14:45:46 +0000

    reap_kill_subtree_once(): handle proctree_lock unlock in reap_kill_proc()
    
    (cherry picked from commit 1d4abf2cfa6e71f4e29ab1f1a6d366d11f3d6027)
---
 sys/kern/kern_procctl.c | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)

diff --git a/sys/kern/kern_procctl.c b/sys/kern/kern_procctl.c
index d6c29ed5894a..02469f25bc3d 100644
--- a/sys/kern/kern_procctl.c
+++ b/sys/kern/kern_procctl.c
@@ -370,8 +370,20 @@ reap_kill_subtree_once(struct thread *td, struct proc *p, struct proc *reaper,
 	TAILQ_INIT(&tracker);
 	reap_kill_sched(&tracker, reaper);
 	while ((t = TAILQ_FIRST(&tracker)) != NULL) {
-		MPASS((t->parent->p_treeflag & P_TREE_REAPER) != 0);
 		TAILQ_REMOVE(&tracker, t, link);
+
+		/*
+		 * Since reap_kill_proc() drops proctree_lock sx, it
+		 * is possible that the tracked reaper is no longer.
+		 * In this case the subtree is reparented to the new
+		 * reaper, which should handle it.
+		 */
+		if ((t->parent->p_treeflag & P_TREE_REAPER) == 0) {
+			free(t, M_TEMP);
+			res = true;
+			continue;
+		}
+
 		LIST_FOREACH(p2, &t->parent->p_reaplist, p_reapsibling) {
 			if (t->parent == reaper &&
 			    (rk->rk_flags & REAPER_KILL_SUBTREE) != 0 &&