This is a note to let you know that I've just added the patch titled mm/mlock: fix vma iterator conversion of apply_vma_lock_flags() to the 6.4-stable tree which can be found at: http://www.kernel.org/git/?p=linux/kernel/git/stable/stable-queue.git;a=summary The filename of the patch is: mm-mlock-fix-vma-iterator-conversion-of-apply_vma_lock_flags.patch and it can be found in the queue-6.4 subdirectory. If you, or anyone else, feels it should not be added to the stable tree, please let <stable@xxxxxxxxxxxxxxx> know about it. >From 2658f94d679243209889cdfa8de3743cde1abea9 Mon Sep 17 00:00:00 2001 From: "Liam R. Howlett" <Liam.Howlett@xxxxxxxxxx> Date: Tue, 11 Jul 2023 13:50:20 -0400 Subject: mm/mlock: fix vma iterator conversion of apply_vma_lock_flags() From: Liam R. Howlett <Liam.Howlett@xxxxxxxxxx> commit 2658f94d679243209889cdfa8de3743cde1abea9 upstream. 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/20230711175020.4091336-1-Liam.Howlett@xxxxxxxxxx Fixes: 37598f5a9d8b ("mlock: convert mlock to vma iterator") Signed-off-by: Liam R. Howlett <Liam.Howlett@xxxxxxxxxx> Reported-by: Ryan Roberts <ryan.roberts@xxxxxxx> Link: https://lore.kernel.org/linux-mm/50341ca1-d582-b33a-e3d0-acb08a65166f@xxxxxxx/ Tested-by: Ryan Roberts <ryan.roberts@xxxxxxx> Cc: <stable@xxxxxxxxxxxxxxx> Signed-off-by: Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx> Signed-off-by: Greg Kroah-Hartman <gregkh@xxxxxxxxxxxxxxxxxxx> --- mm/mlock.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) --- a/mm/mlock.c +++ b/mm/mlock.c @@ -471,7 +471,6 @@ static int apply_vma_lock_flags(unsigned { unsigned long nstart, end, tmp; struct vm_area_struct *vma, *prev; - int error; VMA_ITERATOR(vmi, current->mm, start); VM_BUG_ON(offset_in_page(start)); @@ -492,6 +491,7 @@ static int apply_vma_lock_flags(unsigned nstart = start; tmp = vma->vm_start; for_each_vma_range(vmi, vma, end) { + int error; vm_flags_t newflags; if (vma->vm_start != tmp) @@ -505,14 +505,15 @@ static int apply_vma_lock_flags(unsigned tmp = end; error = mlock_fixup(&vmi, vma, &prev, nstart, tmp, newflags); if (error) - break; + return error; + tmp = vma_iter_end(&vmi); nstart = tmp; } - if (vma_iter_end(&vmi) < end) + if (tmp < end) return -ENOMEM; - return error; + return 0; } /* Patches currently in stable-queue which might be from Liam.Howlett@xxxxxxxxxx are queue-6.4/mm-mlock-fix-vma-iterator-conversion-of-apply_vma_lock_flags.patch queue-6.4/maple_tree-fix-node-allocation-testing-on-32-bit.patch queue-6.4/maple_tree-set-the-node-limit-when-creating-a-new-root-node.patch