* [PATCH] mm/madvise: fix MADV_FREE handling for PMD-mapped THPs
@ 2026-11-19 16:04 David Hildenbrand (Arm)
2026-11-19 16:44 ` Michal Hocko
2026-11-19 17:21 ` Usama Arif
0 siblings, 2 replies; 3+ messages in thread
From: David Hildenbrand (Arm) @ 2026-11-19 16:04 UTC (permalink / raw)
To: Zi Yan, Baolin Wang, Liam R. Howlett, Nico Pache, Ryan Roberts,
Dev Jain, Barry Song, Lance Yang, Usama Arif, Kiryl Shutsemau,
Andrew Morton, Lorenzo Stoakes, Vlastimil Babka, Mike Rapoport,
Suren Baghdasaryan, Michal Hocko, Jann Horn, Ying Huang
Cc: linux-mm, linux-kernel, stable, Chris Mason, Sashiko,
David Hildenbrand (Arm)
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>
---
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>
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] mm/madvise: fix MADV_FREE handling for PMD-mapped THPs
2026-11-19 16:04 [PATCH] mm/madvise: fix MADV_FREE handling for PMD-mapped THPs David Hildenbrand (Arm)
@ 2026-11-19 16:44 ` Michal Hocko
2026-11-19 17:21 ` Usama Arif
1 sibling, 0 replies; 3+ messages in thread
From: Michal Hocko @ 2026-11-19 16:44 UTC (permalink / raw)
To: David Hildenbrand (Arm)
Cc: Zi Yan, Baolin Wang, Liam R. Howlett, Nico Pache, Ryan Roberts,
Dev Jain, Barry Song, Lance Yang, Usama Arif, Kiryl Shutsemau,
Andrew Morton, Lorenzo Stoakes, Vlastimil Babka, Mike Rapoport,
Suren Baghdasaryan, Jann Horn, Ying Huang, linux-mm,
linux-kernel, stable, Chris Mason, Sashiko
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
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] mm/madvise: fix MADV_FREE handling for PMD-mapped THPs
2026-11-19 16:04 [PATCH] mm/madvise: fix MADV_FREE handling for PMD-mapped THPs David Hildenbrand (Arm)
2026-11-19 16:44 ` Michal Hocko
@ 2026-11-19 17:21 ` Usama Arif
1 sibling, 0 replies; 3+ messages in thread
From: Usama Arif @ 2026-11-19 17:21 UTC (permalink / raw)
To: David Hildenbrand (Arm),
Zi Yan, Baolin Wang, Liam R. Howlett, Nico Pache, Ryan Roberts,
Dev Jain, Barry Song, Lance Yang, Kiryl Shutsemau, Andrew Morton,
Lorenzo Stoakes, Vlastimil Babka, Mike Rapoport,
Suren Baghdasaryan, Michal Hocko, Jann Horn, Ying Huang
Cc: linux-mm, linux-kernel, stable, Chris Mason, Sashiko
On 19/11/2026 08:04, David Hildenbrand (Arm) 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>
> ---
> 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(-)
>
Yeah looking at pmd swap, I imagine there might be more cases
which are correctly handled by pte path but not pmd.
Acked-by: Usama Arif <usama.arif@linux.dev>
> 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>
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-11-19 17:21 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-19 16:04 [PATCH] mm/madvise: fix MADV_FREE handling for PMD-mapped THPs David Hildenbrand (Arm)
2026-11-19 16:44 ` Michal Hocko
2026-11-19 17:21 ` Usama Arif
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®