]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
KVM: x86: Deduplicate MSR interception enabling and disabling
authorChao Gao <chao.gao@intel.com>
Thu, 12 Jun 2025 08:19:46 +0000 (01:19 -0700)
committerSean Christopherson <seanjc@google.com>
Tue, 24 Jun 2025 22:42:12 +0000 (15:42 -0700)
Extract a common function from MSR interception disabling logic and create
disabling and enabling functions based on it. This removes most of the
duplicated code for MSR interception disabling/enabling.

No functional change intended.

Signed-off-by: Chao Gao <chao.gao@intel.com>
Reviewed-by: Dapeng Mi <dapeng1.mi@linux.intel.com>
Link: https://lore.kernel.org/r/20250612081947.94081-2-chao.gao@intel.com
[sean: s/enable/set, inline the wrappers]
Signed-off-by: Sean Christopherson <seanjc@google.com>
arch/x86/kvm/svm/svm.c
arch/x86/kvm/svm/svm.h
arch/x86/kvm/vmx/vmx.c
arch/x86/kvm/vmx/vmx.h

index 3c5e82ed6bd476427617a4095ba813bcfc286359..803574920e4109f489a209a67ce4c14457b809c2 100644 (file)
@@ -679,21 +679,21 @@ static bool msr_write_intercepted(struct kvm_vcpu *vcpu, u32 msr)
        return svm_test_msr_bitmap_write(msrpm, msr);
 }
 
