Only free healthy pages in high-order has_hwpoisoned folio
v3 cover Only free healthy pages in high-order has_hwpoisoned 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_hwpoisoned() to "salvage" the healthy pages and excludes the HWPoison ones in the high-order folio. free_has_hwpoisoned() happens after free_pages_prepare(), which already deals with both decomposing the original compound page, updating page metadata like alloc tag and page owner. Its 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 is freed via free_one_page() directly.
free_has_hwpoisoned has linear time complexity 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 2M or 1G hugetlb page that contains 1~8 HWPoison raw pages:
- HWPoison pages are excluded by free_has_hwpoisoned().
- Some healthy pages can be in zone->per_cpu_pageset (pcplist) because pcp_count is not high enough. Many healthy pages are 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.
To illustrate the latency free_has_hwpoisoned() added to the freeing memory path, I tested its time cost with 8 HWPoison pages with instrument code in [4] for 20 sample runs:
- Has HWPoison path: mean=2.02ms, stdev=0.14ms
- No HWPoison path: mean=66us, stdev=6us
free_has_hwpoisoned() is around 30x the baseline. It is far from triggering soft lockup, and the cost is fair for handling exceptional hardware memory errors.
Given this nontrivial overhead, checking PG_has_hwpoisoned, doing normal free_pages_prepare(), and doing free_has_hwpoisoned() when necessary are wrapped in free_pages_prepare_has_hwpoisoned(), which replaces free_pages_prepare() calls in free_frozen_pages().
With free_has_hwpoisoned() ensuring HWPoison pages never made into buddy allocator, MFR don't need to take_page_off_buddy() anymore after disovling HWPoison hugepages. So refactor page_handle_poison to remove take_page_off_buddy() in case of hugepage, but still take_page_off_buddy() in case of free buddy page.
Based on commit ccd1cdca5cd4 ("Merge tag 'nfsd-6.19-1' of git://git.kernel.org/pub/scm/linux/kernel/git/cel/linux")
Changelog
v2 [7] -> v3:
- Address comments from Mathew Wilcox, Harry Hoo, Miaohe Lin.
- Let free_has_hwpoisoned() happen after free_pages_prepare(), which help to deal with decomposing the original compound page, and with page metadata like alloc tag and page owner.
- Tested with "page_owner=on" and CONFIG_MEM_ALLOC_PROFILING*=y.
- Wrap checking PG_has_hwpoisoned and free_has_hwpoisoned() into free_pages_prepare_has_hwpoisoned(), which replaces free_pages_prepare() calls in free_frozen_pages().
- Rename free_has_hwpoison_page() to free_has_hwpoisoned().
- Measure latency added by free_has_hwpoisoned().
- Ensure struct page *end is only used for pointer arithmetic, instead of accessed as page.
- Refactor page_handl_poison instead of just __page_handle_poison.
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 the HWPoison page(s) won't be freed within high-order folio.
[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]
[7] 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 has_hwpoisoned
folio
mm/memory-failure: refactor page_handle_poison()
include/linux/page-flags.h | 2 +- mm/memory-failure.c | 85 ++++++++++---------- mm/page_alloc.c | 157 ++++++++++++++++++++++++++++++++++++- 3 files changed, 197 insertions(+), 47 deletions(-)
Signature · 1 line
2.52.0.457.g6b5491de43-goog
PATCH v3 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
@@ -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
@@ -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.457.g6b5491de43-goog
Zi Yan
reviewerRe: [PATCH v3 1/3] mm/memory-failure: set has_hwpoisoned flags on HugeTLB folio
Quoted 36 lines · Jiaqi Yan
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_hwpoisonedflags 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.
Signed-off-by Jiaqi Yan <[email protected]>--- 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@@ -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 +0
-0
mm/memory-failure.c void folio_clear_hugetlb_hwpoison(struct folio *folio) · line 1957
Quoted 6 lines
mm/memory-failure.c +1
-0@@ -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); }
Should this patch go after Patch 2 where has_hwpoisoned folio handling code is added?
--
Best Regards,
Yan, Zi
Re: [PATCH v3 1/3] mm/memory-failure: set has_hwpoisoned flags on HugeTLB folio
Quoted 40 lines · Zi Yan
Quoted 37 lines · Jiaqi Yan
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_hwpoisonedflags 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.
Signed-off-by Jiaqi Yan <[email protected]>--- 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@@ -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 +0
-0@@ -1952,6 +1952,7 @@ void folio_clear_hugetlb_hwpoison(struct folio *folio)if (folio_test_hugetlb_vmemmap_optimized (folio))
Quoted 8 lines
mm/memory-failure.c void folio_clear_hugetlb_hwpoison(struct folio *folio) · line 1957
Quoted 5 lines
mm/memory-failure.c +1
-0return ;folio_clear_hwpoison (folio); +folio_set_has_hwpoisoned (folio);folio_free_raw_hwp (folio, true); }Should this patch go after Patch 2 where
has_hwpoisonedfolio handling code is added?
Hi Zi, thanks for the comment. Will reorder this one.
I initially thought this patch was for preparation. However, if I look at patch 2 differently: it enables page_alloc to handle PG_has_hwpoisoned folios generally, not only for HugeTLB, but also for split-failed THP compound pages. Then this one can be just to make HugeTLB start to use patch 2.
Quoted 4 lines
--
Best Regards,
Yan, Zi
PATCH v3 2/3 mm/page_alloc: only free healthy pages in high-order has_hwpoisoned folio
In reply to Jiaqi YanAt the end of dissolve_free_hugetlb_folio(), a free HugeTLB folio becomes non-HugeTLB, and 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_hwpoisoned() to only free the healthy pages and to exclude 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 having different orders. Every block meets the requirements to be freed via __free_one_page().
free_has_hwpoisoned() has linear time complexity 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. For a 1G hugepage having several HWPoison pages, free_has_hwpoisoned() takes around 2ms on average.
Since free_has_hwpoisoned() has nontrivial overhead, it is wrapped inside free_pages_prepare_has_hwpoisoned() and done only PG_has_hwpoisoned indicates HWPoison page exists and after free_pages_prepare() succeeded.
[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 | 157 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 154 insertions(+), 3 deletions(-)
mm/page_alloc.c +154 -3
@@ -215,6 +215,9 @@ gfp_t gfp_allowed_mask __read_mostly = GFP_BOOT_MASK;unsigned int pageblock_order__read_mostly ;#endif +static bool free_pages_prepare_has_hwpoisoned (struct page *page, +unsigned int order, +fpi_t fpi_flags);static void __free_pages_ok (struct page *page,unsigned int order,fpi_t fpi_flags);
@@ -1568,8 +1571,10 @@ static void __free_pages_ok(struct page *page, unsigned int order,unsigned long pfn =page_to_pfn (page);struct zone *zone =page_zone (page); -if (free_pages_prepare (page, order)) -free_one_page (zone, page, pfn, order, fpi_flags); +if (!free_pages_prepare_has_hwpoisoned (page, order, fpi_flags)) +return ; + +free_one_page (zone, page, pfn, order, fpi_flags); } void __meminit __free_pages_core(struct page *page, unsigned int order,
@@ -2923,6 +2928,152 @@ static bool free_frozen_page_commit(struct zone *zone,return ret; } +/* +* Given a range of physically contiguous pages, efficiently free them +* block by block. Block order is chosen to meet the PFN alignment +* requirement in __free_one_page(). +*/ +static void free_contiguous_pages (struct page *curr,unsigned long nr_pages, +fpi_t fpi_flags) +{ +unsigned int order; +unsigned int align_order; +unsigned int size_order; +unsigned long remaining; +unsigned long pfn =page_to_pfn (curr); +const unsigned long end_pfn = pfn + nr_pages; +struct zone *zone =page_zone (curr); + +/* +* 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 (pfn < end_pfn) { + remaining = end_pfn - pfn; + align_order =ffs (pfn) -1 ; + size_order =fls_long (remaining) -1 ; + order =min (align_order, size_order); + +free_one_page (zone, curr, pfn, order, fpi_flags); + curr += (1UL << order); + pfn += (1UL << order); + } + +VM_WARN_ON (pfn != end_pfn); +} + +/* +* Given a high-order compound page containing certain number of HWPoison +* pages, free only the healthy ones to buddy allocator. +* +* Pages must have passed free_pages_prepare(). Even if having HWPoison +* pages, breaking down compound page and updating metadata (e.g. page +* owner, alloc tag) can be done together during free_pages_prepare(), +* which simplifies the splitting here: unlike __split_unmapped_folio(), +* there is no need to turn split pages into a compound page or to carry +* metadata. +* +* It calls free_one_page O(2^order) times and cause nontrivial overhead. +* So only use this when the compound page really contains HWPoison. +* +* This implementation doesn't work in memdesc world. +*/ +static void free_has_hwpoisoned (struct page *page,unsigned int order, +fpi_t fpi_flags) +{ +struct page *curr = page; +struct page *next; +unsigned long nr_pages; +/* +* Don't assume end points to a valid page. It is only used +* here for pointer arithmetic. +*/ +struct page *end = page + (1 << order); +unsigned long total_freed =0 ; +unsigned long total_hwp =0 ; + +VM_WARN_ON (order ==0 ); +VM_WARN_ON (page->flags.f &PAGE_FLAGS_CHECK_AT_PREP ); + +while (curr < end) { + next = curr; + nr_pages =0 ; + +while (next < end && !PageHWPoison (next)) { + ++next; + ++nr_pages; + } + +if (next != end &&PageHWPoison (next)) { +clear_page_tag_ref (next); + ++total_hwp; + } + +free_contiguous_pages (curr, nr_pages, fpi_flags); + total_freed += nr_pages; +if (next == end) +break ; + + curr =PageHWPoison (next) ? next +1 : next; + } + +VM_WARN_ON (total_freed + total_hwp != (1 << order)); +pr_info ("Freed %#lx pages, excluded %lu hwpoison pages\n" , + total_freed, total_hwp); +} + +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)); +} + +/* +* Do free_has_hwpoisoned() when needed after free_pages_prepare(). +* Returns +* - true: free_pages_prepare() is good and caller can proceed freeing. +* - false: caller should not free pages for one of the two reasons: +* 1. free_pages_prepare() failed so it is not safe to proceed freeing. +* 2. this is a compound page having some HWPoison pages, and healthy +* pages are already safely freed. +*/ +static bool free_pages_prepare_has_hwpoisoned (struct page *page, +unsigned int order, +fpi_t fpi_flags) +{ +/* +* free_pages_prepare() clears PAGE_FLAGS_SECOND flags on the +* first tail page of a compound page, which clears PG_has_hwpoisoned. +* So this call must be before free_pages_prepare(). +* +* Note we can't 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. +*/ +bool should_fhh =compound_has_hwpoisoned (page, order); + +if (!free_pages_prepare (page, order)) +return false; + +/* +* After free_pages_prepare() breaks down compound page and deals +* with page metadata (e.g. page owner and page alloc tags), +* free_has_hwpoisoned() can directly use free_one_page() whenever +* it knows the appropriate orders of page blocks to free. +*/ +if (should_fhh) { +free_has_hwpoisoned (page, order, fpi_flags); +return false; + } + +return true; +} +/* * Free a pcp page */
@@ -2940,7 +3091,7 @@ static void __free_frozen_pages(struct page *page, unsigned int order,return ; } -if (!free_pages_prepare (page, order)) +if (!free_pages_prepare_has_hwpoisoned (page, order, fpi_flags))return ;/*
Signature · 1 line
2.52.0.457.g6b5491de43-goog
Re: [PATCH v3 2/3] mm/page_alloc: only free healthy pages in high-order has_hwpoisoned folio
Quoted 11 lines · Jiaqi Yan
At the end of
dissolve_free_hugetlb_folio(), a free HugeTLB folio becomes non-HugeTLB, and 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.
Quoted 6 lines
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 betweendissolve_free_hugetlb_folio()frees a HWPoison high-order folio to buddy allocator and MFR takes HWPoison raw page off buddy allocator.
I wonder if this is something we want to backport to -stable.
Quoted 41 lines
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_hwpoisoned()to only free the healthy pages and to exclude 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 having different orders. Every block meets the requirements to be freed via__free_one_page().
free_has_hwpoisoned()has linear time complexity 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 identifyPageHWPoison()defines the overall time complexity. For a 1G hugepage having several HWPoison pages,free_has_hwpoisoned()takes around 2ms on average.Since
free_has_hwpoisoned()has nontrivial overhead, it is wrapped insidefree_pages_prepare_has_hwpoisoned()and done onlyPG_has_hwpoisonedindicates HWPoison page exists and afterfree_pages_prepare()succeeded.[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]Signed-off-by Jiaqi Yan <[email protected]>--- mm/page_alloc.c | 157 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 154 insertions(+), 3 deletions(-)mm/page_alloc.c +0
-0
mm/page_alloc.c static bool free_frozen_page_commit(struct zone *zone, · line 2929
Quoted 2 lines
mm/page_alloc.c +0
-0@@ -2923,6 +2928,152 @@ static bool free_frozen_page_commit(struct zone *zone,return ret; }
From correctness point of view I think it looks good to me. Let's see what the page allocator folks say.
A few nits below.
mm/page_alloc.c static bool free_frozen_page_commit(struct zone *zone, · line 3031
Quoted 4 lines
mm/page_alloc.c +4
-0+static bool compound_has_hwpoisoned (struct page *page,unsigned int order) +{ +if (order ==0 || !PageCompound (page)) +return false;
nit: since order-0 compound page is not a thing, !PageCompound(page) check should cover order == 0 case.
Quoted 5 lines
mm/page_alloc.c +5
-0+return folio_test_has_hwpoisoned (page_folio (page)); +} + +/* +* Do free_has_hwpoisoned() when needed after free_pages_prepare().
mm/page_alloc.c static bool free_frozen_page_commit(struct zone *zone, · line 3047
Quoted 10 lines
mm/page_alloc.c +10
-0+* Returns +* - true: free_pages_prepare() is good and caller can proceed freeing. +* - false: caller should not free pages for one of the two reasons: +* 1. free_pages_prepare() failed so it is not safe to proceed freeing. +* 2. this is a compound page having some HWPoison pages, and healthy +* pages are already safely freed. +*/ +static bool free_pages_prepare_has_hwpoisoned (struct page *page, +unsigned int order, +fpi_t fpi_flags)
nit: Hope we'll come up with a better name than free_pages_prepare_has_poisoned(), but I don't have any better suggestion... :)
And I hope somebody familiar with compaction (as compaction_free() calls free_pages_prepare() and ignores its return value) could confirm that it is safe to do a compound_has_hwpoisoned() check and, when it returns true, call free_has_hwpoisoned() in free_pages_prepare(),
so that we won't need a separate function to do this.
Signature · 2 lines
Cheers, Harry / Hyeonggon
Zi Yan
reviewerRe: [PATCH v3 2/3] mm/page_alloc: only free healthy pages in high-order has_hwpoisoned folio
Quoted 98 lines · Harry Yoo
Quoted 11 lines · Jiaqi Yan
At the end of
dissolve_free_hugetlb_folio(), a free HugeTLB folio becomes non-HugeTLB, and 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.
Quoted 6 lines
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 betweendissolve_free_hugetlb_folio()frees a HWPoison high-order folio to buddy allocator and MFR takes HWPoison raw page off buddy allocator.I wonder if this is something we want to backport to -stable.
Quoted 41 lines
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_hwpoisoned()to only free the healthy pages and to exclude 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 having different orders. Every block meets the requirements to be freed via__free_one_page().
free_has_hwpoisoned()has linear time complexity 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 identifyPageHWPoison()defines the overall time complexity. For a 1G hugepage having several HWPoison pages,free_has_hwpoisoned()takes around 2ms on average.Since
free_has_hwpoisoned()has nontrivial overhead, it is wrapped insidefree_pages_prepare_has_hwpoisoned()and done onlyPG_has_hwpoisonedindicates HWPoison page exists and afterfree_pages_prepare()succeeded.[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]Signed-off-by Jiaqi Yan <[email protected]>--- mm/page_alloc.c | 157 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 154 insertions(+), 3 deletions(-)mm/page_alloc.c +0
-0mm/page_alloc.c static bool free_frozen_page_commit(struct zone *zone, · line 2929
Quoted 2 lines
mm/page_alloc.c +0
-0@@ -2923,6 +2928,152 @@ static bool free_frozen_page_commit(struct zone *zone,return ret; }From correctness point of view I think it looks good to me. Let's see what the page allocator folks say.
A few nits below.
mm/page_alloc.c static bool free_frozen_page_commit(struct zone *zone, · line 3031
Quoted 4 lines
mm/page_alloc.c +4
-0+static bool compound_has_hwpoisoned (struct page *page,unsigned int order) +{ +if (order ==0 || !PageCompound (page)) +return false;nit: since order-0 compound page is not a thing, !PageCompound(page) check should cover order == 0 case.
Quoted 5 lines
mm/page_alloc.c +5
-0+return folio_test_has_hwpoisoned (page_folio (page)); +} + +/* +* Do free_has_hwpoisoned() when needed after free_pages_prepare(). mm/page_alloc.c static bool free_frozen_page_commit(struct zone *zone, · line 3047
Quoted 10 lines
mm/page_alloc.c +10
-0+* Returns +* - true: free_pages_prepare() is good and caller can proceed freeing. +* - false: caller should not free pages for one of the two reasons: +* 1. free_pages_prepare() failed so it is not safe to proceed freeing. +* 2. this is a compound page having some HWPoison pages, and healthy +* pages are already safely freed. +*/ +static bool free_pages_prepare_has_hwpoisoned (struct page *page, +unsigned int order, +fpi_t fpi_flags)nit: Hope we'll come up with a better name than
free_pages_prepare_has_poisoned(), but I don't have any better suggestion... :)
Quoted 5 lines
And I hope somebody familiar with compaction (as
compaction_free()callsfree_pages_prepare()and ignores its return value) could confirm that it is safe to do acompound_has_hwpoisoned()check and, when it returns true, callfree_has_hwpoisoned()infree_pages_prepare(),
so that we won't need a separate function to do this.
I wrote the function. compact_control->freepages[] is an array of free pages isolated from buddy allocator as a temporary free page buffer. I wonder if compaction_free() can get a HWPoisoned folio or not, since to get to compaction_free(), the folio needs to be copied to a new folio. If the memory goes bad, folio_mc_copy() will return -EHWPOISON to abort the migration, but I do not see any code marking the folio has_hwpoisoned. When it reaches to compact_free(), there is no way to detect the error. I think the corrupted page will be put back to buddy and be marked as has_hwpoison after it is allocated and accessed by an application.
You might need to add has_hwpoisoned code in migration code path to catch folio_mc_copy() errors.
Best Regards,
Yan, Zi
Re: [PATCH v3 2/3] mm/page_alloc: only free healthy pages in high-order has_hwpoisoned folio
Quoted 101 lines · Zi Yan
Quoted 98 lines · Harry Yoo
Quoted 11 lines · Jiaqi Yan
At the end of
dissolve_free_hugetlb_folio(), a free HugeTLB folio becomes non-HugeTLB, and 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.
Quoted 6 lines
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 betweendissolve_free_hugetlb_folio()frees a HWPoison high-order folio to buddy allocator and MFR takes HWPoison raw page off buddy allocator.I wonder if this is something we want to backport to -stable.
Quoted 41 lines
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_hwpoisoned()to only free the healthy pages and to exclude 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 having different orders. Every block meets the requirements to be freed via__free_one_page().
free_has_hwpoisoned()has linear time complexity 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 identifyPageHWPoison()defines the overall time complexity. For a 1G hugepage having several HWPoison pages,free_has_hwpoisoned()takes around 2ms on average.Since
free_has_hwpoisoned()has nontrivial overhead, it is wrapped insidefree_pages_prepare_has_hwpoisoned()and done onlyPG_has_hwpoisonedindicates HWPoison page exists and afterfree_pages_prepare()succeeded.[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]Signed-off-by Jiaqi Yan <[email protected]>--- mm/page_alloc.c | 157 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 154 insertions(+), 3 deletions(-)mm/page_alloc.c +0
-0mm/page_alloc.c static bool free_frozen_page_commit(struct zone *zone, · line 2929
Quoted 2 lines
mm/page_alloc.c +0
-0@@ -2923,6 +2928,152 @@ static bool free_frozen_page_commit(struct zone *zone,return ret; }From correctness point of view I think it looks good to me. Let's see what the page allocator folks say.
A few nits below.
mm/page_alloc.c static bool free_frozen_page_commit(struct zone *zone, · line 3031
Quoted 4 lines
mm/page_alloc.c +4
-0+static bool compound_has_hwpoisoned (struct page *page,unsigned int order) +{ +if (order ==0 || !PageCompound (page)) +return false;nit: since order-0 compound page is not a thing, !PageCompound(page) check should cover order == 0 case.
Quoted 5 lines
mm/page_alloc.c +5
-0+return folio_test_has_hwpoisoned (page_folio (page)); +} + +/* +* Do free_has_hwpoisoned() when needed after free_pages_prepare(). mm/page_alloc.c static bool free_frozen_page_commit(struct zone *zone, · line 3047
Quoted 10 lines
mm/page_alloc.c +10
-0+* Returns +* - true: free_pages_prepare() is good and caller can proceed freeing. +* - false: caller should not free pages for one of the two reasons: +* 1. free_pages_prepare() failed so it is not safe to proceed freeing. +* 2. this is a compound page having some HWPoison pages, and healthy +* pages are already safely freed. +*/ +static bool free_pages_prepare_has_hwpoisoned (struct page *page, +unsigned int order, +fpi_t fpi_flags)nit: Hope we'll come up with a better name than
free_pages_prepare_has_poisoned(), but I don't have any better suggestion... :)
Quoted 8 lines
Quoted 5 lines
And I hope somebody familiar with compaction (as
compaction_free()callsfree_pages_prepare()and ignores its return value) could confirm that it is safe to do acompound_has_hwpoisoned()check and, when it returns true, callfree_has_hwpoisoned()infree_pages_prepare(),
so that we won't need a separate function to do this.I wrote the function. compact_control->freepages[] is an array of free pages isolated from buddy allocator as a temporary free page buffer.
Thanks for the context, Zi!
Quoted 7 lines
I wonder if
compaction_free()can get a HWPoisoned folio or not, since to get tocompaction_free(), the folio needs to be copied to a new folio. If the memory goes bad,folio_mc_copy()will return-EHWPOISONto abort the migration, but I do not see any code marking the foliohas_hwpoisoned. When it reaches tocompact_free(), there is no way to detect the error. I think the corrupted page will be put back to buddy and be marked ashas_hwpoisonafter it is allocated and accessed by an application.
IIRC compaction_free() is used to deal with the *target* folio (i.e. dst) if migration fails for any reason, say the src folio is hardware corrupted and folio_mc_copy() returns -EHWPOISON. We can know nothing about the integrity of the target folio out of folio_mc_copy(), hence no marking PG_has_hwpoisoned for target page. The src folio should already be marked async by memory_failure_queue() in copy_mc_highpage().
So I think to compaction_free(), it always deal with non-PG_has_hwpoisoned folio, and there isn't any difference between free_pages_prepare_has_hwpoisoned() and free_pages_prepare(). I can merge them in v4.
Quoted 7 lines
You might need to add
has_hwpoisonedcode in migration code path to catchfolio_mc_copy()errors.Best Regards,
Yan, Zi
Miaohe Lin
maintainerRe: [PATCH v3 2/3] mm/page_alloc: only free healthy pages in high-order has_hwpoisoned folio
In reply to Harry YooQuoted 89 lines · Harry Yoo
Quoted 11 lines · Jiaqi Yan
At the end of
dissolve_free_hugetlb_folio(), a free HugeTLB folio becomes non-HugeTLB, and 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.
Quoted 6 lines
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 betweendissolve_free_hugetlb_folio()frees a HWPoison high-order folio to buddy allocator and MFR takes HWPoison raw page off buddy allocator.I wonder if this is something we want to backport to -stable.
Quoted 44 lines
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_hwpoisoned()to only free the healthy pages and to exclude 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 having different orders. Every block meets the requirements to be freed via__free_one_page().
free_has_hwpoisoned()has linear time complexity 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 identifyPageHWPoison()defines the overall time complexity. For a 1G hugepage having several HWPoison pages,free_has_hwpoisoned()takes around 2ms on average.Since
free_has_hwpoisoned()has nontrivial overhead, it is wrapped insidefree_pages_prepare_has_hwpoisoned()and done onlyPG_has_hwpoisonedindicates HWPoison page exists and afterfree_pages_prepare()succeeded.[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]Signed-off-by Jiaqi Yan <[email protected]>--- mm/page_alloc.c | 157 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 154 insertions(+), 3 deletions(-)mm/page_alloc.c +0
-0@@ -2923,6 +2928,152 @@ static bool free_frozen_page_commit(struct zone *zone,return ret; }Quoted 1 line
From correctness point of view I think it looks good to me.
Let's see what the page allocator folks say.
A few nits below.
mm/page_alloc.c static bool free_frozen_page_commit(struct zone *zone, · line 3031
Quoted 4 lines
mm/page_alloc.c +4
-0+static bool compound_has_hwpoisoned (struct page *page,unsigned int order) +{ +if (order ==0 || !PageCompound (page)) +return false;nit: since order-0 compound page is not a thing, !PageCompound(page) check should cover order == 0 case.
Quoted 11 lines
mm/page_alloc.c +11
-0+return folio_test_has_hwpoisoned (page_folio (page)); +} + +/* +* Do free_has_hwpoisoned() when needed after free_pages_prepare(). +* Returns +* - true: free_pages_prepare() is good and caller can proceed freeing. +* - false: caller should not free pages for one of the two reasons: +* 1. free_pages_prepare() failed so it is not safe to proceed freeing. +* 2. this is a compound page having some HWPoison pages, and healthy +* pages are already safely freed.
Quoted 8 lines
mm/page_alloc.c static bool free_frozen_page_commit(struct zone *zone, · line 3047
Quoted 4 lines
+ */
mm/page_alloc.c +3
-0+static bool free_pages_prepare_has_hwpoisoned (struct page *page, +unsigned int order, +fpi_t fpi_flags)nit: Hope we'll come up with a better name than
free_pages_prepare_has_poisoned(), but I don't have any better suggestion... :)
What about something like free_healthy_pages_prepare?
Thanks both.
.
Re: [PATCH v3 2/3] mm/page_alloc: only free healthy pages in high-order has_hwpoisoned folio
In reply to Harry YooQuoted 58 lines · Harry Yoo
Quoted 11 lines · Jiaqi Yan
At the end of
dissolve_free_hugetlb_folio(), a free HugeTLB folio becomes non-HugeTLB, and 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.
Quoted 6 lines
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 betweendissolve_free_hugetlb_folio()frees a HWPoison high-order folio to buddy allocator and MFR takes HWPoison raw page off buddy allocator.I wonder if this is something we want to backport to -stable.
Quoted 36 lines
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_hwpoisoned()to only free the healthy pages and to exclude 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 having different orders. Every block meets the requirements to be freed via__free_one_page().
free_has_hwpoisoned()has linear time complexity 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 identifyPageHWPoison()defines the overall time complexity. For a 1G hugepage having several HWPoison pages,free_has_hwpoisoned()takes around 2ms on average.Since
free_has_hwpoisoned()has nontrivial overhead, it is wrapped insidefree_pages_prepare_has_hwpoisoned()and done onlyPG_has_hwpoisonedindicates HWPoison page exists and afterfree_pages_prepare()succeeded.[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]Signed-off-by Jiaqi Yan <[email protected]>--- mm/page_alloc.c | 157 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 154 insertions(+), 3 deletions(-)
Quoted 9 lines
mm/page_alloc.c static bool free_frozen_page_commit(struct zone *zone, · line 2929
Quoted 7 lines
mm/page_alloc.c +0
-0@@ -2923,6 +2928,152 @@ static bool free_frozen_page_commit(struct zone *zone,return ret; }From correctness point of view I think it looks good to me.
Thanks, Harry!
Quoted 11 lines
Let's see what the page allocator folks say.
A few nits below.
mm/page_alloc.c static bool free_frozen_page_commit(struct zone *zone, · line 3031
Quoted 4 lines
mm/page_alloc.c +4
-0+static bool compound_has_hwpoisoned (struct page *page,unsigned int order) +{ +if (order ==0 || !PageCompound (page)) +return false;nit: since order-0 compound page is not a thing, !PageCompound(page) check should cover order == 0 case.
ack, will simplify to something like PageCompound && folio_test_has_hwpoisoned.
Quoted 30 lines
Quoted 6 lines
mm/page_alloc.c +5
-0+return folio_test_has_hwpoisoned (page_folio (page)); +} + +/* +* Do free_has_hwpoisoned() when needed after free_pages_prepare(). mm/page_alloc.c static bool free_frozen_page_commit(struct zone *zone, · line 3047
Quoted 10 lines
mm/page_alloc.c +10
-0+* Returns +* - true: free_pages_prepare() is good and caller can proceed freeing. +* - false: caller should not free pages for one of the two reasons: +* 1. free_pages_prepare() failed so it is not safe to proceed freeing. +* 2. this is a compound page having some HWPoison pages, and healthy +* pages are already safely freed. +*/ +static bool free_pages_prepare_has_hwpoisoned (struct page *page, +unsigned int order, +fpi_t fpi_flags)nit: Hope we'll come up with a better name than
free_pages_prepare_has_poisoned(), but I don't have any better suggestion... :)And I hope somebody familiar with compaction (as
compaction_free()callsfree_pages_prepare()and ignores its return value) could confirm that it is safe to do acompound_has_hwpoisoned()check and, when it returns true, callfree_has_hwpoisoned()infree_pages_prepare(),
so that we won't need a separate function to do this.--
Cheers,
Harry / Hyeonggon
Miaohe Lin
maintainerRe: [PATCH v3 2/3] mm/page_alloc: only free healthy pages in high-order has_hwpoisoned folio
In reply to Jiaqi YanQuoted 44 lines · Jiaqi Yan
At the end of
dissolve_free_hugetlb_folio(), a free HugeTLB folio becomes non-HugeTLB, and 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 betweendissolve_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_hwpoisoned()to only free the healthy pages and to exclude 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 having different orders. Every block meets the requirements to be freed via__free_one_page().
free_has_hwpoisoned()has linear time complexity 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 identifyPageHWPoison()defines the overall time complexity. For a 1G hugepage having several HWPoison pages,free_has_hwpoisoned()takes around 2ms on average.Since
free_has_hwpoisoned()has nontrivial overhead, it is wrapped insidefree_pages_prepare_has_hwpoisoned()and done onlyPG_has_hwpoisonedindicates HWPoison page exists and afterfree_pages_prepare()succeeded.
Quoted 5 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]Signed-off-by Jiaqi Yan <[email protected]>
Thanks for your patch. This patch looks good to me. A few nits below.
Quoted 19 lines
--- mm/page_alloc.c | 157 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 154 insertions(+), 3 deletions(-)mm/page_alloc.c +3
-0@@ -215,6 +215,9 @@ gfp_t gfp_allowed_mask __read_mostly = GFP_BOOT_MASK;unsigned int pageblock_order__read_mostly ;#endif +static bool free_pages_prepare_has_hwpoisoned (struct page *page, +unsigned int order, +fpi_t fpi_flags);static void __free_pages_ok (struct page *page,unsigned int order,fpi_t fpi_flags);
mm/page_alloc.c static void __free_pages_ok(struct page *page, unsigned int order, · line 1577
Quoted 9 lines
mm/page_alloc.c +4
-2@@ -1568,8 +1571,10 @@ static void __free_pages_ok(struct page *page, unsigned int order,unsigned long pfn =page_to_pfn (page);struct zone *zone =page_zone (page); -if (free_pages_prepare (page, order)) -free_one_page (zone, page, pfn, order, fpi_flags); +if (!free_pages_prepare_has_hwpoisoned (page, order, fpi_flags)) +return ; + +free_one_page (zone, page, pfn, order, fpi_flags);
It might be better to write as:
if (free_pages_prepare_has_hwpoisoned(page, order, fpi_flags))
free_one_page(zone, page, pfn, order, fpi_flags);
just like previous one.
Quoted 77 lines
mm/page_alloc.c +70
-0} void __meminit __free_pages_core(struct page *page, unsigned int order,@@ -2923,6 +2928,152 @@ static bool free_frozen_page_commit(struct zone *zone,return ret; } +/* +* Given a range of physically contiguous pages, efficiently free them +* block by block. Block order is chosen to meet the PFN alignment +* requirement in __free_one_page(). +*/ +static void free_contiguous_pages (struct page *curr,unsigned long nr_pages, +fpi_t fpi_flags) +{ +unsigned int order; +unsigned int align_order; +unsigned int size_order; +unsigned long remaining; +unsigned long pfn =page_to_pfn (curr); +const unsigned long end_pfn = pfn + nr_pages; +struct zone *zone =page_zone (curr); + +/* +* 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 (pfn < end_pfn) { + remaining = end_pfn - pfn; + align_order =ffs (pfn) -1 ; + size_order =fls_long (remaining) -1 ; + order =min (align_order, size_order); + +free_one_page (zone, curr, pfn, order, fpi_flags); + curr += (1UL << order); + pfn += (1UL << order); + } + +VM_WARN_ON (pfn != end_pfn); +} + +/* +* Given a high-order compound page containing certain number of HWPoison +* pages, free only the healthy ones to buddy allocator. +* +* Pages must have passed free_pages_prepare(). Even if having HWPoison +* pages, breaking down compound page and updating metadata (e.g. page +* owner, alloc tag) can be done together during free_pages_prepare(), +* which simplifies the splitting here: unlike __split_unmapped_folio(), +* there is no need to turn split pages into a compound page or to carry +* metadata. +* +* It calls free_one_page O(2^order) times and cause nontrivial overhead. +* So only use this when the compound page really contains HWPoison. +* +* This implementation doesn't work in memdesc world. +*/ +static void free_has_hwpoisoned (struct page *page,unsigned int order, +fpi_t fpi_flags) +{ +struct page *curr = page; +struct page *next; +unsigned long nr_pages; +/* +* Don't assume end points to a valid page. It is only used +* here for pointer arithmetic. +*/ +struct page *end = page + (1 << order); +unsigned long total_freed =0 ; +unsigned long total_hwp =0 ; + +VM_WARN_ON (order ==0 ); +VM_WARN_ON (page->flags.f &PAGE_FLAGS_CHECK_AT_PREP ); +
mm/page_alloc.c static bool free_frozen_page_commit(struct zone *zone, · line 3010
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 (next != end &&PageHWPoison (next)) {
A comment why clear_page_tag_ref is needed here should be helpful.
mm/page_alloc.c static bool free_frozen_page_commit(struct zone *zone, · line 3020
Quoted 10 lines
mm/page_alloc.c +10
-0+clear_page_tag_ref (next); + ++total_hwp; + } + +free_contiguous_pages (curr, nr_pages, fpi_flags); + total_freed += nr_pages; +if (next == end) +break ; + + curr =PageHWPoison (next) ? next +1 : next;
IIUC, when code reaches here, we must have found a hwpoison page or next will equal to end. So I think PageHWPoison(next) is always true and above code can be simplified as:
curr = next + 1;
Thanks.
.
Re: [PATCH v3 2/3] mm/page_alloc: only free healthy pages in high-order has_hwpoisoned folio
Quoted 46 lines · Miaohe Lin
Quoted 44 lines · Jiaqi Yan
At the end of
dissolve_free_hugetlb_folio(), a free HugeTLB folio becomes non-HugeTLB, and 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 betweendissolve_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_hwpoisoned()to only free the healthy pages and to exclude 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 having different orders. Every block meets the requirements to be freed via__free_one_page().
free_has_hwpoisoned()has linear time complexity 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 identifyPageHWPoison()defines the overall time complexity. For a 1G hugepage having several HWPoison pages,free_has_hwpoisoned()takes around 2ms on average.Since
free_has_hwpoisoned()has nontrivial overhead, it is wrapped insidefree_pages_prepare_has_hwpoisoned()and done onlyPG_has_hwpoisonedindicates HWPoison page exists and afterfree_pages_prepare()succeeded.
Quoted 7 lines
Quoted 5 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]Signed-off-by Jiaqi Yan <[email protected]>Thanks for your patch. This patch looks good to me. A few nits below.
Thanks for taking a look, Miaohe!
Quoted 32 lines
Quoted 20 lines
--- mm/page_alloc.c | 157 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 154 insertions(+), 3 deletions(-)mm/page_alloc.c +3
-0@@ -215,6 +215,9 @@ gfp_t gfp_allowed_mask __read_mostly = GFP_BOOT_MASK;unsigned int pageblock_order__read_mostly ;#endif +static bool free_pages_prepare_has_hwpoisoned (struct page *page, +unsigned int order, +fpi_t fpi_flags);static void __free_pages_ok (struct page *page,unsigned int order,fpi_t fpi_flags);mm/page_alloc.c static void __free_pages_ok(struct page *page, unsigned int order, · line 1577
Quoted 9 lines
mm/page_alloc.c +4
-2@@ -1568,8 +1571,10 @@ static void __free_pages_ok(struct page *page, unsigned int order,unsigned long pfn =page_to_pfn (page);struct zone *zone =page_zone (page); -if (free_pages_prepare (page, order)) -free_one_page (zone, page, pfn, order, fpi_flags); +if (!free_pages_prepare_has_hwpoisoned (page, order, fpi_flags)) +return ; + +free_one_page (zone, page, pfn, order, fpi_flags);It might be better to write as:
Quoted 4 lines
if (
free_pages_prepare_has_hwpoisoned(page, order, fpi_flags))free_one_page(zone, page, pfn, order, fpi_flags);just like previous one.
Ack, but I probably won't need this change after merging free_pages_prepare_has_hwpoisoned() into free_pages_prepare().
Quoted 82 lines
Quoted 82 lines
mm/page_alloc.c +74
-0} void __meminit __free_pages_core(struct page *page, unsigned int order,@@ -2923,6 +2928,152 @@ static bool free_frozen_page_commit(struct zone *zone,return ret; } +/* +* Given a range of physically contiguous pages, efficiently free them +* block by block. Block order is chosen to meet the PFN alignment +* requirement in __free_one_page(). +*/ +static void free_contiguous_pages (struct page *curr,unsigned long nr_pages, +fpi_t fpi_flags) +{ +unsigned int order; +unsigned int align_order; +unsigned int size_order; +unsigned long remaining; +unsigned long pfn =page_to_pfn (curr); +const unsigned long end_pfn = pfn + nr_pages; +struct zone *zone =page_zone (curr); + +/* +* 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 (pfn < end_pfn) { + remaining = end_pfn - pfn; + align_order =ffs (pfn) -1 ; + size_order =fls_long (remaining) -1 ; + order =min (align_order, size_order); + +free_one_page (zone, curr, pfn, order, fpi_flags); + curr += (1UL << order); + pfn += (1UL << order); + } + +VM_WARN_ON (pfn != end_pfn); +} + +/* +* Given a high-order compound page containing certain number of HWPoison +* pages, free only the healthy ones to buddy allocator. +* +* Pages must have passed free_pages_prepare(). Even if having HWPoison +* pages, breaking down compound page and updating metadata (e.g. page +* owner, alloc tag) can be done together during free_pages_prepare(), +* which simplifies the splitting here: unlike __split_unmapped_folio(), +* there is no need to turn split pages into a compound page or to carry +* metadata. +* +* It calls free_one_page O(2^order) times and cause nontrivial overhead. +* So only use this when the compound page really contains HWPoison. +* +* This implementation doesn't work in memdesc world. +*/ +static void free_has_hwpoisoned (struct page *page,unsigned int order, +fpi_t fpi_flags) +{ +struct page *curr = page; +struct page *next; +unsigned long nr_pages; +/* +* Don't assume end points to a valid page. It is only used +* here for pointer arithmetic. +*/ +struct page *end = page + (1 << order); +unsigned long total_freed =0 ; +unsigned long total_hwp =0 ; + +VM_WARN_ON (order ==0 ); +VM_WARN_ON (page->flags.f &PAGE_FLAGS_CHECK_AT_PREP ); + +while (curr < end) { + next = curr; + nr_pages =0 ; +
Quoted 8 lines
mm/page_alloc.c static bool free_frozen_page_commit(struct zone *zone, · line 3010
Quoted 6 lines
mm/page_alloc.c +6
-0+while (next < end && !PageHWPoison (next)) { + ++next; + ++nr_pages; + } + +if (next != end &&PageHWPoison (next)) {A comment why
clear_page_tag_refis needed here should be helpful.
Ack.
Quoted 15 lines
mm/page_alloc.c static bool free_frozen_page_commit(struct zone *zone, · line 3020
Quoted 11 lines
mm/page_alloc.c +10
-0+clear_page_tag_ref (next); + ++total_hwp; + } + +free_contiguous_pages (curr, nr_pages, fpi_flags); + total_freed += nr_pages; +if (next == end) +break ; + + curr =PageHWPoison (next) ? next +1 : next;IIUC, when code reaches here, we must have found a hwpoison page or next will equal to end. So I think PageHWPoison(next) is always true and above code can be simplified as:
Quoted 1 line
curr = next + 1;
Yeah, good catch! Will simplify.
Quoted 3 lines
Thanks.
.
David Hildenbrand (Red Hat)
maintainerRe: [PATCH v3 2/3] mm/page_alloc: only free healthy pages in high-order has_hwpoisoned folio
In reply to Jiaqi YanQuoted 10 lines · Jiaqi Yan
At the end of
dissolve_free_hugetlb_folio(), a free HugeTLB folio becomes non-HugeTLB, and 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.
Do we really have to have all that complexity in free_frozen_pages().
Can't we hook into __update_and_free_hugetlb_folio() and just free the chunks there?
Quoted 7 lines
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 betweendissolve_free_hugetlb_folio()frees a HWPoison high-order folio to buddy allocator and MFR takes HWPoison raw page off buddy allocator.
I wonder whether marking the pageblock as isolated before freeing it could work?
In that case, nobody will be able to allocate the page before we un-isolate it.
Just a thought: but when you are dealing with a possible race, you can avoid that race by prohibiting the intermediate allocation from happening in the first place.
Also, this is a lot of complexity. Was this issue already hit in the past or is it purely theoretical?
Signature · 3 lines
Cheers David
Re: [PATCH v3 2/3] mm/page_alloc: only free healthy pages in high-order has_hwpoisoned folio
Sorry for the late reply, Harry, Zi, Miaohe, and David. I was occupied by some other duty in the past two weeks.
Quoted 14 lines · David Hildenbrand (Red Hat)
Quoted 10 lines · Jiaqi Yan
At the end of
dissolve_free_hugetlb_folio(), a free HugeTLB folio becomes non-HugeTLB, and 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.
Quoted 4 lines
Do we really have to have all that complexity in
free_frozen_pages().Can't we hook into
__update_and_free_hugetlb_folio()and just free the chunks there?
I don't know but I imagine to get chunks you will need the ability to split a in-use (not free_huge_folio()-ed) hugetlb folio like splitting a THP folio? Won't that be more complicated and very specific to hugetlb? This solution is implemented in free_frozen_pages() to avoid heavylifing splitting.
Also, doing in __update_and_free_hugetlb_folio() definitely won't help with [1]: to handle a previously split-failed and now to be freed PG_has_hwpoisoned THP. This solution works.
Quoted 11 lines
Quoted 8 lines
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 betweendissolve_free_hugetlb_folio()frees a HWPoison high-order folio to buddy allocator and MFR takes HWPoison raw page off buddy allocator.I wonder whether marking the pageblock as isolated before freeing it could work?
Maybe... but where can the pageblock be isolated? Is there an existing candidate, or inventing a scribble pad to stash it?
And then who and when should un-isolate the pageblock, if we don't want to leak it?
Quoted 11 lines
In that case, nobody will be able to allocate the page before we un-isolate it.
Just a thought: but when you are dealing with a possible race, you can avoid that race by prohibiting the intermediate allocation from happening in the first place.
Also, this is a lot of complexity. Was this issue already hit in the past or is it purely theoretical?
Theoretical for now. But when I work on [2] with William and Harry, the issue can be reproduced fairly easily [3].
[2] https://lore.kernel.org/linux-mm/[email protected]/T/#m87c95ddf0b9397b409a9f8fac4e772ecc9abf209
[3] https://lore.kernel.org/linux-mm/[email protected]
Quoted 5 lines
--
CheersDavid
PATCH v3 3/3 mm/memory-failure: refactor page_handle_poison()
In reply to Jiaqi YanNow that HWPoison page(s) within HugeTLB page will be rejected by buddy allocator during dissolve_free_hugetlb_folio(), there is no need to drain_all_pages() and take_page_off_buddy() anymore. In fact, calling take_page_off_buddy() after dissolve_free_hugetlb_folio() succeeded returns false, making caller think page_handl_poion() failed.
On the other hand, for hardware corrupted pages in buddy allocator, take_page_off_buddy() is still a must-have.
Given hugepage and free buddy page should be treated differently, refactor page_handle_poison() and __page_handle_poison():
- __page_handle_poison() is unwind into page_handle_poison().
- Callers of page_handle_poison() also need to explicitly tell if page is HugeTLB hugepage or free buddy page.
- Add helper hugepage_handle_poison() for several existing HugeTLB specific callsites.
--- mm/memory-failure.c | 84 ++++++++++++++++++++++----------------------- 1 file changed, 41 insertions(+), 43 deletions(-)
mm/memory-failure.c +41 -43
@@ -162,54 +162,48 @@ static struct rb_root_cached pfn_space_itree = RB_ROOT_CACHED;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: failed to dissolve. +/** +* Handle the HugeTLB hugepage that @page belongs to. Return values: +* = 0: the hugepage is free hugepage and is dissolved. +* < 0: hugepage is in-use or failed to dissolve. */ -static int __page_handle_poison (struct page *page) +static int hugepage_handle_poison (struct page *page) { -int ret; +return dissolve_free_hugetlb_folio (page_folio (page)); +} + +/** +* Helper at the end of handling @page having hardware errors. +* @huge: @page is part of a HugeTLB hugepage. +* @free: @page is free buddy page. +* @release: memory-failure module should release a pending refcount. +*/ +static bool page_handle_poison (struct page *page,bool huge,bool free, +bool release) +{ +int ret =0 ;/* -* 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. +* Buddy allocator will exclude the HWPoison page after hugepage +* is successfully dissolved. */ - ret =dissolve_free_hugetlb_folio (page_folio (page)); -if (!ret) { +if (huge) + ret =hugepage_handle_poison (page); + +if (free) {drain_all_pages (page_zone (page)); - ret =take_page_off_buddy (page); + ret =take_page_off_buddy (page) ?0 :-1 ; } -return ret; -} - -static bool page_handle_poison (struct page *page,bool hugepage_or_freepage,bool release) -{ -if (hugepage_or_freepage) { +if ((huge || free) && ret <0 )/* -* Doing this check for free pages is also fine since -* dissolve_free_hugetlb_folio() returns 0 for non-hugetlb folios as well. +* We could fail to take off the target page from buddy +* for example due to racy page allocation, but that's +* acceptable because soft-offlined page is not broken +* and if someone really want to use it, they should +* take it. */ -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 -* acceptable because soft-offlined page is not broken -* and if someone really want to use it, they should -* take it. -*/ -return false; - } +return false;SetPageHWPoison (page);if (release)
@@ -1174,7 +1168,7 @@ static int me_huge_page(struct page_state *ps, struct page *p)* subpages. */ folio_put (folio); -if (__page_handle_poison (p) >0 ) { +if (!hugepage_handle_poison (p)) {page_ref_inc (p); res =MF_RECOVERED ; }else {
@@ -2067,7 +2061,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 (!hugepage_handle_poison (p)) {page_ref_inc (p); res =MF_RECOVERED ; }else {
@@ -2815,7 +2809,7 @@ static int soft_offline_in_use_page(struct page *page)if (ret) {pr_info ("%#lx: invalidated\n" , pfn); -page_handle_poison (page, false, true); +page_handle_poison (page, false, false, true);return 0 ; }
@@ -2836,7 +2830,7 @@ static int soft_offline_in_use_page(struct page *page)if (!ret) {bool release = !huge; -if (!page_handle_poison (page, huge, release)) +if (!page_handle_poison (page, huge, false, release)) ret = -EBUSY ; }else {if (!list_empty (&pagelist))
@@ -2884,6 +2878,8 @@ int soft_offline_page(unsigned long pfn, int flags) {int ret;bool try_again = true; +bool huge; +bool free;struct page *page;if (!pfn_valid (pfn)) {
@@ -2929,7 +2925,9 @@ int soft_offline_page(unsigned long pfn, int flags)if (ret >0 ) { ret =soft_offline_in_use_page (page); }else if (ret ==0 ) { -if (!page_handle_poison (page, true, false)) { + huge =folio_test_hugetlb (page_folio (page)); + free =is_free_buddy_page (page); +if (!page_handle_poison (page, huge, free, false)) {if (try_again) { try_again = false; flags &= ~MF_COUNT_INCREASED ;
Signature · 1 line
2.52.0.457.g6b5491de43-goog
Miaohe Lin
maintainerRe: [PATCH v3 3/3] mm/memory-failure: refactor page_handle_poison()
Quoted 5 lines · Jiaqi Yan
Now that HWPoison page(s) within HugeTLB page will be rejected by buddy allocator during
dissolve_free_hugetlb_folio(), there is no need todrain_all_pages()andtake_page_off_buddy()anymore. In fact, callingtake_page_off_buddy()afterdissolve_free_hugetlb_folio()succeeded returns false, making caller thinkpage_handl_poion()failed.
s/page_handl_poion/page_handle_poison/
Quoted 26 lines
On the other hand, for hardware corrupted pages in buddy allocator,
take_page_off_buddy()is still a must-have.Given hugepage and free buddy page should be treated differently, refactor
page_handle_poison()and__page_handle_poison():-
__page_handle_poison()is unwind intopage_handle_poison().- Callers of
page_handle_poison()also need to explicitly tell if page is HugeTLB hugepage or free buddy page.- Add helper
hugepage_handle_poison()for several existing HugeTLB specific callsites.Signed-off-by Jiaqi Yan <[email protected]>--- mm/memory-failure.c | 84 ++++++++++++++++++++++----------------------- 1 file changed, 41 insertions(+), 43 deletions(-)mm/memory-failure.c +0
-0@@ -162,54 +162,48 @@ static struct rb_root_cached pfn_space_itree = RB_ROOT_CACHED;
mm/memory-failure.c static struct rb_root_cached pfn_space_itree = RB_ROOT_CACHED; · line 167
Quoted 10 lines
mm/memory-failure.c +3
-5static 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: failed to dissolve. +/** +* Handle the HugeTLB hugepage that @page belongs to. Return values: +* = 0: the hugepage is free hugepage and is dissolved.
In soft offline scene, dissolve_free_hugetlb_folio would return 0 when the page becomes a normal page due to race.
Quoted 30 lines
mm/memory-failure.c +15
-11+* < 0: hugepage is in-use or failed to dissolve. */ -static int __page_handle_poison (struct page *page) +static int hugepage_handle_poison (struct page *page) { -int ret; +return dissolve_free_hugetlb_folio (page_folio (page)); +} + +/** +* Helper at the end of handling @page having hardware errors. +* @huge: @page is part of a HugeTLB hugepage. +* @free: @page is free buddy page. +* @release: memory-failure module should release a pending refcount. +*/ +static bool page_handle_poison (struct page *page,bool huge,bool free, +bool release) +{ +int ret =0 ;/* -* 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
mm/memory-failure.c static struct rb_root_cached pfn_space_itree = RB_ROOT_CACHED; · line 193
Quoted 10 lines
mm/memory-failure.c +6
-3-* queue if we need to refill those. +* Buddy allocator will exclude the HWPoison page after hugepage +* is successfully dissolved. */ - ret =dissolve_free_hugetlb_folio (page_folio (page)); -if (!ret) { +if (huge) + ret =hugepage_handle_poison (page); + +if (free) {
Nit: huge and free won't be both true. So we could write it as:
if (huge) {
...
} else if (free) {mm/memory-failure.c static struct rb_root_cached pfn_space_itree = RB_ROOT_CACHED; · line 198
Quoted 12 lines
mm/memory-failure.c +2
-7drain_all_pages (page_zone (page)); - ret =take_page_off_buddy (page); + ret =take_page_off_buddy (page) ?0 :-1 ; } -return ret; -} - -static bool page_handle_poison (struct page *page,bool hugepage_or_freepage,bool release) -{ -if (hugepage_or_freepage) { +if ((huge || free) && ret <0 )
Nit: ret won't be <0 if both huge and free are false. So I think we might simplify it as:
if (ret < 0)
Quoted 51 lines
mm/memory-failure.c +9
-15/* -* Doing this check for free pages is also fine since -* dissolve_free_hugetlb_folio() returns 0 for non-hugetlb folios as well. +* We could fail to take off the target page from buddy +* for example due to racy page allocation, but that's +* acceptable because soft-offlined page is not broken +* and if someone really want to use it, they should +* take it. */ -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 -* acceptable because soft-offlined page is not broken -* and if someone really want to use it, they should -* take it. -*/ -return false; - } +return false;SetPageHWPoison (page);if (release)@@ -1174,7 +1168,7 @@ static int me_huge_page(struct page_state *ps, struct page *p)* subpages. */ folio_put (folio); -if (__page_handle_poison (p) >0 ) { +if (!hugepage_handle_poison (p)) {page_ref_inc (p); res =MF_RECOVERED ; }else {@@ -2067,7 +2061,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 (!hugepage_handle_poison (p)) {page_ref_inc (p); res =MF_RECOVERED ; }else {@@ -2815,7 +2809,7 @@ static int soft_offline_in_use_page(struct page *page)if (ret) {pr_info ("%#lx: invalidated\n" , pfn); -page_handle_poison (page, false, true); +page_handle_poison (page, false, false, true);return 0 ; }
mm/memory-failure.c static int soft_offline_in_use_page(struct page *page) · line 2833
Quoted 5 lines
mm/memory-failure.c +1
-1@@ -2836,7 +2830,7 @@ static int soft_offline_in_use_page(struct page *page)if (!ret) {bool release = !huge; -if (!page_handle_poison (page, huge, release)) +if (!page_handle_poison (page, huge, false, release))
This might not work for soft offline. PageHWPoison is not yet set so folio_clear_hugetlb_hwpoison won't be called when dissolve hugetlb hugepages...
Quoted 13 lines
mm/memory-failure.c +2
-0ret = -EBUSY ; }else {if (!list_empty (&pagelist))@@ -2884,6 +2878,8 @@ int soft_offline_page(unsigned long pfn, int flags) {int ret;bool try_again = true; +bool huge; +bool free;struct page *page;if (!pfn_valid (pfn)) {
mm/memory-failure.c int soft_offline_page(unsigned long pfn, int flags) · line 2928
Quoted 5 lines
mm/memory-failure.c +1
-1@@ -2929,7 +2925,9 @@ int soft_offline_page(unsigned long pfn, int flags)if (ret >0 ) { ret =soft_offline_in_use_page (page); }else if (ret ==0 ) { -if (!page_handle_poison (page, true, false)) { + huge =folio_test_hugetlb (page_folio (page));
folio_test_hugetlb check is racy because there's no guarantee that hugetlb hugepage won't be dissolved before calling page_handle_poison. That will lead to problem...
soft_offline_page
folio_test_hugetlb -- true now
page_handle_poison
/* Hugepage is dissolved somewhere. */
hugepage_handle_poison -- return 0 because page is normal page or free buddy page. SetPageHWPoison(page);
page_ref_inc(page); -- refcnt is increased while page might be on buddy...
mm/memory-failure.c int soft_offline_page(unsigned long pfn, int flags) · line 2930
Quoted 2 lines
mm/memory-failure.c +2
-0+ free =is_free_buddy_page (page); +if (!page_handle_poison (page, huge, free, false)) {
We assume free is always true due to ret is 0. So we can write it as:
if (!page_handle_poison(page, huge, true, false)) {mm/memory-failure.c int soft_offline_page(unsigned long pfn, int flags) · line 2933
Quoted 4 lines
mm/memory-failure.c +0
-0if (try_again) { try_again = false; flags &= ~MF_COUNT_INCREASED ;
Thanks.
.
Re: [PATCH v3 3/3] mm/memory-failure: refactor page_handle_poison()
Quoted 167 lines · Miaohe Lin
Quoted 5 lines · Jiaqi Yan
Now that HWPoison page(s) within HugeTLB page will be rejected by buddy allocator during
dissolve_free_hugetlb_folio(), there is no need todrain_all_pages()andtake_page_off_buddy()anymore. In fact, callingtake_page_off_buddy()afterdissolve_free_hugetlb_folio()succeeded returns false, making caller thinkpage_handl_poion()failed.s/page_handl_poion/page_handle_poison/
Quoted 26 lines
On the other hand, for hardware corrupted pages in buddy allocator,
take_page_off_buddy()is still a must-have.Given hugepage and free buddy page should be treated differently, refactor
page_handle_poison()and__page_handle_poison():-
__page_handle_poison()is unwind intopage_handle_poison().- Callers of
page_handle_poison()also need to explicitly tell if page is HugeTLB hugepage or free buddy page.- Add helper
hugepage_handle_poison()for several existing HugeTLB specific callsites.Signed-off-by Jiaqi Yan <[email protected]>--- mm/memory-failure.c | 84 ++++++++++++++++++++++----------------------- 1 file changed, 41 insertions(+), 43 deletions(-)mm/memory-failure.c +0
-0@@ -162,54 +162,48 @@ static struct rb_root_cached pfn_space_itree = RB_ROOT_CACHED;mm/memory-failure.c static struct rb_root_cached pfn_space_itree = RB_ROOT_CACHED; · line 167
Quoted 10 lines
mm/memory-failure.c +3
-5static 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: failed to dissolve. +/** +* Handle the HugeTLB hugepage that @page belongs to. Return values: +* = 0: the hugepage is free hugepage and is dissolved. In soft offline scene,
dissolve_free_hugetlb_foliowould return 0 when the page becomes a normal page due to race.Quoted 30 lines
mm/memory-failure.c +15
-11+* < 0: hugepage is in-use or failed to dissolve. */ -static int __page_handle_poison (struct page *page) +static int hugepage_handle_poison (struct page *page) { -int ret; +return dissolve_free_hugetlb_folio (page_folio (page)); +} + +/** +* Helper at the end of handling @page having hardware errors. +* @huge: @page is part of a HugeTLB hugepage. +* @free: @page is free buddy page. +* @release: memory-failure module should release a pending refcount. +*/ +static bool page_handle_poison (struct page *page,bool huge,bool free, +bool release) +{ +int ret =0 ;/* -* 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 mm/memory-failure.c static struct rb_root_cached pfn_space_itree = RB_ROOT_CACHED; · line 193
Quoted 10 lines
mm/memory-failure.c +6
-3-* queue if we need to refill those. +* Buddy allocator will exclude the HWPoison page after hugepage +* is successfully dissolved. */ - ret =dissolve_free_hugetlb_folio (page_folio (page)); -if (!ret) { +if (huge) + ret =hugepage_handle_poison (page); + +if (free) {Nit: huge and free won't be both true. So we could write it as:
if (huge) { ... } else if (free) {mm/memory-failure.c static struct rb_root_cached pfn_space_itree = RB_ROOT_CACHED; · line 198
Quoted 12 lines
mm/memory-failure.c +2
-7drain_all_pages (page_zone (page)); - ret =take_page_off_buddy (page); + ret =take_page_off_buddy (page) ?0 :-1 ; } -return ret; -} - -static bool page_handle_poison (struct page *page,bool hugepage_or_freepage,bool release) -{ -if (hugepage_or_freepage) { +if ((huge || free) && ret <0 )Nit: ret won't be <0 if both huge and free are false. So I think we might simplify it as:
if (ret < 0)Quoted 54 lines
mm/memory-failure.c +9
-15/* -* Doing this check for free pages is also fine since -* dissolve_free_hugetlb_folio() returns 0 for non-hugetlb folios as well. +* We could fail to take off the target page from buddy +* for example due to racy page allocation, but that's +* acceptable because soft-offlined page is not broken +* and if someone really want to use it, they should +* take it. */ -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 -* acceptable because soft-offlined page is not broken -* and if someone really want to use it, they should -* take it. -*/ -return false; - } +return false;SetPageHWPoison (page);if (release)@@ -1174,7 +1168,7 @@ static int me_huge_page(struct page_state *ps, struct page *p)* subpages. */ folio_put (folio); -if (__page_handle_poison (p) >0 ) { +if (!hugepage_handle_poison (p)) {page_ref_inc (p); res =MF_RECOVERED ; }else {@@ -2067,7 +2061,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 (!hugepage_handle_poison (p)) {page_ref_inc (p); res =MF_RECOVERED ; }else {@@ -2815,7 +2809,7 @@ static int soft_offline_in_use_page(struct page *page)if (ret) {pr_info ("%#lx: invalidated\n" , pfn); -page_handle_poison (page, false, true); +page_handle_poison (page, false, false, true);return 0 ; }@@ -2836,7 +2830,7 @@ static int soft_offline_in_use_page(struct page *page)if (!ret) {bool release = !huge;
Quoted 5 lines
mm/memory-failure.c static int soft_offline_in_use_page(struct page *page) · line 2833
Quoted 2 lines
mm/memory-failure.c +1
-1-if (!page_handle_poison (page, huge, release)) +if (!page_handle_poison (page, huge, false, release))This might not work for soft offline. PageHWPoison is not yet set so
folio_clear_hugetlb_hwpoisonwon't be called when dissolve hugetlb hugepages...
Thanks for pointing this problem (and the later problem) out, Miaohe! You are right, and I think the reason for both problems is, soft offline is a totally different case than memory_failure(): there is no PG_HWPoison until the end of page_handle_poison(). So free_has_hwpoisoned() can't help dissolve_free_hugetlb_folio() to exclude the page that triggered soft_offline_page().
For free_has_hwpoisoned(), I should only change the call sites in the memory_failure() path, and leave soft_offline_page()/page_handle_poison()/__ __page_handle_poison() alone. Looking at the current code, HWPoison hugetlb pages happens to be handled by __page_handle_poison() in either me_huge_page() or try_memory_failure_hugetlb(). So I think I can replace them with a new function that doesn't do take_page_off_buddy(), something like:
/*
+ * Only for a HugeTLB page being handled by memory_failure(). The key
+ * difference to soft_offline() is that, no HWPoison subpage will make
+ * into buddy allocator after a successful dissolve_free_hugetlb_folio(),
+ * so take_page_off_buddy() is unnecessary.
+ */
+static int __hugepage_handle_poison(struct page *page)
+{
+ struct folio *folio = page_folio(page);
+
+ VM_WARN_ON_FOLIO(!folio_test_hwpoison(folio), folio);
+
+ /*
+ * Can't use dissolve_free_hugetlb_folio() without a reliable + * raw_hwp_list telling which subpage is HWPoison. + */
+ if (folio_test_hugetlb_raw_hwp_unreliable(folio))
+ /* raw_hwp_list becomes unreliable when kmalloc() fails. */
+ return -ENOMEM;
+
+ return dissolve_free_hugetlb_folio(folio);
+}
+
On the other hand, just leave __page_handle_poison() and page_handle_poison() as is to do take_page_off_buddy() for soft offline case.
Quoted 45 lines
Quoted 14 lines
mm/memory-failure.c +2
-0ret = -EBUSY ; }else {if (!list_empty (&pagelist))@@ -2884,6 +2878,8 @@ int soft_offline_page(unsigned long pfn, int flags) {int ret;bool try_again = true; +bool huge; +bool free;struct page *page;if (!pfn_valid (pfn)) {mm/memory-failure.c int soft_offline_page(unsigned long pfn, int flags) · line 2928
Quoted 5 lines
mm/memory-failure.c +1
-1@@ -2929,7 +2925,9 @@ int soft_offline_page(unsigned long pfn, int flags)if (ret >0 ) { ret =soft_offline_in_use_page (page); }else if (ret ==0 ) { -if (!page_handle_poison (page, true, false)) { + huge =folio_test_hugetlb (page_folio (page));
folio_test_hugetlbcheck is racy because there's no guarantee that hugetlb hugepage won't be dissolved before callingpage_handle_poison. That will lead to problem...
soft_offline_page
folio_test_hugetlb-- true now
page_handle_poison
/* Hugepage is dissolved somewhere. */
hugepage_handle_poison-- return 0 because page is normal page or free buddy page. SetPageHWPoison(page);
page_ref_inc(page); -- refcnt is increased while page might be on buddy...mm/memory-failure.c int soft_offline_page(unsigned long pfn, int flags) · line 2930
Quoted 2 lines
mm/memory-failure.c +2
-0+ free =is_free_buddy_page (page); +if (!page_handle_poison (page, huge, free, false)) {We assume free is always true due to ret is 0. So we can write it as:
if (!page_handle_poison(page, huge, true, false)) {mm/memory-failure.c int soft_offline_page(unsigned long pfn, int flags) · line 2933
Quoted 4 lines
mm/memory-failure.c +0
-0if (try_again) { try_again = false; flags &= ~MF_COUNT_INCREASED ;Thanks.
.