]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/commitdiff
select: add a timespec_add_safe() function
authorThomas Gleixner <tglx@linutronix.de>
Sun, 31 Aug 2008 15:09:53 +0000 (08:09 -0700)
committerArjan van de Ven <arjan@linux.intel.com>
Sat, 6 Sep 2008 04:34:57 +0000 (21:34 -0700)
For the select() rework, it's important to be able to add timespec
structures in an overflow-safe manner.

This patch adds a timespec_add_safe() function for this which is similar in
operation to ktime_add_safe(), but works on a struct timespec.

Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: Arjan van de Ven <arjan@linux.intel.com>
include/linux/time.h
kernel/time.c

index e15206a7e82ec9278b25fc062585302f9ed71687..726976478480a644c13a75ebf8f048e44a38ef98 100644 (file)
@@ -38,6 +38,8 @@ struct timezone {
 #define NSEC_PER_SEC   1000000000L
 #define FSEC_PER_SEC   1000000000000000L
 
+#define TIME_T_MAX     (time_t)((1UL << ((sizeof(time_t) << 3) - 1)) - 1)
+
 static inline int timespec_equal(const struct timespec *a,
                                  const struct timespec *b)
 {
@@ -72,6 +74,8 @@ extern unsigned long mktime(const unsigned int year, const unsigned int mon,
                            const unsigned int min, const unsigned int sec);
 
 extern void set_normalized_timespec(struct timespec *ts, time_t sec, long nsec);
+extern struct timespec timespec_add_safe(const struct timespec lhs,
+                                        const struct timespec rhs);
 
 /*
  * sub = lhs - rhs, in normalized form
index 6a08660b4fac30f97ea573b9cd017941401046ff..d63a4336fad69c85f007788a7932959595f0cb73 100644 (file)
@@ -669,3 +669,21 @@ EXPORT_SYMBOL(get_jiffies_64);
 #endif
 
 EXPORT_SYMBOL(jiffies);
+
+/*
+ * Add two timespec values and do a safety check for overflow.
+ * It's assumed that both values are valid (>= 0)
+ */
+struct timespec timespec_add_safe(const struct timespec lhs,
+                                 const struct timespec rhs)
+{
+       struct timespec res;
+
+       set_normalized_timespec(&res, lhs.tv_sec + rhs.tv_sec,
+                               lhs.tv_nsec + rhs.tv_nsec);
+
+       if (res.tv_sec < lhs.tv_sec || res.tv_sec < rhs.tv_sec)
+               res.tv_sec = TIME_T_MAX;
+
+       return res;
+}