All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Alexandre Ghiti" <alex@ghiti.fr>,
	"Andrew Morton" <akpm@linux-foundation.org>,
	"David Hildenbrand" <david@kernel.org>, "Zi Yan" <ziy@nvidia.com>
Cc: "Baolin Wang" <baolin.wang@linux.alibaba.com>,
	"Liam R. Howlett" <liam@infradead.org>,
	"Nico Pache" <nico.pache@linux.dev>,
	"Ryan Roberts" <ryan.roberts@arm.com>,
	"Dev Jain" <dev.jain@arm.com>, "Barry Song" <baohua@kernel.org>,
	"Lance Yang" <lance.yang@linux.dev>,
	"Usama Arif" <usama.arif@linux.dev>,
	"Kiryl Shutsemau" <kas@kernel.org>,
	"Vlastimil Babka" <vbabka@kernel.org>,
	"Mike Rapoport" <rppt@kernel.org>,
	"Michal Hocko" <mhocko@suse.com>,
	"Lorenzo Stoakes" <ljs@kernel.org>,
	"Suren Baghdasaryan" <surenb@google.com>,
	"Shakeel Butt" <shakeel.butt@linux.dev>,
	"Pedro Falcato" <pfalcato@suse.de>,
	"Jann Horn" <jannh@google.com>,
	linux-mm@kvack.org
Subject: Re: [RFC PATCH] mm: madvise: don't split a PMD on partial THP MADV_DONTNEED
Date: Thu, 03 Dec 2026 10:28:33 +0000	[thread overview]
Message-ID: <sashiko-outbox-247575@kernel.org> (raw)
In-Reply-To: <20261202102818.1317130-1-alex@ghiti.fr>

Thank you for your contribution! Sashiko AI review found 7 potential
issues to consider:
- [Medium] mm: VM_WARN_ON_FOLIO() in deferred_split_folio() from
  zero_huge_pmd_range()
- [Medium] mm: zero_huge_pmd_range() redirties lazyfree THPs and cancels
  MADV_FREE
- [Medium] mm: touch_pmd() in zero_huge_pmd_range() corrupts x86 shadow
  stack PMDs
- [Medium] mm: race in zero_huge_pmd_range() drops underused THPs from
  split queue
- [Medium] mm: missing cache synchronization after zeroing in
  zero_huge_pmd_range()
- [Medium] mm: zero_huge_pmd_range() fails to queue swapcache THP on
  deferred split list
- [Medium] mm:huge_memory: reclaim and lazyfree regressions in
  zero_huge_pmd_range()
--

commit 68aeca962725b589429831991dcbf23772370b18
Author: Alexandre Ghiti <alex@ghiti.fr>

mm: madvise: don't split a PMD on partial THP MADV_DONTNEED

This commit zeroes partial MADV_DONTNEED ranges of exclusive PMD-mapped
anonymous THPs in place and queues the THP on the deferred split list
instead of splitting the PMD immediately.

> diff --git a/mm/huge_memory.c b/mm/huge_memory.c
> index 4fc888ddfb69f..121da93971941 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;
> +	}

[Severity: Medium]
Can a swapcache THP pass this check and then fail to be queued by
deferred_split_folio()?

folio_expected_ref_count() includes folio_test_swapcache(folio) << order,
so an exclusive PMD-mapped THP in the swapcache (for instance, after
shrink_folio_list() allocated swap and try_to_unmap_one() stopped on a
concurrent VMA_LOCKONFAULT walk before splitting the PMD) is accepted here
and zeroed in place.  However, deferred_split_folio() returns immediately
when folio_test_swapcache(folio) is true:

mm/huge_memory.c:deferred_split_folio() {
	...
	if (folio_test_swapcache(folio))
		return;
	...
}

Because zero_huge_pmd_range() neither frees the swapcache via
folio_free_swap() nor rejects swapcache folios, it returns true without
queueing the THP on the deferred split list, so the underused THP shrinker
will not reclaim the zeroed pages.

> +	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);

