From patchwork Wed Jan 22 06:11:51 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liu Shixin X-Patchwork-Id: 13946884 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 D3BD3C02181 for ; Wed, 22 Jan 2025 06:19:00 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 45FC66B0085; Wed, 22 Jan 2025 01:19:00 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 40EF26B0088; Wed, 22 Jan 2025 01:19:00 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2FDAF6B0089; Wed, 22 Jan 2025 01:19:00 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 134576B0085 for ; Wed, 22 Jan 2025 01:19:00 -0500 (EST) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id A74FD8134F for ; Wed, 22 Jan 2025 06:18:59 +0000 (UTC) X-FDA: 83034084798.13.39123E2 Received: from szxga05-in.huawei.com (szxga05-in.huawei.com [45.249.212.191]) by imf24.hostedemail.com (Postfix) with ESMTP id D3731180005 for ; Wed, 22 Jan 2025 06:18:56 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=none; spf=pass (imf24.hostedemail.com: domain of liushixin2@huawei.com designates 45.249.212.191 as permitted sender) smtp.mailfrom=liushixin2@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=1737526738; 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=hobS6Mkv3W/RzLTaNEhOxos4r0+Ge/dtdeLBlypxdec=; b=YzYuwadI4A9phMrAYxfiGC3WODuzuYaGbyHP0njuSIh8zcEKE+Z2BScJAqv3NpFPu31LTL xtT/EXVray87EgHIg3Za+Kfmt/dmqeqFT9F1NT1u+wMkKBqdyJYrUQjkz2aahdiR18boS3 T6NhlzMMpB371uRv3lQ6AYdM+nZkiMk= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=none; spf=pass (imf24.hostedemail.com: domain of liushixin2@huawei.com designates 45.249.212.191 as permitted sender) smtp.mailfrom=liushixin2@huawei.com; dmarc=pass (policy=quarantine) header.from=huawei.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1737526738; a=rsa-sha256; cv=none; b=4STo2S1xhPVAmTCjhrUBXcMv2IH9yH1vyk0m8pP3JH1vBkuo7jDstNVJ4dpiZ3I2lIQ1Pe UKCG+BrImT1aSw4et5dipZNAE/uM92Yd4UJ7/Xjy9gcTJVkQfbmOphtG1pSNA+PmI3DVyc xcm4RPgpiOyPzhGnDX6rgYAsc73thlg= Received: from mail.maildlp.com (unknown [172.19.88.214]) by szxga05-in.huawei.com (SkyGuard) with ESMTP id 4YdDQS2Vx1z1JHlY; Wed, 22 Jan 2025 14:17:52 +0800 (CST) Received: from kwepemg200013.china.huawei.com (unknown [7.202.181.64]) by mail.maildlp.com (Postfix) with ESMTPS id 8CF7E1A016C; Wed, 22 Jan 2025 14:18:52 +0800 (CST) Received: from huawei.com (10.175.113.32) by kwepemg200013.china.huawei.com (7.202.181.64) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.11; Wed, 22 Jan 2025 14:18:51 +0800 From: Liu Shixin To: Andrew Morton , Kefeng Wang , Muchun Song , David Hildenbrand , Zi Yan , Johannes Weiner , "Kirill A . Shutemov" , Nanyong Sun CC: , , Liu Shixin Subject: [PATCH] mm: page_isolation: avoid call folio_hstate() without hugetlb_lock Date: Wed, 22 Jan 2025 14:11:51 +0800 Message-ID: <20250122061151.578768-1-liushixin2@huawei.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Originating-IP: [10.175.113.32] X-ClientProxiedBy: dggems702-chm.china.huawei.com (10.3.19.179) To kwepemg200013.china.huawei.com (7.202.181.64) X-Rspam-User: X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: D3731180005 X-Stat-Signature: gfdtp9pu174uiumdjh16pgd913xstheb X-HE-Tag: 1737526736-507966 X-HE-Meta: U2FsdGVkX1/gsZS+mJ03nhMhmNYWGZPms9bGWeXhAPsINxKtkwdRttSztkhfzPKBplRRpZ0697MvJu4DZa6qQM4zMlqlKAyKeDTs5qsgO/gSwXk/PGHCIFzfkx7C+954J3l2nlL2N6qVwPXFRFJLJue3IF7wVsLHEM10fvVAIIgQnj1q1CP1Ys36K++2CXDnmSTjpyUGKvSrvk0mlUcWn3f0stUeov3NmS+4yS0iX0vuXVOp9F4M4BRLS327kazEmGQGnAwqNOV//624qpe6iZFWlcRwV2gzaLA7dVYF2GOFWKxBOv9UCi1+GX9K+xTKVcc37urFPkY7mjilgC3aps1Wk7MWrWicWwFWFhxeeHHKrdruHvebA2rUiL7qzIzheHyCvcj7nF6BA+Q9FWRBa1Tyy088cK76uYO/Ii8SIyqL5TOZo4A7sWwk06uYyGILOKtt8NfC+i2vsECEkBFftXFfMJUvLMZyi+T6hMtLRwGPph54dWzvd9TWzRxnApoH7J+7E/S7OBUQYQEjqvgkkYsHIUeKe6Y/OpHuZwZZsqJCuUgu8WlWdOgblN7R2TMx4tRrA1hero2e63pX2fVT7vKrpX3ZcemwuXXo1nE9MVIHMA/l1QyGFms1YAa6tGHrQC82wJKNF0qN7gWmyQ/A73+yWkeT+SfHg4WWXGnCkERuAJvHiWeCZPyQ3F+DU06O5Hw8BHCUxMqni/cjfy79LYhttYWXfA2hXIqOdKBleVvjHcdFAWtdfE9WbvwTCI6aXUMDEOXnuxR3kFlzxJn3t630Oj9ivw/E4vb+bdoVmjxiKI/W0Tr+2elq0/OzxllynMGDn+mmbWbxsK9smYiGXKa6NjOkFlzxXZNyrj66LdcJgoyffPUmOgYGx9i50zO+UeRNkTPDYkMQqpqn+QEB3hSElWxnrW7tFj8+0y/AFimaZp6rI3L4G+o00vQB5GSHFOF9KWA6BiryspWtUwQ Z+Lcotvr hHR6mTwhVzZnusqu3+tWtD+rqD4U9QmWOUo2lqWz34drs6JougvXPOPqUXjp9ZqRLsmouqqLxWsPfquAudVkoVCG5T4PfKvRuE/K1iZ2ZNKRWMdApp8ykUfXNx8m4Eb1HD+IUfQWaMwDBmBnMbJ72DmjfvqjeF8L8ucbvDVnz60u55jyp2jr7jIdOXWprsmz/l5Jx 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: I found a NULL pointer dereference as followed: BUG: kernel NULL pointer dereference, address: 0000000000000028 #PF: supervisor read access in kernel mode #PF: error_code(0x0000) - not-present page PGD 0 P4D 0 Oops: Oops: 0000 [#1] SMP PTI CPU: 5 UID: 0 PID: 5964 Comm: sh Kdump: loaded Not tainted 6.13.0-dirty #20 Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.13.0-1ubuntu1. RIP: 0010:has_unmovable_pages+0x184/0x360 ... Call Trace: set_migratetype_isolate+0xd1/0x180 start_isolate_page_range+0xd2/0x170 alloc_contig_range_noprof+0x101/0x660 alloc_contig_pages_noprof+0x238/0x290 alloc_gigantic_folio.isra.0+0xb6/0x1f0 only_alloc_fresh_hugetlb_folio.isra.0+0xf/0x60 alloc_pool_huge_folio+0x80/0xf0 set_max_huge_pages+0x211/0x490 __nr_hugepages_store_common+0x5f/0xe0 nr_hugepages_store+0x77/0x80 kernfs_fop_write_iter+0x118/0x200 vfs_write+0x23c/0x3f0 ksys_write+0x62/0xe0 do_syscall_64+0x5b/0x170 entry_SYSCALL_64_after_hwframe+0x76/0x7e As has_unmovable_pages() call folio_hstate() without hugetlb_lock, there is a race to free the HugeTLB page between PageHuge() and folio_hstate(). There is no need to add hugetlb_lock here as the HugeTLB page can be freed in lot of places. So it's enough to unfold folio_hstate() and add a check to avoid NULL pointer dereference for hugepage_migration_supported(). Fixes: 464c7ffbcb16 ("mm/hugetlb: filter out hugetlb pages if HUGEPAGE migration is not supported.") Signed-off-by: Liu Shixin Acked-by: David Hildenbrand --- mm/page_isolation.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/mm/page_isolation.c b/mm/page_isolation.c index 7e04047977cf..2a38f429defb 100644 --- a/mm/page_isolation.c +++ b/mm/page_isolation.c @@ -83,7 +83,14 @@ static struct page *has_unmovable_pages(unsigned long start_pfn, unsigned long e unsigned int skip_pages; if (PageHuge(page)) { - if (!hugepage_migration_supported(folio_hstate(folio))) + struct hstate *h; + + /* + * The huge page may be freed so can not + * use folio_hstate() directly. + */ + h = size_to_hstate(folio_size(folio)); + if (h && !hugepage_migration_supported(h)) return page; } else if (!folio_test_lru(folio) && !__folio_test_movable(folio)) { return page;