From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6F664C433F5 for ; Thu, 10 Mar 2022 06:24:04 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D0B5B8D0003; Thu, 10 Mar 2022 01:24:03 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id CB9698D0001; Thu, 10 Mar 2022 01:24:03 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BD0D58D0003; Thu, 10 Mar 2022 01:24:03 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (relay.a.hostedemail.com [64.99.140.24]) by kanga.kvack.org (Postfix) with ESMTP id AE2308D0001 for ; Thu, 10 Mar 2022 01:24:03 -0500 (EST) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay11.hostedemail.com (Postfix) with ESMTP id 89026806CE for ; Thu, 10 Mar 2022 06:24:03 +0000 (UTC) X-FDA: 79227486366.15.2D87B7A Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by imf23.hostedemail.com (Postfix) with ESMTP id 40D1014000B for ; Thu, 10 Mar 2022 06:24:02 +0000 (UTC) Received: from canpemm500002.china.huawei.com (unknown [172.30.72.56]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4KDf6338g4zcZys; Thu, 10 Mar 2022 14:19:07 +0800 (CST) Received: from [10.174.177.76] (10.174.177.76) by canpemm500002.china.huawei.com (7.192.104.244) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2308.21; Thu, 10 Mar 2022 14:23:57 +0800 Subject: Re: [PATCH v1] mm/hwpoison: set PageHWPoison after taking page lock in memory_failure_hugetlb() To: Yang Shi , =?UTF-8?B?SE9SSUdVQ0hJIE5BT1lBKOWggOWPoyDnm7TkuZ8p?= CC: Naoya Horiguchi , Linux MM , Andrew Morton , Mike Kravetz , Linux Kernel Mailing List References: <20220309091449.2753904-1-naoya.horiguchi@linux.dev> <20220310000024.GA1577304@hori.linux.bs1.fc.nec.co.jp> From: Miaohe Lin Message-ID: <2a61c5c1-a99b-9da9-b319-90f95a420ab1@huawei.com> Date: Thu, 10 Mar 2022 14:23:56 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Thunderbird/78.6.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset="utf-8" Content-Language: en-US Content-Transfer-Encoding: 8bit X-Originating-IP: [10.174.177.76] X-ClientProxiedBy: dggems704-chm.china.huawei.com (10.3.19.181) To canpemm500002.china.huawei.com (7.192.104.244) X-CFilter-Loop: Reflected X-Rspamd-Server: rspam11 X-Rspamd-Queue-Id: 40D1014000B X-Rspam-User: Authentication-Results: imf23.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf23.hostedemail.com: domain of linmiaohe@huawei.com designates 45.249.212.187 as permitted sender) smtp.mailfrom=linmiaohe@huawei.com X-Stat-Signature: oux3nnaqhmnti3tqg8kwgsutsx7waxxq X-HE-Tag: 1646893442-995011 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On 2022/3/10 8:30, Yang Shi wrote: > On Wed, Mar 9, 2022 at 4:01 PM HORIGUCHI NAOYA(堀口 直也) > wrote: >> >> On Wed, Mar 09, 2022 at 01:55:30PM -0800, Yang Shi wrote: >>> On Wed, Mar 9, 2022 at 1:15 AM Naoya Horiguchi >>> wrote: >>>> >>>> From: Naoya Horiguchi >>>> >>>> There is a race condition between memory_failure_hugetlb() and hugetlb >>>> free/demotion, which causes setting PageHWPoison flag on the wrong page >>>> (which was a hugetlb when memory_failrue() was called, but was removed >>>> or demoted when memory_failure_hugetlb() is called). This results in >>>> killing wrong processes. So set PageHWPoison flag with holding page lock, >>>> >>>> Signed-off-by: Naoya Horiguchi >>>> --- >>>> mm/memory-failure.c | 27 ++++++++++++--------------- >>>> 1 file changed, 12 insertions(+), 15 deletions(-) >>>> >>>> diff --git a/mm/memory-failure.c b/mm/memory-failure.c >>>> index ac6492e36978..fe25eee8f9d6 100644 >>>> --- a/mm/memory-failure.c >>>> +++ b/mm/memory-failure.c >>>> @@ -1494,24 +1494,11 @@ static int memory_failure_hugetlb(unsigned long pfn, int flags) >>>> int res; >>>> unsigned long page_flags; >>>> >>>> - if (TestSetPageHWPoison(head)) { >>>> - pr_err("Memory failure: %#lx: already hardware poisoned\n", >>>> - pfn); >>>> - res = -EHWPOISON; >>>> - if (flags & MF_ACTION_REQUIRED) >>>> - res = kill_accessing_process(current, page_to_pfn(head), flags); >>>> - return res; >>>> - } >>>> - >>>> - num_poisoned_pages_inc(); >>>> - >>>> if (!(flags & MF_COUNT_INCREASED)) { >>>> res = get_hwpoison_page(p, flags); >>> >>> I'm not an expert of hugetlb, I may be wrong. I'm wondering how this >>> could solve the race? Is the below race still possible? >>> >>> __get_hwpoison_page() >>> head = compound_head(page) >>> >>> hugetlb demotion (1G --> 2M) >>> get_hwpoison_huge_page(head, &hugetlb); >> >> Thanks for the comment. >> I assume Miaohe's patch below introduces additional check to detect the >> race. The patch calls compound_head() for the raw error page again, so >> the demotion case should be detected. I'll make the dependency clear in >> the commit log. >> >> https://lore.kernel.org/linux-mm/20220228140245.24552-2-linmiaohe@huawei.com/ >> >>> >>> >>> Then the head may point to a 2M page, but the hwpoisoned subpage is >>> not in that 2M range? >>> >>> >>>> if (!res) { >>>> lock_page(head); >>>> if (hwpoison_filter(p)) { >>>> - if (TestClearPageHWPoison(head)) >>>> - num_poisoned_pages_dec(); >>>> unlock_page(head); >>>> return -EOPNOTSUPP; >>>> } >>>> @@ -1544,13 +1531,16 @@ static int memory_failure_hugetlb(unsigned long pfn, int flags) >>>> page_flags = head->flags; >>>> >>>> if (hwpoison_filter(p)) { >>>> - if (TestClearPageHWPoison(head)) >>>> - num_poisoned_pages_dec(); >>>> put_page(p); >>>> res = -EOPNOTSUPP; >>>> goto out; >>>> } >>>> >>>> + if (TestSetPageHWPoison(head)) >>> >>> And I don't think "head" is still the head you expected if the race >>> happened. I think we need to re-retrieve the head once the page >>> refcount is bumped and locked. >> >> I think the above justification works for this. >> When the kernel reaches this line, the hugepage is properly pinned without being >> freed or demoted, so "head" is still pointing to the same head page as expected. > > I think Mike's comment in the earlier email works for this too. The > huge page may get demoted before the page is pinned and locked, so the > actual hwpoisoned subpage may belong to another smaller huge page now. > I thinks Naoya assumes that there is a check before we use "head": diff --git a/mm/memory-failure.c b/mm/memory-failure.c index 5444a8ef4867..0d7c58340a98 100644 --- a/mm/memory-failure.c +++ b/mm/memory-failure.c @@ -1534,6 +1534,17 @@ static int memory_failure_hugetlb(unsigned long pfn, int flags) } lock_page(head); + + /** + * The page could have changed compound pages due to race window. + * If this happens just bail out. + */ + if (!PageHuge(p) || compound_head(p) != head) { + action_result(pfn, MF_MSG_DIFFERENT_COMPOUND, MF_IGNORED); + res = -EBUSY; + goto out; + } + page_flags = head->flags; if (hwpoison_filter(p)) { -- from: https://lore.kernel.org/linux-mm/20220228140245.24552-2-linmiaohe@huawei.com/ Thanks. > >> >> Thanks, >> Naoya Horiguchi >> >>> >>>> + goto already_hwpoisoned; >>>> + >>>> + num_poisoned_pages_inc(); >>>> + >>>> /* >>>> * TODO: hwpoison for pud-sized hugetlb doesn't work right now, so >>>> * simply disable it. In order to make it work properly, we need >>>> @@ -1576,6 +1566,13 @@ static int memory_failure_hugetlb(unsigned long pfn, int flags) >>>> out: >>>> unlock_page(head); >>>> return res; >>>> +already_hwpoisoned: >>>> + unlock_page(head); >>>> + pr_err("Memory failure: %#lx: already hardware poisoned\n", pfn); >>>> + res = -EHWPOISON; >>>> + if (flags & MF_ACTION_REQUIRED) >>>> + res = kill_accessing_process(current, page_to_pfn(head), flags); >>>> + return res; >>>> } >>>> >>>> static int memory_failure_dev_pagemap(unsigned long pfn, int flags, >>>> -- >>>> 2.25.1 >>>> > . >