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

File tree

29 files changed

+486
-68
lines changed

29 files changed

+486
-68
lines changed

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"

include/asm-alpha/param.h

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,7 @@
55
hardware ignores reprogramming. We also need userland buy-in to the
66
change in HZ, since this is visible in the wait4 resources etc. */
77

8-
9-
#ifndef HZ
10-
# ifndef CONFIG_ALPHA_RAWHIDE
11-
# define HZ 1024
12-
# else
13-
# define HZ 1200
14-
# endif
15-
#endif
16-
8+
#define HZ CONFIG_HZ
179
#define USER_HZ HZ
1810

1911
#define EXEC_PAGESIZE 8192

include/asm-cris/param.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
/* Currently we assume that HZ=100 is good for CRIS. */
55
#ifdef __KERNEL__
6-
# define HZ 100 /* Internal kernel timer frequency */
6+
# define HZ CONFIG_HZ /* Internal kernel timer frequency */
77
# define USER_HZ 100 /* .. some user interfaces are in "ticks" */
88
# define CLOCKS_PER_SEC (USER_HZ) /* like times() */
99
#endif

include/asm-frv/param.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#define _ASM_PARAM_H
33

44
#ifdef __KERNEL__
5-
#define HZ 1000 /* Internal kernel timer frequency */
5+
#define HZ CONFIG_HZ /* Internal kernel timer frequency */
66
#define USER_HZ 100 /* .. some user interfaces are in "ticks" */
77
#define CLOCKS_PER_SEC (USER_HZ) /* like times() */
88
#endif

include/asm-h8300/param.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44

55
#ifndef HZ
6-
#define HZ 100
6+
#define HZ CONFIG_HZ
77
#endif
88

99
#ifdef __KERNEL__

include/asm-m32r/param.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#define _ASM_M32R_PARAM_H
33

44
#ifdef __KERNEL__
5-
# define HZ 100 /* Internal kernel timer frequency */
5+
# define HZ CONFIG_HZ /* Internal kernel timer frequency */
66
# define USER_HZ 100 /* .. some user interfaces are in "ticks" */
77
# define CLOCKS_PER_SEC (USER_HZ) /* like times() */
88
#endif

include/asm-m68k/param.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#define _M68K_PARAM_H
33

44
#ifdef __KERNEL__
5-
# define HZ 100 /* Internal kernel timer frequency */
5+
# define HZ CONFIG_HZ /* Internal kernel timer frequency */
66
# define USER_HZ 100 /* .. some user interfaces are in "ticks" */
77
# define CLOCKS_PER_SEC (USER_HZ) /* like times() */
88
#endif

include/asm-m68knommu/param.h

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
11
#ifndef _M68KNOMMU_PARAM_H
22
#define _M68KNOMMU_PARAM_H
33

4-
5-
#if defined(CONFIG_CLEOPATRA)
6-
#define HZ 1000
7-
#endif
8-
#ifndef HZ
9-
#define HZ 100
10-
#endif
4+
#define HZ CONFIG_HZ
115

126
#ifdef __KERNEL__
137
#define USER_HZ HZ

include/asm-sparc/param.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#define _ASMSPARC_PARAM_H
44

55
#ifdef __KERNEL__
6-
# define HZ 100 /* Internal kernel timer frequency */
6+
# define HZ CONFIG_HZ /* Internal kernel timer frequency */
77
# define USER_HZ 100 /* .. some user interfaces are in "ticks" */
88
# define CLOCKS_PER_SEC (USER_HZ)
99
#endif

