Skip to content

Commit eac96c3

Browse files
yang-shitorvalds
authored andcommitted
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 f9ce0be ("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. Link: https://lkml.kernel.org/r/[email protected] Fixes: 800d8c6 ("shmem: add huge pages support") Signed-off-by: Yang Shi <[email protected]> Reviewed-by: Naoya Horiguchi <[email protected]> Suggested-by: Kirill A. Shutemov <[email protected]> Cc: Hugh Dickins <[email protected]> Cc: Matthew Wilcox <[email protected]> Cc: Oscar Salvador <[email protected]> Cc: Peter Xu <[email protected]> Cc: <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent c7cb42e commit eac96c3

File tree

5 files changed

+51
-1
lines changed

5 files changed

+51
-1
lines changed

include/linux/page-flags.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,15 @@ enum pageflags {
171171
/* Compound pages. Stored in first tail page's flags */
172172
PG_double_map = PG_workingset,
173173

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+
174183
/* non-lru isolated movable page */
175184
PG_isolated = PG_reclaim,
176185

@@ -668,6 +677,20 @@ PAGEFLAG_FALSE(DoubleMap)
668677
TESTSCFLAG_FALSE(DoubleMap)
669678
#endif
670679

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+
*/
687+
PAGEFLAG(HasHWPoisoned, has_hwpoisoned, PF_SECOND)
688+
TESTSCFLAG(HasHWPoisoned, has_hwpoisoned, PF_SECOND)
689+
#else
690+
PAGEFLAG_FALSE(HasHWPoisoned)
691+
TESTSCFLAG_FALSE(HasHWPoisoned)
692+
#endif
693+
671694
/*
672695
* Check if a page is currently marked HWPoisoned. Note that this check is
673696
* best effort only and inherently racy: there is no way to synchronize with

mm/huge_memory.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2426,6 +2426,8 @@ static void __split_huge_page(struct page *page, struct list_head *list,
24262426
/* lock lru list/PageCompound, ref frozen by page_ref_freeze */
24272427
lruvec = lock_page_lruvec(head);
24282428

2429+
ClearPageHasHWPoisoned(head);
2430+
24292431
for (i = nr - 1; i >= 1; i--) {
24302432
__split_huge_page_tail(head, i, lruvec, list);
24312433
/* Some pages can be beyond EOF: drop them from page cache */

mm/memory-failure.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1694,6 +1694,20 @@ int memory_failure(unsigned long pfn, int flags)
16941694
}
16951695

16961696
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);
16971711
if (try_to_split_thp_page(p, "Memory Failure") < 0) {
16981712
action_result(pfn, MF_MSG_UNSPLIT_THP, MF_IGNORED);
16991713
res = -EBUSY;

mm/memory.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3906,6 +3906,15 @@ vm_fault_t do_set_pmd(struct vm_fault *vmf, struct page *page)
39063906
if (compound_order(page) != HPAGE_PMD_ORDER)
39073907
return ret;
39083908

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+
39093918
/*
39103919
* Archs like ppc64 need additional space to store information
39113920
* related to pte entry. Use the preallocated table for that.

mm/page_alloc.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1312,8 +1312,10 @@ static __always_inline bool free_pages_prepare(struct page *page,
13121312

13131313
VM_BUG_ON_PAGE(compound && compound_order(page) != order, page);
13141314

1315-
if (compound)
1315+
if (compound) {
13161316
ClearPageDoubleMap(page);
1317+
ClearPageHasHWPoisoned(page);
1318+
}
13171319
for (i = 1; i < (1 << order); i++) {
13181320
if (compound)
13191321
bad += free_tail_pages_check(page, page + i);

0 commit comments

Comments
 (0)