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

From: Mike Rapoport

Date: Tue Nov 17 2026 - 14:52:38 EST


On Wed, Nov 18, 2026 at 03:18:53AM +0800, 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.

COW reuse of PTE-mapped large folios only appeared with commit
1da190f4d0a6 ("mm: Copy-on-Write (COW) reuse support for PTE-mapped
THP"), so this scenario didn't exist when UFFDIO_MOVE was added.

> 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.

This one, and fork() with a single page of a PTE-mapped large folio
pinned, could produce a mix of exclusive and shared pages in a folio
before that, so the Fixes tag is right, but please make it clear in the
changelog that the fork() + reuse case is a later addition.

> 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.

It also works the other way around: if the first page is exclusive and
the page being moved is not, we needlessly split the folio before the
retry notices the page is shared and returns -EBUSY.

Worth mentioning that as well.

...

> Signed-off-by: nakixii <nakixii@xxxxxxxxx>

Please use a known identity in Signed-off-by rather than a nickname, see
"Developer's Certificate of Origin" in
Documentation/process/submitting-patches.rst.

...

> - 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)) {

This would be simpler as

page = vm_normal_page(src_vma, src_addr, orig_src_pte);
if (!page || !PageAnonExclusive(page)) {
spin_unlock(src_ptl);
ret = -EBUSY;
goto out;
}

folio = page_folio(page);

> spin_unlock(src_ptl);
> ret = -EBUSY;
> goto out;

With that addressed

Acked-by: Mike Rapoport (Microsoft) <rppt@xxxxxxxxxx>

--
Sincerely yours,
Mike.