Skip to content

Commit 6057497

Browse files
committed
KVM: x86: Rework KVM_REQ_MSR_FILTER_CHANGED into a generic RECALC_INTERCEPTS
Rework the MSR_FILTER_CHANGED request into a more generic RECALC_INTERCEPTS request, and expand the responsibilities of vendor code to recalculate all intercepts that vary based on userspace input, e.g. instruction intercepts that are tied to guest CPUID. Providing a generic recalc request will allow the upcoming mediated PMU support to trigger a recalc when PMU features, e.g. PERF_CAPABILITIES, are set by userspace, without having to make multiple calls to/from PMU code. As a bonus, using a request will effectively coalesce recalcs, e.g. will reduce the number of recalcs for normal usage from 3+ to 1 (vCPU create, set CPUID, set PERF_CAPABILITIES (Intel only), set filter). The downside is that MSR filter changes that are done in isolation will do a small amount of unnecessary work, but that's already a relatively slow path, and the cost of recalculating instruction intercepts is negligible. Tested-by: Xudong Hao <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Sean Christopherson <[email protected]>
1 parent cdfed93 commit 6057497

File tree

7 files changed

+29
-25
lines changed

7 files changed

+29
-25
lines changed

arch/x86/include/asm/kvm-x86-ops.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ KVM_X86_OP(check_emulate_instruction)
138138
KVM_X86_OP(apic_init_signal_blocked)
139139
KVM_X86_OP_OPTIONAL(enable_l2_tlb_flush)
140140
KVM_X86_OP_OPTIONAL(migrate_timers)
141-
KVM_X86_OP(recalc_msr_intercepts)
141+
KVM_X86_OP(recalc_intercepts)
142142
KVM_X86_OP(complete_emulated_msr)
143143
KVM_X86_OP(vcpu_deliver_sipi_vector)
144144
KVM_X86_OP_OPTIONAL_RET0(vcpu_get_apicv_inhibit_reasons);

arch/x86/include/asm/kvm_host.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@
120120
#define KVM_REQ_TLB_FLUSH_GUEST \
121121
KVM_ARCH_REQ_FLAGS(27, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP)
122122
#define KVM_REQ_APF_READY KVM_ARCH_REQ(28)
123-
#define KVM_REQ_MSR_FILTER_CHANGED KVM_ARCH_REQ(29)
123+
#define KVM_REQ_RECALC_INTERCEPTS KVM_ARCH_REQ(29)
124124
#define KVM_REQ_UPDATE_CPU_DIRTY_LOGGING \
125125
KVM_ARCH_REQ_FLAGS(30, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP)
126126
#define KVM_REQ_MMU_FREE_OBSOLETE_ROOTS \
@@ -1914,7 +1914,7 @@ struct kvm_x86_ops {
19141914
int (*enable_l2_tlb_flush)(struct kvm_vcpu *vcpu);
19151915

19161916
void (*migrate_timers)(struct kvm_vcpu *vcpu);
1917-
void (*recalc_msr_intercepts)(struct kvm_vcpu *vcpu);
1917+
void (*recalc_intercepts)(struct kvm_vcpu *vcpu);
19181918
int (*complete_emulated_msr)(struct kvm_vcpu *vcpu, int err);
19191919

19201920
void (*vcpu_deliver_sipi_vector)(struct kvm_vcpu *vcpu, u8 vector);

arch/x86/kvm/svm/svm.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1077,7 +1077,7 @@ static void svm_recalc_instruction_intercepts(struct kvm_vcpu *vcpu)
10771077
}
10781078
}
10791079

1080-
static void svm_recalc_intercepts_after_set_cpuid(struct kvm_vcpu *vcpu)
1080+
static void svm_recalc_intercepts(struct kvm_vcpu *vcpu)
10811081
{
10821082
svm_recalc_instruction_intercepts(vcpu);
10831083
svm_recalc_msr_intercepts(vcpu);
@@ -1225,7 +1225,7 @@ static void init_vmcb(struct kvm_vcpu *vcpu)
12251225

12261226
svm_hv_init_vmcb(vmcb);
12271227

1228-
svm_recalc_intercepts_after_set_cpuid(vcpu);
1228+
svm_recalc_intercepts(vcpu);
12291229

12301230
vmcb_mark_all_dirty(vmcb);
12311231

@@ -4479,7 +4479,7 @@ static void svm_vcpu_after_set_cpuid(struct kvm_vcpu *vcpu)
44794479
if (sev_guest(vcpu->kvm))
44804480
sev_vcpu_after_set_cpuid(svm);
44814481

4482-
svm_recalc_intercepts_after_set_cpuid(vcpu);
4482+
svm_recalc_intercepts(vcpu);
44834483
}
44844484

44854485
static bool svm_has_wbinvd_exit(void)
@@ -5181,7 +5181,7 @@ static struct kvm_x86_ops svm_x86_ops __initdata = {
51815181

51825182
.apic_init_signal_blocked = svm_apic_init_signal_blocked,
51835183

5184-
.recalc_msr_intercepts = svm_recalc_msr_intercepts,
5184+
.recalc_intercepts = svm_recalc_intercepts,
51855185
.complete_emulated_msr = svm_complete_emulated_msr,
51865186

51875187
.vcpu_deliver_sipi_vector = svm_vcpu_deliver_sipi_vector,

arch/x86/kvm/vmx/main.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -188,18 +188,18 @@ static int vt_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
188188
return vmx_get_msr(vcpu, msr_info);
189189
}
190190

