diff options
author | Linus Torvalds <torvalds@linux-foundation.org> | 2015-05-23 17:57:40 -0700 |
---|---|---|
committer | Linus Torvalds <torvalds@linux-foundation.org> | 2015-05-23 17:57:40 -0700 |
commit | c5db6a3bdeb72f4238e1faefa4ce4eab7a64baea (patch) | |
tree | e0034ec600453c1c1377e6b42f8818d56127617c /include/linux | |
parent | 9bd4459a3d31cfe79dfb5ea00912471537f219c3 (diff) | |
parent | f7bcb70ebae0dcdb5a2d859b09e4465784d99029 (diff) | |
download | linux-stable-c5db6a3bdeb72f4238e1faefa4ce4eab7a64baea.tar.gz linux-stable-c5db6a3bdeb72f4238e1faefa4ce4eab7a64baea.tar.bz2 linux-stable-c5db6a3bdeb72f4238e1faefa4ce4eab7a64baea.zip |
Merge branch 'timers-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip
Pull timer fix from Thomas Gleixner:
"One more fix from the timer departement:
- Handle division of negative nanosecond values proper on 32bit.
A recent cleanup wrecked the sign handling of the dividend and
dropped the check for negative divisors"
* 'timers-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip:
ktime: Fix ktime_divns to do signed division
Diffstat (limited to 'include/linux')
-rw-r--r-- | include/linux/ktime.h | 27 |
1 files changed, 21 insertions, 6 deletions
diff --git a/include/linux/ktime.h b/include/linux/ktime.h index 5fc3d1083071..2b6a204bd8d4 100644 --- a/include/linux/ktime.h +++ b/include/linux/ktime.h @@ -166,19 +166,34 @@ static inline bool ktime_before(const ktime_t cmp1, const ktime_t cmp2) } #if BITS_PER_LONG < 64 -extern u64 __ktime_divns(const ktime_t kt, s64 div); -static inline u64 ktime_divns(const ktime_t kt, s64 div) +extern s64 __ktime_divns(const ktime_t kt, s64 div); +static inline s64 ktime_divns(const ktime_t kt, s64 div) { + /* + * Negative divisors could cause an inf loop, + * so bug out here. + */ + BUG_ON(div < 0); if (__builtin_constant_p(div) && !(div >> 32)) { - u64 ns = kt.tv64; - do_div(ns, div); - return ns; + s64 ns = kt.tv64; + u64 tmp = ns < 0 ? -ns : ns; + + do_div(tmp, div); + return ns < 0 ? -tmp : tmp; } else { return __ktime_divns(kt, div); } } #else /* BITS_PER_LONG < 64 */ -# define ktime_divns(kt, div) (u64)((kt).tv64 / (div)) +static inline s64 ktime_divns(const ktime_t kt, s64 div) +{ + /* + * 32-bit implementation cannot handle negative divisors, + * so catch them on 64bit as well. + */ + WARN_ON(div < 0); + return kt.tv64 / div; +} #endif static inline s64 ktime_to_us(const ktime_t kt) |