All of lore.kernel.org
 help / color / mirror / Atom feed
* [RFC PATCH] mm: madvise: don't split a PMD on partial THP MADV_DONTNEED
@ 2026-12-02 10:28 Alexandre Ghiti
  2026-12-02 11:22 ` Lorenzo Stoakes (ARM)
  2026-12-02 11:22 ` David Hildenbrand (Arm)
  0 siblings, 2 replies; 3+ messages in thread
From: Alexandre Ghiti @ 2026-12-02 10:28 UTC (permalink / raw)
  To: Andrew Morton, David Hildenbrand, Zi Yan
  Cc: Baolin Wang, Liam R. Howlett, Nico Pache, Ryan Roberts, Dev Jain,
	Barry Song, Lance Yang, Usama Arif, Kiryl Shutsemau,
	Vlastimil Babka, Mike Rapoport, Michal Hocko, Lorenzo Stoakes,
	Suren Baghdasaryan, Shakeel Butt, Pedro Falcato, Jann Horn,
	linux-mm, linux-kernel, Alexandre Ghiti

MADV_DONTNEED on a range that covers only part of a PMD-mapped THP splits
the PMD and zaps the PTEs of the range. The THP itself is not freed: it
stays allocated and charged as a whole, now partially mapped, until the
deferred split shrinker splits it under memory pressure. Until then the
process has lost the PMD mapping, and the TLB reach that comes with it, for
no memory benefit.

Besides, a THP is checked for being underused only once: it is queued on
the deferred split list when it is created, and the shrinker drops it from
the list for good once it has found it not underused. A long-lived THP
whose pages are freed later is never looked at again, so its unused memory
can't be reclaimed.

When the underused THP shrinker can reclaim the range later, zero it in
place instead: it reads as zeroes, as MADV_DONTNEED requires, and the PMD
mapping stays. The THP is queued again on the deferred split list, so the
shrinker looks at it again.

For an exclusive anonymous THP, MADV_DONTNEED on part of it thus no longer
leaves it partially mapped: the range reads as zeroes at once, and the
memory is given back when reclaim needs it, once the THP holds more than
max_ptes_none zeroed pages. Below that, the THP is not split even under
memory pressure, where the partially mapped THP used to be split: it keeps
up to max_ptes_none zeroed pages, the waste khugepaged already allows when
collapsing.

The range is zapped as before when the zeroed pages could not be freed,
e.g. with max_ptes_none at 511 (the default), or when anyone else may see
the THP, e.g. after fork or through a GUP pin.

Results on a large production workload (THP=always, max_ptes_none=0,
allocator purging with MADV_DONTNEED), same allocator configuration before
and after this patch:

  THPs and memory                       change
  ------------------------------------  ------
  THPs partially mapped                   -97%
  memory mapped by THPs (AnonHugePages)   +29%
  underused THP splits                   +145%
  PMD splits                              +10%
  workload anonymous memory              +0.2%

  Performance                           change
  ------------------------------------  ------
  p50 latency                            -1.7%
  p99 latency                            -2.3%
  throughput                      within noise
  dTLB misses per instruction            -0.4%
  IPC                                    +0.2%

Signed-off-by: Alexandre Ghiti <alex@ghiti.fr>
---
Open questions:

- MADV_DONTNEED now zeroes the range synchronously, up to a whole PMD
  minus one page (2M on x86, 512M with 64K base pages on arm64). Should
  zeroing be capped, falling back to the split above some size?

- A pin taken while the range is being zeroed goes unnoticed, as neither
  GUP-fast nor GUP takes the folio lock: a device write through it (e.g. an
  O_DIRECT read into the range from another thread) can land after
  MADV_DONTNEED returns. Only a process doing I/O into memory it is
  discarding can see it; pins that exist when the THP is checked fall back
  to the zap. Closing the window would take a PMD invalidation and a TLB
  flush per call, which this patch avoids, or GUP backing off on a folio
  being zeroed. Is the race acceptable?

- RSS (and so oom_badness()), /proc/pid/pagemap and mincore() keep showing
  the range as resident until the THP is split, where the zap made it
  disappear at once. memcg usage is unchanged: the THP stayed charged until
  its split before too.

