-/*****************************************************************************\
+/*
* Copyright (C) 2007-2010 Lawrence Livermore National Security, LLC.
* Copyright (C) 2007 The Regents of the University of California.
* Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
* UCRL-CODE-235197
*
* This file is part of the SPL, Solaris Porting Layer.
- * For details, see <http://github.com/behlendorf/spl/>.
+ * For details, see <http://zfsonlinux.org/>.
*
* The SPL is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
*
* You should have received a copy of the GNU General Public License along
* with the SPL. If not, see <http://www.gnu.org/licenses/>.
-\*****************************************************************************/
+ */
#ifndef _SPL_MUTEX_H
-#define _SPL_MUTEX_H
+#define _SPL_MUTEX_H
#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_DEFAULT = 0,
+ MUTEX_SPIN = 1,
+ MUTEX_ADAPTIVE = 2,
+ MUTEX_NOLOCKDEP = 3
} kmutex_type_t;
-#ifdef HAVE_MUTEX_OWNER
-
-typedef struct mutex kmutex_t;
-
-static inline kthread_t *
-mutex_owner(kmutex_t *mp)
-{
- if (mp->owner)
- return (mp->owner)->task;
-
- return NULL;
-}
-#define mutex_owned(mp) (mutex_owner(mp) == current)
-#define MUTEX_HELD(mp) mutex_owned(mp)
-#undef mutex_init
-#define mutex_init(mp, name, type, ibc) \
-({ \
- static struct lock_class_key __key; \
- ASSERT(type == MUTEX_DEFAULT); \
- \
- __mutex_init((mp), #mp, &__key); \
-})
-
-#undef mutex_destroy
-#define mutex_destroy(mp) \
-({ \
- VERIFY(!MUTEX_HELD(mp)); \
-})
-
-#define mutex_tryenter(mp) mutex_trylock(mp)
-#define mutex_enter(mp) mutex_lock(mp)
-#define mutex_exit(mp) mutex_unlock(mp)
-
-
-#ifdef HAVE_GPL_ONLY_SYMBOLS
-# define mutex_enter_nested(mp, sc) mutex_lock_nested(mp, sc)
-#else
-# define mutex_enter_nested(mp, sc) mutex_enter(mp)
-#endif /* HAVE_GPL_ONLY_SYMBOLS */
-
-#else /* HAVE_MUTEX_OWNER */
-
typedef struct {
- struct mutex m_mutex;
- kthread_t *m_owner;
+ struct mutex m_mutex;
+ spinlock_t m_lock; /* used for serializing mutex_exit */
+ /* only when kernel doesn't have owner */
+ kthread_t *m_owner;
+#ifdef CONFIG_LOCKDEP
+ kmutex_type_t m_type;
+#endif /* CONFIG_LOCKDEP */
} kmutex_t;
-#ifdef HAVE_TASK_CURR
-extern int spl_mutex_spin_max(void);
-#else /* HAVE_TASK_CURR */
-# define task_curr(owner) 0
-# define spl_mutex_spin_max() 0
-#endif /* HAVE_TASK_CURR */
-
-#define MUTEX(mp) ((struct mutex *)(mp))
-
-static inline kthread_t *
-spl_mutex_get_owner(kmutex_t *mp)
-{
- return mp->m_owner;
-}
+#define MUTEX(mp) (&((mp)->m_mutex))
static inline void
spl_mutex_set_owner(kmutex_t *mp)
{
- unsigned long flags;
-
- spin_lock_irqsave(&MUTEX(mp)->wait_lock, flags);
- mp->m_owner = current;
- spin_unlock_irqrestore(&MUTEX(mp)->wait_lock, flags);
+ /*
+ * kernel will handle its owner, so we don't need to do anything if it
+ * is defined.
+ */
+ mp->m_owner = current;
}
static inline void
spl_mutex_clear_owner(kmutex_t *mp)
{
- unsigned long flags;
-
- spin_lock_irqsave(&MUTEX(mp)->wait_lock, flags);
- mp->m_owner = NULL;
- spin_unlock_irqrestore(&MUTEX(mp)->wait_lock, flags);
+ mp->m_owner = NULL;
}
-static inline kthread_t *
-mutex_owner(kmutex_t *mp)
-{
- unsigned long flags;
- kthread_t *owner;
-
- spin_lock_irqsave(&MUTEX(mp)->wait_lock, flags);
- owner = spl_mutex_get_owner(mp);
- spin_unlock_irqrestore(&MUTEX(mp)->wait_lock, flags);
+#define mutex_owner(mp) (ACCESS_ONCE(MUTEX(mp)->owner))
+#define mutex_owned(mp) (mutex_owner(mp) == current)
+#define MUTEX_HELD(mp) mutex_owned(mp)
+#define MUTEX_NOT_HELD(mp) (!MUTEX_HELD(mp))
- return owner;
+#ifdef CONFIG_LOCKDEP
+static inline void
+spl_mutex_set_type(kmutex_t *mp, kmutex_type_t type)
+{
+ mp->m_type = type;
}
-
-#define mutex_owned(mp) (mutex_owner(mp) == current)
-#define MUTEX_HELD(mp) mutex_owned(mp)
+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.
* for the built in kernel lock analysis tools
*/
#undef mutex_init
-#define mutex_init(mp, name, type, ibc) \
-({ \
- static struct lock_class_key __key; \
- ASSERT(type == MUTEX_DEFAULT); \
- \
- __mutex_init(MUTEX(mp), #mp, &__key); \
- spl_mutex_clear_owner(mp); \
-})
+#define mutex_init(mp, name, type, ibc) \
+{ \
+ static struct lock_class_key __key; \
+ 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
-#define mutex_destroy(mp) \
-({ \
- VERIFY(!MUTEX_HELD(mp)); \
-})
-
-#define mutex_tryenter(mp) \
-({ \
- int _rc_; \
- \
- if ((_rc_ = mutex_trylock(MUTEX(mp))) == 1) \
- spl_mutex_set_owner(mp); \
- \
- _rc_; \
-})
+#define mutex_destroy(mp) \
+{ \
+ VERIFY3P(mutex_owner(mp), ==, NULL); \
+}
-/*
- * Adaptive mutexs assume that the lock may be held by a task running
- * on a different cpu. The expectation is that the task will drop the
- * lock before leaving the head of the run queue. So the ideal thing
- * to do is spin until we acquire the lock and avoid a context switch.
- * However it is also possible the task holding the lock yields the
- * processor with out dropping lock. In this case, we know it's going
- * to be a while so we stop spinning and go to sleep waiting for the
- * lock to be available. This should strike the optimum balance
- * between spinning and sleeping waiting for a lock.
- */
-#define mutex_enter(mp) \
-({ \
- kthread_t *_owner_; \
- int _rc_, _count_; \
- \
- _rc_ = 0; \
- _count_ = 0; \
- _owner_ = mutex_owner(mp); \
- \
- while (_owner_ && task_curr(_owner_) && \
- _count_ <= spl_mutex_spin_max()) { \
- if ((_rc_ = mutex_trylock(MUTEX(mp)))) \
- break; \
- \
- _count_++; \
- } \
- \
- if (!_rc_) \
- mutex_lock(MUTEX(mp)); \
- \
- spl_mutex_set_owner(mp); \
+#define mutex_tryenter(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_; \
})
-#define mutex_exit(mp) \
-({ \
- spl_mutex_clear_owner(mp); \
- mutex_unlock(MUTEX(mp)); \
-})
+#ifdef CONFIG_DEBUG_LOCK_ALLOC
+#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 */
-#ifdef HAVE_GPL_ONLY_SYMBOLS
-# define mutex_enter_nested(mp, sc) \
-({ \
- mutex_lock_nested(MUTEX(mp, sc)); \
- spl_mutex_set_owner(mp); \
-})
-#else
-# define mutex_enter_nested(mp, sc) \
-({ \
- mutex_enter(mp); \
-})
-#endif
+#define mutex_enter(mp) mutex_enter_nested((mp), 0)
-#endif /* HAVE_MUTEX_OWNER */
+/*
+ * The reason for the spinlock:
+ *
+ * The Linux mutex is designed with a fast-path/slow-path design such that it
+ * does not guarantee serialization upon itself, allowing a race where latter
+ * acquirers finish mutex_unlock before former ones.
+ *
+ * The race renders it unsafe to be used for serializing the freeing of an
+ * object in which the mutex is embedded, where the latter acquirer could go
+ * on to free the object while the former one is still doing mutex_unlock and
+ * causing memory corruption.
+ *
+ * However, there are many places in ZFS where the mutex is used for
+ * serializing object freeing, and the code is shared among other OSes without
+ * this issue. Thus, we need the spinlock to force the serialization on
+ * mutex_exit().
+ *
+ * See http://lwn.net/Articles/575477/ for the information about the race.
+ */
+#define mutex_exit(mp) \
+{ \
+ spl_mutex_lockdep_off_maybe(mp); \
+ spin_lock(&(mp)->m_lock); \
+ spl_mutex_clear_owner(mp); \
+ mutex_unlock(MUTEX(mp)); \
+ spin_unlock(&(mp)->m_lock); \
+ spl_mutex_lockdep_on_maybe(mp); \
+}
int spl_mutex_init(void);
void spl_mutex_fini(void);