Retire MUTEX_OWNER checks

To minimize the size of a kmutex_t a MUTEX_OWNER check was added.
It allowed the kmutex_t wrapper to leverage the mutex owner which was
already stored in the mutex for certain kernel configurations.

The upside to this was that it reduced the size of the kmutex_t wrapper
structure by the size of a task_struct pointer (4/8 bytes).  The
downside was that two mutex implementations needed to be maintained.
Depending on your exact kernel configuration the correct one would
be selected.

Over the years this solution worked but it could be fragile since it
depending heavily on assumed kernel mutex implementation details.  For
example the SPL_AC_MUTEX_OWNER_TASK_STRUCT configure check needed to
be added when the kernel changed how the owner was stored.  It also
made the code more complicated than it needed to be.

Therefore, in the name of simplicity and portability this optimization
is being retired.  It will slightly increase the memory requirements
for a kmutex_t but only very slightly.

Signed-off-by: Brian Behlendorf <behlendorf1@llnl.gov>
Signed-off-by: Tim Chase <tim@chase2k.com>
Issue #435
This commit is contained in:
Brian Behlendorf
2015-02-25 10:23:49 -08:00
parent a900e28e71
commit 5f920fbee1
2 changed files with 19 additions and 127 deletions
+19 -77
View File
@@ -35,81 +35,6 @@ typedef enum {
MUTEX_ADAPTIVE = 2
} kmutex_type_t;
#if defined(HAVE_MUTEX_OWNER) && defined(CONFIG_SMP) && \
!defined(CONFIG_DEBUG_MUTEXES)
typedef struct {
struct mutex m;
spinlock_t m_lock; /* used for serializing mutex_exit */
} kmutex_t;
static inline kthread_t *
mutex_owner(kmutex_t *mp)
{
#if defined(HAVE_MUTEX_OWNER_TASK_STRUCT)
return (ACCESS_ONCE(mp->m.owner));
#else
struct thread_info *owner = ACCESS_ONCE(mp->m.owner);
if (owner)
return (owner->task);
return (NULL);
#endif
}
#define mutex_owned(mp) (mutex_owner(mp) == current)
#define MUTEX_HELD(mp) mutex_owned(mp)
#define MUTEX_NOT_HELD(mp) (!MUTEX_HELD(mp))
#undef mutex_init
#define mutex_init(mp, name, type, ibc) \
{ \
static struct lock_class_key __key; \
ASSERT(type == MUTEX_DEFAULT); \
\
__mutex_init(&(mp)->m, #mp, &__key); \
spin_lock_init(&(mp)->m_lock); \
}
#undef mutex_destroy
#define mutex_destroy(mp) \
{ \
VERIFY3P(mutex_owner(mp), ==, NULL); \
}
#define mutex_tryenter(mp) mutex_trylock(&(mp)->m)
#define mutex_enter(mp) \
{ \
ASSERT3P(mutex_owner(mp), !=, current); \
mutex_lock(&(mp)->m); \
}
/*
* 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) \
{ \
spin_lock(&(mp)->m_lock); \
mutex_unlock(&(mp)->m); \
spin_unlock(&(mp)->m_lock); \
}
#else /* HAVE_MUTEX_OWNER */
typedef struct {
struct mutex m_mutex;
spinlock_t m_lock; /* used for serializing mutex_exit */
@@ -175,6 +100,25 @@ spl_mutex_clear_owner(kmutex_t *mp)
spl_mutex_set_owner(mp); \
}
/*
* 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) \
{ \
spin_lock(&(mp)->m_lock); \
@@ -183,8 +127,6 @@ spl_mutex_clear_owner(kmutex_t *mp)
spin_unlock(&(mp)->m_lock); \
}
#endif /* HAVE_MUTEX_OWNER */
int spl_mutex_init(void);
void spl_mutex_fini(void);