Skip to contentSkip to conversation

viridian: fix the HvFlushVirtualAddress/List hypercall implementation

PATCH In review Paul Durrant 2 participants 3 messages Feb 13, 2019 – Feb 14, 2019

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 viridian: fix the HvFlushVirtualAddress/List hypercall implementation

The current code uses hvm_asid_flush_vcpu() but this is insufficient for a guest running in shadow mode, which results in guest crashes early in boot if the 'hcall_remote_tlb_flush' is enabled.

This patch, instead of open coding a new flush algorithm, adapts the one already used by the HVMOP_flush_tlbs Xen hypercall. The implementation is modified to allow TLB flushing a subset of a domain's vCPUs. A callback function determines whether or not a vCPU requires flushing. This mechanism was chosen because, while it is the case that the currently implemented viridian hypercalls specify a vCPU mask, there are newer variants which specify a sparse HV_VP_SET and thus use of a callback will avoid needing to expose details of this outside of the viridian subsystem if and when those newer variants are implemented.

NOTE: Use of the common flush function requires that the hypercalls are
restartable and so, with this patch applied, viridian_hypercall() can now return HVM_HCALL_preempted. This is safe as no modification to struct cpu_user_regs is done before the return.

---
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>
Cc: Wei Liu <[email protected]>
Cc: "Roger Pau Monné" <[email protected]>

---
 xen/arch/x86/hvm/hvm.c               | 55 +++++++++++++++++++++-------
 xen/arch/x86/hvm/viridian/viridian.c | 41 +++++----------------
 xen/include/asm-x86/hvm/hvm.h        |  2 +
 3 files changed, 53 insertions(+), 45 deletions(-)
xen/arch/x86/hvm/hvm.c +41 -14
index 410623d437..5de2c2aec7 100644
@@ -3964,45 +3964,72 @@ static void hvm_s3_resume(struct domain *d)
     }
 }
 
-static int hvmop_flush_tlb_all(void)
+static DEFINE_PER_CPU(cpumask_t, flush_cpumask);
+
+bool hvm_flush_vcpu_tlb(bool (*flush_vcpu)(void *ctxt, struct vcpu *v),
+                        void *ctxt)
 {
+    cpumask_t *mask = &this_cpu(flush_cpumask);
     struct domain *d = current->domain;
     struct vcpu *v;
 
-    if ( !is_hvm_domain(d) )
-        return -EINVAL;
-
     /* Avoid deadlock if more than one vcpu tries this at the same time. */
     if ( !spin_trylock(&d->hypercall_deadlock_mutex) )
-        return -ERESTART;
+        return false;
 
     /* Pause all other vcpus. */
     for_each_vcpu ( d, v )
-        if ( v != current )
+        if ( v != current && flush_vcpu(ctxt, v) )
             vcpu_pause_nosync(v);
 
+    cpumask_clear(mask);
+
     /* Now that all VCPUs are signalled to deschedule, we wait... */
     for_each_vcpu ( d, v )
-        if ( v != current )
-            while ( !vcpu_runnable(v) && v->is_running )
-                cpu_relax();
+    {
+        unsigned int cpu;
+
+        if ( v == current || !flush_vcpu(ctxt, v) )
+            continue;
+
+        while ( !vcpu_runnable(v) && v->is_running )
+            cpu_relax();
+
+        cpu = read_atomic(&v->dirty_cpu);
+        if ( is_vcpu_dirty_cpu(cpu) )
+            __cpumask_set_cpu(cpu, mask);
+    }
 
     /* All other vcpus are paused, safe to unlock now. */
     spin_unlock(&d->hypercall_deadlock_mutex);
 
     /* Flush paging-mode soft state (e.g., va->gfn cache; PAE PDPE cache). */
     for_each_vcpu ( d, v )
-        paging_update_cr3(v, false);
+        if ( flush_vcpu(ctxt, v) )
+            paging_update_cr3(v, false);
 
-    /* Flush all dirty TLBs. */
-    flush_tlb_mask(d->dirty_cpumask);
+    /* Flush TLBs on all CPUs with dirty vcpu state. */
+    flush_tlb_mask(mask);
 
     /* Done. */
     for_each_vcpu ( d, v )
-        if ( v != current )
+        if ( v != current && flush_vcpu(ctxt, v) )
             vcpu_unpause(v);
 
-    return 0;
+    return true;
+}
+
+static bool always_flush(void *ctxt, struct vcpu *v)
+{
+    return true;
+}
+
+static int hvmop_flush_tlb_all(void)
+{
+    if ( !is_hvm_domain(current->domain) )
+        return -EINVAL;
+
+    return hvm_flush_vcpu_tlb(always_flush, NULL) ? 0 : -ERESTART;
 }
 
 static int hvmop_set_evtchn_upcall_vector(
xen/arch/x86/hvm/viridian/viridian.c +10 -31

61 lines not on this page · raw message

xen/include/asm-x86/hvm/hvm.h +2 -0
index 0a10b51554..53ffebb2c5 100644
@@ -338,6 +338,8 @@ const char *hvm_efer_valid(const struct vcpu *v, uint64_t value,
                            signed int cr0_pg);
 unsigned long hvm_cr4_guest_valid_bits(const struct domain *d, bool restore);
 
+bool hvm_flush_vcpu_tlb(bool (*flush_vcpu)(void *ctxt, struct vcpu *v),
+                        void *ctxt);
 
 #ifdef CONFIG_HVM
 
Signature · 7 lines
2.20.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#1 lore raw

xen/arch/x86/hvm/hvm.c static void hvm_s3_resume(struct domain *d) · line 3967

Quoted 9 lines
Quoted 1 line
Quoted 1 line · On 13.02.19 at 16:39, <[email protected]> wrote:
xen/arch/x86/hvm/hvm.c +1 -1
@@ -3964,45 +3964,72 @@ static void hvm_s3_resume(struct domain *d)
     }
 }
 
