-#ifndef _SPL_MUTEX_H
-#define _SPL_MUTEX_H
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-#include <linux/module.h>
-#include <linux/hardirq.h>
-#include <sys/types.h>
-
-/* See the "Big Theory Statement" in solaris mutex.c.
+/*
+ * 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).
+ * Written by Brian Behlendorf <behlendorf1@llnl.gov>.
+ * UCRL-CODE-235197
+ *
+ * This file is part of the SPL, Solaris Porting Layer.
+ * 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
+ * Free Software Foundation; either version 2 of the License, or (at your
+ * option) any later version.
*
- * Spin mutexes apparently aren't needed by zfs so we assert
- * if ibc is non-zero.
+ * The SPL is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * for more details.
*
- * Our impementation of adaptive mutexes aren't really adaptive.
- * They go to sleep every time.
+ * You should have received a copy of the GNU General Public License along
+ * with the SPL. If not, see <http://www.gnu.org/licenses/>.
*/
-#define MUTEX_DEFAULT 0
-#define MUTEX_HELD(x) (mutex_owned(x))
+#ifndef _SPL_MUTEX_H
+#define _SPL_MUTEX_H
-#define KM_MAGIC 0x42424242
-#define KM_POISON 0x84
+#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_NOLOCKDEP = 3
+} kmutex_type_t;
typedef struct {
- int km_magic;
- char *km_name;
- struct task_struct *km_owner;
- struct semaphore km_sem;
+ 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;
-#undef mutex_init
-static __inline__ void
-mutex_init(kmutex_t *mp, char *name, int type, void *ibc)
-{
- BUG_ON(ibc != NULL); /* XXX - Spin mutexes not needed? */
- BUG_ON(type != MUTEX_DEFAULT); /* XXX - Only default type supported? */
-
- mp->km_magic = KM_MAGIC;
- sema_init(&mp->km_sem, 1);
- mp->km_owner = NULL;
- mp->km_name = NULL;
-
- if (name) {
- mp->km_name = kmalloc(strlen(name) + 1, GFP_KERNEL);
- if (mp->km_name)
- strcpy(mp->km_name, name);
- }
-}
+#define MUTEX(mp) (&((mp)->m_mutex))
-#undef mutex_destroy
-static __inline__ void
-mutex_destroy(kmutex_t *mp)
+static inline void
+spl_mutex_set_owner(kmutex_t *mp)
{
- BUG_ON(mp->km_magic != KM_MAGIC);
-
- if (mp->km_name)
- kfree(mp->km_name);
-
- memset(mp, KM_POISON, sizeof(*mp));
+ /*
+ * kernel will handle its owner, so we don't need to do anything if it
+ * is defined.
+ */
+ mp->m_owner = current;
}
-static __inline__ void
-mutex_enter(kmutex_t *mp)
+static inline void
+spl_mutex_clear_owner(kmutex_t *mp)
{
- BUG_ON(mp->km_magic != KM_MAGIC);
+ mp->m_owner = NULL;
+}
- if (unlikely(in_atomic() && !current->exit_state)) {
- printk("May schedule while atomic: %s/0x%08x/%d\n",
- current->comm, preempt_count(), current->pid);
- BUG();
- }
+#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))
- down(&mp->km_sem); /* Will check in_atomic() for us */
- BUG_ON(mp->km_owner != NULL);
- mp->km_owner = current;
+#ifdef CONFIG_LOCKDEP
+static inline void
+spl_mutex_set_type(kmutex_t *mp, kmutex_type_t type)
+{
+ mp->m_type = type;
}
-
-/* Return 1 if we acquired the mutex, else zero.
+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)
+ * will be correctly located in the users code which is important
+ * for the built in kernel lock analysis tools
*/
-static __inline__ int
-mutex_tryenter(kmutex_t *mp)
-{
- int result;
-
- BUG_ON(mp->km_magic != KM_MAGIC);
-
- if (unlikely(in_atomic() && !current->exit_state)) {
- printk("May schedule while atomic: %s/0x%08x/%d\n",
- current->comm, preempt_count(), current->pid);
- BUG();
- }
-
- result = down_trylock(&mp->km_sem); /* returns 0 if acquired */
- if (result == 0) {
- BUG_ON(mp->km_owner != NULL);
- mp->km_owner = current;
- return 1;
- }
- return 0;
+#undef mutex_init
+#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); \
}
-static __inline__ void
-mutex_exit(kmutex_t *mp)
-{
- BUG_ON(mp->km_magic != KM_MAGIC);
- BUG_ON(mp->km_owner != current);
- mp->km_owner = NULL;
- up(&mp->km_sem);
+#undef mutex_destroy
+#define mutex_destroy(mp) \
+{ \
+ VERIFY3P(mutex_owner(mp), ==, NULL); \
}
-/* Return 1 if mutex is held by current process, else zero.
- */
-static __inline__ int
-mutex_owned(kmutex_t *mp)
-{
- BUG_ON(mp->km_magic != KM_MAGIC);
- return (mp->km_owner == current);
+#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_; \
+})
+
+#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 */
+
+#define mutex_enter(mp) mutex_enter_nested((mp), 0)
-/* Return owner if mutex is owned, else NULL.
+/*
+ * 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.
*/
-static __inline__ kthread_t *
-mutex_owner(kmutex_t *mp)
-{
- BUG_ON(mp->km_magic != KM_MAGIC);
- return mp->km_owner;
+#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); \
}
-#ifdef __cplusplus
-}
-#endif
+int spl_mutex_init(void);
+void spl_mutex_fini(void);
-#endif /* _SPL_MUTEX_H */
+#endif /* _SPL_MUTEX_H */