-void svm_disable_intercept_for_msr(struct kvm_vcpu *vcpu, u32 msr, int type)
+void svm_set_intercept_for_msr(struct kvm_vcpu *vcpu, u32 msr, int type, bool set)
 {
        struct vcpu_svm *svm = to_svm(vcpu);
        void *msrpm = svm->msrpm;
 
        /* Don't disable interception for MSRs userspace wants to handle. */
        if (type & MSR_TYPE_R) {
-               if (kvm_msr_allowed(vcpu, msr, KVM_MSR_FILTER_READ))
+               if (!set && kvm_msr_allowed(vcpu, msr, KVM_MSR_FILTER_READ))
                        svm_clear_msr_bitmap_read(msrpm, msr);
                else
                        svm_set_msr_bitmap_read(msrpm, msr);
        }
 
        if (type & MSR_TYPE_W) {
-               if (kvm_msr_allowed(vcpu, msr, KVM_MSR_FILTER_WRITE))
+               if (!set && kvm_msr_allowed(vcpu, msr, KVM_MSR_FILTER_WRITE))
                        svm_clear_msr_bitmap_write(msrpm, msr);
                else
                        svm_set_msr_bitmap_write(msrpm, msr);
@@ -703,21 +703,6 @@ void svm_disable_intercept_for_msr(struct kvm_vcpu *vcpu, u32 msr, int type)
        svm->nested.force_msr_bitmap_recalc = true;
 }
 
-void svm_enable_intercept_for_msr(struct kvm_vcpu *vcpu, u32 msr, int type)
-{
-       struct vcpu_svm *svm = to_svm(vcpu);
-       void *msrpm = svm->msrpm;
-
-       if (type & MSR_TYPE_R)
-               svm_set_msr_bitmap_read(msrpm, msr);
-
-       if (type & MSR_TYPE_W)
-               svm_set_msr_bitmap_write(msrpm, msr);
-
-       svm_hv_vmcb_dirty_nested_enlightenments(vcpu);
-       svm->nested.force_msr_bitmap_recalc = true;
-}
-
 void *svm_alloc_permissions_map(unsigned long size, gfp_t gfp_mask)
 {
        unsigned int order = get_order(size);
index 8d32795632613e900541a3294893fb94eb91ee74..dabd69d6fd158775328c7eb73599c48dc7affc3a 100644 (file)
@@ -694,16 +694,18 @@ void svm_set_x2apic_msr_interception(struct vcpu_svm *svm, bool disable);
 void svm_complete_interrupt_delivery(struct kvm_vcpu *vcpu, int delivery_mode,
                                     int trig_mode, int vec);
 
-void svm_enable_intercept_for_msr(struct kvm_vcpu *vcpu, u32 msr, int type);
-void svm_disable_intercept_for_msr(struct kvm_vcpu *vcpu, u32 msr, int type);
+void svm_set_intercept_for_msr(struct kvm_vcpu *vcpu, u32 msr, int type, bool set);
 
-static inline void svm_set_intercept_for_msr(struct kvm_vcpu *vcpu, u32 msr,
-                                            int type, bool enable_intercept)
+static inline void svm_disable_intercept_for_msr(struct kvm_vcpu *vcpu,
+                                                u32 msr, int type)
 {
-       if (enable_intercept)
-               svm_enable_intercept_for_msr(vcpu, msr, type);
-       else
-               svm_disable_intercept_for_msr(vcpu, msr, type);
+       svm_set_intercept_for_msr(vcpu, msr, type, false);
+}
+
+static inline void svm_enable_intercept_for_msr(struct kvm_vcpu *vcpu,
+                                               u32 msr, int type)
+{
+       svm_set_intercept_for_msr(vcpu, msr, type, true);
 }
 
 /* nested.c */
index e2de4748d662ccc2e3104de6410fe4b13e1d8dba..f81710d7d992f30cd80756410b29440927d81822 100644 (file)
@@ -3963,7 +3963,7 @@ static void vmx_msr_bitmap_l01_changed(struct vcpu_vmx *vmx)
        vmx->nested.force_msr_bitmap_recalc = true;
 }
 
-void vmx_disable_intercept_for_msr(struct kvm_vcpu *vcpu, u32 msr, int type)
+void vmx_set_intercept_for_msr(struct kvm_vcpu *vcpu, u32 msr, int type, bool set)
 {
        struct vcpu_vmx *vmx = to_vmx(vcpu);
        unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap;
@@ -3974,37 +3974,20 @@ void vmx_disable_intercept_for_msr(struct kvm_vcpu *vcpu, u32 msr, int type)
        vmx_msr_bitmap_l01_changed(vmx);
 
        if (type & MSR_TYPE_R) {
-               if (kvm_msr_allowed(vcpu, msr, KVM_MSR_FILTER_READ))
+               if (!set && kvm_msr_allowed(vcpu, msr, KVM_MSR_FILTER_READ))
                        vmx_clear_msr_bitmap_read(msr_bitmap, msr);
                else
                        vmx_set_msr_bitmap_read(msr_bitmap, msr);
        }
 
        if (type & MSR_TYPE_W) {
-               if (kvm_msr_allowed(vcpu, msr, KVM_MSR_FILTER_WRITE))
+               if (!set && kvm_msr_allowed(vcpu, msr, KVM_MSR_FILTER_WRITE))
                        vmx_clear_msr_bitmap_write(msr_bitmap, msr);
                else
                        vmx_set_msr_bitmap_write(msr_bitmap, msr);
        }
 }
 
-void vmx_enable_intercept_for_msr(struct kvm_vcpu *vcpu, u32 msr, int type)
-{
-       struct vcpu_vmx *vmx = to_vmx(vcpu);
-       unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap;
-
-       if (!cpu_has_vmx_msr_bitmap())
-               return;
-
-       vmx_msr_bitmap_l01_changed(vmx);
-
-       if (type & MSR_TYPE_R)
-               vmx_set_msr_bitmap_read(msr_bitmap, msr);
-
-       if (type & MSR_TYPE_W)
-               vmx_set_msr_bitmap_write(msr_bitmap, msr);
-}
-
 static void vmx_update_msr_bitmap_x2apic(struct kvm_vcpu *vcpu)
 {
        /*
index 5a87be8854f346f52310be2e087e24b09d416157..87174d961c85b17123d61130bf3116fce733d6f4 100644 (file)
@@ -386,23 +386,25 @@ bool __vmx_vcpu_run(struct vcpu_vmx *vmx, unsigned long *regs,
 int vmx_find_loadstore_msr_slot(struct vmx_msrs *m, u32 msr);
 void vmx_ept_load_pdptrs(struct kvm_vcpu *vcpu);
 
-void vmx_disable_intercept_for_msr(struct kvm_vcpu *vcpu, u32 msr, int type);
-void vmx_enable_intercept_for_msr(struct kvm_vcpu *vcpu, u32 msr, int type);
+void vmx_set_intercept_for_msr(struct kvm_vcpu *vcpu, u32 msr, int type, bool set);
+
+static inline void vmx_disable_intercept_for_msr(struct kvm_vcpu *vcpu,
+                                                u32 msr, int type)
+{
+       vmx_set_intercept_for_msr(vcpu, msr, type, false);
+}
+
+static inline void vmx_enable_intercept_for_msr(struct kvm_vcpu *vcpu,
+                                               u32 msr, int type)
+{
+       vmx_set_intercept_for_msr(vcpu, msr, type, true);
+}
 
 u64 vmx_get_l2_tsc_offset(struct kvm_vcpu *vcpu);
 u64 vmx_get_l2_tsc_multiplier(struct kvm_vcpu *vcpu);
 
 gva_t vmx_get_untagged_addr(struct kvm_vcpu *vcpu, gva_t gva, unsigned int flags);
 
-static inline void vmx_set_intercept_for_msr(struct kvm_vcpu *vcpu, u32 msr,
-                                            int type, bool value)
-{
-       if (value)
-               vmx_enable_intercept_for_msr(vcpu, msr, type);
-       else
-               vmx_disable_intercept_for_msr(vcpu, msr, type);
-}
-
 void vmx_update_cpu_dirty_logging(struct kvm_vcpu *vcpu);
 
 u64 vmx_get_supported_debugctl(struct kvm_vcpu *vcpu, bool host_initiated);