Avoid overflows in kernel/time.c (version 4)
Subsystems (5)
PATCH 1/1 Avoid overflows in kernel/time.c (version 4)
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 theMakefile. 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, sh64, sparc, v850, and xtensa.) It does
*not* resolve a weird case in the sh architecture, where HZ !CONFIG_HZ if CONFIG_SH_WDT is set. Paul Mundt has been contacted about that, and has agreed to fix it up.
---
Delta from previous version: indentation fixed in timeconst.pl
arch/alpha/Kconfig | 5 + arch/cris/Kconfig | 4 + arch/frv/Kconfig | 4 + arch/h8300/Kconfig | 4 + arch/m32r/Kconfig | 4 + arch/m68k/Kconfig | 4 + arch/m68knommu/Kconfig | 5 + arch/sh64/Kconfig | 6 + arch/sparc/Kconfig | 4 + arch/v850/Kconfig | 7 + arch/xtensa/Kconfig | 4 + include/asm-alpha/param.h | 10 +- include/asm-cris/param.h | 2 +- include/asm-frv/param.h | 2 +- include/asm-h8300/param.h | 2 +- include/asm-m32r/param.h | 2 +- include/asm-m68k/param.h | 2 +- include/asm-m68knommu/param.h | 8 +- include/asm-sh64/param.h | 6 +- include/asm-sparc/param.h | 2 +- include/asm-v850/anna.h | 6 - include/asm-v850/as85ep1.h | 6 - include/asm-v850/fpga85e2c.h | 6 - include/asm-v850/param.h | 3 +- include/asm-v850/rte_cb.h | 6 - include/asm-v850/sim.h | 5 - include/asm-v850/sim85e2.h | 6 - include/asm-xtensa/param.h | 2 +- kernel/Makefile | 8 + kernel/time.c | 29 +++- kernel/timeconst.pl | 340 +++++++++++++++++++++++++++++++++++++++++ 31 files changed, 431 insertions(+), 73 deletions(-) create mode 100644 kernel/timeconst.pl
arch/alpha/Kconfig +5 -0
@@ -616,6 +616,11 @@ config VERBOSE_MCHECK_ON 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/eisa/Kconfig"
arch/cris/Kconfig +4 -0
@@ -55,6 +55,10 @@ config CRIS bool default y +config HZ + int + default 100 + source "init/Kconfig" menu "General setup"
arch/frv/Kconfig +4 -0
@@ -57,6 +57,10 @@ config ARCH_HAS_ILOG2_U64 bool default y +config HZ + int + default 1000 + mainmenu "Fujitsu FR-V Kernel Configuration" source "init/Kconfig"
arch/h8300/Kconfig +4 -0
@@ -79,6 +79,10 @@ config PCI bool default n +config HZ + int + default 100 + source "init/Kconfig" source "arch/h8300/Kconfig.cpu"
arch/m32r/Kconfig +4 -0
@@ -34,6 +34,10 @@ config NO_IOPORT config NO_DMA def_bool y +config HZ + int + default 100 + source "init/Kconfig"
arch/m68k/Kconfig +4 -0
@@ -52,6 +52,10 @@ config NO_IOPORT config NO_DMA def_bool SUN3 +config HZ + int + default 100 + mainmenu "Linux/68k Kernel Configuration" source "init/Kconfig"
arch/m68knommu/Kconfig +5 -0
@@ -518,6 +518,11 @@ config 4KSTACKS running more threads on a system and also reduces the pressure on the VM subsystem for higher order allocations. +config HZ + int + default 1000 if CLEOPATRA + default 100 + comment "RAM configuration" config RAMBASE
arch/sh64/Kconfig +6 -0
@@ -293,3 +293,9 @@ source "security/Kconfig" source "crypto/Kconfig" source "lib/Kconfig" + +# Higher frequency needed for high-res WOVF +config HZ + int + default 1000 if SH_WDT + default 100
arch/sparc/Kconfig +4 -0
@@ -27,6 +27,10 @@ config ARCH_NO_VIRT_TO_BUS config OF def_bool y +config HZ + int + default 100 + source "init/Kconfig" menu "General machine setup"
arch/v850/Kconfig +7 -0
@@ -212,6 +212,13 @@ menu "Processor type and features" bool 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 config ROM_KERNEL
arch/xtensa/Kconfig +4 -0
@@ -49,6 +49,10 @@ config ARCH_HAS_ILOG2_U64 config NO_IOPORT def_bool y +config HZ + int + default 100 + source "init/Kconfig" menu "Processor type and features"
include/asm-alpha/param.h +1 -9
@@ -5,15 +5,7 @@hardware ignores reprogramming. We also need userland buy-in to the change in HZ, since this is visible in the wait4 resources etc. */ - -#ifndef HZ -# ifndef CONFIG_ALPHA_RAWHIDE -# define HZ 1024 -# else -# define HZ 1200 -# endif -#endif - +#define HZ CONFIG_HZ#define USER_HZ HZ#define EXEC_PAGESIZE 8192
include/asm-cris/param.h +1 -1
@@ -3,7 +3,7 @@/* Currently we assume that HZ0 is good for CRIS. */ #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 CLOCKS_PER_SEC (USER_HZ)/* like times() */ #endif
include/asm-frv/param.h +1 -1
@@ -2,7 +2,7 @@#define _ASM_PARAM_H#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 CLOCKS_PER_SEC (USER_HZ)/* like times() */ #endif
include/asm-h8300/param.h +1 -1
@@ -3,7 +3,7 @@#ifndef HZ -#define HZ 100 +#define HZ CONFIG_HZ#endif #ifdef __KERNEL__
include/asm-m32r/param.h +1 -1
@@ -2,7 +2,7 @@#define _ASM_M32R_PARAM_H#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 CLOCKS_PER_SEC (USER_HZ)/* like times() */ #endif
include/asm-m68k/param.h +1 -1
@@ -2,7 +2,7 @@#define _M68K_PARAM_H#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 CLOCKS_PER_SEC (USER_HZ)/* like times() */ #endif
include/asm-m68knommu/param.h +1 -7
@@ -1,13 +1,7 @@#ifndef _M68KNOMMU_PARAM_H#define _M68KNOMMU_PARAM_H - -#if defined(CONFIG_CLEOPATRA ) -#define HZ 1000 -#endif -#ifndef HZ -#define HZ 100 -#endif +#define HZ CONFIG_HZ#ifdef __KERNEL__#define USER_HZ HZ
include/asm-sh64/param.h +1 -5
@@ -14,11 +14,7 @@#ifdef __KERNEL__ -# ifdef CONFIG_SH_WDT -# define HZ 1000/* Needed for high-res WOVF */ -# else -# define HZ 100 -# endif +# define HZ CONFIG_HZ# define USER_HZ 100/* User interfaces are in "ticks" */ # define CLOCKS_PER_SEC (USER_HZ)/* frequency at which times() counts */ #endif
include/asm-sparc/param.h +1 -1
@@ -3,7 +3,7 @@#define _ASMSPARC_PARAM_H#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 CLOCKS_PER_SEC (USER_HZ)#endif
include/asm-v850/anna.h +0 -6
@@ -134,10 +134,4 @@ extern void anna_uart_pre_configure (unsigned chan,#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__ */
include/asm-v850/as85ep1.h +0 -6
@@ -149,10 +149,4 @@ extern void as85ep1_uart_pre_configure (unsigned chan,#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__ */
include/asm-v850/fpga85e2c.h +0 -6
@@ -79,10 +79,4 @@ extern char _r0_ram;#endif -/* For <asm/param.h> */ -#ifndef HZ -#define HZ 122/* actually, 8.192ms ticks =~ 122.07 */ -#endif - -#endif /* __V850_FPGA85E2C_H__ */
include/asm-v850/param.h +1 -2
@@ -23,8 +23,7 @@#define MAXHOSTNAMELEN 64/* max length of hostname */ #ifdef __KERNEL__ -#include <asm/machdep.h> /* For HZ */ - +# define HZ CONFIG_HZ# define USER_HZ 100# define CLOCKS_PER_SEC USER_HZ#endif
include/asm-v850/rte_cb.h +0 -6
@@ -69,12 +69,6 @@#endif /* CONFIG_RTE_MB_A_PCI */ -/* For <asm/param.h> */ -#ifndef HZ -#define HZ 100 -#endif - -#ifndef __ASSEMBLY__extern void rte_cb_early_init (void );extern void rte_cb_init_irqs (void );
include/asm-v850/sim.h +0 -5
@@ -40,11 +40,6 @@#define R0_RAM_ADDR 0xFFFFF000 -/* For <asm/param.h> */ -#ifndef HZ -#define HZ 24/* Minimum supported frequency. */ -#endif -/* For <asm/irq.h> */ #define NUM_CPU_IRQS 6
include/asm-v850/sim85e2.h +0 -6
@@ -66,10 +66,4 @@#define R0_RAM_ADDR 0xFFFFE000 -/* For <asm/param.h> */ -#ifndef HZ -#define HZ 24/* Minimum supported frequency. */ -#endif - -#endif /* __V850_SIM85E2_H__ */
include/asm-xtensa/param.h +1 -1
@@ -12,7 +12,7 @@#define _XTENSA_PARAM_H#ifdef __KERNEL__ -# define HZ 100/* internal timer frequency */ +# define HZ CONFIG_HZ/* internal timer frequency */ # define USER_HZ 100/* for user interfaces in "ticks" */ # define CLOCKS_PER_SEC (USER_HZ)/* frequnzy at which times() counts */ #endif
kernel/Makefile +8 -0
@@ -80,3 +80,11 @@ quiet_cmd_ikconfiggz = IKCFG $@ targets += config_data.h $(obj)/config_data.h: $(obj)/config_data.gz FORCE $(call if_changed,ikconfiggz) + +$(obj)/time.o: $(obj)/timeconst.h + +quiet_cmd_timeconst = TIMEC $@ + cmd_timeconst = $(PERL) $< $(CONFIG_HZ) > $@ +targets += timeconst.h +$(obj)/timeconst.h: $(src)/timeconst.pl $(wildcard include/config/hz.h) FORCE + $(call if_changed,timeconst)
kernel/time.c +21 -8
@@ -39,6 +39,8 @@#include <asm/uaccess.h> #include <asm/unistd.h> +#include "timeconst.h" +/* * The timezone where the local system is located. Used as a default by some * programs who obtain this value by using gettimeofday.
@@ -93,7 +95,8 @@ asmlinkage long sys_stime(time_t __user *tptr)#endif /* __ARCH_WANT_SYS_TIME */ -asmlinkage long sys_gettimeofday (struct timeval __user *tv,struct timezone __user *tz) +asmlinkage long sys_gettimeofday (struct timeval __user *tv, +struct timezone __user *tz) {if (likely (tv !=NULL )) {struct timeval ktv;
@@ -118,7 +121,7 @@ asmlinkage long sys_gettimeofday(struct timeval __user *tv, struct timezone __us* hard to make the program warp the clock precisely n hours) or * compile in the timezone information into the kernel. Bad, bad.... * -* - TYT, 1992-01-01 +* - TYT, 1992-01-01 * * The best thing to do is to keep the CMOS clock in universal time (UTC) * as real UNIX machines always do it. This avoids all headaches about
@@ -239,7 +242,11 @@ unsigned int inline jiffies_to_msecs(const unsigned long j)#elif HZ >MSEC_PER_SEC && !(HZ %MSEC_PER_SEC )return (j + (HZ /MSEC_PER_SEC ) -1 )/(HZ /MSEC_PER_SEC );#else -return (j *MSEC_PER_SEC ) /HZ ; +# if BITS_PER_LONG = 32 +return ((u64 )HZ_TO_MSEC_MUL32 * j) >>HZ_TO_MSEC_SHR32 ; +# else +return (j *HZ_TO_MSEC_NUM ) /HZ_TO_MSEC_DEN ; +# endif #endif }EXPORT_SYMBOL (jiffies_to_msecs);
@@ -251,7 +258,11 @@ unsigned int inline jiffies_to_usecs(const unsigned long j)#elif HZ >USEC_PER_SEC && !(HZ %USEC_PER_SEC )return (j + (HZ /USEC_PER_SEC ) -1 )/(HZ /USEC_PER_SEC );#else -return (j *USEC_PER_SEC ) /HZ ; +# if BITS_PER_LONG = 32 +return ((u64 )HZ_TO_USEC_MUL32 * j) >>HZ_TO_USEC_SHR32 ; +# else +return (j *HZ_TO_USEC_NUM ) /HZ_TO_USEC_DEN ; +# endif #endif }EXPORT_SYMBOL (jiffies_to_usecs);
@@ -351,7 +362,7 @@ EXPORT_SYMBOL(mktime);* normalize to the timespec storage format * * Note: The tv_nsec part is always in the range of -* 0 <= tv_nsec < NSEC_PER_SEC +* 0 <= tv_nsec < NSEC_PER_SEC * For negative values only the tv_sec field is negative ! */ void set_normalized_timespec (struct timespec *ts,time_t sec,long nsec)
@@ -452,12 +463,13 @@ unsigned long msecs_to_jiffies(const unsigned int m)/* * Generic case - multiply, round and divide. But first * check that if we are doing a net multiplication, that -* we wouldnt overflow: +* we wouldn't overflow: */ if (HZ >MSEC_PER_SEC && m >jiffies_to_msecs (MAX_JIFFY_OFFSET ))return MAX_JIFFY_OFFSET ; -return (m *HZ +MSEC_PER_SEC -1 ) /MSEC_PER_SEC ; +return ((u64 )MSEC_TO_HZ_MUL32 * m +MSEC_TO_HZ_ADJ32 ) + >>MSEC_TO_HZ_SHR32 ;#endif }EXPORT_SYMBOL (msecs_to_jiffies);
@@ -471,7 +483,8 @@ unsigned long usecs_to_jiffies(const unsigned int u)#elif HZ >USEC_PER_SEC && !(HZ %USEC_PER_SEC )return u * (HZ /USEC_PER_SEC );#else -return (u *HZ +USEC_PER_SEC -1 ) /USEC_PER_SEC ; +return ((u64 )USEC_TO_HZ_MUL32 * m +USEC_TO_HZ_ADJ32 ) + >>USEC_TO_HZ_SHR32 ;#endif }EXPORT_SYMBOL (usecs_to_jiffies);
kernel/timeconst.pl new file +340 -0
340 lines not on this page · raw message
Signature · 1 line
1.5.3.4
Quoted 6 lines
kernel/Makefile +0
-0@@ -80,3 +80,11 @@ quiet_cmd_ikconfiggz = IKCFG $@ targets += config_data.h
kernel/Makefile quiet_cmd_ikconfiggz = IKCFG $@ · line 90
Quoted 10 lines
kernel/Makefile +8
-0$(obj)/config_data.h: $(obj)/config_data.gz FORCE $(call if_changed,ikconfiggz) + +$(obj)/time.o: $(obj)/timeconst.h + +quiet_cmd_timeconst = TIMEC $@ + cmd_timeconst = $(PERL) $< $(CONFIG_HZ) > $@ +targets += timeconst.h +$(obj)/timeconst.h: $(src)/timeconst.pl $(wildcard include/config/hz.h) FORCE + $(call if_changed,timeconst)
The prerequisite $(wildcard include/config/hz.h) is not needed. You will run the perl script if:
- timeconst.h is missing
- if commandline changes (new CONFIG_HZ value)
Otherwise it looks OK to me.
Sam
PATCH 1/1 Avoid overflows in kernel/time.c (version 5)
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 theMakefile. 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, sh64, sparc, v850, and xtensa.) It does
*not* resolve a weird case in the sh architecture, where HZ !CONFIG_HZ if CONFIG_SH_WDT is set. Paul Mundt has been contacted about that, and has agreed to fix it up.
---
Delta from previous version: remove redundant dependency in kernel/Makefile.
arch/alpha/Kconfig | 5 + arch/cris/Kconfig | 4 + arch/frv/Kconfig | 4 + arch/h8300/Kconfig | 4 + arch/m32r/Kconfig | 4 + arch/m68k/Kconfig | 4 + arch/m68knommu/Kconfig | 5 + arch/sh64/Kconfig | 6 + arch/sparc/Kconfig | 4 + arch/v850/Kconfig | 7 + arch/xtensa/Kconfig | 4 + include/asm-alpha/param.h | 10 +- include/asm-cris/param.h | 2 +- include/asm-frv/param.h | 2 +- include/asm-h8300/param.h | 2 +- include/asm-m32r/param.h | 2 +- include/asm-m68k/param.h | 2 +- include/asm-m68knommu/param.h | 8 +- include/asm-sh64/param.h | 6 +- include/asm-sparc/param.h | 2 +- include/asm-v850/anna.h | 6 - include/asm-v850/as85ep1.h | 6 - include/asm-v850/fpga85e2c.h | 6 - include/asm-v850/param.h | 3 +- include/asm-v850/rte_cb.h | 6 - include/asm-v850/sim.h | 5 - include/asm-v850/sim85e2.h | 6 - include/asm-xtensa/param.h | 2 +- kernel/Makefile | 8 + kernel/time.c | 29 +++- kernel/timeconst.pl | 340 +++++++++++++++++++++++++++++++++++++++++ 31 files changed, 431 insertions(+), 73 deletions(-) create mode 100644 kernel/timeconst.pl
arch/alpha/Kconfig +5 -0
@@ -616,6 +616,11 @@ config VERBOSE_MCHECK_ON 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/eisa/Kconfig"
arch/cris/Kconfig +4 -0
@@ -55,6 +55,10 @@ config CRIS bool default y +config HZ + int + default 100 + source "init/Kconfig" menu "General setup"
arch/frv/Kconfig +4 -0
@@ -57,6 +57,10 @@ config ARCH_HAS_ILOG2_U64 bool default y +config HZ + int + default 1000 + mainmenu "Fujitsu FR-V Kernel Configuration" source "init/Kconfig"
arch/h8300/Kconfig +4 -0
@@ -79,6 +79,10 @@ config PCI bool default n +config HZ + int + default 100 + source "init/Kconfig" source "arch/h8300/Kconfig.cpu"
arch/m32r/Kconfig +4 -0
@@ -34,6 +34,10 @@ config NO_IOPORT config NO_DMA def_bool y +config HZ + int + default 100 + source "init/Kconfig"
arch/m68k/Kconfig +4 -0
@@ -52,6 +52,10 @@ config NO_IOPORT config NO_DMA def_bool SUN3 +config HZ + int + default 100 + mainmenu "Linux/68k Kernel Configuration" source "init/Kconfig"
arch/m68knommu/Kconfig +5 -0
@@ -518,6 +518,11 @@ config 4KSTACKS running more threads on a system and also reduces the pressure on the VM subsystem for higher order allocations. +config HZ + int + default 1000 if CLEOPATRA + default 100 + comment "RAM configuration" config RAMBASE
arch/sh64/Kconfig +6 -0
@@ -293,3 +293,9 @@ source "security/Kconfig" source "crypto/Kconfig" source "lib/Kconfig" + +# Higher frequency needed for high-res WOVF +config HZ + int + default 1000 if SH_WDT + default 100
arch/sparc/Kconfig +4 -0
@@ -27,6 +27,10 @@ config ARCH_NO_VIRT_TO_BUS config OF def_bool y +config HZ + int + default 100 + source "init/Kconfig" menu "General machine setup"
arch/v850/Kconfig +7 -0
@@ -212,6 +212,13 @@ menu "Processor type and features" bool 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 config ROM_KERNEL
arch/xtensa/Kconfig +4 -0
@@ -49,6 +49,10 @@ config ARCH_HAS_ILOG2_U64 config NO_IOPORT def_bool y +config HZ + int + default 100 + source "init/Kconfig" menu "Processor type and features"
include/asm-alpha/param.h +1 -9
@@ -5,15 +5,7 @@hardware ignores reprogramming. We also need userland buy-in to the change in HZ, since this is visible in the wait4 resources etc. */ - -#ifndef HZ -# ifndef CONFIG_ALPHA_RAWHIDE -# define HZ 1024 -# else -# define HZ 1200 -# endif -#endif - +#define HZ CONFIG_HZ#define USER_HZ HZ#define EXEC_PAGESIZE 8192
include/asm-cris/param.h +1 -1
@@ -3,7 +3,7 @@/* Currently we assume that HZ0 is good for CRIS. */ #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 CLOCKS_PER_SEC (USER_HZ)/* like times() */ #endif
include/asm-frv/param.h +1 -1
@@ -2,7 +2,7 @@#define _ASM_PARAM_H#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 CLOCKS_PER_SEC (USER_HZ)/* like times() */ #endif
include/asm-h8300/param.h +1 -1
@@ -3,7 +3,7 @@#ifndef HZ -#define HZ 100 +#define HZ CONFIG_HZ#endif #ifdef __KERNEL__
include/asm-m32r/param.h +1 -1
@@ -2,7 +2,7 @@#define _ASM_M32R_PARAM_H#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 CLOCKS_PER_SEC (USER_HZ)/* like times() */ #endif
include/asm-m68k/param.h +1 -1
@@ -2,7 +2,7 @@#define _M68K_PARAM_H#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 CLOCKS_PER_SEC (USER_HZ)/* like times() */ #endif
include/asm-m68knommu/param.h +1 -7
@@ -1,13 +1,7 @@#ifndef _M68KNOMMU_PARAM_H#define _M68KNOMMU_PARAM_H - -#if defined(CONFIG_CLEOPATRA ) -#define HZ 1000 -#endif -#ifndef HZ -#define HZ 100 -#endif +#define HZ CONFIG_HZ#ifdef __KERNEL__#define USER_HZ HZ
include/asm-sh64/param.h +1 -5
@@ -14,11 +14,7 @@#ifdef __KERNEL__ -# ifdef CONFIG_SH_WDT -# define HZ 1000/* Needed for high-res WOVF */ -# else -# define HZ 100 -# endif +# define HZ CONFIG_HZ# define USER_HZ 100/* User interfaces are in "ticks" */ # define CLOCKS_PER_SEC (USER_HZ)/* frequency at which times() counts */ #endif
include/asm-sparc/param.h +1 -1
@@ -3,7 +3,7 @@#define _ASMSPARC_PARAM_H#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 CLOCKS_PER_SEC (USER_HZ)#endif
include/asm-v850/anna.h +0 -6
@@ -134,10 +134,4 @@ extern void anna_uart_pre_configure (unsigned chan,#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__ */
include/asm-v850/as85ep1.h +0 -6
@@ -149,10 +149,4 @@ extern void as85ep1_uart_pre_configure (unsigned chan,#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__ */
include/asm-v850/fpga85e2c.h +0 -6
@@ -79,10 +79,4 @@ extern char _r0_ram;#endif -/* For <asm/param.h> */ -#ifndef HZ -#define HZ 122/* actually, 8.192ms ticks =~ 122.07 */ -#endif - -#endif /* __V850_FPGA85E2C_H__ */
include/asm-v850/param.h +1 -2
@@ -23,8 +23,7 @@#define MAXHOSTNAMELEN 64/* max length of hostname */ #ifdef __KERNEL__ -#include <asm/machdep.h> /* For HZ */ - +# define HZ CONFIG_HZ# define USER_HZ 100# define CLOCKS_PER_SEC USER_HZ#endif
include/asm-v850/rte_cb.h +0 -6
@@ -69,12 +69,6 @@#endif /* CONFIG_RTE_MB_A_PCI */ -/* For <asm/param.h> */ -#ifndef HZ -#define HZ 100 -#endif - -#ifndef __ASSEMBLY__extern void rte_cb_early_init (void );extern void rte_cb_init_irqs (void );
include/asm-v850/sim.h +0 -5
@@ -40,11 +40,6 @@#define R0_RAM_ADDR 0xFFFFF000 -/* For <asm/param.h> */ -#ifndef HZ -#define HZ 24/* Minimum supported frequency. */ -#endif -/* For <asm/irq.h> */ #define NUM_CPU_IRQS 6
include/asm-v850/sim85e2.h +0 -6
@@ -66,10 +66,4 @@#define R0_RAM_ADDR 0xFFFFE000 -/* For <asm/param.h> */ -#ifndef HZ -#define HZ 24/* Minimum supported frequency. */ -#endif - -#endif /* __V850_SIM85E2_H__ */
include/asm-xtensa/param.h +1 -1
@@ -12,7 +12,7 @@#define _XTENSA_PARAM_H#ifdef __KERNEL__ -# define HZ 100/* internal timer frequency */ +# define HZ CONFIG_HZ/* internal timer frequency */ # define USER_HZ 100/* for user interfaces in "ticks" */ # define CLOCKS_PER_SEC (USER_HZ)/* frequnzy at which times() counts */ #endif
kernel/Makefile +8 -0
@@ -80,3 +80,11 @@ quiet_cmd_ikconfiggz = IKCFG $@ targets += config_data.h $(obj)/config_data.h: $(obj)/config_data.gz FORCE $(call if_changed,ikconfiggz) + +$(obj)/time.o: $(obj)/timeconst.h + +quiet_cmd_timeconst = TIMEC $@ + cmd_timeconst = $(PERL) $< $(CONFIG_HZ) > $@ +targets += timeconst.h +$(obj)/timeconst.h: $(src)/timeconst.pl FORCE + $(call if_changed,timeconst)
kernel/time.c +21 -8
@@ -39,6 +39,8 @@#include <asm/uaccess.h> #include <asm/unistd.h> +#include "timeconst.h" +/* * The timezone where the local system is located. Used as a default by some * programs who obtain this value by using gettimeofday.
@@ -93,7 +95,8 @@ asmlinkage long sys_stime(time_t __user *tptr)#endif /* __ARCH_WANT_SYS_TIME */ -asmlinkage long sys_gettimeofday (struct timeval __user *tv,struct timezone __user *tz) +asmlinkage long sys_gettimeofday (struct timeval __user *tv, +struct timezone __user *tz) {if (likely (tv !=NULL )) {struct timeval ktv;
@@ -118,7 +121,7 @@ asmlinkage long sys_gettimeofday(struct timeval __user *tv, struct timezone __us* hard to make the program warp the clock precisely n hours) or * compile in the timezone information into the kernel. Bad, bad.... * -* - TYT, 1992-01-01 +* - TYT, 1992-01-01 * * The best thing to do is to keep the CMOS clock in universal time (UTC) * as real UNIX machines always do it. This avoids all headaches about
@@ -239,7 +242,11 @@ unsigned int inline jiffies_to_msecs(const unsigned long j)#elif HZ >MSEC_PER_SEC && !(HZ %MSEC_PER_SEC )return (j + (HZ /MSEC_PER_SEC ) -1 )/(HZ /MSEC_PER_SEC );#else -return (j *MSEC_PER_SEC ) /HZ ; +# if BITS_PER_LONG = 32 +return ((u64 )HZ_TO_MSEC_MUL32 * j) >>HZ_TO_MSEC_SHR32 ; +# else +return (j *HZ_TO_MSEC_NUM ) /HZ_TO_MSEC_DEN ; +# endif #endif }EXPORT_SYMBOL (jiffies_to_msecs);
@@ -251,7 +258,11 @@ unsigned int inline jiffies_to_usecs(const unsigned long j)#elif HZ >USEC_PER_SEC && !(HZ %USEC_PER_SEC )return (j + (HZ /USEC_PER_SEC ) -1 )/(HZ /USEC_PER_SEC );#else -return (j *USEC_PER_SEC ) /HZ ; +# if BITS_PER_LONG = 32 +return ((u64 )HZ_TO_USEC_MUL32 * j) >>HZ_TO_USEC_SHR32 ; +# else +return (j *HZ_TO_USEC_NUM ) /HZ_TO_USEC_DEN ; +# endif #endif }EXPORT_SYMBOL (jiffies_to_usecs);
@@ -351,7 +362,7 @@ EXPORT_SYMBOL(mktime);* normalize to the timespec storage format * * Note: The tv_nsec part is always in the range of -* 0 <= tv_nsec < NSEC_PER_SEC +* 0 <= tv_nsec < NSEC_PER_SEC * For negative values only the tv_sec field is negative ! */ void set_normalized_timespec (struct timespec *ts,time_t sec,long nsec)
@@ -452,12 +463,13 @@ unsigned long msecs_to_jiffies(const unsigned int m)/* * Generic case - multiply, round and divide. But first * check that if we are doing a net multiplication, that -* we wouldnt overflow: +* we wouldn't overflow: */ if (HZ >MSEC_PER_SEC && m >jiffies_to_msecs (MAX_JIFFY_OFFSET ))return MAX_JIFFY_OFFSET ; -return (m *HZ +MSEC_PER_SEC -1 ) /MSEC_PER_SEC ; +return ((u64 )MSEC_TO_HZ_MUL32 * m +MSEC_TO_HZ_ADJ32 ) + >>MSEC_TO_HZ_SHR32 ;#endif }EXPORT_SYMBOL (msecs_to_jiffies);
@@ -471,7 +483,8 @@ unsigned long usecs_to_jiffies(const unsigned int u)#elif HZ >USEC_PER_SEC && !(HZ %USEC_PER_SEC )return u * (HZ /USEC_PER_SEC );#else -return (u *HZ +USEC_PER_SEC -1 ) /USEC_PER_SEC ; +return ((u64 )USEC_TO_HZ_MUL32 * m +USEC_TO_HZ_ADJ32 ) + >>USEC_TO_HZ_SHR32 ;#endif }EXPORT_SYMBOL (usecs_to_jiffies);
kernel/timeconst.pl new file +340 -0
340 lines not on this page · raw message
Signature · 1 line
1.5.3.4
Re: [PATCH] Avoid overflows in kernel/time.c (version 5)
Quoted 10 lines · On Mon, 17 Dec 2007 13:53:23 -0800 "H. Peter Anvin" <[email protected]> wrote:
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.
Quoted 8 lines
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).
alpha allmodconfig:
kernel/time.c: In function 'jiffies_to_msecs':kernel/time.c:248: error: 'HZ_TO_MSEC_NUM' undeclared (first use in this function) kernel/time.c:248: error: (Each undeclared identifier is reported only once kernel/time.c:248: error: for each function it appears in.) kernel/time.c:248: error: 'HZ_TO_MSEC_DEN' undeclared (first use in this function) kernel/time.c: In function 'jiffies_to_usecs':kernel/time.c:264: error: 'HZ_TO_USEC_NUM' undeclared (first use in this function) kernel/time.c:264: error: 'HZ_TO_USEC_DEN' undeclared (first use in this function) kernel/time.c: In function 'usecs_to_jiffies':kernel/time.c:486: error: 'm' undeclared (first use in this function) kernel/time.c:489: warning: control reaches end of non-void function kernel/time.c: In function 'jiffies_to_usecs':kernel/time.c:267: warning: control reaches end of non-void function kernel/time.c: In function 'jiffies_to_msecs':kernel/time.c:251: warning: control reaches end of non-void function
PATCH 1/1 Avoid overflows in kernel/time.c (version 6)
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.
---
This version adds back the _NUM and _DEN constants, which were inadvertently dropped when converting the script to Perl.
This version also drops the sh64 fix, since Paul Mundt has fixed sh and sh64 in the sh tree.
arch/alpha/Kconfig | 5 + arch/cris/Kconfig | 4 + arch/frv/Kconfig | 4 + arch/h8300/Kconfig | 4 + arch/m32r/Kconfig | 4 + arch/m68k/Kconfig | 4 + arch/m68knommu/Kconfig | 5 + arch/sparc/Kconfig | 4 + arch/v850/Kconfig | 7 + arch/xtensa/Kconfig | 4 + include/asm-alpha/param.h | 10 +- include/asm-cris/param.h | 2 +- include/asm-frv/param.h | 2 +- include/asm-h8300/param.h | 2 +- include/asm-m32r/param.h | 2 +- include/asm-m68k/param.h | 2 +- include/asm-m68knommu/param.h | 8 +- include/asm-sh64/param.h | 6 +- include/asm-sparc/param.h | 2 +- include/asm-v850/anna.h | 6 - include/asm-v850/as85ep1.h | 6 - include/asm-v850/fpga85e2c.h | 6 - include/asm-v850/param.h | 3 +- include/asm-v850/rte_cb.h | 6 - include/asm-v850/sim.h | 5 - include/asm-v850/sim85e2.h | 6 - include/asm-xtensa/param.h | 2 +- kernel/Makefile | 8 + kernel/time.c | 29 ++- kernel/timeconst.pl | 402 +++++++++++++++++++++++++++++++++++++++++ 30 files changed, 487 insertions(+), 73 deletions(-) create mode 100644 kernel/timeconst.pl
arch/alpha/Kconfig +5 -0
@@ -616,6 +616,11 @@ config VERBOSE_MCHECK_ON 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/eisa/Kconfig"
arch/cris/Kconfig +4 -0
@@ -55,6 +55,10 @@ config CRIS bool default y +config HZ + int + default 100 + source "init/Kconfig" menu "General setup"
arch/frv/Kconfig +4 -0
@@ -57,6 +57,10 @@ config ARCH_HAS_ILOG2_U64 bool default y +config HZ + int + default 1000 + mainmenu "Fujitsu FR-V Kernel Configuration" source "init/Kconfig"
arch/h8300/Kconfig +4 -0
@@ -79,6 +79,10 @@ config PCI bool default n +config HZ + int + default 100 + source "init/Kconfig" source "arch/h8300/Kconfig.cpu"
arch/m32r/Kconfig +4 -0
@@ -34,6 +34,10 @@ config NO_IOPORT config NO_DMA def_bool y +config HZ + int + default 100 + source "init/Kconfig"
arch/m68k/Kconfig +4 -0
@@ -52,6 +52,10 @@ config NO_IOPORT config NO_DMA def_bool SUN3 +config HZ + int + default 100 + mainmenu "Linux/68k Kernel Configuration" source "init/Kconfig"
arch/m68knommu/Kconfig +5 -0
@@ -518,6 +518,11 @@ config 4KSTACKS running more threads on a system and also reduces the pressure on the VM subsystem for higher order allocations. +config HZ + int + default 1000 if CLEOPATRA + default 100 + comment "RAM configuration" config RAMBASE
arch/sparc/Kconfig +4 -0
@@ -27,6 +27,10 @@ config ARCH_NO_VIRT_TO_BUS config OF def_bool y +config HZ + int + default 100 + source "init/Kconfig" menu "General machine setup"
arch/v850/Kconfig +7 -0
@@ -212,6 +212,13 @@ menu "Processor type and features" bool 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 config ROM_KERNEL
arch/xtensa/Kconfig +4 -0
@@ -49,6 +49,10 @@ config ARCH_HAS_ILOG2_U64 config NO_IOPORT def_bool y +config HZ + int + default 100 + source "init/Kconfig" menu "Processor type and features"
include/asm-alpha/param.h +1 -9
@@ -5,15 +5,7 @@hardware ignores reprogramming. We also need userland buy-in to the change in HZ, since this is visible in the wait4 resources etc. */ - -#ifndef HZ -# ifndef CONFIG_ALPHA_RAWHIDE -# define HZ 1024 -# else -# define HZ 1200 -# endif -#endif - +#define HZ CONFIG_HZ#define USER_HZ HZ#define EXEC_PAGESIZE 8192
include/asm-cris/param.h +1 -1
@@ -3,7 +3,7 @@/* Currently we assume that HZ0 is good for CRIS. */ #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 CLOCKS_PER_SEC (USER_HZ)/* like times() */ #endif
include/asm-frv/param.h +1 -1
@@ -2,7 +2,7 @@#define _ASM_PARAM_H#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 CLOCKS_PER_SEC (USER_HZ)/* like times() */ #endif
include/asm-h8300/param.h +1 -1
@@ -3,7 +3,7 @@#ifndef HZ -#define HZ 100 +#define HZ CONFIG_HZ#endif #ifdef __KERNEL__
include/asm-m32r/param.h +1 -1
@@ -2,7 +2,7 @@#define _ASM_M32R_PARAM_H#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 CLOCKS_PER_SEC (USER_HZ)/* like times() */ #endif
include/asm-m68k/param.h +1 -1
@@ -2,7 +2,7 @@#define _M68K_PARAM_H#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 CLOCKS_PER_SEC (USER_HZ)/* like times() */ #endif
include/asm-m68knommu/param.h +1 -7
@@ -1,13 +1,7 @@#ifndef _M68KNOMMU_PARAM_H#define _M68KNOMMU_PARAM_H - -#if defined(CONFIG_CLEOPATRA ) -#define HZ 1000 -#endif -#ifndef HZ -#define HZ 100 -#endif +#define HZ CONFIG_HZ#ifdef __KERNEL__#define USER_HZ HZ
include/asm-sh64/param.h +1 -5
@@ -14,11 +14,7 @@#ifdef __KERNEL__ -# ifdef CONFIG_SH_WDT -# define HZ 1000/* Needed for high-res WOVF */ -# else -# define HZ 100 -# endif +# define HZ CONFIG_HZ# define USER_HZ 100/* User interfaces are in "ticks" */ # define CLOCKS_PER_SEC (USER_HZ)/* frequency at which times() counts */ #endif
include/asm-sparc/param.h +1 -1
@@ -3,7 +3,7 @@#define _ASMSPARC_PARAM_H#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 CLOCKS_PER_SEC (USER_HZ)#endif
include/asm-v850/anna.h +0 -6
@@ -134,10 +134,4 @@ extern void anna_uart_pre_configure (unsigned chan,#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__ */
include/asm-v850/as85ep1.h +0 -6
@@ -149,10 +149,4 @@ extern void as85ep1_uart_pre_configure (unsigned chan,#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__ */
include/asm-v850/fpga85e2c.h +0 -6
@@ -79,10 +79,4 @@ extern char _r0_ram;#endif -/* For <asm/param.h> */ -#ifndef HZ -#define HZ 122/* actually, 8.192ms ticks =~ 122.07 */ -#endif - -#endif /* __V850_FPGA85E2C_H__ */
include/asm-v850/param.h +1 -2
@@ -23,8 +23,7 @@#define MAXHOSTNAMELEN 64/* max length of hostname */ #ifdef __KERNEL__ -#include <asm/machdep.h> /* For HZ */ - +# define HZ CONFIG_HZ# define USER_HZ 100# define CLOCKS_PER_SEC USER_HZ#endif
include/asm-v850/rte_cb.h +0 -6
@@ -69,12 +69,6 @@#endif /* CONFIG_RTE_MB_A_PCI */ -/* For <asm/param.h> */ -#ifndef HZ -#define HZ 100 -#endif - -#ifndef __ASSEMBLY__extern void rte_cb_early_init (void );extern void rte_cb_init_irqs (void );
include/asm-v850/sim.h +0 -5
@@ -40,11 +40,6 @@#define R0_RAM_ADDR 0xFFFFF000 -/* For <asm/param.h> */ -#ifndef HZ -#define HZ 24/* Minimum supported frequency. */ -#endif -/* For <asm/irq.h> */ #define NUM_CPU_IRQS 6
include/asm-v850/sim85e2.h +0 -6
@@ -66,10 +66,4 @@#define R0_RAM_ADDR 0xFFFFE000 -/* For <asm/param.h> */ -#ifndef HZ -#define HZ 24/* Minimum supported frequency. */ -#endif - -#endif /* __V850_SIM85E2_H__ */
include/asm-xtensa/param.h +1 -1
@@ -12,7 +12,7 @@#define _XTENSA_PARAM_H#ifdef __KERNEL__ -# define HZ 100/* internal timer frequency */ +# define HZ CONFIG_HZ/* internal timer frequency */ # define USER_HZ 100/* for user interfaces in "ticks" */ # define CLOCKS_PER_SEC (USER_HZ)/* frequnzy at which times() counts */ #endif
kernel/Makefile +8 -0
@@ -80,3 +80,11 @@ quiet_cmd_ikconfiggz = IKCFG $@ targets += config_data.h $(obj)/config_data.h: $(obj)/config_data.gz FORCE $(call if_changed,ikconfiggz) + +$(obj)/time.o: $(obj)/timeconst.h + +quiet_cmd_timeconst = TIMEC $@ + cmd_timeconst = $(PERL) $< $(CONFIG_HZ) > $@ +targets += timeconst.h +$(obj)/timeconst.h: $(src)/timeconst.pl FORCE + $(call if_changed,timeconst)
kernel/time.c +21 -8
@@ -39,6 +39,8 @@#include <asm/uaccess.h> #include <asm/unistd.h> +#include "timeconst.h" +/* * The timezone where the local system is located. Used as a default by some * programs who obtain this value by using gettimeofday.
@@ -93,7 +95,8 @@ asmlinkage long sys_stime(time_t __user *tptr)#endif /* __ARCH_WANT_SYS_TIME */ -asmlinkage long sys_gettimeofday (struct timeval __user *tv,struct timezone __user *tz) +asmlinkage long sys_gettimeofday (struct timeval __user *tv, +struct timezone __user *tz) {if (likely (tv !=NULL )) {struct timeval ktv;
@@ -118,7 +121,7 @@ asmlinkage long sys_gettimeofday(struct timeval __user *tv, struct timezone __us* hard to make the program warp the clock precisely n hours) or * compile in the timezone information into the kernel. Bad, bad.... * -* - TYT, 1992-01-01 +* - TYT, 1992-01-01 * * The best thing to do is to keep the CMOS clock in universal time (UTC) * as real UNIX machines always do it. This avoids all headaches about
@@ -239,7 +242,11 @@ unsigned int inline jiffies_to_msecs(const unsigned long j)#elif HZ >MSEC_PER_SEC && !(HZ %MSEC_PER_SEC )return (j + (HZ /MSEC_PER_SEC ) -1 )/(HZ /MSEC_PER_SEC );#else -return (j *MSEC_PER_SEC ) /HZ ; +# if BITS_PER_LONG = 32 +return ((u64 )HZ_TO_MSEC_MUL32 * j) >>HZ_TO_MSEC_SHR32 ; +# else +return (j *HZ_TO_MSEC_NUM ) /HZ_TO_MSEC_DEN ; +# endif #endif }EXPORT_SYMBOL (jiffies_to_msecs);
@@ -251,7 +258,11 @@ unsigned int inline jiffies_to_usecs(const unsigned long j)#elif HZ >USEC_PER_SEC && !(HZ %USEC_PER_SEC )return (j + (HZ /USEC_PER_SEC ) -1 )/(HZ /USEC_PER_SEC );#else -return (j *USEC_PER_SEC ) /HZ ; +# if BITS_PER_LONG = 32 +return ((u64 )HZ_TO_USEC_MUL32 * j) >>HZ_TO_USEC_SHR32 ; +# else +return (j *HZ_TO_USEC_NUM ) /HZ_TO_USEC_DEN ; +# endif #endif }EXPORT_SYMBOL (jiffies_to_usecs);
@@ -351,7 +362,7 @@ EXPORT_SYMBOL(mktime);* normalize to the timespec storage format * * Note: The tv_nsec part is always in the range of -* 0 <= tv_nsec < NSEC_PER_SEC +* 0 <= tv_nsec < NSEC_PER_SEC * For negative values only the tv_sec field is negative ! */ void set_normalized_timespec (struct timespec *ts,time_t sec,long nsec)
@@ -452,12 +463,13 @@ unsigned long msecs_to_jiffies(const unsigned int m)/* * Generic case - multiply, round and divide. But first * check that if we are doing a net multiplication, that -* we wouldnt overflow: +* we wouldn't overflow: */ if (HZ >MSEC_PER_SEC && m >jiffies_to_msecs (MAX_JIFFY_OFFSET ))return MAX_JIFFY_OFFSET ; -return (m *HZ +MSEC_PER_SEC -1 ) /MSEC_PER_SEC ; +return ((u64 )MSEC_TO_HZ_MUL32 * m +MSEC_TO_HZ_ADJ32 ) + >>MSEC_TO_HZ_SHR32 ;#endif }EXPORT_SYMBOL (msecs_to_jiffies);
@@ -471,7 +483,8 @@ unsigned long usecs_to_jiffies(const unsigned int u)#elif HZ >USEC_PER_SEC && !(HZ %USEC_PER_SEC )return u * (HZ /USEC_PER_SEC );#else -return (u *HZ +USEC_PER_SEC -1 ) /USEC_PER_SEC ; +return ((u64 )USEC_TO_HZ_MUL32 * u +USEC_TO_HZ_ADJ32 ) + >>USEC_TO_HZ_SHR32 ;#endif }EXPORT_SYMBOL (usecs_to_jiffies);
kernel/timeconst.pl new file +402 -0
402 lines not on this page · raw message
Signature · 1 line
1.5.3.6
Re: [PATCH] Avoid overflows in kernel/time.c (version 5)
In reply to H. Peter AnvinHi,
Quoted 1 line · H. Peter Anvin
kernel/timeconst.pl | 340
I agree with Jan that it would be better to put this into scripts. In the long term we could also detect some of the simple special cases, so we can finally inline some of these functions again, this would mean the generated file would go into include/linux/ and the script should be in scripts/.
kernel/timeconst.pl line 174
Quoted 1 line
kernel/timeconst.pl new file +1
-0+ my ($b,$n,$d) = @_;
I noticed this already in the old script, using something else than single letter variables would increase readability considerably.
bye, Roman