Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 20 Apr 2011 18:16:55 +0200
From:      Pawel Jakub Dawidek <pjd@FreeBSD.org>
To:        Rick Macklem <rmacklem@uoguelph.ca>
Cc:        svn-src-head@freebsd.org, Rick Macklem <rmacklem@FreeBSD.org>, svn-src-all@freebsd.org, src-committers@freebsd.org
Subject:   Re: svn commit: r220877 - head/sys/fs/nfsclient
Message-ID:  <20110420161655.GA1907@garage.freebsd.pl>
In-Reply-To: <630616771.329277.1303301372199.JavaMail.root@erie.cs.uoguelph.ca>
References:  <20110420054655.GD1826@garage.freebsd.pl> <630616771.329277.1303301372199.JavaMail.root@erie.cs.uoguelph.ca>

next in thread | previous in thread | raw e-mail | index | archive | help

--mP3DRpeJDSE+ciuQ
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

On Wed, Apr 20, 2011 at 08:09:32AM -0400, Rick Macklem wrote:
> > > + tmp_off =3D 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);
> >=20
> > 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.
> >=20
> 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 informa=
tion.)
> 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 sta=
tes
>     that this attribute will not change. In practice, I suspect that serv=
ers
>     seldom, if ever, change it.
>=20
> 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 =3D b', but by
something like this:

	mtx_lock(&nmp->nm_mtx);
	nmp->nm_maxfilesize =3D some_32bit_array[0];
	nmp->nm_maxfilesize |=3D 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 =3D 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 =3D 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.

--=20
Pawel Jakub Dawidek                       http://www.wheelsystems.com
FreeBSD committer                         http://www.FreeBSD.org
Am I Evil? Yes, I Am!                     http://yomoli.com

--mP3DRpeJDSE+ciuQ
Content-Type: application/pgp-signature

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.14 (FreeBSD)

iEYEARECAAYFAk2vBvYACgkQForvXbEpPzT+/QCdFqw13GVrE7j8yGvnYJ/LaYlj
KVsAnjSrq6O6PvHuGcaOJDuNOMEoc94G
=dG5B
-----END PGP SIGNATURE-----

--mP3DRpeJDSE+ciuQ--



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20110420161655.GA1907>