Skip to content

Commit f6adeae

Browse files
sean-jcbonzini
authored andcommitted
KVM: x86/mmu: Handle no-slot faults at the beginning of kvm_faultin_pfn()
Handle the "no memslot" case at the beginning of kvm_faultin_pfn(), just after the private versus shared check, so that there's no need to repeatedly query whether or not a slot exists. This also makes it more obvious that, except for private vs. shared attributes, the process of faulting in a pfn simply doesn't apply to gfns without a slot. Opportunistically stuff @fault's metadata in kvm_handle_noslot_fault() so that it doesn't need to be duplicated in all paths that invoke kvm_handle_noslot_fault(), and to minimize the probability of not stuffing the right fields. Leave the existing handle behind, but convert it to a WARN, to guard against __kvm_faultin_pfn() unexpectedly nullifying fault->slot. Cc: David Matlack <[email protected]> Signed-off-by: Sean Christopherson <[email protected]> Reviewed-by: Kai Huang <[email protected]> Message-ID: <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
1 parent cd272fc commit f6adeae

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

arch/x86/kvm/mmu/mmu.c

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3269,6 +3269,10 @@ static int kvm_handle_noslot_fault(struct kvm_vcpu *vcpu,
32693269
vcpu_cache_mmio_info(vcpu, gva, fault->gfn,
32703270
access & shadow_mmio_access_mask);
32713271

3272+
fault->slot = NULL;
3273+
fault->pfn = KVM_PFN_NOSLOT;
3274+
fault->map_writable = false;
3275+
32723276
/*
32733277
* If MMIO caching is disabled, emulate immediately without
32743278
* touching the shadow page tables as attempting to install an
@@ -4349,15 +4353,18 @@ static int kvm_faultin_pfn(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault,
43494353
return -EFAULT;
43504354
}
43514355

4356+
if (unlikely(!slot))
4357+
return kvm_handle_noslot_fault(vcpu, fault, access);
4358+
43524359
/*
43534360
* Retry the page fault if the gfn hit a memslot that is being deleted
43544361
* or moved. This ensures any existing SPTEs for the old memslot will
43554362
* be zapped before KVM inserts a new MMIO SPTE for the gfn.
43564363
*/
4357-
if (slot && (slot->flags & KVM_MEMSLOT_INVALID))
4364+
if (slot->flags & KVM_MEMSLOT_INVALID)
43584365
return RET_PF_RETRY;
43594366

4360-
if (slot && slot->id == APIC_ACCESS_PAGE_PRIVATE_MEMSLOT) {
4367+
if (slot->id == APIC_ACCESS_PAGE_PRIVATE_MEMSLOT) {
43614368
/*
43624369
* Don't map L1's APIC access page into L2, KVM doesn't support
43634370
* using APICv/AVIC to accelerate L2 accesses to L1's APIC,
@@ -4369,12 +4376,9 @@ static int kvm_faultin_pfn(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault,
43694376
* uses different roots for L1 vs. L2, i.e. there is no danger
43704377
* of breaking APICv/AVIC for L1.
43714378
*/
4372-
if (is_guest_mode(vcpu)) {
4373-
fault->slot = NULL;
4374-
fault->pfn = KVM_PFN_NOSLOT;
4375-
fault->map_writable = false;
4376-
goto faultin_done;
4377-
}
4379+
if (is_guest_mode(vcpu))
4380+
return kvm_handle_noslot_fault(vcpu, fault, access);
4381+
43784382
/*
43794383
* If the APIC access page exists but is disabled, go directly
43804384
* to emulation without caching the MMIO access or creating a
@@ -4385,6 +4389,9 @@ static int kvm_faultin_pfn(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault,
43854389
return RET_PF_EMULATE;
43864390
}
43874391

4392+
fault->mmu_seq = vcpu->kvm->mmu_invalidate_seq;
4393+
smp_rmb();
4394+
43884395
/*
43894396
* Check for a relevant mmu_notifier invalidation event before getting
43904397
* the pfn from the primary MMU, and before acquiring mmu_lock.
@@ -4406,19 +4413,17 @@ static int kvm_faultin_pfn(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault,
44064413
* *guaranteed* to need to retry, i.e. waiting until mmu_lock is held
44074414
* to detect retry guarantees the worst case latency for the vCPU.
44084415
*/
4409-
if (fault->slot &&
4410-
mmu_invalidate_retry_gfn_unsafe(vcpu->kvm, fault->mmu_seq, fault->gfn))
4416+
if (mmu_invalidate_retry_gfn_unsafe(vcpu->kvm, fault->mmu_seq, fault->gfn))
44114417
return RET_PF_RETRY;
44124418

44134419
ret = __kvm_faultin_pfn(vcpu, fault);
44144420
if (ret != RET_PF_CONTINUE)
44154421
return ret;
44164422

4417-
faultin_done:
44184423
if (unlikely(is_error_pfn(fault->pfn)))
44194424
return kvm_handle_error_pfn(vcpu, fault);
44204425

4421-
if (unlikely(!fault->slot))
4426+
if (WARN_ON_ONCE(!fault->slot))
44224427
return kvm_handle_noslot_fault(vcpu, fault, access);
44234428

44244429
/*

0 commit comments

Comments
 (0)