Skip to content

Commit bc7fe2f

Browse files
committed
KVM: x86: Move PAT MSR handling out of mtrr.c
Drop handling of MSR_IA32_CR_PAT from mtrr.c now that SVM and VMX handle writes without bouncing through kvm_set_msr_common(). PAT isn't truly an MTRR even though it affects memory types, and more importantly KVM enables hardware virtualization of guest PAT (by NOT setting "ignore guest PAT") when a guest has non-coherent DMA, i.e. KVM doesn't need to zap SPTEs when the guest PAT changes. The read path is and always has been trivial, i.e. burying it in the MTRR code does more harm than good. WARN and continue for the PAT case in kvm_set_msr_common(), as that code is _currently_ reached if and only if KVM is buggy. Defer cleaning up the lack of symmetry between the read and write paths to a future patch. Reviewed-by: Kai Huang <kai.huang@intel.com> Link: https://lore.kernel.org/r/20230511233351.635053-7-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent 34a83de commit bc7fe2f

2 files changed

Lines changed: 19 additions & 13 deletions

File tree

arch/x86/kvm/mtrr.c

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ static bool msr_mtrr_valid(unsigned msr)
5555
case MSR_MTRRfix4K_F0000:
5656
case MSR_MTRRfix4K_F8000:
5757
case MSR_MTRRdefType:
58-
case MSR_IA32_CR_PAT:
5958
return true;
6059
}
6160
return false;
@@ -74,9 +73,7 @@ bool kvm_mtrr_valid(struct kvm_vcpu *vcpu, u32 msr, u64 data)
7473
if (!msr_mtrr_valid(msr))
7574
return false;
7675

77-
if (msr == MSR_IA32_CR_PAT) {
78-
return kvm_pat_valid(data);
79-
} else if (msr == MSR_MTRRdefType) {
76+
if (msr == MSR_MTRRdefType) {
8077
if (data & ~0xcff)
8178
return false;
8279
return valid_mtrr_type(data & 0xff);
@@ -324,8 +321,7 @@ static void update_mtrr(struct kvm_vcpu *vcpu, u32 msr)
324321
struct kvm_mtrr *mtrr_state = &vcpu->arch.mtrr_state;
325322
gfn_t start, end;
326323

327-
if (msr == MSR_IA32_CR_PAT || !tdp_enabled ||
328-
!kvm_arch_has_noncoherent_dma(vcpu->kvm))
324+
if (!tdp_enabled || !kvm_arch_has_noncoherent_dma(vcpu->kvm))
329325
return;
330326

331327
if (!mtrr_is_enabled(mtrr_state) && msr != MSR_MTRRdefType)
@@ -392,8 +388,6 @@ int kvm_mtrr_set_msr(struct kvm_vcpu *vcpu, u32 msr, u64 data)
392388
*(u64 *)&vcpu->arch.mtrr_state.fixed_ranges[index] = data;
393389
else if (msr == MSR_MTRRdefType)
394390
vcpu->arch.mtrr_state.deftype = data;
395-
else if (msr == MSR_IA32_CR_PAT)
396-
vcpu->arch.pat = data;
397391
else
398392
set_var_mtrr_msr(vcpu, msr, data);
399393

@@ -421,13 +415,12 @@ int kvm_mtrr_get_msr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
421415
return 1;
422416

423417
index = fixed_msr_to_range_index(msr);
424-
if (index >= 0)
418+
if (index >= 0) {
425419
*pdata = *(u64 *)&vcpu->arch.mtrr_state.fixed_ranges[index];
426-
else if (msr == MSR_MTRRdefType)
420+
} else if (msr == MSR_MTRRdefType) {
427421
*pdata = vcpu->arch.mtrr_state.deftype;
428-
else if (msr == MSR_IA32_CR_PAT)
429-
*pdata = vcpu->arch.pat;
430-
else { /* Variable MTRRs */
422+
} else {
423+
/* Variable MTRRs */
431424
if (is_mtrr_base_msr(msr))
432425
*pdata = var_mtrr_msr_to_range(vcpu, msr)->base;
433426
else

arch/x86/kvm/x86.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3703,6 +3703,17 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
37033703
}
37043704
break;
37053705
case MSR_IA32_CR_PAT:
3706+
/*
3707+
* Writes to PAT should be handled by vendor code as both SVM
3708+
* and VMX track the guest's PAT in the VMCB/VMCS.
3709+
*/
3710+
WARN_ON_ONCE(1);
3711+
3712+
if (!kvm_pat_valid(data))
3713+
return 1;
3714+
3715+
vcpu->arch.pat = data;
3716+
break;
37063717
case MTRRphysBase_MSR(0) ... MSR_MTRRfix4K_F8000:
37073718
case MSR_MTRRdefType:
37083719
return kvm_mtrr_set_msr(vcpu, msr, data);
@@ -4112,6 +4123,8 @@ int kvm_get_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
41124123
break;
41134124
}
41144125
case MSR_IA32_CR_PAT:
4126+
msr_info->data = vcpu->arch.pat;
4127+
break;
41154128
case MSR_MTRRcap:
41164129
case MTRRphysBase_MSR(0) ... MSR_MTRRfix4K_F8000:
41174130
case MSR_MTRRdefType:

0 commit comments

Comments
 (0)