linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Linus Torvalds <torvalds@linux-foundation.org>
To: Ingo Molnar <mingo@elte.hu>
Cc: linux-kernel@vger.kernel.org,
	Andrew Morton <akpm@linux-foundation.org>,
	Thomas Gleixner <tglx@linutronix.de>
Subject: Re: [git pull] timer fix
Date: Wed, 4 Feb 2009 14:11:55 -0800 (PST)	[thread overview]
Message-ID: <alpine.LFD.2.00.0902041400100.3247@localhost.localdomain> (raw)
In-Reply-To: <20090204192551.GA19539@elte.hu>



On Wed, 4 Feb 2009, Ingo Molnar wrote:
>
> Pavel Emelyanov (1):
>       x86: fix hpet timer reinit for x86_64
> 
> 
>  arch/x86/kernel/hpet.c |    2 +-
>  1 files changed, 1 insertions(+), 1 deletions(-)
> 
> diff --git a/arch/x86/kernel/hpet.c b/arch/x86/kernel/hpet.c
> index 64d5ad0..ec319d1 100644
> --- a/arch/x86/kernel/hpet.c
> +++ b/arch/x86/kernel/hpet.c
> @@ -1075,7 +1075,7 @@ static void hpet_rtc_timer_reinit(void)
>  		hpet_t1_cmp += delta;
>  		hpet_writel(hpet_t1_cmp, HPET_T1_CMP);
>  		lost_ints++;
> -	} while ((long)(hpet_readl(HPET_COUNTER) - hpet_t1_cmp) > 0);
> +	} while ((long)(hpet_readl(HPET_COUNTER) - (u32)hpet_t1_cmp) > 0);

This is bordering on not being correct.

It may happen to _work_, but the fact is, you want a 32-bit signed 
compare, not a 64-bit subtract that just happens to work. So the proper 
fix is to just make it do

	} while ((s32)(hpet_readl(HPET_COUNTER) - hpet_t1_cmp) > 0);

Otherwise you always end up depending on very subtle internal logic, and 
the exact types of the things involved.

In particular, think about when HPET_COUNTER or hpet_t1_cmp overflows in 
32 bits, and what you want to happen. If you do the subtract add test in 
64 bits, it will simply do the wrong thing. Think what happens if 
hpet_t1_cmp is actually _larger_ than HPET_COUNTER, but overflowed in 32 
bits, and you're now looking at:

	(long) (0xffffffff - 0x00000001)

which is actually > 0, so the thing will continue to loop INCORRECTLY. It 
should have stopped (and _would_ have stopped on 32-bit x86).

In contrast, look at what happens if you do the subtracting (or at least 
test the _result_ of the subtract) in the right size:

	(s32) (0xffffffff - 0x00000001) 

which becomes -2, which is not larger than 0, which means that we exit 
(which is correct, because the comparator value is actually ahead of the 
current count: 0x00000001 is _ahead_ of 0xffffffff, even if it's smaller 
in an "unsigned long".

So I'm not going to pull it. This cast is simply wrong.

Either cast the result of the subtract to "s32" (or "int", whatever), or 
cast _both_ of them to (s32) so that the subtract is done in a signed 
type, and then the expansion to (long) will still be right - but 
unnecessary - in the sign.

			Linus

  reply	other threads:[~2009-02-04 22:12 UTC|newest]

Thread overview: 59+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-02-04 19:25 [git pull] timer fix Ingo Molnar
2009-02-04 22:11 ` Linus Torvalds [this message]
2009-02-04 22:16   ` Linus Torvalds
2009-02-04 22:25   ` Ingo Molnar
2009-02-04 22:58   ` Ingo Molnar
2009-02-04 23:13     ` H. Peter Anvin
2009-02-05  0:04       ` Ingo Molnar
2009-02-05  7:51     ` Kirill Korotaev
2009-02-05  9:58       ` Pavel Emelyanov
2009-02-05 14:30         ` Ingo Molnar
2009-02-05 16:04         ` Ray Lee
2009-02-17 16:38 Ingo Molnar
2009-06-20 16:55 [GIT PULL] " Ingo Molnar
2009-08-04 19:04 Ingo Molnar
2009-08-09 16:09 Ingo Molnar
2009-09-26 12:27 Ingo Molnar
2009-10-02 12:38 Ingo Molnar
2010-01-31 17:26 Ingo Molnar
2011-02-15 17:06 Ingo Molnar
2011-02-28 17:39 Ingo Molnar
2011-04-29 18:11 Ingo Molnar
2011-10-17  1:39 Linux 3.1-rc9 Linus Torvalds
2011-10-17 10:34 ` Peter Zijlstra
2011-10-17 14:57   ` Linus Torvalds
2011-10-17 17:54     ` Peter Zijlstra
2011-10-17 18:31       ` Linus Torvalds
2011-10-17 19:23         ` Peter Zijlstra
2011-10-17 21:00           ` Thomas Gleixner
2011-10-18  8:39             ` Thomas Gleixner
2011-10-18  9:05               ` Peter Zijlstra
2011-10-18 14:59                 ` Linus Torvalds
2011-10-18 18:14                   ` [GIT PULL] timer fix Ingo Molnar
2013-09-18 16:22 Ingo Molnar
2013-10-26 12:27 Ingo Molnar
2014-01-15 18:27 Ingo Molnar
2014-03-29 18:44 Ingo Molnar
2015-02-06 18:38 Ingo Molnar
2015-07-18  3:06 Ingo Molnar
2015-08-14  7:13 Ingo Molnar
2016-04-23 11:34 Ingo Molnar
2016-07-13 12:58 Ingo Molnar
2016-10-18 11:18 Ingo Molnar
2016-12-23 22:53 Ingo Molnar
2017-01-18  9:37 Ingo Molnar
2017-05-12  7:35 Ingo Molnar
2017-07-21 10:21 Ingo Molnar
2017-08-26  7:17 Ingo Molnar
2017-09-24 11:25 Ingo Molnar
2018-03-25  9:00 Ingo Molnar
2018-12-21 12:34 Ingo Molnar
2018-12-21 19:30 ` pr-tracker-bot
2018-12-23 19:29 ` Heiko Carstens
2019-01-17  9:51   ` Ingo Molnar
2019-01-17 15:58     ` Heiko Carstens
2019-01-17 16:57       ` Thomas Gleixner
2019-04-12 13:09 Ingo Molnar
2019-04-13  4:05 ` pr-tracker-bot
2019-09-26 20:18 Ingo Molnar
2019-09-26 23:00 ` pr-tracker-bot
2019-10-02 22:06 Ingo Molnar
2019-10-02 23:00 ` pr-tracker-bot
2019-11-16 21:38 Ingo Molnar
2019-11-17  0:35 ` pr-tracker-bot
2020-04-25 10:16 Ingo Molnar
2020-04-25 19:30 ` pr-tracker-bot
2020-06-28 18:39 Ingo Molnar
2020-06-28 22:05 ` pr-tracker-bot
2024-05-10 11:12 Ingo Molnar
2024-05-10 17:29 ` pr-tracker-bot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=alpine.LFD.2.00.0902041400100.3247@localhost.localdomain \
    --to=torvalds@linux-foundation.org \
    --cc=akpm@linux-foundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@elte.hu \
    --cc=tglx@linutronix.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).