HugetlbPages in /proc//status can be wrong, and even drop below zero, if we fork() or unmap while mapped hugetlb folios are being migrated. There is an inconsistency: when installing migration entries we don't adjust the counter, treating them as if they were present. However, when copying or unmapping a migration entry, we don't adjust the counter like we would for a present entry. Let's fix it by adjusting the counter also for migration entries in these cases. Add pte_is_migration_entry() like we did for other softleaf types already. This has low urgency and given that this has been around forever and likely nobody cares, let's not CC stable. Fixes: 5d317b2b6536 ("mm: hugetlb: proc: add HugetlbPages field to /proc/PID/status") Reported-by: Chris Mason Signed-off-by: David Hildenbrand (Arm) --- include/linux/leafops.h | 11 +++++++++++ mm/hugetlb.c | 3 +++ 2 files changed, 14 insertions(+) diff --git a/include/linux/leafops.h b/include/linux/leafops.h index 4c1476ae32343..c315a3531485e 100644 --- a/include/linux/leafops.h +++ b/include/linux/leafops.h @@ -587,6 +587,17 @@ static inline bool pte_is_uffd_marker(pte_t pte) return false; } +/** + * pte_is_migration_entry() - Does this PTE entry encode a migration entry? + * @pte: PTE entry. + * + * Returns: true if the PTE encodes a migration entry, otherwise false. + */ +static inline bool pte_is_migration_entry(pte_t pte) +{ + return softleaf_is_migration(softleaf_from_pte(pte)); +} + #if defined(CONFIG_ZONE_DEVICE) && defined(CONFIG_ARCH_HAS_PMD_SOFTLEAVES) /** diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 2bba0940a40d5..89d71e3dadd77 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -5131,6 +5131,7 @@ int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src, if (!userfaultfd_protected(dst_vma)) entry = pte_swp_clear_uffd(entry); set_huge_pte_at(dst, addr, dst_pte, entry, sz); + hugetlb_count_add(npages, dst); } else if (unlikely(pte_is_marker(entry))) { const pte_marker marker = copy_pte_marker(softleaf, dst_vma); @@ -5480,6 +5481,8 @@ static void hugetlb_unmap_range(struct mmu_gather *tlb, spin_unlock(ptl); continue; } + if (pte_is_migration_entry(pte)) + hugetlb_count_sub(pages_per_huge_page(h), mm); /* * If the pte was wr-protected by uffd-wp in any of the * swap forms, meanwhile the caller does not want to -- 2.56.1