Skip to content

Commit 0e4cd9f

Browse files
committed
Merge branch 'for-next/read-barrier-depends' into for-next/core
* for-next/read-barrier-depends: : Allow architectures to override __READ_ONCE() arm64: Reduce the number of header files pulled into vmlinux.lds.S compiler.h: Move compiletime_assert() macros into compiler_types.h checkpatch: Remove checks relating to [smp_]read_barrier_depends() include/linux: Remove smp_read_barrier_depends() from comments tools/memory-model: Remove smp_read_barrier_depends() from informal doc Documentation/barriers/kokr: Remove references to [smp_]read_barrier_depends() Documentation/barriers: Remove references to [smp_]read_barrier_depends() locking/barriers: Remove definitions for [smp_]read_barrier_depends() alpha: Replace smp_read_barrier_depends() usage with smp_[r]mb() vhost: Remove redundant use of read_barrier_depends() barrier asm/rwonce: Don't pull <asm/barrier.h> into 'asm-generic/rwonce.h' asm/rwonce: Remove smp_read_barrier_depends() invocation alpha: Override READ_ONCE() with barriered implementation asm/rwonce: Allow __READ_ONCE to be overridden by the architecture compiler.h: Split {READ,WRITE}_ONCE definitions out into rwonce.h tools: bpf: Use local copy of headers including uapi/linux/filter.h
2 parents 18aa3bd + 5f1f7f6 commit 0e4cd9f

File tree

31 files changed

+324
-547
lines changed

31 files changed

+324
-547
lines changed

Documentation/RCU/Design/Requirements/Requirements.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,7 @@ again without disrupting RCU readers.
463463
This guarantee was only partially premeditated. DYNIX/ptx used an
464464
explicit memory barrier for publication, but had nothing resembling
465465
``rcu_dereference()`` for subscription, nor did it have anything
466-
resembling the ``smp_read_barrier_depends()`` that was later subsumed
466+
resembling the dependency-ordering barrier that was later subsumed
467467
into ``rcu_dereference()`` and later still into ``READ_ONCE()``. The
468468
need for these operations made itself known quite suddenly at a
469469
late-1990s meeting with the DEC Alpha architects, back in the days when

Documentation/memory-barriers.txt

Lines changed: 8 additions & 148 deletions
Original file line numberDiff line numberDiff line change
@@ -553,12 +553,12 @@ There are certain things that the Linux kernel memory barriers do not guarantee:
553553
DATA DEPENDENCY BARRIERS (HISTORICAL)
554554
-------------------------------------
555555

556-
As of v4.15 of the Linux kernel, an smp_read_barrier_depends() was
557-
added to READ_ONCE(), which means that about the only people who
558-
need to pay attention to this section are those working on DEC Alpha
559-
architecture-specific code and those working on READ_ONCE() itself.
560-
For those who need it, and for those who are interested in the history,
561-
here is the story of data-dependency barriers.
556+
As of v4.15 of the Linux kernel, an smp_mb() was added to READ_ONCE() for
557+
DEC Alpha, which means that about the only people who need to pay attention
558+
to this section are those working on DEC Alpha architecture-specific code
559+
and those working on READ_ONCE() itself. For those who need it, and for
560+
those who are interested in the history, here is the story of
561+
data-dependency barriers.
562562

563563
The usage requirements of data dependency barriers are a little subtle, and
564564
it's not always obvious that they're needed. To illustrate, consider the
@@ -2708,144 +2708,6 @@ the properties of the memory window through which devices are accessed and/or
27082708
the use of any special device communication instructions the CPU may have.
27092709

27102710

