kvm.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/2] KVM: VMX: fix invalid cpu passed to smp_call_function_single
@ 2012-11-28 12:53 Xiao Guangrong
  2012-11-28 12:54 ` [PATCH 2/2] KVM: VMX: fix memory order between loading vmcs and clearing vmcs Xiao Guangrong
  2012-11-29  0:06 ` [PATCH 1/2] KVM: VMX: fix invalid cpu passed to smp_call_function_single Marcelo Tosatti
  0 siblings, 2 replies; 7+ messages in thread
From: Xiao Guangrong @ 2012-11-28 12:53 UTC (permalink / raw)
  To: Avi Kivity; +Cc: Marcelo Tosatti, Gleb Natapov, LKML, KVM

In loaded_vmcs_clear, loaded_vmcs->cpu is the fist parameter passed to
smp_call_function_single, if the target cpu is downing (doing cpu hot remove),
loaded_vmcs->cpu can become -1 then -1 is passed to smp_call_function_single

It can be triggered when vcpu is being destroyed, loaded_vmcs_clear is called
in the preemptionable context

Signed-off-by: Xiao Guangrong <xiaoguangrong@linux.vnet.ibm.com>
---
 arch/x86/kvm/vmx.c |    8 +++++---
 1 files changed, 5 insertions(+), 3 deletions(-)

diff --git a/arch/x86/kvm/vmx.c b/arch/x86/kvm/vmx.c
index 6599e45..29e8f42 100644
--- a/arch/x86/kvm/vmx.c
+++ b/arch/x86/kvm/vmx.c
@@ -1007,9 +1007,11 @@ static void __loaded_vmcs_clear(void *arg)

 static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
 {
-	if (loaded_vmcs->cpu != -1)
-		smp_call_function_single(
-			loaded_vmcs->cpu, __loaded_vmcs_clear, loaded_vmcs, 1);
+	int cpu = loaded_vmcs->cpu;
+
+	if (cpu != -1)
+		smp_call_function_single(cpu,
+			 __loaded_vmcs_clear, loaded_vmcs, 1);
 }

 static inline void vpid_sync_vcpu_single(struct vcpu_vmx *vmx)
-- 
1.7.7.6

^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH 2/2] KVM: VMX: fix memory order between loading vmcs and clearing vmcs
  2012-11-28 12:53 [PATCH 1/2] KVM: VMX: fix invalid cpu passed to smp_call_function_single Xiao Guangrong
@ 2012-11-28 12:54 ` Xiao Guangrong
  2012-11-29  0:04   ` Marcelo Tosatti
  2012-11-29 23:15   ` Marcelo Tosatti
  2012-11-29  0:06 ` [PATCH 1/2] KVM: VMX: fix invalid cpu passed to smp_call_function_single Marcelo Tosatti
  1 sibling, 2 replies; 7+ messages in thread
From: Xiao Guangrong @ 2012-11-28 12:54 UTC (permalink / raw)
  To: Xiao Guangrong; +Cc: Avi Kivity, Marcelo Tosatti, Gleb Natapov, LKML, KVM

vmcs->cpu indicates whether it exists on the target cpu, -1 means the vmcs
does not exist on any vcpu

If vcpu load vmcs with vmcs.cpu = -1, it can be directly added to cpu's percpu
list. The list can be corrupted if the cpu prefetch the vmcs's list before
reading vmcs->cpu. Meanwhile, we should remove vmcs from the list before
making vmcs->vcpu == -1 be visible

Signed-off-by: Xiao Guangrong <xiaoguangrong@linux.vnet.ibm.com>
---
 arch/x86/kvm/vmx.c |   17 +++++++++++++++++
 1 files changed, 17 insertions(+), 0 deletions(-)

diff --git a/arch/x86/kvm/vmx.c b/arch/x86/kvm/vmx.c
index 29e8f42..6056d88 100644
--- a/arch/x86/kvm/vmx.c
+++ b/arch/x86/kvm/vmx.c
@@ -1002,6 +1002,15 @@ static void __loaded_vmcs_clear(void *arg)
 	if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
 		per_cpu(current_vmcs, cpu) = NULL;
 	list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
