VFS locking questions
kostikbel at gmail.com
Mon Aug 6 05:46:40 PDT 2007
On Mon, Aug 06, 2007 at 01:10:18PM +0200, Pawel Jakub Dawidek wrote:
> On Fri, Aug 03, 2007 at 09:29:33PM +0200, Ulf Lilleengen wrote:
> > Hi,
> > I have a couple of questions regarding VFS, since I'm trying to SMPify the
> > fdescfs code in an effort to get some experience with VFS and freebsd locking...
> > What is really LK_INTERLOCK? When should it be used? When should one acquire it
> > (with VI_LOCK i assume), and what are the "semantics"?
> Vnode internal lock (v_interlock, VI_LOCK()) is used to protect various
> field in the vnode structure (those marked with 'i' letter in vnode.h).
> You pass the LK_INTERLOCK flag to functions like lockmgr(), vn_lock(),
> VOP_UNLOCK() when you already hold vnode's interlock. This way if one of
> those functions needs vnode's interlock internally, it knows if you
> already hold it or not (thus the function needs to acquire it on its
> own). We could probably just use mtx_owned() inside those functions.
> > Let's say I have a function that should return a locked vnode. I lock the
> > hash-table with a regular mutex. Then, when I traverse the list, I check if the
> > entry is what I look for. If it is, I call VI_LOCK() on the vnode, use vget to
> > increment refcount, and then use vn_lock(vp, LK_EXCLUSIVE...) to lock the vnode
> > before the function returns. Is this correct behaviour?
> Instead of doing what you suggest:
> vget(vp, LK_INTERLOCK, td);
> vn_lock(vp, LK_EXCLUSIVE, td);
> You can simply call:
> vget(vp, LK_EXCLUSIVE, td);
> This is why:
> - You haven't passed LK_INTERLOCK, so vget() will lock it by itself if
> needed (it does need it).
> - You passed LK_EXCLUSIVE, so vget() will return locked vnode.
> > The LK_INTERLOCK bothers me a bit, because I'm not 100% sure on how it works.
> It probably mostly an optimization and probably protection before some
> races, so you can call various functions with vnode's interlock already
You cannot sleep while holding mutex. So, if you hash is locked by mutex,
you cannot sleep in lockmgr() waiting for vnode lock. The solution is vnode
interlock. Basically, while still holding hash list mutex, you can do
error = vget(vp, LK_EXCLUSIVE|LK_INTERLOCK, td);
avoiding the race with vnode being reclaimed.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Size: 187 bytes
Desc: not available
Url : http://lists.freebsd.org/pipermail/freebsd-hackers/attachments/20070806/f76cc3ad/attachment.pgp
More information about the freebsd-hackers