[RFC 07/23] filemap: align the index to mapping_min_order in __filemap_add_folio()

From: Pankaj Raghav
Date: Fri Sep 15 2023 - 14:43:02 EST


From: Luis Chamberlain <mcgrof@xxxxxxxxxx>

Align the index to the mapping_min_order number of pages while setting
the XA_STATE and xas_set_order().

Signed-off-by: Luis Chamberlain <mcgrof@xxxxxxxxxx>
---
mm/filemap.c | 10 +++++++---
1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/mm/filemap.c b/mm/filemap.c
index 33de71bfa953..15bc810bfc89 100644
--- a/mm/filemap.c
+++ b/mm/filemap.c
@@ -859,7 +859,10 @@ EXPORT_SYMBOL_GPL(replace_page_cache_folio);
noinline int __filemap_add_folio(struct address_space *mapping,
struct folio *folio, pgoff_t index, gfp_t gfp, void **shadowp)
{
- XA_STATE(xas, &mapping->i_pages, index);
+ unsigned int min_order = mapping_min_folio_order(mapping);
+ unsigned int nr_of_pages = (1U << min_order);
+ pgoff_t rounded_index = round_down(index, nr_of_pages);
+ XA_STATE(xas, &mapping->i_pages, rounded_index);
int huge = folio_test_hugetlb(folio);
bool charged = false;
long nr = 1;
@@ -875,8 +878,8 @@ noinline int __filemap_add_folio(struct address_space *mapping,
charged = true;
}

- VM_BUG_ON_FOLIO(index & (folio_nr_pages(folio) - 1), folio);
- xas_set_order(&xas, index, folio_order(folio));
+ VM_BUG_ON_FOLIO(rounded_index & (folio_nr_pages(folio) - 1), folio);
+ xas_set_order(&xas, rounded_index, folio_order(folio));
nr = folio_nr_pages(folio);

gfp &= GFP_RECLAIM_MASK;
@@ -913,6 +916,7 @@ noinline int __filemap_add_folio(struct address_space *mapping,
}
}

+ VM_BUG_ON_FOLIO(folio_order(folio) < min_order, folio);
xas_store(&xas, folio);
if (xas_error(&xas))
goto unlock;
--
2.40.1