Reimplement rwlocks for Linux lock profiling/analysis.

It turns out that the previous rwlock implementation worked well but
did not integrate properly with the upstream kernel lock profiling/
analysis tools.  This is a major problem since it would be awfully
nice to be able to use the automatic lock checker and profiler.

The problem is that the upstream lock tools use the pre-processor
to create a lock class for each uniquely named locked.  Since the
rwsem was embedded in a wrapper structure the name was always the
same.  The effect was that we only ended up with one lock class for
the entire SPL which caused the lock dependency checker to flag
nearly everything as a possible deadlock.

The solution was to directly map a krwlock to a Linux rwsem using
a typedef there by eliminating the wrapper structure.  This was not
done initially because the rwsem implementation is specific to the arch.
To fully implement the Solaris krwlock API using only the provided rwsem
API is not possible.  It can only be done by directly accessing some of
the internal data member of the rwsem structure.

For example, the Linux API provides a different function for dropping
a reader vs writer lock.  Whereas the Solaris API uses the same function
and the caller does not pass in what type of lock it is.  This means to
properly drop the lock we need to determine if the lock is currently a
reader or writer lock.  Then we need to call the proper Linux API function.
Unfortunately, there is no provided API for this so we must extracted this
information directly from arch specific lock implementation.  This is
all do able, and what I did, but it does complicate things considerably.

The good news is that in addition to the profiling benefits of this
change.  We may see performance improvements due to slightly reduced
overhead when creating rwlocks and manipulating them.

The only function I was forced to sacrafice was rw_owner() because this
information is simply not stored anywhere in the rwsem.  Luckily this
appears not to be a commonly used function on Solaris, and it is my
understanding it is mainly used for debugging anyway.

In addition to the core rwlock changes, extensive updates were made to
the rwlock regression tests.  Each class of test was extended to provide
more API coverage and to be more rigerous in checking for misbehavior.

This is a pretty significant change and with that in mind I have been
careful to validate it on several platforms before committing.  The full
SPLAT regression test suite was run numberous times on all of the following
platforms.  This includes various kernels ranging from 2.6.16 to 2.6.29.

- SLES10   (ppc64)
- SLES11   (x86_64)
- CHAOS4.2 (x86_64)
- RHEL5.3  (x86_64)
- RHEL6    (x86_64)
- FC11     (x86_64)
This commit is contained in:
Brian Behlendorf 2009-09-18 16:09:47 -07:00
parent 73358d5a1d
commit e811949a57
9 changed files with 692 additions and 920 deletions

View File

