aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/time.c
diff options
context:
space:
mode:
authorH. Peter Anvin <hpa@zytor.com>2008-02-08 07:21:26 -0500
committerLinus Torvalds <torvalds@woody.linux-foundation.org>2008-02-08 12:22:39 -0500
commitbdc807871d58285737d50dc6163d0feb72cb0dc2 (patch)
tree1a6d35f3537ed1a7460811549efd045ae97a0e6e /kernel/time.c
parent7ef3d2fd17c377ef64a2aa19677d17576606c3b4 (diff)
avoid overflows in kernel/time.c
When the conversion factor between jiffies and milli- or microseconds is not a single multiply or divide, as for the case of HZ == 300, we currently do a multiply followed by a divide. The intervening result, however, is subject to overflows, especially since the fraction is not simplified (for HZ == 300, we multiply by 300 and divide by 1000). This is exposed to the user when passing a large timeout to poll(), for example. This patch replaces the multiply-divide with a reciprocal multiplication on 32-bit platforms. When the input is an unsigned long, there is no portable way to do this on 64-bit platforms there is no portable way to do this since it requires a 128-bit intermediate result (which gcc does support on 64-bit platforms but may generate libgcc calls, e.g. on 64-bit s390), but since the output is a 32-bit integer in the cases affected, just simplify the multiply-divide (*3/10 instead of *300/1000). The reciprocal multiply used can have off-by-one errors in the upper half of the valid output range. This could be avoided at the expense of having to deal with a potential 65-bit intermediate result. Since the intent is to avoid overflow problems and most of the other time conversions are only semiexact, the off-by-one errors were considered an acceptable tradeoff. At Ralf Baechle's suggestion, this version uses a Perl script to compute the necessary constants. We already have dependencies on Perl for kernel compiles. This does, however, require the Perl module Math::BigInt, which is included in the standard Perl distribution starting with version 5.8.0. In order to support older versions of Perl, include a table of canned constants in the script itself, and structure the script so that Math::BigInt isn't required if pulling values from said table. Running the script requires that the HZ value is available from the Makefile. Thus, this patch also adds the Kconfig variable CONFIG_HZ to the architectures which didn't already have it (alpha, cris, frv, h8300, m32r, m68k, m68knommu, sparc, v850, and xtensa.) It does *not* touch the sh or sh64 architectures, since Paul Mundt has dealt with those separately in the sh tree. Signed-off-by: H. Peter Anvin <hpa@zytor.com> Cc: Ralf Baechle <ralf@linux-mips.org>, Cc: Sam Ravnborg <sam@ravnborg.org>, Cc: Paul Mundt <lethal@linux-sh.org>, Cc: Richard Henderson <rth@twiddle.net>, Cc: Michael Starvik <starvik@axis.com>, Cc: David Howells <dhowells@redhat.com>, Cc: Yoshinori Sato <ysato@users.sourceforge.jp>, Cc: Hirokazu Takata <takata@linux-m32r.org>, Cc: Geert Uytterhoeven <geert@linux-m68k.org>, Cc: Roman Zippel <zippel@linux-m68k.org>, Cc: William L. Irwin <sparclinux@vger.kernel.org>, Cc: Chris Zankel <chris@zankel.net>, Cc: H. Peter Anvin <hpa@zytor.com>, Cc: Jan Engelhardt <jengelh@computergmbh.de> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'kernel/time.c')
-rw-r--r--kernel/time.c29
1 files changed, 21 insertions, 8 deletions
diff --git a/kernel/time.c b/kernel/time.c
index 3b705ecc3fb7..a5ec013b6c80 100644
--- a/kernel/time.c
+++ b/kernel/time.c
@@ -39,6 +39,8 @@
39#include <asm/uaccess.h> 39#include <asm/uaccess.h>
40#include <asm/unistd.h> 40#include <asm/unistd.h>
41 41
42#include "timeconst.h"
43
42/* 44/*
43 * The timezone where the local system is located. Used as a default by some 45 * The timezone where the local system is located. Used as a default by some
44 * programs who obtain this value by using gettimeofday. 46 * programs who obtain this value by using gettimeofday.
@@ -93,7 +95,8 @@ asmlinkage long sys_stime(time_t __user *tptr)
93 95
94#endif /* __ARCH_WANT_SYS_TIME */ 96#endif /* __ARCH_WANT_SYS_TIME */
95 97
96asmlinkage long sys_gettimeofday(struct timeval __user *tv, struct timezone __user *tz) 98asmlinkage long sys_gettimeofday(struct timeval __user *tv,
99 struct timezone __user *tz)
97{ 100{
98 if (likely(tv != NULL)) { 101 if (likely(tv != NULL)) {
99 struct timeval ktv; 102 struct timeval ktv;
@@ -118,7 +121,7 @@ asmlinkage long sys_gettimeofday(struct timeval __user *tv, struct timezone __us
118 * hard to make the program warp the clock precisely n hours) or 121 * hard to make the program warp the clock precisely n hours) or
119 * compile in the timezone information into the kernel. Bad, bad.... 122 * compile in the timezone information into the kernel. Bad, bad....
120 * 123 *
121 * - TYT, 1992-01-01 124 * - TYT, 1992-01-01
122 * 125 *
123 * The best thing to do is to keep the CMOS clock in universal time (UTC) 126 * The best thing to do is to keep the CMOS clock in universal time (UTC)
124 * as real UNIX machines always do it. This avoids all headaches about 127 * as real UNIX machines always do it. This avoids all headaches about
@@ -240,7 +243,11 @@ unsigned int inline jiffies_to_msecs(const unsigned long j)
240#elif HZ > MSEC_PER_SEC && !(HZ % MSEC_PER_SEC) 243#elif HZ > MSEC_PER_SEC && !(HZ % MSEC_PER_SEC)
241 return (j + (HZ / MSEC_PER_SEC) - 1)/(HZ / MSEC_PER_SEC); 244 return (j + (HZ / MSEC_PER_SEC) - 1)/(HZ / MSEC_PER_SEC);
242#else 245#else
243 return (j * MSEC_PER_SEC) / HZ; 246# if BITS_PER_LONG == 32
247 return ((u64)HZ_TO_MSEC_MUL32 * j) >> HZ_TO_MSEC_SHR32;
248# else
249 return (j * HZ_TO_MSEC_NUM) / HZ_TO_MSEC_DEN;
250# endif
244#endif 251#endif
245} 252}
246EXPORT_SYMBOL(jiffies_to_msecs); 253EXPORT_SYMBOL(jiffies_to_msecs);
@@ -252,7 +259,11 @@ unsigned int inline jiffies_to_usecs(const unsigned long j)
252#elif HZ > USEC_PER_SEC && !(HZ % USEC_PER_SEC) 259#elif HZ > USEC_PER_SEC && !(HZ % USEC_PER_SEC)
253 return (j + (HZ / USEC_PER_SEC) - 1)/(HZ / USEC_PER_SEC); 260 return (j + (HZ / USEC_PER_SEC) - 1)/(HZ / USEC_PER_SEC);
254#else 261#else
255 return (j * USEC_PER_SEC) / HZ; 262# if BITS_PER_LONG == 32
263 return ((u64)HZ_TO_USEC_MUL32 * j) >> HZ_TO_USEC_SHR32;
264# else
265 return (j * HZ_TO_USEC_NUM) / HZ_TO_USEC_DEN;
266# endif
256#endif 267#endif
257} 268}
258EXPORT_SYMBOL(jiffies_to_usecs); 269EXPORT_SYMBOL(jiffies_to_usecs);
@@ -352,7 +363,7 @@ EXPORT_SYMBOL(mktime);
352 * normalize to the timespec storage format 363 * normalize to the timespec storage format
353 * 364 *
354 * Note: The tv_nsec part is always in the range of 365 * Note: The tv_nsec part is always in the range of
355 * 0 <= tv_nsec < NSEC_PER_SEC 366 * 0 <= tv_nsec < NSEC_PER_SEC
356 * For negative values only the tv_sec field is negative ! 367 * For negative values only the tv_sec field is negative !
357 */ 368 */
358void set_normalized_timespec(struct timespec *ts, time_t sec, long nsec) 369void set_normalized_timespec(struct timespec *ts, time_t sec, long nsec)
@@ -453,12 +464,13 @@ unsigned long msecs_to_jiffies(const unsigned int m)
453 /* 464 /*
454 * Generic case - multiply, round and divide. But first 465 * Generic case - multiply, round and divide. But first
455 * check that if we are doing a net multiplication, that 466 * check that if we are doing a net multiplication, that
456 * we wouldnt overflow: 467 * we wouldn't overflow:
457 */ 468 */
458 if (HZ > MSEC_PER_SEC && m > jiffies_to_msecs(MAX_JIFFY_OFFSET)) 469 if (HZ > MSEC_PER_SEC && m > jiffies_to_msecs(MAX_JIFFY_OFFSET))
459 return MAX_JIFFY_OFFSET; 470 return MAX_JIFFY_OFFSET;
460 471
461 return (m * HZ + MSEC_PER_SEC - 1) / MSEC_PER_SEC; 472 return ((u64)MSEC_TO_HZ_MUL32 * m + MSEC_TO_HZ_ADJ32)
473 >> MSEC_TO_HZ_SHR32;
462#endif 474#endif
463} 475}
464EXPORT_SYMBOL(msecs_to_jiffies); 476EXPORT_SYMBOL(msecs_to_jiffies);
@@ -472,7 +484,8 @@ unsigned long usecs_to_jiffies(const unsigned int u)
472#elif HZ > USEC_PER_SEC && !(HZ % USEC_PER_SEC) 484#elif HZ > USEC_PER_SEC && !(HZ % USEC_PER_SEC)
473 return u * (HZ / USEC_PER_SEC); 485 return u * (HZ / USEC_PER_SEC);
474#else 486#else
475 return (u * HZ + USEC_PER_SEC - 1) / USEC_PER_SEC; 487 return ((u64)USEC_TO_HZ_MUL32 * u + USEC_TO_HZ_ADJ32)
488 >> USEC_TO_HZ_SHR32;
476#endif 489#endif
477} 490}
478EXPORT_SYMBOL(usecs_to_jiffies); 491EXPORT_SYMBOL(usecs_to_jiffies);