Author: bz
Date: Fri Aug 14 21:46:54 2009
New Revision: 196226
URL: http://svn.freebsd.org/changeset/base/196226

Log:
  Add a new macro to test that a variable could be loaded atomically.
  Check that the given variable is at most uintptr_t in size and that
  it is aligned.
  
  Note: ASSERT_ATOMIC_LOAD() uses ALIGN() to check for adequate
        alignment -- however, the function of ALIGN() is to guarantee
        alignment, and therefore may lead to stronger alignment
        enforcement than necessary for types that are smaller than
        sizeof(uintptr_t).
  
  Add checks to mtx, rw and sx locks init functions to detect possible
  breakage. This was used during debugging of the problem fixed with
  r196118 where a pointer was on an un-aligned address in the dpcpu area.
  
  In collaboration with:        rwatson
  Reviewed by:          rwatson
  Approved by:          re (kib)

Modified:
  head/sys/kern/kern_mutex.c
  head/sys/kern/kern_rwlock.c
  head/sys/kern/kern_sx.c
  head/sys/sys/systm.h

Modified: head/sys/kern/kern_mutex.c
==============================================================================
--- head/sys/kern/kern_mutex.c  Fri Aug 14 21:07:41 2009        (r196225)
+++ head/sys/kern/kern_mutex.c  Fri Aug 14 21:46:54 2009        (r196226)
@@ -783,6 +783,8 @@ mtx_init(struct mtx *m, const char *name
 
        MPASS((opts & ~(MTX_SPIN | MTX_QUIET | MTX_RECURSE |
                MTX_NOWITNESS | MTX_DUPOK | MTX_NOPROFILE)) == 0);
+       ASSERT_ATOMIC_LOAD(m->mtx_lock, ("%s: mtx_lock not aligned for %s: %p",
+           __func__, name, &m->mtx_lock));
 
 #ifdef MUTEX_DEBUG
        /* Diagnostic and error correction */

Modified: head/sys/kern/kern_rwlock.c
==============================================================================
--- head/sys/kern/kern_rwlock.c Fri Aug 14 21:07:41 2009        (r196225)
+++ head/sys/kern/kern_rwlock.c Fri Aug 14 21:46:54 2009        (r196226)
@@ -174,6 +174,8 @@ rw_init_flags(struct rwlock *rw, const c
 
        MPASS((opts & ~(RW_DUPOK | RW_NOPROFILE | RW_NOWITNESS | RW_QUIET |
            RW_RECURSE)) == 0);
+       ASSERT_ATOMIC_LOAD(rw->rw_lock, ("%s: rw_lock not aligned for %s: %p",
+           __func__, name, &rw->rw_lock));
 
        flags = LO_UPGRADABLE;
        if (opts & RW_DUPOK)

Modified: head/sys/kern/kern_sx.c
==============================================================================
--- head/sys/kern/kern_sx.c     Fri Aug 14 21:07:41 2009        (r196225)
+++ head/sys/kern/kern_sx.c     Fri Aug 14 21:46:54 2009        (r196226)
@@ -205,6 +205,8 @@ sx_init_flags(struct sx *sx, const char 
 
        MPASS((opts & ~(SX_QUIET | SX_RECURSE | SX_NOWITNESS | SX_DUPOK |
            SX_NOPROFILE | SX_NOADAPTIVE)) == 0);
+       ASSERT_ATOMIC_LOAD(sx->sx_lock, ("%s: sx_lock not aligned for %s: %p",
+           __func__, description, &sx->sx_lock));
 
        flags = LO_SLEEPABLE | LO_UPGRADABLE;
        if (opts & SX_DUPOK)

Modified: head/sys/sys/systm.h
==============================================================================
--- head/sys/sys/systm.h        Fri Aug 14 21:07:41 2009        (r196225)
+++ head/sys/sys/systm.h        Fri Aug 14 21:46:54 2009        (r196226)
@@ -89,6 +89,10 @@ extern int maxusers;         /* system tune hin
 #define        __CTASSERT(x, y)        typedef char __assert ## y[(x) ? 1 : -1]
 #endif
 
+#define        ASSERT_ATOMIC_LOAD(var,msg)                                     
\
+       KASSERT(sizeof(var) <= sizeof(uintptr_t) &&                     \
+           ALIGN(&(var)) == (uintptr_t)&(var), msg)
+
 /*
  * XXX the hints declarations are even more misplaced than most declarations
  * in this file, since they are needed in one file (per arch) and only used
_______________________________________________
svn-src-all@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/svn-src-all
To unsubscribe, send any mail to "svn-src-all-unsubscr...@freebsd.org"

Reply via email to