2711-
CACHE COHERENCY
2712-
---------------
2713-
2714-
Life isn't quite as simple as it may appear above, however: for while the
2715-
caches are expected to be coherent, there's no guarantee that that coherency
2716-
will be ordered. This means that while changes made on one CPU will
2717-
eventually become visible on all CPUs, there's no guarantee that they will
2718-
become apparent in the same order on those other CPUs.
2719-
2720-
2721-
Consider dealing with a system that has a pair of CPUs (1 & 2), each of which
2722-
has a pair of parallel data caches (CPU 1 has A/B, and CPU 2 has C/D):
2723-
2724-
:
2725-
: +--------+
2726-
: +---------+ | |
2727-
+--------+ : +--->| Cache A |<------->| |
2728-
| | : | +---------+ | |
2729-
| CPU 1 |<---+ | |
2730-
| | : | +---------+ | |
2731-
+--------+ : +--->| Cache B |<------->| |
2732-
: +---------+ | |
2733-
: | Memory |
2734-
: +---------+ | System |
2735-
+--------+ : +--->| Cache C |<------->| |
2736-
| | : | +---------+ | |
2737-
| CPU 2 |<---+ | |
2738-
| | : | +---------+ | |
2739-
+--------+ : +--->| Cache D |<------->| |
2740-
: +---------+ | |
2741-
: +--------+
2742-
:
2743-
2744-
Imagine the system has the following properties:
2745-
2746-
(*) an odd-numbered cache line may be in cache A, cache C or it may still be
2747-
resident in memory;
2748-
2749-
(*) an even-numbered cache line may be in cache B, cache D or it may still be
2750-
resident in memory;
2751-
2752-
(*) while the CPU core is interrogating one cache, the other cache may be
2753-
making use of the bus to access the rest of the system - perhaps to
2754-
displace a dirty cacheline or to do a speculative load;
2755-
2756-
(*) each cache has a queue of operations that need to be applied to that cache
2757-
to maintain coherency with the rest of the system;
2758-
2759-
(*) the coherency queue is not flushed by normal loads to lines already
2760-
present in the cache, even though the contents of the queue may
2761-
potentially affect those loads.
2762-
2763-
Imagine, then, that two writes are made on the first CPU, with a write barrier
2764-
between them to guarantee that they will appear to reach that CPU's caches in
2765-
the requisite order:
2766-
2767-
CPU 1 CPU 2 COMMENT
2768-
=============== =============== =======================================
2769-
u == 0, v == 1 and p == &u, q == &u
2770-
v = 2;
2771-
smp_wmb(); Make sure change to v is visible before
2772-
change to p
2773-
<A:modify v=2> v is now in cache A exclusively
2774-
p = &v;
2775-
<B:modify p=&v> p is now in cache B exclusively
2776-
2777-
The write memory barrier forces the other CPUs in the system to perceive that
2778-
the local CPU's caches have apparently been updated in the correct order. But
2779-
now imagine that the second CPU wants to read those values:
2780-
2781-
CPU 1 CPU 2 COMMENT
2782-
=============== =============== =======================================
2783-
...
2784-
q = p;
2785-
x = *q;
2786-
2787-
The above pair of reads may then fail to happen in the expected order, as the
2788-
cacheline holding p may get updated in one of the second CPU's caches while
2789-
the update to the cacheline holding v is delayed in the other of the second
2790-
CPU's caches by some other cache event:
2791-
2792-
CPU 1 CPU 2 COMMENT
2793-
=============== =============== =======================================
2794-
u == 0, v == 1 and p == &u, q == &u
2795-
v = 2;
2796-
smp_wmb();
2797-
<A:modify v=2> <C:busy>
2798-
<C:queue v=2>
2799-
p = &v; q = p;
2800-
<D:request p>
2801-
<B:modify p=&v> <D:commit p=&v>
2802-
<D:read p>
2803-
x = *q;
2804-
<C:read *q> Reads from v before v updated in cache
2805-
<C:unbusy>
2806-
<C:commit v=2>
2807-
2808-
Basically, while both cachelines will be updated on CPU 2 eventually, there's
2809-
no guarantee that, without intervention, the order of update will be the same
2810-
as that committed on CPU 1.
2811-
2812-
2813-
To intervene, we need to interpolate a data dependency barrier or a read
2814-
barrier between the loads (which as of v4.15 is supplied unconditionally
2815-
by the READ_ONCE() macro). This will force the cache to commit its
2816-
coherency queue before processing any further requests:
2817-
2818-
CPU 1 CPU 2 COMMENT
2819-
=============== =============== =======================================
2820-
u == 0, v == 1 and p == &u, q == &u
2821-
v = 2;
2822-
smp_wmb();
2823-
<A:modify v=2> <C:busy>
2824-
<C:queue v=2>
2825-
p = &v; q = p;
2826-
<D:request p>
2827-
<B:modify p=&v> <D:commit p=&v>
2828-
<D:read p>
2829-
smp_read_barrier_depends()
2830-
<C:unbusy>
2831-
<C:commit v=2>
2832-
x = *q;
2833-
<C:read *q> Reads from v after v updated in cache
2834-
2835-
2836-
This sort of problem can be encountered on DEC Alpha processors as they have a
2837-
split cache that improves performance by making better use of the data bus.
2838-
While most CPUs do imply a data dependency barrier on the read when a memory
2839-
access depends on a read, not all do, so it may not be relied on.
2840-
2841-
Other CPUs may also have split caches, but must coordinate between the various
2842-
cachelets for normal memory accesses. The semantics of the Alpha removes the
2843-
need for hardware coordination in the absence of memory barriers, which
2844-
permitted Alpha to sport higher CPU clock rates back in the day. However,
2845-
please note that (again, as of v4.15) smp_read_barrier_depends() should not
2846-
be used except in Alpha arch-specific code and within the READ_ONCE() macro.
2847-
2848-
28492711
CACHE COHERENCY VS DMA
28502712
----------------------
28512713