@ -68,6 +68,7 @@ AC_DEFUN([SPL_AC_CONFIG_KERNEL], [
SPL_AC_4ARGS_VFS_RENAME
SPL_AC_CRED_STRUCT
SPL_AC_GROUPS_SEARCH
SPL_AC_PUT_TASK_STRUCT
])
AC_DEFUN([SPL_AC_MODULE_SYMVERS], [
@ -1263,7 +1264,7 @@ AC_DEFUN([SPL_AC_CRED_STRUCT], [
])
dnl #
dnl # Custom SPL patch may export this symbol
dnl # Custom SPL patch may export this symbol.
dnl #
AC_DEFUN([SPL_AC_GROUPS_SEARCH], [
SPL_CHECK_SYMBOL_EXPORT(
@ -1273,3 +1274,16 @@ AC_DEFUN([SPL_AC_GROUPS_SEARCH], [
[groups_search() is available])],
[])
])
dnl #
dnl # 2.6.x API change,
dnl # __put_task_struct() was exported in RHEL5 but unavailable elsewhere.
dnl #
AC_DEFUN([SPL_AC_PUT_TASK_STRUCT], [
SPL_CHECK_SYMBOL_EXPORT(
[__put_task_struct],
[],
[AC_DEFINE(HAVE_PUT_TASK_STRUCT, 1,
[__put_task_struct() is available])],
[])
])

82
configure vendored
View File

@ -21916,6 +21916,47 @@ _ACEOF
fi
echo "$as_me:$LINENO: checking whether symbol __put_task_struct is exported" >&5
echo $ECHO_N "checking whether symbol __put_task_struct is exported... $ECHO_C" >&6
grep -q -E '[[:space:]]__put_task_struct[[:space:]]' \
$LINUX_OBJ/Module*.symvers 2>/dev/null
rc=$?
if test $rc -ne 0; then
export=0
for file in ; do
grep -q -E "EXPORT_SYMBOL.*(__put_task_struct)" \
"$LINUX_OBJ/$file" 2>/dev/null
rc=$?
if test $rc -eq 0; then
export=1
break;
fi
done
if test $export -eq 0; then
echo "$as_me:$LINENO: result: no" >&5
echo "${ECHO_T}no" >&6
else
echo "$as_me:$LINENO: result: yes" >&5
echo "${ECHO_T}yes" >&6
cat >>confdefs.h <<\_ACEOF
#define HAVE_PUT_TASK_STRUCT 1
_ACEOF
fi
else
echo "$as_me:$LINENO: result: yes" >&5
echo "${ECHO_T}yes" >&6
cat >>confdefs.h <<\_ACEOF
#define HAVE_PUT_TASK_STRUCT 1
_ACEOF
fi
;;
user) ;;
all)
@ -24884,6 +24925,47 @@ _ACEOF
echo "$as_me:$LINENO: checking whether symbol __put_task_struct is exported" >&5
echo $ECHO_N "checking whether symbol __put_task_struct is exported... $ECHO_C" >&6
grep -q -E '[[:space:]]__put_task_struct[[:space:]]' \
$LINUX_OBJ/Module*.symvers 2>/dev/null
rc=$?
if test $rc -ne 0; then
export=0
for file in ; do
grep -q -E "EXPORT_SYMBOL.*(__put_task_struct)" \
"$LINUX_OBJ/$file" 2>/dev/null
rc=$?
if test $rc -eq 0; then
export=1
break;
fi
done
if test $export -eq 0; then
echo "$as_me:$LINENO: result: no" >&5
echo "${ECHO_T}no" >&6
else
echo "$as_me:$LINENO: result: yes" >&5
echo "${ECHO_T}yes" >&6
cat >>confdefs.h <<\_ACEOF
#define HAVE_PUT_TASK_STRUCT 1
_ACEOF
fi
else
echo "$as_me:$LINENO: result: yes" >&5
echo "${ECHO_T}yes" >&6
cat >>confdefs.h <<\_ACEOF
#define HAVE_PUT_TASK_STRUCT 1
_ACEOF
fi
;;
*)
echo "$as_me:$LINENO: result: Error!" >&5

View File

