Skip to contentSkip to conversation

mm: fix LLVM code-generation issue

PATCH In review Roger Pau Monne 6 participants 24 messages Nov 22, 2018 – Nov 23, 2018 5 messages not in the archive

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mm: fix LLVM code-generation issue

LLVM code generation can attempt to perform a load from a variable in the next condition of an expression under certain circumstances, thus turning the following condition:

if ( system_state < SYS_STATE_active && opt_bootscrub == BOOTSCRUB_IDLE )

Into:

0xffff82d080223967 <+103>: cmpl   $0x3,0x37b032(%rip) # 0xffff82d08059e9a0 <system_state>
0xffff82d08022396e <+110>: setb   -0x29(%rbp)
0xffff82d080223972 <+114>: cmpl   $0x2,0x228a8b(%rip) # 0xffff82d08044c404 <opt_bootscrub>

Such code will trigger a page fault if system_state >= SYS_STATE_active.

In order to prevent such optimization signal to the compiler that accessing opt_bootscrub can have side effects by using ACCESS_ONCE. This has been reported and discussed with upstream LLVM:

https://bugs.llvm.org/show_bug.cgi?id=39707

I haven't been able to find any other instances of such conditional expression that uses system_state together with an init variable or function.

---
Cc: Andrew Cooper <[email protected]>
Cc: George Dunlap <[email protected]>
Cc: Ian Jackson <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Julien Grall <[email protected]>
Cc: Konrad Rzeszutek Wilk <[email protected]>
Cc: Stefano Stabellini <[email protected]>
Cc: Tim Deegan <[email protected]>
Cc: Wei Liu <[email protected]>

---
 xen/common/page_alloc.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
