public inbox for kvm@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] Fix almost infinite loop in APIC
@ 2009-01-08 16:36 Alexander Graf
  2009-01-09  6:34 ` Sheng Yang
  0 siblings, 1 reply; 30+ messages in thread
From: Alexander Graf @ 2009-01-08 16:36 UTC (permalink / raw)
  To: kvm; +Cc: avi, Kevin Wolf

While booting Linux in VMware ESX, I encountered a strange effect
in the in-kernel lapic implementation: time went backwards!

While this should never occur, because of that the while loop that
is done after the invalid calculations caused my host system to hang.

In order to make debugging easier, let's replace this as suggested
with a modulo function and not run into the danger of looping forever.

To replace the nice hint this bug gave me that the values are broken,
I added a printk message so people encountering this can at least
see that something is fishy.

Of course, the real issue needs to be fixed as well! I'm open to ideas
why now < last_update!

(Thanks to Kevin for his help in debugging this)

Signed-off-by: Alexander Graf <agraf@suse.de>
Signed-off-by: Kevin Wolf <kwolf@suse.de>
---
 arch/x86/kvm/lapic.c |   17 +++++++++--------
 1 files changed, 9 insertions(+), 8 deletions(-)

diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c
index afac68c..737217b 100644
--- a/arch/x86/kvm/lapic.c
+++ b/arch/x86/kvm/lapic.c
@@ -539,19 +539,20 @@ static u32 apic_get_tmcct(struct kvm_lapic *apic)
 	counter_passed = div64_u64(ktime_to_ns(passed),
 				   (APIC_BUS_CYCLE_NS * apic->timer.divide_count));
 
+	if (counter_passed > 0x7f00000000000000) {
+		/* If we're in here we probably encountered a bug! */
+		printk(KERN_INFO "LAPIC: Too high counter_passed value: "
+				 "0x%lx | 0x%lx (0x%lx) | 0x%lx (0x%lx)\n",
+				 counter_passed, ktime_to_ns(passed), passed,
+				 now.tv64, apic->timer.last_update.tv64);
+	}
+
 	if (counter_passed > tmcct) {
 		if (unlikely(!apic_lvtt_period(apic))) {
 			/* one-shot timers stick at 0 until reset */
 			tmcct = 0;
 		} else {
-			/*
-			 * periodic timers reset to APIC_TMICT when they
-			 * hit 0. The while loop simulates this happening N
-			 * times. (counter_passed %= tmcct) would also work,
-			 * but might be slower or not work on 32-bit??
-			 */
-			while (counter_passed > tmcct)
-				counter_passed -= tmcct;
+			counter_passed %= (u64)tmcct;
 			tmcct -= counter_passed;
 		}
 	} else {
-- 
1.6.0.2


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

end of thread, other threads:[~2009-01-23 17:58 UTC | newest]

Thread overview: 30+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-01-08 16:36 [PATCH] Fix almost infinite loop in APIC Alexander Graf
2009-01-09  6:34 ` Sheng Yang
2009-01-09 10:49   ` Alexander Graf
2009-01-09 12:57   ` Alexander Graf
2009-01-10 11:21     ` Sheng Yang
2009-01-11  4:55       ` Marcelo Tosatti
2009-01-13  7:47         ` Sheng Yang
2009-01-13 22:01           ` Marcelo Tosatti
2009-01-14  9:17             ` Sheng Yang
2009-01-14 17:03               ` Marcelo Tosatti
2009-01-15  7:20                 ` Sheng Yang
2009-01-16  5:01                   ` Marcelo Tosatti
2009-01-20 10:41                     ` Alexander Graf
2009-01-20 11:20                       ` Sheng Yang
2009-01-20 12:09                         ` Alexander Graf
2009-01-20 12:30                           ` Sheng Yang
2009-01-20 13:43                       ` Sheng Yang
2009-01-20 18:51                         ` Marcelo Tosatti
2009-01-21  2:40                           ` Sheng Yang
2009-01-21  4:23                             ` Marcelo Tosatti
2009-01-21  5:11                               ` Sheng Yang
2009-01-21 15:07                                 ` Marcelo Tosatti
2009-01-21 16:01                                   ` Alexander Graf
2009-01-21 16:03                                     ` Alexander Graf
2009-01-21 16:18                                   ` Alexander Graf
2009-01-21 16:55                                     ` Marcelo Tosatti
2009-01-22 13:08                                   ` Avi Kivity
2009-01-23 17:58                                     ` Alex Williamson
2009-01-10 11:25     ` Sheng Yang
2009-01-10 11:28     ` Sheng Yang

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox