Skip to contentSkip to conversation

math: Introduce div64_long

PATCH In review Sasha Levin 3 participants 7 messages Mar 15, 2012 – Mar 16, 2012 Timekeeping, clocksource core, NTP, alarmtimer

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 math: Introduce div64_long

Add a div64_long macro which is used to devide a 64bit number by a long (which can be 4 bytes on 32bit systems and 8 bytes on 64bit systems).

---
 include/linux/math64.h |    4 ++++
 1 files changed, 4 insertions(+), 0 deletions(-)
include/linux/math64.h +4 -0
index 23fcdfc..b8ba855 100644
@@ -6,6 +6,8 @@
 
 #if BITS_PER_LONG == 64
 
+#define div64_long(x,y) div64_s64((x),(y))
+
 /**
  * div_u64_rem - unsigned 64bit divide with 32bit divisor with remainder
  *
@@ -45,6 +47,8 @@ static inline s64 div64_s64(s64 dividend, s64 divisor)
 
 #elif BITS_PER_LONG == 32
 
+#define div64_long(x,y) div_s64((x),(y))
+
 #ifndef div_u64_rem
 static inline u64 div_u64_rem(u64 dividend, u32 divisor, u32 *remainder)
 {
Signature · 1 line
1.7.8.4
#1 lore raw

PATCH 2/2 ntp: Fix integer overflow when setting time

'long secs' was being passed as divisor to div_s64, which accepts a 32bit divisor. On 64bit machines that value would be trimmed back from 8 bytes back to 4, allowing a divide by zero when the number is bigger than (1 << 32) - 1 and all 32 lower bits are 0.

---
 kernel/time/ntp.c |    6 +++++-
 1 files changed, 5 insertions(+), 1 deletions(-)
kernel/time/ntp.c +5 -1
index 17fb1b9..c83c228 100644
@@ -289,7 +289,11 @@ static inline s64 ntp_update_offset_fll(s64 offset64, long secs)
 
 	time_status |= STA_MODE;
 
-	return div_s64(offset64 << (NTP_SCALE_SHIFT - SHIFT_FLL), secs);
+	/* 
+	 * secs is 8 bytes on 64bit systems, which means that it can
+	 * wrap around when dividing.
+	 */
+	return div64_long(offset64 << (NTP_SCALE_SHIFT - SHIFT_FLL), secs);
 }
 
 static void ntp_update_offset(long offset)
Signature · 1 line
1.7.8.4
#6 lore raw
In reply to Sasha Levin
Commit-ID:  f910381a55cdaa097030291f272f6e6e4380c39a
Gitweb:     http://git.kernel.org/tip/f910381a55cdaa097030291f272f6e6e4380c39a
Author:     Sasha Levin <[email protected]>
AuthorDate: Thu, 15 Mar 2012 12:36:13 -0400
Committer:  Thomas Gleixner <[email protected]>
CommitDate: Thu, 15 Mar 2012 21:41:34 +0100

math: Introduce div64_long

Add a div64_long macro which is used to devide a 64bit number by a long (which can be 4 bytes on 32bit systems and 8 bytes on 64bit systems).

---
 include/linux/math64.h |    4 ++++
 1 files changed, 4 insertions(+), 0 deletions(-)
include/linux/math64.h +4 -0
index 23fcdfc..b8ba855 100644
@@ -6,6 +6,8 @@
 
 #if BITS_PER_LONG == 64
 
