blob: 23b462566bb762787197b553e871647d72f1aecd [file] [log] [blame]
Jérôme Glisse0f108512017-11-15 17:34:07 -08001When do you need to notify inside page table lock ?
2
3When clearing a pte/pmd we are given a choice to notify the event through
4(notify version of *_clear_flush call mmu_notifier_invalidate_range) under
5the page table lock. But that notification is not necessary in all cases.
6
7For secondary TLB (non CPU TLB) like IOMMU TLB or device TLB (when device use
8thing like ATS/PASID to get the IOMMU to walk the CPU page table to access a
9process virtual address space). There is only 2 cases when you need to notify
10those secondary TLB while holding page table lock when clearing a pte/pmd:
11
12 A) page backing address is free before mmu_notifier_invalidate_range_end()
13 B) a page table entry is updated to point to a new page (COW, write fault
14 on zero page, __replace_page(), ...)
15
16Case A is obvious you do not want to take the risk for the device to write to
17a page that might now be used by some completely different task.
18
19Case B is more subtle. For correctness it requires the following sequence to
20happen:
21 - take page table lock
22 - clear page table entry and notify ([pmd/pte]p_huge_clear_flush_notify())
23 - set page table entry to point to new page
24
25If clearing the page table entry is not followed by a notify before setting
26the new pte/pmd value then you can break memory model like C11 or C++11 for
27the device.
28
29Consider the following scenario (device use a feature similar to ATS/PASID):
30
31Two address addrA and addrB such that |addrA - addrB| >= PAGE_SIZE we assume
32they are write protected for COW (other case of B apply too).
33
34[Time N] --------------------------------------------------------------------
35CPU-thread-0 {try to write to addrA}
36CPU-thread-1 {try to write to addrB}
37CPU-thread-2 {}
38CPU-thread-3 {}
39DEV-thread-0 {read addrA and populate device TLB}
40DEV-thread-2 {read addrB and populate device TLB}
41[Time N+1] ------------------------------------------------------------------
42CPU-thread-0 {COW_step0: {mmu_notifier_invalidate_range_start(addrA)}}
43CPU-thread-1 {COW_step0: {mmu_notifier_invalidate_range_start(addrB)}}
44CPU-thread-2 {}
45CPU-thread-3 {}
46DEV-thread-0 {}
47DEV-thread-2 {}
48[Time N+2] ------------------------------------------------------------------
49CPU-thread-0 {COW_step1: {update page table to point to new page for addrA}}
50CPU-thread-1 {COW_step1: {update page table to point to new page for addrB}}
51CPU-thread-2 {}
52CPU-thread-3 {}
53DEV-thread-0 {}
54DEV-thread-2 {}
55[Time N+3] ------------------------------------------------------------------
56CPU-thread-0 {preempted}
57CPU-thread-1 {preempted}
58CPU-thread-2 {write to addrA which is a write to new page}
59CPU-thread-3 {}
60DEV-thread-0 {}
61DEV-thread-2 {}
62[Time N+3] ------------------------------------------------------------------
63CPU-thread-0 {preempted}
64CPU-thread-1 {preempted}
65CPU-thread-2 {}
66CPU-thread-3 {write to addrB which is a write to new page}
67DEV-thread-0 {}
68DEV-thread-2 {}
69[Time N+4] ------------------------------------------------------------------
70CPU-thread-0 {preempted}
71CPU-thread-1 {COW_step3: {mmu_notifier_invalidate_range_end(addrB)}}
72CPU-thread-2 {}
73CPU-thread-3 {}
74DEV-thread-0 {}
75DEV-thread-2 {}
76[Time N+5] ------------------------------------------------------------------
77CPU-thread-0 {preempted}
78CPU-thread-1 {}
79CPU-thread-2 {}
80CPU-thread-3 {}
81DEV-thread-0 {read addrA from old page}
82DEV-thread-2 {read addrB from new page}
83
84So here because at time N+2 the clear page table entry was not pair with a
85notification to invalidate the secondary TLB, the device see the new value for
86addrB before seing the new value for addrA. This break total memory ordering
87for the device.
88
89When changing a pte to write protect or to point to a new write protected page
90with same content (KSM) it is fine to delay the mmu_notifier_invalidate_range
91call to mmu_notifier_invalidate_range_end() outside the page table lock. This
92is true even if the thread doing the page table update is preempted right after
93releasing page table lock but before call mmu_notifier_invalidate_range_end().