@@ -4557,3 +4557,67 @@ int sev_update_mem_attr(struct kvm_memory_slot *slot, unsigned int attr,
return 0;
}
+
+void sev_invalidate_private_range(struct kvm_memory_slot *slot, gfn_t start, gfn_t end)
+{
+ gfn_t gfn = start;
+
+ if (!sev_snp_guest(slot->kvm))
+ return;
+
+ if (!kvm_slot_can_be_private(slot)) {
+ pr_warn_ratelimited("SEV: Memslot for GFN: 0x%llx is not private.\n",
+ gfn);
+ return;
+ }
+
+ while (gfn < end) {
+ gpa_t gpa = gfn_to_gpa(gfn);
+ int level = PG_LEVEL_4K;
+ int order, rc;
+ kvm_pfn_t pfn;
+
+ if (!kvm_mem_is_private(slot->kvm, gfn)) {
+ gfn++;
+ continue;
+ }
+
+ rc = kvm_restricted_mem_get_pfn(slot, gfn, &pfn, &order);
+ if (rc) {
+ pr_warn_ratelimited("SEV: Failed to retrieve restricted PFN for GFN 0x%llx, rc: %d\n",
+ gfn, rc);
+ gfn++;
+ continue;
+ }
+
+ if (order) {
+ int rmp_level;
+
+ if (IS_ALIGNED(gpa, page_level_size(PG_LEVEL_2M)) &&
+ gpa + page_level_size(PG_LEVEL_2M) <= gfn_to_gpa(end))
+ level = PG_LEVEL_2M;
+ else
+ pr_debug("%s: GPA 0x%llx is not aligned to 2M, skipping 2M directmap restoration\n",
+ __func__, gpa);
+
+ /* TODO: It may still be possible to restore 2M mapping here, but keep it simple for now. */
+ if (level == PG_LEVEL_2M &&
+ (!snp_lookup_rmpentry(pfn, &rmp_level) || rmp_level == PG_LEVEL_4K)) {
+ pr_debug("%s: PFN 0x%llx is not mapped as 2M private range, skipping 2M directmap restoration\n",
+ __func__, pfn);
+ level = PG_LEVEL_4K;
+ }
+ }
+
+ pr_debug("%s: GPA %llx PFN %llx order %d level %d\n",
+ __func__, gpa, pfn, order, level);
+ rc = snp_make_page_shared(slot->kvm, gpa, pfn, level);
+ if (rc)
+ pr_err("SEV: Failed to restore page to shared, GPA: 0x%llx PFN: 0x%llx order: %d rc: %d\n",
+ gpa, pfn, order, rc);
+
+ gfn += page_level_size(level) >> PAGE_SHIFT;
+ put_page(pfn_to_page(pfn));
+ cond_resched();
+ }
+}
@@ -4873,6 +4873,7 @@ static struct kvm_x86_ops svm_x86_ops __initdata = {
.rmp_page_level_adjust = sev_rmp_page_level_adjust,
.update_mem_attr = sev_update_mem_attr,
+ .invalidate_restricted_mem = sev_invalidate_private_range,
};
/*
@@ -743,6 +743,7 @@ void handle_rmp_page_fault(struct kvm_vcpu *vcpu, gpa_t gpa, u64 error_code);
void sev_snp_init_protected_guest_state(struct kvm_vcpu *vcpu);
int sev_update_mem_attr(struct kvm_memory_slot *slot, unsigned int attr,
gfn_t start, gfn_t end);
+void sev_invalidate_private_range(struct kvm_memory_slot *slot, gfn_t start, gfn_t end);
int sev_fault_is_private(struct kvm *kvm, gpa_t gpa, u64 error_code, bool *private_fault);
Implement a platform hook to do the work of restoring the direct map entries and cleaning up RMP table entries for restricted memory that is being freed back to the host. Signed-off-by: Michael Roth <michael.roth@amd.com> --- arch/x86/kvm/svm/sev.c | 64 ++++++++++++++++++++++++++++++++++++++++++ arch/x86/kvm/svm/svm.c | 1 + arch/x86/kvm/svm/svm.h | 1 + 3 files changed, 66 insertions(+)