[PATCH Part2 RFC v2 24/37] KVM: SVM: Reclaim the guest pages when SEV-SNP VM terminates
From: Brijesh Singh
Date: Fri Apr 30 2021 - 08:43:52 EST
The guest pages of the SEV-SNP VM maybe added as a private page in the
RMP entry (assigned bit is set). The guest private pages must be
transitioned to the hypervisor state before its freed.
Signed-off-by: Brijesh Singh <brijesh.singh@xxxxxxx>
---
arch/x86/kvm/svm/sev.c | 39 +++++++++++++++++++++++++++++++++++++++
1 file changed, 39 insertions(+)
diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c
index d97f37df1f3b..4ce91c2583a3 100644
--- a/arch/x86/kvm/svm/sev.c
+++ b/arch/x86/kvm/svm/sev.c
@@ -1920,6 +1920,45 @@ find_enc_region(struct kvm *kvm, struct kvm_enc_region *range)
static void __unregister_enc_region_locked(struct kvm *kvm,
struct enc_region *region)
{
+ struct rmpupdate val = {};
+ unsigned long i, pfn;
+ struct rmpentry *e;
+ int level, rc;
+
+ /*
+ * The guest memory pages are assigned in the RMP table. Unassign it
+ * before releasing the memory.
+ */
+ if (sev_snp_guest(kvm)) {
+ for (i = 0; i < region->npages; i++) {
+ pfn = page_to_pfn(region->pages[i]);
+
+ if (need_resched())
+ schedule();
+
+ e = snp_lookup_page_in_rmptable(region->pages[i], &level);
+ if (unlikely(!e))
+ continue;
+
+ /* If its not a guest assigned page then skip it. */
+ if (!rmpentry_assigned(e))
+ continue;
+
+ /* Is the page part of a 2MB RMP entry? */
+ if (level == PG_LEVEL_2M) {
+ val.pagesize = RMP_PG_SIZE_2M;
+ pfn &= ~(KVM_PAGES_PER_HPAGE(PG_LEVEL_2M) - 1);
+ } else {
+ val.pagesize = RMP_PG_SIZE_4K;
+ }
+
+ /* Transition the page to hypervisor owned. */
+ rc = rmpupdate(pfn_to_page(pfn), &val);
+ if (rc)
+ pr_err("Failed to release pfn 0x%lx ret=%d\n", pfn, rc);
+ }
+ }
+
sev_unpin_memory(kvm, region->pages, region->npages);
list_del(®ion->list);
kfree(region);
--
2.17.1