[PATCHv5 06/36] thp: handle write-protection faults for file THP

From: Kirill A. Shutemov
Date: Tue Nov 29 2016 - 06:23:48 EST


For filesystems that wants to be write-notified (has mkwrite), we will
encount write-protection faults for huge PMDs in shared mappings.

The easiest way to handle them is to clear the PMD and let it refault as
wriable.

Signed-off-by: Kirill A. Shutemov <kirill.shutemov@xxxxxxxxxxxxxxx>
Reviewed-by: Jan Kara <jack@xxxxxxx>
---
mm/memory.c | 10 +++++++++-
1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/mm/memory.c b/mm/memory.c
index 455c3e628d52..e3d7cea8cc6a 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -3495,8 +3495,16 @@ static int wp_huge_pmd(struct vm_fault *vmf, pmd_t orig_pmd)
return vmf->vma->vm_ops->pmd_fault(vmf->vma, vmf->address,
vmf->pmd, vmf->flags);

+ if (vmf->vma->vm_flags & VM_SHARED) {
+ /* Clear PMD */
+ zap_page_range_single(vmf->vma, vmf->address & HPAGE_PMD_MASK,
+ HPAGE_PMD_SIZE, NULL);
+
+ /* Refault to establish writable PMD */
+ return 0;
+ }
+
/* COW handled on pte level: split pmd */
- VM_BUG_ON_VMA(vmf->vma->vm_flags & VM_SHARED, vmf->vma);
__split_huge_pmd(vmf->vma, vmf->pmd, vmf->address, false, NULL);

return VM_FAULT_FALLBACK;
--
2.10.2