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: 13308138 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id EE65BEB64DD for ; Tue, 11 Jul 2023 05:50:17 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id EC8608E0005; Tue, 11 Jul 2023 01:50:11 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E030B8E0003; Tue, 11 Jul 2023 01:50:11 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7E82C8E0006; Tue, 11 Jul 2023 01:50:11 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 1B4188D0002 for ; Tue, 11 Jul 2023 01:50:11 -0400 (EDT) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id E6A9A40002 for ; Tue, 11 Jul 2023 05:50:10 +0000 (UTC) X-FDA: 80998255380.18.4A8BA58 Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by imf15.hostedemail.com (Postfix) with ESMTP id 5FA5CA000E for ; Tue, 11 Jul 2023 05:50:07 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=none; spf=pass (imf15.hostedemail.com: domain of linmiaohe@huawei.com designates 45.249.212.187 as permitted sender) smtp.mailfrom=linmiaohe@huawei.com; dmarc=pass (policy=quarantine) header.from=huawei.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1689054608; a=rsa-sha256; cv=none; b=5f3b119Sg+rqYGi+m6N6Zzh5BfAARMlJx8J0FqdGPqmey05ksrgNl0veyn+tBUfBGgY1UO 1J1XyYa7fC90P0JSP28qdmt7SJPtbSLHh5h128P/c5CQR6nnCwZYYrCMEsvU/6FfAILHpp Xa4PsAuHTnTRiwwR4t6q+cUI4MTS2kY= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=none; spf=pass (imf15.hostedemail.com: domain of linmiaohe@huawei.com designates 45.249.212.187 as permitted sender) smtp.mailfrom=linmiaohe@huawei.com; dmarc=pass (policy=quarantine) header.from=huawei.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1689054608; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=kZxpc3LLQdKRwi1qZzsu5rtB5wD/2h93r2whajVTytA=; b=fkgT7WlHjfqlYCe3B/5mmvO6iUayixPJq8gMoQcCrfwEAva14FUDSoKbf1yHgbcLpiXj6x nJAWbFgPcLZSMqIdlXpg7+2BrVJL3S6lJn1eW6xFB/s6lfnPKjPXao5roYixP9aTftpX8R rk7zZoEiyHTicbWyHiUqo+V9rWQOiYc= 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-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 5FA5CA000E X-Stat-Signature: g3ihzusrzk38tfqfgqaub4gcofp93p35 X-HE-Tag: 1689054607-985031 X-HE-Meta: U2FsdGVkX1+BMAj3+ttkjXRgDNbI2hL9lV/BN3gRvZLyNlEsW6cu2QyExkbkKzHIhUovSGV8KRDoyl8YV4G0vUQo+gJQae0X4mEmcs/FnK22CGbpQeSzqjfNA3Y5txDVulWOd4TY9zQ0Yz4jGzE/9rBZoOYrdmyRGnfSnR66YU/pWh+mlJeLPY/2Tm3FXUvdbIKSdM3MFYBqcslqDSQfBeEhbSlXYP86WMI2AXxz4i3DkVZZPaQH8RHXnKh4za3uV6NIL+MNyTGkaF+0hB/SNiC3Ii07YjalR3uBWajbnzwubSsyt7udOXHSKX9q3m2cevCO/VD8WJC217XrB9NY1VqtPO0Qh5C3TFh0b/OWXb0b2uG3fTxVc649bupw5dCsrAzveP//tuM8IBotvpgVVUofx69wWtYXgePLOWyQ+zf7zArhLMzl00yFGqgMaFvpqfhA+T43FkxuW+hPOUgtu952ivGrBlIBy9xRfYFmLfpl5aW3hua2wrgetVlVeva73BmWQKKaMHk+fiGdmgWOQd35hMtc4UUldPXl/R0fULKSjjL3l4m936Tdl+aNz3n4vdXtxq5WY0XELeRGr6s7h9CRmm7MtD8I3oGbie37apYkX5FnuT/Zfqjc97Xun63DsNPmpwBrVyEKAqC/QpoEAxvv0dsBpXYfEoIXb3QecNm92eAk88RmrhTqbAOi+fC7iSNIhZoaMqytYzXHN8e8h+Iv1pU9hrHzOsCthnZY6WG/KNlwuuKYYfqAVPVzjXl3lCPSTZ0zuS8Qjzu9cyPpEL24n6qXDNSiTtCYB3BbhpWJpqsodv3fGyxQpcSbFNdcJeSIyHGazIDeXJMCNDnCoDxZPb91HE65edbF6ReaYOdsr5R65OE0CJTmXZFsbUor52UcpaxqiaieHqxLNpR+G4sD+2100FQ/i0jdSX1ol7zhbFQ05l0/Sc89MFptplpTnn9TgHKWi++ctpWl7le iabKKgSi MOazzqCdpbCtPAn8zejnKtVeKwt9fj/Tle18lcIF1TKsvvfEnPD9eGa+TiUX4U/rluJlbGt/D/SYMTdc50lq0hK6KXY8PLIwX7mABx5zXNyUMkLROWtdEp6nQAJ7pr0wANOwqvPsAtkaganaE5Gn5ivfxpsqh4cZFaVjaVkf0c2xPK3Y= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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,