Skip to content

KVM: x86: Clean up MSR interception code

Sean Christopherson kvm lkml 2 versions – Applied

Status from this message: Applied to kvm-x86 misc, thanks!

Get series with b4
b4 am [email protected]

Versions newest first · linked by author and title

  1. v2 Latest32 patches 13 repliesApplied
  2. v128 patches 31 repliesSuperseded

Changes in v2 from the cover letter · 17 lines

- Add a patch to set MSR_IA32_SPEC_CTRL interception as appropriate. [Chao]
- Add a patch to cleanup {svm,vmx}_disable_intercept_for_msr() once the dust has settled. [Dapeng]
- Return -ENOSPC if msrpm_offsets[] is full. [Chao]
- Free iopm_pages directly instead of bouncing through iopm_base. [Chao]
- Check for "offset == MSR_INVALID" before using offset. [Chao]
- Temporarily keep MSR_IA32_DEBUGCTLMSR in the nested list. [Chao]
- Add a comment to explain nested_svm_msrpm_merge_offsets. [Chao]
- Add a patch to shift the IOPM allocation to avoid having to unwind it.
- Init nested_svm_msrpm_merge_offsets iff nested=1. [Chao]
- Add a helper to dedup alloc+init of MSRPM and IOPM.
- Tag merge_msrs as "static" and "__initconst". [Paolo]
- Rework helpers to use fewer macros. [Paolo]
- Account for each MSRPM byte covering 4 MSRs. [Paolo]
- Opportunistically use cpu_feature_enabled(). [Xin]
- Fully remove MAX_DIRECT_ACCESS_MSRS, MSRPM_OFFSETS, and msrpm_offsets. [Francesco]
- Fix typos. [Dapeng, Chao]
- Collect reviews. [Chao, Dapeng, Xin]

Patches in v2 Applied

