Skip to contentSkip to conversation

mm/madvise: atomically clear the young bit on THP MADV_COLD

PATCH Applied Pedro Falcato 6 participants 8 messages Oct 13 – Oct 15

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mm/madvise: atomically clear the young bit on THP MADV_COLD

As-is, madvise_cold_or_pageout_pte_range() discards the hardware young bit by invalidating the PMD, doing pmd_mkold() and subsequently writing to the pmd again. However, it does so in a racy manner, looking at a possibly-stale snapshot. It's entirely possible that a write elsewhere set the hardware D bit, which this logic now loses.

Fix it by using pmdp_test_and_clear_young(). This is an atomic modification of the PMD entry, and thus safe against any possible racing writes. As a (nice) side-effect, this elides one non-required TLB flush in pmdp_invalidate().

This bug can interact with the swapping system in complicated ways, leading to data loss. For instance, if a folio is marked lazyfree, but was later read from, and then MADV_COLD'd while racing a write to that same THP, dropping the dirty bit may lead to data loss as reclaim/MADV_PAGEOUT drops the "clean" anon folio:

T0 T1
================================================================
MADV_FREE(thp) |
folio marked lazyfree |
read(thp) |
PMD marked young by hardware |
| MADV_COLD(thp)
write(thp) | if (pmd_young(pmd)) {
PMD marked dirty by hardware | set(pmdp, pmd_mkold(pmd));
MADV_PAGEOUT/reclaim |
dirty bit may or may not be |
set |
reclaim/pageout may drop |
the folio |

---
 mm/madvise.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
mm/madvise.c +1 -4
index 85ab9bc76e06..19cd3bb4d1b9 100644
@@ -431,10 +431,7 @@ static int madvise_cold_or_pageout_pte_range(pmd_t *pmd,
 		}
 
 		if (!pageout && pmd_young(orig_pmd)) {
-			pmdp_invalidate(vma, addr, pmd);
-			orig_pmd = pmd_mkold(orig_pmd);
-
-			set_pmd_at(mm, addr, pmd, orig_pmd);
+			pmdp_test_and_clear_young(vma, addr, pmd);
 			tlb_remove_pmd_tlb_entry(tlb, pmd, addr);
 		}
 
Signature · 1 line
2.55.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 32 lines · Pedro Falcato

As-is, madvise_cold_or_pageout_pte_range() discards the hardware young bit by invalidating the PMD, doing pmd_mkold() and subsequently writing to the pmd again. However, it does so in a racy manner, looking at a possibly-stale snapshot. It's entirely possible that a write elsewhere set the hardware D bit, which this logic now loses.

Fix it by using pmdp_test_and_clear_young(). This is an atomic modification of the PMD entry, and thus safe against any possible racing writes. As a (nice) side-effect, this elides one non-required TLB flush in pmdp_invalidate().

This bug can interact with the swapping system in complicated ways, leading to data loss. For instance, if a folio is marked lazyfree, but was later read from, and then MADV_COLD'd while racing a write to that same THP, dropping the dirty bit may lead to data loss as reclaim/MADV_PAGEOUT drops the "clean" anon folio:

T0 T1
================================================================
MADV_FREE(thp) |
folio marked lazyfree |
read(thp) |
PMD marked young by hardware |
| MADV_COLD(thp)
write(thp) | if (pmd_young(pmd)) {
PMD marked dirty by hardware | set(pmdp, pmd_mkold(pmd));
MADV_PAGEOUT/reclaim |
dirty bit may or may not be |
set |
reclaim/pageout may drop |
the folio |

Quoted 5 lines

The function that keeps on giving (bugs)

maintainer Acked-by on v1 1/1
#2 lore raw
In reply to Pedro Falcato
Quoted 46 lines · Pedro Falcato

As-is, madvise_cold_or_pageout_pte_range() discards the hardware young bit by invalidating the PMD, doing pmd_mkold() and subsequently writing to the pmd again. However, it does so in a racy manner, looking at a possibly-stale snapshot. It's entirely possible that a write elsewhere set the hardware D bit, which this logic now loses.

Fix it by using pmdp_test_and_clear_young(). This is an atomic modification of the PMD entry, and thus safe against any possible racing writes. As a (nice) side-effect, this elides one non-required TLB flush in pmdp_invalidate().

This bug can interact with the swapping system in complicated ways, leading to data loss. For instance, if a folio is marked lazyfree, but was later read from, and then MADV_COLD'd while racing a write to that same THP, dropping the dirty bit may lead to data loss as reclaim/MADV_PAGEOUT drops the "clean" anon folio:

T0 T1
================================================================
MADV_FREE(thp) |
folio marked lazyfree |
read(thp) |
PMD marked young by hardware |
| MADV_COLD(thp)
write(thp) | if (pmd_young(pmd)) {
PMD marked dirty by hardware | set(pmdp, pmd_mkold(pmd));
MADV_PAGEOUT/reclaim |
dirty bit may or may not be |
set |
reclaim/pageout may drop |
the folio |

---
 mm/madvise.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
mm/madvise.c +0 -0
index 85ab9bc76e06..19cd3bb4d1b9 100644

mm/madvise.c static int madvise_cold_or_pageout_pte_range(pmd_t *pmd, · line 435

Quoted 9 lines
mm/madvise.c +1 -4
@@ -431,10 +431,7 @@ static int madvise_cold_or_pageout_pte_range(pmd_t *pmd,
 		}
 
 		if (!pageout && pmd_young(orig_pmd)) {
-			pmdp_invalidate(vma, addr, pmd);
-			orig_pmd = pmd_mkold(orig_pmd);
-
-			set_pmd_at(mm, addr, pmd, orig_pmd);
+			pmdp_test_and_clear_young(vma, addr, pmd);
 			tlb_remove_pmd_tlb_entry(tlb, pmd, addr);

Ah, we even already have that arch helper, nice. Let's hope all relevant architectures implement a proper variant :)

Signature · 3 lines
Cheers,

David
Acked-by on v1 1/1
#3 lore raw
In reply to Pedro Falcato
Quoted 32 lines · Pedro Falcato

As-is, madvise_cold_or_pageout_pte_range() discards the hardware young bit by invalidating the PMD, doing pmd_mkold() and subsequently writing to the pmd again. However, it does so in a racy manner, looking at a possibly-stale snapshot. It's entirely possible that a write elsewhere set the hardware D bit, which this logic now loses.

Fix it by using pmdp_test_and_clear_young(). This is an atomic modification of the PMD entry, and thus safe against any possible racing writes. As a (nice) side-effect, this elides one non-required TLB flush in pmdp_invalidate().

This bug can interact with the swapping system in complicated ways, leading to data loss. For instance, if a folio is marked lazyfree, but was later read from, and then MADV_COLD'd while racing a write to that same THP, dropping the dirty bit may lead to data loss as reclaim/MADV_PAGEOUT drops the "clean" anon folio:

T0 T1
================================================================
MADV_FREE(thp) |
folio marked lazyfree |
read(thp) |
PMD marked young by hardware |
| MADV_COLD(thp)
write(thp) | if (pmd_young(pmd)) {
PMD marked dirty by hardware | set(pmdp, pmd_mkold(pmd));
MADV_PAGEOUT/reclaim |
dirty bit may or may not be |
set |
reclaim/pageout may drop |
the folio |

Quoted 9 lines
---
 mm/madvise.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)

LGTM.

Signature · 2 lines
Best Regards,
Yan, Zi
maintainer Reviewed-by on v1 1/1
#4 lore raw
In reply to Pedro Falcato

+cc Gregory

Quoted 1 line · Pedro Falcato

As-is, madvise_cold_or_pageout_pte_range() discards the hardware young

The function from hell... Gregory - PLEASE FIX THIS SOON :))

Quoted 9 lines

bit by invalidating the PMD, doing pmd_mkold() and subsequently writing to the pmd again. However, it does so in a racy manner, looking at a possibly-stale snapshot. It's entirely possible that a write elsewhere set the hardware D bit, which this logic now loses.

Fix it by using pmdp_test_and_clear_young(). This is an atomic modification of the PMD entry, and thus safe against any possible racing writes. As a (nice) side-effect, this elides one non-required TLB flush in pmdp_invalidate().

Nice, I'm guessing the tlb_remove_pmd_tlb_entry() does what we need already? (as discussed off-list :)

Quoted 9 lines

This bug can interact with the swapping system in complicated ways, leading to data loss. For instance, if a folio is marked lazyfree, but was later read from, and then MADV_COLD'd while racing a write to that same THP, dropping the dirty bit may lead to data loss as reclaim/MADV_PAGEOUT drops the "clean" anon folio:

T0 T1
================================================================

Quoted 13 lines
MADV_FREE(thp)                  |
  folio marked lazyfree         |
read(thp)                       |
  PMD marked young by hardware  |
                                | MADV_COLD(thp)
write(thp)                      |   if (pmd_young(pmd)) {
  PMD marked dirty by hardware  |     set(pmdp, pmd_mkold(pmd));
MADV_PAGEOUT/reclaim            |
  dirty bit may or may not be   |
  set                           |
    reclaim/pageout may drop    |
    the folio                   |

Thanks, nice explanation (even if the >quoting ruins the indentation on my editor :P) [untabbed for my benefit]

Quoted 6 lines

LGTM so:

Quoted 9 lines
---
 mm/madvise.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
mm/madvise.c +0 -0
index 85ab9bc76e06..19cd3bb4d1b9 100644

mm/madvise.c static int madvise_cold_or_pageout_pte_range(pmd_t *pmd, · line 434

Quoted 8 lines
mm/madvise.c +1 -4
@@ -431,10 +431,7 @@ static int madvise_cold_or_pageout_pte_range(pmd_t *pmd,
 		}
 
 		if (!pageout && pmd_young(orig_pmd)) {
-			pmdp_invalidate(vma, addr, pmd);
-			orig_pmd = pmd_mkold(orig_pmd);
-
-			set_pmd_at(mm, addr, pmd, orig_pmd);
+			pmdp_test_and_clear_young(vma, addr, pmd);

Lovely diffstat :)

Quoted 6 lines
mm/madvise.c +0 -1
 			tlb_remove_pmd_tlb_entry(tlb, pmd, addr);
 		}
 
--

2.55.0

--
Cheers, Lorenzo

#5 lore raw
Quoted 6 lines · Lorenzo Stoakes (ARM)

+cc Gregory

Quoted 1 line · Pedro Falcato

As-is, madvise_cold_or_pageout_pte_range() discards the hardware young

The function from hell... Gregory - PLEASE FIX THIS SOON :))

:))

