From nobody Mon Jun 13 19:33:33 2022 X-Original-To: dev-commits-src-main@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 ABFB1854D3E; Mon, 13 Jun 2022 19:33:35 +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 4LMMDt4qxQz3qCv; Mon, 13 Jun 2022 19:33:34 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1655148814; 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=eVFYn6B8IvKR4dRbG/pfFtp+z14+uhyTgQN86qXzarg=; b=POTnktNHwc9mAS9OA2dfMFkXx7PCLuyt0U+Kr1ATdaC8ikuZiaD4qVchJncEY5sT87E4lD c9Q0v2KRLX6MSUVAfS9IleJBwOLCiw8m7zU+Pnym4nHq9q+T+ZOYNNAXUt7QNMnCnXZION RaybHQCnlTkwTJ/SZCE1GfioWCfZYWAt5z5b4AxNsSoytyeSkmU8y64re7nKV4GrR10Vxj lNJxSoHJclOFofWoxNvbGFnB9zG/v39ER6upJWKIjoRhJA0Irp6u0C7EC59658dbpmvR5q fWzCxhk5p3edbOSMflVjdZOa9nIVGzlpLA9ggUTTMV7TupdbjygyOO47im8Bxg== 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 2505E1BABC; Mon, 13 Jun 2022 19:33:34 +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 25DJXX3x023636; Mon, 13 Jun 2022 19:33:33 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 25DJXXOk023635; Mon, 13 Jun 2022 19:33:33 GMT (envelope-from git) Date: Mon, 13 Jun 2022 19:33:33 GMT Message-Id: <202206131933.25DJXXOk023635@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Konstantin Belousov Subject: git: 4493a13e3bfb - main - Do not single-thread itself when the process single-threaded some another process List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-main@freebsd.org X-BeenThere: dev-commits-src-main@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/main X-Git-Reftype: branch X-Git-Commit: 4493a13e3bfbbdf8488993843281ec688057ee0f Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1655148814; 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=eVFYn6B8IvKR4dRbG/pfFtp+z14+uhyTgQN86qXzarg=; b=bhCoRHW4QzPxkTj3u9iBjbJkNcVtrACJ5NusUeBEQm86LDdALCndfb+nIQOR4ik3QzxTPh DmXcbom6XxyzBa5BiGjecjdaGgO4gTWLq0A/3A6UZqnERdX4syl3kf09xTk08ZE/97JIba 1aH1oT9nESxj2pCOwfnGGyJ7m6gZxNTUPtMsPWdKlomdRIIpseFIA2dnxEjf1VklEasdXr KT97zcuhoO5bW7lvATG24zsKsShMauKWTG9gfJHEu4eQmfsqv6dSplY/E1usYaEW+HKWnn QeEmpJZV+K+EADi0nVs/ShlRcMpBDlmton+W9YAhbdy2QJZuT56m3ogJmNe1sQ== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1655148814; a=rsa-sha256; cv=none; b=brjwnCKiaq1PbyKcffDLS+IuVt83a9hGEHgYCVfSS/Bd/geQnGlaMJ3oPpHh0q2cWKVcSH CkHb9RrmBllzjJfZRJ3ILhIQJxOOr7syX4U021n1NrUCknjmBsosi8w2uCBF6YWiCnx4zk axJGbCO/mh4ENd6yHQSixKdqHktjCm60/vHK+btrZ/TXcP7Bm+Gar6sIwF3qP9MmimEdAH ifdZfeUsWuWeX0CeO37sFX62o4QTJlxs6J3iQXrro0ew1g3b1sqp70fueHuXrhaAv83Ybt 1GuyETWnkWDINdpJuqi3Y2Xxhevy73g9hbfkdpceBNDItzH4mzqdChAm8NZ3pA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by kib: URL: https://cgit.FreeBSD.org/src/commit/?id=4493a13e3bfbbdf8488993843281ec688057ee0f commit 4493a13e3bfbbdf8488993843281ec688057ee0f Author: Konstantin Belousov AuthorDate: 2022-05-15 21:55:32 +0000 Commit: Konstantin Belousov CommitDate: 2022-06-13 19:30:03 +0000 Do not single-thread itself when the process single-threaded some another process Since both self single-threading and remote single-threading rely on suspending the thread doing thread_single(), it cannot be mixed: thread doing thread_suspend_switch() might be subject to thread_suspend_one() and vice versa. In collaboration with: pho Reviewed by: markj Sponsored by: The FreeBSD Foundation MFC after: 2 weeks Differential revision: https://reviews.freebsd.org/D35310 --- sys/kern/kern_exec.c | 9 +++++++++ sys/kern/kern_exit.c | 11 ++++++++++- sys/kern/kern_fork.c | 10 ++++++++++ sys/kern/kern_procctl.c | 13 +++++++++++++ sys/kern/kern_sig.c | 3 ++- sys/kern/kern_thread.c | 2 +- sys/sys/proc.h | 3 +++ 7 files changed, 48 insertions(+), 3 deletions(-) diff --git a/sys/kern/kern_exec.c b/sys/kern/kern_exec.c index 33213c8304db..5951883cdc62 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 fcd9bffe862d..0d549d8ecea8 100644 --- a/sys/kern/kern_exit.c +++ b/sys/kern/kern_exit.c @@ -213,6 +213,15 @@ sys_exit(struct thread *td, struct exit_args *uap) __unreachable(); } +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 0062f7419ac0..5c33d2b32101 100644 --- a/sys/kern/kern_fork.c +++ b/sys/kern/kern_fork.c @@ -319,9 +319,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); 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 e3bbbd23ae6c..4512212a0847 100644 --- a/sys/kern/kern_sig.c +++ b/sys/kern/kern_sig.c @@ -3416,7 +3416,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/kern/kern_thread.c b/sys/kern/kern_thread.c index de04fee8cb96..a1cc77f4f1a4 100644 --- a/sys/kern/kern_thread.c +++ b/sys/kern/kern_thread.c @@ -99,7 +99,7 @@ _Static_assert(offsetof(struct proc, p_pid) == 0xc4, "struct proc KBI p_pid"); _Static_assert(offsetof(struct proc, p_filemon) == 0x3c8, "struct proc KBI p_filemon"); -_Static_assert(offsetof(struct proc, p_comm) == 0x3e0, +_Static_assert(offsetof(struct proc, p_comm) == 0x3e4, "struct proc KBI p_comm"); _Static_assert(offsetof(struct proc, p_emuldata) == 0x4c8, "struct proc KBI p_emuldata"); diff --git a/sys/sys/proc.h b/sys/sys/proc.h index 5027cca5da8b..3c210c5d8ff7 100644 --- a/sys/sys/proc.h +++ b/sys/sys/proc.h @@ -691,6 +691,8 @@ struct proc { int p_pendingexits; /* (c) Count of pending thread exits. */ struct filemon *p_filemon; /* (c) filemon-specific data. */ int p_pdeathsig; /* (c) Signal from parent on exit. */ + int p_singlethr; /* (c) Count of threads doing + external thread_single() */ /* End area that is zeroed on creation. */ #define p_endzero p_magic @@ -1158,6 +1160,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);