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>
This commit is contained in:
H. Peter Anvin
2008-02-08 04:21:26 -08:00
committed by Linus Torvalds
parent 7ef3d2fd17
commit bdc807871d
29 changed files with 486 additions and 68 deletions
+5
View File
@@ -615,6 +615,11 @@ config VERBOSE_MCHECK_ON
Take the default (1) unless you want more control or more info. Take the default (1) unless you want more control or more info.
config HZ
int
default 1200 if ALPHA_RAWHIDE
default 1024
source "drivers/pci/Kconfig" source "drivers/pci/Kconfig"
source "drivers/eisa/Kconfig" source "drivers/eisa/Kconfig"
+4
View File
@@ -55,6 +55,10 @@ config CRIS
bool bool
default y default y
config HZ
int
default 100
source "init/Kconfig" source "init/Kconfig"
menu "General setup" menu "General setup"
+4
View File
@@ -57,6 +57,10 @@ config ARCH_HAS_ILOG2_U64
bool bool
default y default y
config HZ
int
default 1000
mainmenu "Fujitsu FR-V Kernel Configuration" mainmenu "Fujitsu FR-V Kernel Configuration"
source "init/Kconfig" source "init/Kconfig"
+4
View File
@@ -82,6 +82,10 @@ config PCI
bool bool
default n default n
config HZ
int
default 100
source "init/Kconfig" source "init/Kconfig"
source "arch/h8300/Kconfig.cpu" source "arch/h8300/Kconfig.cpu"
+4
View File
@@ -38,6 +38,10 @@ config NO_DMA
config ARCH_SUPPORTS_AOUT config ARCH_SUPPORTS_AOUT
def_bool y def_bool y
config HZ
int
default 100
source "init/Kconfig" source "init/Kconfig"
+4
View File
@@ -55,6 +55,10 @@ config NO_DMA
config ARCH_SUPPORTS_AOUT config ARCH_SUPPORTS_AOUT
def_bool y def_bool y
config HZ
int
default 100
mainmenu "Linux/68k Kernel Configuration" mainmenu "Linux/68k Kernel Configuration"
source "init/Kconfig" source "init/Kconfig"
+5
View File
@@ -525,6 +525,11 @@ config 4KSTACKS
running more threads on a system and also reduces the pressure running more threads on a system and also reduces the pressure
on the VM subsystem for higher order allocations. on the VM subsystem for higher order allocations.
config HZ
int
default 1000 if CLEOPATRA
default 100
comment "RAM configuration" comment "RAM configuration"
config RAMBASE config RAMBASE
+4
View File
@@ -30,6 +30,10 @@ config OF
config ARCH_SUPPORTS_AOUT config ARCH_SUPPORTS_AOUT
def_bool y def_bool y
config HZ
int
default 100
source "init/Kconfig" source "init/Kconfig"
menu "General machine setup" menu "General machine setup"
+7
View File
@@ -215,6 +215,13 @@ menu "Processor type and features"
bool bool
default !V850E_CACHE && !V850E2_CACHE default !V850E_CACHE && !V850E2_CACHE
# HZ depends on the platform
config HZ
int
default 24 if V850E_SIM || V850E2_SIM85E2
default 122 if V850E2_FPGA85E2C
default 100
#### Misc config #### Misc config
config ROM_KERNEL config ROM_KERNEL
+4
View File
@@ -49,6 +49,10 @@ config ARCH_HAS_ILOG2_U64
config NO_IOPORT config NO_IOPORT
def_bool y def_bool y
config HZ
int
default 100
source "init/Kconfig" source "init/Kconfig"
menu "Processor type and features" menu "Processor type and features"
+1 -9
View File
@@ -5,15 +5,7 @@
hardware ignores reprogramming. We also need userland buy-in to the hardware ignores reprogramming. We also need userland buy-in to the
change in HZ, since this is visible in the wait4 resources etc. */ change in HZ, since this is visible in the wait4 resources etc. */
#define HZ CONFIG_HZ
#ifndef HZ
# ifndef CONFIG_ALPHA_RAWHIDE
# define HZ 1024
# else
# define HZ 1200
# endif
#endif
#define USER_HZ HZ #define USER_HZ HZ
#define EXEC_PAGESIZE 8192 #define EXEC_PAGESIZE 8192
+1 -1
View File
@@ -3,7 +3,7 @@
/* Currently we assume that HZ=100 is good for CRIS. */ /* Currently we assume that HZ=100 is good for CRIS. */
#ifdef __KERNEL__ #ifdef __KERNEL__
# define HZ 100 /* Internal kernel timer frequency */ # define HZ CONFIG_HZ /* Internal kernel timer frequency */
# define USER_HZ 100 /* .. some user interfaces are in "ticks" */ # define USER_HZ 100 /* .. some user interfaces are in "ticks" */
# define CLOCKS_PER_SEC (USER_HZ) /* like times() */ # define CLOCKS_PER_SEC (USER_HZ) /* like times() */
#endif #endif
+1 -1
View File
@@ -2,7 +2,7 @@
#define _ASM_PARAM_H #define _ASM_PARAM_H
#ifdef __KERNEL__ #ifdef __KERNEL__
#define HZ 1000 /* Internal kernel timer frequency */ #define HZ CONFIG_HZ /* Internal kernel timer frequency */
#define USER_HZ 100 /* .. some user interfaces are in "ticks" */ #define USER_HZ 100 /* .. some user interfaces are in "ticks" */
#define CLOCKS_PER_SEC (USER_HZ) /* like times() */ #define CLOCKS_PER_SEC (USER_HZ) /* like times() */
#endif #endif
+1 -1
View File
@@ -3,7 +3,7 @@
#ifndef HZ #ifndef HZ
#define HZ 100 #define HZ CONFIG_HZ
#endif #endif
#ifdef __KERNEL__ #ifdef __KERNEL__
+1 -1
View File
@@ -2,7 +2,7 @@
#define _ASM_M32R_PARAM_H #define _ASM_M32R_PARAM_H
#ifdef __KERNEL__ #ifdef __KERNEL__
# define HZ 100 /* Internal kernel timer frequency */ # define HZ CONFIG_HZ /* Internal kernel timer frequency */
# define USER_HZ 100 /* .. some user interfaces are in "ticks" */ # define USER_HZ 100 /* .. some user interfaces are in "ticks" */
# define CLOCKS_PER_SEC (USER_HZ) /* like times() */ # define CLOCKS_PER_SEC (USER_HZ) /* like times() */
#endif #endif
+1 -1
View File
@@ -2,7 +2,7 @@
#define _M68K_PARAM_H #define _M68K_PARAM_H
#ifdef __KERNEL__ #ifdef __KERNEL__
# define HZ 100 /* Internal kernel timer frequency */ # define HZ CONFIG_HZ /* Internal kernel timer frequency */
# define USER_HZ 100 /* .. some user interfaces are in "ticks" */ # define USER_HZ 100 /* .. some user interfaces are in "ticks" */
# define CLOCKS_PER_SEC (USER_HZ) /* like times() */ # define CLOCKS_PER_SEC (USER_HZ) /* like times() */
#endif #endif
+1 -7
View File
@@ -1,13 +1,7 @@
#ifndef _M68KNOMMU_PARAM_H #ifndef _M68KNOMMU_PARAM_H
#define _M68KNOMMU_PARAM_H #define _M68KNOMMU_PARAM_H
#define HZ CONFIG_HZ
#if defined(CONFIG_CLEOPATRA)
#define HZ 1000
#endif
#ifndef HZ
#define HZ 100
#endif
#ifdef __KERNEL__ #ifdef __KERNEL__
#define USER_HZ HZ #define USER_HZ HZ
+1 -1
View File
@@ -3,7 +3,7 @@
#define _ASMSPARC_PARAM_H #define _ASMSPARC_PARAM_H
#ifdef __KERNEL__ #ifdef __KERNEL__
# define HZ 100 /* Internal kernel timer frequency */ # define HZ CONFIG_HZ /* Internal kernel timer frequency */
# define USER_HZ 100 /* .. some user interfaces are in "ticks" */ # define USER_HZ 100 /* .. some user interfaces are in "ticks" */
# define CLOCKS_PER_SEC (USER_HZ) # define CLOCKS_PER_SEC (USER_HZ)
#endif #endif
-6
View File
@@ -134,10 +134,4 @@ extern void anna_uart_pre_configure (unsigned chan,
#define V850E_TIMER_D_TMCD_CS_MIN 1 /* min 2^1 divider */ #define V850E_TIMER_D_TMCD_CS_MIN 1 /* min 2^1 divider */
/* For <asm/param.h> */
#ifndef HZ
#define HZ 100
#endif
#endif /* __V850_ANNA_H__ */ #endif /* __V850_ANNA_H__ */
-6
View File
@@ -149,10 +149,4 @@ extern void as85ep1_uart_pre_configure (unsigned chan,
#define V850E_TIMER_D_TMCD_CS_MIN 2 /* min 2^2 divider */ #define V850E_TIMER_D_TMCD_CS_MIN 2 /* min 2^2 divider */
/* For <asm/param.h> */
#ifndef HZ
#define HZ 100
#endif
#endif /* __V850_AS85EP1_H__ */ #endif /* __V850_AS85EP1_H__ */

Some files were not shown because too many files have changed in this diff Show More