From owner-svn-src-head@freebsd.org Sun Feb 25 15:16:59 2018 Return-Path: Delivered-To: svn-src-head@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 3FCF5F336B4; Sun, 25 Feb 2018 15:16:59 +0000 (UTC) (envelope-from mjg@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id DE8CF80FC1; Sun, 25 Feb 2018 15:16:58 +0000 (UTC) (envelope-from mjg@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id D00F769E8; Sun, 25 Feb 2018 15:16:58 +0000 (UTC) (envelope-from mjg@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id w1PFGwlM060392; Sun, 25 Feb 2018 15:16:58 GMT (envelope-from mjg@FreeBSD.org) Received: (from mjg@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id w1PFGw7K060391; Sun, 25 Feb 2018 15:16:58 GMT (envelope-from mjg@FreeBSD.org) Message-Id: <201802251516.w1PFGw7K060391@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: mjg set sender to mjg@FreeBSD.org using -f From: Mateusz Guzik Date: Sun, 25 Feb 2018 15:16:58 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r329985 - head/sys/kern X-SVN-Group: head X-SVN-Commit-Author: mjg X-SVN-Commit-Paths: head/sys/kern X-SVN-Commit-Revision: 329985 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.25 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 25 Feb 2018 15:16:59 -0000 Author: mjg Date: Sun Feb 25 15:16:58 2018 New Revision: 329985 URL: https://svnweb.freebsd.org/changeset/base/329985 Log: Don't generate data in sysctl_out_proc unless we intend to copy out. The first call is used to gauge how much spaces is needed. Just computing the size instead of generating the output allows to not take the proctree lock. Modified: head/sys/kern/kern_proc.c Modified: head/sys/kern/kern_proc.c ============================================================================== --- head/sys/kern/kern_proc.c Sun Feb 25 14:29:43 2018 (r329984) +++ head/sys/kern/kern_proc.c Sun Feb 25 15:16:58 2018 (r329985) @@ -1347,6 +1347,32 @@ freebsd32_kinfo_proc_out(const struct kinfo_proc *ki, } #endif +static ssize_t +kern_proc_out_size(struct proc *p, int flags) +{ + ssize_t size = 0; + + PROC_LOCK_ASSERT(p, MA_OWNED); + + if ((flags & KERN_PROC_NOTHREADS) != 0) { +#ifdef COMPAT_FREEBSD32 + if ((flags & KERN_PROC_MASK32) != 0) { + size += sizeof(struct kinfo_proc32); + } else +#endif + size += sizeof(struct kinfo_proc); + } else { +#ifdef COMPAT_FREEBSD32 + if ((flags & KERN_PROC_MASK32) != 0) + size += sizeof(struct kinfo_proc32) * p->p_numthreads; + else +#endif + size += sizeof(struct kinfo_proc) * p->p_numthreads; + } + PROC_UNLOCK(p); + return (size); +} + int kern_proc_out(struct proc *p, struct sbuf *sb, int flags) { @@ -1399,6 +1425,9 @@ sysctl_out_proc(struct proc *p, struct sysctl_req *req struct kinfo_proc ki; int error, error2; + if (req->oldptr == NULL) + return (SYSCTL_OUT(req, 0, kern_proc_out_size(p, flags))); + sbuf_new_for_sysctl(&sb, (char *)&ki, sizeof(ki), req); sbuf_clear_flags(&sb, SBUF_INCLUDENUL); error = kern_proc_out(p, &sb, flags); @@ -1461,16 +1490,22 @@ sysctl_kern_proc(SYSCTL_HANDLER_ARGS) break; } - if (!req->oldptr) { + if (req->oldptr == NULL) { /* overestimate by 5 procs */ error = SYSCTL_OUT(req, 0, sizeof (struct kinfo_proc) * 5); if (error) return (error); + } else { + error = sysctl_wire_old_buffer(req, 0); + if (error != 0) + return (error); + /* + * This lock is only needed to safely grab the parent of a + * traced process. Only grab it if we are producing any + * data to begin with. + */ + sx_slock(&proctree_lock); } - error = sysctl_wire_old_buffer(req, 0); - if (error != 0) - return (error); - sx_slock(&proctree_lock); sx_slock(&allproc_lock); for (doingzomb=0 ; doingzomb < 2 ; doingzomb++) { if (!doingzomb) @@ -1571,16 +1606,15 @@ sysctl_kern_proc(SYSCTL_HANDLER_ARGS) } error = sysctl_out_proc(p, req, flags); - if (error) { - sx_sunlock(&allproc_lock); - sx_sunlock(&proctree_lock); - return (error); - } + if (error) + goto out; } } +out: sx_sunlock(&allproc_lock); - sx_sunlock(&proctree_lock); - return (0); + if (req->oldptr != NULL) + sx_sunlock(&proctree_lock); + return (error); } struct pargs *