mm/rmap: clear vma->anon_vma on error
Subsystems (2)
Lorenzo Stoakes (Oracle)
maintainerPATCH 1/1 mm/rmap: clear vma->anon_vma on error
Commit 542eda1a8329 ("mm/rmap: improve anon_vma_clone(), unlink_anon_vmas() comments, add asserts") alters the way errors are handled, but overlooked one important aspect of clean up.
When a VMA encounters an error state in anon_vma_clone() (that is, on attempted allocation of anon_vma_chain objects), it cleans up partially established state in cleanup_partial_anon_vmas(), before returning an error.
However, this occurs prior to anon_vma->num_active_vmas being incremented, and it also fails to clear the VMA's vma->anon_vma field, which remains in place.
This is immediately an inconsistent state, because anon_vma->num_active_vmas is supposed to track the number of VMAs whose vma->anon_vma field references that anon_vma, and now that count is off-by-negative-1 for each VMA for which this error state has occurred.
When VMAs are unlinked from this anon_vma, unlink_anon_vmas() will eventually underflow anon_vma->num_active_vmas, which will trigger a warning.
This will always eventually happen, as we unlink anon_vma's at process teardown.
It could also cause maybe_reuse_anon_vma() to incorrectly permit the reuse of an anon_vma which has active VMAs attached, which will lead to a persistently invalid state.
The solution is to clear the VMA's anon_vma field when we clean up partial state, as the fact we are doing so indicates clearly that the VMA is not correctly integrated into the anon_vma tree and thus this field is invalid.
--- mm/rmap.c | 7 +++++++ 1 file changed, 7 insertions(+)
mm/rmap.c +7 -0
@@ -457,6 +457,13 @@ static void cleanup_partial_anon_vmas(struct vm_area_struct *vma)list_del (&avc->same_vma);anon_vma_chain_free (avc); } + +/* +* The anon_vma assigned to this VMA is no longer valid, as we were not +* able to correctly clone AVC state. Avoid inconsistent anon_vma tree +* state by resetting. +*/ + vma->anon_vma =NULL ; }/**
--
2.53.0
Quoted 48 lines · Lorenzo Stoakes (Oracle)
Commit
542eda1a8329("mm/rmap: improveanon_vma_clone(),unlink_anon_vmas()comments, add asserts") alters the way errors are handled, but overlooked one important aspect of clean up.When a VMA encounters an error state in
anon_vma_clone()(that is, on attempted allocation ofanon_vma_chainobjects), it cleans up partially established state incleanup_partial_anon_vmas(), before returning an error.However, this occurs prior to
anon_vma->num_active_vmasbeing incremented, and it also fails to clear the VMA'svma->anon_vmafield, which remains in place.This is immediately an inconsistent state, because
anon_vma->num_active_vmasis supposed to track the number of VMAs whosevma->anon_vmafield references thatanon_vma, and now that count is off-by-negative-1 for each VMA for which this error state has occurred.When VMAs are unlinked from this
anon_vma,unlink_anon_vmas()will eventually underflowanon_vma->num_active_vmas, which will trigger a warning.This will always eventually happen, as we unlink anon_vma's at process teardown.
It could also cause
maybe_reuse_anon_vma()to incorrectly permit the reuse of ananon_vmawhich has active VMAs attached, which will lead to a persistently invalid state.The solution is to clear the VMA's
anon_vmafield when we clean up partial state, as the fact we are doing so indicates clearly that the VMA is not correctly integrated into theanon_vmatree and thus this field is invalid.Reported-by Sasha Levin <[email protected]>Reported-by Jiakai Xu <[email protected]>Closes https://lore.kernel.org/linux-mm/CAFb8wJvRhatRD-9DVmr5v5pixTMPEr3UKjYBJjCd09OfH55CKg@mail.gmail.com/ open hereFixes 542eda1a8329 ("mm/rmap: improve anon_vma_clone(), unlink_anon_vmas() comments, add asserts")Signed-off-by Lorenzo Stoakes (Oracle) <[email protected]>--- mm/rmap.c | 7 +++++++ 1 file changed, 7 insertions(+)mm/rmap.c +0
-0
mm/rmap.c static void cleanup_partial_anon_vmas(struct vm_area_struct *vma) · line 466
Quoted 10 lines
mm/rmap.c +7
-0@@ -457,6 +457,13 @@ static void cleanup_partial_anon_vmas(struct vm_area_struct *vma)list_del (&avc->same_vma);anon_vma_chain_free (avc); } + +/* +* The anon_vma assigned to this VMA is no longer valid, as we were not +* able to correctly clone AVC state. Avoid inconsistent anon_vma tree +* state by resetting. +*/ + vma->anon_vma =NULL ;
LGTM. I was wondering whether anon_vma_clone() should take care of setting up dst->anon_vma. It looks a bit odd in dup_anon_vma.
Signature · 3 lines
Cheers, David
Lorenzo Stoakes (Oracle)
maintainerQuoted 3 lines · David Hildenbrand (Arm)
Thanks!
Quoted 2 lines
LGTM. I was wondering whether
anon_vma_clone()should take care of setting updst->anon_vma. It looks a bit odd indup_anon_vma.
Well in the fork case we don't want to set vma->anon_vma, as we use vma->anon_vma to denote whether or not we reused another anon_vma. For split + remap, we already duplicated it via vm_area_dup() -> vm_area_init_from().
But... now we have enum vma_operation threaded through here, we can just do that there like:
/* dst is unfaulted, so inherit src's anon_vma. */ if (operation == VMA_OP_MERGE_UNFAULTED) dst->anon_vma = src->anon_vma;
?
Quoted 5 lines
--
Cheers,David
Cheers, Lorenzo
David Hildenbrand (Arm)
maintainerQuoted 15 lines · Lorenzo Stoakes (Oracle)
Quoted 3 lines · David Hildenbrand (Arm)
Thanks!
Quoted 2 lines
LGTM. I was wondering whether
anon_vma_clone()should take care of setting updst->anon_vma. It looks a bit odd indup_anon_vma.Well in the fork case we don't want to set
vma->anon_vma, as we usevma->anon_vmato denote whether or not we reused anotheranon_vma. For split + remap, we already duplicated it viavm_area_dup()->vm_area_init_from().
Quoted 7 lines
But... now we have enum
vma_operationthreaded through here, we can just do that there like:/* dst is unfaulted, so inherit src's anon_vma. */ if (operation == VMA_OP_MERGE_UNFAULTED) dst->anon_vma = src->anon_vma;
Exactly what I had in mind.
Signature · 3 lines
Cheers, David
Lorenzo Stoakes (Oracle)
maintainerQuoted 19 lines · David Hildenbrand (Arm)
Quoted 18 lines · Lorenzo Stoakes (Oracle)
Quoted 3 lines · David Hildenbrand (Arm)
Thanks!
Quoted 2 lines
LGTM. I was wondering whether
anon_vma_clone()should take care of setting updst->anon_vma. It looks a bit odd indup_anon_vma.Well in the fork case we don't want to set
vma->anon_vma, as we usevma->anon_vmato denote whether or not we reused anotheranon_vma. For split + remap, we already duplicated it viavm_area_dup()->vm_area_init_from().But... now we have enum
vma_operationthreaded through here, we can just do that there like:
Quoted 6 lines
Quoted 4 lines
/* dst is unfaulted, so inherit src's anon_vma. */ if (operation == VMA_OP_MERGE_UNFAULTED) dst->anon_vma = src->anon_vma;Exactly what I had in mind.
That's fair, will send a separate patch for that!
Quoted 5 lines
--
Cheers,David
Cheers, Lorenzo
Quoted 33 lines · Lorenzo Stoakes (Oracle)
Commit
542eda1a8329("mm/rmap: improveanon_vma_clone(),unlink_anon_vmas()comments, add asserts") alters the way errors are handled, but overlooked one important aspect of clean up.When a VMA encounters an error state in
anon_vma_clone()(that is, on attempted allocation ofanon_vma_chainobjects), it cleans up partially established state incleanup_partial_anon_vmas(), before returning an error.However, this occurs prior to
anon_vma->num_active_vmasbeing incremented, and it also fails to clear the VMA'svma->anon_vmafield, which remains in place.This is immediately an inconsistent state, because
anon_vma->num_active_vmasis supposed to track the number of VMAs whosevma->anon_vmafield references thatanon_vma, and now that count is off-by-negative-1 for each VMA for which this error state has occurred.When VMAs are unlinked from this
anon_vma,unlink_anon_vmas()will eventually underflowanon_vma->num_active_vmas, which will trigger a warning.This will always eventually happen, as we unlink anon_vma's at process teardown.
It could also cause
maybe_reuse_anon_vma()to incorrectly permit the reuse of ananon_vmawhich has active VMAs attached, which will lead to a persistently invalid state.The solution is to clear the VMA's
anon_vmafield when we clean up partial state, as the fact we are doing so indicates clearly that the VMA is not correctly integrated into theanon_vmatree and thus this field is invalid.
Quoted 6 lines
Reported-by Sasha Levin <[email protected]>Reported-by Jiakai Xu <[email protected]>Closes https://lore.kernel.org/linux-mm/CAFb8wJvRhatRD-9DVmr5v5pixTMPEr3UKjYBJjCd09OfH55CKg@mail.gmail.com/ open hereFixes 542eda1a8329 ("mm/rmap: improve anon_vma_clone(), unlink_anon_vmas() comments, add asserts")Signed-off-by Lorenzo Stoakes (Oracle) <[email protected]>
Thanks!
Quoted 24 lines
--- mm/rmap.c | 7 +++++++ 1 file changed, 7 insertions(+)mm/rmap.c +7
-1@@ -457,6 +457,13 @@ static void cleanup_partial_anon_vmas(struct vm_area_struct *vma)list_del (&avc->same_vma);anon_vma_chain_free (avc); } + +/* +* The anon_vma assigned to this VMA is no longer valid, as we were not +* able to correctly clone AVC state. Avoid inconsistent anon_vma tree +* state by resetting. +*/ + vma->anon_vma =NULL ; }/** -- 2.53.0
Quoted 33 lines
Commit
542eda1a8329("mm/rmap: improveanon_vma_clone(),unlink_anon_vmas()comments, add asserts") alters the way errors are handled, but overlooked one important aspect of clean up.When a VMA encounters an error state in
anon_vma_clone()(that is, on attempted allocation ofanon_vma_chainobjects), it cleans up partially established state incleanup_partial_anon_vmas(), before returning an error.However, this occurs prior to
anon_vma->num_active_vmasbeing incremented, and it also fails to clear the VMA'svma->anon_vmafield, which remains in place.This is immediately an inconsistent state, because
anon_vma->num_active_vmasis supposed to track the number of VMAs whosevma->anon_vmafield references thatanon_vma, and now that count is off-by-negative-1 for each VMA for which this error state has occurred.When VMAs are unlinked from this
anon_vma,unlink_anon_vmas()will eventually underflowanon_vma->num_active_vmas, which will trigger a warning.This will always eventually happen, as we unlink anon_vma's at process teardown.
It could also cause
maybe_reuse_anon_vma()to incorrectly permit the reuse of ananon_vmawhich has active VMAs attached, which will lead to a persistently invalid state.The solution is to clear the VMA's
anon_vmafield when we clean up partial state, as the fact we are doing so indicates clearly that the VMA is not correctly integrated into theanon_vmatree and thus this field is invalid.
Quoted 6 lines
Reported-by Sasha Levin <[email protected]>Reported-by Jiakai Xu <[email protected]>Closes https://lore.kernel.org/linux-mm/CAFb8wJvRhatRD-9DVmr5v5pixTMPEr3UKjYBJjCd09OfH55CKg@mail.gmail.com/ open hereFixes 542eda1a8329 ("mm/rmap: improve anon_vma_clone(), unlink_anon_vmas() comments, add asserts")Signed-off-by Lorenzo Stoakes (Oracle) <[email protected]>
Thanks!
Quoted 24 lines
--- mm/rmap.c | 7 +++++++ 1 file changed, 7 insertions(+)mm/rmap.c +7
-1@@ -457,6 +457,13 @@ static void cleanup_partial_anon_vmas(struct vm_area_struct *vma)list_del (&avc->same_vma);anon_vma_chain_free (avc); } + +/* +* The anon_vma assigned to this VMA is no longer valid, as we were not +* able to correctly clone AVC state. Avoid inconsistent anon_vma tree +* state by resetting. +*/ + vma->anon_vma =NULL ; }/** -- 2.53.0
Quoted 33 lines · Lorenzo Stoakes (Oracle)
Commit
542eda1a8329("mm/rmap: improveanon_vma_clone(),unlink_anon_vmas()comments, add asserts") alters the way errors are handled, but overlooked one important aspect of clean up.When a VMA encounters an error state in
anon_vma_clone()(that is, on attempted allocation ofanon_vma_chainobjects), it cleans up partially established state incleanup_partial_anon_vmas(), before returning an error.However, this occurs prior to
anon_vma->num_active_vmasbeing incremented, and it also fails to clear the VMA'svma->anon_vmafield, which remains in place.This is immediately an inconsistent state, because
anon_vma->num_active_vmasis supposed to track the number of VMAs whosevma->anon_vmafield references thatanon_vma, and now that count is off-by-negative-1 for each VMA for which this error state has occurred.When VMAs are unlinked from this
anon_vma,unlink_anon_vmas()will eventually underflowanon_vma->num_active_vmas, which will trigger a warning.This will always eventually happen, as we unlink anon_vma's at process teardown.
It could also cause
maybe_reuse_anon_vma()to incorrectly permit the reuse of ananon_vmawhich has active VMAs attached, which will lead to a persistently invalid state.The solution is to clear the VMA's
anon_vmafield when we clean up partial state, as the fact we are doing so indicates clearly that the VMA is not correctly integrated into theanon_vmatree and thus this field is invalid.
Quoted 7 lines
Reported-by Sasha Levin <[email protected]>Reported-by Jiakai Xu <[email protected]>Closes https://lore.kernel.org/linux-mm/CAFb8wJvRhatRD-9DVmr5v5pixTMPEr3UKjYBJjCd09OfH55CKg@mail.gmail.com/ open hereFixes 542eda1a8329 ("mm/rmap: improve anon_vma_clone(), unlink_anon_vmas() comments, add asserts")Signed-off-by Lorenzo Stoakes (Oracle) <[email protected]>---
Signature · 2 lines
Cheers, Harry / Hyeonggon