From nobody Mon Feb 9 01:44:20 2026 Received: from out-180.mta1.migadu.com (out-180.mta1.migadu.com [95.215.58.180]) (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 8A2DF272803 for ; Thu, 29 Jan 2026 18:41:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769712098; cv=none; b=ciSB/4TL3CsE6WtK1fAhr/ufljEip2QbIoJw6rn6wl+gOOxBdRRNknb2h6sDlyCq/O3qUn6eEJfOoVu4o4V9WySw1ZSSios2CR+F/7Lo+SE0ZQ5Uunrj89aokv6EdHBySrXoaBMqiZx7btSkaMwKWxGK/VViORwlHVHzheCiO6I= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769712098; c=relaxed/simple; bh=+ExvWFoTikYc/HLg4vWR4/vq/2/rFal2AzTRP9Izhy0=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=fbCinHRTP/e470q7jC5LX7HPCwT3OC10x8i37q+IqVkMau745kfiL8TkXO4LXaRtw5PbHitF7RtxkqLKpy5h8bnshy2/9s4ibKi8Mbxycp9Z1nwrxXvRnBLYnVTIMNqIFirpODcoZhoz+QxKzZSo4vXuRZLPaSwOAmPXsYkI5u0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=EQTSuFqu; arc=none smtp.client-ip=95.215.58.180 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="EQTSuFqu" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1769712084; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=Xlnz6aumZlbTSchYue5J7VboFXhHLrpxFx7kxdM7jFE=; b=EQTSuFqu2W3JWsXY4FbUgQCd0SKbNN90pOEAd2Qp3H1CxbG+umwpCdeFrwFn5VozImAsV7 P2i18zlTAQta3caaUmOBL2jwk+HHw6WVSXKd2mcH3MYqYrvfK+rEW73UDNfzPqey+njdS9 ZCaq5KgPZNWPDSjkwN/KypqKs8hUWJM= From: Shakeel Butt To: Andrew Morton Cc: Johannes Weiner , Rik van Riel , Song Liu , Kiryl Shutsemau , Usama Arif , David Hildenbrand , Lorenzo Stoakes , Zi Yan , Baolin Wang , "Liam R . Howlett" , Nico Pache , Ryan Roberts , Dev Jain , Barry Song , Lance Yang , Meta kernel team , linux-mm@kvack.org, cgroups@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH] mm: khugepaged: fix NR_FILE_PAGES accounting in collapse_file() Date: Thu, 29 Jan 2026 10:40:54 -0800 Message-ID: <20260129184054.910897-1-shakeel.butt@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" In META's fleet, we are seeing high level cgroups with zero file memcg stat but their descendants have non-zero file stat. This should not be possible. On further inspection by looking at kernel data structures though drgn, it was revealed that the high level cgroups have negative file stat which was aggregated from their children. Another interesting point was that this specific issue start happening more often as we started deploying thp-always more widely which indicates some correlation between file memory and THPs and indeed it was found that file memcg stat accounting is buggy in the collapse code path from the start. When collapse_file() replaces small folios with a large THP, it fails to properly update the NR_FILE_PAGES memcg stat for both the old folios being freed and the new THP being added. It assumes the old and new folios belong to the same cgroup. However this assumption breaks in couple of scenarios: 1. Binary (executable) package downloader running in a different cgroup than the actual job executing the downloaded package. 2. File shared and mapped by processes running in different cgroups. One process read-in the file and the second process either through madvise(COLLAPSE) or khugepaged on behalf of second process collapsing the file. So, the current code has two bugs: 1. For non-shmem files, NR_FILE_PAGES is never incremented for the new THP because nr_none is always 0 for non-shmem, and the stat update is inside the "if (nr_none)" block. 2. When freeing old folios, NR_FILE_PAGES is never decremented because folio->mapping is set to NULL directly without calling filemap_unaccount_folio(). These bugs cause incorrect per-memcg accounting when the process triggering the collapse (MADV_COLLAPSE or khugepaged) belongs to a different memcg than the process that originally faulted in the pages: - Process A (memcg X) reads file, creating 512 small page cache folios charged to memcg X (NR_FILE_PAGES +=3D 512 for memcg X) - Process B (memcg Y) triggers collapse via MADV_COLLAPSE or khugepaged scans B's mm. The new THP is charged to memcg Y. - Old folios freed: NR_FILE_PAGES not decremented (bug) New THP added: NR_FILE_PAGES not incremented (bug) - Later, THP removed from page cache: NR_FILE_PAGES -=3D 512 for memcg Y Result: memcg X has +512 inflated pages, memcg Y has -512 (negative!) Fix this by: 1. Always incrementing NR_FILE_PAGES by HPAGE_PMD_NR for the new THP 2. Decrementing NR_FILE_PAGES for each old folio before clearing its mapping pointer For shmem with holes (nr_none > 0), the net change is still +nr_none since we decrement (HPAGE_PMD_NR - nr_none) old pages and increment HPAGE_PMD_NR new pages. Fixes: 99cb0dbd47a1 ("mm,thp: add read-only THP support for (non-shmem) FS") Signed-off-by: Shakeel Butt Acked-by: Usama Arif --- mm/khugepaged.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/mm/khugepaged.c b/mm/khugepaged.c index 1d994b6c58c6..1cf8e154e214 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -2200,8 +2200,8 @@ static enum scan_result collapse_file(struct mm_struc= t *mm, unsigned long addr, else lruvec_stat_mod_folio(new_folio, NR_FILE_THPS, HPAGE_PMD_NR); =20 + lruvec_stat_mod_folio(new_folio, NR_FILE_PAGES, 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); } @@ -2238,6 +2238,8 @@ static enum scan_result collapse_file(struct mm_struc= t *mm, unsigned long addr, */ 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)); folio->mapping =3D NULL; folio_clear_active(folio); folio_clear_unevictable(folio); --=20 2.47.3