linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: "Matthew Wilcox (Oracle)" <willy@infradead.org>
To: linux-mm@kvack.org, linux-fsdevel@vger.kernel.org
Cc: "Matthew Wilcox (Oracle)" <willy@infradead.org>,
	linux-kernel@vger.kernel.org, Yu Kuai <yukuai3@huawei.com>,
	Kent Overstreet <kent.overstreet@gmail.com>
Subject: [PATCH 1/3] filemap: Correct the conditions for marking a folio as accessed
Date: Sun, 19 Jun 2022 16:11:41 +0100	[thread overview]
Message-ID: <20220619151143.1054746-2-willy@infradead.org> (raw)
In-Reply-To: <20220619151143.1054746-1-willy@infradead.org>

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 <yukuai3@huawei.com>
Reviewed-by: Kent Overstreet <kent.overstreet@gmail.com>
Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org>
---
 mm/filemap.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)

diff --git a/mm/filemap.c b/mm/filemap.c
index ac3775c1ce4c..577068868449 100644
--- a/mm/filemap.c
+++ b/mm/filemap.c
@@ -2629,6 +2629,13 @@ static int filemap_get_pages(struct kiocb *iocb, struct iov_iter *iter,
 	return err;
 }
 
+static inline bool pos_same_folio(loff_t pos1, loff_t pos2, struct folio *folio)
+{
+	unsigned int shift = folio_shift(folio);
+
+	return (pos1 >> shift == pos2 >> shift);
+}
+
 /**
  * filemap_read - Read data from the page cache.
  * @iocb: The iocb to read.
@@ -2700,11 +2707,11 @@ ssize_t filemap_read(struct kiocb *iocb, struct iov_iter *iter,
 		writably_mapped = mapping_writably_mapped(mapping);
 
 		/*
-		 * When a sequential read accesses a page several times, only
+		 * When a read accesses the same folio several times, only
 		 * mark it as accessed the first time.
 		 */
-		if (iocb->ki_pos >> PAGE_SHIFT !=
-		    ra->prev_pos >> PAGE_SHIFT)
+		if (!pos_same_folio(iocb->ki_pos, ra->prev_pos - 1,
+							fbatch.folios[0]))
 			folio_mark_accessed(fbatch.folios[0]);
 
 		for (i = 0; i < folio_batch_count(&fbatch); i++) {
-- 
2.35.1



  reply	other threads:[~2022-06-19 15:11 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-06-19 15:11 [PATCH 0/3] Fixes for 5.19b Matthew Wilcox (Oracle)
2022-06-19 15:11 ` Matthew Wilcox (Oracle) [this message]
2022-06-20  6:25   ` [PATCH 1/3] filemap: Correct the conditions for marking a folio as accessed Christoph Hellwig
2022-06-27  9:13   ` [filemap] 91fd26c76b: phoronix-test-suite.fio.SequentialRead.LinuxAIO.Yes.No.4KB.DefaultTestDirectory.mb_s -8.9% regression kernel test robot
2022-06-30  7:29   ` Major btrfs fiemap slowdown on file with many extents once in cache (RCU stalls?) (Was: [PATCH 1/3] filemap: Correct the conditions for marking a folio as accessed) Dominique MARTINET
2022-06-30 14:37     ` Theodore Ts'o
2022-06-30 22:43       ` Dominique MARTINET
2022-07-01  1:55     ` Yu Kuai
2022-07-07  8:42       ` Dominique MARTINET
2022-06-19 15:11 ` [PATCH 2/3] filemap: Handle sibling entries in filemap_get_read_batch() Matthew Wilcox (Oracle)
2022-06-20 10:15   ` Brian Foster
2022-06-19 15:11 ` [PATCH 3/3] mm: Clear page->private when splitting or migrating a page Matthew Wilcox (Oracle)
2022-06-20  6:41   ` Xiubo Li
2022-06-20 23:42   ` Xiubo Li

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220619151143.1054746-2-willy@infradead.org \
    --to=willy@infradead.org \
    --cc=kent.overstreet@gmail.com \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=yukuai3@huawei.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).