Re: [RFC][PATCH] Dynamic Tick: Allow 32-bit machines to sleep formorethan2.15 seconds

From: Jon Hunter
Date: Fri May 15 2009 - 14:56:01 EST



Jon Hunter wrote:
+ /*
+ * If the result overflows, return the max value we can.
+ */
+ if (overflow)
+ ret = LONG_MAX;
+ else
+ ret = (s64)((upper << 32) + lower);
+
return ret;
}

Correction. Should have been LLONG_MAX and not LONG_MAX in the above. See below.

Jon


diff --git a/include/linux/clocksource.h b/include/linux/clocksource.h
index 5a40d14..647f228 100644
--- a/include/linux/clocksource.h
+++ b/include/linux/clocksource.h
@@ -316,8 +316,32 @@ static inline void clocksource_disable(struct clocksource *cs)
*/
static inline s64 cyc2ns(struct clocksource *cs, cycle_t cycles)
{
- u64 ret = (u64)cycles;
- ret = (ret * cs->mult) >> cs->shift;
+ s64 ret;
+ u64 upper, lower, overflow;
+
+ /*
+ * Split the calculation into two halves to ensure
+ * that we can catch any overflow that may occur.
+ */
+ upper = ((cycles >> 32) * cs->mult) >> cs->shift;
+ lower = ((cycles & 0xFFFFFFFF) * cs->mult) >> cs->shift;
+
+ /*
+ * Check to see if the result will overflow. If
+ * overflow is non-zero then the result is greater
+ * than 63-bits which is the max positive value
+ * for a signed result.
+ */
+ overflow = (upper + (lower >> 32)) >> 31;
+
+ /*
+ * If the result overflows, return the max value we can.
+ */
+ if (overflow)
+ ret = LLONG_MAX;
+ else
+ ret = (s64)((upper << 32) + lower);
+
return ret;
}

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/