- The cow (THP_RUN_SINGLE_PTE), khugepaged and transhuge-stress selftests
  use a partial MADV_DONTNEED to split a PMD, which MADV_DONTNEED never
  guaranteed. khugepaged sets max_ptes_none to 511 first, but cow and
  transhuge-stress keep the host's value: with a lower max_ptes_none they
  still pass, while silently testing a PMD-mapped THP. They should pin
  max_ptes_none or split the PMD explicitly (MADV_NOHUGEPAGE, mprotect() or
  debugfs).

PTE-mapped large folios (mTHP) are left for later.

 include/linux/huge_mm.h |  2 ++
 include/linux/mm.h      |  2 ++
 mm/huge_memory.c        | 69 +++++++++++++++++++++++++++++++++++++++++
 mm/internal.h           |  3 ++
 mm/madvise.c            |  2 +-
 mm/memory.c             | 14 ++++++---
 6 files changed, 87 insertions(+), 5 deletions(-)

diff --git a/include/linux/huge_mm.h b/include/linux/huge_mm.h
index 087920c38163..4ca675435856 100644
--- a/include/linux/huge_mm.h
+++ b/include/linux/huge_mm.h
@@ -28,6 +28,8 @@ bool madvise_free_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma,
 			   pmd_t *pmd, unsigned long addr, unsigned long next);
 bool zap_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma, pmd_t *pmd,
 		  unsigned long addr);