191-
static void vt_recalc_msr_intercepts(struct kvm_vcpu *vcpu)
191+
static void vt_recalc_intercepts(struct kvm_vcpu *vcpu)
192192
{
193193
/*
194-
* TDX doesn't allow VMM to configure interception of MSR accesses.
195-
* TDX guest requests MSR accesses by calling TDVMCALL. The MSR
196-
* filters will be applied when handling the TDVMCALL for RDMSR/WRMSR
197-
* if the userspace has set any.
194+
* TDX doesn't allow VMM to configure interception of instructions or
195+
* MSR accesses. TDX guest requests MSR accesses by calling TDVMCALL.
196+
* The MSR filters will be applied when handling the TDVMCALL for
197+
* RDMSR/WRMSR if the userspace has set any.
198198
*/
199199
if (is_td_vcpu(vcpu))
200200
return;
201201

202-
vmx_recalc_msr_intercepts(vcpu);
202+
vmx_recalc_intercepts(vcpu);
203203
}
204204

205205
static int vt_complete_emulated_msr(struct kvm_vcpu *vcpu, int err)
@@ -995,7 +995,7 @@ struct kvm_x86_ops vt_x86_ops __initdata = {
995995
.apic_init_signal_blocked = vt_op(apic_init_signal_blocked),
996996
.migrate_timers = vmx_migrate_timers,
997997

998-
.recalc_msr_intercepts = vt_op(recalc_msr_intercepts),
998+
.recalc_intercepts = vt_op(recalc_intercepts),
999999
.complete_emulated_msr = vt_op(complete_emulated_msr),
10001000

10011001
.vcpu_deliver_sipi_vector = kvm_vcpu_deliver_sipi_vector,

arch/x86/kvm/vmx/vmx.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4068,7 +4068,7 @@ void pt_update_intercept_for_msr(struct kvm_vcpu *vcpu)
40684068
}
40694069
}
40704070

4071-
void vmx_recalc_msr_intercepts(struct kvm_vcpu *vcpu)
4071+
static void vmx_recalc_msr_intercepts(struct kvm_vcpu *vcpu)
40724072
{
40734073
if (!cpu_has_vmx_msr_bitmap())
40744074
return;
@@ -4121,6 +4121,11 @@ void vmx_recalc_msr_intercepts(struct kvm_vcpu *vcpu)
41214121
*/
41224122
}
41234123

4124+
void vmx_recalc_intercepts(struct kvm_vcpu *vcpu)
4125+
{
4126+
vmx_recalc_msr_intercepts(vcpu);
4127+
}
4128+
41244129
static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
41254130
int vector)
41264131
{
@@ -7802,7 +7807,7 @@ void vmx_vcpu_after_set_cpuid(struct kvm_vcpu *vcpu)
78027807
~FEAT_CTL_SGX_LC_ENABLED;
78037808

78047809
/* Recalc MSR interception to account for feature changes. */
7805-
vmx_recalc_msr_intercepts(vcpu);
7810+
vmx_recalc_intercepts(vcpu);
78067811

78077812
/* Refresh #PF interception to account for MAXPHYADDR changes. */
78087813
vmx_update_exception_bitmap(vcpu);

arch/x86/kvm/vmx/x86_ops.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ void vmx_deliver_interrupt(struct kvm_lapic *apic, int delivery_mode,
5252
int trig_mode, int vector);
5353
void vmx_vcpu_after_set_cpuid(struct kvm_vcpu *vcpu);
5454
bool vmx_has_emulated_msr(struct kvm *kvm, u32 index);
55-
void vmx_recalc_msr_intercepts(struct kvm_vcpu *vcpu);
55+
void vmx_recalc_intercepts(struct kvm_vcpu *vcpu);
5656
void vmx_prepare_switch_to_guest(struct kvm_vcpu *vcpu);
5757
void vmx_update_exception_bitmap(struct kvm_vcpu *vcpu);
5858
int vmx_get_feature_msr(u32 msr, u64 *data);

arch/x86/kvm/x86.c

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6794,7 +6794,11 @@ static int kvm_vm_ioctl_set_msr_filter(struct kvm *kvm,
67946794

67956795
kvm_free_msr_filter(old_filter);
67966796

6797-
kvm_make_all_cpus_request(kvm, KVM_REQ_MSR_FILTER_CHANGED);
6797+
/*
6798+
* Recalc MSR intercepts as userspace may want to intercept accesses to
6799+
* MSRs that KVM would otherwise pass through to the guest.
6800+
*/
6801+
kvm_make_all_cpus_request(kvm, KVM_REQ_RECALC_INTERCEPTS);
67986802

67996803
return 0;
68006804
}
@@ -10827,13 +10831,8 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
1082710831
if (kvm_check_request(KVM_REQ_APF_READY, vcpu))
1082810832
kvm_check_async_pf_completion(vcpu);
1082910833

10830-
/*
10831-
* Recalc MSR intercepts as userspace may want to intercept
10832-
* accesses to MSRs that KVM would otherwise pass through to
10833-
* the guest.
10834-
*/
10835-
if (kvm_check_request(KVM_REQ_MSR_FILTER_CHANGED, vcpu))
10836-
kvm_x86_call(recalc_msr_intercepts)(vcpu);
10834+
if (kvm_check_request(KVM_REQ_RECALC_INTERCEPTS, vcpu))
10835+
kvm_x86_call(recalc_intercepts)(vcpu);
1083710836

1083810837
if (kvm_check_request(KVM_REQ_UPDATE_CPU_DIRTY_LOGGING, vcpu))
1083910838
kvm_x86_call(update_cpu_dirty_logging)(vcpu);

0 commit comments

Comments
 (0)