mm/thp: fix __split_huge_pmd_locked() for migration PMD
Subsystems (4)
PATCH v3 1/1 mm/thp: fix __split_huge_pmd_locked() for migration PMD
A migrating transparent huge page has to already be unmapped. Otherwise, the page could be modified while it is being copied to a new page and data could be lost. The function __split_huge_pmd() checks for a PMD migration entry before calling __split_huge_pmd_locked() leading one to think that __split_huge_pmd_locked() can handle splitting a migrating PMD. However, the code always increments the page->_mapcount and adjusts the memory control group accounting assuming the page is mapped. Also, if the PMD entry is a migration PMD entry, the call to is_huge_zero_pmd(*pmd) is incorrect because it calls pmd_pfn(pmd) instead of migration_entry_to_pfn(pmd_to_swp_entry(pmd)).
Fix these problems by checking for a PMD migration entry.
---
No changes in v3 to this patch, just added reviewed-by and fixes to the change log and sending this as a separate patch from the rest of the series ("mm/hmm/nouveau: add THP migration to migrate_vma_*"). I'll hold off resending the series without this patch unless there are changes needed.
mm/huge_memory.c | 42 +++++++++++++++++++++++------------------- 1 file changed, 23 insertions(+), 19 deletions(-)
mm/huge_memory.c +23 -19
@@ -2023,7 +2023,7 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd,put_page (page);add_mm_counter (mm,mm_counter_file (page), -HPAGE_PMD_NR );return ; - }else if (is_huge_zero_pmd (*pmd)) { + }else if (pmd_trans_huge (*pmd) &&is_huge_zero_pmd (*pmd)) {/* * FIXME: Do we want to invalidate secondary mmu by calling * mmu_notifier_invalidate_range() see comments below inside
@@ -2117,30 +2117,34 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd, pte =pte_offset_map (&_pmd, addr);BUG_ON (!pte_none (*pte));set_pte_at (mm, addr, pte, entry); -atomic_inc (&page[i]._mapcount); -pte_unmap (pte); - } - -/* -* Set PG_double_map before dropping compound_mapcount to avoid -* false-negative page_mapped(). -*/ -if (compound_mapcount (page) >1 && !TestSetPageDoubleMap (page)) { -for (i =0 ; i <HPAGE_PMD_NR ; i++) +if (!pmd_migration)atomic_inc (&page[i]._mapcount); +pte_unmap (pte); } -lock_page_memcg (page); -if (atomic_add_negative (-1 ,compound_mapcount_ptr (page))) { -/* Last compound_mapcount is gone. */ -__dec_lruvec_page_state (page,NR_ANON_THPS ); -if (TestClearPageDoubleMap (page)) { -/* No need in mapcount reference anymore */ +if (!pmd_migration) { +/* +* Set PG_double_map before dropping compound_mapcount to avoid +* false-negative page_mapped(). +*/ +if (compound_mapcount (page) >1 && + !TestSetPageDoubleMap (page)) {for (i =0 ; i <HPAGE_PMD_NR ; i++) -atomic_dec (&page[i]._mapcount); +atomic_inc (&page[i]._mapcount); + } + +lock_page_memcg (page); +if (atomic_add_negative (-1 ,compound_mapcount_ptr (page))) { +/* Last compound_mapcount is gone. */ +__dec_lruvec_page_state (page,NR_ANON_THPS ); +if (TestClearPageDoubleMap (page)) { +/* No need in mapcount reference anymore */ +for (i =0 ; i <HPAGE_PMD_NR ; i++) +atomic_dec (&page[i]._mapcount); + } } +unlock_page_memcg (page); } -unlock_page_memcg (page);smp_wmb ();/* make pte visible before pmd */ pmd_populate (mm, pmd, pgtable);
Signature · 1 line
2.20.1
Sasha Levin
maintainerHi
[This is an automated email]
This commit has been processed because it contains a "Fixes:" tag fixing commit: 84c3fc4e9c56 ("mm: thp: check pmd migration entry in common path").
The bot has tested the following trees: v5.8.6, v5.4.62, v4.19.143, v4.14.196.
v5.8.6: Build OK!
v5.4.62: Failed to apply! Possible dependencies:
0d1c20722ab3 ("mm: memcontrol: switch to native NR_FILE_PAGES and NR_SHMEM counters") 3fba69a56e16 ("mm: memcontrol: drop @compound parameter from memcg charging API") 468c398233da ("mm: memcontrol: switch to native NR_ANON_THPS counter") 72981e0e7b60 ("userfaultfd: wp: add UFFDIO_COPY_MODE_WP")
83d116c53058 ("mm: fix double page fault on arm64 if PTE_AF is cleared") 85b9f46e8ea4 ("mm, thp: track fallbacks due to failed memcg charges separately") 92855270ff08 ("mm/memcontrol.c: cleanup some useless code")
be5d0a74c62d ("mm: memcontrol: switch to native NR_ANON_MAPPED counter") c23a0c99793f ("mm/migrate: clean up some minor coding style") dcdf11ee1441 ("mm, shmem: add vmstat for hugepage fallback") f4129ea3591a ("mm: fix NUMA node file count error in replace_page_cache()") ffe945e633b5 ("khugepaged: do not stop collapse if less than half PTEs are referenced")
v4.19.143: Failed to apply! Possible dependencies:
0ac261042084 ("x86/irq/64: Init hardirq_stack_ptr during CPU hotplug")
0d1c20722ab3 ("mm: memcontrol: switch to native NR_FILE_PAGES and NR_SHMEM counters")
117ed4548541 ("x86/irq/64: Remove stack overflow debug code")
231c4846b106 ("x86/irq/32: Make irq stack a character array")
30842211506e ("x86/exceptions: Remove unused stack defines on 32bit")
39656e83dab9 ("mm: lift the x86_32 PAE version of gup_get_pte to common code")
468c398233da ("mm: memcontrol: switch to native NR_ANON_THPS counter")
4f44b8f0b33b ("x86/irq/64: Remove a hardcoded irq_stack_union access")
66c7ceb47f62 ("x86/irq/32: Handle irq stack allocation failure proper")
758a2e312228 ("x86/irq/64: Rename irq_stack_ptr to hardirq_stack_ptr")
7ac870747988 ("x86/vdso: Switch to generic vDSO implementation")
99cb0dbd47a1 ("mm,thp: add read-only THP support for (non-shmem) FS")
a754fe2b76d1 ("x86/irq/32: Rename hard/softirq_stack to hard/softirq_stack_ptr")
aa641c287b2f ("x86/irq/32: Define IRQ_STACK_SIZE")
be5d0a74c62d ("mm: memcontrol: switch to native NR_ANON_MAPPED counter")
df835e7083be ("x86/irq/64: Sanitize the top/bottom confusion")
e6401c130931 ("x86/irq/64: Split the IRQ stack into its own pages")v4.14.196: Failed to apply! Possible dependencies:
050e9baa9dc9 ("Kbuild: rename CC_STACKPROTECTOR[_STRONG] config variables") 0d1c20722ab3 ("mm: memcontrol: switch to native NR_FILE_PAGES and NR_SHMEM counters") 117ed4548541 ("x86/irq/64: Remove stack overflow debug code")
152e93af3cfe ("mm, thp: Do not make pmd/pud dirty without a reason") 2a61f4747eea ("stack-protector: test compiler capability in Kconfig and drop AUTO mode") 2b8383927525 ("Makefile: move stack-protector compiler breakage test earlier") 2bc2f688fdf8 ("Makefile: move stack-protector availability out of Kconfig") 39656e83dab9 ("mm: lift the x86_32 PAE version of gup_get_pte to common code") 44c6dc940b19 ("Makefile: introduce CONFIG_CC_STACKPROTECTOR_AUTO") 4645b9fe84bf ("mm/mmu_notifier: avoid call to invalidate_range() in range_end()") 468c398233da ("mm: memcontrol: switch to native NR_ANON_THPS counter") 59c66c5f8c4f ("mm: factor out page cache page freeing into a separate function") 5ecc4d852c03 ("mm: factor out checks and accounting from __delete_from_page_cache()") 76253fbc8fbf ("mm: move accounting updates before page_cache_tree_delete()") 7ac870747988 ("x86/vdso: Switch to generic vDSO implementation") 8373b7d9d174 ("Documentation: kconfig: add recommended way to describe compiler support") 99cb0dbd47a1 ("mm,thp: add read-only THP support for (non-shmem) FS") be5d0a74c62d ("mm: memcontrol: switch to native NR_ANON_MAPPED counter") e6401c130931 ("x86/irq/64: Split the IRQ stack into its own pages") fadae2953072 ("thp: use mm_file_counter to determine update which rss counter")
NOTE: The patch will not be queued to stable trees until it is upstream.
How should we proceed with this patch?
Signature · 2 lines
Thanks Sasha