Skip to content

Commit aebcbb6

Browse files
committed
KVM: x86: Acquire SRCU in WRMSR fastpath iff instruction needs to be skipped
Acquire SRCU in the WRMSR fastpath if and only if an instruction needs to be skipped, i.e. only if the fastpath succeeds. The reasoning in commit 3f2739b ("KVM: x86: Acquire SRCU read lock when handling fastpath MSR writes") about "avoid having to play whack-a-mole" seems sound, but in hindsight unconditionally acquiring SRCU does more harm than good. While acquiring/releasing SRCU isn't slow per se, the things that are _protected_ by kvm->srcu are generally safe to access only in the "slow" VM-Exit path. E.g. accessing memslots in generic helpers is never safe, because accessing guest memory with IRQs disabled is unless unsafe (except when kvm_vcpu_read_guest_atomic() is used, but that API should never be used in emulation helpers). In other words, playing whack-a-mole is actually desirable in this case, because every access to an asset protected by kvm->srcu warrants further scrutiny. Link: https://lore.kernel.org/r/20250805190526.1453366-7-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent 0a94b20 commit aebcbb6

1 file changed

Lines changed: 8 additions & 13 deletions

File tree

arch/x86/kvm/x86.c

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2159,10 +2159,8 @@ fastpath_t handle_fastpath_set_msr_irqoff(struct kvm_vcpu *vcpu)
21592159
{
21602160
u32 msr = kvm_rcx_read(vcpu);
21612161
u64 data;
2162-
fastpath_t ret;
21632162
bool handled;
2164-
2165-
kvm_vcpu_srcu_read_lock(vcpu);
2163+
int r;
21662164

21672165
switch (msr) {
21682166
case APIC_BASE_MSR + (APIC_ICR >> 4):
@@ -2178,19 +2176,16 @@ fastpath_t handle_fastpath_set_msr_irqoff(struct kvm_vcpu *vcpu)
21782176
break;
21792177
}
21802178

2181-
if (handled) {
2182-
if (!kvm_skip_emulated_instruction(vcpu))
2183-
ret = EXIT_FASTPATH_EXIT_USERSPACE;
2184-
else
2185-
ret = EXIT_FASTPATH_REENTER_GUEST;
2186-
trace_kvm_msr_write(msr, data);
2187-
} else {
2188-
ret = EXIT_FASTPATH_NONE;
2189-
}
2179+
if (!handled)
2180+
return EXIT_FASTPATH_NONE;
21902181

2182+
kvm_vcpu_srcu_read_lock(vcpu);
2183+
r = kvm_skip_emulated_instruction(vcpu);
21912184
kvm_vcpu_srcu_read_unlock(vcpu);
21922185

2193-
return ret;
2186+
trace_kvm_msr_write(msr, data);
2187+
2188+
return r ? EXIT_FASTPATH_REENTER_GUEST : EXIT_FASTPATH_EXIT_USERSPACE;
21942189
}
21952190
EXPORT_SYMBOL_GPL(handle_fastpath_set_msr_irqoff);
21962191

0 commit comments

Comments
 (0)