FAILED: patch "[PATCH] mm/gup: recheck the PUD under PTL in follow_pud_mask()" failed to apply to 6.12-stable tree
Subsystems (3)
Sasha Levin
maintainerThe patch below does not apply to the 6.12-stable tree.
If someone wants it applied there, or to any other stable or longterm tree, then please email the backport, including the original git commit id to <[email protected]>.
To reproduce the conflict and resubmit, you may use the following commands:
git fetch https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/ linux-6.12.y
git checkout FETCH_HEAD
git cherry-pick -x 79cb51d538abc459f74e046d27797d36b133ce0e
# <resolve conflicts, build, test, etc.>
git commit -s
git send-email --to '<[email protected]>' --in-reply-to '[email protected]' --subject-prefix 'PATCH 6.12.y' 'HEAD^..'
Thanks,
Sasha
------------------ original commit in Linus's tree ------------------
From 79cb51d538abc459f74e046d27797d36b133ce0e Mon Sep 17 00:00:00 2001
From: "David Hildenbrand (Arm)" <[email protected]>
Date: Sat, 31 Oct 2026 10:39:48 +0100
Subject: [PATCH] mm/gup: recheck the PUD under PTL in follow_pud_mask()
follow_pud_mask() reads the PUD without a lock. If it is a leaf, it takes the lock and calls follow_huge_pud(), which reads the entry again, but only tests whether it is present.
A leaf can be zapped concurrently and a page fault can fill the empty entry with a page table.
This currently only applies when device-dax makes use of PUD entries, and the fault after e.g., MADV_DONTNEED would find THPs to be disabled.
That requires a sequence like
1) Fault in the PUD
2) Disable THP
3) Have GUP race against concurrent MADV_DONTNEED + refault
Which is rather unlikely to happen in practice, which is why we never received a report about such a problem.
Before commit 1b1676180246 ("mm/gup: handle huge pud for follow_pud_mask()"), we performed that check in follow_devmap_pud() by testing for pud_devmap().
Let's fix it by just structuring follow_pud_mask() similar to follow_pmd_mask(), minus protnone stuff that does not apply and minus FOLL_SPLIT_PMD handling. That makes it much easier to spot any such inconsistencies.
Perform the pud_bad() check early in follow_pmd_mask() and just forward the PUD value. We can drop the pud_present() check in follow_huge_pud(), just like we have in follow_huge_pmd().
Even though we are performing more changes than strictly necessary, the fix should be easy to backport.
--- mm/gup.c | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-)
mm/gup.c +18 -14
@@ -658,9 +658,6 @@ static struct page *follow_huge_pud(struct vm_area_struct *vma,assert_spin_locked (pud_lockptr (mm, pudp)); -if (!pud_present (pud)) -return NULL ; -if ((flags &FOLL_WRITE ) && !can_follow_write_pud (pud,pfn_to_page (pfn), vma, flags))return NULL ;
@@ -896,15 +893,17 @@ static struct page *follow_page_pte(struct vm_area_struct *vma, }static struct page *follow_pmd_mask (struct vm_area_struct *vma, -unsigned long address,pud_t *pudp, -unsigned int flags, -unsigned long *page_mask) +unsigned long address,pud_t *pudp,pud_t pud, +unsigned int flags,unsigned long *page_mask) {pmd_t *pmd, pmdval;spinlock_t *ptl;struct page *page;struct mm_struct *mm = vma->vm_mm; +if (unlikely (pud_bad (pud))) +return no_page_table (vma, flags, address); + pmd =pmd_offset (pudp, address); pmdval =pmdp_get_lockless (pmd);if (pmd_none (pmdval))
@@ -953,18 +952,23 @@ static struct page *follow_pud_mask(struct vm_area_struct *vma, pud =pudp_get (pudp);if (!pud_present (pud))return no_page_table (vma, flags, address); -if (pud_leaf (pud)) { - ptl =pud_lock (mm, pudp); - page =follow_huge_pud (vma, address, pudp, flags, page_mask); +if (!pud_leaf (pud)) +return follow_pmd_mask (vma, address, pudp, pud, flags, page_mask); + + ptl =pud_lock (mm, pudp); + pud = *pudp; +if (unlikely (!pud_present (pud))) {spin_unlock (ptl); -if (page) -return page;return no_page_table (vma, flags, address); } -if (unlikely (pud_bad (pud))) -return no_page_table (vma, flags, address); +if (unlikely (!pud_leaf (pud))) { +spin_unlock (ptl); +return follow_pmd_mask (vma, address, pudp, pud, flags, page_mask); + } -return follow_pmd_mask (vma, address, pudp, flags, page_mask); + page =follow_huge_pud (vma, address, pudp, flags, page_mask); +spin_unlock (ptl); +return page; } static struct page *follow_p4d_mask(struct vm_area_struct *vma,
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 1/4 mm/gup: remove redundant check for PCI P2PDMA page
From: Alistair Popple <[email protected]>
[ Upstream commit a58c6fb6623d8aef36c0c4e0b8a48770deef3213 ]
PCI P2PDMA pages are not mapped with pXX_devmap PTEs therefore the check in __gup_device_huge() is redundant. Remove it
Stable-dep-of: 79cb51d538ab ("mm/gup: recheck the PUD under PTL in follow_pud_mask()")
--- mm/gup.c | 5 ----- 1 file changed, 5 deletions(-)
mm/gup.c +0 -5
@@ -3058,11 +3058,6 @@ static int gup_fast_devmap_leaf(unsigned long pfn, unsigned long addr,break ; } -if (!(flags &FOLL_PCI_P2PDMA ) &&is_pci_p2pdma_page (page)) { -gup_fast_undo_dev_pagemap (nr, nr_start, flags, pages); -break ; - } - folio =try_grab_folio_fast (page,1 , flags);if (!folio) {gup_fast_undo_dev_pagemap (nr, nr_start, flags, pages);
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 2/4 mm/gup: remove pXX_devmap usage from get_user_pages()
From: Alistair Popple <[email protected]>
[ Upstream commit fd2825b0760a10a9626986cca64f5664302ffdfc ]
GUP uses pXX_devmap() calls to see if it needs to a get a reference on the associated pgmap data structure to ensure the pages won't go away. However it's a driver responsibility to ensure that if pages are mapped (ie. discoverable by GUP) that they are not offlined or removed from the memmap so there is no need to hold a reference on the pgmap data structure to ensure this.
Furthermore mappings with PFN_DEV are no longer created, hence this effectively dead code anyway so can be removed.
Stable-dep-of: 79cb51d538ab ("mm/gup: recheck the PUD under PTL in follow_pud_mask()")
--- include/linux/huge_mm.h | 3 - mm/gup.c | 160 ++-------------------------------------- mm/huge_memory.c | 40 ---------- 3 files changed, 5 insertions(+), 198 deletions(-)
include/linux/huge_mm.h +0 -3
@@ -441,9 +441,6 @@ static inline bool folio_test_pmd_mappable(struct folio *folio)return folio_order (folio) >=HPAGE_PMD_ORDER ; } -struct page *follow_devmap_pmd (struct vm_area_struct *vma,unsigned long addr, -pmd_t *pmd,int flags,struct dev_pagemap **pgmap); -vm_fault_t do_huge_pmd_numa_page (struct vm_fault *vmf);extern struct folio *huge_zero_folio;
mm/gup.c +5 -155
216 lines not on this page · raw message
mm/huge_memory.c +0 -40
@@ -1575,46 +1575,6 @@ void touch_pmd(struct vm_area_struct *vma, unsigned long addr,update_mmu_cache_pmd (vma, addr, pmd); } -struct page *follow_devmap_pmd (struct vm_area_struct *vma,unsigned long addr, -pmd_t *pmd,int flags,struct dev_pagemap **pgmap) -{ -unsigned long pfn =pmd_pfn (*pmd); -struct mm_struct *mm = vma->vm_mm; -struct page *page; -int ret; - -assert_spin_locked (pmd_lockptr (mm, pmd)); - -if (flags &FOLL_WRITE && !pmd_write (*pmd)) -return NULL ; - -if (pmd_present (*pmd) &&pmd_devmap (*pmd)) -/* pass */ ; -else -return NULL ; - -if (flags &FOLL_TOUCH ) -touch_pmd (vma, addr, pmd, flags &FOLL_WRITE ); - -/* -* device mapped pages can only be returned if the -* caller will manage the page reference count. -*/ -if (!(flags & (FOLL_GET |FOLL_PIN ))) -return ERR_PTR (-EEXIST ); - - pfn += (addr & ~PMD_MASK ) >>PAGE_SHIFT ; - *pgmap =get_dev_pagemap (pfn, *pgmap); -if (!*pgmap) -return ERR_PTR (-EFAULT ); - page =pfn_to_page (pfn); - ret =try_grab_folio (page_folio (page),1 , flags); -if (ret) - page =ERR_PTR (ret); - -return page; -} -int copy_huge_pmd (struct mm_struct *dst_mm,struct mm_struct *src_mm,pmd_t *dst_pmd,pmd_t *src_pmd,unsigned long addr,struct vm_area_struct *dst_vma,struct vm_area_struct *src_vma)
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 3/4 mm/gup: remove dead pgmap refcounting code
In reply to Sasha LevinFrom: Alistair Popple <[email protected]>
[ Upstream commit d3f7922b929a92873e14f7c61a026a6b576e8773 ]
Prior to commit aed877c2b425 ("device/dax: properly refcount device dax pages when mapping") ZONE_DEVICE pages were not fully reference counted when mapped into user page tables. Instead GUP would take a reference on the associated pgmap to ensure the results of pfn_to_page() remained valid.
This is no longer required and most of the code was removed by commit fd2825b0760a ("mm/gup: remove pXX_devmap usage from get_user_pages()"). Finish cleaning this up by removing the dead calls to put_dev_pagemap() and the temporary context struct.
Stable-dep-of: 79cb51d538ab ("mm/gup: recheck the PUD under PTL in follow_pud_mask()")
--- mm/gup.c | 67 ++++++++++++++++++++++---------------------------------- 1 file changed, 26 insertions(+), 41 deletions(-)
mm/gup.c +26 -41
@@ -27,11 +27,6 @@#include "internal.h" -struct follow_page_context { -struct dev_pagemap *pgmap; -unsigned int page_mask; -}; -static inline void sanity_check_pinned_pages (struct page **pages,unsigned long npages) {
@@ -624,7 +619,7 @@ static struct page *no_page_table(struct vm_area_struct *vma,#ifdef CONFIG_PGTABLE_HAS_HUGE_LEAVES static struct page *follow_huge_pud (struct vm_area_struct *vma,unsigned long addr,pud_t *pudp, -int flags,struct follow_page_context *ctx) +int flags,unsigned long *page_mask) {struct mm_struct *mm = vma->vm_mm;struct page *page;
@@ -650,7 +645,7 @@ static struct page *follow_huge_pud(struct vm_area_struct *vma,if (ret) page =ERR_PTR (ret);else - ctx->page_mask =HPAGE_PUD_NR -1 ; + *page_mask =HPAGE_PUD_NR -1 ;return page; }
@@ -696,7 +691,7 @@ static inline bool can_follow_write_pmd(pmd_t pmd, struct page *page,static struct page *follow_huge_pmd (struct vm_area_struct *vma,unsigned long addr,pmd_t *pmd,unsigned int flags, -struct follow_page_context *ctx) +unsigned long *page_mask) {struct mm_struct *mm = vma->vm_mm;pmd_t pmdval = *pmd;
@@ -733,7 +728,7 @@ static struct page *follow_huge_pmd(struct vm_area_struct *vma,#endif /* CONFIG_TRANSPARENT_HUGEPAGE */ page += (addr & ~HPAGE_PMD_MASK ) >>PAGE_SHIFT ; - ctx->page_mask =HPAGE_PMD_NR -1 ; + *page_mask =HPAGE_PMD_NR -1 ;return page; }
@@ -741,7 +736,7 @@ static struct page *follow_huge_pmd(struct vm_area_struct *vma,#else /* CONFIG_PGTABLE_HAS_HUGE_LEAVES */ static struct page *follow_huge_pud (struct vm_area_struct *vma,unsigned long addr,pud_t *pudp, -int flags,struct follow_page_context *ctx) +int flags,unsigned long *page_mask) {return NULL ; }
@@ -749,7 +744,7 @@ static struct page *follow_huge_pud(struct vm_area_struct *vma,static struct page *follow_huge_pmd (struct vm_area_struct *vma,unsigned long addr,pmd_t *pmd,unsigned int flags, -struct follow_page_context *ctx) +unsigned long *page_mask) {return NULL ; }
@@ -815,8 +810,7 @@ static inline bool can_follow_write_pte(pte_t pte, struct page *page, }static struct page *follow_page_pte (struct vm_area_struct *vma, -unsigned long address,pmd_t *pmd,unsigned int flags, -struct dev_pagemap **pgmap) +unsigned long address,pmd_t *pmd,unsigned int flags) {struct mm_struct *mm = vma->vm_mm;struct folio *folio;
@@ -919,7 +913,7 @@ static struct page *follow_page_pte(struct vm_area_struct *vma,static struct page *follow_pmd_mask (struct vm_area_struct *vma,unsigned long address,pud_t *pudp,unsigned int flags, -struct follow_page_context *ctx) +unsigned long *page_mask) {pmd_t *pmd, pmdval;spinlock_t *ptl;
@@ -933,7 +927,7 @@ static struct page *follow_pmd_mask(struct vm_area_struct *vma,if (!pmd_present (pmdval))return no_page_table (vma, flags, address);if (likely (!pmd_leaf (pmdval))) -return follow_page_pte (vma, address, pmd, flags, &ctx->pgmap); +return follow_page_pte (vma, address, pmd, flags);if (pmd_protnone (pmdval) && !gup_can_follow_protnone (vma, flags))return no_page_table (vma, flags, address);
@@ -946,16 +940,16 @@ static struct page *follow_pmd_mask(struct vm_area_struct *vma, }if (unlikely (!pmd_leaf (pmdval))) {spin_unlock (ptl); -return follow_page_pte (vma, address, pmd, flags, &ctx->pgmap); +return follow_page_pte (vma, address, pmd, flags); }if (pmd_trans_huge (pmdval) && (flags &FOLL_SPLIT_PMD )) {spin_unlock (ptl);split_huge_pmd (vma, pmd, address);/* If pmd was left empty, stuff a page table in there quickly */ return pte_alloc (mm, pmd) ?ERR_PTR (-ENOMEM ) : -follow_page_pte (vma, address, pmd, flags, &ctx->pgmap); +follow_page_pte (vma, address, pmd, flags); } - page =follow_huge_pmd (vma, address, pmd, flags, ctx); + page =follow_huge_pmd (vma, address, pmd, flags, page_mask);spin_unlock (ptl);return page; }
@@ -963,7 +957,7 @@ static struct page *follow_pmd_mask(struct vm_area_struct *vma,static struct page *follow_pud_mask (struct vm_area_struct *vma,unsigned long address,p4d_t *p4dp,unsigned int flags, -struct follow_page_context *ctx) +unsigned long *page_mask) {pud_t *pudp, pud;spinlock_t *ptl;
@@ -976,7 +970,7 @@ static struct page *follow_pud_mask(struct vm_area_struct *vma,return no_page_table (vma, flags, address);if (pud_leaf (pud)) { ptl =pud_lock (mm, pudp); - page =follow_huge_pud (vma, address, pudp, flags, ctx); + page =follow_huge_pud (vma, address, pudp, flags, page_mask);spin_unlock (ptl);if (page)return page;
@@ -985,13 +979,13 @@ static struct page *follow_pud_mask(struct vm_area_struct *vma,if (unlikely (pud_bad (pud)))return no_page_table (vma, flags, address); -return follow_pmd_mask (vma, address, pudp, flags, ctx); +return follow_pmd_mask (vma, address, pudp, flags, page_mask); }static struct page *follow_p4d_mask (struct vm_area_struct *vma,unsigned long address,pgd_t *pgdp,unsigned int flags, -struct follow_page_context *ctx) +unsigned long *page_mask) {p4d_t *p4dp, p4d;
@@ -1002,7 +996,7 @@ static struct page *follow_p4d_mask(struct vm_area_struct *vma,if (!p4d_present (p4d) ||p4d_bad (p4d))return no_page_table (vma, flags, address); -return follow_pud_mask (vma, address, p4dp, flags, ctx); +return follow_pud_mask (vma, address, p4dp, flags, page_mask); }/**
@@ -1010,20 +1004,16 @@ static struct page *follow_p4d_mask(struct vm_area_struct *vma,* @vma: vm_area_struct mapping @address * @address: virtual address to look up * @flags: flags modifying lookup behaviour -* @ctx: contains dev_pagemap for %ZONE_DEVICE memory pinning and a -* pointer to output page_mask +* @page_mask: a pointer to output page_mask * * @flags can have FOLL_ flags set, defined in <linux/mm.h> * -* When getting pages from ZONE_DEVICE memory, the @ctx->pgmap caches -* the device's dev_pagemap metadata to avoid repeating expensive lookups. -* * When getting an anonymous page and the caller has to trigger unsharing * of a shared anonymous page first, -EMLINK is returned. The caller should * trigger a fault with FAULT_FLAG_UNSHARE set. Note that unsharing is only * relevant with FOLL_PIN and !FOLL_WRITE. * -* On output, the @ctx->page_mask is set according to the size of the page. +* On output, @page_mask is set according to the size of the page. * * Return: the mapped (struct page *), %NULL if no mapping exists, or * an error pointer if there is a mapping to something not represented
@@ -1031,7 +1021,7 @@ static struct page *follow_p4d_mask(struct vm_area_struct *vma,*/ static struct page *follow_page_mask (struct vm_area_struct *vma,unsigned long address,unsigned int flags, -struct follow_page_context *ctx) +unsigned long *page_mask) {pgd_t *pgd;struct mm_struct *mm = vma->vm_mm;
@@ -1039,13 +1029,13 @@ static struct page *follow_page_mask(struct vm_area_struct *vma,vma_pgtable_walk_begin (vma); - ctx->page_mask =0 ; + *page_mask =0 ; pgd =pgd_offset (mm, address);if (pgd_none (*pgd) ||unlikely (pgd_bad (*pgd))) page =no_page_table (vma, flags, address);else - page =follow_p4d_mask (vma, address, pgd, flags, ctx); + page =follow_p4d_mask (vma, address, pgd, flags, page_mask);vma_pgtable_walk_end (vma);
@@ -1389,7 +1379,7 @@ static long __get_user_pages(struct mm_struct *mm, {long ret =0 , i =0 ;struct vm_area_struct *vma =NULL ; -struct follow_page_context ctx = {NULL }; +unsigned long page_mask =0 ;if (!nr_pages)return 0 ;
@@ -1427,7 +1417,7 @@ static long __get_user_pages(struct mm_struct *mm, pages ? &page : NULL);if (ret) goto out; - ctx.page_mask =0 ; + page_mask =0 ; goto next_page; }
@@ -1450,7 +1440,7 @@ static long __get_user_pages(struct mm_struct *mm, }cond_resched (); - page =follow_page_mask (vma, start, gup_flags, &ctx); + page =follow_page_mask (vma, start, gup_flags, &page_mask);if (!page ||PTR_ERR (page) == -EMLINK ) { ret =faultin_page (vma, start, gup_flags,PTR_ERR (page) == -EMLINK , locked);
@@ -1483,7 +1473,7 @@ static long __get_user_pages(struct mm_struct *mm, goto out; } next_page: - page_increm =1 + (~(start >>PAGE_SHIFT ) & ctx.page_mask); + page_increm =1 + (~(start >>PAGE_SHIFT ) & page_mask);if (page_increm > nr_pages) page_increm = nr_pages;
@@ -1533,8 +1523,6 @@ static long __get_user_pages(struct mm_struct *mm, nr_pages -= page_increm; }while (nr_pages); out: -if (ctx.pgmap) -put_dev_pagemap (ctx.pgmap);return i ? i : ret; }
@@ -2894,7 +2882,6 @@ static int gup_fast_pte_range(pmd_t pmd, pmd_t *pmdp, unsigned long addr, unsigned long end, unsigned int flags, struct page **pages, int *nr) { - struct dev_pagemap *pgmap = NULL; int ret = 0;pte_t *ptep, *ptem;
@@ -2966,8 +2953,6 @@ static int gup_fast_pte_range(pmd_t pmd, pmd_t *pmdp, unsigned long addr, ret =1 ; pte_unmap: -if (pgmap) -put_dev_pagemap (pgmap);pte_unmap (ptem);return ret; }
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 4/4 mm/gup: recheck the PUD under PTL in follow_pud_mask()
In reply to Sasha LevinFrom: "David Hildenbrand (Arm)" <[email protected]>
[ Upstream commit 79cb51d538abc459f74e046d27797d36b133ce0e ]
follow_pud_mask() reads the PUD without a lock. If it is a leaf, it takes the lock and calls follow_huge_pud(), which reads the entry again, but only tests whether it is present.
A leaf can be zapped concurrently and a page fault can fill the empty entry with a page table.
This currently only applies when device-dax makes use of PUD entries, and the fault after e.g., MADV_DONTNEED would find THPs to be disabled.
That requires a sequence like
1) Fault in the PUD
2) Disable THP
3) Have GUP race against concurrent MADV_DONTNEED + refault
Which is rather unlikely to happen in practice, which is why we never received a report about such a problem.
Before commit 1b1676180246 ("mm/gup: handle huge pud for follow_pud_mask()"), we performed that check in follow_devmap_pud() by testing for pud_devmap().
Let's fix it by just structuring follow_pud_mask() similar to follow_pmd_mask(), minus protnone stuff that does not apply and minus FOLL_SPLIT_PMD handling. That makes it much easier to spot any such inconsistencies.
Perform the pud_bad() check early in follow_pmd_mask() and just forward the PUD value. We can drop the pud_present() check in follow_huge_pud(), just like we have in follow_huge_pmd().
Even though we are performing more changes than strictly necessary, the fix should be easy to backport.
--- mm/gup.c | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-)
mm/gup.c +18 -14
@@ -632,9 +632,6 @@ static struct page *follow_huge_pud(struct vm_area_struct *vma,if ((flags &FOLL_WRITE ) && !pud_write (pud))return NULL ; -if (!pud_present (pud)) -return NULL ; - pfn += (addr & ~PUD_MASK ) >>PAGE_SHIFT ; page =pfn_to_page (pfn);
@@ -911,15 +908,17 @@ static struct page *follow_page_pte(struct vm_area_struct *vma, }static struct page *follow_pmd_mask (struct vm_area_struct *vma, -unsigned long address,pud_t *pudp, -unsigned int flags, -unsigned long *page_mask) +unsigned long address,pud_t *pudp,pud_t pud, +unsigned int flags,unsigned long *page_mask) {pmd_t *pmd, pmdval;spinlock_t *ptl;struct page *page;struct mm_struct *mm = vma->vm_mm; +if (unlikely (pud_bad (pud))) +return no_page_table (vma, flags, address); + pmd =pmd_offset (pudp, address); pmdval =pmdp_get_lockless (pmd);if (pmd_none (pmdval))
@@ -968,18 +967,23 @@ static struct page *follow_pud_mask(struct vm_area_struct *vma, pud =pudp_get (pudp);if (!pud_present (pud))return no_page_table (vma, flags, address); -if (pud_leaf (pud)) { - ptl =pud_lock (mm, pudp); - page =follow_huge_pud (vma, address, pudp, flags, page_mask); +if (!pud_leaf (pud)) +return follow_pmd_mask (vma, address, pudp, pud, flags, page_mask); + + ptl =pud_lock (mm, pudp); + pud = *pudp; +if (unlikely (!pud_present (pud))) {spin_unlock (ptl); -if (page) -return page;return no_page_table (vma, flags, address); } -if (unlikely (pud_bad (pud))) -return no_page_table (vma, flags, address); +if (unlikely (!pud_leaf (pud))) { +spin_unlock (ptl); +return follow_pmd_mask (vma, address, pudp, pud, flags, page_mask); + } -return follow_pmd_mask (vma, address, pudp, flags, page_mask); + page =follow_huge_pud (vma, address, pudp, flags, page_mask); +spin_unlock (ptl); +return page; } static struct page *follow_p4d_mask(struct vm_area_struct *vma,
Signature · 1 line
2.53.0