@@ -3009,10 +2871,8 @@ caches with the memory coherence system, thus making it seem like pointer
30092871
changes vs new data occur in the right order.
30102872

30112873
The Alpha defines the Linux kernel's memory model, although as of v4.15
3012-
the Linux kernel's addition of smp_read_barrier_depends() to READ_ONCE()
3013-
greatly reduced Alpha's impact on the memory model.
3014-
3015-
See the subsection on "Cache Coherency" above.
2874+
the Linux kernel's addition of smp_mb() to READ_ONCE() on Alpha greatly
2875+
reduced its impact on the memory model.
30162876

30172877

30182878
VIRTUAL MACHINE GUESTS

Documentation/translations/ko_KR/memory-barriers.txt

Lines changed: 3 additions & 143 deletions
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,7 @@ ACQUIRE 는 해당 오퍼레이션의 로드 부분에만 적용되고 RELEASE
577577
데이터 의존성 배리어 (역사적)
578578
-----------------------------
579579

580-
리눅스 커널 v4.15 기준으로, smp_read_barrier_depends() 가 READ_ONCE()
580+
리눅스 커널 v4.15 기준으로, smp_mb() 가 DEC Alpha 용 READ_ONCE() 코드에
581581
추가되었는데, 이는 이 섹션에 주의를 기울여야 하는 사람들은 DEC Alpha 아키텍쳐
582582
전용 코드를 만드는 사람들과 READ_ONCE() 자체를 만드는 사람들 뿐임을 의미합니다.
583583
그런 분들을 위해, 그리고 역사에 관심 있는 분들을 위해, 여기 데이터 의존성
@@ -2664,144 +2664,6 @@ CPU 코어는 프로그램의 인과성이 유지된다고만 여겨진다면
26642664
수도 있습니다.
26652665

26662666

