linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Kairui Song <ryncsn@gmail.com>
To: linux-mm@kvack.org
Cc: Andrew Morton <akpm@linux-foundation.org>,
	Matthew Wilcox <willy@infradead.org>,
	Hugh Dickins <hughd@google.com>, Chris Li <chrisl@kernel.org>,
	David Hildenbrand <david@redhat.com>,
	Yosry Ahmed <yosryahmed@google.com>,
	"Huang, Ying" <ying.huang@linux.alibaba.com>,
	Nhat Pham <nphamcs@gmail.com>,
	Johannes Weiner <hannes@cmpxchg.org>,
	linux-kernel@vger.kernel.org, Kairui Song <kasong@tencent.com>
Subject: [PATCH v3 4/6] filemap: do not use folio_contains for swap cache folios
Date: Thu,  1 May 2025 02:10:50 +0800	[thread overview]
Message-ID: <20250430181052.55698-5-ryncsn@gmail.com> (raw)
In-Reply-To: <20250430181052.55698-1-ryncsn@gmail.com>

From: Kairui Song <kasong@tencent.com>

Currently, none of the folio_contains callers should encounter swap
cache folios.

For fs/ callers, swap cache folios are never part of their workflow.

For filemap and truncate, folio_contains is only used for sanity
checks to verify the folio index matches the expected
lookup / invalidation target.

The swap cache does not utilize filemap or truncate helpers in ways
that would trigger these checks, as it mostly implements its own
cache management.

Shmem won't trigger these sanity checks either unless thing went
wrong, as it would directly trigger a BUG because swap cache index are
unrelated and almost never matches shmem index. Shmem have to handle
mixed values of folios, shadows, and swap entries, so it has its own
way of handling the mapping.

While some filemap helpers works for swap cache space, the swap cache
is different from the page cache in many ways. So this particular helper
will unlikely to work in a helpful way for swap cache folios.

So make it explicit here that folio_contains should not be used for
swap cache folios. This helps to avoid misuse, make swap cache less
exposed and remove the folio_index usage here.

Signed-off-by: Kairui Song <kasong@tencent.com>
Acked-by: David Hildenbrand <david@redhat.com>
---
 include/linux/pagemap.h | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/include/linux/pagemap.h b/include/linux/pagemap.h
index af25fb640463..0c9aff5ec77f 100644
--- a/include/linux/pagemap.h
+++ b/include/linux/pagemap.h
@@ -935,14 +935,14 @@ static inline struct page *folio_file_page(struct folio *folio, pgoff_t index)
  * @folio: The folio.
  * @index: The page index within the file.
  *
- * Context: The caller should have the page locked in order to prevent
- * (eg) shmem from moving the page between the page cache and swap cache
- * and changing its index in the middle of the operation.
+ * Context: The caller should have the folio locked and ensure
+ * e.g., shmem did not move this folio to the swap cache.
  * Return: true or false.
  */
 static inline bool folio_contains(struct folio *folio, pgoff_t index)
 {
-	return index - folio_index(folio) < folio_nr_pages(folio);
+	VM_WARN_ON_FOLIO(folio_test_swapcache(folio), folio);
+	return index - folio->index < folio_nr_pages(folio);
 }
 
 unsigned filemap_get_folios(struct address_space *mapping, pgoff_t *start,
-- 
2.49.0



  parent reply	other threads:[~2025-04-30 18:11 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-04-30 18:10 [PATCH v3 0/6] mm, swap: clean up swap cache mapping helper Kairui Song
2025-04-30 18:10 ` [PATCH v3 1/6] fuse: drop usage of folio_index Kairui Song
2025-04-30 21:08   ` David Hildenbrand
2025-04-30 18:10 ` [PATCH v3 2/6] btrfs: " Kairui Song
2025-04-30 21:06   ` Andrew Morton
2025-04-30 21:07     ` David Hildenbrand
2025-05-01 10:13       ` Kairui Song
2025-04-30 21:08   ` David Hildenbrand
2025-05-01  8:50   ` David Sterba
2025-05-02 11:10   ` David Sterba
2025-04-30 18:10 ` [PATCH v3 3/6] f2fs: " Kairui Song
2025-04-30 21:08   ` David Hildenbrand
2025-05-06  8:49   ` Chao Yu
2025-06-09 20:56   ` [f2fs-dev] " patchwork-bot+f2fs
2025-04-30 18:10 ` Kairui Song [this message]
2025-04-30 18:15   ` [PATCH v3 4/6] filemap: do not use folio_contains for swap cache folios Kairui Song
2025-04-30 18:10 ` [PATCH v3 5/6] mm: move folio_index to mm/swap.h and remove no longer needed helper Kairui Song
2025-04-30 21:11   ` David Hildenbrand
2025-05-01 10:08     ` Kairui Song
2025-04-30 18:10 ` [PATCH v3 6/6] mm, swap: remove no longer used swap mapping helper Kairui Song

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=20250430181052.55698-5-ryncsn@gmail.com \
    --to=ryncsn@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=chrisl@kernel.org \
    --cc=david@redhat.com \
    --cc=hannes@cmpxchg.org \
    --cc=hughd@google.com \
    --cc=kasong@tencent.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=nphamcs@gmail.com \
    --cc=willy@infradead.org \
    --cc=ying.huang@linux.alibaba.com \
    --cc=yosryahmed@google.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