mm-avoid-using-vma_merge-for-new-vmas.patch removed from -mm tree
The quilt patch titled
Subject: mm: avoid using vma_merge() for new VMAs
has been removed from the -mm tree. Its filename was
mm-avoid-using-vma_merge-for-new-vmas.patch
This patch was dropped because it was merged into the mm-stable branch of git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
------------------------------------------------------
From: Lorenzo Stoakes <[email protected]>
Subject: mm: avoid using vma_merge() for new VMAs
Date: Fri, 30 Aug 2024 19:10:18 +0100
Abstract vma_merge_new_vma() to use vma_merge_struct and rename the resultant function vma_merge_new_range() to be clear what the purpose of this function is - a new VMA is desired in the specified range, and we wish to see if it is possible to 'merge' surrounding VMAs into this range rather than having to allocate a new VMA.
Note that this function uses vma_extend() exclusively, so adopts its requirement that the iterator point at or before the gap. We add an assert to this effect.
This is as opposed to vma_merge_existing_range(), which will be introduced in a subsequent commit, and provide the same functionality for cases in which we are modifying an existing VMA.
In mmap_region() and do_brk_flags() we open code scenarios where we prefer to use vma_expand() rather than invoke a full vma_merge() operation.
Abstract this logic and eliminate all of the open-coding, and also use the same logic for all cases where we add new VMAs to, rather than ultimately use vma_merge(), rather use vma_expand().
Doing so removes duplication and simplifies VMA merging in all such cases, laying the ground for us to eliminate the merging of new VMAs in vma_merge() altogether.
Also add the ability for the vmg to track state, and able to report errors, allowing for us to differentiate a failed merge from an inability to allocate memory in callers.
This makes it far easier to understand what is happening in these cases avoiding confusion, bugs and allowing for future optimisation.
Also introduce vma_iter_next_rewind() to allow for retrieval of the next, and (optionally) the prev VMA, rewinding to the start of the previous gap.
Introduce are_anon_vmas_compatible() to abstract individual VMA anon_vma comparison for the case of merging on both sides where the anon_vma of the VMA being merged maybe compatible with prev and next, but prev and next's anon_vma's may not be compatible with each other.
Finally also introduce can_vma_merge_left() / can_vma_merge_right() to check adjacent VMA compatibility and that they are indeed adjacent.
---
mm/mmap.c | 92 ++----------- mm/vma.c | 200 +++++++++++++++++++++++++---- mm/vma.h | 48 ++++++ tools/testing/vma/vma.c | 33 ++++ tools/testing/vma/vma_internal.h | 6 5 files changed, 279 insertions(+), 100 deletions(-)
mm/mmap.c renamed from mm/mmap.c~mm-avoid-using-vma_merge-for-new-vmas +20 -72
@@ -1364,8 +1364,8 @@ unsigned long mmap_region(struct file *f {struct mm_struct *mm = current->mm;struct vm_area_struct *vma =NULL ; -struct vm_area_struct *next, *prev, *merge;pgoff_t pglen =PHYS_PFN (len); +struct vm_area_struct *merge;unsigned long charged =0 ;struct vma_munmap_struct vms;struct ma_state mas_detach;
@@ -1389,14 +1389,11 @@ unsigned long mmap_region(struct file *fif (error) goto gather_failed; - next = vmg.next = vms.next; - prev = vmg.prev = vms.prev; + vmg.next = vms.next; + vmg.prev = vms.prev; vma =NULL ; }else { - next = vmg.next =vma_next (&vmi); - prev = vmg.prev =vma_prev (&vmi); -if (prev) -vma_iter_next_range (&vmi); + vmg.next =vma_iter_next_rewind (&vmi, &vmg.prev); }/* Check against address space limit. */
@@ -1417,46 +1414,9 @@ unsigned long mmap_region(struct file *f vmg.flags = vm_flags; } -if (vm_flags &VM_SPECIAL ) - goto cannot_expand; - -/* Attempt to expand an old mapping */ -/* Check next */ -if (next && next->vm_start == end &&can_vma_merge_before (&vmg)) { - vmg.end = next->vm_end; - vma = vmg.vma = next; - vmg.pgoff = next->vm_pgoff - pglen; -/* -* We set this here so if we will merge with the previous VMA in -* the code below, can_vma_merge_after() ensures anon_vma -* compatibility between prev and next. -*/ - vmg.anon_vma = vma->anon_vma; - vmg.uffd_ctx = vma->vm_userfaultfd_ctx; - } - -/* Check prev */ -if (prev && prev->vm_end == addr &&can_vma_merge_after (&vmg)) { - vmg.start = prev->vm_start; - vma = vmg.vma = prev; - vmg.pgoff = prev->vm_pgoff; -vma_prev (&vmi);/* Equivalent to going to the previous range */ - } - -if (vma) { -/* Actually expand, if possible */ -if (!vma_expand (&vmg)) { -khugepaged_enter_vma (vma, vm_flags); - goto expanded; - } - -/* If the expand fails, then reposition the vma iterator */ -if (unlikely (vma == prev)) -vma_iter_set (&vmi, addr); - } - -cannot_expand: - + vma =vma_merge_new_range (&vmg); +if (vma) + goto expanded;/* * Determine the object being mapped and call the appropriate * specific mapper. the address has already been validated, but
@@ -1503,10 +1463,11 @@ cannot_expand:* If vm_flags changed after call_mmap(), we should try merge * vma again as we may succeed this time. */ -if (unlikely (vm_flags != vma->vm_flags && prev)) { - merge =vma_merge_new_vma (&vmi, prev, vma, - vma->vm_start, vma->vm_end, - vma->vm_pgoff); +if (unlikely (vm_flags != vma->vm_flags && vmg.prev)) { + vmg.flags = vma->vm_flags; +/* If this fails, state is reset ready for a reattempt. */ + merge =vma_merge_new_range (&vmg); +if (merge) {/* * ->mmap() can change vma->vm_file and fput
@@ -1522,6 +1483,7 @@ cannot_expand: vm_flags = vma->vm_flags; goto unmap_writable; } +vma_iter_config (&vmi, addr, end); } vm_flags = vma->vm_flags;
@@ -1554,7 +1516,7 @@ cannot_expand:vma_link_file (vma);/* -* vma_merge() calls khugepaged_enter_vma() either, the below +* vma_merge_new_range() calls khugepaged_enter_vma() too, the below * call covers the non-merge case. */ khugepaged_enter_vma (vma, vma->vm_flags);
@@ -1609,7 +1571,7 @@ unmap_and_free_vma:vma_iter_set (&vmi, vma->vm_end);/* Undo any partial mapping done by a device driver. */ -unmap_region (&vmi.mas, vma, prev, next); +unmap_region (&vmi.mas, vma, vmg.prev, vmg.next); }if (writable_file_mapping)mapping_unmap_writable (file->f_mapping);
@@ -1756,7 +1718,6 @@ static int do_brk_flags(struct vma_itera unsigned long addr, unsigned long len, unsigned long flags) { struct mm_struct *mm = current->mm; -struct vma_prepare vp;/* * Check against address space limits by the changed size
@@ -1780,25 +1741,12 @@ static int do_brk_flags(struct vma_iteraVMG_STATE (vmg, mm, vmi, addr, addr + len, flags,PHYS_PFN (addr)); vmg.prev = vma; -if (can_vma_merge_after (&vmg)) { -vma_iter_config (vmi, vma->vm_start, addr + len); -if (vma_iter_prealloc (vmi, vma)) - goto unacct_fail; - -vma_start_write (vma); - -init_vma_prep (&vp, vma); -vma_prepare (&vp); -vma_adjust_trans_huge (vma, vma->vm_start, addr + len,0 ); - vma->vm_end = addr + len; -vm_flags_set (vma,VM_SOFTDIRTY ); -vma_iter_store (vmi, vma); - -vma_complete (&vp, vmi, mm); -validate_mm (mm); -khugepaged_enter_vma (vma, flags); +vma_iter_next_range (vmi); + +if (vma_merge_new_range (&vmg)) goto out; - } +else if (vmg_nomem (&vmg)) + goto unacct_fail; }if (vma)
mm/vma.c renamed from mm/vma.c~mm-avoid-using-vma_merge-for-new-vmas +178 -22
284 lines not on this page · raw message
mm/vma.h renamed from mm/vma.h~mm-avoid-using-vma_merge-for-new-vmas +44 -4
86 lines not on this page · raw message
tools/testing/vma/vma.c renamed from tools/testing/vma/vma.c~mm-avoid-using-vma_merge-for-new-vmas +31 -2
@@ -101,9 +101,9 @@ static struct vm_area_struct *merge_new(*/ vmg->next =vma_next (vmg->vmi); vmg->prev =vma_prev (vmg->vmi); +vma_iter_next_range (vmg->vmi); -vma_iter_set (vmg->vmi, vmg->start); -return vma_merge (vmg); +return vma_merge_new_range (vmg); }/*
@@ -162,10 +162,14 @@ static struct vm_area_struct *try_merge_ merged =merge_new (vmg);if (merged) { *was_merged = true; +ASSERT_EQ (vmg->state,VMA_MERGE_SUCCESS );return merged; } *was_merged = false; + +ASSERT_EQ (vmg->state,VMA_MERGE_NOMERGE ); +return alloc_and_link_vma (mm, start, end, pgoff, flags); }
@@ -595,6 +599,7 @@ static bool test_vma_merge_special_flags vmg.flags = flags | special_flag; vma =merge_new (&vmg);ASSERT_EQ (vma,NULL ); +ASSERT_EQ (vmg.state,VMA_MERGE_NOMERGE ); }/* 2. Modify VMA with special flag that would otherwise merge. */
@@ -616,6 +621,7 @@ static bool test_vma_merge_special_flags vmg.flags = flags | special_flag; vma =merge_existing (&vmg);ASSERT_EQ (vma,NULL ); +ASSERT_EQ (vmg.state,VMA_MERGE_NOMERGE ); }cleanup_mm (&mm, &vmi);
@@ -708,6 +714,7 @@ static bool test_vma_merge_with_close(vo/* The next VMA having a close() operator should cause the merge to fail.*/ ASSERT_EQ (merge_new (&vmg),NULL ); +ASSERT_EQ (vmg.state,VMA_MERGE_NOMERGE );/* Now create the VMA so we can merge via modified flags */ vmg_set_range (&vmg,0x1000 ,0x2000 ,1 , flags);
@@ -719,6 +726,7 @@ static bool test_vma_merge_with_close(vo* also fail. */ ASSERT_EQ (merge_existing (&vmg),NULL ); +ASSERT_EQ (vmg.state,VMA_MERGE_NOMERGE );/* SCENARIO B *
@@ -744,6 +752,7 @@ static bool test_vma_merge_with_close(vo vmg.vma = vma;/* Make sure merge does not occur. */ ASSERT_EQ (merge_existing (&vmg),NULL ); +ASSERT_EQ (vmg.state,VMA_MERGE_NOMERGE );cleanup_mm (&mm, &vmi);return true;
@@ -792,6 +801,7 @@ static bool test_vma_merge_new_with_closvmg_set_range (&vmg,0x2000 ,0x5000 ,2 , flags); vma =merge_new (&vmg);ASSERT_NE (vma,NULL ); +ASSERT_EQ (vmg.state,VMA_MERGE_SUCCESS );ASSERT_EQ (vma->vm_start,0 );ASSERT_EQ (vma->vm_end,0x5000 );ASSERT_EQ (vma->vm_pgoff,0 );
@@ -831,6 +841,7 @@ static bool test_merge_existing(void) vmg.prev = vma; vma->anon_vma = &dummy_anon_vma;ASSERT_EQ (merge_existing (&vmg), vma_next); +ASSERT_EQ (vmg.state,VMA_MERGE_SUCCESS );ASSERT_EQ (vma_next->vm_start,0x3000 );ASSERT_EQ (vma_next->vm_end,0x9000 );ASSERT_EQ (vma_next->vm_pgoff,3 );
@@ -861,6 +872,7 @@ static bool test_merge_existing(void) vmg.vma = vma; vma->anon_vma = &dummy_anon_vma;ASSERT_EQ (merge_existing (&vmg), vma_next); +ASSERT_EQ (vmg.state,VMA_MERGE_SUCCESS );ASSERT_EQ (vma_next->vm_start,0x2000 );ASSERT_EQ (vma_next->vm_end,0x9000 );ASSERT_EQ (vma_next->vm_pgoff,2 );
@@ -889,6 +901,7 @@ static bool test_merge_existing(void) vma->anon_vma = &dummy_anon_vma;ASSERT_EQ (merge_existing (&vmg), vma_prev); +ASSERT_EQ (vmg.state,VMA_MERGE_SUCCESS );ASSERT_EQ (vma_prev->vm_start,0 );ASSERT_EQ (vma_prev->vm_end,0x6000 );ASSERT_EQ (vma_prev->vm_pgoff,0 );
@@ -920,6 +933,7 @@ static bool test_merge_existing(void) vmg.vma = vma; vma->anon_vma = &dummy_anon_vma;ASSERT_EQ (merge_existing (&vmg), vma_prev); +ASSERT_EQ (vmg.state,VMA_MERGE_SUCCESS );ASSERT_EQ (vma_prev->vm_start,0 );ASSERT_EQ (vma_prev->vm_end,0x7000 );ASSERT_EQ (vma_prev->vm_pgoff,0 );
@@ -948,6 +962,7 @@ static bool test_merge_existing(void) vmg.vma = vma; vma->anon_vma = &dummy_anon_vma;ASSERT_EQ (merge_existing (&vmg), vma_prev); +ASSERT_EQ (vmg.state,VMA_MERGE_SUCCESS );ASSERT_EQ (vma_prev->vm_start,0 );ASSERT_EQ (vma_prev->vm_end,0x9000 );ASSERT_EQ (vma_prev->vm_pgoff,0 );
@@ -981,31 +996,37 @@ static bool test_merge_existing(void) vmg.prev = vma; vmg.vma = vma;ASSERT_EQ (merge_existing (&vmg),NULL ); +ASSERT_EQ (vmg.state,VMA_MERGE_NOMERGE );vmg_set_range (&vmg,0x5000 ,0x6000 ,5 , flags); vmg.prev = vma; vmg.vma = vma;ASSERT_EQ (merge_existing (&vmg),NULL ); +ASSERT_EQ (vmg.state,VMA_MERGE_NOMERGE );vmg_set_range (&vmg,0x6000 ,0x7000 ,6 , flags); vmg.prev = vma; vmg.vma = vma;ASSERT_EQ (merge_existing (&vmg),NULL ); +ASSERT_EQ (vmg.state,VMA_MERGE_NOMERGE );vmg_set_range (&vmg,0x4000 ,0x7000 ,4 , flags); vmg.prev = vma; vmg.vma = vma;ASSERT_EQ (merge_existing (&vmg),NULL ); +ASSERT_EQ (vmg.state,VMA_MERGE_NOMERGE );vmg_set_range (&vmg,0x4000 ,0x6000 ,4 , flags); vmg.prev = vma; vmg.vma = vma;ASSERT_EQ (merge_existing (&vmg),NULL ); +ASSERT_EQ (vmg.state,VMA_MERGE_NOMERGE );vmg_set_range (&vmg,0x5000 ,0x6000 ,5 , flags); vmg.prev = vma; vmg.vma = vma;ASSERT_EQ (merge_existing (&vmg),NULL ); +ASSERT_EQ (vmg.state,VMA_MERGE_NOMERGE );ASSERT_EQ (cleanup_mm (&mm, &vmi),3 );
@@ -1071,6 +1092,7 @@ static bool test_anon_vma_non_mergeable( vmg.vma = vma;ASSERT_EQ (merge_existing (&vmg), vma_prev); +ASSERT_EQ (vmg.state,VMA_MERGE_SUCCESS );ASSERT_EQ (vma_prev->vm_start,0 );ASSERT_EQ (vma_prev->vm_end,0x7000 );ASSERT_EQ (vma_prev->vm_pgoff,0 );
@@ -1106,6 +1128,7 @@ static bool test_anon_vma_non_mergeable( vmg.prev = vma_prev;ASSERT_EQ (merge_new (&vmg), vma_prev); +ASSERT_EQ (vmg.state,VMA_MERGE_SUCCESS );ASSERT_EQ (vma_prev->vm_start,0 );ASSERT_EQ (vma_prev->vm_end,0x7000 );ASSERT_EQ (vma_prev->vm_pgoff,0 );
@@ -1181,6 +1204,7 @@ static bool test_dup_anon_vma(void) vmg.vma = vma;ASSERT_EQ (merge_existing (&vmg), vma_prev); +ASSERT_EQ (vmg.state,VMA_MERGE_SUCCESS );ASSERT_EQ (vma_prev->vm_start,0 );ASSERT_EQ (vma_prev->vm_end,0x8000 );
@@ -1209,6 +1233,7 @@ static bool test_dup_anon_vma(void) vmg.vma = vma;ASSERT_EQ (merge_existing (&vmg), vma_prev); +ASSERT_EQ (vmg.state,VMA_MERGE_SUCCESS );ASSERT_EQ (vma_prev->vm_start,0 );ASSERT_EQ (vma_prev->vm_end,0x8000 );
@@ -1236,6 +1261,7 @@ static bool test_dup_anon_vma(void) vmg.vma = vma;ASSERT_EQ (merge_existing (&vmg), vma_prev); +ASSERT_EQ (vmg.state,VMA_MERGE_SUCCESS );ASSERT_EQ (vma_prev->vm_start,0 );ASSERT_EQ (vma_prev->vm_end,0x5000 );
@@ -1263,6 +1289,7 @@ static bool test_dup_anon_vma(void) vmg.vma = vma;ASSERT_EQ (merge_existing (&vmg), vma_next); +ASSERT_EQ (vmg.state,VMA_MERGE_SUCCESS );ASSERT_EQ (vma_next->vm_start,0x3000 );ASSERT_EQ (vma_next->vm_end,0x8000 );
@@ -1303,6 +1330,7 @@ static bool test_vmi_prealloc_fail(void)/* This will cause the merge to fail. */ ASSERT_EQ (merge_existing (&vmg),NULL ); +ASSERT_EQ (vmg.state,VMA_MERGE_ERROR_NOMEM );/* We will already have assigned the anon_vma. */ ASSERT_EQ (vma_prev->anon_vma, &dummy_anon_vma);/* And it was both cloned and unlinked. */
@@ -1327,6 +1355,7 @@ static bool test_vmi_prealloc_fail(void) fail_prealloc = true;ASSERT_EQ (expand_existing (&vmg), -ENOMEM ); +ASSERT_EQ (vmg.state,VMA_MERGE_ERROR_NOMEM );ASSERT_EQ (vma_prev->anon_vma, &dummy_anon_vma);ASSERT_TRUE (dummy_anon_vma.was_cloned);
tools/testing/vma/vma_internal.h renamed from tools/testing/vma/vma_internal.h~mm-avoid-using-vma_merge-for-new-vmas +6 -0
@@ -740,6 +740,12 @@ static inline void vma_iter_free(structmas_destroy (&vmi->mas); } +static inline +struct vm_area_struct *vma_iter_next_range (struct vma_iterator *vmi) +{ +return mas_next_range (&vmi->mas,ULONG_MAX ); +} +static inline void vm_acct_memory (long pages) { }
_
Patches currently in -mm which might be from [email protected] are