]> git.proxmox.com Git - mirror_spl.git/commitdiff
Correctly handle rwsem_is_locked() behavior
authorNed Bass <bass6@llnl.gov>
Tue, 10 Aug 2010 18:01:46 +0000 (11:01 -0700)
committerBrian Behlendorf <behlendorf1@llnl.gov>
Tue, 10 Aug 2010 23:43:00 +0000 (16:43 -0700)
A race condition in rwsem_is_locked() was fixed in Linux 2.6.33 and the fix was
backported to RHEL5 as of kernel 2.6.18-190.el5.  Details can be found here:

https://bugzilla.redhat.com/show_bug.cgi?id=526092

The race condition was fixed in the kernel by acquiring the semaphore's
wait_lock inside rwsem_is_locked().  The SPL worked around the race condition
by acquiring the wait_lock before calling that function, but with the fix in
place it must not do that.

This commit implements an autoconf test to detect whether the fixed version of
rwsem_is_locked() is present.  The previous version of rwsem_is_locked() was an
inline static function while the new version is exported as a symbol which we
can check for in module.symvers.  Depending on the result we correctly
implement the needed compatibility macros for proper spinlock handling.

Finally, we do the right thing with spin locks in RW_*_HELD() by using the
new compatibility macros.  We only only acquire the semaphore's wait_lock if
it is calling a rwsem_is_locked() that does not itself try to acquire the lock.

Some new overhead and a small harmless race is introduced by this change.
This is because RW_READ_HELD() and RW_WRITE_HELD() now acquire and release
the wait_lock twice: once for the call to rwsem_is_locked() and once for
the call to rw_owner().  This can't be avoided if calling a rwsem_is_locked()
that takes the wait_lock, as it will in more recent kernels.

The other case which only occurs in legacy kernels could be optimized by
taking the lock only once, as was done prior to this commit.  However, I
decided that the performance gain probably wasn't significant enough to
justify the messy special cases required.

The function spl_rw_get_owner() was only used to enable the afore-mentioned
optimization.  Since it is no longer used, I removed it.

Signed-off-by: Brian Behlendorf <behlendorf1@llnl.gov>
config/spl-build.m4
configure
include/linux/rwsem_compat.h [new file with mode: 0644]
include/sys/rwlock.h
spl_config.h.in

index facaf7404c73961bded44ca7db65ea16d07eb973..0b9f8f4301b1a2328d83e5a932f5ddfdaa00ab24 100644 (file)
@@ -77,6 +77,7 @@ AC_DEFUN([SPL_AC_CONFIG_KERNEL], [
        SPL_AC_5ARGS_PROC_HANDLER
        SPL_AC_KVASPRINTF
        SPL_AC_3ARGS_FILE_FSYNC
+       SPL_AC_EXPORTED_RWSEM_IS_LOCKED
 ])
 
 AC_DEFUN([SPL_AC_MODULE_SYMVERS], [
@@ -1598,3 +1599,19 @@ AC_DEFUN([SPL_AC_3ARGS_FILE_FSYNC], [
                AC_MSG_RESULT(no)
        ])
 ])
+
+dnl #
+dnl # 2.6.33 API change. Also backported in RHEL5 as of 2.6.18-190.el5.
+dnl # Earlier versions of rwsem_is_locked() were inline and had a race
+dnl # condition.  The fixed version is exported as a symbol.  The race
+dnl # condition is fixed by acquiring sem->wait_lock, so we must not
+dnl # call that version while holding sem->wait_lock.
+dnl #
+AC_DEFUN([SPL_AC_EXPORTED_RWSEM_IS_LOCKED], [
+       SPL_CHECK_SYMBOL_EXPORT(
+               [rwsem_is_locked],
+               [lib/rwsem-spinlock.c],
+               [AC_DEFINE(RWSEM_IS_LOCKED_TAKES_WAIT_LOCK, 1,
+               [rwsem_is_locked() acquires sem->wait_lock])],
+               [])
+])
index bcef31e4744e8c8092413359bca1e9d4d24bcbd9..285c9a434ddddd7424eeaac5aa65d63fbef9ca49 100755 (executable)
--- a/configure
+++ b/configure
 
 
 
