Skip to contentSkip to conversation

Only free healthy pages in high-order HWPoison folio

PATCH Superseded Jiaqi Yan 4 participants 17 messages Dec 19, 2025 – Jan 10

Subsystems (4)

v2 of 7 Superseded Latest is v7 →

lore
Share & tools
lore

Jiaqi Yan

author
#0 lore raw

v2 cover Only free healthy pages in high-order HWPoison folio

At the end of dissolve_free_hugetlb_folio that a free HugeTLB folio becomes non-HugeTLB, it is released to buddy allocator as a high-order folio, e.g. a folio that contains 262144 pages if the folio was a 1G HugeTLB hugepage.

This is problematic if the HugeTLB hugepage contained HWPoison subpages. In that case, since buddy allocator does not check HWPoison for non-zero-order folio, the raw HWPoison page can be given out with its buddy page and be re-used by either kernel or userspace.

Memory failure recovery (MFR) in kernel does attempt to take raw HWPoison page off buddy allocator after dissolve_free_hugetlb_folio. However, there is always a time window between dissolve_free_hugetlb_folio frees a HWPoison high-order folio to buddy allocator and MFR takes HWPoison raw page off buddy allocator.

One obvious way to avoid this problem is to add page sanity checks in page allocate or free path. However, it is against the past efforts to reduce sanity check overhead [1,2,3].

Introduce free_has_hwpoison_pages to only free the healthy pages and excludes the HWPoison ones in the high-order folio. The idea is to iterate through the sub-pages of the folio to identify contiguous ranges of healthy pages. Instead of freeing pages one by one, decompose healthy ranges into the largest possible blocks. Each block meets the requirements to be freed to buddy allocator by calling __free_frozen_pages directly.

free_has_hwpoison_pages has linear time complexity O(N) wrt the number of pages in the folio. While the power-of-two decomposition ensures that the number of calls to the buddy allocator is logarithmic for each contiguous healthy range, the mandatory linear scan of pages to identify PageHWPoison defines the overall time complexity.

I tested with some test-only code [4] and hugetlb-mfr [5], by checking the status of pcplist and freelist immediately after dissolve_free_hugetlb_folio a free hugetlb page that contains 3 HWPoison raw pages:

* HWPoison pages are excluded by free_has_hwpoison_pages.

* Some healthy pages can be in zone->per_cpu_pageset (pcplist) because pcp_count is not high enough.

* Many healthy pages are already in some order's
zone->free_area[order].free_list (freelist).

* In rare cases, some healthy pages are in neither pcplist nor freelist. My best guest is they are allocated before the test checks.

[1] https://lore.kernel.org/linux-mm/[email protected]/
[2] https://lore.kernel.org/linux-mm/[email protected]/
[3] https://lore.kernel.org/all/[email protected]
[4] https://drive.google.com/file/d/1CzJn1Cc4wCCm183Y77h244fyZIkTLzCt/view?usp=sharing
[5] https://lore.kernel.org/linux-mm/[email protected]

Jiaqi Yan (3):
mm/memory-failure: set has_hwpoisoned flags on HugeTLB folio mm/page_alloc: only free healthy pages in high-order HWPoison folio mm/memory-failure: simplify __page_handle_poison

 include/linux/page-flags.h |   2 +-
 mm/memory-failure.c        |  32 +++---------
 mm/page_alloc.c            | 101 +++++++++++++++++++++++++++++++++++++
 3 files changed, 108 insertions(+), 27 deletions(-)
Signature · 1 line
2.52.0.322.g1dd061c0dc-goog
author
#1 lore raw

PATCH v2 1/3 mm/memory-failure: set has_hwpoisoned flags on HugeTLB folio

When a free HWPoison HugeTLB folio is dissolved, it becomes non-HugeTLB and is released to buddy allocator as a high-order folio.

Set has_hwpoisoned flags on the high-order folio so that buddy allocator can tell that it contains certain HWPoison page(s). This is a prepare change for buddy allocator to handle only the high-order HWPoison folio differently.

This cannot be done with hwpoison flag because users cannot tell from the case that the page with hwpoison is hardware corrupted.

---
 include/linux/page-flags.h | 2 +-
 mm/memory-failure.c        | 1 +
 2 files changed, 2 insertions(+), 1 deletion(-)
include/linux/page-flags.h +1 -1
index f7a0e4af0c734..d13835e265952 100644
@@ -904,7 +904,7 @@ static inline int PageTransCompound(const struct page *page)
 TESTPAGEFLAG_FALSE(TransCompound, transcompound)
 #endif
 
-#if defined(CONFIG_MEMORY_FAILURE) && defined(CONFIG_TRANSPARENT_HUGEPAGE)
+#if defined(CONFIG_MEMORY_FAILURE) && (defined(CONFIG_TRANSPARENT_HUGEPAGE) || defined(CONFIG_HUGETLB_PAGE))
 /*
  * PageHasHWPoisoned indicates that at least one subpage is hwpoisoned in the
  * compound page.
mm/memory-failure.c +1 -0
index fbc5a01260c89..d204de6c9792a 100644
@@ -1952,6 +1952,7 @@ void folio_clear_hugetlb_hwpoison(struct folio *folio)
 	if (folio_test_hugetlb_vmemmap_optimized(folio))
 		return;
 	folio_clear_hwpoison(folio);
+	folio_set_has_hwpoisoned(folio);
 	folio_free_raw_hwp(folio, true);
 }
 
Signature · 1 line
2.52.0.322.g1dd061c0dc-goog
author
#2 lore raw

PATCH v2 2/3 mm/page_alloc: only free healthy pages in high-order HWPoison folio

In reply to Jiaqi Yan

At the end of dissolve_free_hugetlb_folio that a free HugeTLB folio becomes non-HugeTLB, it is released to buddy allocator as a high-order folio, e.g. a folio that contains 262144 pages if the folio was a 1G HugeTLB hugepage.

This is problematic if the HugeTLB hugepage contained HWPoison subpages. In that case, since buddy allocator does not check HWPoison for non-zero-order folio, the raw HWPoison page can be given out with its buddy page and be re-used by either kernel or userspace.

Memory failure recovery (MFR) in kernel does attempt to take raw HWPoison page off buddy allocator after dissolve_free_hugetlb_folio. However, there is always a time window between dissolve_free_hugetlb_folio frees a HWPoison high-order folio to buddy allocator and MFR takes HWPoison raw page off buddy allocator.

One obvious way to avoid this problem is to add page sanity checks in page allocate or free path. However, it is against the past efforts to reduce sanity check overhead [1,2,3].

Introduce free_has_hwpoison_pages to only free the healthy pages and excludes the HWPoison ones in the high-order folio. The idea is to iterate through the sub-pages of the folio to identify contiguous ranges of healthy pages. Instead of freeing pages one by one, decompose healthy ranges into the largest possible blocks. Each block meets the requirements to be freed to buddy allocator (__free_frozen_pages).

free_has_hwpoison_pages has linear time complexity O(N) wrt the number of pages in the folio. While the power-of-two decomposition ensures that the number of calls to the buddy allocator is logarithmic for each contiguous healthy range, the mandatory linear scan of pages to identify PageHWPoison defines the overall time complexity.

[1] https://lore.kernel.org/linux-mm/[email protected]/
[2] https://lore.kernel.org/linux-mm/[email protected]/
[3] https://lore.kernel.org/all/[email protected]

---
 mm/page_alloc.c | 101 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 101 insertions(+)
mm/page_alloc.c +101 -0
index 822e05f1a9646..20c8862ce594e 100644
@@ -2976,8 +2976,109 @@ static void __free_frozen_pages(struct page *page, unsigned int order,
 	}
 }
 
+static void prepare_compound_page_to_free(struct page *new_head,
+					  unsigned int order,
+					  unsigned long flags)
+{
+	new_head->flags.f = flags & (~PAGE_FLAGS_CHECK_AT_FREE);
+	new_head->mapping = NULL;
+	new_head->private = 0;
+
+	clear_compound_head(new_head);
+	if (order)
+		prep_compound_page(new_head, order);
+}
+
+/*
+ * Given a range of pages physically contiguous physical, efficiently
+ * free them in blocks that meet __free_frozen_pages's requirements.
+ */
+static void free_contiguous_pages(struct page *curr, struct page *next,
+				  unsigned long flags)
+{
+	unsigned int order;
+	unsigned int align_order;
+	unsigned int size_order;
+	unsigned long pfn;
+	unsigned long end_pfn = page_to_pfn(next);
+	unsigned long remaining;
+
+	/*
+	 * This decomposition algorithm at every iteration chooses the
+	 * order to be the minimum of two constraints:
+	 * - Alignment: the largest power-of-two that divides current pfn.
+	 * - Size: the largest power-of-two that fits in the
+	 *   current remaining number of pages.
+	 */
+	while (curr < next) {
+		pfn = page_to_pfn(curr);
+		remaining = end_pfn - pfn;
+
+		align_order = ffs(pfn) - 1;
+		size_order = fls_long(remaining) - 1;
+		order = min(align_order, size_order);
+
+		prepare_compound_page_to_free(curr, order, flags);
+		__free_frozen_pages(curr, order, FPI_NONE);
+		curr += (1UL << order);
+	}
+
+	VM_WARN_ON(curr != next);
+}
+
+/*
+ * Given a high-order compound page containing certain number of HWPoison
+ * pages, free only the healthy ones to buddy allocator.
+ *
+ * It calls __free_frozen_pages O(2^order) times and cause nontrivial
+ * overhead. So only use this when compound page really contains HWPoison.
+ *
+ * This implementation doesn't work in memdesc world.
+ */
+static void free_has_hwpoison_pages(struct page *page, unsigned int order)
+{
+	struct page *curr = page;
+	struct page *end = page + (1 << order);
+	struct page *next;
+	unsigned long flags = page->flags.f;
+	unsigned long nr_pages;
+	unsigned long total_freed = 0;
+	unsigned long total_hwp = 0;
+
+	VM_WARN_ON(flags & PAGE_FLAGS_CHECK_AT_FREE);
+
+	while (curr < end) {
+		next = curr;
+		nr_pages = 0;
+
+		while (next < end && !PageHWPoison(next)) {
+			++next;
+			++nr_pages;
+		}
+
+		if (PageHWPoison(next))
+			++total_hwp;
+
+		free_contiguous_pages(curr, next, flags);
+
+		total_freed += nr_pages;
+		curr = PageHWPoison(next) ? next + 1 : next;
+	}
+
+	pr_info("Excluded %lu hwpoison pages from folio\n", total_hwp);
+	pr_info("Freed %#lx pages from folio\n", total_freed);
+}
+
 void free_frozen_pages(struct page *page, unsigned int order)
 {
+	struct folio *folio = page_folio(page);
+
+	if (order > 0 && unlikely(folio_test_has_hwpoisoned(folio))) {
+		folio_clear_has_hwpoisoned(folio);
+		free_has_hwpoison_pages(page, order);
+		return;
+	}
+
 	__free_frozen_pages(page, order, FPI_NONE);
 }
 
Signature · 1 line
2.52.0.322.g1dd061c0dc-goog
#3 lore raw

Re: [PATCH v2 2/3] mm/page_alloc: only free healthy pages in high-order HWPoison folio

Quoted 30 lines · Jiaqi Yan

At the end of dissolve_free_hugetlb_folio that a free HugeTLB folio becomes non-HugeTLB, it is released to buddy allocator as a high-order folio, e.g. a folio that contains 262144 pages if the folio was a 1G HugeTLB hugepage.

This is problematic if the HugeTLB hugepage contained HWPoison subpages. In that case, since buddy allocator does not check HWPoison for non-zero-order folio, the raw HWPoison page can be given out with its buddy page and be re-used by either kernel or userspace.

Memory failure recovery (MFR) in kernel does attempt to take raw HWPoison page off buddy allocator after dissolve_free_hugetlb_folio. However, there is always a time window between dissolve_free_hugetlb_folio frees a HWPoison high-order folio to buddy allocator and MFR takes HWPoison raw page off buddy allocator.

One obvious way to avoid this problem is to add page sanity checks in page allocate or free path. However, it is against the past efforts to reduce sanity check overhead [1,2,3].

Introduce free_has_hwpoison_pages to only free the healthy pages and excludes the HWPoison ones in the high-order folio. The idea is to iterate through the sub-pages of the folio to identify contiguous ranges of healthy pages. Instead of freeing pages one by one, decompose healthy ranges into the largest possible blocks. Each block meets the requirements to be freed to buddy allocator (__free_frozen_pages).

Quoted 6 lines

free_has_hwpoison_pages has linear time complexity O(N) wrt the number of pages in the folio. While the power-of-two decomposition ensures that the number of calls to the buddy allocator is logarithmic for each contiguous healthy range, the mandatory linear scan of pages to identify PageHWPoison defines the overall time complexity.

Hi Jiaqi, thanks for the patch!

Have you tried measuring the latency of free_has_hwpoison_pages() when a few pages in a 1GB folio are hwpoisoned?

Just wanted to make sure we don't introduce a possible soft lockup... Or am I worrying too much?

Quoted 19 lines

[1] https://lore.kernel.org/linux-mm/[email protected]/
[2] https://lore.kernel.org/linux-mm/[email protected]/
[3] https://lore.kernel.org/all/[email protected]

---
 mm/page_alloc.c | 101 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 101 insertions(+)
mm/page_alloc.c +2 -0
index 822e05f1a9646..20c8862ce594e 100644
@@ -2976,8 +2976,109 @@ static void __free_frozen_pages(struct page *page, unsigned int order,
 	}
 }
 
