From patchwork Tue Jul 11 05:50:16 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Miaohe Lin X-Patchwork-Id: 118269 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:a6b2:0:b0:3e4:2afc:c1 with SMTP id c18csp267525vqm; Mon, 10 Jul 2023 23:01:42 -0700 (PDT) X-Google-Smtp-Source: APBJJlEZ/4ZJ3pfln2cEDaTVXBj88FJUKU/P/t1YIBZMNlVzqi6sHbwzN2BU+ZAMTujGDaA9xU8n X-Received: by 2002:a05:6a20:9383:b0:12e:c5fb:757 with SMTP id x3-20020a056a20938300b0012ec5fb0757mr23974391pzh.5.1689055301952; Mon, 10 Jul 2023 23:01:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689055301; cv=none; d=google.com; s=arc-20160816; b=ankENFTHnbcuqTZAwji+NgmJe4CHT8kjd2vg/OUY8/g+dxyAdQY3aJq+DF8I4YoVcw QiNqr+rFCul6MsA4g77r0mjNLJtibxXUzwv1O0jtzBXVyOEQHBRKqiJ2PMF4N6wUS/09 ZqFhKZ633Vr5sEMoloj0rFE2FdAizEg0PXafIVsSwIcv2h9g3OZgfoDIB3v2hPT43euH qOzzPHJaBpZejWBaEqXb9yWGgAtu7vSG/BxcpGet7KAxKUzXtaSJ9U5QPshTZP8+P7wF V9AF3KG6OYddS5LfMAnfL2VaZ9GkngyzOO0pWvE2zQa7LVjTdHjHI920OsKUVE1QQds/ BLTA== 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 Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id h184-20020a6383c1000000b0055b52bc1cabsi855475pge.196.2023.07.10.23.01.21; Mon, 10 Jul 2023 23:01: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; 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 S230266AbjGKFu3 (ORCPT + 99 others); Tue, 11 Jul 2023 01:50:29 -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 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 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1771102852280582523 X-GMAIL-MSGID: 1771102852280582523 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,