mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Rik van Riel <riel@surriel.com>
To: "David Hildenbrand (Arm)" <david@kernel.org>,
	 linux-kernel@vger.kernel.org
Cc: linux-mm@kvack.org, Andrew Morton <akpm@linux-foundation.org>,
	Suren Baghdasaryan <surenb@google.com>,
	Lorenzo Stoakes <ljs@kernel.org>,
	Christoph Hellwig <hch@infradead.org>,
	 Yuan-Hao Hsu <aa9736195201@gmail.com>,
	John Hubbard <jhubbard@nvidia.com>,
	Jason Gunthorpe <jgg@ziepe.ca>,  Peter Xu <peterx@redhat.com>
Subject: Re: [PATCH 9/9] mm/gup: unshare-check every page of a pte batch
Date: Thu, 26 Nov 2026 12:37:57 -0500	[thread overview]
Message-ID: <577d25b6aa80bcfc216eda5bb4311ffe796be49e.camel@surriel.com> (raw)
In-Reply-To: <05de9fe1-9a3f-432d-9ce6-9f497852d1d6@kernel.org>

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.

      reply	other threads:[~2026-11-26 17:38 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-23 19:56 [PATCH v6 0/9] mm/gup: walk and batch whole PTE tables in slow GUP Rik van Riel
2026-11-23 19:56 ` [PATCH 1/9] mm/gup: break out gup_fill_pages() helper Rik van Riel
2026-11-26 13:40   ` David Hildenbrand (Arm)
2026-11-23 19:56 ` [PATCH 2/9] mm/gup: break out gup_flush_pages() helper Rik van Riel
2026-11-26 13:41   ` David Hildenbrand (Arm)
2026-11-26 17:41     ` Rik van Riel
2026-11-23 19:56 ` [PATCH 3/9] mm/gup: convert follow_page_mask() to return a long Rik van Riel
2026-11-26 14:04   ` David Hildenbrand (Arm)
2026-11-23 19:56 ` [PATCH 4/9] mm/gup: split out folio grab-and-store helper Rik van Riel
2026-11-23 19:56 ` [PATCH 5/9] mm/gup: break out gup_lookup_pte() helper Rik van Riel
2026-11-23 19:56 ` [PATCH 6/9] mm/gup: return a huge page's full count from follow_page_mask() Rik van Riel
2026-11-23 19:56 ` [PATCH 7/9] mm/gup: walk multiple PTEs per follow_page_pte() call Rik van Riel
2026-11-23 19:56 ` [PATCH 8/9] mm/gup: batch contiguous same-folio PTEs into one refcount grab Rik van Riel
2026-11-23 19:56 ` [PATCH 9/9] mm/gup: unshare-check every page of a pte batch Rik van Riel
2026-11-26 13:50   ` David Hildenbrand (Arm)
2026-11-26 17:37     ` Rik van Riel [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=577d25b6aa80bcfc216eda5bb4311ffe796be49e.camel@surriel.com \
    --to=riel@surriel.com \
    --cc=aa9736195201@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=david@kernel.org \
    --cc=hch@infradead.org \
    --cc=jgg@ziepe.ca \
    --cc=jhubbard@nvidia.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=ljs@kernel.org \
    --cc=peterx@redhat.com \
    --cc=surenb@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®