From: Eliav Farber 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 [ ljs: rewrote commit message ] Signed-off-by: Lorenzo Stoakes (ARM) --- 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; } -- 2.56.1 From: Eliav Farber 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. This is not currently safely handled by insert_pfn(), however, which obtains its PTE entry via get_locked_pte(), and treats a NULL return value as an allocation failure, returning VM_FAULT_OOM. The issue is that get_locked_pte() also returns NULL if the PMD entry is not a page table which can happen if a concurrent huge fault installs a huge PMD entry between __handle_mm_fault() observing the PMD entry to be empty and insert_pfn() being invoked. This can result in VM_FAULT_OOM being reported incorrectly. Address this by proactively checking whether a page table allocation actually failed, and returning VM_FAULT_NOPAGE if the NULL indicates that we were raced instead. The same assumption is made in insert_page(), however only DAX implements ->huge_fault and can reach it, and it serialises faults of different orders with its own mapping entry lock, so it does not require a change. Target the commit introducing the first driver able to do this. Link: https://lore.kernel.org/all/20261112051440.16140-3-farbere@amazon.com/ Fixes: f9e54c3a2f5b ("vfio/pci: implement huge_fault support") Cc: stable@vger.kernel.org Suggested-by: Jose A. Perez de Azpillaga Signed-off-by: Eliav Farber [ ljs: rewrote commit message, trimmed comment ] Signed-off-by: Lorenzo Stoakes (ARM) --- mm/memory.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/mm/memory.c b/mm/memory.c index e03110906aa8..cff39eccfd42 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -2866,10 +2866,17 @@ static vm_fault_t insert_pfn(struct vm_area_struct *vma, unsigned long addr, struct mm_struct *mm = vma->vm_mm; pte_t *pte, entry; spinlock_t *ptl; + pmd_t *pmd; - pte = get_locked_pte(mm, addr, &ptl); - if (!pte) + pmd = walk_to_pmd(mm, addr); + if (!pmd) return VM_FAULT_OOM; + if (pte_alloc(mm, pmd)) + return VM_FAULT_OOM; + /* We may have raced a huge fault, if so let the fault be retried. */ + pte = pte_offset_map_lock(mm, pmd, addr, &ptl); + if (!pte) + return VM_FAULT_NOPAGE; entry = ptep_get(pte); if (!pte_none(entry)) { if (mkwrite) { -- 2.56.1 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 Closes: https://lore.kernel.org/all/20261112051440.16140-4-farbere@amazon.com/ Fixes: 7bfe3b8ea6e3 ("Drivers: hv: Introduce mshv_vtl driver") Cc: stable@vger.kernel.org Signed-off-by: Lorenzo Stoakes (ARM) --- 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)) + goto out_unlock; + if (write) { if (pmd_pfn(*pmd) != pfn) { WARN_ON_ONCE(!is_huge_zero_pmd(*pmd)); @@ -1770,6 +1774,10 @@ static vm_fault_t insert_pud(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 (!pud_present(*pud) || !pud_trans_huge(*pud)) + goto out_unlock; + if (write) { if (WARN_ON_ONCE(pud_pfn(*pud) != pfn)) goto out_unlock; -- 2.56.1