[RFC PATCH 16/18] mm/shmem: count accesses to a folio the way filemap_read() does

From: Kiryl Shutsemau (Meta)

Date: Mon Oct 19 2026 - 14:41:51 EST


shmem_file_read_iter() marks a folio accessed only when a read starts at
the beginning of the folio. Small reads that start anywhere else never
count, so a folio that is read over and over at one offset ages as if it
were never read.

Count accesses with filemap_read_accessed(), as filemap_read() does: a
read marks the folio accessed unless it goes on in the folio that the
previous read through the file ended in. Reading the same bytes again
through one open file counts once, as for other files, where it used to
count each time it started at the beginning of the folio. The end of
the previous read is kept in f_ra.prev_pos, as for other files.

This also sets PG_rcu_free on the tmpfs folios that reads keep coming
back to. On a shmem folio the PG_readahead bit is PG_reclaim, and shmem
does no readahead, so do not let that bit keep the flag off. A write
marks a tmpfs folio accessed too, so a folio written in several pieces
can get the flag on its first read.

Assisted-by: LLM
Signed-off-by: Kiryl Shutsemau (Meta) <kas@xxxxxxxxxx>
---
mm/filemap.c | 9 +++++----
mm/internal.h | 1 +
mm/shmem.c | 11 ++++++-----
3 files changed, 12 insertions(+), 9 deletions(-)

diff --git a/mm/filemap.c b/mm/filemap.c
index ace6b1fd6b9e..4b5b7ba78f39 100644
--- a/mm/filemap.c
+++ b/mm/filemap.c
@@ -2778,8 +2778,10 @@ static void filemap_mark_hot(struct folio *folio)
if (!IS_ENABLED(CONFIG_PAGE_RCU_FREE))
return;
if (folio_test_rcu_free(folio) || !folio_test_uptodate(folio) ||
- folio_test_readahead(folio) || folio_test_dropbehind(folio) ||
- folio_test_unevictable(folio))
+ folio_test_dropbehind(folio) || folio_test_unevictable(folio))
+ return;
+ /* shmem does no readahead: there the bit is PG_reclaim */
+ if (folio_test_readahead(folio) && !folio_test_swapbacked(folio))
return;
if (folio_test_set_rcu_free(folio))
return;
@@ -2802,8 +2804,7 @@ static void filemap_mark_hot(struct folio *folio)
* LRU records, or when this open file goes back to data that it has just
* read.
*/
-static void filemap_read_accessed(struct folio *folio, loff_t pos,
- loff_t last_pos)
+void filemap_read_accessed(struct folio *folio, loff_t pos, loff_t last_pos)
{
bool hot;

diff --git a/mm/internal.h b/mm/internal.h
index 38b1165212c9..18391dc50418 100644
--- a/mm/internal.h
+++ b/mm/internal.h
@@ -1248,6 +1248,7 @@ struct migration_target_control {
*/
size_t splice_folio_into_pipe(struct pipe_inode_info *pipe,
struct folio *folio, loff_t fpos, size_t size);
+void filemap_read_accessed(struct folio *folio, loff_t pos, loff_t last_pos);

static inline bool vma_is_single_threaded_private(struct vm_area_struct *vma)
{
diff --git a/mm/shmem.c b/mm/shmem.c
index 848316eaa7f4..43ddb50974a5 100644
--- a/mm/shmem.c
+++ b/mm/shmem.c
@@ -3309,6 +3309,7 @@ static ssize_t shmem_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
struct file *file = iocb->ki_filp;
struct inode *inode = file_inode(file);
struct address_space *mapping = inode->i_mapping;
+ loff_t last_pos = file->f_ra.prev_pos;
pgoff_t index;
unsigned long offset;
int error = 0;
@@ -3378,11 +3379,7 @@ static ssize_t shmem_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
flush_dcache_page(page);
}

- /*
- * Mark the folio accessed if we read the beginning.
- */
- if (!offset)
- folio_mark_accessed(folio);
+ filemap_read_accessed(folio, iocb->ki_pos, last_pos);
/*
* Ok, we have the page, and it's up-to-date, so
* now we can copy it to user space...
@@ -3410,6 +3407,7 @@ static ssize_t shmem_file_read_iter(struct kiocb *iocb, struct iov_iter *to)

retval += ret;
iocb->ki_pos += ret;
+ last_pos = iocb->ki_pos;

if (!iov_iter_count(to))
break;
@@ -3421,6 +3419,9 @@ static ssize_t shmem_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
}

file_accessed(file);
+ /* Readers sharing the file would bounce the line for nothing */
+ if (file->f_ra.prev_pos != last_pos)
+ file->f_ra.prev_pos = last_pos;
return retval ? retval : error;
}

--
2.54.0