* [PATCH v2 1/4] mm: drop the huge PMD assertion from walk_to_pmd()
2026-11-12 5:14 [PATCH v2 0/4] mm: fixes for huge PFN mappings Eliav Farber
@ 2026-11-12 5:14 ` Eliav Farber
2026-11-12 5:14 ` [PATCH v2 2/4] mm: don't report OOM when a huge PMD race blocks a pfn insert Eliav Farber
` (2 subsequent siblings)
3 siblings, 0 replies; 7+ messages in thread
From: Eliav Farber @ 2026-11-12 5:14 UTC (permalink / raw)
To: Andrew Morton, David Hildenbrand, Zi Yan
Cc: Lorenzo Stoakes, Liam R. Howlett, Vlastimil Babka, Mike Rapoport,
Suren Baghdasaryan, Michal Hocko, Baolin Wang, Nico Pache,
Ryan Roberts, Dev Jain, Barry Song, Lance Yang, Usama Arif,
Kiryl Shutsemau, Thomas Gleixner, Ingo Molnar, Borislav Petkov,
Dave Hansen, H. Peter Anvin, x86, William Kucharski, Vishal Moola,
Alex Williamson, Matthew Wilcox, Peter Xu,
Jose A. Perez de Azpillaga, linux-mm, linux-kernel, Eliav Farber
walk_to_pmd() asserts that the PMD it returns is not a huge entry. That
held when commit c2febafc6773 ("mm: convert generic code to 5-level
paging") added the assertion: nothing installed a huge PMD over the
mappings its callers walk.
ARCH_SUPPORTS_HUGE_PFNMAP changed that. A VM_PFNMAP mapping whose driver
provides both ->fault and ->huge_fault can have a PMD leaf installed by a
concurrent huge fault over the same address, so a caller can observe one
and BUG on CONFIG_DEBUG_VM=y.
The walk does not need the invariant. pmd_alloc() only allocates when the
PUD is empty and otherwise returns pmd_offset(), so a leaf is returned
rather than overwritten. Every caller already copes with one, because
they all reach pte_offset_map_lock(), which refuses to walk a leaf and
returns NULL: insert_pages() turns that into -EFAULT, get_locked_pte()
passes the NULL to its callers, and insert_pfn() returns a fault code.
Fixes: f9e54c3a2f5b ("vfio/pci: implement huge_fault support")
Signed-off-by: Eliav Farber <farbere@amazon.com>
Reviewed-by: Jose A. Perez de Azpillaga <azpijr@gmail.com>
---
mm/memory.c | 1 -
1 file changed, 1 deletion(-)
diff --git a/mm/memory.c b/mm/memory.c
index ec3d9d99ccee..0f54ee0a6d88 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.47.3
^ permalink raw reply related [flat|nested] 7+ messages in thread* [PATCH v2 2/4] mm: don't report OOM when a huge PMD race blocks a pfn insert
2026-11-12 5:14 [PATCH v2 0/4] mm: fixes for huge PFN mappings Eliav Farber
2026-11-12 5:14 ` [PATCH v2 1/4] mm: drop the huge PMD assertion from walk_to_pmd() Eliav Farber
@ 2026-11-12 5:14 ` Eliav Farber
2026-11-12 10:07 ` Jose A. Perez de Azpillaga
2026-11-12 5:14 ` [PATCH v2 3/4] mm/huge_memory: don't warn when a page table blocks a huge " Eliav Farber
2026-11-12 5:14 ` [PATCH v2 4/4] mm/huge_memory: use the large-page PAT encoding for huge pfn mappings Eliav Farber
3 siblings, 1 reply; 7+ messages in thread
From: Eliav Farber @ 2026-11-12 5:14 UTC (permalink / raw)
To: Andrew Morton, David Hildenbrand, Zi Yan
Cc: Lorenzo Stoakes, Liam R. Howlett, Vlastimil Babka, Mike Rapoport,
Suren Baghdasaryan, Michal Hocko, Baolin Wang, Nico Pache,
Ryan Roberts, Dev Jain, Barry Song, Lance Yang, Usama Arif,
Kiryl Shutsemau, Thomas Gleixner, Ingo Molnar, Borislav Petkov,
Dave Hansen, H. Peter Anvin, x86, William Kucharski, Vishal Moola,
Alex Williamson, Matthew Wilcox, Peter Xu,
Jose A. Perez de Azpillaga, linux-mm, linux-kernel, Eliav Farber
insert_pfn() maps the PTE with get_locked_pte() and treats a NULL return
as an allocation failure. pte_offset_map_lock() also returns NULL when
the PMD is a leaf, which a VM_PFNMAP mapping with both ->fault and
->huge_fault can hit: an order-0 fault that passed handle_pte_fault()'s
pmd_none() check can reach insert_pfn() after a concurrent huge fault
installed a PMD leaf over the same address. The task then takes a
VM_FAULT_OOM for a mapping that is perfectly healthy. Changing an mm's
THP policy while another thread faults the same PMD, for example with
prctl(PR_SET_THP_DISABLE), is enough to produce the mixed orders.
get_locked_pte() cannot tell the two apart, because pte_alloc_map_lock()
folds the allocation and the map into one call. Walk to the PMD,
allocate, and map as three steps, so that only a failed allocation
returns VM_FAULT_OOM. A NULL from the map returns VM_FAULT_NOPAGE and
the access refaults, which is what a PMD leaf, a PMD that is not present
and a bad PMD that __pte_offset_map() cleared all want.
Fixes: f9e54c3a2f5b ("vfio/pci: implement huge_fault support")
Signed-off-by: Eliav Farber <farbere@amazon.com>
Suggested-by: Jose A. Perez de Azpillaga <azpijr@gmail.com>
---
v2: Split pte_alloc_map_lock() into pte_alloc() followed by
pte_offset_map_lock(), so that only a failed allocation returns
VM_FAULT_OOM and a NULL from the map returns VM_FAULT_NOPAGE. v1
called pte_alloc_map_lock() and then re-read the PMD with
pmd_trans_huge() to tell the two apart, which left the non-present
and bad-PMD cases reported as OOM. Suggested by Jose A. Perez de
Azpillaga.
mm/memory.c | 18 ++++++++++++++++--
1 file changed, 16 insertions(+), 2 deletions(-)
diff --git a/mm/memory.c b/mm/memory.c
index 0f54ee0a6d88..05ae87518206 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -2866,10 +2866,24 @@ 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;
+
+ /*
+ * A concurrent ->huge_fault may have installed a PMD leaf over addr,
+ * which pte_offset_map_lock() refuses to walk. The mapping is fine, so
+ * let the access refault instead of reporting a failed allocation.
+ */
+ 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.47.3
^ permalink raw reply related [flat|nested] 7+ messages in thread* Re: [PATCH v2 2/4] mm: don't report OOM when a huge PMD race blocks a pfn insert
2026-11-12 5:14 ` [PATCH v2 2/4] mm: don't report OOM when a huge PMD race blocks a pfn insert Eliav Farber
@ 2026-11-12 10:07 ` Jose A. Perez de Azpillaga
0 siblings, 0 replies; 7+ messages in thread
From: Jose A. Perez de Azpillaga @ 2026-11-12 10:07 UTC (permalink / raw)
To: Eliav Farber, Andrew Morton, David Hildenbrand, Zi Yan
Cc: Lorenzo Stoakes, Liam R. Howlett, Vlastimil Babka, Mike Rapoport,
Suren Baghdasaryan, Michal Hocko, Baolin Wang, Nico Pache,
Ryan Roberts, Dev Jain, Barry Song, Lance Yang, Usama Arif,
Kiryl Shutsemau, Thomas Gleixner, Ingo Molnar, Borislav Petkov,
Dave Hansen, H. Peter Anvin, x86, William Kucharski, Vishal Moola,
Alex Williamson, Matthew Wilcox, Peter Xu, linux-mm, linux-kernel
On 11/12/26 6:14 AM, Eliav Farber wrote:
> insert_pfn() maps the PTE with get_locked_pte() and treats a NULL return
> as an allocation failure. pte_offset_map_lock() also returns NULL when
> the PMD is a leaf, which a VM_PFNMAP mapping with both ->fault and
> ->huge_fault can hit: an order-0 fault that passed handle_pte_fault()'s
> pmd_none() check can reach insert_pfn() after a concurrent huge fault
> installed a PMD leaf over the same address. The task then takes a
> VM_FAULT_OOM for a mapping that is perfectly healthy. Changing an mm's
> THP policy while another thread faults the same PMD, for example with
> prctl(PR_SET_THP_DISABLE), is enough to produce the mixed orders.
>
> get_locked_pte() cannot tell the two apart, because pte_alloc_map_lock()
> folds the allocation and the map into one call. Walk to the PMD,
> allocate, and map as three steps, so that only a failed allocation
> returns VM_FAULT_OOM. A NULL from the map returns VM_FAULT_NOPAGE and
> the access refaults, which is what a PMD leaf, a PMD that is not present
> and a bad PMD that __pte_offset_map() cleared all want.
>
> Fixes: f9e54c3a2f5b ("vfio/pci: implement huge_fault support")
> Signed-off-by: Eliav Farber <farbere@amazon.com>
> Suggested-by: Jose A. Perez de Azpillaga <azpijr@gmail.com>
LGTM:
Reviewed-by: Jose A. Perez de Azpillaga <azpijr@gmail.com>
--
cheers,
jose a. p-a
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH v2 3/4] mm/huge_memory: don't warn when a page table blocks a huge pfn insert
2026-11-12 5:14 [PATCH v2 0/4] mm: fixes for huge PFN mappings Eliav Farber
2026-11-12 5:14 ` [PATCH v2 1/4] mm: drop the huge PMD assertion from walk_to_pmd() Eliav Farber
2026-11-12 5:14 ` [PATCH v2 2/4] mm: don't report OOM when a huge PMD race blocks a pfn insert Eliav Farber
@ 2026-11-12 5:14 ` Eliav Farber
2026-11-12 5:14 ` [PATCH v2 4/4] mm/huge_memory: use the large-page PAT encoding for huge pfn mappings Eliav Farber
3 siblings, 0 replies; 7+ messages in thread
From: Eliav Farber @ 2026-11-12 5:14 UTC (permalink / raw)
To: Andrew Morton, David Hildenbrand, Zi Yan
Cc: Lorenzo Stoakes, Liam R. Howlett, Vlastimil Babka, Mike Rapoport,
Suren Baghdasaryan, Michal Hocko, Baolin Wang, Nico Pache,
Ryan Roberts, Dev Jain, Barry Song, Lance Yang, Usama Arif,
Kiryl Shutsemau, Thomas Gleixner, Ingo Molnar, Borislav Petkov,
Dave Hansen, H. Peter Anvin, x86, William Kucharski, Vishal Moola,
Alex Williamson, Matthew Wilcox, Peter Xu,
Jose A. Perez de Azpillaga, linux-mm, linux-kernel, Eliav Farber
insert_pmd() and insert_pud() treat a non-empty entry at their own level
as a leaf and compare its PFN against the one being inserted. A page
table installed by a concurrent order-0 fault also makes the entry
non-empty, and pmd_pfn()/pud_pfn() then report the PFN of that table,
which never matches. insert_pmd() warns unless the entry is the huge
zero PMD, and insert_pud() warns on any mismatch.
A VM_PFNMAP mapping with both ->fault and ->huge_fault can produce the
mixed orders: an order-0 fault installs a page table over an address
while a concurrent huge fault on the same address reaches insert_pmd().
Changing an mm's THP policy while another thread faults the same PMD,
for example with prctl(PR_SET_THP_DISABLE), is enough to do it.
Compare the PFN only for a leaf entry. A page table is not an error, so
take the existing out_unlock path, which returns VM_FAULT_NOPAGE and
lets the access refault onto the smaller entry.
Fixes: f9e54c3a2f5b ("vfio/pci: implement huge_fault support")
Signed-off-by: Eliav Farber <farbere@amazon.com>
Reviewed-by: Jose A. Perez de Azpillaga <azpijr@gmail.com>
---
mm/huge_memory.c | 16 ++++++++++++++++
1 file changed, 16 insertions(+)
diff --git a/mm/huge_memory.c b/mm/huge_memory.c
index eb913acb03b7..052c50dadc48 100644
--- a/mm/huge_memory.c
+++ b/mm/huge_memory.c
@@ -1643,6 +1643,14 @@ 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;
+ /*
+ * A concurrent order-0 fault may have installed a PTE table
+ * here, which pmd_pfn() would report as the PFN of the table
+ * itself. Only a leaf entry can be compared.
+ */
+ if (!pmd_leaf(*pmd))
+ goto out_unlock;
+
if (write) {
if (pmd_pfn(*pmd) != pfn) {
WARN_ON_ONCE(!is_huge_zero_pmd(*pmd));
@@ -1770,6 +1778,14 @@ 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;
+ /*
+ * A concurrent order-0 fault may have installed a PMD table
+ * here, which pud_pfn() would report as the PFN of the table
+ * itself. Only a leaf entry can be compared.
+ */
+ if (!pud_leaf(*pud))
+ goto out_unlock;
+
if (write) {
if (WARN_ON_ONCE(pud_pfn(*pud) != pfn))
goto out_unlock;
--
2.47.3
^ permalink raw reply related [flat|nested] 7+ messages in thread* [PATCH v2 4/4] mm/huge_memory: use the large-page PAT encoding for huge pfn mappings
2026-11-12 5:14 [PATCH v2 0/4] mm: fixes for huge PFN mappings Eliav Farber
` (2 preceding siblings ...)
2026-11-12 5:14 ` [PATCH v2 3/4] mm/huge_memory: don't warn when a page table blocks a huge " Eliav Farber
@ 2026-11-12 5:14 ` Eliav Farber
2026-11-12 10:08 ` Jose A. Perez de Azpillaga
3 siblings, 1 reply; 7+ messages in thread
From: Eliav Farber @ 2026-11-12 5:14 UTC (permalink / raw)
To: Andrew Morton, David Hildenbrand, Zi Yan
Cc: Lorenzo Stoakes, Liam R. Howlett, Vlastimil Babka, Mike Rapoport,
Suren Baghdasaryan, Michal Hocko, Baolin Wang, Nico Pache,
Ryan Roberts, Dev Jain, Barry Song, Lance Yang, Usama Arif,
Kiryl Shutsemau, Thomas Gleixner, Ingo Molnar, Borislav Petkov,
Dave Hansen, H. Peter Anvin, x86, William Kucharski, Vishal Moola,
Alex Williamson, Matthew Wilcox, Peter Xu,
Jose A. Perez de Azpillaga, linux-mm, linux-kernel, Eliav Farber
insert_pmd() and insert_pud() build the entry from the pgprot that
vmf_insert_pfn_pmd()/_pud() took from vma->vm_page_prot and passed
through pfnmap_setup_cachemode_pfn(). That pgprot carries the PTE-level
cache mode encoding, but x86 keeps the high PAT bit at bit 7 for a PTE
(_PAGE_BIT_PAT) and at bit 12 for a PMD or PUD (_PAGE_BIT_PAT_LARGE),
where bit 7 is _PAGE_PSE.
A memtype that needs that bit, write-through or write-protect, loses it:
the cache mode index of the huge entry drops from 4-7 to 0-3. With the
PAT slots Linux programs, write-through (slot 7) becomes UC (slot 3),
and write-protect (slot 5) becomes write-combining (slot 1), which loses
the write protection. The other x86 paths that build a large entry from
a PTE pgprot convert it first, see populate_pmd() and populate_pud().
Convert in the two huge pfn paths. Add a generic pgprot_4k_2_large()
that returns its argument unchanged, so that only architectures which
encode the levels differently have to define it, and x86's existing
definition is picked up.
The folio paths keep using vma->vm_page_prot as is. They map
struct-page-backed memory, which x86 maps write-back.
Fixes: 5cad465d7fa6 ("mm: add vmf_insert_pfn_pmd()")
Signed-off-by: Eliav Farber <farbere@amazon.com>
---
v2: Corrected the second paragraph of the commit message. It said a
write-through range ends up write-back; with the PAT slots Linux
programs, write-through (slot 7) ends up UC (slot 3) and
write-protect (slot 5) ends up write-combining (slot 1).
Changed Fixes: from 6857be5fecae to 5cad465d7fa6 ("mm: add
vmf_insert_pfn_pmd()"), which is the commit that introduced the
unconverted pgprot. Both pointed out by Jose A. Perez de Azpillaga.
No code change.
arch/x86/include/asm/pgtable_types.h | 2 ++
include/linux/pgtable.h | 9 +++++++++
mm/huge_memory.c | 4 ++--
3 files changed, 13 insertions(+), 2 deletions(-)
diff --git a/arch/x86/include/asm/pgtable_types.h b/arch/x86/include/asm/pgtable_types.h
index b1a669e775f4..b1ffd72e47ef 100644
--- a/arch/x86/include/asm/pgtable_types.h
+++ b/arch/x86/include/asm/pgtable_types.h
@@ -497,6 +497,8 @@ static inline pgprotval_t protval_4k_2_large(pgprotval_t val)
return (val & ~(_PAGE_PAT | _PAGE_PAT_LARGE)) |
((val & _PAGE_PAT) << (_PAGE_BIT_PAT_LARGE - _PAGE_BIT_PAT));
}
+
+#define pgprot_4k_2_large pgprot_4k_2_large
static inline pgprot_t pgprot_4k_2_large(pgprot_t pgprot)
{
return __pgprot(protval_4k_2_large(pgprot_val(pgprot)));
diff --git a/include/linux/pgtable.h b/include/linux/pgtable.h
index 780fe849ff8b..112814d31f0c 100644
--- a/include/linux/pgtable.h
+++ b/include/linux/pgtable.h
@@ -1787,6 +1787,15 @@ void arch_sync_kernel_mappings(unsigned long start, unsigned long end);
#define pgprot_mhp(prot) (prot)
#endif
+/*
+ * Convert a pgprot built for a PTE so that it can be used in a PMD or PUD
+ * entry. Only needed on architectures that encode attributes differently
+ * between the levels; elsewhere the pgprot is used as is.
+ */
+#ifndef pgprot_4k_2_large
+#define pgprot_4k_2_large(prot) (prot)
+#endif
+
#ifdef CONFIG_MMU
#ifndef pgprot_modify
#define pgprot_modify pgprot_modify
diff --git a/mm/huge_memory.c b/mm/huge_memory.c
index 052c50dadc48..945d480c1512 100644
--- a/mm/huge_memory.c
+++ b/mm/huge_memory.c
@@ -1675,7 +1675,7 @@ static vm_fault_t insert_pmd(struct vm_area_struct *vma, unsigned long addr,
add_mm_counter(mm, mm_counter_file(fop.folio), HPAGE_PMD_NR);
}
} else {
- entry = pmd_mkhuge(pfn_pmd(fop.pfn, prot));
+ entry = pmd_mkhuge(pfn_pmd(fop.pfn, pgprot_4k_2_large(prot)));
entry = pmd_mkspecial(entry);
}
if (write) {
@@ -1804,7 +1804,7 @@ static vm_fault_t insert_pud(struct vm_area_struct *vma, unsigned long addr,
folio_add_file_rmap_pud(fop.folio, &fop.folio->page, vma);
add_mm_counter(mm, mm_counter_file(fop.folio), HPAGE_PUD_NR);
} else {
- entry = pud_mkhuge(pfn_pud(fop.pfn, prot));
+ entry = pud_mkhuge(pfn_pud(fop.pfn, pgprot_4k_2_large(prot)));
entry = pud_mkspecial(entry);
}
if (write) {
--
2.47.3
^ permalink raw reply related [flat|nested] 7+ messages in thread* Re: [PATCH v2 4/4] mm/huge_memory: use the large-page PAT encoding for huge pfn mappings
2026-11-12 5:14 ` [PATCH v2 4/4] mm/huge_memory: use the large-page PAT encoding for huge pfn mappings Eliav Farber
@ 2026-11-12 10:08 ` Jose A. Perez de Azpillaga
0 siblings, 0 replies; 7+ messages in thread
From: Jose A. Perez de Azpillaga @ 2026-11-12 10:08 UTC (permalink / raw)
To: Eliav Farber, Andrew Morton, David Hildenbrand, Zi Yan
Cc: Lorenzo Stoakes, Liam R. Howlett, Vlastimil Babka, Mike Rapoport,
Suren Baghdasaryan, Michal Hocko, Baolin Wang, Nico Pache,
Ryan Roberts, Dev Jain, Barry Song, Lance Yang, Usama Arif,
Kiryl Shutsemau, Thomas Gleixner, Ingo Molnar, Borislav Petkov,
Dave Hansen, H. Peter Anvin, x86, William Kucharski, Vishal Moola,
Alex Williamson, Matthew Wilcox, Peter Xu, linux-mm, linux-kernel
On 11/12/26 6:14 AM, Eliav Farber wrote:
> insert_pmd() and insert_pud() build the entry from the pgprot that
> vmf_insert_pfn_pmd()/_pud() took from vma->vm_page_prot and passed
> through pfnmap_setup_cachemode_pfn(). That pgprot carries the PTE-level
> cache mode encoding, but x86 keeps the high PAT bit at bit 7 for a PTE
> (_PAGE_BIT_PAT) and at bit 12 for a PMD or PUD (_PAGE_BIT_PAT_LARGE),
> where bit 7 is _PAGE_PSE.
>
> A memtype that needs that bit, write-through or write-protect, loses it:
> the cache mode index of the huge entry drops from 4-7 to 0-3. With the
> PAT slots Linux programs, write-through (slot 7) becomes UC (slot 3),
> and write-protect (slot 5) becomes write-combining (slot 1), which loses
> the write protection. The other x86 paths that build a large entry from
> a PTE pgprot convert it first, see populate_pmd() and populate_pud().
>
> Convert in the two huge pfn paths. Add a generic pgprot_4k_2_large()
> that returns its argument unchanged, so that only architectures which
> encode the levels differently have to define it, and x86's existing
> definition is picked up.
>
> The folio paths keep using vma->vm_page_prot as is. They map
> struct-page-backed memory, which x86 maps write-back.
>
> Fixes: 5cad465d7fa6 ("mm: add vmf_insert_pfn_pmd()")
> Signed-off-by: Eliav Farber <farbere@amazon.com>
LGTM:
Reviewed-by: Jose A. Perez de Azpillaga <azpijr@gmail.com>
--
cheers,
jose a. p-a
^ permalink raw reply [flat|nested] 7+ messages in thread