hugetlb_pmd_shared() reads "pt_share_count" from the ptdesc of the page table that maps a hugetlb folio, independent of the folio size, to tell whether that page table is shared. Only PMD tables have a valid "pt_share_count"; for other folio sizes, such as 1 GiB folios on x86 that are mapped by a PUD table, we read whatever another user of the union left behind. As a result, such folios might be reported as shared in smaps and as not exclusively mapped in pagemap, and mbind(MPOL_MF_MOVE) and migrate_pages() might skip them. Let's fix it by adding hugetlb_folio_maybe_mapped_shared(), which combines folio_maybe_mapped_shared(), documented to not care about page table sharing, with a check for PMD table sharing only for PMD-sized folios, and using it in the affected callers. This has low urgency. Fixes: ca1a47cd3f5f ("mm/hugetlb: fix hugetlb_pmd_shared()") Cc: Reported-by: Chris Mason Signed-off-by: David Hildenbrand (Arm) --- fs/proc/task_mmu.c | 6 ++---- include/linux/hugetlb.h | 16 ++++++++++++++++ mm/mempolicy.c | 2 +- 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c index d2a524c6ad4b9..41b280ab7b19d 100644 --- a/fs/proc/task_mmu.c +++ b/fs/proc/task_mmu.c @@ -1211,8 +1211,7 @@ static int smaps_hugetlb_range(pte_t *pte, unsigned long hmask, if (folio) { /* We treat non-present entries as "maybe shared". */ - if (!present || folio_maybe_mapped_shared(folio) || - hugetlb_pmd_shared(pte)) + if (!present || hugetlb_folio_maybe_mapped_shared(folio, pte)) mss->shared_hugetlb += huge_page_size(hstate_vma(vma)); else mss->private_hugetlb += huge_page_size(hstate_vma(vma)); @@ -2131,8 +2130,7 @@ static int pagemap_hugetlb_range(pte_t *ptep, unsigned long hmask, if (!folio_test_anon(folio)) flags |= PM_FILE; - if (!folio_maybe_mapped_shared(folio) && - !hugetlb_pmd_shared(ptep)) + if (!hugetlb_folio_maybe_mapped_shared(folio, ptep)) flags |= PM_MMAP_EXCLUSIVE; if (huge_pte_uffd(pte)) diff --git a/include/linux/hugetlb.h b/include/linux/hugetlb.h index 0d5414392d870..49dad5ae57512 100644 --- a/include/linux/hugetlb.h +++ b/include/linux/hugetlb.h @@ -1386,6 +1386,22 @@ static inline bool hugetlb_pmd_shared(pte_t *pte) } #endif +#ifdef CONFIG_HUGETLB_PAGE +static inline bool hugetlb_folio_maybe_mapped_shared(struct folio *folio, + pte_t *ptep) +{ + if (folio_maybe_mapped_shared(folio)) + return true; + return folio_size(folio) == PMD_SIZE && hugetlb_pmd_shared(ptep); +} +#else /* !CONFIG_HUGETLB_PAGE */ +static inline bool hugetlb_folio_maybe_mapped_shared(struct folio *folio, + pte_t *ptep) +{ + return true; +} +#endif /* CONFIG_HUGETLB_PAGE */ + bool want_pmd_share(struct vm_area_struct *vma, unsigned long addr); #ifndef __HAVE_ARCH_FLUSH_HUGETLB_TLB_RANGE diff --git a/mm/mempolicy.c b/mm/mempolicy.c index 6f800fb961aec..229efcdb2c7db 100644 --- a/mm/mempolicy.c +++ b/mm/mempolicy.c @@ -833,7 +833,7 @@ static int queue_folios_hugetlb(pte_t *pte, unsigned long hmask, * cannot easily detect if a folio is shared. */ if ((flags & MPOL_MF_MOVE_ALL) || - (!folio_maybe_mapped_shared(folio) && !hugetlb_pmd_shared(pte))) + !hugetlb_folio_maybe_mapped_shared(folio, pte)) if (!folio_isolate_hugetlb(folio, qp->pagelist)) qp->nr_failed++; unlock: -- 2.56.1