From: Kairui Song <ryncsn@gmail.com>
To: linux-mm@kvack.org
Cc: Andrew Morton <akpm@linux-foundation.org>,
"Liam R. Howlett" <Liam.Howlett@oracle.com>,
Lorenzo Stoakes <lorenzo.stoakes@oracle.com>,
Vlastimil Babka <vbabka@suse.cz>, Jann Horn <jannh@google.com>,
Pedro Falcato <pfalcato@suse.de>,
Matthew Wilcox <willy@infradead.org>,
Hugh Dickins <hughd@google.com>,
David Hildenbrand <david@redhat.com>,
Chris Li <chrisl@kernel.org>, Barry Song <baohua@kernel.org>,
Baoquan He <bhe@redhat.com>, Nhat Pham <nphamcs@gmail.com>,
Kemeng Shi <shikemeng@huaweicloud.com>,
linux-kernel@vger.kernel.org, Kairui Song <kasong@tencent.com>
Subject: [RFC PATCH 2/3] mm/mincore: use a helper for checking the swap cache
Date: Thu, 7 Aug 2025 23:27:19 +0800 [thread overview]
Message-ID: <20250807152720.62032-3-ryncsn@gmail.com> (raw)
In-Reply-To: <20250807152720.62032-1-ryncsn@gmail.com>
From: Kairui Song <kasong@tencent.com>
Introduce a mincore_swap helper for checking swap entries, seperate it
from page cache checking.
The new helper will only be called on swap address space, so it always
grab the swap device before checking the entry, caller won't need to
lock anything.
The sanity WARN_ON check will also cover all use case now, previously it
only worked for PTE checking.
Signed-off-by: Kairui Song <kasong@tencent.com>
---
mm/mincore.c | 58 ++++++++++++++++++++++++++++------------------------
1 file changed, 31 insertions(+), 27 deletions(-)
diff --git a/mm/mincore.c b/mm/mincore.c
index f0d3c9419e58..1ac53acac239 100644
--- a/mm/mincore.c
+++ b/mm/mincore.c
@@ -47,6 +47,31 @@ static int mincore_hugetlb(pte_t *pte, unsigned long hmask, unsigned long addr,
return 0;
}
+static unsigned char mincore_swap(swp_entry_t entry)
+{
+ struct swap_info_struct *si;
+ struct folio *folio = NULL;
+ unsigned char present = 0;
+
+ if (!IS_ENABLED(CONFIG_SWAP)) {
+ WARN_ON(1);
+ return 1;
+ }
+
+ si = get_swap_device(entry);
+ if (si) {
+ folio = filemap_get_folio(swap_address_space(entry),
+ swap_cache_index(entry));
+ put_swap_device(si);
+ if (folio) {
+ present = folio_test_uptodate(folio);
+ folio_put(folio);
+ }
+ }
+
+ return present;
+}
+
/*
* Later we can get more picky about what "in core" means precisely.
* For now, simply check to see if the page is in the page cache,
@@ -64,33 +89,18 @@ static unsigned char mincore_page(struct address_space *mapping, pgoff_t index)
* any other file mapping (ie. marked !present and faulted in with
* tmpfs's .fault). So swapped out tmpfs mappings are tested here.
*/
- if (IS_ENABLED(CONFIG_SWAP) && shmem_mapping(mapping)) {
- folio = filemap_get_entry(mapping, index);
- /*
- * shmem/tmpfs may return swap: account for swapcache
- * page too.
- */
+ folio = filemap_get_entry(mapping, index);
+ if (folio) {
if (xa_is_value(folio)) {
- struct swap_info_struct *si;
swp_entry_t swp = radix_to_swp_entry(folio);
/* There might be swapin error entries in shmem mapping. */
if (non_swap_entry(swp))
return 0;
- /* Prevent swap device to being swapoff under us */
- si = get_swap_device(swp);
- if (si) {
- folio = filemap_get_folio(swap_address_space(swp),
- swap_cache_index(swp));
- put_swap_device(si);
- } else {
+ if (shmem_mapping(mapping))
+ return mincore_swap(swp);
+ else
return 0;
- }
}
- } else {
- folio = filemap_get_folio(mapping, index);
- }
-
- if (folio) {
present = folio_test_uptodate(folio);
folio_put(folio);
}
@@ -177,13 +187,7 @@ static int mincore_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end,
*/
*vec = 1;
} else {
-#ifdef CONFIG_SWAP
- *vec = mincore_page(swap_address_space(entry),
- swap_cache_index(entry));
-#else
- WARN_ON(1);
- *vec = 1;
-#endif
+ *vec = mincore_swap(entry);
}
}
vec += step;
--
2.50.1
next prev parent reply other threads:[~2025-08-07 15:27 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-08-07 15:27 [RFC PATCH 0/3] mm/mincore: clean up swap cache helper and PTL Kairui Song
2025-08-07 15:27 ` [RFC PATCH 1/3] mm/mincore, swap: consolidate swap cache checking for mincore Kairui Song
2025-08-07 18:06 ` Nhat Pham
2025-08-07 18:23 ` Kairui Song
2025-08-07 15:27 ` Kairui Song [this message]
2025-08-07 15:27 ` [RFC PATCH 3/3] mm/mincore: avoid touching the PTL Kairui Song
2025-08-07 16:02 ` Jann Horn
2025-08-07 17:27 ` Kairui Song
2025-08-07 17:45 ` Jann Horn
2025-08-07 18:09 ` Kairui Song
2025-08-11 8:41 ` David Hildenbrand
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=20250807152720.62032-3-ryncsn@gmail.com \
--to=ryncsn@gmail.com \
--cc=Liam.Howlett@oracle.com \
--cc=akpm@linux-foundation.org \
--cc=baohua@kernel.org \
--cc=bhe@redhat.com \
--cc=chrisl@kernel.org \
--cc=david@redhat.com \
--cc=hughd@google.com \
--cc=jannh@google.com \
--cc=kasong@tencent.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=lorenzo.stoakes@oracle.com \
--cc=nphamcs@gmail.com \
--cc=pfalcato@suse.de \
--cc=shikemeng@huaweicloud.com \
--cc=vbabka@suse.cz \
--cc=willy@infradead.org \
/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).