Re: [PATCH v2] mm: use memory barriers for non PMD-sized folios to sync
From: David Hildenbrand (Arm)
Date: Tue Nov 17 2026 - 16:20:45 EST
On 11/17/26 08:00, Baolin Wang wrote:
> Commit 243cf1f67b36 ("mm: memory: fix truncation removing mapped folios")
> uses PMD lock via sync_with_folio_pmd_zap() to synchronize when a cleared
> PMD is found while unmapping a folio.
>
> However, for non PMD-sized folios, the only PMD clear that can race with
> this unmap is PTE table reclaim, which clears the PMD only after the rmaps
> for the table have been removed. This is plain message passing, and taking
> the PMD lock is more than needed, like I did in the previous patch[1].
>
> Without barriers, the walker can observe the cleared PMD but a stale
> mapcount on weakly ordered architectures:
>
> CPU 0 (unmap folio) CPU 1 (MADV_DONTNEED, full PMD)
> ================================ ================================
> folio_lock()
> lock(ptl)
> clear PTEs
> remove rmaps /* W mapcount */
> [mapcount load satisfied early,
> folio still mapped]
> pmd_clear() /* W pmd */
> unlock(ptl)
> pmdp_get_lockless() -> none /* R pmd */
> skip the PMD
> folio_mapped() -> true /* R mapcount, stale */
> unmapping assumed to have failed
>
> Pairing an smp_wmb() before the PMD clear with an smp_rmb() after the
> PMD read forbids this outcome:
>
> CPU 0 (unmap folio) CPU 1 (MADV_DONTNEED, full PMD)
> ================================ ================================
> remove rmaps /* W mapcount */
> smp_wmb()
> pmd_clear() /* W pmd */
> pmdp_get_lockless() -> none /* R pmd */
> smp_rmb()
> folio_mapped() /* R mapcount */
>
> If R pmd observes W pmd, R mapcount must observe W mapcount, so
> folio_mapped() no longer reports the removed mappings.
>
> Pass a 'folio' parameter to sync_with_folio_pmd_zap(), allowing us to
> skip those pfn walkers: pfn_mkclean_range and mapping_wrprotect_range,
> which don't need synchronization.
>
> Similarly, skip sync for page_mapped_in_vma() since the subsequent
> operations don't check the folio's mapcount.
Do we have any way to measure the benefit of this? I think we'll need that to
convince reviewers (like Lorenzo :) )
--
Cheers,
David