[PATCH v4 1/4] mm/vma: cleanup error handling path in vma_expand()

Suren Baghdasaryan
2026-03-22T05:43:20Z

vma_expand() error handling is a bit confusing with "if (ret) return ret;"
mixed with "if (!ret && ...) ret = ...;". Simplify the code to check
for errors and return immediately after an operation that might fail.
This also makes later changes to this function more readable.
Change variable name for storing the error code from "ret" to "err".

No functional change intended.

Suggested-by: Jann Horn <jannh@google.com>
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
Reviewed-by: Liam R. Howlett <Liam.Howlett@oracle.com>
Reviewed-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
---
mm/vma.c | 18 +++++++++++-------
1 file changed, 11 insertions(+), 7 deletions(-)

diff --git a/mm/vma.c b/mm/vma.c
index a43f3c5d4b3d..ba78ab1f397a 100644
--- a/mm/vma.c
+++ b/mm/vma.c
@@ -1170,7 +1170,7 @@ int vma_expand(struct vma_merge_struct *vmg)
vma_flags_t sticky_flags =
vma_flags_and_mask(&vmg->vma_flags, VMA_STICKY_FLAGS);
vma_flags_t target_sticky;
- int ret = 0;
+ int err = 0;

mmap_assert_write_locked(vmg->mm);
vma_start_write(target);
@@ -1200,12 +1200,16 @@ int vma_expand(struct vma_merge_struct *vmg)
* Note that, by convention, callers ignore OOM for this case, so
* we don't need to account for vmg->give_up_on_mm here.
*/
- if (remove_next)
- ret = dup_anon_vma(target, next, &anon_dup);
- if (!ret && vmg->copied_from)
- ret = dup_anon_vma(target, vmg->copied_from, &anon_dup);
- if (ret)
- return ret;
+ if (remove_next) {
+ err = dup_anon_vma(target, next, &anon_dup);
+ if (err)
+ return err;
+ }
+ if (vmg->copied_from) {
+ err = dup_anon_vma(target, vmg->copied_from, &anon_dup);
+ if (err)
+ return err;
+ }

if (remove_next) {
vma_flags_t next_sticky;
--
2.53.0.1018.g2bb0e51243-goog