mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] mm: synchronize with PTE table reclaim for all folio sizes
@ 2026-11-05  2:56 Baolin Wang
  2026-11-05  3:22 ` Qi Zheng
  2026-11-05  4:54 ` Lance Yang
  0 siblings, 2 replies; 4+ messages in thread
From: Baolin Wang @ 2026-11-05  2:56 UTC (permalink / raw)
  To: akpm, david, ljs
  Cc: riel, liam, vbabka, harry, jannh, lance.yang, qi.zheng, pfalcato,
	ayushr, baolin.wang, linux-mm, linux-kernel

Similar to commit 243cf1f67b36 ("mm: memory: fix truncation removing mapped
folios"), the '!pmd_present()' check in page_vma_mapped_walk() can also race
with concurrent page table reclamation via zap_empty_pte_table().

page_vma_mapped_walk() is used by many functions including try_to_unmap(),
try_to_migrate(), folio_mkclean(), pfn_mkclean_range(), remove_migration_ptes(),
page_mapped_in_vma(), and mapping_wrprotect_range(). Except for try_to_unmap()
and try_to_migrate(), the other callers do not perform post-walk accounting
checks, so the race has no observable impact on them.

For migration, the race can cause unnecessary retries. For reclaim via
try_to_unmap(), the race can cause failure to reclaim a folio that is in
fact already unmapped (as shown below), which may lead to unexpected OOM
or increased refaults.

CPU 0 (reclaim)                      CPU 1 (MADV_DONTNEED)
--------------------------------    --------------------------------
folio_lock(F)
try_to_unmap(F, TTU_SYNC)
                                    zap_pte_range(full PMD range)
                                      spin_lock(ptl)
                                      clear PTEs
                                      zap_empty_pte_table()
                                        spin_trylock(pml)
                                        pmd_clear()
                                        spin_unlock(pml)

  page_vma_mapped_walk()
    observe !pmd_present(pmde)
    nr_pages < HPAGE_PMD_NR
    skip synchronization
    return false

try_to_unmap() returns
folio_mapped(F) == true
nr_unmap_fail += folio_nr_pages(F)
                                    tlb_flush_mmu_tlbonly()
                                    tlb_flush_rmaps()
                                      remove F's rmaps
                                    spin_unlock(ptl)

Commit 243cf1f67b36 ("mm: memory: fix truncation removing mapped folios")
has already moved the PMD clear operation (via zap_empty_pte_table()) to
after tlb_flush_rmaps(). Similarly, page_vma_mapped_walk() also needs to
use sync_with_folio_pmd_zap() to synchronize and avoid memory ordering
issues on weakly ordered CPUs.

Fixes: 6375e95f381e ("mm: pgtable: reclaim empty PTE page in madvise(MADV_DONTNEED)")
Cc: stable@vger.kernel.org
Suggested-by: David Hildenbrand (Arm) <david@kernel.org>
Signed-off-by: Baolin Wang <baolin.wang@linux.alibaba.com>
---
 mm/page_vma_mapped.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)

diff --git a/mm/page_vma_mapped.c b/mm/page_vma_mapped.c
index e6b6170c2f4f..8797467b6f0c 100644
--- a/mm/page_vma_mapped.c
+++ b/mm/page_vma_mapped.c
@@ -293,10 +293,7 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw)
 			spin_unlock(pvmw->ptl);
 			pvmw->ptl = NULL;
 		} else if (!pmd_present(pmde)) {
-			if ((pvmw->flags & PVMW_SYNC) &&
-			    thp_vma_suitable_order(vma, pvmw->address,
-						   PMD_ORDER) &&
-			    (pvmw->nr_pages >= HPAGE_PMD_NR))
+			if (pvmw->flags & PVMW_SYNC)
 				sync_with_folio_pmd_zap(mm, pvmw->pmd);
 
 			goto next_pmd;
-- 
2.47.3


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] mm: synchronize with PTE table reclaim for all folio sizes
  2026-11-05  2:56 [PATCH] mm: synchronize with PTE table reclaim for all folio sizes Baolin Wang
@ 2026-11-05  3:22 ` Qi Zheng
  2026-11-05  4:54 ` Lance Yang
  1 sibling, 0 replies; 4+ messages in thread
From: Qi Zheng @ 2026-11-05  3:22 UTC (permalink / raw)
  To: Baolin Wang, akpm, david, ljs
  Cc: riel, liam, vbabka, harry, jannh, lance.yang, pfalcato, ayushr,
	linux-mm, linux-kernel



