locking/rwsem: Add DEBUG_RWSEMS to look for lock/unlock mismatches
Subsystems (2)
Waiman Long
reviewerPATCH 1/1 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.
--- kernel/locking/rwsem.c | 4 ++++ kernel/locking/rwsem.h | 8 +++++++- lib/Kconfig.debug | 7 +++++++ 3 files changed, 18 insertions(+), 1 deletion(-)
kernel/locking/rwsem.c +4 -0
@@ -117,6 +117,7 @@ int down_write_trylock(struct rw_semaphore *sem)void up_read (struct rw_semaphore *sem) {rwsem_release (&sem->dep_map,1 , _RET_IP_); +DEBUG_RWSEMS_WARN_ON (sem->owner !=RWSEM_READER_OWNED );__up_read (sem); }
@@ -129,6 +130,7 @@ void up_read(struct rw_semaphore *sem)void up_write (struct rw_semaphore *sem) {rwsem_release (&sem->dep_map,1 , _RET_IP_); +DEBUG_RWSEMS_WARN_ON (sem->owner != current);rwsem_clear_owner (sem);__up_write (sem);
@@ -142,6 +144,7 @@ void up_write(struct rw_semaphore *sem)void downgrade_write (struct rw_semaphore *sem) {lock_downgrade (&sem->dep_map, _RET_IP_); +DEBUG_RWSEMS_WARN_ON (sem->owner != current);rwsem_set_reader_owned (sem);__downgrade_write (sem);
@@ -211,6 +214,7 @@ int __sched down_write_killable_nested(struct rw_semaphore *sem, int subclass)void up_read_non_owner (struct rw_semaphore *sem) { +DEBUG_RWSEMS_WARN_ON (RWSEM_WRITER_OWNED (sem->owner));__up_read (sem); }
kernel/locking/rwsem.h +7 -1
@@ -16,6 +16,12 @@*/ #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 /* * 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)* do a write to the rwsem cacheline when it is really necessary * 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 ); }
lib/Kconfig.debug +7 -0
@@ -1075,6 +1075,13 @@ config DEBUG_WW_MUTEX_SLOWPATH even a debug kernel. If you are a driver writer, enable it. If 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 feature allows mismatched rw semaphore locks and unlocks + to be detected and reported. + config DEBUG_LOCK_ALLOC bool "Lock debugging: detect incorrect freeing of live locks" depends on DEBUG_KERNEL && TRACE_IRQFLAGS_SUPPORT && STACKTRACE_SUPPORT && LOCKDEP_SUPPORT
Signature · 1 line
1.8.3.1
kbuild test robot
build botThank you for the patch! Yet something to improve:
Hi Waiman,
Thank you for the patch! Yet something to improve:
[auto build test ERROR on tip/locking/core]
[also build test ERROR on v4.16-rc7 next-20180326]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]
url: https://github.com/0day-ci/linux/commits/Waiman-Long/locking-rwsem-Add-DEBUG_RWSEMS-to-look-for-lock-unlock-mismatches/20180327-151310
config: i386-allmodconfig (attached as .config)
compiler: gcc-7 (Debian 7.3.0-1) 7.3.0 reproduce:
# save the attached .config to linux build tree
make ARCH=i386 All error/warnings (new ones prefixed by >>):
In file included from include/linux/kernel.h:10:0,
from kernel/locking/rwsem.c:9:
kernel/locking/rwsem.c: In function 'up_read_non_owner':
>> kernel/locking/rwsem.c:217:23: error: implicit declaration of function 'RWSEM_WRITER_OWNED'; did you mean 'RWSEM_READER_OWNED'? [-Werror=implicit-function-declaration]
DEBUG_RWSEMS_WARN_ON(RWSEM_WRITER_OWNED(sem->owner));
^
include/linux/compiler.h:77:42: note: in definition of macro 'unlikely'
# define unlikely(x) __builtin_expect(!!(x), 0)
^
>> kernel/locking/rwsem.h:20:33: note: in expansion of macro 'DEBUG_LOCKS_WARN_ON'
#define DEBUG_RWSEMS_WARN_ON(c) DEBUG_LOCKS_WARN_ON(c)
^~~~~~~~~~~~~~~~~~~
>> kernel/locking/rwsem.c:217:2: note: in expansion of macro 'DEBUG_RWSEMS_WARN_ON'
DEBUG_RWSEMS_WARN_ON(RWSEM_WRITER_OWNED(sem->owner));
^~~~~~~~~~~~~~~~~~~~cc1: some warnings being treated as errors
vim +217 kernel/locking/rwsem.c
214
215 void up_read_non_owner(struct rw_semaphore *sem)
216 {
> 217 DEBUG_RWSEMS_WARN_ON(RWSEM_WRITER_OWNED(sem->owner));
218 __up_read(sem);
219 }
220
---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation
Waiman Long
reviewerQuoted 12 lines · kbuild test robot
Hi Waiman,
Thank you for the patch! Yet something to improve:
[auto build test ERROR on tip/locking/core]
[also build test ERROR on v4.16-rc7 next-20180326]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]url: https://github.com/0day-ci/linux/commits/Waiman-Long/locking-rwsem-Add-DEBUG_RWSEMS-to-look-for-lock-unlock-mismatches/20180327-151310
config: i386-allmodconfig (attached as .config)
compiler: gcc-7 (Debian 7.3.0-1) 7.3.0 reproduce:
Quoted 9 lines
# save the attached .config to linux build tree make ARCH=i386All error/warnings (new ones prefixed by >>):
In file included from
include/linux/kernel.h:10:0,
fromkernel/locking/rwsem.c:9:
kernel/locking/rwsem.c: In function 'up_read_non_owner':Quoted 1 line
Quoted 1 line
kernel/locking/rwsem.c:217:23: error: implicit declaration of function 'RWSEM_WRITER_OWNED'; did you mean 'RWSEM_READER_OWNED'? [-Werror=implicit-function-declaration]
Yes, I noticed that error right after sending out the patch. That is why I sent out a v2 patch immediately to fix this. Thanks for the notification anyway.
Cheers,
Longman