2014-11-25 04:36:02

by John Stultz

[permalink] [raw]
Subject: [PATCH] time: Fix sign bug in ntp mult overflow warning

In commit 6067dc5a8c2b ("time: Avoid possible NTP adjustment mult
overflow") a new check was added to watch for adjustments that could
cause a mult overflow.

Unfortunately the check compares a signed with unsigned value and
ignored the case where the adjustment was negative, which causes
spurious warn-ons on some systems (and seems like it would result in
problematic time adjustments there as well, due to the early
return).

Thus this patch adds a check to make sure the adjustment is positive
before we check for an overflow, and resovles the issue in my
testing.

Cc: pang.xunlei <[email protected]>
Cc: Fengguang Wu <[email protected]>
Cc: Thomas Gleixner <[email protected]>
Cc: Ingo Molnar <[email protected]>
Reported-by: Fengguang Wu <[email protected]>
Debugged-by: pang.xunlei <[email protected]>
Signed-off-by: John Stultz <[email protected]>
---
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 29a7d67..2dc0646 100644
--- a/kernel/time/timekeeping.c
+++ b/kernel/time/timekeeping.c
@@ -1330,7 +1330,7 @@ static __always_inline void timekeeping_apply_adjustment(struct timekeeper *tk,
*
* XXX - TODO: Doc ntp_error calculation.
*/
- if (tk->tkr.mult + mult_adj < mult_adj) {
+ if ((mult_adj > 0) && (tk->tkr.mult + mult_adj < mult_adj)) {
/* NTP adjustment caused clocksource mult overflow */
WARN_ON_ONCE(1);
return;
--
1.9.1

Subject: [tip:timers/core] time: Fix sign bug in NTP mult overflow warning

Commit-ID: cb2aa63469f81426c7406227be70b628b42f7a05
Gitweb: http://git.kernel.org/tip/cb2aa63469f81426c7406227be70b628b42f7a05
Author: John Stultz <[email protected]>
AuthorDate: Mon, 24 Nov 2014 20:35:45 -0800
Committer: Ingo Molnar <[email protected]>
CommitDate: Tue, 25 Nov 2014 07:18:34 +0100

time: Fix sign bug in NTP mult overflow warning

In commit 6067dc5a8c2b ("time: Avoid possible NTP adjustment
mult overflow") a new check was added to watch for adjustments
that could cause a mult overflow.

Unfortunately the check compares a signed with unsigned value
and ignored the case where the adjustment was negative, which
causes spurious warn-ons on some systems (and seems like it
would result in problematic time adjustments there as well, due
to the early return).

Thus this patch adds a check to make sure the adjustment is
positive before we check for an overflow, and resovles the issue
in my testing.

Reported-by: Fengguang Wu <[email protected]>
Debugged-by: pang.xunlei <[email protected]>
Signed-off-by: John Stultz <[email protected]>
Link: http://lkml.kernel.org/r/[email protected]
Signed-off-by: Ingo Molnar <[email protected]>
---
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 29a7d67..2dc0646 100644
--- a/kernel/time/timekeeping.c
+++ b/kernel/time/timekeeping.c
@@ -1330,7 +1330,7 @@ static __always_inline void timekeeping_apply_adjustment(struct timekeeper *tk,
*
* XXX - TODO: Doc ntp_error calculation.
*/
- if (tk->tkr.mult + mult_adj < mult_adj) {
+ if ((mult_adj > 0) && (tk->tkr.mult + mult_adj < mult_adj)) {
/* NTP adjustment caused clocksource mult overflow */
WARN_ON_ONCE(1);
return;

2014-12-02 09:32:28

by Jeremiah Mahler

[permalink] [raw]
Subject: Re: [PATCH] time: Fix sign bug in ntp mult overflow warning

John,

On Mon, Nov 24, 2014 at 08:35:45PM -0800, John Stultz wrote:
> In commit 6067dc5a8c2b ("time: Avoid possible NTP adjustment mult
> overflow") a new check was added to watch for adjustments that could
> cause a mult overflow.
>
> Unfortunately the check compares a signed with unsigned value and
> ignored the case where the adjustment was negative, which causes
> spurious warn-ons on some systems (and seems like it would result in
> problematic time adjustments there as well, due to the early
> return).
>
> Thus this patch adds a check to make sure the adjustment is positive
> before we check for an overflow, and resovles the issue in my
> testing.
>
> Cc: pang.xunlei <[email protected]>
> Cc: Fengguang Wu <[email protected]>
> Cc: Thomas Gleixner <[email protected]>
> Cc: Ingo Molnar <[email protected]>
> Reported-by: Fengguang Wu <[email protected]>
> Debugged-by: pang.xunlei <[email protected]>
> Signed-off-by: John Stultz <[email protected]>
> ---
> 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 29a7d67..2dc0646 100644
> --- a/kernel/time/timekeeping.c
> +++ b/kernel/time/timekeeping.c
> @@ -1330,7 +1330,7 @@ static __always_inline void timekeeping_apply_adjustment(struct timekeeper *tk,
> *
> * XXX - TODO: Doc ntp_error calculation.
> */
> - if (tk->tkr.mult + mult_adj < mult_adj) {
> + if ((mult_adj > 0) && (tk->tkr.mult + mult_adj < mult_adj)) {
> /* NTP adjustment caused clocksource mult overflow */
> WARN_ON_ONCE(1);
> return;

This change does quiet the warning but I think it does so for the wrong
reason.

mult_adj is a signed number and tk->tkr.mult is an unsigned number.
Adding the check that (mult_adj > 0) limits the test to only positive
numbers. A positive number plus a positive number will never be less
than either of the two positive numbers. The test is always false.

--
- Jeremiah Mahler

2014-12-02 15:00:28

by Xunlei Pang

[permalink] [raw]
Subject: Re: [PATCH] time: Fix sign bug in ntp mult overflow warning

On 2 December 2014 at 17:32, Jeremiah Mahler <[email protected]> wrote:
> John,
>
> On Mon, Nov 24, 2014 at 08:35:45PM -0800, John Stultz wrote:
>> In commit 6067dc5a8c2b ("time: Avoid possible NTP adjustment mult
>> overflow") a new check was added to watch for adjustments that could
>> cause a mult overflow.
>>
>> Unfortunately the check compares a signed with unsigned value and
>> ignored the case where the adjustment was negative, which causes
>> spurious warn-ons on some systems (and seems like it would result in
>> problematic time adjustments there as well, due to the early
>> return).
>>
>> Thus this patch adds a check to make sure the adjustment is positive
>> before we check for an overflow, and resovles the issue in my
>> testing.
>>
>> Cc: pang.xunlei <[email protected]>
>> Cc: Fengguang Wu <[email protected]>
>> Cc: Thomas Gleixner <[email protected]>
>> Cc: Ingo Molnar <[email protected]>
>> Reported-by: Fengguang Wu <[email protected]>
>> Debugged-by: pang.xunlei <[email protected]>
>> Signed-off-by: John Stultz <[email protected]>
>> ---
>> 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 29a7d67..2dc0646 100644
>> --- a/kernel/time/timekeeping.c
>> +++ b/kernel/time/timekeeping.c
>> @@ -1330,7 +1330,7 @@ static __always_inline void timekeeping_apply_adjustment(struct timekeeper *tk,
>> *
>> * XXX - TODO: Doc ntp_error calculation.
>> */
>> - if (tk->tkr.mult + mult_adj < mult_adj) {
>> + if ((mult_adj > 0) && (tk->tkr.mult + mult_adj < mult_adj)) {
>> /* NTP adjustment caused clocksource mult overflow */
>> WARN_ON_ONCE(1);
>> return;
>
> This change does quiet the warning but I think it does so for the wrong
> reason.
>
> mult_adj is a signed number and tk->tkr.mult is an unsigned number.
> Adding the check that (mult_adj > 0) limits the test to only positive
> numbers. A positive number plus a positive number will never be less
> than either of the two positive numbers. The test is always false.

Hi Jeremiah,

The result of a positive number plus a positive number may overflow,
for example, u32 (0xFFFF_FFF0 + 0x20) will be 0x10,
that's what this patch is dealing with.

Thanks,
Xunlei
>
> --
> - Jeremiah Mahler

2014-12-02 21:31:09

by Jeremiah Mahler

[permalink] [raw]
Subject: Re: [PATCH] time: Fix sign bug in ntp mult overflow warning

Xunlei,

On Tue, Dec 02, 2014 at 11:00:26PM +0800, Xunlei Pang wrote:
> On 2 December 2014 at 17:32, Jeremiah Mahler <[email protected]> wrote:
> > John,
> >
> > On Mon, Nov 24, 2014 at 08:35:45PM -0800, John Stultz wrote:
> >> In commit 6067dc5a8c2b ("time: Avoid possible NTP adjustment mult
> >> @@ -1330,7 +1330,7 @@ static __always_inline void timekeeping_apply_adjustment(struct timekeeper *tk,
[...]
> >> *
> >> * XXX - TODO: Doc ntp_error calculation.
> >> */
> >> - if (tk->tkr.mult + mult_adj < mult_adj) {
> >> + if ((mult_adj > 0) && (tk->tkr.mult + mult_adj < mult_adj)) {
> >> /* NTP adjustment caused clocksource mult overflow */
> >> WARN_ON_ONCE(1);
> >> return;
> >
> > This change does quiet the warning but I think it does so for the wrong
> > reason.
> >
> > mult_adj is a signed number and tk->tkr.mult is an unsigned number.
> > Adding the check that (mult_adj > 0) limits the test to only positive
> > numbers. A positive number plus a positive number will never be less
> > than either of the two positive numbers. The test is always false.
>
> Hi Jeremiah,
>
> The result of a positive number plus a positive number may overflow,
> for example, u32 (0xFFFF_FFF0 + 0x20) will be 0x10,
> that's what this patch is dealing with.
>
> Thanks,
> Xunlei
> >
> > --
> > - Jeremiah Mahler

Oh, got it. That makes sense now. Thanks :)

--
- Jeremiah Mahler