Skip to content

Commit 9ea4c38

Browse files
Peter Zijlstraingomolnar
authored andcommitted
locking: Optimize lock_bh functions
Currently all _bh_ lock functions do two preempt_count operations: local_bh_disable(); preempt_disable(); and for the unlock: preempt_enable_no_resched(); local_bh_enable(); Since its a waste of perfectly good cycles to modify the same variable twice when you can do it in one go; use the new __local_bh_{dis,en}able_ip() functions that allow us to provide a preempt_count value to add/sub. So define SOFTIRQ_LOCK_OFFSET as the offset a _bh_ lock needs to add/sub to be done in one go. As a bonus it gets rid of the preempt_enable_no_resched() usage. This reduces a 1000 loops of: spin_lock_bh(&bh_lock); spin_unlock_bh(&bh_lock); from 53596 cycles to 51995 cycles. I didn't do enough measurements to say for absolute sure that the result is significant but the the few runs I did for each suggest it is so. Reviewed-by: Thomas Gleixner <tglx@linutronix.de> Signed-off-by: Peter Zijlstra <peterz@infradead.org> Cc: jacob.jun.pan@linux.intel.com Cc: Mike Galbraith <bitbucket@online.de> Cc: hpa@zytor.com Cc: Arjan van de Ven <arjan@linux.intel.com> Cc: lenb@kernel.org Cc: rjw@rjwysocki.net Cc: rui.zhang@intel.com Cc: Linus Torvalds <torvalds@linux-foundation.org> Cc: Andrew Morton <akpm@linux-foundation.org> Link: http://lkml.kernel.org/r/20131119151338.GF3694@twins.programming.kicks-ass.net Signed-off-by: Ingo Molnar <mingo@kernel.org>
1 parent c726099 commit 9ea4c38

5 files changed

Lines changed: 36 additions & 23 deletions

File tree

‎include/linux/preempt_mask.h‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,21 @@
7878
# define PREEMPT_CHECK_OFFSET 0
7979
#endif
8080

81+
/*
82+
* The preempt_count offset needed for things like:
83+
*
84+
* spin_lock_bh()
85+
*
86+
* Which need to disable both preemption (CONFIG_PREEMPT_COUNT) and
87+
* softirqs, such that unlock sequences of:
88+
*
89+
* spin_unlock();
90+
* local_bh_enable();
91+
*
92+
* Work as expected.
93+
*/
94+
#define SOFTIRQ_LOCK_OFFSET (SOFTIRQ_DISABLE_OFFSET + PREEMPT_CHECK_OFFSET)
95+
8196
/*
8297
* Are we running in atomic context? WARNING: this macro cannot
8398
* always detect atomic context; in particular, it cannot know about

‎include/linux/rwlock_api_smp.h‎

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,7 @@ static inline void __raw_read_lock_irq(rwlock_t *lock)
172172

173173
static inline void __raw_read_lock_bh(rwlock_t *lock)
174174
{
175-
local_bh_disable();
176-
preempt_disable();
175+
__local_bh_disable_ip(_RET_IP_, SOFTIRQ_LOCK_OFFSET);
177176
rwlock_acquire_read(&lock->dep_map, 0, 0, _RET_IP_);
178177
LOCK_CONTENDED(lock, do_raw_read_trylock, do_raw_read_lock);
179178
}
@@ -200,8 +199,7 @@ static inline void __raw_write_lock_irq(rwlock_t *lock)
200199

201200
static inline void __raw_write_lock_bh(rwlock_t *lock)
202201
{
203-
local_bh_disable();
204-
preempt_disable();
202+
__local_bh_disable_ip(_RET_IP_, SOFTIRQ_LOCK_OFFSET);
205203
rwlock_acquire(&lock->dep_map, 0, 0, _RET_IP_);
206204
LOCK_CONTENDED(lock, do_raw_write_trylock, do_raw_write_lock);
207205
}
@@ -250,8 +248,7 @@ static inline void __raw_read_unlock_bh(rwlock_t *lock)
250248
{
251249
rwlock_release(&lock->dep_map, 1, _RET_IP_);
252250
do_raw_read_unlock(lock);
253-
preempt_enable_no_resched();
254-
local_bh_enable_ip((unsigned long)__builtin_return_address(0));
251+
__local_bh_enable_ip(_RET_IP_, SOFTIRQ_LOCK_OFFSET);
255252
}
256253

257254
static inline void __raw_write_unlock_irqrestore(rwlock_t *lock,
@@ -275,8 +272,7 @@ static inline void __raw_write_unlock_bh(rwlock_t *lock)
275272
{
276273
rwlock_release(&lock->dep_map, 1, _RET_IP_);
277274
do_raw_write_unlock(lock);
278-
preempt_enable_no_resched();
279-
local_bh_enable_ip((unsigned long)__builtin_return_address(0));
275+
__local_bh_enable_ip(_RET_IP_, SOFTIRQ_LOCK_OFFSET);
280276
}
281277

282278
#endif /* __LINUX_RWLOCK_API_SMP_H */