+bool zero_huge_pmd_range(struct vm_area_struct *vma, pmd_t *pmd,
+			 unsigned long addr, unsigned long end);
 int zap_huge_pud(struct mmu_gather *tlb, struct vm_area_struct *vma, pud_t *pud,
 		 unsigned long addr);
 bool move_huge_pmd(struct vm_area_struct *vma, unsigned long old_addr,
diff --git a/include/linux/mm.h b/include/linux/mm.h
index 00abd4d250a9..d28ba827f1d5 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -3413,6 +3413,8 @@ struct zap_details {
 #define  ZAP_FLAG_DROP_MARKER        ((__force zap_flags_t) BIT(0))
 /* Set in unmap_vmas() to indicate a final unmap call.  Only used by hugetlb */
 #define  ZAP_FLAG_UNMAP              ((__force zap_flags_t) BIT(1))
+/* MADV_DONTNEED: zero part of a PMD-mapped THP in place instead of splitting */
+#define  ZAP_FLAG_ZERO_PARTIAL_THP   ((__force zap_flags_t) BIT(2))
 
 #ifdef CONFIG_MMU
 extern bool can_do_mlock(void);
diff --git a/mm/huge_memory.c b/mm/huge_memory.c
index 4fc888ddfb69..121da9397194 100644
--- a/mm/huge_memory.c
+++ b/mm/huge_memory.c
@@ -2623,6 +2623,75 @@ bool zap_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma,
 	return true;
 }
 
+/*
+ * MADV_DONTNEED on [addr, end), part of a PMD-mapped THP: zero it in place
+ * instead of splitting the PMD, and queue the THP for the deferred split
+ * shrinker, which frees the zeroed pages once the THP is underused. Return
+ * false if the caller must split the PMD and zap the range instead.
+ */
+bool zero_huge_pmd_range(struct vm_area_struct *vma, pmd_t *pmd,
+			 unsigned long addr, unsigned long end)
+{
+	unsigned long haddr = addr & HPAGE_PMD_MASK;
+	struct folio *folio;
+	spinlock_t *ptl;
+	pmd_t orig_pmd;
+	bool ret;
+
+	/* With max_ptes_none at 511, no THP is ever underused. */
+	if (!READ_ONCE(split_underused_thp) ||
+	    khugepaged_max_ptes_none == HPAGE_PMD_NR - 1)
+		return false;
+
+	if ((vma->vm_flags & VM_MTE) || userfaultfd_armed(vma) ||
+	    mm_forbids_zeropage(vma->vm_mm))
+		return false;
+
+	ptl = pmd_trans_huge_lock(pmd, vma);
+	if (!ptl)
+		return false;
+	orig_pmd = pmdp_get(pmd);
+	if (is_huge_zero_pmd(orig_pmd)) {
+		spin_unlock(ptl);
+		return true;
+	}
+
+	folio = pmd_present(orig_pmd) ?
+		vm_normal_folio_pmd(vma, haddr, orig_pmd) : NULL;
+	if (!folio || !folio_test_anon(folio) ||
+	    !PageAnonExclusive(&folio->page) || folio_test_mlocked(folio) ||
+	    folio_contain_hwpoisoned_page(folio) ||
+	    folio_ref_count(folio) != folio_expected_ref_count(folio) ||
+	    !folio_trylock(folio)) {
+		spin_unlock(ptl);
+		return false;
+	}
+	folio_get(folio);
+	spin_unlock(ptl);
+
+	flush_cache_range(vma, addr, end);
+	clear_contig_highpages(folio_page(folio, (addr - haddr) >> PAGE_SHIFT),
+			       addr, (end - addr) >> PAGE_SHIFT);
+
+	/*
+	 * Mark the PMD dirty, and soft-dirty, as a write through it would. If
+	 * the PMD was split meanwhile, let the caller zap the range instead.
+	 */
+	ptl = pmd_lock(vma->vm_mm, pmd);
+	orig_pmd = pmdp_get(pmd);
+	ret = pmd_present(orig_pmd) && pmd_trans_huge(orig_pmd) &&
+	      pmd_folio(orig_pmd) == folio;
+	if (ret) {
+		touch_pmd(vma, haddr, pmd, true);
+		deferred_split_folio(folio, false);
+	}
+	spin_unlock(ptl);
+
+	folio_unlock(folio);
+	folio_put(folio);
+	return ret;
+}
+
 #ifndef pmd_move_must_withdraw
 static inline int pmd_move_must_withdraw(spinlock_t *new_pmd_ptl,
 					 spinlock_t *old_pmd_ptl,
diff --git a/mm/internal.h b/mm/internal.h
index 8942e57744a9..afa301cf8eaf 100644
--- a/mm/internal.h
+++ b/mm/internal.h
@@ -281,6 +281,9 @@ static inline int mmap_file(struct file *file, struct vm_area_struct *vma)
 
 /* unmap_vmas is in mm/memory.c */
 void unmap_vmas(struct mmu_gather *tlb, struct unmap_desc *unmap);
+/* clear_contig_highpages is in mm/memory.c */
+void clear_contig_highpages(struct page *page, unsigned long addr,
+			    unsigned int nr_pages);
 
 #ifdef CONFIG_MMU
 
diff --git a/mm/madvise.c b/mm/madvise.c
index 66600e82605c..35114546008e 100644
--- a/mm/madvise.c
+++ b/mm/madvise.c
@@ -888,7 +888,7 @@ static void zap_range(struct madvise_behavior *madv_behavior,
 static long madvise_dontneed_single_vma(struct madvise_behavior *madv_behavior)
 
 {
-	zap_range(madv_behavior, 0);
+	zap_range(madv_behavior, ZAP_FLAG_ZERO_PARTIAL_THP);
 	return 0;
 }
 
diff --git a/mm/memory.c b/mm/memory.c
index 82e1ec8e44e7..36a43b7ece84 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -2112,9 +2112,15 @@ static inline unsigned long zap_pmd_range(struct mmu_gather *tlb,
 	do {
 		next = pmd_addr_end(addr, end);
 		if (pmd_is_huge(*pmd)) {
-			if (next - addr != HPAGE_PMD_SIZE)
+			if (next - addr != HPAGE_PMD_SIZE) {
+				if (details &&
+				    (details->zap_flags & ZAP_FLAG_ZERO_PARTIAL_THP) &&
+				    zero_huge_pmd_range(vma, pmd, addr, next)) {
+					addr = next;
+					continue;
+				}
 				__split_huge_pmd(vma, pmd, addr, false);
-			else if (zap_huge_pmd(tlb, vma, pmd, addr)) {
+			} else if (zap_huge_pmd(tlb, vma, pmd, addr)) {
 				addr = next;
 				continue;
 			}
@@ -7674,8 +7680,8 @@ static inline int process_huge_page(
 	return 0;
 }
 
-static void clear_contig_highpages(struct page *page, unsigned long addr,
-				   unsigned int nr_pages)
+void clear_contig_highpages(struct page *page, unsigned long addr,
+			    unsigned int nr_pages)
 {
 	unsigned int i, count;
 	/*

base-commit: 0bbbdf85abf48b019cb8dd7a6e87f0a8995fdcd2
-- 
2.53.1-Meta



^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-12-02 11:22 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-02 10:28 [RFC PATCH] mm: madvise: don't split a PMD on partial THP MADV_DONTNEED Alexandre Ghiti
2026-12-02 11:22 ` Lorenzo Stoakes (ARM)
2026-12-02 11:22 ` David Hildenbrand (Arm)

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.