Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

mm/smaps: fix race between smaps_hugetlb_range and migration

smaps_hugetlb_range() handles the pte without holdling ptl, and may be
concurrenct with migration, leaing to BUG_ON in pfn_swap_entry_to_page().
The race is as follows.

smaps_hugetlb_range migrate_pages
huge_ptep_get
remove_migration_ptes
folio_unlock
pfn_swap_entry_folio
BUG_ON

To fix it, hold ptl lock in smaps_hugetlb_range().

Link: https://lkml.kernel.org/r/20250724090958.455887-1-tujinjiang@huawei.com
Link: https://lkml.kernel.org/r/20250724090958.455887-2-tujinjiang@huawei.com
Fixes: 25ee01a2fca0 ("mm: hugetlb: proc: add hugetlb-related fields to /proc/PID/smaps")
Signed-off-by: Jinjiang Tu <tujinjiang@huawei.com>
Acked-by: David Hildenbrand <david@redhat.com>
Cc: Andrei Vagin <avagin@gmail.com>
Cc: Andrii Nakryiko <andrii@kernel.org>
Cc: Baolin Wang <baolin.wang@linux.alibaba.com>
Cc: Brahmajit Das <brahmajit.xyz@gmail.com>
Cc: Catalin Marinas <catalin.marinas@arm.com>
Cc: Christophe Leroy <christophe.leroy@csgroup.eu>
Cc: David Rientjes <rientjes@google.com>
Cc: Dev Jain <dev.jain@arm.com>
Cc: Hugh Dickins <hughd@google.com>
Cc: Joern Engel <joern@logfs.org>
Cc: Kefeng Wang <wangkefeng.wang@huawei.com>
Cc: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
Cc: Michal Hocko <mhocko@suse.com>
Cc: Ryan Roberts <ryan.roberts@arm.com>
Cc: Thiago Jung Bauermann <thiago.bauermann@linaro.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

authored by

Jinjiang Tu and committed by
Andrew Morton
45d19b4b 366a4532

+5 -1
+5 -1
fs/proc/task_mmu.c
··· 1148 1148 { 1149 1149 struct mem_size_stats *mss = walk->private; 1150 1150 struct vm_area_struct *vma = walk->vma; 1151 - pte_t ptent = huge_ptep_get(walk->mm, addr, pte); 1152 1151 struct folio *folio = NULL; 1153 1152 bool present = false; 1153 + spinlock_t *ptl; 1154 + pte_t ptent; 1154 1155 1156 + ptl = huge_pte_lock(hstate_vma(vma), walk->mm, pte); 1157 + ptent = huge_ptep_get(walk->mm, addr, pte); 1155 1158 if (pte_present(ptent)) { 1156 1159 folio = page_folio(pte_page(ptent)); 1157 1160 present = true; ··· 1173 1170 else 1174 1171 mss->private_hugetlb += huge_page_size(hstate_vma(vma)); 1175 1172 } 1173 + spin_unlock(ptl); 1176 1174 return 0; 1177 1175 } 1178 1176 #else