All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Lorenzo Stoakes (ARM)" <ljs@kernel.org>
To: "David Hildenbrand (Arm)" <david@kernel.org>
Cc: Andrew Morton <akpm@linux-foundation.org>,
	 "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>,
	Alex Williamson <alex@shazbot.org>, Peter Xu <peterx@redhat.com>,
	 Zi Yan <ziy@nvidia.com>,
	Baolin Wang <baolin.wang@linux.alibaba.com>,
	 Nico Pache <nico.pache@linux.dev>,
	Ryan Roberts <ryan.roberts@arm.com>, Dev Jain <dev.jain@arm.com>,
	 Barry Song <baohua@kernel.org>,
	Lance Yang <lance.yang@linux.dev>,
	 Usama Arif <usama.arif@linux.dev>,
	Kiryl Shutsemau <kas@kernel.org>, Wei Liu <wei.liu@kernel.org>,
	 Roman Kisel <romank@linux.microsoft.com>,
	Saurabh Sengar <ssengar@linux.microsoft.com>,
	 Naman Jain <namjain@linux.microsoft.com>,
	Michael Kelley <mhklinux@outlook.com>,
	linux-mm@kvack.org,  linux-kernel@vger.kernel.org,
	Eliav Farber <farbere@amazon.com>,
	stable@vger.kernel.org
Subject: Re: [PATCH 1/3] mm/memory: remove racy huge PMD assertion from walk_to_pmd()
Date: Tue, 17 Nov 2026 09:35:30 +0000	[thread overview]
Message-ID: <avwbrON3_cCtaR20@gremlin> (raw)
In-Reply-To: <61d4f3ee-7a0b-4a8b-b5a2-35380dd46b4c@kernel.org>

On Mon, Nov 16, 2026 at 09:06:21PM +0100, David Hildenbrand (Arm) wrote:
> On 11/16/26 18:10, Lorenzo Stoakes (ARM) wrote:
> > On Mon, Nov 16, 2026 at 04:56:25PM +0100, David Hildenbrand (Arm) wrote:
> >> On 11/15/26 21:58, Lorenzo Stoakes (ARM) wrote:
> >>> From: Eliav Farber <farbere@amazon.com>
> >>>
> >>> walk_to_pmd() asserts that the PMD entry it returns is not huge, but does
> >>> so without holding the PMD lock, so the assertion is only valid if nothing
> >>> can concurrently install a huge PMD entry in the range.
> >>>
> >>> This is not the case for a VMA whose driver implements both ->fault and
> >>> ->huge_fault and which invokes vmf_insert_pfn() from the former.
> >>>
> >>> The existence of prctl(PR_SET_THP_DISABLE) is problematic here, as it only
> >>> takes the mmap write lock and faults taken under the VMA lock are
> >>> unaffected by this.
> >>>
> >>> This can result in two concurrent threads faulting the same thing
> >>> disagreeing as to whether to attempt a huge fault and thus ->fault and
> >>> ->huge_fault running at the same time and triggering this assertion.
> >>>
> >>> So just remove the assert.
> >>>
> >>> Target the commit which introduced the first driver able to do this for the
> >>> fix - vfio-pci. DAX also mixes fault orders within a VMA, but serialises
> >>> them using its own mapping entry lock.
> >>>
> >>> Link: https://lore.kernel.org/all/20261112051440.16140-2-farbere@amazon.com/
> >>> Fixes: f9e54c3a2f5b ("vfio/pci: implement huge_fault support")
> >>> Cc: stable@vger.kernel.org
> >>> Signed-off-by: Eliav Farber <farbere@amazon.com>
> >>> [ ljs: rewrote commit message ]
> >>> Signed-off-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>
> >>> ---
> >>>  mm/memory.c | 1 -
> >>>  1 file changed, 1 deletion(-)
> >>>
> >>> diff --git a/mm/memory.c b/mm/memory.c
> >>> index 3a20d64fd276..e03110906aa8 100644
> >>> --- a/mm/memory.c
> >>> +++ b/mm/memory.c
> >>> @@ -2384,7 +2384,6 @@ static pmd_t *walk_to_pmd(struct mm_struct *mm, unsigned long addr)
> >>>  	if (!pmd)
> >>>  		return NULL;
> >>>
> >>> -	VM_BUG_ON(pmd_trans_huge(*pmd));
> >>>  	return pmd;
> >>>  }
> >>>
> >>>
> >>
> >> Could this also stumble over a huge PUD somehow? Should we just prepare it for
> >> any leaves and bail out if we find some?
> >
> > No, walk_to_pmd() can only come from the order-0 path right now and
> > __handle_mm_fault() makes sure that can't happen in the fault path:
>
> That implies that get_locked_pte() and all insert_pages interfaces can only be
> called from that path in __handle_mm_fault().
>
> x86 an s390x use get_locked_pte() for some other paths, so I wouldn't want to
> trust on that :)

