diff mbox series

KVM: x86: Remove redundant vm_entry_controls_clearbit() call

Message ID 20220310111354.504565-1-zhenzhong.duan@intel.com (mailing list archive)
State New, archived
Headers show
Series KVM: x86: Remove redundant vm_entry_controls_clearbit() call | expand

Commit Message

Duan, Zhenzhong March 10, 2022, 11:13 a.m. UTC
When emulating exit from long mode, EFER_LMA is cleared which lead to
efer writing emulation, which will unset VM_ENTRY_IA32E_MODE control
bit as requested by SDM. So no need to unset VM_ENTRY_IA32E_MODE again
in exit_lmode() explicitly.

In fact benefited from shadow controls mechanism, this change doesn't
eliminate vmread or vmwrite.

Opportunistically remove unnecessory assignment to uret MSR data field
as vmx_setup_uret_msrs() will do the same thing.

In case EFER isn't supported by hardware, long mode isn't supported,
so this will no break.

Signed-off-by: Zhenzhong Duan <zhenzhong.duan@intel.com>
---
 arch/x86/kvm/vmx/vmx.c | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)

Comments

Sean Christopherson March 10, 2022, 5:14 p.m. UTC | #1
On Thu, Mar 10, 2022, Zhenzhong Duan wrote:
> When emulating exit from long mode, EFER_LMA is cleared which lead to
> efer writing emulation, which will unset VM_ENTRY_IA32E_MODE control
> bit as requested by SDM. So no need to unset VM_ENTRY_IA32E_MODE again
> in exit_lmode() explicitly.
> 
> In fact benefited from shadow controls mechanism, this change doesn't
> eliminate vmread or vmwrite.
> 
> Opportunistically remove unnecessory assignment to uret MSR data field
> as vmx_setup_uret_msrs() will do the same thing.

This needs to be a separate patch, it's much more subtle than "xyz will do the
same thing".  update_transition_efer() doesn't unconditionally set uret->data,
which on the surface makes this look suspect, but it's safe because uret->data
is consumed if and only if uret->load_into_hardware is true, and it's (a) set to
false if uret->data isn't updated and (b) uret->data is guaranteed to be updated
before it's set to true.

> In case EFER isn't supported by hardware, long mode isn't supported,
> so this will no break.
>
> 
> Signed-off-by: Zhenzhong Duan <zhenzhong.duan@intel.com>
> ---
>  arch/x86/kvm/vmx/vmx.c | 8 ++------
>  1 file changed, 2 insertions(+), 6 deletions(-)
> 
> diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c
> index b730d799c26e..b04588dc7faa 100644
> --- a/arch/x86/kvm/vmx/vmx.c
> +++ b/arch/x86/kvm/vmx/vmx.c
> @@ -2878,14 +2878,11 @@ int vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
>  		return 0;
>  
>  	vcpu->arch.efer = efer;
> -	if (efer & EFER_LMA) {
> +	if (efer & EFER_LMA)
>  		vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
> -		msr->data = efer;
> -	} else {
> +	else
>  		vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
>  
> -		msr->data = efer & ~EFER_LME;
> -	}

While you're doing opportunistic cleanups, drop the local "msr" and use "vmx"
directly instead of redoing to_vmx(), i.e. this

int vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
{
	struct vcpu_vmx *vmx = to_vmx(vcpu);

	/* Nothing to do if hardware doesn't support EFER. */
	if (!vmx_find_uret_msr(vmx, MSR_EFER))
		return 0;

	vcpu->arch.efer = efer;
	if (efer & EFER_LMA)
		vm_entry_controls_setbit(vmx, VM_ENTRY_IA32E_MODE);
	else
		vm_entry_controls_clearbit(vmx, VM_ENTRY_IA32E_MODE);

	vmx_setup_uret_msrs(vmx);
	return 0;
}
diff mbox series

Patch

diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c
index b730d799c26e..b04588dc7faa 100644
--- a/arch/x86/kvm/vmx/vmx.c
+++ b/arch/x86/kvm/vmx/vmx.c
@@ -2878,14 +2878,11 @@  int vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
 		return 0;
 
 	vcpu->arch.efer = efer;
-	if (efer & EFER_LMA) {
+	if (efer & EFER_LMA)
 		vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
-		msr->data = efer;
-	} else {
+	else
 		vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
 
-		msr->data = efer & ~EFER_LME;
-	}
 	vmx_setup_uret_msrs(vmx);
 	return 0;
 }
@@ -2911,7 +2908,6 @@  static void enter_lmode(struct kvm_vcpu *vcpu)
 
 static void exit_lmode(struct kvm_vcpu *vcpu)
 {
-	vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
 	vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
 }