mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] mm/gup: unshare-check every page of a pte batch
@ 2026-11-19 15:57 Rik van Riel
  2026-11-19 16:16 ` David Hildenbrand (Arm)
  0 siblings, 1 reply; 4+ messages in thread
From: Rik van Riel @ 2026-11-19 15:57 UTC (permalink / raw)
  To: David Hildenbrand
  Cc: Yeoreum Yun, Jason Gunthorpe, John Hubbard, Peter Xu,
	Andrew Morton, linux-mm, linux-kernel, Lorenzo Stoakes,
	Suren Baghdasaryan, Peter Xu, Liam R. Howlett, Mike Rapoport,
	Michal Hocko

A 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 tail page
                                          fault copies it elsewhere
    read pinned page -> stale

For every FOLL_WRITE call the helper returns the whole run on
the head page's credentials, never reaching the per-page loop below.
After fork, a FOLL_FORCE write unsharing only the head leaves
the tail pages read-only and non-exclusive behind identical
PTEs, which batching (PTE bits only, never the exclusivity flag)
covers at once.

A page pinned for write must be exclusive to the pinning process,
and one vetted PTE proves that for one page only. FOLL_GET and
single-page FOLL_WRITE keep the shortcut; multi-page callers run
the per-page check. Writable batches and GUP-fast are unchanged.

Without this fix, the pin holder sees stale page content,
with it the pin and program address map the same page.

Signed-off-by: Rik van Riel <riel@surriel.com>
---
This fix applies on gup-7.5-ptebatch in mm/core.git

 mm/gup.c | 20 +++++++++++++++-----
 1 file changed, 15 insertions(+), 5 deletions(-)

diff --git a/mm/gup.c b/mm/gup.c
index b0a0019de80a..6b6e4c6f8144 100644
--- a/mm/gup.c
+++ b/mm/gup.c
@@ -639,15 +639,25 @@ static unsigned long gup_pages_not_requiring_unshare(
 {
 	unsigned int i;
 
+	/* A short term reference does not need the pages to be exclusive. */
+	if (!(flags & FOLL_PIN))
+		return nr_pages;
+
 	/*
-	 * FOLL_WRITE is implicitly handled correctly as the page table entry
-	 * has to be writable -- and if it references (part of) an anonymous
-	 * folio, that part is required to be marked exclusive.
+	 * FOLL_WRITE for a single page is checked by the caller, which
+	 * checked that the page table entry is writable, or FOLL_FORCE
+	 * on an exclusive anonymous page; if it references (part of) an
+	 * anonymous folio, that is exclusive.
 	 */
-	if ((flags & (FOLL_WRITE | FOLL_PIN)) != FOLL_PIN)
+	if ((flags & FOLL_WRITE) && nr_pages == 1)
 		return nr_pages;
 
-	/* folio_test_anon(folio) is stable until folio gets freed. */
+	/*
+	 * Check each page for suitability below. The caller may have
+	 * only checked the page tables.
+	 *
+	 * folio_test_anon(folio) is stable until folio gets freed.
+	 */
 	if (!folio_test_anon(folio)) {
 		/*
 		 * We only care about R/O long-term pining: R/O short-term
-- 
2.56.0


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] mm/gup: unshare-check every page of a pte batch
  2026-11-19 15:57 [PATCH] mm/gup: unshare-check every page of a pte batch Rik van Riel
@ 2026-11-19 16:16 ` David Hildenbrand (Arm)
  2026-11-19 16:20   ` David Hildenbrand (Arm)
  0 siblings, 1 reply; 4+ messages in thread
From: David Hildenbrand (Arm) @ 2026-11-19 16:16 UTC (permalink / raw)
  To: Rik van Riel
  Cc: Yeoreum Yun, Jason Gunthorpe, John Hubbard, Peter Xu,
	Andrew Morton, linux-mm, linux-kernel, Lorenzo Stoakes,
	Suren Baghdasaryan, Liam R. Howlett, Mike Rapoport, Michal Hocko

On 11/19/26 16:57, Rik van Riel wrote:
> A 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

All PTEs must be writable or all pages must be PageAnonExclusive

>                                         write to a tail page
>                                           fault copies it elsewhere

Which copies only if !pte_write() && !PageAnonExclusive

>     read pinned page -> stale
> 
> For every FOLL_WRITE call the helper returns the whole run on
> the head page's credentials, never reaching the per-page loop below.
> After fork, a FOLL_FORCE write unsharing only the head leaves
> the tail pages read-only and non-exclusive behind identical
> PTEs, which batching (PTE bits only, never the exclusivity flag)
> covers at once.
> 
> A page pinned for write must be exclusive to the pinning process,
> and one vetted PTE proves that for one page only. FOLL_GET and
> single-page FOLL_WRITE keep the shortcut; multi-page callers run
> the per-page check. Writable batches and GUP-fast are unchanged.
> 
> Without this fix, the pin holder sees stale page content,
> with it the pin and program address map the same page.
> 
> Signed-off-by: Rik van Riel <riel@surriel.com>
> ---
> This fix applies on gup-7.5-ptebatch in mm/core.git

I was confused when I read the above.

I fed this to Claude and it told me

1) The path it describes can't happen.

2) The patch fixes nothing.

3) The changelog contradicts itself.

But you said that you had a reproducer. So now I am even more confused. :)

Can you share the reproducer and against which branch it ran?


-- 
Cheers,

David

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] mm/gup: unshare-check every page of a pte batch
  2026-11-19 16:16 ` David Hildenbrand (Arm)