Quoted 6 lines
Quoted 6 lines

bit by invalidating the PMD, doing pmd_mkold() and subsequently writing to the pmd again. However, it does so in a racy manner, looking at a possibly-stale snapshot. It's entirely possible that a write elsewhere set the hardware D bit, which this logic now loses.

Quoted 7 lines
Quoted 4 lines

Fix it by using pmdp_test_and_clear_young(). This is an atomic modification of the PMD entry, and thus safe against any possible racing writes. As a (nice) side-effect, this elides one non-required TLB flush in pmdp_invalidate().

Nice, I'm guessing the tlb_remove_pmd_tlb_entry() does what we need already? (as discussed off-list :)

Right, so the whole gist of it is that indeed for 99% of cases you don't need to flush TLBs when clearing young. Out of all the ptep_clear_flush_young() implementations out there:
- arm64 does a light tlbi to help make sure the A bit will be cleared whenever.
(I remember reading about this, and it was due to some really weird hardware that kinda wanted this)
- ppc64 just clears it
- parisc does a non-atomic get+clear with a tlb flush in #if CONFIG_FLUSH_PAGE_ACCESSED.
CONFIG_FLUSH_PAGE_ACCESSED is hardcoded 0.
- riscv is just a clear
- s390 is just a clear
- x86 is just a clear
- pgtable-generic does a TLB flush. Unclear if most/all users using it actually
require it, or if it's just nonsense.

