* [PATCH 1/2] time: Fix timekeeping_freqadjust()'s incorrect use of abs() instead of abs64()
@ 2015-09-09 23:07 John Stultz
2015-09-13 8:32 ` Ingo Molnar
[not found] ` <tip-2619d7e9c92d524cb155ec89fd72875321512e5b@git.kernel.org>
0 siblings, 2 replies; 5+ messages in thread
From: John Stultz @ 2015-09-09 23:07 UTC (permalink / raw)
To: LKML
Cc: John Stultz, Nuno Gonçalves, Miroslav Lichvar,
Prarit Bhargava, Richard Cochran, Ingo Molnar, Thomas Gleixner,
stable
The internal clocksteering done for fine-grained error correction
uses a logarithmic approximation, so any time adjtimex() adjusts
the clock steering, timekeeping_freqadjust() quickly approximates
the correct clock frequency over a series of ticks.
Unfortunately, the logic in timekeeping_freqadjust(), introduced
in commit dc491596f639438 (Rework frequency adjustments to work
better w/ nohz), used the abs() function with a s64 error value
to calculate the size of the approximated adjustment to be made.
Per include/linux/kernel.h: "abs() should not be used for 64-bit
types (s64, u64, long long) - use abs64()".
Thus on 32-bit platforms, this resulted in the clocksteering to
take a quite dampended random walk trying to converge on the
proper frequency, which caused the adjustments to be made much
slower then intended (most easily observed when large adjustments
are made).
This patch fixes the issue by using abs64() instead.
Cc: Nuno Gonçalves <nunojpg@gmail.com>
Cc: Miroslav Lichvar <mlichvar@redhat.com>
Cc: Prarit Bhargava <prarit@redhat.com>
Cc: Richard Cochran <richardcochran@gmail.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: <stable@vger.kernel.org> # v3.17+
Reported-by: Nuno Gonçalves <nunojpg@gmail.com>
Tested-by: Nuno Goncalves <nunojpg@gmail.com>
Signed-off-by: John Stultz <john.stultz@linaro.org>
---
kernel/time/timekeeping.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c
index f6ee2e6..3739ac6 100644
--- a/kernel/time/timekeeping.c
+++ b/kernel/time/timekeeping.c
@@ -1614,7 +1614,7 @@ static __always_inline void timekeeping_freqadjust(struct timekeeper *tk,
negative = (tick_error < 0);
/* Sort out the magnitude of the correction */
- tick_error = abs(tick_error);
+ tick_error = abs64(tick_error);
for (adj = 0; tick_error > interval; adj++)
tick_error >>= 1;
--
1.9.1
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH 1/2] time: Fix timekeeping_freqadjust()'s incorrect use of abs() instead of abs64()
2015-09-09 23:07 [PATCH 1/2] time: Fix timekeeping_freqadjust()'s incorrect use of abs() instead of abs64() John Stultz
@ 2015-09-13 8:32 ` Ingo Molnar
2015-09-14 23:24 ` John Stultz
[not found] ` <tip-2619d7e9c92d524cb155ec89fd72875321512e5b@git.kernel.org>
1 sibling, 1 reply; 5+ messages in thread
From: Ingo Molnar @ 2015-09-13 8:32 UTC (permalink / raw)
To: John Stultz, Linus Torvalds, Andrew Morton, Peter Zijlstra,
Thomas Gleixner
Cc: LKML, Nuno Gonçalves, Miroslav Lichvar, Prarit Bhargava,
Richard Cochran, Thomas Gleixner, stable
* John Stultz <john.stultz@linaro.org> wrote:
> The internal clocksteering done for fine-grained error correction
> uses a logarithmic approximation, so any time adjtimex() adjusts
> the clock steering, timekeeping_freqadjust() quickly approximates
> the correct clock frequency over a series of ticks.
>
> Unfortunately, the logic in timekeeping_freqadjust(), introduced
> in commit dc491596f639438 (Rework frequency adjustments to work
> better w/ nohz), used the abs() function with a s64 error value
> to calculate the size of the approximated adjustment to be made.
>
> Per include/linux/kernel.h: "abs() should not be used for 64-bit
> types (s64, u64, long long) - use abs64()".
>
> Thus on 32-bit platforms, this resulted in the clocksteering to
> take a quite dampended random walk trying to converge on the
> proper frequency, which caused the adjustments to be made much
> slower then intended (most easily observed when large adjustments
> are made).
>
> This patch fixes the issue by using abs64() instead.
> /* Sort out the magnitude of the correction */
> - tick_error = abs(tick_error);
> + tick_error = abs64(tick_error);
Ugh, and we had this bug for almost two years!
Would it be possible to make abs() warn about 64-bit types during build time,
to prevent such mishaps?
Thanks,
Ingo
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH 1/2] time: Fix timekeeping_freqadjust()'s incorrect use of abs() instead of abs64()
2015-09-13 8:32 ` Ingo Molnar
@ 2015-09-14 23:24 ` John Stultz
0 siblings, 0 replies; 5+ messages in thread
From: John Stultz @ 2015-09-14 23:24 UTC (permalink / raw)
To: Ingo Molnar
Cc: Linus Torvalds, Andrew Morton, Peter Zijlstra, Thomas Gleixner,
LKML, Nuno Gonçalves, Miroslav Lichvar, Prarit Bhargava,
Richard Cochran, stable, Joe Perches
[-- Attachment #1: Type: text/plain, Size: 2083 bytes --]
On Sun, Sep 13, 2015 at 1:32 AM, Ingo Molnar <mingo@kernel.org> wrote:
>
> * John Stultz <john.stultz@linaro.org> wrote:
>
>> The internal clocksteering done for fine-grained error correction
>> uses a logarithmic approximation, so any time adjtimex() adjusts
>> the clock steering, timekeeping_freqadjust() quickly approximates
>> the correct clock frequency over a series of ticks.
>>
>> Unfortunately, the logic in timekeeping_freqadjust(), introduced
>> in commit dc491596f639438 (Rework frequency adjustments to work
>> better w/ nohz), used the abs() function with a s64 error value
>> to calculate the size of the approximated adjustment to be made.
>>
>> Per include/linux/kernel.h: "abs() should not be used for 64-bit
>> types (s64, u64, long long) - use abs64()".
>>
>> Thus on 32-bit platforms, this resulted in the clocksteering to
>> take a quite dampended random walk trying to converge on the
>> proper frequency, which caused the adjustments to be made much
>> slower then intended (most easily observed when large adjustments
>> are made).
>>
>> This patch fixes the issue by using abs64() instead.
>
>> /* Sort out the magnitude of the correction */
>> - tick_error = abs(tick_error);
>> + tick_error = abs64(tick_error);
>
> Ugh, and we had this bug for almost two years!
Well. I sat on the patch for quite awhile, so the author date isn't
really representative. It was only included in mainline in 3.17, so
its been in use for a little over a year. But yea, still.
> Would it be possible to make abs() warn about 64-bit types during build time,
> to prevent such mishaps?
Yea. I was surprised this wasn't something the compiler would catch previously.
So is BUILD_BUG_ON() the best option for this? Its catching a whole
bunch of other related issues (frighteningly, more then Joe's cocci
script). The down-side is BUILD_BUG_ON causes build errors, not
warnings, and its output isn't super easy to parse on first view.
Potential BUILD_BUG_ON patch attached. I'll also try to spin some
patches to fix the issues this one catches.
thanks
-john
[-- Attachment #2: abs-build-bug.patch --]
[-- Type: text/x-patch, Size: 687 bytes --]
diff --git a/include/linux/kernel.h b/include/linux/kernel.h
index 5582410..753be99 100644
--- a/include/linux/kernel.h
+++ b/include/linux/kernel.h
@@ -8,6 +8,7 @@
#include <linux/types.h>
#include <linux/compiler.h>
#include <linux/bitops.h>
+#include <linux/bug.h>
#include <linux/log2.h>
#include <linux/typecheck.h>
#include <linux/printk.h>
@@ -208,6 +209,9 @@ extern int _cond_resched(void);
*/
#define abs(x) ({ \
long ret; \
+ BUILD_BUG_ON_MSG( \
+ sizeof(typeof(x)) > sizeof(long), \
+ "abs() should not be used for 64-bit types - use abs64()");\
if (sizeof(x) == sizeof(long)) { \
long __x = (x); \
ret = (__x < 0) ? -__x : __x; \
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [tip:timers/urgent] time: Fix timekeeping_freqadjust()' s incorrect use of abs() instead of abs64()
[not found] ` <tip-2619d7e9c92d524cb155ec89fd72875321512e5b@git.kernel.org>
@ 2015-10-05 15:10 ` Nuno Gonçalves
2015-10-06 8:05 ` Greg KH
0 siblings, 1 reply; 5+ messages in thread
From: Nuno Gonçalves @ 2015-10-05 15:10 UTC (permalink / raw)
To: stable
Cc: linux-tip-commits, mingo, hpa, john.stultz, mlichvar,
richardcochran, peterz, tglx, linux-kernel, prarit, torvalds
On Sun, Sep 13, 2015 at 12:07 PM, tip-bot for John Stultz
<tipbot@zytor.com> wrote:
> Commit-ID: 2619d7e9c92d524cb155ec89fd72875321512e5b
> Gitweb: http://git.kernel.org/tip/2619d7e9c92d524cb155ec89fd72875321512e5b
> Author: John Stultz <john.stultz@linaro.org>
> AuthorDate: Wed, 9 Sep 2015 16:07:30 -0700
> Committer: Ingo Molnar <mingo@kernel.org>
> CommitDate: Sun, 13 Sep 2015 10:30:47 +0200
>
> time: Fix timekeeping_freqadjust()'s incorrect use of abs() instead of abs64()
>
> The internal clocksteering done for fine-grained error
> correction uses a logarithmic approximation, so any time
> adjtimex() adjusts the clock steering, timekeeping_freqadjust()
> quickly approximates the correct clock frequency over a series
> of ticks.
>
> Unfortunately, the logic in timekeeping_freqadjust(), introduced
> in commit:
>
> dc491596f639 ("timekeeping: Rework frequency adjustments to work better w/ nohz")
>
> used the abs() function with a s64 error value to calculate the
> size of the approximated adjustment to be made.
>
> Per include/linux/kernel.h:
>
> "abs() should not be used for 64-bit types (s64, u64, long long) - use abs64()".
>
> Thus on 32-bit platforms, this resulted in the clocksteering to
> take a quite dampended random walk trying to converge on the
> proper frequency, which caused the adjustments to be made much
> slower then intended (most easily observed when large
> adjustments are made).
>
> This patch fixes the issue by using abs64() instead.
>
> Reported-by: Nuno Gonçalves <nunojpg@gmail.com>
> Tested-by: Nuno Goncalves <nunojpg@gmail.com>
> Signed-off-by: John Stultz <john.stultz@linaro.org>
> Cc: <stable@vger.kernel.org> # v3.17+
> Cc: Linus Torvalds <torvalds@linux-foundation.org>
> Cc: Miroslav Lichvar <mlichvar@redhat.com>
> Cc: Peter Zijlstra <peterz@infradead.org>
> Cc: Prarit Bhargava <prarit@redhat.com>
> Cc: Richard Cochran <richardcochran@gmail.com>
> Cc: Thomas Gleixner <tglx@linutronix.de>
> Link: http://lkml.kernel.org/r/1441840051-20244-1-git-send-email-john.stultz@linaro.org
> Signed-off-by: Ingo Molnar <mingo@kernel.org>
> ---
> kernel/time/timekeeping.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c
> index f6ee2e6..3739ac6 100644
> --- a/kernel/time/timekeeping.c
> +++ b/kernel/time/timekeeping.c
> @@ -1614,7 +1614,7 @@ static __always_inline void timekeeping_freqadjust(struct timekeeper *tk,
> negative = (tick_error < 0);
>
> /* Sort out the magnitude of the correction */
> - tick_error = abs(tick_error);
> + tick_error = abs64(tick_error);
> for (adj = 0; tick_error > interval; adj++)
> tick_error >>= 1;
>
I think this got lost on its way to the linux-stable queue (or I don't
understand the stable rules).
Thanks,
Nuno
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [tip:timers/urgent] time: Fix timekeeping_freqadjust()' s incorrect use of abs() instead of abs64()
2015-10-05 15:10 ` [tip:timers/urgent] time: Fix timekeeping_freqadjust()' s " Nuno Gonçalves
@ 2015-10-06 8:05 ` Greg KH
0 siblings, 0 replies; 5+ messages in thread
From: Greg KH @ 2015-10-06 8:05 UTC (permalink / raw)
To: Nuno Gonçalves
Cc: stable, linux-tip-commits, mingo, hpa, john.stultz, mlichvar,
richardcochran, peterz, tglx, linux-kernel, prarit, torvalds
On Mon, Oct 05, 2015 at 04:10:28PM +0100, Nuno Gon�alves wrote:
> On Sun, Sep 13, 2015 at 12:07 PM, tip-bot for John Stultz
> <tipbot@zytor.com> wrote:
> > Commit-ID: 2619d7e9c92d524cb155ec89fd72875321512e5b
> > Gitweb: http://git.kernel.org/tip/2619d7e9c92d524cb155ec89fd72875321512e5b
> > Author: John Stultz <john.stultz@linaro.org>
> > AuthorDate: Wed, 9 Sep 2015 16:07:30 -0700
> > Committer: Ingo Molnar <mingo@kernel.org>
> > CommitDate: Sun, 13 Sep 2015 10:30:47 +0200
> >
> > time: Fix timekeeping_freqadjust()'s incorrect use of abs() instead of abs64()
> >
> > The internal clocksteering done for fine-grained error
> > correction uses a logarithmic approximation, so any time
> > adjtimex() adjusts the clock steering, timekeeping_freqadjust()
> > quickly approximates the correct clock frequency over a series
> > of ticks.
> >
> > Unfortunately, the logic in timekeeping_freqadjust(), introduced
> > in commit:
> >
> > dc491596f639 ("timekeeping: Rework frequency adjustments to work better w/ nohz")
> >
> > used the abs() function with a s64 error value to calculate the
> > size of the approximated adjustment to be made.
> >
> > Per include/linux/kernel.h:
> >
> > "abs() should not be used for 64-bit types (s64, u64, long long) - use abs64()".
> >
> > Thus on 32-bit platforms, this resulted in the clocksteering to
> > take a quite dampended random walk trying to converge on the
> > proper frequency, which caused the adjustments to be made much
> > slower then intended (most easily observed when large
> > adjustments are made).
> >
> > This patch fixes the issue by using abs64() instead.
> >
> > Reported-by: Nuno Gon�alves <nunojpg@gmail.com>
> > Tested-by: Nuno Goncalves <nunojpg@gmail.com>
> > Signed-off-by: John Stultz <john.stultz@linaro.org>
> > Cc: <stable@vger.kernel.org> # v3.17+
> > Cc: Linus Torvalds <torvalds@linux-foundation.org>
> > Cc: Miroslav Lichvar <mlichvar@redhat.com>
> > Cc: Peter Zijlstra <peterz@infradead.org>
> > Cc: Prarit Bhargava <prarit@redhat.com>
> > Cc: Richard Cochran <richardcochran@gmail.com>
> > Cc: Thomas Gleixner <tglx@linutronix.de>
> > Link: http://lkml.kernel.org/r/1441840051-20244-1-git-send-email-john.stultz@linaro.org
> > Signed-off-by: Ingo Molnar <mingo@kernel.org>
> > ---
> > kernel/time/timekeeping.c | 2 +-
> > 1 file changed, 1 insertion(+), 1 deletion(-)
> >
> > diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c
> > index f6ee2e6..3739ac6 100644
> > --- a/kernel/time/timekeeping.c
> > +++ b/kernel/time/timekeeping.c
> > @@ -1614,7 +1614,7 @@ static __always_inline void timekeeping_freqadjust(struct timekeeper *tk,
> > negative = (tick_error < 0);
> >
> > /* Sort out the magnitude of the correction */
> > - tick_error = abs(tick_error);
> > + tick_error = abs64(tick_error);
> > for (adj = 0; tick_error > interval; adj++)
> > tick_error >>= 1;
> >
>
> I think this got lost on its way to the linux-stable queue (or I don't
> understand the stable rules).
It's still in the "todo" queue, along with 159 other patches that I have
yet to get to :(
thanks,
greg k-h
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2015-10-06 8:06 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-09-09 23:07 [PATCH 1/2] time: Fix timekeeping_freqadjust()'s incorrect use of abs() instead of abs64() John Stultz
2015-09-13 8:32 ` Ingo Molnar
2015-09-14 23:24 ` John Stultz
[not found] ` <tip-2619d7e9c92d524cb155ec89fd72875321512e5b@git.kernel.org>
2015-10-05 15:10 ` [tip:timers/urgent] time: Fix timekeeping_freqadjust()' s " Nuno Gonçalves
2015-10-06 8:05 ` Greg KH
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).