From owner-svn-src-all@freebsd.org Fri Jul 10 06:47:59 2020 Return-Path: Delivered-To: svn-src-all@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id 00952361088; Fri, 10 Jul 2020 06:47: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.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 "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4B33Wy6FfRz4CPk; Fri, 10 Jul 2020 06:47: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 B9E001DE38; Fri, 10 Jul 2020 06:47: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 06A6lwWI015393; Fri, 10 Jul 2020 06:47:58 GMT (envelope-from mjg@FreeBSD.org) Received: (from mjg@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id 06A6lwCE015392; Fri, 10 Jul 2020 06:47:58 GMT (envelope-from mjg@FreeBSD.org) Message-Id: <202007100647.06A6lwCE015392@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: mjg set sender to mjg@FreeBSD.org using -f From: Mateusz Guzik Date: Fri, 10 Jul 2020 06:47:58 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r363069 - head/sys/kern X-SVN-Group: head X-SVN-Commit-Author: mjg X-SVN-Commit-Paths: head/sys/kern X-SVN-Commit-Revision: 363069 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.33 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 10 Jul 2020 06:47:59 -0000 Author: mjg Date: Fri Jul 10 06:47:58 2020 New Revision: 363069 URL: https://svnweb.freebsd.org/changeset/base/363069 Log: vfs: depessimize getfsstat when only the count is requested This avoids relocking mountlist_mtx for each entry. Modified: head/sys/kern/vfs_syscalls.c Modified: head/sys/kern/vfs_syscalls.c ============================================================================== --- head/sys/kern/vfs_syscalls.c Fri Jul 10 06:46:42 2020 (r363068) +++ head/sys/kern/vfs_syscalls.c Fri Jul 10 06:47:58 2020 (r363069) @@ -441,7 +441,46 @@ restart: tofree = sfsp = *buf = malloc(maxcount * sizeof(struct statfs), M_STATFS, M_WAITOK); } + count = 0; + + /* + * If there is no target buffer they only want the count. + * + * This could be TAILQ_FOREACH but it is open-coded to match the original + * code below. + */ + if (sfsp == NULL) { + mtx_lock(&mountlist_mtx); + for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) { + if (prison_canseemount(td->td_ucred, mp) != 0) { + nmp = TAILQ_NEXT(mp, mnt_list); + continue; + } +#ifdef MAC + if (mac_mount_check_stat(td->td_ucred, mp) != 0) { + nmp = TAILQ_NEXT(mp, mnt_list); + continue; + } +#endif + count++; + nmp = TAILQ_NEXT(mp, mnt_list); + } + mtx_unlock(&mountlist_mtx); + *countp = count; + return (0); + } + + /* + * They want the entire thing. + * + * Short-circuit the corner case of no room for anything, avoids + * relocking below. + */ + if (maxcount < 1) { + goto out; + } + mtx_lock(&mountlist_mtx); for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) { if (prison_canseemount(td->td_ucred, mp) != 0) { @@ -473,53 +512,55 @@ restart: continue; } } - if (sfsp != NULL && count < maxcount) { - sp = &mp->mnt_stat; - /* - * If MNT_NOWAIT is specified, do not refresh - * the fsstat cache. - */ - if (mode != MNT_NOWAIT) { - error = VFS_STATFS(mp, sp); - if (error != 0) { - mtx_lock(&mountlist_mtx); - nmp = TAILQ_NEXT(mp, mnt_list); - vfs_unbusy(mp); - continue; - } + sp = &mp->mnt_stat; + /* + * If MNT_NOWAIT is specified, do not refresh + * the fsstat cache. + */ + if (mode != MNT_NOWAIT) { + error = VFS_STATFS(mp, sp); + if (error != 0) { + mtx_lock(&mountlist_mtx); + nmp = TAILQ_NEXT(mp, mnt_list); + vfs_unbusy(mp); + continue; } - if (priv_check_cred_vfs_generation(td->td_ucred)) { - sptmp = malloc(sizeof(struct statfs), M_STATFS, - M_WAITOK); - *sptmp = *sp; - sptmp->f_fsid.val[0] = sptmp->f_fsid.val[1] = 0; - prison_enforce_statfs(td->td_ucred, mp, sptmp); - sp = sptmp; - } else - sptmp = NULL; - if (bufseg == UIO_SYSSPACE) { - bcopy(sp, sfsp, sizeof(*sp)); - free(sptmp, M_STATFS); - } else /* if (bufseg == UIO_USERSPACE) */ { - error = copyout(sp, sfsp, sizeof(*sp)); - free(sptmp, M_STATFS); - if (error != 0) { - vfs_unbusy(mp); - return (error); - } + } + if (priv_check_cred_vfs_generation(td->td_ucred)) { + sptmp = malloc(sizeof(struct statfs), M_STATFS, + M_WAITOK); + *sptmp = *sp; + sptmp->f_fsid.val[0] = sptmp->f_fsid.val[1] = 0; + prison_enforce_statfs(td->td_ucred, mp, sptmp); + sp = sptmp; + } else + sptmp = NULL; + if (bufseg == UIO_SYSSPACE) { + bcopy(sp, sfsp, sizeof(*sp)); + free(sptmp, M_STATFS); + } else /* if (bufseg == UIO_USERSPACE) */ { + error = copyout(sp, sfsp, sizeof(*sp)); + free(sptmp, M_STATFS); + if (error != 0) { + vfs_unbusy(mp); + return (error); } - sfsp++; } + sfsp++; count++; + + if (count == maxcount) { + vfs_unbusy(mp); + break; + } + mtx_lock(&mountlist_mtx); nmp = TAILQ_NEXT(mp, mnt_list); vfs_unbusy(mp); } mtx_unlock(&mountlist_mtx); - if (sfsp != NULL && count > maxcount) - *countp = maxcount; - else - *countp = count; +out: + *countp = count; return (0); }