+
+	/*
+	 * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link
+	 * is before setting loaded_vmcs->vcpu to -1 which is done in
+	 * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist
+	 * then adds the vmcs into percpu list before it is deleted.
+	 */
+	smp_wmb();
+
 	loaded_vmcs_init(loaded_vmcs);
 }

@@ -1537,6 +1546,14 @@ static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)

 		kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
 		local_irq_disable();
+
+		/*
+		 * Read loaded_vmcs->cpu should be before fetching
+		 * loaded_vmcs->loaded_vmcss_on_cpu_link.
+		 * See the comments in __loaded_vmcs_clear().
+		 */
+		smp_rmb();
+
 		list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
 			 &per_cpu(loaded_vmcss_on_cpu, cpu));
 		local_irq_enable();
-- 
1.7.7.6

^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH 2/2] KVM: VMX: fix memory order between loading vmcs and clearing vmcs
  2012-11-28 12:54 ` [PATCH 2/2] KVM: VMX: fix memory order between loading vmcs and clearing vmcs Xiao Guangrong
@ 2012-11-29  0:04   ` Marcelo Tosatti
  2012-11-29  2:04     ` Xiao Guangrong
  2012-11-29  3:06     ` Xiao Guangrong
  2012-11-29 23:15   ` Marcelo Tosatti
  1 sibling, 2 replies; 7+ messages in thread
From: Marcelo Tosatti @ 2012-11-29  0:04 UTC (permalink / raw)
  To: Xiao Guangrong; +Cc: Avi Kivity, Gleb Natapov, LKML, KVM

On Wed, Nov 28, 2012 at 08:54:14PM +0800, Xiao Guangrong wrote:
> vmcs->cpu indicates whether it exists on the target cpu, -1 means the vmcs
> does not exist on any vcpu
> 
> If vcpu load vmcs with vmcs.cpu = -1, it can be directly added to cpu's percpu
> list. The list can be corrupted if the cpu prefetch the vmcs's list before
> reading vmcs->cpu. Meanwhile, we should remove vmcs from the list before
> making vmcs->vcpu == -1 be visible
> 
> Signed-off-by: Xiao Guangrong <xiaoguangrong@linux.vnet.ibm.com>
> ---
>  arch/x86/kvm/vmx.c |   17 +++++++++++++++++
>  1 files changed, 17 insertions(+), 0 deletions(-)
> 
> diff --git a/arch/x86/kvm/vmx.c b/arch/x86/kvm/vmx.c
> index 29e8f42..6056d88 100644
> --- a/arch/x86/kvm/vmx.c
> +++ b/arch/x86/kvm/vmx.c
> @@ -1002,6 +1002,15 @@ static void __loaded_vmcs_clear(void *arg)
>  	if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
>  		per_cpu(current_vmcs, cpu) = NULL;
>  	list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
> +
> +	/*
> +	 * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link
> +	 * is before setting loaded_vmcs->vcpu to -1 which is done in
> +	 * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist
> +	 * then adds the vmcs into percpu list before it is deleted.
> +	 */
> +	smp_wmb();
> +

Neither loads nor stores are reordered with like operations (see
section 8.2.3.2 of intel's volume 3). This behaviour makes the barrier
not necessary.

However, i agree access to loaded_vmcs is not obviously safe. I can't
tell its safe with vmm_exclusive = 0 (where vcpu->cpu can change at any
time).

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 1/2] KVM: VMX: fix invalid cpu passed to smp_call_function_single
  2012-11-28 12:53 [PATCH 1/2] KVM: VMX: fix invalid cpu passed to smp_call_function_single Xiao Guangrong
  2012-11-28 12:54 ` [PATCH 2/2] KVM: VMX: fix memory order between loading vmcs and clearing vmcs Xiao Guangrong
@ 2012-11-29  0:06 ` Marcelo Tosatti
  1 sibling, 0 replies; 7+ messages in thread
From: Marcelo Tosatti @ 2012-11-29  0:06 UTC (permalink / raw)
  To: Xiao Guangrong; +Cc: Avi Kivity, Gleb Natapov, LKML, KVM

On Wed, Nov 28, 2012 at 08:53:15PM +0800, Xiao Guangrong wrote:
> In loaded_vmcs_clear, loaded_vmcs->cpu is the fist parameter passed to
> smp_call_function_single, if the target cpu is downing (doing cpu hot remove),
> loaded_vmcs->cpu can become -1 then -1 is passed to smp_call_function_single
> 
> It can be triggered when vcpu is being destroyed, loaded_vmcs_clear is called
> in the preemptionable context
> 
> Signed-off-by: Xiao Guangrong <xiaoguangrong@linux.vnet.ibm.com>
> ---
>  arch/x86/kvm/vmx.c |    8 +++++---
>  1 files changed, 5 insertions(+), 3 deletions(-)

Applied, thanks.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 2/2] KVM: VMX: fix memory order between loading vmcs and clearing vmcs
  2012-11-29  0:04   ` Marcelo Tosatti
