From patchwork Mon Jan 10 17:31:43 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kamal Mostafa X-Patchwork-Id: 1578037 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=canonical.com header.i=@canonical.com header.a=rsa-sha256 header.s=20210705 header.b=nbNOhgdo; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=none (no SPF record) smtp.mailfrom=lists.ubuntu.com (client-ip=91.189.94.19; helo=huckleberry.canonical.com; envelope-from=kernel-team-bounces@lists.ubuntu.com; receiver=) Received: from huckleberry.canonical.com (huckleberry.canonical.com [91.189.94.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4JXgqf5zM4z9sSs for ; Tue, 11 Jan 2022 04:31:58 +1100 (AEDT) Received: from localhost ([127.0.0.1] helo=huckleberry.canonical.com) by huckleberry.canonical.com with esmtp (Exim 4.86_2) (envelope-from ) id 1n6yWN-0004Ee-NA; Mon, 10 Jan 2022 17:31:51 +0000 Received: from smtp-relay-canonical-0.internal ([10.131.114.83] helo=smtp-relay-canonical-0.canonical.com) by huckleberry.canonical.com with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.86_2) (envelope-from ) id 1n6yWM-0004Dt-4I for kernel-team@lists.ubuntu.com; Mon, 10 Jan 2022 17:31:50 +0000 Received: from ascalon (unknown [192.188.8.63]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp-relay-canonical-0.canonical.com (Postfix) with ESMTPSA id DF0CE40004 for ; Mon, 10 Jan 2022 17:31:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=canonical.com; s=20210705; t=1641835909; bh=QcLpTG5UAbiI921ykpER0OF0fp9tHwz7at2R47w356c=; h=From:To:Subject:Date:Message-Id:In-Reply-To:References; b=nbNOhgdokMVbWffyLpRTBEuj9Pdu7WFizwqqlotnFAPAbyKWiDpRGAS1mxJLkRQed caRvYF6Ky32nNWUx9OrfbPrUolosBoR2922s+vz+aMcPTpkCpqsnlo/rEAYs4Y6wb8 bFSfdZgyEBOpMN33XQGOkqgbVkfrXpCMgMNZn4kfk0r3EXmeVgW1hIZ+NHYOufN8Hr tyKL+OwSta9yLB5PjbBgsK+YO3CBlnpSnYC7K9FqTcb5BM9uV38g2TNreXKTbg0jpP 04uMa+Ir4lJh9B2ab27nn9aSzEQ/5QZCcrngQ2MB+rwmgFxqqd3HU89CYivWV8I9Fh X9+h2sUIV710Q== Received: from kamal by ascalon with local (Exim 4.90_1) (envelope-from ) id 1n6yWJ-0003tN-Mg for kernel-team@lists.ubuntu.com; Mon, 10 Jan 2022 09:31:47 -0800 From: Kamal Mostafa To: kernel-team@lists.ubuntu.com Subject: [Hirsute][PATCH 1/2] Revert "mm: filemap: check if THP has hwpoisoned subpage for PMD page fault" Date: Mon, 10 Jan 2022 09:31:43 -0800 Message-Id: <20220110173144.14826-2-kamal@canonical.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220110173144.14826-1-kamal@canonical.com> References: <20220110173144.14826-1-kamal@canonical.com> X-BeenThere: kernel-team@lists.ubuntu.com X-Mailman-Version: 2.1.20 Precedence: list List-Id: Kernel team discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: kernel-team-bounces@lists.ubuntu.com Sender: "kernel-team" BugLink: https://bugs.launchpad.net/bugs/1956984 This reverts commit 327aa2137d989f278ece7e8e31c218dfb3416c35. This Ubuntu backport is bogus (hunk applied to routine finish_fault() should have been applied to do_set_pmd()). To be replaced by corrected backport. Signed-off-by: Kamal Mostafa --- include/linux/page-flags.h | 23 ----------------------- mm/huge_memory.c | 2 -- mm/memory-failure.c | 14 -------------- mm/memory.c | 9 --------- mm/page_alloc.c | 4 +--- 5 files changed, 1 insertion(+), 51 deletions(-) diff --git a/include/linux/page-flags.h b/include/linux/page-flags.h index 9b85f492bf56..ec5d0290e0ee 100644 --- a/include/linux/page-flags.h +++ b/include/linux/page-flags.h @@ -168,15 +168,6 @@ enum pageflags { /* Compound pages. Stored in first tail page's flags */ PG_double_map = PG_workingset, -#ifdef CONFIG_MEMORY_FAILURE - /* - * Compound pages. Stored in first tail page's flags. - * Indicates that at least one subpage is hwpoisoned in the - * THP. - */ - PG_has_hwpoisoned = PG_mappedtodisk, -#endif - /* non-lru isolated movable page */ PG_isolated = PG_reclaim, @@ -709,20 +700,6 @@ PAGEFLAG_FALSE(DoubleMap) TESTSCFLAG_FALSE(DoubleMap) #endif -#if defined(CONFIG_MEMORY_FAILURE) && defined(CONFIG_TRANSPARENT_HUGEPAGE) -/* - * PageHasHWPoisoned indicates that at least one subpage is hwpoisoned in the - * compound page. - * - * This flag is set by hwpoison handler. Cleared by THP split or free page. - */ -PAGEFLAG(HasHWPoisoned, has_hwpoisoned, PF_SECOND) - TESTSCFLAG(HasHWPoisoned, has_hwpoisoned, PF_SECOND) -#else -PAGEFLAG_FALSE(HasHWPoisoned) - TESTSCFLAG_FALSE(HasHWPoisoned) -#endif - /* * For pages that are never mapped to userspace (and aren't PageSlab), * page_type may be used. Because it is initialised to -1, we invert the diff --git a/mm/huge_memory.c b/mm/huge_memory.c index eab1ed3e9675..f735eb3755db 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -2499,8 +2499,6 @@ static void __split_huge_page(struct page *page, struct list_head *list, /* lock lru list/PageCompound, ref freezed by page_ref_freeze */ lruvec = lock_page_lruvec(head); - ClearPageHasHWPoisoned(head); - for (i = nr - 1; i >= 1; i--) { __split_huge_page_tail(head, i, lruvec, list); /* Some pages can be beyond i_size: drop them from page cache */ diff --git a/mm/memory-failure.c b/mm/memory-failure.c index 9890f822d692..5562b00db788 100644 --- a/mm/memory-failure.c +++ b/mm/memory-failure.c @@ -1474,20 +1474,6 @@ int memory_failure(unsigned long pfn, int flags) } if (PageTransHuge(hpage)) { - /* - * The flag must be set after the refcount is bumped - * otherwise it may race with THP split. - * And the flag can't be set in get_hwpoison_page() since - * it is called by soft offline too and it is just called - * for !MF_COUNT_INCREASE. So here seems to be the best - * place. - * - * Don't need care about the above error handling paths for - * get_hwpoison_page() since they handle either free page - * or unhandlable page. The refcount is bumped iff the - * page is a valid handlable page. - */ - SetPageHasHWPoisoned(hpage); if (try_to_split_thp_page(p, "Memory Failure") < 0) { action_result(pfn, MF_MSG_UNSPLIT_THP, MF_IGNORED); res = -EBUSY; diff --git a/mm/memory.c b/mm/memory.c index c5c4edd74b5c..f7d2fc871423 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -3923,15 +3923,6 @@ vm_fault_t finish_fault(struct vm_fault *vmf) else page = vmf->page; - /* - * Just backoff if any subpage of a THP is corrupted otherwise - * the corrupted page may mapped by PMD silently to escape the - * check. This kind of THP just can be PTE mapped. Access to - * the corrupted subpage should trigger SIGBUS as expected. - */ - if (unlikely(PageHasHWPoisoned(page))) - return ret; - /* * check even for read faults because we might have lost our CoWed * page diff --git a/mm/page_alloc.c b/mm/page_alloc.c index a65a76a7fb5a..132c85fa0913 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -1236,10 +1236,8 @@ static __always_inline bool free_pages_prepare(struct page *page, VM_BUG_ON_PAGE(compound && compound_order(page) != order, page); - if (compound) { + if (compound) ClearPageDoubleMap(page); - ClearPageHasHWPoisoned(page); - } for (i = 1; i < (1 << order); i++) { if (compound) bad += free_tail_pages_check(page, page + i); From patchwork Mon Jan 10 17:31:44 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kamal Mostafa X-Patchwork-Id: 1578035 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=canonical.com header.i=@canonical.com header.a=rsa-sha256 header.s=20210705 header.b=MJU8NYgI; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=none (no SPF record) smtp.mailfrom=lists.ubuntu.com (client-ip=91.189.94.19; helo=huckleberry.canonical.com; envelope-from=kernel-team-bounces@lists.ubuntu.com; receiver=) Received: from huckleberry.canonical.com (huckleberry.canonical.com [91.189.94.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4JXgqf3ZW6z9sRR for ; Tue, 11 Jan 2022 04:31:58 +1100 (AEDT) Received: from localhost ([127.0.0.1] helo=huckleberry.canonical.com) by huckleberry.canonical.com with esmtp (Exim 4.86_2) (envelope-from ) id 1n6yWO-0004FE-Ri; Mon, 10 Jan 2022 17:31:52 +0000 Received: from smtp-relay-canonical-1.internal ([10.131.114.174] helo=smtp-relay-canonical-1.canonical.com) by huckleberry.canonical.com with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.86_2) (envelope-from ) id 1n6yWM-0004E6-BZ for kernel-team@lists.ubuntu.com; Mon, 10 Jan 2022 17:31:50 +0000 Received: from ascalon (unknown [192.188.8.63]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp-relay-canonical-1.canonical.com (Postfix) with ESMTPSA id 2BCDF3F130 for ; Mon, 10 Jan 2022 17:31:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=canonical.com; s=20210705; t=1641835910; bh=b2YbuQJEIu98IZnIMJ/Gt2DY17Nn2d/lDtHNkVP3c+0=; h=From:To:Subject:Date:Message-Id:In-Reply-To:References; b=MJU8NYgIpvSZw5BuZfM92cqTaHXJ01X74AKW6gmWx2Mm/QsYwFNBTb2M3uPjFziSh qWDMFq+9CcnhlLHYFeERJTuX1gx/rR24EeifxgtILjWYBhIoxi2s4OS2UOtQAh4/q/ vtiG+JkCIAFfx/f6bRavRS4t/mNOS70D9A54DDqwgWSYILKxORHi69c3KwhNxfqy+F /DlY5YtOo4+pcuI/0QXfctV/WZmQsBUAx7FLWZ14GXogywd8ChnBE5nm91MN23h3/l 5wRVBRGSMVOIoH0y9kBjrPWh6k2AXmK/xAIQqrUDQnol9a/pPMyhdI+Z/umjQWO91f RnKLdD4l3bQ2w== Received: from kamal by ascalon with local (Exim 4.90_1) (envelope-from ) id 1n6yWK-0003tv-CK for kernel-team@lists.ubuntu.com; Mon, 10 Jan 2022 09:31:48 -0800 From: Kamal Mostafa To: kernel-team@lists.ubuntu.com Subject: [Hirsute][PATCH 2/2] mm: filemap: check if THP has hwpoisoned subpage for PMD page fault Date: Mon, 10 Jan 2022 09:31:44 -0800 Message-Id: <20220110173144.14826-3-kamal@canonical.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220110173144.14826-1-kamal@canonical.com> References: <20220110173144.14826-1-kamal@canonical.com> X-BeenThere: kernel-team@lists.ubuntu.com X-Mailman-Version: 2.1.20 Precedence: list List-Id: Kernel team discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: kernel-team-bounces@lists.ubuntu.com Sender: "kernel-team" From: Yang Shi BugLink: https://bugs.launchpad.net/bugs/1956984 commit eac96c3efdb593df1a57bb5b95dbe037bfa9a522 upstream. 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. Link: https://lkml.kernel.org/r/20211020210755.23964-3-shy828301@gmail.com Fixes: 800d8c63b2e9 ("shmem: add huge pages support") Signed-off-by: Yang Shi Reviewed-by: Naoya Horiguchi Suggested-by: Kirill A. Shutemov Cc: Hugh Dickins Cc: Matthew Wilcox Cc: Oscar Salvador Cc: Peter Xu Cc: Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds Signed-off-by: Greg Kroah-Hartman Signed-off-by: Kamal Mostafa --- include/linux/page-flags.h | 23 +++++++++++++++++++++++ mm/huge_memory.c | 2 ++ mm/memory-failure.c | 14 ++++++++++++++ mm/memory.c | 9 +++++++++ mm/page_alloc.c | 4 +++- 5 files changed, 51 insertions(+), 1 deletion(-) diff --git a/include/linux/page-flags.h b/include/linux/page-flags.h index ec5d0290e0ee..9b85f492bf56 100644 --- a/include/linux/page-flags.h +++ b/include/linux/page-flags.h @@ -168,6 +168,15 @@ enum pageflags { /* Compound pages. Stored in first tail page's flags */ PG_double_map = PG_workingset, +#ifdef CONFIG_MEMORY_FAILURE + /* + * Compound pages. Stored in first tail page's flags. + * Indicates that at least one subpage is hwpoisoned in the + * THP. + */ + PG_has_hwpoisoned = PG_mappedtodisk, +#endif + /* non-lru isolated movable page */ PG_isolated = PG_reclaim, @@ -700,6 +709,20 @@ PAGEFLAG_FALSE(DoubleMap) TESTSCFLAG_FALSE(DoubleMap) #endif +#if defined(CONFIG_MEMORY_FAILURE) && defined(CONFIG_TRANSPARENT_HUGEPAGE) +/* + * PageHasHWPoisoned indicates that at least one subpage is hwpoisoned in the + * compound page. + * + * This flag is set by hwpoison handler. Cleared by THP split or free page. + */ +PAGEFLAG(HasHWPoisoned, has_hwpoisoned, PF_SECOND) + TESTSCFLAG(HasHWPoisoned, has_hwpoisoned, PF_SECOND) +#else +PAGEFLAG_FALSE(HasHWPoisoned) + TESTSCFLAG_FALSE(HasHWPoisoned) +#endif + /* * For pages that are never mapped to userspace (and aren't PageSlab), * page_type may be used. Because it is initialised to -1, we invert the diff --git a/mm/huge_memory.c b/mm/huge_memory.c index f735eb3755db..eab1ed3e9675 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -2499,6 +2499,8 @@ static void __split_huge_page(struct page *page, struct list_head *list, /* lock lru list/PageCompound, ref freezed by page_ref_freeze */ lruvec = lock_page_lruvec(head); + ClearPageHasHWPoisoned(head); + for (i = nr - 1; i >= 1; i--) { __split_huge_page_tail(head, i, lruvec, list); /* Some pages can be beyond i_size: drop them from page cache */ diff --git a/mm/memory-failure.c b/mm/memory-failure.c index 5562b00db788..9890f822d692 100644 --- a/mm/memory-failure.c +++ b/mm/memory-failure.c @@ -1474,6 +1474,20 @@ int memory_failure(unsigned long pfn, int flags) } if (PageTransHuge(hpage)) { + /* + * The flag must be set after the refcount is bumped + * otherwise it may race with THP split. + * And the flag can't be set in get_hwpoison_page() since + * it is called by soft offline too and it is just called + * for !MF_COUNT_INCREASE. So here seems to be the best + * place. + * + * Don't need care about the above error handling paths for + * get_hwpoison_page() since they handle either free page + * or unhandlable page. The refcount is bumped iff the + * page is a valid handlable page. + */ + SetPageHasHWPoisoned(hpage); if (try_to_split_thp_page(p, "Memory Failure") < 0) { action_result(pfn, MF_MSG_UNSPLIT_THP, MF_IGNORED); res = -EBUSY; diff --git a/mm/memory.c b/mm/memory.c index f7d2fc871423..260a1a58e201 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -3784,6 +3784,15 @@ static vm_fault_t do_set_pmd(struct vm_fault *vmf, struct page *page) if (compound_order(page) != HPAGE_PMD_ORDER) return ret; + /* + * Just backoff if any subpage of a THP is corrupted otherwise + * the corrupted page may mapped by PMD silently to escape the + * check. This kind of THP just can be PTE mapped. Access to + * the corrupted subpage should trigger SIGBUS as expected. + */ + if (unlikely(PageHasHWPoisoned(page))) + return ret; + /* * Archs like ppc64 need additonal space to store information * related to pte entry. Use the preallocated table for that. diff --git a/mm/page_alloc.c b/mm/page_alloc.c index 132c85fa0913..a65a76a7fb5a 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -1236,8 +1236,10 @@ static __always_inline bool free_pages_prepare(struct page *page, VM_BUG_ON_PAGE(compound && compound_order(page) != order, page); - if (compound) + if (compound) { ClearPageDoubleMap(page); + ClearPageHasHWPoisoned(page); + } for (i = 1; i < (1 << order); i++) { if (compound) bad += free_tail_pages_check(page, page + i);