From nobody Fri Jun 24 19:37:00 2022 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id E47EF864A19; Fri, 24 Jun 2022 19:37:02 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4LV6nn3Hcdz4XSX; Fri, 24 Jun 2022 19:37:00 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1656099422; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=ouxuJxjZnBFr4QM5/kE5i32IN0J2RekphAuBW8chrfQ=; b=B4fDw2cFK+NrHNPzH/rtzvLpQ9Ewp7r7aD8D+D1sZsrbtp2r2bZhlgHlmLrDGKGX8dchUx tveOp6noNHd7Kk5gWaKD3vFTbj5VMOkfT1/JGHBDhv+YhmPi84rFob6l3P/rmG6Lvxknpd Hcd7tBQo0waVdOlIJRm1M4XOovkrOHcAlvRYHs7B51s3DT27QE08luo00A82oOIqDNZAYx tzHX4K1kDC0xwRmXBoXKBBQ0fzDzzfB9Em+4Pj2SFqnEtbdru2LgBZ+JCeohVkXgK41Tj2 xyRIBaQVLnzq6Cd9Y24DmRq65b1K7F4jgY8G7v3BHiFphU8ZEmA9Rg7APPv8Xw== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id B18E2162FE; Fri, 24 Jun 2022 19:37:00 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 25OJb0lj084285; Fri, 24 Jun 2022 19:37:00 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 25OJb0gb084284; Fri, 24 Jun 2022 19:37:00 GMT (envelope-from git) Date: Fri, 24 Jun 2022 19:37:00 GMT Message-Id: <202206241937.25OJb0gb084284@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Konstantin Belousov Subject: git: d64c3f263fcf - stable/13 - Do not single-thread itself when the process single-threaded some another process List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: kib X-Git-Repository: src X-Git-Refname: refs/heads/stable/13 X-Git-Reftype: branch X-Git-Commit: d64c3f263fcfbe350b3fcaadc296f6d95dfe47e9 Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1656099422; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=ouxuJxjZnBFr4QM5/kE5i32IN0J2RekphAuBW8chrfQ=; b=sQly4pG+InmKhHwlff7LUqi/RWDl5E8U/N/2bsSkq/KVXnFYLUtjd/rk8yLro4pUe6CtcQ rl9rvG0PwFi5dBka6mTn818BGOIVWCEHof6oUz+ddZ+cioE36ix/E/wwFfhte0feJlhIfj 1XGpBijUwA7TBTwMB270QqNP382WIalCrO8jYHA911bFwIhBfOWM4llYclkpM1JcizF3nr UWNcchMSdoQEUvdNV9KvrZvo86GnsEdlNspV244zjMkFYWEDIvBHn56gy9nfFFVzmMjKFe 78Q+1px70CQZ2ej/ssdgUaonJCW+QGHmwiFz3QgQRGGgCsRzPbenM3lm22YF6Q== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1656099422; a=rsa-sha256; cv=none; b=CTZgaJ0zA+AXvXOwACXA2CLPjeTk4vw7gTbgcggM7O3DbbJ0Zs7RAyfBVZpgUf65Xt5Q8W r7eyxtNu5MeOtomGVlfCoKa7OUkNihUfZtJyaRSKLA6Q3PNak75yuTxSOG0NJ9JNKmn1u+ aR9bfTCUKEbpbVsp+e8CoaZbMnS4dUyqiXyMj9gjSztaDE44VmQ3aQReMOT1C6kOKO8FqN 3cSAAQu5Z0GV/zHdqJDka0MzKv8LsYS9OVwg8k1LxQEV8VxjyhxccCsi4k58uRQJc9ax6h YrEZ1+JpQTMTm81A4d3p6iGxlOg5/M1kYLUCtGrji62OOhFs3jpiIVL0Gn7V5g== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch stable/13 has been updated by kib: URL: https://cgit.FreeBSD.org/src/commit/?id=d64c3f263fcfbe350b3fcaadc296f6d95dfe47e9 commit d64c3f263fcfbe350b3fcaadc296f6d95dfe47e9 Author: Konstantin Belousov AuthorDate: 2022-05-15 21:55:32 +0000 Commit: Konstantin Belousov 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);