forked from mirrors/linux
		
	 fbd7a5a035
			
		
	
	
		fbd7a5a035
		
	
	
	
	
		
			
			Since we've exposed Lock::from_raw() and Guard::new() publically, we
want to be able to make sure that we assert that a lock is actually held
when constructing a Guard for it to handle instances of unsafe
Guard::new() calls outside of our lock module.
Hence add a new method assert_is_held() to Backend, which uses lockdep
to check whether or not a lock has been acquired. When lockdep is
disabled, this has no overhead.
[Boqun: Resolve the conflicts with exposing Guard::new(), reword the
 commit log a bit and format "unsafe { <statement>; }" into "unsafe {
 <statement> }" for the consistency. ]
Signed-off-by: Lyude Paul <lyude@redhat.com>
Signed-off-by: Boqun Feng <boqun.feng@gmail.com>
Link: https://lore.kernel.org/r/20241125204139.656801-1-lyude@redhat.com
		
	
			
		
			
				
	
	
		
			37 lines
		
	
	
	
		
			814 B
		
	
	
	
		
			C
		
	
	
	
	
	
			
		
		
	
	
			37 lines
		
	
	
	
		
			814 B
		
	
	
	
		
			C
		
	
	
	
	
	
| // SPDX-License-Identifier: GPL-2.0
 | |
| 
 | |
| #include <linux/spinlock.h>
 | |
| 
 | |
| void rust_helper___spin_lock_init(spinlock_t *lock, const char *name,
 | |
| 				  struct lock_class_key *key)
 | |
| {
 | |
| #ifdef CONFIG_DEBUG_SPINLOCK
 | |
| # if defined(CONFIG_PREEMPT_RT)
 | |
| 	__spin_lock_init(lock, name, key, false);
 | |
| # else /*!CONFIG_PREEMPT_RT */
 | |
| 	__raw_spin_lock_init(spinlock_check(lock), name, key, LD_WAIT_CONFIG);
 | |
| # endif /* CONFIG_PREEMPT_RT */
 | |
| #else /* !CONFIG_DEBUG_SPINLOCK */
 | |
| 	spin_lock_init(lock);
 | |
| #endif /* CONFIG_DEBUG_SPINLOCK */
 | |
| }
 | |
| 
 | |
| void rust_helper_spin_lock(spinlock_t *lock)
 | |
| {
 | |
| 	spin_lock(lock);
 | |
| }
 | |
| 
 | |
| void rust_helper_spin_unlock(spinlock_t *lock)
 | |
| {
 | |
| 	spin_unlock(lock);
 | |
| }
 | |
| 
 | |
| int rust_helper_spin_trylock(spinlock_t *lock)
 | |
| {
 | |
| 	return spin_trylock(lock);
 | |
| }
 | |
| 
 | |
| void rust_helper_spin_assert_is_held(spinlock_t *lock)
 | |
| {
 | |
| 	lockdep_assert_held(lock);
 | |
| }
 |