1. 01 3月, 2022 2 次提交
    • S
      KVM: x86/mmu: Zap only obsolete roots if a root shadow page is zapped · 527d5cd7
      Sean Christopherson 提交于
      Zap only obsolete roots when responding to zapping a single root shadow
      page.  Because KVM keeps root_count elevated when stuffing a previous
      root into its PGD cache, shadowing a 64-bit guest means that zapping any
      root causes all vCPUs to reload all roots, even if their current root is
      not affected by the zap.
      
      For many kernels, zapping a single root is a frequent operation, e.g. in
      Linux it happens whenever an mm is dropped, e.g. process exits, etc...
      Signed-off-by: NSean Christopherson <seanjc@google.com>
      Reviewed-by: NBen Gardon <bgardon@google.com>
      Message-Id: <20220225182248.3812651-5-seanjc@google.com>
      Signed-off-by: NPaolo Bonzini <pbonzini@redhat.com>
      527d5cd7
    • S
      KVM: Drop kvm_reload_remote_mmus(), open code request in x86 users · 2f6f66cc
      Sean Christopherson 提交于
      Remove the generic kvm_reload_remote_mmus() and open code its
      functionality into the two x86 callers.  x86 is (obviously) the only
      architecture that uses the hook, and is also the only architecture that
      uses KVM_REQ_MMU_RELOAD in a way that's consistent with the name.  That
      will change in a future patch, as x86's usage when zapping a single
      shadow page x86 doesn't actually _need_ to reload all vCPUs' MMUs, only
      MMUs whose root is being zapped actually need to be reloaded.
      
      s390 also uses KVM_REQ_MMU_RELOAD, but for a slightly different purpose.
      
      Drop the generic code in anticipation of implementing s390 and x86 arch
      specific requests, which will allow dropping KVM_REQ_MMU_RELOAD entirely.
      
      Opportunistically reword the x86 TDP MMU comment to avoid making
      references to functions (and requests!) when possible, and to remove the
      rather ambiguous "this".
      
      No functional change intended.
      
      Cc: Ben Gardon <bgardon@google.com>
      Signed-off-by: NSean Christopherson <seanjc@google.com>
      Reviewed-by: NBen Gardon <bgardon@google.com>
      Message-Id: <20220225182248.3812651-4-seanjc@google.com>
      Signed-off-by: NPaolo Bonzini <pbonzini@redhat.com>
      2f6f66cc
  2. 25 2月, 2022 8 次提交
    • P
      KVM: x86/mmu: clear MMIO cache when unloading the MMU · 6d58f275
      Paolo Bonzini 提交于
      For cleanliness, do not leave a stale GVA in the cache after all the roots are
      cleared.  In practice, kvm_mmu_load will go through kvm_mmu_sync_roots if
      paging is on, and will not use vcpu_match_mmio_gva at all if paging is off.
      However, leaving data in the cache might cause bugs in the future.
      Signed-off-by: NPaolo Bonzini <pbonzini@redhat.com>
      6d58f275
    • P
      KVM: x86/mmu: Always use current mmu's role when loading new PGD · d2e5f333
      Paolo Bonzini 提交于
      Since the guest PGD is now loaded after the MMU has been set up
      completely, the desired role for a cache hit is simply the current
      mmu_role.  There is no need to compute it again, so __kvm_mmu_new_pgd
      can be folded in kvm_mmu_new_pgd.
      Reviewed-by: NSean Christopherson <seanjc@google.com>
      Signed-off-by: NPaolo Bonzini <pbonzini@redhat.com>
      d2e5f333
    • P
      KVM: x86/mmu: load new PGD after the shadow MMU is initialized · 3cffc89d
      Paolo Bonzini 提交于
      Now that __kvm_mmu_new_pgd does not look at the MMU's root_level and
      shadow_root_level anymore, pull the PGD load after the initialization of
      the shadow MMUs.
      
      Besides being more intuitive, this enables future simplifications
      and optimizations because it's not necessary anymore to compute the
      role outside kvm_init_mmu.  In particular, kvm_mmu_reset_context was not
      attempting to use a cached PGD to avoid having to figure out the new role.
      With this change, it could follow what nested_{vmx,svm}_load_cr3 are doing,
      and avoid unloading all the cached roots.
      Reviewed-by: NSean Christopherson <seanjc@google.com>
      Signed-off-by: NPaolo Bonzini <pbonzini@redhat.com>
      3cffc89d
    • P
      KVM: x86/mmu: look for a cached PGD when going from 32-bit to 64-bit · 5499ea73
      Paolo Bonzini 提交于
      Right now, PGD caching avoids placing a PAE root in the cache by using the
      old value of mmu->root_level and mmu->shadow_root_level; it does not look
      for a cached PGD if the old root is a PAE one, and then frees it using
      kvm_mmu_free_roots.
      
      Change the logic instead to free the uncacheable root early.
      This way, __kvm_new_mmu_pgd is able to look up the cache when going from
      32-bit to 64-bit (if there is a hit, the invalid root becomes the least
      recently used).  An example of this is nested virtualization with shadow
      paging, when a 64-bit L1 runs a 32-bit L2.
      
      As a side effect (which is actually the reason why this patch was
      written), PGD caching does not use the old value of mmu->root_level
      and mmu->shadow_root_level anymore.
      Reviewed-by: NSean Christopherson <seanjc@google.com>
      Signed-off-by: NPaolo Bonzini <pbonzini@redhat.com>
      5499ea73
    • P
      KVM: x86/mmu: do not pass vcpu to root freeing functions · 0c1c92f1
      Paolo Bonzini 提交于
      These functions only operate on a given MMU, of which there is more
      than one in a vCPU (we care about two, because the third does not have
      any roots and is only used to walk guest page tables).  They do need a
      struct kvm in order to lock the mmu_lock, but they do not needed anything
      else in the struct kvm_vcpu.  So, pass the vcpu->kvm directly to them.
      Signed-off-by: NPaolo Bonzini <pbonzini@redhat.com>
      0c1c92f1
    • P
      KVM: x86/mmu: do not consult levels when freeing roots · 594bef79
      Paolo Bonzini 提交于
      Right now, PGD caching requires a complicated dance of first computing
      the MMU role and passing it to __kvm_mmu_new_pgd(), and then separately calling
      kvm_init_mmu().
      
      Part of this is due to kvm_mmu_free_roots using mmu->root_level and
      mmu->shadow_root_level to distinguish whether the page table uses a single
      root or 4 PAE roots.  Because kvm_init_mmu() can overwrite mmu->root_level,
      kvm_mmu_free_roots() must be called before kvm_init_mmu().
      
      However, even after kvm_init_mmu() there is a way to detect whether the
      page table may hold PAE roots, as root.hpa isn't backed by a shadow when
      it points at PAE roots.  Using this method results in simpler code, and
      is one less obstacle in moving all calls to __kvm_mmu_new_pgd() after the
      MMU has been initialized.
      Signed-off-by: NPaolo Bonzini <pbonzini@redhat.com>
      594bef79
    • P
      KVM: x86: use struct kvm_mmu_root_info for mmu->root · b9e5603c
      Paolo Bonzini 提交于
      The root_hpa and root_pgd fields form essentially a struct kvm_mmu_root_info.
      Use the struct to have more consistency between mmu->root and
      mmu->prev_roots.
      
      The patch is entirely search and replace except for cached_root_available,
      which does not need a temporary struct kvm_mmu_root_info anymore.
      Reviewed-by: NSean Christopherson <seanjc@google.com>
      Signed-off-by: NPaolo Bonzini <pbonzini@redhat.com>
      b9e5603c
    • P
      KVM: x86/mmu: avoid NULL-pointer dereference on page freeing bugs · 9191b8f0
      Paolo Bonzini 提交于
      WARN and bail if KVM attempts to free a root that isn't backed by a shadow
      page.  KVM allocates a bare page for "special" roots, e.g. when using PAE
      paging or shadowing 2/3/4-level page tables with 4/5-level, and so root_hpa
      will be valid but won't be backed by a shadow page.  It's all too easy to
      blindly call mmu_free_root_page() on root_hpa, be nice and WARN instead of
      crashing KVM and possibly the kernel.
      Reviewed-by: NSean Christopherson <seanjc@google.com>
      Signed-off-by: NPaolo Bonzini <pbonzini@redhat.com>
      9191b8f0
  3. 19 2月, 2022 1 次提交
  4. 11 2月, 2022 14 次提交
  5. 20 1月, 2022 1 次提交
  6. 20 12月, 2021 1 次提交
    • S
      KVM: x86: Retry page fault if MMU reload is pending and root has no sp · 18c841e1
      Sean Christopherson 提交于
      Play nice with a NULL shadow page when checking for an obsolete root in
      the page fault handler by flagging the page fault as stale if there's no
      shadow page associated with the root and KVM_REQ_MMU_RELOAD is pending.
      Invalidating memslots, which is the only case where _all_ roots need to
      be reloaded, requests all vCPUs to reload their MMUs while holding
      mmu_lock for lock.
      
      The "special" roots, e.g. pae_root when KVM uses PAE paging, are not
      backed by a shadow page.  Running with TDP disabled or with nested NPT
      explodes spectaculary due to dereferencing a NULL shadow page pointer.
      
      Skip the KVM_REQ_MMU_RELOAD check if there is a valid shadow page for the
      root.  Zapping shadow pages in response to guest activity, e.g. when the
      guest frees a PGD, can trigger KVM_REQ_MMU_RELOAD even if the current
      vCPU isn't using the affected root.  I.e. KVM_REQ_MMU_RELOAD can be seen
      with a completely valid root shadow page.  This is a bit of a moot point
      as KVM currently unloads all roots on KVM_REQ_MMU_RELOAD, but that will
      be cleaned up in the future.
      
      Fixes: a955cad8 ("KVM: x86/mmu: Retry page fault if root is invalidated by memslot update")
      Cc: stable@vger.kernel.org
      Cc: Maxim Levitsky <mlevitsk@redhat.com>
      Signed-off-by: NSean Christopherson <seanjc@google.com>
      Message-Id: <20211209060552.2956723-2-seanjc@google.com>
      Signed-off-by: NPaolo Bonzini <pbonzini@redhat.com>
      18c841e1
  7. 08 12月, 2021 13 次提交