@ -1,7 +1,7 @@
/*
* This file is part of the SPL: Solaris Porting Layer.
*
* Copyright (c) 2008 Lawrence Livermore National Security, LLC.
* Copyright (c) 2009 Lawrence Livermore National Security, LLC.
* Produced at Lawrence Livermore National Laboratory
* Written by:
* Brian Behlendorf <behlendorf1@llnl.gov>,
@ -30,68 +30,89 @@
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/rwsem.h>
#include <asm/current.h>
#include <sys/types.h>
#include <sys/kmem.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef enum {
RW_DRIVER = 2, /* driver (DDI) rwlock */
RW_DEFAULT = 4 /* kernel default rwlock */
RW_DRIVER = 2,
RW_DEFAULT = 4
} krw_type_t;
typedef enum {
RW_WRITER,
RW_READER
RW_NONE = 0,
RW_WRITER = 1,
RW_READER = 2
} krw_t;
typedef struct rw_semaphore krwlock_t;
#define RW_MAGIC 0x3423645a
#define RW_POISON 0xa6
typedef struct {
int32_t rw_magic;
int32_t rw_name_size;
char *rw_name;
struct rw_semaphore rw_sem;
struct task_struct *rw_owner; /* holder of the write lock */
} krwlock_t;
extern void __rw_init(krwlock_t *rwlp, char *name, krw_type_t type, void *arg);
extern void __rw_destroy(krwlock_t *rwlp);
extern int __rw_tryenter(krwlock_t *rwlp, krw_t rw);
extern void __rw_enter(krwlock_t *rwlp, krw_t rw);
extern void __rw_exit(krwlock_t *rwlp);
extern void __rw_downgrade(krwlock_t *rwlp);
extern int __rw_tryupgrade(krwlock_t *rwlp);
extern kthread_t *__rw_owner(krwlock_t *rwlp);
extern int __rw_read_held(krwlock_t *rwlp);
extern int __rw_write_held(krwlock_t *rwlp);
extern int __rw_lock_held(krwlock_t *rwlp);
#define rw_init(rwlp, name, type, arg) \
({ \
if ((name) == NULL) \
__rw_init(rwlp, #rwlp, type, arg); \
else \
__rw_init(rwlp, name, type, arg); \
})
#define rw_destroy(rwlp) __rw_destroy(rwlp)
#define rw_tryenter(rwlp, rw) __rw_tryenter(rwlp, rw)
#define rw_enter(rwlp, rw) __rw_enter(rwlp, rw)
#define rw_exit(rwlp) __rw_exit(rwlp)
#define rw_downgrade(rwlp) __rw_downgrade(rwlp)
#define rw_tryupgrade(rwlp) __rw_tryupgrade(rwlp)
#define rw_owner(rwlp) __rw_owner(rwlp)
#define RW_READ_HELD(rwlp) __rw_read_held(rwlp)
#define RW_WRITE_HELD(rwlp) __rw_write_held(rwlp)
#define RW_LOCK_HELD(rwlp) __rw_lock_held(rwlp)
#ifdef __cplusplus
}
#define rw_init(rwlp, name, type, arg) init_rwsem(rwlp)
#define rw_destroy(rwlp) ((void)0)
#define rw_downgrade(rwlp) downgrade_write(rwlp)
#define RW_LOCK_HELD(rwlp) rwsem_is_locked(rwlp)
/*
* the rw-semaphore definition
* - if activity/count is 0 then there are no active readers or writers
* - if activity/count is +ve then that is the number of active readers
* - if activity/count is -1 then there is one active writer
*/
#if defined(CONFIG_RWSEM_GENERIC_SPINLOCK)
# define RW_COUNT(rwlp) ((rwlp)->activity)
# define RW_READ_HELD(rwlp) ((RW_COUNT(rwlp) > 0) ? RW_COUNT(rwlp) : 0)
# define RW_WRITE_HELD(rwlp) ((RW_COUNT(rwlp) < 0))
# define rw_exit_locked(rwlp) __up_read_locked(rwlp)
# define rw_tryenter_locked(rwlp) __down_write_trylock_locked(rwlp)
void __up_read_locked(struct rw_semaphore *);
int __down_write_trylock_locked(struct rw_semaphore *);
#else
# define RW_COUNT(rwlp) ((rwlp)->count & RWSEM_ACTIVE_MASK)
# define RW_READ_HELD(rwlp) ((RW_COUNT(rwlp) > 0) ? RW_COUNT(rwlp) : 0)
# define RW_WRITE_HELD(rwlp) ((RW_COUNT(rwlp) < 0))
# define rw_exit_locked(rwlp) up_read(rwlp)
# define rw_tryenter_locked(rwlp) down_write_trylock(rwlp)
#endif
#define rw_tryenter(rwlp, rw) \
({ \
int _rc_ = 0; \
switch (rw) { \
case RW_READER: _rc_ = down_read_trylock(rwlp); break; \
case RW_WRITER: _rc_ = down_write_trylock(rwlp); break; \
default: SBUG(); \
} \
_rc_; \
})
#define rw_enter(rwlp, rw) \
({ \
switch (rw) { \
case RW_READER: down_read(rwlp); break; \
case RW_WRITER: down_write(rwlp); break; \
default: SBUG(); \
} \
})
#define rw_exit(rwlp) \
({ \
if (RW_READ_HELD(rwlp)) \
up_read(rwlp); \
else if (RW_WRITE_HELD(rwlp)) \
up_write(rwlp); \
else \
SBUG(); \
})
#define rw_tryupgrade(rwlp) \
({ \
unsigned long flags; \
int _rc_ = 0; \
spin_lock_irqsave(&(rwlp)->wait_lock, flags); \
if (list_empty(&(rwlp)->wait_list) && (RW_READ_HELD(rwlp) == 1)) { \
rw_exit_locked(rwlp); \
_rc_ = rw_tryenter_locked(rwlp); \
ASSERT(_rc_); \
} \
spin_unlock_irqrestore(&(rwlp)->wait_lock, flags); \
_rc_; \
})
#endif /* _SPL_RWLOCK_H */

