Re: [PATCH 3/3] mm/huge_memory: only compare PFNs of huge entries on huge fault insert
From: Lorenzo Stoakes (ARM)
Date: Mon Nov 16 2026 - 11:55:32 EST
On Mon, Nov 16, 2026 at 05:01:52PM +0100, David Hildenbrand (Arm) wrote:
> On 11/15/26 21:58, Lorenzo Stoakes (ARM) wrote:
> > A driver that implements both ->fault and ->huge_fault must anticipate
> > concurrent use of both.
> >
> > This can happen, for example, if a user calls prctl(PR_SET_THP_DISABLE)
> > concurrent with faulting, as that only holds the mmap write lock and thus
> > does not exclude faulting threads stabilised by VMA lock.
> >
> > insert_pmd() and insert_pud() have issues with this as they both assume
> > that, if the page table entry being populated is not empty, it must a
> > present huge entry.
> >
> > This is problematic as if it is a write fault, it compares its PFN against
> > the one being inserted, warning if they differ, on the assumption that the
> > entry is a huge leaf.
> >
> > Since the entry may be a page table, this is invalid and can result in
> > spurious warnings.
> >
> > Equally, the entry might be non-present. Nothing can install such an entry
> > in the VMAs that reach this code today, but there is no reason to rely upon
> > that.
> >
> > Therefore only examine the PFN if the entry is both present and huge, and
> > otherwise simply return VM_FAULT_NOPAGE so the fault is retried and handled
> > at the appropriate level.
> >
> > This is what already happens for a read fault, for which the existing entry
> > is not examined at all.
> >
> > Target the commit which introduced the first driver which both mixes fault
> > orders and passes the write flag for the fix (since this commit, both
> > drm_gem_shmem and panthor are prey to this issue).
> >
> > Reported-by: Eliav Farber <farbere@xxxxxxxxxx>
> > Closes: https://lore.kernel.org/all/20261112051440.16140-4-farbere@xxxxxxxxxx/
> > Fixes: 7bfe3b8ea6e3 ("Drivers: hv: Introduce mshv_vtl driver")
> > Cc: stable@xxxxxxxxxxxxxxx
> > Signed-off-by: Lorenzo Stoakes (ARM) <ljs@xxxxxxxxxx>
> > ---
> > mm/huge_memory.c | 8 ++++++++
> > 1 file changed, 8 insertions(+)
> >
> > diff --git a/mm/huge_memory.c b/mm/huge_memory.c
> > index 0d589be136d2..da632c3e834b 100644
> > --- a/mm/huge_memory.c
> > +++ b/mm/huge_memory.c
> > @@ -1643,6 +1643,10 @@ static vm_fault_t insert_pmd(struct vm_area_struct *vma, unsigned long addr,
> > const unsigned long pfn = fop.is_folio ? folio_pfn(fop.folio) :
> > fop.pfn;
> >
> > + /* We may have raced a fault which installed a page table. */
> > + if (!pmd_present(*pmd) || !pmd_trans_huge(*pmd))
>
> Ok, we hold the PMD lock, so that cannot change concurrently.
>
> Given that we are in THP code, I wonder if we could just test for pmd_leaf()
> instead.
>
> Same below for PUDs.
That's what the original did, but as the commit message says I don't want
to rely on the entry not being a softleaf here, in case we do something
later that changes that.
It's future-proofing.
>
> Acked-by: David Hildenbrand (Arm) <david@xxxxxxxxxx>
Thanks!
>
> --
> Cheers,
>
> David
--
Cheers, Lorenzo