Re: [PATCH v2] mm: implement and use vma_anon_tracked(), silence KCSAN
From: David Hildenbrand (Arm)
Date: Thu Sep 10 2026 - 10:10:53 EST
On 9/10/26 15:18, Lorenzo Stoakes (ARM) wrote:
> Provide a function to abstract the common task of checking whether
> a VMA is faulted in or not.
>
> A VMA or mmap lock must be held when calling this function. For an attached
> VMA the transitions between unfaulted/faulted state are:
>
> Transition | VMA/mmap Lock state
> ------------------------|-----------------------------------------------
> unfaulted to faulted | write lock OR read lock + mm->page_table_lock
> faulted to unfaulted | write lock
>
Still using the faulted/unfaulted terminology above is subotimal but I don't
mind too much as long as the code is easy to grasp :)
> So vma_anon_tracked() never provides a false positive (the lock precludes
> it), but if only a read lock is held, a negative result must be re-checked
> with mm->page_table_lock held.
>
> Detached VMAs cannot be concurrently manipulated as they are removed from
> the maple tree so require no guarantees.
>
> Use data_race() to silence KCSAN about non-existent data races between
> concurrent vma->anon_vma read/write on optimistic fault tests.
>
> Also while here, const-ify vma_is_attached(), vma_assert_stabilised() and
> dependants.
>
> Finally, update the core VMA merge/split, rmap, mremap, KSM and fault
> preparation callers which test vma->anon_vma directly to use
> vma_anon_tracked() instead.
>
> Note that the lockless read in reusable_anon_vma() is doing more than
> checking whether the VMA is faulted - it is returning the anon_vma to be
> used on fault, so this check is not altered.
>
> There is one odd one out - file_backed_vma_is_retractable() - which holds
> neither a VMA nor mmap lock and is stabilised by the file rmap lock only.
>
> Therefore just add a comment to explain why the direct vma->anon_vma check
> is required.
>
> Reported-by: Guilherme Giacomo Simoes <trintaeoitogc@xxxxxxxxx>
> Closes: https://lore.kernel.org/all/20260829100034.423064-1-trintaeoitogc@xxxxxxxxx/
> Closes: https://lore.kernel.org/all/20260909115723.528501-1-trintaeoitogc@xxxxxxxxx/
> Signed-off-by: Lorenzo Stoakes (ARM) <ljs@xxxxxxxxxx>
> ---
> v2:
> - Renamed vma_is_faulted() to vma_anon_tracked() as per David.
>
> v1:
> https://lore.kernel.org/r/20260909-vma-is-faulted-v1-1-3a701f48984a@xxxxxxxxxx
> ---
> index dd66c6ad5af1..40669bec953c 100644
> --- a/mm/huge_memory.c
> +++ b/mm/huge_memory.c
> @@ -264,7 +264,7 @@ unsigned long __thp_vma_allowable_orders(struct vm_area_struct *vma,
> * Allow page fault since anon_vma may be not initialized until
> * the first page fault.
Do we directly want to fix up such comments to not talk about "anon_vma" but
instead of the abstraction?
Not immediately sure how to rephrase, but I guess the goal would be to avoid the
term "anon_vma" and least outside of core-rmap/anon_vma code.
[...]
> /* KSM folios don't reach here because of the !anon_vma check */
> diff --git a/mm/swapfile.c b/mm/swapfile.c
> index 01e7b6b046b6..9f9cd6657360 100644
> --- a/mm/swapfile.c
> +++ b/mm/swapfile.c
> @@ -2705,7 +2705,7 @@ static int unuse_mm(struct mm_struct *mm, unsigned int type)
> if (check_stable_address_space(mm))
> goto unlock;
> for_each_vma(vmi, vma) {
> - if (vma->anon_vma && !is_vm_hugetlb_page(vma)) {
> + if (vma_anon_tracked(vma) && !is_vm_hugetlb_page(vma)) {
> ret = unuse_vma(vma, type);
> if (ret)
> break;
[...]
> +/**
> + * vma_anon_tracked() - is @vma tracked by an anon_vma?
> + * @vma: The VMA to be checked.
> + *
> + * A VMA or mmap lock must be held.
I would document here what the implication is: if untracked, there cannot be any
anon folios, if tracked, there might. Some callers of this function (e.g., ksm,
swapfile) want to know exactly that: could there be any anon folio in there such
that they have to provess the VMA.
--
Cheers,
David