Re: [PATCH v2 04/40] mm/rmap: introduce and use hugetlb_try_dup_anon_rmap()

From: Matthew Wilcox
Date: Wed Dec 20 2023 - 23:40:35 EST


On Wed, Dec 20, 2023 at 11:44:28PM +0100, David Hildenbrand wrote:
> hugetlb rmap handling differs quite a lot from "ordinary" rmap code.
> For example, hugetlb currently only supports entire mappings, and treats
> any mapping as mapped using a single "logical PTE". Let's move it out
> of the way so we can overhaul our "ordinary" rmap.
> implementation/interface.
>
> So let's introduce and use hugetlb_try_dup_anon_rmap() to make all
> hugetlb handling use dedicated hugetlb_* rmap functions.
>
> Add sanity checks that we end up with the right folios in the right
> functions.
>
> Note that is_device_private_page() does not apply to hugetlb.
>
> Reviewed-by: Yin Fengwei <fengwei.yin@xxxxxxxxx>
> Reviewed-by: Ryan Roberts <ryan.roberts@xxxxxxx>
> Signed-off-by: David Hildenbrand <david@xxxxxxxxxx>

Reviewed-by: Matthew Wilcox (Oracle) <willy@xxxxxxxxxxxxx>

> +static inline bool folio_needs_cow_for_dma(struct vm_area_struct *vma,
> + struct folio *folio)

I particularly like it that you introduced this.

> +static inline int hugetlb_try_dup_anon_rmap(struct folio *folio,
> + struct vm_area_struct *vma)
> +{
> + VM_WARN_ON_FOLIO(!folio_test_hugetlb(folio), folio);
> + VM_WARN_ON_FOLIO(!folio_test_anon(folio), folio);
> +
> + if (PageAnonExclusive(&folio->page)) {

I wonder if we need a folio_test_hugetlb_anon_exclusive() to make this
a little more ergonomic?

> + if (unlikely(folio_needs_cow_for_dma(vma, folio)))
> + return -EBUSY;
> + ClearPageAnonExclusive(&folio->page);

... and set/clear variants.