-static int hvmop_flush_tlb_all(void)
+static DEFINE_PER_CPU(cpumask_t, flush_cpumask);

It's not outright unacceptable (after all delete the other one from viridian.c), but this amount to half a kb of per-CPU data in a 4095-CPU build. Is there a reason not to use cpumask_scratch, given that the local CPU isn't undergoing any scheduling actions?

If it's going to stay, my minimal request would be for it to be moved into the (only) function using it.

Quoted 45 lines
xen/arch/x86/hvm/hvm.c +20 -8
+bool hvm_flush_vcpu_tlb(bool (*flush_vcpu)(void *ctxt, struct vcpu *v),
+                        void *ctxt)
 {
+    cpumask_t *mask = &this_cpu(flush_cpumask);
     struct domain *d = current->domain;
     struct vcpu *v;
 
-    if ( !is_hvm_domain(d) )
-        return -EINVAL;
-
     /* Avoid deadlock if more than one vcpu tries this at the same time. */
     if ( !spin_trylock(&d->hypercall_deadlock_mutex) )
-        return -ERESTART;
+        return false;
 
     /* Pause all other vcpus. */
     for_each_vcpu ( d, v )
-        if ( v != current )
+        if ( v != current && flush_vcpu(ctxt, v) )
             vcpu_pause_nosync(v);
 
+    cpumask_clear(mask);
+
     /* Now that all VCPUs are signalled to deschedule, we wait... */
     for_each_vcpu ( d, v )
-        if ( v != current )
-            while ( !vcpu_runnable(v) && v->is_running )
-                cpu_relax();
+    {
+        unsigned int cpu;
+
+        if ( v == current || !flush_vcpu(ctxt, v) )
+            continue;
+
+        while ( !vcpu_runnable(v) && v->is_running )
+            cpu_relax();
+
+        cpu = read_atomic(&v->dirty_cpu);
+        if ( is_vcpu_dirty_cpu(cpu) )
+            __cpumask_set_cpu(cpu, mask);
+    }
 
     /* All other vcpus are paused, safe to unlock now. */
     spin_unlock(&d->hypercall_deadlock_mutex);
 

xen/arch/x86/hvm/hvm.c static void hvm_s3_resume(struct domain *d) · line 4012

Quoted 10 lines
xen/arch/x86/hvm/hvm.c +4 -3
     /* Flush paging-mode soft state (e.g., va->gfn cache; PAE PDPE cache). */
     for_each_vcpu ( d, v )
-        paging_update_cr3(v, false);
+        if ( flush_vcpu(ctxt, v) )
+            paging_update_cr3(v, false);
 
-    /* Flush all dirty TLBs. */
-    flush_tlb_mask(d->dirty_cpumask);
+    /* Flush TLBs on all CPUs with dirty vcpu state. */
+    flush_tlb_mask(mask);

The logic above skips the local CPU when accumulating into mask. Previously there was no such special case, and the local CPU is guaranteed to have its bit set in d->dirty_cpumask.

I also think you'd better delay latching dirty state as much as possible, as the chances then grow for the dirty state to be gone by the time you evaluate it. By moving the latching into the loop right after dropping the lock, you'd deal with both issues at the same time.

xen/arch/x86/hvm/viridian/viridian.c void viridian_domain_deinit(struct domain *d) · line 438

Quoted 13 lines
xen/arch/x86/hvm/viridian/viridian.c +6 -1
@@ -430,7 +430,12 @@ void viridian_domain_deinit(struct domain *d)
         viridian_vcpu_deinit(v);
 }
 
