• David Woodhouse's avatar
    KVM: x86: Fix recording of guest steal time / preempted status · 7e2175eb
    David Woodhouse authored
    In commit b0431382 ("x86/KVM: Make sure KVM_VCPU_FLUSH_TLB flag is
    not missed") we switched to using a gfn_to_pfn_cache for accessing the
    guest steal time structure in order to allow for an atomic xchg of the
    preempted field. This has a couple of problems.
    
    Firstly, kvm_map_gfn() doesn't work at all for IOMEM pages when the
    atomic flag is set, which it is in kvm_steal_time_set_preempted(). So a
    guest vCPU using an IOMEM page for its steal time would never have its
    preempted field set.
    
    Secondly, the gfn_to_pfn_cache is not invalidated in all cases where it
    should have been. There are two stages to the GFN->PFN conversion;
    first the GFN is converted to a userspace HVA, and then that HVA is
    looked up in the process page tables to find the underlying host PFN.
    Correct invalidation of the latter would require being hooked up to the
    MMU notifiers, but that doesn't happen---so it just keeps mapping and
    unmapping the *wrong* PFN after the userspace page tables change.
    
    In the !IOMEM case at least the stale page *is* pinned all the time it's
    cached, so it won't be freed and reused by anyone else while still
    receiving the steal time updates. The map/unmap dance only takes care
    of the KVM administrivia such as marking the page dirty.
    
    Until the gfn_to_pfn cache handles the remapping automatically by
    integrating with the MMU notifiers, we might as well not get a
    kernel mapping of it, and use the perfectly serviceable userspace HVA
    that we already have.  We just need to implement the atomic xchg on
    the userspace address with appropriate exception handling, which is
    fairly trivial.
    
    Cc: stable@vger.kernel.org
    Fixes: b0431382 ("x86/KVM: Make sure KVM_VCPU_FLUSH_TLB flag is not missed")
    Signed-off-by: default avatarDavid Woodhouse <dwmw@amazon.co.uk>
    Message-Id: <3645b9b889dac6438394194bb5586a46b68d581f.camel@infradead.org>
    [I didn't entirely agree with David's assessment of the
     usefulness of the gfn_to_pfn cache, and integrated the outcome
     of the discussion in the above commit message. - Paolo]
    Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
    7e2175eb
kvm_host.h 58.6 KB