Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp32987958rwd; Sat, 8 Jul 2023 03:03:57 -0700 (PDT) X-Google-Smtp-Source: APBJJlFRhdxVZ8Q2d7Zhn6Q2xINq+LzDIC31eLLvJRm6uZRhXtnVS2837ifGGulIjUJyW3xhs13R X-Received: by 2002:a05:6a20:2595:b0:126:5ffd:c5b7 with SMTP id k21-20020a056a20259500b001265ffdc5b7mr11357523pzd.22.1688810636818; Sat, 08 Jul 2023 03:03:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1688810636; cv=none; d=google.com; s=arc-20160816; b=Tj1Y3GbHTBlOzU5FgexO3ebIrnI4I6f9xHce/6h0la8L4ckl38kcD0dgoVSvOQThU9 2YJc0fmPyvQda++FIl6vjOMDRRI3Pzq1/0urysYKzwHKQanZDXkonZnFumIIh2QZi3iS 5sx0fLeEJkvmoz6yVoxnz8XLdmJC+/IuUvXMKrG69yHJT6Ix/4cPVmvQ5A8lp01JOGsr Xi20QtEk9qzKQ7vAeEfMQiZZV6jQjvwt18MK0RCJmjXov+1dVy+a2WT41XxZTRfqqiCk Ufz314n+yK7VuObA6+tRNgoN70k9KPRWk5+cB0yVaI7zsOHPYljuC9kXcaH71AuiqENg MFdQ== 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id dc6-20020a056a0035c600b0066c72802132si5444797pfb.369.2023.07.08.03.03.34; Sat, 08 Jul 2023 03:03:56 -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 S230115AbjGHI5g (ORCPT + 99 others); Sat, 8 Jul 2023 04:57:36 -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 Content-Transfer-Encoding: 7BIT Content-Type: text/plain; charset=US-ASCII 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 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 --- 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, -- 2.33.0