include/asm-v850/anna.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,4 @@ extern void anna_uart_pre_configure (unsigned chan,
134134
#define V850E_TIMER_D_TMCD_CS_MIN 1 /* min 2^1 divider */
135135

136136

137-
/* For <asm/param.h> */
138-
#ifndef HZ
139-
#define HZ 100
140-
#endif
141-
142-
143137
#endif /* __V850_ANNA_H__ */

include/asm-v850/as85ep1.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,4 @@ extern void as85ep1_uart_pre_configure (unsigned chan,
149149
#define V850E_TIMER_D_TMCD_CS_MIN 2 /* min 2^2 divider */
150150

151151

152-
/* For <asm/param.h> */
153-
#ifndef HZ
154-
#define HZ 100
155-
#endif
156-
157-
158152
#endif /* __V850_AS85EP1_H__ */

include/asm-v850/fpga85e2c.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,4 @@ extern char _r0_ram;
7979
#endif
8080

8181

82-
/* For <asm/param.h> */
83-
#ifndef HZ
84-
#define HZ 122 /* actually, 8.192ms ticks =~ 122.07 */
85-
#endif
86-
87-
8882
#endif /* __V850_FPGA85E2C_H__ */

include/asm-v850/param.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@
2323
#define MAXHOSTNAMELEN 64 /* max length of hostname */
2424

2525
#ifdef __KERNEL__
26-
#include <asm/machdep.h> /* For HZ */
27-
26+
# define HZ CONFIG_HZ
2827
# define USER_HZ 100
2928
# define CLOCKS_PER_SEC USER_HZ
3029
#endif

include/asm-v850/rte_cb.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,6 @@
6969
#endif /* CONFIG_RTE_MB_A_PCI */
7070

7171

72-
/* For <asm/param.h> */
73-
#ifndef HZ
74-
#define HZ 100
75-
#endif
76-
77-
7872
#ifndef __ASSEMBLY__
7973
extern void rte_cb_early_init (void);
8074
extern void rte_cb_init_irqs (void);

include/asm-v850/sim.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,6 @@
4040
#define R0_RAM_ADDR 0xFFFFF000
4141

4242

43-
/* For <asm/param.h> */
44-
#ifndef HZ
45-
#define HZ 24 /* Minimum supported frequency. */
46-
#endif
47-
4843
/* For <asm/irq.h> */
4944
#define NUM_CPU_IRQS 6
5045

include/asm-v850/sim85e2.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,4 @@
6666
#define R0_RAM_ADDR 0xFFFFE000
6767

6868

69-
/* For <asm/param.h> */
70-
#ifndef HZ
71-
#define HZ 24 /* Minimum supported frequency. */
72-
#endif
73-
74-
7569
#endif /* __V850_SIM85E2_H__ */

include/asm-xtensa/param.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#define _XTENSA_PARAM_H
1313

1414
#ifdef __KERNEL__
15-
# define HZ 100 /* internal timer frequency */
15+
# define HZ CONFIG_HZ /* internal timer frequency */
1616
# define USER_HZ 100 /* for user interfaces in "ticks" */
1717
# define CLOCKS_PER_SEC (USER_HZ) /* frequnzy at which times() counts */
1818
#endif

kernel/Makefile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,11 @@ quiet_cmd_ikconfiggz = IKCFG $@
9191
targets += config_data.h
9292
$(obj)/config_data.h: $(obj)/config_data.gz FORCE
9393
$(call if_changed,ikconfiggz)
94+
95+
$(obj)/time.o: $(obj)/timeconst.h
96+
97+
quiet_cmd_timeconst = TIMEC $@
98+
cmd_timeconst = $(PERL) $< $(CONFIG_HZ) > $@
99+
targets += timeconst.h
100+
$(obj)/timeconst.h: $(src)/timeconst.pl FORCE
101+
$(call if_changed,timeconst)

kernel/time.c

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
#include <asm/uaccess.h>
4040
#include <asm/unistd.h>
4141

42+
#include "timeconst.h"
43+
4244
/*
4345
* The timezone where the local system is located. Used as a default by some
4446
* programs who obtain this value by using gettimeofday.
@@ -93,7 +95,8 @@ asmlinkage long sys_stime(time_t __user *tptr)
9395

9496
#endif /* __ARCH_WANT_SYS_TIME */
9597

96-
asmlinkage long sys_gettimeofday(struct timeval __user *tv, struct timezone __user *tz)
98+
asmlinkage long sys_gettimeofday(struct timeval __user *tv,
99+
struct timezone __user *tz)
97100
{
98101
if (likely(tv != NULL)) {
99102
struct timeval ktv;
@@ -118,7 +121,7 @@ asmlinkage long sys_gettimeofday(struct timeval __user *tv, struct timezone __us
118121
* hard to make the program warp the clock precisely n hours) or
119122
* compile in the timezone information into the kernel. Bad, bad....
120123
*
121-
* - TYT, 1992-01-01
124+
* - TYT, 1992-01-01
122125
*
123126
* The best thing to do is to keep the CMOS clock in universal time (UTC)
124127
* 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)
240243
#elif HZ > MSEC_PER_SEC && !(HZ % MSEC_PER_SEC)
241244
return (j + (HZ / MSEC_PER_SEC) - 1)/(HZ / MSEC_PER_SEC);
242245
#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
244251
#endif
245252
}
246253
EXPORT_SYMBOL(jiffies_to_msecs);
@@ -252,7 +259,11 @@ unsigned int inline jiffies_to_usecs(const unsigned long j)
252259
#elif HZ > USEC_PER_SEC && !(HZ % USEC_PER_SEC)
253260
return (j + (HZ / USEC_PER_SEC) - 1)/(HZ / USEC_PER_SEC);
254261
#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
256267
#endif
257268
}
258269
EXPORT_SYMBOL(jiffies_to_usecs);
@@ -352,7 +363,7 @@ EXPORT_SYMBOL(mktime);
352363
* normalize to the timespec storage format
353364
*
354365
* 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
356367
* For negative values only the tv_sec field is negative !
357368
*/
358369
void set_normalized_timespec(struct timespec *ts, time_t sec, long nsec)
@@ -453,12 +464,13 @@ unsigned long msecs_to_jiffies(const unsigned int m)
453464
/*
454465
* Generic case - multiply, round and divide. But first
455466
* check that if we are doing a net multiplication, that
456-
* we wouldnt overflow:
467+
* we wouldn't overflow:
457468
*/
458469
if (HZ > MSEC_PER_SEC && m > jiffies_to_msecs(MAX_JIFFY_OFFSET))
459470
return MAX_JIFFY_OFFSET;
460471

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;
462474
#endif
463475
}
464476
EXPORT_SYMBOL(msecs_to_jiffies);
@@ -472,7 +484,8 @@ unsigned long usecs_to_jiffies(const unsigned int u)
472484
#elif HZ > USEC_PER_SEC && !(HZ % USEC_PER_SEC)
473485
return u * (HZ / USEC_PER_SEC);
474486
#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;
476489
#endif
477490
}
478491
EXPORT_SYMBOL(usecs_to_jiffies);

0 commit comments

Comments
 (0)