View File

@ -253,6 +253,22 @@ ddi_copyout(const void *from, void *to, size_t len, int flags)
}
EXPORT_SYMBOL(ddi_copyout);
#ifndef HAVE_PUT_TASK_STRUCT
/*
* This is only a stub function which should never be used. The SPL should
* never be putting away the last reference on a task structure so this will
* not be called. However, we still need to define it so the module does not
* have undefined symbol at load time. That all said if this impossible
* thing does somehow happen SBUG() immediately so we know about it.
*/
void
__put_task_struct(struct task_struct *t)
{
SBUG();
}
EXPORT_SYMBOL(__put_task_struct);
#endif /* HAVE_PUT_TASK_STRUCT */
struct new_utsname *__utsname(void)
{
#ifdef HAVE_INIT_UTSNAME

View File

@ -33,6 +33,11 @@
#define DEBUG_SUBSYSTEM S_RWLOCK
#ifdef CONFIG_RWSEM_GENERIC_SPINLOCK
/*
* From lib/rwsem-spinlock.c but modified such that the caller is
* responsible for acquiring and dropping the sem->wait_lock.
*/
struct rwsem_waiter {
struct list_head list;
struct task_struct *task;
@ -40,6 +45,7 @@ struct rwsem_waiter {
#define RWSEM_WAITING_FOR_READ 0x00000001
#define RWSEM_WAITING_FOR_WRITE 0x00000002
};
/* wake a single writer */
static struct rw_semaphore *
__rwsem_wake_one_writer_locked(struct rw_semaphore *sem)
@ -61,301 +67,27 @@ __rwsem_wake_one_writer_locked(struct rw_semaphore *sem)
}
/* release a read lock on the semaphore */
static void
void
__up_read_locked(struct rw_semaphore *sem)
{
if (--sem->activity == 0 && !list_empty(&sem->wait_list))
(void)__rwsem_wake_one_writer_locked(sem);
}
EXPORT_SYMBOL(__up_read_locked);
/* trylock for writing -- returns 1 if successful, 0 if contention */
static int
int
__down_write_trylock_locked(struct rw_semaphore *sem)
{
int ret = 0;
if (sem->activity == 0 && list_empty(&sem->wait_list)) {
/* granted */
sem->activity = -1;
ret = 1;
}
return ret;
}
EXPORT_SYMBOL(__down_write_trylock_locked);
#endif
void
__rw_init(krwlock_t *rwlp, char *name, krw_type_t type, void *arg)
{
int flags = KM_SLEEP;
ASSERT(rwlp);
ASSERT(name);
ASSERT(type == RW_DEFAULT); /* XXX no irq handler use */
ASSERT(arg == NULL); /* XXX no irq handler use */
rwlp->rw_magic = RW_MAGIC;
rwlp->rw_owner = NULL;
rwlp->rw_name = NULL;
rwlp->rw_name_size = strlen(name) + 1;
/* We may be called when there is a non-zero preempt_count or
* interrupts are disabled is which case we must not sleep.
*/
if (current_thread_info()->preempt_count || irqs_disabled())
flags = KM_NOSLEEP;
rwlp->rw_name = kmem_alloc(rwlp->rw_name_size, flags);
if (rwlp->rw_name == NULL)
return;
init_rwsem(&rwlp->rw_sem);
strcpy(rwlp->rw_name, name);
}
EXPORT_SYMBOL(__rw_init);
void
__rw_destroy(krwlock_t *rwlp)
{
ASSERT(rwlp);
ASSERT(rwlp->rw_magic == RW_MAGIC);
ASSERT(rwlp->rw_owner == NULL);
spin_lock(&rwlp->rw_sem.wait_lock);
ASSERT(list_empty(&rwlp->rw_sem.wait_list));
spin_unlock(&rwlp->rw_sem.wait_lock);
kmem_free(rwlp->rw_name, rwlp->rw_name_size);
memset(rwlp, RW_POISON, sizeof(krwlock_t));
}
EXPORT_SYMBOL(__rw_destroy);
/* Return 0 if the lock could not be obtained without blocking. */
int
__rw_tryenter(krwlock_t *rwlp, krw_t rw)
{
int rc = 0;
ENTRY;
ASSERT(rwlp);
ASSERT(rwlp->rw_magic == RW_MAGIC);
switch (rw) {
/* these functions return 1 if success, 0 if contention */
case RW_READER:
/* Here the Solaris code would return 0
* if there were any write waiters. Specifically
* thinking about the case where readers may have
* the lock and we would also allow this thread
* to grab the read lock with a writer waiting in the
* queue. This doesn't seem like a correctness
* issue, so just call down_read_trylock()
* for the test. We may have to revisit this if
* it becomes an issue */
rc = down_read_trylock(&rwlp->rw_sem);
break;
case RW_WRITER:
rc = down_write_trylock(&rwlp->rw_sem);
if (rc) {
/* there better not be anyone else
* holding the write lock here */
ASSERT(rwlp->rw_owner == NULL);
rwlp->rw_owner = current;
}
break;
default:
SBUG();
}
RETURN(rc);
}
EXPORT_SYMBOL(__rw_tryenter);
void
__rw_enter(krwlock_t *rwlp, krw_t rw)
{
ENTRY;
ASSERT(rwlp);
ASSERT(rwlp->rw_magic == RW_MAGIC);
switch (rw) {
case RW_READER:
/* Here the Solaris code would block
* if there were any write waiters. Specifically
* thinking about the case where readers may have
* the lock and we would also allow this thread
* to grab the read lock with a writer waiting in the
* queue. This doesn't seem like a correctness
* issue, so just call down_read()
* for the test. We may have to revisit this if
* it becomes an issue */
down_read(&rwlp->rw_sem);
break;
case RW_WRITER:
down_write(&rwlp->rw_sem);
/* there better not be anyone else
* holding the write lock here */
ASSERT(rwlp->rw_owner == NULL);
rwlp->rw_owner = current;
break;
default:
SBUG();
}
EXIT;
}
EXPORT_SYMBOL(__rw_enter);
void
__rw_exit(krwlock_t *rwlp)
{
ENTRY;
ASSERT(rwlp);
ASSERT(rwlp->rw_magic == RW_MAGIC);
/* rw_owner is held by current
* thread iff it is a writer */
if (rwlp->rw_owner == current) {
rwlp->rw_owner = NULL;
up_write(&rwlp->rw_sem);
} else {
up_read(&rwlp->rw_sem);
}
EXIT;
}
EXPORT_SYMBOL(__rw_exit);
void
__rw_downgrade(krwlock_t *rwlp)
{
ENTRY;
ASSERT(rwlp);
ASSERT(rwlp->rw_magic == RW_MAGIC);
ASSERT(rwlp->rw_owner == current);
rwlp->rw_owner = NULL;
downgrade_write(&rwlp->rw_sem);
EXIT;
}
EXPORT_SYMBOL(__rw_downgrade);
/* Return 0 if unable to perform the upgrade.
* Might be wise to fix the caller
* to acquire the write lock first?
*/
int
__rw_tryupgrade(krwlock_t *rwlp)
{
int rc = 0;
ENTRY;
ASSERT(rwlp);
ASSERT(rwlp->rw_magic == RW_MAGIC);
spin_lock(&rwlp->rw_sem.wait_lock);
/* Check if there is anyone waiting for the
* lock. If there is, then we know we should
* not try to upgrade the lock */
if (!list_empty(&rwlp->rw_sem.wait_list)) {
spin_unlock(&rwlp->rw_sem.wait_lock);
RETURN(0);
}
#ifdef CONFIG_RWSEM_GENERIC_SPINLOCK
/* Note that activity is protected by
* the wait_lock. Don't try to upgrade
* if there are multiple readers currently
* holding the lock */
if (rwlp->rw_sem.activity > 1) {
#else
/* Don't try to upgrade
* if there are multiple readers currently
* holding the lock */
if ((rwlp->rw_sem.count & RWSEM_ACTIVE_MASK) > 1) {
#endif
spin_unlock(&rwlp->rw_sem.wait_lock);
RETURN(0);
}
#ifdef CONFIG_RWSEM_GENERIC_SPINLOCK
/* Here it should be safe to drop the
* read lock and reacquire it for writing since
* we know there are no waiters */
__up_read_locked(&rwlp->rw_sem);
/* returns 1 if success, 0 if contention */
rc = __down_write_trylock_locked(&rwlp->rw_sem);
#else
/* Here it should be safe to drop the
* read lock and reacquire it for writing since
* we know there are no waiters */
up_read(&rwlp->rw_sem);
/* returns 1 if success, 0 if contention */
rc = down_write_trylock(&rwlp->rw_sem);
#endif
/* Check if upgrade failed. Should not ever happen
* if we got to this point */
ASSERT(rc);
ASSERT(rwlp->rw_owner == NULL);
rwlp->rw_owner = current;
spin_unlock(&rwlp->rw_sem.wait_lock);
RETURN(1);
}
EXPORT_SYMBOL(__rw_tryupgrade);
kthread_t *
__rw_owner(krwlock_t *rwlp)
{
ENTRY;
ASSERT(rwlp);
ASSERT(rwlp->rw_magic == RW_MAGIC);
RETURN(rwlp->rw_owner);
}
EXPORT_SYMBOL(__rw_owner);
int
__rw_read_held(krwlock_t *rwlp)
{
ENTRY;
ASSERT(rwlp);
ASSERT(rwlp->rw_magic == RW_MAGIC);
RETURN(__rw_lock_held(rwlp) && rwlp->rw_owner == NULL);
}
EXPORT_SYMBOL(__rw_read_held);
int
__rw_write_held(krwlock_t *rwlp)
{
ENTRY;
ASSERT(rwlp);
ASSERT(rwlp->rw_magic == RW_MAGIC);
RETURN(rwlp->rw_owner == current);
}
EXPORT_SYMBOL(__rw_write_held);
int
__rw_lock_held(krwlock_t *rwlp)
{
int rc = 0;
ENTRY;
ASSERT(rwlp);
ASSERT(rwlp->rw_magic == RW_MAGIC);
spin_lock_irq(&(rwlp->rw_sem.wait_lock));
#ifdef CONFIG_RWSEM_GENERIC_SPINLOCK
if (rwlp->rw_sem.activity != 0) {
#else
if (rwlp->rw_sem.count != 0) {
#endif
rc = 1;
}
spin_unlock_irq(&(rwlp->rw_sem.wait_lock));
RETURN(rc);
}
EXPORT_SYMBOL(__rw_lock_held);

View File

@ -41,6 +41,7 @@
#include <linux/device.h>
#include <linux/list.h>
#include <linux/swap.h>
#include <linux/delay.h>
#include <asm/ioctls.h>
#include <asm/uaccess.h>
@ -195,6 +196,15 @@ typedef struct splat_info {
#define splat_vprint(file, test, format, args...) \
splat_print(file, "%*s: " format, SPLAT_NAME_SIZE, test, args)
#define splat_locked_test(lock, test) \
({ \
int _rc_; \
spin_lock(lock); \
_rc_ = (test) ? 1 : 0; \
spin_unlock(lock); \
_rc_; \
})
splat_subsystem_t *splat_condvar_init(void);
splat_subsystem_t *splat_kmem_init(void);
splat_subsystem_t *splat_mutex_init(void);

File diff suppressed because it is too large Load Diff

View File

@ -92,3 +92,15 @@ index f5b7d17..1468a22 100644
static inline int zref_in_nodemask(struct zoneref *zref, nodemask_t *nodes)
{
diff --git a/kernel/fork.c b/kernel/fork.c
index 9b42695..852499e 100644
--- a/kernel/fork.c
+++ b/kernel/fork.c
@@ -159,6 +159,7 @@ void __put_task_struct(struct task_struct *tsk)
if (!profile_handoff_task(tsk))
free_task(tsk);
}
+EXPORT_SYMBOL(__put_task_struct);
/*
* macro override instead of weak attribute alias, to workaround

View File

@ -117,6 +117,9 @@
/* pgdat_list is available */
#undef HAVE_PGDAT_LIST
/* __put_task_struct() is available */
#undef HAVE_PUT_TASK_STRUCT
/* set_normalized_timespec() is available as export */
#undef HAVE_SET_NORMALIZED_TIMESPEC_EXPORT