@ 2012-11-29  2:04     ` Xiao Guangrong
  2012-11-29  3:06     ` Xiao Guangrong
  1 sibling, 0 replies; 7+ messages in thread
From: Xiao Guangrong @ 2012-11-29  2:04 UTC (permalink / raw)
  To: Marcelo Tosatti; +Cc: Avi Kivity, Gleb Natapov, LKML, KVM

On 11/29/2012 08:04 AM, Marcelo Tosatti wrote:
> On Wed, Nov 28, 2012 at 08:54:14PM +0800, Xiao Guangrong wrote:
>> vmcs->cpu indicates whether it exists on the target cpu, -1 means the vmcs
>> does not exist on any vcpu
>>
>> If vcpu load vmcs with vmcs.cpu = -1, it can be directly added to cpu's percpu
>> list. The list can be corrupted if the cpu prefetch the vmcs's list before
>> reading vmcs->cpu. Meanwhile, we should remove vmcs from the list before
>> making vmcs->vcpu == -1 be visible
>>
>> Signed-off-by: Xiao Guangrong <xiaoguangrong@linux.vnet.ibm.com>
>> ---
>>  arch/x86/kvm/vmx.c |   17 +++++++++++++++++
>>  1 files changed, 17 insertions(+), 0 deletions(-)
>>
>> diff --git a/arch/x86/kvm/vmx.c b/arch/x86/kvm/vmx.c
>> index 29e8f42..6056d88 100644
>> --- a/arch/x86/kvm/vmx.c
>> +++ b/arch/x86/kvm/vmx.c
>> @@ -1002,6 +1002,15 @@ static void __loaded_vmcs_clear(void *arg)
>>  	if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
>>  		per_cpu(current_vmcs, cpu) = NULL;
>>  	list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
>> +
>> +	/*
>> +	 * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link
>> +	 * is before setting loaded_vmcs->vcpu to -1 which is done in
>> +	 * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist
>> +	 * then adds the vmcs into percpu list before it is deleted.
>> +	 */
>> +	smp_wmb();
>> +
> 
> Neither loads nor stores are reordered with like operations (see
> section 8.2.3.2 of intel's volume 3). This behaviour makes the barrier
> not necessary.

Ouch, yes, you are right. My memory is wrong. It seems only later-read
can be reordered with early-write.

But if 'read vs read' and 'write vs write' can be guaranteed by CPU, smp_wmb()
and smp_rmb() should only be a complier barrier, so i think we can add the barriers
to improve the readable and the portable.

And anyway, the current code missed complier-barrier.

> 
> However, i agree access to loaded_vmcs is not obviously safe. I can't
> tell its safe with vmm_exclusive = 0 (where vcpu->cpu can change at any
> time).

If vmm_exclusive = 0, the vmcs can removed from percpu list when vcpu is scheduled
out. The list is not broken.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 2/2] KVM: VMX: fix memory order between loading vmcs and clearing vmcs
  2012-11-29  0:04   ` Marcelo Tosatti
  2012-11-29  2:04     ` Xiao Guangrong
