Subject: + mm-shmem-fix-thp-allocation-and-fallback-loop.patch added to mm-hotfixes-unstable branch

From: Andrew Morton <[email-masked]>
Date: Wed, 22 Oct 2025 14:08:42 -0700

The patch titled
     Subject: mm/shmem: fix THP allocation and fallback loop
has been added to the -mm mm-hotfixes-unstable branch.  Its filename is
     mm-shmem-fix-thp-allocation-and-fallback-loop.patch

This patch will shortly appear at
     https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/mm-shmem-fix-thp-allocation-and-fallback-loop.patch

This patch will later appear in the mm-hotfixes-unstable branch at
    git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm

Before you just go and hit "reply", please:
   a) Consider who else should be cc'ed
   b) Prefer to cc a suitable mailing list as well
   c) Ideally: find the original patch on the mailing list and do a
      reply-to-all to that, adding suitable additional cc's

*** Remember to use Documentation/process/submit-checklist.rst when testing your code ***

The -mm tree is included into linux-next via the mm-everything
branch at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
and is updated there every 2-3 working days

------------------------------------------------------
From: Kairui Song <[email-masked]>
Subject: mm/shmem: fix THP allocation and fallback loop
Date: Wed, 22 Oct 2025 18:57:19 +0800

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].

Link: https://lkml.kernel.org/r/[email-masked]
Link: https://lore.kernel.org/linux-mm/[email-masked]/ [1]
Fixes: e7a2ab7b3bb5d ("mm: shmem: add mTHP support for anonymous shmem")
Signed-off-by: Kairui Song <[email-masked]>
Cc: Baolin Wang <[email-masked]>
Cc: Barry Song <[email-masked]>
Cc: David Hildenbrand <[email-masked]>
Cc: Dev Jain <[email-masked]>
Cc: Hugh Dickins <[email-masked]>
Cc: Liam Howlett <[email-masked]>
Cc: Lorenzo Stoakes <[email-masked]>
Cc: Matthew Wilcox (Oracle) <[email-masked]>
Cc: Nico Pache <[email-masked]>
Cc: Ryan Roberts <[email-masked]>
Cc: Zi Yan <[email-masked]>
Cc: <[email-masked]>
Signed-off-by: Andrew Morton <[email-masked]>
---

 mm/shmem.c |    7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

--- a/mm/shmem.c~mm-shmem-fix-thp-allocation-and-fallback-loop
+++ a/mm/shmem.c
@@ -1895,10 +1895,11 @@ static struct folio *shmem_alloc_and_add
 		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);
_

Patches currently in -mm which might be from [email-masked] are

mm-shmem-fix-thp-allocation-and-fallback-loop.patch