Skip to contentSkip to conversation

mm/migrate: move NR_KERNEL_FILE_PAGES on cross-node migration

PATCH Applied Ridong Chen 6 participants 8 messages Oct 27 – Oct 30

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mm/migrate: move NR_KERNEL_FILE_PAGES on cross-node migration

From: Ridong Chen <[email protected]>

NR_KERNEL_FILE_PAGES counts page cache folios of mappings flagged AS_KERNEL_FILE (currently the btrfs btree inode). It is a per-node
counter: filemap_add_folio() adds to folio_pgdat() of the new folio and filemap_unaccount_folio() subtracts from folio_pgdat() of the folio being removed.

__folio_migrate_mapping() does not move it when such a folio is migrated to another node, so the source node keeps the count forever and the destination node underflows once the folio is freed. btree folios are migratable through btree_migrate_folio(), so demotion, DAMON-based migration (DAMOS_MIGRATE_{HOT,COLD}) or memory offlining all hit this.

Reproducer, on a QEMU guest with two memory tiers (node0 is DRAM with all CPUs, node1 is a CPU-less slower node described via HMAT) and /sys/kernel/mm/numa/demotion_enabled set:

1. mount a btrfs and create 20000 empty files, so that btree folios

     are cached on node0;
  2. echo 2G > /sys/devices/system/node/node0/reclaim, which demotes
     them to node1;

3. umount and drop caches.

nr_kernel_file_pages in /sys/devices/system/node/nodeN/vmstat after each step:

without this patch with this patch
node0 node1 node0 node1

  1. cached         1868         0        1868         0
  2. demoted        1868         0          36      1832
  3. dropped        1832         0 (*)       0         0

(*) the counter has underflowed to -1832; negative values read as 0.

The global value in /proc/vmstat is 0 after step 3 in both cases.

Move it together with the other node-keyed page cache counters. It is a node-only counter, so use the node accessor, matching how it is accounted elsewhere. Interrupts are disabled here, as for the zone counters below.

This was pointed out by Shivank while reviewing the NR_FILE_THPS fix for the same function.

Fixes e3a9ac4e866e ("mm: add vmstat for kernel_file pages")
Assisted-by LLM
---
 mm/migrate.c | 6 ++++++
 1 file changed, 6 insertions(+)
mm/migrate.c +6 -0
index 99e3026e3126..f829c5017c96 100644
@@ -705,6 +705,12 @@ static int __folio_migrate_mapping(struct address_space *mapping,
 			mod_lruvec_state(old_lruvec, NR_FILE_THPS, -nr);
 			mod_lruvec_state(new_lruvec, NR_FILE_THPS, nr);
 		}
+		if (test_bit(AS_KERNEL_FILE, &mapping->flags)) {
+			__mod_node_page_state(oldzone->zone_pgdat,
+					      NR_KERNEL_FILE_PAGES, -nr);
+			__mod_node_page_state(newzone->zone_pgdat,
+					      NR_KERNEL_FILE_PAGES, nr);
+		}
 #ifdef CONFIG_SWAP
 		if (folio_test_swapcache(folio)) {
 			mod_lruvec_state(old_lruvec, NR_SWAPCACHE, -nr);
Signature · 1 line
2.34.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 61 lines · Ridong Chen

From: Ridong Chen <[email protected]>

NR_KERNEL_FILE_PAGES counts page cache folios of mappings flagged AS_KERNEL_FILE (currently the btrfs btree inode). It is a per-node
counter: filemap_add_folio() adds to folio_pgdat() of the new folio and filemap_unaccount_folio() subtracts from folio_pgdat() of the folio being removed.

__folio_migrate_mapping() does not move it when such a folio is migrated to another node, so the source node keeps the count forever and the destination node underflows once the folio is freed. btree folios are migratable through btree_migrate_folio(), so demotion, DAMON-based migration (DAMOS_MIGRATE_{HOT,COLD}) or memory offlining all hit this.

Reproducer, on a QEMU guest with two memory tiers (node0 is DRAM with all CPUs, node1 is a CPU-less slower node described via HMAT) and /sys/kernel/mm/numa/demotion_enabled set:

1. mount a btrfs and create 20000 empty files, so that btree folios

     are cached on node0;
  2. echo 2G > /sys/devices/system/node/node0/reclaim, which demotes
     them to node1;

3. umount and drop caches.

nr_kernel_file_pages in /sys/devices/system/node/nodeN/vmstat after each step:

without this patch with this patch
node0 node1 node0 node1

  1. cached         1868         0        1868         0
  2. demoted        1868         0          36      1832
  3. dropped        1832         0 (*)       0         0

(*) the counter has underflowed to -1832; negative values read as 0.

The global value in /proc/vmstat is 0 after step 3 in both cases.

Move it together with the other node-keyed page cache counters. It is a node-only counter, so use the node accessor, matching how it is accounted elsewhere. Interrupts are disabled here, as for the zone counters below.

This was pointed out by Shivank while reviewing the NR_FILE_THPS fix for the same function.

Fixes e3a9ac4e866e ("mm: add vmstat for kernel_file pages")
Assisted-by LLM
---
 mm/migrate.c | 6 ++++++
 1 file changed, 6 insertions(+)
mm/migrate.c +0 -0
index 99e3026e3126..f829c5017c96 100644
@@ -705,6 +705,12 @@ static int __folio_migrate_mapping(struct address_space *mapping,
 			mod_lruvec_state(old_lruvec, NR_FILE_THPS, -nr);
 			mod_lruvec_state(new_lruvec, NR_FILE_THPS, nr);

mm/migrate.c static int __folio_migrate_mapping(struct address_space *mapping, · line 716

Quoted 10 lines
mm/migrate.c +6 -0
 		}
+		if (test_bit(AS_KERNEL_FILE, &mapping->flags)) {
+			__mod_node_page_state(oldzone->zone_pgdat,
+					      NR_KERNEL_FILE_PAGES, -nr);
+			__mod_node_page_state(newzone->zone_pgdat,
+					      NR_KERNEL_FILE_PAGES, nr);
+		}
 #ifdef CONFIG_SWAP
 		if (folio_test_swapcache(folio)) {
 			mod_lruvec_state(old_lruvec, NR_SWAPCACHE, -nr);

LGTM

Thanks,
Shivank

reviewer Acked-by on v1 1/1
#2 lore raw
In reply to Ridong Chen
Quoted 46 lines · Ridong Chen

From: Ridong Chen <[email protected]>

NR_KERNEL_FILE_PAGES counts page cache folios of mappings flagged AS_KERNEL_FILE (currently the btrfs btree inode). It is a per-node
counter: filemap_add_folio() adds to folio_pgdat() of the new folio and filemap_unaccount_folio() subtracts from folio_pgdat() of the folio being removed.

__folio_migrate_mapping() does not move it when such a folio is migrated to another node, so the source node keeps the count forever and the destination node underflows once the folio is freed. btree folios are migratable through btree_migrate_folio(), so demotion, DAMON-based migration (DAMOS_MIGRATE_{HOT,COLD}) or memory offlining all hit this.

Reproducer, on a QEMU guest with two memory tiers (node0 is DRAM with all CPUs, node1 is a CPU-less slower node described via HMAT) and /sys/kernel/mm/numa/demotion_enabled set:

1. mount a btrfs and create 20000 empty files, so that btree folios

     are cached on node0;
  2. echo 2G > /sys/devices/system/node/node0/reclaim, which demotes
     them to node1;

3. umount and drop caches.

nr_kernel_file_pages in /sys/devices/system/node/nodeN/vmstat after each step:

without this patch with this patch
node0 node1 node0 node1

  1. cached         1868         0        1868         0
  2. demoted        1868         0          36      1832
  3. dropped        1832         0 (*)       0         0

(*) the counter has underflowed to -1832; negative values read as 0.

The global value in /proc/vmstat is 0 after step 3 in both cases.

Move it together with the other node-keyed page cache counters. It is a node-only counter, so use the node accessor, matching how it is accounted elsewhere. Interrupts are disabled here, as for the zone counters below.

This was pointed out by Shivank while reviewing the NR_FILE_THPS fix for the same function.

Quoted 8 lines
Fixes e3a9ac4e866e ("mm: add vmstat for kernel_file pages")
Assisted-by LLM
---
 mm/migrate.c | 6 ++++++
 1 file changed, 6 insertions(+)

LGTM.

Best Regards,
Yan, Zi

maintainer Acked-by on v1 1/1
#3 lore raw
In reply to Ridong Chen
Quoted 61 lines · Ridong Chen

From: Ridong Chen <[email protected]>

NR_KERNEL_FILE_PAGES counts page cache folios of mappings flagged AS_KERNEL_FILE (currently the btrfs btree inode). It is a per-node
counter: filemap_add_folio() adds to folio_pgdat() of the new folio and filemap_unaccount_folio() subtracts from folio_pgdat() of the folio being removed.

__folio_migrate_mapping() does not move it when such a folio is migrated to another node, so the source node keeps the count forever and the destination node underflows once the folio is freed. btree folios are migratable through btree_migrate_folio(), so demotion, DAMON-based migration (DAMOS_MIGRATE_{HOT,COLD}) or memory offlining all hit this.

Reproducer, on a QEMU guest with two memory tiers (node0 is DRAM with all CPUs, node1 is a CPU-less slower node described via HMAT) and /sys/kernel/mm/numa/demotion_enabled set:

1. mount a btrfs and create 20000 empty files, so that btree folios

     are cached on node0;
  2. echo 2G > /sys/devices/system/node/node0/reclaim, which demotes
     them to node1;

3. umount and drop caches.

nr_kernel_file_pages in /sys/devices/system/node/nodeN/vmstat after each step:

without this patch with this patch
node0 node1 node0 node1

  1. cached         1868         0        1868         0
  2. demoted        1868         0          36      1832
  3. dropped        1832         0 (*)       0         0

(*) the counter has underflowed to -1832; negative values read as 0.

The global value in /proc/vmstat is 0 after step 3 in both cases.

Move it together with the other node-keyed page cache counters. It is a node-only counter, so use the node accessor, matching how it is accounted elsewhere. Interrupts are disabled here, as for the zone counters below.

This was pointed out by Shivank while reviewing the NR_FILE_THPS fix for the same function.

Fixes e3a9ac4e866e ("mm: add vmstat for kernel_file pages")
Assisted-by LLM
---
 mm/migrate.c | 6 ++++++
 1 file changed, 6 insertions(+)
mm/migrate.c +0 -0
index 99e3026e3126..f829c5017c96 100644
@@ -705,6 +705,12 @@ static int __folio_migrate_mapping(struct address_space *mapping,
 			mod_lruvec_state(old_lruvec, NR_FILE_THPS, -nr);
 			mod_lruvec_state(new_lruvec, NR_FILE_THPS, nr);

mm/migrate.c static int __folio_migrate_mapping(struct address_space *mapping, · line 716

Quoted 10 lines
mm/migrate.c +6 -0
 		}
+		if (test_bit(AS_KERNEL_FILE, &mapping->flags)) {
+			__mod_node_page_state(oldzone->zone_pgdat,
+					      NR_KERNEL_FILE_PAGES, -nr);
+			__mod_node_page_state(newzone->zone_pgdat,
+					      NR_KERNEL_FILE_PAGES, nr);
+		}
 #ifdef CONFIG_SWAP
 		if (folio_test_swapcache(folio)) {
 			mod_lruvec_state(old_lruvec, NR_SWAPCACHE, -nr);

Today I learned about NR_KERNEL_FILE_PAGES :D

Signature · 3 lines
Cheers,

David
maintainer
#4 lore raw
In reply to Ridong Chen
Quoted 38 lines · On Tue, 27 Oct 2026 12:32:14 +0800 Ridong Chen <[email protected]> wrote:

From: Ridong Chen <[email protected]>

NR_KERNEL_FILE_PAGES counts page cache folios of mappings flagged AS_KERNEL_FILE (currently the btrfs btree inode). It is a per-node
counter: filemap_add_folio() adds to folio_pgdat() of the new folio and filemap_unaccount_folio() subtracts from folio_pgdat() of the folio being removed.

__folio_migrate_mapping() does not move it when such a folio is migrated to another node, so the source node keeps the count forever and the destination node underflows once the folio is freed. btree folios are migratable through btree_migrate_folio(), so demotion, DAMON-based migration (DAMOS_MIGRATE_{HOT,COLD}) or memory offlining all hit this.

Reproducer, on a QEMU guest with two memory tiers (node0 is DRAM with all CPUs, node1 is a CPU-less slower node described via HMAT) and /sys/kernel/mm/numa/demotion_enabled set:

1. mount a btrfs and create 20000 empty files, so that btree folios

     are cached on node0;
  2. echo 2G > /sys/devices/system/node/node0/reclaim, which demotes
     them to node1;

3. umount and drop caches.

nr_kernel_file_pages in /sys/devices/system/node/nodeN/vmstat after each step:

without this patch with this patch
node0 node1 node0 node1

  1. cached         1868         0        1868         0
  2. demoted        1868         0          36      1832
  3. dropped        1832         0 (*)       0         0

(*) the counter has underflowed to -1832; negative values read as 0.

The global value in /proc/vmstat is 0 after step 3 in both cases.

Quoted 8 lines

Move it together with the other node-keyed page cache counters. It is a node-only counter, so use the node accessor, matching how it is accounted elsewhere. Interrupts are disabled here, as for the zone counters below.

This was pointed out by Shivank while reviewing the NR_FILE_THPS fix for the same function.

Thanks.

This and your "mm/migrate: move NR_FILE_THPS on cross-node migration" "only" address accounting errors, but a bug is a bug and these bugs might mess up people's memory-monitoring and NUMA-management tools. So I think cc:stable.

What do people think?

#5 lore raw
Quoted 49 lines · Andrew Morton
Quoted 38 lines · On Tue, 27 Oct 2026 12:32:14 +0800 Ridong Chen <[email protected]> wrote:

From: Ridong Chen <[email protected]>

NR_KERNEL_FILE_PAGES counts page cache folios of mappings flagged AS_KERNEL_FILE (currently the btrfs btree inode). It is a per-node
counter: filemap_add_folio() adds to folio_pgdat() of the new folio and filemap_unaccount_folio() subtracts from folio_pgdat() of the folio being removed.

__folio_migrate_mapping() does not move it when such a folio is migrated to another node, so the source node keeps the count forever and the destination node underflows once the folio is freed. btree folios are migratable through btree_migrate_folio(), so demotion, DAMON-based migration (DAMOS_MIGRATE_{HOT,COLD}) or memory offlining all hit this.

Reproducer, on a QEMU guest with two memory tiers (node0 is DRAM with all CPUs, node1 is a CPU-less slower node described via HMAT) and /sys/kernel/mm/numa/demotion_enabled set:

1. mount a btrfs and create 20000 empty files, so that btree folios

      are cached on node0;
   2. echo 2G > /sys/devices/system/node/node0/reclaim, which demotes
      them to node1;

3. umount and drop caches.

nr_kernel_file_pages in /sys/devices/system/node/nodeN/vmstat after each step:

without this patch with this patch
node0 node1 node0 node1

   1. cached         1868         0        1868         0
   2. demoted        1868         0          36      1832
   3. dropped        1832         0 (*)       0         0

(*) the counter has underflowed to -1832; negative values read as 0.

The global value in /proc/vmstat is 0 after step 3 in both cases.

Quoted 8 lines

Move it together with the other node-keyed page cache counters. It is a node-only counter, so use the node accessor, matching how it is accounted elsewhere. Interrupts are disabled here, as for the zone counters below.

This was pointed out by Shivank while reviewing the NR_FILE_THPS fix for the same function.

Quoted 8 lines

Thanks.

This and your "mm/migrate: move NR_FILE_THPS on cross-node migration" "only" address accounting errors, but a bug is a bug and these bugs might mess up people's memory-monitoring and NUMA-management tools. So I think cc:stable.

What do people think?

Agreed, thanks.

Signature · 2 lines
Best regards
Ridong
Acked-by on v1 1/1
#7 lore raw
In reply to Ridong Chen
Quoted 43 lines · Ridong Chen

From: Ridong Chen <[email protected]>

NR_KERNEL_FILE_PAGES counts page cache folios of mappings flagged AS_KERNEL_FILE (currently the btrfs btree inode). It is a per-node
counter: filemap_add_folio() adds to folio_pgdat() of the new folio and filemap_unaccount_folio() subtracts from folio_pgdat() of the folio being removed.

__folio_migrate_mapping() does not move it when such a folio is migrated to another node, so the source node keeps the count forever and the destination node underflows once the folio is freed. btree folios are migratable through btree_migrate_folio(), so demotion, DAMON-based migration (DAMOS_MIGRATE_{HOT,COLD}) or memory offlining all hit this.

Reproducer, on a QEMU guest with two memory tiers (node0 is DRAM with all CPUs, node1 is a CPU-less slower node described via HMAT) and /sys/kernel/mm/numa/demotion_enabled set:

1. mount a btrfs and create 20000 empty files, so that btree folios

     are cached on node0;
  2. echo 2G > /sys/devices/system/node/node0/reclaim, which demotes
     them to node1;

3. umount and drop caches.

nr_kernel_file_pages in /sys/devices/system/node/nodeN/vmstat after each step:

without this patch with this patch
node0 node1 node0 node1

  1. cached         1868         0        1868         0
  2. demoted        1868         0          36      1832
  3. dropped        1832         0 (*)       0         0

(*) the counter has underflowed to -1832; negative values read as 0.

The global value in /proc/vmstat is 0 after step 3 in both cases.

Move it together with the other node-keyed page cache counters. It is a node-only counter, so use the node accessor, matching how it is accounted elsewhere. Interrupts are disabled here, as for the zone counters below.

Quoted 7 lines

This was pointed out by Shivank while reviewing the NR_FILE_THPS fix for the same function.

Fixes e3a9ac4e866e ("mm: add vmstat for kernel_file pages")
Assisted-by LLM

Thanks for fixing this.

#6 lore raw
In reply to Andrew Morton
Quoted 49 lines · Andrew Morton
Quoted 38 lines · On Tue, 27 Oct 2026 12:32:14 +0800 Ridong Chen <[email protected]> wrote:

From: Ridong Chen <[email protected]>

NR_KERNEL_FILE_PAGES counts page cache folios of mappings flagged AS_KERNEL_FILE (currently the btrfs btree inode). It is a per-node
counter: filemap_add_folio() adds to folio_pgdat() of the new folio and filemap_unaccount_folio() subtracts from folio_pgdat() of the folio being removed.

__folio_migrate_mapping() does not move it when such a folio is migrated to another node, so the source node keeps the count forever and the destination node underflows once the folio is freed. btree folios are migratable through btree_migrate_folio(), so demotion, DAMON-based migration (DAMOS_MIGRATE_{HOT,COLD}) or memory offlining all hit this.

Reproducer, on a QEMU guest with two memory tiers (node0 is DRAM with all CPUs, node1 is a CPU-less slower node described via HMAT) and /sys/kernel/mm/numa/demotion_enabled set:

1. mount a btrfs and create 20000 empty files, so that btree folios

     are cached on node0;
  2. echo 2G > /sys/devices/system/node/node0/reclaim, which demotes
     them to node1;

3. umount and drop caches.

nr_kernel_file_pages in /sys/devices/system/node/nodeN/vmstat after each step:

without this patch with this patch
node0 node1 node0 node1

  1. cached         1868         0        1868         0
  2. demoted        1868         0          36      1832
  3. dropped        1832         0 (*)       0         0

(*) the counter has underflowed to -1832; negative values read as 0.

The global value in /proc/vmstat is 0 after step 3 in both cases.

Quoted 8 lines

Move it together with the other node-keyed page cache counters. It is a node-only counter, so use the node accessor, matching how it is accounted elsewhere. Interrupts are disabled here, as for the zone counters below.

This was pointed out by Shivank while reviewing the NR_FILE_THPS fix for the same function.

Quoted 8 lines

Thanks.

This and your "mm/migrate: move NR_FILE_THPS on cross-node migration" "only" address accounting errors, but a bug is a bug and these bugs might mess up people's memory-monitoring and NUMA-management tools. So I think cc:stable.

What do people think?

Yes this is stable material.