FAILED: patch "[PATCH] mm/khugepaged: fix race with folio split/free using temporary" failed to apply to 6.15-stable tree
Subsystems (3)
[email protected]
maintainerThe patch below does not apply to the 6.15-stable tree.
If someone wants it applied there, or to any other stable or longterm tree, then please email the backport, including the original git commit id to <[email protected]>.
To reproduce the conflict and resubmit, you may use the following commands:
git fetch https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/ linux-6.15.y
git checkout FETCH_HEAD
git cherry-pick -x 595cf683519ab5a277d258a2251ee8cc7b838d6d
# <resolve conflicts, build, test, etc.>
git commit -s
git send-email --to '<[email protected]>' --in-reply-to '2025062039-policy-handheld-01c6@gregkh' --subject-prefix 'PATCH 6.15.y' HEAD^..
Possible dependencies:
thanks,
greg k-h
------------------ original commit in Linus's tree ------------------
From 595cf683519ab5a277d258a2251ee8cc7b838d6d Mon Sep 17 00:00:00 2001
From: Shivank Garg <[email protected]>
Date: Mon, 26 May 2025 18:28:18 +0000
Subject: [PATCH] mm/khugepaged: fix race with folio split/free using temporary
reference
hpage_collapse_scan_file() calls is_refcount_suitable(), which in turn calls folio_mapcount(). folio_mapcount() checks folio_test_large() before proceeding to folio_large_mapcount(), but there is a race window where the folio may get split/freed between these checks, triggering:
VM_WARN_ON_FOLIO(!folio_test_large(folio), folio)
Take a temporary reference to the folio in hpage_collapse_scan_file(). This stabilizes the folio during refcount check and prevents incorrect large folio detection due to concurrent split/free. Use helper folio_expected_ref_count() + 1 to compare with folio_ref_count() instead of using is_refcount_suitable().
mm/khugepaged.c +17 -1
@@ -2293,6 +2293,17 @@ static int hpage_collapse_scan_file(struct mm_struct *mm, unsigned long addr,continue ; } +if (!folio_try_get (folio)) { +xas_reset (&xas); +continue ; + } + +if (unlikely (folio !=xas_reload (&xas))) { +folio_put (folio); +xas_reset (&xas); +continue ; + } +if (folio_order (folio) ==HPAGE_PMD_ORDER && folio->index == start) {/* Maybe PMD-mapped */
@@ -2303,23 +2314,27 @@ static int hpage_collapse_scan_file(struct mm_struct *mm, unsigned long addr,* it's safe to skip LRU and refcount checks before * returning. */ +folio_put (folio);break ; } node =folio_nid (folio);if (hpage_collapse_scan_abort (node, cc)) { result =SCAN_SCAN_ABORT ; +folio_put (folio);break ; } cc->node_load[node]++;if (!folio_test_lru (folio)) { result =SCAN_PAGE_LRU ; +folio_put (folio);break ; } -if (!is_refcount_suitable (folio)) { +if (folio_expected_ref_count (folio) +1 !=folio_ref_count (folio)) { result =SCAN_PAGE_COUNT ; +folio_put (folio);break ; }
@@ -2331,6 +2346,7 @@ static int hpage_collapse_scan_file(struct mm_struct *mm, unsigned long addr,*/ present +=folio_nr_pages (folio); +folio_put (folio);if (need_resched ()) {xas_pause (&xas);
PATCH 1/2 mm: add folio_expected_ref_count() for reference count calculation
Patch series " JFS: Implement migrate_folio for jfs_metapage_aops" v5.
This patchset addresses a warning that occurs during memory compaction due to JFS's missing migrate_folio operation. The warning was introduced by commit 7ee3647243e5 ("migrate: Remove call to ->writepage") which added explicit warnings when filesystem don't implement migrate_folio.
The syzbot reported following [1]:
jfs_metapage_aops does not implement migrate_folio
WARNING: CPU: 1 PID: 5861 at mm/migrate.c:955 fallback_migrate_folio mm/migrate.c:953 [inline] WARNING: CPU: 1 PID: 5861 at mm/migrate.c:955 move_to_new_folio+0x70e/0x840 mm/migrate.c:1007 Modules linked in: CPU: 1 UID: 0 PID: 5861 Comm: syz-executor280 Not tainted 6.15.0-rc1-next-20250411-syzkaller #0 PREEMPT(full) Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 02/12/2025 RIP: 0010:fallback_migrate_folio mm/migrate.c:953 [inline] RIP: 0010:move_to_new_folio+0x70e/0x840 mm/migrate.c:1007
To fix this issue, this series implement metapage_migrate_folio() for JFS which handles both single and multiple metapages per page configurations.
While most filesystems leverage existing migration implementations like filemap_migrate_folio(), buffer_migrate_folio_norefs() or buffer_migrate_folio() (which internally used folio_expected_refs()), JFS's metapage architecture requires special handling of its private data during migration. To support this, this series introduce the folio_expected_ref_count(), which calculates external references to a folio from page/swap cache, private data, and page table mappings.
This standardized implementation replaces the previous ad-hoc folio_expected_refs() function and enables JFS to accurately determine whether a folio has unexpected references before attempting migration.
Implement folio_expected_ref_count() to calculate expected folio reference counts from:
- Page/swap cache (1 per page)
- Private data (1)
- Page table mappings (1 per map)
While originally needed for page migration operations, this improved implementation standardizes reference counting by consolidating all refcount contributors into a single, reusable function that can benefit any subsystem needing to detect unexpected references to folios.
The folio_expected_ref_count() returns the sum of these external references without including any reference the caller itself might hold. Callers comparing against the actual folio_ref_count() must account for their own references separately.
--- include/linux/mm.h | 55 ++++++++++++++++++++++++++++++++++++++++++++++ mm/migrate.c | 22 ++++--------------- 2 files changed, 59 insertions(+), 18 deletions(-)
include/linux/mm.h +55 -0
@@ -2306,6 +2306,61 @@ static inline bool folio_maybe_mapped_shared(struct folio *folio)return folio_test_large_maybe_mapped_shared (folio); } +/** +* folio_expected_ref_count - calculate the expected folio refcount +* @folio: the folio +* +* Calculate the expected folio refcount, taking references from the pagecache, +* swapcache, PG_private and page table mappings into account. Useful in +* combination with folio_ref_count() to detect unexpected references (e.g., +* GUP or other temporary references). +* +* Does currently not consider references from the LRU cache. If the folio +* was isolated from the LRU (which is the case during migration or split), +* the LRU cache does not apply. +* +* Calling this function on an unmapped folio -- !folio_mapped() -- that is +* locked will return a stable result. +* +* Calling this function on a mapped folio will not result in a stable result, +* because nothing stops additional page table mappings from coming (e.g., +* fork()) or going (e.g., munmap()). +* +* Calling this function without the folio lock will also not result in a +* stable result: for example, the folio might get dropped from the swapcache +* concurrently. +* +* However, even when called without the folio lock or on a mapped folio, +* this function can be used to detect unexpected references early (for example, +* if it makes sense to even lock the folio and unmap it). +* +* The caller must add any reference (e.g., from folio_try_get()) it might be +* holding itself to the result. +* +* Returns the expected folio refcount. +*/ +static inline int folio_expected_ref_count (const struct folio *folio) +{ +const int order =folio_order (folio); +int ref_count =0 ; + +if (WARN_ON_ONCE (folio_test_slab (folio))) +return 0 ; + +if (folio_test_anon (folio)) { +/* One reference per page from the swapcache. */ + ref_count +=folio_test_swapcache (folio) << order; + }else if (!((unsigned long )folio->mapping &PAGE_MAPPING_FLAGS )) { +/* One reference per page from the pagecache. */ + ref_count += !!folio->mapping << order; +/* One reference from PG_private. */ + ref_count +=folio_test_private (folio); + } + +/* One reference per page table mapping. */ +return ref_count +folio_mapcount (folio); +} +#ifndef HAVE_ARCH_MAKE_FOLIO_ACCESSIBLE static inline int arch_make_folio_accessible (struct folio *folio) {
mm/migrate.c +4 -18
@@ -445,20 +445,6 @@ void pmd_migration_entry_wait(struct mm_struct *mm, pmd_t *pmd) }#endif -static int folio_expected_refs (struct address_space *mapping, -struct folio *folio) -{ -int refs =1 ; -if (!mapping) -return refs; - - refs +=folio_nr_pages (folio); -if (folio_test_private (folio)) - refs++; - -return refs; -} -/* * Replace the folio in the mapping. *
@@ -601,7 +587,7 @@ static int __folio_migrate_mapping(struct address_space *mapping,int folio_migrate_mapping (struct address_space *mapping,struct folio *newfolio,struct folio *folio,int extra_count) { -int expected_count =folio_expected_refs (mapping, folio) + extra_count; +int expected_count =folio_expected_ref_count (folio) + extra_count +1 ;if (folio_ref_count (folio) != expected_count)return -EAGAIN ;
@@ -618,7 +604,7 @@ int migrate_huge_page_move_mapping(struct address_space *mapping, struct folio *dst, struct folio *src) { XA_STATE(xas, &mapping->i_pages, folio_index(src)); -int rc, expected_count =folio_expected_refs (mapping, src); +int rc, expected_count =folio_expected_ref_count (src) +1 ;if (folio_ref_count (src) != expected_count)return -EAGAIN ;
@@ -749,7 +735,7 @@ static int __migrate_folio(struct address_space *mapping, struct folio *dst, struct folio *src, void *src_private, enum migrate_mode mode) { - int rc, expected_count = folio_expected_refs(mapping, src); + int rc, expected_count = folio_expected_ref_count(src) + 1;/* Check whether src does not have extra refs before we do more work */ if (folio_ref_count (src) != expected_count)
@@ -837,7 +823,7 @@ static int __buffer_migrate_folio(struct address_space *mapping,return migrate_folio (mapping, dst, src, mode);/* Check whether page does not have extra refs before we do more work */ - expected_count =folio_expected_refs (mapping, src); + expected_count =folio_expected_ref_count (src) +1 ;if (folio_ref_count (src) != expected_count)return -EAGAIN ;
Signature · 1 line
2.43.0
PATCH 2/2 mm/khugepaged: fix race with folio split/free using temporary reference
hpage_collapse_scan_file() calls is_refcount_suitable(), which in turn calls folio_mapcount(). folio_mapcount() checks folio_test_large() before proceeding to folio_large_mapcount(), but there is a race window where the folio may get split/freed between these checks, triggering:
VM_WARN_ON_FOLIO(!folio_test_large(folio), folio)
Take a temporary reference to the folio in hpage_collapse_scan_file(). This stabilizes the folio during refcount check and prevents incorrect large folio detection due to concurrent split/free. Use helper folio_expected_ref_count() + 1 to compare with folio_ref_count() instead of using is_refcount_suitable().
--- mm/khugepaged.c | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-)
mm/khugepaged.c +17 -1
@@ -2295,6 +2295,17 @@ static int hpage_collapse_scan_file(struct mm_struct *mm, unsigned long addr,continue ; } +if (!folio_try_get (folio)) { +xas_reset (&xas); +continue ; + } + +if (unlikely (folio !=xas_reload (&xas))) { +folio_put (folio); +xas_reset (&xas); +continue ; + } +if (folio_order (folio) ==HPAGE_PMD_ORDER && folio->index == start) {/* Maybe PMD-mapped */
@@ -2305,23 +2316,27 @@ static int hpage_collapse_scan_file(struct mm_struct *mm, unsigned long addr,* it's safe to skip LRU and refcount checks before * returning. */ +folio_put (folio);break ; } node =folio_nid (folio);if (hpage_collapse_scan_abort (node, cc)) { result =SCAN_SCAN_ABORT ; +folio_put (folio);break ; } cc->node_load[node]++;if (!folio_test_lru (folio)) { result =SCAN_PAGE_LRU ; +folio_put (folio);break ; } -if (!is_refcount_suitable (folio)) { +if (folio_expected_ref_count (folio) +1 !=folio_ref_count (folio)) { result =SCAN_PAGE_COUNT ; +folio_put (folio);break ; }
@@ -2333,6 +2348,7 @@ static int hpage_collapse_scan_file(struct mm_struct *mm, unsigned long addr,*/ present +=folio_nr_pages (folio); +folio_put (folio);if (need_resched ()) {xas_pause (&xas);
Signature · 1 line
2.43.0
Greg KH
maintainerRe: [PATCH 6.15.y 1/2] mm: add folio_expected_ref_count() for reference count calculation
In reply to Shivank GargQuoted 62 lines · Shivank Garg
Patch series " JFS: Implement
migrate_folioforjfs_metapage_aops" v5.This patchset addresses a warning that occurs during memory compaction due to JFS's missing
migrate_foliooperation. The warning was introduced by commit7ee3647243e5("migrate: Remove call to ->writepage") which added explicit warnings when filesystem don't implementmigrate_folio.The syzbot reported following [1]:
jfs_metapage_aopsdoes not implementmigrate_folioWARNING: CPU: 1 PID: 5861 at mm/migrate.c:955 fallback_migrate_folio mm/migrate.c:953 [inline] WARNING: CPU: 1 PID: 5861 at mm/migrate.c:955 move_to_new_folio+0x70e/0x840 mm/migrate.c:1007 Modules linked in: CPU: 1 UID: 0 PID: 5861 Comm: syz-executor280 Not tainted 6.15.0-rc1-next-20250411-syzkaller #0 PREEMPT(full) Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 02/12/2025 RIP: 0010:fallback_migrate_folio mm/migrate.c:953 [inline] RIP: 0010:move_to_new_folio+0x70e/0x840 mm/migrate.c:1007To fix this issue, this series implement
metapage_migrate_folio()for JFS which handles both single and multiple metapages per page configurations.While most filesystems leverage existing migration implementations like
filemap_migrate_folio(),buffer_migrate_folio_norefs()orbuffer_migrate_folio()(which internally usedfolio_expected_refs()), JFS's metapage architecture requires special handling of its private data during migration. To support this, this series introduce thefolio_expected_ref_count(), which calculates external references to a folio from page/swap cache, private data, and page table mappings.This standardized implementation replaces the previous ad-hoc
folio_expected_refs()function and enables JFS to accurately determine whether a folio has unexpected references before attempting migration.Implement
folio_expected_ref_count()to calculate expected folio reference counts from:
- Page/swap cache (1 per page)
- Private data (1)
- Page table mappings (1 per map)While originally needed for page migration operations, this improved implementation standardizes reference counting by consolidating all refcount contributors into a single, reusable function that can benefit any subsystem needing to detect unexpected references to folios.
The
folio_expected_ref_count()returns the sum of these external references without including any reference the caller itself might hold. Callers comparing against the actualfolio_ref_count()must account for their own references separately.Signed-off-by David Hildenbrand <[email protected]>Signed-off-by Shivank Garg <[email protected]>Suggested-by Matthew Wilcox <[email protected]>Co-developed-by David Hildenbrand <[email protected]>Cc Alistair Popple <[email protected]>Cc Dave Kleikamp <[email protected]>Cc Donet Tom <[email protected]>Cc Jane Chu <[email protected]>Cc Kefeng Wang <[email protected]>Cc Zi Yan <[email protected]>Signed-off-by Andrew Morton <[email protected]>
Quoted 5 lines
--- include/linux/mm.h | 55 ++++++++++++++++++++++++++++++++++++++++++++++ mm/migrate.c | 22 ++++--------------- 2 files changed, 59 insertions(+), 18 deletions(-)
<formletter>
This is not the correct way to submit patches for inclusion in the stable kernel tree. Please read:
https://www.kernel.org/doc/html/latest/process/stable-kernel-rules.html
for how to do this properly.
</formletter>