]> git.proxmox.com Git - mirror_spl-debian.git/blobdiff - include/sys/mutex.h
New upstream version 0.7.2
[mirror_spl-debian.git] / include / sys / mutex.h
index be69deaab5d585915a001889b35928fbb560f00e..8cbab7c477ebc590910e4c528fb7893ab12d3700 100644 (file)
 #include <sys/types.h>
 #include <linux/mutex.h>
 #include <linux/compiler_compat.h>
+#include <linux/lockdep.h>
 
 typedef enum {
        MUTEX_DEFAULT   = 0,
        MUTEX_SPIN      = 1,
-       MUTEX_ADAPTIVE  = 2
+       MUTEX_ADAPTIVE  = 2,
+       MUTEX_NOLOCKDEP = 3
 } kmutex_type_t;
 
 typedef struct {
        struct mutex            m_mutex;
        spinlock_t              m_lock; /* used for serializing mutex_exit */
-#ifndef HAVE_MUTEX_OWNER
-       /* only when kernel doesn't have owner */
        kthread_t               *m_owner;
-#endif
+#ifdef CONFIG_LOCKDEP
+       kmutex_type_t           m_type;
+#endif /* CONFIG_LOCKDEP */
 } kmutex_t;
 
 #define        MUTEX(mp)               (&((mp)->m_mutex))
@@ -49,32 +51,44 @@ typedef struct {
 static inline void
 spl_mutex_set_owner(kmutex_t *mp)
 {
-       /*
-        * kernel will handle its owner, so we don't need to do anything if it
-        * is defined.
-        */
-#ifndef HAVE_MUTEX_OWNER
        mp->m_owner = current;
-#endif
 }
 
 static inline void
 spl_mutex_clear_owner(kmutex_t *mp)
 {
-#ifndef HAVE_MUTEX_OWNER
        mp->m_owner = NULL;
-#endif
 }
 
-#ifdef HAVE_MUTEX_OWNER
-#define        mutex_owner(mp)         (ACCESS_ONCE(MUTEX(mp)->owner))
-#else
 #define        mutex_owner(mp)         (ACCESS_ONCE((mp)->m_owner))
-#endif
 #define        mutex_owned(mp)         (mutex_owner(mp) == current)
 #define        MUTEX_HELD(mp)          mutex_owned(mp)
 #define        MUTEX_NOT_HELD(mp)      (!MUTEX_HELD(mp))
 
+#ifdef CONFIG_LOCKDEP
+static inline void
+spl_mutex_set_type(kmutex_t *mp, kmutex_type_t type)
+{
+       mp->m_type = type;
+}
+static inline void
+spl_mutex_lockdep_off_maybe(kmutex_t *mp)                      \
+{                                                              \
+       if (mp && mp->m_type == MUTEX_NOLOCKDEP)                \
+               lockdep_off();                                  \
+}
+static inline void
+spl_mutex_lockdep_on_maybe(kmutex_t *mp)                       \
+{                                                              \
+       if (mp && mp->m_type == MUTEX_NOLOCKDEP)                \
+               lockdep_on();                                   \
+}
+#else  /* CONFIG_LOCKDEP */
+#define spl_mutex_set_type(mp, type)
+#define spl_mutex_lockdep_off_maybe(mp)
+#define spl_mutex_lockdep_on_maybe(mp)
+#endif /* CONFIG_LOCKDEP */
+
 /*
  * The following functions must be a #define and not static inline.
  * This ensures that the native linux mutex functions (lock/unlock)
@@ -85,11 +99,12 @@ spl_mutex_clear_owner(kmutex_t *mp)
 #define        mutex_init(mp, name, type, ibc)                         \
 {                                                              \
        static struct lock_class_key __key;                     \
-       ASSERT(type == MUTEX_DEFAULT);                          \
+       ASSERT(type == MUTEX_DEFAULT || type == MUTEX_NOLOCKDEP); \
                                                                \
        __mutex_init(MUTEX(mp), (name) ? (#name) : (#mp), &__key); \
        spin_lock_init(&(mp)->m_lock);                          \
        spl_mutex_clear_owner(mp);                              \
+       spl_mutex_set_type(mp, type);                           \
 }
 
 #undef mutex_destroy
@@ -102,8 +117,10 @@ spl_mutex_clear_owner(kmutex_t *mp)
 ({                                                             \
        int _rc_;                                               \
                                                                \
+       spl_mutex_lockdep_off_maybe(mp);                        \
        if ((_rc_ = mutex_trylock(MUTEX(mp))) == 1)             \
                spl_mutex_set_owner(mp);                        \
+       spl_mutex_lockdep_on_maybe(mp);                         \
                                                                \
        _rc_;                                                   \
 })
@@ -112,14 +129,18 @@ spl_mutex_clear_owner(kmutex_t *mp)
 #define        mutex_enter_nested(mp, subclass)                        \
 {                                                              \
        ASSERT3P(mutex_owner(mp), !=, current);                 \
+       spl_mutex_lockdep_off_maybe(mp);                        \
        mutex_lock_nested(MUTEX(mp), (subclass));               \
+       spl_mutex_lockdep_on_maybe(mp);                         \
        spl_mutex_set_owner(mp);                                \
 }
 #else /* CONFIG_DEBUG_LOCK_ALLOC */
 #define        mutex_enter_nested(mp, subclass)                        \
 {                                                              \
        ASSERT3P(mutex_owner(mp), !=, current);                 \
+       spl_mutex_lockdep_off_maybe(mp);                        \
        mutex_lock(MUTEX(mp));                                  \
+       spl_mutex_lockdep_on_maybe(mp);                         \
        spl_mutex_set_owner(mp);                                \
 }
 #endif /*  CONFIG_DEBUG_LOCK_ALLOC */
@@ -147,10 +168,13 @@ spl_mutex_clear_owner(kmutex_t *mp)
  */
 #define        mutex_exit(mp)                                          \
 {                                                              \
-       spin_lock(&(mp)->m_lock);                               \
        spl_mutex_clear_owner(mp);                              \
+       spin_lock(&(mp)->m_lock);                               \
+       spl_mutex_lockdep_off_maybe(mp);                        \
        mutex_unlock(MUTEX(mp));                                \
+       spl_mutex_lockdep_on_maybe(mp);                         \
        spin_unlock(&(mp)->m_lock);                             \
+       /* NOTE: do not dereference mp after this point */      \
 }
 
 int spl_mutex_init(void);