]> www.infradead.org Git - nvme.git/commitdiff
KVM: x86: Implement hook for determining max NPT mapping level
authorMichael Roth <michael.roth@amd.com>
Wed, 1 May 2024 08:52:05 +0000 (03:52 -0500)
committerPaolo Bonzini <pbonzini@redhat.com>
Sun, 12 May 2024 08:09:33 +0000 (04:09 -0400)
In the case of SEV-SNP, whether or not a 2MB page can be mapped via a
2MB mapping in the guest's nested page table depends on whether or not
any subpages within the range have already been initialized as private
in the RMP table. The existing mixed-attribute tracking in KVM is
insufficient here, for instance:

  - gmem allocates 2MB page
  - guest issues PVALIDATE on 2MB page
  - guest later converts a subpage to shared
  - SNP host code issues PSMASH to split 2MB RMP mapping to 4K
  - KVM MMU splits NPT mapping to 4K
  - guest later converts that shared page back to private

At this point there are no mixed attributes, and KVM would normally
allow for 2MB NPT mappings again, but this is actually not allowed
because the RMP table mappings are 4K and cannot be promoted on the
hypervisor side, so the NPT mappings must still be limited to 4K to
match this.

Implement a kvm_x86_ops.private_max_mapping_level() hook for SEV that
checks for this condition and adjusts the mapping level accordingly.

Reviewed-by: Paolo Bonzini <pbonzini@redhat.com>
Signed-off-by: Michael Roth <michael.roth@amd.com>
Message-ID: <20240501085210.2213060-16-michael.roth@amd.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
arch/x86/kvm/svm/sev.c
arch/x86/kvm/svm/svm.c
arch/x86/kvm/svm/svm.h

index dc00b89404a2d8fce70fcb3e5d91b240c87b82ea..0bbbcadbac72c6d77b38cf18e2328ec27450a53e 100644 (file)
@@ -4727,3 +4727,18 @@ next_pfn:
                cond_resched();
        }
 }
+
+int sev_private_max_mapping_level(struct kvm *kvm, kvm_pfn_t pfn)
+{
+       int level, rc;
+       bool assigned;
+
+       if (!sev_snp_guest(kvm))
+               return 0;
+
+       rc = snp_lookup_rmpentry(pfn, &assigned, &level);
+       if (rc || !assigned)
+               return PG_LEVEL_4K;
+
+       return level;
+}
index 653cdb23a7d1b6833855135859ccbe17f22176b1..3d0549ca246f5b783b1c6d0d2236ad16ecf59124 100644 (file)
@@ -5084,6 +5084,7 @@ static struct kvm_x86_ops svm_x86_ops __initdata = {
 
        .gmem_prepare = sev_gmem_prepare,
        .gmem_invalidate = sev_gmem_invalidate,
+       .private_max_mapping_level = sev_private_max_mapping_level,
 };
 
 /*
index 3cea024a7c185bc133b71b497a7459d09ca4836f..555c55f50298b703437425b2d63ef995afbb007c 100644 (file)
@@ -738,6 +738,7 @@ void sev_vcpu_unblocking(struct kvm_vcpu *vcpu);
 void sev_snp_init_protected_guest_state(struct kvm_vcpu *vcpu);
 int sev_gmem_prepare(struct kvm *kvm, kvm_pfn_t pfn, gfn_t gfn, int max_order);
 void sev_gmem_invalidate(kvm_pfn_t start, kvm_pfn_t end);
+int sev_private_max_mapping_level(struct kvm *kvm, kvm_pfn_t pfn);
 #else
 static inline struct page *snp_safe_alloc_page(struct kvm_vcpu *vcpu) {
        return alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO);
@@ -759,6 +760,10 @@ static inline int sev_gmem_prepare(struct kvm *kvm, kvm_pfn_t pfn, gfn_t gfn, in
        return 0;
 }
 static inline void sev_gmem_invalidate(kvm_pfn_t start, kvm_pfn_t end) {}
+static inline int sev_private_max_mapping_level(struct kvm *kvm, kvm_pfn_t pfn)
+{
+       return 0;
+}
 
 #endif