Skip to content

Commit f32fb32

Browse files
mdrothbonzini
authored andcommitted
KVM: x86: Add hook for determining max NPT mapping level
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. Add a hook to determine the max NPT mapping size in situations like this. Suggested-by: Sean Christopherson <seanjc@google.com> Signed-off-by: Michael Roth <michael.roth@amd.com> Reviewed-by: Isaku Yamahata <isaku.yamahata@intel.com> Message-ID: <20240501085210.2213060-3-michael.roth@amd.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
1 parent a90764f commit f32fb32

3 files changed

Lines changed: 23 additions & 2 deletions

File tree

arch/x86/include/asm/kvm-x86-ops.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ KVM_X86_OP_OPTIONAL_RET0(vcpu_get_apicv_inhibit_reasons);
140140
KVM_X86_OP_OPTIONAL(get_untagged_addr)
141141
KVM_X86_OP_OPTIONAL(alloc_apic_backing_page)
142142
KVM_X86_OP_OPTIONAL_RET0(gmem_prepare)
143+
KVM_X86_OP_OPTIONAL_RET0(private_max_mapping_level)
143144
KVM_X86_OP_OPTIONAL(gmem_invalidate)
144145

145146
#undef KVM_X86_OP

arch/x86/include/asm/kvm_host.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1814,6 +1814,7 @@ struct kvm_x86_ops {
18141814
void *(*alloc_apic_backing_page)(struct kvm_vcpu *vcpu);
18151815
int (*gmem_prepare)(struct kvm *kvm, kvm_pfn_t pfn, gfn_t gfn, int max_order);
18161816
void (*gmem_invalidate)(kvm_pfn_t start, kvm_pfn_t end);
1817+
int (*private_max_mapping_level)(struct kvm *kvm, kvm_pfn_t pfn);
18171818
};
18181819

18191820
struct kvm_x86_nested_ops {

arch/x86/kvm/mmu/mmu.c

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4270,6 +4270,25 @@ static inline u8 kvm_max_level_for_order(int order)
42704270
return PG_LEVEL_4K;
42714271
}
42724272

4273+
static u8 kvm_max_private_mapping_level(struct kvm *kvm, kvm_pfn_t pfn,
4274+
u8 max_level, int gmem_order)
4275+
{
4276+
u8 req_max_level;
4277+
4278+
if (max_level == PG_LEVEL_4K)
4279+
return PG_LEVEL_4K;
4280+
4281+
max_level = min(kvm_max_level_for_order(gmem_order), max_level);
4282+
if (max_level == PG_LEVEL_4K)
4283+
return PG_LEVEL_4K;
4284+
4285+
req_max_level = static_call(kvm_x86_private_max_mapping_level)(kvm, pfn);
4286+
if (req_max_level)
4287+
max_level = min(max_level, req_max_level);
4288+
4289+
return req_max_level;
4290+
}
4291+
42734292
static int kvm_faultin_pfn_private(struct kvm_vcpu *vcpu,
42744293
struct kvm_page_fault *fault)
42754294
{
@@ -4287,9 +4306,9 @@ static int kvm_faultin_pfn_private(struct kvm_vcpu *vcpu,
42874306
return r;
42884307
}
42894308

4290-
fault->max_level = min(kvm_max_level_for_order(max_order),
4291-
fault->max_level);
42924309
fault->map_writable = !(fault->slot->flags & KVM_MEM_READONLY);
4310+
fault->max_level = kvm_max_private_mapping_level(vcpu->kvm, fault->pfn,
4311+
fault->max_level, max_order);
42934312

42944313
return RET_PF_CONTINUE;
42954314
}

0 commit comments

Comments
 (0)