Skip to content

Commit ec93675

Browse files
xinli-intelsean-jc
authored andcommitted
KVM: VMX: Support the immediate form of WRMSRNS in the VM-Exit fastpath
Add support for handling "WRMSRNS with an immediate" VM-Exits in KVM's fastpath. On Intel, all writes to the x2APIC ICR and to the TSC Deadline MSR are non-serializing, i.e. it's highly likely guest kernels will switch to using WRMSRNS when possible. And in general, any MSR written via WRMSRNS is probably worth handling in the fastpath, as the entire point of WRMSRNS is to shave cycles in hot paths. Signed-off-by: Xin Li (Intel) <xin@zytor.com> [sean: rewrite changelog, split rename to separate patch] Link: https://lore.kernel.org/r/20250805202224.1475590-6-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent 885df2d commit ec93675

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

arch/x86/kvm/vmx/vmx.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7192,6 +7192,9 @@ static fastpath_t vmx_exit_handlers_fastpath(struct kvm_vcpu *vcpu,
71927192
switch (vmx_get_exit_reason(vcpu).basic) {
71937193
case EXIT_REASON_MSR_WRITE:
71947194
return handle_fastpath_wrmsr(vcpu);
7195+
case EXIT_REASON_MSR_WRITE_IMM:
7196+
return handle_fastpath_wrmsr_imm(vcpu, vmx_get_exit_qual(vcpu),
7197+
vmx_get_msr_imm_reg(vcpu));
71957198
case EXIT_REASON_PREEMPTION_TIMER:
71967199
return handle_fastpath_preemption_timer(vcpu, force_immediate_exit);
71977200
case EXIT_REASON_HLT:

arch/x86/kvm/x86.c

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2178,11 +2178,8 @@ static inline bool kvm_vcpu_exit_request(struct kvm_vcpu *vcpu)
21782178
kvm_request_pending(vcpu) || xfer_to_guest_mode_work_pending();
21792179
}
21802180

2181-
fastpath_t handle_fastpath_wrmsr(struct kvm_vcpu *vcpu)
2181+
static fastpath_t __handle_fastpath_wrmsr(struct kvm_vcpu *vcpu, u32 msr, u64 data)
21822182
{
2183-
u64 data = kvm_read_edx_eax(vcpu);
2184-
u32 msr = kvm_rcx_read(vcpu);
2185-
21862183
switch (msr) {
21872184
case APIC_BASE_MSR + (APIC_ICR >> 4):
21882185
if (!lapic_in_kernel(vcpu) || !apic_x2apic_mode(vcpu->arch.apic) ||
@@ -2203,8 +2200,20 @@ fastpath_t handle_fastpath_wrmsr(struct kvm_vcpu *vcpu)
22032200

22042201
return EXIT_FASTPATH_REENTER_GUEST;
22052202
}
2203+
2204+
fastpath_t handle_fastpath_wrmsr(struct kvm_vcpu *vcpu)
2205+
{
2206+
return __handle_fastpath_wrmsr(vcpu, kvm_rcx_read(vcpu),
2207+
kvm_read_edx_eax(vcpu));
2208+
}
22062209
EXPORT_SYMBOL_GPL(handle_fastpath_wrmsr);
22072210

2211+
fastpath_t handle_fastpath_wrmsr_imm(struct kvm_vcpu *vcpu, u32 msr, int reg)
2212+
{
2213+
return __handle_fastpath_wrmsr(vcpu, msr, kvm_register_read(vcpu, reg));
2214+
}
2215+
EXPORT_SYMBOL_GPL(handle_fastpath_wrmsr_imm);
2216+
22082217
/*
22092218
* Adapt set_msr() to msr_io()'s calling convention
22102219
*/

arch/x86/kvm/x86.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,7 @@ int x86_decode_emulated_instruction(struct kvm_vcpu *vcpu, int emulation_type,
438438
int x86_emulate_instruction(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa,
439439
int emulation_type, void *insn, int insn_len);
440440
fastpath_t handle_fastpath_wrmsr(struct kvm_vcpu *vcpu);
441+
fastpath_t handle_fastpath_wrmsr_imm(struct kvm_vcpu *vcpu, u32 msr, int reg);
441442
fastpath_t handle_fastpath_hlt(struct kvm_vcpu *vcpu);
442443
fastpath_t handle_fastpath_invd(struct kvm_vcpu *vcpu);
443444

0 commit comments

Comments
 (0)