timekeeping: Try to catch clocksource delta underflows
authorJohn Stultz <[email protected]>
Thu, 12 Mar 2015 04:16:34 +0000 (21:16 -0700)
committerIngo Molnar <[email protected]>
Fri, 13 Mar 2015 07:07:05 +0000 (08:07 +0100)
In the case where there is a broken clocksource
where there are multiple actual clocks that
aren't perfectly aligned, we may see small "negative"
deltas when we subtract 'now' from 'cycle_last'.

The values are actually negative with respect to the
clocksource mask value, not necessarily negative
if cast to a s64, but we can check by checking the
delta to see if it is a small (relative to the mask)
negative value (again negative relative to the mask).

If so, we assume we jumped backwards somehow and
instead use zero for our delta.

Signed-off-by: John Stultz <[email protected]>
Cc: Dave Jones <[email protected]>
Cc: Linus Torvalds <[email protected]>
Cc: Peter Zijlstra <[email protected]>
Cc: Prarit Bhargava <[email protected]>
Cc: Richard Cochran <[email protected]>
Cc: Stephen Boyd <[email protected]>
Cc: Thomas Gleixner <[email protected]>
Link: http://lkml.kernel.org/r/[email protected]
Signed-off-by: Ingo Molnar <[email protected]>
kernel/time/timekeeping.c

index 657414cf2e46625a23ce91822da4ed0a71873baa..187149be83ead70d365d452398e338d15e8b96c2 100644 (file)
@@ -148,6 +148,13 @@ static inline cycle_t timekeeping_get_delta(struct tk_read_base *tkr)
        /* calculate the delta since the last update_wall_time */
        delta = clocksource_delta(cycle_now, tkr->cycle_last, tkr->mask);
 
+       /*
+        * Try to catch underflows by checking if we are seeing small
+        * mask-relative negative values.
+        */
+       if (unlikely((~delta & tkr->mask) < (tkr->mask >> 3)))
+               delta = 0;
+
        /* Cap delta value to the max_cycles values to avoid mult overflows */
        if (unlikely(delta > tkr->clock->max_cycles))
                delta = tkr->clock->max_cycles;