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 99660C19F2D for ; Fri, 5 Aug 2022 02:33:23 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1DCDA8E0003; Thu, 4 Aug 2022 22:33:23 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 1990C8E0001; Thu, 4 Aug 2022 22:33:23 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 056088E0003; Thu, 4 Aug 2022 22:33:23 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id E9CA18E0001 for ; Thu, 4 Aug 2022 22:33:22 -0400 (EDT) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id C15D21C71F1 for ; Fri, 5 Aug 2022 02:33:22 +0000 (UTC) X-FDA: 79763967444.09.F981C49 Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by imf29.hostedemail.com (Postfix) with ESMTP id 09DDF120118 for ; Fri, 5 Aug 2022 02:33:20 +0000 (UTC) Received: from canpemm500002.china.huawei.com (unknown [172.30.72.55]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4LzV1c5Z2zzjXl1; Fri, 5 Aug 2022 10:30:12 +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.2375.24; Fri, 5 Aug 2022 10:33:17 +0800 Subject: Re: [bug report] mm, hwpoison: memory_failure races with alloc_fresh_huge_page/free_huge_page To: Naoya Horiguchi CC: Muchun Song , Linux-MM , linux-kernel , Andrew Morton , Mike Kravetz References: <3c542543-0965-ef60-4627-1a4116077a5b@huawei.com> <20220804074025.GA2551573@ik1-406-35019.vs.sakura.ne.jp> <20220804074444.GB2551573@ik1-406-35019.vs.sakura.ne.jp> From: Miaohe Lin Message-ID: Date: Fri, 5 Aug 2022 10:33:16 +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: <20220804074444.GB2551573@ik1-406-35019.vs.sakura.ne.jp> Content-Type: text/plain; charset="utf-8" Content-Language: en-US Content-Transfer-Encoding: 7bit X-Originating-IP: [10.174.177.76] X-ClientProxiedBy: dggems703-chm.china.huawei.com (10.3.19.180) To canpemm500002.china.huawei.com (7.192.104.244) X-CFilter-Loop: Reflected ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=none; spf=pass (imf29.hostedemail.com: domain of linmiaohe@huawei.com designates 45.249.212.187 as permitted sender) smtp.mailfrom=linmiaohe@huawei.com; dmarc=pass (policy=quarantine) header.from=huawei.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1659666802; a=rsa-sha256; cv=none; b=HuqRNjKTW+7S6q3fv0/1cSntgqgzmcF0XeX807cYeq3r0YP7yz9Rmis2wJlXRkTKz1xcL1 HV6T8iprkKHFBGEuNH7FWqANwnKdElFItNasZeMyLAg5aBre+ihvxa5YDm+nsrXBLQ3IO4 VFR5L0v8II0d6O3+mc0+HU77xfh3fQE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1659666802; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=LBPxnU5MuHGTEQHfbmBeI0g1mfOeFa+abhKd74E81qs=; b=2xkPx2OsOg+SRPXkQ2gUjSX1Jl3hvuDBk1+Exg8O4OJQOQwlf/58zNNEWPq3NVbbfXeDvF Zs6wmD2ez5jUTqfLXodb2zc1zcT4pvvXYZo+ZvwUxciAfFIb2VETbuLyeyvjnd0bBV0GXG 9K04/k+XYaUWhqu96amgPrb2bh9ccp8= X-Stat-Signature: qnos83hseo786swtdy4hkh4zi7epzmk7 X-Rspamd-Queue-Id: 09DDF120118 Authentication-Results: imf29.hostedemail.com; dkim=none; spf=pass (imf29.hostedemail.com: domain of linmiaohe@huawei.com designates 45.249.212.187 as permitted sender) smtp.mailfrom=linmiaohe@huawei.com; dmarc=pass (policy=quarantine) header.from=huawei.com X-Rspam-User: X-Rspamd-Server: rspam12 X-HE-Tag: 1659666800-401361 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/8/4 15:44, Naoya Horiguchi wrote: > On Thu, Aug 04, 2022 at 04:40:25PM +0900, Naoya Horiguchi wrote: >> On Tue, Aug 02, 2022 at 02:27:36PM +0800, Miaohe Lin wrote: >>> On 2022/8/2 12:07, Muchun Song wrote: >>>> On Tue, Aug 02, 2022 at 10:00:50AM +0800, Miaohe Lin wrote: >>>>> Hi all: >>>>> When I investigate the mm/memory-failure.c code again, I found there's a possible race window >>>>> between memory_failure and alloc_fresh_huge_page/free_huge_page. Thank about the below scene: >>>>> >>>>> CPU 1 CPU 2 >>>>> alloc_fresh_huge_page -- page refcnt > 0 memory_failure >>>>> prep_new_huge_page get_huge_page_for_hwpoison >>>>> !PageHeadHuge -- so 2(not a hugepage) is returned >>>>> hugetlb_vmemmap_optimize -- subpages is read-only >>>>> set_compound_page_dtor -- PageHuge is true now, but too late!!! >>>>> TestSetPageHWPoison(p) >>>>> -- We might write to read-only subpages here!!! >>>>> >>>>> Another similar scene: >>>>> >>>>> CPU 1 CPU 2 >>>>> free_huge_page -- page refcnt == 0 and not PageHuge memory_failure >>>>> get_huge_page_for_hwpoison >>>>> !PageHeadHuge -- so 2(not a hugepage) is returned >>>>> TestSetPageHWPoison(p) >>>>> -- We might write to read-only subpages here!!! >>>>> hugetlb_vmemmap_restore -- subpages can be written to now, but too late!!! >>>>> >>>> >>>> I agree this race is possible, I have proposed this race in thread [1]. >> >> Thank you for reminding this, and I agree that we need some solution. >> >>> >>> Oh, I remember I see the race proposed in [1] but I did not look into that carefully at that time. Sorry. >>> >>>> But I didn't think more how to solve it. >>> I hope this thread can find a good solution. :) >> >> Both of the races show that __get_huge_page_for_hwpoison() fails to >> capture the case of generic compound page during turning into hugetlb, > > I noticed my wrong description just after sending this out. The second race > is about generic compound page turning into "buddy", sorry. Never mind. Thanks for your thought. > > - Naoya Horiguchi > >> What makes things complicated is that no one knows to which state >> such a compound page finally turns into. So I think that if the page >> to be handled is an unknown compound page, we need to wait until it >> becomes some known page state to avoid misjudging. >> >> If we need a quick small fix, we may replace the check "!PageHeadHuge()" >> in __get_huge_page_for_hwpoison() with "!PageCompound()", and add another >> retry path in get_huge_page_for_hwpoison() for non-hugetlb compound pages. Sounds like a good idea. If we can figure out a stable way to tell it from other compound page, it will be worth fixing it. Let's have a try. :) >> >> Thanks, >> Naoya Horiguchi Many thanks for your suggestion, Naoya. > . >