Skip to content

Commit bea44d1

Browse files
committed
KVM: x86: Simplify userspace filter logic when disabling MSR interception
Refactor {svm,vmx}_disable_intercept_for_msr() to simplify the handling of userspace filters that disallow access to an MSR. The more complicated logic is no longer needed or justified now that KVM recalculates all MSR intercepts on a userspace MSR filter change, i.e. now that KVM doesn't need to also update shadow bitmaps. No functional change intended. Suggested-by: Dapeng Mi <dapeng1.mi@linux.intel.com> Reviewed-by: Dapeng Mi <dapeng1.mi@linux.intel.com> Link: https://lore.kernel.org/r/20250610225737.156318-32-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent 73be81b commit bea44d1

2 files changed

Lines changed: 20 additions & 28 deletions

File tree

arch/x86/kvm/svm/svm.c

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -685,24 +685,20 @@ void svm_disable_intercept_for_msr(struct kvm_vcpu *vcpu, u32 msr, int type)
685685
void *msrpm = svm->msrpm;
686686

687687
/* Don't disable interception for MSRs userspace wants to handle. */
688-
if ((type & MSR_TYPE_R) &&
689-
!kvm_msr_allowed(vcpu, msr, KVM_MSR_FILTER_READ)) {
690-
svm_set_msr_bitmap_read(msrpm, msr);
691-
type &= ~MSR_TYPE_R;
688+
if (type & MSR_TYPE_R) {
689+
if (kvm_msr_allowed(vcpu, msr, KVM_MSR_FILTER_READ))
690+
svm_clear_msr_bitmap_read(msrpm, msr);
691+
else
692+
svm_set_msr_bitmap_read(msrpm, msr);
692693
}
693694

694-
if ((type & MSR_TYPE_W) &&
695-
!kvm_msr_allowed(vcpu, msr, KVM_MSR_FILTER_WRITE)) {
696-
svm_set_msr_bitmap_write(msrpm, msr);
697-
type &= ~MSR_TYPE_W;
695+
if (type & MSR_TYPE_W) {
696+
if (kvm_msr_allowed(vcpu, msr, KVM_MSR_FILTER_WRITE))
697+
svm_clear_msr_bitmap_write(msrpm, msr);
698+
else
699+
svm_set_msr_bitmap_write(msrpm, msr);
698700
}
699701

700-
if (type & MSR_TYPE_R)
701-
svm_clear_msr_bitmap_read(msrpm, msr);
702-
703-
if (type & MSR_TYPE_W)
704-
svm_clear_msr_bitmap_write(msrpm, msr);
705-
706702
svm_hv_vmcb_dirty_nested_enlightenments(vcpu);
707703
svm->nested.force_msr_bitmap_recalc = true;
708704
}

arch/x86/kvm/vmx/vmx.c

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3973,23 +3973,19 @@ void vmx_disable_intercept_for_msr(struct kvm_vcpu *vcpu, u32 msr, int type)
39733973

39743974
vmx_msr_bitmap_l01_changed(vmx);
39753975

3976-
if ((type & MSR_TYPE_R) &&
3977-
!kvm_msr_allowed(vcpu, msr, KVM_MSR_FILTER_READ)) {
3978-
vmx_set_msr_bitmap_read(msr_bitmap, msr);
3979-
type &= ~MSR_TYPE_R;
3976+
if (type & MSR_TYPE_R) {
3977+
if (kvm_msr_allowed(vcpu, msr, KVM_MSR_FILTER_READ))
3978+
vmx_clear_msr_bitmap_read(msr_bitmap, msr);
3979+
else
3980+
vmx_set_msr_bitmap_read(msr_bitmap, msr);
39803981
}
39813982

3982-
if ((type & MSR_TYPE_W) &&
3983-
!kvm_msr_allowed(vcpu, msr, KVM_MSR_FILTER_WRITE)) {
3984-
vmx_set_msr_bitmap_write(msr_bitmap, msr);
3985-
type &= ~MSR_TYPE_W;
3983+
if (type & MSR_TYPE_W) {
3984+
if (kvm_msr_allowed(vcpu, msr, KVM_MSR_FILTER_WRITE))
3985+
vmx_clear_msr_bitmap_write(msr_bitmap, msr);
3986+
else
3987+
vmx_set_msr_bitmap_write(msr_bitmap, msr);
39863988
}
3987-
3988-
if (type & MSR_TYPE_R)
3989-
vmx_clear_msr_bitmap_read(msr_bitmap, msr);
3990-
3991-
if (type & MSR_TYPE_W)
3992-
vmx_clear_msr_bitmap_write(msr_bitmap, msr);
39933989
}
39943990

39953991
void vmx_enable_intercept_for_msr(struct kvm_vcpu *vcpu, u32 msr, int type)

0 commit comments

Comments
 (0)