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 8147EC19F2C for ; Thu, 4 Aug 2022 07:40:36 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id ABE0E8E0002; Thu, 4 Aug 2022 03:40:35 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A6DBC8E0001; Thu, 4 Aug 2022 03:40:35 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9344C8E0002; Thu, 4 Aug 2022 03:40:35 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 8201B8E0001 for ; Thu, 4 Aug 2022 03:40:35 -0400 (EDT) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 3C245ABF7B for ; Thu, 4 Aug 2022 07:40:35 +0000 (UTC) X-FDA: 79761112830.08.B8091BB Received: from out1.migadu.com (out1.migadu.com [91.121.223.63]) by imf01.hostedemail.com (Postfix) with ESMTP id 5FC4C4012F for ; Thu, 4 Aug 2022 07:40:34 +0000 (UTC) Date: Thu, 4 Aug 2022 16:40:25 +0900 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1659598832; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=aKocoFOHQjUWCKVWggVJ2diOKUCvDqNI0lkZdeg2bgA=; b=i9z7N0lExHqwmkveUOibcw3zZZk0YbJ90WJB3O858Gpqzft3a0kkWOFQj8zmJvOzvcIDtE mbvuw/O43psDK87O1qnOBvSsdZ5tUncP0EDzG5ZdtSFBurwyXRbknQQBWGZiLGsZZXFptI 8wpOloER93fRmvRiOZyL/P1FmU5mwss= X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Naoya Horiguchi To: Miaohe Lin Cc: Muchun Song , Linux-MM , linux-kernel , Andrew Morton , Mike Kravetz Subject: Re: [bug report] mm, hwpoison: memory_failure races with alloc_fresh_huge_page/free_huge_page Message-ID: <20220804074025.GA2551573@ik1-406-35019.vs.sakura.ne.jp> References: <3c542543-0965-ef60-4627-1a4116077a5b@huawei.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: X-Migadu-Flow: FLOW_OUT X-Migadu-Auth-User: linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1659598834; a=rsa-sha256; cv=none; b=6KEmlRSQrc1g7kj92ODc3N3tnKpASivnjffUbyZ6YBf17DFp30Makex7HAcqQaYpyzlHPs dQGofGna3Gq4Ho4Z74f+ESdRvePYeJ9V8R5wlFSXT+Cn8fdEURz8wMlkS/ArqPOOgBd5UZ 8RqeuhtrVK/PWjjVX7Aesak4OXHwzIQ= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=i9z7N0lE; spf=pass (imf01.hostedemail.com: domain of naoya.horiguchi@linux.dev designates 91.121.223.63 as permitted sender) smtp.mailfrom=naoya.horiguchi@linux.dev; dmarc=pass (policy=none) header.from=linux.dev ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1659598834; 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: in-reply-to:in-reply-to:references:references:dkim-signature; bh=aKocoFOHQjUWCKVWggVJ2diOKUCvDqNI0lkZdeg2bgA=; b=YFsjETh7jlDfQtSY0JxSklEewoOj3P6cjhACHasYDmmJhTJjMrqxwZvDsyA8GiRW5ZqPNk 3s16WiUq9MqKL8wNi7bgH8iqpylUmupynIbmg7DyUuJYtLzIx6xKmKmaSfsTfs1IYjYXBd 6GcjArpS/2hDbZVCXF6t3Z5Rga2Lnr8= X-Stat-Signature: 13ie8ucbfxr7ujth1itw3u6eau3j9rwn X-Rspamd-Queue-Id: 5FC4C4012F Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=i9z7N0lE; spf=pass (imf01.hostedemail.com: domain of naoya.horiguchi@linux.dev designates 91.121.223.63 as permitted sender) smtp.mailfrom=naoya.horiguchi@linux.dev; dmarc=pass (policy=none) header.from=linux.dev X-Rspam-User: X-Rspamd-Server: rspam11 X-HE-Tag: 1659598834-336340 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 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, 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. Thanks, Naoya Horiguchi > > > > > [1] https://lore.kernel.org/linux-mm/20220623235153.2623702-1-naoya.horiguchi@linux.dev/T/#ma094a7cea7df8fd9a77a91551bf39077d89e23bd > > > >> I think the above scenes are possible. But I can't found a stable solution to fix it. Any suggestions? > >> Or is it not worth to fix it as it's too rare? Or am I miss something? > >> > > > > Luckily, the system will panic at once, which encountering this race. However, > > we don't see any bug report. If we have an easy way to fix it, I think it is worth. > > Agree. But I can't find a easy way to fix it yet. > > > Just a quick reply, no suggestion/solutions from me. > > Many thanks for your quick reply. > > > > > Thanks. > > > >> Any response would be appreciated! > >> > >> Thanks! > >> > > . > > >