linux-fsdevel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v14] mm: don't set readahead flag on a folio when lookahead_size > nr_to_read
@ 2024-10-15 16:41 Pankaj Raghav (Samsung)
  2024-10-15 17:29 ` Pankaj Raghav
  2024-10-15 17:33 ` Matthew Wilcox
  0 siblings, 2 replies; 6+ messages in thread
From: Pankaj Raghav (Samsung) @ 2024-10-15 16:41 UTC (permalink / raw)
  To: akpm
  Cc: linux-mm, willy, linux-fsdevel, mcgrof, gost.dev, kernel,
	Pankaj Raghav

From: Pankaj Raghav <p.raghav@samsung.com>

The readahead flag is set on a folio based on the lookahead_size and
nr_to_read. For example, when the readahead happens from index to index
+ nr_to_read, then the readahead `mark` offset from index is set at
nr_to_read - lookahead_size.

There are some scenarios where the lookahead_size > nr_to_read. If this
happens, readahead flag is not set on any folio on the current
readahead window.

There are two problems at the moment in the way `mark` is calculated
when lookahead_size > nr_to_read:

- unsigned long `mark` will be assigned a negative value which can lead
  to unexpected results in extreme cases due to wrap around.

- The current calculation for `mark` with mapping_min_order > 0 gives
  incorrect results when lookahead_size > nr_to_read due to rounding
  up operation.

Explicitly initialize `mark` to be ULONG_MAX and only calculate it
when lookahead_size is within the readahead window.

Fixes: 26cfdb395eef ("readahead: allocate folios with mapping_min_order in readahead")
Signed-off-by: Pankaj Raghav <p.raghav@samsung.com>
---
 mm/readahead.c | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)

diff --git a/mm/readahead.c b/mm/readahead.c
index 3dc6c7a128dd..475d2940a1ed 100644
--- a/mm/readahead.c
+++ b/mm/readahead.c
@@ -206,9 +206,9 @@ void page_cache_ra_unbounded(struct readahead_control *ractl,
 		unsigned long nr_to_read, unsigned long lookahead_size)
 {
 	struct address_space *mapping = ractl->mapping;
-	unsigned long ra_folio_index, index = readahead_index(ractl);
+	unsigned long index = readahead_index(ractl);
 	gfp_t gfp_mask = readahead_gfp_mask(mapping);
-	unsigned long mark, i = 0;
+	unsigned long mark = ULONG_MAX, i = 0;
 	unsigned int min_nrpages = mapping_min_folio_nrpages(mapping);
 
 	/*
@@ -232,9 +232,14 @@ void page_cache_ra_unbounded(struct readahead_control *ractl,
 	 * index that only has lookahead or "async_region" to set the
 	 * readahead flag.
 	 */
-	ra_folio_index = round_up(readahead_index(ractl) + nr_to_read - lookahead_size,
-				  min_nrpages);
-	mark = ra_folio_index - index;
+	if (lookahead_size <= nr_to_read) {
+		unsigned long ra_folio_index;
+
+		ra_folio_index = round_up(readahead_index(ractl) +
+					  nr_to_read - lookahead_size,
+					  min_nrpages);
+		mark = ra_folio_index - index;
+	}
 	nr_to_read += readahead_index(ractl) - index;
 	ractl->_index = index;
 

base-commit: d61a00525464bfc5fe92c6ad713350988e492b88
-- 
2.44.1


^ permalink raw reply related	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2024-10-16 13:06 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-10-15 16:41 [PATCH v14] mm: don't set readahead flag on a folio when lookahead_size > nr_to_read Pankaj Raghav (Samsung)
2024-10-15 17:29 ` Pankaj Raghav
2024-10-15 17:33 ` Matthew Wilcox
2024-10-16 10:05   ` Pankaj Raghav (Samsung)
2024-10-16 11:57     ` Matthew Wilcox
2024-10-16 13:06       ` Pankaj Raghav (Samsung)

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).