@ 2026-11-19 16:20   ` David Hildenbrand (Arm)
  2026-11-19 18:37     ` Rik van Riel
  0 siblings, 1 reply; 4+ messages in thread
From: David Hildenbrand (Arm) @ 2026-11-19 16:20 UTC (permalink / raw)
  To: Rik van Riel
  Cc: Yeoreum Yun, Jason Gunthorpe, John Hubbard, Peter Xu,
	Andrew Morton, linux-mm, linux-kernel, Lorenzo Stoakes,
	Suren Baghdasaryan, Liam R. Howlett, Mike Rapoport, Michal Hocko

On 11/19/26 17:16, David Hildenbrand (Arm) wrote:
> On 11/19/26 16:57, Rik van Riel wrote:
>> A 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
> 
> All PTEs must be writable or all pages must be PageAnonExclusive
> 
>>                                         write to a tail page
>>                                           fault copies it elsewhere
> 
> Which copies only if !pte_write() && !PageAnonExclusive
> 
>>     read pinned page -> stale
>>
>> For every FOLL_WRITE call the helper returns the whole run on
>> the head page's credentials, never reaching the per-page loop below.
>> After fork, a FOLL_FORCE write unsharing only the head leaves
>> the tail pages read-only and non-exclusive behind identical
>> PTEs, which batching (PTE bits only, never the exclusivity flag)
>> covers at once.
>>
>> A page pinned for write must be exclusive to the pinning process,
>> and one vetted PTE proves that for one page only. FOLL_GET and
>> single-page FOLL_WRITE keep the shortcut; multi-page callers run
>> the per-page check. Writable batches and GUP-fast are unchanged.
>>
>> Without this fix, the pin holder sees stale page content,
>> with it the pin and program address map the same page.
>>
>> Signed-off-by: Rik van Riel <riel@surriel.com>
>> ---
>> This fix applies on gup-7.5-ptebatch in mm/core.git
> 
> I was confused when I read the above.
> 
> I fed this to Claude and it told me
> 
> 1) The path it describes can't happen.
> 
> 2) The patch fixes nothing.
> 
> 3) The changelog contradicts itself.

It argued that because "Writable batches and GUP-fast are unchanged.", which
also caused the biggest confusion on my end.

So this smells like a bug in your new GUP user instead? Or some incompatibility
with the helper when used in different context (that would surprise me).

-- 
Cheers,

David

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] mm/gup: unshare-check every page of a pte batch
  2026-11-19 16:20   ` David Hildenbrand (Arm)
@ 2026-11-19 18:37     ` Rik van Riel
  0 siblings, 0 replies; 4+ messages in thread
From: Rik van Riel @ 2026-11-19 18:37 UTC (permalink / raw)
  To: David Hildenbrand (Arm)
  Cc: Yeoreum Yun, Jason Gunthorpe, John Hubbard, Peter Xu,
	Andrew Morton, linux-mm, linux-kernel, Lorenzo Stoakes,
	Suren Baghdasaryan, Liam R. Howlett, Mike Rapoport, Michal Hocko

On Thu, 2026-11-19 at 17:20 +0100, David Hildenbrand (Arm) wrote:
> 
> So this smells like a bug in your new GUP user instead? Or some
> incompatibility
> with the helper when used in different context (that would surprise
> me).

It turns out it was a bit of both. The GUP slow
path was using this helper wrong.

I'll send v6 of the GUP slow series soon(ish).

-- 
All Rights Reversed.

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-11-19 18:37 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-19 15:57 [PATCH] mm/gup: unshare-check every page of a pte batch Rik van Riel
2026-11-19 16:16 ` David Hildenbrand (Arm)
2026-11-19 16:20   ` David Hildenbrand (Arm)
2026-11-19 18:37     ` Rik van Riel

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®