From: Michal Hocko <mhocko@suse.com>
To: "David Hildenbrand (Arm)" <david@kernel.org>
Cc: Zi Yan <ziy@nvidia.com>,
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>,
Andrew Morton <akpm@linux-foundation.org>,
Lorenzo Stoakes <ljs@kernel.org>,
Vlastimil Babka <vbabka@kernel.org>,
Mike Rapoport <rppt@kernel.org>,
Suren Baghdasaryan <surenb@google.com>,
Jann Horn <jannh@google.com>,
Ying Huang <huang.ying.caritas@gmail.com>,
linux-mm@kvack.org, linux-kernel@vger.kernel.org,
stable@vger.kernel.org, Chris Mason <mason@kernel.org>,
Sashiko <sashiko-bot@kernel.org>
Subject: Re: [PATCH] mm/madvise: fix MADV_FREE handling for PMD-mapped THPs
Date: Thu, 19 Nov 2026 17:44:16 +0100 [thread overview]
Message-ID: <av8nYAco3bMhUIBI@tiehlicka> (raw)
In-Reply-To: <20261119-madv_free_thp-v1-1-a312919e1ee5@kernel.org>
On Thu 19-11-26 17:04:34, David Hildenbrand wrote:
> After MADV_FREE cleared the dirty flag of an anonymous folio, reclaim will
> later free the folio without writing it back if no mappings are dirty.
>
> If our folio is still in the swap cache, the swap entries then point at
> swap slots that were never written, exposing stale content of the swap
> device. Bad.
>
> madvise_free_pte_range() therefore frees the swap slots first, and skips
> the folio if that is impossible. madvise_free_huge_pmd() does not, and a
> PMD-mapped THP can be in the swap cache if try_to_unmap() failed during
> reclaim, for example due to a racing mlock().
>
> Further, madvise_free_huge_pmd() checks whether the THP is mapped by
> other processes before taking the folio lock. Another process that still
> has swap entries for the THP can concurrently map it from the swap cache,
> and we might end up clearing the dirty flag of a shared folio.
>
> Fix it by factoring out the folio handling of madvise_free_pte_range()
> into madvise_free_try_prepare_folio(), and using it for PMD-mapped THPs
> as well. Hopefully this prevents further bugs.
>
> For PMD-mapped THPs, this change implies:
>
> 1. We free the swap slots of a THP in the swap cache, and skip the THP if
> that is impossible.
> 2. We check the mapcount under the folio lock.
> 3. We no longer lock and check THPs that are neither dirty nor in the
> swap cache, just like for PTE-mapped folios.
>
> For PTE-mapped folios, this means:
>
> 4. We check the mapcount once before taking the folio lock, to not lock
> folios that are mapped by other processes.
>
> Only compile madvise_free_huge_pmd() with CONFIG_ADVISE_SYSCALLS to keep
> !CONFIG_ADVISE_SYSCALLS builds working.
>
> Chris and Sashiko reported the missing swap cache handling for PMD-mapped
> THPs, the other issue was found by code inspection.
>
> This has medium urgency.
>
> Fixes: bd4c82c22c36 ("mm, THP, swap: delay splitting THP after swapped out")
> Cc: stable@vger.kernel.org
> Reported-by: Chris Mason <mason@kernel.org>
> Reported-by: Sashiko <sashiko-bot@kernel.org>
> Closes: https://lore.kernel.org/sashiko-outbox-202086@kernel.org/
> Signed-off-by: David Hildenbrand (Arm) <david@kernel.org>
Acked-by: Michal Hocko <mhocko@suse.com>
Thanks!
> ---
> I would queue this fairly soon on mm-core/for-next-fixes
> ---
> mm/huge_memory.c | 19 ++++++-----------
> mm/internal.h | 1 +
> mm/madvise.c | 65 +++++++++++++++++++++++++++++---------------------------
> 3 files changed, 41 insertions(+), 44 deletions(-)
>
> diff --git a/mm/huge_memory.c b/mm/huge_memory.c
> index 79d400ea9553d..e22daf846dbe3 100644
> --- a/mm/huge_memory.c
> +++ b/mm/huge_memory.c
> @@ -2421,6 +2421,7 @@ vm_fault_t do_huge_pmd_numa_page(struct vm_fault *vmf)
> return 0;
> }
>
> +#ifdef CONFIG_ADVISE_SYSCALLS
> /*
> * Return true if we do MADV_FREE successfully on entire pmd page.
> * Otherwise, return false.
> @@ -2455,21 +2456,13 @@ bool madvise_free_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma,
> if (folio_is_zone_device(folio))
> goto out;
>
> - /*
> - * If other processes are mapping this folio, we couldn't discard
> - * the folio unless they all do MADV_FREE so let's skip the folio.
> - */
> - if (folio_maybe_mapped_shared(folio))
> - goto out;
> -
> - if (!folio_trylock(folio))
> - goto out;
> -
> /*
> * If user want to discard part-pages of THP, split it so MADV_FREE
> * will deactivate only them.
> */
> if (next - addr != HPAGE_PMD_SIZE) {
> + if (folio_maybe_mapped_shared(folio) || !folio_trylock(folio))
> + goto out;
> folio_get(folio);
> spin_unlock(ptl);
> split_folio(folio);
> @@ -2478,9 +2471,8 @@ bool madvise_free_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma,
> goto out_unlocked;
> }
>
> - if (folio_test_dirty(folio))
> - folio_clear_dirty(folio);
> - folio_unlock(folio);
> + if (!madvise_free_try_prepare_folio(folio, 1))
> + goto out;
>
> if (pmd_young(orig_pmd) || pmd_dirty(orig_pmd)) {
> pmdp_invalidate(vma, addr, pmd);
> @@ -2498,6 +2490,7 @@ bool madvise_free_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma,
> out_unlocked:
> return ret;
> }
> +#endif /* CONFIG_ADVISE_SYSCALLS */
>
> static inline void zap_deposited_table(struct mm_struct *mm, pmd_t *pmd)
> {
> diff --git a/mm/internal.h b/mm/internal.h
> index 85cf7becf8588..96cb55d7816b1 100644
> --- a/mm/internal.h
> +++ b/mm/internal.h
> @@ -61,6 +61,7 @@ void lru_add_drain_cpu(int cpu);
> void lru_add_drain_cpu_zone(struct zone *zone);
> void folio_deactivate(struct folio *folio);
> void folio_mark_lazyfree(struct folio *folio);
> +bool madvise_free_try_prepare_folio(struct folio *folio, int nr_mappings);
>
> /* mm/vmscan.c */
> unsigned long zone_reclaimable_pages(struct zone *zone);
> diff --git a/mm/madvise.c b/mm/madvise.c
> index 0214efd2a014f..ebb4ce7daf6cb 100644
> --- a/mm/madvise.c
> +++ b/mm/madvise.c
> @@ -661,6 +661,38 @@ static long madvise_pageout(struct madvise_behavior *madv_behavior)
> return 0;
> }
>
> +bool madvise_free_try_prepare_folio(struct folio *folio, int nr_mappings)
> +{
> + if (!folio_test_swapcache(folio) && !folio_test_dirty(folio))
> + return true;
> +
> + if (folio_mapcount(folio) != nr_mappings || !folio_trylock(folio))
> + return false;
> +
> + /*
> + * KSM folios can gain new mappings at any time, so we must
> + * not clear their dirty flag.
> + */
> + if (folio_test_ksm(folio))
> + goto unlock;
> +
> + /*
> + * Check under the folio lock that the folio is exclusive: we own all
> + * mappings, and there are no references from swap entries.
> + */
> + if (folio_mapcount(folio) != nr_mappings)
> + goto unlock;
> + if (folio_test_swapcache(folio) && !folio_free_swap(folio))
> + goto unlock;
> +
> + folio_clear_dirty(folio);
> + folio_unlock(folio);
> + return true;
> +unlock:
> + folio_unlock(folio);
> + return false;
> +}
> +
> static int madvise_free_pte_range(pmd_t *pmd, unsigned long addr,
> unsigned long end, struct mm_walk *walk)
>
> @@ -753,37 +785,8 @@ static int madvise_free_pte_range(pmd_t *pmd, unsigned long addr,
> }
> }
>
> - if (folio_test_swapcache(folio) || folio_test_dirty(folio)) {
> - if (!folio_trylock(folio))
> - continue;
> - /*
> - * KSM folios can gain new mappings at any time, so we must
> - * not clear their dirty flag.
> - */
> - if (folio_test_ksm(folio)) {
> - folio_unlock(folio);
> - continue;
> - }
> -
> - /*
> - * If we have a large folio at this point, we know it is
> - * fully mapped so if its mapcount is the same as its
> - * number of pages, it must be exclusive.
> - */
> - if (folio_mapcount(folio) != folio_nr_pages(folio)) {
> - folio_unlock(folio);
> - continue;
> - }
> -
> - if (folio_test_swapcache(folio) &&
> - !folio_free_swap(folio)) {
> - folio_unlock(folio);
> - continue;
> - }
> -
> - folio_clear_dirty(folio);
> - folio_unlock(folio);
> - }
> + if (!madvise_free_try_prepare_folio(folio, folio_nr_pages(folio)))
> + continue;
>
> if (pte_young(ptent) || pte_dirty(ptent)) {
> clear_young_dirty_ptes(vma, addr, pte, nr, cydp_flags);
>
> ---
> base-commit: 5e600d79135124a4eb8e9db30ad524224c267c07
> change-id: 20261119-madv_free_thp-3c2a9887a722
>
> Best regards,
> --
> David Hildenbrand (Arm) <david@kernel.org>
--
Michal Hocko
SUSE Labs
next prev parent reply other threads:[~2026-11-19 16:44 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-19 16:04 David Hildenbrand (Arm)
2026-11-19 16:44 ` Michal Hocko [this message]
2026-11-19 17:21 ` Usama Arif
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=av8nYAco3bMhUIBI@tiehlicka \
--to=mhocko@suse.com \
--cc=akpm@linux-foundation.org \
--cc=baohua@kernel.org \
--cc=baolin.wang@linux.alibaba.com \
--cc=david@kernel.org \
--cc=dev.jain@arm.com \
--cc=huang.ying.caritas@gmail.com \
--cc=jannh@google.com \
--cc=kas@kernel.org \
--cc=lance.yang@linux.dev \
--cc=liam@infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=ljs@kernel.org \
--cc=mason@kernel.org \
--cc=nico.pache@linux.dev \
--cc=rppt@kernel.org \
--cc=ryan.roberts@arm.com \
--cc=sashiko-bot@kernel.org \
--cc=stable@vger.kernel.org \
--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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®