2667-
캐시 일관성
2668-
-----------
2669-
2670-
하지만 삶은 앞에서 이야기한 것처럼 단순하지 않습니다: 캐시들은 일관적일 것으로
2671-
기대되지만, 그 일관성이 순서에도 적용될 거라는 보장은 없습니다. 한 CPU 에서
2672-
만들어진 변경 사항은 최종적으로는 시스템의 모든 CPU 에게 보여지게 되지만, 다른
2673-
CPU 들에게도 같은 순서로 보이게 될 거라는 보장은 없다는 뜻입니다.
2674-
2675-
2676-
두개의 CPU (1 & 2) 가 달려 있고, 각 CPU 에 두개의 데이터 캐시(CPU 1 은 A/B 를,
2677-
CPU 2 는 C/D 를 갖습니다)가 병렬로 연결되어 있는 시스템을 다룬다고 생각해
2678-
봅시다:
2679-
2680-
:
2681-
: +--------+
2682-
: +---------+ | |
2683-
+--------+ : +--->| Cache A |<------->| |
2684-
| | : | +---------+ | |
2685-
| CPU 1 |<---+ | |
2686-
| | : | +---------+ | |
2687-
+--------+ : +--->| Cache B |<------->| |
2688-
: +---------+ | |
2689-
: | Memory |
2690-
: +---------+ | System |
2691-
+--------+ : +--->| Cache C |<------->| |
2692-
| | : | +---------+ | |
2693-
| CPU 2 |<---+ | |
2694-
| | : | +---------+ | |
2695-
+--------+ : +--->| Cache D |<------->| |
2696-
: +---------+ | |
2697-
: +--------+
2698-
:
2699-
2700-
이 시스템이 다음과 같은 특성을 갖는다 생각해 봅시다:
2701-
2702-
(*) 홀수번 캐시라인은 캐시 A, 캐시 C 또는 메모리에 위치할 수 있음;
2703-
2704-
(*) 짝수번 캐시라인은 캐시 B, 캐시 D 또는 메모리에 위치할 수 있음;
2705-
2706-
(*) CPU 코어가 한개의 캐시에 접근하는 동안, 다른 캐시는 - 더티 캐시라인을
2707-
메모리에 내리거나 추측성 로드를 하거나 하기 위해 - 시스템의 다른 부분에
2708-
액세스 하기 위해 버스를 사용할 수 있음;
2709-
2710-
(*) 각 캐시는 시스템의 나머지 부분들과 일관성을 맞추기 위해 해당 캐시에
2711-
적용되어야 할 오퍼레이션들의 큐를 가짐;
2712-
2713-
(*) 이 일관성 큐는 캐시에 이미 존재하는 라인에 가해지는 평범한 로드에 의해서는
2714-
비워지지 않는데, 큐의 오퍼레이션들이 이 로드의 결과에 영향을 끼칠 수 있다
2715-
할지라도 그러함.
2716-
2717-
이제, 첫번째 CPU 에서 두개의 쓰기 오퍼레이션을 만드는데, 해당 CPU 의 캐시에
2718-
요청된 순서로 오퍼레이션이 도달됨을 보장하기 위해 두 오퍼레이션 사이에 쓰기
2719-
배리어를 사용하는 상황을 상상해 봅시다:
2720-
2721-
CPU 1 CPU 2 COMMENT
2722-
=============== =============== =======================================
2723-
u == 0, v == 1 and p == &u, q == &u
2724-
v = 2;
2725-
smp_wmb(); v 의 변경이 p 의 변경 전에 보일 것을
2726-
분명히 함
2727-
<A:modify v=2> v 는 이제 캐시 A 에 독점적으로 존재함
2728-
p = &v;
2729-
<B:modify p=&v> p 는 이제 캐시 B 에 독점적으로 존재함
2730-
2731-
여기서의 쓰기 메모리 배리어는 CPU 1 의 캐시가 올바른 순서로 업데이트 된 것으로
2732-
시스템의 다른 CPU 들이 인지하게 만듭니다. 하지만, 이제 두번째 CPU 가 그 값들을
2733-
읽으려 하는 상황을 생각해 봅시다:
2734-
2735-
CPU 1 CPU 2 COMMENT
2736-
=============== =============== =======================================
2737-
...
2738-
q = p;
2739-
x = *q;
2740-
2741-
위의 두개의 읽기 오퍼레이션은 예상된 순서로 일어나지 못할 수 있는데, 두번째 CPU
2742-
의 한 캐시에 다른 캐시 이벤트가 발생해 v 를 담고 있는 캐시라인의 해당 캐시에의
2743-
업데이트가 지연되는 사이, p 를 담고 있는 캐시라인은 두번째 CPU 의 다른 캐시에
2744-
업데이트 되어버렸을 수 있기 때문입니다.
2745-
2746-
CPU 1 CPU 2 COMMENT
2747-
=============== =============== =======================================
2748-
u == 0, v == 1 and p == &u, q == &u
2749-
v = 2;
2750-
smp_wmb();
2751-
<A:modify v=2> <C:busy>
2752-
<C:queue v=2>
2753-
p = &v; q = p;
2754-
<D:request p>
2755-
<B:modify p=&v> <D:commit p=&v>
2756-
<D:read p>
2757-
x = *q;
2758-
<C:read *q> 캐시에 업데이트 되기 전의 v 를 읽음
2759-
<C:unbusy>
2760-
<C:commit v=2>
2761-
2762-
기본적으로, 두개의 캐시라인 모두 CPU 2 에 최종적으로는 업데이트 될 것이지만,
2763-
별도의 개입 없이는, 업데이트의 순서가 CPU 1 에서 만들어진 순서와 동일할
2764-
것이라는 보장이 없습니다.
2765-
2766-
2767-
여기에 개입하기 위해선, 데이터 의존성 배리어나 읽기 배리어를 로드 오퍼레이션들
2768-
사이에 넣어야 합니다 (v4.15 부터는 READ_ONCE() 매크로에 의해 무조건적으로
2769-
그렇게 됩니다). 이렇게 함으로써 캐시가 다음 요청을 처리하기 전에 일관성 큐를
2770-
처리하도록 강제하게 됩니다.
2771-
2772-
CPU 1 CPU 2 COMMENT
2773-
=============== =============== =======================================
2774-
u == 0, v == 1 and p == &u, q == &u
2775-
v = 2;
2776-
smp_wmb();
2777-
<A:modify v=2> <C:busy>
2778-
<C:queue v=2>
2779-
p = &v; q = p;
2780-
<D:request p>
2781-
<B:modify p=&v> <D:commit p=&v>
2782-
<D:read p>
2783-
smp_read_barrier_depends()
2784-
<C:unbusy>
2785-
<C:commit v=2>
2786-
x = *q;
2787-
<C:read *q> 캐시에 업데이트 된 v 를 읽음
2788-
2789-
2790-
이런 부류의 문제는 DEC Alpha 계열 프로세서들에서 발견될 수 있는데, 이들은
2791-
데이터 버스를 좀 더 잘 사용해 성능을 개선할 수 있는, 분할된 캐시를 가지고 있기
2792-
때문입니다. 대부분의 CPU 는 하나의 읽기 오퍼레이션의 메모리 액세스가 다른 읽기
2793-
오퍼레이션에 의존적이라면 데이터 의존성 배리어를 내포시킵니다만, 모두가 그런건
2794-
아니기 때문에 이점에 의존해선 안됩니다.
2795-
2796-
다른 CPU 들도 분할된 캐시를 가지고 있을 수 있지만, 그런 CPU 들은 평범한 메모리
2797-
액세스를 위해서도 이 분할된 캐시들 사이의 조정을 해야만 합니다. Alpha 는 가장
2798-
약한 메모리 순서 시맨틱 (semantic) 을 선택함으로써 메모리 배리어가 명시적으로
2799-
사용되지 않았을 때에는 그런 조정이 필요하지 않게 했으며, 이는 Alpha 가 당시에
2800-
더 높은 CPU 클락 속도를 가질 수 있게 했습니다. 하지만, (다시 말하건대, v4.15
2801-
이후부터는) Alpha 아키텍쳐 전용 코드와 READ_ONCE() 매크로 내부에서를 제외하고는
2802-
smp_read_barrier_depends() 가 사용되지 않아야 함을 알아두시기 바랍니다.
2803-
2804-
28052667
캐시 일관성 VS DMA
28062668
------------------
28072669

@@ -2962,10 +2824,8 @@ Alpha CPU 의 일부 버전은 분할된 데이터 캐시를 가지고 있어서
29622824
데이터의 발견을 올바른 순서로 일어나게 하기 때문입니다.
29632825

29642826
리눅스 커널의 메모리 배리어 모델은 Alpha 에 기초해서 정의되었습니다만, v4.15
2965-
부터는 리눅스 커널이 READ_ONCE() 내에 smp_read_barrier_depends() 를 추가해서
2966-
Alpha 의 메모리 모델로의 영향력이 크게 줄어들긴 했습니다.
2967-
2968-
위의 "캐시 일관성" 서브섹션을 참고하세요.
2827+
부터는 Alpha 용 READ_ONCE() 코드 내에 smp_mb() 가 추가되어서 메모리 모델로의
2828+
Alpha 의 영향력이 크게 줄어들었습니다.
29692829

29702830

29712831
가상 머신 게스트

0 commit comments

Comments
 (0)