Re: [PATCH v2 1/5] mm/filemap: reduce unnecessary xarray lookups when read cached pages

From: Chi Zhiling

Date: Mon Jun 01 2026 - 10:48:55 EST


Hi, Matthew

On 6/1/26 8:51 PM, Matthew Wilcox wrote:
On Mon, Jun 01, 2026 at 01:57:00PM +0800, Chi Zhiling wrote:
From: Chi Zhiling <chizhiling@xxxxxxxxxx>

When reading small amounts of data from the page cache, only a single
folio is typically returned from filemap_read_get_batch(). In this case,
calling xas_advance() or xas_next() after adding the folio to the batch
is unnecessary and only introduces extra branches.

I don't think xas_advance() is all that expensive. The expensive part
is going to be calling xas_next() and realising that the folio now lies
beyond the required range. I think you'll get just as much benefit from
doing this:

diff --git a/mm/filemap.c b/mm/filemap.c
index 4e636647100c..578b03bd5514 100644
--- a/mm/filemap.c
+++ b/mm/filemap.c
@@ -2459,11 +2459,14 @@ static void filemap_get_read_batch(struct address_space *mapping,
XA_STATE(xas, &mapping->i_pages, index);
struct folio *folio;
+ if (index > max)
+ return;
+
rcu_read_lock();
for (folio = xas_load(&xas); folio; folio = xas_next(&xas)) {
if (xas_retry(&xas, folio))
continue;
- if (xas.xa_index > max || xa_is_value(folio))
+ if (xa_is_value(folio))
break;
if (xa_is_sibling(folio))
break;
@@ -2480,6 +2483,8 @@ static void filemap_get_read_batch(struct address_space *mapping,
if (folio_test_readahead(folio))
break;
xas_advance(&xas, folio_next_index(folio) - 1);
+ if (xas.index >= max)
+ break;
continue;
put_folio:
folio_put(folio);


Looks good. I'll include it in the next version and make the same change in patch 2/5.

Thanks!