or
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up
Syntax | Example | Reference | |
---|---|---|---|
# Header | Header | 基本排版 | |
- Unordered List |
|
||
1. Ordered List |
|
||
- [ ] Todo List |
|
||
> Blockquote | Blockquote |
||
**Bold font** | Bold font | ||
*Italics font* | Italics font | ||
~~Strikethrough~~ | |||
19^th^ | 19th | ||
H~2~O | H2O | ||
++Inserted text++ | Inserted text | ||
==Marked text== | Marked text | ||
[link text](https:// "title") | Link | ||
 | Image | ||
`Code` | Code |
在筆記中貼入程式碼 | |
```javascript var i = 0; ``` |
|
||
:smile: | ![]() |
Emoji list | |
{%youtube youtube_id %} | Externals | ||
$L^aT_eX$ | LaTeX | ||
:::info This is a alert area. ::: |
This is a alert area. |
On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?
Please give us some advice and help us improve HackMD.
Syncing
xxxxxxxxxx
從 memory model 的角度探討 Linux kernel 在 concurrency 的行為 - 翁敏維
由於場地問題,第二天我們移動到另一棟大樓啦!議程教室變動請見網站上的議程表。
歡迎來到 https://hackmd.io/@coscup/2019 共筆
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →點擊本頁上方的 開始用 Markdown 一起寫筆記!
手機版請點選上方 按鈕展開議程列表。
簡報模式
Agenda
Introduction
這裡會從 out-of-order、reordering 一路探討到 memory model,並以實驗來理解 atomic operation、memory barrier。
然後我們會看到 linux kernel 怎麼支援 concurrency,例如 membarrier。
What is reorder?
Arm 工程師 Will Deacon 在 Edinburgh 的演講 Uh-oh; it's I/O ordering 中提到:
初始條件:*x, *y 在記憶體裡,初始值為 0,foo, bar 是 local variable
a, b, c, d 存在著幾種可能的 交錯執行順序
{a, b, c, d}, {a, c, b, d}, {a, c, d, b}, {c, d, a, b},…
即便是這個順序 {a, b, c, d},foo 和 bar 的結果 有沒有可能會是 foo == bar == 0?
為什麼要探討 reordering?
看起來和 linux kernel 無關,也看不出和 concurrency 有關。
為什麼執行順序和 program order 不一樣?
當我們看到執行結果的時候,
對照一下 program order 所預期的結果,為什麼不一樣?
In-order and Out-of-Order
我們從這兩種順序來看 CPU 執行順序對效能及正確性的影響
in-order
當 processor 是用 in-order 在執行 instruction,
執行 2 個 instruction 需要 8 個 cycle。
out-of-order
如果是 out-of-order,執行 2 個 instruction 只需要 5 個 cycle。
diy, litmus, armmem
我們用這個工具來作一些實驗
linux kernel 使用 litmus 在檢驗 memory-ordering model。
為什麼突然講到 litmus 呢?
linux kernel 在檢驗 memory model 的時候,也是需要工具,這些工具包括了 diy, litmus, armmem。
如果從 C/C++ 檢驗 memory model,
還有一個工具 cppmem
cppmem 是由劍橋大學 The Computer Laboratory 所開發,以 web 為介面。
litmus 是在模擬什麼?
怎麼使用 diy, litmus 作實驗?
用 diy 產生 test case
透過修改 X.conf,可以產生 test case 來模擬 ARM 或是 x86
產生了 6 個 test case:arm000.litmus, arm001.litmus, …, arm005.litmus
然後就可以用 litmus 作實驗
litmus7 的 output 有一個
arm000.c
,我們在這個檔案裡可以看到探討 memory barrier 之前,我們先看 store buffer
What is store buffer?
在沒有
store buffer
之前,CPU 會直接對 cache 進行load
/store
,像以下這張圖這樣。Memory Barriers: a Hardware View for Software Hackers
有了 store buffer
load
會先從 cache 去讀。store
並不是直接寫入 cache,而是把store
先放進 store buffer。Data Memory Barrier (DMB)
因為是以 Arm 做實驗,所以我們先說明 DMB
Data Memory Barrier (DMB)
This prevents reordering of data accesses instructions across the DMB instruction
It also ensures that any explicit preceding data or unified cache maintenance operations have completed before any subsequent data accesses are executed
以上節錄自 Arm 官方文件
這個範例來自於 Professor Peter Sewell
(劍橋大學 Computer Laboratory)
[SB+dmbs]
我們將分別用 litmus7、armmem 來對 SB+dmbs 作實驗。
litmus7 實驗結果
0:R1=0; 1:R1=0
,原因之後會說明armmem 實驗結果
0:R1=0; 1:R1=0
為什麼 litmus7 和 armmem 實驗的結果不一樣?
暫停一下,目前為止的實驗是要證明什麼事?
這裡說明了 atomic operation 的必要性。
What is atomic operation?
以 Arm 架構的指令 (
ldrex
、strex
) 探討 atomic以上節錄自 spinlock
從這 3 個角度來理解 atomic operation
Mutual exclusion
Mutual exclusion
Dekker's algorithm
[wiki]
process
不是processor
Dekker's algorithm
process
進入critical section
process
進入了critical section
,下一個要進入critical section
的process
就會 busy wait,直到 P0 離開了critical section
(假設 P0 先進入critical section
)busy wait 會有什麼問題?
busy wait
表示起碼會有一個 最低限度的時間 是花在 等待 其他 process 離開critical section
。Exclusive Monitors
Exclusive monitors
Arm 的 Exclusive monitors 有兩種狀態:
open
和exclusive
。load 進行中的時候無法 store
如果 存在 著一個 exclusive monitor 還沒有 把狀態改成 exclusive,那麼 store 就不會成功。
Reference:https://www.cl.cam.ac.uk/~pes20/arm-supplemental/arm040.html#tst@SB+dmbs
當第 10 行的
STR R0, [%x0]
、STR R0, [%y1]
都 尚未 complete,第 12 行的LDR R1, [%y0]
、LDR R1, [%x1]
卻同時執行,會是什麼結果?dmb 保證 P0 的 LDR 沒有被 reordering,也保證了 P1 的 LDR 沒有被 reordering,但是少了 atomic,所以仍然有可能發生 P0: R1=0; P1: R1=0
回顧一開始的範例
WRITE_ONCE、READ_ONCE 是 atomic operation 嗎?
What is memory model?
memory model 和 linux kernel 有什麼關係?
memory model 和 concurrency 有什麼關係?
Memory model
不同的 CPU 會有不一樣的 memory model
我們分別探討 x64 和 Arm
x64
Intel® 64 Architecture Memory Ordering White Paper 對於 memory ordering 有 8 條 principle ,這裡舉其中一個例子
2.3. Loads may be reordered with older stores to different locations
mov r1, [_y] // M2
mov r2, [_x] // M4
r1 == 0 and r2 == 0 is allowed
Reference:
http://www.cs.cmu.edu/~410-f10/doc/Intel_Reordering_318147.pdf
Volumn 3A, 8.2 MEMORY ORDERING
我們以這個例子來看 strong memory model
8.2.3.3 Stores Are Not Reordered With Earlier Loads
mov [_y], 1 // M2
mov [_x], 1 // M4
r1 == 1 and r2 == 1 is not allowed
Reference:
https://software.intel.com/sites/default/files/managed/7c/f1/253668-sdm-vol-3a.pdf
M2 不會 reordering,M4 也不會 reordering
Arm
這裡以 ARMv8A 為例來說明 weakly ordered model
weakly memory model
the order of memory accesses is not necessarily required to be the same as the program order for load and store operations.
Reference:
https://developer.arm.com/docs/100941/latest/the-memory-model
load 和 store 的順序不一定要和 program order 一樣
How Linux kernel supports concurrency?
這份文件 User-space RCU: Memory-barrier menagerie 是由 Paul E. McKenney, Mathieu Desnoyers, Lai Jiangshan, and Josh Triplett 所發表,我們先從這份文件澄清對 memory barrier 的一些誤會。
Reference:
https://lwn.net/Articles/573436/
Reference:
https://lwn.net/Articles/573436/
membarrier 是一個 system call,從Linux 4.3 才開始加入
Barrier
The ARM architecture includes barrier instructions to force access ordering and access completion at a specific point.
Reference:
https://developer.arm.com/docs/100941/latest/barriers
Reference
END
tags:
COSCUP2019
系統軟體社群議程
IB201