Linux Documentation
 help / color / mirror / Atom feed
From: "Lorenzo Stoakes (ARM)" <ljs@kernel.org>
To: "David Hildenbrand (Arm)" <david@kernel.org>
Cc: Hugh Dickins <hughd@google.com>,
	 Baolin Wang <baolin.wang@linux.alibaba.com>,
	Andrew Morton <akpm@linux-foundation.org>,
	 David Rheinsberg <david@readahead.eu>,
	"Liam R. Howlett" <liam@infradead.org>,
	 Vlastimil Babka <vbabka@kernel.org>,
	Mike Rapoport <rppt@kernel.org>,
	 Suren Baghdasaryan <surenb@google.com>,
	Michal Hocko <mhocko@suse.com>, Jonathan Corbet <corbet@lwn.net>,
	 Shuah Khan <skhan@linuxfoundation.org>,
	Randy Dunlap <rdunlap@infradead.org>,
	linux-mm@kvack.org,  linux-kernel@vger.kernel.org,
	linux-doc@vger.kernel.org, stable@vger.kernel.org,
	 Chris Mason <mason@kernel.org>
Subject: Re: [PATCH 1/2] mm/memfd: handle GUP pins more reliably when write-sealing
Date: Wed, 4 Nov 2026 09:15:06 +0000	[thread overview]
Message-ID: <aur3R_3_oz4Isjr4@gremlin> (raw)
In-Reply-To: <20261103-fixes_core_2-v1-1-f332954f1df4@kernel.org>

On Tue, Nov 03, 2026 at 07:31:44PM +0100, David Hildenbrand (Arm) wrote:
> memfd_wait_for_pins() detects GUP references by comparing a folio's
> actual refcount with its expected refcount. As spelled out by
> folio_expected_ref_count(), obtaining the expected refcount when there
> can be concurrent folio (un)mapping is racy.
>
> So it is possible that a changing folio mapcount can race with our
> check for GUP references, and F_SEAL_WRITE might succeed even if there
> are some, allowing file contents to be modified after sealing.
>
> For folios with a reliable pincount, detect FOLL_PIN references using
> folio_maybe_dma_pinned() before falling back to the refcount comparison.
> For small folios without that, the 1024 pinning BIAS makes it very hard
> (although not impossible) to misguide the refcount comparison.
>
> This improves FOLL_PIN detection for folios with a pincount. Handling
> FOLL_GET and small folios requires a more fundamental change.
>
> Fortunately, most interfaces that allow for GUP'ing such pages use
> FOLL_PIN, in particular interfaces using FOLL_LONGTERM. However, we
> still have some FOLL_GET leftovers ...
>
> Fixes: 05f65b5c7090 ("shm: wait for pins to be released when sealing")
> Cc: stable@vger.kernel.org
> Cc: Chris Mason <mason@kernel.org>
> Signed-off-by: David Hildenbrand (Arm) <david@kernel.org>

All very delicate stuff, I wonder if we could somehow make the logic here
generalised in a follow-up?

Anyway seems reasonable to me so:

Reviewed-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>

> ---
>  mm/memfd.c | 7 +++++++
>  1 file changed, 7 insertions(+)
>
> diff --git a/mm/memfd.c b/mm/memfd.c
> index 0f6fff004f5e4..9eda4c3eeaca5 100644
> --- a/mm/memfd.c
> +++ b/mm/memfd.c
> @@ -32,6 +32,13 @@
>
>  static bool memfd_folio_has_extra_refs(struct folio *folio)
>  {
> +	/*
> +	 * Use the precise pincount where available to avoid racy refcount
> +	 * comparisons.
> +	 */
> +	if (folio_has_pincount(folio) && folio_maybe_dma_pinned(folio))
> +		return true;
> +
>  	return folio_ref_count(folio) != folio_expected_ref_count(folio);
>  }
>
>
> --
> 2.43.0
>

--
Cheers, Lorenzo

  reply	other threads:[~2026-11-04  9:15 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-03 18:31 [PATCH 0/2] mm/memfd: handle GUP references reliably when write-sealing David Hildenbrand (Arm)
2026-11-03 18:31 ` [PATCH 1/2] mm/memfd: handle GUP pins more " David Hildenbrand (Arm)
2026-11-04  9:15   ` Lorenzo Stoakes (ARM) [this message]
2026-11-03 18:31 ` [PATCH 2/2] mm/memfd: handle GUP references " David Hildenbrand (Arm)

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=aur3R_3_oz4Isjr4@gremlin \
    --to=ljs@kernel.org \
    --cc=akpm@linux-foundation.org \
    --cc=baolin.wang@linux.alibaba.com \
    --cc=corbet@lwn.net \
    --cc=david@kernel.org \
    --cc=david@readahead.eu \
    --cc=hughd@google.com \
    --cc=liam@infradead.org \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=mason@kernel.org \
    --cc=mhocko@suse.com \
    --cc=rdunlap@infradead.org \
    --cc=rppt@kernel.org \
    --cc=skhan@linuxfoundation.org \
    --cc=stable@vger.kernel.org \
    --cc=surenb@google.com \
    --cc=vbabka@kernel.org \
    /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