1 /* SPDX-License-Identifier: GPL-2.0 */ << 2 #ifndef __LINUX_BIT_SPINLOCK_H 1 #ifndef __LINUX_BIT_SPINLOCK_H 3 #define __LINUX_BIT_SPINLOCK_H 2 #define __LINUX_BIT_SPINLOCK_H 4 3 5 #include <linux/kernel.h> << 6 #include <linux/preempt.h> << 7 #include <linux/atomic.h> << 8 #include <linux/bug.h> << 9 << 10 /* 4 /* 11 * bit-based spin_lock() 5 * bit-based spin_lock() 12 * 6 * 13 * Don't use this unless you really need to: s 7 * Don't use this unless you really need to: spin_lock() and spin_unlock() 14 * are significantly faster. 8 * are significantly faster. 15 */ 9 */ 16 static inline void bit_spin_lock(int bitnum, u 10 static inline void bit_spin_lock(int bitnum, unsigned long *addr) 17 { 11 { 18 /* 12 /* 19 * Assuming the lock is uncontended, t 13 * Assuming the lock is uncontended, this never enters 20 * the body of the outer loop. If it i 14 * the body of the outer loop. If it is contended, then 21 * within the inner loop a non-atomic 15 * within the inner loop a non-atomic test is used to 22 * busywait with less bus contention f 16 * busywait with less bus contention for a good time to 23 * attempt to acquire the lock bit. 17 * attempt to acquire the lock bit. 24 */ 18 */ 25 preempt_disable(); 19 preempt_disable(); 26 #if defined(CONFIG_SMP) || defined(CONFIG_DEBU 20 #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK) 27 while (unlikely(test_and_set_bit_lock( 21 while (unlikely(test_and_set_bit_lock(bitnum, addr))) { 28 preempt_enable(); !! 22 while (test_bit(bitnum, addr)) { 29 do { !! 23 preempt_enable(); 30 cpu_relax(); 24 cpu_relax(); 31 } while (test_bit(bitnum, addr !! 25 preempt_disable(); 32 preempt_disable(); !! 26 } 33 } 27 } 34 #endif 28 #endif 35 __acquire(bitlock); 29 __acquire(bitlock); 36 } 30 } 37 31 38 /* 32 /* 39 * Return true if it was acquired 33 * Return true if it was acquired 40 */ 34 */ 41 static inline int bit_spin_trylock(int bitnum, 35 static inline int bit_spin_trylock(int bitnum, unsigned long *addr) 42 { 36 { 43 preempt_disable(); 37 preempt_disable(); 44 #if defined(CONFIG_SMP) || defined(CONFIG_DEBU 38 #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK) 45 if (unlikely(test_and_set_bit_lock(bit 39 if (unlikely(test_and_set_bit_lock(bitnum, addr))) { 46 preempt_enable(); 40 preempt_enable(); 47 return 0; 41 return 0; 48 } 42 } 49 #endif 43 #endif 50 __acquire(bitlock); 44 __acquire(bitlock); 51 return 1; 45 return 1; 52 } 46 } 53 47 54 /* 48 /* 55 * bit-based spin_unlock() 49 * bit-based spin_unlock() 56 */ 50 */ 57 static inline void bit_spin_unlock(int bitnum, 51 static inline void bit_spin_unlock(int bitnum, unsigned long *addr) 58 { 52 { 59 #ifdef CONFIG_DEBUG_SPINLOCK 53 #ifdef CONFIG_DEBUG_SPINLOCK 60 BUG_ON(!test_bit(bitnum, addr)); 54 BUG_ON(!test_bit(bitnum, addr)); 61 #endif 55 #endif 62 #if defined(CONFIG_SMP) || defined(CONFIG_DEBU 56 #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK) 63 clear_bit_unlock(bitnum, addr); 57 clear_bit_unlock(bitnum, addr); 64 #endif 58 #endif 65 preempt_enable(); 59 preempt_enable(); 66 __release(bitlock); 60 __release(bitlock); 67 } 61 } 68 62 69 /* 63 /* 70 * bit-based spin_unlock() 64 * bit-based spin_unlock() 71 * non-atomic version, which can be used eg. 65 * non-atomic version, which can be used eg. if the bit lock itself is 72 * protecting the rest of the flags in the wo 66 * protecting the rest of the flags in the word. 73 */ 67 */ 74 static inline void __bit_spin_unlock(int bitnu 68 static inline void __bit_spin_unlock(int bitnum, unsigned long *addr) 75 { 69 { 76 #ifdef CONFIG_DEBUG_SPINLOCK 70 #ifdef CONFIG_DEBUG_SPINLOCK 77 BUG_ON(!test_bit(bitnum, addr)); 71 BUG_ON(!test_bit(bitnum, addr)); 78 #endif 72 #endif 79 #if defined(CONFIG_SMP) || defined(CONFIG_DEBU 73 #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK) 80 __clear_bit_unlock(bitnum, addr); 74 __clear_bit_unlock(bitnum, addr); 81 #endif 75 #endif 82 preempt_enable(); 76 preempt_enable(); 83 __release(bitlock); 77 __release(bitlock); 84 } 78 } 85 79 86 /* 80 /* 87 * Return true if the lock is held. 81 * Return true if the lock is held. 88 */ 82 */ 89 static inline int bit_spin_is_locked(int bitnu 83 static inline int bit_spin_is_locked(int bitnum, unsigned long *addr) 90 { 84 { 91 #if defined(CONFIG_SMP) || defined(CONFIG_DEBU 85 #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK) 92 return test_bit(bitnum, addr); 86 return test_bit(bitnum, addr); 93 #elif defined CONFIG_PREEMPT_COUNT !! 87 #elif defined CONFIG_PREEMPT 94 return preempt_count(); 88 return preempt_count(); 95 #else 89 #else 96 return 1; 90 return 1; 97 #endif 91 #endif 98 } 92 } 99 93 100 #endif /* __LINUX_BIT_SPINLOCK_H */ 94 #endif /* __LINUX_BIT_SPINLOCK_H */ 101 95 102 96
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.