Skip to content

KVM: SVM: Fix soft int/ex re-injection

Sean Christopherson kvm lkml 2 versions – In review

Get series with b4
b4 am [email protected]

Versions newest first · linked by author and title

  1. v2 Latest11 patches 18 repliesIn review
  2. v18 patches 36 repliesSuperseded

Changes in v2 from the cover letter

- Collect reviews. [Maxim]
- Drop a stale comment midway through. [Paolo]
- Correctly handle (at least as correctly as SVM allows) the scenario where an injected soft interrupt/exception has no backing insn. [Maxim]
- Tag reinjected exceptions in the tracepoint. [Maxim]
- Use the correct L2 RIP (hopefully) in svm_set_nested_state. [Maciej]
- Fix a BUG that can be triggered by userspace.
- Fix the error code FIXME in the exception tracepoint.
- Differentiate soft vs. hard "IRQ" injection in tracepoint.
- Assert that the first soft int is injected on the correct RIP in the selftest.

Patches in v2 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/11KVM: nSVM: Sync next_rip field from vmcb12 to vmcb02arch/x86/kvm/svm/nested.c arch/x86/kvm/svm/svm.h+20 -31 reply
2/11KVM: SVM: Don't BUG if userspace injects a soft interrupt with GIF=0arch/x86/kvm/svm/svm.c+1 -15 replies
3/11KVM: SVM: Unwind "speculative" RIP advancement if INTn injection "fails"arch/x86/kvm/svm/svm.c+15 -80 replies
4/11KVM: SVM: Stuff next_rip on emulated INT3 injection if NRIPS is supportedarch/x86/kvm/svm/svm.c+5 -10 replies
5/11KVM: SVM: Re-inject INT3/INTO instead of retrying the instructionarch/x86/kvm/svm/nested.c arch/x86/kvm/svm/svm.c arch/x86/kvm/svm/svm.h+130 -444 replies
6/11KVM: SVM: Re-inject INTn instead of retrying the insn on "failure"arch/x86/kvm/svm/nested.c arch/x86/kvm/svm/svm.c+22 -90 replies
7/11KVM: x86: Trace re-injected exceptionsarch/x86/kvm/trace.h arch/x86/kvm/x86.c+17 -111 reply
8/11KVM: x86: Print error code in exception injection tracepoint iff validarch/x86/kvm/trace.h+4 -31 reply
9/11KVM: x86: Differentiate Soft vs. Hard IRQs vs. reinjected in tracepointarch/x86/include/asm/kvm_host.h arch/x86/kvm/svm/svm.c arch/x86/kvm/trace.h +2 more+19 -121 reply
10/11KVM: selftests: nSVM: Add svm_nested_soft_inject_testtools/testing/selftests/kvm/.gitignore tools/testing/selftests/kvm/Makefile tools/testing/selftests/kvm/include/x86_64/svm_util.h +1 more+156 -11 reply
11/11KVM: SVM: Drop support for CPUs without NRIPS (NextRIP Save) supportarch/x86/kvm/svm/nested.c arch/x86/kvm/svm/svm.c tools/testing/selftests/kvm/x86_64/svm_nested_soft_inject_test.c+32 -602 replies

Reviews trailers per patch

6 of 11 patches have a Reviewed-by or Acked-by.

PatchMaxim LevitskySean Christopherson
1/11R-b
2/11
3/11R-b
4/11R-b
5/11
6/11
7/11R-b
8/11R-b
9/11
10/11R-b
11/11Not-signed-off-by

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/11] KVM: SVM: Fix soft int/ex re-injection

Fix soft interrupt/exception reinjection on SVM.

The underlying issue is that SVM simply retries INT* instructions instead of reinjecting the soft interupt/exception if an exception VM-Exit occurred during vectoring. Lack of reinjection breaks nested virtualization if the injected event came from L1 and the VM-Exit is not forwarded to L1, as there is no instruction to retry. More fundamentally, retrying the instruction is wrong as it can produce side effects that shouldn't occur, e.g. code #DBs.

