svn commit: r220877 - head/sys/fs/nfsclient

Pawel Jakub Dawidek pjd at FreeBSD.org
Wed Apr 20 16:17:13 UTC 2011


On Wed, Apr 20, 2011 at 08:09:32AM -0400, Rick Macklem wrote:
> > > + tmp_off = uio->uio_offset + uio->uio_resid;
> > > + mtx_lock(&nmp->nm_mtx);
> > > + if (tmp_off > nmp->nm_maxfilesize || tmp_off < uio->uio_offset) {
> > > + mtx_unlock(&nmp->nm_mtx);
> > >  		return (EFBIG);
> > > + }
> > > + mtx_unlock(&nmp->nm_mtx);
> > 
> > I don't think you need the lock to protect nm_maxfilesize. Can it
> > change
> > from under us? My guess is that it is set on mount time and is not
> > modified afterwards.
> > 
> Good question.
> For NFSv3 - it is only modified by the first fsinfo RPC and that normally
>     happens at mount time, as you guessed above. (This is consistent with
>     RFC1813, which defines the fsinfo RPC as getting non-volatile information.)
> For NFSv4 - it gets it each time VFS_STATFS() happens. I am not sure that
>     this is correct, but I don't know of anywhere in RFC3530 where it states
>     that this attribute will not change. In practice, I suspect that servers
>     seldom, if ever, change it.
> 
> So, it is unlikely to change and I'd be comfortable taking the mutex lock
> off the check for it, if others are? (As you might be aware, I started a
> thread on hackers-freebsd@ where my question was basically "do you need to
> mutex lock when you read a global variable". My main concern there was a
> case that I'm working on w.r.t. forced dismounts. jhb@ suggested that he
> thinks it is good practice to always lock, to play it safe. At least that
> was my interpretation?)

This is not that easy, I'm afraid. You need to ask yourself a question
what you are trying to protect from. Here, the mutex only guarantees to
have consistent view of the nm_maxfilesize field. For example if this
field modification wouldn't be atomic you would need the mutex to ensure
that the value is correct.

Imagine a situation where it is modifed not by simple 'a = b', but by
something like this:

	mtx_lock(&nmp->nm_mtx);
	nmp->nm_maxfilesize = some_32bit_array[0];
	nmp->nm_maxfilesize |= some_32bit_array[1] << 32;
	mtx_unlock(&nmp->nm_mtx);

To read that properly you need a mutex to ensure you won't read the
value between those two operations.

If it is not the case - its modification is atomic and reading it is
atomic then you don't need mutex to read the value as it will always be
consistent. The question is what will happen if it changes after you
read it.

thread0					thread1
-------					-------

					mtx_lock(&nmp->nm_mtx);
					nmp->nm_maxfilesize = 8192;
					mtx_unlock(&nmp->nm_mtx);

mtx_lock(&nmp->nm_mtx);
if (tmp_off > nmp->nm_maxfilesize) {
	mtx_unlock(&nmp->nm_mtx);
	return (EFBIG);
}
mtx_unlock(&nmp->nm_mtx);

					mtx_lock(&nmp->nm_mtx);
					nmp->nm_maxfilesize = 2048;
					mtx_unlock(&nmp->nm_mtx);

<some other code>

Now, if tmp_off is 4096 what will happen if you have a race like the
above? Is it critical? Then you need to protect <some other code> with
this mutex as well.

-- 
Pawel Jakub Dawidek                       http://www.wheelsystems.com
FreeBSD committer                         http://www.FreeBSD.org
Am I Evil? Yes, I Am!                     http://yomoli.com
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 196 bytes
Desc: not available
Url : http://lists.freebsd.org/pipermail/svn-src-all/attachments/20110420/e2e4c22a/attachment.pgp


More information about the svn-src-all mailing list