All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 1/2] math: Introduce div64_long
@ 2012-03-15 16:36 Sasha Levin
  2012-03-15 16:36 ` [PATCH 2/2] ntp: Fix integer overflow when setting time Sasha Levin
  2012-03-15 21:52 ` [tip:timers/core] math: Introduce div64_long tip-bot for Sasha Levin
  0 siblings, 2 replies; 7+ messages in thread
From: Sasha Levin @ 2012-03-15 16:36 UTC (permalink / raw)
  To: johnstul, tglx, linux-kernel; +Cc: Sasha Levin

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).

Suggested-by: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: Sasha Levin <levinsasha928@gmail.com>
---
 include/linux/math64.h |    4 ++++
 1 files changed, 4 insertions(+), 0 deletions(-)

diff --git a/include/linux/math64.h b/include/linux/math64.h
index 23fcdfc..b8ba855 100644
--- a/include/linux/math64.h
+++ b/include/linux/math64.h
@@ -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)
 {
-- 
1.7.8.4


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH 2/2] ntp: Fix integer overflow when setting time
  2012-03-15 16:36 [PATCH 1/2] math: Introduce div64_long Sasha Levin
@ 2012-03-15 16:36 ` Sasha Levin
  2012-03-15 21:53   ` [tip:timers/core] " tip-bot for Sasha Levin
  2012-03-15 21:52 ` [tip:timers/core] math: Introduce div64_long tip-bot for Sasha Levin
  1 sibling, 1 reply; 7+ messages in thread
From: Sasha Levin @ 2012-03-15 16:36 UTC (permalink / raw)
  To: johnstul, tglx, linux-kernel; +Cc: Sasha Levin

'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.

Signed-off-by: Sasha Levin <levinsasha928@gmail.com>
---
 kernel/time/ntp.c |    6 +++++-
 1 files changed, 5 insertions(+), 1 deletions(-)

diff --git a/kernel/time/ntp.c b/kernel/time/ntp.c
index 17fb1b9..c83c228 100644
--- a/kernel/time/ntp.c
+++ b/kernel/time/ntp.c
@@ -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)
-- 
1.7.8.4


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [tip:timers/core] math: Introduce div64_long
  2012-03-15 16:36 [PATCH 1/2] math: Introduce div64_long Sasha Levin
  2012-03-15 16:36 ` [PATCH 2/2] ntp: Fix integer overflow when setting time Sasha Levin
@ 2012-03-15 21:52 ` tip-bot for Sasha Levin
  1 sibling, 0 replies; 7+ messages in thread
From: tip-bot for Sasha Levin @ 2012-03-15 21:52 UTC (permalink / raw)
  To: linux-tip-commits; +Cc: linux-kernel, levinsasha928, hpa, mingo, tglx

Commit-ID:  f910381a55cdaa097030291f272f6e6e4380c39a
Gitweb:     http://git.kernel.org/tip/f910381a55cdaa097030291f272f6e6e4380c39a
Author:     Sasha Levin <levinsasha928@gmail.com>
AuthorDate: Thu, 15 Mar 2012 12:36:13 -0400
Committer:  Thomas Gleixner <tglx@linutronix.de>
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).

Suggested-by: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: Sasha Levin <levinsasha928@gmail.com>
Cc: johnstul@us.ibm.com
Link: http://lkml.kernel.org/r/1331829374-31543-1-git-send-email-levinsasha928@gmail.com
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
---
 include/linux/math64.h |    4 ++++
 1 files changed, 4 insertions(+), 0 deletions(-)

diff --git a/include/linux/math64.h b/include/linux/math64.h
index 23fcdfc..b8ba855 100644
--- a/include/linux/math64.h
+++ b/include/linux/math64.h
@@ -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)
 {

^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [tip:timers/core] ntp: Fix integer overflow when setting time
  2012-03-15 16:36 ` [PATCH 2/2] ntp: Fix integer overflow when setting time Sasha Levin
@ 2012-03-15 21:53   ` tip-bot for Sasha Levin
  2012-03-16  1:29     ` H. Peter Anvin
  0 siblings, 1 reply; 7+ messages in thread
From: tip-bot for Sasha Levin @ 2012-03-15 21:53 UTC (permalink / raw)
  To: linux-tip-commits; +Cc: linux-kernel, levinsasha928, hpa, mingo, tglx

