mirror of
				https://github.com/torvalds/linux.git
				synced 2025-11-04 02:30:34 +02:00 
			
		
		
		
	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 <cuiyunhui@bytedance.com> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Link: https://lkml.kernel.org/r/20250126033243.53069-1-cuiyunhui@bytedance.com
This commit is contained in:
		
							parent
							
								
									2d352ec9fc
								
							
						
					
					
						commit
						337369f8ce
					
				
					 1 changed files with 2 additions and 0 deletions
				
			
		| 
						 | 
				
			
			@ -143,6 +143,8 @@ static __always_inline bool __mutex_trylock_fast(struct mutex *lock)
 | 
			
		|||
	unsigned long curr = (unsigned long)current;
 | 
			
		||||
	unsigned long zero = 0UL;
 | 
			
		||||
 | 
			
		||||
	MUTEX_WARN_ON(lock->magic != lock);
 | 
			
		||||
 | 
			
		||||
	if (atomic_long_try_cmpxchg_acquire(&lock->owner, &zero, curr))
 | 
			
		||||
		return true;
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in a new issue