Skip to content

Commit 2658f94

Browse files
howlettakpm00
authored andcommitted
mm/mlock: fix vma iterator conversion of apply_vma_lock_flags()
apply_vma_lock_flags() calls mlock_fixup(), which could merge the VMA after where the vma iterator is located. Although this is not an issue, the next iteration of the loop will check the start of the vma to be equal to the locally saved 'tmp' variable and cause an incorrect failure scenario. Fix the error by setting tmp to the end of the vma iterator value before restarting the loop. There is also a potential of the error code being overwritten when the loop terminates early. Fix the return issue by directly returning when an error is encountered since there is nothing to undo after the loop. Link: https://lkml.kernel.org/r/[email protected] Fixes: 37598f5 ("mlock: convert mlock to vma iterator") Signed-off-by: Liam R. Howlett <[email protected]> Reported-by: Ryan Roberts <[email protected]> Link: https://lore.kernel.org/linux-mm/[email protected]/ Tested-by: Ryan Roberts <[email protected]> Cc: <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent 636e348 commit 2658f94

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

mm/mlock.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,6 @@ static int apply_vma_lock_flags(unsigned long start, size_t len,
477477
{
478478
unsigned long nstart, end, tmp;
479479
struct vm_area_struct *vma, *prev;
480-
int error;
481480
VMA_ITERATOR(vmi, current->mm, start);
482481

483482
VM_BUG_ON(offset_in_page(start));
@@ -498,6 +497,7 @@ static int apply_vma_lock_flags(unsigned long start, size_t len,
498497
nstart = start;
499498
tmp = vma->vm_start;
500499
for_each_vma_range(vmi, vma, end) {
500+
int error;
501501
vm_flags_t newflags;
502502

503503
if (vma->vm_start != tmp)
@@ -511,14 +511,15 @@ static int apply_vma_lock_flags(unsigned long start, size_t len,
511511
tmp = end;
512512
error = mlock_fixup(&vmi, vma, &prev, nstart, tmp, newflags);
513513
if (error)
514-
break;
514+
return error;
515+
tmp = vma_iter_end(&vmi);
515516
nstart = tmp;
516517
}
517518

518-
if (vma_iter_end(&vmi) < end)
519+
if (tmp < end)
519520
return -ENOMEM;
520521

521-
return error;
522+
return 0;
522523
}
523524

524525
/*

0 commit comments

Comments
 (0)