Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

mm/huge_memory: don't mark refcounted folios special in vmf_insert_folio_pud()

Marking PUDs that map a "normal" refcounted folios as special is against
our rules documented for vm_normal_page(). normal (refcounted) folios
shall never have the page table mapping marked as special.

Fortunately, there are not that many pud_special() check that can be
mislead and are right now rather harmless: e.g., none so far bases
decisions whether to grab a folio reference on that decision.

Well, and GUP-fast will fallback to GUP-slow. All in all, so far no big
implications as it seems.

Getting this right will get more important as we introduce
folio_normal_page_pud() and start using it in more place where we
currently special-case based on other VMA flags.

Fix it just like we fixed vmf_insert_folio_pmd().

Add folio_mk_pud() to mimic what we do with folio_mk_pmd().

Link: https://lkml.kernel.org/r/20250613092702.1943533-4-david@redhat.com
Fixes: dbe54153296d ("mm/huge_memory: add vmf_insert_folio_pud()")
Signed-off-by: David Hildenbrand <david@redhat.com>
Reviewed-by: Dan Williams <dan.j.williams@intel.com>
Reviewed-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
Reviewed-by: Jason Gunthorpe <jgg@nvidia.com>
Tested-by: Dan Williams <dan.j.williams@intel.com>
Reviewed-by: Oscar Salvador <osalvador@suse.de>
Cc: Alistair Popple <apopple@nvidia.com>
Cc: Baolin Wang <baolin.wang@linux.alibaba.com>
Cc: Dev Jain <dev.jain@arm.com>
Cc: Liam Howlett <liam.howlett@oracle.com>
Cc: Mariano Pache <npache@redhat.com>
Cc: Michal Hocko <mhocko@suse.com>
Cc: Mike Rapoport <rppt@kernel.org>
Cc: Ryan Roberts <ryan.roberts@arm.com>
Cc: Suren Baghdasaryan <surenb@google.com>
Cc: Vlastimil Babka <vbabka@suse.cz>
Cc: Zi Yan <ziy@nvidia.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

authored by

David Hildenbrand and committed by
Andrew Morton
02825c09 c4297465

