linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Jinjiang Tu <tujinjiang@huawei.com>
To: <akpm@linux-foundation.org>, <david@redhat.com>,
	<dev.jain@arm.com>, <catalin.marinas@arm.com>,
	<lorenzo.stoakes@oracle.com>, <thiago.bauermann@linaro.org>,
	<superman.xpt@gmail.com>, <christophe.leroy@csgroup.eu>,
	<brahmajit.xyz@gmail.com>, <andrii@kernel.org>,
	<avagin@gmail.com>, <baolin.wang@linux.alibaba.com>,
	<ryan.roberts@arm.com>, <hughd@google.com>, <rientjes@google.com>,
	<mhocko@suse.com>, <joern@logfs.org>
Cc: <linux-mm@kvack.org>, <wangkefeng.wang@huawei.com>,
	<tujinjiang@huawei.com>
Subject: [PATCH v2 2/3] fs/proc/task_mmu: hold PTL in pagemap_hugetlb_range and gather_hugetlb_stats
Date: Thu, 24 Jul 2025 17:09:57 +0800	[thread overview]
Message-ID: <20250724090958.455887-3-tujinjiang@huawei.com> (raw)
In-Reply-To: <20250724090958.455887-1-tujinjiang@huawei.com>

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.

Signed-off-by: Jinjiang Tu <tujinjiang@huawei.com>
---
 fs/proc/task_mmu.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)

diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c
index 0102ab3aaec1..68077aef6c5b 100644
--- a/fs/proc/task_mmu.c
+++ b/fs/proc/task_mmu.c
@@ -1893,12 +1893,14 @@ static int pagemap_hugetlb_range(pte_t *ptep, unsigned long hmask,
 	struct pagemapread *pm = walk->private;
 	struct vm_area_struct *vma = walk->vma;
 	u64 flags = 0, frame = 0;
+	spinlock_t *ptl;
 	int err = 0;
 	pte_t pte;
 
 	if (vma->vm_flags & VM_SOFTDIRTY)
 		flags |= PM_SOFT_DIRTY;
 
+	ptl = huge_pte_lock(hstate_vma(vma), walk->mm, ptep);
 	pte = huge_ptep_get(walk->mm, addr, ptep);
 	if (pte_present(pte)) {
 		struct folio *folio = page_folio(pte_page(pte));
@@ -1926,11 +1928,12 @@ static int pagemap_hugetlb_range(pte_t *ptep, unsigned long hmask,
 
 		err = add_to_pagemap(&pme, pm);
 		if (err)
-			return err;
+			break;
 		if (pm->show_pfn && (flags & PM_PRESENT))
 			frame++;
 	}
 
+	spin_unlock(ptl);
 	cond_resched();
 
 	return err;
@@ -3004,17 +3007,22 @@ static int gather_pte_stats(pmd_t *pmd, unsigned long addr,
 static int gather_hugetlb_stats(pte_t *pte, unsigned long hmask,
 		unsigned long addr, unsigned long end, struct mm_walk *walk)
 {
-	pte_t huge_pte = huge_ptep_get(walk->mm, addr, pte);
+	pte_t huge_pte;
 	struct numa_maps *md;
 	struct page *page;
+	spinlock_t *ptl;
 
+	ptl = huge_pte_lock(hstate_vma(walk->vma), walk->mm, pte);
+	huge_pte = huge_ptep_get(walk->mm, addr, pte);
 	if (!pte_present(huge_pte))
-		return 0;
+		goto out;
 
 	page = pte_page(huge_pte);
 
 	md = walk->private;
 	gather_stats(page, md, pte_dirty(huge_pte), 1);
+out:
+	spin_unlock(ptl);
 	return 0;
 }
 
-- 
2.43.0



  parent reply	other threads:[~2025-07-24  9:11 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-07-24  9:09 [PATCH v2 0/3] hold PTL for several .hugetlb_entry callbacks Jinjiang Tu
2025-07-24  9:09 ` [PATCH v2 1/3] mm/smaps: fix race between smaps_hugetlb_range and migration Jinjiang Tu
2025-07-24 16:27   ` David Hildenbrand
2025-07-24  9:09 ` Jinjiang Tu [this message]
2025-07-24 16:27   ` [PATCH v2 2/3] fs/proc/task_mmu: hold PTL in pagemap_hugetlb_range and gather_hugetlb_stats David Hildenbrand
2025-07-24  9:09 ` [PATCH v2 3/3] mm/mincore: hold PTL in mincore_hugetlb Jinjiang Tu
2025-07-24 16:29   ` David Hildenbrand
2025-07-24 16:26 ` [PATCH v2 0/3] hold PTL for several .hugetlb_entry callbacks David Hildenbrand
2025-07-25  2:02   ` Jinjiang Tu
2025-07-25  3:31 ` [PATCH] mm/memory-failure: hold PTL in hwpoison_hugetlb_range Jinjiang Tu
2025-07-25 15:48   ` David Hildenbrand
2025-07-25 23:29   ` Andrew Morton
2025-07-26  0:28     ` Jinjiang Tu

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250724090958.455887-3-tujinjiang@huawei.com \
    --to=tujinjiang@huawei.com \
    --cc=akpm@linux-foundation.org \
    --cc=andrii@kernel.org \
    --cc=avagin@gmail.com \
    --cc=baolin.wang@linux.alibaba.com \
    --cc=brahmajit.xyz@gmail.com \
    --cc=catalin.marinas@arm.com \
    --cc=christophe.leroy@csgroup.eu \
    --cc=david@redhat.com \
    --cc=dev.jain@arm.com \
    --cc=hughd@google.com \
    --cc=joern@logfs.org \
    --cc=linux-mm@kvack.org \
    --cc=lorenzo.stoakes@oracle.com \
    --cc=mhocko@suse.com \
    --cc=rientjes@google.com \
    --cc=ryan.roberts@arm.com \
    --cc=superman.xpt@gmail.com \
    --cc=thiago.bauermann@linaro.org \
    --cc=wangkefeng.wang@huawei.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox