From patchwork Sat Jul 8 08:57:44 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Miaohe Lin X-Patchwork-Id: 117424 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9f45:0:b0:3ea:f831:8777 with SMTP id v5csp3828979vqx; Sat, 8 Jul 2023 03:16:18 -0700 (PDT) X-Google-Smtp-Source: APBJJlF7rQ+EFiid5WFTy2eZHR6XUq0wEI7L6lYZ4H4JhiAe2lEcHM9V/ZoFj+pIPMDSwSj20flZ X-Received: by 2002:a50:e606:0:b0:51e:d96:15e4 with SMTP id y6-20020a50e606000000b0051e0d9615e4mr6042913edm.19.1688811378249; Sat, 08 Jul 2023 03:16:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1688811378; cv=none; d=google.com; s=arc-20160816; b=JypQ6Stad4o/wDWzv7lwxLiYRXACx8d30JKU1Q5o4rjW8AVof4+8uDzVUnr8syWmwx znDIcsUQbr9ZUFsx3KGtX32n6u2P9iozbdu3iBGXQbIK+LsHDaKyaQQb5ApY8yS3hMME ulcOtW6BdTNJkQrsllMA8EJfCP2hk7EENt3kj1DK3ke8sqar3fgxlpPdOv4knF+JuogU +O2xF+S3iYk3A24rcVgp8TOZpbtVG4MtSFaYGOqshugxo09nW+yPJFG/q7qbEq/yv/fv uqxrOwtuAIIXdUeESU9Fk1QV1E1+1zgxAK4Ym8lOl/5N/uuYei41jWoWwEfEmbQG8ZHY Mehw== 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=Qv7v0MakVggbJJx0U+5+eQj6+hfyeFZeeQvIWRA1lsE=; fh=4o7DIXI/66oHEW09L3854GeQKXh9nrvBy5ZxENMFBvA=; b=FfpzLWFgIqiLgQIFDnBfvQ8js7/nXVj4NyouH6PIQ2bDGkHz4JpH0BdkUNIAvpxrwI OWn/sjVtwHOl0IZx0HEkOZw64yB8a6FOZCvoHghbfHaD0iISh5uiZoVzZRI/IzAHdjBG 0hJ9x/zvMNLu9+nbzB3TAq9xjuqJF4ra6y/pzm2EeMUrNY21N28HjMeYRf2rfD83mt8R qG49YWVCzQSmE/WYhX5RRDx4Fy51osiv1UNSzQqAF9A0GIYEKey3XBsNO+yOOhbNUbyQ b5j57UOw7OtoEn0+yuqLnQ9D02BxKL3f/TT+JUE2Mhc4CopY8/WOcCkW+cM8RKdkFEAh ImQQ== 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 d4-20020a50fb04000000b0051e26731172si3190490edq.672.2023.07.08.03.15.43; Sat, 08 Jul 2023 03:16:18 -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 S230122AbjGHI5h (ORCPT + 99 others); Sat, 8 Jul 2023 04:57:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40766 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229874AbjGHI5f (ORCPT ); Sat, 8 Jul 2023 04:57:35 -0400 Received: from szxga08-in.huawei.com (szxga08-in.huawei.com [45.249.212.255]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E88F22105 for ; Sat, 8 Jul 2023 01:57:33 -0700 (PDT) Received: from canpemm500002.china.huawei.com (unknown [172.30.72.57]) by szxga08-in.huawei.com (SkyGuard) with ESMTP id 4QykfQ5qvgz1FDgw; Sat, 8 Jul 2023 16:57:02 +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; Sat, 8 Jul 2023 16:57:31 +0800 From: Miaohe Lin To: , CC: , , Subject: [PATCH 8/8] mm: memory-failure: fix race window when trying to get hugetlb folio Date: Sat, 8 Jul 2023 16:57:44 +0800 Message-ID: <20230708085744.3599311-9-linmiaohe@huawei.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20230708085744.3599311-1-linmiaohe@huawei.com> References: <20230708085744.3599311-1-linmiaohe@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.174.151.185] X-ClientProxiedBy: dggems706-chm.china.huawei.com (10.3.19.183) To canpemm500002.china.huawei.com (7.192.104.244) X-CFilter-Loop: Reflected X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_MED, 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: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1770847079708933741?= X-GMAIL-MSGID: =?utf-8?q?1770847079708933741?= 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 | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/mm/memory-failure.c b/mm/memory-failure.c index 76d88d27cdbe..066bf57f2d22 100644 --- a/mm/memory-failure.c +++ b/mm/memory-failure.c @@ -1388,8 +1388,14 @@ 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) { + 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 @@ -1478,8 +1484,12 @@ 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) { + if (folio == page_folio(page)) + return ret; + if (ret > 0) + folio_put(folio); + } /* * PageHWPoisonTakenOff pages are not only marked as PG_hwpoison,