mm/migrate: preserve exact soft-dirty state
authorPaul Gofman <pgofman@codeweavers.com>
Tue, 6 Feb 2024 08:48:01 +0000 (13:48 +0500)
committerAndrew Morton <akpm@linux-foundation.org>
Thu, 22 Feb 2024 18:24:55 +0000 (10:24 -0800)
pte_mkdirty() sets both _PAGE_DIRTY and _PAGE_SOFT_DIRTY bits.  The
_PAGE_SOFT_DIRTY can get set even if it wasn't set on original page before
migration.  This makes non-soft-dirty pages soft-dirty just because of
migration/compaction.  Clear the _PAGE_SOFT_DIRTY flag if it wasn't set on
original page.

By definition of soft-dirty feature, there can be spurious soft-dirty
pages because of kernel's internal activity such as VMA merging or
migration/compaction.  This patch is eliminating the spurious soft-dirty
pages because of migration/compaction.

Link: https://lkml.kernel.org/r/20240206084838.34560-1-usama.anjum@collabora.com
Signed-off-by: Paul Gofman <pgofman@codeweavers.com>
Signed-off-by: Muhammad Usama Anjum <usama.anjum@collabora.com>
Acked-by: Andrei Vagin <avagin@gmail.com>
Cc: Michał Mirosław <emmir@google.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
mm/migrate.c

index cc9f2bcd73b492aebacab4b812a515cf7e70b92b..05d6ca437321564925e4f1ac0e2949684c32b1f3 100644 (file)
@@ -211,14 +211,17 @@ static bool remove_migration_pte(struct folio *folio,
                folio_get(folio);
                pte = mk_pte(new, READ_ONCE(vma->vm_page_prot));
                old_pte = ptep_get(pvmw.pte);
-               if (pte_swp_soft_dirty(old_pte))
-                       pte = pte_mksoft_dirty(pte);
 
                entry = pte_to_swp_entry(old_pte);
                if (!is_migration_entry_young(entry))
                        pte = pte_mkold(pte);
                if (folio_test_dirty(folio) && is_migration_entry_dirty(entry))
                        pte = pte_mkdirty(pte);
+               if (pte_swp_soft_dirty(old_pte))
+                       pte = pte_mksoft_dirty(pte);
+               else
+                       pte = pte_clear_soft_dirty(pte);
+
                if (is_writable_migration_entry(entry))
                        pte = pte_mkwrite(pte, vma);
                else if (pte_swp_uffd_wp(old_pte))