Skip to content

Commit 5ccc944

Browse files
author
Matthew Wilcox (Oracle)
committed
filemap: Correct the conditions for marking a folio as accessed
We had an off-by-one error which meant that we never marked the first page in a read as accessed. This was visible as a slowdown when re-reading a file as pages were being evicted from cache too soon. In reviewing this code, we noticed a second bug where a multi-page folio would be marked as accessed multiple times when doing reads that were less than the size of the folio. Abstract the comparison of whether two file positions are in the same folio into a new function, fixing both of these bugs. Reported-by: Yu Kuai <[email protected]> Reviewed-by: Kent Overstreet <[email protected]> Signed-off-by: Matthew Wilcox (Oracle) <[email protected]>
1 parent 78ca558 commit 5ccc944

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

mm/filemap.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2629,6 +2629,13 @@ static int filemap_get_pages(struct kiocb *iocb, struct iov_iter *iter,
26292629
return err;
26302630
}
26312631

2632+
static inline bool pos_same_folio(loff_t pos1, loff_t pos2, struct folio *folio)
2633+
{
2634+
unsigned int shift = folio_shift(folio);
2635+
2636+
return (pos1 >> shift == pos2 >> shift);
2637+
}
2638+
26322639
/**
26332640
* filemap_read - Read data from the page cache.
26342641
* @iocb: The iocb to read.
@@ -2700,11 +2707,11 @@ ssize_t filemap_read(struct kiocb *iocb, struct iov_iter *iter,
27002707
writably_mapped = mapping_writably_mapped(mapping);
27012708

27022709
/*
2703-
* When a sequential read accesses a page several times, only
2710+
* When a read accesses the same folio several times, only
27042711
* mark it as accessed the first time.
27052712
*/
2706-
if (iocb->ki_pos >> PAGE_SHIFT !=
2707-
ra->prev_pos >> PAGE_SHIFT)
2713+
if (!pos_same_folio(iocb->ki_pos, ra->prev_pos - 1,
2714+
fbatch.folios[0]))
27082715
folio_mark_accessed(fbatch.folios[0]);
27092716

27102717
for (i = 0; i < folio_batch_count(&fbatch); i++) {

0 commit comments

Comments
 (0)