On 11/5/26 10:56 AM, Baolin Wang wrote:
> Similar to commit 243cf1f67b36 ("mm: memory: fix truncation removing mapped
> folios"), the '!pmd_present()' check in page_vma_mapped_walk() can also race
> with concurrent page table reclamation via zap_empty_pte_table().
> 
> page_vma_mapped_walk() is used by many functions including try_to_unmap(),
> try_to_migrate(), folio_mkclean(), pfn_mkclean_range(), remove_migration_ptes(),
> page_mapped_in_vma(), and mapping_wrprotect_range(). Except for try_to_unmap()
> and try_to_migrate(), the other callers do not perform post-walk accounting
> checks, so the race has no observable impact on them.
> 
> For migration, the race can cause unnecessary retries. For reclaim via
> try_to_unmap(), the race can cause failure to reclaim a folio that is in
> fact already unmapped (as shown below), which may lead to unexpected OOM
> or increased refaults.
> 
> CPU 0 (reclaim)                      CPU 1 (MADV_DONTNEED)
> --------------------------------    --------------------------------
> folio_lock(F)
> try_to_unmap(F, TTU_SYNC)
>                                      zap_pte_range(full PMD range)
>                                        spin_lock(ptl)
>                                        clear PTEs
>                                        zap_empty_pte_table()
>                                          spin_trylock(pml)
>                                          pmd_clear()
>                                          spin_unlock(pml)
> 
>    page_vma_mapped_walk()
>      observe !pmd_present(pmde)
>      nr_pages < HPAGE_PMD_NR
>      skip synchronization
>      return false
> 
> try_to_unmap() returns
> folio_mapped(F) == true
> nr_unmap_fail += folio_nr_pages(F)
>                                      tlb_flush_mmu_tlbonly()
>                                      tlb_flush_rmaps()
>                                        remove F's rmaps
>                                      spin_unlock(ptl)
> 
> Commit 243cf1f67b36 ("mm: memory: fix truncation removing mapped folios")
> has already moved the PMD clear operation (via zap_empty_pte_table()) to
> after tlb_flush_rmaps(). Similarly, page_vma_mapped_walk() also needs to
> use sync_with_folio_pmd_zap() to synchronize and avoid memory ordering
> issues on weakly ordered CPUs.
> 
> Fixes: 6375e95f381e ("mm: pgtable: reclaim empty PTE page in madvise(MADV_DONTNEED)")
> Cc: stable@vger.kernel.org
> Suggested-by: David Hildenbrand (Arm) <david@kernel.org>
> Signed-off-by: Baolin Wang <baolin.wang@linux.alibaba.com>
> ---
>   mm/page_vma_mapped.c | 5 +----
>   1 file changed, 1 insertion(+), 4 deletions(-)
> 
> diff --git a/mm/page_vma_mapped.c b/mm/page_vma_mapped.c
> index e6b6170c2f4f..8797467b6f0c 100644
> --- a/mm/page_vma_mapped.c
> +++ b/mm/page_vma_mapped.c
> @@ -293,10 +293,7 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw)
>   			spin_unlock(pvmw->ptl);
>   			pvmw->ptl = NULL;
>   		} else if (!pmd_present(pmde)) {
> -			if ((pvmw->flags & PVMW_SYNC) &&
> -			    thp_vma_suitable_order(vma, pvmw->address,
> -						   PMD_ORDER) &&
> -			    (pvmw->nr_pages >= HPAGE_PMD_NR))
> +			if (pvmw->flags & PVMW_SYNC)
>   				sync_with_folio_pmd_zap(mm, pvmw->pmd);
>   
>   			goto next_pmd;

Acked-by: Qi Zheng <qi.zheng@linux.dev>

Thanks,
Qi




^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] mm: synchronize with PTE table reclaim for all folio sizes
  2026-11-05  2:56 [PATCH] mm: synchronize with PTE table reclaim for all folio sizes Baolin Wang
  2026-11-05  3:22 ` Qi Zheng
@ 2026-11-05  4:54 ` Lance Yang
  2026-11-05  5:48   ` Lance Yang
  1 sibling, 1 reply; 4+ messages in thread
From: Lance Yang @ 2026-11-05  4:54 UTC (permalink / raw)
  To: baolin.wang
  Cc: akpm, david, ljs, riel, liam, vbabka, harry, jannh, lance.yang,
	qi.zheng, pfalcato, ayushr, linux-mm, linux-kernel


On Thu, Nov 05, 2026 at 10:56:28AM +0800, Baolin Wang wrote:
>Similar to commit 243cf1f67b36 ("mm: memory: fix truncation removing mapped
>folios"), the '!pmd_present()' check in page_vma_mapped_walk() can also race
>with concurrent page table reclamation via zap_empty_pte_table().

Good catch!

>page_vma_mapped_walk() is used by many functions including try_to_unmap(),
>try_to_migrate(), folio_mkclean(), pfn_mkclean_range(), remove_migration_ptes(),
>page_mapped_in_vma(), and mapping_wrprotect_range(). Except for try_to_unmap()
>and try_to_migrate(), the other callers do not perform post-walk accounting
>checks, so the race has no observable impact on them.
>
>For migration, the race can cause unnecessary retries. For reclaim via
>try_to_unmap(), the race can cause failure to reclaim a folio that is in
>fact already unmapped (as shown below), which may lead to unexpected OOM
>or increased refaults.
>
>CPU 0 (reclaim)                      CPU 1 (MADV_DONTNEED)
>--------------------------------    --------------------------------
>folio_lock(F)
>try_to_unmap(F, TTU_SYNC)
>                                    zap_pte_range(full PMD range)
>                                      spin_lock(ptl)
>                                      clear PTEs
>                                      zap_empty_pte_table()
>                                        spin_trylock(pml)
>                                        pmd_clear()
>                                        spin_unlock(pml)
>
>  page_vma_mapped_walk()
>    observe !pmd_present(pmde)
>    nr_pages < HPAGE_PMD_NR
>    skip synchronization
>    return false
>
>try_to_unmap() returns
>folio_mapped(F) == true
>nr_unmap_fail += folio_nr_pages(F)
>                                    tlb_flush_mmu_tlbonly()
>                                    tlb_flush_rmaps()
>                                      remove F's rmaps
>                                    spin_unlock(ptl)
>
>Commit 243cf1f67b36 ("mm: memory: fix truncation removing mapped folios")
>has already moved the PMD clear operation (via zap_empty_pte_table()) to
>after tlb_flush_rmaps(). Similarly, page_vma_mapped_walk() also needs to
>use sync_with_folio_pmd_zap() to synchronize and avoid memory ordering
>issues on weakly ordered CPUs.
>
>Fixes: 6375e95f381e ("mm: pgtable: reclaim empty PTE page in madvise(MADV_DONTNEED)")
>Cc: stable@vger.kernel.org
>Suggested-by: David Hildenbrand (Arm) <david@kernel.org>
>Signed-off-by: Baolin Wang <baolin.wang@linux.alibaba.com>
>---
> mm/page_vma_mapped.c | 5 +----
> 1 file changed, 1 insertion(+), 4 deletions(-)
>
>diff --git a/mm/page_vma_mapped.c b/mm/page_vma_mapped.c
>index e6b6170c2f4f..8797467b6f0c 100644
>--- a/mm/page_vma_mapped.c
>+++ b/mm/page_vma_mapped.c
>@@ -293,10 +293,7 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw)
> 			spin_unlock(pvmw->ptl);
> 			pvmw->ptl = NULL;
> 		} else if (!pmd_present(pmde)) {
>-			if ((pvmw->flags & PVMW_SYNC) &&
>-			    thp_vma_suitable_order(vma, pvmw->address,
>-						   PMD_ORDER) &&
>-			    (pvmw->nr_pages >= HPAGE_PMD_NR))
>+			if (pvmw->flags & PVMW_SYNC)

Right. Whether the folio can be PMD-mapped doesn't tell us whether its
PTE table can be reclaimed, so those extra checks can skip synchronization
that the caller explicitly asked for ;)

LGTM, thanks!

Reviewed-by: Lance Yang <lance.yang@linux.dev>

> 				sync_with_folio_pmd_zap(mm, pvmw->pmd);
> 
> 			goto next_pmd;
>-- 
>2.47.3
>
>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] mm: synchronize with PTE table reclaim for all folio sizes
  2026-11-05  4:54 ` Lance Yang
