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