@ 2012-11-29  3:06     ` Xiao Guangrong
  1 sibling, 0 replies; 7+ messages in thread
From: Xiao Guangrong @ 2012-11-29  3:06 UTC (permalink / raw)
  To: Marcelo Tosatti; +Cc: Avi Kivity, Gleb Natapov, LKML, KVM

On 11/29/2012 08:04 AM, Marcelo Tosatti wrote:
> On Wed, Nov 28, 2012 at 08:54:14PM +0800, Xiao Guangrong wrote:
>> vmcs->cpu indicates whether it exists on the target cpu, -1 means the vmcs
>> does not exist on any vcpu
>>
>> If vcpu load vmcs with vmcs.cpu = -1, it can be directly added to cpu's percpu
>> list. The list can be corrupted if the cpu prefetch the vmcs's list before
>> reading vmcs->cpu. Meanwhile, we should remove vmcs from the list before
>> making vmcs->vcpu == -1 be visible
>>
>> Signed-off-by: Xiao Guangrong <xiaoguangrong@linux.vnet.ibm.com>
>> ---
>>  arch/x86/kvm/vmx.c |   17 +++++++++++++++++
>>  1 files changed, 17 insertions(+), 0 deletions(-)
>>
>> diff --git a/arch/x86/kvm/vmx.c b/arch/x86/kvm/vmx.c
>> index 29e8f42..6056d88 100644
>> --- a/arch/x86/kvm/vmx.c
>> +++ b/arch/x86/kvm/vmx.c
>> @@ -1002,6 +1002,15 @@ static void __loaded_vmcs_clear(void *arg)
>>  	if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
>>  		per_cpu(current_vmcs, cpu) = NULL;
>>  	list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
>> +
>> +	/*
>> +	 * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link
>> +	 * is before setting loaded_vmcs->vcpu to -1 which is done in
>> +	 * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist
>> +	 * then adds the vmcs into percpu list before it is deleted.
>> +	 */
>> +	smp_wmb();
>> +
> 
> Neither loads nor stores are reordered with like operations (see
> section 8.2.3.2 of intel's volume 3). This behaviour makes the barrier
> not necessary.

Ouch, yes, you are right. My memory is wrong. It seems only later-read
can be reordered with early-write.

But if 'read vs read' and 'write vs write' can be guaranteed by CPU, smp_wmb()
and smp_rmb() should act as a complier barrier, so i think we can add the barriers
to improve the readable and the portable.

And anyway, the current code missed complier-barrier.

> 
> However, i agree access to loaded_vmcs is not obviously safe. I can't
> tell its safe with vmm_exclusive = 0 (where vcpu->cpu can change at any
> time).

If vmm_exclusive = 0, the vmcs can removed from percpu list when vcpu is scheduled
out. The list is not broken.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 2/2] KVM: VMX: fix memory order between loading vmcs and clearing vmcs
  2012-11-28 12:54 ` [PATCH 2/2] KVM: VMX: fix memory order between loading vmcs and clearing vmcs Xiao Guangrong
  2012-11-29  0:04   ` Marcelo Tosatti
@ 2012-11-29 23:15   ` Marcelo Tosatti
  1 sibling, 0 replies; 7+ messages in thread
From: Marcelo Tosatti @ 2012-11-29 23:15 UTC (permalink / raw)
  To: Xiao Guangrong; +Cc: Avi Kivity, Gleb Natapov, LKML, KVM

On Wed, Nov 28, 2012 at 08:54:14PM +0800, Xiao Guangrong wrote:
> vmcs->cpu indicates whether it exists on the target cpu, -1 means the vmcs
> does not exist on any vcpu
> 
> If vcpu load vmcs with vmcs.cpu = -1, it can be directly added to cpu's percpu
> list. The list can be corrupted if the cpu prefetch the vmcs's list before
> reading vmcs->cpu. Meanwhile, we should remove vmcs from the list before
> making vmcs->vcpu == -1 be visible
> 
> Signed-off-by: Xiao Guangrong <xiaoguangrong@linux.vnet.ibm.com>
> ---
>  arch/x86/kvm/vmx.c |   17 +++++++++++++++++
>  1 files changed, 17 insertions(+), 0 deletions(-)

Applied, thanks.

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2012-11-29 23:15 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-11-28 12:53 [PATCH 1/2] KVM: VMX: fix invalid cpu passed to smp_call_function_single Xiao Guangrong
2012-11-28 12:54 ` [PATCH 2/2] KVM: VMX: fix memory order between loading vmcs and clearing vmcs Xiao Guangrong
2012-11-29  0:04   ` Marcelo Tosatti
2012-11-29  2:04     ` Xiao Guangrong
2012-11-29  3:06     ` Xiao Guangrong
2012-11-29 23:15   ` Marcelo Tosatti
2012-11-29  0:06 ` [PATCH 1/2] KVM: VMX: fix invalid cpu passed to smp_call_function_single Marcelo Tosatti

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).