]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
mm: migrate_device: fix pte_pfn/pte_dirty called on non-present PTE
authorKefeng Wang <wangkefeng.wang@huawei.com>
Mon, 6 Jul 2026 11:19:58 +0000 (19:19 +0800)
committerAndrew Morton <akpm@linux-foundation.org>
Tue, 21 Jul 2026 00:41:27 +0000 (17:41 -0700)
pte_pfn() and pte_dirty() have undefined behaviour when called on a
non-present PTE. In migrate_vma_collect_pmd(), these functions may be
invoked on non-present entries (e.g., device-private entries), leading
to potential crashes from pte_pfn() or incorrect dirty folio accounting
from pte_dirty(). Fix both by guarding with pte_present() checks.

Link: https://lore.kernel.org/20260708003955.4024340-1-wangkefeng.wang@huawei.com
Link: https://lore.kernel.org/20260706111958.3649651-1-wangkefeng.wang@huawei.com
Fixes: fd35ca3d12cc ("mm/migrate_device.c: copy pte dirty bit to page")
Fixes: 6c287605fd56 ("mm: remember exclusively mapped anonymous pages with PG_anon_exclusive")
Signed-off-by: Kefeng Wang <wangkefeng.wang@huawei.com>
Reviewed-by: Balbir Singh <balbirs@nvidia.com>
Acked-by: Zi Yan <ziy@nvidia.com>
Cc: Alistair Popple <apopple@nvidia.com>
Cc: Byungchul Park <byungchul@sk.com>
Cc: David Hildenbrand <david@kernel.org>
Cc: Gregory Price <gourry@gourry.net>
Cc: "Huang, Ying" <ying.huang@linux.alibaba.com>
Cc: Joshua Hahn <joshua.hahnjy@gmail.com>
Cc: Matthew Brost <matthew.brost@intel.com>
Cc: Rakie Kim <rakie.kim@sk.com>
Cc: Ying Huang <ying.huang@linux.alibaba.com>
Cc: <stable@vger.kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
mm/migrate_device.c

index 554754eb26ff2f0a59b8cbd20f9f6a187005e7c7..908d2d4ec43ac93fee3df1c39e4fcc2a1858d79c 100644 (file)
@@ -401,7 +401,8 @@ again:
                        bool anon_exclusive;
                        pte_t swp_pte;
 
-                       flush_cache_page(vma, addr, pte_pfn(pte));
+                       if (pte_present(pte))
+                               flush_cache_page(vma, addr, pte_pfn(pte));
                        anon_exclusive = folio_test_anon(folio) &&
                                          PageAnonExclusive(page);
                        if (anon_exclusive) {
@@ -422,7 +423,7 @@ again:
                        migrate->cpages++;
 
                        /* Set the dirty flag on the folio now the pte is gone. */
-                       if (pte_dirty(pte))
+                       if (pte_present(pte) && pte_dirty(pte))
                                folio_mark_dirty(folio);
 
                        /* Setup special migration page table entry */