+
+       { $as_echo "$as_me:$LINENO: checking whether symbol rwsem_is_locked is exported" >&5
+$as_echo_n "checking whether symbol rwsem_is_locked is exported... " >&6; }
+       grep -q -E '[[:space:]]rwsem_is_locked[[:space:]]' \
+               $LINUX_OBJ/Module*.symvers 2>/dev/null
+       rc=$?
+       if test $rc -ne 0; then
+               export=0
+               for file in lib/rwsem-spinlock.c; do
+                       grep -q -E "EXPORT_SYMBOL.*(rwsem_is_locked)" \
+                               "$LINUX_OBJ/$file" 2>/dev/null
+                       rc=$?
+                       if test $rc -eq 0; then
+                               export=1
+                               break;
+                       fi
+               done
+               if test $export -eq 0; then
+                       { $as_echo "$as_me:$LINENO: result: no" >&5
+$as_echo "no" >&6; }
+
+               else
+                       { $as_echo "$as_me:$LINENO: result: yes" >&5
+$as_echo "yes" >&6; }
+
+cat >>confdefs.h <<\_ACEOF
+#define RWSEM_IS_LOCKED_TAKES_WAIT_LOCK 1
+_ACEOF
+
+               fi
+       else
+               { $as_echo "$as_me:$LINENO: result: yes" >&5
+$as_echo "yes" >&6; }
+
+cat >>confdefs.h <<\_ACEOF
+#define RWSEM_IS_LOCKED_TAKES_WAIT_LOCK 1
+_ACEOF
+
+       fi
+
+
  ;;
                 user)
 
 
 
 
+       { $as_echo "$as_me:$LINENO: checking whether symbol rwsem_is_locked is exported" >&5
+$as_echo_n "checking whether symbol rwsem_is_locked is exported... " >&6; }
+       grep -q -E '[[:space:]]rwsem_is_locked[[:space:]]' \
+               $LINUX_OBJ/Module*.symvers 2>/dev/null
+       rc=$?
+       if test $rc -ne 0; then
+               export=0
+               for file in lib/rwsem-spinlock.c; do
+                       grep -q -E "EXPORT_SYMBOL.*(rwsem_is_locked)" \
+                               "$LINUX_OBJ/$file" 2>/dev/null
+                       rc=$?
+                       if test $rc -eq 0; then
+                               export=1
+                               break;
+                       fi
+               done
+               if test $export -eq 0; then
+                       { $as_echo "$as_me:$LINENO: result: no" >&5
+$as_echo "no" >&6; }
+
+               else
+                       { $as_echo "$as_me:$LINENO: result: yes" >&5
+$as_echo "yes" >&6; }
+
+cat >>confdefs.h <<\_ACEOF
+#define RWSEM_IS_LOCKED_TAKES_WAIT_LOCK 1
+_ACEOF
+
+               fi
+       else
+               { $as_echo "$as_me:$LINENO: result: yes" >&5
+$as_echo "yes" >&6; }
+
+cat >>confdefs.h <<\_ACEOF
+#define RWSEM_IS_LOCKED_TAKES_WAIT_LOCK 1
+_ACEOF
+
+       fi
+
+
+
 
 
        if test "x$AWK" != xgawk; then