#Subject and filesSizeRepliesApplied
1/32KVM: SVM: Disable interception of SPEC_CTRL iff the MSR exists for the guestarch/x86/kvm/svm/svm.c+6 -32 replies
2/32KVM: SVM: Allocate IOPM pages after initial setup in svm_hardware_setup()arch/x86/kvm/svm/svm.c+9 -90 replies
3/32KVM: SVM: Don't BUG if setting up the MSR intercept bitmaps failsarch/x86/kvm/svm/svm.c+13 -120 replies
4/32KVM: SVM: Tag MSR bitmap initialization helpers with __initarch/x86/kvm/svm/svm.c+2 -20 replies
5/32KVM: SVM: Use ARRAY_SIZE() to iterate over direct_access_msrsarch/x86/kvm/svm/svm.c arch/x86/kvm/svm/svm.h+12 -80 replies
6/32KVM: SVM: Kill the VM instead of the host if MSR interception is buggyarch/x86/kvm/svm/svm.c+6 -41 reply
7/32KVM: x86: Use non-atomic bit ops to manipulate "shadow" MSR interceptsarch/x86/kvm/svm/svm.c arch/x86/kvm/vmx/vmx.c+10 -101 reply
8/32KVM: SVM: Massage name and param of helper that merges vmcb01 and vmcb12 MSRPMsarch/x86/kvm/svm/nested.c arch/x86/kvm/svm/svm.c+8 -91 reply
9/32KVM: SVM: Clean up macros related to architectural MSRPM definitionsarch/x86/kvm/svm/svm.c arch/x86/kvm/svm/svm.h+16 -91 reply
10/32KVM: nSVM: Use dedicated array of MSRPM offsets to merge L0 and L1 bitmapsarch/x86/kvm/svm/nested.c arch/x86/kvm/svm/svm.c arch/x86/kvm/svm/svm.h+78 -110 replies
11/32KVM: nSVM: Omit SEV-ES specific passthrough MSRs from L0+L1 bitmap mergearch/x86/kvm/svm/nested.c+1 -80 replies
12/32KVM: nSVM: Don't initialize vmcb02 MSRPM with vmcb01's "always passthrough"arch/x86/kvm/svm/nested.c arch/x86/kvm/svm/svm.c arch/x86/kvm/svm/svm.h+3 -40 replies
13/32KVM: SVM: Add helpers for accessing MSR bitmap that don't rely on offsetsarch/x86/kvm/svm/svm.c arch/x86/kvm/svm/svm.h+58 -160 replies
14/32KVM: SVM: Implement and adopt VMX style MSR intercepts APIsarch/x86/kvm/svm/sev.c arch/x86/kvm/svm/svm.c arch/x86/kvm/svm/svm.h+90 -391 reply
15/32KVM: SVM: Pass through GHCB MSR if and only if VM is an SEV-ES guestarch/x86/kvm/svm/sev.c arch/x86/kvm/svm/svm.c+3 -20 replies
16/32KVM: SVM: Drop "always" flag from list of possible passthrough MSRsarch/x86/kvm/svm/svm.c+62 -620 replies
17/32KVM: x86: Move definition of X2APIC_MSR() to lapic.harch/x86/kvm/lapic.h arch/x86/kvm/svm/svm.c arch/x86/kvm/vmx/vmx.h+2 -41 reply
18/32KVM: VMX: Manually recalc all MSR intercepts on userspace MSR filter changearch/x86/kvm/vmx/vmx.c arch/x86/kvm/vmx/vmx.h+46 -1441 reply
19/32KVM: SVM: Manually recalc all MSR intercepts on userspace MSR filter changearch/x86/kvm/svm/sev.c arch/x86/kvm/svm/svm.c arch/x86/kvm/svm/svm.h+108 -2910 replies
20/32KVM: x86: Rename msr_filter_changed() => recalc_msr_intercepts()arch/x86/include/asm/kvm-x86-ops.h arch/x86/include/asm/kvm_host.h arch/x86/kvm/svm/svm.c +4 more+15 -201 reply
21/32KVM: SVM: Rename init_vmcb_after_set_cpuid() to make it intercepts specificarch/x86/kvm/svm/svm.c+4 -30 replies
22/32KVM: SVM: Fold svm_vcpu_init_msrpm() into its sole callerarch/x86/kvm/svm/svm.c+11 -160 replies
23/32KVM: SVM: Merge "after set CPUID" intercept recalc helpersarch/x86/kvm/svm/svm.c+7 -110 replies
24/32KVM: SVM: Drop explicit check on MSRPM offset when emulating SEV-ES accessesarch/x86/kvm/svm/svm.c+2 -30 replies
25/32KVM: SVM: Move svm_msrpm_offset() to nested.carch/x86/kvm/svm/nested.c arch/x86/kvm/svm/svm.c arch/x86/kvm/svm/svm.h+23 -240 replies
26/32KVM: SVM: Store MSRPM pointer as "void *" instead of "u32 *"arch/x86/kvm/svm/nested.c arch/x86/kvm/svm/svm.c arch/x86/kvm/svm/svm.h+15 -100 replies
27/32KVM: nSVM: Access MSRPM in 4-byte chunks only for merging L0 and L1 bitmapsarch/x86/kvm/svm/nested.c+18 -340 replies
28/32KVM: SVM: Return -EINVAL instead of MSR_INVALID to signal out-of-range MSRarch/x86/kvm/svm/nested.c arch/x86/kvm/svm/svm.h+9 -110 replies
29/32KVM: nSVM: Merge MSRPM in 64-bit chunks on 64-bit kernelsarch/x86/kvm/svm/nested.c+11 -100 replies
30/32KVM: SVM: Add a helper to allocate and initialize permissions bitmapsarch/x86/kvm/svm/svm.c arch/x86/kvm/svm/svm.h+22 -170 replies
31/32KVM: x86: Simplify userspace filter logic when disabling MSR interceptionarch/x86/kvm/svm/svm.c arch/x86/kvm/vmx/vmx.c+20 -281 reply
32/32KVM: selftests: Verify KVM disable interception (for userspace) on filter changetools/testing/selftests/kvm/x86/userspace_msr_exit_test.c+8 -00 replies

Reviews trailers per patch

10 of 32 patches have a Reviewed-by or Acked-by.

PatchChao GaoXin Li (Intel)Dapeng MiManali ShuklaBinbin Wu
SeriesT-b
1/32
2/32
3/32
4/32R-b
5/32R-b
6/32R-b
7/32R-b
8/32R-b
9/32
10/32
11/32
12/32
13/32
14/32R-b R-b
15/32
16/32
17/32R-b
18/32R-b R-b R-b R-b
19/32
20/32R-b R-b
21/32
22/32
23/32
24/32
25/32
26/32
27/32
28/32
29/32
30/32
31/32R-b
32/32T-b T-b

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes. Scroll the table sideways for all 5 reviewers.

Read the v2 thread

Cover letter [PATCH v2 00/32] KVM: x86: Clean up MSR interception code

Clean up KVM's MSR interception code (especially the SVM code, which is all kinds of ugly). The main goals are to:

- Make the SVM and VMX APIs consistent (and sane; the current SVM APIs have inverted polarity).

- Eliminate the shadow bitmaps that are used to determine intercepts on userspace MSR filter update.

