Skip to contentSkip to conversation

timer fix

GIT PULL Ingo Molnar 6 participants 11 messages Feb 4, 2009 – Feb 5, 2009

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

Linus,

Please pull the latest timers-fixes-for-linus git tree from:

git://git.kernel.org/pub/scm/linux/kernel/git/tip/linux-2.6-tip.git timers-fixes-for-linus

Thanks,

Ingo

------------------>
Pavel Emelyanov (1):
x86: fix hpet timer reinit for x86_64

 arch/x86/kernel/hpet.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
arch/x86/kernel/hpet.c +1 -1
index 64d5ad0..ec319d1 100644
@@ -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);
 
 	if (lost_ints) {
 		if (hpet_rtc_flags & RTC_PIE)
#1 lore raw
Quoted 13 lines · Ingo Molnar

Pavel Emelyanov (1):
x86: fix hpet timer reinit for x86_64

 arch/x86/kernel/hpet.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
arch/x86/kernel/hpet.c +0 -0
index 64d5ad0..ec319d1 100644

arch/x86/kernel/hpet.c static void hpet_rtc_timer_reinit(void) · line 1078

Quoted 5 lines
arch/x86/kernel/hpet.c +1 -1
@@ -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

#2 lore raw
Quoted 5 lines · Linus Torvalds

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.

Btw, doing it with a nice helper macro or function is also perhaps a good idea, at least if these "compare hpet values" things happen more than once.

Look at "time_after()" in <linux/jiffies.h> to see how to do these kinds of "comparisons of things that may overflow" really carefully. You absolutely need to do the compare in a size that is no larger than the size of the actual values (and in the case of HPET, it's 32-bit, at least the way we do things now - I guess HPET's _could_ be 64-bit, but we don't read more than 32 bits or whatever).

So <linux/jiffies.h> does the cast to "(long)", but it does so because the incoming values really have type "unsigned long" and are valid in all bits.

Linus

#3 lore raw
In reply to Linus Torvalds
Quoted 9 lines · Linus Torvalds
Quoted 8 lines · Ingo Molnar

Pavel Emelyanov (1):
x86: fix hpet timer reinit for x86_64

 arch/x86/kernel/hpet.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
Quoted 12 lines

arch/x86/kernel/hpet.c static void hpet_rtc_timer_reinit(void) · line 1078

Quoted 10 lines
arch/x86/kernel/hpet.c +1 -1
index 64d5ad0..ec319d1 100644
@@ -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.

yeah, i had to look twice. The only reason i left it that way was because i couldnt reproduce the problem and hpet is hellishly fragile and this patch was tested so i chickened out.

OTOH that fragility is partly because such constructs have piled up so you very much have a valid point ...

We'll clean this up. I've already added the clean 32-bit casts - which also has another advantage: it does not actually trust the hw to always return 32-bit values - it explicitly cuts to 32 bits and does signed arithmetics on that. Will also do the helper function cleanup to abstract the counter arithmetics away.

Quoted 10 lines

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

yeah, allowing that to happen is just wrong.

Ingo

#4 lore raw
In reply to Linus Torvalds
Quoted 1 line · Linus Torvalds
	} while ((s32)(hpet_readl(HPET_COUNTER) - hpet_t1_cmp) > 0);

that is also more efficient code by 6 bytes:

before:

ffffffff81020856:	ff c3                	inc    %ebx
ffffffff81020858:	48 05 f0 00 00 00    	add    $0xf0,%rax
ffffffff8102085e:	8b 00                	mov    (%rax),%eax
ffffffff81020860:	8b 15 2a 79 85 00    	mov    0x85792a(%rip),%edx        # ffffffff81878190 <hpet_t1_cmp>
ffffffff81020866:	89 c0                	mov    %eax,%eax
ffffffff81020868:	48 29 d0             	sub    %rdx,%rax
ffffffff8102086b:	48 85 c0             	test   %rax,%rax
ffffffff8102086e:	7f bf                	jg     ffffffff8102082f <hpet_rtc_interrupt+0x68>
ffffffff81020870:	85 db                	test   %ebx,%ebx

after:

ffffffff81020856:	ff c3                	inc    %ebx
ffffffff81020858:	48 05 f0 00 00 00    	add    $0xf0,%rax
ffffffff8102085e:	8b 00                	mov    (%rax),%eax
ffffffff81020860:	2b 05 2a 79 85 00    	sub    0x85792a(%rip),%eax        # ffffffff81878190 <hpet_t1_cmp>
ffffffff81020866:	85 c0                	test   %eax,%eax
ffffffff81020868:	7f c5                	jg     ffffffff8102082f <hpet_rtc_interrupt+0x68>
ffffffff8102086a:	85 db                	test   %ebx,%ebx

Kirill, Pavel, could you please re-test the updated commit attached below?

Ingo

---------------->

Quoted 1 line

From 66a36a1e95fe9de9c6a56f0bcd01f4ba21929f86 Mon Sep 17 00:00:00 2001

From: Pavel Emelyanov <[email protected]>
Date: Wed, 4 Feb 2009 13:40:31 +0300
Subject: [PATCH] x86: fix hpet timer reinit for x86_64

There's a small problem with hpet_rtc_reinit function - it checks for the:

hpet_readl(HPET_COUNTER) - hpet_t1_cmp > 0

to continue increasing both the HPET_T1_CMP (register) and the hpet_t1_cmp (variable).

But since the HPET_COUNTER is always 32-bit, if the hpet_t1_cmp is 64-bit this condition will always be FALSE once the latter hits the 32-bit boundary, and we can have a situation, when we don't increase the HPET_T1_CMP register high enough.

The result - timer stops ticking, since HPET_T1_CMP becomes less, than the COUNTER and never increased again.

The solution is (based on Linus's suggestion) to compare 64-bits (on 64-bit x86), but to do the comparison on 32-bit signed integers.

---
 arch/x86/kernel/hpet.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
arch/x86/kernel/hpet.c +1 -1
index 64d5ad0..c761f91 100644
@@ -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 ((s32)(hpet_readl(HPET_COUNTER) - hpet_t1_cmp) > 0);
 
 	if (lost_ints) {
 		if (hpet_rtc_flags & RTC_PIE)
#5 lore raw
Quoted 2 lines · Ingo Molnar

The solution is (based on Linus's suggestion) to compare 64-bits

^not, presumably

Quoted 2 lines

(on 64-bit x86), but to do the comparison on 32-bit signed integers.

-hpa

#6 lore raw
Quoted 6 lines · H. Peter Anvin
Quoted 2 lines · Ingo Molnar

The solution is (based on Linus's suggestion) to compare 64-bits

^not, presumably

Quoted 2 lines

(on 64-bit x86), but to do the comparison on 32-bit signed integers.

fixed, thanks!

Ingo

#7 lore raw
In reply to Ingo Molnar

ACK, it works. Why not save another 4 bytes of .bss then by changing hept_t1_cmp to u32? ;-)

Kirill

Quoted 84 lines · Ingo Molnar
Quoted 1 line · Linus Torvalds
      } while ((s32)(hpet_readl(HPET_COUNTER) - hpet_t1_cmp) > 0);

that is also more efficient code by 6 bytes:

before:

ffffffff81020856:       ff c3                   inc    %ebx
ffffffff81020858:       48 05 f0 00 00 00       add    $0xf0,%rax
ffffffff8102085e:       8b 00                   mov    (%rax),%eax
ffffffff81020860:       8b 15 2a 79 85 00       mov    0x85792a(%rip),%edx
# ffffffff81878190 <hpet_t1_cmp>
ffffffff81020866:       89 c0                   mov    %eax,%eax
ffffffff81020868:       48 29 d0                sub    %rdx,%rax
ffffffff8102086b:       48 85 c0                test   %rax,%rax
ffffffff8102086e:       7f bf                   jg     ffffffff8102082f
<hpet_rtc_interrupt+0x68>
ffffffff81020870:       85 db                   test   %ebx,%ebx

after:

ffffffff81020856:       ff c3                   inc    %ebx
ffffffff81020858:       48 05 f0 00 00 00       add    $0xf0,%rax
ffffffff8102085e:       8b 00                   mov    (%rax),%eax
ffffffff81020860:       2b 05 2a 79 85 00       sub    0x85792a(%rip),%eax
# ffffffff81878190 <hpet_t1_cmp>
ffffffff81020866:       85 c0                   test   %eax,%eax
ffffffff81020868:       7f c5                   jg     ffffffff8102082f
<hpet_rtc_interrupt+0x68>
ffffffff8102086a:       85 db                   test   %ebx,%ebx

Kirill, Pavel, could you please re-test the updated commit attached below?

Ingo

---------------->
From 66a36a1e95fe9de9c6a56f0bcd01f4ba21929f86 Mon Sep 17 00:00:00 2001
From: Pavel Emelyanov <[email protected]>
Date: Wed, 4 Feb 2009 13:40:31 +0300
Subject: [PATCH] x86: fix hpet timer reinit for x86_64

There's a small problem with hpet_rtc_reinit function - it checks for the:

hpet_readl(HPET_COUNTER) - hpet_t1_cmp > 0

to continue increasing both the HPET_T1_CMP (register) and the hpet_t1_cmp (variable).

But since the HPET_COUNTER is always 32-bit, if the hpet_t1_cmp is 64-bit this condition will always be FALSE once the latter hits the 32-bit boundary, and we can have a situation, when we don't increase the HPET_T1_CMP register high enough.

The result - timer stops ticking, since HPET_T1_CMP becomes less, than the COUNTER and never increased again.

The solution is (based on Linus's suggestion) to compare 64-bits (on 64-bit x86), but to do the comparison on 32-bit signed integers.

---
 arch/x86/kernel/hpet.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
arch/x86/kernel/hpet.c +1 -1
index 64d5ad0..c761f91 100644
@@ -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 ((s32)(hpet_readl(HPET_COUNTER) - hpet_t1_cmp) > 0);
 
        if (lost_ints) {
                if (hpet_rtc_flags & RTC_PIE)
#8 lore raw
Quoted 2 lines · Kirill Korotaev

ACK, it works. Why not save another 4 bytes of .bss then by changing hept_t1_cmp to u32? ;-)

Hm... .bss you say? :) The bloat-o-meter results would be:

For the original fix with casting hpet_t1_cmp to u32 inside the loop add/remove: 0/0 grow/shrink: 1/0 up/down: 2/0 (2)

function                                     old     new   delta
hpet_rtc_interrupt                           741     743      +2

For the fix with casting the substitution result to s32 add/remove: 0/0 grow/shrink: 0/1 up/down: 0/-2 (-2)

function                                     old     new   delta
hpet_rtc_interrupt                           741     739      -2

For the proposed by Kirill type change of the hpet_t1_cmp add/remove: 0/0 grow/shrink: 0/3 up/down: 0/-6 (-6)

function                                     old     new   delta
hpet_rtc_timer_init                          186     185      -1
hpet_rtc_interrupt                           741     740      -1

hpet_t1_cmp 8 4 -4

That's the fix:

arch/x86/kernel/hpet.c +1 -1
index 64d5ad0..dfbbb94 100644
@@ -897,7 +897,7 @@ static unsigned long hpet_rtc_flags;
 static int hpet_prev_update_sec;
 static struct rtc_time hpet_alarm_time;
 static unsigned long hpet_pie_count;
-static unsigned long hpet_t1_cmp;
+static u32 hpet_t1_cmp;
 static unsigned long hpet_default_delta;
 static unsigned long hpet_pie_delta;
 static unsigned long hpet_pie_limit;

This one also works. Should I re-send this patch in a proper way?

Quoted 90 lines

Kirill

Quoted 82 lines · Ingo Molnar
Quoted 1 line · Linus Torvalds
      } while ((s32)(hpet_readl(HPET_COUNTER) - hpet_t1_cmp) > 0);

that is also more efficient code by 6 bytes:

before:

ffffffff81020856:       ff c3                   inc    %ebx
ffffffff81020858:       48 05 f0 00 00 00       add    $0xf0,%rax
ffffffff8102085e:       8b 00                   mov    (%rax),%eax
ffffffff81020860:       8b 15 2a 79 85 00       mov    0x85792a(%rip),%edx
# ffffffff81878190 <hpet_t1_cmp>
ffffffff81020866:       89 c0                   mov    %eax,%eax
ffffffff81020868:       48 29 d0                sub    %rdx,%rax
ffffffff8102086b:       48 85 c0                test   %rax,%rax
ffffffff8102086e:       7f bf                   jg     ffffffff8102082f
<hpet_rtc_interrupt+0x68>
ffffffff81020870:       85 db                   test   %ebx,%ebx

after:

ffffffff81020856:       ff c3                   inc    %ebx
ffffffff81020858:       48 05 f0 00 00 00       add    $0xf0,%rax
ffffffff8102085e:       8b 00                   mov    (%rax),%eax
ffffffff81020860:       2b 05 2a 79 85 00       sub    0x85792a(%rip),%eax
# ffffffff81878190 <hpet_t1_cmp>
ffffffff81020866:       85 c0                   test   %eax,%eax
ffffffff81020868:       7f c5                   jg     ffffffff8102082f
<hpet_rtc_interrupt+0x68>
ffffffff8102086a:       85 db                   test   %ebx,%ebx

Kirill, Pavel, could you please re-test the updated commit attached below?

Ingo

---------------->
From 66a36a1e95fe9de9c6a56f0bcd01f4ba21929f86 Mon Sep 17 00:00:00 2001
From: Pavel Emelyanov <[email protected]>
Date: Wed, 4 Feb 2009 13:40:31 +0300
Subject: [PATCH] x86: fix hpet timer reinit for x86_64

There's a small problem with hpet_rtc_reinit function - it checks for the:

hpet_readl(HPET_COUNTER) - hpet_t1_cmp > 0

to continue increasing both the HPET_T1_CMP (register) and the hpet_t1_cmp (variable).

But since the HPET_COUNTER is always 32-bit, if the hpet_t1_cmp is 64-bit this condition will always be FALSE once the latter hits the 32-bit boundary, and we can have a situation, when we don't increase the HPET_T1_CMP register high enough.

The result - timer stops ticking, since HPET_T1_CMP becomes less, than the COUNTER and never increased again.

The solution is (based on Linus's suggestion) to compare 64-bits (on 64-bit x86), but to do the comparison on 32-bit signed integers.

---
 arch/x86/kernel/hpet.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
arch/x86/kernel/hpet.c +1 -1
index 64d5ad0..c761f91 100644
@@ -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 ((s32)(hpet_readl(HPET_COUNTER) - hpet_t1_cmp) > 0);
 
        if (lost_ints) {
                if (hpet_rtc_flags & RTC_PIE)
#10 lore raw
In reply to Pavel Emelyanov
Quoted 39 lines · Pavel Emelyanov
Quoted 2 lines · Kirill Korotaev

ACK, it works. Why not save another 4 bytes of .bss then by changing hept_t1_cmp to u32? ;-)

Hm... .bss you say? :) The bloat-o-meter results would be:

For the original fix with casting hpet_t1_cmp to u32 inside the loop add/remove: 0/0 grow/shrink: 1/0 up/down: 2/0 (2)

function                                     old     new   delta
hpet_rtc_interrupt                           741     743      +2

For the fix with casting the substitution result to s32 add/remove: 0/0 grow/shrink: 0/1 up/down: 0/-2 (-2)

function                                     old     new   delta
hpet_rtc_interrupt                           741     739      -2

For the proposed by Kirill type change of the hpet_t1_cmp add/remove: 0/0 grow/shrink: 0/3 up/down: 0/-6 (-6)

function                                     old     new   delta
hpet_rtc_timer_init                          186     185      -1
hpet_rtc_interrupt                           741     740      -1

hpet_t1_cmp 8 4 -4

That's the fix:

arch/x86/kernel/hpet.c +1 -1
index 64d5ad0..dfbbb94 100644
@@ -897,7 +897,7 @@ static unsigned long hpet_rtc_flags;
 static int hpet_prev_update_sec;
 static struct rtc_time hpet_alarm_time;
 static unsigned long hpet_pie_count;
-static unsigned long hpet_t1_cmp;
+static u32 hpet_t1_cmp;
 static unsigned long hpet_default_delta;
 static unsigned long hpet_pie_delta;
 static unsigned long hpet_pie_limit;
Quoted 4 lines

This one also works. Should I re-send this patch in a proper way?

Ugh. This version seems too subtle for a new reader coming into this code. I'd prefer the fix be kept in the comparison site, where people already expect wraparound issues.

If you don't have time to add the helper, I'd really rather see the version with the explicit cast acting as documentation, rather than a test that magically works due to type issues.

Dunno, maybe it's just me.