All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Rik van Riel" <riel@surriel.com>,
	"David Hildenbrand" <david@kernel.org>
Cc: "Yeoreum Yun" <yeoreum.yun@arm.com>,
	"Jason Gunthorpe" <jgg@ziepe.ca>,
	"John Hubbard" <jhubbard@nvidia.com>,
	"Peter Xu" <peterx@redhat.com>,
	"Andrew Morton" <akpm@linux-foundation.org>,
	linux-mm@kvack.org, "Lorenzo Stoakes" <ljs@kernel.org>,
	"Suren Baghdasaryan" <surenb@google.com>,
	"Liam R. Howlett" <liam@infradead.org>,
	"Mike Rapoport" <rppt@kernel.org>,
	"Michal Hocko" <mhocko@suse.com>
Subject: Re: [PATCH] mm/gup: unshare-check every page of a pte batch
Date: Fri, 20 Nov 2026 15:57:38 +0000	[thread overview]
Message-ID: <sashiko-outbox-225486@kernel.org> (raw)
In-Reply-To: <20261119105701.3f57ac54@fangorn>

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 <riel@surriel.com>

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.

> diff --git a/mm/gup.c b/mm/gup.c
> index ec0bc99202169..57ad488b4538d 100644
> --- a/mm/gup.c
> +++ b/mm/gup.c
> @@ -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
> -	 * 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().

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


      parent reply	other threads:[~2026-11-20 15:57 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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
2026-11-20 15:57 ` sashiko-bot [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=sashiko-outbox-225486@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=akpm@linux-foundation.org \
    --cc=david@kernel.org \
    --cc=jgg@ziepe.ca \
    --cc=jhubbard@nvidia.com \
    --cc=liam@infradead.org \
    --cc=linux-mm@kvack.org \
    --cc=ljs@kernel.org \
    --cc=mhocko@suse.com \
    --cc=peterx@redhat.com \
    --cc=riel@surriel.com \
    --cc=rppt@kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=surenb@google.com \
    --cc=yeoreum.yun@arm.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.