-static DEFINE_PER_CPU(cpumask_t, ipi_cpumask);
+static bool need_flush(void *ctxt, struct vcpu *v)
+{
+    uint64_t vcpu_mask = *(uint64_t *)ctxt;
+
+    return vcpu_mask & (1ul << v->vcpu_id);
+}

I've been puzzled by similar code before - the Viridian interface continues to be limited to 64 CPUs? If so, as an unrelated change, shouldn't we deny turning on support for it on guests with more vCPU-s? Iirc Windows goes south in such a case.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#2 lore raw
Quoted 17 lines

-----Original Message-----
From: Jan Beulich [mailto:[email protected]]
Sent: 14 February 2019 10:43
To: Paul Durrant <[email protected]>
Cc: Andrew Cooper <[email protected]>; Roger Pau Monne <[email protected]>; Wei Liu <[email protected]>; xen-devel <xen- [email protected]>
Subject: Re: [PATCH] viridian: fix the HvFlushVirtualAddress/List hypercall implementation

Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 13.02.19 at 16:39, <[email protected]> wrote:
xen/arch/x86/hvm/hvm.c +0 -0
@@ -3964,45 +3964,72 @@ static void hvm_s3_resume(struct domain *d)
     }
 }
 
Quoted 7 lines

xen/arch/x86/hvm/hvm.c static void hvm_s3_resume(struct domain *d) · line 3967

Quoted 2 lines
xen/arch/x86/hvm/hvm.c +1 -1
-static int hvmop_flush_tlb_all(void)
+static DEFINE_PER_CPU(cpumask_t, flush_cpumask);

It's not outright unacceptable (after all delete the other one from viridian.c), but this amount to half a kb of per-CPU data in a 4095-CPU build. Is there a reason not to use cpumask_scratch, given that the local CPU isn't undergoing any scheduling actions?

Only that I was unaware of it! Yes, that would work perfectly well.

Quoted 57 lines

If it's going to stay, my minimal request would be for it to be moved into the (only) function using it.

xen/arch/x86/hvm/hvm.c static void hvm_s3_resume(struct domain *d)

