Re: [RFC PATCH 3/4] mm/shmem: optimize file read with folio batching

From: Chi Zhiling

Date: Mon May 18 2026 - 22:17:35 EST


On 5/18/26 14:57, Baolin Wang wrote:


On 5/15/26 5:47 PM, Chi Zhiling wrote:
From: Chi Zhiling <chizhiling@xxxxxxxxxx>

Optimize shmem file read by implementing folio batching in the read
iteration path. Only uptodate folios are added to the batch, ensuring
all folios in the batch are valid and ready for use without additional
checking.

Signed-off-by: Chi Zhiling <chizhiling@xxxxxxxxxx>
---
  mm/shmem.c | 102 ++++++++++++++++++++++++++++++++++++++++++++++++-----
  1 file changed, 93 insertions(+), 9 deletions(-)

diff --git a/mm/shmem.c b/mm/shmem.c
index 767610f78d0d..4bc4e463ca97 100644
--- a/mm/shmem.c
+++ b/mm/shmem.c
@@ -3348,16 +3348,100 @@ shmem_write_end(const struct kiocb *iocb, struct address_space *mapping,
      return copied;
  }
+static pgoff_t shmem_get_read_batch(struct address_space *mapping,
+        pgoff_t index, pgoff_t max, struct folio_batch *fbatch)
+{
+    XA_STATE(xas, &mapping->i_pages, index);
+    struct folio *folio;
+    pgoff_t end = max;
+
+    rcu_read_lock();
+    xas_for_each(&xas, folio, max) {
+        if (xas_retry(&xas, folio))
+            continue;
+        if (xa_is_value(folio)) {
+            end = xas.xa_index;
+            break;
+        }
+        if (!folio_try_get(folio))
+            goto retry;
+
+        if (unlikely(folio != xas_reload(&xas)))
+            goto put_folio;
+
+        end = folio_next_index(folio);
+
+        if (!folio_test_uptodate(folio)) {
+            xas_advance(&xas, end - 1);
+            folio_put(folio);
+            continue;
+        }
+        if (!folio_batch_add(fbatch, folio))
+            break;
+        xas_advance(&xas, end - 1);
+        continue;
+put_folio:
+        folio_put(folio);
+retry:
+        xas_reset(&xas);
+    }
+    rcu_read_unlock();
+
+    return end;
+}

I haven't looked at this patch in detail yet, but I feel this part should belong in mm/filemap.c, or perhaps mm/filemap.c already has a similar function implemented? Let's wait for Matthew's comments on this.

You are right. The existing filemap_get_folios_contig can handle this with clearer code, nice!


Thanks,