KVM: LAPIC: ensure APIC map is up to date on concurrent update requests
authorPaolo Bonzini <pbonzini@redhat.com>
Mon, 22 Jun 2020 14:37:42 +0000 (16:37 +0200)
committerPaolo Bonzini <pbonzini@redhat.com>
Mon, 22 Jun 2020 17:37:30 +0000 (13:37 -0400)
commit44d527170731c75587e95052f3eea72b8c651daf
treeb982f99840d0e5b36f01e730d8d7ad5c8227c067
parentaf28dfacbe00d53df5dec2bf50640df33138b1fe
KVM: LAPIC: ensure APIC map is up to date on concurrent update requests

The following race can cause lost map update events:

         cpu1                            cpu2

                                apic_map_dirty = true
  ------------------------------------------------------------
                                kvm_recalculate_apic_map:
                                     pass check
                                         mutex_lock(&kvm->arch.apic_map_lock);
                                         if (!kvm->arch.apic_map_dirty)
                                     and in process of updating map
  -------------------------------------------------------------
    other calls to
       apic_map_dirty = true         might be too late for affected cpu
  -------------------------------------------------------------
                                     apic_map_dirty = false
  -------------------------------------------------------------
    kvm_recalculate_apic_map:
    bail out on
      if (!kvm->arch.apic_map_dirty)

To fix it, record the beginning of an update of the APIC map in
apic_map_dirty.  If another APIC map change switches apic_map_dirty
back to DIRTY during the update, kvm_recalculate_apic_map should not
make it CLEAN, and the other caller will go through the slow path.

Reported-by: Igor Mammedov <imammedo@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
arch/x86/include/asm/kvm_host.h
arch/x86/kvm/lapic.c