Re: [PATCH] mm/huge_memory: flush dirty file PMDs before removing rmap

From: David Hildenbrand (Arm)

Date: Mon Nov 16 2026 - 09:38:49 EST


On 11/16/26 13:25, Lorenzo Stoakes (ARM) wrote:
> On Fri, Nov 13, 2026 at 06:15:26PM +0100, David Hildenbrand (Arm) wrote:
>> Unmapping a file PMD can lose concurrent stores when it races with
>> writeback. zap_huge_pmd() removes rmap and drops the PMD lock before the
>> gather flushes the TLB. Writeback can then clean the folio while another
>> CPU can still write through a stale writable TLB entry. Those stores do
>> not fault and dirty the folio again. Filesystem DAX has the same problem
>> with its dirty tags.
>>
>> Commit 5df397dec7c4 ("mm: delay page_remove_rmap() until after the TLB
>> has been flushed") fixed the same race for PTEs by adding delayed rmap
>> removal to the mmu_gather infrastructure. The PMD path still removes
>> rmap before flushing.
>>
>> Flush the TLB under the PMD lock before removing rmap for dirty file
>> PMDs. Skip the early flush at address-space teardown, when no users
>> remain, and for mappings without writeback, including shmem and device
>> DAX.
>>
>> Use tlb_can_delay_rmap() to share the PTE path's architecture checks.
>> On s390, clearing the entry already handles TLB synchronization, and
>> __tlb_remove_page_size() immediately drops the mapping reference, so
>> rmap must be removed first. UP cannot have concurrent stores from
>> another CPU while holding the page table lock. Make the predicate
>> side-effect-free and set the gather's delayed_rmap flag when an entry
>> is actually queued for delayed rmap removal.
>>
>> Queue the folio before the early flush, while the gather still has a
>> valid range. Keep batching references and drain a full batch only after
>> dropping the PMD lock. The early flush resets the gather's TLB state,
>> so draining the batch does not flush that range again.
>>
>> Although writing to files through mmap is generally discouraged,
>> unmapping such mappings may become slower: each dirty file PMD now
>> requires a TLB flush under the PMD lock. Future work should properly
>> extend the delayed-rmap machinery to PMDs and hold the PMD spinlock
>> across multiple entries, so their TLB invalidations and rmap removals
>> can be batched.
>>
>> Fixes: 4b4bb46d00b3 ("dax: clear dirty entry tags on cache flush")
>> Cc: stable@xxxxxxxxxxxxxxx
>> Reported-by: Kiryl Shutsemau <kas@xxxxxxxxxx>
>> Link: https://lore.kernel.org/linux-mm/aoWw_iKEDLjKL2jr@thinkstation/
>> Reported-by: Chris Mason <mason@xxxxxxxxxx>
>> Suggested-by: Lance Yang <lance.yang@xxxxxxxxx>
>> Suggested-by: Usama Arif <usama.arif@xxxxxxxxx>
>> Signed-off-by: David Hildenbrand (Arm) <david@xxxxxxxxxx>
>> ---
>> include/asm-generic/tlb.h | 10 +++-------
>> mm/huge_memory.c | 38 ++++++++++++++++++++++++++++++--------
>> mm/memory.c | 2 +-
>> mm/mmu_gather.c | 2 ++
>> 4 files changed, 36 insertions(+), 16 deletions(-)
>>
>> diff --git a/include/asm-generic/tlb.h b/include/asm-generic/tlb.h
>> index 4bba02fd5d543..2dcf4ce0de589 100644
>> --- a/include/asm-generic/tlb.h
>> +++ b/include/asm-generic/tlb.h
>> @@ -250,11 +250,7 @@ bool __tlb_remove_folio_pages(struct mmu_gather *tlb, struct page *page,
>> unsigned int nr_pages, bool delay_rmap);
>>
>> #ifdef CONFIG_SMP
>> -/*
>> - * This both sets 'delayed_rmap', and returns true. It would be an inline
>> - * function, except we define it before the 'struct mmu_gather'.
>> - */
>> -#define tlb_delay_rmap(tlb) (((tlb)->delayed_rmap = 1), true)
>> +#define tlb_can_delay_rmap() (true)
>> extern void tlb_flush_rmaps(struct mmu_gather *tlb, struct vm_area_struct *vma);
>> #endif
>>
>> @@ -267,8 +263,8 @@ extern void tlb_flush_rmaps(struct mmu_gather *tlb, struct vm_area_struct *vma);
>> * remote TLBs to flush and is not preemptible due to this
>> * all happening under the page table lock.
>> */
>> -#ifndef tlb_delay_rmap
>> -#define tlb_delay_rmap(tlb) (false)
>> +#ifndef tlb_can_delay_rmap
>> +#define tlb_can_delay_rmap() (false)
>> static inline void tlb_flush_rmaps(struct mmu_gather *tlb, struct vm_area_struct *vma) { }
>> #endif
>>
>> diff --git a/mm/huge_memory.c b/mm/huge_memory.c
>> index 7791872f14af7..600a68a54ecf8 100644
>> --- a/mm/huge_memory.c
>> +++ b/mm/huge_memory.c
>> @@ -2508,10 +2508,12 @@ static inline void zap_deposited_table(struct mm_struct *mm, pmd_t *pmd)
>> mm_dec_nr_ptes(mm);
>> }
>>
>> -static void zap_huge_pmd_folio(struct mm_struct *mm, struct vm_area_struct *vma,
>> - pmd_t pmdval, struct folio *folio, bool is_present)
>> +static void zap_huge_pmd_folio(struct mmu_gather *tlb, struct vm_area_struct *vma,
>> + pmd_t pmdval, struct folio *folio, bool is_present, bool *force_flush)
>> {
>> const bool is_device_private = pmd_is_device_private_entry(pmdval);
>> + struct mm_struct *mm = tlb->mm;
>> + bool delay_rmap = false;
>>
>> if (folio_test_anon(folio)) {
>> add_mm_counter(mm, MM_ANONPAGES, -HPAGE_PMD_NR);
>> @@ -2519,23 +2521,41 @@ static void zap_huge_pmd_folio(struct mm_struct *mm, struct vm_area_struct *vma,
>> add_mm_counter(mm, mm_counter_file(folio),
>> -HPAGE_PMD_NR);
>>
>> - if (is_present && pmd_dirty(pmdval))
>> + if (is_present && pmd_dirty(pmdval)) {
>> folio_mark_dirty(folio);
>> + /*
>> + * Remove rmap after the TLB flush if other CPUs could
>> + * still write to a mapping that supports writeback.
>> + */
>> + if (!tlb->fullmm && vma->vm_file &&
>> + mapping_can_writeback(vma->vm_file->f_mapping))
>> + delay_rmap = tlb_can_delay_rmap();
>
> Claude flagged an issue here - for UP arches this is false, so
> zap_huge_pmd_folio() doesn't flush under the PMD lock or defer the rmap removal.

Yes, and that's a preexisting issue in the PTE case. Let me dig.

--
Cheers,

David