public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] prevent timespec/timeval to ktime_t overflow
@ 2006-08-30  8:44 Thomas Gleixner
  2006-08-30 21:44 ` Frank v Waveren
  2006-09-01  3:46 ` Andrew Morton
  0 siblings, 2 replies; 20+ messages in thread
From: Thomas Gleixner @ 2006-08-30  8:44 UTC (permalink / raw)
  To: Andrew Morton; +Cc: Ingo Molnar, LKML, Frank v Waveren

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>

diff --git a/include/linux/ktime.h b/include/linux/ktime.h
index ed3396d..1beafb3 100644
--- a/include/linux/ktime.h
+++ b/include/linux/ktime.h
@@ -57,6 +57,7 @@ typedef union {
 } ktime_t;
 
 #define KTIME_MAX			(~((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 };
 }
 



^ permalink raw reply related	[flat|nested] 20+ messages in thread

end of thread, other threads:[~2006-09-03  3:12 UTC | newest]

Thread overview: 20+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2006-08-30  8:44 [PATCH] prevent timespec/timeval to ktime_t overflow Thomas Gleixner
2006-08-30 21:44 ` Frank v Waveren
2006-08-30 22:05   ` Thomas Gleixner
2006-08-30 22:08     ` Frank v Waveren
2006-08-30 22:22       ` Thomas Gleixner
2006-08-30 22:26         ` Frank v Waveren
2006-09-01  3:46 ` Andrew Morton
2006-09-01  8:56   ` Thomas Gleixner
2006-09-01  9:04     ` Andrew Morton
2006-09-01  9:30       ` Thomas Gleixner
2006-09-02  3:13         ` Andrew Morton
2006-09-02  3:32           ` Andrew Morton
2006-09-02  8:08           ` Andi Kleen
2006-09-02 18:41           ` Thomas Gleixner
2006-09-02 19:28             ` Thomas Gleixner
2006-09-02 19:43               ` Andrew Morton
2006-09-02 19:32             ` Andrew Morton
2006-09-02 11:04   ` Frank v Waveren
2006-09-02 18:44     ` Thomas Gleixner
2006-09-03  3:13       ` Frank v Waveren

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox