Skip to content

Commit b299c27

Browse files
committed
KVM: x86/mmu: Move event re-injection unprotect+retry into common path
Move the event re-injection unprotect+retry logic into kvm_mmu_write_protect_fault(), i.e. unprotect and retry if and only if the #PF actually hit a write-protected gfn. Note, there is a small possibility that the gfn was unprotected by a different tasking between hitting the #PF and acquiring mmu_lock, but in that case, KVM will resume the guest immediately anyways because KVM will treat the fault as spurious. As a bonus, unprotecting _after_ handling the page fault also addresses the case where the installing a SPTE to handle fault encounters a shadowed PTE, i.e. *creates* a read-only SPTE. Opportunstically add a comment explaining what on earth the intent of the code is, as based on the changelog from commit 577bdc4 ("KVM: Avoid instruction emulation when event delivery is pending"). Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Sean Christopherson <[email protected]>
1 parent 29e495b commit b299c27

File tree

1 file changed

+9
-21
lines changed

1 file changed

+9
-21
lines changed

arch/x86/kvm/mmu/mmu.c

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2743,23 +2743,6 @@ bool kvm_mmu_unprotect_gfn_and_retry(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa)
27432743
return r;
27442744
}
27452745

2746-
static int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
2747-
{
2748-
gpa_t gpa;
2749-
int r;
2750-
2751-
if (vcpu->arch.mmu->root_role.direct)
2752-
return 0;
2753-
2754-
gpa = kvm_mmu_gva_to_gpa_write(vcpu, gva, NULL);
2755-
if (gpa == INVALID_GPA)
2756-
return 0;
2757-
2758-
r = kvm_mmu_unprotect_page(vcpu->kvm, gpa >> PAGE_SHIFT);
2759-
2760-
return r;
2761-
}
2762-
27632746
static void kvm_unsync_page(struct kvm *kvm, struct kvm_mmu_page *sp)
27642747
{
27652748
trace_kvm_mmu_unsync_page(sp);
@@ -4630,8 +4613,6 @@ int kvm_handle_page_fault(struct kvm_vcpu *vcpu, u64 error_code,
46304613
if (!flags) {
46314614
trace_kvm_page_fault(vcpu, fault_address, error_code);
46324615

4633-
if (kvm_event_needs_reinjection(vcpu))
4634-
kvm_mmu_unprotect_page_virt(vcpu, fault_address);
46354616
r = kvm_mmu_page_fault(vcpu, fault_address, error_code, insn,
46364617
insn_len);
46374618
} else if (flags & KVM_PV_REASON_PAGE_NOT_PRESENT) {
@@ -6037,8 +6018,15 @@ static int kvm_mmu_write_protect_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa,
60376018
* Note, this code also applies to Intel CPUs, even though it is *very*
60386019
* unlikely that an L1 will share its page tables (IA32/PAE/paging64
60396020
* format) with L2's page tables (EPT format).
6040-
*/
6041-
if (direct && is_write_to_guest_page_table(error_code) &&
6021+
*
6022+
* For indirect MMUs, i.e. if KVM is shadowing the current MMU, try to
6023+
* unprotect the gfn and retry if an event is awaiting reinjection. If
6024+
* KVM emulates multiple instructions before completing event injection,
6025+
* the event could be delayed beyond what is architecturally allowed,
6026+
* e.g. KVM could inject an IRQ after the TPR has been raised.
6027+
*/
6028+
if (((direct && is_write_to_guest_page_table(error_code)) ||
6029+
(!direct && kvm_event_needs_reinjection(vcpu))) &&
60426030
kvm_mmu_unprotect_gfn_and_retry(vcpu, cr2_or_gpa))
60436031
return RET_PF_RETRY;
60446032

0 commit comments

Comments
 (0)