]> www.infradead.org Git - users/jedix/linux-maple.git/commitdiff
mm/nommu: fix error handling in split_vma()
authorYang Yingliang <yangyingliang@huawei.com>
Wed, 24 Aug 2022 04:24:24 +0000 (12:24 +0800)
committerAndrew Morton <akpm@linux-foundation.org>
Fri, 26 Aug 2022 05:03:21 +0000 (22:03 -0700)
The memory allocated before calling mas_preallocate() is leaked if it
fails.  'mas' won't be modify until calling mas_preallocate(), so move it
up and add error label for free the memory.

Link: https://lkml.kernel.org/r/20220824042424.2031508-1-yangyingliang@huawei.com
Fixes: 8aff7dbeaeb1 ("nommu: remove uses of VMA linked list")
Signed-off-by: Yang Yingliang <yangyingliang@huawei.com>
Reviewed-by: Liam R. Howlett <Liam.Howlett@oracle.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
mm/nommu.c

index 171faa07e577a2a57604987bcc76785c89c0c9f9..269df51e92266ed9d7c9bac672af444a8059f25e 100644 (file)
@@ -1361,9 +1361,13 @@ int split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
                return -ENOMEM;
 
        new = vm_area_dup(vma);
-       if (!new) {
-               kmem_cache_free(vm_region_jar, region);
-               return -ENOMEM;
+       if (!new)
+               goto err_vma_dup;
+
+       if (mas_preallocate(&mas, vma, GFP_KERNEL)) {
+               pr_warn("Allocation of vma tree for process %d failed\n",
+                       current->pid);
+               goto err_mas_preallocate;
        }
 
        /* most fields are the same, copy all, and then fixup */
@@ -1394,11 +1398,6 @@ int split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
        add_nommu_region(vma->vm_region);
        add_nommu_region(new->vm_region);
        up_write(&nommu_region_sem);
-       if (mas_preallocate(&mas, vma, GFP_KERNEL)) {
-               pr_warn("Allocation of vma tree for process %d failed\n",
-                      current->pid);
-               return -ENOMEM;
-       }
 
        setup_vma_to_mm(vma, mm);
        setup_vma_to_mm(new, mm);
@@ -1406,6 +1405,12 @@ int split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
        mas_store(&mas, vma);
        vma_mas_store(new, &mas);
        return 0;
+
+err_mas_preallocate:
+       vm_area_free(new);
+err_vma_dup:
+       kmem_cache_free(vm_region_jar, region);
+       return -ENOMEM;
 }
 
 /*