Quoted 11 lines
xen/arch/x86/hvm/hvm.c +3 -3
+bool hvm_flush_vcpu_tlb(bool (*flush_vcpu)(void *ctxt, struct vcpu *v),
+                        void *ctxt)
 {
+    cpumask_t *mask = &this_cpu(flush_cpumask);
     struct domain *d = current->domain;
     struct vcpu *v;
 
-    if ( !is_hvm_domain(d) )
-        return -EINVAL;
-
     /* Avoid deadlock if more than one vcpu tries this at the same

time. */

Quoted 25 lines
xen/arch/x86/hvm/hvm.c +12 -5
     if ( !spin_trylock(&d->hypercall_deadlock_mutex) )
-        return -ERESTART;
+        return false;
 
     /* Pause all other vcpus. */
     for_each_vcpu ( d, v )
-        if ( v != current )
+        if ( v != current && flush_vcpu(ctxt, v) )
             vcpu_pause_nosync(v);
 
+    cpumask_clear(mask);
+
     /* Now that all VCPUs are signalled to deschedule, we wait... */
     for_each_vcpu ( d, v )
-        if ( v != current )
-            while ( !vcpu_runnable(v) && v->is_running )
-                cpu_relax();
+    {
+        unsigned int cpu;
+
+        if ( v == current || !flush_vcpu(ctxt, v) )
+            continue;
+
+        while ( !vcpu_runnable(v) && v->is_running )
+            cpu_relax();

xen/arch/x86/hvm/hvm.c static void hvm_s3_resume(struct domain *d)

Quoted 10 lines
xen/arch/x86/hvm/hvm.c +5 -0
+
+        cpu = read_atomic(&v->dirty_cpu);
+        if ( is_vcpu_dirty_cpu(cpu) )
+            __cpumask_set_cpu(cpu, mask);
+    }
 
     /* All other vcpus are paused, safe to unlock now. */
     spin_unlock(&d->hypercall_deadlock_mutex);
 
     /* Flush paging-mode soft state (e.g., va->gfn cache; PAE PDPE

cache). */

Quoted 5 lines
xen/arch/x86/hvm/hvm.c +2 -1
     for_each_vcpu ( d, v )
-        paging_update_cr3(v, false);
+        if ( flush_vcpu(ctxt, v) )
+            paging_update_cr3(v, false);
 
Quoted 6 lines

xen/arch/x86/hvm/hvm.c static void hvm_s3_resume(struct domain *d) · line 4012

Quoted 4 lines
xen/arch/x86/hvm/hvm.c +2 -2
-    /* Flush all dirty TLBs. */
-    flush_tlb_mask(d->dirty_cpumask);
+    /* Flush TLBs on all CPUs with dirty vcpu state. */
+    flush_tlb_mask(mask);

The logic above skips the local CPU when accumulating into mask.

Ah yes, that's an oversight.

Quoted 9 lines

Previously there was no such special case, and the local CPU is guaranteed to have its bit set in d->dirty_cpumask.

I also think you'd better delay latching dirty state as much as possible, as the chances then grow for the dirty state to be gone by the time you evaluate it. By moving the latching into the loop right after dropping the lock, you'd deal with both issues at the same time.

Yes... exactly what I was thinking after your previous comment.

Quoted 6 lines
Quoted 6 lines
xen/arch/x86/hvm/viridian/viridian.c +0 -0
@@ -430,7 +430,12 @@ void viridian_domain_deinit(struct domain *d)
         viridian_vcpu_deinit(v);
 }
 
Quoted 10 lines

xen/arch/x86/hvm/viridian/viridian.c void viridian_domain_deinit(struct domain *d) · line 438

Quoted 7 lines
xen/arch/x86/hvm/viridian/viridian.c +6 -1
-static DEFINE_PER_CPU(cpumask_t, ipi_cpumask);
+static bool need_flush(void *ctxt, struct vcpu *v)
+{
+    uint64_t vcpu_mask = *(uint64_t *)ctxt;
+
+    return vcpu_mask & (1ul << v->vcpu_id);
+}

I've been puzzled by similar code before - the Viridian interface continues to be limited to 64 CPUs?

Yes, in these hypercalls (and other similar ones). As I said in the commit comment, there are newer variants (having an 'Ex' suffix) that use a structure called an HV_VP_SET to allow more than more than 64 vcpus (by breaking cpus down into 'banks' of 64).

Quoted 3 lines

If so, as an unrelated
change, shouldn't we deny turning on support for it on guests with more vCPU-s? Iirc Windows goes south in such a case.

I think Windows really should not issuing these older hypercalls if the VM has more than 64 vCPUs but I guess there's still a risk it might use one if all the CPU indices for the particular hypercall happened to be <64. The right thing to do, of course, is add support for the new variants and I'll get to that a.s.a.p.

Paul

Quoted 3 lines

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel