Skip to contentSkip to conversation

mm/migrate: fix shmem xarray update during migration

PATCH Superseded Zi Yan 3 participants 5 messages Feb 28, 2025

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Zi Yan

author reviewer
#0 lore raw

PATCH 1/1 mm/migrate: fix shmem xarray update during migration

Pagecache uses multi-index entries for large folio, so does shmem. Only swap cache still stores multiple entries for a single large folio. Commit fc346d0a70a1 ("mm: migrate high-order folios in swap cache correctly") fixed swap cache but got shmem wrong by storing multiple entries for a large shmem folio. Fix it by storing a single entry for a shmem folio.

Fixes fc346d0a70a1 ("mm: migrate high-order folios in swap cache correctly")
---
 mm/migrate.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
mm/migrate.c +6 -1
index 365c6daa8d1b..9db26f5527a8 100644
@@ -44,6 +44,7 @@
 #include <linux/sched/sysctl.h>
 #include <linux/memory-tiers.h>
 #include <linux/pagewalk.h>
+#include <linux/shmem_fs.h>
 
 #include <asm/tlbflush.h>
 
@@ -524,7 +525,11 @@ static int __folio_migrate_mapping(struct address_space *mapping,
 			folio_set_swapcache(newfolio);
 			newfolio->private = folio_get_private(folio);
 		}
-		entries = nr;
+		/* shmem uses high-order entry */
+		if (shmem_mapping(mapping))
+			entries = 1;
+		else
+			entries = nr;
 	} else {
 		VM_BUG_ON_FOLIO(folio_test_swapcache(folio), folio);
 		entries = 1;
Signature · 1 line
2.47.2
#1 lore raw

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

Quoted 7 lines · Zi Yan
mm/migrate.c +2 -1
@@ -524,7 +525,11 @@ static int __folio_migrate_mapping(struct address_space *mapping,
 			folio_set_swapcache(newfolio);
 			newfolio->private = folio_get_private(folio);
 		}
-		entries = nr;
+		/* shmem uses high-order entry */
+		if (shmem_mapping(mapping))

It's be cheaper to check folio_test_anon() here, right?

Also, how did this bug remain unnoticed for almost 4 years? Our testing is terrible ;-(

Quoted 9 lines
mm/migrate.c +3 -0
+			entries = 1;
+		else
+			entries = nr;
 	} else {
 		VM_BUG_ON_FOLIO(folio_test_swapcache(folio), folio);
 		entries = 1;
Signature · 1 line
2.47.2
author reviewer
#2 lore raw
Quoted 10 lines · Matthew Wilcox

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

Quoted 7 lines · Zi Yan
mm/migrate.c +2 -1
@@ -524,7 +525,11 @@ static int __folio_migrate_mapping(struct address_space *mapping,
 			folio_set_swapcache(newfolio);
 			newfolio->private = folio_get_private(folio);
 		}
-		entries = nr;
+		/* shmem uses high-order entry */
+		if (shmem_mapping(mapping))

It's be cheaper to check folio_test_anon() here, right?

Yes and it gets rid of the new include. Let me send v2.

Quoted 3 lines

Also, how did this bug remain unnoticed for almost 4 years? Our testing is terrible ;-(

Probably not 4 years. Before fc346d0a70a1 (2023), shmem was right, swap cache was wrong. After fc346d0a70a1, shmem is wrong, swap cache is right. And before Baolin’s patch, shmem only has PMD size folio to use multi-index entries. Maybe getting PMD size folio is really impossible when compaction runs a lot?

Quoted 10 lines
Quoted 10 lines
mm/migrate.c +3 -0
+			entries = 1;
+		else
+			entries = nr;
 	} else {
 		VM_BUG_ON_FOLIO(folio_test_swapcache(folio), folio);
 		entries = 1;
Signature · 1 line
2.47.2

Best Regards,
Yan, Zi

Reviewed-by on v1 1/1
#3 lore raw
In reply to Zi Yan
Quoted 28 lines · Zi Yan

Pagecache uses multi-index entries for large folio, so does shmem. Only swap cache still stores multiple entries for a single large folio. Commit fc346d0a70a1 ("mm: migrate high-order folios in swap cache correctly") fixed swap cache but got shmem wrong by storing multiple entries for a large shmem folio. Fix it by storing a single entry for a shmem folio.

Fixes fc346d0a70a1 ("mm: migrate high-order folios in swap cache correctly")
---
 mm/migrate.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
mm/migrate.c +1 -0
index 365c6daa8d1b..9db26f5527a8 100644
@@ -44,6 +44,7 @@
 #include <linux/sched/sysctl.h>
 #include <linux/memory-tiers.h>
 #include <linux/pagewalk.h>
+#include <linux/shmem_fs.h>
 
 #include <asm/tlbflush.h>
 

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

Quoted 9 lines
mm/migrate.c +5 -1
@@ -524,7 +525,11 @@ static int __folio_migrate_mapping(struct address_space *mapping,
 			folio_set_swapcache(newfolio);
 			newfolio->private = folio_get_private(folio);
 		}
-		entries = nr;
+		/* shmem uses high-order entry */
+		if (shmem_mapping(mapping))
+			entries = 1;
+		else
+			entries = nr;

LGTM functionally.
As a minor style suggestion, can we consider using a ternary operator:

entries = shmem_mapping(mapping) ? 1 : nr; /* shmem uses high-order entry */

This looks cleaner to me.

Quoted 3 lines
mm/migrate.c +0 -0
 	} else {
 		VM_BUG_ON_FOLIO(folio_test_swapcache(folio), folio);
 		entries = 1;
author reviewer
#4 lore raw
Quoted 42 lines · Shivank Garg
Quoted 28 lines · Zi Yan

Pagecache uses multi-index entries for large folio, so does shmem. Only swap cache still stores multiple entries for a single large folio. Commit fc346d0a70a1 ("mm: migrate high-order folios in swap cache correctly") fixed swap cache but got shmem wrong by storing multiple entries for a large shmem folio. Fix it by storing a single entry for a shmem folio.

Fixes fc346d0a70a1 ("mm: migrate high-order folios in swap cache correctly")
---
 mm/migrate.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
mm/migrate.c +1 -0
index 365c6daa8d1b..9db26f5527a8 100644
@@ -44,6 +44,7 @@
 #include <linux/sched/sysctl.h>
 #include <linux/memory-tiers.h>
 #include <linux/pagewalk.h>
+#include <linux/shmem_fs.h>
 
 #include <asm/tlbflush.h>
 

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

Quoted 9 lines
mm/migrate.c +5 -1
@@ -524,7 +525,11 @@ static int __folio_migrate_mapping(struct address_space *mapping,
 			folio_set_swapcache(newfolio);
 			newfolio->private = folio_get_private(folio);
 		}
-		entries = nr;
+		/* shmem uses high-order entry */
+		if (shmem_mapping(mapping))
+			entries = 1;
+		else
+			entries = nr;

LGTM functionally.
As a minor style suggestion, can we consider using a ternary operator:

Quoted 5 lines

entries = shmem_mapping(mapping) ? 1 : nr; /* shmem uses high-order entry */

This looks cleaner to me.

Thanks.

Best Regards,
Yan, Zi