Skip to contentSkip to conversation

lib/spinlock_debug: Tweak the loop time to fit different _delay()

PATCH In review Wang, Xiaoming 3 participants 5 messages Apr 30, 2014 Locking primitives

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 lib/spinlock_debug: Tweak the loop time to fit different _delay()

loops_per_jiffy*Hz is not always 1 second exactly it depends on the realization of _delay() . delay_tsc is used as _delay() in arch/x86/lib/delay.c It makes loop loops_per_jiffy larger than exception and causes one thread can not obtain the spin lock for a long time which may trigger HARD LOCKUP in this case. So we use cpu_clock() which is more accurate.

---
 kernel/locking/spinlock_debug.c |    9 ++++++---
 1 files changed, 6 insertions(+), 3 deletions(-)
kernel/locking/spinlock_debug.c +6 -3
index 0374a59..471d26c 100644
@@ -105,10 +105,13 @@ static inline void debug_spin_unlock(raw_spinlock_t *lock)
 
 static void __spin_lock_debug(raw_spinlock_t *lock)
 {
-	u64 i;
-	u64 loops = loops_per_jiffy * HZ;
+	u64 t;
+	u64 one_second = 1000000000;
+	u32 this_cpu = raw_smp_processor_id();
+
+	t = cpu_clock(this_cpu);
 
-	for (i = 0; i < loops; i++) {
+	while (cpu_clock(this_cpu) - t < one_second) {
 		if (arch_spin_trylock(&lock->raw_lock))
 			return;
 		__delay(1);
Signature · 1 line
1.7.1
maintainer
#1 lore raw
Quoted 25 lines · Xiaoming

loops_per_jiffy*Hz is not always 1 second exactly it depends on the realization of _delay() . delay_tsc is used as _delay() in arch/x86/lib/delay.c It makes loop loops_per_jiffy larger than exception and causes one thread can not obtain the spin lock for a long time which may trigger HARD LOCKUP in this case. So we use cpu_clock() which is more accurate.

---
 kernel/locking/spinlock_debug.c |    9 ++++++---
 1 files changed, 6 insertions(+), 3 deletions(-)
kernel/locking/spinlock_debug.c +1 -2
index 0374a59..471d26c 100644
@@ -105,10 +105,13 @@ static inline void debug_spin_unlock(raw_spinlock_t *lock)
 
 static void __spin_lock_debug(raw_spinlock_t *lock)
 {
-	u64 i;
-	u64 loops = loops_per_jiffy * HZ;
+	u64 t;

kernel/locking/spinlock_debug.c static inline void debug_spin_unlock(raw_spinlock_t *lock) · line 117

Quoted 10 lines
kernel/locking/spinlock_debug.c +5 -1
+	u64 one_second = 1000000000;
+	u32 this_cpu = raw_smp_processor_id();
+
+	t = cpu_clock(this_cpu);
 
-	for (i = 0; i < loops; i++) {
+	while (cpu_clock(this_cpu) - t < one_second) {
 		if (arch_spin_trylock(&lock->raw_lock))
 			return;
 		__delay(1);

Yep, and now you've broken support for archs that fall back to jiffies for cpu_clock :-), jiffies need not progress if you've got IRQs disabled.

maintainer
#2 lore raw
In reply to Wang, Xiaoming
Quoted 3 lines · Xiaoming

loops_per_jiffy*Hz is not always 1 second exactly it depends on the realization of _delay() . delay_tsc is used as _delay() in arch/x86/lib/delay.c

This just states delay() is broken. The primary response should be to try and fix that, no?

#3 lore raw

Hello Peter,

Quoted 8 lines

-----Original Message-----
From: Peter Zijlstra [mailto:[email protected]]
Sent: Wednesday, April 30, 2014 4:55 PM
To: Wang, Xiaoming
Cc: [email protected]; [email protected]; Liu, Chuansheng
Subject: Re: [PATCH] lib/spinlock_debug: Tweak the loop time to fit different _delay()

Quoted 7 lines
Quoted 3 lines · Xiaoming

loops_per_jiffy*Hz is not always 1 second exactly it depends on the realization of _delay() . delay_tsc is used as _delay() in arch/x86/lib/delay.c

This just states delay() is broken. The primary response should be to try and fix that, no?

delay(1s_count) is accurate, but delay(1) is not accurate indeed, since executing some instruction, then the 1 cycle delay maybe be used already.

maintainer
#4 lore raw
Quoted 12 lines · Chuansheng
Quoted 12 lines

From: Peter Zijlstra [mailto:[email protected]]
Sent: Wednesday, April 30, 2014 4:55 PM
To: Wang, Xiaoming
Cc: [email protected]; [email protected]; Liu, Chuansheng
Subject: Re: [PATCH] lib/spinlock_debug: Tweak the loop time to fit different _delay()

Quoted 3 lines · Xiaoming

loops_per_jiffy*Hz is not always 1 second exactly it depends on the realization of _delay() . delay_tsc is used as _delay() in arch/x86/lib/delay.c

Quoted 6 lines
Quoted 2 lines

This just states delay() is broken. The primary response should be to try and fix that, no?

delay(1s_count) is accurate, but delay(1) is not accurate indeed, since executing some instruction, then the 1 cycle delay maybe be used already.

OK, so there's (finally) a problem statement, so is there anything sane we can do about that?

But yes, a trylock is a cmpxchg, and a cmpxchg on a contended cacheline can be _much_ longer than one loop.

Now the real problem is coming up with something that'll work for all architectures.