Skip to content

Commit 6e01b76

Browse files
committed
KVM: x86: Implement kvm_arch_vcpu_pre_fault_memory()
Wire KVM_PRE_FAULT_MEMORY ioctl to kvm_mmu_do_page_fault() to populate guest memory. It can be called right after KVM_CREATE_VCPU creates a vCPU, since at that point kvm_mmu_create() and kvm_init_mmu() are called and the vCPU is ready to invoke the KVM page fault handler. The helper function kvm_tdp_map_page() takes care of the logic to process RET_PF_* return values and convert them to success or errno. Signed-off-by: Isaku Yamahata <[email protected]> Message-ID: <9b866a0ae7147f96571c439e75429a03dcb659b6.1712785629.git.isaku.yamahata@intel.com> Signed-off-by: Paolo Bonzini <[email protected]>
1 parent 58ef246 commit 6e01b76

File tree

3 files changed

+77
-0
lines changed

3 files changed

+77
-0
lines changed

arch/x86/kvm/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ config KVM
4444
select KVM_VFIO
4545
select HAVE_KVM_PM_NOTIFIER if PM
4646
select KVM_GENERIC_HARDWARE_ENABLING
47+
select KVM_GENERIC_PRE_FAULT_MEMORY
4748
select KVM_WERROR if WERROR
4849
help
4950
Support hosting fully virtualized guest machines using hardware

arch/x86/kvm/mmu/mmu.c

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4709,6 +4709,79 @@ int kvm_tdp_page_fault(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)
47094709
return direct_page_fault(vcpu, fault);
47104710
}
47114711

4712+
static int kvm_tdp_map_page(struct kvm_vcpu *vcpu, gpa_t gpa, u64 error_code,
4713+
u8 *level)
4714+
{
4715+
int r;
4716+
4717+
/*
4718+
* Restrict to TDP page fault, since that's the only case where the MMU
4719+
* is indexed by GPA.
4720+
*/
4721+
if (vcpu->arch.mmu->page_fault != kvm_tdp_page_fault)
4722+
return -EOPNOTSUPP;
4723+
4724+
do {
4725+
if (signal_pending(current))
4726+
return -EINTR;
4727+
cond_resched();
4728+
r = kvm_mmu_do_page_fault(vcpu, gpa, error_code, true, NULL, level);
4729+
} while (r == RET_PF_RETRY);
4730+
4731+
if (r < 0)
4732+
return r;
4733+
4734+
switch (r) {
4735+
case RET_PF_FIXED:
4736+
case RET_PF_SPURIOUS:
4737+
return 0;
4738+
4739+
case RET_PF_EMULATE:
4740+
return -ENOENT;
4741+
4742+
case RET_PF_RETRY:
4743+
case RET_PF_CONTINUE:
4744+
case RET_PF_INVALID:
4745+
default:
4746+
WARN_ONCE(1, "could not fix page fault during prefault");
4747+
return -EIO;
4748+
}
4749+
}
4750+
4751+
long kvm_arch_vcpu_pre_fault_memory(struct kvm_vcpu *vcpu,
4752+
struct kvm_pre_fault_memory *range)
4753+
{
4754+
u64 error_code = PFERR_GUEST_FINAL_MASK;
4755+
u8 level = PG_LEVEL_4K;
4756+
u64 end;
4757+
int r;
4758+
4759+
/*
4760+
* reload is efficient when called repeatedly, so we can do it on
4761+
* every iteration.
4762+
*/
4763+
kvm_mmu_reload(vcpu);
4764+
4765+
if (kvm_arch_has_private_mem(vcpu->kvm) &&
4766+
kvm_mem_is_private(vcpu->kvm, gpa_to_gfn(range->gpa)))
4767+
error_code |= PFERR_PRIVATE_ACCESS;
4768+
4769+
/*
4770+
* Shadow paging uses GVA for kvm page fault, so restrict to
4771+
* two-dimensional paging.
4772+
*/
4773+
r = kvm_tdp_map_page(vcpu, range->gpa, error_code, &level);
4774+
if (r < 0)
4775+
return r;
4776+
4777+
/*
4778+
* If the mapping that covers range->gpa can use a huge page, it
4779+
* may start below it or end after range->gpa + range->size.
4780+
*/
4781+
end = (range->gpa & KVM_HPAGE_MASK(level)) + KVM_HPAGE_SIZE(level);
4782+
return min(range->size, end - range->gpa);
4783+
}
4784+
47124785
static void nonpaging_init_context(struct kvm_mmu *context)
47134786
{
47144787
context->page_fault = nonpaging_page_fault;

arch/x86/kvm/x86.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4705,6 +4705,9 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
47054705
case KVM_CAP_MEMORY_FAULT_INFO:
47064706
r = 1;
47074707
break;
4708+
case KVM_CAP_PRE_FAULT_MEMORY:
4709+
r = tdp_enabled;
4710+
break;
47084711
case KVM_CAP_EXIT_HYPERCALL:
47094712
r = KVM_EXIT_HYPERCALL_VALID_MASK;
47104713
break;

0 commit comments

Comments
 (0)