History

mm: filemap: check if THP has hwpoisoned subpage for PMD page fault

When handling shmem page fault the THP with corrupted subpage could be PMD mapped if certain conditions are satisfied. But kernel is supposed to send SIGBUS when trying to map hwpoisoned page. There are two paths which may do PMD map: fault around and regular fault. Before commit f9ce0be71d1f ("mm: Cleanup faultaround and finish_fault() codepaths") the thing was even worse in fault around path. The THP could be PMD mapped as long as the VMA fits regardless what subpage is accessed and corrupted. After this commit as long as head page is not corrupted the THP could be PMD mapped. In the regular fault path the THP could be PMD mapped as long as the corrupted page is not accessed and the VMA fits. This loophole could be fixed by iterating every subpage to check if any of them is hwpoisoned or not, but it is somewhat costly in page fault path. So introduce a new page flag called HasHWPoisoned on the first tail page. It indicates the THP has hwpoisoned subpage(s). It is set if any subpage of THP is found hwpoisoned by memory failure and after the refcount is bumped successfully, then cleared when the THP is freed or split. The soft offline path doesn't need this since soft offline handler just marks a subpage hwpoisoned when the subpage is migrated successfully. But shmem THP didn't get split then migrated at all.

Yang Shi committed 5 years ago

Commit eac96c31 parent c7cb42e5 files · +51 −1Browse files

Unified · Split

5 files
  1. include/linux/page-flags.h +23 −0
  2. mm/huge_memory.c +2 −0
  3. mm/memory-failure.c +14 −0
  4. mm/memory.c +9 −0
  5. mm/page_alloc.c +3 −1
include/linux/page-flags.h+23−0
@@ -171,6 +171,15 @@ enum pageflags { · 170 unmodified lines
171 /* Compound pages. Stored in first tail page's flags */171 /* Compound pages. Stored in first tail page's flags */
172 PG_double_map = PG_workingset,172 PG_double_map = PG_workingset,
173 173
174#ifdef CONFIG_MEMORY_FAILURE
175 /*
176 * Compound pages. Stored in first tail page's flags.
177 * Indicates that at least one subpage is hwpoisoned in the
178 * THP.
179 */
180 PG_has_hwpoisoned = PG_mappedtodisk,
181#endif
182
174 /* non-lru isolated movable page */183 /* non-lru isolated movable page */
175 PG_isolated = PG_reclaim,184 PG_isolated = PG_reclaim,
176 185
@@ -668,6 +677,20 @@ PAGEFLAG_FALSE(DoubleMap) · 491 unmodified lines
668 TESTSCFLAG_FALSE(DoubleMap)677 TESTSCFLAG_FALSE(DoubleMap)
669#endif678#endif
670 679
680#if defined(CONFIG_MEMORY_FAILURE) && defined(CONFIG_TRANSPARENT_HUGEPAGE)
681/*
682 * PageHasHWPoisoned indicates that at least one subpage is hwpoisoned in the
683 * compound page.
684 *
685 * This flag is set by hwpoison handler. Cleared by THP split or free page.
686 */
687PAGEFLAG(HasHWPoisoned, has_hwpoisoned, PF_SECOND)
688 TESTSCFLAG(HasHWPoisoned, has_hwpoisoned, PF_SECOND)
689#else
690PAGEFLAG_FALSE(HasHWPoisoned)
691 TESTSCFLAG_FALSE(HasHWPoisoned)
692#endif
693
671/*694/*
672 * Check if a page is currently marked HWPoisoned. Note that this check is695 * Check if a page is currently marked HWPoisoned. Note that this check is
673 * best effort only and inherently racy: there is no way to synchronize with696 * best effort only and inherently racy: there is no way to synchronize with
mm/huge_memory.c+2−0
@@ -2426,6 +2426,8 @@ static void __split_huge_page(struct page *page, struct list_head *list, · 2425 unmodified lines
2426 /* lock lru list/PageCompound, ref frozen by page_ref_freeze */2426 /* lock lru list/PageCompound, ref frozen by page_ref_freeze */
2427 lruvec = lock_page_lruvec(head);2427 lruvec = lock_page_lruvec(head);
2428 2428
2429 ClearPageHasHWPoisoned(head);
2430
2429 for (i = nr - 1; i >= 1; i--) {2431 for (i = nr - 1; i >= 1; i--) {
2430 __split_huge_page_tail(head, i, lruvec, list);2432 __split_huge_page_tail(head, i, lruvec, list);
2431 /* Some pages can be beyond EOF: drop them from page cache */2433 /* Some pages can be beyond EOF: drop them from page cache */
mm/memory-failure.c+14−0
@@ -1694,6 +1694,20 @@ try_again: · 1693 unmodified lines
1694 }1694 }
1695 1695
1696 if (PageTransHuge(hpage)) {1696 if (PageTransHuge(hpage)) {
1697 /*
1698 * The flag must be set after the refcount is bumped
1699 * otherwise it may race with THP split.
1700 * And the flag can't be set in get_hwpoison_page() since
1701 * it is called by soft offline too and it is just called
1702 * for !MF_COUNT_INCREASE. So here seems to be the best
1703 * place.
1704 *
1705 * Don't need care about the above error handling paths for
1706 * get_hwpoison_page() since they handle either free page
1707 * or unhandlable page. The refcount is bumped iff the
1708 * page is a valid handlable page.
1709 */
1710 SetPageHasHWPoisoned(hpage);
1697 if (try_to_split_thp_page(p, "Memory Failure") < 0) {1711 if (try_to_split_thp_page(p, "Memory Failure") < 0) {
1698 action_result(pfn, MF_MSG_UNSPLIT_THP, MF_IGNORED);1712 action_result(pfn, MF_MSG_UNSPLIT_THP, MF_IGNORED);
1699 res = -EBUSY;1713 res = -EBUSY;
mm/memory.c+9−0
@@ -3906,6 +3906,15 @@ vm_fault_t do_set_pmd(struct vm_fault *vmf, struct page *page) · 3905 unmodified lines
3906 if (compound_order(page) != HPAGE_PMD_ORDER)3906 if (compound_order(page) != HPAGE_PMD_ORDER)
3907 return ret;3907 return ret;
3908 3908
3909 /*
3910 * Just backoff if any subpage of a THP is corrupted otherwise
3911 * the corrupted page may mapped by PMD silently to escape the
3912 * check. This kind of THP just can be PTE mapped. Access to
3913 * the corrupted subpage should trigger SIGBUS as expected.
3914 */
3915 if (unlikely(PageHasHWPoisoned(page)))
3916 return ret;
3917
3909 /*3918 /*
3910 * Archs like ppc64 need additional space to store information3919 * Archs like ppc64 need additional space to store information
3911 * related to pte entry. Use the preallocated table for that.3920 * related to pte entry. Use the preallocated table for that.
mm/page_alloc.c+3−1
@@ -1312,8 +1312,10 @@ static __always_inline bool free_pages_prepare(struct page *page, · 1311 unmodified lines
1312 1312
1313 VM_BUG_ON_PAGE(compound && compound_order(page) != order, page);1313 VM_BUG_ON_PAGE(compound && compound_order(page) != order, page);
1314 1314
1315 if (compound)1315 if (compound) {
1316 ClearPageDoubleMap(page);1316 ClearPageDoubleMap(page);
1317 ClearPageHasHWPoisoned(page);
1318 }
1317 for (i = 1; i < (1 << order); i++) {1319 for (i = 1; i < (1 << order); i++) {
1318 if (compound)1320 if (compound)
1319 bad += free_tail_pages_check(page, page + i);1321 bad += free_tail_pages_check(page, page + i);