mm/madvise: use folio_trylock() in the cold/pageout PMD split
Subsystems (3)
PATCH RESEND 1/1 mm/madvise: use folio_trylock() in the cold/pageout PMD split
MADV_COLD or MADV_PAGEOUT over part of a PMD splits the THP in madvise_cold_or_pageout_pte_range(). Two threads doing that to the same THP create spurious failures.
CPU0 CPU1
---- ----
folio_get()
spin_unlock(ptl)
folio_lock()
folio_get() spin_unlock(ptl) folio_lock() <- blocks, keeps its ref
split_folio()
folio_expected_ref_count(folio) != folio_ref_count(folio) - 1
-EAGAINCPU1 cannot drop its reference until it gets the lock CPU0 holds, so CPU0's split always fails. folio_trylock() makes CPU1 leave without ever taking a reference. The PTE branch of this same function already does this, as do madvise_free_pte_range() and madvise_free_huge_pmd().
Reproducer: 400 rounds of eight threads calling MADV_COLD on half of each of eight THPs, re-formed with MADV_COLLAPSE between rounds. From /proc/vmstat:
thp_split_page thp_split_page_failed
before 3186 860
after 3200 0
The short before count is rounds where every thread failed and the advice was dropped for that THP entirely.
On failure the walker returns 0 and nothing retries. The PMD path becomes best effort when the folio lock is held elsewhere - same as the PTE path.
--- mm/madvise.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
mm/madvise.c +2 -1
@@ -419,9 +419,10 @@ static int madvise_cold_or_pageout_pte_range(pmd_t *pmd,if (next - addr !=HPAGE_PMD_SIZE ) {int err; +if (!folio_trylock (folio)) + goto huge_unlock;folio_get (folio);spin_unlock (ptl); -folio_lock (folio); err =split_folio (folio);folio_unlock (folio);folio_put (folio);
Signature · 1 line
2.55.0
Andrew Morton
maintainerQuoted 29 lines · On Sat, 12 Sep 2026 07:05:40 -0400 Gregory Price <[email protected]> wrote:
MADV_COLDorMADV_PAGEOUTover part of a PMD splits the THP inmadvise_cold_or_pageout_pte_range(). Two threads doing that to the same THP create spurious failures.CPU0 CPU1
---- ----
folio_get()
spin_unlock(ptl)
folio_lock()
folio_get()spin_unlock(ptl)folio_lock()<- blocks, keeps its ref
split_folio()folio_expected_ref_count(folio) != folio_ref_count(folio) - 1 -EAGAINCPU1 cannot drop its reference until it gets the lock CPU0 holds, so CPU0's split always fails.
folio_trylock()makes CPU1 leave without ever taking a reference. The PTE branch of this same function already does this, as domadvise_free_pte_range()andmadvise_free_huge_pmd().Reproducer: 400 rounds of eight threads calling
MADV_COLDon half of each of eight THPs, re-formed withMADV_COLLAPSEbetween rounds. From/proc/vmstat:
thp_split_pagethp_split_page_failed
before 3186 860
after 3200 0
Quoted 5 lines
The short before count is rounds where every thread failed and the advice was dropped for that THP entirely.
On failure the walker returns 0 and nothing retries. The PMD path becomes best effort when the folio lock is held elsewhere - same as the PTE path.
Can this result in more EAGAINs being returned to userspace?
Quoted 4 lines
mm/madvise.c +0
-0@@ -419,9 +419,10 @@ static int madvise_cold_or_pageout_pte_range(pmd_t *pmd,if (next - addr !=HPAGE_PMD_SIZE ) {
mm/madvise.c static int madvise_cold_or_pageout_pte_range(pmd_t *pmd, · line 428
Quoted 10 lines
mm/madvise.c +2
-1int err; +if (!folio_trylock (folio)) + goto huge_unlock;folio_get (folio);spin_unlock (ptl); -folio_lock (folio); err =split_folio (folio);folio_unlock (folio);folio_put (folio);
Sashiko shares my reaction, "dear god, why does that function exist".
https://sashiko.dev/#/patchset/[email protected]
Like the entire function, that random mix of "goto foo" with "return whatever" needs to die.
Anyway, please check it out while you're on a roll.
Quoted 6 lines · Andrew Morton
Quoted 3 lines
On failure the walker returns 0 and nothing retries. The PMD path becomes best effort when the folio lock is held elsewhere - same as the PTE path.
Can this result in more EAGAINs being returned to userspace?
I don't see where this can result in more EAGAIN, at least from the madvise side. The race is the same, we're just giving up and saying "I tried, doesn't matter if i succeeded".
Quoted 17 lines
Quoted 4 lines
mm/madvise.c +0
-0@@ -419,9 +419,10 @@ static int madvise_cold_or_pageout_pte_range(pmd_t *pmd,if (next - addr !=HPAGE_PMD_SIZE ) {mm/madvise.c static int madvise_cold_or_pageout_pte_range(pmd_t *pmd, · line 428
Quoted 10 lines
mm/madvise.c +2
-1int err; +if (!folio_trylock (folio)) + goto huge_unlock;folio_get (folio);spin_unlock (ptl); -folio_lock (folio); err =split_folio (folio);folio_unlock (folio);folio_put (folio);Sashiko shares my reaction, "dear god, why does that function exist".
Quoted 6 lines
https://sashiko.dev/#/patchset/[email protected]Like the entire function, that random mix of "goto foo" with "return whatever" needs to die.
Anyway, please check it out while you're on a roll.
I've been trying to shore up the sashiko garbage first in a backportable manner before doing a full restructure.
I would hate to rewrite this just to carry bugs forward, and clearly the code isn't even remotely correct as-is.
The remaining issue on that link is the memory leak patch you already looked at.
It's taken a bit to generate positive tests for these bugs and go through validation, but I think we are finally at the end of the nasty part and can actually get to cleaning this up.
~Gregory
Quoted 25 lines · Gregory Price
MADV_COLDorMADV_PAGEOUTover part of a PMD splits the THP inmadvise_cold_or_pageout_pte_range(). Two threads doing that to the same THP create spurious failures.CPU0 CPU1
---- ----
folio_get()
spin_unlock(ptl)
folio_lock()
folio_get()spin_unlock(ptl)folio_lock()<- blocks, keeps its ref
split_folio()folio_expected_ref_count(folio) != folio_ref_count(folio) - 1 -EAGAINCPU1 cannot drop its reference until it gets the lock CPU0 holds, so CPU0's split always fails.
folio_trylock()makes CPU1 leave without ever taking a reference. The PTE branch of this same function already does this, as domadvise_free_pte_range()andmadvise_free_huge_pmd().Reproducer: 400 rounds of eight threads calling
MADV_COLDon half of each of eight THPs, re-formed withMADV_COLLAPSEbetween rounds. From/proc/vmstat:
Quoted 6 lines
thp_split_pagethp_split_page_failed
before 3186 860
after 3200 0The short before count is rounds where every thread failed and the advice was dropped for that THP entirely.
While the split now succeeds, one of both calls will just effectively skip processing the page table. SO while CPU0 will succeed with the split, CPU1 would just skip the page table.
And what happened before?
Split on CPU0 failed and it would skip the page table. Split on CPU1, however, would likely have succeeded?
So this is all far from perfect.
... but this matches what we do further down in the PTE scenario.
Signature · 3 lines
Cheers, David
Quoted 26 lines · David Hildenbrand (Arm)
Quoted 25 lines · Gregory Price
MADV_COLDorMADV_PAGEOUTover part of a PMD splits the THP inmadvise_cold_or_pageout_pte_range(). Two threads doing that to the same THP create spurious failures.CPU0 CPU1
---- ----
folio_get()
spin_unlock(ptl)
folio_lock()
folio_get()spin_unlock(ptl)folio_lock()<- blocks, keeps its ref
split_folio()folio_expected_ref_count(folio) != folio_ref_count(folio) - 1 -EAGAINCPU1 cannot drop its reference until it gets the lock CPU0 holds, so CPU0's split always fails.
folio_trylock()makes CPU1 leave without ever taking a reference. The PTE branch of this same function already does this, as domadvise_free_pte_range()andmadvise_free_huge_pmd().Reproducer: 400 rounds of eight threads calling
MADV_COLDon half of each of eight THPs, re-formed withMADV_COLLAPSEbetween rounds. From/proc/vmstat:
Quoted 6 lines
Quoted 6 lines
thp_split_pagethp_split_page_failed
before 3186 860
after 3200 0The short before count is rounds where every thread failed and the advice was dropped for that THP entirely.
bleh i thought i rewrote this sentence.
reading madvise is driving me crazy.
Quoted 10 lines
While the split now succeeds, one of both calls will just effectively skip processing the page table. SO while CPU0 will succeed with the split, CPU1 would just skip the page table.
And what happened before?
Split on CPU0 failed and it would skip the page table. Split on CPU1, however, would likely have succeeded?
Yes., all correct.
Quoted 6 lines
So this is all far from perfect.
... but this matches what we do further down in the PTE scenario.
Thank you!
~gregory