Skip to contentSkip to conversation

viridian: fix the HvFlushVirtualAddress/List hypercall implementation

PATCH Superseded Paul Durrant 4 participants 5 messages Feb 14, 2019

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 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]>

v2:
- Use cpumask_scratch
- Make sure local CPU flush isn't missed

---
 xen/arch/x86/hvm/hvm.c               | 48 +++++++++++++++++++++-------
 xen/arch/x86/hvm/viridian/viridian.c | 45 ++++++++------------------
 xen/include/asm-x86/hvm/hvm.h        |  2 ++
 3 files changed, 53 insertions(+), 42 deletions(-)
xen/arch/x86/hvm/hvm.c +37 -11
index 410623d437..4ab4409242 100644
@@ -38,6 +38,7 @@
 #include <xen/warning.h>
 #include <xen/vpci.h>
 #include <xen/nospec.h>
+#include <xen/sched-if.h>
 #include <asm/shadow.h>
 #include <asm/hap.h>
 #include <asm/current.h>
@@ -3964,26 +3965,27 @@ static void hvm_s3_resume(struct domain *d)
     }
 }
 
-static int hvmop_flush_tlb_all(void)
+bool hvm_flush_vcpu_tlb(bool (*flush_vcpu)(void *ctxt, struct vcpu *v),
+                        void *ctxt)
 {
+    cpumask_t *mask = cpumask_scratch;
     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 )
+        if ( v != current && flush_vcpu(ctxt, v) )
             while ( !vcpu_runnable(v) && v->is_running )
                 cpu_relax();
 
@@ -3992,17 +3994,41 @@ static int hvmop_flush_tlb_all(void)
 
     /* Flush paging-mode soft state (e.g., va->gfn cache; PAE PDPE cache). */
     for_each_vcpu ( d, v )
+    {
+        unsigned int cpu;
+
+        if ( !flush_vcpu(ctxt, v) )
+            continue;
+
         paging_update_cr3(v, false);
 
-    /* Flush all dirty TLBs. */
-    flush_tlb_mask(d->dirty_cpumask);
+        cpu = read_atomic(&v->dirty_cpu);
+        if ( is_vcpu_dirty_cpu(cpu) )
+            __cpumask_set_cpu(cpu, mask);
+    }
+
+    /* 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 +14 -31

65 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.2.gb21ebb671


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

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.

Quoted 8 lines

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

v2:
- Use cpumask_scratch

That's not a good idea. cpumask_scratch may be used from other cpus as long as the respectice scheduler lock is being held. See the comment in include/xen/sched-if.h:

/*
* Scratch space, for avoiding having too many cpumask_t on the stack.
* Within each scheduler, when using the scratch mask of one pCPU:
* - the pCPU must belong to the scheduler,
* - the caller must own the per-pCPU scheduler lock (a.k.a. runqueue
* lock).
*/

So please don't use cpumask_scratch outside the scheduler!

Juergen

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

#2 lore raw
Quoted 57 lines

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

Quoted 6 lines · Paul Durrant

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

Quoted 2 lines

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

Quoted 3 lines

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

Quoted 1 line

expose details of this outside of the viridian subsystem if and when

those

Quoted 5 lines

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

Quoted 11 lines

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]>

v2:
- Use cpumask_scratch

That's not a good idea. cpumask_scratch may be used from other cpus as long as the respectice scheduler lock is being held. See the comment in include/xen/sched-if.h:

/*
* Scratch space, for avoiding having too many cpumask_t on the stack.
* Within each scheduler, when using the scratch mask of one pCPU:
* - the pCPU must belong to the scheduler,
* - the caller must own the per-pCPU scheduler lock (a.k.a. runqueue
* lock).
*/

Quoted 1 line

So please don't use cpumask_scratch outside the scheduler!

Ah, yes, it's because of cpumask_scratch_cpu()... I'd indeed missed that. In which case a dedicated flush_cpumask is still required.

Paul

Quoted 3 lines

Juergen

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

#3 lore raw
Quoted 20 lines
Quoted 20 lines
Quoted 1 line · On 14.02.19 at 13:38, <[email protected]> wrote:

From: Juergen Gross [mailto:[email protected]]
Sent: 14 February 2019 12:35

Quoted 2 lines · Paul Durrant

v2:
- Use cpumask_scratch

That's not a good idea. cpumask_scratch may be used from other cpus as long as the respectice scheduler lock is being held. See the comment in include/xen/sched-if.h:

/*
* Scratch space, for avoiding having too many cpumask_t on the stack.
* Within each scheduler, when using the scratch mask of one pCPU:
* - the pCPU must belong to the scheduler,
* - the caller must own the per-pCPU scheduler lock (a.k.a. runqueue
* lock).
*/

Quoted 4 lines
Quoted 1 line

So please don't use cpumask_scratch outside the scheduler!

Ah, yes, it's because of cpumask_scratch_cpu()... I'd indeed missed that. In which case a dedicated flush_cpumask is still required.

And I didn't recall this aspect either - I'm sorry for misguiding you. So Jürgen - thanks for spotting!

Jan

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

#4 lore raw
Quoted 19 lines · Jan Beulich
Quoted 19 lines
Quoted 19 lines
Quoted 1 line · On 14.02.19 at 13:38, <[email protected]> wrote:

From: Juergen Gross [mailto:[email protected]]
Sent: 14 February 2019 12:35

Quoted 2 lines · Paul Durrant

v2:
- Use cpumask_scratch

That's not a good idea. cpumask_scratch may be used from other cpus as long as the respectice scheduler lock is being held. See the comment in include/xen/sched-if.h:

/*
* Scratch space, for avoiding having too many cpumask_t on the stack.
* Within each scheduler, when using the scratch mask of one pCPU:
* - the pCPU must belong to the scheduler,
* - the caller must own the per-pCPU scheduler lock (a.k.a. runqueue
* lock).
*/

Quoted 5 lines
Quoted 3 lines
Quoted 1 line

So please don't use cpumask_scratch outside the scheduler!

Ah, yes, it's because of cpumask_scratch_cpu()... I'd indeed missed that. In which case a dedicated flush_cpumask is still required.

And I didn't recall this aspect either - I'm sorry for misguiding you. So Jürgen - thanks for spotting!

Can cpumask_scratch move into a scheduler private header file so the compiler won't let us accidentally make this mistake again?

~Andrew

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