Dynamic reads without locking.

Jeffrey Hsu hsu at FreeBSD.ORG
Thu Oct 9 12:46:44 PDT 2003


  > I'm wondering...
  > Jeffrey Hsu was talking about this at BSDCon03.
  > There is no need to lock data when we just made simple read, for example:
  > 
  >     mtx_lock(&foo_mtx);
  >     foo = 5;
  >     mtx_unlock(&foo_mtx);
  > but only:
  >     bar = foo;
  > 
  > IMHO this is quite dangerous.
  > Let's see:
  > 
  >     thread1                 thread2
  >     mtx_lock(&foo_mtx);
  >     foo = data_from_user;
  >                             bar = foo;
  >     foo &= MASK;
  >     mtx_unlock(&foo_mtx);
  > 
  > In this case we have really dangerous race if data from user are
  > safe only when we made 'and' operation on them.
  > OR of course we can just store wrong value in 'bar' and this could
  > be case of different problems.

This case (along with some other cases where locks of atomic reads
are required) is covered in the paper as

  But, one case where locks would be required is if the field
  temporarily holds a value that no one else is supposed to see and
  the writer, operating with the lock held, will store a valid value
  before releasing his lock. In this case, both the writer and
  reader need to hold the lock before accessing this field.

						Jeffrey


More information about the freebsd-hackers mailing list