[Severity: Medium]
Does zeroing an already-mapped folio via clear_contig_highpages() without
flush_dcache_folio() leave stale instruction or data cache lines on
architectures with non-coherent I-caches or aliasing D-caches?

clear_contig_highpages() writes zeroes through the kernel linear mapping,
and flush_cache_range() is a no-op on architectures such as arm64 and
riscv that track cache cleanliness via PG_dcache_clean.  Without
flush_dcache_folio(folio) after zeroing, PG_dcache_clean stays set and the
zeroed lines are not cleaned to PoU or invalidated from the I-cache, so an
executable VMA (or a JIT VMA later mprotected back to PROT_EXEC, where
__sync_icache_dcache() skips sync_icache_aliases() when PG_dcache_clean is
set) can execute stale instructions from before MADV_DONTNEED.

> +	/*
> +	 * 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);

[Severity: Medium]
Does calling touch_pmd(vma, haddr, pmd, true) on a lazyfree THP redirty
the entire PMD and cancel an earlier MADV_FREE?

When MADV_FREE is called on a PMD-mapped THP, madvise_free_huge_pmd()
clears PG_dirty, marks the PMD old and clean, and clears PG_swapbacked via
folio_mark_lazyfree().  A subsequent partial MADV_DONTNEED now calls
touch_pmd(..., true), setting pmd_mkdirty() and pmd_mkyoung() on the whole
PMD.  When reclaim later inspects or splits the PMD in
__discard_anon_folio_pmd_locked() or __split_huge_pmd_locked() ->
ttu_anon_lazyfree_folio(), pmd_dirty() is transferred to PG_dirty on the
folio, which causes reclaim to restore PG_swapbacked and swap out the
remaining pages instead of discarding them without I/O.

[Severity: Medium]
Can touch_pmd(vma, haddr, pmd, true) corrupt an x86 shadow stack PMD?

On x86, a writable shadow stack PMD in a VM_SHADOW_STACK VMA is encoded
with _PAGE_RW=0 and _PAGE_DIRTY=1.  touch_pmd() calls pmd_mkdirty(entry)
without maybe_pmd_mkwrite(entry, vma):

arch/x86/include/asm/pgtable.h:pmd_mkdirty() {
	pmd = pmd_set_flags(pmd, _PAGE_DIRTY | _PAGE_SOFT_DIRTY);

	return pmd_mksaveddirty(pmd);
}

Because _PAGE_RW is 0, pmd_mksaveddirty() clears _PAGE_DIRTY and sets
_PAGE_SAVED_DIRTY, and pmdp_set_access_flags() writes that entry into
*pmd, turning the shadow stack PMD into a read-only non-shadow-stack PMD.

[Severity: Medium]
Does keeping the THP mapped with partially_mapped=false and marking the
PMD young cause shrink_folio_list() to swap out underused THPs in full
instead of splitting them?

In shrink_folio_list(), queued large folios are only split before
folio_alloc_swap() when folio_test_partially_mapped(folio) is true:

mm/vmscan.c:shrink_folio_list() {
	...
	if (data_race(!list_empty(&folio->_deferred_list) &&
	    folio_test_partially_mapped(folio)) &&
	    split_folio_to_list(folio, folio_list))
		goto activate_locked;
}

Because shrink_lruvec() runs shrink_folio_list() before shrink_slab() (and
MADV_PAGEOUT / DAMON_PAGEOUT do not run shrink_slab() at all), an
underused THP queued with partially_mapped=false is not split before
folio_alloc_swap() and is written to a 2MB swap cluster in full, including
the MADV_DONTNEED-zeroed pages.  In addition, touch_pmd() marks the PMD
young, causing folio_check_references() to treat a cold THP being
partially discarded as recently accessed.

> +		deferred_split_folio(folio, false);
> +	}

[Severity: Medium]
Can deferred_split_folio(folio, false) trigger
VM_WARN_ON_FOLIO(folio_test_partially_mapped(folio), folio) here?

deferred_split_folio() warns whenever partially_mapped is false and
PG_partially_mapped is already set on the folio:

mm/huge_memory.c:deferred_split_folio() {
	...
	if (partially_mapped) {
		...
	} else {
		/* partially mapped folios cannot become non-partially mapped */
		VM_WARN_ON_FOLIO(folio_test_partially_mapped(folio), folio);
	}
	...
}

