mm/madvise: atomically clear the young bit on THP MADV_COLD
Subsystems (3)
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
@@ -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
Quoted 32 lines · Pedro Falcato
As-is,
madvise_cold_or_pageout_pte_range()discards the hardware young bit by invalidating the PMD, doingpmd_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 inpmdp_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
Fixes 9c276cc65a58 ("mm: introduce MADV_COLD")Reported-by Samarth Narang <[email protected]>Signed-off-by Pedro Falcato <[email protected]>
The function that keeps on giving (bugs)
Quoted 46 lines · Pedro Falcato
As-is,
madvise_cold_or_pageout_pte_range()discards the hardware young bit by invalidating the PMD, doingpmd_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 inpmdp_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 |Fixes 9c276cc65a58 ("mm: introduce MADV_COLD")Reported-by Samarth Narang <[email protected]>Signed-off-by Pedro Falcato <[email protected]>--- mm/madvise.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-)mm/madvise.c +0
-0
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
Quoted 32 lines · Pedro Falcato
As-is,
madvise_cold_or_pageout_pte_range()discards the hardware young bit by invalidating the PMD, doingpmd_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 inpmdp_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
Fixes 9c276cc65a58 ("mm: introduce MADV_COLD")Reported-by Samarth Narang <[email protected]>Signed-off-by Pedro Falcato <[email protected]>--- mm/madvise.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-)
LGTM.
Signature · 2 lines
Best Regards, Yan, Zi
+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 inpmdp_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
Fixes 9c276cc65a58 ("mm: introduce MADV_COLD")Reported-by Samarth Narang <[email protected]>Signed-off-by Pedro Falcato <[email protected]>
LGTM so:
Quoted 9 lines
--- mm/madvise.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-)mm/madvise.c +0
-0
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
-1tlb_remove_pmd_tlb_entry (tlb, pmd, addr); } --2.55.0
--
Cheers, Lorenzo
Quoted 6 lines · Lorenzo Stoakes (ARM)
+cc Gregory
Quoted 1 line · Pedro Falcato
As-is,
madvise_cold_or_pageout_pte_range()discards the hardware youngThe 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 inpmdp_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
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 youngThe function from hell... Gregory - PLEASE FIX THIS SOON :))
:))
Sending up selftests and reworks shortly.
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, doingpmd_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 inpmdp_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
Fixes 9c276cc65a58 ("mm: introduce MADV_COLD")Reported-by Samarth Narang <[email protected]>Signed-off-by Pedro Falcato <[email protected]>
Thanks,
SJ
[...]