Commit c6f0b395b211 ("fsdax,xfs: set the shared flag when file extent is shared") replaced the write-only cow check in dax_insert_entry() with shared = iter->iomap.flags & IOMAP_F_SHARED so that dax_associate_entry() receives shared=true on read faults of shared extents as well as CoW write faults. However, unmap_mapping_pages() and XArray entry replacement were also switched from cow to shared. As a result, whenever a filesystem sets IOMAP_F_SHARED on DAX read faults (such as XFS on reflinked extents via xfs_read_iomap_begin()), every read fault on an already-populated entry zaps all mappings of this file for that range and replaces the XArray entry with an identical one. Two processes (or distinct VMAs) reading the same page of a reflinked DAX file then ping-pong: each read fault unmaps the other VMA's PTE, causing repeated page faults, XArray entry lock contention, and TLB shootdowns. Furthermore, a PTE read fault hitting an existing shared PMD entry unmaps and overwrites the PMD entry with a PTE-flagged entry. Keep shared for dax_associate_entry(), and restore cow = write && shared to gate unmap_mapping_pages(), entry replacement of existing real entries, and PAGECACHE_TAG_TOWRITE. First read faults on empty or zero entries still enter the insertion branch and pass shared=true to dax_associate_entry(), while subsequent read faults leave the existing PTE or PMD entry and other mappings intact. Fixes: c6f0b395b211 ("fsdax,xfs: set the shared flag when file extent is shared") Cc: stable@vger.kernel.org # v6.2+ Signed-off-by: Ramon Nogueira --- fs/dax.c | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/fs/dax.c b/fs/dax.c index 9d161d7ef..de3931788 100644 --- a/fs/dax.c +++ b/fs/dax.c @@ -1078,11 +1078,12 @@ static void *dax_insert_entry(struct xa_state *xas, struct vm_fault *vmf, bool write = iter->flags & IOMAP_WRITE; bool dirty = write && !dax_fault_is_synchronous(iter, vmf->vma); bool shared = iter->iomap.flags & IOMAP_F_SHARED; + bool cow = write && shared; if (dirty) __mark_inode_dirty(mapping->host, I_DIRTY_PAGES); - if (shared || (dax_is_zero_entry(entry) && !(flags & DAX_ZERO_PAGE))) { + if (cow || (dax_is_zero_entry(entry) && !(flags & DAX_ZERO_PAGE))) { unsigned long index = xas->xa_index; /* we are replacing a zero page with block mapping */ if (dax_is_pmd_entry(entry)) @@ -1094,7 +1095,7 @@ static void *dax_insert_entry(struct xa_state *xas, struct vm_fault *vmf, xas_reset(xas); xas_lock_irq(xas); - if (shared || dax_is_zero_entry(entry) || dax_is_empty_entry(entry)) { + if (cow || dax_is_zero_entry(entry) || dax_is_empty_entry(entry)) { void *old; dax_disassociate_entry(entry, mapping, false); @@ -1102,12 +1103,13 @@ static void *dax_insert_entry(struct xa_state *xas, struct vm_fault *vmf, vmf->address, shared); /* - * Only swap our new entry into the page cache if the current - * entry is a zero page or an empty entry. If a normal PTE or - * PMD entry is already in the cache, we leave it alone. This - * means that if we are trying to insert a PTE and the - * existing entry is a PMD, we will just leave the PMD in the - * tree and dirty it if necessary. + * Only swap our new entry into the page cache on a CoW write + * fault, or if the current entry is a zero page or an empty + * entry. If a normal PTE or PMD entry is already in the cache + * on a read fault, we leave it alone. This means that if we + * are trying to insert a PTE and the existing entry is a PMD, + * we will just leave the PMD in the tree and dirty it if + * necessary. */ old = dax_lock_entry(xas, new_entry); WARN_ON_ONCE(old != xa_mk_value(xa_to_value(entry) | @@ -1120,7 +1122,7 @@ static void *dax_insert_entry(struct xa_state *xas, struct vm_fault *vmf, if (dirty) xas_set_mark(xas, PAGECACHE_TAG_DIRTY); - if (write && shared) + if (cow) xas_set_mark(xas, PAGECACHE_TAG_TOWRITE); xas_unlock_irq(xas); base-commit: 9437ee8b6cf607e883b36e44bd755968daaebd48 -- 2.54.0 (Apple Git-157)