18c2ecf20Sopenharmony_ciNOTE:
28c2ecf20Sopenharmony_ciThis is a version of Documentation/memory-barriers.txt translated into Korean.
38c2ecf20Sopenharmony_ciThis document is maintained by SeongJae Park <sj38.park@gmail.com>.
48c2ecf20Sopenharmony_ciIf you find any difference between this document and the original file or
58c2ecf20Sopenharmony_cia problem with the translation, please contact the maintainer of this file.
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ciPlease also note that the purpose of this file is to be easier to
88c2ecf20Sopenharmony_ciread for non English (read: Korean) speakers and is not intended as
98c2ecf20Sopenharmony_cia fork.  So if you have any comments or updates for this file please
108c2ecf20Sopenharmony_ciupdate the original English file first.  The English version is
118c2ecf20Sopenharmony_cidefinitive, and readers should look there if they have any doubt.
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci===================================
148c2ecf20Sopenharmony_ci이 문서는
158c2ecf20Sopenharmony_ciDocumentation/memory-barriers.txt
168c2ecf20Sopenharmony_ci의 한글 번역입니다.
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci역자: 박성재 <sj38.park@gmail.com>
198c2ecf20Sopenharmony_ci===================================
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci			 =========================
238c2ecf20Sopenharmony_ci			 리눅스 커널 메모리 배리어
248c2ecf20Sopenharmony_ci			 =========================
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci저자: David Howells <dhowells@redhat.com>
278c2ecf20Sopenharmony_ci      Paul E. McKenney <paulmck@linux.ibm.com>
288c2ecf20Sopenharmony_ci      Will Deacon <will.deacon@arm.com>
298c2ecf20Sopenharmony_ci      Peter Zijlstra <peterz@infradead.org>
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci========
328c2ecf20Sopenharmony_ci면책조항
338c2ecf20Sopenharmony_ci========
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci이 문서는 명세서가 아닙니다; 이 문서는 완벽하지 않은데, 간결성을 위해 의도된
368c2ecf20Sopenharmony_ci부분도 있고, 의도하진 않았지만 사람에 의해 쓰였다보니 불완전한 부분도 있습니다.
378c2ecf20Sopenharmony_ci이 문서는 리눅스에서 제공하는 다양한 메모리 배리어들을 사용하기 위한
388c2ecf20Sopenharmony_ci안내서입니다만, 뭔가 이상하다 싶으면 (그런게 많을 겁니다) 질문을 부탁드립니다.
398c2ecf20Sopenharmony_ci일부 이상한 점들은 공식적인 메모리 일관성 모델과 tools/memory-model/ 에 있는
408c2ecf20Sopenharmony_ci관련 문서를 참고해서 해결될 수 있을 겁니다.  그러나, 이 메모리 모델조차도 그
418c2ecf20Sopenharmony_ci관리자들의 의견의 집합으로 봐야지, 절대 옳은 예언자로 신봉해선 안될 겁니다.
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci다시 말하지만, 이 문서는 리눅스가 하드웨어에 기대하는 사항에 대한 명세서가
448c2ecf20Sopenharmony_ci아닙니다.
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci이 문서의 목적은 두가지입니다:
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci (1) 어떤 특정 배리어에 대해 기대할 수 있는 최소한의 기능을 명세하기 위해서,
498c2ecf20Sopenharmony_ci     그리고
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci (2) 사용 가능한 배리어들에 대해 어떻게 사용해야 하는지에 대한 안내를 제공하기
528c2ecf20Sopenharmony_ci     위해서.
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci어떤 아키텍쳐는 특정한 배리어들에 대해서는 여기서 이야기하는 최소한의
558c2ecf20Sopenharmony_ci요구사항들보다 많은 기능을 제공할 수도 있습니다만, 여기서 이야기하는
568c2ecf20Sopenharmony_ci요구사항들을 충족하지 않는 아키텍쳐가 있다면 그 아키텍쳐가 잘못된 것이란 점을
578c2ecf20Sopenharmony_ci알아두시기 바랍니다.
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci또한, 특정 아키텍쳐에서 일부 배리어는 해당 아키텍쳐의 특수한 동작 방식으로 인해
608c2ecf20Sopenharmony_ci해당 배리어의 명시적 사용이 불필요해서 no-op 이 될수도 있음을 알아두시기
618c2ecf20Sopenharmony_ci바랍니다.
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci역자: 본 번역 역시 완벽하지 않은데, 이 역시 부분적으로는 의도된 것이기도
648c2ecf20Sopenharmony_ci합니다.  여타 기술 문서들이 그렇듯 완벽한 이해를 위해서는 번역문과 원문을 함께
658c2ecf20Sopenharmony_ci읽으시되 번역문을 하나의 가이드로 활용하시길 추천드리며, 발견되는 오역 등에
668c2ecf20Sopenharmony_ci대해서는 언제든 의견을 부탁드립니다.  과한 번역으로 인한 오해를 최소화하기 위해
678c2ecf20Sopenharmony_ci애매한 부분이 있을 경우에는 어색함이 있더라도 원래의 용어를 차용합니다.
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci=====
718c2ecf20Sopenharmony_ci목차:
728c2ecf20Sopenharmony_ci=====
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci (*) 추상 메모리 액세스 모델.
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci     - 디바이스 오퍼레이션.
778c2ecf20Sopenharmony_ci     - 보장사항.
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci (*) 메모리 배리어란 무엇인가?
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci     - 메모리 배리어의 종류.
828c2ecf20Sopenharmony_ci     - 메모리 배리어에 대해 가정해선 안될 것.
838c2ecf20Sopenharmony_ci     - 데이터 의존성 배리어 (역사적).
848c2ecf20Sopenharmony_ci     - 컨트롤 의존성.
858c2ecf20Sopenharmony_ci     - SMP 배리어 짝맞추기.
868c2ecf20Sopenharmony_ci     - 메모리 배리어 시퀀스의 예.
878c2ecf20Sopenharmony_ci     - 읽기 메모리 배리어 vs 로드 예측.
888c2ecf20Sopenharmony_ci     - Multicopy 원자성.
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci (*) 명시적 커널 배리어.
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci     - 컴파일러 배리어.
938c2ecf20Sopenharmony_ci     - CPU 메모리 배리어.
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci (*) 암묵적 커널 메모리 배리어.
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci     - 락 Acquisition 함수.
988c2ecf20Sopenharmony_ci     - 인터럽트 비활성화 함수.
998c2ecf20Sopenharmony_ci     - 슬립과 웨이크업 함수.
1008c2ecf20Sopenharmony_ci     - 그외의 함수들.
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci (*) CPU 간 ACQUIRING 배리어의 효과.
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci     - Acquire vs 메모리 액세스.
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci (*) 메모리 배리어가 필요한 곳
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci     - 프로세서간 상호 작용.
1098c2ecf20Sopenharmony_ci     - 어토믹 오퍼레이션.
1108c2ecf20Sopenharmony_ci     - 디바이스 액세스.
1118c2ecf20Sopenharmony_ci     - 인터럽트.
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci (*) 커널 I/O 배리어의 효과.
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci (*) 가정되는 가장 완화된 실행 순서 모델.
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci (*) CPU 캐시의 영향.
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci     - 캐시 일관성.
1208c2ecf20Sopenharmony_ci     - 캐시 일관성 vs DMA.
1218c2ecf20Sopenharmony_ci     - 캐시 일관성 vs MMIO.
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci (*) CPU 들이 저지르는 일들.
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci     - 그리고, Alpha 가 있다.
1268c2ecf20Sopenharmony_ci     - 가상 머신 게스트.
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci (*) 사용 예.
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_ci     - 순환식 버퍼.
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci (*) 참고 문헌.
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci=======================
1368c2ecf20Sopenharmony_ci추상 메모리 액세스 모델
1378c2ecf20Sopenharmony_ci=======================
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci다음과 같이 추상화된 시스템 모델을 생각해 봅시다:
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_ci		            :                :
1428c2ecf20Sopenharmony_ci		            :                :
1438c2ecf20Sopenharmony_ci		            :                :
1448c2ecf20Sopenharmony_ci		+-------+   :   +--------+   :   +-------+
1458c2ecf20Sopenharmony_ci		|       |   :   |        |   :   |       |
1468c2ecf20Sopenharmony_ci		|       |   :   |        |   :   |       |
1478c2ecf20Sopenharmony_ci		| CPU 1 |<----->| Memory |<----->| CPU 2 |
1488c2ecf20Sopenharmony_ci		|       |   :   |        |   :   |       |
1498c2ecf20Sopenharmony_ci		|       |   :   |        |   :   |       |
1508c2ecf20Sopenharmony_ci		+-------+   :   +--------+   :   +-------+
1518c2ecf20Sopenharmony_ci		    ^       :       ^        :       ^
1528c2ecf20Sopenharmony_ci		    |       :       |        :       |
1538c2ecf20Sopenharmony_ci		    |       :       |        :       |
1548c2ecf20Sopenharmony_ci		    |       :       v        :       |
1558c2ecf20Sopenharmony_ci		    |       :   +--------+   :       |
1568c2ecf20Sopenharmony_ci		    |       :   |        |   :       |
1578c2ecf20Sopenharmony_ci		    |       :   |        |   :       |
1588c2ecf20Sopenharmony_ci		    +---------->| Device |<----------+
1598c2ecf20Sopenharmony_ci		            :   |        |   :
1608c2ecf20Sopenharmony_ci		            :   |        |   :
1618c2ecf20Sopenharmony_ci		            :   +--------+   :
1628c2ecf20Sopenharmony_ci		            :                :
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ci프로그램은 여러 메모리 액세스 오퍼레이션을 발생시키고, 각각의 CPU 는 그런
1658c2ecf20Sopenharmony_ci프로그램들을 실행합니다.  추상화된 CPU 모델에서 메모리 오퍼레이션들의 순서는
1668c2ecf20Sopenharmony_ci매우 완화되어 있고, CPU 는 프로그램이 인과관계를 어기지 않는 상태로 관리된다고
1678c2ecf20Sopenharmony_ci보일 수만 있다면 메모리 오퍼레이션을 자신이 원하는 어떤 순서대로든 재배치해
1688c2ecf20Sopenharmony_ci동작시킬 수 있습니다.  비슷하게, 컴파일러 또한 프로그램의 정상적 동작을 해치지
1698c2ecf20Sopenharmony_ci않는 한도 내에서는 어떤 순서로든 자신이 원하는 대로 인스트럭션을 재배치 할 수
1708c2ecf20Sopenharmony_ci있습니다.
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_ci따라서 위의 다이어그램에서 한 CPU가 동작시키는 메모리 오퍼레이션이 만들어내는
1738c2ecf20Sopenharmony_ci변화는 해당 오퍼레이션이 CPU 와 시스템의 다른 부분들 사이의 인터페이스(점선)를
1748c2ecf20Sopenharmony_ci지나가면서 시스템의 나머지 부분들에 인지됩니다.
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_ci예를 들어, 다음의 일련의 이벤트들을 생각해 봅시다:
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_ci	CPU 1		CPU 2
1808c2ecf20Sopenharmony_ci	===============	===============
1818c2ecf20Sopenharmony_ci	{ A == 1; B == 2 }
1828c2ecf20Sopenharmony_ci	A = 3;		x = B;
1838c2ecf20Sopenharmony_ci	B = 4;		y = A;
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_ci다이어그램의 가운데에 위치한 메모리 시스템에 보여지게 되는 액세스들은 다음의 총
1868c2ecf20Sopenharmony_ci24개의 조합으로 재구성될 수 있습니다:
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ci	STORE A=3,	STORE B=4,	y=LOAD A->3,	x=LOAD B->4
1898c2ecf20Sopenharmony_ci	STORE A=3,	STORE B=4,	x=LOAD B->4,	y=LOAD A->3
1908c2ecf20Sopenharmony_ci	STORE A=3,	y=LOAD A->3,	STORE B=4,	x=LOAD B->4
1918c2ecf20Sopenharmony_ci	STORE A=3,	y=LOAD A->3,	x=LOAD B->2,	STORE B=4
1928c2ecf20Sopenharmony_ci	STORE A=3,	x=LOAD B->2,	STORE B=4,	y=LOAD A->3
1938c2ecf20Sopenharmony_ci	STORE A=3,	x=LOAD B->2,	y=LOAD A->3,	STORE B=4
1948c2ecf20Sopenharmony_ci	STORE B=4,	STORE A=3,	y=LOAD A->3,	x=LOAD B->4
1958c2ecf20Sopenharmony_ci	STORE B=4, ...
1968c2ecf20Sopenharmony_ci	...
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_ci따라서 다음의 네가지 조합의 값들이 나올 수 있습니다:
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_ci	x == 2, y == 1
2018c2ecf20Sopenharmony_ci	x == 2, y == 3
2028c2ecf20Sopenharmony_ci	x == 4, y == 1
2038c2ecf20Sopenharmony_ci	x == 4, y == 3
2048c2ecf20Sopenharmony_ci
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_ci한발 더 나아가서, 한 CPU 가 메모리 시스템에 반영한 스토어 오퍼레이션들의 결과는
2078c2ecf20Sopenharmony_ci다른 CPU 에서의 로드 오퍼레이션을 통해 인지되는데, 이 때 스토어가 반영된 순서와
2088c2ecf20Sopenharmony_ci다른 순서로 인지될 수도 있습니다.
2098c2ecf20Sopenharmony_ci
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_ci예로, 아래의 일련의 이벤트들을 생각해 봅시다:
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_ci	CPU 1		CPU 2
2148c2ecf20Sopenharmony_ci	===============	===============
2158c2ecf20Sopenharmony_ci	{ A == 1, B == 2, C == 3, P == &A, Q == &C }
2168c2ecf20Sopenharmony_ci	B = 4;		Q = P;
2178c2ecf20Sopenharmony_ci	P = &B		D = *Q;
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_ciD 로 읽혀지는 값은 CPU 2 에서 P 로부터 읽혀진 주소값에 의존적이기 때문에 여기엔
2208c2ecf20Sopenharmony_ci분명한 데이터 의존성이 있습니다.  하지만 이 이벤트들의 실행 결과로는 아래의
2218c2ecf20Sopenharmony_ci결과들이 모두 나타날 수 있습니다:
2228c2ecf20Sopenharmony_ci
2238c2ecf20Sopenharmony_ci	(Q == &A) and (D == 1)
2248c2ecf20Sopenharmony_ci	(Q == &B) and (D == 2)
2258c2ecf20Sopenharmony_ci	(Q == &B) and (D == 4)
2268c2ecf20Sopenharmony_ci
2278c2ecf20Sopenharmony_ciCPU 2 는 *Q 의 로드를 요청하기 전에 P 를 Q 에 넣기 때문에 D 에 C 를 집어넣는
2288c2ecf20Sopenharmony_ci일은 없음을 알아두세요.
2298c2ecf20Sopenharmony_ci
2308c2ecf20Sopenharmony_ci
2318c2ecf20Sopenharmony_ci디바이스 오퍼레이션
2328c2ecf20Sopenharmony_ci-------------------
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_ci일부 디바이스는 자신의 컨트롤 인터페이스를 메모리의 특정 영역으로 매핑해서
2358c2ecf20Sopenharmony_ci제공하는데(Memory mapped I/O), 해당 컨트롤 레지스터에 접근하는 순서는 매우
2368c2ecf20Sopenharmony_ci중요합니다.  예를 들어, 어드레스 포트 레지스터 (A) 와 데이터 포트 레지스터 (D)
2378c2ecf20Sopenharmony_ci를 통해 접근되는 내부 레지스터 집합을 갖는 이더넷 카드를 생각해 봅시다.  내부의
2388c2ecf20Sopenharmony_ci5번 레지스터를 읽기 위해 다음의 코드가 사용될 수 있습니다:
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_ci	*A = 5;
2418c2ecf20Sopenharmony_ci	x = *D;
2428c2ecf20Sopenharmony_ci
2438c2ecf20Sopenharmony_ci하지만, 이건 다음의 두 조합 중 하나로 만들어질 수 있습니다:
2448c2ecf20Sopenharmony_ci
2458c2ecf20Sopenharmony_ci	STORE *A = 5, x = LOAD *D
2468c2ecf20Sopenharmony_ci	x = LOAD *D, STORE *A = 5
2478c2ecf20Sopenharmony_ci
2488c2ecf20Sopenharmony_ci두번째 조합은 데이터를 읽어온 _후에_ 주소를 설정하므로, 오동작을 일으킬 겁니다.
2498c2ecf20Sopenharmony_ci
2508c2ecf20Sopenharmony_ci
2518c2ecf20Sopenharmony_ci보장사항
2528c2ecf20Sopenharmony_ci--------
2538c2ecf20Sopenharmony_ci
2548c2ecf20Sopenharmony_ciCPU 에게 기대할 수 있는 최소한의 보장사항 몇가지가 있습니다:
2558c2ecf20Sopenharmony_ci
2568c2ecf20Sopenharmony_ci (*) 어떤 CPU 든, 의존성이 존재하는 메모리 액세스들은 해당 CPU 자신에게
2578c2ecf20Sopenharmony_ci     있어서는 순서대로 메모리 시스템에 수행 요청됩니다. 즉, 다음에 대해서:
2588c2ecf20Sopenharmony_ci
2598c2ecf20Sopenharmony_ci	Q = READ_ONCE(P); D = READ_ONCE(*Q);
2608c2ecf20Sopenharmony_ci
2618c2ecf20Sopenharmony_ci     CPU 는 다음과 같은 메모리 오퍼레이션 시퀀스를 수행 요청합니다:
2628c2ecf20Sopenharmony_ci
2638c2ecf20Sopenharmony_ci	Q = LOAD P, D = LOAD *Q
2648c2ecf20Sopenharmony_ci
2658c2ecf20Sopenharmony_ci     그리고 그 시퀀스 내에서의 순서는 항상 지켜집니다.  하지만, DEC Alpha 에서
2668c2ecf20Sopenharmony_ci     READ_ONCE() 는 메모리 배리어 명령도 내게 되어 있어서, DEC Alpha CPU 는
2678c2ecf20Sopenharmony_ci     다음과 같은 메모리 오퍼레이션들을 내놓게 됩니다:
2688c2ecf20Sopenharmony_ci
2698c2ecf20Sopenharmony_ci	Q = LOAD P, MEMORY_BARRIER, D = LOAD *Q, MEMORY_BARRIER
2708c2ecf20Sopenharmony_ci
2718c2ecf20Sopenharmony_ci     DEC Alpha 에서 수행되든 아니든, READ_ONCE() 는 컴파일러로부터의 악영향
2728c2ecf20Sopenharmony_ci     또한 제거합니다.
2738c2ecf20Sopenharmony_ci
2748c2ecf20Sopenharmony_ci (*) 특정 CPU 내에서 겹치는 영역의 메모리에 행해지는 로드와 스토어 들은 해당
2758c2ecf20Sopenharmony_ci     CPU 안에서는 순서가 바뀌지 않은 것으로 보여집니다.  즉, 다음에 대해서:
2768c2ecf20Sopenharmony_ci
2778c2ecf20Sopenharmony_ci	a = READ_ONCE(*X); WRITE_ONCE(*X, b);
2788c2ecf20Sopenharmony_ci
2798c2ecf20Sopenharmony_ci     CPU 는 다음의 메모리 오퍼레이션 시퀀스만을 메모리에 요청할 겁니다:
2808c2ecf20Sopenharmony_ci
2818c2ecf20Sopenharmony_ci	a = LOAD *X, STORE *X = b
2828c2ecf20Sopenharmony_ci
2838c2ecf20Sopenharmony_ci     그리고 다음에 대해서는:
2848c2ecf20Sopenharmony_ci
2858c2ecf20Sopenharmony_ci	WRITE_ONCE(*X, c); d = READ_ONCE(*X);
2868c2ecf20Sopenharmony_ci
2878c2ecf20Sopenharmony_ci     CPU 는 다음의 수행 요청만을 만들어 냅니다:
2888c2ecf20Sopenharmony_ci
2898c2ecf20Sopenharmony_ci	STORE *X = c, d = LOAD *X
2908c2ecf20Sopenharmony_ci
2918c2ecf20Sopenharmony_ci     (로드 오퍼레이션과 스토어 오퍼레이션이 겹치는 메모리 영역에 대해
2928c2ecf20Sopenharmony_ci     수행된다면 해당 오퍼레이션들은 겹친다고 표현됩니다).
2938c2ecf20Sopenharmony_ci
2948c2ecf20Sopenharmony_ci그리고 _반드시_ 또는 _절대로_ 가정하거나 가정하지 말아야 하는 것들이 있습니다:
2958c2ecf20Sopenharmony_ci
2968c2ecf20Sopenharmony_ci (*) 컴파일러가 READ_ONCE() 나 WRITE_ONCE() 로 보호되지 않은 메모리 액세스를
2978c2ecf20Sopenharmony_ci     당신이 원하는 대로 할 것이라는 가정은 _절대로_ 해선 안됩니다.  그것들이
2988c2ecf20Sopenharmony_ci     없다면, 컴파일러는 컴파일러 배리어 섹션에서 다루게 될, 모든 "창의적인"
2998c2ecf20Sopenharmony_ci     변경들을 만들어낼 권한을 갖게 됩니다.
3008c2ecf20Sopenharmony_ci
3018c2ecf20Sopenharmony_ci (*) 개별적인 로드와 스토어들이 주어진 순서대로 요청될 것이라는 가정은 _절대로_
3028c2ecf20Sopenharmony_ci     하지 말아야 합니다.  이 말은 곧:
3038c2ecf20Sopenharmony_ci
3048c2ecf20Sopenharmony_ci	X = *A; Y = *B; *D = Z;
3058c2ecf20Sopenharmony_ci
3068c2ecf20Sopenharmony_ci     는 다음의 것들 중 어느 것으로든 만들어질 수 있다는 의미입니다:
3078c2ecf20Sopenharmony_ci
3088c2ecf20Sopenharmony_ci	X = LOAD *A,  Y = LOAD *B,  STORE *D = Z
3098c2ecf20Sopenharmony_ci	X = LOAD *A,  STORE *D = Z, Y = LOAD *B
3108c2ecf20Sopenharmony_ci	Y = LOAD *B,  X = LOAD *A,  STORE *D = Z
3118c2ecf20Sopenharmony_ci	Y = LOAD *B,  STORE *D = Z, X = LOAD *A
3128c2ecf20Sopenharmony_ci	STORE *D = Z, X = LOAD *A,  Y = LOAD *B
3138c2ecf20Sopenharmony_ci	STORE *D = Z, Y = LOAD *B,  X = LOAD *A
3148c2ecf20Sopenharmony_ci
3158c2ecf20Sopenharmony_ci (*) 겹치는 메모리 액세스들은 합쳐지거나 버려질 수 있음을 _반드시_ 가정해야
3168c2ecf20Sopenharmony_ci     합니다.  다음의 코드는:
3178c2ecf20Sopenharmony_ci
3188c2ecf20Sopenharmony_ci	X = *A; Y = *(A + 4);
3198c2ecf20Sopenharmony_ci
3208c2ecf20Sopenharmony_ci     다음의 것들 중 뭐든 될 수 있습니다:
3218c2ecf20Sopenharmony_ci
3228c2ecf20Sopenharmony_ci	X = LOAD *A; Y = LOAD *(A + 4);
3238c2ecf20Sopenharmony_ci	Y = LOAD *(A + 4); X = LOAD *A;
3248c2ecf20Sopenharmony_ci	{X, Y} = LOAD {*A, *(A + 4) };
3258c2ecf20Sopenharmony_ci
3268c2ecf20Sopenharmony_ci     그리고:
3278c2ecf20Sopenharmony_ci
3288c2ecf20Sopenharmony_ci	*A = X; *(A + 4) = Y;
3298c2ecf20Sopenharmony_ci
3308c2ecf20Sopenharmony_ci     는 다음 중 뭐든 될 수 있습니다:
3318c2ecf20Sopenharmony_ci
3328c2ecf20Sopenharmony_ci	STORE *A = X; STORE *(A + 4) = Y;
3338c2ecf20Sopenharmony_ci	STORE *(A + 4) = Y; STORE *A = X;
3348c2ecf20Sopenharmony_ci	STORE {*A, *(A + 4) } = {X, Y};
3358c2ecf20Sopenharmony_ci
3368c2ecf20Sopenharmony_ci그리고 보장사항에 반대되는 것들(anti-guarantees)이 있습니다:
3378c2ecf20Sopenharmony_ci
3388c2ecf20Sopenharmony_ci (*) 이 보장사항들은 bitfield 에는 적용되지 않는데, 컴파일러들은 bitfield 를
3398c2ecf20Sopenharmony_ci     수정하는 코드를 생성할 때 원자성 없는(non-atomic) 읽고-수정하고-쓰는
3408c2ecf20Sopenharmony_ci     인스트럭션들의 조합을 만드는 경우가 많기 때문입니다.  병렬 알고리즘의
3418c2ecf20Sopenharmony_ci     동기화에 bitfield 를 사용하려 하지 마십시오.
3428c2ecf20Sopenharmony_ci
3438c2ecf20Sopenharmony_ci (*) bitfield 들이 여러 락으로 보호되는 경우라 하더라도, 하나의 bitfield 의
3448c2ecf20Sopenharmony_ci     모든 필드들은 하나의 락으로 보호되어야 합니다.  만약 한 bitfield 의 두
3458c2ecf20Sopenharmony_ci     필드가 서로 다른 락으로 보호된다면, 컴파일러의 원자성 없는
3468c2ecf20Sopenharmony_ci     읽고-수정하고-쓰는 인스트럭션 조합은 한 필드에의 업데이트가 근처의
3478c2ecf20Sopenharmony_ci     필드에도 영향을 끼치게 할 수 있습니다.
3488c2ecf20Sopenharmony_ci
3498c2ecf20Sopenharmony_ci (*) 이 보장사항들은 적절하게 정렬되고 크기가 잡힌 스칼라 변수들에 대해서만
3508c2ecf20Sopenharmony_ci     적용됩니다.  "적절하게 크기가 잡힌" 이라함은 현재로써는 "char", "short",
3518c2ecf20Sopenharmony_ci     "int" 그리고 "long" 과 같은 크기의 변수들을 의미합니다.  "적절하게 정렬된"
3528c2ecf20Sopenharmony_ci     은 자연스런 정렬을 의미하는데, 따라서 "char" 에 대해서는 아무 제약이 없고,
3538c2ecf20Sopenharmony_ci     "short" 에 대해서는 2바이트 정렬을, "int" 에는 4바이트 정렬을, 그리고
3548c2ecf20Sopenharmony_ci     "long" 에 대해서는 32-bit 시스템인지 64-bit 시스템인지에 따라 4바이트 또는
3558c2ecf20Sopenharmony_ci     8바이트 정렬을 의미합니다.  이 보장사항들은 C11 표준에서 소개되었으므로,
3568c2ecf20Sopenharmony_ci     C11 전의 오래된 컴파일러(예를 들어, gcc 4.6) 를 사용할 때엔 주의하시기
3578c2ecf20Sopenharmony_ci     바랍니다.  표준에 이 보장사항들은 "memory location" 을 정의하는 3.14
3588c2ecf20Sopenharmony_ci     섹션에 다음과 같이 설명되어 있습니다:
3598c2ecf20Sopenharmony_ci     (역자: 인용문이므로 번역하지 않습니다)
3608c2ecf20Sopenharmony_ci
3618c2ecf20Sopenharmony_ci	memory location
3628c2ecf20Sopenharmony_ci		either an object of scalar type, or a maximal sequence
3638c2ecf20Sopenharmony_ci		of adjacent bit-fields all having nonzero width
3648c2ecf20Sopenharmony_ci
3658c2ecf20Sopenharmony_ci		NOTE 1: Two threads of execution can update and access
3668c2ecf20Sopenharmony_ci		separate memory locations without interfering with
3678c2ecf20Sopenharmony_ci		each other.
3688c2ecf20Sopenharmony_ci
3698c2ecf20Sopenharmony_ci		NOTE 2: A bit-field and an adjacent non-bit-field member
3708c2ecf20Sopenharmony_ci		are in separate memory locations. The same applies
3718c2ecf20Sopenharmony_ci		to two bit-fields, if one is declared inside a nested
3728c2ecf20Sopenharmony_ci		structure declaration and the other is not, or if the two
3738c2ecf20Sopenharmony_ci		are separated by a zero-length bit-field declaration,
3748c2ecf20Sopenharmony_ci		or if they are separated by a non-bit-field member
3758c2ecf20Sopenharmony_ci		declaration. It is not safe to concurrently update two
3768c2ecf20Sopenharmony_ci		bit-fields in the same structure if all members declared
3778c2ecf20Sopenharmony_ci		between them are also bit-fields, no matter what the
3788c2ecf20Sopenharmony_ci		sizes of those intervening bit-fields happen to be.
3798c2ecf20Sopenharmony_ci
3808c2ecf20Sopenharmony_ci
3818c2ecf20Sopenharmony_ci=========================
3828c2ecf20Sopenharmony_ci메모리 배리어란 무엇인가?
3838c2ecf20Sopenharmony_ci=========================
3848c2ecf20Sopenharmony_ci
3858c2ecf20Sopenharmony_ci앞에서 봤듯이, 상호간 의존성이 없는 메모리 오퍼레이션들은 실제로는 무작위적
3868c2ecf20Sopenharmony_ci순서로 수행될 수 있으며, 이는 CPU 와 CPU 간의 상호작용이나 I/O 에 문제가 될 수
3878c2ecf20Sopenharmony_ci있습니다.  따라서 컴파일러와 CPU 가 순서를 바꾸는데 제약을 걸 수 있도록 개입할
3888c2ecf20Sopenharmony_ci수 있는 어떤 방법이 필요합니다.
3898c2ecf20Sopenharmony_ci
3908c2ecf20Sopenharmony_ci메모리 배리어는 그런 개입 수단입니다.  메모리 배리어는 배리어를 사이에 둔 앞과
3918c2ecf20Sopenharmony_ci뒤 양측의 메모리 오퍼레이션들 간에 부분적 순서가 존재하도록 하는 효과를 줍니다.
3928c2ecf20Sopenharmony_ci
3938c2ecf20Sopenharmony_ci시스템의 CPU 들과 여러 디바이스들은 성능을 올리기 위해 명령어 재배치, 실행
3948c2ecf20Sopenharmony_ci유예, 메모리 오퍼레이션들의 조합, 예측적 로드(speculative load), 브랜치
3958c2ecf20Sopenharmony_ci예측(speculative branch prediction), 다양한 종류의 캐싱(caching) 등의 다양한
3968c2ecf20Sopenharmony_ci트릭을 사용할 수 있기 때문에 이런 강제력은 중요합니다.  메모리 배리어들은 이런
3978c2ecf20Sopenharmony_ci트릭들을 무효로 하거나 억제하는 목적으로 사용되어져서 코드가 여러 CPU 와
3988c2ecf20Sopenharmony_ci디바이스들 간의 상호작용을 정상적으로 제어할 수 있게 해줍니다.
3998c2ecf20Sopenharmony_ci
4008c2ecf20Sopenharmony_ci
4018c2ecf20Sopenharmony_ci메모리 배리어의 종류
4028c2ecf20Sopenharmony_ci--------------------
4038c2ecf20Sopenharmony_ci
4048c2ecf20Sopenharmony_ci메모리 배리어는 네개의 기본 타입으로 분류됩니다:
4058c2ecf20Sopenharmony_ci
4068c2ecf20Sopenharmony_ci (1) 쓰기 (또는 스토어) 메모리 배리어.
4078c2ecf20Sopenharmony_ci
4088c2ecf20Sopenharmony_ci     쓰기 메모리 배리어는 시스템의 다른 컴포넌트들에 해당 배리어보다 앞서
4098c2ecf20Sopenharmony_ci     명시된 모든 STORE 오퍼레이션들이 해당 배리어 뒤에 명시된 모든 STORE
4108c2ecf20Sopenharmony_ci     오퍼레이션들보다 먼저 수행된 것으로 보일 것을 보장합니다.
4118c2ecf20Sopenharmony_ci
4128c2ecf20Sopenharmony_ci     쓰기 배리어는 스토어 오퍼레이션들에 대한 부분적 순서 세우기입니다; 로드
4138c2ecf20Sopenharmony_ci     오퍼레이션들에 대해서는 어떤 영향도 끼치지 않습니다.
4148c2ecf20Sopenharmony_ci
4158c2ecf20Sopenharmony_ci     CPU 는 시간의 흐름에 따라 메모리 시스템에 일련의 스토어 오퍼레이션들을
4168c2ecf20Sopenharmony_ci     하나씩 요청해 집어넣습니다.  쓰기 배리어 앞의 모든 스토어 오퍼레이션들은
4178c2ecf20Sopenharmony_ci     쓰기 배리어 뒤의 모든 스토어 오퍼레이션들보다 _앞서_ 수행될 겁니다.
4188c2ecf20Sopenharmony_ci
4198c2ecf20Sopenharmony_ci     [!] 쓰기 배리어들은 읽기 또는 데이터 의존성 배리어와 함께 짝을 맞춰
4208c2ecf20Sopenharmony_ci     사용되어야만 함을 알아두세요; "SMP 배리어 짝맞추기" 서브섹션을 참고하세요.
4218c2ecf20Sopenharmony_ci
4228c2ecf20Sopenharmony_ci
4238c2ecf20Sopenharmony_ci (2) 데이터 의존성 배리어.
4248c2ecf20Sopenharmony_ci
4258c2ecf20Sopenharmony_ci     데이터 의존성 배리어는 읽기 배리어의 보다 완화된 형태입니다.  두개의 로드
4268c2ecf20Sopenharmony_ci     오퍼레이션이 있고 두번째 것이 첫번째 것의 결과에 의존하고 있을 때(예:
4278c2ecf20Sopenharmony_ci     두번째 로드가 참조할 주소를 첫번째 로드가 읽는 경우), 두번째 로드가 읽어올
4288c2ecf20Sopenharmony_ci     데이터는 첫번째 로드에 의해 그 주소가 얻어진 뒤에 업데이트 됨을 보장하기
4298c2ecf20Sopenharmony_ci     위해서 데이터 의존성 배리어가 필요할 수 있습니다.
4308c2ecf20Sopenharmony_ci
4318c2ecf20Sopenharmony_ci     데이터 의존성 배리어는 상호 의존적인 로드 오퍼레이션들 사이의 부분적 순서
4328c2ecf20Sopenharmony_ci     세우기입니다; 스토어 오퍼레이션들이나 독립적인 로드들, 또는 중복되는
4338c2ecf20Sopenharmony_ci     로드들에 대해서는 어떤 영향도 끼치지 않습니다.
4348c2ecf20Sopenharmony_ci
4358c2ecf20Sopenharmony_ci     (1) 에서 언급했듯이, 시스템의 CPU 들은 메모리 시스템에 일련의 스토어
4368c2ecf20Sopenharmony_ci     오퍼레이션들을 던져 넣고 있으며, 거기에 관심이 있는 다른 CPU 는 그
4378c2ecf20Sopenharmony_ci     오퍼레이션들을 메모리 시스템이 실행한 결과를 인지할 수 있습니다.  이처럼
4388c2ecf20Sopenharmony_ci     다른 CPU 의 스토어 오퍼레이션의 결과에 관심을 두고 있는 CPU 가 수행 요청한
4398c2ecf20Sopenharmony_ci     데이터 의존성 배리어는, 배리어 앞의 어떤 로드 오퍼레이션이 다른 CPU 에서
4408c2ecf20Sopenharmony_ci     던져 넣은 스토어 오퍼레이션과 같은 영역을 향했다면, 그런 스토어
4418c2ecf20Sopenharmony_ci     오퍼레이션들이 만들어내는 결과가 데이터 의존성 배리어 뒤의 로드
4428c2ecf20Sopenharmony_ci     오퍼레이션들에게는 보일 것을 보장합니다.
4438c2ecf20Sopenharmony_ci
4448c2ecf20Sopenharmony_ci     이 순서 세우기 제약에 대한 그림을 보기 위해선 "메모리 배리어 시퀀스의 예"
4458c2ecf20Sopenharmony_ci     서브섹션을 참고하시기 바랍니다.
4468c2ecf20Sopenharmony_ci
4478c2ecf20Sopenharmony_ci     [!] 첫번째 로드는 반드시 _데이터_ 의존성을 가져야지 컨트롤 의존성을 가져야
4488c2ecf20Sopenharmony_ci     하는게 아님을 알아두십시오.  만약 두번째 로드를 위한 주소가 첫번째 로드에
4498c2ecf20Sopenharmony_ci     의존적이지만 그 의존성은 조건적이지 그 주소 자체를 가져오는게 아니라면,
4508c2ecf20Sopenharmony_ci     그것은 _컨트롤_ 의존성이고, 이 경우에는 읽기 배리어나 그보다 강력한
4518c2ecf20Sopenharmony_ci     무언가가 필요합니다.  더 자세한 내용을 위해서는 "컨트롤 의존성" 서브섹션을
4528c2ecf20Sopenharmony_ci     참고하시기 바랍니다.
4538c2ecf20Sopenharmony_ci
4548c2ecf20Sopenharmony_ci     [!] 데이터 의존성 배리어는 보통 쓰기 배리어들과 함께 짝을 맞춰 사용되어야
4558c2ecf20Sopenharmony_ci     합니다; "SMP 배리어 짝맞추기" 서브섹션을 참고하세요.
4568c2ecf20Sopenharmony_ci
4578c2ecf20Sopenharmony_ci
4588c2ecf20Sopenharmony_ci (3) 읽기 (또는 로드) 메모리 배리어.
4598c2ecf20Sopenharmony_ci
4608c2ecf20Sopenharmony_ci     읽기 배리어는 데이터 의존성 배리어 기능의 보장사항에 더해서 배리어보다
4618c2ecf20Sopenharmony_ci     앞서 명시된 모든 LOAD 오퍼레이션들이 배리어 뒤에 명시되는 모든 LOAD
4628c2ecf20Sopenharmony_ci     오퍼레이션들보다 먼저 행해진 것으로 시스템의 다른 컴포넌트들에 보여질 것을
4638c2ecf20Sopenharmony_ci     보장합니다.
4648c2ecf20Sopenharmony_ci
4658c2ecf20Sopenharmony_ci     읽기 배리어는 로드 오퍼레이션에 행해지는 부분적 순서 세우기입니다; 스토어
4668c2ecf20Sopenharmony_ci     오퍼레이션에 대해서는 어떤 영향도 끼치지 않습니다.
4678c2ecf20Sopenharmony_ci
4688c2ecf20Sopenharmony_ci     읽기 메모리 배리어는 데이터 의존성 배리어를 내장하므로 데이터 의존성
4698c2ecf20Sopenharmony_ci     배리어를 대신할 수 있습니다.
4708c2ecf20Sopenharmony_ci
4718c2ecf20Sopenharmony_ci     [!] 읽기 배리어는 일반적으로 쓰기 배리어들과 함께 짝을 맞춰 사용되어야
4728c2ecf20Sopenharmony_ci     합니다; "SMP 배리어 짝맞추기" 서브섹션을 참고하세요.
4738c2ecf20Sopenharmony_ci
4748c2ecf20Sopenharmony_ci
4758c2ecf20Sopenharmony_ci (4) 범용 메모리 배리어.
4768c2ecf20Sopenharmony_ci
4778c2ecf20Sopenharmony_ci     범용(general) 메모리 배리어는 배리어보다 앞서 명시된 모든 LOAD 와 STORE
4788c2ecf20Sopenharmony_ci     오퍼레이션들이 배리어 뒤에 명시된 모든 LOAD 와 STORE 오퍼레이션들보다
4798c2ecf20Sopenharmony_ci     먼저 수행된 것으로 시스템의 나머지 컴포넌트들에 보이게 됨을 보장합니다.
4808c2ecf20Sopenharmony_ci
4818c2ecf20Sopenharmony_ci     범용 메모리 배리어는 로드와 스토어 모두에 대한 부분적 순서 세우기입니다.
4828c2ecf20Sopenharmony_ci
4838c2ecf20Sopenharmony_ci     범용 메모리 배리어는 읽기 메모리 배리어, 쓰기 메모리 배리어 모두를
4848c2ecf20Sopenharmony_ci     내장하므로, 두 배리어를 모두 대신할 수 있습니다.
4858c2ecf20Sopenharmony_ci
4868c2ecf20Sopenharmony_ci
4878c2ecf20Sopenharmony_ci그리고 두개의 명시적이지 않은 타입이 있습니다:
4888c2ecf20Sopenharmony_ci
4898c2ecf20Sopenharmony_ci (5) ACQUIRE 오퍼레이션.
4908c2ecf20Sopenharmony_ci
4918c2ecf20Sopenharmony_ci     이 타입의 오퍼레이션은 단방향의 투과성 배리어처럼 동작합니다.  ACQUIRE
4928c2ecf20Sopenharmony_ci     오퍼레이션 뒤의 모든 메모리 오퍼레이션들이 ACQUIRE 오퍼레이션 후에
4938c2ecf20Sopenharmony_ci     일어난 것으로 시스템의 나머지 컴포넌트들에 보이게 될 것이 보장됩니다.
4948c2ecf20Sopenharmony_ci     LOCK 오퍼레이션과 smp_load_acquire(), smp_cond_load_acquire() 오퍼레이션도
4958c2ecf20Sopenharmony_ci     ACQUIRE 오퍼레이션에 포함됩니다.
4968c2ecf20Sopenharmony_ci
4978c2ecf20Sopenharmony_ci     ACQUIRE 오퍼레이션 앞의 메모리 오퍼레이션들은 ACQUIRE 오퍼레이션 완료 후에
4988c2ecf20Sopenharmony_ci     수행된 것처럼 보일 수 있습니다.
4998c2ecf20Sopenharmony_ci
5008c2ecf20Sopenharmony_ci     ACQUIRE 오퍼레이션은 거의 항상 RELEASE 오퍼레이션과 짝을 지어 사용되어야
5018c2ecf20Sopenharmony_ci     합니다.
5028c2ecf20Sopenharmony_ci
5038c2ecf20Sopenharmony_ci
5048c2ecf20Sopenharmony_ci (6) RELEASE 오퍼레이션.
5058c2ecf20Sopenharmony_ci
5068c2ecf20Sopenharmony_ci     이 타입의 오퍼레이션들도 단방향 투과성 배리어처럼 동작합니다.  RELEASE
5078c2ecf20Sopenharmony_ci     오퍼레이션 앞의 모든 메모리 오퍼레이션들은 RELEASE 오퍼레이션 전에 완료된
5088c2ecf20Sopenharmony_ci     것으로 시스템의 다른 컴포넌트들에 보여질 것이 보장됩니다.  UNLOCK 류의
5098c2ecf20Sopenharmony_ci     오퍼레이션들과 smp_store_release() 오퍼레이션도 RELEASE 오퍼레이션의
5108c2ecf20Sopenharmony_ci     일종입니다.
5118c2ecf20Sopenharmony_ci
5128c2ecf20Sopenharmony_ci     RELEASE 오퍼레이션 뒤의 메모리 오퍼레이션들은 RELEASE 오퍼레이션이
5138c2ecf20Sopenharmony_ci     완료되기 전에 행해진 것처럼 보일 수 있습니다.
5148c2ecf20Sopenharmony_ci
5158c2ecf20Sopenharmony_ci     ACQUIRE 와 RELEASE 오퍼레이션의 사용은 일반적으로 다른 메모리 배리어의
5168c2ecf20Sopenharmony_ci     필요성을 없앱니다.  또한, RELEASE+ACQUIRE 조합은 범용 메모리 배리어처럼
5178c2ecf20Sopenharmony_ci     동작할 것을 보장하지 -않습니다-.  하지만, 어떤 변수에 대한 RELEASE
5188c2ecf20Sopenharmony_ci     오퍼레이션을 앞서는 메모리 액세스들의 수행 결과는 이 RELEASE 오퍼레이션을
5198c2ecf20Sopenharmony_ci     뒤이어 같은 변수에 대해 수행된 ACQUIRE 오퍼레이션을 뒤따르는 메모리
5208c2ecf20Sopenharmony_ci     액세스에는 보여질 것이 보장됩니다.  다르게 말하자면, 주어진 변수의
5218c2ecf20Sopenharmony_ci     크리티컬 섹션에서는, 해당 변수에 대한 앞의 크리티컬 섹션에서의 모든
5228c2ecf20Sopenharmony_ci     액세스들이 완료되었을 것을 보장합니다.
5238c2ecf20Sopenharmony_ci
5248c2ecf20Sopenharmony_ci     즉, ACQUIRE 는 최소한의 "취득" 동작처럼, 그리고 RELEASE 는 최소한의 "공개"
5258c2ecf20Sopenharmony_ci     처럼 동작한다는 의미입니다.
5268c2ecf20Sopenharmony_ci
5278c2ecf20Sopenharmony_ciatomic_t.txt 에 설명된 어토믹 오퍼레이션들 중 일부는 완전히 순서잡힌 것들과
5288c2ecf20Sopenharmony_ci(배리어를 사용하지 않는) 완화된 순서의 것들 외에 ACQUIRE 와 RELEASE 부류의
5298c2ecf20Sopenharmony_ci것들도 존재합니다.  로드와 스토어를 모두 수행하는 조합된 어토믹 오퍼레이션에서,
5308c2ecf20Sopenharmony_ciACQUIRE 는 해당 오퍼레이션의 로드 부분에만 적용되고 RELEASE 는 해당
5318c2ecf20Sopenharmony_ci오퍼레이션의 스토어 부분에만 적용됩니다.
5328c2ecf20Sopenharmony_ci
5338c2ecf20Sopenharmony_ci메모리 배리어들은 두 CPU 간, 또는 CPU 와 디바이스 간에 상호작용의 가능성이 있을
5348c2ecf20Sopenharmony_ci때에만 필요합니다.  만약 어떤 코드에 그런 상호작용이 없을 것이 보장된다면, 해당
5358c2ecf20Sopenharmony_ci코드에서는 메모리 배리어를 사용할 필요가 없습니다.
5368c2ecf20Sopenharmony_ci
5378c2ecf20Sopenharmony_ci
5388c2ecf20Sopenharmony_ci이것들은 _최소한의_ 보장사항들임을 알아두세요.  다른 아키텍쳐에서는 더 강력한
5398c2ecf20Sopenharmony_ci보장사항을 제공할 수도 있습니다만, 그런 보장사항은 아키텍쳐 종속적 코드 이외의
5408c2ecf20Sopenharmony_ci부분에서는 신뢰되지 _않을_ 겁니다.
5418c2ecf20Sopenharmony_ci
5428c2ecf20Sopenharmony_ci
5438c2ecf20Sopenharmony_ci메모리 배리어에 대해 가정해선 안될 것
5448c2ecf20Sopenharmony_ci-------------------------------------
5458c2ecf20Sopenharmony_ci
5468c2ecf20Sopenharmony_ci리눅스 커널 메모리 배리어들이 보장하지 않는 것들이 있습니다:
5478c2ecf20Sopenharmony_ci
5488c2ecf20Sopenharmony_ci (*) 메모리 배리어 앞에서 명시된 어떤 메모리 액세스도 메모리 배리어 명령의 수행
5498c2ecf20Sopenharmony_ci     완료 시점까지 _완료_ 될 것이란 보장은 없습니다; 배리어가 하는 일은 CPU 의
5508c2ecf20Sopenharmony_ci     액세스 큐에 특정 타입의 액세스들은 넘을 수 없는 선을 긋는 것으로 생각될 수
5518c2ecf20Sopenharmony_ci     있습니다.
5528c2ecf20Sopenharmony_ci
5538c2ecf20Sopenharmony_ci (*) 한 CPU 에서 메모리 배리어를 수행하는게 시스템의 다른 CPU 나 하드웨어에
5548c2ecf20Sopenharmony_ci     어떤 직접적인 영향을 끼친다는 보장은 존재하지 않습니다.  배리어 수행이
5558c2ecf20Sopenharmony_ci     만드는 간접적 영향은 두번째 CPU 가 첫번째 CPU 의 액세스들의 결과를
5568c2ecf20Sopenharmony_ci     바라보는 순서가 됩니다만, 다음 항목을 보세요:
5578c2ecf20Sopenharmony_ci
5588c2ecf20Sopenharmony_ci (*) 첫번째 CPU 가 두번째 CPU 의 메모리 액세스들의 결과를 바라볼 때, _설령_
5598c2ecf20Sopenharmony_ci     두번째 CPU 가 메모리 배리어를 사용한다 해도, 첫번째 CPU _또한_ 그에 맞는
5608c2ecf20Sopenharmony_ci     메모리 배리어를 사용하지 않는다면 ("SMP 배리어 짝맞추기" 서브섹션을
5618c2ecf20Sopenharmony_ci     참고하세요) 그 결과가 올바른 순서로 보여진다는 보장은 없습니다.
5628c2ecf20Sopenharmony_ci
5638c2ecf20Sopenharmony_ci (*) CPU 바깥의 하드웨어[*] 가 메모리 액세스들의 순서를 바꾸지 않는다는 보장은
5648c2ecf20Sopenharmony_ci     존재하지 않습니다.  CPU 캐시 일관성 메커니즘은 메모리 배리어의 간접적
5658c2ecf20Sopenharmony_ci     영향을 CPU 사이에 전파하긴 하지만, 순서대로 전파하지는 않을 수 있습니다.
5668c2ecf20Sopenharmony_ci
5678c2ecf20Sopenharmony_ci	[*] 버스 마스터링 DMA 와 일관성에 대해서는 다음을 참고하시기 바랍니다:
5688c2ecf20Sopenharmony_ci
5698c2ecf20Sopenharmony_ci	    Documentation/driver-api/pci/pci.rst
5708c2ecf20Sopenharmony_ci	    Documentation/core-api/dma-api-howto.rst
5718c2ecf20Sopenharmony_ci	    Documentation/core-api/dma-api.rst
5728c2ecf20Sopenharmony_ci
5738c2ecf20Sopenharmony_ci
5748c2ecf20Sopenharmony_ci데이터 의존성 배리어 (역사적)
5758c2ecf20Sopenharmony_ci-----------------------------
5768c2ecf20Sopenharmony_ci
5778c2ecf20Sopenharmony_ci리눅스 커널 v4.15 기준으로, smp_mb() 가 DEC Alpha 용 READ_ONCE() 코드에
5788c2ecf20Sopenharmony_ci추가되었는데, 이는 이 섹션에 주의를 기울여야 하는 사람들은 DEC Alpha 아키텍쳐
5798c2ecf20Sopenharmony_ci전용 코드를 만드는 사람들과 READ_ONCE() 자체를 만드는 사람들 뿐임을 의미합니다.
5808c2ecf20Sopenharmony_ci그런 분들을 위해, 그리고 역사에 관심 있는 분들을 위해, 여기 데이터 의존성
5818c2ecf20Sopenharmony_ci배리어에 대한 이야기를 적습니다.
5828c2ecf20Sopenharmony_ci
5838c2ecf20Sopenharmony_ci데이터 의존성 배리어의 사용에 있어 지켜야 하는 사항들은 약간 미묘하고, 데이터
5848c2ecf20Sopenharmony_ci의존성 배리어가 사용되어야 하는 상황도 항상 명백하지는 않습니다.  설명을 위해
5858c2ecf20Sopenharmony_ci다음의 이벤트 시퀀스를 생각해 봅시다:
5868c2ecf20Sopenharmony_ci
5878c2ecf20Sopenharmony_ci	CPU 1		      CPU 2
5888c2ecf20Sopenharmony_ci	===============	      ===============
5898c2ecf20Sopenharmony_ci	{ A == 1, B == 2, C == 3, P == &A, Q == &C }
5908c2ecf20Sopenharmony_ci	B = 4;
5918c2ecf20Sopenharmony_ci	<쓰기 배리어>
5928c2ecf20Sopenharmony_ci	WRITE_ONCE(P, &B)
5938c2ecf20Sopenharmony_ci			      Q = READ_ONCE(P);
5948c2ecf20Sopenharmony_ci			      D = *Q;
5958c2ecf20Sopenharmony_ci
5968c2ecf20Sopenharmony_ci여기엔 분명한 데이터 의존성이 존재하므로, 이 시퀀스가 끝났을 때 Q 는 &A 또는 &B
5978c2ecf20Sopenharmony_ci일 것이고, 따라서:
5988c2ecf20Sopenharmony_ci
5998c2ecf20Sopenharmony_ci	(Q == &A) 는 (D == 1) 를,
6008c2ecf20Sopenharmony_ci	(Q == &B) 는 (D == 4) 를 의미합니다.
6018c2ecf20Sopenharmony_ci
6028c2ecf20Sopenharmony_ci하지만!  CPU 2 는 B 의 업데이트를 인식하기 전에 P 의 업데이트를 인식할 수 있고,
6038c2ecf20Sopenharmony_ci따라서 다음의 결과가 가능합니다:
6048c2ecf20Sopenharmony_ci
6058c2ecf20Sopenharmony_ci	(Q == &B) and (D == 2) ????
6068c2ecf20Sopenharmony_ci
6078c2ecf20Sopenharmony_ci이런 결과는 일관성이나 인과 관계 유지가 실패한 것처럼 보일 수도 있겠지만,
6088c2ecf20Sopenharmony_ci그렇지 않습니다, 그리고 이 현상은 (DEC Alpha 와 같은) 여러 CPU 에서 실제로
6098c2ecf20Sopenharmony_ci발견될 수 있습니다.
6108c2ecf20Sopenharmony_ci
6118c2ecf20Sopenharmony_ci이 문제 상황을 제대로 해결하기 위해, 데이터 의존성 배리어나 그보다 강화된
6128c2ecf20Sopenharmony_ci무언가가 주소를 읽어올 때와 데이터를 읽어올 때 사이에 추가되어야만 합니다:
6138c2ecf20Sopenharmony_ci
6148c2ecf20Sopenharmony_ci	CPU 1		      CPU 2
6158c2ecf20Sopenharmony_ci	===============	      ===============
6168c2ecf20Sopenharmony_ci	{ A == 1, B == 2, C == 3, P == &A, Q == &C }
6178c2ecf20Sopenharmony_ci	B = 4;
6188c2ecf20Sopenharmony_ci	<쓰기 배리어>
6198c2ecf20Sopenharmony_ci	WRITE_ONCE(P, &B);
6208c2ecf20Sopenharmony_ci			      Q = READ_ONCE(P);
6218c2ecf20Sopenharmony_ci			      <데이터 의존성 배리어>
6228c2ecf20Sopenharmony_ci			      D = *Q;
6238c2ecf20Sopenharmony_ci
6248c2ecf20Sopenharmony_ci이 변경은 앞의 처음 두가지 결과 중 하나만이 발생할 수 있고, 세번째의 결과는
6258c2ecf20Sopenharmony_ci발생할 수 없도록 합니다.
6268c2ecf20Sopenharmony_ci
6278c2ecf20Sopenharmony_ci
6288c2ecf20Sopenharmony_ci[!] 이 상당히 반직관적인 상황은 분리된 캐시를 가지는 기계들에서 가장 잘
6298c2ecf20Sopenharmony_ci발생하는데, 예를 들면 한 캐시 뱅크는 짝수 번호의 캐시 라인들을 처리하고, 다른
6308c2ecf20Sopenharmony_ci뱅크는 홀수 번호의 캐시 라인들을 처리하는 경우임을 알아두시기 바랍니다.  포인터
6318c2ecf20Sopenharmony_ciP 는 짝수 번호 캐시 라인에 저장되어 있고, 변수 B 는 홀수 번호 캐시 라인에
6328c2ecf20Sopenharmony_ci저장되어 있을 수 있습니다.  여기서 값을 읽어오는 CPU 의 캐시의 홀수 번호 처리
6338c2ecf20Sopenharmony_ci뱅크는 열심히 일감을 처리중인 반면 홀수 번호 처리 뱅크는 할 일 없이 한가한
6348c2ecf20Sopenharmony_ci중이라면 포인터 P (&B) 의 새로운 값과 변수 B 의 기존 값 (2) 를 볼 수 있습니다.
6358c2ecf20Sopenharmony_ci
6368c2ecf20Sopenharmony_ci
6378c2ecf20Sopenharmony_ci의존적 쓰기들의 순서를 맞추는데에는 데이터 의존성 배리어가 필요치 않은데, 이는
6388c2ecf20Sopenharmony_ci리눅스 커널이 지원하는 CPU 들은 (1) 쓰기가 정말로 일어날지, (2) 쓰기가 어디에
6398c2ecf20Sopenharmony_ci이루어질지, 그리고 (3) 쓰여질 값을 확실히 알기 전까지는 쓰기를 수행하지 않기
6408c2ecf20Sopenharmony_ci때문입니다.  하지만 "컨트롤 의존성" 섹션과
6418c2ecf20Sopenharmony_ciDocumentation/RCU/rcu_dereference.rst 파일을 주의 깊게 읽어 주시기 바랍니다:
6428c2ecf20Sopenharmony_ci컴파일러는 매우 창의적인 많은 방법으로 종속성을 깰 수 있습니다.
6438c2ecf20Sopenharmony_ci
6448c2ecf20Sopenharmony_ci	CPU 1		      CPU 2
6458c2ecf20Sopenharmony_ci	===============	      ===============
6468c2ecf20Sopenharmony_ci	{ A == 1, B == 2, C = 3, P == &A, Q == &C }
6478c2ecf20Sopenharmony_ci	B = 4;
6488c2ecf20Sopenharmony_ci	<쓰기 배리어>
6498c2ecf20Sopenharmony_ci	WRITE_ONCE(P, &B);
6508c2ecf20Sopenharmony_ci			      Q = READ_ONCE(P);
6518c2ecf20Sopenharmony_ci			      WRITE_ONCE(*Q, 5);
6528c2ecf20Sopenharmony_ci
6538c2ecf20Sopenharmony_ci따라서, Q 로의 읽기와 *Q 로의 쓰기 사이에는 데이터 종속성 배리어가 필요치
6548c2ecf20Sopenharmony_ci않습니다.  달리 말하면, 데이터 종속성 배리어가 없더라도 다음 결과는 생기지
6558c2ecf20Sopenharmony_ci않습니다:
6568c2ecf20Sopenharmony_ci
6578c2ecf20Sopenharmony_ci	(Q == &B) && (B == 4)
6588c2ecf20Sopenharmony_ci
6598c2ecf20Sopenharmony_ci이런 패턴은 드물게 사용되어야 함을 알아 두시기 바랍니다.  무엇보다도, 의존성
6608c2ecf20Sopenharmony_ci순서 규칙의 의도는 쓰기 작업을 -예방- 해서 그로 인해 발생하는 비싼 캐시 미스도
6618c2ecf20Sopenharmony_ci없애려는 것입니다.  이 패턴은 드물게 발생하는 에러 조건 같은것들을 기록하는데
6628c2ecf20Sopenharmony_ci사용될 수 있으며, CPU의 자연적인 순서 보장이 그런 기록들을 사라지지 않게
6638c2ecf20Sopenharmony_ci해줍니다.
6648c2ecf20Sopenharmony_ci
6658c2ecf20Sopenharmony_ci
6668c2ecf20Sopenharmony_ci데이터 의존성에 의해 제공되는 이 순서규칙은 이를 포함하고 있는 CPU 에
6678c2ecf20Sopenharmony_ci지역적임을 알아두시기 바랍니다.  더 많은 정보를 위해선 "Multicopy 원자성"
6688c2ecf20Sopenharmony_ci섹션을 참고하세요.
6698c2ecf20Sopenharmony_ci
6708c2ecf20Sopenharmony_ci
6718c2ecf20Sopenharmony_ci데이터 의존성 배리어는 매우 중요한데, 예를 들어 RCU 시스템에서 그렇습니다.
6728c2ecf20Sopenharmony_ciinclude/linux/rcupdate.h 의 rcu_assign_pointer() 와 rcu_dereference() 를
6738c2ecf20Sopenharmony_ci참고하세요.  여기서 데이터 의존성 배리어는 RCU 로 관리되는 포인터의 타겟을 현재
6748c2ecf20Sopenharmony_ci타겟에서 수정된 새로운 타겟으로 바꾸는 작업에서 새로 수정된 타겟이 초기화가
6758c2ecf20Sopenharmony_ci완료되지 않은 채로 보여지는 일이 일어나지 않게 해줍니다.
6768c2ecf20Sopenharmony_ci
6778c2ecf20Sopenharmony_ci더 많은 예를 위해선 "캐시 일관성" 서브섹션을 참고하세요.
6788c2ecf20Sopenharmony_ci
6798c2ecf20Sopenharmony_ci
6808c2ecf20Sopenharmony_ci컨트롤 의존성
6818c2ecf20Sopenharmony_ci-------------
6828c2ecf20Sopenharmony_ci
6838c2ecf20Sopenharmony_ci현재의 컴파일러들은 컨트롤 의존성을 이해하고 있지 않기 때문에 컨트롤 의존성은
6848c2ecf20Sopenharmony_ci약간 다루기 어려울 수 있습니다.  이 섹션의 목적은 여러분이 컴파일러의 무시로
6858c2ecf20Sopenharmony_ci인해 여러분의 코드가 망가지는 걸 막을 수 있도록 돕는겁니다.
6868c2ecf20Sopenharmony_ci
6878c2ecf20Sopenharmony_ci로드-로드 컨트롤 의존성은 데이터 의존성 배리어만으로는 정확히 동작할 수가
6888c2ecf20Sopenharmony_ci없어서 읽기 메모리 배리어를 필요로 합니다.  아래의 코드를 봅시다:
6898c2ecf20Sopenharmony_ci
6908c2ecf20Sopenharmony_ci	q = READ_ONCE(a);
6918c2ecf20Sopenharmony_ci	if (q) {
6928c2ecf20Sopenharmony_ci		<데이터 의존성 배리어>  /* BUG: No data dependency!!! */
6938c2ecf20Sopenharmony_ci		p = READ_ONCE(b);
6948c2ecf20Sopenharmony_ci	}
6958c2ecf20Sopenharmony_ci
6968c2ecf20Sopenharmony_ci이 코드는 원하는 대로의 효과를 내지 못할 수 있는데, 이 코드에는 데이터 의존성이
6978c2ecf20Sopenharmony_ci아니라 컨트롤 의존성이 존재하기 때문으로, 이런 상황에서 CPU 는 실행 속도를 더
6988c2ecf20Sopenharmony_ci빠르게 하기 위해 분기 조건의 결과를 예측하고 코드를 재배치 할 수 있어서 다른
6998c2ecf20Sopenharmony_ciCPU 는 b 로부터의 로드 오퍼레이션이 a 로부터의 로드 오퍼레이션보다 먼저 발생한
7008c2ecf20Sopenharmony_ci걸로 인식할 수 있습니다.  여기에 정말로 필요했던 건 다음과 같습니다:
7018c2ecf20Sopenharmony_ci
7028c2ecf20Sopenharmony_ci	q = READ_ONCE(a);
7038c2ecf20Sopenharmony_ci	if (q) {
7048c2ecf20Sopenharmony_ci		<읽기 배리어>
7058c2ecf20Sopenharmony_ci		p = READ_ONCE(b);
7068c2ecf20Sopenharmony_ci	}
7078c2ecf20Sopenharmony_ci
7088c2ecf20Sopenharmony_ci하지만, 스토어 오퍼레이션은 예측적으로 수행되지 않습니다.  즉, 다음 예에서와
7098c2ecf20Sopenharmony_ci같이 로드-스토어 컨트롤 의존성이 존재하는 경우에는 순서가 -지켜진다-는
7108c2ecf20Sopenharmony_ci의미입니다.
7118c2ecf20Sopenharmony_ci
7128c2ecf20Sopenharmony_ci	q = READ_ONCE(a);
7138c2ecf20Sopenharmony_ci	if (q) {
7148c2ecf20Sopenharmony_ci		WRITE_ONCE(b, 1);
7158c2ecf20Sopenharmony_ci	}
7168c2ecf20Sopenharmony_ci
7178c2ecf20Sopenharmony_ci컨트롤 의존성은 보통 다른 타입의 배리어들과 짝을 맞춰 사용됩니다.  그렇다곤
7188c2ecf20Sopenharmony_ci하나, READ_ONCE() 도 WRITE_ONCE() 도 선택사항이 아니라 필수사항임을 부디
7198c2ecf20Sopenharmony_ci명심하세요!  READ_ONCE() 가 없다면, 컴파일러는 'a' 로부터의 로드를 'a' 로부터의
7208c2ecf20Sopenharmony_ci또다른 로드와 조합할 수 있습니다.  WRITE_ONCE() 가 없다면, 컴파일러는 'b' 로의
7218c2ecf20Sopenharmony_ci스토어를 'b' 로의 또라느 스토어들과 조합할 수 있습니다.  두 경우 모두 순서에
7228c2ecf20Sopenharmony_ci있어 상당히 비직관적인 결과를 초래할 수 있습니다.
7238c2ecf20Sopenharmony_ci
7248c2ecf20Sopenharmony_ci이걸로 끝이 아닌게, 컴파일러가 변수 'a' 의 값이 항상 0이 아니라고 증명할 수
7258c2ecf20Sopenharmony_ci있다면, 앞의 예에서 "if" 문을 없애서 다음과 같이 최적화 할 수도 있습니다:
7268c2ecf20Sopenharmony_ci
7278c2ecf20Sopenharmony_ci	q = a;
7288c2ecf20Sopenharmony_ci	b = 1;  /* BUG: Compiler and CPU can both reorder!!! */
7298c2ecf20Sopenharmony_ci
7308c2ecf20Sopenharmony_ci그러니 READ_ONCE() 를 반드시 사용하세요.
7318c2ecf20Sopenharmony_ci
7328c2ecf20Sopenharmony_ci다음과 같이 "if" 문의 양갈래 브랜치에 모두 존재하는 동일한 스토어에 대해 순서를
7338c2ecf20Sopenharmony_ci강제하고 싶은 경우가 있을 수 있습니다:
7348c2ecf20Sopenharmony_ci
7358c2ecf20Sopenharmony_ci	q = READ_ONCE(a);
7368c2ecf20Sopenharmony_ci	if (q) {
7378c2ecf20Sopenharmony_ci		barrier();
7388c2ecf20Sopenharmony_ci		WRITE_ONCE(b, 1);
7398c2ecf20Sopenharmony_ci		do_something();
7408c2ecf20Sopenharmony_ci	} else {
7418c2ecf20Sopenharmony_ci		barrier();
7428c2ecf20Sopenharmony_ci		WRITE_ONCE(b, 1);
7438c2ecf20Sopenharmony_ci		do_something_else();
7448c2ecf20Sopenharmony_ci	}
7458c2ecf20Sopenharmony_ci
7468c2ecf20Sopenharmony_ci안타깝게도, 현재의 컴파일러들은 높은 최적화 레벨에서는 이걸 다음과 같이
7478c2ecf20Sopenharmony_ci바꿔버립니다:
7488c2ecf20Sopenharmony_ci
7498c2ecf20Sopenharmony_ci	q = READ_ONCE(a);
7508c2ecf20Sopenharmony_ci	barrier();
7518c2ecf20Sopenharmony_ci	WRITE_ONCE(b, 1);  /* BUG: No ordering vs. load from a!!! */
7528c2ecf20Sopenharmony_ci	if (q) {
7538c2ecf20Sopenharmony_ci		/* WRITE_ONCE(b, 1); -- moved up, BUG!!! */
7548c2ecf20Sopenharmony_ci		do_something();
7558c2ecf20Sopenharmony_ci	} else {
7568c2ecf20Sopenharmony_ci		/* WRITE_ONCE(b, 1); -- moved up, BUG!!! */
7578c2ecf20Sopenharmony_ci		do_something_else();
7588c2ecf20Sopenharmony_ci	}
7598c2ecf20Sopenharmony_ci
7608c2ecf20Sopenharmony_ci이제 'a' 에서의 로드와 'b' 로의 스토어 사이에는 조건적 관계가 없기 때문에 CPU
7618c2ecf20Sopenharmony_ci는 이들의 순서를 바꿀 수 있게 됩니다: 이런 경우에 조건적 관계는 반드시
7628c2ecf20Sopenharmony_ci필요한데, 모든 컴파일러 최적화가 이루어지고 난 후의 어셈블리 코드에서도
7638c2ecf20Sopenharmony_ci마찬가지입니다.  따라서, 이 예에서 순서를 지키기 위해서는 smp_store_release()
7648c2ecf20Sopenharmony_ci와 같은 명시적 메모리 배리어가 필요합니다:
7658c2ecf20Sopenharmony_ci
7668c2ecf20Sopenharmony_ci	q = READ_ONCE(a);
7678c2ecf20Sopenharmony_ci	if (q) {
7688c2ecf20Sopenharmony_ci		smp_store_release(&b, 1);
7698c2ecf20Sopenharmony_ci		do_something();
7708c2ecf20Sopenharmony_ci	} else {
7718c2ecf20Sopenharmony_ci		smp_store_release(&b, 1);
7728c2ecf20Sopenharmony_ci		do_something_else();
7738c2ecf20Sopenharmony_ci	}
7748c2ecf20Sopenharmony_ci
7758c2ecf20Sopenharmony_ci반면에 명시적 메모리 배리어가 없다면, 이런 경우의 순서는 스토어 오퍼레이션들이
7768c2ecf20Sopenharmony_ci서로 다를 때에만 보장되는데, 예를 들면 다음과 같은 경우입니다:
7778c2ecf20Sopenharmony_ci
7788c2ecf20Sopenharmony_ci	q = READ_ONCE(a);
7798c2ecf20Sopenharmony_ci	if (q) {
7808c2ecf20Sopenharmony_ci		WRITE_ONCE(b, 1);
7818c2ecf20Sopenharmony_ci		do_something();
7828c2ecf20Sopenharmony_ci	} else {
7838c2ecf20Sopenharmony_ci		WRITE_ONCE(b, 2);
7848c2ecf20Sopenharmony_ci		do_something_else();
7858c2ecf20Sopenharmony_ci	}
7868c2ecf20Sopenharmony_ci
7878c2ecf20Sopenharmony_ci처음의 READ_ONCE() 는 컴파일러가 'a' 의 값을 증명해내는 것을 막기 위해 여전히
7888c2ecf20Sopenharmony_ci필요합니다.
7898c2ecf20Sopenharmony_ci
7908c2ecf20Sopenharmony_ci또한, 로컬 변수 'q' 를 가지고 하는 일에 대해 주의해야 하는데, 그러지 않으면
7918c2ecf20Sopenharmony_ci컴파일러는 그 값을 추측하고 또다시 필요한 조건관계를 없애버릴 수 있습니다.
7928c2ecf20Sopenharmony_ci예를 들면:
7938c2ecf20Sopenharmony_ci
7948c2ecf20Sopenharmony_ci	q = READ_ONCE(a);
7958c2ecf20Sopenharmony_ci	if (q % MAX) {
7968c2ecf20Sopenharmony_ci		WRITE_ONCE(b, 1);
7978c2ecf20Sopenharmony_ci		do_something();
7988c2ecf20Sopenharmony_ci	} else {
7998c2ecf20Sopenharmony_ci		WRITE_ONCE(b, 2);
8008c2ecf20Sopenharmony_ci		do_something_else();
8018c2ecf20Sopenharmony_ci	}
8028c2ecf20Sopenharmony_ci
8038c2ecf20Sopenharmony_ci만약 MAX 가 1 로 정의된 상수라면, 컴파일러는 (q % MAX) 는 0이란 것을 알아채고,
8048c2ecf20Sopenharmony_ci위의 코드를 아래와 같이 바꿔버릴 수 있습니다:
8058c2ecf20Sopenharmony_ci
8068c2ecf20Sopenharmony_ci	q = READ_ONCE(a);
8078c2ecf20Sopenharmony_ci	WRITE_ONCE(b, 2);
8088c2ecf20Sopenharmony_ci	do_something_else();
8098c2ecf20Sopenharmony_ci
8108c2ecf20Sopenharmony_ci이렇게 되면, CPU 는 변수 'a' 로부터의 로드와 변수 'b' 로의 스토어 사이의 순서를
8118c2ecf20Sopenharmony_ci지켜줄 필요가 없어집니다.  barrier() 를 추가해 해결해 보고 싶겠지만, 그건
8128c2ecf20Sopenharmony_ci도움이 안됩니다.  조건 관계는 사라졌고, barrier() 는 이를 되돌리지 못합니다.
8138c2ecf20Sopenharmony_ci따라서, 이 순서를 지켜야 한다면, MAX 가 1 보다 크다는 것을, 다음과 같은 방법을
8148c2ecf20Sopenharmony_ci사용해 분명히 해야 합니다:
8158c2ecf20Sopenharmony_ci
8168c2ecf20Sopenharmony_ci	q = READ_ONCE(a);
8178c2ecf20Sopenharmony_ci	BUILD_BUG_ON(MAX <= 1); /* Order load from a with store to b. */
8188c2ecf20Sopenharmony_ci	if (q % MAX) {
8198c2ecf20Sopenharmony_ci		WRITE_ONCE(b, 1);
8208c2ecf20Sopenharmony_ci		do_something();
8218c2ecf20Sopenharmony_ci	} else {
8228c2ecf20Sopenharmony_ci		WRITE_ONCE(b, 2);
8238c2ecf20Sopenharmony_ci		do_something_else();
8248c2ecf20Sopenharmony_ci	}
8258c2ecf20Sopenharmony_ci
8268c2ecf20Sopenharmony_ci'b' 로의 스토어들은 여전히 서로 다름을 알아두세요.  만약 그것들이 동일하면,
8278c2ecf20Sopenharmony_ci앞에서 이야기했듯, 컴파일러가 그 스토어 오퍼레이션들을 'if' 문 바깥으로
8288c2ecf20Sopenharmony_ci끄집어낼 수 있습니다.
8298c2ecf20Sopenharmony_ci
8308c2ecf20Sopenharmony_ci또한 이진 조건문 평가에 너무 의존하지 않도록 조심해야 합니다.  다음의 예를
8318c2ecf20Sopenharmony_ci봅시다:
8328c2ecf20Sopenharmony_ci
8338c2ecf20Sopenharmony_ci	q = READ_ONCE(a);
8348c2ecf20Sopenharmony_ci	if (q || 1 > 0)
8358c2ecf20Sopenharmony_ci		WRITE_ONCE(b, 1);
8368c2ecf20Sopenharmony_ci
8378c2ecf20Sopenharmony_ci첫번째 조건만으로는 브랜치 조건 전체를 거짓으로 만들 수 없고 두번째 조건은 항상
8388c2ecf20Sopenharmony_ci참이기 때문에, 컴파일러는 이 예를 다음과 같이 바꿔서 컨트롤 의존성을 없애버릴
8398c2ecf20Sopenharmony_ci수 있습니다:
8408c2ecf20Sopenharmony_ci
8418c2ecf20Sopenharmony_ci	q = READ_ONCE(a);
8428c2ecf20Sopenharmony_ci	WRITE_ONCE(b, 1);
8438c2ecf20Sopenharmony_ci
8448c2ecf20Sopenharmony_ci이 예는 컴파일러가 코드를 추측으로 수정할 수 없도록 분명히 해야 한다는 점을
8458c2ecf20Sopenharmony_ci강조합니다.  조금 더 일반적으로 말해서, READ_ONCE() 는 컴파일러에게 주어진 로드
8468c2ecf20Sopenharmony_ci오퍼레이션을 위한 코드를 정말로 만들도록 하지만, 컴파일러가 그렇게 만들어진
8478c2ecf20Sopenharmony_ci코드의 수행 결과를 사용하도록 강제하지는 않습니다.
8488c2ecf20Sopenharmony_ci
8498c2ecf20Sopenharmony_ci또한, 컨트롤 의존성은 if 문의 then 절과 else 절에 대해서만 적용됩니다.  상세히
8508c2ecf20Sopenharmony_ci말해서, 컨트롤 의존성은 if 문을 뒤따르는 코드에는 적용되지 않습니다:
8518c2ecf20Sopenharmony_ci
8528c2ecf20Sopenharmony_ci	q = READ_ONCE(a);
8538c2ecf20Sopenharmony_ci	if (q) {
8548c2ecf20Sopenharmony_ci		WRITE_ONCE(b, 1);
8558c2ecf20Sopenharmony_ci	} else {
8568c2ecf20Sopenharmony_ci		WRITE_ONCE(b, 2);
8578c2ecf20Sopenharmony_ci	}
8588c2ecf20Sopenharmony_ci	WRITE_ONCE(c, 1);  /* BUG: No ordering against the read from 'a'. */
8598c2ecf20Sopenharmony_ci
8608c2ecf20Sopenharmony_ci컴파일러는 volatile 타입에 대한 액세스를 재배치 할 수 없고 이 조건 하의 'b'
8618c2ecf20Sopenharmony_ci로의 쓰기를 재배치 할 수 없기 때문에 여기에 순서 규칙이 존재한다고 주장하고
8628c2ecf20Sopenharmony_ci싶을 겁니다.  불행히도 이 경우에, 컴파일러는 다음의 가상의 pseudo-assembly 언어
8638c2ecf20Sopenharmony_ci코드처럼 'b' 로의 두개의 쓰기 오퍼레이션을 conditional-move 인스트럭션으로
8648c2ecf20Sopenharmony_ci번역할 수 있습니다:
8658c2ecf20Sopenharmony_ci
8668c2ecf20Sopenharmony_ci	ld r1,a
8678c2ecf20Sopenharmony_ci	cmp r1,$0
8688c2ecf20Sopenharmony_ci	cmov,ne r4,$1
8698c2ecf20Sopenharmony_ci	cmov,eq r4,$2
8708c2ecf20Sopenharmony_ci	st r4,b
8718c2ecf20Sopenharmony_ci	st $1,c
8728c2ecf20Sopenharmony_ci
8738c2ecf20Sopenharmony_ci완화된 순서 규칙의 CPU 는 'a' 로부터의 로드와 'c' 로의 스토어 사이에 어떤
8748c2ecf20Sopenharmony_ci종류의 의존성도 갖지 않을 겁니다.  이 컨트롤 의존성은 두개의 cmov 인스트럭션과
8758c2ecf20Sopenharmony_ci거기에 의존하는 스토어 에게만 적용될 겁니다.  짧게 말하자면, 컨트롤 의존성은
8768c2ecf20Sopenharmony_ci주어진 if 문의 then 절과 else 절에게만 (그리고 이 두 절 내에서 호출되는
8778c2ecf20Sopenharmony_ci함수들에게까지) 적용되지, 이 if 문을 뒤따르는 코드에는 적용되지 않습니다.
8788c2ecf20Sopenharmony_ci
8798c2ecf20Sopenharmony_ci
8808c2ecf20Sopenharmony_ci컨트롤 의존성에 의해 제공되는 이 순서규칙은 이를 포함하고 있는 CPU 에
8818c2ecf20Sopenharmony_ci지역적입니다.  더 많은 정보를 위해선 "Multicopy 원자성" 섹션을 참고하세요.
8828c2ecf20Sopenharmony_ci
8838c2ecf20Sopenharmony_ci
8848c2ecf20Sopenharmony_ci요약하자면:
8858c2ecf20Sopenharmony_ci
8868c2ecf20Sopenharmony_ci  (*) 컨트롤 의존성은 앞의 로드들을 뒤의 스토어들에 대해 순서를 맞춰줍니다.
8878c2ecf20Sopenharmony_ci      하지만, 그 외의 어떤 순서도 보장하지 -않습니다-: 앞의 로드와 뒤의 로드들
8888c2ecf20Sopenharmony_ci      사이에도, 앞의 스토어와 뒤의 스토어들 사이에도요.  이런 다른 형태의
8898c2ecf20Sopenharmony_ci      순서가 필요하다면 smp_rmb() 나 smp_wmb()를, 또는, 앞의 스토어들과 뒤의
8908c2ecf20Sopenharmony_ci      로드들 사이의 순서를 위해서는 smp_mb() 를 사용하세요.
8918c2ecf20Sopenharmony_ci
8928c2ecf20Sopenharmony_ci  (*) "if" 문의 양갈래 브랜치가 같은 변수에의 동일한 스토어로 시작한다면, 그
8938c2ecf20Sopenharmony_ci      스토어들은 각 스토어 앞에 smp_mb() 를 넣거나 smp_store_release() 를
8948c2ecf20Sopenharmony_ci      사용해서 스토어를 하는 식으로 순서를 맞춰줘야 합니다.  이 문제를 해결하기
8958c2ecf20Sopenharmony_ci      위해 "if" 문의 양갈래 브랜치의 시작 지점에 barrier() 를 넣는 것만으로는
8968c2ecf20Sopenharmony_ci      충분한 해결이 되지 않는데, 이는 앞의 예에서 본것과 같이, 컴파일러의
8978c2ecf20Sopenharmony_ci      최적화는 barrier() 가 의미하는 바를 지키면서도 컨트롤 의존성을 손상시킬
8988c2ecf20Sopenharmony_ci      수 있기 때문이라는 점을 부디 알아두시기 바랍니다.
8998c2ecf20Sopenharmony_ci
9008c2ecf20Sopenharmony_ci  (*) 컨트롤 의존성은 앞의 로드와 뒤의 스토어 사이에 최소 하나의, 실행
9018c2ecf20Sopenharmony_ci      시점에서의 조건관계를 필요로 하며, 이 조건관계는 앞의 로드와 관계되어야
9028c2ecf20Sopenharmony_ci      합니다.  만약 컴파일러가 조건 관계를 최적화로 없앨수 있다면, 순서도
9038c2ecf20Sopenharmony_ci      최적화로 없애버렸을 겁니다.  READ_ONCE() 와 WRITE_ONCE() 의 주의 깊은
9048c2ecf20Sopenharmony_ci      사용은 주어진 조건 관계를 유지하는데 도움이 될 수 있습니다.
9058c2ecf20Sopenharmony_ci
9068c2ecf20Sopenharmony_ci  (*) 컨트롤 의존성을 위해선 컴파일러가 조건관계를 없애버리는 것을 막아야
9078c2ecf20Sopenharmony_ci      합니다.  주의 깊은 READ_ONCE() 나 atomic{,64}_read() 의 사용이 컨트롤
9088c2ecf20Sopenharmony_ci      의존성이 사라지지 않게 하는데 도움을 줄 수 있습니다.  더 많은 정보를
9098c2ecf20Sopenharmony_ci      위해선 "컴파일러 배리어" 섹션을 참고하시기 바랍니다.
9108c2ecf20Sopenharmony_ci
9118c2ecf20Sopenharmony_ci  (*) 컨트롤 의존성은 컨트롤 의존성을 갖는 if 문의 then 절과 else 절과 이 두 절
9128c2ecf20Sopenharmony_ci      내에서 호출되는 함수들에만 적용됩니다.  컨트롤 의존성은 컨트롤 의존성을
9138c2ecf20Sopenharmony_ci      갖는 if 문을 뒤따르는 코드에는 적용되지 -않습니다-.
9148c2ecf20Sopenharmony_ci
9158c2ecf20Sopenharmony_ci  (*) 컨트롤 의존성은 보통 다른 타입의 배리어들과 짝을 맞춰 사용됩니다.
9168c2ecf20Sopenharmony_ci
9178c2ecf20Sopenharmony_ci  (*) 컨트롤 의존성은 multicopy 원자성을 제공하지 -않습니다-.  모든 CPU 들이
9188c2ecf20Sopenharmony_ci      특정 스토어를 동시에 보길 원한다면, smp_mb() 를 사용하세요.
9198c2ecf20Sopenharmony_ci
9208c2ecf20Sopenharmony_ci  (*) 컴파일러는 컨트롤 의존성을 이해하고 있지 않습니다.  따라서 컴파일러가
9218c2ecf20Sopenharmony_ci      여러분의 코드를 망가뜨리지 않도록 하는건 여러분이 해야 하는 일입니다.
9228c2ecf20Sopenharmony_ci
9238c2ecf20Sopenharmony_ci
9248c2ecf20Sopenharmony_ciSMP 배리어 짝맞추기
9258c2ecf20Sopenharmony_ci--------------------
9268c2ecf20Sopenharmony_ci
9278c2ecf20Sopenharmony_ciCPU 간 상호작용을 다룰 때에 일부 타입의 메모리 배리어는 항상 짝을 맞춰
9288c2ecf20Sopenharmony_ci사용되어야 합니다.  적절하게 짝을 맞추지 않은 코드는 사실상 에러에 가깝습니다.
9298c2ecf20Sopenharmony_ci
9308c2ecf20Sopenharmony_ci범용 배리어들은 범용 배리어끼리도 짝을 맞추지만 multicopy 원자성이 없는
9318c2ecf20Sopenharmony_ci대부분의 다른 타입의 배리어들과도 짝을 맞춥니다.  ACQUIRE 배리어는 RELEASE
9328c2ecf20Sopenharmony_ci배리어와 짝을 맞춥니다만, 둘 다 범용 배리어를 포함해 다른 배리어들과도 짝을
9338c2ecf20Sopenharmony_ci맞출 수 있습니다.  쓰기 배리어는 데이터 의존성 배리어나 컨트롤 의존성, ACQUIRE
9348c2ecf20Sopenharmony_ci배리어, RELEASE 배리어, 읽기 배리어, 또는 범용 배리어와 짝을 맞춥니다.
9358c2ecf20Sopenharmony_ci비슷하게 읽기 배리어나 컨트롤 의존성, 또는 데이터 의존성 배리어는 쓰기 배리어나
9368c2ecf20Sopenharmony_ciACQUIRE 배리어, RELEASE 배리어, 또는 범용 배리어와 짝을 맞추는데, 다음과
9378c2ecf20Sopenharmony_ci같습니다:
9388c2ecf20Sopenharmony_ci
9398c2ecf20Sopenharmony_ci	CPU 1		      CPU 2
9408c2ecf20Sopenharmony_ci	===============	      ===============
9418c2ecf20Sopenharmony_ci	WRITE_ONCE(a, 1);
9428c2ecf20Sopenharmony_ci	<쓰기 배리어>
9438c2ecf20Sopenharmony_ci	WRITE_ONCE(b, 2);     x = READ_ONCE(b);
9448c2ecf20Sopenharmony_ci			      <읽기 배리어>
9458c2ecf20Sopenharmony_ci			      y = READ_ONCE(a);
9468c2ecf20Sopenharmony_ci
9478c2ecf20Sopenharmony_ci또는:
9488c2ecf20Sopenharmony_ci
9498c2ecf20Sopenharmony_ci	CPU 1		      CPU 2
9508c2ecf20Sopenharmony_ci	===============	      ===============================
9518c2ecf20Sopenharmony_ci	a = 1;
9528c2ecf20Sopenharmony_ci	<쓰기 배리어>
9538c2ecf20Sopenharmony_ci	WRITE_ONCE(b, &a);    x = READ_ONCE(b);
9548c2ecf20Sopenharmony_ci			      <데이터 의존성 배리어>
9558c2ecf20Sopenharmony_ci			      y = *x;
9568c2ecf20Sopenharmony_ci
9578c2ecf20Sopenharmony_ci또는:
9588c2ecf20Sopenharmony_ci
9598c2ecf20Sopenharmony_ci	CPU 1		      CPU 2
9608c2ecf20Sopenharmony_ci	===============	      ===============================
9618c2ecf20Sopenharmony_ci	r1 = READ_ONCE(y);
9628c2ecf20Sopenharmony_ci	<범용 배리어>
9638c2ecf20Sopenharmony_ci	WRITE_ONCE(x, 1);     if (r2 = READ_ONCE(x)) {
9648c2ecf20Sopenharmony_ci			         <묵시적 컨트롤 의존성>
9658c2ecf20Sopenharmony_ci			         WRITE_ONCE(y, 1);
9668c2ecf20Sopenharmony_ci			      }
9678c2ecf20Sopenharmony_ci
9688c2ecf20Sopenharmony_ci	assert(r1 == 0 || r2 == 0);
9698c2ecf20Sopenharmony_ci
9708c2ecf20Sopenharmony_ci기본적으로, 여기서의 읽기 배리어는 "더 완화된" 타입일 순 있어도 항상 존재해야
9718c2ecf20Sopenharmony_ci합니다.
9728c2ecf20Sopenharmony_ci
9738c2ecf20Sopenharmony_ci[!] 쓰기 배리어 앞의 스토어 오퍼레이션은 일반적으로 읽기 배리어나 데이터
9748c2ecf20Sopenharmony_ci의존성 배리어 뒤의 로드 오퍼레이션과 매치될 것이고, 반대도 마찬가지입니다:
9758c2ecf20Sopenharmony_ci
9768c2ecf20Sopenharmony_ci	CPU 1                               CPU 2
9778c2ecf20Sopenharmony_ci	===================                 ===================
9788c2ecf20Sopenharmony_ci	WRITE_ONCE(a, 1);    }----   --->{  v = READ_ONCE(c);
9798c2ecf20Sopenharmony_ci	WRITE_ONCE(b, 2);    }    \ /    {  w = READ_ONCE(d);
9808c2ecf20Sopenharmony_ci	<쓰기 배리어>              \        <읽기 배리어>
9818c2ecf20Sopenharmony_ci	WRITE_ONCE(c, 3);    }    / \    {  x = READ_ONCE(a);
9828c2ecf20Sopenharmony_ci	WRITE_ONCE(d, 4);    }----   --->{  y = READ_ONCE(b);
9838c2ecf20Sopenharmony_ci
9848c2ecf20Sopenharmony_ci
9858c2ecf20Sopenharmony_ci메모리 배리어 시퀀스의 예
9868c2ecf20Sopenharmony_ci-------------------------
9878c2ecf20Sopenharmony_ci
9888c2ecf20Sopenharmony_ci첫째, 쓰기 배리어는 스토어 오퍼레이션들의 부분적 순서 세우기로 동작합니다.
9898c2ecf20Sopenharmony_ci아래의 이벤트 시퀀스를 보세요:
9908c2ecf20Sopenharmony_ci
9918c2ecf20Sopenharmony_ci	CPU 1
9928c2ecf20Sopenharmony_ci	=======================
9938c2ecf20Sopenharmony_ci	STORE A = 1
9948c2ecf20Sopenharmony_ci	STORE B = 2
9958c2ecf20Sopenharmony_ci	STORE C = 3
9968c2ecf20Sopenharmony_ci	<쓰기 배리어>
9978c2ecf20Sopenharmony_ci	STORE D = 4
9988c2ecf20Sopenharmony_ci	STORE E = 5
9998c2ecf20Sopenharmony_ci
10008c2ecf20Sopenharmony_ci이 이벤트 시퀀스는 메모리 일관성 시스템에 원소끼리의 순서가 존재하지 않는 집합
10018c2ecf20Sopenharmony_ci{ STORE A, STORE B, STORE C } 가 역시 원소끼리의 순서가 존재하지 않는 집합
10028c2ecf20Sopenharmony_ci{ STORE D, STORE E } 보다 먼저 일어난 것으로 시스템의 나머지 요소들에 보이도록
10038c2ecf20Sopenharmony_ci전달됩니다:
10048c2ecf20Sopenharmony_ci
10058c2ecf20Sopenharmony_ci	+-------+       :      :
10068c2ecf20Sopenharmony_ci	|       |       +------+
10078c2ecf20Sopenharmony_ci	|       |------>| C=3  |     }     /\
10088c2ecf20Sopenharmony_ci	|       |  :    +------+     }-----  \  -----> 시스템의 나머지 요소에
10098c2ecf20Sopenharmony_ci	|       |  :    | A=1  |     }        \/       보여질 수 있는 이벤트들
10108c2ecf20Sopenharmony_ci	|       |  :    +------+     }
10118c2ecf20Sopenharmony_ci	| CPU 1 |  :    | B=2  |     }
10128c2ecf20Sopenharmony_ci	|       |       +------+     }
10138c2ecf20Sopenharmony_ci	|       |   wwwwwwwwwwwwwwww }   <--- 여기서 쓰기 배리어는 배리어 앞의
10148c2ecf20Sopenharmony_ci	|       |       +------+     }        모든 스토어가 배리어 뒤의 스토어
10158c2ecf20Sopenharmony_ci	|       |  :    | E=5  |     }        전에 메모리 시스템에 전달되도록
10168c2ecf20Sopenharmony_ci	|       |  :    +------+     }        합니다
10178c2ecf20Sopenharmony_ci	|       |------>| D=4  |     }
10188c2ecf20Sopenharmony_ci	|       |       +------+
10198c2ecf20Sopenharmony_ci	+-------+       :      :
10208c2ecf20Sopenharmony_ci	                   |
10218c2ecf20Sopenharmony_ci	                   | CPU 1 에 의해 메모리 시스템에 전달되는
10228c2ecf20Sopenharmony_ci	                   | 일련의 스토어 오퍼레이션들
10238c2ecf20Sopenharmony_ci	                   V
10248c2ecf20Sopenharmony_ci
10258c2ecf20Sopenharmony_ci
10268c2ecf20Sopenharmony_ci둘째, 데이터 의존성 배리어는 데이터 의존적 로드 오퍼레이션들의 부분적 순서
10278c2ecf20Sopenharmony_ci세우기로 동작합니다.  다음 일련의 이벤트들을 보세요:
10288c2ecf20Sopenharmony_ci
10298c2ecf20Sopenharmony_ci	CPU 1			CPU 2
10308c2ecf20Sopenharmony_ci	=======================	=======================
10318c2ecf20Sopenharmony_ci		{ B = 7; X = 9; Y = 8; C = &Y }
10328c2ecf20Sopenharmony_ci	STORE A = 1
10338c2ecf20Sopenharmony_ci	STORE B = 2
10348c2ecf20Sopenharmony_ci	<쓰기 배리어>
10358c2ecf20Sopenharmony_ci	STORE C = &B		LOAD X
10368c2ecf20Sopenharmony_ci	STORE D = 4		LOAD C (gets &B)
10378c2ecf20Sopenharmony_ci				LOAD *C (reads B)
10388c2ecf20Sopenharmony_ci
10398c2ecf20Sopenharmony_ci여기에 별다른 개입이 없다면, CPU 1 의 쓰기 배리어에도 불구하고 CPU 2 는 CPU 1
10408c2ecf20Sopenharmony_ci의 이벤트들을 완전히 무작위적 순서로 인지하게 됩니다:
10418c2ecf20Sopenharmony_ci
10428c2ecf20Sopenharmony_ci	+-------+       :      :                :       :
10438c2ecf20Sopenharmony_ci	|       |       +------+                +-------+  | CPU 2 에 인지되는
10448c2ecf20Sopenharmony_ci	|       |------>| B=2  |-----       --->| Y->8  |  | 업데이트 이벤트
10458c2ecf20Sopenharmony_ci	|       |  :    +------+     \          +-------+  | 시퀀스
10468c2ecf20Sopenharmony_ci	| CPU 1 |  :    | A=1  |      \     --->| C->&Y |  V
10478c2ecf20Sopenharmony_ci	|       |       +------+       |        +-------+
10488c2ecf20Sopenharmony_ci	|       |   wwwwwwwwwwwwwwww   |        :       :
10498c2ecf20Sopenharmony_ci	|       |       +------+       |        :       :
10508c2ecf20Sopenharmony_ci	|       |  :    | C=&B |---    |        :       :       +-------+
10518c2ecf20Sopenharmony_ci	|       |  :    +------+   \   |        +-------+       |       |
10528c2ecf20Sopenharmony_ci	|       |------>| D=4  |    ----------->| C->&B |------>|       |
10538c2ecf20Sopenharmony_ci	|       |       +------+       |        +-------+       |       |
10548c2ecf20Sopenharmony_ci	+-------+       :      :       |        :       :       |       |
10558c2ecf20Sopenharmony_ci	                               |        :       :       |       |
10568c2ecf20Sopenharmony_ci	                               |        :       :       | CPU 2 |
10578c2ecf20Sopenharmony_ci	                               |        +-------+       |       |
10588c2ecf20Sopenharmony_ci	    분명히 잘못된        --->  |        | B->7  |------>|       |
10598c2ecf20Sopenharmony_ci	    B 의 값 인지 (!)           |        +-------+       |       |
10608c2ecf20Sopenharmony_ci	                               |        :       :       |       |
10618c2ecf20Sopenharmony_ci	                               |        +-------+       |       |
10628c2ecf20Sopenharmony_ci	    X 의 로드가 B 의    --->    \       | X->9  |------>|       |
10638c2ecf20Sopenharmony_ci	    일관성 유지를                \      +-------+       |       |
10648c2ecf20Sopenharmony_ci	    지연시킴                      ----->| B->2  |       +-------+
10658c2ecf20Sopenharmony_ci	                                        +-------+
10668c2ecf20Sopenharmony_ci	                                        :       :
10678c2ecf20Sopenharmony_ci
10688c2ecf20Sopenharmony_ci
10698c2ecf20Sopenharmony_ci앞의 예에서, CPU 2 는 (B 의 값이 될) *C 의 값 읽기가 C 의 LOAD 뒤에 이어짐에도
10708c2ecf20Sopenharmony_ciB 가 7 이라는 결과를 얻습니다.
10718c2ecf20Sopenharmony_ci
10728c2ecf20Sopenharmony_ci하지만, 만약 데이터 의존성 배리어가 C 의 로드와 *C (즉, B) 의 로드 사이에
10738c2ecf20Sopenharmony_ci있었다면:
10748c2ecf20Sopenharmony_ci
10758c2ecf20Sopenharmony_ci	CPU 1			CPU 2
10768c2ecf20Sopenharmony_ci	=======================	=======================
10778c2ecf20Sopenharmony_ci		{ B = 7; X = 9; Y = 8; C = &Y }
10788c2ecf20Sopenharmony_ci	STORE A = 1
10798c2ecf20Sopenharmony_ci	STORE B = 2
10808c2ecf20Sopenharmony_ci	<쓰기 배리어>
10818c2ecf20Sopenharmony_ci	STORE C = &B		LOAD X
10828c2ecf20Sopenharmony_ci	STORE D = 4		LOAD C (gets &B)
10838c2ecf20Sopenharmony_ci				<데이터 의존성 배리어>
10848c2ecf20Sopenharmony_ci				LOAD *C (reads B)
10858c2ecf20Sopenharmony_ci
10868c2ecf20Sopenharmony_ci다음과 같이 됩니다:
10878c2ecf20Sopenharmony_ci
10888c2ecf20Sopenharmony_ci	+-------+       :      :                :       :
10898c2ecf20Sopenharmony_ci	|       |       +------+                +-------+
10908c2ecf20Sopenharmony_ci	|       |------>| B=2  |-----       --->| Y->8  |
10918c2ecf20Sopenharmony_ci	|       |  :    +------+     \          +-------+
10928c2ecf20Sopenharmony_ci	| CPU 1 |  :    | A=1  |      \     --->| C->&Y |
10938c2ecf20Sopenharmony_ci	|       |       +------+       |        +-------+
10948c2ecf20Sopenharmony_ci	|       |   wwwwwwwwwwwwwwww   |        :       :
10958c2ecf20Sopenharmony_ci	|       |       +------+       |        :       :
10968c2ecf20Sopenharmony_ci	|       |  :    | C=&B |---    |        :       :       +-------+
10978c2ecf20Sopenharmony_ci	|       |  :    +------+   \   |        +-------+       |       |
10988c2ecf20Sopenharmony_ci	|       |------>| D=4  |    ----------->| C->&B |------>|       |
10998c2ecf20Sopenharmony_ci	|       |       +------+       |        +-------+       |       |
11008c2ecf20Sopenharmony_ci	+-------+       :      :       |        :       :       |       |
11018c2ecf20Sopenharmony_ci	                               |        :       :       |       |
11028c2ecf20Sopenharmony_ci	                               |        :       :       | CPU 2 |
11038c2ecf20Sopenharmony_ci	                               |        +-------+       |       |
11048c2ecf20Sopenharmony_ci	                               |        | X->9  |------>|       |
11058c2ecf20Sopenharmony_ci	                               |        +-------+       |       |
11068c2ecf20Sopenharmony_ci	  C 로의 스토어 앞의     --->   \   ddddddddddddddddd   |       |
11078c2ecf20Sopenharmony_ci	  모든 이벤트 결과가             \      +-------+       |       |
11088c2ecf20Sopenharmony_ci	  뒤의 로드에게                   ----->| B->2  |------>|       |
11098c2ecf20Sopenharmony_ci	  보이게 강제한다                       +-------+       |       |
11108c2ecf20Sopenharmony_ci	                                        :       :       +-------+
11118c2ecf20Sopenharmony_ci
11128c2ecf20Sopenharmony_ci
11138c2ecf20Sopenharmony_ci셋째, 읽기 배리어는 로드 오퍼레이션들에의 부분적 순서 세우기로 동작합니다.
11148c2ecf20Sopenharmony_ci아래의 일련의 이벤트를 봅시다:
11158c2ecf20Sopenharmony_ci
11168c2ecf20Sopenharmony_ci	CPU 1			CPU 2
11178c2ecf20Sopenharmony_ci	=======================	=======================
11188c2ecf20Sopenharmony_ci		{ A = 0, B = 9 }
11198c2ecf20Sopenharmony_ci	STORE A=1
11208c2ecf20Sopenharmony_ci	<쓰기 배리어>
11218c2ecf20Sopenharmony_ci	STORE B=2
11228c2ecf20Sopenharmony_ci				LOAD B
11238c2ecf20Sopenharmony_ci				LOAD A
11248c2ecf20Sopenharmony_ci
11258c2ecf20Sopenharmony_ciCPU 1 은 쓰기 배리어를 쳤지만, 별다른 개입이 없다면 CPU 2 는 CPU 1 에서 행해진
11268c2ecf20Sopenharmony_ci이벤트의 결과를 무작위적 순서로 인지하게 됩니다.
11278c2ecf20Sopenharmony_ci
11288c2ecf20Sopenharmony_ci	+-------+       :      :                :       :
11298c2ecf20Sopenharmony_ci	|       |       +------+                +-------+
11308c2ecf20Sopenharmony_ci	|       |------>| A=1  |------      --->| A->0  |
11318c2ecf20Sopenharmony_ci	|       |       +------+      \         +-------+
11328c2ecf20Sopenharmony_ci	| CPU 1 |   wwwwwwwwwwwwwwww   \    --->| B->9  |
11338c2ecf20Sopenharmony_ci	|       |       +------+        |       +-------+
11348c2ecf20Sopenharmony_ci	|       |------>| B=2  |---     |       :       :
11358c2ecf20Sopenharmony_ci	|       |       +------+   \    |       :       :       +-------+
11368c2ecf20Sopenharmony_ci	+-------+       :      :    \   |       +-------+       |       |
11378c2ecf20Sopenharmony_ci	                             ---------->| B->2  |------>|       |
11388c2ecf20Sopenharmony_ci	                                |       +-------+       | CPU 2 |
11398c2ecf20Sopenharmony_ci	                                |       | A->0  |------>|       |
11408c2ecf20Sopenharmony_ci	                                |       +-------+       |       |
11418c2ecf20Sopenharmony_ci	                                |       :       :       +-------+
11428c2ecf20Sopenharmony_ci	                                 \      :       :
11438c2ecf20Sopenharmony_ci	                                  \     +-------+
11448c2ecf20Sopenharmony_ci	                                   ---->| A->1  |
11458c2ecf20Sopenharmony_ci	                                        +-------+
11468c2ecf20Sopenharmony_ci	                                        :       :
11478c2ecf20Sopenharmony_ci
11488c2ecf20Sopenharmony_ci
11498c2ecf20Sopenharmony_ci하지만, 만약 읽기 배리어가 B 의 로드와 A 의 로드 사이에 존재한다면:
11508c2ecf20Sopenharmony_ci
11518c2ecf20Sopenharmony_ci	CPU 1			CPU 2
11528c2ecf20Sopenharmony_ci	=======================	=======================
11538c2ecf20Sopenharmony_ci		{ A = 0, B = 9 }
11548c2ecf20Sopenharmony_ci	STORE A=1
11558c2ecf20Sopenharmony_ci	<쓰기 배리어>
11568c2ecf20Sopenharmony_ci	STORE B=2
11578c2ecf20Sopenharmony_ci				LOAD B
11588c2ecf20Sopenharmony_ci				<읽기 배리어>
11598c2ecf20Sopenharmony_ci				LOAD A
11608c2ecf20Sopenharmony_ci
11618c2ecf20Sopenharmony_ciCPU 1 에 의해 만들어진 부분적 순서가 CPU 2 에도 그대로 인지됩니다:
11628c2ecf20Sopenharmony_ci
11638c2ecf20Sopenharmony_ci	+-------+       :      :                :       :
11648c2ecf20Sopenharmony_ci	|       |       +------+                +-------+
11658c2ecf20Sopenharmony_ci	|       |------>| A=1  |------      --->| A->0  |
11668c2ecf20Sopenharmony_ci	|       |       +------+      \         +-------+
11678c2ecf20Sopenharmony_ci	| CPU 1 |   wwwwwwwwwwwwwwww   \    --->| B->9  |
11688c2ecf20Sopenharmony_ci	|       |       +------+        |       +-------+
11698c2ecf20Sopenharmony_ci	|       |------>| B=2  |---     |       :       :
11708c2ecf20Sopenharmony_ci	|       |       +------+   \    |       :       :       +-------+
11718c2ecf20Sopenharmony_ci	+-------+       :      :    \   |       +-------+       |       |
11728c2ecf20Sopenharmony_ci	                             ---------->| B->2  |------>|       |
11738c2ecf20Sopenharmony_ci	                                |       +-------+       | CPU 2 |
11748c2ecf20Sopenharmony_ci	                                |       :       :       |       |
11758c2ecf20Sopenharmony_ci	                                |       :       :       |       |
11768c2ecf20Sopenharmony_ci	  여기서 읽기 배리어는   ---->   \  rrrrrrrrrrrrrrrrr   |       |
11778c2ecf20Sopenharmony_ci	  B 로의 스토어 전의              \     +-------+       |       |
11788c2ecf20Sopenharmony_ci	  모든 결과를 CPU 2 에             ---->| A->1  |------>|       |
11798c2ecf20Sopenharmony_ci	  보이도록 한다                         +-------+       |       |
11808c2ecf20Sopenharmony_ci	                                        :       :       +-------+
11818c2ecf20Sopenharmony_ci
11828c2ecf20Sopenharmony_ci
11838c2ecf20Sopenharmony_ci더 완벽한 설명을 위해, A 의 로드가 읽기 배리어 앞과 뒤에 있으면 어떻게 될지
11848c2ecf20Sopenharmony_ci생각해 봅시다:
11858c2ecf20Sopenharmony_ci
11868c2ecf20Sopenharmony_ci	CPU 1			CPU 2
11878c2ecf20Sopenharmony_ci	=======================	=======================
11888c2ecf20Sopenharmony_ci		{ A = 0, B = 9 }
11898c2ecf20Sopenharmony_ci	STORE A=1
11908c2ecf20Sopenharmony_ci	<쓰기 배리어>
11918c2ecf20Sopenharmony_ci	STORE B=2
11928c2ecf20Sopenharmony_ci				LOAD B
11938c2ecf20Sopenharmony_ci				LOAD A [first load of A]
11948c2ecf20Sopenharmony_ci				<읽기 배리어>
11958c2ecf20Sopenharmony_ci				LOAD A [second load of A]
11968c2ecf20Sopenharmony_ci
11978c2ecf20Sopenharmony_ciA 의 로드 두개가 모두 B 의 로드 뒤에 있지만, 서로 다른 값을 얻어올 수
11988c2ecf20Sopenharmony_ci있습니다:
11998c2ecf20Sopenharmony_ci
12008c2ecf20Sopenharmony_ci	+-------+       :      :                :       :
12018c2ecf20Sopenharmony_ci	|       |       +------+                +-------+
12028c2ecf20Sopenharmony_ci	|       |------>| A=1  |------      --->| A->0  |
12038c2ecf20Sopenharmony_ci	|       |       +------+      \         +-------+
12048c2ecf20Sopenharmony_ci	| CPU 1 |   wwwwwwwwwwwwwwww   \    --->| B->9  |
12058c2ecf20Sopenharmony_ci	|       |       +------+        |       +-------+
12068c2ecf20Sopenharmony_ci	|       |------>| B=2  |---     |       :       :
12078c2ecf20Sopenharmony_ci	|       |       +------+   \    |       :       :       +-------+
12088c2ecf20Sopenharmony_ci	+-------+       :      :    \   |       +-------+       |       |
12098c2ecf20Sopenharmony_ci	                             ---------->| B->2  |------>|       |
12108c2ecf20Sopenharmony_ci	                                |       +-------+       | CPU 2 |
12118c2ecf20Sopenharmony_ci	                                |       :       :       |       |
12128c2ecf20Sopenharmony_ci	                                |       :       :       |       |
12138c2ecf20Sopenharmony_ci	                                |       +-------+       |       |
12148c2ecf20Sopenharmony_ci	                                |       | A->0  |------>| 1st   |
12158c2ecf20Sopenharmony_ci	                                |       +-------+       |       |
12168c2ecf20Sopenharmony_ci	  여기서 읽기 배리어는   ---->   \  rrrrrrrrrrrrrrrrr   |       |
12178c2ecf20Sopenharmony_ci	  B 로의 스토어 전의              \     +-------+       |       |
12188c2ecf20Sopenharmony_ci	  모든 결과를 CPU 2 에             ---->| A->1  |------>| 2nd   |
12198c2ecf20Sopenharmony_ci	  보이도록 한다                         +-------+       |       |
12208c2ecf20Sopenharmony_ci	                                        :       :       +-------+
12218c2ecf20Sopenharmony_ci
12228c2ecf20Sopenharmony_ci
12238c2ecf20Sopenharmony_ci하지만 CPU 1 에서의 A 업데이트는 읽기 배리어가 완료되기 전에도 보일 수도
12248c2ecf20Sopenharmony_ci있긴 합니다:
12258c2ecf20Sopenharmony_ci
12268c2ecf20Sopenharmony_ci	+-------+       :      :                :       :
12278c2ecf20Sopenharmony_ci	|       |       +------+                +-------+
12288c2ecf20Sopenharmony_ci	|       |------>| A=1  |------      --->| A->0  |
12298c2ecf20Sopenharmony_ci	|       |       +------+      \         +-------+
12308c2ecf20Sopenharmony_ci	| CPU 1 |   wwwwwwwwwwwwwwww   \    --->| B->9  |
12318c2ecf20Sopenharmony_ci	|       |       +------+        |       +-------+
12328c2ecf20Sopenharmony_ci	|       |------>| B=2  |---     |       :       :
12338c2ecf20Sopenharmony_ci	|       |       +------+   \    |       :       :       +-------+
12348c2ecf20Sopenharmony_ci	+-------+       :      :    \   |       +-------+       |       |
12358c2ecf20Sopenharmony_ci	                             ---------->| B->2  |------>|       |
12368c2ecf20Sopenharmony_ci	                                |       +-------+       | CPU 2 |
12378c2ecf20Sopenharmony_ci	                                |       :       :       |       |
12388c2ecf20Sopenharmony_ci	                                 \      :       :       |       |
12398c2ecf20Sopenharmony_ci	                                  \     +-------+       |       |
12408c2ecf20Sopenharmony_ci	                                   ---->| A->1  |------>| 1st   |
12418c2ecf20Sopenharmony_ci	                                        +-------+       |       |
12428c2ecf20Sopenharmony_ci	                                    rrrrrrrrrrrrrrrrr   |       |
12438c2ecf20Sopenharmony_ci	                                        +-------+       |       |
12448c2ecf20Sopenharmony_ci	                                        | A->1  |------>| 2nd   |
12458c2ecf20Sopenharmony_ci	                                        +-------+       |       |
12468c2ecf20Sopenharmony_ci	                                        :       :       +-------+
12478c2ecf20Sopenharmony_ci
12488c2ecf20Sopenharmony_ci
12498c2ecf20Sopenharmony_ci여기서 보장되는 건, 만약 B 의 로드가 B == 2 라는 결과를 봤다면, A 에의 두번째
12508c2ecf20Sopenharmony_ci로드는 항상 A == 1 을 보게 될 것이라는 겁니다.  A 에의 첫번째 로드에는 그런
12518c2ecf20Sopenharmony_ci보장이 없습니다; A == 0 이거나 A == 1 이거나 둘 중 하나의 결과를 보게 될겁니다.
12528c2ecf20Sopenharmony_ci
12538c2ecf20Sopenharmony_ci
12548c2ecf20Sopenharmony_ci읽기 메모리 배리어 VS 로드 예측
12558c2ecf20Sopenharmony_ci-------------------------------
12568c2ecf20Sopenharmony_ci
12578c2ecf20Sopenharmony_ci많은 CPU들이 로드를 예측적으로 (speculatively) 합니다: 어떤 데이터를 메모리에서
12588c2ecf20Sopenharmony_ci로드해야 하게 될지 예측을 했다면, 해당 데이터를 로드하는 인스트럭션을 실제로는
12598c2ecf20Sopenharmony_ci아직 만나지 않았더라도 다른 로드 작업이 없어 버스 (bus) 가 아무 일도 하고 있지
12608c2ecf20Sopenharmony_ci않다면, 그 데이터를 로드합니다.  이후에 실제 로드 인스트럭션이 실행되면 CPU 가
12618c2ecf20Sopenharmony_ci이미 그 값을 가지고 있기 때문에 그 로드 인스트럭션은 즉시 완료됩니다.
12628c2ecf20Sopenharmony_ci
12638c2ecf20Sopenharmony_ci해당 CPU 는 실제로는 그 값이 필요치 않았다는 사실이 나중에 드러날 수도 있는데 -
12648c2ecf20Sopenharmony_ci해당 로드 인스트럭션이 브랜치로 우회되거나 했을 수 있겠죠 - , 그렇게 되면 앞서
12658c2ecf20Sopenharmony_ci읽어둔 값을 버리거나 나중의 사용을 위해 캐시에 넣어둘 수 있습니다.
12668c2ecf20Sopenharmony_ci
12678c2ecf20Sopenharmony_ci다음을 생각해 봅시다:
12688c2ecf20Sopenharmony_ci
12698c2ecf20Sopenharmony_ci	CPU 1			CPU 2
12708c2ecf20Sopenharmony_ci	=======================	=======================
12718c2ecf20Sopenharmony_ci				LOAD B
12728c2ecf20Sopenharmony_ci				DIVIDE		} 나누기 명령은 일반적으로
12738c2ecf20Sopenharmony_ci				DIVIDE		} 긴 시간을 필요로 합니다
12748c2ecf20Sopenharmony_ci				LOAD A
12758c2ecf20Sopenharmony_ci
12768c2ecf20Sopenharmony_ci는 이렇게 될 수 있습니다:
12778c2ecf20Sopenharmony_ci
12788c2ecf20Sopenharmony_ci	                                        :       :       +-------+
12798c2ecf20Sopenharmony_ci	                                        +-------+       |       |
12808c2ecf20Sopenharmony_ci	                                    --->| B->2  |------>|       |
12818c2ecf20Sopenharmony_ci	                                        +-------+       | CPU 2 |
12828c2ecf20Sopenharmony_ci	                                        :       :DIVIDE |       |
12838c2ecf20Sopenharmony_ci	                                        +-------+       |       |
12848c2ecf20Sopenharmony_ci	나누기 하느라 바쁜       --->       --->| A->0  |~~~~   |       |
12858c2ecf20Sopenharmony_ci	CPU 는 A 의 LOAD 를                     +-------+   ~   |       |
12868c2ecf20Sopenharmony_ci	예측해서 수행한다                       :       :   ~   |       |
12878c2ecf20Sopenharmony_ci	                                        :       :DIVIDE |       |
12888c2ecf20Sopenharmony_ci	                                        :       :   ~   |       |
12898c2ecf20Sopenharmony_ci	나누기가 끝나면       --->     --->     :       :   ~-->|       |
12908c2ecf20Sopenharmony_ci	CPU 는 해당 LOAD 를                     :       :       |       |
12918c2ecf20Sopenharmony_ci	즉각 완료한다                           :       :       +-------+
12928c2ecf20Sopenharmony_ci
12938c2ecf20Sopenharmony_ci
12948c2ecf20Sopenharmony_ci읽기 배리어나 데이터 의존성 배리어를 두번째 로드 직전에 놓는다면:
12958c2ecf20Sopenharmony_ci
12968c2ecf20Sopenharmony_ci	CPU 1			CPU 2
12978c2ecf20Sopenharmony_ci	=======================	=======================
12988c2ecf20Sopenharmony_ci				LOAD B
12998c2ecf20Sopenharmony_ci				DIVIDE
13008c2ecf20Sopenharmony_ci				DIVIDE
13018c2ecf20Sopenharmony_ci				<읽기 배리어>
13028c2ecf20Sopenharmony_ci				LOAD A
13038c2ecf20Sopenharmony_ci
13048c2ecf20Sopenharmony_ci예측으로 얻어진 값은 사용된 배리어의 타입에 따라서 해당 값이 옳은지 검토되게
13058c2ecf20Sopenharmony_ci됩니다.  만약 해당 메모리 영역에 변화가 없었다면, 예측으로 얻어두었던 값이
13068c2ecf20Sopenharmony_ci사용됩니다:
13078c2ecf20Sopenharmony_ci
13088c2ecf20Sopenharmony_ci	                                        :       :       +-------+
13098c2ecf20Sopenharmony_ci	                                        +-------+       |       |
13108c2ecf20Sopenharmony_ci	                                    --->| B->2  |------>|       |
13118c2ecf20Sopenharmony_ci	                                        +-------+       | CPU 2 |
13128c2ecf20Sopenharmony_ci	                                        :       :DIVIDE |       |
13138c2ecf20Sopenharmony_ci	                                        +-------+       |       |
13148c2ecf20Sopenharmony_ci	나누기 하느라 바쁜       --->       --->| A->0  |~~~~   |       |
13158c2ecf20Sopenharmony_ci	CPU 는 A 의 LOAD 를                     +-------+   ~   |       |
13168c2ecf20Sopenharmony_ci	예측한다                                :       :   ~   |       |
13178c2ecf20Sopenharmony_ci	                                        :       :DIVIDE |       |
13188c2ecf20Sopenharmony_ci	                                        :       :   ~   |       |
13198c2ecf20Sopenharmony_ci	                                        :       :   ~   |       |
13208c2ecf20Sopenharmony_ci	                                    rrrrrrrrrrrrrrrr~   |       |
13218c2ecf20Sopenharmony_ci	                                        :       :   ~   |       |
13228c2ecf20Sopenharmony_ci	                                        :       :   ~-->|       |
13238c2ecf20Sopenharmony_ci	                                        :       :       |       |
13248c2ecf20Sopenharmony_ci	                                        :       :       +-------+
13258c2ecf20Sopenharmony_ci
13268c2ecf20Sopenharmony_ci
13278c2ecf20Sopenharmony_ci하지만 다른 CPU 에서 업데이트나 무효화가 있었다면, 그 예측은 무효화되고 그 값은
13288c2ecf20Sopenharmony_ci다시 읽혀집니다:
13298c2ecf20Sopenharmony_ci
13308c2ecf20Sopenharmony_ci	                                        :       :       +-------+
13318c2ecf20Sopenharmony_ci	                                        +-------+       |       |
13328c2ecf20Sopenharmony_ci	                                    --->| B->2  |------>|       |
13338c2ecf20Sopenharmony_ci	                                        +-------+       | CPU 2 |
13348c2ecf20Sopenharmony_ci	                                        :       :DIVIDE |       |
13358c2ecf20Sopenharmony_ci	                                        +-------+       |       |
13368c2ecf20Sopenharmony_ci	나누기 하느라 바쁜       --->       --->| A->0  |~~~~   |       |
13378c2ecf20Sopenharmony_ci	CPU 는 A 의 LOAD 를                     +-------+   ~   |       |
13388c2ecf20Sopenharmony_ci	예측한다                                :       :   ~   |       |
13398c2ecf20Sopenharmony_ci	                                        :       :DIVIDE |       |
13408c2ecf20Sopenharmony_ci	                                        :       :   ~   |       |
13418c2ecf20Sopenharmony_ci	                                        :       :   ~   |       |
13428c2ecf20Sopenharmony_ci	                                    rrrrrrrrrrrrrrrrr   |       |
13438c2ecf20Sopenharmony_ci	                                        +-------+       |       |
13448c2ecf20Sopenharmony_ci	예측성 동작은 무효화 되고    --->   --->| A->1  |------>|       |
13458c2ecf20Sopenharmony_ci	업데이트된 값이 다시 읽혀진다           +-------+       |       |
13468c2ecf20Sopenharmony_ci	                                        :       :       +-------+
13478c2ecf20Sopenharmony_ci
13488c2ecf20Sopenharmony_ci
13498c2ecf20Sopenharmony_ciMULTICOPY 원자성
13508c2ecf20Sopenharmony_ci----------------
13518c2ecf20Sopenharmony_ci
13528c2ecf20Sopenharmony_ciMulticopy 원자성은 실제의 컴퓨터 시스템에서 항상 제공되지는 않는, 순서 맞추기에
13538c2ecf20Sopenharmony_ci대한 상당히 직관적인 개념으로, 특정 스토어가 모든 CPU 들에게 동시에 보여지게
13548c2ecf20Sopenharmony_ci됨을, 달리 말하자면 모든 CPU 들이 모든 스토어들이 보여지는 순서를 동의하게 되는
13558c2ecf20Sopenharmony_ci것입니다.  하지만, 완전한 multicopy 원자성의 사용은 가치있는 하드웨어
13568c2ecf20Sopenharmony_ci최적화들을 무능하게 만들어버릴 수 있어서, 보다 완화된 형태의 ``다른 multicopy
13578c2ecf20Sopenharmony_ci원자성'' 라는 이름의, 특정 스토어가 모든 -다른- CPU 들에게는 동시에 보여지게
13588c2ecf20Sopenharmony_ci하는 보장을 대신 제공합니다.  이 문서의 뒷부분들은 이 완화된 형태에 대해 논하게
13598c2ecf20Sopenharmony_ci됩니다만, 단순히 ``multicopy 원자성'' 이라고 부르겠습니다.
13608c2ecf20Sopenharmony_ci
13618c2ecf20Sopenharmony_ci다음의 예가 multicopy 원자성을 보입니다:
13628c2ecf20Sopenharmony_ci
13638c2ecf20Sopenharmony_ci	CPU 1			CPU 2			CPU 3
13648c2ecf20Sopenharmony_ci	=======================	=======================	=======================
13658c2ecf20Sopenharmony_ci		{ X = 0, Y = 0 }
13668c2ecf20Sopenharmony_ci	STORE X=1		r1=LOAD X (reads 1)	LOAD Y (reads 1)
13678c2ecf20Sopenharmony_ci				<범용 배리어>		<읽기 배리어>
13688c2ecf20Sopenharmony_ci				STORE Y=r1		LOAD X
13698c2ecf20Sopenharmony_ci
13708c2ecf20Sopenharmony_ciCPU 2 의 Y 로의 스토어에 사용되는 X 로드의 결과가 1 이었고 CPU 3 의 Y 로드가
13718c2ecf20Sopenharmony_ci1을 리턴했다고 해봅시다.  이는 CPU 1 의 X 로의 스토어가 CPU 2 의 X 로부터의
13728c2ecf20Sopenharmony_ci로드를 앞서고 CPU 2 의 Y 로의 스토어가 CPU 3 의 Y 로부터의 로드를 앞섬을
13738c2ecf20Sopenharmony_ci의미합니다.  또한, 여기서의 메모리 배리어들은 CPU 2 가 자신의 로드를 자신의
13748c2ecf20Sopenharmony_ci스토어 전에 수행하고, CPU 3 가 Y 로부터의 로드를 X 로부터의 로드 전에 수행함을
13758c2ecf20Sopenharmony_ci보장합니다.  그럼 "CPU 3 의 X 로부터의 로드는 0 을 리턴할 수 있을까요?"
13768c2ecf20Sopenharmony_ci
13778c2ecf20Sopenharmony_ciCPU 3 의 X 로드가 CPU 2 의 로드보다 뒤에 이루어졌으므로, CPU 3 의 X 로부터의
13788c2ecf20Sopenharmony_ci로드는 1 을 리턴한다고 예상하는게 당연합니다.  이런 예상은 multicopy
13798c2ecf20Sopenharmony_ci원자성으로부터 나옵니다: CPU B 에서 수행된 로드가 CPU A 의 같은 변수로부터의
13808c2ecf20Sopenharmony_ci로드를 뒤따른다면 (그리고 CPU A 가 자신이 읽은 값으로 먼저 해당 변수에 스토어
13818c2ecf20Sopenharmony_ci하지 않았다면) multicopy 원자성을 제공하는 시스템에서는, CPU B 의 로드가 CPU A
13828c2ecf20Sopenharmony_ci의 로드와 같은 값 또는 그 나중 값을 리턴해야만 합니다.  하지만, 리눅스 커널은
13838c2ecf20Sopenharmony_ci시스템들이 multicopy 원자성을 제공할 것을 요구하지 않습니다.
13848c2ecf20Sopenharmony_ci
13858c2ecf20Sopenharmony_ci앞의 범용 메모리 배리어의 사용은 모든 multicopy 원자성의 부족을 보상해줍니다.
13868c2ecf20Sopenharmony_ci앞의 예에서, CPU 2 의 X 로부터의 로드가 1 을 리턴했고 CPU 3 의 Y 로부터의
13878c2ecf20Sopenharmony_ci로드가 1 을 리턴했다면, CPU 3 의 X 로부터의 로드는 1을 리턴해야만 합니다.
13888c2ecf20Sopenharmony_ci
13898c2ecf20Sopenharmony_ci하지만, 의존성, 읽기 배리어, 쓰기 배리어는 항상 non-multicopy 원자성을 보상해
13908c2ecf20Sopenharmony_ci주지는 않습니다.  예를 들어, CPU 2 의 범용 배리어가 앞의 예에서 사라져서
13918c2ecf20Sopenharmony_ci아래처럼 데이터 의존성만 남게 되었다고 해봅시다:
13928c2ecf20Sopenharmony_ci
13938c2ecf20Sopenharmony_ci	CPU 1			CPU 2			CPU 3
13948c2ecf20Sopenharmony_ci	=======================	=======================	=======================
13958c2ecf20Sopenharmony_ci		{ X = 0, Y = 0 }
13968c2ecf20Sopenharmony_ci	STORE X=1		r1=LOAD X (reads 1)	LOAD Y (reads 1)
13978c2ecf20Sopenharmony_ci				<데이터 의존성>		<읽기 배리어>
13988c2ecf20Sopenharmony_ci				STORE Y=r1		LOAD X (reads 0)
13998c2ecf20Sopenharmony_ci
14008c2ecf20Sopenharmony_ci이 변화는 non-multicopy 원자성이 만연하게 합니다: 이 예에서, CPU 2 의 X
14018c2ecf20Sopenharmony_ci로부터의 로드가 1을 리턴하고, CPU 3 의 Y 로부터의 로드가 1 을 리턴하는데, CPU 3
14028c2ecf20Sopenharmony_ci의 X 로부터의 로드가 0 을 리턴하는게 완전히 합법적입니다.
14038c2ecf20Sopenharmony_ci
14048c2ecf20Sopenharmony_ci핵심은, CPU 2 의 데이터 의존성이 자신의 로드와 스토어를 순서짓지만, CPU 1 의
14058c2ecf20Sopenharmony_ci스토어에 대한 순서는 보장하지 않는다는 것입니다.  따라서, 이 예제가 CPU 1 과
14068c2ecf20Sopenharmony_ciCPU 2 가 스토어 버퍼나 한 수준의 캐시를 공유하는, multicopy 원자성을 제공하지
14078c2ecf20Sopenharmony_ci않는 시스템에서 수행된다면 CPU 2 는 CPU 1 의 쓰기에 이른 접근을 할 수도
14088c2ecf20Sopenharmony_ci있습니다.  따라서, 모든 CPU 들이 여러 접근들의 조합된 순서에 대해서 동의하게
14098c2ecf20Sopenharmony_ci하기 위해서는 범용 배리어가 필요합니다.
14108c2ecf20Sopenharmony_ci
14118c2ecf20Sopenharmony_ci범용 배리어는 non-multicopy 원자성만 보상할 수 있는게 아니라, -모든- CPU 들이
14128c2ecf20Sopenharmony_ci-모든- 오퍼레이션들의 순서를 동일하게 인식하게 하는 추가적인 순서 보장을
14138c2ecf20Sopenharmony_ci만들어냅니다.  반대로, release-acquire 짝의 연결은 이런 추가적인 순서는
14148c2ecf20Sopenharmony_ci제공하지 않는데, 해당 연결에 들어있는 CPU 들만이 메모리 접근의 조합된 순서에
14158c2ecf20Sopenharmony_ci대해 동의할 것으로 보장됨을 의미합니다.  예를 들어, 존경스런 Herman Hollerith
14168c2ecf20Sopenharmony_ci의 코드를 C 코드로 변환하면:
14178c2ecf20Sopenharmony_ci
14188c2ecf20Sopenharmony_ci	int u, v, x, y, z;
14198c2ecf20Sopenharmony_ci
14208c2ecf20Sopenharmony_ci	void cpu0(void)
14218c2ecf20Sopenharmony_ci	{
14228c2ecf20Sopenharmony_ci		r0 = smp_load_acquire(&x);
14238c2ecf20Sopenharmony_ci		WRITE_ONCE(u, 1);
14248c2ecf20Sopenharmony_ci		smp_store_release(&y, 1);
14258c2ecf20Sopenharmony_ci	}
14268c2ecf20Sopenharmony_ci
14278c2ecf20Sopenharmony_ci	void cpu1(void)
14288c2ecf20Sopenharmony_ci	{
14298c2ecf20Sopenharmony_ci		r1 = smp_load_acquire(&y);
14308c2ecf20Sopenharmony_ci		r4 = READ_ONCE(v);
14318c2ecf20Sopenharmony_ci		r5 = READ_ONCE(u);
14328c2ecf20Sopenharmony_ci		smp_store_release(&z, 1);
14338c2ecf20Sopenharmony_ci	}
14348c2ecf20Sopenharmony_ci
14358c2ecf20Sopenharmony_ci	void cpu2(void)
14368c2ecf20Sopenharmony_ci	{
14378c2ecf20Sopenharmony_ci		r2 = smp_load_acquire(&z);
14388c2ecf20Sopenharmony_ci		smp_store_release(&x, 1);
14398c2ecf20Sopenharmony_ci	}
14408c2ecf20Sopenharmony_ci
14418c2ecf20Sopenharmony_ci	void cpu3(void)
14428c2ecf20Sopenharmony_ci	{
14438c2ecf20Sopenharmony_ci		WRITE_ONCE(v, 1);
14448c2ecf20Sopenharmony_ci		smp_mb();
14458c2ecf20Sopenharmony_ci		r3 = READ_ONCE(u);
14468c2ecf20Sopenharmony_ci	}
14478c2ecf20Sopenharmony_ci
14488c2ecf20Sopenharmony_cicpu0(), cpu1(), 그리고 cpu2() 는 smp_store_release()/smp_load_acquire() 쌍의
14498c2ecf20Sopenharmony_ci연결에 참여되어 있으므로, 다음과 같은 결과는 나오지 않을 겁니다:
14508c2ecf20Sopenharmony_ci
14518c2ecf20Sopenharmony_ci	r0 == 1 && r1 == 1 && r2 == 1
14528c2ecf20Sopenharmony_ci
14538c2ecf20Sopenharmony_ci더 나아가서, cpu0() 와 cpu1() 사이의 release-acquire 관계로 인해, cpu1() 은
14548c2ecf20Sopenharmony_cicpu0() 의 쓰기를 봐야만 하므로, 다음과 같은 결과도 없을 겁니다:
14558c2ecf20Sopenharmony_ci
14568c2ecf20Sopenharmony_ci	r1 == 1 && r5 == 0
14578c2ecf20Sopenharmony_ci
14588c2ecf20Sopenharmony_ci하지만, release-acquire 에 의해 제공되는 순서는 해당 연결에 동참한 CPU 들에만
14598c2ecf20Sopenharmony_ci적용되므로 cpu3() 에, 적어도 스토어들 외에는 적용되지 않습니다.  따라서, 다음과
14608c2ecf20Sopenharmony_ci같은 결과가 가능합니다:
14618c2ecf20Sopenharmony_ci
14628c2ecf20Sopenharmony_ci	r0 == 0 && r1 == 1 && r2 == 1 && r3 == 0 && r4 == 0
14638c2ecf20Sopenharmony_ci
14648c2ecf20Sopenharmony_ci비슷하게, 다음과 같은 결과도 가능합니다:
14658c2ecf20Sopenharmony_ci
14668c2ecf20Sopenharmony_ci	r0 == 0 && r1 == 1 && r2 == 1 && r3 == 0 && r4 == 0 && r5 == 1
14678c2ecf20Sopenharmony_ci
14688c2ecf20Sopenharmony_cicpu0(), cpu1(), 그리고 cpu2() 는 그들의 읽기와 쓰기를 순서대로 보게 되지만,
14698c2ecf20Sopenharmony_cirelease-acquire 체인에 관여되지 않은 CPU 들은 그 순서에 이견을 가질 수
14708c2ecf20Sopenharmony_ci있습니다.  이런 이견은 smp_load_acquire() 와 smp_store_release() 의 구현에
14718c2ecf20Sopenharmony_ci사용되는 완화된 메모리 배리어 인스트럭션들은 항상 배리어 앞의 스토어들을 뒤의
14728c2ecf20Sopenharmony_ci로드들에 앞세울 필요는 없다는 사실에서 기인합니다.  이 말은 cpu3() 는 cpu0() 의
14738c2ecf20Sopenharmony_ciu 로의 스토어를 cpu1() 의 v 로부터의 로드 뒤에 일어난 것으로 볼 수 있다는
14748c2ecf20Sopenharmony_ci뜻입니다, cpu0() 와 cpu1() 은 이 두 오퍼레이션이 의도된 순서대로 일어났음에
14758c2ecf20Sopenharmony_ci모두 동의하는데도 말입니다.
14768c2ecf20Sopenharmony_ci
14778c2ecf20Sopenharmony_ci하지만, smp_load_acquire() 는 마술이 아님을 명심하시기 바랍니다.  구체적으로,
14788c2ecf20Sopenharmony_ci이 함수는 단순히 순서 규칙을 지키며 인자로부터의 읽기를 수행합니다.  이것은
14798c2ecf20Sopenharmony_ci어떤 특정한 값이 읽힐 것인지는 보장하지 -않습니다-.  따라서, 다음과 같은 결과도
14808c2ecf20Sopenharmony_ci가능합니다:
14818c2ecf20Sopenharmony_ci
14828c2ecf20Sopenharmony_ci	r0 == 0 && r1 == 0 && r2 == 0 && r5 == 0
14838c2ecf20Sopenharmony_ci
14848c2ecf20Sopenharmony_ci이런 결과는 어떤 것도 재배치 되지 않는, 순차적 일관성을 가진 가상의
14858c2ecf20Sopenharmony_ci시스템에서도 일어날 수 있음을 기억해 두시기 바랍니다.
14868c2ecf20Sopenharmony_ci
14878c2ecf20Sopenharmony_ci다시 말하지만, 당신의 코드가 모든 오퍼레이션들의 완전한 순서를 필요로 한다면,
14888c2ecf20Sopenharmony_ci범용 배리어를 사용하십시오.
14898c2ecf20Sopenharmony_ci
14908c2ecf20Sopenharmony_ci
14918c2ecf20Sopenharmony_ci==================
14928c2ecf20Sopenharmony_ci명시적 커널 배리어
14938c2ecf20Sopenharmony_ci==================
14948c2ecf20Sopenharmony_ci
14958c2ecf20Sopenharmony_ci리눅스 커널은 서로 다른 단계에서 동작하는 다양한 배리어들을 가지고 있습니다:
14968c2ecf20Sopenharmony_ci
14978c2ecf20Sopenharmony_ci  (*) 컴파일러 배리어.
14988c2ecf20Sopenharmony_ci
14998c2ecf20Sopenharmony_ci  (*) CPU 메모리 배리어.
15008c2ecf20Sopenharmony_ci
15018c2ecf20Sopenharmony_ci
15028c2ecf20Sopenharmony_ci컴파일러 배리어
15038c2ecf20Sopenharmony_ci---------------
15048c2ecf20Sopenharmony_ci
15058c2ecf20Sopenharmony_ci리눅스 커널은 컴파일러가 메모리 액세스를 재배치 하는 것을 막아주는 명시적인
15068c2ecf20Sopenharmony_ci컴파일러 배리어를 가지고 있습니다:
15078c2ecf20Sopenharmony_ci
15088c2ecf20Sopenharmony_ci	barrier();
15098c2ecf20Sopenharmony_ci
15108c2ecf20Sopenharmony_ci이건 범용 배리어입니다 -- barrier() 의 읽기-읽기 나 쓰기-쓰기 변종은 없습니다.
15118c2ecf20Sopenharmony_ci하지만, READ_ONCE() 와 WRITE_ONCE() 는 특정 액세스들에 대해서만 동작하는
15128c2ecf20Sopenharmony_cibarrier() 의 완화된 형태로 볼 수 있습니다.
15138c2ecf20Sopenharmony_ci
15148c2ecf20Sopenharmony_cibarrier() 함수는 다음과 같은 효과를 갖습니다:
15158c2ecf20Sopenharmony_ci
15168c2ecf20Sopenharmony_ci (*) 컴파일러가 barrier() 뒤의 액세스들이 barrier() 앞의 액세스보다 앞으로
15178c2ecf20Sopenharmony_ci     재배치되지 못하게 합니다.  예를 들어, 인터럽트 핸들러 코드와 인터럽트 당한
15188c2ecf20Sopenharmony_ci     코드 사이의 통신을 신중히 하기 위해 사용될 수 있습니다.
15198c2ecf20Sopenharmony_ci
15208c2ecf20Sopenharmony_ci (*) 루프에서, 컴파일러가 루프 조건에 사용된 변수를 매 이터레이션마다
15218c2ecf20Sopenharmony_ci     메모리에서 로드하지 않아도 되도록 최적화 하는걸 방지합니다.
15228c2ecf20Sopenharmony_ci
15238c2ecf20Sopenharmony_ciREAD_ONCE() 와 WRITE_ONCE() 함수는 싱글 쓰레드 코드에서는 문제 없지만 동시성이
15248c2ecf20Sopenharmony_ci있는 코드에서는 문제가 될 수 있는 모든 최적화를 막습니다.  이런 류의 최적화에
15258c2ecf20Sopenharmony_ci대한 예를 몇가지 들어보면 다음과 같습니다:
15268c2ecf20Sopenharmony_ci
15278c2ecf20Sopenharmony_ci (*) 컴파일러는 같은 변수에 대한 로드와 스토어를 재배치 할 수 있고, 어떤
15288c2ecf20Sopenharmony_ci     경우에는 CPU가 같은 변수로부터의 로드들을 재배치할 수도 있습니다.  이는
15298c2ecf20Sopenharmony_ci     다음의 코드가:
15308c2ecf20Sopenharmony_ci
15318c2ecf20Sopenharmony_ci	a[0] = x;
15328c2ecf20Sopenharmony_ci	a[1] = x;
15338c2ecf20Sopenharmony_ci
15348c2ecf20Sopenharmony_ci     x 의 예전 값이 a[1] 에, 새 값이 a[0] 에 있게 할 수 있다는 뜻입니다.
15358c2ecf20Sopenharmony_ci     컴파일러와 CPU가 이런 일을 못하게 하려면 다음과 같이 해야 합니다:
15368c2ecf20Sopenharmony_ci
15378c2ecf20Sopenharmony_ci	a[0] = READ_ONCE(x);
15388c2ecf20Sopenharmony_ci	a[1] = READ_ONCE(x);
15398c2ecf20Sopenharmony_ci
15408c2ecf20Sopenharmony_ci     즉, READ_ONCE() 와 WRITE_ONCE() 는 여러 CPU 에서 하나의 변수에 가해지는
15418c2ecf20Sopenharmony_ci     액세스들에 캐시 일관성을 제공합니다.
15428c2ecf20Sopenharmony_ci
15438c2ecf20Sopenharmony_ci (*) 컴파일러는 같은 변수에 대한 연속적인 로드들을 병합할 수 있습니다.  그런
15448c2ecf20Sopenharmony_ci     병합 작업으로 컴파일러는 다음의 코드를:
15458c2ecf20Sopenharmony_ci
15468c2ecf20Sopenharmony_ci	while (tmp = a)
15478c2ecf20Sopenharmony_ci		do_something_with(tmp);
15488c2ecf20Sopenharmony_ci
15498c2ecf20Sopenharmony_ci     다음과 같이, 싱글 쓰레드 코드에서는 말이 되지만 개발자의 의도와 전혀 맞지
15508c2ecf20Sopenharmony_ci     않는 방향으로 "최적화" 할 수 있습니다:
15518c2ecf20Sopenharmony_ci
15528c2ecf20Sopenharmony_ci	if (tmp = a)
15538c2ecf20Sopenharmony_ci		for (;;)
15548c2ecf20Sopenharmony_ci			do_something_with(tmp);
15558c2ecf20Sopenharmony_ci
15568c2ecf20Sopenharmony_ci     컴파일러가 이런 짓을 하지 못하게 하려면 READ_ONCE() 를 사용하세요:
15578c2ecf20Sopenharmony_ci
15588c2ecf20Sopenharmony_ci	while (tmp = READ_ONCE(a))
15598c2ecf20Sopenharmony_ci		do_something_with(tmp);
15608c2ecf20Sopenharmony_ci
15618c2ecf20Sopenharmony_ci (*) 예컨대 레지스터 사용량이 많아 컴파일러가 모든 데이터를 레지스터에 담을 수
15628c2ecf20Sopenharmony_ci     없는 경우, 컴파일러는 변수를 다시 로드할 수 있습니다.  따라서 컴파일러는
15638c2ecf20Sopenharmony_ci     앞의 예에서 변수 'tmp' 사용을 최적화로 없애버릴 수 있습니다:
15648c2ecf20Sopenharmony_ci
15658c2ecf20Sopenharmony_ci	while (tmp = a)
15668c2ecf20Sopenharmony_ci		do_something_with(tmp);
15678c2ecf20Sopenharmony_ci
15688c2ecf20Sopenharmony_ci     이 코드는 다음과 같이 싱글 쓰레드에서는 완벽하지만 동시성이 존재하는
15698c2ecf20Sopenharmony_ci     경우엔 치명적인 코드로 바뀔 수 있습니다:
15708c2ecf20Sopenharmony_ci
15718c2ecf20Sopenharmony_ci	while (a)
15728c2ecf20Sopenharmony_ci		do_something_with(a);
15738c2ecf20Sopenharmony_ci
15748c2ecf20Sopenharmony_ci     예를 들어, 최적화된 이 코드는 변수 a 가 다른 CPU 에 의해 "while" 문과
15758c2ecf20Sopenharmony_ci     do_something_with() 호출 사이에 바뀌어 do_something_with() 에 0을 넘길
15768c2ecf20Sopenharmony_ci     수도 있습니다.
15778c2ecf20Sopenharmony_ci
15788c2ecf20Sopenharmony_ci     이번에도, 컴파일러가 그런 짓을 하는걸 막기 위해 READ_ONCE() 를 사용하세요:
15798c2ecf20Sopenharmony_ci
15808c2ecf20Sopenharmony_ci	while (tmp = READ_ONCE(a))
15818c2ecf20Sopenharmony_ci		do_something_with(tmp);
15828c2ecf20Sopenharmony_ci
15838c2ecf20Sopenharmony_ci     레지스터가 부족한 상황을 겪는 경우, 컴파일러는 tmp 를 스택에 저장해둘 수도
15848c2ecf20Sopenharmony_ci     있습니다.  컴파일러가 변수를 다시 읽어들이는건 이렇게 저장해두고 후에 다시
15858c2ecf20Sopenharmony_ci     읽어들이는데 드는 오버헤드 때문입니다.  그렇게 하는게 싱글 쓰레드
15868c2ecf20Sopenharmony_ci     코드에서는 안전하므로, 안전하지 않은 경우에는 컴파일러에게 직접 알려줘야
15878c2ecf20Sopenharmony_ci     합니다.
15888c2ecf20Sopenharmony_ci
15898c2ecf20Sopenharmony_ci (*) 컴파일러는 그 값이 무엇일지 알고 있다면 로드를 아예 안할 수도 있습니다.
15908c2ecf20Sopenharmony_ci     예를 들어, 다음의 코드는 변수 'a' 의 값이 항상 0임을 증명할 수 있다면:
15918c2ecf20Sopenharmony_ci
15928c2ecf20Sopenharmony_ci	while (tmp = a)
15938c2ecf20Sopenharmony_ci		do_something_with(tmp);
15948c2ecf20Sopenharmony_ci
15958c2ecf20Sopenharmony_ci     이렇게 최적화 되어버릴 수 있습니다:
15968c2ecf20Sopenharmony_ci
15978c2ecf20Sopenharmony_ci	do { } while (0);
15988c2ecf20Sopenharmony_ci
15998c2ecf20Sopenharmony_ci     이 변환은 싱글 쓰레드 코드에서는 도움이 되는데 로드와 브랜치를 제거했기
16008c2ecf20Sopenharmony_ci     때문입니다.  문제는 컴파일러가 'a' 의 값을 업데이트 하는건 현재의 CPU 하나
16018c2ecf20Sopenharmony_ci     뿐이라는 가정 위에서 증명을 했다는데 있습니다.  만약 변수 'a' 가 공유되어
16028c2ecf20Sopenharmony_ci     있다면, 컴파일러의 증명은 틀린 것이 될겁니다.  컴파일러는 그 자신이
16038c2ecf20Sopenharmony_ci     생각하는 것만큼 많은 것을 알고 있지 못함을 컴파일러에게 알리기 위해
16048c2ecf20Sopenharmony_ci     READ_ONCE() 를 사용하세요:
16058c2ecf20Sopenharmony_ci
16068c2ecf20Sopenharmony_ci	while (tmp = READ_ONCE(a))
16078c2ecf20Sopenharmony_ci		do_something_with(tmp);
16088c2ecf20Sopenharmony_ci
16098c2ecf20Sopenharmony_ci     하지만 컴파일러는 READ_ONCE() 뒤에 나오는 값에 대해서도 눈길을 두고 있음을
16108c2ecf20Sopenharmony_ci     기억하세요.  예를 들어, 다음의 코드에서 MAX 는 전처리기 매크로로, 1의 값을
16118c2ecf20Sopenharmony_ci     갖는다고 해봅시다:
16128c2ecf20Sopenharmony_ci
16138c2ecf20Sopenharmony_ci	while ((tmp = READ_ONCE(a)) % MAX)
16148c2ecf20Sopenharmony_ci		do_something_with(tmp);
16158c2ecf20Sopenharmony_ci
16168c2ecf20Sopenharmony_ci     이렇게 되면 컴파일러는 MAX 를 가지고 수행되는 "%" 오퍼레이터의 결과가 항상
16178c2ecf20Sopenharmony_ci     0이라는 것을 알게 되고, 컴파일러가 코드를 실질적으로는 존재하지 않는
16188c2ecf20Sopenharmony_ci     것처럼 최적화 하는 것이 허용되어 버립니다.  ('a' 변수의 로드는 여전히
16198c2ecf20Sopenharmony_ci     행해질 겁니다.)
16208c2ecf20Sopenharmony_ci
16218c2ecf20Sopenharmony_ci (*) 비슷하게, 컴파일러는 변수가 저장하려 하는 값을 이미 가지고 있다는 것을
16228c2ecf20Sopenharmony_ci     알면 스토어 자체를 제거할 수 있습니다.  이번에도, 컴파일러는 현재의 CPU
16238c2ecf20Sopenharmony_ci     만이 그 변수에 값을 쓰는 오로지 하나의 존재라고 생각하여 공유된 변수에
16248c2ecf20Sopenharmony_ci     대해서는 잘못된 일을 하게 됩니다.  예를 들어, 다음과 같은 경우가 있을 수
16258c2ecf20Sopenharmony_ci     있습니다:
16268c2ecf20Sopenharmony_ci
16278c2ecf20Sopenharmony_ci	a = 0;
16288c2ecf20Sopenharmony_ci	... 변수 a 에 스토어를 하지 않는 코드 ...
16298c2ecf20Sopenharmony_ci	a = 0;
16308c2ecf20Sopenharmony_ci
16318c2ecf20Sopenharmony_ci     컴파일러는 변수 'a' 의 값은 이미 0이라는 것을 알고, 따라서 두번째 스토어를
16328c2ecf20Sopenharmony_ci     삭제할 겁니다.  만약 다른 CPU 가 그 사이 변수 'a' 에 다른 값을 썼다면
16338c2ecf20Sopenharmony_ci     황당한 결과가 나올 겁니다.
16348c2ecf20Sopenharmony_ci
16358c2ecf20Sopenharmony_ci     컴파일러가 그런 잘못된 추측을 하지 않도록 WRITE_ONCE() 를 사용하세요:
16368c2ecf20Sopenharmony_ci
16378c2ecf20Sopenharmony_ci	WRITE_ONCE(a, 0);
16388c2ecf20Sopenharmony_ci	... 변수 a 에 스토어를 하지 않는 코드 ...
16398c2ecf20Sopenharmony_ci	WRITE_ONCE(a, 0);
16408c2ecf20Sopenharmony_ci
16418c2ecf20Sopenharmony_ci (*) 컴파일러는 하지 말라고 하지 않으면 메모리 액세스들을 재배치 할 수
16428c2ecf20Sopenharmony_ci     있습니다.  예를 들어, 다음의 프로세스 레벨 코드와 인터럽트 핸들러 사이의
16438c2ecf20Sopenharmony_ci     상호작용을 생각해 봅시다:
16448c2ecf20Sopenharmony_ci
16458c2ecf20Sopenharmony_ci	void process_level(void)
16468c2ecf20Sopenharmony_ci	{
16478c2ecf20Sopenharmony_ci		msg = get_message();
16488c2ecf20Sopenharmony_ci		flag = true;
16498c2ecf20Sopenharmony_ci	}
16508c2ecf20Sopenharmony_ci
16518c2ecf20Sopenharmony_ci	void interrupt_handler(void)
16528c2ecf20Sopenharmony_ci	{
16538c2ecf20Sopenharmony_ci		if (flag)
16548c2ecf20Sopenharmony_ci			process_message(msg);
16558c2ecf20Sopenharmony_ci	}
16568c2ecf20Sopenharmony_ci
16578c2ecf20Sopenharmony_ci     이 코드에는 컴파일러가 process_level() 을 다음과 같이 변환하는 것을 막을
16588c2ecf20Sopenharmony_ci     수단이 없고, 이런 변환은 싱글쓰레드에서라면 실제로 훌륭한 선택일 수
16598c2ecf20Sopenharmony_ci     있습니다:
16608c2ecf20Sopenharmony_ci
16618c2ecf20Sopenharmony_ci	void process_level(void)
16628c2ecf20Sopenharmony_ci	{
16638c2ecf20Sopenharmony_ci		flag = true;
16648c2ecf20Sopenharmony_ci		msg = get_message();
16658c2ecf20Sopenharmony_ci	}
16668c2ecf20Sopenharmony_ci
16678c2ecf20Sopenharmony_ci     이 두개의 문장 사이에 인터럽트가 발생한다면, interrupt_handler() 는 의미를
16688c2ecf20Sopenharmony_ci     알 수 없는 메세지를 받을 수도 있습니다.  이걸 막기 위해 다음과 같이
16698c2ecf20Sopenharmony_ci     WRITE_ONCE() 를 사용하세요:
16708c2ecf20Sopenharmony_ci
16718c2ecf20Sopenharmony_ci	void process_level(void)
16728c2ecf20Sopenharmony_ci	{
16738c2ecf20Sopenharmony_ci		WRITE_ONCE(msg, get_message());
16748c2ecf20Sopenharmony_ci		WRITE_ONCE(flag, true);
16758c2ecf20Sopenharmony_ci	}
16768c2ecf20Sopenharmony_ci
16778c2ecf20Sopenharmony_ci	void interrupt_handler(void)
16788c2ecf20Sopenharmony_ci	{
16798c2ecf20Sopenharmony_ci		if (READ_ONCE(flag))
16808c2ecf20Sopenharmony_ci			process_message(READ_ONCE(msg));
16818c2ecf20Sopenharmony_ci	}
16828c2ecf20Sopenharmony_ci
16838c2ecf20Sopenharmony_ci     interrupt_handler() 안에서도 중첩된 인터럽트나 NMI 와 같이 인터럽트 핸들러
16848c2ecf20Sopenharmony_ci     역시 'flag' 와 'msg' 에 접근하는 또다른 무언가에 인터럽트 될 수 있다면
16858c2ecf20Sopenharmony_ci     READ_ONCE() 와 WRITE_ONCE() 를 사용해야 함을 기억해 두세요.  만약 그런
16868c2ecf20Sopenharmony_ci     가능성이 없다면, interrupt_handler() 안에서는 문서화 목적이 아니라면
16878c2ecf20Sopenharmony_ci     READ_ONCE() 와 WRITE_ONCE() 는 필요치 않습니다.  (근래의 리눅스 커널에서
16888c2ecf20Sopenharmony_ci     중첩된 인터럽트는 보통 잘 일어나지 않음도 기억해 두세요, 실제로, 어떤
16898c2ecf20Sopenharmony_ci     인터럽트 핸들러가 인터럽트가 활성화된 채로 리턴하면 WARN_ONCE() 가
16908c2ecf20Sopenharmony_ci     실행됩니다.)
16918c2ecf20Sopenharmony_ci
16928c2ecf20Sopenharmony_ci     컴파일러는 READ_ONCE() 와 WRITE_ONCE() 뒤의 READ_ONCE() 나 WRITE_ONCE(),
16938c2ecf20Sopenharmony_ci     barrier(), 또는 비슷한 것들을 담고 있지 않은 코드를 움직일 수 있을 것으로
16948c2ecf20Sopenharmony_ci     가정되어야 합니다.
16958c2ecf20Sopenharmony_ci
16968c2ecf20Sopenharmony_ci     이 효과는 barrier() 를 통해서도 만들 수 있지만, READ_ONCE() 와
16978c2ecf20Sopenharmony_ci     WRITE_ONCE() 가 좀 더 안목 높은 선택입니다: READ_ONCE() 와 WRITE_ONCE()는
16988c2ecf20Sopenharmony_ci     컴파일러에 주어진 메모리 영역에 대해서만 최적화 가능성을 포기하도록
16998c2ecf20Sopenharmony_ci     하지만, barrier() 는 컴파일러가 지금까지 기계의 레지스터에 캐시해 놓은
17008c2ecf20Sopenharmony_ci     모든 메모리 영역의 값을 버려야 하게 하기 때문입니다.  물론, 컴파일러는
17018c2ecf20Sopenharmony_ci     READ_ONCE() 와 WRITE_ONCE() 가 일어난 순서도 지켜줍니다, CPU 는 당연히
17028c2ecf20Sopenharmony_ci     그 순서를 지킬 의무가 없지만요.
17038c2ecf20Sopenharmony_ci
17048c2ecf20Sopenharmony_ci (*) 컴파일러는 다음의 예에서와 같이 변수에의 스토어를 날조해낼 수도 있습니다:
17058c2ecf20Sopenharmony_ci
17068c2ecf20Sopenharmony_ci	if (a)
17078c2ecf20Sopenharmony_ci		b = a;
17088c2ecf20Sopenharmony_ci	else
17098c2ecf20Sopenharmony_ci		b = 42;
17108c2ecf20Sopenharmony_ci
17118c2ecf20Sopenharmony_ci     컴파일러는 아래와 같은 최적화로 브랜치를 줄일 겁니다:
17128c2ecf20Sopenharmony_ci
17138c2ecf20Sopenharmony_ci	b = 42;
17148c2ecf20Sopenharmony_ci	if (a)
17158c2ecf20Sopenharmony_ci		b = a;
17168c2ecf20Sopenharmony_ci
17178c2ecf20Sopenharmony_ci     싱글 쓰레드 코드에서 이 최적화는 안전할 뿐 아니라 브랜치 갯수를
17188c2ecf20Sopenharmony_ci     줄여줍니다.  하지만 안타깝게도, 동시성이 있는 코드에서는 이 최적화는 다른
17198c2ecf20Sopenharmony_ci     CPU 가 'b' 를 로드할 때, -- 'a' 가 0이 아닌데도 -- 가짜인 값, 42를 보게
17208c2ecf20Sopenharmony_ci     되는 경우를 가능하게 합니다.  이걸 방지하기 위해 WRITE_ONCE() 를
17218c2ecf20Sopenharmony_ci     사용하세요:
17228c2ecf20Sopenharmony_ci
17238c2ecf20Sopenharmony_ci	if (a)
17248c2ecf20Sopenharmony_ci		WRITE_ONCE(b, a);
17258c2ecf20Sopenharmony_ci	else
17268c2ecf20Sopenharmony_ci		WRITE_ONCE(b, 42);
17278c2ecf20Sopenharmony_ci
17288c2ecf20Sopenharmony_ci     컴파일러는 로드를 만들어낼 수도 있습니다.  일반적으로는 문제를 일으키지
17298c2ecf20Sopenharmony_ci     않지만, 캐시 라인 바운싱을 일으켜 성능과 확장성을 떨어뜨릴 수 있습니다.
17308c2ecf20Sopenharmony_ci     날조된 로드를 막기 위해선 READ_ONCE() 를 사용하세요.
17318c2ecf20Sopenharmony_ci
17328c2ecf20Sopenharmony_ci (*) 정렬된 메모리 주소에 위치한, 한번의 메모리 참조 인스트럭션으로 액세스
17338c2ecf20Sopenharmony_ci     가능한 크기의 데이터는 하나의 큰 액세스가 여러개의 작은 액세스들로
17348c2ecf20Sopenharmony_ci     대체되는 "로드 티어링(load tearing)" 과 "스토어 티어링(store tearing)" 을
17358c2ecf20Sopenharmony_ci     방지합니다.  예를 들어, 주어진 아키텍쳐가 7-bit imeediate field 를 갖는
17368c2ecf20Sopenharmony_ci     16-bit 스토어 인스트럭션을 제공한다면, 컴파일러는 다음의 32-bit 스토어를
17378c2ecf20Sopenharmony_ci     구현하는데에 두개의 16-bit store-immediate 명령을 사용하려 할겁니다:
17388c2ecf20Sopenharmony_ci
17398c2ecf20Sopenharmony_ci	p = 0x00010002;
17408c2ecf20Sopenharmony_ci
17418c2ecf20Sopenharmony_ci     스토어 할 상수를 만들고 그 값을 스토어 하기 위해 두개가 넘는 인스트럭션을
17428c2ecf20Sopenharmony_ci     사용하게 되는, 이런 종류의 최적화를 GCC 는 실제로 함을 부디 알아 두십시오.
17438c2ecf20Sopenharmony_ci     이 최적화는 싱글 쓰레드 코드에서는 성공적인 최적화 입니다.  실제로, 근래에
17448c2ecf20Sopenharmony_ci     발생한 (그리고 고쳐진) 버그는 GCC 가 volatile 스토어에 비정상적으로 이
17458c2ecf20Sopenharmony_ci     최적화를 사용하게 했습니다.  그런 버그가 없다면, 다음의 예에서
17468c2ecf20Sopenharmony_ci     WRITE_ONCE() 의 사용은 스토어 티어링을 방지합니다:
17478c2ecf20Sopenharmony_ci
17488c2ecf20Sopenharmony_ci	WRITE_ONCE(p, 0x00010002);
17498c2ecf20Sopenharmony_ci
17508c2ecf20Sopenharmony_ci     Packed 구조체의 사용 역시 다음의 예처럼  로드 / 스토어 티어링을 유발할 수
17518c2ecf20Sopenharmony_ci     있습니다:
17528c2ecf20Sopenharmony_ci
17538c2ecf20Sopenharmony_ci	struct __attribute__((__packed__)) foo {
17548c2ecf20Sopenharmony_ci		short a;
17558c2ecf20Sopenharmony_ci		int b;
17568c2ecf20Sopenharmony_ci		short c;
17578c2ecf20Sopenharmony_ci	};
17588c2ecf20Sopenharmony_ci	struct foo foo1, foo2;
17598c2ecf20Sopenharmony_ci	...
17608c2ecf20Sopenharmony_ci
17618c2ecf20Sopenharmony_ci	foo2.a = foo1.a;
17628c2ecf20Sopenharmony_ci	foo2.b = foo1.b;
17638c2ecf20Sopenharmony_ci	foo2.c = foo1.c;
17648c2ecf20Sopenharmony_ci
17658c2ecf20Sopenharmony_ci     READ_ONCE() 나 WRITE_ONCE() 도 없고 volatile 마킹도 없기 때문에,
17668c2ecf20Sopenharmony_ci     컴파일러는 이 세개의 대입문을 두개의 32-bit 로드와 두개의 32-bit 스토어로
17678c2ecf20Sopenharmony_ci     변환할 수 있습니다.  이는 'foo1.b' 의 값의 로드 티어링과 'foo2.b' 의
17688c2ecf20Sopenharmony_ci     스토어 티어링을 초래할 겁니다.  이 예에서도 READ_ONCE() 와 WRITE_ONCE()
17698c2ecf20Sopenharmony_ci     가 티어링을 막을 수 있습니다:
17708c2ecf20Sopenharmony_ci
17718c2ecf20Sopenharmony_ci	foo2.a = foo1.a;
17728c2ecf20Sopenharmony_ci	WRITE_ONCE(foo2.b, READ_ONCE(foo1.b));
17738c2ecf20Sopenharmony_ci	foo2.c = foo1.c;
17748c2ecf20Sopenharmony_ci
17758c2ecf20Sopenharmony_ci그렇지만, volatile 로 마크된 변수에 대해서는 READ_ONCE() 와 WRITE_ONCE() 가
17768c2ecf20Sopenharmony_ci필요치 않습니다.  예를 들어, 'jiffies' 는 volatile 로 마크되어 있기 때문에,
17778c2ecf20Sopenharmony_ciREAD_ONCE(jiffies) 라고 할 필요가 없습니다.  READ_ONCE() 와 WRITE_ONCE() 가
17788c2ecf20Sopenharmony_ci실은 volatile 캐스팅으로 구현되어 있어서 인자가 이미 volatile 로 마크되어
17798c2ecf20Sopenharmony_ci있다면 또다른 효과를 내지는 않기 때문입니다.
17808c2ecf20Sopenharmony_ci
17818c2ecf20Sopenharmony_ci이 컴파일러 배리어들은 CPU 에는 직접적 효과를 전혀 만들지 않기 때문에, 결국은
17828c2ecf20Sopenharmony_ci재배치가 일어날 수도 있음을 부디 기억해 두십시오.
17838c2ecf20Sopenharmony_ci
17848c2ecf20Sopenharmony_ci
17858c2ecf20Sopenharmony_ciCPU 메모리 배리어
17868c2ecf20Sopenharmony_ci-----------------
17878c2ecf20Sopenharmony_ci
17888c2ecf20Sopenharmony_ci리눅스 커널은 다음의 여덟개 기본 CPU 메모리 배리어를 가지고 있습니다:
17898c2ecf20Sopenharmony_ci
17908c2ecf20Sopenharmony_ci	TYPE		MANDATORY		SMP CONDITIONAL
17918c2ecf20Sopenharmony_ci	===============	=======================	===========================
17928c2ecf20Sopenharmony_ci	범용		mb()			smp_mb()
17938c2ecf20Sopenharmony_ci	쓰기		wmb()			smp_wmb()
17948c2ecf20Sopenharmony_ci	읽기		rmb()			smp_rmb()
17958c2ecf20Sopenharmony_ci	데이터 의존성				READ_ONCE()
17968c2ecf20Sopenharmony_ci
17978c2ecf20Sopenharmony_ci
17988c2ecf20Sopenharmony_ci데이터 의존성 배리어를 제외한 모든 메모리 배리어는 컴파일러 배리어를
17998c2ecf20Sopenharmony_ci포함합니다.  데이터 의존성은 컴파일러에의 추가적인 순서 보장을 포함하지
18008c2ecf20Sopenharmony_ci않습니다.
18018c2ecf20Sopenharmony_ci
18028c2ecf20Sopenharmony_ci방백: 데이터 의존성이 있는 경우, 컴파일러는 해당 로드를 올바른 순서로 일으킬
18038c2ecf20Sopenharmony_ci것으로 (예: `a[b]` 는 a[b] 를 로드 하기 전에 b 의 값을 먼저 로드한다)
18048c2ecf20Sopenharmony_ci기대되지만, C 언어 사양에는 컴파일러가 b 의 값을 추측 (예: 1 과 같음) 해서
18058c2ecf20Sopenharmony_cib  로드 전에 a 로드를 하는 코드 (예: tmp = a[1]; if (b != 1) tmp = a[b]; ) 를
18068c2ecf20Sopenharmony_ci만들지 않아야 한다는 내용 같은 건 없습니다.  또한 컴파일러는 a[b] 를 로드한
18078c2ecf20Sopenharmony_ci후에 b 를 또다시 로드할 수도 있어서, a[b] 보다 최신 버전의 b 값을 가질 수도
18088c2ecf20Sopenharmony_ci있습니다.  이런 문제들의 해결책에 대한 의견 일치는 아직 없습니다만, 일단
18098c2ecf20Sopenharmony_ciREAD_ONCE() 매크로부터 보기 시작하는게 좋은 시작이 될겁니다.
18108c2ecf20Sopenharmony_ci
18118c2ecf20Sopenharmony_ciSMP 메모리 배리어들은 유니프로세서로 컴파일된 시스템에서는 컴파일러 배리어로
18128c2ecf20Sopenharmony_ci바뀌는데, 하나의 CPU 는 스스로 일관성을 유지하고, 겹치는 액세스들 역시 올바른
18138c2ecf20Sopenharmony_ci순서로 행해질 것으로 생각되기 때문입니다.  하지만, 아래의 "Virtual Machine
18148c2ecf20Sopenharmony_ciGuests" 서브섹션을 참고하십시오.
18158c2ecf20Sopenharmony_ci
18168c2ecf20Sopenharmony_ci[!] SMP 시스템에서 공유메모리로의 접근들을 순서 세워야 할 때, SMP 메모리
18178c2ecf20Sopenharmony_ci배리어는 _반드시_ 사용되어야 함을 기억하세요, 그대신 락을 사용하는 것으로도
18188c2ecf20Sopenharmony_ci충분하긴 하지만 말이죠.
18198c2ecf20Sopenharmony_ci
18208c2ecf20Sopenharmony_ciMandatory 배리어들은 SMP 시스템에서도 UP 시스템에서도 SMP 효과만 통제하기에는
18218c2ecf20Sopenharmony_ci불필요한 오버헤드를 갖기 때문에 SMP 효과만 통제하면 되는 곳에는 사용되지 않아야
18228c2ecf20Sopenharmony_ci합니다.  하지만, 느슨한 순서 규칙의 메모리 I/O 윈도우를 통한 MMIO 의 효과를
18238c2ecf20Sopenharmony_ci통제할 때에는 mandatory 배리어들이 사용될 수 있습니다.  이 배리어들은
18248c2ecf20Sopenharmony_ci컴파일러와 CPU 모두 재배치를 못하도록 함으로써 메모리 오퍼레이션들이 디바이스에
18258c2ecf20Sopenharmony_ci보여지는 순서에도 영향을 주기 때문에, SMP 가 아닌 시스템이라 할지라도 필요할 수
18268c2ecf20Sopenharmony_ci있습니다.
18278c2ecf20Sopenharmony_ci
18288c2ecf20Sopenharmony_ci
18298c2ecf20Sopenharmony_ci일부 고급 배리어 함수들도 있습니다:
18308c2ecf20Sopenharmony_ci
18318c2ecf20Sopenharmony_ci (*) smp_store_mb(var, value)
18328c2ecf20Sopenharmony_ci
18338c2ecf20Sopenharmony_ci     이 함수는 특정 변수에 특정 값을 대입하고 범용 메모리 배리어를 칩니다.
18348c2ecf20Sopenharmony_ci     UP 컴파일에서는 컴파일러 배리어보다 더한 것을 친다고는 보장되지 않습니다.
18358c2ecf20Sopenharmony_ci
18368c2ecf20Sopenharmony_ci
18378c2ecf20Sopenharmony_ci (*) smp_mb__before_atomic();
18388c2ecf20Sopenharmony_ci (*) smp_mb__after_atomic();
18398c2ecf20Sopenharmony_ci
18408c2ecf20Sopenharmony_ci     이것들은 메모리 배리어를 내포하지 않는 어토믹 RMW 함수를 사용하지만 코드에
18418c2ecf20Sopenharmony_ci     메모리 배리어가 필요한 경우를 위한 것들입니다.  메모리 배리어를 내포하지
18428c2ecf20Sopenharmony_ci     않는 어토믹 RMW 함수들의 예로는 더하기, 빼기, (실패한) 조건적
18438c2ecf20Sopenharmony_ci     오퍼레이션들, _relaxed 함수들이 있으며, atomic_read 나 atomic_set 은 이에
18448c2ecf20Sopenharmony_ci     해당되지 않습니다.  메모리 배리어가 필요해지는 흔한 예로는 어토믹
18458c2ecf20Sopenharmony_ci     오퍼레이션을 사용해 레퍼런스 카운트를 수정하는 경우를 들 수 있습니다.
18468c2ecf20Sopenharmony_ci
18478c2ecf20Sopenharmony_ci     이것들은 또한 (set_bit 과 clear_bit 같은) 메모리 배리어를 내포하지 않는
18488c2ecf20Sopenharmony_ci     어토믹 RMW bitop 함수들을 위해서도 사용될 수 있습니다.
18498c2ecf20Sopenharmony_ci
18508c2ecf20Sopenharmony_ci     한 예로, 객체 하나를 무효한 것으로 표시하고 그 객체의 레퍼런스 카운트를
18518c2ecf20Sopenharmony_ci     감소시키는 다음 코드를 보세요:
18528c2ecf20Sopenharmony_ci
18538c2ecf20Sopenharmony_ci	obj->dead = 1;
18548c2ecf20Sopenharmony_ci	smp_mb__before_atomic();
18558c2ecf20Sopenharmony_ci	atomic_dec(&obj->ref_count);
18568c2ecf20Sopenharmony_ci
18578c2ecf20Sopenharmony_ci     이 코드는 객체의 업데이트된 death 마크가 레퍼런스 카운터 감소 동작
18588c2ecf20Sopenharmony_ci     *전에* 보일 것을 보장합니다.
18598c2ecf20Sopenharmony_ci
18608c2ecf20Sopenharmony_ci     더 많은 정보를 위해선 Documentation/atomic_{t,bitops}.txt 문서를
18618c2ecf20Sopenharmony_ci     참고하세요.
18628c2ecf20Sopenharmony_ci
18638c2ecf20Sopenharmony_ci
18648c2ecf20Sopenharmony_ci (*) dma_wmb();
18658c2ecf20Sopenharmony_ci (*) dma_rmb();
18668c2ecf20Sopenharmony_ci
18678c2ecf20Sopenharmony_ci     이것들은 CPU 와 DMA 가능한 디바이스에서 모두 액세스 가능한 공유 메모리의
18688c2ecf20Sopenharmony_ci     읽기, 쓰기 작업들의 순서를 보장하기 위해 consistent memory 에서 사용하기
18698c2ecf20Sopenharmony_ci     위한 것들입니다.
18708c2ecf20Sopenharmony_ci
18718c2ecf20Sopenharmony_ci     예를 들어, 디바이스와 메모리를 공유하며, 디스크립터 상태 값을 사용해
18728c2ecf20Sopenharmony_ci     디스크립터가 디바이스에 속해 있는지 아니면 CPU 에 속해 있는지 표시하고,
18738c2ecf20Sopenharmony_ci     공지용 초인종(doorbell) 을 사용해 업데이트된 디스크립터가 디바이스에 사용
18748c2ecf20Sopenharmony_ci     가능해졌음을 공지하는 디바이스 드라이버를 생각해 봅시다:
18758c2ecf20Sopenharmony_ci
18768c2ecf20Sopenharmony_ci	if (desc->status != DEVICE_OWN) {
18778c2ecf20Sopenharmony_ci		/* 디스크립터를 소유하기 전에는 데이터를 읽지 않음 */
18788c2ecf20Sopenharmony_ci		dma_rmb();
18798c2ecf20Sopenharmony_ci
18808c2ecf20Sopenharmony_ci		/* 데이터를 읽고 씀 */
18818c2ecf20Sopenharmony_ci		read_data = desc->data;
18828c2ecf20Sopenharmony_ci		desc->data = write_data;
18838c2ecf20Sopenharmony_ci
18848c2ecf20Sopenharmony_ci		/* 상태 업데이트 전 수정사항을 반영 */
18858c2ecf20Sopenharmony_ci		dma_wmb();
18868c2ecf20Sopenharmony_ci
18878c2ecf20Sopenharmony_ci		/* 소유권을 수정 */
18888c2ecf20Sopenharmony_ci		desc->status = DEVICE_OWN;
18898c2ecf20Sopenharmony_ci
18908c2ecf20Sopenharmony_ci		/* 업데이트된 디스크립터의 디바이스에 공지 */
18918c2ecf20Sopenharmony_ci		writel(DESC_NOTIFY, doorbell);
18928c2ecf20Sopenharmony_ci	}
18938c2ecf20Sopenharmony_ci
18948c2ecf20Sopenharmony_ci     dma_rmb() 는 디스크립터로부터 데이터를 읽어오기 전에 디바이스가 소유권을
18958c2ecf20Sopenharmony_ci     내려놓았을 것을 보장하고, dma_wmb() 는 디바이스가 자신이 소유권을 다시
18968c2ecf20Sopenharmony_ci     가졌음을 보기 전에 디스크립터에 데이터가 쓰였을 것을 보장합니다.  참고로,
18978c2ecf20Sopenharmony_ci     writel() 을 사용하면 캐시 일관성이 있는 메모리 (cache coherent memory)
18988c2ecf20Sopenharmony_ci     쓰기가 MMIO 영역에의 쓰기 전에 완료되었을 것을 보장하므로 writel() 앞에
18998c2ecf20Sopenharmony_ci     wmb() 를 실행할 필요가 없음을 알아두시기 바랍니다.  writel() 보다 비용이
19008c2ecf20Sopenharmony_ci     저렴한 writel_relaxed() 는 이런 보장을 제공하지 않으므로 여기선 사용되지
19018c2ecf20Sopenharmony_ci     않아야 합니다.
19028c2ecf20Sopenharmony_ci
19038c2ecf20Sopenharmony_ci     writel_relaxed() 와 같은 완화된 I/O 접근자들에 대한 자세한 내용을 위해서는
19048c2ecf20Sopenharmony_ci     "커널 I/O 배리어의 효과" 섹션을, consistent memory 에 대한 자세한 내용을
19058c2ecf20Sopenharmony_ci     위해선 Documentation/core-api/dma-api.rst 문서를 참고하세요.
19068c2ecf20Sopenharmony_ci
19078c2ecf20Sopenharmony_ci (*) pmem_wmb();
19088c2ecf20Sopenharmony_ci
19098c2ecf20Sopenharmony_ci     이것은 persistent memory 를 위한 것으로, persistent 저장소에 가해진 변경
19108c2ecf20Sopenharmony_ci     사항이 플랫폼 연속성 도메인에 도달했을 것을 보장하기 위한 것입니다.
19118c2ecf20Sopenharmony_ci
19128c2ecf20Sopenharmony_ci     예를 들어, 임시적이지 않은 pmem 영역으로의 쓰기 후, 우리는 쓰기가 플랫폼
19138c2ecf20Sopenharmony_ci     연속성 도메인에 도달했을 것을 보장하기 위해 pmem_wmb() 를 사용합니다.
19148c2ecf20Sopenharmony_ci     이는 쓰기가 뒤따르는 instruction 들이 유발하는 어떠한 데이터 액세스나
19158c2ecf20Sopenharmony_ci     데이터 전송의 시작 전에 persistent 저장소를 업데이트 했을 것을 보장합니다.
19168c2ecf20Sopenharmony_ci     이는 wmb() 에 의해 이뤄지는 순서 규칙을 포함합니다.
19178c2ecf20Sopenharmony_ci
19188c2ecf20Sopenharmony_ci     Persistent memory 에서의 로드를 위해선 현재의 읽기 메모리 배리어로도 읽기
19198c2ecf20Sopenharmony_ci     순서를 보장하는데 충분합니다.
19208c2ecf20Sopenharmony_ci
19218c2ecf20Sopenharmony_ci=========================
19228c2ecf20Sopenharmony_ci암묵적 커널 메모리 배리어
19238c2ecf20Sopenharmony_ci=========================
19248c2ecf20Sopenharmony_ci
19258c2ecf20Sopenharmony_ci리눅스 커널의 일부 함수들은 메모리 배리어를 내장하고 있는데, 락(lock)과
19268c2ecf20Sopenharmony_ci스케쥴링 관련 함수들이 대부분입니다.
19278c2ecf20Sopenharmony_ci
19288c2ecf20Sopenharmony_ci여기선 _최소한의_ 보장을 설명합니다; 특정 아키텍쳐에서는 이 설명보다 더 많은
19298c2ecf20Sopenharmony_ci보장을 제공할 수도 있습니다만 해당 아키텍쳐에 종속적인 코드 외의 부분에서는
19308c2ecf20Sopenharmony_ci그런 보장을 기대해선 안될겁니다.
19318c2ecf20Sopenharmony_ci
19328c2ecf20Sopenharmony_ci
19338c2ecf20Sopenharmony_ci락 ACQUISITION 함수
19348c2ecf20Sopenharmony_ci-------------------
19358c2ecf20Sopenharmony_ci
19368c2ecf20Sopenharmony_ci리눅스 커널은 다양한 락 구성체를 가지고 있습니다:
19378c2ecf20Sopenharmony_ci
19388c2ecf20Sopenharmony_ci (*) 스핀 락
19398c2ecf20Sopenharmony_ci (*) R/W 스핀 락
19408c2ecf20Sopenharmony_ci (*) 뮤텍스
19418c2ecf20Sopenharmony_ci (*) 세마포어
19428c2ecf20Sopenharmony_ci (*) R/W 세마포어
19438c2ecf20Sopenharmony_ci
19448c2ecf20Sopenharmony_ci각 구성체마다 모든 경우에 "ACQUIRE" 오퍼레이션과 "RELEASE" 오퍼레이션의 변종이
19458c2ecf20Sopenharmony_ci존재합니다.  이 오퍼레이션들은 모두 적절한 배리어를 내포하고 있습니다:
19468c2ecf20Sopenharmony_ci
19478c2ecf20Sopenharmony_ci (1) ACQUIRE 오퍼레이션의 영향:
19488c2ecf20Sopenharmony_ci
19498c2ecf20Sopenharmony_ci     ACQUIRE 뒤에서 요청된 메모리 오퍼레이션은 ACQUIRE 오퍼레이션이 완료된
19508c2ecf20Sopenharmony_ci     뒤에 완료됩니다.
19518c2ecf20Sopenharmony_ci
19528c2ecf20Sopenharmony_ci     ACQUIRE 앞에서 요청된 메모리 오퍼레이션은 ACQUIRE 오퍼레이션이 완료된 후에
19538c2ecf20Sopenharmony_ci     완료될 수 있습니다.
19548c2ecf20Sopenharmony_ci
19558c2ecf20Sopenharmony_ci (2) RELEASE 오퍼레이션의 영향:
19568c2ecf20Sopenharmony_ci
19578c2ecf20Sopenharmony_ci     RELEASE 앞에서 요청된 메모리 오퍼레이션은 RELEASE 오퍼레이션이 완료되기
19588c2ecf20Sopenharmony_ci     전에 완료됩니다.
19598c2ecf20Sopenharmony_ci
19608c2ecf20Sopenharmony_ci     RELEASE 뒤에서 요청된 메모리 오퍼레이션은 RELEASE 오퍼레이션 완료 전에
19618c2ecf20Sopenharmony_ci     완료될 수 있습니다.
19628c2ecf20Sopenharmony_ci
19638c2ecf20Sopenharmony_ci (3) ACQUIRE vs ACQUIRE 영향:
19648c2ecf20Sopenharmony_ci
19658c2ecf20Sopenharmony_ci     어떤 ACQUIRE 오퍼레이션보다 앞에서 요청된 모든 ACQUIRE 오퍼레이션은 그
19668c2ecf20Sopenharmony_ci     ACQUIRE 오퍼레이션 전에 완료됩니다.
19678c2ecf20Sopenharmony_ci
19688c2ecf20Sopenharmony_ci (4) ACQUIRE vs RELEASE implication:
19698c2ecf20Sopenharmony_ci
19708c2ecf20Sopenharmony_ci     어떤 RELEASE 오퍼레이션보다 앞서 요청된 ACQUIRE 오퍼레이션은 그 RELEASE
19718c2ecf20Sopenharmony_ci     오퍼레이션보다 먼저 완료됩니다.
19728c2ecf20Sopenharmony_ci
19738c2ecf20Sopenharmony_ci (5) 실패한 조건적 ACQUIRE 영향:
19748c2ecf20Sopenharmony_ci
19758c2ecf20Sopenharmony_ci     ACQUIRE 오퍼레이션의 일부 락(lock) 변종은 락이 곧바로 획득하기에는
19768c2ecf20Sopenharmony_ci     불가능한 상태이거나 락이 획득 가능해지도록 기다리는 도중 시그널을 받거나
19778c2ecf20Sopenharmony_ci     해서 실패할 수 있습니다.  실패한 락은 어떤 배리어도 내포하지 않습니다.
19788c2ecf20Sopenharmony_ci
19798c2ecf20Sopenharmony_ci[!] 참고: 락 ACQUIRE 와 RELEASE 가 단방향 배리어여서 나타나는 현상 중 하나는
19808c2ecf20Sopenharmony_ci크리티컬 섹션 바깥의 인스트럭션의 영향이 크리티컬 섹션 내부로도 들어올 수
19818c2ecf20Sopenharmony_ci있다는 것입니다.
19828c2ecf20Sopenharmony_ci
19838c2ecf20Sopenharmony_ciRELEASE 후에 요청되는 ACQUIRE 는 전체 메모리 배리어라 여겨지면 안되는데,
19848c2ecf20Sopenharmony_ciACQUIRE 앞의 액세스가 ACQUIRE 후에 수행될 수 있고, RELEASE 후의 액세스가
19858c2ecf20Sopenharmony_ciRELEASE 전에 수행될 수도 있으며, 그 두개의 액세스가 서로를 지나칠 수도 있기
19868c2ecf20Sopenharmony_ci때문입니다:
19878c2ecf20Sopenharmony_ci
19888c2ecf20Sopenharmony_ci	*A = a;
19898c2ecf20Sopenharmony_ci	ACQUIRE M
19908c2ecf20Sopenharmony_ci	RELEASE M
19918c2ecf20Sopenharmony_ci	*B = b;
19928c2ecf20Sopenharmony_ci
19938c2ecf20Sopenharmony_ci는 다음과 같이 될 수도 있습니다:
19948c2ecf20Sopenharmony_ci
19958c2ecf20Sopenharmony_ci	ACQUIRE M, STORE *B, STORE *A, RELEASE M
19968c2ecf20Sopenharmony_ci
19978c2ecf20Sopenharmony_ciACQUIRE 와 RELEASE 가 락 획득과 해제라면, 그리고 락의 ACQUIRE 와 RELEASE 가
19988c2ecf20Sopenharmony_ci같은 락 변수에 대한 것이라면, 해당 락을 쥐고 있지 않은 다른 CPU 의 시야에는
19998c2ecf20Sopenharmony_ci이와 같은 재배치가 일어나는 것으로 보일 수 있습니다.  요약하자면, ACQUIRE 에
20008c2ecf20Sopenharmony_ci이어 RELEASE 오퍼레이션을 순차적으로 실행하는 행위가 전체 메모리 배리어로
20018c2ecf20Sopenharmony_ci생각되어선 -안됩니다-.
20028c2ecf20Sopenharmony_ci
20038c2ecf20Sopenharmony_ci비슷하게, 앞의 반대 케이스인 RELEASE 와 ACQUIRE 두개 오퍼레이션의 순차적 실행
20048c2ecf20Sopenharmony_ci역시 전체 메모리 배리어를 내포하지 않습니다.  따라서, RELEASE, ACQUIRE 로
20058c2ecf20Sopenharmony_ci규정되는 크리티컬 섹션의 CPU 수행은 RELEASE 와 ACQUIRE 를 가로지를 수 있으므로,
20068c2ecf20Sopenharmony_ci다음과 같은 코드는:
20078c2ecf20Sopenharmony_ci
20088c2ecf20Sopenharmony_ci	*A = a;
20098c2ecf20Sopenharmony_ci	RELEASE M
20108c2ecf20Sopenharmony_ci	ACQUIRE N
20118c2ecf20Sopenharmony_ci	*B = b;
20128c2ecf20Sopenharmony_ci
20138c2ecf20Sopenharmony_ci다음과 같이 수행될 수 있습니다:
20148c2ecf20Sopenharmony_ci
20158c2ecf20Sopenharmony_ci	ACQUIRE N, STORE *B, STORE *A, RELEASE M
20168c2ecf20Sopenharmony_ci
20178c2ecf20Sopenharmony_ci이런 재배치는 데드락을 일으킬 수도 있을 것처럼 보일 수 있습니다.  하지만, 그런
20188c2ecf20Sopenharmony_ci데드락의 조짐이 있다면 RELEASE 는 단순히 완료될 것이므로 데드락은 존재할 수
20198c2ecf20Sopenharmony_ci없습니다.
20208c2ecf20Sopenharmony_ci
20218c2ecf20Sopenharmony_ci	이게 어떻게 올바른 동작을 할 수 있을까요?
20228c2ecf20Sopenharmony_ci
20238c2ecf20Sopenharmony_ci	우리가 이야기 하고 있는건 재배치를 하는 CPU 에 대한 이야기이지,
20248c2ecf20Sopenharmony_ci	컴파일러에 대한 것이 아니란 점이 핵심입니다.  컴파일러 (또는, 개발자)
20258c2ecf20Sopenharmony_ci	가 오퍼레이션들을 이렇게 재배치하면, 데드락이 일어날 수 -있습-니다.
20268c2ecf20Sopenharmony_ci
20278c2ecf20Sopenharmony_ci	하지만 CPU 가 오퍼레이션들을 재배치 했다는걸 생각해 보세요.  이 예에서,
20288c2ecf20Sopenharmony_ci	어셈블리 코드 상으로는 언락이 락을 앞서게 되어 있습니다.  CPU 가 이를
20298c2ecf20Sopenharmony_ci	재배치해서 뒤의 락 오퍼레이션을 먼저 실행하게 됩니다.  만약 데드락이
20308c2ecf20Sopenharmony_ci	존재한다면, 이 락 오퍼레이션은 그저 스핀을 하며 계속해서 락을
20318c2ecf20Sopenharmony_ci	시도합니다 (또는, 한참 후에겠지만, 잠듭니다).  CPU 는 언젠가는
20328c2ecf20Sopenharmony_ci	(어셈블리 코드에서는 락을 앞서는) 언락 오퍼레이션을 실행하는데, 이 언락
20338c2ecf20Sopenharmony_ci	오퍼레이션이 잠재적 데드락을 해결하고, 락 오퍼레이션도 뒤이어 성공하게
20348c2ecf20Sopenharmony_ci	됩니다.
20358c2ecf20Sopenharmony_ci
20368c2ecf20Sopenharmony_ci	하지만 만약 락이 잠을 자는 타입이었다면요?  그런 경우에 코드는
20378c2ecf20Sopenharmony_ci	스케쥴러로 들어가려 할 거고, 여기서 결국은 메모리 배리어를 만나게
20388c2ecf20Sopenharmony_ci	되는데, 이 메모리 배리어는 앞의 언락 오퍼레이션이 완료되도록 만들고,
20398c2ecf20Sopenharmony_ci	데드락은 이번에도 해결됩니다.  잠을 자는 행위와 언락 사이의 경주 상황
20408c2ecf20Sopenharmony_ci	(race) 도 있을 수 있겠습니다만, 락 관련 기능들은 그런 경주 상황을 모든
20418c2ecf20Sopenharmony_ci	경우에 제대로 해결할 수 있어야 합니다.
20428c2ecf20Sopenharmony_ci
20438c2ecf20Sopenharmony_ci락과 세마포어는 UP 컴파일된 시스템에서의 순서에 대해 보장을 하지 않기 때문에,
20448c2ecf20Sopenharmony_ci그런 상황에서 인터럽트 비활성화 오퍼레이션과 함께가 아니라면 어떤 일에도 - 특히
20458c2ecf20Sopenharmony_ciI/O 액세스와 관련해서는 - 제대로 사용될 수 없을 겁니다.
20468c2ecf20Sopenharmony_ci
20478c2ecf20Sopenharmony_ci"CPU 간 ACQUIRING 배리어 효과" 섹션도 참고하시기 바랍니다.
20488c2ecf20Sopenharmony_ci
20498c2ecf20Sopenharmony_ci
20508c2ecf20Sopenharmony_ci예를 들어, 다음과 같은 코드를 생각해 봅시다:
20518c2ecf20Sopenharmony_ci
20528c2ecf20Sopenharmony_ci	*A = a;
20538c2ecf20Sopenharmony_ci	*B = b;
20548c2ecf20Sopenharmony_ci	ACQUIRE
20558c2ecf20Sopenharmony_ci	*C = c;
20568c2ecf20Sopenharmony_ci	*D = d;
20578c2ecf20Sopenharmony_ci	RELEASE
20588c2ecf20Sopenharmony_ci	*E = e;
20598c2ecf20Sopenharmony_ci	*F = f;
20608c2ecf20Sopenharmony_ci
20618c2ecf20Sopenharmony_ci여기선 다음의 이벤트 시퀀스가 생길 수 있습니다:
20628c2ecf20Sopenharmony_ci
20638c2ecf20Sopenharmony_ci	ACQUIRE, {*F,*A}, *E, {*C,*D}, *B, RELEASE
20648c2ecf20Sopenharmony_ci
20658c2ecf20Sopenharmony_ci	[+] {*F,*A} 는 조합된 액세스를 의미합니다.
20668c2ecf20Sopenharmony_ci
20678c2ecf20Sopenharmony_ci하지만 다음과 같은 건 불가능하죠:
20688c2ecf20Sopenharmony_ci
20698c2ecf20Sopenharmony_ci	{*F,*A}, *B,	ACQUIRE, *C, *D,	RELEASE, *E
20708c2ecf20Sopenharmony_ci	*A, *B, *C,	ACQUIRE, *D,		RELEASE, *E, *F
20718c2ecf20Sopenharmony_ci	*A, *B,		ACQUIRE, *C,		RELEASE, *D, *E, *F
20728c2ecf20Sopenharmony_ci	*B,		ACQUIRE, *C, *D,	RELEASE, {*F,*A}, *E
20738c2ecf20Sopenharmony_ci
20748c2ecf20Sopenharmony_ci
20758c2ecf20Sopenharmony_ci
20768c2ecf20Sopenharmony_ci인터럽트 비활성화 함수
20778c2ecf20Sopenharmony_ci----------------------
20788c2ecf20Sopenharmony_ci
20798c2ecf20Sopenharmony_ci인터럽트를 비활성화 하는 함수 (ACQUIRE 와 동일) 와 인터럽트를 활성화 하는 함수
20808c2ecf20Sopenharmony_ci(RELEASE 와 동일) 는 컴파일러 배리어처럼만 동작합니다.  따라서, 별도의 메모리
20818c2ecf20Sopenharmony_ci배리어나 I/O 배리어가 필요한 상황이라면 그 배리어들은 인터럽트 비활성화 함수
20828c2ecf20Sopenharmony_ci외의 방법으로 제공되어야만 합니다.
20838c2ecf20Sopenharmony_ci
20848c2ecf20Sopenharmony_ci
20858c2ecf20Sopenharmony_ci슬립과 웨이크업 함수
20868c2ecf20Sopenharmony_ci--------------------
20878c2ecf20Sopenharmony_ci
20888c2ecf20Sopenharmony_ci글로벌 데이터에 표시된 이벤트에 의해 프로세스를 잠에 빠트리는 것과 깨우는 것은
20898c2ecf20Sopenharmony_ci해당 이벤트를 기다리는 태스크의 태스크 상태와 그 이벤트를 알리기 위해 사용되는
20908c2ecf20Sopenharmony_ci글로벌 데이터, 두 데이터간의 상호작용으로 볼 수 있습니다.  이것이 옳은 순서대로
20918c2ecf20Sopenharmony_ci일어남을 분명히 하기 위해, 프로세스를 잠에 들게 하는 기능과 깨우는 기능은
20928c2ecf20Sopenharmony_ci몇가지 배리어를 내포합니다.
20938c2ecf20Sopenharmony_ci
20948c2ecf20Sopenharmony_ci먼저, 잠을 재우는 쪽은 일반적으로 다음과 같은 이벤트 시퀀스를 따릅니다:
20958c2ecf20Sopenharmony_ci
20968c2ecf20Sopenharmony_ci	for (;;) {
20978c2ecf20Sopenharmony_ci		set_current_state(TASK_UNINTERRUPTIBLE);
20988c2ecf20Sopenharmony_ci		if (event_indicated)
20998c2ecf20Sopenharmony_ci			break;
21008c2ecf20Sopenharmony_ci		schedule();
21018c2ecf20Sopenharmony_ci	}
21028c2ecf20Sopenharmony_ci
21038c2ecf20Sopenharmony_ciset_current_state() 에 의해, 태스크 상태가 바뀐 후 범용 메모리 배리어가
21048c2ecf20Sopenharmony_ci자동으로 삽입됩니다:
21058c2ecf20Sopenharmony_ci
21068c2ecf20Sopenharmony_ci	CPU 1
21078c2ecf20Sopenharmony_ci	===============================
21088c2ecf20Sopenharmony_ci	set_current_state();
21098c2ecf20Sopenharmony_ci	  smp_store_mb();
21108c2ecf20Sopenharmony_ci	    STORE current->state
21118c2ecf20Sopenharmony_ci	    <범용 배리어>
21128c2ecf20Sopenharmony_ci	LOAD event_indicated
21138c2ecf20Sopenharmony_ci
21148c2ecf20Sopenharmony_ciset_current_state() 는 다음의 것들로 감싸질 수도 있습니다:
21158c2ecf20Sopenharmony_ci
21168c2ecf20Sopenharmony_ci	prepare_to_wait();
21178c2ecf20Sopenharmony_ci	prepare_to_wait_exclusive();
21188c2ecf20Sopenharmony_ci
21198c2ecf20Sopenharmony_ci이것들 역시 상태를 설정한 후 범용 메모리 배리어를 삽입합니다.
21208c2ecf20Sopenharmony_ci앞의 전체 시퀀스는 다음과 같은 함수들로 한번에 수행 가능한데, 이것들은 모두
21218c2ecf20Sopenharmony_ci올바른 장소에 메모리 배리어를 삽입합니다:
21228c2ecf20Sopenharmony_ci
21238c2ecf20Sopenharmony_ci	wait_event();
21248c2ecf20Sopenharmony_ci	wait_event_interruptible();
21258c2ecf20Sopenharmony_ci	wait_event_interruptible_exclusive();
21268c2ecf20Sopenharmony_ci	wait_event_interruptible_timeout();
21278c2ecf20Sopenharmony_ci	wait_event_killable();
21288c2ecf20Sopenharmony_ci	wait_event_timeout();
21298c2ecf20Sopenharmony_ci	wait_on_bit();
21308c2ecf20Sopenharmony_ci	wait_on_bit_lock();
21318c2ecf20Sopenharmony_ci
21328c2ecf20Sopenharmony_ci
21338c2ecf20Sopenharmony_ci두번째로, 깨우기를 수행하는 코드는 일반적으로 다음과 같을 겁니다:
21348c2ecf20Sopenharmony_ci
21358c2ecf20Sopenharmony_ci	event_indicated = 1;
21368c2ecf20Sopenharmony_ci	wake_up(&event_wait_queue);
21378c2ecf20Sopenharmony_ci
21388c2ecf20Sopenharmony_ci또는:
21398c2ecf20Sopenharmony_ci
21408c2ecf20Sopenharmony_ci	event_indicated = 1;
21418c2ecf20Sopenharmony_ci	wake_up_process(event_daemon);
21428c2ecf20Sopenharmony_ci
21438c2ecf20Sopenharmony_ciwake_up() 이 무언가를 깨우게 되면, 이 함수는 범용 메모리 배리어를 수행합니다.
21448c2ecf20Sopenharmony_ci이 함수가 아무것도 깨우지 않는다면 메모리 배리어는 수행될 수도, 수행되지 않을
21458c2ecf20Sopenharmony_ci수도 있습니다; 이 경우에 메모리 배리어를 수행할 거라 오해해선 안됩니다.  이
21468c2ecf20Sopenharmony_ci배리어는 태스크 상태가 접근되기 전에 수행되는데, 자세히 말하면 이 이벤트를
21478c2ecf20Sopenharmony_ci알리기 위한 STORE 와 TASK_RUNNING 으로 상태를 쓰는 STORE 사이에 수행됩니다:
21488c2ecf20Sopenharmony_ci
21498c2ecf20Sopenharmony_ci	CPU 1 (Sleeper)			CPU 2 (Waker)
21508c2ecf20Sopenharmony_ci	===============================	===============================
21518c2ecf20Sopenharmony_ci	set_current_state();		STORE event_indicated
21528c2ecf20Sopenharmony_ci	  smp_store_mb();		wake_up();
21538c2ecf20Sopenharmony_ci	    STORE current->state	  ...
21548c2ecf20Sopenharmony_ci	    <범용 배리어>		  <범용 배리어>
21558c2ecf20Sopenharmony_ci	LOAD event_indicated		  if ((LOAD task->state) & TASK_NORMAL)
21568c2ecf20Sopenharmony_ci					    STORE task->state
21578c2ecf20Sopenharmony_ci
21588c2ecf20Sopenharmony_ci여기서 "task" 는 깨어나지는 쓰레드이고 CPU 1 의 "current" 와 같습니다.
21598c2ecf20Sopenharmony_ci
21608c2ecf20Sopenharmony_ci반복하지만, wake_up() 이 무언가를 정말 깨운다면 범용 메모리 배리어가 수행될
21618c2ecf20Sopenharmony_ci것이 보장되지만, 그렇지 않다면 그런 보장이 없습니다.  이걸 이해하기 위해, X 와
21628c2ecf20Sopenharmony_ciY 는 모두 0 으로 초기화 되어 있다는 가정 하에 아래의 이벤트 시퀀스를 생각해
21638c2ecf20Sopenharmony_ci봅시다:
21648c2ecf20Sopenharmony_ci
21658c2ecf20Sopenharmony_ci	CPU 1				CPU 2
21668c2ecf20Sopenharmony_ci	===============================	===============================
21678c2ecf20Sopenharmony_ci	X = 1;				Y = 1;
21688c2ecf20Sopenharmony_ci	smp_mb();			wake_up();
21698c2ecf20Sopenharmony_ci	LOAD Y				LOAD X
21708c2ecf20Sopenharmony_ci
21718c2ecf20Sopenharmony_ci정말로 깨우기가 행해졌다면, 두 로드 중 (최소한) 하나는 1 을 보게 됩니다.
21728c2ecf20Sopenharmony_ci반면에, 실제 깨우기가 행해지지 않았다면, 두 로드 모두 0을 볼 수도 있습니다.
21738c2ecf20Sopenharmony_ci
21748c2ecf20Sopenharmony_ciwake_up_process() 는 항상 범용 메모리 배리어를 수행합니다.  이 배리어 역시
21758c2ecf20Sopenharmony_ci태스크 상태가 접근되기 전에 수행됩니다.  특히, 앞의 예제 코드에서 wake_up() 이
21768c2ecf20Sopenharmony_ciwake_up_process() 로 대체된다면 두 로드 중 하나는 1을 볼 것이 보장됩니다.
21778c2ecf20Sopenharmony_ci
21788c2ecf20Sopenharmony_ci사용 가능한 깨우기류 함수들로 다음과 같은 것들이 있습니다:
21798c2ecf20Sopenharmony_ci
21808c2ecf20Sopenharmony_ci	complete();
21818c2ecf20Sopenharmony_ci	wake_up();
21828c2ecf20Sopenharmony_ci	wake_up_all();
21838c2ecf20Sopenharmony_ci	wake_up_bit();
21848c2ecf20Sopenharmony_ci	wake_up_interruptible();
21858c2ecf20Sopenharmony_ci	wake_up_interruptible_all();
21868c2ecf20Sopenharmony_ci	wake_up_interruptible_nr();
21878c2ecf20Sopenharmony_ci	wake_up_interruptible_poll();
21888c2ecf20Sopenharmony_ci	wake_up_interruptible_sync();
21898c2ecf20Sopenharmony_ci	wake_up_interruptible_sync_poll();
21908c2ecf20Sopenharmony_ci	wake_up_locked();
21918c2ecf20Sopenharmony_ci	wake_up_locked_poll();
21928c2ecf20Sopenharmony_ci	wake_up_nr();
21938c2ecf20Sopenharmony_ci	wake_up_poll();
21948c2ecf20Sopenharmony_ci	wake_up_process();
21958c2ecf20Sopenharmony_ci
21968c2ecf20Sopenharmony_ci메모리 순서규칙 관점에서, 이 함수들은 모두 wake_up() 과 같거나 보다 강한 순서
21978c2ecf20Sopenharmony_ci보장을 제공합니다.
21988c2ecf20Sopenharmony_ci
21998c2ecf20Sopenharmony_ci[!] 잠재우는 코드와 깨우는 코드에 내포되는 메모리 배리어들은 깨우기 전에
22008c2ecf20Sopenharmony_ci이루어진 스토어를 잠재우는 코드가 set_current_state() 를 호출한 후에 행하는
22018c2ecf20Sopenharmony_ci로드에 대해 순서를 맞추지 _않는다는_ 점을 기억하세요.  예를 들어, 잠재우는
22028c2ecf20Sopenharmony_ci코드가 다음과 같고:
22038c2ecf20Sopenharmony_ci
22048c2ecf20Sopenharmony_ci	set_current_state(TASK_INTERRUPTIBLE);
22058c2ecf20Sopenharmony_ci	if (event_indicated)
22068c2ecf20Sopenharmony_ci		break;
22078c2ecf20Sopenharmony_ci	__set_current_state(TASK_RUNNING);
22088c2ecf20Sopenharmony_ci	do_something(my_data);
22098c2ecf20Sopenharmony_ci
22108c2ecf20Sopenharmony_ci깨우는 코드는 다음과 같다면:
22118c2ecf20Sopenharmony_ci
22128c2ecf20Sopenharmony_ci	my_data = value;
22138c2ecf20Sopenharmony_ci	event_indicated = 1;
22148c2ecf20Sopenharmony_ci	wake_up(&event_wait_queue);
22158c2ecf20Sopenharmony_ci
22168c2ecf20Sopenharmony_cievent_indecated 에의 변경이 잠재우는 코드에게 my_data 에의 변경 후에 이루어진
22178c2ecf20Sopenharmony_ci것으로 인지될 것이라는 보장이 없습니다.  이런 경우에는 양쪽 코드 모두 각각의
22188c2ecf20Sopenharmony_ci데이터 액세스 사이에 메모리 배리어를 직접 쳐야 합니다.  따라서 앞의 재우는
22198c2ecf20Sopenharmony_ci코드는 다음과 같이:
22208c2ecf20Sopenharmony_ci
22218c2ecf20Sopenharmony_ci	set_current_state(TASK_INTERRUPTIBLE);
22228c2ecf20Sopenharmony_ci	if (event_indicated) {
22238c2ecf20Sopenharmony_ci		smp_rmb();
22248c2ecf20Sopenharmony_ci		do_something(my_data);
22258c2ecf20Sopenharmony_ci	}
22268c2ecf20Sopenharmony_ci
22278c2ecf20Sopenharmony_ci그리고 깨우는 코드는 다음과 같이 되어야 합니다:
22288c2ecf20Sopenharmony_ci
22298c2ecf20Sopenharmony_ci	my_data = value;
22308c2ecf20Sopenharmony_ci	smp_wmb();
22318c2ecf20Sopenharmony_ci	event_indicated = 1;
22328c2ecf20Sopenharmony_ci	wake_up(&event_wait_queue);
22338c2ecf20Sopenharmony_ci
22348c2ecf20Sopenharmony_ci
22358c2ecf20Sopenharmony_ci그외의 함수들
22368c2ecf20Sopenharmony_ci-------------
22378c2ecf20Sopenharmony_ci
22388c2ecf20Sopenharmony_ci그외의 배리어를 내포하는 함수들은 다음과 같습니다:
22398c2ecf20Sopenharmony_ci
22408c2ecf20Sopenharmony_ci (*) schedule() 과 그 유사한 것들이 완전한 메모리 배리어를 내포합니다.
22418c2ecf20Sopenharmony_ci
22428c2ecf20Sopenharmony_ci
22438c2ecf20Sopenharmony_ci==============================
22448c2ecf20Sopenharmony_ciCPU 간 ACQUIRING 배리어의 효과
22458c2ecf20Sopenharmony_ci==============================
22468c2ecf20Sopenharmony_ci
22478c2ecf20Sopenharmony_ciSMP 시스템에서의 락 기능들은 더욱 강력한 형태의 배리어를 제공합니다: 이
22488c2ecf20Sopenharmony_ci배리어는 동일한 락을 사용하는 다른 CPU 들의 메모리 액세스 순서에도 영향을
22498c2ecf20Sopenharmony_ci끼칩니다.
22508c2ecf20Sopenharmony_ci
22518c2ecf20Sopenharmony_ci
22528c2ecf20Sopenharmony_ciACQUIRE VS 메모리 액세스
22538c2ecf20Sopenharmony_ci------------------------
22548c2ecf20Sopenharmony_ci
22558c2ecf20Sopenharmony_ci다음의 예를 생각해 봅시다: 시스템은 두개의 스핀락 (M) 과 (Q), 그리고 세개의 CPU
22568c2ecf20Sopenharmony_ci를 가지고 있습니다; 여기에 다음의 이벤트 시퀀스가 발생합니다:
22578c2ecf20Sopenharmony_ci
22588c2ecf20Sopenharmony_ci	CPU 1				CPU 2
22598c2ecf20Sopenharmony_ci	===============================	===============================
22608c2ecf20Sopenharmony_ci	WRITE_ONCE(*A, a);		WRITE_ONCE(*E, e);
22618c2ecf20Sopenharmony_ci	ACQUIRE M			ACQUIRE Q
22628c2ecf20Sopenharmony_ci	WRITE_ONCE(*B, b);		WRITE_ONCE(*F, f);
22638c2ecf20Sopenharmony_ci	WRITE_ONCE(*C, c);		WRITE_ONCE(*G, g);
22648c2ecf20Sopenharmony_ci	RELEASE M			RELEASE Q
22658c2ecf20Sopenharmony_ci	WRITE_ONCE(*D, d);		WRITE_ONCE(*H, h);
22668c2ecf20Sopenharmony_ci
22678c2ecf20Sopenharmony_ci*A 로의 액세스부터 *H 로의 액세스까지가 어떤 순서로 CPU 3 에게 보여질지에
22688c2ecf20Sopenharmony_ci대해서는 각 CPU 에서의 락 사용에 의해 내포되어 있는 제약을 제외하고는 어떤
22698c2ecf20Sopenharmony_ci보장도 존재하지 않습니다.  예를 들어, CPU 3 에게 다음과 같은 순서로 보여지는
22708c2ecf20Sopenharmony_ci것이 가능합니다:
22718c2ecf20Sopenharmony_ci
22728c2ecf20Sopenharmony_ci	*E, ACQUIRE M, ACQUIRE Q, *G, *C, *F, *A, *B, RELEASE Q, *D, *H, RELEASE M
22738c2ecf20Sopenharmony_ci
22748c2ecf20Sopenharmony_ci하지만 다음과 같이 보이지는 않을 겁니다:
22758c2ecf20Sopenharmony_ci
22768c2ecf20Sopenharmony_ci	*B, *C or *D preceding ACQUIRE M
22778c2ecf20Sopenharmony_ci	*A, *B or *C following RELEASE M
22788c2ecf20Sopenharmony_ci	*F, *G or *H preceding ACQUIRE Q
22798c2ecf20Sopenharmony_ci	*E, *F or *G following RELEASE Q
22808c2ecf20Sopenharmony_ci
22818c2ecf20Sopenharmony_ci
22828c2ecf20Sopenharmony_ci=========================
22838c2ecf20Sopenharmony_ci메모리 배리어가 필요한 곳
22848c2ecf20Sopenharmony_ci=========================
22858c2ecf20Sopenharmony_ci
22868c2ecf20Sopenharmony_ci설령 SMP 커널을 사용하더라도 싱글 쓰레드로 동작하는 코드는 올바르게 동작하는
22878c2ecf20Sopenharmony_ci것으로 보여질 것이기 때문에, 평범한 시스템 운영중에 메모리 오퍼레이션 재배치는
22888c2ecf20Sopenharmony_ci일반적으로 문제가 되지 않습니다.  하지만, 재배치가 문제가 _될 수 있는_ 네가지
22898c2ecf20Sopenharmony_ci환경이 있습니다:
22908c2ecf20Sopenharmony_ci
22918c2ecf20Sopenharmony_ci (*) 프로세서간 상호 작용.
22928c2ecf20Sopenharmony_ci
22938c2ecf20Sopenharmony_ci (*) 어토믹 오퍼레이션.
22948c2ecf20Sopenharmony_ci
22958c2ecf20Sopenharmony_ci (*) 디바이스 액세스.
22968c2ecf20Sopenharmony_ci
22978c2ecf20Sopenharmony_ci (*) 인터럽트.
22988c2ecf20Sopenharmony_ci
22998c2ecf20Sopenharmony_ci
23008c2ecf20Sopenharmony_ci프로세서간 상호 작용
23018c2ecf20Sopenharmony_ci--------------------
23028c2ecf20Sopenharmony_ci
23038c2ecf20Sopenharmony_ci두개 이상의 프로세서를 가진 시스템이 있다면, 시스템의 두개 이상의 CPU 는 동시에
23048c2ecf20Sopenharmony_ci같은 데이터에 대한 작업을 할 수 있습니다.  이는 동기화 문제를 일으킬 수 있고,
23058c2ecf20Sopenharmony_ci이 문제를 해결하는 일반적 방법은 락을 사용하는 것입니다.  하지만, 락은 상당히
23068c2ecf20Sopenharmony_ci비용이 비싸서 가능하면 락을 사용하지 않고 일을 처리하는 것이 낫습니다.  이런
23078c2ecf20Sopenharmony_ci경우, 두 CPU 모두에 영향을 끼치는 오퍼레이션들은 오동작을 막기 위해 신중하게
23088c2ecf20Sopenharmony_ci순서가 맞춰져야 합니다.
23098c2ecf20Sopenharmony_ci
23108c2ecf20Sopenharmony_ci예를 들어, R/W 세마포어의 느린 수행경로 (slow path) 를 생각해 봅시다.
23118c2ecf20Sopenharmony_ci세마포어를 위해 대기를 하는 하나의 프로세스가 자신의 스택 중 일부를 이
23128c2ecf20Sopenharmony_ci세마포어의 대기 프로세스 리스트에 링크한 채로 있습니다:
23138c2ecf20Sopenharmony_ci
23148c2ecf20Sopenharmony_ci	struct rw_semaphore {
23158c2ecf20Sopenharmony_ci		...
23168c2ecf20Sopenharmony_ci		spinlock_t lock;
23178c2ecf20Sopenharmony_ci		struct list_head waiters;
23188c2ecf20Sopenharmony_ci	};
23198c2ecf20Sopenharmony_ci
23208c2ecf20Sopenharmony_ci	struct rwsem_waiter {
23218c2ecf20Sopenharmony_ci		struct list_head list;
23228c2ecf20Sopenharmony_ci		struct task_struct *task;
23238c2ecf20Sopenharmony_ci	};
23248c2ecf20Sopenharmony_ci
23258c2ecf20Sopenharmony_ci특정 대기 상태 프로세스를 깨우기 위해, up_read() 나 up_write() 함수는 다음과
23268c2ecf20Sopenharmony_ci같은 일을 합니다:
23278c2ecf20Sopenharmony_ci
23288c2ecf20Sopenharmony_ci (1) 다음 대기 상태 프로세스 레코드는 어디있는지 알기 위해 이 대기 상태
23298c2ecf20Sopenharmony_ci     프로세스 레코드의 next 포인터를 읽습니다;
23308c2ecf20Sopenharmony_ci
23318c2ecf20Sopenharmony_ci (2) 이 대기 상태 프로세스의 task 구조체로의 포인터를 읽습니다;
23328c2ecf20Sopenharmony_ci
23338c2ecf20Sopenharmony_ci (3) 이 대기 상태 프로세스가 세마포어를 획득했음을 알리기 위해 task
23348c2ecf20Sopenharmony_ci     포인터를 초기화 합니다;
23358c2ecf20Sopenharmony_ci
23368c2ecf20Sopenharmony_ci (4) 해당 태스크에 대해 wake_up_process() 를 호출합니다; 그리고
23378c2ecf20Sopenharmony_ci
23388c2ecf20Sopenharmony_ci (5) 해당 대기 상태 프로세스의 task 구조체를 잡고 있던 레퍼런스를 해제합니다.
23398c2ecf20Sopenharmony_ci
23408c2ecf20Sopenharmony_ci달리 말하자면, 다음 이벤트 시퀀스를 수행해야 합니다:
23418c2ecf20Sopenharmony_ci
23428c2ecf20Sopenharmony_ci	LOAD waiter->list.next;
23438c2ecf20Sopenharmony_ci	LOAD waiter->task;
23448c2ecf20Sopenharmony_ci	STORE waiter->task;
23458c2ecf20Sopenharmony_ci	CALL wakeup
23468c2ecf20Sopenharmony_ci	RELEASE task
23478c2ecf20Sopenharmony_ci
23488c2ecf20Sopenharmony_ci그리고 이 이벤트들이 다른 순서로 수행된다면, 오동작이 일어날 수 있습니다.
23498c2ecf20Sopenharmony_ci
23508c2ecf20Sopenharmony_ci한번 세마포어의 대기줄에 들어갔고 세마포어 락을 놓았다면, 해당 대기 프로세스는
23518c2ecf20Sopenharmony_ci락을 다시는 잡지 않습니다; 대신 자신의 task 포인터가 초기화 되길 기다립니다.
23528c2ecf20Sopenharmony_ci그 레코드는 대기 프로세스의 스택에 있기 때문에, 리스트의 next 포인터가 읽혀지기
23538c2ecf20Sopenharmony_ci_전에_ task 포인터가 지워진다면, 다른 CPU 는 해당 대기 프로세스를 시작해 버리고
23548c2ecf20Sopenharmony_ciup*() 함수가 next 포인터를 읽기 전에 대기 프로세스의 스택을 마구 건드릴 수
23558c2ecf20Sopenharmony_ci있습니다.
23568c2ecf20Sopenharmony_ci
23578c2ecf20Sopenharmony_ci그렇게 되면 위의 이벤트 시퀀스에 어떤 일이 일어나는지 생각해 보죠:
23588c2ecf20Sopenharmony_ci
23598c2ecf20Sopenharmony_ci	CPU 1				CPU 2
23608c2ecf20Sopenharmony_ci	===============================	===============================
23618c2ecf20Sopenharmony_ci					down_xxx()
23628c2ecf20Sopenharmony_ci					Queue waiter
23638c2ecf20Sopenharmony_ci					Sleep
23648c2ecf20Sopenharmony_ci	up_yyy()
23658c2ecf20Sopenharmony_ci	LOAD waiter->task;
23668c2ecf20Sopenharmony_ci	STORE waiter->task;
23678c2ecf20Sopenharmony_ci					Woken up by other event
23688c2ecf20Sopenharmony_ci	<preempt>
23698c2ecf20Sopenharmony_ci					Resume processing
23708c2ecf20Sopenharmony_ci					down_xxx() returns
23718c2ecf20Sopenharmony_ci					call foo()
23728c2ecf20Sopenharmony_ci					foo() clobbers *waiter
23738c2ecf20Sopenharmony_ci	</preempt>
23748c2ecf20Sopenharmony_ci	LOAD waiter->list.next;
23758c2ecf20Sopenharmony_ci	--- OOPS ---
23768c2ecf20Sopenharmony_ci
23778c2ecf20Sopenharmony_ci이 문제는 세마포어 락의 사용으로 해결될 수도 있겠지만, 그렇게 되면 깨어난 후에
23788c2ecf20Sopenharmony_cidown_xxx() 함수가 불필요하게 스핀락을 또다시 얻어야만 합니다.
23798c2ecf20Sopenharmony_ci
23808c2ecf20Sopenharmony_ci이 문제를 해결하는 방법은 범용 SMP 메모리 배리어를 추가하는 겁니다:
23818c2ecf20Sopenharmony_ci
23828c2ecf20Sopenharmony_ci	LOAD waiter->list.next;
23838c2ecf20Sopenharmony_ci	LOAD waiter->task;
23848c2ecf20Sopenharmony_ci	smp_mb();
23858c2ecf20Sopenharmony_ci	STORE waiter->task;
23868c2ecf20Sopenharmony_ci	CALL wakeup
23878c2ecf20Sopenharmony_ci	RELEASE task
23888c2ecf20Sopenharmony_ci
23898c2ecf20Sopenharmony_ci이 경우에, 배리어는 시스템의 나머지 CPU 들에게 모든 배리어 앞의 메모리 액세스가
23908c2ecf20Sopenharmony_ci배리어 뒤의 메모리 액세스보다 앞서 일어난 것으로 보이게 만듭니다.  배리어 앞의
23918c2ecf20Sopenharmony_ci메모리 액세스들이 배리어 명령 자체가 완료되는 시점까지 완료된다고는 보장하지
23928c2ecf20Sopenharmony_ci_않습니다_.
23938c2ecf20Sopenharmony_ci
23948c2ecf20Sopenharmony_ci(이게 문제가 되지 않을) 단일 프로세서 시스템에서 smp_mb() 는 실제로는 그저
23958c2ecf20Sopenharmony_ci컴파일러가 CPU 안에서의 순서를 바꾸거나 하지 않고 주어진 순서대로 명령을
23968c2ecf20Sopenharmony_ci내리도록 하는 컴파일러 배리어일 뿐입니다.  오직 하나의 CPU 만 있으니, CPU 의
23978c2ecf20Sopenharmony_ci의존성 순서 로직이 그 외의 모든것을 알아서 처리할 겁니다.
23988c2ecf20Sopenharmony_ci
23998c2ecf20Sopenharmony_ci
24008c2ecf20Sopenharmony_ci어토믹 오퍼레이션
24018c2ecf20Sopenharmony_ci-----------------
24028c2ecf20Sopenharmony_ci
24038c2ecf20Sopenharmony_ci어토믹 오퍼레이션은 기술적으로 프로세서간 상호작용으로 분류되며 그 중 일부는
24048c2ecf20Sopenharmony_ci전체 메모리 배리어를 내포하고 또 일부는 내포하지 않지만, 커널에서 상당히
24058c2ecf20Sopenharmony_ci의존적으로 사용하는 기능 중 하나입니다.
24068c2ecf20Sopenharmony_ci
24078c2ecf20Sopenharmony_ci더 많은 내용을 위해선 Documentation/atomic_t.txt 를 참고하세요.
24088c2ecf20Sopenharmony_ci
24098c2ecf20Sopenharmony_ci
24108c2ecf20Sopenharmony_ci디바이스 액세스
24118c2ecf20Sopenharmony_ci---------------
24128c2ecf20Sopenharmony_ci
24138c2ecf20Sopenharmony_ci많은 디바이스가 메모리 매핑 기법으로 제어될 수 있는데, 그렇게 제어되는
24148c2ecf20Sopenharmony_ci디바이스는 CPU 에는 단지 특정 메모리 영역의 집합처럼 보이게 됩니다.  드라이버는
24158c2ecf20Sopenharmony_ci그런 디바이스를 제어하기 위해 정확히 올바른 순서로 올바른 메모리 액세스를
24168c2ecf20Sopenharmony_ci만들어야 합니다.
24178c2ecf20Sopenharmony_ci
24188c2ecf20Sopenharmony_ci하지만, 액세스들을 재배치 하거나 조합하거나 병합하는게 더 효율적이라 판단하는
24198c2ecf20Sopenharmony_ci영리한 CPU 나 컴파일러들을 사용하면 드라이버 코드의 조심스럽게 순서 맞춰진
24208c2ecf20Sopenharmony_ci액세스들이 디바이스에는 요청된 순서대로 도착하지 못하게 할 수 있는 - 디바이스가
24218c2ecf20Sopenharmony_ci오동작을 하게 할 - 잠재적 문제가 생길 수 있습니다.
24228c2ecf20Sopenharmony_ci
24238c2ecf20Sopenharmony_ci리눅스 커널 내부에서, I/O 는 어떻게 액세스들을 적절히 순차적이게 만들 수 있는지
24248c2ecf20Sopenharmony_ci알고 있는, - inb() 나 writel() 과 같은 - 적절한 액세스 루틴을 통해 이루어져야만
24258c2ecf20Sopenharmony_ci합니다.  이것들은 대부분의 경우에는 명시적 메모리 배리어 와 함께 사용될 필요가
24268c2ecf20Sopenharmony_ci없습니다만, 완화된 메모리 액세스 속성으로 I/O 메모리 윈도우로의 참조를 위해
24278c2ecf20Sopenharmony_ci액세스 함수가 사용된다면 순서를 강제하기 위해 _mandatory_ 메모리 배리어가
24288c2ecf20Sopenharmony_ci필요합니다.
24298c2ecf20Sopenharmony_ci
24308c2ecf20Sopenharmony_ci더 많은 정보를 위해선 Documentation/driver-api/device-io.rst 를 참고하십시오.
24318c2ecf20Sopenharmony_ci
24328c2ecf20Sopenharmony_ci
24338c2ecf20Sopenharmony_ci인터럽트
24348c2ecf20Sopenharmony_ci--------
24358c2ecf20Sopenharmony_ci
24368c2ecf20Sopenharmony_ci드라이버는 자신의 인터럽트 서비스 루틴에 의해 인터럽트 당할 수 있기 때문에
24378c2ecf20Sopenharmony_ci드라이버의 이 두 부분은 서로의 디바이스 제어 또는 액세스 부분과 상호 간섭할 수
24388c2ecf20Sopenharmony_ci있습니다.
24398c2ecf20Sopenharmony_ci
24408c2ecf20Sopenharmony_ci스스로에게 인터럽트 당하는 걸 불가능하게 하고, 드라이버의 크리티컬한
24418c2ecf20Sopenharmony_ci오퍼레이션들을 모두 인터럽트가 불가능하게 된 영역에 집어넣거나 하는 방법 (락의
24428c2ecf20Sopenharmony_ci한 형태) 으로 이런 상호 간섭을 - 최소한 부분적으로라도 - 줄일 수 있습니다.
24438c2ecf20Sopenharmony_ci드라이버의 인터럽트 루틴이 실행 중인 동안, 해당 드라이버의 코어는 같은 CPU 에서
24448c2ecf20Sopenharmony_ci수행되지 않을 것이며, 현재의 인터럽트가 처리되는 중에는 또다시 인터럽트가
24458c2ecf20Sopenharmony_ci일어나지 못하도록 되어 있으니 인터럽트 핸들러는 그에 대해서는 락을 잡지 않아도
24468c2ecf20Sopenharmony_ci됩니다.
24478c2ecf20Sopenharmony_ci
24488c2ecf20Sopenharmony_ci하지만, 어드레스 레지스터와 데이터 레지스터를 갖는 이더넷 카드를 다루는
24498c2ecf20Sopenharmony_ci드라이버를 생각해 봅시다.  만약 이 드라이버의 코어가 인터럽트를 비활성화시킨
24508c2ecf20Sopenharmony_ci채로 이더넷 카드와 대화하고 드라이버의 인터럽트 핸들러가 호출되었다면:
24518c2ecf20Sopenharmony_ci
24528c2ecf20Sopenharmony_ci	LOCAL IRQ DISABLE
24538c2ecf20Sopenharmony_ci	writew(ADDR, 3);
24548c2ecf20Sopenharmony_ci	writew(DATA, y);
24558c2ecf20Sopenharmony_ci	LOCAL IRQ ENABLE
24568c2ecf20Sopenharmony_ci	<interrupt>
24578c2ecf20Sopenharmony_ci	writew(ADDR, 4);
24588c2ecf20Sopenharmony_ci	q = readw(DATA);
24598c2ecf20Sopenharmony_ci	</interrupt>
24608c2ecf20Sopenharmony_ci
24618c2ecf20Sopenharmony_ci만약 순서 규칙이 충분히 완화되어 있다면 데이터 레지스터에의 스토어는 어드레스
24628c2ecf20Sopenharmony_ci레지스터에 두번째로 행해지는 스토어 뒤에 일어날 수도 있습니다:
24638c2ecf20Sopenharmony_ci
24648c2ecf20Sopenharmony_ci	STORE *ADDR = 3, STORE *ADDR = 4, STORE *DATA = y, q = LOAD *DATA
24658c2ecf20Sopenharmony_ci
24668c2ecf20Sopenharmony_ci
24678c2ecf20Sopenharmony_ci만약 순서 규칙이 충분히 완화되어 있고 묵시적으로든 명시적으로든 배리어가
24688c2ecf20Sopenharmony_ci사용되지 않았다면 인터럽트 비활성화 섹션에서 일어난 액세스가 바깥으로 새어서
24698c2ecf20Sopenharmony_ci인터럽트 내에서 일어난 액세스와 섞일 수 있다고 - 그리고 그 반대도 - 가정해야만
24708c2ecf20Sopenharmony_ci합니다.
24718c2ecf20Sopenharmony_ci
24728c2ecf20Sopenharmony_ci그런 영역 안에서 일어나는 I/O 액세스는 묵시적 I/O 배리어를 형성하는, 엄격한
24738c2ecf20Sopenharmony_ci순서 규칙의 I/O 레지스터로의 로드 오퍼레이션을 포함하기 때문에 일반적으로는
24748c2ecf20Sopenharmony_ci문제가 되지 않습니다.
24758c2ecf20Sopenharmony_ci
24768c2ecf20Sopenharmony_ci
24778c2ecf20Sopenharmony_ci하나의 인터럽트 루틴과 별도의 CPU 에서 수행중이며 서로 통신을 하는 두 루틴
24788c2ecf20Sopenharmony_ci사이에도 비슷한 상황이 일어날 수 있습니다.  만약 그런 경우가 발생할 가능성이
24798c2ecf20Sopenharmony_ci있다면, 순서를 보장하기 위해 인터럽트 비활성화 락이 사용되어져야만 합니다.
24808c2ecf20Sopenharmony_ci
24818c2ecf20Sopenharmony_ci
24828c2ecf20Sopenharmony_ci======================
24838c2ecf20Sopenharmony_ci커널 I/O 배리어의 효과
24848c2ecf20Sopenharmony_ci======================
24858c2ecf20Sopenharmony_ci
24868c2ecf20Sopenharmony_ciI/O 액세스를 통한 주변장치와의 통신은 아키텍쳐와 기기에 매우 종속적입니다.
24878c2ecf20Sopenharmony_ci따라서, 본질적으로 이식성이 없는 드라이버는 가능한 가장 적은 오버헤드로
24888c2ecf20Sopenharmony_ci동기화를 하기 위해 각자의 타겟 시스템의 특정 동작에 의존할 겁니다.  다양한
24898c2ecf20Sopenharmony_ci아키텍쳐와 버스 구현에 이식성을 가지려 하는 드라이버를 위해, 커널은 다양한
24908c2ecf20Sopenharmony_ci정도의 순서 보장을 제공하는 일련의 액세스 함수를 제공합니다.
24918c2ecf20Sopenharmony_ci
24928c2ecf20Sopenharmony_ci (*) readX(), writeX():
24938c2ecf20Sopenharmony_ci
24948c2ecf20Sopenharmony_ci	readX() 와 writeX() MMIO 액세스 함수는 접근되는 주변장치로의 포인터를
24958c2ecf20Sopenharmony_ci	__iomem * 패러미터로 받습니다.  디폴트 I/O 기능으로 매핑되는 포인터
24968c2ecf20Sopenharmony_ci	(예: ioremap() 으로 반환되는 것) 의 순서 보장은 다음과 같습니다:
24978c2ecf20Sopenharmony_ci
24988c2ecf20Sopenharmony_ci	1. 같은 주변장치로의 모든 readX() 와 writeX() 액세스는 각자에 대해
24998c2ecf20Sopenharmony_ci	   순서지어집니다.  이는 같은 CPU 쓰레드에 의한 특정 디바이스로의 MMIO
25008c2ecf20Sopenharmony_ci	   레지스터 액세스가 프로그램 순서대로 도착할 것을 보장합니다.
25018c2ecf20Sopenharmony_ci
25028c2ecf20Sopenharmony_ci	2. 한 스핀락을 잡은 CPU 쓰레드에 의한 writeX() 는 같은 스핀락을 나중에
25038c2ecf20Sopenharmony_ci	   잡은 다른 CPU 쓰레드에 의해 같은 주변장치를 향해 호출된 writeX()
25048c2ecf20Sopenharmony_ci	   앞으로 순서지어집니다.  이는 스핀락을 잡은 채 특정 디바이스를 향해
25058c2ecf20Sopenharmony_ci	   호출된 MMIO 레지스터 쓰기는 해당 락의 획득에 일관적인 순서로 도달할
25068c2ecf20Sopenharmony_ci	   것을 보장합니다.
25078c2ecf20Sopenharmony_ci
25088c2ecf20Sopenharmony_ci	3. 특정 주변장치를 향한 특정 CPU 쓰레드의 writeX() 는 먼저 해당
25098c2ecf20Sopenharmony_ci	   쓰레드로 전파되는, 또는 해당 쓰레드에 의해 요청된 모든 앞선 메모리
25108c2ecf20Sopenharmony_ci	   쓰기가 완료되기 전까지 먼저 기다립니다.  이는 dma_alloc_coherent()
25118c2ecf20Sopenharmony_ci	   를 통해 할당된 전송용 DMA 버퍼로의 해당 CPU 의 쓰기가 이 CPU 가 이
25128c2ecf20Sopenharmony_ci	   전송을 시작시키기 위해 MMIO 컨트롤 레지스터에 쓰기를 할 때 DMA
25138c2ecf20Sopenharmony_ci	   엔진에 보여질 것을 보장합니다.
25148c2ecf20Sopenharmony_ci
25158c2ecf20Sopenharmony_ci	4. 특정 CPU 쓰레드에 의한 주변장치로의 readX() 는 같은 쓰레드에 의한
25168c2ecf20Sopenharmony_ci	   모든 뒤따르는 메모리 읽기가 시작되기 전에 완료됩니다.  이는
25178c2ecf20Sopenharmony_ci	   dma_alloc_coherent() 를 통해 할당된 수신용 DMA 버퍼로부터의 CPU 의
25188c2ecf20Sopenharmony_ci	   읽기는 이 DMA 수신의 완료를 표시하는 DMA 엔진의 MMIO 상태 레지스터
25198c2ecf20Sopenharmony_ci	   읽기 후에는 오염된 데이터를 읽지 않을 것을 보장합니다.
25208c2ecf20Sopenharmony_ci
25218c2ecf20Sopenharmony_ci	5. CPU 에 의한 주변장치로의 readX() 는 모든 뒤따르는 delay() 루프가
25228c2ecf20Sopenharmony_ci	   수행을 시작하기 전에 완료됩니다.  이는 CPU 의 특정
25238c2ecf20Sopenharmony_ci	   주변장치로의 두개의 MMIO 레지스터 쓰기가 행해지는데 첫번째 쓰기가
25248c2ecf20Sopenharmony_ci	   readX() 를 통해 곧바로 읽어졌고 이어 두번째 writeX() 전에 udelay(1)
25258c2ecf20Sopenharmony_ci	   이 호출되었다면 이 두개의 쓰기는 최소 1us 의 간격을 두고 행해질 것을
25268c2ecf20Sopenharmony_ci	   보장합니다:
25278c2ecf20Sopenharmony_ci
25288c2ecf20Sopenharmony_ci		writel(42, DEVICE_REGISTER_0); // 디바이스에 도착함...
25298c2ecf20Sopenharmony_ci		readl(DEVICE_REGISTER_0);
25308c2ecf20Sopenharmony_ci		udelay(1);
25318c2ecf20Sopenharmony_ci		writel(42, DEVICE_REGISTER_1); // ...이것보다 최소 1us 전에.
25328c2ecf20Sopenharmony_ci
25338c2ecf20Sopenharmony_ci	디폴트가 아닌 기능을 통해 얻어지는 __iomem 포인터 (예: ioremap_wc() 를
25348c2ecf20Sopenharmony_ci	통해 리턴되는 것) 의 순서 속성은 실제 아키텍쳐에 의존적이어서 이런
25358c2ecf20Sopenharmony_ci	종류의 매핑으로의 액세스는 앞서 설명된 보장사항에 의존할 수 없습니다.
25368c2ecf20Sopenharmony_ci
25378c2ecf20Sopenharmony_ci (*) readX_relaxed(), writeX_relaxed()
25388c2ecf20Sopenharmony_ci
25398c2ecf20Sopenharmony_ci	이것들은 readX() 와 writeX() 랑 비슷하지만, 더 완화된 메모리 순서
25408c2ecf20Sopenharmony_ci	보장을 제공합니다.  구체적으로, 이것들은 일반적 메모리 액세스나 delay()
25418c2ecf20Sopenharmony_ci	루프 (예:앞의 2-5 항목) 에 대해 순서를 보장하지 않습니다만 디폴트 I/O
25428c2ecf20Sopenharmony_ci	기능으로 매핑된 __iomem 포인터에 대해 동작할 때, 같은 CPU 쓰레드에 의한
25438c2ecf20Sopenharmony_ci	같은 주변장치로의 액세스에는 순서가 맞춰질 것이 보장됩니다.
25448c2ecf20Sopenharmony_ci
25458c2ecf20Sopenharmony_ci (*) readsX(), writesX():
25468c2ecf20Sopenharmony_ci
25478c2ecf20Sopenharmony_ci	readsX() 와 writesX() MMIO 액세스 함수는 DMA 를 수행하는데 적절치 않은,
25488c2ecf20Sopenharmony_ci	주변장치 내의 메모리 매핑된 레지스터 기반 FIFO 로의 액세스를 위해
25498c2ecf20Sopenharmony_ci	설계되었습니다.  따라서, 이 기능들은 앞서 설명된 readX_relaxed() 와
25508c2ecf20Sopenharmony_ci	writeX_relaxed() 의 순서 보장만을 제공합니다.
25518c2ecf20Sopenharmony_ci
25528c2ecf20Sopenharmony_ci (*) inX(), outX():
25538c2ecf20Sopenharmony_ci
25548c2ecf20Sopenharmony_ci	inX() 와 outX() 액세스 함수는 일부 아키텍쳐 (특히 x86) 에서는 특수한
25558c2ecf20Sopenharmony_ci	명령어를 필요로 하며 포트에 매핑되는, 과거의 유산인 I/O 주변장치로의
25568c2ecf20Sopenharmony_ci	접근을 위해 만들어졌습니다.
25578c2ecf20Sopenharmony_ci
25588c2ecf20Sopenharmony_ci	많은 CPU 아키텍쳐가 결국은 이런 주변장치를 내부의 가상 메모리 매핑을
25598c2ecf20Sopenharmony_ci	통해 접근하기 때문에, inX() 와 outX() 가 제공하는 이식성 있는 순서
25608c2ecf20Sopenharmony_ci	보장은 디폴트 I/O 기능을 통한 매핑을 접근할 때의 readX() 와 writeX() 에
25618c2ecf20Sopenharmony_ci	의해 제공되는 것과 각각 동일합니다.
25628c2ecf20Sopenharmony_ci
25638c2ecf20Sopenharmony_ci	디바이스 드라이버는 outX() 가 리턴하기 전에 해당 I/O 주변장치로부터의
25648c2ecf20Sopenharmony_ci	완료 응답을 기다리는 쓰기 트랜잭션을 만들어 낸다고 기대할 수도
25658c2ecf20Sopenharmony_ci	있습니다.  이는 모든 아키텍쳐에서 보장되지는 않고, 따라서 이식성 있는
25668c2ecf20Sopenharmony_ci	순서 규칙의 일부분이 아닙니다.
25678c2ecf20Sopenharmony_ci
25688c2ecf20Sopenharmony_ci (*) insX(), outsX():
25698c2ecf20Sopenharmony_ci
25708c2ecf20Sopenharmony_ci	앞에서와 같이, insX() 와 outsX() 액세스 함수는 디폴트 I/O 기능을 통한
25718c2ecf20Sopenharmony_ci	매핑을 접근할 때 각각 readX() 와 writeX() 와 같은 순서 보장을
25728c2ecf20Sopenharmony_ci	제공합니다.
25738c2ecf20Sopenharmony_ci
25748c2ecf20Sopenharmony_ci (*) ioreadX(), iowriteX()
25758c2ecf20Sopenharmony_ci
25768c2ecf20Sopenharmony_ci	이것들은 inX()/outX() 나 readX()/writeX() 처럼 실제로 수행하는 액세스의
25778c2ecf20Sopenharmony_ci	종류에 따라 적절하게 수행될 것입니다.
25788c2ecf20Sopenharmony_ci
25798c2ecf20Sopenharmony_ciString 액세스 함수 (insX(), outsX(), readsX() 그리고 writesX()) 의 예외를
25808c2ecf20Sopenharmony_ci제외하고는, 앞의 모든 것이 아랫단의 주변장치가 little-endian 이라 가정하며,
25818c2ecf20Sopenharmony_ci따라서 big-endian 아키텍쳐에서는 byte-swapping 오퍼레이션을 수행합니다.
25828c2ecf20Sopenharmony_ci
25838c2ecf20Sopenharmony_ci
25848c2ecf20Sopenharmony_ci===================================
25858c2ecf20Sopenharmony_ci가정되는 가장 완화된 실행 순서 모델
25868c2ecf20Sopenharmony_ci===================================
25878c2ecf20Sopenharmony_ci
25888c2ecf20Sopenharmony_ci컨셉적으로 CPU 는 주어진 프로그램에 대해 프로그램 그 자체에는 인과성 (program
25898c2ecf20Sopenharmony_cicausality) 을 지키는 것처럼 보이게 하지만 일반적으로는 순서를 거의 지켜주지
25908c2ecf20Sopenharmony_ci않는다고 가정되어야만 합니다.  (i386 이나 x86_64 같은) 일부 CPU 들은 코드
25918c2ecf20Sopenharmony_ci재배치에 (powerpc 나 frv 와 같은) 다른 것들에 비해 강한 제약을 갖지만, 아키텍쳐
25928c2ecf20Sopenharmony_ci종속적 코드 이외의 코드에서는 순서에 대한 제약이 가장 완화된 경우 (DEC Alpha)
25938c2ecf20Sopenharmony_ci를 가정해야 합니다.
25948c2ecf20Sopenharmony_ci
25958c2ecf20Sopenharmony_ci이 말은, CPU 에게 주어지는 인스트럭션 스트림 내의 한 인스트럭션이 앞의
25968c2ecf20Sopenharmony_ci인스트럭션에 종속적이라면 앞의 인스트럭션은 뒤의 종속적 인스트럭션이 실행되기
25978c2ecf20Sopenharmony_ci전에 완료[*]될 수 있어야 한다는 제약 (달리 말해서, 인과성이 지켜지는 것으로
25988c2ecf20Sopenharmony_ci보이게 함) 외에는 자신이 원하는 순서대로 - 심지어 병렬적으로도 - 그 스트림을
25998c2ecf20Sopenharmony_ci실행할 수 있음을 의미합니다
26008c2ecf20Sopenharmony_ci
26018c2ecf20Sopenharmony_ci [*] 일부 인스트럭션은 하나 이상의 영향 - 조건 코드를 바꾼다던지, 레지스터나
26028c2ecf20Sopenharmony_ci     메모리를 바꾼다던지 - 을 만들어내며, 다른 인스트럭션은 다른 효과에
26038c2ecf20Sopenharmony_ci     종속적일 수 있습니다.
26048c2ecf20Sopenharmony_ci
26058c2ecf20Sopenharmony_ciCPU 는 최종적으로 아무 효과도 만들지 않는 인스트럭션 시퀀스는 없애버릴 수도
26068c2ecf20Sopenharmony_ci있습니다.  예를 들어, 만약 두개의 연속되는 인스트럭션이 둘 다 같은 레지스터에
26078c2ecf20Sopenharmony_ci직접적인 값 (immediate value) 을 집어넣는다면, 첫번째 인스트럭션은 버려질 수도
26088c2ecf20Sopenharmony_ci있습니다.
26098c2ecf20Sopenharmony_ci
26108c2ecf20Sopenharmony_ci
26118c2ecf20Sopenharmony_ci비슷하게, 컴파일러 역시 프로그램의 인과성만 지켜준다면 인스트럭션 스트림을
26128c2ecf20Sopenharmony_ci자신이 보기에 올바르다 생각되는대로 재배치 할 수 있습니다.
26138c2ecf20Sopenharmony_ci
26148c2ecf20Sopenharmony_ci
26158c2ecf20Sopenharmony_ci===============
26168c2ecf20Sopenharmony_ciCPU 캐시의 영향
26178c2ecf20Sopenharmony_ci===============
26188c2ecf20Sopenharmony_ci
26198c2ecf20Sopenharmony_ci캐시된 메모리 오퍼레이션들이 시스템 전체에 어떻게 인지되는지는 CPU 와 메모리
26208c2ecf20Sopenharmony_ci사이에 존재하는 캐시들, 그리고 시스템 상태의 일관성을 관리하는 메모리 일관성
26218c2ecf20Sopenharmony_ci시스템에 상당 부분 영향을 받습니다.
26228c2ecf20Sopenharmony_ci
26238c2ecf20Sopenharmony_ci한 CPU 가 시스템의 다른 부분들과 캐시를 통해 상호작용한다면, 메모리 시스템은
26248c2ecf20Sopenharmony_ciCPU 의 캐시들을 포함해야 하며, CPU 와 CPU 자신의 캐시 사이에서의 동작을 위한
26258c2ecf20Sopenharmony_ci메모리 배리어를 가져야 합니다. (메모리 배리어는 논리적으로는 다음 그림의
26268c2ecf20Sopenharmony_ci점선에서 동작합니다):
26278c2ecf20Sopenharmony_ci
26288c2ecf20Sopenharmony_ci	    <--- CPU --->         :       <----------- Memory ----------->
26298c2ecf20Sopenharmony_ci	                          :
26308c2ecf20Sopenharmony_ci	+--------+    +--------+  :   +--------+    +-----------+
26318c2ecf20Sopenharmony_ci	|        |    |        |  :   |        |    |           |    +--------+
26328c2ecf20Sopenharmony_ci	|  CPU   |    | Memory |  :   | CPU    |    |           |    |        |
26338c2ecf20Sopenharmony_ci	|  Core  |--->| Access |----->| Cache  |<-->|           |    |        |
26348c2ecf20Sopenharmony_ci	|        |    | Queue  |  :   |        |    |           |--->| Memory |
26358c2ecf20Sopenharmony_ci	|        |    |        |  :   |        |    |           |    |        |
26368c2ecf20Sopenharmony_ci	+--------+    +--------+  :   +--------+    |           |    |        |
26378c2ecf20Sopenharmony_ci	                          :                 | Cache     |    +--------+
26388c2ecf20Sopenharmony_ci	                          :                 | Coherency |
26398c2ecf20Sopenharmony_ci	                          :                 | Mechanism |    +--------+
26408c2ecf20Sopenharmony_ci	+--------+    +--------+  :   +--------+    |           |    |	      |
26418c2ecf20Sopenharmony_ci	|        |    |        |  :   |        |    |           |    |        |
26428c2ecf20Sopenharmony_ci	|  CPU   |    | Memory |  :   | CPU    |    |           |--->| Device |
26438c2ecf20Sopenharmony_ci	|  Core  |--->| Access |----->| Cache  |<-->|           |    |        |
26448c2ecf20Sopenharmony_ci	|        |    | Queue  |  :   |        |    |           |    |        |
26458c2ecf20Sopenharmony_ci	|        |    |        |  :   |        |    |           |    +--------+
26468c2ecf20Sopenharmony_ci	+--------+    +--------+  :   +--------+    +-----------+
26478c2ecf20Sopenharmony_ci	                          :
26488c2ecf20Sopenharmony_ci	                          :
26498c2ecf20Sopenharmony_ci
26508c2ecf20Sopenharmony_ci특정 로드나 스토어는 해당 오퍼레이션을 요청한 CPU 의 캐시 내에서 동작을 완료할
26518c2ecf20Sopenharmony_ci수도 있기 때문에 해당 CPU 의 바깥에는 보이지 않을 수 있지만, 다른 CPU 가 관심을
26528c2ecf20Sopenharmony_ci갖는다면 캐시 일관성 메커니즘이 해당 캐시라인을 해당 CPU 에게 전달하고, 해당
26538c2ecf20Sopenharmony_ci메모리 영역에 대한 오퍼레이션이 발생할 때마다 그 영향을 전파시키기 때문에, 해당
26548c2ecf20Sopenharmony_ci오퍼레이션은 메모리에 실제로 액세스를 한것처럼 나타날 것입니다.
26558c2ecf20Sopenharmony_ci
26568c2ecf20Sopenharmony_ciCPU 코어는 프로그램의 인과성이 유지된다고만 여겨진다면 인스트럭션들을 어떤
26578c2ecf20Sopenharmony_ci순서로든 재배치해서 수행할 수 있습니다.  일부 인스트럭션들은 로드나 스토어
26588c2ecf20Sopenharmony_ci오퍼레이션을 만드는데 이 오퍼레이션들은 이후 수행될 메모리 액세스 큐에 들어가게
26598c2ecf20Sopenharmony_ci됩니다.  코어는 이 오퍼레이션들을 해당 큐에 어떤 순서로든 원하는대로 넣을 수
26608c2ecf20Sopenharmony_ci있고, 다른 인스트럭션의 완료를 기다리도록 강제되기 전까지는 수행을 계속합니다.
26618c2ecf20Sopenharmony_ci
26628c2ecf20Sopenharmony_ci메모리 배리어가 하는 일은 CPU 쪽에서 메모리 쪽으로 넘어가는 액세스들의 순서,
26638c2ecf20Sopenharmony_ci그리고 그 액세스의 결과가 시스템의 다른 관찰자들에게 인지되는 순서를 제어하는
26648c2ecf20Sopenharmony_ci것입니다.
26658c2ecf20Sopenharmony_ci
26668c2ecf20Sopenharmony_ci[!] CPU 들은 항상 그들 자신의 로드와 스토어는 프로그램 순서대로 일어난 것으로
26678c2ecf20Sopenharmony_ci보기 때문에, 주어진 CPU 내에서는 메모리 배리어를 사용할 필요가 _없습니다_.
26688c2ecf20Sopenharmony_ci
26698c2ecf20Sopenharmony_ci[!] MMIO 나 다른 디바이스 액세스들은 캐시 시스템을 우회할 수도 있습니다.  우회
26708c2ecf20Sopenharmony_ci여부는 디바이스가 액세스 되는 메모리 윈도우의 특성에 의해 결정될 수도 있고, CPU
26718c2ecf20Sopenharmony_ci가 가지고 있을 수 있는 특수한 디바이스 통신 인스트럭션의 사용에 의해서 결정될
26728c2ecf20Sopenharmony_ci수도 있습니다.
26738c2ecf20Sopenharmony_ci
26748c2ecf20Sopenharmony_ci
26758c2ecf20Sopenharmony_ci캐시 일관성 VS DMA
26768c2ecf20Sopenharmony_ci------------------
26778c2ecf20Sopenharmony_ci
26788c2ecf20Sopenharmony_ci모든 시스템이 DMA 를 하는 디바이스에 대해서까지 캐시 일관성을 유지하지는
26798c2ecf20Sopenharmony_ci않습니다.  그런 경우, DMA 를 시도하는 디바이스는 RAM 으로부터 잘못된 데이터를
26808c2ecf20Sopenharmony_ci읽을 수 있는데, 더티 캐시 라인이 CPU 의 캐시에 머무르고 있고, 바뀐 값이 아직
26818c2ecf20Sopenharmony_ciRAM 에 써지지 않았을 수 있기 때문입니다.  이 문제를 해결하기 위해선, 커널의
26828c2ecf20Sopenharmony_ci적절한 부분에서 각 CPU 캐시의 문제되는 비트들을 플러시 (flush) 시켜야만 합니다
26838c2ecf20Sopenharmony_ci(그리고 그것들을 무효화 - invalidation - 시킬 수도 있겠죠).
26848c2ecf20Sopenharmony_ci
26858c2ecf20Sopenharmony_ci또한, 디바이스에 의해 RAM 에 DMA 로 쓰여진 값은 디바이스가 쓰기를 완료한 후에
26868c2ecf20Sopenharmony_ciCPU 의 캐시에서 RAM 으로 쓰여지는 더티 캐시 라인에 의해 덮어써질 수도 있고, CPU
26878c2ecf20Sopenharmony_ci의 캐시에 존재하는 캐시 라인이 해당 캐시에서 삭제되고 다시 값을 읽어들이기
26888c2ecf20Sopenharmony_ci전까지는 RAM 이 업데이트 되었다는 사실 자체가 숨겨져 버릴 수도 있습니다.  이
26898c2ecf20Sopenharmony_ci문제를 해결하기 위해선, 커널의 적절한 부분에서 각 CPU 의 캐시 안의 문제가 되는
26908c2ecf20Sopenharmony_ci비트들을 무효화 시켜야 합니다.
26918c2ecf20Sopenharmony_ci
26928c2ecf20Sopenharmony_ci캐시 관리에 대한 더 많은 정보를 위해선 Documentation/core-api/cachetlb.rst26938c2ecf20Sopenharmony_ci참고하세요.
26948c2ecf20Sopenharmony_ci
26958c2ecf20Sopenharmony_ci
26968c2ecf20Sopenharmony_ci캐시 일관성 VS MMIO
26978c2ecf20Sopenharmony_ci-------------------
26988c2ecf20Sopenharmony_ci
26998c2ecf20Sopenharmony_ciMemory mapped I/O 는 일반적으로 CPU 의 메모리 공간 내의 한 윈도우의 특정 부분
27008c2ecf20Sopenharmony_ci내의 메모리 지역에 이루어지는데, 이 윈도우는 일반적인, RAM 으로 향하는
27018c2ecf20Sopenharmony_ci윈도우와는 다른 특성을 갖습니다.
27028c2ecf20Sopenharmony_ci
27038c2ecf20Sopenharmony_ci그런 특성 가운데 하나는, 일반적으로 그런 액세스는 캐시를 완전히 우회하고
27048c2ecf20Sopenharmony_ci디바이스 버스로 곧바로 향한다는 것입니다.  이 말은 MMIO 액세스는 먼저
27058c2ecf20Sopenharmony_ci시작되어서 캐시에서 완료된 메모리 액세스를 추월할 수 있다는 뜻입니다.  이런
27068c2ecf20Sopenharmony_ci경우엔 메모리 배리어만으로는 충분치 않고, 만약 캐시된 메모리 쓰기 오퍼레이션과
27078c2ecf20Sopenharmony_ciMMIO 액세스가 어떤 방식으로든 의존적이라면 해당 캐시는 두 오퍼레이션 사이에
27088c2ecf20Sopenharmony_ci비워져(flush)야만 합니다.
27098c2ecf20Sopenharmony_ci
27108c2ecf20Sopenharmony_ci
27118c2ecf20Sopenharmony_ci======================
27128c2ecf20Sopenharmony_ciCPU 들이 저지르는 일들
27138c2ecf20Sopenharmony_ci======================
27148c2ecf20Sopenharmony_ci
27158c2ecf20Sopenharmony_ci프로그래머는 CPU 가 메모리 오퍼레이션들을 정확히 요청한대로 수행해 줄 것이라고
27168c2ecf20Sopenharmony_ci생각하는데, 예를 들어 다음과 같은 코드를 CPU 에게 넘긴다면:
27178c2ecf20Sopenharmony_ci
27188c2ecf20Sopenharmony_ci	a = READ_ONCE(*A);
27198c2ecf20Sopenharmony_ci	WRITE_ONCE(*B, b);
27208c2ecf20Sopenharmony_ci	c = READ_ONCE(*C);
27218c2ecf20Sopenharmony_ci	d = READ_ONCE(*D);
27228c2ecf20Sopenharmony_ci	WRITE_ONCE(*E, e);
27238c2ecf20Sopenharmony_ci
27248c2ecf20Sopenharmony_ciCPU 는 다음 인스트럭션을 처리하기 전에 현재의 인스트럭션을 위한 메모리
27258c2ecf20Sopenharmony_ci오퍼레이션을 완료할 것이라 생각하고, 따라서 시스템 외부에서 관찰하기에도 정해진
27268c2ecf20Sopenharmony_ci순서대로 오퍼레이션이 수행될 것으로 예상합니다:
27278c2ecf20Sopenharmony_ci
27288c2ecf20Sopenharmony_ci	LOAD *A, STORE *B, LOAD *C, LOAD *D, STORE *E.
27298c2ecf20Sopenharmony_ci
27308c2ecf20Sopenharmony_ci
27318c2ecf20Sopenharmony_ci당연하지만, 실제로는 훨씬 엉망입니다.  많은 CPU 와 컴파일러에서 앞의 가정은
27328c2ecf20Sopenharmony_ci성립하지 못하는데 그 이유는 다음과 같습니다:
27338c2ecf20Sopenharmony_ci
27348c2ecf20Sopenharmony_ci (*) 로드 오퍼레이션들은 실행을 계속 해나가기 위해 곧바로 완료될 필요가 있는
27358c2ecf20Sopenharmony_ci     경우가 많은 반면, 스토어 오퍼레이션들은 종종 별다른 문제 없이 유예될 수
27368c2ecf20Sopenharmony_ci     있습니다;
27378c2ecf20Sopenharmony_ci
27388c2ecf20Sopenharmony_ci (*) 로드 오퍼레이션들은 예측적으로 수행될 수 있으며, 필요없는 로드였다고
27398c2ecf20Sopenharmony_ci     증명된 예측적 로드의 결과는 버려집니다;
27408c2ecf20Sopenharmony_ci
27418c2ecf20Sopenharmony_ci (*) 로드 오퍼레이션들은 예측적으로 수행될 수 있으므로, 예상된 이벤트의
27428c2ecf20Sopenharmony_ci     시퀀스와 다른 시간에 로드가 이뤄질 수 있습니다;
27438c2ecf20Sopenharmony_ci
27448c2ecf20Sopenharmony_ci (*) 메모리 액세스 순서는 CPU 버스와 캐시를 좀 더 잘 사용할 수 있도록 재배치
27458c2ecf20Sopenharmony_ci     될 수 있습니다;
27468c2ecf20Sopenharmony_ci
27478c2ecf20Sopenharmony_ci (*) 로드와 스토어는 인접한 위치에의 액세스들을 일괄적으로 처리할 수 있는
27488c2ecf20Sopenharmony_ci     메모리나 I/O 하드웨어 (메모리와 PCI 디바이스 둘 다 이게 가능할 수
27498c2ecf20Sopenharmony_ci     있습니다) 에 대해 요청되는 경우, 개별 오퍼레이션을 위한 트랜잭션 설정
27508c2ecf20Sopenharmony_ci     비용을 아끼기 위해 조합되어 실행될 수 있습니다; 그리고
27518c2ecf20Sopenharmony_ci
27528c2ecf20Sopenharmony_ci (*) 해당 CPU 의 데이터 캐시가 순서에 영향을 끼칠 수도 있고, 캐시 일관성
27538c2ecf20Sopenharmony_ci     메커니즘이 - 스토어가 실제로 캐시에 도달한다면 - 이 문제를 완화시킬 수는
27548c2ecf20Sopenharmony_ci     있지만 이 일관성 관리가 다른 CPU 들에도 같은 순서로 전달된다는 보장은
27558c2ecf20Sopenharmony_ci     없습니다.
27568c2ecf20Sopenharmony_ci
27578c2ecf20Sopenharmony_ci따라서, 앞의 코드에 대해 다른 CPU 가 보는 결과는 다음과 같을 수 있습니다:
27588c2ecf20Sopenharmony_ci
27598c2ecf20Sopenharmony_ci	LOAD *A, ..., LOAD {*C,*D}, STORE *E, STORE *B
27608c2ecf20Sopenharmony_ci
27618c2ecf20Sopenharmony_ci	("LOAD {*C,*D}" 는 조합된 로드입니다)
27628c2ecf20Sopenharmony_ci
27638c2ecf20Sopenharmony_ci
27648c2ecf20Sopenharmony_ci하지만, CPU 는 스스로는 일관적일 것을 보장합니다: CPU _자신_ 의 액세스들은
27658c2ecf20Sopenharmony_ci자신에게는 메모리 배리어가 없음에도 불구하고 정확히 순서 세워진 것으로 보여질
27668c2ecf20Sopenharmony_ci것입니다.  예를 들어 다음의 코드가 주어졌다면:
27678c2ecf20Sopenharmony_ci
27688c2ecf20Sopenharmony_ci	U = READ_ONCE(*A);
27698c2ecf20Sopenharmony_ci	WRITE_ONCE(*A, V);
27708c2ecf20Sopenharmony_ci	WRITE_ONCE(*A, W);
27718c2ecf20Sopenharmony_ci	X = READ_ONCE(*A);
27728c2ecf20Sopenharmony_ci	WRITE_ONCE(*A, Y);
27738c2ecf20Sopenharmony_ci	Z = READ_ONCE(*A);
27748c2ecf20Sopenharmony_ci
27758c2ecf20Sopenharmony_ci그리고 외부의 영향에 의한 간섭이 없다고 가정하면, 최종 결과는 다음과 같이
27768c2ecf20Sopenharmony_ci나타날 것이라고 예상될 수 있습니다:
27778c2ecf20Sopenharmony_ci
27788c2ecf20Sopenharmony_ci	U == *A 의 최초 값
27798c2ecf20Sopenharmony_ci	X == W
27808c2ecf20Sopenharmony_ci	Z == Y
27818c2ecf20Sopenharmony_ci	*A == Y
27828c2ecf20Sopenharmony_ci
27838c2ecf20Sopenharmony_ci앞의 코드는 CPU 가 다음의 메모리 액세스 시퀀스를 만들도록 할겁니다:
27848c2ecf20Sopenharmony_ci
27858c2ecf20Sopenharmony_ci	U=LOAD *A, STORE *A=V, STORE *A=W, X=LOAD *A, STORE *A=Y, Z=LOAD *A
27868c2ecf20Sopenharmony_ci
27878c2ecf20Sopenharmony_ci하지만, 별다른 개입이 없고 프로그램의 시야에 이 세상이 여전히 일관적이라고
27888c2ecf20Sopenharmony_ci보인다는 보장만 지켜진다면 이 시퀀스는 어떤 조합으로든 재구성될 수 있으며, 각
27898c2ecf20Sopenharmony_ci액세스들은 합쳐지거나 버려질 수 있습니다.  일부 아키텍쳐에서 CPU 는 같은 위치에
27908c2ecf20Sopenharmony_ci대한 연속적인 로드 오퍼레이션들을 재배치 할 수 있기 때문에 앞의 예에서의
27918c2ecf20Sopenharmony_ciREAD_ONCE() 와 WRITE_ONCE() 는 반드시 존재해야 함을 알아두세요.  그런 종류의
27928c2ecf20Sopenharmony_ci아키텍쳐에서 READ_ONCE() 와 WRITE_ONCE() 는 이 문제를 막기 위해 필요한 일을
27938c2ecf20Sopenharmony_ci뭐가 됐든지 하게 되는데, 예를 들어 Itanium 에서는 READ_ONCE() 와 WRITE_ONCE()
27948c2ecf20Sopenharmony_ci가 사용하는 volatile 캐스팅은 GCC 가 그런 재배치를 방지하는 특수 인스트럭션인
27958c2ecf20Sopenharmony_cild.acqstl.rel 인스트럭션을 각각 만들어 내도록 합니다.
27968c2ecf20Sopenharmony_ci
27978c2ecf20Sopenharmony_ci컴파일러 역시 이 시퀀스의 액세스들을 CPU 가 보기도 전에 합치거나 버리거나 뒤로
27988c2ecf20Sopenharmony_ci미뤄버릴 수 있습니다.
27998c2ecf20Sopenharmony_ci
28008c2ecf20Sopenharmony_ci예를 들어:
28018c2ecf20Sopenharmony_ci
28028c2ecf20Sopenharmony_ci	*A = V;
28038c2ecf20Sopenharmony_ci	*A = W;
28048c2ecf20Sopenharmony_ci
28058c2ecf20Sopenharmony_ci는 다음과 같이 변형될 수 있습니다:
28068c2ecf20Sopenharmony_ci
28078c2ecf20Sopenharmony_ci	*A = W;
28088c2ecf20Sopenharmony_ci
28098c2ecf20Sopenharmony_ci따라서, 쓰기 배리어나 WRITE_ONCE() 가 없다면 *A 로의 V 값의 저장의 효과는
28108c2ecf20Sopenharmony_ci사라진다고 가정될 수 있습니다.  비슷하게:
28118c2ecf20Sopenharmony_ci
28128c2ecf20Sopenharmony_ci	*A = Y;
28138c2ecf20Sopenharmony_ci	Z = *A;
28148c2ecf20Sopenharmony_ci
28158c2ecf20Sopenharmony_ci는, 메모리 배리어나 READ_ONCE() 와 WRITE_ONCE() 없이는 다음과 같이 변형될 수
28168c2ecf20Sopenharmony_ci있습니다:
28178c2ecf20Sopenharmony_ci
28188c2ecf20Sopenharmony_ci	*A = Y;
28198c2ecf20Sopenharmony_ci	Z = Y;
28208c2ecf20Sopenharmony_ci
28218c2ecf20Sopenharmony_ci그리고 이 LOAD 오퍼레이션은 CPU 바깥에는 아예 보이지 않습니다.
28228c2ecf20Sopenharmony_ci
28238c2ecf20Sopenharmony_ci
28248c2ecf20Sopenharmony_ci그리고, ALPHA 가 있다
28258c2ecf20Sopenharmony_ci---------------------
28268c2ecf20Sopenharmony_ci
28278c2ecf20Sopenharmony_ciDEC Alpha CPU 는 가장 완화된 메모리 순서의 CPU 중 하나입니다.  뿐만 아니라,
28288c2ecf20Sopenharmony_ciAlpha CPU 의 일부 버전은 분할된 데이터 캐시를 가지고 있어서, 의미적으로
28298c2ecf20Sopenharmony_ci관계되어 있는 두개의 캐시 라인이 서로 다른 시간에 업데이트 되는게 가능합니다.
28308c2ecf20Sopenharmony_ci이게 데이터 의존성 배리어가 정말 필요해지는 부분인데, 데이터 의존성 배리어는
28318c2ecf20Sopenharmony_ci메모리 일관성 시스템과 함께 두개의 캐시를 동기화 시켜서, 포인터 변경과 새로운
28328c2ecf20Sopenharmony_ci데이터의 발견을 올바른 순서로 일어나게 하기 때문입니다.
28338c2ecf20Sopenharmony_ci
28348c2ecf20Sopenharmony_ci리눅스 커널의 메모리 배리어 모델은 Alpha 에 기초해서 정의되었습니다만, v4.15
28358c2ecf20Sopenharmony_ci부터는 Alpha 용 READ_ONCE() 코드 내에 smp_mb() 가 추가되어서 메모리 모델로의
28368c2ecf20Sopenharmony_ciAlpha 의 영향력이 크게 줄어들었습니다.
28378c2ecf20Sopenharmony_ci
28388c2ecf20Sopenharmony_ci
28398c2ecf20Sopenharmony_ci가상 머신 게스트
28408c2ecf20Sopenharmony_ci----------------
28418c2ecf20Sopenharmony_ci
28428c2ecf20Sopenharmony_ci가상 머신에서 동작하는 게스트들은 게스트 자체는 SMP 지원 없이 컴파일 되었다
28438c2ecf20Sopenharmony_ci해도 SMP 영향을 받을 수 있습니다.  이건 UP 커널을 사용하면서 SMP 호스트와
28448c2ecf20Sopenharmony_ci결부되어 발생하는 부작용입니다.  이 경우에는 mandatory 배리어를 사용해서 문제를
28458c2ecf20Sopenharmony_ci해결할 수 있겠지만 그런 해결은 대부분의 경우 최적의 해결책이 아닙니다.
28468c2ecf20Sopenharmony_ci
28478c2ecf20Sopenharmony_ci이 문제를 완벽하게 해결하기 위해, 로우 레벨의 virt_mb() 등의 매크로를 사용할 수
28488c2ecf20Sopenharmony_ci있습니다. 이것들은 SMP 가 활성화 되어 있다면 smp_mb() 등과 동일한 효과를
28498c2ecf20Sopenharmony_ci갖습니다만, SMP 와 SMP 아닌 시스템 모두에 대해 동일한 코드를 만들어냅니다.
28508c2ecf20Sopenharmony_ci예를 들어, 가상 머신 게스트들은 (SMP 일 수 있는) 호스트와 동기화를 할 때에는
28518c2ecf20Sopenharmony_cismp_mb() 가 아니라 virt_mb() 를 사용해야 합니다.
28528c2ecf20Sopenharmony_ci
28538c2ecf20Sopenharmony_ci이것들은 smp_mb() 류의 것들과 모든 부분에서 동일하며, 특히, MMIO 의 영향에
28548c2ecf20Sopenharmony_ci대해서는 간여하지 않습니다: MMIO 의 영향을 제어하려면, mandatory 배리어를
28558c2ecf20Sopenharmony_ci사용하시기 바랍니다.
28568c2ecf20Sopenharmony_ci
28578c2ecf20Sopenharmony_ci
28588c2ecf20Sopenharmony_ci=======
28598c2ecf20Sopenharmony_ci사용 예
28608c2ecf20Sopenharmony_ci=======
28618c2ecf20Sopenharmony_ci
28628c2ecf20Sopenharmony_ci순환식 버퍼
28638c2ecf20Sopenharmony_ci-----------
28648c2ecf20Sopenharmony_ci
28658c2ecf20Sopenharmony_ci메모리 배리어는 순환식 버퍼를 생성자(producer)와 소비자(consumer) 사이의
28668c2ecf20Sopenharmony_ci동기화에 락을 사용하지 않고 구현하는데에 사용될 수 있습니다.  더 자세한 내용을
28678c2ecf20Sopenharmony_ci위해선 다음을 참고하세요:
28688c2ecf20Sopenharmony_ci
28698c2ecf20Sopenharmony_ci	Documentation/core-api/circular-buffers.rst
28708c2ecf20Sopenharmony_ci
28718c2ecf20Sopenharmony_ci
28728c2ecf20Sopenharmony_ci=========
28738c2ecf20Sopenharmony_ci참고 문헌
28748c2ecf20Sopenharmony_ci=========
28758c2ecf20Sopenharmony_ci
28768c2ecf20Sopenharmony_ciAlpha AXP Architecture Reference Manual, Second Edition (Sites & Witek,
28778c2ecf20Sopenharmony_ciDigital Press)
28788c2ecf20Sopenharmony_ci	Chapter 5.2: Physical Address Space Characteristics
28798c2ecf20Sopenharmony_ci	Chapter 5.4: Caches and Write Buffers
28808c2ecf20Sopenharmony_ci	Chapter 5.5: Data Sharing
28818c2ecf20Sopenharmony_ci	Chapter 5.6: Read/Write Ordering
28828c2ecf20Sopenharmony_ci
28838c2ecf20Sopenharmony_ciAMD64 Architecture Programmer's Manual Volume 2: System Programming
28848c2ecf20Sopenharmony_ci	Chapter 7.1: Memory-Access Ordering
28858c2ecf20Sopenharmony_ci	Chapter 7.4: Buffering and Combining Memory Writes
28868c2ecf20Sopenharmony_ci
28878c2ecf20Sopenharmony_ciARM Architecture Reference Manual (ARMv8, for ARMv8-A architecture profile)
28888c2ecf20Sopenharmony_ci	Chapter B2: The AArch64 Application Level Memory Model
28898c2ecf20Sopenharmony_ci
28908c2ecf20Sopenharmony_ciIA-32 Intel Architecture Software Developer's Manual, Volume 3:
28918c2ecf20Sopenharmony_ciSystem Programming Guide
28928c2ecf20Sopenharmony_ci	Chapter 7.1: Locked Atomic Operations
28938c2ecf20Sopenharmony_ci	Chapter 7.2: Memory Ordering
28948c2ecf20Sopenharmony_ci	Chapter 7.4: Serializing Instructions
28958c2ecf20Sopenharmony_ci
28968c2ecf20Sopenharmony_ciThe SPARC Architecture Manual, Version 9
28978c2ecf20Sopenharmony_ci	Chapter 8: Memory Models
28988c2ecf20Sopenharmony_ci	Appendix D: Formal Specification of the Memory Models
28998c2ecf20Sopenharmony_ci	Appendix J: Programming with the Memory Models
29008c2ecf20Sopenharmony_ci
29018c2ecf20Sopenharmony_ciStorage in the PowerPC (Stone and Fitzgerald)
29028c2ecf20Sopenharmony_ci
29038c2ecf20Sopenharmony_ciUltraSPARC Programmer Reference Manual
29048c2ecf20Sopenharmony_ci	Chapter 5: Memory Accesses and Cacheability
29058c2ecf20Sopenharmony_ci	Chapter 15: Sparc-V9 Memory Models
29068c2ecf20Sopenharmony_ci
29078c2ecf20Sopenharmony_ciUltraSPARC III Cu User's Manual
29088c2ecf20Sopenharmony_ci	Chapter 9: Memory Models
29098c2ecf20Sopenharmony_ci
29108c2ecf20Sopenharmony_ciUltraSPARC IIIi Processor User's Manual
29118c2ecf20Sopenharmony_ci	Chapter 8: Memory Models
29128c2ecf20Sopenharmony_ci
29138c2ecf20Sopenharmony_ciUltraSPARC Architecture 2005
29148c2ecf20Sopenharmony_ci	Chapter 9: Memory
29158c2ecf20Sopenharmony_ci	Appendix D: Formal Specifications of the Memory Models
29168c2ecf20Sopenharmony_ci
29178c2ecf20Sopenharmony_ciUltraSPARC T1 Supplement to the UltraSPARC Architecture 2005
29188c2ecf20Sopenharmony_ci	Chapter 8: Memory Models
29198c2ecf20Sopenharmony_ci	Appendix F: Caches and Cache Coherency
29208c2ecf20Sopenharmony_ci
29218c2ecf20Sopenharmony_ciSolaris Internals, Core Kernel Architecture, p63-68:
29228c2ecf20Sopenharmony_ci	Chapter 3.3: Hardware Considerations for Locks and
29238c2ecf20Sopenharmony_ci			Synchronization
29248c2ecf20Sopenharmony_ci
29258c2ecf20Sopenharmony_ciUnix Systems for Modern Architectures, Symmetric Multiprocessing and Caching
29268c2ecf20Sopenharmony_cifor Kernel Programmers:
29278c2ecf20Sopenharmony_ci	Chapter 13: Other Memory Models
29288c2ecf20Sopenharmony_ci
29298c2ecf20Sopenharmony_ciIntel Itanium Architecture Software Developer's Manual: Volume 1:
29308c2ecf20Sopenharmony_ci	Section 2.6: Speculation
29318c2ecf20Sopenharmony_ci	Section 4.4: Memory Access
2932