【问题标题】:How can a writer after a barrier be visible before a write preceding the barrier?屏障之后的写入器如何在屏障之前的写入之前可见?
【发布时间】:2014-08-04 23:01:18
【问题描述】:

在 linux 内核的内存屏障文档 (Documentation/memory-barriers.txt) 中,有一些示例显示内存屏障之后的写入器在其他 CPU 的内存屏障之前的写入之前可见。这怎么可能发生?为什么写屏障不足以对这些写入进行排序?

特别是以下内容:

843         CPU 1                   CPU 2
844         ======================= =======================
845                 { B = 7; X = 9; Y = 8; C = &Y }
846         STORE A = 1
847         STORE B = 2
848         <write barrier>
849         STORE C = &B            LOAD X
850         STORE D = 4             LOAD C (gets &B)
851                                 LOAD *C (reads B)
852 
853 Without intervention, CPU 2 may perceive the events on CPU 1 in some
854 effectively random order, despite the write barrier issued by CPU 1:
855 
856         +-------+       :      :                :       :
857         |       |       +------+                +-------+  | Sequence of update
858         |       |------>| B=2  |-----       --->| Y->8  |  | of perception on
859         |       |  :    +------+     \          +-------+  | CPU 2
860         | CPU 1 |  :    | A=1  |      \     --->| C->&Y |  V
861         |       |       +------+       |        +-------+
862         |       |   wwwwwwwwwwwwwwww   |        :       :
863         |       |       +------+       |        :       :
864         |       |  :    | C=&B |---    |        :       :       +-------+
865         |       |  :    +------+   \   |        +-------+       |       |
866         |       |------>| D=4  |    ----------->| C->&B |------>|       |
867         |       |       +------+       |        +-------+       |       |
868         +-------+       :      :       |        :       :       |       |
869                                        |        :       :       |       |
870                                        |        :       :       | CPU 2 |
871                                        |        +-------+       |       |
872             Apparently incorrect --->  |        | B->7  |------>|       |
873             perception of B (!)        |        +-------+       |       |
874                                        |        :       :       |       |
875                                        |        +-------+       |       |
876             The load of X holds --->    \       | X->9  |------>|       |
877             up the maintenance           \      +-------+       |       |
878             of coherence of B             ----->| B->2  |       +-------+
879                                                 +-------+
880                                                 :       :
881 
882 
883 In the above example, CPU 2 perceives that B is 7, despite the load of *C
884 (which would be B) coming after the LOAD of C.

【问题讨论】:

    标签: linux-kernel shared-memory memory-model


    【解决方案1】:

    写入屏障确实正确地排序了写入。

    如下文所述,问题在于 CPU 2 可以在 C 之前读取 *C,因为它不使用任何类型的读取屏障。

    【讨论】:

    • 由于存在隐式数据依赖(读C后才能读*C),目前还不清楚这是怎么回事。
    • 啊,很清楚。该示例试图解释在没有数据依赖障碍的情况下可能发生的情况。谢谢!
    • *C 的读取可以提前推测,并且可能已经在缓存中。
    【解决方案2】:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-24
      • 2021-04-01
      • 1970-01-01
      • 2013-08-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多