git: d64c3f263fcf - stable/13 - Do not single-thread itself when the process single-threaded some another process
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Fri, 24 Jun 2022 19:37:00 UTC
The branch stable/13 has been updated by kib: URL: https://cgit.FreeBSD.org/src/commit/?id=d64c3f263fcfbe350b3fcaadc296f6d95dfe47e9 commit d64c3f263fcfbe350b3fcaadc296f6d95dfe47e9 Author: Konstantin Belousov <kib@FreeBSD.org> AuthorDate: 2022-05-15 21:55:32 +0000 Commit: Konstantin Belousov <kib@FreeBSD.org> CommitDate: 2022-06-24 14:45:46 +0000 Do not single-thread itself when the process single-threaded some another process (cherry picked from commit 4493a13e3bfbbdf8488993843281ec688057ee0f) --- sys/kern/kern_exec.c | 9 +++++++++ sys/kern/kern_exit.c | 11 ++++++++++- sys/kern/kern_fork.c | 11 +++++++++++ sys/kern/kern_procctl.c | 13 +++++++++++++ sys/kern/kern_sig.c | 3 ++- sys/sys/proc.h | 3 +++ 6 files changed, 48 insertions(+), 2 deletions(-) diff --git a/sys/kern/kern_exec.c b/sys/kern/kern_exec.c index d8e1779825c6..1848bf5234bb 100644 --- a/sys/kern/kern_exec.c +++ b/sys/kern/kern_exec.c @@ -307,8 +307,17 @@ pre_execve(struct thread *td, struct vmspace **oldvmspace) p = td->td_proc; if ((p->p_flag & P_HADTHREADS) != 0) { PROC_LOCK(p); + while (p->p_singlethr > 0) { + error = msleep(&p->p_singlethr, &p->p_mtx, + PWAIT | PCATCH, "exec1t", 0); + if (error != 0) { + error = ERESTART; + goto unlock; + } + } if (thread_single(p, SINGLE_BOUNDARY) != 0) error = ERESTART; +unlock: PROC_UNLOCK(p); } KASSERT(error != 0 || (td->td_pflags & TDP_EXECVMSPC) == 0, diff --git a/sys/kern/kern_exit.c b/sys/kern/kern_exit.c index 073174a1a82d..c14c2a7a9cde 100644 --- a/sys/kern/kern_exit.c +++ b/sys/kern/kern_exit.c @@ -213,6 +213,15 @@ sys_sys_exit(struct thread *td, struct sys_exit_args *uap) /* NOTREACHED */ } +void +proc_set_p2_wexit(struct proc *p) +{ + PROC_LOCK_ASSERT(p, MA_OWNED); + p->p_flag2 |= P2_WEXIT; + while (p->p_singlethr > 0) + msleep(&p->p_singlethr, &p->p_mtx, PWAIT | PCATCH, "exit1t", 0); +} + /* * Exit: deallocate address space and other resources, change proc state to * zombie, and unlink proc from allproc and parent's lists. Save exit status @@ -251,7 +260,7 @@ exit1(struct thread *td, int rval, int signo) * MUST abort all other threads before proceeding past here. */ PROC_LOCK(p); - p->p_flag2 |= P2_WEXIT; + proc_set_p2_wexit(p); /* * First check if some other thread or external request got diff --git a/sys/kern/kern_fork.c b/sys/kern/kern_fork.c index 47da6e28af30..32866aa26fba 100644 --- a/sys/kern/kern_fork.c +++ b/sys/kern/kern_fork.c @@ -318,9 +318,19 @@ fork_norfproc(struct thread *td, int flags) * must ensure that other threads do not concurrently create a second * process sharing the vmspace, see vmspace_unshare(). */ +again: if ((p1->p_flag & (P_HADTHREADS | P_SYSTEM)) == P_HADTHREADS && ((flags & (RFCFDG | RFFDG)) != 0 || (flags & RFMEM) == 0)) { PROC_LOCK(p1); + while (p1->p_singlethr > 0) { + error = msleep(&p1->p_singlethr, &p1->p_mtx, + PWAIT | PCATCH, "rfork1t", 0); + if (error != 0) { + PROC_UNLOCK(p1); + return (ERESTART); + } + goto again; + } if (thread_single(p1, SINGLE_BOUNDARY)) { PROC_UNLOCK(p1); return (ERESTART); @@ -384,6 +394,7 @@ do_fork(struct thread *td, struct fork_req *fr, struct proc *p2, struct thread * bzero(&p2->p_startzero, __rangeof(struct proc, p_startzero, p_endzero)); + p2->p_singlethr = 0; /* Tell the prison that we exist. */ prison_proc_hold(p2->p_ucred->cr_prison); diff --git a/sys/kern/kern_procctl.c b/sys/kern/kern_procctl.c index 640ebc32ee55..6919fac71c5a 100644 --- a/sys/kern/kern_procctl.c +++ b/sys/kern/kern_procctl.c @@ -412,8 +412,21 @@ reap_kill_subtree(struct thread *td, struct proc *p, struct proc *reaper, * repeated. */ init_unrhdr(&pids, 1, PID_MAX, UNR_NO_MTX); + PROC_LOCK(td->td_proc); + if ((td->td_proc->p_flag2 & P2_WEXIT) != 0) { + PROC_UNLOCK(td->td_proc); + goto out; + } + td->td_proc->p_singlethr++; + PROC_UNLOCK(td->td_proc); while (reap_kill_subtree_once(td, p, reaper, rk, ksi, error, &pids)) ; + PROC_LOCK(td->td_proc); + td->td_proc->p_singlethr--; + if (td->td_proc->p_singlethr == 0) + wakeup(&p->p_singlethr); + PROC_UNLOCK(td->td_proc); +out: clean_unrhdr(&pids); clear_unrhdr(&pids); } diff --git a/sys/kern/kern_sig.c b/sys/kern/kern_sig.c index b132eaa8de98..994cbb3f2af9 100644 --- a/sys/kern/kern_sig.c +++ b/sys/kern/kern_sig.c @@ -3436,7 +3436,8 @@ sigexit(struct thread *td, int sig) struct proc *p = td->td_proc; PROC_LOCK_ASSERT(p, MA_OWNED); - p->p_flag2 |= P2_WEXIT; + proc_set_p2_wexit(p); + p->p_acflag |= AXSIG; /* * We must be single-threading to generate a core dump. This diff --git a/sys/sys/proc.h b/sys/sys/proc.h index 188e65e75f0a..f9d60a6b9935 100644 --- a/sys/sys/proc.h +++ b/sys/sys/proc.h @@ -727,6 +727,8 @@ struct proc { char *p_binname; /* (b) Binary hardlink name. */ void *p_elf_brandinfo; /* (x) Elf_Brandinfo, NULL for non ELF binaries. */ + int p_singlethr; /* (c) Count of threads doing + external thread_single() */ }; #define p_session p_pgrp->pg_session @@ -1132,6 +1134,7 @@ void proc_linkup(struct proc *p, struct thread *td); struct proc *proc_realparent(struct proc *child); void proc_reap(struct thread *td, struct proc *p, int *status, int options); void proc_reparent(struct proc *child, struct proc *newparent, bool set_oppid); +void proc_set_p2_wexit(struct proc *p); void proc_set_traced(struct proc *p, bool stop); void proc_wkilled(struct proc *p); struct pstats *pstats_alloc(void);