diff mbox series

[v4,2/9] KVM: nVMX: Load GUEST_IA32_PERF_GLOBAL_CTRL MSR on vm-entry

Message ID 20190906210313.128316-3-oupton@google.com (mailing list archive)
State New, archived
Headers show
Series KVM: VMX: Add full nested support for IA32_PERF_GLOBAL_CTRL | expand

Commit Message

Oliver Upton Sept. 6, 2019, 9:03 p.m. UTC
Add condition to prepare_vmcs02 which loads IA32_PERF_GLOBAL_CTRL on
VM-entry if the "load IA32_PERF_GLOBAL_CTRL" bit on the VM-entry control
is set. Use kvm_set_msr() rather than directly writing to the field to
avoid overwrite by atomic_switch_perf_msrs().

Suggested-by: Jim Mattson <jmattson@google.com>
Co-developed-by: Krish Sadhukhan <krish.sadhukhan@oracle.com>
Signed-off-by: Krish Sadhukhan <krish.sadhukhan@oracle.com>
Signed-off-by: Oliver Upton <oupton@google.com>
Reviewed-by: Jim Mattson <jmattson@google.com>
Reviewed-by: Peter Shier <pshier@google.com>
---
 arch/x86/kvm/vmx/nested.c | 11 +++++++++++
 1 file changed, 11 insertions(+)

Comments

