From patchwork Mon Jul 8 02:51:27 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Miaohe Lin X-Patchwork-Id: 13726146 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 08D7CC38150 for ; Mon, 8 Jul 2024 02:55:59 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 962B86B0089; Sun, 7 Jul 2024 22:55:58 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 912E76B008A; Sun, 7 Jul 2024 22:55:58 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 801916B008C; Sun, 7 Jul 2024 22:55:58 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 61E5F6B0089 for ; Sun, 7 Jul 2024 22:55:58 -0400 (EDT) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id CD3AD8108D for ; Mon, 8 Jul 2024 02:55:57 +0000 (UTC) X-FDA: 82315070754.25.C26C27E Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by imf11.hostedemail.com (Postfix) with ESMTP id 2205040017 for ; Mon, 8 Jul 2024 02:55:54 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=none; spf=pass (imf11.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=1720407333; 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: references; bh=ogUsGwhRcuQub9UaiIJglIERrWI0mER+UYAzyxjeukw=; b=V863Nu0LOaAkLU4FDTfwwEG+IbcQIBfmORl93dliihIwJ2rIMbduaZ/NrEKnryJnKo21+v vtQAzin9rwVRHlyHtzKCFGrYb015zH98bc9iXwQ7vBYNSAkoWsMM8mHO9VTTONpFYyQb6a nJvjaQo/9+gnSjpu7Myubu3Vao07HGE= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=none; spf=pass (imf11.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=1720407333; a=rsa-sha256; cv=none; b=TCn4JV8oOGlMYSSGpjqMPDTy3SEqB0OWAUxuI9ekDtwQtEggLHFkC7E9+VIOtPhg2RZSlb HRoah8k5J929D7iWImL1yIVDLfamOtwPMbPTS26CXpO7RfzJdSd6wJ7K+8X7xzWQbBanbE Hz8yzlhv2KDbrx/Hq4BX43gA/Zo4N2Y= Received: from mail.maildlp.com (unknown [172.19.163.252]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4WHTCJ2sT9zwWJT; Mon, 8 Jul 2024 10:51:08 +0800 (CST) Received: from kwepemd200019.china.huawei.com (unknown [7.221.188.193]) by mail.maildlp.com (Postfix) with ESMTPS id EB7C9180AE6; Mon, 8 Jul 2024 10:55:47 +0800 (CST) Received: from huawei.com (10.173.127.72) by kwepemd200019.china.huawei.com (7.221.188.193) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.11; Mon, 8 Jul 2024 10:55:47 +0800 From: Miaohe Lin To: , CC: , , Subject: [PATCH] mm/hugetlb: fix potential race in __update_and_free_hugetlb_folio() Date: Mon, 8 Jul 2024 10:51:27 +0800 Message-ID: <20240708025127.107713-1-linmiaohe@huawei.com> X-Mailer: git-send-email 2.33.0 MIME-Version: 1.0 X-Originating-IP: [10.173.127.72] X-ClientProxiedBy: dggems704-chm.china.huawei.com (10.3.19.181) To kwepemd200019.china.huawei.com (7.221.188.193) X-Stat-Signature: 1eksjfk8k8gn3683howb3cq95doti3if X-Rspam-User: X-Rspamd-Queue-Id: 2205040017 X-Rspamd-Server: rspam02 X-HE-Tag: 1720407354-640580 X-HE-Meta: U2FsdGVkX19nS6uNFNqRzviZLdeIGd2b+q0Fy+3KLQWVixEWPF6wa5ffKXG2/0syoiMqtYhyEVNKJcBqKv8j3aXJh4HA2OTVV/DD1CfN69PJGanVWWhUjpjRyMm7h86ztb/zxp2iqEVIys7fxI7A/l1iryEW1F64priiUXL6zqCNtUXe23MqRh0Ze5CXBp6UyMlsgmd1b4Tacm50YhK9K0AcNBVBqQu+z9JrgPYD0pdLOALU3F5BZW70vHmIM2jsV4d+LnoQwXuWZ6V1AJ3KVY+U6j6mjpjZuXoaFFKSKum6nMuzc8WglnvsKtXDikHH70v2h6zZL6gGEnZcktbd0gKm4nuZQ/TEW6bE0ht7gWSXAsP36SuVgH8MMlsrjcXzLJr6rrkeHCpXdZBYpPp4hUmUR4P5FY3/nRjfKPIfCl4/XZrB6HSWFsdzip7xeDZxFWNmLEuJgGyxUFBZLEKCiFiA02CWHB9Qlzsqb+/bGHb4+CMBvuE0HTrmE3p2GE1jeGRKmQPqMFln2d8IG2ruDp3rH6xGDePS8USbZaMIlpgfimR+3lAvFesZCwhOoYOY6ZxQ0gTjm0B0jpEXhr1zXpHWm/7i1QkudI2v2jLe/S1SNADanVPCbLNfgoNB9detdpeEVxMqnCGfnCQ0JM+vPlQhe81611c2T51jNKEdCA1wRl16qk8VBlj3AErk4jRp90maNhvYbixv88mXJy0Mjvw8VfkZXOZfpzwjfLG51eD6IYIHKvdqLp1dP5kxFSFYORAWaYhJU7Z/KhPUs1iRAq3aJ38JcLRz2SqoB0Ipg94FlSXIgOU1BlpsMhyjtt4Ca9HaNDqH3Opc89Cy2mm97qMe8/r5riC46AZUclYdmI9Fz92/SGUDzsLn0YQCfwRkTpHCsMnar/fNpexluz7unjlwJ007gUTuU5HoWcg9LZcCNciQIoy2yu9VdXywmY2bgohQU4D/DqqqNFlWF9i 0Qii8nFn eVaOZZSPnJ/sm28SEpiWJvx+Q9TZopicX0DU0ixtxqYkBo/xPI0hxzd4DoyoWqz5W96jjA7ZwP6BJIa5Z+kik0Dv2zoCVtfj9W2O/TjOpLaHOI/8QHhL9pOBi3kHXfdqjHq1oJ36egVk1yYfdaZXH4Ba4R9+m7rlNgnv6cVq65h/EYbyHYPskyyhRxddGA8Aq+9b2netR2KoE0+Igc7zZkx0Iu7DZfj87I4dsusJMTCAKI/7FYW9vBNye/g== 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: List-Subscribe: List-Unsubscribe: There is a potential race between __update_and_free_hugetlb_folio() and try_memory_failure_hugetlb(): CPU1 CPU2 __update_and_free_hugetlb_folio try_memory_failure_hugetlb folio_test_hugetlb -- It's still hugetlb folio. folio_clear_hugetlb_hwpoison spin_lock_irq(&hugetlb_lock); __get_huge_page_for_hwpoison folio_set_hugetlb_hwpoison spin_unlock_irq(&hugetlb_lock); spin_lock_irq(&hugetlb_lock); __folio_clear_hugetlb(folio); -- Hugetlb flag is cleared but too late. spin_unlock_irq(&hugetlb_lock); When above race occurs, raw error page info will be leaked. Even worse, raw error pages won't have hwpoisoned flag set and hit pcplists/buddy. Fix this issue by deferring folio_clear_hugetlb_hwpoison() until __folio_clear_hugetlb() is done. So all raw error pages will have hwpoisoned flag set. Fixes: 32c877191e02 ("hugetlb: do not clear hugetlb dtor until allocating vmemmap") Signed-off-by: Miaohe Lin Cc: Acked-by: Muchun Song Reviewed-by: Oscar Salvador --- mm/hugetlb.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 61847f799b6a..63a2f1132a81 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -1729,13 +1729,6 @@ static void __update_and_free_hugetlb_folio(struct hstate *h, return; } - /* - * Move PageHWPoison flag from head page to the raw error pages, - * which makes any healthy subpages reusable. - */ - if (unlikely(folio_test_hwpoison(folio))) - folio_clear_hugetlb_hwpoison(folio); - /* * If vmemmap pages were allocated above, then we need to clear the * hugetlb flag under the hugetlb lock. @@ -1746,6 +1739,13 @@ static void __update_and_free_hugetlb_folio(struct hstate *h, spin_unlock_irq(&hugetlb_lock); } + /* + * Move PageHWPoison flag from head page to the raw error pages, + * which makes any healthy subpages reusable. + */ + if (unlikely(folio_test_hwpoison(folio))) + folio_clear_hugetlb_hwpoison(folio); + folio_ref_unfreeze(folio, 1); /*