--- /dev/null
+From stable+bounces-27001-greg=kroah.com@vger.kernel.org Wed Mar 6 16:52:30 2024
+From: Zi Yan <zi.yan@sent.com>
+Date: Wed, 6 Mar 2024 10:52:17 -0500
+Subject: mm/migrate: set swap entry values of THP tail pages properly.
+To: gregkh@linuxfoundation.org, stable@vger.kernel.org
+Cc: Zi Yan <ziy@nvidia.com>, linux-mm@kvack.org, Charan Teja Kalla <quic_charante@quicinc.com>, "Matthew Wilcox (Oracle)" <willy@infradead.org>, David Hildenbrand <david@redhat.com>, Andrew Morton <akpm@linux-foundation.org>, Huang Ying <ying.huang@intel.com>, Naoya Horiguchi <naoya.horiguchi@linux.dev>
+Message-ID: <20240306155217.118467-1-zi.yan@sent.com>
+
+From: Zi Yan <ziy@nvidia.com>
+
+The tail pages in a THP can have swap entry information stored in their
+private field. When migrating to a new page, all tail pages of the new
+page need to update ->private to avoid future data corruption.
+
+This fix is stable-only, since after commit 07e09c483cbe ("mm/huge_memory:
+work on folio->swap instead of page->private when splitting folio"),
+subpages of a swapcached THP no longer requires the maintenance.
+
+Adding THPs to the swapcache was introduced in commit
+38d8b4e6bdc87 ("mm, THP, swap: delay splitting THP during swap out"),
+where each subpage of a THP added to the swapcache had its own swapcache
+entry and required the ->private field to point to the correct swapcache
+entry. Later, when THP migration functionality was implemented in commit
+616b8371539a6 ("mm: thp: enable thp migration in generic path"),
+it initially did not handle the subpages of swapcached THPs, failing to
+update their ->private fields or replace the subpage pointers in the
+swapcache. Subsequently, commit e71769ae5260 ("mm: enable thp migration
+for shmem thp") addressed the swapcache update aspect. This patch fixes
+the update of subpage ->private fields.
+
+Closes: https://lore.kernel.org/linux-mm/1707814102-22682-1-git-send-email-quic_charante@quicinc.com/
+Fixes: 616b8371539a ("mm: thp: enable thp migration in generic path")
+Signed-off-by: Zi Yan <ziy@nvidia.com>
+Acked-by: David Hildenbrand <david@redhat.com>
+Reported-and-tested-by: Charan Teja Kalla <quic_charante@quicinc.com>
+Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
+---
+ mm/migrate.c | 6 +++++-
+ 1 file changed, 5 insertions(+), 1 deletion(-)
+
+--- a/mm/migrate.c
++++ b/mm/migrate.c
+@@ -423,8 +423,12 @@ int folio_migrate_mapping(struct address
+ if (folio_test_swapbacked(folio)) {
+ __folio_set_swapbacked(newfolio);
+ if (folio_test_swapcache(folio)) {
++ int i;
++
+ folio_set_swapcache(newfolio);
+- newfolio->private = folio_get_private(folio);
++ for (i = 0; i < nr; i++)
++ set_page_private(folio_page(newfolio, i),
++ page_private(folio_page(folio, i)));
+ }
+ entries = nr;
+ } else {