From owner-freebsd-fs@FreeBSD.ORG Sun Jun 13 14:42:49 2010 Return-Path: Delivered-To: freebsd-fs@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 183931065674; Sun, 13 Jun 2010 14:42:49 +0000 (UTC) (envelope-from kostikbel@gmail.com) Received: from mail.zoral.com.ua (mx0.zoral.com.ua [91.193.166.200]) by mx1.freebsd.org (Postfix) with ESMTP id 5AA518FC13; Sun, 13 Jun 2010 14:42:47 +0000 (UTC) Received: from deviant.kiev.zoral.com.ua (root@deviant.kiev.zoral.com.ua [10.1.1.148]) by mail.zoral.com.ua (8.14.2/8.14.2) with ESMTP id o5DEggSl032257 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Sun, 13 Jun 2010 17:42:42 +0300 (EEST) (envelope-from kostikbel@gmail.com) Received: from deviant.kiev.zoral.com.ua (kostik@localhost [127.0.0.1]) by deviant.kiev.zoral.com.ua (8.14.4/8.14.4) with ESMTP id o5DEggru083594; Sun, 13 Jun 2010 17:42:42 +0300 (EEST) (envelope-from kostikbel@gmail.com) Received: (from kostik@localhost) by deviant.kiev.zoral.com.ua (8.14.4/8.14.4/Submit) id o5DEgekc083593; Sun, 13 Jun 2010 17:42:40 +0300 (EEST) (envelope-from kostikbel@gmail.com) X-Authentication-Warning: deviant.kiev.zoral.com.ua: kostik set sender to kostikbel@gmail.com using -f Date: Sun, 13 Jun 2010 17:42:40 +0300 From: Kostik Belousov To: Attilio Rao Message-ID: <20100613144240.GV13238@deviant.kiev.zoral.com.ua> References: <20100603143501.GA3176@a91-153-117-195.elisa-laajakaista.fi> Mime-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="3jK+0sHr6j/jwA0V" Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.4.2.3i X-Virus-Scanned: clamav-milter 0.95.2 at skuns.kiev.zoral.com.ua X-Virus-Status: Clean X-Spam-Status: No, score=-2.3 required=5.0 tests=ALL_TRUSTED,AWL,BAYES_50, DNS_FROM_OPENWHOIS autolearn=no version=3.2.5 X-Spam-Checker-Version: SpamAssassin 3.2.5 (2008-06-10) on skuns.kiev.zoral.com.ua Cc: freebsd-fs@freebsd.org, Jaakko Heinonen Subject: Re: syncer vnode leak because of nmount() race X-BeenThere: freebsd-fs@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Filesystems List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 13 Jun 2010 14:42:49 -0000 --3jK+0sHr6j/jwA0V Content-Type: text/plain; charset=koi8-r Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Fri, Jun 04, 2010 at 05:00:49PM +0200, Attilio Rao wrote: > 2010/6/3 Jaakko Heinonen : > > > > Hi, > > > > I have been playing with devfs and stress2 recently and I was able to > > trigger a syncer vnode leak with devfs.sh. As far as I can tell it is a > > nmount(2) (initial) mount race against update mount. The code in > > vfs_domount() is protected with vfs_busy()/vfs_unbusy() but it doesn't > > protect against update mounts. The protection by Giant is defeated > > because devfs_root() may sleep due to sx_xlock(). vfs_domount() calls > > VFS_ROOT() before allocating the syncer vnode. VI_MOUNT vnode flag > > doesn't provide sufficient protection against update mounts either. >=20 > Thanks for this bug report. > I think that, luckilly, it is not a very common condition to have the > mount still in flight and get updates... :) > However, I think that the real breakage here is that the check on > mnt->mnt_syncer is done lockless and it is unsafe. It might really be > protected by the mount interlock but that's tricky because > vfs_allocate_syncvnode() sleeps. Also re-checking the condition (after > the allocation takes place) is not too simple here. Is it ? I think that the patch below would fix the issue, by syncronizing mnt_syncer by syncer mutex. On the other hand, I prefer the jh' patch, because it seemingly disallows parallel updates of the mount point. I believe that mp->mnt_vnodecovered should be stable after the call to vfs_busy() succeeded. > I found also this bug when rewriting the syncer and I resolved that by > using a separate flag for that (in my case it was simpler and more > beneficial actually for some other reasons, but you may do the same > thing with a mnt_kern_flag entry). > If you have no time I can work actively on the patch, even if I'm > confident, luckilly, this problem is very hard to happen in the real > life. >=20 > Additively, note that vfs_busy() here is not intended to protect > against such situations but against unmount. > Also, I'm very unsure about what Giant protection might bring here. > IMHO we might probabilly remove it at all as long as all the sleeping > point present make it completely unuseful if anything (and I don't see > a reason why it may be needed). >=20 > > PS. vfs_unbusy(9) manual page is out of date after r184554 and IMO > > =9A =9Avfs_busy(9) manual page is misleading because it talks about > > =9A =9Asynchronizing access to a mount point. >=20 > May you be more precise on what is misleading please? diff --git a/sys/kern/vfs_mount.c b/sys/kern/vfs_mount.c index 088d939..053bd68 100644 --- a/sys/kern/vfs_mount.c +++ b/sys/kern/vfs_mount.c @@ -1034,14 +1034,10 @@ vfs_domount( else mp->mnt_kern_flag &=3D ~MNTK_ASYNC; MNT_IUNLOCK(mp); - if ((mp->mnt_flag & MNT_RDONLY) =3D=3D 0) { - if (mp->mnt_syncer =3D=3D NULL) - error =3D vfs_allocate_syncvnode(mp); - } else { - if (mp->mnt_syncer !=3D NULL) - vrele(mp->mnt_syncer); - mp->mnt_syncer =3D NULL; - } + if ((mp->mnt_flag & MNT_RDONLY) =3D=3D 0) + error =3D vfs_allocate_syncvnode(mp); + else + vfs_deallocate_syncvnode(mp); vfs_unbusy(mp); VI_LOCK(vp); vp->v_iflag &=3D ~VI_MOUNT; diff --git a/sys/kern/vfs_subr.c b/sys/kern/vfs_subr.c index ff6744a..6e4bb12 100644 --- a/sys/kern/vfs_subr.c +++ b/sys/kern/vfs_subr.c @@ -3400,12 +3400,31 @@ vfs_allocate_syncvnode(struct mount *mp) /* XXX - vn_syncer_add_to_worklist() also grabs and drops sync_mtx. */ mtx_lock(&sync_mtx); sync_vnode_count++; + if (mp->mnt_syncer =3D=3D NULL) { + mp->mnt_syncer =3D vp; + vp =3D NULL; + } mtx_unlock(&sync_mtx); BO_UNLOCK(bo); - mp->mnt_syncer =3D vp; + if (vp !=3D NULL) + vgone(vp); return (0); } =20 +void +vfs_deallocate_syncvnode(struct mount *mp) +{ + struct vnode *vp; + + mtx_lock(&sync_mtx); + vp =3D mp->mnt_syncer; + if (vp !=3D NULL) + mp->mnt_syncer =3D NULL; + mtx_unlock(&sync_mtx); + if (vp !=3D NULL) + vrele(vp); +} + /* * Do a lazy sync of the filesystem. */ @@ -3484,15 +3503,16 @@ sync_reclaim(struct vop_reclaim_args *ap) =20 bo =3D &vp->v_bufobj; BO_LOCK(bo); - vp->v_mount->mnt_syncer =3D NULL; + mtx_lock(&sync_mtx); + if (vp->v_mount->mnt_syncer =3D=3D vp) + vp->v_mount->mnt_syncer =3D NULL; if (bo->bo_flag & BO_ONWORKLST) { - mtx_lock(&sync_mtx); LIST_REMOVE(bo, bo_synclist); syncer_worklist_len--; sync_vnode_count--; - mtx_unlock(&sync_mtx); bo->bo_flag &=3D ~BO_ONWORKLST; } + mtx_unlock(&sync_mtx); BO_UNLOCK(bo); =20 return (0); diff --git a/sys/sys/mount.h b/sys/sys/mount.h index 20dcf64..dcff206 100644 --- a/sys/sys/mount.h +++ b/sys/sys/mount.h @@ -731,6 +731,7 @@ int vfs_busy(struct mount *, int); int vfs_export /* process mount export info */ (struct mount *, struct export_args *); int vfs_allocate_syncvnode(struct mount *); +void vfs_deallocate_syncvnode(struct mount *); int vfs_donmount(struct thread *td, int fsflags, struct uio *fsoptions); void vfs_getnewfsid(struct mount *); struct cdev *vfs_getrootfsid(struct mount *); --3jK+0sHr6j/jwA0V Content-Type: application/pgp-signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.10 (FreeBSD) iEYEARECAAYFAkwU7l8ACgkQC3+MBN1Mb4hljgCfaImXDvro2UDwP2GHdnHVvgDS ziYAoNtaKSPCKsLL89I3OmV//8arTa9N =BR4w -----END PGP SIGNATURE----- --3jK+0sHr6j/jwA0V--