Skip to contentSkip to conversation

mm: rmap: explicitly reset vma->anon_vma in unlink_anon_vmas()

PATCH New Li Xinhai 1 participant 2 messages Jan 27, 2021

Subsystems (3)
lore
Share & tools
lore

Li Xinhai

author
#0 lore raw

PATCH v2 1/2 mm: rmap: explicitly reset vma->anon_vma in unlink_anon_vmas()

In case the vma will continue to be used after unlink its relevant anon_vma, we need to reset the vma->anon_vma pointer to NULL. So, later when fault happen within this vma again, a new anon_vma will be prepared.

By this way, the vma will only be checked for reverse mapping of pages which been fault in after the unlink_anon_vmas call.

Currently, the mremap with MREMAP_DONTUNMAP scenario will continue use the vma after moved its page table entries to a new vma. For other scenarios, the vma itself will be freed after call unlink_anon_vmas.

---
v1->v2:
No chnages.

 mm/rmap.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
mm/rmap.c +8 -1
index 08c56aaf72eb..31ef76180c53 100644
@@ -413,8 +413,15 @@ void unlink_anon_vmas(struct vm_area_struct *vma)
 		list_del(&avc->same_vma);
 		anon_vma_chain_free(avc);
 	}
-	if (vma->anon_vma)
+	if (vma->anon_vma) {
 		vma->anon_vma->degree--;
+
+		/*
+		 * vma would still be needed after unlink, and anon_vma will be prepared
+		 * when handle fault.
+		 */
+		vma->anon_vma = NULL;
+	}
 	unlock_anon_vma_root(root);
 
 	/*
Signature · 1 line
2.18.4
author
#1 lore raw

PATCH v2 2/2 mm: mremap: unlink anon_vmas when mremap with MREMAP_DONTUNMAP success

mremap with MREMAP_DONTUNMAP can move all page table entries to new vma, which means all pages allocated for the old vma are not relevant to it anymore, and the relevant anon_vma links needs to be unlinked, in nature the old vma is much like been freshly created and have no pages been fault in.

But we should not do unlink, if the new vma has effectively merged with the old one.

Cc Brian Geffon <[email protected]>
Cc Lokesh Gidra <[email protected]>
Cc Minchan Kim <[email protected]>
Cc Kirill A. Shutemov <[email protected]>
Cc Vlastimil Babka <[email protected]>
Cc Andrea Arcangeli <[email protected]>

---
v1->v2:
Need to consider the old vma just been partially moved, if so we must keep the anon_vma links for remaining mapped pages in old vma.

 mm/mremap.c | 8 ++++++++
 1 file changed, 8 insertions(+)
mm/mremap.c +8 -0
index f554320281cc..ec6c63e3ea66 100644
@@ -595,6 +595,14 @@ static unsigned long move_vma(struct vm_area_struct *vma,
 		/* We always clear VM_LOCKED[ONFAULT] on the old vma */
 		vma->vm_flags &= VM_LOCKED_CLEAR_MASK;
 
+		/*
+		 * anon_vma links of the old vma is no longer needed after its page
+		 * table has been moved.
+		 */
+		if (new_vma != vma && vma->vm_start == old_addr &&
+			vma->vm_end == (old_addr + old_len))
+			unlink_anon_vmas(vma);
+
 		/* Because we won't unmap we don't need to touch locked_vm */
 		return new_addr;
 	}
Signature · 1 line
2.18.4