From nobody Wed Apr 27 23:27:55 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 A808C1AA96AA; Wed, 27 Apr 2022 23:27:56 +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 4KpZg02KvPz4YR2; Wed, 27 Apr 2022 23:27:55 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1651102076; 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=MF/LAspdn65IsENws1rvyYurdcY4D7+w+9VT3CYzCfw=; b=v2zxtn0uI46EnMgjUKru5sSnNsxXc+iD44XbOgzrCRpteM/z5uNLekPAEgVrRwQ1WzbIuH ByYq6CtDyQ1H7oGVwnjKAd7CnMdZi+yy5kR5y6ka8cgvkG6kSA0zXI6QIu5j2VH9LaSCLy 73HahdQ1l+Rk5ltbvK+9pC0KGudvWSTZqVEI7GSgogdlf8t6ILiQGg4etnOAfAM6I5hHk5 M864wWmNC3aiEK3G1zfF6rmHpEOPsUu0mvocbUiFCRS4TWSL4JmfXWZovoE69GIzEtAenK LunM0JniuLjrRkDgbYxRaG6JpvjLPkRbqGSLwz+lxmCxPpGtHwbxh2YhMDm+FA== 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 35A315648; Wed, 27 Apr 2022 23:27:55 +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 23RNRtEK071966; Wed, 27 Apr 2022 23:27:55 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 23RNRtJV071965; Wed, 27 Apr 2022 23:27:55 GMT (envelope-from git) Date: Wed, 27 Apr 2022 23:27:55 GMT Message-Id: <202204272327.23RNRtJV071965@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: 39794d80ad90 - main - Fix a race between fork(2) and PROC_REAP_KILL subtree 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/main X-Git-Reftype: branch X-Git-Commit: 39794d80ad900915e5c4940e9917ba5cb59a8634 Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1651102076; 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=MF/LAspdn65IsENws1rvyYurdcY4D7+w+9VT3CYzCfw=; b=om59FWjhwq3FDGiXU2pLZzqmET1bn7mJul5rnrSXBQBfaeTY00Vnnhlj8BijDEh15SpZaL epLNlE5+zx71m56LZcdZNCcS07xt5snXYZMnHoZ/w3v9jUiGFLz99ORpBz0bY6/NNr20UR iEvyKCjzIBhNVUf4L2e49cxjhgeR159RDN1M3y6ipKC0XMOKQkmsPpb6bMF7Su2E+zoGb+ LV8HLDLYJQlCV0fhGB9GnKcYDr5b0OQqIy6wyJ6G/FMMBzM45k4YxtturrTSiFFI/Pkr64 i7wTWwCUsCeXG4BhZqL3Y5BOr1wNd9FRcvArNhS9WKQnE6I0qhXAQ+Ujabv+eQ== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1651102076; a=rsa-sha256; cv=none; b=AMcAOPIUVzYGKop+BeAfCqohj/LmkzTEIdHuwFrR2+UykMdanUFE7kjD0cUK/blgsv/Bgy 9ieX3Y1ZdppIC6IObS/J+8F8wDPkizWluTU37UBN8Rm6qFZjn9cECnb4FUFl1NB1trAbDU ESRhFaBPGRx9jNneqI/v7BVNrCdJ8IypSmXUIBbwddf834pJ+P6qrn8oExiG69O4ZFwvqE 95HMaq7CKV0emvJ5K1DuUyQZzm+kGSMUiiAx8XVHsryd4tfrtDwyoB9VRNBVLHdZSkQ660 jJUiMSZwf8GjH/VWeN+c09WO/ZtTKGfiz2jRNwCl1r91gTVBJ3V779ffpIjWdg== 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=39794d80ad900915e5c4940e9917ba5cb59a8634 commit 39794d80ad900915e5c4940e9917ba5cb59a8634 Author: Konstantin Belousov AuthorDate: 2022-04-20 21:33:51 +0000 Commit: Konstantin Belousov CommitDate: 2022-04-27 23:27:35 +0000 Fix a race between fork(2) and PROC_REAP_KILL subtree by repeating iteration over the subtree until there are no new processes to signal. Reported and tested by: pho Reviewed by: markj Sponsored by: The FreeBSD Foundation MFC after: 1 week Differential revision: https://reviews.freebsd.org/D35014 --- sys/kern/kern_procctl.c | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/sys/kern/kern_procctl.c b/sys/kern/kern_procctl.c index 1a9f17de2d7a..83fcc57f8f78 100644 --- a/sys/kern/kern_procctl.c +++ b/sys/kern/kern_procctl.c @@ -31,6 +31,7 @@ __FBSDID("$FreeBSD$"); #include +#include #include #include #include @@ -293,14 +294,17 @@ reap_kill_children(struct thread *td, struct proc *reaper, } } -static void -reap_kill_subtree(struct thread *td, struct proc *p, struct proc *reaper, - struct procctl_reaper_kill *rk, ksiginfo_t *ksi, int *error) +static bool +reap_kill_subtree_once(struct thread *td, struct proc *p, struct proc *reaper, + struct procctl_reaper_kill *rk, ksiginfo_t *ksi, int *error, + struct unrhdr *pids) { struct reap_kill_tracker_head tracker; struct reap_kill_tracker *t; struct proc *p2; + bool res; + res = false; TAILQ_INIT(&tracker); reap_kill_sched(&tracker, reaper); while ((t = TAILQ_FIRST(&tracker)) != NULL) { @@ -313,10 +317,32 @@ reap_kill_subtree(struct thread *td, struct proc *p, struct proc *reaper, continue; if ((p2->p_treeflag & P_TREE_REAPER) != 0) reap_kill_sched(&tracker, p2); - reap_kill_proc(td, p2, ksi, rk, error); + if (alloc_unr_specific(pids, p2->p_pid) == p2->p_pid) { + reap_kill_proc(td, p2, ksi, rk, error); + res = true; + } } free(t, M_TEMP); } + return (res); +} + +static void +reap_kill_subtree(struct thread *td, struct proc *p, struct proc *reaper, + struct procctl_reaper_kill *rk, ksiginfo_t *ksi, int *error) +{ + struct unrhdr pids; + + /* + * pids records processes which were already signalled, to + * avoid doubling signals to them if iteration needs to be + * repeated. + */ + init_unrhdr(&pids, 1, PID_MAX, UNR_NO_MTX); + while (reap_kill_subtree_once(td, p, reaper, rk, ksi, error, &pids)) + ; + clean_unrhdr(&pids); + clear_unrhdr(&pids); } static bool