Commit 5149cbac authored by Waiman Long's avatar Waiman Long Committed by Ingo Molnar

locking/rwsem: Add DEBUG_RWSEMS to look for lock/unlock mismatches

For a rwsem, locking can either be exclusive or shared. The corresponding
exclusive or shared unlock must be used. Otherwise, the protected data
structures may get corrupted or the lock may be in an inconsistent state.

In order to detect such anomaly, a new configuration option DEBUG_RWSEMS
is added which can be enabled to look for such mismatches and print
warnings that that happens.
Signed-off-by: default avatarWaiman Long <longman@redhat.com>
Acked-by: default avatarDavidlohr Bueso <dave@stgolabs.net>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Link: http://lkml.kernel.org/r/1522445280-7767-2-git-send-email-longman@redhat.comSigned-off-by: default avatarIngo Molnar <mingo@kernel.org>
parent 169310f7
...@@ -117,6 +117,7 @@ EXPORT_SYMBOL(down_write_trylock); ...@@ -117,6 +117,7 @@ EXPORT_SYMBOL(down_write_trylock);
void up_read(struct rw_semaphore *sem) void up_read(struct rw_semaphore *sem)
{ {
rwsem_release(&sem->dep_map, 1, _RET_IP_); rwsem_release(&sem->dep_map, 1, _RET_IP_);
DEBUG_RWSEMS_WARN_ON(sem->owner != RWSEM_READER_OWNED);
__up_read(sem); __up_read(sem);
} }
...@@ -129,6 +130,7 @@ EXPORT_SYMBOL(up_read); ...@@ -129,6 +130,7 @@ EXPORT_SYMBOL(up_read);
void up_write(struct rw_semaphore *sem) void up_write(struct rw_semaphore *sem)
{ {
rwsem_release(&sem->dep_map, 1, _RET_IP_); rwsem_release(&sem->dep_map, 1, _RET_IP_);
DEBUG_RWSEMS_WARN_ON(sem->owner != current);
rwsem_clear_owner(sem); rwsem_clear_owner(sem);
__up_write(sem); __up_write(sem);
...@@ -142,6 +144,7 @@ EXPORT_SYMBOL(up_write); ...@@ -142,6 +144,7 @@ EXPORT_SYMBOL(up_write);
void downgrade_write(struct rw_semaphore *sem) void downgrade_write(struct rw_semaphore *sem)
{ {
lock_downgrade(&sem->dep_map, _RET_IP_); lock_downgrade(&sem->dep_map, _RET_IP_);
DEBUG_RWSEMS_WARN_ON(sem->owner != current);
rwsem_set_reader_owned(sem); rwsem_set_reader_owned(sem);
__downgrade_write(sem); __downgrade_write(sem);
...@@ -211,6 +214,7 @@ EXPORT_SYMBOL(down_write_killable_nested); ...@@ -211,6 +214,7 @@ EXPORT_SYMBOL(down_write_killable_nested);
void up_read_non_owner(struct rw_semaphore *sem) void up_read_non_owner(struct rw_semaphore *sem)
{ {
DEBUG_RWSEMS_WARN_ON(sem->owner != RWSEM_READER_OWNED);
__up_read(sem); __up_read(sem);
} }
......
...@@ -16,6 +16,12 @@ ...@@ -16,6 +16,12 @@
*/ */
#define RWSEM_READER_OWNED ((struct task_struct *)1UL) #define RWSEM_READER_OWNED ((struct task_struct *)1UL)
#ifdef CONFIG_DEBUG_RWSEMS
# define DEBUG_RWSEMS_WARN_ON(c) DEBUG_LOCKS_WARN_ON(c)
#else
# define DEBUG_RWSEMS_WARN_ON(c)
#endif
#ifdef CONFIG_RWSEM_SPIN_ON_OWNER #ifdef CONFIG_RWSEM_SPIN_ON_OWNER
/* /*
* All writes to owner are protected by WRITE_ONCE() to make sure that * All writes to owner are protected by WRITE_ONCE() to make sure that
...@@ -41,7 +47,7 @@ static inline void rwsem_set_reader_owned(struct rw_semaphore *sem) ...@@ -41,7 +47,7 @@ static inline void rwsem_set_reader_owned(struct rw_semaphore *sem)
* do a write to the rwsem cacheline when it is really necessary * do a write to the rwsem cacheline when it is really necessary
* to minimize cacheline contention. * to minimize cacheline contention.
*/ */
if (sem->owner != RWSEM_READER_OWNED) if (READ_ONCE(sem->owner) != RWSEM_READER_OWNED)
WRITE_ONCE(sem->owner, RWSEM_READER_OWNED); WRITE_ONCE(sem->owner, RWSEM_READER_OWNED);
} }
......
...@@ -1075,6 +1075,13 @@ config DEBUG_WW_MUTEX_SLOWPATH ...@@ -1075,6 +1075,13 @@ config DEBUG_WW_MUTEX_SLOWPATH
even a debug kernel. If you are a driver writer, enable it. If even a debug kernel. If you are a driver writer, enable it. If
you are a distro, do not. you are a distro, do not.
config DEBUG_RWSEMS
bool "RW Semaphore debugging: basic checks"
depends on DEBUG_KERNEL && RWSEM_SPIN_ON_OWNER
help
This debugging feature allows mismatched rw semaphore locks and unlocks
to be detected and reported.
config DEBUG_LOCK_ALLOC config DEBUG_LOCK_ALLOC
bool "Lock debugging: detect incorrect freeing of live locks" bool "Lock debugging: detect incorrect freeing of live locks"
depends on DEBUG_KERNEL && TRACE_IRQFLAGS_SUPPORT && STACKTRACE_SUPPORT && LOCKDEP_SUPPORT depends on DEBUG_KERNEL && TRACE_IRQFLAGS_SUPPORT && STACKTRACE_SUPPORT && LOCKDEP_SUPPORT
...@@ -1097,6 +1104,7 @@ config PROVE_LOCKING ...@@ -1097,6 +1104,7 @@ config PROVE_LOCKING
select DEBUG_SPINLOCK select DEBUG_SPINLOCK
select DEBUG_MUTEXES select DEBUG_MUTEXES
select DEBUG_RT_MUTEXES if RT_MUTEXES select DEBUG_RT_MUTEXES if RT_MUTEXES
select DEBUG_RWSEMS if RWSEM_SPIN_ON_OWNER
select DEBUG_LOCK_ALLOC select DEBUG_LOCK_ALLOC
select TRACE_IRQFLAGS select TRACE_IRQFLAGS
default n default n
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment