Skip to content

KVM: x86: Fix multiple #PF RO infinite loop bugs

Sean Christopherson kvm lkml 2 versions – Applied

Status from this message: Applied to kvm-x86 mmu, except for patch 1, which I put at the end of "vmx" in

Get series with b4
b4 am [email protected]

Versions newest first · linked by author and title

  1. v2 Latest22 patches 3 repliesApplied
  2. v122 patches 30 repliesSuperseded

Changes in v2 from the cover letter

- Gather reviews.
- Rewrite the comment for the nested NPT "fast" unprotect path to explain what it actually handles. [Yuan]
- Drop Cc: stable for the remaining patches, as it's extremely unlikely these fixes would actually prevent a guest from dying. [Paolo]
- Move the patch to replace PFERR_NESTED_GUEST_PAGE much earlier. [Paolo]
- Wrap indirect_shadow_pages with READ_ONCE(). [Paolo]
- Massage a few changelogs to (hopefully) fix weird wordings. [Paolo]
- Always refer directly to last_retry_{addr,eip} in comments, instead of indirectly referencing them as "infinite loop protection". [Paolo]
- WARN if write-protection hits the MMIO cache. [Yuan]

Patches in v2 Applied

#Subject and filesSizeRepliesApplied
1/22KVM: VMX: Set PFERR_GUEST_{FINAL,PAGE}_MASK if and only if the GVA is validarch/x86/kvm/vmx/vmx.c+3 -20 replies
2/22KVM: x86/mmu: Replace PFERR_NESTED_GUEST_PAGE with a more descriptive helperarch/x86/include/asm/kvm_host.h arch/x86/kvm/mmu/mmu.c+8 -50 replies
3/22KVM: x86/mmu: Trigger unprotect logic only on write-protection page faultsarch/x86/kvm/mmu/mmu.c arch/x86/kvm/mmu/mmu_internal.h arch/x86/kvm/mmu/mmutrace.h +2 more+50 -370 replies
4/22KVM: x86/mmu: Skip emulation on page fault iff 1+ SPs were unprotectedarch/x86/kvm/mmu/mmu.c+29 -81 reply
5/22KVM: x86: Retry to-be-emulated insn in "slow" unprotect path iff sp is zappedarch/x86/kvm/x86.c+6 -61 reply
6/22KVM: x86: Get RIP from vCPU state when storing it to last_retry_eiparch/x86/kvm/x86.c+1 -10 replies
7/22KVM: x86: Store gpa as gpa_t, not unsigned long, when unprotecting for retryarch/x86/kvm/x86.c+2 -10 replies
8/22KVM: x86/mmu: Apply retry protection to "fast nTDP unprotect" patharch/x86/include/asm/kvm_host.h arch/x86/kvm/mmu/mmu.c arch/x86/kvm/x86.c+40 -270 replies
9/22KVM: x86/mmu: Try "unprotect for retry" iff there are indirect SPsarch/x86/kvm/mmu/mmu.c+11 -00 replies
10/22KVM: x86: Move EMULTYPE_ALLOW_RETRY_PF to x86_emulate_instruction()arch/x86/kvm/x86.c+5 -80 replies
11/22KVM: x86: Fold retry_instruction() into x86_emulate_instruction()arch/x86/kvm/x86.c+9 -210 replies
12/22KVM: x86/mmu: Don't try to unprotect an INVALID_GPAarch/x86/kvm/mmu/mmu.c+6 -10 replies
13/22KVM: x86/mmu: Always walk guest PTEs with WRITE access when unprotectingarch/x86/kvm/mmu/mmu.c+1 -10 replies
14/22KVM: x86/mmu: Move event re-injection unprotect+retry into common patharch/x86/kvm/mmu/mmu.c+8 -200 replies
15/22KVM: x86: Remove manual pfn lookup when retrying #PF after failed emulationarch/x86/kvm/x86.c+0 -180 replies
16/22KVM: x86: Check EMULTYPE_WRITE_PF_TO_SP before unprotecting gfnarch/x86/kvm/x86.c+19 -90 replies
17/22KVM: x86: Apply retry protection to "unprotect on failure" patharch/x86/kvm/x86.c+1 -190 replies
18/22KVM: x86: Update retry protection fields when forcing retry on emulation failurearch/x86/include/asm/kvm_host.h arch/x86/kvm/mmu/mmu.c arch/x86/kvm/x86.c+17 -70 replies
19/22KVM: x86: Rename reexecute_instruction()=>kvm_unprotect_and_retry_on_failure()arch/x86/kvm/x86.c+7 -50 replies
20/22KVM: x86/mmu: Subsume kvm_mmu_unprotect_page() into the and_retry() versionarch/x86/include/asm/kvm_host.h arch/x86/kvm/mmu/mmu.c+13 -210 replies
21/22KVM: x86/mmu: Detect if unprotect will do anything based on invalid_listarch/x86/kvm/mmu/mmu.c+7 -40 replies
22/22KVM: x86/mmu: WARN on MMIO cache hit when emulating write-protected gfnarch/x86/kvm/mmu/mmu.c+19 -90 replies

Reviews trailers per patch

4 of 22 patches have a Reviewed-by or Acked-by.

PatchYuan Yao
1/22R-b
2/22
3/22R-b
4/22R-b
5/22R-b
6/22
7/22
8/22
9/22
10/22
11/22
12/22
13/22
14/22
15/22
16/22
17/22
18/22
19/22
20/22
21/22
22/22

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes.

Read the v2 thread

Cover letter [PATCH v2 00/22] KVM: x86: Fix multiple #PF RO infinite loop bugs

Fix an amusing number of minor bugs that can lead to KVM putting the guest into an infinite "retry #PF" loop, and cleanup and consolidate the unprotect+retry paths (there are four-ish).

As a bonus, adding RET_PF_WRITE_PROTECTED obviates the need for kvm_lookup_pfn()[*].

[*] https://lore.kernel.org/all/[email protected]

v2:
- Gather reviews.
- Rewrite the comment for the nested NPT "fast" unprotect path to explain what it actually handles. [Yuan]
- Drop Cc: stable for the remaining patches, as it's extremely unlikely these fixes would actually prevent a guest from dying. [Paolo]
- Move the patch to replace PFERR_NESTED_GUEST_PAGE much earlier. [Paolo]
- Wrap indirect_shadow_pages with READ_ONCE(). [Paolo]
- Massage a few changelogs to (hopefully) fix weird wordings. [Paolo]
- Always refer directly to last_retry_{addr,eip} in comments, instead of indirectly referencing them as "infinite loop protection". [Paolo]
- WARN if write-protection hits the MMIO cache. [Yuan]

v1: https://lore.kernel.org/all/[email protected]

Sean Christopherson (22):
KVM: VMX: Set PFERR_GUEST_{FINAL,PAGE}_MASK if and only if the GVA is
valid
KVM: x86/mmu: Replace PFERR_NESTED_GUEST_PAGE with a more descriptive
helper
KVM: x86/mmu: Trigger unprotect logic only on write-protection page
faults
KVM: x86/mmu: Skip emulation on page fault iff 1+ SPs were unprotected
KVM: x86: Retry to-be-emulated insn in "slow" unprotect path iff sp is
zapped
KVM: x86: Get RIP from vCPU state when storing it to last_retry_eip
KVM: x86: Store gpa as gpa_t, not unsigned long, when unprotecting for
retry
KVM: x86/mmu: Apply retry protection to "fast nTDP unprotect" path
KVM: x86/mmu: Try "unprotect for retry" iff there are indirect SPs
KVM: x86: Move EMULTYPE_ALLOW_RETRY_PF to x86_emulate_instruction()
KVM: x86: Fold retry_instruction() into x86_emulate_instruction()
KVM: x86/mmu: Don't try to unprotect an INVALID_GPA
KVM: x86/mmu: Always walk guest PTEs with WRITE access when
unprotecting
KVM: x86/mmu: Move event re-injection unprotect+retry into common path
KVM: x86: Remove manual pfn lookup when retrying #PF after failed
emulation
KVM: x86: Check EMULTYPE_WRITE_PF_TO_SP before unprotecting gfn
KVM: x86: Apply retry protection to "unprotect on failure" path
KVM: x86: Update retry protection fields when forcing retry on
emulation failure
KVM: x86: Rename
reexecute_instruction()=>kvm_unprotect_and_retry_on_failure()
KVM: x86/mmu: Subsume kvm_mmu_unprotect_page() into the and_retry()
version
KVM: x86/mmu: Detect if unprotect will do anything based on
invalid_list
KVM: x86/mmu: WARN on MMIO cache hit when emulating write-protected
gfn

 arch/x86/include/asm/kvm_host.h |  14 ++-
 arch/x86/kvm/mmu/mmu.c          | 200 +++++++++++++++++++++++---------
 arch/x86/kvm/mmu/mmu_internal.h |   3 +
 arch/x86/kvm/mmu/mmutrace.h     |   1 +
 arch/x86/kvm/mmu/paging_tmpl.h  |   2 +-
 arch/x86/kvm/mmu/tdp_mmu.c      |   6 +-
 arch/x86/kvm/vmx/vmx.c          |   5 +-
 arch/x86/kvm/x86.c              | 133 ++++++---------------
 8 files changed, 198 insertions(+), 166 deletions(-)

base-commit: a1206bc992c3cd3f758a9b46117dfc7e59e8c10f

Signature · 1 line
2.46.0.469.g59c65b2a67-goog