Skip to contentSkip to conversation

mm/shmem: fix THP allocation and fallback loop

PATCH Superseded Kairui Song 2 participants 4 messages Oct 22, 2025 – Oct 23, 2025

Subsystems (3)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 mm/shmem: fix THP allocation and fallback loop

From: Kairui Song <[email protected]>

The order check and fallback loop is updating the index value on every loop, this will cause the index to be aligned by a larger value while the loop shrinks the order.

This may result in inserting and returning a folio of the wrong index and cause data corruption with some userspace workloads [1].

---

Changes from V1:
- Link to V1: https://lore.kernel.org/linux-mm/[email protected]/
- Remove unnecessary cleanup and simplify the commit message.

 mm/shmem.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
mm/shmem.c +4 -3
index b50ce7dbc84a..7559773ebb30 100644
@@ -1895,10 +1895,11 @@ static struct folio *shmem_alloc_and_add_folio(struct vm_fault *vmf,
 		order = highest_order(suitable_orders);
 		while (suitable_orders) {
 			pages = 1UL << order;
-			index = round_down(index, pages);
-			folio = shmem_alloc_folio(gfp, order, info, index);
-			if (folio)
+			folio = shmem_alloc_folio(gfp, order, info, round_down(index, pages));
+			if (folio) {
+				index = round_down(index, pages);
 				goto allocated;
+			}
 
 			if (pages == HPAGE_PMD_NR)
 				count_vm_event(THP_FILE_FALLBACK);
Signature · 1 line
2.51.0
#1 lore raw
Quoted 7 lines
mm/shmem.c +0 -0
index b50ce7dbc84a..7559773ebb30 100644
@@ -1895,10 +1895,11 @@ static struct folio *shmem_alloc_and_add_folio(struct vm_fault *vmf,
                order = highest_order(suitable_orders);

mm/shmem.c static struct folio *shmem_alloc_and_add_folio(struct vm_fault *vmf, · line 1902

Quoted 10 lines
mm/shmem.c +4 -3
                while (suitable_orders) {
                        pages = 1UL << order;
-                       index = round_down(index, pages);
-                       folio = shmem_alloc_folio(gfp, order, info, index);
-                       if (folio)
+                       folio = shmem_alloc_folio(gfp, order, info, round_down(index, pages));
+                       if (folio) {
+                               index = round_down(index, pages);
                                goto allocated;
+                       }

Could this be a temporary variable to store round_down(index, pages)?

mm/shmem.c static struct folio *shmem_alloc_and_add_folio(struct vm_fault *vmf,

Quoted 4 lines
mm/shmem.c +0 -1
                        if (pages == HPAGE_PMD_NR)
                                count_vm_event(THP_FILE_FALLBACK);
--

Thanks
Barry

#2 lore raw
Quoted 10 lines · Barry Song
Quoted 10 lines
mm/shmem.c +0 -2
index b50ce7dbc84a..7559773ebb30 100644
@@ -1895,10 +1895,11 @@ static struct folio *shmem_alloc_and_add_folio(struct vm_fault *vmf,
                order = highest_order(suitable_orders);
                while (suitable_orders) {
                        pages = 1UL << order;
-                       index = round_down(index, pages);
-                       folio = shmem_alloc_folio(gfp, order, info, index);
Quoted 8 lines

mm/shmem.c static struct folio *shmem_alloc_and_add_folio(struct vm_fault *vmf, · line 1902

Quoted 6 lines
mm/shmem.c +4 -1
-                       if (folio)
+                       folio = shmem_alloc_folio(gfp, order, info, round_down(index, pages));
+                       if (folio) {
+                               index = round_down(index, pages);
                                goto allocated;
+                       }

Could this be a temporary variable to store round_down(index, pages)?

Right we can do that, but the generated code should be the same, the compiler is smart enough, I just checked the generated code with gcc / clang.

Do you think the code will be cleaner with a temporary variable? I can send a V3 if anyone suggests, it's really a trivial change.

#3 lore raw
Quoted 21 lines · Kairui Song
Quoted 10 lines · Barry Song
Quoted 10 lines
mm/shmem.c +0 -2
index b50ce7dbc84a..7559773ebb30 100644
@@ -1895,10 +1895,11 @@ static struct folio *shmem_alloc_and_add_folio(struct vm_fault *vmf,
                order = highest_order(suitable_orders);
                while (suitable_orders) {
                        pages = 1UL << order;
-                       index = round_down(index, pages);
-                       folio = shmem_alloc_folio(gfp, order, info, index);
Quoted 8 lines

mm/shmem.c static struct folio *shmem_alloc_and_add_folio(struct vm_fault *vmf, · line 1902

Quoted 6 lines
mm/shmem.c +4 -1
-                       if (folio)
+                       folio = shmem_alloc_folio(gfp, order, info, round_down(index, pages));
+                       if (folio) {
+                               index = round_down(index, pages);
                                goto allocated;
+                       }

Could this be a temporary variable to store round_down(index, pages)?

Quoted 6 lines

Right we can do that, but the generated code should be the same, the compiler is smart enough, I just checked the generated code with gcc / clang.

Do you think the code will be cleaner with a temporary variable? I can send a V3 if anyone suggests, it's really a trivial change.

Personally, I think a temporary variable makes the code cleaner, but I don’t have any strong preference.

The fix looks correct to me.

Thanks
Barry