SMP question w.r.t. reading kernel variables

Kostik Belousov kostikbel at gmail.com
Sun Apr 17 20:59:55 UTC 2011


On Sun, Apr 17, 2011 at 03:49:48PM -0400, Rick Macklem wrote:
> Hi,
> 
> I should know the answer to this, but... When reading a global kernel
> variable, where its modifications are protected by a mutex, is it
> necessary to get the mutex lock to just read its value?
> 
> For example:
> A    if ((mp->mnt_kern_flag & MNTK_UNMOUNTF) != 0)
>           return (EPERM);
> versus
> B    MNT_ILOCK(mp);
>      if ((mp->mnt_kern_flag & MNTK_UNMOUNTF) != 0) {
>           MNT_IUNLOCK(mp);
>           return (EPERM);
>      }
>      MNT_IUNLOCK(mp);
> 
> My hunch is that B is necessary if you need an up-to-date value
> for the variable (mp->mnt_kern_flag in this case).
> 
> Is that correct?

mnt_kern_flag read is atomic on all architectures.
If, as I suspect, the fragment is for the VFS_UNMOUNT() fs method,
then VFS guarantees the stability of mnt_kern_flag, by blocking
other attempts to unmount until current one is finished.
If not, then either you do not need the lock, or provided snipped
which takes a lock is unsufficient, since you are dropping the lock
but continue the action that depends on the flag not being set.
-------------- 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/freebsd-hackers/attachments/20110417/120cdadb/attachment.pgp


More information about the freebsd-hackers mailing list