fix in-kernel timer / IRQ injection races
cover fix in-kernel timer / IRQ injection races
Signature · 8 lines
-- ------------------------------------------------------------------------- This SF.net email is sponsored by the 2008 JavaOne(SM) Conference Don't miss this year's exciting event. There's still time to save $100. Use priority code J8TL2D2. http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone
PATCH 1/2 KVM: hlt emulation should take in-kernel APIC/PIT timers into account
Timers that fire between guest hlt and vcpu_block's add_wait_queue() are ignored, possibly resulting in hangs.
Also make sure that atomic_inc and waitqueue_active tests happen in the specified order, otherwise the following race is open:
CPU0 CPU1
if (waitqueue_active(wq))add_wait_queue()
if (!atomic_read(pit_timer->pending))
schedule()
atomic_inc(pit_timer->pending)Which is not an issue for the APIC timer due to migration logic.
arch/x86/kvm/i8254.c +11 -0
@@ -199,6 +199,7 @@ int __pit_timer_fn(struct kvm_kpit_statestruct kvm_kpit_timer *pt = &ps->pit_timer;atomic_inc (&pt->pending); +smp_mb__after_atomic_inc ();if (vcpu0 &&waitqueue_active (&vcpu0->wq)) { vcpu0->arch.mp_state =VCPU_MP_STATE_RUNNABLE ;wake_up_interruptible (&vcpu0->wq);
@@ -210,6 +211,16 @@ int __pit_timer_fn(struct kvm_kpit_statereturn (pt->period ==0 ?0 :1 ); } +int pit_has_pending_timer (struct kvm_vcpu *vcpu) +{ +struct kvm_pit *pit = vcpu->kvm->arch.vpit; + +if (pit && vcpu->vcpu_id ==0 ) +return atomic_read (&pit->pit_state.pit_timer.pending); + +return 0 ; +} +static enum hrtimer_restart pit_timer_fn (struct hrtimer *data) {struct kvm_kpit_state *ps;
arch/x86/kvm/irq.c +15 -0
@@ -26,6 +26,21 @@#include "i8254.h" /* +* check if there are pending timer events +* to be processed. +*/ +int kvm_cpu_has_pending_timer (struct kvm_vcpu *vcpu) +{ +int ret; + + ret =pit_has_pending_timer (vcpu); + ret |=apic_has_pending_timer (vcpu); + +return ret; +} +EXPORT_SYMBOL (kvm_cpu_has_pending_timer); + +/* * check if there is pending interrupt without * intack. */
arch/x86/kvm/irq.h +3 -0
@@ -85,4 +85,7 @@ void kvm_inject_pending_timer_irqs(strucvoid kvm_inject_apic_timer_irqs (struct kvm_vcpu *vcpu);void __kvm_migrate_apic_timer (struct kvm_vcpu *vcpu); +int pit_has_pending_timer (struct kvm_vcpu *vcpu); +int apic_has_pending_timer (struct kvm_vcpu *vcpu); +#endif
arch/x86/kvm/lapic.c +10 -0
@@ -952,6 +952,16 @@ static int __apic_timer_fn(struct kvm_lareturn result; } +int apic_has_pending_timer (struct kvm_vcpu *vcpu) +{ +struct kvm_lapic *lapic = vcpu->arch.apic; + +if (lapic) +return atomic_read (&lapic->timer.pending); + +return 0 ; +} +static int __inject_apic_timer_irq (struct kvm_lapic *apic) {int vector;
include/linux/kvm_host.h +1 -0
@@ -272,6 +272,7 @@ void kvm_arch_destroy_vm(struct kvm *kvmint kvm_cpu_get_interrupt (struct kvm_vcpu *v);int kvm_cpu_has_interrupt (struct kvm_vcpu *v); +int kvm_cpu_has_pending_timer (struct kvm_vcpu *vcpu);void kvm_vcpu_kick (struct kvm_vcpu *vcpu);static inline void kvm_guest_enter (void )
virt/kvm/kvm_main.c +3 -0
@@ -752,6 +752,7 @@ void mark_page_dirty(struct kvm *kvm, gf } } +#ifdef CONFIG_X86 /* * The vCPU has executed a HLT instruction with in-kernel mode enabled. */
@@ -765,6 +766,7 @@ void kvm_vcpu_block(struct kvm_vcpu *vcp* We will block until either an interrupt or a signal wakes us up */ while (!kvm_cpu_has_interrupt (vcpu) + && !kvm_cpu_has_pending_timer (vcpu) && !signal_pending (current) && !kvm_arch_vcpu_runnable (vcpu)) {set_current_state (TASK_INTERRUPTIBLE );
@@ -776,6 +778,7 @@ void kvm_vcpu_block(struct kvm_vcpu *vcp__set_current_state (TASK_RUNNING ); remove_wait_queue(&vcpu->wq, &wait); } +#endif void kvm_resched (struct kvm_vcpu *vcpu) {
Signature · 5 lines
------------------------------------------------------------------------- This SF.net email is sponsored by the 2008 JavaOne(SM) Conference Don't miss this year's exciting event. There's still time to save $100. Use priority code J8TL2D2. http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone
PATCH 2/2 KVM: fix kvm_vcpu_kick vs __vcpu_run race
In reply to Marcelo TosattiThere is a window open between testing of pending IRQ's and assignment of guest_mode in __vcpu_run.
Injection of IRQ's can race with __vcpu_run as follows:
CPU0 CPU1kvm_x86_ops->run()vcpu->guest_mode = 0 SET_IRQ_LINE ioctl
..kvm_x86_ops->inject_pending_irqkvm_cpu_has_interrupt()
apic_test_and_set_irr()
kvm_vcpu_kick
if (vcpu->guest_mode)
send_ipi()vcpu->guest_mode = 1
So move guest_mode=1 assignment before ->inject_pending_irq, and make sure that it won't reorder after it.
arch/x86/kvm/x86.c +9 -2
@@ -2777,6 +2777,13 @@ again: goto out; } + vcpu->guest_mode =1 ; +/* +* Make sure that guest_mode assignment won't happen after +* testing the pending IRQ vector bitmap. +*/ +smp_wmb (); +if (vcpu->arch.exception.pending)__queue_exception (vcpu);else if (irqchip_in_kernel (vcpu->kvm))
@@ -2788,7 +2795,6 @@ again:up_read (&vcpu->kvm->slots_lock); - vcpu->guest_mode =1 ;kvm_guest_enter ();if (vcpu->requests)
@@ -3944,11 +3950,12 @@ static void vcpu_kick_intr(void *info)void kvm_vcpu_kick (struct kvm_vcpu *vcpu) {int ipi_pcpu = vcpu->cpu; +int cpu =smp_processor_id ();if (waitqueue_active (&vcpu->wq)) {wake_up_interruptible (&vcpu->wq); ++vcpu->stat.halt_wakeup; } -if (vcpu->guest_mode) +if (vcpu->guest_mode && vcpu->cpu != cpu)smp_call_function_single (ipi_pcpu, vcpu_kick_intr, vcpu,0 ,0 ); }
Signature · 5 lines
------------------------------------------------------------------------- This SF.net email is sponsored by the 2008 JavaOne(SM) Conference Don't miss this year's exciting event. There's still time to save $100. Use priority code J8TL2D2. http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone
Re: [patch 1/2] KVM: hlt emulation should take in-kernel APIC/PIT timers into account
In reply to Marcelo TosattiQuoted 6 lines · Marcelo Tosatti
Timers that fire between guest hlt and vcpu_block's
add_wait_queue()are ignored, possibly resulting in hangs.Also make sure that
atomic_incandwaitqueue_activetests happen in the specified order, otherwise the following race is open:
Quoted 10 lines
CPU0 CPU1
if (waitqueue_active(wq))add_wait_queue()
if (!atomic_read(pit_timer->pending))schedule() atomic_inc(pit_timer->pending)Which is not an issue for the APIC timer due to migration logic.
Nasty. I hope we can get Dor's interrupt injection notification working, so we don't have to handle these bugs.
Quoted 13 lines
Index:
kvm/virt/kvm/kvm_main.c
===================================================================virt/kvm/kvm_main.c +1
-0@@ -752,6 +752,7 @@ void mark_page_dirty(struct kvm *kvm, gf } } +#ifdef CONFIG_X86 /* * The vCPU has executed a HLT instruction with in-kernel mode enabled. */
virt/kvm/kvm_main.c void kvm_vcpu_block(struct kvm_vcpu *vcp · line 766
Quoted 1 line
virt/kvm/kvm_main.c +0
-0@@ -765,6 +766,7 @@ void kvm_vcpu_block(struct kvm_vcpu *vcp
This breaks ia64 (and shouldn't s390 use this too?)
virt/kvm/kvm_main.c void kvm_vcpu_block(struct kvm_vcpu *vcp
Quoted 5 lines
virt/kvm/kvm_main.c +1
-0* We will block until either an interrupt or a signal wakes us up */ while (!kvm_cpu_has_interrupt (vcpu) + && !kvm_cpu_has_pending_timer(vcpu)
I guess the fix is to stub this out for the other archs.
Signature · 8 lines
Any sufficiently difficult bug is indistinguishable from a feature. ------------------------------------------------------------------------- This SF.net email is sponsored by the 2008 JavaOne(SM) Conference Don't miss this year's exciting event. There's still time to save $100. Use priority code J8TL2D2. http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone
Re: [patch 2/2] KVM: fix kvm_vcpu_kick vs __vcpu_run race
In reply to Marcelo TosattiQuoted 28 lines · Marcelo Tosatti
There is a window open between testing of pending IRQ's and assignment of
guest_modein__vcpu_run.Injection of IRQ's can race with
__vcpu_runas follows:CPU0 CPU1
kvm_x86_ops->run()vcpu->guest_mode= 0SET_IRQ_LINEioctl
..kvm_x86_ops->inject_pending_irqkvm_cpu_has_interrupt()apic_test_and_set_irr() kvm_vcpu_kick if (vcpu->guest_mode) send_ipi()
vcpu->guest_mode= 1So move guest_mode=1 assignment before ->inject_pending_irq, and make sure that it won't reorder after it.
arch/x86/kvm/x86.c +0
-0@@ -3944,11 +3950,12 @@ static void vcpu_kick_intr(void *info)void kvm_vcpu_kick (struct kvm_vcpu *vcpu) {int ipi_pcpu = vcpu->cpu;
arch/x86/kvm/x86.c static void vcpu_kick_intr(void *info) · line 3961
Quoted 10 lines
arch/x86/kvm/x86.c +2
-1+int cpu =smp_processor_id ();if (waitqueue_active (&vcpu->wq)) {wake_up_interruptible (&vcpu->wq); ++vcpu->stat.halt_wakeup; } -if (vcpu->guest_mode) +if (vcpu->guest_mode && vcpu->cpu != cpu)smp_call_function_single (ipi_pcpu, vcpu_kick_intr, vcpu,0 ,0 ); }
kvm_vcpu_kick() can be called from nonatomic contexts, so the vcpu->cpu == cpu check is dangerous (and will warn on preemptible kernels, no?)
Signature · 8 lines
Any sufficiently difficult bug is indistinguishable from a feature. ------------------------------------------------------------------------- This SF.net email is sponsored by the 2008 JavaOne(SM) Conference Don't miss this year's exciting event. There's still time to save $100. Use priority code J8TL2D2. http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone
Re: [patch 1/2] KVM: hlt emulation should take in-kernel APIC/PIT timers into account
In reply to Avi KivityQuoted 8 lines · Avi Kivity
This breaks ia64 (and shouldn't s390 use this too?)
Review
Quoted 5 lines
* We will block until either an interrupt or a signal wakes us up */ while (!kvm_cpu_has_interrupt(vcpu)(quoted diff) +1
-0+ && !kvm_cpu_has_pending_timer(vcpu)I guess the fix is to stub this out for the other archs.
Agreed. How's this.
-----------
KVM: hlt emulation should take in-kernel APIC/PIT timers into account
Timers that fire between guest hlt and vcpu_block's add_wait_queue() are ignored, possibly resulting in hangs.
Also make sure that atomic_inc and waitqueue_active tests happen in the specified order, otherwise the following race is open:
CPU0 CPU1
if (waitqueue_active(wq))add_wait_queue()
if (!atomic_read(pit_timer->pending))
schedule()
atomic_inc(pit_timer->pending)arch/ia64/kvm/kvm-ia64.c +5 -0
@@ -1778,6 +1778,11 @@ int kvm_cpu_has_interrupt(struct kvm_vcpreturn 0 ; } +int kvm_cpu_has_pending_timer (struct kvm_vcpu *vcpu) +{ +return 0 ; +} +gfn_t unalias_gfn (struct kvm *kvm,gfn_t gfn) {return gfn;
arch/s390/kvm/interrupt.c +5 -0
@@ -321,6 +321,11 @@ int kvm_cpu_has_interrupt(struct kvm_vcpreturn rc; } +int kvm_cpu_has_pending_timer (struct kvm_vcpu *vcpu) +{ +return 0 ; +} +int kvm_s390_handle_wait (struct kvm_vcpu *vcpu) {u64 now, sltime;
arch/x86/kvm/i8254.c +11 -0
@@ -199,6 +199,7 @@ int __pit_timer_fn(struct kvm_kpit_statestruct kvm_kpit_timer *pt = &ps->pit_timer;atomic_inc (&pt->pending); +smp_mb__after_atomic_inc ();if (vcpu0 &&waitqueue_active (&vcpu0->wq)) { vcpu0->arch.mp_state =VCPU_MP_STATE_RUNNABLE ;wake_up_interruptible (&vcpu0->wq);
@@ -210,6 +211,16 @@ int __pit_timer_fn(struct kvm_kpit_statereturn (pt->period ==0 ?0 :1 ); } +int pit_has_pending_timer (struct kvm_vcpu *vcpu) +{ +struct kvm_pit *pit = vcpu->kvm->arch.vpit; + +if (pit && vcpu->vcpu_id ==0 ) +return atomic_read (&pit->pit_state.pit_timer.pending); + +return 0 ; +} +static enum hrtimer_restart pit_timer_fn (struct hrtimer *data) {struct kvm_kpit_state *ps;
arch/x86/kvm/irq.c +15 -0
@@ -26,6 +26,21 @@#include "i8254.h" /* +* check if there are pending timer events +* to be processed. +*/ +int kvm_cpu_has_pending_timer (struct kvm_vcpu *vcpu) +{ +int ret; + + ret =pit_has_pending_timer (vcpu); + ret |=apic_has_pending_timer (vcpu); + +return ret; +} +EXPORT_SYMBOL (kvm_cpu_has_pending_timer); + +/* * check if there is pending interrupt without * intack. */
arch/x86/kvm/irq.h +3 -0
@@ -85,4 +85,7 @@ void kvm_inject_pending_timer_irqs(strucvoid kvm_inject_apic_timer_irqs (struct kvm_vcpu *vcpu);void __kvm_migrate_apic_timer (struct kvm_vcpu *vcpu); +int pit_has_pending_timer (struct kvm_vcpu *vcpu); +int apic_has_pending_timer (struct kvm_vcpu *vcpu); +#endif
arch/x86/kvm/lapic.c +10 -0
@@ -952,6 +952,16 @@ static int __apic_timer_fn(struct kvm_lareturn result; } +int apic_has_pending_timer (struct kvm_vcpu *vcpu) +{ +struct kvm_lapic *lapic = vcpu->arch.apic; + +if (lapic) +return atomic_read (&lapic->timer.pending); + +return 0 ; +} +static int __inject_apic_timer_irq (struct kvm_lapic *apic) {int vector;
include/linux/kvm_host.h +1 -0
@@ -272,6 +272,7 @@ void kvm_arch_destroy_vm(struct kvm *kvmint kvm_cpu_get_interrupt (struct kvm_vcpu *v);int kvm_cpu_has_interrupt (struct kvm_vcpu *v); +int kvm_cpu_has_pending_timer (struct kvm_vcpu *vcpu);void kvm_vcpu_kick (struct kvm_vcpu *vcpu);static inline void kvm_guest_enter (void )
virt/kvm/kvm_main.c +1 -0
@@ -765,6 +765,7 @@ void kvm_vcpu_block(struct kvm_vcpu *vcp* We will block until either an interrupt or a signal wakes us up */ while (!kvm_cpu_has_interrupt (vcpu) + && !kvm_cpu_has_pending_timer (vcpu) && !signal_pending (current) && !kvm_arch_vcpu_runnable (vcpu)) {set_current_state (TASK_INTERRUPTIBLE );
-------------------------------------------------------------------------
This SF.net email is sponsored by the 2008 JavaOne(SM) Conference Don't miss this year's exciting event. There's still time to save $100. Use priority code J8TL2D2.
http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone
Re: [patch 2/2] KVM: fix kvm_vcpu_kick vs __vcpu_run race
In reply to Avi KivityQuoted 2 lines · Avi Kivity
kvm_vcpu_kick()can be called from nonatomic contexts, so thevcpu->cpu== cpu check is dangerous (and will warn on preemptible kernels, no?)
Doh, right. How's this.
-----------
KVM: fix kvm_vcpu_kick vs __vcpu_run race
There is a window open between testing of pending IRQ's and assignment of guest_mode in __vcpu_run.
Injection of IRQ's can race with __vcpu_run as follows:
CPU0 CPU1kvm_x86_ops->run()vcpu->guest_mode = 0 SET_IRQ_LINE ioctl
..kvm_x86_ops->inject_pending_irqkvm_cpu_has_interrupt()
apic_test_and_set_irr()
kvm_vcpu_kick
if (vcpu->guest_mode)
send_ipi()vcpu->guest_mode = 1
So move guest_mode=1 assignment before ->inject_pending_irq, and make sure that it won't reorder after it.
arch/x86/kvm/x86.c +10 -2
@@ -2777,6 +2777,13 @@ again: goto out; } + vcpu->guest_mode =1 ; +/* +* Make sure that guest_mode assignment won't happen after +* testing the pending IRQ vector bitmap. +*/ +smp_wmb (); +if (vcpu->arch.exception.pending)__queue_exception (vcpu);else if (irqchip_in_kernel (vcpu->kvm))
@@ -2788,7 +2795,6 @@ again:up_read (&vcpu->kvm->slots_lock); - vcpu->guest_mode =1 ;kvm_guest_enter ();if (vcpu->requests)
@@ -3944,11 +3950,13 @@ static void vcpu_kick_intr(void *info)void kvm_vcpu_kick (struct kvm_vcpu *vcpu) {int ipi_pcpu = vcpu->cpu; +int cpu =get_cpu ();if (waitqueue_active (&vcpu->wq)) {wake_up_interruptible (&vcpu->wq); ++vcpu->stat.halt_wakeup; } -if (vcpu->guest_mode) +if (vcpu->guest_mode && vcpu->cpu != cpu)smp_call_function_single (ipi_pcpu, vcpu_kick_intr, vcpu,0 ,0 ); +put_cpu (); }
-------------------------------------------------------------------------
This SF.net email is sponsored by the 2008 JavaOne(SM) Conference Don't miss this year's exciting event. There's still time to save $100. Use priority code J8TL2D2.
http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone
Re: [patch 1/2] KVM: hlt emulation should take in-kernel APIC/PIT timers into account
In reply to Avi KivityQuoted 5 lines · Avi Kivity
Review void kvm_vcpu_block(struct kvm_vcpu *vcp · line 766
Quoted 2 lines
(quoted diff) +0
-0@@ -765,6 +766,7 @@ void kvm_vcpu_block(struct kvm_vcpu *vcpThis breaks ia64 (and shouldn't s390 use this too?)
Quoted 7 lines
virt/kvm/kvm_main.c void kvm_vcpu_block(struct kvm_vcpu *vcp
Quoted 5 lines
virt/kvm/kvm_main.c +1
-0* We will block until either an interrupt or a signal wakes us up */ while (!kvm_cpu_has_interrupt (vcpu) + && !kvm_cpu_has_pending_timer(vcpu)I guess the fix is to stub this out for the other archs.
We don't use that, we have our own implementation of vcpu_block.
-------------------------------------------------------------------------
This SF.net email is sponsored by the 2008 JavaOne(SM) Conference Don't miss this year's exciting event. There's still time to save $100. Use priority code J8TL2D2.
http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone
Re: [patch 1/2] KVM: hlt emulation should take in-kernel APIC/PIT timers into account
In reply to Marcelo TosattiQuoted 14 lines · Marcelo Tosatti
Quoted 10 lines · Avi Kivity
This breaks ia64 (and shouldn't s390 use this too?)
virt/kvm/kvm_main.c void kvm_vcpu_block(struct kvm_vcpu *vcp · line 768
Quoted 6 lines
* We will block until either an interrupt or a signal wakes us up */ while (!kvm_cpu_has_interrupt(vcpu)virt/kvm/kvm_main.c +1
-0+ && !kvm_cpu_has_pending_timer (vcpu)I guess the fix is to stub this out for the other archs.
Quoted 3 lines
Agreed. How's this.
Better :); applied.
Signature · 8 lines
error compiling committee.c: too many arguments to function ------------------------------------------------------------------------- This SF.net email is sponsored by the 2008 JavaOne(SM) Conference Don't miss this year's exciting event. There's still time to save $100. Use priority code J8TL2D2. http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone
Re: [patch 1/2] KVM: hlt emulation should take in-kernel APIC/PIT timers into account
In reply to Carsten OtteQuoted 12 lines · Carsten Otte
Quoted 11 lines · Avi Kivity
Review void kvm_vcpu_block(struct kvm_vcpu *vcp · line 766
Quoted 2 lines
(quoted diff) +0
-0@@ -765,6 +766,7 @@ void kvm_vcpu_block(struct kvm_vcpu *vcpThis breaks ia64 (and shouldn't s390 use this too?)
Quoted 5 lines
virt/kvm/kvm_main.c +1
-0* We will block until either an interrupt or a signal wakes us up */ while (!kvm_cpu_has_interrupt (vcpu) + && !kvm_cpu_has_pending_timer(vcpu)
Quoted 3 lines
Quoted 1 line
I guess the fix is to stub this out for the other archs.
We don't use that, we have our own implementation of
vcpu_block.
Why?
Signature · 8 lines
error compiling committee.c: too many arguments to function ------------------------------------------------------------------------- This SF.net email is sponsored by the 2008 JavaOne(SM) Conference Don't miss this year's exciting event. There's still time to save $100. Use priority code J8TL2D2. http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone
Avi Kivity
AppliedRe: [patch 2/2] KVM: fix kvm_vcpu_kick vs __vcpu_run race
In reply to Marcelo TosattiQuoted 29 lines · Marcelo Tosatti
Quoted 3 lines · Avi Kivity
kvm_vcpu_kick()can be called from nonatomic contexts, so thevcpu->cpu== cpu check is dangerous (and will warn on preemptible kernels, no?)Doh, right. How's this.
-----------
KVM: fix
kvm_vcpu_kickvs__vcpu_runraceThere is a window open between testing of pending IRQ's and assignment of
guest_modein__vcpu_run.Injection of IRQ's can race with
__vcpu_runas follows:CPU0 CPU1
kvm_x86_ops->run()vcpu->guest_mode= 0SET_IRQ_LINEioctl
..kvm_x86_ops->inject_pending_irqkvm_cpu_has_interrupt()apic_test_and_set_irr() kvm_vcpu_kick if (vcpu->guest_mode) send_ipi()
Quoted 7 lines
vcpu->guest_mode= 1So move guest_mode=1 assignment before ->inject_pending_irq, and make sure that it won't reorder after it.
Applied, but this
Quoted 6 lines
arch/x86/kvm/x86.c +1
-0@@ -3944,11 +3950,13 @@ static void vcpu_kick_intr(void *info)void kvm_vcpu_kick (struct kvm_vcpu *vcpu) {int ipi_pcpu = vcpu->cpu; +int cpu =get_cpu ();
arch/x86/kvm/x86.c static void vcpu_kick_intr(void *info) · line 3962
Quoted 10 lines
arch/x86/kvm/x86.c +2
-1if (waitqueue_active (&vcpu->wq)) {wake_up_interruptible (&vcpu->wq); ++vcpu->stat.halt_wakeup; } -if (vcpu->guest_mode) +if (vcpu->guest_mode && vcpu->cpu != cpu)smp_call_function_single (ipi_pcpu, vcpu_kick_intr, vcpu,0 ,0 ); +put_cpu (); }
Looks like a no-op now, as vcpu_kick_intr() does nothing and smp_call_function_single() won't force an exit if vcpu->cpu == cpu, so I dropped this hunk.
Signature · 8 lines
error compiling committee.c: too many arguments to function ------------------------------------------------------------------------- This SF.net email is sponsored by the 2008 JavaOne(SM) Conference Don't miss this year's exciting event. There's still time to save $100. Use priority code J8TL2D2. http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone
Re: [patch 2/2] KVM: fix kvm_vcpu_kick vs __vcpu_run race
Quoted 7 lines · Avi Kivity
Quoted 7 lines
arch/x86/kvm/x86.c +1
-0@@ -3944,11 +3950,13 @@ static void vcpu_kick_intr(void *info)void kvm_vcpu_kick (struct kvm_vcpu *vcpu) {int ipi_pcpu = vcpu->cpu; +int cpu =get_cpu ();
Quoted 15 lines
arch/x86/kvm/x86.c static void vcpu_kick_intr(void *info) · line 3962
Quoted 10 lines
arch/x86/kvm/x86.c +2
-1if (waitqueue_active (&vcpu->wq)) {wake_up_interruptible (&vcpu->wq); ++vcpu->stat.halt_wakeup; } -if (vcpu->guest_mode) +if (vcpu->guest_mode && vcpu->cpu != cpu)smp_call_function_single (ipi_pcpu, vcpu_kick_intr, vcpu,0 ,0 ); +put_cpu (); }Looks like a no-op now, as
vcpu_kick_intr()does nothing andsmp_call_function_single()won't force an exit ifvcpu->cpu== cpu, so I dropped this hunk.
Oh, I see the reason now: the irq stuff now happens with irqs disabled which annoys smp_call_function_single(). Well, I'd like to avoid all this irq-disabled processing, so I'm looking at an alternate fix using a new KVM_REQ_EVAL_IRQ.
Signature · 8 lines
error compiling committee.c: too many arguments to function ------------------------------------------------------------------------- This SF.net email is sponsored by the 2008 JavaOne(SM) Conference Don't miss this year's exciting event. There's still time to save $100. Use priority code J8TL2D2. http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone
Re: [patch 2/2] KVM: fix kvm_vcpu_kick vs __vcpu_run race
Quoted 25 lines · Avi Kivity
Quoted 7 lines · Avi Kivity
Quoted 7 lines
arch/x86/kvm/x86.c +1
-0@@ -3944,11 +3950,13 @@ static void vcpu_kick_intr(void *info)void kvm_vcpu_kick (struct kvm_vcpu *vcpu) {int ipi_pcpu = vcpu->cpu; +int cpu =get_cpu ();Quoted 16 lines
Quoted 11 lines
arch/x86/kvm/x86.c +2
-1if (waitqueue_active (&vcpu->wq)) {wake_up_interruptible (&vcpu->wq); ++vcpu->stat.halt_wakeup; } -if (vcpu->guest_mode) +if (vcpu->guest_mode && vcpu->cpu != cpu)smp_call_function_single (ipi_pcpu, vcpu_kick_intr, vcpu,0 ,0 ); +put_cpu ();}
Looks like a no-op now, as
vcpu_kick_intr()does nothing andsmp_call_function_single()won't force an exit ifvcpu->cpu== cpu, so I dropped this hunk.
Quoted 5 lines
Oh, I see the reason now: the irq stuff now happens with irqs disabled which annoys
smp_call_function_single(). Well, I'd like to avoid all this irq-disabled processing, so I'm looking at an alternate fix using a newKVM_REQ_EVAL_IRQ.
!@%$#@%, we can't move guest irq processing out of the critical section since we can't commit to irq delivery (we may have to deliver an exception instead).
I'll apply your patch as is, and will look at disentangling this later.
Signature · 8 lines
error compiling committee.c: too many arguments to function ------------------------------------------------------------------------- This SF.net email is sponsored by the 2008 JavaOne(SM) Conference Don't miss this year's exciting event. There's still time to save $100. Use priority code J8TL2D2. http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone
Re: [patch 1/2] KVM: hlt emulation should take in-kernel APIC/PIT timers into account
In reply to Avi KivityQuoted 1 line · Avi Kivity
Why?
This one does'nt work for us. Our arch defines various reasons why we would not fall asleep but do something else, and we need to check them while in atomic of a lock that other archs don't have before sleeping. See kvm_s390_handle_wait in arch/s390/kvm/interrupt.c.
-------------------------------------------------------------------------
This SF.net email is sponsored by the 2008 JavaOne(SM) Conference Don't miss this year's exciting event. There's still time to save $100. Use priority code J8TL2D2.
http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone
Re: [patch 1/2] KVM: hlt emulation should take in-kernel APIC/PIT timers into account
In reply to Avi KivityQuoted 11 lines · On Sunday 13 April 2008 17:28:22 Avi Kivity wrote:
Quoted 10 lines · Marcelo Tosatti
Quoted 8 lines · Avi Kivity
This breaks ia64 (and shouldn't s390 use this too?)
Review
Quoted 4 lines
* We will block until either an interrupt or a signal wakes us up */ while (!kvm_cpu_has_interrupt(vcpu)(quoted diff) +1
-0+ && !kvm_cpu_has_pending_timer(vcpu)I guess the fix is to stub this out for the other archs.
Quoted 3 lines
Quoted 1 line
Agreed. How's this.
Better :); applied.
Hi, Marcelo
This patch got into trouble when OS don't use PIT/LAPIC timer and don't disable them. Then the pending counters would keep increasing, but the HLT emulation can't be executed. And this would resulted in mass a lot (above 220,000 per second) halt_exit for the Windows XP that using RTC as the clocksource (and keep PIT enabled after bios did, just mask the pin) idle, and the cpu utilize would be about 100% of QEmu process.
The following patch used another way to fix the issue, though not very formal.
From 4d08ef3173084a6f0b7b76a0727e04ff42b614ba Mon Sep 17 00:00:00 2001
From: Sheng Yang <[email protected]>
Date: Fri, 9 May 2008 15:36:27 +0800
Subject: [PATCH] KVM: Fix CPU utilize hit 100% when emulate HLT in some OS
--- arch/x86/kvm/i8254.c | 2 ++ arch/x86/kvm/lapic.c | 2 ++ include/asm-x86/kvm_host.h | 2 ++ virt/kvm/kvm_main.c | 2 +- 4 files changed, 7 insertions(+), 1 deletions(-)
arch/x86/kvm/i8254.c +2 -0
@@ -199,6 +199,7 @@ static int __pit_timer_fn(struct kvm_kpit_state *ps)struct kvm_kpit_timer *pt = &ps->pit_timer;atomic_inc (&pt->pending); + vcpu0->arch.timer_pending_updated =1 ;smp_mb__after_atomic_inc ();/* FIXME: handle case where the guest is in guest mode */ if (vcpu0 &&waitqueue_active (&vcpu0->wq)) {
@@ -577,6 +578,7 @@ void kvm_inject_pit_timer_irqs(struct kvm_vcpu *vcpu)struct kvm *kvm = vcpu->kvm;struct kvm_kpit_state *ps; + kvm->vcpus[0 ]->arch.timer_pending_updated =0 ;if (vcpu && pit) { ps = &pit->pit_state;
arch/x86/kvm/lapic.c +2 -0
@@ -944,6 +944,7 @@ static int __apic_timer_fn(struct kvm_lapic *apic)wait_queue_head_t *q = &apic->vcpu->wq;atomic_inc (&apic->timer.pending); + apic->vcpu->arch.timer_pending_updated =1 ;if (waitqueue_active (q)) { apic->vcpu->arch.mp_state =KVM_MP_STATE_RUNNABLE ;wake_up_interruptible (q);
@@ -1067,6 +1068,7 @@ void kvm_inject_apic_timer_irqs(struct kvm_vcpu *vcpu) {struct kvm_lapic *apic = vcpu->arch.apic; + vcpu->arch.timer_pending_updated =0 ;if (apic &&apic_lvt_enabled (apic,APIC_LVTT ) &&atomic_read (&apic->timer.pending) >0 ) {if (__inject_apic_timer_irq (apic))
include/asm-x86/kvm_host.h +2 -0
@@ -285,6 +285,8 @@ struct kvm_vcpu_arch {struct kvm_vcpu_time_info hv_clock;unsigned int time_offset;struct page *time_page; + +bool timer_check_pending; };struct kvm_mem_alias {
virt/kvm/kvm_main.c +1 -1
@@ -791,7 +791,7 @@ void kvm_vcpu_block(struct kvm_vcpu *vcpu)* We will block until either an interrupt or a signal wakes us up */ while (!kvm_cpu_has_interrupt (vcpu) - && !kvm_cpu_has_pending_timer (vcpu) + && !vcpu->arch.timer_pending_updated && !signal_pending (current) && !kvm_arch_vcpu_runnable (vcpu)) {set_current_state (TASK_INTERRUPTIBLE );
--
1.5.5
From 4d08ef3173084a6f0b7b76a0727e04ff42b614ba Mon Sep 17 00:00:00 2001
From: Sheng Yang <[email protected]>
Date: Fri, 9 May 2008 15:36:27 +0800
Subject: [PATCH] KVM: Fix CPU utilize hit 100% when emulate HLT in some OS
--- arch/x86/kvm/i8254.c | 2 ++ arch/x86/kvm/lapic.c | 2 ++ include/asm-x86/kvm_host.h | 2 ++ virt/kvm/kvm_main.c | 2 +- 4 files changed, 7 insertions(+), 1 deletions(-)
arch/x86/kvm/i8254.c +2 -0
@@ -199,6 +199,7 @@ static int __pit_timer_fn(struct kvm_kpit_state *ps)struct kvm_kpit_timer *pt = &ps->pit_timer;atomic_inc (&pt->pending); + vcpu0->arch.timer_pending_updated =1 ;smp_mb__after_atomic_inc ();/* FIXME: handle case where the guest is in guest mode */ if (vcpu0 &&waitqueue_active (&vcpu0->wq)) {
@@ -577,6 +578,7 @@ void kvm_inject_pit_timer_irqs(struct kvm_vcpu *vcpu)struct kvm *kvm = vcpu->kvm;struct kvm_kpit_state *ps; + kvm->vcpus[0 ]->arch.timer_pending_updated =0 ;if (vcpu && pit) { ps = &pit->pit_state;
arch/x86/kvm/lapic.c +2 -0
@@ -944,6 +944,7 @@ static int __apic_timer_fn(struct kvm_lapic *apic)wait_queue_head_t *q = &apic->vcpu->wq;atomic_inc (&apic->timer.pending); + apic->vcpu->arch.timer_pending_updated =1 ;if (waitqueue_active (q)) { apic->vcpu->arch.mp_state =KVM_MP_STATE_RUNNABLE ;wake_up_interruptible (q);
@@ -1067,6 +1068,7 @@ void kvm_inject_apic_timer_irqs(struct kvm_vcpu *vcpu) {struct kvm_lapic *apic = vcpu->arch.apic; + vcpu->arch.timer_pending_updated =0 ;if (apic &&apic_lvt_enabled (apic,APIC_LVTT ) &&atomic_read (&apic->timer.pending) >0 ) {if (__inject_apic_timer_irq (apic))
include/asm-x86/kvm_host.h +2 -0
@@ -285,6 +285,8 @@ struct kvm_vcpu_arch {struct kvm_vcpu_time_info hv_clock;unsigned int time_offset;struct page *time_page; + +bool timer_check_pending; };struct kvm_mem_alias {
virt/kvm/kvm_main.c +1 -1
@@ -791,7 +791,7 @@ void kvm_vcpu_block(struct kvm_vcpu *vcpu)* We will block until either an interrupt or a signal wakes us up */ while (!kvm_cpu_has_interrupt (vcpu) - && !kvm_cpu_has_pending_timer (vcpu) + && !vcpu->arch.timer_pending_updated && !signal_pending (current) && !kvm_arch_vcpu_runnable (vcpu)) {set_current_state (TASK_INTERRUPTIBLE );
Signature · 1 line
1.5.5
-------------------------------------------------------------------------
This SF.net email is sponsored by the 2008 JavaOne(SM) Conference Don't miss this year's exciting event. There's still time to save $100. Use priority code J8TL2D2.
http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone
_______________________________________________
kvm-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/kvm-devel
Re: [patch 1/2] KVM: hlt emulation should take in-kernel APIC/PIT timers into account
Quoted 18 lines · Sheng
Quoted 11 lines · On Sunday 13 April 2008 17:28:22 Avi Kivity wrote:
Quoted 10 lines · Marcelo Tosatti
Quoted 8 lines · Avi Kivity
This breaks ia64 (and shouldn't s390 use this too?)
Review
Quoted 4 lines
* We will block until either an interrupt or a signal wakes us up */ while (!kvm_cpu_has_interrupt(vcpu)(quoted diff) +1
-0+ && !kvm_cpu_has_pending_timer(vcpu)I guess the fix is to stub this out for the other archs.
Quoted 3 lines
Quoted 1 line
Agreed. How's this.
Better :); applied.
Hi, Marcelo
Quoted 8 lines
This patch got into trouble when OS don't use PIT/LAPIC timer and don't disable them. Then the pending counters would keep increasing, but the HLT emulation can't be executed. And this would resulted in mass a lot (above 220,000 per second)
halt_exitfor the Windows XP that using RTC as the clocksource (and keep PIT enabled after bios did, just mask the pin) idle, and the cpu utilize would be about 100% of QEmu process.The following patch used another way to fix the issue, though not very formal.
Hi Sheng,
Did you have kvm.git commit 8ae6dc90ac84d9734e343210c8ec709f50cd9d89 when testing this?
I believe it should fix that issue, because "ps->inject_pending" won't be set by kvm_pit_timer_intr_post() if the IRQ is masked. Please correct me if I'm wrong.
-------------------------------------------------------------------------
This SF.net email is sponsored by the 2008 JavaOne(SM) Conference Don't miss this year's exciting event. There's still time to save $100. Use priority code J8TL2D2.
http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone
Re: [patch 1/2] KVM: hlt emulation should take in-kernel APIC/PIT timers into account
Quoted 29 lines · On Friday 09 May 2008 22:53:00 Marcelo Tosatti wrote:
Quoted 25 lines · Sheng
Quoted 11 lines · On Sunday 13 April 2008 17:28:22 Avi Kivity wrote:
Quoted 10 lines · Marcelo Tosatti
Quoted 8 lines · Avi Kivity
This breaks ia64 (and shouldn't s390 use this too?)
Review
Quoted 4 lines
* We will block until either an interrupt or a signal wakes us up */ while (!kvm_cpu_has_interrupt(vcpu)(quoted diff) +1
-0+ && !kvm_cpu_has_pending_timer(vcpu)I guess the fix is to stub this out for the other archs.
Quoted 3 lines
Quoted 1 line
Agreed. How's this.
Better :); applied.
Hi, Marcelo
This patch got into trouble when OS don't use PIT/LAPIC timer and don't disable them. Then the pending counters would keep increasing, but the HLT emulation can't be executed. And this would resulted in mass a lot (above 220,000 per second)
halt_exitfor the Windows XP that using RTC as the clocksource (and keep PIT enabled after bios did, just mask the pin) idle, and the cpu utilize would be about 100% of QEmu process.Quoted 2 lines
The following patch used another way to fix the issue, though not very formal.
Quoted 8 lines
Hi Sheng,
Did you have kvm.git commit
8ae6dc90ac84d9734e343210c8ec709f50cd9d89when testing this?I believe it should fix that issue, because "
ps->inject_pending" won't be set bykvm_pit_timer_intr_post()if the IRQ is masked. Please correct me if I'm wrong.
Oh, sorry, I missed that commit. But... It just solved an half of the problem. LAPIC suffered from it as well, and the current HLT emulation still didn't work... And I can't find something like inject_pending in LAPIC timer.
I have to say, I think my method is more preciously, directly and efficient... It also can be extended easily if we got more clock sources (though I don't think this would happen in near future...). In fact, I think take care of pending counts is some kind of *wrong concept*... We should take care of the window, or when the increment of pending counters happened, CMIIW. And it got nothing to do with the current counter number (yeah, I realized it after saw the hlt behaviour in XP, not before ;) ).
--
Thanks
Yang, Sheng
-------------------------------------------------------------------------
This SF.net email is sponsored by the 2008 JavaOne(SM) Conference Don't miss this year's exciting event. There's still time to save $100. Use priority code J8TL2D2.
http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone
Re: [patch 1/2] KVM: hlt emulation should take in-kernel APIC/PIT timers into account
Quoted 11 lines · Sheng
Quoted 6 lines
Did you have kvm.git commit
8ae6dc90ac84d9734e343210c8ec709f50cd9d89when testing this?I believe it should fix that issue, because "
ps->inject_pending" won't be set bykvm_pit_timer_intr_post()if the IRQ is masked. Please correct me if I'm wrong.Oh, sorry, I missed that commit. But... It just solved an half of the problem. LAPIC suffered from it as well, and the current HLT emulation still didn't work... And I can't find something like
inject_pendingin LAPIC timer.
Quoted 7 lines
I have to say, I think my method is more preciously, directly and efficient... It also can be extended easily if we got more clock sources (though I don't think this would happen in near future...). In fact, I think take care of pending counts is some kind of *wrong concept*... We should take care of the window, or when the increment of pending counters happened, CMIIW. And it got nothing to do with the current counter number (yeah, I realized it after saw the hlt behaviour in XP, not before ;) ).
Sheng,
The problem is that you don't want to emulate hlt if you have a pending timer _and_ the guest is accepting events. So for example if there are two apic timers pending, you inject one of them, guest execute's hlt, we end up in vcpu_block().
Does this work for you?
arch/x86/kvm/lapic.c +1 -1
@@ -961,7 +961,7 @@ int apic_has_pending_timer(struct kvm_vcpu *vcpu) {struct kvm_lapic *lapic = vcpu->arch.apic; -if (lapic) +if (lapic &&apic_enabled (lapic) &&apic_lvt_enabled (lapic,APIC_LVTT ))return atomic_read (&lapic->timer.pending);return 0 ;
-------------------------------------------------------------------------
This SF.net email is sponsored by the 2008 JavaOne(SM) Conference Don't miss this year's exciting event. There's still time to save $100. Use priority code J8TL2D2.
http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone
Re: [patch 1/2] KVM: hlt emulation should take in-kernel APIC/PIT timers into account
Quoted 21 lines · On Tuesday 13 May 2008 00:40:05 Marcelo Tosatti wrote:
Quoted 12 lines · Sheng
Quoted 6 lines
Did you have kvm.git commit
8ae6dc90ac84d9734e343210c8ec709f50cd9d89when testing this?I believe it should fix that issue, because "
ps->inject_pending" won't be set bykvm_pit_timer_intr_post()if the IRQ is masked. Please correct me if I'm wrong.Oh, sorry, I missed that commit. But... It just solved an half of the problem. LAPIC suffered from it as well, and the current HLT emulation still didn't work... And I can't find something like
inject_pendingin LAPIC timer.Quoted 7 lines
I have to say, I think my method is more preciously, directly and efficient... It also can be extended easily if we got more clock sources (though I don't think this would happen in near future...). In fact, I think take care of pending counts is some kind of *wrong concept*... We should take care of the window, or when the increment of pending counters happened, CMIIW. And it got nothing to do with the current counter number (yeah, I realized it after saw the hlt behaviour in XP, not before ;) ).
Quoted 8 lines
Sheng,
The problem is that you don't want to emulate hlt if you have a pending timer _and_ the guest is accepting events. So for example if there are two apic timers pending, you inject one of them, guest execute's hlt, we end up in
vcpu_block().Does this work for you?
Yeah. I also suggest using the consistent implement for all the _has_pending_timer. (in fact, if take pending counters as the interrupts which have to delay their injection, the explanation is well :) )
Signature · 2 lines
Thanks Yang, Sheng
Quoted 6 lines
arch/x86/kvm/lapic.c +0
-0
arch/x86/kvm/lapic.c int apic_has_pending_timer(struct kvm_vcpu *vcpu) · line 967
Quoted 8 lines
arch/x86/kvm/lapic.c +1
-1@@ -961,7 +961,7 @@ int apic_has_pending_timer(struct kvm_vcpu *vcpu) {struct kvm_lapic *lapic = vcpu->arch.apic; -if (lapic) +if (lapic &&apic_enabled (lapic) &&apic_lvt_enabled (lapic,APIC_LVTT ))return atomic_read (&lapic->timer.pending);return 0 ;
-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/