Skip to contentSkip to conversation

x86/VPMU: Clear last_vcpu when destroying VPMU

PATCH Superseded Boris Ostrovsky 3 participants 6 messages Dec 12, 2014 – Dec 16, 2014

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/VPMU: Clear last_vcpu when destroying VPMU

We need to make sure that last_vcpu is not pointing to VCPU whose VPMU is being destroyed. Otherwise we may try dereference it in the future, when VCPU is gone.

---
 xen/arch/x86/hvm/vpmu.c |   22 ++++++++++++++++++++++
 1 files changed, 22 insertions(+), 0 deletions(-)

This needs to be backported to 4.3 and 4.4 as well

xen/arch/x86/hvm/vpmu.c +22 -0
index 1df74c2..6d39680 100644
@@ -247,10 +247,32 @@ void vpmu_initialise(struct vcpu *v)
     }
 }
 
+static void vpmu_clear_last(void *arg)
+{
+    struct vcpu *v = (struct vcpu *)arg;
+
+    if ( this_cpu(last_vcpu) == v )
+        this_cpu(last_vcpu) = NULL;
+}
+
 void vpmu_destroy(struct vcpu *v)
 {
     struct vpmu_struct *vpmu = vcpu_vpmu(v);
 
+    if ( vpmu_is_set(vpmu, VPMU_CONTEXT_ALLOCATED) )
+    {
+        /* Need to clear last_vcpu in case it points to v */
+        if ( vpmu->last_pcpu != smp_processor_id() )
+            on_selected_cpus(cpumask_of(vpmu->last_pcpu),
+                             vpmu_clear_last, (void *)v, 1);
+        else
+        {
+            local_irq_disable();
+            vpmu_clear_last((void *)v);
+            local_irq_enable();
+        }
+    }
+
     if ( vpmu->arch_vpmu_ops && vpmu->arch_vpmu_ops->arch_vpmu_destroy )
         vpmu->arch_vpmu_ops->arch_vpmu_destroy(v);
 }
Signature · 1 line
1.7.1
#1 lore raw
Quoted 5 lines · Boris Ostrovsky

We need to make sure that last_vcpu is not pointing to VCPU whose VPMU is being destroyed. Otherwise we may try dereference it in the future, when VCPU is gone.

Quoted 46 lines
---
 xen/arch/x86/hvm/vpmu.c |   22 ++++++++++++++++++++++
 1 files changed, 22 insertions(+), 0 deletions(-)

This needs to be backported to 4.3 and 4.4 as well

xen/arch/x86/hvm/vpmu.c +22 -0
index 1df74c2..6d39680 100644
@@ -247,10 +247,32 @@ void vpmu_initialise(struct vcpu *v)
     }
 }
 
+static void vpmu_clear_last(void *arg)
+{
+    struct vcpu *v = (struct vcpu *)arg;
+
+    if ( this_cpu(last_vcpu) == v )
+        this_cpu(last_vcpu) = NULL;
+}
+
 void vpmu_destroy(struct vcpu *v)
 {
     struct vpmu_struct *vpmu = vcpu_vpmu(v);
 
+    if ( vpmu_is_set(vpmu, VPMU_CONTEXT_ALLOCATED) )
+    {
+        /* Need to clear last_vcpu in case it points to v */
+        if ( vpmu->last_pcpu != smp_processor_id() )
+            on_selected_cpus(cpumask_of(vpmu->last_pcpu),
+                             vpmu_clear_last, (void *)v, 1);
+        else
+        {
+            local_irq_disable();
+            vpmu_clear_last((void *)v);
+            local_irq_enable();
+        }
+    }
+
     if ( vpmu->arch_vpmu_ops && vpmu->arch_vpmu_ops->arch_vpmu_destroy )
         vpmu->arch_vpmu_ops->arch_vpmu_destroy(v);
 }
Signature · 1 line
1.7.1
#2 lore raw
Quoted 7 lines · Konrad Rzeszutek Wilk
Quoted 5 lines · Boris Ostrovsky

We need to make sure that last_vcpu is not pointing to VCPU whose VPMU is being destroyed. Otherwise we may try dereference it in the future, when VCPU is gone.

I would like to send a slightly better patch on Monday (along the same lines but trying to avoid unnecessary IPIs if not needed).

-boris

Quoted 46 lines
Quoted 46 lines
---
  xen/arch/x86/hvm/vpmu.c |   22 ++++++++++++++++++++++
  1 files changed, 22 insertions(+), 0 deletions(-)

This needs to be backported to 4.3 and 4.4 as well