+#define div64_long(x,y) div64_s64((x),(y))
+
 /**
  * div_u64_rem - unsigned 64bit divide with 32bit divisor with remainder
  *
@@ -45,6 +47,8 @@ static inline s64 div64_s64(s64 dividend, s64 divisor)
 
 #elif BITS_PER_LONG == 32
 
+#define div64_long(x,y) div_s64((x),(y))
+
 #ifndef div_u64_rem
 static inline u64 div_u64_rem(u64 dividend, u32 divisor, u32 *remainder)
 {
#2 lore raw

[tip:timers/core] ntp: Fix integer overflow when setting time

In reply to Sasha Levin
Commit-ID:  a078c6d0e6288fad6d83fb6d5edd91ddb7b6ab33
Gitweb:     http://git.kernel.org/tip/a078c6d0e6288fad6d83fb6d5edd91ddb7b6ab33
Author:     Sasha Levin <[email protected]>
AuthorDate: Thu, 15 Mar 2012 12:36:14 -0400
Committer:  Thomas Gleixner <[email protected]>
CommitDate: Thu, 15 Mar 2012 21:41:34 +0100

ntp: Fix integer overflow when setting time

'long secs' is passed as divisor to div_s64, which accepts a 32bit divisor. On 64bit machines that value is trimmed back from 8 bytes back to 4, causing a divide by zero when the number is bigger than (1 << 32) - 1 and all 32 lower bits are 0.

Use div64_long() instead.

---
 kernel/time/ntp.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
kernel/time/ntp.c +1 -1
index 17fb1b9..6e039b1 100644
@@ -289,7 +289,7 @@ static inline s64 ntp_update_offset_fll(s64 offset64, long secs)
 
 	time_status |= STA_MODE;
 
-	return div_s64(offset64 << (NTP_SCALE_SHIFT - SHIFT_FLL), secs);
+	return div64_long(offset64 << (NTP_SCALE_SHIFT - SHIFT_FLL), secs);
 }
 
 static void ntp_update_offset(long offset)
#3 lore raw

Re: [tip:timers/core] ntp: Fix integer overflow when setting time

Quoted 7 lines · tip-bot for Sasha Levin
Commit-ID:  a078c6d0e6288fad6d83fb6d5edd91ddb7b6ab33
Gitweb:     http://git.kernel.org/tip/a078c6d0e6288fad6d83fb6d5edd91ddb7b6ab33
Author:     Sasha Levin <[email protected]>
AuthorDate: Thu, 15 Mar 2012 12:36:14 -0400
Committer:  Thomas Gleixner <[email protected]>
CommitDate: Thu, 15 Mar 2012 21:41:34 +0100
Quoted 9 lines

ntp: Fix integer overflow when setting time

'long secs' is passed as divisor to div_s64, which accepts a 32bit divisor. On 64bit machines that value is trimmed back from 8 bytes back to 4, causing a divide by zero when the number is bigger than (1 << 32) - 1 and all 32 lower bits are 0.

Use div64_long() instead.

Perhaps we should use the same kind of multiply-and-shift tricks we're doing for jiffies conversion? If nothing else it ought to perform better.

-hpa

Signature · 2 lines
H. Peter Anvin, Intel Open Source Technology Center
I work for Intel.  I don't speak on their behalf.
#4 lore raw

Re: [tip:timers/core] ntp: Fix integer overflow when setting time

Quoted 15 lines · H. Peter Anvin
Quoted 14 lines · tip-bot for Sasha Levin
Commit-ID:  a078c6d0e6288fad6d83fb6d5edd91ddb7b6ab33
Gitweb:     http://git.kernel.org/tip/a078c6d0e6288fad6d83fb6d5edd91ddb7b6ab33
Author:     Sasha Levin <[email protected]>
AuthorDate: Thu, 15 Mar 2012 12:36:14 -0400
Committer:  Thomas Gleixner <[email protected]>
CommitDate: Thu, 15 Mar 2012 21:41:34 +0100

ntp: Fix integer overflow when setting time

'long secs' is passed as divisor to div_s64, which accepts a 32bit divisor. On 64bit machines that value is trimmed back from 8 bytes back to 4, causing a divide by zero when the number is bigger than (1 << 32) - 1 and all 32 lower bits are 0.

Quoted 5 lines
Quoted 2 lines

Use div64_long() instead.

Perhaps we should use the same kind of multiply-and-shift tricks we're doing for jiffies conversion? If nothing else it ought to perform better.

It's not a hotpath where performance matters, but divide by zero does :)

#5 lore raw

Re: [tip:timers/core] ntp: Fix integer overflow when setting time

Quoted 5 lines · Thomas Gleixner
Quoted 3 lines

Perhaps we should use the same kind of multiply-and-shift tricks we're doing for jiffies conversion? If nothing else it ought to perform better.

It's not a hotpath where performance matters, but divide by zero does :)

Doing the inverse multiply wouldn't have a divide by zero problem. What perhaps is more important (and the reason we went to shift-multiply) is that in addition to being faster, it also avoids unnnecessary overflows.
I would really like to figure out how to do proper shift-multiply for
the 64-bit jiffy conversions too.

-hpa

Signature · 2 lines
H. Peter Anvin, Intel Open Source Technology Center
I work for Intel.  I don't speak on their behalf.