Skip to content

Commit b285b1f

Browse files
jchu314atgithubgregkh
authored andcommitted
mm/hugetlb: fix copy_hugetlb_page_range() to use ->pt_share_count
commit 14967a9 upstream. commit 59d9094 ("mm: hugetlb: independent PMD page table shared count") introduced ->pt_share_count dedicated to hugetlb PMD share count tracking, but omitted fixing copy_hugetlb_page_range(), leaving the function relying on page_count() for tracking that no longer works. When lazy page table copy for hugetlb is disabled, that is, revert commit bcd51a3 ("hugetlb: lazy page table copies in fork()") fork()'ing with hugetlb PMD sharing quickly lockup - [ 239.446559] watchdog: BUG: soft lockup - CPU#75 stuck for 27s! [ 239.446611] RIP: 0010:native_queued_spin_lock_slowpath+0x7e/0x2e0 [ 239.446631] Call Trace: [ 239.446633] <TASK> [ 239.446636] _raw_spin_lock+0x3f/0x60 [ 239.446639] copy_hugetlb_page_range+0x258/0xb50 [ 239.446645] copy_page_range+0x22b/0x2c0 [ 239.446651] dup_mmap+0x3e2/0x770 [ 239.446654] dup_mm.constprop.0+0x5e/0x230 [ 239.446657] copy_process+0xd17/0x1760 [ 239.446660] kernel_clone+0xc0/0x3e0 [ 239.446661] __do_sys_clone+0x65/0xa0 [ 239.446664] do_syscall_64+0x82/0x930 [ 239.446668] ? count_memcg_events+0xd2/0x190 [ 239.446671] ? syscall_trace_enter+0x14e/0x1f0 [ 239.446676] ? syscall_exit_work+0x118/0x150 [ 239.446677] ? arch_exit_to_user_mode_prepare.constprop.0+0x9/0xb0 [ 239.446681] ? clear_bhb_loop+0x30/0x80 [ 239.446684] ? clear_bhb_loop+0x30/0x80 [ 239.446686] entry_SYSCALL_64_after_hwframe+0x76/0x7e There are two options to resolve the potential latent issue: 1. warn against PMD sharing in copy_hugetlb_page_range(), 2. fix it. This patch opts for the second option. While at it, simplify the comment, the details are not actually relevant anymore. Link: https://lkml.kernel.org/r/20250916004520.1604530-1-jane.chu@oracle.com Fixes: 59d9094 ("mm: hugetlb: independent PMD page table shared count") Signed-off-by: Jane Chu <jane.chu@oracle.com> Reviewed-by: Harry Yoo <harry.yoo@oracle.com> Acked-by: Oscar Salvador <osalvador@suse.de> Acked-by: David Hildenbrand <david@redhat.com> Cc: Jann Horn <jannh@google.com> Cc: Liu Shixin <liushixin2@huawei.com> Cc: Muchun Song <muchun.song@linux.dev> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: David Hildenbrand (Arm) <david@kernel.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 56ef68a commit b285b1f

2 files changed

Lines changed: 10 additions & 10 deletions

File tree

include/linux/mm_types.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,11 @@ static inline int ptdesc_pmd_pts_count(struct ptdesc *ptdesc)
540540
{
541541
return atomic_read(&ptdesc->pt_share_count);
542542
}
543+
544+
static inline bool ptdesc_pmd_is_shared(struct ptdesc *ptdesc)
545+
{
546+
return !!ptdesc_pmd_pts_count(ptdesc);
547+
}
543548
#else
544549
static inline void ptdesc_pmd_pts_init(struct ptdesc *ptdesc)
545550
{

mm/hugetlb.c

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5258,18 +5258,13 @@ int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src,
52585258
break;
52595259
}
52605260

5261-
/*
5262-
* If the pagetables are shared don't copy or take references.
5263-
*
5264-
* dst_pte == src_pte is the common case of src/dest sharing.
5265-
* However, src could have 'unshared' and dst shares with
5266-
* another vma. So page_count of ptep page is checked instead
5267-
* to reliably determine whether pte is shared.
5268-
*/
5269-
if (page_count(virt_to_page(dst_pte)) > 1) {
5261+
#ifdef CONFIG_HUGETLB_PMD_PAGE_TABLE_SHARING
5262+
/* If the pagetables are shared, there is nothing to do */
5263+
if (ptdesc_pmd_is_shared(virt_to_ptdesc(dst_pte))) {
52705264
addr |= last_addr_mask;
52715265
continue;
52725266
}
5267+
#endif
52735268

52745269
dst_ptl = huge_pte_lock(h, dst, dst_pte);
52755270
src_ptl = huge_pte_lockptr(h, src, src_pte);
@@ -7270,7 +7265,7 @@ int huge_pmd_unshare(struct mm_struct *mm, struct vm_area_struct *vma,
72707265
hugetlb_vma_assert_locked(vma);
72717266
if (sz != PMD_SIZE)
72727267
return 0;
7273-
if (!ptdesc_pmd_pts_count(virt_to_ptdesc(ptep)))
7268+
if (!ptdesc_pmd_is_shared(virt_to_ptdesc(ptep)))
72747269
return 0;
72757270

72767271
pud_clear(pud);

0 commit comments

Comments
 (0)