Skip to content

Commit a8f4ea1

Browse files
Hugh DickinsSasha Levin
authored andcommitted
mm/thp: fix __split_huge_pmd_locked() on shmem migration entry
[ Upstream commit 99fa8a4 ] 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/20210412180659.B9E3.409509F4@e16-tech.com/ 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. Link: https://lkml.kernel.org/r/af88612-1473-2eaa-903-8d1a448b26@google.com Link: https://lkml.kernel.org/r/dd221a99-efb3-cd1d-6256-7e646af29314@google.com Fixes: e71769a ("mm: enable thp migration for shmem thp") Signed-off-by: Hugh Dickins <hughd@google.com> Cc: Kirill A. Shutemov <kirill.shutemov@linux.intel.com> Cc: Yang Shi <shy828301@gmail.com> Cc: Wang Yugui <wangyugui@e16-tech.com> Cc: "Matthew Wilcox (Oracle)" <willy@infradead.org> Cc: Naoya Horiguchi <naoya.horiguchi@nec.com> Cc: Alistair Popple <apopple@nvidia.com> Cc: Ralph Campbell <rcampbell@nvidia.com> Cc: Zi Yan <ziy@nvidia.com> Cc: Miaohe Lin <linmiaohe@huawei.com> Cc: Minchan Kim <minchan@kernel.org> Cc: Jue Wang <juew@google.com> Cc: Peter Xu <peterx@redhat.com> Cc: Jan Kara <jack@suse.cz> Cc: Shakeel Butt <shakeelb@google.com> Cc: Oscar Salvador <osalvador@suse.de> Cc: <stable@vger.kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org> Note on stable backport: this commit made intervening cleanups in pmdp_huge_clear_flush() redundant: here it's rediffed to skip them. Signed-off-by: Hugh Dickins <hughd@google.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 32f954e commit a8f4ea1

2 files changed

Lines changed: 20 additions & 11 deletions

File tree

‎mm/huge_memory.c‎

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2031,7 +2031,7 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd,
20312031
count_vm_event(THP_SPLIT_PMD);
20322032

20332033
if (!vma_is_anonymous(vma)) {
2034-
_pmd = pmdp_huge_clear_flush_notify(vma, haddr, pmd);
2034+
old_pmd = pmdp_huge_clear_flush_notify(vma, haddr, pmd);
20352035
/*
20362036
* We are going to unmap this huge page. So
20372037
* just go ahead and zap it
@@ -2040,16 +2040,25 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd,
20402040
zap_deposited_table(mm, pmd);
20412041
if (vma_is_special_huge(vma))
20422042
return;
2043-
page = pmd_page(_pmd);
2044-
if (!PageDirty(page) && pmd_dirty(_pmd))
2045-
set_page_dirty(page);
2046-
if (!PageReferenced(page) && pmd_young(_pmd))
2047-
SetPageReferenced(page);
2048-
page_remove_rmap(page, true);
2049-
put_page(page);
2043+
if (unlikely(is_pmd_migration_entry(old_pmd))) {
2044+
swp_entry_t entry;
2045+
2046+
entry = pmd_to_swp_entry(old_pmd);
2047+
page = migration_entry_to_page(entry);
2048+
} else {
2049+
page = pmd_page(old_pmd);
2050+
if (!PageDirty(page) && pmd_dirty(old_pmd))
2051+
set_page_dirty(page);
2052+
if (!PageReferenced(page) && pmd_young(old_pmd))
2053+
SetPageReferenced(page);
2054+
page_remove_rmap(page, true);
2055+
put_page(page);
2056+
}
20502057
add_mm_counter(mm, mm_counter_file(page), -HPAGE_PMD_NR);
20512058
return;
2052-
} else if (pmd_trans_huge(*pmd) && is_huge_zero_pmd(*pmd)) {
2059+
}
2060+
2061+
if (pmd_trans_huge(*pmd) && is_huge_zero_pmd(*pmd)) {
20532062
/*
20542063
* FIXME: Do we want to invalidate secondary mmu by calling
20552064
* mmu_notifier_invalidate_range() see comments below inside

‎mm/pgtable-generic.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,8 @@ pmd_t pmdp_huge_clear_flush(struct vm_area_struct *vma, unsigned long address,
135135
{
136136
pmd_t pmd;
137137
VM_BUG_ON(address & ~HPAGE_PMD_MASK);
138-
VM_BUG_ON((pmd_present(*pmdp) && !pmd_trans_huge(*pmdp) &&
139-
!pmd_devmap(*pmdp)) || !pmd_present(*pmdp));
138+
VM_BUG_ON(pmd_present(*pmdp) && !pmd_trans_huge(*pmdp) &&
139+
!pmd_devmap(*pmdp));
140140
pmd = pmdp_huge_get_and_clear(vma->vm_mm, address, pmdp);
141141
flush_pmd_tlb_range(vma, address, address + HPAGE_PMD_SIZE);
142142
return pmd;

0 commit comments

Comments
 (0)