Re: [PATCH] mm/userfaultfd: check exclusivity of the page being moved

From: David Hildenbrand (Arm)

Date: Tue Nov 17 2026 - 16:15:19 EST


On 11/17/26 20:18, nakixii wrote:
> move_pages_ptes() gets the folio of a present source PTE and then checks
> PageAnonExclusive() on the first page of that folio. PG_anon_exclusive
> is per page, so for a PTE-mapped large folio this can give the wrong
> answer for the page that is actually being moved.
>
> This happens after fork(). fork() clears the flag on every page of the
> folio. Once the child exits, a write fault in the parent can reuse the
> large folio, and do_wp_page() sets the flag again only on the faulting
> page. Reclaim can leave the same state behind when try_to_unmap()
> replaces the earlier pages with swap entries and then fails on a later
> one.
>
> Moving that page with UFFDIO_MOVE then fails with -EBUSY, even though
> the folio could be split and the page moved. Callers that fall back to
> UFFDIO_COPY on -EBUSY end up copying instead.

Ah, I was about to say, nothing bad will happen because we reject to move large
folios.

So this is pretty harmless and pretty uncommon to hit.

> @@ -1570,8 +1571,14 @@ static long move_pages_ptes(struct mm_struct *mm, pmd_t *dst_pmd, pmd_t *src_pmd
> goto out;
> }
>
> - folio = vm_normal_folio(src_vma, src_addr, orig_src_pte);
> - if (!folio || !PageAnonExclusive(&folio->page)) {
> + /*
> + * PG_anon_exclusive is per page. The first page of a
> + * large folio can be shared while the page mapped here
> + * is still exclusive, so check the page being moved.
> + */
> + page = vm_normal_page(src_vma, src_addr, orig_src_pte);
> + folio = page ? page_folio(page) : NULL;
> + if (!folio || !PageAnonExclusive(page)) {

Yeah, we could have also moved it after the split and test for an early
folio_likely_mapped_shared() first, but doesn't really matter.

--
Cheers,

David