Skip to content

Commit 3a206a8

Browse files
Lorenzo Stoakes (Oracle)akpm00
authored andcommitted
mm/rmap: clear vma->anon_vma on error
Commit 542eda1 ("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. Link: https://lkml.kernel.org/r/20260318122632.63404-1-ljs@kernel.org Fixes: 542eda1 ("mm/rmap: improve anon_vma_clone(), unlink_anon_vmas() comments, add asserts") Signed-off-by: Lorenzo Stoakes (Oracle) <ljs@kernel.org> Reported-by: Sasha Levin <sashal@kernel.org> Closes: https://lore.kernel.org/linux-mm/20260302151547.2389070-1-sashal@kernel.org/ Reported-by: Jiakai Xu <jiakaipeanut@gmail.com> Closes: https://lore.kernel.org/linux-mm/CAFb8wJvRhatRD-9DVmr5v5pixTMPEr3UKjYBJjCd09OfH55CKg@mail.gmail.com/ Acked-by: David Hildenbrand (Arm) <david@kernel.org> Acked-by: Vlastimil Babka (SUSE) <vbabka@kernel.org> Tested-by: Jiakai Xu <jiakaipeanut@gmail.com> Acked-by: Harry Yoo <harry.yoo@oracle.com> Cc: Jann Horn <jannh@google.com> Cc: Liam Howlett <liam.howlett@oracle.com> Cc: Rik van Riel <riel@surriel.com> Cc: Sasha Levin (Microsoft) <sashal@kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
1 parent f0caa1d commit 3a206a8

1 file changed

Lines changed: 7 additions & 0 deletions

File tree

mm/rmap.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,13 @@ static void cleanup_partial_anon_vmas(struct vm_area_struct *vma)
457457
list_del(&avc->same_vma);
458458
anon_vma_chain_free(avc);
459459
}
460+
461+
/*
462+
* The anon_vma assigned to this VMA is no longer valid, as we were not
463+
* able to correctly clone AVC state. Avoid inconsistent anon_vma tree
464+
* state by resetting.
465+
*/
466+
vma->anon_vma = NULL;
460467
}
461468

462469
/**

0 commit comments

Comments
 (0)