diff options
author | Chris Lalancette <clalance@redhat.com> | 2010-06-16 17:11:11 -0400 |
---|---|---|
committer | Avi Kivity <avi@redhat.com> | 2010-08-01 03:46:49 -0400 |
commit | 33572ac0ad5ba5016da72e6654e607726568f9c0 (patch) | |
tree | fe622a883803896c98de49226fb1899807005ba9 /arch/x86/kvm/i8254.c | |
parent | c37eda138473f8c843f2b4aa8da252fdfdaaafa3 (diff) |
KVM: x86: Introduce a workqueue to deliver PIT timer interrupts
We really want to "kvm_set_irq" during the hrtimer callback,
but that is risky because that is during interrupt context.
Instead, offload the work to a workqueue, which is a bit safer
and should provide most of the same functionality.
Signed-off-by: Chris Lalancette <clalance@redhat.com>
Signed-off-by: Marcelo Tosatti <mtosatti@redhat.com>
Diffstat (limited to 'arch/x86/kvm/i8254.c')
-rw-r--r-- | arch/x86/kvm/i8254.c | 141 |
1 files changed, 85 insertions, 56 deletions
diff --git a/arch/x86/kvm/i8254.c b/arch/x86/kvm/i8254.c index 188d82762c1b..467cc47fb733 100644 --- a/arch/x86/kvm/i8254.c +++ b/arch/x86/kvm/i8254.c | |||
@@ -34,6 +34,7 @@ | |||
34 | 34 | ||
35 | #include <linux/kvm_host.h> | 35 | #include <linux/kvm_host.h> |
36 | #include <linux/slab.h> | 36 | #include <linux/slab.h> |
37 | #include <linux/workqueue.h> | ||
37 | 38 | ||
38 | #include "irq.h" | 39 | #include "irq.h" |
39 | #include "i8254.h" | 40 | #include "i8254.h" |
@@ -244,11 +245,22 @@ static void kvm_pit_ack_irq(struct kvm_irq_ack_notifier *kian) | |||
244 | { | 245 | { |
245 | struct kvm_kpit_state *ps = container_of(kian, struct kvm_kpit_state, | 246 | struct kvm_kpit_state *ps = container_of(kian, struct kvm_kpit_state, |
246 | irq_ack_notifier); | 247 | irq_ack_notifier); |
247 | raw_spin_lock(&ps->inject_lock); | 248 | int value; |
248 | if (atomic_dec_return(&ps->pit_timer.pending) < 0) | 249 | |
250 | spin_lock(&ps->inject_lock); | ||
251 | value = atomic_dec_return(&ps->pit_timer.pending); | ||
252 | if (value < 0) | ||
253 | /* spurious acks can be generated if, for example, the | ||
254 | * PIC is being reset. Handle it gracefully here | ||
255 | */ | ||
249 | atomic_inc(&ps->pit_timer.pending); | 256 | atomic_inc(&ps->pit_timer.pending); |
257 | else if (value > 0) | ||
258 | /* in this case, we had multiple outstanding pit interrupts | ||
259 | * that we needed to inject. Reinject | ||
260 | */ | ||
261 | queue_work(ps->pit->wq, &ps->pit->expired); | ||
250 | ps->irq_ack = 1; | 262 | ps->irq_ack = 1; |
251 | raw_spin_unlock(&ps->inject_lock); | 263 | spin_unlock(&ps->inject_lock); |
252 | } | 264 | } |
253 | 265 | ||
254 | void __kvm_migrate_pit_timer(struct kvm_vcpu *vcpu) | 266 | void __kvm_migrate_pit_timer(struct kvm_vcpu *vcpu) |
@@ -264,10 +276,10 @@ void __kvm_migrate_pit_timer(struct kvm_vcpu *vcpu) | |||
264 | hrtimer_start_expires(timer, HRTIMER_MODE_ABS); | 276 | hrtimer_start_expires(timer, HRTIMER_MODE_ABS); |
265 | } | 277 | } |
266 | 278 | ||
267 | static void destroy_pit_timer(struct kvm_timer *pt) | 279 | static void destroy_pit_timer(struct kvm_pit *pit) |
268 | { | 280 | { |
269 | pr_debug("execute del timer!\n"); | 281 | hrtimer_cancel(&pit->pit_state.pit_timer.timer); |
270 | hrtimer_cancel(&pt->timer); | 282 | cancel_work_sync(&pit->expired); |
271 | } | 283 | } |
272 | 284 | ||
273 | static bool kpit_is_periodic(struct kvm_timer *ktimer) | 285 | static bool kpit_is_periodic(struct kvm_timer *ktimer) |
@@ -281,6 +293,60 @@ static struct kvm_timer_ops kpit_ops = { | |||
281 | .is_periodic = kpit_is_periodic, | 293 | .is_periodic = kpit_is_periodic, |
282 | }; | 294 | }; |
283 | 295 | ||
296 | static void pit_do_work(struct work_struct *work) | ||
297 | { | ||
298 | struct kvm_pit *pit = container_of(work, struct kvm_pit, expired); | ||
299 | struct kvm *kvm = pit->kvm; | ||
300 | struct kvm_vcpu *vcpu; | ||
301 | int i; | ||
302 | struct kvm_kpit_state *ps = &pit->pit_state; | ||
303 | int inject = 0; | ||
304 | |||
305 | /* Try to inject pending interrupts when | ||
306 | * last one has been acked. | ||
307 | */ | ||
308 | spin_lock(&ps->inject_lock); | ||
309 | if (ps->irq_ack) { | ||
310 | ps->irq_ack = 0; | ||
311 | inject = 1; | ||
312 | } | ||
313 | spin_unlock(&ps->inject_lock); | ||
314 | if (inject) { | ||
315 | kvm_set_irq(kvm, kvm->arch.vpit->irq_source_id, 0, 1); | ||
316 | kvm_set_irq(kvm, kvm->arch.vpit->irq_source_id, 0, 0); | ||
317 | |||
318 | /* | ||
319 | * Provides NMI watchdog support via Virtual Wire mode. | ||
320 | * The route is: PIT -> PIC -> LVT0 in NMI mode. | ||
321 | * | ||
322 | * Note: Our Virtual Wire implementation is simplified, only | ||
323 | * propagating PIT interrupts to all VCPUs when they have set | ||
324 | * LVT0 to NMI delivery. Other PIC interrupts are just sent to | ||
325 | * VCPU0, and only if its LVT0 is in EXTINT mode. | ||
326 | */ | ||
327 | if (kvm->arch.vapics_in_nmi_mode > 0) | ||
328 | kvm_for_each_vcpu(i, vcpu, kvm) | ||
329 | kvm_apic_nmi_wd_deliver(vcpu); | ||
330 | } | ||
331 | } | ||
332 | |||
333 | static enum hrtimer_restart pit_timer_fn(struct hrtimer *data) | ||
334 | { | ||
335 | struct kvm_timer *ktimer = container_of(data, struct kvm_timer, timer); | ||
336 | struct kvm_pit *pt = ktimer->kvm->arch.vpit; | ||
337 | |||
338 | if (ktimer->reinject || !atomic_read(&ktimer->pending)) { | ||
339 | atomic_inc(&ktimer->pending); | ||
340 | queue_work(pt->wq, &pt->expired); | ||
341 | } | ||
342 | |||
343 | if (ktimer->t_ops->is_periodic(ktimer)) { | ||
344 | hrtimer_add_expires_ns(&ktimer->timer, ktimer->period); | ||
345 | return HRTIMER_RESTART; | ||
346 | } else | ||
347 | return HRTIMER_NORESTART; | ||
348 | } | ||
349 | |||
284 | static void create_pit_timer(struct kvm_kpit_state *ps, u32 val, int is_period) | 350 | static void create_pit_timer(struct kvm_kpit_state *ps, u32 val, int is_period) |
285 | { | 351 | { |
286 | struct kvm_timer *pt = &ps->pit_timer; | 352 | struct kvm_timer *pt = &ps->pit_timer; |
@@ -292,13 +358,13 @@ static void create_pit_timer(struct kvm_kpit_state *ps, u32 val, int is_period) | |||
292 | 358 | ||
293 | /* TODO The new value only affected after the retriggered */ | 359 | /* TODO The new value only affected after the retriggered */ |
294 | hrtimer_cancel(&pt->timer); | 360 | hrtimer_cancel(&pt->timer); |
361 | cancel_work_sync(&ps->pit->expired); | ||
295 | pt->period = interval; | 362 | pt->period = interval; |
296 | ps->is_periodic = is_period; | 363 | ps->is_periodic = is_period; |
297 | 364 | ||
298 | pt->timer.function = kvm_timer_fn; | 365 | pt->timer.function = pit_timer_fn; |
299 | pt->t_ops = &kpit_ops; | 366 | pt->t_ops = &kpit_ops; |
300 | pt->kvm = ps->pit->kvm; | 367 | pt->kvm = ps->pit->kvm; |
301 | pt->vcpu = pt->kvm->bsp_vcpu; | ||
302 | 368 | ||
303 | atomic_set(&pt->pending, 0); | 369 | atomic_set(&pt->pending, 0); |
304 | ps->irq_ack = 1; | 370 | ps->irq_ack = 1; |
@@ -347,7 +413,7 @@ static void pit_load_count(struct kvm *kvm, int channel, u32 val) | |||
347 | } | 413 | } |
348 | break; | 414 | break; |
349 | default: | 415 | default: |
350 | destroy_pit_timer(&ps->pit_timer); | 416 | destroy_pit_timer(kvm->arch.vpit); |
351 | } | 417 | } |
352 | } | 418 | } |
353 | 419 | ||
@@ -626,7 +692,14 @@ struct kvm_pit *kvm_create_pit(struct kvm *kvm, u32 flags) | |||
626 | 692 | ||
627 | mutex_init(&pit->pit_state.lock); | 693 | mutex_init(&pit->pit_state.lock); |
628 | mutex_lock(&pit->pit_state.lock); | 694 | mutex_lock(&pit->pit_state.lock); |
629 | raw_spin_lock_init(&pit->pit_state.inject_lock); | 695 | spin_lock_init(&pit->pit_state.inject_lock); |
696 | |||
697 | pit->wq = create_singlethread_workqueue("kvm-pit-wq"); | ||
698 | if (!pit->wq) { | ||
699 | kfree(pit); | ||
700 | return NULL; | ||
701 | } | ||
702 | INIT_WORK(&pit->expired, pit_do_work); | ||
630 | 703 | ||
631 | kvm->arch.vpit = pit; | 704 | kvm->arch.vpit = pit; |
632 | pit->kvm = kvm; | 705 | pit->kvm = kvm; |
@@ -685,54 +758,10 @@ void kvm_free_pit(struct kvm *kvm) | |||
685 | mutex_lock(&kvm->arch.vpit->pit_state.lock); | 758 | mutex_lock(&kvm->arch.vpit->pit_state.lock); |
686 | timer = &kvm->arch.vpit->pit_state.pit_timer.timer; | 759 | timer = &kvm->arch.vpit->pit_state.pit_timer.timer; |
687 | hrtimer_cancel(timer); | 760 | hrtimer_cancel(timer); |
761 | cancel_work_sync(&kvm->arch.vpit->expired); | ||
688 | kvm_free_irq_source_id(kvm, kvm->arch.vpit->irq_source_id); | 762 | kvm_free_irq_source_id(kvm, kvm->arch.vpit->irq_source_id); |
689 | mutex_unlock(&kvm->arch.vpit->pit_state.lock); | 763 | mutex_unlock(&kvm->arch.vpit->pit_state.lock); |
764 | destroy_workqueue(kvm->arch.vpit->wq); | ||
690 | kfree(kvm->arch.vpit); | 765 | kfree(kvm->arch.vpit); |
691 | } | 766 | } |
692 | } | 767 | } |
693 | |||
694 | static void __inject_pit_timer_intr(struct kvm *kvm) | ||
695 | { | ||
696 | struct kvm_vcpu *vcpu; | ||
697 | int i; | ||
698 | |||
699 | kvm_set_irq(kvm, kvm->arch.vpit->irq_source_id, 0, 1); | ||
700 | kvm_set_irq(kvm, kvm->arch.vpit->irq_source_id, 0, 0); | ||
701 | |||
702 | /* | ||
703 | * Provides NMI watchdog support via Virtual Wire mode. | ||
704 | * The route is: PIT -> PIC -> LVT0 in NMI mode. | ||
705 | * | ||
706 | * Note: Our Virtual Wire implementation is simplified, only | ||
707 | * propagating PIT interrupts to all VCPUs when they have set | ||
708 | * LVT0 to NMI delivery. Other PIC interrupts are just sent to | ||
709 | * VCPU0, and only if its LVT0 is in EXTINT mode. | ||
710 | */ | ||
711 | if (kvm->arch.vapics_in_nmi_mode > 0) | ||
712 | kvm_for_each_vcpu(i, vcpu, kvm) | ||
713 | kvm_apic_nmi_wd_deliver(vcpu); | ||
714 | } | ||
715 | |||
716 | void kvm_inject_pit_timer_irqs(struct kvm_vcpu *vcpu) | ||
717 | { | ||
718 | struct kvm_pit *pit = vcpu->kvm->arch.vpit; | ||
719 | struct kvm *kvm = vcpu->kvm; | ||
720 | struct kvm_kpit_state *ps; | ||
721 | |||
722 | if (pit) { | ||
723 | int inject = 0; | ||
724 | ps = &pit->pit_state; | ||
725 | |||
726 | /* Try to inject pending interrupts when | ||
727 | * last one has been acked. | ||
728 | */ | ||
729 | raw_spin_lock(&ps->inject_lock); | ||
730 | if (atomic_read(&ps->pit_timer.pending) && ps->irq_ack) { | ||
731 | ps->irq_ack = 0; | ||
732 | inject = 1; | ||
733 | } | ||
734 | raw_spin_unlock(&ps->inject_lock); | ||
735 | if (inject) | ||
736 | __inject_pit_timer_intr(kvm); | ||
737 | } | ||
738 | } | ||