Skip to content

Commit aa5a10b

Browse files
tujinjiang11akpm00
authored andcommitted
fs/proc/task_mmu: hold PTL in pagemap_hugetlb_range and gather_hugetlb_stats
Hold PTL in pagemap_hugetlb_range() and gather_hugetlb_stats() to avoid operating on stale page, as pagemap_pmd_range() and gather_pte_stats() have done. Link: https://lkml.kernel.org/r/20250724090958.455887-3-tujinjiang@huawei.com Signed-off-by: Jinjiang Tu <tujinjiang@huawei.com> Acked-by: David Hildenbrand <david@redhat.com> Cc: Andrei Vagin <avagin@gmail.com> Cc: Andrii Nakryiko <andrii@kernel.org> Cc: Baolin Wang <baolin.wang@linux.alibaba.com> Cc: Brahmajit Das <brahmajit.xyz@gmail.com> Cc: Catalin Marinas <catalin.marinas@arm.com> Cc: Christophe Leroy <christophe.leroy@csgroup.eu> Cc: David Rientjes <rientjes@google.com> Cc: Dev Jain <dev.jain@arm.com> Cc: Hugh Dickins <hughd@google.com> Cc: Joern Engel <joern@logfs.org> Cc: Kefeng Wang <wangkefeng.wang@huawei.com> Cc: Lorenzo Stoakes <lorenzo.stoakes@oracle.com> Cc: Michal Hocko <mhocko@suse.com> Cc: Ryan Roberts <ryan.roberts@arm.com> Cc: Thiago Jung Bauermann <thiago.bauermann@linaro.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
1 parent 45d19b4 commit aa5a10b

1 file changed

Lines changed: 11 additions & 3 deletions

File tree

fs/proc/task_mmu.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2021,12 +2021,14 @@ static int pagemap_hugetlb_range(pte_t *ptep, unsigned long hmask,
20212021
struct pagemapread *pm = walk->private;
20222022
struct vm_area_struct *vma = walk->vma;
20232023
u64 flags = 0, frame = 0;
2024+
spinlock_t *ptl;
20242025
int err = 0;
20252026
pte_t pte;
20262027

20272028
if (vma->vm_flags & VM_SOFTDIRTY)
20282029
flags |= PM_SOFT_DIRTY;
20292030

2031+
ptl = huge_pte_lock(hstate_vma(vma), walk->mm, ptep);
20302032
pte = huge_ptep_get(walk->mm, addr, ptep);
20312033
if (pte_present(pte)) {
20322034
struct folio *folio = page_folio(pte_page(pte));
@@ -2054,11 +2056,12 @@ static int pagemap_hugetlb_range(pte_t *ptep, unsigned long hmask,
20542056

20552057
err = add_to_pagemap(&pme, pm);
20562058
if (err)
2057-
return err;
2059+
break;
20582060
if (pm->show_pfn && (flags & PM_PRESENT))
20592061
frame++;
20602062
}
20612063

2064+
spin_unlock(ptl);
20622065
cond_resched();
20632066

20642067
return err;
@@ -3132,17 +3135,22 @@ static int gather_pte_stats(pmd_t *pmd, unsigned long addr,
31323135
static int gather_hugetlb_stats(pte_t *pte, unsigned long hmask,
31333136
unsigned long addr, unsigned long end, struct mm_walk *walk)
31343137
{
3135-
pte_t huge_pte = huge_ptep_get(walk->mm, addr, pte);
3138+
pte_t huge_pte;
31363139
struct numa_maps *md;
31373140
struct page *page;
3141+
spinlock_t *ptl;
31383142

3143+
ptl = huge_pte_lock(hstate_vma(walk->vma), walk->mm, pte);
3144+
huge_pte = huge_ptep_get(walk->mm, addr, pte);
31393145
if (!pte_present(huge_pte))
3140-
return 0;
3146+
goto out;
31413147

31423148
page = pte_page(huge_pte);
31433149

31443150
md = walk->private;
31453151
gather_stats(page, md, pte_dirty(huge_pte), 1);
3152+
out:
3153+
spin_unlock(ptl);
31463154
return 0;
31473155
}
31483156

0 commit comments

Comments
 (0)