Commit-ID:  a078c6d0e6288fad6d83fb6d5edd91ddb7b6ab33
Gitweb:     http://git.kernel.org/tip/a078c6d0e6288fad6d83fb6d5edd91ddb7b6ab33
Author:     Sasha Levin <levinsasha928@gmail.com>
AuthorDate: Thu, 15 Mar 2012 12:36:14 -0400
Committer:  Thomas Gleixner <tglx@linutronix.de>
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.

Signed-off-by: Sasha Levin <levinsasha928@gmail.com>
Cc: johnstul@us.ibm.com
Link: http://lkml.kernel.org/r/1331829374-31543-2-git-send-email-levinsasha928@gmail.com
Cc: stable@vger.kernel.org
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
---
 kernel/time/ntp.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)

diff --git a/kernel/time/ntp.c b/kernel/time/ntp.c
index 17fb1b9..6e039b1 100644
--- a/kernel/time/ntp.c
+++ b/kernel/time/ntp.c
@@ -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)

^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [tip:timers/core] ntp: Fix integer overflow when setting time
  2012-03-15 21:53   ` [tip:timers/core] " tip-bot for Sasha Levin
@ 2012-03-16  1:29     ` H. Peter Anvin
  2012-03-16  9:48       ` Thomas Gleixner
  0 siblings, 1 reply; 7+ messages in thread
From: H. Peter Anvin @ 2012-03-16  1:29 UTC (permalink / raw)
  To: mingo, hpa, levinsasha928, linux-kernel, tglx; +Cc: linux-tip-commits

On 03/15/2012 02:53 PM, tip-bot for Sasha Levin wrote:
> Commit-ID:  a078c6d0e6288fad6d83fb6d5edd91ddb7b6ab33
> Gitweb:     http://git.kernel.org/tip/a078c6d0e6288fad6d83fb6d5edd91ddb7b6ab33
> Author:     Sasha Levin <levinsasha928@gmail.com>
> AuthorDate: Thu, 15 Mar 2012 12:36:14 -0400
> Committer:  Thomas Gleixner <tglx@linutronix.de>
> 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.
> 

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

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


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [tip:timers/core] ntp: Fix integer overflow when setting time
  2012-03-16  1:29     ` H. Peter Anvin
@ 2012-03-16  9:48       ` Thomas Gleixner
  2012-03-16 14:42         ` H. Peter Anvin
  0 siblings, 1 reply; 7+ messages in thread
From: Thomas Gleixner @ 2012-03-16  9:48 UTC (permalink / raw)
  To: H. Peter Anvin; +Cc: mingo, levinsasha928, linux-kernel, linux-tip-commits

On Thu, 15 Mar 2012, H. Peter Anvin wrote:

> On 03/15/2012 02:53 PM, tip-bot for Sasha Levin wrote:
> > Commit-ID:  a078c6d0e6288fad6d83fb6d5edd91ddb7b6ab33
> > Gitweb:     http://git.kernel.org/tip/a078c6d0e6288fad6d83fb6d5edd91ddb7b6ab33
> > Author:     Sasha Levin <levinsasha928@gmail.com>
> > AuthorDate: Thu, 15 Mar 2012 12:36:14 -0400
> > Committer:  Thomas Gleixner <tglx@linutronix.de>
> > 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.
> > 
> 
> 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 :)

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [tip:timers/core] ntp: Fix integer overflow when setting time
  2012-03-16  9:48       ` Thomas Gleixner
@ 2012-03-16 14:42         ` H. Peter Anvin
  0 siblings, 0 replies; 7+ messages in thread
From: H. Peter Anvin @ 2012-03-16 14:42 UTC (permalink / raw)
  To: Thomas Gleixner; +Cc: mingo, levinsasha928, linux-kernel, linux-tip-commits

On 03/16/2012 02:48 AM, Thomas Gleixner wrote:
>>
>> 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

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


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2012-03-16 14:43 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-03-15 16:36 [PATCH 1/2] math: Introduce div64_long Sasha Levin
2012-03-15 16:36 ` [PATCH 2/2] ntp: Fix integer overflow when setting time Sasha Levin
2012-03-15 21:53   ` [tip:timers/core] " tip-bot for Sasha Levin
2012-03-16  1:29     ` H. Peter Anvin
2012-03-16  9:48       ` Thomas Gleixner
2012-03-16 14:42         ` H. Peter Anvin
2012-03-15 21:52 ` [tip:timers/core] math: Introduce div64_long tip-bot for Sasha Levin

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.