Skip to contentSkip to conversation

xen/arm: sched: Ensure the vCPU context is seen before vcpu_pause() returns

PATCH Superseded Julien Grall 1 participant 2 messages Sep 22, 2020

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xen/arm: sched: Ensure the vCPU context is seen before vcpu_pause() returns

From: Julien Grall <[email protected]>

Some callers of vcpu_pause() will expect to access the latest vcpu context when the function returns (see XENDOMCTL_{set,get}vcpucontext}.

However, the latest vCPU context can only be observed after v->is_running has been observed to be false.

As there is no memory barrier instruction generated, a processor could try to speculatively access the vCPU context before it was observed.

To prevent the corruption of the vCPU context, we need to insert a memory barrier instruction after v->is_running is observed and before the context is accessed. This barrier is added in sync_vcpu_execstate() as it seems to be the place where we expect the synchronization to happen.

---

Cc Andrew Cooper <[email protected]>
Cc Jan Beulich <[email protected]>
Cc George Dunlap <[email protected]>
Cc Dario Faggioli <[email protected]>
Cc Bertrand Marquis <[email protected]>

I am also adding the x86 and scheduler maintainers because I am not sure whether this barrier should be part of the common code instead.

---
 xen/arch/arm/domain.c | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)
xen/arch/arm/domain.c +14 -1
index 9258f6d3faa2..3b37f899b9da 100644
@@ -371,7 +371,20 @@ void sync_local_execstate(void)
 
 void sync_vcpu_execstate(struct vcpu *v)
 {
-    /* Nothing to do -- no lazy switching */
+    /*
+     * We don't support lazy switching.
+     *
+     * However the context may have been saved from a remote pCPU so we
+     * need a barrier to ensure it is observed before continuing.
+     *
+     * Per vcpu_context_saved(), the context can be observed when
+     * v->is_running is false (the caller should check it before calling
+     * this function).
+     *
+     * Note this is a full barrier to also prevent update of the context
+     * to happen before it was observed.
+     */
+    smp_mb();
 }
 
 #define NEXT_ARG(fmt, args)                                                 \
Signature · 1 line
2.17.1
#1 lore raw

Doh, I forgot to CC Stefano on the e-mail. I will resend it.

Sorry for the inconvenience.

Quoted 60 lines · Julien Grall

From: Julien Grall <[email protected]>

Some callers of vcpu_pause() will expect to access the latest vcpu context when the function returns (see XENDOMCTL_{set,get}vcpucontext}.

However, the latest vCPU context can only be observed after v->is_running has been observed to be false.

As there is no memory barrier instruction generated, a processor could try to speculatively access the vCPU context before it was observed.

To prevent the corruption of the vCPU context, we need to insert a memory barrier instruction after v->is_running is observed and before the context is accessed. This barrier is added in sync_vcpu_execstate() as it seems to be the place where we expect the synchronization to happen.

---

Cc Andrew Cooper <[email protected]>
Cc Jan Beulich <[email protected]>
Cc George Dunlap <[email protected]>
Cc Dario Faggioli <[email protected]>
Cc Bertrand Marquis <[email protected]>

I am also adding the x86 and scheduler maintainers because I am not sure whether this barrier should be part of the common code instead.

---
  xen/arch/arm/domain.c | 15 ++++++++++++++-
  1 file changed, 14 insertions(+), 1 deletion(-)
xen/arch/arm/domain.c +14 -1
index 9258f6d3faa2..3b37f899b9da 100644
@@ -371,7 +371,20 @@ void sync_local_execstate(void)
 
  void sync_vcpu_execstate(struct vcpu *v)
  {
-    /* Nothing to do -- no lazy switching */
+    /*
+     * We don't support lazy switching.
+     *
+     * However the context may have been saved from a remote pCPU so we
+     * need a barrier to ensure it is observed before continuing.
+     *
+     * Per vcpu_context_saved(), the context can be observed when
+     * v->is_running is false (the caller should check it before calling
+     * this function).
+     *
+     * Note this is a full barrier to also prevent update of the context
+     * to happen before it was observed.
+     */
+    smp_mb();
  }
 
  #define NEXT_ARG(fmt, args)                                                 \
Signature · 1 line
Julien Grall