Skip to content

Commit bdc8078

Browse files
H. Peter AnvinLinus Torvalds
authored andcommitted
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>
1 parent 7ef3d2f commit bdc8078

29 files changed

Lines changed: 486 additions & 68 deletions

File tree

‎arch/alpha/Kconfig‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,11 @@ config VERBOSE_MCHECK_ON
615615

616616
Take the default (1) unless you want more control or more info.
617617

618+
config HZ
619+
int
620+
default 1200 if ALPHA_RAWHIDE
621+
default 1024
622+
618623
source "drivers/pci/Kconfig"
619624
source "drivers/eisa/Kconfig"
620625

‎arch/cris/Kconfig‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ config CRIS
5555
bool
5656
default y
5757

58+
config HZ
59+
int
60+
default 100
61+
5862
source "init/Kconfig"
5963

6064
menu "General setup"

‎arch/frv/Kconfig‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ config ARCH_HAS_ILOG2_U64
5757
bool
5858
default y
5959

60+
config HZ
61+
int
62+
default 1000
63+
6064
mainmenu "Fujitsu FR-V Kernel Configuration"
6165

6266
source "init/Kconfig"

‎arch/h8300/Kconfig‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ config PCI
8282
bool
8383
default n
8484

85+
config HZ
86+
int
87+
default 100
88+
8589
source "init/Kconfig"
8690

8791
source "arch/h8300/Kconfig.cpu"

‎arch/m32r/Kconfig‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ config NO_DMA
3838
config ARCH_SUPPORTS_AOUT
3939
def_bool y
4040

41+
config HZ
42+
int
43+
default 100
44+
4145
source "init/Kconfig"
4246

4347

‎arch/m68k/Kconfig‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ config NO_DMA
5555
config ARCH_SUPPORTS_AOUT
5656
def_bool y
5757

58+
config HZ
59+
int
60+
default 100
61+
5862
mainmenu "Linux/68k Kernel Configuration"
5963

6064
source "init/Kconfig"

‎arch/m68knommu/Kconfig‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,11 @@ config 4KSTACKS
525525
running more threads on a system and also reduces the pressure
526526
on the VM subsystem for higher order allocations.
527527

528+
config HZ
529+
int
530+
default 1000 if CLEOPATRA
531+
default 100
532+
528533
comment "RAM configuration"
529534

530535
config RAMBASE

‎arch/sparc/Kconfig‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ config OF
3030
config ARCH_SUPPORTS_AOUT
3131
def_bool y
3232

33+
config HZ
34+
int
35+
default 100
36+
3337
source "init/Kconfig"
3438

3539
menu "General machine setup"

‎arch/v850/Kconfig‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,13 @@ menu "Processor type and features"
215215
bool
216216
default !V850E_CACHE && !V850E2_CACHE
217217

218+
# HZ depends on the platform
219+
config HZ
220+
int
221+
default 24 if V850E_SIM || V850E2_SIM85E2
222+
default 122 if V850E2_FPGA85E2C
223+
default 100
224+
218225
#### Misc config
219226

220227
config ROM_KERNEL

‎arch/xtensa/Kconfig‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ config ARCH_HAS_ILOG2_U64
4949
config NO_IOPORT
5050
def_bool y
5151

52+
config HZ
53+
int
54+
default 100
55+
5256
source "init/Kconfig"
5357

5458
menu "Processor type and features"

0 commit comments

Comments
 (0)