diff --git a/include/linux/rwsem_compat.h b/include/linux/rwsem_compat.h
new file mode 100644 (file)
index 0000000..67a82bb
--- /dev/null
@@ -0,0 +1,63 @@
+/*****************************************************************************\
+ *  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://github.com/behlendorf/spl/>.
+ *
+ *  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.
+ *
+ *  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.
+ *
+ *  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_RWSEM_COMPAT_H
+#define _SPL_RWSEM_COMPAT_H
+
+#include <linux/rwsem.h>
+
+#ifdef RWSEM_IS_LOCKED_TAKES_WAIT_LOCK
+/*
+ * A race condition in rwsem_is_locked() was fixed in Linux 2.6.33 and the fix
+ * was backported to RHEL5 as of kernel 2.6.18-190.el5.  Details can be found
+ * here:
+ *
+ * https://bugzilla.redhat.com/show_bug.cgi?id=526092
+
+ * The race condition was fixed in the kernel by acquiring the semaphore's
+ * wait_lock inside rwsem_is_locked().  The SPL worked around the race
+ * condition by acquiring the wait_lock before calling that function, but
+ * with the fix in place we must not do that.
+ */
+
+#define spl_rwsem_is_locked(rwsem)                                     \
+({                                                                     \
+       rwsem_is_locked(rwsem);                                         \
+})
+
+#else
+
+#define spl_rwsem_is_locked(rwsem)                                     \
+({                                                                     \
+       unsigned long _flags_;                                          \
+       int _rc_;                                                       \
+       spin_lock_irqsave(&rwsem->wait_lock, _flags_);                  \
+       _rc_ = rwsem_is_locked(rwsem);                                  \
+       spin_unlock_irqrestore(&rwsem->wait_lock, _flags_);             \
+       _rc_;                                                           \
+})
+
+#endif /* RWSEM_IS_LOCKED_TAKES_WAIT_LOCK */
+
+#endif /* _SPL_RWSEM_COMPAT_H */
index eb763ec78ab2f38e70dc9680cbcd6aa43d35093f..3d9808599c6d3cd7ad28c81ce079edec78cbe013 100644 (file)
@@ -27,6 +27,7 @@
 
 #include <sys/types.h>
 #include <linux/rwsem.h>
+#include <linux/rwsem_compat.h>
 
 typedef enum {
         RW_DRIVER  = 2,
@@ -46,12 +47,6 @@ typedef struct {
 
 #define SEM(rwp)                        ((struct rw_semaphore *)(rwp))
 
-static inline kthread_t *
-spl_rw_get_owner(krwlock_t *rwp)
-{
-        return rwp->rw_owner;
-}
-
 static inline void
 spl_rw_set_owner(krwlock_t *rwp)
 {
@@ -79,7 +74,7 @@ rw_owner(krwlock_t *rwp)
         kthread_t *owner;
 
         spin_lock_irqsave(&SEM(rwp)->wait_lock, flags);
-        owner = spl_rw_get_owner(rwp);
+        owner = rwp->rw_owner;
         spin_unlock_irqrestore(&SEM(rwp)->wait_lock, flags);
 
         return owner;
@@ -88,40 +83,21 @@ rw_owner(krwlock_t *rwp)
 static inline int
 RW_READ_HELD(krwlock_t *rwp)
 {
-        unsigned long flags;
-        int rc;
-
-        spin_lock_irqsave(&SEM(rwp)->wait_lock, flags);
-        rc = (rwsem_is_locked(SEM(rwp)) && spl_rw_get_owner(rwp) == NULL);
-        spin_unlock_irqrestore(&SEM(rwp)->wait_lock, flags);
-
-        return rc;
+       return (spl_rwsem_is_locked(SEM(rwp)) &&
+               rw_owner(rwp) == NULL);
 }
 
 static inline int
 RW_WRITE_HELD(krwlock_t *rwp)
 {
-        unsigned long flags;
-        int rc;
-
-        spin_lock_irqsave(&SEM(rwp)->wait_lock, flags);
-        rc = (rwsem_is_locked(SEM(rwp)) && spl_rw_get_owner(rwp) == current);
-        spin_unlock_irqrestore(&SEM(rwp)->wait_lock, flags);
-
-        return rc;
+       return (spl_rwsem_is_locked(SEM(rwp)) &&
+               rw_owner(rwp) == current);
 }
 
 static inline int
 RW_LOCK_HELD(krwlock_t *rwp)
 {
-        unsigned long flags;
-        int rc;
-
-        spin_lock_irqsave(&SEM(rwp)->wait_lock, flags);
-        rc = rwsem_is_locked(SEM(rwp));
-        spin_unlock_irqrestore(&SEM(rwp)->wait_lock, flags);
-
-        return rc;
+       return spl_rwsem_is_locked(SEM(rwp));
 }
 
 /*
index 8d57a63ea215d7bd87811b8610e058767711082f..d3928f4ba9cc649b260d8df24aa5dbc11de3365f 100644 (file)
 /* Define to the version of this package. */
 #undef PACKAGE_VERSION
 
+/* rwsem_is_locked() acquires sem->wait_lock */
+#undef RWSEM_IS_LOCKED_TAKES_WAIT_LOCK
+
 /* Define the project alias string. */
 #undef SPL_META_ALIAS