‎include/linux/spinlock_api_smp.h‎

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,7 @@ static inline void __raw_spin_lock_irq(raw_spinlock_t *lock)
131131

132132
static inline void __raw_spin_lock_bh(raw_spinlock_t *lock)
133133
{
134-
local_bh_disable();
135-
preempt_disable();
134+
__local_bh_disable_ip(_RET_IP_, SOFTIRQ_LOCK_OFFSET);
136135
spin_acquire(&lock->dep_map, 0, 0, _RET_IP_);
137136
LOCK_CONTENDED(lock, do_raw_spin_trylock, do_raw_spin_lock);
138137
}
@@ -174,20 +173,17 @@ static inline void __raw_spin_unlock_bh(raw_spinlock_t *lock)
174173
{
175174
spin_release(&lock->dep_map, 1, _RET_IP_);
176175
do_raw_spin_unlock(lock);
177-
preempt_enable_no_resched();
178-
local_bh_enable_ip((unsigned long)__builtin_return_address(0));
176+
__local_bh_enable_ip(_RET_IP_, SOFTIRQ_LOCK_OFFSET);
179177
}
180178

181179
static inline int __raw_spin_trylock_bh(raw_spinlock_t *lock)
182180
{
183-
local_bh_disable();
184-
preempt_disable();
181+
__local_bh_disable_ip(_RET_IP_, SOFTIRQ_LOCK_OFFSET);
185182
if (do_raw_spin_trylock(lock)) {
186183
spin_acquire(&lock->dep_map, 0, 1, _RET_IP_);
187184
return 1;
188185
}
189-
preempt_enable_no_resched();
190-
local_bh_enable_ip((unsigned long)__builtin_return_address(0));
186+
__local_bh_enable_ip(_RET_IP_, SOFTIRQ_LOCK_OFFSET);
191187
return 0;
192188
}
193189

‎include/linux/spinlock_api_up.h‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,24 +24,30 @@
2424
* flags straight, to suppress compiler warnings of unused lock
2525
* variables, and to add the proper checker annotations:
2626
*/
27+
#define ___LOCK(lock) \
28+
do { __acquire(lock); (void)(lock); } while (0)
29+
2730
#define __LOCK(lock) \
28-
do { preempt_disable(); __acquire(lock); (void)(lock); } while (0)
31+
do { preempt_disable(); ___LOCK(lock); } while (0)
2932

3033
#define __LOCK_BH(lock) \
31-
do { local_bh_disable(); __LOCK(lock); } while (0)
34+
do { __local_bh_disable_ip(_THIS_IP_, SOFTIRQ_LOCK_OFFSET); ___LOCK(lock); } while (0)
3235

3336
#define __LOCK_IRQ(lock) \
3437
do { local_irq_disable(); __LOCK(lock); } while (0)
3538

3639
#define __LOCK_IRQSAVE(lock, flags) \
3740
do { local_irq_save(flags); __LOCK(lock); } while (0)
3841

42+
#define ___UNLOCK(lock) \
43+
do { __release(lock); (void)(lock); } while (0)
44+
3945
#define __UNLOCK(lock) \
40-
do { preempt_enable(); __release(lock); (void)(lock); } while (0)
46+
do { preempt_enable(); ___UNLOCK(lock); } while (0)
4147

4248
#define __UNLOCK_BH(lock) \
43-
do { preempt_enable_no_resched(); local_bh_enable(); \
44-
__release(lock); (void)(lock); } while (0)
49+
do { __local_bh_enable_ip(_THIS_IP_, SOFTIRQ_LOCK_OFFSET); \
50+
___UNLOCK(lock); } while (0)
4551

4652
#define __UNLOCK_IRQ(lock) \
4753
do { local_irq_enable(); __UNLOCK(lock); } while (0)

‎kernel/softirq.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ static void __local_bh_disable(unsigned long ip, unsigned int cnt)
107107
/*
108108
* Were softirqs turned off above:
109109
*/
110-
if (softirq_count() == cnt)
110+
if (softirq_count() == (cnt & SOFTIRQ_MASK))
111111
trace_softirqs_off(ip);
112112
raw_local_irq_restore(flags);
113113

@@ -133,7 +133,7 @@ static void __local_bh_enable(unsigned int cnt)
133133
{
134134
WARN_ON_ONCE(!irqs_disabled());
135135

136-
if (softirq_count() == cnt)
136+
if (softirq_count() == (cnt & SOFTIRQ_MASK))
137137
trace_softirqs_on(_RET_IP_);
138138
preempt_count_sub(cnt);
139139
}

0 commit comments

Comments
 (0)