Currently anonymous folios belonging to CoW'd MAP_PRIVATE file-backed
mappings are indexed by their page offset within the file in which they
were originally mapped.

This differs from anonymous folios belonging to pure anon mappings which
are indexed by their anonymous page offset (the address at which they'd
belong in the VMA when first faulted).

This change fixes this inconsistency, always indexing anonymous folios by
their anonymous page offset regardless of the VMA to which they belong.

The foundations have been laid such that we need only switch this
functionality on such by:

* Using linear_anon_page_index() in __folio_set_anon() to assign the
  folio's index to the anonymous linear index rather than the file-backed
  one.

* Otherwise using linear_anon_page_index() in all instances where
  anonymous folios are being referenced or manipulated.

* Replacing vma_address() with vma_filebacked_address() or
  vma_anon_address() as appropriate.

* Updating the merging logic to check that anonymous page offsets are
  aligned as well as filebacked ones for MAP_PRIVATE file-backed VMAs,
  introducing needs_adjacent_anon_pgoff() to figure out when this is
  required.

* Updating linear_folio_page_index() to invoke linear_anon_page_index()
  if the folio is anonymous.

* Updating vma_address_end() to use the VMA's anonymous page offset when
  pvmw->pgoff is anonymous.

* Correcting folio_within_range() to use anonymous page offset for
  anonymous folios.

This will have no impact on merging of anonymous VMAs, whose page offset
and anonymous page offset are identical, nor will it impact shared
file-backed VMAs, which will continue to be merged based on the file-backed
page offset.

However, MAP_PRIVATE file-backed mappings must now be aligned on anonymous
page offset as well.

In most instances this should have no impact on merging of file-backed
mappings, which are usually not merged all that often, let alone
MAP_PRIVATE mapped ones, and rarely remapped and faulted before being moved
back in place (the case in which a merge may now fail).

One subtle impact of this change is in NUMA interleaving - since commit
88c91dc58582 ("mempolicy: migration attempt to match interleave nodes"),
migration heuristically tries to maintain interleaving behaviour matching
the policy using folio indices.

When doing migration of CoW'd MAP_PRIVATE-file backed ranges, the 'base'
upon which the interleaving behaviour is performed will vary for these
ranges. However the commit notes that ranges spanning multiple VMAs will
already cause varying bases, and that this is an acceptable approximation.

It is very unlikely real world use-cases will be impacted by
this (MAP_PRIVATE file-backed mappings are already an edge case), and all
that will happen is that such ranges will cause interleaving to be rotated
over the CoW'd range, with little to no impact.

This commit lays the foundations for future scalable CoW work which needs
to track some remaps, meaning that most remap tracking can be avoided, and
in nearly all cases the anonymous page offset will be able to be used to
quickly find the VMA in an mm.

Note that the need_rmap_locks check doesn't need to be updated, as any
remapping will offset both the anonymous and file-backed page offset, so it
suffices to check only one.

Signed-off-by: Lorenzo Stoakes (ARM) <[email protected]>
---
 include/linux/pagemap.h |  9 ++++++++-
 mm/internal.h           | 28 +++++++++-------------------
 mm/interval_tree.c      |  4 ++--
 mm/ksm.c                |  7 ++++---
 mm/page_vma_mapped.c    |  2 +-
 mm/rmap.c               | 12 ++++++------
 mm/vma.c                | 28 +++++++++++++++++++++++++++-
 7 files changed, 57 insertions(+), 33 deletions(-)

