| 1 |
:Original: Documentation/mm/mmu_notifier.rst |
| 2 |
|
| 3 |
:翻译: |
| 4 |
|
| 5 |
司延腾 Yanteng Si <[email protected]> |
| 6 |
|
| 7 |
:校译: |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
什么时候需要页表锁内通知? |
| 12 |
========================== |
| 13 |
|
| 14 |
当清除一个pte/pmd时,我们可以选择通过在页表锁下(通知版的\*_clear_flush调用 |
| 15 |
mmu_notifier_invalidate_range)通知事件。但这种通知并不是在所有情况下都需要的。 |
| 16 |
|
| 17 |
对于二级TLB(非CPU TLB),如IOMMU TLB或设备TLB(当设备使用类似ATS/PASID的东西让 |
| 18 |
IOMMU走CPU页表来访问进程的虚拟地址空间)。只有两种情况需要在清除pte/pmd时在持有页 |
| 19 |
表锁的同时通知这些二级TLB: |
| 20 |
|
| 21 |
A) 在mmu_notifier_invalidate_range_end()之前,支持页的地址被释放。 |
| 22 |
B) 一个页表项被更新以指向一个新的页面(COW,零页上的写异常,__replace_page(),...)。 |
| 23 |
|
| 24 |
情况A很明显,你不想冒风险让设备写到一个现在可能被一些完全不同的任务使用的页面。 |
| 25 |
|
| 26 |
情况B更加微妙。为了正确起见,它需要按照以下序列发生: |
| 27 |
|
| 28 |
- 上页表锁 |
| 29 |
- 清除页表项并通知 ([pmd/pte]p_huge_clear_flush_notify()) |
| 30 |
- 设置页表项以指向新页 |
| 31 |
|
| 32 |
如果在设置新的pte/pmd值之前,清除页表项之后没有进行通知,那么你就会破坏设备的C11或 |
| 33 |
C++11等内存模型。 |
| 34 |
|
| 35 |
考虑以下情况(设备使用类似于ATS/PASID的功能)。 |
| 36 |
|
| 37 |
两个地址addrA和addrB,这样|addrA - addrB| >= PAGE_SIZE,我们假设它们是COW的 |
| 38 |
写保护(B的其他情况也适用)。 |
| 39 |
|
| 40 |
:: |
| 41 |
|
| 42 |
[Time N] -------------------------------------------------------------------- |
| 43 |
CPU-thread-0 {尝试写到addrA} |
| 44 |
CPU-thread-1 {尝试写到addrB} |
| 45 |
CPU-thread-2 {} |
| 46 |
CPU-thread-3 {} |
| 47 |
DEV-thread-0 {读取addrA并填充设备TLB} |
| 48 |
DEV-thread-2 {读取addrB并填充设备TLB} |
| 49 |
[Time N+1] ------------------------------------------------------------------ |
| 50 |
CPU-thread-0 {COW_step0: {mmu_notifier_invalidate_range_start(addrA)}} |
| 51 |
CPU-thread-1 {COW_step0: {mmu_notifier_invalidate_range_start(addrB)}} |
| 52 |
CPU-thread-2 {} |
| 53 |
CPU-thread-3 {} |
| 54 |
DEV-thread-0 {} |
| 55 |
DEV-thread-2 {} |
| 56 |
[Time N+2] ------------------------------------------------------------------ |
| 57 |
CPU-thread-0 {COW_step1: {更新页表以指向addrA的新页}} |
| 58 |
CPU-thread-1 {COW_step1: {更新页表以指向addrB的新页}} |
| 59 |
CPU-thread-2 {} |
| 60 |
CPU-thread-3 {} |
| 61 |
DEV-thread-0 {} |
| 62 |
DEV-thread-2 {} |
| 63 |
[Time N+3] ------------------------------------------------------------------ |
| 64 |
CPU-thread-0 {preempted} |
| 65 |
CPU-thread-1 {preempted} |
| 66 |
CPU-thread-2 {写入addrA,这是对新页面的写入} |
| 67 |
CPU-thread-3 {} |
| 68 |
DEV-thread-0 {} |
| 69 |
DEV-thread-2 {} |
| 70 |
[Time N+3] ------------------------------------------------------------------ |
| 71 |
CPU-thread-0 {preempted} |
| 72 |
CPU-thread-1 {preempted} |
| 73 |
CPU-thread-2 {} |
| 74 |
CPU-thread-3 {写入addrB,这是一个写入新页的过程} |
| 75 |
DEV-thread-0 {} |
| 76 |
DEV-thread-2 {} |
| 77 |
[Time N+4] ------------------------------------------------------------------ |
| 78 |
CPU-thread-0 {preempted} |
| 79 |
CPU-thread-1 {COW_step3: {mmu_notifier_invalidate_range_end(addrB)}} |
| 80 |
CPU-thread-2 {} |
| 81 |
CPU-thread-3 {} |
| 82 |
DEV-thread-0 {} |
| 83 |
DEV-thread-2 {} |
| 84 |
[Time N+5] ------------------------------------------------------------------ |
| 85 |
CPU-thread-0 {preempted} |
| 86 |
CPU-thread-1 {} |
| 87 |
CPU-thread-2 {} |
| 88 |
CPU-thread-3 {} |
| 89 |
DEV-thread-0 {从旧页中读取addrA} |
| 90 |
DEV-thread-2 {从新页面读取addrB} |
| 91 |
|
| 92 |
所以在这里,因为在N+2的时候,清空页表项没有和通知一起作废二级TLB,设备在看到addrA的新值之前 |
| 93 |
就看到了addrB的新值。这就破坏了设备的总内存序。 |
| 94 |
|
| 95 |
当改变一个pte的写保护或指向一个新的具有相同内容的写保护页(KSM)时,将mmu_notifier_invalidate_range |
| 96 |
调用延迟到页表锁外的mmu_notifier_invalidate_range_end()是可以的。即使做页表更新的线程 |
| 97 |
在释放页表锁后但在调用mmu_notifier_invalidate_range_end()前被抢占,也是如此。 |