Sean Christopherson Sept. 16, 2019, 6:06 p.m. UTC | #1
On Fri, Sep 06, 2019 at 02:03:06PM -0700, Oliver Upton wrote:
> Add condition to prepare_vmcs02 which loads IA32_PERF_GLOBAL_CTRL on
> VM-entry if the "load IA32_PERF_GLOBAL_CTRL" bit on the VM-entry control
> is set. Use kvm_set_msr() rather than directly writing to the field to
> avoid overwrite by atomic_switch_perf_msrs().
> 
> Suggested-by: Jim Mattson <jmattson@google.com>
> Co-developed-by: Krish Sadhukhan <krish.sadhukhan@oracle.com>
> Signed-off-by: Krish Sadhukhan <krish.sadhukhan@oracle.com>
> Signed-off-by: Oliver Upton <oupton@google.com>
> Reviewed-by: Jim Mattson <jmattson@google.com>
> Reviewed-by: Peter Shier <pshier@google.com>
> ---
>  arch/x86/kvm/vmx/nested.c | 11 +++++++++++
>  1 file changed, 11 insertions(+)
> 
> diff --git a/arch/x86/kvm/vmx/nested.c b/arch/x86/kvm/vmx/nested.c
> index b0ca34bf4d21..9ba90b38d74b 100644
> --- a/arch/x86/kvm/vmx/nested.c
> +++ b/arch/x86/kvm/vmx/nested.c
> @@ -2281,6 +2281,7 @@ static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
>  {
>  	struct vcpu_vmx *vmx = to_vmx(vcpu);
>  	struct hv_enlightened_vmcs *hv_evmcs = vmx->nested.hv_evmcs;
> +	struct msr_data msr_info;
>  	bool load_guest_pdptrs_vmcs12 = false;
>  
>  	if (vmx->nested.dirty_vmcs12 || hv_evmcs) {
> @@ -2404,6 +2405,16 @@ static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
>  	if (!enable_ept)
>  		vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
>  
> +	if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL) {
> +		msr_info.host_initiated = false;
> +		msr_info.index = MSR_CORE_PERF_GLOBAL_CTRL;
> +		msr_info.data = vmcs12->guest_ia32_perf_global_ctrl;
> +		if (kvm_set_msr(vcpu, &msr_info))
> +			pr_debug_ratelimited(
> +				"%s cannot write MSR (0x%x, 0x%llx)\n",
> +				__func__, msr_info.index, msr_info.data);

Same comment on printing the name.  Might be work adding a helper function
or macro?  That'd also avoid blasting past the 80-column guideline.

> +	}
> +
>  	kvm_rsp_write(vcpu, vmcs12->guest_rsp);
>  	kvm_rip_write(vcpu, vmcs12->guest_rip);
>  	return 0;
> -- 
> 2.23.0.187.g17f5b7556c-goog
>
Oliver Upton Sept. 16, 2019, 9:17 p.m. UTC | #2
On Mon, Sep 16, 2019 at 11:06:14AM -0700, Sean Christopherson wrote:
> On Fri, Sep 06, 2019 at 02:03:06PM -0700, Oliver Upton wrote:
> > Add condition to prepare_vmcs02 which loads IA32_PERF_GLOBAL_CTRL on
> > VM-entry if the "load IA32_PERF_GLOBAL_CTRL" bit on the VM-entry control
> > is set. Use kvm_set_msr() rather than directly writing to the field to
> > avoid overwrite by atomic_switch_perf_msrs().
> > 
> > Suggested-by: Jim Mattson <jmattson@google.com>
> > Co-developed-by: Krish Sadhukhan <krish.sadhukhan@oracle.com>
> > Signed-off-by: Krish Sadhukhan <krish.sadhukhan@oracle.com>
> > Signed-off-by: Oliver Upton <oupton@google.com>
> > Reviewed-by: Jim Mattson <jmattson@google.com>
> > Reviewed-by: Peter Shier <pshier@google.com>
> > ---
> >  arch/x86/kvm/vmx/nested.c | 11 +++++++++++
> >  1 file changed, 11 insertions(+)
> > 
> > diff --git a/arch/x86/kvm/vmx/nested.c b/arch/x86/kvm/vmx/nested.c
> > index b0ca34bf4d21..9ba90b38d74b 100644
> > --- a/arch/x86/kvm/vmx/nested.c
> > +++ b/arch/x86/kvm/vmx/nested.c
> > @@ -2281,6 +2281,7 @@ static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
> >  {
> >  	struct vcpu_vmx *vmx = to_vmx(vcpu);
> >  	struct hv_enlightened_vmcs *hv_evmcs = vmx->nested.hv_evmcs;
> > +	struct msr_data msr_info;
> >  	bool load_guest_pdptrs_vmcs12 = false;
> >  
> >  	if (vmx->nested.dirty_vmcs12 || hv_evmcs) {
> > @@ -2404,6 +2405,16 @@ static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
> >  	if (!enable_ept)
> >  		vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
> >  
> > +	if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL) {
> > +		msr_info.host_initiated = false;
> > +		msr_info.index = MSR_CORE_PERF_GLOBAL_CTRL;
> > +		msr_info.data = vmcs12->guest_ia32_perf_global_ctrl;
> > +		if (kvm_set_msr(vcpu, &msr_info))
> > +			pr_debug_ratelimited(
> > +				"%s cannot write MSR (0x%x, 0x%llx)\n",
> > +				__func__, msr_info.index, msr_info.data);
> 
> Same comment on printing the name.  Might be work adding a helper function
> or macro?  That'd also avoid blasting past the 80-column guideline.

Thanks for the review, Sean. I believe that in one of the prior sets I
mailed out you had mentioned a macro for this as well, but was a fix
throughout KVM. Shall I introduce the macro as part of this series, but
only apply it to my changes (and fix other call sites later on)?

> > +	}
> > +
> >  	kvm_rsp_write(vcpu, vmcs12->guest_rsp);
> >  	kvm_rip_write(vcpu, vmcs12->guest_rip);
> >  	return 0;
> > -- 
> > 2.23.0.187.g17f5b7556c-goog
> >
Sean Christopherson Sept. 16, 2019, 10:50 p.m. UTC | #3
On Mon, Sep 16, 2019 at 02:17:42PM -0700, Oliver Upton wrote:
> On Mon, Sep 16, 2019 at 11:06:14AM -0700, Sean Christopherson wrote:
> > On Fri, Sep 06, 2019 at 02:03:06PM -0700, Oliver Upton wrote:
> > > Add condition to prepare_vmcs02 which loads IA32_PERF_GLOBAL_CTRL on
> > > VM-entry if the "load IA32_PERF_GLOBAL_CTRL" bit on the VM-entry control
> > > is set. Use kvm_set_msr() rather than directly writing to the field to
> > > avoid overwrite by atomic_switch_perf_msrs().
> > > 
> > > Suggested-by: Jim Mattson <jmattson@google.com>
> > > Co-developed-by: Krish Sadhukhan <krish.sadhukhan@oracle.com>
> > > Signed-off-by: Krish Sadhukhan <krish.sadhukhan@oracle.com>
> > > Signed-off-by: Oliver Upton <oupton@google.com>
> > > Reviewed-by: Jim Mattson <jmattson@google.com>
> > > Reviewed-by: Peter Shier <pshier@google.com>
> > > ---
> > >  arch/x86/kvm/vmx/nested.c | 11 +++++++++++
> > >  1 file changed, 11 insertions(+)
> > > 
> > > diff --git a/arch/x86/kvm/vmx/nested.c b/arch/x86/kvm/vmx/nested.c
> > > index b0ca34bf4d21..9ba90b38d74b 100644
> > > --- a/arch/x86/kvm/vmx/nested.c
> > > +++ b/arch/x86/kvm/vmx/nested.c
> > > @@ -2281,6 +2281,7 @@ static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
> > >  {
> > >  	struct vcpu_vmx *vmx = to_vmx(vcpu);
> > >  	struct hv_enlightened_vmcs *hv_evmcs = vmx->nested.hv_evmcs;
> > > +	struct msr_data msr_info;
> > >  	bool load_guest_pdptrs_vmcs12 = false;
> > >  
> > >  	if (vmx->nested.dirty_vmcs12 || hv_evmcs) {
> > > @@ -2404,6 +2405,16 @@ static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
> > >  	if (!enable_ept)
> > >  		vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
> > >  
> > > +	if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL) {
> > > +		msr_info.host_initiated = false;
> > > +		msr_info.index = MSR_CORE_PERF_GLOBAL_CTRL;
> > > +		msr_info.data = vmcs12->guest_ia32_perf_global_ctrl;
> > > +		if (kvm_set_msr(vcpu, &msr_info))
> > > +			pr_debug_ratelimited(
> > > +				"%s cannot write MSR (0x%x, 0x%llx)\n",
> > > +				__func__, msr_info.index, msr_info.data);
> > 
> > Same comment on printing the name.  Might be work adding a helper function
> > or macro?  That'd also avoid blasting past the 80-column guideline.
> 
> Thanks for the review, Sean. I believe that in one of the prior sets I
> mailed out you had mentioned a macro for this as well, but was a fix
> throughout KVM. Shall I introduce the macro as part of this series, but
> only apply it to my changes (and fix other call sites later on)?

Heh, I'm not sure if by "KVM wide" I was thinking just interface to
kvm_set_msr() and kvm_get_msr(), or if I was thiking this specific
pr_debug line as well.

But, it's a moot point if we can pre-check the vmcs12 value and thus
WARN on failure here.
diff mbox series

Patch

diff --git a/arch/x86/kvm/vmx/nested.c b/arch/x86/kvm/vmx/nested.c
index b0ca34bf4d21..9ba90b38d74b 100644
--- a/arch/x86/kvm/vmx/nested.c
+++ b/arch/x86/kvm/vmx/nested.c
@@ -2281,6 +2281,7 @@  static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
 {
 	struct vcpu_vmx *vmx = to_vmx(vcpu);
 	struct hv_enlightened_vmcs *hv_evmcs = vmx->nested.hv_evmcs;
+	struct msr_data msr_info;
 	bool load_guest_pdptrs_vmcs12 = false;
 
 	if (vmx->nested.dirty_vmcs12 || hv_evmcs) {
@@ -2404,6 +2405,16 @@  static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
 	if (!enable_ept)
 		vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
 
+	if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL) {
+		msr_info.host_initiated = false;
+		msr_info.index = MSR_CORE_PERF_GLOBAL_CTRL;
+		msr_info.data = vmcs12->guest_ia32_perf_global_ctrl;
+		if (kvm_set_msr(vcpu, &msr_info))
+			pr_debug_ratelimited(
+				"%s cannot write MSR (0x%x, 0x%llx)\n",
+				__func__, msr_info.index, msr_info.data);
+	}
+
 	kvm_rsp_write(vcpu, vmcs12->guest_rsp);
 	kvm_rip_write(vcpu, vmcs12->guest_rip);
 	return 0;