Skip to content

Commit

Permalink
KVM: SEV: Fix return code interpretation for RMP nested page faults
Browse files Browse the repository at this point in the history
The intended logic when handling #NPFs with the RMP bit set (31) is to
first check to see if the #NPF requires a shared<->private transition
and, if so, to go ahead and let the corresponding KVM_EXIT_MEMORY_FAULT
get forwarded on to userspace before proceeding with any handling of
other potential RMP fault conditions like needing to PSMASH the RMP
entry/etc (which will be done later if the guest still re-faults after
the KVM_EXIT_MEMORY_FAULT is processed by userspace).

The determination of whether any userspace handling of
KVM_EXIT_MEMORY_FAULT is needed is done by interpreting the return code
of kvm_mmu_page_fault(). However, the current code misinterprets the
return code, expecting 0 to indicate a userspace exit rather than less
than or equal to 0 (-EFAULT in the above case). This leads to the
following unexpected behavior:

  - for KVM_EXIT_MEMORY_FAULTs resulting for implicit shared->private
    conversions, warnings get printed from sev_handle_rmp_fault()
    because it does not expect to be called for GPAs where
    KVM_MEMORY_ATTRIBUTE_PRIVATE is not set. Standard linux guests don't
    generally do this, but it is allowed and should be handled
    similarly to private->shared conversions rather than triggering any
    sort of warnings

  - if gmem support for 2MB folios is enabled (via currently out-of-tree
    code), implicit shared<->private conversions will always result in
    a PSMASH being attempted, even if it's not actually needed to
    resolve the RMP fault. This doesn't cause any harm, but results in a
    needless PSMASH and zapping of the sPTE

Resolve these issues by calling sev_handle_rmp_fault() only when
kvm_mmu_page_fault()'s return code is greater than 0, indicating a
KVM_MEMORY_EXIT_FAULT/-EFAULT isn't needed. While here, simplify the
code slightly and remove the outdated comment.

Fixes: ccc9d83 ("KVM: SEV: Add support to handle RMP nested page faults")
Signed-off-by: Michael Roth <michael.roth@amd.com>
  • Loading branch information
mdroth committed May 10, 2024
1 parent b74e4f8 commit 0a0ba0d
Showing 1 changed file with 1 addition and 8 deletions.
9 changes: 1 addition & 8 deletions arch/x86/kvm/svm/svm.c
Original file line number Diff line number Diff line change
Expand Up @@ -2069,15 +2069,8 @@ static int npf_interception(struct kvm_vcpu *vcpu)
svm->vmcb->control.insn_bytes : NULL,
svm->vmcb->control.insn_len);

/*
* rc == 0 indicates a userspace exit is needed to handle page
* transitions, so do that first before updating the RMP table.
*/
if (error_code & PFERR_GUEST_RMP_MASK) {
if (rc == 0)
return rc;
if (rc > 0 && error_code & PFERR_GUEST_RMP_MASK)
sev_handle_rmp_fault(vcpu, fault_address, error_code);
}

return rc;
}
Expand Down

0 comments on commit 0a0ba0d

Please sign in to comment.