mm/vmscan: fix hwpoisoned large folio handling in shrink_folio_list
Subsystems (4)
PATCH v3 1/1 mm/vmscan: fix hwpoisoned large folio handling in shrink_folio_list
In shrink_folio_list(), the hwpoisoned folio may be large folio, which can't be handled by unmap_poisoned_folio(). For THP, try_to_unmap_one() must be passed with TTU_SPLIT_HUGE_PMD to split huge PMD first and then retry. Without TTU_SPLIT_HUGE_PMD, we will trigger null-ptr deref of pvmw.pte. Even we passed TTU_SPLIT_HUGE_PMD, we will trigger a WARN_ON_ONCE due to the page isn't in swapcache.
Since UCE is rare in real world, and race with reclaimation is more rare, just skipping the hwpoisoned large folio is enough. memory_failure() will handle it if the UCE is triggered again.
---
v3:
* collect Acked-by and Reviewed-by
* update commit message and commemts, sugguested by Oscar Salvador.
mm/memory-failure.c | 4 ++++ mm/vmscan.c | 8 ++++++++ 2 files changed, 12 insertions(+)
mm/memory-failure.c +4 -0
@@ -1561,6 +1561,10 @@ static int get_hwpoison_page(struct page *p, unsigned long flags)return ret; } +/* +* The caller must guarantee the folio isn't large folio. try_to_unmap() +* can't handle it. +*/ int unmap_poisoned_folio (struct folio *folio,unsigned long pfn,bool must_kill) {enum ttu_flags ttu =TTU_IGNORE_MLOCK |TTU_SYNC |TTU_HWPOISON ;
mm/vmscan.c +8 -0
@@ -1138,6 +1138,14 @@ static unsigned int shrink_folio_list(struct list_head *folio_list, goto keep;if (folio_contain_hwpoisoned_page (folio)) { +/* +* unmap_poisoned_folio() can't handle large +* folio, just skip it. memory_failure() will +* handle it if the UCE is triggered again. +*/ +if (folio_test_large (folio)) + goto keep_locked; +unmap_poisoned_folio (folio,folio_pfn (folio), false);folio_unlock (folio);folio_put (folio);
Signature · 1 line
2.43.0
Quoted 17 lines · Jinjiang Tu
In
shrink_folio_list(), the hwpoisoned folio may be large folio, which can't be handled byunmap_poisoned_folio(). For THP,try_to_unmap_one()must be passed withTTU_SPLIT_HUGE_PMDto split huge PMD first and then retry. WithoutTTU_SPLIT_HUGE_PMD, we will trigger null-ptr deref of pvmw.pte. Even we passedTTU_SPLIT_HUGE_PMD, we will trigger aWARN_ON_ONCEdue to the page isn't in swapcache.Since UCE is rare in real world, and race with reclaimation is more rare, just skipping the hwpoisoned large folio is enough.
memory_failure()will handle it if the UCE is triggered again.Fixes 1b0449544c64 ("mm/vmscan: don't try to reclaim hwpoison folio")Reported-by [email protected]Reviewed-by Miaohe Lin <[email protected]>Signed-off-by Jinjiang Tu <[email protected]>
Quoted 9 lines
---
v3:
* collect Acked-by and Reviewed-by
* update commit message and commemts, sugguested by Oscar Salvador.mm/memory-failure.c | 4 ++++ mm/vmscan.c | 8 ++++++++ 2 files changed, 12 insertions(+)
Best Regards,
Yan, Zi
Quoted 11 lines · Jinjiang Tu
In
shrink_folio_list(), the hwpoisoned folio may be large folio, which can't be handled byunmap_poisoned_folio(). For THP,try_to_unmap_one()must be passed withTTU_SPLIT_HUGE_PMDto split huge PMD first and then retry. WithoutTTU_SPLIT_HUGE_PMD, we will trigger null-ptr deref of pvmw.pte. Even we passedTTU_SPLIT_HUGE_PMD, we will trigger aWARN_ON_ONCEdue to the page isn't in swapcache.Since UCE is rare in real world, and race with reclaimation is more rare, just skipping the hwpoisoned large folio is enough.
memory_failure()will handle it if the UCE is triggered again.
Quoted 6 lines
Fixes 1b0449544c64 ("mm/vmscan: don't try to reclaim hwpoison folio")Reported-by [email protected]Reviewed-by Miaohe Lin <[email protected]>Signed-off-by Jinjiang Tu <[email protected]>
Signature · 2 lines
Oscar Salvador SUSE Labs
Quoted 31 lines · Jinjiang Tu
In
shrink_folio_list(), the hwpoisoned folio may be large folio, which can't be handled byunmap_poisoned_folio(). For THP,try_to_unmap_one()must be passed withTTU_SPLIT_HUGE_PMDto split huge PMD first and then retry. WithoutTTU_SPLIT_HUGE_PMD, we will trigger null-ptr deref of pvmw.pte. Even we passedTTU_SPLIT_HUGE_PMD, we will trigger aWARN_ON_ONCEdue to the page isn't in swapcache.Since UCE is rare in real world, and race with reclaimation is more rare, just skipping the hwpoisoned large folio is enough.
memory_failure()will handle it if the UCE is triggered again.Fixes 1b0449544c64 ("mm/vmscan: don't try to reclaim hwpoison folio")Reported-by [email protected]Reviewed-by Miaohe Lin <[email protected]>Signed-off-by Jinjiang Tu <[email protected]>---
v3:
* collect Acked-by and Reviewed-by
* update commit message and commemts, sugguested by Oscar Salvador.mm/memory-failure.c | 4 ++++ mm/vmscan.c | 8 ++++++++ 2 files changed, 12 insertions(+)mm/memory-failure.c +0
-0
mm/memory-failure.c static int get_hwpoison_page(struct page *p, unsigned long flags) · line 1566
Quoted 6 lines
mm/memory-failure.c +3
-0@@ -1561,6 +1561,10 @@ static int get_hwpoison_page(struct page *p, unsigned long flags)return ret; } +/* +* The caller must guarantee the folio isn't large folio. try_to_unmap() +* can't handle it.
Not completely accurate: it may be a hugetlb folios, that is also large, but supported.
"isn't a large folio, except hugetlb."
Signature · 3 lines
Cheers, David / dhildenb
PATCH v4 1/1 mm/vmscan: fix hwpoisoned large folio handling in shrink_folio_list
In shrink_folio_list(), the hwpoisoned folio may be large folio, which can't be handled by unmap_poisoned_folio(). For THP, try_to_unmap_one() must be passed with TTU_SPLIT_HUGE_PMD to split huge PMD first and then retry. Without TTU_SPLIT_HUGE_PMD, we will trigger null-ptr deref of pvmw.pte. Even we passed TTU_SPLIT_HUGE_PMD, we will trigger a WARN_ON_ONCE due to the page isn't in swapcache.
Since UCE is rare in real world, and race with reclaimation is more rare, just skipping the hwpoisoned large folio is enough. memory_failure() will handle it if the UCE is triggered again.
---
mm/memory-failure.c | 4 ++++
mm/vmscan.c | 8 ++++++++
2 files changed, 12 insertions(+)
mm/memory-failure.c +0 -0
@@ -1561,6 +1561,10 @@ static int get_hwpoison_page(struct page *p, unsigned long flags)
return ret;
}
+/*
+ * The caller must guarantee the folio isn't large folio, except hugetlb.
+ * try_to_unmap() can't handle it.
+ */
int unmap_poisoned_folio(struct folio *folio, unsigned long pfn, bool must_kill) {
enum ttu_flags ttu = TTU_IGNORE_MLOCK | TTU_SYNC | TTU_HWPOISON;
mm/vmscan.c +0 -0
@@ -1138,6 +1138,14 @@ static unsigned int shrink_folio_list(struct list_head *folio_list,
goto keep;
if (folio_contain_hwpoisoned_page(folio)) {
+ /*
+ * unmap_poisoned_folio() can't handle large + * folio, just skip it. memory_failure() will + * handle it if the UCE is triggered again. + */
+ if (folio_test_large(folio))
+ goto keep_locked;
+
unmap_poisoned_folio(folio, folio_pfn(folio), false); folio_unlock(folio);
folio_put(folio);
Signature · 1 line
2.43.0
Andrew Morton
maintainer Acked-by David Hildenbrand Reviewed-by Miaohe Lin Acked-by Zi Yan Reviewed-by Oscar Salvador on v4 1/1Quoted 10 lines · On Fri, 11 Jul 2025 16:55:45 +0800 Jinjiang Tu <[email protected]> wrote:
In
shrink_folio_list(), the hwpoisoned folio may be large folio, which can't be handled byunmap_poisoned_folio(). For THP,try_to_unmap_one()must be passed withTTU_SPLIT_HUGE_PMDto split huge PMD first and then retry. WithoutTTU_SPLIT_HUGE_PMD, we will trigger null-ptr deref of pvmw.pte. Even we passedTTU_SPLIT_HUGE_PMD, we will trigger aWARN_ON_ONCEdue to the page isn't in swapcache.Since UCE is rare in real world, and race with reclaimation is more rare, just skipping the hwpoisoned large folio is enough.
memory_failure()will handle it if the UCE is triggered again.
Your email client made a mess of the whitespace. I fixed that up and turned this into a v2->v4 delta so I/we can see what happened:
mm/memory-failure.c renamed from mm/memory-failure.c~mm-vmscan-fix-hwpoisoned-large-folio-handling-in-shrink_folio_list-v4 +4 -0
@@ -1561,6 +1561,10 @@ static int get_hwpoison_page(struct pagereturn ret; } +/* +* The caller must guarantee the folio isn't large folio, except hugetlb. +* try_to_unmap() can't handle it. +*/ int unmap_poisoned_folio (struct folio *folio,unsigned long pfn,bool must_kill) {enum ttu_flags ttu =TTU_IGNORE_MLOCK |TTU_SYNC |TTU_HWPOISON ;
_
Also, the v2 patch's changelog (probably as amended by me) had a nice description of the race, which is lost in this v4 patch. I restored it, so the final changelog is as below. Please check.
From: Jinjiang Tu <[email protected]>
Subject: mm/vmscan: fix hwpoisoned large folio handling in shrink_folio_list
Date: Fri, 27 Jun 2025 20:57:46 +0800
In shrink_folio_list(), the hwpoisoned folio may be large folio, which can't be handled by unmap_poisoned_folio(). For THP, try_to_unmap_one() must be passed with TTU_SPLIT_HUGE_PMD to split huge PMD first and then retry. Without TTU_SPLIT_HUGE_PMD, we will trigger null-ptr deref of pvmw.pte. Even we passed TTU_SPLIT_HUGE_PMD, we will trigger a WARN_ON_ONCE due to the page isn't in swapcache.
Since UCE is rare in real world, and race with reclaimation is more rare, just skipping the hwpoisoned large folio is enough. memory_failure() will handle it if the UCE is triggered again.
This happens when memory reclaim for large folio races with memory_failure(), and will lead to kernel panic. The race is as follows:
cpu0 cpu1
shrink_folio_list memory_failure
TestSetPageHWPoison
unmap_poisoned_folio
--> trigger BUG_ON due to
unmap_poisoned_folio couldn't
handle large folio
---
mm/vmscan.c | 8 ++++++++ 1 file changed, 8 insertions(+)
mm/vmscan.c renamed from mm/vmscan.c~mm-vmscan-fix-hwpoisoned-large-folio-handling-in-shrink_folio_list +8 -0
@@ -1138,6 +1138,14 @@ retry: goto keep;if (folio_contain_hwpoisoned_page (folio)) { +/* +* unmap_poisoned_folio() can't handle large +* folio, just skip it. memory_failure() will +* handle it if the UCE is triggered again. +*/ +if (folio_test_large (folio)) + goto keep_locked; +unmap_poisoned_folio (folio,folio_pfn (folio), false);folio_unlock (folio);folio_put (folio);
_
Quoted 42 lines · David Hildenbrand
Quoted 34 lines · Jinjiang Tu
In
shrink_folio_list(), the hwpoisoned folio may be large folio, which can't be handled byunmap_poisoned_folio(). For THP,try_to_unmap_one()must be passed withTTU_SPLIT_HUGE_PMDto split huge PMD first and then retry. WithoutTTU_SPLIT_HUGE_PMD, we will trigger null-ptr deref of pvmw.pte. Even we passedTTU_SPLIT_HUGE_PMD, we will trigger aWARN_ON_ONCEdue to the page isn't in swapcache.Since UCE is rare in real world, and race with reclaimation is more rare, just skipping the hwpoisoned large folio is enough.
memory_failure()will handle it if the UCE is triggered again.Fixes 1b0449544c64 ("mm/vmscan: don't try to reclaim hwpoison folio")Reported-by [email protected]Reviewed-by Miaohe Lin <[email protected]>Signed-off-by Jinjiang Tu <[email protected]>---
v3:
* collect Acked-by and Reviewed-by
* update commit message and commemts, sugguested by Oscar Salvador.
mm/memory-failure.c| 4 ++++
mm/vmscan.c | 8 ++++++++2 files changed, 12 insertions(+)mm/memory-failure.c +0
-0@@ -1561,6 +1561,10 @@ static int get_hwpoison_page(struct page *p, unsigned long flags) return ret; }Quoted 4 lines
+/*
+ * The caller must guarantee the folio isn't large folio.try_to_unmap()
+ * can't handle it.Not completely accurate: it may be a hugetlb folios, that is also large, but supported.
Quoted 2 lines
"isn't a large folio, except hugetlb."
Thanks, updated it.