Skip to content

Commit 659c55e

Browse files
yangx-jyakpm00
authored andcommitted
mm/vma: return the exact errno in vms_gather_munmap_vmas()
__split_vma() and mas_store_gfp() returns several types of errno on failure so don't ignore them in vms_gather_munmap_vmas(). For example, __split_vma() returns -EINVAL when an unaligned huge page is unmapped. This issue is reproduced by ltp memfd_create03 test. Don't initialise the error variable and assign it when a failure actually occurs. [[email protected]: fix whitespace, per Liam] Link: https://lkml.kernel.org/r/[email protected] Fixes: 6898c90 ("mm/vma: extract the gathering of vmas from do_vmi_align_munmap()") Signed-off-by: Xiao Yang <[email protected]> Suggested-by: Lorenzo Stoakes <[email protected]> Reported-by: kernel test robot <[email protected]> Closes: https://lore.kernel.org/oe-lkp/[email protected] Cc: "Liam R. Howlett" <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent f2c5101 commit 659c55e

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

mm/vma.c

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1171,13 +1171,13 @@ void vms_complete_munmap_vmas(struct vma_munmap_struct *vms,
11711171
* @vms: The vma munmap struct
11721172
* @mas_detach: The maple state tracking the detached tree
11731173
*
1174-
* Return: 0 on success, -EPERM on mseal vmas, -ENOMEM otherwise
1174+
* Return: 0 on success, error otherwise
11751175
*/
11761176
int vms_gather_munmap_vmas(struct vma_munmap_struct *vms,
11771177
struct ma_state *mas_detach)
11781178
{
11791179
struct vm_area_struct *next = NULL;
1180-
int error = -ENOMEM;
1180+
int error;
11811181

11821182
/*
11831183
* If we need to split any vma, do it now to save pain later.
@@ -1191,16 +1191,19 @@ int vms_gather_munmap_vmas(struct vma_munmap_struct *vms,
11911191
* its limit temporarily, to help free resources as expected.
11921192
*/
11931193
if (vms->end < vms->vma->vm_end &&
1194-
vms->vma->vm_mm->map_count >= sysctl_max_map_count)
1194+
vms->vma->vm_mm->map_count >= sysctl_max_map_count) {
1195+
error = -ENOMEM;
11951196
goto map_count_exceeded;
1197+
}
11961198

11971199
/* Don't bother splitting the VMA if we can't unmap it anyway */
11981200
if (!can_modify_vma(vms->vma)) {
11991201
error = -EPERM;
12001202
goto start_split_failed;
12011203
}
12021204

1203-
if (__split_vma(vms->vmi, vms->vma, vms->start, 1))
1205+
error = __split_vma(vms->vmi, vms->vma, vms->start, 1);
1206+
if (error)
12041207
goto start_split_failed;
12051208
}
12061209
vms->prev = vma_prev(vms->vmi);
@@ -1220,12 +1223,14 @@ int vms_gather_munmap_vmas(struct vma_munmap_struct *vms,
12201223
}
12211224
/* Does it split the end? */
12221225
if (next->vm_end > vms->end) {
1223-
if (__split_vma(vms->vmi, next, vms->end, 0))
1226+
error = __split_vma(vms->vmi, next, vms->end, 0);
1227+
if (error)
12241228
goto end_split_failed;
12251229
}
12261230
vma_start_write(next);
12271231
mas_set(mas_detach, vms->vma_count++);
1228-
if (mas_store_gfp(mas_detach, next, GFP_KERNEL))
1232+
error = mas_store_gfp(mas_detach, next, GFP_KERNEL);
1233+
if (error)
12291234
goto munmap_gather_failed;
12301235

12311236
vma_mark_detached(next, true);
@@ -1255,8 +1260,9 @@ int vms_gather_munmap_vmas(struct vma_munmap_struct *vms,
12551260
* split, despite we could. This is unlikely enough
12561261
* failure that it's not worth optimizing it for.
12571262
*/
1258-
if (userfaultfd_unmap_prep(next, vms->start, vms->end,
1259-
vms->uf))
1263+
error = userfaultfd_unmap_prep(next, vms->start,
1264+
vms->end, vms->uf);
1265+
if (error)
12601266
goto userfaultfd_error;
12611267
}
12621268
#ifdef CONFIG_DEBUG_VM_MAPLE_TREE

0 commit comments

Comments
 (0)