__split_vma() has its failure paths in place but ignores the result of vm_ops->open(). Add vma_open() and fail the split with its error. Nothing has been done to the original VMA at this point, so the unwind only has to undo the work on the new one. The call moves ahead of get_file() so that there's no file reference to drop, the original VMA holds one for as long as this runs. Signed-off-by: Josef Bacik --- mm/internal.h | 15 +++++++++++++++ mm/vma.c | 9 ++++++--- tools/testing/vma/include/custom.h | 3 ++- tools/testing/vma/include/dup.h | 7 +++++++ 4 files changed, 30 insertions(+), 4 deletions(-) diff --git a/mm/internal.h b/mm/internal.h index cd3675c044e0..ae7aae28a2be 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -218,6 +218,21 @@ static inline void *folio_raw_mapping(const struct folio *folio) return (void *)(mapping & ~FOLIO_MAPPING_FLAGS); } +/* + * vma_open - Call vm_ops->open() for a new VMA, if there is one. + * @vma: The new VMA, which isn't in the VMA tree yet. + * + * On failure the caller discards the VMA without calling vma_close(). + * + * Returns: 0 on success, or an error otherwise. + */ +static inline int vma_open(struct vm_area_struct *vma) +{ + if (vma->vm_ops && vma->vm_ops->open) + return vma->vm_ops->open(vma); + return 0; +} + /* * If the VMA has a close hook then close it, and since closing it might leave * it in an inconsistent state which makes the use of any hooks suspect, clear diff --git a/mm/vma.c b/mm/vma.c index c82e9306b11a..212c2497bad4 100644 --- a/mm/vma.c +++ b/mm/vma.c @@ -570,12 +570,13 @@ __split_vma(struct vma_iterator *vmi, struct vm_area_struct *vma, if (err) goto out_free_mpol; + err = vma_open(new); + if (err) + goto out_unlink_anon; + if (new->vm_file) get_file(new->vm_file); - if (new->vm_ops && new->vm_ops->open) - new->vm_ops->open(new); - vma_start_write(new); init_vma_prep(&vp, vma); @@ -611,6 +612,8 @@ __split_vma(struct vma_iterator *vmi, struct vm_area_struct *vma, return 0; +out_unlink_anon: + unlink_anon_vmas(new); out_free_mpol: mpol_put(vma_policy(new)); out_free_vmi: diff --git a/tools/testing/vma/include/custom.h b/tools/testing/vma/include/custom.h index 744fe874c168..66a54ca9fd7a 100644 --- a/tools/testing/vma/include/custom.h +++ b/tools/testing/vma/include/custom.h @@ -35,7 +35,8 @@ struct anon_vma { static inline void unlink_anon_vmas(struct vm_area_struct *vma) { /* For testing purposes, indicate that the anon_vma was unlinked. */ - vma->anon_vma->was_unlinked = true; + if (vma->anon_vma) + vma->anon_vma->was_unlinked = true; } static inline void vma_start_write(struct vm_area_struct *vma) diff --git a/tools/testing/vma/include/dup.h b/tools/testing/vma/include/dup.h index d418d9000b63..83d6caf16753 100644 --- a/tools/testing/vma/include/dup.h +++ b/tools/testing/vma/include/dup.h @@ -1307,6 +1307,13 @@ static inline int vma_iter_clear_gfp(struct vma_iterator *vmi, return 0; } +static inline int vma_open(struct vm_area_struct *vma) +{ + if (vma->vm_ops && vma->vm_ops->open) + return vma->vm_ops->open(vma); + return 0; +} + static inline void vma_set_anonymous(struct vm_area_struct *vma) { vma->vm_ops = NULL; -- 2.55.0