Skip to content

Commit 8410996

Browse files
Liu Shixingregkh
authored andcommitted
mm: hugetlb: independent PMD page table shared count
commit 59d9094 upstream. The folio refcount may be increased unexpectly through try_get_folio() by caller such as split_huge_pages. In huge_pmd_unshare(), we use refcount to check whether a pmd page table is shared. The check is incorrect if the refcount is increased by the above caller, and this can cause the page table leaked: BUG: Bad page state in process sh pfn:109324 page: refcount:0 mapcount:0 mapping:0000000000000000 index:0x66 pfn:0x109324 flags: 0x17ffff800000000(node=0|zone=2|lastcpupid=0xfffff) page_type: f2(table) raw: 017ffff800000000 0000000000000000 0000000000000000 0000000000000000 raw: 0000000000000066 0000000000000000 00000000f2000000 0000000000000000 page dumped because: nonzero mapcount ... CPU: 31 UID: 0 PID: 7515 Comm: sh Kdump: loaded Tainted: G B 6.13.0-rc2master+ #7 Tainted: [B]=BAD_PAGE Hardware name: QEMU KVM Virtual Machine, BIOS 0.0.0 02/06/2015 Call trace: show_stack+0x20/0x38 (C) dump_stack_lvl+0x80/0xf8 dump_stack+0x18/0x28 bad_page+0x8c/0x130 free_page_is_bad_report+0xa4/0xb0 free_unref_page+0x3cc/0x620 __folio_put+0xf4/0x158 split_huge_pages_all+0x1e0/0x3e8 split_huge_pages_write+0x25c/0x2d8 full_proxy_write+0x64/0xd8 vfs_write+0xcc/0x280 ksys_write+0x70/0x110 __arm64_sys_write+0x24/0x38 invoke_syscall+0x50/0x120 el0_svc_common.constprop.0+0xc8/0xf0 do_el0_svc+0x24/0x38 el0_svc+0x34/0x128 el0t_64_sync_handler+0xc8/0xd0 el0t_64_sync+0x190/0x198 The issue may be triggered by damon, offline_page, page_idle, etc, which will increase the refcount of page table. 1. The page table itself will be discarded after reporting the "nonzero mapcount". 2. The HugeTLB page mapped by the page table miss freeing since we treat the page table as shared and a shared page table will not be unmapped. Fix it by introducing independent PMD page table shared count. As described by comment, pt_index/pt_mm/pt_frag_refcount are used for s390 gmap, x86 pgds and powerpc, pt_share_count is used for x86/arm64/riscv pmds, so we can reuse the field as pt_share_count. Link: https://lkml.kernel.org/r/[email protected] Fixes: 39dde65 ("[PATCH] shared page table for hugetlb page") Signed-off-by: Liu Shixin <[email protected]> Cc: Kefeng Wang <[email protected]> Cc: Ken Chen <[email protected]> Cc: Muchun Song <[email protected]> Cc: Nanyong Sun <[email protected]> Cc: Jane Chu <[email protected]> Cc: <[email protected]> Signed-off-by: Andrew Morton <[email protected]> [backport note: struct ptdesc did not exist yet, stuff it equivalently into struct page instead] Signed-off-by: Jann Horn <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 366298f commit 8410996

File tree

3 files changed

+14
-10
lines changed

3 files changed

+14
-10
lines changed

include/linux/mm.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2352,6 +2352,9 @@ static inline bool pgtable_pmd_page_ctor(struct page *page)
23522352
if (!pmd_ptlock_init(page))
23532353
return false;
23542354
__SetPageTable(page);
2355+
#ifdef CONFIG_ARCH_WANT_HUGE_PMD_SHARE
2356+
atomic_set(&page->pt_share_count, 0);
2357+
#endif
23552358
inc_lruvec_page_state(page, NR_PAGETABLE);
23562359
return true;
23572360
}

include/linux/mm_types.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,9 @@ struct page {
166166
union {
167167
struct mm_struct *pt_mm; /* x86 pgds only */
168168
atomic_t pt_frag_refcount; /* powerpc */
169+
#ifdef CONFIG_ARCH_WANT_HUGE_PMD_SHARE
170+
atomic_t pt_share_count;
171+
#endif
169172
};
170173
#if ALLOC_SPLIT_PTLOCKS
171174
spinlock_t *ptl;

mm/hugetlb.c

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6046,7 +6046,7 @@ pte_t *huge_pmd_share(struct mm_struct *mm, struct vm_area_struct *vma,
60466046
spte = huge_pte_offset(svma->vm_mm, saddr,
60476047
vma_mmu_pagesize(svma));
60486048
if (spte) {
6049-
get_page(virt_to_page(spte));
6049+
atomic_inc(&virt_to_page(spte)->pt_share_count);
60506050
break;
60516051
}
60526052
}
@@ -6061,7 +6061,7 @@ pte_t *huge_pmd_share(struct mm_struct *mm, struct vm_area_struct *vma,
60616061
(pmd_t *)((unsigned long)spte & PAGE_MASK));
60626062
mm_inc_nr_pmds(mm);
60636063
} else {
6064-
put_page(virt_to_page(spte));
6064+
atomic_dec(&virt_to_page(spte)->pt_share_count);
60656065
}
60666066
spin_unlock(ptl);
60676067
out:
@@ -6072,29 +6072,27 @@ pte_t *huge_pmd_share(struct mm_struct *mm, struct vm_area_struct *vma,
60726072
/*
60736073
* unmap huge page backed by shared pte.
60746074
*
6075-
* Hugetlb pte page is ref counted at the time of mapping. If pte is shared
6076-
* indicated by page_count > 1, unmap is achieved by clearing pud and
6077-
* decrementing the ref count. If count == 1, the pte page is not shared.
6078-
*
6079-
* Called with page table lock held and i_mmap_rwsem held in write mode.
6075+
* Called with page table lock held.
60806076
*
60816077
* returns: 1 successfully unmapped a shared pte page
60826078
* 0 the underlying pte page is not shared, or it is the last user
60836079
*/
60846080
int huge_pmd_unshare(struct mm_struct *mm, struct vm_area_struct *vma,
60856081
unsigned long *addr, pte_t *ptep)
60866082
{
6083+
unsigned long sz = huge_page_size(hstate_vma(vma));
60876084
pgd_t *pgd = pgd_offset(mm, *addr);
60886085
p4d_t *p4d = p4d_offset(pgd, *addr);
60896086
pud_t *pud = pud_offset(p4d, *addr);
60906087

60916088
i_mmap_assert_write_locked(vma->vm_file->f_mapping);
6092-
BUG_ON(page_count(virt_to_page(ptep)) == 0);
6093-
if (page_count(virt_to_page(ptep)) == 1)
6089+
if (sz != PMD_SIZE)
6090+
return 0;
6091+
if (!atomic_read(&virt_to_page(ptep)->pt_share_count))
60946092
return 0;
60956093

60966094
pud_clear(pud);
6097-
put_page(virt_to_page(ptep));
6095+
atomic_dec(&virt_to_page(ptep)->pt_share_count);
60986096
mm_dec_nr_pmds(mm);
60996097
/*
61006098
* This update of passed address optimizes loops sequentially

0 commit comments

Comments
 (0)