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 between dissolve_free_hugetlb_folio() frees a HWPoison high-order folio to buddy allocator and MFR takes HWPoison raw page off buddy allocator. Another similar situation is when a transparent huge page (THP) runs into memory failure but splitting failed. Such THP will eventually be released to buddy allocator when owning userspace processes are gone, but with certain subpages having HWPoison. One obvious way to avoid both problems 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. free_has_hwpoisoned() is added in free_pages_prepare() as a shortcut and is only invoked if PG_has_hwpoisoned indicates HWPoison page exists and after checks and preparations in free_pages_prepare() all succeeded. free_has_hwpoisoned() then can re-use free_prepared_contig_range() [4] to decompose healthy ranges into the largest possible chunks of different orders. Every chunk 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 8 HWPoison pages, free_has_hwpoisoned() takes around 1ms on average on a system having 56 Intel Skylake physical cores. This is 15x to the case of freeing no HWPoison page. The cost is far from triggering soft lockup, and fair for handling exceptional hardware memory errors. [1] https://lore.kernel.org/linux-mm/1460711275-1130-15-git-send-email-mgorman@techsingularity.net [2] https://lore.kernel.org/linux-mm/1460711275-1130-16-git-send-email-mgorman@techsingularity.net [3] https://lore.kernel.org/all/20230216095131.17336-1-vbabka@suse.cz [4] https://lore.kernel.org/all/20260401101634.2868165-2-usama.anjum@arm.com Signed-off-by: Jiaqi Yan --- mm/page_alloc.c | 85 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) diff --git a/mm/page_alloc.c b/mm/page_alloc.c index e47679e7a9db..03df929abca6 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -208,6 +208,7 @@ gfp_t gfp_allowed_mask __read_mostly = GFP_BOOT_MASK; unsigned int pageblock_order __read_mostly; #endif +static void free_has_hwpoisoned(struct page *page, unsigned int order); static void __free_pages_ok(struct page *page, unsigned int order, fpi_t fpi_flags); static void reserve_highatomic_pageblock(struct page *page, int order, @@ -1309,6 +1310,14 @@ static inline void pgalloc_tag_sub_pages(struct alloc_tag *tag, unsigned int nr) #endif /* CONFIG_MEM_ALLOC_PROFILING */ +/* + * Returns + * - true: checks and preparations all good, caller can proceed freeing. + * - false: do not proceed freeing for one of the following reasons: + * 1. Some check failed so it is not safe to proceed freeing. + * 2. A compound page has some HWPoison pages. The healthy pages + * are already safely freed, and the HWPoison ones isolated. + */ static __always_inline bool __free_pages_prepare(struct page *page, unsigned int order, fpi_t fpi_flags) { @@ -1317,6 +1326,15 @@ static __always_inline bool __free_pages_prepare(struct page *page, bool init = want_init_on_free(); bool compound = PageCompound(page); struct folio *folio = page_folio(page); + /* + * When dealing with compound page, PG_has_hwpoisoned is cleared + * with PAGE_FLAGS_SECOND. So the check must be done first. + * + * 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 && folio_test_has_hwpoisoned(folio); if (fpi_flags & FPI_PREPARED) return true; @@ -1443,6 +1461,16 @@ static __always_inline bool __free_pages_prepare(struct page *page, debug_pagealloc_unmap_pages(page, 1 << order); + /* + * After breaking down compound page and dealing with page metadata + * (e.g. page owner and page alloc tags), take a shortcut if this + * was a compound page containing certain HWPoison subpages. + */ + if (should_fhh) { + free_has_hwpoisoned(page, order); + return false; + } + return true; } @@ -6936,6 +6964,63 @@ void __free_contig_range(unsigned long pfn, unsigned long nr_pages) __free_contig_range_common(pfn, nr_pages, /* is_frozen= */ false); } +/* + * Given a high-order compound page containing certain number of HWPoison + * pages, free only the healthy ones. + * + * 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 scans every raw page of the compound page and cause nontrivial overhead. + * So only use this when the compound page contains HWPoison page(s). + * + * This implementation needs rework in memdesc world. + */ +static void free_has_hwpoisoned(struct page *page, unsigned int order) +{ + unsigned long curr = page_to_pfn(page); + unsigned long end_pfn = curr + (1 << order); + unsigned long next; + 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_pfn) { + next = curr; + + while (next < end_pfn && !PageHWPoison(pfn_to_page(next))) + ++next; + + if (next != end_pfn && PageHWPoison(pfn_to_page(next))) { + /* + * Avoid accounting error when the page is freed + * by unpoison_memory(). + */ + clear_page_tag_ref(pfn_to_page(next)); + ++total_hwp; + } + + free_prepared_contig_range(pfn_to_page(curr), next - curr); + total_freed += next - curr; + + if (next == end_pfn) + break; + + VM_WARN_ON(!PageHWPoison(pfn_to_page(next))); + curr = next + 1; + } + + VM_WARN_ON(total_freed + total_hwp != (1 << order)); + pr_info("Freed %#lx pages, excluded %lu HWPoison pages\n", + total_freed, total_hwp); +} + #ifdef CONFIG_CONTIG_ALLOC /* Usage: See admin-guide/dynamic-debug-howto.rst */ static void alloc_contig_dump_pages(struct list_head *page_list) -- 2.54.0.823.g6e5bcc1fc9-goog 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), and can handle it specially with free_has_hwpoisoned(). Signed-off-by: Jiaqi Yan --- include/linux/page-flags.h | 2 +- mm/memory-failure.c | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/include/linux/page-flags.h b/include/linux/page-flags.h index 7223f6f4e2b4..223ec3b2d62f 100644 --- a/include/linux/page-flags.h +++ b/include/linux/page-flags.h @@ -893,7 +893,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. diff --git a/mm/memory-failure.c b/mm/memory-failure.c index 51508a55c405..95979b7995c1 100644 --- a/mm/memory-failure.c +++ b/mm/memory-failure.c @@ -1951,6 +1951,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); } -- 2.54.0.823.g6e5bcc1fc9-goog Now that HWPoison subpage(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_handle_poison() failed. Add __hugepage_handle_poison() and replace __page_handle_poison() at HugeTLB specific call sites. The being handled HugeTLB page either is free at the moment of try_memory_failure_hugetlb(), or becomes free at the moment of me_huge_page(). Signed-off-by: Jiaqi Yan --- mm/memory-failure.c | 36 ++++++++++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/mm/memory-failure.c b/mm/memory-failure.c index 95979b7995c1..098c4407e818 100644 --- a/mm/memory-failure.c +++ b/mm/memory-failure.c @@ -163,6 +163,30 @@ static struct rb_root_cached pfn_space_itree = RB_ROOT_CACHED; static DEFINE_MUTEX(pfn_space_lock); /* + * 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); +} + +/* + * Only for a free or HugeTLB page being handled by soft_offline(). * 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, @@ -1166,11 +1190,11 @@ 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)) { + res = MF_FAILED; + } else { page_ref_inc(p); res = MF_RECOVERED; - } else { - res = MF_FAILED; } } @@ -2076,11 +2100,11 @@ static int try_memory_failure_hugetlb(unsigned long pfn, int flags) */ if (res == MF_HUGETLB_FREED) { folio_unlock(folio); - if (__page_handle_poison(p) > 0) { + if (__hugepage_handle_poison(p)) { + res = MF_FAILED; + } else { page_ref_inc(p); res = MF_RECOVERED; - } else { - res = MF_FAILED; } return action_result(pfn, MF_MSG_FREE_HUGE, res); } -- 2.54.0.823.g6e5bcc1fc9-goog Add a new testcase to validate memory failure recovery for HWPoison anonymous 1G HugeTLB page, including proper SIGBUS delivery, releasing a 1G HugeTLB page containing one HWPoison page to buddy allocator, and isolation of the raw HWPoison page. Although can be added in future, this patch does not support testing the MADV_SOFT variant. Signed-off-by: Jiaqi Yan --- tools/testing/selftests/mm/memory-failure.c | 73 +++++++++++++++++++-- 1 file changed, 68 insertions(+), 5 deletions(-) diff --git a/tools/testing/selftests/mm/memory-failure.c b/tools/testing/selftests/mm/memory-failure.c index 032ed952057c..ea43b2877c81 100644 --- a/tools/testing/selftests/mm/memory-failure.c +++ b/tools/testing/selftests/mm/memory-failure.c @@ -18,6 +18,7 @@ #include #include +#include "hugepage_settings.h" #include "vm_util.h" enum inject_type { @@ -27,6 +28,7 @@ enum inject_type { enum result_type { MADV_HARD_ANON, + MADV_HARD_ANON_HUGETLB, MADV_HARD_CLEAN_PAGECACHE, MADV_HARD_DIRTY_PAGECACHE, MADV_SOFT_ANON, @@ -47,6 +49,8 @@ FIXTURE(memory_failure) int pagemap_fd; int kpageflags_fd; bool triggered; + /* Number of initial HugeTLB pages with default page size. */ + unsigned long nr_hugetlb_pages; }; FIXTURE_VARIANT(memory_failure) @@ -157,11 +161,11 @@ static void check(struct __test_metadata *_metadata, FIXTURE_DATA(memory_failure void *vaddr, enum result_type type, int setjmp) { unsigned long size; + unsigned long nr_hugetlb_pages; uint64_t pfn_flags; switch (type) { case MADV_SOFT_ANON: - case MADV_HARD_CLEAN_PAGECACHE: case MADV_SOFT_CLEAN_PAGECACHE: case MADV_SOFT_DIRTY_PAGECACHE: /* It is not expected to receive a SIGBUS signal. */ @@ -174,6 +178,7 @@ static void check(struct __test_metadata *_metadata, FIXTURE_DATA(memory_failure ASSERT_NE(pagemap_get_pfn(self->pagemap_fd, vaddr), self->pfn); break; case MADV_HARD_ANON: + case MADV_HARD_ANON_HUGETLB: case MADV_HARD_DIRTY_PAGECACHE: /* The SIGBUS signal should have been received. */ ASSERT_EQ(setjmp, 1); @@ -183,17 +188,36 @@ static void check(struct __test_metadata *_metadata, FIXTURE_DATA(memory_failure ASSERT_EQ(siginfo.si_code, BUS_MCEERR_AR); ASSERT_EQ(1UL << siginfo.si_addr_lsb, self->page_size); ASSERT_EQ(siginfo.si_addr, vaddr); - - /* XXX Check backing pte is hwpoison entry when supported. */ - ASSERT_TRUE(pagemap_is_swapped(self->pagemap_fd, vaddr)); break; default: SKIP(return, "unexpected inject type %d.\n", type); } + if (type == MADV_HARD_ANON || type == MADV_HARD_DIRTY_PAGECACHE) { + /* + * Check backing pte is hwpoison entry when supported. + * Although try_to_unmap_one() also installs hwpoison entry + * for HugeTLB, pagemap_hugetlb_range() doesn't parse + * swap entries at all. + */ + ASSERT_TRUE(pagemap_is_swapped(self->pagemap_fd, vaddr)); + } + /* Check if the value of HardwareCorrupted has increased. */ ASSERT_EQ(get_hardware_corrupted_size(&size), 0); - ASSERT_EQ(size, self->corrupted_size + self->page_size / 1024); + + if (type == MADV_HARD_ANON_HUGETLB) { + /* + * Only one page is hardware corrupted; the rest should all be + * released to buddy allocator. + */ + ASSERT_EQ(size, self->corrupted_size + getpagesize() / 1024); + /* HugeTLB should have lost the HWPoison HugeTLB page. */ + nr_hugetlb_pages = hugetlb_nr_default_pages(); + ASSERT_EQ(nr_hugetlb_pages + 1, self->nr_hugetlb_pages); + } else { + ASSERT_EQ(size, self->corrupted_size + self->page_size / 1024); + } /* Check if HWPoison flag is set. */ ASSERT_EQ(pageflags_get(self->pfn, self->kpageflags_fd, &pfn_flags), 0); @@ -247,6 +271,45 @@ TEST_F(memory_failure, anon) ASSERT_EQ(munmap(addr, self->page_size), 0); } +TEST_F(memory_failure, anon_hugetlb) +{ + char *addr; + int ret; + const unsigned long nr_alloc_hugetlb_pages = 4; + unsigned long alloc_size; + + if (variant->type == MADV_SOFT) + SKIP(return, "Soft offline test is not implemented"); + + /* HugeTLB settings will be automatically restored when test exits. */ + hugetlb_setup_default(nr_alloc_hugetlb_pages); + + alloc_size = default_huge_page_size() * nr_alloc_hugetlb_pages; + self->page_size = default_huge_page_size(); + self->nr_hugetlb_pages = hugetlb_nr_default_pages(); + + addr = mmap(0, alloc_size, PROT_READ | PROT_WRITE, + MAP_ANONYMOUS | MAP_PRIVATE | MAP_HUGETLB, -1, 0); + if (addr == MAP_FAILED) + SKIP(return, "mmap failed, not enough memory or 1G hugetlb not supported.\n"); + memset(addr, 0xce, alloc_size); + + prepare(_metadata, self, addr); + + ret = sigsetjmp(signal_jmp_buf, 1); + if (!self->triggered) { + self->triggered = true; + ASSERT_EQ(variant->inject(self, addr), 0); + FORCE_READ(*addr); + } + + check(_metadata, self, addr, MADV_HARD_ANON_HUGETLB, ret); + + cleanup(_metadata, self, addr); + + ASSERT_EQ(munmap(addr, alloc_size), 0); +} + static int prepare_file(const char *fname, unsigned long size) { int fd; -- 2.54.0.823.g6e5bcc1fc9-goog