1 When do you need to notify inside page table lock ?
3 When 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
5 the page table lock. But that notification is not necessary in all cases.
7 For secondary TLB (non CPU TLB) like IOMMU TLB or device TLB (when device use
8 thing like ATS/PASID to get the IOMMU to walk the CPU page table to access a
9 process virtual address space). There is only 2 cases when you need to notify
10 those secondary TLB while holding page table lock when clearing a pte/pmd:
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(), ...)
16 Case A is obvious you do not want to take the risk for the device to write to
17 a page that might now be used by some completely different task.
19 Case B is more subtle. For correctness it requires the following sequence to
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
25 If clearing the page table entry is not followed by a notify before setting
26 the new pte/pmd value then you can break memory model like C11 or C++11 for
29 Consider the following scenario (device use a feature similar to ATS/PASID):
31 Two address addrA and addrB such that |addrA - addrB| >= PAGE_SIZE we assume
32 they are write protected for COW (other case of B apply too).
34 [Time N] --------------------------------------------------------------------
35 CPU-thread-0 {try to write to addrA}
36 CPU-thread-1 {try to write to addrB}
39 DEV-thread-0 {read addrA and populate device TLB}
40 DEV-thread-2 {read addrB and populate device TLB}
41 [Time N+1] ------------------------------------------------------------------
42 CPU-thread-0 {COW_step0: {mmu_notifier_invalidate_range_start(addrA)}}
43 CPU-thread-1 {COW_step0: {mmu_notifier_invalidate_range_start(addrB)}}
48 [Time N+2] ------------------------------------------------------------------
49 CPU-thread-0 {COW_step1: {update page table to point to new page for addrA}}
50 CPU-thread-1 {COW_step1: {update page table to point to new page for addrB}}
55 [Time N+3] ------------------------------------------------------------------
56 CPU-thread-0 {preempted}
57 CPU-thread-1 {preempted}
58 CPU-thread-2 {write to addrA which is a write to new page}
62 [Time N+3] ------------------------------------------------------------------
63 CPU-thread-0 {preempted}
64 CPU-thread-1 {preempted}
66 CPU-thread-3 {write to addrB which is a write to new page}
69 [Time N+4] ------------------------------------------------------------------
70 CPU-thread-0 {preempted}
71 CPU-thread-1 {COW_step3: {mmu_notifier_invalidate_range_end(addrB)}}
76 [Time N+5] ------------------------------------------------------------------
77 CPU-thread-0 {preempted}
81 DEV-thread-0 {read addrA from old page}
82 DEV-thread-2 {read addrB from new page}
84 So here because at time N+2 the clear page table entry was not pair with a
85 notification to invalidate the secondary TLB, the device see the new value for
86 addrB before seing the new value for addrA. This break total memory ordering
89 When changing a pte to write protect or to point to a new write protected page
90 with same content (KSM) it is fine to delay the mmu_notifier_invalidate_range
91 call to mmu_notifier_invalidate_range_end() outside the page table lock. This
92 is true even if the thread doing the page table update is preempted right after
93 releasing page table lock but before call mmu_notifier_invalidate_range_end().