From nobody Tue Nov 24 11:27:45 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=1794389881; b=tf4kX7CwnIpzuRqooGYGTuvsBeLBwyrXsnZf3umIYhuUD8L+HjUh1O4z3XBRUymzsuF0 Hpkr8bI7SXKNcisLtzsIx2Gp32IBWA2YaCZHa73qZd8mFqx3UJBwhf7jo6dCGthoEUbSn 9xn5abo5auHFH0+9hzrUBxv4VZA4ILCP5KBp5McKjP0mbCs3vnoeEGdxRlyUuMdnDL0gN zeSax7dsfOPB55vDeoL6Ut6X4MY3jIH9gfbLpTp8iIKhQdV59SlDBA/x/UB0jxmWUKF08 xfw6xuhsnUgwEsD6IlgZQ0FlGHmjA6bhFvcDrNX1HQnJ/EZ5jTtAVNaUtnGOSBt/leQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1794389881; 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=J/lAuxNghYAit4d32b2tzmDG6oe5SfeXzLViO4wF0VE=; b=DuDnP8aKz96IFNaJumWmfnBEJt4zWuw82YCmDnWIwD0FdgWAHPkit4Nb04Trm5QY/Fqu WFFhTTI4Oyf6wWv0XrFxZ5JcLhz/h7PepHckyH748LreZwmA9cxLamR+tjSvIluyHGCeW U5TkTuCPseFDqnOhdBJJqxLDLx8ldV8FSgrAC2b6zMtXTahZQ3/YtZi9ae8v3Y6OaXf/g kvDlL7lLv3J9Oa9yv8fn7ZV8HZXJJyg7ZsFa5eGp2lBBWrymMwx8tAPZKis7KsFFIMvzr RkDQdif5wyeQVebksSCola8n4E+EUZtFT88fPodQHtVrzNFO6eRULK/1Zylg3ZLF/yQ== 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="S79/0nLL"; 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 3E051606A338 for ; Wed, 11 Nov 2026 09:38:01 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 3E051606A338 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=S79/0nLL Received: by mail-dy1-f182.google.com with SMTP id 5a478bee46e88-3645f0f3fcdso6134738eec.0 for ; Wed, 11 Nov 2026 01:38:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1794389880; x=1794994680; 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=J/lAuxNghYAit4d32b2tzmDG6oe5SfeXzLViO4wF0VE=; b=S79/0nLLr8Y/haTXb45MLdlAHOd6mL8N7KA+6JaBFGEfJ4mJW54lMIw+985zMrpw15 xho9tXRKqDH0xoJ07/wvi+1UOKBHJG4rvfXJEJtxCCNRcRn+QrlwMvNncHZDR7WXUURz pMOxdTIPkset8GgRI7l/+vCKpQBNHGqCJay96g+KtXfDztgyMZjnxEI9p6BEnjFZ0iNn FNwQTHQPzEGViDKdh9krSMfu8V+kH0sTMKgbuw/3H7URheiDpxgPW+GPrrwgC/KmHkuu BVKFUPWP321aWCDg/ilrBmebep9AXj/lratILzAgN8Xb00SAyOyUm4MNPtWa7RPROQyl 7GrQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1794389880; x=1794994680; 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=J/lAuxNghYAit4d32b2tzmDG6oe5SfeXzLViO4wF0VE=; b=Fyc+HsDEgshYjImDKnaGdjT5B0Zb6cZW7eb/Ca7+bpRgV58OnFepKvXoWJTRIW97hb YEGwEPOIeITFYnVdt+CbefyLPPJszb7t6l3Gq4KjRg9u17tkAehP+mJSnvgWNcullKly q+Kz7I4BbcagvhGYGhedlcU4FEnKlzkmx+HV0EUAliv8qKXOSzHy9G4GCWYurhy1vXpg ApZuWhbpJxJrgnLP3mMIFVD/KnM5/osFQKbAPNadyGwYTFaikC3NGjcFqU6BbROVGmfz AkYQ+VryJf/vj0KiCFDiBlNJCTt6gRI8gOMIxuPvmRFJQE29teF7TO3n2AAYIgHaqU+N IDDQ== X-Forwarded-Encrypted: i=1; AL+hErFDibCEQzYDPvucoyF85gQlPRFzI4giTZwV+vd3sM1F6ZL8PHy0teJjoyFISwm2nPalMQY611FcfoFyXg4=@vger.kernel.org X-Gm-Message-State: APuU4Z8ETbdvHCNZ+shtCWwBSxCJpibe3qqZCCimnU72oXw9OQvv3JsP /iAsvp+VWuB/b+jW9VUUyPnAEsUf7a+q2JjF3NsRa47JO8arwT7+yxNgZZ6UNhHdfR4= X-Gm-Gg: AYBFou3zxNGl7bS5+NN7fYOajo3Chu4xrzrli7Zh9C8ZboUzs2UAzWQx/5jl1aafooN S7SC5AuS9PyjvBUzLLYC5xi5VvvbBWGL3rbGRyoA9XZLILMIunD6iZVCPuS5UI415PkBB40GOiH cIbYEhdftHjt9mGawfdPqH3oxFAzLZmNfyFG2ndV4cGMyuukl9o0GOhVcQdgv08BokEVIbit4NZ CtOgZ7DGgpi+mtvAzSEaTz5NYBi9IkXi3B7UGXYO19Gsy0JkPiGgt8j6AIVwVdS/HPPv66CC9UN ytKWwSOy/u43o2UM25yUqAQPHwzFYrwoG4uzyDMmu4YgjrBh/MyH8fClJrmgXkdNe8zuuwCaJUA weEWHtj7WiAh0uB0Ly118gxYKhoO57TUrPRFKfscptEmk8t72h700KIe2tLxw8h0zVXdcPHcn9X 7hjmUl7VM5Pq6dKn2R0Sv51RezSXQDus7gpt2XrYFeRk1q7rLqyhV0CVfpJe1Ua6h/ekfs0HXjW /2BO2GHm+Yziml0lGr8M6vLtluWT9kbfOwe/WXV9BWPRTO+yCPixiaxxSGk6xal7lr703mTv+jF lV5dO+8PAadN2AXXJ7rt/V6aUkyskCxKy1BzyH1JSt6Oq2xmq54afwaOxru/CQ== X-Received: by 2002:a05:7301:2907:b0:369:fdae:c9fd with SMTP id 5a478bee46e88-376850ce224mr3460636eec.13.1794389880052; Wed, 11 Nov 2026 01:38:00 -0800 (PST) Received: from G6L4RL2QG9.bytedance.net ([61.213.176.10]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-375212e9e03sm29016749eec.23.2026.11.11.01.37.56 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Wed, 11 Nov 2026 01:37:59 -0800 (PST) From: Muchun Song To: Oscar Salvador , Andrew Morton , Miaohe Lin Cc: Muchun Song , David Hildenbrand , Mike Rapoport , Naoya Horiguchi , Joao Martins , Dave Hansen , Ingo Molnar , Lorenzo Stoakes , Sashiko , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Muchun Song , stable@vger.kernel.org Subject: [PATCH v2 1/3] mm/hugetlb_vmemmap: fix lost HWPoison state during optimization Date: Wed, 11 Nov 2026 17:37:44 +0800 Message-ID: <20261111093747.50048-2-songmuchun@bytedance.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20261111093747.50048-1-songmuchun@bytedance.com> References: <20261111093747.50048-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. Serialize HVO remapping against HWPoison updates. 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. 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 --- mm/hugetlb_vmemmap.c | 10 ++++++++++ mm/hugetlb_vmemmap.h | 24 ++++++++++++++++++++++++ mm/memory-failure.c | 2 ++ 3 files changed, 36 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..164a02866fd5 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)) { --=20 2.54.0 From nobody Tue Nov 24 11:27:45 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.170 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1794389885; b=tjeSr9ltuA7wgd+43h3nPQ0oBvnNPk67QW8xwQee8/Gi9WnqKF+gR1tO0O91oQhIK4OA dZ+0gBf4u0iX0RE5Wd5TgwMGz680FbM2Kyw5rg5W8NPoj+BVHIzooRPpERYEYqKkwRyiv 4TkPf/yYEg8Gr9Z3PT9EoChUk48QykVCWlwRmfz10Ewr58e8jK1rjXwbMOMQ2Xue2yk+t RA2pGVqU6XYzzO557tc6Dhn4ieueCIiOKsuKqa62EjQ3U091eJQ1nSvNTQ+cJe/R+Ac+c 2nyzjnUAbws22wEV99ZZDb36xvykhCNCj7PIs7xK6TK5DYet+9rnmWKXqH6e6f5A06A== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1794389885; 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=mlI2hSgARAsESdSujlJ/ayVkPK6G1sKTaez08nG/rjk=; b=pUs4JuuzelY/Yj3XhfbzQk8WfD9FmFsF21eutkf98PV7rO3Phaa+ud+jNUjuAEjFDqrR aJn+BAON6h+kBJfn2GUCMiIlxTQwPHYAZpsQZUYEKBkMGEakQpOaQg8yNZLL12x4C4p2i P/1boDP5zQb/psxXy3x3jt9ZSgk02Vn3xGBwuZVSOP+b8YNDbnZx8T0c7e7jXmWK70FK2 BmiwxB880CTuafEY3z6I9c8sXOROM2S5eJttboSntoJwpIZ9RB5ilLzk+g2m4x6PMAEp/ 7JHtRPE2lWLu7n6Ef+IsEv2KFCD5K6vqPCU53b+shPhWjZ4C9ca9z77BXWMM3o+yAng== 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=Zgm48Ofx; arc=none smtp.remote-ip=74.125.82.170 Received: from mail-dy1-f170.google.com (mail-dy1-f170.google.com [74.125.82.170]) (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 4CBF7606A338 for ; Wed, 11 Nov 2026 09:38:05 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 4CBF7606A338 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=Zgm48Ofx Received: by mail-dy1-f170.google.com with SMTP id 5a478bee46e88-36fc0a3fe93so5859651eec.0 for ; Wed, 11 Nov 2026 01:38:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1794389884; x=1794994684; 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=mlI2hSgARAsESdSujlJ/ayVkPK6G1sKTaez08nG/rjk=; b=Zgm48Ofxl21FGBkrlHF5gDrVbftQ+STGBI/4ooxBGqMSJbydCwVl6sCHL5nFtygXi4 uGFNmpJ49L/NbICbOyiM36wpnxVf+MKDQn14BWDQdOYVJxvxfBt785yqWKlAJ5m9daCh H3zBqbdeLM3chDniXlZK/INvjPIv4mpA7Xs+zxEd56A/bHskYVLW+22DVLajn5qtRujz dYo+xyLgFT/M08FIImBdetnP0we98esiK3nmppdvt6ejCNLSqAPlq0+VOGkMgObThUST 2yQKzHIg2zBEbTE+z2oSTU8DUePWZAfk9o+ObEVgzkAcYrw1JrHh0NIjr3j8g/LZBO9A KLlg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1794389884; x=1794994684; 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=mlI2hSgARAsESdSujlJ/ayVkPK6G1sKTaez08nG/rjk=; b=W+7H3Qs24GZ5CUZ23rN50A/7bZntoHN9HsJ7UpIgM0kLd98BzJZuT00ofcqzSM9w7D v/NwukDgClQD2w4wYt7knfpkxQIBj1kY/Rc7zNkpUWBdeNiV62l9bhl1gHCUNv45ijo1 WwvGo5y/E7ZKwoSr1cWK3k+1l+67t6xTmM7CqQfNx0T1/mkGi8tC43G8yag7PBGfI+62 1qDFBpviIRsycs60GpxttTbrxFMGVQ1jau6XohBO2AZhO5+VcuBOenmaIx0wjQc8+wrd V743NDLHHglsiQ5uU9X/SiIUVkgVMCWwwPyK6BytDcaRHA/lj48p5iCX7EQ601w2uVgG DZiQ== X-Forwarded-Encrypted: i=1; AL+hErHkb5DeZdx91yepUEPPeCU6KM+1DSiYMpcN5lpa8IELrWevFu30eK+2p5CqZ7BslO9P6IS0bGRCtlMZVQM=@vger.kernel.org X-Gm-Message-State: APuU4Z+Mxt0JVVu1G4VkRvAvmYOHWmcyfuut2UKjrpzQvNbMNNa/AFl+ QkscPlo3GF/ltc57E5/GYo+1ixbF1CpiWWGrnHoOdD4JjVLlZLluKb5X6DOOhblGO1k= X-Gm-Gg: AYBFou030IEEFPYddY3+lgPgmRe4F7bQKjprcRb/mO9qzsJpq04BeoWaH1KzoplWeDc K0jgY5ldt3HYwZYJqPZQN9h1m7D1V7mTdecG3OGA3nK+3kcgvcpbdQBQj99wBIF4Psb05qL1eAL 3UKEsW1Lx7AWeaEp54vZEraYhRJ9HqEciXY5Zq0u1FEi4DlWb0qnvwFWYo2nlpRCzSK6OQfbOkj 65/9ntIO4GWyMZVA9aJElkxc5pilXZmv7Qd/k/CGHVrdnfiCx2SlwCJ0gEhoFn/UjRE2VceUW/N aLMm9YYPODpd++z9DmYRa4eilCAprWtuw9dCYBwpX/Mxu6+hL6gCp8f47Ysx7BFpwtkDGnQV+Tc gZPmeRUe0aU/fIhl8osi1NlT2jFBP9zARsuVPrHqF/K3pcNHcZXpaJcF+EgA2RvHAaKQ3GDyXQA QpmgCkMcWtvQZXOck+jw1AVNzsZwToC400Pl96WxOGPJ9RPO7ETDhV08OEYckMVk20JKIHK2VNs 7jrfkRg9uT1NMgMmUODGx5TzgTxqBN2KXimiHazw4qYoiehIVKykl9SWWdVSd6jwOXDlFZZMZ1J 29Ld7WLSzEsLPqc0aKoJZSN+qAxBzAJmX+XCytndKRylLAdSYDoJe6E9Is0QAME= X-Received: by 2002:a05:7301:dcd:b0:370:d7fe:6585 with SMTP id 5a478bee46e88-37685c7a46dmr2269843eec.11.1794389884220; Wed, 11 Nov 2026 01:38:04 -0800 (PST) Received: from G6L4RL2QG9.bytedance.net ([61.213.176.10]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-375212e9e03sm29016749eec.23.2026.11.11.01.38.00 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Wed, 11 Nov 2026 01:38:03 -0800 (PST) From: Muchun Song To: Oscar Salvador , Andrew Morton , Miaohe Lin Cc: Muchun Song , David Hildenbrand , Mike Rapoport , Naoya Horiguchi , Joao Martins , Dave Hansen , Ingo Molnar , Lorenzo Stoakes , Sashiko , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Muchun Song , stable@vger.kernel.org Subject: [PATCH v2 2/3] mm/hugetlb_vmemmap: fix PMD split TLB flush race Date: Wed, 11 Nov 2026 17:37:45 +0800 Message-ID: <20261111093747.50048-3-songmuchun@bytedance.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20261111093747.50048-1-songmuchun@bytedance.com> References: <20261111093747.50048-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 --- 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:27:45 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.175 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1794389889; b=C1MMkWvHsvTjAKVi3IZP0OPrFLkEaeQJsZSjhBHx2iC9v4UsWS3k+UdczqV7QSDa/AS5 Wlodfuo493f38Y0KA/sH/NvEL4iTG1oBPP0i1RHNSesflzcNHDBjhjkR10F0nNPCp7wTW tx3VHSYQcmICejWjwxInqx9EvMbkhFdktQVLN90gOz8GDKtkRQ+p/I04Jrqdl2QyzSYfJ +1RtELyk8t7LSIf5KqvVeqyNyPKMzFE3J62eija9CLGyebiVrBOReesj72VJ7wVcCowv0 q5IqTvU1nyk83kZ+DoprUTrBDKQO3n2Golw/O69IejsIuDh9YvdWpyJ2EQEZmDG1ihQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1794389889; 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=nNJ7JQckRH+BEIq6xP+FgdYGraXKkj9NjALFdeEe068=; b=SrINecJmLoOVgHJ1S1cHELmsG+qEpfwWi8XzJxojiD4IZxZHsUk3LDDmFXUq632AJHxA 55FBPocdkoP3ad/kZ4cmnCPfoPDKXokRrfFAEf8q2dBB7s9j5rWN6UYy/3xnB8sPxH7Mr sMzYAEViFjSHUwmWotAB6ftNs2hTeriIoP+i/jv3nZwN3fu4TgLrV3Yr88xH6cqKaEAAI dDrZIgt3RcA7YWnQxGABHziuhoJo4JoRqh/B9XvXFI8RMDGaUjP10oEf8osTiW0S6uWjP mEHaK28/ICS+JcnWw8T8yXJKjvtPtKbrmhwhR0x8l+h7QIntT6Ske3odBN9uvkRE/2g== 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=Hp8TINu+; arc=none smtp.remote-ip=74.125.82.175 Received: from mail-dy1-f175.google.com (mail-dy1-f175.google.com [74.125.82.175]) (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 5E63860A2623 for ; Wed, 11 Nov 2026 09:38:09 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 5E63860A2623 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=Hp8TINu+ Received: by mail-dy1-f175.google.com with SMTP id 5a478bee46e88-37071c0f42aso5165760eec.0 for ; Wed, 11 Nov 2026 01:38:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1794389888; x=1794994688; 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=nNJ7JQckRH+BEIq6xP+FgdYGraXKkj9NjALFdeEe068=; b=Hp8TINu+StVbWUF9STZfwLttRriFG4m/TTYAn77RmTP7oLZkquAPtP7LDofs0l6YaA mXPlO1+FE4H4/APt9LqPOS0nTpcaffuF2gWKMGdl3AfVowWkxWwy52ja0UhNR47UMD+Y 4j40IXj6fEW45aK8lxNu1G/qBPELv/6Hno8gmS4aaanQb5hJaGZ4m4T6cPO54tLdpr0K VsBWhYE12WYodlVzWVPNWM5eoMXBgee/68IdlhFwMsn4fD9WhAhs6Hkpp69nM/2ImCoY kI9t8I/N1m32OIEnHE7hjkAHX7GsKZSJPJnve5ZY2Pif67LTxxy+heRKCrEHX3ZsqrxJ bY8A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1794389888; x=1794994688; 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=nNJ7JQckRH+BEIq6xP+FgdYGraXKkj9NjALFdeEe068=; b=hjM8AjVKmhPW+MP7AHLN6+XzBuUC6lh2LfC4FXjbRkOz8m4y2drtA3VjB4ATv5vOF7 nKWhUdUVDraFeQQS0AM60F4WIzDVHwMy6XBvzQvGhvvxSEVbwNwkkYkSJ/ya67I+3+7U bxEo1RgF3J+j+ZE9y+bPHQLTvgG35YI2PDuHK+AcBtVkwl01qwuEXI+ixI8KPYLdgdWJ wBuLE+EyM9e6V4F48adAJOcRqLhWdi0Ish4b3z1dserBhUTStu0kEkIM0y5P/nE4VHkq UOXNe8NPtcilfaAGydEegSyzHCzTo7wjJ4HJtp916IExkDFy8x48wdlV2AVGkMRBIHTR zYeQ== X-Forwarded-Encrypted: i=1; AL+hErG4cNZiwcFOtFO6VkHGzIRWKBvQ7ysJ5S++v9Uw860V/8XZE9YRg3yqQsi71DZ1HyHcYX829ujXzVy8mkM=@vger.kernel.org X-Gm-Message-State: APuU4Z8ehjHxbL+MEpydHK0BiFnDrrajYcW+wvC6+0AbJVXtt1Ce2DP7 vsJpohOF+2ZcOgMlX1y1oH5V+0rToRPB1s1pRqat7OY0hEVObKxxH+BCWDQhngJ6zds= X-Gm-Gg: AYBFou25YuZLiekong8JR7oBkcGK9ts1tseqDdpywyeeXSvpt4nRIh6NSiKh4CygBDl ruGwF2G9w7M5EaEnLA42cYlSfW5PqRUt29fr9ioC80UBJ15Kagb8JWB4gUwcGjW0u/o4p0xkyyr i16D3IOOFdKLuKx5cxSoWInzVqPqCZnTD1+qy+/HHEmKsrXOlSLGEc/AWCc6YHXTOVPheeVcSDr 2/ZKPJrPgGtyZVoboOOCRKRaR9cQP1WGmr/pb09Nndixc+rDnmOZEq6BEw1ez901RjXMgkG2ZAI e/zPSEjVMIkE1a70ouXuAGCvSHcgtmix48sJ9nTyduvGsqhBgO8tEbKMYKnfsKgujbS2SEj4IF+ 4/NQhx+9u2nKpZs0MzusbmoaQRrZOGSP7Hla0HEQJRNsusuNFGjQR6UZekmWVyHQvXhnqFlDOrU DtHmbc+6v7OmU47DSkLmwcyZ8/4FzljEginVbq+P6HIuSiFGa6Owh6X9o/N0MGkDk4QtfKhccUl v3MixEJnLsqbSo3gzZO6X5dchbJeznulp2UvMOVAciH484xuQSVO5xNNSxlDNiIpWzHovXTxnqz UAQeMeZ/pu49cYYD+vyWnOD23csMrFjnHWrrVb5jGCrwLtMhHJ3tWgKB5k24vA== X-Received: by 2002:a05:7301:4902:b0:376:f63a:cb24 with SMTP id 5a478bee46e88-376f63ad124mr1305017eec.3.1794389888322; Wed, 11 Nov 2026 01:38:08 -0800 (PST) Received: from G6L4RL2QG9.bytedance.net ([61.213.176.10]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-375212e9e03sm29016749eec.23.2026.11.11.01.38.04 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Wed, 11 Nov 2026 01:38:07 -0800 (PST) From: Muchun Song To: Oscar Salvador , Andrew Morton , Miaohe Lin Cc: Muchun Song , David Hildenbrand , Mike Rapoport , Naoya Horiguchi , Joao Martins , Dave Hansen , Ingo Molnar , Lorenzo Stoakes , Sashiko , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Muchun Song , stable@vger.kernel.org Subject: [PATCH v2 3/3] mm/hugetlb_vmemmap: fix deadlock during PMD split Date: Wed, 11 Nov 2026 17:37:46 +0800 Message-ID: <20261111093747.50048-4-songmuchun@bytedance.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20261111093747.50048-1-songmuchun@bytedance.com> References: <20261111093747.50048-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 --- 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