Received: by 2002:ac0:e350:0:0:0:0:0 with SMTP id g16csp175456imn; Thu, 4 Aug 2022 01:17:41 -0700 (PDT) X-Google-Smtp-Source: AA6agR6R5c/LgkFe2U3Eh+rcDhg/LCjYYypPORcnu5fq+iDFg5yAgnxqTvQpAkMeBvuyf8ypZEG9 X-Received: by 2002:a17:907:6d04:b0:72e:e204:a70b with SMTP id sa4-20020a1709076d0400b0072ee204a70bmr587839ejc.432.1659601061642; Thu, 04 Aug 2022 01:17:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1659601061; cv=none; d=google.com; s=arc-20160816; b=Tu8d0UdCFBW98PrxhX3NCuaw3or879Zjpo4ZJLjmg2Jjg9dGQnx2+kGJdJLUO3ICK6 DJ3KSvkL4ZKH4RVIJSlCJZh6swFvan0OTR4zbTcvQkl21Tfmbwsfc1Eh03PQeDFX8DUf Ib5ukP15Yo6OxOOV7zKmOi4gVroJBoFAxCXEMqDMD+Een1tbrTW94NYUs3uoyR/So4Of oDvjm0wDtvBr8V9Q+b0bxD8w0vlZdjfnWf99EKx4aT+8Me+hbsN5acZVjeMzyPV4zyVl xu6itDf5YEmTPH9zTIQbaRNsYEL9nEykvGQso1OWVl5Z166yv1aX0ta2/2DntiHxTUI4 foAw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:dkim-signature:date; bh=7zrMafA/86PiYIeMqh2hEYMbncktkYrbFlWU8+oQQI0=; b=l4a4nrRy04b+9qHSDaKq+RPENd1ichW04YSEFKqqk4VFRlqCA4cBt5kkzpgBffBhW/ jMdc5c/Z2Y7YTEyVxurKcbXCKLkn69L1wDuOge/Tj0WazX7a9W8I3AIe1ZvTnHiJsYjp YM68U7RPwlnuLk2L6bQMSE2BPR82P30NOtk5g76rP9cZtRdx5123xjcYKR96Doec4VGa tDKQ9KrE2vn7JhvCC4Wj9gBAIl4BmzDXPRCaRfJNLjyMIt8YEeKCbyh8Qz/+3a/M2ssv +7AgwZvdu5ffvfZZBKjEgPI2AndOXQ1j4F/sl0nY8g+Bz8YSTNJmZsPZe8s6mLCbLn/6 Xj1g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=FQyYAptT; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id jx2-20020a170907760200b0072af54e038fsi190358ejc.286.2022.08.04.01.17.15; Thu, 04 Aug 2022 01:17:41 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=FQyYAptT; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239207AbiHDHpC (ORCPT + 99 others); Thu, 4 Aug 2022 03:45:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33158 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232405AbiHDHox (ORCPT ); Thu, 4 Aug 2022 03:44:53 -0400 Received: from out0.migadu.com (out0.migadu.com [IPv6:2001:41d0:2:267::]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1B62D61D87 for ; Thu, 4 Aug 2022 00:44:52 -0700 (PDT) Date: Thu, 4 Aug 2022 16:44:44 +0900 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1659599090; 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=7zrMafA/86PiYIeMqh2hEYMbncktkYrbFlWU8+oQQI0=; b=FQyYAptT4/w13rMekB6dEv9UZSjfysjISClpqkD/C5OxGMYgOZDlBo2jel9xya/A9+iScI ckQAb7wocLF4PvBNSxy1yXonhk/rh+2D4AYgWNXEseRMs/Z6K916WmOTB4FwB555xvNizg 3+fXCtUV6/oNO2h27/tWCuHnX8mf9Ps= 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: <20220804074444.GB2551573@ik1-406-35019.vs.sakura.ne.jp> References: <3c542543-0965-ef60-4627-1a4116077a5b@huawei.com> <20220804074025.GA2551573@ik1-406-35019.vs.sakura.ne.jp> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <20220804074025.GA2551573@ik1-406-35019.vs.sakura.ne.jp> X-Migadu-Flow: FLOW_OUT X-Migadu-Auth-User: linux.dev X-Spam-Status: No, score=-2.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_LOW,SPF_HELO_PASS, SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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. - 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. > > Thanks, > Naoya Horiguchi