Skip to content

Commit 8eb0190

Browse files
mdrothbonzini
authored andcommitted
KVM: SEV: Implement gmem hook for invalidating private pages
Implement a platform hook to do the work of restoring the direct map entries of gmem-managed pages and transitioning the corresponding RMP table entries back to the default shared/hypervisor-owned state. Signed-off-by: Michael Roth <[email protected]> Message-ID: <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
1 parent 4f2e7aa commit 8eb0190

File tree

4 files changed

+68
-0
lines changed

4 files changed

+68
-0
lines changed

arch/x86/kvm/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ config KVM_AMD_SEV
138138
select ARCH_HAS_CC_PLATFORM
139139
select KVM_GENERIC_PRIVATE_MEM
140140
select HAVE_KVM_GMEM_PREPARE
141+
select HAVE_KVM_GMEM_INVALIDATE
141142
help
142143
Provides support for launching Encrypted VMs (SEV) and Encrypted VMs
143144
with Encrypted State (SEV-ES) on AMD processors.

arch/x86/kvm/svm/sev.c

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4663,3 +4663,67 @@ int sev_gmem_prepare(struct kvm *kvm, kvm_pfn_t pfn, gfn_t gfn, int max_order)
46634663

46644664
return 0;
46654665
}
4666+
4667+
void sev_gmem_invalidate(kvm_pfn_t start, kvm_pfn_t end)
4668+
{
4669+
kvm_pfn_t pfn;
4670+
4671+
pr_debug("%s: PFN start 0x%llx PFN end 0x%llx\n", __func__, start, end);
4672+
4673+
for (pfn = start; pfn < end;) {
4674+
bool use_2m_update = false;
4675+
int rc, rmp_level;
4676+
bool assigned;
4677+
4678+
rc = snp_lookup_rmpentry(pfn, &assigned, &rmp_level);
4679+
if (WARN_ONCE(rc, "SEV: Failed to retrieve RMP entry for PFN 0x%llx error %d\n",
4680+
pfn, rc))
4681+
goto next_pfn;
4682+
4683+
if (!assigned)
4684+
goto next_pfn;
4685+
4686+
use_2m_update = IS_ALIGNED(pfn, PTRS_PER_PMD) &&
4687+
end >= (pfn + PTRS_PER_PMD) &&
4688+
rmp_level > PG_LEVEL_4K;
4689+
4690+
/*
4691+
* If an unaligned PFN corresponds to a 2M region assigned as a
4692+
* large page in the RMP table, PSMASH the region into individual
4693+
* 4K RMP entries before attempting to convert a 4K sub-page.
4694+
*/
4695+
if (!use_2m_update && rmp_level > PG_LEVEL_4K) {
4696+
/*
4697+
* This shouldn't fail, but if it does, report it, but
4698+
* still try to update RMP entry to shared and pray this
4699+
* was a spurious error that can be addressed later.
4700+
*/
4701+
rc = snp_rmptable_psmash(pfn);
4702+
WARN_ONCE(rc, "SEV: Failed to PSMASH RMP entry for PFN 0x%llx error %d\n",
4703+
pfn, rc);
4704+
}
4705+
4706+
rc = rmp_make_shared(pfn, use_2m_update ? PG_LEVEL_2M : PG_LEVEL_4K);
4707+
if (WARN_ONCE(rc, "SEV: Failed to update RMP entry for PFN 0x%llx error %d\n",
4708+
pfn, rc))
4709+
goto next_pfn;
4710+
4711+
/*
4712+
* SEV-ES avoids host/guest cache coherency issues through
4713+
* WBINVD hooks issued via MMU notifiers during run-time, and
4714+
* KVM's VM destroy path at shutdown. Those MMU notifier events
4715+
* don't cover gmem since there is no requirement to map pages
4716+
* to a HVA in order to use them for a running guest. While the
4717+
* shutdown path would still likely cover things for SNP guests,
4718+
* userspace may also free gmem pages during run-time via
4719+
* hole-punching operations on the guest_memfd, so flush the
4720+
* cache entries for these pages before free'ing them back to
4721+
* the host.
4722+
*/
4723+
clflush_cache_range(__va(pfn_to_hpa(pfn)),
4724+
use_2m_update ? PMD_SIZE : PAGE_SIZE);
4725+
next_pfn:
4726+
pfn += use_2m_update ? PTRS_PER_PMD : 1;
4727+
cond_resched();
4728+
}
4729+
}

arch/x86/kvm/svm/svm.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5083,6 +5083,7 @@ static struct kvm_x86_ops svm_x86_ops __initdata = {
50835083
.alloc_apic_backing_page = svm_alloc_apic_backing_page,
50845084

50855085
.gmem_prepare = sev_gmem_prepare,
5086+
.gmem_invalidate = sev_gmem_invalidate,
50865087
};
50875088

50885089
/*

arch/x86/kvm/svm/svm.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -737,6 +737,7 @@ void sev_handle_rmp_fault(struct kvm_vcpu *vcpu, gpa_t gpa, u64 error_code);
737737
void sev_vcpu_unblocking(struct kvm_vcpu *vcpu);
738738
void sev_snp_init_protected_guest_state(struct kvm_vcpu *vcpu);
739739
int sev_gmem_prepare(struct kvm *kvm, kvm_pfn_t pfn, gfn_t gfn, int max_order);
740+
void sev_gmem_invalidate(kvm_pfn_t start, kvm_pfn_t end);
740741
#else
741742
static inline struct page *snp_safe_alloc_page(struct kvm_vcpu *vcpu) {
742743
return alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO);
@@ -757,6 +758,7 @@ static inline int sev_gmem_prepare(struct kvm *kvm, kvm_pfn_t pfn, gfn_t gfn, in
757758
{
758759
return 0;
759760
}
761+
static inline void sev_gmem_invalidate(kvm_pfn_t start, kvm_pfn_t end) {}
760762

761763
#endif
762764

0 commit comments

Comments
 (0)