All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v3] mm/memory-failure: fix races when handling free HugeTLB folios
@ 2026-12-05 12:12 Muchun Song
  2026-12-05 23:54 ` + mm-memory-failure-fix-races-when-handling-free-hugetlb-folios.patch added to mm-new branch Andrew Morton
  0 siblings, 1 reply; 2+ messages in thread
From: Muchun Song @ 2026-12-05 12:12 UTC (permalink / raw)
  To: Oscar Salvador, Andrew Morton, Miaohe Lin, Mike Rapoport
  Cc: Muchun Song, David Hildenbrand, Naoya Horiguchi, Joao Martins,
	linux-mm, stable, Muchun Song

try_memory_failure_hugetlb() does not hold a folio reference when
get_huge_page_for_hwpoison() returns MF_HUGETLB_FREED, but it still
locks the folio and may clear its HugeTLB HWPoison state after dropping
hugetlb_lock.

A concurrent pool shrink can dissolve and free the folio during this
window. Locking it can race with the page allocator's free checks and
cause a bad-page report and a leak, or modify a page that has already
been freed or reused.

CPU 0                           CPU 1
try_memory_failure_hugetlb()
  get_huge_page_for_hwpoison()
    spin_unlock_irq(&hugetlb_lock)
    return MF_HUGETLB_FREED
  folio = page_folio(p)
                                remove_hugetlb_folio()
                                __update_and_free_hugetlb_folio()
                                  folio_clear_hugetlb_hwpoison()
  folio_lock(folio)
                                  free_frozen_pages()
                                    __free_pages_prepare()
                                      free_page_is_bad()
                                        /* PG_locked is set. */
                                        bad_page()

Cleanup can also interpret reused tail-page metadata as raw_hwp_page
entries. Resolving page_folio() again does not ensure that it identifies
the folio originally marked HWPoison.

CPU 0                           CPU 1
try_memory_failure_hugetlb()
  get_huge_page_for_hwpoison()
    spin_unlock_irq(&hugetlb_lock)
    return MF_HUGETLB_FREED
  folio = page_folio(p)
                                remove_hugetlb_folio()
                                __update_and_free_hugetlb_folio()
                                  hugetlb_vmemmap_restore_folio()
                                  __folio_clear_hugetlb()
                                  folio_clear_hugetlb_hwpoison()
                                  free_frozen_pages()
                                /* Tail private is reused. */
  folio_lock(folio)
  if (hwpoison_filter(p))
    folio_clear_hugetlb_hwpoison()
      folio_free_raw_hwp(..., true)
        __folio_free_raw_hwp()
          /* Reads and clears reused private. */
          llist_del_all(raw_hwp_list_head(folio))

Batched vmemmap restoration exposes another race in the same window:
HPG_vmemmap_optimized is cleared before the final TLB flush. Cleanup can
then write HWPoison to a raw tail page through a stale shared mapping,
corrupting metadata shared by other HugeTLB folios.

CPU 0                           CPU 1
try_memory_failure_hugetlb()
  get_huge_page_for_hwpoison()
    hugetlb_update_hwpoison()
    spin_unlock_irq(&hugetlb_lock)
    return MF_HUGETLB_FREED
  folio = page_folio(p)
                                remove_hugetlb_folio()
                                hugetlb_vmemmap_restore_folios()
                                  __hugetlb_vmemmap_restore_folio()
                                    /* Restore without a TLB flush. */
                                    folio_clear_hugetlb_vmemmap_optimized()
  folio_lock(folio)
  if (hwpoison_filter(p))
    folio_clear_hugetlb_hwpoison()
      folio_test_hugetlb_vmemmap_optimized() -> false
      folio_free_raw_hwp(..., true)
        __folio_free_raw_hwp()
          /* May use a stale shared TLB mapping. */
          SetPageHWPoison(raw tail)
                                  flush_tlb_all()

Perform free-folio filtering and HWPoison cleanup in the same
hugetlb_lock critical section that identifies and marks the folio.
This prevents concurrent dissolution and vmemmap restoration during
cleanup.

Only lock in-use folios, whose lifetime is pinned by a reference.

Fixes: 8c6c2ecb4466 ("HWPOSION, hugetlb: recover from free hugepage error when !MF_COUNT_INCREASED")
Fixes: 161df60e9e89 ("mm, hwpoison, hugetlb: support saving mechanism of raw error pages")
Fixes: c24f188b2289 ("hugetlb: batch TLB flushes when restoring vmemmap")
Cc: stable@vger.kernel.org
Signed-off-by: Muchun Song <songmuchun@bytedance.com>
---
v3:
- Handle free-folio filtering and HWPoison cleanup under hugetlb_lock,
  and avoid folio_lock() on unreferenced folios
- Drop the vmemmap_remap_rwsem changes from v2, as hugetlb_lock now
  keeps remove_hugetlb_folio() out of the cleanup window

v2: https://lore.kernel.org/20261205013246.18791-1-songmuchun@bytedance.com/
- Take vmemmap_remap_rwsem for write in try_memory_failure_hugetlb()
  instead of folio_clear_hugetlb_hwpoison() to avoid sleeping in atomic
  context and recursive locking on the free path (reported by sashiko-bot)
- Skip taking vmemmap_remap_rwsem in hugetlb_vmemmap_restore_folios()
  when the folio list is empty (suggested by sashiko-bot)

v1: https://lore.kernel.org/20261204122056.85384-1-songmuchun@bytedance.com/
---
 mm/memory-failure.c | 46 +++++++++++++++++++++++++++------------------
 1 file changed, 28 insertions(+), 18 deletions(-)

diff --git a/mm/memory-failure.c b/mm/memory-failure.c
index 227148d116dd..bc38993bd0d6 100644
--- a/mm/memory-failure.c
+++ b/mm/memory-failure.c
@@ -1958,6 +1958,7 @@ static unsigned long __folio_free_raw_hwp(struct folio *folio, bool move_flag)
 #define	MF_HUGETLB_FOLIO_PRE_POISONED	3	/* folio already poisoned */
 #define	MF_HUGETLB_PAGE_PRE_POISONED	4	/* exact page already poisoned */
 #define	MF_HUGETLB_RETRY		5	/* hugepage is busy, retry */
+#define	MF_HUGETLB_FILTERED		6	/* rejected by hwpoison filter */
 /*
  * Set hugetlb folio as hwpoisoned, update folio private raw hwpoison list
  * to keep track of the poisoned pages.
@@ -2069,6 +2070,17 @@ static int get_huge_page_for_hwpoison(unsigned long pfn, int flags,
 		goto out_unlock;
 	}
 
+	/*
+	 * Free folios have no reference pinning them. Filter and clear their
+	 * HWPoison state under hugetlb_lock to prevent concurrent dissolution
+	 * and reuse.
+	 */
+	if (ret == MF_HUGETLB_FREED && hwpoison_filter(page)) {
+		folio_clear_hugetlb_hwpoison(folio);
+		ret = MF_HUGETLB_FILTERED;
+		goto out_unlock;
+	}
+
 	/*
 	 * Clearing hugetlb_migratable for hwpoisoned hugepages to prevent them
 	 * from being migrated by memory hotremove.
@@ -2129,8 +2141,22 @@ static int try_memory_failure_hugetlb(unsigned long pfn, int flags)
 		else
 			action_result(pfn, MF_MSG_HUGE, MF_FAILED);
 		return rv;
+	case MF_HUGETLB_FILTERED:
+		return -EOPNOTSUPP;
+	case MF_HUGETLB_FREED:
+		/*
+		 * PageHWPoison prevents allocation or demotion from the HugeTLB
+		 * pool. The folio cannot be locked without a reference.
+		 */
+		if (__page_handle_poison(p) > 0) {
+			page_ref_inc(p);
+			res = MF_RECOVERED;
+		} else {
+			res = MF_FAILED;
+		}
+		return action_result(pfn, MF_MSG_FREE_HUGE, res);
 	default:
-		WARN_ON((res != MF_HUGETLB_FREED) && (res != MF_HUGETLB_IN_USED));
+		WARN_ON(res != MF_HUGETLB_IN_USED);
 		break;
 	}
 
@@ -2142,26 +2168,10 @@ static int try_memory_failure_hugetlb(unsigned long pfn, int flags)
 		if (migratable_cleared)
 			folio_set_hugetlb_migratable(folio);
 		folio_unlock(folio);
-		if (res == MF_HUGETLB_IN_USED)
-			folio_put(folio);
+		folio_put(folio);
 		return -EOPNOTSUPP;
 	}
 
-	/*
-	 * Handling free hugepage.  The possible race with hugepage allocation
-	 * or demotion can be prevented by PageHWPoison flag.
-	 */
-	if (res == MF_HUGETLB_FREED) {
-		folio_unlock(folio);
-		if (__page_handle_poison(p) > 0) {
-			page_ref_inc(p);
-			res = MF_RECOVERED;
-		} else {
-			res = MF_FAILED;
-		}
-		return action_result(pfn, MF_MSG_FREE_HUGE, res);
-	}
-
 	page_flags = folio->flags.f;
 
 	if (!hwpoison_user_mappings(folio, p, pfn, flags)) {

base-commit: d0d65ff2e433e741c23715e8359c4a248d432179
-- 
2.54.0


^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-12-05 23:54 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-05 12:12 [PATCH v3] mm/memory-failure: fix races when handling free HugeTLB folios Muchun Song
2026-12-05 23:54 ` + mm-memory-failure-fix-races-when-handling-free-hugetlb-folios.patch added to mm-new branch Andrew Morton

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.