Skip to content

Commit 337369f

Browse files
cuiyunhuiPeter Zijlstra
authored andcommitted
locking/mutex: Add MUTEX_WARN_ON() into fast path
Scenario: In platform_device_register, the driver misuses struct device as platform_data, making kmemdup duplicate a device. Accessing the duplicate may cause list corruption due to its mutex magic or list holding old content. It recurs randomly as the first mutex - getting process skips the slow path and mutex check. Adding MUTEX_WARN_ON(lock->magic!= lock) in __mutex_trylock_fast() makes it always happen. Signed-off-by: Yunhui Cui <[email protected]> Signed-off-by: Peter Zijlstra (Intel) <[email protected]> Link: https://lkml.kernel.org/r/[email protected]
1 parent 2d352ec commit 337369f

File tree

1 file changed

+2
-0
lines changed

1 file changed

+2
-0
lines changed

kernel/locking/mutex.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,8 @@ static __always_inline bool __mutex_trylock_fast(struct mutex *lock)
143143
unsigned long curr = (unsigned long)current;
144144
unsigned long zero = 0UL;
145145

146+
MUTEX_WARN_ON(lock->magic != lock);
147+
146148
if (atomic_long_try_cmpxchg_acquire(&lock->owner, &zero, curr))
147149
return true;
148150

0 commit comments

Comments
 (0)