From 7505cffa56d7347436b32f8d3f192ad1c4383ad8 Mon Sep 17 00:00:00 2001 From: Mateusz Guzik Date: Sun, 22 Sep 2019 20:50:24 +0000 Subject: [PATCH] cache: try to avoid vhold if locks held Sponsored by: The FreeBSD Foundation --- sys/kern/vfs_cache.c | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/sys/kern/vfs_cache.c b/sys/kern/vfs_cache.c index 7b43d4e24d7b..ab55f672a544 100644 --- a/sys/kern/vfs_cache.c +++ b/sys/kern/vfs_cache.c @@ -1690,7 +1690,7 @@ cache_enter_time(struct vnode *dvp, struct vnode *vp, struct componentname *cnp, uint32_t hash; int flag; int len; - bool neg_locked; + bool neg_locked, held_dvp; u_long lnumcache; CTR3(KTR_VFS, "cache_enter(%p, %p, %s)", dvp, vp, cnp->cn_nameptr); @@ -1769,6 +1769,13 @@ cache_enter_time(struct vnode *dvp, struct vnode *vp, struct componentname *cnp, } } + held_dvp = false; + if (LIST_EMPTY(&dvp->v_cache_src) && flag != NCF_ISDOTDOT) { + vhold(dvp); + atomic_add_long(&numcachehv, 1); + held_dvp = true; + } + /* * Calculate the hash key and setup as much of the new * namecache entry as possible before acquiring the lock. @@ -1858,8 +1865,21 @@ cache_enter_time(struct vnode *dvp, struct vnode *vp, struct componentname *cnp, if (flag != NCF_ISDOTDOT) { if (LIST_EMPTY(&dvp->v_cache_src)) { - vhold(dvp); - atomic_add_rel_long(&numcachehv, 1); + if (!held_dvp) { + vhold(dvp); + atomic_add_long(&numcachehv, 1); + } + } else { + if (held_dvp) { + /* + * This will not take the interlock as someone + * else already holds the vnode on account of + * the namecache and we hold locks preventing + * this from changing. + */ + vdrop(dvp); + atomic_subtract_long(&numcachehv, 1); + } } LIST_INSERT_HEAD(&dvp->v_cache_src, ncp, nc_src); } @@ -1894,6 +1914,10 @@ cache_enter_time(struct vnode *dvp, struct vnode *vp, struct componentname *cnp, out_unlock_free: cache_enter_unlock(&cel); cache_free(ncp); + if (held_dvp) { + vdrop(dvp); + atomic_subtract_long(&numcachehv, 1); + } return; }