+static void prepare_compound_page_to_free(struct page *new_head,
+					  unsigned int order,

mm/page_alloc.c static void __free_frozen_pages(struct page *page, unsigned int order, · line 2990

Quoted 10 lines
mm/page_alloc.c +10 -0
+					  unsigned long flags)
+{
+	new_head->flags.f = flags & (~PAGE_FLAGS_CHECK_AT_FREE);
+	new_head->mapping = NULL;
+	new_head->private = 0;
+
+	clear_compound_head(new_head);
+	if (order)
+		prep_compound_page(new_head, order);
+}

Not sure why it's building compound pages, just to decompose them when freeing via __free_frozen_pages()?

If you intended to reset compound head & tails, I think it's more readable to decompose the whole compound page at once and not build compound pages when freeing it?

Quoted 61 lines
mm/page_alloc.c +61 -0
+/*
+ * Given a range of pages physically contiguous physical, efficiently
+ * free them in blocks that meet __free_frozen_pages's requirements.
+ */
+static void free_contiguous_pages(struct page *curr, struct page *next,
+				  unsigned long flags)
+{
+	unsigned int order;
+	unsigned int align_order;
+	unsigned int size_order;
+	unsigned long pfn;
+	unsigned long end_pfn = page_to_pfn(next);
+	unsigned long remaining;
+
+	/*
+	 * This decomposition algorithm at every iteration chooses the
+	 * order to be the minimum of two constraints:
+	 * - Alignment: the largest power-of-two that divides current pfn.
+	 * - Size: the largest power-of-two that fits in the
+	 *   current remaining number of pages.
+	 */
+	while (curr < next) {
+		pfn = page_to_pfn(curr);
+		remaining = end_pfn - pfn;
+
+		align_order = ffs(pfn) - 1;
+		size_order = fls_long(remaining) - 1;
+		order = min(align_order, size_order);
+
+		prepare_compound_page_to_free(curr, order, flags);
+		__free_frozen_pages(curr, order, FPI_NONE);
+		curr += (1UL << order);
+	}
+
+	VM_WARN_ON(curr != next);
+}
+
+/*
+ * Given a high-order compound page containing certain number of HWPoison
+ * pages, free only the healthy ones to buddy allocator.
+ *
+ * It calls __free_frozen_pages O(2^order) times and cause nontrivial
+ * overhead. So only use this when compound page really contains HWPoison.
+ *
+ * This implementation doesn't work in memdesc world.
+ */
+static void free_has_hwpoison_pages(struct page *page, unsigned int order)
+{
+	struct page *curr = page;
+	struct page *end = page + (1 << order);
+	struct page *next;
+	unsigned long flags = page->flags.f;
+	unsigned long nr_pages;
+	unsigned long total_freed = 0;
+	unsigned long total_hwp = 0;
+
+	VM_WARN_ON(flags & PAGE_FLAGS_CHECK_AT_FREE);
+
+	while (curr < end) {
+		next = curr;
+		nr_pages = 0;

mm/page_alloc.c static void __free_frozen_pages(struct page *page, unsigned int order, · line 3062

Quoted 10 lines
mm/page_alloc.c +10 -0
+
+		while (next < end && !PageHWPoison(next)) {
+			++next;
+			++nr_pages;
+		}
+
+		if (PageHWPoison(next))
+			++total_hwp;
+
+		free_contiguous_pages(curr, next, flags);

page_owner, memory profiling (anything else?) will be confused because it was allocated as a larger size, but we're freeing only some portion of it.

Perhaps we need to run some portion of this code snippet (from free_pages_prepare()), before freeing portions of it:

        page_cpupid_reset_last(page);
        page->flags.f &= ~PAGE_FLAGS_CHECK_AT_PREP;
        reset_page_owner(page, order);
        page_table_check_free(page, order);
        pgalloc_tag_sub(page, 1 << order);
Quoted 8 lines
mm/page_alloc.c +8 -0
+		total_freed += nr_pages;
+		curr = PageHWPoison(next) ? next + 1 : next;
+	}
+
+	pr_info("Excluded %lu hwpoison pages from folio\n", total_hwp);
+	pr_info("Freed %#lx pages from folio\n", total_freed);
+}
+

mm/page_alloc.c static void __free_frozen_pages(struct page *page, unsigned int order, · line 3081

Quoted 10 lines
mm/page_alloc.c +8 -0
 void free_frozen_pages(struct page *page, unsigned int order)
 {
+	struct folio *folio = page_folio(page);
+
+	if (order > 0 && unlikely(folio_test_has_hwpoisoned(folio))) {
+		folio_clear_has_hwpoisoned(folio);
+		free_has_hwpoison_pages(page, order);
+		return;
+	}
+

It feels like it's a bit random place to do has_hwpoisoned check. Can we move this to free_pages_prepare() where we have some sanity checks (and also order-0 hwpoison page handling)?

Quoted 5 lines
mm/page_alloc.c +0 -0
 	__free_frozen_pages(page, order, FPI_NONE);
 }
 
Signature · 1 line
2.52.0.322.g1dd061c0dc-goog
Signature · 2 lines
Cheers,
Harry / Hyeonggon
author
#4 lore raw

Re: [PATCH v2 2/3] mm/page_alloc: only free healthy pages in high-order HWPoison folio

Quoted 32 lines · Harry Yoo
Quoted 30 lines · Jiaqi Yan

At the end of dissolve_free_hugetlb_folio that a free HugeTLB folio becomes non-HugeTLB, it is released to buddy allocator as a high-order folio, e.g. a folio that contains 262144 pages if the folio was a 1G HugeTLB hugepage.

This is problematic if the HugeTLB hugepage contained HWPoison subpages. In that case, since buddy allocator does not check HWPoison for non-zero-order folio, the raw HWPoison page can be given out with its buddy page and be re-used by either kernel or userspace.

Memory failure recovery (MFR) in kernel does attempt to take raw HWPoison page off buddy allocator after dissolve_free_hugetlb_folio. However, there is always a time window between dissolve_free_hugetlb_folio frees a HWPoison high-order folio to buddy allocator and MFR takes HWPoison raw page off buddy allocator.

One obvious way to avoid this problem is to add page sanity checks in page allocate or free path. However, it is against the past efforts to reduce sanity check overhead [1,2,3].

Introduce free_has_hwpoison_pages to only free the healthy pages and excludes the HWPoison ones in the high-order folio. The idea is to iterate through the sub-pages of the folio to identify contiguous ranges of healthy pages. Instead of freeing pages one by one, decompose healthy ranges into the largest possible blocks. Each block meets the requirements to be freed to buddy allocator (__free_frozen_pages).

Quoted 8 lines
Quoted 6 lines

free_has_hwpoison_pages has linear time complexity O(N) wrt the number of pages in the folio. While the power-of-two decomposition ensures that the number of calls to the buddy allocator is logarithmic for each contiguous healthy range, the mandatory linear scan of pages to identify PageHWPoison defines the overall time complexity.

Hi Jiaqi, thanks for the patch!

Thanks for your review/comments!

Quoted 6 lines

Have you tried measuring the latency of free_has_hwpoison_pages() when a few pages in a 1GB folio are hwpoisoned?

Just wanted to make sure we don't introduce a possible soft lockup... Or am I worrying too much?

In my local tests, freeing a 1GB folio with 1 / 3 / 8 HWPoison pages, I never run into a soft lockup. The 8-HWPoison-page case takes more time than other cases, meaning that handling the additional HWPoison page adds to the time cost.

After adding some instrument code, 10 sample runs of free_has_hwpoison_pages with 8 HWPoison pages:
- observed mean is 7.03 ms (5.97 ms when 3 HWPoison pages)
- observed standard deviation is 0.76 ms (0.18 ms when 3 HWPoison pages)

In comparison, freeing a 1G folio without any HWPoison pages 10 times (with same kernel config):
- observed mean is 3.39 ms
- observed standard deviation is 0.16ms

So it's around twice the baseline. It should be far from triggering a soft lockup, and the cost seems fair for handling exceptional hardware memory errors.

I can add these measurements in future revisions.

Quoted 18 lines
Quoted 18 lines

[1] https://lore.kernel.org/linux-mm/[email protected]/
[2] https://lore.kernel.org/linux-mm/[email protected]/
[3] https://lore.kernel.org/all/[email protected]

---
 mm/page_alloc.c | 101 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 101 insertions(+)
mm/page_alloc.c +0 -0
index 822e05f1a9646..20c8862ce594e 100644
@@ -2976,8 +2976,109 @@ static void __free_frozen_pages(struct page *page, unsigned int order,
      }
 }
 
Quoted 15 lines

mm/page_alloc.c static void __free_frozen_pages(struct page *page, unsigned int order, · line 2990

Quoted 12 lines
mm/page_alloc.c +12 -0
+static void prepare_compound_page_to_free(struct page *new_head,
+                                       unsigned int order,
+                                       unsigned long flags)
+{
+     new_head->flags.f = flags & (~PAGE_FLAGS_CHECK_AT_FREE);
+     new_head->mapping = NULL;
+     new_head->private = 0;
+
+     clear_compound_head(new_head);
+     if (order)
+             prep_compound_page(new_head, order);
+}

Not sure why it's building compound pages, just to decompose them when freeing via __free_frozen_pages()?

prepare_compound_page_to_free() borrowed the idea from __split_folio_to_order(). Conceptually the original folio is split into new compound pages with different orders; here this is done on the fly in free_contiguous_pages() when order is decided. __free_frozen_pages() is also happy with a compound page when order > 0, as I tested with free_pages_prepare before calling __free_frozen_pages().

Quoted 4 lines

If you intended to reset compound head & tails, I think it's more readable to decompose the whole compound page at once and not build compound pages when freeing it?

I don't think prepare_compound_page_to_free() is that hard to read, but I'm open to more opinions.

Quoted 68 lines
Quoted 68 lines
mm/page_alloc.c +67 -0
+/*
+ * Given a range of pages physically contiguous physical, efficiently
+ * free them in blocks that meet __free_frozen_pages's requirements.
+ */
+static void free_contiguous_pages(struct page *curr, struct page *next,
+                               unsigned long flags)
+{
+     unsigned int order;
+     unsigned int align_order;
+     unsigned int size_order;
+     unsigned long pfn;
+     unsigned long end_pfn = page_to_pfn(next);
+     unsigned long remaining;
+
+     /*
+      * This decomposition algorithm at every iteration chooses the
+      * order to be the minimum of two constraints:
+      * - Alignment: the largest power-of-two that divides current pfn.
+      * - Size: the largest power-of-two that fits in the
+      *   current remaining number of pages.
+      */
+     while (curr < next) {
+             pfn = page_to_pfn(curr);
+             remaining = end_pfn - pfn;
+
+             align_order = ffs(pfn) - 1;
+             size_order = fls_long(remaining) - 1;
+             order = min(align_order, size_order);
+
+             prepare_compound_page_to_free(curr, order, flags);
+             __free_frozen_pages(curr, order, FPI_NONE);
+             curr += (1UL << order);
+     }
+
+     VM_WARN_ON(curr != next);
+}
+
+/*
+ * Given a high-order compound page containing certain number of HWPoison
+ * pages, free only the healthy ones to buddy allocator.
+ *
+ * It calls __free_frozen_pages O(2^order) times and cause nontrivial
+ * overhead. So only use this when compound page really contains HWPoison.
+ *
+ * This implementation doesn't work in memdesc world.
+ */
+static void free_has_hwpoison_pages(struct page *page, unsigned int order)
+{
+     struct page *curr = page;
+     struct page *end = page + (1 << order);
+     struct page *next;
+     unsigned long flags = page->flags.f;
+     unsigned long nr_pages;
+     unsigned long total_freed = 0;
+     unsigned long total_hwp = 0;
+
+     VM_WARN_ON(flags & PAGE_FLAGS_CHECK_AT_FREE);
+
+     while (curr < end) {
+             next = curr;
+             nr_pages = 0;
+
+             while (next < end && !PageHWPoison(next)) {
+                     ++next;
+                     ++nr_pages;
+             }
+
Quoted 8 lines

mm/page_alloc.c static void __free_frozen_pages(struct page *page, unsigned int order, · line 3062

Quoted 4 lines
mm/page_alloc.c +4 -0
+             if (PageHWPoison(next))
+                     ++total_hwp;
+
+             free_contiguous_pages(curr, next, flags);

page_owner, memory profiling (anything else?) will be confused because it was allocated as a larger size, but we're freeing only some portion of it.

I am not sure, but looking at __split_unmapped_folio, it calls pgalloc_tag_split(folio, old_order, split_order) when splitting an old_order-order folio into a new split_order.

Maybe prepare_compound_page_to_free really should update_page_tag_ref(), I need to take a closer look at this with CONFIG_MEM_ALLOC_PROFILING (not something I usually enable).

Quoted 9 lines

Perhaps we need to run some portion of this code snippet (from free_pages_prepare()), before freeing portions of it:

        page_cpupid_reset_last(page);
        page->flags.f &= ~PAGE_FLAGS_CHECK_AT_PREP;
        reset_page_owner(page, order);
        page_table_check_free(page, order);
        pgalloc_tag_sub(page, 1 << order);

Since they come from free_pages_prepare, I believe these lines are already executed via free_contiguous_pages()=> __free_frozen_pages()=> free_pages_prepare(), right? Or am I missing something?

Quoted 15 lines
Quoted 15 lines
mm/page_alloc.c +12 -0
+             total_freed += nr_pages;
+             curr = PageHWPoison(next) ? next + 1 : next;
+     }
+
+     pr_info("Excluded %lu hwpoison pages from folio\n", total_hwp);
+     pr_info("Freed %#lx pages from folio\n", total_freed);
+}
+
 void free_frozen_pages(struct page *page, unsigned int order)
 {
+     struct folio *folio = page_folio(page);
+
+     if (order > 0 && unlikely(folio_test_has_hwpoisoned(folio))) {
+             folio_clear_has_hwpoisoned(folio);
Quoted 8 lines

mm/page_alloc.c static void __free_frozen_pages(struct page *page, unsigned int order, · line 3081

Quoted 4 lines
mm/page_alloc.c +4 -0
+             free_has_hwpoison_pages(page, order);
+             return;
+     }
+

It feels like it's a bit random place to do has_hwpoisoned check. Can we move this to free_pages_prepare() where we have some sanity checks (and also order-0 hwpoison page handling)?

While free_pages_prepare() seems to be a better place to do the has_hwpoisoned check, it is not a good place to do free_has_hwpoison_pages().

Quoted 10 lines
Quoted 6 lines
mm/page_alloc.c +0 -1
      __free_frozen_pages(page, order, FPI_NONE);
 }
 
--

2.52.0.322.g1dd061c0dc-goog

--
Cheers,
Harry / Hyeonggon

#5 lore raw

Re: [PATCH v2 2/3] mm/page_alloc: only free healthy pages in high-order HWPoison folio

Quoted 61 lines · Jiaqi Yan
Quoted 32 lines · Harry Yoo
Quoted 30 lines · Jiaqi Yan

At the end of dissolve_free_hugetlb_folio that a free HugeTLB folio becomes non-HugeTLB, it is released to buddy allocator as a high-order folio, e.g. a folio that contains 262144 pages if the folio was a 1G HugeTLB hugepage.

This is problematic if the HugeTLB hugepage contained HWPoison subpages. In that case, since buddy allocator does not check HWPoison for non-zero-order folio, the raw HWPoison page can be given out with its buddy page and be re-used by either kernel or userspace.

Memory failure recovery (MFR) in kernel does attempt to take raw HWPoison page off buddy allocator after dissolve_free_hugetlb_folio. However, there is always a time window between dissolve_free_hugetlb_folio frees a HWPoison high-order folio to buddy allocator and MFR takes HWPoison raw page off buddy allocator.

One obvious way to avoid this problem is to add page sanity checks in page allocate or free path. However, it is against the past efforts to reduce sanity check overhead [1,2,3].

Introduce free_has_hwpoison_pages to only free the healthy pages and excludes the HWPoison ones in the high-order folio. The idea is to iterate through the sub-pages of the folio to identify contiguous ranges of healthy pages. Instead of freeing pages one by one, decompose healthy ranges into the largest possible blocks. Each block meets the requirements to be freed to buddy allocator (__free_frozen_pages).

Quoted 8 lines
Quoted 6 lines

free_has_hwpoison_pages has linear time complexity O(N) wrt the number of pages in the folio. While the power-of-two decomposition ensures that the number of calls to the buddy allocator is logarithmic for each contiguous healthy range, the mandatory linear scan of pages to identify PageHWPoison defines the overall time complexity.

Hi Jiaqi, thanks for the patch!

Thanks for your review/comments!

Quoted 6 lines

Have you tried measuring the latency of free_has_hwpoison_pages() when a few pages in a 1GB folio are hwpoisoned?

Just wanted to make sure we don't introduce a possible soft lockup... Or am I worrying too much?

In my local tests, freeing a 1GB folio with 1 / 3 / 8 HWPoison pages, I never run into a soft lockup. The 8-HWPoison-page case takes more time than other cases, meaning that handling the additional HWPoison page adds to the time cost.

After adding some instrument code, 10 sample runs of free_has_hwpoison_pages with 8 HWPoison pages:
- observed mean is 7.03 ms (5.97 ms when 3 HWPoison pages)
- observed standard deviation is 0.76 ms (0.18 ms when 3 HWPoison pages)

Quoted 4 lines

In comparison, freeing a 1G folio without any HWPoison pages 10 times (with same kernel config):
- observed mean is 3.39 ms
- observed standard deviation is 0.16ms

Thanks for the measurement!

Quoted 3 lines

So it's around twice the baseline. It should be far from triggering a soft lockup, and the cost seems fair for handling exceptional hardware memory errors.

Yeah it looks fine to me.

Quoted 1 line

I can add these measurements in future revisions.

That would be nice, thanks.

Quoted 33 lines
Quoted 17 lines
Quoted 17 lines

[1] https://lore.kernel.org/linux-mm/[email protected]/
[2] https://lore.kernel.org/linux-mm/[email protected]/
[3] https://lore.kernel.org/all/[email protected]

---
 mm/page_alloc.c | 101 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 101 insertions(+)
mm/page_alloc.c +0 -0
index 822e05f1a9646..20c8862ce594e 100644
@@ -2976,8 +2976,109 @@ static void __free_frozen_pages(struct page *page, unsigned int order,
      }
 }
 
Quoted 15 lines

mm/page_alloc.c static void __free_frozen_pages(struct page *page, unsigned int order, · line 2990

Quoted 12 lines
mm/page_alloc.c +12 -0
+static void prepare_compound_page_to_free(struct page *new_head,
+                                       unsigned int order,
+                                       unsigned long flags)
+{
+     new_head->flags.f = flags & (~PAGE_FLAGS_CHECK_AT_FREE);
+     new_head->mapping = NULL;
+     new_head->private = 0;
+
+     clear_compound_head(new_head);
+     if (order)
+             prep_compound_page(new_head, order);
+}

Not sure why it's building compound pages, just to decompose them when freeing via __free_frozen_pages()?

Quoted 3 lines

prepare_compound_page_to_free() borrowed the idea from __split_folio_to_order(). Conceptually the original folio is split into new compound pages with different orders;

I see, and per the previous discussion we don't want to split it to 262,144 4K pages in the future, anyway...

Quoted 96 lines

here this is done on
the fly in free_contiguous_pages() when order is decided.

Quoted 67 lines
Quoted 67 lines
mm/page_alloc.c +67 -0
+/*
+ * Given a range of pages physically contiguous physical, efficiently
+ * free them in blocks that meet __free_frozen_pages's requirements.
+ */
+static void free_contiguous_pages(struct page *curr, struct page *next,
+                               unsigned long flags)
+{
+     unsigned int order;
+     unsigned int align_order;
+     unsigned int size_order;
+     unsigned long pfn;
+     unsigned long end_pfn = page_to_pfn(next);
+     unsigned long remaining;
+
+     /*
+      * This decomposition algorithm at every iteration chooses the
+      * order to be the minimum of two constraints:
+      * - Alignment: the largest power-of-two that divides current pfn.
+      * - Size: the largest power-of-two that fits in the
+      *   current remaining number of pages.
+      */
+     while (curr < next) {
+             pfn = page_to_pfn(curr);
+             remaining = end_pfn - pfn;
+
+             align_order = ffs(pfn) - 1;
+             size_order = fls_long(remaining) - 1;
+             order = min(align_order, size_order);
+
+             prepare_compound_page_to_free(curr, order, flags);
+             __free_frozen_pages(curr, order, FPI_NONE);
+             curr += (1UL << order);
+     }
+
+     VM_WARN_ON(curr != next);
+}
+
+/*
+ * Given a high-order compound page containing certain number of HWPoison
+ * pages, free only the healthy ones to buddy allocator.
+ *
+ * It calls __free_frozen_pages O(2^order) times and cause nontrivial
+ * overhead. So only use this when compound page really contains HWPoison.
+ *
+ * This implementation doesn't work in memdesc world.
+ */
+static void free_has_hwpoison_pages(struct page *page, unsigned int order)
+{
+     struct page *curr = page;
+     struct page *end = page + (1 << order);
+     struct page *next;
+     unsigned long flags = page->flags.f;
+     unsigned long nr_pages;
+     unsigned long total_freed = 0;
+     unsigned long total_hwp = 0;
+
+     VM_WARN_ON(flags & PAGE_FLAGS_CHECK_AT_FREE);
+
+     while (curr < end) {
+             next = curr;
+             nr_pages = 0;
+
+             while (next < end && !PageHWPoison(next)) {
+                     ++next;
+                     ++nr_pages;
+             }
+
Quoted 8 lines

mm/page_alloc.c static void __free_frozen_pages(struct page *page, unsigned int order, · line 3062

Quoted 4 lines
mm/page_alloc.c +4 -0
+             if (PageHWPoison(next))
+                     ++total_hwp;
+
+             free_contiguous_pages(curr, next, flags);

page_owner, memory profiling (anything else?) will be confused because it was allocated as a larger size, but we're freeing only some portion of it.

I am not sure, but looking at __split_unmapped_folio, it calls pgalloc_tag_split(folio, old_order, split_order) when splitting an old_order-order folio into a new split_order.

Maybe prepare_compound_page_to_free really should update_page_tag_ref(), I need to take a closer look at this with CONFIG_MEM_ALLOC_PROFILING (not something I usually enable).

Quoted 8 lines

Perhaps we need to run some portion of this code snippet (from free_pages_prepare()), before freeing portions of it:

        page_cpupid_reset_last(page);
        page->flags.f &= ~PAGE_FLAGS_CHECK_AT_PREP;
        reset_page_owner(page, order);
        page_table_check_free(page, order);
        pgalloc_tag_sub(page, 1 << order);
Quoted 3 lines

Since they come from free_pages_prepare, I believe these lines are already executed via free_contiguous_pages()=> __free_frozen_pages()=> free_pages_prepare(), right? Or am I missing something?

But they're called with order that is smaller than the original order. That's could be problematic; for example, memory profiling stores metadata only on the first page. If you pass anything other than the first page to free_pages_prepare(), it will not recognize that metadata was stored during allocation.

In general, I think they're not designed to handle cases where the allocation order and the free order differ (unless we split metadata like __split_unmapped_folio() does).

Quoted 23 lines
Quoted 14 lines
Quoted 14 lines
mm/page_alloc.c +12 -0
+             total_freed += nr_pages;
+             curr = PageHWPoison(next) ? next + 1 : next;
+     }
+
+     pr_info("Excluded %lu hwpoison pages from folio\n", total_hwp);
+     pr_info("Freed %#lx pages from folio\n", total_freed);
+}
+
 void free_frozen_pages(struct page *page, unsigned int order)
 {
+     struct folio *folio = page_folio(page);
+
+     if (order > 0 && unlikely(folio_test_has_hwpoisoned(folio))) {
+             folio_clear_has_hwpoisoned(folio);
Quoted 8 lines

mm/page_alloc.c static void __free_frozen_pages(struct page *page, unsigned int order, · line 3081

Quoted 4 lines
mm/page_alloc.c +4 -0
+             free_has_hwpoison_pages(page, order);
+             return;
+     }
+

It feels like it's a bit random place to do has_hwpoisoned check. Can we move this to free_pages_prepare() where we have some sanity checks (and also order-0 hwpoison page handling)?

Quoted 3 lines

While free_pages_prepare() seems to be a better place to do the has_hwpoisoned check, it is not a good place to do free_has_hwpoison_pages().

Why is it not a good place for free_has_hwpoison_pages()?

Callers of free_pages_prepare() are supposed to avoid freeing it back to the buddy or using the page when it returns false.

...except compaction_free(), which I don't have much idea what it's doing.

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
mm/page_alloc.c +0 -0
      __free_frozen_pages(page, order, FPI_NONE);
 }
Signature · 2 lines
Cheers,
Harry / Hyeonggon
author
#6 lore raw

Re: [PATCH v2 2/3] mm/page_alloc: only free healthy pages in high-order HWPoison folio

Quoted 220 lines · Harry Yoo
Quoted 61 lines · Jiaqi Yan
Quoted 32 lines · Harry Yoo
Quoted 30 lines · Jiaqi Yan

At the end of dissolve_free_hugetlb_folio that a free HugeTLB folio becomes non-HugeTLB, it is released to buddy allocator as a high-order folio, e.g. a folio that contains 262144 pages if the folio was a 1G HugeTLB hugepage.

This is problematic if the HugeTLB hugepage contained HWPoison subpages. In that case, since buddy allocator does not check HWPoison for non-zero-order folio, the raw HWPoison page can be given out with its buddy page and be re-used by either kernel or userspace.

Memory failure recovery (MFR) in kernel does attempt to take raw HWPoison page off buddy allocator after dissolve_free_hugetlb_folio. However, there is always a time window between dissolve_free_hugetlb_folio frees a HWPoison high-order folio to buddy allocator and MFR takes HWPoison raw page off buddy allocator.

One obvious way to avoid this problem is to add page sanity checks in page allocate or free path. However, it is against the past efforts to reduce sanity check overhead [1,2,3].

Introduce free_has_hwpoison_pages to only free the healthy pages and excludes the HWPoison ones in the high-order folio. The idea is to iterate through the sub-pages of the folio to identify contiguous ranges of healthy pages. Instead of freeing pages one by one, decompose healthy ranges into the largest possible blocks. Each block meets the requirements to be freed to buddy allocator (__free_frozen_pages).

Quoted 8 lines
Quoted 6 lines

free_has_hwpoison_pages has linear time complexity O(N) wrt the number of pages in the folio. While the power-of-two decomposition ensures that the number of calls to the buddy allocator is logarithmic for each contiguous healthy range, the mandatory linear scan of pages to identify PageHWPoison defines the overall time complexity.

Hi Jiaqi, thanks for the patch!

Thanks for your review/comments!

Quoted 6 lines

Have you tried measuring the latency of free_has_hwpoison_pages() when a few pages in a 1GB folio are hwpoisoned?

Just wanted to make sure we don't introduce a possible soft lockup... Or am I worrying too much?

In my local tests, freeing a 1GB folio with 1 / 3 / 8 HWPoison pages, I never run into a soft lockup. The 8-HWPoison-page case takes more time than other cases, meaning that handling the additional HWPoison page adds to the time cost.

After adding some instrument code, 10 sample runs of free_has_hwpoison_pages with 8 HWPoison pages:
- observed mean is 7.03 ms (5.97 ms when 3 HWPoison pages)
- observed standard deviation is 0.76 ms (0.18 ms when 3 HWPoison pages)

Quoted 4 lines

In comparison, freeing a 1G folio without any HWPoison pages 10 times (with same kernel config):
- observed mean is 3.39 ms
- observed standard deviation is 0.16ms

Thanks for the measurement!

Quoted 3 lines

So it's around twice the baseline. It should be far from triggering a soft lockup, and the cost seems fair for handling exceptional hardware memory errors.

Yeah it looks fine to me.

Quoted 1 line

I can add these measurements in future revisions.

That would be nice, thanks.

Quoted 33 lines
Quoted 17 lines
Quoted 17 lines

[1] https://lore.kernel.org/linux-mm/[email protected]/
[2] https://lore.kernel.org/linux-mm/[email protected]/
[3] https://lore.kernel.org/all/[email protected]

---
 mm/page_alloc.c | 101 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 101 insertions(+)
mm/page_alloc.c +0 -0
index 822e05f1a9646..20c8862ce594e 100644
@@ -2976,8 +2976,109 @@ static void __free_frozen_pages(struct page *page, unsigned int order,
      }
 }
 
Quoted 15 lines

mm/page_alloc.c static void __free_frozen_pages(struct page *page, unsigned int order, · line 2990

Quoted 12 lines
mm/page_alloc.c +12 -0
+static void prepare_compound_page_to_free(struct page *new_head,
+                                       unsigned int order,
+                                       unsigned long flags)
+{
+     new_head->flags.f = flags & (~PAGE_FLAGS_CHECK_AT_FREE);
+     new_head->mapping = NULL;
+     new_head->private = 0;
+
+     clear_compound_head(new_head);
+     if (order)
+             prep_compound_page(new_head, order);
+}

Not sure why it's building compound pages, just to decompose them when freeing via __free_frozen_pages()?

Quoted 3 lines

prepare_compound_page_to_free() borrowed the idea from __split_folio_to_order(). Conceptually the original folio is split into new compound pages with different orders;

I see, and per the previous discussion we don't want to split it to 262,144 4K pages in the future, anyway...

Quoted 96 lines

here this is done on
the fly in free_contiguous_pages() when order is decided.

Quoted 67 lines
Quoted 67 lines
mm/page_alloc.c +67 -0
+/*
+ * Given a range of pages physically contiguous physical, efficiently
+ * free them in blocks that meet __free_frozen_pages's requirements.
+ */
+static void free_contiguous_pages(struct page *curr, struct page *next,
+                               unsigned long flags)
+{
+     unsigned int order;
+     unsigned int align_order;
+     unsigned int size_order;
+     unsigned long pfn;
+     unsigned long end_pfn = page_to_pfn(next);
+     unsigned long remaining;
+
+     /*
+      * This decomposition algorithm at every iteration chooses the
+      * order to be the minimum of two constraints:
+      * - Alignment: the largest power-of-two that divides current pfn.
+      * - Size: the largest power-of-two that fits in the
+      *   current remaining number of pages.
+      */
+     while (curr < next) {
+             pfn = page_to_pfn(curr);
+             remaining = end_pfn - pfn;
+
+             align_order = ffs(pfn) - 1;
+             size_order = fls_long(remaining) - 1;
+             order = min(align_order, size_order);
+
+             prepare_compound_page_to_free(curr, order, flags);
+             __free_frozen_pages(curr, order, FPI_NONE);
+             curr += (1UL << order);
+     }
+
+     VM_WARN_ON(curr != next);
+}
+
+/*
+ * Given a high-order compound page containing certain number of HWPoison
+ * pages, free only the healthy ones to buddy allocator.
+ *
+ * It calls __free_frozen_pages O(2^order) times and cause nontrivial
+ * overhead. So only use this when compound page really contains HWPoison.
+ *
+ * This implementation doesn't work in memdesc world.
+ */
+static void free_has_hwpoison_pages(struct page *page, unsigned int order)
+{
+     struct page *curr = page;
+     struct page *end = page + (1 << order);
+     struct page *next;
+     unsigned long flags = page->flags.f;
+     unsigned long nr_pages;
+     unsigned long total_freed = 0;
+     unsigned long total_hwp = 0;
+
+     VM_WARN_ON(flags & PAGE_FLAGS_CHECK_AT_FREE);
+
+     while (curr < end) {
+             next = curr;
+             nr_pages = 0;
+
+             while (next < end && !PageHWPoison(next)) {
+                     ++next;
+                     ++nr_pages;
+             }
+
Quoted 8 lines

mm/page_alloc.c static void __free_frozen_pages(struct page *page, unsigned int order, · line 3062

Quoted 4 lines
mm/page_alloc.c +4 -0
+             if (PageHWPoison(next))
+                     ++total_hwp;
+
+             free_contiguous_pages(curr, next, flags);

page_owner, memory profiling (anything else?) will be confused because it was allocated as a larger size, but we're freeing only some portion of it.

I am not sure, but looking at __split_unmapped_folio, it calls pgalloc_tag_split(folio, old_order, split_order) when splitting an old_order-order folio into a new split_order.

Maybe prepare_compound_page_to_free really should update_page_tag_ref(), I need to take a closer look at this with CONFIG_MEM_ALLOC_PROFILING (not something I usually enable).

Quoted 8 lines

Perhaps we need to run some portion of this code snippet (from free_pages_prepare()), before freeing portions of it:

        page_cpupid_reset_last(page);
        page->flags.f &= ~PAGE_FLAGS_CHECK_AT_PREP;
        reset_page_owner(page, order);
        page_table_check_free(page, order);
        pgalloc_tag_sub(page, 1 << order);
Quoted 3 lines

Since they come from free_pages_prepare, I believe these lines are already executed via free_contiguous_pages()=> __free_frozen_pages()=> free_pages_prepare(), right? Or am I missing something?

Quoted 6 lines

But they're called with order that is smaller than the original order. That's could be problematic; for example, memory profiling stores metadata only on the first page. If you pass anything other than the first page to free_pages_prepare(), it will not recognize that metadata was stored during allocation.

Right, with MEM_ALLOC_PROFILING enabled, I ran into the following WARNING when freeing all blocks except the 1st one (which contains the original head page):

[ 2101.713669] ------------[ cut here ]------------
[ 2101.713670] alloc_tag was not set
[ 2101.713671] WARNING: ./include/linux/alloc_tag.h:164 at
__pgalloc_tag_sub+0xdf/0x160, CPU#18: hugetlb-mfr-3pa/33675
[ 2101.713693] CPU: 18 UID: 0 PID: 33675 Comm: hugetlb-mfr-3pa
Tainted: G S W O 6.19.0-smp-DEV #2 NONE
[ 2101.713698] Tainted: [S]=CPU_OUT_OF_SPEC, [W]=WARN, [O]=OOT_MODULE
[ 2101.713702] RIP: 0010:__pgalloc_tag_sub+0xdf/0x160
...
[ 2101.713723] Call Trace:
[ 2101.713725] <TASK>
[ 2101.713727] free_has_hwpoison_pages+0xbc/0x370
[ 2101.713731] free_frozen_pages+0xb3/0x100
[ 2101.713733] __folio_put+0xd5/0x100
[ 2101.713739] dissolve_free_hugetlb_folio+0x17f/0x1a0
[ 2101.713743] filemap_offline_hwpoison_folio+0x193/0x4c0
[ 2101.713747] ? __pfx_workingset_update_node+0x10/0x10
[ 2101.713751] remove_inode_hugepages+0x209/0x690
[ 2101.713757] ? on_each_cpu_cond_mask+0x1a/0x20
[ 2101.713760] ? __cond_resched+0x23/0x60
[ 2101.713768] ? n_tty_write+0x4c7/0x500
[ 2101.713773] hugetlbfs_setattr+0x127/0x170
[ 2101.713776] notify_change+0x32e/0x390
[ 2101.713781] do_ftruncate+0x12c/0x1a0
[ 2101.713786] __x64_sys_ftruncate+0x3e/0x70
[ 2101.713789] do_syscall_64+0x6f/0x890
[ 2101.713792] entry_SYSCALL_64_after_hwframe+0x76/0x7e
[ 2101.713811] </TASK>
[ 2101.713812] ---[ end trace 0000000000000000 ]---

This is because in free_pages_prepare(), pgalloc_tag_sub() found there is no alloc tag on the compound page being freeing.

Quoted 3 lines

In general, I think they're not designed to handle cases where the allocation order and the free order differ (unless we split metadata like __split_unmapped_folio() does).

I believe the proper way to fix this is to do something similar to pgalloc_tag_split(), used by __split_unmapped_folio(). When we split a new block from the original folio, we create a compound page from the block (just the way prep_compound_page_to_free does) and link the alloc tag of the original head page to the head of the new compound page. Something like copy_alloc_tag (to be added in v3) below to demo my idea, assuming tag=pgalloc_tag_get(original head page):

+/*
+ * Point page's alloc tag to an existing one.
+ */
+static void copy_alloc_tag(struct page *page, struct alloc_tag *tag) +{

+       union pgtag_ref_handle handle;
+       union codetag_ref ref;
+       unsigned long pfn = page_to_pfn(page);

+
+ if (!mem_alloc_profiling_enabled())
+ return;
+
+ /* tag is NULL if HugeTLB page is allocated in boot process. */
+ if (!tag)
+ return;
+
+ if (!get_page_tag_ref(page, &ref, &handle))
+ return;
+
+ /* Avoid overriding existing alloc tag from page. */
+ if (!ref.ct || is_codetag_empty(&ref)) {
+ alloc_tag_ref_set(&ref, tag);
+ update_page_tag_ref(handle, &ref);
+ }
+ put_page_tag_ref(handle);
+}
+
+static void prep_compound_page_to_free(struct page *head, unsigned int order,
+ unsigned long flags, struct
alloc_tag *tag)
+{

+       head->flags.f = flags & (~PAGE_FLAGS_CHECK_AT_FREE);
+       head->mapping = NULL;
+       head->private = 0;

+
+ clear_compound_head(head);
+ if (order)
+ prep_compound_page(head, order);
+
+ copy_alloc_tag(head, tag);
+}

I tested now the WARNING from include/linux/alloc_tag.h:164 is gone for both 2M and 1G pages. BTW we also need to copy_alloc_tag() for HWPoison pages before pgalloc_tag_sub().

Quoted 24 lines
Quoted 24 lines
Quoted 23 lines
Quoted 9 lines
mm/page_alloc.c +8 -0
+             total_freed += nr_pages;
+             curr = PageHWPoison(next) ? next + 1 : next;
+     }
+
+     pr_info("Excluded %lu hwpoison pages from folio\n", total_hwp);
+     pr_info("Freed %#lx pages from folio\n", total_freed);
+}
+

mm/page_alloc.c static void __free_frozen_pages(struct page *page, unsigned int order, · line 3081

Quoted 10 lines
mm/page_alloc.c +8 -0
 void free_frozen_pages(struct page *page, unsigned int order)
 {
+     struct folio *folio = page_folio(page);
+
+     if (order > 0 && unlikely(folio_test_has_hwpoisoned(folio))) {
+             folio_clear_has_hwpoisoned(folio);
+             free_has_hwpoison_pages(page, order);
+             return;
+     }
+

It feels like it's a bit random place to do has_hwpoisoned check. Can we move this to free_pages_prepare() where we have some sanity checks (and also order-0 hwpoison page handling)?

Quoted 8 lines
Quoted 3 lines

While free_pages_prepare() seems to be a better place to do the has_hwpoisoned check, it is not a good place to do free_has_hwpoison_pages().

Why is it not a good place for free_has_hwpoison_pages()?

Callers of free_pages_prepare() are supposed to avoid freeing it back to the buddy or using the page when it returns false.

What I mean is, callers of free_pages_prepare() wouldn't know from the single false return value whether 1) they should completely bail out or 2) they should retry with free_has_hwpoison_pages. So for now I think it'd better that free_frozen_pages does the check and act upon the check result. Not sure if there is a better place, or worthy to change free_pages_prepare()'s return type?

Quoted 10 lines

...except compaction_free(), which I don't have much idea what it's doing.

Quoted 2 lines
Quoted 2 lines

mm/page_alloc.c static void __free_frozen_pages(struct page *page, unsigned int order, · line 3083

Quoted 2 lines
mm/page_alloc.c +0 -0
      __free_frozen_pages(page, order, FPI_NONE);
 }

--
Cheers,
Harry / Hyeonggon

#7 lore raw

Re: [PATCH v2 2/3] mm/page_alloc: only free healthy pages in high-order HWPoison folio

Quoted 116 lines · Jiaqi Yan
Quoted 71 lines · Harry Yoo
Quoted 66 lines · Jiaqi Yan
Quoted 39 lines · Harry Yoo
Quoted 38 lines · Jiaqi Yan
---
 mm/page_alloc.c | 101 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 101 insertions(+)
mm/page_alloc.c +30 -0
index 822e05f1a9646..20c8862ce594e 100644
+/*
+ * Given a high-order compound page containing certain number of HWPoison
+ * pages, free only the healthy ones to buddy allocator.
+ *
+ * It calls __free_frozen_pages O(2^order) times and cause nontrivial
+ * overhead. So only use this when compound page really contains HWPoison.
+ *
+ * This implementation doesn't work in memdesc world.
+ */
+static void free_has_hwpoison_pages(struct page *page, unsigned int order)
+{
+     struct page *curr = page;
+     struct page *end = page + (1 << order);
+     struct page *next;
+     unsigned long flags = page->flags.f;
+     unsigned long nr_pages;
+     unsigned long total_freed = 0;
+     unsigned long total_hwp = 0;
+
+     VM_WARN_ON(flags & PAGE_FLAGS_CHECK_AT_FREE);
+
+     while (curr < end) {
+             next = curr;
+             nr_pages = 0;
+
+             while (next < end && !PageHWPoison(next)) {
+                     ++next;
+                     ++nr_pages;
+             }
+
Quoted 8 lines

mm/page_alloc.c static void __free_frozen_pages(struct page *page, unsigned int order, · line 3062

Quoted 4 lines
mm/page_alloc.c +4 -0
+             if (PageHWPoison(next))
+                     ++total_hwp;
+
+             free_contiguous_pages(curr, next, flags);

page_owner, memory profiling (anything else?) will be confused because it was allocated as a larger size, but we're freeing only some portion of it.

I am not sure, but looking at __split_unmapped_folio, it calls pgalloc_tag_split(folio, old_order, split_order) when splitting an old_order-order folio into a new split_order.

Maybe prepare_compound_page_to_free really should update_page_tag_ref(), I need to take a closer look at this with CONFIG_MEM_ALLOC_PROFILING (not something I usually enable).

Quoted 8 lines

Perhaps we need to run some portion of this code snippet (from free_pages_prepare()), before freeing portions of it:

        page_cpupid_reset_last(page);
        page->flags.f &= ~PAGE_FLAGS_CHECK_AT_PREP;
        reset_page_owner(page, order);
        page_table_check_free(page, order);
        pgalloc_tag_sub(page, 1 << order);
Quoted 3 lines

Since they come from free_pages_prepare, I believe these lines are already executed via free_contiguous_pages()=> __free_frozen_pages()=> free_pages_prepare(), right? Or am I missing something?

Quoted 6 lines

But they're called with order that is smaller than the original order. That's could be problematic; for example, memory profiling stores metadata only on the first page. If you pass anything other than the first page to free_pages_prepare(), it will not recognize that metadata was stored during allocation.

Right, with MEM_ALLOC_PROFILING enabled, I ran into the following WARNING when freeing all blocks except the 1st one (which contains the original head page):

[ 2101.713669] ------------[ cut here ]------------
[ 2101.713670] alloc_tag was not set
[ 2101.713671] WARNING: ./include/linux/alloc_tag.h:164 at
__pgalloc_tag_sub+0xdf/0x160, CPU#18: hugetlb-mfr-3pa/33675
[ 2101.713693] CPU: 18 UID: 0 PID: 33675 Comm: hugetlb-mfr-3pa
Tainted: G S W O 6.19.0-smp-DEV #2 NONE
[ 2101.713698] Tainted: [S]=CPU_OUT_OF_SPEC, [W]=WARN, [O]=OOT_MODULE
[ 2101.713702] RIP: 0010:__pgalloc_tag_sub+0xdf/0x160
...
[ 2101.713723] Call Trace:
[ 2101.713725] <TASK>
[ 2101.713727] free_has_hwpoison_pages+0xbc/0x370
[ 2101.713731] free_frozen_pages+0xb3/0x100
[ 2101.713733] __folio_put+0xd5/0x100
[ 2101.713739] dissolve_free_hugetlb_folio+0x17f/0x1a0
[ 2101.713743] filemap_offline_hwpoison_folio+0x193/0x4c0
[ 2101.713747] ? __pfx_workingset_update_node+0x10/0x10
[ 2101.713751] remove_inode_hugepages+0x209/0x690
[ 2101.713757] ? on_each_cpu_cond_mask+0x1a/0x20
[ 2101.713760] ? __cond_resched+0x23/0x60
[ 2101.713768] ? n_tty_write+0x4c7/0x500
[ 2101.713773] hugetlbfs_setattr+0x127/0x170
[ 2101.713776] notify_change+0x32e/0x390
[ 2101.713781] do_ftruncate+0x12c/0x1a0
[ 2101.713786] __x64_sys_ftruncate+0x3e/0x70
[ 2101.713789] do_syscall_64+0x6f/0x890
[ 2101.713792] entry_SYSCALL_64_after_hwframe+0x76/0x7e
[ 2101.713811] </TASK>
[ 2101.713812] ---[ end trace 0000000000000000 ]---

This is because in free_pages_prepare(), pgalloc_tag_sub() found there is no alloc tag on the compound page being freeing.

Quoted 6 lines
Quoted 3 lines

In general, I think they're not designed to handle cases where the allocation order and the free order differ (unless we split metadata like __split_unmapped_folio() does).

I believe the proper way to fix this is to do something similar to pgalloc_tag_split(), used by __split_unmapped_folio().

Yeah, that will work.

The difference is that pgalloc_tag_split(), split_page_owner(), and split_page_memcg() only support splitting the whole page into many (> 1) smaller pieces, but we're trying to create only one smaller page from the original page.

mm/page_alloc.c static void __free_frozen_pages(struct page *page, unsigned int order,

Quoted 13 lines

When we split a
new block from the original folio, we create a compound page from the block (just the way prep_compound_page_to_free does) and link the alloc tag of the original head page to the head of the new compound page.

Something like copy_alloc_tag (to be added in v3) below to demo my idea, assuming tag=pgalloc_tag_get(original head page):

mm/page_alloc.c +4 -0
+/*
+ * Point page's alloc tag to an existing one.
+ */
+static void copy_alloc_tag(struct page *page, struct alloc_tag *tag)

This should be defined in lib/alloc_tag.c

Quoted 7 lines
(quoted diff) +7 -0
+{
+       union pgtag_ref_handle handle;
+       union codetag_ref ref;
+       unsigned long pfn = page_to_pfn(page);
+
+       if (!mem_alloc_profiling_enabled())
+               return;

Review

Quoted 10 lines
(quoted diff) +10 -0
+
+       /* tag is NULL if HugeTLB page is allocated in boot process. */
+       if (!tag)
+               return;
+
+       if (!get_page_tag_ref(page, &ref, &handle))
+               return;
+
+       /* Avoid overriding existing alloc tag from page. */
+       if (!ref.ct || is_codetag_empty(&ref)) {

Is it an error if the page already has a valid tag?

Quoted 9 lines
(quoted diff) +8 -0
+               alloc_tag_ref_set(&ref, tag);
+               update_page_tag_ref(handle, &ref);
+       }
+       put_page_tag_ref(handle);
+}
+
+static void prep_compound_page_to_free(struct page *head, unsigned int order,
+                                      unsigned long flags, struct

alloc_tag *tag)

Review

Quoted 10 lines
(quoted diff) +10 -0
+{
+       head->flags.f = flags & (~PAGE_FLAGS_CHECK_AT_FREE);
+       head->mapping = NULL;
+       head->private = 0;
+
+       clear_compound_head(head);
+       if (order)
+               prep_compound_page(head, order);
+
+       copy_alloc_tag(head, tag);

Do we need a similar thing for memcg? Probably not, since it should have been uncharged before freeing (as long as we're not using it for kmem pages) Perhaps a comment on why we don't need to split memcg will be enough.

Do we need a similar thing for page_owner? I think yes, although it won't crash or cause a warning, it will be inconsistent if we split page_owner in split_page()/__split_unmapped_folio()/etc but not in prep_compound_page_to_free().

Quoted 33 lines

+}

I tested now the WARNING from include/linux/alloc_tag.h:164 is gone for both 2M and 1G pages. BTW we also need to copy_alloc_tag() for HWPoison pages before pgalloc_tag_sub().

Quoted 27 lines
Quoted 26 lines
Quoted 14 lines
Quoted 14 lines
mm/page_alloc.c +12 -0
+             total_freed += nr_pages;
+             curr = PageHWPoison(next) ? next + 1 : next;
+     }
+
+     pr_info("Excluded %lu hwpoison pages from folio\n", total_hwp);
+     pr_info("Freed %#lx pages from folio\n", total_freed);
+}
+
 void free_frozen_pages(struct page *page, unsigned int order)
 {
+     struct folio *folio = page_folio(page);
+
+     if (order > 0 && unlikely(folio_test_has_hwpoisoned(folio))) {
+             folio_clear_has_hwpoisoned(folio);
Quoted 8 lines

mm/page_alloc.c static void __free_frozen_pages(struct page *page, unsigned int order, · line 3081

Quoted 4 lines
mm/page_alloc.c +4 -0
+             free_has_hwpoison_pages(page, order);
+             return;
+     }
+

It feels like it's a bit random place to do has_hwpoisoned check. Can we move this to free_pages_prepare() where we have some sanity checks (and also order-0 hwpoison page handling)?

While free_pages_prepare() seems to be a better place to do the has_hwpoisoned check, it is not a good place to do free_has_hwpoison_pages().

Quoted 8 lines
Quoted 4 lines

Why is it not a good place for free_has_hwpoison_pages()?

Callers of free_pages_prepare() are supposed to avoid freeing it back to the buddy or using the page when it returns false.

What I mean is, callers of free_pages_prepare() wouldn't know from the single false return value whether 1) they should completely bail out or 2) they should retry with free_has_hwpoison_pages.

I was thinking that once free_pages_prepare() returns false, it already has done all the work to isolate HWPoison pages and freed healthy portions, so the caller doesn't have to do anything and can bail out completely.

Quoted 10 lines

So for now I
think it'd better that free_frozen_pages does the check and act upon the check result. Not sure if there is a better place, or worthy to change free_pages_prepare()'s return type?

Quoted 5 lines

...except compaction_free(), which I don't have much idea what it's doing.

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
      __free_frozen_pages(page, order, FPI_NONE);

}

Signature · 2 lines
Cheers,
Harry / Hyeonggon
author
#8 lore raw

Re: [PATCH v2 2/3] mm/page_alloc: only free healthy pages in high-order HWPoison folio

Quoted 125 lines · Harry Yoo
Quoted 116 lines · Jiaqi Yan
Quoted 71 lines · Harry Yoo
Quoted 66 lines · Jiaqi Yan
Quoted 39 lines · Harry Yoo
Quoted 38 lines · Jiaqi Yan
---
 mm/page_alloc.c | 101 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 101 insertions(+)
mm/page_alloc.c +30 -0
index 822e05f1a9646..20c8862ce594e 100644
+/*
+ * Given a high-order compound page containing certain number of HWPoison
+ * pages, free only the healthy ones to buddy allocator.
+ *
+ * It calls __free_frozen_pages O(2^order) times and cause nontrivial
+ * overhead. So only use this when compound page really contains HWPoison.
+ *
+ * This implementation doesn't work in memdesc world.
+ */
+static void free_has_hwpoison_pages(struct page *page, unsigned int order)
+{
+     struct page *curr = page;
+     struct page *end = page + (1 << order);
+     struct page *next;
+     unsigned long flags = page->flags.f;
+     unsigned long nr_pages;
+     unsigned long total_freed = 0;
+     unsigned long total_hwp = 0;
+
+     VM_WARN_ON(flags & PAGE_FLAGS_CHECK_AT_FREE);
+
+     while (curr < end) {
+             next = curr;
+             nr_pages = 0;
+
+             while (next < end && !PageHWPoison(next)) {
+                     ++next;
+                     ++nr_pages;
+             }
+
Quoted 8 lines

mm/page_alloc.c static void __free_frozen_pages(struct page *page, unsigned int order, · line 3062

Quoted 4 lines
mm/page_alloc.c +4 -0
+             if (PageHWPoison(next))
+                     ++total_hwp;
+
+             free_contiguous_pages(curr, next, flags);

page_owner, memory profiling (anything else?) will be confused because it was allocated as a larger size, but we're freeing only some portion of it.

I am not sure, but looking at __split_unmapped_folio, it calls pgalloc_tag_split(folio, old_order, split_order) when splitting an old_order-order folio into a new split_order.

Maybe prepare_compound_page_to_free really should update_page_tag_ref(), I need to take a closer look at this with CONFIG_MEM_ALLOC_PROFILING (not something I usually enable).

Quoted 8 lines

Perhaps we need to run some portion of this code snippet (from free_pages_prepare()), before freeing portions of it:

        page_cpupid_reset_last(page);
        page->flags.f &= ~PAGE_FLAGS_CHECK_AT_PREP;
        reset_page_owner(page, order);
        page_table_check_free(page, order);
        pgalloc_tag_sub(page, 1 << order);
Quoted 3 lines

Since they come from free_pages_prepare, I believe these lines are already executed via free_contiguous_pages()=> __free_frozen_pages()=> free_pages_prepare(), right? Or am I missing something?

Quoted 6 lines

But they're called with order that is smaller than the original order. That's could be problematic; for example, memory profiling stores metadata only on the first page. If you pass anything other than the first page to free_pages_prepare(), it will not recognize that metadata was stored during allocation.

Right, with MEM_ALLOC_PROFILING enabled, I ran into the following WARNING when freeing all blocks except the 1st one (which contains the original head page):

[ 2101.713669] ------------[ cut here ]------------
[ 2101.713670] alloc_tag was not set
[ 2101.713671] WARNING: ./include/linux/alloc_tag.h:164 at
__pgalloc_tag_sub+0xdf/0x160, CPU#18: hugetlb-mfr-3pa/33675
[ 2101.713693] CPU: 18 UID: 0 PID: 33675 Comm: hugetlb-mfr-3pa
Tainted: G S W O 6.19.0-smp-DEV #2 NONE
[ 2101.713698] Tainted: [S]=CPU_OUT_OF_SPEC, [W]=WARN, [O]=OOT_MODULE
[ 2101.713702] RIP: 0010:__pgalloc_tag_sub+0xdf/0x160
...
[ 2101.713723] Call Trace:
[ 2101.713725] <TASK>
[ 2101.713727] free_has_hwpoison_pages+0xbc/0x370
[ 2101.713731] free_frozen_pages+0xb3/0x100
[ 2101.713733] __folio_put+0xd5/0x100
[ 2101.713739] dissolve_free_hugetlb_folio+0x17f/0x1a0
[ 2101.713743] filemap_offline_hwpoison_folio+0x193/0x4c0
[ 2101.713747] ? __pfx_workingset_update_node+0x10/0x10
[ 2101.713751] remove_inode_hugepages+0x209/0x690
[ 2101.713757] ? on_each_cpu_cond_mask+0x1a/0x20
[ 2101.713760] ? __cond_resched+0x23/0x60
[ 2101.713768] ? n_tty_write+0x4c7/0x500
[ 2101.713773] hugetlbfs_setattr+0x127/0x170
[ 2101.713776] notify_change+0x32e/0x390
[ 2101.713781] do_ftruncate+0x12c/0x1a0
[ 2101.713786] __x64_sys_ftruncate+0x3e/0x70
[ 2101.713789] do_syscall_64+0x6f/0x890
[ 2101.713792] entry_SYSCALL_64_after_hwframe+0x76/0x7e
[ 2101.713811] </TASK>
[ 2101.713812] ---[ end trace 0000000000000000 ]---

This is because in free_pages_prepare(), pgalloc_tag_sub() found there is no alloc tag on the compound page being freeing.

Quoted 6 lines
Quoted 3 lines

In general, I think they're not designed to handle cases where the allocation order and the free order differ (unless we split metadata like __split_unmapped_folio() does).

I believe the proper way to fix this is to do something similar to pgalloc_tag_split(), used by __split_unmapped_folio().

Quoted 15 lines

Yeah, that will work.

The difference is that pgalloc_tag_split(), split_page_owner(), and split_page_memcg() only support splitting the whole page into many (> 1) smaller pieces, but we're trying to create only one smaller page from the original page.

Quoted 8 lines

When we split a
new block from the original folio, we create a compound page from the block (just the way prep_compound_page_to_free does) and link the alloc tag of the original head page to the head of the new compound page.

Something like copy_alloc_tag (to be added in v3) below to demo my idea, assuming tag=pgalloc_tag_get(original head page):

Actually I had another idea for page metadata like page alloc tag and page owner - how about we do the special handling AFTER free_pages_prepare()?

Looking at free_pages_prepare, it actually breaks down compound page (if it is compound, using free_tail_page_prepare), adjust page flags, and deals page metadata:

for (i = 1; i < (1 << order); i++) {
    if (compound)
        bad += free_tail_page_prepare(page, page + i);

...
page->flags.f &= ~PAGE_FLAGS_CHECK_AT_PREP;
reset_page_owner(page, order);
page_table_check_free(page, order);
pgalloc_tag_sub(page, 1 << order);

Even the original compound page has some HWPoison pages, these operations should be done in the same way when no HWPoison page exist. The compound structure will be gone after free, the alloc tag will be reduced after free, the page owner will be reset after free. So instead of splitting these stuff into temporary compound pages, why not getting rid of them in the first place with free_pages_prepare()? This is unique to the freeing path, so we don't need to mimic everything __split_unmapped_folio() does.

At high level, applying the idea to __free_pages_ok (or __free_frozen_pages) looks like this:

+static bool compound_has_hwpoisoned(struct page *page, unsigned int order)
{
+ if (order == 0 || !PageCompound(page))
+ return false;
+
+ return folio_test_has_hwpoisoned(page_folio(page));
+}

static void __free_pages_ok(struct page *page, unsigned int order,

 {
        unsigned long pfn = page_to_pfn(page);
        struct zone *zone = page_zone(page);

+ bool should_fhh = compound_has_hwpoisoned(page, order);

- if (free_pages_prepare(page, order))
- free_one_page(zone, page, pfn, order, fpi_flags);
+ if (!free_pages_prepare(page, order))
+ return;
+
+ if (should_fhh) {
+ free_has_hwpoisoned(page, order, fpi_flags);
+ return;
+ }
+
+ free_one_page(zone, page, pfn, order, fpi_flags);
}

Under this idea, free_has_hwpoisoned() doesn't need prepare_compound_page_to_free() anymore. It can just directly call free_one_page() when it selects the right order.

The only imperfect part is compound_has_hwpoisoned(), that we need to make a call to whether free_has_hwpoisoned() or not ealier than free_pages_prepare(). free_pages_prepare() clears PAGE_FLAGS_SECOND flags on the first tail page of a compound, which clears PG_has_hwpoisoned. On the other hand, we can't easily exclude PG_has_hwpoisoned from PAGE_FLAGS_SECOND. Because PG_has_hwpoisoned == PG_active, free_page_is_bad() will confuse and complaint that the first tail page is still active.

I have implemented a working prototype and tested just fine. I can send it out as v3 after clean it up.

Quoted 19 lines

mm/page_alloc.c static void __free_frozen_pages(struct page *page, unsigned int order,

Quoted 5 lines
mm/page_alloc.c +4 -0
+/*
+ * Point page's alloc tag to an existing one.
+ */
+static void copy_alloc_tag(struct page *page, struct alloc_tag *tag)

This should be defined in lib/alloc_tag.c

Quoted 11 lines
(quoted diff) +11 -0
+{
+       union pgtag_ref_handle handle;
+       union codetag_ref ref;
+       unsigned long pfn = page_to_pfn(page);
+
+       if (!mem_alloc_profiling_enabled())
+               return;
+
+       /* tag is NULL if HugeTLB page is allocated in boot process. */
+       if (!tag)
+               return;
Quoted 8 lines

mm/page_alloc.c static void __free_frozen_pages(struct page *page, unsigned int order,

Quoted 6 lines
mm/page_alloc.c +6 -0
+
+       if (!get_page_tag_ref(page, &ref, &handle))
+               return;
+
+       /* Avoid overriding existing alloc tag from page. */
+       if (!ref.ct || is_codetag_empty(&ref)) {

Is it an error if the page already has a valid tag?

Probably not important anymore, but I don't think it is an error. The split block having the original folio's head page will have a valid tag. We don't need to set + update for this block; otherwise alloc_tag_add_check will WARN().

Quoted 14 lines
Quoted 14 lines
(quoted diff) +8 -0
+               alloc_tag_ref_set(&ref, tag);
+               update_page_tag_ref(handle, &ref);
+       }
+       put_page_tag_ref(handle);
+}
+
+static void prep_compound_page_to_free(struct page *head, unsigned int order,
+                                      unsigned long flags, struct

alloc_tag *tag)

(quoted diff) +4 -0
+{
+       head->flags.f = flags & (~PAGE_FLAGS_CHECK_AT_FREE);
+       head->mapping = NULL;
+       head->private = 0;
Quoted 8 lines

mm/page_alloc.c static void __free_frozen_pages(struct page *page, unsigned int order,

Quoted 6 lines
mm/page_alloc.c +6 -0
+
+       clear_compound_head(head);
+       if (order)
+               prep_compound_page(head, order);
+
+       copy_alloc_tag(head, tag);

Do we need a similar thing for memcg? Probably not, since it should have

Yes, I think we don't need to do anything for memgc, it is already uncharged in __folio_put().

Quoted 1 line

been uncharged before freeing (as long as we're not using it for kmem pages)

You mean the folio was charged to kernel memory, right? From my reading of uncharge_folio(), it covers the kmem case.

Quoted 45 lines

Perhaps a comment on why we don't need to split memcg will be enough.

Do we need a similar thing for page_owner? I think yes, although it won't crash or cause a warning, it will be inconsistent if we split page_owner in split_page()/__split_unmapped_folio()/etc but not in prep_compound_page_to_free().

Quoted 38 lines

+}

I tested now the WARNING from include/linux/alloc_tag.h:164 is gone for both 2M and 1G pages. BTW we also need to copy_alloc_tag() for HWPoison pages before pgalloc_tag_sub().

Quoted 31 lines
Quoted 23 lines
Quoted 14 lines
Quoted 14 lines
mm/page_alloc.c +12 -0
+             total_freed += nr_pages;
+             curr = PageHWPoison(next) ? next + 1 : next;
+     }
+
+     pr_info("Excluded %lu hwpoison pages from folio\n", total_hwp);
+     pr_info("Freed %#lx pages from folio\n", total_freed);
+}
+
 void free_frozen_pages(struct page *page, unsigned int order)
 {
+     struct folio *folio = page_folio(page);
+
+     if (order > 0 && unlikely(folio_test_has_hwpoisoned(folio))) {
+             folio_clear_has_hwpoisoned(folio);
Quoted 8 lines

mm/page_alloc.c static void __free_frozen_pages(struct page *page, unsigned int order, · line 3081

Quoted 4 lines
mm/page_alloc.c +4 -0
+             free_has_hwpoison_pages(page, order);
+             return;
+     }
+

It feels like it's a bit random place to do has_hwpoisoned check. Can we move this to free_pages_prepare() where we have some sanity checks (and also order-0 hwpoison page handling)?

Quoted 3 lines

While free_pages_prepare() seems to be a better place to do the has_hwpoisoned check, it is not a good place to do free_has_hwpoison_pages().

Why is it not a good place for free_has_hwpoison_pages()?

Callers of free_pages_prepare() are supposed to avoid freeing it back to the buddy or using the page when it returns false.

Quoted 7 lines
Quoted 3 lines

What I mean is, callers of free_pages_prepare() wouldn't know from the single false return value whether 1) they should completely bail out or 2) they should retry with free_has_hwpoison_pages.

I was thinking that once free_pages_prepare() returns false, it already has done all the work to isolate HWPoison pages and freed healthy portions, so the caller doesn't have to do anything and can bail out completely.

Does this change the meaning of free_pages_prepare()'s return value? My first take is it returns if the preparation+check works are good. I don't know if doing the freeing work in free_pages_prepare() is a good idea. But if it is fine, we can actually hide the ugly compound_has_hwpoisoned() entirely inside free_pages_prepare():
- true means preparation + check work are all good but caller needs to free prepared+checked pages itself
- false means one of the two:
- some check failed and it is impossible to safely free pages for callers
- this is a compound page with some HWPoison pages, and healthy
pages are already freed safely

Doesn't seem very clean with a boolean return type...

Quoted 15 lines
Quoted 11 lines

So for now I
think it'd better that free_frozen_pages does the check and act upon the check result. Not sure if there is a better place, or worthy to change free_pages_prepare()'s return type?

Quoted 5 lines

...except compaction_free(), which I don't have much idea what it's doing.

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
      __free_frozen_pages(page, order, FPI_NONE);

}

--
Cheers,
Harry / Hyeonggon

#9 lore raw

Re: [PATCH v2 2/3] mm/page_alloc: only free healthy pages in high-order HWPoison folio

Quoted 26 lines · Jiaqi Yan
Quoted 25 lines · Harry Yoo
Quoted 10 lines · Jiaqi Yan
Quoted 6 lines · Harry Yoo
Quoted 2 lines · Jiaqi Yan
Quoted 1 line · Harry Yoo

On Fri, Dec 19, 2025 at 06:33:45PM +0000, Jiaqi Yan wrote:

In general, I think they're not designed to handle cases where the allocation order and the free order differ (unless we split metadata like __split_unmapped_folio() does).

I believe the proper way to fix this is to do something similar to pgalloc_tag_split(), used by __split_unmapped_folio().

Yeah, that will work.

The difference is that pgalloc_tag_split(), split_page_owner(), and split_page_memcg() only support splitting the whole page into many (> 1) smaller pieces, but we're trying to create only one smaller page from the original page.

Quoted 6 lines

When we split a
new block from the original folio, we create a compound page from the block (just the way prep_compound_page_to_free does) and link the alloc tag of the original head page to the head of the new compound page.

Quoted 6 lines
Quoted 2 lines
Quoted 2 lines

Something like copy_alloc_tag (to be added in v3) below to demo my idea, assuming tag=pgalloc_tag_get(original head page):

Actually I had another idea for page metadata like page alloc tag and page owner - how about we do the special handling AFTER free_pages_prepare()?

Sounds good to me.

Quoted 7 lines

Looking at free_pages_prepare, it actually breaks down compound page (if it is compound, using free_tail_page_prepare), adjust page flags, and deals page metadata:

for (i = 1; i < (1 << order); i++) {
    if (compound)
        bad += free_tail_page_prepare(page, page + i);
Quoted 8 lines

...
page->flags.f &= ~PAGE_FLAGS_CHECK_AT_PREP;
reset_page_owner(page, order);
page_table_check_free(page, order);
pgalloc_tag_sub(page, 1 << order);

Even the original compound page has some HWPoison pages, these operations should be done in the same way when no HWPoison page exist.

and we're not losing hwpoison flag after free_pages_prepare()... that actually sounds fine.

Quoted 2 lines

The compound structure will be gone after free, the alloc tag will be reduced after free, the page owner will be reset after free.

Right.

Quoted 5 lines

So instead of splitting these stuff into temporary compound pages, why not getting rid of them in the first place with free_pages_prepare()?

This is unique to the freeing path, so we don't need to mimic everything __split_unmapped_folio() does.

Not 100% sure how it'd work in the memdesc world,
but for today, right.

Quoted 30 lines

At high level, applying the idea to __free_pages_ok (or __free_frozen_pages) looks like this:

(quoted diff) +6 -0
+static bool compound_has_hwpoisoned(struct page *page, unsigned int order)
 {
+       if (order == 0 || !PageCompound(page))
+               return false;
+
+       return folio_test_has_hwpoisoned(page_folio(page));
+}

static void __free_pages_ok(struct page *page, unsigned int order,

 {
        unsigned long pfn = page_to_pfn(page);
        struct zone *zone = page_zone(page);
(quoted diff) +10 -2
+       bool should_fhh = compound_has_hwpoisoned(page, order);
 
-       if (free_pages_prepare(page, order))
-               free_one_page(zone, page, pfn, order, fpi_flags);
+       if (!free_pages_prepare(page, order))
+               return;
+
+       if (should_fhh) {
+               free_has_hwpoisoned(page, order, fpi_flags);
+               return;
+       }
+
+       free_one_page(zone, page, pfn, order, fpi_flags);
 }
Quoted 3 lines

Under this idea, free_has_hwpoisoned() doesn't need prepare_compound_page_to_free() anymore. It can just directly call free_one_page() when it selects the right order.

Right.

Quoted 3 lines

The only imperfect part is compound_has_hwpoisoned(), that we need to make a call to whether free_has_hwpoisoned() or not ealier than free_pages_prepare().

Right.

Quoted 6 lines

free_pages_prepare() clears PAGE_FLAGS_SECOND
flags on the first tail page of a compound, which clears PG_has_hwpoisoned. On the other hand, we can't easily exclude PG_has_hwpoisoned from PAGE_FLAGS_SECOND. Because PG_has_hwpoisoned == PG_active, free_page_is_bad() will confuse and complaint that the first tail page is still active.

Adding a comment like this wouldn't hurt:

/*
* Should be called before decomposing compound page in
* free_pages_prepare().
*/
bool should_fhh = compound_has_hwpoisoned(page, order);

Quoted 19 lines

I have implemented a working prototype and tested just fine. I can send it out as v3 after clean it up.

Quoted 7 lines
Quoted 7 lines
(quoted diff) +2 -0
+static void prep_compound_page_to_free(struct page *head, unsigned int order,
+                                      unsigned long flags, struct

alloc_tag *tag)

(quoted diff) +4 -0
+{
+       head->flags.f = flags & (~PAGE_FLAGS_CHECK_AT_FREE);
+       head->mapping = NULL;
+       head->private = 0;
Quoted 8 lines

Review

Quoted 6 lines
(quoted diff) +6 -0
+
+       clear_compound_head(head);
+       if (order)
+               prep_compound_page(head, order);
+
+       copy_alloc_tag(head, tag);

Do we need a similar thing for memcg? Probably not, since it should have

Quoted 7 lines

Yes, I think we don't need to do anything for memgc, it is already uncharged in __folio_put().

Quoted 1 line

been uncharged before freeing (as long as we're not using it for kmem pages)

You mean the folio was charged to kernel memory, right? From my reading of uncharge_folio(), it covers the kmem case.

Yes but I think not all kmem pages go through uncharge_folio() given free_pages_prepare() handles uncharging kmem pages.

Quoted 53 lines
Quoted 45 lines

Perhaps a comment on why we don't need to split memcg will be enough.

Do we need a similar thing for page_owner? I think yes, although it won't crash or cause a warning, it will be inconsistent if we split page_owner in split_page()/__split_unmapped_folio()/etc but not in prep_compound_page_to_free().

Quoted 38 lines

+}

I tested now the WARNING from include/linux/alloc_tag.h:164 is gone for both 2M and 1G pages. BTW we also need to copy_alloc_tag() for HWPoison pages before pgalloc_tag_sub().

Quoted 31 lines
Quoted 23 lines
Quoted 14 lines
Quoted 14 lines
(quoted diff) +12 -0
+             total_freed += nr_pages;
+             curr = PageHWPoison(next) ? next + 1 : next;
+     }
+
+     pr_info("Excluded %lu hwpoison pages from folio\n", total_hwp);
+     pr_info("Freed %#lx pages from folio\n", total_freed);
+}
+
 void free_frozen_pages(struct page *page, unsigned int order)
 {
+     struct folio *folio = page_folio(page);
+
+     if (order > 0 && unlikely(folio_test_has_hwpoisoned(folio))) {
+             folio_clear_has_hwpoisoned(folio);
Quoted 8 lines

Review

Quoted 4 lines
(quoted diff) +4 -0
+             free_has_hwpoison_pages(page, order);
+             return;
+     }
+

It feels like it's a bit random place to do has_hwpoisoned check. Can we move this to free_pages_prepare() where we have some sanity checks (and also order-0 hwpoison page handling)?

Quoted 3 lines

While free_pages_prepare() seems to be a better place to do the has_hwpoisoned check, it is not a good place to do free_has_hwpoison_pages().

Why is it not a good place for free_has_hwpoison_pages()?

Callers of free_pages_prepare() are supposed to avoid freeing it back to the buddy or using the page when it returns false.

Quoted 7 lines
Quoted 3 lines

What I mean is, callers of free_pages_prepare() wouldn't know from the single false return value whether 1) they should completely bail out or 2) they should retry with free_has_hwpoison_pages.

I was thinking that once free_pages_prepare() returns false, it already has done all the work to isolate HWPoison pages and freed healthy portions, so the caller doesn't have to do anything and can bail out completely.

Quoted 5 lines

Does this change the meaning of free_pages_prepare()'s return value? My first take is it returns if the preparation+check works are good.

I don't know if doing the freeing work in free_pages_prepare() is a good idea.

As long as it's documented in a comment, why not.

It doesn't make callers' code less readable. The semantic is the same; callers should free the page only if free_pages_prepare() returns true.

They don't have to care why they should not free it. (either it should not be freed back to buddy because it's bad page,
or it had hwpoison page(s) and healthy portions are already freed)

Quoted 8 lines

But if it is fine, we can actually hide the ugly compound_has_hwpoisoned() entirely inside free_pages_prepare():
- true means preparation + check work are all good but caller needs to free prepared+checked pages itself
- false means one of the two:
- some check failed and it is impossible to safely free pages for callers
- this is a compound page with some HWPoison pages, and healthy pages are already freed safely

Yes, something like this could be documented in free_pages_prepare().

Quoted 1 line

Doesn't seem very clean with a boolean return type...

The return type could be an enum type, but that wouldn't affect the caller's behavior (bailing out) anyway?

No strong opinion from me on the type.

Signature · 2 lines
Cheers,
Harry / Hyeonggon
author
#10 lore raw

Re: [PATCH v2 2/3] mm/page_alloc: only free healthy pages in high-order HWPoison folio

Quoted 112 lines · Harry Yoo
Quoted 26 lines · Jiaqi Yan
Quoted 25 lines · Harry Yoo
Quoted 10 lines · Jiaqi Yan
Quoted 6 lines · Harry Yoo
Quoted 2 lines · Jiaqi Yan
Quoted 1 line · Harry Yoo

On Fri, Dec 19, 2025 at 06:33:45PM +0000, Jiaqi Yan wrote:

In general, I think they're not designed to handle cases where the allocation order and the free order differ (unless we split metadata like __split_unmapped_folio() does).

I believe the proper way to fix this is to do something similar to pgalloc_tag_split(), used by __split_unmapped_folio().

Yeah, that will work.

The difference is that pgalloc_tag_split(), split_page_owner(), and split_page_memcg() only support splitting the whole page into many (> 1) smaller pieces, but we're trying to create only one smaller page from the original page.

Quoted 6 lines

When we split a
new block from the original folio, we create a compound page from the block (just the way prep_compound_page_to_free does) and link the alloc tag of the original head page to the head of the new compound page.

Quoted 6 lines
Quoted 2 lines
Quoted 2 lines

Something like copy_alloc_tag (to be added in v3) below to demo my idea, assuming tag=pgalloc_tag_get(original head page):

Actually I had another idea for page metadata like page alloc tag and page owner - how about we do the special handling AFTER free_pages_prepare()?

Sounds good to me.

Quoted 7 lines

Looking at free_pages_prepare, it actually breaks down compound page (if it is compound, using free_tail_page_prepare), adjust page flags, and deals page metadata:

for (i = 1; i < (1 << order); i++) {
    if (compound)
        bad += free_tail_page_prepare(page, page + i);
Quoted 8 lines

...
page->flags.f &= ~PAGE_FLAGS_CHECK_AT_PREP;
reset_page_owner(page, order);
page_table_check_free(page, order);
pgalloc_tag_sub(page, 1 << order);

Even the original compound page has some HWPoison pages, these operations should be done in the same way when no HWPoison page exist.

and we're not losing hwpoison flag after free_pages_prepare()... that actually sounds fine.

Quoted 2 lines

The compound structure will be gone after free, the alloc tag will be reduced after free, the page owner will be reset after free.

Right.

Quoted 5 lines

So instead of splitting these stuff into temporary compound pages, why not getting rid of them in the first place with free_pages_prepare()?

This is unique to the freeing path, so we don't need to mimic everything __split_unmapped_folio() does.

Not 100% sure how it'd work in the memdesc world,
but for today, right.

Quoted 30 lines

At high level, applying the idea to __free_pages_ok (or __free_frozen_pages) looks like this:

(quoted diff) +6 -0
+static bool compound_has_hwpoisoned(struct page *page, unsigned int order)
 {
+       if (order == 0 || !PageCompound(page))
+               return false;
+
+       return folio_test_has_hwpoisoned(page_folio(page));
+}

static void __free_pages_ok(struct page *page, unsigned int order,

 {
        unsigned long pfn = page_to_pfn(page);
        struct zone *zone = page_zone(page);
(quoted diff) +10 -2
+       bool should_fhh = compound_has_hwpoisoned(page, order);
 
-       if (free_pages_prepare(page, order))
-               free_one_page(zone, page, pfn, order, fpi_flags);
+       if (!free_pages_prepare(page, order))
+               return;
+
+       if (should_fhh) {
+               free_has_hwpoisoned(page, order, fpi_flags);
+               return;
+       }
+
+       free_one_page(zone, page, pfn, order, fpi_flags);
 }
Quoted 3 lines

Under this idea, free_has_hwpoisoned() doesn't need prepare_compound_page_to_free() anymore. It can just directly call free_one_page() when it selects the right order.

Right.

Quoted 3 lines

The only imperfect part is compound_has_hwpoisoned(), that we need to make a call to whether free_has_hwpoisoned() or not ealier than free_pages_prepare().

Right.

Quoted 8 lines
Quoted 6 lines

free_pages_prepare() clears PAGE_FLAGS_SECOND
flags on the first tail page of a compound, which clears PG_has_hwpoisoned. On the other hand, we can't easily exclude PG_has_hwpoisoned from PAGE_FLAGS_SECOND. Because PG_has_hwpoisoned == PG_active, free_page_is_bad() will confuse and complaint that the first tail page is still active.

Adding a comment like this wouldn't hurt:

ack.

Quoted 29 lines

/*
* Should be called before decomposing compound page in
* free_pages_prepare().
*/
bool should_fhh = compound_has_hwpoisoned(page, order);

Quoted 22 lines

I have implemented a working prototype and tested just fine. I can send it out as v3 after clean it up.

Quoted 7 lines
Quoted 7 lines
(quoted diff) +2 -0
+static void prep_compound_page_to_free(struct page *head, unsigned int order,
+                                      unsigned long flags, struct

alloc_tag *tag)

(quoted diff) +4 -0
+{
+       head->flags.f = flags & (~PAGE_FLAGS_CHECK_AT_FREE);
+       head->mapping = NULL;
+       head->private = 0;
Quoted 8 lines

Review

Quoted 6 lines
(quoted diff) +6 -0
+
+       clear_compound_head(head);
+       if (order)
+               prep_compound_page(head, order);
+
+       copy_alloc_tag(head, tag);

Do we need a similar thing for memcg? Probably not, since it should have

Yes, I think we don't need to do anything for memgc, it is already uncharged in __folio_put().

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

been uncharged before freeing (as long as we're not using it for kmem pages)

You mean the folio was charged to kernel memory, right? From my reading of uncharge_folio(), it covers the kmem case.

Yes but I think not all kmem pages go through uncharge_folio() given free_pages_prepare() handles uncharging kmem pages.

I see. Then doing free_has_hwpoisoned() *after* free_pages_prepare() also address this concern, right?

Quoted 78 lines
Quoted 54 lines
Quoted 46 lines

Perhaps a comment on why we don't need to split memcg will be enough.

Do we need a similar thing for page_owner? I think yes, although it won't crash or cause a warning, it will be inconsistent if we split page_owner in split_page()/__split_unmapped_folio()/etc but not in prep_compound_page_to_free().

Quoted 38 lines

+}

I tested now the WARNING from include/linux/alloc_tag.h:164 is gone for both 2M and 1G pages. BTW we also need to copy_alloc_tag() for HWPoison pages before pgalloc_tag_sub().

Quoted 31 lines
Quoted 23 lines
Quoted 14 lines
Quoted 14 lines
(quoted diff) +12 -0
+             total_freed += nr_pages;
+             curr = PageHWPoison(next) ? next + 1 : next;
+     }
+
+     pr_info("Excluded %lu hwpoison pages from folio\n", total_hwp);
+     pr_info("Freed %#lx pages from folio\n", total_freed);
+}
+
 void free_frozen_pages(struct page *page, unsigned int order)
 {
+     struct folio *folio = page_folio(page);
+
+     if (order > 0 && unlikely(folio_test_has_hwpoisoned(folio))) {
+             folio_clear_has_hwpoisoned(folio);
Quoted 8 lines

Review

Quoted 4 lines
(quoted diff) +4 -0
+             free_has_hwpoison_pages(page, order);
+             return;
+     }
+

It feels like it's a bit random place to do has_hwpoisoned check. Can we move this to free_pages_prepare() where we have some sanity checks (and also order-0 hwpoison page handling)?

Quoted 3 lines

While free_pages_prepare() seems to be a better place to do the has_hwpoisoned check, it is not a good place to do free_has_hwpoison_pages().

Why is it not a good place for free_has_hwpoison_pages()?

Callers of free_pages_prepare() are supposed to avoid freeing it back to the buddy or using the page when it returns false.

Quoted 7 lines
Quoted 3 lines

What I mean is, callers of free_pages_prepare() wouldn't know from the single false return value whether 1) they should completely bail out or 2) they should retry with free_has_hwpoison_pages.

I was thinking that once free_pages_prepare() returns false, it already has done all the work to isolate HWPoison pages and freed healthy portions, so the caller doesn't have to do anything and can bail out completely.

Quoted 5 lines

Does this change the meaning of free_pages_prepare()'s return value? My first take is it returns if the preparation+check works are good.

I don't know if doing the freeing work in free_pages_prepare() is a good idea.

As long as it's documented in a comment, why not.

It doesn't make callers' code less readable. The semantic is the same; callers should free the page only if free_pages_prepare() returns true.

They don't have to care why they should not free it. (either it should not be freed back to buddy because it's bad page,
or it had hwpoison page(s) and healthy portions are already freed)

Quoted 8 lines

But if it is fine, we can actually hide the ugly compound_has_hwpoisoned() entirely inside free_pages_prepare():
- true means preparation + check work are all good but caller needs to free prepared+checked pages itself
- false means one of the two:
- some check failed and it is impossible to safely free pages for callers
- this is a compound page with some HWPoison pages, and healthy pages are already freed safely

Quoted 8 lines

Yes, something like this could be documented in free_pages_prepare().

Quoted 1 line

Doesn't seem very clean with a boolean return type...

The return type could be an enum type, but that wouldn't affect the caller's behavior (bailing out) anyway?

No strong opinion from me on the type.

I just noticed free_pages_prepare() does have an external caller compaction_free(), so I want to be cautious about adding new freeing behavoir to it.

Maybe I can wrap compound_has_hwpoisoned(), free_pages_prepare(), and free_has_hwpoisoned() into free_pages_prepare_has_hwpoisoned() and make it private to page_alloc.c.

Let me put this together in V3. I also want to correct my previous latency measurement. Both the compared group and baseline take less time (2ms vs 0.066ms), but it is not ~2x but ~30x.

Quoted 4 lines

--
Cheers,
Harry / Hyeonggon

maintainer
#11 lore raw

Re: [PATCH v2 2/3] mm/page_alloc: only free healthy pages in high-order HWPoison folio

In reply to Jiaqi Yan
Quoted 30 lines · Jiaqi Yan

At the end of dissolve_free_hugetlb_folio that a free HugeTLB folio becomes non-HugeTLB, it is released to buddy allocator as a high-order folio, e.g. a folio that contains 262144 pages if the folio was a 1G HugeTLB hugepage.

This is problematic if the HugeTLB hugepage contained HWPoison subpages. In that case, since buddy allocator does not check HWPoison for non-zero-order folio, the raw HWPoison page can be given out with its buddy page and be re-used by either kernel or userspace.

Memory failure recovery (MFR) in kernel does attempt to take raw HWPoison page off buddy allocator after dissolve_free_hugetlb_folio. However, there is always a time window between dissolve_free_hugetlb_folio frees a HWPoison high-order folio to buddy allocator and MFR takes HWPoison raw page off buddy allocator.

One obvious way to avoid this problem is to add page sanity checks in page allocate or free path. However, it is against the past efforts to reduce sanity check overhead [1,2,3].

Introduce free_has_hwpoison_pages to only free the healthy pages and excludes the HWPoison ones in the high-order folio. The idea is to iterate through the sub-pages of the folio to identify contiguous ranges of healthy pages. Instead of freeing pages one by one, decompose healthy ranges into the largest possible blocks. Each block meets the requirements to be freed to buddy allocator (__free_frozen_pages).

Quoted 7 lines

free_has_hwpoison_pages has linear time complexity O(N) wrt the number of pages in the folio. While the power-of-two decomposition ensures that the number of calls to the buddy allocator is logarithmic for each contiguous healthy range, the mandatory linear scan of pages to identify PageHWPoison defines the overall time complexity.

Thanks for your patch.

Quoted 51 lines

[1] https://lore.kernel.org/linux-mm/[email protected]/
[2] https://lore.kernel.org/linux-mm/[email protected]/
[3] https://lore.kernel.org/all/[email protected]

---
 mm/page_alloc.c | 101 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 101 insertions(+)
mm/page_alloc.c +34 -0
index 822e05f1a9646..20c8862ce594e 100644
@@ -2976,8 +2976,109 @@ static void __free_frozen_pages(struct page *page, unsigned int order,
 	}
 }
 
+static void prepare_compound_page_to_free(struct page *new_head,
+					  unsigned int order,
+					  unsigned long flags)
+{
+	new_head->flags.f = flags & (~PAGE_FLAGS_CHECK_AT_FREE);
+	new_head->mapping = NULL;
+	new_head->private = 0;
+
+	clear_compound_head(new_head);
+	if (order)
+		prep_compound_page(new_head, order);
+}
+
+/*
+ * Given a range of pages physically contiguous physical, efficiently
+ * free them in blocks that meet __free_frozen_pages's requirements.
+ */
+static void free_contiguous_pages(struct page *curr, struct page *next,
+				  unsigned long flags)
+{
+	unsigned int order;
+	unsigned int align_order;
+	unsigned int size_order;
+	unsigned long pfn;
+	unsigned long end_pfn = page_to_pfn(next);
+	unsigned long remaining;
+
+	/*
+	 * This decomposition algorithm at every iteration chooses the
+	 * order to be the minimum of two constraints:
+	 * - Alignment: the largest power-of-two that divides current pfn.
+	 * - Size: the largest power-of-two that fits in the
+	 *   current remaining number of pages.
+	 */

mm/page_alloc.c static void __free_frozen_pages(struct page *page, unsigned int order, · line 3022

Quoted 10 lines
mm/page_alloc.c +10 -0
+	while (curr < next) {
+		pfn = page_to_pfn(curr);
+		remaining = end_pfn - pfn;
+
+		align_order = ffs(pfn) - 1;
+		size_order = fls_long(remaining) - 1;
+		order = min(align_order, size_order);
+
+		prepare_compound_page_to_free(curr, order, flags);> +		__free_frozen_pages(curr, order, FPI_NONE);
+		curr += (1UL << order);

For hwpoisoned pages, nothing is done for them. I think we should run at least some portion of code snippet from free_pages_prepare():

if (unlikely(PageHWPoison(page)) && !order) {
		/* Do not let hwpoison pages hit pcplists/buddy */
		reset_page_owner(page, order);
		page_table_check_free(page, order);
		pgalloc_tag_sub(page, 1 << order);

		/*
		 * The page is isolated and accounted for.
		 * Mark the codetag as empty to avoid accounting error
		 * when the page is freed by unpoison_memory().
		 */
		clear_page_tag_ref(page);
		return false;
	}
Quoted 26 lines
mm/page_alloc.c +26 -0
+	}
+
+	VM_WARN_ON(curr != next);
+}
+
+/*
+ * Given a high-order compound page containing certain number of HWPoison
+ * pages, free only the healthy ones to buddy allocator.
+ *
+ * It calls __free_frozen_pages O(2^order) times and cause nontrivial
+ * overhead. So only use this when compound page really contains HWPoison.
+ *
+ * This implementation doesn't work in memdesc world.
+ */
+static void free_has_hwpoison_pages(struct page *page, unsigned int order)
+{
+	struct page *curr = page;
+	struct page *end = page + (1 << order);
+	struct page *next;
+	unsigned long flags = page->flags.f;
+	unsigned long nr_pages;
+	unsigned long total_freed = 0;
+	unsigned long total_hwp = 0;
+
+	VM_WARN_ON(flags & PAGE_FLAGS_CHECK_AT_FREE);
+

mm/page_alloc.c static void __free_frozen_pages(struct page *page, unsigned int order, · line 3059

Quoted 10 lines
mm/page_alloc.c +10 -0
+	while (curr < end) {
+		next = curr;
+		nr_pages = 0;
+
+		while (next < end && !PageHWPoison(next)) {
+			++next;
+			++nr_pages;
+		}
+
+		if (PageHWPoison(next))

Would it be possible next points to end? In that case, irrelevant even nonexistent page will be accessed ?

Thanks.
.

author
#12 lore raw

Re: [PATCH v2 2/3] mm/page_alloc: only free healthy pages in high-order HWPoison folio

Quoted 40 lines · Miaohe Lin
Quoted 30 lines · Jiaqi Yan

At the end of dissolve_free_hugetlb_folio that a free HugeTLB folio becomes non-HugeTLB, it is released to buddy allocator as a high-order folio, e.g. a folio that contains 262144 pages if the folio was a 1G HugeTLB hugepage.

This is problematic if the HugeTLB hugepage contained HWPoison subpages. In that case, since buddy allocator does not check HWPoison for non-zero-order folio, the raw HWPoison page can be given out with its buddy page and be re-used by either kernel or userspace.

Memory failure recovery (MFR) in kernel does attempt to take raw HWPoison page off buddy allocator after dissolve_free_hugetlb_folio. However, there is always a time window between dissolve_free_hugetlb_folio frees a HWPoison high-order folio to buddy allocator and MFR takes HWPoison raw page off buddy allocator.

One obvious way to avoid this problem is to add page sanity checks in page allocate or free path. However, it is against the past efforts to reduce sanity check overhead [1,2,3].

Introduce free_has_hwpoison_pages to only free the healthy pages and excludes the HWPoison ones in the high-order folio. The idea is to iterate through the sub-pages of the folio to identify contiguous ranges of healthy pages. Instead of freeing pages one by one, decompose healthy ranges into the largest possible blocks. Each block meets the requirements to be freed to buddy allocator (__free_frozen_pages).

Quoted 7 lines

free_has_hwpoison_pages has linear time complexity O(N) wrt the number of pages in the folio. While the power-of-two decomposition ensures that the number of calls to the buddy allocator is logarithmic for each contiguous healthy range, the mandatory linear scan of pages to identify PageHWPoison defines the overall time complexity.

Quoted 1 line

Thanks for your patch.

Thanks for your review/comments!

Quoted 58 lines
Quoted 58 lines

[1] https://lore.kernel.org/linux-mm/[email protected]/
[2] https://lore.kernel.org/linux-mm/[email protected]/
[3] https://lore.kernel.org/all/[email protected]

---
 mm/page_alloc.c | 101 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 101 insertions(+)
mm/page_alloc.c +40 -0
index 822e05f1a9646..20c8862ce594e 100644
@@ -2976,8 +2976,109 @@ static void __free_frozen_pages(struct page *page, unsigned int order,
      }
 }
 
+static void prepare_compound_page_to_free(struct page *new_head,
+                                       unsigned int order,
+                                       unsigned long flags)
+{
+     new_head->flags.f = flags & (~PAGE_FLAGS_CHECK_AT_FREE);
+     new_head->mapping = NULL;
+     new_head->private = 0;
+
+     clear_compound_head(new_head);
+     if (order)
+             prep_compound_page(new_head, order);
+}
+
+/*
+ * Given a range of pages physically contiguous physical, efficiently
+ * free them in blocks that meet __free_frozen_pages's requirements.
+ */
+static void free_contiguous_pages(struct page *curr, struct page *next,
+                               unsigned long flags)
+{
+     unsigned int order;
+     unsigned int align_order;
+     unsigned int size_order;
+     unsigned long pfn;
+     unsigned long end_pfn = page_to_pfn(next);
+     unsigned long remaining;
+
+     /*
+      * This decomposition algorithm at every iteration chooses the
+      * order to be the minimum of two constraints:
+      * - Alignment: the largest power-of-two that divides current pfn.
+      * - Size: the largest power-of-two that fits in the
+      *   current remaining number of pages.
+      */
+     while (curr < next) {
+             pfn = page_to_pfn(curr);
+             remaining = end_pfn - pfn;
+
+             align_order = ffs(pfn) - 1;
+             size_order = fls_long(remaining) - 1;
Quoted 8 lines

mm/page_alloc.c static void __free_frozen_pages(struct page *page, unsigned int order, · line 3023

Quoted 5 lines
mm/page_alloc.c +5 -0
+             order = min(align_order, size_order);
+
+             prepare_compound_page_to_free(curr, order, flags);
+             __free_frozen_pages(curr, order, FPI_NONE);
+             curr += (1UL << order);

For hwpoisoned pages, nothing is done for them. I think we should run at least some portion of code snippet from free_pages_prepare():

Agreed, will add in v3.

Quoted 46 lines
if (unlikely(PageHWPoison(page)) && !order) {
                /* Do not let hwpoison pages hit pcplists/buddy */
                reset_page_owner(page, order);
                page_table_check_free(page, order);
                pgalloc_tag_sub(page, 1 << order);

                /*
                 * The page is isolated and accounted for.
                 * Mark the codetag as empty to avoid accounting error
                 * when the page is freed by unpoison_memory().
                 */
                clear_page_tag_ref(page);
                return false;
        }
Quoted 30 lines
mm/page_alloc.c +30 -0
+     }
+
+     VM_WARN_ON(curr != next);
+}
+
+/*
+ * Given a high-order compound page containing certain number of HWPoison
+ * pages, free only the healthy ones to buddy allocator.
+ *
+ * It calls __free_frozen_pages O(2^order) times and cause nontrivial
+ * overhead. So only use this when compound page really contains HWPoison.
+ *
+ * This implementation doesn't work in memdesc world.
+ */
+static void free_has_hwpoison_pages(struct page *page, unsigned int order)
+{
+     struct page *curr = page;
+     struct page *end = page + (1 << order);
+     struct page *next;
+     unsigned long flags = page->flags.f;
+     unsigned long nr_pages;
+     unsigned long total_freed = 0;
+     unsigned long total_hwp = 0;
+
+     VM_WARN_ON(flags & PAGE_FLAGS_CHECK_AT_FREE);
+
+     while (curr < end) {
+             next = curr;
+             nr_pages = 0;
+
Quoted 8 lines

mm/page_alloc.c static void __free_frozen_pages(struct page *page, unsigned int order, · line 3059

Quoted 6 lines
mm/page_alloc.c +6 -0
+             while (next < end && !PageHWPoison(next)) {
+                     ++next;
+                     ++nr_pages;
+             }
+
+             if (PageHWPoison(next))

Would it be possible next points to end? In that case, irrelevant even nonexistent page will be accessed ?

Thanks for catching that. Let me avoid access end as a page at all, both here and in free_contiguous_pages.

Quoted 3 lines

Thanks.
.

author
#13 lore raw

PATCH v2 3/3 mm/memory-failure: simplify __page_handle_poison

In reply to Jiaqi Yan

Now that no HWPoison page will be given away to buddy allocator at the end of dissolve_free_hugetlb_folio, there is no need to drain_all_pages and take_page_off_buddy anymore, so remove them.

Also make __page_handle_poison return either 0 for success or negative for failure, following the convention for functions that perform an action.

---
 mm/memory-failure.c | 31 +++++--------------------------
 1 file changed, 5 insertions(+), 26 deletions(-)
mm/memory-failure.c +5 -26
index d204de6c9792a..54ea840ded162 100644
@@ -164,33 +164,12 @@ static DEFINE_MUTEX(pfn_space_lock);
 
 /*
  * Return values:
- *   1:   the page is dissolved (if needed) and taken off from buddy,
- *   0:   the page is dissolved (if needed) and not taken off from buddy,
+ *   = 0: the page is dissolved (if needed)
  *   < 0: failed to dissolve.
  */
 static int __page_handle_poison(struct page *page)
 {
-	int ret;
-
-	/*
-	 * zone_pcp_disable() can't be used here. It will
-	 * hold pcp_batch_high_lock and dissolve_free_hugetlb_folio() might hold
-	 * cpu_hotplug_lock via static_key_slow_dec() when hugetlb vmemmap
-	 * optimization is enabled. This will break current lock dependency
-	 * chain and leads to deadlock.
-	 * Disabling pcp before dissolving the page was a deterministic
-	 * approach because we made sure that those pages cannot end up in any
-	 * PCP list. Draining PCP lists expels those pages to the buddy system,
-	 * but nothing guarantees that those pages do not get back to a PCP
-	 * queue if we need to refill those.
-	 */
-	ret = dissolve_free_hugetlb_folio(page_folio(page));
-	if (!ret) {
-		drain_all_pages(page_zone(page));
-		ret = take_page_off_buddy(page);
-	}
-
-	return ret;
+	return dissolve_free_hugetlb_folio(page_folio(page));
 }
 
 static bool page_handle_poison(struct page *page, bool hugepage_or_freepage, bool release)
@@ -200,7 +179,7 @@ static bool page_handle_poison(struct page *page, bool hugepage_or_freepage, boo
 		 * Doing this check for free pages is also fine since
 		 * dissolve_free_hugetlb_folio() returns 0 for non-hugetlb folios as well.
 		 */
-		if (__page_handle_poison(page) <= 0)
+		if (__page_handle_poison(page) < 0)
 			/*
 			 * We could fail to take off the target page from buddy
 			 * for example due to racy page allocation, but that's
@@ -1174,7 +1153,7 @@ static int me_huge_page(struct page_state *ps, struct page *p)
 		 * subpages.
 		 */
 		folio_put(folio);
-		if (__page_handle_poison(p) > 0) {
+		if (!__page_handle_poison(p)) {
 			page_ref_inc(p);
 			res = MF_RECOVERED;
 		} else {
@@ -2067,7 +2046,7 @@ static int try_memory_failure_hugetlb(unsigned long pfn, int flags, int *hugetlb
 	 */
 	if (res == 0) {
 		folio_unlock(folio);
-		if (__page_handle_poison(p) > 0) {
+		if (!__page_handle_poison(p)) {
 			page_ref_inc(p);
 			res = MF_RECOVERED;
 		} else {
Signature · 1 line
2.52.0.322.g1dd061c0dc-goog
#14 lore raw

Re: [PATCH v2 3/3] mm/memory-failure: simplify __page_handle_poison

Quoted 3 lines · Jiaqi Yan

Now that no HWPoison page will be given away to buddy allocator at the end of dissolve_free_hugetlb_folio, there is no need to drain_all_pages and take_page_off_buddy anymore, so remove them.

What if we discover a hardware error in a page which is currently in the buddy system? Why don't we need to remove that page from the buddy system?

author
#15 lore raw

Re: [PATCH v2 3/3] mm/memory-failure: simplify __page_handle_poison

Quoted 9 lines · Matthew Wilcox
Quoted 3 lines · Jiaqi Yan

Now that no HWPoison page will be given away to buddy allocator at the end of dissolve_free_hugetlb_folio, there is no need to drain_all_pages and take_page_off_buddy anymore, so remove them.

What if we discover a hardware error in a page which is currently in the buddy system? Why don't we need to remove that page from the buddy system?

Thanks for your comment.

memory_failure() explicitly handles is_free_buddy_page() case and is removing from buddy allocator with take_page_off_buddy() directly.

However, when soft_offline_page() handles free page with page_handle_poison(), removing from buddy allocator is missing with this patch.

I will fix this in v3. One way is to split hugepage_or_freepage and only take_page_off_buddy() for non-huge free page.

author
#16 lore raw
In reply to Jiaqi Yan
Quoted 48 lines · Jiaqi Yan

At the end of dissolve_free_hugetlb_folio that a free HugeTLB folio becomes non-HugeTLB, it is released to buddy allocator as a high-order folio, e.g. a folio that contains 262144 pages if the folio was a 1G HugeTLB hugepage.

This is problematic if the HugeTLB hugepage contained HWPoison subpages. In that case, since buddy allocator does not check HWPoison for non-zero-order folio, the raw HWPoison page can be given out with its buddy page and be re-used by either kernel or userspace.

Memory failure recovery (MFR) in kernel does attempt to take raw HWPoison page off buddy allocator after dissolve_free_hugetlb_folio. However, there is always a time window between dissolve_free_hugetlb_folio frees a HWPoison high-order folio to buddy allocator and MFR takes HWPoison raw page off buddy allocator.

One obvious way to avoid this problem is to add page sanity checks in page allocate or free path. However, it is against the past efforts to reduce sanity check overhead [1,2,3].

Introduce free_has_hwpoison_pages to only free the healthy pages and excludes the HWPoison ones in the high-order folio. The idea is to iterate through the sub-pages of the folio to identify contiguous ranges of healthy pages. Instead of freeing pages one by one, decompose healthy ranges into the largest possible blocks. Each block meets the requirements to be freed to buddy allocator by calling __free_frozen_pages directly.

free_has_hwpoison_pages has linear time complexity O(N) wrt the number of pages in the folio. While the power-of-two decomposition ensures that the number of calls to the buddy allocator is logarithmic for each contiguous healthy range, the mandatory linear scan of pages to identify PageHWPoison defines the overall time complexity.

I tested with some test-only code [4] and hugetlb-mfr [5], by checking the status of pcplist and freelist immediately after dissolve_free_hugetlb_folio a free hugetlb page that contains 3 HWPoison raw pages:

* HWPoison pages are excluded by free_has_hwpoison_pages.

* Some healthy pages can be in zone->per_cpu_pageset (pcplist) because pcp_count is not high enough.

Quoted 6 lines

* Many healthy pages are already in some order's
zone->free_area[order].free_list (freelist).

* In rare cases, some healthy pages are in neither pcplist nor freelist. My best guest is they are allocated before the test checks.

Sorry, just realized changelog is missing. Appending it here:

Changelog

v1 [6] => v2:
- Total reimplementation based on discussions with Mathew Wilcox, Harry Hoo, Zi Yan etc.
- hugetlb_free_hwpoison_folio => free_has_hwpoison_pages.
- Utilize has_hwpoisoned flag to tell buddy allocator a high-order folio contains HWPoison.
- Simplify __page_handle_poison given that HWPoison page won't be freed within the high-order folio.

Quoted 6 lines

[1] https://lore.kernel.org/linux-mm/[email protected]/
[2] https://lore.kernel.org/linux-mm/[email protected]/
[3] https://lore.kernel.org/all/[email protected]
[4] https://drive.google.com/file/d/1CzJn1Cc4wCCm183Y77h244fyZIkTLzCt/view?usp=sharing
[5] https://lore.kernel.org/linux-mm/[email protected]

[6] https://lore.kernel.org/linux-mm/[email protected]

Quoted 14 lines

Jiaqi Yan (3):
mm/memory-failure: set has_hwpoisoned flags on HugeTLB folio mm/page_alloc: only free healthy pages in high-order HWPoison folio mm/memory-failure: simplify __page_handle_poison

 include/linux/page-flags.h |   2 +-
 mm/memory-failure.c        |  32 +++---------
 mm/page_alloc.c            | 101 +++++++++++++++++++++++++++++++++++++
 3 files changed, 108 insertions(+), 27 deletions(-)

--
2.52.0.322.g1dd061c0dc-goog