Commit 04241ffe authored by Liam R. Howlett's avatar Liam R. Howlett Committed by Andrew Morton

mm/mmap: introduce dup_vma_anon() helper

Create a helper for duplicating the anon vma when adjusting the vma.  This
simplifies the logic of __vma_adjust().

Link: https://lkml.kernel.org/r/20230120162650.984577-47-Liam.Howlett@oracle.comSigned-off-by: default avatarLiam R. Howlett <Liam.Howlett@oracle.com>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
parent cf51e86d
...@@ -617,6 +617,29 @@ static inline void vma_complete(struct vma_prepare *vp, ...@@ -617,6 +617,29 @@ static inline void vma_complete(struct vma_prepare *vp,
uprobe_mmap(vp->insert); uprobe_mmap(vp->insert);
} }
/*
* dup_anon_vma() - Helper function to duplicate anon_vma
* @dst: The destination VMA
* @src: The source VMA
*
* Returns: 0 on success.
*/
static inline int dup_anon_vma(struct vm_area_struct *dst,
struct vm_area_struct *src)
{
/*
* Easily overlooked: when mprotect shifts the boundary, make sure the
* expanding vma has anon_vma set if the shrinking vma had, to cover any
* anon pages imported.
*/
if (src->anon_vma && !dst->anon_vma) {
dst->anon_vma = src->anon_vma;
return anon_vma_clone(dst, src);
}
return 0;
}
/* /*
* vma_expand - Expand an existing VMA * vma_expand - Expand an existing VMA
* *
...@@ -642,15 +665,12 @@ int vma_expand(struct vma_iterator *vmi, struct vm_area_struct *vma, ...@@ -642,15 +665,12 @@ int vma_expand(struct vma_iterator *vmi, struct vm_area_struct *vma,
struct vma_prepare vp; struct vma_prepare vp;
if (next && (vma != next) && (end == next->vm_end)) { if (next && (vma != next) && (end == next->vm_end)) {
remove_next = true; int ret;
if (next->anon_vma && !vma->anon_vma) {
int error;
vma->anon_vma = next->anon_vma; remove_next = true;
error = anon_vma_clone(vma, next); ret = dup_anon_vma(vma, next);
if (error) if (ret)
return error; return ret;
}
} }
init_multi_vma_prep(&vp, vma, NULL, remove_next ? next : NULL, NULL); init_multi_vma_prep(&vp, vma, NULL, remove_next ? next : NULL, NULL);
...@@ -739,10 +759,11 @@ int __vma_adjust(struct vma_iterator *vmi, struct vm_area_struct *vma, ...@@ -739,10 +759,11 @@ int __vma_adjust(struct vma_iterator *vmi, struct vm_area_struct *vma,
struct file *file = vma->vm_file; struct file *file = vma->vm_file;
bool vma_changed = false; bool vma_changed = false;
long adjust_next = 0; long adjust_next = 0;
struct vm_area_struct *exporter = NULL, *importer = NULL;
struct vma_prepare vma_prep; struct vma_prepare vma_prep;
if (next) { if (next) {
int error = 0;
if (end >= next->vm_end) { if (end >= next->vm_end) {
/* /*
* vma expands, overlapping all the next, and * vma expands, overlapping all the next, and
...@@ -777,15 +798,14 @@ int __vma_adjust(struct vma_iterator *vmi, struct vm_area_struct *vma, ...@@ -777,15 +798,14 @@ int __vma_adjust(struct vma_iterator *vmi, struct vm_area_struct *vma,
end != remove2->vm_end); end != remove2->vm_end);
} }
exporter = next;
importer = vma;
/* /*
* If next doesn't have anon_vma, import from vma after * If next doesn't have anon_vma, import from vma after
* next, if the vma overlaps with it. * next, if the vma overlaps with it.
*/ */
if (remove2 != NULL && !next->anon_vma) if (remove != NULL && !next->anon_vma)
exporter = remove2; error = dup_anon_vma(vma, remove2);
else
error = dup_anon_vma(vma, remove);
} else if (end > next->vm_start) { } else if (end > next->vm_start) {
/* /*
...@@ -793,9 +813,8 @@ int __vma_adjust(struct vma_iterator *vmi, struct vm_area_struct *vma, ...@@ -793,9 +813,8 @@ int __vma_adjust(struct vma_iterator *vmi, struct vm_area_struct *vma,
* mprotect case 5 shifting the boundary up. * mprotect case 5 shifting the boundary up.
*/ */
adjust_next = (end - next->vm_start); adjust_next = (end - next->vm_start);
exporter = next; VM_WARN_ON(expand != vma);
importer = vma; error = dup_anon_vma(vma, next);
VM_WARN_ON(expand != importer);
} else if (end < vma->vm_end) { } else if (end < vma->vm_end) {
/* /*
* vma shrinks, and !insert tells it's not * vma shrinks, and !insert tells it's not
...@@ -803,25 +822,12 @@ int __vma_adjust(struct vma_iterator *vmi, struct vm_area_struct *vma, ...@@ -803,25 +822,12 @@ int __vma_adjust(struct vma_iterator *vmi, struct vm_area_struct *vma,
* mprotect case 4 shifting the boundary down. * mprotect case 4 shifting the boundary down.
*/ */
adjust_next = -(vma->vm_end - end); adjust_next = -(vma->vm_end - end);
exporter = vma; VM_WARN_ON(expand != next);
importer = next; error = dup_anon_vma(next, vma);
VM_WARN_ON(expand != importer);
} }
/*
* Easily overlooked: when mprotect shifts the boundary,
* make sure the expanding vma has anon_vma set if the
* shrinking vma had, to cover any anon pages imported.
*/
if (exporter && exporter->anon_vma && !importer->anon_vma) {
int error;
importer->anon_vma = exporter->anon_vma;
error = anon_vma_clone(importer, exporter);
if (error) if (error)
return error; return error;
} }
}
if (vma_iter_prealloc(vmi)) if (vma_iter_prealloc(vmi))
return -ENOMEM; return -ENOMEM;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment