Re: [PATCH 9/9] mm/gup: unshare-check every page of a pte batch

From: Rik van Riel

Date: Thu Nov 26 2026 - 12:38:26 EST


On Thu, 2026-11-26 at 14:50 +0100, David Hildenbrand (Arm) wrote:
> On 11/23/26 20:56, Rik van Riel wrote:
> > A slow GUP write pin over a PTE batch can hand back pages that a
> > later
> > write fault replaces under the pin holder: the process writes to
> > the copy, the holder keeps the old page:
> >
> >     pin holder                          process (mm owner)
> >     pin all 16 for write
> >       batch passes unchecked
> >                                         write to a page
> >                                           fault copies it elsewhere
> >     read pinned page -> stale
> >
> > With GUP-fast, FOLL_WRITE | FOLL_PIN can never reach
> > gup_pages_not_requiring_unshare, but slow GUP can reach it
> > with a read-only head PTE.
>
> I assume you mean FOLL_FORCE?
>
Indeede, it is FOLL_FORCE. I need to rewrite that.

> I don't quite understand the problem you are trying to solve reading
> this.
>
> So can you walk me through the exact sequence of events + flags that
> are being
> passed?

The sequence seems somewhat contrived, but it
looks like it can happen, and some runtimes
(maybe Java?) might actually do stuff like this.

1) mmap 64K private anon, touch it (one mTHP folio, 16 pages).

2) fork, child exits/execs

3) Parent holds 16 pages, all PTEs read-only

4) Parent writes first page, COW makes it exclusive. 
This is now the only PageAnonExclusive page in the mTHP,
while the other pages in the folio are not.

5) mprotect the range read-only. Now all PTEs are read-only,
but the head is exclusive, while the 15 tails not.

6) Slow GUP pin_user_pages(FOLL_FORCE|FOLL_WRITE), which
pins all 16 pages without COW faults, since all the
PTEs have the same permissions, they all map the
same folio, and the first page is PageAnonExclusive.

7) mprotect the range read-write again, this changes the
first PTE (on an exclusive page) back to read-write,
but GUP already did its thing

8) A write by the program to page 5 now causes a
COW fault, since that page is not PageAnonExclusive,
and the refcount is raised

9) A write to page 5 by the GUP buffer holder
goes to the old page, and becomes invisible
to the program

The fix is to simply do the same checks for
FOLL_WRITE that we already have in place for
FOLL_PIN.

Want me to put the above explanation in the
changelog, or tweak it some more?

--
All Rights Reversed.