From owner-svn-src-head@freebsd.org Sat Feb 1 20:38:39 2020 Return-Path: Delivered-To: svn-src-head@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 C47F622C497; Sat, 1 Feb 2020 20:38:39 +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) server-signature RSA-PSS (4096 bits) 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 4895XH4w3dz43qD; Sat, 1 Feb 2020 20:38:39 +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 A38744A98; Sat, 1 Feb 2020 20:38:39 +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 011Kcdml052117; Sat, 1 Feb 2020 20:38:39 GMT (envelope-from mjg@FreeBSD.org) Received: (from mjg@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id 011Kcc8q052112; Sat, 1 Feb 2020 20:38:38 GMT (envelope-from mjg@FreeBSD.org) Message-Id: <202002012038.011Kcc8q052112@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: mjg set sender to mjg@FreeBSD.org using -f From: Mateusz Guzik Date: Sat, 1 Feb 2020 20:38:38 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r357387 - in head/sys: compat/linux kern sys X-SVN-Group: head X-SVN-Commit-Author: mjg X-SVN-Commit-Paths: in head/sys: compat/linux kern sys X-SVN-Commit-Revision: 357387 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.29 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: Sat, 01 Feb 2020 20:38:39 -0000 Author: mjg Date: Sat Feb 1 20:38:38 2020 New Revision: 357387 URL: https://svnweb.freebsd.org/changeset/base/357387 Log: cache: replace kern___getcwd with vn_getcwd The previous routine was resulting in extra data copies most notably in linux_getcwd. Modified: head/sys/compat/linux/linux_getcwd.c head/sys/kern/kern_sig.c head/sys/kern/vfs_cache.c head/sys/sys/syscallsubr.h head/sys/sys/vnode.h Modified: head/sys/compat/linux/linux_getcwd.c ============================================================================== --- head/sys/compat/linux/linux_getcwd.c Sat Feb 1 20:38:22 2020 (r357386) +++ head/sys/compat/linux/linux_getcwd.c Sat Feb 1 20:38:38 2020 (r357387) @@ -42,7 +42,7 @@ __FBSDID("$FreeBSD$"); #include #include -#include +#include #include #include @@ -60,28 +60,25 @@ __FBSDID("$FreeBSD$"); * Find pathname of process's current directory. */ int -linux_getcwd(struct thread *td, struct linux_getcwd_args *args) +linux_getcwd(struct thread *td, struct linux_getcwd_args *uap) { - char *path; - int error, lenused; + char *buf, *retbuf; + size_t buflen; + int error; - /* - * Linux returns ERANGE instead of EINVAL. - */ - if (args->bufsize < 2) + buflen = uap->bufsize; + if (__predict_false(buflen < 2)) return (ERANGE); + if (buflen > LINUX_PATH_MAX) + buflen = LINUX_PATH_MAX; - path = malloc(LINUX_PATH_MAX, M_LINUX, M_WAITOK); - - error = kern___getcwd(td, path, UIO_SYSSPACE, args->bufsize, - LINUX_PATH_MAX); + buf = malloc(buflen, M_TEMP, M_WAITOK); + error = vn_getcwd(td, buf, &retbuf, &buflen); if (error == 0) { - lenused = strlen(path) + 1; - error = copyout(path, args->buf, lenused); + error = copyout(retbuf, uap->buf, buflen); if (error == 0) - td->td_retval[0] = lenused; + td->td_retval[0] = buflen; } - - free(path, M_LINUX); + free(buf, M_TEMP); return (error); } Modified: head/sys/kern/kern_sig.c ============================================================================== --- head/sys/kern/kern_sig.c Sat Feb 1 20:38:22 2020 (r357386) +++ head/sys/kern/kern_sig.c Sat Feb 1 20:38:38 2020 (r357387) @@ -3608,6 +3608,7 @@ coredump(struct thread *td) struct vnode *vp; struct flock lf; struct vattr vattr; + size_t fullpathsize; int error, error1, locked; char *name; /* name of corefile */ void *rl_cookie; @@ -3711,13 +3712,14 @@ coredump(struct thread *td) * if the path of the core is relative, add the current dir in front if it. */ if (name[0] != '/') { - fullpath = malloc(MAXPATHLEN, M_TEMP, M_WAITOK); - if (kern___getcwd(td, fullpath, UIO_SYSSPACE, MAXPATHLEN, MAXPATHLEN) != 0) { - free(fullpath, M_TEMP); + fullpathsize = MAXPATHLEN; + freepath = malloc(fullpathsize, M_TEMP, M_WAITOK); + if (vn_getcwd(td, freepath, &fullpath, &fullpathsize) != 0) { + free(freepath, M_TEMP); goto out2; } devctl_safe_quote_sb(sb, fullpath); - free(fullpath, M_TEMP); + free(freepath, M_TEMP); sbuf_putc(sb, '/'); } devctl_safe_quote_sb(sb, name); Modified: head/sys/kern/vfs_cache.c ============================================================================== --- head/sys/kern/vfs_cache.c Sat Feb 1 20:38:22 2020 (r357386) +++ head/sys/kern/vfs_cache.c Sat Feb 1 20:38:38 2020 (r357387) @@ -364,7 +364,7 @@ STATNODE_COUNTER(numposhits, "Number of cache hits (po STATNODE_COUNTER(numnegzaps, "Number of cache hits (negative) we do not want to cache"); STATNODE_COUNTER(numneghits, "Number of cache hits (negative)"); -/* These count for kern___getcwd(), too. */ +/* These count for vn_getcwd(), too. */ STATNODE_COUNTER(numfullpathcalls, "Number of fullpath search calls"); STATNODE_COUNTER(numfullpathfail1, "Number of fullpath search errors (ENOTDIR)"); STATNODE_COUNTER(numfullpathfail2, @@ -2171,26 +2171,31 @@ vfs_cache_lookup(struct vop_lookup_args *ap) int sys___getcwd(struct thread *td, struct __getcwd_args *uap) { + char *buf, *retbuf; + size_t buflen; + int error; - return (kern___getcwd(td, uap->buf, UIO_USERSPACE, uap->buflen, - MAXPATHLEN)); + buflen = uap->buflen; + if (__predict_false(buflen < 2)) + return (EINVAL); + if (buflen > MAXPATHLEN) + buflen = MAXPATHLEN; + + buf = malloc(buflen, M_TEMP, M_WAITOK); + error = vn_getcwd(td, buf, &retbuf, &buflen); + if (error == 0) + error = copyout(retbuf, uap->buf, buflen); + free(buf, M_TEMP); + return (error); } int -kern___getcwd(struct thread *td, char *buf, enum uio_seg bufseg, size_t buflen, - size_t path_max) +vn_getcwd(struct thread *td, char *buf, char **retbuf, size_t *buflen) { - char *bp, *tmpbuf; struct filedesc *fdp; struct vnode *cdir, *rdir; int error; - if (__predict_false(buflen < 2)) - return (EINVAL); - if (buflen > path_max) - buflen = path_max; - - tmpbuf = malloc(buflen, M_TEMP, M_WAITOK); fdp = td->td_proc->p_fd; FILEDESC_SLOCK(fdp); cdir = fdp->fd_cdir; @@ -2198,21 +2203,14 @@ kern___getcwd(struct thread *td, char *buf, enum uio_s rdir = fdp->fd_rdir; vrefact(rdir); FILEDESC_SUNLOCK(fdp); - error = vn_fullpath1(td, cdir, rdir, tmpbuf, &bp, &buflen); + error = vn_fullpath1(td, cdir, rdir, buf, retbuf, buflen); vrele(rdir); vrele(cdir); - if (!error) { - if (bufseg == UIO_SYSSPACE) - bcopy(bp, buf, buflen); - else - error = copyout(bp, buf, buflen); #ifdef KTRACE - if (KTRPOINT(curthread, KTR_NAMEI)) - ktrnamei(bp); + if (KTRPOINT(curthread, KTR_NAMEI) && error == 0) + ktrnamei(*retbuf); #endif - } - free(tmpbuf, M_TEMP); return (error); } @@ -2338,7 +2336,7 @@ vn_vptocnp(struct vnode **vp, struct ucred *cred, char } /* - * The magic behind kern___getcwd() and vn_fullpath(). + * The magic behind vn_getcwd() and vn_fullpath(). */ static int vn_fullpath1(struct thread *td, struct vnode *vp, struct vnode *rdir, Modified: head/sys/sys/syscallsubr.h ============================================================================== --- head/sys/sys/syscallsubr.h Sat Feb 1 20:38:22 2020 (r357386) +++ head/sys/sys/syscallsubr.h Sat Feb 1 20:38:38 2020 (r357387) @@ -65,8 +65,6 @@ struct uio; typedef int (*mmap_check_fp_fn)(struct file *, int, int, int); -int kern___getcwd(struct thread *td, char *buf, enum uio_seg bufseg, - size_t buflen, size_t path_max); int kern_accept(struct thread *td, int s, struct sockaddr **name, socklen_t *namelen, struct file **fp); int kern_accept4(struct thread *td, int s, struct sockaddr **name, Modified: head/sys/sys/vnode.h ============================================================================== --- head/sys/sys/vnode.h Sat Feb 1 20:38:22 2020 (r357386) +++ head/sys/sys/vnode.h Sat Feb 1 20:38:38 2020 (r357387) @@ -632,6 +632,7 @@ u_quad_t init_va_filerev(void); int speedup_syncer(void); int vn_vptocnp(struct vnode **vp, struct ucred *cred, char *buf, size_t *buflen); +int vn_getcwd(struct thread *td, char *buf, char **retbuf, size_t *buflen); int vn_fullpath(struct thread *td, struct vnode *vn, char **retbuf, char **freebuf); int vn_fullpath_global(struct thread *td, struct vnode *vn,