diff mbox series

[01/29] KVM: VMX: Compare only a single byte for VMCS' "launched" in vCPU-run

Message ID 20190118212037.24412-2-sean.j.christopherson@intel.com (mailing list archive)
State New, archived
Headers show
Series KVM: VMX: Move vCPU-run to proper asm sub-routine | expand

Commit Message

Sean Christopherson Jan. 18, 2019, 9:20 p.m. UTC
The vCPU-run asm blob does a manual comparison of a VMCS' launched
status to execute the correct VM-Enter instruction, i.e. VMLAUNCH vs.
VMRESUME.  The launched flag is a bool, which is a typedef of _Bool.
C99 does not define an exact size for _Bool, stating only that is must
be large enough to hold '0' and '1'.  Most, if not all, compilers use
a single byte for _Bool, including gcc[1].

Originally, 'launched' was of type 'int' and so the asm blob used 'cmpl'
to check the launch status.  When 'launched' was moved to be stored on a
per-VMCS basis, struct vcpu_vmx's "temporary" __launched flag was added
in order to avoid having to pass the current VMCS into the asm blob.
The new  '__launched' was defined as a 'bool' and not an 'int', but the
'cmp' instruction was not updated.

This has not caused any known problems, likely due to compilers aligning
variables to 4-byte or 8-byte boundaries and KVM zeroing out struct
vcpu_vmx during allocation.  I.e. vCPU-run accesses "junk" data, it just
happens to always be zero and so doesn't affect the result.

[1] https://gcc.gnu.org/ml/gcc-patches/2000-10/msg01127.html

Fixes: d462b8192368 ("KVM: VMX: Keep list of loaded VMCSs, instead of vcpus")
Cc: <stable@vger.kernel.org>
Signed-off-by: Sean Christopherson <sean.j.christopherson@intel.com>
---
 arch/x86/kvm/vmx/vmx.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Comments

Jim Mattson Jan. 18, 2019, 10 p.m. UTC | #1
On Fri, Jan 18, 2019 at 1:22 PM Sean Christopherson
<sean.j.christopherson@intel.com> wrote:
>
> The vCPU-run asm blob does a manual comparison of a VMCS' launched
> status to execute the correct VM-Enter instruction, i.e. VMLAUNCH vs.
> VMRESUME.  The launched flag is a bool, which is a typedef of _Bool.
> C99 does not define an exact size for _Bool, stating only that is must
> be large enough to hold '0' and '1'.  Most, if not all, compilers use
> a single byte for _Bool, including gcc[1].
>
> Originally, 'launched' was of type 'int' and so the asm blob used 'cmpl'
> to check the launch status.  When 'launched' was moved to be stored on a
> per-VMCS basis, struct vcpu_vmx's "temporary" __launched flag was added
> in order to avoid having to pass the current VMCS into the asm blob.
> The new  '__launched' was defined as a 'bool' and not an 'int', but the
> 'cmp' instruction was not updated.
>
> This has not caused any known problems, likely due to compilers aligning
> variables to 4-byte or 8-byte boundaries and KVM zeroing out struct
> vcpu_vmx during allocation.  I.e. vCPU-run accesses "junk" data, it just
> happens to always be zero and so doesn't affect the result.
>
> [1] https://gcc.gnu.org/ml/gcc-patches/2000-10/msg01127.html
>
> Fixes: d462b8192368 ("KVM: VMX: Keep list of loaded VMCSs, instead of vcpus")
> Cc: <stable@vger.kernel.org>
> Signed-off-by: Sean Christopherson <sean.j.christopherson@intel.com>
Reviewed-by: Jim Mattson <jmattson@google.com>
Konrad Rzeszutek Wilk Jan. 22, 2019, 8:25 a.m. UTC | #2
On Fri, Jan 18, 2019 at 01:20:09PM -0800, Sean Christopherson wrote:
> The vCPU-run asm blob does a manual comparison of a VMCS' launched
> status to execute the correct VM-Enter instruction, i.e. VMLAUNCH vs.
> VMRESUME.  The launched flag is a bool, which is a typedef of _Bool.
> C99 does not define an exact size for _Bool, stating only that is must
> be large enough to hold '0' and '1'.  Most, if not all, compilers use
> a single byte for _Bool, including gcc[1].
> 
> Originally, 'launched' was of type 'int' and so the asm blob used 'cmpl'
> to check the launch status.  When 'launched' was moved to be stored on a
> per-VMCS basis, struct vcpu_vmx's "temporary" __launched flag was added
> in order to avoid having to pass the current VMCS into the asm blob.
> The new  '__launched' was defined as a 'bool' and not an 'int', but the
> 'cmp' instruction was not updated.
> 
> This has not caused any known problems, likely due to compilers aligning
> variables to 4-byte or 8-byte boundaries and KVM zeroing out struct
> vcpu_vmx during allocation.  I.e. vCPU-run accesses "junk" data, it just
> happens to always be zero and so doesn't affect the result.
> 
> [1] https://gcc.gnu.org/ml/gcc-patches/2000-10/msg01127.html
> 
> Fixes: d462b8192368 ("KVM: VMX: Keep list of loaded VMCSs, instead of vcpus")
> Cc: <stable@vger.kernel.org>
> Signed-off-by: Sean Christopherson <sean.j.christopherson@intel.com>
Reviewed-by: Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>

Thank you!
> ---
>  arch/x86/kvm/vmx/vmx.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c
> index 80262c7a4495..23e5fa58751a 100644
> --- a/arch/x86/kvm/vmx/vmx.c
> +++ b/arch/x86/kvm/vmx/vmx.c
> @@ -6401,7 +6401,7 @@ static void __vmx_vcpu_run(struct kvm_vcpu *vcpu, struct vcpu_vmx *vmx)
>  		"mov %%" _ASM_AX", %%cr2 \n\t"
>  		"3: \n\t"
>  		/* Check if vmlaunch or vmresume is needed */
> -		"cmpl $0, %c[launched](%%" _ASM_CX ") \n\t"
> +		"cmpb $0, %c[launched](%%" _ASM_CX ") \n\t"
>  		/* Load guest registers.  Don't clobber flags. */
>  		"mov %c[rax](%%" _ASM_CX "), %%" _ASM_AX " \n\t"
>  		"mov %c[rbx](%%" _ASM_CX "), %%" _ASM_BX " \n\t"
> -- 
> 2.20.1
>
diff mbox series

Patch

diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c
index 80262c7a4495..23e5fa58751a 100644
--- a/arch/x86/kvm/vmx/vmx.c
+++ b/arch/x86/kvm/vmx/vmx.c
@@ -6401,7 +6401,7 @@  static void __vmx_vcpu_run(struct kvm_vcpu *vcpu, struct vcpu_vmx *vmx)
 		"mov %%" _ASM_AX", %%cr2 \n\t"
 		"3: \n\t"
 		/* Check if vmlaunch or vmresume is needed */
-		"cmpl $0, %c[launched](%%" _ASM_CX ") \n\t"
+		"cmpb $0, %c[launched](%%" _ASM_CX ") \n\t"
 		/* Load guest registers.  Don't clobber flags. */
 		"mov %c[rax](%%" _ASM_CX "), %%" _ASM_AX " \n\t"
 		"mov %c[rbx](%%" _ASM_CX "), %%" _ASM_BX " \n\t"