VMX has been fixed since commit 66fd3f7f901f ("KVM: Do not re-execute INTn instruction."), but SVM was left behind. Probably because fixing SVM is a mess due to NRIPS not being supported on all architectures, and due to it being poorly implemented (with respect to soft events) when it is supported.

Opportunistically clean up related tracepoints to make debugging related issues less painful in the future.

The last patch is not-signed-off-by as I think it needs broader review and feedback before KVM drops support for CPUs that are old, but not thaaaaat old.

The tracepoint output looks like:

kvm_inj_exception: #GP (0x0)
kvm_inj_exception: #UD
kvm_inj_exception: #DE
kvm_inj_exception: #DE [reinjected]
kvm_inj_exception: #BP [reinjected]
kvm_inj_exception: #NP (0x18) [reinjected]

and for "irqs":

    kvm_inj_virq: Soft/INTn 0x20 [reinjected]
    kvm_inj_virq: Soft/INTn 0x19 [reinjected]
    kvm_inj_virq: IRQ 0x20
    kvm_inj_virq: IRQ 0xf1

v2:
- Collect reviews. [Maxim]
- Drop a stale comment midway through. [Paolo]
- Correctly handle (at least as correctly as SVM allows) the scenario where an injected soft interrupt/exception has no backing insn. [Maxim]
- Tag reinjected exceptions in the tracepoint. [Maxim]
- Use the correct L2 RIP (hopefully) in svm_set_nested_state. [Maciej]
- Fix a BUG that can be triggered by userspace.
- Fix the error code FIXME in the exception tracepoint.
- Differentiate soft vs. hard "IRQ" injection in tracepoint.
- Assert that the first soft int is injected on the correct RIP in the selftest.

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

Maciej's original series:
https://lore.kernel.org/all/[email protected]

Maciej S. Szmigiero (3):
KVM: nSVM: Sync next_rip field from vmcb12 to vmcb02
KVM: SVM: Don't BUG if userspace injects a soft interrupt with GIF=0
KVM: selftests: nSVM: Add svm_nested_soft_inject_test

Sean Christopherson (8):
KVM: SVM: Unwind "speculative" RIP advancement if INTn injection

    "fails"

KVM: SVM: Stuff next_rip on emulated INT3 injection if NRIPS is
supported
KVM: SVM: Re-inject INT3/INTO instead of retrying the instruction
KVM: SVM: Re-inject INTn instead of retrying the insn on "failure"
KVM: x86: Trace re-injected exceptions
KVM: x86: Print error code in exception injection tracepoint iff valid
KVM: x86: Differentiate Soft vs. Hard IRQs vs. reinjected in
tracepoint
KVM: SVM: Drop support for CPUs without NRIPS (NextRIP Save) support

 arch/x86/include/asm/kvm_host.h               |   2 +-
 arch/x86/kvm/svm/nested.c                     |  46 ++++-
 arch/x86/kvm/svm/svm.c                        | 169 ++++++++++++------
 arch/x86/kvm/svm/svm.h                        |   7 +-
 arch/x86/kvm/trace.h                          |  31 ++--
 arch/x86/kvm/vmx/vmx.c                        |   4 +-
 arch/x86/kvm/x86.c                            |  20 ++-
 tools/testing/selftests/kvm/.gitignore        |   3 +-
 tools/testing/selftests/kvm/Makefile          |   1 +
 .../selftests/kvm/include/x86_64/svm_util.h   |   2 +
 .../kvm/x86_64/svm_nested_soft_inject_test.c  | 149 +++++++++++++++
 11 files changed, 351 insertions(+), 83 deletions(-)
 create mode 100644 tools/testing/selftests/kvm/x86_64/svm_nested_soft_inject_test.c

base-commit: 2a39d8b39bffdaf1a4223d0d22f07baee154c8f3

Signature · 1 line
2.36.0.rc2.479.g8af0fa9b8e-goog