From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796388921; b=LlEWzZgqAtw40K+XFNpaqkcp3zG3kDajycrDmBN3NlN91Z8L8kUlNI5IU3VZFz+EhD78 2rgmfeJLLRLeeqIAx5dkwiHdhgkeegp+yHNpnI0fxr/P2+mGqkWL4UvCv60QsLHMfI671 wlhh6VB/ZdI4OqdE0FH32JSFx66wJ4ZuCkDByJ9PvWZ2gffs/BJ0eq9H9KVYHdHJsCIhR mGGU4Y1/mZgHgDv8hHnuG5Ly30mjm2GfF2ReFRyM2urnl0ZKphWd01MCUmMKUdNKyxt6J tAL3hbKR7gzDpxyu94q01SeNINu8IwDkrULYS6JkT7a8g0oC+eroWvQ5hemGiIgrBPQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796388921; h=Received:DKIM-Signature:From:Date:Subject:MIME-Version:Content-Type: Content-Transfer-Encoding:Message-Id:References:In-Reply-To:To:Cc: X-Mailer:X-Developer-Signature:X-Developer-Key; bh=tCrR9x7KXc/K1KYU1RAuQuk8ahCv7s7bVsM0EWHsW9Q=; b=F2kFQJEfJJ1ZhCRBFlvynhwocYReaK6QDRd4+pI7zSdzsaBkQI8o1cH0420sdETLkI+L X6agDNUlMN9nZoTkY1GYEzfAs+k7+fb1864QQEj3bKa85NbkElfWfDOOHMZ++Mbd0pNuV XX6qvdxB2KBKYZEWym+ubYJw7S24J1aC/b/2CGPlhREFq2wRbjasZ0Gp3TB2aUEwpU7wZ 1oZCTNir8ClP6MFUmQg0b1IXgnzPFzvJuEI1VfV/ud+g71iHtAvWvyKgIRoZsywmZJjx4 VyHVM3I7ygea8udin8Qt/CpSwKPUlyz5N5BdRxBh1JHhD2F/dw25P23fjlToZj7+jZA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=E+C33LPK; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A1D2C606A32D; Fri, 4 Dec 2026 12:55:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=E+C33LPK Received: by smtp.kernel.org (Postfix) with ESMTPSA id 474361F00A02; Fri, 4 Dec 2026 12:55:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796388920; bh=tCrR9x7KXc/K1KYU1RAuQuk8ahCv7s7bVsM0EWHsW9Q=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=E+C33LPKK5kfbnhiQB76tNdywOAkUjFhI0pOXuIlkewOfsD8BNqKCWkfyooNWTMd8 P2MkEneaDrzj2gmVCpyKR0tyG+CazcoA6THnDq+j+CKkWknS8a8mBtLUH1xC5LnIdI QHZf/t1wbAuVc2MLu2D21QYyMIYI1tXhetDndNdWj2v45IoExiI7lxuTiRAt0pkXlg emUhZs4Aw/rTx9Mz82P04hTjXpdZDVXdArsrYvVcMKpMWy3vWuP1qto7+Mx1CunAje VqNGBtAdUbG4zlBurWmMbUQMCI3aXf9H0hWsBszNiA15IJVUzL4B+p3AwQb0B9O92Y IRnjjqKbzUPCA== From: "David Hildenbrand (Arm)" Date: Fri, 04 Dec 2026 13:50:07 +0100 Subject: [PATCH v2 16/31] mm/memory: don't clear a PUD leaf as bad in a zap X-Mailing-List: stable@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20261204-fixes_core_3-v2-16-9aad65727632@kernel.org> References: <20261204-fixes_core_3-v2-0-9aad65727632@kernel.org> In-Reply-To: <20261204-fixes_core_3-v2-0-9aad65727632@kernel.org> To: Hugh Dickins , Baolin Wang , Andrew Morton , Mike Rapoport , Peter Xu , Michal Hocko , Kiryl Shutsemau , Manfred Spraul , Davidlohr Bueso , Cyrill Gorcunov , Zi Yan , "Liam R. Howlett" , Nico Pache , Ryan Roberts , Dev Jain , Barry Song , Lance Yang , Usama Arif , Lorenzo Stoakes , Rik van Riel , Vlastimil Babka , Harry Yoo , Jann Horn , Yang Shi , Zach O'Keefe , Song Liu , David Stevens , Kefeng Wang , Alex Williamson , Suren Baghdasaryan , Dave Jiang , Matthew Wilcox , Miaohe Lin , David Rientjes , Shakeel Butt , Jan Kara , Mel Gorman , Jason Gunthorpe , John Hubbard , Gerd Hoffmann , Vivek Kasireddy , Dave Airlie , Matthew Brost , Joshua Hahn , Byungchul Park , Gregory Price , Ying Huang , Alistair Popple , Hagen Paul Pfeifer , James Bottomley , Felix Kuehling , Alex Sierra , Balbir Singh , Kairui Song , Qi Zheng , Axel Rasmussen , Yuanchu Xie , Wei Xu , Baoquan He , Oscar Salvador , Muchun Song , Dan Williams , Rakie Kim , Hao Li , Christoph Lameter , Roman Gushchin , Sebastian Andrzej Siewior , Clark Williams , Steven Rostedt , Alexei Starovoitov , Xu Xin , Chengming Zhou , Shivank Garg Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-cxl@vger.kernel.org, linux-rt-devel@lists.linux.dev, SJ Park , "David Hildenbrand (Arm)" , stable@vger.kernel.org, Chris Mason , "Jose A. Perez de Azpillaga" X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=3213; i=david@kernel.org; h=from:subject:message-id; bh=Mh6VhoYyoFjZyn++TD7LqB38lALhiINphZDIhVIOIo0=; b=owGbwMvMwCF2/OVpCdfQ3ZWMp9WSGLKFtv1/wrSQ8cBK/lAFr1KL7xzat4Sf6gT8V9bg2tW0f /X5j9krO0pZGMQ4GGTFFFmCgu8bvHy5rrbajY8ZZg4rE8gQBi5OAZjIdAuG/y6fHde5TODKijV6 7vNmls0fux+zlUwercqoTbkeXmZelcDI8OfPH/04MWWRlKUKkf2mZXOuTAu/UJxeJLZi8fqM0E/ JzAA= X-Developer-Key: i=david@kernel.org; a=openpgp; fpr=1BD9CAAD735C4C3A460DFCCA4DDE10F700FF835A A zap can race with a page fault that installs a PUD leaf, for example with device-dax or VFIO huge pfnmaps: MADV_DONTNEED only holds the VMA lock in read mode, and unmap_mapping_range() only the i_mmap_rwsem in read mode. zap_pud_range() reads the PUD entry multiple times without holding the PUD lock. If it first observes an empty entry, but a concurrent fault installs a leaf before pud_none_or_clear_bad(), pud_bad() will be true for the leaf, and pud_clear_bad() will clear it: zap_pud_range() __handle_mm_fault() =============== =================== pud_trans_huge(*pud) is false create_huge_pud() insert_pud() set_pud_at() pud_none_or_clear_bad() pud_bad(*pud) is true pud_clear_bad() Consequently, this leaves a stale TLB entry behind, leaks the folio reference, etc. Worse, a concurrent zap_huge_pud() or __split_huge_pud() can then read the cleared entry, and drop the mapcount and a reference of an unrelated folio. Let's fix it by reading the PUD entry once, and by reading it again only after we split the leaf or failed to zap it. Skip a PUD leaf just like an empty entry. Note that, in contrast to zap_pmd_range(), there is no need to retry if we find a PUD leaf after we split the leaf or failed to zap it: there is no collapse that could move mapped PTEs into a PUD leaf, so such a leaf was installed by a concurrent fault. Keep falling through after splitting a PUD leaf, so we would zap any page table that a split might install in the future. This has medium urgency, as it can result in a use-after-free, but requires PUD mappings, such as device-dax or VFIO huge pfnmaps. Fixes: a00cc7d9dd93 ("mm, x86: add support for PUD-sized transparent hugepages") Cc: stable@vger.kernel.org Reported-by: Chris Mason Reviewed-by: Jose A. Perez de Azpillaga Signed-off-by: David Hildenbrand (Arm) --- mm/memory.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/mm/memory.c b/mm/memory.c index b794444db449f..60e07ab3306bc 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -2147,21 +2147,27 @@ static inline unsigned long zap_pud_range(struct mmu_gather *tlb, unsigned long addr, unsigned long end, struct zap_details *details) { - pud_t *pud; + pud_t *pud, pudval; unsigned long next; pud = pud_offset(p4d, addr); do { next = pud_addr_end(addr, end); - if (pud_trans_huge(*pud)) { + pudval = pudp_get(pud); + if (pud_trans_huge(pudval)) { if (next - addr != HPAGE_PUD_SIZE) split_huge_pud(vma, pud, addr); else if (zap_huge_pud(tlb, vma, pud, addr)) goto next; + pudval = pudp_get(pud); /* fall through */ } - if (pud_none_or_clear_bad(pud)) + if (pud_none(pudval) || pud_trans_huge(pudval)) continue; + if (unlikely(pud_bad(pudval))) { + pud_clear_bad(pud); + continue; + } next = zap_pmd_range(tlb, vma, pud, addr, next, details); next: cond_resched(); -- 2.56.1