From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 85625337BAC for ; Sat, 31 Jan 2026 21:15:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769894140; cv=none; b=ejsbtuXY/7rfRxuIyNXA4FbkIIMgpbvIYUkMRHxoBjjZ7LRJ4y9Z8GUrdmHS8yVThmBzGfDcKyWt7vy2EM7LEIuWmNqZrXVKqiatcL5EWnyozVbnn3Lk32IvwH90ukJY39G5FHFr+KFHADTyi2Eiqja+dRP4Lz0R8DssZBNdq8A= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769894140; c=relaxed/simple; bh=xSPIV9VQoWWm5hnIAlnRVPlhHCavHXNgZCSmhywRVe8=; h=Date:To:From:Subject:Message-Id; b=ZLJ9vHRZIr4KJ+gdDQ9YnQ+/RFyn85lbotW7wkuyYhu7oUH6kdqH0IfOpVPY6Y57zCdSWiDlRiT5wTbNu0VvkxWzHyPFIfjwSlFKUtCF0fWwliwzzLYnn1Dh76/3SbWTsPF6nfVYpuHcV/Y8qPZsgWCY6mhw79cFQc1bGf4oaYE= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux-foundation.org header.i=@linux-foundation.org header.b=z8xe3JL5; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux-foundation.org header.i=@linux-foundation.org header.b="z8xe3JL5" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2131BC4CEF1; Sat, 31 Jan 2026 21:15:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linux-foundation.org; s=korg; t=1769894140; bh=xSPIV9VQoWWm5hnIAlnRVPlhHCavHXNgZCSmhywRVe8=; h=Date:To:From:Subject:From; b=z8xe3JL5BJbDl4i5Bw6Irxum3wM34hhTCprmrj0lGtDUQhSti1Bqge82JQlCKc7UP SFOm960U4D6h5CW+3Ax0XHzmkhfhmJbHQXvaCnnBqvVprQkxC551tJw0lkihmHX2Cd 8dZ1772nPXknyxcl9zMN0OG2ulYLcVODouxqKt9M= Date: Sat, 31 Jan 2026 13:15:39 -0800 To: mm-commits@vger.kernel.org,ziy@nvidia.com,willy@infradead.org,usamaarif642@gmail.com,songliubraving@fb.com,ryan.roberts@arm.com,riel@surriel.com,npache@redhat.com,lorenzo.stoakes@oracle.com,liam.howlett@oracle.com,lance.yang@linux.dev,kas@kernel.org,hannes@cmpxchg.org,dev.jain@arm.com,david@kernel.org,baolin.wang@linux.alibaba.com,baohua@kernel.org,shakeel.butt@linux.dev,akpm@linux-foundation.org From: Andrew Morton Subject: + mm-khugepaged-fix-nr_file_pages-and-nr_shmem-in-collapse_file.patch added to mm-unstable branch Message-Id: <20260131211540.2131BC4CEF1@smtp.kernel.org> Precedence: bulk X-Mailing-List: mm-commits@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: The patch titled Subject: mm: khugepaged: fix NR_FILE_PAGES and NR_SHMEM in collapse_file() has been added to the -mm mm-unstable branch. Its filename is mm-khugepaged-fix-nr_file_pages-and-nr_shmem-in-collapse_file.patch This patch will shortly appear at https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/mm-khugepaged-fix-nr_file_pages-and-nr_shmem-in-collapse_file.patch This patch will later appear in the mm-unstable branch at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm Before you just go and hit "reply", please: a) Consider who else should be cc'ed b) Prefer to cc a suitable mailing list as well c) Ideally: find the original patch on the mailing list and do a reply-to-all to that, adding suitable additional cc's *** Remember to use Documentation/process/submit-checklist.rst when testing your code *** The -mm tree is included into linux-next via various branches at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm and is updated there most days ------------------------------------------------------ From: Shakeel Butt Subject: mm: khugepaged: fix NR_FILE_PAGES and NR_SHMEM in collapse_file() Date: Thu, 29 Jan 2026 20:29:25 -0800 In META's fleet, we observed high-level cgroups showing zero file memcg stats while their descendants had non-zero values. Investigation using drgn revealed that these parent cgroups actually had negative file stats, aggregated from their children. This issue became more frequent after deploying thp-always more widely, pointing to a correlation with THP file collapsing. The root cause is that collapse_file() assumes old folios and the new THP belong to the same node and memcg. When this assumption breaks, stats become skewed. The bug affects not just memcg stats but also per-numa stats, and not just NR_FILE_PAGES but also NR_SHMEM. The assumption breaks in scenarios such as: 1. Small folios allocated on one node while the THP gets allocated on a different node. 2. A package downloader running in one cgroup populates the page cache, while a job in a different cgroup executes the downloaded binary. 3. A file shared between processes in different cgroups, where one process faults in the pages and khugepaged (or madvise(COLLAPSE)) collapses them on behalf of the other. Fix the accounting by explicitly incrementing stats for the new THP and decrementing stats for the old folios being replaced. Link: https://lkml.kernel.org/r/20260130042925.2797946-1-shakeel.butt@linux.dev Fixes: f3f0e1d2150b ("khugepaged: add support of collapse for tmpfs/shmem pages") Signed-off-by: Shakeel Butt Reviewed-by: Baolin Wang Reviewed-by: Dev Jain Reviewed-by: Barry Song Acked-by: Johannes Weiner Reviewed-by: Kiryl Shutsemau Cc: David Hildenbrand Cc: Lance Yang Cc: Liam Howlett Cc: Lorenzo Stoakes Cc: Matthew Wilcox (Oracle) Cc: Nico Pache Cc: Rik van Riel Cc: Ryan Roberts Cc: Song Liu Cc: Usama Arif Cc: Zi Yan Signed-off-by: Andrew Morton --- mm/khugepaged.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) --- a/mm/khugepaged.c~mm-khugepaged-fix-nr_file_pages-and-nr_shmem-in-collapse_file +++ a/mm/khugepaged.c @@ -2195,16 +2195,13 @@ immap_locked: xas_lock_irq(&xas); } - if (is_shmem) + if (is_shmem) { + lruvec_stat_mod_folio(new_folio, NR_SHMEM, HPAGE_PMD_NR); lruvec_stat_mod_folio(new_folio, NR_SHMEM_THPS, HPAGE_PMD_NR); - else + } else { lruvec_stat_mod_folio(new_folio, NR_FILE_THPS, HPAGE_PMD_NR); - - if (nr_none) { - lruvec_stat_mod_folio(new_folio, NR_FILE_PAGES, nr_none); - /* nr_none is always 0 for non-shmem. */ - lruvec_stat_mod_folio(new_folio, NR_SHMEM, nr_none); } + lruvec_stat_mod_folio(new_folio, NR_FILE_PAGES, HPAGE_PMD_NR); /* * Mark new_folio as uptodate before inserting it into the @@ -2238,6 +2235,11 @@ immap_locked: */ list_for_each_entry_safe(folio, tmp, &pagelist, lru) { list_del(&folio->lru); + lruvec_stat_mod_folio(folio, NR_FILE_PAGES, + -folio_nr_pages(folio)); + if (is_shmem) + lruvec_stat_mod_folio(folio, NR_SHMEM, + -folio_nr_pages(folio)); folio->mapping = NULL; folio_clear_active(folio); folio_clear_unevictable(folio); _ Patches currently in -mm which might be from shakeel.butt@linux.dev are mm-khugepaged-fix-nr_file_pages-and-nr_shmem-in-collapse_file.patch