diff options
author | Radim Krčmář <rkrcmar@redhat.com> | 2016-03-02 16:56:41 -0500 |
---|---|---|
committer | Paolo Bonzini <pbonzini@redhat.com> | 2016-03-04 03:29:47 -0500 |
commit | ddf54503e2bbed01958cf5fb16ad6378971d2468 (patch) | |
tree | d9c327a9036e3118653ca67181c4a32fabf04608 /arch/x86/kvm/i8254.c | |
parent | fd700a00dc2e821be92b0b56fd5d8ebf8c63f9ba (diff) |
KVM: i8254: use atomic_t instead of pit.inject_lock
The lock was an overkill, the same can be done with atomics.
A mb() was added in kvm_pit_ack_irq, to pair with implicit barrier
between pit_timer_fn and pit_do_work. The mb() prevents a race that
could happen if pending == 0 and irq_ack == 0:
kvm_pit_ack_irq: | pit_timer_fn:
p = atomic_read(&ps->pending); |
| atomic_inc(&ps->pending);
| queue_work(pit_do_work);
| pit_do_work:
| atomic_xchg(&ps->irq_ack, 0);
| return;
atomic_set(&ps->irq_ack, 1); |
if (p == 0) return; |
where the interrupt would not be delivered in this tick of pit_timer_fn.
PIT would have eventually delivered the interrupt, but we sacrifice
perofmance to make sure that interrupts are not needlessly delayed.
sfence isn't enough: atomic_dec_if_positive does atomic_read first and
x86 can reorder loads before stores. lfence isn't enough: store can
pass lfence, turning it into a nop. A compiler barrier would be more
than enough as CPU needs to stall for unbelievably long to use fences.
This patch doesn't do anything in kvm_pit_reset_reinject, because any
order of resets can race, but the result differs by at most one
interrupt, which is ok, because it's the same result as if the reset
happened at a slightly different time. (Original code didn't protect
the reset path with a proper lock, so users have to be robust.)
Signed-off-by: Radim Krčmář <rkrcmar@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Diffstat (limited to 'arch/x86/kvm/i8254.c')
-rw-r--r-- | arch/x86/kvm/i8254.c | 56 |
1 files changed, 23 insertions, 33 deletions
diff --git a/arch/x86/kvm/i8254.c b/arch/x86/kvm/i8254.c index bdbb3f076e72..0f5655c50e0c 100644 --- a/arch/x86/kvm/i8254.c +++ b/arch/x86/kvm/i8254.c | |||
@@ -237,11 +237,13 @@ static void kvm_pit_ack_irq(struct kvm_irq_ack_notifier *kian) | |||
237 | struct kvm_kpit_state *ps = container_of(kian, struct kvm_kpit_state, | 237 | struct kvm_kpit_state *ps = container_of(kian, struct kvm_kpit_state, |
238 | irq_ack_notifier); | 238 | irq_ack_notifier); |
239 | 239 | ||
240 | spin_lock(&ps->inject_lock); | 240 | atomic_set(&ps->irq_ack, 1); |
241 | /* irq_ack should be set before pending is read. Order accesses with | ||
242 | * inc(pending) in pit_timer_fn and xchg(irq_ack, 0) in pit_do_work. | ||
243 | */ | ||
244 | smp_mb(); | ||
241 | if (atomic_dec_if_positive(&ps->pending) > 0 && ps->reinject) | 245 | if (atomic_dec_if_positive(&ps->pending) > 0 && ps->reinject) |
242 | queue_kthread_work(&ps->pit->worker, &ps->pit->expired); | 246 | queue_kthread_work(&ps->pit->worker, &ps->pit->expired); |
243 | ps->irq_ack = 1; | ||
244 | spin_unlock(&ps->inject_lock); | ||
245 | } | 247 | } |
246 | 248 | ||
247 | void __kvm_migrate_pit_timer(struct kvm_vcpu *vcpu) | 249 | void __kvm_migrate_pit_timer(struct kvm_vcpu *vcpu) |
@@ -272,36 +274,25 @@ static void pit_do_work(struct kthread_work *work) | |||
272 | struct kvm_vcpu *vcpu; | 274 | struct kvm_vcpu *vcpu; |
273 | int i; | 275 | int i; |
274 | struct kvm_kpit_state *ps = &pit->pit_state; | 276 | struct kvm_kpit_state *ps = &pit->pit_state; |
275 | int inject = 0; | ||
276 | 277 | ||
277 | /* Try to inject pending interrupts when | 278 | if (ps->reinject && !atomic_xchg(&ps->irq_ack, 0)) |
278 | * last one has been acked. | 279 | return; |
280 | |||
281 | kvm_set_irq(kvm, kvm->arch.vpit->irq_source_id, 0, 1, false); | ||
282 | kvm_set_irq(kvm, kvm->arch.vpit->irq_source_id, 0, 0, false); | ||
283 | |||
284 | /* | ||
285 | * Provides NMI watchdog support via Virtual Wire mode. | ||
286 | * The route is: PIT -> LVT0 in NMI mode. | ||
287 | * | ||
288 | * Note: Our Virtual Wire implementation does not follow | ||
289 | * the MP specification. We propagate a PIT interrupt to all | ||
290 | * VCPUs and only when LVT0 is in NMI mode. The interrupt can | ||
291 | * also be simultaneously delivered through PIC and IOAPIC. | ||
279 | */ | 292 | */ |
280 | spin_lock(&ps->inject_lock); | 293 | if (atomic_read(&kvm->arch.vapics_in_nmi_mode) > 0) |
281 | if (!ps->reinject) | 294 | kvm_for_each_vcpu(i, vcpu, kvm) |
282 | inject = 1; | 295 | kvm_apic_nmi_wd_deliver(vcpu); |
283 | else if (ps->irq_ack) { | ||
284 | ps->irq_ack = 0; | ||
285 | inject = 1; | ||
286 | } | ||
287 | spin_unlock(&ps->inject_lock); | ||
288 | if (inject) { | ||
289 | kvm_set_irq(kvm, kvm->arch.vpit->irq_source_id, 0, 1, false); | ||
290 | kvm_set_irq(kvm, kvm->arch.vpit->irq_source_id, 0, 0, false); | ||
291 | |||
292 | /* | ||
293 | * Provides NMI watchdog support via Virtual Wire mode. | ||
294 | * The route is: PIT -> PIC -> LVT0 in NMI mode. | ||
295 | * | ||
296 | * Note: Our Virtual Wire implementation is simplified, only | ||
297 | * propagating PIT interrupts to all VCPUs when they have set | ||
298 | * LVT0 to NMI delivery. Other PIC interrupts are just sent to | ||
299 | * VCPU0, and only if its LVT0 is in EXTINT mode. | ||
300 | */ | ||
301 | if (atomic_read(&kvm->arch.vapics_in_nmi_mode) > 0) | ||
302 | kvm_for_each_vcpu(i, vcpu, kvm) | ||
303 | kvm_apic_nmi_wd_deliver(vcpu); | ||
304 | } | ||
305 | } | 296 | } |
306 | 297 | ||
307 | static enum hrtimer_restart pit_timer_fn(struct hrtimer *data) | 298 | static enum hrtimer_restart pit_timer_fn(struct hrtimer *data) |
@@ -324,7 +315,7 @@ static enum hrtimer_restart pit_timer_fn(struct hrtimer *data) | |||
324 | static inline void kvm_pit_reset_reinject(struct kvm_pit *pit) | 315 | static inline void kvm_pit_reset_reinject(struct kvm_pit *pit) |
325 | { | 316 | { |
326 | atomic_set(&pit->pit_state.pending, 0); | 317 | atomic_set(&pit->pit_state.pending, 0); |
327 | pit->pit_state.irq_ack = 1; | 318 | atomic_set(&pit->pit_state.irq_ack, 1); |
328 | } | 319 | } |
329 | 320 | ||
330 | static void create_pit_timer(struct kvm *kvm, u32 val, int is_period) | 321 | static void create_pit_timer(struct kvm *kvm, u32 val, int is_period) |
@@ -691,7 +682,6 @@ struct kvm_pit *kvm_create_pit(struct kvm *kvm, u32 flags) | |||
691 | 682 | ||
692 | mutex_init(&pit->pit_state.lock); | 683 | mutex_init(&pit->pit_state.lock); |
693 | mutex_lock(&pit->pit_state.lock); | 684 | mutex_lock(&pit->pit_state.lock); |
694 | spin_lock_init(&pit->pit_state.inject_lock); | ||
695 | 685 | ||
696 | pid = get_pid(task_tgid(current)); | 686 | pid = get_pid(task_tgid(current)); |
697 | pid_nr = pid_vnr(pid); | 687 | pid_nr = pid_vnr(pid); |