aboutsummaryrefslogtreecommitdiffstats
path: root/include/linux/ktime.h
diff options
context:
space:
mode:
authorThomas Gleixner <tglx@linutronix.de>2006-09-06 03:03:42 -0400
committerLinus Torvalds <torvalds@g5.osdl.org>2006-09-06 14:00:02 -0400
commit96dd7421a06a5bc6eb731323b95efcb2fd864854 (patch)
treea450951e439f897ee92ba6b068efb77c0edc6a1a /include/linux/ktime.h
parentfe2bbc4832659b7ffc867cac03e0a92ae81e11e4 (diff)
[PATCH] prevent timespec/timeval to ktime_t overflow
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>
Diffstat (limited to 'include/linux/ktime.h')
-rw-r--r--include/linux/ktime.h7
1 files changed, 6 insertions, 1 deletions
diff --git a/include/linux/ktime.h b/include/linux/ktime.h
index ed3396dcc4f7..84eeecd60a02 100644
--- a/include/linux/ktime.h
+++ b/include/linux/ktime.h
@@ -56,7 +56,8 @@ typedef union {
56#endif 56#endif
57} ktime_t; 57} ktime_t;
58 58
59#define KTIME_MAX (~((u64)1 << 63)) 59#define KTIME_MAX ((s64)~((u64)1 << 63))
60#define KTIME_SEC_MAX (KTIME_MAX / NSEC_PER_SEC)
60 61
61/* 62/*
62 * ktime_t definitions when using the 64-bit scalar representation: 63 * ktime_t definitions when using the 64-bit scalar representation:
@@ -73,6 +74,10 @@ typedef union {
73 */ 74 */
74static inline ktime_t ktime_set(const long secs, const unsigned long nsecs) 75static inline ktime_t ktime_set(const long secs, const unsigned long nsecs)
75{ 76{
77#if (BITS_PER_LONG == 64)
78 if (unlikely(secs >= KTIME_SEC_MAX))
79 return (ktime_t){ .tv64 = KTIME_MAX };
80#endif
76 return (ktime_t) { .tv64 = (s64)secs * NSEC_PER_SEC + (s64)nsecs }; 81 return (ktime_t) { .tv64 = (s64)secs * NSEC_PER_SEC + (s64)nsecs };
77} 82}
78 83