]> www.infradead.org Git - users/jedix/linux-maple.git/commitdiff
mm/mmap: Don't use __vma_adjust() in shift_arg_pages()
authorLiam R. Howlett <Liam.Howlett@Oracle.com>
Tue, 15 Nov 2022 19:57:13 +0000 (14:57 -0500)
committerLiam R. Howlett <Liam.Howlett@oracle.com>
Mon, 28 Nov 2022 21:01:17 +0000 (16:01 -0500)
Introduce shrink_vma() which uses the vma_prepare() and vma_complete()
functions to reduce the vma coverage.

Convert shift_arg_pages() to use expand_vma() and the new shrink_vma()
function.  Remove support from __vma_adjust() to reduce a vma size since
shift_arg_pages() is the only user that shrinks a VMA in this way.

Signed-off-by: Liam R. Howlett <Liam.Howlett@oracle.com>
fs/exec.c
include/linux/mm.h
mm/mmap.c

index b3fa6dd2d0e80995152a8e1b9e9390e526cd3cc9..8903351e101b96fa2def43728c6a48b59e7cfb43 100644 (file)
--- a/fs/exec.c
+++ b/fs/exec.c
@@ -698,7 +698,7 @@ static int shift_arg_pages(struct vm_area_struct *vma, unsigned long shift)
        /*
         * cover the whole range: [new_start, old_end)
         */
-       if (vma_adjust(&vmi, vma, new_start, old_end, vma->vm_pgoff))
+       if (vma_expand(&vmi, vma, new_start, old_end, vma->vm_pgoff, NULL))
                return -ENOMEM;
 
        /*
@@ -732,7 +732,7 @@ static int shift_arg_pages(struct vm_area_struct *vma, unsigned long shift)
 
        vma_prev(&vmi);
        /* Shrink the vma to just the new range */
-       return vma_adjust(&vmi, vma, new_start, new_end, vma->vm_pgoff);
+       return vma_shrink(&vmi, vma, new_start, new_end, vma->vm_pgoff);
 }
 
 /*
index 13abf070e911b405361ec3be16c5030430f620dd..5483041ef7d8db464810d565e92e5014b5bfe673 100644 (file)
@@ -2692,14 +2692,11 @@ void anon_vma_interval_tree_verify(struct anon_vma_chain *node);
 
 /* mmap.c */
 extern int __vm_enough_memory(struct mm_struct *mm, long pages, int cap_sys_admin);
-extern int __vma_adjust(struct vma_iterator *vmi, struct vm_area_struct *vma, unsigned long start,
-       unsigned long end, pgoff_t pgoff, struct vm_area_struct *expand);
-static inline int vma_adjust(struct vma_iterator *vmi,
-       struct vm_area_struct *vma, unsigned long start, unsigned long end,
-       pgoff_t pgoff)
-{
-       return __vma_adjust(vmi, vma, start, end, pgoff, NULL);
-}
+extern int vma_expand(struct vma_iterator *vmi, struct vm_area_struct *vma,
+                     unsigned long start, unsigned long end, pgoff_t pgoff,
+                     struct vm_area_struct *next);
+extern int vma_shrink(struct vma_iterator *vmi, struct vm_area_struct *vma,
+                      unsigned long start, unsigned long end, pgoff_t pgoff);
 extern struct vm_area_struct *vma_merge(struct vma_iterator *vmi,
        struct mm_struct *, struct vm_area_struct *prev, unsigned long addr,
        unsigned long end, unsigned long vm_flags, struct anon_vma *,
index 900a5f2d58d1fcbe0893d0886feac767525a8273..1d76cc5bcb778848479f5c834fc53df78416afae 100644 (file)
--- a/mm/mmap.c
+++ b/mm/mmap.c
@@ -694,10 +694,9 @@ again:
  *
  * Returns: 0 on success
  */
-inline int vma_expand(struct vma_iterator *vmi, struct vm_area_struct *vma,
-                     unsigned long start, unsigned long end, pgoff_t pgoff,
-                     struct vm_area_struct *next)
-
+int vma_expand(struct vma_iterator *vmi, struct vm_area_struct *vma,
+              unsigned long start, unsigned long end, pgoff_t pgoff,
+              struct vm_area_struct *next)
 {
        bool remove_next = false;
        struct vma_prepare vp;
@@ -743,6 +742,44 @@ inline int vma_expand(struct vma_iterator *vmi, struct vm_area_struct *vma,
 nomem:
        return -ENOMEM;
 }
+
+/*
+ * vma_shrink() - Reduce an existing VMAs memory area
+ * @mas: The maple state
+ * @vma: The VMA to modify
+ * @start: The new start
+ * @end: The new end
+ *
+ * Returns: 0 on success, -ENOMEM otherwise
+ */
+int vma_shrink(struct vma_iterator *vmi, struct vm_area_struct *vma,
+              unsigned long start, unsigned long end, pgoff_t pgoff)
+{
+       struct vma_prepare vp;
+
+       WARN_ON((vma->vm_start != start) && (vma->vm_end != end));
+
+       if (vma_iter_prealloc(vmi, vma))
+               return -ENOMEM;
+
+       init_vma_prep(&vp, vma);
+       vma_adjust_trans_huge(vma, start, end, 0);
+       vma_prepare(&vp);
+
+       if (vma->vm_start < start)
+               vma_iter_clear(vmi, vma->vm_start, start);
+
+       if (vma->vm_end > end)
+               vma_iter_clear(vmi, end, vma->vm_end);
+
+       vma->vm_start = start;
+       vma->vm_end = end;
+       vma->vm_pgoff = pgoff;
+       vma_complete(&vp, vmi, vma->vm_mm);
+       validate_mm(vma->vm_mm);
+       return 0;
+}
+
 /*
  * We cannot adjust vm_start, vm_end, vm_pgoff fields of a vma that
  * is already present in an i_mmap tree without adjusting the tree.
@@ -858,17 +895,8 @@ int __vma_adjust(struct vma_iterator *vmi, struct vm_area_struct *vma,
 
        vma_prepare(&vma_prep);
 
-       if (vma->vm_start < start) {
-               vma_iter_clear(vmi, vma->vm_start, start);
-       } else if (start != vma->vm_start) {
-               vma_changed = true;
-       }
-
-       if (vma->vm_end > end) {
-               vma_iter_clear(vmi, end, vma->vm_end);
-       } else if (end != vma->vm_end) {
+       if (start < vma->vm_start || end > vma->vm_end)
                vma_changed = true;
-       }
 
        vma->vm_start = start;
        vma->vm_end = end;
@@ -880,7 +908,10 @@ int __vma_adjust(struct vma_iterator *vmi, struct vm_area_struct *vma,
        if (adjust_next) {
                next->vm_start += adjust_next;
                next->vm_pgoff += adjust_next >> PAGE_SHIFT;
-               vma_iter_store(vmi, next);
+               if (adjust_next < 0) {
+                       BUG_ON(vma_changed);
+                       vma_iter_store(vmi, next);
+               }
        }
 
        vma_complete(&vma_prep, vmi, mm);