Well, I had claude go look through everything and there are no paths which are
problematic _now_ :) but future-proofing is a good idea because assumptions like
this bite you in the... assembly *ahem*

>
> (did I say that I hate walk_to_pmd and get_locked_pte? But you likely guessed it
> at this point lol)

Yeah they're bloody awful aren't they? :)

>
> >
> > 	vmf.pmd = pmd_alloc(mm, vmf.pud, address);
> > 	if (!vmf.pmd)
> > 		return VM_FAULT_OOM;
> >
> > 	/* Huge pud page fault raced with pmd_alloc? */
> > 	if (pud_trans_unstable(vmf.pud))
> > 		goto retry_pud;
> >
> > So by the time any ->fault path runs, the PUD is stable.
> >
> > We don't have to worry about the mmap paths as at that point nothing has
> > faulted in and it's all fresh.
> >
> > So it's all safe as things stand.
> >
> > But could update for future-proofing like:
> >
> > 	/* PUD leaf might have been installed. */
> > 	if (pud_trans_unstable(pud))
> > 		return NULL;
>
> Yeah, that makes sense. I was just wondering whether we'd actually want to
> return a proper error then instead, because this would not be an -ENOMEM case.

Let me have a fiddle and see what I can come up with, maybe we can use this as
an excuse to improve things a bit... :)

>
> --
> Cheers,
>
> David

--
Cheers, Lorenzo

  reply	other threads:[~2026-11-17  9:35 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-15 20:58 [PATCH 0/3] mm: fix races between huge and non-huge faults in PFN mappings Lorenzo Stoakes (ARM)
2026-11-15 20:58 ` [PATCH 1/3] mm/memory: remove racy huge PMD assertion from walk_to_pmd() Lorenzo Stoakes (ARM)
2026-11-16 10:35   ` Jose A. Perez de Azpillaga
2026-11-16 15:56   ` David Hildenbrand (Arm)
2026-11-16 17:10     ` Lorenzo Stoakes (ARM)
2026-11-16 20:06       ` David Hildenbrand (Arm)
2026-11-17  9:35         ` Lorenzo Stoakes (ARM) [this message]
2026-11-16 17:40   ` Gregory Price (Meta)
2026-11-17 17:36   ` Zi Yan
2026-11-15 20:58 ` [PATCH 2/3] mm/memory: do not report OOM if insert_pfn() races a huge fault Lorenzo Stoakes (ARM)
2026-11-16 10:36   ` Jose A. Perez de Azpillaga
2026-11-16 15:59   ` David Hildenbrand (Arm)
2026-11-16 17:21     ` Lorenzo Stoakes (ARM)
2026-11-16 20:10       ` David Hildenbrand (Arm)
2026-11-16 18:13   ` Gregory Price (Meta)
2026-11-17 18:04   ` Zi Yan
2026-11-15 20:58 ` [PATCH 3/3] mm/huge_memory: only compare PFNs of huge entries on huge fault insert Lorenzo Stoakes (ARM)
2026-11-16 10:42   ` Jose A. Perez de Azpillaga
2026-11-16 16:01   ` David Hildenbrand (Arm)
2026-11-16 16:55     ` Lorenzo Stoakes (ARM)
2026-11-16 20:09       ` David Hildenbrand (Arm)
2026-11-16 18:22   ` Gregory Price (Meta)
2026-11-17 18:18   ` Zi Yan
2026-11-18  6:42   ` Naman Jain

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=avwbrON3_cCtaR20@gremlin \
    --to=ljs@kernel.org \
    --cc=akpm@linux-foundation.org \
    --cc=alex@shazbot.org \
    --cc=baohua@kernel.org \
    --cc=baolin.wang@linux.alibaba.com \
    --cc=david@kernel.org \
    --cc=dev.jain@arm.com \
    --cc=farbere@amazon.com \
    --cc=kas@kernel.org \
    --cc=lance.yang@linux.dev \
    --cc=liam@infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=mhklinux@outlook.com \
    --cc=mhocko@suse.com \
    --cc=namjain@linux.microsoft.com \
    --cc=nico.pache@linux.dev \
    --cc=peterx@redhat.com \
    --cc=romank@linux.microsoft.com \
    --cc=rppt@kernel.org \
    --cc=ryan.roberts@arm.com \
    --cc=ssengar@linux.microsoft.com \
    --cc=stable@vger.kernel.org \
    --cc=surenb@google.com \
    --cc=usama.arif@linux.dev \
    --cc=vbabka@kernel.org \
    --cc=wei.liu@kernel.org \
    --cc=ziy@nvidia.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.