Skip to contentSkip to conversation

mm/gup: unshare-check every page of a pte batch

PATCH In review Rik van Riel 3 participants 5 messages Nov 19 – Nov 20

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mm/gup: unshare-check every page of a pte batch

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.

---
This fix applies on gup-7.5-ptebatch in mm/core.git

 mm/gup.c | 20 +++++++++++++++-----
 1 file changed, 15 insertions(+), 5 deletions(-)
mm/gup.c +15 -5
index b0a0019de80a..6b6e4c6f8144 100644
@@ -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
Signature · 1 line
2.56.0
#1 lore raw
Quoted 7 lines · Rik van Riel

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

Quoted 2 lines

write to a tail page
fault copies it elsewhere

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

Quoted 14 lines
    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.

Quoted 6 lines

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

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

Signature · 3 lines
Cheers,

David
#2 lore raw
Quoted 39 lines · David Hildenbrand (Arm)
Quoted 7 lines · Rik van Riel

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

Quoted 2 lines

write to a tail page
fault copies it elsewhere

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

Quoted 14 lines
    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.

Quoted 6 lines

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

---
This fix applies on gup-7.5-ptebatch in mm/core.git

I was confused when I read the above.

Quoted 7 lines

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

Signature · 3 lines
Cheers,

David
#3 lore raw
Quoted 5 lines · David Hildenbrand (Arm)

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

Signature · 1 line
All Rights Reversed.
#4 lore raw
In reply to Rik van Riel
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] mm/gup: unreachable and broken FOLL_WRITE batch check in gup_pages_not_requiring_unshare() --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] mm/gup: unreachable and broken FOLL_WRITE batch check in gup_pages_not_requiring_unshare()
--

commit 69eb6845bb430fc095f06e523cf6406cda8b81b9
Author: Rik van Riel <[email protected]>

mm/gup: unshare-check every page of a pte batch

This commit changes gup_pages_not_requiring_unshare() to only take the early FOLL_WRITE shortcut when nr_pages == 1, so that multi-page FOLL_WRITE calls fall through to the per-page PageAnonExclusive() check.

Quoted 14 lines
mm/gup.c +4 -1
index ec0bc99202169..57ad488b4538d 100644
@@ -649,15 +649,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

mm/gup.c static unsigned long gup_pages_not_requiring_unshare( · line 663

Quoted 10 lines
mm/gup.c +5 -3
-	 * 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;

[Severity: Medium]
Can gup_pages_not_requiring_unshare() ever be reached with FOLL_WRITE and nr_pages > 1, and is it the right place to validate FOLL_WRITE | FOLL_FORCE PTE batches?

There are only two callers in the tree: gup_must_unshare(), which passes nr_pages == 1, and gup_fast_pte_range(), which only calls gup_pages_not_requiring_unshare() when !pte_write(pte) after pte_access_permitted(pte, flags & FOLL_WRITE) has already rejected read-only PTEs for FOLL_WRITE:

mm/gup.c:gup_fast_pte_range() {
	...
		if (!pte_access_permitted(pte, flags & FOLL_WRITE))
			goto out;
	...
		if (!pte_write(pte)) {
			nr_batch = gup_pages_not_requiring_unshare(NULL, flags, folio, page, nr_batch);
	...
}

If a future slow-GUP caller does pass FOLL_WRITE | FOLL_FORCE with nr_pages > 1 on a read-only PTE batch, checking it here has three issues:

1. Returning !(flags & FOLL_PIN) first skips the per-page PageAnonExclusive() check for FOLL_GET | FOLL_WRITE | FOLL_FORCE (such as ptrace or /proc/pid/mem writes), allowing writes to non-exclusive tail pages without breaking COW, whereas can_follow_write_common() requires PageAnon(page) && PageAnonExclusive(page) for all FOLL_WRITE | FOLL_FORCE accesses on read-only PTEs.

2. When FOLL_PIN | FOLL_WRITE with nr_pages > 1 falls through into the !folio_test_anon(folio) branch below, that branch assumes a read-only pin and returns nr_pages whenever !(flags & FOLL_LONGTERM) or !vma_is_cow_mapping(vma), whereas can_follow_write_common() rejects non-anonymous pages on read-only PTEs.

3. gup_pages_not_requiring_unshare() drives FAULT_FLAG_UNSHARE (-EMLINK), which is only valid for read-only pins; triggering an unshare fault when FOLL_WRITE is set hits VM_WARN_ON_ONCE(fault_flags & FAULT_FLAG_WRITE) in faultin_page() and WARN_ON_ONCE(*flags & FAULT_FLAG_WRITE) (returning VM_FAULT_SIGSEGV) in sanitize_fault_flags(), whereas FOLL_WRITE | FOLL_FORCE on a non-exclusive page needs a regular FAULT_FLAG_WRITE fault via can_follow_write_pte().

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/20261119105701.3f57ac54@fangorn?part=1