mm/mlock: set the correct prev on failure
Subsystems (2)
PATCH 1/1 mm/mlock: set the correct prev on failure
After commit 94d7d9233951 ("mm: abstract the vma_merge()/split_vma() pattern for mprotect() et al."), if vma_modify_flags() return error, the vma is set to an error code. This will lead to an invalid prev be returned.
--- mm/mlock.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
mm/mlock.c +4 -3
@@ -478,11 +478,12 @@ static int mlock_fixup(struct vma_iterator *vmi, struct vm_area_struct *vma,/* don't set VM_LOCKED or VM_LOCKONFAULT and don't count */ goto out; - vma =vma_modify_flags (vmi, *prev, vma, start, end, newflags); -if (IS_ERR (vma)) { - ret =PTR_ERR (vma); + *prev =vma_modify_flags (vmi, *prev, vma, start, end, newflags); +if (IS_ERR (*prev)) { + ret =PTR_ERR (*prev); goto out; } + vma = *prev;/* * Keep track of amount of locked VM.
Signature · 1 line
2.34.1
+ Vlastimil, Liam, Jann as this is VMA-related.
We really need to bring all VMA-ish files under the VMA MAINTAINERS block... will maybe address that once things around that file... calm down a bit.
But please cc all of us on anything that even vaguely relates to VMAs, thanks!
Quoted 4 lines · Wei Yang
After commit
94d7d9233951("mm: abstract the vma_merge()/split_vma() pattern formprotect()et al."), ifvma_modify_flags()return error, the vma is set to an error code. This will lead to an invalid prev be returned.
This is a great spot, but this commit message is missing critical details. This is only meaningful for apply_mlockall_flags() which is both ignoring errors AND assuming mlock_fixup(), even on error, is correctly updating the prev state. Which is imo wrong.
So I'd _add_ a bit more information here like:
Generally this shouldn't matter as the caller should treat an error as indicating state is now invalidated, however unfortunately apply_mlockall_flags() does not check for errors and assumes that mlock_fixup() correctly maintains prev even if an error were to occur.
This patch fixes that assumption.
We'll also need to backport this, so a:
Needs to be added, and make the next revision [PATCH hotfix 6.12 v2] to make it clear this needs to go to 6.12.
Quoted 14 lines
Signed-off-by Wei Yang <[email protected]>CC Lorenzo Stoakes <[email protected]>--- mm/mlock.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)mm/mlock.c +0
-0@@ -478,11 +478,12 @@ static int mlock_fixup(struct vma_iterator *vmi, struct vm_area_struct *vma,/* don't set VM_LOCKED or VM_LOCKONFAULT and don't count */ goto out;
mm/mlock.c static int mlock_fixup(struct vma_iterator *vmi, struct vm_area_struct *vma, · line 486
Quoted 10 lines
mm/mlock.c +4
-3- vma =vma_modify_flags (vmi, *prev, vma, start, end, newflags); -if (IS_ERR (vma)) { - ret =PTR_ERR (vma); + *prev =vma_modify_flags (vmi, *prev, vma, start, end, newflags); +if (IS_ERR (*prev)) { + ret =PTR_ERR (*prev); goto out; } + vma = *prev;
Yeah sorry I hate this, it was icky before and it's kinda disgusting to assign *prev then *prev to vma, then, if success, vma to *prev - yeah that's super confusing :)
I mean a better alternative if you were to do this approach would be to have a new vma local but I don't actually think that's the correct approach.
Really the caller _must_ deal with errors, and not assume any state is valid after an error occurs.
So I think the fix should be in apply_mlockall_flags() instead like:
...
for_each_vma(vmi, vma) {
...
int error;
...
error = mlock_fixup(&vmi, vma, &prev, vma->vm_start, vma->vm_end,
newflags);
/* Ignore errors, but prev needs fixing up. */
if (error)
prev = vma;
...
}This is also a smaller delta for backporting.
Quoted 7 lines
mm/mlock.c +0
-1/* * Keep track of amount of locked VM. -- 2.34.1
I'm happy for you to resubmit like this and take full credit by the way! :) assuming you agree with this approach.
This is also reminding me that I need to refactor all this crap, the whole passing prev around and looping like that is horrible. Also the outer loop should be maintaining prev, not the inner one.
This is going on my TODO list!
Quoted 9 lines · Lorenzo Stoakes
+ Vlastimil, Liam, Jann as this is VMA-related.
We really need to bring all VMA-ish files under the VMA
MAINTAINERSblock... will maybe address that once things around that file... calm down a bit.But please cc all of us on anything that even vaguely relates to VMAs, thanks!
Sure, will add them in later change.
Quoted 11 lines · Wei Yang
Quoted 4 lines
After commit
94d7d9233951("mm: abstract the vma_merge()/split_vma() pattern formprotect()et al."), ifvma_modify_flags()return error, the vma is set to an error code. This will lead to an invalid prev be returned.This is a great spot, but this commit message is missing critical details. This is only meaningful for
apply_mlockall_flags()which is both ignoring errors AND assumingmlock_fixup(), even on error, is correctly updating the prev state. Which is imo wrong.
Yes.
Quoted 9 lines
So I'd _add_ a bit more information here like:
Generally this shouldn't matter as the caller should treat an error as indicating state is now invalidated, however unfortunately
apply_mlockall_flags()does not check for errors and assumes thatmlock_fixup()correctly maintains prev even if an error were to occur.This patch fixes that assumption.
Quoted 8 lines
We'll also need to backport this, so a:
Fixes 94d7d9233951 ("mm: abstract the vma_merge()/split_vma() pattern for mprotect() et al.")Cc <[email protected]>Needs to be added, and make the next revision [PATCH hotfix 6.12 v2] to make it clear this needs to go to 6.12.
Will follow this.
Quoted 55 lines
Quoted 14 lines
Signed-off-by Wei Yang <[email protected]>CC Lorenzo Stoakes <[email protected]>--- mm/mlock.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)mm/mlock.c +0
-0@@ -478,11 +478,12 @@ static int mlock_fixup(struct vma_iterator *vmi, struct vm_area_struct *vma,/* don't set VM_LOCKED or VM_LOCKONFAULT and don't count */ goto out;mm/mlock.c static int mlock_fixup(struct vma_iterator *vmi, struct vm_area_struct *vma, · line 486
Quoted 10 lines
mm/mlock.c +4
-3- vma =vma_modify_flags (vmi, *prev, vma, start, end, newflags); -if (IS_ERR (vma)) { - ret =PTR_ERR (vma); + *prev =vma_modify_flags (vmi, *prev, vma, start, end, newflags); +if (IS_ERR (*prev)) { + ret =PTR_ERR (*prev); goto out; } + vma = *prev;Yeah sorry I hate this, it was icky before and it's kinda disgusting to assign *prev then *prev to vma, then, if success, vma to *prev - yeah that's super confusing :)
I mean a better alternative if you were to do this approach would be to have a new vma local but I don't actually think that's the correct approach.
Really the caller _must_ deal with errors, and not assume any state is valid after an error occurs.
So I think the fix should be in
apply_mlockall_flags()instead like:... for_each_vma(vmi, vma) { ... int error; ... error = mlock_fixup(&vmi, vma, &prev, vma->vm_start, vma->vm_end, newflags); /* Ignore errors, but prev needs fixing up. */ if (error) prev = vma; ... }
Quoted 2 lines
This is also a smaller delta for backporting.
I have to say this one look better.
Thanks
Quoted 17 lines
Quoted 8 lines
mm/mlock.c +0
-1/* * Keep track of amount of locked VM. -- 2.34.1
I'm happy for you to resubmit like this and take full credit by the way! :) assuming you agree with this approach.
This is also reminding me that I need to refactor all this crap, the whole passing prev around and looping like that is horrible. Also the outer loop should be maintaining prev, not the inner one.
This is going on my TODO list!
Signature · 2 lines
Wei Yang Help you, Help me
On Sun, Oct 27, 2024 at 11:41:13AM +0000, Lorenzo Stoakes wrote:
[...]
Quoted 10 lines
Quoted 2 lines
I'm happy for you to resubmit like this and take full credit by the way! :) assuming you agree with this approach.
This is also reminding me that I need to refactor all this crap, the whole passing prev around and looping like that is horrible. Also the outer loop should be maintaining prev, not the inner one.
Looping like for_each_vma()/for_each_vma_range(). Any other stuff?
If you would like to name others, I will pay attention later.
Quoted 1 line
This is going on my TODO list!
Signature · 2 lines
Wei Yang Help you, Help me
Quoted 52 lines · Lorenzo Stoakes
+ Vlastimil, Liam, Jann as this is VMA-related.
We really need to bring all VMA-ish files under the VMA
MAINTAINERSblock... will maybe address that once things around that file... calm down a bit.But please cc all of us on anything that even vaguely relates to VMAs, thanks!
Quoted 4 lines · Wei Yang
After commit
94d7d9233951("mm: abstract the vma_merge()/split_vma() pattern formprotect()et al."), ifvma_modify_flags()return error, the vma is set to an error code. This will lead to an invalid prev be returned.This is a great spot, but this commit message is missing critical details. This is only meaningful for
apply_mlockall_flags()which is both ignoring errors AND assumingmlock_fixup(), even on error, is correctly updating the prev state. Which is imo wrong.So I'd _add_ a bit more information here like:
Generally this shouldn't matter as the caller should treat an error as indicating state is now invalidated, however unfortunately
apply_mlockall_flags()does not check for errors and assumes thatmlock_fixup()correctly maintains prev even if an error were to occur.This patch fixes that assumption.
We'll also need to backport this, so a:
Fixes 94d7d9233951 ("mm: abstract the vma_merge()/split_vma() pattern for mprotect() et al.")Cc <[email protected]>Needs to be added, and make the next revision [PATCH hotfix 6.12 v2] to make it clear this needs to go to 6.12.
Quoted 15 lines
Signed-off-by Wei Yang <[email protected]>CC Lorenzo Stoakes <[email protected]>--- mm/mlock.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)mm/mlock.c +0
-0@@ -478,11 +478,12 @@ static int mlock_fixup(struct vma_iterator *vmi, struct vm_area_struct *vma,/* don't set VM_LOCKED or VM_LOCKONFAULT and don't count */ goto out;
Quoted 9 lines
mm/mlock.c static int mlock_fixup(struct vma_iterator *vmi, struct vm_area_struct *vma, · line 486
Quoted 9 lines
mm/mlock.c +4
-3- vma =vma_modify_flags (vmi, *prev, vma, start, end, newflags); -if (IS_ERR (vma)) { - ret =PTR_ERR (vma); + *prev =vma_modify_flags (vmi, *prev, vma, start, end, newflags); +if (IS_ERR (*prev)) { + ret =PTR_ERR (*prev); goto out; } + vma = *prev;
If we move the assignment *prev = vma to the start of this function, then we can just get rid of the "out:" label and return on errors.
But the v2 seems fine,
Liam
Quoted 53 lines · Liam R. Howlett
Quoted 52 lines · Lorenzo Stoakes
+ Vlastimil, Liam, Jann as this is VMA-related.
We really need to bring all VMA-ish files under the VMA
MAINTAINERSblock... will maybe address that once things around that file... calm down a bit.But please cc all of us on anything that even vaguely relates to VMAs, thanks!
Quoted 4 lines · Wei Yang
After commit
94d7d9233951("mm: abstract the vma_merge()/split_vma() pattern formprotect()et al."), ifvma_modify_flags()return error, the vma is set to an error code. This will lead to an invalid prev be returned.This is a great spot, but this commit message is missing critical details. This is only meaningful for
apply_mlockall_flags()which is both ignoring errors AND assumingmlock_fixup(), even on error, is correctly updating the prev state. Which is imo wrong.So I'd _add_ a bit more information here like:
Generally this shouldn't matter as the caller should treat an error as indicating state is now invalidated, however unfortunately
apply_mlockall_flags()does not check for errors and assumes thatmlock_fixup()correctly maintains prev even if an error were to occur.This patch fixes that assumption.
We'll also need to backport this, so a:
Fixes 94d7d9233951 ("mm: abstract the vma_merge()/split_vma() pattern for mprotect() et al.")Cc <[email protected]>Needs to be added, and make the next revision [PATCH hotfix 6.12 v2] to make it clear this needs to go to 6.12.
Quoted 15 lines
Signed-off-by Wei Yang <[email protected]>CC Lorenzo Stoakes <[email protected]>--- mm/mlock.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)mm/mlock.c +0
-0@@ -478,11 +478,12 @@ static int mlock_fixup(struct vma_iterator *vmi, struct vm_area_struct *vma,/* don't set VM_LOCKED or VM_LOCKONFAULT and don't count */ goto out;
Quoted 14 lines
Quoted 9 lines
mm/mlock.c static int mlock_fixup(struct vma_iterator *vmi, struct vm_area_struct *vma, · line 486
Quoted 9 lines
mm/mlock.c +4
-3- vma =vma_modify_flags (vmi, *prev, vma, start, end, newflags); -if (IS_ERR (vma)) { - ret =PTR_ERR (vma); + *prev =vma_modify_flags (vmi, *prev, vma, start, end, newflags); +if (IS_ERR (*prev)) { + ret =PTR_ERR (*prev); goto out; } + vma = *prev;If we move the assignment *prev = vma to the start of this function, then we can just get rid of the "out:" label and return on errors.
Maybe not, if my understanding is correct.
vma = vma_modify_flags(, vma, ...)
vma_modify()
merged = vma_merge_existing_range()
return merged
For example, if we merge left, the returned one would be the prev instead of vma we passed in. Even we may release the original vma.
Quoted 2 lines
But the v2 seems fine,
Liam
Signature · 2 lines
Wei Yang Help you, Help me