xen/arch/x86/hvm/vpmu.c +22 -0
index 1df74c2..6d39680 100644
@@ -247,10 +247,32 @@ void vpmu_initialise(struct vcpu *v)
      }
  }
 
+static void vpmu_clear_last(void *arg)
+{
+    struct vcpu *v = (struct vcpu *)arg;
+
+    if ( this_cpu(last_vcpu) == v )
+        this_cpu(last_vcpu) = NULL;
+}
+
  void vpmu_destroy(struct vcpu *v)
  {
      struct vpmu_struct *vpmu = vcpu_vpmu(v);
 
+    if ( vpmu_is_set(vpmu, VPMU_CONTEXT_ALLOCATED) )
+    {
+        /* Need to clear last_vcpu in case it points to v */
+        if ( vpmu->last_pcpu != smp_processor_id() )
+            on_selected_cpus(cpumask_of(vpmu->last_pcpu),
+                             vpmu_clear_last, (void *)v, 1);
+        else
+        {
+            local_irq_disable();
+            vpmu_clear_last((void *)v);
+            local_irq_enable();
+        }
+    }
+
      if ( vpmu->arch_vpmu_ops && vpmu->arch_vpmu_ops->arch_vpmu_destroy )
          vpmu->arch_vpmu_ops->arch_vpmu_destroy(v);
  }
Signature · 1 line
1.7.1
#3 lore raw
In reply to Boris Ostrovsky

xen/arch/x86/hvm/vpmu.c void vpmu_initialise(struct vcpu *v) · line 252

Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 12.12.14 at 22:20, <[email protected]> wrote:
xen/arch/x86/hvm/vpmu.c +3 -0
@@ -247,10 +247,32 @@ void vpmu_initialise(struct vcpu *v)
     }
 }
 
