Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/netbsd-7]: src/sys/kern Pull up following revision(s) (requested by hann...



details:   https://anonhg.NetBSD.org/src/rev/148446802771
branches:  netbsd-7
changeset: 799770:148446802771
user:      snj <snj%NetBSD.org@localhost>
date:      Tue Jan 26 23:43:34 2016 +0000

description:
Pull up following revision(s) (requested by hannken in ticket #1070):
        sys/kern/vfs_vnode.c: revision 1.46 via patch
Take the vnode lock before the vnode is marked VI_CHANGING and fed
to vclean().  Prevents a deadlock with two null mounts on the same
physical mount where one thread tries to vclean() a layer node and
another thread tries to vget() a layer node pointing to the same
physical node.
Fixes PR kern/50375 layerfs (nullfs) locking problem leading to livelock

diffstat:

 sys/kern/vfs_vnode.c |  45 +++++++++++++++++++++++++++------------------
 1 files changed, 27 insertions(+), 18 deletions(-)

diffs (133 lines):

diff -r 8da2ea023c2d -r 148446802771 sys/kern/vfs_vnode.c
--- a/sys/kern/vfs_vnode.c      Tue Jan 26 04:57:58 2016 +0000
+++ b/sys/kern/vfs_vnode.c      Tue Jan 26 23:43:34 2016 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: vfs_vnode.c,v 1.37.2.1 2014/10/19 10:02:59 martin Exp $        */
+/*     $NetBSD: vfs_vnode.c,v 1.37.2.2 2016/01/26 23:43:34 snj Exp $   */
 
 /*-
  * Copyright (c) 1997-2011 The NetBSD Foundation, Inc.
@@ -116,7 +116,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: vfs_vnode.c,v 1.37.2.1 2014/10/19 10:02:59 martin Exp $");
+__KERNEL_RCSID(0, "$NetBSD: vfs_vnode.c,v 1.37.2.2 2016/01/26 23:43:34 snj Exp $");
 
 #define _VFS_VNODE_PRIVATE
 
@@ -328,15 +328,17 @@
                KASSERT((vp->v_iflag & VI_CLEAN) == 0);
                KASSERT(vp->v_freelisthd == listhd);
 
-               if (!mutex_tryenter(vp->v_interlock))
+               if (vn_lock(vp, LK_EXCLUSIVE | LK_NOWAIT) != 0)
                        continue;
-               if ((vp->v_iflag & VI_XLOCK) != 0) {
-                       mutex_exit(vp->v_interlock);
+               if (!mutex_tryenter(vp->v_interlock)) {
+                       VOP_UNLOCK(vp);
                        continue;
                }
+               KASSERT((vp->v_iflag & VI_XLOCK) == 0);
                mp = vp->v_mount;
                if (fstrans_start_nowait(mp, FSTRANS_SHARED) != 0) {
                        mutex_exit(vp->v_interlock);
+                       VOP_UNLOCK(vp);
                        continue;
                }
                break;
@@ -735,6 +737,11 @@
                 * Note that VOP_INACTIVE() will drop the vnode lock.
                 */
                VOP_INACTIVE(vp, &recycle);
+               if (recycle) {
+                       /* vclean() below will drop the lock. */
+                       if (vn_lock(vp, LK_EXCLUSIVE) != 0)
+                               recycle = false;
+               }
                mutex_enter(vp->v_interlock);
                if (!recycle) {
                        if (vtryrele(vp)) {
@@ -959,6 +966,7 @@
 /*
  * Disassociate the underlying file system from a vnode.
  *
+ * Must be called with vnode locked and will return unlocked.
  * Must be called with the interlock held, and will return with it held.
  */
 static void
@@ -968,28 +976,21 @@
        bool recycle, active, doclose;
        int error;
 
+       KASSERT((vp->v_vflag & VV_LOCKSWORK) == 0 ||
+           VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
        KASSERT(mutex_owned(vp->v_interlock));
        KASSERT((vp->v_iflag & VI_MARKER) == 0);
+       KASSERT((vp->v_iflag & (VI_XLOCK | VI_CLEAN)) == 0);
        KASSERT(vp->v_usecount != 0);
 
-       /* If already clean, nothing to do. */
-       if ((vp->v_iflag & VI_CLEAN) != 0) {
-               return;
-       }
-
        active = (vp->v_usecount > 1);
        doclose = ! (active && vp->v_type == VBLK &&
            spec_node_getmountedfs(vp) != NULL);
-       mutex_exit(vp->v_interlock);
-
-       vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
 
        /*
         * Prevent the vnode from being recycled or brought into use
         * while we clean it out.
         */
-       mutex_enter(vp->v_interlock);
-       KASSERT((vp->v_iflag & (VI_XLOCK | VI_CLEAN)) == 0);
        vp->v_iflag |= VI_XLOCK;
        if (vp->v_iflag & VI_EXECMAP) {
                atomic_add_int(&uvmexp.execpages, -vp->v_uobj.uo_npages);
@@ -1073,23 +1074,26 @@
 vrecycle(vnode_t *vp)
 {
 
+       if (vn_lock(vp, LK_EXCLUSIVE) != 0)
+               return false;
+
        mutex_enter(vp->v_interlock);
 
        KASSERT((vp->v_iflag & VI_MARKER) == 0);
 
        if (vp->v_usecount != 1) {
                mutex_exit(vp->v_interlock);
+               VOP_UNLOCK(vp);
                return false;
        }
        if ((vp->v_iflag & VI_CHANGING) != 0)
                vwait(vp, VI_CHANGING);
        if (vp->v_usecount != 1) {
                mutex_exit(vp->v_interlock);
+               VOP_UNLOCK(vp);
                return false;
-       } else if ((vp->v_iflag & VI_CLEAN) != 0) {
-               mutex_exit(vp->v_interlock);
-               return true;
        }
+       KASSERT((vp->v_iflag & VI_CLEAN) == 0);
        vp->v_iflag |= VI_CHANGING;
        vclean(vp);
        vrelel(vp, VRELEL_CHANGING_SET);
@@ -1137,6 +1141,11 @@
 vgone(vnode_t *vp)
 {
 
+       if (vn_lock(vp, LK_EXCLUSIVE) != 0) {
+               KASSERT((vp->v_iflag & VI_CLEAN) != 0);
+               vrele(vp);
+       }
+
        mutex_enter(vp->v_interlock);
        if ((vp->v_iflag & VI_CHANGING) != 0)
                vwait(vp, VI_CHANGING);



Home | Main Index | Thread Index | Old Index