Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp36351957rwd; Mon, 10 Jul 2023 23:05:30 -0700 (PDT) X-Google-Smtp-Source: APBJJlG1bseG07pdWn7pXK667RWnv9c8xG7ylWdBEv894EgbAhjsMtoz0ufkUPoXOF/wm/PT7d3/ X-Received: by 2002:a17:902:d2cf:b0:1b8:3936:7b64 with SMTP id n15-20020a170902d2cf00b001b839367b64mr20978508plc.1.1689055529804; Mon, 10 Jul 2023 23:05:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689055529; cv=none; d=google.com; s=arc-20160816; b=ntvK0zL7ALFtCHAhAl+RJ1mrRJScEsRTZ0cMr3M9LkgFteGi2HcfS7BtVQt8XAts9a KxLBzFDrhzk380cxZ0IDlzow1OJDGvm0cy2EZu84GP9ZuZMNW/27n1I+avok1aWSuEgz ONCWXJ3TtXkgj2qFckfk8RwPafGZwmCnOOYirOz/2i7cDgO3rP7jbHAiamywrUQNXh53 q9rGJ8EJOvuUFgtSSjwtfOR/Kmj9A/eO9Xi/qlj2n4VevuMa1rkqUi2CHQcZ9cgO5Uvl hzbqmbYoLT+zm/aoxCWV/H2fCDCTww34muh6d+NIA/J6vwyq0fJrtCUuEzF9JJvQa3yC p6TQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=kZxpc3LLQdKRwi1qZzsu5rtB5wD/2h93r2whajVTytA=; fh=4o7DIXI/66oHEW09L3854GeQKXh9nrvBy5ZxENMFBvA=; b=Q9s4TrpCuooZdk+7VIfcvmeo3AoVURIQln6X2Rqe0DUwhwkYkNT6xGH94cLs4hmWTB jWACSFjvXp1+mXSnB6EPztPqsLTLDkXnkiBh50M7kc5MFKAvXPPTOXRWyRdTf2Wj+AU2 t1xmAyEwkHRMG4+5jN172orjt5hYYTZOWzKqUjycnGghaMH7fNPK70B+FuVnrSkOaLC+ +zaqlxBjc/jm96tE7Lkn7PTi43J1I3zh8Q95wDyxvPeDAJi37py6/nk1NVc14l36Q2wK 9QAy3xyxkc6O5px9/lBG+RlQls69RsKuvGwKzDOFXaUimynUiZQd6DCc6NUSg+hi48AZ nkFg== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id x6-20020a170902ec8600b001b23d4573b2si1060350plg.27.2023.07.10.23.05.16; Mon, 10 Jul 2023 23:05:29 -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; 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=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229551AbjGKFu2 (ORCPT + 99 others); Tue, 11 Jul 2023 01:50:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34846 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230015AbjGKFuI (ORCPT ); Tue, 11 Jul 2023 01:50:08 -0400 Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3FF441BC for ; Mon, 10 Jul 2023 22:50:07 -0700 (PDT) Received: from canpemm500002.china.huawei.com (unknown [172.30.72.55]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4R0VHv0DmQztR5C; Tue, 11 Jul 2023 13:47:07 +0800 (CST) Received: from huawei.com (10.174.151.185) 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.2507.27; Tue, 11 Jul 2023 13:50:04 +0800 From: Miaohe Lin To: , CC: , , Subject: [PATCH v2 8/8] mm: memory-failure: fix race window when trying to get hugetlb folio Date: Tue, 11 Jul 2023 13:50:16 +0800 Message-ID: <20230711055016.2286677-9-linmiaohe@huawei.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20230711055016.2286677-1-linmiaohe@huawei.com> References: <20230711055016.2286677-1-linmiaohe@huawei.com> MIME-Version: 1.0 Content-Transfer-Encoding: 7BIT Content-Type: text/plain; charset=US-ASCII X-Originating-IP: [10.174.151.185] X-ClientProxiedBy: dggems705-chm.china.huawei.com (10.3.19.182) To canpemm500002.china.huawei.com (7.192.104.244) X-CFilter-Loop: Reflected X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_BLOCKED,RCVD_IN_MSPIKE_H5,RCVD_IN_MSPIKE_WL, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE 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 page_folio() is fetched before calling get_hwpoison_hugetlb_folio() without hugetlb_lock being held. So hugetlb page could be demoted before get_hwpoison_hugetlb_folio() holding hugetlb_lock but after page_folio() is fetched. So get_hwpoison_hugetlb_folio() will hold unexpected extra refcnt of hugetlb folio while leaving demoted page un-refcnted. Fixes: 25182f05ffed ("mm,hwpoison: fix race with hugetlb page allocation") Signed-off-by: Miaohe Lin Acked-by: Naoya Horiguchi --- mm/memory-failure.c | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/mm/memory-failure.c b/mm/memory-failure.c index af34fd4669d3..9ab97016877e 100644 --- a/mm/memory-failure.c +++ b/mm/memory-failure.c @@ -1383,8 +1383,15 @@ static int __get_hwpoison_page(struct page *page, unsigned long flags) bool hugetlb = false; ret = get_hwpoison_hugetlb_folio(folio, &hugetlb, false); - if (hugetlb) - return ret; + if (hugetlb) { + /* Make sure hugetlb demotion did not happen from under us. */ + if (folio == page_folio(page)) + return ret; + if (ret > 0) { + folio_put(folio); + folio = page_folio(page); + } + } /* * This check prevents from calling folio_try_get() for any @@ -1473,8 +1480,13 @@ static int __get_unpoison_page(struct page *page) bool hugetlb = false; ret = get_hwpoison_hugetlb_folio(folio, &hugetlb, true); - if (hugetlb) - return ret; + if (hugetlb) { + /* Make sure hugetlb demotion did not happen from under us. */ + if (folio == page_folio(page)) + return ret; + if (ret > 0) + folio_put(folio); + } /* * PageHWPoisonTakenOff pages are not only marked as PG_hwpoison, -- 2.33.0