Skip to contentSkip to conversation

sched_clock: fix jiffie fallback clock

PATCH Applied Peter Zijlstra 2 participants 3 messages Sep 15, 2008 – Sep 17, 2008 x86

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 sched_clock: fix jiffie fallback clock

David pointed out that the default sched_clock() fallback is broken in that it wraps too soon. Fix this by using the 64 bit jiffie value so that we're large enough to overflow properly.

---
 arch/x86/kernel/tsc.c |    6 ++----
 kernel/sched_clock.c  |    2 +-
 2 files changed, 3 insertions(+), 5 deletions(-)
arch/x86/kernel/tsc.c +2 -4
Index: linux-2.6/arch/x86/kernel/tsc.c
===================================================================
@@ -46,10 +46,8 @@ u64 native_sched_clock(void)
 	 *   very important for it to be as fast as the platform
 	 *   can achive it. )
 	 */
-	if (unlikely(tsc_disabled)) {
-		/* No locking but a rare wrong value is not a big deal: */
-		return (jiffies_64 - INITIAL_JIFFIES) * (1000000000 / HZ);
-	}
+	if (unlikely(tsc_disabled))
+		return (get_jiffies_64() - INITIAL_JIFFIES) * (NSEC_PER_SEC/HZ);
 
 	/* read the Time Stamp Counter: */
 	rdtscll(this_offset);
kernel/sched_clock.c +1 -1
Index: linux-2.6/kernel/sched_clock.c
===================================================================
@@ -38,7 +38,7 @@
  */
 unsigned long long __attribute__((weak)) sched_clock(void)
 {
-	return (unsigned long long)jiffies * (NSEC_PER_SEC / HZ);
+	return (get_jiffies_64() - INITIAL_JIFFIES) * (NSEC_PER_SEC/HZ);
 }
 
 static __read_mostly int sched_clock_running;
Applied
#1 lore raw
Quoted 6 lines · Peter Zijlstra

David pointed out that the default sched_clock() fallback is broken in that it wraps too soon. Fix this by using the 64 bit jiffie value so that we're large enough to overflow properly.

applied to tip/sched/urgent, thanks Peter!

Ingo

#2 lore raw
In reply to Peter Zijlstra
Quoted 6 lines · Peter Zijlstra

David pointed out that the default sched_clock() fallback is broken in that it wraps too soon. Fix this by using the 64 bit jiffie value so that we're large enough to overflow properly.

FYI, -tip testing found bootup hard-lockups today on 32-bit testsystems.

I've bisected it down to this change. After some head-scratching and an hour of debugging, it turns out that this aspect:

arch/x86/kernel/tsc.c u64 native_sched_clock(void) · line 50

Quoted 10 lines
arch/x86/kernel/tsc.c +2 -4
@@ -46,10 +46,8 @@ u64 native_sched_clock(void)
       *   very important for it to be as fast as the platform
       *   can achive it. )
       */
-     if (unlikely(tsc_disabled)) {
-             /* No locking but a rare wrong value is not a big deal: */
-             return (jiffies_64 - INITIAL_JIFFIES) * (1000000000 / HZ);
-     }
+     if (unlikely(tsc_disabled))
+             return (get_jiffies_64() - INITIAL_JIFFIES) * (NSEC_PER_SEC/HZ);

is the buggy one: it calls get_jiffies_64() which does:

                seq = read_seqbegin(&xtime_lock);

that's not a very wise thing to do within xtime-locked sections - and we do call cpu_clock()/sched_clock() in a number of xtime-locked sections.

So i've zapped this commit for the time being, this needs to be solved differently.

Ingo