xen/common/page_alloc.c +11 -1
index 08ee8cfbb9..60adf6f64b 100644
@@ -1772,7 +1772,17 @@ static void init_heap_pages(
     first_valid_mfn = mfn_min(page_to_mfn(pg), first_valid_mfn);
     spin_unlock(&heap_lock);
 
-    if ( system_state < SYS_STATE_active && opt_bootscrub == BOOTSCRUB_IDLE )
+    if ( system_state < SYS_STATE_active &&
+         /*
+          * Use ACCESS_ONCE in order to let the compiler know accessing
+          * opt_bootscrub in this context can have side-effects (since it
+          * might be unmapped depending on the value of system_state).
+          * This prevents the compiler from attempting a load of
+          * opt_bootscrub before checking the value of system_state. See:
+          *
+          * https://bugs.llvm.org/show_bug.cgi?id=39707
+          */
+         ACCESS_ONCE(opt_bootscrub) == BOOTSCRUB_IDLE )
         idle_scrub = true;
 
     for ( i = 0; i < nr_pages; i++ )
Signature · 7 lines
2.19.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Acked-by on v1 1/1
#1 lore raw
Quoted 19 lines · Roger Pau Monne

LLVM code generation can attempt to perform a load from a variable in the next condition of an expression under certain circumstances, thus turning the following condition:

if ( system_state < SYS_STATE_active && opt_bootscrub == BOOTSCRUB_IDLE )

Into:

0xffff82d080223967 <+103>: cmpl   $0x3,0x37b032(%rip) # 0xffff82d08059e9a0 <system_state>
0xffff82d08022396e <+110>: setb   -0x29(%rbp)
0xffff82d080223972 <+114>: cmpl   $0x2,0x228a8b(%rip) # 0xffff82d08044c404 <opt_bootscrub>

Such code will trigger a page fault if system_state >= SYS_STATE_active.

In order to prevent such optimization signal to the compiler that accessing opt_bootscrub can have side effects by using ACCESS_ONCE. This has been reported and discussed with upstream LLVM:

Quoted 7 lines

https://bugs.llvm.org/show_bug.cgi?id=39707

I haven't been able to find any other instances of such conditional expression that uses system_state together with an init variable or function.

I think long term we should invent / adopt some systematical approach, but this will do for now.

I have written a QEMU based smoke test which will hopefully help us catch further issues quickly.

Wei.

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

#2 lore raw
In reply to Roger Pau Monne
Quoted 11 lines · Roger Pau Monne

LLVM code generation can attempt to perform a load from a variable in the next condition of an expression under certain circumstances, thus turning the following condition:

if ( system_state < SYS_STATE_active && opt_bootscrub == BOOTSCRUB_IDLE )

Into:

0xffff82d080223967 <+103>: cmpl   $0x3,0x37b032(%rip) # 0xffff82d08059e9a0 <system_state>
0xffff82d08022396e <+110>: setb   -0x29(%rbp)
0xffff82d080223972 <+114>: cmpl   $0x2,0x228a8b(%rip) # 0xffff82d08044c404 <opt_bootscrub>

You're actually missing two pieces here.  If I recall the disassembly correctly, there was a sete %al, and an and %al, -0x29(%rbp) which had the cumulative effect of calculating idle_scrub branchlessly (which is no doubt the intended optimisation).

Quoted 8 lines

Such code will trigger a page fault if system_state >= SYS_STATE_active.

In order to prevent such optimization signal to the compiler that accessing opt_bootscrub can have side effects by using ACCESS_ONCE. This has been reported and discussed with upstream LLVM:

Quoted 7 lines

https://bugs.llvm.org/show_bug.cgi?id=39707

I haven't been able to find any other instances of such conditional expression that uses system_state together with an init variable or function.

To unblock the Clang build, Acked-by: Andrew Cooper <[email protected]>, although ideally with the expanded disassembly.

I still think that longterm, we need to reconsider our position of referencing __init things from non-__init functions.

~Andrew

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

#3 lore raw

Hi Andrew,

Quoted 28 lines · Andrew Cooper
Quoted 11 lines · Roger Pau Monne

LLVM code generation can attempt to perform a load from a variable in the next condition of an expression under certain circumstances, thus turning the following condition:

if ( system_state < SYS_STATE_active && opt_bootscrub == BOOTSCRUB_IDLE )

Into:

0xffff82d080223967 <+103>: cmpl   $0x3,0x37b032(%rip) # 0xffff82d08059e9a0 <system_state>
0xffff82d08022396e <+110>: setb   -0x29(%rbp)
0xffff82d080223972 <+114>: cmpl   $0x2,0x228a8b(%rip) # 0xffff82d08044c404 <opt_bootscrub>

You're actually missing two pieces here.  If I recall the disassembly correctly, there was a sete %al, and an and %al, -0x29(%rbp) which had the cumulative effect of calculating idle_scrub branchlessly (which is no doubt the intended optimisation).

Quoted 10 lines

Such code will trigger a page fault if system_state >= SYS_STATE_active.

In order to prevent such optimization signal to the compiler that accessing opt_bootscrub can have side effects by using ACCESS_ONCE. This has been reported and discussed with upstream LLVM:

https://bugs.llvm.org/show_bug.cgi?id=39707

Quoted 8 lines
Quoted 5 lines

I haven't been able to find any other instances of such conditional expression that uses system_state together with an init variable or function.

To unblock the Clang build, Acked-by: Andrew Cooper <[email protected]>, although ideally with the expanded disassembly.

Actually is it enough in all the case? We are only preventing the re-ordering for the compiler. The processor is still free to re-order it and load opt_bootscrub before loading system_state.

Furthermore, this is relying on system_state to always be visible before the init section is freed. I am not entirely whether we have the explicit barrier for that.

Quoted 3 lines

I still think that longterm, we need to reconsider our position of referencing __init things from non-__init functions.

+1.

Cheers,

Signature · 6 lines
Julien Grall

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#4 lore raw
Quoted 38 lines
Quoted 1 line
Quoted 1 line · On 22.11.18 at 13:38, <[email protected]> wrote:

Hi Andrew,

Quoted 10 lines · Andrew Cooper
Quoted 9 lines · Roger Pau Monne

LLVM code generation can attempt to perform a load from a variable in the next condition of an expression under certain circumstances, thus turning the following condition:

if ( system_state < SYS_STATE_active && opt_bootscrub == BOOTSCRUB_IDLE )

Into:

0xffff82d080223967 <+103>: cmpl $0x3,0x37b032(%rip) # 0xffff82d08059e9a0

<system_state>

Quoted 2 lines
Quoted 2 lines

0xffff82d08022396e <+110>: setb -0x29(%rbp)
0xffff82d080223972 <+114>: cmpl $0x2,0x228a8b(%rip) # 0xffff82d08044c404

<opt_bootscrub>

Quoted 20 lines

You're actually missing two pieces here. If I recall the disassembly correctly, there was a sete %al, and an and %al, -0x29(%rbp) which had the cumulative effect of calculating idle_scrub branchlessly (which is no doubt the intended optimisation).

Quoted 14 lines

Such code will trigger a page fault if system_state >= SYS_STATE_active.

In order to prevent such optimization signal to the compiler that accessing opt_bootscrub can have side effects by using ACCESS_ONCE. This has been reported and discussed with upstream LLVM:

https://bugs.llvm.org/show_bug.cgi?id=39707

I haven't been able to find any other instances of such conditional expression that uses system_state together with an init variable or function.

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

To unblock the Clang build, Acked-by: Andrew Cooper <[email protected]>, although ideally with the expanded disassembly.

Actually is it enough in all the case? We are only preventing the re-ordering for the compiler. The processor is still free to re-order it and load opt_bootscrub before loading system_state.

The processor is fine to issue the load early, but it is not permitted to raise an exception resulting from that read attempt before the reading insn is the next one to retire.

Jan

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

#5 lore raw
Quoted 37 lines · Jan Beulich
Quoted 37 lines
Quoted 1 line
Quoted 1 line · On 22.11.18 at 13:38, <[email protected]> wrote:

Hi Andrew,

Quoted 10 lines · Andrew Cooper
Quoted 9 lines · Roger Pau Monne

LLVM code generation can attempt to perform a load from a variable in the next condition of an expression under certain circumstances, thus turning the following condition:

if ( system_state < SYS_STATE_active && opt_bootscrub == BOOTSCRUB_IDLE )

Into:

0xffff82d080223967 <+103>: cmpl $0x3,0x37b032(%rip) # 0xffff82d08059e9a0

<system_state>

Quoted 2 lines
Quoted 2 lines

0xffff82d08022396e <+110>: setb -0x29(%rbp)
0xffff82d080223972 <+114>: cmpl $0x2,0x228a8b(%rip) # 0xffff82d08044c404

<opt_bootscrub>

Quoted 19 lines

You're actually missing two pieces here. If I recall the disassembly correctly, there was a sete %al, and an and %al, -0x29(%rbp) which had the cumulative effect of calculating idle_scrub branchlessly (which is no doubt the intended optimisation).

Quoted 14 lines

Such code will trigger a page fault if system_state >= SYS_STATE_active.

In order to prevent such optimization signal to the compiler that accessing opt_bootscrub can have side effects by using ACCESS_ONCE. This has been reported and discussed with upstream LLVM:

https://bugs.llvm.org/show_bug.cgi?id=39707

I haven't been able to find any other instances of such conditional expression that uses system_state together with an init variable or function.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

To unblock the Clang build, Acked-by: Andrew Cooper <[email protected]>, although ideally with the expanded disassembly.

Actually is it enough in all the case? We are only preventing the re-ordering for the compiler. The processor is still free to re-order it and load opt_bootscrub before loading system_state.

The processor is fine to issue the load early, but it is not permitted to raise an exception resulting from that read attempt before the reading insn is the next one to retire.

I think Julien's point is that without explicitly barriers, CPU0's update to system_state may not be visible on CPU1, even though the mappings have been shot down.

Therefore, from the processors point of view, it did everything correctly, and hit a real pagefault.

~Andrew

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

#6 lore raw
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 22.11.18 at 14:31, <[email protected]> wrote:

I think Julien's point is that without explicitly barriers, CPU0's update to system_state may not be visible on CPU1, even though the mappings have been shot down.

Therefore, from the processors point of view, it did everything correctly, and hit a real pagefault.

Boot time updates of system_state should be of no interest here, as at that time the APs are all idling. The only other update is during S3 suspend/resume, in that case the mappings have been removed long ago.

Jan

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

#7 lore raw

Hi Jan,

Quoted 10 lines · Jan Beulich
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 22.11.18 at 14:31, <[email protected]> wrote:

I think Julien's point is that without explicitly barriers, CPU0's update to system_state may not be visible on CPU1, even though the mappings have been shot down.

Therefore, from the processors point of view, it did everything correctly, and hit a real pagefault.

Boot time updates of system_state should be of no interest here, as at that time the APs are all idling.

That's probably true today. But this code looks rather fragile as you don't know how this is going to be used in the future.

If you decide to gate init code with system_state, then you need a barrier to ensure the code is future proof.

Cheers,

Signature · 6 lines
Julien Grall

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#8 lore raw
Quoted 11 lines · Julien Grall

Hi Jan,

Quoted 8 lines · Jan Beulich
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 22.11.18 at 14:31, <[email protected]> wrote:

I think Julien's point is that without explicitly barriers, CPU0's update to system_state may not be visible on CPU1, even though the mappings have been shot down.

Therefore, from the processors point of view, it did everything correctly, and hit a real pagefault.

Quoted 8 lines
Quoted 2 lines

Boot time updates of system_state should be of no interest here, as at that time the APs are all idling.

That's probably true today. But this code looks rather fragile as you don't know how this is going to be used in the future.

If you decide to gate init code with system_state, then you need a barrier to ensure the code is future proof.

Wouldn't it be enough to declare system_state as volatile?

Roger.

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

#9 lore raw
Quoted 11 lines · Roger Pau Monné
Quoted 10 lines · Julien Grall

Hi Jan,

Quoted 7 lines · Jan Beulich
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 22.11.18 at 14:31, <[email protected]> wrote:

I think Julien's point is that without explicitly barriers, CPU0's update to system_state may not be visible on CPU1, even though the mappings have been shot down.

Therefore, from the processors point of view, it did everything correctly, and hit a real pagefault.

Quoted 8 lines
Quoted 7 lines
Quoted 2 lines

Boot time updates of system_state should be of no interest here, as at that time the APs are all idling.

That's probably true today. But this code looks rather fragile as you don't know how this is going to be used in the future.

If you decide to gate init code with system_state, then you need a barrier to ensure the code is future proof.

Wouldn't it be enough to declare system_state as volatile?

No.  volatility (or lack thereof) is a compiler level construct.

ARM has weaker cache coherency than x86, so a write which has completed on one CPU0 in the past may legitimately not be visible on CPU1 yet.

If you need guarantees about the visibility of updated, you must use appropriate barriers.

~Andrew

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

#10 lore raw
Quoted 21 lines · Andrew Cooper
Quoted 11 lines · Roger Pau Monné
Quoted 10 lines · Julien Grall

Hi Jan,

Quoted 7 lines · Jan Beulich
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 22.11.18 at 14:31, <[email protected]> wrote:

I think Julien's point is that without explicitly barriers, CPU0's update to system_state may not be visible on CPU1, even though the mappings have been shot down.

Therefore, from the processors point of view, it did everything correctly, and hit a real pagefault.

Quoted 8 lines
Quoted 7 lines
Quoted 2 lines

Boot time updates of system_state should be of no interest here, as at that time the APs are all idling.

That's probably true today. But this code looks rather fragile as you don't know how this is going to be used in the future.

If you decide to gate init code with system_state, then you need a barrier to ensure the code is future proof.

Wouldn't it be enough to declare system_state as volatile?

Quoted 7 lines

No.  volatility (or lack thereof) is a compiler level construct.

ARM has weaker cache coherency than x86, so a write which has completed on one CPU0 in the past may legitimately not be visible on CPU1 yet.

If you need guarantees about the visibility of updated, you must use appropriate barriers.

Right. There's some differences between ARM and x86, ARM sets SYS_STATE_active and continues to make use of init functions. In any case I have the following diff:

xen/arch/arm/setup.c +1 -0
index e83221ab79..cf50d05620 100644
@@ -966,6 +966,7 @@ void __init start_xen(unsigned long boot_phys_offset,
     serial_endboot();
 
     system_state = SYS_STATE_active;
+    smp_wmb();
 
     create_domUs();
 
xen/arch/x86/setup.c +1 -0
index 9cbff22fb3..41044c0b6f 100644
@@ -593,6 +593,7 @@ static void noinline init_done(void)
     unsigned long start, end;
 
     system_state = SYS_STATE_active;
+    smp_wmb();
 
     domain_unpause_by_systemcontroller(dom0);
 

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

#11 lore raw
Quoted 50 lines · Roger Pau Monné
Quoted 19 lines · Andrew Cooper
Quoted 18 lines · Roger Pau Monné
Quoted 17 lines · Julien Grall

Hi Jan,

Quoted 9 lines · Jan Beulich
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 22.11.18 at 14:31, <[email protected]> wrote:

I think Julien's point is that without explicitly barriers, CPU0's update to system_state may not be visible on CPU1, even though the mappings have been shot down.

Therefore, from the processors point of view, it did everything correctly, and hit a real pagefault.

Boot time updates of system_state should be of no interest here, as at that time the APs are all idling.

That's probably true today. But this code looks rather fragile as you don't know how this is going to be used in the future.

If you decide to gate init code with system_state, then you need a barrier to ensure the code is future proof.

Quoted 8 lines
Quoted 1 line

Wouldn't it be enough to declare system_state as volatile?

No.  volatility (or lack thereof) is a compiler level construct.

ARM has weaker cache coherency than x86, so a write which has completed on one CPU0 in the past may legitimately not be visible on CPU1 yet.

If you need guarantees about the visibility of updated, you must use appropriate barriers.

Right. There's some differences between ARM and x86, ARM sets SYS_STATE_active and continues to make use of init functions. In any case I have the following diff:

xen/arch/arm/setup.c +1 -0
index e83221ab79..cf50d05620 100644
@@ -966,6 +966,7 @@ void __init start_xen(unsigned long boot_phys_offset,
     serial_endboot();
 
     system_state = SYS_STATE_active;
+    smp_wmb();
 
     create_domUs();
 
xen/arch/x86/setup.c +0 -0
index 9cbff22fb3..41044c0b6f 100644

xen/arch/x86/setup.c static void noinline init_done(void) · line 599

Quoted 7 lines
xen/arch/x86/setup.c +1 -0
@@ -593,6 +593,7 @@ static void noinline init_done(void)
     unsigned long start, end;
 
     system_state = SYS_STATE_active;
+    smp_wmb();
 
     domain_unpause_by_systemcontroller(dom0);
 

I'm afraid that that won't do anything to help at all.

smp_{wmb,rmb}() must be in matched pairs, and mb() must be matched with itself.

~Andrew

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

#12 lore raw
Quoted 55 lines · Andrew Cooper
Quoted 54 lines · Roger Pau Monné
Quoted 19 lines · Andrew Cooper
Quoted 18 lines · Roger Pau Monné
Quoted 17 lines · Julien Grall

Hi Jan,

Quoted 9 lines · Jan Beulich
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 22.11.18 at 14:31, <[email protected]> wrote:

I think Julien's point is that without explicitly barriers, CPU0's update to system_state may not be visible on CPU1, even though the mappings have been shot down.

Therefore, from the processors point of view, it did everything correctly, and hit a real pagefault.

Boot time updates of system_state should be of no interest here, as at that time the APs are all idling.

That's probably true today. But this code looks rather fragile as you don't know how this is going to be used in the future.

If you decide to gate init code with system_state, then you need a barrier to ensure the code is future proof.

Quoted 8 lines
Quoted 1 line

Wouldn't it be enough to declare system_state as volatile?

No.  volatility (or lack thereof) is a compiler level construct.

ARM has weaker cache coherency than x86, so a write which has completed on one CPU0 in the past may legitimately not be visible on CPU1 yet.

If you need guarantees about the visibility of updated, you must use appropriate barriers.

Right. There's some differences between ARM and x86, ARM sets SYS_STATE_active and continues to make use of init functions. In any case I have the following diff:

xen/arch/arm/setup.c +1 -0
index e83221ab79..cf50d05620 100644
@@ -966,6 +966,7 @@ void __init start_xen(unsigned long boot_phys_offset,
     serial_endboot();
 
     system_state = SYS_STATE_active;
+    smp_wmb();
 
     create_domUs();
 
xen/arch/x86/setup.c +1 -0
index 9cbff22fb3..41044c0b6f 100644
@@ -593,6 +593,7 @@ static void noinline init_done(void)
     unsigned long start, end;
 
     system_state = SYS_STATE_active;
+    smp_wmb();
 
Quoted 8 lines

xen/arch/x86/setup.c static void noinline init_done(void) · line 599

Quoted 3 lines
xen/arch/x86/setup.c +0 -0
     domain_unpause_by_systemcontroller(dom0);
 

I'm afraid that that won't do anything to help at all.

smp_{wmb,rmb}() must be in matched pairs, and mb() must be matched with itself.

Then I'm not sure about whether our previous plan still stands, are we OK with using ACCESS_ONCE here and forgetting about the memory barriers given the current usage?

If we have to add memory barriers I think I prefer to just make opt_bootscrub non-init.

Roger.

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

#13 lore raw

Hi Roger,

Quoted 62 lines · Roger Pau Monné
Quoted 61 lines · Andrew Cooper
Quoted 50 lines · Roger Pau Monné
Quoted 19 lines · Andrew Cooper
Quoted 18 lines · Roger Pau Monné
Quoted 17 lines · Julien Grall

Hi Jan,

Quoted 9 lines · Jan Beulich
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 22.11.18 at 14:31, <[email protected]> wrote:

I think Julien's point is that without explicitly barriers, CPU0's update to system_state may not be visible on CPU1, even though the mappings have been shot down.

Therefore, from the processors point of view, it did everything correctly, and hit a real pagefault.

Boot time updates of system_state should be of no interest here, as at that time the APs are all idling.

That's probably true today. But this code looks rather fragile as you don't know how this is going to be used in the future.

If you decide to gate init code with system_state, then you need a barrier to ensure the code is future proof.

Quoted 8 lines
Quoted 1 line

Wouldn't it be enough to declare system_state as volatile?

No.  volatility (or lack thereof) is a compiler level construct.

ARM has weaker cache coherency than x86, so a write which has completed on one CPU0 in the past may legitimately not be visible on CPU1 yet.

If you need guarantees about the visibility of updated, you must use appropriate barriers.

Right. There's some differences between ARM and x86, ARM sets SYS_STATE_active and continues to make use of init functions. In any case I have the following diff:

xen/arch/arm/setup.c +1 -0
index e83221ab79..cf50d05620 100644
@@ -966,6 +966,7 @@ void __init start_xen(unsigned long boot_phys_offset,
      serial_endboot();
 
      system_state = SYS_STATE_active;
+    smp_wmb();
 
      create_domUs();
 
xen/arch/x86/setup.c +0 -0
index 9cbff22fb3..41044c0b6f 100644

xen/arch/x86/setup.c static void noinline init_done(void) · line 599

Quoted 7 lines
xen/arch/x86/setup.c +1 -0
@@ -593,6 +593,7 @@ static void noinline init_done(void)
      unsigned long start, end;
 
      system_state = SYS_STATE_active;
+    smp_wmb();
 
      domain_unpause_by_systemcontroller(dom0);
 

I'm afraid that that won't do anything to help at all.

Quoted 6 lines
Quoted 2 lines

smp_{wmb,rmb}() must be in matched pairs, and mb() must be matched with itself.

Then I'm not sure about whether our previous plan still stands, are we OK with using ACCESS_ONCE here and forgetting about the memory barriers given the current usage?

The problem is not the current usage but how it could be used. Debugging memory ordering is quite a pain so I would prefer this to be fixed correctly.

Quoted 3 lines

If we have to add memory barriers I think I prefer to just make opt_bootscrub non-init.

The memory barriers would not be too bad as smp_wmb() on Arm only ensure that the write to system_state will be observed before any write after.

But opt_bootscrub in non-init is also a solution.

Cheers,

Signature · 6 lines
Julien Grall

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#14 lore raw
Quoted 66 lines · Julien Grall

Hi Roger,

Quoted 63 lines · Roger Pau Monné
Quoted 61 lines · Andrew Cooper
Quoted 54 lines · Roger Pau Monné
Quoted 22 lines · Andrew Cooper
Quoted 13 lines · Roger Pau Monné
Quoted 12 lines · Julien Grall

Hi Jan,

Quoted 9 lines · Jan Beulich
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 22.11.18 at 14:31, <[email protected]> wrote:

I think Julien's point is that without explicitly barriers, CPU0's update to system_state may not be visible on CPU1, even though the mappings have been shot down.

Therefore, from the processors point of view, it did everything correctly, and hit a real pagefault.

Boot time updates of system_state should be of no interest here, as at that time the APs are all idling.

Quoted 8 lines
Quoted 7 lines

That's probably true today. But this code looks rather fragile as you don't
know how this is going to be used in the future.

If you decide to gate init code with system_state, then you need a barrier
to ensure the code is future proof.

Wouldn't it be enough to declare system_state as volatile?

Quoted 8 lines

No.  volatility (or lack thereof) is a compiler level construct.

ARM has weaker cache coherency than x86, so a write which has completed
on one CPU0 in the past may legitimately not be visible on CPU1 yet.

If you need guarantees about the visibility of updated, you must use appropriate barriers.

Right. There's some differences between ARM and x86, ARM sets SYS_STATE_active and continues to make use of init functions. In any case I have the following diff:

xen/arch/arm/setup.c +1 -0
index e83221ab79..cf50d05620 100644
@@ -966,6 +966,7 @@ void __init start_xen(unsigned long
 boot_phys_offset,
       serial_endboot();
         system_state = SYS_STATE_active;
+    smp_wmb();
         create_domUs();

  diff --git a/xen/arch/x86/setup.c b/xen/arch/x86/setup.c index 9cbff22fb3..41044c0b6f 100644

xen/arch/x86/setup.c +1 -0
@@ -593,6 +593,7 @@ static void noinline init_done(void)
       unsigned long start, end;
         system_state = SYS_STATE_active;
+    smp_wmb();
         domain_unpause_by_systemcontroller(dom0);
Quoted 1 line

I'm afraid that that won't do anything to help at all.

smp_{wmb,rmb}() must be in matched pairs, and mb() must be matched with itself.

Quoted 7 lines
Quoted 3 lines

Then I'm not sure about whether our previous plan still stands, are we OK with using ACCESS_ONCE here and forgetting about the memory barriers given the current usage?

The problem is not the current usage but how it could be used. Debugging memory ordering is quite a pain so I would prefer this to be fixed correctly.

But in this case it wouldn't be a pain, because the only possible failure mode is if the processor faults trying to read opt_bootscrub, right?

-George

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

#15 lore raw
Quoted 71 lines · George Dunlap
Quoted 70 lines · Julien Grall

Hi Roger,

Quoted 66 lines · Roger Pau Monné
Quoted 59 lines · Andrew Cooper
Quoted 50 lines · Roger Pau Monné
Quoted 22 lines · Andrew Cooper
Quoted 13 lines · Roger Pau Monné
Quoted 12 lines · Julien Grall

Hi Jan,

Quoted 9 lines · Jan Beulich
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 22.11.18 at 14:31, <[email protected]> wrote:

I think Julien's point is that without explicitly barriers, CPU0's update to system_state may not be visible on CPU1, even though the mappings have been shot down.

Therefore, from the processors point of view, it did everything correctly, and hit a real pagefault.

Boot time updates of system_state should be of no interest here, as at that time the APs are all idling.

Quoted 8 lines
Quoted 7 lines

That's probably true today. But this code looks rather fragile as you don't
know how this is going to be used in the future.

If you decide to gate init code with system_state, then you need a barrier
to ensure the code is future proof.

Wouldn't it be enough to declare system_state as volatile?

Quoted 8 lines

No.  volatility (or lack thereof) is a compiler level construct.

ARM has weaker cache coherency than x86, so a write which has completed
on one CPU0 in the past may legitimately not be visible on CPU1 yet.

If you need guarantees about the visibility of updated, you must use appropriate barriers.

Right. There's some differences between ARM and x86, ARM sets SYS_STATE_active and continues to make use of init functions. In any case I have the following diff:

xen/arch/arm/setup.c +1 -0
index e83221ab79..cf50d05620 100644
@@ -966,6 +966,7 @@ void __init start_xen(unsigned long
 boot_phys_offset,
       serial_endboot();
         system_state = SYS_STATE_active;
+    smp_wmb();
         create_domUs();
   diff --git a/xen/arch/x86/setup.c b/xen/arch/x86/setup.c

index 9cbff22fb3..41044c0b6f 100644

xen/arch/x86/setup.c static void noinline init_done(void) · line 597

Quoted 5 lines
xen/arch/x86/setup.c +1 -0
@@ -593,6 +593,7 @@ static void noinline init_done(void)
       unsigned long start, end;
         system_state = SYS_STATE_active;
+    smp_wmb();
         domain_unpause_by_systemcontroller(dom0);
 

I'm afraid that that won't do anything to help at all.

Quoted 2 lines

smp_{wmb,rmb}() must be in matched pairs, and mb() must be matched with itself.

Then I'm not sure about whether our previous plan still stands, are we OK with using ACCESS_ONCE here and forgetting about the memory barriers given the current usage?

Quoted 6 lines
Quoted 3 lines

The problem is not the current usage but how it could be used. Debugging memory ordering is quite a pain so I would prefer this to be fixed correctly.

But in this case it wouldn't be a pain, because the only possible failure mode is if the processor faults trying to read opt_bootscrub, right?

Possibly. But I don't see any reason to defer the fix until someone comes up with unreliable crash.

Cheers,

Signature · 6 lines
Julien Grall

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#16 lore raw
Quoted 77 lines · Julien Grall
Quoted 74 lines · George Dunlap
Quoted 72 lines · Julien Grall

Hi Roger,

Quoted 59 lines · Roger Pau Monné
Quoted 58 lines · Andrew Cooper
Quoted 50 lines · Roger Pau Monné
Quoted 22 lines · Andrew Cooper
Quoted 13 lines · Roger Pau Monné
Quoted 12 lines · Julien Grall

Hi Jan,

Quoted 9 lines · Jan Beulich
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 22.11.18 at 14:31, <[email protected]> wrote:

I think Julien's point is that without explicitly barriers, CPU0's update to system_state may not be visible on CPU1, even though the mappings have been shot down.

Therefore, from the processors point of view, it did everything correctly, and hit a real pagefault.

Boot time updates of system_state should be of no interest here, as at that time the APs are all idling.

Quoted 8 lines
Quoted 7 lines

That's probably true today. But this code looks rather fragile as you don't
know how this is going to be used in the future.

If you decide to gate init code with system_state, then you need a barrier
to ensure the code is future proof.

Wouldn't it be enough to declare system_state as volatile?

Quoted 8 lines

No.  volatility (or lack thereof) is a compiler level construct.

ARM has weaker cache coherency than x86, so a write which has completed
on one CPU0 in the past may legitimately not be visible on CPU1 yet.

If you need guarantees about the visibility of updated, you must use appropriate barriers.

Right. There's some differences between ARM and x86, ARM sets SYS_STATE_active and continues to make use of init functions. In any case I have the following diff:

xen/arch/arm/setup.c +1 -0
index e83221ab79..cf50d05620 100644
@@ -966,6 +966,7 @@ void __init start_xen(unsigned long
 boot_phys_offset,
       serial_endboot();
         system_state = SYS_STATE_active;
+    smp_wmb();
         create_domUs();
   diff --git a/xen/arch/x86/setup.c b/xen/arch/x86/setup.c

index 9cbff22fb3..41044c0b6f 100644

xen/arch/x86/setup.c static void noinline init_done(void) · line 596

Quoted 4 lines
xen/arch/x86/setup.c +1 -0
@@ -593,6 +593,7 @@ static void noinline init_done(void)
       unsigned long start, end;
         system_state = SYS_STATE_active;
+    smp_wmb();
         domain_unpause_by_systemcontroller(dom0);

I'm afraid that that won't do anything to help at all.

Quoted 6 lines
Quoted 2 lines

smp_{wmb,rmb}() must be in matched pairs, and mb() must be matched with itself.

Then I'm not sure about whether our previous plan still stands, are we OK with using ACCESS_ONCE here and forgetting about the memory barriers given the current usage?

The problem is not the current usage but how it could be used. Debugging memory ordering is quite a pain so I would prefer this to be fixed correctly.

Quoted 5 lines
Quoted 2 lines

But in this case it wouldn't be a pain, because the only possible failure mode is if the processor faults trying to read opt_bootscrub, right?

Possibly. But I don't see any reason to defer the fix until someone comes up with unreliable crash.

If we have to go down that route, shouldn't we also protect system_state with a lock so that it cannot be modified by a CPU while it's being read from another?

Image an AP reads system_state < SYS_STATE_active, then BSP sets system_state = SYS_STATE_active and clears the init mappings, then when the AP tries to read an init variable it would get a page fault.

Roger.

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

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

#22 lore raw
Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 23.11.18 at 12:23, <[email protected]> wrote:

Image an AP reads system_state < SYS_STATE_active, then BSP sets system_state = SYS_STATE_active and clears the init mappings, then when the AP tries to read an init variable it would get a page fault.

As said before - APs are supposed to be quiescent during this particular transition of system_state, and the idle scrubbing added an exception to that rule. I don't see why such an exception may not involve exceptional treatment on the particular reading path. Furthermore APs, which may also be brought up at run time, may never ever touch init mappings of any kind.

Jan

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

#23 lore raw

Hi Jan,

Quoted 11 lines · Jan Beulich
Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 23.11.18 at 12:23, <[email protected]> wrote:

Image an AP reads system_state < SYS_STATE_active, then BSP sets system_state = SYS_STATE_active and clears the init mappings, then when the AP tries to read an init variable it would get a page fault.

As said before - APs are supposed to be quiescent during this particular transition of system_state, and the idle scrubbing added an exception to that rule. I don't see why such an exception may not involve exceptional treatment on the particular reading path. Furthermore APs, which may also be brought up at run time, may never ever touch init mappings of any kind.

The "supposed" is quite worrying because we have no way to verify that assumption until we get a random crash.

Also, the cost of the barrier should be quite cheap over the rest of the function.

Cheers,

Signature · 6 lines
Julien Grall

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#24 lore raw
Quoted 15 lines · Julien Grall

Hi Jan,

Quoted 11 lines · Jan Beulich
Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 23.11.18 at 12:23, <[email protected]> wrote:

Image an AP reads system_state < SYS_STATE_active, then BSP sets system_state = SYS_STATE_active and clears the init mappings, then when the AP tries to read an init variable it would get a page fault.

As said before - APs are supposed to be quiescent during this particular transition of system_state, and the idle scrubbing added an exception to that rule. I don't see why such an exception may not involve exceptional treatment on the particular reading path. Furthermore APs, which may also be brought up at run time, may never ever touch init mappings of any kind.

Quoted 4 lines

The "supposed" is quite worrying because we have no way to verify that assumption until we get a random crash.

Also, the cost of the barrier should be quite cheap over the rest of the function.

Right, but unless I'm missing something that won't completely fix the issue either?

The only way I see to be completely safe is to either use a mutex or to make opt_bootscrub non-init.

Roger.

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

#25 lore raw
In reply to Roger Pau Monné
Quoted 81 lines · Roger Pau Monné
Quoted 80 lines · Julien Grall
Quoted 76 lines · George Dunlap
Quoted 65 lines · Julien Grall

Hi Roger,

Quoted 62 lines · Roger Pau Monné
Quoted 60 lines · Andrew Cooper
Quoted 50 lines · Roger Pau Monné
Quoted 22 lines · Andrew Cooper
Quoted 13 lines · Roger Pau Monné
Quoted 12 lines · Julien Grall

Hi Jan,

Quoted 9 lines · Jan Beulich
Quoted 7 lines
Quoted 1 line

On 22.11.18 at 14:31, <[email protected]> wrote:

I think Julien's point is that without explicitly barriers, CPU0's update to system_state may not be visible on CPU1, even though the mappings have been shot down.

Therefore, from the processors point of view, it did everything correctly, and hit a real pagefault.

Boot time updates of system_state should be of no interest here, as at that time the APs are all idling.

Quoted 8 lines
Quoted 7 lines

That's probably true today. But this code looks rather fragile as you don't
know how this is going to be used in the future.

If you decide to gate init code with system_state, then you need a barrier
to ensure the code is future proof.

Wouldn't it be enough to declare system_state as volatile?

Quoted 8 lines

No.  volatility (or lack thereof) is a compiler level construct.

ARM has weaker cache coherency than x86, so a write which has completed
on one CPU0 in the past may legitimately not be visible on CPU1 yet.

If you need guarantees about the visibility of updated, you must use appropriate barriers.

Right. There's some differences between ARM and x86, ARM sets SYS_STATE_active and continues to make use of init functions. In any case I have the following diff:

xen/arch/arm/setup.c +1 -0
index e83221ab79..cf50d05620 100644
@@ -966,6 +966,7 @@ void __init start_xen(unsigned long
 boot_phys_offset,
        serial_endboot();
          system_state = SYS_STATE_active;
+    smp_wmb();
          create_domUs();
    diff --git a/xen/arch/x86/setup.c b/xen/arch/x86/setup.c

index 9cbff22fb3..41044c0b6f 100644

xen/arch/x86/setup.c static void noinline init_done(void) · line 596

Quoted 4 lines
xen/arch/x86/setup.c +1 -0
@@ -593,6 +593,7 @@ static void noinline init_done(void)
        unsigned long start, end;
          system_state = SYS_STATE_active;
+    smp_wmb();
          domain_unpause_by_systemcontroller(dom0);

I'm afraid that that won't do anything to help at all.

smp_{wmb,rmb}() must be in matched pairs, and mb() must be matched with itself.

Quoted 7 lines
Quoted 3 lines

Then I'm not sure about whether our previous plan still stands, are we OK with using ACCESS_ONCE here and forgetting about the memory barriers given the current usage?

The problem is not the current usage but how it could be used. Debugging memory ordering is quite a pain so I would prefer this to be fixed correctly.

But in this case it wouldn't be a pain, because the only possible failure mode is if the processor faults trying to read opt_bootscrub, right?

Quoted 6 lines
Quoted 2 lines

Possibly. But I don't see any reason to defer the fix until someone comes up with unreliable crash.

If we have to go down that route, shouldn't we also protect system_state with a lock so that it cannot be modified by a CPU while it's being read from another?

The locking might be a bit too much. Modifying the system_state should not be an issue if you put the correct barrier in place.

Cheers,

Signature · 6 lines
Julien Grall

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#26 lore raw
Quoted 87 lines · Julien Grall
Quoted 84 lines · Roger Pau Monné
Quoted 82 lines · Julien Grall
Quoted 70 lines · George Dunlap
Quoted 69 lines · Julien Grall

Hi Roger,

Quoted 65 lines · Roger Pau Monné
Quoted 58 lines · Andrew Cooper
Quoted 50 lines · Roger Pau Monné
Quoted 22 lines · Andrew Cooper
Quoted 13 lines · Roger Pau Monné
Quoted 12 lines · Julien Grall

Hi Jan,

Quoted 9 lines · Jan Beulich
Quoted 7 lines

On 22.11.18 at 14:31, <[email protected]> wrote:
I think Julien's point is that without explicitly barriers, CPU0's
update to system_state may not be visible on CPU1, even though the
mappings have been shot down.

Therefore, from the processors point of view, it did everything
correctly, and hit a real pagefault.

Boot time updates of system_state should be of no interest here, as at that time the APs are all idling.

Quoted 8 lines
Quoted 7 lines

That's probably true today. But this code looks rather fragile as you don't
know how this is going to be used in the future.

If you decide to gate init code with system_state, then you need a barrier
to ensure the code is future proof.

Wouldn't it be enough to declare system_state as volatile?

Quoted 8 lines

No.  volatility (or lack thereof) is a compiler level construct.

ARM has weaker cache coherency than x86, so a write which has completed
on one CPU0 in the past may legitimately not be visible on CPU1 yet.

If you need guarantees about the visibility of updated, you must use appropriate barriers.

Right. There's some differences between ARM and x86, ARM sets SYS_STATE_active and continues to make use of init functions. In any case I have the following diff:

xen/arch/arm/setup.c +1 -0
index e83221ab79..cf50d05620 100644
@@ -966,6 +966,7 @@ void __init start_xen(unsigned long
 boot_phys_offset,
        serial_endboot();
          system_state = SYS_STATE_active;
+    smp_wmb();
          create_domUs();
    diff --git a/xen/arch/x86/setup.c b/xen/arch/x86/setup.c

index 9cbff22fb3..41044c0b6f 100644

xen/arch/x86/setup.c static void noinline init_done(void) · line 596

Quoted 4 lines
xen/arch/x86/setup.c +1 -0
@@ -593,6 +593,7 @@ static void noinline init_done(void)
        unsigned long start, end;
          system_state = SYS_STATE_active;
+    smp_wmb();
          domain_unpause_by_systemcontroller(dom0);

I'm afraid that that won't do anything to help at all.

Quoted 2 lines

smp_{wmb,rmb}() must be in matched pairs, and mb() must be matched with itself.

Then I'm not sure about whether our previous plan still stands, are we OK with using ACCESS_ONCE here and forgetting about the memory barriers given the current usage?

Quoted 6 lines
Quoted 3 lines

The problem is not the current usage but how it could be used. Debugging memory ordering is quite a pain so I would prefer this to be fixed correctly.

But in this case it wouldn't be a pain, because the only possible failure mode is if the processor faults trying to read opt_bootscrub, right?

Possibly. But I don't see any reason to defer the fix until someone comes up with unreliable crash.

Quoted 6 lines
Quoted 3 lines

If we have to go down that route, shouldn't we also protect system_state with a lock so that it cannot be modified by a CPU while it's being read from another?

The locking might be a bit too much. Modifying the system_state should not be an issue if you put the correct barrier in place.

What about the following scenario?

BSP write;wmb(); remove init mapping;

AP   rmb();read                                          read init var;
    -----------time------>

Yes matching barriers are in place, but the result is still wrong. Can this happen? Even if we make opt_bootscrub non-init to avoid the fault, we just defer the error to a later point.

This isn't really about coherency. Maybe we should put reading state under heap lock?

Wei.

Quoted 5 lines

Cheers,

Signature · 1 line
Julien Grall

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

#27 lore raw
Quoted 92 lines · Wei Liu
Quoted 91 lines · Julien Grall
Quoted 87 lines · Roger Pau Monné
Quoted 77 lines · Julien Grall
Quoted 74 lines · George Dunlap
Quoted 72 lines · Julien Grall

Hi Roger,

Quoted 59 lines · Roger Pau Monné
Quoted 58 lines · Andrew Cooper
Quoted 50 lines · Roger Pau Monné
Quoted 22 lines · Andrew Cooper
Quoted 13 lines · Roger Pau Monné
Quoted 12 lines · Julien Grall

Hi Jan,

Quoted 9 lines · Jan Beulich

On 22.11.18 at 14:31, <[email protected]> wrote:
I think Julien's point is that without explicitly barriers, CPU0's
update to system_state may not be visible on CPU1, even though the
mappings have been shot down.

Therefore, from the processors point of view, it did everything
correctly, and hit a real pagefault.
Boot time updates of system_state should be of no interest here,
as at that time the APs are all idling.

Quoted 8 lines
Quoted 7 lines

That's probably true today. But this code looks rather fragile as you don't
know how this is going to be used in the future.

If you decide to gate init code with system_state, then you need a barrier
to ensure the code is future proof.

Wouldn't it be enough to declare system_state as volatile?

Quoted 8 lines

No.  volatility (or lack thereof) is a compiler level construct.

ARM has weaker cache coherency than x86, so a write which has completed
on one CPU0 in the past may legitimately not be visible on CPU1 yet.

If you need guarantees about the visibility of updated, you must use appropriate barriers.

Right. There's some differences between ARM and x86, ARM sets SYS_STATE_active and continues to make use of init functions. In any case I have the following diff:

xen/arch/arm/setup.c +1 -0
index e83221ab79..cf50d05620 100644
@@ -966,6 +966,7 @@ void __init start_xen(unsigned long
 boot_phys_offset,
         serial_endboot();
           system_state = SYS_STATE_active;
+    smp_wmb();
           create_domUs();
     diff --git a/xen/arch/x86/setup.c b/xen/arch/x86/setup.c

index 9cbff22fb3..41044c0b6f 100644

xen/arch/x86/setup.c static void noinline init_done(void) · line 596

Quoted 4 lines
xen/arch/x86/setup.c +1 -0
@@ -593,6 +593,7 @@ static void noinline init_done(void)
         unsigned long start, end;
           system_state = SYS_STATE_active;
+    smp_wmb();
           domain_unpause_by_systemcontroller(dom0);

I'm afraid that that won't do anything to help at all.

Quoted 6 lines
Quoted 2 lines

smp_{wmb,rmb}() must be in matched pairs, and mb() must be matched with itself.

Then I'm not sure about whether our previous plan still stands, are we OK with using ACCESS_ONCE here and forgetting about the memory barriers given the current usage?

The problem is not the current usage but how it could be used. Debugging memory ordering is quite a pain so I would prefer this to be fixed correctly.

Quoted 5 lines
Quoted 2 lines

But in this case it wouldn't be a pain, because the only possible failure mode is if the processor faults trying to read opt_bootscrub, right?

Possibly. But I don't see any reason to defer the fix until someone comes up with unreliable crash.

If we have to go down that route, shouldn't we also protect system_state with a lock so that it cannot be modified by a CPU while it's being read from another?

Quoted 8 lines
Quoted 2 lines

The locking might be a bit too much. Modifying the system_state should not be an issue if you put the correct barrier in place.

What about the following scenario?

BSP write;wmb(); remove init mapping;

AP   rmb();read                                          read init var;
     -----------time------>

I don't think the barrier would be correctly placed for the AP. You would need to do:

read system_state; rmb(); read init var;

Quoted 4 lines

Yes matching barriers are in place, but the result is still wrong. Can
this happen?

Hmmm yes. Because there are still a chance that system_state != ACTIVE but by the time we read init it may have been clobbered.

Quoted 2 lines

Even if we make opt_bootscrub non-init to avoid the fault,
we just defer the error to a later point.

What would be the error if opt_bootscrub is read one more time?

Quoted 3 lines

This isn't really about coherency. Maybe we should put reading state under heap lock?

But to be honest, the problem is we are trying to read init data from core code.
Do we really want to keep that option init?

Cheers,

Signature · 6 lines
Julien Grall

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#28 lore raw
In reply to Andrew Cooper
Quoted 9 lines · Andrew Cooper
Quoted 8 lines · Roger Pau Monne

LLVM code generation can attempt to perform a load from a variable in the next condition of an expression under certain circumstances, thus turning the following condition:

if ( system_state < SYS_STATE_active && opt_bootscrub == BOOTSCRUB_IDLE )

Into:

Quoted 8 lines
Quoted 3 lines
0xffff82d080223967 <+103>: cmpl   $0x3,0x37b032(%rip) # 0xffff82d08059e9a0 <system_state>
0xffff82d08022396e <+110>: setb   -0x29(%rbp)
0xffff82d080223972 <+114>: cmpl   $0x2,0x228a8b(%rip) # 0xffff82d08044c404 <opt_bootscrub>

You're actually missing two pieces here.  If I recall the disassembly correctly, there was a sete %al, and an and %al, -0x29(%rbp) which had the cumulative effect of calculating idle_scrub branchlessly (which is no doubt the intended optimisation).

Right, my point here was to merely show that opt_bootscrub is accessed regardless of the value of system_state. I can expand the asm chunk, but the bug reference below already has the disassembly of the whole function.

Quoted 9 lines
Quoted 9 lines

Such code will trigger a page fault if system_state >= SYS_STATE_active.

In order to prevent such optimization signal to the compiler that accessing opt_bootscrub can have side effects by using ACCESS_ONCE. This has been reported and discussed with upstream LLVM:

https://bugs.llvm.org/show_bug.cgi?id=39707

Thanks, Roger.

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