diff --git a/include/linux/pagemap.h b/include/linux/pagemap.h
index 6eb8d811ba4c..939b8850df49 100644
--- a/include/linux/pagemap.h
+++ b/include/linux/pagemap.h
@@ -1150,7 +1150,11 @@ static inline pgoff_t linear_anon_page_index(const 
struct vm_area_struct *vma,
  * @vma: The VMA in which @address resides.
  * @address: The address whose absolute page offset is required.
  *
- * For compatibility, currently identical to linear_page_index().
+ * Determines whether to obtain the anonymous linear page index based on
+ * whether @folio is anonymous or not.
+ *
+ * See the descriptions of linear_anon_page_index() and linear_page_index() for
+ * details of each.
  *
  * Returns: The absolute page offset of @address within @vma.
  */
@@ -1158,6 +1162,9 @@ static inline pgoff_t linear_folio_page_index(const 
struct folio *folio,
                                              const struct vm_area_struct *vma,
                                              const unsigned long address)
 {
+       if (folio_test_anon(folio))
+               return linear_anon_page_index(vma, address);
+
        return linear_page_index(vma, address);
 }
 
diff --git a/mm/internal.h b/mm/internal.h
index fbfa68d8ecc3..47f87a256405 100644
--- a/mm/internal.h
+++ b/mm/internal.h
@@ -933,7 +933,8 @@ folio_within_range(struct folio *folio, struct 
vm_area_struct *vma,
                return false;
 
        pgoff_folio = folio_pgoff(folio);
-       pgoff_vma_start = vma_start_pgoff(vma);
+       pgoff_vma_start = folio_test_anon(folio) ?
+               vma_start_anon_pgoff(vma) : vma_start_pgoff(vma);
 
        if (start < vma->vm_start)
                start = vma->vm_start;
@@ -1044,23 +1045,8 @@ static inline unsigned long vma_filebacked_address(const 
struct vm_area_struct *
 }
 
 /**
- * vma_address - Find the virtual address a page range is mapped at.
- * @vma: The vma which maps this object.
- * @pgoff: The page offset within its object.
- * @nr_pages: The number of pages to consider.
- *
- * If any page in this range is mapped by this VMA, return the first address
- * where any of these pages appear.  Otherwise, return -EFAULT.
- */
-static inline unsigned long vma_address(const struct vm_area_struct *vma,
-               pgoff_t pgoff, unsigned long nr_pages)
-{
-       return __vma_address(vma, pgoff, vma_start_pgoff(vma), nr_pages);
-}
-
-/**
- * vma_anon_address - Find the address an anonymous folio with index 
@pgoff_anon
- * is mapped at.
+ * vma_anon_address - Find the virtual address an anonymous page range is 
mapped
+ * at.
  * @vma: The vma which maps this object.
  * @pgoff_anon: The anonymous page index belonging to the folio.
  * @nr_pages: The number of pages to consider.
@@ -1094,7 +1080,11 @@ static inline unsigned long vma_address_end(struct 
page_vma_mapped_walk *pvmw)
        if (pvmw->nr_pages == 1)
                return pvmw->address + PAGE_SIZE;
 
-       pgoff_vma_start = vma_start_pgoff(vma);
+       if (pvmw->pgoff_is_anon)
+               pgoff_vma_start = vma_start_anon_pgoff(vma);
+       else
+               pgoff_vma_start = vma_start_pgoff(vma);
+
        pgoff_end = pgoff + pvmw->nr_pages;
        address = vma->vm_start +
                ((pgoff_end - pgoff_vma_start) << PAGE_SHIFT);
diff --git a/mm/interval_tree.c b/mm/interval_tree.c
index 3ae9e106d3af..7bbbf15cfbf0 100644
--- a/mm/interval_tree.c
+++ b/mm/interval_tree.c
@@ -83,12 +83,12 @@ mapping_rmap_tree_iter_next(struct vm_area_struct *vma,
 
 static pgoff_t avc_start_pgoff(struct anon_vma_chain *avc)
 {
-       return vma_start_pgoff(avc->vma);
+       return vma_start_anon_pgoff(avc->vma);
 }
 
 static pgoff_t avc_last_pgoff(struct anon_vma_chain *avc)
 {
-       return vma_last_pgoff(avc->vma);
+       return vma_last_anon_pgoff(avc->vma);
 }
 
 INTERVAL_TREE_DEFINE(struct anon_vma_chain, rb, pgoff_t, rb_subtree_last,
diff --git a/mm/ksm.c b/mm/ksm.c
index 47006f494fcb..7a3f75fc8af0 100644
--- a/mm/ksm.c
+++ b/mm/ksm.c
@@ -1625,7 +1625,8 @@ static int try_to_merge_with_ksm_page(struct 
ksm_rmap_item *rmap_item,
         * stable_tree, break_cow() will clean it up.
         */
        rmap_item->anon_vma = vma->anon_vma;
-       rmap_item->linear_page_index = linear_page_index(vma, 
rmap_item->address);
+       /* The VMA is always anon/MAP_PRIVATE-file backed so use anon index. */
+       rmap_item->linear_page_index = linear_anon_page_index(vma, 
rmap_item->address);
        get_anon_vma(vma->anon_vma);
 out:
        mmap_read_unlock(mm);
@@ -3152,7 +3153,7 @@ struct folio *ksm_might_need_to_copy(struct folio *folio,
                        return folio;   /* no need to copy it */
        } else if (!anon_vma) {
                return folio;           /* no need to copy it */
-       } else if (folio->index == linear_page_index(vma, addr) &&
+       } else if (folio->index == linear_anon_page_index(vma, addr) &&
                        anon_vma->root == vma->anon_vma->root) {
                return folio;           /* still no need to copy it */
        }
@@ -3222,7 +3223,7 @@ void rmap_walk_ksm(struct folio *folio, struct 
rmap_walk_control *rwc)
                /*
                 * Currently, KSM folios are always small folios, so it's
                 * sufficient to search for a single page. We can simply use
-                * the linear_page_index of the original de-duplicate
+                * the linear_anon_page_index of the original de-duplicate
                 * anonymous page that we remembered in the rmap_item while
                 * de-duplicating. Note that mremap() always de-duplicates KSM
                 * folios: so if there was mremap() in our parent or our child,
diff --git a/mm/page_vma_mapped.c b/mm/page_vma_mapped.c
index 081e483cc7bf..4e964545e5e8 100644
--- a/mm/page_vma_mapped.c
+++ b/mm/page_vma_mapped.c
@@ -365,7 +365,7 @@ unsigned long page_mapped_in_vma(const struct page *page,
        };
 
        if (folio_test_anon(folio))
-               pvmw.address = vma_address(vma, pgoff, 1);
+               pvmw.address = vma_anon_address(vma, pgoff, 1);
        else
                pvmw.address = vma_filebacked_address(vma, pgoff, 1);
        if (pvmw.address == -EFAULT)
diff --git a/mm/rmap.c b/mm/rmap.c
index ab3f879454de..2a9ae25ae72d 100644
--- a/mm/rmap.c
+++ b/mm/rmap.c
@@ -866,7 +866,7 @@ unsigned long page_address_in_vma(const struct folio *folio,
                    vma->anon_vma->root != anon_vma->root)
                        return -EFAULT;
                /* KSM folios don't reach here because of the !anon_vma check */
-               return vma_address(vma, page_pgoff(folio, page), 1);
+               return vma_anon_address(vma, page_pgoff(folio, page), 1);
        } else if (!vma->vm_file) {
                return -EFAULT;
        } else if (vma->vm_file->f_mapping != folio->mapping) {
@@ -1485,7 +1485,7 @@ static void __folio_set_anon(struct folio *folio, struct 
vm_area_struct *vma,
         */
        anon_vma = (void *) anon_vma + FOLIO_MAPPING_ANON;
        WRITE_ONCE(folio->mapping, (struct address_space *) anon_vma);
-       folio->index = linear_page_index(vma, address);
+       folio->index = linear_anon_page_index(vma, address);
 }
 
 /**
@@ -1512,8 +1512,8 @@ static void __page_check_anon_rmap(const struct folio 
*folio,
         */
        VM_BUG_ON_FOLIO(folio_anon_vma(folio)->root != vma->anon_vma->root,
                        folio);
-       VM_BUG_ON_PAGE(page_pgoff(folio, page) != linear_page_index(vma, 
address),
-                      page);
+       VM_BUG_ON_PAGE(page_pgoff(folio, page) !=
+                      linear_anon_page_index(vma, address), page);
 }
 
 static __always_inline void __folio_add_anon_rmap(struct folio *folio,
@@ -3040,10 +3040,10 @@ static void rmap_walk_anon(struct folio *folio,
        pgoff_end = pgoff_start + folio_nr_pages(folio) - 1;
        anon_rmap_tree_foreach(avc, anon_vma, pgoff_start, pgoff_end) {
                struct vm_area_struct *vma = avc->vma;
-               unsigned long address = vma_address(vma, pgoff_start,
+               const unsigned long address = vma_anon_address(vma, pgoff_start,
                                folio_nr_pages(folio));
 
-               VM_BUG_ON_VMA(address == -EFAULT, vma);
+               VM_WARN_ON_ONCE_VMA(address == -EFAULT, vma);
                cond_resched();
 
                if (rwc->invalid_vma && rwc->invalid_vma(vma, rwc->arg))
diff --git a/mm/vma.c b/mm/vma.c
index 91bb1c6160a0..b87fb2a42530 100644
--- a/mm/vma.c
+++ b/mm/vma.c
@@ -204,6 +204,21 @@ static void init_multi_vma_prep(struct vma_prepare *vp,
                vp->skip_vma_uprobe = true;
 }
 
+/*
+ * Does this merge require that adjacent VMAs must have adjacent anonymous page
+ * offsets in addition to having adjacent vma->vm_pgoff?
+ *
+ * This is only required for MAP_PRIVATE-file backed mappings as the page 
offset
+ * for pure anonymous VMAs is equal to the anonymous page offset.
+ *
+ * 'Special' mappings in the sense of VDSO, VVAR etc. have !file but would in
+ * any case not be candidates for merge nor be mergeable.
+ */
+static bool needs_adjacent_anon_pgoff(const struct vma_merge_struct *vmg)
+{
+       return vmg->file && !vma_flags_test(&vmg->vma_flags, VMA_SHARED_BIT);
+}
+
 /*
  * Return true if we can merge this (vma_flags,anon_vma,file,vm_pgoff)
  * in front of (at a lower virtual address and file offset than) the vma.
@@ -225,6 +240,9 @@ static bool can_vma_merge_before(struct vma_merge_struct 
*vmg)
                return false;
        if (vmg_end_pgoff(vmg) != vma_start_pgoff(vmg->next))
                return false;
+       if (needs_adjacent_anon_pgoff(vmg) &&
+           vmg_end_anon_pgoff(vmg) != vma_start_anon_pgoff(vmg->next))
+               return false;
        return true;
 }
 
@@ -245,6 +263,9 @@ static bool can_vma_merge_after(struct vma_merge_struct 
*vmg)
                return false;
        if (vma_end_pgoff(vmg->prev) != vmg_start_pgoff(vmg))
                return false;
+       if (needs_adjacent_anon_pgoff(vmg) &&
+           vma_end_anon_pgoff(vmg->prev) != vmg_start_anon_pgoff(vmg))
+               return false;
        return true;
 }
 
@@ -2050,7 +2071,12 @@ static int anon_vma_compatible(struct vm_area_struct *a, 
struct vm_area_struct *
        if (!vma_flags_empty(&diff))
                return false;
        /* Page offset must align. */
-       return vma_end_pgoff(a) == vma_start_pgoff(b);
+       if (vma_end_pgoff(a) != vma_start_pgoff(b))
+               return false;
+       /* Only reached from anon path, so either MAP_PRIVATE file or anon. */
+       if (vma_end_anon_pgoff(a) != vma_start_anon_pgoff(b))
+               return false;
+       return true;
 }
 
 /*

-- 
2.55.0


Reply via email to