v2:
- Add a patch to set MSR_IA32_SPEC_CTRL interception as appropriate. [Chao]
- Add a patch to cleanup {svm,vmx}_disable_intercept_for_msr() once the dust has settled. [Dapeng]
- Return -ENOSPC if msrpm_offsets[] is full. [Chao]
- Free iopm_pages directly instead of bouncing through iopm_base. [Chao]
- Check for "offset == MSR_INVALID" before using offset. [Chao]
- Temporarily keep MSR_IA32_DEBUGCTLMSR in the nested list. [Chao]
- Add a comment to explain nested_svm_msrpm_merge_offsets. [Chao]
- Add a patch to shift the IOPM allocation to avoid having to unwind it.
- Init nested_svm_msrpm_merge_offsets iff nested=1. [Chao]
- Add a helper to dedup alloc+init of MSRPM and IOPM.
- Tag merge_msrs as "static" and "__initconst". [Paolo]
- Rework helpers to use fewer macros. [Paolo]
- Account for each MSRPM byte covering 4 MSRs. [Paolo]
- Opportunistically use cpu_feature_enabled(). [Xin]
- Fully remove MAX_DIRECT_ACCESS_MSRS, MSRPM_OFFSETS, and msrpm_offsets. [Francesco]
- Fix typos. [Dapeng, Chao]
- Collect reviews. [Chao, Dapeng, Xin]

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

v0: https://lore.kernel.org/kvm/[email protected]

Sean Christopherson (32):
KVM: SVM: Disable interception of SPEC_CTRL iff the MSR exists for the
guest
KVM: SVM: Allocate IOPM pages after initial setup in
svm_hardware_setup()
KVM: SVM: Don't BUG if setting up the MSR intercept bitmaps fails
KVM: SVM: Tag MSR bitmap initialization helpers with __init
KVM: SVM: Use ARRAY_SIZE() to iterate over direct_access_msrs
KVM: SVM: Kill the VM instead of the host if MSR interception is buggy
KVM: x86: Use non-atomic bit ops to manipulate "shadow" MSR intercepts
KVM: SVM: Massage name and param of helper that merges vmcb01 and
vmcb12 MSRPMs
KVM: SVM: Clean up macros related to architectural MSRPM definitions
KVM: nSVM: Use dedicated array of MSRPM offsets to merge L0 and L1
bitmaps
KVM: nSVM: Omit SEV-ES specific passthrough MSRs from L0+L1 bitmap
merge
KVM: nSVM: Don't initialize vmcb02 MSRPM with vmcb01's "always
passthrough"
KVM: SVM: Add helpers for accessing MSR bitmap that don't rely on
offsets
KVM: SVM: Implement and adopt VMX style MSR intercepts APIs
KVM: SVM: Pass through GHCB MSR if and only if VM is an SEV-ES guest
KVM: SVM: Drop "always" flag from list of possible passthrough MSRs
KVM: x86: Move definition of X2APIC_MSR() to lapic.h
KVM: VMX: Manually recalc all MSR intercepts on userspace MSR filter
change
KVM: SVM: Manually recalc all MSR intercepts on userspace MSR filter
change
KVM: x86: Rename msr_filter_changed() => recalc_msr_intercepts()
KVM: SVM: Rename init_vmcb_after_set_cpuid() to make it intercepts
specific
KVM: SVM: Fold svm_vcpu_init_msrpm() into its sole caller
KVM: SVM: Merge "after set CPUID" intercept recalc helpers
KVM: SVM: Drop explicit check on MSRPM offset when emulating SEV-ES
accesses
KVM: SVM: Move svm_msrpm_offset() to nested.c
KVM: SVM: Store MSRPM pointer as "void *" instead of "u32 *"
KVM: nSVM: Access MSRPM in 4-byte chunks only for merging L0 and L1
bitmaps
KVM: SVM: Return -EINVAL instead of MSR_INVALID to signal out-of-range
MSR
KVM: nSVM: Merge MSRPM in 64-bit chunks on 64-bit kernels
KVM: SVM: Add a helper to allocate and initialize permissions bitmaps
KVM: x86: Simplify userspace filter logic when disabling MSR
interception
KVM: selftests: Verify KVM disable interception (for userspace) on
filter change

 arch/x86/include/asm/kvm-x86-ops.h            |   2 +-
 arch/x86/include/asm/kvm_host.h               |   2 +-
 arch/x86/kvm/lapic.h                          |   2 +
 arch/x86/kvm/svm/nested.c                     | 126 +++--
 arch/x86/kvm/svm/sev.c                        |  29 +-
 arch/x86/kvm/svm/svm.c                        | 490 ++++++------------
 arch/x86/kvm/svm/svm.h                        | 102 +++-
 arch/x86/kvm/vmx/main.c                       |   6 +-
 arch/x86/kvm/vmx/vmx.c                        | 202 ++------
 arch/x86/kvm/vmx/vmx.h                        |   9 -
 arch/x86/kvm/vmx/x86_ops.h                    |   2 +-
 arch/x86/kvm/x86.c                            |   8 +-
 .../kvm/x86/userspace_msr_exit_test.c         |   8 +
 13 files changed, 426 insertions(+), 562 deletions(-)

base-commit: 61374cc145f4a56377eaf87c7409a97ec7a34041

Signature · 1 line
2.50.0.rc0.642.g800a2b2222-goog