From: Sean Christopherson <seanjc@google.com>
To: Sean Christopherson <seanjc@google.com>,
Paolo Bonzini <pbonzini@redhat.com>
Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org,
Wenyao Hai <haiwenyao@uniontech.com>,
Ke Guo <guoke@uniontech.com>
Subject: [PATCH 5/5] KVM: x86: Move PAT MSR handling out of mtrr.c
Date: Wed, 3 May 2023 11:28:52 -0700 [thread overview]
Message-ID: <20230503182852.3431281-6-seanjc@google.com> (raw)
In-Reply-To: <20230503182852.3431281-1-seanjc@google.com>
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 isn't
factored into KVM's handling of non-coherent DMA.
The read path is and always has been trivial, i.e. burying it in the MTRR
code does more harm than good.
Inject #GP unconditionally for the PAT "handlng" in kvm_set_msr_common(),
as that code is reached if and only if KVM is buggy. Simply deleting the
condition would provide similar functionality, but warning and explicitly
alerting userspace that KVM is buggy is desirable.
Signed-off-by: Sean Christopherson <seanjc@google.com>
---
arch/x86/kvm/mtrr.c | 18 +++++-------------
arch/x86/kvm/x86.c | 4 +++-
2 files changed, 8 insertions(+), 14 deletions(-)
diff --git a/arch/x86/kvm/mtrr.c b/arch/x86/kvm/mtrr.c
index d2c428f4ae42..76b132ba9df2 100644
--- a/arch/x86/kvm/mtrr.c
+++ b/arch/x86/kvm/mtrr.c
@@ -41,7 +41,6 @@ static bool msr_mtrr_valid(unsigned msr)
case MSR_MTRRfix4K_F0000:
case MSR_MTRRfix4K_F8000:
case MSR_MTRRdefType:
- case MSR_IA32_CR_PAT:
return true;
}
return false;
@@ -60,9 +59,7 @@ bool kvm_mtrr_valid(struct kvm_vcpu *vcpu, u32 msr, u64 data)
if (!msr_mtrr_valid(msr))
return false;
- if (msr == MSR_IA32_CR_PAT) {
- return kvm_pat_valid(data);
- } else if (msr == MSR_MTRRdefType) {
+ if (msr == MSR_MTRRdefType) {
if (data & ~0xcff)
return false;
return valid_mtrr_type(data & 0xff);
@@ -310,8 +307,7 @@ static void update_mtrr(struct kvm_vcpu *vcpu, u32 msr)
gfn_t start, end;
int index;
- if (msr == MSR_IA32_CR_PAT || !tdp_enabled ||
- !kvm_arch_has_noncoherent_dma(vcpu->kvm))
+ if (!tdp_enabled || !kvm_arch_has_noncoherent_dma(vcpu->kvm))
return;
if (!mtrr_is_enabled(mtrr_state) && msr != MSR_MTRRdefType)
@@ -382,8 +378,6 @@ int kvm_mtrr_set_msr(struct kvm_vcpu *vcpu, u32 msr, u64 data)
*(u64 *)&vcpu->arch.mtrr_state.fixed_ranges[index] = data;
else if (msr == MSR_MTRRdefType)
vcpu->arch.mtrr_state.deftype = data;
- else if (msr == MSR_IA32_CR_PAT)
- vcpu->arch.pat = data;
else
set_var_mtrr_msr(vcpu, msr, data);
@@ -411,13 +405,11 @@ int kvm_mtrr_get_msr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
return 1;
index = fixed_msr_to_range_index(msr);
- if (index >= 0)
+ if (index >= 0) {
*pdata = *(u64 *)&vcpu->arch.mtrr_state.fixed_ranges[index];
- else if (msr == MSR_MTRRdefType)
+ } else if (msr == MSR_MTRRdefType) {
*pdata = vcpu->arch.mtrr_state.deftype;
- else if (msr == MSR_IA32_CR_PAT)
- *pdata = vcpu->arch.pat;
- else { /* Variable MTRRs */
+ } else { /* Variable MTRRs */
int is_mtrr_mask;
index = (msr - 0x200) / 2;
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index c36256d00250..3aa93401a398 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -3706,7 +3706,7 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
* and VMX track the guest's PAT in the VMCB/VMCS.
*/
WARN_ON_ONCE(1);
- fallthrough;
+ return 1;
case MTRRphysBase_MSR(0) ... MSR_MTRRfix4K_F8000:
case MSR_MTRRdefType:
return kvm_mtrr_set_msr(vcpu, msr, data);
@@ -4116,6 +4116,8 @@ int kvm_get_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
break;
}
case MSR_IA32_CR_PAT:
+ msr_info->data = vcpu->arch.pat;
+ break;
case MSR_MTRRcap:
case MTRRphysBase_MSR(0) ... MSR_MTRRfix4K_F8000:
case MSR_MTRRdefType:
--
2.40.1.495.gc816e09b53d-goog
prev parent reply other threads:[~2023-05-03 18:29 UTC|newest]
Thread overview: 21+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-05-03 18:28 [PATCH 0/5] KVM: x86: Clean up MSR PAT handling Sean Christopherson
2023-05-03 18:28 ` [PATCH 1/5] KVM: VMX: Open code writing vCPU's PAT in VMX's MSR handler Sean Christopherson
2023-05-03 23:00 ` Huang, Kai
2023-05-03 23:25 ` Sean Christopherson
2023-05-03 23:41 ` Huang, Kai
2023-05-04 17:23 ` Sean Christopherson
2023-05-03 18:28 ` [PATCH 2/5] KVM: SVM: Use kvm_pat_valid() directly instead of kvm_mtrr_valid() Sean Christopherson
2023-05-03 23:04 ` Huang, Kai
2023-05-04 15:34 ` Sean Christopherson
2023-05-05 11:20 ` Huang, Kai
2023-05-11 23:03 ` Sean Christopherson
2023-05-03 18:28 ` [PATCH 3/5] KVM: x86: Use MTRR macros to define possible MTRR MSR ranges Sean Christopherson
2023-05-03 23:23 ` Huang, Kai
2023-05-03 23:36 ` Sean Christopherson
2023-05-03 23:49 ` Huang, Kai
2023-05-04 9:02 ` Yan Zhao
2023-05-04 15:36 ` Sean Christopherson
2023-05-03 18:28 ` [PATCH 4/5] KVM: x86: WARN if writes to PAT MSR are handled by common KVM code Sean Christopherson
2023-05-03 23:26 ` Huang, Kai
2023-05-03 23:38 ` Sean Christopherson
2023-05-03 18:28 ` Sean Christopherson [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230503182852.3431281-6-seanjc@google.com \
--to=seanjc@google.com \
--cc=guoke@uniontech.com \
--cc=haiwenyao@uniontech.com \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=pbonzini@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®