xen/arm: sched: Ensure the vCPU context is seen before vcpu_pause() returns
PATCH RESEND 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.
---
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
@@ -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
Hi Julien,
Quoted 12 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_runninghas 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.
The function vcpu_context_saved does contain a memory barrier already.
Shouldn’t we make sure instead that any time is_running is modified to false there is a barrier before (which is the case in vcpu_context_saved) ?
I understand the goal here but the barrier seem very far from the modification of is_running.
Cheers,
Bertrand
Quoted 52 lines
To prevent the corruption of the vCPU context, we need to insert a memory barrier instruction after
v->is_runningis observed and before the context is accessed. This barrier is added insync_vcpu_execstate()as it seems to be the place where we expect the synchronization to happen.Signed-off-by Julien Grall <[email protected]>---
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@@ -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
Quoted 9 lines · Bertrand Marquis
Hi Julien,
Quoted 7 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}.
Quoted 7 lines
Quoted 5 lines
However, the latest vCPU context can only be observed after
v->is_runninghas 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.
The function
vcpu_context_saveddoes contain a memory barrier already.
Memory barriers usually work in pair. We have a write barrier in vcpu_context_saved() but no read barrier in the code relying on the v->is_running.
Quoted 5 lines
Shouldn’t we make sure instead that any time
is_runningis modified to false there is a barrier before (which is the case invcpu_context_saved) ?I understand the goal here but the barrier seem very far from the modification of
is_running.
That's not what I am trying to fix (see above). Instead, this patch will ensure that when a pCPU observe v->is_running = false, then it can rely on the context of the vCPU to be valid.
Cheers,
Signature · 1 line
Julien Grall
Quoted 15 lines · Julien Grall
Quoted 8 lines · Bertrand Marquis
Hi Julien,
Quoted 7 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}.
Quoted 8 lines
Quoted 6 lines
Quoted 5 lines
However, the latest vCPU context can only be observed after
v->is_runninghas 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.
The function
vcpu_context_saveddoes contain a memory barrier already.Memory barriers usually work in pair. We have a write barrier in
vcpu_context_saved()but no read barrier in the code relying on thev->is_running.
Ok.
Quoted 7 lines
Quoted 5 lines
Shouldn’t we make sure instead that any time
is_runningis modified to false there is a barrier before (which is the case invcpu_context_saved) ? I understand the goal here but the barrier seem very far from the modification ofis_running.That's not what I am trying to fix (see above). Instead, this patch will ensure that when a pCPU observe
v->is_running= false, then it can rely on the context of the vCPU to be valid.
Ok you need a memory barrier after setting is_running to false, got it.
Cheers
Bertrand
Quoted 5 lines
Cheers,
Signature · 1 line
Julien Grall
Hi,
Quoted 13 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_runninghas 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.
Quoted 7 lines
To prevent the corruption of the vCPU context, we need to insert a memory barrier instruction after
v->is_runningis observed and before the context is accessed. This barrier is added insync_vcpu_execstate()as it seems to be the place where we expect the synchronization to happen.Signed-off-by Julien Grall <[email protected]>
Cheers
Bertrand
Quoted 44 lines
---
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@@ -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
Stefano Stabellini
Acked-byQuoted 15 lines · Bertrand Marquis
Hi,
Quoted 13 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_runninghas 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.
Quoted 8 lines
Quoted 7 lines
To prevent the corruption of the vCPU context, we need to insert a memory barrier instruction after
v->is_runningis observed and before the context is accessed. This barrier is added insync_vcpu_execstate()as it seems to be the place where we expect the synchronization to happen.Signed-off-by Julien Grall <[email protected]>Reviewed-by Bertrand Marquis <[email protected]>
Quoted 44 lines
Quoted 43 lines
---
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@@ -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