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

Rick Macklem rmacklem at uoguelph.ca
Wed Apr 20 22:06:24 UTC 2011


> 
> 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.

Well, its value will be consistent, but not necessarily the "up to date"
value set by another thread, if I understood alc@'s recent post.
If you haven't yet read it, take a look at his post today on freebsd-hackers@
under the Subject
Re: SMP question w.r.t. reading kernel variables

If I understood his explanation, a thread must lock a mutex that the thread
that modified the value has locked/unlocked before it is guaranteed to see
that value for the variable instead of some stale memory cached value.
(It can be any mutex, but it is probably much easier to use the same one
 that the modifying thread used during modification instead of finding some
 other mutex the same thread locked/unlocked after the modification.)

> 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.
> 
Yes, understood. I need to look at the above check w.r.t. nm_maxfilesize
further (such as discussed by bde@). If a server were to change the value
during a read or write, all that should happen is that the server will
return an error for an operation that goes past its specified maxfilesize.
(As you noted, it is unlikely to ever change and, if it did change, I
 suspect the server would "grow" it and not "shrink" it.)

The mtx_lock(&nmp->nm_mtx); and mtx_unlock(&nmp->nm_mtx); just ensures
that it reads the "most up to date" value for the variable set by
another thread.

rick


More information about the svn-src-head mailing list