+47 -24
+18 -1
include/linux/mm.h
··· 1816 1816 { 1817 1817 return pmd_mkhuge(pfn_pmd(folio_pfn(folio), pgprot)); 1818 1818 } 1819 - #endif 1819 + 1820 + #ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD 1821 + /** 1822 + * folio_mk_pud - Create a PUD for this folio 1823 + * @folio: The folio to create a PUD for 1824 + * @pgprot: The page protection bits to use 1825 + * 1826 + * Create a page table entry for the first page of this folio. 1827 + * This is suitable for passing to set_pud_at(). 1828 + * 1829 + * Return: A page table entry suitable for mapping this folio. 1830 + */ 1831 + static inline pud_t folio_mk_pud(struct folio *folio, pgprot_t pgprot) 1832 + { 1833 + return pud_mkhuge(pfn_pud(folio_pfn(folio), pgprot)); 1834 + } 1835 + #endif /* CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */ 1836 + #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ 1820 1837 #endif /* CONFIG_MMU */ 1821 1838 1822 1839 static inline bool folio_has_pincount(const struct folio *folio)
+29 -23
mm/huge_memory.c
··· 1536 1536 return pud; 1537 1537 } 1538 1538 1539 - static void insert_pfn_pud(struct vm_area_struct *vma, unsigned long addr, 1540 - pud_t *pud, pfn_t pfn, pgprot_t prot, bool write) 1539 + static void insert_pud(struct vm_area_struct *vma, unsigned long addr, 1540 + pud_t *pud, struct folio_or_pfn fop, pgprot_t prot, bool write) 1541 1541 { 1542 1542 struct mm_struct *mm = vma->vm_mm; 1543 1543 pud_t entry; 1544 1544 1545 1545 if (!pud_none(*pud)) { 1546 + const unsigned long pfn = fop.is_folio ? folio_pfn(fop.folio) : 1547 + pfn_t_to_pfn(fop.pfn); 1548 + 1546 1549 if (write) { 1547 - if (WARN_ON_ONCE(pud_pfn(*pud) != pfn_t_to_pfn(pfn))) 1550 + if (WARN_ON_ONCE(pud_pfn(*pud) != pfn)) 1548 1551 return; 1549 1552 entry = pud_mkyoung(*pud); 1550 1553 entry = maybe_pud_mkwrite(pud_mkdirty(entry), vma); ··· 1557 1554 return; 1558 1555 } 1559 1556 1560 - entry = pud_mkhuge(pfn_t_pud(pfn, prot)); 1561 - if (pfn_t_devmap(pfn)) 1562 - entry = pud_mkdevmap(entry); 1563 - else 1564 - entry = pud_mkspecial(entry); 1557 + if (fop.is_folio) { 1558 + entry = folio_mk_pud(fop.folio, vma->vm_page_prot); 1559 + 1560 + folio_get(fop.folio); 1561 + folio_add_file_rmap_pud(fop.folio, &fop.folio->page, vma); 1562 + add_mm_counter(mm, mm_counter_file(fop.folio), HPAGE_PUD_NR); 1563 + } else { 1564 + entry = pud_mkhuge(pfn_t_pud(fop.pfn, prot)); 1565 + 1566 + if (pfn_t_devmap(fop.pfn)) 1567 + entry = pud_mkdevmap(entry); 1568 + else 1569 + entry = pud_mkspecial(entry); 1570 + } 1565 1571 if (write) { 1566 1572 entry = pud_mkyoung(pud_mkdirty(entry)); 1567 1573 entry = maybe_pud_mkwrite(entry, vma); ··· 1594 1582 unsigned long addr = vmf->address & PUD_MASK; 1595 1583 struct vm_area_struct *vma = vmf->vma; 1596 1584 pgprot_t pgprot = vma->vm_page_prot; 1585 + struct folio_or_pfn fop = { 1586 + .pfn = pfn, 1587 + }; 1597 1588 spinlock_t *ptl; 1598 1589 1599 1590 /* ··· 1616 1601 pfnmap_setup_cachemode_pfn(pfn_t_to_pfn(pfn), &pgprot); 1617 1602 1618 1603 ptl = pud_lock(vma->vm_mm, vmf->pud); 1619 - insert_pfn_pud(vma, addr, vmf->pud, pfn, pgprot, write); 1604 + insert_pud(vma, addr, vmf->pud, fop, pgprot, write); 1620 1605 spin_unlock(ptl); 1621 1606 1622 1607 return VM_FAULT_NOPAGE; ··· 1638 1623 unsigned long addr = vmf->address & PUD_MASK; 1639 1624 pud_t *pud = vmf->pud; 1640 1625 struct mm_struct *mm = vma->vm_mm; 1626 + struct folio_or_pfn fop = { 1627 + .folio = folio, 1628 + .is_folio = true, 1629 + }; 1641 1630 spinlock_t *ptl; 1642 1631 1643 1632 if (addr < vma->vm_start || addr >= vma->vm_end) ··· 1651 1632 return VM_FAULT_SIGBUS; 1652 1633 1653 1634 ptl = pud_lock(mm, pud); 1654 - 1655 - /* 1656 - * If there is already an entry present we assume the folio is 1657 - * already mapped, hence no need to take another reference. We 1658 - * still call insert_pfn_pud() though in case the mapping needs 1659 - * upgrading to writeable. 1660 - */ 1661 - if (pud_none(*vmf->pud)) { 1662 - folio_get(folio); 1663 - folio_add_file_rmap_pud(folio, &folio->page, vma); 1664 - add_mm_counter(mm, mm_counter_file(folio), HPAGE_PUD_NR); 1665 - } 1666 - insert_pfn_pud(vma, addr, vmf->pud, pfn_to_pfn_t(folio_pfn(folio)), 1667 - vma->vm_page_prot, write); 1635 + insert_pud(vma, addr, vmf->pud, fop, vma->vm_page_prot, write); 1668 1636 spin_unlock(ptl); 1669 1637 1670 1638 return VM_FAULT_NOPAGE;