This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

[ARMV8] dmb nshld vs dmb ishld -- practical differences?

Hello arm experts,

I am trying to understand when a load access of a memory location might produce side effects that other observers in the system may care about. So far all the examples I can find around dmb memory barriers in the ARMV8 reference material, are focused on observability of *writes*, whose importance and shareability domains are fairly self-explanatory. What I have not been able to find, is an example of when one might prefer dmb ishld over dmb nshld, for example. Whether the memory address is in shareable memory or not, or visible to coherent caches or not, surely a read access cannot produce observable effects that would affect the correctness of the PE executing the dmb instruction?

If this is correct, then why does ARMV8 offer various domains instead of simply some dmb ld with the least restrictive domain possible? And, if this is not correct, then what would be a practical example where the difference between dmb nshld, dmb ishld, and dmb oshld, would matter?

Thanks!

Parents
  • The share-ability argument is saying who has to see the guarantees of the barrier (NSH=just this observer, ISH=observers in the Inner domain...).  So the answer to your question is that you'd use ISHLD over NSHLD when it mattered that observers in the Inner share-ability domain saw the loads in order.

    Let's take an example;

    Thread 0     | Thread 1     ;
    MOV W0,#1    | LDR W0,[FLAG];
    STR W0,[MSG] | DMB xLD      ;
    DMB xST      | LDR W2,[MSG] ;
    MOV W2,#1    |              ;
    STR W2,[FLAG]|              ;

    Thread 0 is going to write a message (STR to MSG), then it is going to write a flag to say the message is valid (STR to FLAG).  Thread 1 does the reverse, it reads the Flag first and then the message.

    What we care about is that if Thread 1 sees the flag set then it MUST the message written also.  To ensure that, we put a DMB xST in Thread 0 and a DMB xLD in Thread 1.

    Now, share-ability.  If Thread 0 and Thread 1 both run on the same PE (i.e. same non-shareable domain), then we could replace x with OSH.  However, if the two threads might run on different PEs within the same Inner domain, then we need to replace x with ISH to get the guarantee we need.  Similarly, if the two threads ran on different PEs in the same Outer domain, then we'd need OSH. 

    There's a great tool for experimenting with ordering type questions Memory Model Tool (arm.com).  We can actually ask it that type of question.  Here's the above test converted into the format used by the tool:

    {
    0:X1=x; 0:X3=y;
    1:X1=x; 1:X3=y;
    }
    P0          | P1 ;
    MOV W0,#1   | LDR W0,[X3] ;
    STR W0,[X1] | DMB NSHLD ;
    DMB NSHST   | LDR W2,[X1] ;
    MOV W2,#1   | ;
    STR W2,[X3] | ;
    exists
    (1:X0=1 /\ 1:X2=0)

    The "exists (1:X0=1 /\ 1:X2=0)" line is a question to the tool.  It's saying "is it possible for P1 to end the test with X0=1 and X2=0", or "is it possible for P1 to end the test having seen the Flag but not the Message".

    If we run the test in the tool it says:

    Test MP Allowed
    States 4
    1:X0=0; 1:X2=0;
    1:X0=0; 1:X2=1;
    1:X0=1; 1:X2=0;
    1:X0=1; 1:X2=1;
    Ok
    Witnesses
    Positive: 1 Negative: 3


    So... yes, it is possible! There are four legal outcomes, one of which mataches the pattern we asked the tool to look for. 

    Meaning, if we specify NSH as the share-ability for the barriers, the ordering guarantee only applies to the Non-shareable domain.  As these are two different PEs, and therefore in different Non-shareable domains, the barriers are not enough to get the desired effect.

    Now, lets change it from NSH to ISH:

    {
    0:X1=x; 0:X3=y;
    1:X1=x; 1:X3=y;
    }
    P0          | P1 ;
    MOV W0,#1   | LDR W0,[X3] ;
    STR W0,[X1] | DMB ISHLD ;
    DMB ISHST   | LDR W2,[X1] ;
    MOV W2,#1   | ;
    STR W2,[X3] | ;
    exists
    (1:X0=1 /\ 1:X2=0)

    Now the model says:

    Test MP Allowed
    States 3
    1:X0=0; 1:X2=0;
    1:X0=0; 1:X2=1;
    1:X0=1; 1:X2=1;
    No
    Witnesses
    Positive: 0 Negative: 3


    Now the tool is saying there is no possible/legal result where P1 sees the flag but not the message.
