From 43fe500f3b619cfdfe110069515994513c30069c Mon Sep 17 00:00:00 2001 From: Thomas Gleixner Date: Sun, 31 Aug 2008 08:09:53 -0700 Subject: [PATCH] --- yaml --- r: 117593 b: refs/heads/master c: df0cc0539b4127bd02f64de2c335b4af1fdb3845 h: refs/heads/master i: 117591: 91e3efb4cf90d2b803cbd35dd676a5c1c159e6b0 v: v3 --- [refs] | 2 +- trunk/include/linux/time.h | 4 ++++ trunk/kernel/time.c | 18 ++++++++++++++++++ 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/[refs] b/[refs] index 81ce37d6bacc..c7951adcebff 100644 --- a/[refs] +++ b/[refs] @@ -1,2 +1,2 @@ --- -refs/heads/master: 7bb67439bf6bd3782f07f1d7be1e63406453d5de +refs/heads/master: df0cc0539b4127bd02f64de2c335b4af1fdb3845 diff --git a/trunk/include/linux/time.h b/trunk/include/linux/time.h index e15206a7e82e..726976478480 100644 --- a/trunk/include/linux/time.h +++ b/trunk/include/linux/time.h @@ -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 diff --git a/trunk/kernel/time.c b/trunk/kernel/time.c index 6a08660b4fac..d63a4336fad6 100644 --- a/trunk/kernel/time.c +++ b/trunk/kernel/time.c @@ -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; +}