From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796388294; b=CCYHemG32qfuTp4BBlRxYwS/T51jG3JEQRPJkPdXj/IQygSc4eNTAbJC0bfnY72GDL1Y 8GM/4WB0Uj92uBWfxoWBqX5bHBHsk+5yVILxJHx+tG6/R67bCKeKGbur7EQHIRNqfh9M7 wN6EAI6WbscbqSuN+ypXqtx5+z4wblP39rgH+S8A6gEEdjYoTHRBDJ/q08BWZoNgwSC5m 4WNgjvPs4p+F6rvRvBZ3oaoddxldS0Q6mrfEBOcPu9NH6nGjlV8dWFGl4/8x0k7+TxSLt LQSXVAvmUt1J+HaiqAG9gV3mXYKQmjyOYtFX6wwtX4KFFFO5ElIVM+jWXkTFJOEyW1w== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796388294; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:Content-Transfer-Encoding; bh=eHJQq1IjUO0V0bnNpugXjoJ6ZUHXf3bPctdG+tRqFF8=; b=dnYQVDbxrYCnq/TseR1VGYhisqCSbtffDJca2JfB5kD3s+RQWqDH0ZHZ/TiPq8ag6TZx ESphxEoB9yvy6DZCwslLeBiwyoa2dT2P//eE3QNppqc2Mveo1WAznMSzMFBRpop9nTpwm RcboyDOnTQKkuRiDWIU/y4SUacaNo9c85bYGUcIikooROs6ywv9af22Rcty6BnZ1nYBT4 +tNw5e/Ez4WQf860QUEdtZRUwJDmEL/POjIn5nQBUWnr3i9082yOHRixjXmOiDlf7dExp WxDACnCl7arhEQbC38o99D2otlNnmnQ8LyTxyRGRi3CSPdsTq6TM3ZyruuI/QsQNFsQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=FFqps8QI; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 32EFD606A334 for ; Fri, 4 Dec 2026 12:44:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=FFqps8QI Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3C6A91F00897; Fri, 4 Dec 2026 12:44:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796388293; bh=eHJQq1IjUO0V0bnNpugXjoJ6ZUHXf3bPctdG+tRqFF8=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=FFqps8QI3Zf1Hdz4FV14XqIiP4Bzh4h1/mCbDuq0z3iGb0D/HF3FpxTh2QGq2m9a9 JhUA7n7b3nd1YH/ezSidaV9uOj7zTtf+zQKM7lwtVGtCZr0oPwPNA1p9cjcyrKmmKi I8C+PQLGDUWQcZHfGhDLo8kg+KbnwXwIaZ92goad4b4UT/Jb/YCf4gBITiiFCGK79/ VhfLRQm7G6AH+r+ZMf2o+c3gE18+Bo0itGkIQJFrDsmQYwqgTsCPxO2CUM92AD6sQJ N/ZZGqN2YfhpVwDude7eDFRyN5MgUb0Phz67nqq+qASSbmFzEBQ9lCU1EiQCne1yrh hnhpQPbFq6yHw== From: Sasha Levin To: stable@vger.kernel.org Cc: Ridong Chen , Andrew Morton , "David Hildenbrand (Arm)" , Zi Yan , Matthew Brost , Joshua Hahn , Byungchul Park , Gregory Price , Ying Huang , Alistair Popple , "Matthew Wilcox (Oracle)" , "Garg, Shivank" , Sasha Levin Subject: [PATCH 6.1.y 2/2] mm/migrate: move NR_FILE_THPS on cross-node migration Date: Fri, 4 Dec 2026 07:44:49 -0500 Message-ID: <20261204124355.3064748-2-sashal@kernel.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20261204124355.3064748-1-sashal@kernel.org> References: <2026120240-antibody-librarian-f579@gregkh> <20261204124355.3064748-1-sashal@kernel.org> X-Mailing-List: stable@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Ridong Chen [ Upstream commit a2108a958143de74b4e5ddca12b01f05ecd030f9 ] When a page cache folio is migrated to another node, __folio_migrate_mapping() moves the per-node and per-memcg counters that are keyed off the folio's node: NR_FILE_PAGES, NR_SHMEM, NR_SHMEM_THPS, NR_SWAPCACHE and NR_FILE_DIRTY. NR_FILE_THPS is not moved, although __filemap_add_folio() charges it to the source node and filemap_unaccount_folio() takes it off whatever node the folio lives on when it is removed. Commit 0b52c420350e ("mm: fix shmem THP counters on migration") added the NR_SHMEM_THPS case but not its file-backed counterpart. Now that ext4, xfs, btrfs and erofs can all have PMD-sized page cache folios, migrating them via NUMA balancing, mbind(), migratepages, demotion or memory offlining leaks FileHugePages on the source node and underflows it on the destination, in both /sys/devices/system/node/nodeN/meminfo and memory.numa_stat. The global value is unaffected, which is why this went unnoticed. Reproducer, on a 2-node QEMU guest with THP set to "always": 1. read a 256M ext4 file through a MADV_HUGEPAGE mapping while bound to node 0, so its page cache is 128 PMD-sized folios on node 0; 2. move the mapping to node 1 with mbind(MPOL_MF_MOVE); 3. unmap the file and drop its page cache. nr_file_hugepages in /sys/devices/system/node/nodeN/vmstat after each step (in units of PMD-sized folios): without this patch with this patch node0 node1 node0 node1 1. cached 128 0 128 0 2. migrated 128 0 0 128 3. dropped 128 0 (*) 0 0 (*) the counter has underflowed to -128; negative values read as 0. Without the patch the 128 folios stay accounted to node 0 forever, and every further round adds another 128. The same happens to the per-memcg per-node counter: after step 3, memory.stat of the cgroup shows "file_thp 0", while memory.numa_stat still shows "file_thp N0=268435456 N1=0". With the patch both are 0. Move NR_FILE_THPS together with NR_FILE_PAGES, using the same condition filemap_unaccount_folio() uses to take it off, so that THPs in the swap cache are left alone. Link: https://lore.kernel.org/20261025110209.4105994-1-ridong.chen@linux.dev Fixes: d68eccad3706 ("mm/filemap: Allow large folios to be added to the page cache") Signed-off-by: Ridong Chen Signed-off-by: Andrew Morton Acked-by: David Hildenbrand (Arm) Cc: Zi Yan Cc: Matthew Brost Cc: Joshua Hahn Cc: Byungchul Park Cc: Gregory Price Cc: Ying Huang Cc: Alistair Popple Cc: Matthew Wilcox (Oracle) Cc: "Garg, Shivank" Cc: Signed-off-by: Sasha Levin --- mm/migrate.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/mm/migrate.c b/mm/migrate.c index 2ba17adb14134..c3bb2ac647e99 100644 --- a/mm/migrate.c +++ b/mm/migrate.c @@ -485,6 +485,10 @@ int folio_migrate_mapping(struct address_space *mapping, __mod_lruvec_state(old_lruvec, NR_SHMEM_THPS, -nr); __mod_lruvec_state(new_lruvec, NR_SHMEM_THPS, nr); } + } else if (!folio_test_swapbacked(folio) && + folio_test_pmd_mappable(folio)) { + mod_lruvec_state(old_lruvec, NR_FILE_THPS, -nr); + mod_lruvec_state(new_lruvec, NR_FILE_THPS, nr); } #ifdef CONFIG_SWAP if (folio_test_swapcache(folio)) { -- 2.53.0