From nobody Sun Feb 8 17:41:35 2026 Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B61EB1C36 for ; Mon, 8 Jul 2024 02:55:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.187 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720407354; cv=none; b=tUfnAC+73beAuscSt5WU/LNykP6mDXMRloCCFez33mQgukBXizDCXtInL96gVc2WWS85uhndayuGHLi5QSAW0hgdjtmmSxgidl6V12IAW1imcH2sMw+K5la+ke8LgflMYexuON/VhGyYhTi77V05gyl19qlHSMqtkClNVPA6jXQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720407354; c=relaxed/simple; bh=TIJFYcwncefs81L64nH97/1r4gDoMr3DB0nk+YkOI3w=; h=From:To:CC:Subject:Date:Message-ID:MIME-Version:Content-Type; b=uUm7xF6c2MAmcL1M7kUkQk4dFAT1AeIA+vflalUQv0CAjrOuTeL+USKSLu6ZujSEpkQHZ3A7wAL1x1Y6Gya4n8T+mPiRGwhsLl/Mp9hTmC8KewpBIgsCeWWaH8fJAxnGtmkCPOF6xrAuKNO7Rrdk/7aUrEfDG0PZLSq7fHbxPL8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com; spf=pass smtp.mailfrom=huawei.com; arc=none smtp.client-ip=45.249.212.187 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huawei.com 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 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: dggems704-chm.china.huawei.com (10.3.19.181) To kwepemd200019.china.huawei.com (7.221.188.193) Content-Type: text/plain; charset="utf-8" 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 v= memmap") 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 h= state *h, return; } =20 - /* - * 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 h= state *h, spin_unlock_irq(&hugetlb_lock); } =20 + /* + * 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); =20 /* --=20 2.33.0