From 7739d927664b408494c3793c1b07db01b16122d4 Mon Sep 17 00:00:00 2001 From: Mateusz Guzik Date: Sat, 1 Feb 2020 20:38:38 +0000 Subject: [PATCH] cache: replace kern___getcwd with vn_getcwd The previous routine was resulting in extra data copies most notably in linux_getcwd. --- sys/compat/linux/linux_getcwd.c | 31 +++++++++++------------ sys/kern/kern_sig.c | 10 +++++--- sys/kern/vfs_cache.c | 44 ++++++++++++++++----------------- sys/sys/syscallsubr.h | 2 -- sys/sys/vnode.h | 1 + 5 files changed, 42 insertions(+), 46 deletions(-) diff --git a/sys/compat/linux/linux_getcwd.c b/sys/compat/linux/linux_getcwd.c index e4c7dd52398c..68812f605a91 100644 --- a/sys/compat/linux/linux_getcwd.c +++ b/sys/compat/linux/linux_getcwd.c @@ -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); } diff --git a/sys/kern/kern_sig.c b/sys/kern/kern_sig.c index 961da27ba7d0..0d14b0665f4c 100644 --- a/sys/kern/kern_sig.c +++ b/sys/kern/kern_sig.c @@ -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); diff --git a/sys/kern/vfs_cache.c b/sys/kern/vfs_cache.c index 940705e7c301..fecf749578e3 100644 --- a/sys/kern/vfs_cache.c +++ b/sys/kern/vfs_cache.c @@ -364,7 +364,7 @@ STATNODE_COUNTER(numposhits, "Number of cache hits (positive)"); 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_seg bufseg, size_t buflen, 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 *buf, size_t *buflen) } /* - * 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, diff --git a/sys/sys/syscallsubr.h b/sys/sys/syscallsubr.h index 7724e8437b6c..8f652a0d1e41 100644 --- a/sys/sys/syscallsubr.h +++ b/sys/sys/syscallsubr.h @@ -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, diff --git a/sys/sys/vnode.h b/sys/sys/vnode.h index b43d73099058..8d983ca8a838 100644 --- a/sys/sys/vnode.h +++ b/sys/sys/vnode.h @@ -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,