• Sean Christopherson's avatar
    KVM: x86/mmu: Zap only SPs that shadow gPTEs when deleting memslot · 58a20a94
    Sean Christopherson authored
    When performing a targeted zap on memslot removal, zap only MMU pages that
    shadow guest PTEs, as zapping all SPs that "match" the gfn is inexact and
    unnecessary.  Furthermore, for_each_gfn_valid_sp() arguably shouldn't
    exist, because it doesn't do what most people would it expect it to do.
    The "round gfn for level" adjustment that is done for direct SPs (no gPTE)
    means that the exact gfn comparison will not get a match, even when a SP
    does "cover" a gfn, or was even created specifically for a gfn.
    
    For memslot deletion specifically, KVM's behavior will vary significantly
    based on the size and alignment of a memslot, and in weird ways.  E.g. for
    a 4KiB memslot, KVM will zap more SPs if the slot is 1GiB aligned than if
    it's only 4KiB aligned.  And as described below, zapping SPs in the
    aligned case overzaps for direct MMUs, as odds are good the upper-level
    SPs are serving other memslots.
    
    To iterate over all potentially-relevant gfns, KVM would need to make a
    pass over the hash table for each level, with the gfn used for lookup
    rounded for said level.  And then check that the SP is of the correct
    level, too, e.g. to avoid over-zapping.
    
    But even then, KVM would massively overzap, as processing every level is
    all but guaranteed to zap SPs that serve other memslots, especially if the
    memslot being removed is relatively small.  KVM could mitigate that issue
    by processing only levels that can be possible guest huge pages, i.e. are
    less likely to be re-used for other memslot, but while somewhat logical,
    that's quite arbitrary and would be a bit of a mess to implement.
    
    So, zap only SPs with gPTEs, as the resulting behavior is easy to describe,
    is predictable, and is explicitly minimal, i.e. KVM only zaps SPs that
    absolutely must be zapped.
    
    Cc: Yan Zhao <yan.y.zhao@intel.com>
    Signed-off-by: default avatarSean Christopherson <seanjc@google.com>
    Reviewed-by: default avatarYan Zhao <yan.y.zhao@intel.com>
    Tested-by: default avatarYan Zhao <yan.y.zhao@intel.com>
    Message-ID: <20241009192345.1148353-2-seanjc@google.com>
    Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
    58a20a94
mmu.c 217 KB