PG_partially_mapped is sticky until the folio is split or freed.  If a
PMD-mapped THP was shared across fork(), the child split its PMD and
unmapped some PTEs, and try_to_migrate() temporarily converted the
parent's PMD to a migration entry, __folio_remove_rmap() saw the last PMD
unmapped while fewer than HPAGE_PMD_NR PTEs remained in the child and
called deferred_split_folio(folio, true), setting PG_partially_mapped
(which migrate_folio_move() also copies to the destination folio).  Once
the child exits and a write fault in do_huge_pmd_wp_page() restores
PageAnonExclusive on the parent's PMD, a partial MADV_DONTNEED reaches
deferred_split_folio(folio, false) and hits the warning.

> +	spin_unlock(ptl);
> +
> +	folio_unlock(folio);
> +	folio_put(folio);
> +	return ret;
> +}

[Severity: Medium]
Can a concurrent deferred_split_scan() run between folio_unlock() and
folio_put(), fail split_folio() because of the extra reference, and drop
the underused THP from the split queue?

CPU0                                    CPU1
zero_huge_pmd_range()
  folio_get(folio)
  ...
  deferred_split_folio(folio, false)
  spin_unlock(ptl)
  folio_unlock(folio)
                                        deferred_split_scan()
                                          thp_underused(folio) -> true
                                          folio_trylock(folio)
                                          split_folio(folio) -> -EAGAIN
                                          folio_unlock(folio)
                                          /* !partially_mapped: not requeued */
  folio_put(folio)

Because deferred_split_scan() only requeues folios after a failed
split_folio() when folio_test_partially_mapped(folio) is true, the
underused THP is removed from deferred_split_lru and its zeroed pages are
not reclaimed.  Dropping the temporary folio reference before
folio_unlock() (or queueing after dropping the extra reference) would
close this window.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261202102818.1317130-1-alex@ghiti.fr?part=1


  parent reply	other threads:[~2026-12-03 10:28 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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-03 14:10   ` Alexandre Ghiti
2026-12-02 11:22 ` David Hildenbrand (Arm)
2026-12-02 14:26   ` Alexandre Ghiti
2026-12-03 12:32     ` David Hildenbrand (Arm)
2026-12-02 16:37 ` Zi Yan
2026-12-02 17:39   ` Alexandre Ghiti
2026-12-02 20:28   ` David Hildenbrand (Arm)
2026-12-02 18:04 ` Shakeel Butt
2026-12-03 14:53   ` Alexandre Ghiti
2026-12-03 15:08     ` Pedro Falcato
2026-12-03 15:36       ` Alexandre Ghiti
2026-12-03 20:07         ` Shakeel Butt
2026-12-04  9:21           ` Pedro Falcato
2026-12-03 20:06     ` Shakeel Butt
2026-12-03 10:28 ` sashiko-bot [this message]
2026-12-04  3:16 ` Lance Yang

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=sashiko-outbox-247575@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=akpm@linux-foundation.org \
    --cc=alex@ghiti.fr \
    --cc=baohua@kernel.org \
    --cc=baolin.wang@linux.alibaba.com \
    --cc=david@kernel.org \
    --cc=dev.jain@arm.com \
    --cc=jannh@google.com \
    --cc=kas@kernel.org \
    --cc=lance.yang@linux.dev \
    --cc=liam@infradead.org \
    --cc=linux-mm@kvack.org \
    --cc=ljs@kernel.org \
    --cc=mhocko@suse.com \
    --cc=nico.pache@linux.dev \
    --cc=pfalcato@suse.de \
    --cc=rppt@kernel.org \
    --cc=ryan.roberts@arm.com \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=shakeel.butt@linux.dev \
    --cc=surenb@google.com \
    --cc=usama.arif@linux.dev \
    --cc=vbabka@kernel.org \
    --cc=ziy@nvidia.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 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.