@ 2026-11-05  5:48   ` Lance Yang
  0 siblings, 0 replies; 4+ messages in thread
From: Lance Yang @ 2026-11-05  5:48 UTC (permalink / raw)
  To: baolin.wang
  Cc: akpm, david, ljs, riel, liam, vbabka, harry, jannh, qi.zheng,
	pfalcato, ayushr, linux-mm, linux-kernel



On 2026/11/5 12:54, Lance Yang wrote:
> 
> On Thu, Nov 05, 2026 at 10:56:28AM +0800, Baolin Wang wrote:
>> Similar to commit 243cf1f67b36 ("mm: memory: fix truncation removing mapped
>> folios"), the '!pmd_present()' check in page_vma_mapped_walk() can also race
>> with concurrent page table reclamation via zap_empty_pte_table().
> 
> Good catch!
> 
>> page_vma_mapped_walk() is used by many functions including try_to_unmap(),
>> try_to_migrate(), folio_mkclean(), pfn_mkclean_range(), remove_migration_ptes(),
>> page_mapped_in_vma(), and mapping_wrprotect_range(). Except for try_to_unmap()
>> and try_to_migrate(), the other callers do not perform post-walk accounting
>> checks, so the race has no observable impact on them.
>>
>> For migration, the race can cause unnecessary retries. For reclaim via
>> try_to_unmap(), the race can cause failure to reclaim a folio that is in
>> fact already unmapped (as shown below), which may lead to unexpected OOM
>> or increased refaults.
>>
>> CPU 0 (reclaim)                      CPU 1 (MADV_DONTNEED)
>> --------------------------------    --------------------------------
>> folio_lock(F)
>> try_to_unmap(F, TTU_SYNC)
>>                                     zap_pte_range(full PMD range)
>>                                       spin_lock(ptl)
>>                                       clear PTEs
>>                                       zap_empty_pte_table()
>>                                         spin_trylock(pml)
>>                                         pmd_clear()
>>                                         spin_unlock(pml)
>>
>>   page_vma_mapped_walk()
>>     observe !pmd_present(pmde)
>>     nr_pages < HPAGE_PMD_NR
>>     skip synchronization
>>     return false
>>
>> try_to_unmap() returns
>> folio_mapped(F) == true
>> nr_unmap_fail += folio_nr_pages(F)
>>                                     tlb_flush_mmu_tlbonly()
>>                                     tlb_flush_rmaps()
>>                                       remove F's rmaps
>>                                     spin_unlock(ptl)
>>
>> Commit 243cf1f67b36 ("mm: memory: fix truncation removing mapped folios")
>> has already moved the PMD clear operation (via zap_empty_pte_table()) to
>> after tlb_flush_rmaps(). Similarly, page_vma_mapped_walk() also needs to
>> use sync_with_folio_pmd_zap() to synchronize and avoid memory ordering
>> issues on weakly ordered CPUs.
>>
>> Fixes: 6375e95f381e ("mm: pgtable: reclaim empty PTE page in madvise(MADV_DONTNEED)")
>> Cc: stable@vger.kernel.org
>> Suggested-by: David Hildenbrand (Arm) <david@kernel.org>
>> Signed-off-by: Baolin Wang <baolin.wang@linux.alibaba.com>
>> ---
>> mm/page_vma_mapped.c | 5 +----
>> 1 file changed, 1 insertion(+), 4 deletions(-)
>>
>> diff --git a/mm/page_vma_mapped.c b/mm/page_vma_mapped.c
>> index e6b6170c2f4f..8797467b6f0c 100644
>> --- a/mm/page_vma_mapped.c
>> +++ b/mm/page_vma_mapped.c
>> @@ -293,10 +293,7 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw)
>> 			spin_unlock(pvmw->ptl);
>> 			pvmw->ptl = NULL;
>> 		} else if (!pmd_present(pmde)) {
>> -			if ((pvmw->flags & PVMW_SYNC) &&
>> -			    thp_vma_suitable_order(vma, pvmw->address,
>> -						   PMD_ORDER) &&
>> -			    (pvmw->nr_pages >= HPAGE_PMD_NR))
>> +			if (pvmw->flags & PVMW_SYNC)
> 
> Right. Whether the folio can be PMD-mapped doesn't tell us whether its
> PTE table can be reclaimed, so those extra checks can skip synchronization
> that the caller explicitly asked for ;)

Ouch ... one thing I missed ... what about order-0 reclaim?

shrink_folio_list() only sets TTU_SYNC for large folio, so can't we still
hit the same race there?

Cheers, Lance

> LGTM, thanks!
> 
> Reviewed-by: Lance Yang <lance.yang@linux.dev>
> 
>> 				sync_with_folio_pmd_zap(mm, pvmw->pmd);
>>
>> 			goto next_pmd;
>> -- 
>> 2.47.3
>>
>>


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-11-05  5:48 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-05  2:56 [PATCH] mm: synchronize with PTE table reclaim for all folio sizes Baolin Wang
2026-11-05  3:22 ` Qi Zheng
2026-11-05  4:54 ` Lance Yang
2026-11-05  5:48   ` Lance Yang

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®