blob: bbc4730a6505c1af8ab04114d87d2a2289672546 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Ingo Molnarfb1c8f92005-09-10 00:25:56 -07002#ifndef __LINUX_BIT_SPINLOCK_H
3#define __LINUX_BIT_SPINLOCK_H
4
Nick Piggin626d6072011-01-07 17:50:04 +11005#include <linux/kernel.h>
6#include <linux/preempt.h>
Arun Sharma600634972011-07-26 16:09:06 -07007#include <linux/atomic.h>
Paul Gortmaker187f1882011-11-23 20:12:59 -05008#include <linux/bug.h>
Nick Piggin626d6072011-01-07 17:50:04 +11009
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070010/*
11 * bit-based spin_lock()
12 *
13 * Don't use this unless you really need to: spin_lock() and spin_unlock()
14 * are significantly faster.
15 */
16static inline void bit_spin_lock(int bitnum, unsigned long *addr)
17{
18 /*
19 * Assuming the lock is uncontended, this never enters
20 * the body of the outer loop. If it is contended, then
21 * within the inner loop a non-atomic test is used to
22 * busywait with less bus contention for a good time to
23 * attempt to acquire the lock bit.
24 */
25 preempt_disable();
26#if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK)
Nick Pigginb8dc93c2007-10-18 03:06:54 -070027 while (unlikely(test_and_set_bit_lock(bitnum, addr))) {
Linus Torvalds3dd2ee42011-04-25 18:10:58 -070028 preempt_enable();
29 do {
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070030 cpu_relax();
Linus Torvalds3dd2ee42011-04-25 18:10:58 -070031 } while (test_bit(bitnum, addr));
32 preempt_disable();
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070033 }
34#endif
35 __acquire(bitlock);
36}
37
38/*
39 * Return true if it was acquired
40 */
41static inline int bit_spin_trylock(int bitnum, unsigned long *addr)
42{
43 preempt_disable();
44#if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK)
Nick Pigginb8dc93c2007-10-18 03:06:54 -070045 if (unlikely(test_and_set_bit_lock(bitnum, addr))) {
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070046 preempt_enable();
47 return 0;
48 }
49#endif
50 __acquire(bitlock);
51 return 1;
52}
53
54/*
55 * bit-based spin_unlock()
56 */
57static inline void bit_spin_unlock(int bitnum, unsigned long *addr)
58{
Nick Pigginb8dc93c2007-10-18 03:06:54 -070059#ifdef CONFIG_DEBUG_SPINLOCK
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070060 BUG_ON(!test_bit(bitnum, addr));
Nick Pigginb8dc93c2007-10-18 03:06:54 -070061#endif
62#if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK)
63 clear_bit_unlock(bitnum, addr);
64#endif
65 preempt_enable();
66 __release(bitlock);
67}
68
69/*
70 * bit-based spin_unlock()
71 * non-atomic version, which can be used eg. if the bit lock itself is
72 * protecting the rest of the flags in the word.
73 */
74static inline void __bit_spin_unlock(int bitnum, unsigned long *addr)
75{
76#ifdef CONFIG_DEBUG_SPINLOCK
77 BUG_ON(!test_bit(bitnum, addr));
78#endif
79#if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK)
80 __clear_bit_unlock(bitnum, addr);
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070081#endif
82 preempt_enable();
83 __release(bitlock);
84}
85
86/*
87 * Return true if the lock is held.
88 */
89static inline int bit_spin_is_locked(int bitnum, unsigned long *addr)
90{
91#if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK)
92 return test_bit(bitnum, addr);
Frederic Weisbeckerbdd4e852011-06-08 01:13:27 +020093#elif defined CONFIG_PREEMPT_COUNT
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070094 return preempt_count();
95#else
96 return 1;
97#endif
98}
99
100#endif /* __LINUX_BIT_SPINLOCK_H */
101