git: 2e7595ef2f53 - main - Add stop_all_proc_block(9)

From: Konstantin Belousov <kib_at_FreeBSD.org>
Date: Wed, 27 Apr 2022 23:27:52 UTC
The branch main has been updated by kib:

URL: https://cgit.FreeBSD.org/src/commit/?id=2e7595ef2f53f6debec3c3d89bb9e0cd10394ed3

commit 2e7595ef2f53f6debec3c3d89bb9e0cd10394ed3
Author:     Konstantin Belousov <kib@FreeBSD.org>
AuthorDate: 2022-04-23 22:55:04 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2022-04-27 23:27:35 +0000

    Add stop_all_proc_block(9)
    
    It allows to have more than one consumer of thread_signle(SIGNLE_ALLPROC) by
    serializing them.
    
    Reviewed by:    markj
    Tested by:      pho
    Sponsored by:   The FreeBSD Foundation
    MFC after:      1 week
    Differential revision:  https://reviews.freebsd.org/D35014
---
 sys/kern/kern_proc.c | 19 +++++++++++++++++++
 sys/sys/proc.h       |  2 ++
 2 files changed, 21 insertions(+)

diff --git a/sys/kern/kern_proc.c b/sys/kern/kern_proc.c
index d5aae09f2c4e..cd528d47830d 100644
--- a/sys/kern/kern_proc.c
+++ b/sys/kern/kern_proc.c
@@ -3396,6 +3396,21 @@ static SYSCTL_NODE(_kern_proc, KERN_PROC_VM_LAYOUT, vm_layout, CTLFLAG_RD |
 	CTLFLAG_ANYBODY | CTLFLAG_MPSAFE, sysctl_kern_proc_vm_layout,
 	"Process virtual address space layout info");
 
+static struct sx stop_all_proc_blocker;
+SX_SYSINIT(stop_all_proc_blocker, &stop_all_proc_blocker, "sapblk");
+
+void
+stop_all_proc_block(void)
+{
+	sx_xlock(&stop_all_proc_blocker);
+}
+
+void
+stop_all_proc_unblock(void)
+{
+	sx_xunlock(&stop_all_proc_blocker);
+}
+
 int allproc_gen;
 
 /*
@@ -3411,6 +3426,8 @@ stop_all_proc(void)
 	int r, gen;
 	bool restart, seen_stopped, seen_exiting, stopped_some;
 
+	stop_all_proc_block();
+
 	cp = curproc;
 allproc_loop:
 	sx_xlock(&allproc_lock);
@@ -3502,6 +3519,8 @@ again:
 			goto again;
 	}
 	sx_xunlock(&allproc_lock);
+
+	stop_all_proc_unblock();
 }
 
 /* #define	TOTAL_STOP_DEBUG	1 */
diff --git a/sys/sys/proc.h b/sys/sys/proc.h
index 2556fd574d84..223714724aca 100644
--- a/sys/sys/proc.h
+++ b/sys/sys/proc.h
@@ -1232,6 +1232,8 @@ void	thread_unlink(struct thread *td);
 void	thread_unsuspend(struct proc *p);
 void	thread_wait(struct proc *p);
 
+void	stop_all_proc_block(void);
+void	stop_all_proc_unblock(void);
 void	stop_all_proc(void);
 void	resume_all_proc(void);