+static void vpmu_clear_last(void *arg)
+{
+    struct vcpu *v = (struct vcpu *)arg;

Please drop this pointless cast, or perhaps the entire variable, as ...

xen/arch/x86/hvm/vpmu.c void vpmu_initialise(struct vcpu *v) · line 254

Quoted 2 lines
xen/arch/x86/hvm/vpmu.c +2 -0
+
+    if ( this_cpu(last_vcpu) == v )

... you don't really need it to be of "struct vcpu *" type - "void *" is quite fine for a comparison.

xen/arch/x86/hvm/vpmu.c void vpmu_initialise(struct vcpu *v) · line 267

Quoted 13 lines
xen/arch/x86/hvm/vpmu.c +9 -0
+        this_cpu(last_vcpu) = NULL;
+}
+
 void vpmu_destroy(struct vcpu *v)
 {
     struct vpmu_struct *vpmu = vcpu_vpmu(v);
 
+    if ( vpmu_is_set(vpmu, VPMU_CONTEXT_ALLOCATED) )
+    {
+        /* Need to clear last_vcpu in case it points to v */
+        if ( vpmu->last_pcpu != smp_processor_id() )
+            on_selected_cpus(cpumask_of(vpmu->last_pcpu),
+                             vpmu_clear_last, (void *)v, 1);

The cast here is pointless too. But considering your subsequent reply this code may go away altogether anyway; if it doesn't, explaining (in the commit message) why you need to use an IPI here would seem necessary.

xen/arch/x86/hvm/vpmu.c void vpmu_initialise(struct vcpu *v) · line 271

Quoted 4 lines
xen/arch/x86/hvm/vpmu.c +4 -0
+        else
+        {
+            local_irq_disable();
+            vpmu_clear_last((void *)v);

And another pointless cast.

Jan

#4 lore raw
Quoted 24 lines · Jan Beulich

xen/arch/x86/hvm/vpmu.c void vpmu_initialise(struct vcpu *v) · line 252

Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 12.12.14 at 22:20, <[email protected]> wrote:
xen/arch/x86/hvm/vpmu.c +3 -0
@@ -247,10 +247,32 @@ void vpmu_initialise(struct vcpu *v)
      }
  }
 
+static void vpmu_clear_last(void *arg)
+{
+    struct vcpu *v = (struct vcpu *)arg;

Please drop this pointless cast, or perhaps the entire variable, as ...

xen/arch/x86/hvm/vpmu.c void vpmu_initialise(struct vcpu *v) · line 254

Quoted 2 lines
xen/arch/x86/hvm/vpmu.c +2 -0
+
+    if ( this_cpu(last_vcpu) == v )

... you don't really need it to be of "struct vcpu *" type - "void *" is quite fine for a comparison.

Quoted 7 lines
xen/arch/x86/hvm/vpmu.c +3 -0
+        this_cpu(last_vcpu) = NULL;
+}
+
  void vpmu_destroy(struct vcpu *v)
  {
      struct vpmu_struct *vpmu = vcpu_vpmu(v);
 
Quoted 10 lines

xen/arch/x86/hvm/vpmu.c void vpmu_initialise(struct vcpu *v) · line 267

Quoted 6 lines
xen/arch/x86/hvm/vpmu.c +6 -0
+    if ( vpmu_is_set(vpmu, VPMU_CONTEXT_ALLOCATED) )
+    {
+        /* Need to clear last_vcpu in case it points to v */
+        if ( vpmu->last_pcpu != smp_processor_id() )
+            on_selected_cpus(cpumask_of(vpmu->last_pcpu),
+                             vpmu_clear_last, (void *)v, 1);

The cast here is pointless too. But considering your subsequent reply this code may go away altogether anyway; if it doesn't, explaining (in the commit message) why you need to use an IPI here would seem necessary.

If I do simply

    if (per_cpu(last_vcpu, vpmu->last_pcpu) == v)
        per_cpu(last_vcpu, vpmu->last_pcpu) = NULL

then there is a (rather theoretical) possibility that between the test and subsequent clearing the remote cpu (i.e. vpmu->last_pcpu) will do load_vpmu() and then save_vpmu() for another VCPU. The former will clear last_vcpu and the latter will set last_vcpu to something else. And then the destroy_vpmu() will set it again to NULL, which is bad.

Doing it in in IPI will guarantee that nothing can happen between test and setting it to NULL.

Again, this very much theoretical, but that's why I have it. (BTW, doing this via IPI also preserves assumption that last_vcpu is always updated on local CPU.)

My changes for next version would make the need to do the IPIs less frequent. But if last_cpu needs to be cleared it would still be via IPI.

-boris

#5 lore raw
Quoted 38 lines
Quoted 1 line
Quoted 1 line · On 15.12.14 at 18:15, <[email protected]> wrote:
Quoted 24 lines · Jan Beulich

xen/arch/x86/hvm/vpmu.c void vpmu_initialise(struct vcpu *v) · line 252

Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 12.12.14 at 22:20, <[email protected]> wrote:
xen/arch/x86/hvm/vpmu.c +3 -0
@@ -247,10 +247,32 @@ void vpmu_initialise(struct vcpu *v)
      }
  }
 
+static void vpmu_clear_last(void *arg)
+{
+    struct vcpu *v = (struct vcpu *)arg;

Please drop this pointless cast, or perhaps the entire variable, as ...

xen/arch/x86/hvm/vpmu.c void vpmu_initialise(struct vcpu *v) · line 254

Quoted 2 lines
xen/arch/x86/hvm/vpmu.c +2 -0
+
+    if ( this_cpu(last_vcpu) == v )

... you don't really need it to be of "struct vcpu *" type - "void *" is quite fine for a comparison.

Quoted 7 lines
xen/arch/x86/hvm/vpmu.c +3 -0
+        this_cpu(last_vcpu) = NULL;
+}
+
  void vpmu_destroy(struct vcpu *v)
  {
      struct vpmu_struct *vpmu = vcpu_vpmu(v);
 
Quoted 10 lines

xen/arch/x86/hvm/vpmu.c void vpmu_initialise(struct vcpu *v) · line 267

Quoted 6 lines
xen/arch/x86/hvm/vpmu.c +6 -0
+    if ( vpmu_is_set(vpmu, VPMU_CONTEXT_ALLOCATED) )
+    {
+        /* Need to clear last_vcpu in case it points to v */
+        if ( vpmu->last_pcpu != smp_processor_id() )
+            on_selected_cpus(cpumask_of(vpmu->last_pcpu),
+                             vpmu_clear_last, (void *)v, 1);

The cast here is pointless too. But considering your subsequent reply this code may go away altogether anyway; if it doesn't, explaining (in the commit message) why you need to use an IPI here would seem necessary.

If I do simply

Quoted 8 lines
     if (per_cpu(last_vcpu, vpmu->last_pcpu) == v)
         per_cpu(last_vcpu, vpmu->last_pcpu) = NULL

then there is a (rather theoretical) possibility that between the test and subsequent clearing the remote cpu (i.e. vpmu->last_pcpu) will do load_vpmu() and then save_vpmu() for another VCPU. The former will clear last_vcpu and the latter will set last_vcpu to something else. And then the destroy_vpmu() will set it again to NULL, which is bad.

So how about using cmpxchg then?

Jan