From nobody Tue Nov 24 11:35:58 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.50 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1794655561; b=5lrplI2fQSRWkesfvK38pqRIorZ0qM1iydVoCOUHHWFmw7KVtIpqyCG9eW0Yt/EePz4V dLaSBIe/f/ZS8Q1Nvo1G44OXI6CopnG3jBz1W+yqpr3ToEQTvRQUhN130shgGfsCGtrE9 LbEHc0CfEuov/dVIfHaHvRr/3JKx7EjoceiE3Qbwrm/ES8rDX46xHJPH1D2dLdyFhDVMC cpFcEdj4TsUfYOkB5YUX/7IG/Iorij47bgcishTx+8zAbX+eQ9cXeYj+E9IbuP6UpZYGf 2BiyFUANrxOW00PtlcVfgxuobRvTOeHCWaovTM6n/IpzsH24PPt6YNcFPYQOAqM0A5Q== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1794655561; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-ID:X-Mailer:In-Reply-To:References: MIME-Version:Content-Transfer-Encoding; bh=Ik3cMVEgluWCZTmAy6XC89hjzBK/NOOgQFoYH5sRUdQ=; b=N6SHm+x5Pw5oHkUT8xRIzsc0Ejvi2IEhYf0ZbUtOLYcKpKZlJcf/sBUgRA4oNB6ryG+z 7spD8nFCyGuPTN1wdeD1efmxGfFS+QqmHfuaj4hEmTGusn3MEZsd9pZ57dW6WVVfReta8 F0hOQ1OJw9aXtHzVoSz0o26G39cOJaskXirMUHBTOn++z11opsNCAFJwMotGTaypgYqz0 x9gSW76CA7iDJr/uNl6NkLPcYzNyYNe+3Zfp1VLauwfpVuYwMccJgRREBdZGVA1rIuBYJ zqSnlOQQpEiDDKG51GTSDjPnW15S5NIRNlTUmwA2bHQrgRwb69jXKQ47OS7qTx8wLLA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=bytedance.com; spf=pass smtp.mailfrom=bytedance.com; dkim=pass header.d=bytedance.com header.i=@bytedance.com header.a=rsa-sha256 header.s=google header.b=A0QRxa8C; arc=none smtp.remote-ip=74.125.82.50 Received: from mail-dl1-f50.google.com (mail-dl1-f50.google.com [74.125.82.50]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B3FD3606A323 for ; Sat, 14 Nov 2026 11:26:00 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org B3FD3606A323 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=bytedance.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bytedance.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bytedance.com header.i=@bytedance.com header.a=rsa-sha256 header.s=google header.b=A0QRxa8C Received: by mail-dl1-f50.google.com with SMTP id a92af1059eb24-1b3a05f8b5bso2278094c88.1 for ; Sat, 14 Nov 2026 03:26:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1794655560; x=1795260360; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=Ik3cMVEgluWCZTmAy6XC89hjzBK/NOOgQFoYH5sRUdQ=; b=A0QRxa8CuVSRxDlN0s8i+K5NVUwAn8ZVpkikQ3XOScT4HKboMnq8mAr/kF2pVDWDSc F01rFqtrZL3wOYfCjWFhc/mgJkNz7WS1K7TwCkeEXa3JU7AxsQET2ciwU+UnaXLPEJqs o8dtsf/2Wlhuczzjn0sPPKQYkkFIHOhhUrWm/MiRK5bTkXyGAT051Q0VQSifWcGgs+X7 fKhRQi6dshPRXerht1zK8y8LgAQwtKxRGkkLrq+KJSs75EQkRLa2jjG4x1WkS/M5gncG +0xf/+6WOYmr5I3fCH74BIcW3N3mYtom+bCVdmH847PhIVth+PpfF0/vm5f7Ut34edz5 fXPA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1794655560; x=1795260360; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=Ik3cMVEgluWCZTmAy6XC89hjzBK/NOOgQFoYH5sRUdQ=; b=hC0Au3Xx7hISu/4S+zPsh/TxZOU2etAbqOM6Vi7QF46MhzPYLInvCdBicTojk4d0+o w/3sKK/Y8Dk6AYN4/L70DknfdBKJ/t4JAcurwDaDAuH49VXSzVSn3au3cVsBCzBIdL6M WlkwQQfGQ5Eh6M/vspRW+jgoo98JlgQX0VdlW339dGvNZ2xBwhD4HdgI9TzNyNLVmJPp LhvF7wiSZwgoJz1dWOIJITIP4rIJgVgcAKVHl3Im/l6ET6abnUuCnwBg2YPEdDdi2cdO 50gH/aDPPy0Hoe0EMFLBmO6cvv0dBV10WkPlRMu+C10hOS2oBg4feSW8MhnPa++hBRLL uaLg== X-Forwarded-Encrypted: i=1; AL+hErEaNNceXAHOoiYFpq7bwZJKhVzKkrFO8Fro/VARX7uZYLBXaHA8V5gOYjEd4YVob7EKr5P6khlXXq0u0ns=@vger.kernel.org X-Gm-Message-State: APuU4Z+8tUu0DXq9OLHUTl5Q3ipcyfYIc2riJgwNgKgLoNNR+57HDUuY Yi1XtZt8em8Vzw6dt701wlkpbSllp3neQw9zjJsvQpa2wpQN69NbA8VGSrZmNqeHv0s= X-Gm-Gg: AYBFou2NiWmVIOCtloEwlkE1obZGAmgIjMlCQ5VRdeZ8zTqbDcqQfF4ukqfggKXnXe0 6ehwbMIVIzEO1oXtT2y/W9r2mwkelnjoNxAOcgQ136ORuue097AWjSCS8aeMMn6E+GJ26eHbGqV 86/ZdTJ4S5IxzorNablGN85PAGUfQ6zttV4/fn67LTZBdyWbKl14x9rzDqBLIuYvzT66CIWqmjN KuXxRLpla75tCEumRkEgPpagjW1Y3afdmwm4gzUfuaZJuQtQQhQyxj05m886SVp3mlyzJS9seCr /RU3xDTa/l97a1TdmwyWL5aRV1I9w4+b9+8oYyxWH4+k1rkkU1R/pjaLBoMBCDLMubpG81U69nb El1xTwrnHtuJuzEYW/SU83ZqccQFethNw9RzD13bpoUMU3TQiDr7baXzXh9EOSm2V0ZZSQpdar4 mHfaW6qnbdzt8aqgCCZKo5lM4otRGLhSMDYITBsH2w1K9QAsl0UUKZnd/dGFUTvrEnzL6JslI+n HxyrIPU8sTJSO/5hRfBP3AuYI/Py4k728WY7KuDa1B5RoBtJEyw2HSyMOdCHRHs+FgrQDwROHK2 hbTi7xRc4pAKPgcFMBUA4s5y89bZvDhAlJk1rghLEzNdU6A1DFRWabyOj9Oy X-Received: by 2002:a05:7301:713:b0:376:ce6a:a499 with SMTP id 5a478bee46e88-379684ae563mr4808021eec.36.1794655559588; Sat, 14 Nov 2026 03:25:59 -0800 (PST) Received: from G6L4RL2QG9 ([2409:8a00:1832:6900:9d36:3b30:f4a3:7b5f]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-379553316fdsm16190627eec.5.2026.11.14.03.25.50 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Sat, 14 Nov 2026 03:25:59 -0800 (PST) From: Muchun Song To: Oscar Salvador , Andrew Morton , Miaohe Lin Cc: Muchun Song , David Hildenbrand , Mike Rapoport , Naoya Horiguchi , Joao Martins , Lorenzo Stoakes , Sashiko , Jiaqi Yan , "Jose A . Perez de Azpillaga" , linux-mm@kvack.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org, Muchun Song Subject: [PATCH v3 1/3] mm/hugetlb_vmemmap: fix HWPoison state races during optimization Date: Sat, 14 Nov 2026 19:25:21 +0800 Message-ID: <20261114112523.75208-2-songmuchun@bytedance.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20261114112523.75208-1-songmuchun@bytedance.com> References: <20261114112523.75208-1-songmuchun@bytedance.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" HVO copies the vmemmap head page before remapping it to a newly allocated backing page. Memory failure can update the old head page after the copy but before the remap's TLB invalidation completes: CPU 0 CPU 1 init_new_hugetlb_folio() __hugetlb_vmemmap_optimize_folio() vmemmap_optimize_pte() copy_page(old_head, new_head) try_memory_failure_hugetlb() get_huge_page_for_hwpoison() hugetlb_update_hwpoison(old_head) set_pte_at(..., new_head) flush_tlb_all() free_vmemmap_page_list(old_head) The raw_hwp_page linked to the old head is then leaked and the folio's HWPoison state is lost when the obsolete backing page is freed, allowing the poisoned huge page to be allocated later. Unpoisoning has a similar race: unpoison_memory() can free the raw_hwp_page and clear PG_hwpoison from the old head after it is copied, leaving the new head with stale HWPoison state and a freed pointer. Serialize HVO remapping against HWPoison updates and removal. This ensures that an update is either copied to the new vmemmap head page or applied after HVO installs the new mapping and completes the TLB flush, and prevents unpoisoning from modifying the old head while HVO installs its replacement. Fixes: 11aad2631bf7 ("mm/hugetlb_vmemmap: remap head page to newly allocate= d page") Cc: stable@vger.kernel.org Signed-off-by: Muchun Song Reported-by: Sashiko Closes: https://sashiko.dev/#/bug/linux-18e93f17-529b-4d84-ba44-a41829f4be64 Reviewed-by: Jiaqi Yan Reviewed-by: Jose A. Perez de Azpillaga Acked-by: Oscar Salvador (SUSE) Reviewed-by: Miaohe Lin Tested-by: Andrew Morton --- v3: - Serialize unpoison_memory() against HVO head remapping (suggested by Sashiko) - Reword the subject and commit message for the extended race coverage - Collect Reviewed-by from Jiaqi Yan and Jose A. Perez de Azpillaga --- mm/hugetlb_vmemmap.c | 10 ++++++++++ mm/hugetlb_vmemmap.h | 24 ++++++++++++++++++++++++ mm/memory-failure.c | 3 +++ 3 files changed, 37 insertions(+) diff --git a/mm/hugetlb_vmemmap.c b/mm/hugetlb_vmemmap.c index ba8e11e399b2..79ca68dff635 100644 --- a/mm/hugetlb_vmemmap.c +++ b/mm/hugetlb_vmemmap.c @@ -15,6 +15,7 @@ #include #include #include +#include #include =20 #include @@ -41,6 +42,8 @@ struct vmemmap_remap_walk { unsigned long flags; }; =20 +DECLARE_RWSEM(vmemmap_remap_rwsem); + static int vmemmap_split_pmd(pmd_t *pmd, struct page *head, unsigned long = start, struct vmemmap_remap_walk *walk) { @@ -333,6 +336,9 @@ static int vmemmap_pteify_folio(const struct folio *fol= io, unsigned long flags) * responsible for freeing the pages left on the @vmemmap_pages list, even= on * failure. * + * Note: The caller must hold vmemmap_remap_rwsem. If TLB flushing is defe= rred, + * the lock must remain held until the final TLB flush. + * * Return: %0 on success, negative error code otherwise. */ static int vmemmap_optimize_folio(const struct folio *folio, @@ -348,6 +354,8 @@ static int vmemmap_optimize_folio(const struct folio *f= olio, .flags =3D flags, }; =20 + rwsem_assert_held(&vmemmap_remap_rwsem); + VM_WARN_ON_ONCE_FOLIO(folio_ref_count(folio), folio); =20 if (!vmemmap_optimizable_order(folio_order(folio))) @@ -536,6 +544,7 @@ void hugetlb_vmemmap_optimize_folio(const struct hstate= *h, struct folio *folio) { LIST_HEAD(vmemmap_pages); =20 + guard(rwsem_read)(&vmemmap_remap_rwsem); __hugetlb_vmemmap_optimize_folio(h, folio, &vmemmap_pages, 0); free_vmemmap_page_list(&vmemmap_pages); } @@ -588,6 +597,7 @@ void hugetlb_vmemmap_optimize_folios(struct hstate *h, = struct list_head *folio_l =20 flush_tlb_all(); =20 + guard(rwsem_read)(&vmemmap_remap_rwsem); list_for_each_entry(folio, folio_list, lru) { int ret; =20 diff --git a/mm/hugetlb_vmemmap.h b/mm/hugetlb_vmemmap.h index d057e8b12bf1..8b28063d78cc 100644 --- a/mm/hugetlb_vmemmap.h +++ b/mm/hugetlb_vmemmap.h @@ -9,9 +9,12 @@ #ifndef _LINUX_HUGETLB_VMEMMAP_H #define _LINUX_HUGETLB_VMEMMAP_H #include +#include #include =20 #ifdef CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP +extern struct rw_semaphore vmemmap_remap_rwsem; + int hugetlb_vmemmap_restore_folio(const struct hstate *h, struct folio *fo= lio); long hugetlb_vmemmap_restore_folios(const struct hstate *h, struct list_head *folio_list, @@ -20,6 +23,16 @@ void hugetlb_vmemmap_optimize_folio(const struct hstate = *h, struct folio *folio) void hugetlb_vmemmap_optimize_folios(struct hstate *h, struct list_head *f= olio_list); void hugetlb_vmemmap_optimize_bootmem_page(unsigned long pfn, unsigned int= order); =20 +static inline void hugetlb_vmemmap_write_lock(void) +{ + down_write(&vmemmap_remap_rwsem); +} + +static inline void hugetlb_vmemmap_write_unlock(void) +{ + up_write(&vmemmap_remap_rwsem); +} + static inline unsigned int hugetlb_vmemmap_size(const struct hstate *h) { return pages_per_huge_page(h) * sizeof(struct page); @@ -59,6 +72,14 @@ static inline void hugetlb_vmemmap_optimize_folios(struc= t hstate *h, struct list { } =20 +static inline void hugetlb_vmemmap_write_lock(void) +{ +} + +static inline void hugetlb_vmemmap_write_unlock(void) +{ +} + static inline unsigned int hugetlb_vmemmap_optimizable_size(const struct h= state *h) { return 0; @@ -69,6 +90,9 @@ static inline void hugetlb_vmemmap_optimize_bootmem_page(= unsigned long pfn, unsi } #endif /* CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP */ =20 +DEFINE_LOCK_GUARD_0(vmemmap_remap, + hugetlb_vmemmap_write_lock(), hugetlb_vmemmap_write_unlock()) + static inline bool hugetlb_vmemmap_optimizable(const struct hstate *h) { return hugetlb_vmemmap_optimizable_size(h) !=3D 0; diff --git a/mm/memory-failure.c b/mm/memory-failure.c index 78f0a13349cb..6a7b7be9928d 100644 --- a/mm/memory-failure.c +++ b/mm/memory-failure.c @@ -69,6 +69,7 @@ #include "page_alloc.h" #include "internal.h" #include "memory-failure.h" +#include "hugetlb_vmemmap.h" =20 static int sysctl_memory_failure_early_kill __read_mostly; =20 @@ -2038,6 +2039,7 @@ static int get_huge_page_for_hwpoison(unsigned long p= fn, int flags, bool count_increased =3D false; int ret, rc; =20 + guard(vmemmap_remap)(); spin_lock_irq(&hugetlb_lock); folio =3D page_folio(page); if (!folio_test_hugetlb(folio)) { @@ -2812,6 +2814,7 @@ int unpoison_memory(unsigned long pfn) =20 ghp =3D get_hwpoison_page(p, MF_UNPOISON); if (!ghp) { + guard(vmemmap_remap)(); if (folio_test_hugetlb(folio)) { huge =3D true; count =3D folio_free_raw_hwp(folio, false); --=20 2.54.0 From nobody Tue Nov 24 11:35:58 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.48 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1794655571; b=xXCMkHJ5Zha8OLRXM9vsPExL7QlWPuJA2X5hZzC3bR2Jo4gqar//uPCxxAKWs2/aAVXn MkORCpUa8ungWIz12+HBgeXjUxnxhSb2Z4gGRiZX5N3wRIMjJ2wGUe0jImpJaa0i8CyX0 269Zifx82qTgPxnI+e+u43UnWPIvZQoEFOMl0tUMct16Ly89SnTEZocLqyi7Btq/MW7iZ hG/75/xQ1RelTKYi2XTUDYIE85QCP7JqjVKqmIxN3cV4UFzBqNXg0GHAKmYYa1HwYW3kW gMQNDmEYtv3CBkDOsy0EWunBHTFHWyOKl5Yev3w/D03stdtRtXDmq7dEC1Yct5KUc4g== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1794655571; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-ID:X-Mailer:In-Reply-To:References: MIME-Version:Content-Transfer-Encoding; bh=yojspyjk6M/XCMUXwgemrJOhlA57VD8UrAkmCoHaT+M=; b=NM9irX1ZpVLT/JQMmP9cNyVvygUy+QmeSDn0G2EFmOFH36xtm7SPRJ9TQDIF1OVgguE6 CKFEgVKXApxiYEBKkTxw5Jb9uXwjAFiUdd8nJOJl3X0WvpOw5SwjUp0ZBf5CkpPOnZDbo hf74qIFCsYqH2CjwcDuOMBK1MxFlELA1t52jH6wva7mbmKAsVvmB1LhWhLb0hp+XVGxxI Yh8lPTkVCXAo8DlXHNqedKfJBrprhF3VF+3RrG1mO85lNMw+E0N1PvRHaTa5St5Y+Jxll VMVD51RK9gH4hlVuqkRigkph5mpHNaAnnHA3KcojXujNX8uKX8bBurOvaSWMWLRBIBA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=bytedance.com; spf=pass smtp.mailfrom=bytedance.com; dkim=pass header.d=bytedance.com header.i=@bytedance.com header.a=rsa-sha256 header.s=google header.b=Vmj9pFsE; arc=none smtp.remote-ip=74.125.82.48 Received: from mail-dl1-f48.google.com (mail-dl1-f48.google.com [74.125.82.48]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EE614606A32D for ; Sat, 14 Nov 2026 11:26:10 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org EE614606A32D Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=bytedance.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bytedance.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bytedance.com header.i=@bytedance.com header.a=rsa-sha256 header.s=google header.b=Vmj9pFsE Received: by mail-dl1-f48.google.com with SMTP id a92af1059eb24-15ea896ddc8so2624533c88.0 for ; Sat, 14 Nov 2026 03:26:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1794655570; x=1795260370; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=yojspyjk6M/XCMUXwgemrJOhlA57VD8UrAkmCoHaT+M=; b=Vmj9pFsEQ/S2z5X6V9lWXqXuC0Hz9CHAmf3gNjCbHT0noCdvcf+34B0kFdPCxPnjN9 F1QWs3Xp9i70g62lofYJilwisuB8w3LugZQu3CBY1VK7qtFUOw7hAhbpV6W8hCyVWilG 1sHEQTXC9ikByO5N20N0PUyCHRH3M/REfrjKLEinYEb3eqrWb5+6fZxUDv7DvuamY/Aj 32oVaAQCyiXBmnpGXBk/soYbpVXJopgbwHfMUHdE8yHf/zLPfEJo/0B40hWX8l1Q6Yc8 hKq6y5Vu5x20EGEdRMvx3PSLc6jwjX80D4Ji7/269OWBXX2lnfq4eNXKORMdfZoZ0TFx 2NXA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1794655570; x=1795260370; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=yojspyjk6M/XCMUXwgemrJOhlA57VD8UrAkmCoHaT+M=; b=F0SiN0zWofZcDXFWtYGeIZOlY7QoS5rLyb/UhJzCaWNU9MSikwpJhKidVuZq4MD2Xs uQJIN3Qyq3EaTRXeGsfKbqUVtCNNk98FmQL8k9REntA8xTYQF+TUryc6ZiRbnn1qnrwu QTUCmSJd2SXn9kVnsk2UQNQrkKTzG0PUQmAZNEZIthc5PuMTz/g3eElk7JmH7W3D1bRU wQtcMOLEUbW8KwjkWqREyTFBmIoFKJbs5gr+tSWABkhgpCxwPAuToEc/OeQHL9r1cLb2 Sl3mVMd3dVM746/NKi0uzVoTQPCzarJIweil61iYA827+Ta64j2J1vvWl5q6Anrt9hHG 6QPQ== X-Forwarded-Encrypted: i=1; AL+hErFRbz+ygDTuYcXiK6QjWeOQFxRuJq7ttpMKSBjBIIECCDMtmtv4IYfVbtmi4e+96+K8oZYemPgyJq7v/gc=@vger.kernel.org X-Gm-Message-State: APuU4Z9AnWw1tn2fi4kheEEq+x9NlJ4vJEMEn2krjqavTGyRWuiWZs1u ZlEfOi6dUIcSKUJ1ZlZ5M2TqeDxsiH+KPVmz5ePt0QlsQ8y89n7vSX0+Xn/zlYDM8oc= X-Gm-Gg: AYBFou1xwvt8lkYGKwa6SIVNv6JXflo1BIFdf11+ccpJRvg8x4A3omC9FYzWRD9YYWN i69Lk3Wv7j9ecoZMhItPzWnwI7KeJRfwCgehCMf3+vlas5Yxi6Ndhcwql/eE1NP72i8EL3j3DtI zi2Q/V6WJR050mX1N8k7xgtAorJONekO1MS9hPvN0rHTtPN9hCcJo1VaZtluzkGqz2OnuSGiC3D +27xBniHKVCSEA/FYnUHSvjTtqN5iv3Fgv0Q7Wd+oem02Db+2Kt5HOUFhuegXiFfn/fpwTGDTYo nnyWw4zm9iLHJD5CcQ2/H6N8T34Gg6EcNMqnmIpvHaAX3uLNIooBzJqZXIYaqq4kYePQYyHjsss Yn/u5Fx5FugFL7gQln+Tgobi2ATti7lgdIArAOK9lHn3zgbVJyBSBhNOf0uXSm469W2rZnye8mZ /akan5UG5E18Uz4P9o/XdezMi9faiJe6fQ1wrlpp6TY2OwKevvmSG3xP0f5CJd80zUiCFXX+Hfk g8+gq3IErUDstbfb4hc/8x6bkqcztxaur/G3BngVqG5gbwZ4g677O+7Va0lPR2ZrQH3IRl50hp/ gu/J8Ni+Iri1RkeZkbabGzd/SJRs/uXbeas6bKoH0ZBE/G3EiDIJFPtDL28kpVq15X+rnpgEjj1 mjagAKw== X-Received: by 2002:a05:7300:5609:b0:377:a1c8:a628 with SMTP id 5a478bee46e88-3796676addcmr4689862eec.6.1794655569869; Sat, 14 Nov 2026 03:26:09 -0800 (PST) Received: from G6L4RL2QG9 ([2409:8a00:1832:6900:9d36:3b30:f4a3:7b5f]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-379553316fdsm16190627eec.5.2026.11.14.03.26.00 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Sat, 14 Nov 2026 03:26:09 -0800 (PST) From: Muchun Song To: Oscar Salvador , Andrew Morton , Miaohe Lin Cc: Muchun Song , David Hildenbrand , Mike Rapoport , Naoya Horiguchi , Joao Martins , Lorenzo Stoakes , Sashiko , Jiaqi Yan , "Jose A . Perez de Azpillaga" , linux-mm@kvack.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org, Muchun Song Subject: [PATCH v3 2/3] mm/hugetlb_vmemmap: fix PMD split TLB flush race Date: Sat, 14 Nov 2026 19:25:22 +0800 Message-ID: <20261114112523.75208-3-songmuchun@bytedance.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20261114112523.75208-1-songmuchun@bytedance.com> References: <20261114112523.75208-1-songmuchun@bytedance.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Bulk HVO optimization converts vmemmap PMDs for a list of folios into PTE tables before issuing a single global TLB flush. After publishing a PTE table, a concurrent single-folio optimization can observe it and remap its PTEs before the deferred flush: CPU0 CPU1 hugetlb_vmemmap_optimize_folios() hugetlb_vmemmap_pteify_folio() vmemmap_split_pmd() pmd_populate_kernel() /* TLB flush deferred */ hugetlb_vmemmap_optimize_folio() vmemmap_pmd_entry() /* sees a PTE table */ vmemmap_optimize_pte() set_pte_at() flush_tlb_all() Modern MMUs permit overlapping TLB entries with different mapping granularities only when they translate to the same physical address. In this race, the stale PMD translation still points at the original vmemmap backing range while the new PTE can point at a shared or replacement vmemmap page for the same virtual address. Serialize deferred PMD splitting against PTE remapping with the HVO rwsem. Hold it for write across the split-only pteify batch and its TLB flush, then downgrade it before remapping the folios' vmemmap pages. This guarantees that no PTE remapper can observe a newly split PMD until the stale PMD translations have been invalidated. Fixes: f4b7e3efaddb ("hugetlb: batch PMD split for bulk vmemmap dedup") Cc: stable@vger.kernel.org Signed-off-by: Muchun Song Reported-by: Sashiko Closes: https://sashiko.dev/#/bug/linux-fbdc3410-a4cf-457e-88e6-684380fbadfe Reviewed-by: Jose A. Perez de Azpillaga Acked-by: Oscar Salvador (SUSE) Tested-by: Andrew Morton --- v3: - Collect Reviewed-by from Jose A. Perez de Azpillaga --- mm/hugetlb_vmemmap.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/mm/hugetlb_vmemmap.c b/mm/hugetlb_vmemmap.c index 79ca68dff635..6fba3184356c 100644 --- a/mm/hugetlb_vmemmap.c +++ b/mm/hugetlb_vmemmap.c @@ -311,6 +311,9 @@ static inline int vmemmap_restore_range(unsigned long s= tart, unsigned long end, * is skipped, the caller must flush TLBs after all pteify operations are = complete * before remapping the folio's vmemmap pages. * + * Note: The caller must hold vmemmap_remap_rwsem for write from the first + * pteify operation through the final TLB flush. + * * Return: %0 on success, negative error code otherwise. */ static int vmemmap_pteify_folio(const struct folio *folio, unsigned long f= lags) @@ -321,6 +324,8 @@ static int vmemmap_pteify_folio(const struct folio *fol= io, unsigned long flags) .flags =3D flags, }; =20 + rwsem_assert_held_write(&vmemmap_remap_rwsem); + return vmemmap_remap_range(start, end, &walk); } =20 @@ -400,6 +405,7 @@ static int vmemmap_restore_folio(const struct folio *fo= lio, unsigned long flags) if (ret) return ret; =20 + guard(rwsem_read)(&vmemmap_remap_rwsem); return vmemmap_restore_range(start, end, &vmemmap_pages, flags); } =20 @@ -568,6 +574,7 @@ void hugetlb_vmemmap_optimize_folios(struct hstate *h, = struct list_head *folio_l if (list_empty(folio_list)) return; =20 + down_write(&vmemmap_remap_rwsem); list_for_each_entry(folio, folio_list, lru) { int ret; =20 @@ -592,12 +599,14 @@ void hugetlb_vmemmap_optimize_folios(struct hstate *h= , struct list_head *folio_l break; } =20 - if (!nr_to_optimize) + if (!nr_to_optimize) { + up_write(&vmemmap_remap_rwsem); return; + } =20 flush_tlb_all(); + downgrade_write(&vmemmap_remap_rwsem); =20 - guard(rwsem_read)(&vmemmap_remap_rwsem); list_for_each_entry(folio, folio_list, lru) { int ret; =20 @@ -620,6 +629,7 @@ void hugetlb_vmemmap_optimize_folios(struct hstate *h, = struct list_head *folio_l } =20 flush_tlb_all(); + up_read(&vmemmap_remap_rwsem); free_vmemmap_page_list(&vmemmap_pages); } =20 --=20 2.54.0 From nobody Tue Nov 24 11:35:58 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.182 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1794655581; b=tvX9XHEfWAOZgCN3oNnMzRTzx+H3Eh7y2VoKp4o89nZZRSNPT6aSn174IvQxqKk7voRd gtYqtlexQVjkZA7WxqMMgFQa9CANqSCO3gkRPSgrd+MxGiX8gOlWkhgERTb9JEJhPvs4W DsA9+basBPutw7tX4wsjtIlRXx1FJ21Eyp/mgGjF4Oc98Wk6Lo0RnyZeogauF+YbA+BI4 EAi7h8naxl1noJuTqQmiEeNx/XT/Uu47MHH795i/AGKrhS0XNpVZuAfbVej+zTM96oNkK EXoUHDsl5usRyx7nvo9Hc8eO6qvTb35SLAbj8V95JG+GfvuMEhsJ4MEFDMJOQRsBMPA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1794655581; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-ID:X-Mailer:In-Reply-To:References: MIME-Version:Content-Transfer-Encoding; bh=tmdYvPt7kTC8oms8BNF2txsi8vvN5s8/xh5Ms39wPok=; b=QHBsZFZvBMPrgktztpjODMHyVxhk0MzfO1fHdc8ts4xcjHLy40mjAzZvcZ+OtrfcvNoe fKXDw4SQE25k0tJPA3BvuVMIrXFUbo2g5wK6H82W/B+1L1rJjaLQ7dO62eFUeK8bFKBG3 +AjJnrq7w2icdDoa2eoHfyHlV030ldJRne5GWAk06t1/ZTtGxW+nv7XHpmBj0j8owBkFP XLX5h4/EEceYbNJzndWLZgkb5rHt/C5jjmj0qMMKRroHZREYay+RGpHvK8lCB3LEooHy8 2MkmuqYij+SBeygXyEv6XzEph+vBC4DF8qHCuTkMFJD/KBvVABqKq9c/Z0hj7IBgLzA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=bytedance.com; spf=pass smtp.mailfrom=bytedance.com; dkim=pass header.d=bytedance.com header.i=@bytedance.com header.a=rsa-sha256 header.s=google header.b=j5PF2d+t; arc=none smtp.remote-ip=74.125.82.182 Received: from mail-dy1-f182.google.com (mail-dy1-f182.google.com [74.125.82.182]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8C2E2606A323 for ; Sat, 14 Nov 2026 11:26:21 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 8C2E2606A323 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=bytedance.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bytedance.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bytedance.com header.i=@bytedance.com header.a=rsa-sha256 header.s=google header.b=j5PF2d+t Received: by mail-dy1-f182.google.com with SMTP id 5a478bee46e88-3720addb068so2985094eec.1 for ; Sat, 14 Nov 2026 03:26:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1794655581; x=1795260381; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=tmdYvPt7kTC8oms8BNF2txsi8vvN5s8/xh5Ms39wPok=; b=j5PF2d+tiObrXoiqmds8cCaHxVxgQc7yIm0XnZIXWqe/YdfdyCdbXCm+p2dzr8nDBR FwcLXNauooXs/aySU3Qlhe4yzrq6goLUUyG2L/P58irz2cFqsIyNoHb8Cjq+f/DdbtlP bvGxuWHuLzqKGNa4dFfN9aKKHsnXwp4iAixdfYsfiZOcdS8UkC4mxmwWRUYOWa1Fm46w K7r5ZLd+RxWUgfo+roLkXXKymXb63EdHIeMKJR4vIkp58vmkzfUhJuSDCU/c9/S5CuN3 ZA2RvcMq4f0W/K/jd8Dygvn57oJijCvD3gaE5I60KnXPJB3rmlp8sLWanjFwPzDOZ0o5 gdzg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1794655581; x=1795260381; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=tmdYvPt7kTC8oms8BNF2txsi8vvN5s8/xh5Ms39wPok=; b=RC5P5II1Mogg/PdFwKnOfF9hw5YQHrr7ldwRDrmtPbDqUWbomGYYZpunyO32b4Ce+i I3XUyQBvdq5/IYpOJZ2+As6LGVK0s4HTMiVieJnb5h1sOajIcHoVvhCAqDZLGH2FDWW+ JBn5Hlr5hZ9Co5JRlT5CVX3IkEylQUOq2qp4Q/JeUcdS7gdwLrUfRLJ5neOfwHOmtrVj fERJqA0jPw5umdi4kxniAPJqxKMx2Gng1vZoVyga5In5t9jJFmMoS5YTMOxywBNh+9AU jW4ywI3W2zcstsmaFLfLUEaJMzcZ0TEclh8wz7i7LooXmlhkU+wH5alhgoX0M4n4mH4B i4pQ== X-Forwarded-Encrypted: i=1; AL+hErGnDQurAA3YGRa2FVbn3HfANYkutvl72Nlc9OZsAlxEzzjkukM2qssajAk5vIVh4HSzHATV3QUP+ai7tuo=@vger.kernel.org X-Gm-Message-State: APuU4Z9/9LWdXuAkhNb5b2jKcVI8TZ73MIvsgNaw6XtSZDoP8DZp1qQh XMrBAazZGk5URaESV0mD1m+R9ADLNLMd0nCcMNDfW3Ci6e0zcGNFsIdZrRDjg69oCNg= X-Gm-Gg: AYBFou2vMAvXyDgDd4LfBqZ6LtN+KWQAWbEgfKmCH/QwxmeNm2Ye8vt7ZibAyItQ2JM ZDSZ2cFkQnsDhju1i1om7zvoQ/Q9ASSkany8Bx5jw1Jl9T2FbN6DaluMhDDiupLO9T4a6LMNIJF JB7oTqsA08DcX/fLNKOtGQHMCNdxC+YJPAUQZGrehvRNX2er/B8rFOaLfjKT3iXM012mwkeWmLT qbXwUufEPWMKH6YHI6Q2/Z/EcdUTr6ewYA5GmwJc3VKEmyHlHhvqV/vjNmxkuAtS+oD4OiPOVUj QIusKmlg1bx1BQSW6mzIu+Z9HwLKI13kM2JAdpA6iVgo5EgNrpELZ6E/A7aWo1EfTrb9nbD9nVG D74uVY2bcOMbr18C1/o4sJ3gZ3UGK9mzdwv1dOGH73zXAL15lU4EC6C/FWmsdnIeJ9PtyN7Fgh2 lZ/u8DYvC8j06wxBZvZ9OnvjoytyXEc6eRgG/pUlicsGgNaq8EvnGeH1MXQA9KiSjUkXr8xt9z9 hEU4ZQh9YIYewZcQ5+9kIuCXIcjUaUAMpFC/LVldeyshL7XJBbzRMNIh3DDp3BoJlVmsaWwdWq6 50CuHTkkA/xuQ4up1get9amCbheo8RwPnhPfy5tONf/55iMRKBmnY5kua2XD1twdehuvEUUJCTT ybNJcqA== X-Received: by 2002:a05:693c:881a:10b0:35c:ba31:6fef with SMTP id 5a478bee46e88-37967cdd3c2mr4870211eec.36.1794655580476; Sat, 14 Nov 2026 03:26:20 -0800 (PST) Received: from G6L4RL2QG9 ([2409:8a00:1832:6900:9d36:3b30:f4a3:7b5f]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-379553316fdsm16190627eec.5.2026.11.14.03.26.10 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Sat, 14 Nov 2026 03:26:19 -0800 (PST) From: Muchun Song To: Oscar Salvador , Andrew Morton , Miaohe Lin Cc: Muchun Song , David Hildenbrand , Mike Rapoport , Naoya Horiguchi , Joao Martins , Lorenzo Stoakes , Sashiko , Jiaqi Yan , "Jose A . Perez de Azpillaga" , linux-mm@kvack.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org, Muchun Song Subject: [PATCH v3 3/3] mm/hugetlb_vmemmap: fix deadlock during PMD split Date: Sat, 14 Nov 2026 19:25:23 +0800 Message-ID: <20261114112523.75208-4-songmuchun@bytedance.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20261114112523.75208-1-songmuchun@bytedance.com> References: <20261114112523.75208-1-songmuchun@bytedance.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" vmemmap_remap_range() currently walks vmemmap page tables under init_mm.mmap_lock. If the walk reaches a leaf PMD, vmemmap_split_pmd() allocates a PTE table with GFP_KERNEL and can enter direct reclaim: hugetlb_vmemmap_optimize_folio() vmemmap_remap_range() mmap_read_lock(&init_mm) <-- outer read lock ... vmemmap_split_pmd() pte_alloc_one_kernel(&init_mm) /* GFP_KERNEL */ ttm_pool_shrinker_scan() ... change_page_attr_set_clr() mmap_read_lock(&init_mm) <-- self-deadlock Since commit d5d8b8662e6e ("x86/mm/pat: Acquire init_mm read lock on attribute changes to avoid UAF"), reclaim through TTM can enter CPA, which also takes init_mm.mmap_lock. If a writer is pending, the nested read blocks behind it while the outer read cannot be released, causing a self-deadlock. HVO walks vmemmap ranges whose intermediate page tables remain present. vmemmap_split_pmd() revalidates the PMD under init_mm.page_table_lock before installing the PTE table. Use the lockless page-table walker so the allocation no longer runs under init_mm.mmap_lock. Fixes: d5d8b8662e6e ("x86/mm/pat: Acquire init_mm read lock on attribute ch= anges to avoid UAF") Cc: stable@vger.kernel.org Signed-off-by: Muchun Song Reported-by: Sashiko Closes: https://lore.kernel.org/sashiko-outbox-206969@kernel.org/ Reviewed-by: Jose A. Perez de Azpillaga Acked-by: Oscar Salvador (SUSE) Tested-by: Andrew Morton --- v3: - Collect Reviewed-by from Jose A. Perez de Azpillaga --- mm/hugetlb_vmemmap.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/mm/hugetlb_vmemmap.c b/mm/hugetlb_vmemmap.c index 6fba3184356c..5852b0a1c479 100644 --- a/mm/hugetlb_vmemmap.c +++ b/mm/hugetlb_vmemmap.c @@ -161,10 +161,8 @@ static int vmemmap_remap_range(unsigned long start, un= signed long end, =20 VM_BUG_ON(!PAGE_ALIGNED(start | end)); =20 - mmap_read_lock(&init_mm); - ret =3D walk_kernel_page_table_range(start, end, &vmemmap_remap_ops, - NULL, walk); - mmap_read_unlock(&init_mm); + ret =3D walk_kernel_page_table_range_lockless(start, end, &vmemmap_remap_= ops, + NULL, walk); if (ret) return ret; =20 --=20 2.54.0