mm/thp: fix __split_huge_pmd_locked() on shmem migration entry
Subsystems (4)
A message that is not in the archive (sent off-list or not yet received): [email protected]
PATCH 3/4 mm/thp: fix __split_huge_pmd_locked() on shmem migration entry
From: Hugh Dickins <[email protected]>
mainline inclusion
from mainline-v5.13-rc7
commit 99fa8a48203d62b3743d866fc48ef6abaee682be
category: bugfix
bugzilla: 110269
DTS: #87
CVE: NA
-------------------------------------------------
Patch series "mm/thp: fix THP splitting unmap BUGs and related", v10.
Here is v2 batch of long-standing THP bug fixes that I had not got around to sending before, but prompted now by Wang Yugui's report
https://lore.kernel.org/linux-mm/[email protected]/
Wang Yugui has tested a rollup of these fixes applied to 5.10.39, and they have done no harm, but have *not* fixed that issue: something more is needed and I have no idea of what.
This patch (of 7):
Stressing huge tmpfs page migration racing hole punch often crashed on the VM_BUG_ON(!pmd_present) in pmdp_huge_clear_flush(), with DEBUG_VM=y kernel; or shortly afterwards, on a bad dereference in __split_huge_pmd_locked() when DEBUG_VM=n. They forgot to allow for pmd migration entries in the non-anonymous case.
Full disclosure: those particular experiments were on a kernel with more relaxed mmap_lock and i_mmap_rwsem locking, and were not repeated on the vanilla kernel: it is conceivable that stricter locking happens to avoid those cases, or makes them less likely; but __split_huge_pmd_locked() already allowed for pmd migration entries when handling anonymous THPs, so this commit brings the shmem and file THP handling into line.
And while there: use old_pmd rather than _pmd, as in the following blocks; and make it clearer to the eye that the !vma_is_anonymous() block is self-contained, making an early return after accounting for unmapping.
Conflicts:
mm/huge_memory.c
--- mm/huge_memory.c | 27 ++++++++++++++++++--------- mm/pgtable-generic.c | 5 ++--- 2 files changed, 20 insertions(+), 12 deletions(-)
mm/huge_memory.c +18 -9
@@ -2199,7 +2199,7 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd,count_vm_event (THP_SPLIT_PMD );if (!vma_is_anonymous (vma)) { - _pmd =pmdp_huge_clear_flush_notify (vma, haddr, pmd); + old_pmd =pmdp_huge_clear_flush_notify (vma, haddr, pmd);/* * We are going to unmap this huge page. So * just go ahead and zap it
@@ -2208,16 +2208,25 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd,zap_deposited_table (mm, pmd);if (vma_is_dax (vma))return ; - page =pmd_page (_pmd); -if (!PageDirty (page) &&pmd_dirty (_pmd)) -set_page_dirty (page); -if (!PageReferenced (page) &&pmd_young (_pmd)) -SetPageReferenced (page); -page_remove_rmap (page, true); -put_page (page); +if (unlikely (is_pmd_migration_entry (old_pmd))) { +swp_entry_t entry; + + entry =pmd_to_swp_entry (old_pmd); + page =migration_entry_to_page (entry); + }else { + page =pmd_page (old_pmd); +if (!PageDirty (page) &&pmd_dirty (old_pmd)) +set_page_dirty (page); +if (!PageReferenced (page) &&pmd_young (old_pmd)) +SetPageReferenced (page); +page_remove_rmap (page, true); +put_page (page); + }add_mm_counter (mm,MM_FILEPAGES , -HPAGE_PMD_NR );return ; - }else if (pmd_trans_huge (*pmd) &&is_huge_zero_pmd (*pmd)) { + } + +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
mm/pgtable-generic.c +2 -3
@@ -125,9 +125,8 @@ pmd_t pmdp_huge_clear_flush(struct vm_area_struct *vma, unsigned long address, {pmd_t pmd;VM_BUG_ON (address & ~HPAGE_PMD_MASK ); -VM_BUG_ON (!pmd_present (*pmdp)); -/* Below assumes pmd_present() is true */ -VM_BUG_ON (!pmd_trans_huge (*pmdp) && !pmd_devmap (*pmdp)); +VM_BUG_ON (pmd_present (*pmdp) && !pmd_trans_huge (*pmdp) && + !pmd_devmap (*pmdp)); pmd =pmdp_huge_get_and_clear (vma->vm_mm, address, pmdp);flush_pmd_tlb_range (vma, address, address +HPAGE_PMD_SIZE );return pmd;
Signature · 1 line
2.18.0.huawei.25
sorry, please ignore this
Quoted 135 lines · Chen Wandun
From: Hugh Dickins <[email protected]>
mainline inclusion
from mainline-v5.13-rc7
commit99fa8a48203d62b3743d866fc48ef6abaee682be
category: bugfix
bugzilla: 110269
DTS: #87
CVE: NA-------------------------------------------------
Patch series "mm/thp: fix THP splitting unmap BUGs and related", v10.
Here is v2 batch of long-standing THP bug fixes that I had not got around to sending before, but prompted now by Wang Yugui's report
https://lore.kernel.org/linux-mm/[email protected]/Wang Yugui has tested a rollup of these fixes applied to 5.10.39, and they have done no harm, but have *not* fixed that issue: something more is needed and I have no idea of what.
This patch (of 7):
Stressing huge tmpfs page migration racing hole punch often crashed on the
VM_BUG_ON(!pmd_present)inpmdp_huge_clear_flush(), with DEBUG_VM=y kernel; or shortly afterwards, on a bad dereference in__split_huge_pmd_locked()when DEBUG_VM=n. They forgot to allow for pmd migration entries in the non-anonymous case.Full disclosure: those particular experiments were on a kernel with more relaxed
mmap_lockandi_mmap_rwsemlocking, and were not repeated on the vanilla kernel: it is conceivable that stricter locking happens to avoid those cases, or makes them less likely; but__split_huge_pmd_locked()already allowed for pmd migration entries when handling anonymous THPs, so this commit brings the shmem and file THP handling into line.And while there: use
old_pmdrather than_pmd, as in the following blocks; and make it clearer to the eye that the !vma_is_anonymous() block is self-contained, making an early return after accounting for unmapping.Fixes e71769ae5260 ("mm: enable thp migration for shmem thp")Signed-off-by Hugh Dickins <[email protected]>Cc Kirill A. Shutemov <[email protected]>Cc Yang Shi <[email protected]>Cc Wang Yugui <[email protected]>Cc "Matthew Wilcox (Oracle)" <[email protected]>Cc Naoya Horiguchi <[email protected]>Cc Alistair Popple <[email protected]>Cc Ralph Campbell <[email protected]>Cc Zi Yan <[email protected]>Cc Miaohe Lin <[email protected]>Cc Minchan Kim <[email protected]>Cc Jue Wang <[email protected]>Cc Peter Xu <[email protected]>Cc Jan Kara <[email protected]>Cc Shakeel Butt <[email protected]>Cc Oscar Salvador <[email protected]>Cc <[email protected]>Signed-off-by Andrew Morton <[email protected]>Signed-off-by Linus Torvalds <[email protected]>Conflicts:
mm/huge_memory.cSigned-off-by Chen Wandun <[email protected]>--- mm/huge_memory.c | 27 ++++++++++++++++++--------- mm/pgtable-generic.c | 5 ++--- 2 files changed, 20 insertions(+), 12 deletions(-)mm/huge_memory.c +18
-9@@ -2199,7 +2199,7 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd,count_vm_event (THP_SPLIT_PMD );if (!vma_is_anonymous (vma)) { - _pmd =pmdp_huge_clear_flush_notify (vma, haddr, pmd); + old_pmd =pmdp_huge_clear_flush_notify (vma, haddr, pmd);/* * We are going to unmap this huge page. So * just go ahead and zap it @@ -2208,16 +2208,25 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd,zap_deposited_table (mm, pmd);if (vma_is_dax (vma))return ; - page =pmd_page (_pmd); -if (!PageDirty (page) &&pmd_dirty (_pmd)) -set_page_dirty (page); -if (!PageReferenced (page) &&pmd_young (_pmd)) -SetPageReferenced (page); -page_remove_rmap (page, true); -put_page (page); +if (unlikely (is_pmd_migration_entry (old_pmd))) { +swp_entry_t entry; + + entry =pmd_to_swp_entry (old_pmd); + page =migration_entry_to_page (entry); + }else { + page =pmd_page (old_pmd); +if (!PageDirty (page) &&pmd_dirty (old_pmd)) +set_page_dirty (page); +if (!PageReferenced (page) &&pmd_young (old_pmd)) +SetPageReferenced (page); +page_remove_rmap (page, true); +put_page (page); + }add_mm_counter (mm,MM_FILEPAGES , -HPAGE_PMD_NR );return ; - }else if (pmd_trans_huge (*pmd) &&is_huge_zero_pmd (*pmd)) { + } + +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 mm/pgtable-generic.c +2
-3@@ -125,9 +125,8 @@ pmd_t pmdp_huge_clear_flush(struct vm_area_struct *vma, unsigned long address, {pmd_t pmd;VM_BUG_ON (address & ~HPAGE_PMD_MASK ); -VM_BUG_ON (!pmd_present (*pmdp)); -/* Below assumes pmd_present() is true */ -VM_BUG_ON (!pmd_trans_huge (*pmdp) && !pmd_devmap (*pmdp)); +VM_BUG_ON (pmd_present (*pmdp) && !pmd_trans_huge (*pmdp) && + !pmd_devmap (*pmdp)); pmd =pmdp_huge_get_and_clear (vma->vm_mm, address, pmdp);flush_pmd_tlb_range (vma, address, address +HPAGE_PMD_SIZE );return pmd;