Reply
  • The share-ability argument is saying who has to see the guarantees of the barrier (NSH=just this observer, ISH=observers in the Inner domain...).  So the answer to your question is that you'd use ISHLD over NSHLD when it mattered that observers in the Inner share-ability domain saw the loads in order.

    Let's take an example;

    Thread 0     | Thread 1     ;
    MOV W0,#1    | LDR W0,[FLAG];
    STR W0,[MSG] | DMB xLD      ;
    DMB xST      | LDR W2,[MSG] ;
    MOV W2,#1    |              ;
    STR W2,[FLAG]|              ;

    Thread 0 is going to write a message (STR to MSG), then it is going to write a flag to say the message is valid (STR to FLAG).  Thread 1 does the reverse, it reads the Flag first and then the message.

    What we care about is that if Thread 1 sees the flag set then it MUST the message written also.  To ensure that, we put a DMB xST in Thread 0 and a DMB xLD in Thread 1.

    Now, share-ability.  If Thread 0 and Thread 1 both run on the same PE (i.e. same non-shareable domain), then we could replace x with OSH.  However, if the two threads might run on different PEs within the same Inner domain, then we need to replace x with ISH to get the guarantee we need.  Similarly, if the two threads ran on different PEs in the same Outer domain, then we'd need OSH. 

    There's a great tool for experimenting with ordering type questions Memory Model Tool (arm.com).  We can actually ask it that type of question.  Here's the above test converted into the format used by the tool:

    {
    0:X1=x; 0:X3=y;
    1:X1=x; 1:X3=y;
    }
    P0          | P1 ;
    MOV W0,#1   | LDR W0,[X3] ;
    STR W0,[X1] | DMB NSHLD ;
    DMB NSHST   | LDR W2,[X1] ;
    MOV W2,#1   | ;
    STR W2,[X3] | ;
    exists
    (1:X0=1 /\ 1:X2=0)

    The "exists (1:X0=1 /\ 1:X2=0)" line is a question to the tool.  It's saying "is it possible for P1 to end the test with X0=1 and X2=0", or "is it possible for P1 to end the test having seen the Flag but not the Message".

    If we run the test in the tool it says:

    Test MP Allowed
    States 4
    1:X0=0; 1:X2=0;
    1:X0=0; 1:X2=1;
    1:X0=1; 1:X2=0;
    1:X0=1; 1:X2=1;
    Ok
    Witnesses
    Positive: 1 Negative: 3


    So... yes, it is possible! There are four legal outcomes, one of which mataches the pattern we asked the tool to look for. 

    Meaning, if we specify NSH as the share-ability for the barriers, the ordering guarantee only applies to the Non-shareable domain.  As these are two different PEs, and therefore in different Non-shareable domains, the barriers are not enough to get the desired effect.

    Now, lets change it from NSH to ISH:

    {
    0:X1=x; 0:X3=y;
    1:X1=x; 1:X3=y;
    }
    P0          | P1 ;
    MOV W0,#1   | LDR W0,[X3] ;
    STR W0,[X1] | DMB ISHLD ;
    DMB ISHST   | LDR W2,[X1] ;
    MOV W2,#1   | ;
    STR W2,[X3] | ;
    exists
    (1:X0=1 /\ 1:X2=0)

    Now the model says:

    Test MP Allowed
    States 3
    1:X0=0; 1:X2=0;
    1:X0=0; 1:X2=1;
    1:X0=1; 1:X2=1;
    No
    Witnesses
    Positive: 0 Negative: 3


    Now the tool is saying there is no possible/legal result where P1 sees the flag but not the message.
Children