From patchwork Wed Jul 10 08:14:45 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Miaohe Lin X-Patchwork-Id: 13728994 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 A0753C3DA42 for ; Wed, 10 Jul 2024 08:19:18 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 10D8E6B0082; Wed, 10 Jul 2024 04:19:18 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 0BE356B0083; Wed, 10 Jul 2024 04:19:18 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EEE046B0085; Wed, 10 Jul 2024 04:19:17 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id D10986B0082 for ; Wed, 10 Jul 2024 04:19:17 -0400 (EDT) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 4E09A81BFF for ; Wed, 10 Jul 2024 08:19:17 +0000 (UTC) X-FDA: 82323143154.03.08AEDDD Received: from szxga04-in.huawei.com (szxga04-in.huawei.com [45.249.212.190]) by imf04.hostedemail.com (Postfix) with ESMTP id D2C154000C for ; Wed, 10 Jul 2024 08:19:14 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf04.hostedemail.com: domain of linmiaohe@huawei.com designates 45.249.212.190 as permitted sender) smtp.mailfrom=linmiaohe@huawei.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1720599530; a=rsa-sha256; cv=none; b=lngwllzFZa4Qj80ZmQZnUpZBBI+64dAdnIpurdKspDlqKhLGLZDIeg6v2Ud1R7yilHuM6f rIJVyywSM3ngt/XlSTpS5VnEZRGKOwBlbbXFj4tM8zzrHzAJl+sdsWB33xCufqEjx3StBQ CuieSi8UNYv93yPA+IWzDLjthD++/Lk= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf04.hostedemail.com: domain of linmiaohe@huawei.com designates 45.249.212.190 as permitted sender) smtp.mailfrom=linmiaohe@huawei.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1720599530; 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=uTJGJXmBvQXnc+JAfphaaS0C9aFfIU04Vk06v7vTxiw=; b=V/LMgLXbSrV270tfCmnqNJgMPJsxEV8I8z7+SdyUBkvy03m8Ra8cMi4LZPPae/uPfXds0W B7ezK9A4Q7L3SIiTH2Mu+kEUmyCor5ViYrWULREjHqifuRa1w69WkeVGwDJOtoEjnt+Um6 qPnaVdAbsakYPe9dATYxFn8QmO/EGTY= Received: from mail.maildlp.com (unknown [172.19.88.214]) by szxga04-in.huawei.com (SkyGuard) with ESMTP id 4WJrJ21Npjz2Cl9C; Wed, 10 Jul 2024 16:14:58 +0800 (CST) Received: from kwepemd200019.china.huawei.com (unknown [7.221.188.193]) by mail.maildlp.com (Postfix) with ESMTPS id 852E41A016C; Wed, 10 Jul 2024 16:19:10 +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; Wed, 10 Jul 2024 16:19:09 +0800 From: Miaohe Lin To: , CC: , , Subject: [PATCH] mm/hugetlb: fix potential race with try_memory_failure_hugetlb() Date: Wed, 10 Jul 2024 16:14:45 +0800 Message-ID: <20240710081445.3307355-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: dggems706-chm.china.huawei.com (10.3.19.183) To kwepemd200019.china.huawei.com (7.221.188.193) X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: D2C154000C X-Stat-Signature: zc3fx3yuywarfn1gb6mm8budgjpnftoz X-Rspam-User: X-HE-Tag: 1720599554-450760 X-HE-Meta: U2FsdGVkX19ceVaV1TvMthY3PnCKjyTqrJu9jBHGDRIiyfx/jyjAzefT+t8XFJewIjKJvyhg9kdfTfxawmHzApD3tbEk08khOIN+YpiDyI4Pi65M+aecFybzUfd5QIea4Y6zcTWfCWjXuVK8p+/hnZNoQrE2laD1XiOKz+Qu+2aFILW2tu9R+u6BZGH9TlL0/OwssRzOQ31FWo7DcWxBOFkkZ2likS9gp0C4rs2ns3a6rFKBEWDyMPygp7ul7ai3bk2VjrYY5uW9XZibcxwU30LmkHwKZFM9g3/yQ+xbdWADwWzqvFw1NEmN8yonEk6elTyT00mJbwwtNlGbAfuugDf0GchxnJHbPJEVBpvwGyWVo93+y5FLPNdP10u3obY/1nEZEf4W7S0emKPAWFPrHk+T2fZc/e09+QF7mrBKOuFiRwf/S0fJUwAwh7Bge4M/LUg4TzkNaTOme8w9bgDLPYLtK+fvjHXNd0LeQ7UCs8jEIjq3zq4N2bffLRzNeV4NFyJJVXr21Drv+b1xZfD75GfuQayymfZGiXtdo7NcbvI6eCLcBnIFp0Zfjthw+ggYWAsmQ3fKxXihVKu2djeTEgNAoq2L0Y9Hhgzo0TvJhiWmjnTEqF5+TVT5SJw27GBHQYV/Xq4nlI/FsZ7ijAufn9Q+8QMXmKi35r9AixupAPwWdcWjY12izm1tJZSnOSbvC4yFJs4ioJs9CI3ts2fI7VY9uoez267Pbw3yXyqSK1elEOjEZ1V/SnH1tZ72KktEB3QRv43+2PFCEeb3M3usKN8RFRY46I49m6ZeKSHfbpQcGVp8QMogQLzS+ybecFjHc8z3M0Zuwt76l1ksCQE2nHVGol5H72UdYlgMCsrapolKs3LVhxWSrNPxDfBC7Js90n/j+ZaPVf0NS877H5nh2pb/OrHsClnk4ckR0Q+otHYskBhRt/gkWG8iY4CXWc+xgbodco+lkEOGTwq9Ems DidX7ubd Ej4dqW3oNRqSy9tbMiJhg4AjRUnNZCpISy4La0EfGqHqd4s9wC/2JRwTP6XPvckMIAyKZZMMSpr92nD7LnsLPT96Eq9U7SXx8k3UyuMR9tU4mPi7FMDobc0nDdcJ3VbmIaQfLjh4B7hKh8iKv7tKGwaLBUmMtI2wQwSMw5Afn1w/unazBV8UsT2lhuXVJRQHVMJaqIzdw1GHkDUI7dzL1HermkfHkbn8Q1X4AQVcPmteERoPJxBbCjeUSEQ== 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 spin_lock_irq(&hugetlb_lock); __get_huge_page_for_hwpoison folio_test_hugetlb -- It's still hugetlb folio. folio_test_hugetlb_raw_hwp_unreliable -- raw_hwp_unreliable flag is not set yet. folio_set_hugetlb_hwpoison -- raw_hwp_unreliable flag might be set. 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 pages will hit pcplists/buddy. Fix this issue by deferring folio_test_hugetlb_raw_hwp_unreliable() until __folio_clear_hugetlb() is done. The raw_hwp_unreliable flag cannot be set after hugetlb folio flag is cleared. Fixes: 32c877191e02 ("hugetlb: do not clear hugetlb dtor until allocating vmemmap") Signed-off-by: Miaohe Lin Cc: --- mm/hugetlb.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 9155144a654c..3d65b68cf78f 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -1705,13 +1705,6 @@ static void __update_and_free_hugetlb_folio(struct hstate *h, if (hstate_is_gigantic(h) && !gigantic_page_runtime_supported()) return; - /* - * If we don't know which subpages are hwpoisoned, we can't free - * the hugepage, so it's leaked intentionally. - */ - if (folio_test_hugetlb_raw_hwp_unreliable(folio)) - return; - /* * If folio is not vmemmap optimized (!clear_flag), then the folio * is no longer identified as a hugetlb page. hugetlb_vmemmap_restore_folio @@ -1739,6 +1732,13 @@ static void __update_and_free_hugetlb_folio(struct hstate *h, spin_unlock_irq(&hugetlb_lock); } + /* + * If we don't know which subpages are hwpoisoned, we can't free + * the hugepage, so it's leaked intentionally. + */ + if (folio_test_hugetlb_raw_hwp_unreliable(folio)) + return; + /* * Move PageHWPoison flag from head page to the raw error pages, * which makes any healthy subpages reusable.