]> pilppa.org Git - linux-2.6-omap-h63xx.git/commitdiff
[PATCH] prevent timespec/timeval to ktime_t overflow
authorThomas Gleixner <tglx@linutronix.de>
Wed, 6 Sep 2006 07:03:42 +0000 (00:03 -0700)
committerLinus Torvalds <torvalds@g5.osdl.org>
Wed, 6 Sep 2006 18:00:02 +0000 (11:00 -0700)
Frank v.  Waveren pointed out that on 64bit machines the timespec to
ktime_t conversion might overflow.  This is also true for timeval to
ktime_t conversions.  This breaks a "sleep inf" on 64bit machines.

While a timespec/timeval with tx.sec = MAX_LONG is valid by specification
the internal representation of ktime_t is based on nanoseconds.  The
conversion of seconds to nanoseconds overflows for seconds values >=
(MAX_LONG / NSEC_PER_SEC).

Check the seconds argument to the conversion and limit it to the maximum
time which can be represented by ktime_t.

Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Cc: Ingo Molnar <mingo@elte.hu>
Cc: Frank v Waveren <fvw@var.cx>
Signed-off-by: Andrew Morton <akpm@osdl.org>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
include/linux/ktime.h

index ed3396dcc4f753d0d7cdfe8780c679f7315bc65d..84eeecd60a02644a6cc64563747a594b8bafbb54 100644 (file)
@@ -56,7 +56,8 @@ typedef union {
 #endif
 } ktime_t;
 
-#define KTIME_MAX                      (~((u64)1 << 63))
+#define KTIME_MAX                      ((s64)~((u64)1 << 63))
+#define KTIME_SEC_MAX                  (KTIME_MAX / NSEC_PER_SEC)
 
 /*
  * ktime_t definitions when using the 64-bit scalar representation:
@@ -73,6 +74,10 @@ typedef union {
  */
 static inline ktime_t ktime_set(const long secs, const unsigned long nsecs)
 {
+#if (BITS_PER_LONG == 64)
+       if (unlikely(secs >= KTIME_SEC_MAX))
+               return (ktime_t){ .tv64 = KTIME_MAX };
+#endif
        return (ktime_t) { .tv64 = (s64)secs * NSEC_PER_SEC + (s64)nsecs };
 }