Sharing hugetlb PMD tables can leave HugetlbPages in /proc//status too low or too high, or make it underflow: when (un)mapping a folio in a shared PMD table, we only adjust mm->hugetlb_usage of the MM we process, not of the other sharers. Accounting this precisely is not worth it for a statistic, so let's just give up: once an MM shares a PMD table, set mm->hugetlb_usage of both MMs to LONG_MIN / 2, and report negative values as 0. Later updates cannot realistically make it positive again, as PMD table sharing is 64-bit only. Document that HugetlbPages is 0 once a process shared hugetlb PMD page tables. This has low urgency, as it only affects a statistic. 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) --- Documentation/filesystems/proc.rst | 3 ++- mm/hugetlb.c | 9 ++++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/Documentation/filesystems/proc.rst b/Documentation/filesystems/proc.rst index bcbcb41814d96..8696359b06498 100644 --- a/Documentation/filesystems/proc.rst +++ b/Documentation/filesystems/proc.rst @@ -268,7 +268,8 @@ It's slow but very precise. VmPTE size of page table entries VmSwap amount of swap used by anonymous private data (shmem swap usage is not included) - HugetlbPages size of hugetlb memory portions + HugetlbPages size of hugetlb memory portions (0 once the + process shared hugetlb PMD page tables) CoreDumping process's memory is currently being dumped (killing the process may lead to a corrupted core) THP_enabled process is allowed to use THP (returns 0 when diff --git a/mm/hugetlb.c b/mm/hugetlb.c index c1fbe66d30332..9c85fc5790f3a 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -4766,8 +4766,9 @@ void hugetlb_show_meminfo_node(int nid) void hugetlb_report_usage(struct seq_file *m, struct mm_struct *mm) { + /* Negative once the MM shared PMD tables, see huge_pmd_share(). */ seq_printf(m, "HugetlbPages:\t%8lu kB\n", - K(atomic_long_read(&mm->hugetlb_usage))); + K(max(atomic_long_read(&mm->hugetlb_usage), 0L))); } /* Return the number pages of memory we physically have, in PAGE_SIZE units. */ @@ -7349,6 +7350,12 @@ pte_t *huge_pmd_share(struct mm_struct *mm, struct vm_area_struct *vma, (pmd_t *)((unsigned long)spte & PAGE_MASK)); hugetlbfs_pmd_sharing_inc(file_inode(vma->vm_file)); mm_inc_nr_pmds(mm); + /* + * (Un)mapping folios with shared PMD tables would require + * updating statistics of all MMs. Instead, give up accounting. + */ + atomic_long_set(&mm->hugetlb_usage, LONG_MIN / 2); + atomic_long_set(&svma->vm_mm->hugetlb_usage, LONG_MIN / 2); } else { ptdesc_pmd_pts_dec(virt_to_ptdesc(spte)); } -- 2.56.1