For now, the tlb_remove_pmd_tlb_entry() (and corresponding tlb flush in the PTE code) does the job.

Anyway, the point being that I kinda want to avoid these flushes, but it's not particularly trivial ATM, with the tlb_gather API. I'll try to remember and send some (hopefully tasteful) patches next cycle, on top of Gregory's make-madvise-cold-not-hurt-my-eyes patches that will have surely landed by then.

Quoted 24 lines
Quoted 10 lines

This bug can interact with the swapping system in complicated ways, leading to data loss. For instance, if a folio is marked lazyfree, but was later read from, and then MADV_COLD'd while racing a write to that same THP, dropping the dirty bit may lead to data loss as reclaim/MADV_PAGEOUT drops the "clean" anon folio:

T0 T1
================================================================

Quoted 13 lines
MADV_FREE(thp)                  |
  folio marked lazyfree         |
read(thp)                       |
  PMD marked young by hardware  |
                                | MADV_COLD(thp)
write(thp)                      |   if (pmd_young(pmd)) {
  PMD marked dirty by hardware  |     set(pmdp, pmd_mkold(pmd));
MADV_PAGEOUT/reclaim            |
  dirty bit may or may not be   |
  set                           |
    reclaim/pageout may drop    |
    the folio                   |
Quoted 2 lines

Thanks, nice explanation (even if the >quoting ruins the indentation on my editor :P) [untabbed for my benefit]

Thanks for the review :) indeed the tabbing looks ugly when quoted.

Signature · 1 line
Pedro
#6 lore raw
Quoted 10 lines · Pedro Falcato
Quoted 6 lines · Lorenzo Stoakes (ARM)

+cc Gregory

Quoted 1 line · Pedro Falcato

As-is, madvise_cold_or_pageout_pte_range() discards the hardware young

The function from hell... Gregory - PLEASE FIX THIS SOON :))

:))

Sending up selftests and reworks shortly.

Reviewed-by on v1 1/1
#7 lore raw
In reply to Pedro Falcato
Quoted 11 lines · On Tue, 13 Oct 2026 20:17:19 +0100 Pedro Falcato <[email protected]> wrote:

As-is, madvise_cold_or_pageout_pte_range() discards the hardware young bit by invalidating the PMD, doing pmd_mkold() and subsequently writing to the pmd again. However, it does so in a racy manner, looking at a possibly-stale snapshot. It's entirely possible that a write elsewhere set the hardware D bit, which this logic now loses.

Fix it by using pmdp_test_and_clear_young(). This is an atomic modification of the PMD entry, and thus safe against any possible racing writes. As a (nice) side-effect, this elides one non-required TLB flush in pmdp_invalidate().

Quoted 5 lines

This bug can interact with the swapping system in complicated ways, leading to data loss. For instance, if a folio is marked lazyfree, but was later read from, and then MADV_COLD'd while racing a write to that same THP, dropping the dirty bit may lead to data loss as reclaim/MADV_PAGEOUT drops the "clean" anon folio:

Thank you for nice description. Makes sense to me.

[...]

Quoted 6 lines

Thanks,
SJ

[...]